wait-process.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /* Waiting for a subprocess to finish.
  2. Copyright (C) 2001-2003, 2005-2013 Free Software Foundation, Inc.
  3. Written by Bruno Haible <haible@clisp.cons.org>, 2001.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. /* Specification. */
  16. #include "wait-process.h"
  17. #include <errno.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <signal.h>
  21. #include <sys/types.h>
  22. #include <sys/wait.h>
  23. #include "error.h"
  24. #include "fatal-signal.h"
  25. #include "xalloc.h"
  26. #include "gettext.h"
  27. #define _(str) gettext (str)
  28. #define SIZEOF(a) (sizeof(a) / sizeof(a[0]))
  29. #if defined _MSC_VER || defined __MINGW32__
  30. #define WIN32_LEAN_AND_MEAN
  31. #include <windows.h>
  32. /* The return value of spawnvp() is really a process handle as returned
  33. by CreateProcess(). Therefore we can kill it using TerminateProcess. */
  34. #define kill(pid,sig) TerminateProcess ((HANDLE) (pid), sig)
  35. #endif
  36. /* Type of an entry in the slaves array.
  37. The 'used' bit determines whether this entry is currently in use.
  38. (If pid_t was an atomic type like sig_atomic_t, we could just set the
  39. 'child' field to 0 when unregistering a slave process, and wouldn't need
  40. the 'used' field.)
  41. The 'used' and 'child' fields are accessed from within the cleanup_slaves()
  42. action, therefore we mark them as 'volatile'. */
  43. typedef struct
  44. {
  45. volatile sig_atomic_t used;
  46. volatile pid_t child;
  47. }
  48. slaves_entry_t;
  49. /* The registered slave subprocesses. */
  50. static slaves_entry_t static_slaves[32];
  51. static slaves_entry_t * volatile slaves = static_slaves;
  52. static sig_atomic_t volatile slaves_count = 0;
  53. static size_t slaves_allocated = SIZEOF (static_slaves);
  54. /* The termination signal for slave subprocesses.
  55. 2003-10-07: Terminator becomes Governator. */
  56. #ifdef SIGHUP
  57. # define TERMINATOR SIGHUP
  58. #else
  59. # define TERMINATOR SIGTERM
  60. #endif
  61. /* The cleanup action. It gets called asynchronously. */
  62. static void
  63. cleanup_slaves (void)
  64. {
  65. for (;;)
  66. {
  67. /* Get the last registered slave. */
  68. size_t n = slaves_count;
  69. if (n == 0)
  70. break;
  71. n--;
  72. slaves_count = n;
  73. /* Skip unused entries in the slaves array. */
  74. if (slaves[n].used)
  75. {
  76. pid_t slave = slaves[n].child;
  77. /* Kill the slave. */
  78. kill (slave, TERMINATOR);
  79. }
  80. }
  81. }
  82. /* Register a subprocess as being a slave process. This means that the
  83. subprocess will be terminated when its creator receives a catchable fatal
  84. signal or exits normally. Registration ends when wait_subprocess()
  85. notices that the subprocess has exited. */
  86. void
  87. register_slave_subprocess (pid_t child)
  88. {
  89. static bool cleanup_slaves_registered = false;
  90. if (!cleanup_slaves_registered)
  91. {
  92. atexit (cleanup_slaves);
  93. at_fatal_signal (cleanup_slaves);
  94. cleanup_slaves_registered = true;
  95. }
  96. /* Try to store the new slave in an unused entry of the slaves array. */
  97. {
  98. slaves_entry_t *s = slaves;
  99. slaves_entry_t *s_end = s + slaves_count;
  100. for (; s < s_end; s++)
  101. if (!s->used)
  102. {
  103. /* The two uses of 'volatile' in the slaves_entry_t type above
  104. (and ISO C 99 section 5.1.2.3.(5)) ensure that we mark the
  105. entry as used only after the child pid has been written to the
  106. memory location s->child. */
  107. s->child = child;
  108. s->used = 1;
  109. return;
  110. }
  111. }
  112. if (slaves_count == slaves_allocated)
  113. {
  114. /* Extend the slaves array. Note that we cannot use xrealloc(),
  115. because then the cleanup_slaves() function could access an already
  116. deallocated array. */
  117. slaves_entry_t *old_slaves = slaves;
  118. size_t new_slaves_allocated = 2 * slaves_allocated;
  119. slaves_entry_t *new_slaves =
  120. (slaves_entry_t *)
  121. malloc (new_slaves_allocated * sizeof (slaves_entry_t));
  122. if (new_slaves == NULL)
  123. {
  124. /* xalloc_die() will call exit() which will invoke cleanup_slaves().
  125. Additionally we need to kill child, because it's not yet among
  126. the slaves list. */
  127. kill (child, TERMINATOR);
  128. xalloc_die ();
  129. }
  130. memcpy (new_slaves, old_slaves,
  131. slaves_allocated * sizeof (slaves_entry_t));
  132. slaves = new_slaves;
  133. slaves_allocated = new_slaves_allocated;
  134. /* Now we can free the old slaves array. */
  135. if (old_slaves != static_slaves)
  136. free (old_slaves);
  137. }
  138. /* The three uses of 'volatile' in the types above (and ISO C 99 section
  139. 5.1.2.3.(5)) ensure that we increment the slaves_count only after the
  140. new slave and its 'used' bit have been written to the memory locations
  141. that make up slaves[slaves_count]. */
  142. slaves[slaves_count].child = child;
  143. slaves[slaves_count].used = 1;
  144. slaves_count++;
  145. }
  146. /* Unregister a child from the list of slave subprocesses. */
  147. static void
  148. unregister_slave_subprocess (pid_t child)
  149. {
  150. /* The easiest way to remove an entry from a list that can be used by
  151. an asynchronous signal handler is just to mark it as unused. For this,
  152. we rely on sig_atomic_t. */
  153. slaves_entry_t *s = slaves;
  154. slaves_entry_t *s_end = s + slaves_count;
  155. for (; s < s_end; s++)
  156. if (s->used && s->child == child)
  157. s->used = 0;
  158. }
  159. /* Wait for a subprocess to finish. Return its exit code.
  160. If it didn't terminate correctly, exit if exit_on_error is true, otherwise
  161. return 127. */
  162. int
  163. wait_subprocess (pid_t child, const char *progname,
  164. bool ignore_sigpipe, bool null_stderr,
  165. bool slave_process, bool exit_on_error,
  166. int *termsigp)
  167. {
  168. #if HAVE_WAITID && defined WNOWAIT && 0
  169. /* Commented out because waitid() without WEXITED and with WNOWAIT doesn't
  170. work: On Solaris 7 and OSF/1 4.0, it returns -1 and sets errno = ECHILD,
  171. and on HP-UX 10.20 it just hangs. */
  172. /* Use of waitid() with WNOWAIT avoids a race condition: If slave_process is
  173. true, and this process sleeps a very long time between the return from
  174. waitpid() and the execution of unregister_slave_subprocess(), and
  175. meanwhile another process acquires the same PID as child, and then - still
  176. before unregister_slave_subprocess() - this process gets a fatal signal,
  177. it would kill the other totally unrelated process. */
  178. siginfo_t info;
  179. if (termsigp != NULL)
  180. *termsigp = 0;
  181. for (;;)
  182. {
  183. if (waitid (P_PID, child, &info, WEXITED | (slave_process ? WNOWAIT : 0))
  184. < 0)
  185. {
  186. # ifdef EINTR
  187. if (errno == EINTR)
  188. continue;
  189. # endif
  190. if (exit_on_error || !null_stderr)
  191. error (exit_on_error ? EXIT_FAILURE : 0, errno,
  192. _("%s subprocess"), progname);
  193. return 127;
  194. }
  195. /* info.si_code is set to one of CLD_EXITED, CLD_KILLED, CLD_DUMPED,
  196. CLD_TRAPPED, CLD_STOPPED, CLD_CONTINUED. Loop until the program
  197. terminates. */
  198. if (info.si_code == CLD_EXITED
  199. || info.si_code == CLD_KILLED || info.si_code == CLD_DUMPED)
  200. break;
  201. }
  202. /* The child process has exited or was signalled. */
  203. if (slave_process)
  204. {
  205. /* Unregister the child from the list of slave subprocesses, so that
  206. later, when we exit, we don't kill a totally unrelated process which
  207. may have acquired the same pid. */
  208. unregister_slave_subprocess (child);
  209. /* Now remove the zombie from the process list. */
  210. for (;;)
  211. {
  212. if (waitid (P_PID, child, &info, WEXITED) < 0)
  213. {
  214. # ifdef EINTR
  215. if (errno == EINTR)
  216. continue;
  217. # endif
  218. if (exit_on_error || !null_stderr)
  219. error (exit_on_error ? EXIT_FAILURE : 0, errno,
  220. _("%s subprocess"), progname);
  221. return 127;
  222. }
  223. break;
  224. }
  225. }
  226. switch (info.si_code)
  227. {
  228. case CLD_KILLED:
  229. case CLD_DUMPED:
  230. if (termsigp != NULL)
  231. *termsigp = info.si_status; /* TODO: or info.si_signo? */
  232. # ifdef SIGPIPE
  233. if (info.si_status == SIGPIPE && ignore_sigpipe)
  234. return 0;
  235. # endif
  236. if (exit_on_error || (!null_stderr && termsigp == NULL))
  237. error (exit_on_error ? EXIT_FAILURE : 0, 0,
  238. _("%s subprocess got fatal signal %d"),
  239. progname, info.si_status);
  240. return 127;
  241. case CLD_EXITED:
  242. if (info.si_status == 127)
  243. {
  244. if (exit_on_error || !null_stderr)
  245. error (exit_on_error ? EXIT_FAILURE : 0, 0,
  246. _("%s subprocess failed"), progname);
  247. return 127;
  248. }
  249. return info.si_status;
  250. default:
  251. abort ();
  252. }
  253. #else
  254. /* waitpid() is just as portable as wait() nowadays. */
  255. int status;
  256. if (termsigp != NULL)
  257. *termsigp = 0;
  258. status = 0;
  259. for (;;)
  260. {
  261. int result = waitpid (child, &status, 0);
  262. if (result != child)
  263. {
  264. # ifdef EINTR
  265. if (errno == EINTR)
  266. continue;
  267. # endif
  268. # if 0 /* defined ECHILD */
  269. if (errno == ECHILD)
  270. {
  271. /* Child process nonexistent?! Assume it terminated
  272. successfully. */
  273. status = 0;
  274. break;
  275. }
  276. # endif
  277. if (exit_on_error || !null_stderr)
  278. error (exit_on_error ? EXIT_FAILURE : 0, errno,
  279. _("%s subprocess"), progname);
  280. return 127;
  281. }
  282. /* One of WIFSIGNALED (status), WIFEXITED (status), WIFSTOPPED (status)
  283. must always be true, since we did not specify WCONTINUED in the
  284. waitpid() call. Loop until the program terminates. */
  285. if (!WIFSTOPPED (status))
  286. break;
  287. }
  288. /* The child process has exited or was signalled. */
  289. if (slave_process)
  290. /* Unregister the child from the list of slave subprocesses, so that
  291. later, when we exit, we don't kill a totally unrelated process which
  292. may have acquired the same pid. */
  293. unregister_slave_subprocess (child);
  294. if (WIFSIGNALED (status))
  295. {
  296. if (termsigp != NULL)
  297. *termsigp = WTERMSIG (status);
  298. # ifdef SIGPIPE
  299. if (WTERMSIG (status) == SIGPIPE && ignore_sigpipe)
  300. return 0;
  301. # endif
  302. if (exit_on_error || (!null_stderr && termsigp == NULL))
  303. error (exit_on_error ? EXIT_FAILURE : 0, 0,
  304. _("%s subprocess got fatal signal %d"),
  305. progname, (int) WTERMSIG (status));
  306. return 127;
  307. }
  308. if (!WIFEXITED (status))
  309. abort ();
  310. if (WEXITSTATUS (status) == 127)
  311. {
  312. if (exit_on_error || !null_stderr)
  313. error (exit_on_error ? EXIT_FAILURE : 0, 0,
  314. _("%s subprocess failed"), progname);
  315. return 127;
  316. }
  317. return WEXITSTATUS (status);
  318. #endif
  319. }