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-2017 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.
12 Libiberty is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with libiberty; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 Boston, MA 02110-1301, USA. */
23 #include "libiberty.h"
24 #include "pex-common.h"
30 #ifdef NEED_DECLARATION_ERRNO
43 #include <sys/types.h>
48 #ifdef HAVE_SYS_WAIT_H
53 #include <sys/resource.h>
55 #ifdef HAVE_SYS_STAT_H
62 #ifdef vfork /* Autoconf may define this to fork for us. */
63 # define VFORK_STRING "fork"
65 # define VFORK_STRING "vfork"
70 #if defined(VMS) && defined (__LONG_POINTERS)
72 typedef char * __char_ptr32
73 __attribute__ ((mode (SI
)));
76 typedef __char_ptr32
*__char_ptr_char_ptr32
77 __attribute__ ((mode (SI
)));
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. */
82 static __char_ptr_char_ptr32
83 to_ptr32 (char **ptr64
)
86 __char_ptr_char_ptr32 short_argv
;
88 /* Count number of arguments. */
89 for (argc
= 0; ptr64
[argc
] != NULL
; argc
++)
92 /* Reallocate argv with 32 bit pointers. */
93 short_argv
= (__char_ptr_char_ptr32
) decc$malloc
94 (sizeof (__char_ptr32
) * (argc
+ 1));
96 for (argc
= 0; ptr64
[argc
] != NULL
; argc
++)
97 short_argv
[argc
] = (__char_ptr32
) decc$
strdup (ptr64
[argc
]);
99 short_argv
[argc
] = (__char_ptr32
) 0;
104 #define to_ptr32(argv) argv
107 /* File mode to use for private and world-readable files. */
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)
113 #define PUBLIC_MODE 0666
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. */
120 static pid_t
pex_wait (struct pex_obj
*, pid_t
, int *, struct pex_time
*);
125 pex_wait (struct pex_obj
*obj ATTRIBUTE_UNUSED
, pid_t pid
, int *status
,
126 struct pex_time
*time
)
133 return waitpid (pid
, status
, 0);
136 ret
= wait4 (pid
, status
, 0, &r
);
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
;
149 #else /* ! defined (HAVE_WAIT4) */
153 #ifndef HAVE_GETRUSAGE
156 pex_wait (struct pex_obj
*obj ATTRIBUTE_UNUSED
, pid_t pid
, int *status
,
157 struct pex_time
*time
)
160 memset (time
, 0, sizeof (struct pex_time
));
161 return waitpid (pid
, status
, 0);
164 #else /* defined (HAVE_GETRUSAGE) */
167 pex_wait (struct pex_obj
*obj ATTRIBUTE_UNUSED
, pid_t pid
, int *status
,
168 struct pex_time
*time
)
170 struct rusage r1
, r2
;
174 return waitpid (pid
, status
, 0);
176 getrusage (RUSAGE_CHILDREN
, &r1
);
178 ret
= waitpid (pid
, status
, 0);
182 getrusage (RUSAGE_CHILDREN
, &r2
);
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
)
188 --time
->user_seconds
;
189 time
->user_microseconds
+= 1000000;
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
)
196 --time
->system_seconds
;
197 time
->system_microseconds
+= 1000000;
203 #endif /* defined (HAVE_GETRUSAGE) */
205 #else /* ! defined (HAVE_WAITPID) */
209 struct status_list
*next
;
212 struct pex_time time
;
216 pex_wait (struct pex_obj
*obj
, pid_t pid
, int *status
, struct pex_time
*time
)
218 struct status_list
**pp
;
220 for (pp
= (struct status_list
**) &obj
->sysdep
;
224 if ((*pp
)->pid
== pid
)
226 struct status_list
*p
;
241 struct status_list
*psl
;
243 #ifdef HAVE_GETRUSAGE
244 struct rusage r1
, r2
;
249 #ifdef HAVE_GETRUSAGE
250 getrusage (RUSAGE_CHILDREN
, &r1
);
252 memset (&pt
, 0, sizeof (struct pex_time
));
256 cpid
= wait (status
);
258 #ifdef HAVE_GETRUSAGE
259 if (time
!= NULL
&& cpid
>= 0)
261 getrusage (RUSAGE_CHILDREN
, &r2
);
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)
268 pt
.user_microseconds
+= 1000000;
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)
276 pt
.system_microseconds
+= 1000000;
281 if (cpid
< 0 || cpid
== pid
)
288 psl
= XNEW (struct status_list
);
290 psl
->status
= *status
;
293 psl
->next
= (struct status_list
*) obj
->sysdep
;
294 obj
->sysdep
= (void *) psl
;
298 #endif /* ! defined (HAVE_WAITPID) */
299 #endif /* ! defined (HAVE_WAIT4) */
301 static void pex_child_error (struct pex_obj
*, const char *, const char *, int)
303 static int pex_unix_open_read (struct pex_obj
*, const char *, int);
304 static int pex_unix_open_write (struct pex_obj
*, const char *, int, int);
305 static pid_t
pex_unix_exec_child (struct pex_obj
*, int, const char *,
306 char * const *, char * const *,
308 const char **, int *);
309 static int pex_unix_close (struct pex_obj
*, int);
310 static int pex_unix_wait (struct pex_obj
*, pid_t
, int *, struct pex_time
*,
311 int, const char **, int *);
312 static int pex_unix_pipe (struct pex_obj
*, int *, int);
313 static FILE *pex_unix_fdopenr (struct pex_obj
*, int, int);
314 static FILE *pex_unix_fdopenw (struct pex_obj
*, int, int);
315 static void pex_unix_cleanup (struct pex_obj
*);
317 /* The list of functions we pass to the common routines. */
319 const struct pex_funcs funcs
=
332 /* Return a newly initialized pex_obj structure. */
335 pex_init (int flags
, const char *pname
, const char *tempbase
)
337 return pex_init_common (flags
, pname
, tempbase
, &funcs
);
340 /* Open a file for reading. */
343 pex_unix_open_read (struct pex_obj
*obj ATTRIBUTE_UNUSED
, const char *name
,
344 int binary ATTRIBUTE_UNUSED
)
346 return open (name
, O_RDONLY
);
349 /* Open a file for writing. */
352 pex_unix_open_write (struct pex_obj
*obj ATTRIBUTE_UNUSED
, const char *name
,
353 int binary ATTRIBUTE_UNUSED
, int append
)
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
358 | (append
? O_APPEND
: O_TRUNC
), PUBLIC_MODE
);
364 pex_unix_close (struct pex_obj
*obj ATTRIBUTE_UNUSED
, int fd
)
369 /* Report an error from a child process. We don't use stdio routines,
370 because we might be here due to a vfork call. */
373 pex_child_error (struct pex_obj
*obj
, const char *executable
,
374 const char *errmsg
, int err
)
377 #define writeerr(s) retval |= (write (STDERR_FILE_NO, s, strlen (s)) < 0)
378 writeerr (obj
->pname
);
379 writeerr (": error trying to exec '");
380 writeerr (executable
);
384 writeerr (xstrerror (err
));
387 /* Exit with -2 if the error output failed, too. */
388 _exit (retval
== 0 ? -1 : -2);
391 /* Execute a child. */
393 #if defined(HAVE_SPAWNVE) && defined(HAVE_SPAWNVPE)
394 /* Implementation of pex->exec_child using the Cygwin spawn operation. */
396 /* Subroutine of pex_unix_exec_child. Move OLD_FD to a new file descriptor
397 to be stored in *PNEW_FD, save the flags in *PFLAGS, and arrange for the
398 saved copy to be close-on-exec. Move CHILD_FD into OLD_FD. If CHILD_FD
399 is -1, OLD_FD is to be closed. Return -1 on error. */
402 save_and_install_fd(int *pnew_fd
, int *pflags
, int old_fd
, int child_fd
)
406 flags
= fcntl (old_fd
, F_GETFD
);
408 /* If we could not retrieve the flags, then OLD_FD was not open. */
411 new_fd
= -1, flags
= 0;
412 if (child_fd
>= 0 && dup2 (child_fd
, old_fd
) < 0)
415 /* If we wish to close OLD_FD, just mark it CLOEXEC. */
416 else if (child_fd
== -1)
419 if ((flags
& FD_CLOEXEC
) == 0 && fcntl (old_fd
, F_SETFD
, FD_CLOEXEC
) < 0)
422 /* Otherwise we need to save a copy of OLD_FD before installing CHILD_FD. */
425 #ifdef F_DUPFD_CLOEXEC
426 new_fd
= fcntl (old_fd
, F_DUPFD_CLOEXEC
, 3);
430 /* Prefer F_DUPFD over dup in order to avoid getting a new fd
431 in the range 0-2, right where a new stderr fd might get put. */
432 new_fd
= fcntl (old_fd
, F_DUPFD
, 3);
435 if (fcntl (new_fd
, F_SETFD
, FD_CLOEXEC
) < 0)
438 if (dup2 (child_fd
, old_fd
) < 0)
445 else if (new_fd
!= old_fd
)
451 /* Subroutine of pex_unix_exec_child. Move SAVE_FD back to OLD_FD
452 restoring FLAGS. If SAVE_FD < 0, OLD_FD is to be closed. */
455 restore_fd(int old_fd
, int save_fd
, int flags
)
457 /* For SAVE_FD < 0, all we have to do is restore the
458 "closed-ness" of the original. */
460 return close (old_fd
);
462 /* For SAVE_FD == OLD_FD, all we have to do is restore the
463 original setting of the CLOEXEC flag. */
464 if (save_fd
== old_fd
)
466 if (flags
& FD_CLOEXEC
)
468 return fcntl (old_fd
, F_SETFD
, flags
);
471 /* Otherwise we have to move the descriptor back, restore the flags,
472 and close the saved copy. */
474 if (flags
== FD_CLOEXEC
)
476 if (dup3 (save_fd
, old_fd
, O_CLOEXEC
) < 0)
482 if (dup2 (save_fd
, old_fd
) < 0)
484 if (flags
!= 0 && fcntl (old_fd
, F_SETFD
, flags
) < 0)
487 return close (save_fd
);
491 pex_unix_exec_child (struct pex_obj
*obj ATTRIBUTE_UNUSED
,
492 int flags
, const char *executable
,
493 char * const * argv
, char * const * env
,
494 int in
, int out
, int errdes
, int toclose
,
495 const char **errmsg
, int *err
)
497 int fl_in
= 0, fl_out
= 0, fl_err
= 0, fl_tc
= 0;
498 int save_in
= -1, save_out
= -1, save_err
= -1;
502 if (flags
& PEX_STDERR_TO_STDOUT
)
505 /* We need the three standard file descriptors to be set up as for
506 the child before we perform the spawn. The file descriptors for
507 the parent need to be moved and marked for close-on-exec. */
508 if (in
!= STDIN_FILE_NO
509 && save_and_install_fd (&save_in
, &fl_in
, STDIN_FILE_NO
, in
) < 0)
511 if (out
!= STDOUT_FILE_NO
512 && save_and_install_fd (&save_out
, &fl_out
, STDOUT_FILE_NO
, out
) < 0)
514 if (errdes
!= STDERR_FILE_NO
515 && save_and_install_fd (&save_err
, &fl_err
, STDERR_FILE_NO
, errdes
) < 0)
518 && save_and_install_fd (NULL
, &fl_tc
, toclose
, -1) < 0)
521 /* Now that we've moved the file descriptors for the child into place,
522 close the originals. Be careful not to close any of the standard
523 file descriptors that we just set up. */
526 max
= STDERR_FILE_NO
;
528 max
= STDOUT_FILE_NO
;
535 if (errdes
> max
&& errdes
!= out
)
538 /* If we were not given an environment, use the global environment. */
542 /* Launch the program. If we get EAGAIN (normally out of pid's), try
543 again a few times with increasing backoff times. */
547 typedef const char * const *cc_cp
;
549 if (flags
& PEX_SEARCH
)
550 pid
= spawnvpe (_P_NOWAITO
, executable
, (cc_cp
)argv
, (cc_cp
)env
);
552 pid
= spawnve (_P_NOWAITO
, executable
, (cc_cp
)argv
, (cc_cp
)env
);
559 if (errno
!= EAGAIN
|| ++retries
== 4)
561 sleep (1 << retries
);
564 /* Success. Restore the parent's file descriptors that we saved above. */
566 && restore_fd (toclose
, toclose
, fl_tc
) < 0)
568 if (in
!= STDIN_FILE_NO
569 && restore_fd (STDIN_FILE_NO
, save_in
, fl_in
) < 0)
571 if (out
!= STDOUT_FILE_NO
572 && restore_fd (STDOUT_FILE_NO
, save_out
, fl_out
) < 0)
574 if (errdes
!= STDERR_FILE_NO
575 && restore_fd (STDERR_FILE_NO
, save_err
, fl_err
) < 0)
587 /* Implementation of pex->exec_child using standard vfork + exec. */
590 pex_unix_exec_child (struct pex_obj
*obj
, int flags
, const char *executable
,
591 char * const * argv
, char * const * env
,
592 int in
, int out
, int errdes
,
593 int toclose
, const char **errmsg
, int *err
)
597 /* We declare these to be volatile to avoid warnings from gcc about
598 them being clobbered by vfork. */
599 volatile int sleep_interval
;
600 volatile int retries
;
602 /* We vfork and then set environ in the child before calling execvp.
603 This clobbers the parent's environ so we need to restore it.
604 It would be nice to use one of the exec* functions that takes an
605 environment as a parameter, but that may have portability issues. */
606 char **save_environ
= environ
;
610 for (retries
= 0; retries
< 4; ++retries
)
615 sleep (sleep_interval
);
623 *errmsg
= VFORK_STRING
;
628 if (in
!= STDIN_FILE_NO
)
630 if (dup2 (in
, STDIN_FILE_NO
) < 0)
631 pex_child_error (obj
, executable
, "dup2", errno
);
633 pex_child_error (obj
, executable
, "close", errno
);
635 if (out
!= STDOUT_FILE_NO
)
637 if (dup2 (out
, STDOUT_FILE_NO
) < 0)
638 pex_child_error (obj
, executable
, "dup2", errno
);
640 pex_child_error (obj
, executable
, "close", errno
);
642 if (errdes
!= STDERR_FILE_NO
)
644 if (dup2 (errdes
, STDERR_FILE_NO
) < 0)
645 pex_child_error (obj
, executable
, "dup2", errno
);
646 if (close (errdes
) < 0)
647 pex_child_error (obj
, executable
, "close", errno
);
651 if (close (toclose
) < 0)
652 pex_child_error (obj
, executable
, "close", errno
);
654 if ((flags
& PEX_STDERR_TO_STDOUT
) != 0)
656 if (dup2 (STDOUT_FILE_NO
, STDERR_FILE_NO
) < 0)
657 pex_child_error (obj
, executable
, "dup2", errno
);
662 /* NOTE: In a standard vfork implementation this clobbers the
663 parent's copy of environ "too" (in reality there's only one copy).
664 This is ok as we restore it below. */
665 environ
= (char**) env
;
668 if ((flags
& PEX_SEARCH
) != 0)
670 execvp (executable
, to_ptr32 (argv
));
671 pex_child_error (obj
, executable
, "execvp", errno
);
675 execv (executable
, to_ptr32 (argv
));
676 pex_child_error (obj
, executable
, "execv", errno
);
683 /* Parent process. */
686 Note that the parent either doesn't run until the child execs/exits
687 (standard vfork behaviour), or if it does run then vfork is behaving
688 more like fork. In either case we needn't worry about clobbering
689 the child's copy of environ. */
690 environ
= save_environ
;
692 if (in
!= STDIN_FILE_NO
)
701 if (out
!= STDOUT_FILE_NO
)
710 if (errdes
!= STDERR_FILE_NO
)
712 if (close (errdes
) < 0)
725 /* Wait for a child process to complete. */
728 pex_unix_wait (struct pex_obj
*obj
, pid_t pid
, int *status
,
729 struct pex_time
*time
, int done
, const char **errmsg
,
732 /* If we are cleaning up when the caller didn't retrieve process
733 status for some reason, encourage the process to go away. */
737 if (pex_wait (obj
, pid
, status
, time
) < 0)
750 pex_unix_pipe (struct pex_obj
*obj ATTRIBUTE_UNUSED
, int *p
,
751 int binary ATTRIBUTE_UNUSED
)
756 /* Get a FILE pointer to read from a file descriptor. */
759 pex_unix_fdopenr (struct pex_obj
*obj ATTRIBUTE_UNUSED
, int fd
,
760 int binary ATTRIBUTE_UNUSED
)
762 return fdopen (fd
, "r");
766 pex_unix_fdopenw (struct pex_obj
*obj ATTRIBUTE_UNUSED
, int fd
,
767 int binary ATTRIBUTE_UNUSED
)
769 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
) < 0)
771 return fdopen (fd
, "w");
775 pex_unix_cleanup (struct pex_obj
*obj ATTRIBUTE_UNUSED
)
777 #if !defined (HAVE_WAIT4) && !defined (HAVE_WAITPID)
778 while (obj
->sysdep
!= NULL
)
780 struct status_list
*this;
781 struct status_list
*next
;
783 this = (struct status_list
*) obj
->sysdep
;
786 obj
->sysdep
= (void *) next
;