- Fix broken handling of order-only prereqs in secondary expansion
[make.git] / job.c
blobb3fef25e96560043c95e97897702807e96324db8
1 /* Job execution and handling for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
4 Foundation, Inc.
5 This file is part of GNU Make.
7 GNU Make is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 3 of the License, or (at your option) any later
10 version.
12 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along with
17 this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "make.h"
21 #include <assert.h>
23 #include "job.h"
24 #include "debug.h"
25 #include "filedef.h"
26 #include "commands.h"
27 #include "variable.h"
28 #include "debug.h"
30 #include <string.h>
32 /* Default shell to use. */
33 #ifdef WINDOWS32
34 #include <windows.h>
36 char *default_shell = "sh.exe";
37 int no_default_sh_exe = 1;
38 int batch_mode_shell = 1;
39 HANDLE main_thread;
41 #elif defined (_AMIGA)
43 char default_shell[] = "";
44 extern int MyExecute (char **);
45 int batch_mode_shell = 0;
47 #elif defined (__MSDOS__)
49 /* The default shell is a pointer so we can change it if Makefile
50 says so. It is without an explicit path so we get a chance
51 to search the $PATH for it (since MSDOS doesn't have standard
52 directories we could trust). */
53 char *default_shell = "command.com";
54 int batch_mode_shell = 0;
56 #elif defined (__EMX__)
58 char *default_shell = "/bin/sh";
59 int batch_mode_shell = 0;
61 #elif defined (VMS)
63 # include <descrip.h>
64 char default_shell[] = "";
65 int batch_mode_shell = 0;
67 #elif defined (__riscos__)
69 char default_shell[] = "";
70 int batch_mode_shell = 0;
72 #else
74 char default_shell[] = "/bin/sh";
75 int batch_mode_shell = 0;
77 #endif
79 #ifdef __MSDOS__
80 # include <process.h>
81 static int execute_by_shell;
82 static int dos_pid = 123;
83 int dos_status;
84 int dos_command_running;
85 #endif /* __MSDOS__ */
87 #ifdef _AMIGA
88 # include <proto/dos.h>
89 static int amiga_pid = 123;
90 static int amiga_status;
91 static char amiga_bname[32];
92 static int amiga_batch_file;
93 #endif /* Amiga. */
95 #ifdef VMS
96 # ifndef __GNUC__
97 # include <processes.h>
98 # endif
99 # include <starlet.h>
100 # include <lib$routines.h>
101 static void vmsWaitForChildren (int *);
102 #endif
104 #ifdef WINDOWS32
105 # include <windows.h>
106 # include <io.h>
107 # include <process.h>
108 # include "sub_proc.h"
109 # include "w32err.h"
110 # include "pathstuff.h"
111 #endif /* WINDOWS32 */
113 #ifdef __EMX__
114 # include <process.h>
115 #endif
117 #if defined (HAVE_SYS_WAIT_H) || defined (HAVE_UNION_WAIT)
118 # include <sys/wait.h>
119 #endif
121 #ifdef HAVE_WAITPID
122 # define WAIT_NOHANG(status) waitpid (-1, (status), WNOHANG)
123 #else /* Don't have waitpid. */
124 # ifdef HAVE_WAIT3
125 # ifndef wait3
126 extern int wait3 ();
127 # endif
128 # define WAIT_NOHANG(status) wait3 ((status), WNOHANG, (struct rusage *) 0)
129 # endif /* Have wait3. */
130 #endif /* Have waitpid. */
132 #if !defined (wait) && !defined (POSIX)
133 int wait ();
134 #endif
136 #ifndef HAVE_UNION_WAIT
138 # define WAIT_T int
140 # ifndef WTERMSIG
141 # define WTERMSIG(x) ((x) & 0x7f)
142 # endif
143 # ifndef WCOREDUMP
144 # define WCOREDUMP(x) ((x) & 0x80)
145 # endif
146 # ifndef WEXITSTATUS
147 # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
148 # endif
149 # ifndef WIFSIGNALED
150 # define WIFSIGNALED(x) (WTERMSIG (x) != 0)
151 # endif
152 # ifndef WIFEXITED
153 # define WIFEXITED(x) (WTERMSIG (x) == 0)
154 # endif
156 #else /* Have `union wait'. */
158 # define WAIT_T union wait
159 # ifndef WTERMSIG
160 # define WTERMSIG(x) ((x).w_termsig)
161 # endif
162 # ifndef WCOREDUMP
163 # define WCOREDUMP(x) ((x).w_coredump)
164 # endif
165 # ifndef WEXITSTATUS
166 # define WEXITSTATUS(x) ((x).w_retcode)
167 # endif
168 # ifndef WIFSIGNALED
169 # define WIFSIGNALED(x) (WTERMSIG(x) != 0)
170 # endif
171 # ifndef WIFEXITED
172 # define WIFEXITED(x) (WTERMSIG(x) == 0)
173 # endif
175 #endif /* Don't have `union wait'. */
177 #ifndef HAVE_UNISTD_H
178 int dup2 ();
179 int execve ();
180 void _exit ();
181 # ifndef VMS
182 int geteuid ();
183 int getegid ();
184 int setgid ();
185 int getgid ();
186 # endif
187 #endif
189 int getloadavg (double loadavg[], int nelem);
190 int start_remote_job (char **argv, char **envp, int stdin_fd, int *is_remote,
191 int *id_ptr, int *used_stdin);
192 int start_remote_job_p (int);
193 int remote_status (int *exit_code_ptr, int *signal_ptr, int *coredump_ptr,
194 int block);
196 RETSIGTYPE child_handler (int);
197 static void free_child (struct child *);
198 static void start_job_command (struct child *child);
199 static int load_too_high (void);
200 static int job_next_command (struct child *);
201 static int start_waiting_job (struct child *);
203 /* Chain of all live (or recently deceased) children. */
205 struct child *children = 0;
207 /* Number of children currently running. */
209 unsigned int job_slots_used = 0;
211 /* Nonzero if the `good' standard input is in use. */
213 static int good_stdin_used = 0;
215 /* Chain of children waiting to run until the load average goes down. */
217 static struct child *waiting_jobs = 0;
219 /* Non-zero if we use a *real* shell (always so on Unix). */
221 int unixy_shell = 1;
223 /* Number of jobs started in the current second. */
225 unsigned long job_counter = 0;
227 /* Number of jobserver tokens this instance is currently using. */
229 unsigned int jobserver_tokens = 0;
231 #ifdef WINDOWS32
233 * The macro which references this function is defined in make.h.
236 w32_kill(int pid, int sig)
238 return ((process_kill((HANDLE)pid, sig) == TRUE) ? 0 : -1);
241 /* This function creates a temporary file name with an extension specified
242 * by the unixy arg.
243 * Return an xmalloc'ed string of a newly created temp file and its
244 * file descriptor, or die. */
245 static char *
246 create_batch_file (char const *base, int unixy, int *fd)
248 const char *const ext = unixy ? "sh" : "bat";
249 const char *error = NULL;
250 char temp_path[MAXPATHLEN]; /* need to know its length */
251 unsigned path_size = GetTempPath(sizeof temp_path, temp_path);
252 int path_is_dot = 0;
253 unsigned uniq = 1;
254 const unsigned sizemax = strlen (base) + strlen (ext) + 10;
256 if (path_size == 0)
258 path_size = GetCurrentDirectory (sizeof temp_path, temp_path);
259 path_is_dot = 1;
262 while (path_size > 0 &&
263 path_size + sizemax < sizeof temp_path &&
264 uniq < 0x10000)
266 unsigned size = sprintf (temp_path + path_size,
267 "%s%s-%x.%s",
268 temp_path[path_size - 1] == '\\' ? "" : "\\",
269 base, uniq, ext);
270 HANDLE h = CreateFile (temp_path, /* file name */
271 GENERIC_READ | GENERIC_WRITE, /* desired access */
272 0, /* no share mode */
273 NULL, /* default security attributes */
274 CREATE_NEW, /* creation disposition */
275 FILE_ATTRIBUTE_NORMAL | /* flags and attributes */
276 FILE_ATTRIBUTE_TEMPORARY, /* we'll delete it */
277 NULL); /* no template file */
279 if (h == INVALID_HANDLE_VALUE)
281 const DWORD er = GetLastError();
283 if (er == ERROR_FILE_EXISTS || er == ERROR_ALREADY_EXISTS)
284 ++uniq;
286 /* the temporary path is not guaranteed to exist */
287 else if (path_is_dot == 0)
289 path_size = GetCurrentDirectory (sizeof temp_path, temp_path);
290 path_is_dot = 1;
293 else
295 error = map_windows32_error_to_string (er);
296 break;
299 else
301 const unsigned final_size = path_size + size + 1;
302 char *const path = xmalloc (final_size);
303 memcpy (path, temp_path, final_size);
304 *fd = _open_osfhandle ((long)h, 0);
305 if (unixy)
307 char *p;
308 int ch;
309 for (p = path; (ch = *p) != 0; ++p)
310 if (ch == '\\')
311 *p = '/';
313 return path; /* good return */
317 *fd = -1;
318 if (error == NULL)
319 error = _("Cannot create a temporary file\n");
320 fatal (NILF, error);
322 /* not reached */
323 return NULL;
325 #endif /* WINDOWS32 */
327 #ifdef __EMX__
328 /* returns whether path is assumed to be a unix like shell. */
330 _is_unixy_shell (const char *path)
332 /* list of non unix shells */
333 const char *known_os2shells[] = {
334 "cmd.exe",
335 "cmd",
336 "4os2.exe",
337 "4os2",
338 "4dos.exe",
339 "4dos",
340 "command.com",
341 "command",
342 NULL
345 /* find the rightmost '/' or '\\' */
346 const char *name = strrchr (path, '/');
347 const char *p = strrchr (path, '\\');
348 unsigned i;
350 if (name && p) /* take the max */
351 name = (name > p) ? name : p;
352 else if (p) /* name must be 0 */
353 name = p;
354 else if (!name) /* name and p must be 0 */
355 name = path;
357 if (*name == '/' || *name == '\\') name++;
359 i = 0;
360 while (known_os2shells[i] != NULL) {
361 if (strcasecmp (name, known_os2shells[i]) == 0)
362 return 0; /* not a unix shell */
363 i++;
366 /* in doubt assume a unix like shell */
367 return 1;
369 #endif /* __EMX__ */
372 /* Write an error message describing the exit status given in
373 EXIT_CODE, EXIT_SIG, and COREDUMP, for the target TARGET_NAME.
374 Append "(ignored)" if IGNORED is nonzero. */
376 static void
377 child_error (const char *target_name,
378 int exit_code, int exit_sig, int coredump, int ignored)
380 if (ignored && silent_flag)
381 return;
383 #ifdef VMS
384 if (!(exit_code & 1))
385 error (NILF,
386 (ignored ? _("*** [%s] Error 0x%x (ignored)")
387 : _("*** [%s] Error 0x%x")),
388 target_name, exit_code);
389 #else
390 if (exit_sig == 0)
391 error (NILF, ignored ? _("[%s] Error %d (ignored)") :
392 _("*** [%s] Error %d"),
393 target_name, exit_code);
394 else
395 error (NILF, "*** [%s] %s%s",
396 target_name, strsignal (exit_sig),
397 coredump ? _(" (core dumped)") : "");
398 #endif /* VMS */
402 /* Handle a dead child. This handler may or may not ever be installed.
404 If we're using the jobserver feature, we need it. First, installing it
405 ensures the read will interrupt on SIGCHLD. Second, we close the dup'd
406 read FD to ensure we don't enter another blocking read without reaping all
407 the dead children. In this case we don't need the dead_children count.
409 If we don't have either waitpid or wait3, then make is unreliable, but we
410 use the dead_children count to reap children as best we can. */
412 static unsigned int dead_children = 0;
414 RETSIGTYPE
415 child_handler (int sig UNUSED)
417 ++dead_children;
419 if (job_rfd >= 0)
421 close (job_rfd);
422 job_rfd = -1;
425 #ifdef __EMX__
426 /* The signal handler must called only once! */
427 signal (SIGCHLD, SIG_DFL);
428 #endif
430 /* This causes problems if the SIGCHLD interrupts a printf().
431 DB (DB_JOBS, (_("Got a SIGCHLD; %u unreaped children.\n"), dead_children));
435 extern int shell_function_pid, shell_function_completed;
437 /* Reap all dead children, storing the returned status and the new command
438 state (`cs_finished') in the `file' member of the `struct child' for the
439 dead child, and removing the child from the chain. In addition, if BLOCK
440 nonzero, we block in this function until we've reaped at least one
441 complete child, waiting for it to die if necessary. If ERR is nonzero,
442 print an error message first. */
444 void
445 reap_children (int block, int err)
447 #ifndef WINDOWS32
448 WAIT_T status;
449 /* Initially, assume we have some. */
450 int reap_more = 1;
451 #endif
453 #ifdef WAIT_NOHANG
454 # define REAP_MORE reap_more
455 #else
456 # define REAP_MORE dead_children
457 #endif
459 /* As long as:
461 We have at least one child outstanding OR a shell function in progress,
463 We're blocking for a complete child OR there are more children to reap
465 we'll keep reaping children. */
467 while ((children != 0 || shell_function_pid != 0)
468 && (block || REAP_MORE))
470 int remote = 0;
471 pid_t pid;
472 int exit_code, exit_sig, coredump;
473 register struct child *lastc, *c;
474 int child_failed;
475 int any_remote, any_local;
476 int dontcare;
478 if (err && block)
480 static int printed = 0;
482 /* We might block for a while, so let the user know why.
483 Only print this message once no matter how many jobs are left. */
484 fflush (stdout);
485 if (!printed)
486 error (NILF, _("*** Waiting for unfinished jobs...."));
487 printed = 1;
490 /* We have one less dead child to reap. As noted in
491 child_handler() above, this count is completely unimportant for
492 all modern, POSIX-y systems that support wait3() or waitpid().
493 The rest of this comment below applies only to early, broken
494 pre-POSIX systems. We keep the count only because... it's there...
496 The test and decrement are not atomic; if it is compiled into:
497 register = dead_children - 1;
498 dead_children = register;
499 a SIGCHLD could come between the two instructions.
500 child_handler increments dead_children.
501 The second instruction here would lose that increment. But the
502 only effect of dead_children being wrong is that we might wait
503 longer than necessary to reap a child, and lose some parallelism;
504 and we might print the "Waiting for unfinished jobs" message above
505 when not necessary. */
507 if (dead_children > 0)
508 --dead_children;
510 any_remote = 0;
511 any_local = shell_function_pid != 0;
512 for (c = children; c != 0; c = c->next)
514 any_remote |= c->remote;
515 any_local |= ! c->remote;
516 DB (DB_JOBS, (_("Live child 0x%08lx (%s) PID %ld %s\n"),
517 (unsigned long int) c, c->file->name,
518 (long) c->pid, c->remote ? _(" (remote)") : ""));
519 #ifdef VMS
520 break;
521 #endif
524 /* First, check for remote children. */
525 if (any_remote)
526 pid = remote_status (&exit_code, &exit_sig, &coredump, 0);
527 else
528 pid = 0;
530 if (pid > 0)
531 /* We got a remote child. */
532 remote = 1;
533 else if (pid < 0)
535 /* A remote status command failed miserably. Punt. */
536 remote_status_lose:
537 pfatal_with_name ("remote_status");
539 else
541 /* No remote children. Check for local children. */
542 #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
543 if (any_local)
545 #ifdef VMS
546 vmsWaitForChildren (&status);
547 pid = c->pid;
548 #else
549 #ifdef WAIT_NOHANG
550 if (!block)
551 pid = WAIT_NOHANG (&status);
552 else
553 #endif
554 EINTRLOOP(pid, wait (&status));
555 #endif /* !VMS */
557 else
558 pid = 0;
560 if (pid < 0)
562 /* The wait*() failed miserably. Punt. */
563 pfatal_with_name ("wait");
565 else if (pid > 0)
567 /* We got a child exit; chop the status word up. */
568 exit_code = WEXITSTATUS (status);
569 exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0;
570 coredump = WCOREDUMP (status);
572 /* If we have started jobs in this second, remove one. */
573 if (job_counter)
574 --job_counter;
576 else
578 /* No local children are dead. */
579 reap_more = 0;
581 if (!block || !any_remote)
582 break;
584 /* Now try a blocking wait for a remote child. */
585 pid = remote_status (&exit_code, &exit_sig, &coredump, 1);
586 if (pid < 0)
587 goto remote_status_lose;
588 else if (pid == 0)
589 /* No remote children either. Finally give up. */
590 break;
592 /* We got a remote child. */
593 remote = 1;
595 #endif /* !__MSDOS__, !Amiga, !WINDOWS32. */
597 #ifdef __MSDOS__
598 /* Life is very different on MSDOS. */
599 pid = dos_pid - 1;
600 status = dos_status;
601 exit_code = WEXITSTATUS (status);
602 if (exit_code == 0xff)
603 exit_code = -1;
604 exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0;
605 coredump = 0;
606 #endif /* __MSDOS__ */
607 #ifdef _AMIGA
608 /* Same on Amiga */
609 pid = amiga_pid - 1;
610 status = amiga_status;
611 exit_code = amiga_status;
612 exit_sig = 0;
613 coredump = 0;
614 #endif /* _AMIGA */
615 #ifdef WINDOWS32
617 HANDLE hPID;
618 int werr;
619 HANDLE hcTID, hcPID;
620 exit_code = 0;
621 exit_sig = 0;
622 coredump = 0;
624 /* Record the thread ID of the main process, so that we
625 could suspend it in the signal handler. */
626 if (!main_thread)
628 hcTID = GetCurrentThread ();
629 hcPID = GetCurrentProcess ();
630 if (!DuplicateHandle (hcPID, hcTID, hcPID, &main_thread, 0,
631 FALSE, DUPLICATE_SAME_ACCESS))
633 DWORD e = GetLastError ();
634 fprintf (stderr,
635 "Determine main thread ID (Error %ld: %s)\n",
636 e, map_windows32_error_to_string(e));
638 else
639 DB (DB_VERBOSE, ("Main thread handle = 0x%08lx\n",
640 (unsigned long)main_thread));
643 /* wait for anything to finish */
644 hPID = process_wait_for_any();
645 if (hPID)
648 /* was an error found on this process? */
649 werr = process_last_err(hPID);
651 /* get exit data */
652 exit_code = process_exit_code(hPID);
654 if (werr)
655 fprintf(stderr, "make (e=%d): %s",
656 exit_code, map_windows32_error_to_string(exit_code));
658 /* signal */
659 exit_sig = process_signal(hPID);
661 /* cleanup process */
662 process_cleanup(hPID);
664 coredump = 0;
666 pid = (pid_t) hPID;
668 #endif /* WINDOWS32 */
671 /* Check if this is the child of the `shell' function. */
672 if (!remote && pid == shell_function_pid)
674 /* It is. Leave an indicator for the `shell' function. */
675 if (exit_sig == 0 && exit_code == 127)
676 shell_function_completed = -1;
677 else
678 shell_function_completed = 1;
679 break;
682 child_failed = exit_sig != 0 || exit_code != 0;
684 /* Search for a child matching the deceased one. */
685 lastc = 0;
686 for (c = children; c != 0; lastc = c, c = c->next)
687 if (c->remote == remote && c->pid == pid)
688 break;
690 if (c == 0)
691 /* An unknown child died.
692 Ignore it; it was inherited from our invoker. */
693 continue;
695 DB (DB_JOBS, (child_failed
696 ? _("Reaping losing child 0x%08lx PID %ld %s\n")
697 : _("Reaping winning child 0x%08lx PID %ld %s\n"),
698 (unsigned long int) c, (long) c->pid,
699 c->remote ? _(" (remote)") : ""));
701 if (c->sh_batch_file) {
702 DB (DB_JOBS, (_("Cleaning up temp batch file %s\n"),
703 c->sh_batch_file));
705 /* just try and remove, don't care if this fails */
706 remove (c->sh_batch_file);
708 /* all done with memory */
709 free (c->sh_batch_file);
710 c->sh_batch_file = NULL;
713 /* If this child had the good stdin, say it is now free. */
714 if (c->good_stdin)
715 good_stdin_used = 0;
717 dontcare = c->dontcare;
719 if (child_failed && !c->noerror && !ignore_errors_flag)
721 /* The commands failed. Write an error message,
722 delete non-precious targets, and abort. */
723 static int delete_on_error = -1;
725 if (!dontcare)
726 child_error (c->file->name, exit_code, exit_sig, coredump, 0);
728 c->file->update_status = 2;
729 if (delete_on_error == -1)
731 struct file *f = lookup_file (".DELETE_ON_ERROR");
732 delete_on_error = f != 0 && f->is_target;
734 if (exit_sig != 0 || delete_on_error)
735 delete_child_targets (c);
737 else
739 if (child_failed)
741 /* The commands failed, but we don't care. */
742 child_error (c->file->name,
743 exit_code, exit_sig, coredump, 1);
744 child_failed = 0;
747 /* If there are more commands to run, try to start them. */
748 if (job_next_command (c))
750 if (handling_fatal_signal)
752 /* Never start new commands while we are dying.
753 Since there are more commands that wanted to be run,
754 the target was not completely remade. So we treat
755 this as if a command had failed. */
756 c->file->update_status = 2;
758 else
760 /* Check again whether to start remotely.
761 Whether or not we want to changes over time.
762 Also, start_remote_job may need state set up
763 by start_remote_job_p. */
764 c->remote = start_remote_job_p (0);
765 start_job_command (c);
766 /* Fatal signals are left blocked in case we were
767 about to put that child on the chain. But it is
768 already there, so it is safe for a fatal signal to
769 arrive now; it will clean up this child's targets. */
770 unblock_sigs ();
771 if (c->file->command_state == cs_running)
772 /* We successfully started the new command.
773 Loop to reap more children. */
774 continue;
777 if (c->file->update_status != 0)
778 /* We failed to start the commands. */
779 delete_child_targets (c);
781 else
782 /* There are no more commands. We got through them all
783 without an unignored error. Now the target has been
784 successfully updated. */
785 c->file->update_status = 0;
788 /* When we get here, all the commands for C->file are finished
789 (or aborted) and C->file->update_status contains 0 or 2. But
790 C->file->command_state is still cs_running if all the commands
791 ran; notice_finish_file looks for cs_running to tell it that
792 it's interesting to check the file's modtime again now. */
794 if (! handling_fatal_signal)
795 /* Notice if the target of the commands has been changed.
796 This also propagates its values for command_state and
797 update_status to its also_make files. */
798 notice_finished_file (c->file);
800 DB (DB_JOBS, (_("Removing child 0x%08lx PID %ld%s from chain.\n"),
801 (unsigned long int) c, (long) c->pid,
802 c->remote ? _(" (remote)") : ""));
804 /* Block fatal signals while frobnicating the list, so that
805 children and job_slots_used are always consistent. Otherwise
806 a fatal signal arriving after the child is off the chain and
807 before job_slots_used is decremented would believe a child was
808 live and call reap_children again. */
809 block_sigs ();
811 /* There is now another slot open. */
812 if (job_slots_used > 0)
813 --job_slots_used;
815 /* Remove the child from the chain and free it. */
816 if (lastc == 0)
817 children = c->next;
818 else
819 lastc->next = c->next;
821 free_child (c);
823 unblock_sigs ();
825 /* If the job failed, and the -k flag was not given, die,
826 unless we are already in the process of dying. */
827 if (!err && child_failed && !dontcare && !keep_going_flag &&
828 /* fatal_error_signal will die with the right signal. */
829 !handling_fatal_signal)
830 die (2);
832 /* Only block for one child. */
833 block = 0;
836 return;
839 /* Free the storage allocated for CHILD. */
841 static void
842 free_child (struct child *child)
844 if (!jobserver_tokens)
845 fatal (NILF, "INTERNAL: Freeing child 0x%08lx (%s) but no tokens left!\n",
846 (unsigned long int) child, child->file->name);
848 /* If we're using the jobserver and this child is not the only outstanding
849 job, put a token back into the pipe for it. */
851 if (job_fds[1] >= 0 && jobserver_tokens > 1)
853 char token = '+';
854 int r;
856 /* Write a job token back to the pipe. */
858 EINTRLOOP (r, write (job_fds[1], &token, 1));
859 if (r != 1)
860 pfatal_with_name (_("write jobserver"));
862 DB (DB_JOBS, (_("Released token for child 0x%08lx (%s).\n"),
863 (unsigned long int) child, child->file->name));
866 --jobserver_tokens;
868 if (handling_fatal_signal) /* Don't bother free'ing if about to die. */
869 return;
871 if (child->command_lines != 0)
873 register unsigned int i;
874 for (i = 0; i < child->file->cmds->ncommand_lines; ++i)
875 free (child->command_lines[i]);
876 free (child->command_lines);
879 if (child->environment != 0)
881 register char **ep = child->environment;
882 while (*ep != 0)
883 free (*ep++);
884 free (child->environment);
887 free (child);
890 #ifdef POSIX
891 extern sigset_t fatal_signal_set;
892 #endif
894 void
895 block_sigs (void)
897 #ifdef POSIX
898 (void) sigprocmask (SIG_BLOCK, &fatal_signal_set, (sigset_t *) 0);
899 #else
900 # ifdef HAVE_SIGSETMASK
901 (void) sigblock (fatal_signal_mask);
902 # endif
903 #endif
906 #ifdef POSIX
907 void
908 unblock_sigs (void)
910 sigset_t empty;
911 sigemptyset (&empty);
912 sigprocmask (SIG_SETMASK, &empty, (sigset_t *) 0);
914 #endif
916 #ifdef MAKE_JOBSERVER
917 RETSIGTYPE
918 job_noop (int sig UNUSED)
921 /* Set the child handler action flags to FLAGS. */
922 static void
923 set_child_handler_action_flags (int set_handler, int set_alarm)
925 struct sigaction sa;
927 #ifdef __EMX__
928 /* The child handler must be turned off here. */
929 signal (SIGCHLD, SIG_DFL);
930 #endif
932 memset (&sa, '\0', sizeof sa);
933 sa.sa_handler = child_handler;
934 sa.sa_flags = set_handler ? 0 : SA_RESTART;
935 #if defined SIGCHLD
936 sigaction (SIGCHLD, &sa, NULL);
937 #endif
938 #if defined SIGCLD && SIGCLD != SIGCHLD
939 sigaction (SIGCLD, &sa, NULL);
940 #endif
941 #if defined SIGALRM
942 if (set_alarm)
944 /* If we're about to enter the read(), set an alarm to wake up in a
945 second so we can check if the load has dropped and we can start more
946 work. On the way out, turn off the alarm and set SIG_DFL. */
947 alarm (set_handler ? 1 : 0);
948 sa.sa_handler = set_handler ? job_noop : SIG_DFL;
949 sa.sa_flags = 0;
950 sigaction (SIGALRM, &sa, NULL);
952 #endif
954 #endif
957 /* Start a job to run the commands specified in CHILD.
958 CHILD is updated to reflect the commands and ID of the child process.
960 NOTE: On return fatal signals are blocked! The caller is responsible
961 for calling `unblock_sigs', once the new child is safely on the chain so
962 it can be cleaned up in the event of a fatal signal. */
964 static void
965 start_job_command (struct child *child)
967 #if !defined(_AMIGA) && !defined(WINDOWS32)
968 static int bad_stdin = -1;
969 #endif
970 char *p;
971 /* Must be volatile to silence bogus GCC warning about longjmp/vfork. */
972 volatile int flags;
973 #ifdef VMS
974 char *argv;
975 #else
976 char **argv;
977 #endif
979 /* If we have a completely empty commandset, stop now. */
980 if (!child->command_ptr)
981 goto next_command;
983 /* Combine the flags parsed for the line itself with
984 the flags specified globally for this target. */
985 flags = (child->file->command_flags
986 | child->file->cmds->lines_flags[child->command_line - 1]);
988 p = child->command_ptr;
989 child->noerror = ((flags & COMMANDS_NOERROR) != 0);
991 while (*p != '\0')
993 if (*p == '@')
994 flags |= COMMANDS_SILENT;
995 else if (*p == '+')
996 flags |= COMMANDS_RECURSE;
997 else if (*p == '-')
998 child->noerror = 1;
999 else if (!isblank ((unsigned char)*p))
1000 break;
1001 ++p;
1004 /* Update the file's command flags with any new ones we found. We only
1005 keep the COMMANDS_RECURSE setting. Even this isn't 100% correct; we are
1006 now marking more commands recursive than should be in the case of
1007 multiline define/endef scripts where only one line is marked "+". In
1008 order to really fix this, we'll have to keep a lines_flags for every
1009 actual line, after expansion. */
1010 child->file->cmds->lines_flags[child->command_line - 1]
1011 |= flags & COMMANDS_RECURSE;
1013 /* Figure out an argument list from this command line. */
1016 char *end = 0;
1017 #ifdef VMS
1018 argv = p;
1019 #else
1020 argv = construct_command_argv (p, &end, child->file,
1021 child->file->cmds->lines_flags[child->command_line - 1],
1022 &child->sh_batch_file);
1023 #endif
1024 if (end == NULL)
1025 child->command_ptr = NULL;
1026 else
1028 *end++ = '\0';
1029 child->command_ptr = end;
1033 /* If -q was given, say that updating `failed' if there was any text on the
1034 command line, or `succeeded' otherwise. The exit status of 1 tells the
1035 user that -q is saying `something to do'; the exit status for a random
1036 error is 2. */
1037 if (argv != 0 && question_flag && !(flags & COMMANDS_RECURSE))
1039 #ifndef VMS
1040 free (argv[0]);
1041 free (argv);
1042 #endif
1043 child->file->update_status = 1;
1044 notice_finished_file (child->file);
1045 return;
1048 if (touch_flag && !(flags & COMMANDS_RECURSE))
1050 /* Go on to the next command. It might be the recursive one.
1051 We construct ARGV only to find the end of the command line. */
1052 #ifndef VMS
1053 if (argv)
1055 free (argv[0]);
1056 free (argv);
1058 #endif
1059 argv = 0;
1062 if (argv == 0)
1064 next_command:
1065 #ifdef __MSDOS__
1066 execute_by_shell = 0; /* in case construct_command_argv sets it */
1067 #endif
1068 /* This line has no commands. Go to the next. */
1069 if (job_next_command (child))
1070 start_job_command (child);
1071 else
1073 /* No more commands. Make sure we're "running"; we might not be if
1074 (e.g.) all commands were skipped due to -n. */
1075 set_command_state (child->file, cs_running);
1076 child->file->update_status = 0;
1077 notice_finished_file (child->file);
1079 return;
1082 /* Print out the command. If silent, we call `message' with null so it
1083 can log the working directory before the command's own error messages
1084 appear. */
1086 message (0, (just_print_flag || (!(flags & COMMANDS_SILENT) && !silent_flag))
1087 ? "%s" : (char *) 0, p);
1089 /* Tell update_goal_chain that a command has been started on behalf of
1090 this target. It is important that this happens here and not in
1091 reap_children (where we used to do it), because reap_children might be
1092 reaping children from a different target. We want this increment to
1093 guaranteedly indicate that a command was started for the dependency
1094 chain (i.e., update_file recursion chain) we are processing. */
1096 ++commands_started;
1098 /* Optimize an empty command. People use this for timestamp rules,
1099 so avoid forking a useless shell. Do this after we increment
1100 commands_started so make still treats this special case as if it
1101 performed some action (makes a difference as to what messages are
1102 printed, etc. */
1104 #if !defined(VMS) && !defined(_AMIGA)
1105 if (
1106 #if defined __MSDOS__ || defined (__EMX__)
1107 unixy_shell /* the test is complicated and we already did it */
1108 #else
1109 (argv[0] && !strcmp (argv[0], "/bin/sh"))
1110 #endif
1111 && (argv[1]
1112 && argv[1][0] == '-' && argv[1][1] == 'c' && argv[1][2] == '\0')
1113 && (argv[2] && argv[2][0] == ':' && argv[2][1] == '\0')
1114 && argv[3] == NULL)
1116 free (argv[0]);
1117 free (argv);
1118 goto next_command;
1120 #endif /* !VMS && !_AMIGA */
1122 /* If -n was given, recurse to get the next line in the sequence. */
1124 if (just_print_flag && !(flags & COMMANDS_RECURSE))
1126 #ifndef VMS
1127 free (argv[0]);
1128 free (argv);
1129 #endif
1130 goto next_command;
1133 /* Flush the output streams so they won't have things written twice. */
1135 fflush (stdout);
1136 fflush (stderr);
1138 #ifndef VMS
1139 #if !defined(WINDOWS32) && !defined(_AMIGA) && !defined(__MSDOS__)
1141 /* Set up a bad standard input that reads from a broken pipe. */
1143 if (bad_stdin == -1)
1145 /* Make a file descriptor that is the read end of a broken pipe.
1146 This will be used for some children's standard inputs. */
1147 int pd[2];
1148 if (pipe (pd) == 0)
1150 /* Close the write side. */
1151 (void) close (pd[1]);
1152 /* Save the read side. */
1153 bad_stdin = pd[0];
1155 /* Set the descriptor to close on exec, so it does not litter any
1156 child's descriptor table. When it is dup2'd onto descriptor 0,
1157 that descriptor will not close on exec. */
1158 CLOSE_ON_EXEC (bad_stdin);
1162 #endif /* !WINDOWS32 && !_AMIGA && !__MSDOS__ */
1164 /* Decide whether to give this child the `good' standard input
1165 (one that points to the terminal or whatever), or the `bad' one
1166 that points to the read side of a broken pipe. */
1168 child->good_stdin = !good_stdin_used;
1169 if (child->good_stdin)
1170 good_stdin_used = 1;
1172 #endif /* !VMS */
1174 child->deleted = 0;
1176 #ifndef _AMIGA
1177 /* Set up the environment for the child. */
1178 if (child->environment == 0)
1179 child->environment = target_environment (child->file);
1180 #endif
1182 #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
1184 #ifndef VMS
1185 /* start_waiting_job has set CHILD->remote if we can start a remote job. */
1186 if (child->remote)
1188 int is_remote, id, used_stdin;
1189 if (start_remote_job (argv, child->environment,
1190 child->good_stdin ? 0 : bad_stdin,
1191 &is_remote, &id, &used_stdin))
1192 /* Don't give up; remote execution may fail for various reasons. If
1193 so, simply run the job locally. */
1194 goto run_local;
1195 else
1197 if (child->good_stdin && !used_stdin)
1199 child->good_stdin = 0;
1200 good_stdin_used = 0;
1202 child->remote = is_remote;
1203 child->pid = id;
1206 else
1207 #endif /* !VMS */
1209 /* Fork the child process. */
1211 char **parent_environ;
1213 run_local:
1214 block_sigs ();
1216 child->remote = 0;
1218 #ifdef VMS
1219 if (!child_execute_job (argv, child)) {
1220 /* Fork failed! */
1221 perror_with_name ("vfork", "");
1222 goto error;
1225 #else
1227 parent_environ = environ;
1229 # ifdef __EMX__
1230 /* If we aren't running a recursive command and we have a jobserver
1231 pipe, close it before exec'ing. */
1232 if (!(flags & COMMANDS_RECURSE) && job_fds[0] >= 0)
1234 CLOSE_ON_EXEC (job_fds[0]);
1235 CLOSE_ON_EXEC (job_fds[1]);
1237 if (job_rfd >= 0)
1238 CLOSE_ON_EXEC (job_rfd);
1240 /* Never use fork()/exec() here! Use spawn() instead in exec_command() */
1241 child->pid = child_execute_job (child->good_stdin ? 0 : bad_stdin, 1,
1242 argv, child->environment);
1243 if (child->pid < 0)
1245 /* spawn failed! */
1246 unblock_sigs ();
1247 perror_with_name ("spawn", "");
1248 goto error;
1251 /* undo CLOSE_ON_EXEC() after the child process has been started */
1252 if (!(flags & COMMANDS_RECURSE) && job_fds[0] >= 0)
1254 fcntl (job_fds[0], F_SETFD, 0);
1255 fcntl (job_fds[1], F_SETFD, 0);
1257 if (job_rfd >= 0)
1258 fcntl (job_rfd, F_SETFD, 0);
1260 #else /* !__EMX__ */
1262 child->pid = vfork ();
1263 environ = parent_environ; /* Restore value child may have clobbered. */
1264 if (child->pid == 0)
1266 /* We are the child side. */
1267 unblock_sigs ();
1269 /* If we aren't running a recursive command and we have a jobserver
1270 pipe, close it before exec'ing. */
1271 if (!(flags & COMMANDS_RECURSE) && job_fds[0] >= 0)
1273 close (job_fds[0]);
1274 close (job_fds[1]);
1276 if (job_rfd >= 0)
1277 close (job_rfd);
1279 #ifdef SET_STACK_SIZE
1280 /* Reset limits, if necessary. */
1281 if (stack_limit.rlim_cur)
1282 setrlimit (RLIMIT_STACK, &stack_limit);
1283 #endif
1285 child_execute_job (child->good_stdin ? 0 : bad_stdin, 1,
1286 argv, child->environment);
1288 else if (child->pid < 0)
1290 /* Fork failed! */
1291 unblock_sigs ();
1292 perror_with_name ("vfork", "");
1293 goto error;
1295 # endif /* !__EMX__ */
1296 #endif /* !VMS */
1299 #else /* __MSDOS__ or Amiga or WINDOWS32 */
1300 #ifdef __MSDOS__
1302 int proc_return;
1304 block_sigs ();
1305 dos_status = 0;
1307 /* We call `system' to do the job of the SHELL, since stock DOS
1308 shell is too dumb. Our `system' knows how to handle long
1309 command lines even if pipes/redirection is needed; it will only
1310 call COMMAND.COM when its internal commands are used. */
1311 if (execute_by_shell)
1313 char *cmdline = argv[0];
1314 /* We don't have a way to pass environment to `system',
1315 so we need to save and restore ours, sigh... */
1316 char **parent_environ = environ;
1318 environ = child->environment;
1320 /* If we have a *real* shell, tell `system' to call
1321 it to do everything for us. */
1322 if (unixy_shell)
1324 /* A *real* shell on MSDOS may not support long
1325 command lines the DJGPP way, so we must use `system'. */
1326 cmdline = argv[2]; /* get past "shell -c" */
1329 dos_command_running = 1;
1330 proc_return = system (cmdline);
1331 environ = parent_environ;
1332 execute_by_shell = 0; /* for the next time */
1334 else
1336 dos_command_running = 1;
1337 proc_return = spawnvpe (P_WAIT, argv[0], argv, child->environment);
1340 /* Need to unblock signals before turning off
1341 dos_command_running, so that child's signals
1342 will be treated as such (see fatal_error_signal). */
1343 unblock_sigs ();
1344 dos_command_running = 0;
1346 /* If the child got a signal, dos_status has its
1347 high 8 bits set, so be careful not to alter them. */
1348 if (proc_return == -1)
1349 dos_status |= 0xff;
1350 else
1351 dos_status |= (proc_return & 0xff);
1352 ++dead_children;
1353 child->pid = dos_pid++;
1355 #endif /* __MSDOS__ */
1356 #ifdef _AMIGA
1357 amiga_status = MyExecute (argv);
1359 ++dead_children;
1360 child->pid = amiga_pid++;
1361 if (amiga_batch_file)
1363 amiga_batch_file = 0;
1364 DeleteFile (amiga_bname); /* Ignore errors. */
1366 #endif /* Amiga */
1367 #ifdef WINDOWS32
1369 HANDLE hPID;
1370 char* arg0;
1372 /* make UNC paths safe for CreateProcess -- backslash format */
1373 arg0 = argv[0];
1374 if (arg0 && arg0[0] == '/' && arg0[1] == '/')
1375 for ( ; arg0 && *arg0; arg0++)
1376 if (*arg0 == '/')
1377 *arg0 = '\\';
1379 /* make sure CreateProcess() has Path it needs */
1380 sync_Path_environment();
1382 hPID = process_easy(argv, child->environment);
1384 if (hPID != INVALID_HANDLE_VALUE)
1385 child->pid = (int) hPID;
1386 else {
1387 int i;
1388 unblock_sigs();
1389 fprintf(stderr,
1390 _("process_easy() failed to launch process (e=%ld)\n"),
1391 process_last_err(hPID));
1392 for (i = 0; argv[i]; i++)
1393 fprintf(stderr, "%s ", argv[i]);
1394 fprintf(stderr, _("\nCounted %d args in failed launch\n"), i);
1395 goto error;
1398 #endif /* WINDOWS32 */
1399 #endif /* __MSDOS__ or Amiga or WINDOWS32 */
1401 /* Bump the number of jobs started in this second. */
1402 ++job_counter;
1404 /* We are the parent side. Set the state to
1405 say the commands are running and return. */
1407 set_command_state (child->file, cs_running);
1409 /* Free the storage used by the child's argument list. */
1410 #ifndef VMS
1411 free (argv[0]);
1412 free (argv);
1413 #endif
1415 return;
1417 error:
1418 child->file->update_status = 2;
1419 notice_finished_file (child->file);
1420 return;
1423 /* Try to start a child running.
1424 Returns nonzero if the child was started (and maybe finished), or zero if
1425 the load was too high and the child was put on the `waiting_jobs' chain. */
1427 static int
1428 start_waiting_job (struct child *c)
1430 struct file *f = c->file;
1432 /* If we can start a job remotely, we always want to, and don't care about
1433 the local load average. We record that the job should be started
1434 remotely in C->remote for start_job_command to test. */
1436 c->remote = start_remote_job_p (1);
1438 /* If we are running at least one job already and the load average
1439 is too high, make this one wait. */
1440 if (!c->remote
1441 && ((job_slots_used > 0 && load_too_high ())
1442 #ifdef WINDOWS32
1443 || (process_used_slots () >= MAXIMUM_WAIT_OBJECTS)
1444 #endif
1447 /* Put this child on the chain of children waiting for the load average
1448 to go down. */
1449 set_command_state (f, cs_running);
1450 c->next = waiting_jobs;
1451 waiting_jobs = c;
1452 return 0;
1455 /* Start the first command; reap_children will run later command lines. */
1456 start_job_command (c);
1458 switch (f->command_state)
1460 case cs_running:
1461 c->next = children;
1462 DB (DB_JOBS, (_("Putting child 0x%08lx (%s) PID %ld%s on the chain.\n"),
1463 (unsigned long int) c, c->file->name,
1464 (long) c->pid, c->remote ? _(" (remote)") : ""));
1465 children = c;
1466 /* One more job slot is in use. */
1467 ++job_slots_used;
1468 unblock_sigs ();
1469 break;
1471 case cs_not_started:
1472 /* All the command lines turned out to be empty. */
1473 f->update_status = 0;
1474 /* FALLTHROUGH */
1476 case cs_finished:
1477 notice_finished_file (f);
1478 free_child (c);
1479 break;
1481 default:
1482 assert (f->command_state == cs_finished);
1483 break;
1486 return 1;
1489 /* Create a `struct child' for FILE and start its commands running. */
1491 void
1492 new_job (struct file *file)
1494 struct commands *cmds = file->cmds;
1495 struct child *c;
1496 char **lines;
1497 unsigned int i;
1499 /* Let any previously decided-upon jobs that are waiting
1500 for the load to go down start before this new one. */
1501 start_waiting_jobs ();
1503 /* Reap any children that might have finished recently. */
1504 reap_children (0, 0);
1506 /* Chop the commands up into lines if they aren't already. */
1507 chop_commands (cmds);
1509 /* Expand the command lines and store the results in LINES. */
1510 lines = xmalloc (cmds->ncommand_lines * sizeof (char *));
1511 for (i = 0; i < cmds->ncommand_lines; ++i)
1513 /* Collapse backslash-newline combinations that are inside variable
1514 or function references. These are left alone by the parser so
1515 that they will appear in the echoing of commands (where they look
1516 nice); and collapsed by construct_command_argv when it tokenizes.
1517 But letting them survive inside function invocations loses because
1518 we don't want the functions to see them as part of the text. */
1520 char *in, *out, *ref;
1522 /* IN points to where in the line we are scanning.
1523 OUT points to where in the line we are writing.
1524 When we collapse a backslash-newline combination,
1525 IN gets ahead of OUT. */
1527 in = out = cmds->command_lines[i];
1528 while ((ref = strchr (in, '$')) != 0)
1530 ++ref; /* Move past the $. */
1532 if (out != in)
1533 /* Copy the text between the end of the last chunk
1534 we processed (where IN points) and the new chunk
1535 we are about to process (where REF points). */
1536 memmove (out, in, ref - in);
1538 /* Move both pointers past the boring stuff. */
1539 out += ref - in;
1540 in = ref;
1542 if (*ref == '(' || *ref == '{')
1544 char openparen = *ref;
1545 char closeparen = openparen == '(' ? ')' : '}';
1546 int count;
1547 char *p;
1549 *out++ = *in++; /* Copy OPENPAREN. */
1550 /* IN now points past the opening paren or brace.
1551 Count parens or braces until it is matched. */
1552 count = 0;
1553 while (*in != '\0')
1555 if (*in == closeparen && --count < 0)
1556 break;
1557 else if (*in == '\\' && in[1] == '\n')
1559 /* We have found a backslash-newline inside a
1560 variable or function reference. Eat it and
1561 any following whitespace. */
1563 int quoted = 0;
1564 for (p = in - 1; p > ref && *p == '\\'; --p)
1565 quoted = !quoted;
1567 if (quoted)
1568 /* There were two or more backslashes, so this is
1569 not really a continuation line. We don't collapse
1570 the quoting backslashes here as is done in
1571 collapse_continuations, because the line will
1572 be collapsed again after expansion. */
1573 *out++ = *in++;
1574 else
1576 /* Skip the backslash, newline and
1577 any following whitespace. */
1578 in = next_token (in + 2);
1580 /* Discard any preceding whitespace that has
1581 already been written to the output. */
1582 while (out > ref
1583 && isblank ((unsigned char)out[-1]))
1584 --out;
1586 /* Replace it all with a single space. */
1587 *out++ = ' ';
1590 else
1592 if (*in == openparen)
1593 ++count;
1595 *out++ = *in++;
1601 /* There are no more references in this line to worry about.
1602 Copy the remaining uninteresting text to the output. */
1603 if (out != in)
1604 memmove (out, in, strlen (in) + 1);
1606 /* Finally, expand the line. */
1607 lines[i] = allocated_variable_expand_for_file (cmds->command_lines[i],
1608 file);
1611 /* Start the command sequence, record it in a new
1612 `struct child', and add that to the chain. */
1614 c = xmalloc (sizeof (struct child));
1615 memset (c, '\0', sizeof (struct child));
1616 c->file = file;
1617 c->command_lines = lines;
1618 c->sh_batch_file = NULL;
1620 /* Cache dontcare flag because file->dontcare can be changed once we
1621 return. Check dontcare inheritance mechanism for details. */
1622 c->dontcare = file->dontcare;
1624 /* Fetch the first command line to be run. */
1625 job_next_command (c);
1627 /* Wait for a job slot to be freed up. If we allow an infinite number
1628 don't bother; also job_slots will == 0 if we're using the jobserver. */
1630 if (job_slots != 0)
1631 while (job_slots_used == job_slots)
1632 reap_children (1, 0);
1634 #ifdef MAKE_JOBSERVER
1635 /* If we are controlling multiple jobs make sure we have a token before
1636 starting the child. */
1638 /* This can be inefficient. There's a decent chance that this job won't
1639 actually have to run any subprocesses: the command script may be empty
1640 or otherwise optimized away. It would be nice if we could defer
1641 obtaining a token until just before we need it, in start_job_command.
1642 To do that we'd need to keep track of whether we'd already obtained a
1643 token (since start_job_command is called for each line of the job, not
1644 just once). Also more thought needs to go into the entire algorithm;
1645 this is where the old parallel job code waits, so... */
1647 else if (job_fds[0] >= 0)
1648 while (1)
1650 char token;
1651 int got_token;
1652 int saved_errno;
1654 DB (DB_JOBS, ("Need a job token; we %shave children\n",
1655 children ? "" : "don't "));
1657 /* If we don't already have a job started, use our "free" token. */
1658 if (!jobserver_tokens)
1659 break;
1661 /* Read a token. As long as there's no token available we'll block.
1662 We enable interruptible system calls before the read(2) so that if
1663 we get a SIGCHLD while we're waiting, we'll return with EINTR and
1664 we can process the death(s) and return tokens to the free pool.
1666 Once we return from the read, we immediately reinstate restartable
1667 system calls. This allows us to not worry about checking for
1668 EINTR on all the other system calls in the program.
1670 There is one other twist: there is a span between the time
1671 reap_children() does its last check for dead children and the time
1672 the read(2) call is entered, below, where if a child dies we won't
1673 notice. This is extremely serious as it could cause us to
1674 deadlock, given the right set of events.
1676 To avoid this, we do the following: before we reap_children(), we
1677 dup(2) the read FD on the jobserver pipe. The read(2) call below
1678 uses that new FD. In the signal handler, we close that FD. That
1679 way, if a child dies during the section mentioned above, the
1680 read(2) will be invoked with an invalid FD and will return
1681 immediately with EBADF. */
1683 /* Make sure we have a dup'd FD. */
1684 if (job_rfd < 0)
1686 DB (DB_JOBS, ("Duplicate the job FD\n"));
1687 job_rfd = dup (job_fds[0]);
1690 /* Reap anything that's currently waiting. */
1691 reap_children (0, 0);
1693 /* Kick off any jobs we have waiting for an opportunity that
1694 can run now (ie waiting for load). */
1695 start_waiting_jobs ();
1697 /* If our "free" slot has become available, use it; we don't need an
1698 actual token. */
1699 if (!jobserver_tokens)
1700 break;
1702 /* There must be at least one child already, or we have no business
1703 waiting for a token. */
1704 if (!children)
1705 fatal (NILF, "INTERNAL: no children as we go to sleep on read\n");
1707 /* Set interruptible system calls, and read() for a job token. */
1708 set_child_handler_action_flags (1, waiting_jobs != NULL);
1709 got_token = read (job_rfd, &token, 1);
1710 saved_errno = errno;
1711 set_child_handler_action_flags (0, waiting_jobs != NULL);
1713 /* If we got one, we're done here. */
1714 if (got_token == 1)
1716 DB (DB_JOBS, (_("Obtained token for child 0x%08lx (%s).\n"),
1717 (unsigned long int) c, c->file->name));
1718 break;
1721 /* If the error _wasn't_ expected (EINTR or EBADF), punt. Otherwise,
1722 go back and reap_children(), and try again. */
1723 errno = saved_errno;
1724 if (errno != EINTR && errno != EBADF)
1725 pfatal_with_name (_("read jobs pipe"));
1726 if (errno == EBADF)
1727 DB (DB_JOBS, ("Read returned EBADF.\n"));
1729 #endif
1731 ++jobserver_tokens;
1733 /* The job is now primed. Start it running.
1734 (This will notice if there is in fact no recipe.) */
1735 if (cmds->fileinfo.filenm)
1736 DB (DB_BASIC, (_("Invoking recipe from %s:%lu to update target `%s'.\n"),
1737 cmds->fileinfo.filenm, cmds->fileinfo.lineno,
1738 c->file->name));
1739 else
1740 DB (DB_BASIC, (_("Invoking builtin recipe to update target `%s'.\n"),
1741 c->file->name));
1744 start_waiting_job (c);
1746 if (job_slots == 1 || not_parallel)
1747 /* Since there is only one job slot, make things run linearly.
1748 Wait for the child to die, setting the state to `cs_finished'. */
1749 while (file->command_state == cs_running)
1750 reap_children (1, 0);
1752 return;
1755 /* Move CHILD's pointers to the next command for it to execute.
1756 Returns nonzero if there is another command. */
1758 static int
1759 job_next_command (struct child *child)
1761 while (child->command_ptr == 0 || *child->command_ptr == '\0')
1763 /* There are no more lines in the expansion of this line. */
1764 if (child->command_line == child->file->cmds->ncommand_lines)
1766 /* There are no more lines to be expanded. */
1767 child->command_ptr = 0;
1768 return 0;
1770 else
1771 /* Get the next line to run. */
1772 child->command_ptr = child->command_lines[child->command_line++];
1774 return 1;
1777 /* Determine if the load average on the system is too high to start a new job.
1778 The real system load average is only recomputed once a second. However, a
1779 very parallel make can easily start tens or even hundreds of jobs in a
1780 second, which brings the system to its knees for a while until that first
1781 batch of jobs clears out.
1783 To avoid this we use a weighted algorithm to try to account for jobs which
1784 have been started since the last second, and guess what the load average
1785 would be now if it were computed.
1787 This algorithm was provided by Thomas Riedl <thomas.riedl@siemens.com>,
1788 who writes:
1790 ! calculate something load-oid and add to the observed sys.load,
1791 ! so that latter can catch up:
1792 ! - every job started increases jobctr;
1793 ! - every dying job decreases a positive jobctr;
1794 ! - the jobctr value gets zeroed every change of seconds,
1795 ! after its value*weight_b is stored into the 'backlog' value last_sec
1796 ! - weight_a times the sum of jobctr and last_sec gets
1797 ! added to the observed sys.load.
1799 ! The two weights have been tried out on 24 and 48 proc. Sun Solaris-9
1800 ! machines, using a several-thousand-jobs-mix of cpp, cc, cxx and smallish
1801 ! sub-shelled commands (rm, echo, sed...) for tests.
1802 ! lowering the 'direct influence' factor weight_a (e.g. to 0.1)
1803 ! resulted in significant excession of the load limit, raising it
1804 ! (e.g. to 0.5) took bad to small, fast-executing jobs and didn't
1805 ! reach the limit in most test cases.
1807 ! lowering the 'history influence' weight_b (e.g. to 0.1) resulted in
1808 ! exceeding the limit for longer-running stuff (compile jobs in
1809 ! the .5 to 1.5 sec. range),raising it (e.g. to 0.5) overrepresented
1810 ! small jobs' effects.
1814 #define LOAD_WEIGHT_A 0.25
1815 #define LOAD_WEIGHT_B 0.25
1817 static int
1818 load_too_high (void)
1820 #if defined(__MSDOS__) || defined(VMS) || defined(_AMIGA) || defined(__riscos__)
1821 return 1;
1822 #else
1823 static double last_sec;
1824 static time_t last_now;
1825 double load, guess;
1826 time_t now;
1828 #ifdef WINDOWS32
1829 /* sub_proc.c cannot wait for more than MAXIMUM_WAIT_OBJECTS children */
1830 if (process_used_slots () >= MAXIMUM_WAIT_OBJECTS)
1831 return 1;
1832 #endif
1834 if (max_load_average < 0)
1835 return 0;
1837 /* Find the real system load average. */
1838 make_access ();
1839 if (getloadavg (&load, 1) != 1)
1841 static int lossage = -1;
1842 /* Complain only once for the same error. */
1843 if (lossage == -1 || errno != lossage)
1845 if (errno == 0)
1846 /* An errno value of zero means getloadavg is just unsupported. */
1847 error (NILF,
1848 _("cannot enforce load limits on this operating system"));
1849 else
1850 perror_with_name (_("cannot enforce load limit: "), "getloadavg");
1852 lossage = errno;
1853 load = 0;
1855 user_access ();
1857 /* If we're in a new second zero the counter and correct the backlog
1858 value. Only keep the backlog for one extra second; after that it's 0. */
1859 now = time (NULL);
1860 if (last_now < now)
1862 if (last_now == now - 1)
1863 last_sec = LOAD_WEIGHT_B * job_counter;
1864 else
1865 last_sec = 0.0;
1867 job_counter = 0;
1868 last_now = now;
1871 /* Try to guess what the load would be right now. */
1872 guess = load + (LOAD_WEIGHT_A * (job_counter + last_sec));
1874 DB (DB_JOBS, ("Estimated system load = %f (actual = %f) (max requested = %f)\n",
1875 guess, load, max_load_average));
1877 return guess >= max_load_average;
1878 #endif
1881 /* Start jobs that are waiting for the load to be lower. */
1883 void
1884 start_waiting_jobs (void)
1886 struct child *job;
1888 if (waiting_jobs == 0)
1889 return;
1893 /* Check for recently deceased descendants. */
1894 reap_children (0, 0);
1896 /* Take a job off the waiting list. */
1897 job = waiting_jobs;
1898 waiting_jobs = job->next;
1900 /* Try to start that job. We break out of the loop as soon
1901 as start_waiting_job puts one back on the waiting list. */
1903 while (start_waiting_job (job) && waiting_jobs != 0);
1905 return;
1908 #ifndef WINDOWS32
1910 /* EMX: Start a child process. This function returns the new pid. */
1911 # if defined __EMX__
1913 child_execute_job (int stdin_fd, int stdout_fd, char **argv, char **envp)
1915 int pid;
1916 /* stdin_fd == 0 means: nothing to do for stdin;
1917 stdout_fd == 1 means: nothing to do for stdout */
1918 int save_stdin = (stdin_fd != 0) ? dup (0) : 0;
1919 int save_stdout = (stdout_fd != 1) ? dup (1): 1;
1921 /* < 0 only if dup() failed */
1922 if (save_stdin < 0)
1923 fatal (NILF, _("no more file handles: could not duplicate stdin\n"));
1924 if (save_stdout < 0)
1925 fatal (NILF, _("no more file handles: could not duplicate stdout\n"));
1927 /* Close unnecessary file handles for the child. */
1928 if (save_stdin != 0)
1929 CLOSE_ON_EXEC (save_stdin);
1930 if (save_stdout != 1)
1931 CLOSE_ON_EXEC (save_stdout);
1933 /* Connect the pipes to the child process. */
1934 if (stdin_fd != 0)
1935 (void) dup2 (stdin_fd, 0);
1936 if (stdout_fd != 1)
1937 (void) dup2 (stdout_fd, 1);
1939 /* stdin_fd and stdout_fd must be closed on exit because we are
1940 still in the parent process */
1941 if (stdin_fd != 0)
1942 CLOSE_ON_EXEC (stdin_fd);
1943 if (stdout_fd != 1)
1944 CLOSE_ON_EXEC (stdout_fd);
1946 /* Run the command. */
1947 pid = exec_command (argv, envp);
1949 /* Restore stdout/stdin of the parent and close temporary FDs. */
1950 if (stdin_fd != 0)
1952 if (dup2 (save_stdin, 0) != 0)
1953 fatal (NILF, _("Could not restore stdin\n"));
1954 else
1955 close (save_stdin);
1958 if (stdout_fd != 1)
1960 if (dup2 (save_stdout, 1) != 1)
1961 fatal (NILF, _("Could not restore stdout\n"));
1962 else
1963 close (save_stdout);
1966 return pid;
1969 #elif !defined (_AMIGA) && !defined (__MSDOS__) && !defined (VMS)
1971 /* UNIX:
1972 Replace the current process with one executing the command in ARGV.
1973 STDIN_FD and STDOUT_FD are used as the process's stdin and stdout; ENVP is
1974 the environment of the new program. This function does not return. */
1975 void
1976 child_execute_job (int stdin_fd, int stdout_fd, char **argv, char **envp)
1978 if (stdin_fd != 0)
1979 (void) dup2 (stdin_fd, 0);
1980 if (stdout_fd != 1)
1981 (void) dup2 (stdout_fd, 1);
1982 if (stdin_fd != 0)
1983 (void) close (stdin_fd);
1984 if (stdout_fd != 1)
1985 (void) close (stdout_fd);
1987 /* Run the command. */
1988 exec_command (argv, envp);
1990 #endif /* !AMIGA && !__MSDOS__ && !VMS */
1991 #endif /* !WINDOWS32 */
1993 #ifndef _AMIGA
1994 /* Replace the current process with one running the command in ARGV,
1995 with environment ENVP. This function does not return. */
1997 /* EMX: This function returns the pid of the child process. */
1998 # ifdef __EMX__
2000 # else
2001 void
2002 # endif
2003 exec_command (char **argv, char **envp)
2005 #ifdef VMS
2006 /* to work around a problem with signals and execve: ignore them */
2007 #ifdef SIGCHLD
2008 signal (SIGCHLD,SIG_IGN);
2009 #endif
2010 /* Run the program. */
2011 execve (argv[0], argv, envp);
2012 perror_with_name ("execve: ", argv[0]);
2013 _exit (EXIT_FAILURE);
2014 #else
2015 #ifdef WINDOWS32
2016 HANDLE hPID;
2017 HANDLE hWaitPID;
2018 int err = 0;
2019 int exit_code = EXIT_FAILURE;
2021 /* make sure CreateProcess() has Path it needs */
2022 sync_Path_environment();
2024 /* launch command */
2025 hPID = process_easy(argv, envp);
2027 /* make sure launch ok */
2028 if (hPID == INVALID_HANDLE_VALUE)
2030 int i;
2031 fprintf(stderr,
2032 _("process_easy() failed to launch process (e=%ld)\n"),
2033 process_last_err(hPID));
2034 for (i = 0; argv[i]; i++)
2035 fprintf(stderr, "%s ", argv[i]);
2036 fprintf(stderr, _("\nCounted %d args in failed launch\n"), i);
2037 exit(EXIT_FAILURE);
2040 /* wait and reap last child */
2041 hWaitPID = process_wait_for_any();
2042 while (hWaitPID)
2044 /* was an error found on this process? */
2045 err = process_last_err(hWaitPID);
2047 /* get exit data */
2048 exit_code = process_exit_code(hWaitPID);
2050 if (err)
2051 fprintf(stderr, "make (e=%d, rc=%d): %s",
2052 err, exit_code, map_windows32_error_to_string(err));
2054 /* cleanup process */
2055 process_cleanup(hWaitPID);
2057 /* expect to find only last pid, warn about other pids reaped */
2058 if (hWaitPID == hPID)
2059 break;
2060 else
2061 fprintf(stderr,
2062 _("make reaped child pid %ld, still waiting for pid %ld\n"),
2063 (DWORD)hWaitPID, (DWORD)hPID);
2066 /* return child's exit code as our exit code */
2067 exit(exit_code);
2069 #else /* !WINDOWS32 */
2071 # ifdef __EMX__
2072 int pid;
2073 # endif
2075 /* Be the user, permanently. */
2076 child_access ();
2078 # ifdef __EMX__
2080 /* Run the program. */
2081 pid = spawnvpe (P_NOWAIT, argv[0], argv, envp);
2083 if (pid >= 0)
2084 return pid;
2086 /* the file might have a strange shell extension */
2087 if (errno == ENOENT)
2088 errno = ENOEXEC;
2090 # else
2092 /* Run the program. */
2093 environ = envp;
2094 execvp (argv[0], argv);
2096 # endif /* !__EMX__ */
2098 switch (errno)
2100 case ENOENT:
2101 error (NILF, _("%s: Command not found"), argv[0]);
2102 break;
2103 case ENOEXEC:
2105 /* The file is not executable. Try it as a shell script. */
2106 extern char *getenv ();
2107 char *shell;
2108 char **new_argv;
2109 int argc;
2110 int i=1;
2112 # ifdef __EMX__
2113 /* Do not use $SHELL from the environment */
2114 struct variable *p = lookup_variable ("SHELL", 5);
2115 if (p)
2116 shell = p->value;
2117 else
2118 shell = 0;
2119 # else
2120 shell = getenv ("SHELL");
2121 # endif
2122 if (shell == 0)
2123 shell = default_shell;
2125 argc = 1;
2126 while (argv[argc] != 0)
2127 ++argc;
2129 # ifdef __EMX__
2130 if (!unixy_shell)
2131 ++argc;
2132 # endif
2134 new_argv = alloca ((1 + argc + 1) * sizeof (char *));
2135 new_argv[0] = shell;
2137 # ifdef __EMX__
2138 if (!unixy_shell)
2140 new_argv[1] = "/c";
2141 ++i;
2142 --argc;
2144 # endif
2146 new_argv[i] = argv[0];
2147 while (argc > 0)
2149 new_argv[i + argc] = argv[argc];
2150 --argc;
2153 # ifdef __EMX__
2154 pid = spawnvpe (P_NOWAIT, shell, new_argv, envp);
2155 if (pid >= 0)
2156 break;
2157 # else
2158 execvp (shell, new_argv);
2159 # endif
2160 if (errno == ENOENT)
2161 error (NILF, _("%s: Shell program not found"), shell);
2162 else
2163 perror_with_name ("execvp: ", shell);
2164 break;
2167 # ifdef __EMX__
2168 case EINVAL:
2169 /* this nasty error was driving me nuts :-( */
2170 error (NILF, _("spawnvpe: environment space might be exhausted"));
2171 /* FALLTHROUGH */
2172 # endif
2174 default:
2175 perror_with_name ("execvp: ", argv[0]);
2176 break;
2179 # ifdef __EMX__
2180 return pid;
2181 # else
2182 _exit (127);
2183 # endif
2184 #endif /* !WINDOWS32 */
2185 #endif /* !VMS */
2187 #else /* On Amiga */
2188 void exec_command (char **argv)
2190 MyExecute (argv);
2193 void clean_tmp (void)
2195 DeleteFile (amiga_bname);
2198 #endif /* On Amiga */
2200 #ifndef VMS
2201 /* Figure out the argument list necessary to run LINE as a command. Try to
2202 avoid using a shell. This routine handles only ' quoting, and " quoting
2203 when no backslash, $ or ` characters are seen in the quotes. Starting
2204 quotes may be escaped with a backslash. If any of the characters in
2205 sh_chars[] is seen, or any of the builtin commands listed in sh_cmds[]
2206 is the first word of a line, the shell is used.
2208 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
2209 If *RESTP is NULL, newlines will be ignored.
2211 SHELL is the shell to use, or nil to use the default shell.
2212 IFS is the value of $IFS, or nil (meaning the default).
2214 FLAGS is the value of lines_flags for this command line. It is
2215 used in the WINDOWS32 port to check whether + or $(MAKE) were found
2216 in this command line, in which case the effect of just_print_flag
2217 is overridden. */
2219 static char **
2220 construct_command_argv_internal (char *line, char **restp, char *shell,
2221 char *ifs, int flags,
2222 char **batch_filename_ptr)
2224 #ifdef __MSDOS__
2225 /* MSDOS supports both the stock DOS shell and ports of Unixy shells.
2226 We call `system' for anything that requires ``slow'' processing,
2227 because DOS shells are too dumb. When $SHELL points to a real
2228 (unix-style) shell, `system' just calls it to do everything. When
2229 $SHELL points to a DOS shell, `system' does most of the work
2230 internally, calling the shell only for its internal commands.
2231 However, it looks on the $PATH first, so you can e.g. have an
2232 external command named `mkdir'.
2234 Since we call `system', certain characters and commands below are
2235 actually not specific to COMMAND.COM, but to the DJGPP implementation
2236 of `system'. In particular:
2238 The shell wildcard characters are in DOS_CHARS because they will
2239 not be expanded if we call the child via `spawnXX'.
2241 The `;' is in DOS_CHARS, because our `system' knows how to run
2242 multiple commands on a single line.
2244 DOS_CHARS also include characters special to 4DOS/NDOS, so we
2245 won't have to tell one from another and have one more set of
2246 commands and special characters. */
2247 static char sh_chars_dos[] = "*?[];|<>%^&()";
2248 static char *sh_cmds_dos[] = { "break", "call", "cd", "chcp", "chdir", "cls",
2249 "copy", "ctty", "date", "del", "dir", "echo",
2250 "erase", "exit", "for", "goto", "if", "md",
2251 "mkdir", "path", "pause", "prompt", "rd",
2252 "rmdir", "rem", "ren", "rename", "set",
2253 "shift", "time", "type", "ver", "verify",
2254 "vol", ":", 0 };
2256 static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^";
2257 static char *sh_cmds_sh[] = { "cd", "echo", "eval", "exec", "exit", "login",
2258 "logout", "set", "umask", "wait", "while",
2259 "for", "case", "if", ":", ".", "break",
2260 "continue", "export", "read", "readonly",
2261 "shift", "times", "trap", "switch", "unset",
2262 "ulimit", 0 };
2264 char *sh_chars;
2265 char **sh_cmds;
2266 #elif defined (__EMX__)
2267 static char sh_chars_dos[] = "*?[];|<>%^&()";
2268 static char *sh_cmds_dos[] = { "break", "call", "cd", "chcp", "chdir", "cls",
2269 "copy", "ctty", "date", "del", "dir", "echo",
2270 "erase", "exit", "for", "goto", "if", "md",
2271 "mkdir", "path", "pause", "prompt", "rd",
2272 "rmdir", "rem", "ren", "rename", "set",
2273 "shift", "time", "type", "ver", "verify",
2274 "vol", ":", 0 };
2276 static char sh_chars_os2[] = "*?[];|<>%^()\"'&";
2277 static char *sh_cmds_os2[] = { "call", "cd", "chcp", "chdir", "cls", "copy",
2278 "date", "del", "detach", "dir", "echo",
2279 "endlocal", "erase", "exit", "for", "goto", "if",
2280 "keys", "md", "mkdir", "move", "path", "pause",
2281 "prompt", "rd", "rem", "ren", "rename", "rmdir",
2282 "set", "setlocal", "shift", "start", "time",
2283 "type", "ver", "verify", "vol", ":", 0 };
2285 static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^~'";
2286 static char *sh_cmds_sh[] = { "echo", "cd", "eval", "exec", "exit", "login",
2287 "logout", "set", "umask", "wait", "while",
2288 "for", "case", "if", ":", ".", "break",
2289 "continue", "export", "read", "readonly",
2290 "shift", "times", "trap", "switch", "unset",
2291 0 };
2292 char *sh_chars;
2293 char **sh_cmds;
2295 #elif defined (_AMIGA)
2296 static char sh_chars[] = "#;\"|<>()?*$`";
2297 static char *sh_cmds[] = { "cd", "eval", "if", "delete", "echo", "copy",
2298 "rename", "set", "setenv", "date", "makedir",
2299 "skip", "else", "endif", "path", "prompt",
2300 "unset", "unsetenv", "version",
2301 0 };
2302 #elif defined (WINDOWS32)
2303 static char sh_chars_dos[] = "\"|&<>";
2304 static char *sh_cmds_dos[] = { "break", "call", "cd", "chcp", "chdir", "cls",
2305 "copy", "ctty", "date", "del", "dir", "echo",
2306 "erase", "exit", "for", "goto", "if", "if", "md",
2307 "mkdir", "path", "pause", "prompt", "rd", "rem",
2308 "ren", "rename", "rmdir", "set", "shift", "time",
2309 "type", "ver", "verify", "vol", ":", 0 };
2310 static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^";
2311 static char *sh_cmds_sh[] = { "cd", "eval", "exec", "exit", "login",
2312 "logout", "set", "umask", "wait", "while", "for",
2313 "case", "if", ":", ".", "break", "continue",
2314 "export", "read", "readonly", "shift", "times",
2315 "trap", "switch", "test",
2316 #ifdef BATCH_MODE_ONLY_SHELL
2317 "echo",
2318 #endif
2319 0 };
2320 char* sh_chars;
2321 char** sh_cmds;
2322 #elif defined(__riscos__)
2323 static char sh_chars[] = "";
2324 static char *sh_cmds[] = { 0 };
2325 #else /* must be UNIX-ish */
2326 static char sh_chars[] = "#;\"*?[]&|<>(){}$`^~!";
2327 static char *sh_cmds[] = { ".", ":", "break", "case", "cd", "continue",
2328 "eval", "exec", "exit", "export", "for", "if",
2329 "login", "logout", "read", "readonly", "set",
2330 "shift", "switch", "test", "times", "trap",
2331 "ulimit", "umask", "unset", "wait", "while", 0 };
2332 # ifdef HAVE_DOS_PATHS
2333 /* This is required if the MSYS/Cygwin ports (which do not define
2334 WINDOWS32) are compiled with HAVE_DOS_PATHS defined, which uses
2335 sh_chars_sh[] directly (see below). */
2336 static char *sh_chars_sh = sh_chars;
2337 # endif /* HAVE_DOS_PATHS */
2338 #endif
2339 int i;
2340 char *p;
2341 char *ap;
2342 char *end;
2343 int instring, word_has_equals, seen_nonequals, last_argument_was_empty;
2344 char **new_argv = 0;
2345 char *argstr = 0;
2346 #ifdef WINDOWS32
2347 int slow_flag = 0;
2349 if (!unixy_shell) {
2350 sh_cmds = sh_cmds_dos;
2351 sh_chars = sh_chars_dos;
2352 } else {
2353 sh_cmds = sh_cmds_sh;
2354 sh_chars = sh_chars_sh;
2356 #endif /* WINDOWS32 */
2358 if (restp != NULL)
2359 *restp = NULL;
2361 /* Make sure not to bother processing an empty line. */
2362 while (isblank ((unsigned char)*line))
2363 ++line;
2364 if (*line == '\0')
2365 return 0;
2367 /* See if it is safe to parse commands internally. */
2368 if (shell == 0)
2369 shell = default_shell;
2370 #ifdef WINDOWS32
2371 else if (strcmp (shell, default_shell))
2373 char *s1 = _fullpath (NULL, shell, 0);
2374 char *s2 = _fullpath (NULL, default_shell, 0);
2376 slow_flag = strcmp ((s1 ? s1 : ""), (s2 ? s2 : ""));
2378 if (s1)
2379 free (s1);
2380 if (s2)
2381 free (s2);
2383 if (slow_flag)
2384 goto slow;
2385 #else /* not WINDOWS32 */
2386 #if defined (__MSDOS__) || defined (__EMX__)
2387 else if (strcasecmp (shell, default_shell))
2389 extern int _is_unixy_shell (const char *_path);
2391 DB (DB_BASIC, (_("$SHELL changed (was `%s', now `%s')\n"),
2392 default_shell, shell));
2393 unixy_shell = _is_unixy_shell (shell);
2394 /* we must allocate a copy of shell: construct_command_argv() will free
2395 * shell after this function returns. */
2396 default_shell = xstrdup (shell);
2398 if (unixy_shell)
2400 sh_chars = sh_chars_sh;
2401 sh_cmds = sh_cmds_sh;
2403 else
2405 sh_chars = sh_chars_dos;
2406 sh_cmds = sh_cmds_dos;
2407 # ifdef __EMX__
2408 if (_osmode == OS2_MODE)
2410 sh_chars = sh_chars_os2;
2411 sh_cmds = sh_cmds_os2;
2413 # endif
2415 #else /* !__MSDOS__ */
2416 else if (strcmp (shell, default_shell))
2417 goto slow;
2418 #endif /* !__MSDOS__ && !__EMX__ */
2419 #endif /* not WINDOWS32 */
2421 if (ifs != 0)
2422 for (ap = ifs; *ap != '\0'; ++ap)
2423 if (*ap != ' ' && *ap != '\t' && *ap != '\n')
2424 goto slow;
2426 i = strlen (line) + 1;
2428 /* More than 1 arg per character is impossible. */
2429 new_argv = xmalloc (i * sizeof (char *));
2431 /* All the args can fit in a buffer as big as LINE is. */
2432 ap = new_argv[0] = argstr = xmalloc (i);
2433 end = ap + i;
2435 /* I is how many complete arguments have been found. */
2436 i = 0;
2437 instring = word_has_equals = seen_nonequals = last_argument_was_empty = 0;
2438 for (p = line; *p != '\0'; ++p)
2440 assert (ap <= end);
2442 if (instring)
2444 /* Inside a string, just copy any char except a closing quote
2445 or a backslash-newline combination. */
2446 if (*p == instring)
2448 instring = 0;
2449 if (ap == new_argv[0] || *(ap-1) == '\0')
2450 last_argument_was_empty = 1;
2452 else if (*p == '\\' && p[1] == '\n')
2454 /* Backslash-newline is handled differently depending on what
2455 kind of string we're in: inside single-quoted strings you
2456 keep them; in double-quoted strings they disappear.
2457 For DOS/Windows/OS2, if we don't have a POSIX shell,
2458 we keep the pre-POSIX behavior of removing the
2459 backslash-newline. */
2460 if (instring == '"'
2461 #if defined (__MSDOS__) || defined (__EMX__) || defined (WINDOWS32)
2462 || !unixy_shell
2463 #endif
2465 ++p;
2466 else
2468 *(ap++) = *(p++);
2469 *(ap++) = *p;
2472 else if (*p == '\n' && restp != NULL)
2474 /* End of the command line. */
2475 *restp = p;
2476 goto end_of_line;
2478 /* Backslash, $, and ` are special inside double quotes.
2479 If we see any of those, punt.
2480 But on MSDOS, if we use COMMAND.COM, double and single
2481 quotes have the same effect. */
2482 else if (instring == '"' && strchr ("\\$`", *p) != 0 && unixy_shell)
2483 goto slow;
2484 else
2485 *ap++ = *p;
2487 else if (strchr (sh_chars, *p) != 0)
2488 /* Not inside a string, but it's a special char. */
2489 goto slow;
2490 #ifdef __MSDOS__
2491 else if (*p == '.' && p[1] == '.' && p[2] == '.' && p[3] != '.')
2492 /* `...' is a wildcard in DJGPP. */
2493 goto slow;
2494 #endif
2495 else
2496 /* Not a special char. */
2497 switch (*p)
2499 case '=':
2500 /* Equals is a special character in leading words before the
2501 first word with no equals sign in it. This is not the case
2502 with sh -k, but we never get here when using nonstandard
2503 shell flags. */
2504 if (! seen_nonequals && unixy_shell)
2505 goto slow;
2506 word_has_equals = 1;
2507 *ap++ = '=';
2508 break;
2510 case '\\':
2511 /* Backslash-newline has special case handling, ref POSIX.
2512 We're in the fastpath, so emulate what the shell would do. */
2513 if (p[1] == '\n')
2515 /* Throw out the backslash and newline. */
2516 ++p;
2518 /* If there's nothing in this argument yet, skip any
2519 whitespace before the start of the next word. */
2520 if (ap == new_argv[i])
2521 p = next_token (p + 1) - 1;
2523 else if (p[1] != '\0')
2525 #ifdef HAVE_DOS_PATHS
2526 /* Only remove backslashes before characters special to Unixy
2527 shells. All other backslashes are copied verbatim, since
2528 they are probably DOS-style directory separators. This
2529 still leaves a small window for problems, but at least it
2530 should work for the vast majority of naive users. */
2532 #ifdef __MSDOS__
2533 /* A dot is only special as part of the "..."
2534 wildcard. */
2535 if (strneq (p + 1, ".\\.\\.", 5))
2537 *ap++ = '.';
2538 *ap++ = '.';
2539 p += 4;
2541 else
2542 #endif
2543 if (p[1] != '\\' && p[1] != '\''
2544 && !isspace ((unsigned char)p[1])
2545 && strchr (sh_chars_sh, p[1]) == 0)
2546 /* back up one notch, to copy the backslash */
2547 --p;
2548 #endif /* HAVE_DOS_PATHS */
2550 /* Copy and skip the following char. */
2551 *ap++ = *++p;
2553 break;
2555 case '\'':
2556 case '"':
2557 instring = *p;
2558 break;
2560 case '\n':
2561 if (restp != NULL)
2563 /* End of the command line. */
2564 *restp = p;
2565 goto end_of_line;
2567 else
2568 /* Newlines are not special. */
2569 *ap++ = '\n';
2570 break;
2572 case ' ':
2573 case '\t':
2574 /* We have the end of an argument.
2575 Terminate the text of the argument. */
2576 *ap++ = '\0';
2577 new_argv[++i] = ap;
2578 last_argument_was_empty = 0;
2580 /* Update SEEN_NONEQUALS, which tells us if every word
2581 heretofore has contained an `='. */
2582 seen_nonequals |= ! word_has_equals;
2583 if (word_has_equals && ! seen_nonequals)
2584 /* An `=' in a word before the first
2585 word without one is magical. */
2586 goto slow;
2587 word_has_equals = 0; /* Prepare for the next word. */
2589 /* If this argument is the command name,
2590 see if it is a built-in shell command.
2591 If so, have the shell handle it. */
2592 if (i == 1)
2594 register int j;
2595 for (j = 0; sh_cmds[j] != 0; ++j)
2597 if (streq (sh_cmds[j], new_argv[0]))
2598 goto slow;
2599 # ifdef __EMX__
2600 /* Non-Unix shells are case insensitive. */
2601 if (!unixy_shell
2602 && strcasecmp (sh_cmds[j], new_argv[0]) == 0)
2603 goto slow;
2604 # endif
2608 /* Ignore multiple whitespace chars. */
2609 p = next_token (p) - 1;
2610 break;
2612 default:
2613 *ap++ = *p;
2614 break;
2617 end_of_line:
2619 if (instring)
2620 /* Let the shell deal with an unterminated quote. */
2621 goto slow;
2623 /* Terminate the last argument and the argument list. */
2625 *ap = '\0';
2626 if (new_argv[i][0] != '\0' || last_argument_was_empty)
2627 ++i;
2628 new_argv[i] = 0;
2630 if (i == 1)
2632 register int j;
2633 for (j = 0; sh_cmds[j] != 0; ++j)
2634 if (streq (sh_cmds[j], new_argv[0]))
2635 goto slow;
2638 if (new_argv[0] == 0)
2640 /* Line was empty. */
2641 free (argstr);
2642 free (new_argv);
2643 return 0;
2646 return new_argv;
2648 slow:;
2649 /* We must use the shell. */
2651 if (new_argv != 0)
2653 /* Free the old argument list we were working on. */
2654 free (argstr);
2655 free (new_argv);
2658 #ifdef __MSDOS__
2659 execute_by_shell = 1; /* actually, call `system' if shell isn't unixy */
2660 #endif
2662 #ifdef _AMIGA
2664 char *ptr;
2665 char *buffer;
2666 char *dptr;
2668 buffer = xmalloc (strlen (line)+1);
2670 ptr = line;
2671 for (dptr=buffer; *ptr; )
2673 if (*ptr == '\\' && ptr[1] == '\n')
2674 ptr += 2;
2675 else if (*ptr == '@') /* Kludge: multiline commands */
2677 ptr += 2;
2678 *dptr++ = '\n';
2680 else
2681 *dptr++ = *ptr++;
2683 *dptr = 0;
2685 new_argv = xmalloc (2 * sizeof (char *));
2686 new_argv[0] = buffer;
2687 new_argv[1] = 0;
2689 #else /* Not Amiga */
2690 #ifdef WINDOWS32
2692 * Not eating this whitespace caused things like
2694 * sh -c "\n"
2696 * which gave the shell fits. I think we have to eat
2697 * whitespace here, but this code should be considered
2698 * suspicious if things start failing....
2701 /* Make sure not to bother processing an empty line. */
2702 while (isspace ((unsigned char)*line))
2703 ++line;
2704 if (*line == '\0')
2705 return 0;
2706 #endif /* WINDOWS32 */
2708 /* SHELL may be a multi-word command. Construct a command line
2709 "SHELL -c LINE", with all special chars in LINE escaped.
2710 Then recurse, expanding this command line to get the final
2711 argument list. */
2713 unsigned int shell_len = strlen (shell);
2714 #ifndef VMS
2715 static char minus_c[] = " -c ";
2716 #else
2717 static char minus_c[] = "";
2718 #endif
2719 unsigned int line_len = strlen (line);
2721 char *new_line = alloca (shell_len + (sizeof (minus_c)-1)
2722 + (line_len*2) + 1);
2723 char *command_ptr = NULL; /* used for batch_mode_shell mode */
2725 # ifdef __EMX__ /* is this necessary? */
2726 if (!unixy_shell)
2727 minus_c[1] = '/'; /* " /c " */
2728 # endif
2730 ap = new_line;
2731 memcpy (ap, shell, shell_len);
2732 ap += shell_len;
2733 memcpy (ap, minus_c, sizeof (minus_c) - 1);
2734 ap += sizeof (minus_c) - 1;
2735 command_ptr = ap;
2736 for (p = line; *p != '\0'; ++p)
2738 if (restp != NULL && *p == '\n')
2740 *restp = p;
2741 break;
2743 else if (*p == '\\' && p[1] == '\n')
2745 /* POSIX says we keep the backslash-newline. If we don't have a
2746 POSIX shell on DOS/Windows/OS2, mimic the pre-POSIX behavior
2747 and remove the backslash/newline. */
2748 #if defined (__MSDOS__) || defined (__EMX__) || defined (WINDOWS32)
2749 # define PRESERVE_BSNL unixy_shell
2750 #else
2751 # define PRESERVE_BSNL 1
2752 #endif
2753 if (PRESERVE_BSNL)
2755 *(ap++) = '\\';
2756 /* Only non-batch execution needs another backslash,
2757 because it will be passed through a recursive
2758 invocation of this function. */
2759 if (!batch_mode_shell)
2760 *(ap++) = '\\';
2761 *(ap++) = '\n';
2763 ++p;
2764 continue;
2767 /* DOS shells don't know about backslash-escaping. */
2768 if (unixy_shell && !batch_mode_shell &&
2769 (*p == '\\' || *p == '\'' || *p == '"'
2770 || isspace ((unsigned char)*p)
2771 || strchr (sh_chars, *p) != 0))
2772 *ap++ = '\\';
2773 #ifdef __MSDOS__
2774 else if (unixy_shell && strneq (p, "...", 3))
2776 /* The case of `...' wildcard again. */
2777 strcpy (ap, "\\.\\.\\");
2778 ap += 5;
2779 p += 2;
2781 #endif
2782 *ap++ = *p;
2784 if (ap == new_line + shell_len + sizeof (minus_c) - 1)
2785 /* Line was empty. */
2786 return 0;
2787 *ap = '\0';
2789 #ifdef WINDOWS32
2790 /* Some shells do not work well when invoked as 'sh -c xxx' to run a
2791 command line (e.g. Cygnus GNUWIN32 sh.exe on WIN32 systems). In these
2792 cases, run commands via a script file. */
2793 if (just_print_flag && !(flags & COMMANDS_RECURSE)) {
2794 /* Need to allocate new_argv, although it's unused, because
2795 start_job_command will want to free it and its 0'th element. */
2796 new_argv = xmalloc(2 * sizeof (char *));
2797 new_argv[0] = xstrdup ("");
2798 new_argv[1] = NULL;
2799 } else if ((no_default_sh_exe || batch_mode_shell) && batch_filename_ptr) {
2800 int temp_fd;
2801 FILE* batch = NULL;
2802 int id = GetCurrentProcessId();
2803 PATH_VAR(fbuf);
2805 /* create a file name */
2806 sprintf(fbuf, "make%d", id);
2807 *batch_filename_ptr = create_batch_file (fbuf, unixy_shell, &temp_fd);
2809 DB (DB_JOBS, (_("Creating temporary batch file %s\n"),
2810 *batch_filename_ptr));
2812 /* Create a FILE object for the batch file, and write to it the
2813 commands to be executed. Put the batch file in TEXT mode. */
2814 _setmode (temp_fd, _O_TEXT);
2815 batch = _fdopen (temp_fd, "wt");
2816 if (!unixy_shell)
2817 fputs ("@echo off\n", batch);
2818 fputs (command_ptr, batch);
2819 fputc ('\n', batch);
2820 fclose (batch);
2821 DB (DB_JOBS, (_("Batch file contents:%s\n\t%s\n"),
2822 !unixy_shell ? "\n\t@echo off" : "", command_ptr));
2824 /* create argv */
2825 new_argv = xmalloc(3 * sizeof (char *));
2826 if (unixy_shell) {
2827 new_argv[0] = xstrdup (shell);
2828 new_argv[1] = *batch_filename_ptr; /* only argv[0] gets freed later */
2829 } else {
2830 new_argv[0] = xstrdup (*batch_filename_ptr);
2831 new_argv[1] = NULL;
2833 new_argv[2] = NULL;
2834 } else
2835 #endif /* WINDOWS32 */
2836 if (unixy_shell)
2837 new_argv = construct_command_argv_internal (new_line, 0, 0, 0, flags, 0);
2838 #ifdef __EMX__
2839 else if (!unixy_shell)
2841 /* new_line is local, must not be freed therefore
2842 We use line here instead of new_line because we run the shell
2843 manually. */
2844 size_t line_len = strlen (line);
2845 char *p = new_line;
2846 char *q = new_line;
2847 memcpy (new_line, line, line_len + 1);
2848 /* replace all backslash-newline combination and also following tabs */
2849 while (*q != '\0')
2851 if (q[0] == '\\' && q[1] == '\n')
2852 q += 2; /* remove '\\' and '\n' */
2853 else
2854 *p++ = *q++;
2856 *p = '\0';
2858 # ifndef NO_CMD_DEFAULT
2859 if (strnicmp (new_line, "echo", 4) == 0
2860 && (new_line[4] == ' ' || new_line[4] == '\t'))
2862 /* the builtin echo command: handle it separately */
2863 size_t echo_len = line_len - 5;
2864 char *echo_line = new_line + 5;
2866 /* special case: echo 'x="y"'
2867 cmd works this way: a string is printed as is, i.e., no quotes
2868 are removed. But autoconf uses a command like echo 'x="y"' to
2869 determine whether make works. autoconf expects the output x="y"
2870 so we will do exactly that.
2871 Note: if we do not allow cmd to be the default shell
2872 we do not need this kind of voodoo */
2873 if (echo_line[0] == '\''
2874 && echo_line[echo_len - 1] == '\''
2875 && strncmp (echo_line + 1, "ac_maketemp=",
2876 strlen ("ac_maketemp=")) == 0)
2878 /* remove the enclosing quotes */
2879 memmove (echo_line, echo_line + 1, echo_len - 2);
2880 echo_line[echo_len - 2] = '\0';
2883 # endif
2886 /* Let the shell decide what to do. Put the command line into the
2887 2nd command line argument and hope for the best ;-) */
2888 size_t sh_len = strlen (shell);
2890 /* exactly 3 arguments + NULL */
2891 new_argv = xmalloc (4 * sizeof (char *));
2892 /* Exactly strlen(shell) + strlen("/c") + strlen(line) + 3 times
2893 the trailing '\0' */
2894 new_argv[0] = xmalloc (sh_len + line_len + 5);
2895 memcpy (new_argv[0], shell, sh_len + 1);
2896 new_argv[1] = new_argv[0] + sh_len + 1;
2897 memcpy (new_argv[1], "/c", 3);
2898 new_argv[2] = new_argv[1] + 3;
2899 memcpy (new_argv[2], new_line, line_len + 1);
2900 new_argv[3] = NULL;
2903 #elif defined(__MSDOS__)
2904 else
2906 /* With MSDOS shells, we must construct the command line here
2907 instead of recursively calling ourselves, because we
2908 cannot backslash-escape the special characters (see above). */
2909 new_argv = xmalloc (sizeof (char *));
2910 line_len = strlen (new_line) - shell_len - sizeof (minus_c) + 1;
2911 new_argv[0] = xmalloc (line_len + 1);
2912 strncpy (new_argv[0],
2913 new_line + shell_len + sizeof (minus_c) - 1, line_len);
2914 new_argv[0][line_len] = '\0';
2916 #else
2917 else
2918 fatal (NILF, _("%s (line %d) Bad shell context (!unixy && !batch_mode_shell)\n"),
2919 __FILE__, __LINE__);
2920 #endif
2922 #endif /* ! AMIGA */
2924 return new_argv;
2926 #endif /* !VMS */
2928 /* Figure out the argument list necessary to run LINE as a command. Try to
2929 avoid using a shell. This routine handles only ' quoting, and " quoting
2930 when no backslash, $ or ` characters are seen in the quotes. Starting
2931 quotes may be escaped with a backslash. If any of the characters in
2932 sh_chars[] is seen, or any of the builtin commands listed in sh_cmds[]
2933 is the first word of a line, the shell is used.
2935 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
2936 If *RESTP is NULL, newlines will be ignored.
2938 FILE is the target whose commands these are. It is used for
2939 variable expansion for $(SHELL) and $(IFS). */
2941 char **
2942 construct_command_argv (char *line, char **restp, struct file *file,
2943 int cmd_flags, char **batch_filename_ptr)
2945 char *shell, *ifs;
2946 char **argv;
2948 #ifdef VMS
2949 char *cptr;
2950 int argc;
2952 argc = 0;
2953 cptr = line;
2954 for (;;)
2956 while ((*cptr != 0)
2957 && (isspace ((unsigned char)*cptr)))
2958 cptr++;
2959 if (*cptr == 0)
2960 break;
2961 while ((*cptr != 0)
2962 && (!isspace((unsigned char)*cptr)))
2963 cptr++;
2964 argc++;
2967 argv = xmalloc (argc * sizeof (char *));
2968 if (argv == 0)
2969 abort ();
2971 cptr = line;
2972 argc = 0;
2973 for (;;)
2975 while ((*cptr != 0)
2976 && (isspace ((unsigned char)*cptr)))
2977 cptr++;
2978 if (*cptr == 0)
2979 break;
2980 DB (DB_JOBS, ("argv[%d] = [%s]\n", argc, cptr));
2981 argv[argc++] = cptr;
2982 while ((*cptr != 0)
2983 && (!isspace((unsigned char)*cptr)))
2984 cptr++;
2985 if (*cptr != 0)
2986 *cptr++ = 0;
2988 #else
2990 /* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
2991 int save = warn_undefined_variables_flag;
2992 warn_undefined_variables_flag = 0;
2994 shell = allocated_variable_expand_for_file ("$(SHELL)", file);
2995 #ifdef WINDOWS32
2997 * Convert to forward slashes so that construct_command_argv_internal()
2998 * is not confused.
3000 if (shell) {
3001 char *p = w32ify (shell, 0);
3002 strcpy (shell, p);
3004 #endif
3005 #ifdef __EMX__
3007 static const char *unixroot = NULL;
3008 static const char *last_shell = "";
3009 static int init = 0;
3010 if (init == 0)
3012 unixroot = getenv ("UNIXROOT");
3013 /* unixroot must be NULL or not empty */
3014 if (unixroot && unixroot[0] == '\0') unixroot = NULL;
3015 init = 1;
3018 /* if we have an unixroot drive and if shell is not default_shell
3019 (which means it's either cmd.exe or the test has already been
3020 performed) and if shell is an absolute path without drive letter,
3021 try whether it exists e.g.: if "/bin/sh" does not exist use
3022 "$UNIXROOT/bin/sh" instead. */
3023 if (unixroot && shell && strcmp (shell, last_shell) != 0
3024 && (shell[0] == '/' || shell[0] == '\\'))
3026 /* trying a new shell, check whether it exists */
3027 size_t size = strlen (shell);
3028 char *buf = xmalloc (size + 7);
3029 memcpy (buf, shell, size);
3030 memcpy (buf + size, ".exe", 5); /* including the trailing '\0' */
3031 if (access (shell, F_OK) != 0 && access (buf, F_OK) != 0)
3033 /* try the same for the unixroot drive */
3034 memmove (buf + 2, buf, size + 5);
3035 buf[0] = unixroot[0];
3036 buf[1] = unixroot[1];
3037 if (access (buf, F_OK) == 0)
3038 /* we have found a shell! */
3039 /* free(shell); */
3040 shell = buf;
3041 else
3042 free (buf);
3044 else
3045 free (buf);
3048 #endif /* __EMX__ */
3050 ifs = allocated_variable_expand_for_file ("$(IFS)", file);
3052 warn_undefined_variables_flag = save;
3055 argv = construct_command_argv_internal (line, restp, shell, ifs,
3056 cmd_flags, batch_filename_ptr);
3058 free (shell);
3059 free (ifs);
3060 #endif /* !VMS */
3061 return argv;
3064 #if !defined(HAVE_DUP2) && !defined(_AMIGA)
3066 dup2 (int old, int new)
3068 int fd;
3070 (void) close (new);
3071 fd = dup (old);
3072 if (fd != new)
3074 (void) close (fd);
3075 errno = EMFILE;
3076 return -1;
3079 return fd;
3081 #endif /* !HAPE_DUP2 && !_AMIGA */
3083 /* On VMS systems, include special VMS functions. */
3085 #ifdef VMS
3086 #include "vmsjobs.c"
3087 #endif