]> git.ipfire.org Git - thirdparty/gcc.git/blame - libiberty/pex-unix.c
re PR other/28797 (Problems with demangling (__cxa_demangle()))
[thirdparty/gcc.git] / libiberty / pex-unix.c
CommitLineData
55d0e5e0
ZW
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).
885f2199 4 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005
55d0e5e0
ZW
5 Free Software Foundation, Inc.
6
7This file is part of the libiberty library.
8Libiberty is free software; you can redistribute it and/or
9modify it under the terms of the GNU Library General Public
10License as published by the Free Software Foundation; either
11version 2 of the License, or (at your option) any later version.
12
13Libiberty is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16Library General Public License for more details.
17
18You should have received a copy of the GNU Library General Public
19License along with libiberty; see the file COPYING.LIB. If not,
ee58dffd
NC
20write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
21Boston, MA 02110-1301, USA. */
55d0e5e0 22
a584cf65
ILT
23#include "config.h"
24#include "libiberty.h"
55d0e5e0
ZW
25#include "pex-common.h"
26
27#include <stdio.h>
a584cf65 28#include <signal.h>
55d0e5e0
ZW
29#include <errno.h>
30#ifdef NEED_DECLARATION_ERRNO
31extern int errno;
32#endif
a584cf65
ILT
33#ifdef HAVE_STDLIB_H
34#include <stdlib.h>
35#endif
55d0e5e0
ZW
36#ifdef HAVE_STRING_H
37#include <string.h>
38#endif
39#ifdef HAVE_UNISTD_H
40#include <unistd.h>
41#endif
a584cf65
ILT
42
43#include <sys/types.h>
44
45#ifdef HAVE_FCNTL_H
46#include <fcntl.h>
55d0e5e0
ZW
47#endif
48#ifdef HAVE_SYS_WAIT_H
49#include <sys/wait.h>
50#endif
a584cf65
ILT
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>
55d0e5e0
ZW
57#endif
58
a584cf65 59
fed8129b
ILT
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 */
72
55d0e5e0 73
a584cf65
ILT
74/* File mode to use for private and world-readable files. */
75
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
82
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. */
86
87static pid_t pex_wait (struct pex_obj *, pid_t, int *, struct pex_time *);
88
89#ifdef HAVE_WAIT4
90
91static pid_t
92pex_wait (struct pex_obj *obj ATTRIBUTE_UNUSED, pid_t pid, int *status,
93 struct pex_time *time)
94{
95 pid_t ret;
96 struct rusage r;
97
98#ifdef HAVE_WAITPID
99 if (time == NULL)
100 return waitpid (pid, status, 0);
101#endif
102
103 ret = wait4 (pid, status, 0, &r);
104
105 if (time != NULL)
106 {
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;
111 }
112
113 return ret;
114}
115
116#else /* ! defined (HAVE_WAIT4) */
117
118#ifdef HAVE_WAITPID
119
120#ifndef HAVE_GETRUSAGE
121
122static pid_t
123pex_wait (struct pex_obj *obj ATTRIBUTE_UNUSED, pid_t pid, int *status,
124 struct pex_time *time)
125{
126 if (time != NULL)
127 memset (time, 0, sizeof (struct pex_time));
128 return waitpid (pid, status, 0);
129}
130
131#else /* defined (HAVE_GETRUSAGE) */
132
133static pid_t
134pex_wait (struct pex_obj *obj ATTRIBUTE_UNUSED, pid_t pid, int *status,
135 struct pex_time *time)
136{
137 struct rusage r1, r2;
138 pid_t ret;
139
140 if (time == NULL)
141 return waitpid (pid, status, 0);
142
143 getrusage (RUSAGE_CHILDREN, &r1);
144
145 ret = waitpid (pid, status, 0);
146 if (ret < 0)
147 return ret;
148
149 getrusage (RUSAGE_CHILDREN, &r2);
150
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)
154 {
155 --time->user_seconds;
156 time->user_microseconds += 1000000;
157 }
158
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)
162 {
163 --time->system_seconds;
164 time->system_microseconds += 1000000;
165 }
fed8129b 166
a584cf65
ILT
167 return ret;
168}
55d0e5e0 169
a584cf65 170#endif /* defined (HAVE_GETRUSAGE) */
55d0e5e0 171
a584cf65
ILT
172#else /* ! defined (HAVE_WAITPID) */
173
174struct status_list
175{
176 struct status_list *next;
177 pid_t pid;
178 int status;
179 struct pex_time time;
180};
181
182static pid_t
183pex_wait (struct pex_obj *obj, pid_t pid, int *status, struct pex_time *time)
184{
185 struct status_list **pp;
186
187 for (pp = (struct status_list **) &obj->sysdep;
188 *pp != NULL;
189 pp = &(*pp)->next)
55d0e5e0 190 {
a584cf65 191 if ((*pp)->pid == pid)
55d0e5e0 192 {
a584cf65
ILT
193 struct status_list *p;
194
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;
55d0e5e0 202 }
55d0e5e0 203 }
a584cf65
ILT
204
205 while (1)
55d0e5e0 206 {
a584cf65
ILT
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
213
214 if (time != NULL)
215 {
216#ifdef HAVE_GETRUSAGE
217 getrusage (RUSAGE_CHILDREN, &r1);
218#else
219 memset (&pt, 0, sizeof (struct pex_time));
220#endif
221 }
222
223 cpid = wait (status);
224
225#ifdef HAVE_GETRUSAGE
226 if (time != NULL && cpid >= 0)
227 {
228 getrusage (RUSAGE_CHILDREN, &r2);
229
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)
233 {
234 --pt.user_seconds;
235 pt.user_microseconds += 1000000;
236 }
237
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)
241 {
242 --pt.system_seconds;
243 pt.system_microseconds += 1000000;
244 }
245 }
246#endif
247
248 if (cpid < 0 || cpid == pid)
249 {
250 if (time != NULL)
251 *time = pt;
252 return cpid;
253 }
254
d7cf8390 255 psl = XNEW (struct status_list);
a584cf65
ILT
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;
55d0e5e0 262 }
a584cf65
ILT
263}
264
265#endif /* ! defined (HAVE_WAITPID) */
266#endif /* ! defined (HAVE_WAIT4) */
267
268static void pex_child_error (struct pex_obj *, const char *, const char *, int)
269 ATTRIBUTE_NORETURN;
270static int pex_unix_open_read (struct pex_obj *, const char *, int);
271static int pex_unix_open_write (struct pex_obj *, const char *, int);
272static long pex_unix_exec_child (struct pex_obj *, int, const char *,
ea60341e
MS
273 char * const *, char * const *,
274 int, int, int, const char **, int *);
a584cf65
ILT
275static int pex_unix_close (struct pex_obj *, int);
276static int pex_unix_wait (struct pex_obj *, long, int *, struct pex_time *,
277 int, const char **, int *);
278static int pex_unix_pipe (struct pex_obj *, int *, int);
279static FILE *pex_unix_fdopenr (struct pex_obj *, int, int);
8eff378c 280static FILE *pex_unix_fdopenw (struct pex_obj *, int, int);
a584cf65
ILT
281static void pex_unix_cleanup (struct pex_obj *);
282
283/* The list of functions we pass to the common routines. */
284
285const struct pex_funcs funcs =
286{
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,
8eff378c 294 pex_unix_fdopenw,
a584cf65
ILT
295 pex_unix_cleanup
296};
297
298/* Return a newly initialized pex_obj structure. */
299
300struct pex_obj *
301pex_init (int flags, const char *pname, const char *tempbase)
302{
303 return pex_init_common (flags, pname, tempbase, &funcs);
304}
305
306/* Open a file for reading. */
307
308static int
309pex_unix_open_read (struct pex_obj *obj ATTRIBUTE_UNUSED, const char *name,
310 int binary ATTRIBUTE_UNUSED)
311{
312 return open (name, O_RDONLY);
313}
314
315/* Open a file for writing. */
316
317static int
318pex_unix_open_write (struct pex_obj *obj ATTRIBUTE_UNUSED, const char *name,
319 int binary ATTRIBUTE_UNUSED)
320{
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);
324}
55d0e5e0 325
a584cf65
ILT
326/* Close a file. */
327
328static int
329pex_unix_close (struct pex_obj *obj ATTRIBUTE_UNUSED, int fd)
330{
331 return close (fd);
332}
333
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. */
336
337static void
338pex_child_error (struct pex_obj *obj, const char *executable,
339 const char *errmsg, int err)
340{
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);
351}
352
353/* Execute a child. */
354
ea60341e
MS
355extern char **environ;
356
a584cf65
ILT
357static long
358pex_unix_exec_child (struct pex_obj *obj, int flags, const char *executable,
ea60341e
MS
359 char * const * argv, char * const * env,
360 int in, int out, int errdes,
a584cf65
ILT
361 const char **errmsg, int *err)
362{
363 pid_t pid;
ea60341e 364
a584cf65
ILT
365 /* We declare these to be volatile to avoid warnings from gcc about
366 them being clobbered by vfork. */
367 volatile int sleep_interval;
368 volatile int retries;
fed8129b 369
55d0e5e0
ZW
370 sleep_interval = 1;
371 pid = -1;
a584cf65 372 for (retries = 0; retries < 4; ++retries)
55d0e5e0 373 {
fed8129b 374 pid = vfork ();
55d0e5e0
ZW
375 if (pid >= 0)
376 break;
377 sleep (sleep_interval);
378 sleep_interval *= 2;
379 }
380
381 switch (pid)
382 {
383 case -1:
a584cf65
ILT
384 *err = errno;
385 *errmsg = VFORK_STRING;
55d0e5e0
ZW
386 return -1;
387
a584cf65
ILT
388 case 0:
389 /* Child process. */
390 if (in != STDIN_FILE_NO)
55d0e5e0 391 {
a584cf65
ILT
392 if (dup2 (in, STDIN_FILE_NO) < 0)
393 pex_child_error (obj, executable, "dup2", errno);
394 if (close (in) < 0)
395 pex_child_error (obj, executable, "close", errno);
55d0e5e0 396 }
a584cf65 397 if (out != STDOUT_FILE_NO)
55d0e5e0 398 {
a584cf65
ILT
399 if (dup2 (out, STDOUT_FILE_NO) < 0)
400 pex_child_error (obj, executable, "dup2", errno);
401 if (close (out) < 0)
402 pex_child_error (obj, executable, "close", errno);
403 }
404 if (errdes != STDERR_FILE_NO)
405 {
406 if (dup2 (errdes, STDERR_FILE_NO) < 0)
407 pex_child_error (obj, executable, "dup2", errno);
408 if (close (errdes) < 0)
409 pex_child_error (obj, executable, "close", errno);
410 }
411 if ((flags & PEX_STDERR_TO_STDOUT) != 0)
412 {
413 if (dup2 (STDOUT_FILE_NO, STDERR_FILE_NO) < 0)
414 pex_child_error (obj, executable, "dup2", errno);
415 }
ea60341e
MS
416
417 if (env)
239559e7 418 environ = (char**) env;
ea60341e 419
a584cf65
ILT
420 if ((flags & PEX_SEARCH) != 0)
421 {
422 execvp (executable, argv);
423 pex_child_error (obj, executable, "execvp", errno);
55d0e5e0 424 }
fed8129b 425 else
a584cf65
ILT
426 {
427 execv (executable, argv);
428 pex_child_error (obj, executable, "execv", errno);
429 }
fed8129b 430
55d0e5e0 431 /* NOTREACHED */
a584cf65 432 return -1;
55d0e5e0
ZW
433
434 default:
a584cf65
ILT
435 /* Parent process. */
436 if (in != STDIN_FILE_NO)
437 {
438 if (close (in) < 0)
439 {
440 *err = errno;
441 *errmsg = "close";
442 return -1;
443 }
444 }
445 if (out != STDOUT_FILE_NO)
446 {
447 if (close (out) < 0)
448 {
449 *err = errno;
450 *errmsg = "close";
451 return -1;
452 }
453 }
454 if (errdes != STDERR_FILE_NO)
455 {
456 if (close (errdes) < 0)
457 {
458 *err = errno;
459 *errmsg = "close";
460 return -1;
461 }
462 }
463
464 return (long) pid;
55d0e5e0
ZW
465 }
466}
467
a584cf65
ILT
468/* Wait for a child process to complete. */
469
470static int
471pex_unix_wait (struct pex_obj *obj, long pid, int *status,
472 struct pex_time *time, int done, const char **errmsg,
473 int *err)
55d0e5e0 474{
a584cf65
ILT
475 /* If we are cleaning up when the caller didn't retrieve process
476 status for some reason, encourage the process to go away. */
477 if (done)
478 kill (pid, SIGTERM);
479
480 if (pex_wait (obj, pid, status, time) < 0)
481 {
482 *err = errno;
483 *errmsg = "wait";
484 return -1;
485 }
486
487 return 0;
488}
489
490/* Create a pipe. */
491
492static int
493pex_unix_pipe (struct pex_obj *obj ATTRIBUTE_UNUSED, int *p,
494 int binary ATTRIBUTE_UNUSED)
495{
496 return pipe (p);
497}
498
499/* Get a FILE pointer to read from a file descriptor. */
500
501static FILE *
502pex_unix_fdopenr (struct pex_obj *obj ATTRIBUTE_UNUSED, int fd,
503 int binary ATTRIBUTE_UNUSED)
504{
505 return fdopen (fd, "r");
506}
507
8eff378c
JB
508static FILE *
509pex_unix_fdopenw (struct pex_obj *obj ATTRIBUTE_UNUSED, int fd,
510 int binary ATTRIBUTE_UNUSED)
511{
512 if (fcntl (fd, F_SETFD, FD_CLOEXEC) < 0)
513 return NULL;
514 return fdopen (fd, "w");
515}
516
a584cf65
ILT
517static void
518pex_unix_cleanup (struct pex_obj *obj ATTRIBUTE_UNUSED)
519{
520#if !defined (HAVE_WAIT4) && !defined (HAVE_WAITPID)
521 while (obj->sysdep != NULL)
522 {
523 struct status_list *this;
524 struct status_list *next;
525
526 this = (struct status_list *) obj->sysdep;
527 next = this->next;
528 free (this);
529 obj->sysdep = (void *) next;
530 }
531#endif
55d0e5e0 532}