Blame SOURCES/tcp_wrappers-7.6-sigchld.patch

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