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