]> git.ipfire.org Git - thirdparty/gcc.git/blob - libiberty/pex-unix.c
0c5f85c3528f185d38411a2b770a5db1de53bdc9
[thirdparty/gcc.git] / libiberty / pex-unix.c
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.
6
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.
12
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.
17
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., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
22
23 #include "pex-common.h"
24
25 #include <stdio.h>
26 #include <errno.h>
27 #ifdef NEED_DECLARATION_ERRNO
28 extern int errno;
29 #endif
30 #ifdef HAVE_STRING_H
31 #include <string.h>
32 #endif
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36 #ifdef HAVE_STDLIB_H
37 #include <stdlib.h>
38 #endif
39 #ifdef HAVE_SYS_WAIT_H
40 #include <sys/wait.h>
41 #endif
42
43 #ifndef HAVE_WAITPID
44 #define waitpid(pid, status, flags) wait(status)
45 #endif
46
47 #ifdef vfork /* Autoconf may define this to fork for us. */
48 # define VFORK_STRING "fork"
49 #else
50 # define VFORK_STRING "vfork"
51 #endif
52 #ifdef HAVE_VFORK_H
53 #include <vfork.h>
54 #endif
55 #ifdef VMS
56 #define vfork() (decc$$alloc_vfork_blocks() >= 0 ? \
57 lib$get_current_invo_context(decc$$get_vfork_jmpbuf()) : -1)
58 #endif /* VMS */
59
60 /* Execute a program, possibly setting up pipes to programs executed
61 via other calls to this function.
62
63 This version of the function uses vfork. In general vfork is
64 similar to setjmp/longjmp, in that any variable which is modified by
65 the child process has an indeterminate value in the parent process.
66 We follow a safe approach here by not modifying any variables at
67 all in the child process (with the possible exception of variables
68 modified by xstrerror if exec fails, but this is unlikely to be
69 detectable).
70
71 We work a little bit harder to avoid gcc warnings. gcc will warn
72 about any automatic variable which is live at the time of the
73 vfork, which is non-volatile, and which is either set more than
74 once or is an argument to the function. This warning isn't quite
75 right, since what we really care about is whether the variable is
76 live at the time of the vfork and set afterward by the child
77 process, but gcc only checks whether the variable is set more than
78 once. To avoid this warning, we ensure that any variable which is
79 live at the time of the vfork (i.e., used after the vfork) is set
80 exactly once and is not an argument, or is marked volatile. */
81
82 int
83 pexecute (const char *program, char * const *argv, const char *this_pname,
84 const char *temp_base ATTRIBUTE_UNUSED,
85 char **errmsg_fmt, char **errmsg_arg, int flagsarg)
86 {
87 int pid;
88 int pdes[2];
89 int out;
90 int input_desc, output_desc;
91 int flags;
92 /* We declare these to be volatile to avoid warnings from gcc about
93 them being clobbered by vfork. */
94 volatile int retries, sleep_interval;
95 /* Pipe waiting from last process, to be used as input for the next one.
96 Value is STDIN_FILE_NO if no pipe is waiting
97 (i.e. the next command is the first of a group). */
98 static int last_pipe_input;
99
100 flags = flagsarg;
101
102 /* If this is the first process, initialize. */
103 if (flags & PEXECUTE_FIRST)
104 last_pipe_input = STDIN_FILE_NO;
105
106 input_desc = last_pipe_input;
107
108 /* If this isn't the last process, make a pipe for its output,
109 and record it as waiting to be the input to the next process. */
110 if (! (flags & PEXECUTE_LAST))
111 {
112 if (pipe (pdes) < 0)
113 {
114 *errmsg_fmt = "pipe";
115 *errmsg_arg = NULL;
116 return -1;
117 }
118 out = pdes[WRITE_PORT];
119 last_pipe_input = pdes[READ_PORT];
120 }
121 else
122 {
123 /* Last process. */
124 out = STDOUT_FILE_NO;
125 last_pipe_input = STDIN_FILE_NO;
126 }
127
128 output_desc = out;
129
130 /* Fork a subprocess; wait and retry if it fails. */
131 sleep_interval = 1;
132 pid = -1;
133 for (retries = 0; retries < 4; retries++)
134 {
135 pid = vfork ();
136 if (pid >= 0)
137 break;
138 sleep (sleep_interval);
139 sleep_interval *= 2;
140 }
141
142 switch (pid)
143 {
144 case -1:
145 *errmsg_fmt = "fork";
146 *errmsg_arg = NULL;
147 return -1;
148
149 case 0: /* child */
150 /* Move the input and output pipes into place, if necessary. */
151 if (input_desc != STDIN_FILE_NO)
152 {
153 close (STDIN_FILE_NO);
154 dup (input_desc);
155 close (input_desc);
156 }
157 if (output_desc != STDOUT_FILE_NO)
158 {
159 close (STDOUT_FILE_NO);
160 dup (output_desc);
161 close (output_desc);
162 }
163
164 /* Close the parent's descs that aren't wanted here. */
165 if (last_pipe_input != STDIN_FILE_NO)
166 close (last_pipe_input);
167
168 /* Exec the program. */
169 if (flags & PEXECUTE_SEARCH)
170 execvp (program, argv);
171 else
172 execv (program, argv);
173
174 /* We don't want to call fprintf after vfork. */
175 #define writeerr(s) write (STDERR_FILE_NO, s, strlen (s))
176 writeerr (this_pname);
177 writeerr (": ");
178 writeerr ("installation problem, cannot exec '");
179 writeerr (program);
180 writeerr ("': ");
181 writeerr (xstrerror (errno));
182 writeerr ("\n");
183 _exit (-1);
184 /* NOTREACHED */
185 return 0;
186
187 default:
188 /* In the parent, after forking.
189 Close the descriptors that we made for this child. */
190 if (input_desc != STDIN_FILE_NO)
191 close (input_desc);
192 if (output_desc != STDOUT_FILE_NO)
193 close (output_desc);
194
195 /* Return child's process number. */
196 return pid;
197 }
198 }
199
200 int
201 pwait (int pid, int *status, int flags ATTRIBUTE_UNUSED)
202 {
203 /* ??? Here's an opportunity to canonicalize the values in STATUS.
204 Needed? */
205 pid = waitpid (pid, status, 0);
206 return pid;
207 }