Home | History | Annotate | Line # | Download | only in libiberty
pex-unix.c revision 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.8  mrg    Copyright (C) 1996-2017 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.6  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.3  mrg #ifdef HAVE_PROCESS_H
     59  1.3  mrg #include <process.h>
     60  1.3  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.3  mrg   /* Count number of arguments.  */
     89  1.3  mrg   for (argc = 0; ptr64[argc] != NULL; argc++)
     90  1.3  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.3  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 void pex_child_error (struct pex_obj *, const char *, const char *, int)
    302  1.1  mrg      ATTRIBUTE_NORETURN;
    303  1.1  mrg static int pex_unix_open_read (struct pex_obj *, const char *, int);
    304  1.5  mrg static int pex_unix_open_write (struct pex_obj *, const char *, int, int);
    305  1.1  mrg static pid_t pex_unix_exec_child (struct pex_obj *, int, const char *,
    306  1.1  mrg 				 char * const *, char * const *,
    307  1.1  mrg 				 int, int, int, int,
    308  1.1  mrg 				 const char **, int *);
    309  1.1  mrg static int pex_unix_close (struct pex_obj *, int);
    310  1.1  mrg static int pex_unix_wait (struct pex_obj *, pid_t, int *, struct pex_time *,
    311  1.1  mrg 			  int, const char **, int *);
    312  1.1  mrg static int pex_unix_pipe (struct pex_obj *, int *, int);
    313  1.1  mrg static FILE *pex_unix_fdopenr (struct pex_obj *, int, int);
    314  1.1  mrg static FILE *pex_unix_fdopenw (struct pex_obj *, int, int);
    315  1.1  mrg static void pex_unix_cleanup (struct pex_obj *);
    316  1.1  mrg 
    317  1.1  mrg /* The list of functions we pass to the common routines.  */
    318  1.1  mrg 
    319  1.1  mrg const struct pex_funcs funcs =
    320  1.1  mrg {
    321  1.1  mrg   pex_unix_open_read,
    322  1.1  mrg   pex_unix_open_write,
    323  1.1  mrg   pex_unix_exec_child,
    324  1.1  mrg   pex_unix_close,
    325  1.1  mrg   pex_unix_wait,
    326  1.1  mrg   pex_unix_pipe,
    327  1.1  mrg   pex_unix_fdopenr,
    328  1.1  mrg   pex_unix_fdopenw,
    329  1.1  mrg   pex_unix_cleanup
    330  1.1  mrg };
    331  1.1  mrg 
    332  1.1  mrg /* Return a newly initialized pex_obj structure.  */
    333  1.1  mrg 
    334  1.1  mrg struct pex_obj *
    335  1.1  mrg pex_init (int flags, const char *pname, const char *tempbase)
    336  1.1  mrg {
    337  1.1  mrg   return pex_init_common (flags, pname, tempbase, &funcs);
    338  1.1  mrg }
    339  1.1  mrg 
    340  1.1  mrg /* Open a file for reading.  */
    341  1.1  mrg 
    342  1.1  mrg static int
    343  1.1  mrg pex_unix_open_read (struct pex_obj *obj ATTRIBUTE_UNUSED, const char *name,
    344  1.1  mrg 		    int binary ATTRIBUTE_UNUSED)
    345  1.1  mrg {
    346  1.1  mrg   return open (name, O_RDONLY);
    347  1.1  mrg }
    348  1.1  mrg 
    349  1.1  mrg /* Open a file for writing.  */
    350  1.1  mrg 
    351  1.1  mrg static int
    352  1.1  mrg pex_unix_open_write (struct pex_obj *obj ATTRIBUTE_UNUSED, const char *name,
    353  1.5  mrg 		     int binary ATTRIBUTE_UNUSED, int append)
    354  1.1  mrg {
    355  1.1  mrg   /* Note that we can't use O_EXCL here because gcc may have already
    356  1.1  mrg      created the temporary file via make_temp_file.  */
    357  1.5  mrg   return open (name, O_WRONLY | O_CREAT
    358  1.5  mrg 		     | (append ? O_APPEND : O_TRUNC), PUBLIC_MODE);
    359  1.1  mrg }
    360  1.1  mrg 
    361  1.1  mrg /* Close a file.  */
    362  1.1  mrg 
    363  1.1  mrg static int
    364  1.1  mrg pex_unix_close (struct pex_obj *obj ATTRIBUTE_UNUSED, int fd)
    365  1.1  mrg {
    366  1.1  mrg   return close (fd);
    367  1.1  mrg }
    368  1.1  mrg 
    369  1.1  mrg /* Report an error from a child process.  We don't use stdio routines,
    370  1.1  mrg    because we might be here due to a vfork call.  */
    371  1.1  mrg 
    372  1.1  mrg static void
    373  1.1  mrg pex_child_error (struct pex_obj *obj, const char *executable,
    374  1.1  mrg 		 const char *errmsg, int err)
    375  1.1  mrg {
    376  1.1  mrg   int retval = 0;
    377  1.1  mrg #define writeerr(s) retval |= (write (STDERR_FILE_NO, s, strlen (s)) < 0)
    378  1.1  mrg   writeerr (obj->pname);
    379  1.1  mrg   writeerr (": error trying to exec '");
    380  1.1  mrg   writeerr (executable);
    381  1.1  mrg   writeerr ("': ");
    382  1.1  mrg   writeerr (errmsg);
    383  1.1  mrg   writeerr (": ");
    384  1.1  mrg   writeerr (xstrerror (err));
    385  1.1  mrg   writeerr ("\n");
    386  1.1  mrg #undef writeerr
    387  1.1  mrg   /* Exit with -2 if the error output failed, too.  */
    388  1.1  mrg   _exit (retval == 0 ? -1 : -2);
    389  1.1  mrg }
    390  1.1  mrg 
    391  1.1  mrg /* Execute a child.  */
    392  1.1  mrg 
    393  1.3  mrg #if defined(HAVE_SPAWNVE) && defined(HAVE_SPAWNVPE)
    394  1.3  mrg /* Implementation of pex->exec_child using the Cygwin spawn operation.  */
    395  1.3  mrg 
    396  1.3  mrg /* Subroutine of pex_unix_exec_child.  Move OLD_FD to a new file descriptor
    397  1.3  mrg    to be stored in *PNEW_FD, save the flags in *PFLAGS, and arrange for the
    398  1.3  mrg    saved copy to be close-on-exec.  Move CHILD_FD into OLD_FD.  If CHILD_FD
    399  1.3  mrg    is -1, OLD_FD is to be closed.  Return -1 on error.  */
    400  1.3  mrg 
    401  1.3  mrg static int
    402  1.3  mrg save_and_install_fd(int *pnew_fd, int *pflags, int old_fd, int child_fd)
    403  1.3  mrg {
    404  1.3  mrg   int new_fd, flags;
    405  1.3  mrg 
    406  1.3  mrg   flags = fcntl (old_fd, F_GETFD);
    407  1.3  mrg 
    408  1.3  mrg   /* If we could not retrieve the flags, then OLD_FD was not open.  */
    409  1.3  mrg   if (flags < 0)
    410  1.3  mrg     {
    411  1.3  mrg       new_fd = -1, flags = 0;
    412  1.3  mrg       if (child_fd >= 0 && dup2 (child_fd, old_fd) < 0)
    413  1.3  mrg 	return -1;
    414  1.3  mrg     }
    415  1.3  mrg   /* If we wish to close OLD_FD, just mark it CLOEXEC.  */
    416  1.3  mrg   else if (child_fd == -1)
    417  1.3  mrg     {
    418  1.3  mrg       new_fd = old_fd;
    419  1.3  mrg       if ((flags & FD_CLOEXEC) == 0 && fcntl (old_fd, F_SETFD, FD_CLOEXEC) < 0)
    420  1.3  mrg 	return -1;
    421  1.3  mrg     }
    422  1.3  mrg   /* Otherwise we need to save a copy of OLD_FD before installing CHILD_FD.  */
    423  1.3  mrg   else
    424  1.3  mrg     {
    425  1.3  mrg #ifdef F_DUPFD_CLOEXEC
    426  1.3  mrg       new_fd = fcntl (old_fd, F_DUPFD_CLOEXEC, 3);
    427  1.3  mrg       if (new_fd < 0)
    428  1.3  mrg 	return -1;
    429  1.3  mrg #else
    430  1.3  mrg       /* Prefer F_DUPFD over dup in order to avoid getting a new fd
    431  1.3  mrg 	 in the range 0-2, right where a new stderr fd might get put.  */
    432  1.3  mrg       new_fd = fcntl (old_fd, F_DUPFD, 3);
    433  1.3  mrg       if (new_fd < 0)
    434  1.3  mrg 	return -1;
    435  1.3  mrg       if (fcntl (new_fd, F_SETFD, FD_CLOEXEC) < 0)
    436  1.3  mrg 	return -1;
    437  1.3  mrg #endif
    438  1.3  mrg       if (dup2 (child_fd, old_fd) < 0)
    439  1.3  mrg 	return -1;
    440  1.3  mrg     }
    441  1.3  mrg 
    442  1.3  mrg   *pflags = flags;
    443  1.3  mrg   if (pnew_fd)
    444  1.3  mrg     *pnew_fd = new_fd;
    445  1.3  mrg   else if (new_fd != old_fd)
    446  1.3  mrg     abort ();
    447  1.3  mrg 
    448  1.3  mrg   return 0;
    449  1.3  mrg }
    450  1.3  mrg 
    451  1.3  mrg /* Subroutine of pex_unix_exec_child.  Move SAVE_FD back to OLD_FD
    452  1.3  mrg    restoring FLAGS.  If SAVE_FD < 0, OLD_FD is to be closed.  */
    453  1.3  mrg 
    454  1.3  mrg static int
    455  1.3  mrg restore_fd(int old_fd, int save_fd, int flags)
    456  1.3  mrg {
    457  1.3  mrg   /* For SAVE_FD < 0, all we have to do is restore the
    458  1.3  mrg      "closed-ness" of the original.  */
    459  1.3  mrg   if (save_fd < 0)
    460  1.3  mrg     return close (old_fd);
    461  1.3  mrg 
    462  1.3  mrg   /* For SAVE_FD == OLD_FD, all we have to do is restore the
    463  1.3  mrg      original setting of the CLOEXEC flag.  */
    464  1.3  mrg   if (save_fd == old_fd)
    465  1.3  mrg     {
    466  1.3  mrg       if (flags & FD_CLOEXEC)
    467  1.3  mrg 	return 0;
    468  1.3  mrg       return fcntl (old_fd, F_SETFD, flags);
    469  1.3  mrg     }
    470  1.3  mrg 
    471  1.3  mrg   /* Otherwise we have to move the descriptor back, restore the flags,
    472  1.3  mrg      and close the saved copy.  */
    473  1.3  mrg #ifdef HAVE_DUP3
    474  1.3  mrg   if (flags == FD_CLOEXEC)
    475  1.3  mrg     {
    476  1.3  mrg       if (dup3 (save_fd, old_fd, O_CLOEXEC) < 0)
    477  1.3  mrg 	return -1;
    478  1.3  mrg     }
    479  1.3  mrg   else
    480  1.3  mrg #endif
    481  1.3  mrg     {
    482  1.3  mrg       if (dup2 (save_fd, old_fd) < 0)
    483  1.3  mrg 	return -1;
    484  1.3  mrg       if (flags != 0 && fcntl (old_fd, F_SETFD, flags) < 0)
    485  1.3  mrg 	return -1;
    486  1.3  mrg     }
    487  1.3  mrg   return close (save_fd);
    488  1.3  mrg }
    489  1.3  mrg 
    490  1.3  mrg static pid_t
    491  1.3  mrg pex_unix_exec_child (struct pex_obj *obj ATTRIBUTE_UNUSED,
    492  1.3  mrg 		     int flags, const char *executable,
    493  1.3  mrg 		     char * const * argv, char * const * env,
    494  1.3  mrg                      int in, int out, int errdes, int toclose,
    495  1.3  mrg 		     const char **errmsg, int *err)
    496  1.3  mrg {
    497  1.3  mrg   int fl_in = 0, fl_out = 0, fl_err = 0, fl_tc = 0;
    498  1.3  mrg   int save_in = -1, save_out = -1, save_err = -1;
    499  1.3  mrg   int max, retries;
    500  1.3  mrg   pid_t pid;
    501  1.3  mrg 
    502  1.3  mrg   if (flags & PEX_STDERR_TO_STDOUT)
    503  1.3  mrg     errdes = out;
    504  1.3  mrg 
    505  1.3  mrg   /* We need the three standard file descriptors to be set up as for
    506  1.3  mrg      the child before we perform the spawn.  The file descriptors for
    507  1.3  mrg      the parent need to be moved and marked for close-on-exec.  */
    508  1.3  mrg   if (in != STDIN_FILE_NO
    509  1.3  mrg       && save_and_install_fd (&save_in, &fl_in, STDIN_FILE_NO, in) < 0)
    510  1.3  mrg     goto error_dup2;
    511  1.3  mrg   if (out != STDOUT_FILE_NO
    512  1.3  mrg       && save_and_install_fd (&save_out, &fl_out, STDOUT_FILE_NO, out) < 0)
    513  1.3  mrg     goto error_dup2;
    514  1.3  mrg   if (errdes != STDERR_FILE_NO
    515  1.3  mrg       && save_and_install_fd (&save_err, &fl_err, STDERR_FILE_NO, errdes) < 0)
    516  1.3  mrg     goto error_dup2;
    517  1.3  mrg   if (toclose >= 0
    518  1.3  mrg       && save_and_install_fd (NULL, &fl_tc, toclose, -1) < 0)
    519  1.3  mrg     goto error_dup2;
    520  1.3  mrg 
    521  1.3  mrg   /* Now that we've moved the file descriptors for the child into place,
    522  1.3  mrg      close the originals.  Be careful not to close any of the standard
    523  1.3  mrg      file descriptors that we just set up.  */
    524  1.3  mrg   max = -1;
    525  1.3  mrg   if (errdes >= 0)
    526  1.3  mrg     max = STDERR_FILE_NO;
    527  1.3  mrg   else if (out >= 0)
    528  1.3  mrg     max = STDOUT_FILE_NO;
    529  1.3  mrg   else if (in >= 0)
    530  1.3  mrg     max = STDIN_FILE_NO;
    531  1.3  mrg   if (in > max)
    532  1.3  mrg     close (in);
    533  1.3  mrg   if (out > max)
    534  1.3  mrg     close (out);
    535  1.3  mrg   if (errdes > max && errdes != out)
    536  1.3  mrg     close (errdes);
    537  1.3  mrg 
    538  1.3  mrg   /* If we were not given an environment, use the global environment.  */
    539  1.3  mrg   if (env == NULL)
    540  1.3  mrg     env = environ;
    541  1.3  mrg 
    542  1.3  mrg   /* Launch the program.  If we get EAGAIN (normally out of pid's), try
    543  1.3  mrg      again a few times with increasing backoff times.  */
    544  1.3  mrg   retries = 0;
    545  1.3  mrg   while (1)
    546  1.3  mrg     {
    547  1.3  mrg       typedef const char * const *cc_cp;
    548  1.3  mrg 
    549  1.3  mrg       if (flags & PEX_SEARCH)
    550  1.3  mrg 	pid = spawnvpe (_P_NOWAITO, executable, (cc_cp)argv, (cc_cp)env);
    551  1.3  mrg       else
    552  1.3  mrg 	pid = spawnve (_P_NOWAITO, executable, (cc_cp)argv, (cc_cp)env);
    553  1.3  mrg 
    554  1.3  mrg       if (pid > 0)
    555  1.3  mrg 	break;
    556  1.3  mrg 
    557  1.3  mrg       *err = errno;
    558  1.3  mrg       *errmsg = "spawn";
    559  1.3  mrg       if (errno != EAGAIN || ++retries == 4)
    560  1.3  mrg 	return (pid_t) -1;
    561  1.3  mrg       sleep (1 << retries);
    562  1.3  mrg     }
    563  1.3  mrg 
    564  1.3  mrg   /* Success.  Restore the parent's file descriptors that we saved above.  */
    565  1.3  mrg   if (toclose >= 0
    566  1.3  mrg       && restore_fd (toclose, toclose, fl_tc) < 0)
    567  1.3  mrg     goto error_dup2;
    568  1.3  mrg   if (in != STDIN_FILE_NO
    569  1.3  mrg       && restore_fd (STDIN_FILE_NO, save_in, fl_in) < 0)
    570  1.3  mrg     goto error_dup2;
    571  1.3  mrg   if (out != STDOUT_FILE_NO
    572  1.3  mrg       && restore_fd (STDOUT_FILE_NO, save_out, fl_out) < 0)
    573  1.3  mrg     goto error_dup2;
    574  1.3  mrg   if (errdes != STDERR_FILE_NO
    575  1.3  mrg       && restore_fd (STDERR_FILE_NO, save_err, fl_err) < 0)
    576  1.3  mrg     goto error_dup2;
    577  1.3  mrg 
    578  1.3  mrg   return pid;
    579  1.3  mrg 
    580  1.3  mrg  error_dup2:
    581  1.3  mrg   *err = errno;
    582  1.3  mrg   *errmsg = "dup2";
    583  1.3  mrg   return (pid_t) -1;
    584  1.3  mrg }
    585  1.3  mrg 
    586  1.3  mrg #else
    587  1.3  mrg /* Implementation of pex->exec_child using standard vfork + exec.  */
    588  1.3  mrg 
    589  1.1  mrg static pid_t
    590  1.1  mrg pex_unix_exec_child (struct pex_obj *obj, int flags, const char *executable,
    591  1.1  mrg 		     char * const * argv, char * const * env,
    592  1.1  mrg                      int in, int out, int errdes,
    593  1.1  mrg 		     int toclose, const char **errmsg, int *err)
    594  1.1  mrg {
    595  1.1  mrg   pid_t pid;
    596  1.1  mrg 
    597  1.1  mrg   /* We declare these to be volatile to avoid warnings from gcc about
    598  1.1  mrg      them being clobbered by vfork.  */
    599  1.1  mrg   volatile int sleep_interval;
    600  1.1  mrg   volatile int retries;
    601  1.1  mrg 
    602  1.1  mrg   /* We vfork and then set environ in the child before calling execvp.
    603  1.1  mrg      This clobbers the parent's environ so we need to restore it.
    604  1.1  mrg      It would be nice to use one of the exec* functions that takes an
    605  1.1  mrg      environment as a parameter, but that may have portability issues.  */
    606  1.1  mrg   char **save_environ = environ;
    607  1.1  mrg 
    608  1.1  mrg   sleep_interval = 1;
    609  1.1  mrg   pid = -1;
    610  1.1  mrg   for (retries = 0; retries < 4; ++retries)
    611  1.1  mrg     {
    612  1.1  mrg       pid = vfork ();
    613  1.1  mrg       if (pid >= 0)
    614  1.1  mrg 	break;
    615  1.1  mrg       sleep (sleep_interval);
    616  1.1  mrg       sleep_interval *= 2;
    617  1.1  mrg     }
    618  1.1  mrg 
    619  1.1  mrg   switch (pid)
    620  1.1  mrg     {
    621  1.1  mrg     case -1:
    622  1.1  mrg       *err = errno;
    623  1.1  mrg       *errmsg = VFORK_STRING;
    624  1.1  mrg       return (pid_t) -1;
    625  1.1  mrg 
    626  1.1  mrg     case 0:
    627  1.1  mrg       /* Child process.  */
    628  1.1  mrg       if (in != STDIN_FILE_NO)
    629  1.1  mrg 	{
    630  1.1  mrg 	  if (dup2 (in, STDIN_FILE_NO) < 0)
    631  1.1  mrg 	    pex_child_error (obj, executable, "dup2", errno);
    632  1.1  mrg 	  if (close (in) < 0)
    633  1.1  mrg 	    pex_child_error (obj, executable, "close", errno);
    634  1.1  mrg 	}
    635  1.1  mrg       if (out != STDOUT_FILE_NO)
    636  1.1  mrg 	{
    637  1.1  mrg 	  if (dup2 (out, STDOUT_FILE_NO) < 0)
    638  1.1  mrg 	    pex_child_error (obj, executable, "dup2", errno);
    639  1.1  mrg 	  if (close (out) < 0)
    640  1.1  mrg 	    pex_child_error (obj, executable, "close", errno);
    641  1.1  mrg 	}
    642  1.1  mrg       if (errdes != STDERR_FILE_NO)
    643  1.1  mrg 	{
    644  1.1  mrg 	  if (dup2 (errdes, STDERR_FILE_NO) < 0)
    645  1.1  mrg 	    pex_child_error (obj, executable, "dup2", errno);
    646  1.1  mrg 	  if (close (errdes) < 0)
    647  1.1  mrg 	    pex_child_error (obj, executable, "close", errno);
    648  1.1  mrg 	}
    649  1.1  mrg       if (toclose >= 0)
    650  1.1  mrg 	{
    651  1.1  mrg 	  if (close (toclose) < 0)
    652  1.1  mrg 	    pex_child_error (obj, executable, "close", errno);
    653  1.1  mrg 	}
    654  1.1  mrg       if ((flags & PEX_STDERR_TO_STDOUT) != 0)
    655  1.1  mrg 	{
    656  1.1  mrg 	  if (dup2 (STDOUT_FILE_NO, STDERR_FILE_NO) < 0)
    657  1.1  mrg 	    pex_child_error (obj, executable, "dup2", errno);
    658  1.1  mrg 	}
    659  1.1  mrg 
    660  1.1  mrg       if (env)
    661  1.1  mrg 	{
    662  1.1  mrg 	  /* NOTE: In a standard vfork implementation this clobbers the
    663  1.1  mrg 	     parent's copy of environ "too" (in reality there's only one copy).
    664  1.1  mrg 	     This is ok as we restore it below.  */
    665  1.1  mrg 	  environ = (char**) env;
    666  1.1  mrg 	}
    667  1.1  mrg 
    668  1.1  mrg       if ((flags & PEX_SEARCH) != 0)
    669  1.1  mrg 	{
    670  1.1  mrg 	  execvp (executable, to_ptr32 (argv));
    671  1.1  mrg 	  pex_child_error (obj, executable, "execvp", errno);
    672  1.1  mrg 	}
    673  1.1  mrg       else
    674  1.1  mrg 	{
    675  1.1  mrg 	  execv (executable, to_ptr32 (argv));
    676  1.1  mrg 	  pex_child_error (obj, executable, "execv", errno);
    677  1.1  mrg 	}
    678  1.1  mrg 
    679  1.1  mrg       /* NOTREACHED */
    680  1.1  mrg       return (pid_t) -1;
    681  1.1  mrg 
    682  1.1  mrg     default:
    683  1.1  mrg       /* Parent process.  */
    684  1.1  mrg 
    685  1.1  mrg       /* Restore environ.
    686  1.1  mrg 	 Note that the parent either doesn't run until the child execs/exits
    687  1.1  mrg 	 (standard vfork behaviour), or if it does run then vfork is behaving
    688  1.1  mrg 	 more like fork.  In either case we needn't worry about clobbering
    689  1.1  mrg 	 the child's copy of environ.  */
    690  1.1  mrg       environ = save_environ;
    691  1.1  mrg 
    692  1.1  mrg       if (in != STDIN_FILE_NO)
    693  1.1  mrg 	{
    694  1.1  mrg 	  if (close (in) < 0)
    695  1.1  mrg 	    {
    696  1.1  mrg 	      *err = errno;
    697  1.1  mrg 	      *errmsg = "close";
    698  1.1  mrg 	      return (pid_t) -1;
    699  1.1  mrg 	    }
    700  1.1  mrg 	}
    701  1.1  mrg       if (out != STDOUT_FILE_NO)
    702  1.1  mrg 	{
    703  1.1  mrg 	  if (close (out) < 0)
    704  1.1  mrg 	    {
    705  1.1  mrg 	      *err = errno;
    706  1.1  mrg 	      *errmsg = "close";
    707  1.1  mrg 	      return (pid_t) -1;
    708  1.1  mrg 	    }
    709  1.1  mrg 	}
    710  1.1  mrg       if (errdes != STDERR_FILE_NO)
    711  1.1  mrg 	{
    712  1.1  mrg 	  if (close (errdes) < 0)
    713  1.1  mrg 	    {
    714  1.1  mrg 	      *err = errno;
    715  1.1  mrg 	      *errmsg = "close";
    716  1.1  mrg 	      return (pid_t) -1;
    717  1.1  mrg 	    }
    718  1.1  mrg 	}
    719  1.1  mrg 
    720  1.1  mrg       return pid;
    721  1.1  mrg     }
    722  1.1  mrg }
    723  1.3  mrg #endif /* SPAWN */
    724  1.1  mrg 
    725  1.1  mrg /* Wait for a child process to complete.  */
    726  1.1  mrg 
    727  1.1  mrg static int
    728  1.1  mrg pex_unix_wait (struct pex_obj *obj, pid_t pid, int *status,
    729  1.1  mrg 	       struct pex_time *time, int done, const char **errmsg,
    730  1.1  mrg 	       int *err)
    731  1.1  mrg {
    732  1.1  mrg   /* If we are cleaning up when the caller didn't retrieve process
    733  1.1  mrg      status for some reason, encourage the process to go away.  */
    734  1.1  mrg   if (done)
    735  1.1  mrg     kill (pid, SIGTERM);
    736  1.1  mrg 
    737  1.1  mrg   if (pex_wait (obj, pid, status, time) < 0)
    738  1.1  mrg     {
    739  1.1  mrg       *err = errno;
    740  1.1  mrg       *errmsg = "wait";
    741  1.1  mrg       return -1;
    742  1.1  mrg     }
    743  1.1  mrg 
    744  1.1  mrg   return 0;
    745  1.1  mrg }
    746  1.1  mrg 
    747  1.1  mrg /* Create a pipe.  */
    748  1.1  mrg 
    749  1.1  mrg static int
    750  1.1  mrg pex_unix_pipe (struct pex_obj *obj ATTRIBUTE_UNUSED, int *p,
    751  1.1  mrg 	       int binary ATTRIBUTE_UNUSED)
    752  1.1  mrg {
    753  1.1  mrg   return pipe (p);
    754  1.1  mrg }
    755  1.1  mrg 
    756  1.1  mrg /* Get a FILE pointer to read from a file descriptor.  */
    757  1.1  mrg 
    758  1.1  mrg static FILE *
    759  1.1  mrg pex_unix_fdopenr (struct pex_obj *obj ATTRIBUTE_UNUSED, int fd,
    760  1.1  mrg 		  int binary ATTRIBUTE_UNUSED)
    761  1.1  mrg {
    762  1.1  mrg   return fdopen (fd, "r");
    763  1.1  mrg }
    764  1.1  mrg 
    765  1.1  mrg static FILE *
    766  1.1  mrg pex_unix_fdopenw (struct pex_obj *obj ATTRIBUTE_UNUSED, int fd,
    767  1.1  mrg 		  int binary ATTRIBUTE_UNUSED)
    768  1.1  mrg {
    769  1.1  mrg   if (fcntl (fd, F_SETFD, FD_CLOEXEC) < 0)
    770  1.1  mrg     return NULL;
    771  1.1  mrg   return fdopen (fd, "w");
    772  1.1  mrg }
    773  1.1  mrg 
    774  1.1  mrg static void
    775  1.1  mrg pex_unix_cleanup (struct pex_obj *obj ATTRIBUTE_UNUSED)
    776  1.1  mrg {
    777  1.1  mrg #if !defined (HAVE_WAIT4) && !defined (HAVE_WAITPID)
    778  1.1  mrg   while (obj->sysdep != NULL)
    779  1.1  mrg     {
    780  1.1  mrg       struct status_list *this;
    781  1.1  mrg       struct status_list *next;
    782  1.1  mrg 
    783  1.1  mrg       this = (struct status_list *) obj->sysdep;
    784  1.1  mrg       next = this->next;
    785  1.1  mrg       free (this);
    786  1.1  mrg       obj->sysdep = (void *) next;
    787  1.1  mrg     }
    788  1.1  mrg #endif
    789  1.1  mrg }
    790