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