version 1.1.1.1, 2012/02/21 16:23:02
|
version 1.1.1.5, 2014/06/15 16:12:55
|
Line 1
|
Line 1
|
/* |
/* |
* Copyright (c) 2009-2011 Todd C. Miller <Todd.Miller@courtesan.com> | * Copyright (c) 2009-2013 Todd C. Miller <Todd.Miller@courtesan.com> |
* |
* |
* Permission to use, copy, modify, and distribute this software for any |
* Permission to use, copy, modify, and distribute this software for any |
* purpose with or without fee is hereby granted, provided that the above |
* purpose with or without fee is hereby granted, provided that the above |
Line 17
|
Line 17
|
#include <config.h> |
#include <config.h> |
|
|
#include <sys/types.h> |
#include <sys/types.h> |
#include <sys/param.h> |
|
#ifdef HAVE_SYS_SYSMACROS_H |
|
# include <sys/sysmacros.h> |
|
#endif |
|
#include <sys/socket.h> |
#include <sys/socket.h> |
#include <sys/time.h> |
#include <sys/time.h> |
#include <sys/wait.h> |
#include <sys/wait.h> |
#include <sys/ioctl.h> |
#include <sys/ioctl.h> |
#ifdef HAVE_SYS_SELECT_H |
|
# include <sys/select.h> |
|
#endif /* HAVE_SYS_SELECT_H */ |
|
#include <stdio.h> |
#include <stdio.h> |
#ifdef STDC_HEADERS |
#ifdef STDC_HEADERS |
# include <stdlib.h> |
# include <stdlib.h> |
Line 49
|
Line 42
|
#ifdef HAVE_UNISTD_H |
#ifdef HAVE_UNISTD_H |
# include <unistd.h> |
# include <unistd.h> |
#endif /* HAVE_UNISTD_H */ |
#endif /* HAVE_UNISTD_H */ |
#if TIME_WITH_SYS_TIME | #ifdef TIME_WITH_SYS_TIME |
# include <time.h> |
# include <time.h> |
#endif |
#endif |
#include <errno.h> |
#include <errno.h> |
Line 58
|
Line 51
|
#include <termios.h> |
#include <termios.h> |
|
|
#include "sudo.h" |
#include "sudo.h" |
|
#include "sudo_event.h" |
#include "sudo_exec.h" |
#include "sudo_exec.h" |
#include "sudo_plugin.h" |
#include "sudo_plugin.h" |
#include "sudo_plugin_int.h" |
#include "sudo_plugin_int.h" |
Line 69
|
Line 63
|
#define SFD_SLAVE 4 |
#define SFD_SLAVE 4 |
#define SFD_USERTTY 5 |
#define SFD_USERTTY 5 |
|
|
|
/* Evaluates to true if the event has /dev/tty as its fd. */ |
|
#define USERTTY_EVENT(_ev) (sudo_ev_get_fd((_ev)) == io_fds[SFD_USERTTY]) |
|
|
#define TERM_COOKED 0 |
#define TERM_COOKED 0 |
#define TERM_RAW 1 |
#define TERM_RAW 1 |
|
|
Line 80
|
Line 77
|
#endif |
#endif |
|
|
struct io_buffer { |
struct io_buffer { |
struct io_buffer *next; | SLIST_ENTRY(io_buffer) entries; |
| struct sudo_event *revent; |
| struct sudo_event *wevent; |
| bool (*action)(const char *buf, unsigned int len); |
int len; /* buffer length (how much produced) */ |
int len; /* buffer length (how much produced) */ |
int off; /* write position (how much already consumed) */ |
int off; /* write position (how much already consumed) */ |
int rfd; /* reader (producer) */ | char buf[32 * 1024]; |
int wfd; /* writer (consumer) */ | |
int (*action)(const char *buf, unsigned int len); | |
char buf[16 * 1024]; | |
}; |
}; |
|
|
|
SLIST_HEAD(io_buffer_list, io_buffer); |
|
|
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, mon_pgrp; |
static sigset_t ttyblock; |
static sigset_t ttyblock; |
static struct io_buffer *iobufs; | static struct io_buffer_list iobufs; |
|
|
static void flush_output(void); | static void del_io_events(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 *details, |
| struct command_status *cstat, 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 ev_free_by_fd(struct sudo_event_base *evbase, int fd); |
|
static void check_foreground(void); |
|
|
/* |
/* |
* Cleanup hook for error()/errorx() | * Cleanup hook for fatal()/fatalx() |
*/ |
*/ |
void | static void |
cleanup(int gotsignal) | pty_cleanup(void) |
{ |
{ |
if (!tq_empty(&io_plugins)) | debug_decl(cleanup, SUDO_DEBUG_EXEC); |
| |
| if (!TAILQ_EMPTY(&io_plugins) && io_fds[SFD_USERTTY] != -1) |
term_restore(io_fds[SFD_USERTTY], 0); |
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 |
|
static 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 |
|
static 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 172 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], |
slavename, sizeof(slavename), uid)) |
slavename, sizeof(slavename), uid)) |
error(1, _("unable to allocate pty")); | fatal(U_("unable to allocate pty")); |
/* Add entry to utmp/utmpx? */ |
/* Add entry to utmp/utmpx? */ |
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); |
| TAILQ_FOREACH(plugin, &io_plugins, entries) { |
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); |
| TAILQ_FOREACH(plugin, &io_plugins, entries) { |
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); |
| TAILQ_FOREACH(plugin, &io_plugins, entries) { |
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); |
| TAILQ_FOREACH(plugin, &io_plugins, entries) { |
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); |
| TAILQ_FOREACH(plugin, &io_plugins, entries) { |
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 310 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, 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 sudo is already the foreground process, just resume the command |
* Otherwise, re-send the signal with the handler disabled. | * in the foreground. If not, we'll suspend sudo and resume later. |
*/ |
*/ |
if (!foreground) |
if (!foreground) |
check_foreground(); |
check_foreground(); |
Line 301 suspend_parent(int signo)
|
Line 354 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; |
|
/* FALLTHROUGH */ |
/* FALLTHROUGH */ |
case SIGSTOP: |
case SIGSTOP: |
case SIGTSTP: |
case SIGTSTP: |
/* Flush any remaining output before suspending. */ | /* Flush any remaining output and deschedule I/O events. */ |
flush_output(); | del_io_events(); |
|
|
/* Restore original tty mode before suspending. */ |
/* Restore original tty mode before suspending. */ |
if (oldmode != TERM_COOKED) { | if (ttymode != TERM_COOKED) |
do { | term_restore(io_fds[SFD_USERTTY], 0); |
n = term_restore(io_fds[SFD_USERTTY], 0); | |
} while (!n && errno == EINTR); | |
} | |
|
|
/* Suspend self and continue child when we resume. */ | if (sig2str(signo, signame) == -1) |
sa.sa_handler = SIG_DFL; | snprintf(signame, sizeof(signame), "%d", signo); |
sigaction(signo, &sa, &osa); | |
sudo_debug(8, "kill parent %d", signo); | /* Suspend self and continue command when we resume. */ |
| if (signo != SIGSTOP) { |
| memset(&sa, 0, sizeof(sa)); |
| sigemptyset(&sa.sa_mask); |
| sa.sa_flags = SA_RESTART; |
| sa.sa_handler = SIG_DFL; |
| sudo_sigaction(signo, &sa, &osa); |
| } |
| 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 | * We always resume the command in the foreground if sudo itself |
* the old tty mode was not cooked or child got SIGTT{IN,OU} | * is the foreground process. This helps work around poorly behaved |
| * programs that catch SIGTTOU/SIGTTIN but suspend themselves with |
| * SIGSTOP. At worst, sudo will go into the background but upon |
| * resume the command will be runnable. Otherwise, we can get into |
| * a situation where the command will immediately suspend itself. |
*/ |
*/ |
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", ttymode, |
| foreground ? TERM_RAW : TERM_COOKED); |
|
|
if (ttymode != TERM_COOKED) { | if (foreground) { |
if (foreground) { | /* Foreground process, set tty to raw mode. */ |
/* Set raw mode. */ | do { |
do { | n = term_raw(io_fds[SFD_USERTTY], 0); |
n = term_raw(io_fds[SFD_USERTTY], 0); | } while (!n && errno == EINTR); |
} while (!n && errno == EINTR); | ttymode = TERM_RAW; |
} else { | } else { |
/* Background process, no access to tty. */ | /* Background process, no access to tty. */ |
ttymode = TERM_COOKED; | ttymode = TERM_COOKED; |
} | |
} |
} |
|
|
sigaction(signo, &osa, NULL); | if (signo != SIGSTOP) |
| sudo_sigaction(signo, &osa, NULL); |
rval = ttymode == TERM_RAW ? SIGCONT_FG : SIGCONT_BG; |
rval = ttymode == TERM_RAW ? SIGCONT_FG : SIGCONT_BG; |
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); |
} |
} |
} |
|
|
|
static struct io_buffer * | debug_return; |
io_buf_new(int rfd, int wfd, int (*action)(const char *, unsigned int), | |
struct io_buffer *head) | |
{ | |
struct io_buffer *iob; | |
| |
iob = emalloc(sizeof(*iob)); | |
zero_bytes(iob, sizeof(*iob)); | |
iob->rfd = rfd; | |
iob->wfd = wfd; | |
iob->action = action; | |
iob->next = head; | |
return iob; | |
} |
} |
|
|
/* |
/* |
* Read/write iobufs depending on fdsr and fdsw. | * Read/write an iobuf that is ready. |
* Returns the number of errors. | |
*/ |
*/ |
int | static void |
perform_io(fd_set *fdsr, fd_set *fdsw, struct command_status *cstat) | io_callback(int fd, int what, void *v) |
{ |
{ |
struct io_buffer *iob; | struct io_buffer *iob = v; |
int n, errors = 0; | struct sudo_event_base *evbase; |
| int n; |
| debug_decl(io_callback, SUDO_DEBUG_EXEC); |
|
|
for (iob = iobufs; iob; iob = iob->next) { | if (ISSET(what, SUDO_EV_READ)) { |
if (iob->rfd != -1 && FD_ISSET(iob->rfd, fdsr)) { | evbase = sudo_ev_get_base(iob->revent); |
do { | do { |
n = read(iob->rfd, iob->buf + iob->len, | n = read(fd, iob->buf + iob->len, sizeof(iob->buf) - iob->len); |
sizeof(iob->buf) - iob->len); | } 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; | |
if (errno != ENXIO && errno != EBADF) { | |
errors++; | |
break; | |
} | |
/* FALLTHROUGH */ | |
case 0: | |
/* got EOF or pty has gone away */ | |
safe_close(iob->rfd); | |
iob->rfd = -1; | |
break; |
break; |
default: | /* treat read error as fatal and close the fd */ |
if (!iob->action(iob->buf + iob->len, n)) | sudo_debug_printf(SUDO_DEBUG_ERROR, |
terminate_child(child, TRUE); | "error reading fd %d: %s", fd, strerror(errno)); |
iob->len += n; | /* FALLTHROUGH */ |
break; | case 0: |
} | /* got EOF or pty has gone away */ |
| if (n == 0) { |
| sudo_debug_printf(SUDO_DEBUG_INFO, |
| "read EOF from fd %d", fd); |
| } |
| safe_close(fd); |
| ev_free_by_fd(evbase, fd); |
| /* If writer already consumed the buffer, close it too. */ |
| if (iob->wevent != NULL && iob->off == iob->len) { |
| safe_close(sudo_ev_get_fd(iob->wevent)); |
| ev_free_by_fd(evbase, sudo_ev_get_fd(iob->wevent)); |
| iob->off = iob->len = 0; |
| } |
| break; |
| default: |
| sudo_debug_printf(SUDO_DEBUG_INFO, |
| "read %d bytes from fd %d", n, fd); |
| if (!iob->action(iob->buf + iob->len, n)) |
| terminate_command(cmnd_pid, true); |
| iob->len += n; |
| /* Enable writer if not /dev/tty or we are foreground pgrp. */ |
| if (iob->wevent != NULL && |
| (foreground || !USERTTY_EVENT(iob->wevent))) { |
| if (sudo_ev_add(evbase, iob->wevent, NULL, false) == -1) |
| fatal(U_("unable to add event to queue")); |
| } |
| /* Re-enable reader if buffer is not full. */ |
| if (iob->len != sizeof(iob->buf)) { |
| if (sudo_ev_add(evbase, iob->revent, NULL, false) == -1) |
| fatal(U_("unable to add event to queue")); |
| } |
| break; |
} |
} |
if (iob->wfd != -1 && FD_ISSET(iob->wfd, fdsw)) { | } |
do { | if (ISSET(what, SUDO_EV_WRITE)) { |
n = write(iob->wfd, iob->buf + iob->off, | evbase = sudo_ev_get_base(iob->wevent); |
iob->len - iob->off); | do { |
} while (n == -1 && errno == EINTR); | n = write(fd, iob->buf + iob->off, iob->len - iob->off); |
if (n == -1) { | } while (n == -1 && errno == EINTR); |
if (errno == EPIPE || errno == ENXIO || errno == EBADF) { | if (n == -1) { |
/* other end of pipe closed or pty revoked */ | switch (errno) { |
if (iob->rfd != -1) { | case EPIPE: |
safe_close(iob->rfd); | case ENXIO: |
iob->rfd = -1; | case EIO: |
} | case EBADF: |
safe_close(iob->wfd); | /* other end of pipe closed or pty revoked */ |
iob->wfd = -1; | sudo_debug_printf(SUDO_DEBUG_INFO, |
continue; | "unable to write %d bytes to fd %d", |
| iob->len - iob->off, fd); |
| if (iob->revent != NULL) { |
| safe_close(sudo_ev_get_fd(iob->revent)); |
| ev_free_by_fd(evbase, sudo_ev_get_fd(iob->revent)); |
} |
} |
if (errno != EAGAIN) | safe_close(fd); |
errors++; | ev_free_by_fd(evbase, fd); |
} else { | break; |
iob->off += n; | case EAGAIN: |
| /* not an error */ |
| break; |
| default: |
| #if 0 /* XXX -- how to set cstat? stash in iobufs instead? */ |
| if (cstat != NULL) { |
| cstat->type = CMD_ERRNO; |
| cstat->val = errno; |
| } |
| #endif /* XXX */ |
| sudo_debug_printf(SUDO_DEBUG_ERROR, |
| "error writing fd %d: %s", fd, strerror(errno)); |
| sudo_ev_loopbreak(evbase); |
| break; |
} |
} |
|
} else { |
|
sudo_debug_printf(SUDO_DEBUG_INFO, |
|
"wrote %d bytes to fd %d", n, fd); |
|
iob->off += n; |
|
/* Reset buffer if fully consumed. */ |
|
if (iob->off == iob->len) { |
|
iob->off = iob->len = 0; |
|
/* Forward the EOF from reader to writer. */ |
|
if (iob->revent == NULL) { |
|
safe_close(fd); |
|
ev_free_by_fd(evbase, fd); |
|
} |
|
} |
|
/* Re-enable writer if buffer is not empty. */ |
|
if (iob->len > iob->off) { |
|
if (sudo_ev_add(evbase, iob->wevent, NULL, false) == -1) |
|
fatal(U_("unable to add event to queue")); |
|
} |
|
/* Enable reader if buffer is not full. */ |
|
if (iob->revent != NULL && |
|
(ttymode == TERM_RAW || !USERTTY_EVENT(iob->revent))) { |
|
if (iob->len != sizeof(iob->buf)) { |
|
if (sudo_ev_add(evbase, iob->revent, NULL, false) == -1) |
|
fatal(U_("unable to add event to queue")); |
|
} |
|
} |
} |
} |
} |
} |
if (errors && cstat != NULL) { |
|
cstat->type = CMD_ERRNO; |
|
cstat->val = errno; |
|
} |
|
return errors; |
|
} |
} |
|
|
|
static void |
|
io_buf_new(int rfd, int wfd, bool (*action)(const char *, unsigned int), |
|
struct io_buffer_list *head) |
|
{ |
|
int n; |
|
struct io_buffer *iob; |
|
debug_decl(io_buf_new, SUDO_DEBUG_EXEC); |
|
|
|
/* Set non-blocking mode. */ |
|
n = fcntl(rfd, F_GETFL, 0); |
|
if (n != -1 && !ISSET(n, O_NONBLOCK)) |
|
(void) fcntl(rfd, F_SETFL, n | O_NONBLOCK); |
|
n = fcntl(wfd, F_GETFL, 0); |
|
if (n != -1 && !ISSET(n, O_NONBLOCK)) |
|
(void) fcntl(wfd, F_SETFL, n | O_NONBLOCK); |
|
|
|
/* Allocate and add to head of list. */ |
|
iob = emalloc(sizeof(*iob)); |
|
iob->revent = sudo_ev_alloc(rfd, SUDO_EV_READ, io_callback, iob); |
|
iob->wevent = sudo_ev_alloc(wfd, SUDO_EV_WRITE, io_callback, iob); |
|
iob->len = 0; |
|
iob->off = 0; |
|
iob->action = action; |
|
iob->buf[0] = '\0'; |
|
if (iob->revent == NULL || iob->wevent == NULL) |
|
fatal(NULL); |
|
SLIST_INSERT_HEAD(head, iob, entries); |
|
|
|
debug_return; |
|
} |
|
|
/* |
/* |
* Fork a monitor process which runs the actual command as its own child |
* Fork a monitor process which runs the actual command as its own child |
* process with std{in,out,err} hooked up to the pty or pipes as appropriate. |
* process with std{in,out,err} hooked up to the pty or pipes as appropriate. |
* 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[], sigset_t *omask) |
{ |
{ |
struct command_status cstat; |
struct command_status cstat; |
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 */ |
| |
zero_bytes(&sa, sizeof(sa)); | memset(&sa, 0, sizeof(sa)); |
sigemptyset(&sa.sa_mask); |
sigemptyset(&sa.sa_mask); |
| |
if (io_fds[SFD_USERTTY] != -1) { |
if (io_fds[SFD_USERTTY] != -1) { |
sa.sa_flags = SA_RESTART; |
sa.sa_flags = SA_RESTART; |
sa.sa_handler = sigwinch; |
sa.sa_handler = sigwinch; |
sigaction(SIGWINCH, &sa, NULL); | sudo_sigaction(SIGWINCH, &sa, NULL); |
} |
} |
|
|
/* So we can block tty-generated signals */ |
/* So we can block tty-generated signals */ |
Line 503 fork_pty(struct command_details *details, int sv[], in
|
Line 652 fork_pty(struct command_details *details, int sv[], in
|
if (io_fds[SFD_USERTTY] != -1) { |
if (io_fds[SFD_USERTTY] != -1) { |
/* Read from /dev/tty, write to pty master */ |
/* Read from /dev/tty, write to pty master */ |
if (!ISSET(details->flags, CD_BACKGROUND)) { |
if (!ISSET(details->flags, CD_BACKGROUND)) { |
iobufs = io_buf_new(io_fds[SFD_USERTTY], io_fds[SFD_MASTER], | io_buf_new(io_fds[SFD_USERTTY], io_fds[SFD_MASTER], |
log_ttyin, iobufs); | log_ttyin, &iobufs); |
} |
} |
|
|
/* Read from pty master, write to /dev/tty */ |
/* Read from pty master, write to /dev/tty */ |
iobufs = io_buf_new(io_fds[SFD_MASTER], io_fds[SFD_USERTTY], | io_buf_new(io_fds[SFD_MASTER], io_fds[SFD_USERTTY], |
log_ttyout, iobufs); | log_ttyout, &iobufs); |
|
|
/* Are we the foreground process? */ |
/* Are we the foreground process? */ |
foreground = tcgetpgrp(io_fds[SFD_USERTTY]) == ppgrp; |
foreground = tcgetpgrp(io_fds[SFD_USERTTY]) == ppgrp; |
Line 521 fork_pty(struct command_details *details, int sv[], in
|
Line 670 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")); | fatal(U_("unable to create pipe")); |
iobufs = io_buf_new(STDIN_FILENO, io_pipe[STDIN_FILENO][1], | io_buf_new(STDIN_FILENO, io_pipe[STDIN_FILENO][1], |
log_stdin, iobufs); | log_stdin, &iobufs); |
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")); | fatal(U_("unable to create pipe")); |
iobufs = io_buf_new(io_pipe[STDOUT_FILENO][0], STDOUT_FILENO, | io_buf_new(io_pipe[STDOUT_FILENO][0], STDOUT_FILENO, |
log_stdout, iobufs); | log_stdout, &iobufs); |
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")); | fatal(U_("unable to create pipe")); |
iobufs = io_buf_new(io_pipe[STDERR_FILENO][0], STDERR_FILENO, | io_buf_new(io_pipe[STDERR_FILENO][0], STDERR_FILENO, |
log_stderr, iobufs); | log_stderr, &iobufs); |
io_fds[SFD_STDERR] = io_pipe[STDERR_FILENO][1]; |
io_fds[SFD_STDERR] = io_pipe[STDERR_FILENO][1]; |
} |
} |
|
|
|
/* We don't want to receive SIGTTIN/SIGTTOU, getting EIO is preferable. */ |
|
sa.sa_handler = SIG_IGN; |
|
sudo_sigaction(SIGTTIN, &sa, NULL); |
|
sudo_sigaction(SIGTTOU, &sa, NULL); |
|
|
/* Job control signals to relay from parent to child. */ |
/* Job control signals to relay from parent to child. */ |
|
sigfillset(&sa.sa_mask); |
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; |
sigaction(SIGTSTP, &sa, NULL); | #endif |
| sudo_sigaction(SIGTSTP, &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]); |
} |
} |
|
|
/* Start out in raw mode if we are not part of a pipeline. */ | /* Start out in raw mode unless part of a pipeline or backgrounded. */ |
if (!pipeline) { | if (!pipeline && !ISSET(details->flags, CD_EXEC_BG)) { |
ttymode = TERM_RAW; |
ttymode = TERM_RAW; |
do { |
do { |
n = term_raw(io_fds[SFD_USERTTY], 0); |
n = term_raw(io_fds[SFD_USERTTY], 0); |
} while (!n && errno == EINTR); |
} while (!n && errno == EINTR); |
if (!n) |
if (!n) |
error(1, _("unable to set terminal to raw mode")); | fatal(U_("unable to set terminal to raw mode")); |
} |
} |
} |
} |
|
|
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) |
| fatalx(U_("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")); | fatal(U_("unable to fork")); |
break; |
break; |
case 0: |
case 0: |
/* child */ |
/* child */ |
Line 578 fork_pty(struct command_details *details, int sv[], in
|
Line 759 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); |
/* 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]); |
if (io_pipe[STDOUT_FILENO][0]) | if (io_pipe[STDOUT_FILENO][0]) |
close(io_pipe[STDOUT_FILENO][0]); | close(io_pipe[STDOUT_FILENO][0]); |
if (io_pipe[STDERR_FILENO][0]) | if (io_pipe[STDERR_FILENO][0]) |
close(io_pipe[STDERR_FILENO][0]); | close(io_pipe[STDERR_FILENO][0]); |
exec_monitor(details, sv[1]); | exec_monitor(details, sv[1]); |
} | |
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 599 fork_pty(struct command_details *details, int sv[], in
|
Line 779 fork_pty(struct command_details *details, int sv[], in
|
close(io_pipe[STDIN_FILENO][0]); |
close(io_pipe[STDIN_FILENO][0]); |
if (io_pipe[STDOUT_FILENO][1]) |
if (io_pipe[STDOUT_FILENO][1]) |
close(io_pipe[STDOUT_FILENO][1]); |
close(io_pipe[STDOUT_FILENO][1]); |
if (io_pipe[STDERR_FILENO][1]) | if (io_pipe[STDERR_FILENO][1]) |
close(io_pipe[STDERR_FILENO][1]); |
close(io_pipe[STDERR_FILENO][1]); |
|
|
for (iob = iobufs; iob; iob = iob->next) { | debug_return_int(child); |
/* Determine maxfd */ | |
if (iob->rfd > *maxfd) | |
*maxfd = iob->rfd; | |
if (iob->wfd > *maxfd) | |
*maxfd = iob->wfd; | |
| |
/* Set non-blocking mode. */ | |
n = fcntl(iob->rfd, F_GETFL, 0); | |
if (n != -1 && !ISSET(n, O_NONBLOCK)) | |
(void) fcntl(iob->rfd, F_SETFL, n | O_NONBLOCK); | |
n = fcntl(iob->wfd, F_GETFL, 0); | |
if (n != -1 && !ISSET(n, O_NONBLOCK)) | |
(void) fcntl(iob->wfd, F_SETFL, n | O_NONBLOCK); | |
} | |
| |
return child; | |
} |
} |
|
|
void |
void |
pty_close(struct command_status *cstat) |
pty_close(struct command_status *cstat) |
{ |
{ |
|
struct io_buffer *iob; |
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 634 pty_close(struct command_status *cstat)
|
Line 800 pty_close(struct command_status *cstat)
|
(void) fcntl(io_fds[SFD_USERTTY], F_SETFL, n); |
(void) fcntl(io_fds[SFD_USERTTY], F_SETFL, n); |
} |
} |
} |
} |
flush_output(); | del_io_events(); |
|
|
if (io_fds[SFD_USERTTY] != -1) { | /* Free I/O buffers. */ |
do { | while ((iob = SLIST_FIRST(&iobufs)) != NULL) { |
n = term_restore(io_fds[SFD_USERTTY], 0); | SLIST_REMOVE_HEAD(&iobufs, entries); |
} while (!n && errno == EINTR); | efree(iob); |
} |
} |
|
|
|
/* Restore terminal settings. */ |
|
if (io_fds[SFD_USERTTY] != -1) |
|
term_restore(io_fds[SFD_USERTTY], 0); |
|
|
/* If child was signalled, write the reason to stdout like the shell. */ |
/* If child was signalled, write the reason to stdout like the shell. */ |
if (cstat->type == CMD_WSTATUS && WIFSIGNALED(cstat->val)) { |
if (cstat->type == CMD_WSTATUS && WIFSIGNALED(cstat->val)) { |
int signo = WTERMSIG(cstat->val); |
int signo = WTERMSIG(cstat->val); |
Line 651 pty_close(struct command_status *cstat)
|
Line 821 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; |
} |
} |
|
|
/* |
/* |
* Fill in fdsr and fdsw based on the io buffers list. | * Schedule I/O events before starting the main event loop or |
* Called prior to select(). | * resuming from suspend. |
*/ |
*/ |
void |
void |
fd_set_iobs(fd_set *fdsr, fd_set *fdsw) | add_io_events(struct sudo_event_base *evbase) |
{ |
{ |
struct io_buffer *iob; |
struct io_buffer *iob; |
|
debug_decl(add_io_events, SUDO_DEBUG_EXEC); |
|
|
for (iob = iobufs; iob; iob = iob->next) { | /* |
if (iob->rfd == -1 && iob->wfd == -1) | * Schedule all readers as long as the buffer is not full. |
continue; | * Schedule writers that contain buffered data. |
if (iob->off == iob->len) { | * Normally, write buffers are added on demand when data is read. |
iob->off = iob->len = 0; | */ |
/* Forward the EOF from reader to writer. */ | SLIST_FOREACH(iob, &iobufs, entries) { |
if (iob->rfd == -1) { | /* Don't read/write from /dev/tty if we are not in the foreground. */ |
safe_close(iob->wfd); | if (iob->revent != NULL && |
iob->wfd = -1; | (ttymode == TERM_RAW || !USERTTY_EVENT(iob->revent))) { |
| if (iob->len != sizeof(iob->buf)) { |
| sudo_debug_printf(SUDO_DEBUG_INFO, |
| "added I/O revent %p, fd %d, events %d", |
| iob->revent, iob->revent->fd, iob->revent->events); |
| if (sudo_ev_add(evbase, iob->revent, NULL, false) == -1) |
| fatal(U_("unable to add event to queue")); |
} |
} |
} |
} |
/* Don't read/write /dev/tty if we are not in the foreground. */ | if (iob->wevent != NULL && |
if (iob->rfd != -1 && | (foreground || !USERTTY_EVENT(iob->wevent))) { |
(ttymode == TERM_RAW || iob->rfd != io_fds[SFD_USERTTY])) { | if (iob->len > iob->off) { |
if (iob->len != sizeof(iob->buf)) | sudo_debug_printf(SUDO_DEBUG_INFO, |
FD_SET(iob->rfd, fdsr); | "added I/O wevent %p, fd %d, events %d", |
| iob->wevent, iob->wevent->fd, iob->wevent->events); |
| if (sudo_ev_add(evbase, iob->wevent, NULL, false) == -1) |
| fatal(U_("unable to add event to queue")); |
| } |
} |
} |
if (iob->wfd != -1 && | } |
(foreground || iob->wfd != io_fds[SFD_USERTTY])) { | debug_return; |
if (iob->len > iob->off) | } |
FD_SET(iob->wfd, fdsw); | |
| /* |
| * Flush any output buffered in iobufs or readable from fds other |
| * than /dev/tty. Removes I/O events from the event base when done. |
| */ |
| static void |
| del_io_events(void) |
| { |
| struct io_buffer *iob; |
| struct sudo_event_base *evbase; |
| debug_decl(del_io_events, SUDO_DEBUG_EXEC); |
| |
| /* Remove iobufs from existing event base. */ |
| SLIST_FOREACH(iob, &iobufs, entries) { |
| if (iob->revent != NULL) { |
| sudo_debug_printf(SUDO_DEBUG_INFO, |
| "deleted I/O revent %p, fd %d, events %d", |
| iob->revent, iob->revent->fd, iob->revent->events); |
| sudo_ev_del(NULL, iob->revent); |
} |
} |
|
if (iob->wevent != NULL) { |
|
sudo_debug_printf(SUDO_DEBUG_INFO, |
|
"deleted I/O wevent %p, fd %d, events %d", |
|
iob->wevent, iob->wevent->fd, iob->wevent->events); |
|
sudo_ev_del(NULL, iob->wevent); |
|
} |
} |
} |
|
|
|
/* Create temporary event base for flushing. */ |
|
evbase = sudo_ev_base_alloc(); |
|
if (evbase == NULL) |
|
fatal(NULL); |
|
|
|
/* Avoid reading from /dev/tty, just flush existing data. */ |
|
SLIST_FOREACH(iob, &iobufs, entries) { |
|
/* Don't read from /dev/tty while flushing. */ |
|
if (iob->revent != NULL && !USERTTY_EVENT(iob->revent)) { |
|
if (iob->len != sizeof(iob->buf)) { |
|
if (sudo_ev_add(evbase, iob->revent, NULL, false) == -1) |
|
fatal(U_("unable to add event to queue")); |
|
} |
|
} |
|
/* Flush any write buffers with data in them. */ |
|
if (iob->wevent != NULL) { |
|
if (iob->len > iob->off) { |
|
if (sudo_ev_add(evbase, iob->wevent, NULL, false) == -1) |
|
fatal(U_("unable to add event to queue")); |
|
} |
|
} |
|
} |
|
|
|
(void) sudo_ev_loop(evbase, SUDO_EVLOOP_NONBLOCK); |
|
|
|
/* Free temporary event base, removing its events. */ |
|
sudo_ev_base_free(evbase); |
|
|
|
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 (signo == SIGCONT_FG) |
|
strlcpy(signame, "CONT_FG", sizeof(signame)); |
|
else if (signo == SIGCONT_BG) |
|
strlcpy(signame, "CONT_BG", sizeof(signame)); |
|
else 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; |
case SIGCONT_BG: |
case SIGCONT_BG: |
/* Continue in background, I take controlling tty. */ |
/* Continue in background, I take controlling tty. */ |
do { |
do { |
status = tcsetpgrp(io_fds[SFD_SLAVE], getpid()); | status = tcsetpgrp(io_fds[SFD_SLAVE], mon_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 969 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 984 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); |
| /* Saved the foreground pgid so we can restore it later. */ |
do { |
do { |
child_pgrp = tcgetpgrp(io_fds[SFD_SLAVE]); | pid = tcgetpgrp(io_fds[SFD_SLAVE]); |
} while (child_pgrp == -1 && errno == EINTR); | } while (pid == -1 && errno == EINTR); |
| if (pid != mon_pgrp) |
| cmnd_pgrp = pid; |
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); |
} |
} |
|
|
|
struct monitor_closure { |
|
struct sudo_event_base *evbase; |
|
struct sudo_event *errpipe_event; |
|
struct sudo_event *backchannel_event; |
|
struct sudo_event *signal_pipe_event; |
|
struct command_status *cstat; |
|
int backchannel; |
|
bool alive; |
|
}; |
|
|
|
static void |
|
mon_signal_pipe_cb(int fd, int what, void *v) |
|
{ |
|
struct monitor_closure *mc = v; |
|
unsigned char signo; |
|
ssize_t n; |
|
debug_decl(mon_signal_pipe_cb, SUDO_DEBUG_EXEC); |
|
|
|
n = read(fd, &signo, sizeof(signo)); |
|
if (n == -1) { |
|
if (errno != EINTR && errno != EAGAIN) { |
|
warning(U_("error reading from signal pipe")); |
|
sudo_ev_loopbreak(mc->evbase); |
|
} |
|
} else { |
|
/* |
|
* Handle SIGCHLD specially and deliver other signals |
|
* directly to the command. |
|
*/ |
|
if (signo == SIGCHLD) { |
|
mc->alive = handle_sigchld(mc->backchannel, mc->cstat); |
|
if (!mc->alive) { |
|
/* Remove all but the errpipe event. */ |
|
sudo_ev_del(mc->evbase, mc->backchannel_event); |
|
sudo_ev_del(mc->evbase, mc->signal_pipe_event); |
|
} |
|
} else { |
|
deliver_signal(cmnd_pid, signo, false); |
|
} |
|
} |
|
debug_return; |
|
} |
|
|
|
static void |
|
mon_errpipe_cb(int fd, int what, void *v) |
|
{ |
|
struct monitor_closure *mc = v; |
|
ssize_t n; |
|
debug_decl(mon_errpipe_cb, SUDO_DEBUG_EXEC); |
|
|
|
/* read errno or EOF from command pipe */ |
|
n = read(fd, mc->cstat, sizeof(struct command_status)); |
|
if (n == -1) { |
|
if (errno != EINTR && errno != EAGAIN) { |
|
warning(U_("error reading from pipe")); |
|
sudo_ev_loopbreak(mc->evbase); |
|
} |
|
} else { |
|
/* Got errno or EOF, either way we are done with errpipe. */ |
|
sudo_ev_del(mc->evbase, mc->errpipe_event); |
|
close(fd); |
|
} |
|
debug_return; |
|
} |
|
|
|
static void |
|
mon_backchannel_cb(int fd, int what, void *v) |
|
{ |
|
struct monitor_closure *mc = v; |
|
struct command_status cstmp; |
|
ssize_t n; |
|
debug_decl(mon_backchannel_cb, SUDO_DEBUG_EXEC); |
|
|
|
/* read command from backchannel, should be a signal */ |
|
n = recv(fd, &cstmp, sizeof(cstmp), MSG_WAITALL); |
|
if (n != sizeof(cstmp)) { |
|
if (n == -1) { |
|
if (errno == EINTR || errno == EAGAIN) |
|
debug_return; |
|
warning(U_("error reading from socketpair")); |
|
} else { |
|
/* short read or EOF, parent process died? */ |
|
} |
|
sudo_ev_loopbreak(mc->evbase); |
|
} else { |
|
if (cstmp.type == CMD_SIGNO) { |
|
deliver_signal(cmnd_pid, cstmp.val, true); |
|
} else { |
|
warningx(U_("unexpected reply type on backchannel: %d"), cstmp.type); |
|
} |
|
} |
|
debug_return; |
|
} |
|
|
/* |
/* |
* Monitor process that creates a new session with the controlling tty, |
* Monitor process that creates a new session with the controlling tty, |
* resets signal handlers and forks a child to call exec_pty(). |
* resets signal handlers and forks a child to call exec_pty(). |
Line 805 static int
|
Line 1160 static int
|
exec_monitor(struct command_details *details, int backchannel) |
exec_monitor(struct command_details *details, int backchannel) |
{ |
{ |
struct command_status cstat; |
struct command_status cstat; |
struct timeval tv; | struct sudo_event_base *evbase; |
fd_set *fdsr; | struct monitor_closure mc; |
sigaction_t sa; |
sigaction_t sa; |
int errpipe[2], maxfd, n, status; | int errpipe[2], n; |
int alive = TRUE; | debug_decl(exec_monitor, SUDO_DEBUG_EXEC); |
unsigned char signo; | |
|
|
/* Close unused fds. */ |
/* Close unused fds. */ |
if (io_fds[SFD_MASTER] != -1) |
if (io_fds[SFD_MASTER] != -1) |
Line 820 exec_monitor(struct command_details *details, int back
|
Line 1174 exec_monitor(struct command_details *details, int back
|
|
|
/* |
/* |
* We use a pipe to atomically handle signal notification within |
* We use a pipe to atomically handle signal notification within |
* the select() loop. | * the event loop. |
*/ |
*/ |
if (pipe_nonblock(signal_pipe) != 0) |
if (pipe_nonblock(signal_pipe) != 0) |
error(1, _("unable to create pipe")); | fatal(U_("unable to create pipe")); |
|
|
/* Reset SIGWINCH and SIGALRM. */ |
/* Reset SIGWINCH and SIGALRM. */ |
zero_bytes(&sa, sizeof(sa)); | memset(&sa, 0, sizeof(sa)); |
sigemptyset(&sa.sa_mask); |
sigemptyset(&sa.sa_mask); |
sa.sa_flags = SA_RESTART; |
sa.sa_flags = SA_RESTART; |
sa.sa_handler = SIG_DFL; |
sa.sa_handler = SIG_DFL; |
sigaction(SIGWINCH, &sa, NULL); | sudo_sigaction(SIGWINCH, &sa, NULL); |
sigaction(SIGALRM, &sa, NULL); | sudo_sigaction(SIGALRM, &sa, NULL); |
|
|
/* Ignore any SIGTTIN or SIGTTOU we get. */ |
/* Ignore any SIGTTIN or SIGTTOU we get. */ |
sa.sa_handler = SIG_IGN; |
sa.sa_handler = SIG_IGN; |
sigaction(SIGTTIN, &sa, NULL); | sudo_sigaction(SIGTTIN, &sa, NULL); |
sigaction(SIGTTOU, &sa, NULL); | sudo_sigaction(SIGTTOU, &sa, NULL); |
|
|
/* Note: HP-UX select() will not be interrupted if SA_RESTART set */ | /* Block all signals in mon_handler(). */ |
| sigfillset(&sa.sa_mask); |
| |
| /* Note: HP-UX poll() will not be interrupted if SA_RESTART is set. */ |
sa.sa_flags = SA_INTERRUPT; |
sa.sa_flags = SA_INTERRUPT; |
sa.sa_handler = handler; | #ifdef SA_SIGINFO |
sigaction(SIGCHLD, &sa, NULL); | sa.sa_flags |= SA_SIGINFO; |
| sa.sa_sigaction = mon_handler; |
| #else |
| sa.sa_handler = mon_handler; |
| #endif |
| sudo_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 |
|
sudo_sigaction(SIGHUP, &sa, NULL); |
|
sudo_sigaction(SIGINT, &sa, NULL); |
|
sudo_sigaction(SIGQUIT, &sa, NULL); |
|
sudo_sigaction(SIGTERM, &sa, NULL); |
|
sudo_sigaction(SIGTSTP, &sa, NULL); |
|
sudo_sigaction(SIGUSR1, &sa, NULL); |
|
sudo_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 855 exec_monitor(struct command_details *details, int back
|
Line 1233 exec_monitor(struct command_details *details, int back
|
if (io_fds[SFD_SLAVE] != -1) { |
if (io_fds[SFD_SLAVE] != -1) { |
#ifdef TIOCSCTTY |
#ifdef TIOCSCTTY |
if (ioctl(io_fds[SFD_SLAVE], TIOCSCTTY, NULL) != 0) |
if (ioctl(io_fds[SFD_SLAVE], TIOCSCTTY, NULL) != 0) |
error(1, _("unable to set controlling tty")); | fatal(U_("unable to set controlling tty")); |
#else |
#else |
/* Set controlling tty by reopening slave. */ |
/* Set controlling tty by reopening slave. */ |
if ((n = open(slavename, O_RDWR)) >= 0) |
if ((n = open(slavename, O_RDWR)) >= 0) |
Line 863 exec_monitor(struct command_details *details, int back
|
Line 1241 exec_monitor(struct command_details *details, int back
|
#endif |
#endif |
} |
} |
|
|
|
mon_pgrp = getpgrp(); /* save a copy of our process group */ |
|
|
/* |
/* |
* If stdin/stdout is not a tty, start command in the background |
* If stdin/stdout is not a tty, start command in the background |
* since it might be part of a pipeline that reads from /dev/tty. |
* since it might be part of a pipeline that reads from /dev/tty. |
Line 870 exec_monitor(struct command_details *details, int back
|
Line 1250 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")); | fatal(U_("unable to create pipe")); |
child = fork(); | cmnd_pid = sudo_debug_fork(); |
if (child == -1) { | if (cmnd_pid == -1) { |
warning(_("unable to fork")); | warning(U_("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 1270 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, &cstat, errpipe[1]); |
cstat.type = CMD_ERRNO; | ignore_result(write(errpipe[1], &cstat, sizeof(cstat))); |
cstat.val = errno; | |
if (write(errpipe[1], &cstat, sizeof(cstat)) == -1) | |
/* 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 907 exec_monitor(struct command_details *details, int back
|
Line 1289 exec_monitor(struct command_details *details, int back
|
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]); |
|
|
/* | /* Put command in its own process group. */ |
* Put child in its own process group. If we are starting the command | cmnd_pgrp = cmnd_pid; |
* in the foreground, assign its pgrp to the tty. | setpgid(cmnd_pid, cmnd_pgrp); |
*/ | |
child_pgrp = child; | /* Make the command the foreground process for the pty slave. */ |
setpgid(child, child_pgrp); | if (foreground && !ISSET(details->flags, CD_EXEC_BG)) { |
if (foreground) { | |
do { |
do { |
status = tcsetpgrp(io_fds[SFD_SLAVE], child_pgrp); | n = tcsetpgrp(io_fds[SFD_SLAVE], cmnd_pgrp); |
} while (status == -1 && errno == EINTR); | } while (n == -1 && errno == EINTR); |
} |
} |
|
|
/* Wait for errno on pipe, signal on backchannel or for SIGCHLD */ | /* |
maxfd = MAX(MAX(errpipe[0], signal_pipe[0]), backchannel); | * Create new event base and register read events for the |
fdsr = (fd_set *)emalloc2(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask)); | * signal pipe, error pipe, and backchannel. |
zero_bytes(fdsr, howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask)); | */ |
zero_bytes(&cstat, sizeof(cstat)); | evbase = sudo_ev_base_alloc(); |
tv.tv_sec = 0; | if (evbase == NULL) |
tv.tv_usec = 0; | fatal(NULL); |
for (;;) { | |
/* Check for signal on backchannel or errno on errpipe. */ | |
FD_SET(backchannel, fdsr); | |
FD_SET(signal_pipe[0], fdsr); | |
if (errpipe[0] != -1) | |
FD_SET(errpipe[0], fdsr); | |
maxfd = MAX(MAX(errpipe[0], signal_pipe[0]), backchannel); | |
|
|
/* If command exited we just poll, there may be data on errpipe. */ | memset(&cstat, 0, sizeof(cstat)); |
n = select(maxfd + 1, fdsr, NULL, NULL, alive ? NULL : &tv); | mc.cstat = &cstat; |
if (n <= 0) { | mc.evbase = evbase; |
if (n == 0) | mc.backchannel = backchannel; |
goto done; | mc.alive = true; |
if (errno == EINTR) | |
continue; | |
error(1, _("select failed")); | |
} | |
|
|
if (FD_ISSET(signal_pipe[0], fdsr)) { | mc.signal_pipe_event = sudo_ev_alloc(signal_pipe[0], |
n = read(signal_pipe[0], &signo, sizeof(signo)); | SUDO_EV_READ|SUDO_EV_PERSIST, mon_signal_pipe_cb, &mc); |
if (n == -1) { | if (mc.signal_pipe_event == NULL) |
if (errno == EINTR || errno == EAGAIN) | fatal(NULL); |
continue; | if (sudo_ev_add(evbase, mc.signal_pipe_event, NULL, false) == -1) |
warning(_("error reading from signal pipe")); | fatal(U_("unable to add event to queue")); |
goto done; | |
} | |
/* | |
* Handle SIGCHLD specially and deliver other signals | |
* directly to the child. | |
*/ | |
if (signo == SIGCHLD) | |
alive = handle_sigchld(backchannel, &cstat); | |
else | |
deliver_signal(child, signo); | |
continue; | |
} | |
if (errpipe[0] != -1 && FD_ISSET(errpipe[0], fdsr)) { | |
/* read errno or EOF from command pipe */ | |
n = read(errpipe[0], &cstat, sizeof(cstat)); | |
if (n == -1) { | |
if (errno == EINTR) | |
continue; | |
warning(_("error reading from pipe")); | |
goto done; | |
} | |
/* Got errno or EOF, either way we are done with errpipe. */ | |
FD_CLR(errpipe[0], fdsr); | |
close(errpipe[0]); | |
errpipe[0] = -1; | |
} | |
if (FD_ISSET(backchannel, fdsr)) { | |
struct command_status cstmp; | |
|
|
/* read command from backchannel, should be a signal */ | mc.errpipe_event = sudo_ev_alloc(errpipe[0], |
n = recv(backchannel, &cstmp, sizeof(cstmp), 0); | SUDO_EV_READ|SUDO_EV_PERSIST, mon_errpipe_cb, &mc); |
if (n == -1) { | if (mc.errpipe_event == NULL) |
if (errno == EINTR) | fatal(NULL); |
continue; | if (sudo_ev_add(evbase, mc.errpipe_event, NULL, false) == -1) |
warning(_("error reading from socketpair")); | fatal(U_("unable to add event to queue")); |
goto done; | |
} | |
if (cstmp.type != CMD_SIGNO) { | |
warningx(_("unexpected reply type on backchannel: %d"), | |
cstmp.type); | |
continue; | |
} | |
deliver_signal(child, cstmp.val); | |
} | |
} | |
|
|
done: | mc.backchannel_event = sudo_ev_alloc(backchannel, |
if (alive) { | SUDO_EV_READ|SUDO_EV_PERSIST, mon_backchannel_cb, &mc); |
/* XXX An error occurred, should send an error back. */ | if (mc.backchannel_event == NULL) |
kill(child, SIGKILL); | fatal(NULL); |
| if (sudo_ev_add(evbase, mc.backchannel_event, NULL, false) == -1) |
| fatal(U_("unable to add event to queue")); |
| |
| /* |
| * Wait for errno on pipe, signal on backchannel or for SIGCHLD. |
| * The event loop ends when the child is no longer running and |
| * the error pipe is closed. |
| */ |
| (void) sudo_ev_loop(evbase, 0); |
| if (mc.alive) { |
| /* XXX An error occurred, should send a message back. */ |
| sudo_debug_printf(SUDO_DEBUG_ERROR, |
| "Command still running after event loop exit, sending 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); |
} |
} |
|
|
/* |
/* |
* Flush any output buffered in iobufs or readable from the fds. |
|
* Does not read from /dev/tty. |
|
*/ |
|
static void |
|
flush_output(void) |
|
{ |
|
struct io_buffer *iob; |
|
struct timeval tv; |
|
fd_set *fdsr, *fdsw; |
|
int nready, nwriters, maxfd = -1; |
|
|
|
/* Determine maxfd */ |
|
for (iob = iobufs; iob; iob = iob->next) { |
|
if (iob->rfd > maxfd) |
|
maxfd = iob->rfd; |
|
if (iob->wfd > maxfd) |
|
maxfd = iob->wfd; |
|
} |
|
if (maxfd == -1) |
|
return; |
|
|
|
fdsr = (fd_set *)emalloc2(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask)); |
|
fdsw = (fd_set *)emalloc2(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask)); |
|
for (;;) { |
|
zero_bytes(fdsw, howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask)); |
|
zero_bytes(fdsr, howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask)); |
|
|
|
nwriters = 0; |
|
for (iob = iobufs; iob; iob = iob->next) { |
|
/* Don't read from /dev/tty while flushing. */ |
|
if (io_fds[SFD_USERTTY] != -1 && iob->rfd == io_fds[SFD_USERTTY]) |
|
continue; |
|
if (iob->rfd == -1 && iob->wfd == -1) |
|
continue; |
|
if (iob->off == iob->len) { |
|
iob->off = iob->len = 0; |
|
/* Forward the EOF from reader to writer. */ |
|
if (iob->rfd == -1) { |
|
safe_close(iob->wfd); |
|
iob->wfd = -1; |
|
} |
|
} |
|
if (iob->rfd != -1) { |
|
if (iob->len != sizeof(iob->buf)) |
|
FD_SET(iob->rfd, fdsr); |
|
} |
|
if (iob->wfd != -1) { |
|
if (iob->len > iob->off) { |
|
nwriters++; |
|
FD_SET(iob->wfd, fdsw); |
|
} |
|
} |
|
} |
|
|
|
/* Don't sleep in select if there are no buffers that need writing. */ |
|
tv.tv_sec = 0; |
|
tv.tv_usec = 0; |
|
nready = select(maxfd + 1, fdsr, fdsw, NULL, nwriters ? NULL : &tv); |
|
if (nready <= 0) { |
|
if (nready == 0) |
|
break; /* all I/O flushed */ |
|
if (errno == EINTR) |
|
continue; |
|
error(1, _("select failed")); |
|
} |
|
if (perform_io(fdsr, fdsw, NULL) != 0) |
|
break; |
|
} |
|
efree(fdsr); |
|
efree(fdsw); |
|
} |
|
|
|
/* |
|
* Sets up std{in,out,err} and executes the actual command. |
* Sets up std{in,out,err} and executes the actual command. |
* 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, |
| struct command_status *cstat, 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. */ | /* Register cleanup function */ |
| fatal_callback_register(pty_cleanup); |
| |
| /* 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. */ |
if (dup2(io_fds[SFD_STDIN], STDIN_FILENO) == -1 || |
if (dup2(io_fds[SFD_STDIN], STDIN_FILENO) == -1 || |
dup2(io_fds[SFD_STDOUT], STDOUT_FILENO) == -1 || |
dup2(io_fds[SFD_STDOUT], STDOUT_FILENO) == -1 || |
dup2(io_fds[SFD_STDERR], STDERR_FILENO) == -1) |
dup2(io_fds[SFD_STDERR], STDERR_FILENO) == -1) |
error(1, "dup2"); | fatal("dup2"); |
|
|
/* 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) { | if (foreground && !ISSET(details->flags, CD_EXEC_BG)) { |
while (tcgetpgrp(io_fds[SFD_SLAVE]) != self) |
while (tcgetpgrp(io_fds[SFD_SLAVE]) != self) |
; /* spin */ |
; /* spin */ |
} |
} |
Line 1117 exec_pty(struct command_details *details)
|
Line 1396 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) | /* Execute command; only returns on error. */ |
closefrom(details->closefrom); | exec_cmnd(details, cstat, errfd); |
#ifdef HAVE_SELINUX | |
if (ISSET(details->flags, CD_RBAC_ENABLED)) | debug_return; |
selinux_execve(details->command, details->argv, details->envp); | |
else | |
#endif | |
my_execve(details->command, details->argv, details->envp); | |
} |
} |
|
|
/* |
/* |
Line 1136 sync_ttysize(int src, int dst)
|
Line 1411 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 1158 sigwinch(int s)
|
Line 1436 sigwinch(int s)
|
} |
} |
|
|
/* |
/* |
|
* Remove and free any events associated with the specified |
|
* file descriptor present in the I/O buffers list. |
|
*/ |
|
static void |
|
ev_free_by_fd(struct sudo_event_base *evbase, int fd) |
|
{ |
|
struct io_buffer *iob; |
|
debug_decl(ev_free_by_fd, SUDO_DEBUG_EXEC); |
|
|
|
/* Deschedule any users of the fd and free up the events. */ |
|
SLIST_FOREACH(iob, &iobufs, entries) { |
|
if (iob->revent != NULL) { |
|
if (sudo_ev_get_fd(iob->revent) == fd) { |
|
sudo_debug_printf(SUDO_DEBUG_INFO, |
|
"%s: deleting and freeing revent %p with fd %d", |
|
__func__, iob->revent, fd); |
|
sudo_ev_del(evbase, iob->revent); |
|
sudo_ev_free(iob->revent); |
|
iob->revent = NULL; |
|
} |
|
} |
|
if (iob->wevent != NULL) { |
|
if (sudo_ev_get_fd(iob->wevent) == fd) { |
|
sudo_debug_printf(SUDO_DEBUG_INFO, |
|
"%s: deleting and freeing wevent %p with fd %d", |
|
__func__, iob->wevent, fd); |
|
sudo_ev_del(evbase, iob->wevent); |
|
sudo_ev_free(iob->wevent); |
|
iob->wevent = NULL; |
|
} |
|
} |
|
} |
|
debug_return; |
|
} |
|
|
|
/* |
* Only close the fd if it is not /dev/tty or std{in,out,err}. |
* Only close the fd if it is not /dev/tty or std{in,out,err}. |
* Return value is the same as send(2). | * Return value is the same as close(2). |
*/ |
*/ |
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]) { |
|
sudo_debug_printf(SUDO_DEBUG_INFO, |
|
"%s: not closing fd %d (/dev/tty)", __func__, fd); |
errno = EINVAL; |
errno = EINVAL; |
return -1; | debug_return_int(-1); |
} |
} |
return close(fd); | sudo_debug_printf(SUDO_DEBUG_INFO, "%s: closing fd %d", __func__, fd); |
| debug_return_int(close(fd)); |
} |
} |