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, 1997, 1998, 1999, 2000, 2001, 2003, 2004
4 Free Software Foundation, Inc.
6 This file is part of the libiberty library.
7 Libiberty is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
12 Libiberty is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with libiberty; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 Boston, MA 02110-1301, USA. */
26 #include "libiberty.h"
29 /* pid_t is may defined by config.h or sys/types.h needs to be
31 #if !defined(pid_t) && defined(HAVE_SYS_TYPES_H)
32 #include <sys/types.h>
35 #define install_error_msg "installation problem, cannot exec `%s'"
37 /* stdin file number. */
38 #define STDIN_FILE_NO 0
40 /* stdout file number. */
41 #define STDOUT_FILE_NO 1
43 /* stderr file number. */
44 #define STDERR_FILE_NO 2
46 /* value of `pipe': port index for reading. */
49 /* value of `pipe': port index for writing. */
52 /* The structure used by pex_init and friends. */
58 /* Name of calling program, for error messages. */
60 /* Base name to use for temporary files. */
62 /* Pipe to use as stdin for next process. */
64 /* File name to use as stdin for next process. */
65 char *next_input_name
;
66 /* Whether next_input_name was allocated using malloc. */
67 int next_input_name_allocated
;
68 /* If not -1, stderr pipe from the last process. */
70 /* Number of child processes. */
72 /* PIDs of child processes; array allocated using malloc. */
74 /* Exit statuses of child processes; array allocated using malloc. */
76 /* Time used by child processes; array allocated using malloc. */
77 struct pex_time
*time
;
78 /* Number of children we have already waited for. */
80 /* FILE created by pex_input_file. */
82 /* FILE created by pex_read_output. */
84 /* FILE created by pex_read_err. */
86 /* Number of temporary files to remove. */
88 /* List of temporary files to remove; array allocated using malloc
89 of strings allocated using malloc. */
91 /* Pointers to system dependent functions. */
92 const struct pex_funcs
*funcs
;
93 /* For use by system dependent code. */
97 /* Functions passed to pex_run_common. */
101 /* Open file NAME for reading. If BINARY is non-zero, open in
102 binary mode. Return >= 0 on success, -1 on error. */
103 int (*open_read
) (struct pex_obj
*, const char */
* name */
, int /* binary */);
104 /* Open file NAME for writing. If BINARY is non-zero, open in
105 binary mode. Return >= 0 on success, -1 on error. */
106 int (*open_write
) (struct pex_obj
*, const char */
* name */
,
107 int /* binary */, int /* append */);
108 /* Execute a child process. FLAGS, EXECUTABLE, ARGV, ERR are from
109 pex_run. IN, OUT, ERRDES, TOCLOSE are all descriptors, from
110 open_read, open_write, or pipe, or they are one of STDIN_FILE_NO,
111 STDOUT_FILE_NO or STDERR_FILE_NO; if IN, OUT, and ERRDES are not
112 STD*_FILE_NO, they should be closed. If the descriptor TOCLOSE
113 is not -1, and the system supports pipes, TOCLOSE should be
114 closed in the child process. The function should handle the
115 PEX_STDERR_TO_STDOUT flag. Return >= 0 on success, or -1 on
116 error and set *ERRMSG and *ERR. */
117 pid_t (*exec_child
) (struct pex_obj
*, int /* flags */,
118 const char */
* executable */
, char * const * /* argv */,
119 char * const * /* env */,
120 int /* in */, int /* out */, int /* errdes */,
121 int /* toclose */, const char **/
* errmsg */
,
123 /* Close a descriptor. Return 0 on success, -1 on error. */
124 int (*close
) (struct pex_obj
*, int);
125 /* Wait for a child to complete, returning exit status in *STATUS
126 and time in *TIME (if it is not null). CHILD is from fork. DONE
127 is 1 if this is called via pex_free. ERRMSG and ERR are as in
128 fork. Return 0 on success, -1 on error. */
129 pid_t (*wait
) (struct pex_obj
*, pid_t
/* child */, int * /* status */,
130 struct pex_time
* /* time */, int /* done */,
131 const char ** /* errmsg */, int * /* err */);
132 /* Create a pipe (only called if PEX_USE_PIPES is set) storing two
133 descriptors in P[0] and P[1]. If BINARY is non-zero, open in
134 binary mode. Return 0 on success, -1 on error. */
135 int (*pipe
) (struct pex_obj
*, int * /* p */, int /* binary */);
136 /* Get a FILE pointer to read from a file descriptor (only called if
137 PEX_USE_PIPES is set). If BINARY is non-zero, open in binary
138 mode. Return pointer on success, NULL on error. */
139 FILE * (*fdopenr
) (struct pex_obj
*, int /* fd */, int /* binary */);
140 /* Get a FILE pointer to write to the file descriptor FD (only
141 called if PEX_USE_PIPES is set). If BINARY is non-zero, open in
142 binary mode. Arrange for FD not to be inherited by the child
143 processes. Return pointer on success, NULL on error. */
144 FILE * (*fdopenw
) (struct pex_obj
*, int /* fd */, int /* binary */);
145 /* Free any system dependent data associated with OBJ. May be
146 NULL if there is nothing to do. */
147 void (*cleanup
) (struct pex_obj
*);
150 extern struct pex_obj
*pex_init_common (int, const char *, const char *,
151 const struct pex_funcs
*);