1 /* Common code for executing a program in a sub-process.
2 Copyright (C) 2005 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@airs.com>.
5 This file is part of the libiberty library.
6 Libiberty is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 Libiberty is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with libiberty; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19 Boston, MA 02110-1301, USA. */
22 #include "libiberty.h"
23 #include "pex-common.h"
27 #ifdef NEED_DECLARATION_ERRNO
40 extern int mkstemps (char *, int);
42 /* This file contains subroutines for the program execution routines
43 (pex_init, pex_run, etc.). This file is compiled on all
46 static void pex_add_remove (struct pex_obj *, const char *, int);
47 static int pex_get_status_and_time (struct pex_obj *, int, const char **,
50 /* Initialize a pex_obj structure. */
53 pex_init_common (int flags, const char *pname, const char *tempbase,
54 const struct pex_funcs *funcs)
58 obj = xmalloc (sizeof (*obj));
61 obj->tempbase = tempbase;
62 obj->next_input = STDIN_FILE_NO;
63 obj->next_input_name = NULL;
64 obj->next_input_name_allocated = 0;
69 obj->number_waited = 0;
70 obj->read_output = NULL;
71 obj->remove_count = 0;
78 /* Add a file to be removed when we are done. */
81 pex_add_remove (struct pex_obj *obj, const char *name, int allocated)
86 obj->remove = xrealloc (obj->remove, obj->remove_count * sizeof (char *));
91 obj->remove[obj->remove_count - 1] = add;
97 pex_run (struct pex_obj *obj, int flags, const char *executable,
98 char * const * argv, const char *orig_outname, const char *errname,
104 int outname_allocated;
111 outname = (char *) orig_outname;
112 outname_allocated = 0;
116 if (obj->next_input_name != NULL)
118 /* We have to make sure that the previous process has completed
119 before we try to read the file. */
120 if (!pex_get_status_and_time (obj, 0, &errmsg, err))
123 in = obj->funcs->open_read (obj, obj->next_input_name,
124 (flags & PEX_BINARY_INPUT) != 0);
128 errmsg = "open temporary file";
131 if (obj->next_input_name_allocated)
133 free (obj->next_input_name);
134 obj->next_input_name_allocated = 0;
136 obj->next_input_name = NULL;
140 in = obj->next_input;
144 errmsg = "pipeline already complete";
149 /* Set OUT and OBJ->NEXT_INPUT/OBJ->NEXT_INPUT_NAME. */
151 if ((flags & PEX_LAST) != 0)
154 out = STDOUT_FILE_NO;
155 else if ((flags & PEX_SUFFIX) != 0)
157 outname = concat (obj->tempbase, outname, NULL);
158 outname_allocated = 1;
160 obj->next_input = -1;
162 else if ((obj->flags & PEX_USE_PIPES) == 0)
166 if (obj->tempbase == NULL)
168 outname = make_temp_file (NULL);
169 outname_allocated = 1;
173 int len = strlen (obj->tempbase);
176 && strcmp (obj->tempbase + len - 6, "XXXXXX") == 0)
177 outname = xstrdup (obj->tempbase);
179 outname = concat (obj->tempbase, "XXXXXX", NULL);
181 outname_allocated = 1;
183 out = mkstemps (outname, 0);
187 errmsg = "could not create temporary output file";
191 /* This isn't obj->funcs->close because we got the
192 descriptor from mkstemps, not from a function in
193 obj->funcs. Calling close here is just like what
194 make_temp_file does. */
199 else if ((flags & PEX_SUFFIX) != 0)
201 if (obj->tempbase == NULL)
202 outname = make_temp_file (outname);
204 outname = concat (obj->tempbase, outname, NULL);
205 outname_allocated = 1;
208 if ((obj->flags & PEX_SAVE_TEMPS) == 0)
210 pex_add_remove (obj, outname, outname_allocated);
211 outname_allocated = 0;
214 if (!outname_allocated)
216 obj->next_input_name = outname;
217 obj->next_input_name_allocated = 0;
221 obj->next_input_name = outname;
222 outname_allocated = 0;
223 obj->next_input_name_allocated = 1;
228 if (obj->funcs->pipe (obj, p, (flags & PEX_BINARY_OUTPUT) != 0) < 0)
236 obj->next_input = p[READ_PORT];
241 out = obj->funcs->open_write (obj, outname,
242 (flags & PEX_BINARY_OUTPUT) != 0);
246 errmsg = "open temporary output file";
251 if (outname_allocated)
254 outname_allocated = 0;
260 errdes = STDERR_FILE_NO;
263 /* We assume that stderr is in text mode--it certainly shouldn't
264 be controlled by PEX_BINARY_OUTPUT. If necessary, we can add
265 a PEX_BINARY_STDERR flag. */
266 errdes = obj->funcs->open_write (obj, errname, 0);
270 errmsg = "open error file";
275 /* Run the program. */
277 pid = obj->funcs->exec_child (obj, flags, executable, argv, in, out, errdes,
283 obj->children = xrealloc (obj->children, obj->count * sizeof (long));
284 obj->children[obj->count - 1] = pid;
289 if (in >= 0 && in != STDIN_FILE_NO)
290 obj->funcs->close (obj, in);
291 if (out >= 0 && out != STDOUT_FILE_NO)
292 obj->funcs->close (obj, out);
293 if (errdes >= 0 && errdes != STDERR_FILE_NO)
294 obj->funcs->close (obj, errdes);
295 if (outname_allocated)
300 /* Return a FILE pointer for the output of the last program
304 pex_read_output (struct pex_obj *obj, int binary)
306 if (obj->next_input_name != NULL)
311 /* We have to make sure that the process has completed before we
312 try to read the file. */
313 if (!pex_get_status_and_time (obj, 0, &errmsg, &err))
319 obj->read_output = fopen (obj->next_input_name, binary ? "rb" : "r");
321 if (obj->next_input_name_allocated)
323 free (obj->next_input_name);
324 obj->next_input_name_allocated = 0;
326 obj->next_input_name = NULL;
333 if (o < 0 || o == STDIN_FILE_NO)
335 obj->read_output = obj->funcs->fdopenr (obj, o, binary);
336 obj->next_input = -1;
339 return obj->read_output;
342 /* Get the exit status and, if requested, the resource time for all
343 the child processes. Return 0 on failure, 1 on success. */
346 pex_get_status_and_time (struct pex_obj *obj, int done, const char **errmsg,
352 if (obj->number_waited == obj->count)
355 obj->status = xrealloc (obj->status, obj->count * sizeof (int));
356 if ((obj->flags & PEX_RECORD_TIMES) != 0)
357 obj->time = xrealloc (obj->time, obj->count * sizeof (struct pex_time));
360 for (i = obj->number_waited; i < obj->count; ++i)
362 if (obj->funcs->wait (obj, obj->children[i], &obj->status[i],
363 obj->time == NULL ? NULL : &obj->time[i],
364 done, errmsg, err) < 0)
367 obj->number_waited = i;
372 /* Get exit status of executed programs. */
375 pex_get_status (struct pex_obj *obj, int count, int *vector)
377 if (obj->status == NULL)
382 if (!pex_get_status_and_time (obj, 0, &errmsg, &err))
386 if (count > obj->count)
388 memset (vector + obj->count, 0, (count - obj->count) * sizeof (int));
392 memcpy (vector, obj->status, count * sizeof (int));
397 /* Get process times of executed programs. */
400 pex_get_times (struct pex_obj *obj, int count, struct pex_time *vector)
402 if (obj->status == NULL)
407 if (!pex_get_status_and_time (obj, 0, &errmsg, &err))
411 if (obj->time == NULL)
414 if (count > obj->count)
416 memset (vector + obj->count, 0,
417 (count - obj->count) * sizeof (struct pex_time));
421 memcpy (vector, obj->time, count * sizeof (struct pex_time));
426 /* Free a pex_obj structure. */
429 pex_free (struct pex_obj *obj)
431 if (obj->next_input >= 0 && obj->next_input != STDIN_FILE_NO)
432 obj->funcs->close (obj, obj->next_input);
434 /* If the caller forgot to wait for the children, we do it here, to
436 if (obj->status == NULL)
441 obj->flags &= ~ PEX_RECORD_TIMES;
442 pex_get_status_and_time (obj, 1, &errmsg, &err);
445 if (obj->next_input_name_allocated)
446 free (obj->next_input_name);
447 if (obj->children != NULL)
448 free (obj->children);
449 if (obj->status != NULL)
451 if (obj->time != NULL)
453 if (obj->read_output != NULL)
454 fclose (obj->read_output);
456 if (obj->remove_count > 0)
460 for (i = 0; i < obj->remove_count; ++i)
462 remove (obj->remove[i]);
463 free (obj->remove[i]);
468 if (obj->funcs->cleanup != NULL)
469 obj->funcs->cleanup (obj);