pex-unix.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. /* Utilities to execute a program in a subprocess (possibly linked by pipes
  2. with other subprocesses), and wait for it. Generic Unix version
  3. (also used for UWIN and VMS).
  4. Copyright (C) 1996-2022 Free Software Foundation, Inc.
  5. This file is part of the libiberty library.
  6. Libiberty is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Library General Public
  8. License as published by the Free Software Foundation; either
  9. version 2 of the License, or (at your option) any later version.
  10. Libiberty is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with libiberty; see the file COPYING.LIB. If not,
  16. write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  17. Boston, MA 02110-1301, USA. */
  18. #include "config.h"
  19. #include "libiberty.h"
  20. #include "pex-common.h"
  21. #include "environ.h"
  22. #include <stdio.h>
  23. #include <signal.h>
  24. #include <errno.h>
  25. #ifdef NEED_DECLARATION_ERRNO
  26. extern int errno;
  27. #endif
  28. #ifdef HAVE_STDLIB_H
  29. #include <stdlib.h>
  30. #endif
  31. #ifdef HAVE_STRING_H
  32. #include <string.h>
  33. #endif
  34. #ifdef HAVE_UNISTD_H
  35. #include <unistd.h>
  36. #endif
  37. #include <sys/types.h>
  38. #ifdef HAVE_FCNTL_H
  39. #include <fcntl.h>
  40. #endif
  41. #ifdef HAVE_SYS_WAIT_H
  42. #include <sys/wait.h>
  43. #endif
  44. #ifdef HAVE_GETRUSAGE
  45. #include <sys/time.h>
  46. #include <sys/resource.h>
  47. #endif
  48. #ifdef HAVE_SYS_STAT_H
  49. #include <sys/stat.h>
  50. #endif
  51. #ifdef HAVE_PROCESS_H
  52. #include <process.h>
  53. #endif
  54. #ifdef vfork /* Autoconf may define this to fork for us. */
  55. # define VFORK_STRING "fork"
  56. #else
  57. # define VFORK_STRING "vfork"
  58. #endif
  59. #ifdef HAVE_VFORK_H
  60. #include <vfork.h>
  61. #endif
  62. #if defined(VMS) && defined (__LONG_POINTERS)
  63. #ifndef __CHAR_PTR32
  64. typedef char * __char_ptr32
  65. __attribute__ ((mode (SI)));
  66. #endif
  67. typedef __char_ptr32 *__char_ptr_char_ptr32
  68. __attribute__ ((mode (SI)));
  69. /* Return a 32 bit pointer to an array of 32 bit pointers
  70. given a 64 bit pointer to an array of 64 bit pointers. */
  71. static __char_ptr_char_ptr32
  72. to_ptr32 (char **ptr64)
  73. {
  74. int argc;
  75. __char_ptr_char_ptr32 short_argv;
  76. /* Count number of arguments. */
  77. for (argc = 0; ptr64[argc] != NULL; argc++)
  78. ;
  79. /* Reallocate argv with 32 bit pointers. */
  80. short_argv = (__char_ptr_char_ptr32) decc$malloc
  81. (sizeof (__char_ptr32) * (argc + 1));
  82. for (argc = 0; ptr64[argc] != NULL; argc++)
  83. short_argv[argc] = (__char_ptr32) decc$strdup (ptr64[argc]);
  84. short_argv[argc] = (__char_ptr32) 0;
  85. return short_argv;
  86. }
  87. #else
  88. #define to_ptr32(argv) argv
  89. #endif
  90. /* File mode to use for private and world-readable files. */
  91. #if defined (S_IRUSR) && defined (S_IWUSR) && defined (S_IRGRP) && defined (S_IWGRP) && defined (S_IROTH) && defined (S_IWOTH)
  92. #define PUBLIC_MODE \
  93. (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
  94. #else
  95. #define PUBLIC_MODE 0666
  96. #endif
  97. /* Get the exit status of a particular process, and optionally get the
  98. time that it took. This is simple if we have wait4, slightly
  99. harder if we have waitpid, and is a pain if we only have wait. */
  100. static pid_t pex_wait (struct pex_obj *, pid_t, int *, struct pex_time *);
  101. #ifdef HAVE_WAIT4
  102. static pid_t
  103. pex_wait (struct pex_obj *obj ATTRIBUTE_UNUSED, pid_t pid, int *status,
  104. struct pex_time *time)
  105. {
  106. pid_t ret;
  107. struct rusage r;
  108. #ifdef HAVE_WAITPID
  109. if (time == NULL)
  110. return waitpid (pid, status, 0);
  111. #endif
  112. ret = wait4 (pid, status, 0, &r);
  113. if (time != NULL)
  114. {
  115. time->user_seconds = r.ru_utime.tv_sec;
  116. time->user_microseconds= r.ru_utime.tv_usec;
  117. time->system_seconds = r.ru_stime.tv_sec;
  118. time->system_microseconds= r.ru_stime.tv_usec;
  119. }
  120. return ret;
  121. }
  122. #else /* ! defined (HAVE_WAIT4) */
  123. #ifdef HAVE_WAITPID
  124. #ifndef HAVE_GETRUSAGE
  125. static pid_t
  126. pex_wait (struct pex_obj *obj ATTRIBUTE_UNUSED, pid_t pid, int *status,
  127. struct pex_time *time)
  128. {
  129. if (time != NULL)
  130. memset (time, 0, sizeof (struct pex_time));
  131. return waitpid (pid, status, 0);
  132. }
  133. #else /* defined (HAVE_GETRUSAGE) */
  134. static pid_t
  135. pex_wait (struct pex_obj *obj ATTRIBUTE_UNUSED, pid_t pid, int *status,
  136. struct pex_time *time)
  137. {
  138. struct rusage r1, r2;
  139. pid_t ret;
  140. if (time == NULL)
  141. return waitpid (pid, status, 0);
  142. getrusage (RUSAGE_CHILDREN, &r1);
  143. ret = waitpid (pid, status, 0);
  144. if (ret < 0)
  145. return ret;
  146. getrusage (RUSAGE_CHILDREN, &r2);
  147. time->user_seconds = r2.ru_utime.tv_sec - r1.ru_utime.tv_sec;
  148. time->user_microseconds = r2.ru_utime.tv_usec - r1.ru_utime.tv_usec;
  149. if (r2.ru_utime.tv_usec < r1.ru_utime.tv_usec)
  150. {
  151. --time->user_seconds;
  152. time->user_microseconds += 1000000;
  153. }
  154. time->system_seconds = r2.ru_stime.tv_sec - r1.ru_stime.tv_sec;
  155. time->system_microseconds = r2.ru_stime.tv_usec - r1.ru_stime.tv_usec;
  156. if (r2.ru_stime.tv_usec < r1.ru_stime.tv_usec)
  157. {
  158. --time->system_seconds;
  159. time->system_microseconds += 1000000;
  160. }
  161. return ret;
  162. }
  163. #endif /* defined (HAVE_GETRUSAGE) */
  164. #else /* ! defined (HAVE_WAITPID) */
  165. struct status_list
  166. {
  167. struct status_list *next;
  168. pid_t pid;
  169. int status;
  170. struct pex_time time;
  171. };
  172. static pid_t
  173. pex_wait (struct pex_obj *obj, pid_t pid, int *status, struct pex_time *time)
  174. {
  175. struct status_list **pp;
  176. for (pp = (struct status_list **) &obj->sysdep;
  177. *pp != NULL;
  178. pp = &(*pp)->next)
  179. {
  180. if ((*pp)->pid == pid)
  181. {
  182. struct status_list *p;
  183. p = *pp;
  184. *status = p->status;
  185. if (time != NULL)
  186. *time = p->time;
  187. *pp = p->next;
  188. free (p);
  189. return pid;
  190. }
  191. }
  192. while (1)
  193. {
  194. pid_t cpid;
  195. struct status_list *psl;
  196. struct pex_time pt;
  197. #ifdef HAVE_GETRUSAGE
  198. struct rusage r1, r2;
  199. #endif
  200. if (time != NULL)
  201. {
  202. #ifdef HAVE_GETRUSAGE
  203. getrusage (RUSAGE_CHILDREN, &r1);
  204. #else
  205. memset (&pt, 0, sizeof (struct pex_time));
  206. #endif
  207. }
  208. cpid = wait (status);
  209. #ifdef HAVE_GETRUSAGE
  210. if (time != NULL && cpid >= 0)
  211. {
  212. getrusage (RUSAGE_CHILDREN, &r2);
  213. pt.user_seconds = r2.ru_utime.tv_sec - r1.ru_utime.tv_sec;
  214. pt.user_microseconds = r2.ru_utime.tv_usec - r1.ru_utime.tv_usec;
  215. if (pt.user_microseconds < 0)
  216. {
  217. --pt.user_seconds;
  218. pt.user_microseconds += 1000000;
  219. }
  220. pt.system_seconds = r2.ru_stime.tv_sec - r1.ru_stime.tv_sec;
  221. pt.system_microseconds = r2.ru_stime.tv_usec - r1.ru_stime.tv_usec;
  222. if (pt.system_microseconds < 0)
  223. {
  224. --pt.system_seconds;
  225. pt.system_microseconds += 1000000;
  226. }
  227. }
  228. #endif
  229. if (cpid < 0 || cpid == pid)
  230. {
  231. if (time != NULL)
  232. *time = pt;
  233. return cpid;
  234. }
  235. psl = XNEW (struct status_list);
  236. psl->pid = cpid;
  237. psl->status = *status;
  238. if (time != NULL)
  239. psl->time = pt;
  240. psl->next = (struct status_list *) obj->sysdep;
  241. obj->sysdep = (void *) psl;
  242. }
  243. }
  244. #endif /* ! defined (HAVE_WAITPID) */
  245. #endif /* ! defined (HAVE_WAIT4) */
  246. static int pex_unix_open_read (struct pex_obj *, const char *, int);
  247. static int pex_unix_open_write (struct pex_obj *, const char *, int, int);
  248. static pid_t pex_unix_exec_child (struct pex_obj *, int, const char *,
  249. char * const *, char * const *,
  250. int, int, int, int,
  251. const char **, int *);
  252. static int pex_unix_close (struct pex_obj *, int);
  253. static int pex_unix_wait (struct pex_obj *, pid_t, int *, struct pex_time *,
  254. int, const char **, int *);
  255. static int pex_unix_pipe (struct pex_obj *, int *, int);
  256. static FILE *pex_unix_fdopenr (struct pex_obj *, int, int);
  257. static FILE *pex_unix_fdopenw (struct pex_obj *, int, int);
  258. static void pex_unix_cleanup (struct pex_obj *);
  259. /* The list of functions we pass to the common routines. */
  260. const struct pex_funcs funcs =
  261. {
  262. pex_unix_open_read,
  263. pex_unix_open_write,
  264. pex_unix_exec_child,
  265. pex_unix_close,
  266. pex_unix_wait,
  267. pex_unix_pipe,
  268. pex_unix_fdopenr,
  269. pex_unix_fdopenw,
  270. pex_unix_cleanup
  271. };
  272. /* Return a newly initialized pex_obj structure. */
  273. struct pex_obj *
  274. pex_init (int flags, const char *pname, const char *tempbase)
  275. {
  276. return pex_init_common (flags, pname, tempbase, &funcs);
  277. }
  278. /* Open a file for reading. */
  279. static int
  280. pex_unix_open_read (struct pex_obj *obj ATTRIBUTE_UNUSED, const char *name,
  281. int binary ATTRIBUTE_UNUSED)
  282. {
  283. return open (name, O_RDONLY);
  284. }
  285. /* Open a file for writing. */
  286. static int
  287. pex_unix_open_write (struct pex_obj *obj ATTRIBUTE_UNUSED, const char *name,
  288. int binary ATTRIBUTE_UNUSED, int append)
  289. {
  290. /* Note that we can't use O_EXCL here because gcc may have already
  291. created the temporary file via make_temp_file. */
  292. return open (name, O_WRONLY | O_CREAT
  293. | (append ? O_APPEND : O_TRUNC), PUBLIC_MODE);
  294. }
  295. /* Close a file. */
  296. static int
  297. pex_unix_close (struct pex_obj *obj ATTRIBUTE_UNUSED, int fd)
  298. {
  299. return close (fd);
  300. }
  301. /* Execute a child. */
  302. #if defined(HAVE_SPAWNVE) && defined(HAVE_SPAWNVPE)
  303. /* Implementation of pex->exec_child using the Cygwin spawn operation. */
  304. /* Subroutine of pex_unix_exec_child. Move OLD_FD to a new file descriptor
  305. to be stored in *PNEW_FD, save the flags in *PFLAGS, and arrange for the
  306. saved copy to be close-on-exec. Move CHILD_FD into OLD_FD. If CHILD_FD
  307. is -1, OLD_FD is to be closed. Return -1 on error. */
  308. static int
  309. save_and_install_fd(int *pnew_fd, int *pflags, int old_fd, int child_fd)
  310. {
  311. int new_fd, flags;
  312. flags = fcntl (old_fd, F_GETFD);
  313. /* If we could not retrieve the flags, then OLD_FD was not open. */
  314. if (flags < 0)
  315. {
  316. new_fd = -1, flags = 0;
  317. if (child_fd >= 0 && dup2 (child_fd, old_fd) < 0)
  318. return -1;
  319. }
  320. /* If we wish to close OLD_FD, just mark it CLOEXEC. */
  321. else if (child_fd == -1)
  322. {
  323. new_fd = old_fd;
  324. if ((flags & FD_CLOEXEC) == 0 && fcntl (old_fd, F_SETFD, FD_CLOEXEC) < 0)
  325. return -1;
  326. }
  327. /* Otherwise we need to save a copy of OLD_FD before installing CHILD_FD. */
  328. else
  329. {
  330. #ifdef F_DUPFD_CLOEXEC
  331. new_fd = fcntl (old_fd, F_DUPFD_CLOEXEC, 3);
  332. if (new_fd < 0)
  333. return -1;
  334. #else
  335. /* Prefer F_DUPFD over dup in order to avoid getting a new fd
  336. in the range 0-2, right where a new stderr fd might get put. */
  337. new_fd = fcntl (old_fd, F_DUPFD, 3);
  338. if (new_fd < 0)
  339. return -1;
  340. if (fcntl (new_fd, F_SETFD, FD_CLOEXEC) < 0)
  341. return -1;
  342. #endif
  343. if (dup2 (child_fd, old_fd) < 0)
  344. return -1;
  345. }
  346. *pflags = flags;
  347. if (pnew_fd)
  348. *pnew_fd = new_fd;
  349. else if (new_fd != old_fd)
  350. abort ();
  351. return 0;
  352. }
  353. /* Subroutine of pex_unix_exec_child. Move SAVE_FD back to OLD_FD
  354. restoring FLAGS. If SAVE_FD < 0, OLD_FD is to be closed. */
  355. static int
  356. restore_fd(int old_fd, int save_fd, int flags)
  357. {
  358. /* For SAVE_FD < 0, all we have to do is restore the
  359. "closed-ness" of the original. */
  360. if (save_fd < 0)
  361. return close (old_fd);
  362. /* For SAVE_FD == OLD_FD, all we have to do is restore the
  363. original setting of the CLOEXEC flag. */
  364. if (save_fd == old_fd)
  365. {
  366. if (flags & FD_CLOEXEC)
  367. return 0;
  368. return fcntl (old_fd, F_SETFD, flags);
  369. }
  370. /* Otherwise we have to move the descriptor back, restore the flags,
  371. and close the saved copy. */
  372. #ifdef HAVE_DUP3
  373. if (flags == FD_CLOEXEC)
  374. {
  375. if (dup3 (save_fd, old_fd, O_CLOEXEC) < 0)
  376. return -1;
  377. }
  378. else
  379. #endif
  380. {
  381. if (dup2 (save_fd, old_fd) < 0)
  382. return -1;
  383. if (flags != 0 && fcntl (old_fd, F_SETFD, flags) < 0)
  384. return -1;
  385. }
  386. return close (save_fd);
  387. }
  388. static pid_t
  389. pex_unix_exec_child (struct pex_obj *obj ATTRIBUTE_UNUSED,
  390. int flags, const char *executable,
  391. char * const * argv, char * const * env,
  392. int in, int out, int errdes, int toclose,
  393. const char **errmsg, int *err)
  394. {
  395. int fl_in = 0, fl_out = 0, fl_err = 0, fl_tc = 0;
  396. int save_in = -1, save_out = -1, save_err = -1;
  397. int max, retries;
  398. pid_t pid;
  399. if (flags & PEX_STDERR_TO_STDOUT)
  400. errdes = out;
  401. /* We need the three standard file descriptors to be set up as for
  402. the child before we perform the spawn. The file descriptors for
  403. the parent need to be moved and marked for close-on-exec. */
  404. if (in != STDIN_FILE_NO
  405. && save_and_install_fd (&save_in, &fl_in, STDIN_FILE_NO, in) < 0)
  406. goto error_dup2;
  407. if (out != STDOUT_FILE_NO
  408. && save_and_install_fd (&save_out, &fl_out, STDOUT_FILE_NO, out) < 0)
  409. goto error_dup2;
  410. if (errdes != STDERR_FILE_NO
  411. && save_and_install_fd (&save_err, &fl_err, STDERR_FILE_NO, errdes) < 0)
  412. goto error_dup2;
  413. if (toclose >= 0
  414. && save_and_install_fd (NULL, &fl_tc, toclose, -1) < 0)
  415. goto error_dup2;
  416. /* Now that we've moved the file descriptors for the child into place,
  417. close the originals. Be careful not to close any of the standard
  418. file descriptors that we just set up. */
  419. max = -1;
  420. if (errdes >= 0)
  421. max = STDERR_FILE_NO;
  422. else if (out >= 0)
  423. max = STDOUT_FILE_NO;
  424. else if (in >= 0)
  425. max = STDIN_FILE_NO;
  426. if (in > max)
  427. close (in);
  428. if (out > max)
  429. close (out);
  430. if (errdes > max && errdes != out)
  431. close (errdes);
  432. /* If we were not given an environment, use the global environment. */
  433. if (env == NULL)
  434. env = environ;
  435. /* Launch the program. If we get EAGAIN (normally out of pid's), try
  436. again a few times with increasing backoff times. */
  437. retries = 0;
  438. while (1)
  439. {
  440. typedef const char * const *cc_cp;
  441. if (flags & PEX_SEARCH)
  442. pid = spawnvpe (_P_NOWAITO, executable, (cc_cp)argv, (cc_cp)env);
  443. else
  444. pid = spawnve (_P_NOWAITO, executable, (cc_cp)argv, (cc_cp)env);
  445. if (pid > 0)
  446. break;
  447. *err = errno;
  448. *errmsg = "spawn";
  449. if (errno != EAGAIN || ++retries == 4)
  450. return (pid_t) -1;
  451. sleep (1 << retries);
  452. }
  453. /* Success. Restore the parent's file descriptors that we saved above. */
  454. if (toclose >= 0
  455. && restore_fd (toclose, toclose, fl_tc) < 0)
  456. goto error_dup2;
  457. if (in != STDIN_FILE_NO
  458. && restore_fd (STDIN_FILE_NO, save_in, fl_in) < 0)
  459. goto error_dup2;
  460. if (out != STDOUT_FILE_NO
  461. && restore_fd (STDOUT_FILE_NO, save_out, fl_out) < 0)
  462. goto error_dup2;
  463. if (errdes != STDERR_FILE_NO
  464. && restore_fd (STDERR_FILE_NO, save_err, fl_err) < 0)
  465. goto error_dup2;
  466. return pid;
  467. error_dup2:
  468. *err = errno;
  469. *errmsg = "dup2";
  470. return (pid_t) -1;
  471. }
  472. #else
  473. /* Implementation of pex->exec_child using standard vfork + exec. */
  474. static pid_t
  475. pex_unix_exec_child (struct pex_obj *obj, int flags, const char *executable,
  476. char * const * argv, char * const * env,
  477. int in, int out, int errdes,
  478. int toclose, const char **errmsg, int *err)
  479. {
  480. pid_t pid = -1;
  481. /* Tuple to communicate error from child to parent. We can safely
  482. transfer string literal pointers as both run with identical
  483. address mappings. */
  484. struct fn_err
  485. {
  486. const char *fn;
  487. int err;
  488. };
  489. volatile int do_pipe = 0;
  490. volatile int pipes[2]; /* [0]:reader,[1]:writer. */
  491. #ifdef O_CLOEXEC
  492. do_pipe = 1;
  493. #endif
  494. if (do_pipe)
  495. {
  496. #ifdef HAVE_PIPE2
  497. if (pipe2 ((int *)pipes, O_CLOEXEC))
  498. do_pipe = 0;
  499. #else
  500. if (pipe ((int *)pipes))
  501. do_pipe = 0;
  502. else
  503. {
  504. if (fcntl (pipes[1], F_SETFD, FD_CLOEXEC) == -1)
  505. {
  506. close (pipes[0]);
  507. close (pipes[1]);
  508. do_pipe = 0;
  509. }
  510. }
  511. #endif
  512. }
  513. /* We declare these to be volatile to avoid warnings from gcc about
  514. them being clobbered by vfork. */
  515. volatile int sleep_interval = 1;
  516. volatile int retries;
  517. /* We vfork and then set environ in the child before calling execvp.
  518. This clobbers the parent's environ so we need to restore it.
  519. It would be nice to use one of the exec* functions that takes an
  520. environment as a parameter, but that may have portability
  521. issues. It is marked volatile so the child doesn't consider it a
  522. dead variable and therefore clobber where ever it is stored. */
  523. char **volatile save_environ = environ;
  524. for (retries = 0; retries < 4; ++retries)
  525. {
  526. pid = vfork ();
  527. if (pid >= 0)
  528. break;
  529. sleep (sleep_interval);
  530. sleep_interval *= 2;
  531. }
  532. switch (pid)
  533. {
  534. case -1:
  535. if (do_pipe)
  536. {
  537. close (pipes[0]);
  538. close (pipes[1]);
  539. }
  540. *err = errno;
  541. *errmsg = VFORK_STRING;
  542. return (pid_t) -1;
  543. case 0:
  544. /* Child process. */
  545. {
  546. struct fn_err failed;
  547. failed.fn = NULL;
  548. if (do_pipe)
  549. close (pipes[0]);
  550. if (!failed.fn && in != STDIN_FILE_NO)
  551. {
  552. if (dup2 (in, STDIN_FILE_NO) < 0)
  553. failed.fn = "dup2", failed.err = errno;
  554. else if (close (in) < 0)
  555. failed.fn = "close", failed.err = errno;
  556. }
  557. if (!failed.fn && out != STDOUT_FILE_NO)
  558. {
  559. if (dup2 (out, STDOUT_FILE_NO) < 0)
  560. failed.fn = "dup2", failed.err = errno;
  561. else if (close (out) < 0)
  562. failed.fn = "close", failed.err = errno;
  563. }
  564. if (!failed.fn && errdes != STDERR_FILE_NO)
  565. {
  566. if (dup2 (errdes, STDERR_FILE_NO) < 0)
  567. failed.fn = "dup2", failed.err = errno;
  568. else if (close (errdes) < 0)
  569. failed.fn = "close", failed.err = errno;
  570. }
  571. if (!failed.fn && toclose >= 0)
  572. {
  573. if (close (toclose) < 0)
  574. failed.fn = "close", failed.err = errno;
  575. }
  576. if (!failed.fn && (flags & PEX_STDERR_TO_STDOUT) != 0)
  577. {
  578. if (dup2 (STDOUT_FILE_NO, STDERR_FILE_NO) < 0)
  579. failed.fn = "dup2", failed.err = errno;
  580. }
  581. if (!failed.fn)
  582. {
  583. if (env)
  584. /* NOTE: In a standard vfork implementation this clobbers
  585. the parent's copy of environ "too" (in reality there's
  586. only one copy). This is ok as we restore it below. */
  587. environ = (char**) env;
  588. if ((flags & PEX_SEARCH) != 0)
  589. {
  590. execvp (executable, to_ptr32 (argv));
  591. failed.fn = "execvp", failed.err = errno;
  592. }
  593. else
  594. {
  595. execv (executable, to_ptr32 (argv));
  596. failed.fn = "execv", failed.err = errno;
  597. }
  598. }
  599. /* Something failed, report an error. We don't use stdio
  600. routines, because we might be here due to a vfork call. */
  601. ssize_t retval = 0;
  602. if (!do_pipe
  603. || write (pipes[1], &failed, sizeof (failed)) != sizeof (failed))
  604. {
  605. /* The parent will not see our scream above, so write to
  606. stdout. */
  607. #define writeerr(s) (retval |= write (STDERR_FILE_NO, s, strlen (s)))
  608. writeerr (obj->pname);
  609. writeerr (": error trying to exec '");
  610. writeerr (executable);
  611. writeerr ("': ");
  612. writeerr (failed.fn);
  613. writeerr (": ");
  614. writeerr (xstrerror (failed.err));
  615. writeerr ("\n");
  616. #undef writeerr
  617. }
  618. /* Exit with -2 if the error output failed, too. */
  619. _exit (retval < 0 ? -2 : -1);
  620. }
  621. /* NOTREACHED */
  622. return (pid_t) -1;
  623. default:
  624. /* Parent process. */
  625. {
  626. /* Restore environ. Note that the parent either doesn't run
  627. until the child execs/exits (standard vfork behaviour), or
  628. if it does run then vfork is behaving more like fork. In
  629. either case we needn't worry about clobbering the child's
  630. copy of environ. */
  631. environ = save_environ;
  632. struct fn_err failed;
  633. failed.fn = NULL;
  634. if (do_pipe)
  635. {
  636. close (pipes[1]);
  637. ssize_t len = read (pipes[0], &failed, sizeof (failed));
  638. if (len < 0)
  639. failed.fn = NULL;
  640. close (pipes[0]);
  641. }
  642. if (!failed.fn && in != STDIN_FILE_NO)
  643. if (close (in) < 0)
  644. failed.fn = "close", failed.err = errno;
  645. if (!failed.fn && out != STDOUT_FILE_NO)
  646. if (close (out) < 0)
  647. failed.fn = "close", failed.err = errno;
  648. if (!failed.fn && errdes != STDERR_FILE_NO)
  649. if (close (errdes) < 0)
  650. failed.fn = "close", failed.err = errno;
  651. if (failed.fn)
  652. {
  653. *err = failed.err;
  654. *errmsg = failed.fn;
  655. return (pid_t) -1;
  656. }
  657. }
  658. return pid;
  659. }
  660. }
  661. #endif /* SPAWN */
  662. /* Wait for a child process to complete. */
  663. static int
  664. pex_unix_wait (struct pex_obj *obj, pid_t pid, int *status,
  665. struct pex_time *time, int done, const char **errmsg,
  666. int *err)
  667. {
  668. /* If we are cleaning up when the caller didn't retrieve process
  669. status for some reason, encourage the process to go away. */
  670. if (done)
  671. kill (pid, SIGTERM);
  672. if (pex_wait (obj, pid, status, time) < 0)
  673. {
  674. *err = errno;
  675. *errmsg = "wait";
  676. return -1;
  677. }
  678. return 0;
  679. }
  680. /* Create a pipe. */
  681. static int
  682. pex_unix_pipe (struct pex_obj *obj ATTRIBUTE_UNUSED, int *p,
  683. int binary ATTRIBUTE_UNUSED)
  684. {
  685. return pipe (p);
  686. }
  687. /* Get a FILE pointer to read from a file descriptor. */
  688. static FILE *
  689. pex_unix_fdopenr (struct pex_obj *obj ATTRIBUTE_UNUSED, int fd,
  690. int binary ATTRIBUTE_UNUSED)
  691. {
  692. return fdopen (fd, "r");
  693. }
  694. static FILE *
  695. pex_unix_fdopenw (struct pex_obj *obj ATTRIBUTE_UNUSED, int fd,
  696. int binary ATTRIBUTE_UNUSED)
  697. {
  698. if (fcntl (fd, F_SETFD, FD_CLOEXEC) < 0)
  699. return NULL;
  700. return fdopen (fd, "w");
  701. }
  702. static void
  703. pex_unix_cleanup (struct pex_obj *obj ATTRIBUTE_UNUSED)
  704. {
  705. #if !defined (HAVE_WAIT4) && !defined (HAVE_WAITPID)
  706. while (obj->sysdep != NULL)
  707. {
  708. struct status_list *this;
  709. struct status_list *next;
  710. this = (struct status_list *) obj->sysdep;
  711. next = this->next;
  712. free (this);
  713. obj->sysdep = (void *) next;
  714. }
  715. #endif
  716. }