version 1.1, 2012/02/21 16:23:02
|
version 1.1.1.3, 2012/10/09 09:29:52
|
Line 85 struct io_buffer {
|
Line 85 struct io_buffer {
|
int off; /* write position (how much already consumed) */ |
int off; /* write position (how much already consumed) */ |
int rfd; /* reader (producer) */ |
int rfd; /* reader (producer) */ |
int wfd; /* writer (consumer) */ |
int wfd; /* writer (consumer) */ |
int (*action)(const char *buf, unsigned int len); | bool (*action)(const char *buf, unsigned int len); |
char buf[16 * 1024]; |
char buf[16 * 1024]; |
}; |
}; |
|
|
static char slavename[PATH_MAX]; |
static char slavename[PATH_MAX]; |
static int foreground; | static bool foreground, pipeline, tty_initialized; |
static int io_fds[6] = { -1, -1, -1, -1, -1, -1}; |
static int io_fds[6] = { -1, -1, -1, -1, -1, -1}; |
static int pipeline = FALSE; |
|
static int tty_initialized; |
|
static int ttymode = TERM_COOKED; |
static int ttymode = TERM_COOKED; |
static pid_t ppgrp, child, child_pgrp; | static pid_t ppgrp, cmnd_pgrp; |
static sigset_t ttyblock; |
static sigset_t ttyblock; |
static struct io_buffer *iobufs; |
static struct io_buffer *iobufs; |
|
|
static void flush_output(void); |
static void flush_output(void); |
static int exec_monitor(struct command_details *details, int backchannel); |
static int exec_monitor(struct command_details *details, int backchannel); |
static void exec_pty(struct command_details *detail); | static void exec_pty(struct command_details *detail, int *errfd); |
static void sigwinch(int s); |
static void sigwinch(int s); |
static void sync_ttysize(int src, int dst); |
static void sync_ttysize(int src, int dst); |
static void deliver_signal(pid_t pid, int signo); | static void deliver_signal(pid_t pid, int signo, bool from_parent); |
static int safe_close(int fd); |
static int safe_close(int fd); |
|
static void check_foreground(void); |
|
|
/* |
/* |
* Cleanup hook for error()/errorx() |
* Cleanup hook for error()/errorx() |
Line 113 static int safe_close(int fd);
|
Line 112 static int safe_close(int fd);
|
void |
void |
cleanup(int gotsignal) |
cleanup(int gotsignal) |
{ |
{ |
if (!tq_empty(&io_plugins)) | debug_decl(cleanup, SUDO_DEBUG_EXEC); |
term_restore(io_fds[SFD_USERTTY], 0); | |
| if (!tq_empty(&io_plugins) && io_fds[SFD_USERTTY] != -1) { |
| check_foreground(); |
| if (foreground) |
| term_restore(io_fds[SFD_USERTTY], 0); |
| } |
#ifdef HAVE_SELINUX |
#ifdef HAVE_SELINUX |
selinux_restore_tty(); |
selinux_restore_tty(); |
#endif |
#endif |
utmp_logout(slavename, 0); /* XXX - only if CD_SET_UTMP */ |
utmp_logout(slavename, 0); /* XXX - only if CD_SET_UTMP */ |
|
|
|
debug_return; |
} |
} |
|
|
/* |
/* |
|
* Generic handler for signals recieved by the monitor process. |
|
* The other end of signal_pipe is checked in the monitor event loop. |
|
*/ |
|
#ifdef SA_SIGINFO |
|
void |
|
mon_handler(int s, siginfo_t *info, void *context) |
|
{ |
|
unsigned char signo = (unsigned char)s; |
|
|
|
/* |
|
* If the signal came from the command we ran, just ignore |
|
* it since we don't want the command to indirectly kill itself. |
|
* This can happen with, e.g. BSD-derived versions of reboot |
|
* that call kill(-1, SIGTERM) to kill all other processes. |
|
*/ |
|
if (info != NULL && info->si_code == SI_USER && info->si_pid == cmnd_pid) |
|
return; |
|
|
|
/* |
|
* The pipe is non-blocking, if we overflow the kernel's pipe |
|
* buffer we drop the signal. This is not a problem in practice. |
|
*/ |
|
ignore_result(write(signal_pipe[1], &signo, sizeof(signo))); |
|
} |
|
#else |
|
void |
|
mon_handler(int s) |
|
{ |
|
unsigned char signo = (unsigned char)s; |
|
|
|
/* |
|
* The pipe is non-blocking, if we overflow the kernel's pipe |
|
* buffer we drop the signal. This is not a problem in practice. |
|
*/ |
|
ignore_result(write(signal_pipe[1], &signo, sizeof(signo))); |
|
} |
|
#endif |
|
|
|
/* |
* Allocate a pty if /dev/tty is a tty. |
* Allocate a pty if /dev/tty is a tty. |
* Fills in io_fds[SFD_USERTTY], io_fds[SFD_MASTER], io_fds[SFD_SLAVE] |
* Fills in io_fds[SFD_USERTTY], io_fds[SFD_MASTER], io_fds[SFD_SLAVE] |
* and slavename globals. |
* and slavename globals. |
Line 129 cleanup(int gotsignal)
|
Line 174 cleanup(int gotsignal)
|
void |
void |
pty_setup(uid_t uid, const char *tty, const char *utmp_user) |
pty_setup(uid_t uid, const char *tty, const char *utmp_user) |
{ |
{ |
|
debug_decl(pty_setup, SUDO_DEBUG_EXEC); |
|
|
io_fds[SFD_USERTTY] = open(_PATH_TTY, O_RDWR|O_NOCTTY, 0); |
io_fds[SFD_USERTTY] = open(_PATH_TTY, O_RDWR|O_NOCTTY, 0); |
if (io_fds[SFD_USERTTY] != -1) { |
if (io_fds[SFD_USERTTY] != -1) { |
if (!get_pty(&io_fds[SFD_MASTER], &io_fds[SFD_SLAVE], |
if (!get_pty(&io_fds[SFD_MASTER], &io_fds[SFD_SLAVE], |
Line 138 pty_setup(uid_t uid, const char *tty, const char *utmp
|
Line 185 pty_setup(uid_t uid, const char *tty, const char *utmp
|
if (utmp_user != NULL) |
if (utmp_user != NULL) |
utmp_login(tty, slavename, io_fds[SFD_SLAVE], utmp_user); |
utmp_login(tty, slavename, io_fds[SFD_SLAVE], utmp_user); |
} |
} |
|
|
|
debug_return; |
} |
} |
|
|
/* Call I/O plugin tty input log method. */ |
/* Call I/O plugin tty input log method. */ |
static int | static bool |
log_ttyin(const char *buf, unsigned int n) |
log_ttyin(const char *buf, unsigned int n) |
{ |
{ |
struct plugin_container *plugin; |
struct plugin_container *plugin; |
sigset_t omask; |
sigset_t omask; |
int rval = TRUE; | bool rval = true; |
| debug_decl(log_ttyin, SUDO_DEBUG_EXEC); |
|
|
sigprocmask(SIG_BLOCK, &ttyblock, &omask); |
sigprocmask(SIG_BLOCK, &ttyblock, &omask); |
|
|
tq_foreach_fwd(&io_plugins, plugin) { |
tq_foreach_fwd(&io_plugins, plugin) { |
if (plugin->u.io->log_ttyin) { |
if (plugin->u.io->log_ttyin) { |
if (!plugin->u.io->log_ttyin(buf, n)) { |
if (!plugin->u.io->log_ttyin(buf, n)) { |
rval = FALSE; | rval = false; |
break; |
break; |
} |
} |
} |
} |
} |
} |
|
|
sigprocmask(SIG_SETMASK, &omask, NULL); |
sigprocmask(SIG_SETMASK, &omask, NULL); |
return rval; | |
| debug_return_bool(rval); |
} |
} |
|
|
/* Call I/O plugin stdin log method. */ |
/* Call I/O plugin stdin log method. */ |
static int | static bool |
log_stdin(const char *buf, unsigned int n) |
log_stdin(const char *buf, unsigned int n) |
{ |
{ |
struct plugin_container *plugin; |
struct plugin_container *plugin; |
sigset_t omask; |
sigset_t omask; |
int rval = TRUE; | bool rval = true; |
| debug_decl(log_stdin, SUDO_DEBUG_EXEC); |
|
|
sigprocmask(SIG_BLOCK, &ttyblock, &omask); |
sigprocmask(SIG_BLOCK, &ttyblock, &omask); |
|
|
tq_foreach_fwd(&io_plugins, plugin) { |
tq_foreach_fwd(&io_plugins, plugin) { |
if (plugin->u.io->log_stdin) { |
if (plugin->u.io->log_stdin) { |
if (!plugin->u.io->log_stdin(buf, n)) { |
if (!plugin->u.io->log_stdin(buf, n)) { |
rval = FALSE; | rval = false; |
break; |
break; |
} |
} |
} |
} |
} |
} |
|
|
sigprocmask(SIG_SETMASK, &omask, NULL); |
sigprocmask(SIG_SETMASK, &omask, NULL); |
return rval; | |
| debug_return_bool(rval); |
} |
} |
|
|
/* Call I/O plugin tty output log method. */ |
/* Call I/O plugin tty output log method. */ |
static int | static bool |
log_ttyout(const char *buf, unsigned int n) |
log_ttyout(const char *buf, unsigned int n) |
{ |
{ |
struct plugin_container *plugin; |
struct plugin_container *plugin; |
sigset_t omask; |
sigset_t omask; |
int rval = TRUE; | bool rval = true; |
| debug_decl(log_ttyout, SUDO_DEBUG_EXEC); |
|
|
sigprocmask(SIG_BLOCK, &ttyblock, &omask); |
sigprocmask(SIG_BLOCK, &ttyblock, &omask); |
|
|
tq_foreach_fwd(&io_plugins, plugin) { |
tq_foreach_fwd(&io_plugins, plugin) { |
if (plugin->u.io->log_ttyout) { |
if (plugin->u.io->log_ttyout) { |
if (!plugin->u.io->log_ttyout(buf, n)) { |
if (!plugin->u.io->log_ttyout(buf, n)) { |
rval = FALSE; | rval = false; |
break; |
break; |
} |
} |
} |
} |
} |
} |
|
|
sigprocmask(SIG_SETMASK, &omask, NULL); |
sigprocmask(SIG_SETMASK, &omask, NULL); |
return rval; | |
| debug_return_bool(rval); |
} |
} |
|
|
/* Call I/O plugin stdout log method. */ |
/* Call I/O plugin stdout log method. */ |
static int | static bool |
log_stdout(const char *buf, unsigned int n) |
log_stdout(const char *buf, unsigned int n) |
{ |
{ |
struct plugin_container *plugin; |
struct plugin_container *plugin; |
sigset_t omask; |
sigset_t omask; |
int rval = TRUE; | bool rval = true; |
| debug_decl(log_stdout, SUDO_DEBUG_EXEC); |
|
|
sigprocmask(SIG_BLOCK, &ttyblock, &omask); |
sigprocmask(SIG_BLOCK, &ttyblock, &omask); |
|
|
tq_foreach_fwd(&io_plugins, plugin) { |
tq_foreach_fwd(&io_plugins, plugin) { |
if (plugin->u.io->log_stdout) { |
if (plugin->u.io->log_stdout) { |
if (!plugin->u.io->log_stdout(buf, n)) { |
if (!plugin->u.io->log_stdout(buf, n)) { |
rval = FALSE; | rval = false; |
break; |
break; |
} |
} |
} |
} |
} |
} |
|
|
sigprocmask(SIG_SETMASK, &omask, NULL); |
sigprocmask(SIG_SETMASK, &omask, NULL); |
return rval; | |
| debug_return_bool(rval); |
} |
} |
|
|
/* Call I/O plugin stderr log method. */ |
/* Call I/O plugin stderr log method. */ |
static int | static bool |
log_stderr(const char *buf, unsigned int n) |
log_stderr(const char *buf, unsigned int n) |
{ |
{ |
struct plugin_container *plugin; |
struct plugin_container *plugin; |
sigset_t omask; |
sigset_t omask; |
int rval = TRUE; | bool rval = true; |
| debug_decl(log_stderr, SUDO_DEBUG_EXEC); |
|
|
sigprocmask(SIG_BLOCK, &ttyblock, &omask); |
sigprocmask(SIG_BLOCK, &ttyblock, &omask); |
|
|
tq_foreach_fwd(&io_plugins, plugin) { |
tq_foreach_fwd(&io_plugins, plugin) { |
if (plugin->u.io->log_stderr) { |
if (plugin->u.io->log_stderr) { |
if (!plugin->u.io->log_stderr(buf, n)) { |
if (!plugin->u.io->log_stderr(buf, n)) { |
rval = FALSE; | rval = false; |
break; |
break; |
} |
} |
} |
} |
} |
} |
|
|
sigprocmask(SIG_SETMASK, &omask, NULL); |
sigprocmask(SIG_SETMASK, &omask, NULL); |
return rval; | |
| debug_return_bool(rval); |
} |
} |
|
|
/* |
/* |
Line 263 log_stderr(const char *buf, unsigned int n)
|
Line 312 log_stderr(const char *buf, unsigned int n)
|
static void |
static void |
check_foreground(void) |
check_foreground(void) |
{ |
{ |
|
debug_decl(check_foreground, SUDO_DEBUG_EXEC); |
|
|
if (io_fds[SFD_USERTTY] != -1) { |
if (io_fds[SFD_USERTTY] != -1) { |
foreground = tcgetpgrp(io_fds[SFD_USERTTY]) == ppgrp; |
foreground = tcgetpgrp(io_fds[SFD_USERTTY]) == ppgrp; |
if (foreground && !tty_initialized) { |
if (foreground && !tty_initialized) { |
if (term_copy(io_fds[SFD_USERTTY], io_fds[SFD_SLAVE])) { |
if (term_copy(io_fds[SFD_USERTTY], io_fds[SFD_SLAVE])) { |
tty_initialized = 1; | tty_initialized = true; |
sync_ttysize(io_fds[SFD_USERTTY], io_fds[SFD_SLAVE]); |
sync_ttysize(io_fds[SFD_USERTTY], io_fds[SFD_SLAVE]); |
} |
} |
} |
} |
} |
} |
|
|
|
debug_return; |
} |
} |
|
|
/* |
/* |
* Suspend sudo if the underlying command is suspended. |
* Suspend sudo if the underlying command is suspended. |
* Returns SIGCONT_FG if the child should be resume in the | * Returns SIGCONT_FG if the command should be resumed in the |
* foreground or SIGCONT_BG if it is a background process. |
* foreground or SIGCONT_BG if it is a background process. |
*/ |
*/ |
int |
int |
suspend_parent(int signo) |
suspend_parent(int signo) |
{ |
{ |
|
char signame[SIG2STR_MAX]; |
sigaction_t sa, osa; |
sigaction_t sa, osa; |
int n, oldmode = ttymode, rval = 0; |
int n, oldmode = ttymode, rval = 0; |
|
debug_decl(suspend_parent, SUDO_DEBUG_EXEC); |
|
|
switch (signo) { |
switch (signo) { |
case SIGTTOU: |
case SIGTTOU: |
case SIGTTIN: |
case SIGTTIN: |
/* |
/* |
* If we are the foreground process, just resume the child. | * If we are the foreground process, just resume the command. |
* Otherwise, re-send the signal with the handler disabled. |
* Otherwise, re-send the signal with the handler disabled. |
*/ |
*/ |
if (!foreground) |
if (!foreground) |
Line 301 suspend_parent(int signo)
|
Line 356 suspend_parent(int signo)
|
} while (!n && errno == EINTR); |
} while (!n && errno == EINTR); |
ttymode = TERM_RAW; |
ttymode = TERM_RAW; |
} |
} |
rval = SIGCONT_FG; /* resume child in foreground */ | rval = SIGCONT_FG; /* resume command in foreground */ |
break; |
break; |
} |
} |
ttymode = TERM_RAW; |
ttymode = TERM_RAW; |
Line 318 suspend_parent(int signo)
|
Line 373 suspend_parent(int signo)
|
} while (!n && errno == EINTR); |
} while (!n && errno == EINTR); |
} |
} |
|
|
/* Suspend self and continue child when we resume. */ | if (sig2str(signo, signame) == -1) |
| snprintf(signame, sizeof(signame), "%d", signo); |
| |
| /* Suspend self and continue command when we resume. */ |
| zero_bytes(&sa, sizeof(sa)); |
| sigemptyset(&sa.sa_mask); |
| sa.sa_flags = SA_INTERRUPT; /* do not restart syscalls */ |
sa.sa_handler = SIG_DFL; |
sa.sa_handler = SIG_DFL; |
sigaction(signo, &sa, &osa); |
sigaction(signo, &sa, &osa); |
sudo_debug(8, "kill parent %d", signo); | sudo_debug_printf(SUDO_DEBUG_INFO, "kill parent SIG%s", signame); |
if (killpg(ppgrp, signo) != 0) |
if (killpg(ppgrp, signo) != 0) |
warning("killpg(%d, %d)", (int)ppgrp, signo); | warning("killpg(%d, SIG%s)", (int)ppgrp, signame); |
|
|
/* Check foreground/background status on resume. */ |
/* Check foreground/background status on resume. */ |
check_foreground(); |
check_foreground(); |
|
|
/* |
/* |
* Only modify term if we are foreground process and either |
* Only modify term if we are foreground process and either |
* the old tty mode was not cooked or child got SIGTT{IN,OU} | * the old tty mode was not cooked or command got SIGTT{IN,OU} |
*/ |
*/ |
sudo_debug(8, "parent is in %s, ttymode %d -> %d", | sudo_debug_printf(SUDO_DEBUG_INFO, "parent is in %s, ttymode %d -> %d", |
foreground ? "foreground" : "background", oldmode, ttymode); |
foreground ? "foreground" : "background", oldmode, ttymode); |
|
|
if (ttymode != TERM_COOKED) { |
if (ttymode != TERM_COOKED) { |
Line 352 suspend_parent(int signo)
|
Line 413 suspend_parent(int signo)
|
break; |
break; |
} |
} |
|
|
return rval; | debug_return_int(rval); |
} |
} |
|
|
/* |
/* |
* Kill child with increasing urgency. | * Kill command with increasing urgency. |
*/ |
*/ |
void |
void |
terminate_child(pid_t pid, int use_pgrp) | terminate_command(pid_t pid, bool use_pgrp) |
{ |
{ |
|
debug_decl(terminate_command, SUDO_DEBUG_EXEC); |
|
|
/* |
/* |
* Note that SIGCHLD will interrupt the sleep() |
* Note that SIGCHLD will interrupt the sleep() |
*/ |
*/ |
if (use_pgrp) { |
if (use_pgrp) { |
|
sudo_debug_printf(SUDO_DEBUG_INFO, "killpg %d SIGHUP", (int)pid); |
killpg(pid, SIGHUP); |
killpg(pid, SIGHUP); |
|
sudo_debug_printf(SUDO_DEBUG_INFO, "killpg %d SIGTERM", (int)pid); |
killpg(pid, SIGTERM); |
killpg(pid, SIGTERM); |
sleep(2); |
sleep(2); |
|
sudo_debug_printf(SUDO_DEBUG_INFO, "killpg %d SIGKILL", (int)pid); |
killpg(pid, SIGKILL); |
killpg(pid, SIGKILL); |
} else { |
} else { |
|
sudo_debug_printf(SUDO_DEBUG_INFO, "kill %d SIGHUP", (int)pid); |
kill(pid, SIGHUP); |
kill(pid, SIGHUP); |
|
sudo_debug_printf(SUDO_DEBUG_INFO, "kill %d SIGTERM", (int)pid); |
kill(pid, SIGTERM); |
kill(pid, SIGTERM); |
sleep(2); |
sleep(2); |
|
sudo_debug_printf(SUDO_DEBUG_INFO, "kill %d SIGKILL", (int)pid); |
kill(pid, SIGKILL); |
kill(pid, SIGKILL); |
} |
} |
|
|
|
debug_return; |
} |
} |
|
|
static struct io_buffer * |
static struct io_buffer * |
io_buf_new(int rfd, int wfd, int (*action)(const char *, unsigned int), | io_buf_new(int rfd, int wfd, bool (*action)(const char *, unsigned int), |
struct io_buffer *head) |
struct io_buffer *head) |
{ |
{ |
struct io_buffer *iob; |
struct io_buffer *iob; |
|
debug_decl(io_buf_new, SUDO_DEBUG_EXEC); |
|
|
iob = emalloc(sizeof(*iob)); | iob = ecalloc(1, sizeof(*iob)); |
zero_bytes(iob, sizeof(*iob)); | |
iob->rfd = rfd; |
iob->rfd = rfd; |
iob->wfd = wfd; |
iob->wfd = wfd; |
iob->action = action; |
iob->action = action; |
iob->next = head; |
iob->next = head; |
return iob; | |
| debug_return_ptr(iob); |
} |
} |
|
|
/* |
/* |
Line 401 perform_io(fd_set *fdsr, fd_set *fdsw, struct command_
|
Line 473 perform_io(fd_set *fdsr, fd_set *fdsw, struct command_
|
{ |
{ |
struct io_buffer *iob; |
struct io_buffer *iob; |
int n, errors = 0; |
int n, errors = 0; |
|
debug_decl(perform_io, SUDO_DEBUG_EXEC); |
|
|
for (iob = iobufs; iob; iob = iob->next) { |
for (iob = iobufs; iob; iob = iob->next) { |
if (iob->rfd != -1 && FD_ISSET(iob->rfd, fdsr)) { |
if (iob->rfd != -1 && FD_ISSET(iob->rfd, fdsr)) { |
Line 410 perform_io(fd_set *fdsr, fd_set *fdsw, struct command_
|
Line 483 perform_io(fd_set *fdsr, fd_set *fdsw, struct command_
|
} while (n == -1 && errno == EINTR); |
} while (n == -1 && errno == EINTR); |
switch (n) { |
switch (n) { |
case -1: |
case -1: |
if (errno == EAGAIN) | if (errno != EAGAIN) { |
break; | /* treat read error as fatal and close the fd */ |
if (errno != ENXIO && errno != EBADF) { | sudo_debug_printf(SUDO_DEBUG_ERROR, |
errors++; | "error reading fd %d: %s", iob->rfd, |
break; | strerror(errno)); |
| safe_close(iob->rfd); |
| iob->rfd = -1; |
} |
} |
/* FALLTHROUGH */ | break; |
case 0: |
case 0: |
/* got EOF or pty has gone away */ |
/* got EOF or pty has gone away */ |
|
sudo_debug_printf(SUDO_DEBUG_INFO, |
|
"read EOF from fd %d", iob->rfd); |
safe_close(iob->rfd); |
safe_close(iob->rfd); |
iob->rfd = -1; |
iob->rfd = -1; |
break; |
break; |
default: |
default: |
|
sudo_debug_printf(SUDO_DEBUG_INFO, |
|
"read %d bytes from fd %d", n, iob->rfd); |
if (!iob->action(iob->buf + iob->len, n)) |
if (!iob->action(iob->buf + iob->len, n)) |
terminate_child(child, TRUE); | terminate_command(cmnd_pid, true); |
iob->len += n; |
iob->len += n; |
break; |
break; |
} |
} |
Line 435 perform_io(fd_set *fdsr, fd_set *fdsw, struct command_
|
Line 514 perform_io(fd_set *fdsr, fd_set *fdsw, struct command_
|
iob->len - iob->off); |
iob->len - iob->off); |
} while (n == -1 && errno == EINTR); |
} while (n == -1 && errno == EINTR); |
if (n == -1) { |
if (n == -1) { |
if (errno == EPIPE || errno == ENXIO || errno == EBADF) { | if (errno == EPIPE || errno == ENXIO || errno == EIO || errno == EBADF) { |
| sudo_debug_printf(SUDO_DEBUG_INFO, |
| "unable to write %d bytes to fd %d", |
| iob->len - iob->off, iob->wfd); |
/* other end of pipe closed or pty revoked */ |
/* other end of pipe closed or pty revoked */ |
if (iob->rfd != -1) { |
if (iob->rfd != -1) { |
safe_close(iob->rfd); |
safe_close(iob->rfd); |
Line 445 perform_io(fd_set *fdsr, fd_set *fdsw, struct command_
|
Line 527 perform_io(fd_set *fdsr, fd_set *fdsw, struct command_
|
iob->wfd = -1; |
iob->wfd = -1; |
continue; |
continue; |
} |
} |
if (errno != EAGAIN) | if (errno != EAGAIN) { |
errors++; |
errors++; |
|
sudo_debug_printf(SUDO_DEBUG_ERROR, |
|
"error writing fd %d: %s", iob->wfd, strerror(errno)); |
|
} |
} else { |
} else { |
|
sudo_debug_printf(SUDO_DEBUG_INFO, |
|
"wrote %d bytes to fd %d", n, iob->wfd); |
iob->off += n; |
iob->off += n; |
} |
} |
} |
} |
Line 456 perform_io(fd_set *fdsr, fd_set *fdsw, struct command_
|
Line 543 perform_io(fd_set *fdsr, fd_set *fdsw, struct command_
|
cstat->type = CMD_ERRNO; |
cstat->type = CMD_ERRNO; |
cstat->val = errno; |
cstat->val = errno; |
} |
} |
return errors; | debug_return_int(errors); |
} |
} |
|
|
/* |
/* |
Line 465 perform_io(fd_set *fdsr, fd_set *fdsw, struct command_
|
Line 552 perform_io(fd_set *fdsr, fd_set *fdsw, struct command_
|
* Returns the child pid. |
* Returns the child pid. |
*/ |
*/ |
int |
int |
fork_pty(struct command_details *details, int sv[], int *maxfd) | fork_pty(struct command_details *details, int sv[], int *maxfd, sigset_t *omask) |
{ |
{ |
struct command_status cstat; |
struct command_status cstat; |
struct io_buffer *iob; |
struct io_buffer *iob; |
int io_pipe[3][2], n; |
int io_pipe[3][2], n; |
sigaction_t sa; |
sigaction_t sa; |
|
sigset_t mask; |
|
pid_t child; |
|
debug_decl(fork_pty, SUDO_DEBUG_EXEC); |
|
|
ppgrp = getpgrp(); /* parent's pgrp, so child can signal us */ |
ppgrp = getpgrp(); /* parent's pgrp, so child can signal us */ |
|
|
Line 521 fork_pty(struct command_details *details, int sv[], in
|
Line 611 fork_pty(struct command_details *details, int sv[], in
|
*/ |
*/ |
memset(io_pipe, 0, sizeof(io_pipe)); |
memset(io_pipe, 0, sizeof(io_pipe)); |
if (io_fds[SFD_STDIN] == -1 || !isatty(STDIN_FILENO)) { |
if (io_fds[SFD_STDIN] == -1 || !isatty(STDIN_FILENO)) { |
pipeline = TRUE; | sudo_debug_printf(SUDO_DEBUG_INFO, "stdin not a tty, creating a pipe"); |
| pipeline = true; |
if (pipe(io_pipe[STDIN_FILENO]) != 0) |
if (pipe(io_pipe[STDIN_FILENO]) != 0) |
error(1, _("unable to create pipe")); |
error(1, _("unable to create pipe")); |
iobufs = io_buf_new(STDIN_FILENO, io_pipe[STDIN_FILENO][1], |
iobufs = io_buf_new(STDIN_FILENO, io_pipe[STDIN_FILENO][1], |
Line 529 fork_pty(struct command_details *details, int sv[], in
|
Line 620 fork_pty(struct command_details *details, int sv[], in
|
io_fds[SFD_STDIN] = io_pipe[STDIN_FILENO][0]; |
io_fds[SFD_STDIN] = io_pipe[STDIN_FILENO][0]; |
} |
} |
if (io_fds[SFD_STDOUT] == -1 || !isatty(STDOUT_FILENO)) { |
if (io_fds[SFD_STDOUT] == -1 || !isatty(STDOUT_FILENO)) { |
pipeline = TRUE; | sudo_debug_printf(SUDO_DEBUG_INFO, "stdout not a tty, creating a pipe"); |
| pipeline = true; |
if (pipe(io_pipe[STDOUT_FILENO]) != 0) |
if (pipe(io_pipe[STDOUT_FILENO]) != 0) |
error(1, _("unable to create pipe")); |
error(1, _("unable to create pipe")); |
iobufs = io_buf_new(io_pipe[STDOUT_FILENO][0], STDOUT_FILENO, |
iobufs = io_buf_new(io_pipe[STDOUT_FILENO][0], STDOUT_FILENO, |
Line 537 fork_pty(struct command_details *details, int sv[], in
|
Line 629 fork_pty(struct command_details *details, int sv[], in
|
io_fds[SFD_STDOUT] = io_pipe[STDOUT_FILENO][1]; |
io_fds[SFD_STDOUT] = io_pipe[STDOUT_FILENO][1]; |
} |
} |
if (io_fds[SFD_STDERR] == -1 || !isatty(STDERR_FILENO)) { |
if (io_fds[SFD_STDERR] == -1 || !isatty(STDERR_FILENO)) { |
|
sudo_debug_printf(SUDO_DEBUG_INFO, "stderr not a tty, creating a pipe"); |
if (pipe(io_pipe[STDERR_FILENO]) != 0) |
if (pipe(io_pipe[STDERR_FILENO]) != 0) |
error(1, _("unable to create pipe")); |
error(1, _("unable to create pipe")); |
iobufs = io_buf_new(io_pipe[STDERR_FILENO][0], STDERR_FILENO, |
iobufs = io_buf_new(io_pipe[STDERR_FILENO][0], STDERR_FILENO, |
Line 546 fork_pty(struct command_details *details, int sv[], in
|
Line 639 fork_pty(struct command_details *details, int sv[], in
|
|
|
/* Job control signals to relay from parent to child. */ |
/* Job control signals to relay from parent to child. */ |
sa.sa_flags = SA_INTERRUPT; /* do not restart syscalls */ |
sa.sa_flags = SA_INTERRUPT; /* do not restart syscalls */ |
|
#ifdef SA_SIGINFO |
|
sa.sa_flags |= SA_SIGINFO; |
|
sa.sa_sigaction = handler; |
|
#else |
sa.sa_handler = handler; |
sa.sa_handler = handler; |
|
#endif |
sigaction(SIGTSTP, &sa, NULL); |
sigaction(SIGTSTP, &sa, NULL); |
|
|
|
/* We don't want to receive SIGTTIN/SIGTTOU, getting EIO is preferable. */ |
|
sa.sa_handler = SIG_IGN; |
|
sigaction(SIGTTIN, &sa, NULL); |
|
sigaction(SIGTTOU, &sa, NULL); |
|
|
if (foreground) { |
if (foreground) { |
/* Copy terminal attrs from user tty -> pty slave. */ |
/* Copy terminal attrs from user tty -> pty slave. */ |
if (term_copy(io_fds[SFD_USERTTY], io_fds[SFD_SLAVE])) { |
if (term_copy(io_fds[SFD_USERTTY], io_fds[SFD_SLAVE])) { |
tty_initialized = 1; | tty_initialized = true; |
sync_ttysize(io_fds[SFD_USERTTY], io_fds[SFD_SLAVE]); |
sync_ttysize(io_fds[SFD_USERTTY], io_fds[SFD_SLAVE]); |
} |
} |
|
|
Line 567 fork_pty(struct command_details *details, int sv[], in
|
Line 670 fork_pty(struct command_details *details, int sv[], in
|
} |
} |
} |
} |
|
|
child = fork(); | /* |
| * The policy plugin's session init must be run before we fork |
| * or certain pam modules won't be able to track their state. |
| */ |
| if (policy_init_session(details) != true) |
| errorx(1, _("policy plugin failed session initialization")); |
| |
| /* |
| * Block some signals until cmnd_pid is set in the parent to avoid a |
| * race between exec of the command and receipt of a fatal signal from it. |
| */ |
| sigemptyset(&mask); |
| sigaddset(&mask, SIGTERM); |
| sigaddset(&mask, SIGHUP); |
| sigaddset(&mask, SIGINT); |
| sigaddset(&mask, SIGQUIT); |
| sigprocmask(SIG_BLOCK, &mask, omask); |
| |
| child = sudo_debug_fork(); |
switch (child) { |
switch (child) { |
case -1: |
case -1: |
error(1, _("unable to fork")); |
error(1, _("unable to fork")); |
Line 578 fork_pty(struct command_details *details, int sv[], in
|
Line 699 fork_pty(struct command_details *details, int sv[], in
|
close(signal_pipe[0]); |
close(signal_pipe[0]); |
close(signal_pipe[1]); |
close(signal_pipe[1]); |
fcntl(sv[1], F_SETFD, FD_CLOEXEC); |
fcntl(sv[1], F_SETFD, FD_CLOEXEC); |
if (exec_setup(details, slavename, io_fds[SFD_SLAVE]) == TRUE) { | sigprocmask(SIG_SETMASK, omask, NULL); |
| if (exec_setup(details, slavename, io_fds[SFD_SLAVE]) == true) { |
/* Close the other end of the stdin/stdout/stderr pipes and exec. */ |
/* Close the other end of the stdin/stdout/stderr pipes and exec. */ |
if (io_pipe[STDIN_FILENO][1]) |
if (io_pipe[STDIN_FILENO][1]) |
close(io_pipe[STDIN_FILENO][1]); |
close(io_pipe[STDIN_FILENO][1]); |
Line 590 fork_pty(struct command_details *details, int sv[], in
|
Line 712 fork_pty(struct command_details *details, int sv[], in
|
} |
} |
cstat.type = CMD_ERRNO; |
cstat.type = CMD_ERRNO; |
cstat.val = errno; |
cstat.val = errno; |
send(sv[1], &cstat, sizeof(cstat), 0); | ignore_result(send(sv[1], &cstat, sizeof(cstat), 0)); |
_exit(1); |
_exit(1); |
} |
} |
|
|
Line 618 fork_pty(struct command_details *details, int sv[], in
|
Line 740 fork_pty(struct command_details *details, int sv[], in
|
(void) fcntl(iob->wfd, F_SETFL, n | O_NONBLOCK); |
(void) fcntl(iob->wfd, F_SETFL, n | O_NONBLOCK); |
} |
} |
|
|
return child; | debug_return_int(child); |
} |
} |
|
|
void |
void |
pty_close(struct command_status *cstat) |
pty_close(struct command_status *cstat) |
{ |
{ |
int n; |
int n; |
|
debug_decl(pty_close, SUDO_DEBUG_EXEC); |
|
|
/* Flush any remaining output (the plugin already got it) */ |
/* Flush any remaining output (the plugin already got it) */ |
if (io_fds[SFD_USERTTY] != -1) { |
if (io_fds[SFD_USERTTY] != -1) { |
Line 637 pty_close(struct command_status *cstat)
|
Line 760 pty_close(struct command_status *cstat)
|
flush_output(); |
flush_output(); |
|
|
if (io_fds[SFD_USERTTY] != -1) { |
if (io_fds[SFD_USERTTY] != -1) { |
do { | check_foreground(); |
n = term_restore(io_fds[SFD_USERTTY], 0); | if (foreground) { |
} while (!n && errno == EINTR); | do { |
| n = term_restore(io_fds[SFD_USERTTY], 0); |
| } while (!n && errno == EINTR); |
| } |
} |
} |
|
|
/* If child was signalled, write the reason to stdout like the shell. */ |
/* If child was signalled, write the reason to stdout like the shell. */ |
Line 651 pty_close(struct command_status *cstat)
|
Line 777 pty_close(struct command_status *cstat)
|
io_fds[SFD_USERTTY] : STDOUT_FILENO; |
io_fds[SFD_USERTTY] : STDOUT_FILENO; |
if (write(n, reason, strlen(reason)) != -1) { |
if (write(n, reason, strlen(reason)) != -1) { |
if (WCOREDUMP(cstat->val)) { |
if (WCOREDUMP(cstat->val)) { |
if (write(n, " (core dumped)", 14) == -1) | ignore_result(write(n, " (core dumped)", 14)); |
/* shut up glibc */; | |
} |
} |
if (write(n, "\n", 1) == -1) | ignore_result(write(n, "\n", 1)); |
/* shut up glibc */; | |
} |
} |
} |
} |
} |
} |
utmp_logout(slavename, cstat->type == CMD_WSTATUS ? cstat->val : 0); /* XXX - only if CD_SET_UTMP */ |
utmp_logout(slavename, cstat->type == CMD_WSTATUS ? cstat->val : 0); /* XXX - only if CD_SET_UTMP */ |
|
debug_return; |
} |
} |
|
|
/* |
/* |
Line 670 void
|
Line 795 void
|
fd_set_iobs(fd_set *fdsr, fd_set *fdsw) |
fd_set_iobs(fd_set *fdsr, fd_set *fdsw) |
{ |
{ |
struct io_buffer *iob; |
struct io_buffer *iob; |
|
debug_decl(fd_set_iobs, SUDO_DEBUG_EXEC); |
|
|
for (iob = iobufs; iob; iob = iob->next) { |
for (iob = iobufs; iob; iob = iob->next) { |
if (iob->rfd == -1 && iob->wfd == -1) |
if (iob->rfd == -1 && iob->wfd == -1) |
Line 694 fd_set_iobs(fd_set *fdsr, fd_set *fdsw)
|
Line 820 fd_set_iobs(fd_set *fdsr, fd_set *fdsw)
|
FD_SET(iob->wfd, fdsw); |
FD_SET(iob->wfd, fdsw); |
} |
} |
} |
} |
|
debug_return; |
} |
} |
|
|
static void |
static void |
deliver_signal(pid_t pid, int signo) | deliver_signal(pid_t pid, int signo, bool from_parent) |
{ |
{ |
|
char signame[SIG2STR_MAX]; |
int status; |
int status; |
|
debug_decl(deliver_signal, SUDO_DEBUG_EXEC); |
|
|
|
if (sig2str(signo, signame) == -1) |
|
snprintf(signame, sizeof(signame), "%d", signo); |
|
|
/* Handle signal from parent. */ |
/* Handle signal from parent. */ |
sudo_debug(8, "signal %d from parent", signo); | sudo_debug_printf(SUDO_DEBUG_INFO, "received SIG%s%s", |
| signame, from_parent ? " from parent" : ""); |
switch (signo) { |
switch (signo) { |
case SIGALRM: |
case SIGALRM: |
terminate_child(pid, TRUE); | terminate_command(pid, true); |
break; |
break; |
case SIGCONT_FG: |
case SIGCONT_FG: |
/* Continue in foreground, grant it controlling tty. */ |
/* Continue in foreground, grant it controlling tty. */ |
do { |
do { |
status = tcsetpgrp(io_fds[SFD_SLAVE], child_pgrp); | status = tcsetpgrp(io_fds[SFD_SLAVE], cmnd_pgrp); |
} while (status == -1 && errno == EINTR); |
} while (status == -1 && errno == EINTR); |
killpg(pid, SIGCONT); |
killpg(pid, SIGCONT); |
break; |
break; |
Line 725 deliver_signal(pid_t pid, int signo)
|
Line 858 deliver_signal(pid_t pid, int signo)
|
_exit(1); /* XXX */ |
_exit(1); /* XXX */ |
/* NOTREACHED */ |
/* NOTREACHED */ |
default: |
default: |
/* Relay signal to child. */ | /* Relay signal to command. */ |
killpg(pid, signo); |
killpg(pid, signo); |
break; |
break; |
} |
} |
|
debug_return; |
} |
} |
|
|
/* |
/* |
Line 739 static int
|
Line 873 static int
|
send_status(int fd, struct command_status *cstat) |
send_status(int fd, struct command_status *cstat) |
{ |
{ |
int n = -1; |
int n = -1; |
|
debug_decl(send_status, SUDO_DEBUG_EXEC); |
|
|
if (cstat->type != CMD_INVALID) { |
if (cstat->type != CMD_INVALID) { |
|
sudo_debug_printf(SUDO_DEBUG_INFO, |
|
"sending status message to parent: [%d, %d]", |
|
cstat->type, cstat->val); |
do { |
do { |
n = send(fd, cstat, sizeof(*cstat), 0); |
n = send(fd, cstat, sizeof(*cstat), 0); |
} while (n == -1 && errno == EINTR); |
} while (n == -1 && errno == EINTR); |
if (n != sizeof(*cstat)) { |
if (n != sizeof(*cstat)) { |
sudo_debug(8, "unable to send status to parent: %s", | sudo_debug_printf(SUDO_DEBUG_ERROR, |
strerror(errno)); | "unable to send status to parent: %s", strerror(errno)); |
} else { | |
sudo_debug(8, "sent status to parent"); | |
} |
} |
cstat->type = CMD_INVALID; /* prevent re-sending */ |
cstat->type = CMD_INVALID; /* prevent re-sending */ |
} |
} |
return n; | debug_return_int(n); |
} |
} |
|
|
/* |
/* |
* Wait for child status after receiving SIGCHLD. | * Wait for command status after receiving SIGCHLD. |
* If the child was stopped, the status is send back to the parent. | * If the command was stopped, the status is send back to the parent. |
* Otherwise, cstat is filled in but not sent. |
* Otherwise, cstat is filled in but not sent. |
* Returns TRUE if child is still alive, else FALSE. | * Returns true if command is still alive, else false. |
*/ |
*/ |
static int | static bool |
handle_sigchld(int backchannel, struct command_status *cstat) |
handle_sigchld(int backchannel, struct command_status *cstat) |
{ |
{ |
int status, alive = TRUE; | bool alive = true; |
| int status; |
pid_t pid; |
pid_t pid; |
|
debug_decl(handle_sigchld, SUDO_DEBUG_EXEC); |
|
|
/* read child status */ | /* read command status */ |
do { |
do { |
pid = waitpid(child, &status, WUNTRACED|WNOHANG); | pid = waitpid(cmnd_pid, &status, WUNTRACED|WNOHANG); |
} while (pid == -1 && errno == EINTR); |
} while (pid == -1 && errno == EINTR); |
if (pid == child) { | if (pid == cmnd_pid) { |
if (cstat->type != CMD_ERRNO) { |
if (cstat->type != CMD_ERRNO) { |
|
char signame[SIG2STR_MAX]; |
|
|
cstat->type = CMD_WSTATUS; |
cstat->type = CMD_WSTATUS; |
cstat->val = status; |
cstat->val = status; |
if (WIFSTOPPED(status)) { |
if (WIFSTOPPED(status)) { |
sudo_debug(8, "command stopped, signal %d", WSTOPSIG(status)); | if (sig2str(WSTOPSIG(status), signame) == -1) |
| snprintf(signame, sizeof(signame), "%d", WSTOPSIG(status)); |
| sudo_debug_printf(SUDO_DEBUG_INFO, |
| "command stopped, SIG%s", signame); |
do { |
do { |
child_pgrp = tcgetpgrp(io_fds[SFD_SLAVE]); | cmnd_pgrp = tcgetpgrp(io_fds[SFD_SLAVE]); |
} while (child_pgrp == -1 && errno == EINTR); | } while (cmnd_pgrp == -1 && errno == EINTR); |
if (send_status(backchannel, cstat) == -1) |
if (send_status(backchannel, cstat) == -1) |
return alive; /* XXX */ |
return alive; /* XXX */ |
} else if (WIFSIGNALED(status)) { |
} else if (WIFSIGNALED(status)) { |
sudo_debug(8, "command killed, signal %d", WTERMSIG(status)); | if (sig2str(WTERMSIG(status), signame) == -1) |
| snprintf(signame, sizeof(signame), "%d", WTERMSIG(status)); |
| sudo_debug_printf(SUDO_DEBUG_INFO, |
| "command killed, SIG%s", signame); |
} else { |
} else { |
sudo_debug(8, "command exited: %d", WEXITSTATUS(status)); | sudo_debug_printf(SUDO_DEBUG_INFO, "command exited: %d", |
| WEXITSTATUS(status)); |
} |
} |
} |
} |
if (!WIFSTOPPED(status)) |
if (!WIFSTOPPED(status)) |
alive = FALSE; | alive = false; |
} |
} |
return alive; | debug_return_bool(alive); |
} |
} |
|
|
/* |
/* |
Line 809 exec_monitor(struct command_details *details, int back
|
Line 956 exec_monitor(struct command_details *details, int back
|
fd_set *fdsr; |
fd_set *fdsr; |
sigaction_t sa; |
sigaction_t sa; |
int errpipe[2], maxfd, n, status; |
int errpipe[2], maxfd, n, status; |
int alive = TRUE; | bool alive = true; |
unsigned char signo; |
unsigned char signo; |
|
debug_decl(exec_monitor, SUDO_DEBUG_EXEC); |
|
|
/* Close unused fds. */ |
/* Close unused fds. */ |
if (io_fds[SFD_MASTER] != -1) |
if (io_fds[SFD_MASTER] != -1) |
Line 840 exec_monitor(struct command_details *details, int back
|
Line 988 exec_monitor(struct command_details *details, int back
|
|
|
/* Note: HP-UX select() will not be interrupted if SA_RESTART set */ |
/* Note: HP-UX select() will not be interrupted if SA_RESTART set */ |
sa.sa_flags = SA_INTERRUPT; |
sa.sa_flags = SA_INTERRUPT; |
sa.sa_handler = handler; | #ifdef SA_SIGINFO |
| sa.sa_flags |= SA_SIGINFO; |
| sa.sa_sigaction = mon_handler; |
| #else |
| sa.sa_handler = mon_handler; |
| #endif |
sigaction(SIGCHLD, &sa, NULL); |
sigaction(SIGCHLD, &sa, NULL); |
|
|
|
/* Catch common signals so we can cleanup properly. */ |
|
sa.sa_flags = SA_RESTART; |
|
#ifdef SA_SIGINFO |
|
sa.sa_flags |= SA_SIGINFO; |
|
sa.sa_sigaction = mon_handler; |
|
#else |
|
sa.sa_handler = mon_handler; |
|
#endif |
|
sigaction(SIGHUP, &sa, NULL); |
|
sigaction(SIGINT, &sa, NULL); |
|
sigaction(SIGQUIT, &sa, NULL); |
|
sigaction(SIGTERM, &sa, NULL); |
|
sigaction(SIGTSTP, &sa, NULL); |
|
sigaction(SIGUSR1, &sa, NULL); |
|
sigaction(SIGUSR2, &sa, NULL); |
|
|
/* |
/* |
* Start a new session with the parent as the session leader |
* Start a new session with the parent as the session leader |
* and the slave pty as the controlling terminal. |
* and the slave pty as the controlling terminal. |
* This allows us to be notified when the child has been suspended. | * This allows us to be notified when the command has been suspended. |
*/ |
*/ |
if (setsid() == -1) { |
if (setsid() == -1) { |
warning("setsid"); |
warning("setsid"); |
Line 870 exec_monitor(struct command_details *details, int back
|
Line 1039 exec_monitor(struct command_details *details, int back
|
* when it needs access to the controlling tty. |
* when it needs access to the controlling tty. |
*/ |
*/ |
if (pipeline) |
if (pipeline) |
foreground = 0; | foreground = false; |
|
|
/* Start command and wait for it to stop or exit */ |
/* Start command and wait for it to stop or exit */ |
if (pipe(errpipe) == -1) |
if (pipe(errpipe) == -1) |
error(1, _("unable to create pipe")); |
error(1, _("unable to create pipe")); |
child = fork(); | cmnd_pid = sudo_debug_fork(); |
if (child == -1) { | if (cmnd_pid == -1) { |
warning(_("unable to fork")); |
warning(_("unable to fork")); |
goto bad; |
goto bad; |
} |
} |
if (child == 0) { | if (cmnd_pid == 0) { |
/* We pass errno back to our parent via pipe on exec failure. */ |
/* We pass errno back to our parent via pipe on exec failure. */ |
close(backchannel); |
close(backchannel); |
close(signal_pipe[0]); |
close(signal_pipe[0]); |
Line 890 exec_monitor(struct command_details *details, int back
|
Line 1059 exec_monitor(struct command_details *details, int back
|
restore_signals(); |
restore_signals(); |
|
|
/* setup tty and exec command */ |
/* setup tty and exec command */ |
exec_pty(details); | exec_pty(details, &errpipe[1]); |
cstat.type = CMD_ERRNO; |
cstat.type = CMD_ERRNO; |
cstat.val = errno; |
cstat.val = errno; |
if (write(errpipe[1], &cstat, sizeof(cstat)) == -1) | ignore_result(write(errpipe[1], &cstat, sizeof(cstat))); |
/* shut up glibc */; | |
_exit(1); |
_exit(1); |
} |
} |
close(errpipe[1]); |
close(errpipe[1]); |
|
|
|
/* Send the command's pid to main sudo process. */ |
|
cstat.type = CMD_PID; |
|
cstat.val = cmnd_pid; |
|
ignore_result(send(backchannel, &cstat, sizeof(cstat), 0)); |
|
|
/* If any of stdin/stdout/stderr are pipes, close them in parent. */ |
/* If any of stdin/stdout/stderr are pipes, close them in parent. */ |
if (io_fds[SFD_STDIN] != io_fds[SFD_SLAVE]) |
if (io_fds[SFD_STDIN] != io_fds[SFD_SLAVE]) |
close(io_fds[SFD_STDIN]); |
close(io_fds[SFD_STDIN]); |
Line 908 exec_monitor(struct command_details *details, int back
|
Line 1081 exec_monitor(struct command_details *details, int back
|
close(io_fds[SFD_STDERR]); |
close(io_fds[SFD_STDERR]); |
|
|
/* |
/* |
* Put child in its own process group. If we are starting the command | * Put command in its own process group. If we are starting the command |
* in the foreground, assign its pgrp to the tty. |
* in the foreground, assign its pgrp to the tty. |
*/ |
*/ |
child_pgrp = child; | cmnd_pgrp = cmnd_pid; |
setpgid(child, child_pgrp); | setpgid(cmnd_pid, cmnd_pgrp); |
if (foreground) { |
if (foreground) { |
do { |
do { |
status = tcsetpgrp(io_fds[SFD_SLAVE], child_pgrp); | status = tcsetpgrp(io_fds[SFD_SLAVE], cmnd_pgrp); |
} while (status == -1 && errno == EINTR); |
} while (status == -1 && errno == EINTR); |
} |
} |
|
|
/* Wait for errno on pipe, signal on backchannel or for SIGCHLD */ |
/* Wait for errno on pipe, signal on backchannel or for SIGCHLD */ |
maxfd = MAX(MAX(errpipe[0], signal_pipe[0]), backchannel); |
maxfd = MAX(MAX(errpipe[0], signal_pipe[0]), backchannel); |
fdsr = (fd_set *)emalloc2(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask)); | fdsr = ecalloc(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask)); |
zero_bytes(fdsr, howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask)); | memset(&cstat, 0, sizeof(cstat)); |
zero_bytes(&cstat, sizeof(cstat)); | |
tv.tv_sec = 0; |
tv.tv_sec = 0; |
tv.tv_usec = 0; |
tv.tv_usec = 0; |
for (;;) { |
for (;;) { |
Line 939 exec_monitor(struct command_details *details, int back
|
Line 1111 exec_monitor(struct command_details *details, int back
|
if (n <= 0) { |
if (n <= 0) { |
if (n == 0) |
if (n == 0) |
goto done; |
goto done; |
if (errno == EINTR) | if (errno == EINTR || errno == ENOMEM) |
continue; |
continue; |
error(1, _("select failed")); | warning("monitor: %s", _("select failed")); |
| break; |
} |
} |
|
|
if (FD_ISSET(signal_pipe[0], fdsr)) { |
if (FD_ISSET(signal_pipe[0], fdsr)) { |
Line 954 exec_monitor(struct command_details *details, int back
|
Line 1127 exec_monitor(struct command_details *details, int back
|
} |
} |
/* |
/* |
* Handle SIGCHLD specially and deliver other signals |
* Handle SIGCHLD specially and deliver other signals |
* directly to the child. | * directly to the command. |
*/ |
*/ |
if (signo == SIGCHLD) | if (signo == SIGCHLD) { |
alive = handle_sigchld(backchannel, &cstat); | if (!handle_sigchld(backchannel, &cstat)) |
else | alive = false; |
deliver_signal(child, signo); | } else { |
| deliver_signal(cmnd_pid, signo, false); |
| } |
continue; |
continue; |
} |
} |
if (errpipe[0] != -1 && FD_ISSET(errpipe[0], fdsr)) { |
if (errpipe[0] != -1 && FD_ISSET(errpipe[0], fdsr)) { |
Line 992 exec_monitor(struct command_details *details, int back
|
Line 1167 exec_monitor(struct command_details *details, int back
|
cstmp.type); |
cstmp.type); |
continue; |
continue; |
} |
} |
deliver_signal(child, cstmp.val); | deliver_signal(cmnd_pid, cstmp.val, true); |
} |
} |
} |
} |
|
|
done: |
done: |
if (alive) { |
if (alive) { |
/* XXX An error occurred, should send an error back. */ |
/* XXX An error occurred, should send an error back. */ |
kill(child, SIGKILL); | kill(cmnd_pid, SIGKILL); |
} else { |
} else { |
/* Send parent status. */ |
/* Send parent status. */ |
send_status(backchannel, &cstat); |
send_status(backchannel, &cstat); |
} |
} |
|
sudo_debug_exit_int(__func__, __FILE__, __LINE__, sudo_debug_subsys, 1); |
_exit(1); |
_exit(1); |
|
|
bad: |
bad: |
return errno; | debug_return_int(errno); |
} |
} |
|
|
/* |
/* |
Line 1021 flush_output(void)
|
Line 1197 flush_output(void)
|
struct timeval tv; |
struct timeval tv; |
fd_set *fdsr, *fdsw; |
fd_set *fdsr, *fdsw; |
int nready, nwriters, maxfd = -1; |
int nready, nwriters, maxfd = -1; |
|
debug_decl(flush_output, SUDO_DEBUG_EXEC); |
|
|
/* Determine maxfd */ |
/* Determine maxfd */ |
for (iob = iobufs; iob; iob = iob->next) { |
for (iob = iobufs; iob; iob = iob->next) { |
Line 1030 flush_output(void)
|
Line 1207 flush_output(void)
|
maxfd = iob->wfd; |
maxfd = iob->wfd; |
} |
} |
if (maxfd == -1) |
if (maxfd == -1) |
return; | debug_return; |
|
|
fdsr = (fd_set *)emalloc2(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask)); | fdsr = emalloc2(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask)); |
fdsw = (fd_set *)emalloc2(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask)); | fdsw = emalloc2(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask)); |
for (;;) { |
for (;;) { |
zero_bytes(fdsw, howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask)); | memset(fdsw, 0, howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask)); |
zero_bytes(fdsr, howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask)); | memset(fdsr, 0, howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask)); |
|
|
nwriters = 0; |
nwriters = 0; |
for (iob = iobufs; iob; iob = iob->next) { |
for (iob = iobufs; iob; iob = iob->next) { |
Line 1072 flush_output(void)
|
Line 1249 flush_output(void)
|
if (nready <= 0) { |
if (nready <= 0) { |
if (nready == 0) |
if (nready == 0) |
break; /* all I/O flushed */ |
break; /* all I/O flushed */ |
if (errno == EINTR) | if (errno == EINTR || errno == ENOMEM) |
continue; |
continue; |
error(1, _("select failed")); | warning(_("select failed")); |
} |
} |
if (perform_io(fdsr, fdsw, NULL) != 0) | if (perform_io(fdsr, fdsw, NULL) != 0 || nready == -1) |
break; |
break; |
} |
} |
efree(fdsr); |
efree(fdsr); |
efree(fdsw); |
efree(fdsw); |
|
debug_return; |
} |
} |
|
|
/* |
/* |
Line 1088 flush_output(void)
|
Line 1266 flush_output(void)
|
* Returns only if execve() fails. |
* Returns only if execve() fails. |
*/ |
*/ |
static void |
static void |
exec_pty(struct command_details *details) | exec_pty(struct command_details *details, int *errfd) |
{ |
{ |
pid_t self = getpid(); |
pid_t self = getpid(); |
|
debug_decl(exec_pty, SUDO_DEBUG_EXEC); |
|
|
/* Set child process group here too to avoid a race. */ | /* Set command process group here too to avoid a race. */ |
setpgid(0, self); |
setpgid(0, self); |
|
|
/* Wire up standard fds, note that stdout/stderr may be pipes. */ |
/* Wire up standard fds, note that stdout/stderr may be pipes. */ |
Line 1117 exec_pty(struct command_details *details)
|
Line 1296 exec_pty(struct command_details *details)
|
if (io_fds[SFD_STDERR] != io_fds[SFD_SLAVE]) |
if (io_fds[SFD_STDERR] != io_fds[SFD_SLAVE]) |
close(io_fds[SFD_STDERR]); |
close(io_fds[SFD_STDERR]); |
|
|
if (details->closefrom >= 0) | sudo_debug_execve(SUDO_DEBUG_INFO, details->command, |
closefrom(details->closefrom); | details->argv, details->envp); |
| |
| if (details->closefrom >= 0) { |
| int maxfd = details->closefrom; |
| dup2(*errfd, maxfd); |
| (void)fcntl(maxfd, F_SETFD, FD_CLOEXEC); |
| *errfd = maxfd++; |
| if (sudo_debug_fd_set(maxfd) != -1) |
| maxfd++; |
| closefrom(maxfd); |
| } |
#ifdef HAVE_SELINUX |
#ifdef HAVE_SELINUX |
if (ISSET(details->flags, CD_RBAC_ENABLED)) | if (ISSET(details->flags, CD_RBAC_ENABLED)) { |
selinux_execve(details->command, details->argv, details->envp); | selinux_execve(details->command, details->argv, details->envp, |
else | ISSET(details->flags, CD_NOEXEC)); |
| } else |
#endif |
#endif |
my_execve(details->command, details->argv, details->envp); | { |
| sudo_execve(details->command, details->argv, details->envp, |
| ISSET(details->flags, CD_NOEXEC)); |
| } |
| sudo_debug_printf(SUDO_DEBUG_ERROR, "unable to exec %s: %s", |
| details->command, strerror(errno)); |
| debug_return; |
} |
} |
|
|
/* |
/* |
Line 1136 sync_ttysize(int src, int dst)
|
Line 1332 sync_ttysize(int src, int dst)
|
#ifdef TIOCGWINSZ |
#ifdef TIOCGWINSZ |
struct winsize wsize; |
struct winsize wsize; |
pid_t pgrp; |
pid_t pgrp; |
|
debug_decl(sync_ttysize, SUDO_DEBUG_EXEC); |
|
|
if (ioctl(src, TIOCGWINSZ, &wsize) == 0) { |
if (ioctl(src, TIOCGWINSZ, &wsize) == 0) { |
ioctl(dst, TIOCSWINSZ, &wsize); |
ioctl(dst, TIOCSWINSZ, &wsize); |
if ((pgrp = tcgetpgrp(dst)) != -1) |
if ((pgrp = tcgetpgrp(dst)) != -1) |
killpg(pgrp, SIGWINCH); |
killpg(pgrp, SIGWINCH); |
} |
} |
|
|
|
debug_return; |
#endif |
#endif |
} |
} |
|
|
Line 1164 sigwinch(int s)
|
Line 1363 sigwinch(int s)
|
static int |
static int |
safe_close(int fd) |
safe_close(int fd) |
{ |
{ |
|
debug_decl(safe_close, SUDO_DEBUG_EXEC); |
|
|
/* Avoid closing /dev/tty or std{in,out,err}. */ |
/* Avoid closing /dev/tty or std{in,out,err}. */ |
if (fd < 3 || fd == io_fds[SFD_USERTTY]) { |
if (fd < 3 || fd == io_fds[SFD_USERTTY]) { |
errno = EINVAL; |
errno = EINVAL; |
return -1; | debug_return_int(-1); |
} |
} |
return close(fd); | debug_return_int(close(fd)); |
} |
} |