#include <config.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/time.h>
#include <sys/wait.h>
#ifdef HAVE_TERMIOS_H
# include <termios.h>
#else
# include <termio.h>
#endif
#include <sys/ioctl.h>
#ifdef HAVE_SYS_SELECT_H
# include <sys/select.h>
#endif
#include <stdio.h>
#ifdef STDC_HEADERS
# include <stdlib.h>
# include <stddef.h>
#else
# ifdef HAVE_STDLIB_H
# include <stdlib.h>
# endif
#endif
#ifdef HAVE_STRING_H
# if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
# include <memory.h>
# endif
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#if TIME_WITH_SYS_TIME
# include <time.h>
#endif
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include "sudo.h"
#include "sudo_exec.h"
#define SFD_STDIN 0
#define SFD_STDOUT 1
#define SFD_STDERR 2
#define SFD_MASTER 3
#define SFD_SLAVE 4
#define SFD_USERTTY 5
#define TERM_COOKED 0
#define TERM_RAW 1
#if !defined(TIOCGWINSZ) && defined(TIOCGSIZE)
# define TIOCGWINSZ TIOCGSIZE
# define TIOCSWINSZ TIOCSSIZE
# define winsize ttysize
#endif
struct io_buffer {
struct io_buffer *next;
int len;
int off;
int rfd;
int wfd;
int (*action) __P((const char *buf, unsigned int len));
char buf[16 * 1024];
};
static char slavename[PATH_MAX];
static int foreground;
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 pid_t ppgrp, cmnd_pgrp, mon_pgrp;;
static struct io_buffer *iobufs;
static void flush_output __P((void));
static int exec_monitor __P((const char *path, char *argv[],
char *envp[], int, int));
static void exec_pty __P((const char *path, char *argv[],
char *envp[], int, int *));
static RETSIGTYPE sigwinch __P((int s));
static void sync_ttysize __P((int src, int dst));
static void deliver_signal __P((pid_t pid, int signo));
static int safe_close __P((int fd));
void
pty_setup(uid)
uid_t uid;
{
io_fds[SFD_USERTTY] = open(_PATH_TTY, O_RDWR|O_NOCTTY, 0);
if (io_fds[SFD_USERTTY] != -1) {
if (!get_pty(&io_fds[SFD_MASTER], &io_fds[SFD_SLAVE],
slavename, sizeof(slavename), uid))
error(1, "Can't get pty");
}
}
static void
check_foreground()
{
if (io_fds[SFD_USERTTY] != -1) {
foreground = tcgetpgrp(io_fds[SFD_USERTTY]) == ppgrp;
if (foreground && !tty_initialized) {
if (term_copy(io_fds[SFD_USERTTY], io_fds[SFD_SLAVE])) {
tty_initialized = 1;
sync_ttysize(io_fds[SFD_USERTTY], io_fds[SFD_SLAVE]);
}
}
}
}
#ifdef SA_SIGINFO
void
mon_handler(s, info, context)
int s;
siginfo_t *info;
void *context;
{
unsigned char signo = (unsigned char)s;
if (info != NULL && info->si_code == SI_USER && info->si_pid == cmnd_pid)
return;
ignore_result(write(signal_pipe[1], &signo, sizeof(signo)));
}
#else
void
mon_handler(s)
int s;
{
unsigned char signo = (unsigned char)s;
ignore_result(write(signal_pipe[1], &signo, sizeof(signo)));
}
#endif
int
suspend_parent(signo)
int signo;
{
sigaction_t sa, osa;
int n, oldmode = ttymode, rval = 0;
switch (signo) {
case SIGTTOU:
case SIGTTIN:
if (!foreground)
check_foreground();
if (foreground) {
if (ttymode != TERM_RAW) {
do {
n = term_raw(io_fds[SFD_USERTTY], 0);
} while (!n && errno == EINTR);
ttymode = TERM_RAW;
}
rval = SIGCONT_FG;
break;
}
ttymode = TERM_RAW;
case SIGSTOP:
case SIGTSTP:
flush_output();
if (oldmode != TERM_COOKED) {
do {
n = term_restore(io_fds[SFD_USERTTY], 0);
} while (!n && errno == EINTR);
}
if (signo != SIGSTOP) {
zero_bytes(&sa, sizeof(sa));
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_INTERRUPT;
sa.sa_handler = SIG_DFL;
sigaction(signo, &sa, &osa);
}
if (killpg(ppgrp, signo) != 0)
warning("killpg(%d, %d)", ppgrp, signo);
check_foreground();
if (ttymode != TERM_COOKED) {
if (foreground) {
do {
n = term_raw(io_fds[SFD_USERTTY], 0);
} while (!n && errno == EINTR);
} else {
ttymode = TERM_COOKED;
}
}
if (signo != SIGSTOP)
sigaction(signo, &osa, NULL);
rval = ttymode == TERM_RAW ? SIGCONT_FG : SIGCONT_BG;
break;
}
return rval;
}
static void
terminate_command(pid, use_pgrp)
pid_t pid;
int use_pgrp;
{
if (use_pgrp) {
killpg(pid, SIGHUP);
killpg(pid, SIGTERM);
sleep(2);
killpg(pid, SIGKILL);
} else {
kill(pid, SIGHUP);
kill(pid, SIGTERM);
sleep(2);
kill(pid, SIGKILL);
}
}
static struct io_buffer *
io_buf_new(rfd, wfd, action, head)
int rfd;
int wfd;
int (*action) __P((const char *, unsigned int));
struct io_buffer *head;
{
struct io_buffer *iob;
iob = ecalloc(1, sizeof(*iob));
iob->rfd = rfd;
iob->wfd = wfd;
iob->action = action;
iob->next = head;
return iob;
}
int
perform_io(fdsr, fdsw, cstat)
fd_set *fdsr;
fd_set *fdsw;
struct command_status *cstat;
{
struct io_buffer *iob;
int n, errors = 0;
for (iob = iobufs; iob; iob = iob->next) {
if (iob->rfd != -1 && FD_ISSET(iob->rfd, fdsr)) {
do {
n = read(iob->rfd, iob->buf + iob->len,
sizeof(iob->buf) - iob->len);
} while (n == -1 && errno == EINTR);
switch (n) {
case -1:
if (errno != EAGAIN) {
safe_close(iob->rfd);
iob->rfd = -1;
}
break;
case 0:
safe_close(iob->rfd);
iob->rfd = -1;
break;
default:
if (!iob->action(iob->buf + iob->len, n))
terminate_command(cmnd_pid, TRUE);
iob->len += n;
break;
}
}
if (iob->wfd != -1 && FD_ISSET(iob->wfd, fdsw)) {
do {
n = write(iob->wfd, iob->buf + iob->off,
iob->len - iob->off);
} while (n == -1 && errno == EINTR);
if (n == -1) {
if (errno == EPIPE || errno == ENXIO || errno == EIO || errno == EBADF) {
if (iob->rfd != -1) {
safe_close(iob->rfd);
iob->rfd = -1;
}
safe_close(iob->wfd);
iob->wfd = -1;
continue;
}
if (errno != EAGAIN)
errors++;
} else {
iob->off += n;
}
}
}
if (errors && cstat != NULL) {
cstat->type = CMD_ERRNO;
cstat->val = errno;
}
return errors;
}
int
fork_pty(path, argv, envp, sv, rbac_enabled, bgmode, maxfd, omask)
const char *path;
char *argv[];
char *envp[];
int sv[2];
int rbac_enabled;
int bgmode;
int *maxfd;
sigset_t *omask;
{
struct command_status cstat;
struct io_buffer *iob;
int io_pipe[3][2], n;
sigaction_t sa;
sigset_t mask;
pid_t child;
ppgrp = getpgrp();
zero_bytes(&sa, sizeof(sa));
sigemptyset(&sa.sa_mask);
if (io_fds[SFD_USERTTY] != -1) {
sa.sa_flags = SA_RESTART;
sa.sa_handler = sigwinch;
sigaction(SIGWINCH, &sa, NULL);
}
if (!bgmode)
io_fds[SFD_STDIN] = io_fds[SFD_SLAVE];
io_fds[SFD_STDOUT] = io_fds[SFD_SLAVE];
io_fds[SFD_STDERR] = io_fds[SFD_SLAVE];
if (io_fds[SFD_USERTTY] != -1) {
if (!bgmode) {
iobufs = io_buf_new(io_fds[SFD_USERTTY], io_fds[SFD_MASTER],
log_ttyin, iobufs);
}
iobufs = io_buf_new(io_fds[SFD_MASTER], io_fds[SFD_USERTTY],
log_ttyout, iobufs);
foreground = tcgetpgrp(io_fds[SFD_USERTTY]) == ppgrp;
}
memset(io_pipe, 0, sizeof(io_pipe));
if (io_fds[SFD_STDIN] == -1 || !isatty(STDIN_FILENO)) {
pipeline = TRUE;
if (pipe(io_pipe[STDIN_FILENO]) != 0)
error(1, "unable to create pipe");
iobufs = io_buf_new(STDIN_FILENO, io_pipe[STDIN_FILENO][1],
log_stdin, iobufs);
io_fds[SFD_STDIN] = io_pipe[STDIN_FILENO][0];
}
if (io_fds[SFD_STDOUT] == -1 || !isatty(STDOUT_FILENO)) {
pipeline = TRUE;
if (pipe(io_pipe[STDOUT_FILENO]) != 0)
error(1, "unable to create pipe");
iobufs = io_buf_new(io_pipe[STDOUT_FILENO][0], STDOUT_FILENO,
log_stdout, iobufs);
io_fds[SFD_STDOUT] = io_pipe[STDOUT_FILENO][1];
}
if (io_fds[SFD_STDERR] == -1 || !isatty(STDERR_FILENO)) {
if (pipe(io_pipe[STDERR_FILENO]) != 0)
error(1, "unable to create pipe");
iobufs = io_buf_new(io_pipe[STDERR_FILENO][0], STDERR_FILENO,
log_stderr, iobufs);
io_fds[SFD_STDERR] = io_pipe[STDERR_FILENO][1];
}
sa.sa_flags = SA_INTERRUPT;
#ifdef SA_SIGINFO
sa.sa_flags |= SA_SIGINFO;
sa.sa_sigaction = handler;
#else
sa.sa_handler = handler;
#endif
sigaction(SIGTSTP, &sa, NULL);
sa.sa_handler = SIG_IGN;
sigaction(SIGTTIN, &sa, NULL);
sigaction(SIGTTOU, &sa, NULL);
if (foreground) {
if (term_copy(io_fds[SFD_USERTTY], io_fds[SFD_SLAVE])) {
tty_initialized = 1;
sync_ttysize(io_fds[SFD_USERTTY], io_fds[SFD_SLAVE]);
}
if (!pipeline) {
ttymode = TERM_RAW;
do {
n = term_raw(io_fds[SFD_USERTTY], 0);
} while (!n && errno == EINTR);
if (!n)
error(1, "Can't set terminal to raw mode");
}
}
sigemptyset(&mask);
sigaddset(&mask, SIGTERM);
sigaddset(&mask, SIGHUP);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGQUIT);
sigprocmask(SIG_BLOCK, &mask, omask);
child = fork();
switch (child) {
case -1:
error(1, "fork");
break;
case 0:
close(sv[0]);
close(signal_pipe[0]);
close(signal_pipe[1]);
fcntl(sv[1], F_SETFD, FD_CLOEXEC);
sigprocmask(SIG_SETMASK, omask, NULL);
if (exec_setup(rbac_enabled, slavename, io_fds[SFD_SLAVE]) == TRUE) {
if (io_pipe[STDIN_FILENO][1])
close(io_pipe[STDIN_FILENO][1]);
if (io_pipe[STDOUT_FILENO][0])
close(io_pipe[STDOUT_FILENO][0]);
if (io_pipe[STDERR_FILENO][0])
close(io_pipe[STDERR_FILENO][0]);
exec_monitor(path, argv, envp, sv[1], rbac_enabled);
}
cstat.type = CMD_ERRNO;
cstat.val = errno;
ignore_result(send(sv[1], &cstat, sizeof(cstat), 0));
_exit(1);
}
if (io_pipe[STDIN_FILENO][0])
close(io_pipe[STDIN_FILENO][0]);
if (io_pipe[STDOUT_FILENO][1])
close(io_pipe[STDOUT_FILENO][1]);
if (io_pipe[STDERR_FILENO][1])
close(io_pipe[STDERR_FILENO][1]);
for (iob = iobufs; iob; iob = iob->next) {
if (iob->rfd > *maxfd)
*maxfd = iob->rfd;
if (iob->wfd > *maxfd)
*maxfd = iob->wfd;
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
pty_close(cstat)
struct command_status *cstat;
{
int n;
if (io_fds[SFD_USERTTY] != -1) {
n = fcntl(io_fds[SFD_USERTTY], F_GETFL, 0);
if (n != -1 && ISSET(n, O_NONBLOCK)) {
CLR(n, O_NONBLOCK);
(void) fcntl(io_fds[SFD_USERTTY], F_SETFL, n);
}
}
flush_output();
if (io_fds[SFD_USERTTY] != -1) {
check_foreground();
if (foreground) {
do {
n = term_restore(io_fds[SFD_USERTTY], 0);
} while (!n && errno == EINTR);
}
}
if (cstat->type == CMD_WSTATUS && WIFSIGNALED(cstat->val)) {
int signo = WTERMSIG(cstat->val);
if (signo && signo != SIGINT && signo != SIGPIPE) {
const char *reason = strsignal(signo);
n = io_fds[SFD_USERTTY] != -1 ?
io_fds[SFD_USERTTY] : STDOUT_FILENO;
if (write(n, reason, strlen(reason)) != -1) {
if (WCOREDUMP(cstat->val)) {
ignore_result(write(n, " (core dumped)", 14));
}
ignore_result(write(n, "\n", 1));
}
}
}
}
void
fd_set_iobs(fdsr, fdsw)
fd_set *fdsr;
fd_set *fdsw;
{
struct io_buffer *iob;
for (iob = iobufs; iob; iob = iob->next) {
if (iob->rfd == -1 && iob->wfd == -1)
continue;
if (iob->off == iob->len) {
iob->off = iob->len = 0;
if (iob->rfd == -1) {
safe_close(iob->wfd);
iob->wfd = -1;
}
}
if (iob->rfd != -1 &&
(ttymode == TERM_RAW || iob->rfd != io_fds[SFD_USERTTY])) {
if (iob->len != sizeof(iob->buf))
FD_SET(iob->rfd, fdsr);
}
if (iob->wfd != -1 &&
(foreground || iob->wfd != io_fds[SFD_USERTTY])) {
if (iob->len > iob->off)
FD_SET(iob->wfd, fdsw);
}
}
}
static void
deliver_signal(pid, signo)
pid_t pid;
int signo;
{
int status;
switch (signo) {
case SIGCONT_FG:
do {
status = tcsetpgrp(io_fds[SFD_SLAVE], cmnd_pgrp);
} while (status == -1 && errno == EINTR);
killpg(pid, SIGCONT);
break;
case SIGCONT_BG:
do {
status = tcsetpgrp(io_fds[SFD_SLAVE], mon_pgrp);
} while (status == -1 && errno == EINTR);
killpg(pid, SIGCONT);
break;
case SIGKILL:
_exit(1);
default:
killpg(pid, signo);
break;
}
}
static int
send_status(fd, cstat)
int fd;
struct command_status *cstat;
{
int n = -1;
if (cstat->type != CMD_INVALID) {
do {
n = send(fd, cstat, sizeof(*cstat), 0);
} while (n == -1 && errno == EINTR);
cstat->type = CMD_INVALID;
}
return n;
}
static int
handle_sigchld(backchannel, cstat)
int backchannel;
struct command_status *cstat;
{
int status, alive = TRUE;
pid_t pid;
do {
#ifdef sudo_waitpid
pid = sudo_waitpid(cmnd_pid, &status, WUNTRACED|WNOHANG);
#else
pid = wait(&status);
#endif
} while (pid == -1 && errno == EINTR);
if (pid == cmnd_pid) {
if (cstat->type != CMD_ERRNO) {
cstat->type = CMD_WSTATUS;
cstat->val = status;
if (WIFSTOPPED(status)) {
do {
pid = tcgetpgrp(io_fds[SFD_SLAVE]);
} while (pid == -1 && errno == EINTR);
if (pid != mon_pgrp)
cmnd_pgrp = pid;
if (send_status(backchannel, cstat) == -1)
return alive;
}
}
if (!WIFSTOPPED(status))
alive = FALSE;
}
return alive;
}
static int
exec_monitor(path, argv, envp, backchannel, rbac)
const char *path;
char *argv[];
char *envp[];
int backchannel;
int rbac;
{
struct command_status cstat;
struct timeval tv;
fd_set *fdsr;
sigaction_t sa;
int errpipe[2], maxfd, n;
int alive = TRUE;
unsigned char signo;
if (io_fds[SFD_MASTER] != -1)
close(io_fds[SFD_MASTER]);
if (io_fds[SFD_USERTTY] != -1)
close(io_fds[SFD_USERTTY]);
if (pipe_nonblock(signal_pipe) != 0)
error(1, "cannot create pipe");
zero_bytes(&sa, sizeof(sa));
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
sa.sa_handler = SIG_DFL;
sigaction(SIGWINCH, &sa, NULL);
sa.sa_handler = SIG_IGN;
sigaction(SIGTTIN, &sa, NULL);
sigaction(SIGTTOU, &sa, NULL);
sa.sa_flags = SA_INTERRUPT;
#ifdef SA_SIGINFO
sa.sa_flags |= SA_SIGINFO;
sa.sa_sigaction = mon_handler;
#else
sa.sa_handler = mon_handler;
#endif
sigaction(SIGCHLD, &sa, NULL);
sa.sa_flags = SA_RESTART;
#ifdef SA_SIGINFO
sa.sa_flags |= SA_SIGINFO;
sa.sa_sigaction = mon_handler;
#else
sa.sa_handler = mon_handler;
#endif
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGQUIT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGTSTP, &sa, NULL);
sigaction(SIGUSR1, &sa, NULL);
sigaction(SIGUSR2, &sa, NULL);
if (setsid() == -1) {
warning("setsid");
goto bad;
}
if (io_fds[SFD_SLAVE] != -1) {
#ifdef TIOCSCTTY
if (ioctl(io_fds[SFD_SLAVE], TIOCSCTTY, NULL) != 0)
error(1, "unable to set controlling tty");
#else
if ((n = open(slavename, O_RDWR)) >= 0)
close(n);
#endif
}
mon_pgrp = getpgrp();
if (pipeline)
foreground = 0;
if (pipe(errpipe) == -1)
error(1, "unable to create pipe");
cmnd_pid = fork();
if (cmnd_pid == -1) {
warning("Can't fork");
goto bad;
}
if (cmnd_pid == 0) {
close(backchannel);
close(signal_pipe[0]);
close(signal_pipe[1]);
close(errpipe[0]);
fcntl(errpipe[1], F_SETFD, FD_CLOEXEC);
restore_signals();
exec_pty(path, argv, envp, rbac, &errpipe[1]);
cstat.type = CMD_ERRNO;
cstat.val = errno;
ignore_result(write(errpipe[1], &cstat, sizeof(cstat)));
_exit(1);
}
close(errpipe[1]);
cstat.type = CMD_PID;
cstat.val = cmnd_pid;
ignore_result(send(backchannel, &cstat, sizeof(cstat), 0));
if (io_fds[SFD_STDIN] != io_fds[SFD_SLAVE])
close(io_fds[SFD_STDIN]);
if (io_fds[SFD_STDOUT] != io_fds[SFD_SLAVE])
close(io_fds[SFD_STDOUT]);
if (io_fds[SFD_STDERR] != io_fds[SFD_SLAVE])
close(io_fds[SFD_STDERR]);
cmnd_pgrp = cmnd_pid;
setpgid(cmnd_pid, cmnd_pgrp);
if (foreground) {
do {
n = tcsetpgrp(io_fds[SFD_SLAVE], cmnd_pgrp);
} while (n == -1 && errno == EINTR);
}
maxfd = MAX(MAX(errpipe[0], signal_pipe[0]), backchannel);
fdsr = ecalloc(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask));
tv.tv_sec = 0;
tv.tv_usec = 0;
for (;;) {
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);
n = select(maxfd + 1, fdsr, NULL, NULL, alive ? NULL : &tv);
if (n <= 0) {
if (n == 0)
goto done;
if (errno == EINTR || errno == ENOMEM)
continue;
warning("monitor: select failed");
break;
}
if (FD_ISSET(signal_pipe[0], fdsr)) {
n = read(signal_pipe[0], &signo, sizeof(signo));
if (n == -1) {
if (errno == EINTR || errno == EAGAIN)
continue;
warning("error reading from signal pipe");
goto done;
}
if (signo == SIGCHLD) {
if (!handle_sigchld(backchannel, &cstat))
alive = FALSE;
} else {
deliver_signal(cmnd_pid, signo);
}
continue;
}
if (errpipe[0] != -1 && FD_ISSET(errpipe[0], fdsr)) {
n = read(errpipe[0], &cstat, sizeof(cstat));
if (n == -1) {
if (errno == EINTR)
continue;
warning("error reading from pipe");
goto done;
}
FD_CLR(errpipe[0], fdsr);
close(errpipe[0]);
errpipe[0] = -1;
}
if (FD_ISSET(backchannel, fdsr)) {
struct command_status cstmp;
n = recv(backchannel, &cstmp, sizeof(cstmp), 0);
if (n == -1) {
if (errno == EINTR)
continue;
warning("error reading from socketpair");
goto done;
}
if (cstmp.type != CMD_SIGNO) {
warningx("unexpected reply type on backchannel: %d", cstmp.type);
continue;
}
deliver_signal(cmnd_pid, cstmp.val);
}
}
done:
if (alive) {
kill(cmnd_pid, SIGKILL);
} else {
send_status(backchannel, &cstat);
}
_exit(1);
bad:
return errno;
}
static void
flush_output()
{
struct io_buffer *iob;
struct timeval tv;
fd_set *fdsr, *fdsw;
int nready, nwriters, maxfd = -1;
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 = emalloc2(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask));
fdsw = emalloc2(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask));
for (;;) {
memset(fdsw, 0, howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask));
memset(fdsr, 0, howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask));
nwriters = 0;
for (iob = iobufs; iob; iob = iob->next) {
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;
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);
}
}
}
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;
if (errno == EINTR || errno == ENOMEM)
continue;
warning("select failed");
}
if (perform_io(fdsr, fdsw, NULL) != 0 || nready == -1)
break;
}
efree(fdsr);
efree(fdsw);
}
static void
exec_pty(path, argv, envp, rbac_enabled, errfd)
const char *path;
char *argv[];
char *envp[];
int rbac_enabled;
int *errfd;
{
int maxfd = def_closefrom;
pid_t self = getpid();
setpgid(0, self);
if (dup2(io_fds[SFD_STDIN], STDIN_FILENO) == -1 ||
dup2(io_fds[SFD_STDOUT], STDOUT_FILENO) == -1 ||
dup2(io_fds[SFD_STDERR], STDERR_FILENO) == -1)
error(1, "dup2");
if (foreground) {
while (tcgetpgrp(io_fds[SFD_SLAVE]) != self)
;
}
if (io_fds[SFD_SLAVE] != -1)
close(io_fds[SFD_SLAVE]);
if (io_fds[SFD_STDIN] != io_fds[SFD_SLAVE])
close(io_fds[SFD_STDIN]);
if (io_fds[SFD_STDOUT] != io_fds[SFD_SLAVE])
close(io_fds[SFD_STDOUT]);
if (io_fds[SFD_STDERR] != io_fds[SFD_SLAVE])
close(io_fds[SFD_STDERR]);
dup2(*errfd, maxfd);
(void)fcntl(maxfd, F_SETFD, FD_CLOEXEC);
*errfd = maxfd++;
closefrom(maxfd);
#ifdef HAVE_SELINUX
if (rbac_enabled)
selinux_execve(path, argv, envp);
else
#endif
my_execve(path, argv, envp);
}
static void
sync_ttysize(src, dst)
int src;
int dst;
{
#ifdef TIOCGWINSZ
struct winsize wsize;
pid_t pgrp;
if (ioctl(src, TIOCGWINSZ, &wsize) == 0) {
ioctl(dst, TIOCSWINSZ, &wsize);
if ((pgrp = tcgetpgrp(dst)) != -1)
killpg(pgrp, SIGWINCH);
}
#endif
}
static void
sigwinch(s)
int s;
{
int serrno = errno;
sync_ttysize(io_fds[SFD_USERTTY], io_fds[SFD_SLAVE]);
errno = serrno;
}
static int
safe_close(fd)
int fd;
{
if (fd < 3 || fd == io_fds[SFD_USERTTY]) {
errno = EINVAL;
return -1;
}
return close(fd);
}
void
cleanup_pty(gotsignal)
int gotsignal;
{
if (io_fds[SFD_USERTTY] != -1) {
check_foreground();
if (foreground)
term_restore(io_fds[SFD_USERTTY], 0);
}
}