catch-sigchld 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. --- tcp_wrappers_7.6/shell_cmd.c.sigchld 1994-12-28 17:42:44.000000000 +0100
  2. +++ tcp_wrappers_7.6/shell_cmd.c 2007-06-28 15:42:17.000000000 +0200
  3. @@ -20,6 +20,11 @@
  4. #include <stdio.h>
  5. #include <syslog.h>
  6. #include <string.h>
  7. +#include <errno.h>
  8. +#include <unistd.h>
  9. +#include <sys/wait.h>
  10. +#include <sys/stat.h>
  11. +#include <fcntl.h>
  12. extern void exit();
  13. @@ -31,13 +36,42 @@
  14. static void do_child();
  15. +/*
  16. + * The sigchld handler. If there is a SIGCHLD caused by a child other than
  17. + * ours, we set a flag and raise the signal later.
  18. + */
  19. +volatile static int foreign_sigchld;
  20. +volatile static int our_child_pid;
  21. +static void sigchld(int sig, siginfo_t *si, void *unused)
  22. +{
  23. + if (si && si->si_pid != our_child_pid)
  24. + foreign_sigchld = 1;
  25. +}
  26. +
  27. /* shell_cmd - execute shell command */
  28. void shell_cmd(command)
  29. char *command;
  30. {
  31. int child_pid;
  32. - int wait_pid;
  33. +
  34. + struct sigaction new_action, old_action;
  35. + sigset_t new_mask, old_mask, empty_mask;
  36. +
  37. + new_action.sa_sigaction = &sigchld;
  38. + new_action.sa_flags = SA_SIGINFO;
  39. + sigemptyset(&new_action.sa_mask);
  40. + sigemptyset(&new_mask);
  41. + sigemptyset(&empty_mask);
  42. + sigaddset(&new_mask, SIGCHLD);
  43. +
  44. + /*
  45. + * Set the variables for handler, set the handler and block the signal
  46. + * until we have the pid.
  47. + */
  48. + foreign_sigchld = 0; our_child_pid = 0;
  49. + sigprocmask(SIG_BLOCK, &new_mask, &old_mask);
  50. + sigaction(SIGCHLD, &new_action, &old_action);
  51. /*
  52. * Most of the work is done within the child process, to minimize the
  53. @@ -49,12 +83,26 @@
  54. tcpd_warn("cannot fork: %m");
  55. break;
  56. case 00: /* child */
  57. + /* Clear the blocked mask for the child not to be surprised. */
  58. + sigprocmask(SIG_SETMASK, &empty_mask, 0);
  59. do_child(command);
  60. /* NOTREACHED */
  61. default: /* parent */
  62. - while ((wait_pid = wait((int *) 0)) != -1 && wait_pid != child_pid)
  63. - /* void */ ;
  64. + our_child_pid = child_pid;
  65. + sigprocmask(SIG_UNBLOCK, &new_mask, 0);
  66. + while (waitpid(child_pid, (int *) 0, 0) == -1 && errno == EINTR);
  67. }
  68. +
  69. + /*
  70. + * Revert the signal mask and the SIGCHLD handler.
  71. + */
  72. + sigprocmask(SIG_SETMASK, &old_mask, 0);
  73. + sigaction(SIGCHLD, &old_action, 0);
  74. +
  75. + /* If there was a foreign SIGCHLD, raise it after we have restored the old
  76. + * mask and handler. */
  77. + if (foreign_sigchld)
  78. + raise(SIGCHLD);
  79. }
  80. /* do_child - exec command with { stdin, stdout, stderr } to /dev/null */