1 /* Job execution and handling for GNU Make.
2 Copyright (C) 1988-2013 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3 of the License, or (at your option) any later
10 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along with
15 this program. If not, see <http://www.gnu.org/licenses/>. */
30 /* Default shell to use. */
34 char *default_shell = "sh.exe";
35 int no_default_sh_exe = 1;
36 int batch_mode_shell = 1;
39 #elif defined (_AMIGA)
41 char default_shell[] = "";
42 extern int MyExecute (char **);
43 int batch_mode_shell = 0;
45 #elif defined (__MSDOS__)
47 /* The default shell is a pointer so we can change it if Makefile
48 says so. It is without an explicit path so we get a chance
49 to search the $PATH for it (since MSDOS doesn't have standard
50 directories we could trust). */
51 char *default_shell = "command.com";
52 int batch_mode_shell = 0;
54 #elif defined (__EMX__)
56 char *default_shell = "/bin/sh";
57 int batch_mode_shell = 0;
62 char default_shell[] = "";
63 int batch_mode_shell = 0;
65 #elif defined (__riscos__)
67 char default_shell[] = "";
68 int batch_mode_shell = 0;
72 char default_shell[] = "/bin/sh";
73 int batch_mode_shell = 0;
79 static int execute_by_shell;
80 static int dos_pid = 123;
82 int dos_command_running;
83 #endif /* __MSDOS__ */
86 # include <proto/dos.h>
87 static int amiga_pid = 123;
88 static int amiga_status;
89 static char amiga_bname[32];
90 static int amiga_batch_file;
95 # include <processes.h>
98 # include <lib$routines.h>
99 static void vmsWaitForChildren (int *);
103 # include <windows.h>
105 # include <process.h>
106 # include "sub_proc.h"
108 # include "pathstuff.h"
109 # define WAIT_NOHANG 1
110 #endif /* WINDOWS32 */
113 # include <process.h>
116 #if defined (HAVE_SYS_WAIT_H) || defined (HAVE_UNION_WAIT)
117 # include <sys/wait.h>
121 # define WAIT_NOHANG(status) waitpid (-1, (status), WNOHANG)
122 #else /* Don't have waitpid. */
127 # define WAIT_NOHANG(status) wait3 ((status), WNOHANG, (struct rusage *) 0)
128 # endif /* Have wait3. */
129 #endif /* Have waitpid. */
131 #if !defined (wait) && !defined (POSIX)
135 #ifndef HAVE_UNION_WAIT
140 # define WTERMSIG(x) ((x) & 0x7f)
143 # define WCOREDUMP(x) ((x) & 0x80)
146 # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
149 # define WIFSIGNALED(x) (WTERMSIG (x) != 0)
152 # define WIFEXITED(x) (WTERMSIG (x) == 0)
155 #else /* Have 'union wait'. */
157 # define WAIT_T union wait
159 # define WTERMSIG(x) ((x).w_termsig)
162 # define WCOREDUMP(x) ((x).w_coredump)
165 # define WEXITSTATUS(x) ((x).w_retcode)
168 # define WIFSIGNALED(x) (WTERMSIG(x) != 0)
171 # define WIFEXITED(x) (WTERMSIG(x) == 0)
174 #endif /* Don't have 'union wait'. */
176 #if !defined(HAVE_UNISTD_H) && !defined(WINDOWS32)
188 /* Different systems have different requirements for pid_t.
189 Plus we have to support gettext string translation... Argh. */
193 static char pidstring[100];
194 #if defined(WINDOWS32) && (__GNUC__ > 3 || _MSC_VER > 1300)
195 /* %Id is only needed for 64-builds, which were not supported by
196 older versions of Windows compilers. */
197 sprintf (pidstring, "%Id", pid);
199 sprintf (pidstring, "%lu", (unsigned long) pid);
204 int getloadavg (double loadavg[], int nelem);
205 int start_remote_job (char **argv, char **envp, int stdin_fd, int *is_remote,
206 int *id_ptr, int *used_stdin);
207 int start_remote_job_p (int);
208 int remote_status (int *exit_code_ptr, int *signal_ptr, int *coredump_ptr,
211 RETSIGTYPE child_handler (int);
212 static void free_child (struct child *);
213 static void start_job_command (struct child *child);
214 static int load_too_high (void);
215 static int job_next_command (struct child *);
216 static int start_waiting_job (struct child *);
218 /* Chain of all live (or recently deceased) children. */
220 struct child *children = 0;
222 /* Number of children currently running. */
224 unsigned int job_slots_used = 0;
226 /* Nonzero if the 'good' standard input is in use. */
228 static int good_stdin_used = 0;
230 /* Chain of children waiting to run until the load average goes down. */
232 static struct child *waiting_jobs = 0;
234 /* Non-zero if we use a *real* shell (always so on Unix). */
238 /* Number of jobs started in the current second. */
240 unsigned long job_counter = 0;
242 /* Number of jobserver tokens this instance is currently using. */
244 unsigned int jobserver_tokens = 0;
249 * The macro which references this function is defined in makeint.h.
252 w32_kill (pid_t pid, int sig)
254 return ((process_kill ((HANDLE)pid, sig) == TRUE) ? 0 : -1);
257 /* This function creates a temporary file name with an extension specified
259 * Return an xmalloc'ed string of a newly created temp file and its
260 * file descriptor, or die. */
262 create_batch_file (char const *base, int unixy, int *fd)
264 const char *const ext = unixy ? "sh" : "bat";
265 const char *error_string = NULL;
266 char temp_path[MAXPATHLEN]; /* need to know its length */
267 unsigned path_size = GetTempPath (sizeof temp_path, temp_path);
269 /* The following variable is static so we won't try to reuse a name
270 that was generated a little while ago, because that file might
271 not be on disk yet, since we use FILE_ATTRIBUTE_TEMPORARY below,
272 which tells the OS it doesn't need to flush the cache to disk.
273 If the file is not yet on disk, we might think the name is
274 available, while it really isn't. This happens in parallel
275 builds, where Make doesn't wait for one job to finish before it
276 launches the next one. */
277 static unsigned uniq = 0;
278 static int second_loop = 0;
279 const unsigned sizemax = strlen (base) + strlen (ext) + 10;
283 path_size = GetCurrentDirectory (sizeof temp_path, temp_path);
288 if (uniq >= 0x10000 && !second_loop)
290 /* If we already had 64K batch files in this
291 process, make a second loop through the numbers,
292 looking for free slots, i.e. files that were
293 deleted in the meantime. */
297 while (path_size > 0 &&
298 path_size + sizemax < sizeof temp_path &&
299 !(uniq >= 0x10000 && second_loop))
301 unsigned size = sprintf (temp_path + path_size,
303 temp_path[path_size - 1] == '\\' ? "" : "\\",
305 HANDLE h = CreateFile (temp_path, /* file name */
306 GENERIC_READ | GENERIC_WRITE, /* desired access */
307 0, /* no share mode */
308 NULL, /* default security attributes */
309 CREATE_NEW, /* creation disposition */
310 FILE_ATTRIBUTE_NORMAL | /* flags and attributes */
311 FILE_ATTRIBUTE_TEMPORARY, /* we'll delete it */
312 NULL); /* no template file */
314 if (h == INVALID_HANDLE_VALUE)
316 const DWORD er = GetLastError ();
318 if (er == ERROR_FILE_EXISTS || er == ERROR_ALREADY_EXISTS)
321 if (uniq == 0x10000 && !second_loop)
328 /* the temporary path is not guaranteed to exist */
329 else if (path_is_dot == 0)
331 path_size = GetCurrentDirectory (sizeof temp_path, temp_path);
337 error_string = map_windows32_error_to_string (er);
343 const unsigned final_size = path_size + size + 1;
344 char *const path = xmalloc (final_size);
345 memcpy (path, temp_path, final_size);
346 *fd = _open_osfhandle ((intptr_t)h, 0);
351 for (p = path; (ch = *p) != 0; ++p)
355 return path; /* good return */
360 if (error_string == NULL)
361 error_string = _("Cannot create a temporary file\n");
362 fatal (NILF, error_string);
367 #endif /* WINDOWS32 */
370 /* returns whether path is assumed to be a unix like shell. */
372 _is_unixy_shell (const char *path)
374 /* list of non unix shells */
375 const char *known_os2shells[] = {
387 /* find the rightmost '/' or '\\' */
388 const char *name = strrchr (path, '/');
389 const char *p = strrchr (path, '\\');
392 if (name && p) /* take the max */
393 name = (name > p) ? name : p;
394 else if (p) /* name must be 0 */
396 else if (!name) /* name and p must be 0 */
399 if (*name == '/' || *name == '\\') name++;
402 while (known_os2shells[i] != NULL)
404 if (strcasecmp (name, known_os2shells[i]) == 0)
405 return 0; /* not a unix shell */
409 /* in doubt assume a unix like shell */
414 /* determines whether path looks to be a Bourne-like shell. */
416 is_bourne_compatible_shell (const char *path)
418 /* List of known POSIX (or POSIX-ish) shells. */
419 static const char *unix_shells[] = {
431 /* find the rightmost '/' or '\\' */
432 const char *name = strrchr (path, '/');
433 char *p = strrchr (path, '\\');
435 if (name && p) /* take the max */
436 name = (name > p) ? name : p;
437 else if (p) /* name must be 0 */
439 else if (!name) /* name and p must be 0 */
442 if (*name == '/' || *name == '\\')
445 /* this should be able to deal with extensions on Windows-like systems */
446 for (s = unix_shells; *s != NULL; ++s)
448 #if defined(WINDOWS32) || defined(__MSDOS__)
449 unsigned int len = strlen (*s);
450 if ((strlen (name) >= len && STOP_SET (name[len], MAP_DOT|MAP_NUL))
451 && strncasecmp (name, *s, len) == 0)
453 if (strcmp (name, *s) == 0)
455 return 1; /* a known unix-style shell */
458 /* if not on the list, assume it's not a Bourne-like shell */
463 /* Write an error message describing the exit status given in
464 EXIT_CODE, EXIT_SIG, and COREDUMP, for the target TARGET_NAME.
465 Append "(ignored)" if IGNORED is nonzero. */
468 child_error (struct child *child,
469 int exit_code, int exit_sig, int coredump, int ignored)
471 const char *pre = "*** ";
472 const char *post = "";
473 const char *dump = "";
474 const struct file *f = child->file;
475 const gmk_floc *flocp = &f->cmds->fileinfo;
478 if (ignored && silent_flag)
481 if (exit_sig && coredump)
482 dump = _(" (core dumped)");
487 post = _(" (ignored)");
494 char *a = alloca (strlen (flocp->filenm) + 1 + 11 + 1);
495 sprintf (a, "%s:%lu", flocp->filenm, flocp->lineno);
499 OUTPUT_SET (&child->output);
501 message (0, _("%s: recipe for target '%s' failed"), nm, f->name);
504 if ((exit_code & 1) != 0)
510 error (NILF, _("%s[%s] Error 0x%x%s"), pre, f->name, exit_code, post);
513 error (NILF, _("%s[%s] Error %d%s"), pre, f->name, exit_code, post);
516 const char *s = strsignal (exit_sig);
517 error (NILF, _("%s[%s] %s%s%s"), pre, f->name, s, dump, post);
525 /* Handle a dead child. This handler may or may not ever be installed.
527 If we're using the jobserver feature, we need it. First, installing it
528 ensures the read will interrupt on SIGCHLD. Second, we close the dup'd
529 read FD to ensure we don't enter another blocking read without reaping all
530 the dead children. In this case we don't need the dead_children count.
532 If we don't have either waitpid or wait3, then make is unreliable, but we
533 use the dead_children count to reap children as best we can. */
535 static unsigned int dead_children = 0;
538 child_handler (int sig UNUSED)
549 /* The signal handler must called only once! */
550 signal (SIGCHLD, SIG_DFL);
553 /* This causes problems if the SIGCHLD interrupts a printf().
554 DB (DB_JOBS, (_("Got a SIGCHLD; %u unreaped children.\n"), dead_children));
558 extern int shell_function_pid, shell_function_completed;
560 /* Reap all dead children, storing the returned status and the new command
561 state ('cs_finished') in the 'file' member of the 'struct child' for the
562 dead child, and removing the child from the chain. In addition, if BLOCK
563 nonzero, we block in this function until we've reaped at least one
564 complete child, waiting for it to die if necessary. If ERR is nonzero,
565 print an error message first. */
568 reap_children (int block, int err)
573 /* Initially, assume we have some. */
577 # define REAP_MORE reap_more
579 # define REAP_MORE dead_children
584 We have at least one child outstanding OR a shell function in progress,
586 We're blocking for a complete child OR there are more children to reap
588 we'll keep reaping children. */
590 while ((children != 0 || shell_function_pid != 0)
591 && (block || REAP_MORE))
595 int exit_code, exit_sig, coredump;
596 struct child *lastc, *c;
598 int any_remote, any_local;
603 static int printed = 0;
605 /* We might block for a while, so let the user know why.
606 Only print this message once no matter how many jobs are left. */
609 error (NILF, _("*** Waiting for unfinished jobs...."));
613 /* We have one less dead child to reap. As noted in
614 child_handler() above, this count is completely unimportant for
615 all modern, POSIX-y systems that support wait3() or waitpid().
616 The rest of this comment below applies only to early, broken
617 pre-POSIX systems. We keep the count only because... it's there...
619 The test and decrement are not atomic; if it is compiled into:
620 register = dead_children - 1;
621 dead_children = register;
622 a SIGCHLD could come between the two instructions.
623 child_handler increments dead_children.
624 The second instruction here would lose that increment. But the
625 only effect of dead_children being wrong is that we might wait
626 longer than necessary to reap a child, and lose some parallelism;
627 and we might print the "Waiting for unfinished jobs" message above
628 when not necessary. */
630 if (dead_children > 0)
634 any_local = shell_function_pid != 0;
635 for (c = children; c != 0; c = c->next)
637 any_remote |= c->remote;
638 any_local |= ! c->remote;
639 DB (DB_JOBS, (_("Live child %p (%s) PID %s %s\n"),
640 c, c->file->name, pid2str (c->pid),
641 c->remote ? _(" (remote)") : ""));
647 /* First, check for remote children. */
649 pid = remote_status (&exit_code, &exit_sig, &coredump, 0);
654 /* We got a remote child. */
658 /* A remote status command failed miserably. Punt. */
660 pfatal_with_name ("remote_status");
664 /* No remote children. Check for local children. */
665 #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
669 vmsWaitForChildren (&status);
674 pid = WAIT_NOHANG (&status);
677 EINTRLOOP(pid, wait (&status));
685 /* The wait*() failed miserably. Punt. */
686 pfatal_with_name ("wait");
690 /* We got a child exit; chop the status word up. */
691 exit_code = WEXITSTATUS (status);
692 exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0;
693 coredump = WCOREDUMP (status);
695 /* If we have started jobs in this second, remove one. */
701 /* No local children are dead. */
704 if (!block || !any_remote)
707 /* Now try a blocking wait for a remote child. */
708 pid = remote_status (&exit_code, &exit_sig, &coredump, 1);
710 goto remote_status_lose;
712 /* No remote children either. Finally give up. */
715 /* We got a remote child. */
718 #endif /* !__MSDOS__, !Amiga, !WINDOWS32. */
721 /* Life is very different on MSDOS. */
724 exit_code = WEXITSTATUS (status);
725 if (exit_code == 0xff)
727 exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0;
729 #endif /* __MSDOS__ */
733 status = amiga_status;
734 exit_code = amiga_status;
742 DWORD dwWaitStatus = 0;
747 /* Record the thread ID of the main process, so that we
748 could suspend it in the signal handler. */
751 hcTID = GetCurrentThread ();
752 hcPID = GetCurrentProcess ();
753 if (!DuplicateHandle (hcPID, hcTID, hcPID, &main_thread, 0,
754 FALSE, DUPLICATE_SAME_ACCESS))
756 DWORD e = GetLastError ();
758 "Determine main thread ID (Error %ld: %s)\n",
759 e, map_windows32_error_to_string (e));
762 DB (DB_VERBOSE, ("Main thread handle = %p\n", main_thread));
765 /* wait for anything to finish */
766 hPID = process_wait_for_any (block, &dwWaitStatus);
769 /* was an error found on this process? */
770 int werr = process_last_err (hPID);
773 exit_code = process_exit_code (hPID);
776 fprintf (stderr, "make (e=%d): %s", exit_code,
777 map_windows32_error_to_string (exit_code));
780 exit_sig = process_signal (hPID);
782 /* cleanup process */
783 process_cleanup (hPID);
787 else if (dwWaitStatus == WAIT_FAILED)
789 /* The WaitForMultipleObjects() failed miserably. Punt. */
790 pfatal_with_name ("WaitForMultipleObjects");
792 else if (dwWaitStatus == WAIT_TIMEOUT)
794 /* No child processes are finished. Give up waiting. */
801 #endif /* WINDOWS32 */
804 /* Check if this is the child of the 'shell' function. */
805 if (!remote && pid == shell_function_pid)
807 /* It is. Leave an indicator for the 'shell' function. */
808 if (exit_sig == 0 && exit_code == 127)
809 shell_function_completed = -1;
811 shell_function_completed = 1;
815 child_failed = exit_sig != 0 || exit_code != 0;
817 /* Search for a child matching the deceased one. */
819 for (c = children; c != 0; lastc = c, c = c->next)
820 if (c->pid == pid && c->remote == remote)
824 /* An unknown child died.
825 Ignore it; it was inherited from our invoker. */
828 DB (DB_JOBS, (child_failed
829 ? _("Reaping losing child %p PID %s %s\n")
830 : _("Reaping winning child %p PID %s %s\n"),
831 c, pid2str (c->pid), c->remote ? _(" (remote)") : ""));
833 if (c->sh_batch_file)
837 DB (DB_JOBS, (_("Cleaning up temp batch file %s\n"),
841 rm_status = remove (c->sh_batch_file);
843 DB (DB_JOBS, (_("Cleaning up temp batch file %s failed (%d)\n"),
844 c->sh_batch_file, errno));
846 /* all done with memory */
847 free (c->sh_batch_file);
848 c->sh_batch_file = NULL;
851 /* If this child had the good stdin, say it is now free. */
855 dontcare = c->dontcare;
857 if (child_failed && !c->noerror && !ignore_errors_flag)
859 /* The commands failed. Write an error message,
860 delete non-precious targets, and abort. */
861 static int delete_on_error = -1;
864 child_error (c, exit_code, exit_sig, coredump, 0);
866 c->file->update_status = us_failed;
867 if (delete_on_error == -1)
869 struct file *f = lookup_file (".DELETE_ON_ERROR");
870 delete_on_error = f != 0 && f->is_target;
872 if (exit_sig != 0 || delete_on_error)
873 delete_child_targets (c);
879 /* The commands failed, but we don't care. */
880 child_error (c, exit_code, exit_sig, coredump, 1);
884 /* If there are more commands to run, try to start them. */
885 if (job_next_command (c))
887 if (handling_fatal_signal)
889 /* Never start new commands while we are dying.
890 Since there are more commands that wanted to be run,
891 the target was not completely remade. So we treat
892 this as if a command had failed. */
893 c->file->update_status = us_failed;
897 #ifndef NO_OUTPUT_SYNC
898 /* If we're sync'ing per line, write the previous line's
899 output before starting the next one. */
900 if (output_sync == OUTPUT_SYNC_LINE)
901 output_dump (&c->output);
903 /* Check again whether to start remotely.
904 Whether or not we want to changes over time.
905 Also, start_remote_job may need state set up
906 by start_remote_job_p. */
907 c->remote = start_remote_job_p (0);
908 start_job_command (c);
909 /* Fatal signals are left blocked in case we were
910 about to put that child on the chain. But it is
911 already there, so it is safe for a fatal signal to
912 arrive now; it will clean up this child's targets. */
914 if (c->file->command_state == cs_running)
915 /* We successfully started the new command.
916 Loop to reap more children. */
920 if (c->file->update_status != us_success)
921 /* We failed to start the commands. */
922 delete_child_targets (c);
925 /* There are no more commands. We got through them all
926 without an unignored error. Now the target has been
927 successfully updated. */
928 c->file->update_status = us_success;
931 /* When we get here, all the commands for c->file are finished. */
933 #ifndef NO_OUTPUT_SYNC
934 /* Synchronize any remaining parallel output. */
935 output_dump (&c->output);
938 /* At this point c->file->update_status is success or failed. But
939 c->file->command_state is still cs_running if all the commands
940 ran; notice_finish_file looks for cs_running to tell it that
941 it's interesting to check the file's modtime again now. */
943 if (! handling_fatal_signal)
944 /* Notice if the target of the commands has been changed.
945 This also propagates its values for command_state and
946 update_status to its also_make files. */
947 notice_finished_file (c->file);
949 DB (DB_JOBS, (_("Removing child %p PID %s%s from chain.\n"),
950 c, pid2str (c->pid), c->remote ? _(" (remote)") : ""));
952 /* Block fatal signals while frobnicating the list, so that
953 children and job_slots_used are always consistent. Otherwise
954 a fatal signal arriving after the child is off the chain and
955 before job_slots_used is decremented would believe a child was
956 live and call reap_children again. */
959 /* There is now another slot open. */
960 if (job_slots_used > 0)
963 /* Remove the child from the chain and free it. */
967 lastc->next = c->next;
973 /* If the job failed, and the -k flag was not given, die,
974 unless we are already in the process of dying. */
975 if (!err && child_failed && !dontcare && !keep_going_flag &&
976 /* fatal_error_signal will die with the right signal. */
977 !handling_fatal_signal)
980 /* Only block for one child. */
987 /* Free the storage allocated for CHILD. */
990 free_child (struct child *child)
992 output_close (&child->output);
994 if (!jobserver_tokens)
995 fatal (NILF, "INTERNAL: Freeing child %p (%s) but no tokens left!\n",
996 child, child->file->name);
998 /* If we're using the jobserver and this child is not the only outstanding
999 job, put a token back into the pipe for it. */
1002 if (has_jobserver_semaphore () && jobserver_tokens > 1)
1004 if (! release_jobserver_semaphore ())
1006 DWORD err = GetLastError ();
1007 fatal (NILF, _("release jobserver semaphore: (Error %ld: %s)"),
1008 err, map_windows32_error_to_string (err));
1011 DB (DB_JOBS, (_("Released token for child %p (%s).\n"), child, child->file->name));
1014 if (job_fds[1] >= 0 && jobserver_tokens > 1)
1019 /* Write a job token back to the pipe. */
1021 EINTRLOOP (r, write (job_fds[1], &token, 1));
1023 pfatal_with_name (_("write jobserver"));
1025 DB (DB_JOBS, (_("Released token for child %p (%s).\n"),
1026 child, child->file->name));
1032 if (handling_fatal_signal) /* Don't bother free'ing if about to die. */
1035 if (child->command_lines != 0)
1037 register unsigned int i;
1038 for (i = 0; i < child->file->cmds->ncommand_lines; ++i)
1039 free (child->command_lines[i]);
1040 free (child->command_lines);
1043 if (child->environment != 0)
1045 register char **ep = child->environment;
1048 free (child->environment);
1055 extern sigset_t fatal_signal_set;
1062 (void) sigprocmask (SIG_BLOCK, &fatal_signal_set, (sigset_t *) 0);
1064 # ifdef HAVE_SIGSETMASK
1065 (void) sigblock (fatal_signal_mask);
1075 sigemptyset (&empty);
1076 sigprocmask (SIG_SETMASK, &empty, (sigset_t *) 0);
1080 #if defined(MAKE_JOBSERVER) && !defined(WINDOWS32)
1082 job_noop (int sig UNUSED)
1085 /* Set the child handler action flags to FLAGS. */
1087 set_child_handler_action_flags (int set_handler, int set_alarm)
1089 struct sigaction sa;
1092 /* The child handler must be turned off here. */
1093 signal (SIGCHLD, SIG_DFL);
1096 memset (&sa, '\0', sizeof sa);
1097 sa.sa_handler = child_handler;
1098 sa.sa_flags = set_handler ? 0 : SA_RESTART;
1100 sigaction (SIGCHLD, &sa, NULL);
1102 #if defined SIGCLD && SIGCLD != SIGCHLD
1103 sigaction (SIGCLD, &sa, NULL);
1108 /* If we're about to enter the read(), set an alarm to wake up in a
1109 second so we can check if the load has dropped and we can start more
1110 work. On the way out, turn off the alarm and set SIG_DFL. */
1111 alarm (set_handler ? 1 : 0);
1112 sa.sa_handler = set_handler ? job_noop : SIG_DFL;
1114 sigaction (SIGALRM, &sa, NULL);
1121 /* Start a job to run the commands specified in CHILD.
1122 CHILD is updated to reflect the commands and ID of the child process.
1124 NOTE: On return fatal signals are blocked! The caller is responsible
1125 for calling 'unblock_sigs', once the new child is safely on the chain so
1126 it can be cleaned up in the event of a fatal signal. */
1129 start_job_command (struct child *child)
1131 #if !defined(_AMIGA) && !defined(WINDOWS32)
1132 static int bad_stdin = -1;
1142 /* If we have a completely empty commandset, stop now. */
1143 if (!child->command_ptr)
1146 /* Combine the flags parsed for the line itself with
1147 the flags specified globally for this target. */
1148 flags = (child->file->command_flags
1149 | child->file->cmds->lines_flags[child->command_line - 1]);
1151 p = child->command_ptr;
1152 child->noerror = ((flags & COMMANDS_NOERROR) != 0);
1157 flags |= COMMANDS_SILENT;
1159 flags |= COMMANDS_RECURSE;
1162 else if (!isblank ((unsigned char)*p))
1167 /* Update the file's command flags with any new ones we found. We only
1168 keep the COMMANDS_RECURSE setting. Even this isn't 100% correct; we are
1169 now marking more commands recursive than should be in the case of
1170 multiline define/endef scripts where only one line is marked "+". In
1171 order to really fix this, we'll have to keep a lines_flags for every
1172 actual line, after expansion. */
1173 child->file->cmds->lines_flags[child->command_line - 1]
1174 |= flags & COMMANDS_RECURSE;
1176 /* POSIX requires that a recipe prefix after a backslash-newline should
1177 be ignored. Remove it now so the output is correct. */
1179 char prefix = child->file->cmds->recipe_prefix;
1185 if (p1[0] == '\n' && p1[1] == prefix)
1192 /* Figure out an argument list from this command line. */
1198 argv = construct_command_argv (p, &end, child->file,
1199 child->file->cmds->lines_flags[child->command_line - 1],
1200 &child->sh_batch_file);
1203 child->command_ptr = NULL;
1207 child->command_ptr = end;
1211 /* If -q was given, say that updating 'failed' if there was any text on the
1212 command line, or 'succeeded' otherwise. The exit status of 1 tells the
1213 user that -q is saying 'something to do'; the exit status for a random
1215 if (argv != 0 && question_flag && !(flags & COMMANDS_RECURSE))
1221 child->file->update_status = us_question;
1222 notice_finished_file (child->file);
1226 if (touch_flag && !(flags & COMMANDS_RECURSE))
1228 /* Go on to the next command. It might be the recursive one.
1229 We construct ARGV only to find the end of the command line. */
1244 execute_by_shell = 0; /* in case construct_command_argv sets it */
1246 /* This line has no commands. Go to the next. */
1247 if (job_next_command (child))
1248 start_job_command (child);
1251 /* No more commands. Make sure we're "running"; we might not be if
1252 (e.g.) all commands were skipped due to -n. */
1253 set_command_state (child->file, cs_running);
1254 child->file->update_status = us_success;
1255 notice_finished_file (child->file);
1262 /* Are we going to synchronize this command's output? Do so if either we're
1263 in SYNC_RECURSE mode or this command is not recursive. We'll also check
1264 output_sync separately below in case it changes due to error. */
1265 child->output.syncout = output_sync && (output_sync == OUTPUT_SYNC_RECURSE
1266 || !(flags & COMMANDS_RECURSE));
1268 OUTPUT_SET (&child->output);
1270 #ifndef NO_OUTPUT_SYNC
1271 if (! child->output.syncout)
1272 /* We don't want to sync this command: to avoid misordered
1273 output ensure any already-synced content is written. */
1274 output_dump (&child->output);
1277 /* Print the command if appropriate. */
1278 if (just_print_flag || trace_flag
1279 || (!(flags & COMMANDS_SILENT) && !silent_flag))
1280 message (0, "%s", p);
1282 /* Tell update_goal_chain that a command has been started on behalf of
1283 this target. It is important that this happens here and not in
1284 reap_children (where we used to do it), because reap_children might be
1285 reaping children from a different target. We want this increment to
1286 guaranteedly indicate that a command was started for the dependency
1287 chain (i.e., update_file recursion chain) we are processing. */
1291 /* Optimize an empty command. People use this for timestamp rules,
1292 so avoid forking a useless shell. Do this after we increment
1293 commands_started so make still treats this special case as if it
1294 performed some action (makes a difference as to what messages are
1297 #if !defined(VMS) && !defined(_AMIGA)
1299 #if defined __MSDOS__ || defined (__EMX__)
1300 unixy_shell /* the test is complicated and we already did it */
1302 (argv[0] && is_bourne_compatible_shell (argv[0]))
1304 && (argv[1] && argv[1][0] == '-'
1306 ((argv[1][1] == 'c' && argv[1][2] == '\0')
1308 (argv[1][1] == 'e' && argv[1][2] == 'c' && argv[1][3] == '\0')))
1309 && (argv[2] && argv[2][0] == ':' && argv[2][1] == '\0')
1316 #endif /* !VMS && !_AMIGA */
1318 /* If -n was given, recurse to get the next line in the sequence. */
1320 if (just_print_flag && !(flags & COMMANDS_RECURSE))
1329 /* We're sure we're going to invoke a command: set up the output. */
1332 /* Flush the output streams so they won't have things written twice. */
1338 #if !defined(WINDOWS32) && !defined(_AMIGA) && !defined(__MSDOS__)
1340 /* Set up a bad standard input that reads from a broken pipe. */
1342 if (bad_stdin == -1)
1344 /* Make a file descriptor that is the read end of a broken pipe.
1345 This will be used for some children's standard inputs. */
1349 /* Close the write side. */
1350 (void) close (pd[1]);
1351 /* Save the read side. */
1354 /* Set the descriptor to close on exec, so it does not litter any
1355 child's descriptor table. When it is dup2'd onto descriptor 0,
1356 that descriptor will not close on exec. */
1357 CLOSE_ON_EXEC (bad_stdin);
1361 #endif /* !WINDOWS32 && !_AMIGA && !__MSDOS__ */
1363 /* Decide whether to give this child the 'good' standard input
1364 (one that points to the terminal or whatever), or the 'bad' one
1365 that points to the read side of a broken pipe. */
1367 child->good_stdin = !good_stdin_used;
1368 if (child->good_stdin)
1369 good_stdin_used = 1;
1376 /* Set up the environment for the child. */
1377 if (child->environment == 0)
1378 child->environment = target_environment (child->file);
1381 #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
1384 /* start_waiting_job has set CHILD->remote if we can start a remote job. */
1387 int is_remote, id, used_stdin;
1388 if (start_remote_job (argv, child->environment,
1389 child->good_stdin ? 0 : bad_stdin,
1390 &is_remote, &id, &used_stdin))
1391 /* Don't give up; remote execution may fail for various reasons. If
1392 so, simply run the job locally. */
1396 if (child->good_stdin && !used_stdin)
1398 child->good_stdin = 0;
1399 good_stdin_used = 0;
1401 child->remote = is_remote;
1408 /* Fork the child process. */
1410 char **parent_environ;
1418 if (!child_execute_job (argv, child))
1421 perror_with_name ("fork", "");
1427 parent_environ = environ;
1430 /* If we aren't running a recursive command and we have a jobserver
1431 pipe, close it before exec'ing. */
1432 if (!(flags & COMMANDS_RECURSE) && job_fds[0] >= 0)
1434 CLOSE_ON_EXEC (job_fds[0]);
1435 CLOSE_ON_EXEC (job_fds[1]);
1438 CLOSE_ON_EXEC (job_rfd);
1440 /* Never use fork()/exec() here! Use spawn() instead in exec_command() */
1441 child->pid = child_execute_job (child->good_stdin ? FD_STDIN : bad_stdin,
1442 FD_STDOUT, FD_STDERR,
1443 argv, child->environment);
1448 perror_with_name ("spawn", "");
1452 /* undo CLOSE_ON_EXEC() after the child process has been started */
1453 if (!(flags & COMMANDS_RECURSE) && job_fds[0] >= 0)
1455 fcntl (job_fds[0], F_SETFD, 0);
1456 fcntl (job_fds[1], F_SETFD, 0);
1459 fcntl (job_rfd, F_SETFD, 0);
1461 #else /* !__EMX__ */
1463 child->pid = fork ();
1464 environ = parent_environ; /* Restore value child may have clobbered. */
1465 if (child->pid == 0)
1467 int outfd = FD_STDOUT;
1468 int errfd = FD_STDERR;
1470 /* We are the child side. */
1473 /* If we aren't running a recursive command and we have a jobserver
1474 pipe, close it before exec'ing. */
1475 if (!(flags & COMMANDS_RECURSE) && job_fds[0] >= 0)
1483 #ifdef SET_STACK_SIZE
1484 /* Reset limits, if necessary. */
1485 if (stack_limit.rlim_cur)
1486 setrlimit (RLIMIT_STACK, &stack_limit);
1489 /* Divert child output if output_sync in use. */
1490 if (child->output.syncout)
1492 if (child->output.out >= 0)
1493 outfd = child->output.out;
1494 if (child->output.err >= 0)
1495 errfd = child->output.err;
1498 child_execute_job (child->good_stdin ? FD_STDIN : bad_stdin,
1499 outfd, errfd, argv, child->environment);
1501 else if (child->pid < 0)
1505 perror_with_name ("fork", "");
1508 # endif /* !__EMX__ */
1512 #else /* __MSDOS__ or Amiga or WINDOWS32 */
1520 /* We call 'system' to do the job of the SHELL, since stock DOS
1521 shell is too dumb. Our 'system' knows how to handle long
1522 command lines even if pipes/redirection is needed; it will only
1523 call COMMAND.COM when its internal commands are used. */
1524 if (execute_by_shell)
1526 char *cmdline = argv[0];
1527 /* We don't have a way to pass environment to 'system',
1528 so we need to save and restore ours, sigh... */
1529 char **parent_environ = environ;
1531 environ = child->environment;
1533 /* If we have a *real* shell, tell 'system' to call
1534 it to do everything for us. */
1537 /* A *real* shell on MSDOS may not support long
1538 command lines the DJGPP way, so we must use 'system'. */
1539 cmdline = argv[2]; /* get past "shell -c" */
1542 dos_command_running = 1;
1543 proc_return = system (cmdline);
1544 environ = parent_environ;
1545 execute_by_shell = 0; /* for the next time */
1549 dos_command_running = 1;
1550 proc_return = spawnvpe (P_WAIT, argv[0], argv, child->environment);
1553 /* Need to unblock signals before turning off
1554 dos_command_running, so that child's signals
1555 will be treated as such (see fatal_error_signal). */
1557 dos_command_running = 0;
1559 /* If the child got a signal, dos_status has its
1560 high 8 bits set, so be careful not to alter them. */
1561 if (proc_return == -1)
1564 dos_status |= (proc_return & 0xff);
1566 child->pid = dos_pid++;
1568 #endif /* __MSDOS__ */
1570 amiga_status = MyExecute (argv);
1573 child->pid = amiga_pid++;
1574 if (amiga_batch_file)
1576 amiga_batch_file = 0;
1577 DeleteFile (amiga_bname); /* Ignore errors. */
1585 /* make UNC paths safe for CreateProcess -- backslash format */
1587 if (arg0 && arg0[0] == '/' && arg0[1] == '/')
1588 for ( ; arg0 && *arg0; arg0++)
1592 /* make sure CreateProcess() has Path it needs */
1593 sync_Path_environment ();
1595 #ifndef NO_OUTPUT_SYNC
1596 /* Divert child output if output_sync in use. Don't capture
1597 recursive make output unless we are synchronizing "make" mode. */
1598 if (child->output.syncout)
1599 hPID = process_easy (argv, child->environment,
1600 child->output.out, child->output.err);
1603 hPID = process_easy (argv, child->environment, -1, -1);
1605 if (hPID != INVALID_HANDLE_VALUE)
1606 child->pid = (pid_t) hPID;
1612 _("process_easy() failed to launch process (e=%ld)\n"),
1613 process_last_err (hPID));
1614 for (i = 0; argv[i]; i++)
1615 fprintf (stderr, "%s ", argv[i]);
1616 fprintf (stderr, _("\nCounted %d args in failed launch\n"), i);
1620 #endif /* WINDOWS32 */
1621 #endif /* __MSDOS__ or Amiga or WINDOWS32 */
1623 /* Bump the number of jobs started in this second. */
1626 /* We are the parent side. Set the state to
1627 say the commands are running and return. */
1629 set_command_state (child->file, cs_running);
1631 /* Free the storage used by the child's argument list. */
1641 child->file->update_status = us_failed;
1642 notice_finished_file (child->file);
1646 /* Try to start a child running.
1647 Returns nonzero if the child was started (and maybe finished), or zero if
1648 the load was too high and the child was put on the 'waiting_jobs' chain. */
1651 start_waiting_job (struct child *c)
1653 struct file *f = c->file;
1655 /* If we can start a job remotely, we always want to, and don't care about
1656 the local load average. We record that the job should be started
1657 remotely in C->remote for start_job_command to test. */
1659 c->remote = start_remote_job_p (1);
1661 /* If we are running at least one job already and the load average
1662 is too high, make this one wait. */
1664 && ((job_slots_used > 0 && load_too_high ())
1666 || (process_used_slots () >= MAXIMUM_WAIT_OBJECTS)
1670 /* Put this child on the chain of children waiting for the load average
1672 set_command_state (f, cs_running);
1673 c->next = waiting_jobs;
1678 /* Start the first command; reap_children will run later command lines. */
1679 start_job_command (c);
1681 switch (f->command_state)
1685 DB (DB_JOBS, (_("Putting child %p (%s) PID %s%s on the chain.\n"),
1686 c, c->file->name, pid2str (c->pid),
1687 c->remote ? _(" (remote)") : ""));
1689 /* One more job slot is in use. */
1694 case cs_not_started:
1695 /* All the command lines turned out to be empty. */
1696 f->update_status = us_success;
1700 notice_finished_file (f);
1705 assert (f->command_state == cs_finished);
1712 /* Create a 'struct child' for FILE and start its commands running. */
1715 new_job (struct file *file)
1717 struct commands *cmds = file->cmds;
1722 /* Let any previously decided-upon jobs that are waiting
1723 for the load to go down start before this new one. */
1724 start_waiting_jobs ();
1726 /* Reap any children that might have finished recently. */
1727 reap_children (0, 0);
1729 /* Chop the commands up into lines if they aren't already. */
1730 chop_commands (cmds);
1732 /* Start the command sequence, record it in a new
1733 'struct child', and add that to the chain. */
1735 c = xcalloc (sizeof (struct child));
1736 output_init (&c->output);
1739 c->sh_batch_file = NULL;
1741 /* Cache dontcare flag because file->dontcare can be changed once we
1742 return. Check dontcare inheritance mechanism for details. */
1743 c->dontcare = file->dontcare;
1745 /* Start saving output in case the expansion uses $(info ...) etc. */
1746 OUTPUT_SET (&c->output);
1748 /* Expand the command lines and store the results in LINES. */
1749 lines = xmalloc (cmds->ncommand_lines * sizeof (char *));
1750 for (i = 0; i < cmds->ncommand_lines; ++i)
1752 /* Collapse backslash-newline combinations that are inside variable
1753 or function references. These are left alone by the parser so
1754 that they will appear in the echoing of commands (where they look
1755 nice); and collapsed by construct_command_argv when it tokenizes.
1756 But letting them survive inside function invocations loses because
1757 we don't want the functions to see them as part of the text. */
1759 char *in, *out, *ref;
1761 /* IN points to where in the line we are scanning.
1762 OUT points to where in the line we are writing.
1763 When we collapse a backslash-newline combination,
1764 IN gets ahead of OUT. */
1766 in = out = cmds->command_lines[i];
1767 while ((ref = strchr (in, '$')) != 0)
1769 ++ref; /* Move past the $. */
1772 /* Copy the text between the end of the last chunk
1773 we processed (where IN points) and the new chunk
1774 we are about to process (where REF points). */
1775 memmove (out, in, ref - in);
1777 /* Move both pointers past the boring stuff. */
1781 if (*ref == '(' || *ref == '{')
1783 char openparen = *ref;
1784 char closeparen = openparen == '(' ? ')' : '}';
1789 *out++ = *in++; /* Copy OPENPAREN. */
1791 /* IN now points past the opening paren or brace.
1792 Count parens or braces until it is matched. */
1796 if (*in == closeparen && --count < 0)
1798 else if (*in == '\\' && in[1] == '\n')
1800 /* We have found a backslash-newline inside a
1801 variable or function reference. Eat it and
1802 any following whitespace. */
1805 for (p = in - 1; p > ref && *p == '\\'; --p)
1809 /* There were two or more backslashes, so this is
1810 not really a continuation line. We don't collapse
1811 the quoting backslashes here as is done in
1812 collapse_continuations, because the line will
1813 be collapsed again after expansion. */
1817 /* Skip the backslash, newline and
1818 any following whitespace. */
1819 in = next_token (in + 2);
1821 /* Discard any preceding whitespace that has
1822 already been written to the output. */
1824 && isblank ((unsigned char)out[-1]))
1827 /* Replace it all with a single space. */
1833 if (*in == openparen)
1842 /* There are no more references in this line to worry about.
1843 Copy the remaining uninteresting text to the output. */
1845 memmove (out, in, strlen (in) + 1);
1847 /* Finally, expand the line. */
1848 lines[i] = allocated_variable_expand_for_file (cmds->command_lines[i],
1852 c->command_lines = lines;
1854 /* Fetch the first command line to be run. */
1855 job_next_command (c);
1857 /* Wait for a job slot to be freed up. If we allow an infinite number
1858 don't bother; also job_slots will == 0 if we're using the jobserver. */
1861 while (job_slots_used == job_slots)
1862 reap_children (1, 0);
1864 #ifdef MAKE_JOBSERVER
1865 /* If we are controlling multiple jobs make sure we have a token before
1866 starting the child. */
1868 /* This can be inefficient. There's a decent chance that this job won't
1869 actually have to run any subprocesses: the command script may be empty
1870 or otherwise optimized away. It would be nice if we could defer
1871 obtaining a token until just before we need it, in start_job_command.
1872 To do that we'd need to keep track of whether we'd already obtained a
1873 token (since start_job_command is called for each line of the job, not
1874 just once). Also more thought needs to go into the entire algorithm;
1875 this is where the old parallel job code waits, so... */
1878 else if (has_jobserver_semaphore ())
1880 else if (job_fds[0] >= 0)
1890 DB (DB_JOBS, ("Need a job token; we %shave children\n",
1891 children ? "" : "don't "));
1893 /* If we don't already have a job started, use our "free" token. */
1894 if (!jobserver_tokens)
1898 /* Read a token. As long as there's no token available we'll block.
1899 We enable interruptible system calls before the read(2) so that if
1900 we get a SIGCHLD while we're waiting, we'll return with EINTR and
1901 we can process the death(s) and return tokens to the free pool.
1903 Once we return from the read, we immediately reinstate restartable
1904 system calls. This allows us to not worry about checking for
1905 EINTR on all the other system calls in the program.
1907 There is one other twist: there is a span between the time
1908 reap_children() does its last check for dead children and the time
1909 the read(2) call is entered, below, where if a child dies we won't
1910 notice. This is extremely serious as it could cause us to
1911 deadlock, given the right set of events.
1913 To avoid this, we do the following: before we reap_children(), we
1914 dup(2) the read FD on the jobserver pipe. The read(2) call below
1915 uses that new FD. In the signal handler, we close that FD. That
1916 way, if a child dies during the section mentioned above, the
1917 read(2) will be invoked with an invalid FD and will return
1918 immediately with EBADF. */
1920 /* Make sure we have a dup'd FD. */
1923 DB (DB_JOBS, ("Duplicate the job FD\n"));
1924 job_rfd = dup (job_fds[0]);
1928 /* Reap anything that's currently waiting. */
1929 reap_children (0, 0);
1931 /* Kick off any jobs we have waiting for an opportunity that
1932 can run now (i.e., waiting for load). */
1933 start_waiting_jobs ();
1935 /* If our "free" slot has become available, use it; we don't need an
1937 if (!jobserver_tokens)
1940 /* There must be at least one child already, or we have no business
1941 waiting for a token. */
1943 fatal (NILF, "INTERNAL: no children as we go to sleep on read\n");
1946 /* On Windows we simply wait for the jobserver semaphore to become
1947 * signalled or one of our child processes to terminate.
1949 got_token = wait_for_semaphore_or_child_process ();
1952 DWORD err = GetLastError ();
1953 fatal (NILF, _("semaphore or child process wait: (Error %ld: %s)"),
1954 err, map_windows32_error_to_string (err));
1957 /* Set interruptible system calls, and read() for a job token. */
1958 set_child_handler_action_flags (1, waiting_jobs != NULL);
1959 got_token = read (job_rfd, &token, 1);
1960 saved_errno = errno;
1961 set_child_handler_action_flags (0, waiting_jobs != NULL);
1964 /* If we got one, we're done here. */
1967 DB (DB_JOBS, (_("Obtained token for child %p (%s).\n"),
1973 /* If the error _wasn't_ expected (EINTR or EBADF), punt. Otherwise,
1974 go back and reap_children(), and try again. */
1975 errno = saved_errno;
1976 if (errno != EINTR && errno != EBADF)
1977 pfatal_with_name (_("read jobs pipe"));
1979 DB (DB_JOBS, ("Read returned EBADF.\n"));
1987 Use message here so that changes to working directories are logged. */
1990 char *newer = allocated_variable_expand_for_file ("$?", c->file);
1993 if (! cmds->fileinfo.filenm)
1994 nm = _("<builtin>");
1997 char *n = alloca (strlen (cmds->fileinfo.filenm) + 1 + 11 + 1);
1998 sprintf (n, "%s:%lu", cmds->fileinfo.filenm, cmds->fileinfo.lineno);
2002 if (newer[0] == '\0')
2003 message (0, _("%s: target '%s' does not exist"), nm, c->file->name);
2005 message (0, _("%s: update target '%s' due to: %s"), nm,
2006 c->file->name, newer);
2011 /* The job is now primed. Start it running.
2012 (This will notice if there is in fact no recipe.) */
2013 start_waiting_job (c);
2015 if (job_slots == 1 || not_parallel)
2016 /* Since there is only one job slot, make things run linearly.
2017 Wait for the child to die, setting the state to 'cs_finished'. */
2018 while (file->command_state == cs_running)
2019 reap_children (1, 0);
2025 /* Move CHILD's pointers to the next command for it to execute.
2026 Returns nonzero if there is another command. */
2029 job_next_command (struct child *child)
2031 while (child->command_ptr == 0 || *child->command_ptr == '\0')
2033 /* There are no more lines in the expansion of this line. */
2034 if (child->command_line == child->file->cmds->ncommand_lines)
2036 /* There are no more lines to be expanded. */
2037 child->command_ptr = 0;
2041 /* Get the next line to run. */
2042 child->command_ptr = child->command_lines[child->command_line++];
2047 /* Determine if the load average on the system is too high to start a new job.
2048 The real system load average is only recomputed once a second. However, a
2049 very parallel make can easily start tens or even hundreds of jobs in a
2050 second, which brings the system to its knees for a while until that first
2051 batch of jobs clears out.
2053 To avoid this we use a weighted algorithm to try to account for jobs which
2054 have been started since the last second, and guess what the load average
2055 would be now if it were computed.
2057 This algorithm was provided by Thomas Riedl <thomas.riedl@siemens.com>,
2060 ! calculate something load-oid and add to the observed sys.load,
2061 ! so that latter can catch up:
2062 ! - every job started increases jobctr;
2063 ! - every dying job decreases a positive jobctr;
2064 ! - the jobctr value gets zeroed every change of seconds,
2065 ! after its value*weight_b is stored into the 'backlog' value last_sec
2066 ! - weight_a times the sum of jobctr and last_sec gets
2067 ! added to the observed sys.load.
2069 ! The two weights have been tried out on 24 and 48 proc. Sun Solaris-9
2070 ! machines, using a several-thousand-jobs-mix of cpp, cc, cxx and smallish
2071 ! sub-shelled commands (rm, echo, sed...) for tests.
2072 ! lowering the 'direct influence' factor weight_a (e.g. to 0.1)
2073 ! resulted in significant excession of the load limit, raising it
2074 ! (e.g. to 0.5) took bad to small, fast-executing jobs and didn't
2075 ! reach the limit in most test cases.
2077 ! lowering the 'history influence' weight_b (e.g. to 0.1) resulted in
2078 ! exceeding the limit for longer-running stuff (compile jobs in
2079 ! the .5 to 1.5 sec. range),raising it (e.g. to 0.5) overrepresented
2080 ! small jobs' effects.
2084 #define LOAD_WEIGHT_A 0.25
2085 #define LOAD_WEIGHT_B 0.25
2088 load_too_high (void)
2090 #if defined(__MSDOS__) || defined(VMS) || defined(_AMIGA) || defined(__riscos__)
2093 static double last_sec;
2094 static time_t last_now;
2099 /* sub_proc.c cannot wait for more than MAXIMUM_WAIT_OBJECTS children */
2100 if (process_used_slots () >= MAXIMUM_WAIT_OBJECTS)
2104 if (max_load_average < 0)
2107 /* Find the real system load average. */
2109 if (getloadavg (&load, 1) != 1)
2111 static int lossage = -1;
2112 /* Complain only once for the same error. */
2113 if (lossage == -1 || errno != lossage)
2116 /* An errno value of zero means getloadavg is just unsupported. */
2118 _("cannot enforce load limits on this operating system"));
2120 perror_with_name (_("cannot enforce load limit: "), "getloadavg");
2127 /* If we're in a new second zero the counter and correct the backlog
2128 value. Only keep the backlog for one extra second; after that it's 0. */
2132 if (last_now == now - 1)
2133 last_sec = LOAD_WEIGHT_B * job_counter;
2141 /* Try to guess what the load would be right now. */
2142 guess = load + (LOAD_WEIGHT_A * (job_counter + last_sec));
2144 DB (DB_JOBS, ("Estimated system load = %f (actual = %f) (max requested = %f)\n",
2145 guess, load, max_load_average));
2147 return guess >= max_load_average;
2151 /* Start jobs that are waiting for the load to be lower. */
2154 start_waiting_jobs (void)
2158 if (waiting_jobs == 0)
2163 /* Check for recently deceased descendants. */
2164 reap_children (0, 0);
2166 /* Take a job off the waiting list. */
2168 waiting_jobs = job->next;
2170 /* Try to start that job. We break out of the loop as soon
2171 as start_waiting_job puts one back on the waiting list. */
2173 while (start_waiting_job (job) && waiting_jobs != 0);
2180 /* EMX: Start a child process. This function returns the new pid. */
2181 # if defined __EMX__
2183 child_execute_job (int stdin_fd, int stdout_fd, int stderr_fd,
2184 char **argv, char **envp)
2187 int save_stdin = -1;
2188 int save_stdout = -1;
2189 int save_stderr = -1;
2191 /* For each FD which needs to be redirected first make a dup of the standard
2192 FD to save and mark it close on exec so our child won't see it. Then
2193 dup2() the standard FD to the redirect FD, and also mark the redirect FD
2194 as close on exec. */
2195 if (stdin_fd != FD_STDIN)
2197 save_stdin = dup (FD_STDIN);
2199 fatal (NILF, _("no more file handles: could not duplicate stdin\n"));
2200 CLOSE_ON_EXEC (save_stdin);
2202 dup2 (stdin_fd, FD_STDIN);
2203 CLOSE_ON_EXEC (stdin_fd);
2206 if (stdout_fd != FD_STDOUT)
2208 save_stdout = dup (FD_STDOUT);
2209 if (save_stdout < 0)
2210 fatal (NILF, _("no more file handles: could not duplicate stdout\n"));
2211 CLOSE_ON_EXEC (save_stdout);
2213 dup2 (stdout_fd, FD_STDOUT);
2214 CLOSE_ON_EXEC (stdout_fd);
2217 if (stderr_fd != FD_STDERR)
2219 if (stderr_fd != stdout_fd)
2221 save_stderr = dup (FD_STDERR);
2222 if (save_stderr < 0)
2223 fatal (NILF, _("no more file handles: could not duplicate stderr\n"));
2224 CLOSE_ON_EXEC (save_stderr);
2227 dup2 (stderr_fd, FD_STDERR);
2228 CLOSE_ON_EXEC (stderr_fd);
2231 /* Run the command. */
2232 pid = exec_command (argv, envp);
2234 /* Restore stdout/stdin/stderr of the parent and close temporary FDs. */
2235 if (save_stdin >= 0)
2237 if (dup2 (save_stdin, FD_STDIN) != FD_STDIN)
2238 fatal (NILF, _("Could not restore stdin\n"));
2243 if (save_stdout >= 0)
2245 if (dup2 (save_stdout, FD_STDOUT) != FD_STDOUT)
2246 fatal (NILF, _("Could not restore stdout\n"));
2248 close (save_stdout);
2251 if (save_stderr >= 0)
2253 if (dup2 (save_stderr, FD_STDERR) != FD_STDERR)
2254 fatal (NILF, _("Could not restore stderr\n"));
2256 close (save_stderr);
2262 #elif !defined (_AMIGA) && !defined (__MSDOS__) && !defined (VMS)
2265 Replace the current process with one executing the command in ARGV.
2266 STDIN_FD/STDOUT_FD/STDERR_FD are used as the process's stdin/stdout/stderr;
2267 ENVP is the environment of the new program. This function does not return. */
2269 child_execute_job (int stdin_fd, int stdout_fd, int stderr_fd,
2270 char **argv, char **envp)
2272 /* For any redirected FD, dup2() it to the standard FD then close it. */
2273 if (stdin_fd != FD_STDIN)
2275 dup2 (stdin_fd, FD_STDIN);
2279 if (stdout_fd != FD_STDOUT)
2280 dup2 (stdout_fd, FD_STDOUT);
2281 if (stderr_fd != FD_STDERR)
2282 dup2 (stderr_fd, FD_STDERR);
2284 if (stdout_fd != FD_STDOUT)
2286 if (stderr_fd != FD_STDERR && stderr_fd != stdout_fd)
2289 /* Run the command. */
2290 exec_command (argv, envp);
2292 #endif /* !AMIGA && !__MSDOS__ && !VMS */
2293 #endif /* !WINDOWS32 */
2296 /* Replace the current process with one running the command in ARGV,
2297 with environment ENVP. This function does not return. */
2299 /* EMX: This function returns the pid of the child process. */
2305 exec_command (char **argv, char **envp)
2308 /* to work around a problem with signals and execve: ignore them */
2310 signal (SIGCHLD,SIG_IGN);
2312 /* Run the program. */
2313 execve (argv[0], argv, envp);
2314 perror_with_name ("execve: ", argv[0]);
2315 _exit (EXIT_FAILURE);
2320 int exit_code = EXIT_FAILURE;
2322 /* make sure CreateProcess() has Path it needs */
2323 sync_Path_environment ();
2325 /* launch command */
2326 hPID = process_easy (argv, envp, -1, -1);
2328 /* make sure launch ok */
2329 if (hPID == INVALID_HANDLE_VALUE)
2332 fprintf (stderr, _("process_easy() failed to launch process (e=%ld)\n"),
2333 process_last_err (hPID));
2334 for (i = 0; argv[i]; i++)
2335 fprintf (stderr, "%s ", argv[i]);
2336 fprintf (stderr, _("\nCounted %d args in failed launch\n"), i);
2337 exit (EXIT_FAILURE);
2340 /* wait and reap last child */
2341 hWaitPID = process_wait_for_any (1, 0);
2344 /* was an error found on this process? */
2345 int err = process_last_err (hWaitPID);
2348 exit_code = process_exit_code (hWaitPID);
2351 fprintf (stderr, "make (e=%d, rc=%d): %s",
2352 err, exit_code, map_windows32_error_to_string (err));
2354 /* cleanup process */
2355 process_cleanup (hWaitPID);
2357 /* expect to find only last pid, warn about other pids reaped */
2358 if (hWaitPID == hPID)
2362 char *pidstr = xstrdup (pid2str ((pid_t)hWaitPID));
2365 _("make reaped child pid %s, still waiting for pid %s\n"),
2366 pidstr, pid2str ((pid_t)hPID));
2371 /* return child's exit code as our exit code */
2374 #else /* !WINDOWS32 */
2380 /* Be the user, permanently. */
2384 /* Run the program. */
2385 pid = spawnvpe (P_NOWAIT, argv[0], argv, envp);
2389 /* the file might have a strange shell extension */
2390 if (errno == ENOENT)
2394 /* Run the program. */
2396 execvp (argv[0], argv);
2398 # endif /* !__EMX__ */
2403 error (NILF, _("%s: Command not found"), argv[0]);
2407 /* The file is not executable. Try it as a shell script. */
2408 extern char *getenv ();
2415 /* Do not use $SHELL from the environment */
2416 struct variable *p = lookup_variable ("SHELL", 5);
2422 shell = getenv ("SHELL");
2425 shell = default_shell;
2428 while (argv[argc] != 0)
2436 new_argv = alloca ((1 + argc + 1) * sizeof (char *));
2437 new_argv[0] = shell;
2448 new_argv[i] = argv[0];
2451 new_argv[i + argc] = argv[argc];
2456 pid = spawnvpe (P_NOWAIT, shell, new_argv, envp);
2460 execvp (shell, new_argv);
2462 if (errno == ENOENT)
2463 error (NILF, _("%s: Shell program not found"), shell);
2465 perror_with_name ("execvp: ", shell);
2471 /* this nasty error was driving me nuts :-( */
2472 error (NILF, _("spawnvpe: environment space might be exhausted"));
2477 perror_with_name ("execvp: ", argv[0]);
2486 #endif /* !WINDOWS32 */
2489 #else /* On Amiga */
2490 void exec_command (char **argv)
2495 void clean_tmp (void)
2497 DeleteFile (amiga_bname);
2500 #endif /* On Amiga */
2503 /* Figure out the argument list necessary to run LINE as a command. Try to
2504 avoid using a shell. This routine handles only ' quoting, and " quoting
2505 when no backslash, $ or ' characters are seen in the quotes. Starting
2506 quotes may be escaped with a backslash. If any of the characters in
2507 sh_chars[] is seen, or any of the builtin commands listed in sh_cmds[]
2508 is the first word of a line, the shell is used.
2510 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
2511 If *RESTP is NULL, newlines will be ignored.
2513 SHELL is the shell to use, or nil to use the default shell.
2514 IFS is the value of $IFS, or nil (meaning the default).
2516 FLAGS is the value of lines_flags for this command line. It is
2517 used in the WINDOWS32 port to check whether + or $(MAKE) were found
2518 in this command line, in which case the effect of just_print_flag
2522 construct_command_argv_internal (char *line, char **restp, char *shell,
2523 char *shellflags, char *ifs, int flags,
2524 char **batch_filename UNUSED)
2527 /* MSDOS supports both the stock DOS shell and ports of Unixy shells.
2528 We call 'system' for anything that requires ''slow'' processing,
2529 because DOS shells are too dumb. When $SHELL points to a real
2530 (unix-style) shell, 'system' just calls it to do everything. When
2531 $SHELL points to a DOS shell, 'system' does most of the work
2532 internally, calling the shell only for its internal commands.
2533 However, it looks on the $PATH first, so you can e.g. have an
2534 external command named 'mkdir'.
2536 Since we call 'system', certain characters and commands below are
2537 actually not specific to COMMAND.COM, but to the DJGPP implementation
2538 of 'system'. In particular:
2540 The shell wildcard characters are in DOS_CHARS because they will
2541 not be expanded if we call the child via 'spawnXX'.
2543 The ';' is in DOS_CHARS, because our 'system' knows how to run
2544 multiple commands on a single line.
2546 DOS_CHARS also include characters special to 4DOS/NDOS, so we
2547 won't have to tell one from another and have one more set of
2548 commands and special characters. */
2549 static char sh_chars_dos[] = "*?[];|<>%^&()";
2550 static char *sh_cmds_dos[] = { "break", "call", "cd", "chcp", "chdir", "cls",
2551 "copy", "ctty", "date", "del", "dir", "echo",
2552 "erase", "exit", "for", "goto", "if", "md",
2553 "mkdir", "path", "pause", "prompt", "rd",
2554 "rmdir", "rem", "ren", "rename", "set",
2555 "shift", "time", "type", "ver", "verify",
2558 static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^";
2559 static char *sh_cmds_sh[] = { "cd", "echo", "eval", "exec", "exit", "login",
2560 "logout", "set", "umask", "wait", "while",
2561 "for", "case", "if", ":", ".", "break",
2562 "continue", "export", "read", "readonly",
2563 "shift", "times", "trap", "switch", "unset",
2568 #elif defined (__EMX__)
2569 static char sh_chars_dos[] = "*?[];|<>%^&()";
2570 static char *sh_cmds_dos[] = { "break", "call", "cd", "chcp", "chdir", "cls",
2571 "copy", "ctty", "date", "del", "dir", "echo",
2572 "erase", "exit", "for", "goto", "if", "md",
2573 "mkdir", "path", "pause", "prompt", "rd",
2574 "rmdir", "rem", "ren", "rename", "set",
2575 "shift", "time", "type", "ver", "verify",
2578 static char sh_chars_os2[] = "*?[];|<>%^()\"'&";
2579 static char *sh_cmds_os2[] = { "call", "cd", "chcp", "chdir", "cls", "copy",
2580 "date", "del", "detach", "dir", "echo",
2581 "endlocal", "erase", "exit", "for", "goto", "if",
2582 "keys", "md", "mkdir", "move", "path", "pause",
2583 "prompt", "rd", "rem", "ren", "rename", "rmdir",
2584 "set", "setlocal", "shift", "start", "time",
2585 "type", "ver", "verify", "vol", ":", 0 };
2587 static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^~'";
2588 static char *sh_cmds_sh[] = { "echo", "cd", "eval", "exec", "exit", "login",
2589 "logout", "set", "umask", "wait", "while",
2590 "for", "case", "if", ":", ".", "break",
2591 "continue", "export", "read", "readonly",
2592 "shift", "times", "trap", "switch", "unset",
2597 #elif defined (_AMIGA)
2598 static char sh_chars[] = "#;\"|<>()?*$`";
2599 static char *sh_cmds[] = { "cd", "eval", "if", "delete", "echo", "copy",
2600 "rename", "set", "setenv", "date", "makedir",
2601 "skip", "else", "endif", "path", "prompt",
2602 "unset", "unsetenv", "version",
2604 #elif defined (WINDOWS32)
2605 /* We used to have a double quote (") in sh_chars_dos[] below, but
2606 that caused any command line with quoted file names be run
2607 through a temporary batch file, which introduces command-line
2608 limit of 4K charcaters imposed by cmd.exe. Since CreateProcess
2609 can handle quoted file names just fine, removing the quote lifts
2610 the limit from a very frequent use case, because using quoted
2611 file names is commonplace on MS-Windows. */
2612 static char sh_chars_dos[] = "|&<>";
2613 static char *sh_cmds_dos[] = { "assoc", "break", "call", "cd", "chcp",
2614 "chdir", "cls", "color", "copy", "ctty",
2615 "date", "del", "dir", "echo", "echo.",
2616 "endlocal", "erase", "exit", "for", "ftype",
2617 "goto", "if", "if", "md", "mkdir", "move",
2618 "path", "pause", "prompt", "rd", "rem", "ren",
2619 "rename", "rmdir", "set", "setlocal",
2620 "shift", "time", "title", "type", "ver",
2621 "verify", "vol", ":", 0 };
2622 static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^";
2623 static char *sh_cmds_sh[] = { "cd", "eval", "exec", "exit", "login",
2624 "logout", "set", "umask", "wait", "while", "for",
2625 "case", "if", ":", ".", "break", "continue",
2626 "export", "read", "readonly", "shift", "times",
2627 "trap", "switch", "test",
2628 #ifdef BATCH_MODE_ONLY_SHELL
2634 #elif defined(__riscos__)
2635 static char sh_chars[] = "";
2636 static char *sh_cmds[] = { 0 };
2637 #else /* must be UNIX-ish */
2638 static char sh_chars[] = "#;\"*?[]&|<>(){}$`^~!";
2639 static char *sh_cmds[] = { ".", ":", "break", "case", "cd", "continue",
2640 "eval", "exec", "exit", "export", "for", "if",
2641 "login", "logout", "read", "readonly", "set",
2642 "shift", "switch", "test", "times", "trap",
2643 "ulimit", "umask", "unset", "wait", "while", 0 };
2644 # ifdef HAVE_DOS_PATHS
2645 /* This is required if the MSYS/Cygwin ports (which do not define
2646 WINDOWS32) are compiled with HAVE_DOS_PATHS defined, which uses
2647 sh_chars_sh[] directly (see below). */
2648 static char *sh_chars_sh = sh_chars;
2649 # endif /* HAVE_DOS_PATHS */
2655 int instring, word_has_equals, seen_nonequals, last_argument_was_empty;
2656 char **new_argv = 0;
2663 sh_cmds = sh_cmds_dos;
2664 sh_chars = sh_chars_dos;
2668 sh_cmds = sh_cmds_sh;
2669 sh_chars = sh_chars_sh;
2671 #endif /* WINDOWS32 */
2676 /* Make sure not to bother processing an empty line. */
2677 while (isblank ((unsigned char)*line))
2682 if (shellflags == 0)
2683 shellflags = posix_pedantic ? "-ec" : "-c";
2685 /* See if it is safe to parse commands internally. */
2687 shell = default_shell;
2689 else if (strcmp (shell, default_shell))
2691 char *s1 = _fullpath (NULL, shell, 0);
2692 char *s2 = _fullpath (NULL, default_shell, 0);
2694 slow_flag = strcmp ((s1 ? s1 : ""), (s2 ? s2 : ""));
2703 #else /* not WINDOWS32 */
2704 #if defined (__MSDOS__) || defined (__EMX__)
2705 else if (strcasecmp (shell, default_shell))
2707 extern int _is_unixy_shell (const char *_path);
2709 DB (DB_BASIC, (_("$SHELL changed (was '%s', now '%s')\n"),
2710 default_shell, shell));
2711 unixy_shell = _is_unixy_shell (shell);
2712 /* we must allocate a copy of shell: construct_command_argv() will free
2713 * shell after this function returns. */
2714 default_shell = xstrdup (shell);
2718 sh_chars = sh_chars_sh;
2719 sh_cmds = sh_cmds_sh;
2723 sh_chars = sh_chars_dos;
2724 sh_cmds = sh_cmds_dos;
2726 if (_osmode == OS2_MODE)
2728 sh_chars = sh_chars_os2;
2729 sh_cmds = sh_cmds_os2;
2733 #else /* !__MSDOS__ */
2734 else if (strcmp (shell, default_shell))
2736 #endif /* !__MSDOS__ && !__EMX__ */
2737 #endif /* not WINDOWS32 */
2740 for (ap = ifs; *ap != '\0'; ++ap)
2741 if (*ap != ' ' && *ap != '\t' && *ap != '\n')
2744 if (shellflags != 0)
2745 if (shellflags[0] != '-'
2746 || ((shellflags[1] != 'c' || shellflags[2] != '\0')
2747 && (shellflags[1] != 'e' || shellflags[2] != 'c' || shellflags[3] != '\0')))
2750 i = strlen (line) + 1;
2752 /* More than 1 arg per character is impossible. */
2753 new_argv = xmalloc (i * sizeof (char *));
2755 /* All the args can fit in a buffer as big as LINE is. */
2756 ap = new_argv[0] = argstr = xmalloc (i);
2759 /* I is how many complete arguments have been found. */
2761 instring = word_has_equals = seen_nonequals = last_argument_was_empty = 0;
2762 for (p = line; *p != '\0'; ++p)
2768 /* Inside a string, just copy any char except a closing quote
2769 or a backslash-newline combination. */
2773 if (ap == new_argv[0] || *(ap-1) == '\0')
2774 last_argument_was_empty = 1;
2776 else if (*p == '\\' && p[1] == '\n')
2778 /* Backslash-newline is handled differently depending on what
2779 kind of string we're in: inside single-quoted strings you
2780 keep them; in double-quoted strings they disappear. For
2781 DOS/Windows/OS2, if we don't have a POSIX shell, we keep the
2782 pre-POSIX behavior of removing the backslash-newline. */
2784 #if defined (__MSDOS__) || defined (__EMX__) || defined (WINDOWS32)
2795 else if (*p == '\n' && restp != NULL)
2797 /* End of the command line. */
2801 /* Backslash, $, and ` are special inside double quotes.
2802 If we see any of those, punt.
2803 But on MSDOS, if we use COMMAND.COM, double and single
2804 quotes have the same effect. */
2805 else if (instring == '"' && strchr ("\\$`", *p) != 0 && unixy_shell)
2808 else if (instring == '"' && strncmp (p, "\\\"", 2) == 0)
2814 else if (strchr (sh_chars, *p) != 0)
2815 /* Not inside a string, but it's a special char. */
2817 else if (one_shell && *p == '\n')
2818 /* In .ONESHELL mode \n is a separator like ; or && */
2821 else if (*p == '.' && p[1] == '.' && p[2] == '.' && p[3] != '.')
2822 /* '...' is a wildcard in DJGPP. */
2826 /* Not a special char. */
2830 /* Equals is a special character in leading words before the
2831 first word with no equals sign in it. This is not the case
2832 with sh -k, but we never get here when using nonstandard
2834 if (! seen_nonequals && unixy_shell)
2836 word_has_equals = 1;
2841 /* Backslash-newline has special case handling, ref POSIX.
2842 We're in the fastpath, so emulate what the shell would do. */
2845 /* Throw out the backslash and newline. */
2848 /* If there's nothing in this argument yet, skip any
2849 whitespace before the start of the next word. */
2850 if (ap == new_argv[i])
2851 p = next_token (p + 1) - 1;
2854 /* Backslash before whitespace is not special if our shell
2856 else if (isspace (p[1]) && !unixy_shell)
2862 else if (p[1] != '\0')
2864 #ifdef HAVE_DOS_PATHS
2865 /* Only remove backslashes before characters special to Unixy
2866 shells. All other backslashes are copied verbatim, since
2867 they are probably DOS-style directory separators. This
2868 still leaves a small window for problems, but at least it
2869 should work for the vast majority of naive users. */
2872 /* A dot is only special as part of the "..."
2874 if (strneq (p + 1, ".\\.\\.", 5))
2882 if (p[1] != '\\' && p[1] != '\''
2883 && !isspace ((unsigned char)p[1])
2884 && strchr (sh_chars_sh, p[1]) == 0)
2885 /* back up one notch, to copy the backslash */
2887 #endif /* HAVE_DOS_PATHS */
2889 /* Copy and skip the following char. */
2902 /* End of the command line. */
2907 /* Newlines are not special. */
2913 /* We have the end of an argument.
2914 Terminate the text of the argument. */
2917 last_argument_was_empty = 0;
2919 /* Update SEEN_NONEQUALS, which tells us if every word
2920 heretofore has contained an '='. */
2921 seen_nonequals |= ! word_has_equals;
2922 if (word_has_equals && ! seen_nonequals)
2923 /* An '=' in a word before the first
2924 word without one is magical. */
2926 word_has_equals = 0; /* Prepare for the next word. */
2928 /* If this argument is the command name,
2929 see if it is a built-in shell command.
2930 If so, have the shell handle it. */
2934 for (j = 0; sh_cmds[j] != 0; ++j)
2936 if (streq (sh_cmds[j], new_argv[0]))
2938 #if defined(__EMX__) || defined(WINDOWS32)
2939 /* Non-Unix shells are case insensitive. */
2941 && strcasecmp (sh_cmds[j], new_argv[0]) == 0)
2947 /* Ignore multiple whitespace chars. */
2948 p = next_token (p) - 1;
2959 /* Let the shell deal with an unterminated quote. */
2962 /* Terminate the last argument and the argument list. */
2965 if (new_argv[i][0] != '\0' || last_argument_was_empty)
2972 for (j = 0; sh_cmds[j] != 0; ++j)
2973 if (streq (sh_cmds[j], new_argv[0]))
2977 if (new_argv[0] == 0)
2979 /* Line was empty. */
2988 /* We must use the shell. */
2992 /* Free the old argument list we were working on. */
2998 execute_by_shell = 1; /* actually, call 'system' if shell isn't unixy */
3007 buffer = xmalloc (strlen (line)+1);
3010 for (dptr=buffer; *ptr; )
3012 if (*ptr == '\\' && ptr[1] == '\n')
3014 else if (*ptr == '@') /* Kludge: multiline commands */
3024 new_argv = xmalloc (2 * sizeof (char *));
3025 new_argv[0] = buffer;
3028 #else /* Not Amiga */
3031 * Not eating this whitespace caused things like
3035 * which gave the shell fits. I think we have to eat
3036 * whitespace here, but this code should be considered
3037 * suspicious if things start failing....
3040 /* Make sure not to bother processing an empty line. */
3041 while (isspace ((unsigned char)*line))
3045 #endif /* WINDOWS32 */
3048 /* SHELL may be a multi-word command. Construct a command line
3049 "$(SHELL) $(.SHELLFLAGS) LINE", with all special chars in LINE escaped.
3050 Then recurse, expanding this command line to get the final
3054 unsigned int shell_len = strlen (shell);
3055 unsigned int line_len = strlen (line);
3056 unsigned int sflags_len = shellflags ? strlen (shellflags) : 0;
3058 char *command_ptr = NULL; /* used for batch_mode_shell mode */
3061 # ifdef __EMX__ /* is this necessary? */
3062 if (!unixy_shell && shellflags)
3063 shellflags[0] = '/'; /* "/c" */
3066 /* In .ONESHELL mode we are allowed to throw the entire current
3067 recipe string at a single shell and trust that the user
3068 has configured the shell and shell flags, and formatted
3069 the string, appropriately. */
3072 /* If the shell is Bourne compatible, we must remove and ignore
3073 interior special chars [@+-] because they're meaningless to
3074 the shell itself. If, however, we're in .ONESHELL mode and
3075 have changed SHELL to something non-standard, we should
3076 leave those alone because they could be part of the
3077 script. In this case we must also leave in place
3078 any leading [@+-] for the same reason. */
3080 /* Remove and ignore interior prefix chars [@+-] because they're
3081 meaningless given a single shell. */
3082 #if defined __MSDOS__ || defined (__EMX__)
3083 if (unixy_shell) /* the test is complicated and we already did it */
3085 if (is_bourne_compatible_shell (shell)
3087 /* If we didn't find any sh.exe, don't behave is if we did! */
3088 && !no_default_sh_exe
3093 const char *f = line;
3096 /* Copy the recipe, removing and ignoring interior prefix chars
3097 [@+-]: they're meaningless in .ONESHELL mode. */
3098 while (f[0] != '\0')
3102 /* This is the start of a new recipe line.
3103 Skip whitespace and prefix characters. */
3104 while (isblank (*f) || *f == '-' || *f == '@' || *f == '+')
3107 /* Copy until we get to the next logical recipe line. */
3115 /* On unescaped newline, we're done with this line. */
3116 if (f[-1] == '\n' && ! esc)
3119 /* Something else: reset the escape sequence. */
3127 else /* non-Posix shell (cmd.exe etc.) */
3129 const char *f = line;
3134 int id = GetCurrentProcessId ();
3137 /* Generate a file name for the temporary batch file. */
3138 sprintf (fbuf, "make%d", id);
3139 *batch_filename = create_batch_file (fbuf, 0, &temp_fd);
3140 DB (DB_JOBS, (_("Creating temporary batch file %s\n"),
3143 /* Create a FILE object for the batch file, and write to it the
3144 commands to be executed. Put the batch file in TEXT mode. */
3145 _setmode (temp_fd, _O_TEXT);
3146 batch = _fdopen (temp_fd, "wt");
3147 fputs ("@echo off\n", batch);
3148 DB (DB_JOBS, (_("Batch file contents:\n\t@echo off\n")));
3150 /* Copy the recipe, removing and ignoring interior prefix chars
3151 [@+-]: they're meaningless in .ONESHELL mode. */
3154 /* This is the start of a new recipe line.
3155 Skip whitespace and prefix characters. */
3156 while (isblank (*f) || *f == '-' || *f == '@' || *f == '+')
3159 /* Copy until we get to the next logical recipe line. */
3162 /* Remove the escaped newlines in the command, and
3163 the whitespace that follows them. Windows
3164 shells cannot handle escaped newlines. */
3165 if (*f == '\\' && f[1] == '\n')
3168 while (isblank (*f))
3172 /* On an unescaped newline, we're done with this
3177 /* Write another line into the batch file. */
3182 fputs (tstart, batch);
3183 DB (DB_JOBS, ("\t%s", tstart));
3188 DB (DB_JOBS, ("\n"));
3191 /* Create an argv list for the shell command line that
3192 will run the batch file. */
3193 new_argv = xmalloc (2 * sizeof (char *));
3194 new_argv[0] = xstrdup (*batch_filename);
3198 #endif /* WINDOWS32 */
3199 /* Create an argv list for the shell command line. */
3203 new_argv = xmalloc ((4 + sflags_len/2) * sizeof (char *));
3204 new_argv[n++] = xstrdup (shell);
3206 /* Chop up the shellflags (if any) and assign them. */
3208 new_argv[n++] = xstrdup ("");
3211 const char *s = shellflags;
3214 while ((t = find_next_token (&s, &len)) != 0)
3215 new_argv[n++] = xstrndup (t, len);
3218 /* Set the command to invoke. */
3219 new_argv[n++] = line;
3220 new_argv[n++] = NULL;
3225 new_line = xmalloc ((shell_len*2) + 1 + sflags_len + 1
3226 + (line_len*2) + 1);
3228 /* Copy SHELL, escaping any characters special to the shell. If
3229 we don't escape them, construct_command_argv_internal will
3230 recursively call itself ad nauseam, or until stack overflow,
3231 whichever happens first. */
3232 for (p = shell; *p != '\0'; ++p)
3234 if (strchr (sh_chars, *p) != 0)
3240 memcpy (ap, shellflags, sflags_len);
3246 for (p = line; *p != '\0'; ++p)
3248 if (restp != NULL && *p == '\n')
3253 else if (*p == '\\' && p[1] == '\n')
3255 /* POSIX says we keep the backslash-newline. If we don't have a
3256 POSIX shell on DOS/Windows/OS2, mimic the pre-POSIX behavior
3257 and remove the backslash/newline. */
3258 #if defined (__MSDOS__) || defined (__EMX__) || defined (WINDOWS32)
3259 # define PRESERVE_BSNL unixy_shell
3261 # define PRESERVE_BSNL 1
3266 /* Only non-batch execution needs another backslash,
3267 because it will be passed through a recursive
3268 invocation of this function. */
3269 if (!batch_mode_shell)
3277 /* DOS shells don't know about backslash-escaping. */
3278 if (unixy_shell && !batch_mode_shell &&
3279 (*p == '\\' || *p == '\'' || *p == '"'
3280 || isspace ((unsigned char)*p)
3281 || strchr (sh_chars, *p) != 0))
3284 else if (unixy_shell && strneq (p, "...", 3))
3286 /* The case of '...' wildcard again. */
3287 strcpy (ap, "\\.\\.\\");
3294 if (ap == new_line + shell_len + sflags_len + 2)
3296 /* Line was empty. */
3303 /* Some shells do not work well when invoked as 'sh -c xxx' to run a
3304 command line (e.g. Cygnus GNUWIN32 sh.exe on WIN32 systems). In these
3305 cases, run commands via a script file. */
3306 if (just_print_flag && !(flags & COMMANDS_RECURSE))
3308 /* Need to allocate new_argv, although it's unused, because
3309 start_job_command will want to free it and its 0'th element. */
3310 new_argv = xmalloc (2 * sizeof (char *));
3311 new_argv[0] = xstrdup ("");
3314 else if ((no_default_sh_exe || batch_mode_shell) && batch_filename)
3318 int id = GetCurrentProcessId ();
3321 /* create a file name */
3322 sprintf (fbuf, "make%d", id);
3323 *batch_filename = create_batch_file (fbuf, unixy_shell, &temp_fd);
3325 DB (DB_JOBS, (_("Creating temporary batch file %s\n"),
3328 /* Create a FILE object for the batch file, and write to it the
3329 commands to be executed. Put the batch file in TEXT mode. */
3330 _setmode (temp_fd, _O_TEXT);
3331 batch = _fdopen (temp_fd, "wt");
3333 fputs ("@echo off\n", batch);
3334 fputs (command_ptr, batch);
3335 fputc ('\n', batch);
3337 DB (DB_JOBS, (_("Batch file contents:%s\n\t%s\n"),
3338 !unixy_shell ? "\n\t@echo off" : "", command_ptr));
3341 new_argv = xmalloc (3 * sizeof (char *));
3344 new_argv[0] = xstrdup (shell);
3345 new_argv[1] = *batch_filename; /* only argv[0] gets freed later */
3349 new_argv[0] = xstrdup (*batch_filename);
3355 #endif /* WINDOWS32 */
3358 new_argv = construct_command_argv_internal (new_line, 0, 0, 0, 0,
3362 else if (!unixy_shell)
3364 /* new_line is local, must not be freed therefore
3365 We use line here instead of new_line because we run the shell
3367 size_t line_len = strlen (line);
3370 memcpy (new_line, line, line_len + 1);
3371 /* Replace all backslash-newline combination and also following tabs.
3372 Important: stop at the first '\n' because that's what the loop above
3373 did. The next line starting at restp[0] will be executed during the
3374 next call of this function. */
3375 while (*q != '\0' && *q != '\n')
3377 if (q[0] == '\\' && q[1] == '\n')
3378 q += 2; /* remove '\\' and '\n' */
3384 # ifndef NO_CMD_DEFAULT
3385 if (strnicmp (new_line, "echo", 4) == 0
3386 && (new_line[4] == ' ' || new_line[4] == '\t'))
3388 /* the builtin echo command: handle it separately */
3389 size_t echo_len = line_len - 5;
3390 char *echo_line = new_line + 5;
3392 /* special case: echo 'x="y"'
3393 cmd works this way: a string is printed as is, i.e., no quotes
3394 are removed. But autoconf uses a command like echo 'x="y"' to
3395 determine whether make works. autoconf expects the output x="y"
3396 so we will do exactly that.
3397 Note: if we do not allow cmd to be the default shell
3398 we do not need this kind of voodoo */
3399 if (echo_line[0] == '\''
3400 && echo_line[echo_len - 1] == '\''
3401 && strncmp (echo_line + 1, "ac_maketemp=",
3402 strlen ("ac_maketemp=")) == 0)
3404 /* remove the enclosing quotes */
3405 memmove (echo_line, echo_line + 1, echo_len - 2);
3406 echo_line[echo_len - 2] = '\0';
3412 /* Let the shell decide what to do. Put the command line into the
3413 2nd command line argument and hope for the best ;-) */
3414 size_t sh_len = strlen (shell);
3416 /* exactly 3 arguments + NULL */
3417 new_argv = xmalloc (4 * sizeof (char *));
3418 /* Exactly strlen(shell) + strlen("/c") + strlen(line) + 3 times
3419 the trailing '\0' */
3420 new_argv[0] = xmalloc (sh_len + line_len + 5);
3421 memcpy (new_argv[0], shell, sh_len + 1);
3422 new_argv[1] = new_argv[0] + sh_len + 1;
3423 memcpy (new_argv[1], "/c", 3);
3424 new_argv[2] = new_argv[1] + 3;
3425 memcpy (new_argv[2], new_line, line_len + 1);
3429 #elif defined(__MSDOS__)
3432 /* With MSDOS shells, we must construct the command line here
3433 instead of recursively calling ourselves, because we
3434 cannot backslash-escape the special characters (see above). */
3435 new_argv = xmalloc (sizeof (char *));
3436 line_len = strlen (new_line) - shell_len - sflags_len - 2;
3437 new_argv[0] = xmalloc (line_len + 1);
3438 strncpy (new_argv[0],
3439 new_line + shell_len + sflags_len + 2, line_len);
3440 new_argv[0][line_len] = '\0';
3444 fatal (NILF, _("%s (line %d) Bad shell context (!unixy && !batch_mode_shell)\n"),
3445 __FILE__, __LINE__);
3450 #endif /* ! AMIGA */
3456 /* Figure out the argument list necessary to run LINE as a command. Try to
3457 avoid using a shell. This routine handles only ' quoting, and " quoting
3458 when no backslash, $ or ' characters are seen in the quotes. Starting
3459 quotes may be escaped with a backslash. If any of the characters in
3460 sh_chars[] is seen, or any of the builtin commands listed in sh_cmds[]
3461 is the first word of a line, the shell is used.
3463 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
3464 If *RESTP is NULL, newlines will be ignored.
3466 FILE is the target whose commands these are. It is used for
3467 variable expansion for $(SHELL) and $(IFS). */
3470 construct_command_argv (char *line, char **restp, struct file *file,
3471 int cmd_flags, char **batch_filename)
3473 char *shell, *ifs, *shellflags;
3485 && (isspace ((unsigned char)*cptr)))
3490 && (!isspace ((unsigned char)*cptr)))
3495 argv = xmalloc (argc * sizeof (char *));
3504 && (isspace ((unsigned char)*cptr)))
3508 DB (DB_JOBS, ("argv[%d] = [%s]\n", argc, cptr));
3509 argv[argc++] = cptr;
3511 && (!isspace ((unsigned char)*cptr)))
3518 /* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
3519 int save = warn_undefined_variables_flag;
3520 warn_undefined_variables_flag = 0;
3522 shell = allocated_variable_expand_for_file ("$(SHELL)", file);
3525 * Convert to forward slashes so that construct_command_argv_internal()
3530 char *p = w32ify (shell, 0);
3536 static const char *unixroot = NULL;
3537 static const char *last_shell = "";
3538 static int init = 0;
3541 unixroot = getenv ("UNIXROOT");
3542 /* unixroot must be NULL or not empty */
3543 if (unixroot && unixroot[0] == '\0') unixroot = NULL;
3547 /* if we have an unixroot drive and if shell is not default_shell
3548 (which means it's either cmd.exe or the test has already been
3549 performed) and if shell is an absolute path without drive letter,
3550 try whether it exists e.g.: if "/bin/sh" does not exist use
3551 "$UNIXROOT/bin/sh" instead. */
3552 if (unixroot && shell && strcmp (shell, last_shell) != 0
3553 && (shell[0] == '/' || shell[0] == '\\'))
3555 /* trying a new shell, check whether it exists */
3556 size_t size = strlen (shell);
3557 char *buf = xmalloc (size + 7);
3558 memcpy (buf, shell, size);
3559 memcpy (buf + size, ".exe", 5); /* including the trailing '\0' */
3560 if (access (shell, F_OK) != 0 && access (buf, F_OK) != 0)
3562 /* try the same for the unixroot drive */
3563 memmove (buf + 2, buf, size + 5);
3564 buf[0] = unixroot[0];
3565 buf[1] = unixroot[1];
3566 if (access (buf, F_OK) == 0)
3567 /* we have found a shell! */
3577 #endif /* __EMX__ */
3579 shellflags = allocated_variable_expand_for_file ("$(.SHELLFLAGS)", file);
3580 ifs = allocated_variable_expand_for_file ("$(IFS)", file);
3582 warn_undefined_variables_flag = save;
3585 argv = construct_command_argv_internal (line, restp, shell, shellflags, ifs,
3586 cmd_flags, batch_filename);
3595 #if !defined(HAVE_DUP2) && !defined(_AMIGA)
3597 dup2 (int old, int new)
3612 #endif /* !HAVE_DUP2 && !_AMIGA */
3614 /* On VMS systems, include special VMS functions. */
3617 #include "vmsjobs.c"