2009-08-06 15:51:12 +00:00
|
|
|
/*
|
2012-01-06 14:23:55 -05:00
|
|
|
* Copyright (c) 2009-2012 Todd C. Miller <Todd.Miller@courtesan.com>
|
2009-08-06 15:51:12 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2011-02-19 08:23:46 -05:00
|
|
|
#ifdef HAVE_SYS_SYSMACROS_H
|
|
|
|
# include <sys/sysmacros.h>
|
|
|
|
#endif
|
2009-11-15 21:42:17 +00:00
|
|
|
#include <sys/socket.h>
|
2009-08-06 15:51:12 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#ifdef HAVE_SYS_SELECT_H
|
2010-02-20 09:41:49 -05:00
|
|
|
# include <sys/select.h>
|
2009-08-06 15:51:12 +00:00
|
|
|
#endif /* HAVE_SYS_SELECT_H */
|
|
|
|
#include <stdio.h>
|
|
|
|
#ifdef STDC_HEADERS
|
|
|
|
# include <stdlib.h>
|
|
|
|
# include <stddef.h>
|
|
|
|
#else
|
|
|
|
# ifdef HAVE_STDLIB_H
|
|
|
|
# include <stdlib.h>
|
|
|
|
# endif
|
|
|
|
#endif /* STDC_HEADERS */
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif /* HAVE_STRING_H */
|
2010-06-29 13:08:05 -04:00
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif /* HAVE_STRINGS_H */
|
2009-08-06 15:51:12 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif /* HAVE_UNISTD_H */
|
|
|
|
#if TIME_WITH_SYS_TIME
|
|
|
|
# include <time.h>
|
|
|
|
#endif
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
2010-06-09 11:09:55 -04:00
|
|
|
#include <termios.h>
|
2009-08-06 15:51:12 +00:00
|
|
|
|
2010-06-09 13:57:07 -04:00
|
|
|
#include "sudo.h"
|
2010-06-16 16:46:56 -04:00
|
|
|
#include "sudo_exec.h"
|
2010-02-20 09:41:49 -05:00
|
|
|
#include "sudo_plugin.h"
|
|
|
|
#include "sudo_plugin_int.h"
|
2009-08-06 15:51:12 +00:00
|
|
|
|
2010-09-10 11:20:32 -04:00
|
|
|
/* Shared with exec_pty.c for use with handler(). */
|
|
|
|
int signal_pipe[2];
|
|
|
|
|
|
|
|
/* We keep a tailq of signals to forward to child. */
|
|
|
|
struct sigforward {
|
|
|
|
struct sigforward *prev, *next;
|
|
|
|
int signo;
|
|
|
|
};
|
|
|
|
TQ_DECLARE(sigforward)
|
|
|
|
static struct sigforward_list sigfwd_list;
|
2012-08-27 10:29:59 -04:00
|
|
|
static pid_t ppgrp = -1;
|
2010-09-10 11:20:32 -04:00
|
|
|
|
2012-08-06 14:38:35 -04:00
|
|
|
volatile pid_t cmnd_pid = -1;
|
|
|
|
|
2013-01-17 09:17:54 -05:00
|
|
|
static int dispatch_signals(int sv[2], pid_t child, int log_io,
|
2010-09-10 11:20:32 -04:00
|
|
|
struct command_status *cstat);
|
|
|
|
static void forward_signals(int fd);
|
|
|
|
static void schedule_signal(int signo);
|
2012-01-17 10:27:33 -05:00
|
|
|
#ifdef SA_SIGINFO
|
2012-08-06 14:38:35 -04:00
|
|
|
static void handler_user_only(int s, siginfo_t *info, void *context);
|
2012-01-17 10:27:33 -05:00
|
|
|
#endif
|
2009-11-15 21:42:17 +00:00
|
|
|
|
2010-05-07 10:15:25 -04:00
|
|
|
/*
|
2010-06-07 18:06:22 -04:00
|
|
|
* Fork and execute a command, returns the child's pid.
|
|
|
|
* Sends errno back on sv[1] if execve() fails.
|
2010-05-07 10:15:25 -04:00
|
|
|
*/
|
2011-03-10 14:24:10 -05:00
|
|
|
static int fork_cmnd(struct command_details *details, int sv[2])
|
2010-05-07 10:15:25 -04:00
|
|
|
{
|
2010-06-07 18:06:22 -04:00
|
|
|
struct command_status cstat;
|
2010-08-24 08:42:47 -04:00
|
|
|
sigaction_t sa;
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_decl(fork_cmnd, SUDO_DEBUG_EXEC)
|
2010-05-07 10:15:25 -04:00
|
|
|
|
2012-08-27 10:29:59 -04:00
|
|
|
ppgrp = getpgrp(); /* parent's process group */
|
|
|
|
|
|
|
|
/*
|
2012-08-27 11:22:33 -04:00
|
|
|
* Handle suspend/restore of sudo and the command.
|
|
|
|
* In most cases, the command will be in the same process group as
|
|
|
|
* sudo and job control will "just work". However, if the command
|
|
|
|
* changes its process group ID and does not change it back (or is
|
|
|
|
* kill by SIGSTOP which is not catchable), we need to resume the
|
|
|
|
* command manually. Also, if SIGTSTP is sent directly to sudo,
|
|
|
|
* we need to suspend the command, and then suspend ourself, restoring
|
|
|
|
* the default SIGTSTP handler temporarily.
|
|
|
|
*
|
|
|
|
* XXX - currently we send SIGCONT upon resume in some cases where
|
|
|
|
* we don't need to (e.g. command pgrp == parent pgrp).
|
2012-08-27 10:29:59 -04:00
|
|
|
*/
|
2010-08-24 08:42:47 -04:00
|
|
|
zero_bytes(&sa, sizeof(sa));
|
2013-01-17 09:17:54 -05:00
|
|
|
sigfillset(&sa.sa_mask);
|
2010-08-24 08:42:47 -04:00
|
|
|
sa.sa_flags = SA_INTERRUPT; /* do not restart syscalls */
|
2012-08-06 14:38:35 -04:00
|
|
|
#ifdef SA_SIGINFO
|
|
|
|
sa.sa_flags |= SA_SIGINFO;
|
|
|
|
sa.sa_sigaction = handler;
|
|
|
|
#else
|
2010-08-24 08:42:47 -04:00
|
|
|
sa.sa_handler = handler;
|
2012-08-06 14:38:35 -04:00
|
|
|
#endif
|
2010-08-24 08:42:47 -04:00
|
|
|
sigaction(SIGCONT, &sa, NULL);
|
2012-08-27 11:22:33 -04:00
|
|
|
#ifdef SA_SIGINFO
|
|
|
|
sa.sa_sigaction = handler_user_only;
|
|
|
|
#endif
|
|
|
|
sigaction(SIGTSTP, &sa, NULL);
|
2010-08-24 08:42:47 -04:00
|
|
|
|
2012-04-23 16:38:16 -04:00
|
|
|
/*
|
|
|
|
* 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"));
|
|
|
|
|
2012-08-06 14:38:35 -04:00
|
|
|
cmnd_pid = sudo_debug_fork();
|
|
|
|
switch (cmnd_pid) {
|
2010-06-07 18:06:22 -04:00
|
|
|
case -1:
|
2011-05-18 13:04:24 -04:00
|
|
|
error(1, _("unable to fork"));
|
2010-06-07 18:06:22 -04:00
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
/* child */
|
|
|
|
close(sv[0]);
|
2010-09-10 11:20:32 -04:00
|
|
|
close(signal_pipe[0]);
|
|
|
|
close(signal_pipe[1]);
|
2010-06-07 18:06:22 -04:00
|
|
|
fcntl(sv[1], F_SETFD, FD_CLOEXEC);
|
2011-02-02 12:44:35 -05:00
|
|
|
restore_signals();
|
2011-12-02 11:27:33 -05:00
|
|
|
if (exec_setup(details, NULL, -1) == true) {
|
2010-06-07 18:06:22 -04:00
|
|
|
/* headed for execve() */
|
2011-10-22 14:40:21 -04:00
|
|
|
sudo_debug_execve(SUDO_DEBUG_INFO, details->command,
|
|
|
|
details->argv, details->envp);
|
2011-11-29 19:51:24 -05:00
|
|
|
if (details->closefrom >= 0) {
|
|
|
|
int maxfd = details->closefrom;
|
|
|
|
dup2(sv[1], maxfd);
|
|
|
|
(void)fcntl(maxfd, F_SETFD, FD_CLOEXEC);
|
|
|
|
sv[1] = maxfd++;
|
|
|
|
if (sudo_debug_fd_set(maxfd) != -1)
|
|
|
|
maxfd++;
|
|
|
|
closefrom(maxfd);
|
|
|
|
}
|
2010-06-07 18:06:22 -04:00
|
|
|
#ifdef HAVE_SELINUX
|
2012-01-25 14:58:02 -05:00
|
|
|
if (ISSET(details->flags, CD_RBAC_ENABLED)) {
|
|
|
|
selinux_execve(details->command, details->argv, details->envp,
|
|
|
|
ISSET(details->flags, CD_NOEXEC));
|
|
|
|
} else
|
2010-06-07 18:06:22 -04:00
|
|
|
#endif
|
2012-01-25 14:58:02 -05:00
|
|
|
{
|
|
|
|
sudo_execve(details->command, details->argv, details->envp,
|
|
|
|
ISSET(details->flags, CD_NOEXEC));
|
|
|
|
}
|
2011-11-07 16:33:49 -05:00
|
|
|
sudo_debug_printf(SUDO_DEBUG_ERROR, "unable to exec %s: %s",
|
2011-10-22 14:40:21 -04:00
|
|
|
details->command, strerror(errno));
|
2010-05-07 10:15:25 -04:00
|
|
|
}
|
2010-06-07 18:06:22 -04:00
|
|
|
cstat.type = CMD_ERRNO;
|
|
|
|
cstat.val = errno;
|
|
|
|
send(sv[1], &cstat, sizeof(cstat), 0);
|
2011-10-22 14:40:21 -04:00
|
|
|
sudo_debug_exit_int(__func__, __FILE__, __LINE__, sudo_debug_subsys, 1);
|
2010-06-07 18:06:22 -04:00
|
|
|
_exit(1);
|
2010-05-07 10:15:25 -04:00
|
|
|
}
|
2012-08-06 14:38:35 -04:00
|
|
|
sudo_debug_printf(SUDO_DEBUG_INFO, "executed %s, pid %d", details->command,
|
2012-08-29 14:40:25 -04:00
|
|
|
(int)cmnd_pid);
|
2012-08-06 14:38:35 -04:00
|
|
|
debug_return_int(cmnd_pid);
|
2010-05-07 10:15:25 -04:00
|
|
|
}
|
|
|
|
|
2011-02-02 12:44:35 -05:00
|
|
|
static struct signal_state {
|
|
|
|
int signo;
|
|
|
|
sigaction_t sa;
|
|
|
|
} saved_signals[] = {
|
|
|
|
{ SIGALRM },
|
|
|
|
{ SIGCHLD },
|
|
|
|
{ SIGCONT },
|
|
|
|
{ SIGHUP },
|
|
|
|
{ SIGINT },
|
|
|
|
{ SIGPIPE },
|
|
|
|
{ SIGQUIT },
|
|
|
|
{ SIGTERM },
|
|
|
|
{ SIGTSTP },
|
|
|
|
{ SIGTTIN },
|
|
|
|
{ SIGTTOU },
|
|
|
|
{ SIGUSR1 },
|
|
|
|
{ SIGUSR2 },
|
|
|
|
{ -1 }
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Save signal handler state so it can be restored before exec.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
save_signals(void)
|
|
|
|
{
|
|
|
|
struct signal_state *ss;
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_decl(save_signals, SUDO_DEBUG_EXEC)
|
2011-02-02 12:44:35 -05:00
|
|
|
|
|
|
|
for (ss = saved_signals; ss->signo != -1; ss++)
|
|
|
|
sigaction(ss->signo, NULL, &ss->sa);
|
2011-10-22 14:40:21 -04:00
|
|
|
|
|
|
|
debug_return;
|
2011-02-02 12:44:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Restore signal handlers to initial state.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
restore_signals(void)
|
|
|
|
{
|
|
|
|
struct signal_state *ss;
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_decl(restore_signals, SUDO_DEBUG_EXEC)
|
2011-02-02 12:44:35 -05:00
|
|
|
|
|
|
|
for (ss = saved_signals; ss->signo != -1; ss++)
|
|
|
|
sigaction(ss->signo, &ss->sa, NULL);
|
2011-10-22 14:40:21 -04:00
|
|
|
|
|
|
|
debug_return;
|
2011-02-02 12:44:35 -05:00
|
|
|
}
|
|
|
|
|
2009-10-11 12:27:11 +00:00
|
|
|
/*
|
2010-06-07 18:06:22 -04:00
|
|
|
* Execute a command, potentially in a pty with I/O loggging.
|
2009-10-11 12:27:11 +00:00
|
|
|
* This is a little bit tricky due to how POSIX job control works and
|
|
|
|
* we fact that we have two different controlling terminals to deal with.
|
|
|
|
*/
|
2009-08-06 15:51:12 +00:00
|
|
|
int
|
2012-01-25 14:58:02 -05:00
|
|
|
sudo_execute(struct command_details *details, struct command_status *cstat)
|
2009-10-11 12:27:11 +00:00
|
|
|
{
|
2011-12-02 11:27:33 -05:00
|
|
|
int maxfd, n, nready, sv[2];
|
2011-03-15 15:53:49 -04:00
|
|
|
const char *utmp_user = NULL;
|
2011-12-02 11:27:33 -05:00
|
|
|
bool log_io = false;
|
2009-10-22 00:58:41 +00:00
|
|
|
fd_set *fdsr, *fdsw;
|
2010-09-10 11:20:32 -04:00
|
|
|
sigaction_t sa;
|
2012-08-06 14:38:35 -04:00
|
|
|
sigset_t omask;
|
2010-06-07 18:06:22 -04:00
|
|
|
pid_t child;
|
2012-01-25 14:58:02 -05:00
|
|
|
debug_decl(sudo_execute, SUDO_DEBUG_EXEC)
|
2010-02-20 09:41:49 -05:00
|
|
|
|
2010-06-09 16:19:45 -04:00
|
|
|
/* If running in background mode, fork and exit. */
|
|
|
|
if (ISSET(details->flags, CD_BACKGROUND)) {
|
2012-04-06 15:20:16 -04:00
|
|
|
switch (sudo_debug_fork()) {
|
2010-06-09 16:19:45 -04:00
|
|
|
case -1:
|
|
|
|
cstat->type = CMD_ERRNO;
|
|
|
|
cstat->val = errno;
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_return_int(-1);
|
2010-06-09 16:19:45 -04:00
|
|
|
case 0:
|
2011-05-31 12:49:22 -04:00
|
|
|
/* child continues without controlling terminal */
|
|
|
|
(void)setpgid(0, 0);
|
2010-06-09 16:19:45 -04:00
|
|
|
break;
|
|
|
|
default:
|
2011-05-31 12:49:22 -04:00
|
|
|
/* parent exits (but does not flush buffers) */
|
2011-10-22 14:40:21 -04:00
|
|
|
sudo_debug_exit_int(__func__, __FILE__, __LINE__,
|
|
|
|
sudo_debug_subsys, 0);
|
2011-05-31 12:49:22 -04:00
|
|
|
_exit(0);
|
2010-06-09 16:19:45 -04:00
|
|
|
}
|
|
|
|
}
|
2009-10-11 12:27:11 +00:00
|
|
|
|
2010-12-20 16:27:46 -05:00
|
|
|
/*
|
|
|
|
* If we have an I/O plugin or the policy plugin has requested one, we
|
|
|
|
* need to allocate a pty. It is OK to set log_io in the pty-only case
|
2011-05-31 12:49:22 -04:00
|
|
|
* as the io plugin tailqueue will be empty and no I/O logging will occur.
|
2010-12-20 16:27:46 -05:00
|
|
|
*/
|
|
|
|
if (!tq_empty(&io_plugins) || ISSET(details->flags, CD_USE_PTY)) {
|
2011-12-02 11:27:33 -05:00
|
|
|
log_io = true;
|
2011-05-27 15:20:20 -04:00
|
|
|
if (ISSET(details->flags, CD_SET_UTMP))
|
|
|
|
utmp_user = details->utmp_user ? details->utmp_user : user_details.username;
|
2011-10-22 14:40:21 -04:00
|
|
|
sudo_debug_printf(SUDO_DEBUG_INFO, "allocate pty for I/O logging");
|
2011-05-27 15:20:20 -04:00
|
|
|
pty_setup(details->euid, user_details.tty, utmp_user);
|
2010-05-27 16:29:48 -04:00
|
|
|
}
|
2010-02-27 16:53:56 -05:00
|
|
|
|
|
|
|
/*
|
2010-06-07 18:06:22 -04:00
|
|
|
* We communicate with the child over a bi-directional pair of sockets.
|
2010-02-27 16:53:56 -05:00
|
|
|
* Parent sends signal info to child and child sends back wait status.
|
|
|
|
*/
|
2011-02-19 08:13:17 -05:00
|
|
|
if (socketpair(PF_UNIX, SOCK_DGRAM, 0, sv) == -1)
|
2011-05-18 12:41:06 -04:00
|
|
|
error(1, _("unable to create sockets"));
|
2009-10-31 17:14:52 +00:00
|
|
|
|
2010-09-10 11:20:32 -04:00
|
|
|
/*
|
|
|
|
* We use a pipe to atomically handle signal notification within
|
|
|
|
* the select() loop.
|
|
|
|
*/
|
|
|
|
if (pipe_nonblock(signal_pipe) != 0)
|
2011-05-18 12:41:06 -04:00
|
|
|
error(1, _("unable to create pipe"));
|
2010-09-10 11:20:32 -04:00
|
|
|
|
2011-02-03 10:25:42 -05:00
|
|
|
/*
|
2011-12-18 14:20:56 -05:00
|
|
|
* Signals to forward to the child process (excluding SIGALRM and SIGCHLD).
|
2013-01-17 09:17:54 -05:00
|
|
|
* We block all other signals while running the signal handler.
|
2011-02-03 10:25:42 -05:00
|
|
|
* Note: HP-UX select() will not be interrupted if SA_RESTART set.
|
|
|
|
*/
|
2013-01-17 09:17:54 -05:00
|
|
|
zero_bytes(&sa, sizeof(sa));
|
|
|
|
sigfillset(&sa.sa_mask);
|
2010-06-10 15:18:23 -04:00
|
|
|
sa.sa_flags = SA_INTERRUPT; /* do not restart syscalls */
|
2012-08-06 14:38:35 -04:00
|
|
|
#ifdef SA_SIGINFO
|
|
|
|
sa.sa_flags |= SA_SIGINFO;
|
|
|
|
sa.sa_sigaction = handler;
|
|
|
|
#else
|
2010-04-08 06:12:47 -04:00
|
|
|
sa.sa_handler = handler;
|
2012-08-06 14:38:35 -04:00
|
|
|
#endif
|
2010-04-08 20:17:26 -04:00
|
|
|
sigaction(SIGALRM, &sa, NULL);
|
2010-03-31 12:43:26 -04:00
|
|
|
sigaction(SIGCHLD, &sa, NULL);
|
2010-05-11 08:59:59 -04:00
|
|
|
sigaction(SIGPIPE, &sa, NULL);
|
2010-04-08 20:17:26 -04:00
|
|
|
sigaction(SIGTERM, &sa, NULL);
|
2011-02-03 10:25:42 -05:00
|
|
|
sigaction(SIGUSR1, &sa, NULL);
|
|
|
|
sigaction(SIGUSR2, &sa, NULL);
|
2010-03-31 12:43:26 -04:00
|
|
|
|
2012-01-17 10:27:33 -05:00
|
|
|
/*
|
|
|
|
* When not running the command in a pty, we do not want to
|
|
|
|
* forward signals generated by the kernel that the child will
|
|
|
|
* already have received either by virtue of being in the
|
|
|
|
* controlling tty's process group (SIGINT, SIGQUIT) or because
|
|
|
|
* the session is terminating (SIGHUP).
|
|
|
|
*/
|
|
|
|
#ifdef SA_SIGINFO
|
|
|
|
if (!log_io) {
|
|
|
|
sa.sa_flags |= SA_SIGINFO;
|
2012-08-06 14:38:35 -04:00
|
|
|
sa.sa_sigaction = handler_user_only;
|
2012-01-17 10:27:33 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
sigaction(SIGHUP, &sa, NULL);
|
|
|
|
sigaction(SIGINT, &sa, NULL);
|
|
|
|
sigaction(SIGQUIT, &sa, NULL);
|
|
|
|
|
2010-06-07 18:06:22 -04:00
|
|
|
/* Max fd we will be selecting on. */
|
2010-09-10 11:20:32 -04:00
|
|
|
maxfd = MAX(sv[0], signal_pipe[0]);
|
2009-10-25 14:55:14 +00:00
|
|
|
|
2009-10-11 12:27:11 +00:00
|
|
|
/*
|
2009-10-22 00:58:41 +00:00
|
|
|
* Child will run the command in the pty, parent will pass data
|
2010-06-07 18:06:22 -04:00
|
|
|
* to and from pty. Adjusts maxfd as needed.
|
2009-10-11 12:27:11 +00:00
|
|
|
*/
|
2010-06-07 18:06:22 -04:00
|
|
|
if (log_io)
|
2012-08-06 14:38:35 -04:00
|
|
|
child = fork_pty(details, sv, &maxfd, &omask);
|
2010-06-07 18:06:22 -04:00
|
|
|
else
|
2011-03-10 14:24:10 -05:00
|
|
|
child = fork_cmnd(details, sv);
|
2009-11-15 21:42:17 +00:00
|
|
|
close(sv[1]);
|
2009-10-31 16:48:34 +00:00
|
|
|
|
2010-04-29 16:47:27 -04:00
|
|
|
/* Set command timeout if specified. */
|
|
|
|
if (ISSET(details->flags, CD_SET_TIMEOUT))
|
|
|
|
alarm(details->timeout);
|
|
|
|
|
2010-07-27 09:45:20 -04:00
|
|
|
/*
|
|
|
|
* I/O logging must be in the C locale for floating point numbers
|
|
|
|
* to be logged consistently.
|
|
|
|
*/
|
|
|
|
setlocale(LC_ALL, "C");
|
|
|
|
|
2009-08-06 15:51:12 +00:00
|
|
|
/*
|
2009-10-18 14:46:01 +00:00
|
|
|
* In the event loop we pass input from user tty to master
|
2010-02-20 09:41:49 -05:00
|
|
|
* and pass output from master to stdout and IO plugin.
|
2009-08-06 15:51:12 +00:00
|
|
|
*/
|
2012-03-19 11:24:24 -04:00
|
|
|
fdsr = emalloc2(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask));
|
|
|
|
fdsw = emalloc2(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask));
|
2010-02-21 10:26:30 -05:00
|
|
|
for (;;) {
|
2012-03-19 11:24:24 -04:00
|
|
|
memset(fdsw, 0, howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask));
|
|
|
|
memset(fdsr, 0, howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask));
|
2009-11-15 21:42:17 +00:00
|
|
|
|
2010-10-01 12:03:47 -04:00
|
|
|
FD_SET(signal_pipe[0], fdsr);
|
2009-11-15 21:42:17 +00:00
|
|
|
FD_SET(sv[0], fdsr);
|
2010-09-10 11:20:32 -04:00
|
|
|
if (!tq_empty(&sigfwd_list))
|
|
|
|
FD_SET(sv[0], fdsw);
|
2010-06-07 18:06:22 -04:00
|
|
|
if (log_io)
|
|
|
|
fd_set_iobs(fdsr, fdsw); /* XXX - better name */
|
2010-02-20 09:41:49 -05:00
|
|
|
nready = select(maxfd + 1, fdsr, fdsw, NULL, NULL);
|
2011-12-08 11:18:38 -05:00
|
|
|
sudo_debug_printf(SUDO_DEBUG_DEBUG, "select returns %d", nready);
|
2009-08-06 15:51:12 +00:00
|
|
|
if (nready == -1) {
|
2012-04-10 10:18:39 -04:00
|
|
|
if (errno == EINTR || errno == ENOMEM)
|
2009-08-06 15:51:12 +00:00
|
|
|
continue;
|
2012-04-10 10:18:39 -04:00
|
|
|
if (errno == EBADF || errno == EIO) {
|
2012-04-05 12:39:46 -04:00
|
|
|
/* One of the ttys must have gone away. */
|
|
|
|
goto do_tty_io;
|
|
|
|
}
|
2012-11-25 09:34:04 -05:00
|
|
|
warning(_("select failed"));
|
2012-04-10 10:18:39 -04:00
|
|
|
sudo_debug_printf(SUDO_DEBUG_ERROR,
|
|
|
|
"select failure, terminating child");
|
|
|
|
schedule_signal(SIGKILL);
|
|
|
|
forward_signals(sv[0]);
|
|
|
|
break;
|
2009-08-06 15:51:12 +00:00
|
|
|
}
|
2010-09-10 11:20:32 -04:00
|
|
|
if (FD_ISSET(sv[0], fdsw)) {
|
|
|
|
forward_signals(sv[0]);
|
|
|
|
}
|
|
|
|
if (FD_ISSET(signal_pipe[0], fdsr)) {
|
2013-01-17 09:17:54 -05:00
|
|
|
n = dispatch_signals(sv, child, log_io, cstat);
|
2010-09-10 11:20:32 -04:00
|
|
|
if (n == 0) {
|
|
|
|
/* Child has exited, cstat is set, we are done. */
|
2011-10-24 11:14:20 -04:00
|
|
|
break;
|
2010-09-10 11:20:32 -04:00
|
|
|
}
|
|
|
|
if (n == -1) {
|
|
|
|
/* Error reading signal_pipe[0], should not happen. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Restart event loop so signals get sent to child immediately. */
|
|
|
|
continue;
|
|
|
|
}
|
2009-11-15 21:42:17 +00:00
|
|
|
if (FD_ISSET(sv[0], fdsr)) {
|
|
|
|
/* read child status */
|
2010-02-20 09:41:49 -05:00
|
|
|
n = recv(sv[0], cstat, sizeof(*cstat), 0);
|
2011-12-08 11:18:38 -05:00
|
|
|
if (n != sizeof(*cstat)) {
|
|
|
|
if (n == -1) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
/*
|
|
|
|
* If not logging I/O we may receive ECONNRESET when
|
|
|
|
* the command is executed and sv is closed.
|
|
|
|
* It is safe to ignore this.
|
|
|
|
*/
|
|
|
|
if (log_io && errno != EAGAIN) {
|
|
|
|
cstat->type = CMD_ERRNO;
|
|
|
|
cstat->val = errno;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sudo_debug_printf(SUDO_DEBUG_ERROR,
|
|
|
|
"failed to read child status: %s", strerror(errno));
|
|
|
|
} else {
|
|
|
|
/* Short read or EOF. */
|
|
|
|
sudo_debug_printf(SUDO_DEBUG_ERROR,
|
|
|
|
"failed to read child status: %s",
|
|
|
|
n ? "short read" : "EOF");
|
|
|
|
/* XXX - should set cstat */
|
2010-06-01 10:58:11 -04:00
|
|
|
break;
|
2010-02-21 10:26:30 -05:00
|
|
|
}
|
2009-11-15 21:42:17 +00:00
|
|
|
}
|
2012-08-06 14:38:35 -04:00
|
|
|
if (cstat->type == CMD_PID) {
|
|
|
|
/*
|
|
|
|
* Once we know the command's pid we can unblock
|
|
|
|
* signals which ere blocked in fork_pty(). This
|
|
|
|
* avoids a race between exec of the command and
|
|
|
|
* receipt of a fatal signal from it.
|
|
|
|
*/
|
|
|
|
cmnd_pid = cstat->val;
|
|
|
|
sudo_debug_printf(SUDO_DEBUG_INFO, "executed %s, pid %d",
|
2012-08-29 14:40:25 -04:00
|
|
|
details->command, (int)cmnd_pid);
|
2012-08-06 14:38:35 -04:00
|
|
|
if (log_io)
|
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
|
|
|
} else if (cstat->type == CMD_WSTATUS) {
|
2010-02-20 09:41:49 -05:00
|
|
|
if (WIFSTOPPED(cstat->val)) {
|
|
|
|
/* Suspend parent and tell child how to resume on return. */
|
2011-10-22 14:40:21 -04:00
|
|
|
sudo_debug_printf(SUDO_DEBUG_INFO,
|
|
|
|
"child stopped, suspending parent");
|
2010-06-07 18:06:22 -04:00
|
|
|
n = suspend_parent(WSTOPSIG(cstat->val));
|
2010-09-10 11:20:32 -04:00
|
|
|
schedule_signal(n);
|
2010-02-20 09:41:49 -05:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
/* Child exited or was killed, either way we are done. */
|
2011-12-08 11:18:38 -05:00
|
|
|
sudo_debug_printf(SUDO_DEBUG_INFO, "child exited or was killed");
|
2010-02-20 09:41:49 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (cstat->type == CMD_ERRNO) {
|
2010-02-21 10:26:30 -05:00
|
|
|
/* Child was unable to execute command or broken pipe. */
|
2011-12-08 11:18:38 -05:00
|
|
|
sudo_debug_printf(SUDO_DEBUG_INFO, "errno from child: %s",
|
|
|
|
strerror(cstat->val));
|
2009-11-15 21:42:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-04-05 12:39:46 -04:00
|
|
|
do_tty_io:
|
2010-10-15 16:24:42 -04:00
|
|
|
if (perform_io(fdsr, fdsw, cstat) != 0) {
|
|
|
|
/* I/O error, kill child if still alive and finish. */
|
2011-12-08 11:18:38 -05:00
|
|
|
sudo_debug_printf(SUDO_DEBUG_ERROR, "I/O error, terminating child");
|
2010-10-15 16:24:42 -04:00
|
|
|
schedule_signal(SIGKILL);
|
|
|
|
forward_signals(sv[0]);
|
2010-05-07 10:15:25 -04:00
|
|
|
break;
|
2010-10-15 16:24:42 -04:00
|
|
|
}
|
2009-08-06 15:51:12 +00:00
|
|
|
}
|
|
|
|
|
2010-02-27 16:53:56 -05:00
|
|
|
if (log_io) {
|
2010-06-07 18:06:22 -04:00
|
|
|
/* Flush any remaining output and free pty-related memory. */
|
|
|
|
pty_close(cstat);
|
|
|
|
}
|
2010-05-24 18:18:50 -04:00
|
|
|
|
|
|
|
#ifdef HAVE_SELINUX
|
2010-06-09 16:25:44 -04:00
|
|
|
if (ISSET(details->flags, CD_RBAC_ENABLED)) {
|
2010-05-25 11:00:39 -04:00
|
|
|
/* This is probably not needed in log_io mode. */
|
|
|
|
if (selinux_restore_tty() != 0)
|
2012-11-25 09:34:04 -05:00
|
|
|
warningx(_("unable to restore tty label"));
|
2010-05-24 18:18:50 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-05-07 10:14:12 -04:00
|
|
|
efree(fdsr);
|
|
|
|
efree(fdsw);
|
2010-09-10 11:20:32 -04:00
|
|
|
while (!tq_empty(&sigfwd_list)) {
|
|
|
|
struct sigforward *sigfwd = tq_first(&sigfwd_list);
|
|
|
|
tq_remove(&sigfwd_list, sigfwd);
|
|
|
|
efree(sigfwd);
|
|
|
|
}
|
2009-10-22 00:58:41 +00:00
|
|
|
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_return_int(cstat->type == CMD_ERRNO ? -1 : 0);
|
2009-10-22 00:58:41 +00:00
|
|
|
}
|
|
|
|
|
2010-09-10 11:20:32 -04:00
|
|
|
/*
|
|
|
|
* Read signals on fd written to by handler().
|
2011-03-09 11:28:51 -05:00
|
|
|
* Returns -1 on error, 0 on child exit, else 1.
|
2010-09-10 11:20:32 -04:00
|
|
|
*/
|
|
|
|
static int
|
2013-01-17 09:17:54 -05:00
|
|
|
dispatch_signals(int sv[2], pid_t child, int log_io, struct command_status *cstat)
|
2010-09-10 11:20:32 -04:00
|
|
|
{
|
2012-08-29 14:25:09 -04:00
|
|
|
char signame[SIG2STR_MAX];
|
2010-09-10 11:20:32 -04:00
|
|
|
unsigned char signo;
|
|
|
|
ssize_t nread;
|
|
|
|
int status;
|
|
|
|
pid_t pid;
|
2013-01-17 09:17:54 -05:00
|
|
|
debug_decl(dispatch_signals, SUDO_DEBUG_EXEC)
|
2010-09-10 11:20:32 -04:00
|
|
|
|
2010-09-10 11:27:20 -04:00
|
|
|
for (;;) {
|
|
|
|
/* read signal pipe */
|
|
|
|
nread = read(signal_pipe[0], &signo, sizeof(signo));
|
|
|
|
if (nread <= 0) {
|
|
|
|
/* It should not be possible to get EOF but just in case. */
|
|
|
|
if (nread == 0)
|
|
|
|
errno = ECONNRESET;
|
2011-03-09 11:28:51 -05:00
|
|
|
/* Restart if interrupted by signal so the pipe doesn't fill. */
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
/* If pipe is empty, we are done. */
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
break;
|
2011-11-07 16:33:49 -05:00
|
|
|
sudo_debug_printf(SUDO_DEBUG_ERROR, "error reading signal pipe %s",
|
2011-10-22 14:40:21 -04:00
|
|
|
strerror(errno));
|
2011-03-09 11:28:51 -05:00
|
|
|
cstat->type = CMD_ERRNO;
|
|
|
|
cstat->val = errno;
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_return_int(-1);
|
2010-09-10 11:20:32 -04:00
|
|
|
}
|
2012-08-29 14:25:09 -04:00
|
|
|
if (sig2str(signo, signame) == -1)
|
|
|
|
snprintf(signame, sizeof(signame), "%d", signo);
|
|
|
|
sudo_debug_printf(SUDO_DEBUG_DIAG, "received SIG%s", signame);
|
2010-09-10 11:27:20 -04:00
|
|
|
if (signo == SIGCHLD) {
|
|
|
|
/*
|
|
|
|
* If logging I/O, child is the intermediate process,
|
|
|
|
* otherwise it is the command itself.
|
|
|
|
*/
|
|
|
|
do {
|
|
|
|
pid = waitpid(child, &status, WUNTRACED|WNOHANG);
|
|
|
|
} while (pid == -1 && errno == EINTR);
|
|
|
|
if (pid == child) {
|
2011-12-08 11:18:38 -05:00
|
|
|
if (log_io) {
|
|
|
|
/*
|
|
|
|
* On BSD we get ECONNRESET on sv[0] if monitor dies
|
|
|
|
* and select() will return with sv[0] readable.
|
2012-01-25 16:29:08 -05:00
|
|
|
* On Linux that doesn't appear to happen so if the
|
|
|
|
* monitor dies, shut down the socketpair to force a
|
|
|
|
* select() notification.
|
2011-12-08 11:18:38 -05:00
|
|
|
*/
|
2012-01-25 16:29:08 -05:00
|
|
|
(void) shutdown(sv[0], SHUT_WR);
|
2011-12-08 11:18:38 -05:00
|
|
|
} else {
|
2010-09-10 11:27:20 -04:00
|
|
|
if (WIFSTOPPED(status)) {
|
2011-03-16 12:02:04 -04:00
|
|
|
/*
|
|
|
|
* Save the controlling terminal's process group
|
2012-08-27 10:29:59 -04:00
|
|
|
* so we can restore it after we resume, if needed.
|
|
|
|
* Most well-behaved shells change the pgrp back to
|
|
|
|
* its original value before suspending so we must
|
|
|
|
* not try to restore in that case, lest we race with
|
|
|
|
* the child upon resume, potentially stopping sudo
|
|
|
|
* with SIGTTOU while the command continues to run.
|
2011-03-16 12:02:04 -04:00
|
|
|
*/
|
2012-08-27 11:22:33 -04:00
|
|
|
sigaction_t sa, osa;
|
2011-03-16 12:02:04 -04:00
|
|
|
pid_t saved_pgrp = (pid_t)-1;
|
2012-08-27 11:22:33 -04:00
|
|
|
int signo = WSTOPSIG(status);
|
2012-01-17 10:25:44 -05:00
|
|
|
int fd = open(_PATH_TTY, O_RDWR|O_NOCTTY, 0);
|
2012-08-27 10:29:59 -04:00
|
|
|
if (fd != -1) {
|
|
|
|
if ((saved_pgrp = tcgetpgrp(fd)) == ppgrp)
|
|
|
|
saved_pgrp = -1;
|
|
|
|
}
|
2012-08-27 11:22:33 -04:00
|
|
|
if (signo == SIGTSTP) {
|
|
|
|
zero_bytes(&sa, sizeof(sa));
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sa.sa_handler = SIG_DFL;
|
2012-09-16 18:40:39 -04:00
|
|
|
sigaction(SIGTSTP, &sa, &osa);
|
2012-08-27 11:22:33 -04:00
|
|
|
}
|
2012-08-29 14:25:09 -04:00
|
|
|
if (kill(getpid(), signo) != 0)
|
|
|
|
warning("kill(%d, SIG%s)", (int)getpid(), signame);
|
2012-08-27 11:22:33 -04:00
|
|
|
if (signo == SIGTSTP)
|
|
|
|
sigaction(SIGTSTP, &osa, NULL);
|
2012-01-17 10:25:44 -05:00
|
|
|
if (fd != -1) {
|
2012-08-27 10:29:59 -04:00
|
|
|
/*
|
|
|
|
* Restore command's process group if different.
|
|
|
|
* Otherwise, we cannot resume some shells.
|
|
|
|
*/
|
2011-03-16 12:02:04 -04:00
|
|
|
if (saved_pgrp != (pid_t)-1)
|
2012-01-17 10:25:44 -05:00
|
|
|
(void)tcsetpgrp(fd, saved_pgrp);
|
|
|
|
close(fd);
|
2011-03-16 12:02:04 -04:00
|
|
|
}
|
2010-09-10 11:27:20 -04:00
|
|
|
} else {
|
|
|
|
/* Child has exited, we are done. */
|
|
|
|
cstat->type = CMD_WSTATUS;
|
|
|
|
cstat->val = status;
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_return_int(0);
|
2010-09-10 11:27:20 -04:00
|
|
|
}
|
2010-09-10 11:20:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2010-09-10 11:27:20 -04:00
|
|
|
if (log_io) {
|
|
|
|
/* Schedule signo to be forwared to the child. */
|
|
|
|
schedule_signal(signo);
|
2010-09-10 11:20:32 -04:00
|
|
|
} else {
|
2010-09-10 11:27:20 -04:00
|
|
|
/* Nothing listening on sv[0], send directly. */
|
2011-03-16 12:02:04 -04:00
|
|
|
if (signo == SIGALRM)
|
2012-08-06 14:38:35 -04:00
|
|
|
terminate_command(child, false);
|
2011-03-16 12:02:04 -04:00
|
|
|
else if (kill(child, signo) != 0)
|
2012-08-29 14:25:09 -04:00
|
|
|
warning("kill(%d, SIG%s)", (int)child, signame);
|
2010-09-10 11:20:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_return_int(1);
|
2010-09-10 11:20:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Forward signals in sigfwd_list to child listening on fd.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
forward_signals(int sock)
|
|
|
|
{
|
2012-08-29 14:25:09 -04:00
|
|
|
char signame[SIG2STR_MAX];
|
2010-09-10 11:20:32 -04:00
|
|
|
struct sigforward *sigfwd;
|
|
|
|
struct command_status cstat;
|
|
|
|
ssize_t nsent;
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_decl(forward_signals, SUDO_DEBUG_EXEC)
|
2010-09-10 11:20:32 -04:00
|
|
|
|
|
|
|
while (!tq_empty(&sigfwd_list)) {
|
|
|
|
sigfwd = tq_first(&sigfwd_list);
|
2012-12-28 11:01:36 -05:00
|
|
|
if (sigfwd->signo == SIGCONT_FG)
|
|
|
|
strlcpy(signame, "CONT_FG", sizeof(signame));
|
|
|
|
else if (sigfwd->signo == SIGCONT_BG)
|
|
|
|
strlcpy(signame, "CONT_BG", sizeof(signame));
|
|
|
|
else if (sig2str(sigfwd->signo, signame) == -1)
|
2012-08-29 14:25:09 -04:00
|
|
|
snprintf(signame, sizeof(signame), "%d", sigfwd->signo);
|
2011-10-22 14:40:21 -04:00
|
|
|
sudo_debug_printf(SUDO_DEBUG_INFO,
|
2012-08-29 14:25:09 -04:00
|
|
|
"sending SIG%s to child over backchannel", signame);
|
2010-09-10 11:20:32 -04:00
|
|
|
cstat.type = CMD_SIGNO;
|
|
|
|
cstat.val = sigfwd->signo;
|
|
|
|
do {
|
|
|
|
nsent = send(sock, &cstat, sizeof(cstat), 0);
|
|
|
|
} while (nsent == -1 && errno == EINTR);
|
|
|
|
tq_remove(&sigfwd_list, sigfwd);
|
|
|
|
efree(sigfwd);
|
|
|
|
if (nsent != sizeof(cstat)) {
|
|
|
|
if (errno == EPIPE) {
|
2011-12-08 11:18:38 -05:00
|
|
|
sudo_debug_printf(SUDO_DEBUG_ERROR,
|
|
|
|
"broken pipe writing to child over backchannel");
|
2010-09-10 11:20:32 -04:00
|
|
|
/* Other end of socket gone, empty out sigfwd_list. */
|
|
|
|
while (!tq_empty(&sigfwd_list)) {
|
|
|
|
sigfwd = tq_first(&sigfwd_list);
|
|
|
|
tq_remove(&sigfwd_list, sigfwd);
|
|
|
|
efree(sigfwd);
|
|
|
|
}
|
2011-12-08 11:18:38 -05:00
|
|
|
/* XXX - child (monitor) is dead, we should exit too? */
|
2010-09-10 11:20:32 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_return;
|
2010-09-10 11:20:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Schedule a signal to be forwared.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
schedule_signal(int signo)
|
|
|
|
{
|
|
|
|
struct sigforward *sigfwd;
|
2012-08-29 14:25:09 -04:00
|
|
|
char signame[SIG2STR_MAX];
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_decl(schedule_signal, SUDO_DEBUG_EXEC)
|
|
|
|
|
2012-12-28 11:01:36 -05:00
|
|
|
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)
|
2012-08-29 14:25:09 -04:00
|
|
|
snprintf(signame, sizeof(signame), "%d", signo);
|
2012-12-28 11:01:36 -05:00
|
|
|
sudo_debug_printf(SUDO_DEBUG_DIAG, "scheduled SIG%s for child", signame);
|
2010-09-10 11:20:32 -04:00
|
|
|
|
2012-03-19 11:24:24 -04:00
|
|
|
sigfwd = ecalloc(1, sizeof(*sigfwd));
|
2010-09-10 11:20:32 -04:00
|
|
|
sigfwd->prev = sigfwd;
|
2012-03-19 11:24:24 -04:00
|
|
|
/* sigfwd->next = NULL; */
|
2010-09-10 11:20:32 -04:00
|
|
|
sigfwd->signo = signo;
|
|
|
|
tq_append(&sigfwd_list, sigfwd);
|
2011-10-22 14:40:21 -04:00
|
|
|
|
|
|
|
debug_return;
|
2010-09-10 11:20:32 -04:00
|
|
|
}
|
|
|
|
|
2009-10-31 17:14:52 +00:00
|
|
|
/*
|
2010-05-21 12:01:11 -04:00
|
|
|
* Generic handler for signals passed from parent -> child.
|
2010-09-10 11:20:32 -04:00
|
|
|
* The other end of signal_pipe is checked in the main event loop.
|
2009-10-31 17:14:52 +00:00
|
|
|
*/
|
2012-08-06 14:38:35 -04:00
|
|
|
#ifdef SA_SIGINFO
|
|
|
|
void
|
|
|
|
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 child 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
|
2010-06-07 18:06:22 -04:00
|
|
|
void
|
2010-02-20 09:41:49 -05:00
|
|
|
handler(int s)
|
2009-10-31 17:14:52 +00:00
|
|
|
{
|
2010-09-10 11:20:32 -04:00
|
|
|
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.
|
|
|
|
*/
|
2012-03-29 10:33:40 -04:00
|
|
|
ignore_result(write(signal_pipe[1], &signo, sizeof(signo)));
|
2010-09-10 11:20:32 -04:00
|
|
|
}
|
2012-08-06 14:38:35 -04:00
|
|
|
#endif
|
2010-09-10 11:20:32 -04:00
|
|
|
|
2012-01-17 10:27:33 -05:00
|
|
|
#ifdef SA_SIGINFO
|
|
|
|
/*
|
|
|
|
* Generic handler for signals passed from parent -> child.
|
|
|
|
* The other end of signal_pipe is checked in the main event loop.
|
|
|
|
* This version is for the non-pty case and does not forward
|
|
|
|
* signals that are generated by the kernel.
|
|
|
|
*/
|
|
|
|
static void
|
2012-08-06 14:38:35 -04:00
|
|
|
handler_user_only(int s, siginfo_t *info, void *context)
|
2012-01-17 10:27:33 -05:00
|
|
|
{
|
|
|
|
unsigned char signo = (unsigned char)s;
|
|
|
|
|
|
|
|
/* Only forward user-generated signals. */
|
2012-07-31 13:36:48 -04:00
|
|
|
if (info != NULL && info->si_code == SI_USER) {
|
2012-01-17 10:27:33 -05:00
|
|
|
/*
|
|
|
|
* The pipe is non-blocking, if we overflow the kernel's pipe
|
|
|
|
* buffer we drop the signal. This is not a problem in practice.
|
|
|
|
*/
|
2012-03-29 10:33:40 -04:00
|
|
|
ignore_result(write(signal_pipe[1], &signo, sizeof(signo)));
|
2012-01-17 10:27:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* SA_SIGINFO */
|
|
|
|
|
2010-09-10 11:20:32 -04:00
|
|
|
/*
|
|
|
|
* Open a pipe and make both ends non-blocking.
|
|
|
|
* Returns 0 on success and -1 on error.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
pipe_nonblock(int fds[2])
|
|
|
|
{
|
|
|
|
int flags, rval;
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_decl(pipe_nonblock, SUDO_DEBUG_EXEC)
|
2010-09-10 11:20:32 -04:00
|
|
|
|
|
|
|
rval = pipe(fds);
|
|
|
|
if (rval != -1) {
|
|
|
|
flags = fcntl(fds[0], F_GETFL, 0);
|
|
|
|
if (flags != -1 && !ISSET(flags, O_NONBLOCK))
|
|
|
|
rval = fcntl(fds[0], F_SETFL, flags | O_NONBLOCK);
|
|
|
|
if (rval != -1) {
|
|
|
|
flags = fcntl(fds[1], F_GETFL, 0);
|
|
|
|
if (flags != -1 && !ISSET(flags, O_NONBLOCK))
|
|
|
|
rval = fcntl(fds[1], F_SETFL, flags | O_NONBLOCK);
|
|
|
|
}
|
|
|
|
if (rval == -1) {
|
|
|
|
close(fds[0]);
|
|
|
|
close(fds[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_return_int(rval);
|
2009-10-31 17:14:52 +00:00
|
|
|
}
|