Reap dead children as they arrive. The previous shell may have
authormdw <mdw>
Wed, 18 Nov 1998 21:25:06 +0000 (21:25 +0000)
committermdw <mdw>
Wed, 18 Nov 1998 21:25:06 +0000 (21:25 +0000)
carelessly left them behind.

xwait.c

diff --git a/xwait.c b/xwait.c
index 7142217..45bb426 100644 (file)
--- a/xwait.c
+++ b/xwait.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: xwait.c,v 1.1 1998/11/16 23:00:49 mdw Exp $
+ * $Id: xwait.c,v 1.2 1998/11/18 21:25:06 mdw Exp $
  *
  * Wait until prodded by another X client
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: xwait.c,v $
+ * Revision 1.2  1998/11/18 21:25:06  mdw
+ * Reap dead children as they arrive.  The previous shell may have
+ * carelessly left them behind.
+ *
  * Revision 1.1  1998/11/16 23:00:49  mdw
  * Initial versions.
  *
 
 /*----- Header files ------------------------------------------------------*/
 
+#include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
+#include <sys/types.h>
+#include <sys/wait.h>
 #include <unistd.h>
 
 #include <X11/Xlib.h>
 
 /*----- Main code ---------------------------------------------------------*/
 
+/* --- @sigchld@ --- */
+
+static void sigchld(int sig)
+{
+  while (waitpid(-1, 0, WNOHANG) > 0)
+    ;
+}
+
+/* --- @main@ --- */
+
 int main(int argc, char *argv[])
 {
   char *display = 0;
@@ -135,6 +152,23 @@ int main(int argc, char *argv[])
     }
   }
 
+  /* --- Set up a handler when children die --- *
+   *
+   * I don't fork any children?  Why is this useful?  Because I've been
+   * execed from a shell which started lots of background processes, and
+   * they'll zombie themselves otherwise.
+   */
+
+  {
+    struct sigaction sa;
+
+    sa.sa_handler = sigchld;
+    sigemptyset(&sa.sa_mask);
+    sigaddset(&sa.sa_mask, SIGCHLD);
+    sa.sa_flags = 0;
+    sigaction(SIGCHLD, &sa, 0);
+  }
+
   /* --- Now wait for an event --- */
 
   for (;;) {