Skip to content

Commit d821e8d

Browse files
committed
Pass SUDO_TERM_OFLAG to sudo_term_raw() when sudo output is piped.
This fixes a problem with "stair-stepped" output when the sudo-run command's output is piped to another program and the command reads input from the terminal. --HG-- branch : 1.9
1 parent 9cdebcf commit d821e8d

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

src/exec_pty.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ struct monitor_message {
5656
TAILQ_HEAD(monitor_message_list, monitor_message);
5757
static struct monitor_message_list monitor_messages =
5858
TAILQ_HEAD_INITIALIZER(monitor_messages);
59+
static unsigned int term_raw_flags;
5960

6061
static void sync_ttysize(struct exec_closure *ec);
6162
static void schedule_signal(struct exec_closure *ec, int signo);
@@ -161,8 +162,8 @@ pty_cleanup_hook(void)
161162
}
162163

163164
/*
164-
* Check whether we are running in the foregroup.
165-
* Updates the foreground flag and updates the window size.
165+
* Check whether sudo is running in the foreground.
166+
* Updates the foreground flag in the closure.
166167
* Returns 0 if there is no tty, the foreground process group ID
167168
* on success, or -1 on failure (tty revoked).
168169
*/
@@ -208,7 +209,7 @@ resume_terminal(struct exec_closure *ec)
208209

209210
if (ec->foreground) {
210211
/* Foreground process, set tty to raw mode. */
211-
if (sudo_term_raw(io_fds[SFD_USERTTY], 0))
212+
if (sudo_term_raw(io_fds[SFD_USERTTY], term_raw_flags))
212213
ec->term_raw = true;
213214
} else {
214215
/* Background process, no access to tty. */
@@ -263,7 +264,7 @@ suspend_sudo_pty(struct exec_closure *ec, int signo)
263264
"%s: command received SIG%s, parent running in the foregound",
264265
__func__, signame);
265266
if (!ec->term_raw) {
266-
if (sudo_term_raw(io_fds[SFD_USERTTY], 0))
267+
if (sudo_term_raw(io_fds[SFD_USERTTY], term_raw_flags))
267268
ec->term_raw = true;
268269
}
269270
ret = SIGCONT_FG; /* resume command in foreground */
@@ -1229,15 +1230,18 @@ exec_pty(struct command_details *details,
12291230
/* Not logging stdout, do not interpose. */
12301231
sudo_debug_printf(SUDO_DEBUG_INFO,
12311232
"stdout not a tty, not logging");
1232-
if (S_ISFIFO(sb.st_mode))
1233+
if (S_ISFIFO(sb.st_mode)) {
12331234
SET(details->flags, CD_EXEC_BG);
1235+
term_raw_flags = SUDO_TERM_OFLAG;
1236+
}
12341237
io_fds[SFD_STDOUT] = dup(STDOUT_FILENO);
12351238
if (io_fds[SFD_STDOUT] == -1)
12361239
sudo_fatal("dup");
12371240
} else {
12381241
sudo_debug_printf(SUDO_DEBUG_INFO,
12391242
"stdout not a tty, creating a pipe");
12401243
SET(details->flags, CD_EXEC_BG);
1244+
term_raw_flags = SUDO_TERM_OFLAG;
12411245
if (pipe2(io_pipe[STDOUT_FILENO], O_CLOEXEC) != 0)
12421246
sudo_fatal("%s", U_("unable to create pipe"));
12431247
io_buf_new(io_pipe[STDOUT_FILENO][0], STDOUT_FILENO,

0 commit comments

Comments
 (0)