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, 2015 Free Software Foundation, Inc.
7 This file is part of the libiberty library.
8 Libiberty is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public
10 License as published by the Free Software Foundation; either
11 version 2 of the License, or (at your option) any later version.
13 Libiberty is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Library General Public License for more details.
18 You should have received a copy of the GNU Library General Public
19 License along with libiberty; see the file COPYING.LIB. If not,
20 write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
21 Boston, MA 02110-1301, USA. */
24 #include "libiberty.h"
25 #include "pex-common.h"
31 #ifdef NEED_DECLARATION_ERRNO
44 #include <sys/types.h>
49 #ifdef HAVE_SYS_WAIT_H
54 #include <sys/resource.h>
56 #ifdef HAVE_SYS_STAT_H
63 #ifdef vfork /* Autoconf may define this to fork for us. */
64 # define VFORK_STRING "fork"
66 # define VFORK_STRING "vfork"
71 #if defined(VMS) && defined (__LONG_POINTERS)
73 typedef char * __char_ptr32
74 __attribute__ ((mode (SI
)));
77 typedef __char_ptr32
*__char_ptr_char_ptr32
78 __attribute__ ((mode (SI
)));
80 /* Return a 32 bit pointer to an array of 32 bit pointers
81 given a 64 bit pointer to an array of 64 bit pointers. */
83 static __char_ptr_char_ptr32
84 to_ptr32 (char **ptr64
)
87 __char_ptr_char_ptr32 short_argv
;
89 /* Count number of arguments. */
90 for (argc
= 0; ptr64
[argc
] != NULL
; argc
++)
93 /* Reallocate argv with 32 bit pointers. */
94 short_argv
= (__char_ptr_char_ptr32
) decc$malloc
95 (sizeof (__char_ptr32
) * (argc
+ 1));
97 for (argc
= 0; ptr64
[argc
] != NULL
; argc
++)
98 short_argv
[argc
] = (__char_ptr32
) decc$
strdup (ptr64
[argc
]);
100 short_argv
[argc
] = (__char_ptr32
) 0;
105 #define to_ptr32(argv) argv
108 /* File mode to use for private and world-readable files. */
110 #if defined (S_IRUSR) && defined (S_IWUSR) && defined (S_IRGRP) && defined (S_IWGRP) && defined (S_IROTH) && defined (S_IWOTH)
111 #define PUBLIC_MODE \
112 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
114 #define PUBLIC_MODE 0666
117 /* Get the exit status of a particular process, and optionally get the
118 time that it took. This is simple if we have wait4, slightly
119 harder if we have waitpid, and is a pain if we only have wait. */
121 static pid_t
pex_wait (struct pex_obj
*, pid_t
, int *, struct pex_time
*);
126 pex_wait (struct pex_obj
*obj ATTRIBUTE_UNUSED
, pid_t pid
, int *status
,
127 struct pex_time
*time
)
134 return waitpid (pid
, status
, 0);
137 ret
= wait4 (pid
, status
, 0, &r
);
141 time
->user_seconds
= r
.ru_utime
.tv_sec
;
142 time
->user_microseconds
= r
.ru_utime
.tv_usec
;
143 time
->system_seconds
= r
.ru_stime
.tv_sec
;
144 time
->system_microseconds
= r
.ru_stime
.tv_usec
;
150 #else /* ! defined (HAVE_WAIT4) */
154 #ifndef HAVE_GETRUSAGE
157 pex_wait (struct pex_obj
*obj ATTRIBUTE_UNUSED
, pid_t pid
, int *status
,
158 struct pex_time
*time
)
161 memset (time
, 0, sizeof (struct pex_time
));
162 return waitpid (pid
, status
, 0);
165 #else /* defined (HAVE_GETRUSAGE) */
168 pex_wait (struct pex_obj
*obj ATTRIBUTE_UNUSED
, pid_t pid
, int *status
,
169 struct pex_time
*time
)
171 struct rusage r1
, r2
;
175 return waitpid (pid
, status
, 0);
177 getrusage (RUSAGE_CHILDREN
, &r1
);
179 ret
= waitpid (pid
, status
, 0);
183 getrusage (RUSAGE_CHILDREN
, &r2
);
185 time
->user_seconds
= r2
.ru_utime
.tv_sec
- r1
.ru_utime
.tv_sec
;
186 time
->user_microseconds
= r2
.ru_utime
.tv_usec
- r1
.ru_utime
.tv_usec
;
187 if (r2
.ru_utime
.tv_usec
< r1
.ru_utime
.tv_usec
)
189 --time
->user_seconds
;
190 time
->user_microseconds
+= 1000000;
193 time
->system_seconds
= r2
.ru_stime
.tv_sec
- r1
.ru_stime
.tv_sec
;
194 time
->system_microseconds
= r2
.ru_stime
.tv_usec
- r1
.ru_stime
.tv_usec
;
195 if (r2
.ru_stime
.tv_usec
< r1
.ru_stime
.tv_usec
)
197 --time
->system_seconds
;
198 time
->system_microseconds
+= 1000000;
204 #endif /* defined (HAVE_GETRUSAGE) */
206 #else /* ! defined (HAVE_WAITPID) */
210 struct status_list
*next
;
213 struct pex_time time
;
217 pex_wait (struct pex_obj
*obj
, pid_t pid
, int *status
, struct pex_time
*time
)
219 struct status_list
**pp
;
221 for (pp
= (struct status_list
**) &obj
->sysdep
;
225 if ((*pp
)->pid
== pid
)
227 struct status_list
*p
;
242 struct status_list
*psl
;
244 #ifdef HAVE_GETRUSAGE
245 struct rusage r1
, r2
;
250 #ifdef HAVE_GETRUSAGE
251 getrusage (RUSAGE_CHILDREN
, &r1
);
253 memset (&pt
, 0, sizeof (struct pex_time
));
257 cpid
= wait (status
);
259 #ifdef HAVE_GETRUSAGE
260 if (time
!= NULL
&& cpid
>= 0)
262 getrusage (RUSAGE_CHILDREN
, &r2
);
264 pt
.user_seconds
= r2
.ru_utime
.tv_sec
- r1
.ru_utime
.tv_sec
;
265 pt
.user_microseconds
= r2
.ru_utime
.tv_usec
- r1
.ru_utime
.tv_usec
;
266 if (pt
.user_microseconds
< 0)
269 pt
.user_microseconds
+= 1000000;
272 pt
.system_seconds
= r2
.ru_stime
.tv_sec
- r1
.ru_stime
.tv_sec
;
273 pt
.system_microseconds
= r2
.ru_stime
.tv_usec
- r1
.ru_stime
.tv_usec
;
274 if (pt
.system_microseconds
< 0)
277 pt
.system_microseconds
+= 1000000;
282 if (cpid
< 0 || cpid
== pid
)
289 psl
= XNEW (struct status_list
);
291 psl
->status
= *status
;
294 psl
->next
= (struct status_list
*) obj
->sysdep
;
295 obj
->sysdep
= (void *) psl
;
299 #endif /* ! defined (HAVE_WAITPID) */
300 #endif /* ! defined (HAVE_WAIT4) */
302 static void pex_child_error (struct pex_obj
*, const char *, const char *, int)
304 static int pex_unix_open_read (struct pex_obj
*, const char *, int);
305 static int pex_unix_open_write (struct pex_obj
*, const char *, int, int);
306 static pid_t
pex_unix_exec_child (struct pex_obj
*, int, const char *,
307 char * const *, char * const *,
309 const char **, int *);
310 static int pex_unix_close (struct pex_obj
*, int);
311 static int pex_unix_wait (struct pex_obj
*, pid_t
, int *, struct pex_time
*,
312 int, const char **, int *);
313 static int pex_unix_pipe (struct pex_obj
*, int *, int);
314 static FILE *pex_unix_fdopenr (struct pex_obj
*, int, int);
315 static FILE *pex_unix_fdopenw (struct pex_obj
*, int, int);
316 static void pex_unix_cleanup (struct pex_obj
*);
318 /* The list of functions we pass to the common routines. */
320 const struct pex_funcs funcs
=
333 /* Return a newly initialized pex_obj structure. */
336 pex_init (int flags
, const char *pname
, const char *tempbase
)
338 return pex_init_common (flags
, pname
, tempbase
, &funcs
);
341 /* Open a file for reading. */
344 pex_unix_open_read (struct pex_obj
*obj ATTRIBUTE_UNUSED
, const char *name
,
345 int binary ATTRIBUTE_UNUSED
)
347 return open (name
, O_RDONLY
);
350 /* Open a file for writing. */
353 pex_unix_open_write (struct pex_obj
*obj ATTRIBUTE_UNUSED
, const char *name
,
354 int binary ATTRIBUTE_UNUSED
, int append
)
356 /* Note that we can't use O_EXCL here because gcc may have already
357 created the temporary file via make_temp_file. */
358 return open (name
, O_WRONLY
| O_CREAT
359 | (append
? O_APPEND
: O_TRUNC
), PUBLIC_MODE
);
365 pex_unix_close (struct pex_obj
*obj ATTRIBUTE_UNUSED
, int fd
)
370 /* Report an error from a child process. We don't use stdio routines,
371 because we might be here due to a vfork call. */
374 pex_child_error (struct pex_obj
*obj
, const char *executable
,
375 const char *errmsg
, int err
)
378 #define writeerr(s) retval |= (write (STDERR_FILE_NO, s, strlen (s)) < 0)
379 writeerr (obj
->pname
);
380 writeerr (": error trying to exec '");
381 writeerr (executable
);
385 writeerr (xstrerror (err
));
388 /* Exit with -2 if the error output failed, too. */
389 _exit (retval
== 0 ? -1 : -2);
392 /* Execute a child. */
394 #if defined(HAVE_SPAWNVE) && defined(HAVE_SPAWNVPE)
395 /* Implementation of pex->exec_child using the Cygwin spawn operation. */
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. */
403 save_and_install_fd(int *pnew_fd
, int *pflags
, int old_fd
, int child_fd
)
407 flags
= fcntl (old_fd
, F_GETFD
);
409 /* If we could not retrieve the flags, then OLD_FD was not open. */
412 new_fd
= -1, flags
= 0;
413 if (child_fd
>= 0 && dup2 (child_fd
, old_fd
) < 0)
416 /* If we wish to close OLD_FD, just mark it CLOEXEC. */
417 else if (child_fd
== -1)
420 if ((flags
& FD_CLOEXEC
) == 0 && fcntl (old_fd
, F_SETFD
, FD_CLOEXEC
) < 0)
423 /* Otherwise we need to save a copy of OLD_FD before installing CHILD_FD. */
426 #ifdef F_DUPFD_CLOEXEC
427 new_fd
= fcntl (old_fd
, F_DUPFD_CLOEXEC
, 3);
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);
436 if (fcntl (new_fd
, F_SETFD
, FD_CLOEXEC
) < 0)
439 if (dup2 (child_fd
, old_fd
) < 0)
446 else if (new_fd
!= old_fd
)
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. */
456 restore_fd(int old_fd
, int save_fd
, int flags
)
458 /* For SAVE_FD < 0, all we have to do is restore the
459 "closed-ness" of the original. */
461 return close (old_fd
);
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
)
467 if (flags
& FD_CLOEXEC
)
469 return fcntl (old_fd
, F_SETFD
, flags
);
472 /* Otherwise we have to move the descriptor back, restore the flags,
473 and close the saved copy. */
475 if (flags
== FD_CLOEXEC
)
477 if (dup3 (save_fd
, old_fd
, O_CLOEXEC
) < 0)
483 if (dup2 (save_fd
, old_fd
) < 0)
485 if (flags
!= 0 && fcntl (old_fd
, F_SETFD
, flags
) < 0)
488 return close (save_fd
);
492 pex_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
)
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;
503 if (flags
& PEX_STDERR_TO_STDOUT
)
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)
512 if (out
!= STDOUT_FILE_NO
513 && save_and_install_fd (&save_out
, &fl_out
, STDOUT_FILE_NO
, out
) < 0)
515 if (errdes
!= STDERR_FILE_NO
516 && save_and_install_fd (&save_err
, &fl_err
, STDERR_FILE_NO
, errdes
) < 0)
519 && save_and_install_fd (NULL
, &fl_tc
, toclose
, -1) < 0)
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. */
527 max
= STDERR_FILE_NO
;
529 max
= STDOUT_FILE_NO
;
536 if (errdes
> max
&& errdes
!= out
)
539 /* If we were not given an environment, use the global environment. */
543 /* Launch the program. If we get EAGAIN (normally out of pid's), try
544 again a few times with increasing backoff times. */
548 typedef const char * const *cc_cp
;
550 if (flags
& PEX_SEARCH
)
551 pid
= spawnvpe (_P_NOWAITO
, executable
, (cc_cp
)argv
, (cc_cp
)env
);
553 pid
= spawnve (_P_NOWAITO
, executable
, (cc_cp
)argv
, (cc_cp
)env
);
560 if (errno
!= EAGAIN
|| ++retries
== 4)
562 sleep (1 << retries
);
565 /* Success. Restore the parent's file descriptors that we saved above. */
567 && restore_fd (toclose
, toclose
, fl_tc
) < 0)
569 if (in
!= STDIN_FILE_NO
570 && restore_fd (STDIN_FILE_NO
, save_in
, fl_in
) < 0)
572 if (out
!= STDOUT_FILE_NO
573 && restore_fd (STDOUT_FILE_NO
, save_out
, fl_out
) < 0)
575 if (errdes
!= STDERR_FILE_NO
576 && restore_fd (STDERR_FILE_NO
, save_err
, fl_err
) < 0)
588 /* Implementation of pex->exec_child using standard vfork + exec. */
591 pex_unix_exec_child (struct pex_obj
*obj
, int flags
, const char *executable
,
592 char * const * argv
, char * const * env
,
593 int in
, int out
, int errdes
,
594 int toclose
, const char **errmsg
, int *err
)
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
;
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
;
611 for (retries
= 0; retries
< 4; ++retries
)
616 sleep (sleep_interval
);
624 *errmsg
= VFORK_STRING
;
629 if (in
!= STDIN_FILE_NO
)
631 if (dup2 (in
, STDIN_FILE_NO
) < 0)
632 pex_child_error (obj
, executable
, "dup2", errno
);
634 pex_child_error (obj
, executable
, "close", errno
);
636 if (out
!= STDOUT_FILE_NO
)
638 if (dup2 (out
, STDOUT_FILE_NO
) < 0)
639 pex_child_error (obj
, executable
, "dup2", errno
);
641 pex_child_error (obj
, executable
, "close", errno
);
643 if (errdes
!= STDERR_FILE_NO
)
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
);
652 if (close (toclose
) < 0)
653 pex_child_error (obj
, executable
, "close", errno
);
655 if ((flags
& PEX_STDERR_TO_STDOUT
) != 0)
657 if (dup2 (STDOUT_FILE_NO
, STDERR_FILE_NO
) < 0)
658 pex_child_error (obj
, executable
, "dup2", errno
);
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
;
669 if ((flags
& PEX_SEARCH
) != 0)
671 execvp (executable
, to_ptr32 (argv
));
672 pex_child_error (obj
, executable
, "execvp", errno
);
676 execv (executable
, to_ptr32 (argv
));
677 pex_child_error (obj
, executable
, "execv", errno
);
684 /* Parent process. */
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
;
693 if (in
!= STDIN_FILE_NO
)
702 if (out
!= STDOUT_FILE_NO
)
711 if (errdes
!= STDERR_FILE_NO
)
713 if (close (errdes
) < 0)
726 /* Wait for a child process to complete. */
729 pex_unix_wait (struct pex_obj
*obj
, pid_t pid
, int *status
,
730 struct pex_time
*time
, int done
, const char **errmsg
,
733 /* If we are cleaning up when the caller didn't retrieve process
734 status for some reason, encourage the process to go away. */
738 if (pex_wait (obj
, pid
, status
, time
) < 0)
751 pex_unix_pipe (struct pex_obj
*obj ATTRIBUTE_UNUSED
, int *p
,
752 int binary ATTRIBUTE_UNUSED
)
757 /* Get a FILE pointer to read from a file descriptor. */
760 pex_unix_fdopenr (struct pex_obj
*obj ATTRIBUTE_UNUSED
, int fd
,
761 int binary ATTRIBUTE_UNUSED
)
763 return fdopen (fd
, "r");
767 pex_unix_fdopenw (struct pex_obj
*obj ATTRIBUTE_UNUSED
, int fd
,
768 int binary ATTRIBUTE_UNUSED
)
770 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
) < 0)
772 return fdopen (fd
, "w");
776 pex_unix_cleanup (struct pex_obj
*obj ATTRIBUTE_UNUSED
)
778 #if !defined (HAVE_WAIT4) && !defined (HAVE_WAITPID)
779 while (obj
->sysdep
!= NULL
)
781 struct status_list
*this;
782 struct status_list
*next
;
784 this = (struct status_list
*) obj
->sysdep
;
787 obj
->sysdep
= (void *) next
;