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 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"
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 for (argc
=0; ptr64
[argc
]; argc
++);
90 /* Reallocate argv with 32 bit pointers. */
91 short_argv
= (__char_ptr_char_ptr32
) decc$malloc
92 (sizeof (__char_ptr32
) * (argc
+ 1));
94 for (argc
=0; ptr64
[argc
]; argc
++)
95 short_argv
[argc
] = (__char_ptr32
) decc$
strdup (ptr64
[argc
]);
97 short_argv
[argc
] = (__char_ptr32
) 0;
102 #define to_ptr32(argv) argv
105 /* File mode to use for private and world-readable files. */
107 #if defined (S_IRUSR) && defined (S_IWUSR) && defined (S_IRGRP) && defined (S_IWGRP) && defined (S_IROTH) && defined (S_IWOTH)
108 #define PUBLIC_MODE \
109 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
111 #define PUBLIC_MODE 0666
114 /* Get the exit status of a particular process, and optionally get the
115 time that it took. This is simple if we have wait4, slightly
116 harder if we have waitpid, and is a pain if we only have wait. */
118 static pid_t
pex_wait (struct pex_obj
*, pid_t
, int *, struct pex_time
*);
123 pex_wait (struct pex_obj
*obj ATTRIBUTE_UNUSED
, pid_t pid
, int *status
,
124 struct pex_time
*time
)
131 return waitpid (pid
, status
, 0);
134 ret
= wait4 (pid
, status
, 0, &r
);
138 time
->user_seconds
= r
.ru_utime
.tv_sec
;
139 time
->user_microseconds
= r
.ru_utime
.tv_usec
;
140 time
->system_seconds
= r
.ru_stime
.tv_sec
;
141 time
->system_microseconds
= r
.ru_stime
.tv_usec
;
147 #else /* ! defined (HAVE_WAIT4) */
151 #ifndef HAVE_GETRUSAGE
154 pex_wait (struct pex_obj
*obj ATTRIBUTE_UNUSED
, pid_t pid
, int *status
,
155 struct pex_time
*time
)
158 memset (time
, 0, sizeof (struct pex_time
));
159 return waitpid (pid
, status
, 0);
162 #else /* defined (HAVE_GETRUSAGE) */
165 pex_wait (struct pex_obj
*obj ATTRIBUTE_UNUSED
, pid_t pid
, int *status
,
166 struct pex_time
*time
)
168 struct rusage r1
, r2
;
172 return waitpid (pid
, status
, 0);
174 getrusage (RUSAGE_CHILDREN
, &r1
);
176 ret
= waitpid (pid
, status
, 0);
180 getrusage (RUSAGE_CHILDREN
, &r2
);
182 time
->user_seconds
= r2
.ru_utime
.tv_sec
- r1
.ru_utime
.tv_sec
;
183 time
->user_microseconds
= r2
.ru_utime
.tv_usec
- r1
.ru_utime
.tv_usec
;
184 if (r2
.ru_utime
.tv_usec
< r1
.ru_utime
.tv_usec
)
186 --time
->user_seconds
;
187 time
->user_microseconds
+= 1000000;
190 time
->system_seconds
= r2
.ru_stime
.tv_sec
- r1
.ru_stime
.tv_sec
;
191 time
->system_microseconds
= r2
.ru_stime
.tv_usec
- r1
.ru_stime
.tv_usec
;
192 if (r2
.ru_stime
.tv_usec
< r1
.ru_stime
.tv_usec
)
194 --time
->system_seconds
;
195 time
->system_microseconds
+= 1000000;
201 #endif /* defined (HAVE_GETRUSAGE) */
203 #else /* ! defined (HAVE_WAITPID) */
207 struct status_list
*next
;
210 struct pex_time time
;
214 pex_wait (struct pex_obj
*obj
, pid_t pid
, int *status
, struct pex_time
*time
)
216 struct status_list
**pp
;
218 for (pp
= (struct status_list
**) &obj
->sysdep
;
222 if ((*pp
)->pid
== pid
)
224 struct status_list
*p
;
239 struct status_list
*psl
;
241 #ifdef HAVE_GETRUSAGE
242 struct rusage r1
, r2
;
247 #ifdef HAVE_GETRUSAGE
248 getrusage (RUSAGE_CHILDREN
, &r1
);
250 memset (&pt
, 0, sizeof (struct pex_time
));
254 cpid
= wait (status
);
256 #ifdef HAVE_GETRUSAGE
257 if (time
!= NULL
&& cpid
>= 0)
259 getrusage (RUSAGE_CHILDREN
, &r2
);
261 pt
.user_seconds
= r2
.ru_utime
.tv_sec
- r1
.ru_utime
.tv_sec
;
262 pt
.user_microseconds
= r2
.ru_utime
.tv_usec
- r1
.ru_utime
.tv_usec
;
263 if (pt
.user_microseconds
< 0)
266 pt
.user_microseconds
+= 1000000;
269 pt
.system_seconds
= r2
.ru_stime
.tv_sec
- r1
.ru_stime
.tv_sec
;
270 pt
.system_microseconds
= r2
.ru_stime
.tv_usec
- r1
.ru_stime
.tv_usec
;
271 if (pt
.system_microseconds
< 0)
274 pt
.system_microseconds
+= 1000000;
279 if (cpid
< 0 || cpid
== pid
)
286 psl
= XNEW (struct status_list
);
288 psl
->status
= *status
;
291 psl
->next
= (struct status_list
*) obj
->sysdep
;
292 obj
->sysdep
= (void *) psl
;
296 #endif /* ! defined (HAVE_WAITPID) */
297 #endif /* ! defined (HAVE_WAIT4) */
299 static void pex_child_error (struct pex_obj
*, const char *, const char *, int)
301 static int pex_unix_open_read (struct pex_obj
*, const char *, int);
302 static int pex_unix_open_write (struct pex_obj
*, const char *, int);
303 static pid_t
pex_unix_exec_child (struct pex_obj
*, int, const char *,
304 char * const *, char * const *,
306 const char **, int *);
307 static int pex_unix_close (struct pex_obj
*, int);
308 static int pex_unix_wait (struct pex_obj
*, pid_t
, int *, struct pex_time
*,
309 int, const char **, int *);
310 static int pex_unix_pipe (struct pex_obj
*, int *, int);
311 static FILE *pex_unix_fdopenr (struct pex_obj
*, int, int);
312 static FILE *pex_unix_fdopenw (struct pex_obj
*, int, int);
313 static void pex_unix_cleanup (struct pex_obj
*);
315 /* The list of functions we pass to the common routines. */
317 const struct pex_funcs funcs
=
330 /* Return a newly initialized pex_obj structure. */
333 pex_init (int flags
, const char *pname
, const char *tempbase
)
335 return pex_init_common (flags
, pname
, tempbase
, &funcs
);
338 /* Open a file for reading. */
341 pex_unix_open_read (struct pex_obj
*obj ATTRIBUTE_UNUSED
, const char *name
,
342 int binary ATTRIBUTE_UNUSED
)
344 return open (name
, O_RDONLY
);
347 /* Open a file for writing. */
350 pex_unix_open_write (struct pex_obj
*obj ATTRIBUTE_UNUSED
, const char *name
,
351 int binary ATTRIBUTE_UNUSED
)
353 /* Note that we can't use O_EXCL here because gcc may have already
354 created the temporary file via make_temp_file. */
355 return open (name
, O_WRONLY
| O_CREAT
| O_TRUNC
, PUBLIC_MODE
);
361 pex_unix_close (struct pex_obj
*obj ATTRIBUTE_UNUSED
, int fd
)
366 /* Report an error from a child process. We don't use stdio routines,
367 because we might be here due to a vfork call. */
370 pex_child_error (struct pex_obj
*obj
, const char *executable
,
371 const char *errmsg
, int err
)
374 #define writeerr(s) retval |= (write (STDERR_FILE_NO, s, strlen (s)) < 0)
375 writeerr (obj
->pname
);
376 writeerr (": error trying to exec '");
377 writeerr (executable
);
381 writeerr (xstrerror (err
));
384 /* Exit with -2 if the error output failed, too. */
385 _exit (retval
== 0 ? -1 : -2);
388 /* Execute a child. */
390 extern char **environ
;
392 #if defined(HAVE_SPAWNVE) && defined(HAVE_SPAWNVPE)
393 /* Implementation of pex->exec_child using the Cygwin spawn operation. */
395 /* Subroutine of pex_unix_exec_child. Move OLD_FD to a new file descriptor
396 to be stored in *PNEW_FD, save the flags in *PFLAGS, and arrange for the
397 saved copy to be close-on-exec. Move CHILD_FD into OLD_FD. If CHILD_FD
398 is -1, OLD_FD is to be closed. Return -1 on error. */
401 save_and_install_fd(int *pnew_fd
, int *pflags
, int old_fd
, int child_fd
)
405 flags
= fcntl (old_fd
, F_GETFD
);
407 /* If we could not retrieve the flags, then OLD_FD was not open. */
410 new_fd
= -1, flags
= 0;
411 if (child_fd
>= 0 && dup2 (child_fd
, old_fd
) < 0)
414 /* If we wish to close OLD_FD, just mark it CLOEXEC. */
415 else if (child_fd
== -1)
418 if ((flags
& FD_CLOEXEC
) == 0 && fcntl (old_fd
, F_SETFD
, FD_CLOEXEC
) < 0)
421 /* Otherwise we need to save a copy of OLD_FD before installing CHILD_FD. */
424 #ifdef F_DUPFD_CLOEXEC
425 new_fd
= fcntl (old_fd
, F_DUPFD_CLOEXEC
, 3);
429 /* Prefer F_DUPFD over dup in order to avoid getting a new fd
430 in the range 0-2, right where a new stderr fd might get put. */
431 new_fd
= fcntl (old_fd
, F_DUPFD
, 3);
434 if (fcntl (new_fd
, F_SETFD
, FD_CLOEXEC
) < 0)
437 if (dup2 (child_fd
, old_fd
) < 0)
444 else if (new_fd
!= old_fd
)
450 /* Subroutine of pex_unix_exec_child. Move SAVE_FD back to OLD_FD
451 restoring FLAGS. If SAVE_FD < 0, OLD_FD is to be closed. */
454 restore_fd(int old_fd
, int save_fd
, int flags
)
456 /* For SAVE_FD < 0, all we have to do is restore the
457 "closed-ness" of the original. */
459 return close (old_fd
);
461 /* For SAVE_FD == OLD_FD, all we have to do is restore the
462 original setting of the CLOEXEC flag. */
463 if (save_fd
== old_fd
)
465 if (flags
& FD_CLOEXEC
)
467 return fcntl (old_fd
, F_SETFD
, flags
);
470 /* Otherwise we have to move the descriptor back, restore the flags,
471 and close the saved copy. */
473 if (flags
== FD_CLOEXEC
)
475 if (dup3 (save_fd
, old_fd
, O_CLOEXEC
) < 0)
481 if (dup2 (save_fd
, old_fd
) < 0)
483 if (flags
!= 0 && fcntl (old_fd
, F_SETFD
, flags
) < 0)
486 return close (save_fd
);
490 pex_unix_exec_child (struct pex_obj
*obj ATTRIBUTE_UNUSED
,
491 int flags
, const char *executable
,
492 char * const * argv
, char * const * env
,
493 int in
, int out
, int errdes
, int toclose
,
494 const char **errmsg
, int *err
)
496 int fl_in
= 0, fl_out
= 0, fl_err
= 0, fl_tc
= 0;
497 int save_in
= -1, save_out
= -1, save_err
= -1;
501 if (flags
& PEX_STDERR_TO_STDOUT
)
504 /* We need the three standard file descriptors to be set up as for
505 the child before we perform the spawn. The file descriptors for
506 the parent need to be moved and marked for close-on-exec. */
507 if (in
!= STDIN_FILE_NO
508 && save_and_install_fd (&save_in
, &fl_in
, STDIN_FILE_NO
, in
) < 0)
510 if (out
!= STDOUT_FILE_NO
511 && save_and_install_fd (&save_out
, &fl_out
, STDOUT_FILE_NO
, out
) < 0)
513 if (errdes
!= STDERR_FILE_NO
514 && save_and_install_fd (&save_err
, &fl_err
, STDERR_FILE_NO
, errdes
) < 0)
517 && save_and_install_fd (NULL
, &fl_tc
, toclose
, -1) < 0)
520 /* Now that we've moved the file descriptors for the child into place,
521 close the originals. Be careful not to close any of the standard
522 file descriptors that we just set up. */
525 max
= STDERR_FILE_NO
;
527 max
= STDOUT_FILE_NO
;
534 if (errdes
> max
&& errdes
!= out
)
537 /* If we were not given an environment, use the global environment. */
541 /* Launch the program. If we get EAGAIN (normally out of pid's), try
542 again a few times with increasing backoff times. */
546 typedef const char * const *cc_cp
;
548 if (flags
& PEX_SEARCH
)
549 pid
= spawnvpe (_P_NOWAITO
, executable
, (cc_cp
)argv
, (cc_cp
)env
);
551 pid
= spawnve (_P_NOWAITO
, executable
, (cc_cp
)argv
, (cc_cp
)env
);
558 if (errno
!= EAGAIN
|| ++retries
== 4)
560 sleep (1 << retries
);
563 /* Success. Restore the parent's file descriptors that we saved above. */
565 && restore_fd (toclose
, toclose
, fl_tc
) < 0)
567 if (in
!= STDIN_FILE_NO
568 && restore_fd (STDIN_FILE_NO
, save_in
, fl_in
) < 0)
570 if (out
!= STDOUT_FILE_NO
571 && restore_fd (STDOUT_FILE_NO
, save_out
, fl_out
) < 0)
573 if (errdes
!= STDERR_FILE_NO
574 && restore_fd (STDERR_FILE_NO
, save_err
, fl_err
) < 0)
586 /* Implementation of pex->exec_child using standard vfork + exec. */
589 pex_unix_exec_child (struct pex_obj
*obj
, int flags
, const char *executable
,
590 char * const * argv
, char * const * env
,
591 int in
, int out
, int errdes
,
592 int toclose
, const char **errmsg
, int *err
)
596 /* We declare these to be volatile to avoid warnings from gcc about
597 them being clobbered by vfork. */
598 volatile int sleep_interval
;
599 volatile int retries
;
601 /* We vfork and then set environ in the child before calling execvp.
602 This clobbers the parent's environ so we need to restore it.
603 It would be nice to use one of the exec* functions that takes an
604 environment as a parameter, but that may have portability issues. */
605 char **save_environ
= environ
;
609 for (retries
= 0; retries
< 4; ++retries
)
614 sleep (sleep_interval
);
622 *errmsg
= VFORK_STRING
;
627 if (in
!= STDIN_FILE_NO
)
629 if (dup2 (in
, STDIN_FILE_NO
) < 0)
630 pex_child_error (obj
, executable
, "dup2", errno
);
632 pex_child_error (obj
, executable
, "close", errno
);
634 if (out
!= STDOUT_FILE_NO
)
636 if (dup2 (out
, STDOUT_FILE_NO
) < 0)
637 pex_child_error (obj
, executable
, "dup2", errno
);
639 pex_child_error (obj
, executable
, "close", errno
);
641 if (errdes
!= STDERR_FILE_NO
)
643 if (dup2 (errdes
, STDERR_FILE_NO
) < 0)
644 pex_child_error (obj
, executable
, "dup2", errno
);
645 if (close (errdes
) < 0)
646 pex_child_error (obj
, executable
, "close", errno
);
650 if (close (toclose
) < 0)
651 pex_child_error (obj
, executable
, "close", errno
);
653 if ((flags
& PEX_STDERR_TO_STDOUT
) != 0)
655 if (dup2 (STDOUT_FILE_NO
, STDERR_FILE_NO
) < 0)
656 pex_child_error (obj
, executable
, "dup2", errno
);
661 /* NOTE: In a standard vfork implementation this clobbers the
662 parent's copy of environ "too" (in reality there's only one copy).
663 This is ok as we restore it below. */
664 environ
= (char**) env
;
667 if ((flags
& PEX_SEARCH
) != 0)
669 execvp (executable
, to_ptr32 (argv
));
670 pex_child_error (obj
, executable
, "execvp", errno
);
674 execv (executable
, to_ptr32 (argv
));
675 pex_child_error (obj
, executable
, "execv", errno
);
682 /* Parent process. */
685 Note that the parent either doesn't run until the child execs/exits
686 (standard vfork behaviour), or if it does run then vfork is behaving
687 more like fork. In either case we needn't worry about clobbering
688 the child's copy of environ. */
689 environ
= save_environ
;
691 if (in
!= STDIN_FILE_NO
)
700 if (out
!= STDOUT_FILE_NO
)
709 if (errdes
!= STDERR_FILE_NO
)
711 if (close (errdes
) < 0)
724 /* Wait for a child process to complete. */
727 pex_unix_wait (struct pex_obj
*obj
, pid_t pid
, int *status
,
728 struct pex_time
*time
, int done
, const char **errmsg
,
731 /* If we are cleaning up when the caller didn't retrieve process
732 status for some reason, encourage the process to go away. */
736 if (pex_wait (obj
, pid
, status
, time
) < 0)
749 pex_unix_pipe (struct pex_obj
*obj ATTRIBUTE_UNUSED
, int *p
,
750 int binary ATTRIBUTE_UNUSED
)
755 /* Get a FILE pointer to read from a file descriptor. */
758 pex_unix_fdopenr (struct pex_obj
*obj ATTRIBUTE_UNUSED
, int fd
,
759 int binary ATTRIBUTE_UNUSED
)
761 return fdopen (fd
, "r");
765 pex_unix_fdopenw (struct pex_obj
*obj ATTRIBUTE_UNUSED
, int fd
,
766 int binary ATTRIBUTE_UNUSED
)
768 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
) < 0)
770 return fdopen (fd
, "w");
774 pex_unix_cleanup (struct pex_obj
*obj ATTRIBUTE_UNUSED
)
776 #if !defined (HAVE_WAIT4) && !defined (HAVE_WAITPID)
777 while (obj
->sysdep
!= NULL
)
779 struct status_list
*this;
780 struct status_list
*next
;
782 this = (struct status_list
*) obj
->sysdep
;
785 obj
->sysdep
= (void *) next
;