]>
git.ipfire.org Git - thirdparty/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)
40 #ifdef vfork /* Autoconf may define this to fork for us. */
41 # define VFORK_STRING "fork"
43 # define VFORK_STRING "vfork"
49 #define vfork() (decc$$alloc_vfork_blocks() >= 0 ? \
50 lib$get_current_invo_context(decc$$get_vfork_jmpbuf()) : -1)
54 #include "gansidecl.h"
55 /* ??? Need to find a suitable header file. */
56 #define PEXECUTE_FIRST 1
57 #define PEXECUTE_LAST 2
58 #define PEXECUTE_ONE (PEXECUTE_FIRST + PEXECUTE_LAST)
59 #define PEXECUTE_SEARCH 4
60 #define PEXECUTE_VERBOSE 8
62 #include "libiberty.h"
65 /* stdin file number. */
66 #define STDIN_FILE_NO 0
68 /* stdout file number. */
69 #define STDOUT_FILE_NO 1
71 /* value of `pipe': port index for reading. */
74 /* value of `pipe': port index for writing. */
77 static char *install_error_msg
= "installation problem, cannot exec `%s'";
79 /* pexecute: execute a program.
81 PROGRAM and ARGV are the arguments to execv/execvp.
83 THIS_PNAME is name of the calling program (i.e. argv[0]).
85 TEMP_BASE is the path name, sans suffix, of a temporary file to use
86 if needed. This is currently only needed for MSDOS ports that don't use
87 GO32 (do any still exist?). Ports that don't need it can pass NULL.
89 (FLAGS & PEXECUTE_SEARCH) is non-zero if $PATH should be searched
90 (??? It's not clear that GCC passes this flag correctly).
91 (FLAGS & PEXECUTE_FIRST) is nonzero for the first process in chain.
92 (FLAGS & PEXECUTE_FIRST) is nonzero for the last process in chain.
93 FIRST_LAST could be simplified to only mark the last of a chain of processes
94 but that requires the caller to always mark the last one (and not give up
95 early if some error occurs). It's more robust to require the caller to
96 mark both ends of the chain.
98 The result is the pid on systems like Unix where we fork/exec and on systems
99 like WIN32 and OS2 where we use spawn. It is up to the caller to wait for
102 The result is the WEXITSTATUS on systems like MSDOS where we spawn and wait
105 Upon failure, ERRMSG_FMT and ERRMSG_ARG are set to the text of the error
106 message with an optional argument (if not needed, ERRMSG_ARG is set to
107 NULL), and -1 is returned. `errno' is available to the caller to use.
109 pwait: cover function for wait.
111 PID is the process id of the task to wait for.
112 STATUS is the `status' argument to wait.
113 FLAGS is currently unused (allows future enhancement without breaking
114 upward compatibility). Pass 0 for now.
116 The result is the pid of the child reaped,
117 or -1 for failure (errno says why).
119 On systems that don't support waiting for a particular child, PID is
120 ignored. On systems like MSDOS that don't really multitask pwait
121 is just a mechanism to provide a consistent interface for the caller.
123 pfinish: finish generation of script
125 pfinish is necessary for systems like MPW where a script is generated that
126 runs the requested programs.
131 /* MSDOS doesn't multitask, but for the sake of a consistent interface
132 the code behaves like it does. pexecute runs the program, tucks the
133 exit code away, and returns a "pid". pwait must be called to fetch the
138 /* For communicating information from pexecute to pwait. */
139 static int last_pid
= 0;
140 static int last_status
= 0;
141 static int last_reaped
= 0;
144 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
147 const char *this_pname
;
148 const char *temp_base
;
149 char **errmsg_fmt
, **errmsg_arg
;
158 if ((flags
& PEXECUTE_ONE
) != PEXECUTE_ONE
)
162 /* ??? What are the possible return values from spawnv? */
163 rc
= (flags
& PEXECUTE_SEARCH
? spawnvp
: spawnv
) (1, program
, argv
);
167 int i
, el
= flags
& PEXECUTE_SEARCH
? 4 : 0;
169 scmd
= (char *) xmalloc (strlen (program
) + strlen (temp_base
) + 6 + el
);
170 rf
= scmd
+ strlen(program
) + 2 + el
;
171 sprintf (scmd
, "%s%s @%s.gp", program
,
172 (flags
& PEXECUTE_SEARCH
? ".exe" : ""), temp_base
);
173 argfile
= fopen (rf
, "w");
176 int errno_save
= errno
;
179 *errmsg_fmt
= "cannot open `%s.gp'";
180 *errmsg_arg
= temp_base
;
184 for (i
=1; argv
[i
]; i
++)
187 for (cp
= argv
[i
]; *cp
; cp
++)
189 if (*cp
== '"' || *cp
== '\'' || *cp
== '\\' || ISSPACE (*cp
))
190 fputc ('\\', argfile
);
191 fputc (*cp
, argfile
);
193 fputc ('\n', argfile
);
200 int errno_save
= errno
;
209 *errmsg_fmt
= install_error_msg
;
210 *errmsg_arg
= program
;
214 /* Tuck the status away for pwait, and return a "pid". */
215 last_status
= rc
<< 8;
220 pwait (pid
, status
, flags
)
225 /* On MSDOS each pexecute must be followed by it's associated pwait. */
227 /* Called twice for the same child? */
228 || pid
== last_reaped
)
230 /* ??? ECHILD would be a better choice. Can we use it here? */
234 /* ??? Here's an opportunity to canonicalize the values in STATUS.
236 *status
= last_status
;
237 last_reaped
= last_pid
;
249 #define fix_argv(argvec) (argvec)
251 extern int _spawnv ();
252 extern int _spawnvp ();
255 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
258 const char *this_pname
;
259 const char *temp_base
;
260 char **errmsg_fmt
, **errmsg_arg
;
265 if ((flags
& PEXECUTE_ONE
) != PEXECUTE_ONE
)
267 pid
= (flags
& PEXECUTE_SEARCH
? _spawnvp
: _spawnv
)
268 (_P_NOWAIT
, program
, fix_argv(argv
));
271 *errmsg_fmt
= install_error_msg
;
272 *errmsg_arg
= program
;
279 pwait (pid
, status
, flags
)
284 /* ??? Here's an opportunity to canonicalize the values in STATUS.
286 return cwait (status
, pid
, WAIT_CHILD
);
289 #else /* ! __CYGWIN32__ */
291 /* This is a kludge to get around the Microsoft C spawn functions' propensity
292 to remove the outermost set of double quotes from all arguments. */
300 for (i
= 1; argvec
[i
] != 0; i
++)
303 char *temp
, *newtemp
;
307 for (j
= 0; j
< len
; j
++)
311 newtemp
= xmalloc (len
+ 2);
312 strncpy (newtemp
, temp
, j
);
314 strncpy (&newtemp
[j
+1], &temp
[j
], len
-j
);
325 return (const char * const *) argvec
;
332 /* mingw32 headers may not define the following. */
337 # define _P_OVERLAY 2
338 # define _P_NOWAITO 3
341 # define WAIT_CHILD 0
342 # define WAIT_GRANDCHILD 1
345 /* Win32 supports pipes */
347 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
350 const char *this_pname
;
351 const char *temp_base
;
352 char **errmsg_fmt
, **errmsg_arg
;
356 int pdes
[2], org_stdin
, org_stdout
;
357 int input_desc
, output_desc
;
358 int retries
, sleep_interval
;
360 /* Pipe waiting from last process, to be used as input for the next one.
361 Value is STDIN_FILE_NO if no pipe is waiting
362 (i.e. the next command is the first of a group). */
363 static int last_pipe_input
;
365 /* If this is the first process, initialize. */
366 if (flags
& PEXECUTE_FIRST
)
367 last_pipe_input
= STDIN_FILE_NO
;
369 input_desc
= last_pipe_input
;
371 /* If this isn't the last process, make a pipe for its output,
372 and record it as waiting to be the input to the next process. */
373 if (! (flags
& PEXECUTE_LAST
))
375 if (_pipe (pdes
, 256, O_BINARY
) < 0)
377 *errmsg_fmt
= "pipe";
381 output_desc
= pdes
[WRITE_PORT
];
382 last_pipe_input
= pdes
[READ_PORT
];
387 output_desc
= STDOUT_FILE_NO
;
388 last_pipe_input
= STDIN_FILE_NO
;
391 if (input_desc
!= STDIN_FILE_NO
)
393 org_stdin
= dup (STDIN_FILE_NO
);
394 dup2 (input_desc
, STDIN_FILE_NO
);
398 if (output_desc
!= STDOUT_FILE_NO
)
400 org_stdout
= dup (STDOUT_FILE_NO
);
401 dup2 (output_desc
, STDOUT_FILE_NO
);
405 pid
= (flags
& PEXECUTE_SEARCH
? _spawnvp
: _spawnv
)
406 (_P_NOWAIT
, program
, fix_argv(argv
));
408 if (input_desc
!= STDIN_FILE_NO
)
410 dup2 (org_stdin
, STDIN_FILE_NO
);
414 if (output_desc
!= STDOUT_FILE_NO
)
416 dup2 (org_stdout
, STDOUT_FILE_NO
);
422 *errmsg_fmt
= install_error_msg
;
423 *errmsg_arg
= program
;
430 /* MS CRTDLL doesn't return enough information in status to decide if the
431 child exited due to a signal or not, rather it simply returns an
432 integer with the exit code of the child; eg., if the child exited with
433 an abort() call and didn't have a handler for SIGABRT, it simply returns
434 with status = 3. We fix the status code to conform to the usual WIF*
435 macros. Note that WIFSIGNALED will never be true under CRTDLL. */
438 pwait (pid
, status
, flags
)
445 pid
= _cwait (&termstat
, pid
, WAIT_CHILD
);
447 /* ??? Here's an opportunity to canonicalize the values in STATUS.
450 /* cwait returns the child process exit code in termstat.
451 A value of 3 indicates that the child caught a signal, but not
452 which one. Since only SIGABRT, SIGFPE and SIGINT do anything, we
457 *status
= (((termstat
) & 0xff) << 8);
462 #endif /* ! defined (__CYGWIN32__) */
468 /* ??? Does OS2 have process.h? */
469 extern int spawnv ();
470 extern int spawnvp ();
473 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
476 const char *this_pname
;
477 const char *temp_base
;
478 char **errmsg_fmt
, **errmsg_arg
;
483 if ((flags
& PEXECUTE_ONE
) != PEXECUTE_ONE
)
485 /* ??? Presumably 1 == _P_NOWAIT. */
486 pid
= (flags
& PEXECUTE_SEARCH
? spawnvp
: spawnv
) (1, program
, argv
);
489 *errmsg_fmt
= install_error_msg
;
490 *errmsg_arg
= program
;
497 pwait (pid
, status
, flags
)
502 /* ??? Here's an opportunity to canonicalize the values in STATUS.
504 int pid
= wait (status
);
512 /* MPW pexecute doesn't actually run anything; instead, it writes out
513 script commands that, when run, will do the actual executing.
515 For example, in GCC's case, GCC will write out several script commands:
522 and then exit. None of the above programs will have run yet. The task
523 that called GCC will then execute the script and cause cpp,etc. to run.
524 The caller must invoke pfinish before calling exit. This adds
525 the finishing touches to the generated script. */
527 static int first_time
= 1;
530 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
533 const char *this_pname
;
534 const char *temp_base
;
535 char **errmsg_fmt
, **errmsg_arg
;
538 char tmpprogram
[255];
542 mpwify_filename (program
, tmpprogram
);
545 printf ("Set Failed 0\n");
549 fputs ("If {Failed} == 0\n", stdout
);
550 /* If being verbose, output a copy of the command. It should be
551 accurate enough and escaped enough to be "clickable". */
552 if (flags
& PEXECUTE_VERBOSE
)
554 fputs ("\tEcho ", stdout
);
555 fputc ('\'', stdout
);
556 fputs (tmpprogram
, stdout
);
557 fputc ('\'', stdout
);
559 for (i
=1; argv
[i
]; i
++)
561 fputc ('\'', stdout
);
562 /* See if we have an argument that needs fixing. */
563 if (strchr(argv
[i
], '/'))
565 tmpname
= (char *) xmalloc (256);
566 mpwify_filename (argv
[i
], tmpname
);
569 for (cp
= argv
[i
]; *cp
; cp
++)
571 /* Write an Option-d escape char in front of special chars. */
572 if (strchr("'+", *cp
))
573 fputc ('\266', stdout
);
576 fputc ('\'', stdout
);
579 fputs ("\n", stdout
);
581 fputs ("\t", stdout
);
582 fputs (tmpprogram
, stdout
);
585 for (i
=1; argv
[i
]; i
++)
587 /* See if we have an argument that needs fixing. */
588 if (strchr(argv
[i
], '/'))
590 tmpname
= (char *) xmalloc (256);
591 mpwify_filename (argv
[i
], tmpname
);
594 if (strchr (argv
[i
], ' '))
595 fputc ('\'', stdout
);
596 for (cp
= argv
[i
]; *cp
; cp
++)
598 /* Write an Option-d escape char in front of special chars. */
599 if (strchr("'+", *cp
))
600 fputc ('\266', stdout
);
603 if (strchr (argv
[i
], ' '))
604 fputc ('\'', stdout
);
608 fputs ("\n", stdout
);
610 /* Output commands that arrange to clean up and exit if a failure occurs.
611 We have to be careful to collect the status from the program that was
612 run, rather than some other script command. Also, we don't exit
613 immediately, since necessary cleanups are at the end of the script. */
614 fputs ("\tSet TmpStatus {Status}\n", stdout
);
615 fputs ("\tIf {TmpStatus} != 0\n", stdout
);
616 fputs ("\t\tSet Failed {TmpStatus}\n", stdout
);
617 fputs ("\tEnd\n", stdout
);
618 fputs ("End\n", stdout
);
620 /* We're just composing a script, can't fail here. */
625 pwait (pid
, status
, flags
)
634 /* Write out commands that will exit with the correct error code
635 if something in the script failed. */
640 printf ("\tExit \"{Failed}\"\n");
645 /* include for Unix-like environments but not for Dos-like environments */
646 #if ! defined (__MSDOS__) && ! defined (OS2) && ! defined (MPW) \
647 && ! defined (_WIN32)
650 extern int execvp ();
652 extern char * my_strerror
PROTO ((int));
656 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
659 const char *this_pname
;
660 const char *temp_base
;
661 char **errmsg_fmt
, **errmsg_arg
;
664 int (*func
)() = (flags
& PEXECUTE_SEARCH
? execvp
: execv
);
667 int input_desc
, output_desc
;
668 int retries
, sleep_interval
;
669 /* Pipe waiting from last process, to be used as input for the next one.
670 Value is STDIN_FILE_NO if no pipe is waiting
671 (i.e. the next command is the first of a group). */
672 static int last_pipe_input
;
674 /* If this is the first process, initialize. */
675 if (flags
& PEXECUTE_FIRST
)
676 last_pipe_input
= STDIN_FILE_NO
;
678 input_desc
= last_pipe_input
;
680 /* If this isn't the last process, make a pipe for its output,
681 and record it as waiting to be the input to the next process. */
682 if (! (flags
& PEXECUTE_LAST
))
686 *errmsg_fmt
= "pipe";
690 output_desc
= pdes
[WRITE_PORT
];
691 last_pipe_input
= pdes
[READ_PORT
];
696 output_desc
= STDOUT_FILE_NO
;
697 last_pipe_input
= STDIN_FILE_NO
;
700 /* Fork a subprocess; wait and retry if it fails. */
702 for (retries
= 0; retries
< 4; retries
++)
707 sleep (sleep_interval
);
715 *errmsg_fmt
= VFORK_STRING
;
721 /* Move the input and output pipes into place, if necessary. */
722 if (input_desc
!= STDIN_FILE_NO
)
724 close (STDIN_FILE_NO
);
728 if (output_desc
!= STDOUT_FILE_NO
)
730 close (STDOUT_FILE_NO
);
735 /* Close the parent's descs that aren't wanted here. */
736 if (last_pipe_input
!= STDIN_FILE_NO
)
737 close (last_pipe_input
);
739 /* Exec the program. */
740 (*func
) (program
, argv
);
742 /* Note: Calling fprintf and exit here doesn't seem right for vfork. */
743 fprintf (stderr
, "%s: ", this_pname
);
744 fprintf (stderr
, install_error_msg
, program
);
746 fprintf (stderr
, ": %s\n", my_strerror (errno
));
748 fprintf (stderr
, ": %s\n", xstrerror (errno
));
755 /* In the parent, after forking.
756 Close the descriptors that we made for this child. */
757 if (input_desc
!= STDIN_FILE_NO
)
759 if (output_desc
!= STDOUT_FILE_NO
)
762 /* Return child's process number. */
768 pwait (pid
, status
, flags
)
773 /* ??? Here's an opportunity to canonicalize the values in STATUS.
776 pid
= waitpid (-1, status
, 0);
783 #endif /* ! __MSDOS__ && ! OS2 && ! MPW && ! _WIN32 */