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. */
37 #define ISSPACE (x) isspace(x)
38 #ifdef HAVE_SYS_WAIT_H
43 #ifdef vfork /* Autoconf may define this to fork for us. */
44 # define VFORK_STRING "fork"
46 # define VFORK_STRING "vfork"
52 #define vfork() (decc$$alloc_vfork_blocks() >= 0 ? \
53 lib$get_current_invo_context(decc$$get_vfork_jmpbuf()) : -1)
56 #include "libiberty.h"
58 /* stdin file number. */
59 #define STDIN_FILE_NO 0
61 /* stdout file number. */
62 #define STDOUT_FILE_NO 1
64 /* value of `pipe': port index for reading. */
67 /* value of `pipe': port index for writing. */
70 static char *install_error_msg
= "installation problem, cannot exec `%s'";
72 /* pexecute: execute a program.
74 PROGRAM and ARGV are the arguments to execv/execvp.
76 THIS_PNAME is name of the calling program (i.e. argv[0]).
78 TEMP_BASE is the path name, sans suffix, of a temporary file to use
79 if needed. This is currently only needed for MSDOS ports that don't use
80 GO32 (do any still exist?). Ports that don't need it can pass NULL.
82 (FLAGS & PEXECUTE_SEARCH) is non-zero if $PATH should be searched
83 (??? It's not clear that GCC passes this flag correctly).
84 (FLAGS & PEXECUTE_FIRST) is nonzero for the first process in chain.
85 (FLAGS & PEXECUTE_FIRST) is nonzero for the last process in chain.
86 FIRST_LAST could be simplified to only mark the last of a chain of processes
87 but that requires the caller to always mark the last one (and not give up
88 early if some error occurs). It's more robust to require the caller to
89 mark both ends of the chain.
91 The result is the pid on systems like Unix where we fork/exec and on systems
92 like WIN32 and OS2 where we use spawn. It is up to the caller to wait for
95 The result is the WEXITSTATUS on systems like MSDOS where we spawn and wait
98 Upon failure, ERRMSG_FMT and ERRMSG_ARG are set to the text of the error
99 message with an optional argument (if not needed, ERRMSG_ARG is set to
100 NULL), and -1 is returned. `errno' is available to the caller to use.
102 pwait: cover function for wait.
104 PID is the process id of the task to wait for.
105 STATUS is the `status' argument to wait.
106 FLAGS is currently unused (allows future enhancement without breaking
107 upward compatibility). Pass 0 for now.
109 The result is the pid of the child reaped,
110 or -1 for failure (errno says why).
112 On systems that don't support waiting for a particular child, PID is
113 ignored. On systems like MSDOS that don't really multitask pwait
114 is just a mechanism to provide a consistent interface for the caller.
116 pfinish: finish generation of script
118 pfinish is necessary for systems like MPW where a script is generated that
119 runs the requested programs.
124 /* MSDOS doesn't multitask, but for the sake of a consistent interface
125 the code behaves like it does. pexecute runs the program, tucks the
126 exit code away, and returns a "pid". pwait must be called to fetch the
131 /* For communicating information from pexecute to pwait. */
132 static int last_pid
= 0;
133 static int last_status
= 0;
134 static int last_reaped
= 0;
137 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
140 const char *this_pname
;
141 const char *temp_base
;
142 char **errmsg_fmt
, **errmsg_arg
;
151 if ((flags
& PEXECUTE_ONE
) != PEXECUTE_ONE
)
155 /* ??? What are the possible return values from spawnv? */
156 rc
= (flags
& PEXECUTE_SEARCH
? spawnvp
: spawnv
) (1, program
, argv
);
160 int i
, el
= flags
& PEXECUTE_SEARCH
? 4 : 0;
162 scmd
= (char *) xmalloc (strlen (program
) + strlen (temp_base
) + 6 + el
);
163 rf
= scmd
+ strlen(program
) + 2 + el
;
164 sprintf (scmd
, "%s%s @%s.gp", program
,
165 (flags
& PEXECUTE_SEARCH
? ".exe" : ""), temp_base
);
166 argfile
= fopen (rf
, "w");
169 int errno_save
= errno
;
172 *errmsg_fmt
= "cannot open `%s.gp'";
173 *errmsg_arg
= temp_base
;
177 for (i
=1; argv
[i
]; i
++)
180 for (cp
= argv
[i
]; *cp
; cp
++)
182 if (*cp
== '"' || *cp
== '\'' || *cp
== '\\' || ISSPACE (*cp
))
183 fputc ('\\', argfile
);
184 fputc (*cp
, argfile
);
186 fputc ('\n', argfile
);
193 int errno_save
= errno
;
202 *errmsg_fmt
= install_error_msg
;
203 *errmsg_arg
= program
;
207 /* Tuck the status away for pwait, and return a "pid". */
208 last_status
= rc
<< 8;
213 pwait (pid
, status
, flags
)
218 /* On MSDOS each pexecute must be followed by it's associated pwait. */
220 /* Called twice for the same child? */
221 || pid
== last_reaped
)
223 /* ??? ECHILD would be a better choice. Can we use it here? */
227 /* ??? Here's an opportunity to canonicalize the values in STATUS.
229 *status
= last_status
;
230 last_reaped
= last_pid
;
242 #define fix_argv(argvec) (argvec)
244 extern int _spawnv ();
245 extern int _spawnvp ();
247 #else /* ! __CYGWIN32__ */
249 /* This is a kludge to get around the Microsoft C spawn functions' propensity
250 to remove the outermost set of double quotes from all arguments. */
258 for (i
= 1; argvec
[i
] != 0; i
++)
261 char *temp
, *newtemp
;
265 for (j
= 0; j
< len
; j
++)
269 newtemp
= xmalloc (len
+ 2);
270 strncpy (newtemp
, temp
, j
);
272 strncpy (&newtemp
[j
+1], &temp
[j
], len
-j
);
283 return (const char * const *) argvec
;
285 #endif /* __CYGWIN32__ */
291 /* mingw32 headers may not define the following. */
296 # define _P_OVERLAY 2
297 # define _P_NOWAITO 3
300 # define WAIT_CHILD 0
301 # define WAIT_GRANDCHILD 1
304 /* Win32 supports pipes */
306 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
309 const char *this_pname
;
310 const char *temp_base
;
311 char **errmsg_fmt
, **errmsg_arg
;
315 int pdes
[2], org_stdin
, org_stdout
;
316 int input_desc
, output_desc
;
317 int retries
, sleep_interval
;
319 /* Pipe waiting from last process, to be used as input for the next one.
320 Value is STDIN_FILE_NO if no pipe is waiting
321 (i.e. the next command is the first of a group). */
322 static int last_pipe_input
;
324 /* If this is the first process, initialize. */
325 if (flags
& PEXECUTE_FIRST
)
326 last_pipe_input
= STDIN_FILE_NO
;
328 input_desc
= last_pipe_input
;
330 /* If this isn't the last process, make a pipe for its output,
331 and record it as waiting to be the input to the next process. */
332 if (! (flags
& PEXECUTE_LAST
))
334 if (_pipe (pdes
, 256, O_BINARY
) < 0)
336 *errmsg_fmt
= "pipe";
340 output_desc
= pdes
[WRITE_PORT
];
341 last_pipe_input
= pdes
[READ_PORT
];
346 output_desc
= STDOUT_FILE_NO
;
347 last_pipe_input
= STDIN_FILE_NO
;
350 if (input_desc
!= STDIN_FILE_NO
)
352 org_stdin
= dup (STDIN_FILE_NO
);
353 dup2 (input_desc
, STDIN_FILE_NO
);
357 if (output_desc
!= STDOUT_FILE_NO
)
359 org_stdout
= dup (STDOUT_FILE_NO
);
360 dup2 (output_desc
, STDOUT_FILE_NO
);
364 pid
= (flags
& PEXECUTE_SEARCH
? _spawnvp
: _spawnv
)
365 (_P_NOWAIT
, program
, fix_argv(argv
));
367 if (input_desc
!= STDIN_FILE_NO
)
369 dup2 (org_stdin
, STDIN_FILE_NO
);
373 if (output_desc
!= STDOUT_FILE_NO
)
375 dup2 (org_stdout
, STDOUT_FILE_NO
);
381 *errmsg_fmt
= install_error_msg
;
382 *errmsg_arg
= program
;
389 /* MS CRTDLL doesn't return enough information in status to decide if the
390 child exited due to a signal or not, rather it simply returns an
391 integer with the exit code of the child; eg., if the child exited with
392 an abort() call and didn't have a handler for SIGABRT, it simply returns
393 with status = 3. We fix the status code to conform to the usual WIF*
394 macros. Note that WIFSIGNALED will never be true under CRTDLL. */
397 pwait (pid
, status
, flags
)
403 return wait (status
);
407 pid
= _cwait (&termstat
, pid
, WAIT_CHILD
);
409 /* ??? Here's an opportunity to canonicalize the values in STATUS.
412 /* cwait returns the child process exit code in termstat.
413 A value of 3 indicates that the child caught a signal, but not
414 which one. Since only SIGABRT, SIGFPE and SIGINT do anything, we
419 *status
= (((termstat
) & 0xff) << 8);
422 #endif /* __CYGWIN32__ */
429 /* ??? Does OS2 have process.h? */
430 extern int spawnv ();
431 extern int spawnvp ();
434 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
437 const char *this_pname
;
438 const char *temp_base
;
439 char **errmsg_fmt
, **errmsg_arg
;
444 if ((flags
& PEXECUTE_ONE
) != PEXECUTE_ONE
)
446 /* ??? Presumably 1 == _P_NOWAIT. */
447 pid
= (flags
& PEXECUTE_SEARCH
? spawnvp
: spawnv
) (1, program
, argv
);
450 *errmsg_fmt
= install_error_msg
;
451 *errmsg_arg
= program
;
458 pwait (pid
, status
, flags
)
463 /* ??? Here's an opportunity to canonicalize the values in STATUS.
465 int pid
= wait (status
);
473 /* MPW pexecute doesn't actually run anything; instead, it writes out
474 script commands that, when run, will do the actual executing.
476 For example, in GCC's case, GCC will write out several script commands:
483 and then exit. None of the above programs will have run yet. The task
484 that called GCC will then execute the script and cause cpp,etc. to run.
485 The caller must invoke pfinish before calling exit. This adds
486 the finishing touches to the generated script. */
488 static int first_time
= 1;
491 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
494 const char *this_pname
;
495 const char *temp_base
;
496 char **errmsg_fmt
, **errmsg_arg
;
499 char tmpprogram
[255];
503 mpwify_filename (program
, tmpprogram
);
506 printf ("Set Failed 0\n");
510 fputs ("If {Failed} == 0\n", stdout
);
511 /* If being verbose, output a copy of the command. It should be
512 accurate enough and escaped enough to be "clickable". */
513 if (flags
& PEXECUTE_VERBOSE
)
515 fputs ("\tEcho ", stdout
);
516 fputc ('\'', stdout
);
517 fputs (tmpprogram
, stdout
);
518 fputc ('\'', stdout
);
520 for (i
=1; argv
[i
]; i
++)
522 fputc ('\'', stdout
);
523 /* See if we have an argument that needs fixing. */
524 if (strchr(argv
[i
], '/'))
526 tmpname
= (char *) xmalloc (256);
527 mpwify_filename (argv
[i
], tmpname
);
530 for (cp
= argv
[i
]; *cp
; cp
++)
532 /* Write an Option-d escape char in front of special chars. */
533 if (strchr("'+", *cp
))
534 fputc ('\266', stdout
);
537 fputc ('\'', stdout
);
540 fputs ("\n", stdout
);
542 fputs ("\t", stdout
);
543 fputs (tmpprogram
, stdout
);
546 for (i
=1; argv
[i
]; i
++)
548 /* See if we have an argument that needs fixing. */
549 if (strchr(argv
[i
], '/'))
551 tmpname
= (char *) xmalloc (256);
552 mpwify_filename (argv
[i
], tmpname
);
555 if (strchr (argv
[i
], ' '))
556 fputc ('\'', stdout
);
557 for (cp
= argv
[i
]; *cp
; cp
++)
559 /* Write an Option-d escape char in front of special chars. */
560 if (strchr("'+", *cp
))
561 fputc ('\266', stdout
);
564 if (strchr (argv
[i
], ' '))
565 fputc ('\'', stdout
);
569 fputs ("\n", stdout
);
571 /* Output commands that arrange to clean up and exit if a failure occurs.
572 We have to be careful to collect the status from the program that was
573 run, rather than some other script command. Also, we don't exit
574 immediately, since necessary cleanups are at the end of the script. */
575 fputs ("\tSet TmpStatus {Status}\n", stdout
);
576 fputs ("\tIf {TmpStatus} != 0\n", stdout
);
577 fputs ("\t\tSet Failed {TmpStatus}\n", stdout
);
578 fputs ("\tEnd\n", stdout
);
579 fputs ("End\n", stdout
);
581 /* We're just composing a script, can't fail here. */
586 pwait (pid
, status
, flags
)
595 /* Write out commands that will exit with the correct error code
596 if something in the script failed. */
601 printf ("\tExit \"{Failed}\"\n");
606 /* include for Unix-like environments but not for Dos-like environments */
607 #if ! defined (__MSDOS__) && ! defined (OS2) && ! defined (MPW) \
608 && ! defined (_WIN32)
611 extern int execvp ();
613 extern char * my_strerror
PROTO ((int));
617 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
620 const char *this_pname
;
621 const char *temp_base
;
622 char **errmsg_fmt
, **errmsg_arg
;
625 int (*func
)() = (flags
& PEXECUTE_SEARCH
? execvp
: execv
);
628 int input_desc
, output_desc
;
629 int retries
, sleep_interval
;
630 /* Pipe waiting from last process, to be used as input for the next one.
631 Value is STDIN_FILE_NO if no pipe is waiting
632 (i.e. the next command is the first of a group). */
633 static int last_pipe_input
;
635 /* If this is the first process, initialize. */
636 if (flags
& PEXECUTE_FIRST
)
637 last_pipe_input
= STDIN_FILE_NO
;
639 input_desc
= last_pipe_input
;
641 /* If this isn't the last process, make a pipe for its output,
642 and record it as waiting to be the input to the next process. */
643 if (! (flags
& PEXECUTE_LAST
))
647 *errmsg_fmt
= "pipe";
651 output_desc
= pdes
[WRITE_PORT
];
652 last_pipe_input
= pdes
[READ_PORT
];
657 output_desc
= STDOUT_FILE_NO
;
658 last_pipe_input
= STDIN_FILE_NO
;
661 /* Fork a subprocess; wait and retry if it fails. */
663 for (retries
= 0; retries
< 4; retries
++)
668 sleep (sleep_interval
);
676 *errmsg_fmt
= VFORK_STRING
;
682 /* Move the input and output pipes into place, if necessary. */
683 if (input_desc
!= STDIN_FILE_NO
)
685 close (STDIN_FILE_NO
);
689 if (output_desc
!= STDOUT_FILE_NO
)
691 close (STDOUT_FILE_NO
);
696 /* Close the parent's descs that aren't wanted here. */
697 if (last_pipe_input
!= STDIN_FILE_NO
)
698 close (last_pipe_input
);
700 /* Exec the program. */
701 (*func
) (program
, argv
);
703 /* Note: Calling fprintf and exit here doesn't seem right for vfork. */
704 fprintf (stderr
, "%s: ", this_pname
);
705 fprintf (stderr
, install_error_msg
, program
);
707 fprintf (stderr
, ": %s\n", my_strerror (errno
));
709 fprintf (stderr
, ": %s\n", xstrerror (errno
));
716 /* In the parent, after forking.
717 Close the descriptors that we made for this child. */
718 if (input_desc
!= STDIN_FILE_NO
)
720 if (output_desc
!= STDOUT_FILE_NO
)
723 /* Return child's process number. */
729 pwait (pid
, status
, flags
)
734 /* ??? Here's an opportunity to canonicalize the values in STATUS.
737 pid
= waitpid (-1, status
, 0);
744 #endif /* ! __MSDOS__ && ! OS2 && ! MPW && ! _WIN32 */