From 76def9a53192c9748808616eaf49f6a476385f17 Mon Sep 17 00:00:00 2001 From: Nathan Straz Date: Fri, 4 Oct 2013 12:21:34 -0400 Subject: Remove child signal handling Instead of moving the setup for SIGCHLD handling, I'm removing it. waitpid() isn't that expensive and we know the pid we are waiting for. --- qarshd.c | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/qarshd.c b/qarshd.c index dc53d21..255defe 100644 --- a/qarshd.c +++ b/qarshd.c @@ -53,7 +53,6 @@ int dopause = 0; /* Globals */ const int qinfd = 0; /* qarshd in file descriptor */ const int qoutfd = 1; /* qarshd out file descriptor */ -int child_exitted = 0; pid_t child_pid; sigset_t orig_sigmask; int childfds[3] = { -1, -1, -1 }; /* pipes to child for stdin/stdout/stderr */ @@ -103,7 +102,6 @@ setup_user(char *user, char *group) void sig_handler(int sig) { - if (sig == SIGCHLD) child_exitted++; } pid_t @@ -129,7 +127,6 @@ run_cmd(const char *cmd) if (pid == 0) { /* child */ setpgrp(); - sigprocmask(SIG_SETMASK, &orig_sigmask, NULL); /* Connect stdin, stdout, and stderr to parent's pipes */ dup2(parentfds[0], fileno(stdin)); @@ -240,9 +237,7 @@ handle_qarsh() { fd_set rfds, wfds; int nfd, maxfd; - struct timespec timeout; - sigset_t sigmask; - struct sigaction sa; + struct timeval timeout; int child_status; struct qa_packet *qp = NULL, *rp = NULL; int allowed_out = 0, allowed_err = 0; /* number of bytes we can send to client */ @@ -252,13 +247,6 @@ handle_qarsh() int eof_in = 0; /* Have we seen EOF on stdin yet? */ int nbytes; - sigemptyset(&sigmask); - sigaddset(&sigmask, SIGCHLD); - sigprocmask(SIG_BLOCK, &sigmask, &orig_sigmask); - sa.sa_handler = sig_handler; - sa.sa_mask = sigmask; - sa.sa_flags = SA_RESTART; - sigaction(SIGCHLD, &sa, NULL); signal(SIGPIPE, SIG_IGN); qp = make_qp_data_allow(0, QARSHD_BUFSIZE); @@ -268,7 +256,7 @@ handle_qarsh() for (;;) { timeout.tv_sec = 3; - timeout.tv_nsec = 0; + timeout.tv_usec = 0; FD_ZERO(&rfds); FD_ZERO(&wfds); FD_SET(qinfd, &rfds); maxfd = qinfd; @@ -285,15 +273,13 @@ handle_qarsh() maxfd = childfds[2] > maxfd ? childfds[2] : maxfd; } - nfd = pselect(maxfd+1, &rfds, &wfds, NULL, &timeout, &orig_sigmask); + nfd = select(maxfd+1, &rfds, &wfds, NULL, &timeout); - if (child_exitted) { - waitpid(child_pid, &child_status, 0); - child_exitted--; - child_pid = 0; + if (child_pid && waitpid(child_pid, &child_status, WNOHANG) == child_pid) { rp = make_qp_cmdexit(child_pid, child_status); send_packet(qoutfd, rp); qpfree(rp); + child_pid = 0; } if (nfd < 0) { if (errno == EINTR) { -- cgit