diff options
author | Todd C. Miller <Todd.Miller@sudo.ws> | 2023-03-22 12:44:41 -0600 |
---|---|---|
committer | Todd C. Miller <Todd.Miller@sudo.ws> | 2023-03-22 12:44:41 -0600 |
commit | d5f913c56f9af37b87f1dd495af36fc0f553b339 (patch) | |
tree | 15c5adc6f4d6bc6814e8d328f300dc1ec0c44009 /src | |
parent | 301ef62d7254c69d0c2e5180e49862ae20c97c49 (diff) | |
download | sudo-d5f913c56f9af37b87f1dd495af36fc0f553b339.tar.gz |
exec_pty: always copy the terminal settings from /dev/tty the pty.
Previously, we only did this when running in the foreground but
this can cause problems when running a program that reads the
terminal settings or window size in the background. If sudo is
running in the background, the terminal settings will be updated
if it transitions to the foreground process.
Based on a suggestion from From Duncan Overbruck.
Diffstat (limited to 'src')
-rw-r--r-- | src/exec_pty.c | 32 |
1 files changed, 19 insertions, 13 deletions
diff --git a/src/exec_pty.c b/src/exec_pty.c index 4ca44b814..b4af177b3 100644 --- a/src/exec_pty.c +++ b/src/exec_pty.c @@ -161,7 +161,7 @@ check_foreground(struct exec_closure *ec) if ((ret = tcgetpgrp(io_fds[SFD_USERTTY])) != -1) { foreground = ret == ec->ppgrp; if (foreground && !tty_initialized) { - /* Lazily initialize the pty if needed. */ + /* Re-initialize the pty if needed. */ if (sudo_term_copy(io_fds[SFD_USERTTY], io_fds[SFD_LEADER])) tty_initialized = true; } @@ -1233,20 +1233,23 @@ exec_pty(struct command_details *details, struct command_status *cstat) } } + /* + * Copy terminal settings from user tty -> pty. If sudo is a + * background process, we'll re-init the pty when foregrounded. + */ + if (!sudo_term_copy(io_fds[SFD_USERTTY], io_fds[SFD_LEADER])) { + sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO, + "%s: unable to copy terminal settings to pty", __func__); + foreground = false; + } + + /* Start in raw mode unless part of a pipeline or backgrounded. */ if (foreground) { - /* Copy terminal attrs from user tty -> pty. */ - if (!sudo_term_copy(io_fds[SFD_USERTTY], io_fds[SFD_LEADER])) { - sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO, - "%s: unable to copy terminal settings to pty", __func__); - foreground = false; - } else { - /* Start in raw mode unless part of a pipeline or backgrounded. */ - if (!pipeline && !ISSET(details->flags, CD_EXEC_BG)) { - if (sudo_term_raw(io_fds[SFD_USERTTY], 0)) - ttymode = TERM_RAW; - } - tty_initialized = true; + if (!pipeline && !ISSET(details->flags, CD_EXEC_BG)) { + if (sudo_term_raw(io_fds[SFD_USERTTY], 0)) + ttymode = TERM_RAW; } + tty_initialized = true; } /* @@ -1412,6 +1415,9 @@ sync_ttysize(struct exec_closure *ec) if (ioctl(io_fds[SFD_USERTTY], TIOCGWINSZ, &wsize) == 0) { if (wsize.ws_row != ec->rows || wsize.ws_col != ec->cols) { + sudo_debug_printf(SUDO_DEBUG_INFO, "%s: %hd x %hd -> %hd x %hd", + __func__, ec->rows, ec->cols, wsize.ws_row, wsize.ws_col); + /* Log window change event. */ log_winchange(ec, wsize.ws_row, wsize.ws_col); |