1 /* Utilities to execute a program in a subprocess (possibly linked by pipes
2 with other subprocesses), and wait for it. Shared logic.
3 Copyright (C) 1996-2018 Free Software Foundation, Inc.
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. */
25 #include "libiberty.h"
28 /* pid_t is may defined by config.h or sys/types.h needs to be
30 #if !defined(pid_t) && defined(HAVE_SYS_TYPES_H)
31 #include <sys/types.h>
34 #define install_error_msg "installation problem, cannot exec `%s'"
36 /* stdin file number. */
37 #define STDIN_FILE_NO 0
39 /* stdout file number. */
40 #define STDOUT_FILE_NO 1
42 /* stderr file number. */
43 #define STDERR_FILE_NO 2
45 /* value of `pipe': port index for reading. */
48 /* value of `pipe': port index for writing. */
51 /* The structure used by pex_init and friends. */
57 /* Name of calling program, for error messages. */
59 /* Base name to use for temporary files. */
61 /* Pipe to use as stdin for next process. */
63 /* File name to use as stdin for next process. */
64 char *next_input_name;
65 /* Whether next_input_name was allocated using malloc. */
66 int next_input_name_allocated;
67 /* If not -1, stderr pipe from the last process. */
69 /* Number of child processes. */
71 /* PIDs of child processes; array allocated using malloc. */
73 /* Exit statuses of child processes; array allocated using malloc. */
75 /* Time used by child processes; array allocated using malloc. */
76 struct pex_time *time;
77 /* Number of children we have already waited for. */
79 /* FILE created by pex_input_file. */
81 /* FILE created by pex_read_output. */
83 /* FILE created by pex_read_err. */
85 /* Number of temporary files to remove. */
87 /* List of temporary files to remove; array allocated using malloc
88 of strings allocated using malloc. */
90 /* Pointers to system dependent functions. */
91 const struct pex_funcs *funcs;
92 /* For use by system dependent code. */
96 /* Functions passed to pex_run_common. */
100 /* Open file NAME for reading. If BINARY is non-zero, open in
101 binary mode. Return >= 0 on success, -1 on error. */
102 int (*open_read) (struct pex_obj *, const char */* name */, int /* binary */);
103 /* Open file NAME for writing. If BINARY is non-zero, open in
104 binary mode. Return >= 0 on success, -1 on error. */
105 int (*open_write) (struct pex_obj *, const char */* name */,
106 int /* binary */, int /* append */);
107 /* Execute a child process. FLAGS, EXECUTABLE, ARGV, ERR are from
108 pex_run. IN, OUT, ERRDES, TOCLOSE are all descriptors, from
109 open_read, open_write, or pipe, or they are one of STDIN_FILE_NO,
110 STDOUT_FILE_NO or STDERR_FILE_NO; if IN, OUT, and ERRDES are not
111 STD*_FILE_NO, they should be closed. If the descriptor TOCLOSE
112 is not -1, and the system supports pipes, TOCLOSE should be
113 closed in the child process. The function should handle the
114 PEX_STDERR_TO_STDOUT flag. Return >= 0 on success, or -1 on
115 error and set *ERRMSG and *ERR. */
116 pid_t (*exec_child) (struct pex_obj *, int /* flags */,
117 const char */* executable */, char * const * /* argv */,
118 char * const * /* env */,
119 int /* in */, int /* out */, int /* errdes */,
120 int /* toclose */, const char **/* errmsg */,
122 /* Close a descriptor. Return 0 on success, -1 on error. */
123 int (*close) (struct pex_obj *, int);
124 /* Wait for a child to complete, returning exit status in *STATUS
125 and time in *TIME (if it is not null). CHILD is from fork. DONE
126 is 1 if this is called via pex_free. ERRMSG and ERR are as in
127 fork. Return 0 on success, -1 on error. */
128 pid_t (*wait) (struct pex_obj *, pid_t /* child */, int * /* status */,
129 struct pex_time * /* time */, int /* done */,
130 const char ** /* errmsg */, int * /* err */);
131 /* Create a pipe (only called if PEX_USE_PIPES is set) storing two
132 descriptors in P[0] and P[1]. If BINARY is non-zero, open in
133 binary mode. Return 0 on success, -1 on error. */
134 int (*pipe) (struct pex_obj *, int * /* p */, int /* binary */);
135 /* Get a FILE pointer to read from a file descriptor (only called if
136 PEX_USE_PIPES is set). If BINARY is non-zero, open in binary
137 mode. Return pointer on success, NULL on error. */
138 FILE * (*fdopenr) (struct pex_obj *, int /* fd */, int /* binary */);
139 /* Get a FILE pointer to write to the file descriptor FD (only
140 called if PEX_USE_PIPES is set). If BINARY is non-zero, open in
141 binary mode. Arrange for FD not to be inherited by the child
142 processes. Return pointer on success, NULL on error. */
143 FILE * (*fdopenw) (struct pex_obj *, int /* fd */, int /* binary */);
144 /* Free any system dependent data associated with OBJ. May be
145 NULL if there is nothing to do. */
146 void (*cleanup) (struct pex_obj *);
149 extern struct pex_obj *pex_init_common (int, const char *, const char *,
150 const struct pex_funcs *);