pex-unix.c revision 1.1.1.8 1 1.1 mrg /* Utilities to execute a program in a subprocess (possibly linked by pipes
2 1.1 mrg with other subprocesses), and wait for it. Generic Unix version
3 1.1 mrg (also used for UWIN and VMS).
4 1.1.1.8 mrg Copyright (C) 1996-2020 Free Software Foundation, Inc.
5 1.1 mrg
6 1.1 mrg This file is part of the libiberty library.
7 1.1 mrg Libiberty is free software; you can redistribute it and/or
8 1.1 mrg modify it under the terms of the GNU Library General Public
9 1.1 mrg License as published by the Free Software Foundation; either
10 1.1 mrg version 2 of the License, or (at your option) any later version.
11 1.1 mrg
12 1.1 mrg Libiberty is distributed in the hope that it will be useful,
13 1.1 mrg but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 mrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 1.1 mrg Library General Public License for more details.
16 1.1 mrg
17 1.1 mrg You should have received a copy of the GNU Library General Public
18 1.1 mrg License along with libiberty; see the file COPYING.LIB. If not,
19 1.1 mrg write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 1.1 mrg Boston, MA 02110-1301, USA. */
21 1.1 mrg
22 1.1 mrg #include "config.h"
23 1.1 mrg #include "libiberty.h"
24 1.1 mrg #include "pex-common.h"
25 1.1.1.4 mrg #include "environ.h"
26 1.1 mrg
27 1.1 mrg #include <stdio.h>
28 1.1 mrg #include <signal.h>
29 1.1 mrg #include <errno.h>
30 1.1 mrg #ifdef NEED_DECLARATION_ERRNO
31 1.1 mrg extern int errno;
32 1.1 mrg #endif
33 1.1 mrg #ifdef HAVE_STDLIB_H
34 1.1 mrg #include <stdlib.h>
35 1.1 mrg #endif
36 1.1 mrg #ifdef HAVE_STRING_H
37 1.1 mrg #include <string.h>
38 1.1 mrg #endif
39 1.1 mrg #ifdef HAVE_UNISTD_H
40 1.1 mrg #include <unistd.h>
41 1.1 mrg #endif
42 1.1 mrg
43 1.1 mrg #include <sys/types.h>
44 1.1 mrg
45 1.1 mrg #ifdef HAVE_FCNTL_H
46 1.1 mrg #include <fcntl.h>
47 1.1 mrg #endif
48 1.1 mrg #ifdef HAVE_SYS_WAIT_H
49 1.1 mrg #include <sys/wait.h>
50 1.1 mrg #endif
51 1.1 mrg #ifdef HAVE_GETRUSAGE
52 1.1 mrg #include <sys/time.h>
53 1.1 mrg #include <sys/resource.h>
54 1.1 mrg #endif
55 1.1 mrg #ifdef HAVE_SYS_STAT_H
56 1.1 mrg #include <sys/stat.h>
57 1.1 mrg #endif
58 1.1.1.2 mrg #ifdef HAVE_PROCESS_H
59 1.1.1.2 mrg #include <process.h>
60 1.1.1.2 mrg #endif
61 1.1 mrg
62 1.1 mrg #ifdef vfork /* Autoconf may define this to fork for us. */
63 1.1 mrg # define VFORK_STRING "fork"
64 1.1 mrg #else
65 1.1 mrg # define VFORK_STRING "vfork"
66 1.1 mrg #endif
67 1.1 mrg #ifdef HAVE_VFORK_H
68 1.1 mrg #include <vfork.h>
69 1.1 mrg #endif
70 1.1 mrg #if defined(VMS) && defined (__LONG_POINTERS)
71 1.1 mrg #ifndef __CHAR_PTR32
72 1.1 mrg typedef char * __char_ptr32
73 1.1 mrg __attribute__ ((mode (SI)));
74 1.1 mrg #endif
75 1.1 mrg
76 1.1 mrg typedef __char_ptr32 *__char_ptr_char_ptr32
77 1.1 mrg __attribute__ ((mode (SI)));
78 1.1 mrg
79 1.1 mrg /* Return a 32 bit pointer to an array of 32 bit pointers
80 1.1 mrg given a 64 bit pointer to an array of 64 bit pointers. */
81 1.1 mrg
82 1.1 mrg static __char_ptr_char_ptr32
83 1.1 mrg to_ptr32 (char **ptr64)
84 1.1 mrg {
85 1.1 mrg int argc;
86 1.1 mrg __char_ptr_char_ptr32 short_argv;
87 1.1 mrg
88 1.1.1.2 mrg /* Count number of arguments. */
89 1.1.1.2 mrg for (argc = 0; ptr64[argc] != NULL; argc++)
90 1.1.1.2 mrg ;
91 1.1 mrg
92 1.1 mrg /* Reallocate argv with 32 bit pointers. */
93 1.1 mrg short_argv = (__char_ptr_char_ptr32) decc$malloc
94 1.1 mrg (sizeof (__char_ptr32) * (argc + 1));
95 1.1 mrg
96 1.1.1.2 mrg for (argc = 0; ptr64[argc] != NULL; argc++)
97 1.1 mrg short_argv[argc] = (__char_ptr32) decc$strdup (ptr64[argc]);
98 1.1 mrg
99 1.1 mrg short_argv[argc] = (__char_ptr32) 0;
100 1.1 mrg return short_argv;
101 1.1 mrg
102 1.1 mrg }
103 1.1 mrg #else
104 1.1 mrg #define to_ptr32(argv) argv
105 1.1 mrg #endif
106 1.1 mrg
107 1.1 mrg /* File mode to use for private and world-readable files. */
108 1.1 mrg
109 1.1 mrg #if defined (S_IRUSR) && defined (S_IWUSR) && defined (S_IRGRP) && defined (S_IWGRP) && defined (S_IROTH) && defined (S_IWOTH)
110 1.1 mrg #define PUBLIC_MODE \
111 1.1 mrg (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
112 1.1 mrg #else
113 1.1 mrg #define PUBLIC_MODE 0666
114 1.1 mrg #endif
115 1.1 mrg
116 1.1 mrg /* Get the exit status of a particular process, and optionally get the
117 1.1 mrg time that it took. This is simple if we have wait4, slightly
118 1.1 mrg harder if we have waitpid, and is a pain if we only have wait. */
119 1.1 mrg
120 1.1 mrg static pid_t pex_wait (struct pex_obj *, pid_t, int *, struct pex_time *);
121 1.1 mrg
122 1.1 mrg #ifdef HAVE_WAIT4
123 1.1 mrg
124 1.1 mrg static pid_t
125 1.1 mrg pex_wait (struct pex_obj *obj ATTRIBUTE_UNUSED, pid_t pid, int *status,
126 1.1 mrg struct pex_time *time)
127 1.1 mrg {
128 1.1 mrg pid_t ret;
129 1.1 mrg struct rusage r;
130 1.1 mrg
131 1.1 mrg #ifdef HAVE_WAITPID
132 1.1 mrg if (time == NULL)
133 1.1 mrg return waitpid (pid, status, 0);
134 1.1 mrg #endif
135 1.1 mrg
136 1.1 mrg ret = wait4 (pid, status, 0, &r);
137 1.1 mrg
138 1.1 mrg if (time != NULL)
139 1.1 mrg {
140 1.1 mrg time->user_seconds = r.ru_utime.tv_sec;
141 1.1 mrg time->user_microseconds= r.ru_utime.tv_usec;
142 1.1 mrg time->system_seconds = r.ru_stime.tv_sec;
143 1.1 mrg time->system_microseconds= r.ru_stime.tv_usec;
144 1.1 mrg }
145 1.1 mrg
146 1.1 mrg return ret;
147 1.1 mrg }
148 1.1 mrg
149 1.1 mrg #else /* ! defined (HAVE_WAIT4) */
150 1.1 mrg
151 1.1 mrg #ifdef HAVE_WAITPID
152 1.1 mrg
153 1.1 mrg #ifndef HAVE_GETRUSAGE
154 1.1 mrg
155 1.1 mrg static pid_t
156 1.1 mrg pex_wait (struct pex_obj *obj ATTRIBUTE_UNUSED, pid_t pid, int *status,
157 1.1 mrg struct pex_time *time)
158 1.1 mrg {
159 1.1 mrg if (time != NULL)
160 1.1 mrg memset (time, 0, sizeof (struct pex_time));
161 1.1 mrg return waitpid (pid, status, 0);
162 1.1 mrg }
163 1.1 mrg
164 1.1 mrg #else /* defined (HAVE_GETRUSAGE) */
165 1.1 mrg
166 1.1 mrg static pid_t
167 1.1 mrg pex_wait (struct pex_obj *obj ATTRIBUTE_UNUSED, pid_t pid, int *status,
168 1.1 mrg struct pex_time *time)
169 1.1 mrg {
170 1.1 mrg struct rusage r1, r2;
171 1.1 mrg pid_t ret;
172 1.1 mrg
173 1.1 mrg if (time == NULL)
174 1.1 mrg return waitpid (pid, status, 0);
175 1.1 mrg
176 1.1 mrg getrusage (RUSAGE_CHILDREN, &r1);
177 1.1 mrg
178 1.1 mrg ret = waitpid (pid, status, 0);
179 1.1 mrg if (ret < 0)
180 1.1 mrg return ret;
181 1.1 mrg
182 1.1 mrg getrusage (RUSAGE_CHILDREN, &r2);
183 1.1 mrg
184 1.1 mrg time->user_seconds = r2.ru_utime.tv_sec - r1.ru_utime.tv_sec;
185 1.1 mrg time->user_microseconds = r2.ru_utime.tv_usec - r1.ru_utime.tv_usec;
186 1.1 mrg if (r2.ru_utime.tv_usec < r1.ru_utime.tv_usec)
187 1.1 mrg {
188 1.1 mrg --time->user_seconds;
189 1.1 mrg time->user_microseconds += 1000000;
190 1.1 mrg }
191 1.1 mrg
192 1.1 mrg time->system_seconds = r2.ru_stime.tv_sec - r1.ru_stime.tv_sec;
193 1.1 mrg time->system_microseconds = r2.ru_stime.tv_usec - r1.ru_stime.tv_usec;
194 1.1 mrg if (r2.ru_stime.tv_usec < r1.ru_stime.tv_usec)
195 1.1 mrg {
196 1.1 mrg --time->system_seconds;
197 1.1 mrg time->system_microseconds += 1000000;
198 1.1 mrg }
199 1.1 mrg
200 1.1 mrg return ret;
201 1.1 mrg }
202 1.1 mrg
203 1.1 mrg #endif /* defined (HAVE_GETRUSAGE) */
204 1.1 mrg
205 1.1 mrg #else /* ! defined (HAVE_WAITPID) */
206 1.1 mrg
207 1.1 mrg struct status_list
208 1.1 mrg {
209 1.1 mrg struct status_list *next;
210 1.1 mrg pid_t pid;
211 1.1 mrg int status;
212 1.1 mrg struct pex_time time;
213 1.1 mrg };
214 1.1 mrg
215 1.1 mrg static pid_t
216 1.1 mrg pex_wait (struct pex_obj *obj, pid_t pid, int *status, struct pex_time *time)
217 1.1 mrg {
218 1.1 mrg struct status_list **pp;
219 1.1 mrg
220 1.1 mrg for (pp = (struct status_list **) &obj->sysdep;
221 1.1 mrg *pp != NULL;
222 1.1 mrg pp = &(*pp)->next)
223 1.1 mrg {
224 1.1 mrg if ((*pp)->pid == pid)
225 1.1 mrg {
226 1.1 mrg struct status_list *p;
227 1.1 mrg
228 1.1 mrg p = *pp;
229 1.1 mrg *status = p->status;
230 1.1 mrg if (time != NULL)
231 1.1 mrg *time = p->time;
232 1.1 mrg *pp = p->next;
233 1.1 mrg free (p);
234 1.1 mrg return pid;
235 1.1 mrg }
236 1.1 mrg }
237 1.1 mrg
238 1.1 mrg while (1)
239 1.1 mrg {
240 1.1 mrg pid_t cpid;
241 1.1 mrg struct status_list *psl;
242 1.1 mrg struct pex_time pt;
243 1.1 mrg #ifdef HAVE_GETRUSAGE
244 1.1 mrg struct rusage r1, r2;
245 1.1 mrg #endif
246 1.1 mrg
247 1.1 mrg if (time != NULL)
248 1.1 mrg {
249 1.1 mrg #ifdef HAVE_GETRUSAGE
250 1.1 mrg getrusage (RUSAGE_CHILDREN, &r1);
251 1.1 mrg #else
252 1.1 mrg memset (&pt, 0, sizeof (struct pex_time));
253 1.1 mrg #endif
254 1.1 mrg }
255 1.1 mrg
256 1.1 mrg cpid = wait (status);
257 1.1 mrg
258 1.1 mrg #ifdef HAVE_GETRUSAGE
259 1.1 mrg if (time != NULL && cpid >= 0)
260 1.1 mrg {
261 1.1 mrg getrusage (RUSAGE_CHILDREN, &r2);
262 1.1 mrg
263 1.1 mrg pt.user_seconds = r2.ru_utime.tv_sec - r1.ru_utime.tv_sec;
264 1.1 mrg pt.user_microseconds = r2.ru_utime.tv_usec - r1.ru_utime.tv_usec;
265 1.1 mrg if (pt.user_microseconds < 0)
266 1.1 mrg {
267 1.1 mrg --pt.user_seconds;
268 1.1 mrg pt.user_microseconds += 1000000;
269 1.1 mrg }
270 1.1 mrg
271 1.1 mrg pt.system_seconds = r2.ru_stime.tv_sec - r1.ru_stime.tv_sec;
272 1.1 mrg pt.system_microseconds = r2.ru_stime.tv_usec - r1.ru_stime.tv_usec;
273 1.1 mrg if (pt.system_microseconds < 0)
274 1.1 mrg {
275 1.1 mrg --pt.system_seconds;
276 1.1 mrg pt.system_microseconds += 1000000;
277 1.1 mrg }
278 1.1 mrg }
279 1.1 mrg #endif
280 1.1 mrg
281 1.1 mrg if (cpid < 0 || cpid == pid)
282 1.1 mrg {
283 1.1 mrg if (time != NULL)
284 1.1 mrg *time = pt;
285 1.1 mrg return cpid;
286 1.1 mrg }
287 1.1 mrg
288 1.1 mrg psl = XNEW (struct status_list);
289 1.1 mrg psl->pid = cpid;
290 1.1 mrg psl->status = *status;
291 1.1 mrg if (time != NULL)
292 1.1 mrg psl->time = pt;
293 1.1 mrg psl->next = (struct status_list *) obj->sysdep;
294 1.1 mrg obj->sysdep = (void *) psl;
295 1.1 mrg }
296 1.1 mrg }
297 1.1 mrg
298 1.1 mrg #endif /* ! defined (HAVE_WAITPID) */
299 1.1 mrg #endif /* ! defined (HAVE_WAIT4) */
300 1.1 mrg
301 1.1 mrg static int pex_unix_open_read (struct pex_obj *, const char *, int);
302 1.1.1.3 mrg static int pex_unix_open_write (struct pex_obj *, const char *, int, int);
303 1.1 mrg static pid_t pex_unix_exec_child (struct pex_obj *, int, const char *,
304 1.1 mrg char * const *, char * const *,
305 1.1 mrg int, int, int, int,
306 1.1 mrg const char **, int *);
307 1.1 mrg static int pex_unix_close (struct pex_obj *, int);
308 1.1 mrg static int pex_unix_wait (struct pex_obj *, pid_t, int *, struct pex_time *,
309 1.1 mrg int, const char **, int *);
310 1.1 mrg static int pex_unix_pipe (struct pex_obj *, int *, int);
311 1.1 mrg static FILE *pex_unix_fdopenr (struct pex_obj *, int, int);
312 1.1 mrg static FILE *pex_unix_fdopenw (struct pex_obj *, int, int);
313 1.1 mrg static void pex_unix_cleanup (struct pex_obj *);
314 1.1 mrg
315 1.1 mrg /* The list of functions we pass to the common routines. */
316 1.1 mrg
317 1.1 mrg const struct pex_funcs funcs =
318 1.1 mrg {
319 1.1 mrg pex_unix_open_read,
320 1.1 mrg pex_unix_open_write,
321 1.1 mrg pex_unix_exec_child,
322 1.1 mrg pex_unix_close,
323 1.1 mrg pex_unix_wait,
324 1.1 mrg pex_unix_pipe,
325 1.1 mrg pex_unix_fdopenr,
326 1.1 mrg pex_unix_fdopenw,
327 1.1 mrg pex_unix_cleanup
328 1.1 mrg };
329 1.1 mrg
330 1.1 mrg /* Return a newly initialized pex_obj structure. */
331 1.1 mrg
332 1.1 mrg struct pex_obj *
333 1.1 mrg pex_init (int flags, const char *pname, const char *tempbase)
334 1.1 mrg {
335 1.1 mrg return pex_init_common (flags, pname, tempbase, &funcs);
336 1.1 mrg }
337 1.1 mrg
338 1.1 mrg /* Open a file for reading. */
339 1.1 mrg
340 1.1 mrg static int
341 1.1 mrg pex_unix_open_read (struct pex_obj *obj ATTRIBUTE_UNUSED, const char *name,
342 1.1 mrg int binary ATTRIBUTE_UNUSED)
343 1.1 mrg {
344 1.1 mrg return open (name, O_RDONLY);
345 1.1 mrg }
346 1.1 mrg
347 1.1 mrg /* Open a file for writing. */
348 1.1 mrg
349 1.1 mrg static int
350 1.1 mrg pex_unix_open_write (struct pex_obj *obj ATTRIBUTE_UNUSED, const char *name,
351 1.1.1.3 mrg int binary ATTRIBUTE_UNUSED, int append)
352 1.1 mrg {
353 1.1 mrg /* Note that we can't use O_EXCL here because gcc may have already
354 1.1 mrg created the temporary file via make_temp_file. */
355 1.1.1.3 mrg return open (name, O_WRONLY | O_CREAT
356 1.1.1.3 mrg | (append ? O_APPEND : O_TRUNC), PUBLIC_MODE);
357 1.1 mrg }
358 1.1 mrg
359 1.1 mrg /* Close a file. */
360 1.1 mrg
361 1.1 mrg static int
362 1.1 mrg pex_unix_close (struct pex_obj *obj ATTRIBUTE_UNUSED, int fd)
363 1.1 mrg {
364 1.1 mrg return close (fd);
365 1.1 mrg }
366 1.1 mrg
367 1.1 mrg /* Execute a child. */
368 1.1 mrg
369 1.1.1.2 mrg #if defined(HAVE_SPAWNVE) && defined(HAVE_SPAWNVPE)
370 1.1.1.2 mrg /* Implementation of pex->exec_child using the Cygwin spawn operation. */
371 1.1.1.2 mrg
372 1.1.1.2 mrg /* Subroutine of pex_unix_exec_child. Move OLD_FD to a new file descriptor
373 1.1.1.2 mrg to be stored in *PNEW_FD, save the flags in *PFLAGS, and arrange for the
374 1.1.1.2 mrg saved copy to be close-on-exec. Move CHILD_FD into OLD_FD. If CHILD_FD
375 1.1.1.2 mrg is -1, OLD_FD is to be closed. Return -1 on error. */
376 1.1.1.2 mrg
377 1.1.1.2 mrg static int
378 1.1.1.2 mrg save_and_install_fd(int *pnew_fd, int *pflags, int old_fd, int child_fd)
379 1.1.1.2 mrg {
380 1.1.1.2 mrg int new_fd, flags;
381 1.1.1.2 mrg
382 1.1.1.2 mrg flags = fcntl (old_fd, F_GETFD);
383 1.1.1.2 mrg
384 1.1.1.2 mrg /* If we could not retrieve the flags, then OLD_FD was not open. */
385 1.1.1.2 mrg if (flags < 0)
386 1.1.1.2 mrg {
387 1.1.1.2 mrg new_fd = -1, flags = 0;
388 1.1.1.2 mrg if (child_fd >= 0 && dup2 (child_fd, old_fd) < 0)
389 1.1.1.2 mrg return -1;
390 1.1.1.2 mrg }
391 1.1.1.2 mrg /* If we wish to close OLD_FD, just mark it CLOEXEC. */
392 1.1.1.2 mrg else if (child_fd == -1)
393 1.1.1.2 mrg {
394 1.1.1.2 mrg new_fd = old_fd;
395 1.1.1.2 mrg if ((flags & FD_CLOEXEC) == 0 && fcntl (old_fd, F_SETFD, FD_CLOEXEC) < 0)
396 1.1.1.2 mrg return -1;
397 1.1.1.2 mrg }
398 1.1.1.2 mrg /* Otherwise we need to save a copy of OLD_FD before installing CHILD_FD. */
399 1.1.1.2 mrg else
400 1.1.1.2 mrg {
401 1.1.1.2 mrg #ifdef F_DUPFD_CLOEXEC
402 1.1.1.2 mrg new_fd = fcntl (old_fd, F_DUPFD_CLOEXEC, 3);
403 1.1.1.2 mrg if (new_fd < 0)
404 1.1.1.2 mrg return -1;
405 1.1.1.2 mrg #else
406 1.1.1.2 mrg /* Prefer F_DUPFD over dup in order to avoid getting a new fd
407 1.1.1.2 mrg in the range 0-2, right where a new stderr fd might get put. */
408 1.1.1.2 mrg new_fd = fcntl (old_fd, F_DUPFD, 3);
409 1.1.1.2 mrg if (new_fd < 0)
410 1.1.1.2 mrg return -1;
411 1.1.1.2 mrg if (fcntl (new_fd, F_SETFD, FD_CLOEXEC) < 0)
412 1.1.1.2 mrg return -1;
413 1.1.1.2 mrg #endif
414 1.1.1.2 mrg if (dup2 (child_fd, old_fd) < 0)
415 1.1.1.2 mrg return -1;
416 1.1.1.2 mrg }
417 1.1.1.2 mrg
418 1.1.1.2 mrg *pflags = flags;
419 1.1.1.2 mrg if (pnew_fd)
420 1.1.1.2 mrg *pnew_fd = new_fd;
421 1.1.1.2 mrg else if (new_fd != old_fd)
422 1.1.1.2 mrg abort ();
423 1.1.1.2 mrg
424 1.1.1.2 mrg return 0;
425 1.1.1.2 mrg }
426 1.1.1.2 mrg
427 1.1.1.2 mrg /* Subroutine of pex_unix_exec_child. Move SAVE_FD back to OLD_FD
428 1.1.1.2 mrg restoring FLAGS. If SAVE_FD < 0, OLD_FD is to be closed. */
429 1.1.1.2 mrg
430 1.1.1.2 mrg static int
431 1.1.1.2 mrg restore_fd(int old_fd, int save_fd, int flags)
432 1.1.1.2 mrg {
433 1.1.1.2 mrg /* For SAVE_FD < 0, all we have to do is restore the
434 1.1.1.2 mrg "closed-ness" of the original. */
435 1.1.1.2 mrg if (save_fd < 0)
436 1.1.1.2 mrg return close (old_fd);
437 1.1.1.2 mrg
438 1.1.1.2 mrg /* For SAVE_FD == OLD_FD, all we have to do is restore the
439 1.1.1.2 mrg original setting of the CLOEXEC flag. */
440 1.1.1.2 mrg if (save_fd == old_fd)
441 1.1.1.2 mrg {
442 1.1.1.2 mrg if (flags & FD_CLOEXEC)
443 1.1.1.2 mrg return 0;
444 1.1.1.2 mrg return fcntl (old_fd, F_SETFD, flags);
445 1.1.1.2 mrg }
446 1.1.1.2 mrg
447 1.1.1.2 mrg /* Otherwise we have to move the descriptor back, restore the flags,
448 1.1.1.2 mrg and close the saved copy. */
449 1.1.1.2 mrg #ifdef HAVE_DUP3
450 1.1.1.2 mrg if (flags == FD_CLOEXEC)
451 1.1.1.2 mrg {
452 1.1.1.2 mrg if (dup3 (save_fd, old_fd, O_CLOEXEC) < 0)
453 1.1.1.2 mrg return -1;
454 1.1.1.2 mrg }
455 1.1.1.2 mrg else
456 1.1.1.2 mrg #endif
457 1.1.1.2 mrg {
458 1.1.1.2 mrg if (dup2 (save_fd, old_fd) < 0)
459 1.1.1.2 mrg return -1;
460 1.1.1.2 mrg if (flags != 0 && fcntl (old_fd, F_SETFD, flags) < 0)
461 1.1.1.2 mrg return -1;
462 1.1.1.2 mrg }
463 1.1.1.2 mrg return close (save_fd);
464 1.1.1.2 mrg }
465 1.1.1.2 mrg
466 1.1.1.2 mrg static pid_t
467 1.1.1.2 mrg pex_unix_exec_child (struct pex_obj *obj ATTRIBUTE_UNUSED,
468 1.1.1.2 mrg int flags, const char *executable,
469 1.1.1.2 mrg char * const * argv, char * const * env,
470 1.1.1.2 mrg int in, int out, int errdes, int toclose,
471 1.1.1.2 mrg const char **errmsg, int *err)
472 1.1.1.2 mrg {
473 1.1.1.2 mrg int fl_in = 0, fl_out = 0, fl_err = 0, fl_tc = 0;
474 1.1.1.2 mrg int save_in = -1, save_out = -1, save_err = -1;
475 1.1.1.2 mrg int max, retries;
476 1.1.1.2 mrg pid_t pid;
477 1.1.1.2 mrg
478 1.1.1.2 mrg if (flags & PEX_STDERR_TO_STDOUT)
479 1.1.1.2 mrg errdes = out;
480 1.1.1.2 mrg
481 1.1.1.2 mrg /* We need the three standard file descriptors to be set up as for
482 1.1.1.2 mrg the child before we perform the spawn. The file descriptors for
483 1.1.1.2 mrg the parent need to be moved and marked for close-on-exec. */
484 1.1.1.2 mrg if (in != STDIN_FILE_NO
485 1.1.1.2 mrg && save_and_install_fd (&save_in, &fl_in, STDIN_FILE_NO, in) < 0)
486 1.1.1.2 mrg goto error_dup2;
487 1.1.1.2 mrg if (out != STDOUT_FILE_NO
488 1.1.1.2 mrg && save_and_install_fd (&save_out, &fl_out, STDOUT_FILE_NO, out) < 0)
489 1.1.1.2 mrg goto error_dup2;
490 1.1.1.2 mrg if (errdes != STDERR_FILE_NO
491 1.1.1.2 mrg && save_and_install_fd (&save_err, &fl_err, STDERR_FILE_NO, errdes) < 0)
492 1.1.1.2 mrg goto error_dup2;
493 1.1.1.2 mrg if (toclose >= 0
494 1.1.1.2 mrg && save_and_install_fd (NULL, &fl_tc, toclose, -1) < 0)
495 1.1.1.2 mrg goto error_dup2;
496 1.1.1.2 mrg
497 1.1.1.2 mrg /* Now that we've moved the file descriptors for the child into place,
498 1.1.1.2 mrg close the originals. Be careful not to close any of the standard
499 1.1.1.2 mrg file descriptors that we just set up. */
500 1.1.1.2 mrg max = -1;
501 1.1.1.2 mrg if (errdes >= 0)
502 1.1.1.2 mrg max = STDERR_FILE_NO;
503 1.1.1.2 mrg else if (out >= 0)
504 1.1.1.2 mrg max = STDOUT_FILE_NO;
505 1.1.1.2 mrg else if (in >= 0)
506 1.1.1.2 mrg max = STDIN_FILE_NO;
507 1.1.1.2 mrg if (in > max)
508 1.1.1.2 mrg close (in);
509 1.1.1.2 mrg if (out > max)
510 1.1.1.2 mrg close (out);
511 1.1.1.2 mrg if (errdes > max && errdes != out)
512 1.1.1.2 mrg close (errdes);
513 1.1.1.2 mrg
514 1.1.1.2 mrg /* If we were not given an environment, use the global environment. */
515 1.1.1.2 mrg if (env == NULL)
516 1.1.1.2 mrg env = environ;
517 1.1.1.2 mrg
518 1.1.1.2 mrg /* Launch the program. If we get EAGAIN (normally out of pid's), try
519 1.1.1.2 mrg again a few times with increasing backoff times. */
520 1.1.1.2 mrg retries = 0;
521 1.1.1.2 mrg while (1)
522 1.1.1.2 mrg {
523 1.1.1.2 mrg typedef const char * const *cc_cp;
524 1.1.1.2 mrg
525 1.1.1.2 mrg if (flags & PEX_SEARCH)
526 1.1.1.2 mrg pid = spawnvpe (_P_NOWAITO, executable, (cc_cp)argv, (cc_cp)env);
527 1.1.1.2 mrg else
528 1.1.1.2 mrg pid = spawnve (_P_NOWAITO, executable, (cc_cp)argv, (cc_cp)env);
529 1.1.1.2 mrg
530 1.1.1.2 mrg if (pid > 0)
531 1.1.1.2 mrg break;
532 1.1.1.2 mrg
533 1.1.1.2 mrg *err = errno;
534 1.1.1.2 mrg *errmsg = "spawn";
535 1.1.1.2 mrg if (errno != EAGAIN || ++retries == 4)
536 1.1.1.2 mrg return (pid_t) -1;
537 1.1.1.2 mrg sleep (1 << retries);
538 1.1.1.2 mrg }
539 1.1.1.2 mrg
540 1.1.1.2 mrg /* Success. Restore the parent's file descriptors that we saved above. */
541 1.1.1.2 mrg if (toclose >= 0
542 1.1.1.2 mrg && restore_fd (toclose, toclose, fl_tc) < 0)
543 1.1.1.2 mrg goto error_dup2;
544 1.1.1.2 mrg if (in != STDIN_FILE_NO
545 1.1.1.2 mrg && restore_fd (STDIN_FILE_NO, save_in, fl_in) < 0)
546 1.1.1.2 mrg goto error_dup2;
547 1.1.1.2 mrg if (out != STDOUT_FILE_NO
548 1.1.1.2 mrg && restore_fd (STDOUT_FILE_NO, save_out, fl_out) < 0)
549 1.1.1.2 mrg goto error_dup2;
550 1.1.1.2 mrg if (errdes != STDERR_FILE_NO
551 1.1.1.2 mrg && restore_fd (STDERR_FILE_NO, save_err, fl_err) < 0)
552 1.1.1.2 mrg goto error_dup2;
553 1.1.1.2 mrg
554 1.1.1.2 mrg return pid;
555 1.1.1.2 mrg
556 1.1.1.2 mrg error_dup2:
557 1.1.1.2 mrg *err = errno;
558 1.1.1.2 mrg *errmsg = "dup2";
559 1.1.1.2 mrg return (pid_t) -1;
560 1.1.1.2 mrg }
561 1.1.1.2 mrg
562 1.1.1.2 mrg #else
563 1.1.1.2 mrg /* Implementation of pex->exec_child using standard vfork + exec. */
564 1.1.1.2 mrg
565 1.1 mrg static pid_t
566 1.1 mrg pex_unix_exec_child (struct pex_obj *obj, int flags, const char *executable,
567 1.1 mrg char * const * argv, char * const * env,
568 1.1 mrg int in, int out, int errdes,
569 1.1 mrg int toclose, const char **errmsg, int *err)
570 1.1 mrg {
571 1.1.1.7 mrg pid_t pid = -1;
572 1.1.1.7 mrg /* Tuple to communicate error from child to parent. We can safely
573 1.1.1.7 mrg transfer string literal pointers as both run with identical
574 1.1.1.7 mrg address mappings. */
575 1.1.1.7 mrg struct fn_err
576 1.1.1.7 mrg {
577 1.1.1.7 mrg const char *fn;
578 1.1.1.7 mrg int err;
579 1.1.1.7 mrg };
580 1.1.1.7 mrg volatile int do_pipe = 0;
581 1.1.1.7 mrg volatile int pipes[2]; /* [0]:reader,[1]:writer. */
582 1.1.1.7 mrg #ifdef O_CLOEXEC
583 1.1.1.7 mrg do_pipe = 1;
584 1.1.1.7 mrg #endif
585 1.1.1.7 mrg if (do_pipe)
586 1.1.1.7 mrg {
587 1.1.1.7 mrg #ifdef HAVE_PIPE2
588 1.1.1.7 mrg if (pipe2 ((int *)pipes, O_CLOEXEC))
589 1.1.1.7 mrg do_pipe = 0;
590 1.1.1.7 mrg #else
591 1.1.1.7 mrg if (pipe ((int *)pipes))
592 1.1.1.7 mrg do_pipe = 0;
593 1.1.1.7 mrg else
594 1.1.1.7 mrg {
595 1.1.1.7 mrg if (fcntl (pipes[1], F_SETFD, FD_CLOEXEC) == -1)
596 1.1.1.7 mrg {
597 1.1.1.7 mrg close (pipes[0]);
598 1.1.1.7 mrg close (pipes[1]);
599 1.1.1.7 mrg do_pipe = 0;
600 1.1.1.7 mrg }
601 1.1.1.7 mrg }
602 1.1.1.7 mrg #endif
603 1.1.1.7 mrg }
604 1.1 mrg
605 1.1 mrg /* We declare these to be volatile to avoid warnings from gcc about
606 1.1 mrg them being clobbered by vfork. */
607 1.1.1.7 mrg volatile int sleep_interval = 1;
608 1.1 mrg volatile int retries;
609 1.1 mrg
610 1.1 mrg /* We vfork and then set environ in the child before calling execvp.
611 1.1 mrg This clobbers the parent's environ so we need to restore it.
612 1.1 mrg It would be nice to use one of the exec* functions that takes an
613 1.1.1.7 mrg environment as a parameter, but that may have portability
614 1.1.1.7 mrg issues. It is marked volatile so the child doesn't consider it a
615 1.1.1.7 mrg dead variable and therefore clobber where ever it is stored. */
616 1.1.1.7 mrg char **volatile save_environ = environ;
617 1.1 mrg
618 1.1 mrg for (retries = 0; retries < 4; ++retries)
619 1.1 mrg {
620 1.1 mrg pid = vfork ();
621 1.1 mrg if (pid >= 0)
622 1.1 mrg break;
623 1.1 mrg sleep (sleep_interval);
624 1.1 mrg sleep_interval *= 2;
625 1.1 mrg }
626 1.1 mrg
627 1.1 mrg switch (pid)
628 1.1 mrg {
629 1.1 mrg case -1:
630 1.1.1.7 mrg if (do_pipe)
631 1.1.1.7 mrg {
632 1.1.1.7 mrg close (pipes[0]);
633 1.1.1.7 mrg close (pipes[1]);
634 1.1.1.7 mrg }
635 1.1 mrg *err = errno;
636 1.1 mrg *errmsg = VFORK_STRING;
637 1.1 mrg return (pid_t) -1;
638 1.1 mrg
639 1.1 mrg case 0:
640 1.1 mrg /* Child process. */
641 1.1.1.7 mrg {
642 1.1.1.7 mrg struct fn_err failed;
643 1.1.1.7 mrg failed.fn = NULL;
644 1.1.1.7 mrg
645 1.1.1.7 mrg if (do_pipe)
646 1.1.1.7 mrg close (pipes[0]);
647 1.1.1.7 mrg if (!failed.fn && in != STDIN_FILE_NO)
648 1.1.1.7 mrg {
649 1.1.1.7 mrg if (dup2 (in, STDIN_FILE_NO) < 0)
650 1.1.1.7 mrg failed.fn = "dup2", failed.err = errno;
651 1.1.1.7 mrg else if (close (in) < 0)
652 1.1.1.7 mrg failed.fn = "close", failed.err = errno;
653 1.1.1.7 mrg }
654 1.1.1.7 mrg if (!failed.fn && out != STDOUT_FILE_NO)
655 1.1.1.7 mrg {
656 1.1.1.7 mrg if (dup2 (out, STDOUT_FILE_NO) < 0)
657 1.1.1.7 mrg failed.fn = "dup2", failed.err = errno;
658 1.1.1.7 mrg else if (close (out) < 0)
659 1.1.1.7 mrg failed.fn = "close", failed.err = errno;
660 1.1.1.7 mrg }
661 1.1.1.7 mrg if (!failed.fn && errdes != STDERR_FILE_NO)
662 1.1.1.7 mrg {
663 1.1.1.7 mrg if (dup2 (errdes, STDERR_FILE_NO) < 0)
664 1.1.1.7 mrg failed.fn = "dup2", failed.err = errno;
665 1.1.1.7 mrg else if (close (errdes) < 0)
666 1.1.1.7 mrg failed.fn = "close", failed.err = errno;
667 1.1.1.7 mrg }
668 1.1.1.7 mrg if (!failed.fn && toclose >= 0)
669 1.1.1.7 mrg {
670 1.1.1.7 mrg if (close (toclose) < 0)
671 1.1.1.7 mrg failed.fn = "close", failed.err = errno;
672 1.1.1.7 mrg }
673 1.1.1.7 mrg if (!failed.fn && (flags & PEX_STDERR_TO_STDOUT) != 0)
674 1.1.1.7 mrg {
675 1.1.1.7 mrg if (dup2 (STDOUT_FILE_NO, STDERR_FILE_NO) < 0)
676 1.1.1.7 mrg failed.fn = "dup2", failed.err = errno;
677 1.1.1.7 mrg }
678 1.1.1.7 mrg if (!failed.fn)
679 1.1.1.7 mrg {
680 1.1.1.7 mrg if (env)
681 1.1.1.7 mrg /* NOTE: In a standard vfork implementation this clobbers
682 1.1.1.7 mrg the parent's copy of environ "too" (in reality there's
683 1.1.1.7 mrg only one copy). This is ok as we restore it below. */
684 1.1.1.7 mrg environ = (char**) env;
685 1.1.1.7 mrg if ((flags & PEX_SEARCH) != 0)
686 1.1.1.7 mrg {
687 1.1.1.7 mrg execvp (executable, to_ptr32 (argv));
688 1.1.1.7 mrg failed.fn = "execvp", failed.err = errno;
689 1.1.1.7 mrg }
690 1.1.1.7 mrg else
691 1.1.1.7 mrg {
692 1.1.1.7 mrg execv (executable, to_ptr32 (argv));
693 1.1.1.7 mrg failed.fn = "execv", failed.err = errno;
694 1.1.1.7 mrg }
695 1.1.1.7 mrg }
696 1.1.1.7 mrg
697 1.1.1.7 mrg /* Something failed, report an error. We don't use stdio
698 1.1.1.7 mrg routines, because we might be here due to a vfork call. */
699 1.1.1.7 mrg ssize_t retval = 0;
700 1.1.1.7 mrg
701 1.1.1.7 mrg if (!do_pipe
702 1.1.1.7 mrg || write (pipes[1], &failed, sizeof (failed)) != sizeof (failed))
703 1.1.1.7 mrg {
704 1.1.1.7 mrg /* The parent will not see our scream above, so write to
705 1.1.1.7 mrg stdout. */
706 1.1.1.7 mrg #define writeerr(s) (retval |= write (STDERR_FILE_NO, s, strlen (s)))
707 1.1.1.7 mrg writeerr (obj->pname);
708 1.1.1.7 mrg writeerr (": error trying to exec '");
709 1.1.1.7 mrg writeerr (executable);
710 1.1.1.7 mrg writeerr ("': ");
711 1.1.1.7 mrg writeerr (failed.fn);
712 1.1.1.7 mrg writeerr (": ");
713 1.1.1.7 mrg writeerr (xstrerror (failed.err));
714 1.1.1.7 mrg writeerr ("\n");
715 1.1.1.7 mrg #undef writeerr
716 1.1.1.7 mrg }
717 1.1 mrg
718 1.1.1.7 mrg /* Exit with -2 if the error output failed, too. */
719 1.1.1.7 mrg _exit (retval < 0 ? -2 : -1);
720 1.1.1.7 mrg }
721 1.1 mrg /* NOTREACHED */
722 1.1 mrg return (pid_t) -1;
723 1.1 mrg
724 1.1 mrg default:
725 1.1 mrg /* Parent process. */
726 1.1.1.7 mrg {
727 1.1.1.7 mrg /* Restore environ. Note that the parent either doesn't run
728 1.1.1.7 mrg until the child execs/exits (standard vfork behaviour), or
729 1.1.1.7 mrg if it does run then vfork is behaving more like fork. In
730 1.1.1.7 mrg either case we needn't worry about clobbering the child's
731 1.1.1.7 mrg copy of environ. */
732 1.1.1.7 mrg environ = save_environ;
733 1.1.1.7 mrg
734 1.1.1.7 mrg struct fn_err failed;
735 1.1.1.7 mrg failed.fn = NULL;
736 1.1.1.7 mrg if (do_pipe)
737 1.1.1.7 mrg {
738 1.1.1.7 mrg close (pipes[1]);
739 1.1.1.7 mrg ssize_t len = read (pipes[0], &failed, sizeof (failed));
740 1.1.1.7 mrg if (len < 0)
741 1.1.1.7 mrg failed.fn = NULL;
742 1.1.1.7 mrg close (pipes[0]);
743 1.1.1.7 mrg }
744 1.1 mrg
745 1.1.1.7 mrg if (!failed.fn && in != STDIN_FILE_NO)
746 1.1 mrg if (close (in) < 0)
747 1.1.1.7 mrg failed.fn = "close", failed.err = errno;
748 1.1.1.7 mrg if (!failed.fn && out != STDOUT_FILE_NO)
749 1.1 mrg if (close (out) < 0)
750 1.1.1.7 mrg failed.fn = "close", failed.err = errno;
751 1.1.1.7 mrg if (!failed.fn && errdes != STDERR_FILE_NO)
752 1.1 mrg if (close (errdes) < 0)
753 1.1.1.7 mrg failed.fn = "close", failed.err = errno;
754 1.1 mrg
755 1.1.1.7 mrg if (failed.fn)
756 1.1.1.7 mrg {
757 1.1.1.7 mrg *err = failed.err;
758 1.1.1.7 mrg *errmsg = failed.fn;
759 1.1.1.7 mrg return (pid_t) -1;
760 1.1.1.7 mrg }
761 1.1.1.7 mrg }
762 1.1 mrg return pid;
763 1.1 mrg }
764 1.1 mrg }
765 1.1.1.2 mrg #endif /* SPAWN */
766 1.1 mrg
767 1.1 mrg /* Wait for a child process to complete. */
768 1.1 mrg
769 1.1 mrg static int
770 1.1 mrg pex_unix_wait (struct pex_obj *obj, pid_t pid, int *status,
771 1.1 mrg struct pex_time *time, int done, const char **errmsg,
772 1.1 mrg int *err)
773 1.1 mrg {
774 1.1 mrg /* If we are cleaning up when the caller didn't retrieve process
775 1.1 mrg status for some reason, encourage the process to go away. */
776 1.1 mrg if (done)
777 1.1 mrg kill (pid, SIGTERM);
778 1.1 mrg
779 1.1 mrg if (pex_wait (obj, pid, status, time) < 0)
780 1.1 mrg {
781 1.1 mrg *err = errno;
782 1.1 mrg *errmsg = "wait";
783 1.1 mrg return -1;
784 1.1 mrg }
785 1.1 mrg
786 1.1 mrg return 0;
787 1.1 mrg }
788 1.1 mrg
789 1.1 mrg /* Create a pipe. */
790 1.1 mrg
791 1.1 mrg static int
792 1.1 mrg pex_unix_pipe (struct pex_obj *obj ATTRIBUTE_UNUSED, int *p,
793 1.1 mrg int binary ATTRIBUTE_UNUSED)
794 1.1 mrg {
795 1.1 mrg return pipe (p);
796 1.1 mrg }
797 1.1 mrg
798 1.1 mrg /* Get a FILE pointer to read from a file descriptor. */
799 1.1 mrg
800 1.1 mrg static FILE *
801 1.1 mrg pex_unix_fdopenr (struct pex_obj *obj ATTRIBUTE_UNUSED, int fd,
802 1.1 mrg int binary ATTRIBUTE_UNUSED)
803 1.1 mrg {
804 1.1 mrg return fdopen (fd, "r");
805 1.1 mrg }
806 1.1 mrg
807 1.1 mrg static FILE *
808 1.1 mrg pex_unix_fdopenw (struct pex_obj *obj ATTRIBUTE_UNUSED, int fd,
809 1.1 mrg int binary ATTRIBUTE_UNUSED)
810 1.1 mrg {
811 1.1 mrg if (fcntl (fd, F_SETFD, FD_CLOEXEC) < 0)
812 1.1 mrg return NULL;
813 1.1 mrg return fdopen (fd, "w");
814 1.1 mrg }
815 1.1 mrg
816 1.1 mrg static void
817 1.1 mrg pex_unix_cleanup (struct pex_obj *obj ATTRIBUTE_UNUSED)
818 1.1 mrg {
819 1.1 mrg #if !defined (HAVE_WAIT4) && !defined (HAVE_WAITPID)
820 1.1 mrg while (obj->sysdep != NULL)
821 1.1 mrg {
822 1.1 mrg struct status_list *this;
823 1.1 mrg struct status_list *next;
824 1.1 mrg
825 1.1 mrg this = (struct status_list *) obj->sysdep;
826 1.1 mrg next = this->next;
827 1.1 mrg free (this);
828 1.1 mrg obj->sysdep = (void *) next;
829 1.1 mrg }
830 1.1 mrg #endif
831 1.1 mrg }
832