home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1999 March / PCWK3A99.iso / Linux / DDD331 / DDD-3_1_.000 / DDD-3_1_ / ddd-3.1.1 / libiberty / pexecute.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-30  |  18.9 KB  |  765 lines

  1. /* Utilities to execute a program in a subprocess (possibly linked by pipes
  2.    with other subprocesses), and wait for it.
  3.    Copyright (C) 1996, 1997 Free Software Foundation, Inc.
  4.  
  5. This file is part of the libiberty library.
  6. Libiberty is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Library General Public
  8. License as published by the Free Software Foundation; either
  9. version 2 of the License, or (at your option) any later version.
  10.  
  11. Libiberty is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14. Library General Public License for more details.
  15.  
  16. You should have received a copy of the GNU Library General Public
  17. License along with libiberty; see the file COPYING.LIB.  If not,
  18. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. Boston, MA 02111-1307, USA.  */
  20.  
  21. /* This file exports two functions: pexecute and pwait.  */
  22.  
  23. /* This file lives in at least two places: libiberty and gcc.
  24.    Don't change one without the other.  */
  25.  
  26. #ifdef IN_GCC
  27. #include "config.h"
  28. #endif
  29.  
  30. #include <stdio.h>
  31. #include <errno.h>
  32.  
  33. #ifdef IN_GCC
  34. #include "gansidecl.h"
  35. /* ??? Need to find a suitable header file.  */
  36. #define PEXECUTE_FIRST   1
  37. #define PEXECUTE_LAST    2
  38. #define PEXECUTE_ONE     (PEXECUTE_FIRST + PEXECUTE_LAST)
  39. #define PEXECUTE_SEARCH  4
  40. #define PEXECUTE_VERBOSE 8
  41. #else
  42. #include "libiberty.h"
  43. #endif
  44.  
  45. /* stdin file number.  */
  46. #define STDIN_FILE_NO 0
  47.  
  48. /* stdout file number.  */
  49. #define STDOUT_FILE_NO 1
  50.  
  51. /* value of `pipe': port index for reading.  */
  52. #define READ_PORT 0
  53.  
  54. /* value of `pipe': port index for writing.  */
  55. #define WRITE_PORT 1
  56.  
  57. static char *install_error_msg = "installation problem, cannot exec `%s'";
  58.  
  59. /* pexecute: execute a program.
  60.  
  61.    PROGRAM and ARGV are the arguments to execv/execvp.
  62.  
  63.    THIS_PNAME is name of the calling program (i.e. argv[0]).
  64.  
  65.    TEMP_BASE is the path name, sans suffix, of a temporary file to use
  66.    if needed.  This is currently only needed for MSDOS ports that don't use
  67.    GO32 (do any still exist?).  Ports that don't need it can pass NULL.
  68.  
  69.    (FLAGS & PEXECUTE_SEARCH) is non-zero if $PATH should be searched
  70.    (??? It's not clear that GCC passes this flag correctly).
  71.    (FLAGS & PEXECUTE_FIRST) is nonzero for the first process in chain.
  72.    (FLAGS & PEXECUTE_FIRST) is nonzero for the last process in chain.
  73.    FIRST_LAST could be simplified to only mark the last of a chain of processes
  74.    but that requires the caller to always mark the last one (and not give up
  75.    early if some error occurs).  It's more robust to require the caller to
  76.    mark both ends of the chain.
  77.  
  78.    The result is the pid on systems like Unix where we fork/exec and on systems
  79.    like WIN32 and OS2 where we use spawn.  It is up to the caller to wait for
  80.    the child.
  81.  
  82.    The result is the WEXITSTATUS on systems like MSDOS where we spawn and wait
  83.    for the child here.
  84.  
  85.    Upon failure, ERRMSG_FMT and ERRMSG_ARG are set to the text of the error
  86.    message with an optional argument (if not needed, ERRMSG_ARG is set to
  87.    NULL), and -1 is returned.  `errno' is available to the caller to use.
  88.  
  89.    pwait: cover function for wait.
  90.  
  91.    PID is the process id of the task to wait for.
  92.    STATUS is the `status' argument to wait.
  93.    FLAGS is currently unused (allows future enhancement without breaking
  94.    upward compatibility).  Pass 0 for now.
  95.  
  96.    The result is the pid of the child reaped,
  97.    or -1 for failure (errno says why).
  98.  
  99.    On systems that don't support waiting for a particular child, PID is
  100.    ignored.  On systems like MSDOS that don't really multitask pwait
  101.    is just a mechanism to provide a consistent interface for the caller.
  102.  
  103.    pfinish: finish generation of script
  104.  
  105.    pfinish is necessary for systems like MPW where a script is generated that
  106.    runs the requested programs.
  107. */
  108.  
  109. #ifdef __MSDOS__
  110.  
  111. /* MSDOS doesn't multitask, but for the sake of a consistent interface
  112.    the code behaves like it does.  pexecute runs the program, tucks the
  113.    exit code away, and returns a "pid".  pwait must be called to fetch the
  114.    exit code.  */
  115.  
  116. #include <process.h>
  117.  
  118. /* For communicating information from pexecute to pwait.  */
  119. static int last_pid = 0;
  120. static int last_status = 0;
  121. static int last_reaped = 0;
  122.  
  123. int
  124. pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
  125.      const char *program;
  126.      char * const *argv;
  127.      const char *this_pname;
  128.      const char *temp_base;
  129.      char **errmsg_fmt, **errmsg_arg;
  130.      int flags;
  131. {
  132.   int rc;
  133.  
  134.   last_pid++;
  135.   if (last_pid < 0)
  136.     last_pid = 1;
  137.  
  138.   if ((flags & PEXECUTE_ONE) != PEXECUTE_ONE)
  139.     abort ();
  140.  
  141. #ifdef __GO32__
  142.   /* ??? What are the possible return values from spawnv?  */
  143.   rc = (flags & PEXECUTE_SEARCH ? spawnvp : spawnv) (1, program, argv);
  144. #else
  145.   char *scmd, *rf;
  146.   FILE *argfile;
  147.   int i, el = flags & PEXECUTE_SEARCH ? 4 : 0;
  148.  
  149.   scmd = (char *) xmalloc (strlen (program) + strlen (temp_base) + 6 + el);
  150.   rf = scmd + strlen(program) + 2 + el;
  151.   sprintf (scmd, "%s%s @%s.gp", program,
  152.        (flags & PEXECUTE_SEARCH ? ".exe" : ""), temp_base);
  153.   argfile = fopen (rf, "w");
  154.   if (argfile == 0)
  155.     {
  156.       int errno_save = errno;
  157.       free (scmd);
  158.       errno = errno_save;
  159.       *errmsg_fmt = "cannot open `%s.gp'";
  160.       *errmsg_arg = temp_base;
  161.       return -1;
  162.     }
  163.  
  164.   for (i=1; argv[i]; i++)
  165.     {
  166.       char *cp;
  167.       for (cp = argv[i]; *cp; cp++)
  168.     {
  169.       if (*cp == '"' || *cp == '\'' || *cp == '\\' || isspace (*cp))
  170.         fputc ('\\', argfile);
  171.       fputc (*cp, argfile);
  172.     }
  173.       fputc ('\n', argfile);
  174.     }
  175.   fclose (argfile);
  176.  
  177.   rc = system (scmd);
  178.  
  179.   {
  180.     int errno_save = errno;
  181.     remove (rf);
  182.     free (scmd);
  183.     errno = errno_save;
  184.   }
  185. #endif
  186.  
  187.   if (rc == -1)
  188.     {
  189.       *errmsg_fmt = install_error_msg;
  190.       *errmsg_arg = program;
  191.       return -1;
  192.     }
  193.  
  194.   /* Tuck the status away for pwait, and return a "pid".  */
  195.   last_status = rc << 8;
  196.   return last_pid;
  197. }
  198.  
  199. int
  200. pwait (pid, status, flags)
  201.      int pid;
  202.      int *status;
  203.      int flags;
  204. {
  205.   /* On MSDOS each pexecute must be followed by it's associated pwait.  */
  206.   if (pid != last_pid
  207.       /* Called twice for the same child?  */
  208.       || pid == last_reaped)
  209.     {
  210.       /* ??? ECHILD would be a better choice.  Can we use it here?  */
  211.       errno = EINVAL;
  212.       return -1;
  213.     }
  214.   /* ??? Here's an opportunity to canonicalize the values in STATUS.
  215.      Needed?  */
  216.   *status = last_status;
  217.   last_reaped = last_pid;
  218.   return last_pid;
  219. }
  220.  
  221. #endif /* MSDOS */
  222.  
  223. #if defined (_WIN32)
  224.  
  225. #include <process.h>
  226.  
  227. #ifdef __CYGWIN32__
  228.  
  229. #define fix_argv(argvec) (argvec)
  230.  
  231. extern int _spawnv ();
  232. extern int _spawnvp ();
  233.  
  234. int
  235. pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
  236.      const char *program;
  237.      char * const *argv;
  238.      const char *this_pname;
  239.      const char *temp_base;
  240.      char **errmsg_fmt, **errmsg_arg;
  241.      int flags;
  242. {
  243.   int pid;
  244.  
  245.   if ((flags & PEXECUTE_ONE) != PEXECUTE_ONE)
  246.     abort ();
  247.   pid = (flags & PEXECUTE_SEARCH ? _spawnvp : _spawnv)
  248.     (_P_NOWAIT, program, fix_argv(argv));
  249.   if (pid == -1)
  250.     {
  251.       *errmsg_fmt = install_error_msg;
  252.       *errmsg_arg = program;
  253.       return -1;
  254.     }
  255.   return pid;
  256. }
  257.  
  258. int
  259. pwait (pid, status, flags)
  260.      int pid;
  261.      int *status;
  262.      int flags;
  263. {
  264.   /* ??? Here's an opportunity to canonicalize the values in STATUS.
  265.      Needed?  */
  266.   return cwait (status, pid, WAIT_CHILD);
  267. }
  268.  
  269. #else /* ! __CYGWIN32__ */
  270.  
  271. /* This is a kludge to get around the Microsoft C spawn functions' propensity
  272.    to remove the outermost set of double quotes from all arguments.  */
  273.  
  274. const char * const *
  275. fix_argv (argvec)
  276.      char **argvec;
  277. {
  278.   int i;
  279.  
  280.   for (i = 1; argvec[i] != 0; i++)
  281.     {
  282.       int len, j;
  283.       char *temp, *newtemp;
  284.  
  285.       temp = argvec[i];
  286.       len = strlen (temp);
  287.       for (j = 0; j < len; j++)
  288.         {
  289.           if (temp[j] == '"')
  290.             {
  291.               newtemp = xmalloc (len + 2);
  292.               strncpy (newtemp, temp, j);
  293.               newtemp [j] = '\\';
  294.               strncpy (&newtemp [j+1], &temp [j], len-j);
  295.               newtemp [len+1] = 0;
  296.               temp = newtemp;
  297.               len++;
  298.               j++;
  299.             }
  300.         }
  301.  
  302.         argvec[i] = temp;
  303.       }
  304.  
  305.   return (const char * const *) argvec;
  306. }
  307.  
  308. #include <io.h>
  309. #include <fcntl.h>
  310. #include <signal.h>
  311.  
  312. /* mingw32 headers may not define the following.  */
  313.  
  314. #ifndef _P_WAIT
  315. #  define _P_WAIT    0
  316. #  define _P_NOWAIT    1
  317. #  define _P_OVERLAY    2
  318. #  define _P_NOWAITO    3
  319. #  define _P_DETACH    4
  320.  
  321. #  define WAIT_CHILD    0
  322. #  define WAIT_GRANDCHILD    1
  323. #endif
  324.  
  325. /* Win32 supports pipes */
  326. int
  327. pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
  328.      const char *program;
  329.      char * const *argv;
  330.      const char *this_pname;
  331.      const char *temp_base;
  332.      char **errmsg_fmt, **errmsg_arg;
  333.      int flags;
  334. {
  335.   int pid;
  336.   int pdes[2], org_stdin, org_stdout;
  337.   int input_desc, output_desc;
  338.   int retries, sleep_interval;
  339.  
  340.   /* Pipe waiting from last process, to be used as input for the next one.
  341.      Value is STDIN_FILE_NO if no pipe is waiting
  342.      (i.e. the next command is the first of a group).  */
  343.   static int last_pipe_input;
  344.  
  345.   /* If this is the first process, initialize.  */
  346.   if (flags & PEXECUTE_FIRST)
  347.     last_pipe_input = STDIN_FILE_NO;
  348.  
  349.   input_desc = last_pipe_input;
  350.  
  351.   /* If this isn't the last process, make a pipe for its output,
  352.      and record it as waiting to be the input to the next process.  */
  353.   if (! (flags & PEXECUTE_LAST))
  354.     {
  355.       if (_pipe (pdes, 256, O_BINARY) < 0)
  356.     {
  357.       *errmsg_fmt = "pipe";
  358.       *errmsg_arg = NULL;
  359.       return -1;
  360.     }
  361.       output_desc = pdes[WRITE_PORT];
  362.       last_pipe_input = pdes[READ_PORT];
  363.     }
  364.   else
  365.     {
  366.       /* Last process.  */
  367.       output_desc = STDOUT_FILE_NO;
  368.       last_pipe_input = STDIN_FILE_NO;
  369.     }
  370.  
  371.   if (input_desc != STDIN_FILE_NO)
  372.     {
  373.       org_stdin = dup (STDIN_FILE_NO);
  374.       dup2 (input_desc, STDIN_FILE_NO);
  375.       close (input_desc); 
  376.     }
  377.  
  378.   if (output_desc != STDOUT_FILE_NO)
  379.     {
  380.       org_stdout = dup (STDOUT_FILE_NO);
  381.       dup2 (output_desc, STDOUT_FILE_NO);
  382.       close (output_desc);
  383.     }
  384.  
  385.   pid = (flags & PEXECUTE_SEARCH ? _spawnvp : _spawnv)
  386.     (_P_NOWAIT, program, fix_argv(argv));
  387.  
  388.   if (input_desc != STDIN_FILE_NO)
  389.     {
  390.       dup2 (org_stdin, STDIN_FILE_NO);
  391.       close (org_stdin);
  392.     }
  393.  
  394.   if (output_desc != STDOUT_FILE_NO)
  395.     {
  396.       dup2 (org_stdout, STDOUT_FILE_NO);
  397.       close (org_stdout);
  398.     }
  399.  
  400.   if (pid == -1)
  401.     {
  402.       *errmsg_fmt = install_error_msg;
  403.       *errmsg_arg = program;
  404.       return -1;
  405.     }
  406.  
  407.   return pid;
  408. }
  409.  
  410. /* MS CRTDLL doesn't return enough information in status to decide if the
  411.    child exited due to a signal or not, rather it simply returns an
  412.    integer with the exit code of the child; eg., if the child exited with 
  413.    an abort() call and didn't have a handler for SIGABRT, it simply returns
  414.    with status = 3. We fix the status code to conform to the usual WIF*
  415.    macros. Note that WIFSIGNALED will never be true under CRTDLL. */
  416.  
  417. int
  418. pwait (pid, status, flags)
  419.      int pid;
  420.      int *status;
  421.      int flags;
  422. {
  423.   int termstat;
  424.  
  425.   pid = _cwait (&termstat, pid, WAIT_CHILD);
  426.  
  427.   /* ??? Here's an opportunity to canonicalize the values in STATUS.
  428.      Needed?  */
  429.  
  430.   /* cwait returns the child process exit code in termstat.
  431.      A value of 3 indicates that the child caught a signal, but not
  432.      which one.  Since only SIGABRT, SIGFPE and SIGINT do anything, we
  433.      report SIGABRT.  */
  434.   if (termstat == 3)
  435.     *status = SIGABRT;
  436.   else
  437.     *status = (((termstat) & 0xff) << 8);
  438.  
  439.   return pid;
  440. }
  441.  
  442. #endif /* ! defined (__CYGWIN32__) */
  443.  
  444. #endif /* _WIN32 */
  445.  
  446. #ifdef OS2
  447.  
  448. /* ??? Does OS2 have process.h?  */
  449. extern int spawnv ();
  450. extern int spawnvp ();
  451.  
  452. int
  453. pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
  454.      const char *program;
  455.      char * const *argv;
  456.      const char *this_pname;
  457.      const char *temp_base;
  458.      char **errmsg_fmt, **errmsg_arg;
  459.      int flags;
  460. {
  461.   int pid;
  462.  
  463.   if ((flags & PEXECUTE_ONE) != PEXECUTE_ONE)
  464.     abort ();
  465.   /* ??? Presumably 1 == _P_NOWAIT.  */
  466.   pid = (flags & PEXECUTE_SEARCH ? spawnvp : spawnv) (1, program, argv);
  467.   if (pid == -1)
  468.     {
  469.       *errmsg_fmt = install_error_msg;
  470.       *errmsg_arg = program;
  471.       return -1;
  472.     }
  473.   return pid;
  474. }
  475.  
  476. int
  477. pwait (pid, status, flags)
  478.      int pid;
  479.      int *status;
  480.      int flags;
  481. {
  482.   /* ??? Here's an opportunity to canonicalize the values in STATUS.
  483.      Needed?  */
  484.   int pid = wait (status);
  485.   return pid;
  486. }
  487.  
  488. #endif /* OS2 */
  489.  
  490. #ifdef MPW
  491.  
  492. /* MPW pexecute doesn't actually run anything; instead, it writes out
  493.    script commands that, when run, will do the actual executing.
  494.  
  495.    For example, in GCC's case, GCC will write out several script commands:
  496.  
  497.    cpp ...
  498.    cc1 ...
  499.    as ...
  500.    ld ...
  501.  
  502.    and then exit.  None of the above programs will have run yet.  The task
  503.    that called GCC will then execute the script and cause cpp,etc. to run.
  504.    The caller must invoke pfinish before calling exit.  This adds
  505.    the finishing touches to the generated script.  */
  506.  
  507. static int first_time = 1;
  508.  
  509. int
  510. pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
  511.      const char *program;
  512.      char **argv;
  513.      const char *this_pname;
  514.      const char *temp_base;
  515.      char **errmsg_fmt, **errmsg_arg;
  516.      int flags;
  517. {
  518.   char tmpprogram[255];
  519.   char *cp, *tmpname;
  520.   int i;
  521.  
  522.   mpwify_filename (program, tmpprogram);
  523.   if (first_time)
  524.     {
  525.       printf ("Set Failed 0\n");
  526.       first_time = 0;
  527.     }
  528.  
  529.   fputs ("If {Failed} == 0\n", stdout);
  530.   /* If being verbose, output a copy of the command.  It should be
  531.      accurate enough and escaped enough to be "clickable".  */
  532.   if (flags & PEXECUTE_VERBOSE)
  533.     {
  534.       fputs ("\tEcho ", stdout);
  535.       fputc ('\'', stdout);
  536.       fputs (tmpprogram, stdout);
  537.       fputc ('\'', stdout);
  538.       fputc (' ', stdout);
  539.       for (i=1; argv[i]; i++)
  540.     {
  541.       /* We have to quote every arg, so that when the echo is
  542.          executed, the quotes are stripped and the original arg
  543.          is left. */
  544.       fputc ('\'', stdout);
  545.       for (cp = argv[i]; *cp; cp++)
  546.         {
  547.           /* Write an Option-d esc char in front of special chars.  */
  548.           if (strchr ("\"'+", *cp))
  549.         fputc ('\266', stdout);
  550.           fputc (*cp, stdout);
  551.         }
  552.       fputc ('\'', stdout);
  553.       fputc (' ', stdout);
  554.     }
  555.       fputs ("\n", stdout);
  556.     }
  557.   fputs ("\t", stdout);
  558.   fputs (tmpprogram, stdout);
  559.   fputc (' ', stdout);
  560.  
  561.   for (i=1; argv[i]; i++)
  562.     {
  563.       if (strchr (argv[i], ' '))
  564.     fputc ('\'', stdout);
  565.       for (cp = argv[i]; *cp; cp++)
  566.     {
  567.       /* Write an Option-d esc char in front of special chars.  */
  568.       if (strchr ("\"'+", *cp))
  569.         {
  570.           fputc ('\266', stdout);
  571.         }
  572.       fputc (*cp, stdout);
  573.     }
  574.       if (strchr (argv[i], ' '))
  575.     fputc ('\'', stdout);
  576.       fputc (' ', stdout);
  577.     }
  578.  
  579.   fputs ("\n", stdout);
  580.  
  581.   /* Output commands that arrange to clean up and exit if a failure occurs.
  582.      We have to be careful to collect the status from the program that was
  583.      run, rather than some other script command.  Also, we don't exit
  584.      immediately, since necessary cleanups are at the end of the script.  */
  585.   fputs ("\tSet TmpStatus {Status}\n", stdout);
  586.   fputs ("\tIf {TmpStatus} != 0\n", stdout);
  587.   fputs ("\t\tSet Failed {TmpStatus}\n", stdout);
  588.   fputs ("\tEnd\n", stdout);
  589.   fputs ("End\n", stdout);
  590.  
  591.   /* We're just composing a script, can't fail here.  */
  592.   return 0;
  593. }
  594.  
  595. int
  596. pwait (pid, status, flags)
  597.      int pid;
  598.      int *status;
  599.      int flags;
  600. {
  601.   *status = 0;
  602.   return 0;
  603. }
  604.  
  605. /* Write out commands that will exit with the correct error code
  606.    if something in the script failed.  */
  607.  
  608. void
  609. pfinish ()
  610. {
  611.   printf ("\tExit \"{Failed}\"\n");
  612. }
  613.  
  614. #endif /* MPW */
  615.  
  616. /* include for Unix-like environments but not for Dos-like environments */
  617. #if ! defined (__MSDOS__) && ! defined (OS2) && ! defined (MPW) \
  618.     && ! defined (_WIN32)
  619.  
  620. #ifdef VMS
  621. #define vfork() (decc$$alloc_vfork_blocks() >= 0 ? \
  622.                lib$get_current_invo_context(decc$$get_vfork_jmpbuf()) : -1)
  623. #else
  624. #ifdef USG
  625. #define vfork fork
  626. #endif
  627. #endif
  628.  
  629. extern int execv ();
  630. extern int execvp ();
  631.  
  632. int
  633. pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
  634.      const char *program;
  635.      char * const *argv;
  636.      const char *this_pname;
  637.      const char *temp_base;
  638.      char **errmsg_fmt, **errmsg_arg;
  639.      int flags;
  640. {
  641.   int (*func)() = (flags & PEXECUTE_SEARCH ? execvp : execv);
  642.   int pid;
  643.   int pdes[2];
  644.   int input_desc, output_desc;
  645.   int retries, sleep_interval;
  646.   /* Pipe waiting from last process, to be used as input for the next one.
  647.      Value is STDIN_FILE_NO if no pipe is waiting
  648.      (i.e. the next command is the first of a group).  */
  649.   static int last_pipe_input;
  650.  
  651.   /* If this is the first process, initialize.  */
  652.   if (flags & PEXECUTE_FIRST)
  653.     last_pipe_input = STDIN_FILE_NO;
  654.  
  655.   input_desc = last_pipe_input;
  656.  
  657.   /* If this isn't the last process, make a pipe for its output,
  658.      and record it as waiting to be the input to the next process.  */
  659.   if (! (flags & PEXECUTE_LAST))
  660.     {
  661.       if (pipe (pdes) < 0)
  662.     {
  663.       *errmsg_fmt = "pipe";
  664.       *errmsg_arg = NULL;
  665.       return -1;
  666.     }
  667.       output_desc = pdes[WRITE_PORT];
  668.       last_pipe_input = pdes[READ_PORT];
  669.     }
  670.   else
  671.     {
  672.       /* Last process.  */
  673.       output_desc = STDOUT_FILE_NO;
  674.       last_pipe_input = STDIN_FILE_NO;
  675.     }
  676.  
  677.   /* Fork a subprocess; wait and retry if it fails.  */
  678.   sleep_interval = 1;
  679.   for (retries = 0; retries < 4; retries++)
  680.     {
  681.       pid = vfork ();
  682.       if (pid >= 0)
  683.     break;
  684.       sleep (sleep_interval);
  685.       sleep_interval *= 2;
  686.     }
  687.  
  688.   switch (pid)
  689.     {
  690.     case -1:
  691.       {
  692. #ifdef vfork
  693.     *errmsg_fmt = "fork";
  694. #else
  695.     *errmsg_fmt = "vfork";
  696. #endif
  697.     *errmsg_arg = NULL;
  698.     return -1;
  699.       }
  700.  
  701.     case 0: /* child */
  702.       /* Move the input and output pipes into place, if necessary.  */
  703.       if (input_desc != STDIN_FILE_NO)
  704.     {
  705.       close (STDIN_FILE_NO);
  706.       dup (input_desc);
  707.       close (input_desc);
  708.     }
  709.       if (output_desc != STDOUT_FILE_NO)
  710.     {
  711.       close (STDOUT_FILE_NO);
  712.       dup (output_desc);
  713.       close (output_desc);
  714.     }
  715.  
  716.       /* Close the parent's descs that aren't wanted here.  */
  717.       if (last_pipe_input != STDIN_FILE_NO)
  718.     close (last_pipe_input);
  719.  
  720.       /* Exec the program.  */
  721.       (*func) (program, argv);
  722.  
  723.       /* Note: Calling fprintf and exit here doesn't seem right for vfork.  */
  724.       fprintf (stderr, "%s: ", this_pname);
  725.       fprintf (stderr, install_error_msg, program);
  726. #ifdef IN_GCC
  727.       fprintf (stderr, ": %s\n", my_strerror (errno));
  728. #else
  729.       fprintf (stderr, ": %s\n", xstrerror (errno));
  730. #endif
  731.       exit (-1);
  732.       /* NOTREACHED */
  733.       return 0;
  734.  
  735.     default:
  736.       /* In the parent, after forking.
  737.      Close the descriptors that we made for this child.  */
  738.       if (input_desc != STDIN_FILE_NO)
  739.     close (input_desc);
  740.       if (output_desc != STDOUT_FILE_NO)
  741.     close (output_desc);
  742.  
  743.       /* Return child's process number.  */
  744.       return pid;
  745.     }
  746. }
  747.  
  748. int
  749. pwait (pid, status, flags)
  750.      int pid;
  751.      int *status;
  752.      int flags;
  753. {
  754.   /* ??? Here's an opportunity to canonicalize the values in STATUS.
  755.      Needed?  */
  756. #ifdef VMS
  757.   pid = waitpid (-1, status, 0);
  758. #else
  759.   pid = wait (status);
  760. #endif
  761.   return pid;
  762. }
  763.  
  764. #endif /* ! __MSDOS__ && ! OS2 && ! MPW && ! _WIN32 */
  765.