e615632
diff -up tcp_wrappers_7.6/shell_cmd.c.patch20 tcp_wrappers_7.6/shell_cmd.c
e615632
--- tcp_wrappers_7.6/shell_cmd.c.patch20	1994-12-28 17:42:44.000000000 +0100
e615632
+++ tcp_wrappers_7.6/shell_cmd.c	2008-08-29 09:45:12.000000000 +0200
e615632
@@ -20,6 +20,11 @@ static char sccsid[] = "@(#) shell_cmd.c
Tomas Janousek 7e27ade
 #include <stdio.h>
Tomas Janousek 7e27ade
 #include <syslog.h>
Tomas Janousek 7e27ade
 #include <string.h>
Tomas Janousek 7e27ade
+#include <errno.h>
Tomas Janousek 7e27ade
+#include <unistd.h>
Tomas Janousek 38552a4
+#include <sys/wait.h>
Tomas Janousek 38552a4
+#include <sys/stat.h>
Tomas Janousek 38552a4
+#include <fcntl.h>
Tomas Janousek 7e27ade
 
Tomas Janousek 7e27ade
 extern void exit();
Tomas Janousek 7e27ade
 
e615632
@@ -31,13 +36,42 @@ extern void exit();
Tomas Janousek f7f9316
 
Tomas Janousek f7f9316
 static void do_child();
Tomas Janousek f7f9316
 
Tomas Janousek 7e27ade
+/*
Tomas Janousek 7e27ade
+ * The sigchld handler. If there is a SIGCHLD caused by a child other than
Tomas Janousek 7e27ade
+ * ours, we set a flag and raise the signal later.
Tomas Janousek 7e27ade
+ */
Tomas Janousek 7e27ade
+volatile static int foreign_sigchld;
Tomas Janousek 7e27ade
+volatile static int our_child_pid;
Tomas Janousek 7e27ade
+static void sigchld(int sig, siginfo_t *si, void *unused)
Tomas Janousek 7e27ade
+{
Tomas Janousek 7e27ade
+    if (si && si->si_pid != our_child_pid)
Tomas Janousek 7e27ade
+	foreign_sigchld = 1;
Tomas Janousek 7e27ade
+}
Tomas Janousek f7f9316
+
Tomas Janousek f7f9316
 /* shell_cmd - execute shell command */
Tomas Janousek f7f9316
 
Tomas Janousek f7f9316
 void    shell_cmd(command)
Tomas Janousek 7e27ade
 char   *command;
Tomas Janousek 7e27ade
 {
Tomas Janousek f7f9316
     int     child_pid;
Tomas Janousek 7e27ade
-    int     wait_pid;
Tomas Janousek 7e27ade
+
Tomas Janousek f7f9316
+    struct sigaction new_action, old_action;
Tomas Janousek 7e27ade
+    sigset_t new_mask, old_mask, empty_mask;
Tomas Janousek f7f9316
+
Tomas Janousek 7e27ade
+    new_action.sa_sigaction = &sigchld;
Tomas Janousek 7e27ade
+    new_action.sa_flags = SA_SIGINFO;
Tomas Janousek f7f9316
+    sigemptyset(&new_action.sa_mask);
Tomas Janousek f7f9316
+    sigemptyset(&new_mask);
Tomas Janousek 7e27ade
+    sigemptyset(&empty_mask);
Tomas Janousek f7f9316
+    sigaddset(&new_mask, SIGCHLD);
Tomas Janousek f7f9316
+
Tomas Janousek f7f9316
+    /*
Tomas Janousek 7e27ade
+     * Set the variables for handler, set the handler and block the signal
Tomas Janousek 7e27ade
+     * until we have the pid.
Tomas Janousek f7f9316
+     */
Tomas Janousek 7e27ade
+    foreign_sigchld = 0; our_child_pid = 0;
Tomas Janousek 7e27ade
+    sigprocmask(SIG_BLOCK, &new_mask, &old_mask);
Tomas Janousek f7f9316
+    sigaction(SIGCHLD, &new_action, &old_action);
Tomas Janousek 7e27ade
 
Tomas Janousek f7f9316
     /*
Tomas Janousek f7f9316
      * Most of the work is done within the child process, to minimize the
e615632
@@ -49,12 +83,26 @@ char   *command;
Tomas Janousek 7e27ade
 	tcpd_warn("cannot fork: %m");
Tomas Janousek 7e27ade
 	break;
Tomas Janousek 7e27ade
     case 00:					/* child */
Tomas Janousek 7e27ade
+	/* Clear the blocked mask for the child not to be surprised. */
Tomas Janousek 7e27ade
+	sigprocmask(SIG_SETMASK, &empty_mask, 0);
Tomas Janousek 7e27ade
 	do_child(command);
Tomas Janousek 7e27ade
 	/* NOTREACHED */
Tomas Janousek 7e27ade
     default:					/* parent */
Tomas Janousek 7e27ade
-	while ((wait_pid = wait((int *) 0)) != -1 && wait_pid != child_pid)
Tomas Janousek 7e27ade
-	     /* void */ ;
Tomas Janousek 7e27ade
+	our_child_pid = child_pid;
Tomas Janousek 7e27ade
+	sigprocmask(SIG_UNBLOCK, &new_mask, 0);
Tomas Janousek 7e27ade
+	while (waitpid(child_pid, (int *) 0, 0) == -1 && errno == EINTR);
Tomas Janousek f7f9316
     }
Tomas Janousek f7f9316
+
Tomas Janousek f7f9316
+    /*
Tomas Janousek f7f9316
+     * Revert the signal mask and the SIGCHLD handler.
Tomas Janousek f7f9316
+     */
Tomas Janousek f7f9316
+    sigprocmask(SIG_SETMASK, &old_mask, 0);
Tomas Janousek f7f9316
+    sigaction(SIGCHLD, &old_action, 0);
Tomas Janousek 7e27ade
+
Tomas Janousek 7e27ade
+    /* If there was a foreign SIGCHLD, raise it after we have restored the old
Tomas Janousek 7e27ade
+     * mask and handler. */
Tomas Janousek 7e27ade
+    if (foreign_sigchld)
Tomas Janousek 7e27ade
+	raise(SIGCHLD);
Tomas Janousek f7f9316
 }
Tomas Janousek f7f9316
 
Tomas Janousek f7f9316
 /* do_child - exec command with { stdin, stdout, stderr } to /dev/null */