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
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)
51 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
54 const char *this_pname
;
55 const char *temp_base ATTRIBUTE_UNUSED
;
56 char **errmsg_fmt
, **errmsg_arg
;
59 int (*func
)() = (flags
& PEXECUTE_SEARCH
? execvp
: execv
);
62 int input_desc
, output_desc
;
63 int retries
, sleep_interval
;
64 /* Pipe waiting from last process, to be used as input for the next one.
65 Value is STDIN_FILE_NO if no pipe is waiting
66 (i.e. the next command is the first of a group). */
67 static int last_pipe_input
;
69 /* If this is the first process, initialize. */
70 if (flags
& PEXECUTE_FIRST
)
71 last_pipe_input
= STDIN_FILE_NO
;
73 input_desc
= last_pipe_input
;
75 /* If this isn't the last process, make a pipe for its output,
76 and record it as waiting to be the input to the next process. */
77 if (! (flags
& PEXECUTE_LAST
))
85 output_desc
= pdes
[WRITE_PORT
];
86 last_pipe_input
= pdes
[READ_PORT
];
91 output_desc
= STDOUT_FILE_NO
;
92 last_pipe_input
= STDIN_FILE_NO
;
95 /* Fork a subprocess; wait and retry if it fails. */
98 for (retries
= 0; retries
< 4; retries
++)
103 sleep (sleep_interval
);
110 *errmsg_fmt
= "fork";
115 /* Move the input and output pipes into place, if necessary. */
116 if (input_desc
!= STDIN_FILE_NO
)
118 close (STDIN_FILE_NO
);
122 if (output_desc
!= STDOUT_FILE_NO
)
124 close (STDOUT_FILE_NO
);
129 /* Close the parent's descs that aren't wanted here. */
130 if (last_pipe_input
!= STDIN_FILE_NO
)
131 close (last_pipe_input
);
133 /* Exec the program. */
134 (*func
) (program
, argv
);
136 fprintf (stderr
, "%s: ", this_pname
);
137 fprintf (stderr
, install_error_msg
, program
);
138 fprintf (stderr
, ": %s\n", xstrerror (errno
));
144 /* In the parent, after forking.
145 Close the descriptors that we made for this child. */
146 if (input_desc
!= STDIN_FILE_NO
)
148 if (output_desc
!= STDOUT_FILE_NO
)
151 /* Return child's process number. */
157 pwait (pid
, status
, flags
)
160 int flags ATTRIBUTE_UNUSED
;
162 /* ??? Here's an opportunity to canonicalize the values in STATUS.
164 pid
= waitpid (pid
, status
, 0);