1 /* Job execution and handling for GNU Make.
2 Copyright (C) 1988-2022 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 <https://www.gnu.org/licenses/>. */
31 /* Default shell to use. */
33 # ifdef HAVE_STRINGS_H
34 # include <strings.h> /* for strcasecmp, strncasecmp */
38 const char *default_shell = "sh.exe";
39 int no_default_sh_exe = 1;
40 int batch_mode_shell = 1;
43 #elif defined (_AMIGA)
45 const 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 const char *default_shell = "command.com";
56 int batch_mode_shell = 0;
58 #elif defined (__EMX__)
60 const char *default_shell = "/bin/sh";
61 int batch_mode_shell = 0;
67 const char *default_shell = "";
68 int batch_mode_shell = 0;
70 #define strsignal vms_strsignal
71 char * vms_strsignal (int status);
74 # define C_FACILITY_NO 0x350000
76 #ifndef VMS_POSIX_EXIT_MASK
77 # define VMS_POSIX_EXIT_MASK (C_FACILITY_NO | 0xA000)
82 const char *default_shell = "/bin/sh";
83 int batch_mode_shell = 0;
89 static int execute_by_shell;
90 static int dos_pid = 123;
92 int dos_command_running;
93 #endif /* __MSDOS__ */
96 # include <proto/dos.h>
97 static int amiga_pid = 123;
98 static int amiga_status;
99 static char amiga_bname[32];
100 static int amiga_batch_file;
105 # include <processes.h>
107 # include <starlet.h>
108 # include <lib$routines.h>
109 static void vmsWaitForChildren (int *);
113 # include <windows.h>
115 # include <process.h>
116 # include "sub_proc.h"
118 # include "pathstuff.h"
119 # define WAIT_NOHANG 1
120 #endif /* WINDOWS32 */
123 # include <process.h>
126 #if defined (HAVE_FCNTL_H)
130 #if defined (HAVE_SYS_WAIT_H) || defined (HAVE_UNION_WAIT)
131 # include <sys/wait.h>
135 # define WAIT_NOHANG(status) waitpid (-1, (status), WNOHANG)
136 #else /* Don't have waitpid. */
141 # define WAIT_NOHANG(status) wait3 ((status), WNOHANG, (struct rusage *) 0)
142 # endif /* Have wait3. */
143 #endif /* Have waitpid. */
145 #ifdef USE_POSIX_SPAWN
147 # include "findprog.h"
150 #if !defined (wait) && !defined (POSIX)
154 #ifndef HAVE_UNION_WAIT
159 # define WTERMSIG(x) ((x) & 0x7f)
162 # define WCOREDUMP(x) ((x) & 0x80)
165 # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
168 # define WIFSIGNALED(x) (WTERMSIG (x) != 0)
171 # define WIFEXITED(x) (WTERMSIG (x) == 0)
174 #else /* Have 'union wait'. */
176 # define WAIT_T union wait
178 # define WTERMSIG(x) ((x).w_termsig)
181 # define WCOREDUMP(x) ((x).w_coredump)
184 # define WEXITSTATUS(x) ((x).w_retcode)
187 # define WIFSIGNALED(x) (WTERMSIG(x) != 0)
190 # define WIFEXITED(x) (WTERMSIG(x) == 0)
193 #endif /* Don't have 'union wait'. */
195 #if !defined(HAVE_UNISTD_H) && !defined(WINDOWS32)
207 #if HAVE_SYS_LOADAVG_H
208 # include <sys/loadavg.h>
211 #if HAVE_DECL_GETLOADAVG == 0
212 int getloadavg (double loadavg[], int nelem);
215 /* Different systems have different requirements for pid_t.
216 Plus we have to support gettext string translation... Argh. */
220 static char pidstring[100];
221 #if defined(WINDOWS32) && (__GNUC__ > 3 || _MSC_VER > 1300)
222 /* %Id is only needed for 64-builds, which were not supported by
223 older versions of Windows compilers. */
224 sprintf (pidstring, "%Id", pid);
226 sprintf (pidstring, "%lu", (unsigned long) pid);
231 static void free_child (struct child *);
232 static void start_job_command (struct child *child);
233 static int load_too_high (void);
234 static int job_next_command (struct child *);
235 static int start_waiting_job (struct child *);
237 /* Chain of all live (or recently deceased) children. */
239 struct child *children = 0;
241 /* Number of children currently running. */
243 unsigned int job_slots_used = 0;
245 /* Nonzero if the 'good' standard input is in use. */
247 static int good_stdin_used = 0;
249 /* Chain of children waiting to run until the load average goes down. */
251 static struct child *waiting_jobs = 0;
253 /* Non-zero if we use a *real* shell (always so on Unix). */
257 /* Number of jobs started in the current second. */
259 unsigned long job_counter = 0;
261 /* Number of jobserver tokens this instance is currently using. */
263 unsigned int jobserver_tokens = 0;
268 * The macro which references this function is defined in makeint.h.
271 w32_kill (pid_t pid, int sig)
273 return ((process_kill ((HANDLE)pid, sig) == TRUE) ? 0 : -1);
276 /* This function creates a temporary file name with an extension specified
278 * Return an xmalloc'ed string of a newly created temp file and its
279 * file descriptor, or die. */
281 create_batch_file (char const *base, int unixy, int *fd)
283 const char *const ext = unixy ? "sh" : "bat";
284 const char *error_string = NULL;
285 char temp_path[MAX_PATH+1]; /* need to know its length */
286 unsigned path_size = GetTempPath (sizeof temp_path, temp_path);
288 /* The following variable is static so we won't try to reuse a name
289 that was generated a little while ago, because that file might
290 not be on disk yet, since we use FILE_ATTRIBUTE_TEMPORARY below,
291 which tells the OS it doesn't need to flush the cache to disk.
292 If the file is not yet on disk, we might think the name is
293 available, while it really isn't. This happens in parallel
294 builds, where Make doesn't wait for one job to finish before it
295 launches the next one. */
296 static unsigned uniq = 0;
297 static int second_loop = 0;
298 const size_t sizemax = strlen (base) + strlen (ext) + 10;
302 path_size = GetCurrentDirectory (sizeof temp_path, temp_path);
307 if (uniq >= 0x10000 && !second_loop)
309 /* If we already had 64K batch files in this
310 process, make a second loop through the numbers,
311 looking for free slots, i.e. files that were
312 deleted in the meantime. */
316 while (path_size > 0 &&
317 path_size + sizemax < sizeof temp_path &&
318 !(uniq >= 0x10000 && second_loop))
320 unsigned size = sprintf (temp_path + path_size,
322 temp_path[path_size - 1] == '\\' ? "" : "\\",
324 HANDLE h = CreateFile (temp_path, /* file name */
325 GENERIC_READ | GENERIC_WRITE, /* desired access */
326 0, /* no share mode */
327 NULL, /* default security attributes */
328 CREATE_NEW, /* creation disposition */
329 FILE_ATTRIBUTE_NORMAL | /* flags and attributes */
330 FILE_ATTRIBUTE_TEMPORARY, /* we'll delete it */
331 NULL); /* no template file */
333 if (h == INVALID_HANDLE_VALUE)
335 const DWORD er = GetLastError ();
337 if (er == ERROR_FILE_EXISTS || er == ERROR_ALREADY_EXISTS)
340 if (uniq == 0x10000 && !second_loop)
347 /* the temporary path is not guaranteed to exist */
348 else if (path_is_dot == 0)
350 path_size = GetCurrentDirectory (sizeof temp_path, temp_path);
356 error_string = map_windows32_error_to_string (er);
362 const unsigned final_size = path_size + size + 1;
363 char *const path = xmalloc (final_size);
364 memcpy (path, temp_path, final_size);
365 *fd = _open_osfhandle ((intptr_t)h, 0);
370 for (p = path; (ch = *p) != 0; ++p)
374 return path; /* good return */
379 if (error_string == NULL)
380 error_string = _("Cannot create a temporary file");
381 O (fatal, NILF, error_string);
386 #endif /* WINDOWS32 */
389 /* returns whether path is assumed to be a unix like shell. */
391 _is_unixy_shell (const char *path)
393 /* list of non unix shells */
394 const char *known_os2shells[] = {
406 /* find the rightmost '/' or '\\' */
407 const char *name = strrchr (path, '/');
408 const char *p = strrchr (path, '\\');
411 if (name && p) /* take the max */
412 name = (name > p) ? name : p;
413 else if (p) /* name must be 0 */
415 else if (!name) /* name and p must be 0 */
418 if (ISDIRSEP (*name))
422 while (known_os2shells[i] != NULL)
424 if (strcasecmp (name, known_os2shells[i]) == 0)
425 return 0; /* not a unix shell */
429 /* in doubt assume a unix like shell */
434 /* determines whether path looks to be a Bourne-like shell. */
436 is_bourne_compatible_shell (const char *path)
438 /* List of known POSIX (or POSIX-ish) shells. */
439 static const char *unix_shells[] = {
451 /* find the last directory separator, or the beginning of the string. */
452 const char *cp = path + strlen (path);
454 while (cp > path && !ISDIRSEP (cp[-1]))
457 /* this should be able to deal with extensions on Windows-like systems */
458 for (s = unix_shells; *s != NULL; ++s)
460 #if defined(WINDOWS32) || defined(__MSDOS__)
461 size_t len = strlen (*s);
462 if ((strlen (cp) >= len && STOP_SET (cp[len], MAP_DOT|MAP_NUL))
463 && strncasecmp (cp, *s, len) == 0)
465 if (strcmp (cp, *s) == 0)
467 return 1; /* a known unix-style shell */
470 /* if not on the list, assume it's not a Bourne-like shell */
475 extern sigset_t fatal_signal_set;
480 sigprocmask (SIG_BLOCK, &fatal_signal_set, (sigset_t *) 0);
486 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, (sigset_t *) 0);
493 sigemptyset (&empty);
494 sigprocmask (SIG_SETMASK, &empty, (sigset_t *) 0);
497 #elif defined(HAVE_SIGSETMASK)
499 extern int fatal_signal_mask;
504 sigblock (fatal_signal_mask);
510 sigsetmask (siggetmask () & ~fatal_signal_mask);
522 #define unblock_sigs()
531 /* Write an error message describing the exit status given in
532 EXIT_CODE, EXIT_SIG, and COREDUMP, for the target TARGET_NAME.
533 Append "(ignored)" if IGNORED is nonzero. */
536 child_error (struct child *child,
537 int exit_code, int exit_sig, int coredump, int ignored)
539 const char *pre = "*** ";
540 const char *post = "";
541 const char *dump = "";
542 const struct file *f = child->file;
543 const floc *flocp = &f->cmds->fileinfo;
548 if (ignored && run_silent)
551 if (exit_sig && coredump)
552 dump = _(" (core dumped)");
557 post = _(" (ignored)");
564 char *a = alloca (strlen (flocp->filenm) + 6 + INTSTR_LENGTH + 1);
565 sprintf (a, "%s:%lu", flocp->filenm, flocp->lineno + flocp->offset);
569 l = strlen (pre) + strlen (nm) + strlen (f->name) + strlen (post);
571 smode = shuffle_get_mode ();
574 #define SHUFFLE_PREFIX " shuffle="
575 char *a = alloca (CSTRLEN(SHUFFLE_PREFIX) + strlen (smode) + 1);
576 sprintf (a, SHUFFLE_PREFIX "%s", smode);
579 #undef SHUFFLE_PREFIX
582 OUTPUT_SET (&child->output);
587 error (NILF, l + INTSTR_LENGTH, _("%s[%s: %s] Error %d%s%s"),
588 pre, nm, f->name, exit_code, post, smode ? smode : "");
591 const char *s = strsignal (exit_sig);
592 error (NILF, l + strlen (s) + strlen (dump), "%s[%s: %s] %s%s%s%s",
593 pre, nm, f->name, s, dump, post, smode ? smode : "");
600 /* Handle a dead child. This handler may or may not ever be installed.
602 If we're using the jobserver feature without pselect(), we need it.
603 First, installing it ensures the read will interrupt on SIGCHLD. Second,
604 we close the dup'd read FD to ensure we don't enter another blocking read
605 without reaping all the dead children. In this case we don't need the
608 If we don't have either waitpid or wait3, then make is unreliable, but we
609 use the dead_children count to reap children as best we can. */
611 static unsigned int dead_children = 0;
614 child_handler (int sig UNUSED)
621 /* The signal handler must called only once! */
622 signal (SIGCHLD, SIG_DFL);
626 extern pid_t shell_function_pid;
628 /* Reap all dead children, storing the returned status and the new command
629 state ('cs_finished') in the 'file' member of the 'struct child' for the
630 dead child, and removing the child from the chain. In addition, if BLOCK
631 nonzero, we block in this function until we've reaped at least one
632 complete child, waiting for it to die if necessary. If ERR is nonzero,
633 print an error message first. */
636 reap_children (int block, int err)
641 /* Initially, assume we have some. */
645 # define REAP_MORE reap_more
647 # define REAP_MORE dead_children
652 We have at least one child outstanding OR a shell function in progress,
654 We're blocking for a complete child OR there are more children to reap
656 we'll keep reaping children. */
658 while ((children != 0 || shell_function_pid != 0)
659 && (block || REAP_MORE))
661 unsigned int remote = 0;
663 int exit_code, exit_sig, coredump;
664 struct child *lastc, *c;
666 int any_remote, any_local;
671 static int printed = 0;
673 /* We might block for a while, so let the user know why.
674 Only print this message once no matter how many jobs are left. */
677 O (error, NILF, _("*** Waiting for unfinished jobs...."));
681 /* We have one less dead child to reap. As noted in
682 child_handler() above, this count is completely unimportant for
683 all modern, POSIX-y systems that support wait3() or waitpid().
684 The rest of this comment below applies only to early, broken
685 pre-POSIX systems. We keep the count only because... it's there...
687 The test and decrement are not atomic; if it is compiled into:
688 register = dead_children - 1;
689 dead_children = register;
690 a SIGCHLD could come between the two instructions.
691 child_handler increments dead_children.
692 The second instruction here would lose that increment. But the
693 only effect of dead_children being wrong is that we might wait
694 longer than necessary to reap a child, and lose some parallelism;
695 and we might print the "Waiting for unfinished jobs" message above
696 when not necessary. */
698 if (dead_children > 0)
702 any_local = shell_function_pid != 0;
704 for (c = children; c != 0; lastc = c, c = c->next)
706 any_remote |= c->remote;
707 any_local |= ! c->remote;
709 /* If pid < 0, this child never even started. Handle it. */
714 /* According to POSIX, 127 is used for command not found. */
719 DB (DB_JOBS, (_("Live child %p (%s) PID %s %s\n"),
720 c, c->file->name, pid2str (c->pid),
721 c->remote ? _(" (remote)") : ""));
727 /* First, check for remote children. */
729 pid = remote_status (&exit_code, &exit_sig, &coredump, 0);
734 /* We got a remote child. */
738 /* A remote status command failed miserably. Punt. */
739 pfatal_with_name ("remote_status");
743 /* No remote children. Check for local children. */
744 #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
748 /* Todo: This needs more untangling multi-process support */
749 /* Just do single child process support now */
750 vmsWaitForChildren (&status);
753 /* VMS failure status can not be fully translated */
754 status = $VMS_STATUS_SUCCESS (c->cstatus) ? 0 : (1 << 8);
756 /* A Posix failure can be exactly translated */
757 if ((c->cstatus & VMS_POSIX_EXIT_MASK) == VMS_POSIX_EXIT_MASK)
758 status = (c->cstatus >> 3 & 255) << 8;
762 pid = WAIT_NOHANG (&status);
765 EINTRLOOP (pid, wait (&status));
773 /* The wait*() failed miserably. Punt. */
774 pfatal_with_name ("wait");
778 /* We got a child exit; chop the status word up. */
779 exit_code = WEXITSTATUS (status);
780 exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0;
781 coredump = WCOREDUMP (status);
785 /* No local children are dead. */
788 if (!block || !any_remote)
791 /* Now try a blocking wait for a remote child. */
792 pid = remote_status (&exit_code, &exit_sig, &coredump, 1);
794 pfatal_with_name ("remote_status");
797 /* No remote children either. Finally give up. */
800 /* We got a remote child. */
803 #endif /* !__MSDOS__, !Amiga, !WINDOWS32. */
806 /* Life is very different on MSDOS. */
809 exit_code = WEXITSTATUS (status);
810 if (exit_code == 0xff)
812 exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0;
814 #endif /* __MSDOS__ */
818 status = amiga_status;
819 exit_code = amiga_status;
827 DWORD dwWaitStatus = 0;
832 /* Record the thread ID of the main process, so that we
833 could suspend it in the signal handler. */
836 hcTID = GetCurrentThread ();
837 hcPID = GetCurrentProcess ();
838 if (!DuplicateHandle (hcPID, hcTID, hcPID, &main_thread, 0,
839 FALSE, DUPLICATE_SAME_ACCESS))
841 DWORD e = GetLastError ();
843 "Determine main thread ID (Error %ld: %s)\n",
844 e, map_windows32_error_to_string (e));
847 DB (DB_VERBOSE, ("Main thread handle = %p\n", main_thread));
850 /* wait for anything to finish */
851 hPID = process_wait_for_any (block, &dwWaitStatus);
854 /* was an error found on this process? */
855 int werr = process_last_err (hPID);
858 exit_code = process_exit_code (hPID);
860 /* the extra tests of exit_code are here to prevent
861 map_windows32_error_to_string from calling 'fatal',
862 which will then call reap_children again */
863 if (werr && exit_code > 0 && exit_code < WSABASEERR)
864 fprintf (stderr, "make (e=%d): %s", exit_code,
865 map_windows32_error_to_string (exit_code));
868 exit_sig = process_signal (hPID);
870 /* cleanup process */
871 process_cleanup (hPID);
875 else if (dwWaitStatus == WAIT_FAILED)
877 /* The WaitForMultipleObjects() failed miserably. Punt. */
878 pfatal_with_name ("WaitForMultipleObjects");
880 else if (dwWaitStatus == WAIT_TIMEOUT)
882 /* No child processes are finished. Give up waiting. */
889 #endif /* WINDOWS32 */
892 /* Some child finished: increment the command count. */
895 /* Check if this is the child of the 'shell' function. */
896 if (!remote && pid == shell_function_pid)
898 shell_completed (exit_code, exit_sig);
902 /* Search for a child matching the deceased one. */
904 for (c = children; c != 0; lastc = c, c = c->next)
905 if (c->pid == pid && c->remote == remote)
909 /* An unknown child died.
910 Ignore it; it was inherited from our invoker. */
913 DB (DB_JOBS, (exit_sig == 0 && exit_code == 0
914 ? _("Reaping winning child %p PID %s %s\n")
915 : _("Reaping losing child %p PID %s %s\n"),
916 c, pid2str (c->pid), c->remote ? _(" (remote)") : ""));
918 /* If we have started jobs in this second, remove one. */
924 #if defined(USE_POSIX_SPAWN)
925 /* Some versions of posix_spawn() do not detect errors such as command
926 not found until after they fork. In that case they will exit with a
927 code of 127. Try to detect that and provide a useful error message.
928 Otherwise we'll just show the error below, as normal. */
929 if (exit_sig == 0 && exit_code == 127 && c->cmd_name)
931 const char *e = NULL;
935 /* There are various ways that this will show a different error than
936 fork/exec. To really get the right error we'd have to fall back
937 to fork/exec but I don't want to bother with that. Just do the
940 EINTRLOOP(r, stat (c->cmd_name, &st));
942 e = strerror (errno);
943 else if (S_ISDIR(st.st_mode) || !(st.st_mode & S_IXUSR))
944 e = strerror (EACCES);
945 else if (st.st_size == 0)
946 e = strerror (ENOEXEC);
949 OSS(error, NILF, "%s: %s", c->cmd_name, e);
953 /* Determine the failure status: 0 for success, 1 for updating target in
954 question mode, 2 for anything else. */
955 if (exit_sig == 0 && exit_code == 0)
956 child_failed = MAKE_SUCCESS;
957 else if (exit_sig == 0 && exit_code == 1 && question_flag && c->recursive)
958 child_failed = MAKE_TROUBLE;
960 child_failed = MAKE_FAILURE;
962 if (c->sh_batch_file)
966 DB (DB_JOBS, (_("Cleaning up temp batch file %s\n"),
970 rm_status = remove (c->sh_batch_file);
972 DB (DB_JOBS, (_("Cleaning up temp batch file %s failed (%d)\n"),
973 c->sh_batch_file, errno));
975 /* all done with memory */
976 free (c->sh_batch_file);
977 c->sh_batch_file = NULL;
980 /* If this child had the good stdin, say it is now free. */
984 dontcare = c->dontcare;
986 if (child_failed && !c->noerror && !ignore_errors_flag)
988 /* The commands failed. Write an error message,
989 delete non-precious targets, and abort. */
990 static int delete_on_error = -1;
992 if (!dontcare && child_failed == MAKE_FAILURE)
993 child_error (c, exit_code, exit_sig, coredump, 0);
995 c->file->update_status = child_failed == MAKE_FAILURE ? us_failed : us_question;
996 if (delete_on_error == -1)
998 struct file *f = lookup_file (".DELETE_ON_ERROR");
999 delete_on_error = f != 0 && f->is_target;
1001 if (exit_sig != 0 || delete_on_error)
1002 delete_child_targets (c);
1008 /* The commands failed, but we don't care. */
1009 child_error (c, exit_code, exit_sig, coredump, 1);
1013 /* If there are more commands to run, try to start them. */
1014 if (job_next_command (c))
1016 if (handling_fatal_signal)
1018 /* Never start new commands while we are dying.
1019 Since there are more commands that wanted to be run,
1020 the target was not completely remade. So we treat
1021 this as if a command had failed. */
1022 c->file->update_status = us_failed;
1026 /* If we're sync'ing per line, write the previous line's
1027 output before starting the next one. */
1028 if (output_sync == OUTPUT_SYNC_LINE)
1029 output_dump (&c->output);
1030 /* Check again whether to start remotely.
1031 Whether or not we want to changes over time.
1032 Also, start_remote_job may need state set up
1033 by start_remote_job_p. */
1034 c->remote = start_remote_job_p (0);
1035 start_job_command (c);
1036 /* Fatal signals are left blocked in case we were
1037 about to put that child on the chain. But it is
1038 already there, so it is safe for a fatal signal to
1039 arrive now; it will clean up this child's targets. */
1041 if (c->file->command_state == cs_running)
1042 /* We successfully started the new command.
1043 Loop to reap more children. */
1047 if (c->file->update_status != us_success)
1048 /* We failed to start the commands. */
1049 delete_child_targets (c);
1052 /* There are no more commands. We got through them all
1053 without an unignored error. Now the target has been
1054 successfully updated. */
1055 c->file->update_status = us_success;
1058 /* When we get here, all the commands for c->file are finished. */
1060 /* Synchronize any remaining parallel output. */
1061 output_dump (&c->output);
1063 /* At this point c->file->update_status is success or failed. But
1064 c->file->command_state is still cs_running if all the commands
1065 ran; notice_finished_file looks for cs_running to tell it that
1066 it's interesting to check the file's modtime again now. */
1068 if (! handling_fatal_signal)
1069 /* Notice if the target of the commands has been changed.
1070 This also propagates its values for command_state and
1071 update_status to its also_make files. */
1072 notice_finished_file (c->file);
1074 /* Block fatal signals while frobnicating the list, so that
1075 children and job_slots_used are always consistent. Otherwise
1076 a fatal signal arriving after the child is off the chain and
1077 before job_slots_used is decremented would believe a child was
1078 live and call reap_children again. */
1083 DB (DB_JOBS, (_("Removing child %p PID %s%s from chain.\n"),
1084 c, pid2str (c->pid), c->remote ? _(" (remote)") : ""));
1087 /* There is now another slot open. */
1088 if (job_slots_used > 0)
1089 job_slots_used -= c->jobslot;
1091 /* Remove the child from the chain and free it. */
1095 lastc->next = c->next;
1101 /* If the job failed, and the -k flag was not given, die,
1102 unless we are already in the process of dying. */
1103 if (!err && child_failed && !dontcare && !keep_going_flag &&
1104 /* fatal_error_signal will die with the right signal. */
1105 !handling_fatal_signal)
1108 /* Only block for one child. */
1115 /* Free the storage allocated for CHILD. */
1118 free_childbase (struct childbase *child)
1120 if (child->environment != 0)
1122 char **ep = child->environment;
1125 free (child->environment);
1128 free (child->cmd_name);
1132 free_child (struct child *child)
1134 output_close (&child->output);
1136 if (!jobserver_tokens)
1137 ONS (fatal, NILF, "INTERNAL: Freeing child %p (%s) but no tokens left",
1138 child, child->file->name);
1140 /* If we're using the jobserver and this child is not the only outstanding
1141 job, put a token back into the pipe for it. */
1143 if (jobserver_enabled () && jobserver_tokens > 1)
1145 jobserver_release (1);
1146 DB (DB_JOBS, (_("Released token for child %p (%s).\n"),
1147 child, child->file->name));
1152 if (handling_fatal_signal) /* Don't bother free'ing if about to die. */
1155 if (child->command_lines != 0)
1158 for (i = 0; i < child->file->cmds->ncommand_lines; ++i)
1159 free (child->command_lines[i]);
1160 free (child->command_lines);
1163 free_childbase ((struct childbase*)child);
1169 /* Start a job to run the commands specified in CHILD.
1170 CHILD is updated to reflect the commands and ID of the child process.
1172 NOTE: On return fatal signals are blocked! The caller is responsible
1173 for calling 'unblock_sigs', once the new child is safely on the chain so
1174 it can be cleaned up in the event of a fatal signal. */
1177 start_job_command (struct child *child)
1182 # define FREE_ARGV(_a)
1185 # define FREE_ARGV(_a) do{ if (_a) { free ((_a)[0]); free (_a); } }while(0)
1189 /* If we have a completely empty commandset, stop now. */
1190 if (!child->command_ptr)
1193 /* Combine the flags parsed for the line itself with
1194 the flags specified globally for this target. */
1195 flags = (child->file->command_flags
1196 | child->file->cmds->lines_flags[child->command_line - 1]);
1198 p = child->command_ptr;
1199 child->noerror = ((flags & COMMANDS_NOERROR) != 0);
1204 flags |= COMMANDS_SILENT;
1206 flags |= COMMANDS_RECURSE;
1209 /* Don't skip newlines. */
1210 else if (!ISBLANK (*p))
1215 child->recursive = ((flags & COMMANDS_RECURSE) != 0);
1217 /* Update the file's command flags with any new ones we found. We only
1218 keep the COMMANDS_RECURSE setting. Even this isn't 100% correct; we are
1219 now marking more commands recursive than should be in the case of
1220 multiline define/endef scripts where only one line is marked "+". In
1221 order to really fix this, we'll have to keep a lines_flags for every
1222 actual line, after expansion. */
1223 child->file->cmds->lines_flags[child->command_line - 1] |= flags & COMMANDS_RECURSE;
1225 /* POSIX requires that a recipe prefix after a backslash-newline should
1226 be ignored. Remove it now so the output is correct. */
1228 char prefix = child->file->cmds->recipe_prefix;
1234 if (p1[0] == '\n' && p1[1] == prefix)
1241 /* Figure out an argument list from this command line. */
1245 /* Skip any leading whitespace */
1252 if ((p[1] != '\n') && (p[1] != 'n') && (p[1] != 't'))
1259 /* Please note, for VMS argv is a string (not an array of strings) which
1260 contains the complete command line, which for multi-line variables
1261 still includes the newlines. So detect newlines and set 'end' (which
1262 is used for child->command_ptr) instead of (re-)writing
1263 construct_command_argv */
1271 instring = !instring;
1272 else if (*s == '\\' && !instring && *(s+1) != 0)
1274 else if (*s == '\n' && !instring)
1283 argv = construct_command_argv (p, &end, child->file,
1284 child->file->cmds->lines_flags[child->command_line - 1],
1285 &child->sh_batch_file);
1288 child->command_ptr = NULL;
1292 child->command_ptr = end;
1296 /* If -q was given, say that updating 'failed' if there was any text on the
1297 command line, or 'succeeded' otherwise. The exit status of 1 tells the
1298 user that -q is saying 'something to do'; the exit status for a random
1300 if (argv != 0 && question_flag && !(flags & COMMANDS_RECURSE))
1304 /* On VMS, argv[0] can be a null string here */
1308 child->file->update_status = us_question;
1309 notice_finished_file (child->file);
1316 if (touch_flag && !(flags & COMMANDS_RECURSE))
1318 /* Go on to the next command. It might be the recursive one.
1319 We construct ARGV only to find the end of the command line. */
1328 execute_by_shell = 0; /* in case construct_command_argv sets it */
1330 /* This line has no commands. Go to the next. */
1331 if (job_next_command (child))
1332 start_job_command (child);
1335 /* No more commands. Make sure we're "running"; we might not be if
1336 (e.g.) all commands were skipped due to -n. */
1337 set_command_state (child->file, cs_running);
1338 child->file->update_status = us_success;
1339 notice_finished_file (child->file);
1346 /* Are we going to synchronize this command's output? Do so if either we're
1347 in SYNC_RECURSE mode or this command is not recursive. We'll also check
1348 output_sync separately below in case it changes due to error. */
1349 child->output.syncout = output_sync && (output_sync == OUTPUT_SYNC_RECURSE
1350 || !(flags & COMMANDS_RECURSE));
1352 OUTPUT_SET (&child->output);
1354 if (! child->output.syncout)
1355 /* We don't want to sync this command: to avoid misordered
1356 output ensure any already-synced content is written. */
1357 output_dump (&child->output);
1359 /* Print the command if appropriate. */
1360 if (just_print_flag || ISDB (DB_PRINT)
1361 || (!(flags & COMMANDS_SILENT) && !run_silent))
1362 OS (message, 0, "%s", p);
1364 /* Tell update_goal_chain that a command has been started on behalf of
1365 this target. It is important that this happens here and not in
1366 reap_children (where we used to do it), because reap_children might be
1367 reaping children from a different target. We want this increment to
1368 guaranteedly indicate that a command was started for the dependency
1369 chain (i.e., update_file recursion chain) we are processing. */
1373 /* Optimize an empty command. People use this for timestamp rules,
1374 so avoid forking a useless shell. Do this after we increment
1375 commands_started so make still treats this special case as if it
1376 performed some action (makes a difference as to what messages are
1379 #if !defined(VMS) && !defined(_AMIGA)
1381 #if defined __MSDOS__ || defined (__EMX__)
1382 unixy_shell /* the test is complicated and we already did it */
1384 (argv[0] && is_bourne_compatible_shell (argv[0]))
1386 && (argv[1] && argv[1][0] == '-'
1388 ((argv[1][1] == 'c' && argv[1][2] == '\0')
1390 (argv[1][1] == 'e' && argv[1][2] == 'c' && argv[1][3] == '\0')))
1391 && (argv[2] && argv[2][0] == ':' && argv[2][1] == '\0')
1397 #endif /* !VMS && !_AMIGA */
1399 /* If -n was given, recurse to get the next line in the sequence. */
1401 if (just_print_flag && !(flags & COMMANDS_RECURSE))
1407 /* We're sure we're going to invoke a command: set up the output. */
1410 /* Flush the output streams so they won't have things written twice. */
1415 /* Decide whether to give this child the 'good' standard input
1416 (one that points to the terminal or whatever), or the 'bad' one
1417 that points to the read side of a broken pipe. */
1419 child->good_stdin = !good_stdin_used;
1420 if (child->good_stdin)
1421 good_stdin_used = 1;
1426 /* Set up the environment for the child. */
1427 if (child->environment == 0)
1428 child->environment = target_environment (child->file, child->recursive);
1431 #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
1434 /* start_waiting_job has set CHILD->remote if we can start a remote job. */
1437 int is_remote, used_stdin;
1439 if (start_remote_job (argv, child->environment,
1440 child->good_stdin ? 0 : get_bad_stdin (),
1441 &is_remote, &id, &used_stdin))
1442 /* Don't give up; remote execution may fail for various reasons. If
1443 so, simply run the job locally. */
1447 if (child->good_stdin && !used_stdin)
1449 child->good_stdin = 0;
1450 good_stdin_used = 0;
1452 child->remote = is_remote;
1459 /* Fork the child process. */
1466 child->pid = child_execute_job ((struct childbase *)child, 1, argv);
1470 jobserver_pre_child (flags & COMMANDS_RECURSE);
1472 child->pid = child_execute_job ((struct childbase *)child,
1473 child->good_stdin, argv);
1475 jobserver_post_child (flags & COMMANDS_RECURSE);
1480 #else /* __MSDOS__ or Amiga or WINDOWS32 */
1488 /* We call 'system' to do the job of the SHELL, since stock DOS
1489 shell is too dumb. Our 'system' knows how to handle long
1490 command lines even if pipes/redirection is needed; it will only
1491 call COMMAND.COM when its internal commands are used. */
1492 if (execute_by_shell)
1494 char *cmdline = argv[0];
1495 /* We don't have a way to pass environment to 'system',
1496 so we need to save and restore ours, sigh... */
1497 char **parent_env = environ;
1499 environ = child->environment;
1501 /* If we have a *real* shell, tell 'system' to call
1502 it to do everything for us. */
1505 /* A *real* shell on MSDOS may not support long
1506 command lines the DJGPP way, so we must use 'system'. */
1507 cmdline = argv[2]; /* get past "shell -c" */
1510 dos_command_running = 1;
1511 proc_return = system (cmdline);
1512 environ = parent_env;
1513 execute_by_shell = 0; /* for the next time */
1517 dos_command_running = 1;
1518 proc_return = spawnvpe (P_WAIT, argv[0], argv, child->environment);
1521 /* Need to unblock signals before turning off
1522 dos_command_running, so that child's signals
1523 will be treated as such (see fatal_error_signal). */
1525 dos_command_running = 0;
1527 /* If the child got a signal, dos_status has its
1528 high 8 bits set, so be careful not to alter them. */
1529 if (proc_return == -1)
1532 dos_status |= (proc_return & 0xff);
1534 child->pid = dos_pid++;
1536 #endif /* __MSDOS__ */
1538 amiga_status = MyExecute (argv);
1541 child->pid = amiga_pid++;
1542 if (amiga_batch_file)
1544 amiga_batch_file = 0;
1545 DeleteFile (amiga_bname); /* Ignore errors. */
1555 /* make UNC paths safe for CreateProcess -- backslash format */
1557 if (arg0 && arg0[0] == '/' && arg0[1] == '/')
1558 for ( ; arg0 && *arg0; arg0++)
1562 /* make sure CreateProcess() has Path it needs */
1563 sync_Path_environment ();
1565 /* Divert child output if output_sync in use. */
1566 if (child->output.syncout)
1568 if (child->output.out >= 0)
1569 outfd = child->output.out;
1570 if (child->output.err >= 0)
1571 errfd = child->output.err;
1574 hPID = process_easy (argv, child->environment, outfd, errfd);
1576 if (hPID != INVALID_HANDLE_VALUE)
1577 child->pid = (pid_t) hPID;
1583 _("process_easy() failed to launch process (e=%ld)\n"),
1584 process_last_err (hPID));
1585 for (i = 0; argv[i]; i++)
1586 fprintf (stderr, "%s ", argv[i]);
1587 fprintf (stderr, _("\nCounted %d args in failed launch\n"), i);
1591 #endif /* WINDOWS32 */
1592 #endif /* __MSDOS__ or Amiga or WINDOWS32 */
1594 /* Bump the number of jobs started in this second. */
1595 if (child->pid >= 0)
1598 /* Set the state to running. */
1599 set_command_state (child->file, cs_running);
1601 /* Free the storage used by the child's argument list. */
1609 /* Try to start a child running.
1610 Returns nonzero if the child was started (and maybe finished), or zero if
1611 the load was too high and the child was put on the 'waiting_jobs' chain. */
1614 start_waiting_job (struct child *c)
1616 struct file *f = c->file;
1618 /* If we can start a job remotely, we always want to, and don't care about
1619 the local load average. We record that the job should be started
1620 remotely in C->remote for start_job_command to test. */
1622 c->remote = start_remote_job_p (1);
1624 /* If we are running at least one job already and the load average
1625 is too high, make this one wait. */
1627 && ((job_slots_used > 0 && load_too_high ())
1629 || process_table_full ()
1633 /* Put this child on the chain of children waiting for the load average
1635 set_command_state (f, cs_running);
1636 c->next = waiting_jobs;
1641 /* Start the first command; reap_children will run later command lines. */
1642 start_job_command (c);
1644 switch (f->command_state)
1650 DB (DB_JOBS, (_("Putting child %p (%s) PID %s%s on the chain.\n"),
1651 c, c->file->name, pid2str (c->pid),
1652 c->remote ? _(" (remote)") : ""));
1653 /* One more job slot is in use. */
1655 assert (c->jobslot == 0);
1662 case cs_not_started:
1663 /* All the command lines turned out to be empty. */
1664 f->update_status = us_success;
1668 notice_finished_file (f);
1673 assert (f->command_state == cs_finished);
1680 /* Create a 'struct child' for FILE and start its commands running. */
1683 new_job (struct file *file)
1685 struct commands *cmds = file->cmds;
1690 /* Let any previously decided-upon jobs that are waiting
1691 for the load to go down start before this new one. */
1692 start_waiting_jobs ();
1694 /* Reap any children that might have finished recently. */
1695 reap_children (0, 0);
1697 /* Chop the commands up into lines if they aren't already. */
1698 chop_commands (cmds);
1700 /* Start the command sequence, record it in a new
1701 'struct child', and add that to the chain. */
1703 c = xcalloc (sizeof (struct child));
1704 output_init (&c->output);
1707 c->sh_batch_file = NULL;
1709 /* Cache dontcare flag because file->dontcare can be changed once we
1710 return. Check dontcare inheritance mechanism for details. */
1711 c->dontcare = file->dontcare;
1713 /* Start saving output in case the expansion uses $(info ...) etc. */
1714 OUTPUT_SET (&c->output);
1716 /* Expand the command lines and store the results in LINES. */
1717 lines = xmalloc (cmds->ncommand_lines * sizeof (char *));
1718 for (i = 0; i < cmds->ncommand_lines; ++i)
1720 /* Collapse backslash-newline combinations that are inside variable
1721 or function references. These are left alone by the parser so
1722 that they will appear in the echoing of commands (where they look
1723 nice); and collapsed by construct_command_argv when it tokenizes.
1724 But letting them survive inside function invocations loses because
1725 we don't want the functions to see them as part of the text. */
1727 char *in, *out, *ref;
1729 /* IN points to where in the line we are scanning.
1730 OUT points to where in the line we are writing.
1731 When we collapse a backslash-newline combination,
1732 IN gets ahead of OUT. */
1734 in = out = cmds->command_lines[i];
1735 while ((ref = strchr (in, '$')) != 0)
1737 ++ref; /* Move past the $. */
1740 /* Copy the text between the end of the last chunk
1741 we processed (where IN points) and the new chunk
1742 we are about to process (where REF points). */
1743 memmove (out, in, ref - in);
1745 /* Move both pointers past the boring stuff. */
1749 if (*ref == '(' || *ref == '{')
1751 char openparen = *ref;
1752 char closeparen = openparen == '(' ? ')' : '}';
1757 *out++ = *in++; /* Copy OPENPAREN. */
1759 /* IN now points past the opening paren or brace.
1760 Count parens or braces until it is matched. */
1764 if (*in == closeparen && --count < 0)
1766 else if (*in == '\\' && in[1] == '\n')
1768 /* We have found a backslash-newline inside a
1769 variable or function reference. Eat it and
1770 any following whitespace. */
1773 for (p = in - 1; p > ref && *p == '\\'; --p)
1777 /* There were two or more backslashes, so this is
1778 not really a continuation line. We don't collapse
1779 the quoting backslashes here as is done in
1780 collapse_continuations, because the line will
1781 be collapsed again after expansion. */
1785 /* Skip the backslash, newline, and whitespace. */
1789 /* Discard any preceding whitespace that has
1790 already been written to the output. */
1791 while (out > outref && ISBLANK (out[-1]))
1794 /* Replace it all with a single space. */
1800 if (*in == openparen)
1809 /* There are no more references in this line to worry about.
1810 Copy the remaining uninteresting text to the output. */
1812 memmove (out, in, strlen (in) + 1);
1814 /* Finally, expand the line. */
1815 cmds->fileinfo.offset = i;
1816 lines[i] = allocated_variable_expand_for_file (cmds->command_lines[i],
1820 cmds->fileinfo.offset = 0;
1821 c->command_lines = lines;
1823 /* Fetch the first command line to be run. */
1824 job_next_command (c);
1826 /* Wait for a job slot to be freed up. If we allow an infinite number
1827 don't bother; also job_slots will == 0 if we're using the jobserver. */
1830 while (job_slots_used == job_slots)
1831 reap_children (1, 0);
1833 #ifdef MAKE_JOBSERVER
1834 /* If we are controlling multiple jobs make sure we have a token before
1835 starting the child. */
1837 /* This can be inefficient. There's a decent chance that this job won't
1838 actually have to run any subprocesses: the command script may be empty
1839 or otherwise optimized away. It would be nice if we could defer
1840 obtaining a token until just before we need it, in start_job_command.
1841 To do that we'd need to keep track of whether we'd already obtained a
1842 token (since start_job_command is called for each line of the job, not
1843 just once). Also more thought needs to go into the entire algorithm;
1844 this is where the old parallel job code waits, so... */
1846 else if (jobserver_enabled ())
1851 DB (DB_JOBS, ("Need a job token; we %shave children\n",
1852 children ? "" : "don't "));
1854 /* If we don't already have a job started, use our "free" token. */
1855 if (!jobserver_tokens)
1858 /* Prepare for jobserver token acquisition. */
1859 jobserver_pre_acquire ();
1861 /* Reap anything that's currently waiting. */
1862 reap_children (0, 0);
1864 /* Kick off any jobs we have waiting for an opportunity that
1865 can run now (i.e., waiting for load). */
1866 start_waiting_jobs ();
1868 /* If our "free" slot is available, use it; we don't need a token. */
1869 if (!jobserver_tokens)
1872 /* There must be at least one child already, or we have no business
1873 waiting for a token. */
1875 O (fatal, NILF, "INTERNAL: no children as we go to sleep on read");
1878 got_token = jobserver_acquire (waiting_jobs != NULL);
1880 /* If we got one, we're done here. */
1883 DB (DB_JOBS, (_("Obtained token for child %p (%s).\n"),
1893 Use message here so that changes to working directories are logged. */
1896 char *newer = allocated_variable_expand_for_file ("$?", c->file);
1899 if (! cmds->fileinfo.filenm)
1900 nm = _("<builtin>");
1903 char *n = alloca (strlen (cmds->fileinfo.filenm) + 1 + 11 + 1);
1904 sprintf (n, "%s:%lu", cmds->fileinfo.filenm, cmds->fileinfo.lineno);
1909 _("%s: update target '%s' due to: %s"), nm, c->file->name,
1910 newer[0] == '\0' ? _("target does not exist") : newer);
1915 /* The job is now primed. Start it running.
1916 (This will notice if there is in fact no recipe.) */
1917 start_waiting_job (c);
1919 if (job_slots == 1 || not_parallel)
1920 /* Since there is only one job slot, make things run linearly.
1921 Wait for the child to die, setting the state to 'cs_finished'. */
1922 while (file->command_state == cs_running)
1923 reap_children (1, 0);
1929 /* Move CHILD's pointers to the next command for it to execute.
1930 Returns nonzero if there is another command. */
1933 job_next_command (struct child *child)
1935 while (child->command_ptr == 0 || *child->command_ptr == '\0')
1937 /* There are no more lines in the expansion of this line. */
1938 if (child->command_line == child->file->cmds->ncommand_lines)
1940 /* There are no more lines to be expanded. */
1941 child->command_ptr = 0;
1942 child->file->cmds->fileinfo.offset = 0;
1946 /* Get the next line to run. */
1947 child->command_ptr = child->command_lines[child->command_line++];
1950 child->file->cmds->fileinfo.offset = child->command_line - 1;
1954 /* Determine if the load average on the system is too high to start a new job.
1956 On systems which provide /proc/loadavg (e.g., Linux), we use an idea
1957 provided by Sven C. Dack <sven.c.dack@sky.com>: retrieve the current number
1958 of runnable processes, if it's greater than the requested load we don't
1959 allow another job to start. We allow a job to start with equal processes
1960 since one of those will be for make itself, which will then pause waiting
1963 If /proc/loadavg is not available for some reason, we obtain the system
1964 load average and compare that.
1966 The system load average is only recomputed once every N (N>=1) seconds.
1967 However, a very parallel make can easily start tens or even hundreds of
1968 jobs in a second, which brings the system to its knees for a while until
1969 that first batch of jobs clears out.
1971 To avoid this we use a weighted algorithm to try to account for jobs which
1972 have been started since the last second, and guess what the load average
1973 would be now if it were computed.
1975 This algorithm was provided by Thomas Riedl <thomas.riedl@siemens.com>,
1976 based on load average being recomputed once per second, which is
1977 (apparently) how Solaris operates. Linux recomputes only once every 5
1978 seconds, but Linux is handled by the /proc/loadavg algorithm above.
1982 ! calculate something load-oid and add to the observed sys.load,
1983 ! so that latter can catch up:
1984 ! - every job started increases jobctr;
1985 ! - every dying job decreases a positive jobctr;
1986 ! - the jobctr value gets zeroed every change of seconds,
1987 ! after its value*weight_b is stored into the 'backlog' value last_sec
1988 ! - weight_a times the sum of jobctr and last_sec gets
1989 ! added to the observed sys.load.
1991 ! The two weights have been tried out on 24 and 48 proc. Sun Solaris-9
1992 ! machines, using a several-thousand-jobs-mix of cpp, cc, cxx and smallish
1993 ! sub-shelled commands (rm, echo, sed...) for tests.
1994 ! lowering the 'direct influence' factor weight_a (e.g. to 0.1)
1995 ! resulted in significant excession of the load limit, raising it
1996 ! (e.g. to 0.5) took bad to small, fast-executing jobs and didn't
1997 ! reach the limit in most test cases.
1999 ! lowering the 'history influence' weight_b (e.g. to 0.1) resulted in
2000 ! exceeding the limit for longer-running stuff (compile jobs in
2001 ! the .5 to 1.5 sec. range),raising it (e.g. to 0.5) overrepresented
2002 ! small jobs' effects.
2006 #define LOAD_WEIGHT_A 0.25
2007 #define LOAD_WEIGHT_B 0.25
2010 load_too_high (void)
2012 #if defined(__MSDOS__) || defined(VMS) || defined(_AMIGA) || defined(__riscos__)
2015 static double last_sec;
2016 static time_t last_now;
2017 static int proc_fd = -2;
2023 /* sub_proc.c is limited in the number of objects it can wait for. */
2024 if (process_table_full ())
2028 if (max_load_average < 0)
2031 /* If we haven't tried to open /proc/loadavg, try now. */
2032 #define LOADAVG "/proc/loadavg"
2035 EINTRLOOP (proc_fd, open (LOADAVG, O_RDONLY));
2037 DB (DB_JOBS, ("Using system load detection method.\n"));
2040 DB (DB_JOBS, ("Using " LOADAVG " load detection method.\n"));
2041 fd_noinherit (proc_fd);
2045 /* Try to read /proc/loadavg if we managed to open it. */
2050 EINTRLOOP (r, lseek (proc_fd, 0, SEEK_SET));
2053 #define PROC_LOADAVG_SIZE 64
2054 char avg[PROC_LOADAVG_SIZE+1];
2056 EINTRLOOP (r, read (proc_fd, avg, PROC_LOADAVG_SIZE));
2061 /* The syntax of /proc/loadavg is:
2062 <1m> <5m> <15m> <running>/<total> <pid>
2063 The load is considered too high if there are more jobs
2064 running than the requested average. */
2067 p = strchr (avg, ' ');
2069 p = strchr (p+1, ' ');
2071 p = strchr (p+1, ' ');
2073 if (p && ISDIGIT(p[1]))
2075 unsigned int cnt = make_toui (p+1, NULL);
2076 DB (DB_JOBS, ("Running: system = %u / make = %u (max requested = %f)\n",
2077 cnt, job_slots_used, max_load_average));
2078 return (double)cnt > max_load_average;
2081 DB (DB_JOBS, ("Failed to parse " LOADAVG ": %s\n", avg));
2085 /* If we got here, something went wrong. Give up on this method. */
2087 DB (DB_JOBS, ("Failed to read " LOADAVG ": %s\n", strerror (errno)));
2093 /* Find the real system load average. */
2095 if (getloadavg (&load, 1) != 1)
2097 static int lossage = -1;
2098 /* Complain only once for the same error. */
2099 if (lossage == -1 || errno != lossage)
2102 /* An errno value of zero means getloadavg is just unsupported. */
2104 _("cannot enforce load limits on this operating system"));
2106 perror_with_name (_("cannot enforce load limit: "), "getloadavg");
2112 /* If we're in a new second zero the counter and correct the backlog
2113 value. Only keep the backlog for one extra second; after that it's 0. */
2117 if (last_now == now - 1)
2118 last_sec = LOAD_WEIGHT_B * job_counter;
2126 /* Try to guess what the load would be right now. */
2127 guess = load + (LOAD_WEIGHT_A * (job_counter + last_sec));
2129 DB (DB_JOBS, ("Estimated system load = %f (actual = %f) (max requested = %f)\n",
2130 guess, load, max_load_average));
2132 return guess >= max_load_average;
2136 /* Start jobs that are waiting for the load to be lower. */
2139 start_waiting_jobs (void)
2143 if (waiting_jobs == 0)
2148 /* Check for recently deceased descendants. */
2149 reap_children (0, 0);
2151 /* Take a job off the waiting list. */
2153 waiting_jobs = job->next;
2155 /* Try to start that job. We break out of the loop as soon
2156 as start_waiting_job puts one back on the waiting list. */
2158 while (start_waiting_job (job) && waiting_jobs != 0);
2165 /* EMX: Start a child process. This function returns the new pid. */
2166 # if defined __EMX__
2168 child_execute_job (struct childbase *child, int good_stdin, char **argv)
2171 int fdin = good_stdin ? FD_STDIN : get_bad_stdin ();
2172 int fdout = FD_STDOUT;
2173 int fderr = FD_STDERR;
2175 int save_fdout = -1;
2176 int save_fderr = -1;
2178 /* Divert child output if we want to capture output. */
2179 if (child->output.syncout)
2181 if (child->output.out >= 0)
2182 fdout = child->output.out;
2183 if (child->output.err >= 0)
2184 fderr = child->output.err;
2187 /* For each FD which needs to be redirected first make a dup of the standard
2188 FD to save and mark it close on exec so our child won't see it. Then
2189 dup2() the standard FD to the redirect FD, and also mark the redirect FD
2190 as close on exec. */
2191 if (fdin != FD_STDIN)
2193 save_fdin = dup (FD_STDIN);
2195 O (fatal, NILF, _("no more file handles: could not duplicate stdin"));
2196 fd_noinherit (save_fdin);
2198 dup2 (fdin, FD_STDIN);
2199 fd_noinherit (fdin);
2202 if (fdout != FD_STDOUT)
2204 save_fdout = dup (FD_STDOUT);
2207 _("no more file handles: could not duplicate stdout"));
2208 fd_noinherit (save_fdout);
2210 dup2 (fdout, FD_STDOUT);
2211 fd_noinherit (fdout);
2214 if (fderr != FD_STDERR)
2218 save_fderr = dup (FD_STDERR);
2221 _("no more file handles: could not duplicate stderr"));
2222 fd_noinherit (save_fderr);
2225 dup2 (fderr, FD_STDERR);
2226 fd_noinherit (fderr);
2229 /* Run the command. */
2230 pid = exec_command (argv, child->environment);
2232 /* Restore stdout/stdin/stderr of the parent and close temporary FDs. */
2235 if (dup2 (save_fdin, FD_STDIN) != FD_STDIN)
2236 O (fatal, NILF, _("Could not restore stdin"));
2241 if (save_fdout >= 0)
2243 if (dup2 (save_fdout, FD_STDOUT) != FD_STDOUT)
2244 O (fatal, NILF, _("Could not restore stdout"));
2249 if (save_fderr >= 0)
2251 if (dup2 (save_fderr, FD_STDERR) != FD_STDERR)
2252 O (fatal, NILF, _("Could not restore stderr"));
2258 OSS (error, NILF, "%s: %s", argv[0], strerror (errno));
2263 #elif !defined (_AMIGA) && !defined (__MSDOS__) && !defined (VMS)
2266 Create a child process executing the command in ARGV.
2267 Returns the PID or -1. */
2269 child_execute_job (struct childbase *child, int good_stdin, char **argv)
2271 const int fdin = good_stdin ? FD_STDIN : get_bad_stdin ();
2272 int fdout = FD_STDOUT;
2273 int fderr = FD_STDERR;
2276 #if defined(USE_POSIX_SPAWN)
2278 posix_spawnattr_t attr;
2279 posix_spawn_file_actions_t fa;
2283 /* Divert child output if we want to capture it. */
2284 if (child->output.syncout)
2286 if (child->output.out >= 0)
2287 fdout = child->output.out;
2288 if (child->output.err >= 0)
2289 fderr = child->output.err;
2292 #if !defined(USE_POSIX_SPAWN)
2295 /* The child may clobber environ so remember ours and restore it. */
2296 char **parent_env = environ;
2300 environ = parent_env;
2305 /* We are the child. */
2306 unblock_all_sigs ();
2308 #ifdef SET_STACK_SIZE
2309 /* Reset limits, if necessary. */
2310 if (stack_limit.rlim_cur)
2311 setrlimit (RLIMIT_STACK, &stack_limit);
2314 /* For any redirected FD, dup2() it to the standard FD.
2315 They are all marked close-on-exec already. */
2316 if (fdin >= 0 && fdin != FD_STDIN)
2317 EINTRLOOP (r, dup2 (fdin, FD_STDIN));
2318 if (fdout != FD_STDOUT)
2319 EINTRLOOP (r, dup2 (fdout, FD_STDOUT));
2320 if (fderr != FD_STDERR)
2321 EINTRLOOP (r, dup2 (fderr, FD_STDERR));
2323 /* Run the command. */
2324 exec_command (argv, child->environment);
2327 #else /* USE_POSIX_SPAWN */
2329 if ((r = posix_spawnattr_init (&attr)) != 0)
2332 if ((r = posix_spawn_file_actions_init (&fa)) != 0)
2334 posix_spawnattr_destroy (&attr);
2338 /* Unblock all signals. */
2339 #ifdef HAVE_POSIX_SPAWNATTR_SETSIGMASK
2342 sigemptyset (&mask);
2343 r = posix_spawnattr_setsigmask (&attr, &mask);
2346 flags |= POSIX_SPAWN_SETSIGMASK;
2348 #endif /* have posix_spawnattr_setsigmask() */
2350 /* USEVFORK can give significant speedup on systems where it's available. */
2351 #ifdef POSIX_SPAWN_USEVFORK
2352 flags |= POSIX_SPAWN_USEVFORK;
2355 /* For any redirected FD, dup2() it to the standard FD.
2356 They are all marked close-on-exec already. */
2357 if (fdin >= 0 && fdin != FD_STDIN)
2358 if ((r = posix_spawn_file_actions_adddup2 (&fa, fdin, FD_STDIN)) != 0)
2360 if (fdout != FD_STDOUT)
2361 if ((r = posix_spawn_file_actions_adddup2 (&fa, fdout, FD_STDOUT)) != 0)
2363 if (fderr != FD_STDERR)
2364 if ((r = posix_spawn_file_actions_adddup2 (&fa, fderr, FD_STDERR)) != 0)
2367 /* We can't use the POSIX_SPAWN_RESETIDS flag: when make is invoked under
2368 restrictive environments like unshare it will fail with EINVAL. */
2370 /* Apply the spawn flags. */
2371 if ((r = posix_spawnattr_setflags (&attr, flags)) != 0)
2374 /* Look up the program on the child's PATH, if needed. */
2376 const char *p = NULL;
2379 for (pp = child->environment; *pp != NULL; ++pp)
2380 if ((*pp)[0] == 'P' && (*pp)[1] == 'A' && (*pp)[2] == 'T'
2381 && (*pp)[3] == 'H' &&(*pp)[4] == '=')
2387 /* execvp() will use a default PATH if none is set; emulate that. */
2390 size_t l = confstr (_CS_PATH, NULL, 0);
2393 char *dp = alloca (l);
2394 confstr (_CS_PATH, dp, l);
2399 cmd = (char *)find_in_given_path (argv[0], p, NULL, 0);
2408 /* Start the program. */
2409 while ((r = posix_spawn (&pid, cmd, &fa, &attr, argv,
2410 child->environment)) == EINTR)
2413 /* posix_spawn() doesn't provide sh fallback like exec() does; implement
2414 it here. POSIX doesn't specify the path to sh so use the default. */
2422 for (pp = argv; *pp != NULL; ++pp)
2425 nargv = xmalloc (sizeof (char *) * (l + 3));
2426 nargv[0] = (char *)default_shell;
2428 memcpy (&nargv[2], &argv[1], sizeof (char *) * l);
2430 while ((r = posix_spawn (&pid, nargv[0], &fa, &attr, nargv,
2431 child->environment)) == EINTR)
2439 /* Spawn succeeded but may fail later: remember the command. */
2440 free (child->cmd_name);
2442 child->cmd_name = cmd;
2444 child->cmd_name = xstrdup(cmd);
2448 posix_spawn_file_actions_destroy (&fa);
2449 posix_spawnattr_destroy (&attr);
2455 #endif /* USE_POSIX_SPAWN */
2458 OSS (error, NILF, "%s: %s", argv[0], strerror (r));
2462 #endif /* !AMIGA && !__MSDOS__ && !VMS */
2463 #endif /* !WINDOWS32 */
2466 /* Replace the current process with one running the command in ARGV,
2467 with environment ENVP. This function does not return. */
2470 exec_command (char **argv, char **envp)
2473 /* to work around a problem with signals and execve: ignore them */
2475 signal (SIGCHLD,SIG_IGN);
2477 /* Run the program. */
2478 execve (argv[0], argv, envp);
2479 OSS (error, NILF, "%s: %s", argv[0], strerror (errno));
2480 _exit (EXIT_FAILURE);
2485 int exit_code = EXIT_FAILURE;
2487 /* make sure CreateProcess() has Path it needs */
2488 sync_Path_environment ();
2490 /* launch command */
2491 hPID = process_easy (argv, envp, -1, -1);
2493 /* make sure launch ok */
2494 if (hPID == INVALID_HANDLE_VALUE)
2497 fprintf (stderr, _("process_easy() failed to launch process (e=%ld)\n"),
2498 process_last_err (hPID));
2499 for (i = 0; argv[i]; i++)
2500 fprintf (stderr, "%s ", argv[i]);
2501 fprintf (stderr, _("\nCounted %d args in failed launch\n"), i);
2502 exit (EXIT_FAILURE);
2505 /* wait and reap last child */
2506 hWaitPID = process_wait_for_any (1, 0);
2509 /* was an error found on this process? */
2510 int err = process_last_err (hWaitPID);
2513 exit_code = process_exit_code (hWaitPID);
2516 fprintf (stderr, "make (e=%d, rc=%d): %s",
2517 err, exit_code, map_windows32_error_to_string (err));
2519 /* cleanup process */
2520 process_cleanup (hWaitPID);
2522 /* expect to find only last pid, warn about other pids reaped */
2523 if (hWaitPID == hPID)
2527 char *pidstr = xstrdup (pid2str ((pid_t)hWaitPID));
2530 _("make reaped child pid %s, still waiting for pid %s\n"),
2531 pidstr, pid2str ((pid_t)hPID));
2536 /* Use the child's exit code as our exit code */
2539 #else /* !WINDOWS32 */
2544 /* Run the program. */
2545 pid = spawnvpe (P_NOWAIT, argv[0], argv, envp);
2549 /* the file might have a strange shell extension */
2550 if (errno == ENOENT)
2555 /* Run the program. Don't use execvpe() as we want the search for argv[0]
2556 to use the new PATH, but execvpe() searches before resetting PATH. */
2558 execvp (argv[0], argv);
2560 # endif /* !__EMX__ */
2565 OSS (error, NILF, "%s: %s", argv[0], strerror (errno));
2569 /* The file was not a program. Try it as a shell script. */
2576 /* Do not use $SHELL from the environment */
2577 struct variable *p = lookup_variable ("SHELL", 5);
2583 shell = getenv ("SHELL");
2586 shell = default_shell;
2589 while (argv[argc] != 0)
2597 new_argv = alloca ((1 + argc + 1) * sizeof (char *));
2598 new_argv[0] = (char *)shell;
2609 new_argv[i] = argv[0];
2612 new_argv[i + argc] = argv[argc];
2617 pid = spawnvpe (P_NOWAIT, shell, new_argv, envp);
2621 execvp (shell, new_argv);
2623 OSS (error, NILF, "%s: %s", new_argv[0], strerror (errno));
2629 /* this nasty error was driving me nuts :-( */
2630 O (error, NILF, _("spawnvpe: environment space might be exhausted"));
2635 OSS (error, NILF, "%s: %s", argv[0], strerror (errno));
2640 #endif /* !WINDOWS32 */
2643 #else /* On Amiga */
2645 exec_command (char **argv)
2650 void clean_tmp (void)
2652 DeleteFile (amiga_bname);
2655 #endif /* On Amiga */
2658 /* Figure out the argument list necessary to run LINE as a command. Try to
2659 avoid using a shell. This routine handles only ' quoting, and " quoting
2660 when no backslash, $ or ' characters are seen in the quotes. Starting
2661 quotes may be escaped with a backslash. If any of the characters in
2662 sh_chars is seen, or any of the builtin commands listed in sh_cmds is the
2663 first word of a line, the shell is used.
2665 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
2666 If *RESTP is NULL, newlines will be ignored.
2668 SHELL is the shell to use, or nil to use the default shell.
2669 IFS is the value of $IFS, or nil (meaning the default).
2671 FLAGS is the value of lines_flags for this command line. It is used in the
2672 WINDOWS32 port to check whether + or $(MAKE) were found in this command
2673 line, in which case the effect of just_print_flag is overridden.
2675 The returned value is either NULL if the line was empty, or else a pointer
2676 to an array of strings. The fist pointer points to the memory used by all
2677 the strings, so to free you free the 0'th element then the returned pointer
2678 (see the FREE_ARGV macro). */
2681 construct_command_argv_internal (char *line, char **restp, const char *shell,
2682 const char *shellflags, const char *ifs,
2683 int flags, char **batch_filename UNUSED)
2686 /* MSDOS supports both the stock DOS shell and ports of Unixy shells.
2687 We call 'system' for anything that requires ''slow'' processing,
2688 because DOS shells are too dumb. When $SHELL points to a real
2689 (unix-style) shell, 'system' just calls it to do everything. When
2690 $SHELL points to a DOS shell, 'system' does most of the work
2691 internally, calling the shell only for its internal commands.
2692 However, it looks on the $PATH first, so you can e.g. have an
2693 external command named 'mkdir'.
2695 Since we call 'system', certain characters and commands below are
2696 actually not specific to COMMAND.COM, but to the DJGPP implementation
2697 of 'system'. In particular:
2699 The shell wildcard characters are in DOS_CHARS because they will
2700 not be expanded if we call the child via 'spawnXX'.
2702 The ';' is in DOS_CHARS, because our 'system' knows how to run
2703 multiple commands on a single line.
2705 DOS_CHARS also include characters special to 4DOS/NDOS, so we
2706 won't have to tell one from another and have one more set of
2707 commands and special characters. */
2708 static const char *sh_chars_dos = "*?[];|<>%^&()";
2709 static const char *sh_cmds_dos[] =
2710 { "break", "call", "cd", "chcp", "chdir", "cls", "copy", "ctty", "date",
2711 "del", "dir", "echo", "erase", "exit", "for", "goto", "if", "md",
2712 "mkdir", "path", "pause", "prompt", "rd", "rmdir", "rem", "ren",
2713 "rename", "set", "shift", "time", "type", "ver", "verify", "vol", ":",
2716 static const char *sh_chars_sh = "#;\"*?[]&|<>(){}$`^";
2717 static const char *sh_cmds_sh[] =
2718 { "cd", "echo", "eval", "exec", "exit", "login", "logout", "set", "umask",
2719 "wait", "while", "for", "case", "if", ":", ".", "break", "continue",
2720 "export", "read", "readonly", "shift", "times", "trap", "switch",
2721 "unset", "ulimit", "command", 0 };
2723 const char *sh_chars;
2724 const char **sh_cmds;
2726 #elif defined (__EMX__)
2727 static const char *sh_chars_dos = "*?[];|<>%^&()";
2728 static const char *sh_cmds_dos[] =
2729 { "break", "call", "cd", "chcp", "chdir", "cls", "copy", "ctty", "date",
2730 "del", "dir", "echo", "erase", "exit", "for", "goto", "if", "md",
2731 "mkdir", "path", "pause", "prompt", "rd", "rmdir", "rem", "ren",
2732 "rename", "set", "shift", "time", "type", "ver", "verify", "vol", ":",
2735 static const char *sh_chars_os2 = "*?[];|<>%^()\"'&";
2736 static const char *sh_cmds_os2[] =
2737 { "call", "cd", "chcp", "chdir", "cls", "copy", "date", "del", "detach",
2738 "dir", "echo", "endlocal", "erase", "exit", "for", "goto", "if", "keys",
2739 "md", "mkdir", "move", "path", "pause", "prompt", "rd", "rem", "ren",
2740 "rename", "rmdir", "set", "setlocal", "shift", "start", "time", "type",
2741 "ver", "verify", "vol", ":", 0 };
2743 static const char *sh_chars_sh = "#;\"*?[]&|<>(){}$`^~'";
2744 static const char *sh_cmds_sh[] =
2745 { "echo", "cd", "eval", "exec", "exit", "login", "logout", "set", "umask",
2746 "wait", "while", "for", "case", "if", ":", ".", "break", "continue",
2747 "export", "read", "readonly", "shift", "times", "trap", "switch",
2748 "unset", "command", 0 };
2750 const char *sh_chars;
2751 const char **sh_cmds;
2753 #elif defined (_AMIGA)
2754 static const char *sh_chars = "#;\"|<>()?*$`";
2755 static const char *sh_cmds[] =
2756 { "cd", "eval", "if", "delete", "echo", "copy", "rename", "set", "setenv",
2757 "date", "makedir", "skip", "else", "endif", "path", "prompt", "unset",
2758 "unsetenv", "version", "command", 0 };
2760 #elif defined (WINDOWS32)
2761 /* We used to have a double quote (") in sh_chars_dos[] below, but
2762 that caused any command line with quoted file names be run
2763 through a temporary batch file, which introduces command-line
2764 limit of 4K characters imposed by cmd.exe. Since CreateProcess
2765 can handle quoted file names just fine, removing the quote lifts
2766 the limit from a very frequent use case, because using quoted
2767 file names is commonplace on MS-Windows. */
2768 static const char *sh_chars_dos = "|&<>";
2769 static const char *sh_cmds_dos[] =
2770 { "assoc", "break", "call", "cd", "chcp", "chdir", "cls", "color", "copy",
2771 "ctty", "date", "del", "dir", "echo", "echo.", "endlocal", "erase",
2772 "exit", "for", "ftype", "goto", "if", "if", "md", "mkdir", "move",
2773 "path", "pause", "prompt", "rd", "rem", "ren", "rename", "rmdir",
2774 "set", "setlocal", "shift", "time", "title", "type", "ver", "verify",
2777 static const char *sh_chars_sh = "#;\"*?[]&|<>(){}$`^";
2778 static const char *sh_cmds_sh[] =
2779 { "cd", "eval", "exec", "exit", "login", "logout", "set", "umask", "wait",
2780 "while", "for", "case", "if", ":", ".", "break", "continue", "export",
2781 "read", "readonly", "shift", "times", "trap", "switch", "test", "command",
2782 #ifdef BATCH_MODE_ONLY_SHELL
2787 const char *sh_chars;
2788 const char **sh_cmds;
2789 #elif defined(__riscos__)
2790 static const char *sh_chars = "";
2791 static const char *sh_cmds[] = { 0 };
2792 #else /* must be UNIX-ish */
2793 static const char *sh_chars = "#;\"*?[]&|<>(){}$`^~!";
2794 static const char *sh_cmds[] =
2795 { ".", ":", "alias", "bg", "break", "case", "cd", "command", "continue",
2796 "eval", "exec", "exit", "export", "fc", "fg", "for", "getopts", "hash",
2797 "if", "jobs", "login", "logout", "read", "readonly", "return", "set",
2798 "shift", "test", "times", "trap", "type", "ulimit", "umask", "unalias",
2799 "unset", "wait", "while", 0 };
2801 # ifdef HAVE_DOS_PATHS
2802 /* This is required if the MSYS/Cygwin ports (which do not define
2803 WINDOWS32) are compiled with HAVE_DOS_PATHS defined, which uses
2804 sh_chars_sh directly (see below). The value must be identical
2805 to that of sh_chars immediately above. */
2806 static const char *sh_chars_sh = "#;\"*?[]&|<>(){}$`^~!";
2807 # endif /* HAVE_DOS_PATHS */
2817 int instring, word_has_equals, seen_nonequals, last_argument_was_empty;
2818 char **new_argv = 0;
2825 sh_cmds = sh_cmds_dos;
2826 sh_chars = sh_chars_dos;
2830 sh_cmds = sh_cmds_sh;
2831 sh_chars = sh_chars_sh;
2833 #endif /* WINDOWS32 */
2838 /* Make sure not to bother processing an empty line but stop at newline. */
2839 while (ISBLANK (*line))
2844 if (shellflags == 0)
2845 shellflags = posix_pedantic ? "-ec" : "-c";
2847 /* See if it is safe to parse commands internally. */
2849 shell = default_shell;
2851 else if (strcmp (shell, default_shell))
2853 char *s1 = _fullpath (NULL, shell, 0);
2854 char *s2 = _fullpath (NULL, default_shell, 0);
2856 slow_flag = strcmp ((s1 ? s1 : ""), (s2 ? s2 : ""));
2863 #else /* not WINDOWS32 */
2864 #if defined (__MSDOS__) || defined (__EMX__)
2865 else if (strcasecmp (shell, default_shell))
2867 extern int _is_unixy_shell (const char *_path);
2869 DB (DB_BASIC, (_("$SHELL changed (was '%s', now '%s')\n"),
2870 default_shell, shell));
2871 unixy_shell = _is_unixy_shell (shell);
2872 /* we must allocate a copy of shell: construct_command_argv() will free
2873 * shell after this function returns. */
2874 default_shell = xstrdup (shell);
2878 sh_chars = sh_chars_sh;
2879 sh_cmds = sh_cmds_sh;
2883 sh_chars = sh_chars_dos;
2884 sh_cmds = sh_cmds_dos;
2886 if (_osmode == OS2_MODE)
2888 sh_chars = sh_chars_os2;
2889 sh_cmds = sh_cmds_os2;
2893 #else /* !__MSDOS__ */
2894 else if (strcmp (shell, default_shell))
2896 #endif /* !__MSDOS__ && !__EMX__ */
2897 #endif /* not WINDOWS32 */
2900 for (cap = ifs; *cap != '\0'; ++cap)
2901 if (*cap != ' ' && *cap != '\t' && *cap != '\n')
2905 if (shellflags[0] != '-'
2906 || ((shellflags[1] != 'c' || shellflags[2] != '\0')
2907 && (shellflags[1] != 'e' || shellflags[2] != 'c' || shellflags[3] != '\0')))
2910 i = strlen (line) + 1;
2912 /* More than 1 arg per character is impossible. */
2913 new_argv = xmalloc (i * sizeof (char *));
2915 /* All the args can fit in a buffer as big as LINE is. */
2916 ap = new_argv[0] = argstr = xmalloc (i);
2921 /* I is how many complete arguments have been found. */
2923 instring = word_has_equals = seen_nonequals = last_argument_was_empty = 0;
2924 for (p = line; *p != '\0'; ++p)
2930 /* Inside a string, just copy any char except a closing quote
2931 or a backslash-newline combination. */
2935 if (ap == new_argv[0] || *(ap-1) == '\0')
2936 last_argument_was_empty = 1;
2938 else if (*p == '\\' && p[1] == '\n')
2940 /* Backslash-newline is handled differently depending on what
2941 kind of string we're in: inside single-quoted strings you
2942 keep them; in double-quoted strings they disappear. For
2943 DOS/Windows/OS2, if we don't have a POSIX shell, we keep the
2944 pre-POSIX behavior of removing the backslash-newline. */
2946 #if defined (__MSDOS__) || defined (__EMX__) || defined (WINDOWS32)
2957 else if (*p == '\n' && restp != NULL)
2959 /* End of the command line. */
2963 /* Backslash, $, and ` are special inside double quotes.
2964 If we see any of those, punt.
2965 But on MSDOS, if we use COMMAND.COM, double and single
2966 quotes have the same effect. */
2967 else if (instring == '"' && strchr ("\\$`", *p) != 0 && unixy_shell)
2970 /* Quoted wildcard characters must be passed quoted to the
2971 command, so give up the fast route. */
2972 else if (instring == '"' && strchr ("*?", *p) != 0 && !unixy_shell)
2974 else if (instring == '"' && strncmp (p, "\\\"", 2) == 0)
2980 else if (strchr (sh_chars, *p) != 0)
2981 /* Not inside a string, but it's a special char. */
2983 else if (one_shell && *p == '\n')
2984 /* In .ONESHELL mode \n is a separator like ; or && */
2987 else if (*p == '.' && p[1] == '.' && p[2] == '.' && p[3] != '.')
2988 /* '...' is a wildcard in DJGPP. */
2992 /* Not a special char. */
2996 /* Equals is a special character in leading words before the
2997 first word with no equals sign in it. This is not the case
2998 with sh -k, but we never get here when using nonstandard
3000 if (! seen_nonequals && unixy_shell)
3002 word_has_equals = 1;
3007 /* Backslash-newline has special case handling, ref POSIX.
3008 We're in the fastpath, so emulate what the shell would do. */
3011 /* Throw out the backslash and newline. */
3014 /* At the beginning of the argument, skip any whitespace other
3015 than newline before the start of the next word. */
3016 if (ap == new_argv[i])
3017 while (ISBLANK (p[1]))
3021 /* Backslash before whitespace is not special if our shell
3023 else if (ISSPACE (p[1]) && !unixy_shell)
3029 else if (p[1] != '\0')
3031 #ifdef HAVE_DOS_PATHS
3032 /* Only remove backslashes before characters special to Unixy
3033 shells. All other backslashes are copied verbatim, since
3034 they are probably DOS-style directory separators. This
3035 still leaves a small window for problems, but at least it
3036 should work for the vast majority of naive users. */
3039 /* A dot is only special as part of the "..."
3041 if (strneq (p + 1, ".\\.\\.", 5))
3049 if (p[1] != '\\' && p[1] != '\'' && !ISSPACE (p[1])
3050 && strchr (sh_chars_sh, p[1]) == 0)
3051 /* back up one notch, to copy the backslash */
3053 #endif /* HAVE_DOS_PATHS */
3055 /* Copy and skip the following char. */
3068 /* End of the command line. */
3073 /* Newlines are not special. */
3079 /* We have the end of an argument.
3080 Terminate the text of the argument. */
3083 last_argument_was_empty = 0;
3085 /* Update SEEN_NONEQUALS, which tells us if every word
3086 heretofore has contained an '='. */
3087 seen_nonequals |= ! word_has_equals;
3088 if (word_has_equals && ! seen_nonequals)
3089 /* An '=' in a word before the first
3090 word without one is magical. */
3092 word_has_equals = 0; /* Prepare for the next word. */
3094 /* If this argument is the command name,
3095 see if it is a built-in shell command.
3096 If so, have the shell handle it. */
3100 for (j = 0; sh_cmds[j] != 0; ++j)
3102 if (streq (sh_cmds[j], new_argv[0]))
3104 #if defined(__EMX__) || defined(WINDOWS32)
3105 /* Non-Unix shells are case insensitive. */
3107 && strcasecmp (sh_cmds[j], new_argv[0]) == 0)
3113 /* Skip whitespace chars, but not newlines. */
3114 while (ISBLANK (p[1]))
3126 /* Let the shell deal with an unterminated quote. */
3129 /* Terminate the last argument and the argument list. */
3132 if (new_argv[i][0] != '\0' || last_argument_was_empty)
3139 for (j = 0; sh_cmds[j] != 0; ++j)
3140 if (streq (sh_cmds[j], new_argv[0]))
3144 if (new_argv[0] == 0)
3146 /* Line was empty. */
3155 /* We must use the shell. */
3159 /* Free the old argument list we were working on. */
3165 execute_by_shell = 1; /* actually, call 'system' if shell isn't unixy */
3174 buffer = xmalloc (strlen (line)+1);
3177 for (dptr=buffer; *ptr; )
3179 if (*ptr == '\\' && ptr[1] == '\n')
3181 else if (*ptr == '@') /* Kludge: multiline commands */
3191 new_argv = xmalloc (2 * sizeof (char *));
3192 new_argv[0] = buffer;
3195 #else /* Not Amiga */
3198 * Not eating this whitespace caused things like
3202 * which gave the shell fits. I think we have to eat
3203 * whitespace here, but this code should be considered
3204 * suspicious if things start failing....
3207 /* Make sure not to bother processing an empty line. */
3211 #endif /* WINDOWS32 */
3214 /* SHELL may be a multi-word command. Construct a command line
3215 "$(SHELL) $(.SHELLFLAGS) LINE", with all special chars in LINE escaped.
3216 Then recurse, expanding this command line to get the final
3220 size_t shell_len = strlen (shell);
3221 size_t line_len = strlen (line);
3222 size_t sflags_len = shellflags ? strlen (shellflags) : 0;
3224 char *command_ptr = NULL; /* used for batch_mode_shell mode */
3227 # ifdef __EMX__ /* is this necessary? */
3228 if (!unixy_shell && shellflags)
3229 shellflags[0] = '/'; /* "/c" */
3232 /* In .ONESHELL mode we are allowed to throw the entire current
3233 recipe string at a single shell and trust that the user
3234 has configured the shell and shell flags, and formatted
3235 the string, appropriately. */
3238 /* If the shell is Bourne compatible, we must remove and ignore
3239 interior special chars [@+-] because they're meaningless to
3240 the shell itself. If, however, we're in .ONESHELL mode and
3241 have changed SHELL to something non-standard, we should
3242 leave those alone because they could be part of the
3243 script. In this case we must also leave in place
3244 any leading [@+-] for the same reason. */
3246 /* Remove and ignore interior prefix chars [@+-] because they're
3247 meaningless given a single shell. */
3248 #if defined __MSDOS__ || defined (__EMX__)
3249 if (unixy_shell) /* the test is complicated and we already did it */
3251 if (is_bourne_compatible_shell (shell)
3253 /* If we didn't find any sh.exe, don't behave is if we did! */
3254 && !no_default_sh_exe
3259 const char *f = line;
3262 /* Copy the recipe, removing and ignoring interior prefix chars
3263 [@+-]: they're meaningless in .ONESHELL mode. */
3264 while (f[0] != '\0')
3268 /* This is the start of a new recipe line. Skip whitespace
3269 and prefix characters but not newlines. */
3270 while (ISBLANK (*f) || *f == '-' || *f == '@' || *f == '+')
3273 /* Copy until we get to the next logical recipe line. */
3281 /* On unescaped newline, we're done with this line. */
3282 if (f[-1] == '\n' && ! esc)
3285 /* Something else: reset the escape sequence. */
3293 else /* non-Posix shell (cmd.exe etc.) */
3295 const char *f = line;
3300 int id = GetCurrentProcessId ();
3303 /* Generate a file name for the temporary batch file. */
3304 sprintf (fbuf, "make%d", id);
3305 *batch_filename = create_batch_file (fbuf, 0, &temp_fd);
3306 DB (DB_JOBS, (_("Creating temporary batch file %s\n"),
3309 /* Create a FILE object for the batch file, and write to it the
3310 commands to be executed. Put the batch file in TEXT mode. */
3311 _setmode (temp_fd, _O_TEXT);
3312 batch = _fdopen (temp_fd, "wt");
3313 fputs ("@echo off\n", batch);
3314 DB (DB_JOBS, (_("Batch file contents:\n\t@echo off\n")));
3316 /* Copy the recipe, removing and ignoring interior prefix chars
3317 [@+-]: they're meaningless in .ONESHELL mode. */
3320 /* This is the start of a new recipe line. Skip whitespace
3321 and prefix characters but not newlines. */
3322 while (ISBLANK (*f) || *f == '-' || *f == '@' || *f == '+')
3325 /* Copy until we get to the next logical recipe line. */
3328 /* Remove the escaped newlines in the command, and the
3329 blanks that follow them. Windows shells cannot handle
3330 escaped newlines. */
3331 if (*f == '\\' && f[1] == '\n')
3334 while (ISBLANK (*f))
3338 /* On an unescaped newline, we're done with this
3343 /* Write another line into the batch file. */
3348 fputs (tstart, batch);
3349 DB (DB_JOBS, ("\t%s", tstart));
3354 DB (DB_JOBS, ("\n"));
3357 /* Create an argv list for the shell command line that
3358 will run the batch file. */
3359 new_argv = xmalloc (2 * sizeof (char *));
3360 new_argv[0] = xstrdup (*batch_filename);
3364 #endif /* WINDOWS32 */
3365 /* Create an argv list for the shell command line. */
3370 new_argv = xmalloc ((4 + sflags_len/2) * sizeof (char *));
3372 nextp = new_argv[0] = xmalloc (shell_len + sflags_len + line_len + 3);
3373 nextp = mempcpy (nextp, shell, shell_len + 1);
3375 /* Chop up the shellflags (if any) and assign them. */
3378 new_argv[n++] = nextp;
3383 /* Parse shellflags using construct_command_argv_internal to
3386 char *f = alloca (sflags_len + 1);
3387 memcpy (f, shellflags, sflags_len + 1);
3388 argv = construct_command_argv_internal (f, 0, 0, 0, 0, flags, 0);
3392 for (a = argv; *a; ++a)
3394 new_argv[n++] = nextp;
3395 nextp = stpcpy (nextp, *a) + 1;
3402 /* Set the command to invoke. */
3403 new_argv[n++] = nextp;
3404 memcpy(nextp, line, line_len + 1);
3405 new_argv[n++] = NULL;
3410 new_line = xmalloc ((shell_len*2) + 1 + sflags_len + 1
3411 + (line_len*2) + 1);
3413 /* Copy SHELL, escaping any characters special to the shell. If
3414 we don't escape them, construct_command_argv_internal will
3415 recursively call itself ad nauseam, or until stack overflow,
3416 whichever happens first. */
3417 for (cp = shell; *cp != '\0'; ++cp)
3419 if (strchr (sh_chars, *cp) != 0)
3426 ap = mempcpy (ap, shellflags, sflags_len);
3432 for (p = line; *p != '\0'; ++p)
3434 if (restp != NULL && *p == '\n')
3439 else if (*p == '\\' && p[1] == '\n')
3441 /* POSIX says we keep the backslash-newline. If we don't have a
3442 POSIX shell on DOS/Windows/OS2, mimic the pre-POSIX behavior
3443 and remove the backslash/newline. */
3444 #if defined (__MSDOS__) || defined (__EMX__) || defined (WINDOWS32)
3445 # define PRESERVE_BSNL unixy_shell
3447 # define PRESERVE_BSNL 1
3452 /* Only non-batch execution needs another backslash,
3453 because it will be passed through a recursive
3454 invocation of this function. */
3455 if (!batch_mode_shell)
3463 /* DOS shells don't know about backslash-escaping. */
3464 if (unixy_shell && !batch_mode_shell &&
3465 (*p == '\\' || *p == '\'' || *p == '"'
3467 || strchr (sh_chars, *p) != 0))
3470 else if (unixy_shell && strneq (p, "...", 3))
3472 /* The case of '...' wildcard again. */
3473 ap = stpcpy (ap, "\\.\\.\\");
3479 if (ap == new_line + shell_len + sflags_len + 2)
3481 /* Line was empty. */
3488 /* Some shells do not work well when invoked as 'sh -c xxx' to run a
3489 command line (e.g. Cygnus GNUWIN32 sh.exe on W32 systems). In these
3490 cases, run commands via a script file. */
3491 if (just_print_flag && !(flags & COMMANDS_RECURSE))
3493 /* Need to allocate new_argv, although it's unused, because
3494 start_job_command will want to free it and its 0'th element. */
3495 new_argv = xmalloc (2 * sizeof (char *));
3496 new_argv[0] = xstrdup ("");
3499 else if ((no_default_sh_exe || batch_mode_shell) && batch_filename)
3503 int id = GetCurrentProcessId ();
3506 /* create a file name */
3507 sprintf (fbuf, "make%d", id);
3508 *batch_filename = create_batch_file (fbuf, unixy_shell, &temp_fd);
3510 DB (DB_JOBS, (_("Creating temporary batch file %s\n"),
3513 /* Create a FILE object for the batch file, and write to it the
3514 commands to be executed. Put the batch file in TEXT mode. */
3515 _setmode (temp_fd, _O_TEXT);
3516 batch = _fdopen (temp_fd, "wt");
3518 fputs ("@echo off\n", batch);
3519 fputs (command_ptr, batch);
3520 fputc ('\n', batch);
3522 DB (DB_JOBS, (_("Batch file contents:%s\n\t%s\n"),
3523 !unixy_shell ? "\n\t@echo off" : "", command_ptr));
3526 new_argv = xmalloc (3 * sizeof (char *));
3529 new_argv[0] = xstrdup (shell);
3530 new_argv[1] = *batch_filename; /* only argv[0] gets freed later */
3534 new_argv[0] = xstrdup (*batch_filename);
3540 #endif /* WINDOWS32 */
3543 new_argv = construct_command_argv_internal (new_line, 0, 0, 0, 0,
3547 else if (!unixy_shell)
3549 /* new_line is local, must not be freed therefore
3550 We use line here instead of new_line because we run the shell
3552 size_t line_len = strlen (line);
3555 memcpy (new_line, line, line_len + 1);
3556 /* Replace all backslash-newline combination and also following tabs.
3557 Important: stop at the first '\n' because that's what the loop above
3558 did. The next line starting at restp[0] will be executed during the
3559 next call of this function. */
3560 while (*q != '\0' && *q != '\n')
3562 if (q[0] == '\\' && q[1] == '\n')
3563 q += 2; /* remove '\\' and '\n' */
3569 # ifndef NO_CMD_DEFAULT
3570 if (strnicmp (new_line, "echo", 4) == 0
3571 && (new_line[4] == ' ' || new_line[4] == '\t'))
3573 /* the builtin echo command: handle it separately */
3574 size_t echo_len = line_len - 5;
3575 char *echo_line = new_line + 5;
3577 /* special case: echo 'x="y"'
3578 cmd works this way: a string is printed as is, i.e., no quotes
3579 are removed. But autoconf uses a command like echo 'x="y"' to
3580 determine whether make works. autoconf expects the output x="y"
3581 so we will do exactly that.
3582 Note: if we do not allow cmd to be the default shell
3583 we do not need this kind of voodoo */
3584 if (echo_line[0] == '\''
3585 && echo_line[echo_len - 1] == '\''
3586 && strncmp (echo_line + 1, "ac_maketemp=",
3587 strlen ("ac_maketemp=")) == 0)
3589 /* remove the enclosing quotes */
3590 memmove (echo_line, echo_line + 1, echo_len - 2);
3591 echo_line[echo_len - 2] = '\0';
3597 /* Let the shell decide what to do. Put the command line into the
3598 2nd command line argument and hope for the best ;-) */
3599 size_t sh_len = strlen (shell);
3601 /* exactly 3 arguments + NULL */
3602 new_argv = xmalloc (4 * sizeof (char *));
3603 /* Exactly strlen(shell) + strlen("/c") + strlen(line) + 3 times
3604 the trailing '\0' */
3605 new_argv[0] = xmalloc (sh_len + line_len + 5);
3606 memcpy (new_argv[0], shell, sh_len + 1);
3607 new_argv[1] = new_argv[0] + sh_len + 1;
3608 memcpy (new_argv[1], "/c", 3);
3609 new_argv[2] = new_argv[1] + 3;
3610 memcpy (new_argv[2], new_line, line_len + 1);
3614 #elif defined(__MSDOS__)
3617 /* With MSDOS shells, we must construct the command line here
3618 instead of recursively calling ourselves, because we
3619 cannot backslash-escape the special characters (see above). */
3620 new_argv = xmalloc (sizeof (char *));
3621 line_len = strlen (new_line) - shell_len - sflags_len - 2;
3622 new_argv[0] = xmalloc (line_len + 1);
3623 strncpy (new_argv[0],
3624 new_line + shell_len + sflags_len + 2, line_len);
3625 new_argv[0][line_len] = '\0';
3629 fatal (NILF, CSTRLEN (__FILE__) + INTSTR_LENGTH,
3630 _("%s (line %d) Bad shell context (!unixy && !batch_mode_shell)\n"),
3631 __FILE__, __LINE__);
3636 #endif /* ! AMIGA */
3642 /* Figure out the argument list necessary to run LINE as a command. Try to
3643 avoid using a shell. This routine handles only ' quoting, and " quoting
3644 when no backslash, $ or ' characters are seen in the quotes. Starting
3645 quotes may be escaped with a backslash. If any of the characters in
3646 sh_chars is seen, or any of the builtin commands listed in sh_cmds
3647 is the first word of a line, the shell is used.
3649 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
3650 If *RESTP is NULL, newlines will be ignored.
3652 FILE is the target whose commands these are. It is used for
3653 variable expansion for $(SHELL) and $(IFS). */
3656 construct_command_argv (char *line, char **restp, struct file *file,
3657 int cmd_flags, char **batch_filename)
3659 char *shell, *ifs, *shellflags;
3663 /* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
3664 int save = warn_undefined_variables_flag;
3665 warn_undefined_variables_flag = 0;
3667 shell = allocated_variable_expand_for_file ("$(SHELL)", file);
3670 * Convert to forward slashes so that construct_command_argv_internal()
3675 char *p = w32ify (shell, 0);
3681 static const char *unixroot = NULL;
3682 static const char *last_shell = "";
3683 static int init = 0;
3686 unixroot = getenv ("UNIXROOT");
3687 /* unixroot must be NULL or not empty */
3688 if (unixroot && unixroot[0] == '\0') unixroot = NULL;
3692 /* if we have an unixroot drive and if shell is not default_shell
3693 (which means it's either cmd.exe or the test has already been
3694 performed) and if shell is an absolute path without drive letter,
3695 try whether it exists e.g.: if "/bin/sh" does not exist use
3696 "$UNIXROOT/bin/sh" instead. */
3697 if (unixroot && shell && ISDIRSEP (shell[0]) && !streq (shell, last_shell))
3699 /* trying a new shell, check whether it exists */
3700 size_t size = strlen (shell);
3701 char *buf = xmalloc (size + 7);
3702 memcpy (buf, shell, size);
3703 memcpy (buf + size, ".exe", 5); /* including the trailing '\0' */
3704 if (access (shell, F_OK) != 0 && access (buf, F_OK) != 0)
3706 /* try the same for the unixroot drive */
3707 memmove (buf + 2, buf, size + 5);
3708 buf[0] = unixroot[0];
3709 buf[1] = unixroot[1];
3710 if (access (buf, F_OK) == 0)
3711 /* we have found a shell! */
3721 #endif /* __EMX__ */
3723 shellflags = allocated_variable_expand_for_file ("$(.SHELLFLAGS)", file);
3724 ifs = allocated_variable_expand_for_file ("$(IFS)", file);
3726 warn_undefined_variables_flag = save;
3729 argv = construct_command_argv_internal (line, restp, shell, shellflags, ifs,
3730 cmd_flags, batch_filename);
3739 #if !defined(HAVE_DUP2) && !defined(_AMIGA)
3741 dup2 (int old, int new)
3746 EINTRLOOP (fd, dup (old));
3756 #endif /* !HAVE_DUP2 && !_AMIGA */
3758 /* On VMS systems, include special VMS functions. */
3761 #include "vmsjobs.c"