]>
gcc.gnu.org Git - gcc.git/blob - libiberty/pexecute.c
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)
57 /* ??? Need to find a suitable header file. */
58 #define PEXECUTE_FIRST 1
59 #define PEXECUTE_LAST 2
60 #define PEXECUTE_ONE (PEXECUTE_FIRST + PEXECUTE_LAST)
61 #define PEXECUTE_SEARCH 4
62 #define PEXECUTE_VERBOSE 8
64 #include "libiberty.h"
67 /* stdin file number. */
68 #define STDIN_FILE_NO 0
70 /* stdout file number. */
71 #define STDOUT_FILE_NO 1
73 /* value of `pipe': port index for reading. */
76 /* value of `pipe': port index for writing. */
79 static char *install_error_msg
= "installation problem, cannot exec `%s'";
81 /* pexecute: execute a program.
83 PROGRAM and ARGV are the arguments to execv/execvp.
85 THIS_PNAME is name of the calling program (i.e. argv[0]).
87 TEMP_BASE is the path name, sans suffix, of a temporary file to use
88 if needed. This is currently only needed for MSDOS ports that don't use
89 GO32 (do any still exist?). Ports that don't need it can pass NULL.
91 (FLAGS & PEXECUTE_SEARCH) is non-zero if $PATH should be searched
92 (??? It's not clear that GCC passes this flag correctly).
93 (FLAGS & PEXECUTE_FIRST) is nonzero for the first process in chain.
94 (FLAGS & PEXECUTE_FIRST) is nonzero for the last process in chain.
95 FIRST_LAST could be simplified to only mark the last of a chain of processes
96 but that requires the caller to always mark the last one (and not give up
97 early if some error occurs). It's more robust to require the caller to
98 mark both ends of the chain.
100 The result is the pid on systems like Unix where we fork/exec and on systems
101 like WIN32 and OS2 where we use spawn. It is up to the caller to wait for
104 The result is the WEXITSTATUS on systems like MSDOS where we spawn and wait
107 Upon failure, ERRMSG_FMT and ERRMSG_ARG are set to the text of the error
108 message with an optional argument (if not needed, ERRMSG_ARG is set to
109 NULL), and -1 is returned. `errno' is available to the caller to use.
111 pwait: cover function for wait.
113 PID is the process id of the task to wait for.
114 STATUS is the `status' argument to wait.
115 FLAGS is currently unused (allows future enhancement without breaking
116 upward compatibility). Pass 0 for now.
118 The result is the pid of the child reaped,
119 or -1 for failure (errno says why).
121 On systems that don't support waiting for a particular child, PID is
122 ignored. On systems like MSDOS that don't really multitask pwait
123 is just a mechanism to provide a consistent interface for the caller.
125 pfinish: finish generation of script
127 pfinish is necessary for systems like MPW where a script is generated that
128 runs the requested programs.
133 /* MSDOS doesn't multitask, but for the sake of a consistent interface
134 the code behaves like it does. pexecute runs the program, tucks the
135 exit code away, and returns a "pid". pwait must be called to fetch the
140 /* For communicating information from pexecute to pwait. */
141 static int last_pid
= 0;
142 static int last_status
= 0;
143 static int last_reaped
= 0;
146 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
149 const char *this_pname
;
150 const char *temp_base
;
151 char **errmsg_fmt
, **errmsg_arg
;
160 if ((flags
& PEXECUTE_ONE
) != PEXECUTE_ONE
)
164 /* ??? What are the possible return values from spawnv? */
165 rc
= (flags
& PEXECUTE_SEARCH
? spawnvp
: spawnv
) (1, program
, argv
);
169 int i
, el
= flags
& PEXECUTE_SEARCH
? 4 : 0;
171 scmd
= (char *) xmalloc (strlen (program
) + strlen (temp_base
) + 6 + el
);
172 rf
= scmd
+ strlen(program
) + 2 + el
;
173 sprintf (scmd
, "%s%s @%s.gp", program
,
174 (flags
& PEXECUTE_SEARCH
? ".exe" : ""), temp_base
);
175 argfile
= fopen (rf
, "w");
178 int errno_save
= errno
;
181 *errmsg_fmt
= "cannot open `%s.gp'";
182 *errmsg_arg
= temp_base
;
186 for (i
=1; argv
[i
]; i
++)
189 for (cp
= argv
[i
]; *cp
; cp
++)
191 if (*cp
== '"' || *cp
== '\'' || *cp
== '\\' || ISSPACE (*cp
))
192 fputc ('\\', argfile
);
193 fputc (*cp
, argfile
);
195 fputc ('\n', argfile
);
202 int errno_save
= errno
;
211 *errmsg_fmt
= install_error_msg
;
212 *errmsg_arg
= program
;
216 /* Tuck the status away for pwait, and return a "pid". */
217 last_status
= rc
<< 8;
222 pwait (pid
, status
, flags
)
227 /* On MSDOS each pexecute must be followed by it's associated pwait. */
229 /* Called twice for the same child? */
230 || pid
== last_reaped
)
232 /* ??? ECHILD would be a better choice. Can we use it here? */
236 /* ??? Here's an opportunity to canonicalize the values in STATUS.
238 *status
= last_status
;
239 last_reaped
= last_pid
;
251 #define fix_argv(argvec) (argvec)
253 extern int _spawnv ();
254 extern int _spawnvp ();
257 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
260 const char *this_pname
;
261 const char *temp_base
;
262 char **errmsg_fmt
, **errmsg_arg
;
267 if ((flags
& PEXECUTE_ONE
) != PEXECUTE_ONE
)
269 pid
= (flags
& PEXECUTE_SEARCH
? _spawnvp
: _spawnv
)
270 (_P_NOWAIT
, program
, fix_argv(argv
));
273 *errmsg_fmt
= install_error_msg
;
274 *errmsg_arg
= program
;
281 pwait (pid
, status
, flags
)
286 /* ??? Here's an opportunity to canonicalize the values in STATUS.
288 return cwait (status
, pid
, WAIT_CHILD
);
291 #else /* ! __CYGWIN32__ */
293 /* This is a kludge to get around the Microsoft C spawn functions' propensity
294 to remove the outermost set of double quotes from all arguments. */
302 for (i
= 1; argvec
[i
] != 0; i
++)
305 char *temp
, *newtemp
;
309 for (j
= 0; j
< len
; j
++)
313 newtemp
= xmalloc (len
+ 2);
314 strncpy (newtemp
, temp
, j
);
316 strncpy (&newtemp
[j
+1], &temp
[j
], len
-j
);
327 return (const char * const *) argvec
;
334 /* mingw32 headers may not define the following. */
339 # define _P_OVERLAY 2
340 # define _P_NOWAITO 3
343 # define WAIT_CHILD 0
344 # define WAIT_GRANDCHILD 1
347 /* Win32 supports pipes */
349 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
352 const char *this_pname
;
353 const char *temp_base
;
354 char **errmsg_fmt
, **errmsg_arg
;
358 int pdes
[2], org_stdin
, org_stdout
;
359 int input_desc
, output_desc
;
360 int retries
, sleep_interval
;
362 /* Pipe waiting from last process, to be used as input for the next one.
363 Value is STDIN_FILE_NO if no pipe is waiting
364 (i.e. the next command is the first of a group). */
365 static int last_pipe_input
;
367 /* If this is the first process, initialize. */
368 if (flags
& PEXECUTE_FIRST
)
369 last_pipe_input
= STDIN_FILE_NO
;
371 input_desc
= last_pipe_input
;
373 /* If this isn't the last process, make a pipe for its output,
374 and record it as waiting to be the input to the next process. */
375 if (! (flags
& PEXECUTE_LAST
))
377 if (_pipe (pdes
, 256, O_BINARY
) < 0)
379 *errmsg_fmt
= "pipe";
383 output_desc
= pdes
[WRITE_PORT
];
384 last_pipe_input
= pdes
[READ_PORT
];
389 output_desc
= STDOUT_FILE_NO
;
390 last_pipe_input
= STDIN_FILE_NO
;
393 if (input_desc
!= STDIN_FILE_NO
)
395 org_stdin
= dup (STDIN_FILE_NO
);
396 dup2 (input_desc
, STDIN_FILE_NO
);
400 if (output_desc
!= STDOUT_FILE_NO
)
402 org_stdout
= dup (STDOUT_FILE_NO
);
403 dup2 (output_desc
, STDOUT_FILE_NO
);
407 pid
= (flags
& PEXECUTE_SEARCH
? _spawnvp
: _spawnv
)
408 (_P_NOWAIT
, program
, fix_argv(argv
));
410 if (input_desc
!= STDIN_FILE_NO
)
412 dup2 (org_stdin
, STDIN_FILE_NO
);
416 if (output_desc
!= STDOUT_FILE_NO
)
418 dup2 (org_stdout
, STDOUT_FILE_NO
);
424 *errmsg_fmt
= install_error_msg
;
425 *errmsg_arg
= program
;
432 /* MS CRTDLL doesn't return enough information in status to decide if the
433 child exited due to a signal or not, rather it simply returns an
434 integer with the exit code of the child; eg., if the child exited with
435 an abort() call and didn't have a handler for SIGABRT, it simply returns
436 with status = 3. We fix the status code to conform to the usual WIF*
437 macros. Note that WIFSIGNALED will never be true under CRTDLL. */
440 pwait (pid
, status
, flags
)
447 pid
= _cwait (&termstat
, pid
, WAIT_CHILD
);
449 /* ??? Here's an opportunity to canonicalize the values in STATUS.
452 /* cwait returns the child process exit code in termstat.
453 A value of 3 indicates that the child caught a signal, but not
454 which one. Since only SIGABRT, SIGFPE and SIGINT do anything, we
459 *status
= (((termstat
) & 0xff) << 8);
464 #endif /* ! defined (__CYGWIN32__) */
470 /* ??? Does OS2 have process.h? */
471 extern int spawnv ();
472 extern int spawnvp ();
475 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
478 const char *this_pname
;
479 const char *temp_base
;
480 char **errmsg_fmt
, **errmsg_arg
;
485 if ((flags
& PEXECUTE_ONE
) != PEXECUTE_ONE
)
487 /* ??? Presumably 1 == _P_NOWAIT. */
488 pid
= (flags
& PEXECUTE_SEARCH
? spawnvp
: spawnv
) (1, program
, argv
);
491 *errmsg_fmt
= install_error_msg
;
492 *errmsg_arg
= program
;
499 pwait (pid
, status
, flags
)
504 /* ??? Here's an opportunity to canonicalize the values in STATUS.
506 int pid
= wait (status
);
514 /* MPW pexecute doesn't actually run anything; instead, it writes out
515 script commands that, when run, will do the actual executing.
517 For example, in GCC's case, GCC will write out several script commands:
524 and then exit. None of the above programs will have run yet. The task
525 that called GCC will then execute the script and cause cpp,etc. to run.
526 The caller must invoke pfinish before calling exit. This adds
527 the finishing touches to the generated script. */
529 static int first_time
= 1;
532 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
535 const char *this_pname
;
536 const char *temp_base
;
537 char **errmsg_fmt
, **errmsg_arg
;
540 char tmpprogram
[255];
544 mpwify_filename (program
, tmpprogram
);
547 printf ("Set Failed 0\n");
551 fputs ("If {Failed} == 0\n", stdout
);
552 /* If being verbose, output a copy of the command. It should be
553 accurate enough and escaped enough to be "clickable". */
554 if (flags
& PEXECUTE_VERBOSE
)
556 fputs ("\tEcho ", stdout
);
557 fputc ('\'', stdout
);
558 fputs (tmpprogram
, stdout
);
559 fputc ('\'', stdout
);
561 for (i
=1; argv
[i
]; i
++)
563 fputc ('\'', stdout
);
564 /* See if we have an argument that needs fixing. */
565 if (strchr(argv
[i
], '/'))
567 tmpname
= (char *) xmalloc (256);
568 mpwify_filename (argv
[i
], tmpname
);
571 for (cp
= argv
[i
]; *cp
; cp
++)
573 /* Write an Option-d escape char in front of special chars. */
574 if (strchr("'+", *cp
))
575 fputc ('\266', stdout
);
578 fputc ('\'', stdout
);
581 fputs ("\n", stdout
);
583 fputs ("\t", stdout
);
584 fputs (tmpprogram
, stdout
);
587 for (i
=1; argv
[i
]; i
++)
589 /* See if we have an argument that needs fixing. */
590 if (strchr(argv
[i
], '/'))
592 tmpname
= (char *) xmalloc (256);
593 mpwify_filename (argv
[i
], tmpname
);
596 if (strchr (argv
[i
], ' '))
597 fputc ('\'', stdout
);
598 for (cp
= argv
[i
]; *cp
; cp
++)
600 /* Write an Option-d escape char in front of special chars. */
601 if (strchr("'+", *cp
))
602 fputc ('\266', stdout
);
605 if (strchr (argv
[i
], ' '))
606 fputc ('\'', stdout
);
610 fputs ("\n", stdout
);
612 /* Output commands that arrange to clean up and exit if a failure occurs.
613 We have to be careful to collect the status from the program that was
614 run, rather than some other script command. Also, we don't exit
615 immediately, since necessary cleanups are at the end of the script. */
616 fputs ("\tSet TmpStatus {Status}\n", stdout
);
617 fputs ("\tIf {TmpStatus} != 0\n", stdout
);
618 fputs ("\t\tSet Failed {TmpStatus}\n", stdout
);
619 fputs ("\tEnd\n", stdout
);
620 fputs ("End\n", stdout
);
622 /* We're just composing a script, can't fail here. */
627 pwait (pid
, status
, flags
)
636 /* Write out commands that will exit with the correct error code
637 if something in the script failed. */
642 printf ("\tExit \"{Failed}\"\n");
647 /* include for Unix-like environments but not for Dos-like environments */
648 #if ! defined (__MSDOS__) && ! defined (OS2) && ! defined (MPW) \
649 && ! defined (_WIN32)
652 extern int execvp ();
654 extern char * my_strerror
PROTO ((int));
658 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
661 const char *this_pname
;
662 const char *temp_base
;
663 char **errmsg_fmt
, **errmsg_arg
;
666 int (*func
)() = (flags
& PEXECUTE_SEARCH
? execvp
: execv
);
669 int input_desc
, output_desc
;
670 int retries
, sleep_interval
;
671 /* Pipe waiting from last process, to be used as input for the next one.
672 Value is STDIN_FILE_NO if no pipe is waiting
673 (i.e. the next command is the first of a group). */
674 static int last_pipe_input
;
676 /* If this is the first process, initialize. */
677 if (flags
& PEXECUTE_FIRST
)
678 last_pipe_input
= STDIN_FILE_NO
;
680 input_desc
= last_pipe_input
;
682 /* If this isn't the last process, make a pipe for its output,
683 and record it as waiting to be the input to the next process. */
684 if (! (flags
& PEXECUTE_LAST
))
688 *errmsg_fmt
= "pipe";
692 output_desc
= pdes
[WRITE_PORT
];
693 last_pipe_input
= pdes
[READ_PORT
];
698 output_desc
= STDOUT_FILE_NO
;
699 last_pipe_input
= STDIN_FILE_NO
;
702 /* Fork a subprocess; wait and retry if it fails. */
704 for (retries
= 0; retries
< 4; retries
++)
709 sleep (sleep_interval
);
717 *errmsg_fmt
= VFORK_STRING
;
723 /* Move the input and output pipes into place, if necessary. */
724 if (input_desc
!= STDIN_FILE_NO
)
726 close (STDIN_FILE_NO
);
730 if (output_desc
!= STDOUT_FILE_NO
)
732 close (STDOUT_FILE_NO
);
737 /* Close the parent's descs that aren't wanted here. */
738 if (last_pipe_input
!= STDIN_FILE_NO
)
739 close (last_pipe_input
);
741 /* Exec the program. */
742 (*func
) (program
, argv
);
744 /* Note: Calling fprintf and exit here doesn't seem right for vfork. */
745 fprintf (stderr
, "%s: ", this_pname
);
746 fprintf (stderr
, install_error_msg
, program
);
748 fprintf (stderr
, ": %s\n", my_strerror (errno
));
750 fprintf (stderr
, ": %s\n", xstrerror (errno
));
757 /* In the parent, after forking.
758 Close the descriptors that we made for this child. */
759 if (input_desc
!= STDIN_FILE_NO
)
761 if (output_desc
!= STDOUT_FILE_NO
)
764 /* Return child's process number. */
770 pwait (pid
, status
, flags
)
775 /* ??? Here's an opportunity to canonicalize the values in STATUS.
778 pid
= waitpid (-1, status
, 0);
785 #endif /* ! __MSDOS__ && ! OS2 && ! MPW && ! _WIN32 */
This page took 0.069548 seconds and 6 git commands to generate.