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
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
60 #ifdef vfork /* Autoconf may define this to fork for us. */
61 # define VFORK_STRING "fork"
63 # define VFORK_STRING "vfork"
69 #define vfork() (decc$$alloc_vfork_blocks() >= 0 ? \
70 lib$get_current_invo_context(decc$$get_vfork_jmpbuf()) : -1)
74 /* File mode to use for private and world-readable files. */
76 #if defined (S_IRUSR) && defined (S_IWUSR) && defined (S_IRGRP) && defined (S_IWGRP) && defined (S_IROTH) && defined (S_IWOTH)
78 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
80 #define PUBLIC_MODE 0666
83 /* Get the exit status of a particular process, and optionally get the
84 time that it took. This is simple if we have wait4, slightly
85 harder if we have waitpid, and is a pain if we only have wait. */
87 static pid_t
pex_wait (struct pex_obj
*, pid_t
, int *, struct pex_time
*);
92 pex_wait (struct pex_obj
*obj ATTRIBUTE_UNUSED
, pid_t pid
, int *status
,
93 struct pex_time
*time
)
100 return waitpid (pid
, status
, 0);
103 ret
= wait4 (pid
, status
, 0, &r
);
107 time
->user_seconds
= r
.ru_utime
.tv_sec
;
108 time
->user_microseconds
= r
.ru_utime
.tv_usec
;
109 time
->system_seconds
= r
.ru_stime
.tv_sec
;
110 time
->system_microseconds
= r
.ru_stime
.tv_usec
;
116 #else /* ! defined (HAVE_WAIT4) */
120 #ifndef HAVE_GETRUSAGE
123 pex_wait (struct pex_obj
*obj ATTRIBUTE_UNUSED
, pid_t pid
, int *status
,
124 struct pex_time
*time
)
127 memset (time
, 0, sizeof (struct pex_time
));
128 return waitpid (pid
, status
, 0);
131 #else /* defined (HAVE_GETRUSAGE) */
134 pex_wait (struct pex_obj
*obj ATTRIBUTE_UNUSED
, pid_t pid
, int *status
,
135 struct pex_time
*time
)
137 struct rusage r1
, r2
;
141 return waitpid (pid
, status
, 0);
143 getrusage (RUSAGE_CHILDREN
, &r1
);
145 ret
= waitpid (pid
, status
, 0);
149 getrusage (RUSAGE_CHILDREN
, &r2
);
151 time
->user_seconds
= r2
.ru_utime
.tv_sec
- r1
.ru_utime
.tv_sec
;
152 time
->user_microseconds
= r2
.ru_utime
.tv_usec
- r1
.ru_utime
.tv_usec
;
153 if (r2
.ru_utime
.tv_usec
< r1
.ru_utime
.tv_usec
)
155 --time
->user_seconds
;
156 time
->user_microseconds
+= 1000000;
159 time
->system_seconds
= r2
.ru_stime
.tv_sec
- r1
.ru_stime
.tv_sec
;
160 time
->system_microseconds
= r2
.ru_stime
.tv_usec
- r1
.ru_stime
.tv_usec
;
161 if (r2
.ru_stime
.tv_usec
< r1
.ru_stime
.tv_usec
)
163 --time
->system_seconds
;
164 time
->system_microseconds
+= 1000000;
170 #endif /* defined (HAVE_GETRUSAGE) */
172 #else /* ! defined (HAVE_WAITPID) */
176 struct status_list
*next
;
179 struct pex_time time
;
183 pex_wait (struct pex_obj
*obj
, pid_t pid
, int *status
, struct pex_time
*time
)
185 struct status_list
**pp
;
187 for (pp
= (struct status_list
**) &obj
->sysdep
;
191 if ((*pp
)->pid
== pid
)
193 struct status_list
*p
;
208 struct status_list
*psl
;
210 #ifdef HAVE_GETRUSAGE
211 struct rusage r1
, r2
;
216 #ifdef HAVE_GETRUSAGE
217 getrusage (RUSAGE_CHILDREN
, &r1
);
219 memset (&pt
, 0, sizeof (struct pex_time
));
223 cpid
= wait (status
);
225 #ifdef HAVE_GETRUSAGE
226 if (time
!= NULL
&& cpid
>= 0)
228 getrusage (RUSAGE_CHILDREN
, &r2
);
230 pt
.user_seconds
= r2
.ru_utime
.tv_sec
- r1
.ru_utime
.tv_sec
;
231 pt
.user_microseconds
= r2
.ru_utime
.tv_usec
- r1
.ru_utime
.tv_usec
;
232 if (pt
.user_microseconds
< 0)
235 pt
.user_microseconds
+= 1000000;
238 pt
.system_seconds
= r2
.ru_stime
.tv_sec
- r1
.ru_stime
.tv_sec
;
239 pt
.system_microseconds
= r2
.ru_stime
.tv_usec
- r1
.ru_stime
.tv_usec
;
240 if (pt
.system_microseconds
< 0)
243 pt
.system_microseconds
+= 1000000;
248 if (cpid
< 0 || cpid
== pid
)
255 psl
= XNEW (struct status_list
);
257 psl
->status
= *status
;
260 psl
->next
= (struct status_list
*) obj
->sysdep
;
261 obj
->sysdep
= (void *) psl
;
265 #endif /* ! defined (HAVE_WAITPID) */
266 #endif /* ! defined (HAVE_WAIT4) */
268 static void pex_child_error (struct pex_obj
*, const char *, const char *, int)
270 static int pex_unix_open_read (struct pex_obj
*, const char *, int);
271 static int pex_unix_open_write (struct pex_obj
*, const char *, int);
272 static long pex_unix_exec_child (struct pex_obj
*, int, const char *,
273 char * const *, char * const *,
275 const char **, int *);
276 static int pex_unix_close (struct pex_obj
*, int);
277 static int pex_unix_wait (struct pex_obj
*, long, int *, struct pex_time
*,
278 int, const char **, int *);
279 static int pex_unix_pipe (struct pex_obj
*, int *, int);
280 static FILE *pex_unix_fdopenr (struct pex_obj
*, int, int);
281 static FILE *pex_unix_fdopenw (struct pex_obj
*, int, int);
282 static void pex_unix_cleanup (struct pex_obj
*);
284 /* The list of functions we pass to the common routines. */
286 const struct pex_funcs funcs
=
299 /* Return a newly initialized pex_obj structure. */
302 pex_init (int flags
, const char *pname
, const char *tempbase
)
304 return pex_init_common (flags
, pname
, tempbase
, &funcs
);
307 /* Open a file for reading. */
310 pex_unix_open_read (struct pex_obj
*obj ATTRIBUTE_UNUSED
, const char *name
,
311 int binary ATTRIBUTE_UNUSED
)
313 return open (name
, O_RDONLY
);
316 /* Open a file for writing. */
319 pex_unix_open_write (struct pex_obj
*obj ATTRIBUTE_UNUSED
, const char *name
,
320 int binary ATTRIBUTE_UNUSED
)
322 /* Note that we can't use O_EXCL here because gcc may have already
323 created the temporary file via make_temp_file. */
324 return open (name
, O_WRONLY
| O_CREAT
| O_TRUNC
, PUBLIC_MODE
);
330 pex_unix_close (struct pex_obj
*obj ATTRIBUTE_UNUSED
, int fd
)
335 /* Report an error from a child process. We don't use stdio routines,
336 because we might be here due to a vfork call. */
339 pex_child_error (struct pex_obj
*obj
, const char *executable
,
340 const char *errmsg
, int err
)
342 #define writeerr(s) write (STDERR_FILE_NO, s, strlen (s))
343 writeerr (obj
->pname
);
344 writeerr (": error trying to exec '");
345 writeerr (executable
);
349 writeerr (xstrerror (err
));
354 /* Execute a child. */
356 extern char **environ
;
359 pex_unix_exec_child (struct pex_obj
*obj
, int flags
, const char *executable
,
360 char * const * argv
, char * const * env
,
361 int in
, int out
, int errdes
,
362 int toclose
, const char **errmsg
, int *err
)
366 /* We declare these to be volatile to avoid warnings from gcc about
367 them being clobbered by vfork. */
368 volatile int sleep_interval
;
369 volatile int retries
;
373 for (retries
= 0; retries
< 4; ++retries
)
378 sleep (sleep_interval
);
386 *errmsg
= VFORK_STRING
;
391 if (in
!= STDIN_FILE_NO
)
393 if (dup2 (in
, STDIN_FILE_NO
) < 0)
394 pex_child_error (obj
, executable
, "dup2", errno
);
396 pex_child_error (obj
, executable
, "close", errno
);
398 if (out
!= STDOUT_FILE_NO
)
400 if (dup2 (out
, STDOUT_FILE_NO
) < 0)
401 pex_child_error (obj
, executable
, "dup2", errno
);
403 pex_child_error (obj
, executable
, "close", errno
);
405 if (errdes
!= STDERR_FILE_NO
)
407 if (dup2 (errdes
, STDERR_FILE_NO
) < 0)
408 pex_child_error (obj
, executable
, "dup2", errno
);
409 if (close (errdes
) < 0)
410 pex_child_error (obj
, executable
, "close", errno
);
414 if (close (toclose
) < 0)
415 pex_child_error (obj
, executable
, "close", errno
);
417 if ((flags
& PEX_STDERR_TO_STDOUT
) != 0)
419 if (dup2 (STDOUT_FILE_NO
, STDERR_FILE_NO
) < 0)
420 pex_child_error (obj
, executable
, "dup2", errno
);
424 environ
= (char**) env
;
426 if ((flags
& PEX_SEARCH
) != 0)
428 execvp (executable
, argv
);
429 pex_child_error (obj
, executable
, "execvp", errno
);
433 execv (executable
, argv
);
434 pex_child_error (obj
, executable
, "execv", errno
);
441 /* Parent process. */
442 if (in
!= STDIN_FILE_NO
)
451 if (out
!= STDOUT_FILE_NO
)
460 if (errdes
!= STDERR_FILE_NO
)
462 if (close (errdes
) < 0)
474 /* Wait for a child process to complete. */
477 pex_unix_wait (struct pex_obj
*obj
, long pid
, int *status
,
478 struct pex_time
*time
, int done
, const char **errmsg
,
481 /* If we are cleaning up when the caller didn't retrieve process
482 status for some reason, encourage the process to go away. */
486 if (pex_wait (obj
, pid
, status
, time
) < 0)
499 pex_unix_pipe (struct pex_obj
*obj ATTRIBUTE_UNUSED
, int *p
,
500 int binary ATTRIBUTE_UNUSED
)
505 /* Get a FILE pointer to read from a file descriptor. */
508 pex_unix_fdopenr (struct pex_obj
*obj ATTRIBUTE_UNUSED
, int fd
,
509 int binary ATTRIBUTE_UNUSED
)
511 return fdopen (fd
, "r");
515 pex_unix_fdopenw (struct pex_obj
*obj ATTRIBUTE_UNUSED
, int fd
,
516 int binary ATTRIBUTE_UNUSED
)
518 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
) < 0)
520 return fdopen (fd
, "w");
524 pex_unix_cleanup (struct pex_obj
*obj ATTRIBUTE_UNUSED
)
526 #if !defined (HAVE_WAIT4) && !defined (HAVE_WAITPID)
527 while (obj
->sysdep
!= NULL
)
529 struct status_list
*this;
530 struct status_list
*next
;
532 this = (struct status_list
*) obj
->sysdep
;
535 obj
->sysdep
= (void *) next
;