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