pex-unix.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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, 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2009,
  5. 2010 Free Software Foundation, Inc.
  6. This file is part of the libiberty library.
  7. Libiberty is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Library General Public
  9. License as published by the Free Software Foundation; either
  10. version 2 of the License, or (at your option) any later version.
  11. Libiberty is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. Library General Public License for more details.
  15. You should have received a copy of the GNU Library General Public
  16. License along with libiberty; see the file COPYING.LIB. If not,
  17. write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  18. Boston, MA 02110-1301, USA. */
  19. #include "config.h"
  20. #include "libiberty.h"
  21. #include "pex-common.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 void pex_child_error (struct pex_obj *, const char *, const char *, int)
  247. ATTRIBUTE_NORETURN;
  248. static int pex_unix_open_read (struct pex_obj *, const char *, int);
  249. static int pex_unix_open_write (struct pex_obj *, const char *, int, int);
  250. static pid_t pex_unix_exec_child (struct pex_obj *, int, const char *,
  251. char * const *, char * const *,
  252. int, int, int, int,
  253. const char **, int *);
  254. static int pex_unix_close (struct pex_obj *, int);
  255. static int pex_unix_wait (struct pex_obj *, pid_t, int *, struct pex_time *,
  256. int, const char **, int *);
  257. static int pex_unix_pipe (struct pex_obj *, int *, int);
  258. static FILE *pex_unix_fdopenr (struct pex_obj *, int, int);
  259. static FILE *pex_unix_fdopenw (struct pex_obj *, int, int);
  260. static void pex_unix_cleanup (struct pex_obj *);
  261. /* The list of functions we pass to the common routines. */
  262. const struct pex_funcs funcs =
  263. {
  264. pex_unix_open_read,
  265. pex_unix_open_write,
  266. pex_unix_exec_child,
  267. pex_unix_close,
  268. pex_unix_wait,
  269. pex_unix_pipe,
  270. pex_unix_fdopenr,
  271. pex_unix_fdopenw,
  272. pex_unix_cleanup
  273. };
  274. /* Return a newly initialized pex_obj structure. */
  275. struct pex_obj *
  276. pex_init (int flags, const char *pname, const char *tempbase)
  277. {
  278. return pex_init_common (flags, pname, tempbase, &funcs);
  279. }
  280. /* Open a file for reading. */
  281. static int
  282. pex_unix_open_read (struct pex_obj *obj ATTRIBUTE_UNUSED, const char *name,
  283. int binary ATTRIBUTE_UNUSED)
  284. {
  285. return open (name, O_RDONLY);
  286. }
  287. /* Open a file for writing. */
  288. static int
  289. pex_unix_open_write (struct pex_obj *obj ATTRIBUTE_UNUSED, const char *name,
  290. int binary ATTRIBUTE_UNUSED, int append)
  291. {
  292. /* Note that we can't use O_EXCL here because gcc may have already
  293. created the temporary file via make_temp_file. */
  294. return open (name, O_WRONLY | O_CREAT
  295. | (append ? O_APPEND : O_TRUNC), PUBLIC_MODE);
  296. }
  297. /* Close a file. */
  298. static int
  299. pex_unix_close (struct pex_obj *obj ATTRIBUTE_UNUSED, int fd)
  300. {
  301. return close (fd);
  302. }
  303. /* Report an error from a child process. We don't use stdio routines,
  304. because we might be here due to a vfork call. */
  305. static void
  306. pex_child_error (struct pex_obj *obj, const char *executable,
  307. const char *errmsg, int err)
  308. {
  309. int retval = 0;
  310. #define writeerr(s) retval |= (write (STDERR_FILE_NO, s, strlen (s)) < 0)
  311. writeerr (obj->pname);
  312. writeerr (": error trying to exec '");
  313. writeerr (executable);
  314. writeerr ("': ");
  315. writeerr (errmsg);
  316. writeerr (": ");
  317. writeerr (xstrerror (err));
  318. writeerr ("\n");
  319. #undef writeerr
  320. /* Exit with -2 if the error output failed, too. */
  321. _exit (retval == 0 ? -1 : -2);
  322. }
  323. /* Execute a child. */
  324. extern char **environ;
  325. #if defined(HAVE_SPAWNVE) && defined(HAVE_SPAWNVPE)
  326. /* Implementation of pex->exec_child using the Cygwin spawn operation. */
  327. /* Subroutine of pex_unix_exec_child. Move OLD_FD to a new file descriptor
  328. to be stored in *PNEW_FD, save the flags in *PFLAGS, and arrange for the
  329. saved copy to be close-on-exec. Move CHILD_FD into OLD_FD. If CHILD_FD
  330. is -1, OLD_FD is to be closed. Return -1 on error. */
  331. static int
  332. save_and_install_fd(int *pnew_fd, int *pflags, int old_fd, int child_fd)
  333. {
  334. int new_fd, flags;
  335. flags = fcntl (old_fd, F_GETFD);
  336. /* If we could not retrieve the flags, then OLD_FD was not open. */
  337. if (flags < 0)
  338. {
  339. new_fd = -1, flags = 0;
  340. if (child_fd >= 0 && dup2 (child_fd, old_fd) < 0)
  341. return -1;
  342. }
  343. /* If we wish to close OLD_FD, just mark it CLOEXEC. */
  344. else if (child_fd == -1)
  345. {
  346. new_fd = old_fd;
  347. if ((flags & FD_CLOEXEC) == 0 && fcntl (old_fd, F_SETFD, FD_CLOEXEC) < 0)
  348. return -1;
  349. }
  350. /* Otherwise we need to save a copy of OLD_FD before installing CHILD_FD. */
  351. else
  352. {
  353. #ifdef F_DUPFD_CLOEXEC
  354. new_fd = fcntl (old_fd, F_DUPFD_CLOEXEC, 3);
  355. if (new_fd < 0)
  356. return -1;
  357. #else
  358. /* Prefer F_DUPFD over dup in order to avoid getting a new fd
  359. in the range 0-2, right where a new stderr fd might get put. */
  360. new_fd = fcntl (old_fd, F_DUPFD, 3);
  361. if (new_fd < 0)
  362. return -1;
  363. if (fcntl (new_fd, F_SETFD, FD_CLOEXEC) < 0)
  364. return -1;
  365. #endif
  366. if (dup2 (child_fd, old_fd) < 0)
  367. return -1;
  368. }
  369. *pflags = flags;
  370. if (pnew_fd)
  371. *pnew_fd = new_fd;
  372. else if (new_fd != old_fd)
  373. abort ();
  374. return 0;
  375. }
  376. /* Subroutine of pex_unix_exec_child. Move SAVE_FD back to OLD_FD
  377. restoring FLAGS. If SAVE_FD < 0, OLD_FD is to be closed. */
  378. static int
  379. restore_fd(int old_fd, int save_fd, int flags)
  380. {
  381. /* For SAVE_FD < 0, all we have to do is restore the
  382. "closed-ness" of the original. */
  383. if (save_fd < 0)
  384. return close (old_fd);
  385. /* For SAVE_FD == OLD_FD, all we have to do is restore the
  386. original setting of the CLOEXEC flag. */
  387. if (save_fd == old_fd)
  388. {
  389. if (flags & FD_CLOEXEC)
  390. return 0;
  391. return fcntl (old_fd, F_SETFD, flags);
  392. }
  393. /* Otherwise we have to move the descriptor back, restore the flags,
  394. and close the saved copy. */
  395. #ifdef HAVE_DUP3
  396. if (flags == FD_CLOEXEC)
  397. {
  398. if (dup3 (save_fd, old_fd, O_CLOEXEC) < 0)
  399. return -1;
  400. }
  401. else
  402. #endif
  403. {
  404. if (dup2 (save_fd, old_fd) < 0)
  405. return -1;
  406. if (flags != 0 && fcntl (old_fd, F_SETFD, flags) < 0)
  407. return -1;
  408. }
  409. return close (save_fd);
  410. }
  411. static pid_t
  412. pex_unix_exec_child (struct pex_obj *obj ATTRIBUTE_UNUSED,
  413. int flags, const char *executable,
  414. char * const * argv, char * const * env,
  415. int in, int out, int errdes, int toclose,
  416. const char **errmsg, int *err)
  417. {
  418. int fl_in = 0, fl_out = 0, fl_err = 0, fl_tc = 0;
  419. int save_in = -1, save_out = -1, save_err = -1;
  420. int max, retries;
  421. pid_t pid;
  422. if (flags & PEX_STDERR_TO_STDOUT)
  423. errdes = out;
  424. /* We need the three standard file descriptors to be set up as for
  425. the child before we perform the spawn. The file descriptors for
  426. the parent need to be moved and marked for close-on-exec. */
  427. if (in != STDIN_FILE_NO
  428. && save_and_install_fd (&save_in, &fl_in, STDIN_FILE_NO, in) < 0)
  429. goto error_dup2;
  430. if (out != STDOUT_FILE_NO
  431. && save_and_install_fd (&save_out, &fl_out, STDOUT_FILE_NO, out) < 0)
  432. goto error_dup2;
  433. if (errdes != STDERR_FILE_NO
  434. && save_and_install_fd (&save_err, &fl_err, STDERR_FILE_NO, errdes) < 0)
  435. goto error_dup2;
  436. if (toclose >= 0
  437. && save_and_install_fd (NULL, &fl_tc, toclose, -1) < 0)
  438. goto error_dup2;
  439. /* Now that we've moved the file descriptors for the child into place,
  440. close the originals. Be careful not to close any of the standard
  441. file descriptors that we just set up. */
  442. max = -1;
  443. if (errdes >= 0)
  444. max = STDERR_FILE_NO;
  445. else if (out >= 0)
  446. max = STDOUT_FILE_NO;
  447. else if (in >= 0)
  448. max = STDIN_FILE_NO;
  449. if (in > max)
  450. close (in);
  451. if (out > max)
  452. close (out);
  453. if (errdes > max && errdes != out)
  454. close (errdes);
  455. /* If we were not given an environment, use the global environment. */
  456. if (env == NULL)
  457. env = environ;
  458. /* Launch the program. If we get EAGAIN (normally out of pid's), try
  459. again a few times with increasing backoff times. */
  460. retries = 0;
  461. while (1)
  462. {
  463. typedef const char * const *cc_cp;
  464. if (flags & PEX_SEARCH)
  465. pid = spawnvpe (_P_NOWAITO, executable, (cc_cp)argv, (cc_cp)env);
  466. else
  467. pid = spawnve (_P_NOWAITO, executable, (cc_cp)argv, (cc_cp)env);
  468. if (pid > 0)
  469. break;
  470. *err = errno;
  471. *errmsg = "spawn";
  472. if (errno != EAGAIN || ++retries == 4)
  473. return (pid_t) -1;
  474. sleep (1 << retries);
  475. }
  476. /* Success. Restore the parent's file descriptors that we saved above. */
  477. if (toclose >= 0
  478. && restore_fd (toclose, toclose, fl_tc) < 0)
  479. goto error_dup2;
  480. if (in != STDIN_FILE_NO
  481. && restore_fd (STDIN_FILE_NO, save_in, fl_in) < 0)
  482. goto error_dup2;
  483. if (out != STDOUT_FILE_NO
  484. && restore_fd (STDOUT_FILE_NO, save_out, fl_out) < 0)
  485. goto error_dup2;
  486. if (errdes != STDERR_FILE_NO
  487. && restore_fd (STDERR_FILE_NO, save_err, fl_err) < 0)
  488. goto error_dup2;
  489. return pid;
  490. error_dup2:
  491. *err = errno;
  492. *errmsg = "dup2";
  493. return (pid_t) -1;
  494. }
  495. #else
  496. /* Implementation of pex->exec_child using standard vfork + exec. */
  497. static pid_t
  498. pex_unix_exec_child (struct pex_obj *obj, int flags, const char *executable,
  499. char * const * argv, char * const * env,
  500. int in, int out, int errdes,
  501. int toclose, const char **errmsg, int *err)
  502. {
  503. pid_t pid;
  504. /* We declare these to be volatile to avoid warnings from gcc about
  505. them being clobbered by vfork. */
  506. volatile int sleep_interval;
  507. volatile int retries;
  508. /* We vfork and then set environ in the child before calling execvp.
  509. This clobbers the parent's environ so we need to restore it.
  510. It would be nice to use one of the exec* functions that takes an
  511. environment as a parameter, but that may have portability issues. */
  512. char **save_environ = environ;
  513. sleep_interval = 1;
  514. pid = -1;
  515. for (retries = 0; retries < 4; ++retries)
  516. {
  517. pid = vfork ();
  518. if (pid >= 0)
  519. break;
  520. sleep (sleep_interval);
  521. sleep_interval *= 2;
  522. }
  523. switch (pid)
  524. {
  525. case -1:
  526. *err = errno;
  527. *errmsg = VFORK_STRING;
  528. return (pid_t) -1;
  529. case 0:
  530. /* Child process. */
  531. if (in != STDIN_FILE_NO)
  532. {
  533. if (dup2 (in, STDIN_FILE_NO) < 0)
  534. pex_child_error (obj, executable, "dup2", errno);
  535. if (close (in) < 0)
  536. pex_child_error (obj, executable, "close", errno);
  537. }
  538. if (out != STDOUT_FILE_NO)
  539. {
  540. if (dup2 (out, STDOUT_FILE_NO) < 0)
  541. pex_child_error (obj, executable, "dup2", errno);
  542. if (close (out) < 0)
  543. pex_child_error (obj, executable, "close", errno);
  544. }
  545. if (errdes != STDERR_FILE_NO)
  546. {
  547. if (dup2 (errdes, STDERR_FILE_NO) < 0)
  548. pex_child_error (obj, executable, "dup2", errno);
  549. if (close (errdes) < 0)
  550. pex_child_error (obj, executable, "close", errno);
  551. }
  552. if (toclose >= 0)
  553. {
  554. if (close (toclose) < 0)
  555. pex_child_error (obj, executable, "close", errno);
  556. }
  557. if ((flags & PEX_STDERR_TO_STDOUT) != 0)
  558. {
  559. if (dup2 (STDOUT_FILE_NO, STDERR_FILE_NO) < 0)
  560. pex_child_error (obj, executable, "dup2", errno);
  561. }
  562. if (env)
  563. {
  564. /* NOTE: In a standard vfork implementation this clobbers the
  565. parent's copy of environ "too" (in reality there's only one copy).
  566. This is ok as we restore it below. */
  567. environ = (char**) env;
  568. }
  569. if ((flags & PEX_SEARCH) != 0)
  570. {
  571. execvp (executable, to_ptr32 (argv));
  572. pex_child_error (obj, executable, "execvp", errno);
  573. }
  574. else
  575. {
  576. execv (executable, to_ptr32 (argv));
  577. pex_child_error (obj, executable, "execv", errno);
  578. }
  579. /* NOTREACHED */
  580. return (pid_t) -1;
  581. default:
  582. /* Parent process. */
  583. /* Restore environ.
  584. Note that the parent either doesn't run until the child execs/exits
  585. (standard vfork behaviour), or if it does run then vfork is behaving
  586. more like fork. In either case we needn't worry about clobbering
  587. the child's copy of environ. */
  588. environ = save_environ;
  589. if (in != STDIN_FILE_NO)
  590. {
  591. if (close (in) < 0)
  592. {
  593. *err = errno;
  594. *errmsg = "close";
  595. return (pid_t) -1;
  596. }
  597. }
  598. if (out != STDOUT_FILE_NO)
  599. {
  600. if (close (out) < 0)
  601. {
  602. *err = errno;
  603. *errmsg = "close";
  604. return (pid_t) -1;
  605. }
  606. }
  607. if (errdes != STDERR_FILE_NO)
  608. {
  609. if (close (errdes) < 0)
  610. {
  611. *err = errno;
  612. *errmsg = "close";
  613. return (pid_t) -1;
  614. }
  615. }
  616. return pid;
  617. }
  618. }
  619. #endif /* SPAWN */
  620. /* Wait for a child process to complete. */
  621. static int
  622. pex_unix_wait (struct pex_obj *obj, pid_t pid, int *status,
  623. struct pex_time *time, int done, const char **errmsg,
  624. int *err)
  625. {
  626. /* If we are cleaning up when the caller didn't retrieve process
  627. status for some reason, encourage the process to go away. */
  628. if (done)
  629. kill (pid, SIGTERM);
  630. if (pex_wait (obj, pid, status, time) < 0)
  631. {
  632. *err = errno;
  633. *errmsg = "wait";
  634. return -1;
  635. }
  636. return 0;
  637. }
  638. /* Create a pipe. */
  639. static int
  640. pex_unix_pipe (struct pex_obj *obj ATTRIBUTE_UNUSED, int *p,
  641. int binary ATTRIBUTE_UNUSED)
  642. {
  643. return pipe (p);
  644. }
  645. /* Get a FILE pointer to read from a file descriptor. */
  646. static FILE *
  647. pex_unix_fdopenr (struct pex_obj *obj ATTRIBUTE_UNUSED, int fd,
  648. int binary ATTRIBUTE_UNUSED)
  649. {
  650. return fdopen (fd, "r");
  651. }
  652. static FILE *
  653. pex_unix_fdopenw (struct pex_obj *obj ATTRIBUTE_UNUSED, int fd,
  654. int binary ATTRIBUTE_UNUSED)
  655. {
  656. if (fcntl (fd, F_SETFD, FD_CLOEXEC) < 0)
  657. return NULL;
  658. return fdopen (fd, "w");
  659. }
  660. static void
  661. pex_unix_cleanup (struct pex_obj *obj ATTRIBUTE_UNUSED)
  662. {
  663. #if !defined (HAVE_WAIT4) && !defined (HAVE_WAITPID)
  664. while (obj->sysdep != NULL)
  665. {
  666. struct status_list *this;
  667. struct status_list *next;
  668. this = (struct status_list *) obj->sysdep;
  669. next = this->next;
  670. free (this);
  671. obj->sysdep = (void *) next;
  672. }
  673. #endif
  674. }