-#if defined(HAVE_WAIT3)
-#ifdef oldhpux /* HP-UX fixed this sometime between 9.01 and 10.20 */
- while ((pid = wait3(&status, WNOHANG, (int *) 0)) > 0)
-#else
- while ((pid = wait3(&status, WNOHANG, (struct rusage *) 0)) > 0)
-#endif
- ; /* swallow 'em up. */
-#elif defined(HAVE_WAITPID)
- while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
- ; /* swallow 'em up. */
-#else /* Zooks! Nothing to do but wait(), and hope we don't block... */
- wait(&status);
+void null_signal_handler(int sig) { (void)sig; }
+
+SIGHANDLERTYPE set_signal_handler(int sig, SIGHANDLERTYPE handler)
+/*
+ * This function is called by other parts of the program to
+ * setup the signal handler after a change to the signal context.
+ * This is done to improve robustness of the signal handling code.
+ * It has the same prototype as signal(2).
+ */
+{
+ SIGHANDLERTYPE rethandler;
+ struct sigaction sa_new, sa_old;
+
+ memset (&sa_new, 0, sizeof sa_new);
+ sigemptyset (&sa_new.sa_mask);
+ sa_new.sa_handler = handler;
+ sa_new.sa_flags = 0;
+ /* system call should restart on all signals except SIGALRM */
+ if (sig != SIGALRM)
+ sa_new.sa_flags |= SA_RESTART;
+ if (sig == SIGCHLD)
+ sa_new.sa_flags |= SA_NOCLDSTOP;
+ sigaction(sig, &sa_new, &sa_old);
+ rethandler = sa_old.sa_handler;
+#if defined(SIGPWR)
+ if (sig == SIGCHLD)
+ sigaction(SIGPWR, &sa_new, NULL);