1 /* Job execution and handling for GNU Make.
2 Copyright (C) 1988-2020 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/>. */
29 /* Default shell to use. */
31 # ifdef HAVE_STRINGS_H
32 # include <strings.h> /* for strcasecmp, strncasecmp */
36 const char *default_shell = "sh.exe";
37 int no_default_sh_exe = 1;
38 int batch_mode_shell = 1;
41 #elif defined (_AMIGA)
43 const char *default_shell = "";
44 extern int MyExecute (char **);
45 int batch_mode_shell = 0;
47 #elif defined (__MSDOS__)
49 /* The default shell is a pointer so we can change it if Makefile
50 says so. It is without an explicit path so we get a chance
51 to search the $PATH for it (since MSDOS doesn't have standard
52 directories we could trust). */
53 const char *default_shell = "command.com";
54 int batch_mode_shell = 0;
56 #elif defined (__EMX__)
58 const char *default_shell = "/bin/sh";
59 int batch_mode_shell = 0;
65 const char *default_shell = "";
66 int batch_mode_shell = 0;
68 #define strsignal vms_strsignal
69 char * vms_strsignal (int status);
72 # define C_FACILITY_NO 0x350000
74 #ifndef VMS_POSIX_EXIT_MASK
75 # define VMS_POSIX_EXIT_MASK (C_FACILITY_NO | 0xA000)
80 const 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 #ifdef USE_POSIX_SPAWN
141 # include "findprog.h"
144 #if !defined (wait) && !defined (POSIX)
148 #ifndef HAVE_UNION_WAIT
153 # define WTERMSIG(x) ((x) & 0x7f)
156 # define WCOREDUMP(x) ((x) & 0x80)
159 # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
162 # define WIFSIGNALED(x) (WTERMSIG (x) != 0)
165 # define WIFEXITED(x) (WTERMSIG (x) == 0)
168 #else /* Have 'union wait'. */
170 # define WAIT_T union wait
172 # define WTERMSIG(x) ((x).w_termsig)
175 # define WCOREDUMP(x) ((x).w_coredump)
178 # define WEXITSTATUS(x) ((x).w_retcode)
181 # define WIFSIGNALED(x) (WTERMSIG(x) != 0)
184 # define WIFEXITED(x) (WTERMSIG(x) == 0)
187 #endif /* Don't have 'union wait'. */
189 #if !defined(HAVE_UNISTD_H) && !defined(WINDOWS32)
201 /* Different systems have different requirements for pid_t.
202 Plus we have to support gettext string translation... Argh. */
206 static char pidstring[100];
207 #if defined(WINDOWS32) && (__GNUC__ > 3 || _MSC_VER > 1300)
208 /* %Id is only needed for 64-builds, which were not supported by
209 older versions of Windows compilers. */
210 sprintf (pidstring, "%Id", pid);
212 sprintf (pidstring, "%lu", (unsigned long) pid);
217 #ifndef HAVE_GETLOADAVG
218 int getloadavg (double loadavg[], int nelem);
221 static void free_child (struct child *);
222 static void start_job_command (struct child *child);
223 static int load_too_high (void);
224 static int job_next_command (struct child *);
225 static int start_waiting_job (struct child *);
227 /* Chain of all live (or recently deceased) children. */
229 struct child *children = 0;
231 /* Number of children currently running. */
233 unsigned int job_slots_used = 0;
235 /* Nonzero if the 'good' standard input is in use. */
237 static int good_stdin_used = 0;
239 /* Chain of children waiting to run until the load average goes down. */
241 static struct child *waiting_jobs = 0;
243 /* Non-zero if we use a *real* shell (always so on Unix). */
247 /* Number of jobs started in the current second. */
249 unsigned long job_counter = 0;
251 /* Number of jobserver tokens this instance is currently using. */
253 unsigned int jobserver_tokens = 0;
258 * The macro which references this function is defined in makeint.h.
261 w32_kill (pid_t pid, int sig)
263 return ((process_kill ((HANDLE)pid, sig) == TRUE) ? 0 : -1);
266 /* This function creates a temporary file name with an extension specified
268 * Return an xmalloc'ed string of a newly created temp file and its
269 * file descriptor, or die. */
271 create_batch_file (char const *base, int unixy, int *fd)
273 const char *const ext = unixy ? "sh" : "bat";
274 const char *error_string = NULL;
275 char temp_path[MAXPATHLEN]; /* need to know its length */
276 unsigned path_size = GetTempPath (sizeof temp_path, temp_path);
278 /* The following variable is static so we won't try to reuse a name
279 that was generated a little while ago, because that file might
280 not be on disk yet, since we use FILE_ATTRIBUTE_TEMPORARY below,
281 which tells the OS it doesn't need to flush the cache to disk.
282 If the file is not yet on disk, we might think the name is
283 available, while it really isn't. This happens in parallel
284 builds, where Make doesn't wait for one job to finish before it
285 launches the next one. */
286 static unsigned uniq = 0;
287 static int second_loop = 0;
288 const size_t sizemax = strlen (base) + strlen (ext) + 10;
292 path_size = GetCurrentDirectory (sizeof temp_path, temp_path);
297 if (uniq >= 0x10000 && !second_loop)
299 /* If we already had 64K batch files in this
300 process, make a second loop through the numbers,
301 looking for free slots, i.e. files that were
302 deleted in the meantime. */
306 while (path_size > 0 &&
307 path_size + sizemax < sizeof temp_path &&
308 !(uniq >= 0x10000 && second_loop))
310 unsigned size = sprintf (temp_path + path_size,
312 temp_path[path_size - 1] == '\\' ? "" : "\\",
314 HANDLE h = CreateFile (temp_path, /* file name */
315 GENERIC_READ | GENERIC_WRITE, /* desired access */
316 0, /* no share mode */
317 NULL, /* default security attributes */
318 CREATE_NEW, /* creation disposition */
319 FILE_ATTRIBUTE_NORMAL | /* flags and attributes */
320 FILE_ATTRIBUTE_TEMPORARY, /* we'll delete it */
321 NULL); /* no template file */
323 if (h == INVALID_HANDLE_VALUE)
325 const DWORD er = GetLastError ();
327 if (er == ERROR_FILE_EXISTS || er == ERROR_ALREADY_EXISTS)
330 if (uniq == 0x10000 && !second_loop)
337 /* the temporary path is not guaranteed to exist */
338 else if (path_is_dot == 0)
340 path_size = GetCurrentDirectory (sizeof temp_path, temp_path);
346 error_string = map_windows32_error_to_string (er);
352 const unsigned final_size = path_size + size + 1;
353 char *const path = xmalloc (final_size);
354 memcpy (path, temp_path, final_size);
355 *fd = _open_osfhandle ((intptr_t)h, 0);
360 for (p = path; (ch = *p) != 0; ++p)
364 return path; /* good return */
369 if (error_string == NULL)
370 error_string = _("Cannot create a temporary file\n");
371 O (fatal, NILF, error_string);
376 #endif /* WINDOWS32 */
379 /* returns whether path is assumed to be a unix like shell. */
381 _is_unixy_shell (const char *path)
383 /* list of non unix shells */
384 const char *known_os2shells[] = {
396 /* find the rightmost '/' or '\\' */
397 const char *name = strrchr (path, '/');
398 const char *p = strrchr (path, '\\');
401 if (name && p) /* take the max */
402 name = (name > p) ? name : p;
403 else if (p) /* name must be 0 */
405 else if (!name) /* name and p must be 0 */
408 if (*name == '/' || *name == '\\') name++;
411 while (known_os2shells[i] != NULL)
413 if (strcasecmp (name, known_os2shells[i]) == 0)
414 return 0; /* not a unix shell */
418 /* in doubt assume a unix like shell */
423 /* determines whether path looks to be a Bourne-like shell. */
425 is_bourne_compatible_shell (const char *path)
427 /* List of known POSIX (or POSIX-ish) shells. */
428 static const char *unix_shells[] = {
440 /* find the rightmost '/' or '\\' */
441 const char *name = strrchr (path, '/');
442 char *p = strrchr (path, '\\');
444 if (name && p) /* take the max */
445 name = (name > p) ? name : p;
446 else if (p) /* name must be 0 */
448 else if (!name) /* name and p must be 0 */
451 if (*name == '/' || *name == '\\')
454 /* this should be able to deal with extensions on Windows-like systems */
455 for (s = unix_shells; *s != NULL; ++s)
457 #if defined(WINDOWS32) || defined(__MSDOS__)
458 size_t len = strlen (*s);
459 if ((strlen (name) >= len && STOP_SET (name[len], MAP_DOT|MAP_NUL))
460 && strncasecmp (name, *s, len) == 0)
462 if (strcmp (name, *s) == 0)
464 return 1; /* a known unix-style shell */
467 /* if not on the list, assume it's not a Bourne-like shell */
472 extern sigset_t fatal_signal_set;
477 sigprocmask (SIG_BLOCK, &fatal_signal_set, (sigset_t *) 0);
483 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, (sigset_t *) 0);
490 sigemptyset (&empty);
491 sigprocmask (SIG_SETMASK, &empty, (sigset_t *) 0);
494 #elif defined(HAVE_SIGSETMASK)
496 extern int fatal_signal_mask;
501 sigblock (fatal_signal_mask);
507 sigsetmask (siggetmask (0) & ~fatal_signal_mask)
519 #define unblock_sigs()
528 /* Write an error message describing the exit status given in
529 EXIT_CODE, EXIT_SIG, and COREDUMP, for the target TARGET_NAME.
530 Append "(ignored)" if IGNORED is nonzero. */
533 child_error (struct child *child,
534 int exit_code, int exit_sig, int coredump, int ignored)
536 const char *pre = "*** ";
537 const char *post = "";
538 const char *dump = "";
539 const struct file *f = child->file;
540 const floc *flocp = &f->cmds->fileinfo;
544 if (ignored && run_silent)
547 if (exit_sig && coredump)
548 dump = _(" (core dumped)");
553 post = _(" (ignored)");
560 char *a = alloca (strlen (flocp->filenm) + 6 + INTSTR_LENGTH + 1);
561 sprintf (a, "%s:%lu", flocp->filenm, flocp->lineno + flocp->offset);
565 l = strlen (pre) + strlen (nm) + strlen (f->name) + strlen (post);
567 OUTPUT_SET (&child->output);
572 error (NILF, l + INTSTR_LENGTH,
573 _("%s[%s: %s] Error %d%s"), pre, nm, f->name, exit_code, post);
576 const char *s = strsignal (exit_sig);
577 error (NILF, l + strlen (s) + strlen (dump),
578 "%s[%s: %s] %s%s%s", pre, nm, f->name, s, dump, post);
585 /* Handle a dead child. This handler may or may not ever be installed.
587 If we're using the jobserver feature without pselect(), we need it.
588 First, installing it ensures the read will interrupt on SIGCHLD. Second,
589 we close the dup'd read FD to ensure we don't enter another blocking read
590 without reaping all the dead children. In this case we don't need the
593 If we don't have either waitpid or wait3, then make is unreliable, but we
594 use the dead_children count to reap children as best we can. */
596 static unsigned int dead_children = 0;
599 child_handler (int sig UNUSED)
606 /* The signal handler must called only once! */
607 signal (SIGCHLD, SIG_DFL);
611 extern pid_t shell_function_pid;
613 /* Reap all dead children, storing the returned status and the new command
614 state ('cs_finished') in the 'file' member of the 'struct child' for the
615 dead child, and removing the child from the chain. In addition, if BLOCK
616 nonzero, we block in this function until we've reaped at least one
617 complete child, waiting for it to die if necessary. If ERR is nonzero,
618 print an error message first. */
621 reap_children (int block, int err)
626 /* Initially, assume we have some. */
630 # define REAP_MORE reap_more
632 # define REAP_MORE dead_children
637 We have at least one child outstanding OR a shell function in progress,
639 We're blocking for a complete child OR there are more children to reap
641 we'll keep reaping children. */
643 while ((children != 0 || shell_function_pid != 0)
644 && (block || REAP_MORE))
646 unsigned int remote = 0;
648 int exit_code, exit_sig, coredump;
649 struct child *lastc, *c;
651 int any_remote, any_local;
656 static int printed = 0;
658 /* We might block for a while, so let the user know why.
659 Only print this message once no matter how many jobs are left. */
662 O (error, NILF, _("*** Waiting for unfinished jobs...."));
666 /* We have one less dead child to reap. As noted in
667 child_handler() above, this count is completely unimportant for
668 all modern, POSIX-y systems that support wait3() or waitpid().
669 The rest of this comment below applies only to early, broken
670 pre-POSIX systems. We keep the count only because... it's there...
672 The test and decrement are not atomic; if it is compiled into:
673 register = dead_children - 1;
674 dead_children = register;
675 a SIGCHLD could come between the two instructions.
676 child_handler increments dead_children.
677 The second instruction here would lose that increment. But the
678 only effect of dead_children being wrong is that we might wait
679 longer than necessary to reap a child, and lose some parallelism;
680 and we might print the "Waiting for unfinished jobs" message above
681 when not necessary. */
683 if (dead_children > 0)
687 any_local = shell_function_pid != 0;
689 for (c = children; c != 0; lastc = c, c = c->next)
691 any_remote |= c->remote;
692 any_local |= ! c->remote;
694 /* If pid < 0, this child never even started. Handle it. */
699 /* According to POSIX, 127 is used for command not found. */
704 DB (DB_JOBS, (_("Live child %p (%s) PID %s %s\n"),
705 c, c->file->name, pid2str (c->pid),
706 c->remote ? _(" (remote)") : ""));
712 /* First, check for remote children. */
714 pid = remote_status (&exit_code, &exit_sig, &coredump, 0);
719 /* We got a remote child. */
723 /* A remote status command failed miserably. Punt. */
724 #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
727 pfatal_with_name ("remote_status");
731 /* No remote children. Check for local children. */
732 #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
736 /* Todo: This needs more untangling multi-process support */
737 /* Just do single child process support now */
738 vmsWaitForChildren (&status);
741 /* VMS failure status can not be fully translated */
742 status = $VMS_STATUS_SUCCESS (c->cstatus) ? 0 : (1 << 8);
744 /* A Posix failure can be exactly translated */
745 if ((c->cstatus & VMS_POSIX_EXIT_MASK) == VMS_POSIX_EXIT_MASK)
746 status = (c->cstatus >> 3 & 255) << 8;
750 pid = WAIT_NOHANG (&status);
753 EINTRLOOP (pid, wait (&status));
761 /* The wait*() failed miserably. Punt. */
762 pfatal_with_name ("wait");
766 /* We got a child exit; chop the status word up. */
767 exit_code = WEXITSTATUS (status);
768 exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0;
769 coredump = WCOREDUMP (status);
773 /* No local children are dead. */
776 if (!block || !any_remote)
779 /* Now try a blocking wait for a remote child. */
780 pid = remote_status (&exit_code, &exit_sig, &coredump, 1);
782 goto remote_status_lose;
784 /* No remote children either. Finally give up. */
787 /* We got a remote child. */
790 #endif /* !__MSDOS__, !Amiga, !WINDOWS32. */
793 /* Life is very different on MSDOS. */
796 exit_code = WEXITSTATUS (status);
797 if (exit_code == 0xff)
799 exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0;
801 #endif /* __MSDOS__ */
805 status = amiga_status;
806 exit_code = amiga_status;
814 DWORD dwWaitStatus = 0;
819 /* Record the thread ID of the main process, so that we
820 could suspend it in the signal handler. */
823 hcTID = GetCurrentThread ();
824 hcPID = GetCurrentProcess ();
825 if (!DuplicateHandle (hcPID, hcTID, hcPID, &main_thread, 0,
826 FALSE, DUPLICATE_SAME_ACCESS))
828 DWORD e = GetLastError ();
830 "Determine main thread ID (Error %ld: %s)\n",
831 e, map_windows32_error_to_string (e));
834 DB (DB_VERBOSE, ("Main thread handle = %p\n", main_thread));
837 /* wait for anything to finish */
838 hPID = process_wait_for_any (block, &dwWaitStatus);
841 /* was an error found on this process? */
842 int werr = process_last_err (hPID);
845 exit_code = process_exit_code (hPID);
847 /* the extra tests of exit_code are here to prevent
848 map_windows32_error_to_string from calling 'fatal',
849 which will then call reap_children again */
850 if (werr && exit_code > 0 && exit_code < WSABASEERR)
851 fprintf (stderr, "make (e=%d): %s", exit_code,
852 map_windows32_error_to_string (exit_code));
855 exit_sig = process_signal (hPID);
857 /* cleanup process */
858 process_cleanup (hPID);
862 else if (dwWaitStatus == WAIT_FAILED)
864 /* The WaitForMultipleObjects() failed miserably. Punt. */
865 pfatal_with_name ("WaitForMultipleObjects");
867 else if (dwWaitStatus == WAIT_TIMEOUT)
869 /* No child processes are finished. Give up waiting. */
876 #endif /* WINDOWS32 */
879 /* Check if this is the child of the 'shell' function. */
880 if (!remote && pid == shell_function_pid)
882 shell_completed (exit_code, exit_sig);
886 /* Search for a child matching the deceased one. */
888 for (c = children; c != 0; lastc = c, c = c->next)
889 if (c->pid == pid && c->remote == remote)
893 /* An unknown child died.
894 Ignore it; it was inherited from our invoker. */
897 DB (DB_JOBS, (exit_sig == 0 && exit_code == 0
898 ? _("Reaping winning child %p PID %s %s\n")
899 : _("Reaping losing child %p PID %s %s\n"),
900 c, pid2str (c->pid), c->remote ? _(" (remote)") : ""));
902 /* If we have started jobs in this second, remove one. */
908 #if defined(USE_POSIX_SPAWN)
909 /* Some versions of posix_spawn() do not detect errors such as command
910 not found until after they fork. In that case they will exit with a
911 code of 127. Try to detect that and provide a useful error message.
912 Otherwise we'll just show the error below, as normal. */
913 if (exit_sig == 0 && exit_code == 127 && c->cmd_name)
915 const char *e = NULL;
919 /* There are various ways that this will show a different error than
920 fork/exec. To really get the right error we'd have to fall back
921 to fork/exec but I don't want to bother with that. Just do the
924 EINTRLOOP(r, stat(c->cmd_name, &st));
926 e = strerror (errno);
927 else if (S_ISDIR(st.st_mode) || !(st.st_mode & S_IXUSR))
928 e = strerror (EACCES);
929 else if (st.st_size == 0)
930 e = strerror (ENOEXEC);
933 OSS(error, NILF, "%s: %s", c->cmd_name, e);
937 /* Determine the failure status: 0 for success, 1 for updating target in
938 question mode, 2 for anything else. */
939 if (exit_sig == 0 && exit_code == 0)
940 child_failed = MAKE_SUCCESS;
941 else if (exit_sig == 0 && exit_code == 1 && question_flag && c->recursive)
942 child_failed = MAKE_TROUBLE;
944 child_failed = MAKE_FAILURE;
946 if (c->sh_batch_file)
950 DB (DB_JOBS, (_("Cleaning up temp batch file %s\n"),
954 rm_status = remove (c->sh_batch_file);
956 DB (DB_JOBS, (_("Cleaning up temp batch file %s failed (%d)\n"),
957 c->sh_batch_file, errno));
959 /* all done with memory */
960 free (c->sh_batch_file);
961 c->sh_batch_file = NULL;
964 /* If this child had the good stdin, say it is now free. */
968 dontcare = c->dontcare;
970 if (child_failed && !c->noerror && !ignore_errors_flag)
972 /* The commands failed. Write an error message,
973 delete non-precious targets, and abort. */
974 static int delete_on_error = -1;
976 if (!dontcare && child_failed == MAKE_FAILURE)
977 child_error (c, exit_code, exit_sig, coredump, 0);
979 c->file->update_status = child_failed == MAKE_FAILURE ? us_failed : us_question;
980 if (delete_on_error == -1)
982 struct file *f = lookup_file (".DELETE_ON_ERROR");
983 delete_on_error = f != 0 && f->is_target;
985 if (exit_sig != 0 || delete_on_error)
986 delete_child_targets (c);
992 /* The commands failed, but we don't care. */
993 child_error (c, exit_code, exit_sig, coredump, 1);
997 /* If there are more commands to run, try to start them. */
998 if (job_next_command (c))
1000 if (handling_fatal_signal)
1002 /* Never start new commands while we are dying.
1003 Since there are more commands that wanted to be run,
1004 the target was not completely remade. So we treat
1005 this as if a command had failed. */
1006 c->file->update_status = us_failed;
1010 #ifndef NO_OUTPUT_SYNC
1011 /* If we're sync'ing per line, write the previous line's
1012 output before starting the next one. */
1013 if (output_sync == OUTPUT_SYNC_LINE)
1014 output_dump (&c->output);
1016 /* Check again whether to start remotely.
1017 Whether or not we want to changes over time.
1018 Also, start_remote_job may need state set up
1019 by start_remote_job_p. */
1020 c->remote = start_remote_job_p (0);
1021 start_job_command (c);
1022 /* Fatal signals are left blocked in case we were
1023 about to put that child on the chain. But it is
1024 already there, so it is safe for a fatal signal to
1025 arrive now; it will clean up this child's targets. */
1027 if (c->file->command_state == cs_running)
1028 /* We successfully started the new command.
1029 Loop to reap more children. */
1033 if (c->file->update_status != us_success)
1034 /* We failed to start the commands. */
1035 delete_child_targets (c);
1038 /* There are no more commands. We got through them all
1039 without an unignored error. Now the target has been
1040 successfully updated. */
1041 c->file->update_status = us_success;
1044 /* When we get here, all the commands for c->file are finished. */
1046 #ifndef NO_OUTPUT_SYNC
1047 /* Synchronize any remaining parallel output. */
1048 output_dump (&c->output);
1051 /* At this point c->file->update_status is success or failed. But
1052 c->file->command_state is still cs_running if all the commands
1053 ran; notice_finished_file looks for cs_running to tell it that
1054 it's interesting to check the file's modtime again now. */
1056 if (! handling_fatal_signal)
1057 /* Notice if the target of the commands has been changed.
1058 This also propagates its values for command_state and
1059 update_status to its also_make files. */
1060 notice_finished_file (c->file);
1062 /* Block fatal signals while frobnicating the list, so that
1063 children and job_slots_used are always consistent. Otherwise
1064 a fatal signal arriving after the child is off the chain and
1065 before job_slots_used is decremented would believe a child was
1066 live and call reap_children again. */
1071 DB (DB_JOBS, (_("Removing child %p PID %s%s from chain.\n"),
1072 c, pid2str (c->pid), c->remote ? _(" (remote)") : ""));
1075 /* There is now another slot open. */
1076 if (job_slots_used > 0)
1077 job_slots_used -= c->jobslot;
1079 /* Remove the child from the chain and free it. */
1083 lastc->next = c->next;
1089 /* If the job failed, and the -k flag was not given, die,
1090 unless we are already in the process of dying. */
1091 if (!err && child_failed && !dontcare && !keep_going_flag &&
1092 /* fatal_error_signal will die with the right signal. */
1093 !handling_fatal_signal)
1096 /* Only block for one child. */
1103 /* Free the storage allocated for CHILD. */
1106 free_child (struct child *child)
1108 output_close (&child->output);
1110 if (!jobserver_tokens)
1111 ONS (fatal, NILF, "INTERNAL: Freeing child %p (%s) but no tokens left!\n",
1112 child, child->file->name);
1114 /* If we're using the jobserver and this child is not the only outstanding
1115 job, put a token back into the pipe for it. */
1117 if (jobserver_enabled () && jobserver_tokens > 1)
1119 jobserver_release (1);
1120 DB (DB_JOBS, (_("Released token for child %p (%s).\n"),
1121 child, child->file->name));
1126 if (handling_fatal_signal) /* Don't bother free'ing if about to die. */
1129 if (child->command_lines != 0)
1132 for (i = 0; i < child->file->cmds->ncommand_lines; ++i)
1133 free (child->command_lines[i]);
1134 free (child->command_lines);
1137 if (child->environment != 0)
1139 char **ep = child->environment;
1142 free (child->environment);
1145 free (child->cmd_name);
1150 /* Start a job to run the commands specified in CHILD.
1151 CHILD is updated to reflect the commands and ID of the child process.
1153 NOTE: On return fatal signals are blocked! The caller is responsible
1154 for calling 'unblock_sigs', once the new child is safely on the chain so
1155 it can be cleaned up in the event of a fatal signal. */
1158 start_job_command (struct child *child)
1163 # define FREE_ARGV(_a)
1166 # define FREE_ARGV(_a) do{ if (_a) { free ((_a)[0]); free (_a); } }while(0)
1170 /* If we have a completely empty commandset, stop now. */
1171 if (!child->command_ptr)
1174 /* Combine the flags parsed for the line itself with
1175 the flags specified globally for this target. */
1176 flags = (child->file->command_flags
1177 | child->file->cmds->lines_flags[child->command_line - 1]);
1179 p = child->command_ptr;
1180 child->noerror = ((flags & COMMANDS_NOERROR) != 0);
1185 flags |= COMMANDS_SILENT;
1187 flags |= COMMANDS_RECURSE;
1190 /* Don't skip newlines. */
1191 else if (!ISBLANK (*p))
1196 child->recursive = ((flags & COMMANDS_RECURSE) != 0);
1198 /* Update the file's command flags with any new ones we found. We only
1199 keep the COMMANDS_RECURSE setting. Even this isn't 100% correct; we are
1200 now marking more commands recursive than should be in the case of
1201 multiline define/endef scripts where only one line is marked "+". In
1202 order to really fix this, we'll have to keep a lines_flags for every
1203 actual line, after expansion. */
1204 child->file->cmds->lines_flags[child->command_line - 1] |= flags & COMMANDS_RECURSE;
1206 /* POSIX requires that a recipe prefix after a backslash-newline should
1207 be ignored. Remove it now so the output is correct. */
1209 char prefix = child->file->cmds->recipe_prefix;
1215 if (p1[0] == '\n' && p1[1] == prefix)
1222 /* Figure out an argument list from this command line. */
1226 /* Skip any leading whitespace */
1233 if ((p[1] != '\n') && (p[1] != 'n') && (p[1] != 't'))
1240 /* Please note, for VMS argv is a string (not an array of strings) which
1241 contains the complete command line, which for multi-line variables
1242 still includes the newlines. So detect newlines and set 'end' (which
1243 is used for child->command_ptr) instead of (re-)writing
1244 construct_command_argv */
1252 instring = !instring;
1253 else if (*s == '\\' && !instring && *(s+1) != 0)
1255 else if (*s == '\n' && !instring)
1264 argv = construct_command_argv (p, &end, child->file,
1265 child->file->cmds->lines_flags[child->command_line - 1],
1266 &child->sh_batch_file);
1269 child->command_ptr = NULL;
1273 child->command_ptr = end;
1277 /* If -q was given, say that updating 'failed' if there was any text on the
1278 command line, or 'succeeded' otherwise. The exit status of 1 tells the
1279 user that -q is saying 'something to do'; the exit status for a random
1281 if (argv != 0 && question_flag && !(flags & COMMANDS_RECURSE))
1285 /* On VMS, argv[0] can be a null string here */
1289 child->file->update_status = us_question;
1290 notice_finished_file (child->file);
1297 if (touch_flag && !(flags & COMMANDS_RECURSE))
1299 /* Go on to the next command. It might be the recursive one.
1300 We construct ARGV only to find the end of the command line. */
1309 execute_by_shell = 0; /* in case construct_command_argv sets it */
1311 /* This line has no commands. Go to the next. */
1312 if (job_next_command (child))
1313 start_job_command (child);
1316 /* No more commands. Make sure we're "running"; we might not be if
1317 (e.g.) all commands were skipped due to -n. */
1318 set_command_state (child->file, cs_running);
1319 child->file->update_status = us_success;
1320 notice_finished_file (child->file);
1327 /* Are we going to synchronize this command's output? Do so if either we're
1328 in SYNC_RECURSE mode or this command is not recursive. We'll also check
1329 output_sync separately below in case it changes due to error. */
1330 child->output.syncout = output_sync && (output_sync == OUTPUT_SYNC_RECURSE
1331 || !(flags & COMMANDS_RECURSE));
1333 OUTPUT_SET (&child->output);
1335 #ifndef NO_OUTPUT_SYNC
1336 if (! child->output.syncout)
1337 /* We don't want to sync this command: to avoid misordered
1338 output ensure any already-synced content is written. */
1339 output_dump (&child->output);
1342 /* Print the command if appropriate. */
1343 if (just_print_flag || trace_flag
1344 || (!(flags & COMMANDS_SILENT) && !run_silent))
1345 OS (message, 0, "%s", p);
1347 /* Tell update_goal_chain that a command has been started on behalf of
1348 this target. It is important that this happens here and not in
1349 reap_children (where we used to do it), because reap_children might be
1350 reaping children from a different target. We want this increment to
1351 guaranteedly indicate that a command was started for the dependency
1352 chain (i.e., update_file recursion chain) we are processing. */
1356 /* Optimize an empty command. People use this for timestamp rules,
1357 so avoid forking a useless shell. Do this after we increment
1358 commands_started so make still treats this special case as if it
1359 performed some action (makes a difference as to what messages are
1362 #if !defined(VMS) && !defined(_AMIGA)
1364 #if defined __MSDOS__ || defined (__EMX__)
1365 unixy_shell /* the test is complicated and we already did it */
1367 (argv[0] && is_bourne_compatible_shell (argv[0]))
1369 && (argv[1] && argv[1][0] == '-'
1371 ((argv[1][1] == 'c' && argv[1][2] == '\0')
1373 (argv[1][1] == 'e' && argv[1][2] == 'c' && argv[1][3] == '\0')))
1374 && (argv[2] && argv[2][0] == ':' && argv[2][1] == '\0')
1380 #endif /* !VMS && !_AMIGA */
1382 /* If -n was given, recurse to get the next line in the sequence. */
1384 if (just_print_flag && !(flags & COMMANDS_RECURSE))
1390 /* We're sure we're going to invoke a command: set up the output. */
1393 /* Flush the output streams so they won't have things written twice. */
1398 /* Decide whether to give this child the 'good' standard input
1399 (one that points to the terminal or whatever), or the 'bad' one
1400 that points to the read side of a broken pipe. */
1402 child->good_stdin = !good_stdin_used;
1403 if (child->good_stdin)
1404 good_stdin_used = 1;
1409 /* Set up the environment for the child. */
1410 if (child->environment == 0)
1411 child->environment = target_environment (child->file);
1414 #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
1417 /* start_waiting_job has set CHILD->remote if we can start a remote job. */
1420 int is_remote, used_stdin;
1422 if (start_remote_job (argv, child->environment,
1423 child->good_stdin ? 0 : get_bad_stdin (),
1424 &is_remote, &id, &used_stdin))
1425 /* Don't give up; remote execution may fail for various reasons. If
1426 so, simply run the job locally. */
1430 if (child->good_stdin && !used_stdin)
1432 child->good_stdin = 0;
1433 good_stdin_used = 0;
1435 child->remote = is_remote;
1442 /* Fork the child process. */
1444 char **parent_environ;
1452 child->pid = child_execute_job ((struct childbase *)child, 1, argv);
1456 parent_environ = environ;
1458 jobserver_pre_child (flags & COMMANDS_RECURSE);
1460 child->pid = child_execute_job ((struct childbase *)child,
1461 child->good_stdin, argv);
1463 environ = parent_environ; /* Restore value child may have clobbered. */
1464 jobserver_post_child (flags & COMMANDS_RECURSE);
1469 #else /* __MSDOS__ or Amiga or WINDOWS32 */
1477 /* We call 'system' to do the job of the SHELL, since stock DOS
1478 shell is too dumb. Our 'system' knows how to handle long
1479 command lines even if pipes/redirection is needed; it will only
1480 call COMMAND.COM when its internal commands are used. */
1481 if (execute_by_shell)
1483 char *cmdline = argv[0];
1484 /* We don't have a way to pass environment to 'system',
1485 so we need to save and restore ours, sigh... */
1486 char **parent_environ = environ;
1488 environ = child->environment;
1490 /* If we have a *real* shell, tell 'system' to call
1491 it to do everything for us. */
1494 /* A *real* shell on MSDOS may not support long
1495 command lines the DJGPP way, so we must use 'system'. */
1496 cmdline = argv[2]; /* get past "shell -c" */
1499 dos_command_running = 1;
1500 proc_return = system (cmdline);
1501 environ = parent_environ;
1502 execute_by_shell = 0; /* for the next time */
1506 dos_command_running = 1;
1507 proc_return = spawnvpe (P_WAIT, argv[0], argv, child->environment);
1510 /* Need to unblock signals before turning off
1511 dos_command_running, so that child's signals
1512 will be treated as such (see fatal_error_signal). */
1514 dos_command_running = 0;
1516 /* If the child got a signal, dos_status has its
1517 high 8 bits set, so be careful not to alter them. */
1518 if (proc_return == -1)
1521 dos_status |= (proc_return & 0xff);
1523 child->pid = dos_pid++;
1525 #endif /* __MSDOS__ */
1527 amiga_status = MyExecute (argv);
1530 child->pid = amiga_pid++;
1531 if (amiga_batch_file)
1533 amiga_batch_file = 0;
1534 DeleteFile (amiga_bname); /* Ignore errors. */
1541 int outfd = FD_STDOUT;
1542 int errfd = FD_STDERR;
1544 /* make UNC paths safe for CreateProcess -- backslash format */
1546 if (arg0 && arg0[0] == '/' && arg0[1] == '/')
1547 for ( ; arg0 && *arg0; arg0++)
1551 /* make sure CreateProcess() has Path it needs */
1552 sync_Path_environment ();
1554 #ifndef NO_OUTPUT_SYNC
1555 /* Divert child output if output_sync in use. */
1556 if (child->output.syncout)
1558 if (child->output.out >= 0)
1559 outfd = child->output.out;
1560 if (child->output.err >= 0)
1561 errfd = child->output.err;
1566 hPID = process_easy (argv, child->environment, outfd, errfd);
1568 if (hPID != INVALID_HANDLE_VALUE)
1569 child->pid = (pid_t) hPID;
1575 _("process_easy() failed to launch process (e=%ld)\n"),
1576 process_last_err (hPID));
1577 for (i = 0; argv[i]; i++)
1578 fprintf (stderr, "%s ", argv[i]);
1579 fprintf (stderr, _("\nCounted %d args in failed launch\n"), i);
1583 #endif /* WINDOWS32 */
1584 #endif /* __MSDOS__ or Amiga or WINDOWS32 */
1586 /* Bump the number of jobs started in this second. */
1587 if (child->pid >= 0)
1590 /* Set the state to running. */
1591 set_command_state (child->file, cs_running);
1593 /* Free the storage used by the child's argument list. */
1601 /* Try to start a child running.
1602 Returns nonzero if the child was started (and maybe finished), or zero if
1603 the load was too high and the child was put on the 'waiting_jobs' chain. */
1606 start_waiting_job (struct child *c)
1608 struct file *f = c->file;
1610 /* If we can start a job remotely, we always want to, and don't care about
1611 the local load average. We record that the job should be started
1612 remotely in C->remote for start_job_command to test. */
1614 c->remote = start_remote_job_p (1);
1616 /* If we are running at least one job already and the load average
1617 is too high, make this one wait. */
1619 && ((job_slots_used > 0 && load_too_high ())
1621 || process_table_full ()
1625 /* Put this child on the chain of children waiting for the load average
1627 set_command_state (f, cs_running);
1628 c->next = waiting_jobs;
1633 /* Start the first command; reap_children will run later command lines. */
1634 start_job_command (c);
1636 switch (f->command_state)
1642 DB (DB_JOBS, (_("Putting child %p (%s) PID %s%s on the chain.\n"),
1643 c, c->file->name, pid2str (c->pid),
1644 c->remote ? _(" (remote)") : ""));
1645 /* One more job slot is in use. */
1647 assert (c->jobslot == 0);
1654 case cs_not_started:
1655 /* All the command lines turned out to be empty. */
1656 f->update_status = us_success;
1660 notice_finished_file (f);
1665 assert (f->command_state == cs_finished);
1672 /* Create a 'struct child' for FILE and start its commands running. */
1675 new_job (struct file *file)
1677 struct commands *cmds = file->cmds;
1682 /* Let any previously decided-upon jobs that are waiting
1683 for the load to go down start before this new one. */
1684 start_waiting_jobs ();
1686 /* Reap any children that might have finished recently. */
1687 reap_children (0, 0);
1689 /* Chop the commands up into lines if they aren't already. */
1690 chop_commands (cmds);
1692 /* Start the command sequence, record it in a new
1693 'struct child', and add that to the chain. */
1695 c = xcalloc (sizeof (struct child));
1696 output_init (&c->output);
1699 c->sh_batch_file = NULL;
1701 /* Cache dontcare flag because file->dontcare can be changed once we
1702 return. Check dontcare inheritance mechanism for details. */
1703 c->dontcare = file->dontcare;
1705 /* Start saving output in case the expansion uses $(info ...) etc. */
1706 OUTPUT_SET (&c->output);
1708 /* Expand the command lines and store the results in LINES. */
1709 lines = xmalloc (cmds->ncommand_lines * sizeof (char *));
1710 for (i = 0; i < cmds->ncommand_lines; ++i)
1712 /* Collapse backslash-newline combinations that are inside variable
1713 or function references. These are left alone by the parser so
1714 that they will appear in the echoing of commands (where they look
1715 nice); and collapsed by construct_command_argv when it tokenizes.
1716 But letting them survive inside function invocations loses because
1717 we don't want the functions to see them as part of the text. */
1719 char *in, *out, *ref;
1721 /* IN points to where in the line we are scanning.
1722 OUT points to where in the line we are writing.
1723 When we collapse a backslash-newline combination,
1724 IN gets ahead of OUT. */
1726 in = out = cmds->command_lines[i];
1727 while ((ref = strchr (in, '$')) != 0)
1729 ++ref; /* Move past the $. */
1732 /* Copy the text between the end of the last chunk
1733 we processed (where IN points) and the new chunk
1734 we are about to process (where REF points). */
1735 memmove (out, in, ref - in);
1737 /* Move both pointers past the boring stuff. */
1741 if (*ref == '(' || *ref == '{')
1743 char openparen = *ref;
1744 char closeparen = openparen == '(' ? ')' : '}';
1749 *out++ = *in++; /* Copy OPENPAREN. */
1751 /* IN now points past the opening paren or brace.
1752 Count parens or braces until it is matched. */
1756 if (*in == closeparen && --count < 0)
1758 else if (*in == '\\' && in[1] == '\n')
1760 /* We have found a backslash-newline inside a
1761 variable or function reference. Eat it and
1762 any following whitespace. */
1765 for (p = in - 1; p > ref && *p == '\\'; --p)
1769 /* There were two or more backslashes, so this is
1770 not really a continuation line. We don't collapse
1771 the quoting backslashes here as is done in
1772 collapse_continuations, because the line will
1773 be collapsed again after expansion. */
1777 /* Skip the backslash, newline, and whitespace. */
1781 /* Discard any preceding whitespace that has
1782 already been written to the output. */
1783 while (out > outref && ISBLANK (out[-1]))
1786 /* Replace it all with a single space. */
1792 if (*in == openparen)
1801 /* There are no more references in this line to worry about.
1802 Copy the remaining uninteresting text to the output. */
1804 memmove (out, in, strlen (in) + 1);
1806 /* Finally, expand the line. */
1807 cmds->fileinfo.offset = i;
1808 lines[i] = allocated_variable_expand_for_file (cmds->command_lines[i],
1812 cmds->fileinfo.offset = 0;
1813 c->command_lines = lines;
1815 /* Fetch the first command line to be run. */
1816 job_next_command (c);
1818 /* Wait for a job slot to be freed up. If we allow an infinite number
1819 don't bother; also job_slots will == 0 if we're using the jobserver. */
1822 while (job_slots_used == job_slots)
1823 reap_children (1, 0);
1825 #ifdef MAKE_JOBSERVER
1826 /* If we are controlling multiple jobs make sure we have a token before
1827 starting the child. */
1829 /* This can be inefficient. There's a decent chance that this job won't
1830 actually have to run any subprocesses: the command script may be empty
1831 or otherwise optimized away. It would be nice if we could defer
1832 obtaining a token until just before we need it, in start_job_command.
1833 To do that we'd need to keep track of whether we'd already obtained a
1834 token (since start_job_command is called for each line of the job, not
1835 just once). Also more thought needs to go into the entire algorithm;
1836 this is where the old parallel job code waits, so... */
1838 else if (jobserver_enabled ())
1843 DB (DB_JOBS, ("Need a job token; we %shave children\n",
1844 children ? "" : "don't "));
1846 /* If we don't already have a job started, use our "free" token. */
1847 if (!jobserver_tokens)
1850 /* Prepare for jobserver token acquisition. */
1851 jobserver_pre_acquire ();
1853 /* Reap anything that's currently waiting. */
1854 reap_children (0, 0);
1856 /* Kick off any jobs we have waiting for an opportunity that
1857 can run now (i.e., waiting for load). */
1858 start_waiting_jobs ();
1860 /* If our "free" slot is available, use it; we don't need a token. */
1861 if (!jobserver_tokens)
1864 /* There must be at least one child already, or we have no business
1865 waiting for a token. */
1867 O (fatal, NILF, "INTERNAL: no children as we go to sleep on read\n");
1870 got_token = jobserver_acquire (waiting_jobs != NULL);
1872 /* If we got one, we're done here. */
1875 DB (DB_JOBS, (_("Obtained token for child %p (%s).\n"),
1885 Use message here so that changes to working directories are logged. */
1888 char *newer = allocated_variable_expand_for_file ("$?", c->file);
1891 if (! cmds->fileinfo.filenm)
1892 nm = _("<builtin>");
1895 char *n = alloca (strlen (cmds->fileinfo.filenm) + 1 + 11 + 1);
1896 sprintf (n, "%s:%lu", cmds->fileinfo.filenm, cmds->fileinfo.lineno);
1900 if (newer[0] == '\0')
1902 _("%s: target '%s' does not exist"), nm, c->file->name);
1905 _("%s: update target '%s' due to: %s"), nm, c->file->name, newer);
1910 /* The job is now primed. Start it running.
1911 (This will notice if there is in fact no recipe.) */
1912 start_waiting_job (c);
1914 if (job_slots == 1 || not_parallel)
1915 /* Since there is only one job slot, make things run linearly.
1916 Wait for the child to die, setting the state to 'cs_finished'. */
1917 while (file->command_state == cs_running)
1918 reap_children (1, 0);
1924 /* Move CHILD's pointers to the next command for it to execute.
1925 Returns nonzero if there is another command. */
1928 job_next_command (struct child *child)
1930 while (child->command_ptr == 0 || *child->command_ptr == '\0')
1932 /* There are no more lines in the expansion of this line. */
1933 if (child->command_line == child->file->cmds->ncommand_lines)
1935 /* There are no more lines to be expanded. */
1936 child->command_ptr = 0;
1937 child->file->cmds->fileinfo.offset = 0;
1941 /* Get the next line to run. */
1942 child->command_ptr = child->command_lines[child->command_line++];
1945 child->file->cmds->fileinfo.offset = child->command_line - 1;
1949 /* Determine if the load average on the system is too high to start a new job.
1951 On systems which provide /proc/loadavg (e.g., Linux), we use an idea
1952 provided by Sven C. Dack <sven.c.dack@sky.com>: retrieve the current number
1953 of processes the kernel is running and, if it's greater than the requested
1954 load we don't allow another job to start. We allow a job to start with
1955 equal processes since one of those will be for make itself, which will then
1956 pause waiting for jobs to clear.
1958 Otherwise, we obtain the system load average and compare that.
1960 The system load average is only recomputed once every N (N>=1) seconds.
1961 However, a very parallel make can easily start tens or even hundreds of
1962 jobs in a second, which brings the system to its knees for a while until
1963 that first batch of jobs clears out.
1965 To avoid this we use a weighted algorithm to try to account for jobs which
1966 have been started since the last second, and guess what the load average
1967 would be now if it were computed.
1969 This algorithm was provided by Thomas Riedl <thomas.riedl@siemens.com>,
1970 based on load average being recomputed once per second, which is
1971 (apparently) how Solaris operates. Linux recomputes only once every 5
1972 seconds, but Linux is handled by the /proc/loadavg algorithm above.
1976 ! calculate something load-oid and add to the observed sys.load,
1977 ! so that latter can catch up:
1978 ! - every job started increases jobctr;
1979 ! - every dying job decreases a positive jobctr;
1980 ! - the jobctr value gets zeroed every change of seconds,
1981 ! after its value*weight_b is stored into the 'backlog' value last_sec
1982 ! - weight_a times the sum of jobctr and last_sec gets
1983 ! added to the observed sys.load.
1985 ! The two weights have been tried out on 24 and 48 proc. Sun Solaris-9
1986 ! machines, using a several-thousand-jobs-mix of cpp, cc, cxx and smallish
1987 ! sub-shelled commands (rm, echo, sed...) for tests.
1988 ! lowering the 'direct influence' factor weight_a (e.g. to 0.1)
1989 ! resulted in significant excession of the load limit, raising it
1990 ! (e.g. to 0.5) took bad to small, fast-executing jobs and didn't
1991 ! reach the limit in most test cases.
1993 ! lowering the 'history influence' weight_b (e.g. to 0.1) resulted in
1994 ! exceeding the limit for longer-running stuff (compile jobs in
1995 ! the .5 to 1.5 sec. range),raising it (e.g. to 0.5) overrepresented
1996 ! small jobs' effects.
2000 #define LOAD_WEIGHT_A 0.25
2001 #define LOAD_WEIGHT_B 0.25
2004 load_too_high (void)
2006 #if defined(__MSDOS__) || defined(VMS) || defined(_AMIGA) || defined(__riscos__)
2009 static double last_sec;
2010 static time_t last_now;
2012 /* This is disabled by default for now, because it will behave badly if the
2013 user gives a value > the number of cores; in that situation the load will
2014 never be exceeded, this function always returns false, and we'll start
2015 all the jobs. Also, it's not quite right to limit jobs to the number of
2016 cores not busy since a job takes some time to start etc. Maybe that's
2017 OK, I'm not sure exactly how to handle that, but for sure we need to
2018 clamp this value at the number of cores before this can be enabled.
2020 #define PROC_FD_INIT -1
2021 static int proc_fd = PROC_FD_INIT;
2027 /* sub_proc.c is limited in the number of objects it can wait for. */
2028 if (process_table_full ())
2032 if (max_load_average < 0)
2035 /* If we haven't tried to open /proc/loadavg, try now. */
2036 #define LOADAVG "/proc/loadavg"
2039 EINTRLOOP (proc_fd, open (LOADAVG, O_RDONLY));
2041 DB (DB_JOBS, ("Using system load detection method.\n"));
2044 DB (DB_JOBS, ("Using " LOADAVG " load detection method.\n"));
2045 fd_noinherit (proc_fd);
2049 /* Try to read /proc/loadavg if we managed to open it. */
2054 EINTRLOOP (r, lseek (proc_fd, 0, SEEK_SET));
2057 #define PROC_LOADAVG_SIZE 64
2058 char avg[PROC_LOADAVG_SIZE+1];
2060 EINTRLOOP (r, read (proc_fd, avg, PROC_LOADAVG_SIZE));
2065 /* The syntax of /proc/loadavg is:
2066 <1m> <5m> <15m> <running>/<total> <pid>
2067 The load is considered too high if there are more jobs
2068 running than the requested average. */
2071 p = strchr (avg, ' ');
2073 p = strchr (p+1, ' ');
2075 p = strchr (p+1, ' ');
2077 if (p && ISDIGIT(p[1]))
2079 int cnt = atoi (p+1);
2080 DB (DB_JOBS, ("Running: system = %d / make = %u (max requested = %f)\n",
2081 cnt, job_slots_used, max_load_average));
2082 return (double)cnt > max_load_average;
2085 DB (DB_JOBS, ("Failed to parse " LOADAVG ": %s\n", avg));
2089 /* If we got here, something went wrong. Give up on this method. */
2091 DB (DB_JOBS, ("Failed to read " LOADAVG ": %s\n", strerror (errno)));
2097 /* Find the real system load average. */
2099 if (getloadavg (&load, 1) != 1)
2101 static int lossage = -1;
2102 /* Complain only once for the same error. */
2103 if (lossage == -1 || errno != lossage)
2106 /* An errno value of zero means getloadavg is just unsupported. */
2108 _("cannot enforce load limits on this operating system"));
2110 perror_with_name (_("cannot enforce load limit: "), "getloadavg");
2117 /* If we're in a new second zero the counter and correct the backlog
2118 value. Only keep the backlog for one extra second; after that it's 0. */
2122 if (last_now == now - 1)
2123 last_sec = LOAD_WEIGHT_B * job_counter;
2131 /* Try to guess what the load would be right now. */
2132 guess = load + (LOAD_WEIGHT_A * (job_counter + last_sec));
2134 DB (DB_JOBS, ("Estimated system load = %f (actual = %f) (max requested = %f)\n",
2135 guess, load, max_load_average));
2137 return guess >= max_load_average;
2141 /* Start jobs that are waiting for the load to be lower. */
2144 start_waiting_jobs (void)
2148 if (waiting_jobs == 0)
2153 /* Check for recently deceased descendants. */
2154 reap_children (0, 0);
2156 /* Take a job off the waiting list. */
2158 waiting_jobs = job->next;
2160 /* Try to start that job. We break out of the loop as soon
2161 as start_waiting_job puts one back on the waiting list. */
2163 while (start_waiting_job (job) && waiting_jobs != 0);
2170 /* EMX: Start a child process. This function returns the new pid. */
2171 # if defined __EMX__
2173 child_execute_job (struct childbase *child, int good_stdin, char **argv)
2176 int fdin = good_stdin ? FD_STDIN : get_bad_stdin ();
2177 int fdout = FD_STDOUT;
2178 int fderr = FD_STDERR;
2180 int save_fdout = -1;
2181 int save_fderr = -1;
2183 /* Divert child output if we want to capture output. */
2184 if (child->output.syncout)
2186 if (child->output.out >= 0)
2187 fdout = child->output.out;
2188 if (child->output.err >= 0)
2189 fderr = child->output.err;
2192 /* For each FD which needs to be redirected first make a dup of the standard
2193 FD to save and mark it close on exec so our child won't see it. Then
2194 dup2() the standard FD to the redirect FD, and also mark the redirect FD
2195 as close on exec. */
2196 if (fdin != FD_STDIN)
2198 save_fdin = dup (FD_STDIN);
2200 O (fatal, NILF, _("no more file handles: could not duplicate stdin\n"));
2201 fd_noinherit (save_fdin);
2203 dup2 (fdin, FD_STDIN);
2204 fd_noinherit (fdin);
2207 if (fdout != FD_STDOUT)
2209 save_fdout = dup (FD_STDOUT);
2212 _("no more file handles: could not duplicate stdout\n"));
2213 fd_noinherit (save_fdout);
2215 dup2 (fdout, FD_STDOUT);
2216 fd_noinherit (fdout);
2219 if (fderr != FD_STDERR)
2223 save_fderr = dup (FD_STDERR);
2226 _("no more file handles: could not duplicate stderr\n"));
2227 fd_noinherit (save_fderr);
2230 dup2 (fderr, FD_STDERR);
2231 fd_noinherit (fderr);
2234 /* Run the command. */
2235 pid = exec_command (argv, child->environment);
2237 /* Restore stdout/stdin/stderr of the parent and close temporary FDs. */
2240 if (dup2 (save_fdin, FD_STDIN) != FD_STDIN)
2241 O (fatal, NILF, _("Could not restore stdin\n"));
2246 if (save_fdout >= 0)
2248 if (dup2 (save_fdout, FD_STDOUT) != FD_STDOUT)
2249 O (fatal, NILF, _("Could not restore stdout\n"));
2254 if (save_fderr >= 0)
2256 if (dup2 (save_fderr, FD_STDERR) != FD_STDERR)
2257 O (fatal, NILF, _("Could not restore stderr\n"));
2263 OSS (error, NILF, "%s: %s", argv[0], strerror (errno));
2268 #elif !defined (_AMIGA) && !defined (__MSDOS__) && !defined (VMS)
2271 Create a child process executing the command in ARGV.
2272 Returns the PID or -1. */
2274 child_execute_job (struct childbase *child, int good_stdin, char **argv)
2276 const int fdin = good_stdin ? FD_STDIN : get_bad_stdin ();
2277 int fdout = FD_STDOUT;
2278 int fderr = FD_STDERR;
2281 #if defined(USE_POSIX_SPAWN)
2283 posix_spawnattr_t attr;
2284 posix_spawn_file_actions_t fa;
2288 /* Divert child output if we want to capture it. */
2289 if (child->output.syncout)
2291 if (child->output.out >= 0)
2292 fdout = child->output.out;
2293 if (child->output.err >= 0)
2294 fderr = child->output.err;
2297 #if !defined(USE_POSIX_SPAWN)
2303 /* We are the child. */
2304 unblock_all_sigs ();
2306 #ifdef SET_STACK_SIZE
2307 /* Reset limits, if necessary. */
2308 if (stack_limit.rlim_cur)
2309 setrlimit (RLIMIT_STACK, &stack_limit);
2312 /* For any redirected FD, dup2() it to the standard FD.
2313 They are all marked close-on-exec already. */
2314 if (fdin >= 0 && fdin != FD_STDIN)
2315 EINTRLOOP (r, dup2 (fdin, FD_STDIN));
2316 if (fdout != FD_STDOUT)
2317 EINTRLOOP (r, dup2 (fdout, FD_STDOUT));
2318 if (fderr != FD_STDERR)
2319 EINTRLOOP (r, dup2 (fderr, FD_STDERR));
2321 /* Run the command. */
2322 exec_command (argv, child->environment);
2324 #else /* USE_POSIX_SPAWN */
2326 if ((r = posix_spawnattr_init (&attr)) != 0)
2329 if ((r = posix_spawn_file_actions_init (&fa)) != 0)
2331 posix_spawnattr_destroy (&attr);
2335 /* Unblock all signals. */
2336 #ifdef HAVE_POSIX_SPAWNATTR_SETSIGMASK
2339 sigemptyset (&mask);
2340 r = posix_spawnattr_setsigmask (&attr, &mask);
2343 flags |= POSIX_SPAWN_SETSIGMASK;
2345 #endif /* have posix_spawnattr_setsigmask() */
2347 /* USEVFORK can give significant speedup on systems where it's available. */
2348 #ifdef POSIX_SPAWN_USEVFORK
2349 flags |= POSIX_SPAWN_USEVFORK;
2352 /* For any redirected FD, dup2() it to the standard FD.
2353 They are all marked close-on-exec already. */
2354 if (fdin >= 0 && fdin != FD_STDIN)
2355 if ((r = posix_spawn_file_actions_adddup2 (&fa, fdin, FD_STDIN)) != 0)
2357 if (fdout != FD_STDOUT)
2358 if ((r = posix_spawn_file_actions_adddup2 (&fa, fdout, FD_STDOUT)) != 0)
2360 if (fderr != FD_STDERR)
2361 if ((r = posix_spawn_file_actions_adddup2 (&fa, fderr, FD_STDERR)) != 0)
2364 /* Be the user, permanently. */
2365 flags |= POSIX_SPAWN_RESETIDS;
2367 /* Apply the spawn flags. */
2368 if ((r = posix_spawnattr_setflags (&attr, flags)) != 0)
2371 /* Look up the program on the child's PATH, if needed. */
2373 const char *p = NULL;
2376 for (pp = child->environment; *pp != NULL; ++pp)
2377 if ((*pp)[0] == 'P' && (*pp)[1] == 'A' && (*pp)[2] == 'T'
2378 && (*pp)[3] == 'H' &&(*pp)[4] == '=')
2384 cmd = (char *)find_in_given_path (argv[0], p, 0);
2393 /* Start the program. */
2394 while ((r = posix_spawn (&pid, cmd, &fa, &attr, argv,
2395 child->environment)) == EINTR)
2398 /* posix_spawn() doesn't provide sh fallback like exec() does; implement
2399 it here. POSIX doesn't specify the path to sh so use the default. */
2407 for (pp = argv; *pp != NULL; ++pp)
2410 nargv = xmalloc (sizeof (char *) * (l + 3));
2411 nargv[0] = (char *)default_shell;
2413 memcpy (&nargv[2], &argv[1], sizeof (char *) * l);
2415 while ((r = posix_spawn (&pid, nargv[0], &fa, &attr, nargv,
2416 child->environment)) == EINTR)
2424 /* Spawn succeeded but may fail later: remember the command. */
2425 free (child->cmd_name);
2427 child->cmd_name = cmd;
2429 child->cmd_name = xstrdup(cmd);
2433 posix_spawn_file_actions_destroy (&fa);
2434 posix_spawnattr_destroy (&attr);
2440 #endif /* USE_POSIX_SPAWN */
2443 OSS (error, NILF, "%s: %s", argv[0], strerror (r));
2447 #endif /* !AMIGA && !__MSDOS__ && !VMS */
2448 #endif /* !WINDOWS32 */
2451 /* Replace the current process with one running the command in ARGV,
2452 with environment ENVP. This function does not return. */
2454 /* EMX: This function returns the pid of the child process. */
2460 exec_command (char **argv, char **envp)
2463 /* to work around a problem with signals and execve: ignore them */
2465 signal (SIGCHLD,SIG_IGN);
2467 /* Run the program. */
2468 execve (argv[0], argv, envp);
2469 OSS (error, NILF, "%s: %s", argv[0], strerror (errno));
2470 _exit (EXIT_FAILURE);
2475 int exit_code = EXIT_FAILURE;
2477 /* make sure CreateProcess() has Path it needs */
2478 sync_Path_environment ();
2480 /* launch command */
2481 hPID = process_easy (argv, envp, -1, -1);
2483 /* make sure launch ok */
2484 if (hPID == INVALID_HANDLE_VALUE)
2487 fprintf (stderr, _("process_easy() failed to launch process (e=%ld)\n"),
2488 process_last_err (hPID));
2489 for (i = 0; argv[i]; i++)
2490 fprintf (stderr, "%s ", argv[i]);
2491 fprintf (stderr, _("\nCounted %d args in failed launch\n"), i);
2492 exit (EXIT_FAILURE);
2495 /* wait and reap last child */
2496 hWaitPID = process_wait_for_any (1, 0);
2499 /* was an error found on this process? */
2500 int err = process_last_err (hWaitPID);
2503 exit_code = process_exit_code (hWaitPID);
2506 fprintf (stderr, "make (e=%d, rc=%d): %s",
2507 err, exit_code, map_windows32_error_to_string (err));
2509 /* cleanup process */
2510 process_cleanup (hWaitPID);
2512 /* expect to find only last pid, warn about other pids reaped */
2513 if (hWaitPID == hPID)
2517 char *pidstr = xstrdup (pid2str ((pid_t)hWaitPID));
2520 _("make reaped child pid %s, still waiting for pid %s\n"),
2521 pidstr, pid2str ((pid_t)hPID));
2526 /* return child's exit code as our exit code */
2529 #else /* !WINDOWS32 */
2535 /* Be the user, permanently. */
2539 /* Run the program. */
2540 pid = spawnvpe (P_NOWAIT, argv[0], argv, envp);
2544 /* the file might have a strange shell extension */
2545 if (errno == ENOENT)
2549 /* Run the program. */
2551 execvp (argv[0], argv);
2553 # endif /* !__EMX__ */
2558 OSS (error, NILF, "%s: %s", argv[0], strerror (errno));
2562 /* The file was not a program. Try it as a shell script. */
2569 /* Do not use $SHELL from the environment */
2570 struct variable *p = lookup_variable ("SHELL", 5);
2576 shell = getenv ("SHELL");
2579 shell = default_shell;
2582 while (argv[argc] != 0)
2590 new_argv = alloca ((1 + argc + 1) * sizeof (char *));
2591 new_argv[0] = (char *)shell;
2602 new_argv[i] = argv[0];
2605 new_argv[i + argc] = argv[argc];
2610 pid = spawnvpe (P_NOWAIT, shell, new_argv, envp);
2614 execvp (shell, new_argv);
2616 OSS (error, NILF, "%s: %s", new_argv[0], strerror (errno));
2622 /* this nasty error was driving me nuts :-( */
2623 O (error, NILF, _("spawnvpe: environment space might be exhausted"));
2628 OSS (error, NILF, "%s: %s", argv[0], strerror (errno));
2637 #endif /* !WINDOWS32 */
2640 #else /* On Amiga */
2642 exec_command (char **argv)
2647 void clean_tmp (void)
2649 DeleteFile (amiga_bname);
2652 #endif /* On Amiga */
2655 /* Figure out the argument list necessary to run LINE as a command. Try to
2656 avoid using a shell. This routine handles only ' quoting, and " quoting
2657 when no backslash, $ or ' characters are seen in the quotes. Starting
2658 quotes may be escaped with a backslash. If any of the characters in
2659 sh_chars is seen, or any of the builtin commands listed in sh_cmds
2660 is the first word of a line, the shell is used.
2662 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
2663 If *RESTP is NULL, newlines will be ignored.
2665 SHELL is the shell to use, or nil to use the default shell.
2666 IFS is the value of $IFS, or nil (meaning the default).
2668 FLAGS is the value of lines_flags for this command line. It is
2669 used in the WINDOWS32 port to check whether + or $(MAKE) were found
2670 in this command line, in which case the effect of just_print_flag
2674 construct_command_argv_internal (char *line, char **restp, const char *shell,
2675 const char *shellflags, const char *ifs,
2676 int flags, char **batch_filename UNUSED)
2679 /* MSDOS supports both the stock DOS shell and ports of Unixy shells.
2680 We call 'system' for anything that requires ''slow'' processing,
2681 because DOS shells are too dumb. When $SHELL points to a real
2682 (unix-style) shell, 'system' just calls it to do everything. When
2683 $SHELL points to a DOS shell, 'system' does most of the work
2684 internally, calling the shell only for its internal commands.
2685 However, it looks on the $PATH first, so you can e.g. have an
2686 external command named 'mkdir'.
2688 Since we call 'system', certain characters and commands below are
2689 actually not specific to COMMAND.COM, but to the DJGPP implementation
2690 of 'system'. In particular:
2692 The shell wildcard characters are in DOS_CHARS because they will
2693 not be expanded if we call the child via 'spawnXX'.
2695 The ';' is in DOS_CHARS, because our 'system' knows how to run
2696 multiple commands on a single line.
2698 DOS_CHARS also include characters special to 4DOS/NDOS, so we
2699 won't have to tell one from another and have one more set of
2700 commands and special characters. */
2701 static const char *sh_chars_dos = "*?[];|<>%^&()";
2702 static const char *sh_cmds_dos[] =
2703 { "break", "call", "cd", "chcp", "chdir", "cls", "copy", "ctty", "date",
2704 "del", "dir", "echo", "erase", "exit", "for", "goto", "if", "md",
2705 "mkdir", "path", "pause", "prompt", "rd", "rmdir", "rem", "ren",
2706 "rename", "set", "shift", "time", "type", "ver", "verify", "vol", ":",
2709 static const char *sh_chars_sh = "#;\"*?[]&|<>(){}$`^";
2710 static const char *sh_cmds_sh[] =
2711 { "cd", "echo", "eval", "exec", "exit", "login", "logout", "set", "umask",
2712 "wait", "while", "for", "case", "if", ":", ".", "break", "continue",
2713 "export", "read", "readonly", "shift", "times", "trap", "switch",
2714 "unset", "ulimit", "command", 0 };
2716 const char *sh_chars;
2717 const char **sh_cmds;
2719 #elif defined (__EMX__)
2720 static const char *sh_chars_dos = "*?[];|<>%^&()";
2721 static const char *sh_cmds_dos[] =
2722 { "break", "call", "cd", "chcp", "chdir", "cls", "copy", "ctty", "date",
2723 "del", "dir", "echo", "erase", "exit", "for", "goto", "if", "md",
2724 "mkdir", "path", "pause", "prompt", "rd", "rmdir", "rem", "ren",
2725 "rename", "set", "shift", "time", "type", "ver", "verify", "vol", ":",
2728 static const char *sh_chars_os2 = "*?[];|<>%^()\"'&";
2729 static const char *sh_cmds_os2[] =
2730 { "call", "cd", "chcp", "chdir", "cls", "copy", "date", "del", "detach",
2731 "dir", "echo", "endlocal", "erase", "exit", "for", "goto", "if", "keys",
2732 "md", "mkdir", "move", "path", "pause", "prompt", "rd", "rem", "ren",
2733 "rename", "rmdir", "set", "setlocal", "shift", "start", "time", "type",
2734 "ver", "verify", "vol", ":", 0 };
2736 static const char *sh_chars_sh = "#;\"*?[]&|<>(){}$`^~'";
2737 static const char *sh_cmds_sh[] =
2738 { "echo", "cd", "eval", "exec", "exit", "login", "logout", "set", "umask",
2739 "wait", "while", "for", "case", "if", ":", ".", "break", "continue",
2740 "export", "read", "readonly", "shift", "times", "trap", "switch",
2741 "unset", "command", 0 };
2743 const char *sh_chars;
2744 const char **sh_cmds;
2746 #elif defined (_AMIGA)
2747 static const char *sh_chars = "#;\"|<>()?*$`";
2748 static const char *sh_cmds[] =
2749 { "cd", "eval", "if", "delete", "echo", "copy", "rename", "set", "setenv",
2750 "date", "makedir", "skip", "else", "endif", "path", "prompt", "unset",
2751 "unsetenv", "version", "command", 0 };
2753 #elif defined (WINDOWS32)
2754 /* We used to have a double quote (") in sh_chars_dos[] below, but
2755 that caused any command line with quoted file names be run
2756 through a temporary batch file, which introduces command-line
2757 limit of 4K charcaters imposed by cmd.exe. Since CreateProcess
2758 can handle quoted file names just fine, removing the quote lifts
2759 the limit from a very frequent use case, because using quoted
2760 file names is commonplace on MS-Windows. */
2761 static const char *sh_chars_dos = "|&<>";
2762 static const char *sh_cmds_dos[] =
2763 { "assoc", "break", "call", "cd", "chcp", "chdir", "cls", "color", "copy",
2764 "ctty", "date", "del", "dir", "echo", "echo.", "endlocal", "erase",
2765 "exit", "for", "ftype", "goto", "if", "if", "md", "mkdir", "move",
2766 "path", "pause", "prompt", "rd", "rem", "ren", "rename", "rmdir",
2767 "set", "setlocal", "shift", "time", "title", "type", "ver", "verify",
2770 static const char *sh_chars_sh = "#;\"*?[]&|<>(){}$`^";
2771 static const char *sh_cmds_sh[] =
2772 { "cd", "eval", "exec", "exit", "login", "logout", "set", "umask", "wait",
2773 "while", "for", "case", "if", ":", ".", "break", "continue", "export",
2774 "read", "readonly", "shift", "times", "trap", "switch", "test", "command",
2775 #ifdef BATCH_MODE_ONLY_SHELL
2780 const char *sh_chars;
2781 const char **sh_cmds;
2782 #elif defined(__riscos__)
2783 static const char *sh_chars = "";
2784 static const char *sh_cmds[] = { 0 };
2785 #else /* must be UNIX-ish */
2786 static const char *sh_chars = "#;\"*?[]&|<>(){}$`^~!";
2787 static const char *sh_cmds[] =
2788 { ".", ":", "alias", "bg", "break", "case", "cd", "command", "continue",
2789 "eval", "exec", "exit", "export", "fc", "fg", "for", "getopts", "hash",
2790 "if", "jobs", "login", "logout", "read", "readonly", "return", "set",
2791 "shift", "test", "times", "trap", "type", "ulimit", "umask", "unalias",
2792 "unset", "wait", "while", 0 };
2794 # ifdef HAVE_DOS_PATHS
2795 /* This is required if the MSYS/Cygwin ports (which do not define
2796 WINDOWS32) are compiled with HAVE_DOS_PATHS defined, which uses
2797 sh_chars_sh directly (see below). The value must be identical
2798 to that of sh_chars immediately above. */
2799 static const char *sh_chars_sh = "#;\"*?[]&|<>(){}$`^~!";
2800 # endif /* HAVE_DOS_PATHS */
2810 int instring, word_has_equals, seen_nonequals, last_argument_was_empty;
2811 char **new_argv = 0;
2818 sh_cmds = sh_cmds_dos;
2819 sh_chars = sh_chars_dos;
2823 sh_cmds = sh_cmds_sh;
2824 sh_chars = sh_chars_sh;
2826 #endif /* WINDOWS32 */
2831 /* Make sure not to bother processing an empty line but stop at newline. */
2832 while (ISBLANK (*line))
2837 if (shellflags == 0)
2838 shellflags = posix_pedantic ? "-ec" : "-c";
2840 /* See if it is safe to parse commands internally. */
2842 shell = default_shell;
2844 else if (strcmp (shell, default_shell))
2846 char *s1 = _fullpath (NULL, shell, 0);
2847 char *s2 = _fullpath (NULL, default_shell, 0);
2849 slow_flag = strcmp ((s1 ? s1 : ""), (s2 ? s2 : ""));
2856 #else /* not WINDOWS32 */
2857 #if defined (__MSDOS__) || defined (__EMX__)
2858 else if (strcasecmp (shell, default_shell))
2860 extern int _is_unixy_shell (const char *_path);
2862 DB (DB_BASIC, (_("$SHELL changed (was '%s', now '%s')\n"),
2863 default_shell, shell));
2864 unixy_shell = _is_unixy_shell (shell);
2865 /* we must allocate a copy of shell: construct_command_argv() will free
2866 * shell after this function returns. */
2867 default_shell = xstrdup (shell);
2871 sh_chars = sh_chars_sh;
2872 sh_cmds = sh_cmds_sh;
2876 sh_chars = sh_chars_dos;
2877 sh_cmds = sh_cmds_dos;
2879 if (_osmode == OS2_MODE)
2881 sh_chars = sh_chars_os2;
2882 sh_cmds = sh_cmds_os2;
2886 #else /* !__MSDOS__ */
2887 else if (strcmp (shell, default_shell))
2889 #endif /* !__MSDOS__ && !__EMX__ */
2890 #endif /* not WINDOWS32 */
2893 for (cap = ifs; *cap != '\0'; ++cap)
2894 if (*cap != ' ' && *cap != '\t' && *cap != '\n')
2898 if (shellflags[0] != '-'
2899 || ((shellflags[1] != 'c' || shellflags[2] != '\0')
2900 && (shellflags[1] != 'e' || shellflags[2] != 'c' || shellflags[3] != '\0')))
2903 i = strlen (line) + 1;
2905 /* More than 1 arg per character is impossible. */
2906 new_argv = xmalloc (i * sizeof (char *));
2908 /* All the args can fit in a buffer as big as LINE is. */
2909 ap = new_argv[0] = argstr = xmalloc (i);
2914 /* I is how many complete arguments have been found. */
2916 instring = word_has_equals = seen_nonequals = last_argument_was_empty = 0;
2917 for (p = line; *p != '\0'; ++p)
2923 /* Inside a string, just copy any char except a closing quote
2924 or a backslash-newline combination. */
2928 if (ap == new_argv[0] || *(ap-1) == '\0')
2929 last_argument_was_empty = 1;
2931 else if (*p == '\\' && p[1] == '\n')
2933 /* Backslash-newline is handled differently depending on what
2934 kind of string we're in: inside single-quoted strings you
2935 keep them; in double-quoted strings they disappear. For
2936 DOS/Windows/OS2, if we don't have a POSIX shell, we keep the
2937 pre-POSIX behavior of removing the backslash-newline. */
2939 #if defined (__MSDOS__) || defined (__EMX__) || defined (WINDOWS32)
2950 else if (*p == '\n' && restp != NULL)
2952 /* End of the command line. */
2956 /* Backslash, $, and ` are special inside double quotes.
2957 If we see any of those, punt.
2958 But on MSDOS, if we use COMMAND.COM, double and single
2959 quotes have the same effect. */
2960 else if (instring == '"' && strchr ("\\$`", *p) != 0 && unixy_shell)
2963 /* Quoted wildcard characters must be passed quoted to the
2964 command, so give up the fast route. */
2965 else if (instring == '"' && strchr ("*?", *p) != 0 && !unixy_shell)
2967 else if (instring == '"' && strncmp (p, "\\\"", 2) == 0)
2973 else if (strchr (sh_chars, *p) != 0)
2974 /* Not inside a string, but it's a special char. */
2976 else if (one_shell && *p == '\n')
2977 /* In .ONESHELL mode \n is a separator like ; or && */
2980 else if (*p == '.' && p[1] == '.' && p[2] == '.' && p[3] != '.')
2981 /* '...' is a wildcard in DJGPP. */
2985 /* Not a special char. */
2989 /* Equals is a special character in leading words before the
2990 first word with no equals sign in it. This is not the case
2991 with sh -k, but we never get here when using nonstandard
2993 if (! seen_nonequals && unixy_shell)
2995 word_has_equals = 1;
3000 /* Backslash-newline has special case handling, ref POSIX.
3001 We're in the fastpath, so emulate what the shell would do. */
3004 /* Throw out the backslash and newline. */
3007 /* At the beginning of the argument, skip any whitespace other
3008 than newline before the start of the next word. */
3009 if (ap == new_argv[i])
3010 while (ISBLANK (p[1]))
3014 /* Backslash before whitespace is not special if our shell
3016 else if (ISSPACE (p[1]) && !unixy_shell)
3022 else if (p[1] != '\0')
3024 #ifdef HAVE_DOS_PATHS
3025 /* Only remove backslashes before characters special to Unixy
3026 shells. All other backslashes are copied verbatim, since
3027 they are probably DOS-style directory separators. This
3028 still leaves a small window for problems, but at least it
3029 should work for the vast majority of naive users. */
3032 /* A dot is only special as part of the "..."
3034 if (strneq (p + 1, ".\\.\\.", 5))
3042 if (p[1] != '\\' && p[1] != '\''
3044 && strchr (sh_chars_sh, p[1]) == 0)
3045 /* back up one notch, to copy the backslash */
3047 #endif /* HAVE_DOS_PATHS */
3049 /* Copy and skip the following char. */
3062 /* End of the command line. */
3067 /* Newlines are not special. */
3073 /* We have the end of an argument.
3074 Terminate the text of the argument. */
3077 last_argument_was_empty = 0;
3079 /* Update SEEN_NONEQUALS, which tells us if every word
3080 heretofore has contained an '='. */
3081 seen_nonequals |= ! word_has_equals;
3082 if (word_has_equals && ! seen_nonequals)
3083 /* An '=' in a word before the first
3084 word without one is magical. */
3086 word_has_equals = 0; /* Prepare for the next word. */
3088 /* If this argument is the command name,
3089 see if it is a built-in shell command.
3090 If so, have the shell handle it. */
3094 for (j = 0; sh_cmds[j] != 0; ++j)
3096 if (streq (sh_cmds[j], new_argv[0]))
3098 #if defined(__EMX__) || defined(WINDOWS32)
3099 /* Non-Unix shells are case insensitive. */
3101 && strcasecmp (sh_cmds[j], new_argv[0]) == 0)
3107 /* Skip whitespace chars, but not newlines. */
3108 while (ISBLANK (p[1]))
3120 /* Let the shell deal with an unterminated quote. */
3123 /* Terminate the last argument and the argument list. */
3126 if (new_argv[i][0] != '\0' || last_argument_was_empty)
3133 for (j = 0; sh_cmds[j] != 0; ++j)
3134 if (streq (sh_cmds[j], new_argv[0]))
3138 if (new_argv[0] == 0)
3140 /* Line was empty. */
3149 /* We must use the shell. */
3153 /* Free the old argument list we were working on. */
3159 execute_by_shell = 1; /* actually, call 'system' if shell isn't unixy */
3168 buffer = xmalloc (strlen (line)+1);
3171 for (dptr=buffer; *ptr; )
3173 if (*ptr == '\\' && ptr[1] == '\n')
3175 else if (*ptr == '@') /* Kludge: multiline commands */
3185 new_argv = xmalloc (2 * sizeof (char *));
3186 new_argv[0] = buffer;
3189 #else /* Not Amiga */
3192 * Not eating this whitespace caused things like
3196 * which gave the shell fits. I think we have to eat
3197 * whitespace here, but this code should be considered
3198 * suspicious if things start failing....
3201 /* Make sure not to bother processing an empty line. */
3205 #endif /* WINDOWS32 */
3208 /* SHELL may be a multi-word command. Construct a command line
3209 "$(SHELL) $(.SHELLFLAGS) LINE", with all special chars in LINE escaped.
3210 Then recurse, expanding this command line to get the final
3214 size_t shell_len = strlen (shell);
3215 size_t line_len = strlen (line);
3216 size_t sflags_len = shellflags ? strlen (shellflags) : 0;
3218 char *command_ptr = NULL; /* used for batch_mode_shell mode */
3221 # ifdef __EMX__ /* is this necessary? */
3222 if (!unixy_shell && shellflags)
3223 shellflags[0] = '/'; /* "/c" */
3226 /* In .ONESHELL mode we are allowed to throw the entire current
3227 recipe string at a single shell and trust that the user
3228 has configured the shell and shell flags, and formatted
3229 the string, appropriately. */
3232 /* If the shell is Bourne compatible, we must remove and ignore
3233 interior special chars [@+-] because they're meaningless to
3234 the shell itself. If, however, we're in .ONESHELL mode and
3235 have changed SHELL to something non-standard, we should
3236 leave those alone because they could be part of the
3237 script. In this case we must also leave in place
3238 any leading [@+-] for the same reason. */
3240 /* Remove and ignore interior prefix chars [@+-] because they're
3241 meaningless given a single shell. */
3242 #if defined __MSDOS__ || defined (__EMX__)
3243 if (unixy_shell) /* the test is complicated and we already did it */
3245 if (is_bourne_compatible_shell (shell)
3247 /* If we didn't find any sh.exe, don't behave is if we did! */
3248 && !no_default_sh_exe
3253 const char *f = line;
3256 /* Copy the recipe, removing and ignoring interior prefix chars
3257 [@+-]: they're meaningless in .ONESHELL mode. */
3258 while (f[0] != '\0')
3262 /* This is the start of a new recipe line. Skip whitespace
3263 and prefix characters but not newlines. */
3264 while (ISBLANK (*f) || *f == '-' || *f == '@' || *f == '+')
3267 /* Copy until we get to the next logical recipe line. */
3275 /* On unescaped newline, we're done with this line. */
3276 if (f[-1] == '\n' && ! esc)
3279 /* Something else: reset the escape sequence. */
3287 else /* non-Posix shell (cmd.exe etc.) */
3289 const char *f = line;
3294 int id = GetCurrentProcessId ();
3297 /* Generate a file name for the temporary batch file. */
3298 sprintf (fbuf, "make%d", id);
3299 *batch_filename = create_batch_file (fbuf, 0, &temp_fd);
3300 DB (DB_JOBS, (_("Creating temporary batch file %s\n"),
3303 /* Create a FILE object for the batch file, and write to it the
3304 commands to be executed. Put the batch file in TEXT mode. */
3305 _setmode (temp_fd, _O_TEXT);
3306 batch = _fdopen (temp_fd, "wt");
3307 fputs ("@echo off\n", batch);
3308 DB (DB_JOBS, (_("Batch file contents:\n\t@echo off\n")));
3310 /* Copy the recipe, removing and ignoring interior prefix chars
3311 [@+-]: they're meaningless in .ONESHELL mode. */
3314 /* This is the start of a new recipe line. Skip whitespace
3315 and prefix characters but not newlines. */
3316 while (ISBLANK (*f) || *f == '-' || *f == '@' || *f == '+')
3319 /* Copy until we get to the next logical recipe line. */
3322 /* Remove the escaped newlines in the command, and the
3323 blanks that follow them. Windows shells cannot handle
3324 escaped newlines. */
3325 if (*f == '\\' && f[1] == '\n')
3328 while (ISBLANK (*f))
3332 /* On an unescaped newline, we're done with this
3337 /* Write another line into the batch file. */
3342 fputs (tstart, batch);
3343 DB (DB_JOBS, ("\t%s", tstart));
3348 DB (DB_JOBS, ("\n"));
3351 /* Create an argv list for the shell command line that
3352 will run the batch file. */
3353 new_argv = xmalloc (2 * sizeof (char *));
3354 new_argv[0] = xstrdup (*batch_filename);
3358 #endif /* WINDOWS32 */
3359 /* Create an argv list for the shell command line. */
3363 new_argv = xmalloc ((4 + sflags_len/2) * sizeof (char *));
3364 new_argv[n++] = xstrdup (shell);
3366 /* Chop up the shellflags (if any) and assign them. */
3368 new_argv[n++] = xstrdup ("");
3371 const char *s = shellflags;
3374 while ((t = find_next_token (&s, &len)) != 0)
3375 new_argv[n++] = xstrndup (t, len);
3378 /* Set the command to invoke. */
3379 new_argv[n++] = line;
3380 new_argv[n++] = NULL;
3385 new_line = xmalloc ((shell_len*2) + 1 + sflags_len + 1
3386 + (line_len*2) + 1);
3388 /* Copy SHELL, escaping any characters special to the shell. If
3389 we don't escape them, construct_command_argv_internal will
3390 recursively call itself ad nauseam, or until stack overflow,
3391 whichever happens first. */
3392 for (cp = shell; *cp != '\0'; ++cp)
3394 if (strchr (sh_chars, *cp) != 0)
3400 memcpy (ap, shellflags, sflags_len);
3406 for (p = line; *p != '\0'; ++p)
3408 if (restp != NULL && *p == '\n')
3413 else if (*p == '\\' && p[1] == '\n')
3415 /* POSIX says we keep the backslash-newline. If we don't have a
3416 POSIX shell on DOS/Windows/OS2, mimic the pre-POSIX behavior
3417 and remove the backslash/newline. */
3418 #if defined (__MSDOS__) || defined (__EMX__) || defined (WINDOWS32)
3419 # define PRESERVE_BSNL unixy_shell
3421 # define PRESERVE_BSNL 1
3426 /* Only non-batch execution needs another backslash,
3427 because it will be passed through a recursive
3428 invocation of this function. */
3429 if (!batch_mode_shell)
3437 /* DOS shells don't know about backslash-escaping. */
3438 if (unixy_shell && !batch_mode_shell &&
3439 (*p == '\\' || *p == '\'' || *p == '"'
3441 || strchr (sh_chars, *p) != 0))
3444 else if (unixy_shell && strneq (p, "...", 3))
3446 /* The case of '...' wildcard again. */
3447 strcpy (ap, "\\.\\.\\");
3454 if (ap == new_line + shell_len + sflags_len + 2)
3456 /* Line was empty. */
3463 /* Some shells do not work well when invoked as 'sh -c xxx' to run a
3464 command line (e.g. Cygnus GNUWIN32 sh.exe on WIN32 systems). In these
3465 cases, run commands via a script file. */
3466 if (just_print_flag && !(flags & COMMANDS_RECURSE))
3468 /* Need to allocate new_argv, although it's unused, because
3469 start_job_command will want to free it and its 0'th element. */
3470 new_argv = xmalloc (2 * sizeof (char *));
3471 new_argv[0] = xstrdup ("");
3474 else if ((no_default_sh_exe || batch_mode_shell) && batch_filename)
3478 int id = GetCurrentProcessId ();
3481 /* create a file name */
3482 sprintf (fbuf, "make%d", id);
3483 *batch_filename = create_batch_file (fbuf, unixy_shell, &temp_fd);
3485 DB (DB_JOBS, (_("Creating temporary batch file %s\n"),
3488 /* Create a FILE object for the batch file, and write to it the
3489 commands to be executed. Put the batch file in TEXT mode. */
3490 _setmode (temp_fd, _O_TEXT);
3491 batch = _fdopen (temp_fd, "wt");
3493 fputs ("@echo off\n", batch);
3494 fputs (command_ptr, batch);
3495 fputc ('\n', batch);
3497 DB (DB_JOBS, (_("Batch file contents:%s\n\t%s\n"),
3498 !unixy_shell ? "\n\t@echo off" : "", command_ptr));
3501 new_argv = xmalloc (3 * sizeof (char *));
3504 new_argv[0] = xstrdup (shell);
3505 new_argv[1] = *batch_filename; /* only argv[0] gets freed later */
3509 new_argv[0] = xstrdup (*batch_filename);
3515 #endif /* WINDOWS32 */
3518 new_argv = construct_command_argv_internal (new_line, 0, 0, 0, 0,
3522 else if (!unixy_shell)
3524 /* new_line is local, must not be freed therefore
3525 We use line here instead of new_line because we run the shell
3527 size_t line_len = strlen (line);
3530 memcpy (new_line, line, line_len + 1);
3531 /* Replace all backslash-newline combination and also following tabs.
3532 Important: stop at the first '\n' because that's what the loop above
3533 did. The next line starting at restp[0] will be executed during the
3534 next call of this function. */
3535 while (*q != '\0' && *q != '\n')
3537 if (q[0] == '\\' && q[1] == '\n')
3538 q += 2; /* remove '\\' and '\n' */
3544 # ifndef NO_CMD_DEFAULT
3545 if (strnicmp (new_line, "echo", 4) == 0
3546 && (new_line[4] == ' ' || new_line[4] == '\t'))
3548 /* the builtin echo command: handle it separately */
3549 size_t echo_len = line_len - 5;
3550 char *echo_line = new_line + 5;
3552 /* special case: echo 'x="y"'
3553 cmd works this way: a string is printed as is, i.e., no quotes
3554 are removed. But autoconf uses a command like echo 'x="y"' to
3555 determine whether make works. autoconf expects the output x="y"
3556 so we will do exactly that.
3557 Note: if we do not allow cmd to be the default shell
3558 we do not need this kind of voodoo */
3559 if (echo_line[0] == '\''
3560 && echo_line[echo_len - 1] == '\''
3561 && strncmp (echo_line + 1, "ac_maketemp=",
3562 strlen ("ac_maketemp=")) == 0)
3564 /* remove the enclosing quotes */
3565 memmove (echo_line, echo_line + 1, echo_len - 2);
3566 echo_line[echo_len - 2] = '\0';
3572 /* Let the shell decide what to do. Put the command line into the
3573 2nd command line argument and hope for the best ;-) */
3574 size_t sh_len = strlen (shell);
3576 /* exactly 3 arguments + NULL */
3577 new_argv = xmalloc (4 * sizeof (char *));
3578 /* Exactly strlen(shell) + strlen("/c") + strlen(line) + 3 times
3579 the trailing '\0' */
3580 new_argv[0] = xmalloc (sh_len + line_len + 5);
3581 memcpy (new_argv[0], shell, sh_len + 1);
3582 new_argv[1] = new_argv[0] + sh_len + 1;
3583 memcpy (new_argv[1], "/c", 3);
3584 new_argv[2] = new_argv[1] + 3;
3585 memcpy (new_argv[2], new_line, line_len + 1);
3589 #elif defined(__MSDOS__)
3592 /* With MSDOS shells, we must construct the command line here
3593 instead of recursively calling ourselves, because we
3594 cannot backslash-escape the special characters (see above). */
3595 new_argv = xmalloc (sizeof (char *));
3596 line_len = strlen (new_line) - shell_len - sflags_len - 2;
3597 new_argv[0] = xmalloc (line_len + 1);
3598 strncpy (new_argv[0],
3599 new_line + shell_len + sflags_len + 2, line_len);
3600 new_argv[0][line_len] = '\0';
3604 fatal (NILF, CSTRLEN (__FILE__) + INTSTR_LENGTH,
3605 _("%s (line %d) Bad shell context (!unixy && !batch_mode_shell)\n"),
3606 __FILE__, __LINE__);
3611 #endif /* ! AMIGA */
3617 /* Figure out the argument list necessary to run LINE as a command. Try to
3618 avoid using a shell. This routine handles only ' quoting, and " quoting
3619 when no backslash, $ or ' characters are seen in the quotes. Starting
3620 quotes may be escaped with a backslash. If any of the characters in
3621 sh_chars is seen, or any of the builtin commands listed in sh_cmds
3622 is the first word of a line, the shell is used.
3624 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
3625 If *RESTP is NULL, newlines will be ignored.
3627 FILE is the target whose commands these are. It is used for
3628 variable expansion for $(SHELL) and $(IFS). */
3631 construct_command_argv (char *line, char **restp, struct file *file,
3632 int cmd_flags, char **batch_filename)
3634 char *shell, *ifs, *shellflags;
3638 /* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
3639 int save = warn_undefined_variables_flag;
3640 warn_undefined_variables_flag = 0;
3642 shell = allocated_variable_expand_for_file ("$(SHELL)", file);
3645 * Convert to forward slashes so that construct_command_argv_internal()
3650 char *p = w32ify (shell, 0);
3656 static const char *unixroot = NULL;
3657 static const char *last_shell = "";
3658 static int init = 0;
3661 unixroot = getenv ("UNIXROOT");
3662 /* unixroot must be NULL or not empty */
3663 if (unixroot && unixroot[0] == '\0') unixroot = NULL;
3667 /* if we have an unixroot drive and if shell is not default_shell
3668 (which means it's either cmd.exe or the test has already been
3669 performed) and if shell is an absolute path without drive letter,
3670 try whether it exists e.g.: if "/bin/sh" does not exist use
3671 "$UNIXROOT/bin/sh" instead. */
3672 if (unixroot && shell && strcmp (shell, last_shell) != 0
3673 && (shell[0] == '/' || shell[0] == '\\'))
3675 /* trying a new shell, check whether it exists */
3676 size_t size = strlen (shell);
3677 char *buf = xmalloc (size + 7);
3678 memcpy (buf, shell, size);
3679 memcpy (buf + size, ".exe", 5); /* including the trailing '\0' */
3680 if (access (shell, F_OK) != 0 && access (buf, F_OK) != 0)
3682 /* try the same for the unixroot drive */
3683 memmove (buf + 2, buf, size + 5);
3684 buf[0] = unixroot[0];
3685 buf[1] = unixroot[1];
3686 if (access (buf, F_OK) == 0)
3687 /* we have found a shell! */
3697 #endif /* __EMX__ */
3699 shellflags = allocated_variable_expand_for_file ("$(.SHELLFLAGS)", file);
3700 ifs = allocated_variable_expand_for_file ("$(IFS)", file);
3702 warn_undefined_variables_flag = save;
3705 argv = construct_command_argv_internal (line, restp, shell, shellflags, ifs,
3706 cmd_flags, batch_filename);
3715 #if !defined(HAVE_DUP2) && !defined(_AMIGA)
3717 dup2 (int old, int new)
3722 EINTRLOOP (fd, dup (old));
3732 #endif /* !HAVE_DUP2 && !_AMIGA */
3734 /* On VMS systems, include special VMS functions. */
3737 #include "vmsjobs.c"