Make sure to assign a boolean value to a 1-bit bitfield. Reported on
[make.git] / job.c
blob71bc3814ff8e24b6b8198e51fb51c7244d16ae77
1 /* Job execution and handling for GNU Make.
2 Copyright (C) 1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1999,
3 2000,2001,2002,2003,2004,2005 Free Software Foundation, Inc.
4 This file is part of GNU Make.
6 GNU Make is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Make 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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Make; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include "make.h"
23 #include <assert.h>
25 #include "job.h"
26 #include "debug.h"
27 #include "filedef.h"
28 #include "commands.h"
29 #include "variable.h"
30 #include "debug.h"
32 #include <string.h>
34 /* Default shell to use. */
35 #ifdef WINDOWS32
36 #include <windows.h>
38 char *default_shell = "sh.exe";
39 int no_default_sh_exe = 1;
40 int batch_mode_shell = 1;
41 HANDLE main_thread;
43 #elif defined (_AMIGA)
45 char default_shell[] = "";
46 extern int MyExecute (char **);
47 int batch_mode_shell = 0;
49 #elif defined (__MSDOS__)
51 /* The default shell is a pointer so we can change it if Makefile
52 says so. It is without an explicit path so we get a chance
53 to search the $PATH for it (since MSDOS doesn't have standard
54 directories we could trust). */
55 char *default_shell = "command.com";
56 int batch_mode_shell = 0;
58 #elif defined (__EMX__)
60 char *default_shell = "/bin/sh";
61 int batch_mode_shell = 0;
63 #elif defined (VMS)
65 # include <descrip.h>
66 char default_shell[] = "";
67 int batch_mode_shell = 0;
69 #elif defined (__riscos__)
71 char default_shell[] = "";
72 int batch_mode_shell = 0;
74 #else
76 char default_shell[] = "/bin/sh";
77 int batch_mode_shell = 0;
79 #endif
81 #ifdef __MSDOS__
82 # include <process.h>
83 static int execute_by_shell;
84 static int dos_pid = 123;
85 int dos_status;
86 int dos_command_running;
87 #endif /* __MSDOS__ */
89 #ifdef _AMIGA
90 # include <proto/dos.h>
91 static int amiga_pid = 123;
92 static int amiga_status;
93 static char amiga_bname[32];
94 static int amiga_batch_file;
95 #endif /* Amiga. */
97 #ifdef VMS
98 # ifndef __GNUC__
99 # include <processes.h>
100 # endif
101 # include <starlet.h>
102 # include <lib$routines.h>
103 static void vmsWaitForChildren PARAMS ((int *));
104 #endif
106 #ifdef WINDOWS32
107 # include <windows.h>
108 # include <io.h>
109 # include <process.h>
110 # include "sub_proc.h"
111 # include "w32err.h"
112 # include "pathstuff.h"
113 #endif /* WINDOWS32 */
115 #ifdef __EMX__
116 # include <process.h>
117 #endif
119 #if defined (HAVE_SYS_WAIT_H) || defined (HAVE_UNION_WAIT)
120 # include <sys/wait.h>
121 #endif
123 #ifdef HAVE_WAITPID
124 # define WAIT_NOHANG(status) waitpid (-1, (status), WNOHANG)
125 #else /* Don't have waitpid. */
126 # ifdef HAVE_WAIT3
127 # ifndef wait3
128 extern int wait3 ();
129 # endif
130 # define WAIT_NOHANG(status) wait3 ((status), WNOHANG, (struct rusage *) 0)
131 # endif /* Have wait3. */
132 #endif /* Have waitpid. */
134 #if !defined (wait) && !defined (POSIX)
135 extern int wait ();
136 #endif
138 #ifndef HAVE_UNION_WAIT
140 # define WAIT_T int
142 # ifndef WTERMSIG
143 # define WTERMSIG(x) ((x) & 0x7f)
144 # endif
145 # ifndef WCOREDUMP
146 # define WCOREDUMP(x) ((x) & 0x80)
147 # endif
148 # ifndef WEXITSTATUS
149 # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
150 # endif
151 # ifndef WIFSIGNALED
152 # define WIFSIGNALED(x) (WTERMSIG (x) != 0)
153 # endif
154 # ifndef WIFEXITED
155 # define WIFEXITED(x) (WTERMSIG (x) == 0)
156 # endif
158 #else /* Have `union wait'. */
160 # define WAIT_T union wait
161 # ifndef WTERMSIG
162 # define WTERMSIG(x) ((x).w_termsig)
163 # endif
164 # ifndef WCOREDUMP
165 # define WCOREDUMP(x) ((x).w_coredump)
166 # endif
167 # ifndef WEXITSTATUS
168 # define WEXITSTATUS(x) ((x).w_retcode)
169 # endif
170 # ifndef WIFSIGNALED
171 # define WIFSIGNALED(x) (WTERMSIG(x) != 0)
172 # endif
173 # ifndef WIFEXITED
174 # define WIFEXITED(x) (WTERMSIG(x) == 0)
175 # endif
177 #endif /* Don't have `union wait'. */
179 #ifndef HAVE_UNISTD_H
180 extern int dup2 ();
181 extern int execve ();
182 extern void _exit ();
183 # ifndef VMS
184 extern int geteuid ();
185 extern int getegid ();
186 extern int setgid ();
187 extern int getgid ();
188 # endif
189 #endif
191 extern char *allocated_variable_expand_for_file PARAMS ((char *line, struct file *file));
193 extern int getloadavg PARAMS ((double loadavg[], int nelem));
194 extern int start_remote_job PARAMS ((char **argv, char **envp, int stdin_fd,
195 int *is_remote, int *id_ptr, int *used_stdin));
196 extern int start_remote_job_p PARAMS ((int));
197 extern int remote_status PARAMS ((int *exit_code_ptr, int *signal_ptr,
198 int *coredump_ptr, int block));
200 RETSIGTYPE child_handler PARAMS ((int));
201 static void free_child PARAMS ((struct child *));
202 static void start_job_command PARAMS ((struct child *child));
203 static int load_too_high PARAMS ((void));
204 static int job_next_command PARAMS ((struct child *));
205 static int start_waiting_job PARAMS ((struct child *));
207 /* Chain of all live (or recently deceased) children. */
209 struct child *children = 0;
211 /* Number of children currently running. */
213 unsigned int job_slots_used = 0;
215 /* Nonzero if the `good' standard input is in use. */
217 static int good_stdin_used = 0;
219 /* Chain of children waiting to run until the load average goes down. */
221 static struct child *waiting_jobs = 0;
223 /* Non-zero if we use a *real* shell (always so on Unix). */
225 int unixy_shell = 1;
227 /* Number of jobs started in the current second. */
229 unsigned long job_counter = 0;
231 /* Number of jobserver tokens this instance is currently using. */
233 unsigned int jobserver_tokens = 0;
235 #ifdef WINDOWS32
237 * The macro which references this function is defined in make.h.
240 w32_kill(int pid, int sig)
242 return ((process_kill((HANDLE)pid, sig) == TRUE) ? 0 : -1);
245 /* This function creates a temporary file name with the given extension
246 * the unixy param controls both the extension and the path separator
247 * return an xmalloc'ed string of a newly created temp file or die. */
248 static char *
249 create_batch_filename(char const *base, int unixy)
251 const char *const ext = unixy ? "sh" : "bat";
252 const char *error = NULL;
253 char temp_path[MAXPATHLEN]; /* need to know its length */
254 unsigned path_size = GetTempPath(sizeof temp_path, temp_path);
255 int path_is_dot = 0;
256 unsigned uniq = 1;
257 const unsigned sizemax = strlen (base) + strlen (ext) + 10;
259 if (path_size == 0)
261 path_size = GetCurrentDirectory (sizeof temp_path, temp_path);
262 path_is_dot = 1;
265 while (path_size > 0 &&
266 path_size + sizemax < sizeof temp_path &&
267 uniq < 0x10000)
269 unsigned size = sprintf (temp_path + path_size,
270 "%s%s-%x.%s",
271 temp_path[path_size - 1] == '\\' ? "" : "\\",
272 base, uniq, ext);
273 HANDLE h = CreateFile (temp_path, /* file name */
274 GENERIC_READ | GENERIC_WRITE, /* desired access */
275 0, /* no share mode */
276 NULL, /* default security attributes */
277 CREATE_NEW, /* creation disposition */
278 FILE_ATTRIBUTE_NORMAL | /* flags and attributes */
279 FILE_ATTRIBUTE_TEMPORARY, /* we'll delete it */
280 NULL); /* no template file */
282 if (h == INVALID_HANDLE_VALUE)
284 const DWORD er = GetLastError();
286 if (er == ERROR_FILE_EXISTS || er == ERROR_ALREADY_EXISTS)
287 ++uniq;
289 /* the temporary path is not guaranteed to exist */
290 else if (path_is_dot == 0)
292 path_size = GetCurrentDirectory (sizeof temp_path, temp_path);
293 path_is_dot = 1;
296 else
298 error = map_windows32_error_to_string (er);
299 break;
302 else
304 const unsigned final_size = path_size + size + 1;
305 char *const path = (char *) xmalloc (final_size);
306 memcpy (path, temp_path, final_size);
307 CloseHandle (h);
308 if (unixy)
310 char *p;
311 int ch;
312 for (p = path; (ch = *p) != 0; ++p)
313 if (ch == '\\')
314 *p = '/';
316 return path; /* good return */
320 if (error == NULL)
321 error = _("Cannot create a temporary file\n");
322 fatal (NILF, error);
324 /* not reached */
325 return NULL;
327 #endif /* WINDOWS32 */
329 #ifdef __EMX__
330 /* returns whether path is assumed to be a unix like shell. */
332 _is_unixy_shell (const char *path)
334 /* list of non unix shells */
335 const char *known_os2shells[] = {
336 "cmd.exe",
337 "cmd",
338 "4os2.exe",
339 "4os2",
340 "4dos.exe",
341 "4dos",
342 "command.com",
343 "command",
344 NULL
347 /* find the rightmost '/' or '\\' */
348 const char *name = strrchr (path, '/');
349 const char *p = strrchr (path, '\\');
350 unsigned i;
352 if (name && p) /* take the max */
353 name = (name > p) ? name : p;
354 else if (p) /* name must be 0 */
355 name = p;
356 else if (!name) /* name and p must be 0 */
357 name = path;
359 if (*name == '/' || *name == '\\') name++;
361 i = 0;
362 while (known_os2shells[i] != NULL) {
363 if (stricmp (name, known_os2shells[i]) == 0) /* strcasecmp() */
364 return 0; /* not a unix shell */
365 i++;
368 /* in doubt assume a unix like shell */
369 return 1;
371 #endif /* __EMX__ */
374 /* Write an error message describing the exit status given in
375 EXIT_CODE, EXIT_SIG, and COREDUMP, for the target TARGET_NAME.
376 Append "(ignored)" if IGNORED is nonzero. */
378 static void
379 child_error (char *target_name, int exit_code, int exit_sig, int coredump,
380 int ignored)
382 if (ignored && silent_flag)
383 return;
385 #ifdef VMS
386 if (!(exit_code & 1))
387 error (NILF,
388 (ignored ? _("*** [%s] Error 0x%x (ignored)")
389 : _("*** [%s] Error 0x%x")),
390 target_name, exit_code);
391 #else
392 if (exit_sig == 0)
393 error (NILF, ignored ? _("[%s] Error %d (ignored)") :
394 _("*** [%s] Error %d"),
395 target_name, exit_code);
396 else
397 error (NILF, "*** [%s] %s%s",
398 target_name, strsignal (exit_sig),
399 coredump ? _(" (core dumped)") : "");
400 #endif /* VMS */
404 /* Handle a dead child. This handler may or may not ever be installed.
406 If we're using the jobserver feature, we need it. First, installing it
407 ensures the read will interrupt on SIGCHLD. Second, we close the dup'd
408 read FD to ensure we don't enter another blocking read without reaping all
409 the dead children. In this case we don't need the dead_children count.
411 If we don't have either waitpid or wait3, then make is unreliable, but we
412 use the dead_children count to reap children as best we can. */
414 static unsigned int dead_children = 0;
416 RETSIGTYPE
417 child_handler (int sig UNUSED)
419 ++dead_children;
421 if (job_rfd >= 0)
423 close (job_rfd);
424 job_rfd = -1;
427 #ifdef __EMX__
428 /* The signal handler must called only once! */
429 signal (SIGCHLD, SIG_DFL);
430 #endif
432 /* This causes problems if the SIGCHLD interrupts a printf().
433 DB (DB_JOBS, (_("Got a SIGCHLD; %u unreaped children.\n"), dead_children));
437 extern int shell_function_pid, shell_function_completed;
439 /* Reap all dead children, storing the returned status and the new command
440 state (`cs_finished') in the `file' member of the `struct child' for the
441 dead child, and removing the child from the chain. In addition, if BLOCK
442 nonzero, we block in this function until we've reaped at least one
443 complete child, waiting for it to die if necessary. If ERR is nonzero,
444 print an error message first. */
446 void
447 reap_children (int block, int err)
449 #ifndef WINDOWS32
450 WAIT_T status;
451 /* Initially, assume we have some. */
452 int reap_more = 1;
453 #endif
455 #ifdef WAIT_NOHANG
456 # define REAP_MORE reap_more
457 #else
458 # define REAP_MORE dead_children
459 #endif
461 /* As long as:
463 We have at least one child outstanding OR a shell function in progress,
465 We're blocking for a complete child OR there are more children to reap
467 we'll keep reaping children. */
469 while ((children != 0 || shell_function_pid != 0)
470 && (block || REAP_MORE))
472 int remote = 0;
473 pid_t pid;
474 int exit_code, exit_sig, coredump;
475 register struct child *lastc, *c;
476 int child_failed;
477 int any_remote, any_local;
478 int dontcare;
480 if (err && block)
482 static int printed = 0;
484 /* We might block for a while, so let the user know why.
485 Only print this message once no matter how many jobs are left. */
486 fflush (stdout);
487 if (!printed)
488 error (NILF, _("*** Waiting for unfinished jobs...."));
489 printed = 1;
492 /* We have one less dead child to reap. As noted in
493 child_handler() above, this count is completely unimportant for
494 all modern, POSIX-y systems that support wait3() or waitpid().
495 The rest of this comment below applies only to early, broken
496 pre-POSIX systems. We keep the count only because... it's there...
498 The test and decrement are not atomic; if it is compiled into:
499 register = dead_children - 1;
500 dead_children = register;
501 a SIGCHLD could come between the two instructions.
502 child_handler increments dead_children.
503 The second instruction here would lose that increment. But the
504 only effect of dead_children being wrong is that we might wait
505 longer than necessary to reap a child, and lose some parallelism;
506 and we might print the "Waiting for unfinished jobs" message above
507 when not necessary. */
509 if (dead_children > 0)
510 --dead_children;
512 any_remote = 0;
513 any_local = shell_function_pid != 0;
514 for (c = children; c != 0; c = c->next)
516 any_remote |= c->remote;
517 any_local |= ! c->remote;
518 DB (DB_JOBS, (_("Live child 0x%08lx (%s) PID %ld %s\n"),
519 (unsigned long int) c, c->file->name,
520 (long) c->pid, c->remote ? _(" (remote)") : ""));
521 #ifdef VMS
522 break;
523 #endif
526 /* First, check for remote children. */
527 if (any_remote)
528 pid = remote_status (&exit_code, &exit_sig, &coredump, 0);
529 else
530 pid = 0;
532 if (pid > 0)
533 /* We got a remote child. */
534 remote = 1;
535 else if (pid < 0)
537 /* A remote status command failed miserably. Punt. */
538 remote_status_lose:
539 pfatal_with_name ("remote_status");
541 else
543 /* No remote children. Check for local children. */
544 #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
545 if (any_local)
547 #ifdef VMS
548 vmsWaitForChildren (&status);
549 pid = c->pid;
550 #else
551 #ifdef WAIT_NOHANG
552 if (!block)
553 pid = WAIT_NOHANG (&status);
554 else
555 #endif
556 pid = wait (&status);
557 #endif /* !VMS */
559 else
560 pid = 0;
562 if (pid < 0)
564 /* The wait*() failed miserably. Punt. */
565 pfatal_with_name ("wait");
567 else if (pid > 0)
569 /* We got a child exit; chop the status word up. */
570 exit_code = WEXITSTATUS (status);
571 exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0;
572 coredump = WCOREDUMP (status);
574 /* If we have started jobs in this second, remove one. */
575 if (job_counter)
576 --job_counter;
578 else
580 /* No local children are dead. */
581 reap_more = 0;
583 if (!block || !any_remote)
584 break;
586 /* Now try a blocking wait for a remote child. */
587 pid = remote_status (&exit_code, &exit_sig, &coredump, 1);
588 if (pid < 0)
589 goto remote_status_lose;
590 else if (pid == 0)
591 /* No remote children either. Finally give up. */
592 break;
594 /* We got a remote child. */
595 remote = 1;
597 #endif /* !__MSDOS__, !Amiga, !WINDOWS32. */
599 #ifdef __MSDOS__
600 /* Life is very different on MSDOS. */
601 pid = dos_pid - 1;
602 status = dos_status;
603 exit_code = WEXITSTATUS (status);
604 if (exit_code == 0xff)
605 exit_code = -1;
606 exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0;
607 coredump = 0;
608 #endif /* __MSDOS__ */
609 #ifdef _AMIGA
610 /* Same on Amiga */
611 pid = amiga_pid - 1;
612 status = amiga_status;
613 exit_code = amiga_status;
614 exit_sig = 0;
615 coredump = 0;
616 #endif /* _AMIGA */
617 #ifdef WINDOWS32
619 HANDLE hPID;
620 int werr;
621 HANDLE hcTID, hcPID;
622 exit_code = 0;
623 exit_sig = 0;
624 coredump = 0;
626 /* Record the thread ID of the main process, so that we
627 could suspend it in the signal handler. */
628 if (!main_thread)
630 hcTID = GetCurrentThread ();
631 hcPID = GetCurrentProcess ();
632 if (!DuplicateHandle (hcPID, hcTID, hcPID, &main_thread, 0,
633 FALSE, DUPLICATE_SAME_ACCESS))
635 DWORD e = GetLastError ();
636 fprintf (stderr,
637 "Determine main thread ID (Error %ld: %s)\n",
638 e, map_windows32_error_to_string(e));
640 else
641 DB (DB_VERBOSE, ("Main thread handle = 0x%08lx\n",
642 (unsigned long)main_thread));
645 /* wait for anything to finish */
646 hPID = process_wait_for_any();
647 if (hPID)
650 /* was an error found on this process? */
651 werr = process_last_err(hPID);
653 /* get exit data */
654 exit_code = process_exit_code(hPID);
656 if (werr)
657 fprintf(stderr, "make (e=%d): %s",
658 exit_code, map_windows32_error_to_string(exit_code));
660 /* signal */
661 exit_sig = process_signal(hPID);
663 /* cleanup process */
664 process_cleanup(hPID);
666 coredump = 0;
668 pid = (pid_t) hPID;
670 #endif /* WINDOWS32 */
673 /* Check if this is the child of the `shell' function. */
674 if (!remote && pid == shell_function_pid)
676 /* It is. Leave an indicator for the `shell' function. */
677 if (exit_sig == 0 && exit_code == 127)
678 shell_function_completed = -1;
679 else
680 shell_function_completed = 1;
681 break;
684 child_failed = exit_sig != 0 || exit_code != 0;
686 /* Search for a child matching the deceased one. */
687 lastc = 0;
688 for (c = children; c != 0; lastc = c, c = c->next)
689 if (c->remote == remote && c->pid == pid)
690 break;
692 if (c == 0)
693 /* An unknown child died.
694 Ignore it; it was inherited from our invoker. */
695 continue;
697 DB (DB_JOBS, (child_failed
698 ? _("Reaping losing child 0x%08lx PID %ld %s\n")
699 : _("Reaping winning child 0x%08lx PID %ld %s\n"),
700 (unsigned long int) c, (long) c->pid,
701 c->remote ? _(" (remote)") : ""));
703 if (c->sh_batch_file) {
704 DB (DB_JOBS, (_("Cleaning up temp batch file %s\n"),
705 c->sh_batch_file));
707 /* just try and remove, don't care if this fails */
708 remove (c->sh_batch_file);
710 /* all done with memory */
711 free (c->sh_batch_file);
712 c->sh_batch_file = NULL;
715 /* If this child had the good stdin, say it is now free. */
716 if (c->good_stdin)
717 good_stdin_used = 0;
719 dontcare = c->file->dontcare;
721 if (child_failed && !c->noerror && !ignore_errors_flag)
723 /* The commands failed. Write an error message,
724 delete non-precious targets, and abort. */
725 static int delete_on_error = -1;
727 if (!dontcare)
728 child_error (c->file->name, exit_code, exit_sig, coredump, 0);
730 c->file->update_status = 2;
731 if (delete_on_error == -1)
733 struct file *f = lookup_file (".DELETE_ON_ERROR");
734 delete_on_error = f != 0 && f->is_target;
736 if (exit_sig != 0 || delete_on_error)
737 delete_child_targets (c);
739 else
741 if (child_failed)
743 /* The commands failed, but we don't care. */
744 child_error (c->file->name,
745 exit_code, exit_sig, coredump, 1);
746 child_failed = 0;
749 /* If there are more commands to run, try to start them. */
750 if (job_next_command (c))
752 if (handling_fatal_signal)
754 /* Never start new commands while we are dying.
755 Since there are more commands that wanted to be run,
756 the target was not completely remade. So we treat
757 this as if a command had failed. */
758 c->file->update_status = 2;
760 else
762 /* Check again whether to start remotely.
763 Whether or not we want to changes over time.
764 Also, start_remote_job may need state set up
765 by start_remote_job_p. */
766 c->remote = start_remote_job_p (0);
767 start_job_command (c);
768 /* Fatal signals are left blocked in case we were
769 about to put that child on the chain. But it is
770 already there, so it is safe for a fatal signal to
771 arrive now; it will clean up this child's targets. */
772 unblock_sigs ();
773 if (c->file->command_state == cs_running)
774 /* We successfully started the new command.
775 Loop to reap more children. */
776 continue;
779 if (c->file->update_status != 0)
780 /* We failed to start the commands. */
781 delete_child_targets (c);
783 else
784 /* There are no more commands. We got through them all
785 without an unignored error. Now the target has been
786 successfully updated. */
787 c->file->update_status = 0;
790 /* When we get here, all the commands for C->file are finished
791 (or aborted) and C->file->update_status contains 0 or 2. But
792 C->file->command_state is still cs_running if all the commands
793 ran; notice_finish_file looks for cs_running to tell it that
794 it's interesting to check the file's modtime again now. */
796 if (! handling_fatal_signal)
797 /* Notice if the target of the commands has been changed.
798 This also propagates its values for command_state and
799 update_status to its also_make files. */
800 notice_finished_file (c->file);
802 DB (DB_JOBS, (_("Removing child 0x%08lx PID %ld%s from chain.\n"),
803 (unsigned long int) c, (long) c->pid,
804 c->remote ? _(" (remote)") : ""));
806 /* Block fatal signals while frobnicating the list, so that
807 children and job_slots_used are always consistent. Otherwise
808 a fatal signal arriving after the child is off the chain and
809 before job_slots_used is decremented would believe a child was
810 live and call reap_children again. */
811 block_sigs ();
813 /* There is now another slot open. */
814 if (job_slots_used > 0)
815 --job_slots_used;
817 /* Remove the child from the chain and free it. */
818 if (lastc == 0)
819 children = c->next;
820 else
821 lastc->next = c->next;
823 free_child (c);
825 unblock_sigs ();
827 /* If the job failed, and the -k flag was not given, die,
828 unless we are already in the process of dying. */
829 if (!err && child_failed && !dontcare && !keep_going_flag &&
830 /* fatal_error_signal will die with the right signal. */
831 !handling_fatal_signal)
832 die (2);
834 /* Only block for one child. */
835 block = 0;
838 return;
841 /* Free the storage allocated for CHILD. */
843 static void
844 free_child (struct child *child)
846 if (!jobserver_tokens)
847 fatal (NILF, "INTERNAL: Freeing child 0x%08lx (%s) but no tokens left!\n",
848 (unsigned long int) child, child->file->name);
850 /* If we're using the jobserver and this child is not the only outstanding
851 job, put a token back into the pipe for it. */
853 if (job_fds[1] >= 0 && jobserver_tokens > 1)
855 char token = '+';
856 int r;
858 /* Write a job token back to the pipe. */
860 EINTRLOOP (r, write (job_fds[1], &token, 1));
861 if (r != 1)
862 pfatal_with_name (_("write jobserver"));
864 DB (DB_JOBS, (_("Released token for child 0x%08lx (%s).\n"),
865 (unsigned long int) child, child->file->name));
868 --jobserver_tokens;
870 if (handling_fatal_signal) /* Don't bother free'ing if about to die. */
871 return;
873 if (child->command_lines != 0)
875 register unsigned int i;
876 for (i = 0; i < child->file->cmds->ncommand_lines; ++i)
877 free (child->command_lines[i]);
878 free ((char *) child->command_lines);
881 if (child->environment != 0)
883 register char **ep = child->environment;
884 while (*ep != 0)
885 free (*ep++);
886 free ((char *) child->environment);
889 free ((char *) child);
892 #ifdef POSIX
893 extern sigset_t fatal_signal_set;
894 #endif
896 void
897 block_sigs (void)
899 #ifdef POSIX
900 (void) sigprocmask (SIG_BLOCK, &fatal_signal_set, (sigset_t *) 0);
901 #else
902 # ifdef HAVE_SIGSETMASK
903 (void) sigblock (fatal_signal_mask);
904 # endif
905 #endif
908 #ifdef POSIX
909 void
910 unblock_sigs (void)
912 sigset_t empty;
913 sigemptyset (&empty);
914 sigprocmask (SIG_SETMASK, &empty, (sigset_t *) 0);
916 #endif
918 #ifdef MAKE_JOBSERVER
919 RETSIGTYPE
920 job_noop (int sig UNUSED)
923 /* Set the child handler action flags to FLAGS. */
924 static void
925 set_child_handler_action_flags (int set_handler, int set_alarm)
927 struct sigaction sa;
929 #ifdef __EMX__
930 /* The child handler must be turned off here. */
931 signal (SIGCHLD, SIG_DFL);
932 #endif
934 bzero ((char *) &sa, sizeof sa);
935 sa.sa_handler = child_handler;
936 sa.sa_flags = set_handler ? 0 : SA_RESTART;
937 #if defined SIGCHLD
938 sigaction (SIGCHLD, &sa, NULL);
939 #endif
940 #if defined SIGCLD && SIGCLD != SIGCHLD
941 sigaction (SIGCLD, &sa, NULL);
942 #endif
943 #if defined SIGALRM
944 if (set_alarm)
946 /* If we're about to enter the read(), set an alarm to wake up in a
947 second so we can check if the load has dropped and we can start more
948 work. On the way out, turn off the alarm and set SIG_DFL. */
949 alarm (set_handler ? 1 : 0);
950 sa.sa_handler = set_handler ? job_noop : SIG_DFL;
951 sa.sa_flags = 0;
952 sigaction (SIGALRM, &sa, NULL);
954 #endif
956 #endif
959 /* Start a job to run the commands specified in CHILD.
960 CHILD is updated to reflect the commands and ID of the child process.
962 NOTE: On return fatal signals are blocked! The caller is responsible
963 for calling `unblock_sigs', once the new child is safely on the chain so
964 it can be cleaned up in the event of a fatal signal. */
966 static void
967 start_job_command (struct child *child)
969 #if !defined(_AMIGA) && !defined(WINDOWS32)
970 static int bad_stdin = -1;
971 #endif
972 register char *p;
973 int flags;
974 #ifdef VMS
975 char *argv;
976 #else
977 char **argv;
978 #endif
980 /* If we have a completely empty commandset, stop now. */
981 if (!child->command_ptr)
982 goto next_command;
984 /* Combine the flags parsed for the line itself with
985 the flags specified globally for this target. */
986 flags = (child->file->command_flags
987 | child->file->cmds->lines_flags[child->command_line - 1]);
989 p = child->command_ptr;
990 child->noerror = ((flags & COMMANDS_NOERROR) != 0);
992 while (*p != '\0')
994 if (*p == '@')
995 flags |= COMMANDS_SILENT;
996 else if (*p == '+')
997 flags |= COMMANDS_RECURSE;
998 else if (*p == '-')
999 child->noerror = 1;
1000 else if (!isblank ((unsigned char)*p))
1001 break;
1002 ++p;
1005 /* Update the file's command flags with any new ones we found. We only
1006 keep the COMMANDS_RECURSE setting. Even this isn't 100% correct; we are
1007 now marking more commands recursive than should be in the case of
1008 multiline define/endef scripts where only one line is marked "+". In
1009 order to really fix this, we'll have to keep a lines_flags for every
1010 actual line, after expansion. */
1011 child->file->cmds->lines_flags[child->command_line - 1]
1012 |= flags & COMMANDS_RECURSE;
1014 /* Figure out an argument list from this command line. */
1017 char *end = 0;
1018 #ifdef VMS
1019 argv = p;
1020 #else
1021 argv = construct_command_argv (p, &end, child->file, &child->sh_batch_file);
1022 #endif
1023 if (end == NULL)
1024 child->command_ptr = NULL;
1025 else
1027 *end++ = '\0';
1028 child->command_ptr = end;
1032 /* If -q was given, say that updating `failed' if there was any text on the
1033 command line, or `succeeded' otherwise. The exit status of 1 tells the
1034 user that -q is saying `something to do'; the exit status for a random
1035 error is 2. */
1036 if (argv != 0 && question_flag && !(flags & COMMANDS_RECURSE))
1038 #ifndef VMS
1039 free (argv[0]);
1040 free ((char *) argv);
1041 #endif
1042 child->file->update_status = 1;
1043 notice_finished_file (child->file);
1044 return;
1047 if (touch_flag && !(flags & COMMANDS_RECURSE))
1049 /* Go on to the next command. It might be the recursive one.
1050 We construct ARGV only to find the end of the command line. */
1051 #ifndef VMS
1052 if (argv)
1054 free (argv[0]);
1055 free ((char *) argv);
1057 #endif
1058 argv = 0;
1061 if (argv == 0)
1063 next_command:
1064 #ifdef __MSDOS__
1065 execute_by_shell = 0; /* in case construct_command_argv sets it */
1066 #endif
1067 /* This line has no commands. Go to the next. */
1068 if (job_next_command (child))
1069 start_job_command (child);
1070 else
1072 /* No more commands. Make sure we're "running"; we might not be if
1073 (e.g.) all commands were skipped due to -n. */
1074 set_command_state (child->file, cs_running);
1075 child->file->update_status = 0;
1076 notice_finished_file (child->file);
1078 return;
1081 /* Print out the command. If silent, we call `message' with null so it
1082 can log the working directory before the command's own error messages
1083 appear. */
1085 message (0, (just_print_flag || (!(flags & COMMANDS_SILENT) && !silent_flag))
1086 ? "%s" : (char *) 0, p);
1088 /* Tell update_goal_chain that a command has been started on behalf of
1089 this target. It is important that this happens here and not in
1090 reap_children (where we used to do it), because reap_children might be
1091 reaping children from a different target. We want this increment to
1092 guaranteedly indicate that a command was started for the dependency
1093 chain (i.e., update_file recursion chain) we are processing. */
1095 ++commands_started;
1097 /* Optimize an empty command. People use this for timestamp rules,
1098 so avoid forking a useless shell. Do this after we increment
1099 commands_started so make still treats this special case as if it
1100 performed some action (makes a difference as to what messages are
1101 printed, etc. */
1103 #if !defined(VMS) && !defined(_AMIGA)
1104 if (
1105 #if defined __MSDOS__ || defined (__EMX__)
1106 unixy_shell /* the test is complicated and we already did it */
1107 #else
1108 (argv[0] && !strcmp (argv[0], "/bin/sh"))
1109 #endif
1110 && (argv[1]
1111 && argv[1][0] == '-' && argv[1][1] == 'c' && argv[1][2] == '\0')
1112 && (argv[2] && argv[2][0] == ':' && argv[2][1] == '\0')
1113 && argv[3] == NULL)
1115 free (argv[0]);
1116 free ((char *) argv);
1117 goto next_command;
1119 #endif /* !VMS && !_AMIGA */
1121 /* If -n was given, recurse to get the next line in the sequence. */
1123 if (just_print_flag && !(flags & COMMANDS_RECURSE))
1125 #ifndef VMS
1126 free (argv[0]);
1127 free ((char *) argv);
1128 #endif
1129 goto next_command;
1132 /* Flush the output streams so they won't have things written twice. */
1134 fflush (stdout);
1135 fflush (stderr);
1137 #ifndef VMS
1138 #if !defined(WINDOWS32) && !defined(_AMIGA) && !defined(__MSDOS__)
1140 /* Set up a bad standard input that reads from a broken pipe. */
1142 if (bad_stdin == -1)
1144 /* Make a file descriptor that is the read end of a broken pipe.
1145 This will be used for some children's standard inputs. */
1146 int pd[2];
1147 if (pipe (pd) == 0)
1149 /* Close the write side. */
1150 (void) close (pd[1]);
1151 /* Save the read side. */
1152 bad_stdin = pd[0];
1154 /* Set the descriptor to close on exec, so it does not litter any
1155 child's descriptor table. When it is dup2'd onto descriptor 0,
1156 that descriptor will not close on exec. */
1157 CLOSE_ON_EXEC (bad_stdin);
1161 #endif /* !WINDOWS32 && !_AMIGA && !__MSDOS__ */
1163 /* Decide whether to give this child the `good' standard input
1164 (one that points to the terminal or whatever), or the `bad' one
1165 that points to the read side of a broken pipe. */
1167 child->good_stdin = !good_stdin_used;
1168 if (child->good_stdin)
1169 good_stdin_used = 1;
1171 #endif /* !VMS */
1173 child->deleted = 0;
1175 #ifndef _AMIGA
1176 /* Set up the environment for the child. */
1177 if (child->environment == 0)
1178 child->environment = target_environment (child->file);
1179 #endif
1181 #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
1183 #ifndef VMS
1184 /* start_waiting_job has set CHILD->remote if we can start a remote job. */
1185 if (child->remote)
1187 int is_remote, id, used_stdin;
1188 if (start_remote_job (argv, child->environment,
1189 child->good_stdin ? 0 : bad_stdin,
1190 &is_remote, &id, &used_stdin))
1191 /* Don't give up; remote execution may fail for various reasons. If
1192 so, simply run the job locally. */
1193 goto run_local;
1194 else
1196 if (child->good_stdin && !used_stdin)
1198 child->good_stdin = 0;
1199 good_stdin_used = 0;
1201 child->remote = is_remote;
1202 child->pid = id;
1205 else
1206 #endif /* !VMS */
1208 /* Fork the child process. */
1210 char **parent_environ;
1212 run_local:
1213 block_sigs ();
1215 child->remote = 0;
1217 #ifdef VMS
1218 if (!child_execute_job (argv, child)) {
1219 /* Fork failed! */
1220 perror_with_name ("vfork", "");
1221 goto error;
1224 #else
1226 parent_environ = environ;
1228 # ifdef __EMX__
1229 /* If we aren't running a recursive command and we have a jobserver
1230 pipe, close it before exec'ing. */
1231 if (!(flags & COMMANDS_RECURSE) && job_fds[0] >= 0)
1233 CLOSE_ON_EXEC (job_fds[0]);
1234 CLOSE_ON_EXEC (job_fds[1]);
1236 if (job_rfd >= 0)
1237 CLOSE_ON_EXEC (job_rfd);
1239 /* Never use fork()/exec() here! Use spawn() instead in exec_command() */
1240 child->pid = child_execute_job (child->good_stdin ? 0 : bad_stdin, 1,
1241 argv, child->environment);
1242 if (child->pid < 0)
1244 /* spawn failed! */
1245 unblock_sigs ();
1246 perror_with_name ("spawn", "");
1247 goto error;
1250 /* undo CLOSE_ON_EXEC() after the child process has been started */
1251 if (!(flags & COMMANDS_RECURSE) && job_fds[0] >= 0)
1253 fcntl (job_fds[0], F_SETFD, 0);
1254 fcntl (job_fds[1], F_SETFD, 0);
1256 if (job_rfd >= 0)
1257 fcntl (job_rfd, F_SETFD, 0);
1259 #else /* !__EMX__ */
1261 child->pid = vfork ();
1262 environ = parent_environ; /* Restore value child may have clobbered. */
1263 if (child->pid == 0)
1265 /* We are the child side. */
1266 unblock_sigs ();
1268 /* If we aren't running a recursive command and we have a jobserver
1269 pipe, close it before exec'ing. */
1270 if (!(flags & COMMANDS_RECURSE) && job_fds[0] >= 0)
1272 close (job_fds[0]);
1273 close (job_fds[1]);
1275 if (job_rfd >= 0)
1276 close (job_rfd);
1278 child_execute_job (child->good_stdin ? 0 : bad_stdin, 1,
1279 argv, child->environment);
1281 else if (child->pid < 0)
1283 /* Fork failed! */
1284 unblock_sigs ();
1285 perror_with_name ("vfork", "");
1286 goto error;
1288 # endif /* !__EMX__ */
1289 #endif /* !VMS */
1292 #else /* __MSDOS__ or Amiga or WINDOWS32 */
1293 #ifdef __MSDOS__
1295 int proc_return;
1297 block_sigs ();
1298 dos_status = 0;
1300 /* We call `system' to do the job of the SHELL, since stock DOS
1301 shell is too dumb. Our `system' knows how to handle long
1302 command lines even if pipes/redirection is needed; it will only
1303 call COMMAND.COM when its internal commands are used. */
1304 if (execute_by_shell)
1306 char *cmdline = argv[0];
1307 /* We don't have a way to pass environment to `system',
1308 so we need to save and restore ours, sigh... */
1309 char **parent_environ = environ;
1311 environ = child->environment;
1313 /* If we have a *real* shell, tell `system' to call
1314 it to do everything for us. */
1315 if (unixy_shell)
1317 /* A *real* shell on MSDOS may not support long
1318 command lines the DJGPP way, so we must use `system'. */
1319 cmdline = argv[2]; /* get past "shell -c" */
1322 dos_command_running = 1;
1323 proc_return = system (cmdline);
1324 environ = parent_environ;
1325 execute_by_shell = 0; /* for the next time */
1327 else
1329 dos_command_running = 1;
1330 proc_return = spawnvpe (P_WAIT, argv[0], argv, child->environment);
1333 /* Need to unblock signals before turning off
1334 dos_command_running, so that child's signals
1335 will be treated as such (see fatal_error_signal). */
1336 unblock_sigs ();
1337 dos_command_running = 0;
1339 /* If the child got a signal, dos_status has its
1340 high 8 bits set, so be careful not to alter them. */
1341 if (proc_return == -1)
1342 dos_status |= 0xff;
1343 else
1344 dos_status |= (proc_return & 0xff);
1345 ++dead_children;
1346 child->pid = dos_pid++;
1348 #endif /* __MSDOS__ */
1349 #ifdef _AMIGA
1350 amiga_status = MyExecute (argv);
1352 ++dead_children;
1353 child->pid = amiga_pid++;
1354 if (amiga_batch_file)
1356 amiga_batch_file = 0;
1357 DeleteFile (amiga_bname); /* Ignore errors. */
1359 #endif /* Amiga */
1360 #ifdef WINDOWS32
1362 HANDLE hPID;
1363 char* arg0;
1365 /* make UNC paths safe for CreateProcess -- backslash format */
1366 arg0 = argv[0];
1367 if (arg0 && arg0[0] == '/' && arg0[1] == '/')
1368 for ( ; arg0 && *arg0; arg0++)
1369 if (*arg0 == '/')
1370 *arg0 = '\\';
1372 /* make sure CreateProcess() has Path it needs */
1373 sync_Path_environment();
1375 hPID = process_easy(argv, child->environment);
1377 if (hPID != INVALID_HANDLE_VALUE)
1378 child->pid = (int) hPID;
1379 else {
1380 int i;
1381 unblock_sigs();
1382 fprintf(stderr,
1383 _("process_easy() failed failed to launch process (e=%ld)\n"),
1384 process_last_err(hPID));
1385 for (i = 0; argv[i]; i++)
1386 fprintf(stderr, "%s ", argv[i]);
1387 fprintf(stderr, _("\nCounted %d args in failed launch\n"), i);
1390 #endif /* WINDOWS32 */
1391 #endif /* __MSDOS__ or Amiga or WINDOWS32 */
1393 /* Bump the number of jobs started in this second. */
1394 ++job_counter;
1396 /* We are the parent side. Set the state to
1397 say the commands are running and return. */
1399 set_command_state (child->file, cs_running);
1401 /* Free the storage used by the child's argument list. */
1402 #ifndef VMS
1403 free (argv[0]);
1404 free ((char *) argv);
1405 #endif
1407 return;
1409 error:
1410 child->file->update_status = 2;
1411 notice_finished_file (child->file);
1412 return;
1415 /* Try to start a child running.
1416 Returns nonzero if the child was started (and maybe finished), or zero if
1417 the load was too high and the child was put on the `waiting_jobs' chain. */
1419 static int
1420 start_waiting_job (struct child *c)
1422 struct file *f = c->file;
1424 /* If we can start a job remotely, we always want to, and don't care about
1425 the local load average. We record that the job should be started
1426 remotely in C->remote for start_job_command to test. */
1428 c->remote = start_remote_job_p (1);
1430 /* If we are running at least one job already and the load average
1431 is too high, make this one wait. */
1432 if (!c->remote && job_slots_used > 0 && load_too_high ())
1434 /* Put this child on the chain of children waiting for the load average
1435 to go down. */
1436 set_command_state (f, cs_running);
1437 c->next = waiting_jobs;
1438 waiting_jobs = c;
1439 return 0;
1442 /* Start the first command; reap_children will run later command lines. */
1443 start_job_command (c);
1445 switch (f->command_state)
1447 case cs_running:
1448 c->next = children;
1449 DB (DB_JOBS, (_("Putting child 0x%08lx (%s) PID %ld%s on the chain.\n"),
1450 (unsigned long int) c, c->file->name,
1451 (long) c->pid, c->remote ? _(" (remote)") : ""));
1452 children = c;
1453 /* One more job slot is in use. */
1454 ++job_slots_used;
1455 unblock_sigs ();
1456 break;
1458 case cs_not_started:
1459 /* All the command lines turned out to be empty. */
1460 f->update_status = 0;
1461 /* FALLTHROUGH */
1463 case cs_finished:
1464 notice_finished_file (f);
1465 free_child (c);
1466 break;
1468 default:
1469 assert (f->command_state == cs_finished);
1470 break;
1473 return 1;
1476 /* Create a `struct child' for FILE and start its commands running. */
1478 void
1479 new_job (struct file *file)
1481 register struct commands *cmds = file->cmds;
1482 register struct child *c;
1483 char **lines;
1484 register unsigned int i;
1486 /* Let any previously decided-upon jobs that are waiting
1487 for the load to go down start before this new one. */
1488 start_waiting_jobs ();
1490 /* Reap any children that might have finished recently. */
1491 reap_children (0, 0);
1493 /* Chop the commands up into lines if they aren't already. */
1494 chop_commands (cmds);
1496 /* Expand the command lines and store the results in LINES. */
1497 lines = (char **) xmalloc (cmds->ncommand_lines * sizeof (char *));
1498 for (i = 0; i < cmds->ncommand_lines; ++i)
1500 /* Collapse backslash-newline combinations that are inside variable
1501 or function references. These are left alone by the parser so
1502 that they will appear in the echoing of commands (where they look
1503 nice); and collapsed by construct_command_argv when it tokenizes.
1504 But letting them survive inside function invocations loses because
1505 we don't want the functions to see them as part of the text. */
1507 char *in, *out, *ref;
1509 /* IN points to where in the line we are scanning.
1510 OUT points to where in the line we are writing.
1511 When we collapse a backslash-newline combination,
1512 IN gets ahead of OUT. */
1514 in = out = cmds->command_lines[i];
1515 while ((ref = strchr (in, '$')) != 0)
1517 ++ref; /* Move past the $. */
1519 if (out != in)
1520 /* Copy the text between the end of the last chunk
1521 we processed (where IN points) and the new chunk
1522 we are about to process (where REF points). */
1523 bcopy (in, out, ref - in);
1525 /* Move both pointers past the boring stuff. */
1526 out += ref - in;
1527 in = ref;
1529 if (*ref == '(' || *ref == '{')
1531 char openparen = *ref;
1532 char closeparen = openparen == '(' ? ')' : '}';
1533 int count;
1534 char *p;
1536 *out++ = *in++; /* Copy OPENPAREN. */
1537 /* IN now points past the opening paren or brace.
1538 Count parens or braces until it is matched. */
1539 count = 0;
1540 while (*in != '\0')
1542 if (*in == closeparen && --count < 0)
1543 break;
1544 else if (*in == '\\' && in[1] == '\n')
1546 /* We have found a backslash-newline inside a
1547 variable or function reference. Eat it and
1548 any following whitespace. */
1550 int quoted = 0;
1551 for (p = in - 1; p > ref && *p == '\\'; --p)
1552 quoted = !quoted;
1554 if (quoted)
1555 /* There were two or more backslashes, so this is
1556 not really a continuation line. We don't collapse
1557 the quoting backslashes here as is done in
1558 collapse_continuations, because the line will
1559 be collapsed again after expansion. */
1560 *out++ = *in++;
1561 else
1563 /* Skip the backslash, newline and
1564 any following whitespace. */
1565 in = next_token (in + 2);
1567 /* Discard any preceding whitespace that has
1568 already been written to the output. */
1569 while (out > ref
1570 && isblank ((unsigned char)out[-1]))
1571 --out;
1573 /* Replace it all with a single space. */
1574 *out++ = ' ';
1577 else
1579 if (*in == openparen)
1580 ++count;
1582 *out++ = *in++;
1588 /* There are no more references in this line to worry about.
1589 Copy the remaining uninteresting text to the output. */
1590 if (out != in)
1591 strcpy (out, in);
1593 /* Finally, expand the line. */
1594 lines[i] = allocated_variable_expand_for_file (cmds->command_lines[i],
1595 file);
1598 /* Start the command sequence, record it in a new
1599 `struct child', and add that to the chain. */
1601 c = (struct child *) xmalloc (sizeof (struct child));
1602 bzero ((char *)c, sizeof (struct child));
1603 c->file = file;
1604 c->command_lines = lines;
1605 c->sh_batch_file = NULL;
1607 /* Fetch the first command line to be run. */
1608 job_next_command (c);
1610 /* Wait for a job slot to be freed up. If we allow an infinite number
1611 don't bother; also job_slots will == 0 if we're using the jobserver. */
1613 if (job_slots != 0)
1614 while (job_slots_used == job_slots)
1615 reap_children (1, 0);
1617 #ifdef MAKE_JOBSERVER
1618 /* If we are controlling multiple jobs make sure we have a token before
1619 starting the child. */
1621 /* This can be inefficient. There's a decent chance that this job won't
1622 actually have to run any subprocesses: the command script may be empty
1623 or otherwise optimized away. It would be nice if we could defer
1624 obtaining a token until just before we need it, in start_job_command.
1625 To do that we'd need to keep track of whether we'd already obtained a
1626 token (since start_job_command is called for each line of the job, not
1627 just once). Also more thought needs to go into the entire algorithm;
1628 this is where the old parallel job code waits, so... */
1630 else if (job_fds[0] >= 0)
1631 while (1)
1633 char token;
1634 int got_token;
1635 int saved_errno;
1637 DB (DB_JOBS, ("Need a job token; we %shave children\n",
1638 children ? "" : "don't "));
1640 /* If we don't already have a job started, use our "free" token. */
1641 if (!jobserver_tokens)
1642 break;
1644 /* Read a token. As long as there's no token available we'll block.
1645 We enable interruptible system calls before the read(2) so that if
1646 we get a SIGCHLD while we're waiting, we'll return with EINTR and
1647 we can process the death(s) and return tokens to the free pool.
1649 Once we return from the read, we immediately reinstate restartable
1650 system calls. This allows us to not worry about checking for
1651 EINTR on all the other system calls in the program.
1653 There is one other twist: there is a span between the time
1654 reap_children() does its last check for dead children and the time
1655 the read(2) call is entered, below, where if a child dies we won't
1656 notice. This is extremely serious as it could cause us to
1657 deadlock, given the right set of events.
1659 To avoid this, we do the following: before we reap_children(), we
1660 dup(2) the read FD on the jobserver pipe. The read(2) call below
1661 uses that new FD. In the signal handler, we close that FD. That
1662 way, if a child dies during the section mentioned above, the
1663 read(2) will be invoked with an invalid FD and will return
1664 immediately with EBADF. */
1666 /* Make sure we have a dup'd FD. */
1667 if (job_rfd < 0)
1669 DB (DB_JOBS, ("Duplicate the job FD\n"));
1670 job_rfd = dup (job_fds[0]);
1673 /* Reap anything that's currently waiting. */
1674 reap_children (0, 0);
1676 /* Kick off any jobs we have waiting for an opportunity that
1677 can run now (ie waiting for load). */
1678 start_waiting_jobs ();
1680 /* If our "free" slot has become available, use it; we don't need an
1681 actual token. */
1682 if (!jobserver_tokens)
1683 break;
1685 /* There must be at least one child already, or we have no business
1686 waiting for a token. */
1687 if (!children)
1688 fatal (NILF, "INTERNAL: no children as we go to sleep on read\n");
1690 /* Set interruptible system calls, and read() for a job token. */
1691 set_child_handler_action_flags (1, waiting_jobs != NULL);
1692 got_token = read (job_rfd, &token, 1);
1693 saved_errno = errno;
1694 set_child_handler_action_flags (0, waiting_jobs != NULL);
1696 /* If we got one, we're done here. */
1697 if (got_token == 1)
1699 DB (DB_JOBS, (_("Obtained token for child 0x%08lx (%s).\n"),
1700 (unsigned long int) c, c->file->name));
1701 break;
1704 /* If the error _wasn't_ expected (EINTR or EBADF), punt. Otherwise,
1705 go back and reap_children(), and try again. */
1706 errno = saved_errno;
1707 if (errno != EINTR && errno != EBADF)
1708 pfatal_with_name (_("read jobs pipe"));
1709 if (errno == EBADF)
1710 DB (DB_JOBS, ("Read returned EBADF.\n"));
1712 #endif
1714 ++jobserver_tokens;
1716 /* The job is now primed. Start it running.
1717 (This will notice if there are in fact no commands.) */
1718 (void) start_waiting_job (c);
1720 if (job_slots == 1 || not_parallel)
1721 /* Since there is only one job slot, make things run linearly.
1722 Wait for the child to die, setting the state to `cs_finished'. */
1723 while (file->command_state == cs_running)
1724 reap_children (1, 0);
1726 return;
1729 /* Move CHILD's pointers to the next command for it to execute.
1730 Returns nonzero if there is another command. */
1732 static int
1733 job_next_command (struct child *child)
1735 while (child->command_ptr == 0 || *child->command_ptr == '\0')
1737 /* There are no more lines in the expansion of this line. */
1738 if (child->command_line == child->file->cmds->ncommand_lines)
1740 /* There are no more lines to be expanded. */
1741 child->command_ptr = 0;
1742 return 0;
1744 else
1745 /* Get the next line to run. */
1746 child->command_ptr = child->command_lines[child->command_line++];
1748 return 1;
1751 /* Determine if the load average on the system is too high to start a new job.
1752 The real system load average is only recomputed once a second. However, a
1753 very parallel make can easily start tens or even hundreds of jobs in a
1754 second, which brings the system to its knees for a while until that first
1755 batch of jobs clears out.
1757 To avoid this we use a weighted algorithm to try to account for jobs which
1758 have been started since the last second, and guess what the load average
1759 would be now if it were computed.
1761 This algorithm was provided by Thomas Riedl <thomas.riedl@siemens.com>,
1762 who writes:
1764 ! calculate something load-oid and add to the observed sys.load,
1765 ! so that latter can catch up:
1766 ! - every job started increases jobctr;
1767 ! - every dying job decreases a positive jobctr;
1768 ! - the jobctr value gets zeroed every change of seconds,
1769 ! after its value*weight_b is stored into the 'backlog' value last_sec
1770 ! - weight_a times the sum of jobctr and last_sec gets
1771 ! added to the observed sys.load.
1773 ! The two weights have been tried out on 24 and 48 proc. Sun Solaris-9
1774 ! machines, using a several-thousand-jobs-mix of cpp, cc, cxx and smallish
1775 ! sub-shelled commands (rm, echo, sed...) for tests.
1776 ! lowering the 'direct influence' factor weight_a (e.g. to 0.1)
1777 ! resulted in significant excession of the load limit, raising it
1778 ! (e.g. to 0.5) took bad to small, fast-executing jobs and didn't
1779 ! reach the limit in most test cases.
1781 ! lowering the 'history influence' weight_b (e.g. to 0.1) resulted in
1782 ! exceeding the limit for longer-running stuff (compile jobs in
1783 ! the .5 to 1.5 sec. range),raising it (e.g. to 0.5) overrepresented
1784 ! small jobs' effects.
1788 #define LOAD_WEIGHT_A 0.25
1789 #define LOAD_WEIGHT_B 0.25
1791 static int
1792 load_too_high (void)
1794 #if defined(__MSDOS__) || defined(VMS) || defined(_AMIGA) || defined(__riscos__)
1795 return 1;
1796 #else
1797 static double last_sec;
1798 static time_t last_now;
1799 double load, guess;
1800 time_t now;
1802 if (max_load_average < 0)
1803 return 0;
1805 /* Find the real system load average. */
1806 make_access ();
1807 if (getloadavg (&load, 1) != 1)
1809 static int lossage = -1;
1810 /* Complain only once for the same error. */
1811 if (lossage == -1 || errno != lossage)
1813 if (errno == 0)
1814 /* An errno value of zero means getloadavg is just unsupported. */
1815 error (NILF,
1816 _("cannot enforce load limits on this operating system"));
1817 else
1818 perror_with_name (_("cannot enforce load limit: "), "getloadavg");
1820 lossage = errno;
1821 load = 0;
1823 user_access ();
1825 /* If we're in a new second zero the counter and correct the backlog
1826 value. Only keep the backlog for one extra second; after that it's 0. */
1827 now = time (NULL);
1828 if (last_now < now)
1830 if (last_now == now - 1)
1831 last_sec = LOAD_WEIGHT_B * job_counter;
1832 else
1833 last_sec = 0.0;
1835 job_counter = 0;
1836 last_now = now;
1839 /* Try to guess what the load would be right now. */
1840 guess = load + (LOAD_WEIGHT_A * (job_counter + last_sec));
1842 DB (DB_JOBS, ("Estimated system load = %f (actual = %f) (max requested = %f)\n",
1843 guess, load, max_load_average));
1845 return guess >= max_load_average;
1846 #endif
1849 /* Start jobs that are waiting for the load to be lower. */
1851 void
1852 start_waiting_jobs (void)
1854 struct child *job;
1856 if (waiting_jobs == 0)
1857 return;
1861 /* Check for recently deceased descendants. */
1862 reap_children (0, 0);
1864 /* Take a job off the waiting list. */
1865 job = waiting_jobs;
1866 waiting_jobs = job->next;
1868 /* Try to start that job. We break out of the loop as soon
1869 as start_waiting_job puts one back on the waiting list. */
1871 while (start_waiting_job (job) && waiting_jobs != 0);
1873 return;
1876 #ifndef WINDOWS32
1878 /* EMX: Start a child process. This function returns the new pid. */
1879 # if defined __MSDOS__ || defined __EMX__
1881 child_execute_job (int stdin_fd, int stdout_fd, char **argv, char **envp)
1883 int pid;
1884 /* stdin_fd == 0 means: nothing to do for stdin;
1885 stdout_fd == 1 means: nothing to do for stdout */
1886 int save_stdin = (stdin_fd != 0) ? dup (0) : 0;
1887 int save_stdout = (stdout_fd != 1) ? dup (1): 1;
1889 /* < 0 only if dup() failed */
1890 if (save_stdin < 0)
1891 fatal (NILF, _("no more file handles: could not duplicate stdin\n"));
1892 if (save_stdout < 0)
1893 fatal (NILF, _("no more file handles: could not duplicate stdout\n"));
1895 /* Close unnecessary file handles for the child. */
1896 if (save_stdin != 0)
1897 CLOSE_ON_EXEC (save_stdin);
1898 if (save_stdout != 1)
1899 CLOSE_ON_EXEC (save_stdout);
1901 /* Connect the pipes to the child process. */
1902 if (stdin_fd != 0)
1903 (void) dup2 (stdin_fd, 0);
1904 if (stdout_fd != 1)
1905 (void) dup2 (stdout_fd, 1);
1907 /* stdin_fd and stdout_fd must be closed on exit because we are
1908 still in the parent process */
1909 if (stdin_fd != 0)
1910 CLOSE_ON_EXEC (stdin_fd);
1911 if (stdout_fd != 1)
1912 CLOSE_ON_EXEC (stdout_fd);
1914 /* Run the command. */
1915 pid = exec_command (argv, envp);
1917 /* Restore stdout/stdin of the parent and close temporary FDs. */
1918 if (stdin_fd != 0)
1920 if (dup2 (save_stdin, 0) != 0)
1921 fatal (NILF, _("Could not restore stdin\n"));
1922 else
1923 close (save_stdin);
1926 if (stdout_fd != 1)
1928 if (dup2 (save_stdout, 1) != 1)
1929 fatal (NILF, _("Could not restore stdout\n"));
1930 else
1931 close (save_stdout);
1934 return pid;
1937 #elif !defined (_AMIGA) && !defined (__MSDOS__) && !defined (VMS)
1939 /* UNIX:
1940 Replace the current process with one executing the command in ARGV.
1941 STDIN_FD and STDOUT_FD are used as the process's stdin and stdout; ENVP is
1942 the environment of the new program. This function does not return. */
1943 void
1944 child_execute_job (int stdin_fd, int stdout_fd, char **argv, char **envp)
1946 if (stdin_fd != 0)
1947 (void) dup2 (stdin_fd, 0);
1948 if (stdout_fd != 1)
1949 (void) dup2 (stdout_fd, 1);
1950 if (stdin_fd != 0)
1951 (void) close (stdin_fd);
1952 if (stdout_fd != 1)
1953 (void) close (stdout_fd);
1955 /* Run the command. */
1956 exec_command (argv, envp);
1958 #endif /* !AMIGA && !__MSDOS__ && !VMS */
1959 #endif /* !WINDOWS32 */
1961 #ifndef _AMIGA
1962 /* Replace the current process with one running the command in ARGV,
1963 with environment ENVP. This function does not return. */
1965 /* EMX: This function returns the pid of the child process. */
1966 # ifdef __EMX__
1968 # else
1969 void
1970 # endif
1971 exec_command (char **argv, char **envp)
1973 #ifdef VMS
1974 /* to work around a problem with signals and execve: ignore them */
1975 #ifdef SIGCHLD
1976 signal (SIGCHLD,SIG_IGN);
1977 #endif
1978 /* Run the program. */
1979 execve (argv[0], argv, envp);
1980 perror_with_name ("execve: ", argv[0]);
1981 _exit (EXIT_FAILURE);
1982 #else
1983 #ifdef WINDOWS32
1984 HANDLE hPID;
1985 HANDLE hWaitPID;
1986 int err = 0;
1987 int exit_code = EXIT_FAILURE;
1989 /* make sure CreateProcess() has Path it needs */
1990 sync_Path_environment();
1992 /* launch command */
1993 hPID = process_easy(argv, envp);
1995 /* make sure launch ok */
1996 if (hPID == INVALID_HANDLE_VALUE)
1998 int i;
1999 fprintf(stderr,
2000 _("process_easy() failed failed to launch process (e=%ld)\n"),
2001 process_last_err(hPID));
2002 for (i = 0; argv[i]; i++)
2003 fprintf(stderr, "%s ", argv[i]);
2004 fprintf(stderr, _("\nCounted %d args in failed launch\n"), i);
2005 exit(EXIT_FAILURE);
2008 /* wait and reap last child */
2009 hWaitPID = process_wait_for_any();
2010 while (hWaitPID)
2012 /* was an error found on this process? */
2013 err = process_last_err(hWaitPID);
2015 /* get exit data */
2016 exit_code = process_exit_code(hWaitPID);
2018 if (err)
2019 fprintf(stderr, "make (e=%d, rc=%d): %s",
2020 err, exit_code, map_windows32_error_to_string(err));
2022 /* cleanup process */
2023 process_cleanup(hWaitPID);
2025 /* expect to find only last pid, warn about other pids reaped */
2026 if (hWaitPID == hPID)
2027 break;
2028 else
2029 fprintf(stderr,
2030 _("make reaped child pid %d, still waiting for pid %d\n"),
2031 hWaitPID, hPID);
2034 /* return child's exit code as our exit code */
2035 exit(exit_code);
2037 #else /* !WINDOWS32 */
2039 # ifdef __EMX__
2040 int pid;
2041 # endif
2043 /* Be the user, permanently. */
2044 child_access ();
2046 # ifdef __EMX__
2048 /* Run the program. */
2049 pid = spawnvpe (P_NOWAIT, argv[0], argv, envp);
2051 if (pid >= 0)
2052 return pid;
2054 /* the file might have a strange shell extension */
2055 if (errno == ENOENT)
2056 errno = ENOEXEC;
2058 # else
2060 /* Run the program. */
2061 environ = envp;
2062 execvp (argv[0], argv);
2064 # endif /* !__EMX__ */
2066 switch (errno)
2068 case ENOENT:
2069 error (NILF, _("%s: Command not found"), argv[0]);
2070 break;
2071 case ENOEXEC:
2073 /* The file is not executable. Try it as a shell script. */
2074 extern char *getenv ();
2075 char *shell;
2076 char **new_argv;
2077 int argc;
2078 int i=1;
2080 # ifdef __EMX__
2081 /* Do not use $SHELL from the environment */
2082 struct variable *p = lookup_variable ("SHELL", 5);
2083 if (p)
2084 shell = p->value;
2085 else
2086 shell = 0;
2087 # else
2088 shell = getenv ("SHELL");
2089 # endif
2090 if (shell == 0)
2091 shell = default_shell;
2093 argc = 1;
2094 while (argv[argc] != 0)
2095 ++argc;
2097 # ifdef __EMX__
2098 if (!unixy_shell)
2099 ++argc;
2100 # endif
2102 new_argv = (char **) alloca ((1 + argc + 1) * sizeof (char *));
2103 new_argv[0] = shell;
2105 # ifdef __EMX__
2106 if (!unixy_shell)
2108 new_argv[1] = "/c";
2109 ++i;
2110 --argc;
2112 # endif
2114 new_argv[i] = argv[0];
2115 while (argc > 0)
2117 new_argv[i + argc] = argv[argc];
2118 --argc;
2121 # ifdef __EMX__
2122 pid = spawnvpe (P_NOWAIT, shell, new_argv, envp);
2123 if (pid >= 0)
2124 break;
2125 # else
2126 execvp (shell, new_argv);
2127 # endif
2128 if (errno == ENOENT)
2129 error (NILF, _("%s: Shell program not found"), shell);
2130 else
2131 perror_with_name ("execvp: ", shell);
2132 break;
2135 # ifdef __EMX__
2136 case EINVAL:
2137 /* this nasty error was driving me nuts :-( */
2138 error (NILF, _("spawnvpe: environment space might be exhausted"));
2139 /* FALLTHROUGH */
2140 # endif
2142 default:
2143 perror_with_name ("execvp: ", argv[0]);
2144 break;
2147 # ifdef __EMX__
2148 return pid;
2149 # else
2150 _exit (127);
2151 # endif
2152 #endif /* !WINDOWS32 */
2153 #endif /* !VMS */
2155 #else /* On Amiga */
2156 void exec_command (char **argv)
2158 MyExecute (argv);
2161 void clean_tmp (void)
2163 DeleteFile (amiga_bname);
2166 #endif /* On Amiga */
2168 #ifndef VMS
2169 /* Figure out the argument list necessary to run LINE as a command. Try to
2170 avoid using a shell. This routine handles only ' quoting, and " quoting
2171 when no backslash, $ or ` characters are seen in the quotes. Starting
2172 quotes may be escaped with a backslash. If any of the characters in
2173 sh_chars[] is seen, or any of the builtin commands listed in sh_cmds[]
2174 is the first word of a line, the shell is used.
2176 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
2177 If *RESTP is NULL, newlines will be ignored.
2179 SHELL is the shell to use, or nil to use the default shell.
2180 IFS is the value of $IFS, or nil (meaning the default). */
2182 static char **
2183 construct_command_argv_internal (char *line, char **restp, char *shell,
2184 char *ifs, char **batch_filename_ptr)
2186 #ifdef __MSDOS__
2187 /* MSDOS supports both the stock DOS shell and ports of Unixy shells.
2188 We call `system' for anything that requires ``slow'' processing,
2189 because DOS shells are too dumb. When $SHELL points to a real
2190 (unix-style) shell, `system' just calls it to do everything. When
2191 $SHELL points to a DOS shell, `system' does most of the work
2192 internally, calling the shell only for its internal commands.
2193 However, it looks on the $PATH first, so you can e.g. have an
2194 external command named `mkdir'.
2196 Since we call `system', certain characters and commands below are
2197 actually not specific to COMMAND.COM, but to the DJGPP implementation
2198 of `system'. In particular:
2200 The shell wildcard characters are in DOS_CHARS because they will
2201 not be expanded if we call the child via `spawnXX'.
2203 The `;' is in DOS_CHARS, because our `system' knows how to run
2204 multiple commands on a single line.
2206 DOS_CHARS also include characters special to 4DOS/NDOS, so we
2207 won't have to tell one from another and have one more set of
2208 commands and special characters. */
2209 static char sh_chars_dos[] = "*?[];|<>%^&()";
2210 static char *sh_cmds_dos[] = { "break", "call", "cd", "chcp", "chdir", "cls",
2211 "copy", "ctty", "date", "del", "dir", "echo",
2212 "erase", "exit", "for", "goto", "if", "md",
2213 "mkdir", "path", "pause", "prompt", "rd",
2214 "rmdir", "rem", "ren", "rename", "set",
2215 "shift", "time", "type", "ver", "verify",
2216 "vol", ":", 0 };
2218 static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^";
2219 static char *sh_cmds_sh[] = { "cd", "echo", "eval", "exec", "exit", "login",
2220 "logout", "set", "umask", "wait", "while",
2221 "for", "case", "if", ":", ".", "break",
2222 "continue", "export", "read", "readonly",
2223 "shift", "times", "trap", "switch", "unset",
2224 0 };
2226 char *sh_chars;
2227 char **sh_cmds;
2228 #elif defined (__EMX__)
2229 static char sh_chars_dos[] = "*?[];|<>%^&()";
2230 static char *sh_cmds_dos[] = { "break", "call", "cd", "chcp", "chdir", "cls",
2231 "copy", "ctty", "date", "del", "dir", "echo",
2232 "erase", "exit", "for", "goto", "if", "md",
2233 "mkdir", "path", "pause", "prompt", "rd",
2234 "rmdir", "rem", "ren", "rename", "set",
2235 "shift", "time", "type", "ver", "verify",
2236 "vol", ":", 0 };
2238 static char sh_chars_os2[] = "*?[];|<>%^()\"'&";
2239 static char *sh_cmds_os2[] = { "call", "cd", "chcp", "chdir", "cls", "copy",
2240 "date", "del", "detach", "dir", "echo",
2241 "endlocal", "erase", "exit", "for", "goto", "if",
2242 "keys", "md", "mkdir", "move", "path", "pause",
2243 "prompt", "rd", "rem", "ren", "rename", "rmdir",
2244 "set", "setlocal", "shift", "start", "time",
2245 "type", "ver", "verify", "vol", ":", 0 };
2247 static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^~'";
2248 static char *sh_cmds_sh[] = { "echo", "cd", "eval", "exec", "exit", "login",
2249 "logout", "set", "umask", "wait", "while",
2250 "for", "case", "if", ":", ".", "break",
2251 "continue", "export", "read", "readonly",
2252 "shift", "times", "trap", "switch", "unset",
2253 0 };
2254 char *sh_chars;
2255 char **sh_cmds;
2257 #elif defined (_AMIGA)
2258 static char sh_chars[] = "#;\"|<>()?*$`";
2259 static char *sh_cmds[] = { "cd", "eval", "if", "delete", "echo", "copy",
2260 "rename", "set", "setenv", "date", "makedir",
2261 "skip", "else", "endif", "path", "prompt",
2262 "unset", "unsetenv", "version",
2263 0 };
2264 #elif defined (WINDOWS32)
2265 static char sh_chars_dos[] = "\"|&<>";
2266 static char *sh_cmds_dos[] = { "break", "call", "cd", "chcp", "chdir", "cls",
2267 "copy", "ctty", "date", "del", "dir", "echo",
2268 "erase", "exit", "for", "goto", "if", "if", "md",
2269 "mkdir", "path", "pause", "prompt", "rd", "rem",
2270 "ren", "rename", "rmdir", "set", "shift", "time",
2271 "type", "ver", "verify", "vol", ":", 0 };
2272 static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^";
2273 static char *sh_cmds_sh[] = { "cd", "eval", "exec", "exit", "login",
2274 "logout", "set", "umask", "wait", "while", "for",
2275 "case", "if", ":", ".", "break", "continue",
2276 "export", "read", "readonly", "shift", "times",
2277 "trap", "switch", "test",
2278 #ifdef BATCH_MODE_ONLY_SHELL
2279 "echo",
2280 #endif
2281 0 };
2282 char* sh_chars;
2283 char** sh_cmds;
2284 #elif defined(__riscos__)
2285 static char sh_chars[] = "";
2286 static char *sh_cmds[] = { 0 };
2287 #else /* must be UNIX-ish */
2288 static char sh_chars[] = "#;\"*?[]&|<>(){}$`^~!";
2289 static char *sh_cmds[] = { ".", ":", "break", "case", "cd", "continue",
2290 "eval", "exec", "exit", "export", "for", "if",
2291 "login", "logout", "read", "readonly", "set",
2292 "shift", "switch", "test", "times", "trap",
2293 "umask", "wait", "while", 0 };
2294 #endif
2295 register int i;
2296 register char *p;
2297 register char *ap;
2298 char *end;
2299 int instring, word_has_equals, seen_nonequals, last_argument_was_empty;
2300 char **new_argv = 0;
2301 char *argstr = 0;
2302 #ifdef WINDOWS32
2303 int slow_flag = 0;
2305 if (!unixy_shell) {
2306 sh_cmds = sh_cmds_dos;
2307 sh_chars = sh_chars_dos;
2308 } else {
2309 sh_cmds = sh_cmds_sh;
2310 sh_chars = sh_chars_sh;
2312 #endif /* WINDOWS32 */
2314 if (restp != NULL)
2315 *restp = NULL;
2317 /* Make sure not to bother processing an empty line. */
2318 while (isblank ((unsigned char)*line))
2319 ++line;
2320 if (*line == '\0')
2321 return 0;
2323 /* See if it is safe to parse commands internally. */
2324 if (shell == 0)
2325 shell = default_shell;
2326 #ifdef WINDOWS32
2327 else if (strcmp (shell, default_shell))
2329 char *s1 = _fullpath(NULL, shell, 0);
2330 char *s2 = _fullpath(NULL, default_shell, 0);
2332 slow_flag = strcmp((s1 ? s1 : ""), (s2 ? s2 : ""));
2334 if (s1)
2335 free (s1);
2336 if (s2)
2337 free (s2);
2339 if (slow_flag)
2340 goto slow;
2341 #else /* not WINDOWS32 */
2342 #if defined (__MSDOS__) || defined (__EMX__)
2343 else if (stricmp (shell, default_shell))
2345 extern int _is_unixy_shell (const char *_path);
2347 DB (DB_BASIC, (_("$SHELL changed (was `%s', now `%s')\n"),
2348 default_shell, shell));
2349 unixy_shell = _is_unixy_shell (shell);
2350 /* we must allocate a copy of shell: construct_command_argv() will free
2351 * shell after this function returns. */
2352 default_shell = xstrdup (shell);
2354 if (unixy_shell)
2356 sh_chars = sh_chars_sh;
2357 sh_cmds = sh_cmds_sh;
2359 else
2361 sh_chars = sh_chars_dos;
2362 sh_cmds = sh_cmds_dos;
2363 # ifdef __EMX__
2364 if (_osmode == OS2_MODE)
2366 sh_chars = sh_chars_os2;
2367 sh_cmds = sh_cmds_os2;
2369 # endif
2371 #else /* !__MSDOS__ */
2372 else if (strcmp (shell, default_shell))
2373 goto slow;
2374 #endif /* !__MSDOS__ && !__EMX__ */
2375 #endif /* not WINDOWS32 */
2377 if (ifs != 0)
2378 for (ap = ifs; *ap != '\0'; ++ap)
2379 if (*ap != ' ' && *ap != '\t' && *ap != '\n')
2380 goto slow;
2382 i = strlen (line) + 1;
2384 /* More than 1 arg per character is impossible. */
2385 new_argv = (char **) xmalloc (i * sizeof (char *));
2387 /* All the args can fit in a buffer as big as LINE is. */
2388 ap = new_argv[0] = argstr = (char *) xmalloc (i);
2389 end = ap + i;
2391 /* I is how many complete arguments have been found. */
2392 i = 0;
2393 instring = word_has_equals = seen_nonequals = last_argument_was_empty = 0;
2394 for (p = line; *p != '\0'; ++p)
2396 assert (ap <= end);
2398 if (instring)
2400 /* Inside a string, just copy any char except a closing quote
2401 or a backslash-newline combination. */
2402 if (*p == instring)
2404 instring = 0;
2405 if (ap == new_argv[0] || *(ap-1) == '\0')
2406 last_argument_was_empty = 1;
2408 else if (*p == '\\' && p[1] == '\n')
2410 /* Backslash-newline is handled differently depending on what
2411 kind of string we're in: inside single-quoted strings you
2412 keep them; in double-quoted strings they disappear.
2413 For DOS/Windows/OS2, if we don't have a POSIX shell,
2414 we keep the pre-POSIX behavior of removing the
2415 backslash-newline. */
2416 if (instring == '"'
2417 #if defined (__MSDOS__) || defined (__EMX__) || defined (WINDOWS32)
2418 || !unixy_shell
2419 #endif
2421 ++p;
2422 else
2424 *(ap++) = *(p++);
2425 *(ap++) = *p;
2427 /* If there's a TAB here, skip it. */
2428 if (p[1] == '\t')
2429 ++p;
2431 else if (*p == '\n' && restp != NULL)
2433 /* End of the command line. */
2434 *restp = p;
2435 goto end_of_line;
2437 /* Backslash, $, and ` are special inside double quotes.
2438 If we see any of those, punt.
2439 But on MSDOS, if we use COMMAND.COM, double and single
2440 quotes have the same effect. */
2441 else if (instring == '"' && strchr ("\\$`", *p) != 0 && unixy_shell)
2442 goto slow;
2443 else
2444 *ap++ = *p;
2446 else if (strchr (sh_chars, *p) != 0)
2447 /* Not inside a string, but it's a special char. */
2448 goto slow;
2449 #ifdef __MSDOS__
2450 else if (*p == '.' && p[1] == '.' && p[2] == '.' && p[3] != '.')
2451 /* `...' is a wildcard in DJGPP. */
2452 goto slow;
2453 #endif
2454 else
2455 /* Not a special char. */
2456 switch (*p)
2458 case '=':
2459 /* Equals is a special character in leading words before the
2460 first word with no equals sign in it. This is not the case
2461 with sh -k, but we never get here when using nonstandard
2462 shell flags. */
2463 if (! seen_nonequals && unixy_shell)
2464 goto slow;
2465 word_has_equals = 1;
2466 *ap++ = '=';
2467 break;
2469 case '\\':
2470 /* Backslash-newline has special case handling, ref POSIX.
2471 We're in the fastpath, so emulate what the shell would do. */
2472 if (p[1] == '\n')
2474 /* Throw out the backslash and newline. */
2475 ++p;
2477 /* If there is a tab after a backslash-newline, remove it. */
2478 if (p[1] == '\t')
2479 ++p;
2481 /* If there's nothing in this argument yet, skip any
2482 whitespace before the start of the next word. */
2483 if (ap == new_argv[i])
2484 p = next_token (p + 1) - 1;
2486 else if (p[1] != '\0')
2488 #ifdef HAVE_DOS_PATHS
2489 /* Only remove backslashes before characters special to Unixy
2490 shells. All other backslashes are copied verbatim, since
2491 they are probably DOS-style directory separators. This
2492 still leaves a small window for problems, but at least it
2493 should work for the vast majority of naive users. */
2495 #ifdef __MSDOS__
2496 /* A dot is only special as part of the "..."
2497 wildcard. */
2498 if (strneq (p + 1, ".\\.\\.", 5))
2500 *ap++ = '.';
2501 *ap++ = '.';
2502 p += 4;
2504 else
2505 #endif
2506 if (p[1] != '\\' && p[1] != '\''
2507 && !isspace ((unsigned char)p[1])
2508 && strchr (sh_chars_sh, p[1]) == 0)
2509 /* back up one notch, to copy the backslash */
2510 --p;
2511 #endif /* HAVE_DOS_PATHS */
2513 /* Copy and skip the following char. */
2514 *ap++ = *++p;
2516 break;
2518 case '\'':
2519 case '"':
2520 instring = *p;
2521 break;
2523 case '\n':
2524 if (restp != NULL)
2526 /* End of the command line. */
2527 *restp = p;
2528 goto end_of_line;
2530 else
2531 /* Newlines are not special. */
2532 *ap++ = '\n';
2533 break;
2535 case ' ':
2536 case '\t':
2537 /* We have the end of an argument.
2538 Terminate the text of the argument. */
2539 *ap++ = '\0';
2540 new_argv[++i] = ap;
2541 last_argument_was_empty = 0;
2543 /* Update SEEN_NONEQUALS, which tells us if every word
2544 heretofore has contained an `='. */
2545 seen_nonequals |= ! word_has_equals;
2546 if (word_has_equals && ! seen_nonequals)
2547 /* An `=' in a word before the first
2548 word without one is magical. */
2549 goto slow;
2550 word_has_equals = 0; /* Prepare for the next word. */
2552 /* If this argument is the command name,
2553 see if it is a built-in shell command.
2554 If so, have the shell handle it. */
2555 if (i == 1)
2557 register int j;
2558 for (j = 0; sh_cmds[j] != 0; ++j)
2560 if (streq (sh_cmds[j], new_argv[0]))
2561 goto slow;
2562 # ifdef __EMX__
2563 /* Non-Unix shells are case insensitive. */
2564 if (!unixy_shell
2565 && strcasecmp (sh_cmds[j], new_argv[0]) == 0)
2566 goto slow;
2567 # endif
2571 /* Ignore multiple whitespace chars. */
2572 p = next_token (p) - 1;
2573 break;
2575 default:
2576 *ap++ = *p;
2577 break;
2580 end_of_line:
2582 if (instring)
2583 /* Let the shell deal with an unterminated quote. */
2584 goto slow;
2586 /* Terminate the last argument and the argument list. */
2588 *ap = '\0';
2589 if (new_argv[i][0] != '\0' || last_argument_was_empty)
2590 ++i;
2591 new_argv[i] = 0;
2593 if (i == 1)
2595 register int j;
2596 for (j = 0; sh_cmds[j] != 0; ++j)
2597 if (streq (sh_cmds[j], new_argv[0]))
2598 goto slow;
2601 if (new_argv[0] == 0)
2603 /* Line was empty. */
2604 free (argstr);
2605 free ((char *)new_argv);
2606 return 0;
2609 return new_argv;
2611 slow:;
2612 /* We must use the shell. */
2614 if (new_argv != 0)
2616 /* Free the old argument list we were working on. */
2617 free (argstr);
2618 free ((char *)new_argv);
2621 #ifdef __MSDOS__
2622 execute_by_shell = 1; /* actually, call `system' if shell isn't unixy */
2623 #endif
2625 #ifdef _AMIGA
2627 char *ptr;
2628 char *buffer;
2629 char *dptr;
2631 buffer = (char *)xmalloc (strlen (line)+1);
2633 ptr = line;
2634 for (dptr=buffer; *ptr; )
2636 if (*ptr == '\\' && ptr[1] == '\n')
2637 ptr += 2;
2638 else if (*ptr == '@') /* Kludge: multiline commands */
2640 ptr += 2;
2641 *dptr++ = '\n';
2643 else
2644 *dptr++ = *ptr++;
2646 *dptr = 0;
2648 new_argv = (char **) xmalloc (2 * sizeof (char *));
2649 new_argv[0] = buffer;
2650 new_argv[1] = 0;
2652 #else /* Not Amiga */
2653 #ifdef WINDOWS32
2655 * Not eating this whitespace caused things like
2657 * sh -c "\n"
2659 * which gave the shell fits. I think we have to eat
2660 * whitespace here, but this code should be considered
2661 * suspicious if things start failing....
2664 /* Make sure not to bother processing an empty line. */
2665 while (isspace ((unsigned char)*line))
2666 ++line;
2667 if (*line == '\0')
2668 return 0;
2669 #endif /* WINDOWS32 */
2671 /* SHELL may be a multi-word command. Construct a command line
2672 "SHELL -c LINE", with all special chars in LINE escaped.
2673 Then recurse, expanding this command line to get the final
2674 argument list. */
2676 unsigned int shell_len = strlen (shell);
2677 #ifndef VMS
2678 static char minus_c[] = " -c ";
2679 #else
2680 static char minus_c[] = "";
2681 #endif
2682 unsigned int line_len = strlen (line);
2684 char *new_line = (char *) alloca (shell_len + (sizeof (minus_c) - 1)
2685 + (line_len * 2) + 1);
2686 char *command_ptr = NULL; /* used for batch_mode_shell mode */
2688 # ifdef __EMX__ /* is this necessary? */
2689 if (!unixy_shell)
2690 minus_c[1] = '/'; /* " /c " */
2691 # endif
2693 ap = new_line;
2694 bcopy (shell, ap, shell_len);
2695 ap += shell_len;
2696 bcopy (minus_c, ap, sizeof (minus_c) - 1);
2697 ap += sizeof (minus_c) - 1;
2698 command_ptr = ap;
2699 for (p = line; *p != '\0'; ++p)
2701 if (restp != NULL && *p == '\n')
2703 *restp = p;
2704 break;
2706 else if (*p == '\\' && p[1] == '\n')
2708 /* POSIX says we keep the backslash-newline, but throw out
2709 the next char if it's a TAB. If we don't have a POSIX
2710 shell on DOS/Windows/OS2, mimic the pre-POSIX behavior
2711 and remove the backslash/newline. */
2712 #if defined (__MSDOS__) || defined (__EMX__) || defined (WINDOWS32)
2713 # define PRESERVE_BSNL unixy_shell
2714 #else
2715 # define PRESERVE_BSNL 1
2716 #endif
2717 if (PRESERVE_BSNL)
2719 *(ap++) = '\\';
2720 *(ap++) = '\\';
2721 *(ap++) = '\n';
2724 ++p;
2725 if (p[1] == '\t')
2726 ++p;
2728 continue;
2731 /* DOS shells don't know about backslash-escaping. */
2732 if (unixy_shell && !batch_mode_shell &&
2733 (*p == '\\' || *p == '\'' || *p == '"'
2734 || isspace ((unsigned char)*p)
2735 || strchr (sh_chars, *p) != 0))
2736 *ap++ = '\\';
2737 #ifdef __MSDOS__
2738 else if (unixy_shell && strneq (p, "...", 3))
2740 /* The case of `...' wildcard again. */
2741 strcpy (ap, "\\.\\.\\");
2742 ap += 5;
2743 p += 2;
2745 #endif
2746 *ap++ = *p;
2748 if (ap == new_line + shell_len + sizeof (minus_c) - 1)
2749 /* Line was empty. */
2750 return 0;
2751 *ap = '\0';
2753 #ifdef WINDOWS32
2754 /* Some shells do not work well when invoked as 'sh -c xxx' to run a
2755 command line (e.g. Cygnus GNUWIN32 sh.exe on WIN32 systems). In these
2756 cases, run commands via a script file. */
2757 if ((no_default_sh_exe || batch_mode_shell) && batch_filename_ptr) {
2758 FILE* batch = NULL;
2759 int id = GetCurrentProcessId();
2760 PATH_VAR(fbuf);
2762 /* create a file name */
2763 sprintf(fbuf, "make%d", id);
2764 *batch_filename_ptr = create_batch_filename (fbuf, unixy_shell);
2766 DB (DB_JOBS, (_("Creating temporary batch file %s\n"),
2767 *batch_filename_ptr));
2769 /* create batch file to execute command */
2770 batch = fopen (*batch_filename_ptr, "w");
2771 if (!unixy_shell)
2772 fputs ("@echo off\n", batch);
2773 fputs (command_ptr, batch);
2774 fputc ('\n', batch);
2775 fclose (batch);
2777 /* create argv */
2778 new_argv = (char **) xmalloc(3 * sizeof (char *));
2779 if (unixy_shell) {
2780 new_argv[0] = xstrdup (shell);
2781 new_argv[1] = *batch_filename_ptr; /* only argv[0] gets freed later */
2782 } else {
2783 new_argv[0] = xstrdup (*batch_filename_ptr);
2784 new_argv[1] = NULL;
2786 new_argv[2] = NULL;
2787 } else
2788 #endif /* WINDOWS32 */
2789 if (unixy_shell)
2790 new_argv = construct_command_argv_internal (new_line, (char **) NULL,
2791 (char *) 0, (char *) 0,
2792 (char **) 0);
2793 #ifdef __EMX__
2794 else if (!unixy_shell)
2796 /* new_line is local, must not be freed therefore
2797 We use line here instead of new_line because we run the shell
2798 manually. */
2799 size_t line_len = strlen (line);
2800 char *p = new_line;
2801 char *q = new_line;
2802 memcpy (new_line, line, line_len + 1);
2803 /* replace all backslash-newline combination and also following tabs */
2804 while (*q != '\0')
2806 if (q[0] == '\\' && q[1] == '\n')
2808 q += 2; /* remove '\\' and '\n' */
2809 if (q[0] == '\t')
2810 q++; /* remove 1st tab in the next line */
2812 else
2813 *p++ = *q++;
2815 *p = '\0';
2817 # ifndef NO_CMD_DEFAULT
2818 if (strnicmp (new_line, "echo", 4) == 0
2819 && (new_line[4] == ' ' || new_line[4] == '\t'))
2821 /* the builtin echo command: handle it separately */
2822 size_t echo_len = line_len - 5;
2823 char *echo_line = new_line + 5;
2825 /* special case: echo 'x="y"'
2826 cmd works this way: a string is printed as is, i.e., no quotes
2827 are removed. But autoconf uses a command like echo 'x="y"' to
2828 determine whether make works. autoconf expects the output x="y"
2829 so we will do exactly that.
2830 Note: if we do not allow cmd to be the default shell
2831 we do not need this kind of voodoo */
2832 if (echo_line[0] == '\''
2833 && echo_line[echo_len - 1] == '\''
2834 && strncmp (echo_line + 1, "ac_maketemp=",
2835 strlen ("ac_maketemp=")) == 0)
2837 /* remove the enclosing quotes */
2838 memmove (echo_line, echo_line + 1, echo_len - 2);
2839 echo_line[echo_len - 2] = '\0';
2842 # endif
2845 /* Let the shell decide what to do. Put the command line into the
2846 2nd command line argument and hope for the best ;-) */
2847 size_t sh_len = strlen (shell);
2849 /* exactly 3 arguments + NULL */
2850 new_argv = (char **) xmalloc (4 * sizeof (char *));
2851 /* Exactly strlen(shell) + strlen("/c") + strlen(line) + 3 times
2852 the trailing '\0' */
2853 new_argv[0] = (char *) malloc (sh_len + line_len + 5);
2854 memcpy (new_argv[0], shell, sh_len + 1);
2855 new_argv[1] = new_argv[0] + sh_len + 1;
2856 memcpy (new_argv[1], "/c", 3);
2857 new_argv[2] = new_argv[1] + 3;
2858 memcpy (new_argv[2], new_line, line_len + 1);
2859 new_argv[3] = NULL;
2862 #elif defined(__MSDOS__)
2863 else
2865 /* With MSDOS shells, we must construct the command line here
2866 instead of recursively calling ourselves, because we
2867 cannot backslash-escape the special characters (see above). */
2868 new_argv = (char **) xmalloc (sizeof (char *));
2869 line_len = strlen (new_line) - shell_len - sizeof (minus_c) + 1;
2870 new_argv[0] = xmalloc (line_len + 1);
2871 strncpy (new_argv[0],
2872 new_line + shell_len + sizeof (minus_c) - 1, line_len);
2873 new_argv[0][line_len] = '\0';
2875 #else
2876 else
2877 fatal (NILF, _("%s (line %d) Bad shell context (!unixy && !batch_mode_shell)\n"),
2878 __FILE__, __LINE__);
2879 #endif
2881 #endif /* ! AMIGA */
2883 return new_argv;
2885 #endif /* !VMS */
2887 /* Figure out the argument list necessary to run LINE as a command. Try to
2888 avoid using a shell. This routine handles only ' quoting, and " quoting
2889 when no backslash, $ or ` characters are seen in the quotes. Starting
2890 quotes may be escaped with a backslash. If any of the characters in
2891 sh_chars[] is seen, or any of the builtin commands listed in sh_cmds[]
2892 is the first word of a line, the shell is used.
2894 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
2895 If *RESTP is NULL, newlines will be ignored.
2897 FILE is the target whose commands these are. It is used for
2898 variable expansion for $(SHELL) and $(IFS). */
2900 char **
2901 construct_command_argv (char *line, char **restp, struct file *file,
2902 char **batch_filename_ptr)
2904 char *shell, *ifs;
2905 char **argv;
2907 #ifdef VMS
2908 char *cptr;
2909 int argc;
2911 argc = 0;
2912 cptr = line;
2913 for (;;)
2915 while ((*cptr != 0)
2916 && (isspace ((unsigned char)*cptr)))
2917 cptr++;
2918 if (*cptr == 0)
2919 break;
2920 while ((*cptr != 0)
2921 && (!isspace((unsigned char)*cptr)))
2922 cptr++;
2923 argc++;
2926 argv = (char **)malloc (argc * sizeof (char *));
2927 if (argv == 0)
2928 abort ();
2930 cptr = line;
2931 argc = 0;
2932 for (;;)
2934 while ((*cptr != 0)
2935 && (isspace ((unsigned char)*cptr)))
2936 cptr++;
2937 if (*cptr == 0)
2938 break;
2939 DB (DB_JOBS, ("argv[%d] = [%s]\n", argc, cptr));
2940 argv[argc++] = cptr;
2941 while ((*cptr != 0)
2942 && (!isspace((unsigned char)*cptr)))
2943 cptr++;
2944 if (*cptr != 0)
2945 *cptr++ = 0;
2947 #else
2949 /* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
2950 int save = warn_undefined_variables_flag;
2951 warn_undefined_variables_flag = 0;
2953 shell = allocated_variable_expand_for_file ("$(SHELL)", file);
2954 #ifdef WINDOWS32
2956 * Convert to forward slashes so that construct_command_argv_internal()
2957 * is not confused.
2959 if (shell) {
2960 char *p = w32ify (shell, 0);
2961 strcpy (shell, p);
2963 #endif
2964 #ifdef __EMX__
2966 static const char *unixroot = NULL;
2967 static const char *last_shell = "";
2968 static int init = 0;
2969 if (init == 0)
2971 unixroot = getenv ("UNIXROOT");
2972 /* unixroot must be NULL or not empty */
2973 if (unixroot && unixroot[0] == '\0') unixroot = NULL;
2974 init = 1;
2977 /* if we have an unixroot drive and if shell is not default_shell
2978 (which means it's either cmd.exe or the test has already been
2979 performed) and if shell is an absolute path without drive letter,
2980 try whether it exists e.g.: if "/bin/sh" does not exist use
2981 "$UNIXROOT/bin/sh" instead. */
2982 if (unixroot && shell && strcmp (shell, last_shell) != 0
2983 && (shell[0] == '/' || shell[0] == '\\'))
2985 /* trying a new shell, check whether it exists */
2986 size_t size = strlen (shell);
2987 char *buf = xmalloc (size + 7);
2988 memcpy (buf, shell, size);
2989 memcpy (buf + size, ".exe", 5); /* including the trailing '\0' */
2990 if (access (shell, F_OK) != 0 && access (buf, F_OK) != 0)
2992 /* try the same for the unixroot drive */
2993 memmove (buf + 2, buf, size + 5);
2994 buf[0] = unixroot[0];
2995 buf[1] = unixroot[1];
2996 if (access (buf, F_OK) == 0)
2997 /* we have found a shell! */
2998 /* free(shell); */
2999 shell = buf;
3000 else
3001 free (buf);
3003 else
3004 free (buf);
3007 #endif /* __EMX__ */
3009 ifs = allocated_variable_expand_for_file ("$(IFS)", file);
3011 warn_undefined_variables_flag = save;
3014 argv = construct_command_argv_internal (line, restp, shell, ifs, batch_filename_ptr);
3016 free (shell);
3017 free (ifs);
3018 #endif /* !VMS */
3019 return argv;
3022 #if !defined(HAVE_DUP2) && !defined(_AMIGA)
3024 dup2 (int old, int new)
3026 int fd;
3028 (void) close (new);
3029 fd = dup (old);
3030 if (fd != new)
3032 (void) close (fd);
3033 errno = EMFILE;
3034 return -1;
3037 return fd;
3039 #endif /* !HAPE_DUP2 && !_AMIGA */
3041 /* On VMS systems, include special VMS functions. */
3043 #ifdef VMS
3044 #include "vmsjobs.c"
3045 #endif