waitpid.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file util_process.c
  7. * \brief utility functions for launching processes and checking their
  8. * status. These functions are kept separately from procmon so that they
  9. * won't require linking against libevent.
  10. **/
  11. #include "orconfig.h"
  12. #ifndef _WIN32
  13. #include "lib/process/waitpid.h"
  14. #include "lib/log/torlog.h"
  15. #include "lib/log/util_bug.h"
  16. #include "lib/malloc/util_malloc.h"
  17. #include "ht.h"
  18. #ifdef HAVE_SYS_WAIT_H
  19. #include <sys/wait.h>
  20. #endif
  21. #include <string.h>
  22. /* ================================================== */
  23. /* Convenience structures for handlers for waitpid().
  24. *
  25. * The tor_process_monitor*() code above doesn't use them, since it is for
  26. * monitoring a non-child process.
  27. */
  28. /** Mapping from a PID to a userfn/userdata pair. */
  29. struct waitpid_callback_t {
  30. HT_ENTRY(waitpid_callback_t) node;
  31. pid_t pid;
  32. void (*userfn)(int, void *userdata);
  33. void *userdata;
  34. unsigned running;
  35. };
  36. static inline unsigned int
  37. process_map_entry_hash_(const waitpid_callback_t *ent)
  38. {
  39. return (unsigned) ent->pid;
  40. }
  41. static inline unsigned int
  42. process_map_entries_eq_(const waitpid_callback_t *a,
  43. const waitpid_callback_t *b)
  44. {
  45. return a->pid == b->pid;
  46. }
  47. static HT_HEAD(process_map, waitpid_callback_t) process_map = HT_INITIALIZER();
  48. HT_PROTOTYPE(process_map, waitpid_callback_t, node, process_map_entry_hash_,
  49. process_map_entries_eq_)
  50. HT_GENERATE2(process_map, waitpid_callback_t, node, process_map_entry_hash_,
  51. process_map_entries_eq_, 0.6, tor_reallocarray_, tor_free_)
  52. /**
  53. * Begin monitoring the child pid <b>pid</b> to see if we get a SIGCHLD for
  54. * it. If we eventually do, call <b>fn</b>, passing it the exit status (as
  55. * yielded by waitpid) and the pointer <b>arg</b>.
  56. *
  57. * To cancel this, or clean up after it has triggered, call
  58. * clear_waitpid_callback().
  59. */
  60. waitpid_callback_t *
  61. set_waitpid_callback(pid_t pid, void (*fn)(int, void *), void *arg)
  62. {
  63. waitpid_callback_t *old_ent;
  64. waitpid_callback_t *ent = tor_malloc_zero(sizeof(waitpid_callback_t));
  65. ent->pid = pid;
  66. ent->userfn = fn;
  67. ent->userdata = arg;
  68. ent->running = 1;
  69. old_ent = HT_REPLACE(process_map, &process_map, ent);
  70. if (old_ent) {
  71. log_warn(LD_BUG, "Replaced a waitpid monitor on pid %u. That should be "
  72. "impossible.", (unsigned) pid);
  73. old_ent->running = 0;
  74. }
  75. return ent;
  76. }
  77. /**
  78. * Cancel a waitpid_callback_t, or clean up after one has triggered. Releases
  79. * all storage held by <b>ent</b>.
  80. */
  81. void
  82. clear_waitpid_callback(waitpid_callback_t *ent)
  83. {
  84. waitpid_callback_t *old_ent;
  85. if (ent == NULL)
  86. return;
  87. if (ent->running) {
  88. old_ent = HT_REMOVE(process_map, &process_map, ent);
  89. if (old_ent != ent) {
  90. log_warn(LD_BUG, "Couldn't remove waitpid monitor for pid %u.",
  91. (unsigned) ent->pid);
  92. return;
  93. }
  94. }
  95. tor_free(ent);
  96. }
  97. /** Helper: find the callack for <b>pid</b>; if there is one, run it,
  98. * reporting the exit status as <b>status</b>. */
  99. static void
  100. notify_waitpid_callback_by_pid(pid_t pid, int status)
  101. {
  102. waitpid_callback_t search, *ent;
  103. search.pid = pid;
  104. ent = HT_REMOVE(process_map, &process_map, &search);
  105. if (!ent || !ent->running) {
  106. log_info(LD_GENERAL, "Child process %u has exited; no callback was "
  107. "registered", (unsigned)pid);
  108. return;
  109. }
  110. log_info(LD_GENERAL, "Child process %u has exited; running callback.",
  111. (unsigned)pid);
  112. ent->running = 0;
  113. ent->userfn(status, ent->userdata);
  114. }
  115. /** Use waitpid() to wait for all children that have exited, and invoke any
  116. * callbacks registered for them. */
  117. void
  118. notify_pending_waitpid_callbacks(void)
  119. {
  120. /* I was going to call this function reap_zombie_children(), but
  121. * that makes it sound way more exciting than it really is. */
  122. pid_t child;
  123. int status = 0;
  124. while ((child = waitpid(-1, &status, WNOHANG)) > 0) {
  125. notify_waitpid_callback_by_pid(child, status);
  126. status = 0; /* should be needless */
  127. }
  128. }
  129. #endif /* !defined(_WIN32) */