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,11 +784,13 @@ 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)) {
recvsig = 0;
switch (signo) {
case SIGHUP: case SIGHUP:
case SIGTERM: case SIGTERM:
/* signal child; we'll get its status when we continue. */ /* signal child; we'll get its status when we continue. */
killpg(child, recvsig); killpg(child, signo);
break; break;
case SIGUSR1: case SIGUSR1:
/* foreground process, grant it controlling tty. */ /* foreground process, grant it controlling tty. */
@@ -794,10 +799,11 @@ script_child(path, argv, foreground, rbac_enabled)
} while (status == -1 && errno == EINTR); } while (status == -1 && errno == EINTR);
/* FALLTHROUGH */ /* FALLTHROUGH */
case SIGUSR2: case SIGUSR2:
status = tcgetpgrp(script_fds[SFD_SLAVE]);
killpg(child, SIGCONT); killpg(child, SIGCONT);
break; 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);
do {
/* Take back controlling tty while child is suspended. */ /* Take back controlling tty while child is suspended. */
do {
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);
@@ -930,7 +936,7 @@ 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;
{ {
@@ -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;