1 - Unblock and catch SIGCHLD from spawned shell commands
2 --- tcp_wrappers_7.6/shell_cmd.c.sigchld 1994-12-28 17:42:44.000000000 +0100
3 +++ tcp_wrappers_7.6/shell_cmd.c 2007-06-28 15:42:17.000000000 +0200
10 +#include <sys/wait.h>
11 +#include <sys/stat.h>
18 static void do_child();
21 + * The sigchld handler. If there is a SIGCHLD caused by a child other than
22 + * ours, we set a flag and raise the signal later.
24 +volatile static int foreign_sigchld;
25 +volatile static int our_child_pid;
26 +static void sigchld(int sig, siginfo_t *si, void *unused)
28 + if (si && si->si_pid != our_child_pid)
29 + foreign_sigchld = 1;
32 /* shell_cmd - execute shell command */
34 void shell_cmd(command)
40 + struct sigaction new_action, old_action;
41 + sigset_t new_mask, old_mask, empty_mask;
43 + new_action.sa_sigaction = &sigchld;
44 + new_action.sa_flags = SA_SIGINFO;
45 + sigemptyset(&new_action.sa_mask);
46 + sigemptyset(&new_mask);
47 + sigemptyset(&empty_mask);
48 + sigaddset(&new_mask, SIGCHLD);
51 + * Set the variables for handler, set the handler and block the signal
52 + * until we have the pid.
54 + foreign_sigchld = 0; our_child_pid = 0;
55 + sigprocmask(SIG_BLOCK, &new_mask, &old_mask);
56 + sigaction(SIGCHLD, &new_action, &old_action);
59 * Most of the work is done within the child process, to minimize the
61 tcpd_warn("cannot fork: %m");
64 + /* Clear the blocked mask for the child not to be surprised. */
65 + sigprocmask(SIG_SETMASK, &empty_mask, 0);
69 - while ((wait_pid = wait((int *) 0)) != -1 && wait_pid != child_pid)
71 + our_child_pid = child_pid;
72 + sigprocmask(SIG_UNBLOCK, &new_mask, 0);
73 + while (waitpid(child_pid, (int *) 0, 0) == -1 && errno == EINTR);
77 + * Revert the signal mask and the SIGCHLD handler.
79 + sigprocmask(SIG_SETMASK, &old_mask, 0);
80 + sigaction(SIGCHLD, &old_action, 0);
82 + /* If there was a foreign SIGCHLD, raise it after we have restored the old
83 + * mask and handler. */
84 + if (foreign_sigchld)
88 /* do_child - exec command with { stdin, stdout, stderr } to /dev/null */