Fixups for warnings on Windows (esp 64bit).
[make.git] / job.c
blob03d8a83abce7c5eb5d7d4e31453e2982cdc17819
1 /* Job execution and handling for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free
4 Software Foundation, Inc.
5 This file is part of GNU Make.
7 GNU Make is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 3 of the License, or (at your option) any later
10 version.
12 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along with
17 this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "make.h"
21 #include <assert.h>
23 #include "job.h"
24 #include "debug.h"
25 #include "filedef.h"
26 #include "commands.h"
27 #include "variable.h"
28 #include "debug.h"
30 #include <string.h>
32 /* Default shell to use. */
33 #ifdef WINDOWS32
34 #include <windows.h>
36 char *default_shell = "sh.exe";
37 int no_default_sh_exe = 1;
38 int batch_mode_shell = 1;
39 HANDLE main_thread;
41 #elif defined (_AMIGA)
43 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 char *default_shell = "command.com";
54 int batch_mode_shell = 0;
56 #elif defined (__EMX__)
58 char *default_shell = "/bin/sh";
59 int batch_mode_shell = 0;
61 #elif defined (VMS)
63 # include <descrip.h>
64 char default_shell[] = "";
65 int batch_mode_shell = 0;
67 #elif defined (__riscos__)
69 char default_shell[] = "";
70 int batch_mode_shell = 0;
72 #else
74 char default_shell[] = "/bin/sh";
75 int batch_mode_shell = 0;
77 #endif
79 #ifdef __MSDOS__
80 # include <process.h>
81 static int execute_by_shell;
82 static int dos_pid = 123;
83 int dos_status;
84 int dos_command_running;
85 #endif /* __MSDOS__ */
87 #ifdef _AMIGA
88 # include <proto/dos.h>
89 static int amiga_pid = 123;
90 static int amiga_status;
91 static char amiga_bname[32];
92 static int amiga_batch_file;
93 #endif /* Amiga. */
95 #ifdef VMS
96 # ifndef __GNUC__
97 # include <processes.h>
98 # endif
99 # include <starlet.h>
100 # include <lib$routines.h>
101 static void vmsWaitForChildren (int *);
102 #endif
104 #ifdef WINDOWS32
105 # include <windows.h>
106 # include <io.h>
107 # include <process.h>
108 # include "sub_proc.h"
109 # include "w32err.h"
110 # include "pathstuff.h"
111 #endif /* WINDOWS32 */
113 #ifdef __EMX__
114 # include <process.h>
115 #endif
117 #if defined (HAVE_SYS_WAIT_H) || defined (HAVE_UNION_WAIT)
118 # include <sys/wait.h>
119 #endif
121 #ifdef HAVE_WAITPID
122 # define WAIT_NOHANG(status) waitpid (-1, (status), WNOHANG)
123 #else /* Don't have waitpid. */
124 # ifdef HAVE_WAIT3
125 # ifndef wait3
126 extern int wait3 ();
127 # endif
128 # define WAIT_NOHANG(status) wait3 ((status), WNOHANG, (struct rusage *) 0)
129 # endif /* Have wait3. */
130 #endif /* Have waitpid. */
132 #if !defined (wait) && !defined (POSIX)
133 int wait ();
134 #endif
136 #ifndef HAVE_UNION_WAIT
138 # define WAIT_T int
140 # ifndef WTERMSIG
141 # define WTERMSIG(x) ((x) & 0x7f)
142 # endif
143 # ifndef WCOREDUMP
144 # define WCOREDUMP(x) ((x) & 0x80)
145 # endif
146 # ifndef WEXITSTATUS
147 # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
148 # endif
149 # ifndef WIFSIGNALED
150 # define WIFSIGNALED(x) (WTERMSIG (x) != 0)
151 # endif
152 # ifndef WIFEXITED
153 # define WIFEXITED(x) (WTERMSIG (x) == 0)
154 # endif
156 #else /* Have `union wait'. */
158 # define WAIT_T union wait
159 # ifndef WTERMSIG
160 # define WTERMSIG(x) ((x).w_termsig)
161 # endif
162 # ifndef WCOREDUMP
163 # define WCOREDUMP(x) ((x).w_coredump)
164 # endif
165 # ifndef WEXITSTATUS
166 # define WEXITSTATUS(x) ((x).w_retcode)
167 # endif
168 # ifndef WIFSIGNALED
169 # define WIFSIGNALED(x) (WTERMSIG(x) != 0)
170 # endif
171 # ifndef WIFEXITED
172 # define WIFEXITED(x) (WTERMSIG(x) == 0)
173 # endif
175 #endif /* Don't have `union wait'. */
177 #ifndef HAVE_UNISTD_H
178 int dup2 ();
179 int execve ();
180 void _exit ();
181 # ifndef VMS
182 int geteuid ();
183 int getegid ();
184 int setgid ();
185 int getgid ();
186 # endif
187 #endif
189 /* Different systems have different requirements for pid_t.
190 Plus we have to support gettext string translation... Argh. */
191 static const char *
192 pid2str (pid_t pid)
194 static char pidstring[100];
195 #ifdef WINDOWS32
196 sprintf (pidstring, "%Id", pid);
197 #else
198 sprintf (pidstring, "%lu", (unsigned long) pid);
199 #endif
200 return pidstring;
203 int getloadavg (double loadavg[], int nelem);
204 int start_remote_job (char **argv, char **envp, int stdin_fd, int *is_remote,
205 int *id_ptr, int *used_stdin);
206 int start_remote_job_p (int);
207 int remote_status (int *exit_code_ptr, int *signal_ptr, int *coredump_ptr,
208 int block);
210 RETSIGTYPE child_handler (int);
211 static void free_child (struct child *);
212 static void start_job_command (struct child *child);
213 static int load_too_high (void);
214 static int job_next_command (struct child *);
215 static int start_waiting_job (struct child *);
217 /* Chain of all live (or recently deceased) children. */
219 struct child *children = 0;
221 /* Number of children currently running. */
223 unsigned int job_slots_used = 0;
225 /* Nonzero if the `good' standard input is in use. */
227 static int good_stdin_used = 0;
229 /* Chain of children waiting to run until the load average goes down. */
231 static struct child *waiting_jobs = 0;
233 /* Non-zero if we use a *real* shell (always so on Unix). */
235 int unixy_shell = 1;
237 /* Number of jobs started in the current second. */
239 unsigned long job_counter = 0;
241 /* Number of jobserver tokens this instance is currently using. */
243 unsigned int jobserver_tokens = 0;
245 #ifdef WINDOWS32
247 * The macro which references this function is defined in make.h.
250 w32_kill(int pid, int sig)
252 return ((process_kill((HANDLE)pid, sig) == TRUE) ? 0 : -1);
255 /* This function creates a temporary file name with an extension specified
256 * by the unixy arg.
257 * Return an xmalloc'ed string of a newly created temp file and its
258 * file descriptor, or die. */
259 static char *
260 create_batch_file (char const *base, int unixy, int *fd)
262 const char *const ext = unixy ? "sh" : "bat";
263 const char *error_string = NULL;
264 char temp_path[MAXPATHLEN]; /* need to know its length */
265 unsigned path_size = GetTempPath(sizeof temp_path, temp_path);
266 int path_is_dot = 0;
267 unsigned uniq = 1;
268 const unsigned sizemax = strlen (base) + strlen (ext) + 10;
270 if (path_size == 0)
272 path_size = GetCurrentDirectory (sizeof temp_path, temp_path);
273 path_is_dot = 1;
276 while (path_size > 0 &&
277 path_size + sizemax < sizeof temp_path &&
278 uniq < 0x10000)
280 unsigned size = sprintf (temp_path + path_size,
281 "%s%s-%x.%s",
282 temp_path[path_size - 1] == '\\' ? "" : "\\",
283 base, uniq, ext);
284 HANDLE h = CreateFile (temp_path, /* file name */
285 GENERIC_READ | GENERIC_WRITE, /* desired access */
286 0, /* no share mode */
287 NULL, /* default security attributes */
288 CREATE_NEW, /* creation disposition */
289 FILE_ATTRIBUTE_NORMAL | /* flags and attributes */
290 FILE_ATTRIBUTE_TEMPORARY, /* we'll delete it */
291 NULL); /* no template file */
293 if (h == INVALID_HANDLE_VALUE)
295 const DWORD er = GetLastError();
297 if (er == ERROR_FILE_EXISTS || er == ERROR_ALREADY_EXISTS)
298 ++uniq;
300 /* the temporary path is not guaranteed to exist */
301 else if (path_is_dot == 0)
303 path_size = GetCurrentDirectory (sizeof temp_path, temp_path);
304 path_is_dot = 1;
307 else
309 error_string = map_windows32_error_to_string (er);
310 break;
313 else
315 const unsigned final_size = path_size + size + 1;
316 char *const path = xmalloc (final_size);
317 memcpy (path, temp_path, final_size);
318 *fd = _open_osfhandle ((long)h, 0);
319 if (unixy)
321 char *p;
322 int ch;
323 for (p = path; (ch = *p) != 0; ++p)
324 if (ch == '\\')
325 *p = '/';
327 return path; /* good return */
331 *fd = -1;
332 if (error_string == NULL)
333 error_string = _("Cannot create a temporary file\n");
334 fatal (NILF, error_string);
336 /* not reached */
337 return NULL;
339 #endif /* WINDOWS32 */
341 #ifdef __EMX__
342 /* returns whether path is assumed to be a unix like shell. */
344 _is_unixy_shell (const char *path)
346 /* list of non unix shells */
347 const char *known_os2shells[] = {
348 "cmd.exe",
349 "cmd",
350 "4os2.exe",
351 "4os2",
352 "4dos.exe",
353 "4dos",
354 "command.com",
355 "command",
356 NULL
359 /* find the rightmost '/' or '\\' */
360 const char *name = strrchr (path, '/');
361 const char *p = strrchr (path, '\\');
362 unsigned i;
364 if (name && p) /* take the max */
365 name = (name > p) ? name : p;
366 else if (p) /* name must be 0 */
367 name = p;
368 else if (!name) /* name and p must be 0 */
369 name = path;
371 if (*name == '/' || *name == '\\') name++;
373 i = 0;
374 while (known_os2shells[i] != NULL) {
375 if (strcasecmp (name, known_os2shells[i]) == 0)
376 return 0; /* not a unix shell */
377 i++;
380 /* in doubt assume a unix like shell */
381 return 1;
383 #endif /* __EMX__ */
386 /* Write an error message describing the exit status given in
387 EXIT_CODE, EXIT_SIG, and COREDUMP, for the target TARGET_NAME.
388 Append "(ignored)" if IGNORED is nonzero. */
390 static void
391 child_error (const char *target_name,
392 int exit_code, int exit_sig, int coredump, int ignored)
394 if (ignored && silent_flag)
395 return;
397 #ifdef VMS
398 if (!(exit_code & 1))
399 error (NILF,
400 (ignored ? _("*** [%s] Error 0x%x (ignored)")
401 : _("*** [%s] Error 0x%x")),
402 target_name, exit_code);
403 #else
404 if (exit_sig == 0)
405 error (NILF, ignored ? _("[%s] Error %d (ignored)") :
406 _("*** [%s] Error %d"),
407 target_name, exit_code);
408 else
409 error (NILF, "*** [%s] %s%s",
410 target_name, strsignal (exit_sig),
411 coredump ? _(" (core dumped)") : "");
412 #endif /* VMS */
416 /* Handle a dead child. This handler may or may not ever be installed.
418 If we're using the jobserver feature, we need it. First, installing it
419 ensures the read will interrupt on SIGCHLD. Second, we close the dup'd
420 read FD to ensure we don't enter another blocking read without reaping all
421 the dead children. In this case we don't need the dead_children count.
423 If we don't have either waitpid or wait3, then make is unreliable, but we
424 use the dead_children count to reap children as best we can. */
426 static unsigned int dead_children = 0;
428 RETSIGTYPE
429 child_handler (int sig UNUSED)
431 ++dead_children;
433 if (job_rfd >= 0)
435 close (job_rfd);
436 job_rfd = -1;
439 #ifdef __EMX__
440 /* The signal handler must called only once! */
441 signal (SIGCHLD, SIG_DFL);
442 #endif
444 /* This causes problems if the SIGCHLD interrupts a printf().
445 DB (DB_JOBS, (_("Got a SIGCHLD; %u unreaped children.\n"), dead_children));
449 extern int shell_function_pid, shell_function_completed;
451 /* Reap all dead children, storing the returned status and the new command
452 state (`cs_finished') in the `file' member of the `struct child' for the
453 dead child, and removing the child from the chain. In addition, if BLOCK
454 nonzero, we block in this function until we've reaped at least one
455 complete child, waiting for it to die if necessary. If ERR is nonzero,
456 print an error message first. */
458 void
459 reap_children (int block, int err)
461 #ifndef WINDOWS32
462 WAIT_T status;
463 /* Initially, assume we have some. */
464 int reap_more = 1;
465 #endif
467 #ifdef WAIT_NOHANG
468 # define REAP_MORE reap_more
469 #else
470 # define REAP_MORE dead_children
471 #endif
473 /* As long as:
475 We have at least one child outstanding OR a shell function in progress,
477 We're blocking for a complete child OR there are more children to reap
479 we'll keep reaping children. */
481 while ((children != 0 || shell_function_pid != 0)
482 && (block || REAP_MORE))
484 int remote = 0;
485 pid_t pid;
486 int exit_code, exit_sig, coredump;
487 register struct child *lastc, *c;
488 int child_failed;
489 int any_remote, any_local;
490 int dontcare;
492 if (err && block)
494 static int printed = 0;
496 /* We might block for a while, so let the user know why.
497 Only print this message once no matter how many jobs are left. */
498 fflush (stdout);
499 if (!printed)
500 error (NILF, _("*** Waiting for unfinished jobs...."));
501 printed = 1;
504 /* We have one less dead child to reap. As noted in
505 child_handler() above, this count is completely unimportant for
506 all modern, POSIX-y systems that support wait3() or waitpid().
507 The rest of this comment below applies only to early, broken
508 pre-POSIX systems. We keep the count only because... it's there...
510 The test and decrement are not atomic; if it is compiled into:
511 register = dead_children - 1;
512 dead_children = register;
513 a SIGCHLD could come between the two instructions.
514 child_handler increments dead_children.
515 The second instruction here would lose that increment. But the
516 only effect of dead_children being wrong is that we might wait
517 longer than necessary to reap a child, and lose some parallelism;
518 and we might print the "Waiting for unfinished jobs" message above
519 when not necessary. */
521 if (dead_children > 0)
522 --dead_children;
524 any_remote = 0;
525 any_local = shell_function_pid != 0;
526 for (c = children; c != 0; c = c->next)
528 any_remote |= c->remote;
529 any_local |= ! c->remote;
530 DB (DB_JOBS, (_("Live child %p (%s) PID %s %s\n"),
531 c, c->file->name, pid2str (c->pid),
532 c->remote ? _(" (remote)") : ""));
533 #ifdef VMS
534 break;
535 #endif
538 /* First, check for remote children. */
539 if (any_remote)
540 pid = remote_status (&exit_code, &exit_sig, &coredump, 0);
541 else
542 pid = 0;
544 if (pid > 0)
545 /* We got a remote child. */
546 remote = 1;
547 else if (pid < 0)
549 /* A remote status command failed miserably. Punt. */
550 remote_status_lose:
551 pfatal_with_name ("remote_status");
553 else
555 /* No remote children. Check for local children. */
556 #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
557 if (any_local)
559 #ifdef VMS
560 vmsWaitForChildren (&status);
561 pid = c->pid;
562 #else
563 #ifdef WAIT_NOHANG
564 if (!block)
565 pid = WAIT_NOHANG (&status);
566 else
567 #endif
568 EINTRLOOP(pid, wait (&status));
569 #endif /* !VMS */
571 else
572 pid = 0;
574 if (pid < 0)
576 /* The wait*() failed miserably. Punt. */
577 pfatal_with_name ("wait");
579 else if (pid > 0)
581 /* We got a child exit; chop the status word up. */
582 exit_code = WEXITSTATUS (status);
583 exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0;
584 coredump = WCOREDUMP (status);
586 /* If we have started jobs in this second, remove one. */
587 if (job_counter)
588 --job_counter;
590 else
592 /* No local children are dead. */
593 reap_more = 0;
595 if (!block || !any_remote)
596 break;
598 /* Now try a blocking wait for a remote child. */
599 pid = remote_status (&exit_code, &exit_sig, &coredump, 1);
600 if (pid < 0)
601 goto remote_status_lose;
602 else if (pid == 0)
603 /* No remote children either. Finally give up. */
604 break;
606 /* We got a remote child. */
607 remote = 1;
609 #endif /* !__MSDOS__, !Amiga, !WINDOWS32. */
611 #ifdef __MSDOS__
612 /* Life is very different on MSDOS. */
613 pid = dos_pid - 1;
614 status = dos_status;
615 exit_code = WEXITSTATUS (status);
616 if (exit_code == 0xff)
617 exit_code = -1;
618 exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0;
619 coredump = 0;
620 #endif /* __MSDOS__ */
621 #ifdef _AMIGA
622 /* Same on Amiga */
623 pid = amiga_pid - 1;
624 status = amiga_status;
625 exit_code = amiga_status;
626 exit_sig = 0;
627 coredump = 0;
628 #endif /* _AMIGA */
629 #ifdef WINDOWS32
631 HANDLE hPID;
632 int werr;
633 HANDLE hcTID, hcPID;
634 exit_code = 0;
635 exit_sig = 0;
636 coredump = 0;
638 /* Record the thread ID of the main process, so that we
639 could suspend it in the signal handler. */
640 if (!main_thread)
642 hcTID = GetCurrentThread ();
643 hcPID = GetCurrentProcess ();
644 if (!DuplicateHandle (hcPID, hcTID, hcPID, &main_thread, 0,
645 FALSE, DUPLICATE_SAME_ACCESS))
647 DWORD e = GetLastError ();
648 fprintf (stderr,
649 "Determine main thread ID (Error %ld: %s)\n",
650 e, map_windows32_error_to_string(e));
652 else
653 DB (DB_VERBOSE, ("Main thread handle = %p\n", main_thread));
656 /* wait for anything to finish */
657 hPID = process_wait_for_any();
658 if (hPID)
661 /* was an error found on this process? */
662 werr = process_last_err(hPID);
664 /* get exit data */
665 exit_code = process_exit_code(hPID);
667 if (werr)
668 fprintf(stderr, "make (e=%d): %s",
669 exit_code, map_windows32_error_to_string(exit_code));
671 /* signal */
672 exit_sig = process_signal(hPID);
674 /* cleanup process */
675 process_cleanup(hPID);
677 coredump = 0;
679 pid = (pid_t) hPID;
681 #endif /* WINDOWS32 */
684 /* Check if this is the child of the `shell' function. */
685 if (!remote && pid == shell_function_pid)
687 /* It is. Leave an indicator for the `shell' function. */
688 if (exit_sig == 0 && exit_code == 127)
689 shell_function_completed = -1;
690 else
691 shell_function_completed = 1;
692 break;
695 child_failed = exit_sig != 0 || exit_code != 0;
697 /* Search for a child matching the deceased one. */
698 lastc = 0;
699 for (c = children; c != 0; lastc = c, c = c->next)
700 if (c->remote == remote && c->pid == pid)
701 break;
703 if (c == 0)
704 /* An unknown child died.
705 Ignore it; it was inherited from our invoker. */
706 continue;
708 DB (DB_JOBS, (child_failed
709 ? _("Reaping losing child %p PID %s %s\n")
710 : _("Reaping winning child %p PID %s %s\n"),
711 c, pid2str (c->pid), c->remote ? _(" (remote)") : ""));
713 if (c->sh_batch_file) {
714 DB (DB_JOBS, (_("Cleaning up temp batch file %s\n"),
715 c->sh_batch_file));
717 /* just try and remove, don't care if this fails */
718 remove (c->sh_batch_file);
720 /* all done with memory */
721 free (c->sh_batch_file);
722 c->sh_batch_file = NULL;
725 /* If this child had the good stdin, say it is now free. */
726 if (c->good_stdin)
727 good_stdin_used = 0;
729 dontcare = c->dontcare;
731 if (child_failed && !c->noerror && !ignore_errors_flag)
733 /* The commands failed. Write an error message,
734 delete non-precious targets, and abort. */
735 static int delete_on_error = -1;
737 if (!dontcare)
738 child_error (c->file->name, exit_code, exit_sig, coredump, 0);
740 c->file->update_status = 2;
741 if (delete_on_error == -1)
743 struct file *f = lookup_file (".DELETE_ON_ERROR");
744 delete_on_error = f != 0 && f->is_target;
746 if (exit_sig != 0 || delete_on_error)
747 delete_child_targets (c);
749 else
751 if (child_failed)
753 /* The commands failed, but we don't care. */
754 child_error (c->file->name,
755 exit_code, exit_sig, coredump, 1);
756 child_failed = 0;
759 /* If there are more commands to run, try to start them. */
760 if (job_next_command (c))
762 if (handling_fatal_signal)
764 /* Never start new commands while we are dying.
765 Since there are more commands that wanted to be run,
766 the target was not completely remade. So we treat
767 this as if a command had failed. */
768 c->file->update_status = 2;
770 else
772 /* Check again whether to start remotely.
773 Whether or not we want to changes over time.
774 Also, start_remote_job may need state set up
775 by start_remote_job_p. */
776 c->remote = start_remote_job_p (0);
777 start_job_command (c);
778 /* Fatal signals are left blocked in case we were
779 about to put that child on the chain. But it is
780 already there, so it is safe for a fatal signal to
781 arrive now; it will clean up this child's targets. */
782 unblock_sigs ();
783 if (c->file->command_state == cs_running)
784 /* We successfully started the new command.
785 Loop to reap more children. */
786 continue;
789 if (c->file->update_status != 0)
790 /* We failed to start the commands. */
791 delete_child_targets (c);
793 else
794 /* There are no more commands. We got through them all
795 without an unignored error. Now the target has been
796 successfully updated. */
797 c->file->update_status = 0;
800 /* When we get here, all the commands for C->file are finished
801 (or aborted) and C->file->update_status contains 0 or 2. But
802 C->file->command_state is still cs_running if all the commands
803 ran; notice_finish_file looks for cs_running to tell it that
804 it's interesting to check the file's modtime again now. */
806 if (! handling_fatal_signal)
807 /* Notice if the target of the commands has been changed.
808 This also propagates its values for command_state and
809 update_status to its also_make files. */
810 notice_finished_file (c->file);
812 DB (DB_JOBS, (_("Removing child %p PID %s%s from chain.\n"),
813 c, pid2str (c->pid), c->remote ? _(" (remote)") : ""));
815 /* Block fatal signals while frobnicating the list, so that
816 children and job_slots_used are always consistent. Otherwise
817 a fatal signal arriving after the child is off the chain and
818 before job_slots_used is decremented would believe a child was
819 live and call reap_children again. */
820 block_sigs ();
822 /* There is now another slot open. */
823 if (job_slots_used > 0)
824 --job_slots_used;
826 /* Remove the child from the chain and free it. */
827 if (lastc == 0)
828 children = c->next;
829 else
830 lastc->next = c->next;
832 free_child (c);
834 unblock_sigs ();
836 /* If the job failed, and the -k flag was not given, die,
837 unless we are already in the process of dying. */
838 if (!err && child_failed && !dontcare && !keep_going_flag &&
839 /* fatal_error_signal will die with the right signal. */
840 !handling_fatal_signal)
841 die (2);
843 /* Only block for one child. */
844 block = 0;
847 return;
850 /* Free the storage allocated for CHILD. */
852 static void
853 free_child (struct child *child)
855 if (!jobserver_tokens)
856 fatal (NILF, "INTERNAL: Freeing child %p (%s) but no tokens left!\n",
857 child, child->file->name);
859 /* If we're using the jobserver and this child is not the only outstanding
860 job, put a token back into the pipe for it. */
862 if (job_fds[1] >= 0 && jobserver_tokens > 1)
864 char token = '+';
865 int r;
867 /* Write a job token back to the pipe. */
869 EINTRLOOP (r, write (job_fds[1], &token, 1));
870 if (r != 1)
871 pfatal_with_name (_("write jobserver"));
873 DB (DB_JOBS, (_("Released token for child %p (%s).\n"),
874 child, child->file->name));
877 --jobserver_tokens;
879 if (handling_fatal_signal) /* Don't bother free'ing if about to die. */
880 return;
882 if (child->command_lines != 0)
884 register unsigned int i;
885 for (i = 0; i < child->file->cmds->ncommand_lines; ++i)
886 free (child->command_lines[i]);
887 free (child->command_lines);
890 if (child->environment != 0)
892 register char **ep = child->environment;
893 while (*ep != 0)
894 free (*ep++);
895 free (child->environment);
898 free (child);
901 #ifdef POSIX
902 extern sigset_t fatal_signal_set;
903 #endif
905 void
906 block_sigs (void)
908 #ifdef POSIX
909 (void) sigprocmask (SIG_BLOCK, &fatal_signal_set, (sigset_t *) 0);
910 #else
911 # ifdef HAVE_SIGSETMASK
912 (void) sigblock (fatal_signal_mask);
913 # endif
914 #endif
917 #ifdef POSIX
918 void
919 unblock_sigs (void)
921 sigset_t empty;
922 sigemptyset (&empty);
923 sigprocmask (SIG_SETMASK, &empty, (sigset_t *) 0);
925 #endif
927 #ifdef MAKE_JOBSERVER
928 RETSIGTYPE
929 job_noop (int sig UNUSED)
932 /* Set the child handler action flags to FLAGS. */
933 static void
934 set_child_handler_action_flags (int set_handler, int set_alarm)
936 struct sigaction sa;
938 #ifdef __EMX__
939 /* The child handler must be turned off here. */
940 signal (SIGCHLD, SIG_DFL);
941 #endif
943 memset (&sa, '\0', sizeof sa);
944 sa.sa_handler = child_handler;
945 sa.sa_flags = set_handler ? 0 : SA_RESTART;
946 #if defined SIGCHLD
947 sigaction (SIGCHLD, &sa, NULL);
948 #endif
949 #if defined SIGCLD && SIGCLD != SIGCHLD
950 sigaction (SIGCLD, &sa, NULL);
951 #endif
952 #if defined SIGALRM
953 if (set_alarm)
955 /* If we're about to enter the read(), set an alarm to wake up in a
956 second so we can check if the load has dropped and we can start more
957 work. On the way out, turn off the alarm and set SIG_DFL. */
958 alarm (set_handler ? 1 : 0);
959 sa.sa_handler = set_handler ? job_noop : SIG_DFL;
960 sa.sa_flags = 0;
961 sigaction (SIGALRM, &sa, NULL);
963 #endif
965 #endif
968 /* Start a job to run the commands specified in CHILD.
969 CHILD is updated to reflect the commands and ID of the child process.
971 NOTE: On return fatal signals are blocked! The caller is responsible
972 for calling `unblock_sigs', once the new child is safely on the chain so
973 it can be cleaned up in the event of a fatal signal. */
975 static void
976 start_job_command (struct child *child)
978 #if !defined(_AMIGA) && !defined(WINDOWS32)
979 static int bad_stdin = -1;
980 #endif
981 char *p;
982 /* Must be volatile to silence bogus GCC warning about longjmp/vfork. */
983 volatile int flags;
984 #ifdef VMS
985 char *argv;
986 #else
987 char **argv;
988 #endif
990 /* If we have a completely empty commandset, stop now. */
991 if (!child->command_ptr)
992 goto next_command;
994 /* Combine the flags parsed for the line itself with
995 the flags specified globally for this target. */
996 flags = (child->file->command_flags
997 | child->file->cmds->lines_flags[child->command_line - 1]);
999 p = child->command_ptr;
1000 child->noerror = ((flags & COMMANDS_NOERROR) != 0);
1002 while (*p != '\0')
1004 if (*p == '@')
1005 flags |= COMMANDS_SILENT;
1006 else if (*p == '+')
1007 flags |= COMMANDS_RECURSE;
1008 else if (*p == '-')
1009 child->noerror = 1;
1010 else if (!isblank ((unsigned char)*p))
1011 break;
1012 ++p;
1015 /* Update the file's command flags with any new ones we found. We only
1016 keep the COMMANDS_RECURSE setting. Even this isn't 100% correct; we are
1017 now marking more commands recursive than should be in the case of
1018 multiline define/endef scripts where only one line is marked "+". In
1019 order to really fix this, we'll have to keep a lines_flags for every
1020 actual line, after expansion. */
1021 child->file->cmds->lines_flags[child->command_line - 1]
1022 |= flags & COMMANDS_RECURSE;
1024 /* Figure out an argument list from this command line. */
1027 char *end = 0;
1028 #ifdef VMS
1029 argv = p;
1030 #else
1031 argv = construct_command_argv (p, &end, child->file,
1032 child->file->cmds->lines_flags[child->command_line - 1],
1033 &child->sh_batch_file);
1034 #endif
1035 if (end == NULL)
1036 child->command_ptr = NULL;
1037 else
1039 *end++ = '\0';
1040 child->command_ptr = end;
1044 /* If -q was given, say that updating `failed' if there was any text on the
1045 command line, or `succeeded' otherwise. The exit status of 1 tells the
1046 user that -q is saying `something to do'; the exit status for a random
1047 error is 2. */
1048 if (argv != 0 && question_flag && !(flags & COMMANDS_RECURSE))
1050 #ifndef VMS
1051 free (argv[0]);
1052 free (argv);
1053 #endif
1054 child->file->update_status = 1;
1055 notice_finished_file (child->file);
1056 return;
1059 if (touch_flag && !(flags & COMMANDS_RECURSE))
1061 /* Go on to the next command. It might be the recursive one.
1062 We construct ARGV only to find the end of the command line. */
1063 #ifndef VMS
1064 if (argv)
1066 free (argv[0]);
1067 free (argv);
1069 #endif
1070 argv = 0;
1073 if (argv == 0)
1075 next_command:
1076 #ifdef __MSDOS__
1077 execute_by_shell = 0; /* in case construct_command_argv sets it */
1078 #endif
1079 /* This line has no commands. Go to the next. */
1080 if (job_next_command (child))
1081 start_job_command (child);
1082 else
1084 /* No more commands. Make sure we're "running"; we might not be if
1085 (e.g.) all commands were skipped due to -n. */
1086 set_command_state (child->file, cs_running);
1087 child->file->update_status = 0;
1088 notice_finished_file (child->file);
1090 return;
1093 /* Print out the command. If silent, we call `message' with null so it
1094 can log the working directory before the command's own error messages
1095 appear. */
1097 message (0, (just_print_flag || (!(flags & COMMANDS_SILENT) && !silent_flag))
1098 ? "%s" : (char *) 0, p);
1100 /* Tell update_goal_chain that a command has been started on behalf of
1101 this target. It is important that this happens here and not in
1102 reap_children (where we used to do it), because reap_children might be
1103 reaping children from a different target. We want this increment to
1104 guaranteedly indicate that a command was started for the dependency
1105 chain (i.e., update_file recursion chain) we are processing. */
1107 ++commands_started;
1109 /* Optimize an empty command. People use this for timestamp rules,
1110 so avoid forking a useless shell. Do this after we increment
1111 commands_started so make still treats this special case as if it
1112 performed some action (makes a difference as to what messages are
1113 printed, etc. */
1115 #if !defined(VMS) && !defined(_AMIGA)
1116 if (
1117 #if defined __MSDOS__ || defined (__EMX__)
1118 unixy_shell /* the test is complicated and we already did it */
1119 #else
1120 (argv[0] && !strcmp (argv[0], "/bin/sh"))
1121 #endif
1122 && (argv[1]
1123 && argv[1][0] == '-' && argv[1][1] == 'c' && argv[1][2] == '\0')
1124 && (argv[2] && argv[2][0] == ':' && argv[2][1] == '\0')
1125 && argv[3] == NULL)
1127 free (argv[0]);
1128 free (argv);
1129 goto next_command;
1131 #endif /* !VMS && !_AMIGA */
1133 /* If -n was given, recurse to get the next line in the sequence. */
1135 if (just_print_flag && !(flags & COMMANDS_RECURSE))
1137 #ifndef VMS
1138 free (argv[0]);
1139 free (argv);
1140 #endif
1141 goto next_command;
1144 /* Flush the output streams so they won't have things written twice. */
1146 fflush (stdout);
1147 fflush (stderr);
1149 #ifndef VMS
1150 #if !defined(WINDOWS32) && !defined(_AMIGA) && !defined(__MSDOS__)
1152 /* Set up a bad standard input that reads from a broken pipe. */
1154 if (bad_stdin == -1)
1156 /* Make a file descriptor that is the read end of a broken pipe.
1157 This will be used for some children's standard inputs. */
1158 int pd[2];
1159 if (pipe (pd) == 0)
1161 /* Close the write side. */
1162 (void) close (pd[1]);
1163 /* Save the read side. */
1164 bad_stdin = pd[0];
1166 /* Set the descriptor to close on exec, so it does not litter any
1167 child's descriptor table. When it is dup2'd onto descriptor 0,
1168 that descriptor will not close on exec. */
1169 CLOSE_ON_EXEC (bad_stdin);
1173 #endif /* !WINDOWS32 && !_AMIGA && !__MSDOS__ */
1175 /* Decide whether to give this child the `good' standard input
1176 (one that points to the terminal or whatever), or the `bad' one
1177 that points to the read side of a broken pipe. */
1179 child->good_stdin = !good_stdin_used;
1180 if (child->good_stdin)
1181 good_stdin_used = 1;
1183 #endif /* !VMS */
1185 child->deleted = 0;
1187 #ifndef _AMIGA
1188 /* Set up the environment for the child. */
1189 if (child->environment == 0)
1190 child->environment = target_environment (child->file);
1191 #endif
1193 #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
1195 #ifndef VMS
1196 /* start_waiting_job has set CHILD->remote if we can start a remote job. */
1197 if (child->remote)
1199 int is_remote, id, used_stdin;
1200 if (start_remote_job (argv, child->environment,
1201 child->good_stdin ? 0 : bad_stdin,
1202 &is_remote, &id, &used_stdin))
1203 /* Don't give up; remote execution may fail for various reasons. If
1204 so, simply run the job locally. */
1205 goto run_local;
1206 else
1208 if (child->good_stdin && !used_stdin)
1210 child->good_stdin = 0;
1211 good_stdin_used = 0;
1213 child->remote = is_remote;
1214 child->pid = id;
1217 else
1218 #endif /* !VMS */
1220 /* Fork the child process. */
1222 char **parent_environ;
1224 run_local:
1225 block_sigs ();
1227 child->remote = 0;
1229 #ifdef VMS
1230 if (!child_execute_job (argv, child)) {
1231 /* Fork failed! */
1232 perror_with_name ("vfork", "");
1233 goto error;
1236 #else
1238 parent_environ = environ;
1240 # ifdef __EMX__
1241 /* If we aren't running a recursive command and we have a jobserver
1242 pipe, close it before exec'ing. */
1243 if (!(flags & COMMANDS_RECURSE) && job_fds[0] >= 0)
1245 CLOSE_ON_EXEC (job_fds[0]);
1246 CLOSE_ON_EXEC (job_fds[1]);
1248 if (job_rfd >= 0)
1249 CLOSE_ON_EXEC (job_rfd);
1251 /* Never use fork()/exec() here! Use spawn() instead in exec_command() */
1252 child->pid = child_execute_job (child->good_stdin ? 0 : bad_stdin, 1,
1253 argv, child->environment);
1254 if (child->pid < 0)
1256 /* spawn failed! */
1257 unblock_sigs ();
1258 perror_with_name ("spawn", "");
1259 goto error;
1262 /* undo CLOSE_ON_EXEC() after the child process has been started */
1263 if (!(flags & COMMANDS_RECURSE) && job_fds[0] >= 0)
1265 fcntl (job_fds[0], F_SETFD, 0);
1266 fcntl (job_fds[1], F_SETFD, 0);
1268 if (job_rfd >= 0)
1269 fcntl (job_rfd, F_SETFD, 0);
1271 #else /* !__EMX__ */
1273 child->pid = vfork ();
1274 environ = parent_environ; /* Restore value child may have clobbered. */
1275 if (child->pid == 0)
1277 /* We are the child side. */
1278 unblock_sigs ();
1280 /* If we aren't running a recursive command and we have a jobserver
1281 pipe, close it before exec'ing. */
1282 if (!(flags & COMMANDS_RECURSE) && job_fds[0] >= 0)
1284 close (job_fds[0]);
1285 close (job_fds[1]);
1287 if (job_rfd >= 0)
1288 close (job_rfd);
1290 #ifdef SET_STACK_SIZE
1291 /* Reset limits, if necessary. */
1292 if (stack_limit.rlim_cur)
1293 setrlimit (RLIMIT_STACK, &stack_limit);
1294 #endif
1296 child_execute_job (child->good_stdin ? 0 : bad_stdin, 1,
1297 argv, child->environment);
1299 else if (child->pid < 0)
1301 /* Fork failed! */
1302 unblock_sigs ();
1303 perror_with_name ("vfork", "");
1304 goto error;
1306 # endif /* !__EMX__ */
1307 #endif /* !VMS */
1310 #else /* __MSDOS__ or Amiga or WINDOWS32 */
1311 #ifdef __MSDOS__
1313 int proc_return;
1315 block_sigs ();
1316 dos_status = 0;
1318 /* We call `system' to do the job of the SHELL, since stock DOS
1319 shell is too dumb. Our `system' knows how to handle long
1320 command lines even if pipes/redirection is needed; it will only
1321 call COMMAND.COM when its internal commands are used. */
1322 if (execute_by_shell)
1324 char *cmdline = argv[0];
1325 /* We don't have a way to pass environment to `system',
1326 so we need to save and restore ours, sigh... */
1327 char **parent_environ = environ;
1329 environ = child->environment;
1331 /* If we have a *real* shell, tell `system' to call
1332 it to do everything for us. */
1333 if (unixy_shell)
1335 /* A *real* shell on MSDOS may not support long
1336 command lines the DJGPP way, so we must use `system'. */
1337 cmdline = argv[2]; /* get past "shell -c" */
1340 dos_command_running = 1;
1341 proc_return = system (cmdline);
1342 environ = parent_environ;
1343 execute_by_shell = 0; /* for the next time */
1345 else
1347 dos_command_running = 1;
1348 proc_return = spawnvpe (P_WAIT, argv[0], argv, child->environment);
1351 /* Need to unblock signals before turning off
1352 dos_command_running, so that child's signals
1353 will be treated as such (see fatal_error_signal). */
1354 unblock_sigs ();
1355 dos_command_running = 0;
1357 /* If the child got a signal, dos_status has its
1358 high 8 bits set, so be careful not to alter them. */
1359 if (proc_return == -1)
1360 dos_status |= 0xff;
1361 else
1362 dos_status |= (proc_return & 0xff);
1363 ++dead_children;
1364 child->pid = dos_pid++;
1366 #endif /* __MSDOS__ */
1367 #ifdef _AMIGA
1368 amiga_status = MyExecute (argv);
1370 ++dead_children;
1371 child->pid = amiga_pid++;
1372 if (amiga_batch_file)
1374 amiga_batch_file = 0;
1375 DeleteFile (amiga_bname); /* Ignore errors. */
1377 #endif /* Amiga */
1378 #ifdef WINDOWS32
1380 HANDLE hPID;
1381 char* arg0;
1383 /* make UNC paths safe for CreateProcess -- backslash format */
1384 arg0 = argv[0];
1385 if (arg0 && arg0[0] == '/' && arg0[1] == '/')
1386 for ( ; arg0 && *arg0; arg0++)
1387 if (*arg0 == '/')
1388 *arg0 = '\\';
1390 /* make sure CreateProcess() has Path it needs */
1391 sync_Path_environment();
1393 hPID = process_easy(argv, child->environment);
1395 if (hPID != INVALID_HANDLE_VALUE)
1396 child->pid = (int) hPID;
1397 else {
1398 int i;
1399 unblock_sigs();
1400 fprintf(stderr,
1401 _("process_easy() failed to launch process (e=%ld)\n"),
1402 process_last_err(hPID));
1403 for (i = 0; argv[i]; i++)
1404 fprintf(stderr, "%s ", argv[i]);
1405 fprintf(stderr, _("\nCounted %d args in failed launch\n"), i);
1406 goto error;
1409 #endif /* WINDOWS32 */
1410 #endif /* __MSDOS__ or Amiga or WINDOWS32 */
1412 /* Bump the number of jobs started in this second. */
1413 ++job_counter;
1415 /* We are the parent side. Set the state to
1416 say the commands are running and return. */
1418 set_command_state (child->file, cs_running);
1420 /* Free the storage used by the child's argument list. */
1421 #ifndef VMS
1422 free (argv[0]);
1423 free (argv);
1424 #endif
1426 return;
1428 error:
1429 child->file->update_status = 2;
1430 notice_finished_file (child->file);
1431 return;
1434 /* Try to start a child running.
1435 Returns nonzero if the child was started (and maybe finished), or zero if
1436 the load was too high and the child was put on the `waiting_jobs' chain. */
1438 static int
1439 start_waiting_job (struct child *c)
1441 struct file *f = c->file;
1443 /* If we can start a job remotely, we always want to, and don't care about
1444 the local load average. We record that the job should be started
1445 remotely in C->remote for start_job_command to test. */
1447 c->remote = start_remote_job_p (1);
1449 /* If we are running at least one job already and the load average
1450 is too high, make this one wait. */
1451 if (!c->remote
1452 && ((job_slots_used > 0 && load_too_high ())
1453 #ifdef WINDOWS32
1454 || (process_used_slots () >= MAXIMUM_WAIT_OBJECTS)
1455 #endif
1458 /* Put this child on the chain of children waiting for the load average
1459 to go down. */
1460 set_command_state (f, cs_running);
1461 c->next = waiting_jobs;
1462 waiting_jobs = c;
1463 return 0;
1466 /* Start the first command; reap_children will run later command lines. */
1467 start_job_command (c);
1469 switch (f->command_state)
1471 case cs_running:
1472 c->next = children;
1473 DB (DB_JOBS, (_("Putting child %p (%s) PID %s%s on the chain.\n"),
1474 c, c->file->name, pid2str (c->pid),
1475 c->remote ? _(" (remote)") : ""));
1476 children = c;
1477 /* One more job slot is in use. */
1478 ++job_slots_used;
1479 unblock_sigs ();
1480 break;
1482 case cs_not_started:
1483 /* All the command lines turned out to be empty. */
1484 f->update_status = 0;
1485 /* FALLTHROUGH */
1487 case cs_finished:
1488 notice_finished_file (f);
1489 free_child (c);
1490 break;
1492 default:
1493 assert (f->command_state == cs_finished);
1494 break;
1497 return 1;
1500 /* Create a `struct child' for FILE and start its commands running. */
1502 void
1503 new_job (struct file *file)
1505 struct commands *cmds = file->cmds;
1506 struct child *c;
1507 char **lines;
1508 unsigned int i;
1510 /* Let any previously decided-upon jobs that are waiting
1511 for the load to go down start before this new one. */
1512 start_waiting_jobs ();
1514 /* Reap any children that might have finished recently. */
1515 reap_children (0, 0);
1517 /* Chop the commands up into lines if they aren't already. */
1518 chop_commands (cmds);
1520 /* Expand the command lines and store the results in LINES. */
1521 lines = xmalloc (cmds->ncommand_lines * sizeof (char *));
1522 for (i = 0; i < cmds->ncommand_lines; ++i)
1524 /* Collapse backslash-newline combinations that are inside variable
1525 or function references. These are left alone by the parser so
1526 that they will appear in the echoing of commands (where they look
1527 nice); and collapsed by construct_command_argv when it tokenizes.
1528 But letting them survive inside function invocations loses because
1529 we don't want the functions to see them as part of the text. */
1531 char *in, *out, *ref;
1533 /* IN points to where in the line we are scanning.
1534 OUT points to where in the line we are writing.
1535 When we collapse a backslash-newline combination,
1536 IN gets ahead of OUT. */
1538 in = out = cmds->command_lines[i];
1539 while ((ref = strchr (in, '$')) != 0)
1541 ++ref; /* Move past the $. */
1543 if (out != in)
1544 /* Copy the text between the end of the last chunk
1545 we processed (where IN points) and the new chunk
1546 we are about to process (where REF points). */
1547 memmove (out, in, ref - in);
1549 /* Move both pointers past the boring stuff. */
1550 out += ref - in;
1551 in = ref;
1553 if (*ref == '(' || *ref == '{')
1555 char openparen = *ref;
1556 char closeparen = openparen == '(' ? ')' : '}';
1557 int count;
1558 char *p;
1560 *out++ = *in++; /* Copy OPENPAREN. */
1561 /* IN now points past the opening paren or brace.
1562 Count parens or braces until it is matched. */
1563 count = 0;
1564 while (*in != '\0')
1566 if (*in == closeparen && --count < 0)
1567 break;
1568 else if (*in == '\\' && in[1] == '\n')
1570 /* We have found a backslash-newline inside a
1571 variable or function reference. Eat it and
1572 any following whitespace. */
1574 int quoted = 0;
1575 for (p = in - 1; p > ref && *p == '\\'; --p)
1576 quoted = !quoted;
1578 if (quoted)
1579 /* There were two or more backslashes, so this is
1580 not really a continuation line. We don't collapse
1581 the quoting backslashes here as is done in
1582 collapse_continuations, because the line will
1583 be collapsed again after expansion. */
1584 *out++ = *in++;
1585 else
1587 /* Skip the backslash, newline and
1588 any following whitespace. */
1589 in = next_token (in + 2);
1591 /* Discard any preceding whitespace that has
1592 already been written to the output. */
1593 while (out > ref
1594 && isblank ((unsigned char)out[-1]))
1595 --out;
1597 /* Replace it all with a single space. */
1598 *out++ = ' ';
1601 else
1603 if (*in == openparen)
1604 ++count;
1606 *out++ = *in++;
1612 /* There are no more references in this line to worry about.
1613 Copy the remaining uninteresting text to the output. */
1614 if (out != in)
1615 memmove (out, in, strlen (in) + 1);
1617 /* Finally, expand the line. */
1618 lines[i] = allocated_variable_expand_for_file (cmds->command_lines[i],
1619 file);
1622 /* Start the command sequence, record it in a new
1623 `struct child', and add that to the chain. */
1625 c = xcalloc (sizeof (struct child));
1626 c->file = file;
1627 c->command_lines = lines;
1628 c->sh_batch_file = NULL;
1630 /* Cache dontcare flag because file->dontcare can be changed once we
1631 return. Check dontcare inheritance mechanism for details. */
1632 c->dontcare = file->dontcare;
1634 /* Fetch the first command line to be run. */
1635 job_next_command (c);
1637 /* Wait for a job slot to be freed up. If we allow an infinite number
1638 don't bother; also job_slots will == 0 if we're using the jobserver. */
1640 if (job_slots != 0)
1641 while (job_slots_used == job_slots)
1642 reap_children (1, 0);
1644 #ifdef MAKE_JOBSERVER
1645 /* If we are controlling multiple jobs make sure we have a token before
1646 starting the child. */
1648 /* This can be inefficient. There's a decent chance that this job won't
1649 actually have to run any subprocesses: the command script may be empty
1650 or otherwise optimized away. It would be nice if we could defer
1651 obtaining a token until just before we need it, in start_job_command.
1652 To do that we'd need to keep track of whether we'd already obtained a
1653 token (since start_job_command is called for each line of the job, not
1654 just once). Also more thought needs to go into the entire algorithm;
1655 this is where the old parallel job code waits, so... */
1657 else if (job_fds[0] >= 0)
1658 while (1)
1660 char token;
1661 int got_token;
1662 int saved_errno;
1664 DB (DB_JOBS, ("Need a job token; we %shave children\n",
1665 children ? "" : "don't "));
1667 /* If we don't already have a job started, use our "free" token. */
1668 if (!jobserver_tokens)
1669 break;
1671 /* Read a token. As long as there's no token available we'll block.
1672 We enable interruptible system calls before the read(2) so that if
1673 we get a SIGCHLD while we're waiting, we'll return with EINTR and
1674 we can process the death(s) and return tokens to the free pool.
1676 Once we return from the read, we immediately reinstate restartable
1677 system calls. This allows us to not worry about checking for
1678 EINTR on all the other system calls in the program.
1680 There is one other twist: there is a span between the time
1681 reap_children() does its last check for dead children and the time
1682 the read(2) call is entered, below, where if a child dies we won't
1683 notice. This is extremely serious as it could cause us to
1684 deadlock, given the right set of events.
1686 To avoid this, we do the following: before we reap_children(), we
1687 dup(2) the read FD on the jobserver pipe. The read(2) call below
1688 uses that new FD. In the signal handler, we close that FD. That
1689 way, if a child dies during the section mentioned above, the
1690 read(2) will be invoked with an invalid FD and will return
1691 immediately with EBADF. */
1693 /* Make sure we have a dup'd FD. */
1694 if (job_rfd < 0)
1696 DB (DB_JOBS, ("Duplicate the job FD\n"));
1697 job_rfd = dup (job_fds[0]);
1700 /* Reap anything that's currently waiting. */
1701 reap_children (0, 0);
1703 /* Kick off any jobs we have waiting for an opportunity that
1704 can run now (ie waiting for load). */
1705 start_waiting_jobs ();
1707 /* If our "free" slot has become available, use it; we don't need an
1708 actual token. */
1709 if (!jobserver_tokens)
1710 break;
1712 /* There must be at least one child already, or we have no business
1713 waiting for a token. */
1714 if (!children)
1715 fatal (NILF, "INTERNAL: no children as we go to sleep on read\n");
1717 /* Set interruptible system calls, and read() for a job token. */
1718 set_child_handler_action_flags (1, waiting_jobs != NULL);
1719 got_token = read (job_rfd, &token, 1);
1720 saved_errno = errno;
1721 set_child_handler_action_flags (0, waiting_jobs != NULL);
1723 /* If we got one, we're done here. */
1724 if (got_token == 1)
1726 DB (DB_JOBS, (_("Obtained token for child %p (%s).\n"),
1727 c, c->file->name));
1728 break;
1731 /* If the error _wasn't_ expected (EINTR or EBADF), punt. Otherwise,
1732 go back and reap_children(), and try again. */
1733 errno = saved_errno;
1734 if (errno != EINTR && errno != EBADF)
1735 pfatal_with_name (_("read jobs pipe"));
1736 if (errno == EBADF)
1737 DB (DB_JOBS, ("Read returned EBADF.\n"));
1739 #endif
1741 ++jobserver_tokens;
1743 /* The job is now primed. Start it running.
1744 (This will notice if there is in fact no recipe.) */
1745 if (cmds->fileinfo.filenm)
1746 DB (DB_BASIC, (_("Invoking recipe from %s:%lu to update target `%s'.\n"),
1747 cmds->fileinfo.filenm, cmds->fileinfo.lineno,
1748 c->file->name));
1749 else
1750 DB (DB_BASIC, (_("Invoking builtin recipe to update target `%s'.\n"),
1751 c->file->name));
1754 start_waiting_job (c);
1756 if (job_slots == 1 || not_parallel)
1757 /* Since there is only one job slot, make things run linearly.
1758 Wait for the child to die, setting the state to `cs_finished'. */
1759 while (file->command_state == cs_running)
1760 reap_children (1, 0);
1762 return;
1765 /* Move CHILD's pointers to the next command for it to execute.
1766 Returns nonzero if there is another command. */
1768 static int
1769 job_next_command (struct child *child)
1771 while (child->command_ptr == 0 || *child->command_ptr == '\0')
1773 /* There are no more lines in the expansion of this line. */
1774 if (child->command_line == child->file->cmds->ncommand_lines)
1776 /* There are no more lines to be expanded. */
1777 child->command_ptr = 0;
1778 return 0;
1780 else
1781 /* Get the next line to run. */
1782 child->command_ptr = child->command_lines[child->command_line++];
1784 return 1;
1787 /* Determine if the load average on the system is too high to start a new job.
1788 The real system load average is only recomputed once a second. However, a
1789 very parallel make can easily start tens or even hundreds of jobs in a
1790 second, which brings the system to its knees for a while until that first
1791 batch of jobs clears out.
1793 To avoid this we use a weighted algorithm to try to account for jobs which
1794 have been started since the last second, and guess what the load average
1795 would be now if it were computed.
1797 This algorithm was provided by Thomas Riedl <thomas.riedl@siemens.com>,
1798 who writes:
1800 ! calculate something load-oid and add to the observed sys.load,
1801 ! so that latter can catch up:
1802 ! - every job started increases jobctr;
1803 ! - every dying job decreases a positive jobctr;
1804 ! - the jobctr value gets zeroed every change of seconds,
1805 ! after its value*weight_b is stored into the 'backlog' value last_sec
1806 ! - weight_a times the sum of jobctr and last_sec gets
1807 ! added to the observed sys.load.
1809 ! The two weights have been tried out on 24 and 48 proc. Sun Solaris-9
1810 ! machines, using a several-thousand-jobs-mix of cpp, cc, cxx and smallish
1811 ! sub-shelled commands (rm, echo, sed...) for tests.
1812 ! lowering the 'direct influence' factor weight_a (e.g. to 0.1)
1813 ! resulted in significant excession of the load limit, raising it
1814 ! (e.g. to 0.5) took bad to small, fast-executing jobs and didn't
1815 ! reach the limit in most test cases.
1817 ! lowering the 'history influence' weight_b (e.g. to 0.1) resulted in
1818 ! exceeding the limit for longer-running stuff (compile jobs in
1819 ! the .5 to 1.5 sec. range),raising it (e.g. to 0.5) overrepresented
1820 ! small jobs' effects.
1824 #define LOAD_WEIGHT_A 0.25
1825 #define LOAD_WEIGHT_B 0.25
1827 static int
1828 load_too_high (void)
1830 #if defined(__MSDOS__) || defined(VMS) || defined(_AMIGA) || defined(__riscos__)
1831 return 1;
1832 #else
1833 static double last_sec;
1834 static time_t last_now;
1835 double load, guess;
1836 time_t now;
1838 #ifdef WINDOWS32
1839 /* sub_proc.c cannot wait for more than MAXIMUM_WAIT_OBJECTS children */
1840 if (process_used_slots () >= MAXIMUM_WAIT_OBJECTS)
1841 return 1;
1842 #endif
1844 if (max_load_average < 0)
1845 return 0;
1847 /* Find the real system load average. */
1848 make_access ();
1849 if (getloadavg (&load, 1) != 1)
1851 static int lossage = -1;
1852 /* Complain only once for the same error. */
1853 if (lossage == -1 || errno != lossage)
1855 if (errno == 0)
1856 /* An errno value of zero means getloadavg is just unsupported. */
1857 error (NILF,
1858 _("cannot enforce load limits on this operating system"));
1859 else
1860 perror_with_name (_("cannot enforce load limit: "), "getloadavg");
1862 lossage = errno;
1863 load = 0;
1865 user_access ();
1867 /* If we're in a new second zero the counter and correct the backlog
1868 value. Only keep the backlog for one extra second; after that it's 0. */
1869 now = time (NULL);
1870 if (last_now < now)
1872 if (last_now == now - 1)
1873 last_sec = LOAD_WEIGHT_B * job_counter;
1874 else
1875 last_sec = 0.0;
1877 job_counter = 0;
1878 last_now = now;
1881 /* Try to guess what the load would be right now. */
1882 guess = load + (LOAD_WEIGHT_A * (job_counter + last_sec));
1884 DB (DB_JOBS, ("Estimated system load = %f (actual = %f) (max requested = %f)\n",
1885 guess, load, max_load_average));
1887 return guess >= max_load_average;
1888 #endif
1891 /* Start jobs that are waiting for the load to be lower. */
1893 void
1894 start_waiting_jobs (void)
1896 struct child *job;
1898 if (waiting_jobs == 0)
1899 return;
1903 /* Check for recently deceased descendants. */
1904 reap_children (0, 0);
1906 /* Take a job off the waiting list. */
1907 job = waiting_jobs;
1908 waiting_jobs = job->next;
1910 /* Try to start that job. We break out of the loop as soon
1911 as start_waiting_job puts one back on the waiting list. */
1913 while (start_waiting_job (job) && waiting_jobs != 0);
1915 return;
1918 #ifndef WINDOWS32
1920 /* EMX: Start a child process. This function returns the new pid. */
1921 # if defined __EMX__
1923 child_execute_job (int stdin_fd, int stdout_fd, char **argv, char **envp)
1925 int pid;
1926 /* stdin_fd == 0 means: nothing to do for stdin;
1927 stdout_fd == 1 means: nothing to do for stdout */
1928 int save_stdin = (stdin_fd != 0) ? dup (0) : 0;
1929 int save_stdout = (stdout_fd != 1) ? dup (1): 1;
1931 /* < 0 only if dup() failed */
1932 if (save_stdin < 0)
1933 fatal (NILF, _("no more file handles: could not duplicate stdin\n"));
1934 if (save_stdout < 0)
1935 fatal (NILF, _("no more file handles: could not duplicate stdout\n"));
1937 /* Close unnecessary file handles for the child. */
1938 if (save_stdin != 0)
1939 CLOSE_ON_EXEC (save_stdin);
1940 if (save_stdout != 1)
1941 CLOSE_ON_EXEC (save_stdout);
1943 /* Connect the pipes to the child process. */
1944 if (stdin_fd != 0)
1945 (void) dup2 (stdin_fd, 0);
1946 if (stdout_fd != 1)
1947 (void) dup2 (stdout_fd, 1);
1949 /* stdin_fd and stdout_fd must be closed on exit because we are
1950 still in the parent process */
1951 if (stdin_fd != 0)
1952 CLOSE_ON_EXEC (stdin_fd);
1953 if (stdout_fd != 1)
1954 CLOSE_ON_EXEC (stdout_fd);
1956 /* Run the command. */
1957 pid = exec_command (argv, envp);
1959 /* Restore stdout/stdin of the parent and close temporary FDs. */
1960 if (stdin_fd != 0)
1962 if (dup2 (save_stdin, 0) != 0)
1963 fatal (NILF, _("Could not restore stdin\n"));
1964 else
1965 close (save_stdin);
1968 if (stdout_fd != 1)
1970 if (dup2 (save_stdout, 1) != 1)
1971 fatal (NILF, _("Could not restore stdout\n"));
1972 else
1973 close (save_stdout);
1976 return pid;
1979 #elif !defined (_AMIGA) && !defined (__MSDOS__) && !defined (VMS)
1981 /* UNIX:
1982 Replace the current process with one executing the command in ARGV.
1983 STDIN_FD and STDOUT_FD are used as the process's stdin and stdout; ENVP is
1984 the environment of the new program. This function does not return. */
1985 void
1986 child_execute_job (int stdin_fd, int stdout_fd, char **argv, char **envp)
1988 if (stdin_fd != 0)
1989 (void) dup2 (stdin_fd, 0);
1990 if (stdout_fd != 1)
1991 (void) dup2 (stdout_fd, 1);
1992 if (stdin_fd != 0)
1993 (void) close (stdin_fd);
1994 if (stdout_fd != 1)
1995 (void) close (stdout_fd);
1997 /* Run the command. */
1998 exec_command (argv, envp);
2000 #endif /* !AMIGA && !__MSDOS__ && !VMS */
2001 #endif /* !WINDOWS32 */
2003 #ifndef _AMIGA
2004 /* Replace the current process with one running the command in ARGV,
2005 with environment ENVP. This function does not return. */
2007 /* EMX: This function returns the pid of the child process. */
2008 # ifdef __EMX__
2010 # else
2011 void
2012 # endif
2013 exec_command (char **argv, char **envp)
2015 #ifdef VMS
2016 /* to work around a problem with signals and execve: ignore them */
2017 #ifdef SIGCHLD
2018 signal (SIGCHLD,SIG_IGN);
2019 #endif
2020 /* Run the program. */
2021 execve (argv[0], argv, envp);
2022 perror_with_name ("execve: ", argv[0]);
2023 _exit (EXIT_FAILURE);
2024 #else
2025 #ifdef WINDOWS32
2026 HANDLE hPID;
2027 HANDLE hWaitPID;
2028 int err = 0;
2029 int exit_code = EXIT_FAILURE;
2031 /* make sure CreateProcess() has Path it needs */
2032 sync_Path_environment();
2034 /* launch command */
2035 hPID = process_easy(argv, envp);
2037 /* make sure launch ok */
2038 if (hPID == INVALID_HANDLE_VALUE)
2040 int i;
2041 fprintf(stderr,
2042 _("process_easy() failed to launch process (e=%ld)\n"),
2043 process_last_err(hPID));
2044 for (i = 0; argv[i]; i++)
2045 fprintf(stderr, "%s ", argv[i]);
2046 fprintf(stderr, _("\nCounted %d args in failed launch\n"), i);
2047 exit(EXIT_FAILURE);
2050 /* wait and reap last child */
2051 hWaitPID = process_wait_for_any();
2052 while (hWaitPID)
2054 /* was an error found on this process? */
2055 err = process_last_err(hWaitPID);
2057 /* get exit data */
2058 exit_code = process_exit_code(hWaitPID);
2060 if (err)
2061 fprintf(stderr, "make (e=%d, rc=%d): %s",
2062 err, exit_code, map_windows32_error_to_string(err));
2064 /* cleanup process */
2065 process_cleanup(hWaitPID);
2067 /* expect to find only last pid, warn about other pids reaped */
2068 if (hWaitPID == hPID)
2069 break;
2070 else
2071 fprintf(stderr,
2072 _("make reaped child pid %Iu, still waiting for pid %Iu\n"),
2073 (DWORD_PTR)hWaitPID, (DWORD_PTR)hPID);
2076 /* return child's exit code as our exit code */
2077 exit(exit_code);
2079 #else /* !WINDOWS32 */
2081 # ifdef __EMX__
2082 int pid;
2083 # endif
2085 /* Be the user, permanently. */
2086 child_access ();
2088 # ifdef __EMX__
2090 /* Run the program. */
2091 pid = spawnvpe (P_NOWAIT, argv[0], argv, envp);
2093 if (pid >= 0)
2094 return pid;
2096 /* the file might have a strange shell extension */
2097 if (errno == ENOENT)
2098 errno = ENOEXEC;
2100 # else
2102 /* Run the program. */
2103 environ = envp;
2104 execvp (argv[0], argv);
2106 # endif /* !__EMX__ */
2108 switch (errno)
2110 case ENOENT:
2111 error (NILF, _("%s: Command not found"), argv[0]);
2112 break;
2113 case ENOEXEC:
2115 /* The file is not executable. Try it as a shell script. */
2116 extern char *getenv ();
2117 char *shell;
2118 char **new_argv;
2119 int argc;
2120 int i=1;
2122 # ifdef __EMX__
2123 /* Do not use $SHELL from the environment */
2124 struct variable *p = lookup_variable ("SHELL", 5);
2125 if (p)
2126 shell = p->value;
2127 else
2128 shell = 0;
2129 # else
2130 shell = getenv ("SHELL");
2131 # endif
2132 if (shell == 0)
2133 shell = default_shell;
2135 argc = 1;
2136 while (argv[argc] != 0)
2137 ++argc;
2139 # ifdef __EMX__
2140 if (!unixy_shell)
2141 ++argc;
2142 # endif
2144 new_argv = alloca ((1 + argc + 1) * sizeof (char *));
2145 new_argv[0] = shell;
2147 # ifdef __EMX__
2148 if (!unixy_shell)
2150 new_argv[1] = "/c";
2151 ++i;
2152 --argc;
2154 # endif
2156 new_argv[i] = argv[0];
2157 while (argc > 0)
2159 new_argv[i + argc] = argv[argc];
2160 --argc;
2163 # ifdef __EMX__
2164 pid = spawnvpe (P_NOWAIT, shell, new_argv, envp);
2165 if (pid >= 0)
2166 break;
2167 # else
2168 execvp (shell, new_argv);
2169 # endif
2170 if (errno == ENOENT)
2171 error (NILF, _("%s: Shell program not found"), shell);
2172 else
2173 perror_with_name ("execvp: ", shell);
2174 break;
2177 # ifdef __EMX__
2178 case EINVAL:
2179 /* this nasty error was driving me nuts :-( */
2180 error (NILF, _("spawnvpe: environment space might be exhausted"));
2181 /* FALLTHROUGH */
2182 # endif
2184 default:
2185 perror_with_name ("execvp: ", argv[0]);
2186 break;
2189 # ifdef __EMX__
2190 return pid;
2191 # else
2192 _exit (127);
2193 # endif
2194 #endif /* !WINDOWS32 */
2195 #endif /* !VMS */
2197 #else /* On Amiga */
2198 void exec_command (char **argv)
2200 MyExecute (argv);
2203 void clean_tmp (void)
2205 DeleteFile (amiga_bname);
2208 #endif /* On Amiga */
2210 #ifndef VMS
2211 /* Figure out the argument list necessary to run LINE as a command. Try to
2212 avoid using a shell. This routine handles only ' quoting, and " quoting
2213 when no backslash, $ or ` characters are seen in the quotes. Starting
2214 quotes may be escaped with a backslash. If any of the characters in
2215 sh_chars[] is seen, or any of the builtin commands listed in sh_cmds[]
2216 is the first word of a line, the shell is used.
2218 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
2219 If *RESTP is NULL, newlines will be ignored.
2221 SHELL is the shell to use, or nil to use the default shell.
2222 IFS is the value of $IFS, or nil (meaning the default).
2224 FLAGS is the value of lines_flags for this command line. It is
2225 used in the WINDOWS32 port to check whether + or $(MAKE) were found
2226 in this command line, in which case the effect of just_print_flag
2227 is overridden. */
2229 static char **
2230 construct_command_argv_internal (char *line, char **restp, char *shell,
2231 char *ifs, int flags,
2232 char **batch_filename_ptr)
2234 #ifdef __MSDOS__
2235 /* MSDOS supports both the stock DOS shell and ports of Unixy shells.
2236 We call `system' for anything that requires ``slow'' processing,
2237 because DOS shells are too dumb. When $SHELL points to a real
2238 (unix-style) shell, `system' just calls it to do everything. When
2239 $SHELL points to a DOS shell, `system' does most of the work
2240 internally, calling the shell only for its internal commands.
2241 However, it looks on the $PATH first, so you can e.g. have an
2242 external command named `mkdir'.
2244 Since we call `system', certain characters and commands below are
2245 actually not specific to COMMAND.COM, but to the DJGPP implementation
2246 of `system'. In particular:
2248 The shell wildcard characters are in DOS_CHARS because they will
2249 not be expanded if we call the child via `spawnXX'.
2251 The `;' is in DOS_CHARS, because our `system' knows how to run
2252 multiple commands on a single line.
2254 DOS_CHARS also include characters special to 4DOS/NDOS, so we
2255 won't have to tell one from another and have one more set of
2256 commands and special characters. */
2257 static char sh_chars_dos[] = "*?[];|<>%^&()";
2258 static char *sh_cmds_dos[] = { "break", "call", "cd", "chcp", "chdir", "cls",
2259 "copy", "ctty", "date", "del", "dir", "echo",
2260 "erase", "exit", "for", "goto", "if", "md",
2261 "mkdir", "path", "pause", "prompt", "rd",
2262 "rmdir", "rem", "ren", "rename", "set",
2263 "shift", "time", "type", "ver", "verify",
2264 "vol", ":", 0 };
2266 static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^";
2267 static char *sh_cmds_sh[] = { "cd", "echo", "eval", "exec", "exit", "login",
2268 "logout", "set", "umask", "wait", "while",
2269 "for", "case", "if", ":", ".", "break",
2270 "continue", "export", "read", "readonly",
2271 "shift", "times", "trap", "switch", "unset",
2272 "ulimit", 0 };
2274 char *sh_chars;
2275 char **sh_cmds;
2276 #elif defined (__EMX__)
2277 static char sh_chars_dos[] = "*?[];|<>%^&()";
2278 static char *sh_cmds_dos[] = { "break", "call", "cd", "chcp", "chdir", "cls",
2279 "copy", "ctty", "date", "del", "dir", "echo",
2280 "erase", "exit", "for", "goto", "if", "md",
2281 "mkdir", "path", "pause", "prompt", "rd",
2282 "rmdir", "rem", "ren", "rename", "set",
2283 "shift", "time", "type", "ver", "verify",
2284 "vol", ":", 0 };
2286 static char sh_chars_os2[] = "*?[];|<>%^()\"'&";
2287 static char *sh_cmds_os2[] = { "call", "cd", "chcp", "chdir", "cls", "copy",
2288 "date", "del", "detach", "dir", "echo",
2289 "endlocal", "erase", "exit", "for", "goto", "if",
2290 "keys", "md", "mkdir", "move", "path", "pause",
2291 "prompt", "rd", "rem", "ren", "rename", "rmdir",
2292 "set", "setlocal", "shift", "start", "time",
2293 "type", "ver", "verify", "vol", ":", 0 };
2295 static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^~'";
2296 static char *sh_cmds_sh[] = { "echo", "cd", "eval", "exec", "exit", "login",
2297 "logout", "set", "umask", "wait", "while",
2298 "for", "case", "if", ":", ".", "break",
2299 "continue", "export", "read", "readonly",
2300 "shift", "times", "trap", "switch", "unset",
2301 0 };
2302 char *sh_chars;
2303 char **sh_cmds;
2305 #elif defined (_AMIGA)
2306 static char sh_chars[] = "#;\"|<>()?*$`";
2307 static char *sh_cmds[] = { "cd", "eval", "if", "delete", "echo", "copy",
2308 "rename", "set", "setenv", "date", "makedir",
2309 "skip", "else", "endif", "path", "prompt",
2310 "unset", "unsetenv", "version",
2311 0 };
2312 #elif defined (WINDOWS32)
2313 static char sh_chars_dos[] = "\"|&<>";
2314 static char *sh_cmds_dos[] = { "assoc", "break", "call", "cd", "chcp",
2315 "chdir", "cls", "color", "copy", "ctty",
2316 "date", "del", "dir", "echo", "echo.",
2317 "endlocal", "erase", "exit", "for", "ftype",
2318 "goto", "if", "if", "md", "mkdir", "path",
2319 "pause", "prompt", "rd", "rem", "ren",
2320 "rename", "rmdir", "set", "setlocal",
2321 "shift", "time", "title", "type", "ver",
2322 "verify", "vol", ":", 0 };
2323 static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^";
2324 static char *sh_cmds_sh[] = { "cd", "eval", "exec", "exit", "login",
2325 "logout", "set", "umask", "wait", "while", "for",
2326 "case", "if", ":", ".", "break", "continue",
2327 "export", "read", "readonly", "shift", "times",
2328 "trap", "switch", "test",
2329 #ifdef BATCH_MODE_ONLY_SHELL
2330 "echo",
2331 #endif
2332 0 };
2333 char* sh_chars;
2334 char** sh_cmds;
2335 #elif defined(__riscos__)
2336 static char sh_chars[] = "";
2337 static char *sh_cmds[] = { 0 };
2338 #else /* must be UNIX-ish */
2339 static char sh_chars[] = "#;\"*?[]&|<>(){}$`^~!";
2340 static char *sh_cmds[] = { ".", ":", "break", "case", "cd", "continue",
2341 "eval", "exec", "exit", "export", "for", "if",
2342 "login", "logout", "read", "readonly", "set",
2343 "shift", "switch", "test", "times", "trap",
2344 "ulimit", "umask", "unset", "wait", "while", 0 };
2345 # ifdef HAVE_DOS_PATHS
2346 /* This is required if the MSYS/Cygwin ports (which do not define
2347 WINDOWS32) are compiled with HAVE_DOS_PATHS defined, which uses
2348 sh_chars_sh[] directly (see below). */
2349 static char *sh_chars_sh = sh_chars;
2350 # endif /* HAVE_DOS_PATHS */
2351 #endif
2352 int i;
2353 char *p;
2354 char *ap;
2355 char *end;
2356 int instring, word_has_equals, seen_nonequals, last_argument_was_empty;
2357 char **new_argv = 0;
2358 char *argstr = 0;
2359 #ifdef WINDOWS32
2360 int slow_flag = 0;
2362 if (!unixy_shell) {
2363 sh_cmds = sh_cmds_dos;
2364 sh_chars = sh_chars_dos;
2365 } else {
2366 sh_cmds = sh_cmds_sh;
2367 sh_chars = sh_chars_sh;
2369 #endif /* WINDOWS32 */
2371 if (restp != NULL)
2372 *restp = NULL;
2374 /* Make sure not to bother processing an empty line. */
2375 while (isblank ((unsigned char)*line))
2376 ++line;
2377 if (*line == '\0')
2378 return 0;
2380 /* See if it is safe to parse commands internally. */
2381 if (shell == 0)
2382 shell = default_shell;
2383 #ifdef WINDOWS32
2384 else if (strcmp (shell, default_shell))
2386 char *s1 = _fullpath (NULL, shell, 0);
2387 char *s2 = _fullpath (NULL, default_shell, 0);
2389 slow_flag = strcmp ((s1 ? s1 : ""), (s2 ? s2 : ""));
2391 if (s1)
2392 free (s1);
2393 if (s2)
2394 free (s2);
2396 if (slow_flag)
2397 goto slow;
2398 #else /* not WINDOWS32 */
2399 #if defined (__MSDOS__) || defined (__EMX__)
2400 else if (strcasecmp (shell, default_shell))
2402 extern int _is_unixy_shell (const char *_path);
2404 DB (DB_BASIC, (_("$SHELL changed (was `%s', now `%s')\n"),
2405 default_shell, shell));
2406 unixy_shell = _is_unixy_shell (shell);
2407 /* we must allocate a copy of shell: construct_command_argv() will free
2408 * shell after this function returns. */
2409 default_shell = xstrdup (shell);
2411 if (unixy_shell)
2413 sh_chars = sh_chars_sh;
2414 sh_cmds = sh_cmds_sh;
2416 else
2418 sh_chars = sh_chars_dos;
2419 sh_cmds = sh_cmds_dos;
2420 # ifdef __EMX__
2421 if (_osmode == OS2_MODE)
2423 sh_chars = sh_chars_os2;
2424 sh_cmds = sh_cmds_os2;
2426 # endif
2428 #else /* !__MSDOS__ */
2429 else if (strcmp (shell, default_shell))
2430 goto slow;
2431 #endif /* !__MSDOS__ && !__EMX__ */
2432 #endif /* not WINDOWS32 */
2434 if (ifs != 0)
2435 for (ap = ifs; *ap != '\0'; ++ap)
2436 if (*ap != ' ' && *ap != '\t' && *ap != '\n')
2437 goto slow;
2439 i = strlen (line) + 1;
2441 /* More than 1 arg per character is impossible. */
2442 new_argv = xmalloc (i * sizeof (char *));
2444 /* All the args can fit in a buffer as big as LINE is. */
2445 ap = new_argv[0] = argstr = xmalloc (i);
2446 end = ap + i;
2448 /* I is how many complete arguments have been found. */
2449 i = 0;
2450 instring = word_has_equals = seen_nonequals = last_argument_was_empty = 0;
2451 for (p = line; *p != '\0'; ++p)
2453 assert (ap <= end);
2455 if (instring)
2457 /* Inside a string, just copy any char except a closing quote
2458 or a backslash-newline combination. */
2459 if (*p == instring)
2461 instring = 0;
2462 if (ap == new_argv[0] || *(ap-1) == '\0')
2463 last_argument_was_empty = 1;
2465 else if (*p == '\\' && p[1] == '\n')
2467 /* Backslash-newline is handled differently depending on what
2468 kind of string we're in: inside single-quoted strings you
2469 keep them; in double-quoted strings they disappear.
2470 For DOS/Windows/OS2, if we don't have a POSIX shell,
2471 we keep the pre-POSIX behavior of removing the
2472 backslash-newline. */
2473 if (instring == '"'
2474 #if defined (__MSDOS__) || defined (__EMX__) || defined (WINDOWS32)
2475 || !unixy_shell
2476 #endif
2478 ++p;
2479 else
2481 *(ap++) = *(p++);
2482 *(ap++) = *p;
2485 else if (*p == '\n' && restp != NULL)
2487 /* End of the command line. */
2488 *restp = p;
2489 goto end_of_line;
2491 /* Backslash, $, and ` are special inside double quotes.
2492 If we see any of those, punt.
2493 But on MSDOS, if we use COMMAND.COM, double and single
2494 quotes have the same effect. */
2495 else if (instring == '"' && strchr ("\\$`", *p) != 0 && unixy_shell)
2496 goto slow;
2497 else
2498 *ap++ = *p;
2500 else if (strchr (sh_chars, *p) != 0)
2501 /* Not inside a string, but it's a special char. */
2502 goto slow;
2503 #ifdef __MSDOS__
2504 else if (*p == '.' && p[1] == '.' && p[2] == '.' && p[3] != '.')
2505 /* `...' is a wildcard in DJGPP. */
2506 goto slow;
2507 #endif
2508 else
2509 /* Not a special char. */
2510 switch (*p)
2512 case '=':
2513 /* Equals is a special character in leading words before the
2514 first word with no equals sign in it. This is not the case
2515 with sh -k, but we never get here when using nonstandard
2516 shell flags. */
2517 if (! seen_nonequals && unixy_shell)
2518 goto slow;
2519 word_has_equals = 1;
2520 *ap++ = '=';
2521 break;
2523 case '\\':
2524 /* Backslash-newline has special case handling, ref POSIX.
2525 We're in the fastpath, so emulate what the shell would do. */
2526 if (p[1] == '\n')
2528 /* Throw out the backslash and newline. */
2529 ++p;
2531 /* If there's nothing in this argument yet, skip any
2532 whitespace before the start of the next word. */
2533 if (ap == new_argv[i])
2534 p = next_token (p + 1) - 1;
2536 else if (p[1] != '\0')
2538 #ifdef HAVE_DOS_PATHS
2539 /* Only remove backslashes before characters special to Unixy
2540 shells. All other backslashes are copied verbatim, since
2541 they are probably DOS-style directory separators. This
2542 still leaves a small window for problems, but at least it
2543 should work for the vast majority of naive users. */
2545 #ifdef __MSDOS__
2546 /* A dot is only special as part of the "..."
2547 wildcard. */
2548 if (strneq (p + 1, ".\\.\\.", 5))
2550 *ap++ = '.';
2551 *ap++ = '.';
2552 p += 4;
2554 else
2555 #endif
2556 if (p[1] != '\\' && p[1] != '\''
2557 && !isspace ((unsigned char)p[1])
2558 && strchr (sh_chars_sh, p[1]) == 0)
2559 /* back up one notch, to copy the backslash */
2560 --p;
2561 #endif /* HAVE_DOS_PATHS */
2563 /* Copy and skip the following char. */
2564 *ap++ = *++p;
2566 break;
2568 case '\'':
2569 case '"':
2570 instring = *p;
2571 break;
2573 case '\n':
2574 if (restp != NULL)
2576 /* End of the command line. */
2577 *restp = p;
2578 goto end_of_line;
2580 else
2581 /* Newlines are not special. */
2582 *ap++ = '\n';
2583 break;
2585 case ' ':
2586 case '\t':
2587 /* We have the end of an argument.
2588 Terminate the text of the argument. */
2589 *ap++ = '\0';
2590 new_argv[++i] = ap;
2591 last_argument_was_empty = 0;
2593 /* Update SEEN_NONEQUALS, which tells us if every word
2594 heretofore has contained an `='. */
2595 seen_nonequals |= ! word_has_equals;
2596 if (word_has_equals && ! seen_nonequals)
2597 /* An `=' in a word before the first
2598 word without one is magical. */
2599 goto slow;
2600 word_has_equals = 0; /* Prepare for the next word. */
2602 /* If this argument is the command name,
2603 see if it is a built-in shell command.
2604 If so, have the shell handle it. */
2605 if (i == 1)
2607 register int j;
2608 for (j = 0; sh_cmds[j] != 0; ++j)
2610 if (streq (sh_cmds[j], new_argv[0]))
2611 goto slow;
2612 # ifdef __EMX__
2613 /* Non-Unix shells are case insensitive. */
2614 if (!unixy_shell
2615 && strcasecmp (sh_cmds[j], new_argv[0]) == 0)
2616 goto slow;
2617 # endif
2621 /* Ignore multiple whitespace chars. */
2622 p = next_token (p) - 1;
2623 break;
2625 default:
2626 *ap++ = *p;
2627 break;
2630 end_of_line:
2632 if (instring)
2633 /* Let the shell deal with an unterminated quote. */
2634 goto slow;
2636 /* Terminate the last argument and the argument list. */
2638 *ap = '\0';
2639 if (new_argv[i][0] != '\0' || last_argument_was_empty)
2640 ++i;
2641 new_argv[i] = 0;
2643 if (i == 1)
2645 register int j;
2646 for (j = 0; sh_cmds[j] != 0; ++j)
2647 if (streq (sh_cmds[j], new_argv[0]))
2648 goto slow;
2651 if (new_argv[0] == 0)
2653 /* Line was empty. */
2654 free (argstr);
2655 free (new_argv);
2656 return 0;
2659 return new_argv;
2661 slow:;
2662 /* We must use the shell. */
2664 if (new_argv != 0)
2666 /* Free the old argument list we were working on. */
2667 free (argstr);
2668 free (new_argv);
2671 #ifdef __MSDOS__
2672 execute_by_shell = 1; /* actually, call `system' if shell isn't unixy */
2673 #endif
2675 #ifdef _AMIGA
2677 char *ptr;
2678 char *buffer;
2679 char *dptr;
2681 buffer = xmalloc (strlen (line)+1);
2683 ptr = line;
2684 for (dptr=buffer; *ptr; )
2686 if (*ptr == '\\' && ptr[1] == '\n')
2687 ptr += 2;
2688 else if (*ptr == '@') /* Kludge: multiline commands */
2690 ptr += 2;
2691 *dptr++ = '\n';
2693 else
2694 *dptr++ = *ptr++;
2696 *dptr = 0;
2698 new_argv = xmalloc (2 * sizeof (char *));
2699 new_argv[0] = buffer;
2700 new_argv[1] = 0;
2702 #else /* Not Amiga */
2703 #ifdef WINDOWS32
2705 * Not eating this whitespace caused things like
2707 * sh -c "\n"
2709 * which gave the shell fits. I think we have to eat
2710 * whitespace here, but this code should be considered
2711 * suspicious if things start failing....
2714 /* Make sure not to bother processing an empty line. */
2715 while (isspace ((unsigned char)*line))
2716 ++line;
2717 if (*line == '\0')
2718 return 0;
2719 #endif /* WINDOWS32 */
2721 /* SHELL may be a multi-word command. Construct a command line
2722 "SHELL -c LINE", with all special chars in LINE escaped.
2723 Then recurse, expanding this command line to get the final
2724 argument list. */
2726 unsigned int shell_len = strlen (shell);
2727 #ifndef VMS
2728 static char minus_c[] = " -c ";
2729 #else
2730 static char minus_c[] = "";
2731 #endif
2732 unsigned int line_len = strlen (line);
2734 char *new_line = alloca (shell_len + (sizeof (minus_c)-1)
2735 + (line_len*2) + 1);
2736 char *command_ptr = NULL; /* used for batch_mode_shell mode */
2738 # ifdef __EMX__ /* is this necessary? */
2739 if (!unixy_shell)
2740 minus_c[1] = '/'; /* " /c " */
2741 # endif
2743 ap = new_line;
2744 memcpy (ap, shell, shell_len);
2745 ap += shell_len;
2746 memcpy (ap, minus_c, sizeof (minus_c) - 1);
2747 ap += sizeof (minus_c) - 1;
2748 command_ptr = ap;
2749 for (p = line; *p != '\0'; ++p)
2751 if (restp != NULL && *p == '\n')
2753 *restp = p;
2754 break;
2756 else if (*p == '\\' && p[1] == '\n')
2758 /* POSIX says we keep the backslash-newline. If we don't have a
2759 POSIX shell on DOS/Windows/OS2, mimic the pre-POSIX behavior
2760 and remove the backslash/newline. */
2761 #if defined (__MSDOS__) || defined (__EMX__) || defined (WINDOWS32)
2762 # define PRESERVE_BSNL unixy_shell
2763 #else
2764 # define PRESERVE_BSNL 1
2765 #endif
2766 if (PRESERVE_BSNL)
2768 *(ap++) = '\\';
2769 /* Only non-batch execution needs another backslash,
2770 because it will be passed through a recursive
2771 invocation of this function. */
2772 if (!batch_mode_shell)
2773 *(ap++) = '\\';
2774 *(ap++) = '\n';
2776 ++p;
2777 continue;
2780 /* DOS shells don't know about backslash-escaping. */
2781 if (unixy_shell && !batch_mode_shell &&
2782 (*p == '\\' || *p == '\'' || *p == '"'
2783 || isspace ((unsigned char)*p)
2784 || strchr (sh_chars, *p) != 0))
2785 *ap++ = '\\';
2786 #ifdef __MSDOS__
2787 else if (unixy_shell && strneq (p, "...", 3))
2789 /* The case of `...' wildcard again. */
2790 strcpy (ap, "\\.\\.\\");
2791 ap += 5;
2792 p += 2;
2794 #endif
2795 *ap++ = *p;
2797 if (ap == new_line + shell_len + sizeof (minus_c) - 1)
2798 /* Line was empty. */
2799 return 0;
2800 *ap = '\0';
2802 #ifdef WINDOWS32
2803 /* Some shells do not work well when invoked as 'sh -c xxx' to run a
2804 command line (e.g. Cygnus GNUWIN32 sh.exe on WIN32 systems). In these
2805 cases, run commands via a script file. */
2806 if (just_print_flag && !(flags & COMMANDS_RECURSE)) {
2807 /* Need to allocate new_argv, although it's unused, because
2808 start_job_command will want to free it and its 0'th element. */
2809 new_argv = xmalloc(2 * sizeof (char *));
2810 new_argv[0] = xstrdup ("");
2811 new_argv[1] = NULL;
2812 } else if ((no_default_sh_exe || batch_mode_shell) && batch_filename_ptr) {
2813 int temp_fd;
2814 FILE* batch = NULL;
2815 int id = GetCurrentProcessId();
2816 PATH_VAR(fbuf);
2818 /* create a file name */
2819 sprintf(fbuf, "make%d", id);
2820 *batch_filename_ptr = create_batch_file (fbuf, unixy_shell, &temp_fd);
2822 DB (DB_JOBS, (_("Creating temporary batch file %s\n"),
2823 *batch_filename_ptr));
2825 /* Create a FILE object for the batch file, and write to it the
2826 commands to be executed. Put the batch file in TEXT mode. */
2827 _setmode (temp_fd, _O_TEXT);
2828 batch = _fdopen (temp_fd, "wt");
2829 if (!unixy_shell)
2830 fputs ("@echo off\n", batch);
2831 fputs (command_ptr, batch);
2832 fputc ('\n', batch);
2833 fclose (batch);
2834 DB (DB_JOBS, (_("Batch file contents:%s\n\t%s\n"),
2835 !unixy_shell ? "\n\t@echo off" : "", command_ptr));
2837 /* create argv */
2838 new_argv = xmalloc(3 * sizeof (char *));
2839 if (unixy_shell) {
2840 new_argv[0] = xstrdup (shell);
2841 new_argv[1] = *batch_filename_ptr; /* only argv[0] gets freed later */
2842 } else {
2843 new_argv[0] = xstrdup (*batch_filename_ptr);
2844 new_argv[1] = NULL;
2846 new_argv[2] = NULL;
2847 } else
2848 #endif /* WINDOWS32 */
2849 if (unixy_shell)
2850 new_argv = construct_command_argv_internal (new_line, 0, 0, 0, flags, 0);
2851 #ifdef __EMX__
2852 else if (!unixy_shell)
2854 /* new_line is local, must not be freed therefore
2855 We use line here instead of new_line because we run the shell
2856 manually. */
2857 size_t line_len = strlen (line);
2858 char *p = new_line;
2859 char *q = new_line;
2860 memcpy (new_line, line, line_len + 1);
2861 /* Replace all backslash-newline combination and also following tabs.
2862 Important: stop at the first '\n' because that's what the loop above
2863 did. The next line starting at restp[0] will be executed during the
2864 next call of this function. */
2865 while (*q != '\0' && *q != '\n')
2867 if (q[0] == '\\' && q[1] == '\n')
2868 q += 2; /* remove '\\' and '\n' */
2869 else
2870 *p++ = *q++;
2872 *p = '\0';
2874 # ifndef NO_CMD_DEFAULT
2875 if (strnicmp (new_line, "echo", 4) == 0
2876 && (new_line[4] == ' ' || new_line[4] == '\t'))
2878 /* the builtin echo command: handle it separately */
2879 size_t echo_len = line_len - 5;
2880 char *echo_line = new_line + 5;
2882 /* special case: echo 'x="y"'
2883 cmd works this way: a string is printed as is, i.e., no quotes
2884 are removed. But autoconf uses a command like echo 'x="y"' to
2885 determine whether make works. autoconf expects the output x="y"
2886 so we will do exactly that.
2887 Note: if we do not allow cmd to be the default shell
2888 we do not need this kind of voodoo */
2889 if (echo_line[0] == '\''
2890 && echo_line[echo_len - 1] == '\''
2891 && strncmp (echo_line + 1, "ac_maketemp=",
2892 strlen ("ac_maketemp=")) == 0)
2894 /* remove the enclosing quotes */
2895 memmove (echo_line, echo_line + 1, echo_len - 2);
2896 echo_line[echo_len - 2] = '\0';
2899 # endif
2902 /* Let the shell decide what to do. Put the command line into the
2903 2nd command line argument and hope for the best ;-) */
2904 size_t sh_len = strlen (shell);
2906 /* exactly 3 arguments + NULL */
2907 new_argv = xmalloc (4 * sizeof (char *));
2908 /* Exactly strlen(shell) + strlen("/c") + strlen(line) + 3 times
2909 the trailing '\0' */
2910 new_argv[0] = xmalloc (sh_len + line_len + 5);
2911 memcpy (new_argv[0], shell, sh_len + 1);
2912 new_argv[1] = new_argv[0] + sh_len + 1;
2913 memcpy (new_argv[1], "/c", 3);
2914 new_argv[2] = new_argv[1] + 3;
2915 memcpy (new_argv[2], new_line, line_len + 1);
2916 new_argv[3] = NULL;
2919 #elif defined(__MSDOS__)
2920 else
2922 /* With MSDOS shells, we must construct the command line here
2923 instead of recursively calling ourselves, because we
2924 cannot backslash-escape the special characters (see above). */
2925 new_argv = xmalloc (sizeof (char *));
2926 line_len = strlen (new_line) - shell_len - sizeof (minus_c) + 1;
2927 new_argv[0] = xmalloc (line_len + 1);
2928 strncpy (new_argv[0],
2929 new_line + shell_len + sizeof (minus_c) - 1, line_len);
2930 new_argv[0][line_len] = '\0';
2932 #else
2933 else
2934 fatal (NILF, _("%s (line %d) Bad shell context (!unixy && !batch_mode_shell)\n"),
2935 __FILE__, __LINE__);
2936 #endif
2938 #endif /* ! AMIGA */
2940 return new_argv;
2942 #endif /* !VMS */
2944 /* Figure out the argument list necessary to run LINE as a command. Try to
2945 avoid using a shell. This routine handles only ' quoting, and " quoting
2946 when no backslash, $ or ` characters are seen in the quotes. Starting
2947 quotes may be escaped with a backslash. If any of the characters in
2948 sh_chars[] is seen, or any of the builtin commands listed in sh_cmds[]
2949 is the first word of a line, the shell is used.
2951 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
2952 If *RESTP is NULL, newlines will be ignored.
2954 FILE is the target whose commands these are. It is used for
2955 variable expansion for $(SHELL) and $(IFS). */
2957 char **
2958 construct_command_argv (char *line, char **restp, struct file *file,
2959 int cmd_flags, char **batch_filename_ptr)
2961 char *shell, *ifs;
2962 char **argv;
2964 #ifdef VMS
2965 char *cptr;
2966 int argc;
2968 argc = 0;
2969 cptr = line;
2970 for (;;)
2972 while ((*cptr != 0)
2973 && (isspace ((unsigned char)*cptr)))
2974 cptr++;
2975 if (*cptr == 0)
2976 break;
2977 while ((*cptr != 0)
2978 && (!isspace((unsigned char)*cptr)))
2979 cptr++;
2980 argc++;
2983 argv = xmalloc (argc * sizeof (char *));
2984 if (argv == 0)
2985 abort ();
2987 cptr = line;
2988 argc = 0;
2989 for (;;)
2991 while ((*cptr != 0)
2992 && (isspace ((unsigned char)*cptr)))
2993 cptr++;
2994 if (*cptr == 0)
2995 break;
2996 DB (DB_JOBS, ("argv[%d] = [%s]\n", argc, cptr));
2997 argv[argc++] = cptr;
2998 while ((*cptr != 0)
2999 && (!isspace((unsigned char)*cptr)))
3000 cptr++;
3001 if (*cptr != 0)
3002 *cptr++ = 0;
3004 #else
3006 /* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
3007 int save = warn_undefined_variables_flag;
3008 warn_undefined_variables_flag = 0;
3010 shell = allocated_variable_expand_for_file ("$(SHELL)", file);
3011 #ifdef WINDOWS32
3013 * Convert to forward slashes so that construct_command_argv_internal()
3014 * is not confused.
3016 if (shell) {
3017 char *p = w32ify (shell, 0);
3018 strcpy (shell, p);
3020 #endif
3021 #ifdef __EMX__
3023 static const char *unixroot = NULL;
3024 static const char *last_shell = "";
3025 static int init = 0;
3026 if (init == 0)
3028 unixroot = getenv ("UNIXROOT");
3029 /* unixroot must be NULL or not empty */
3030 if (unixroot && unixroot[0] == '\0') unixroot = NULL;
3031 init = 1;
3034 /* if we have an unixroot drive and if shell is not default_shell
3035 (which means it's either cmd.exe or the test has already been
3036 performed) and if shell is an absolute path without drive letter,
3037 try whether it exists e.g.: if "/bin/sh" does not exist use
3038 "$UNIXROOT/bin/sh" instead. */
3039 if (unixroot && shell && strcmp (shell, last_shell) != 0
3040 && (shell[0] == '/' || shell[0] == '\\'))
3042 /* trying a new shell, check whether it exists */
3043 size_t size = strlen (shell);
3044 char *buf = xmalloc (size + 7);
3045 memcpy (buf, shell, size);
3046 memcpy (buf + size, ".exe", 5); /* including the trailing '\0' */
3047 if (access (shell, F_OK) != 0 && access (buf, F_OK) != 0)
3049 /* try the same for the unixroot drive */
3050 memmove (buf + 2, buf, size + 5);
3051 buf[0] = unixroot[0];
3052 buf[1] = unixroot[1];
3053 if (access (buf, F_OK) == 0)
3054 /* we have found a shell! */
3055 /* free(shell); */
3056 shell = buf;
3057 else
3058 free (buf);
3060 else
3061 free (buf);
3064 #endif /* __EMX__ */
3066 ifs = allocated_variable_expand_for_file ("$(IFS)", file);
3068 warn_undefined_variables_flag = save;
3071 argv = construct_command_argv_internal (line, restp, shell, ifs,
3072 cmd_flags, batch_filename_ptr);
3074 free (shell);
3075 free (ifs);
3076 #endif /* !VMS */
3077 return argv;
3080 #if !defined(HAVE_DUP2) && !defined(_AMIGA)
3082 dup2 (int old, int new)
3084 int fd;
3086 (void) close (new);
3087 fd = dup (old);
3088 if (fd != new)
3090 (void) close (fd);
3091 errno = EMFILE;
3092 return -1;
3095 return fd;
3097 #endif /* !HAPE_DUP2 && !_AMIGA */
3099 /* On VMS systems, include special VMS functions. */
3101 #ifdef VMS
3102 #include "vmsjobs.c"
3103 #endif