Need to ignore SIGTT{IN,OU} in child when running the command in the

background.  Also some minor cleanup.
This commit is contained in:
Todd C. Miller
2009-11-01 15:14:58 +00:00
parent 78cd63e074
commit febc3cbb1f

View File

@@ -110,7 +110,6 @@ static void handler __P((int s));
static void sigchild __P((int s)); static void sigchild __P((int s));
static void sigcont __P((int s)); static void sigcont __P((int s));
static void sigrelay __P((int s)); static void sigrelay __P((int s));
static void sigtstp __P((int s));
static void sigwinch __P((int s)); static void sigwinch __P((int s));
static void flush_output __P((struct script_buf *output, struct timeval *then, static void flush_output __P((struct script_buf *output, struct timeval *then,
struct timeval *now, void *ofile, void *tfile)); struct timeval *now, void *ofile, void *tfile));
@@ -428,7 +427,7 @@ script_execv(path, argv)
/* Handler for tty-based signals */ /* Handler for tty-based signals */
sa.sa_flags = 0; /* do not restart syscalls for these three */ sa.sa_flags = 0; /* do not restart syscalls for these three */
sa.sa_handler = sigtstp; sa.sa_handler = handler;
sigaction(SIGTSTP, &sa, NULL); sigaction(SIGTSTP, &sa, NULL);
sigaction(SIGTTIN, &sa, NULL); sigaction(SIGTTIN, &sa, NULL);
sigaction(SIGTTOU, &sa, NULL); sigaction(SIGTTOU, &sa, NULL);
@@ -442,8 +441,6 @@ script_execv(path, argv)
log_error(USE_ERRNO, "Can't set terminal to raw mode"); log_error(USE_ERRNO, "Can't set terminal to raw mode");
} }
/* XXX - should also catch terminal signals and kill child */
/* /*
* Child will run the command in the pty, parent will pass data * Child will run the command in the pty, parent will pass data
* to and from pty. * to and from pty.
@@ -633,7 +630,6 @@ script_execv(path, argv)
/* Update output and timing files. */ /* Update output and timing files. */
log_output(output.buf + output.len, n, &then, &now, ofile, tfile); log_output(output.buf + output.len, n, &then, &now, ofile, tfile);
output.len += n; output.len += n;
} }
} }
@@ -706,10 +702,13 @@ script_child(path, argv, foreground, rbac_enabled)
sa.sa_handler = SIG_DFL; sa.sa_handler = SIG_DFL;
sigaction(SIGCHLD, &sa, NULL); sigaction(SIGCHLD, &sa, NULL);
sigaction(SIGCONT, &sa, NULL); sigaction(SIGCONT, &sa, NULL);
sigaction(SIGTSTP, &sa, NULL); sigaction(SIGWINCH, &sa, NULL);
/* Ignore SIGTT{IN,OU} if we get any. */
/* XXX - how can we since the pgrp is orphaned? */
sa.sa_handler = SIG_IGN;
sigaction(SIGTTIN, &sa, NULL); sigaction(SIGTTIN, &sa, NULL);
sigaction(SIGTTOU, &sa, NULL); sigaction(SIGTTOU, &sa, NULL);
sigaction(SIGWINCH, &sa, NULL);
/* /*
* Handle signals from parent and pass to child. * Handle signals from parent and pass to child.
@@ -760,6 +759,10 @@ script_child(path, argv, foreground, rbac_enabled)
_exit(1); _exit(1);
} }
if (child == 0) { if (child == 0) {
sa.sa_flags = SA_RESTART;
sa.sa_handler = SIG_DFL;
sigaction(SIGTTIN, &sa, NULL);
sigaction(SIGTTOU, &sa, NULL);
/* setup tty and exec command */ /* setup tty and exec command */
script_run(path, argv, rbac_enabled); script_run(path, argv, rbac_enabled);
warning("unable to execute %s", path); warning("unable to execute %s", path);
@@ -781,23 +784,26 @@ script_child(path, argv, foreground, rbac_enabled)
/* Wait for signal to arrive or for child to stop/exit. */ /* Wait for signal to arrive or for child to stop/exit. */
for (;;) { for (;;) {
switch (recvsig) { while ((signo = recvsig)) {
case SIGHUP: recvsig = 0;
case SIGTERM: switch (signo) {
/* signal child; we'll get its status when we continue. */ case SIGHUP:
killpg(child, recvsig); case SIGTERM:
break; /* signal child; we'll get its status when we continue. */
case SIGUSR1: killpg(child, signo);
/* foreground process, grant it controlling tty. */ break;
do { case SIGUSR1:
status = tcsetpgrp(script_fds[SFD_SLAVE], child); /* foreground process, grant it controlling tty. */
} while (status == -1 && errno == EINTR); do {
/* FALLTHROUGH */ status = tcsetpgrp(script_fds[SFD_SLAVE], child);
case SIGUSR2: } while (status == -1 && errno == EINTR);
killpg(child, SIGCONT); /* FALLTHROUGH */
break; case SIGUSR2:
status = tcgetpgrp(script_fds[SFD_SLAVE]);
killpg(child, SIGCONT);
break;
}
} }
recvsig = 0;
pid = waitpid(child, &status, WUNTRACED); pid = waitpid(child, &status, WUNTRACED);
if (pid != child) { if (pid != child) {
@@ -812,11 +818,11 @@ script_child(path, argv, foreground, rbac_enabled)
break; break;
/* /*
* Child was stopped by signal, signal parent and stop self. * Child was stopped by signal, signal parent and wait for SIGUSR[12].
*/ */
signo = WSTOPSIG(status); signo = WSTOPSIG(status);
/* Take back controlling tty while child is suspended. */
do { do {
/* Take back controlling tty while child is suspended. */
status = tcsetpgrp(script_fds[SFD_SLAVE], self); status = tcsetpgrp(script_fds[SFD_SLAVE], self);
} while (status == -1 && errno == EINTR); } while (status == -1 && errno == EINTR);
kill(parent, signo); kill(parent, signo);
@@ -902,7 +908,7 @@ script_run(path, argv, rbac_enabled)
#ifdef HAVE_SELINUX #ifdef HAVE_SELINUX
if (rbac_enabled) if (rbac_enabled)
selinux_execv(path, argv); selinux_execv(path, argv);
else else
#endif #endif
execv(path, argv); execv(path, argv);
@@ -930,9 +936,9 @@ sync_winsize(src, dst)
/* /*
* Handler for SIGCONT in parent * Handler for SIGCONT in parent
*/ */
void static void
sigcont(s) sigcont(s)
int s; int s;
{ {
int serrno = errno; int serrno = errno;
@@ -989,16 +995,6 @@ sigrelay(s)
errno = serrno; errno = serrno;
} }
/*
* Signal handler for SIG{TSTP,TTOU,TTIN}
*/
static void
sigtstp(s)
int s;
{
recvsig = s;
}
static void static void
sigwinch(s) sigwinch(s)
int s; int s;