1 /* Job execution and handling for GNU Make.
2 Copyright (C) 1988-2013 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3 of the License, or (at your option) any later
10 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along with
15 this program. If not, see <http://www.gnu.org/licenses/>. */
30 #if defined (HAVE_LINUX_BINFMTS_H) && defined (HAVE_SYS_USER_H)
32 #include <linux/binfmts.h>
34 #define PAGE_SIZE getpagesize()
38 /* Default shell to use. */
42 char *default_shell = "sh.exe";
43 int no_default_sh_exe = 1;
44 int batch_mode_shell = 1;
47 #elif defined (_AMIGA)
49 char default_shell[] = "";
50 extern int MyExecute (char **);
51 int batch_mode_shell = 0;
53 #elif defined (__MSDOS__)
55 /* The default shell is a pointer so we can change it if Makefile
56 says so. It is without an explicit path so we get a chance
57 to search the $PATH for it (since MSDOS doesn't have standard
58 directories we could trust). */
59 char *default_shell = "command.com";
60 int batch_mode_shell = 0;
62 #elif defined (__EMX__)
64 char *default_shell = "/bin/sh";
65 int batch_mode_shell = 0;
70 char default_shell[] = "";
71 int batch_mode_shell = 0;
73 #elif defined (__riscos__)
75 char default_shell[] = "";
76 int batch_mode_shell = 0;
80 char default_shell[] = "/bin/sh";
81 int batch_mode_shell = 0;
87 static int execute_by_shell;
88 static int dos_pid = 123;
90 int dos_command_running;
91 #endif /* __MSDOS__ */
94 # include <proto/dos.h>
95 static int amiga_pid = 123;
96 static int amiga_status;
97 static char amiga_bname[32];
98 static int amiga_batch_file;
103 # include <processes.h>
105 # include <starlet.h>
106 # include <lib$routines.h>
107 static void vmsWaitForChildren (int *);
111 # include <windows.h>
113 # include <process.h>
114 # include "sub_proc.h"
116 # include "pathstuff.h"
117 # define WAIT_NOHANG 1
118 #endif /* WINDOWS32 */
121 # include <process.h>
124 #if defined (HAVE_SYS_WAIT_H) || defined (HAVE_UNION_WAIT)
125 # include <sys/wait.h>
129 # define WAIT_NOHANG(status) waitpid (-1, (status), WNOHANG)
130 #else /* Don't have waitpid. */
135 # define WAIT_NOHANG(status) wait3 ((status), WNOHANG, (struct rusage *) 0)
136 # endif /* Have wait3. */
137 #endif /* Have waitpid. */
139 #if !defined (wait) && !defined (POSIX)
143 #ifndef HAVE_UNION_WAIT
148 # define WTERMSIG(x) ((x) & 0x7f)
151 # define WCOREDUMP(x) ((x) & 0x80)
154 # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
157 # define WIFSIGNALED(x) (WTERMSIG (x) != 0)
160 # define WIFEXITED(x) (WTERMSIG (x) == 0)
163 #else /* Have 'union wait'. */
165 # define WAIT_T union wait
167 # define WTERMSIG(x) ((x).w_termsig)
170 # define WCOREDUMP(x) ((x).w_coredump)
173 # define WEXITSTATUS(x) ((x).w_retcode)
176 # define WIFSIGNALED(x) (WTERMSIG(x) != 0)
179 # define WIFEXITED(x) (WTERMSIG(x) == 0)
182 #endif /* Don't have 'union wait'. */
184 #if !defined(HAVE_UNISTD_H) && !defined(WINDOWS32)
196 /* Different systems have different requirements for pid_t.
197 Plus we have to support gettext string translation... Argh. */
201 static char pidstring[100];
202 #if defined(WINDOWS32) && (__GNUC__ > 3 || _MSC_VER > 1300)
203 /* %Id is only needed for 64-builds, which were not supported by
204 older versions of Windows compilers. */
205 sprintf (pidstring, "%Id", pid);
207 sprintf (pidstring, "%lu", (unsigned long) pid);
212 int getloadavg (double loadavg[], int nelem);
213 int start_remote_job (char **argv, char **envp, int stdin_fd, int *is_remote,
214 int *id_ptr, int *used_stdin);
215 int start_remote_job_p (int);
216 int remote_status (int *exit_code_ptr, int *signal_ptr, int *coredump_ptr,
219 RETSIGTYPE child_handler (int);
220 static void free_child (struct child *);
221 static void start_job_command (struct child *child);
222 static int load_too_high (void);
223 static int job_next_command (struct child *);
224 static int start_waiting_job (struct child *);
226 /* Chain of all live (or recently deceased) children. */
228 struct child *children = 0;
230 /* Number of children currently running. */
232 unsigned int job_slots_used = 0;
234 /* Nonzero if the 'good' standard input is in use. */
236 static int good_stdin_used = 0;
238 /* Chain of children waiting to run until the load average goes down. */
240 static struct child *waiting_jobs = 0;
242 /* Non-zero if we use a *real* shell (always so on Unix). */
246 /* Number of jobs started in the current second. */
248 unsigned long job_counter = 0;
250 /* Number of jobserver tokens this instance is currently using. */
252 unsigned int jobserver_tokens = 0;
257 * The macro which references this function is defined in makeint.h.
260 w32_kill (pid_t pid, int sig)
262 return ((process_kill ((HANDLE)pid, sig) == TRUE) ? 0 : -1);
265 /* This function creates a temporary file name with an extension specified
267 * Return an xmalloc'ed string of a newly created temp file and its
268 * file descriptor, or die. */
270 create_batch_file (char const *base, int unixy, int *fd)
272 const char *const ext = unixy ? "sh" : "bat";
273 const char *error_string = NULL;
274 char temp_path[MAXPATHLEN]; /* need to know its length */
275 unsigned path_size = GetTempPath (sizeof temp_path, temp_path);
277 /* The following variable is static so we won't try to reuse a name
278 that was generated a little while ago, because that file might
279 not be on disk yet, since we use FILE_ATTRIBUTE_TEMPORARY below,
280 which tells the OS it doesn't need to flush the cache to disk.
281 If the file is not yet on disk, we might think the name is
282 available, while it really isn't. This happens in parallel
283 builds, where Make doesn't wait for one job to finish before it
284 launches the next one. */
285 static unsigned uniq = 0;
286 static int second_loop = 0;
287 const unsigned sizemax = strlen (base) + strlen (ext) + 10;
291 path_size = GetCurrentDirectory (sizeof temp_path, temp_path);
296 if (uniq >= 0x10000 && !second_loop)
298 /* If we already had 64K batch files in this
299 process, make a second loop through the numbers,
300 looking for free slots, i.e. files that were
301 deleted in the meantime. */
305 while (path_size > 0 &&
306 path_size + sizemax < sizeof temp_path &&
307 !(uniq >= 0x10000 && second_loop))
309 unsigned size = sprintf (temp_path + path_size,
311 temp_path[path_size - 1] == '\\' ? "" : "\\",
313 HANDLE h = CreateFile (temp_path, /* file name */
314 GENERIC_READ | GENERIC_WRITE, /* desired access */
315 0, /* no share mode */
316 NULL, /* default security attributes */
317 CREATE_NEW, /* creation disposition */
318 FILE_ATTRIBUTE_NORMAL | /* flags and attributes */
319 FILE_ATTRIBUTE_TEMPORARY, /* we'll delete it */
320 NULL); /* no template file */
322 if (h == INVALID_HANDLE_VALUE)
324 const DWORD er = GetLastError ();
326 if (er == ERROR_FILE_EXISTS || er == ERROR_ALREADY_EXISTS)
329 if (uniq == 0x10000 && !second_loop)
336 /* the temporary path is not guaranteed to exist */
337 else if (path_is_dot == 0)
339 path_size = GetCurrentDirectory (sizeof temp_path, temp_path);
345 error_string = map_windows32_error_to_string (er);
351 const unsigned final_size = path_size + size + 1;
352 char *const path = xmalloc (final_size);
353 memcpy (path, temp_path, final_size);
354 *fd = _open_osfhandle ((intptr_t)h, 0);
359 for (p = path; (ch = *p) != 0; ++p)
363 return path; /* good return */
368 if (error_string == NULL)
369 error_string = _("Cannot create a temporary file\n");
370 fatal (NILF, error_string);
375 #endif /* WINDOWS32 */
378 /* returns whether path is assumed to be a unix like shell. */
380 _is_unixy_shell (const char *path)
382 /* list of non unix shells */
383 const char *known_os2shells[] = {
395 /* find the rightmost '/' or '\\' */
396 const char *name = strrchr (path, '/');
397 const char *p = strrchr (path, '\\');
400 if (name && p) /* take the max */
401 name = (name > p) ? name : p;
402 else if (p) /* name must be 0 */
404 else if (!name) /* name and p must be 0 */
407 if (*name == '/' || *name == '\\') name++;
410 while (known_os2shells[i] != NULL)
412 if (strcasecmp (name, known_os2shells[i]) == 0)
413 return 0; /* not a unix shell */
417 /* in doubt assume a unix like shell */
422 /* determines whether path looks to be a Bourne-like shell. */
424 is_bourne_compatible_shell (const char *path)
426 /* List of known POSIX (or POSIX-ish) shells. */
427 static const char *unix_shells[] = {
439 /* find the rightmost '/' or '\\' */
440 const char *name = strrchr (path, '/');
441 char *p = strrchr (path, '\\');
443 if (name && p) /* take the max */
444 name = (name > p) ? name : p;
445 else if (p) /* name must be 0 */
447 else if (!name) /* name and p must be 0 */
450 if (*name == '/' || *name == '\\')
453 /* this should be able to deal with extensions on Windows-like systems */
454 for (s = unix_shells; *s != NULL; ++s)
456 #if defined(WINDOWS32) || defined(__MSDOS__)
457 unsigned int len = strlen (*s);
458 if ((strlen (name) >= len && STOP_SET (name[len], MAP_DOT|MAP_NUL))
459 && strncasecmp (name, *s, len) == 0)
461 if (strcmp (name, *s) == 0)
463 return 1; /* a known unix-style shell */
466 /* if not on the list, assume it's not a Bourne-like shell */
471 /* Write an error message describing the exit status given in
472 EXIT_CODE, EXIT_SIG, and COREDUMP, for the target TARGET_NAME.
473 Append "(ignored)" if IGNORED is nonzero. */
476 child_error (struct child *child,
477 int exit_code, int exit_sig, int coredump, int ignored)
479 const char *pre = "*** ";
480 const char *post = "";
481 const char *dump = "";
482 const struct file *f = child->file;
483 const gmk_floc *flocp = &f->cmds->fileinfo;
486 if (ignored && silent_flag)
489 if (exit_sig && coredump)
490 dump = _(" (core dumped)");
495 post = _(" (ignored)");
502 char *a = alloca (strlen (flocp->filenm) + 1 + 11 + 1);
503 sprintf (a, "%s:%lu", flocp->filenm, flocp->lineno);
507 OUTPUT_SET (&child->output);
509 message (0, _("%s: recipe for target '%s' failed"), nm, f->name);
512 if ((exit_code & 1) != 0)
518 error (NILF, _("%s[%s] Error 0x%x%s"), pre, f->name, exit_code, post);
521 error (NILF, _("%s[%s] Error %d%s"), pre, f->name, exit_code, post);
524 const char *s = strsignal (exit_sig);
525 error (NILF, _("%s[%s] %s%s%s"), pre, f->name, s, dump, post);
533 /* Handle a dead child. This handler may or may not ever be installed.
535 If we're using the jobserver feature, we need it. First, installing it
536 ensures the read will interrupt on SIGCHLD. Second, we close the dup'd
537 read FD to ensure we don't enter another blocking read without reaping all
538 the dead children. In this case we don't need the dead_children count.
540 If we don't have either waitpid or wait3, then make is unreliable, but we
541 use the dead_children count to reap children as best we can. */
543 static unsigned int dead_children = 0;
546 child_handler (int sig UNUSED)
557 /* The signal handler must called only once! */
558 signal (SIGCHLD, SIG_DFL);
561 /* This causes problems if the SIGCHLD interrupts a printf().
562 DB (DB_JOBS, (_("Got a SIGCHLD; %u unreaped children.\n"), dead_children));
566 extern int shell_function_pid, shell_function_completed;
568 /* Reap all dead children, storing the returned status and the new command
569 state ('cs_finished') in the 'file' member of the 'struct child' for the
570 dead child, and removing the child from the chain. In addition, if BLOCK
571 nonzero, we block in this function until we've reaped at least one
572 complete child, waiting for it to die if necessary. If ERR is nonzero,
573 print an error message first. */
576 reap_children (int block, int err)
581 /* Initially, assume we have some. */
585 # define REAP_MORE reap_more
587 # define REAP_MORE dead_children
592 We have at least one child outstanding OR a shell function in progress,
594 We're blocking for a complete child OR there are more children to reap
596 we'll keep reaping children. */
598 while ((children != 0 || shell_function_pid != 0)
599 && (block || REAP_MORE))
603 int exit_code, exit_sig, coredump;
604 struct child *lastc, *c;
606 int any_remote, any_local;
611 static int printed = 0;
613 /* We might block for a while, so let the user know why.
614 Only print this message once no matter how many jobs are left. */
617 error (NILF, _("*** Waiting for unfinished jobs...."));
621 /* We have one less dead child to reap. As noted in
622 child_handler() above, this count is completely unimportant for
623 all modern, POSIX-y systems that support wait3() or waitpid().
624 The rest of this comment below applies only to early, broken
625 pre-POSIX systems. We keep the count only because... it's there...
627 The test and decrement are not atomic; if it is compiled into:
628 register = dead_children - 1;
629 dead_children = register;
630 a SIGCHLD could come between the two instructions.
631 child_handler increments dead_children.
632 The second instruction here would lose that increment. But the
633 only effect of dead_children being wrong is that we might wait
634 longer than necessary to reap a child, and lose some parallelism;
635 and we might print the "Waiting for unfinished jobs" message above
636 when not necessary. */
638 if (dead_children > 0)
642 any_local = shell_function_pid != 0;
643 for (c = children; c != 0; c = c->next)
645 any_remote |= c->remote;
646 any_local |= ! c->remote;
647 DB (DB_JOBS, (_("Live child %p (%s) PID %s %s\n"),
648 c, c->file->name, pid2str (c->pid),
649 c->remote ? _(" (remote)") : ""));
655 /* First, check for remote children. */
657 pid = remote_status (&exit_code, &exit_sig, &coredump, 0);
662 /* We got a remote child. */
666 /* A remote status command failed miserably. Punt. */
668 pfatal_with_name ("remote_status");
672 /* No remote children. Check for local children. */
673 #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
677 vmsWaitForChildren (&status);
682 pid = WAIT_NOHANG (&status);
685 EINTRLOOP(pid, wait (&status));
693 /* The wait*() failed miserably. Punt. */
694 pfatal_with_name ("wait");
698 /* We got a child exit; chop the status word up. */
699 exit_code = WEXITSTATUS (status);
700 exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0;
701 coredump = WCOREDUMP (status);
703 /* If we have started jobs in this second, remove one. */
709 /* No local children are dead. */
712 if (!block || !any_remote)
715 /* Now try a blocking wait for a remote child. */
716 pid = remote_status (&exit_code, &exit_sig, &coredump, 1);
718 goto remote_status_lose;
720 /* No remote children either. Finally give up. */
723 /* We got a remote child. */
726 #endif /* !__MSDOS__, !Amiga, !WINDOWS32. */
729 /* Life is very different on MSDOS. */
732 exit_code = WEXITSTATUS (status);
733 if (exit_code == 0xff)
735 exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0;
737 #endif /* __MSDOS__ */
741 status = amiga_status;
742 exit_code = amiga_status;
750 DWORD dwWaitStatus = 0;
755 /* Record the thread ID of the main process, so that we
756 could suspend it in the signal handler. */
759 hcTID = GetCurrentThread ();
760 hcPID = GetCurrentProcess ();
761 if (!DuplicateHandle (hcPID, hcTID, hcPID, &main_thread, 0,
762 FALSE, DUPLICATE_SAME_ACCESS))
764 DWORD e = GetLastError ();
766 "Determine main thread ID (Error %ld: %s)\n",
767 e, map_windows32_error_to_string (e));
770 DB (DB_VERBOSE, ("Main thread handle = %p\n", main_thread));
773 /* wait for anything to finish */
774 hPID = process_wait_for_any (block, &dwWaitStatus);
777 /* was an error found on this process? */
778 int werr = process_last_err (hPID);
781 exit_code = process_exit_code (hPID);
784 fprintf (stderr, "make (e=%d): %s", exit_code,
785 map_windows32_error_to_string (exit_code));
788 exit_sig = process_signal (hPID);
790 /* cleanup process */
791 process_cleanup (hPID);
795 else if (dwWaitStatus == WAIT_FAILED)
797 /* The WaitForMultipleObjects() failed miserably. Punt. */
798 pfatal_with_name ("WaitForMultipleObjects");
800 else if (dwWaitStatus == WAIT_TIMEOUT)
802 /* No child processes are finished. Give up waiting. */
809 #endif /* WINDOWS32 */
812 /* Check if this is the child of the 'shell' function. */
813 if (!remote && pid == shell_function_pid)
815 /* It is. Leave an indicator for the 'shell' function. */
816 if (exit_sig == 0 && exit_code == 127)
817 shell_function_completed = -1;
819 shell_function_completed = 1;
823 child_failed = exit_sig != 0 || exit_code != 0;
825 /* Search for a child matching the deceased one. */
827 for (c = children; c != 0; lastc = c, c = c->next)
828 if (c->pid == pid && c->remote == remote)
832 /* An unknown child died.
833 Ignore it; it was inherited from our invoker. */
836 DB (DB_JOBS, (child_failed
837 ? _("Reaping losing child %p PID %s %s\n")
838 : _("Reaping winning child %p PID %s %s\n"),
839 c, pid2str (c->pid), c->remote ? _(" (remote)") : ""));
841 if (c->sh_batch_file)
845 DB (DB_JOBS, (_("Cleaning up temp batch file %s\n"),
849 rm_status = remove (c->sh_batch_file);
851 DB (DB_JOBS, (_("Cleaning up temp batch file %s failed (%d)\n"),
852 c->sh_batch_file, errno));
854 /* all done with memory */
855 free (c->sh_batch_file);
856 c->sh_batch_file = NULL;
859 /* If this child had the good stdin, say it is now free. */
863 dontcare = c->dontcare;
865 if (child_failed && !c->noerror && !ignore_errors_flag)
867 /* The commands failed. Write an error message,
868 delete non-precious targets, and abort. */
869 static int delete_on_error = -1;
872 child_error (c, exit_code, exit_sig, coredump, 0);
874 c->file->update_status = us_failed;
875 if (delete_on_error == -1)
877 struct file *f = lookup_file (".DELETE_ON_ERROR");
878 delete_on_error = f != 0 && f->is_target;
880 if (exit_sig != 0 || delete_on_error)
881 delete_child_targets (c);
887 /* The commands failed, but we don't care. */
888 child_error (c, exit_code, exit_sig, coredump, 1);
892 /* If there are more commands to run, try to start them. */
893 if (job_next_command (c))
895 if (handling_fatal_signal)
897 /* Never start new commands while we are dying.
898 Since there are more commands that wanted to be run,
899 the target was not completely remade. So we treat
900 this as if a command had failed. */
901 c->file->update_status = us_failed;
905 #ifndef NO_OUTPUT_SYNC
906 /* If we're sync'ing per line, write the previous line's
907 output before starting the next one. */
908 if (output_sync == OUTPUT_SYNC_LINE)
909 output_dump (&c->output);
911 /* Check again whether to start remotely.
912 Whether or not we want to changes over time.
913 Also, start_remote_job may need state set up
914 by start_remote_job_p. */
915 c->remote = start_remote_job_p (0);
916 start_job_command (c);
917 /* Fatal signals are left blocked in case we were
918 about to put that child on the chain. But it is
919 already there, so it is safe for a fatal signal to
920 arrive now; it will clean up this child's targets. */
922 if (c->file->command_state == cs_running)
923 /* We successfully started the new command.
924 Loop to reap more children. */
928 if (c->file->update_status != us_success)
929 /* We failed to start the commands. */
930 delete_child_targets (c);
933 /* There are no more commands. We got through them all
934 without an unignored error. Now the target has been
935 successfully updated. */
936 c->file->update_status = us_success;
939 /* When we get here, all the commands for c->file are finished. */
941 #ifndef NO_OUTPUT_SYNC
942 /* Synchronize any remaining parallel output. */
943 output_dump (&c->output);
946 /* At this point c->file->update_status is success or failed. But
947 c->file->command_state is still cs_running if all the commands
948 ran; notice_finish_file looks for cs_running to tell it that
949 it's interesting to check the file's modtime again now. */
951 if (! handling_fatal_signal)
952 /* Notice if the target of the commands has been changed.
953 This also propagates its values for command_state and
954 update_status to its also_make files. */
955 notice_finished_file (c->file);
957 DB (DB_JOBS, (_("Removing child %p PID %s%s from chain.\n"),
958 c, pid2str (c->pid), c->remote ? _(" (remote)") : ""));
960 /* Block fatal signals while frobnicating the list, so that
961 children and job_slots_used are always consistent. Otherwise
962 a fatal signal arriving after the child is off the chain and
963 before job_slots_used is decremented would believe a child was
964 live and call reap_children again. */
967 /* There is now another slot open. */
968 if (job_slots_used > 0)
971 /* Remove the child from the chain and free it. */
975 lastc->next = c->next;
981 /* If the job failed, and the -k flag was not given, die,
982 unless we are already in the process of dying. */
983 if (!err && child_failed && !dontcare && !keep_going_flag &&
984 /* fatal_error_signal will die with the right signal. */
985 !handling_fatal_signal)
988 /* Only block for one child. */
995 /* Free the storage allocated for CHILD. */
998 free_child (struct child *child)
1000 output_close (&child->output);
1002 if (!jobserver_tokens)
1003 fatal (NILF, "INTERNAL: Freeing child %p (%s) but no tokens left!\n",
1004 child, child->file->name);
1006 /* If we're using the jobserver and this child is not the only outstanding
1007 job, put a token back into the pipe for it. */
1010 if (has_jobserver_semaphore () && jobserver_tokens > 1)
1012 if (! release_jobserver_semaphore ())
1014 DWORD err = GetLastError ();
1015 fatal (NILF, _("release jobserver semaphore: (Error %ld: %s)"),
1016 err, map_windows32_error_to_string (err));
1019 DB (DB_JOBS, (_("Released token for child %p (%s).\n"), child, child->file->name));
1022 if (job_fds[1] >= 0 && jobserver_tokens > 1)
1027 /* Write a job token back to the pipe. */
1029 EINTRLOOP (r, write (job_fds[1], &token, 1));
1031 pfatal_with_name (_("write jobserver"));
1033 DB (DB_JOBS, (_("Released token for child %p (%s).\n"),
1034 child, child->file->name));
1040 if (handling_fatal_signal) /* Don't bother free'ing if about to die. */
1043 if (child->command_lines != 0)
1045 register unsigned int i;
1046 for (i = 0; i < child->file->cmds->ncommand_lines; ++i)
1047 free (child->command_lines[i]);
1048 free (child->command_lines);
1051 if (child->environment != 0)
1053 register char **ep = child->environment;
1056 free (child->environment);
1063 extern sigset_t fatal_signal_set;
1070 (void) sigprocmask (SIG_BLOCK, &fatal_signal_set, (sigset_t *) 0);
1072 # ifdef HAVE_SIGSETMASK
1073 (void) sigblock (fatal_signal_mask);
1083 sigemptyset (&empty);
1084 sigprocmask (SIG_SETMASK, &empty, (sigset_t *) 0);
1088 #if defined(MAKE_JOBSERVER) && !defined(WINDOWS32)
1090 job_noop (int sig UNUSED)
1093 /* Set the child handler action flags to FLAGS. */
1095 set_child_handler_action_flags (int set_handler, int set_alarm)
1097 struct sigaction sa;
1100 /* The child handler must be turned off here. */
1101 signal (SIGCHLD, SIG_DFL);
1104 memset (&sa, '\0', sizeof sa);
1105 sa.sa_handler = child_handler;
1106 sa.sa_flags = set_handler ? 0 : SA_RESTART;
1108 sigaction (SIGCHLD, &sa, NULL);
1110 #if defined SIGCLD && SIGCLD != SIGCHLD
1111 sigaction (SIGCLD, &sa, NULL);
1116 /* If we're about to enter the read(), set an alarm to wake up in a
1117 second so we can check if the load has dropped and we can start more
1118 work. On the way out, turn off the alarm and set SIG_DFL. */
1119 alarm (set_handler ? 1 : 0);
1120 sa.sa_handler = set_handler ? job_noop : SIG_DFL;
1122 sigaction (SIGALRM, &sa, NULL);
1129 /* Start a job to run the commands specified in CHILD.
1130 CHILD is updated to reflect the commands and ID of the child process.
1132 NOTE: On return fatal signals are blocked! The caller is responsible
1133 for calling 'unblock_sigs', once the new child is safely on the chain so
1134 it can be cleaned up in the event of a fatal signal. */
1137 start_job_command (struct child *child)
1139 #if !defined(_AMIGA) && !defined(WINDOWS32)
1140 static int bad_stdin = -1;
1150 /* If we have a completely empty commandset, stop now. */
1151 if (!child->command_ptr)
1154 /* Combine the flags parsed for the line itself with
1155 the flags specified globally for this target. */
1156 flags = (child->file->command_flags
1157 | child->file->cmds->lines_flags[child->command_line - 1]);
1159 p = child->command_ptr;
1160 child->noerror = ((flags & COMMANDS_NOERROR) != 0);
1165 flags |= COMMANDS_SILENT;
1167 flags |= COMMANDS_RECURSE;
1170 else if (!isblank ((unsigned char)*p))
1175 /* Update the file's command flags with any new ones we found. We only
1176 keep the COMMANDS_RECURSE setting. Even this isn't 100% correct; we are
1177 now marking more commands recursive than should be in the case of
1178 multiline define/endef scripts where only one line is marked "+". In
1179 order to really fix this, we'll have to keep a lines_flags for every
1180 actual line, after expansion. */
1181 child->file->cmds->lines_flags[child->command_line - 1]
1182 |= flags & COMMANDS_RECURSE;
1184 /* POSIX requires that a recipe prefix after a backslash-newline should
1185 be ignored. Remove it now so the output is correct. */
1187 char prefix = child->file->cmds->recipe_prefix;
1193 if (p1[0] == '\n' && p1[1] == prefix)
1200 /* Figure out an argument list from this command line. */
1206 argv = construct_command_argv (p, &end, child->file,
1207 child->file->cmds->lines_flags[child->command_line - 1],
1208 &child->sh_batch_file);
1211 child->command_ptr = NULL;
1215 child->command_ptr = end;
1219 /* If -q was given, say that updating 'failed' if there was any text on the
1220 command line, or 'succeeded' otherwise. The exit status of 1 tells the
1221 user that -q is saying 'something to do'; the exit status for a random
1223 if (argv != 0 && question_flag && !(flags & COMMANDS_RECURSE))
1229 child->file->update_status = us_question;
1230 notice_finished_file (child->file);
1234 if (touch_flag && !(flags & COMMANDS_RECURSE))
1236 /* Go on to the next command. It might be the recursive one.
1237 We construct ARGV only to find the end of the command line. */
1252 execute_by_shell = 0; /* in case construct_command_argv sets it */
1254 /* This line has no commands. Go to the next. */
1255 if (job_next_command (child))
1256 start_job_command (child);
1259 /* No more commands. Make sure we're "running"; we might not be if
1260 (e.g.) all commands were skipped due to -n. */
1261 set_command_state (child->file, cs_running);
1262 child->file->update_status = us_success;
1263 notice_finished_file (child->file);
1270 /* Are we going to synchronize this command's output? Do so if either we're
1271 in SYNC_RECURSE mode or this command is not recursive. We'll also check
1272 output_sync separately below in case it changes due to error. */
1273 child->output.syncout = output_sync && (output_sync == OUTPUT_SYNC_RECURSE
1274 || !(flags & COMMANDS_RECURSE));
1276 OUTPUT_SET (&child->output);
1278 #ifndef NO_OUTPUT_SYNC
1279 if (! child->output.syncout)
1280 /* We don't want to sync this command: to avoid misordered
1281 output ensure any already-synced content is written. */
1282 output_dump (&child->output);
1285 /* Print the command if appropriate. */
1286 if (just_print_flag || trace_flag
1287 || (!(flags & COMMANDS_SILENT) && !silent_flag))
1288 message (0, "%s", p);
1290 /* Tell update_goal_chain that a command has been started on behalf of
1291 this target. It is important that this happens here and not in
1292 reap_children (where we used to do it), because reap_children might be
1293 reaping children from a different target. We want this increment to
1294 guaranteedly indicate that a command was started for the dependency
1295 chain (i.e., update_file recursion chain) we are processing. */
1299 /* Optimize an empty command. People use this for timestamp rules,
1300 so avoid forking a useless shell. Do this after we increment
1301 commands_started so make still treats this special case as if it
1302 performed some action (makes a difference as to what messages are
1305 #if !defined(VMS) && !defined(_AMIGA)
1307 #if defined __MSDOS__ || defined (__EMX__)
1308 unixy_shell /* the test is complicated and we already did it */
1310 (argv[0] && is_bourne_compatible_shell (argv[0]))
1312 && (argv[1] && argv[1][0] == '-'
1314 ((argv[1][1] == 'c' && argv[1][2] == '\0')
1316 (argv[1][1] == 'e' && argv[1][2] == 'c' && argv[1][3] == '\0')))
1317 && (argv[2] && argv[2][0] == ':' && argv[2][1] == '\0')
1324 #endif /* !VMS && !_AMIGA */
1326 /* If -n was given, recurse to get the next line in the sequence. */
1328 if (just_print_flag && !(flags & COMMANDS_RECURSE))
1337 /* We're sure we're going to invoke a command: set up the output. */
1340 /* Flush the output streams so they won't have things written twice. */
1346 #if !defined(WINDOWS32) && !defined(_AMIGA) && !defined(__MSDOS__)
1348 /* Set up a bad standard input that reads from a broken pipe. */
1350 if (bad_stdin == -1)
1352 /* Make a file descriptor that is the read end of a broken pipe.
1353 This will be used for some children's standard inputs. */
1357 /* Close the write side. */
1358 (void) close (pd[1]);
1359 /* Save the read side. */
1362 /* Set the descriptor to close on exec, so it does not litter any
1363 child's descriptor table. When it is dup2'd onto descriptor 0,
1364 that descriptor will not close on exec. */
1365 CLOSE_ON_EXEC (bad_stdin);
1369 #endif /* !WINDOWS32 && !_AMIGA && !__MSDOS__ */
1371 /* Decide whether to give this child the 'good' standard input
1372 (one that points to the terminal or whatever), or the 'bad' one
1373 that points to the read side of a broken pipe. */
1375 child->good_stdin = !good_stdin_used;
1376 if (child->good_stdin)
1377 good_stdin_used = 1;
1384 /* Set up the environment for the child. */
1385 if (child->environment == 0)
1386 child->environment = target_environment (child->file);
1389 #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
1392 /* start_waiting_job has set CHILD->remote if we can start a remote job. */
1395 int is_remote, id, used_stdin;
1396 if (start_remote_job (argv, child->environment,
1397 child->good_stdin ? 0 : bad_stdin,
1398 &is_remote, &id, &used_stdin))
1399 /* Don't give up; remote execution may fail for various reasons. If
1400 so, simply run the job locally. */
1404 if (child->good_stdin && !used_stdin)
1406 child->good_stdin = 0;
1407 good_stdin_used = 0;
1409 child->remote = is_remote;
1416 /* Fork the child process. */
1418 char **parent_environ;
1426 if (!child_execute_job (argv, child))
1429 perror_with_name ("fork", "");
1435 parent_environ = environ;
1438 /* If we aren't running a recursive command and we have a jobserver
1439 pipe, close it before exec'ing. */
1440 if (!(flags & COMMANDS_RECURSE) && job_fds[0] >= 0)
1442 CLOSE_ON_EXEC (job_fds[0]);
1443 CLOSE_ON_EXEC (job_fds[1]);
1446 CLOSE_ON_EXEC (job_rfd);
1448 /* Never use fork()/exec() here! Use spawn() instead in exec_command() */
1449 child->pid = child_execute_job (child->good_stdin ? FD_STDIN : bad_stdin,
1450 FD_STDOUT, FD_STDERR,
1451 argv, child->environment);
1456 perror_with_name ("spawn", "");
1460 /* undo CLOSE_ON_EXEC() after the child process has been started */
1461 if (!(flags & COMMANDS_RECURSE) && job_fds[0] >= 0)
1463 fcntl (job_fds[0], F_SETFD, 0);
1464 fcntl (job_fds[1], F_SETFD, 0);
1467 fcntl (job_rfd, F_SETFD, 0);
1469 #else /* !__EMX__ */
1471 child->pid = fork ();
1472 environ = parent_environ; /* Restore value child may have clobbered. */
1473 if (child->pid == 0)
1475 int outfd = FD_STDOUT;
1476 int errfd = FD_STDERR;
1478 /* We are the child side. */
1481 /* If we aren't running a recursive command and we have a jobserver
1482 pipe, close it before exec'ing. */
1483 if (!(flags & COMMANDS_RECURSE) && job_fds[0] >= 0)
1491 #ifdef SET_STACK_SIZE
1492 /* Reset limits, if necessary. */
1493 if (stack_limit.rlim_cur)
1494 setrlimit (RLIMIT_STACK, &stack_limit);
1497 /* Divert child output if output_sync in use. */
1498 if (child->output.syncout)
1500 if (child->output.out >= 0)
1501 outfd = child->output.out;
1502 if (child->output.err >= 0)
1503 errfd = child->output.err;
1506 child_execute_job (child->good_stdin ? FD_STDIN : bad_stdin,
1507 outfd, errfd, argv, child->environment);
1509 else if (child->pid < 0)
1513 perror_with_name ("fork", "");
1516 # endif /* !__EMX__ */
1520 #else /* __MSDOS__ or Amiga or WINDOWS32 */
1528 /* We call 'system' to do the job of the SHELL, since stock DOS
1529 shell is too dumb. Our 'system' knows how to handle long
1530 command lines even if pipes/redirection is needed; it will only
1531 call COMMAND.COM when its internal commands are used. */
1532 if (execute_by_shell)
1534 char *cmdline = argv[0];
1535 /* We don't have a way to pass environment to 'system',
1536 so we need to save and restore ours, sigh... */
1537 char **parent_environ = environ;
1539 environ = child->environment;
1541 /* If we have a *real* shell, tell 'system' to call
1542 it to do everything for us. */
1545 /* A *real* shell on MSDOS may not support long
1546 command lines the DJGPP way, so we must use 'system'. */
1547 cmdline = argv[2]; /* get past "shell -c" */
1550 dos_command_running = 1;
1551 proc_return = system (cmdline);
1552 environ = parent_environ;
1553 execute_by_shell = 0; /* for the next time */
1557 dos_command_running = 1;
1558 proc_return = spawnvpe (P_WAIT, argv[0], argv, child->environment);
1561 /* Need to unblock signals before turning off
1562 dos_command_running, so that child's signals
1563 will be treated as such (see fatal_error_signal). */
1565 dos_command_running = 0;
1567 /* If the child got a signal, dos_status has its
1568 high 8 bits set, so be careful not to alter them. */
1569 if (proc_return == -1)
1572 dos_status |= (proc_return & 0xff);
1574 child->pid = dos_pid++;
1576 #endif /* __MSDOS__ */
1578 amiga_status = MyExecute (argv);
1581 child->pid = amiga_pid++;
1582 if (amiga_batch_file)
1584 amiga_batch_file = 0;
1585 DeleteFile (amiga_bname); /* Ignore errors. */
1593 /* make UNC paths safe for CreateProcess -- backslash format */
1595 if (arg0 && arg0[0] == '/' && arg0[1] == '/')
1596 for ( ; arg0 && *arg0; arg0++)
1600 /* make sure CreateProcess() has Path it needs */
1601 sync_Path_environment ();
1603 #ifndef NO_OUTPUT_SYNC
1604 /* Divert child output if output_sync in use. Don't capture
1605 recursive make output unless we are synchronizing "make" mode. */
1606 if (child->output.syncout)
1607 hPID = process_easy (argv, child->environment,
1608 child->output.out, child->output.err);
1611 hPID = process_easy (argv, child->environment, -1, -1);
1613 if (hPID != INVALID_HANDLE_VALUE)
1614 child->pid = (pid_t) hPID;
1620 _("process_easy() failed to launch process (e=%ld)\n"),
1621 process_last_err (hPID));
1622 for (i = 0; argv[i]; i++)
1623 fprintf (stderr, "%s ", argv[i]);
1624 fprintf (stderr, _("\nCounted %d args in failed launch\n"), i);
1628 #endif /* WINDOWS32 */
1629 #endif /* __MSDOS__ or Amiga or WINDOWS32 */
1631 /* Bump the number of jobs started in this second. */
1634 /* We are the parent side. Set the state to
1635 say the commands are running and return. */
1637 set_command_state (child->file, cs_running);
1639 /* Free the storage used by the child's argument list. */
1649 child->file->update_status = us_failed;
1650 notice_finished_file (child->file);
1654 /* Try to start a child running.
1655 Returns nonzero if the child was started (and maybe finished), or zero if
1656 the load was too high and the child was put on the 'waiting_jobs' chain. */
1659 start_waiting_job (struct child *c)
1661 struct file *f = c->file;
1663 /* If we can start a job remotely, we always want to, and don't care about
1664 the local load average. We record that the job should be started
1665 remotely in C->remote for start_job_command to test. */
1667 c->remote = start_remote_job_p (1);
1669 /* If we are running at least one job already and the load average
1670 is too high, make this one wait. */
1672 && ((job_slots_used > 0 && load_too_high ())
1674 || (process_used_slots () >= MAXIMUM_WAIT_OBJECTS)
1678 /* Put this child on the chain of children waiting for the load average
1680 set_command_state (f, cs_running);
1681 c->next = waiting_jobs;
1686 /* Start the first command; reap_children will run later command lines. */
1687 start_job_command (c);
1689 switch (f->command_state)
1693 DB (DB_JOBS, (_("Putting child %p (%s) PID %s%s on the chain.\n"),
1694 c, c->file->name, pid2str (c->pid),
1695 c->remote ? _(" (remote)") : ""));
1697 /* One more job slot is in use. */
1702 case cs_not_started:
1703 /* All the command lines turned out to be empty. */
1704 f->update_status = us_success;
1708 notice_finished_file (f);
1713 assert (f->command_state == cs_finished);
1720 /* Create a 'struct child' for FILE and start its commands running. */
1723 new_job (struct file *file)
1725 struct commands *cmds = file->cmds;
1730 /* Let any previously decided-upon jobs that are waiting
1731 for the load to go down start before this new one. */
1732 start_waiting_jobs ();
1734 /* Reap any children that might have finished recently. */
1735 reap_children (0, 0);
1737 /* Chop the commands up into lines if they aren't already. */
1738 chop_commands (cmds);
1740 /* Start the command sequence, record it in a new
1741 'struct child', and add that to the chain. */
1743 c = xcalloc (sizeof (struct child));
1744 output_init (&c->output);
1747 c->sh_batch_file = NULL;
1749 /* Cache dontcare flag because file->dontcare can be changed once we
1750 return. Check dontcare inheritance mechanism for details. */
1751 c->dontcare = file->dontcare;
1753 /* Start saving output in case the expansion uses $(info ...) etc. */
1754 OUTPUT_SET (&c->output);
1756 /* Expand the command lines and store the results in LINES. */
1757 lines = xmalloc (cmds->ncommand_lines * sizeof (char *));
1758 for (i = 0; i < cmds->ncommand_lines; ++i)
1760 /* Collapse backslash-newline combinations that are inside variable
1761 or function references. These are left alone by the parser so
1762 that they will appear in the echoing of commands (where they look
1763 nice); and collapsed by construct_command_argv when it tokenizes.
1764 But letting them survive inside function invocations loses because
1765 we don't want the functions to see them as part of the text. */
1767 char *in, *out, *ref;
1769 /* IN points to where in the line we are scanning.
1770 OUT points to where in the line we are writing.
1771 When we collapse a backslash-newline combination,
1772 IN gets ahead of OUT. */
1774 in = out = cmds->command_lines[i];
1775 while ((ref = strchr (in, '$')) != 0)
1777 ++ref; /* Move past the $. */
1780 /* Copy the text between the end of the last chunk
1781 we processed (where IN points) and the new chunk
1782 we are about to process (where REF points). */
1783 memmove (out, in, ref - in);
1785 /* Move both pointers past the boring stuff. */
1789 if (*ref == '(' || *ref == '{')
1791 char openparen = *ref;
1792 char closeparen = openparen == '(' ? ')' : '}';
1797 *out++ = *in++; /* Copy OPENPAREN. */
1799 /* IN now points past the opening paren or brace.
1800 Count parens or braces until it is matched. */
1804 if (*in == closeparen && --count < 0)
1806 else if (*in == '\\' && in[1] == '\n')
1808 /* We have found a backslash-newline inside a
1809 variable or function reference. Eat it and
1810 any following whitespace. */
1813 for (p = in - 1; p > ref && *p == '\\'; --p)
1817 /* There were two or more backslashes, so this is
1818 not really a continuation line. We don't collapse
1819 the quoting backslashes here as is done in
1820 collapse_continuations, because the line will
1821 be collapsed again after expansion. */
1825 /* Skip the backslash, newline and
1826 any following whitespace. */
1827 in = next_token (in + 2);
1829 /* Discard any preceding whitespace that has
1830 already been written to the output. */
1832 && isblank ((unsigned char)out[-1]))
1835 /* Replace it all with a single space. */
1841 if (*in == openparen)
1850 /* There are no more references in this line to worry about.
1851 Copy the remaining uninteresting text to the output. */
1853 memmove (out, in, strlen (in) + 1);
1855 /* Finally, expand the line. */
1856 lines[i] = allocated_variable_expand_for_file (cmds->command_lines[i],
1860 c->command_lines = lines;
1862 /* Fetch the first command line to be run. */
1863 job_next_command (c);
1865 /* Wait for a job slot to be freed up. If we allow an infinite number
1866 don't bother; also job_slots will == 0 if we're using the jobserver. */
1869 while (job_slots_used == job_slots)
1870 reap_children (1, 0);
1872 #ifdef MAKE_JOBSERVER
1873 /* If we are controlling multiple jobs make sure we have a token before
1874 starting the child. */
1876 /* This can be inefficient. There's a decent chance that this job won't
1877 actually have to run any subprocesses: the command script may be empty
1878 or otherwise optimized away. It would be nice if we could defer
1879 obtaining a token until just before we need it, in start_job_command.
1880 To do that we'd need to keep track of whether we'd already obtained a
1881 token (since start_job_command is called for each line of the job, not
1882 just once). Also more thought needs to go into the entire algorithm;
1883 this is where the old parallel job code waits, so... */
1886 else if (has_jobserver_semaphore ())
1888 else if (job_fds[0] >= 0)
1898 DB (DB_JOBS, ("Need a job token; we %shave children\n",
1899 children ? "" : "don't "));
1901 /* If we don't already have a job started, use our "free" token. */
1902 if (!jobserver_tokens)
1906 /* Read a token. As long as there's no token available we'll block.
1907 We enable interruptible system calls before the read(2) so that if
1908 we get a SIGCHLD while we're waiting, we'll return with EINTR and
1909 we can process the death(s) and return tokens to the free pool.
1911 Once we return from the read, we immediately reinstate restartable
1912 system calls. This allows us to not worry about checking for
1913 EINTR on all the other system calls in the program.
1915 There is one other twist: there is a span between the time
1916 reap_children() does its last check for dead children and the time
1917 the read(2) call is entered, below, where if a child dies we won't
1918 notice. This is extremely serious as it could cause us to
1919 deadlock, given the right set of events.
1921 To avoid this, we do the following: before we reap_children(), we
1922 dup(2) the read FD on the jobserver pipe. The read(2) call below
1923 uses that new FD. In the signal handler, we close that FD. That
1924 way, if a child dies during the section mentioned above, the
1925 read(2) will be invoked with an invalid FD and will return
1926 immediately with EBADF. */
1928 /* Make sure we have a dup'd FD. */
1931 DB (DB_JOBS, ("Duplicate the job FD\n"));
1932 job_rfd = dup (job_fds[0]);
1936 /* Reap anything that's currently waiting. */
1937 reap_children (0, 0);
1939 /* Kick off any jobs we have waiting for an opportunity that
1940 can run now (i.e., waiting for load). */
1941 start_waiting_jobs ();
1943 /* If our "free" slot has become available, use it; we don't need an
1945 if (!jobserver_tokens)
1948 /* There must be at least one child already, or we have no business
1949 waiting for a token. */
1951 fatal (NILF, "INTERNAL: no children as we go to sleep on read\n");
1954 /* On Windows we simply wait for the jobserver semaphore to become
1955 * signalled or one of our child processes to terminate.
1957 got_token = wait_for_semaphore_or_child_process ();
1960 DWORD err = GetLastError ();
1961 fatal (NILF, _("semaphore or child process wait: (Error %ld: %s)"),
1962 err, map_windows32_error_to_string (err));
1965 /* Set interruptible system calls, and read() for a job token. */
1966 set_child_handler_action_flags (1, waiting_jobs != NULL);
1967 got_token = read (job_rfd, &token, 1);
1968 saved_errno = errno;
1969 set_child_handler_action_flags (0, waiting_jobs != NULL);
1972 /* If we got one, we're done here. */
1975 DB (DB_JOBS, (_("Obtained token for child %p (%s).\n"),
1981 /* If the error _wasn't_ expected (EINTR or EBADF), punt. Otherwise,
1982 go back and reap_children(), and try again. */
1983 errno = saved_errno;
1984 if (errno != EINTR && errno != EBADF)
1985 pfatal_with_name (_("read jobs pipe"));
1987 DB (DB_JOBS, ("Read returned EBADF.\n"));
1995 Use message here so that changes to working directories are logged. */
1998 char *newer = allocated_variable_expand_for_file ("$?", c->file);
2001 if (! cmds->fileinfo.filenm)
2002 nm = _("<builtin>");
2005 char *n = alloca (strlen (cmds->fileinfo.filenm) + 1 + 11 + 1);
2006 sprintf (n, "%s:%lu", cmds->fileinfo.filenm, cmds->fileinfo.lineno);
2010 if (newer[0] == '\0')
2011 message (0, _("%s: target '%s' does not exist"), nm, c->file->name);
2013 message (0, _("%s: update target '%s' due to: %s"), nm,
2014 c->file->name, newer);
2019 /* The job is now primed. Start it running.
2020 (This will notice if there is in fact no recipe.) */
2021 start_waiting_job (c);
2023 if (job_slots == 1 || not_parallel)
2024 /* Since there is only one job slot, make things run linearly.
2025 Wait for the child to die, setting the state to 'cs_finished'. */
2026 while (file->command_state == cs_running)
2027 reap_children (1, 0);
2033 /* Move CHILD's pointers to the next command for it to execute.
2034 Returns nonzero if there is another command. */
2037 job_next_command (struct child *child)
2039 while (child->command_ptr == 0 || *child->command_ptr == '\0')
2041 /* There are no more lines in the expansion of this line. */
2042 if (child->command_line == child->file->cmds->ncommand_lines)
2044 /* There are no more lines to be expanded. */
2045 child->command_ptr = 0;
2049 /* Get the next line to run. */
2050 child->command_ptr = child->command_lines[child->command_line++];
2055 /* Determine if the load average on the system is too high to start a new job.
2056 The real system load average is only recomputed once a second. However, a
2057 very parallel make can easily start tens or even hundreds of jobs in a
2058 second, which brings the system to its knees for a while until that first
2059 batch of jobs clears out.
2061 To avoid this we use a weighted algorithm to try to account for jobs which
2062 have been started since the last second, and guess what the load average
2063 would be now if it were computed.
2065 This algorithm was provided by Thomas Riedl <thomas.riedl@siemens.com>,
2068 ! calculate something load-oid and add to the observed sys.load,
2069 ! so that latter can catch up:
2070 ! - every job started increases jobctr;
2071 ! - every dying job decreases a positive jobctr;
2072 ! - the jobctr value gets zeroed every change of seconds,
2073 ! after its value*weight_b is stored into the 'backlog' value last_sec
2074 ! - weight_a times the sum of jobctr and last_sec gets
2075 ! added to the observed sys.load.
2077 ! The two weights have been tried out on 24 and 48 proc. Sun Solaris-9
2078 ! machines, using a several-thousand-jobs-mix of cpp, cc, cxx and smallish
2079 ! sub-shelled commands (rm, echo, sed...) for tests.
2080 ! lowering the 'direct influence' factor weight_a (e.g. to 0.1)
2081 ! resulted in significant excession of the load limit, raising it
2082 ! (e.g. to 0.5) took bad to small, fast-executing jobs and didn't
2083 ! reach the limit in most test cases.
2085 ! lowering the 'history influence' weight_b (e.g. to 0.1) resulted in
2086 ! exceeding the limit for longer-running stuff (compile jobs in
2087 ! the .5 to 1.5 sec. range),raising it (e.g. to 0.5) overrepresented
2088 ! small jobs' effects.
2092 #define LOAD_WEIGHT_A 0.25
2093 #define LOAD_WEIGHT_B 0.25
2096 load_too_high (void)
2098 #if defined(__MSDOS__) || defined(VMS) || defined(_AMIGA) || defined(__riscos__)
2101 static double last_sec;
2102 static time_t last_now;
2107 /* sub_proc.c cannot wait for more than MAXIMUM_WAIT_OBJECTS children */
2108 if (process_used_slots () >= MAXIMUM_WAIT_OBJECTS)
2112 if (max_load_average < 0)
2115 /* Find the real system load average. */
2117 if (getloadavg (&load, 1) != 1)
2119 static int lossage = -1;
2120 /* Complain only once for the same error. */
2121 if (lossage == -1 || errno != lossage)
2124 /* An errno value of zero means getloadavg is just unsupported. */
2126 _("cannot enforce load limits on this operating system"));
2128 perror_with_name (_("cannot enforce load limit: "), "getloadavg");
2135 /* If we're in a new second zero the counter and correct the backlog
2136 value. Only keep the backlog for one extra second; after that it's 0. */
2140 if (last_now == now - 1)
2141 last_sec = LOAD_WEIGHT_B * job_counter;
2149 /* Try to guess what the load would be right now. */
2150 guess = load + (LOAD_WEIGHT_A * (job_counter + last_sec));
2152 DB (DB_JOBS, ("Estimated system load = %f (actual = %f) (max requested = %f)\n",
2153 guess, load, max_load_average));
2155 return guess >= max_load_average;
2159 /* Start jobs that are waiting for the load to be lower. */
2162 start_waiting_jobs (void)
2166 if (waiting_jobs == 0)
2171 /* Check for recently deceased descendants. */
2172 reap_children (0, 0);
2174 /* Take a job off the waiting list. */
2176 waiting_jobs = job->next;
2178 /* Try to start that job. We break out of the loop as soon
2179 as start_waiting_job puts one back on the waiting list. */
2181 while (start_waiting_job (job) && waiting_jobs != 0);
2188 /* EMX: Start a child process. This function returns the new pid. */
2189 # if defined __EMX__
2191 child_execute_job (int stdin_fd, int stdout_fd, int stderr_fd,
2192 char **argv, char **envp)
2195 int save_stdin = -1;
2196 int save_stdout = -1;
2197 int save_stderr = -1;
2199 /* For each FD which needs to be redirected first make a dup of the standard
2200 FD to save and mark it close on exec so our child won't see it. Then
2201 dup2() the standard FD to the redirect FD, and also mark the redirect FD
2202 as close on exec. */
2203 if (stdin_fd != FD_STDIN)
2205 save_stdin = dup (FD_STDIN);
2207 fatal (NILF, _("no more file handles: could not duplicate stdin\n"));
2208 CLOSE_ON_EXEC (save_stdin);
2210 dup2 (stdin_fd, FD_STDIN);
2211 CLOSE_ON_EXEC (stdin_fd);
2214 if (stdout_fd != FD_STDOUT)
2216 save_stdout = dup (FD_STDOUT);
2217 if (save_stdout < 0)
2218 fatal (NILF, _("no more file handles: could not duplicate stdout\n"));
2219 CLOSE_ON_EXEC (save_stdout);
2221 dup2 (stdout_fd, FD_STDOUT);
2222 CLOSE_ON_EXEC (stdout_fd);
2225 if (stderr_fd != FD_STDERR)
2227 if (stderr_fd != stdout_fd)
2229 save_stderr = dup (FD_STDERR);
2230 if (save_stderr < 0)
2231 fatal (NILF, _("no more file handles: could not duplicate stderr\n"));
2232 CLOSE_ON_EXEC (save_stderr);
2235 dup2 (stderr_fd, FD_STDERR);
2236 CLOSE_ON_EXEC (stderr_fd);
2239 /* Run the command. */
2240 pid = exec_command (argv, envp);
2242 /* Restore stdout/stdin/stderr of the parent and close temporary FDs. */
2243 if (save_stdin >= 0)
2245 if (dup2 (save_stdin, FD_STDIN) != FD_STDIN)
2246 fatal (NILF, _("Could not restore stdin\n"));
2251 if (save_stdout >= 0)
2253 if (dup2 (save_stdout, FD_STDOUT) != FD_STDOUT)
2254 fatal (NILF, _("Could not restore stdout\n"));
2256 close (save_stdout);
2259 if (save_stderr >= 0)
2261 if (dup2 (save_stderr, FD_STDERR) != FD_STDERR)
2262 fatal (NILF, _("Could not restore stderr\n"));
2264 close (save_stderr);
2270 #elif !defined (_AMIGA) && !defined (__MSDOS__) && !defined (VMS)
2273 Replace the current process with one executing the command in ARGV.
2274 STDIN_FD/STDOUT_FD/STDERR_FD are used as the process's stdin/stdout/stderr;
2275 ENVP is the environment of the new program. This function does not return. */
2277 child_execute_job (int stdin_fd, int stdout_fd, int stderr_fd,
2278 char **argv, char **envp)
2280 /* For any redirected FD, dup2() it to the standard FD then close it. */
2281 if (stdin_fd != FD_STDIN)
2283 dup2 (stdin_fd, FD_STDIN);
2287 if (stdout_fd != FD_STDOUT)
2288 dup2 (stdout_fd, FD_STDOUT);
2289 if (stderr_fd != FD_STDERR)
2290 dup2 (stderr_fd, FD_STDERR);
2292 if (stdout_fd != FD_STDOUT)
2294 if (stderr_fd != FD_STDERR && stderr_fd != stdout_fd)
2297 /* Run the command. */
2298 exec_command (argv, envp);
2300 #endif /* !AMIGA && !__MSDOS__ && !VMS */
2301 #endif /* !WINDOWS32 */
2304 /* Replace the current process with one running the command in ARGV,
2305 with environment ENVP. This function does not return. */
2307 /* EMX: This function returns the pid of the child process. */
2313 exec_command (char **argv, char **envp)
2316 /* to work around a problem with signals and execve: ignore them */
2318 signal (SIGCHLD,SIG_IGN);
2320 /* Run the program. */
2321 execve (argv[0], argv, envp);
2322 perror_with_name ("execve: ", argv[0]);
2323 _exit (EXIT_FAILURE);
2328 int exit_code = EXIT_FAILURE;
2330 /* make sure CreateProcess() has Path it needs */
2331 sync_Path_environment ();
2333 /* launch command */
2334 hPID = process_easy (argv, envp, -1, -1);
2336 /* make sure launch ok */
2337 if (hPID == INVALID_HANDLE_VALUE)
2340 fprintf (stderr, _("process_easy() failed to launch process (e=%ld)\n"),
2341 process_last_err (hPID));
2342 for (i = 0; argv[i]; i++)
2343 fprintf (stderr, "%s ", argv[i]);
2344 fprintf (stderr, _("\nCounted %d args in failed launch\n"), i);
2345 exit (EXIT_FAILURE);
2348 /* wait and reap last child */
2349 hWaitPID = process_wait_for_any (1, 0);
2352 /* was an error found on this process? */
2353 int err = process_last_err (hWaitPID);
2356 exit_code = process_exit_code (hWaitPID);
2359 fprintf (stderr, "make (e=%d, rc=%d): %s",
2360 err, exit_code, map_windows32_error_to_string (err));
2362 /* cleanup process */
2363 process_cleanup (hWaitPID);
2365 /* expect to find only last pid, warn about other pids reaped */
2366 if (hWaitPID == hPID)
2370 char *pidstr = xstrdup (pid2str ((pid_t)hWaitPID));
2373 _("make reaped child pid %s, still waiting for pid %s\n"),
2374 pidstr, pid2str ((pid_t)hPID));
2379 /* return child's exit code as our exit code */
2382 #else /* !WINDOWS32 */
2388 /* Be the user, permanently. */
2392 /* Run the program. */
2393 pid = spawnvpe (P_NOWAIT, argv[0], argv, envp);
2397 /* the file might have a strange shell extension */
2398 if (errno == ENOENT)
2402 /* Run the program. */
2404 execvp (argv[0], argv);
2406 # endif /* !__EMX__ */
2411 error (NILF, _("%s: Command not found"), argv[0]);
2415 /* The file is not executable. Try it as a shell script. */
2416 extern char *getenv ();
2423 /* Do not use $SHELL from the environment */
2424 struct variable *p = lookup_variable ("SHELL", 5);
2430 shell = getenv ("SHELL");
2433 shell = default_shell;
2436 while (argv[argc] != 0)
2444 new_argv = alloca ((1 + argc + 1) * sizeof (char *));
2445 new_argv[0] = shell;
2456 new_argv[i] = argv[0];
2459 new_argv[i + argc] = argv[argc];
2464 pid = spawnvpe (P_NOWAIT, shell, new_argv, envp);
2468 execvp (shell, new_argv);
2470 if (errno == ENOENT)
2471 error (NILF, _("%s: Shell program not found"), shell);
2473 perror_with_name ("execvp: ", shell);
2479 /* this nasty error was driving me nuts :-( */
2480 error (NILF, _("spawnvpe: environment space might be exhausted"));
2485 perror_with_name ("execvp: ", argv[0]);
2494 #endif /* !WINDOWS32 */
2497 #else /* On Amiga */
2498 void exec_command (char **argv)
2503 void clean_tmp (void)
2505 DeleteFile (amiga_bname);
2508 #endif /* On Amiga */
2511 /* Figure out the argument list necessary to run LINE as a command. Try to
2512 avoid using a shell. This routine handles only ' quoting, and " quoting
2513 when no backslash, $ or ' characters are seen in the quotes. Starting
2514 quotes may be escaped with a backslash. If any of the characters in
2515 sh_chars[] is seen, or any of the builtin commands listed in sh_cmds[]
2516 is the first word of a line, the shell is used.
2518 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
2519 If *RESTP is NULL, newlines will be ignored.
2521 SHELL is the shell to use, or nil to use the default shell.
2522 IFS is the value of $IFS, or nil (meaning the default).
2524 FLAGS is the value of lines_flags for this command line. It is
2525 used in the WINDOWS32 port to check whether + or $(MAKE) were found
2526 in this command line, in which case the effect of just_print_flag
2530 construct_command_argv_internal (char *line, char **restp, char *shell,
2531 char *shellflags, char *ifs, int flags,
2532 char **batch_filename UNUSED)
2535 /* MSDOS supports both the stock DOS shell and ports of Unixy shells.
2536 We call 'system' for anything that requires ''slow'' processing,
2537 because DOS shells are too dumb. When $SHELL points to a real
2538 (unix-style) shell, 'system' just calls it to do everything. When
2539 $SHELL points to a DOS shell, 'system' does most of the work
2540 internally, calling the shell only for its internal commands.
2541 However, it looks on the $PATH first, so you can e.g. have an
2542 external command named 'mkdir'.
2544 Since we call 'system', certain characters and commands below are
2545 actually not specific to COMMAND.COM, but to the DJGPP implementation
2546 of 'system'. In particular:
2548 The shell wildcard characters are in DOS_CHARS because they will
2549 not be expanded if we call the child via 'spawnXX'.
2551 The ';' is in DOS_CHARS, because our 'system' knows how to run
2552 multiple commands on a single line.
2554 DOS_CHARS also include characters special to 4DOS/NDOS, so we
2555 won't have to tell one from another and have one more set of
2556 commands and special characters. */
2557 static char sh_chars_dos[] = "*?[];|<>%^&()";
2558 static char *sh_cmds_dos[] = { "break", "call", "cd", "chcp", "chdir", "cls",
2559 "copy", "ctty", "date", "del", "dir", "echo",
2560 "erase", "exit", "for", "goto", "if", "md",
2561 "mkdir", "path", "pause", "prompt", "rd",
2562 "rmdir", "rem", "ren", "rename", "set",
2563 "shift", "time", "type", "ver", "verify",
2566 static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^";
2567 static char *sh_cmds_sh[] = { "cd", "echo", "eval", "exec", "exit", "login",
2568 "logout", "set", "umask", "wait", "while",
2569 "for", "case", "if", ":", ".", "break",
2570 "continue", "export", "read", "readonly",
2571 "shift", "times", "trap", "switch", "unset",
2576 #elif defined (__EMX__)
2577 static char sh_chars_dos[] = "*?[];|<>%^&()";
2578 static char *sh_cmds_dos[] = { "break", "call", "cd", "chcp", "chdir", "cls",
2579 "copy", "ctty", "date", "del", "dir", "echo",
2580 "erase", "exit", "for", "goto", "if", "md",
2581 "mkdir", "path", "pause", "prompt", "rd",
2582 "rmdir", "rem", "ren", "rename", "set",
2583 "shift", "time", "type", "ver", "verify",
2586 static char sh_chars_os2[] = "*?[];|<>%^()\"'&";
2587 static char *sh_cmds_os2[] = { "call", "cd", "chcp", "chdir", "cls", "copy",
2588 "date", "del", "detach", "dir", "echo",
2589 "endlocal", "erase", "exit", "for", "goto", "if",
2590 "keys", "md", "mkdir", "move", "path", "pause",
2591 "prompt", "rd", "rem", "ren", "rename", "rmdir",
2592 "set", "setlocal", "shift", "start", "time",
2593 "type", "ver", "verify", "vol", ":", 0 };
2595 static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^~'";
2596 static char *sh_cmds_sh[] = { "echo", "cd", "eval", "exec", "exit", "login",
2597 "logout", "set", "umask", "wait", "while",
2598 "for", "case", "if", ":", ".", "break",
2599 "continue", "export", "read", "readonly",
2600 "shift", "times", "trap", "switch", "unset",
2605 #elif defined (_AMIGA)
2606 static char sh_chars[] = "#;\"|<>()?*$`";
2607 static char *sh_cmds[] = { "cd", "eval", "if", "delete", "echo", "copy",
2608 "rename", "set", "setenv", "date", "makedir",
2609 "skip", "else", "endif", "path", "prompt",
2610 "unset", "unsetenv", "version",
2612 #elif defined (WINDOWS32)
2613 /* We used to have a double quote (") in sh_chars_dos[] below, but
2614 that caused any command line with quoted file names be run
2615 through a temporary batch file, which introduces command-line
2616 limit of 4K charcaters imposed by cmd.exe. Since CreateProcess
2617 can handle quoted file names just fine, removing the quote lifts
2618 the limit from a very frequent use case, because using quoted
2619 file names is commonplace on MS-Windows. */
2620 static char sh_chars_dos[] = "|&<>";
2621 static char *sh_cmds_dos[] = { "assoc", "break", "call", "cd", "chcp",
2622 "chdir", "cls", "color", "copy", "ctty",
2623 "date", "del", "dir", "echo", "echo.",
2624 "endlocal", "erase", "exit", "for", "ftype",
2625 "goto", "if", "if", "md", "mkdir", "move",
2626 "path", "pause", "prompt", "rd", "rem", "ren",
2627 "rename", "rmdir", "set", "setlocal",
2628 "shift", "time", "title", "type", "ver",
2629 "verify", "vol", ":", 0 };
2630 static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^";
2631 static char *sh_cmds_sh[] = { "cd", "eval", "exec", "exit", "login",
2632 "logout", "set", "umask", "wait", "while", "for",
2633 "case", "if", ":", ".", "break", "continue",
2634 "export", "read", "readonly", "shift", "times",
2635 "trap", "switch", "test",
2636 #ifdef BATCH_MODE_ONLY_SHELL
2642 #elif defined(__riscos__)
2643 static char sh_chars[] = "";
2644 static char *sh_cmds[] = { 0 };
2645 #else /* must be UNIX-ish */
2646 static char sh_chars[] = "#;\"*?[]&|<>(){}$`^~!";
2647 static char *sh_cmds[] = { ".", ":", "break", "case", "cd", "continue",
2648 "eval", "exec", "exit", "export", "for", "if",
2649 "login", "logout", "read", "readonly", "set",
2650 "shift", "switch", "test", "times", "trap",
2651 "ulimit", "umask", "unset", "wait", "while", 0 };
2652 # ifdef HAVE_DOS_PATHS
2653 /* This is required if the MSYS/Cygwin ports (which do not define
2654 WINDOWS32) are compiled with HAVE_DOS_PATHS defined, which uses
2655 sh_chars_sh[] directly (see below). */
2656 static char *sh_chars_sh = sh_chars;
2657 # endif /* HAVE_DOS_PATHS */
2663 int instring, word_has_equals, seen_nonequals, last_argument_was_empty;
2664 char **new_argv = 0;
2671 sh_cmds = sh_cmds_dos;
2672 sh_chars = sh_chars_dos;
2676 sh_cmds = sh_cmds_sh;
2677 sh_chars = sh_chars_sh;
2679 #endif /* WINDOWS32 */
2684 /* Make sure not to bother processing an empty line. */
2685 while (isblank ((unsigned char)*line))
2690 if (shellflags == 0)
2691 shellflags = posix_pedantic ? "-ec" : "-c";
2693 /* See if it is safe to parse commands internally. */
2695 shell = default_shell;
2697 else if (strcmp (shell, default_shell))
2699 char *s1 = _fullpath (NULL, shell, 0);
2700 char *s2 = _fullpath (NULL, default_shell, 0);
2702 slow_flag = strcmp ((s1 ? s1 : ""), (s2 ? s2 : ""));
2711 #else /* not WINDOWS32 */
2712 #if defined (__MSDOS__) || defined (__EMX__)
2713 else if (strcasecmp (shell, default_shell))
2715 extern int _is_unixy_shell (const char *_path);
2717 DB (DB_BASIC, (_("$SHELL changed (was '%s', now '%s')\n"),
2718 default_shell, shell));
2719 unixy_shell = _is_unixy_shell (shell);
2720 /* we must allocate a copy of shell: construct_command_argv() will free
2721 * shell after this function returns. */
2722 default_shell = xstrdup (shell);
2726 sh_chars = sh_chars_sh;
2727 sh_cmds = sh_cmds_sh;
2731 sh_chars = sh_chars_dos;
2732 sh_cmds = sh_cmds_dos;
2734 if (_osmode == OS2_MODE)
2736 sh_chars = sh_chars_os2;
2737 sh_cmds = sh_cmds_os2;
2741 #else /* !__MSDOS__ */
2742 else if (strcmp (shell, default_shell))
2744 #endif /* !__MSDOS__ && !__EMX__ */
2745 #endif /* not WINDOWS32 */
2748 for (ap = ifs; *ap != '\0'; ++ap)
2749 if (*ap != ' ' && *ap != '\t' && *ap != '\n')
2752 if (shellflags != 0)
2753 if (shellflags[0] != '-'
2754 || ((shellflags[1] != 'c' || shellflags[2] != '\0')
2755 && (shellflags[1] != 'e' || shellflags[2] != 'c' || shellflags[3] != '\0')))
2758 i = strlen (line) + 1;
2760 /* More than 1 arg per character is impossible. */
2761 new_argv = xmalloc (i * sizeof (char *));
2763 /* All the args can fit in a buffer as big as LINE is. */
2764 ap = new_argv[0] = argstr = xmalloc (i);
2767 /* I is how many complete arguments have been found. */
2769 instring = word_has_equals = seen_nonequals = last_argument_was_empty = 0;
2770 for (p = line; *p != '\0'; ++p)
2776 /* Inside a string, just copy any char except a closing quote
2777 or a backslash-newline combination. */
2781 if (ap == new_argv[0] || *(ap-1) == '\0')
2782 last_argument_was_empty = 1;
2784 else if (*p == '\\' && p[1] == '\n')
2786 /* Backslash-newline is handled differently depending on what
2787 kind of string we're in: inside single-quoted strings you
2788 keep them; in double-quoted strings they disappear. For
2789 DOS/Windows/OS2, if we don't have a POSIX shell, we keep the
2790 pre-POSIX behavior of removing the backslash-newline. */
2792 #if defined (__MSDOS__) || defined (__EMX__) || defined (WINDOWS32)
2803 else if (*p == '\n' && restp != NULL)
2805 /* End of the command line. */
2809 /* Backslash, $, and ` are special inside double quotes.
2810 If we see any of those, punt.
2811 But on MSDOS, if we use COMMAND.COM, double and single
2812 quotes have the same effect. */
2813 else if (instring == '"' && strchr ("\\$`", *p) != 0 && unixy_shell)
2816 else if (instring == '"' && strncmp (p, "\\\"", 2) == 0)
2822 else if (strchr (sh_chars, *p) != 0)
2823 /* Not inside a string, but it's a special char. */
2825 else if (one_shell && *p == '\n')
2826 /* In .ONESHELL mode \n is a separator like ; or && */
2829 else if (*p == '.' && p[1] == '.' && p[2] == '.' && p[3] != '.')
2830 /* '...' is a wildcard in DJGPP. */
2834 /* Not a special char. */
2838 /* Equals is a special character in leading words before the
2839 first word with no equals sign in it. This is not the case
2840 with sh -k, but we never get here when using nonstandard
2842 if (! seen_nonequals && unixy_shell)
2844 word_has_equals = 1;
2849 /* Backslash-newline has special case handling, ref POSIX.
2850 We're in the fastpath, so emulate what the shell would do. */
2853 /* Throw out the backslash and newline. */
2856 /* If there's nothing in this argument yet, skip any
2857 whitespace before the start of the next word. */
2858 if (ap == new_argv[i])
2859 p = next_token (p + 1) - 1;
2862 /* Backslash before whitespace is not special if our shell
2864 else if (isspace (p[1]) && !unixy_shell)
2870 else if (p[1] != '\0')
2872 #ifdef HAVE_DOS_PATHS
2873 /* Only remove backslashes before characters special to Unixy
2874 shells. All other backslashes are copied verbatim, since
2875 they are probably DOS-style directory separators. This
2876 still leaves a small window for problems, but at least it
2877 should work for the vast majority of naive users. */
2880 /* A dot is only special as part of the "..."
2882 if (strneq (p + 1, ".\\.\\.", 5))
2890 if (p[1] != '\\' && p[1] != '\''
2891 && !isspace ((unsigned char)p[1])
2892 && strchr (sh_chars_sh, p[1]) == 0)
2893 /* back up one notch, to copy the backslash */
2895 #endif /* HAVE_DOS_PATHS */
2897 /* Copy and skip the following char. */
2910 /* End of the command line. */
2915 /* Newlines are not special. */
2921 /* We have the end of an argument.
2922 Terminate the text of the argument. */
2925 last_argument_was_empty = 0;
2927 /* Update SEEN_NONEQUALS, which tells us if every word
2928 heretofore has contained an '='. */
2929 seen_nonequals |= ! word_has_equals;
2930 if (word_has_equals && ! seen_nonequals)
2931 /* An '=' in a word before the first
2932 word without one is magical. */
2934 word_has_equals = 0; /* Prepare for the next word. */
2936 /* If this argument is the command name,
2937 see if it is a built-in shell command.
2938 If so, have the shell handle it. */
2942 for (j = 0; sh_cmds[j] != 0; ++j)
2944 if (streq (sh_cmds[j], new_argv[0]))
2946 #if defined(__EMX__) || defined(WINDOWS32)
2947 /* Non-Unix shells are case insensitive. */
2949 && strcasecmp (sh_cmds[j], new_argv[0]) == 0)
2955 /* Ignore multiple whitespace chars. */
2956 p = next_token (p) - 1;
2967 /* Let the shell deal with an unterminated quote. */
2970 /* Terminate the last argument and the argument list. */
2973 if (new_argv[i][0] != '\0' || last_argument_was_empty)
2980 for (j = 0; sh_cmds[j] != 0; ++j)
2981 if (streq (sh_cmds[j], new_argv[0]))
2985 if (new_argv[0] == 0)
2987 /* Line was empty. */
2996 /* We must use the shell. */
3000 /* Free the old argument list we were working on. */
3006 execute_by_shell = 1; /* actually, call 'system' if shell isn't unixy */
3015 buffer = xmalloc (strlen (line)+1);
3018 for (dptr=buffer; *ptr; )
3020 if (*ptr == '\\' && ptr[1] == '\n')
3022 else if (*ptr == '@') /* Kludge: multiline commands */
3032 new_argv = xmalloc (2 * sizeof (char *));
3033 new_argv[0] = buffer;
3036 #else /* Not Amiga */
3039 * Not eating this whitespace caused things like
3043 * which gave the shell fits. I think we have to eat
3044 * whitespace here, but this code should be considered
3045 * suspicious if things start failing....
3048 /* Make sure not to bother processing an empty line. */
3049 while (isspace ((unsigned char)*line))
3053 #endif /* WINDOWS32 */
3056 /* SHELL may be a multi-word command. Construct a command line
3057 "$(SHELL) $(.SHELLFLAGS) LINE", with all special chars in LINE escaped.
3058 Then recurse, expanding this command line to get the final
3062 unsigned int shell_len = strlen (shell);
3063 unsigned int line_len = strlen (line);
3064 unsigned int sflags_len = shellflags ? strlen (shellflags) : 0;
3066 char *command_ptr = NULL; /* used for batch_mode_shell mode */
3070 # ifdef __EMX__ /* is this necessary? */
3071 if (!unixy_shell && shellflags)
3072 shellflags[0] = '/'; /* "/c" */
3075 /* In .ONESHELL mode we are allowed to throw the entire current
3076 recipe string at a single shell and trust that the user
3077 has configured the shell and shell flags, and formatted
3078 the string, appropriately. */
3081 /* If the shell is Bourne compatible, we must remove and ignore
3082 interior special chars [@+-] because they're meaningless to
3083 the shell itself. If, however, we're in .ONESHELL mode and
3084 have changed SHELL to something non-standard, we should
3085 leave those alone because they could be part of the
3086 script. In this case we must also leave in place
3087 any leading [@+-] for the same reason. */
3089 /* Remove and ignore interior prefix chars [@+-] because they're
3090 meaningless given a single shell. */
3091 #if defined __MSDOS__ || defined (__EMX__)
3092 if (unixy_shell) /* the test is complicated and we already did it */
3094 if (is_bourne_compatible_shell (shell)
3096 /* If we didn't find any sh.exe, don't behave is if we did! */
3097 && !no_default_sh_exe
3102 const char *f = line;
3105 /* Copy the recipe, removing and ignoring interior prefix chars
3106 [@+-]: they're meaningless in .ONESHELL mode. */
3107 while (f[0] != '\0')
3111 /* This is the start of a new recipe line.
3112 Skip whitespace and prefix characters. */
3113 while (isblank (*f) || *f == '-' || *f == '@' || *f == '+')
3116 /* Copy until we get to the next logical recipe line. */
3124 /* On unescaped newline, we're done with this line. */
3125 if (f[-1] == '\n' && ! esc)
3128 /* Something else: reset the escape sequence. */
3136 else /* non-Posix shell (cmd.exe etc.) */
3138 const char *f = line;
3143 int id = GetCurrentProcessId ();
3146 /* Generate a file name for the temporary batch file. */
3147 sprintf (fbuf, "make%d", id);
3148 *batch_filename = create_batch_file (fbuf, 0, &temp_fd);
3149 DB (DB_JOBS, (_("Creating temporary batch file %s\n"),
3152 /* Create a FILE object for the batch file, and write to it the
3153 commands to be executed. Put the batch file in TEXT mode. */
3154 _setmode (temp_fd, _O_TEXT);
3155 batch = _fdopen (temp_fd, "wt");
3156 fputs ("@echo off\n", batch);
3157 DB (DB_JOBS, (_("Batch file contents:\n\t@echo off\n")));
3159 /* Copy the recipe, removing and ignoring interior prefix chars
3160 [@+-]: they're meaningless in .ONESHELL mode. */
3163 /* This is the start of a new recipe line.
3164 Skip whitespace and prefix characters. */
3165 while (isblank (*f) || *f == '-' || *f == '@' || *f == '+')
3168 /* Copy until we get to the next logical recipe line. */
3171 /* Remove the escaped newlines in the command, and
3172 the whitespace that follows them. Windows
3173 shells cannot handle escaped newlines. */
3174 if (*f == '\\' && f[1] == '\n')
3177 while (isblank (*f))
3181 /* On an unescaped newline, we're done with this
3186 /* Write another line into the batch file. */
3191 fputs (tstart, batch);
3192 DB (DB_JOBS, ("\t%s", tstart));
3197 DB (DB_JOBS, ("\n"));
3200 /* Create an argv list for the shell command line that
3201 will run the batch file. */
3202 new_argv = xmalloc (2 * sizeof (char *));
3203 new_argv[0] = xstrdup (*batch_filename);
3207 #endif /* WINDOWS32 */
3208 /* Create an argv list for the shell command line. */
3212 new_argv = xmalloc ((4 + sflags_len/2) * sizeof (char *));
3213 new_argv[n++] = xstrdup (shell);
3215 /* Chop up the shellflags (if any) and assign them. */
3217 new_argv[n++] = xstrdup ("");
3220 const char *s = shellflags;
3223 while ((t = find_next_token (&s, &len)) != 0)
3224 new_argv[n++] = xstrndup (t, len);
3227 /* Set the command to invoke. */
3228 new_argv[n++] = line;
3229 new_argv[n++] = NULL;
3234 #ifdef MAX_ARG_STRLEN
3235 static char eval_line[] = "eval\\ \\\"set\\ x\\;\\ shift\\;\\ ";
3236 #define ARG_NUMBER_DIGITS 5
3237 #define EVAL_LEN (sizeof(eval_line)-1 + shell_len + 4 \
3238 + (7 + ARG_NUMBER_DIGITS) * 2 * line_len / (MAX_ARG_STRLEN - 2))
3243 new_line = xmalloc ((shell_len*2) + 1 + sflags_len + 1
3244 + (line_len*2) + 1 + EVAL_LEN);
3246 /* Copy SHELL, escaping any characters special to the shell. If
3247 we don't escape them, construct_command_argv_internal will
3248 recursively call itself ad nauseam, or until stack overflow,
3249 whichever happens first. */
3250 for (p = shell; *p != '\0'; ++p)
3252 if (strchr (sh_chars, *p) != 0)
3258 memcpy (ap, shellflags, sflags_len);
3265 #if !defined (WINDOWS32) && defined (MAX_ARG_STRLEN)
3266 if (unixy_shell && line_len > MAX_ARG_STRLEN)
3269 memcpy (ap, eval_line, sizeof (eval_line) - 1);
3270 ap += sizeof (eval_line) - 1;
3271 for (j = 1; j <= 2 * line_len / (MAX_ARG_STRLEN - 2); j++)
3272 ap += sprintf (ap, "\\$\\{%u\\}", j);
3276 /* Copy only the first word of SHELL to $0. */
3277 for (p = shell; *p != '\0'; ++p)
3279 if (isspace ((unsigned char)*p))
3288 for (p = line; *p != '\0'; ++p)
3290 if (restp != NULL && *p == '\n')
3295 else if (*p == '\\' && p[1] == '\n')
3297 /* POSIX says we keep the backslash-newline. If we don't have a
3298 POSIX shell on DOS/Windows/OS2, mimic the pre-POSIX behavior
3299 and remove the backslash/newline. */
3300 #if defined (__MSDOS__) || defined (__EMX__) || defined (WINDOWS32)
3301 # define PRESERVE_BSNL unixy_shell
3303 # define PRESERVE_BSNL 1
3308 /* Only non-batch execution needs another backslash,
3309 because it will be passed through a recursive
3310 invocation of this function. */
3311 if (!batch_mode_shell)
3319 /* DOS shells don't know about backslash-escaping. */
3320 if (unixy_shell && !batch_mode_shell &&
3321 (*p == '\\' || *p == '\'' || *p == '"'
3322 || isspace ((unsigned char)*p)
3323 || strchr (sh_chars, *p) != 0))
3326 else if (unixy_shell && strneq (p, "...", 3))
3328 /* The case of '...' wildcard again. */
3329 strcpy (ap, "\\.\\.\\");
3336 #if !defined (WINDOWS32) && defined (MAX_ARG_STRLEN)
3337 if (unixy_shell && line_len > MAX_ARG_STRLEN && (ap - args_ptr > MAX_ARG_STRLEN - 2))
3344 if (ap == new_line + shell_len + sflags_len + 2)
3346 /* Line was empty. */
3353 /* Some shells do not work well when invoked as 'sh -c xxx' to run a
3354 command line (e.g. Cygnus GNUWIN32 sh.exe on WIN32 systems). In these
3355 cases, run commands via a script file. */
3356 if (just_print_flag && !(flags & COMMANDS_RECURSE))
3358 /* Need to allocate new_argv, although it's unused, because
3359 start_job_command will want to free it and its 0'th element. */
3360 new_argv = xmalloc (2 * sizeof (char *));
3361 new_argv[0] = xstrdup ("");
3364 else if ((no_default_sh_exe || batch_mode_shell) && batch_filename)
3368 int id = GetCurrentProcessId ();
3371 /* create a file name */
3372 sprintf (fbuf, "make%d", id);
3373 *batch_filename = create_batch_file (fbuf, unixy_shell, &temp_fd);
3375 DB (DB_JOBS, (_("Creating temporary batch file %s\n"),
3378 /* Create a FILE object for the batch file, and write to it the
3379 commands to be executed. Put the batch file in TEXT mode. */
3380 _setmode (temp_fd, _O_TEXT);
3381 batch = _fdopen (temp_fd, "wt");
3383 fputs ("@echo off\n", batch);
3384 fputs (command_ptr, batch);
3385 fputc ('\n', batch);
3387 DB (DB_JOBS, (_("Batch file contents:%s\n\t%s\n"),
3388 !unixy_shell ? "\n\t@echo off" : "", command_ptr));
3391 new_argv = xmalloc (3 * sizeof (char *));
3394 new_argv[0] = xstrdup (shell);
3395 new_argv[1] = *batch_filename; /* only argv[0] gets freed later */
3399 new_argv[0] = xstrdup (*batch_filename);
3405 #endif /* WINDOWS32 */
3408 new_argv = construct_command_argv_internal (new_line, 0, 0, 0, 0,
3412 else if (!unixy_shell)
3414 /* new_line is local, must not be freed therefore
3415 We use line here instead of new_line because we run the shell
3417 size_t line_len = strlen (line);
3420 memcpy (new_line, line, line_len + 1);
3421 /* Replace all backslash-newline combination and also following tabs.
3422 Important: stop at the first '\n' because that's what the loop above
3423 did. The next line starting at restp[0] will be executed during the
3424 next call of this function. */
3425 while (*q != '\0' && *q != '\n')
3427 if (q[0] == '\\' && q[1] == '\n')
3428 q += 2; /* remove '\\' and '\n' */
3434 # ifndef NO_CMD_DEFAULT
3435 if (strnicmp (new_line, "echo", 4) == 0
3436 && (new_line[4] == ' ' || new_line[4] == '\t'))
3438 /* the builtin echo command: handle it separately */
3439 size_t echo_len = line_len - 5;
3440 char *echo_line = new_line + 5;
3442 /* special case: echo 'x="y"'
3443 cmd works this way: a string is printed as is, i.e., no quotes
3444 are removed. But autoconf uses a command like echo 'x="y"' to
3445 determine whether make works. autoconf expects the output x="y"
3446 so we will do exactly that.
3447 Note: if we do not allow cmd to be the default shell
3448 we do not need this kind of voodoo */
3449 if (echo_line[0] == '\''
3450 && echo_line[echo_len - 1] == '\''
3451 && strncmp (echo_line + 1, "ac_maketemp=",
3452 strlen ("ac_maketemp=")) == 0)
3454 /* remove the enclosing quotes */
3455 memmove (echo_line, echo_line + 1, echo_len - 2);
3456 echo_line[echo_len - 2] = '\0';
3462 /* Let the shell decide what to do. Put the command line into the
3463 2nd command line argument and hope for the best ;-) */
3464 size_t sh_len = strlen (shell);
3466 /* exactly 3 arguments + NULL */
3467 new_argv = xmalloc (4 * sizeof (char *));
3468 /* Exactly strlen(shell) + strlen("/c") + strlen(line) + 3 times
3469 the trailing '\0' */
3470 new_argv[0] = xmalloc (sh_len + line_len + 5);
3471 memcpy (new_argv[0], shell, sh_len + 1);
3472 new_argv[1] = new_argv[0] + sh_len + 1;
3473 memcpy (new_argv[1], "/c", 3);
3474 new_argv[2] = new_argv[1] + 3;
3475 memcpy (new_argv[2], new_line, line_len + 1);
3479 #elif defined(__MSDOS__)
3482 /* With MSDOS shells, we must construct the command line here
3483 instead of recursively calling ourselves, because we
3484 cannot backslash-escape the special characters (see above). */
3485 new_argv = xmalloc (sizeof (char *));
3486 line_len = strlen (new_line) - shell_len - sflags_len - 2;
3487 new_argv[0] = xmalloc (line_len + 1);
3488 strncpy (new_argv[0],
3489 new_line + shell_len + sflags_len + 2, line_len);
3490 new_argv[0][line_len] = '\0';
3494 fatal (NILF, _("%s (line %d) Bad shell context (!unixy && !batch_mode_shell)\n"),
3495 __FILE__, __LINE__);
3500 #endif /* ! AMIGA */
3506 /* Figure out the argument list necessary to run LINE as a command. Try to
3507 avoid using a shell. This routine handles only ' quoting, and " quoting
3508 when no backslash, $ or ' characters are seen in the quotes. Starting
3509 quotes may be escaped with a backslash. If any of the characters in
3510 sh_chars[] is seen, or any of the builtin commands listed in sh_cmds[]
3511 is the first word of a line, the shell is used.
3513 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
3514 If *RESTP is NULL, newlines will be ignored.
3516 FILE is the target whose commands these are. It is used for
3517 variable expansion for $(SHELL) and $(IFS). */
3520 construct_command_argv (char *line, char **restp, struct file *file,
3521 int cmd_flags, char **batch_filename)
3523 char *shell, *ifs, *shellflags;
3535 && (isspace ((unsigned char)*cptr)))
3540 && (!isspace ((unsigned char)*cptr)))
3545 argv = xmalloc (argc * sizeof (char *));
3554 && (isspace ((unsigned char)*cptr)))
3558 DB (DB_JOBS, ("argv[%d] = [%s]\n", argc, cptr));
3559 argv[argc++] = cptr;
3561 && (!isspace ((unsigned char)*cptr)))
3568 /* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
3569 int save = warn_undefined_variables_flag;
3570 warn_undefined_variables_flag = 0;
3572 shell = allocated_variable_expand_for_file ("$(SHELL)", file);
3575 * Convert to forward slashes so that construct_command_argv_internal()
3580 char *p = w32ify (shell, 0);
3586 static const char *unixroot = NULL;
3587 static const char *last_shell = "";
3588 static int init = 0;
3591 unixroot = getenv ("UNIXROOT");
3592 /* unixroot must be NULL or not empty */
3593 if (unixroot && unixroot[0] == '\0') unixroot = NULL;
3597 /* if we have an unixroot drive and if shell is not default_shell
3598 (which means it's either cmd.exe or the test has already been
3599 performed) and if shell is an absolute path without drive letter,
3600 try whether it exists e.g.: if "/bin/sh" does not exist use
3601 "$UNIXROOT/bin/sh" instead. */
3602 if (unixroot && shell && strcmp (shell, last_shell) != 0
3603 && (shell[0] == '/' || shell[0] == '\\'))
3605 /* trying a new shell, check whether it exists */
3606 size_t size = strlen (shell);
3607 char *buf = xmalloc (size + 7);
3608 memcpy (buf, shell, size);
3609 memcpy (buf + size, ".exe", 5); /* including the trailing '\0' */
3610 if (access (shell, F_OK) != 0 && access (buf, F_OK) != 0)
3612 /* try the same for the unixroot drive */
3613 memmove (buf + 2, buf, size + 5);
3614 buf[0] = unixroot[0];
3615 buf[1] = unixroot[1];
3616 if (access (buf, F_OK) == 0)
3617 /* we have found a shell! */
3627 #endif /* __EMX__ */
3629 shellflags = allocated_variable_expand_for_file ("$(.SHELLFLAGS)", file);
3630 ifs = allocated_variable_expand_for_file ("$(IFS)", file);
3632 warn_undefined_variables_flag = save;
3635 argv = construct_command_argv_internal (line, restp, shell, shellflags, ifs,
3636 cmd_flags, batch_filename);
3645 #if !defined(HAVE_DUP2) && !defined(_AMIGA)
3647 dup2 (int old, int new)
3662 #endif /* !HAVE_DUP2 && !_AMIGA */
3664 /* On VMS systems, include special VMS functions. */
3667 #include "vmsjobs.c"