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
= XNEW (struct pex_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
->input_file
= NULL
;
71 obj
->read_output
= NULL
;
72 obj
->remove_count
= 0;
79 /* Add a file to be removed when we are done. */
82 pex_add_remove (struct pex_obj
*obj
, const char *name
, int allocated
)
87 obj
->remove
= XRESIZEVEC (char *, obj
->remove
, obj
->remove_count
);
92 obj
->remove
[obj
->remove_count
- 1] = add
;
95 /* Generate a temporary file name based on OBJ, FLAGS, and NAME.
96 Return NULL if we were unable to reserve a temporary filename.
98 If non-NULL, the result is either allocated with malloc, or the
99 same pointer as NAME. */
101 temp_file (struct pex_obj
*obj
, int flags
, char *name
)
105 if (obj
->tempbase
== NULL
)
107 name
= make_temp_file (NULL
);
111 int len
= strlen (obj
->tempbase
);
115 && strcmp (obj
->tempbase
+ len
- 6, "XXXXXX") == 0)
116 name
= xstrdup (obj
->tempbase
);
118 name
= concat (obj
->tempbase
, "XXXXXX", NULL
);
120 out
= mkstemps (name
, 0);
127 /* This isn't obj->funcs->close because we got the
128 descriptor from mkstemps, not from a function in
129 obj->funcs. Calling close here is just like what
130 make_temp_file does. */
134 else if ((flags
& PEX_SUFFIX
) != 0)
136 if (obj
->tempbase
== NULL
)
137 name
= make_temp_file (name
);
139 name
= concat (obj
->tempbase
, name
, NULL
);
146 /* As for pex_run (), but permits the environment for the child process
150 pex_run_in_environment (struct pex_obj
*obj
, int flags
, const char *executable
,
151 char * const * argv
, char * const * env
,
152 const char *orig_outname
, const char *errname
,
158 int outname_allocated
;
167 outname
= (char *) orig_outname
;
168 outname_allocated
= 0;
170 /* If the user called pex_input_file, close the file now. */
173 if (fclose (obj
->input_file
) == EOF
)
175 errmsg
= "closing pipeline input file";
178 obj
->input_file
= NULL
;
183 if (obj
->next_input_name
!= NULL
)
185 /* We have to make sure that the previous process has completed
186 before we try to read the file. */
187 if (!pex_get_status_and_time (obj
, 0, &errmsg
, err
))
190 in
= obj
->funcs
->open_read (obj
, obj
->next_input_name
,
191 (flags
& PEX_BINARY_INPUT
) != 0);
195 errmsg
= "open temporary file";
198 if (obj
->next_input_name_allocated
)
200 free (obj
->next_input_name
);
201 obj
->next_input_name_allocated
= 0;
203 obj
->next_input_name
= NULL
;
207 in
= obj
->next_input
;
211 errmsg
= "pipeline already complete";
216 /* Set OUT and OBJ->NEXT_INPUT/OBJ->NEXT_INPUT_NAME. */
218 if ((flags
& PEX_LAST
) != 0)
221 out
= STDOUT_FILE_NO
;
222 else if ((flags
& PEX_SUFFIX
) != 0)
224 outname
= concat (obj
->tempbase
, outname
, NULL
);
225 outname_allocated
= 1;
227 obj
->next_input
= -1;
229 else if ((obj
->flags
& PEX_USE_PIPES
) == 0)
231 outname
= temp_file (obj
, flags
, outname
);
235 errmsg
= "could not create temporary file";
239 if (outname
!= orig_outname
)
240 outname_allocated
= 1;
242 if ((obj
->flags
& PEX_SAVE_TEMPS
) == 0)
244 pex_add_remove (obj
, outname
, outname_allocated
);
245 outname_allocated
= 0;
248 /* Hand off ownership of outname to the next stage. */
249 obj
->next_input_name
= outname
;
250 obj
->next_input_name_allocated
= outname_allocated
;
251 outname_allocated
= 0;
255 if (obj
->funcs
->pipe (obj
, p
, (flags
& PEX_BINARY_OUTPUT
) != 0) < 0)
263 obj
->next_input
= p
[READ_PORT
];
268 out
= obj
->funcs
->open_write (obj
, outname
,
269 (flags
& PEX_BINARY_OUTPUT
) != 0);
273 errmsg
= "open temporary output file";
278 if (outname_allocated
)
281 outname_allocated
= 0;
287 errdes
= STDERR_FILE_NO
;
290 /* We assume that stderr is in text mode--it certainly shouldn't
291 be controlled by PEX_BINARY_OUTPUT. If necessary, we can add
292 a PEX_BINARY_STDERR flag. */
293 errdes
= obj
->funcs
->open_write (obj
, errname
, 0);
297 errmsg
= "open error file";
302 /* Run the program. */
304 pid
= obj
->funcs
->exec_child (obj
, flags
, executable
, argv
, env
,
305 in
, out
, errdes
, &errmsg
, err
);
306 if (p
[WRITE_PORT
] != -1)
307 obj
->funcs
->close (obj
, p
[WRITE_PORT
]);
312 obj
->children
= XRESIZEVEC (long, obj
->children
, obj
->count
);
313 obj
->children
[obj
->count
- 1] = pid
;
318 if (in
>= 0 && in
!= STDIN_FILE_NO
)
319 obj
->funcs
->close (obj
, in
);
320 if (out
>= 0 && out
!= STDOUT_FILE_NO
)
321 obj
->funcs
->close (obj
, out
);
322 if (errdes
>= 0 && errdes
!= STDERR_FILE_NO
)
323 obj
->funcs
->close (obj
, errdes
);
324 if (outname_allocated
)
329 /* Return a FILE pointer for the input of the first program
333 pex_write_input (struct pex_obj
*obj
, int binary
)
338 /* You must call pex_write_input before the first pex_run or pex_one. */
342 /* You must be using pipes. Implementations that don't support
343 pipes clear this flag before calling pex_init_common. */
344 if (! (obj
->flags
& PEX_USE_PIPES
))
347 /* If we have somehow already selected other input, that's a
349 if ((obj
->next_input
>= 0 && obj
->next_input
!= STDIN_FILE_NO
)
350 || obj
->next_input_name
)
353 if (obj
->funcs
->pipe (obj
, p
, binary
!= 0) < 0)
356 write_input
= obj
->funcs
->fdopenw (obj
, p
[WRITE_PORT
], binary
!= 0);
359 int saved_errno
= errno
;
360 obj
->funcs
->close (obj
, p
[READ_PORT
]);
361 obj
->funcs
->close (obj
, p
[WRITE_PORT
]);
366 obj
->next_input
= p
[READ_PORT
];
378 pex_run (struct pex_obj
*obj
, int flags
, const char *executable
,
379 char * const * argv
, const char *orig_outname
, const char *errname
,
382 return pex_run_in_environment (obj
, flags
, executable
, argv
, NULL
,
383 orig_outname
, errname
, err
);
386 /* Return a FILE pointer for a temporary file to fill with input for
389 pex_input_file (struct pex_obj
*obj
, int flags
, const char *in_name
)
391 char *name
= (char *) in_name
;
394 /* This must be called before the first pipeline stage is run, and
395 there must not have been any other input selected. */
397 || (obj
->next_input
>= 0 && obj
->next_input
!= STDIN_FILE_NO
)
398 || obj
->next_input_name
)
404 name
= temp_file (obj
, flags
, name
);
408 f
= fopen (name
, (flags
& PEX_BINARY_OUTPUT
) ? "wb" : "w");
416 obj
->next_input_name
= name
;
417 obj
->next_input_name_allocated
= (name
!= in_name
);
422 /* Return a stream for a pipe connected to the standard input of the
423 first stage of the pipeline. */
425 pex_input_pipe (struct pex_obj
*obj
, int binary
)
430 /* You must call pex_input_pipe before the first pex_run or pex_one. */
434 /* You must be using pipes. Implementations that don't support
435 pipes clear this flag before calling pex_init_common. */
436 if (! (obj
->flags
& PEX_USE_PIPES
))
439 /* If we have somehow already selected other input, that's a
441 if ((obj
->next_input
>= 0 && obj
->next_input
!= STDIN_FILE_NO
)
442 || obj
->next_input_name
)
445 if (obj
->funcs
->pipe (obj
, p
, binary
!= 0) < 0)
448 f
= obj
->funcs
->fdopenw (obj
, p
[WRITE_PORT
], binary
!= 0);
451 int saved_errno
= errno
;
452 obj
->funcs
->close (obj
, p
[READ_PORT
]);
453 obj
->funcs
->close (obj
, p
[WRITE_PORT
]);
458 obj
->next_input
= p
[READ_PORT
];
467 /* Return a FILE pointer for the output of the last program
471 pex_read_output (struct pex_obj
*obj
, int binary
)
473 if (obj
->next_input_name
!= NULL
)
478 /* We have to make sure that the process has completed before we
479 try to read the file. */
480 if (!pex_get_status_and_time (obj
, 0, &errmsg
, &err
))
486 obj
->read_output
= fopen (obj
->next_input_name
, binary
? "rb" : "r");
488 if (obj
->next_input_name_allocated
)
490 free (obj
->next_input_name
);
491 obj
->next_input_name_allocated
= 0;
493 obj
->next_input_name
= NULL
;
500 if (o
< 0 || o
== STDIN_FILE_NO
)
502 obj
->read_output
= obj
->funcs
->fdopenr (obj
, o
, binary
);
503 obj
->next_input
= -1;
506 return obj
->read_output
;
509 /* Get the exit status and, if requested, the resource time for all
510 the child processes. Return 0 on failure, 1 on success. */
513 pex_get_status_and_time (struct pex_obj
*obj
, int done
, const char **errmsg
,
519 if (obj
->number_waited
== obj
->count
)
522 obj
->status
= XRESIZEVEC (int, obj
->status
, obj
->count
);
523 if ((obj
->flags
& PEX_RECORD_TIMES
) != 0)
524 obj
->time
= XRESIZEVEC (struct pex_time
, obj
->time
, obj
->count
);
527 for (i
= obj
->number_waited
; i
< obj
->count
; ++i
)
529 if (obj
->funcs
->wait (obj
, obj
->children
[i
], &obj
->status
[i
],
530 obj
->time
== NULL
? NULL
: &obj
->time
[i
],
531 done
, errmsg
, err
) < 0)
534 obj
->number_waited
= i
;
539 /* Get exit status of executed programs. */
542 pex_get_status (struct pex_obj
*obj
, int count
, int *vector
)
544 if (obj
->status
== NULL
)
549 if (!pex_get_status_and_time (obj
, 0, &errmsg
, &err
))
553 if (count
> obj
->count
)
555 memset (vector
+ obj
->count
, 0, (count
- obj
->count
) * sizeof (int));
559 memcpy (vector
, obj
->status
, count
* sizeof (int));
564 /* Get process times of executed programs. */
567 pex_get_times (struct pex_obj
*obj
, int count
, struct pex_time
*vector
)
569 if (obj
->status
== NULL
)
574 if (!pex_get_status_and_time (obj
, 0, &errmsg
, &err
))
578 if (obj
->time
== NULL
)
581 if (count
> obj
->count
)
583 memset (vector
+ obj
->count
, 0,
584 (count
- obj
->count
) * sizeof (struct pex_time
));
588 memcpy (vector
, obj
->time
, count
* sizeof (struct pex_time
));
593 /* Free a pex_obj structure. */
596 pex_free (struct pex_obj
*obj
)
598 if (obj
->next_input
>= 0 && obj
->next_input
!= STDIN_FILE_NO
)
599 obj
->funcs
->close (obj
, obj
->next_input
);
601 /* If the caller forgot to wait for the children, we do it here, to
603 if (obj
->status
== NULL
)
608 obj
->flags
&= ~ PEX_RECORD_TIMES
;
609 pex_get_status_and_time (obj
, 1, &errmsg
, &err
);
612 if (obj
->next_input_name_allocated
)
613 free (obj
->next_input_name
);
614 if (obj
->children
!= NULL
)
615 free (obj
->children
);
616 if (obj
->status
!= NULL
)
618 if (obj
->time
!= NULL
)
620 if (obj
->read_output
!= NULL
)
621 fclose (obj
->read_output
);
623 if (obj
->remove_count
> 0)
627 for (i
= 0; i
< obj
->remove_count
; ++i
)
629 remove (obj
->remove
[i
]);
630 free (obj
->remove
[i
]);
635 if (obj
->funcs
->cleanup
!= NULL
)
636 obj
->funcs
->cleanup (obj
);