Skip to content

Commit 9cdebcf

Browse files
committed
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. --HG-- branch : 1.9
1 parent 77f209e commit 9cdebcf

2 files changed

Lines changed: 14 additions & 19 deletions

File tree

src/exec_monitor.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ exec_cmnd_pty(struct command_details *details, sigset_t *mask,
373373
close(io_fds[SFD_STDERR]);
374374

375375
/* Wait for parent to grant us the tty if we are foreground. */
376-
if (foreground && !ISSET(details->flags, CD_EXEC_BG)) {
376+
if (foreground) {
377377
struct timespec ts = { 0, 1000 }; /* 1us */
378378
sudo_debug_printf(SUDO_DEBUG_DEBUG, "%s: waiting for controlling tty",
379379
__func__);
@@ -652,7 +652,7 @@ exec_monitor(struct command_details *details, sigset_t *oset,
652652
setpgid(mc.cmnd_pid, mc.cmnd_pgrp);
653653

654654
/* Make the command the foreground process for the pty follower. */
655-
if (foreground && !ISSET(details->flags, CD_EXEC_BG)) {
655+
if (foreground) {
656656
if (tcsetpgrp(io_fds[SFD_FOLLOWER], mc.cmnd_pgrp) == -1) {
657657
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
658658
"%s: unable to set foreground pgrp to %d (command)",

src/exec_pty.c

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,7 @@ exec_pty(struct command_details *details,
10721072
struct exec_closure ec = { 0 };
10731073
struct plugin_container *plugin;
10741074
int evloop_retries = -1;
1075-
bool pipeline = false;
1075+
bool cmnd_foreground;
10761076
sigset_t set, oset;
10771077
struct sigaction sa;
10781078
struct stat sb;
@@ -1183,14 +1183,14 @@ exec_pty(struct command_details *details,
11831183
sudo_debug_printf(SUDO_DEBUG_INFO,
11841184
"stdin not a tty, not logging");
11851185
if (S_ISFIFO(sb.st_mode))
1186-
pipeline = true;
1186+
SET(details->flags, CD_EXEC_BG);
11871187
io_fds[SFD_STDIN] = dup(STDIN_FILENO);
11881188
if (io_fds[SFD_STDIN] == -1)
11891189
sudo_fatal("dup");
11901190
} else {
11911191
sudo_debug_printf(SUDO_DEBUG_INFO,
11921192
"stdin not a tty, creating a pipe");
1193-
pipeline = true;
1193+
SET(details->flags, CD_EXEC_BG);
11941194
if (pipe2(io_pipe[STDIN_FILENO], O_CLOEXEC) != 0)
11951195
sudo_fatal("%s", U_("unable to create pipe"));
11961196
io_buf_new(STDIN_FILENO, io_pipe[STDIN_FILENO][1],
@@ -1217,7 +1217,7 @@ exec_pty(struct command_details *details,
12171217
*/
12181218
sudo_debug_printf(SUDO_DEBUG_INFO,
12191219
"terminal input not available, creating empty pipe");
1220-
pipeline = true;
1220+
SET(details->flags, CD_EXEC_BG);
12211221
if (pipe2(io_pipe[STDIN_FILENO], O_CLOEXEC) != 0)
12221222
sudo_fatal("%s", U_("unable to create pipe"));
12231223
io_fds[SFD_STDIN] = io_pipe[STDIN_FILENO][0];
@@ -1230,14 +1230,14 @@ exec_pty(struct command_details *details,
12301230
sudo_debug_printf(SUDO_DEBUG_INFO,
12311231
"stdout not a tty, not logging");
12321232
if (S_ISFIFO(sb.st_mode))
1233-
pipeline = true;
1233+
SET(details->flags, CD_EXEC_BG);
12341234
io_fds[SFD_STDOUT] = dup(STDOUT_FILENO);
12351235
if (io_fds[SFD_STDOUT] == -1)
12361236
sudo_fatal("dup");
12371237
} else {
12381238
sudo_debug_printf(SUDO_DEBUG_INFO,
12391239
"stdout not a tty, creating a pipe");
1240-
pipeline = true;
1240+
SET(details->flags, CD_EXEC_BG);
12411241
if (pipe2(io_pipe[STDOUT_FILENO], O_CLOEXEC) != 0)
12421242
sudo_fatal("%s", U_("unable to create pipe"));
12431243
io_buf_new(io_pipe[STDOUT_FILENO][0], STDOUT_FILENO,
@@ -1250,8 +1250,6 @@ exec_pty(struct command_details *details,
12501250
/* Not logging stderr, do not interpose. */
12511251
sudo_debug_printf(SUDO_DEBUG_INFO,
12521252
"stderr not a tty, not logging");
1253-
if (S_ISFIFO(sb.st_mode))
1254-
pipeline = true;
12551253
io_fds[SFD_STDERR] = dup(STDERR_FILENO);
12561254
if (io_fds[SFD_STDERR] == -1)
12571255
sudo_fatal("dup");
@@ -1275,13 +1273,11 @@ exec_pty(struct command_details *details,
12751273
"%s: unable to copy terminal settings to pty", __func__);
12761274
ec.foreground = false;
12771275
}
1278-
1279-
/* Start in raw mode unless part of a pipeline or backgrounded. */
1280-
if (ec.foreground) {
1281-
if (!pipeline && !ISSET(details->flags, CD_EXEC_BG)) {
1282-
if (sudo_term_raw(io_fds[SFD_USERTTY], 0))
1283-
ec.term_raw = true;
1284-
}
1276+
/* Start in raw mode unless the command will run in the background. */
1277+
cmnd_foreground = ec.foreground && !ISSET(details->flags, CD_EXEC_BG);
1278+
if (cmnd_foreground) {
1279+
if (sudo_term_raw(io_fds[SFD_USERTTY], 0))
1280+
ec.term_raw = true;
12851281
}
12861282

12871283
/*
@@ -1324,8 +1320,7 @@ exec_pty(struct command_details *details,
13241320
* In this case, we rely on the command receiving SIGTTOU or SIGTTIN
13251321
* when it needs access to the controlling tty.
13261322
*/
1327-
exec_monitor(details, &oset, ec.foreground && !pipeline, sv[1],
1328-
intercept_sv[1]);
1323+
exec_monitor(details, &oset, cmnd_foreground, sv[1], intercept_sv[1]);
13291324
cstat->type = CMD_ERRNO;
13301325
cstat->val = errno;
13311326
if (send(sv[1], cstat, sizeof(*cstat), 0) == -1) {

0 commit comments

Comments
 (0)