Simplify the exec_monitor() foreground flag.

Add cmnd_foreground flag that is only true if sudo is the foreground
process and the CD_EXEC_BG flag is not set and pass it to exec_monitor().
This means exec_monitor() no longer needs to check for CD_EXEC_BG.
This commit is contained in:
Todd C. Miller
2023-07-14 14:08:59 -06:00
parent 625653de08
commit db4bdb7327
2 changed files with 14 additions and 19 deletions

View File

@@ -373,7 +373,7 @@ exec_cmnd_pty(struct command_details *details, sigset_t *mask,
close(io_fds[SFD_STDERR]); close(io_fds[SFD_STDERR]);
/* Wait for parent to grant us the tty if we are foreground. */ /* Wait for parent to grant us the tty if we are foreground. */
if (foreground && !ISSET(details->flags, CD_EXEC_BG)) { if (foreground) {
struct timespec ts = { 0, 1000 }; /* 1us */ struct timespec ts = { 0, 1000 }; /* 1us */
sudo_debug_printf(SUDO_DEBUG_DEBUG, "%s: waiting for controlling tty", sudo_debug_printf(SUDO_DEBUG_DEBUG, "%s: waiting for controlling tty",
__func__); __func__);
@@ -653,7 +653,7 @@ exec_monitor(struct command_details *details, sigset_t *oset,
setpgid(mc.cmnd_pid, mc.cmnd_pgrp); setpgid(mc.cmnd_pid, mc.cmnd_pgrp);
/* Make the command the foreground process for the pty follower. */ /* Make the command the foreground process for the pty follower. */
if (foreground && !ISSET(details->flags, CD_EXEC_BG)) { if (foreground) {
if (tcsetpgrp(io_fds[SFD_FOLLOWER], mc.cmnd_pgrp) == -1) { if (tcsetpgrp(io_fds[SFD_FOLLOWER], mc.cmnd_pgrp) == -1) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO, sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
"%s: unable to set foreground pgrp to %d (command)", "%s: unable to set foreground pgrp to %d (command)",

View File

@@ -1072,7 +1072,7 @@ exec_pty(struct command_details *details,
struct exec_closure ec = { 0 }; struct exec_closure ec = { 0 };
struct plugin_container *plugin; struct plugin_container *plugin;
int evloop_retries = -1; int evloop_retries = -1;
bool pipeline = false; bool cmnd_foreground;
sigset_t set, oset; sigset_t set, oset;
struct sigaction sa; struct sigaction sa;
struct stat sb; struct stat sb;
@@ -1183,14 +1183,14 @@ exec_pty(struct command_details *details,
sudo_debug_printf(SUDO_DEBUG_INFO, sudo_debug_printf(SUDO_DEBUG_INFO,
"stdin not a tty, not logging"); "stdin not a tty, not logging");
if (S_ISFIFO(sb.st_mode)) if (S_ISFIFO(sb.st_mode))
pipeline = true; SET(details->flags, CD_EXEC_BG);
io_fds[SFD_STDIN] = dup(STDIN_FILENO); io_fds[SFD_STDIN] = dup(STDIN_FILENO);
if (io_fds[SFD_STDIN] == -1) if (io_fds[SFD_STDIN] == -1)
sudo_fatal("dup"); sudo_fatal("dup");
} else { } else {
sudo_debug_printf(SUDO_DEBUG_INFO, sudo_debug_printf(SUDO_DEBUG_INFO,
"stdin not a tty, creating a pipe"); "stdin not a tty, creating a pipe");
pipeline = true; SET(details->flags, CD_EXEC_BG);
if (pipe2(io_pipe[STDIN_FILENO], O_CLOEXEC) != 0) if (pipe2(io_pipe[STDIN_FILENO], O_CLOEXEC) != 0)
sudo_fatal("%s", U_("unable to create pipe")); sudo_fatal("%s", U_("unable to create pipe"));
io_buf_new(STDIN_FILENO, io_pipe[STDIN_FILENO][1], io_buf_new(STDIN_FILENO, io_pipe[STDIN_FILENO][1],
@@ -1217,7 +1217,7 @@ exec_pty(struct command_details *details,
*/ */
sudo_debug_printf(SUDO_DEBUG_INFO, sudo_debug_printf(SUDO_DEBUG_INFO,
"terminal input not available, creating empty pipe"); "terminal input not available, creating empty pipe");
pipeline = true; SET(details->flags, CD_EXEC_BG);
if (pipe2(io_pipe[STDIN_FILENO], O_CLOEXEC) != 0) if (pipe2(io_pipe[STDIN_FILENO], O_CLOEXEC) != 0)
sudo_fatal("%s", U_("unable to create pipe")); sudo_fatal("%s", U_("unable to create pipe"));
io_fds[SFD_STDIN] = io_pipe[STDIN_FILENO][0]; io_fds[SFD_STDIN] = io_pipe[STDIN_FILENO][0];
@@ -1230,14 +1230,14 @@ exec_pty(struct command_details *details,
sudo_debug_printf(SUDO_DEBUG_INFO, sudo_debug_printf(SUDO_DEBUG_INFO,
"stdout not a tty, not logging"); "stdout not a tty, not logging");
if (S_ISFIFO(sb.st_mode)) if (S_ISFIFO(sb.st_mode))
pipeline = true; SET(details->flags, CD_EXEC_BG);
io_fds[SFD_STDOUT] = dup(STDOUT_FILENO); io_fds[SFD_STDOUT] = dup(STDOUT_FILENO);
if (io_fds[SFD_STDOUT] == -1) if (io_fds[SFD_STDOUT] == -1)
sudo_fatal("dup"); sudo_fatal("dup");
} else { } else {
sudo_debug_printf(SUDO_DEBUG_INFO, sudo_debug_printf(SUDO_DEBUG_INFO,
"stdout not a tty, creating a pipe"); "stdout not a tty, creating a pipe");
pipeline = true; SET(details->flags, CD_EXEC_BG);
if (pipe2(io_pipe[STDOUT_FILENO], O_CLOEXEC) != 0) if (pipe2(io_pipe[STDOUT_FILENO], O_CLOEXEC) != 0)
sudo_fatal("%s", U_("unable to create pipe")); sudo_fatal("%s", U_("unable to create pipe"));
io_buf_new(io_pipe[STDOUT_FILENO][0], STDOUT_FILENO, io_buf_new(io_pipe[STDOUT_FILENO][0], STDOUT_FILENO,
@@ -1250,8 +1250,6 @@ exec_pty(struct command_details *details,
/* Not logging stderr, do not interpose. */ /* Not logging stderr, do not interpose. */
sudo_debug_printf(SUDO_DEBUG_INFO, sudo_debug_printf(SUDO_DEBUG_INFO,
"stderr not a tty, not logging"); "stderr not a tty, not logging");
if (S_ISFIFO(sb.st_mode))
pipeline = true;
io_fds[SFD_STDERR] = dup(STDERR_FILENO); io_fds[SFD_STDERR] = dup(STDERR_FILENO);
if (io_fds[SFD_STDERR] == -1) if (io_fds[SFD_STDERR] == -1)
sudo_fatal("dup"); sudo_fatal("dup");
@@ -1275,14 +1273,12 @@ exec_pty(struct command_details *details,
"%s: unable to copy terminal settings to pty", __func__); "%s: unable to copy terminal settings to pty", __func__);
ec.foreground = false; ec.foreground = false;
} }
/* Start in raw mode unless the command will run in the background. */
/* Start in raw mode unless part of a pipeline or backgrounded. */ cmnd_foreground = ec.foreground && !ISSET(details->flags, CD_EXEC_BG);
if (ec.foreground) { if (cmnd_foreground) {
if (!pipeline && !ISSET(details->flags, CD_EXEC_BG)) {
if (sudo_term_raw(io_fds[SFD_USERTTY], 0)) if (sudo_term_raw(io_fds[SFD_USERTTY], 0))
ec.term_raw = true; ec.term_raw = true;
} }
}
/* /*
* Block signals until we have our handlers setup in the parent so * Block signals until we have our handlers setup in the parent so
@@ -1324,8 +1320,7 @@ exec_pty(struct command_details *details,
* In this case, we rely on the command receiving SIGTTOU or SIGTTIN * In this case, we rely on the command receiving SIGTTOU or SIGTTIN
* when it needs access to the controlling tty. * when it needs access to the controlling tty.
*/ */
exec_monitor(details, &oset, ec.foreground && !pipeline, sv[1], exec_monitor(details, &oset, cmnd_foreground, sv[1], intercept_sv[1]);
intercept_sv[1]);
cstat->type = CMD_ERRNO; cstat->type = CMD_ERRNO;
cstat->val = errno; cstat->val = errno;
if (send(sv[1], cstat, sizeof(*cstat), 0) == -1) { if (send(sv[1], cstat, sizeof(*cstat), 0) == -1) {