]> gcc.gnu.org Git - gcc.git/blob - libiberty/pexecute.c
configure.in: Call AC_HEADER_SYS_WAIT.
[gcc.git] / 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.
4
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.
10
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.
15
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. */
20
21 /* This file exports two functions: pexecute and pwait. */
22
23 /* This file lives in at least two places: libiberty and gcc.
24 Don't change one without the other. */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29 #ifdef IN_GCC
30 #include "system.h"
31 #else
32 #include <stdio.h>
33 #include <errno.h>
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37 #define ISSPACE (x) isspace(x)
38 #ifdef HAVE_SYS_WAIT_H
39 #include <sys/wait.h>
40 #endif
41 #endif
42
43 #ifdef vfork /* Autoconf may define this to fork for us. */
44 # define VFORK_STRING "fork"
45 #else
46 # define VFORK_STRING "vfork"
47 #endif
48 #ifdef HAVE_VFORK_H
49 #include <vfork.h>
50 #endif
51 #ifdef VMS
52 #define vfork() (decc$$alloc_vfork_blocks() >= 0 ? \
53 lib$get_current_invo_context(decc$$get_vfork_jmpbuf()) : -1)
54 #endif /* VMS */
55
56 #ifdef IN_GCC
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
63 #else
64 #include "libiberty.h"
65 #endif
66
67 /* stdin file number. */
68 #define STDIN_FILE_NO 0
69
70 /* stdout file number. */
71 #define STDOUT_FILE_NO 1
72
73 /* value of `pipe': port index for reading. */
74 #define READ_PORT 0
75
76 /* value of `pipe': port index for writing. */
77 #define WRITE_PORT 1
78
79 static char *install_error_msg = "installation problem, cannot exec `%s'";
80
81 /* pexecute: execute a program.
82
83 PROGRAM and ARGV are the arguments to execv/execvp.
84
85 THIS_PNAME is name of the calling program (i.e. argv[0]).
86
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.
90
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.
99
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
102 the child.
103
104 The result is the WEXITSTATUS on systems like MSDOS where we spawn and wait
105 for the child here.
106
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.
110
111 pwait: cover function for wait.
112
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.
117
118 The result is the pid of the child reaped,
119 or -1 for failure (errno says why).
120
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.
124
125 pfinish: finish generation of script
126
127 pfinish is necessary for systems like MPW where a script is generated that
128 runs the requested programs.
129 */
130
131 #ifdef __MSDOS__
132
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
136 exit code. */
137
138 #include <process.h>
139
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;
144
145 int
146 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
147 const char *program;
148 char * const *argv;
149 const char *this_pname;
150 const char *temp_base;
151 char **errmsg_fmt, **errmsg_arg;
152 int flags;
153 {
154 int rc;
155
156 last_pid++;
157 if (last_pid < 0)
158 last_pid = 1;
159
160 if ((flags & PEXECUTE_ONE) != PEXECUTE_ONE)
161 abort ();
162
163 #ifdef __GO32__
164 /* ??? What are the possible return values from spawnv? */
165 rc = (flags & PEXECUTE_SEARCH ? spawnvp : spawnv) (1, program, argv);
166 #else
167 char *scmd, *rf;
168 FILE *argfile;
169 int i, el = flags & PEXECUTE_SEARCH ? 4 : 0;
170
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");
176 if (argfile == 0)
177 {
178 int errno_save = errno;
179 free (scmd);
180 errno = errno_save;
181 *errmsg_fmt = "cannot open `%s.gp'";
182 *errmsg_arg = temp_base;
183 return -1;
184 }
185
186 for (i=1; argv[i]; i++)
187 {
188 char *cp;
189 for (cp = argv[i]; *cp; cp++)
190 {
191 if (*cp == '"' || *cp == '\'' || *cp == '\\' || ISSPACE (*cp))
192 fputc ('\\', argfile);
193 fputc (*cp, argfile);
194 }
195 fputc ('\n', argfile);
196 }
197 fclose (argfile);
198
199 rc = system (scmd);
200
201 {
202 int errno_save = errno;
203 remove (rf);
204 free (scmd);
205 errno = errno_save;
206 }
207 #endif
208
209 if (rc == -1)
210 {
211 *errmsg_fmt = install_error_msg;
212 *errmsg_arg = program;
213 return -1;
214 }
215
216 /* Tuck the status away for pwait, and return a "pid". */
217 last_status = rc << 8;
218 return last_pid;
219 }
220
221 int
222 pwait (pid, status, flags)
223 int pid;
224 int *status;
225 int flags;
226 {
227 /* On MSDOS each pexecute must be followed by it's associated pwait. */
228 if (pid != last_pid
229 /* Called twice for the same child? */
230 || pid == last_reaped)
231 {
232 /* ??? ECHILD would be a better choice. Can we use it here? */
233 errno = EINVAL;
234 return -1;
235 }
236 /* ??? Here's an opportunity to canonicalize the values in STATUS.
237 Needed? */
238 *status = last_status;
239 last_reaped = last_pid;
240 return last_pid;
241 }
242
243 #endif /* MSDOS */
244
245 #if defined (_WIN32)
246
247 #include <process.h>
248
249 #ifdef __CYGWIN32__
250
251 #define fix_argv(argvec) (argvec)
252
253 extern int _spawnv ();
254 extern int _spawnvp ();
255
256 int
257 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
258 const char *program;
259 char * const *argv;
260 const char *this_pname;
261 const char *temp_base;
262 char **errmsg_fmt, **errmsg_arg;
263 int flags;
264 {
265 int pid;
266
267 if ((flags & PEXECUTE_ONE) != PEXECUTE_ONE)
268 abort ();
269 pid = (flags & PEXECUTE_SEARCH ? _spawnvp : _spawnv)
270 (_P_NOWAIT, program, fix_argv(argv));
271 if (pid == -1)
272 {
273 *errmsg_fmt = install_error_msg;
274 *errmsg_arg = program;
275 return -1;
276 }
277 return pid;
278 }
279
280 int
281 pwait (pid, status, flags)
282 int pid;
283 int *status;
284 int flags;
285 {
286 /* ??? Here's an opportunity to canonicalize the values in STATUS.
287 Needed? */
288 return cwait (status, pid, WAIT_CHILD);
289 }
290
291 #else /* ! __CYGWIN32__ */
292
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. */
295
296 const char * const *
297 fix_argv (argvec)
298 char **argvec;
299 {
300 int i;
301
302 for (i = 1; argvec[i] != 0; i++)
303 {
304 int len, j;
305 char *temp, *newtemp;
306
307 temp = argvec[i];
308 len = strlen (temp);
309 for (j = 0; j < len; j++)
310 {
311 if (temp[j] == '"')
312 {
313 newtemp = xmalloc (len + 2);
314 strncpy (newtemp, temp, j);
315 newtemp [j] = '\\';
316 strncpy (&newtemp [j+1], &temp [j], len-j);
317 newtemp [len+1] = 0;
318 temp = newtemp;
319 len++;
320 j++;
321 }
322 }
323
324 argvec[i] = temp;
325 }
326
327 return (const char * const *) argvec;
328 }
329
330 #include <io.h>
331 #include <fcntl.h>
332 #include <signal.h>
333
334 /* mingw32 headers may not define the following. */
335
336 #ifndef _P_WAIT
337 # define _P_WAIT 0
338 # define _P_NOWAIT 1
339 # define _P_OVERLAY 2
340 # define _P_NOWAITO 3
341 # define _P_DETACH 4
342
343 # define WAIT_CHILD 0
344 # define WAIT_GRANDCHILD 1
345 #endif
346
347 /* Win32 supports pipes */
348 int
349 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
350 const char *program;
351 char * const *argv;
352 const char *this_pname;
353 const char *temp_base;
354 char **errmsg_fmt, **errmsg_arg;
355 int flags;
356 {
357 int pid;
358 int pdes[2], org_stdin, org_stdout;
359 int input_desc, output_desc;
360 int retries, sleep_interval;
361
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;
366
367 /* If this is the first process, initialize. */
368 if (flags & PEXECUTE_FIRST)
369 last_pipe_input = STDIN_FILE_NO;
370
371 input_desc = last_pipe_input;
372
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))
376 {
377 if (_pipe (pdes, 256, O_BINARY) < 0)
378 {
379 *errmsg_fmt = "pipe";
380 *errmsg_arg = NULL;
381 return -1;
382 }
383 output_desc = pdes[WRITE_PORT];
384 last_pipe_input = pdes[READ_PORT];
385 }
386 else
387 {
388 /* Last process. */
389 output_desc = STDOUT_FILE_NO;
390 last_pipe_input = STDIN_FILE_NO;
391 }
392
393 if (input_desc != STDIN_FILE_NO)
394 {
395 org_stdin = dup (STDIN_FILE_NO);
396 dup2 (input_desc, STDIN_FILE_NO);
397 close (input_desc);
398 }
399
400 if (output_desc != STDOUT_FILE_NO)
401 {
402 org_stdout = dup (STDOUT_FILE_NO);
403 dup2 (output_desc, STDOUT_FILE_NO);
404 close (output_desc);
405 }
406
407 pid = (flags & PEXECUTE_SEARCH ? _spawnvp : _spawnv)
408 (_P_NOWAIT, program, fix_argv(argv));
409
410 if (input_desc != STDIN_FILE_NO)
411 {
412 dup2 (org_stdin, STDIN_FILE_NO);
413 close (org_stdin);
414 }
415
416 if (output_desc != STDOUT_FILE_NO)
417 {
418 dup2 (org_stdout, STDOUT_FILE_NO);
419 close (org_stdout);
420 }
421
422 if (pid == -1)
423 {
424 *errmsg_fmt = install_error_msg;
425 *errmsg_arg = program;
426 return -1;
427 }
428
429 return pid;
430 }
431
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. */
438
439 int
440 pwait (pid, status, flags)
441 int pid;
442 int *status;
443 int flags;
444 {
445 int termstat;
446
447 pid = _cwait (&termstat, pid, WAIT_CHILD);
448
449 /* ??? Here's an opportunity to canonicalize the values in STATUS.
450 Needed? */
451
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
455 report SIGABRT. */
456 if (termstat == 3)
457 *status = SIGABRT;
458 else
459 *status = (((termstat) & 0xff) << 8);
460
461 return pid;
462 }
463
464 #endif /* ! defined (__CYGWIN32__) */
465
466 #endif /* _WIN32 */
467
468 #ifdef OS2
469
470 /* ??? Does OS2 have process.h? */
471 extern int spawnv ();
472 extern int spawnvp ();
473
474 int
475 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
476 const char *program;
477 char * const *argv;
478 const char *this_pname;
479 const char *temp_base;
480 char **errmsg_fmt, **errmsg_arg;
481 int flags;
482 {
483 int pid;
484
485 if ((flags & PEXECUTE_ONE) != PEXECUTE_ONE)
486 abort ();
487 /* ??? Presumably 1 == _P_NOWAIT. */
488 pid = (flags & PEXECUTE_SEARCH ? spawnvp : spawnv) (1, program, argv);
489 if (pid == -1)
490 {
491 *errmsg_fmt = install_error_msg;
492 *errmsg_arg = program;
493 return -1;
494 }
495 return pid;
496 }
497
498 int
499 pwait (pid, status, flags)
500 int pid;
501 int *status;
502 int flags;
503 {
504 /* ??? Here's an opportunity to canonicalize the values in STATUS.
505 Needed? */
506 int pid = wait (status);
507 return pid;
508 }
509
510 #endif /* OS2 */
511
512 #ifdef MPW
513
514 /* MPW pexecute doesn't actually run anything; instead, it writes out
515 script commands that, when run, will do the actual executing.
516
517 For example, in GCC's case, GCC will write out several script commands:
518
519 cpp ...
520 cc1 ...
521 as ...
522 ld ...
523
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. */
528
529 static int first_time = 1;
530
531 int
532 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
533 const char *program;
534 char * const *argv;
535 const char *this_pname;
536 const char *temp_base;
537 char **errmsg_fmt, **errmsg_arg;
538 int flags;
539 {
540 char tmpprogram[255];
541 char *cp, *tmpname;
542 int i;
543
544 mpwify_filename (program, tmpprogram);
545 if (first_time)
546 {
547 printf ("Set Failed 0\n");
548 first_time = 0;
549 }
550
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)
555 {
556 fputs ("\tEcho ", stdout);
557 fputc ('\'', stdout);
558 fputs (tmpprogram, stdout);
559 fputc ('\'', stdout);
560 fputc (' ', stdout);
561 for (i=1; argv[i]; i++)
562 {
563 fputc ('\'', stdout);
564 /* See if we have an argument that needs fixing. */
565 if (strchr(argv[i], '/'))
566 {
567 tmpname = (char *) xmalloc (256);
568 mpwify_filename (argv[i], tmpname);
569 argv[i] = tmpname;
570 }
571 for (cp = argv[i]; *cp; cp++)
572 {
573 /* Write an Option-d escape char in front of special chars. */
574 if (strchr("'+", *cp))
575 fputc ('\266', stdout);
576 fputc (*cp, stdout);
577 }
578 fputc ('\'', stdout);
579 fputc (' ', stdout);
580 }
581 fputs ("\n", stdout);
582 }
583 fputs ("\t", stdout);
584 fputs (tmpprogram, stdout);
585 fputc (' ', stdout);
586
587 for (i=1; argv[i]; i++)
588 {
589 /* See if we have an argument that needs fixing. */
590 if (strchr(argv[i], '/'))
591 {
592 tmpname = (char *) xmalloc (256);
593 mpwify_filename (argv[i], tmpname);
594 argv[i] = tmpname;
595 }
596 if (strchr (argv[i], ' '))
597 fputc ('\'', stdout);
598 for (cp = argv[i]; *cp; cp++)
599 {
600 /* Write an Option-d escape char in front of special chars. */
601 if (strchr("'+", *cp))
602 fputc ('\266', stdout);
603 fputc (*cp, stdout);
604 }
605 if (strchr (argv[i], ' '))
606 fputc ('\'', stdout);
607 fputc (' ', stdout);
608 }
609
610 fputs ("\n", stdout);
611
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);
621
622 /* We're just composing a script, can't fail here. */
623 return 0;
624 }
625
626 int
627 pwait (pid, status, flags)
628 int pid;
629 int *status;
630 int flags;
631 {
632 *status = 0;
633 return 0;
634 }
635
636 /* Write out commands that will exit with the correct error code
637 if something in the script failed. */
638
639 void
640 pfinish ()
641 {
642 printf ("\tExit \"{Failed}\"\n");
643 }
644
645 #endif /* MPW */
646
647 /* include for Unix-like environments but not for Dos-like environments */
648 #if ! defined (__MSDOS__) && ! defined (OS2) && ! defined (MPW) \
649 && ! defined (_WIN32)
650
651 extern int execv ();
652 extern int execvp ();
653 #ifdef IN_GCC
654 extern char * my_strerror PROTO ((int));
655 #endif
656
657 int
658 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
659 const char *program;
660 char * const *argv;
661 const char *this_pname;
662 const char *temp_base;
663 char **errmsg_fmt, **errmsg_arg;
664 int flags;
665 {
666 int (*func)() = (flags & PEXECUTE_SEARCH ? execvp : execv);
667 int pid;
668 int pdes[2];
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;
675
676 /* If this is the first process, initialize. */
677 if (flags & PEXECUTE_FIRST)
678 last_pipe_input = STDIN_FILE_NO;
679
680 input_desc = last_pipe_input;
681
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))
685 {
686 if (pipe (pdes) < 0)
687 {
688 *errmsg_fmt = "pipe";
689 *errmsg_arg = NULL;
690 return -1;
691 }
692 output_desc = pdes[WRITE_PORT];
693 last_pipe_input = pdes[READ_PORT];
694 }
695 else
696 {
697 /* Last process. */
698 output_desc = STDOUT_FILE_NO;
699 last_pipe_input = STDIN_FILE_NO;
700 }
701
702 /* Fork a subprocess; wait and retry if it fails. */
703 sleep_interval = 1;
704 for (retries = 0; retries < 4; retries++)
705 {
706 pid = vfork ();
707 if (pid >= 0)
708 break;
709 sleep (sleep_interval);
710 sleep_interval *= 2;
711 }
712
713 switch (pid)
714 {
715 case -1:
716 {
717 *errmsg_fmt = VFORK_STRING;
718 *errmsg_arg = NULL;
719 return -1;
720 }
721
722 case 0: /* child */
723 /* Move the input and output pipes into place, if necessary. */
724 if (input_desc != STDIN_FILE_NO)
725 {
726 close (STDIN_FILE_NO);
727 dup (input_desc);
728 close (input_desc);
729 }
730 if (output_desc != STDOUT_FILE_NO)
731 {
732 close (STDOUT_FILE_NO);
733 dup (output_desc);
734 close (output_desc);
735 }
736
737 /* Close the parent's descs that aren't wanted here. */
738 if (last_pipe_input != STDIN_FILE_NO)
739 close (last_pipe_input);
740
741 /* Exec the program. */
742 (*func) (program, argv);
743
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);
747 #ifdef IN_GCC
748 fprintf (stderr, ": %s\n", my_strerror (errno));
749 #else
750 fprintf (stderr, ": %s\n", xstrerror (errno));
751 #endif
752 exit (-1);
753 /* NOTREACHED */
754 return 0;
755
756 default:
757 /* In the parent, after forking.
758 Close the descriptors that we made for this child. */
759 if (input_desc != STDIN_FILE_NO)
760 close (input_desc);
761 if (output_desc != STDOUT_FILE_NO)
762 close (output_desc);
763
764 /* Return child's process number. */
765 return pid;
766 }
767 }
768
769 int
770 pwait (pid, status, flags)
771 int pid;
772 int *status;
773 int flags;
774 {
775 /* ??? Here's an opportunity to canonicalize the values in STATUS.
776 Needed? */
777 #ifdef VMS
778 pid = waitpid (-1, status, 0);
779 #else
780 pid = wait (status);
781 #endif
782 return pid;
783 }
784
785 #endif /* ! __MSDOS__ && ! OS2 && ! MPW && ! _WIN32 */
This page took 0.069548 seconds and 6 git commands to generate.