util_process.c 4.1 KB

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