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. | |
e495212d | 3 | Copyright (C) 1996-2017 Free Software Foundation, Inc. |
5a17353c DD |
4 | |
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. | |
10 | ||
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. | |
15 | ||
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, | |
979c05d3 NC |
18 | write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, |
19 | Boston, MA 02110-1301, USA. */ | |
5a17353c DD |
20 | |
21 | #ifndef PEX_COMMON_H | |
22 | #define PEX_COMMON_H | |
23 | ||
24 | #include "config.h" | |
25 | #include "libiberty.h" | |
b109e79a | 26 | #include <stdio.h> |
5a17353c | 27 | |
29d89e07 DD |
28 | /* pid_t is may defined by config.h or sys/types.h needs to be |
29 | included. */ | |
30 | #if !defined(pid_t) && defined(HAVE_SYS_TYPES_H) | |
31 | #include <sys/types.h> | |
32 | #endif | |
33 | ||
5a17353c DD |
34 | #define install_error_msg "installation problem, cannot exec `%s'" |
35 | ||
36 | /* stdin file number. */ | |
37 | #define STDIN_FILE_NO 0 | |
38 | ||
39 | /* stdout file number. */ | |
40 | #define STDOUT_FILE_NO 1 | |
41 | ||
12a7367e DD |
42 | /* stderr file number. */ |
43 | #define STDERR_FILE_NO 2 | |
44 | ||
5a17353c DD |
45 | /* value of `pipe': port index for reading. */ |
46 | #define READ_PORT 0 | |
47 | ||
48 | /* value of `pipe': port index for writing. */ | |
49 | #define WRITE_PORT 1 | |
50 | ||
b109e79a ILT |
51 | /* The structure used by pex_init and friends. */ |
52 | ||
53 | struct pex_obj | |
54 | { | |
55 | /* Flags. */ | |
56 | int flags; | |
57 | /* Name of calling program, for error messages. */ | |
58 | const char *pname; | |
59 | /* Base name to use for temporary files. */ | |
60 | const char *tempbase; | |
61 | /* Pipe to use as stdin for next process. */ | |
62 | int next_input; | |
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; | |
53d7966f VP |
67 | /* If not -1, stderr pipe from the last process. */ |
68 | int stderr_pipe; | |
b109e79a ILT |
69 | /* Number of child processes. */ |
70 | int count; | |
9223c945 | 71 | /* PIDs of child processes; array allocated using malloc. */ |
587c6b96 | 72 | pid_t *children; |
b109e79a ILT |
73 | /* Exit statuses of child processes; array allocated using malloc. */ |
74 | int *status; | |
75 | /* Time used by child processes; array allocated using malloc. */ | |
76 | struct pex_time *time; | |
77 | /* Number of children we have already waited for. */ | |
78 | int number_waited; | |
3db2e6dd DD |
79 | /* FILE created by pex_input_file. */ |
80 | FILE *input_file; | |
b109e79a ILT |
81 | /* FILE created by pex_read_output. */ |
82 | FILE *read_output; | |
53d7966f VP |
83 | /* FILE created by pex_read_err. */ |
84 | FILE *read_err; | |
b109e79a ILT |
85 | /* Number of temporary files to remove. */ |
86 | int remove_count; | |
87 | /* List of temporary files to remove; array allocated using malloc | |
88 | of strings allocated using malloc. */ | |
89 | char **remove; | |
90 | /* Pointers to system dependent functions. */ | |
91 | const struct pex_funcs *funcs; | |
92 | /* For use by system dependent code. */ | |
93 | void *sysdep; | |
94 | }; | |
95 | ||
96 | /* Functions passed to pex_run_common. */ | |
97 | ||
98 | struct pex_funcs | |
99 | { | |
100 | /* Open file NAME for reading. If BINARY is non-zero, open in | |
101 | binary mode. Return >= 0 on success, -1 on error. */ | |
9223c945 | 102 | int (*open_read) (struct pex_obj *, const char */* name */, int /* binary */); |
b109e79a ILT |
103 | /* Open file NAME for writing. If BINARY is non-zero, open in |
104 | binary mode. Return >= 0 on success, -1 on error. */ | |
9223c945 | 105 | int (*open_write) (struct pex_obj *, const char */* name */, |
b55f9678 | 106 | int /* binary */, int /* append */); |
b109e79a | 107 | /* Execute a child process. FLAGS, EXECUTABLE, ARGV, ERR are from |
cb6c09ac DD |
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 | |
b109e79a ILT |
114 | PEX_STDERR_TO_STDOUT flag. Return >= 0 on success, or -1 on |
115 | error and set *ERRMSG and *ERR. */ | |
29d89e07 | 116 | pid_t (*exec_child) (struct pex_obj *, int /* flags */, |
9223c945 | 117 | const char */* executable */, char * const * /* argv */, |
014a8caf | 118 | char * const * /* env */, |
9223c945 | 119 | int /* in */, int /* out */, int /* errdes */, |
cb6c09ac DD |
120 | int /* toclose */, const char **/* errmsg */, |
121 | int */* err */); | |
b109e79a ILT |
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. */ | |
587c6b96 | 128 | pid_t (*wait) (struct pex_obj *, pid_t /* child */, int * /* status */, |
9223c945 DD |
129 | struct pex_time * /* time */, int /* done */, |
130 | const char ** /* errmsg */, int * /* err */); | |
b109e79a | 131 | /* Create a pipe (only called if PEX_USE_PIPES is set) storing two |
9223c945 DD |
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 */); | |
b109e79a ILT |
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. */ | |
9223c945 | 138 | FILE * (*fdopenr) (struct pex_obj *, int /* fd */, int /* binary */); |
3db2e6dd DD |
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 */); | |
b109e79a ILT |
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 *); | |
147 | }; | |
148 | ||
149 | extern struct pex_obj *pex_init_common (int, const char *, const char *, | |
150 | const struct pex_funcs *); | |
151 | ||
5a17353c | 152 | #endif |