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., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 #include "pex-common.h"
27 #ifdef NEED_DECLARATION_ERRNO
39 #ifdef HAVE_SYS_WAIT_H
44 #define waitpid(pid, status, flags) wait(status)
47 #ifdef vfork /* Autoconf may define this to fork for us. */
48 # define VFORK_STRING "fork"
50 # define VFORK_STRING "vfork"
56 #define vfork() (decc$$alloc_vfork_blocks() >= 0 ? \
57 lib$get_current_invo_context(decc$$get_vfork_jmpbuf()) : -1)
60 /* Execute a program, possibly setting up pipes to programs executed
61 via other calls to this function.
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
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. */
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
)
90 int input_desc
, output_desc
;
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
;
102 /* If this is the first process, initialize. */
103 if (flags
& PEXECUTE_FIRST
)
104 last_pipe_input
= STDIN_FILE_NO
;
106 input_desc
= last_pipe_input
;
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
))
114 *errmsg_fmt
= "pipe";
118 out
= pdes
[WRITE_PORT
];
119 last_pipe_input
= pdes
[READ_PORT
];
124 out
= STDOUT_FILE_NO
;
125 last_pipe_input
= STDIN_FILE_NO
;
130 /* Fork a subprocess; wait and retry if it fails. */
133 for (retries
= 0; retries
< 4; retries
++)
138 sleep (sleep_interval
);
145 *errmsg_fmt
= "fork";
150 /* Move the input and output pipes into place, if necessary. */
151 if (input_desc
!= STDIN_FILE_NO
)
153 close (STDIN_FILE_NO
);
157 if (output_desc
!= STDOUT_FILE_NO
)
159 close (STDOUT_FILE_NO
);
164 /* Close the parent's descs that aren't wanted here. */
165 if (last_pipe_input
!= STDIN_FILE_NO
)
166 close (last_pipe_input
);
168 /* Exec the program. */
169 if (flags
& PEXECUTE_SEARCH
)
170 execvp (program
, argv
);
172 execv (program
, argv
);
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
);
178 writeerr ("installation problem, cannot exec '");
181 writeerr (xstrerror (errno
));
188 /* In the parent, after forking.
189 Close the descriptors that we made for this child. */
190 if (input_desc
!= STDIN_FILE_NO
)
192 if (output_desc
!= STDOUT_FILE_NO
)
195 /* Return child's process number. */
201 pwait (int pid
, int *status
, int flags ATTRIBUTE_UNUSED
)
203 /* ??? Here's an opportunity to canonicalize the values in STATUS.
205 pid
= waitpid (pid
, status
, 0);