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 #include "gansidecl.h"
38 /* ??? Need to find a suitable header file. */
39 #define PEXECUTE_FIRST 1
40 #define PEXECUTE_LAST 2
41 #define PEXECUTE_ONE (PEXECUTE_FIRST + PEXECUTE_LAST)
42 #define PEXECUTE_SEARCH 4
43 #define PEXECUTE_VERBOSE 8
45 #include "libiberty.h"
48 /* stdin file number. */
49 #define STDIN_FILE_NO 0
51 /* stdout file number. */
52 #define STDOUT_FILE_NO 1
54 /* value of `pipe': port index for reading. */
57 /* value of `pipe': port index for writing. */
60 static char *install_error_msg
= "installation problem, cannot exec `%s'";
62 /* pexecute: execute a program.
64 PROGRAM and ARGV are the arguments to execv/execvp.
66 THIS_PNAME is name of the calling program (i.e. argv[0]).
68 TEMP_BASE is the path name, sans suffix, of a temporary file to use
69 if needed. This is currently only needed for MSDOS ports that don't use
70 GO32 (do any still exist?). Ports that don't need it can pass NULL.
72 (FLAGS & PEXECUTE_SEARCH) is non-zero if $PATH should be searched
73 (??? It's not clear that GCC passes this flag correctly).
74 (FLAGS & PEXECUTE_FIRST) is nonzero for the first process in chain.
75 (FLAGS & PEXECUTE_FIRST) is nonzero for the last process in chain.
76 FIRST_LAST could be simplified to only mark the last of a chain of processes
77 but that requires the caller to always mark the last one (and not give up
78 early if some error occurs). It's more robust to require the caller to
79 mark both ends of the chain.
81 The result is the pid on systems like Unix where we fork/exec and on systems
82 like WIN32 and OS2 where we use spawn. It is up to the caller to wait for
85 The result is the WEXITSTATUS on systems like MSDOS where we spawn and wait
88 Upon failure, ERRMSG_FMT and ERRMSG_ARG are set to the text of the error
89 message with an optional argument (if not needed, ERRMSG_ARG is set to
90 NULL), and -1 is returned. `errno' is available to the caller to use.
92 pwait: cover function for wait.
94 PID is the process id of the task to wait for.
95 STATUS is the `status' argument to wait.
96 FLAGS is currently unused (allows future enhancement without breaking
97 upward compatibility). Pass 0 for now.
99 The result is the pid of the child reaped,
100 or -1 for failure (errno says why).
102 On systems that don't support waiting for a particular child, PID is
103 ignored. On systems like MSDOS that don't really multitask pwait
104 is just a mechanism to provide a consistent interface for the caller.
106 pfinish: finish generation of script
108 pfinish is necessary for systems like MPW where a script is generated that
109 runs the requested programs.
114 /* MSDOS doesn't multitask, but for the sake of a consistent interface
115 the code behaves like it does. pexecute runs the program, tucks the
116 exit code away, and returns a "pid". pwait must be called to fetch the
121 /* For communicating information from pexecute to pwait. */
122 static int last_pid
= 0;
123 static int last_status
= 0;
124 static int last_reaped
= 0;
127 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
130 const char *this_pname
;
131 const char *temp_base
;
132 char **errmsg_fmt
, **errmsg_arg
;
141 if ((flags
& PEXECUTE_ONE
) != PEXECUTE_ONE
)
145 /* ??? What are the possible return values from spawnv? */
146 rc
= (flags
& PEXECUTE_SEARCH
? spawnvp
: spawnv
) (1, program
, argv
);
150 int i
, el
= flags
& PEXECUTE_SEARCH
? 4 : 0;
152 scmd
= (char *) xmalloc (strlen (program
) + strlen (temp_base
) + 6 + el
);
153 rf
= scmd
+ strlen(program
) + 2 + el
;
154 sprintf (scmd
, "%s%s @%s.gp", program
,
155 (flags
& PEXECUTE_SEARCH
? ".exe" : ""), temp_base
);
156 argfile
= fopen (rf
, "w");
159 int errno_save
= errno
;
162 *errmsg_fmt
= "cannot open `%s.gp'";
163 *errmsg_arg
= temp_base
;
167 for (i
=1; argv
[i
]; i
++)
170 for (cp
= argv
[i
]; *cp
; cp
++)
172 if (*cp
== '"' || *cp
== '\'' || *cp
== '\\' || isspace (*cp
))
173 fputc ('\\', argfile
);
174 fputc (*cp
, argfile
);
176 fputc ('\n', argfile
);
183 int errno_save
= errno
;
192 *errmsg_fmt
= install_error_msg
;
193 *errmsg_arg
= program
;
197 /* Tuck the status away for pwait, and return a "pid". */
198 last_status
= rc
<< 8;
203 pwait (pid
, status
, flags
)
208 /* On MSDOS each pexecute must be followed by it's associated pwait. */
210 /* Called twice for the same child? */
211 || pid
== last_reaped
)
213 /* ??? ECHILD would be a better choice. Can we use it here? */
217 /* ??? Here's an opportunity to canonicalize the values in STATUS.
219 *status
= last_status
;
220 last_reaped
= last_pid
;
232 #define fix_argv(argvec) (argvec)
234 extern int _spawnv ();
235 extern int _spawnvp ();
238 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
241 const char *this_pname
;
242 const char *temp_base
;
243 char **errmsg_fmt
, **errmsg_arg
;
248 if ((flags
& PEXECUTE_ONE
) != PEXECUTE_ONE
)
250 pid
= (flags
& PEXECUTE_SEARCH
? _spawnvp
: _spawnv
)
251 (_P_NOWAIT
, program
, fix_argv(argv
));
254 *errmsg_fmt
= install_error_msg
;
255 *errmsg_arg
= program
;
262 pwait (pid
, status
, flags
)
267 /* ??? Here's an opportunity to canonicalize the values in STATUS.
269 return cwait (status
, pid
, WAIT_CHILD
);
272 #else /* ! __CYGWIN32__ */
274 /* This is a kludge to get around the Microsoft C spawn functions' propensity
275 to remove the outermost set of double quotes from all arguments. */
283 for (i
= 1; argvec
[i
] != 0; i
++)
286 char *temp
, *newtemp
;
290 for (j
= 0; j
< len
; j
++)
294 newtemp
= xmalloc (len
+ 2);
295 strncpy (newtemp
, temp
, j
);
297 strncpy (&newtemp
[j
+1], &temp
[j
], len
-j
);
308 return (const char * const *) argvec
;
315 /* mingw32 headers may not define the following. */
320 # define _P_OVERLAY 2
321 # define _P_NOWAITO 3
324 # define WAIT_CHILD 0
325 # define WAIT_GRANDCHILD 1
328 /* Win32 supports pipes */
330 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
333 const char *this_pname
;
334 const char *temp_base
;
335 char **errmsg_fmt
, **errmsg_arg
;
339 int pdes
[2], org_stdin
, org_stdout
;
340 int input_desc
, output_desc
;
341 int retries
, sleep_interval
;
343 /* Pipe waiting from last process, to be used as input for the next one.
344 Value is STDIN_FILE_NO if no pipe is waiting
345 (i.e. the next command is the first of a group). */
346 static int last_pipe_input
;
348 /* If this is the first process, initialize. */
349 if (flags
& PEXECUTE_FIRST
)
350 last_pipe_input
= STDIN_FILE_NO
;
352 input_desc
= last_pipe_input
;
354 /* If this isn't the last process, make a pipe for its output,
355 and record it as waiting to be the input to the next process. */
356 if (! (flags
& PEXECUTE_LAST
))
358 if (_pipe (pdes
, 256, O_BINARY
) < 0)
360 *errmsg_fmt
= "pipe";
364 output_desc
= pdes
[WRITE_PORT
];
365 last_pipe_input
= pdes
[READ_PORT
];
370 output_desc
= STDOUT_FILE_NO
;
371 last_pipe_input
= STDIN_FILE_NO
;
374 if (input_desc
!= STDIN_FILE_NO
)
376 org_stdin
= dup (STDIN_FILE_NO
);
377 dup2 (input_desc
, STDIN_FILE_NO
);
381 if (output_desc
!= STDOUT_FILE_NO
)
383 org_stdout
= dup (STDOUT_FILE_NO
);
384 dup2 (output_desc
, STDOUT_FILE_NO
);
388 pid
= (flags
& PEXECUTE_SEARCH
? _spawnvp
: _spawnv
)
389 (_P_NOWAIT
, program
, fix_argv(argv
));
391 if (input_desc
!= STDIN_FILE_NO
)
393 dup2 (org_stdin
, STDIN_FILE_NO
);
397 if (output_desc
!= STDOUT_FILE_NO
)
399 dup2 (org_stdout
, STDOUT_FILE_NO
);
405 *errmsg_fmt
= install_error_msg
;
406 *errmsg_arg
= program
;
413 /* MS CRTDLL doesn't return enough information in status to decide if the
414 child exited due to a signal or not, rather it simply returns an
415 integer with the exit code of the child; eg., if the child exited with
416 an abort() call and didn't have a handler for SIGABRT, it simply returns
417 with status = 3. We fix the status code to conform to the usual WIF*
418 macros. Note that WIFSIGNALED will never be true under CRTDLL. */
421 pwait (pid
, status
, flags
)
428 pid
= _cwait (&termstat
, pid
, WAIT_CHILD
);
430 /* ??? Here's an opportunity to canonicalize the values in STATUS.
433 /* cwait returns the child process exit code in termstat.
434 A value of 3 indicates that the child caught a signal, but not
435 which one. Since only SIGABRT, SIGFPE and SIGINT do anything, we
440 *status
= (((termstat
) & 0xff) << 8);
445 #endif /* ! defined (__CYGWIN32__) */
451 /* ??? Does OS2 have process.h? */
452 extern int spawnv ();
453 extern int spawnvp ();
456 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
459 const char *this_pname
;
460 const char *temp_base
;
461 char **errmsg_fmt
, **errmsg_arg
;
466 if ((flags
& PEXECUTE_ONE
) != PEXECUTE_ONE
)
468 /* ??? Presumably 1 == _P_NOWAIT. */
469 pid
= (flags
& PEXECUTE_SEARCH
? spawnvp
: spawnv
) (1, program
, argv
);
472 *errmsg_fmt
= install_error_msg
;
473 *errmsg_arg
= program
;
480 pwait (pid
, status
, flags
)
485 /* ??? Here's an opportunity to canonicalize the values in STATUS.
487 int pid
= wait (status
);
495 /* MPW pexecute doesn't actually run anything; instead, it writes out
496 script commands that, when run, will do the actual executing.
498 For example, in GCC's case, GCC will write out several script commands:
505 and then exit. None of the above programs will have run yet. The task
506 that called GCC will then execute the script and cause cpp,etc. to run.
507 The caller must invoke pfinish before calling exit. This adds
508 the finishing touches to the generated script. */
510 static int first_time
= 1;
513 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
516 const char *this_pname
;
517 const char *temp_base
;
518 char **errmsg_fmt
, **errmsg_arg
;
521 char tmpprogram
[255];
525 mpwify_filename (program
, tmpprogram
);
528 printf ("Set Failed 0\n");
532 fputs ("If {Failed} == 0\n", stdout
);
533 /* If being verbose, output a copy of the command. It should be
534 accurate enough and escaped enough to be "clickable". */
535 if (flags
& PEXECUTE_VERBOSE
)
537 fputs ("\tEcho ", stdout
);
538 fputc ('\'', stdout
);
539 fputs (tmpprogram
, stdout
);
540 fputc ('\'', stdout
);
542 for (i
=1; argv
[i
]; i
++)
544 fputc ('\'', stdout
);
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 for (cp
= argv
[i
]; *cp
; cp
++)
554 /* Write an Option-d escape char in front of special chars. */
555 if (strchr("'+", *cp
))
556 fputc ('\266', stdout
);
559 fputc ('\'', stdout
);
562 fputs ("\n", stdout
);
564 fputs ("\t", stdout
);
565 fputs (tmpprogram
, stdout
);
568 for (i
=1; argv
[i
]; i
++)
570 /* See if we have an argument that needs fixing. */
571 if (strchr(argv
[i
], '/'))
573 tmpname
= (char *) xmalloc (256);
574 mpwify_filename (argv
[i
], tmpname
);
577 if (strchr (argv
[i
], ' '))
578 fputc ('\'', stdout
);
579 for (cp
= argv
[i
]; *cp
; cp
++)
581 /* Write an Option-d escape char in front of special chars. */
582 if (strchr("'+", *cp
))
583 fputc ('\266', stdout
);
586 if (strchr (argv
[i
], ' '))
587 fputc ('\'', stdout
);
591 fputs ("\n", stdout
);
593 /* Output commands that arrange to clean up and exit if a failure occurs.
594 We have to be careful to collect the status from the program that was
595 run, rather than some other script command. Also, we don't exit
596 immediately, since necessary cleanups are at the end of the script. */
597 fputs ("\tSet TmpStatus {Status}\n", stdout
);
598 fputs ("\tIf {TmpStatus} != 0\n", stdout
);
599 fputs ("\t\tSet Failed {TmpStatus}\n", stdout
);
600 fputs ("\tEnd\n", stdout
);
601 fputs ("End\n", stdout
);
603 /* We're just composing a script, can't fail here. */
608 pwait (pid
, status
, flags
)
617 /* Write out commands that will exit with the correct error code
618 if something in the script failed. */
623 printf ("\tExit \"{Failed}\"\n");
628 /* include for Unix-like environments but not for Dos-like environments */
629 #if ! defined (__MSDOS__) && ! defined (OS2) && ! defined (MPW) \
630 && ! defined (_WIN32)
633 #define vfork() (decc$$alloc_vfork_blocks() >= 0 ? \
634 lib$get_current_invo_context(decc$$get_vfork_jmpbuf()) : -1)
642 extern int execvp ();
644 extern char * my_strerror();
648 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
651 const char *this_pname
;
652 const char *temp_base
;
653 char **errmsg_fmt
, **errmsg_arg
;
656 int (*func
)() = (flags
& PEXECUTE_SEARCH
? execvp
: execv
);
659 int input_desc
, output_desc
;
660 int retries
, sleep_interval
;
661 /* Pipe waiting from last process, to be used as input for the next one.
662 Value is STDIN_FILE_NO if no pipe is waiting
663 (i.e. the next command is the first of a group). */
664 static int last_pipe_input
;
666 /* If this is the first process, initialize. */
667 if (flags
& PEXECUTE_FIRST
)
668 last_pipe_input
= STDIN_FILE_NO
;
670 input_desc
= last_pipe_input
;
672 /* If this isn't the last process, make a pipe for its output,
673 and record it as waiting to be the input to the next process. */
674 if (! (flags
& PEXECUTE_LAST
))
678 *errmsg_fmt
= "pipe";
682 output_desc
= pdes
[WRITE_PORT
];
683 last_pipe_input
= pdes
[READ_PORT
];
688 output_desc
= STDOUT_FILE_NO
;
689 last_pipe_input
= STDIN_FILE_NO
;
692 /* Fork a subprocess; wait and retry if it fails. */
694 for (retries
= 0; retries
< 4; retries
++)
699 sleep (sleep_interval
);
708 *errmsg_fmt
= "fork";
710 *errmsg_fmt
= "vfork";
717 /* Move the input and output pipes into place, if necessary. */
718 if (input_desc
!= STDIN_FILE_NO
)
720 close (STDIN_FILE_NO
);
724 if (output_desc
!= STDOUT_FILE_NO
)
726 close (STDOUT_FILE_NO
);
731 /* Close the parent's descs that aren't wanted here. */
732 if (last_pipe_input
!= STDIN_FILE_NO
)
733 close (last_pipe_input
);
735 /* Exec the program. */
736 (*func
) (program
, argv
);
738 /* Note: Calling fprintf and exit here doesn't seem right for vfork. */
739 fprintf (stderr
, "%s: ", this_pname
);
740 fprintf (stderr
, install_error_msg
, program
);
742 fprintf (stderr
, ": %s\n", my_strerror (errno
));
744 fprintf (stderr
, ": %s\n", xstrerror (errno
));
751 /* In the parent, after forking.
752 Close the descriptors that we made for this child. */
753 if (input_desc
!= STDIN_FILE_NO
)
755 if (output_desc
!= STDOUT_FILE_NO
)
758 /* Return child's process number. */
764 pwait (pid
, status
, flags
)
769 /* ??? Here's an opportunity to canonicalize the values in STATUS.
772 pid
= waitpid (-1, status
, 0);
779 #endif /* ! __MSDOS__ && ! OS2 && ! MPW && ! _WIN32 */