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, 1998 Free Software Foundation, Inc.
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.
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.
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. */
21 /* This file exports two functions: pexecute and pwait. */
23 /* This file lives in at least two places: libiberty and gcc.
24 Don't change one without the other. */
35 #define ISSPACE (x) isspace(x)
36 #ifdef HAVE_SYS_WAIT_H
40 #ifdef vfork /* Autoconf may define this to fork for us. */
41 # define VFORK_STRING "fork"
43 # define VFORK_STRING "vfork"
49 #define vfork() (decc$$alloc_vfork_blocks() >= 0 ? \
50 lib$get_current_invo_context(decc$$get_vfork_jmpbuf()) : -1)
53 #include "libiberty.h"
55 /* stdin file number. */
56 #define STDIN_FILE_NO 0
58 /* stdout file number. */
59 #define STDOUT_FILE_NO 1
61 /* value of `pipe': port index for reading. */
64 /* value of `pipe': port index for writing. */
67 static char *install_error_msg
= "installation problem, cannot exec `%s'";
69 /* pexecute: execute a program.
71 PROGRAM and ARGV are the arguments to execv/execvp.
73 THIS_PNAME is name of the calling program (i.e. argv[0]).
75 TEMP_BASE is the path name, sans suffix, of a temporary file to use
76 if needed. This is currently only needed for MSDOS ports that don't use
77 GO32 (do any still exist?). Ports that don't need it can pass NULL.
79 (FLAGS & PEXECUTE_SEARCH) is non-zero if $PATH should be searched
80 (??? It's not clear that GCC passes this flag correctly).
81 (FLAGS & PEXECUTE_FIRST) is nonzero for the first process in chain.
82 (FLAGS & PEXECUTE_FIRST) is nonzero for the last process in chain.
83 FIRST_LAST could be simplified to only mark the last of a chain of processes
84 but that requires the caller to always mark the last one (and not give up
85 early if some error occurs). It's more robust to require the caller to
86 mark both ends of the chain.
88 The result is the pid on systems like Unix where we fork/exec and on systems
89 like WIN32 and OS2 where we use spawn. It is up to the caller to wait for
92 The result is the WEXITSTATUS on systems like MSDOS where we spawn and wait
95 Upon failure, ERRMSG_FMT and ERRMSG_ARG are set to the text of the error
96 message with an optional argument (if not needed, ERRMSG_ARG is set to
97 NULL), and -1 is returned. `errno' is available to the caller to use.
99 pwait: cover function for wait.
101 PID is the process id of the task to wait for.
102 STATUS is the `status' argument to wait.
103 FLAGS is currently unused (allows future enhancement without breaking
104 upward compatibility). Pass 0 for now.
106 The result is the pid of the child reaped,
107 or -1 for failure (errno says why).
109 On systems that don't support waiting for a particular child, PID is
110 ignored. On systems like MSDOS that don't really multitask pwait
111 is just a mechanism to provide a consistent interface for the caller.
113 pfinish: finish generation of script
115 pfinish is necessary for systems like MPW where a script is generated that
116 runs the requested programs.
121 /* MSDOS doesn't multitask, but for the sake of a consistent interface
122 the code behaves like it does. pexecute runs the program, tucks the
123 exit code away, and returns a "pid". pwait must be called to fetch the
128 /* For communicating information from pexecute to pwait. */
129 static int last_pid
= 0;
130 static int last_status
= 0;
131 static int last_reaped
= 0;
134 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
137 const char *this_pname
;
138 const char *temp_base
;
139 char **errmsg_fmt
, **errmsg_arg
;
148 if ((flags
& PEXECUTE_ONE
) != PEXECUTE_ONE
)
152 /* ??? What are the possible return values from spawnv? */
153 rc
= (flags
& PEXECUTE_SEARCH
? spawnvp
: spawnv
) (1, program
, argv
);
157 int i
, el
= flags
& PEXECUTE_SEARCH
? 4 : 0;
159 scmd
= (char *) xmalloc (strlen (program
) + strlen (temp_base
) + 6 + el
);
160 rf
= scmd
+ strlen(program
) + 2 + el
;
161 sprintf (scmd
, "%s%s @%s.gp", program
,
162 (flags
& PEXECUTE_SEARCH
? ".exe" : ""), temp_base
);
163 argfile
= fopen (rf
, "w");
166 int errno_save
= errno
;
169 *errmsg_fmt
= "cannot open `%s.gp'";
170 *errmsg_arg
= temp_base
;
174 for (i
=1; argv
[i
]; i
++)
177 for (cp
= argv
[i
]; *cp
; cp
++)
179 if (*cp
== '"' || *cp
== '\'' || *cp
== '\\' || ISSPACE (*cp
))
180 fputc ('\\', argfile
);
181 fputc (*cp
, argfile
);
183 fputc ('\n', argfile
);
190 int errno_save
= errno
;
199 *errmsg_fmt
= install_error_msg
;
200 *errmsg_arg
= program
;
204 /* Tuck the status away for pwait, and return a "pid". */
205 last_status
= rc
<< 8;
210 pwait (pid
, status
, flags
)
215 /* On MSDOS each pexecute must be followed by it's associated pwait. */
217 /* Called twice for the same child? */
218 || pid
== last_reaped
)
220 /* ??? ECHILD would be a better choice. Can we use it here? */
224 /* ??? Here's an opportunity to canonicalize the values in STATUS.
226 *status
= last_status
;
227 last_reaped
= last_pid
;
233 #if defined (_WIN32) && ! defined (_UWIN)
239 #define fix_argv(argvec) (argvec)
241 extern int _spawnv ();
242 extern int _spawnvp ();
244 #else /* ! __CYGWIN__ */
246 /* This is a kludge to get around the Microsoft C spawn functions' propensity
247 to remove the outermost set of double quotes from all arguments. */
255 for (i
= 1; argvec
[i
] != 0; i
++)
258 char *temp
, *newtemp
;
262 for (j
= 0; j
< len
; j
++)
266 newtemp
= xmalloc (len
+ 2);
267 strncpy (newtemp
, temp
, j
);
269 strncpy (&newtemp
[j
+1], &temp
[j
], len
-j
);
280 return (const char * const *) argvec
;
282 #endif /* __CYGWIN__ */
288 /* mingw32 headers may not define the following. */
293 # define _P_OVERLAY 2
294 # define _P_NOWAITO 3
297 # define WAIT_CHILD 0
298 # define WAIT_GRANDCHILD 1
301 /* Win32 supports pipes */
303 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
306 const char *this_pname
;
307 const char *temp_base
;
308 char **errmsg_fmt
, **errmsg_arg
;
312 int pdes
[2], org_stdin
, org_stdout
;
313 int input_desc
, output_desc
;
314 int retries
, sleep_interval
;
316 /* Pipe waiting from last process, to be used as input for the next one.
317 Value is STDIN_FILE_NO if no pipe is waiting
318 (i.e. the next command is the first of a group). */
319 static int last_pipe_input
;
321 /* If this is the first process, initialize. */
322 if (flags
& PEXECUTE_FIRST
)
323 last_pipe_input
= STDIN_FILE_NO
;
325 input_desc
= last_pipe_input
;
327 /* If this isn't the last process, make a pipe for its output,
328 and record it as waiting to be the input to the next process. */
329 if (! (flags
& PEXECUTE_LAST
))
331 if (_pipe (pdes
, 256, O_BINARY
) < 0)
333 *errmsg_fmt
= "pipe";
337 output_desc
= pdes
[WRITE_PORT
];
338 last_pipe_input
= pdes
[READ_PORT
];
343 output_desc
= STDOUT_FILE_NO
;
344 last_pipe_input
= STDIN_FILE_NO
;
347 if (input_desc
!= STDIN_FILE_NO
)
349 org_stdin
= dup (STDIN_FILE_NO
);
350 dup2 (input_desc
, STDIN_FILE_NO
);
354 if (output_desc
!= STDOUT_FILE_NO
)
356 org_stdout
= dup (STDOUT_FILE_NO
);
357 dup2 (output_desc
, STDOUT_FILE_NO
);
361 pid
= (flags
& PEXECUTE_SEARCH
? _spawnvp
: _spawnv
)
362 (_P_NOWAIT
, program
, fix_argv(argv
));
364 if (input_desc
!= STDIN_FILE_NO
)
366 dup2 (org_stdin
, STDIN_FILE_NO
);
370 if (output_desc
!= STDOUT_FILE_NO
)
372 dup2 (org_stdout
, STDOUT_FILE_NO
);
378 *errmsg_fmt
= install_error_msg
;
379 *errmsg_arg
= program
;
386 /* MS CRTDLL doesn't return enough information in status to decide if the
387 child exited due to a signal or not, rather it simply returns an
388 integer with the exit code of the child; eg., if the child exited with
389 an abort() call and didn't have a handler for SIGABRT, it simply returns
390 with status = 3. We fix the status code to conform to the usual WIF*
391 macros. Note that WIFSIGNALED will never be true under CRTDLL. */
394 pwait (pid
, status
, flags
)
400 return wait (status
);
404 pid
= _cwait (&termstat
, pid
, WAIT_CHILD
);
406 /* ??? Here's an opportunity to canonicalize the values in STATUS.
409 /* cwait returns the child process exit code in termstat.
410 A value of 3 indicates that the child caught a signal, but not
411 which one. Since only SIGABRT, SIGFPE and SIGINT do anything, we
416 *status
= (((termstat
) & 0xff) << 8);
419 #endif /* __CYGWIN__ */
422 #endif /* _WIN32 && ! _UWIN */
426 /* ??? Does OS2 have process.h? */
427 extern int spawnv ();
428 extern int spawnvp ();
431 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
434 const char *this_pname
;
435 const char *temp_base
;
436 char **errmsg_fmt
, **errmsg_arg
;
441 if ((flags
& PEXECUTE_ONE
) != PEXECUTE_ONE
)
443 /* ??? Presumably 1 == _P_NOWAIT. */
444 pid
= (flags
& PEXECUTE_SEARCH
? spawnvp
: spawnv
) (1, program
, argv
);
447 *errmsg_fmt
= install_error_msg
;
448 *errmsg_arg
= program
;
455 pwait (pid
, status
, flags
)
460 /* ??? Here's an opportunity to canonicalize the values in STATUS.
462 int pid
= wait (status
);
470 /* MPW pexecute doesn't actually run anything; instead, it writes out
471 script commands that, when run, will do the actual executing.
473 For example, in GCC's case, GCC will write out several script commands:
480 and then exit. None of the above programs will have run yet. The task
481 that called GCC will then execute the script and cause cpp,etc. to run.
482 The caller must invoke pfinish before calling exit. This adds
483 the finishing touches to the generated script. */
485 static int first_time
= 1;
488 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
491 const char *this_pname
;
492 const char *temp_base
;
493 char **errmsg_fmt
, **errmsg_arg
;
496 char tmpprogram
[255];
500 mpwify_filename (program
, tmpprogram
);
503 printf ("Set Failed 0\n");
507 fputs ("If {Failed} == 0\n", stdout
);
508 /* If being verbose, output a copy of the command. It should be
509 accurate enough and escaped enough to be "clickable". */
510 if (flags
& PEXECUTE_VERBOSE
)
512 fputs ("\tEcho ", stdout
);
513 fputc ('\'', stdout
);
514 fputs (tmpprogram
, stdout
);
515 fputc ('\'', stdout
);
517 for (i
=1; argv
[i
]; i
++)
519 fputc ('\'', stdout
);
520 /* See if we have an argument that needs fixing. */
521 if (strchr(argv
[i
], '/'))
523 tmpname
= (char *) xmalloc (256);
524 mpwify_filename (argv
[i
], tmpname
);
527 for (cp
= argv
[i
]; *cp
; cp
++)
529 /* Write an Option-d escape char in front of special chars. */
530 if (strchr("'+", *cp
))
531 fputc ('\266', stdout
);
534 fputc ('\'', stdout
);
537 fputs ("\n", stdout
);
539 fputs ("\t", stdout
);
540 fputs (tmpprogram
, stdout
);
543 for (i
=1; argv
[i
]; i
++)
545 /* See if we have an argument that needs fixing. */
546 if (strchr(argv
[i
], '/'))
548 tmpname
= (char *) xmalloc (256);
549 mpwify_filename (argv
[i
], tmpname
);
552 if (strchr (argv
[i
], ' '))
553 fputc ('\'', stdout
);
554 for (cp
= argv
[i
]; *cp
; cp
++)
556 /* Write an Option-d escape char in front of special chars. */
557 if (strchr("'+", *cp
))
558 fputc ('\266', stdout
);
561 if (strchr (argv
[i
], ' '))
562 fputc ('\'', stdout
);
566 fputs ("\n", stdout
);
568 /* Output commands that arrange to clean up and exit if a failure occurs.
569 We have to be careful to collect the status from the program that was
570 run, rather than some other script command. Also, we don't exit
571 immediately, since necessary cleanups are at the end of the script. */
572 fputs ("\tSet TmpStatus {Status}\n", stdout
);
573 fputs ("\tIf {TmpStatus} != 0\n", stdout
);
574 fputs ("\t\tSet Failed {TmpStatus}\n", stdout
);
575 fputs ("\tEnd\n", stdout
);
576 fputs ("End\n", stdout
);
578 /* We're just composing a script, can't fail here. */
583 pwait (pid
, status
, flags
)
592 /* Write out commands that will exit with the correct error code
593 if something in the script failed. */
598 printf ("\tExit \"{Failed}\"\n");
603 /* include for Unix-like environments but not for Dos-like environments */
604 #if ! defined (__MSDOS__) && ! defined (OS2) && ! defined (MPW) \
605 && ! (defined (_WIN32) && ! defined (_UWIN))
608 extern int execvp ();
611 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
614 const char *this_pname
;
615 const char *temp_base
;
616 char **errmsg_fmt
, **errmsg_arg
;
619 int (*func
)() = (flags
& PEXECUTE_SEARCH
? execvp
: execv
);
622 int input_desc
, output_desc
;
623 int retries
, sleep_interval
;
624 /* Pipe waiting from last process, to be used as input for the next one.
625 Value is STDIN_FILE_NO if no pipe is waiting
626 (i.e. the next command is the first of a group). */
627 static int last_pipe_input
;
629 /* If this is the first process, initialize. */
630 if (flags
& PEXECUTE_FIRST
)
631 last_pipe_input
= STDIN_FILE_NO
;
633 input_desc
= last_pipe_input
;
635 /* If this isn't the last process, make a pipe for its output,
636 and record it as waiting to be the input to the next process. */
637 if (! (flags
& PEXECUTE_LAST
))
641 *errmsg_fmt
= "pipe";
645 output_desc
= pdes
[WRITE_PORT
];
646 last_pipe_input
= pdes
[READ_PORT
];
651 output_desc
= STDOUT_FILE_NO
;
652 last_pipe_input
= STDIN_FILE_NO
;
655 /* Fork a subprocess; wait and retry if it fails. */
657 for (retries
= 0; retries
< 4; retries
++)
662 sleep (sleep_interval
);
670 *errmsg_fmt
= VFORK_STRING
;
676 /* Move the input and output pipes into place, if necessary. */
677 if (input_desc
!= STDIN_FILE_NO
)
679 close (STDIN_FILE_NO
);
683 if (output_desc
!= STDOUT_FILE_NO
)
685 close (STDOUT_FILE_NO
);
690 /* Close the parent's descs that aren't wanted here. */
691 if (last_pipe_input
!= STDIN_FILE_NO
)
692 close (last_pipe_input
);
694 /* Exec the program. */
695 (*func
) (program
, argv
);
697 /* Note: Calling fprintf and exit here doesn't seem right for vfork. */
698 fprintf (stderr
, "%s: ", this_pname
);
699 fprintf (stderr
, install_error_msg
, program
);
700 fprintf (stderr
, ": %s\n", xstrerror (errno
));
706 /* In the parent, after forking.
707 Close the descriptors that we made for this child. */
708 if (input_desc
!= STDIN_FILE_NO
)
710 if (output_desc
!= STDOUT_FILE_NO
)
713 /* Return child's process number. */
719 pwait (pid
, status
, flags
)
724 /* ??? Here's an opportunity to canonicalize the values in STATUS.
727 pid
= waitpid (-1, status
, 0);
734 #endif /* ! __MSDOS__ && ! OS2 && ! MPW && ! (_WIN32 && ! _UWIN) */