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. */
32 #define ISSPACE (x) isspace(x)
36 #include "gansidecl.h"
37 /* ??? Need to find a suitable header file. */
38 #define PEXECUTE_FIRST 1
39 #define PEXECUTE_LAST 2
40 #define PEXECUTE_ONE (PEXECUTE_FIRST + PEXECUTE_LAST)
41 #define PEXECUTE_SEARCH 4
42 #define PEXECUTE_VERBOSE 8
44 #include "libiberty.h"
47 /* stdin file number. */
48 #define STDIN_FILE_NO 0
50 /* stdout file number. */
51 #define STDOUT_FILE_NO 1
53 /* value of `pipe': port index for reading. */
56 /* value of `pipe': port index for writing. */
59 static char *install_error_msg
= "installation problem, cannot exec `%s'";
61 /* pexecute: execute a program.
63 PROGRAM and ARGV are the arguments to execv/execvp.
65 THIS_PNAME is name of the calling program (i.e. argv[0]).
67 TEMP_BASE is the path name, sans suffix, of a temporary file to use
68 if needed. This is currently only needed for MSDOS ports that don't use
69 GO32 (do any still exist?). Ports that don't need it can pass NULL.
71 (FLAGS & PEXECUTE_SEARCH) is non-zero if $PATH should be searched
72 (??? It's not clear that GCC passes this flag correctly).
73 (FLAGS & PEXECUTE_FIRST) is nonzero for the first process in chain.
74 (FLAGS & PEXECUTE_FIRST) is nonzero for the last process in chain.
75 FIRST_LAST could be simplified to only mark the last of a chain of processes
76 but that requires the caller to always mark the last one (and not give up
77 early if some error occurs). It's more robust to require the caller to
78 mark both ends of the chain.
80 The result is the pid on systems like Unix where we fork/exec and on systems
81 like WIN32 and OS2 where we use spawn. It is up to the caller to wait for
84 The result is the WEXITSTATUS on systems like MSDOS where we spawn and wait
87 Upon failure, ERRMSG_FMT and ERRMSG_ARG are set to the text of the error
88 message with an optional argument (if not needed, ERRMSG_ARG is set to
89 NULL), and -1 is returned. `errno' is available to the caller to use.
91 pwait: cover function for wait.
93 PID is the process id of the task to wait for.
94 STATUS is the `status' argument to wait.
95 FLAGS is currently unused (allows future enhancement without breaking
96 upward compatibility). Pass 0 for now.
98 The result is the pid of the child reaped,
99 or -1 for failure (errno says why).
101 On systems that don't support waiting for a particular child, PID is
102 ignored. On systems like MSDOS that don't really multitask pwait
103 is just a mechanism to provide a consistent interface for the caller.
105 pfinish: finish generation of script
107 pfinish is necessary for systems like MPW where a script is generated that
108 runs the requested programs.
113 /* MSDOS doesn't multitask, but for the sake of a consistent interface
114 the code behaves like it does. pexecute runs the program, tucks the
115 exit code away, and returns a "pid". pwait must be called to fetch the
120 /* For communicating information from pexecute to pwait. */
121 static int last_pid
= 0;
122 static int last_status
= 0;
123 static int last_reaped
= 0;
126 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
129 const char *this_pname
;
130 const char *temp_base
;
131 char **errmsg_fmt
, **errmsg_arg
;
140 if ((flags
& PEXECUTE_ONE
) != PEXECUTE_ONE
)
144 /* ??? What are the possible return values from spawnv? */
145 rc
= (flags
& PEXECUTE_SEARCH
? spawnvp
: spawnv
) (1, program
, argv
);
149 int i
, el
= flags
& PEXECUTE_SEARCH
? 4 : 0;
151 scmd
= (char *) xmalloc (strlen (program
) + strlen (temp_base
) + 6 + el
);
152 rf
= scmd
+ strlen(program
) + 2 + el
;
153 sprintf (scmd
, "%s%s @%s.gp", program
,
154 (flags
& PEXECUTE_SEARCH
? ".exe" : ""), temp_base
);
155 argfile
= fopen (rf
, "w");
158 int errno_save
= errno
;
161 *errmsg_fmt
= "cannot open `%s.gp'";
162 *errmsg_arg
= temp_base
;
166 for (i
=1; argv
[i
]; i
++)
169 for (cp
= argv
[i
]; *cp
; cp
++)
171 if (*cp
== '"' || *cp
== '\'' || *cp
== '\\' || ISSPACE (*cp
))
172 fputc ('\\', argfile
);
173 fputc (*cp
, argfile
);
175 fputc ('\n', argfile
);
182 int errno_save
= errno
;
191 *errmsg_fmt
= install_error_msg
;
192 *errmsg_arg
= program
;
196 /* Tuck the status away for pwait, and return a "pid". */
197 last_status
= rc
<< 8;
202 pwait (pid
, status
, flags
)
207 /* On MSDOS each pexecute must be followed by it's associated pwait. */
209 /* Called twice for the same child? */
210 || pid
== last_reaped
)
212 /* ??? ECHILD would be a better choice. Can we use it here? */
216 /* ??? Here's an opportunity to canonicalize the values in STATUS.
218 *status
= last_status
;
219 last_reaped
= last_pid
;
231 #define fix_argv(argvec) (argvec)
233 extern int _spawnv ();
234 extern int _spawnvp ();
237 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
240 const char *this_pname
;
241 const char *temp_base
;
242 char **errmsg_fmt
, **errmsg_arg
;
247 if ((flags
& PEXECUTE_ONE
) != PEXECUTE_ONE
)
249 pid
= (flags
& PEXECUTE_SEARCH
? _spawnvp
: _spawnv
)
250 (_P_NOWAIT
, program
, fix_argv(argv
));
253 *errmsg_fmt
= install_error_msg
;
254 *errmsg_arg
= program
;
261 pwait (pid
, status
, flags
)
266 /* ??? Here's an opportunity to canonicalize the values in STATUS.
268 return cwait (status
, pid
, WAIT_CHILD
);
271 #else /* ! __CYGWIN32__ */
273 /* This is a kludge to get around the Microsoft C spawn functions' propensity
274 to remove the outermost set of double quotes from all arguments. */
282 for (i
= 1; argvec
[i
] != 0; i
++)
285 char *temp
, *newtemp
;
289 for (j
= 0; j
< len
; j
++)
293 newtemp
= xmalloc (len
+ 2);
294 strncpy (newtemp
, temp
, j
);
296 strncpy (&newtemp
[j
+1], &temp
[j
], len
-j
);
307 return (const char * const *) argvec
;
314 /* mingw32 headers may not define the following. */
319 # define _P_OVERLAY 2
320 # define _P_NOWAITO 3
323 # define WAIT_CHILD 0
324 # define WAIT_GRANDCHILD 1
327 /* Win32 supports pipes */
329 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
332 const char *this_pname
;
333 const char *temp_base
;
334 char **errmsg_fmt
, **errmsg_arg
;
338 int pdes
[2], org_stdin
, org_stdout
;
339 int input_desc
, output_desc
;
340 int retries
, sleep_interval
;
342 /* Pipe waiting from last process, to be used as input for the next one.
343 Value is STDIN_FILE_NO if no pipe is waiting
344 (i.e. the next command is the first of a group). */
345 static int last_pipe_input
;
347 /* If this is the first process, initialize. */
348 if (flags
& PEXECUTE_FIRST
)
349 last_pipe_input
= STDIN_FILE_NO
;
351 input_desc
= last_pipe_input
;
353 /* If this isn't the last process, make a pipe for its output,
354 and record it as waiting to be the input to the next process. */
355 if (! (flags
& PEXECUTE_LAST
))
357 if (_pipe (pdes
, 256, O_BINARY
) < 0)
359 *errmsg_fmt
= "pipe";
363 output_desc
= pdes
[WRITE_PORT
];
364 last_pipe_input
= pdes
[READ_PORT
];
369 output_desc
= STDOUT_FILE_NO
;
370 last_pipe_input
= STDIN_FILE_NO
;
373 if (input_desc
!= STDIN_FILE_NO
)
375 org_stdin
= dup (STDIN_FILE_NO
);
376 dup2 (input_desc
, STDIN_FILE_NO
);
380 if (output_desc
!= STDOUT_FILE_NO
)
382 org_stdout
= dup (STDOUT_FILE_NO
);
383 dup2 (output_desc
, STDOUT_FILE_NO
);
387 pid
= (flags
& PEXECUTE_SEARCH
? _spawnvp
: _spawnv
)
388 (_P_NOWAIT
, program
, fix_argv(argv
));
390 if (input_desc
!= STDIN_FILE_NO
)
392 dup2 (org_stdin
, STDIN_FILE_NO
);
396 if (output_desc
!= STDOUT_FILE_NO
)
398 dup2 (org_stdout
, STDOUT_FILE_NO
);
404 *errmsg_fmt
= install_error_msg
;
405 *errmsg_arg
= program
;
412 /* MS CRTDLL doesn't return enough information in status to decide if the
413 child exited due to a signal or not, rather it simply returns an
414 integer with the exit code of the child; eg., if the child exited with
415 an abort() call and didn't have a handler for SIGABRT, it simply returns
416 with status = 3. We fix the status code to conform to the usual WIF*
417 macros. Note that WIFSIGNALED will never be true under CRTDLL. */
420 pwait (pid
, status
, flags
)
427 pid
= _cwait (&termstat
, pid
, WAIT_CHILD
);
429 /* ??? Here's an opportunity to canonicalize the values in STATUS.
432 /* cwait returns the child process exit code in termstat.
433 A value of 3 indicates that the child caught a signal, but not
434 which one. Since only SIGABRT, SIGFPE and SIGINT do anything, we
439 *status
= (((termstat
) & 0xff) << 8);
444 #endif /* ! defined (__CYGWIN32__) */
450 /* ??? Does OS2 have process.h? */
451 extern int spawnv ();
452 extern int spawnvp ();
455 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
458 const char *this_pname
;
459 const char *temp_base
;
460 char **errmsg_fmt
, **errmsg_arg
;
465 if ((flags
& PEXECUTE_ONE
) != PEXECUTE_ONE
)
467 /* ??? Presumably 1 == _P_NOWAIT. */
468 pid
= (flags
& PEXECUTE_SEARCH
? spawnvp
: spawnv
) (1, program
, argv
);
471 *errmsg_fmt
= install_error_msg
;
472 *errmsg_arg
= program
;
479 pwait (pid
, status
, flags
)
484 /* ??? Here's an opportunity to canonicalize the values in STATUS.
486 int pid
= wait (status
);
494 /* MPW pexecute doesn't actually run anything; instead, it writes out
495 script commands that, when run, will do the actual executing.
497 For example, in GCC's case, GCC will write out several script commands:
504 and then exit. None of the above programs will have run yet. The task
505 that called GCC will then execute the script and cause cpp,etc. to run.
506 The caller must invoke pfinish before calling exit. This adds
507 the finishing touches to the generated script. */
509 static int first_time
= 1;
512 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
515 const char *this_pname
;
516 const char *temp_base
;
517 char **errmsg_fmt
, **errmsg_arg
;
520 char tmpprogram
[255];
524 mpwify_filename (program
, tmpprogram
);
527 printf ("Set Failed 0\n");
531 fputs ("If {Failed} == 0\n", stdout
);
532 /* If being verbose, output a copy of the command. It should be
533 accurate enough and escaped enough to be "clickable". */
534 if (flags
& PEXECUTE_VERBOSE
)
536 fputs ("\tEcho ", stdout
);
537 fputc ('\'', stdout
);
538 fputs (tmpprogram
, stdout
);
539 fputc ('\'', stdout
);
541 for (i
=1; argv
[i
]; i
++)
543 fputc ('\'', stdout
);
544 /* See if we have an argument that needs fixing. */
545 if (strchr(argv
[i
], '/'))
547 tmpname
= (char *) xmalloc (256);
548 mpwify_filename (argv
[i
], tmpname
);
551 for (cp
= argv
[i
]; *cp
; cp
++)
553 /* Write an Option-d escape char in front of special chars. */
554 if (strchr("'+", *cp
))
555 fputc ('\266', stdout
);
558 fputc ('\'', stdout
);
561 fputs ("\n", stdout
);
563 fputs ("\t", stdout
);
564 fputs (tmpprogram
, stdout
);
567 for (i
=1; argv
[i
]; i
++)
569 /* See if we have an argument that needs fixing. */
570 if (strchr(argv
[i
], '/'))
572 tmpname
= (char *) xmalloc (256);
573 mpwify_filename (argv
[i
], tmpname
);
576 if (strchr (argv
[i
], ' '))
577 fputc ('\'', stdout
);
578 for (cp
= argv
[i
]; *cp
; cp
++)
580 /* Write an Option-d escape char in front of special chars. */
581 if (strchr("'+", *cp
))
582 fputc ('\266', stdout
);
585 if (strchr (argv
[i
], ' '))
586 fputc ('\'', stdout
);
590 fputs ("\n", stdout
);
592 /* Output commands that arrange to clean up and exit if a failure occurs.
593 We have to be careful to collect the status from the program that was
594 run, rather than some other script command. Also, we don't exit
595 immediately, since necessary cleanups are at the end of the script. */
596 fputs ("\tSet TmpStatus {Status}\n", stdout
);
597 fputs ("\tIf {TmpStatus} != 0\n", stdout
);
598 fputs ("\t\tSet Failed {TmpStatus}\n", stdout
);
599 fputs ("\tEnd\n", stdout
);
600 fputs ("End\n", stdout
);
602 /* We're just composing a script, can't fail here. */
607 pwait (pid
, status
, flags
)
616 /* Write out commands that will exit with the correct error code
617 if something in the script failed. */
622 printf ("\tExit \"{Failed}\"\n");
627 /* include for Unix-like environments but not for Dos-like environments */
628 #if ! defined (__MSDOS__) && ! defined (OS2) && ! defined (MPW) \
629 && ! defined (_WIN32)
632 #define vfork() (decc$$alloc_vfork_blocks() >= 0 ? \
633 lib$get_current_invo_context(decc$$get_vfork_jmpbuf()) : -1)
641 extern int execvp ();
643 extern char * my_strerror
PROTO ((int));
647 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
650 const char *this_pname
;
651 const char *temp_base
;
652 char **errmsg_fmt
, **errmsg_arg
;
655 int (*func
)() = (flags
& PEXECUTE_SEARCH
? execvp
: execv
);
658 int input_desc
, output_desc
;
659 int retries
, sleep_interval
;
660 /* Pipe waiting from last process, to be used as input for the next one.
661 Value is STDIN_FILE_NO if no pipe is waiting
662 (i.e. the next command is the first of a group). */
663 static int last_pipe_input
;
665 /* If this is the first process, initialize. */
666 if (flags
& PEXECUTE_FIRST
)
667 last_pipe_input
= STDIN_FILE_NO
;
669 input_desc
= last_pipe_input
;
671 /* If this isn't the last process, make a pipe for its output,
672 and record it as waiting to be the input to the next process. */
673 if (! (flags
& PEXECUTE_LAST
))
677 *errmsg_fmt
= "pipe";
681 output_desc
= pdes
[WRITE_PORT
];
682 last_pipe_input
= pdes
[READ_PORT
];
687 output_desc
= STDOUT_FILE_NO
;
688 last_pipe_input
= STDIN_FILE_NO
;
691 /* Fork a subprocess; wait and retry if it fails. */
693 for (retries
= 0; retries
< 4; retries
++)
698 sleep (sleep_interval
);
707 *errmsg_fmt
= "fork";
709 *errmsg_fmt
= "vfork";
716 /* Move the input and output pipes into place, if necessary. */
717 if (input_desc
!= STDIN_FILE_NO
)
719 close (STDIN_FILE_NO
);
723 if (output_desc
!= STDOUT_FILE_NO
)
725 close (STDOUT_FILE_NO
);
730 /* Close the parent's descs that aren't wanted here. */
731 if (last_pipe_input
!= STDIN_FILE_NO
)
732 close (last_pipe_input
);
734 /* Exec the program. */
735 (*func
) (program
, argv
);
737 /* Note: Calling fprintf and exit here doesn't seem right for vfork. */
738 fprintf (stderr
, "%s: ", this_pname
);
739 fprintf (stderr
, install_error_msg
, program
);
741 fprintf (stderr
, ": %s\n", my_strerror (errno
));
743 fprintf (stderr
, ": %s\n", xstrerror (errno
));
750 /* In the parent, after forking.
751 Close the descriptors that we made for this child. */
752 if (input_desc
!= STDIN_FILE_NO
)
754 if (output_desc
!= STDOUT_FILE_NO
)
757 /* Return child's process number. */
763 pwait (pid
, status
, flags
)
768 /* ??? Here's an opportunity to canonicalize the values in STATUS.
771 pid
= waitpid (-1, status
, 0);
778 #endif /* ! __MSDOS__ && ! OS2 && ! MPW && ! _WIN32 */