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
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 (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg,
87 const char *this_pname;
88 const char *temp_base ATTRIBUTE_UNUSED;
89 char **errmsg_fmt, **errmsg_arg;
95 int input_desc, output_desc;
97 /* We declare these to be volatile to avoid warnings from gcc about
98 them being clobbered by vfork. */
99 volatile int retries, sleep_interval;
100 /* Pipe waiting from last process, to be used as input for the next one.
101 Value is STDIN_FILE_NO if no pipe is waiting
102 (i.e. the next command is the first of a group). */
103 static int last_pipe_input;
107 /* If this is the first process, initialize. */
108 if (flags & PEXECUTE_FIRST)
109 last_pipe_input = STDIN_FILE_NO;
111 input_desc = last_pipe_input;
113 /* If this isn't the last process, make a pipe for its output,
114 and record it as waiting to be the input to the next process. */
115 if (! (flags & PEXECUTE_LAST))
119 *errmsg_fmt = "pipe";
123 out = pdes[WRITE_PORT];
124 last_pipe_input = pdes[READ_PORT];
129 out = STDOUT_FILE_NO;
130 last_pipe_input = STDIN_FILE_NO;
135 /* Fork a subprocess; wait and retry if it fails. */
138 for (retries = 0; retries < 4; retries++)
143 sleep (sleep_interval);
150 *errmsg_fmt = "fork";
155 /* Move the input and output pipes into place, if necessary. */
156 if (input_desc != STDIN_FILE_NO)
158 close (STDIN_FILE_NO);
162 if (output_desc != STDOUT_FILE_NO)
164 close (STDOUT_FILE_NO);
169 /* Close the parent's descs that aren't wanted here. */
170 if (last_pipe_input != STDIN_FILE_NO)
171 close (last_pipe_input);
173 /* Exec the program. */
174 if (flags & PEXECUTE_SEARCH)
175 execvp (program, argv);
177 execv (program, argv);
179 /* We don't want to call fprintf after vfork. */
180 #define writeerr(s) write (STDERR_FILE_NO, s, strlen (s))
181 writeerr (this_pname);
183 writeerr ("installation problem, cannot exec '");
186 writeerr (xstrerror (errno));
193 /* In the parent, after forking.
194 Close the descriptors that we made for this child. */
195 if (input_desc != STDIN_FILE_NO)
197 if (output_desc != STDOUT_FILE_NO)
200 /* Return child's process number. */
206 pwait (pid, status, flags)
209 int flags ATTRIBUTE_UNUSED;
211 /* ??? Here's an opportunity to canonicalize the values in STATUS.
213 pid = waitpid (pid, status, 0);