Commit | Line | Data |
---|---|---|
5a17353c DD |
1 | /* Utilities to execute a program in a subprocess (possibly linked by pipes |
2 | with other subprocesses), and wait for it. Shared logic. | |
12a7367e | 3 | Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2004 |
5a17353c DD |
4 | Free Software Foundation, Inc. |
5 | ||
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. | |
11 | ||
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. | |
16 | ||
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, | |
979c05d3 NC |
19 | write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, |
20 | Boston, MA 02110-1301, USA. */ | |
5a17353c DD |
21 | |
22 | #ifndef PEX_COMMON_H | |
23 | #define PEX_COMMON_H | |
24 | ||
25 | #include "config.h" | |
26 | #include "libiberty.h" | |
b109e79a | 27 | #include <stdio.h> |
5a17353c DD |
28 | |
29 | #define install_error_msg "installation problem, cannot exec `%s'" | |
30 | ||
31 | /* stdin file number. */ | |
32 | #define STDIN_FILE_NO 0 | |
33 | ||
34 | /* stdout file number. */ | |
35 | #define STDOUT_FILE_NO 1 | |
36 | ||
12a7367e DD |
37 | /* stderr file number. */ |
38 | #define STDERR_FILE_NO 2 | |
39 | ||
5a17353c DD |
40 | /* value of `pipe': port index for reading. */ |
41 | #define READ_PORT 0 | |
42 | ||
43 | /* value of `pipe': port index for writing. */ | |
44 | #define WRITE_PORT 1 | |
45 | ||
b109e79a ILT |
46 | /* The structure used by pex_init and friends. */ |
47 | ||
48 | struct pex_obj | |
49 | { | |
50 | /* Flags. */ | |
51 | int flags; | |
52 | /* Name of calling program, for error messages. */ | |
53 | const char *pname; | |
54 | /* Base name to use for temporary files. */ | |
55 | const char *tempbase; | |
56 | /* Pipe to use as stdin for next process. */ | |
57 | int next_input; | |
58 | /* File name to use as stdin for next process. */ | |
59 | char *next_input_name; | |
60 | /* Whether next_input_name was allocated using malloc. */ | |
61 | int next_input_name_allocated; | |
53d7966f VP |
62 | /* If not -1, stderr pipe from the last process. */ |
63 | int stderr_pipe; | |
b109e79a ILT |
64 | /* Number of child processes. */ |
65 | int count; | |
9223c945 | 66 | /* PIDs of child processes; array allocated using malloc. */ |
b109e79a ILT |
67 | long *children; |
68 | /* Exit statuses of child processes; array allocated using malloc. */ | |
69 | int *status; | |
70 | /* Time used by child processes; array allocated using malloc. */ | |
71 | struct pex_time *time; | |
72 | /* Number of children we have already waited for. */ | |
73 | int number_waited; | |
3db2e6dd DD |
74 | /* FILE created by pex_input_file. */ |
75 | FILE *input_file; | |
b109e79a ILT |
76 | /* FILE created by pex_read_output. */ |
77 | FILE *read_output; | |
53d7966f VP |
78 | /* FILE created by pex_read_err. */ |
79 | FILE *read_err; | |
b109e79a ILT |
80 | /* Number of temporary files to remove. */ |
81 | int remove_count; | |
82 | /* List of temporary files to remove; array allocated using malloc | |
83 | of strings allocated using malloc. */ | |
84 | char **remove; | |
85 | /* Pointers to system dependent functions. */ | |
86 | const struct pex_funcs *funcs; | |
87 | /* For use by system dependent code. */ | |
88 | void *sysdep; | |
89 | }; | |
90 | ||
91 | /* Functions passed to pex_run_common. */ | |
92 | ||
93 | struct pex_funcs | |
94 | { | |
95 | /* Open file NAME for reading. If BINARY is non-zero, open in | |
96 | binary mode. Return >= 0 on success, -1 on error. */ | |
9223c945 | 97 | int (*open_read) (struct pex_obj *, const char */* name */, int /* binary */); |
b109e79a ILT |
98 | /* Open file NAME for writing. If BINARY is non-zero, open in |
99 | binary mode. Return >= 0 on success, -1 on error. */ | |
9223c945 DD |
100 | int (*open_write) (struct pex_obj *, const char */* name */, |
101 | int /* binary */); | |
b109e79a | 102 | /* Execute a child process. FLAGS, EXECUTABLE, ARGV, ERR are from |
cb6c09ac DD |
103 | pex_run. IN, OUT, ERRDES, TOCLOSE are all descriptors, from |
104 | open_read, open_write, or pipe, or they are one of STDIN_FILE_NO, | |
105 | STDOUT_FILE_NO or STDERR_FILE_NO; if IN, OUT, and ERRDES are not | |
106 | STD*_FILE_NO, they should be closed. If the descriptor TOCLOSE | |
107 | is not -1, and the system supports pipes, TOCLOSE should be | |
108 | closed in the child process. The function should handle the | |
b109e79a ILT |
109 | PEX_STDERR_TO_STDOUT flag. Return >= 0 on success, or -1 on |
110 | error and set *ERRMSG and *ERR. */ | |
9223c945 DD |
111 | long (*exec_child) (struct pex_obj *, int /* flags */, |
112 | const char */* executable */, char * const * /* argv */, | |
014a8caf | 113 | char * const * /* env */, |
9223c945 | 114 | int /* in */, int /* out */, int /* errdes */, |
cb6c09ac DD |
115 | int /* toclose */, const char **/* errmsg */, |
116 | int */* err */); | |
b109e79a ILT |
117 | /* Close a descriptor. Return 0 on success, -1 on error. */ |
118 | int (*close) (struct pex_obj *, int); | |
119 | /* Wait for a child to complete, returning exit status in *STATUS | |
120 | and time in *TIME (if it is not null). CHILD is from fork. DONE | |
121 | is 1 if this is called via pex_free. ERRMSG and ERR are as in | |
122 | fork. Return 0 on success, -1 on error. */ | |
9223c945 DD |
123 | int (*wait) (struct pex_obj *, long /* child */, int * /* status */, |
124 | struct pex_time * /* time */, int /* done */, | |
125 | const char ** /* errmsg */, int * /* err */); | |
b109e79a | 126 | /* Create a pipe (only called if PEX_USE_PIPES is set) storing two |
9223c945 DD |
127 | descriptors in P[0] and P[1]. If BINARY is non-zero, open in |
128 | binary mode. Return 0 on success, -1 on error. */ | |
129 | int (*pipe) (struct pex_obj *, int * /* p */, int /* binary */); | |
b109e79a ILT |
130 | /* Get a FILE pointer to read from a file descriptor (only called if |
131 | PEX_USE_PIPES is set). If BINARY is non-zero, open in binary | |
132 | mode. Return pointer on success, NULL on error. */ | |
9223c945 | 133 | FILE * (*fdopenr) (struct pex_obj *, int /* fd */, int /* binary */); |
3db2e6dd DD |
134 | /* Get a FILE pointer to write to the file descriptor FD (only |
135 | called if PEX_USE_PIPES is set). If BINARY is non-zero, open in | |
136 | binary mode. Arrange for FD not to be inherited by the child | |
137 | processes. Return pointer on success, NULL on error. */ | |
138 | FILE * (*fdopenw) (struct pex_obj *, int /* fd */, int /* binary */); | |
b109e79a ILT |
139 | /* Free any system dependent data associated with OBJ. May be |
140 | NULL if there is nothing to do. */ | |
141 | void (*cleanup) (struct pex_obj *); | |
142 | }; | |
143 | ||
144 | extern struct pex_obj *pex_init_common (int, const char *, const char *, | |
145 | const struct pex_funcs *); | |
146 | ||
5a17353c | 147 | #endif |