1 /* Common code for executing a program in a sub-process.
2 Copyright (C) 2005-2019 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;
65 obj
->stderr_pipe
= -1;
70 obj
->number_waited
= 0;
71 obj
->input_file
= NULL
;
72 obj
->read_output
= NULL
;
74 obj
->remove_count
= 0;
81 /* Add a file to be removed when we are done. */
84 pex_add_remove (struct pex_obj
*obj
, const char *name
, int allocated
)
89 obj
->remove
= XRESIZEVEC (char *, obj
->remove
, obj
->remove_count
);
94 obj
->remove
[obj
->remove_count
- 1] = add
;
97 /* Generate a temporary file name based on OBJ, FLAGS, and NAME.
98 Return NULL if we were unable to reserve a temporary filename.
100 If non-NULL, the result is either allocated with malloc, or the
101 same pointer as NAME. */
103 temp_file (struct pex_obj
*obj
, int flags
, char *name
)
107 if (obj
->tempbase
== NULL
)
109 name
= make_temp_file (NULL
);
113 int len
= strlen (obj
->tempbase
);
117 && strcmp (obj
->tempbase
+ len
- 6, "XXXXXX") == 0)
118 name
= xstrdup (obj
->tempbase
);
120 name
= concat (obj
->tempbase
, "XXXXXX", NULL
);
122 out
= mkstemps (name
, 0);
129 /* This isn't obj->funcs->close because we got the
130 descriptor from mkstemps, not from a function in
131 obj->funcs. Calling close here is just like what
132 make_temp_file does. */
136 else if ((flags
& PEX_SUFFIX
) != 0)
138 if (obj
->tempbase
== NULL
)
139 name
= make_temp_file (name
);
141 name
= concat (obj
->tempbase
, name
, NULL
);
148 /* As for pex_run (), but permits the environment for the child process
152 pex_run_in_environment (struct pex_obj
*obj
, int flags
, const char *executable
,
153 char * const * argv
, char * const * env
,
154 const char *orig_outname
, const char *errname
,
160 int outname_allocated
;
168 outname
= (char *) orig_outname
;
169 outname_allocated
= 0;
171 /* If the user called pex_input_file, close the file now. */
174 if (fclose (obj
->input_file
) == EOF
)
176 errmsg
= "closing pipeline input file";
179 obj
->input_file
= NULL
;
184 if (obj
->next_input_name
!= NULL
)
186 /* We have to make sure that the previous process has completed
187 before we try to read the file. */
188 if (!pex_get_status_and_time (obj
, 0, &errmsg
, err
))
191 in
= obj
->funcs
->open_read (obj
, obj
->next_input_name
,
192 (flags
& PEX_BINARY_INPUT
) != 0);
196 errmsg
= "open temporary file";
199 if (obj
->next_input_name_allocated
)
201 free (obj
->next_input_name
);
202 obj
->next_input_name_allocated
= 0;
204 obj
->next_input_name
= NULL
;
208 in
= obj
->next_input
;
212 errmsg
= "pipeline already complete";
217 /* Set OUT and OBJ->NEXT_INPUT/OBJ->NEXT_INPUT_NAME. */
219 if ((flags
& PEX_LAST
) != 0)
222 out
= STDOUT_FILE_NO
;
223 else if ((flags
& PEX_SUFFIX
) != 0)
225 outname
= concat (obj
->tempbase
, outname
, NULL
);
226 outname_allocated
= 1;
228 obj
->next_input
= -1;
230 else if ((obj
->flags
& PEX_USE_PIPES
) == 0)
232 outname
= temp_file (obj
, flags
, outname
);
236 errmsg
= "could not create temporary file";
240 if (outname
!= orig_outname
)
241 outname_allocated
= 1;
243 if ((obj
->flags
& PEX_SAVE_TEMPS
) == 0)
245 pex_add_remove (obj
, outname
, outname_allocated
);
246 outname_allocated
= 0;
249 /* Hand off ownership of outname to the next stage. */
250 obj
->next_input_name
= outname
;
251 obj
->next_input_name_allocated
= outname_allocated
;
252 outname_allocated
= 0;
256 if (obj
->funcs
->pipe (obj
, p
, (flags
& PEX_BINARY_OUTPUT
) != 0) < 0)
264 obj
->next_input
= p
[READ_PORT
];
269 out
= obj
->funcs
->open_write (obj
, outname
,
270 (flags
& PEX_BINARY_OUTPUT
) != 0,
271 (flags
& PEX_STDOUT_APPEND
) != 0);
275 errmsg
= "open temporary output file";
280 if (outname_allocated
)
283 outname_allocated
= 0;
288 if (errname
!= NULL
&& (flags
& PEX_STDERR_TO_PIPE
) != 0)
291 errmsg
= "both ERRNAME and PEX_STDERR_TO_PIPE specified.";
295 if (obj
->stderr_pipe
!= -1)
298 errmsg
= "PEX_STDERR_TO_PIPE used in the middle of pipeline";
304 if (flags
& PEX_STDERR_TO_PIPE
)
306 if (obj
->funcs
->pipe (obj
, p
, (flags
& PEX_BINARY_ERROR
) != 0) < 0)
313 errdes
= p
[WRITE_PORT
];
314 obj
->stderr_pipe
= p
[READ_PORT
];
318 errdes
= STDERR_FILE_NO
;
323 errdes
= obj
->funcs
->open_write (obj
, errname
,
324 (flags
& PEX_BINARY_ERROR
) != 0,
325 (flags
& PEX_STDERR_APPEND
) != 0);
329 errmsg
= "open error file";
334 /* If we are using pipes, the child process has to close the next
337 if ((obj
->flags
& PEX_USE_PIPES
) == 0)
340 toclose
= obj
->next_input
;
342 /* Run the program. */
344 pid
= obj
->funcs
->exec_child (obj
, flags
, executable
, argv
, env
,
345 in
, out
, errdes
, toclose
, &errmsg
, err
);
350 obj
->children
= XRESIZEVEC (pid_t
, obj
->children
, obj
->count
);
351 obj
->children
[obj
->count
- 1] = pid
;
356 if (in
>= 0 && in
!= STDIN_FILE_NO
)
357 obj
->funcs
->close (obj
, in
);
358 if (out
>= 0 && out
!= STDOUT_FILE_NO
)
359 obj
->funcs
->close (obj
, out
);
360 if (errdes
>= 0 && errdes
!= STDERR_FILE_NO
)
361 obj
->funcs
->close (obj
, errdes
);
362 if (outname_allocated
)
370 pex_run (struct pex_obj
*obj
, int flags
, const char *executable
,
371 char * const * argv
, const char *orig_outname
, const char *errname
,
374 return pex_run_in_environment (obj
, flags
, executable
, argv
, NULL
,
375 orig_outname
, errname
, err
);
378 /* Return a FILE pointer for a temporary file to fill with input for
381 pex_input_file (struct pex_obj
*obj
, int flags
, const char *in_name
)
383 char *name
= (char *) in_name
;
386 /* This must be called before the first pipeline stage is run, and
387 there must not have been any other input selected. */
389 || (obj
->next_input
>= 0 && obj
->next_input
!= STDIN_FILE_NO
)
390 || obj
->next_input_name
)
396 name
= temp_file (obj
, flags
, name
);
400 f
= fopen (name
, (flags
& PEX_BINARY_OUTPUT
) ? "wb" : "w");
408 obj
->next_input_name
= name
;
409 obj
->next_input_name_allocated
= (name
!= in_name
);
414 /* Return a stream for a pipe connected to the standard input of the
415 first stage of the pipeline. */
417 pex_input_pipe (struct pex_obj
*obj
, int binary
)
422 /* You must call pex_input_pipe before the first pex_run or pex_one. */
426 /* You must be using pipes. Implementations that don't support
427 pipes clear this flag before calling pex_init_common. */
428 if (! (obj
->flags
& PEX_USE_PIPES
))
431 /* If we have somehow already selected other input, that's a
433 if ((obj
->next_input
>= 0 && obj
->next_input
!= STDIN_FILE_NO
)
434 || obj
->next_input_name
)
437 if (obj
->funcs
->pipe (obj
, p
, binary
!= 0) < 0)
440 f
= obj
->funcs
->fdopenw (obj
, p
[WRITE_PORT
], binary
!= 0);
443 int saved_errno
= errno
;
444 obj
->funcs
->close (obj
, p
[READ_PORT
]);
445 obj
->funcs
->close (obj
, p
[WRITE_PORT
]);
450 obj
->next_input
= p
[READ_PORT
];
459 /* Return a FILE pointer for the output of the last program
463 pex_read_output (struct pex_obj
*obj
, int binary
)
465 if (obj
->next_input_name
!= NULL
)
470 /* We have to make sure that the process has completed before we
471 try to read the file. */
472 if (!pex_get_status_and_time (obj
, 0, &errmsg
, &err
))
478 obj
->read_output
= fopen (obj
->next_input_name
, binary
? "rb" : "r");
480 if (obj
->next_input_name_allocated
)
482 free (obj
->next_input_name
);
483 obj
->next_input_name_allocated
= 0;
485 obj
->next_input_name
= NULL
;
492 if (o
< 0 || o
== STDIN_FILE_NO
)
494 obj
->read_output
= obj
->funcs
->fdopenr (obj
, o
, binary
);
495 obj
->next_input
= -1;
498 return obj
->read_output
;
502 pex_read_err (struct pex_obj
*obj
, int binary
)
506 o
= obj
->stderr_pipe
;
507 if (o
< 0 || o
== STDIN_FILE_NO
)
509 obj
->read_err
= obj
->funcs
->fdopenr (obj
, o
, binary
);
510 obj
->stderr_pipe
= -1;
511 return obj
->read_err
;
514 /* Get the exit status and, if requested, the resource time for all
515 the child processes. Return 0 on failure, 1 on success. */
518 pex_get_status_and_time (struct pex_obj
*obj
, int done
, const char **errmsg
,
524 if (obj
->number_waited
== obj
->count
)
527 obj
->status
= XRESIZEVEC (int, obj
->status
, obj
->count
);
528 if ((obj
->flags
& PEX_RECORD_TIMES
) != 0)
529 obj
->time
= XRESIZEVEC (struct pex_time
, obj
->time
, obj
->count
);
532 for (i
= obj
->number_waited
; i
< obj
->count
; ++i
)
534 if (obj
->funcs
->wait (obj
, obj
->children
[i
], &obj
->status
[i
],
535 obj
->time
== NULL
? NULL
: &obj
->time
[i
],
536 done
, errmsg
, err
) < 0)
539 obj
->number_waited
= i
;
544 /* Get exit status of executed programs. */
547 pex_get_status (struct pex_obj
*obj
, int count
, int *vector
)
549 if (obj
->status
== NULL
)
554 if (!pex_get_status_and_time (obj
, 0, &errmsg
, &err
))
558 if (count
> obj
->count
)
560 memset (vector
+ obj
->count
, 0, (count
- obj
->count
) * sizeof (int));
564 memcpy (vector
, obj
->status
, count
* sizeof (int));
569 /* Get process times of executed programs. */
572 pex_get_times (struct pex_obj
*obj
, int count
, struct pex_time
*vector
)
574 if (obj
->status
== NULL
)
579 if (!pex_get_status_and_time (obj
, 0, &errmsg
, &err
))
583 if (obj
->time
== NULL
)
586 if (count
> obj
->count
)
588 memset (vector
+ obj
->count
, 0,
589 (count
- obj
->count
) * sizeof (struct pex_time
));
593 memcpy (vector
, obj
->time
, count
* sizeof (struct pex_time
));
598 /* Free a pex_obj structure. */
601 pex_free (struct pex_obj
*obj
)
603 /* Close pipe file descriptors corresponding to child's stdout and
604 stderr so that the child does not hang trying to output something
605 while we're waiting for it. */
606 if (obj
->next_input
>= 0 && obj
->next_input
!= STDIN_FILE_NO
)
607 obj
->funcs
->close (obj
, obj
->next_input
);
608 if (obj
->stderr_pipe
>= 0 && obj
->stderr_pipe
!= STDIN_FILE_NO
)
609 obj
->funcs
->close (obj
, obj
->stderr_pipe
);
610 if (obj
->read_output
!= NULL
)
611 fclose (obj
->read_output
);
612 if (obj
->read_err
!= NULL
)
613 fclose (obj
->read_err
);
615 /* If the caller forgot to wait for the children, we do it here, to
617 if (obj
->status
== NULL
)
622 obj
->flags
&= ~ PEX_RECORD_TIMES
;
623 pex_get_status_and_time (obj
, 1, &errmsg
, &err
);
626 if (obj
->next_input_name_allocated
)
627 free (obj
->next_input_name
);
628 free (obj
->children
);
632 if (obj
->remove_count
> 0)
636 for (i
= 0; i
< obj
->remove_count
; ++i
)
638 remove (obj
->remove
[i
]);
639 free (obj
->remove
[i
]);
644 if (obj
->funcs
->cleanup
!= NULL
)
645 obj
->funcs
->cleanup (obj
);