wait-process.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* Waiting for a subprocess to finish.
  2. Copyright (C) 2001-2003, 2005-2020 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 <https://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 _WIN32 && ! defined __CYGWIN__
  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 _GL_ASYNC_SAFE 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. /* The cleanup action, taking a signal argument.
  83. It gets called asynchronously. */
  84. static _GL_ASYNC_SAFE void
  85. cleanup_slaves_action (int sig _GL_UNUSED)
  86. {
  87. cleanup_slaves ();
  88. }
  89. /* Register a subprocess as being a slave process. This means that the
  90. subprocess will be terminated when its creator receives a catchable fatal
  91. signal or exits normally. Registration ends when wait_subprocess()
  92. notices that the subprocess has exited. */
  93. void
  94. register_slave_subprocess (pid_t child)
  95. {
  96. static bool cleanup_slaves_registered = false;
  97. if (!cleanup_slaves_registered)
  98. {
  99. atexit (cleanup_slaves);
  100. at_fatal_signal (cleanup_slaves_action);
  101. cleanup_slaves_registered = true;
  102. }
  103. /* Try to store the new slave in an unused entry of the slaves array. */
  104. {
  105. slaves_entry_t *s = slaves;
  106. slaves_entry_t *s_end = s + slaves_count;
  107. for (; s < s_end; s++)
  108. if (!s->used)
  109. {
  110. /* The two uses of 'volatile' in the slaves_entry_t type above
  111. (and ISO C 99 section 5.1.2.3.(5)) ensure that we mark the
  112. entry as used only after the child pid has been written to the
  113. memory location s->child. */
  114. s->child = child;
  115. s->used = 1;
  116. return;
  117. }
  118. }
  119. if (slaves_count == slaves_allocated)
  120. {
  121. /* Extend the slaves array. Note that we cannot use xrealloc(),
  122. because then the cleanup_slaves() function could access an already
  123. deallocated array. */
  124. slaves_entry_t *old_slaves = slaves;
  125. size_t new_slaves_allocated = 2 * slaves_allocated;
  126. slaves_entry_t *new_slaves =
  127. (slaves_entry_t *)
  128. malloc (new_slaves_allocated * sizeof (slaves_entry_t));
  129. if (new_slaves == NULL)
  130. {
  131. /* xalloc_die() will call exit() which will invoke cleanup_slaves().
  132. Additionally we need to kill child, because it's not yet among
  133. the slaves list. */
  134. kill (child, TERMINATOR);
  135. xalloc_die ();
  136. }
  137. memcpy (new_slaves, old_slaves,
  138. slaves_allocated * sizeof (slaves_entry_t));
  139. slaves = new_slaves;
  140. slaves_allocated = new_slaves_allocated;
  141. /* Now we can free the old slaves array. */
  142. if (old_slaves != static_slaves)
  143. free (old_slaves);
  144. }
  145. /* The three uses of 'volatile' in the types above (and ISO C 99 section
  146. 5.1.2.3.(5)) ensure that we increment the slaves_count only after the
  147. new slave and its 'used' bit have been written to the memory locations
  148. that make up slaves[slaves_count]. */
  149. slaves[slaves_count].child = child;
  150. slaves[slaves_count].used = 1;
  151. slaves_count++;
  152. }
  153. /* Unregister a child from the list of slave subprocesses. */
  154. static void
  155. unregister_slave_subprocess (pid_t child)
  156. {
  157. /* The easiest way to remove an entry from a list that can be used by
  158. an asynchronous signal handler is just to mark it as unused. For this,
  159. we rely on sig_atomic_t. */
  160. slaves_entry_t *s = slaves;
  161. slaves_entry_t *s_end = s + slaves_count;
  162. for (; s < s_end; s++)
  163. if (s->used && s->child == child)
  164. s->used = 0;
  165. }
  166. /* Wait for a subprocess to finish. Return its exit code.
  167. If it didn't terminate correctly, exit if exit_on_error is true, otherwise
  168. return 127. */
  169. int
  170. wait_subprocess (pid_t child, const char *progname,
  171. bool ignore_sigpipe, bool null_stderr,
  172. bool slave_process, bool exit_on_error,
  173. int *termsigp)
  174. {
  175. #if HAVE_WAITID && defined WNOWAIT && 0
  176. /* Commented out because waitid() without WEXITED and with WNOWAIT doesn't
  177. work: On Solaris 7 and OSF/1 4.0, it returns -1 and sets errno = ECHILD,
  178. and on HP-UX 10.20 it just hangs. */
  179. /* Use of waitid() with WNOWAIT avoids a race condition: If slave_process is
  180. true, and this process sleeps a very long time between the return from
  181. waitpid() and the execution of unregister_slave_subprocess(), and
  182. meanwhile another process acquires the same PID as child, and then - still
  183. before unregister_slave_subprocess() - this process gets a fatal signal,
  184. it would kill the other totally unrelated process. */
  185. siginfo_t info;
  186. if (termsigp != NULL)
  187. *termsigp = 0;
  188. for (;;)
  189. {
  190. if (waitid (P_PID, child, &info, WEXITED | (slave_process ? WNOWAIT : 0))
  191. < 0)
  192. {
  193. # ifdef EINTR
  194. if (errno == EINTR)
  195. continue;
  196. # endif
  197. if (exit_on_error || !null_stderr)
  198. error (exit_on_error ? EXIT_FAILURE : 0, errno,
  199. _("%s subprocess"), progname);
  200. return 127;
  201. }
  202. /* info.si_code is set to one of CLD_EXITED, CLD_KILLED, CLD_DUMPED,
  203. CLD_TRAPPED, CLD_STOPPED, CLD_CONTINUED. Loop until the program
  204. terminates. */
  205. if (info.si_code == CLD_EXITED
  206. || info.si_code == CLD_KILLED || info.si_code == CLD_DUMPED)
  207. break;
  208. }
  209. /* The child process has exited or was signalled. */
  210. if (slave_process)
  211. {
  212. /* Unregister the child from the list of slave subprocesses, so that
  213. later, when we exit, we don't kill a totally unrelated process which
  214. may have acquired the same pid. */
  215. unregister_slave_subprocess (child);
  216. /* Now remove the zombie from the process list. */
  217. for (;;)
  218. {
  219. if (waitid (P_PID, child, &info, WEXITED) < 0)
  220. {
  221. # ifdef EINTR
  222. if (errno == EINTR)
  223. continue;
  224. # endif
  225. if (exit_on_error || !null_stderr)
  226. error (exit_on_error ? EXIT_FAILURE : 0, errno,
  227. _("%s subprocess"), progname);
  228. return 127;
  229. }
  230. break;
  231. }
  232. }
  233. switch (info.si_code)
  234. {
  235. case CLD_KILLED:
  236. case CLD_DUMPED:
  237. if (termsigp != NULL)
  238. *termsigp = info.si_status; /* TODO: or info.si_signo? */
  239. # ifdef SIGPIPE
  240. if (info.si_status == SIGPIPE && ignore_sigpipe)
  241. return 0;
  242. # endif
  243. if (exit_on_error || (!null_stderr && termsigp == NULL))
  244. error (exit_on_error ? EXIT_FAILURE : 0, 0,
  245. _("%s subprocess got fatal signal %d"),
  246. progname, info.si_status);
  247. return 127;
  248. case CLD_EXITED:
  249. if (info.si_status == 127)
  250. {
  251. if (exit_on_error || !null_stderr)
  252. error (exit_on_error ? EXIT_FAILURE : 0, 0,
  253. _("%s subprocess failed"), progname);
  254. return 127;
  255. }
  256. return info.si_status;
  257. default:
  258. abort ();
  259. }
  260. #else
  261. /* waitpid() is just as portable as wait() nowadays. */
  262. int status;
  263. if (termsigp != NULL)
  264. *termsigp = 0;
  265. status = 0;
  266. for (;;)
  267. {
  268. int result = waitpid (child, &status, 0);
  269. if (result != child)
  270. {
  271. # ifdef EINTR
  272. if (errno == EINTR)
  273. continue;
  274. # endif
  275. # if 0 /* defined ECHILD */
  276. if (errno == ECHILD)
  277. {
  278. /* Child process nonexistent?! Assume it terminated
  279. successfully. */
  280. status = 0;
  281. break;
  282. }
  283. # endif
  284. if (exit_on_error || !null_stderr)
  285. error (exit_on_error ? EXIT_FAILURE : 0, errno,
  286. _("%s subprocess"), progname);
  287. return 127;
  288. }
  289. /* One of WIFSIGNALED (status), WIFEXITED (status), WIFSTOPPED (status)
  290. must always be true, since we did not specify WCONTINUED in the
  291. waitpid() call. Loop until the program terminates. */
  292. if (!WIFSTOPPED (status))
  293. break;
  294. }
  295. /* The child process has exited or was signalled. */
  296. if (slave_process)
  297. /* Unregister the child from the list of slave subprocesses, so that
  298. later, when we exit, we don't kill a totally unrelated process which
  299. may have acquired the same pid. */
  300. unregister_slave_subprocess (child);
  301. if (WIFSIGNALED (status))
  302. {
  303. if (termsigp != NULL)
  304. *termsigp = WTERMSIG (status);
  305. # ifdef SIGPIPE
  306. if (WTERMSIG (status) == SIGPIPE && ignore_sigpipe)
  307. return 0;
  308. # endif
  309. if (exit_on_error || (!null_stderr && termsigp == NULL))
  310. error (exit_on_error ? EXIT_FAILURE : 0, 0,
  311. _("%s subprocess got fatal signal %d"),
  312. progname, (int) WTERMSIG (status));
  313. return 127;
  314. }
  315. if (!WIFEXITED (status))
  316. abort ();
  317. if (WEXITSTATUS (status) == 127)
  318. {
  319. if (exit_on_error || !null_stderr)
  320. error (exit_on_error ? EXIT_FAILURE : 0, 0,
  321. _("%s subprocess failed"), progname);
  322. return 127;
  323. }
  324. return WEXITSTATUS (status);
  325. #endif
  326. }