Fix mail address.
[official-gcc.git] / libiberty / pex-unix.c
blobc92a429797128c4ba36aff5676649eeded39da47
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. */
23 #include "config.h"
24 #include "libiberty.h"
25 #include "pex-common.h"
27 #include <stdio.h>
28 #include <signal.h>
29 #include <errno.h>
30 #ifdef NEED_DECLARATION_ERRNO
31 extern int errno;
32 #endif
33 #ifdef HAVE_STDLIB_H
34 #include <stdlib.h>
35 #endif
36 #ifdef HAVE_STRING_H
37 #include <string.h>
38 #endif
39 #ifdef HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif
43 #include <sys/types.h>
45 #ifdef HAVE_FCNTL_H
46 #include <fcntl.h>
47 #endif
48 #ifdef HAVE_SYS_WAIT_H
49 #include <sys/wait.h>
50 #endif
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>
57 #endif
60 #ifdef vfork /* Autoconf may define this to fork for us. */
61 # define VFORK_STRING "fork"
62 #else
63 # define VFORK_STRING "vfork"
64 #endif
65 #ifdef HAVE_VFORK_H
66 #include <vfork.h>
67 #endif
68 #ifdef VMS
69 #define vfork() (decc$$alloc_vfork_blocks() >= 0 ? \
70 lib$get_current_invo_context(decc$$get_vfork_jmpbuf()) : -1)
71 #endif /* VMS */
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)
77 #define PUBLIC_MODE \
78 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
79 #else
80 #define PUBLIC_MODE 0666
81 #endif
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 *);
89 #ifdef HAVE_WAIT4
91 static pid_t
92 pex_wait (struct pex_obj *obj ATTRIBUTE_UNUSED, pid_t pid, int *status,
93 struct pex_time *time)
95 pid_t ret;
96 struct rusage r;
98 #ifdef HAVE_WAITPID
99 if (time == NULL)
100 return waitpid (pid, status, 0);
101 #endif
103 ret = wait4 (pid, status, 0, &r);
105 if (time != NULL)
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;
113 return ret;
116 #else /* ! defined (HAVE_WAIT4) */
118 #ifdef HAVE_WAITPID
120 #ifndef HAVE_GETRUSAGE
122 static pid_t
123 pex_wait (struct pex_obj *obj ATTRIBUTE_UNUSED, pid_t pid, int *status,
124 struct pex_time *time)
126 if (time != NULL)
127 memset (time, 0, sizeof (struct pex_time));
128 return waitpid (pid, status, 0);
131 #else /* defined (HAVE_GETRUSAGE) */
133 static pid_t
134 pex_wait (struct pex_obj *obj ATTRIBUTE_UNUSED, pid_t pid, int *status,
135 struct pex_time *time)
137 struct rusage r1, r2;
138 pid_t ret;
140 if (time == NULL)
141 return waitpid (pid, status, 0);
143 getrusage (RUSAGE_CHILDREN, &r1);
145 ret = waitpid (pid, status, 0);
146 if (ret < 0)
147 return ret;
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;
167 return ret;
170 #endif /* defined (HAVE_GETRUSAGE) */
172 #else /* ! defined (HAVE_WAITPID) */
174 struct status_list
176 struct status_list *next;
177 pid_t pid;
178 int status;
179 struct pex_time time;
182 static pid_t
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;
188 *pp != NULL;
189 pp = &(*pp)->next)
191 if ((*pp)->pid == pid)
193 struct status_list *p;
195 p = *pp;
196 *status = p->status;
197 if (time != NULL)
198 *time = p->time;
199 *pp = p->next;
200 free (p);
201 return pid;
205 while (1)
207 pid_t cpid;
208 struct status_list *psl;
209 struct pex_time pt;
210 #ifdef HAVE_GETRUSAGE
211 struct rusage r1, r2;
212 #endif
214 if (time != NULL)
216 #ifdef HAVE_GETRUSAGE
217 getrusage (RUSAGE_CHILDREN, &r1);
218 #else
219 memset (&pt, 0, sizeof (struct pex_time));
220 #endif
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)
234 --pt.user_seconds;
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)
242 --pt.system_seconds;
243 pt.system_microseconds += 1000000;
246 #endif
248 if (cpid < 0 || cpid == pid)
250 if (time != NULL)
251 *time = pt;
252 return cpid;
255 psl = XNEW (struct status_list);
256 psl->pid = cpid;
257 psl->status = *status;
258 if (time != NULL)
259 psl->time = pt;
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)
269 ATTRIBUTE_NORETURN;
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 *, int, int, int,
274 const char **, int *);
275 static int pex_unix_close (struct pex_obj *, int);
276 static int pex_unix_wait (struct pex_obj *, long, int *, struct pex_time *,
277 int, const char **, int *);
278 static int pex_unix_pipe (struct pex_obj *, int *, int);
279 static FILE *pex_unix_fdopenr (struct pex_obj *, int, int);
280 static FILE *pex_unix_fdopenw (struct pex_obj *, int, int);
281 static void pex_unix_cleanup (struct pex_obj *);
283 /* The list of functions we pass to the common routines. */
285 const struct pex_funcs funcs =
287 pex_unix_open_read,
288 pex_unix_open_write,
289 pex_unix_exec_child,
290 pex_unix_close,
291 pex_unix_wait,
292 pex_unix_pipe,
293 pex_unix_fdopenr,
294 pex_unix_fdopenw,
295 pex_unix_cleanup
298 /* Return a newly initialized pex_obj structure. */
300 struct pex_obj *
301 pex_init (int flags, const char *pname, const char *tempbase)
303 return pex_init_common (flags, pname, tempbase, &funcs);
306 /* Open a file for reading. */
308 static int
309 pex_unix_open_read (struct pex_obj *obj ATTRIBUTE_UNUSED, const char *name,
310 int binary ATTRIBUTE_UNUSED)
312 return open (name, O_RDONLY);
315 /* Open a file for writing. */
317 static int
318 pex_unix_open_write (struct pex_obj *obj ATTRIBUTE_UNUSED, const char *name,
319 int binary ATTRIBUTE_UNUSED)
321 /* Note that we can't use O_EXCL here because gcc may have already
322 created the temporary file via make_temp_file. */
323 return open (name, O_WRONLY | O_CREAT | O_TRUNC, PUBLIC_MODE);
326 /* Close a file. */
328 static int
329 pex_unix_close (struct pex_obj *obj ATTRIBUTE_UNUSED, int fd)
331 return close (fd);
334 /* Report an error from a child process. We don't use stdio routines,
335 because we might be here due to a vfork call. */
337 static void
338 pex_child_error (struct pex_obj *obj, const char *executable,
339 const char *errmsg, int err)
341 #define writeerr(s) write (STDERR_FILE_NO, s, strlen (s))
342 writeerr (obj->pname);
343 writeerr (": error trying to exec '");
344 writeerr (executable);
345 writeerr ("': ");
346 writeerr (errmsg);
347 writeerr (": ");
348 writeerr (xstrerror (err));
349 writeerr ("\n");
350 _exit (-1);
353 /* Execute a child. */
355 static long
356 pex_unix_exec_child (struct pex_obj *obj, int flags, const char *executable,
357 char * const * argv, int in, int out, int errdes,
358 const char **errmsg, int *err)
360 pid_t pid;
361 /* We declare these to be volatile to avoid warnings from gcc about
362 them being clobbered by vfork. */
363 volatile int sleep_interval;
364 volatile int retries;
366 sleep_interval = 1;
367 pid = -1;
368 for (retries = 0; retries < 4; ++retries)
370 pid = vfork ();
371 if (pid >= 0)
372 break;
373 sleep (sleep_interval);
374 sleep_interval *= 2;
377 switch (pid)
379 case -1:
380 *err = errno;
381 *errmsg = VFORK_STRING;
382 return -1;
384 case 0:
385 /* Child process. */
386 if (in != STDIN_FILE_NO)
388 if (dup2 (in, STDIN_FILE_NO) < 0)
389 pex_child_error (obj, executable, "dup2", errno);
390 if (close (in) < 0)
391 pex_child_error (obj, executable, "close", errno);
393 if (out != STDOUT_FILE_NO)
395 if (dup2 (out, STDOUT_FILE_NO) < 0)
396 pex_child_error (obj, executable, "dup2", errno);
397 if (close (out) < 0)
398 pex_child_error (obj, executable, "close", errno);
400 if (errdes != STDERR_FILE_NO)
402 if (dup2 (errdes, STDERR_FILE_NO) < 0)
403 pex_child_error (obj, executable, "dup2", errno);
404 if (close (errdes) < 0)
405 pex_child_error (obj, executable, "close", errno);
407 if ((flags & PEX_STDERR_TO_STDOUT) != 0)
409 if (dup2 (STDOUT_FILE_NO, STDERR_FILE_NO) < 0)
410 pex_child_error (obj, executable, "dup2", errno);
412 if ((flags & PEX_SEARCH) != 0)
414 execvp (executable, argv);
415 pex_child_error (obj, executable, "execvp", errno);
417 else
419 execv (executable, argv);
420 pex_child_error (obj, executable, "execv", errno);
423 /* NOTREACHED */
424 return -1;
426 default:
427 /* Parent process. */
428 if (in != STDIN_FILE_NO)
430 if (close (in) < 0)
432 *err = errno;
433 *errmsg = "close";
434 return -1;
437 if (out != STDOUT_FILE_NO)
439 if (close (out) < 0)
441 *err = errno;
442 *errmsg = "close";
443 return -1;
446 if (errdes != STDERR_FILE_NO)
448 if (close (errdes) < 0)
450 *err = errno;
451 *errmsg = "close";
452 return -1;
456 return (long) pid;
460 /* Wait for a child process to complete. */
462 static int
463 pex_unix_wait (struct pex_obj *obj, long pid, int *status,
464 struct pex_time *time, int done, const char **errmsg,
465 int *err)
467 /* If we are cleaning up when the caller didn't retrieve process
468 status for some reason, encourage the process to go away. */
469 if (done)
470 kill (pid, SIGTERM);
472 if (pex_wait (obj, pid, status, time) < 0)
474 *err = errno;
475 *errmsg = "wait";
476 return -1;
479 return 0;
482 /* Create a pipe. */
484 static int
485 pex_unix_pipe (struct pex_obj *obj ATTRIBUTE_UNUSED, int *p,
486 int binary ATTRIBUTE_UNUSED)
488 return pipe (p);
491 /* Get a FILE pointer to read from a file descriptor. */
493 static FILE *
494 pex_unix_fdopenr (struct pex_obj *obj ATTRIBUTE_UNUSED, int fd,
495 int binary ATTRIBUTE_UNUSED)
497 return fdopen (fd, "r");
500 static FILE *
501 pex_unix_fdopenw (struct pex_obj *obj ATTRIBUTE_UNUSED, int fd,
502 int binary ATTRIBUTE_UNUSED)
504 if (fcntl (fd, F_SETFD, FD_CLOEXEC) < 0)
505 return NULL;
506 return fdopen (fd, "w");
509 static void
510 pex_unix_cleanup (struct pex_obj *obj ATTRIBUTE_UNUSED)
512 #if !defined (HAVE_WAIT4) && !defined (HAVE_WAITPID)
513 while (obj->sysdep != NULL)
515 struct status_list *this;
516 struct status_list *next;
518 this = (struct status_list *) obj->sysdep;
519 next = this->next;
520 free (this);
521 obj->sysdep = (void *) next;
523 #endif