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. DJGPP specialization. | |
b109e79a | 3 | Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2005 |
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 | #include "pex-common.h" | |
23 | ||
24 | #include <stdio.h> | |
25 | #include <errno.h> | |
26 | #ifdef NEED_DECLARATION_ERRNO | |
27 | extern int errno; | |
28 | #endif | |
29 | #ifdef HAVE_STDLIB_H | |
30 | #include <stdlib.h> | |
31 | #endif | |
282d9ec3 ILT |
32 | #include <string.h> |
33 | #include <fcntl.h> | |
34 | #include <unistd.h> | |
35 | #include <sys/stat.h> | |
5a17353c DD |
36 | #include <process.h> |
37 | ||
38 | /* Use ECHILD if available, otherwise use EINVAL. */ | |
39 | #ifdef ECHILD | |
40 | #define PWAIT_ERROR ECHILD | |
41 | #else | |
42 | #define PWAIT_ERROR EINVAL | |
43 | #endif | |
44 | ||
b109e79a | 45 | static int pex_djgpp_open_read (struct pex_obj *, const char *, int); |
b55f9678 | 46 | static int pex_djgpp_open_write (struct pex_obj *, const char *, int, int); |
29d89e07 | 47 | static pid_t pex_djgpp_exec_child (struct pex_obj *, int, const char *, |
014a8caf | 48 | char * const *, char * const *, |
cb6c09ac | 49 | int, int, int, int, |
b109e79a ILT |
50 | const char **, int *); |
51 | static int pex_djgpp_close (struct pex_obj *, int); | |
587c6b96 | 52 | static pid_t pex_djgpp_wait (struct pex_obj *, pid_t, int *, struct pex_time *, |
b109e79a | 53 | int, const char **, int *); |
5a17353c | 54 | |
b109e79a | 55 | /* The list of functions we pass to the common routines. */ |
5a17353c | 56 | |
b109e79a | 57 | const struct pex_funcs funcs = |
5a17353c | 58 | { |
b109e79a ILT |
59 | pex_djgpp_open_read, |
60 | pex_djgpp_open_write, | |
61 | pex_djgpp_exec_child, | |
62 | pex_djgpp_close, | |
63 | pex_djgpp_wait, | |
64 | NULL, /* pipe */ | |
65 | NULL, /* fdopenr */ | |
3db2e6dd | 66 | NULL, /* fdopenw */ |
b109e79a ILT |
67 | NULL /* cleanup */ |
68 | }; | |
5a17353c | 69 | |
b109e79a | 70 | /* Return a newly initialized pex_obj structure. */ |
5a17353c | 71 | |
b109e79a ILT |
72 | struct pex_obj * |
73 | pex_init (int flags, const char *pname, const char *tempbase) | |
74 | { | |
75 | /* DJGPP does not support pipes. */ | |
76 | flags &= ~ PEX_USE_PIPES; | |
282d9ec3 | 77 | return pex_init_common (flags, pname, tempbase, &funcs); |
b109e79a | 78 | } |
5a17353c | 79 | |
b109e79a | 80 | /* Open a file for reading. */ |
5a17353c | 81 | |
b109e79a ILT |
82 | static int |
83 | pex_djgpp_open_read (struct pex_obj *obj ATTRIBUTE_UNUSED, | |
84 | const char *name, int binary) | |
85 | { | |
86 | return open (name, O_RDONLY | (binary ? O_BINARY : O_TEXT)); | |
87 | } | |
88 | ||
89 | /* Open a file for writing. */ | |
90 | ||
91 | static int | |
92 | pex_djgpp_open_write (struct pex_obj *obj ATTRIBUTE_UNUSED, | |
b55f9678 | 93 | const char *name, int binary, int append) |
b109e79a ILT |
94 | { |
95 | /* Note that we can't use O_EXCL here because gcc may have already | |
96 | created the temporary file via make_temp_file. */ | |
b55f9678 IB |
97 | if (append) |
98 | return -1; | |
b109e79a ILT |
99 | return open (name, |
100 | (O_WRONLY | O_CREAT | O_TRUNC | |
101 | | (binary ? O_BINARY : O_TEXT)), | |
102 | S_IRUSR | S_IWUSR); | |
103 | } | |
104 | ||
105 | /* Close a file. */ | |
5a17353c | 106 | |
b109e79a ILT |
107 | static int |
108 | pex_djgpp_close (struct pex_obj *obj ATTRIBUTE_UNUSED, int fd) | |
109 | { | |
110 | return close (fd); | |
5a17353c DD |
111 | } |
112 | ||
b109e79a ILT |
113 | /* Execute a child. */ |
114 | ||
29d89e07 | 115 | static pid_t |
b109e79a | 116 | pex_djgpp_exec_child (struct pex_obj *obj, int flags, const char *executable, |
014a8caf DD |
117 | char * const * argv, char * const * env, |
118 | int in, int out, int errdes, | |
cb6c09ac DD |
119 | int toclose ATTRIBUTE_UNUSED, const char **errmsg, |
120 | int *err) | |
5a17353c | 121 | { |
b109e79a ILT |
122 | int org_in, org_out, org_errdes; |
123 | int status; | |
124 | int *statuses; | |
125 | ||
126 | org_in = -1; | |
127 | org_out = -1; | |
128 | org_errdes = -1; | |
129 | ||
130 | if (in != STDIN_FILE_NO) | |
131 | { | |
282d9ec3 | 132 | org_in = dup (STDIN_FILE_NO); |
b109e79a ILT |
133 | if (org_in < 0) |
134 | { | |
135 | *err = errno; | |
282d9ec3 | 136 | *errmsg = "dup"; |
29d89e07 | 137 | return (pid_t) -1; |
b109e79a | 138 | } |
282d9ec3 | 139 | if (dup2 (in, STDIN_FILE_NO) < 0) |
b109e79a ILT |
140 | { |
141 | *err = errno; | |
282d9ec3 | 142 | *errmsg = "dup2"; |
29d89e07 | 143 | return (pid_t) -1; |
b109e79a | 144 | } |
282d9ec3 | 145 | if (close (in) < 0) |
b109e79a ILT |
146 | { |
147 | *err = errno; | |
282d9ec3 | 148 | *errmsg = "close"; |
29d89e07 | 149 | return (pid_t) -1; |
b109e79a ILT |
150 | } |
151 | } | |
152 | ||
153 | if (out != STDOUT_FILE_NO) | |
154 | { | |
282d9ec3 | 155 | org_out = dup (STDOUT_FILE_NO); |
b109e79a ILT |
156 | if (org_out < 0) |
157 | { | |
158 | *err = errno; | |
282d9ec3 | 159 | *errmsg = "dup"; |
29d89e07 | 160 | return (pid_t) -1; |
b109e79a | 161 | } |
282d9ec3 | 162 | if (dup2 (out, STDOUT_FILE_NO) < 0) |
b109e79a ILT |
163 | { |
164 | *err = errno; | |
282d9ec3 | 165 | *errmsg = "dup2"; |
29d89e07 | 166 | return (pid_t) -1; |
b109e79a | 167 | } |
282d9ec3 | 168 | if (close (out) < 0) |
b109e79a ILT |
169 | { |
170 | *err = errno; | |
282d9ec3 | 171 | *errmsg = "close"; |
29d89e07 | 172 | return (pid_t) -1; |
b109e79a ILT |
173 | } |
174 | } | |
175 | ||
176 | if (errdes != STDERR_FILE_NO | |
177 | || (flags & PEX_STDERR_TO_STDOUT) != 0) | |
178 | { | |
282d9ec3 | 179 | org_errdes = dup (STDERR_FILE_NO); |
b109e79a ILT |
180 | if (org_errdes < 0) |
181 | { | |
182 | *err = errno; | |
282d9ec3 | 183 | *errmsg = "dup"; |
29d89e07 | 184 | return (pid_t) -1; |
b109e79a | 185 | } |
282d9ec3 | 186 | if (dup2 ((flags & PEX_STDERR_TO_STDOUT) != 0 ? STDOUT_FILE_NO : errdes, |
b109e79a ILT |
187 | STDERR_FILE_NO) < 0) |
188 | { | |
189 | *err = errno; | |
282d9ec3 | 190 | *errmsg = "dup2"; |
29d89e07 | 191 | return (pid_t) -1; |
b109e79a ILT |
192 | } |
193 | if (errdes != STDERR_FILE_NO) | |
194 | { | |
282d9ec3 | 195 | if (close (errdes) < 0) |
b109e79a ILT |
196 | { |
197 | *err = errno; | |
282d9ec3 | 198 | *errmsg = "close"; |
29d89e07 | 199 | return (pid_t) -1; |
b109e79a ILT |
200 | } |
201 | } | |
202 | } | |
203 | ||
014a8caf DD |
204 | if (env) |
205 | status = (((flags & PEX_SEARCH) != 0 ? spawnvpe : spawnve) | |
206 | (P_WAIT, executable, argv, env)); | |
207 | else | |
208 | status = (((flags & PEX_SEARCH) != 0 ? spawnvp : spawnv) | |
209 | (P_WAIT, executable, argv)); | |
b109e79a ILT |
210 | |
211 | if (status == -1) | |
212 | { | |
213 | *err = errno; | |
282d9ec3 | 214 | *errmsg = ((flags & PEX_SEARCH) != 0) ? "spawnvp" : "spawnv"; |
b109e79a ILT |
215 | } |
216 | ||
217 | if (in != STDIN_FILE_NO) | |
218 | { | |
282d9ec3 | 219 | if (dup2 (org_in, STDIN_FILE_NO) < 0) |
b109e79a ILT |
220 | { |
221 | *err = errno; | |
282d9ec3 | 222 | *errmsg = "dup2"; |
29d89e07 | 223 | return (pid_t) -1; |
b109e79a | 224 | } |
282d9ec3 | 225 | if (close (org_in) < 0) |
b109e79a ILT |
226 | { |
227 | *err = errno; | |
282d9ec3 | 228 | *errmsg = "close"; |
29d89e07 | 229 | return (pid_t) -1; |
b109e79a ILT |
230 | } |
231 | } | |
232 | ||
233 | if (out != STDOUT_FILE_NO) | |
234 | { | |
282d9ec3 | 235 | if (dup2 (org_out, STDOUT_FILE_NO) < 0) |
b109e79a ILT |
236 | { |
237 | *err = errno; | |
282d9ec3 | 238 | *errmsg = "dup2"; |
29d89e07 | 239 | return (pid_t) -1; |
b109e79a | 240 | } |
282d9ec3 | 241 | if (close (org_out) < 0) |
b109e79a ILT |
242 | { |
243 | *err = errno; | |
282d9ec3 | 244 | *errmsg = "close"; |
29d89e07 | 245 | return (pid_t) -1; |
b109e79a ILT |
246 | } |
247 | } | |
248 | ||
249 | if (errdes != STDERR_FILE_NO | |
250 | || (flags & PEX_STDERR_TO_STDOUT) != 0) | |
5a17353c | 251 | { |
282d9ec3 | 252 | if (dup2 (org_errdes, STDERR_FILE_NO) < 0) |
b109e79a ILT |
253 | { |
254 | *err = errno; | |
282d9ec3 | 255 | *errmsg = "dup2"; |
29d89e07 | 256 | return (pid_t) -1; |
b109e79a | 257 | } |
282d9ec3 | 258 | if (close (org_errdes) < 0) |
b109e79a ILT |
259 | { |
260 | *err = errno; | |
282d9ec3 | 261 | *errmsg = "close"; |
29d89e07 | 262 | return (pid_t) -1; |
b109e79a | 263 | } |
5a17353c | 264 | } |
b109e79a ILT |
265 | |
266 | /* Save the exit status for later. When we are called, obj->count | |
267 | is the number of children which have executed before this | |
268 | one. */ | |
269 | statuses = (int *) obj->sysdep; | |
abf6a75b | 270 | statuses = XRESIZEVEC (int, statuses, obj->count + 1); |
b109e79a ILT |
271 | statuses[obj->count] = status; |
272 | obj->sysdep = (void *) statuses; | |
273 | ||
29d89e07 | 274 | return (pid_t) obj->count; |
b109e79a ILT |
275 | } |
276 | ||
277 | /* Wait for a child process to complete. Actually the child process | |
278 | has already completed, and we just need to return the exit | |
279 | status. */ | |
280 | ||
587c6b96 | 281 | static pid_t |
29d89e07 | 282 | pex_djgpp_wait (struct pex_obj *obj, pid_t pid, int *status, |
282d9ec3 ILT |
283 | struct pex_time *time, int done ATTRIBUTE_UNUSED, |
284 | const char **errmsg ATTRIBUTE_UNUSED, | |
285 | int *err ATTRIBUTE_UNUSED) | |
b109e79a ILT |
286 | { |
287 | int *statuses; | |
288 | ||
289 | if (time != NULL) | |
290 | memset (time, 0, sizeof *time); | |
291 | ||
292 | statuses = (int *) obj->sysdep; | |
293 | *status = statuses[pid]; | |
294 | ||
295 | return 0; | |
5a17353c | 296 | } |