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 ();
248 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
251 const char *this_pname
;
252 const char *temp_base
;
253 char **errmsg_fmt
, **errmsg_arg
;
258 if ((flags
& PEXECUTE_ONE
) != PEXECUTE_ONE
)
260 pid
= (flags
& PEXECUTE_SEARCH
? _spawnvp
: _spawnv
)
261 (_P_NOWAIT
, program
, fix_argv(argv
));
264 *errmsg_fmt
= install_error_msg
;
265 *errmsg_arg
= program
;
272 pwait (pid
, status
, flags
)
277 /* ??? Here's an opportunity to canonicalize the values in STATUS.
279 return cwait (status
, pid
, WAIT_CHILD
);
282 #else /* ! __CYGWIN32__ */
284 /* This is a kludge to get around the Microsoft C spawn functions' propensity
285 to remove the outermost set of double quotes from all arguments. */
293 for (i
= 1; argvec
[i
] != 0; i
++)
296 char *temp
, *newtemp
;
300 for (j
= 0; j
< len
; j
++)
304 newtemp
= xmalloc (len
+ 2);
305 strncpy (newtemp
, temp
, j
);
307 strncpy (&newtemp
[j
+1], &temp
[j
], len
-j
);
318 return (const char * const *) argvec
;
325 /* mingw32 headers may not define the following. */
330 # define _P_OVERLAY 2
331 # define _P_NOWAITO 3
334 # define WAIT_CHILD 0
335 # define WAIT_GRANDCHILD 1
338 /* Win32 supports pipes */
340 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
343 const char *this_pname
;
344 const char *temp_base
;
345 char **errmsg_fmt
, **errmsg_arg
;
349 int pdes
[2], org_stdin
, org_stdout
;
350 int input_desc
, output_desc
;
351 int retries
, sleep_interval
;
353 /* Pipe waiting from last process, to be used as input for the next one.
354 Value is STDIN_FILE_NO if no pipe is waiting
355 (i.e. the next command is the first of a group). */
356 static int last_pipe_input
;
358 /* If this is the first process, initialize. */
359 if (flags
& PEXECUTE_FIRST
)
360 last_pipe_input
= STDIN_FILE_NO
;
362 input_desc
= last_pipe_input
;
364 /* If this isn't the last process, make a pipe for its output,
365 and record it as waiting to be the input to the next process. */
366 if (! (flags
& PEXECUTE_LAST
))
368 if (_pipe (pdes
, 256, O_BINARY
) < 0)
370 *errmsg_fmt
= "pipe";
374 output_desc
= pdes
[WRITE_PORT
];
375 last_pipe_input
= pdes
[READ_PORT
];
380 output_desc
= STDOUT_FILE_NO
;
381 last_pipe_input
= STDIN_FILE_NO
;
384 if (input_desc
!= STDIN_FILE_NO
)
386 org_stdin
= dup (STDIN_FILE_NO
);
387 dup2 (input_desc
, STDIN_FILE_NO
);
391 if (output_desc
!= STDOUT_FILE_NO
)
393 org_stdout
= dup (STDOUT_FILE_NO
);
394 dup2 (output_desc
, STDOUT_FILE_NO
);
398 pid
= (flags
& PEXECUTE_SEARCH
? _spawnvp
: _spawnv
)
399 (_P_NOWAIT
, program
, fix_argv(argv
));
401 if (input_desc
!= STDIN_FILE_NO
)
403 dup2 (org_stdin
, STDIN_FILE_NO
);
407 if (output_desc
!= STDOUT_FILE_NO
)
409 dup2 (org_stdout
, STDOUT_FILE_NO
);
415 *errmsg_fmt
= install_error_msg
;
416 *errmsg_arg
= program
;
423 /* MS CRTDLL doesn't return enough information in status to decide if the
424 child exited due to a signal or not, rather it simply returns an
425 integer with the exit code of the child; eg., if the child exited with
426 an abort() call and didn't have a handler for SIGABRT, it simply returns
427 with status = 3. We fix the status code to conform to the usual WIF*
428 macros. Note that WIFSIGNALED will never be true under CRTDLL. */
431 pwait (pid
, status
, flags
)
438 pid
= _cwait (&termstat
, pid
, WAIT_CHILD
);
440 /* ??? Here's an opportunity to canonicalize the values in STATUS.
443 /* cwait returns the child process exit code in termstat.
444 A value of 3 indicates that the child caught a signal, but not
445 which one. Since only SIGABRT, SIGFPE and SIGINT do anything, we
450 *status
= (((termstat
) & 0xff) << 8);
455 #endif /* ! defined (__CYGWIN32__) */
461 /* ??? Does OS2 have process.h? */
462 extern int spawnv ();
463 extern int spawnvp ();
466 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
469 const char *this_pname
;
470 const char *temp_base
;
471 char **errmsg_fmt
, **errmsg_arg
;
476 if ((flags
& PEXECUTE_ONE
) != PEXECUTE_ONE
)
478 /* ??? Presumably 1 == _P_NOWAIT. */
479 pid
= (flags
& PEXECUTE_SEARCH
? spawnvp
: spawnv
) (1, program
, argv
);
482 *errmsg_fmt
= install_error_msg
;
483 *errmsg_arg
= program
;
490 pwait (pid
, status
, flags
)
495 /* ??? Here's an opportunity to canonicalize the values in STATUS.
497 int pid
= wait (status
);
505 /* MPW pexecute doesn't actually run anything; instead, it writes out
506 script commands that, when run, will do the actual executing.
508 For example, in GCC's case, GCC will write out several script commands:
515 and then exit. None of the above programs will have run yet. The task
516 that called GCC will then execute the script and cause cpp,etc. to run.
517 The caller must invoke pfinish before calling exit. This adds
518 the finishing touches to the generated script. */
520 static int first_time
= 1;
523 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
526 const char *this_pname
;
527 const char *temp_base
;
528 char **errmsg_fmt
, **errmsg_arg
;
531 char tmpprogram
[255];
535 mpwify_filename (program
, tmpprogram
);
538 printf ("Set Failed 0\n");
542 fputs ("If {Failed} == 0\n", stdout
);
543 /* If being verbose, output a copy of the command. It should be
544 accurate enough and escaped enough to be "clickable". */
545 if (flags
& PEXECUTE_VERBOSE
)
547 fputs ("\tEcho ", stdout
);
548 fputc ('\'', stdout
);
549 fputs (tmpprogram
, stdout
);
550 fputc ('\'', stdout
);
552 for (i
=1; argv
[i
]; i
++)
554 fputc ('\'', stdout
);
555 /* See if we have an argument that needs fixing. */
556 if (strchr(argv
[i
], '/'))
558 tmpname
= (char *) xmalloc (256);
559 mpwify_filename (argv
[i
], tmpname
);
562 for (cp
= argv
[i
]; *cp
; cp
++)
564 /* Write an Option-d escape char in front of special chars. */
565 if (strchr("'+", *cp
))
566 fputc ('\266', stdout
);
569 fputc ('\'', stdout
);
572 fputs ("\n", stdout
);
574 fputs ("\t", stdout
);
575 fputs (tmpprogram
, stdout
);
578 for (i
=1; argv
[i
]; i
++)
580 /* See if we have an argument that needs fixing. */
581 if (strchr(argv
[i
], '/'))
583 tmpname
= (char *) xmalloc (256);
584 mpwify_filename (argv
[i
], tmpname
);
587 if (strchr (argv
[i
], ' '))
588 fputc ('\'', stdout
);
589 for (cp
= argv
[i
]; *cp
; cp
++)
591 /* Write an Option-d escape char in front of special chars. */
592 if (strchr("'+", *cp
))
593 fputc ('\266', stdout
);
596 if (strchr (argv
[i
], ' '))
597 fputc ('\'', stdout
);
601 fputs ("\n", stdout
);
603 /* Output commands that arrange to clean up and exit if a failure occurs.
604 We have to be careful to collect the status from the program that was
605 run, rather than some other script command. Also, we don't exit
606 immediately, since necessary cleanups are at the end of the script. */
607 fputs ("\tSet TmpStatus {Status}\n", stdout
);
608 fputs ("\tIf {TmpStatus} != 0\n", stdout
);
609 fputs ("\t\tSet Failed {TmpStatus}\n", stdout
);
610 fputs ("\tEnd\n", stdout
);
611 fputs ("End\n", stdout
);
613 /* We're just composing a script, can't fail here. */
618 pwait (pid
, status
, flags
)
627 /* Write out commands that will exit with the correct error code
628 if something in the script failed. */
633 printf ("\tExit \"{Failed}\"\n");
638 /* include for Unix-like environments but not for Dos-like environments */
639 #if ! defined (__MSDOS__) && ! defined (OS2) && ! defined (MPW) \
640 && ! defined (_WIN32)
643 extern int execvp ();
645 extern char * my_strerror
PROTO ((int));
649 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
652 const char *this_pname
;
653 const char *temp_base
;
654 char **errmsg_fmt
, **errmsg_arg
;
657 int (*func
)() = (flags
& PEXECUTE_SEARCH
? execvp
: execv
);
660 int input_desc
, output_desc
;
661 int retries
, sleep_interval
;
662 /* Pipe waiting from last process, to be used as input for the next one.
663 Value is STDIN_FILE_NO if no pipe is waiting
664 (i.e. the next command is the first of a group). */
665 static int last_pipe_input
;
667 /* If this is the first process, initialize. */
668 if (flags
& PEXECUTE_FIRST
)
669 last_pipe_input
= STDIN_FILE_NO
;
671 input_desc
= last_pipe_input
;
673 /* If this isn't the last process, make a pipe for its output,
674 and record it as waiting to be the input to the next process. */
675 if (! (flags
& PEXECUTE_LAST
))
679 *errmsg_fmt
= "pipe";
683 output_desc
= pdes
[WRITE_PORT
];
684 last_pipe_input
= pdes
[READ_PORT
];
689 output_desc
= STDOUT_FILE_NO
;
690 last_pipe_input
= STDIN_FILE_NO
;
693 /* Fork a subprocess; wait and retry if it fails. */
695 for (retries
= 0; retries
< 4; retries
++)
700 sleep (sleep_interval
);
708 *errmsg_fmt
= VFORK_STRING
;
714 /* Move the input and output pipes into place, if necessary. */
715 if (input_desc
!= STDIN_FILE_NO
)
717 close (STDIN_FILE_NO
);
721 if (output_desc
!= STDOUT_FILE_NO
)
723 close (STDOUT_FILE_NO
);
728 /* Close the parent's descs that aren't wanted here. */
729 if (last_pipe_input
!= STDIN_FILE_NO
)
730 close (last_pipe_input
);
732 /* Exec the program. */
733 (*func
) (program
, argv
);
735 /* Note: Calling fprintf and exit here doesn't seem right for vfork. */
736 fprintf (stderr
, "%s: ", this_pname
);
737 fprintf (stderr
, install_error_msg
, program
);
739 fprintf (stderr
, ": %s\n", my_strerror (errno
));
741 fprintf (stderr
, ": %s\n", xstrerror (errno
));
748 /* In the parent, after forking.
749 Close the descriptors that we made for this child. */
750 if (input_desc
!= STDIN_FILE_NO
)
752 if (output_desc
!= STDOUT_FILE_NO
)
755 /* Return child's process number. */
761 pwait (pid
, status
, flags
)
766 /* ??? Here's an opportunity to canonicalize the values in STATUS.
769 pid
= waitpid (-1, status
, 0);
776 #endif /* ! __MSDOS__ && ! OS2 && ! MPW && ! _WIN32 */