VirtualBox

source: kBuild/trunk/src/kmk/job.c@ 3387

Last change on this file since 3387 was 3312, checked in by bird, 4 years ago

kmk: indent fix

  • Property svn:eol-style set to native
File size: 120.3 KB
Line 
1/* Job execution and handling for GNU Make.
2Copyright (C) 1988-2016 Free Software Foundation, Inc.
3This file is part of GNU Make.
4
5GNU Make is free software; you can redistribute it and/or modify it under the
6terms of the GNU General Public License as published by the Free Software
7Foundation; either version 3 of the License, or (at your option) any later
8version.
9
10GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License along with
15this program. If not, see <http://www.gnu.org/licenses/>. */
16
17#include "makeint.h"
18
19#include <assert.h>
20
21#include "job.h"
22#include "debug.h"
23#include "filedef.h"
24#include "commands.h"
25#include "variable.h"
26#include "os.h"
27#ifdef CONFIG_WITH_KMK_BUILTIN
28# include "kmkbuiltin.h"
29#endif
30#ifdef KMK
31# include "kbuild.h"
32#endif
33
34
35#include <string.h>
36
37/* Default shell to use. */
38#ifdef WINDOWS32
39#include <windows.h>
40
41const char *default_shell = "sh.exe";
42int no_default_sh_exe = 1;
43int batch_mode_shell = 1;
44# ifndef CONFIG_NEW_WIN32_CTRL_EVENT
45HANDLE main_thread;
46# endif
47
48#elif defined (_AMIGA)
49
50const char *default_shell = "";
51extern int MyExecute (char **);
52int batch_mode_shell = 0;
53
54#elif defined (__MSDOS__)
55
56/* The default shell is a pointer so we can change it if Makefile
57 says so. It is without an explicit path so we get a chance
58 to search the $PATH for it (since MSDOS doesn't have standard
59 directories we could trust). */
60const char *default_shell = "command.com";
61int batch_mode_shell = 0;
62
63#elif defined (__EMX__)
64
65const char *default_shell = "sh.exe"; /* bird changed this from "/bin/sh" as that doesn't make sense on OS/2. */
66int batch_mode_shell = 0;
67
68#elif defined (VMS)
69
70# include <descrip.h>
71# include <stsdef.h>
72const char *default_shell = "";
73int batch_mode_shell = 0;
74
75#define strsignal vms_strsignal
76char * vms_strsignal (int status);
77
78#ifndef C_FACILITY_NO
79# define C_FACILITY_NO 0x350000
80#endif
81#ifndef VMS_POSIX_EXIT_MASK
82# define VMS_POSIX_EXIT_MASK (C_FACILITY_NO | 0xA000)
83#endif
84
85#else
86
87const char *default_shell = "/bin/sh";
88int batch_mode_shell = 0;
89
90#endif
91
92#ifdef __MSDOS__
93# include <process.h>
94static int execute_by_shell;
95static int dos_pid = 123;
96int dos_status;
97int dos_command_running;
98#endif /* __MSDOS__ */
99
100#ifdef _AMIGA
101# include <proto/dos.h>
102static int amiga_pid = 123;
103static int amiga_status;
104static char amiga_bname[32];
105static int amiga_batch_file;
106#endif /* Amiga. */
107
108#ifdef VMS
109# ifndef __GNUC__
110# include <processes.h>
111# endif
112# include <starlet.h>
113# include <lib$routines.h>
114static void vmsWaitForChildren (int *);
115#endif
116
117#ifdef WINDOWS32
118# include <windows.h>
119# include <io.h>
120# include <process.h>
121# ifdef CONFIG_NEW_WIN_CHILDREN
122# include "w32/winchildren.h"
123# else
124# include "sub_proc.h"
125# endif
126# include "w32err.h"
127# include "pathstuff.h"
128# define WAIT_NOHANG 1
129#endif /* WINDOWS32 */
130
131#ifdef __EMX__
132# include <process.h>
133#endif
134
135#if defined (HAVE_SYS_WAIT_H) || defined (HAVE_UNION_WAIT)
136# include <sys/wait.h>
137#endif
138
139#ifdef HAVE_WAITPID
140# define WAIT_NOHANG(status) waitpid (-1, (status), WNOHANG)
141#else /* Don't have waitpid. */
142# ifdef HAVE_WAIT3
143# ifndef wait3
144extern int wait3 ();
145# endif
146# define WAIT_NOHANG(status) wait3 ((status), WNOHANG, (struct rusage *) 0)
147# endif /* Have wait3. */
148#endif /* Have waitpid. */
149
150#if !defined (wait) && !defined (POSIX)
151int wait ();
152#endif
153
154#ifndef HAVE_UNION_WAIT
155
156# define WAIT_T int
157
158# ifndef WTERMSIG
159# define WTERMSIG(x) ((x) & 0x7f)
160# endif
161# ifndef WCOREDUMP
162# define WCOREDUMP(x) ((x) & 0x80)
163# endif
164# ifndef WEXITSTATUS
165# define WEXITSTATUS(x) (((x) >> 8) & 0xff)
166# endif
167# ifndef WIFSIGNALED
168# define WIFSIGNALED(x) (WTERMSIG (x) != 0)
169# endif
170# ifndef WIFEXITED
171# define WIFEXITED(x) (WTERMSIG (x) == 0)
172# endif
173
174#else /* Have 'union wait'. */
175
176# define WAIT_T union wait
177# ifndef WTERMSIG
178# define WTERMSIG(x) ((x).w_termsig)
179# endif
180# ifndef WCOREDUMP
181# define WCOREDUMP(x) ((x).w_coredump)
182# endif
183# ifndef WEXITSTATUS
184# define WEXITSTATUS(x) ((x).w_retcode)
185# endif
186# ifndef WIFSIGNALED
187# define WIFSIGNALED(x) (WTERMSIG(x) != 0)
188# endif
189# ifndef WIFEXITED
190# define WIFEXITED(x) (WTERMSIG(x) == 0)
191# endif
192
193#endif /* Don't have 'union wait'. */
194
195#if !defined(HAVE_UNISTD_H) && !defined(WINDOWS32)
196# ifndef _MSC_VER /* bird */
197int dup2 ();
198int execve ();
199void _exit ();
200# endif /* bird */
201# ifndef VMS
202int geteuid ();
203int getegid ();
204int setgid ();
205int getgid ();
206# endif
207#endif
208
209/* Different systems have different requirements for pid_t.
210 Plus we have to support gettext string translation... Argh. */
211static const char *
212pid2str (pid_t pid)
213{
214 static char pidstring[100];
215#if defined(WINDOWS32) && (__GNUC__ > 3 || _MSC_VER > 1300)
216 /* %Id is only needed for 64-builds, which were not supported by
217 older versions of Windows compilers. */
218 sprintf (pidstring, "%Id", pid);
219#else
220 sprintf (pidstring, "%lu", (unsigned long) pid);
221#endif
222 return pidstring;
223}
224
225#ifndef HAVE_GETLOADAVG
226int getloadavg (double loadavg[], int nelem);
227#endif
228
229static void free_child (struct child *);
230static void start_job_command (struct child *child);
231static int load_too_high (void);
232static int job_next_command (struct child *);
233static int start_waiting_job (struct child *);
234#ifdef CONFIG_WITH_PRINT_TIME_SWITCH
235static void print_job_time (struct child *);
236#endif
237
238
239/* Chain of all live (or recently deceased) children. */
240
241struct child *children = 0;
242
243/* Number of children currently running. */
244
245unsigned int job_slots_used = 0;
246
247/* Nonzero if the 'good' standard input is in use. */
248
249static int good_stdin_used = 0;
250
251/* Chain of children waiting to run until the load average goes down. */
252
253static struct child *waiting_jobs = 0;
254
255/* Non-zero if we use a *real* shell (always so on Unix). */
256
257int unixy_shell = 1;
258
259/* Number of jobs started in the current second. */
260
261unsigned long job_counter = 0;
262
263/* Number of jobserver tokens this instance is currently using. */
264
265unsigned int jobserver_tokens = 0;
266
267
268
269#ifdef WINDOWS32
270# ifndef CONFIG_NEW_WIN_CHILDREN /* (only used by commands.c) */
271/*
272 * The macro which references this function is defined in makeint.h.
273 */
274int
275w32_kill (pid_t pid, int sig)
276{
277 return ((process_kill ((HANDLE)pid, sig) == TRUE) ? 0 : -1);
278}
279# endif /* !CONFIG_NEW_WIN_CHILDREN */
280
281/* This function creates a temporary file name with an extension specified
282 * by the unixy arg.
283 * Return an xmalloc'ed string of a newly created temp file and its
284 * file descriptor, or die. */
285static char *
286create_batch_file (char const *base, int unixy, int *fd)
287{
288 const char *const ext = unixy ? "sh" : "bat";
289 const char *error_string = NULL;
290 char temp_path[MAXPATHLEN]; /* need to know its length */
291 unsigned path_size = GetTempPath (sizeof temp_path, temp_path);
292 int path_is_dot = 0;
293 /* The following variable is static so we won't try to reuse a name
294 that was generated a little while ago, because that file might
295 not be on disk yet, since we use FILE_ATTRIBUTE_TEMPORARY below,
296 which tells the OS it doesn't need to flush the cache to disk.
297 If the file is not yet on disk, we might think the name is
298 available, while it really isn't. This happens in parallel
299 builds, where Make doesn't wait for one job to finish before it
300 launches the next one. */
301 static unsigned uniq = 0;
302 static int second_loop = 0;
303 const unsigned sizemax = strlen (base) + strlen (ext) + 10;
304
305 if (path_size == 0)
306 {
307 path_size = GetCurrentDirectory (sizeof temp_path, temp_path);
308 path_is_dot = 1;
309 }
310
311 ++uniq;
312 if (uniq >= 0x10000 && !second_loop)
313 {
314 /* If we already had 64K batch files in this
315 process, make a second loop through the numbers,
316 looking for free slots, i.e. files that were
317 deleted in the meantime. */
318 second_loop = 1;
319 uniq = 1;
320 }
321 while (path_size > 0 &&
322 path_size + sizemax < sizeof temp_path &&
323 !(uniq >= 0x10000 && second_loop))
324 {
325 unsigned size = sprintf (temp_path + path_size,
326 "%s%s-%x.%s",
327 temp_path[path_size - 1] == '\\' ? "" : "\\",
328 base, uniq, ext);
329 HANDLE h = CreateFile (temp_path, /* file name */
330 GENERIC_READ | GENERIC_WRITE, /* desired access */
331 0, /* no share mode */
332 NULL, /* default security attributes */
333 CREATE_NEW, /* creation disposition */
334 FILE_ATTRIBUTE_NORMAL | /* flags and attributes */
335 FILE_ATTRIBUTE_TEMPORARY, /* we'll delete it */
336 NULL); /* no template file */
337
338 if (h == INVALID_HANDLE_VALUE)
339 {
340 const DWORD er = GetLastError ();
341
342 if (er == ERROR_FILE_EXISTS || er == ERROR_ALREADY_EXISTS)
343 {
344 ++uniq;
345 if (uniq == 0x10000 && !second_loop)
346 {
347 second_loop = 1;
348 uniq = 1;
349 }
350 }
351
352 /* the temporary path is not guaranteed to exist */
353 else if (path_is_dot == 0)
354 {
355 path_size = GetCurrentDirectory (sizeof temp_path, temp_path);
356 path_is_dot = 1;
357 }
358
359 else
360 {
361 error_string = map_windows32_error_to_string (er);
362 break;
363 }
364 }
365 else
366 {
367 const unsigned final_size = path_size + size + 1;
368 char *const path = xmalloc (final_size);
369 memcpy (path, temp_path, final_size);
370 *fd = _open_osfhandle ((intptr_t)h, 0);
371 if (unixy)
372 {
373 char *p;
374 int ch;
375 for (p = path; (ch = *p) != 0; ++p)
376 if (ch == '\\')
377 *p = '/';
378 }
379 return path; /* good return */
380 }
381 }
382
383 *fd = -1;
384 if (error_string == NULL)
385 error_string = _("Cannot create a temporary file\n");
386 O (fatal, NILF, error_string);
387
388 /* not reached */
389 return NULL;
390}
391#endif /* WINDOWS32 */
392
393#ifdef __EMX__
394/* returns whether path is assumed to be a unix like shell. */
395int
396_is_unixy_shell (const char *path)
397{
398 /* list of non unix shells */
399 const char *known_os2shells[] = {
400 "cmd.exe",
401 "cmd",
402 "4os2.exe",
403 "4os2",
404 "4dos.exe",
405 "4dos",
406 "command.com",
407 "command",
408 NULL
409 };
410
411 /* find the rightmost '/' or '\\' */
412 const char *name = strrchr (path, '/');
413 const char *p = strrchr (path, '\\');
414 unsigned i;
415
416 if (name && p) /* take the max */
417 name = (name > p) ? name : p;
418 else if (p) /* name must be 0 */
419 name = p;
420 else if (!name) /* name and p must be 0 */
421 name = path;
422
423 if (*name == '/' || *name == '\\') name++;
424
425 i = 0;
426 while (known_os2shells[i] != NULL)
427 {
428 if (strcasecmp (name, known_os2shells[i]) == 0)
429 return 0; /* not a unix shell */
430 i++;
431 }
432
433 /* in doubt assume a unix like shell */
434 return 1;
435}
436#endif /* __EMX__ */
437
438/* determines whether path looks to be a Bourne-like shell. */
439int
440is_bourne_compatible_shell (const char *path)
441{
442 /* List of known POSIX (or POSIX-ish) shells. */
443 static const char *unix_shells[] = {
444 "sh",
445 "bash",
446 "ksh",
447 "rksh",
448 "zsh",
449 "ash",
450 "dash",
451 NULL
452 };
453 const char **s;
454
455 /* find the rightmost '/' or '\\' */
456 const char *name = strrchr (path, '/');
457 char *p = strrchr (path, '\\');
458
459 if (name && p) /* take the max */
460 name = (name > p) ? name : p;
461 else if (p) /* name must be 0 */
462 name = p;
463 else if (!name) /* name and p must be 0 */
464 name = path;
465
466 if (*name == '/' || *name == '\\')
467 ++name;
468
469 /* this should be able to deal with extensions on Windows-like systems */
470 for (s = unix_shells; *s != NULL; ++s)
471 {
472#if defined(WINDOWS32) || defined(__MSDOS__)
473 unsigned int len = strlen (*s);
474 if ((strlen (name) >= len && STOP_SET (name[len], MAP_DOT|MAP_NUL))
475 && strncasecmp (name, *s, len) == 0)
476#else
477 if (strcmp (name, *s) == 0)
478#endif
479 return 1; /* a known unix-style shell */
480 }
481
482 /* if not on the list, assume it's not a Bourne-like shell */
483 return 0;
484}
485
486
487
488/* Write an error message describing the exit status given in
489 EXIT_CODE, EXIT_SIG, and COREDUMP, for the target TARGET_NAME.
490 Append "(ignored)" if IGNORED is nonzero. */
491
492static void
493child_error (struct child *child,
494 int exit_code, int exit_sig, int coredump, int ignored)
495{
496 const char *pre = "*** ";
497 const char *post = "";
498 const char *dump = "";
499 const struct file *f = child->file;
500 const floc *flocp = &f->cmds->fileinfo;
501 const char *nm;
502 size_t l;
503
504 if (ignored && silent_flag)
505 return;
506
507 if (exit_sig && coredump)
508 dump = _(" (core dumped)");
509
510 if (ignored)
511 {
512 pre = "";
513 post = _(" (ignored)");
514 }
515
516 if (! flocp->filenm)
517 nm = _("<builtin>");
518 else
519 {
520 char *a = alloca (strlen (flocp->filenm) + 1 + 11 + 1);
521 sprintf (a, "%s:%lu", flocp->filenm, flocp->lineno + flocp->offset);
522 nm = a;
523 }
524
525 l = strlen (pre) + strlen (nm) + strlen (f->name) + strlen (post);
526
527 OUTPUT_SET (&child->output);
528
529 show_goal_error ();
530
531 if (exit_sig == 0)
532# if defined(KMK) && defined(KBUILD_OS_WINDOWS)
533 {
534 const char *exit_name = NULL;
535 switch ((unsigned)exit_code)
536 {
537 case 0xc0000005U: exit_name = "STATUS_ACCESS_VIOLATION"; break;
538 case 0xc000013aU: exit_name = "STATUS_CONTROL_C_EXIT"; break;
539 case 0xc0000374U: exit_name = "STATUS_HEAP_CORRUPTION"; break;
540 case 0xc0000409U: exit_name = "STATUS_STACK_BUFFER_OVERRUN"; break;
541 case 0xc0000417U: exit_name = "STATUS_INVALID_CRUNTIME_PARAMETER"; break;
542 case 0x80000003U: exit_name = "STATUS_BREAKPOINT"; break;
543 case 0x40000015U: exit_name = "STATUS_FATAL_APP_EXIT"; break;
544 case 0x40010004U: exit_name = "DBG_TERMINATE_PROCESS"; break;
545 case 0x40010005U: exit_name = "DBG_CONTROL_C"; break;
546 case 0x40010008U: exit_name = "DBG_CONTROL_BREAK"; break;
547 }
548 if (exit_name)
549 error (NILF, l + strlen (exit_name) + INTSTR_LENGTH,
550 _("%s[%s: %s] Error %d (%s)%s"),
551 pre, nm, f->name, exit_code, exit_name, post);
552 else
553 error (NILF, l + INTSTR_LENGTH + INTSTR_LENGTH,
554 _("%s[%s: %s] Error %d (%#x)%s"),
555 pre, nm, f->name, exit_code, exit_code, post);
556 }
557# else
558 error (NILF, l + INTSTR_LENGTH,
559 _("%s[%s: %s] Error %d%s"), pre, nm, f->name, exit_code, post);
560# endif
561 else
562 {
563 const char *s = strsignal (exit_sig);
564 error (NILF, l + strlen (s) + strlen (dump),
565 "%s[%s: %s] %s%s%s", pre, nm, f->name, s, dump, post);
566 }
567
568 OUTPUT_UNSET ();
569}
570
571
572
573/* Handle a dead child. This handler may or may not ever be installed.
574
575 If we're using the jobserver feature without pselect(), we need it.
576 First, installing it ensures the read will interrupt on SIGCHLD. Second,
577 we close the dup'd read FD to ensure we don't enter another blocking read
578 without reaping all the dead children. In this case we don't need the
579 dead_children count.
580
581 If we don't have either waitpid or wait3, then make is unreliable, but we
582 use the dead_children count to reap children as best we can. */
583
584static unsigned int dead_children = 0;
585
586RETSIGTYPE
587child_handler (int sig UNUSED)
588{
589 ++dead_children;
590
591 jobserver_signal ();
592
593#if defined __EMX__ && !defined(__INNOTEK_LIBC__) /* bird */
594 /* The signal handler must called only once! */
595 signal (SIGCHLD, SIG_DFL);
596#endif
597}
598
599extern pid_t shell_function_pid;
600
601/* Reap all dead children, storing the returned status and the new command
602 state ('cs_finished') in the 'file' member of the 'struct child' for the
603 dead child, and removing the child from the chain. In addition, if BLOCK
604 nonzero, we block in this function until we've reaped at least one
605 complete child, waiting for it to die if necessary. If ERR is nonzero,
606 print an error message first. */
607
608void
609reap_children (int block, int err)
610{
611#ifndef WINDOWS32
612 WAIT_T status;
613#endif
614 /* Initially, assume we have some. */
615 int reap_more = 1;
616
617#ifdef WAIT_NOHANG
618# define REAP_MORE reap_more
619#else
620# define REAP_MORE dead_children
621#endif
622
623 /* As long as:
624
625 We have at least one child outstanding OR a shell function in progress,
626 AND
627 We're blocking for a complete child OR there are more children to reap
628
629 we'll keep reaping children. */
630
631 while ((children != 0 || shell_function_pid != 0)
632 && (block || REAP_MORE))
633 {
634 unsigned int remote = 0;
635 pid_t pid;
636 int exit_code, exit_sig, coredump;
637 struct child *lastc, *c;
638 int child_failed;
639 int any_remote, any_local;
640 int dontcare;
641#ifdef CONFIG_WITH_KMK_BUILTIN
642 struct child *completed_child = NULL;
643#endif
644
645 if (err && block)
646 {
647 static int printed = 0;
648
649 /* We might block for a while, so let the user know why.
650 Only print this message once no matter how many jobs are left. */
651 fflush (stdout);
652 if (!printed)
653 O (error, NILF, _("*** Waiting for unfinished jobs...."));
654 printed = 1;
655 }
656
657 /* We have one less dead child to reap. As noted in
658 child_handler() above, this count is completely unimportant for
659 all modern, POSIX-y systems that support wait3() or waitpid().
660 The rest of this comment below applies only to early, broken
661 pre-POSIX systems. We keep the count only because... it's there...
662
663 The test and decrement are not atomic; if it is compiled into:
664 register = dead_children - 1;
665 dead_children = register;
666 a SIGCHLD could come between the two instructions.
667 child_handler increments dead_children.
668 The second instruction here would lose that increment. But the
669 only effect of dead_children being wrong is that we might wait
670 longer than necessary to reap a child, and lose some parallelism;
671 and we might print the "Waiting for unfinished jobs" message above
672 when not necessary. */
673
674 if (dead_children > 0)
675 --dead_children;
676
677 any_remote = 0;
678 any_local = shell_function_pid != 0;
679 for (c = children; c != 0; c = c->next)
680 {
681 any_remote |= c->remote;
682 any_local |= ! c->remote;
683#ifdef CONFIG_WITH_KMK_BUILTIN
684 if (c->has_status)
685 {
686 completed_child = c;
687 DB (DB_JOBS, (_("builtin child %p (%s) PID %s %s Status %ld\n"),
688 (void *)c, c->file->name,
689 pid2str (c->pid), c->remote ? _(" (remote)") : "",
690 (long) c->status));
691 }
692 else
693#endif
694 DB (DB_JOBS, (_("Live child %p (%s) PID %s %s\n"),
695 (void *)c, c->file->name, pid2str (c->pid),
696 c->remote ? _(" (remote)") : ""));
697#ifdef VMS
698 break;
699#endif
700 }
701
702 /* First, check for remote children. */
703 if (any_remote)
704 pid = remote_status (&exit_code, &exit_sig, &coredump, 0);
705 else
706 pid = 0;
707
708 if (pid > 0)
709 /* We got a remote child. */
710 remote = 1;
711 else if (pid < 0)
712 {
713 /* A remote status command failed miserably. Punt. */
714 remote_status_lose:
715 pfatal_with_name ("remote_status");
716 }
717 else
718 {
719 /* No remote children. Check for local children. */
720#ifdef CONFIG_WITH_KMK_BUILTIN
721 if (completed_child)
722 {
723 pid = completed_child->pid;
724# if defined(WINDOWS32)
725 exit_code = completed_child->status;
726 exit_sig = 0;
727 coredump = 0;
728# else
729 status = (WAIT_T)completed_child->status;
730# endif
731 }
732 else
733#endif /* CONFIG_WITH_KMK_BUILTIN */
734#if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
735 if (any_local)
736 {
737#ifdef VMS
738 /* Todo: This needs more untangling multi-process support */
739 /* Just do single child process support now */
740 vmsWaitForChildren (&status);
741 pid = c->pid;
742
743 /* VMS failure status can not be fully translated */
744 status = $VMS_STATUS_SUCCESS (c->cstatus) ? 0 : (1 << 8);
745
746 /* A Posix failure can be exactly translated */
747 if ((c->cstatus & VMS_POSIX_EXIT_MASK) == VMS_POSIX_EXIT_MASK)
748 status = (c->cstatus >> 3 & 255) << 8;
749#else
750#ifdef WAIT_NOHANG
751 if (!block)
752 pid = WAIT_NOHANG (&status);
753 else
754#endif
755 EINTRLOOP (pid, wait (&status));
756#endif /* !VMS */
757 }
758 else
759 pid = 0;
760
761 if (pid < 0)
762 {
763 /* The wait*() failed miserably. Punt. */
764 pfatal_with_name ("wait");
765 }
766 else if (pid > 0)
767 {
768 /* We got a child exit; chop the status word up. */
769 exit_code = WEXITSTATUS (status);
770 exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0;
771 coredump = WCOREDUMP (status);
772
773 /* If we have started jobs in this second, remove one. */
774 if (job_counter)
775 --job_counter;
776 }
777 else
778 {
779 /* No local children are dead. */
780 reap_more = 0;
781
782 if (!block || !any_remote)
783 break;
784
785 /* Now try a blocking wait for a remote child. */
786 pid = remote_status (&exit_code, &exit_sig, &coredump, 1);
787 if (pid < 0)
788 goto remote_status_lose;
789 else if (pid == 0)
790 /* No remote children either. Finally give up. */
791 break;
792
793 /* We got a remote child. */
794 remote = 1;
795 }
796#endif /* !__MSDOS__, !Amiga, !WINDOWS32. */
797
798#ifdef __MSDOS__
799 /* Life is very different on MSDOS. */
800 pid = dos_pid - 1;
801 status = dos_status;
802 exit_code = WEXITSTATUS (status);
803 if (exit_code == 0xff)
804 exit_code = -1;
805 exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0;
806 coredump = 0;
807#endif /* __MSDOS__ */
808#ifdef _AMIGA
809 /* Same on Amiga */
810 pid = amiga_pid - 1;
811 status = amiga_status;
812 exit_code = amiga_status;
813 exit_sig = 0;
814 coredump = 0;
815#endif /* _AMIGA */
816#ifdef WINDOWS32
817 {
818# ifndef CONFIG_NEW_WIN_CHILDREN
819 HANDLE hPID;
820 HANDLE hcTID, hcPID;
821 DWORD dwWaitStatus = 0;
822 exit_code = 0;
823 exit_sig = 0;
824 coredump = 0;
825
826# ifndef CONFIG_NEW_WIN32_CTRL_EVENT
827 /* Record the thread ID of the main process, so that we
828 could suspend it in the signal handler. */
829 if (!main_thread)
830 {
831 hcTID = GetCurrentThread ();
832 hcPID = GetCurrentProcess ();
833 if (!DuplicateHandle (hcPID, hcTID, hcPID, &main_thread, 0,
834 FALSE, DUPLICATE_SAME_ACCESS))
835 {
836 DWORD e = GetLastError ();
837 fprintf (stderr,
838 "Determine main thread ID (Error %ld: %s)\n",
839 e, map_windows32_error_to_string (e));
840 }
841 else
842 DB (DB_VERBOSE, ("Main thread handle = %p\n", main_thread));
843 }
844# endif
845
846 /* wait for anything to finish */
847 hPID = process_wait_for_any (block, &dwWaitStatus);
848 if (hPID)
849 {
850 /* was an error found on this process? */
851 int werr = process_last_err (hPID);
852
853 /* get exit data */
854 exit_code = process_exit_code (hPID);
855
856 if (werr)
857 fprintf (stderr, "make (e=%d): %s", exit_code,
858 map_windows32_error_to_string (exit_code));
859
860 /* signal */
861 exit_sig = process_signal (hPID);
862
863 /* cleanup process */
864 process_cleanup (hPID);
865
866 coredump = 0;
867 }
868 else if (dwWaitStatus == WAIT_FAILED)
869 {
870 /* The WaitForMultipleObjects() failed miserably. Punt. */
871 pfatal_with_name ("WaitForMultipleObjects");
872 }
873 else if (dwWaitStatus == WAIT_TIMEOUT)
874 {
875 /* No child processes are finished. Give up waiting. */
876 reap_more = 0;
877 break;
878 }
879
880 pid = (pid_t) hPID;
881# else /* CONFIG_NEW_WIN_CHILDREN */
882# ifndef CONFIG_NEW_WIN32_CTRL_EVENT
883 /* Ctrl-C handler needs to suspend the main thread handle to
884 prevent mayhem when concurrently calling reap_children. */
885 if ( !main_thread
886 && !DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
887 GetCurrentProcess (), &main_thread, 0,
888 FALSE, DUPLICATE_SAME_ACCESS))
889 fprintf (stderr, "Failed to duplicate main thread handle: %u\n",
890 GetLastError ());
891# endif
892
893 assert (!any_remote);
894 pid = 0;
895 coredump = exit_sig = exit_code = 0;
896 {
897 int rc = MkWinChildWait(block, &pid, &exit_code, &exit_sig, &coredump, &c);
898 if (rc != 0)
899 ON (fatal, NILF, _("MkWinChildWait: %u"), rc);
900 }
901 if (pid == 0)
902 {
903 /* No more children, stop. */
904 reap_more = 0;
905 break;
906 }
907
908 /* If we have started jobs in this second, remove one. */
909 if (job_counter)
910 --job_counter;
911# endif /* CONFIG_NEW_WIN_CHILDREN */
912 }
913#endif /* WINDOWS32 */
914 }
915
916 /* Check if this is the child of the 'shell' function. */
917 if (!remote && pid == shell_function_pid)
918 {
919 shell_completed (exit_code, exit_sig);
920 break;
921 }
922
923 /* Search for a child matching the deceased one. */
924 lastc = 0;
925 for (c = children; c != 0; lastc = c, c = c->next)
926 if (c->pid == pid && c->remote == remote)
927 break;
928
929 if (c == 0)
930 /* An unknown child died.
931 Ignore it; it was inherited from our invoker. */
932 continue;
933
934 /* Determine the failure status: 0 for success, 1 for updating target in
935 question mode, 2 for anything else. */
936 if (exit_sig == 0 && exit_code == 0)
937 child_failed = MAKE_SUCCESS;
938 else if (exit_sig == 0 && exit_code == 1 && question_flag && c->recursive)
939 child_failed = MAKE_TROUBLE;
940 else
941 child_failed = MAKE_FAILURE;
942
943 DB (DB_JOBS, (child_failed
944 ? _("Reaping losing child %p PID %s %s\n")
945 : _("Reaping winning child %p PID %s %s\n"),
946 (void *)c, pid2str (c->pid), c->remote ? _(" (remote)") : ""));
947
948 if (c->sh_batch_file)
949 {
950 int rm_status;
951
952 DB (DB_JOBS, (_("Cleaning up temp batch file %s\n"),
953 c->sh_batch_file));
954
955 errno = 0;
956 rm_status = remove (c->sh_batch_file);
957 if (rm_status)
958 DB (DB_JOBS, (_("Cleaning up temp batch file %s failed (%d)\n"),
959 c->sh_batch_file, errno));
960
961 /* all done with memory */
962 free (c->sh_batch_file);
963 c->sh_batch_file = NULL;
964 }
965
966 /* If this child had the good stdin, say it is now free. */
967 if (c->good_stdin)
968 good_stdin_used = 0;
969
970 dontcare = c->dontcare;
971
972 if (child_failed && !c->noerror && !ignore_errors_flag)
973 {
974 /* The commands failed. Write an error message,
975 delete non-precious targets, and abort. */
976 static int delete_on_error = -1;
977
978 if (!dontcare && child_failed == MAKE_FAILURE)
979#ifdef KMK
980 {
981 child_error (c, exit_code, exit_sig, coredump, 0);
982 if ( ( c->file->cmds->lines_flags[c->command_line - 1]
983 & (COMMANDS_SILENT | COMMANDS_RECURSE))
984 == COMMANDS_SILENT
985# ifdef KBUILD_OS_WINDOWS /* show commands for NT statuses */
986 || (exit_code & 0xc0000000)
987# endif
988 || exit_sig != 0)
989 OS (message, 0, "The failing command:\n%s", c->file->cmds->command_lines[c->command_line - 1]);
990 }
991#else /* !KMK */
992 child_error (c, exit_code, exit_sig, coredump, 0);
993#endif /* !KMK */
994
995 c->file->update_status = child_failed == MAKE_FAILURE ? us_failed : us_question;
996 if (delete_on_error == -1)
997 {
998 struct file *f = lookup_file (".DELETE_ON_ERROR");
999 delete_on_error = f != 0 && f->is_target;
1000 }
1001 if (exit_sig != 0 || delete_on_error)
1002 delete_child_targets (c);
1003 }
1004 else
1005 {
1006 if (child_failed)
1007 {
1008 /* The commands failed, but we don't care. */
1009 child_error (c, exit_code, exit_sig, coredump, 1);
1010 child_failed = 0;
1011 }
1012
1013 /* If there are more commands to run, try to start them. */
1014 if (job_next_command (c))
1015 {
1016 if (handling_fatal_signal)
1017 {
1018 /* Never start new commands while we are dying.
1019 Since there are more commands that wanted to be run,
1020 the target was not completely remade. So we treat
1021 this as if a command had failed. */
1022 c->file->update_status = us_failed;
1023 }
1024 else
1025 {
1026#ifndef NO_OUTPUT_SYNC
1027 /* If we're sync'ing per line, write the previous line's
1028 output before starting the next one. */
1029 if (output_sync == OUTPUT_SYNC_LINE)
1030 output_dump (&c->output);
1031#endif
1032 /* Check again whether to start remotely.
1033 Whether or not we want to changes over time.
1034 Also, start_remote_job may need state set up
1035 by start_remote_job_p. */
1036 c->remote = start_remote_job_p (0);
1037 start_job_command (c);
1038 /* Fatal signals are left blocked in case we were
1039 about to put that child on the chain. But it is
1040 already there, so it is safe for a fatal signal to
1041 arrive now; it will clean up this child's targets. */
1042 unblock_sigs ();
1043 if (c->file->command_state == cs_running)
1044 /* We successfully started the new command.
1045 Loop to reap more children. */
1046 continue;
1047 }
1048
1049 if (c->file->update_status != us_success)
1050 /* We failed to start the commands. */
1051 delete_child_targets (c);
1052 }
1053 else
1054 /* There are no more commands. We got through them all
1055 without an unignored error. Now the target has been
1056 successfully updated. */
1057 c->file->update_status = us_success;
1058 }
1059
1060 /* When we get here, all the commands for c->file are finished. */
1061
1062#ifndef NO_OUTPUT_SYNC
1063 /* Synchronize any remaining parallel output. */
1064 output_dump (&c->output);
1065#endif
1066
1067 /* At this point c->file->update_status is success or failed. But
1068 c->file->command_state is still cs_running if all the commands
1069 ran; notice_finish_file looks for cs_running to tell it that
1070 it's interesting to check the file's modtime again now. */
1071
1072 if (! handling_fatal_signal)
1073 /* Notice if the target of the commands has been changed.
1074 This also propagates its values for command_state and
1075 update_status to its also_make files. */
1076 notice_finished_file (c->file);
1077
1078 DB (DB_JOBS, (_("Removing child %p PID %s%s from chain.\n"),
1079 (void *)c, pid2str (c->pid), c->remote ? _(" (remote)") : ""));
1080
1081 /* Block fatal signals while frobnicating the list, so that
1082 children and job_slots_used are always consistent. Otherwise
1083 a fatal signal arriving after the child is off the chain and
1084 before job_slots_used is decremented would believe a child was
1085 live and call reap_children again. */
1086 block_sigs ();
1087
1088 /* There is now another slot open. */
1089 if (job_slots_used > 0)
1090 --job_slots_used;
1091
1092 /* Remove the child from the chain and free it. */
1093 if (lastc == 0)
1094 children = c->next;
1095 else
1096 lastc->next = c->next;
1097
1098 free_child (c);
1099
1100 unblock_sigs ();
1101
1102 /* If the job failed, and the -k flag was not given, die,
1103 unless we are already in the process of dying. */
1104 if (!err && child_failed && !dontcare && !keep_going_flag &&
1105 /* fatal_error_signal will die with the right signal. */
1106 !handling_fatal_signal)
1107 die (child_failed);
1108
1109 /* Only block for one child. */
1110 block = 0;
1111 }
1112
1113 return;
1114}
1115
1116
1117/* Free the storage allocated for CHILD. */
1118
1119static void
1120free_child (struct child *child)
1121{
1122#ifdef CONFIG_WITH_PRINT_TIME_SWITCH
1123 print_job_time (child);
1124#endif
1125 output_close (&child->output);
1126
1127 /* bird: Make sure the output_context doesn't point to a freed structure when
1128 we return from this function. This is probably an issue elsewhere
1129 in the code, however it doesn't cost us much fixing it here. (The
1130 access after free was caught in a die() scenario, both in error
1131 situations and successful ones.) */
1132 if (output_context == &child->output)
1133 OUTPUT_UNSET();
1134
1135 if (!jobserver_tokens)
1136 ONS (fatal, NILF, "INTERNAL: Freeing child %p (%s) but no tokens left!\n",
1137 (void *)child, child->file->name);
1138
1139 /* If we're using the jobserver and this child is not the only outstanding
1140 job, put a token back into the pipe for it. */
1141
1142 if (jobserver_enabled () && jobserver_tokens > 1)
1143 {
1144 jobserver_release (1);
1145 DB (DB_JOBS, (_("Released token for child %p (%s).\n"),
1146 (void *)child, child->file->name));
1147 }
1148
1149 --jobserver_tokens;
1150
1151 if (handling_fatal_signal) /* Don't bother free'ing if about to die. */
1152 return;
1153
1154 if (child->command_lines != 0)
1155 {
1156 register unsigned int i;
1157 for (i = 0; i < child->file->cmds->ncommand_lines; ++i)
1158 free (child->command_lines[i]);
1159 free (child->command_lines);
1160 }
1161
1162 if (child->environment != 0)
1163 {
1164 register char **ep = child->environment;
1165 while (*ep != 0)
1166 free (*ep++);
1167 free (child->environment);
1168 }
1169
1170#ifdef CONFIG_WITH_MEMORY_OPTIMIZATIONS
1171 /* Free the chopped command lines for simple targets when
1172 there are no more active references to them. */
1173
1174 child->file->cmds->refs--;
1175 if ( !child->file->intermediate
1176 && !child->file->pat_variables)
1177 free_chopped_commands(child->file->cmds);
1178#endif /* CONFIG_WITH_MEMORY_OPTIMIZATIONS */
1179
1180 free (child);
1181}
1182
1183
1184#ifdef POSIX
1185extern sigset_t fatal_signal_set;
1186#endif
1187
1188void
1189block_sigs (void)
1190{
1191#ifdef POSIX
1192 (void) sigprocmask (SIG_BLOCK, &fatal_signal_set, (sigset_t *) 0);
1193#else
1194# ifdef HAVE_SIGSETMASK
1195 (void) sigblock (fatal_signal_mask);
1196# endif
1197#endif
1198}
1199
1200#ifdef POSIX
1201void
1202unblock_sigs (void)
1203{
1204 sigset_t empty;
1205 sigemptyset (&empty);
1206 sigprocmask (SIG_SETMASK, &empty, (sigset_t *) 0);
1207}
1208#endif
1209
1210/* Start a job to run the commands specified in CHILD.
1211 CHILD is updated to reflect the commands and ID of the child process.
1212
1213 NOTE: On return fatal signals are blocked! The caller is responsible
1214 for calling 'unblock_sigs', once the new child is safely on the chain so
1215 it can be cleaned up in the event of a fatal signal. */
1216
1217static void
1218start_job_command (struct child *child)
1219{
1220 int flags;
1221 char *p;
1222#ifdef VMS
1223 char *argv;
1224#else
1225 char **argv;
1226#endif
1227
1228 /* If we have a completely empty commandset, stop now. */
1229 if (!child->command_ptr)
1230 goto next_command;
1231
1232#ifdef CONFIG_WITH_PRINT_TIME_SWITCH
1233 if (child->start_ts == -1)
1234 child->start_ts = nano_timestamp ();
1235#endif
1236
1237 /* Combine the flags parsed for the line itself with
1238 the flags specified globally for this target. */
1239 flags = (child->file->command_flags
1240 | child->file->cmds->lines_flags[child->command_line - 1]);
1241
1242 p = child->command_ptr;
1243 child->noerror = ((flags & COMMANDS_NOERROR) != 0);
1244
1245 while (*p != '\0')
1246 {
1247 if (*p == '@')
1248 flags |= COMMANDS_SILENT;
1249 else if (*p == '+')
1250 flags |= COMMANDS_RECURSE;
1251 else if (*p == '-')
1252 child->noerror = 1;
1253#ifdef CONFIG_WITH_COMMANDS_FUNC
1254 else if (*p == '%')
1255 flags |= COMMAND_GETTER_SKIP_IT;
1256#endif
1257 /* Don't skip newlines. */
1258 else if (!ISBLANK (*p))
1259#ifndef CONFIG_WITH_KMK_BUILTIN
1260 break;
1261#else /* CONFIG_WITH_KMK_BUILTIN */
1262
1263 {
1264 if ( !(flags & COMMANDS_KMK_BUILTIN)
1265 && !strncmp(p, "kmk_builtin_", sizeof("kmk_builtin_") - 1))
1266 flags |= COMMANDS_KMK_BUILTIN;
1267 break;
1268 }
1269#endif /* CONFIG_WITH_KMK_BUILTIN */
1270 ++p;
1271 }
1272
1273 child->recursive = ((flags & COMMANDS_RECURSE) != 0);
1274
1275 /* Update the file's command flags with any new ones we found. We only
1276 keep the COMMANDS_RECURSE setting. Even this isn't 100% correct; we are
1277 now marking more commands recursive than should be in the case of
1278 multiline define/endef scripts where only one line is marked "+". In
1279 order to really fix this, we'll have to keep a lines_flags for every
1280 actual line, after expansion. */
1281 child->file->cmds->lines_flags[child->command_line - 1] |= flags & COMMANDS_RECURSE;
1282
1283 /* POSIX requires that a recipe prefix after a backslash-newline should
1284 be ignored. Remove it now so the output is correct. */
1285 {
1286 char prefix = child->file->cmds->recipe_prefix;
1287 char *p1, *p2;
1288 p1 = p2 = p;
1289 while (*p1 != '\0')
1290 {
1291 *(p2++) = *p1;
1292 if (p1[0] == '\n' && p1[1] == prefix)
1293 ++p1;
1294 ++p1;
1295 }
1296 *p2 = *p1;
1297 }
1298
1299 /* Figure out an argument list from this command line. */
1300 {
1301 char *end = 0;
1302#ifdef VMS
1303 /* Skip any leading whitespace */
1304 while (*p)
1305 {
1306 if (!ISSPACE (*p))
1307 {
1308 if (*p != '\\')
1309 break;
1310 if ((p[1] != '\n') && (p[1] != 'n') && (p[1] != 't'))
1311 break;
1312 }
1313 p++;
1314 }
1315
1316 argv = p;
1317 /* Although construct_command_argv contains some code for VMS, it was/is
1318 not called/used. Please note, for VMS argv is a string (not an array
1319 of strings) which contains the complete command line, which for
1320 multi-line variables still includes the newlines. So detect newlines
1321 and set 'end' (which is used for child->command_ptr) instead of
1322 (re-)writing construct_command_argv */
1323 if (!one_shell)
1324 {
1325 char *s = p;
1326 int instring = 0;
1327 while (*s)
1328 {
1329 if (*s == '"')
1330 instring = !instring;
1331 else if (*s == '\\' && !instring && *(s+1) != 0)
1332 s++;
1333 else if (*s == '\n' && !instring)
1334 {
1335 end = s;
1336 break;
1337 }
1338 ++s;
1339 }
1340 }
1341#else
1342 argv = construct_command_argv (p, &end, child->file,
1343 child->file->cmds->lines_flags[child->command_line - 1],
1344 &child->sh_batch_file);
1345#endif
1346 if (end == NULL)
1347 child->command_ptr = NULL;
1348 else
1349 {
1350 *end++ = '\0';
1351 child->command_ptr = end;
1352 }
1353 }
1354
1355 /* If -q was given, say that updating 'failed' if there was any text on the
1356 command line, or 'succeeded' otherwise. The exit status of 1 tells the
1357 user that -q is saying 'something to do'; the exit status for a random
1358 error is 2. */
1359 if (argv != 0 && question_flag && !(flags & COMMANDS_RECURSE))
1360 {
1361#ifndef VMS
1362 free (argv[0]);
1363 free (argv);
1364#endif
1365#ifdef VMS
1366 /* On VMS, argv[0] can be a null string here */
1367 if (argv[0] != 0)
1368 {
1369#endif
1370 child->file->update_status = us_question;
1371 notice_finished_file (child->file);
1372 return;
1373#ifdef VMS
1374 }
1375#endif
1376 }
1377
1378 if (touch_flag && !(flags & COMMANDS_RECURSE))
1379 {
1380 /* Go on to the next command. It might be the recursive one.
1381 We construct ARGV only to find the end of the command line. */
1382#ifndef VMS
1383 if (argv)
1384 {
1385 free (argv[0]);
1386 free (argv);
1387 }
1388#endif
1389 argv = 0;
1390 }
1391
1392 if (argv == 0)
1393 {
1394 next_command:
1395#ifdef __MSDOS__
1396 execute_by_shell = 0; /* in case construct_command_argv sets it */
1397#endif
1398 /* This line has no commands. Go to the next. */
1399 if (job_next_command (child))
1400 start_job_command (child);
1401 else
1402 {
1403 /* No more commands. Make sure we're "running"; we might not be if
1404 (e.g.) all commands were skipped due to -n. */
1405 set_command_state (child->file, cs_running);
1406 child->file->update_status = us_success;
1407 notice_finished_file (child->file);
1408 }
1409
1410 OUTPUT_UNSET();
1411 return;
1412 }
1413
1414 /* Are we going to synchronize this command's output? Do so if either we're
1415 in SYNC_RECURSE mode or this command is not recursive. We'll also check
1416 output_sync separately below in case it changes due to error. */
1417 child->output.syncout = output_sync && (output_sync == OUTPUT_SYNC_RECURSE
1418 || !(flags & COMMANDS_RECURSE));
1419 OUTPUT_SET (&child->output);
1420
1421#ifndef NO_OUTPUT_SYNC
1422 if (! child->output.syncout)
1423 /* We don't want to sync this command: to avoid misordered
1424 output ensure any already-synced content is written. */
1425 output_dump (&child->output);
1426#endif
1427
1428 /* Print the command if appropriate. */
1429#ifdef CONFIG_PRETTY_COMMAND_PRINTING
1430 if ( pretty_command_printing
1431 && (just_print_flag || (!(flags & COMMANDS_SILENT) && !silent_flag))
1432 && argv[0][0] != '\0')
1433 {
1434 unsigned i;
1435 for (i = 0; argv[i]; i++)
1436 OSSS ( message, 0, "%s'%s'%s", i ? "\t" : "> ", argv[i], argv[i + 1] ? " \\" : "");
1437 }
1438 else
1439#endif /* CONFIG_PRETTY_COMMAND_PRINTING */
1440 if (just_print_flag || trace_flag
1441 || (!(flags & COMMANDS_SILENT) && !silent_flag))
1442 OS (message, 0, "%s", p);
1443
1444 /* Tell update_goal_chain that a command has been started on behalf of
1445 this target. It is important that this happens here and not in
1446 reap_children (where we used to do it), because reap_children might be
1447 reaping children from a different target. We want this increment to
1448 guaranteedly indicate that a command was started for the dependency
1449 chain (i.e., update_file recursion chain) we are processing. */
1450
1451 ++commands_started;
1452
1453 /* Optimize an empty command. People use this for timestamp rules,
1454 so avoid forking a useless shell. Do this after we increment
1455 commands_started so make still treats this special case as if it
1456 performed some action (makes a difference as to what messages are
1457 printed, etc. */
1458
1459#if !defined(VMS) && !defined(_AMIGA)
1460 if (
1461#if defined __MSDOS__ || defined (__EMX__)
1462 unixy_shell /* the test is complicated and we already did it */
1463#else
1464 (argv[0] && is_bourne_compatible_shell (argv[0]))
1465#endif
1466 && (argv[1] && argv[1][0] == '-'
1467 &&
1468 ((argv[1][1] == 'c' && argv[1][2] == '\0')
1469 ||
1470 (argv[1][1] == 'e' && argv[1][2] == 'c' && argv[1][3] == '\0')))
1471 && (argv[2] && argv[2][0] == ':' && argv[2][1] == '\0')
1472 && argv[3] == NULL)
1473 {
1474 free (argv[0]);
1475 free (argv);
1476 goto next_command;
1477 }
1478#endif /* !VMS && !_AMIGA */
1479
1480 /* If -n was given, recurse to get the next line in the sequence. */
1481
1482 if (just_print_flag && !(flags & COMMANDS_RECURSE))
1483 {
1484#ifndef VMS
1485 free (argv[0]);
1486 free (argv);
1487#endif
1488 goto next_command;
1489 }
1490
1491 /* We're sure we're going to invoke a command: set up the output. */
1492 output_start ();
1493
1494 /* Flush the output streams so they won't have things written twice. */
1495
1496 fflush (stdout);
1497 fflush (stderr);
1498
1499#ifdef CONFIG_WITH_KMK_BUILTIN
1500 /* If builtin command then pass it on to the builtin shell interpreter. */
1501
1502 if ((flags & COMMANDS_KMK_BUILTIN) && !just_print_flag)
1503 {
1504 int rc;
1505 char **argv_spawn = NULL;
1506 char **p2 = argv;
1507 while (*p2 && strncmp (*p2, "kmk_builtin_", sizeof("kmk_builtin_") - 1))
1508 p2++;
1509 assert (*p2);
1510 set_command_state (child->file, cs_running);
1511 child->deleted = 0;
1512 child->pid = 0;
1513 if (p2 != argv)
1514 rc = kmk_builtin_command (*p2, child, &argv_spawn, &child->pid);
1515 else
1516 {
1517 int argc = 1;
1518 while (argv[argc])
1519 argc++;
1520 rc = kmk_builtin_command_parsed (argc, argv, child, &argv_spawn, &child->pid);
1521 }
1522
1523# ifndef VMS
1524 free (argv[0]);
1525 free ((char *) argv);
1526# endif
1527
1528 if (!rc)
1529 {
1530 /* spawned a child? */
1531 if (child->pid)
1532 {
1533 ++job_counter;
1534 return;
1535 }
1536
1537 /* synchronous command execution? */
1538 if (!argv_spawn)
1539 goto next_command;
1540 }
1541
1542 /* failure? */
1543 if (rc)
1544 {
1545 child->pid = (pid_t)42424242;
1546 child->status = rc << 8;
1547 child->has_status = 1;
1548 unblock_sigs();
1549 return;
1550 }
1551
1552 /* conditional check == true; kicking off a child (not kmk_builtin_*). */
1553 argv = argv_spawn;
1554 }
1555#endif /* CONFIG_WITH_KMK_BUILTIN */
1556
1557 /* Decide whether to give this child the 'good' standard input
1558 (one that points to the terminal or whatever), or the 'bad' one
1559 that points to the read side of a broken pipe. */
1560
1561 child->good_stdin = !good_stdin_used;
1562 if (child->good_stdin)
1563 good_stdin_used = 1;
1564
1565 child->deleted = 0;
1566
1567#ifndef _AMIGA
1568 /* Set up the environment for the child. */
1569 if (child->environment == 0)
1570 child->environment = target_environment (child->file);
1571#endif
1572
1573#if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
1574
1575#ifndef VMS
1576 /* start_waiting_job has set CHILD->remote if we can start a remote job. */
1577 if (child->remote)
1578 {
1579 int is_remote, id, used_stdin;
1580 if (start_remote_job (argv, child->environment,
1581 child->good_stdin ? 0 : get_bad_stdin (),
1582 &is_remote, &id, &used_stdin))
1583 /* Don't give up; remote execution may fail for various reasons. If
1584 so, simply run the job locally. */
1585 goto run_local;
1586 else
1587 {
1588 if (child->good_stdin && !used_stdin)
1589 {
1590 child->good_stdin = 0;
1591 good_stdin_used = 0;
1592 }
1593 child->remote = is_remote;
1594 child->pid = id;
1595 }
1596 }
1597 else
1598#endif /* !VMS */
1599 {
1600 /* Fork the child process. */
1601
1602 char **parent_environ;
1603
1604 run_local:
1605 block_sigs ();
1606
1607 child->remote = 0;
1608
1609#ifdef VMS
1610 if (!child_execute_job (child, argv))
1611 {
1612 /* Fork failed! */
1613 perror_with_name ("fork", "");
1614 goto error;
1615 }
1616
1617#else
1618
1619 parent_environ = environ;
1620
1621 jobserver_pre_child (flags & COMMANDS_RECURSE);
1622
1623 child->pid = child_execute_job (&child->output, child->good_stdin, argv, child->environment);
1624
1625 environ = parent_environ; /* Restore value child may have clobbered. */
1626 jobserver_post_child (flags & COMMANDS_RECURSE);
1627
1628 if (child->pid < 0)
1629 {
1630 /* Fork failed! */
1631 unblock_sigs ();
1632 perror_with_name ("fork", "");
1633 goto error;
1634 }
1635#endif /* !VMS */
1636 }
1637
1638#else /* __MSDOS__ or Amiga or WINDOWS32 */
1639#ifdef __MSDOS__
1640 {
1641 int proc_return;
1642
1643 block_sigs ();
1644 dos_status = 0;
1645
1646 /* We call 'system' to do the job of the SHELL, since stock DOS
1647 shell is too dumb. Our 'system' knows how to handle long
1648 command lines even if pipes/redirection is needed; it will only
1649 call COMMAND.COM when its internal commands are used. */
1650 if (execute_by_shell)
1651 {
1652 char *cmdline = argv[0];
1653 /* We don't have a way to pass environment to 'system',
1654 so we need to save and restore ours, sigh... */
1655 char **parent_environ = environ;
1656
1657 environ = child->environment;
1658
1659 /* If we have a *real* shell, tell 'system' to call
1660 it to do everything for us. */
1661 if (unixy_shell)
1662 {
1663 /* A *real* shell on MSDOS may not support long
1664 command lines the DJGPP way, so we must use 'system'. */
1665 cmdline = argv[2]; /* get past "shell -c" */
1666 }
1667
1668 dos_command_running = 1;
1669 proc_return = system (cmdline);
1670 environ = parent_environ;
1671 execute_by_shell = 0; /* for the next time */
1672 }
1673 else
1674 {
1675 dos_command_running = 1;
1676 proc_return = spawnvpe (P_WAIT, argv[0], argv, child->environment);
1677 }
1678
1679 /* Need to unblock signals before turning off
1680 dos_command_running, so that child's signals
1681 will be treated as such (see fatal_error_signal). */
1682 unblock_sigs ();
1683 dos_command_running = 0;
1684
1685 /* If the child got a signal, dos_status has its
1686 high 8 bits set, so be careful not to alter them. */
1687 if (proc_return == -1)
1688 dos_status |= 0xff;
1689 else
1690 dos_status |= (proc_return & 0xff);
1691 ++dead_children;
1692 child->pid = dos_pid++;
1693 }
1694#endif /* __MSDOS__ */
1695#ifdef _AMIGA
1696 amiga_status = MyExecute (argv);
1697
1698 ++dead_children;
1699 child->pid = amiga_pid++;
1700 if (amiga_batch_file)
1701 {
1702 amiga_batch_file = 0;
1703 DeleteFile (amiga_bname); /* Ignore errors. */
1704 }
1705#endif /* Amiga */
1706#ifdef WINDOWS32
1707 {
1708# ifndef CONFIG_NEW_WIN_CHILDREN
1709 HANDLE hPID;
1710 char* arg0;
1711 int outfd = FD_STDOUT;
1712 int errfd = FD_STDERR;
1713
1714 /* make UNC paths safe for CreateProcess -- backslash format */
1715 arg0 = argv[0];
1716 if (arg0 && arg0[0] == '/' && arg0[1] == '/')
1717 for ( ; arg0 && *arg0; arg0++)
1718 if (*arg0 == '/')
1719 *arg0 = '\\';
1720
1721 /* make sure CreateProcess() has Path it needs */
1722 sync_Path_environment ();
1723
1724#ifndef NO_OUTPUT_SYNC
1725 /* Divert child output if output_sync in use. */
1726 if (child->output.syncout)
1727 {
1728 if (child->output.out >= 0)
1729 outfd = child->output.out;
1730 if (child->output.err >= 0)
1731 errfd = child->output.err;
1732 }
1733#else
1734 outfd = errfd = -1;
1735#endif
1736 hPID = process_easy (argv, child->environment, outfd, errfd);
1737
1738 if (hPID != INVALID_HANDLE_VALUE)
1739 child->pid = (pid_t) hPID;
1740 else
1741 {
1742 int i;
1743 unblock_sigs ();
1744 fprintf (stderr,
1745 _("process_easy() failed to launch process (e=%ld)\n"),
1746 process_last_err (hPID));
1747 for (i = 0; argv[i]; i++)
1748 fprintf (stderr, "%s ", argv[i]);
1749 fprintf (stderr, _("\nCounted %d args in failed launch\n"), i);
1750 goto error;
1751 }
1752# else /* CONFIG_NEW_WIN_CHILDREN */
1753 struct variable *shell_var = lookup_variable("SHELL", 5);
1754 const char *shell_value = !shell_var ? NULL
1755 : !shell_var->recursive || strchr(shell_var->value, '$') == NULL
1756 ? shell_var->value : variable_expand (shell_var->value);
1757 int rc = MkWinChildCreate(argv, child->environment, shell_value, child, &child->pid);
1758 if (rc != 0)
1759 {
1760 int i;
1761 unblock_sigs ();
1762 fprintf (stderr, _("failed to launch process (rc=%d)\n"), rc);
1763 for (i = 0; argv[i]; i++)
1764 fprintf (stderr, "%s ", argv[i]);
1765 fprintf (stderr, "\n", argv[i]);
1766 goto error;
1767 }
1768# endif /* CONFIG_NEW_WIN_CHILDREN */
1769 }
1770#endif /* WINDOWS32 */
1771#endif /* __MSDOS__ or Amiga or WINDOWS32 */
1772
1773 /* Bump the number of jobs started in this second. */
1774 ++job_counter;
1775
1776 /* We are the parent side. Set the state to
1777 say the commands are running and return. */
1778
1779 set_command_state (child->file, cs_running);
1780
1781 /* Free the storage used by the child's argument list. */
1782#ifdef KMK /* leak */
1783 cleanup_argv:
1784#endif
1785#ifndef VMS
1786 free (argv[0]);
1787 free (argv);
1788#endif
1789
1790 OUTPUT_UNSET();
1791 return;
1792
1793 error:
1794 child->file->update_status = us_failed;
1795 notice_finished_file (child->file);
1796#ifdef KMK /* fix leak */
1797 goto cleanup_argv;
1798#else
1799 OUTPUT_UNSET();
1800#endif
1801}
1802
1803/* Try to start a child running.
1804 Returns nonzero if the child was started (and maybe finished), or zero if
1805 the load was too high and the child was put on the 'waiting_jobs' chain. */
1806
1807static int
1808start_waiting_job (struct child *c)
1809{
1810 struct file *f = c->file;
1811#ifdef DB_KMK
1812 DB (DB_KMK, (_("start_waiting_job %p (`%s') command_flags=%#x slots=%d/%d\n"),
1813 (void *)c, c->file->name, c->file->command_flags, job_slots_used, job_slots));
1814#endif
1815
1816 /* If we can start a job remotely, we always want to, and don't care about
1817 the local load average. We record that the job should be started
1818 remotely in C->remote for start_job_command to test. */
1819
1820 c->remote = start_remote_job_p (1);
1821
1822#ifdef CONFIG_WITH_EXTENDED_NOTPARALLEL
1823 if (c->file->command_flags & COMMANDS_NOTPARALLEL)
1824 {
1825 DB (DB_KMK, (_("not_parallel %d -> %d (file=%p `%s') [start_waiting_job]\n"),
1826 not_parallel, not_parallel + 1, (void *)c->file, c->file->name));
1827 assert(not_parallel >= 0);
1828 ++not_parallel;
1829 }
1830#endif /* CONFIG_WITH_EXTENDED_NOTPARALLEL */
1831
1832 /* If we are running at least one job already and the load average
1833 is too high, make this one wait. */
1834 if (!c->remote
1835#ifdef CONFIG_WITH_EXTENDED_NOTPARALLEL
1836 && ((job_slots_used > 0 && (not_parallel > 0 || load_too_high ()))
1837#else
1838 && ((job_slots_used > 0 && load_too_high ())
1839#endif
1840#ifdef WINDOWS32
1841# ifndef CONFIG_NEW_WIN_CHILDREN
1842 || (process_used_slots () >= MAXIMUM_WAIT_OBJECTS)
1843# endif
1844#endif
1845 ))
1846 {
1847#ifndef CONFIG_WITH_EXTENDED_NOTPARALLEL
1848 /* Put this child on the chain of children waiting for the load average
1849 to go down. */
1850 set_command_state (f, cs_running);
1851 c->next = waiting_jobs;
1852 waiting_jobs = c;
1853
1854#else /* CONFIG_WITH_EXTENDED_NOTPARALLEL */
1855
1856 /* Put this child on the chain of children waiting for the load average
1857 to go down. If not parallel, put it last. */
1858 set_command_state (f, cs_running);
1859 c->next = waiting_jobs;
1860 if (c->next && (c->file->command_flags & COMMANDS_NOTPARALLEL))
1861 {
1862 struct child *prev = waiting_jobs;
1863 while (prev->next)
1864 prev = prev->next;
1865 c->next = 0;
1866 prev->next = c;
1867 }
1868 else /* FIXME: insert after the last node with COMMANDS_NOTPARALLEL set */
1869 waiting_jobs = c;
1870 DB (DB_KMK, (_("queued child %p (`%s')\n"), (void *)c, c->file->name));
1871#endif /* CONFIG_WITH_EXTENDED_NOTPARALLEL */
1872 return 0;
1873 }
1874
1875 /* Start the first command; reap_children will run later command lines. */
1876 start_job_command (c);
1877
1878 switch (f->command_state)
1879 {
1880 case cs_running:
1881 c->next = children;
1882 DB (DB_JOBS, (_("Putting child %p (%s) PID %s%s on the chain.\n"),
1883 (void *)c, c->file->name, pid2str (c->pid),
1884 c->remote ? _(" (remote)") : ""));
1885 children = c;
1886 /* One more job slot is in use. */
1887 ++job_slots_used;
1888 unblock_sigs ();
1889 break;
1890
1891 case cs_not_started:
1892 /* All the command lines turned out to be empty. */
1893 f->update_status = us_success;
1894 /* FALLTHROUGH */
1895
1896 case cs_finished:
1897 notice_finished_file (f);
1898 free_child (c);
1899 break;
1900
1901 default:
1902 assert (f->command_state == cs_finished);
1903 break;
1904 }
1905
1906 return 1;
1907}
1908
1909/* Create a 'struct child' for FILE and start its commands running. */
1910
1911void
1912new_job (struct file *file)
1913{
1914 struct commands *cmds = file->cmds;
1915 struct child *c;
1916 char **lines;
1917 unsigned int i;
1918
1919 /* Let any previously decided-upon jobs that are waiting
1920 for the load to go down start before this new one. */
1921 start_waiting_jobs ();
1922
1923 /* Reap any children that might have finished recently. */
1924 reap_children (0, 0);
1925
1926 /* Chop the commands up into lines if they aren't already. */
1927 chop_commands (cmds);
1928#ifdef CONFIG_WITH_MEMORY_OPTIMIZATIONS
1929 cmds->refs++; /* retain the chopped lines. */
1930#endif
1931
1932 /* Start the command sequence, record it in a new
1933 'struct child', and add that to the chain. */
1934
1935 c = xcalloc (sizeof (struct child));
1936 output_init (&c->output);
1937
1938 c->file = file;
1939 c->sh_batch_file = NULL;
1940
1941 /* Cache dontcare flag because file->dontcare can be changed once we
1942 return. Check dontcare inheritance mechanism for details. */
1943 c->dontcare = file->dontcare;
1944
1945 /* Start saving output in case the expansion uses $(info ...) etc. */
1946 OUTPUT_SET (&c->output);
1947
1948 /* Expand the command lines and store the results in LINES. */
1949 lines = xmalloc (cmds->ncommand_lines * sizeof (char *));
1950 for (i = 0; i < cmds->ncommand_lines; ++i)
1951 {
1952 /* Collapse backslash-newline combinations that are inside variable
1953 or function references. These are left alone by the parser so
1954 that they will appear in the echoing of commands (where they look
1955 nice); and collapsed by construct_command_argv when it tokenizes.
1956 But letting them survive inside function invocations loses because
1957 we don't want the functions to see them as part of the text. */
1958
1959 char *in, *out, *ref;
1960
1961 /* IN points to where in the line we are scanning.
1962 OUT points to where in the line we are writing.
1963 When we collapse a backslash-newline combination,
1964 IN gets ahead of OUT. */
1965
1966 in = out = cmds->command_lines[i];
1967 while ((ref = strchr (in, '$')) != 0)
1968 {
1969 ++ref; /* Move past the $. */
1970
1971 if (out != in)
1972 /* Copy the text between the end of the last chunk
1973 we processed (where IN points) and the new chunk
1974 we are about to process (where REF points). */
1975 memmove (out, in, ref - in);
1976
1977 /* Move both pointers past the boring stuff. */
1978 out += ref - in;
1979 in = ref;
1980
1981 if (*ref == '(' || *ref == '{')
1982 {
1983 char openparen = *ref;
1984 char closeparen = openparen == '(' ? ')' : '}';
1985 char *outref;
1986 int count;
1987 char *p;
1988
1989 *out++ = *in++; /* Copy OPENPAREN. */
1990 outref = out;
1991 /* IN now points past the opening paren or brace.
1992 Count parens or braces until it is matched. */
1993 count = 0;
1994 while (*in != '\0')
1995 {
1996 if (*in == closeparen && --count < 0)
1997 break;
1998 else if (*in == '\\' && in[1] == '\n')
1999 {
2000 /* We have found a backslash-newline inside a
2001 variable or function reference. Eat it and
2002 any following whitespace. */
2003
2004 int quoted = 0;
2005 for (p = in - 1; p > ref && *p == '\\'; --p)
2006 quoted = !quoted;
2007
2008 if (quoted)
2009 /* There were two or more backslashes, so this is
2010 not really a continuation line. We don't collapse
2011 the quoting backslashes here as is done in
2012 collapse_continuations, because the line will
2013 be collapsed again after expansion. */
2014 *out++ = *in++;
2015 else
2016 {
2017 /* Skip the backslash, newline, and whitespace. */
2018 in += 2;
2019 NEXT_TOKEN (in);
2020
2021 /* Discard any preceding whitespace that has
2022 already been written to the output. */
2023 while (out > outref && ISBLANK (out[-1]))
2024 --out;
2025
2026 /* Replace it all with a single space. */
2027 *out++ = ' ';
2028 }
2029 }
2030 else
2031 {
2032 if (*in == openparen)
2033 ++count;
2034
2035 *out++ = *in++;
2036 }
2037 }
2038 }
2039 }
2040
2041 /* There are no more references in this line to worry about.
2042 Copy the remaining uninteresting text to the output. */
2043 if (out != in)
2044 memmove (out, in, strlen (in) + 1);
2045
2046 /* Finally, expand the line. */
2047 cmds->fileinfo.offset = i;
2048 lines[i] = allocated_variable_expand_for_file (cmds->command_lines[i],
2049 file);
2050 }
2051
2052 cmds->fileinfo.offset = 0;
2053 c->command_lines = lines;
2054#ifdef CONFIG_WITH_PRINT_TIME_SWITCH
2055 c->start_ts = -1;
2056#endif
2057
2058 /* Fetch the first command line to be run. */
2059 job_next_command (c);
2060
2061 /* Wait for a job slot to be freed up. If we allow an infinite number
2062 don't bother; also job_slots will == 0 if we're using the jobserver. */
2063
2064 if (job_slots != 0)
2065 while (job_slots_used == job_slots)
2066 reap_children (1, 0);
2067
2068#ifdef MAKE_JOBSERVER
2069 /* If we are controlling multiple jobs make sure we have a token before
2070 starting the child. */
2071
2072 /* This can be inefficient. There's a decent chance that this job won't
2073 actually have to run any subprocesses: the command script may be empty
2074 or otherwise optimized away. It would be nice if we could defer
2075 obtaining a token until just before we need it, in start_job_command.
2076 To do that we'd need to keep track of whether we'd already obtained a
2077 token (since start_job_command is called for each line of the job, not
2078 just once). Also more thought needs to go into the entire algorithm;
2079 this is where the old parallel job code waits, so... */
2080
2081 else if (jobserver_enabled ())
2082 while (1)
2083 {
2084 int got_token;
2085
2086 DB (DB_JOBS, ("Need a job token; we %shave children\n",
2087 children ? "" : "don't "));
2088
2089 /* If we don't already have a job started, use our "free" token. */
2090 if (!jobserver_tokens)
2091 break;
2092
2093 /* Prepare for jobserver token acquisition. */
2094 jobserver_pre_acquire ();
2095
2096 /* Reap anything that's currently waiting. */
2097 reap_children (0, 0);
2098
2099 /* Kick off any jobs we have waiting for an opportunity that
2100 can run now (i.e., waiting for load). */
2101 start_waiting_jobs ();
2102
2103 /* If our "free" slot is available, use it; we don't need a token. */
2104 if (!jobserver_tokens)
2105 break;
2106
2107 /* There must be at least one child already, or we have no business
2108 waiting for a token. */
2109 if (!children)
2110 O (fatal, NILF, "INTERNAL: no children as we go to sleep on read\n");
2111
2112 /* Get a token. */
2113 got_token = jobserver_acquire (waiting_jobs != NULL);
2114
2115 /* If we got one, we're done here. */
2116 if (got_token == 1)
2117 {
2118 DB (DB_JOBS, (_("Obtained token for child %p (%s).\n"),
2119 (void *)c, c->file->name));
2120 break;
2121 }
2122 }
2123#endif
2124
2125 ++jobserver_tokens;
2126
2127 /* Trace the build.
2128 Use message here so that changes to working directories are logged. */
2129 if (trace_flag)
2130 {
2131 char *newer = allocated_variable_expand_for_file ("$?", c->file);
2132 const char *nm;
2133
2134 if (! cmds->fileinfo.filenm)
2135 nm = _("<builtin>");
2136 else
2137 {
2138 char *n = alloca (strlen (cmds->fileinfo.filenm) + 1 + 11 + 1);
2139 sprintf (n, "%s:%lu", cmds->fileinfo.filenm, cmds->fileinfo.lineno);
2140 nm = n;
2141 }
2142
2143 if (newer[0] == '\0')
2144 OSS (message, 0,
2145 _("%s: target '%s' does not exist"), nm, c->file->name);
2146 else
2147 OSSS (message, 0,
2148 _("%s: update target '%s' due to: %s"), nm, c->file->name, newer);
2149
2150 free (newer);
2151 }
2152
2153 /* The job is now primed. Start it running.
2154 (This will notice if there is in fact no recipe.) */
2155 start_waiting_job (c);
2156
2157#ifndef CONFIG_WITH_EXTENDED_NOTPARALLEL
2158 if (job_slots == 1 || not_parallel)
2159 /* Since there is only one job slot, make things run linearly.
2160 Wait for the child to die, setting the state to 'cs_finished'. */
2161 while (file->command_state == cs_running)
2162 reap_children (1, 0);
2163
2164#else /* CONFIG_WITH_EXTENDED_NOTPARALLEL */
2165
2166 if (job_slots == 1 || not_parallel < 0)
2167 {
2168 /* Since there is only one job slot, make things run linearly.
2169 Wait for the child to die, setting the state to `cs_finished'. */
2170 while (file->command_state == cs_running)
2171 reap_children (1, 0);
2172 }
2173 else if (not_parallel > 0)
2174 {
2175 /* wait for all live children to finish and then continue
2176 with the not-parallel child(s). FIXME: this loop could be better? */
2177 while (file->command_state == cs_running
2178 && (children != 0 || shell_function_pid != 0) /* reap_child condition */
2179 && not_parallel > 0)
2180 reap_children (1, 0);
2181 }
2182#endif /* CONFIG_WITH_EXTENDED_NOTPARALLEL */
2183
2184 OUTPUT_UNSET ();
2185 return;
2186}
2187
2188
2189/* Move CHILD's pointers to the next command for it to execute.
2190 Returns nonzero if there is another command. */
2191
2192static int
2193job_next_command (struct child *child)
2194{
2195 while (child->command_ptr == 0 || *child->command_ptr == '\0')
2196 {
2197 /* There are no more lines in the expansion of this line. */
2198 if (child->command_line == child->file->cmds->ncommand_lines)
2199 {
2200 /* There are no more lines to be expanded. */
2201 child->command_ptr = 0;
2202 child->file->cmds->fileinfo.offset = 0;
2203 return 0;
2204 }
2205 else
2206 /* Get the next line to run. */
2207 child->command_ptr = child->command_lines[child->command_line++];
2208 }
2209
2210 child->file->cmds->fileinfo.offset = child->command_line - 1;
2211 return 1;
2212}
2213
2214/* Determine if the load average on the system is too high to start a new job.
2215 The real system load average is only recomputed once a second. However, a
2216 very parallel make can easily start tens or even hundreds of jobs in a
2217 second, which brings the system to its knees for a while until that first
2218 batch of jobs clears out.
2219
2220 To avoid this we use a weighted algorithm to try to account for jobs which
2221 have been started since the last second, and guess what the load average
2222 would be now if it were computed.
2223
2224 This algorithm was provided by Thomas Riedl <thomas.riedl@siemens.com>,
2225 who writes:
2226
2227! calculate something load-oid and add to the observed sys.load,
2228! so that latter can catch up:
2229! - every job started increases jobctr;
2230! - every dying job decreases a positive jobctr;
2231! - the jobctr value gets zeroed every change of seconds,
2232! after its value*weight_b is stored into the 'backlog' value last_sec
2233! - weight_a times the sum of jobctr and last_sec gets
2234! added to the observed sys.load.
2235!
2236! The two weights have been tried out on 24 and 48 proc. Sun Solaris-9
2237! machines, using a several-thousand-jobs-mix of cpp, cc, cxx and smallish
2238! sub-shelled commands (rm, echo, sed...) for tests.
2239! lowering the 'direct influence' factor weight_a (e.g. to 0.1)
2240! resulted in significant excession of the load limit, raising it
2241! (e.g. to 0.5) took bad to small, fast-executing jobs and didn't
2242! reach the limit in most test cases.
2243!
2244! lowering the 'history influence' weight_b (e.g. to 0.1) resulted in
2245! exceeding the limit for longer-running stuff (compile jobs in
2246! the .5 to 1.5 sec. range),raising it (e.g. to 0.5) overrepresented
2247! small jobs' effects.
2248
2249 */
2250
2251#define LOAD_WEIGHT_A 0.25
2252#define LOAD_WEIGHT_B 0.25
2253
2254static int
2255load_too_high (void)
2256{
2257#if defined(__MSDOS__) || defined(VMS) || defined(_AMIGA) || defined(__riscos__) || defined(__HAIKU__)
2258 return 1;
2259#else
2260 static double last_sec;
2261 static time_t last_now;
2262 double load, guess;
2263 time_t now;
2264
2265#if defined(WINDOWS32) && !defined(CONFIG_NEW_WIN_CHILDREN)
2266 /* sub_proc.c cannot wait for more than MAXIMUM_WAIT_OBJECTS children */
2267 if (process_used_slots () >= MAXIMUM_WAIT_OBJECTS)
2268 return 1;
2269#endif
2270
2271 if (max_load_average < 0)
2272 return 0;
2273
2274 /* Find the real system load average. */
2275 make_access ();
2276 if (getloadavg (&load, 1) != 1)
2277 {
2278 static int lossage = -1;
2279 /* Complain only once for the same error. */
2280 if (lossage == -1 || errno != lossage)
2281 {
2282 if (errno == 0)
2283 /* An errno value of zero means getloadavg is just unsupported. */
2284 O (error, NILF,
2285 _("cannot enforce load limits on this operating system"));
2286 else
2287 perror_with_name (_("cannot enforce load limit: "), "getloadavg");
2288 }
2289 lossage = errno;
2290 load = 0;
2291 }
2292 user_access ();
2293
2294 /* If we're in a new second zero the counter and correct the backlog
2295 value. Only keep the backlog for one extra second; after that it's 0. */
2296 now = time (NULL);
2297 if (last_now < now)
2298 {
2299 if (last_now == now - 1)
2300 last_sec = LOAD_WEIGHT_B * job_counter;
2301 else
2302 last_sec = 0.0;
2303
2304 job_counter = 0;
2305 last_now = now;
2306 }
2307
2308 /* Try to guess what the load would be right now. */
2309 guess = load + (LOAD_WEIGHT_A * (job_counter + last_sec));
2310
2311 DB (DB_JOBS, ("Estimated system load = %f (actual = %f) (max requested = %f)\n",
2312 guess, load, max_load_average));
2313
2314 return guess >= max_load_average;
2315#endif
2316}
2317
2318/* Start jobs that are waiting for the load to be lower. */
2319
2320void
2321start_waiting_jobs (void)
2322{
2323 struct child *job;
2324
2325 if (waiting_jobs == 0)
2326 return;
2327
2328 do
2329 {
2330 /* Check for recently deceased descendants. */
2331 reap_children (0, 0);
2332
2333 /* Take a job off the waiting list. */
2334 job = waiting_jobs;
2335 waiting_jobs = job->next;
2336
2337#ifdef CONFIG_WITH_EXTENDED_NOTPARALLEL
2338 /* If it's a not-parallel job, we've already counted it once
2339 when it was queued in start_waiting_job, so decrement
2340 before sending it to start_waiting_job again. */
2341 if (job->file->command_flags & COMMANDS_NOTPARALLEL)
2342 {
2343 DB (DB_KMK, (_("not_parallel %d -> %d (file=%p `%s') [start_waiting_jobs]\n"),
2344 not_parallel, not_parallel - 1, (void *) job->file, job->file->name));
2345 assert(not_parallel > 0);
2346 --not_parallel;
2347 }
2348#endif /* CONFIG_WITH_EXTENDED_NOTPARALLEL */
2349
2350 /* Try to start that job. We break out of the loop as soon
2351 as start_waiting_job puts one back on the waiting list. */
2352 }
2353 while (start_waiting_job (job) && waiting_jobs != 0);
2354
2355 return;
2356}
2357
2358
2359#ifndef WINDOWS32
2360
2361/* EMX: Start a child process. This function returns the new pid. */
2362# if defined __EMX__
2363int
2364child_execute_job (struct output *out, int good_stdin, char **argv, char **envp)
2365{
2366 int pid;
2367 int fdin = good_stdin ? FD_STDIN : get_bad_stdin ();
2368 int fdout = FD_STDOUT;
2369 int fderr = FD_STDERR;
2370 int save_fdin = -1;
2371 int save_fdout = -1;
2372 int save_fderr = -1;
2373
2374 /* Divert child output if we want to capture output. */
2375 if (out && out->syncout)
2376 {
2377 if (out->out >= 0)
2378 fdout = out->out;
2379 if (out->err >= 0)
2380 fderr = out->err;
2381 }
2382
2383 /* For each FD which needs to be redirected first make a dup of the standard
2384 FD to save and mark it close on exec so our child won't see it. Then
2385 dup2() the standard FD to the redirect FD, and also mark the redirect FD
2386 as close on exec. */
2387 if (fdin != FD_STDIN)
2388 {
2389 save_fdin = dup (FD_STDIN);
2390 if (save_fdin < 0)
2391 O (fatal, NILF, _("no more file handles: could not duplicate stdin\n"));
2392 CLOSE_ON_EXEC (save_fdin);
2393
2394 dup2 (fdin, FD_STDIN);
2395 CLOSE_ON_EXEC (fdin);
2396 }
2397
2398 if (fdout != FD_STDOUT)
2399 {
2400 save_fdout = dup (FD_STDOUT);
2401 if (save_fdout < 0)
2402 O (fatal, NILF,
2403 _("no more file handles: could not duplicate stdout\n"));
2404 CLOSE_ON_EXEC (save_fdout);
2405
2406 dup2 (fdout, FD_STDOUT);
2407 CLOSE_ON_EXEC (fdout);
2408 }
2409
2410 if (fderr != FD_STDERR)
2411 {
2412 if (fderr != fdout)
2413 {
2414 save_fderr = dup (FD_STDERR);
2415 if (save_fderr < 0)
2416 O (fatal, NILF,
2417 _("no more file handles: could not duplicate stderr\n"));
2418 CLOSE_ON_EXEC (save_fderr);
2419 }
2420
2421 dup2 (fderr, FD_STDERR);
2422 CLOSE_ON_EXEC (fderr);
2423 }
2424
2425 /* Run the command. */
2426 pid = exec_command (argv, envp);
2427
2428 /* Restore stdout/stdin/stderr of the parent and close temporary FDs. */
2429 if (save_fdin >= 0)
2430 {
2431 if (dup2 (save_fdin, FD_STDIN) != FD_STDIN)
2432 O (fatal, NILF, _("Could not restore stdin\n"));
2433 else
2434 close (save_fdin);
2435 }
2436
2437 if (save_fdout >= 0)
2438 {
2439 if (dup2 (save_fdout, FD_STDOUT) != FD_STDOUT)
2440 O (fatal, NILF, _("Could not restore stdout\n"));
2441 else
2442 close (save_fdout);
2443 }
2444
2445 if (save_fderr >= 0)
2446 {
2447 if (dup2 (save_fderr, FD_STDERR) != FD_STDERR)
2448 O (fatal, NILF, _("Could not restore stderr\n"));
2449 else
2450 close (save_fderr);
2451 }
2452
2453 return pid;
2454}
2455
2456#elif !defined (_AMIGA) && !defined (__MSDOS__) && !defined (VMS)
2457
2458/* POSIX:
2459 Create a child process executing the command in ARGV.
2460 ENVP is the environment of the new program. Returns the PID or -1. */
2461int
2462child_execute_job (struct output *out, int good_stdin, char **argv, char **envp)
2463{
2464 int r;
2465 int pid;
2466 int fdin = good_stdin ? FD_STDIN : get_bad_stdin ();
2467 int fdout = FD_STDOUT;
2468 int fderr = FD_STDERR;
2469
2470 /* Divert child output if we want to capture it. */
2471 if (out && out->syncout)
2472 {
2473 if (out->out >= 0)
2474 fdout = out->out;
2475 if (out->err >= 0)
2476 fderr = out->err;
2477 }
2478
2479 pid = vfork();
2480 if (pid != 0)
2481 return pid;
2482
2483 /* We are the child. */
2484 unblock_sigs ();
2485
2486#ifdef SET_STACK_SIZE
2487 /* Reset limits, if necessary. */
2488 if (stack_limit.rlim_cur)
2489 setrlimit (RLIMIT_STACK, &stack_limit);
2490#endif
2491
2492 /* For any redirected FD, dup2() it to the standard FD.
2493 They are all marked close-on-exec already. */
2494 if (fdin != FD_STDIN)
2495 EINTRLOOP (r, dup2 (fdin, FD_STDIN));
2496 if (fdout != FD_STDOUT)
2497 EINTRLOOP (r, dup2 (fdout, FD_STDOUT));
2498 if (fderr != FD_STDERR)
2499 EINTRLOOP (r, dup2 (fderr, FD_STDERR));
2500
2501 /* Run the command. */
2502 exec_command (argv, envp);
2503}
2504#endif /* !AMIGA && !__MSDOS__ && !VMS */
2505#endif /* !WINDOWS32 */
2506
2507
2508#if !defined(WINDOWS32) || !defined(CONFIG_NEW_WIN_CHILDREN)
2509#ifndef _AMIGA
2510/* Replace the current process with one running the command in ARGV,
2511 with environment ENVP. This function does not return. */
2512
2513/* EMX: This function returns the pid of the child process. */
2514# ifdef __EMX__
2515int
2516# else
2517void
2518# endif
2519exec_command (char **argv, char **envp)
2520{
2521#ifdef VMS
2522 /* to work around a problem with signals and execve: ignore them */
2523#ifdef SIGCHLD
2524 signal (SIGCHLD,SIG_IGN);
2525#endif
2526 /* Run the program. */
2527 execve (argv[0], argv, envp);
2528 perror_with_name ("execve: ", argv[0]);
2529 _exit (EXIT_FAILURE);
2530#else
2531#ifdef WINDOWS32
2532# ifndef CONFIG_NEW_WIN_CHILDREN
2533 HANDLE hPID;
2534 HANDLE hWaitPID;
2535 int exit_code = EXIT_FAILURE;
2536
2537 /* make sure CreateProcess() has Path it needs */
2538 sync_Path_environment ();
2539
2540 /* launch command */
2541 hPID = process_easy (argv, envp, -1, -1);
2542
2543 /* make sure launch ok */
2544 if (hPID == INVALID_HANDLE_VALUE)
2545 {
2546 int i;
2547 fprintf (stderr, _("process_easy() failed to launch process (e=%ld)\n"),
2548 process_last_err (hPID));
2549 for (i = 0; argv[i]; i++)
2550 fprintf (stderr, "%s ", argv[i]);
2551 fprintf (stderr, _("\nCounted %d args in failed launch\n"), i);
2552 exit (EXIT_FAILURE);
2553 }
2554
2555 /* wait and reap last child */
2556 hWaitPID = process_wait_for_any (1, 0);
2557 while (hWaitPID)
2558 {
2559 /* was an error found on this process? */
2560 int err = process_last_err (hWaitPID);
2561
2562 /* get exit data */
2563 exit_code = process_exit_code (hWaitPID);
2564
2565 if (err)
2566 fprintf (stderr, "make (e=%d, rc=%d): %s",
2567 err, exit_code, map_windows32_error_to_string (err));
2568
2569 /* cleanup process */
2570 process_cleanup (hWaitPID);
2571
2572 /* expect to find only last pid, warn about other pids reaped */
2573 if (hWaitPID == hPID)
2574 break;
2575 else
2576 {
2577 char *pidstr = xstrdup (pid2str ((pid_t)hWaitPID));
2578
2579 fprintf (stderr,
2580 _("make reaped child pid %s, still waiting for pid %s\n"),
2581 pidstr, pid2str ((pid_t)hPID));
2582 free (pidstr);
2583 }
2584 }
2585
2586 /* return child's exit code as our exit code */
2587 exit (exit_code);
2588# else /* CONFIG_NEW_WIN_CHILDREN */
2589
2590# endif /* CONFIG_NEW_WIN_CHILDREN */
2591#else /* !WINDOWS32 */
2592
2593# ifdef __EMX__
2594 int pid;
2595# endif
2596
2597 /* Be the user, permanently. */
2598 child_access ();
2599
2600# ifdef __EMX__
2601 /* Run the program. */
2602 pid = spawnvpe (P_NOWAIT, argv[0], argv, envp);
2603 if (pid >= 0)
2604 return pid;
2605
2606 /* the file might have a strange shell extension */
2607 if (errno == ENOENT)
2608 errno = ENOEXEC;
2609
2610# else
2611 /* Run the program. */
2612 environ = envp;
2613 execvp (argv[0], argv);
2614
2615# endif /* !__EMX__ */
2616
2617 switch (errno)
2618 {
2619 case ENOENT:
2620 /* We are in the child: don't use the output buffer.
2621 It's not right to run fprintf() here! */
2622 if (makelevel == 0)
2623 fprintf (stderr, _("%s: %s: Command not found\n"), program, argv[0]);
2624 else
2625 fprintf (stderr, _("%s[%u]: %s: Command not found\n"),
2626 program, makelevel, argv[0]);
2627 break;
2628 case ENOEXEC:
2629 {
2630 /* The file is not executable. Try it as a shell script. */
2631 const char *shell;
2632 char **new_argv;
2633 int argc;
2634 int i=1;
2635
2636# ifdef __EMX__
2637 /* Do not use $SHELL from the environment */
2638 struct variable *p = lookup_variable ("SHELL", 5);
2639 if (p)
2640 shell = p->value;
2641 else
2642 shell = 0;
2643# else
2644 shell = getenv ("SHELL");
2645# endif
2646 if (shell == 0)
2647 shell = default_shell;
2648
2649 argc = 1;
2650 while (argv[argc] != 0)
2651 ++argc;
2652
2653# ifdef __EMX__
2654 if (!unixy_shell)
2655 ++argc;
2656# endif
2657
2658 new_argv = alloca ((1 + argc + 1) * sizeof (char *));
2659 new_argv[0] = (char *)shell;
2660
2661# ifdef __EMX__
2662 if (!unixy_shell)
2663 {
2664 new_argv[1] = "/c";
2665 ++i;
2666 --argc;
2667 }
2668# endif
2669
2670 new_argv[i] = argv[0];
2671 while (argc > 0)
2672 {
2673 new_argv[i + argc] = argv[argc];
2674 --argc;
2675 }
2676
2677# ifdef __EMX__
2678 pid = spawnvpe (P_NOWAIT, shell, new_argv, envp);
2679 if (pid >= 0)
2680 break;
2681# else
2682 execvp (shell, new_argv);
2683# endif
2684 if (errno == ENOENT)
2685 OS (error, NILF, _("%s: Shell program not found"), shell);
2686 else
2687 perror_with_name ("execvp: ", shell);
2688 break;
2689 }
2690
2691# ifdef __EMX__
2692 case EINVAL:
2693 /* this nasty error was driving me nuts :-( */
2694 O (error, NILF, _("spawnvpe: environment space might be exhausted"));
2695 /* FALLTHROUGH */
2696# endif
2697
2698 default:
2699 perror_with_name ("execvp: ", argv[0]);
2700 break;
2701 }
2702
2703# ifdef __EMX__
2704 return pid;
2705# else
2706 _exit (127);
2707# endif
2708#endif /* !WINDOWS32 */
2709#endif /* !VMS */
2710}
2711#else /* On Amiga */
2712void
2713exec_command (char **argv)
2714{
2715 MyExecute (argv);
2716}
2717
2718void clean_tmp (void)
2719{
2720 DeleteFile (amiga_bname);
2721}
2722
2723#endif /* On Amiga */
2724#endif /* !defined(WINDOWS32) || !defined(CONFIG_NEW_WIN_CHILDREN) */
2725
2726
2727#ifndef VMS
2728/* Figure out the argument list necessary to run LINE as a command. Try to
2729 avoid using a shell. This routine handles only ' quoting, and " quoting
2730 when no backslash, $ or ' characters are seen in the quotes. Starting
2731 quotes may be escaped with a backslash. If any of the characters in
2732 sh_chars is seen, or any of the builtin commands listed in sh_cmds
2733 is the first word of a line, the shell is used.
2734
2735 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
2736 If *RESTP is NULL, newlines will be ignored.
2737
2738 SHELL is the shell to use, or nil to use the default shell.
2739 IFS is the value of $IFS, or nil (meaning the default).
2740
2741 FLAGS is the value of lines_flags for this command line. It is
2742 used in the WINDOWS32 port to check whether + or $(MAKE) were found
2743 in this command line, in which case the effect of just_print_flag
2744 is overridden. */
2745
2746static char **
2747construct_command_argv_internal (char *line, char **restp, const char *shell,
2748 const char *shellflags, const char *ifs,
2749 int flags, char **batch_filename UNUSED)
2750{
2751#ifdef __MSDOS__
2752 /* MSDOS supports both the stock DOS shell and ports of Unixy shells.
2753 We call 'system' for anything that requires ''slow'' processing,
2754 because DOS shells are too dumb. When $SHELL points to a real
2755 (unix-style) shell, 'system' just calls it to do everything. When
2756 $SHELL points to a DOS shell, 'system' does most of the work
2757 internally, calling the shell only for its internal commands.
2758 However, it looks on the $PATH first, so you can e.g. have an
2759 external command named 'mkdir'.
2760
2761 Since we call 'system', certain characters and commands below are
2762 actually not specific to COMMAND.COM, but to the DJGPP implementation
2763 of 'system'. In particular:
2764
2765 The shell wildcard characters are in DOS_CHARS because they will
2766 not be expanded if we call the child via 'spawnXX'.
2767
2768 The ';' is in DOS_CHARS, because our 'system' knows how to run
2769 multiple commands on a single line.
2770
2771 DOS_CHARS also include characters special to 4DOS/NDOS, so we
2772 won't have to tell one from another and have one more set of
2773 commands and special characters. */
2774 static const char *sh_chars_dos = "*?[];|<>%^&()";
2775 static const char *sh_cmds_dos[] =
2776 { "break", "call", "cd", "chcp", "chdir", "cls", "copy", "ctty", "date",
2777 "del", "dir", "echo", "erase", "exit", "for", "goto", "if", "md",
2778 "mkdir", "path", "pause", "prompt", "rd", "rmdir", "rem", "ren",
2779 "rename", "set", "shift", "time", "type", "ver", "verify", "vol", ":",
2780 0 };
2781
2782 static const char *sh_chars_sh = "#;\"*?[]&|<>(){}$`^";
2783 static const char *sh_cmds_sh[] =
2784 { "cd", "echo", "eval", "exec", "exit", "login", "logout", "set", "umask",
2785 "wait", "while", "for", "case", "if", ":", ".", "break", "continue",
2786 "export", "read", "readonly", "shift", "times", "trap", "switch",
2787 "unset", "ulimit", 0 };
2788
2789 const char *sh_chars;
2790 const char **sh_cmds;
2791
2792#elif defined (__EMX__)
2793 static const char *sh_chars_dos = "*?[];|<>%^&()";
2794 static const char *sh_cmds_dos[] =
2795 { "break", "call", "cd", "chcp", "chdir", "cls", "copy", "ctty", "date",
2796 "del", "dir", "echo", "erase", "exit", "for", "goto", "if", "md",
2797 "mkdir", "path", "pause", "prompt", "rd", "rmdir", "rem", "ren",
2798 "rename", "set", "shift", "time", "type", "ver", "verify", "vol", ":",
2799 0 };
2800
2801 static const char *sh_chars_os2 = "*?[];|<>%^()\"'&";
2802 static const char *sh_cmds_os2[] =
2803 { "call", "cd", "chcp", "chdir", "cls", "copy", "date", "del", "detach",
2804 "dir", "echo", "endlocal", "erase", "exit", "for", "goto", "if", "keys",
2805 "md", "mkdir", "move", "path", "pause", "prompt", "rd", "rem", "ren",
2806 "rename", "rmdir", "set", "setlocal", "shift", "start", "time", "type",
2807 "ver", "verify", "vol", ":", 0 };
2808
2809 static const char *sh_chars_sh = "#;\"*?[]&|<>(){}$`^~'";
2810 static const char *sh_cmds_sh[] =
2811 { "echo", "cd", "eval", "exec", "exit", "login", "logout", "set", "umask",
2812 "wait", "while", "for", "case", "if", ":", ".", "break", "continue",
2813 "export", "read", "readonly", "shift", "times", "trap", "switch",
2814 "unset", 0 };
2815
2816 const char *sh_chars;
2817 const char **sh_cmds;
2818
2819#elif defined (_AMIGA)
2820 static const char *sh_chars = "#;\"|<>()?*$`";
2821 static const char *sh_cmds[] =
2822 { "cd", "eval", "if", "delete", "echo", "copy", "rename", "set", "setenv",
2823 "date", "makedir", "skip", "else", "endif", "path", "prompt", "unset",
2824 "unsetenv", "version", 0 };
2825
2826#elif defined (WINDOWS32)
2827 /* We used to have a double quote (") in sh_chars_dos[] below, but
2828 that caused any command line with quoted file names be run
2829 through a temporary batch file, which introduces command-line
2830 limit of 4K charcaters imposed by cmd.exe. Since CreateProcess
2831 can handle quoted file names just fine, removing the quote lifts
2832 the limit from a very frequent use case, because using quoted
2833 file names is commonplace on MS-Windows. */
2834 static const char *sh_chars_dos = "|&<>";
2835 static const char *sh_cmds_dos[] =
2836 { "assoc", "break", "call", "cd", "chcp", "chdir", "cls", "color", "copy",
2837 "ctty", "date", "del", "dir", "echo", "echo.", "endlocal", "erase",
2838 "exit", "for", "ftype", "goto", "if", "if", "md", "mkdir", "move",
2839 "path", "pause", "prompt", "rd", "rem", "ren", "rename", "rmdir",
2840 "set", "setlocal", "shift", "time", "title", "type", "ver", "verify",
2841 "vol", ":", 0 };
2842
2843 static const char *sh_chars_sh = "#;\"*?[]&|<>(){}$`^";
2844 static const char *sh_cmds_sh[] =
2845 { "cd", "eval", "exec", "exit", "login", "logout", "set", "umask", "wait",
2846 "while", "for", "case", "if", ":", ".", "break", "continue", "export",
2847 "read", "readonly", "shift", "times", "trap", "switch", "test",
2848#ifdef BATCH_MODE_ONLY_SHELL
2849 "echo",
2850#endif
2851 0 };
2852
2853 const char *sh_chars;
2854 char const * const * sh_cmds; /* kmk: +_sh +const*2 */
2855#elif defined(__riscos__)
2856 static const char *sh_chars = "";
2857 static const char *sh_cmds[] = { 0 };
2858#else /* must be UNIX-ish */
2859 static const char *sh_chars_sh = "#;\"*?[]&|<>(){}$`^~!"; /* kmk: +_sh */
2860 static const char *sh_cmds_sh[] = /* kmk: +_sh */
2861 { ".", ":", "break", "case", "cd", "continue", "eval", "exec", "exit",
2862 "export", "for", "if", "login", "logout", "read", "readonly", "set",
2863 "shift", "switch", "test", "times", "trap", "ulimit", "umask", "unset",
2864 "wait", "while", 0 };
2865
2866# if 0 /*def HAVE_DOS_PATHS - kmk */
2867 /* This is required if the MSYS/Cygwin ports (which do not define
2868 WINDOWS32) are compiled with HAVE_DOS_PATHS defined, which uses
2869 sh_chars_sh directly (see below). The value must be identical
2870 to that of sh_chars immediately above. */
2871 static const char *sh_chars_sh = "#;\"*?[]&|<>(){}$`^~!";
2872# endif /* HAVE_DOS_PATHS */
2873 char const * sh_chars = sh_chars_sh; /* kmk: +_sh +const */
2874 char const * const * sh_cmds = sh_cmds_sh; /* kmk: +_sh +const*2 */
2875#endif
2876#ifdef KMK
2877 static const char sh_chars_kash[] = "#;*?[]&|<>(){}$`^~!"; /* note: no \" - good idea? */
2878 static const char * const sh_cmds_kash[] = {
2879 ".", ":", "break", "case", "cd", "continue", "echo", "eval", "exec", "exit",
2880 "export", "for", "if", "login", "logout", "read", "readonly", "set",
2881 "shift", "switch", "test", "times", "trap", "umask", "wait", "while", 0 /* +echo, -ulimit, -unset */
2882 };
2883 int is_kmk_shell = 0;
2884#endif
2885 int i;
2886 char *p;
2887#ifndef NDEBUG
2888 char *end;
2889#endif
2890 char *ap;
2891 const char *cap;
2892 const char *cp;
2893 int instring, word_has_equals, seen_nonequals, last_argument_was_empty;
2894 char **new_argv = 0;
2895 char *argstr = 0;
2896#ifdef WINDOWS32
2897 int slow_flag = 0;
2898
2899 if (!unixy_shell)
2900 {
2901 sh_cmds = sh_cmds_dos;
2902 sh_chars = sh_chars_dos;
2903 }
2904 else
2905 {
2906 sh_cmds = sh_cmds_sh;
2907 sh_chars = sh_chars_sh;
2908 }
2909#endif /* WINDOWS32 */
2910
2911 if (restp != NULL)
2912 *restp = NULL;
2913
2914 /* Make sure not to bother processing an empty line but stop at newline. */
2915 while (ISBLANK (*line))
2916 ++line;
2917 if (*line == '\0')
2918 return 0;
2919
2920 if (shellflags == 0)
2921 shellflags = posix_pedantic ? "-ec" : "-c";
2922
2923 /* See if it is safe to parse commands internally. */
2924#ifdef KMK /* kmk_ash and kmk_kash are both fine, kmk_ash is the default btw. */
2925 if (shell == 0)
2926 {
2927 is_kmk_shell = 1;
2928 shell = (char *)get_default_kbuild_shell ();
2929 }
2930 else if (!strcmp (shell, get_default_kbuild_shell()))
2931 is_kmk_shell = 1;
2932 else
2933 {
2934 const char *psz = strstr (shell, "/kmk_ash");
2935 if (psz)
2936 psz += sizeof ("/kmk_ash") - 1;
2937 else
2938 {
2939 psz = strstr (shell, "/kmk_kash");
2940 if (psz)
2941 psz += sizeof ("/kmk_kash") - 1;
2942 }
2943# if defined (__OS2__) || defined (_WIN32) || defined (WINDOWS32)
2944 is_kmk_shell = psz && (*psz == '\0' || !stricmp (psz, ".exe"));
2945# else
2946 is_kmk_shell = psz && *psz == '\0';
2947# endif
2948 }
2949 if (is_kmk_shell)
2950 {
2951 sh_chars = sh_chars_kash;
2952 sh_cmds = sh_cmds_kash;
2953 }
2954#else /* !KMK */
2955 if (shell == 0)
2956 shell = default_shell;
2957#endif /* !KMK */
2958#ifdef WINDOWS32
2959 else if (strcmp (shell, default_shell))
2960 {
2961 char *s1 = _fullpath (NULL, shell, 0);
2962 char *s2 = _fullpath (NULL, default_shell, 0);
2963
2964 slow_flag = strcmp ((s1 ? s1 : ""), (s2 ? s2 : ""));
2965
2966 free (s1);
2967 free (s2);
2968 }
2969 if (slow_flag)
2970 goto slow;
2971#else /* not WINDOWS32 */
2972#if defined (__MSDOS__) || defined (__EMX__)
2973 else if (strcasecmp (shell, default_shell))
2974 {
2975 extern int _is_unixy_shell (const char *_path);
2976
2977 DB (DB_BASIC, (_("$SHELL changed (was '%s', now '%s')\n"),
2978 default_shell, shell));
2979 unixy_shell = _is_unixy_shell (shell);
2980 /* we must allocate a copy of shell: construct_command_argv() will free
2981 * shell after this function returns. */
2982 default_shell = xstrdup (shell);
2983 }
2984# ifdef KMK
2985 if (is_kmk_shell)
2986 { /* done above already */ }
2987 else
2988# endif
2989 if (unixy_shell)
2990 {
2991 sh_chars = sh_chars_sh;
2992 sh_cmds = sh_cmds_sh;
2993 }
2994 else
2995 {
2996 sh_chars = sh_chars_dos;
2997 sh_cmds = sh_cmds_dos;
2998# ifdef __EMX__
2999 if (_osmode == OS2_MODE)
3000 {
3001 sh_chars = sh_chars_os2;
3002 sh_cmds = sh_cmds_os2;
3003 }
3004# endif
3005 }
3006#else /* !__MSDOS__ */
3007 else if (strcmp (shell, default_shell))
3008 goto slow;
3009#endif /* !__MSDOS__ && !__EMX__ */
3010#endif /* not WINDOWS32 */
3011
3012 if (ifs)
3013 for (cap = ifs; *cap != '\0'; ++cap)
3014 if (*cap != ' ' && *cap != '\t' && *cap != '\n')
3015 goto slow;
3016
3017 if (shellflags)
3018 if (shellflags[0] != '-'
3019 || ((shellflags[1] != 'c' || shellflags[2] != '\0')
3020 && (shellflags[1] != 'e' || shellflags[2] != 'c' || shellflags[3] != '\0')))
3021 goto slow;
3022
3023 i = strlen (line) + 1;
3024
3025 /* More than 1 arg per character is impossible. */
3026 new_argv = xmalloc (i * sizeof (char *));
3027
3028 /* All the args can fit in a buffer as big as LINE is. */
3029 ap = new_argv[0] = argstr = xmalloc (i);
3030#ifndef NDEBUG
3031 end = ap + i;
3032#endif
3033
3034 /* I is how many complete arguments have been found. */
3035 i = 0;
3036 instring = word_has_equals = seen_nonequals = last_argument_was_empty = 0;
3037 for (p = line; *p != '\0'; ++p)
3038 {
3039 assert (ap <= end);
3040
3041 if (instring)
3042 {
3043 /* Inside a string, just copy any char except a closing quote
3044 or a backslash-newline combination. */
3045 if (*p == instring)
3046 {
3047 instring = 0;
3048 if (ap == new_argv[0] || *(ap-1) == '\0')
3049 last_argument_was_empty = 1;
3050 }
3051 else if (*p == '\\' && p[1] == '\n')
3052 {
3053 /* Backslash-newline is handled differently depending on what
3054 kind of string we're in: inside single-quoted strings you
3055 keep them; in double-quoted strings they disappear. For
3056 DOS/Windows/OS2, if we don't have a POSIX shell, we keep the
3057 pre-POSIX behavior of removing the backslash-newline. */
3058 if (instring == '"'
3059#if defined (__MSDOS__) || defined (__EMX__) || defined (WINDOWS32)
3060 || !unixy_shell
3061#endif
3062 )
3063 ++p;
3064 else
3065 {
3066 *(ap++) = *(p++);
3067 *(ap++) = *p;
3068 }
3069 }
3070 else if (*p == '\n' && restp != NULL)
3071 {
3072 /* End of the command line. */
3073 *restp = p;
3074 goto end_of_line;
3075 }
3076 /* Backslash, $, and ` are special inside double quotes.
3077 If we see any of those, punt.
3078 But on MSDOS, if we use COMMAND.COM, double and single
3079 quotes have the same effect. */
3080 else if (instring == '"' && strchr ("\\$`", *p) != 0 && unixy_shell)
3081 goto slow;
3082#ifdef WINDOWS32
3083 /* Quoted wildcard characters must be passed quoted to the
3084 command, so give up the fast route. */
3085 else if (instring == '"' && strchr ("*?", *p) != 0 && !unixy_shell)
3086 goto slow;
3087 else if (instring == '"' && strncmp (p, "\\\"", 2) == 0)
3088 *ap++ = *++p;
3089#endif
3090 else
3091 *ap++ = *p;
3092 }
3093 else if (strchr (sh_chars, *p) != 0)
3094#ifdef KMK
3095 {
3096 /* Tilde is only special if at the start of a path spec,
3097 i.e. don't get excited when we by 8.3 files on windows. */
3098 if ( *p == '~'
3099 && p > line
3100 && !ISSPACE (p[-1])
3101 && p[-1] != '='
3102 && p[-1] != ':'
3103 && p[-1] != '"'
3104 && p[-1] != '\'')
3105 *ap++ = *p;
3106 else
3107 /* Not inside a string, but it's a special char. */
3108 goto slow;
3109 }
3110#else /* !KMK */
3111 /* Not inside a string, but it's a special char. */
3112 goto slow;
3113#endif /* !KMK */
3114 else if (one_shell && *p == '\n')
3115 /* In .ONESHELL mode \n is a separator like ; or && */
3116 goto slow;
3117#ifdef __MSDOS__
3118 else if (*p == '.' && p[1] == '.' && p[2] == '.' && p[3] != '.')
3119 /* '...' is a wildcard in DJGPP. */
3120 goto slow;
3121#endif
3122 else
3123 /* Not a special char. */
3124 switch (*p)
3125 {
3126 case '=':
3127 /* Equals is a special character in leading words before the
3128 first word with no equals sign in it. This is not the case
3129 with sh -k, but we never get here when using nonstandard
3130 shell flags. */
3131 if (! seen_nonequals && unixy_shell)
3132 goto slow;
3133 word_has_equals = 1;
3134 *ap++ = '=';
3135 break;
3136
3137 case '\\':
3138 /* Backslash-newline has special case handling, ref POSIX.
3139 We're in the fastpath, so emulate what the shell would do. */
3140 if (p[1] == '\n')
3141 {
3142 /* Throw out the backslash and newline. */
3143 ++p;
3144
3145 /* At the beginning of the argument, skip any whitespace other
3146 than newline before the start of the next word. */
3147 if (ap == new_argv[i])
3148 while (ISBLANK (p[1]))
3149 ++p;
3150 }
3151#ifdef WINDOWS32
3152 /* Backslash before whitespace is not special if our shell
3153 is not Unixy. */
3154 else if (ISSPACE (p[1]) && !unixy_shell)
3155 {
3156 *ap++ = *p;
3157 break;
3158 }
3159#endif
3160 else if (p[1] != '\0')
3161 {
3162#ifdef HAVE_DOS_PATHS
3163 /* Only remove backslashes before characters special to Unixy
3164 shells. All other backslashes are copied verbatim, since
3165 they are probably DOS-style directory separators. This
3166 still leaves a small window for problems, but at least it
3167 should work for the vast majority of naive users. */
3168
3169#ifdef __MSDOS__
3170 /* A dot is only special as part of the "..."
3171 wildcard. */
3172 if (strneq (p + 1, ".\\.\\.", 5))
3173 {
3174 *ap++ = '.';
3175 *ap++ = '.';
3176 p += 4;
3177 }
3178 else
3179#endif
3180 if (p[1] != '\\' && p[1] != '\''
3181 && !ISSPACE (p[1])
3182# ifdef KMK
3183 && strchr (sh_chars, p[1]) == 0
3184 && (p[1] != '"' || !unixy_shell))
3185# else
3186 && strchr (sh_chars_sh, p[1]) == 0)
3187# endif
3188 /* back up one notch, to copy the backslash */
3189 --p;
3190#endif /* HAVE_DOS_PATHS */
3191
3192 /* Copy and skip the following char. */
3193 *ap++ = *++p;
3194 }
3195 break;
3196
3197 case '\'':
3198 case '"':
3199 instring = *p;
3200 break;
3201
3202 case '\n':
3203 if (restp != NULL)
3204 {
3205 /* End of the command line. */
3206 *restp = p;
3207 goto end_of_line;
3208 }
3209 else
3210 /* Newlines are not special. */
3211 *ap++ = '\n';
3212 break;
3213
3214 case ' ':
3215 case '\t':
3216 /* We have the end of an argument.
3217 Terminate the text of the argument. */
3218 *ap++ = '\0';
3219 new_argv[++i] = ap;
3220 last_argument_was_empty = 0;
3221
3222 /* Update SEEN_NONEQUALS, which tells us if every word
3223 heretofore has contained an '='. */
3224 seen_nonequals |= ! word_has_equals;
3225 if (word_has_equals && ! seen_nonequals)
3226 /* An '=' in a word before the first
3227 word without one is magical. */
3228 goto slow;
3229 word_has_equals = 0; /* Prepare for the next word. */
3230
3231 /* If this argument is the command name,
3232 see if it is a built-in shell command.
3233 If so, have the shell handle it. */
3234 if (i == 1)
3235 {
3236 register int j;
3237 for (j = 0; sh_cmds[j] != 0; ++j)
3238 {
3239 if (streq (sh_cmds[j], new_argv[0]))
3240 goto slow;
3241#if defined(__EMX__) || defined(WINDOWS32)
3242 /* Non-Unix shells are case insensitive. */
3243 if (!unixy_shell
3244 && strcasecmp (sh_cmds[j], new_argv[0]) == 0)
3245 goto slow;
3246#endif
3247 }
3248 }
3249
3250 /* Skip whitespace chars, but not newlines. */
3251 while (ISBLANK (p[1]))
3252 ++p;
3253 break;
3254
3255 default:
3256 *ap++ = *p;
3257 break;
3258 }
3259 }
3260 end_of_line:
3261
3262 if (instring)
3263 /* Let the shell deal with an unterminated quote. */
3264 goto slow;
3265
3266 /* Terminate the last argument and the argument list. */
3267
3268 *ap = '\0';
3269 if (new_argv[i][0] != '\0' || last_argument_was_empty)
3270 ++i;
3271 new_argv[i] = 0;
3272
3273 if (i == 1)
3274 {
3275 register int j;
3276 for (j = 0; sh_cmds[j] != 0; ++j)
3277 if (streq (sh_cmds[j], new_argv[0]))
3278 goto slow;
3279 }
3280
3281 if (new_argv[0] == 0)
3282 {
3283 /* Line was empty. */
3284 free (argstr);
3285 free (new_argv);
3286 return 0;
3287 }
3288
3289 return new_argv;
3290
3291 slow:;
3292 /* We must use the shell. */
3293
3294 if (new_argv != 0)
3295 {
3296 /* Free the old argument list we were working on. */
3297 free (argstr);
3298 free (new_argv);
3299 }
3300
3301#ifdef __MSDOS__
3302 execute_by_shell = 1; /* actually, call 'system' if shell isn't unixy */
3303#endif
3304
3305#ifdef _AMIGA
3306 {
3307 char *ptr;
3308 char *buffer;
3309 char *dptr;
3310
3311 buffer = xmalloc (strlen (line)+1);
3312
3313 ptr = line;
3314 for (dptr=buffer; *ptr; )
3315 {
3316 if (*ptr == '\\' && ptr[1] == '\n')
3317 ptr += 2;
3318 else if (*ptr == '@') /* Kludge: multiline commands */
3319 {
3320 ptr += 2;
3321 *dptr++ = '\n';
3322 }
3323 else
3324 *dptr++ = *ptr++;
3325 }
3326 *dptr = 0;
3327
3328 new_argv = xmalloc (2 * sizeof (char *));
3329 new_argv[0] = buffer;
3330 new_argv[1] = 0;
3331 }
3332#else /* Not Amiga */
3333#ifdef WINDOWS32
3334 /*
3335 * Not eating this whitespace caused things like
3336 *
3337 * sh -c "\n"
3338 *
3339 * which gave the shell fits. I think we have to eat
3340 * whitespace here, but this code should be considered
3341 * suspicious if things start failing....
3342 */
3343
3344 /* Make sure not to bother processing an empty line. */
3345 NEXT_TOKEN (line);
3346 if (*line == '\0')
3347 return 0;
3348#endif /* WINDOWS32 */
3349
3350 {
3351 /* SHELL may be a multi-word command. Construct a command line
3352 "$(SHELL) $(.SHELLFLAGS) LINE", with all special chars in LINE escaped.
3353 Then recurse, expanding this command line to get the final
3354 argument list. */
3355
3356 char *new_line;
3357 unsigned int shell_len = strlen (shell);
3358 unsigned int line_len = strlen (line);
3359 unsigned int sflags_len = shellflags ? strlen (shellflags) : 0;
3360#ifdef WINDOWS32
3361 char *command_ptr = NULL; /* used for batch_mode_shell mode */
3362#endif
3363
3364# ifdef __EMX__ /* is this necessary? */
3365 if (!unixy_shell && shellflags)
3366 ((char *)shellflags)[0] = '/'; /* "/c" */
3367# endif
3368
3369 /* In .ONESHELL mode we are allowed to throw the entire current
3370 recipe string at a single shell and trust that the user
3371 has configured the shell and shell flags, and formatted
3372 the string, appropriately. */
3373 if (one_shell)
3374 {
3375 /* If the shell is Bourne compatible, we must remove and ignore
3376 interior special chars [@+-] because they're meaningless to
3377 the shell itself. If, however, we're in .ONESHELL mode and
3378 have changed SHELL to something non-standard, we should
3379 leave those alone because they could be part of the
3380 script. In this case we must also leave in place
3381 any leading [@+-] for the same reason. */
3382
3383 /* Remove and ignore interior prefix chars [@+-] because they're
3384 meaningless given a single shell. */
3385#if defined __MSDOS__ || defined (__EMX__)
3386 if (unixy_shell) /* the test is complicated and we already did it */
3387#else
3388 if (is_bourne_compatible_shell (shell)
3389#ifdef WINDOWS32
3390 /* If we didn't find any sh.exe, don't behave is if we did! */
3391 && !no_default_sh_exe
3392#endif
3393 )
3394#endif
3395 {
3396 const char *f = line;
3397 char *t = line;
3398
3399 /* Copy the recipe, removing and ignoring interior prefix chars
3400 [@+-]: they're meaningless in .ONESHELL mode. */
3401 while (f[0] != '\0')
3402 {
3403 int esc = 0;
3404
3405 /* This is the start of a new recipe line. Skip whitespace
3406 and prefix characters but not newlines. */
3407#ifndef CONFIG_WITH_COMMANDS_FUNC
3408 while (ISBLANK (*f) || *f == '-' || *f == '@' || *f == '+')
3409#else
3410 char ch;
3411 while (ISBLANK ((ch = *f)) || ch == '-' || ch == '@' || ch == '+' || ch == '%')
3412#endif
3413 ++f;
3414
3415 /* Copy until we get to the next logical recipe line. */
3416 while (*f != '\0')
3417 {
3418 *(t++) = *(f++);
3419 if (f[-1] == '\\')
3420 esc = !esc;
3421 else
3422 {
3423 /* On unescaped newline, we're done with this line. */
3424 if (f[-1] == '\n' && ! esc)
3425 break;
3426
3427 /* Something else: reset the escape sequence. */
3428 esc = 0;
3429 }
3430 }
3431 }
3432 *t = '\0';
3433 }
3434#ifdef WINDOWS32
3435 else /* non-Posix shell (cmd.exe etc.) */
3436 {
3437 const char *f = line;
3438 char *t = line;
3439 char *tstart = t;
3440 int temp_fd;
3441 FILE* batch = NULL;
3442 int id = GetCurrentProcessId ();
3443 PATH_VAR(fbuf);
3444
3445 /* Generate a file name for the temporary batch file. */
3446 sprintf (fbuf, "make%d", id);
3447 *batch_filename = create_batch_file (fbuf, 0, &temp_fd);
3448 DB (DB_JOBS, (_("Creating temporary batch file %s\n"),
3449 *batch_filename));
3450
3451 /* Create a FILE object for the batch file, and write to it the
3452 commands to be executed. Put the batch file in TEXT mode. */
3453 _setmode (temp_fd, _O_TEXT);
3454 batch = _fdopen (temp_fd, "wt");
3455 fputs ("@echo off\n", batch);
3456 DB (DB_JOBS, (_("Batch file contents:\n\t@echo off\n")));
3457
3458 /* Copy the recipe, removing and ignoring interior prefix chars
3459 [@+-]: they're meaningless in .ONESHELL mode. */
3460 while (*f != '\0')
3461 {
3462 /* This is the start of a new recipe line. Skip whitespace
3463 and prefix characters but not newlines. */
3464#ifndef CONFIG_WITH_COMMANDS_FUNC
3465 while (ISBLANK (*f) || *f == '-' || *f == '@' || *f == '+')
3466#else
3467 char ch;
3468 while (ISBLANK ((ch = *f)) || ch == '-' || ch == '@' || ch == '+' || ch == '%')
3469#endif
3470 ++f;
3471
3472 /* Copy until we get to the next logical recipe line. */
3473 while (*f != '\0')
3474 {
3475 /* Remove the escaped newlines in the command, and the
3476 blanks that follow them. Windows shells cannot handle
3477 escaped newlines. */
3478 if (*f == '\\' && f[1] == '\n')
3479 {
3480 f += 2;
3481 while (ISBLANK (*f))
3482 ++f;
3483 }
3484 *(t++) = *(f++);
3485 /* On an unescaped newline, we're done with this
3486 line. */
3487 if (f[-1] == '\n')
3488 break;
3489 }
3490 /* Write another line into the batch file. */
3491 if (t > tstart)
3492 {
3493 char c = *t;
3494 *t = '\0';
3495 fputs (tstart, batch);
3496 DB (DB_JOBS, ("\t%s", tstart));
3497 tstart = t;
3498 *t = c;
3499 }
3500 }
3501 DB (DB_JOBS, ("\n"));
3502 fclose (batch);
3503
3504 /* Create an argv list for the shell command line that
3505 will run the batch file. */
3506 new_argv = xmalloc (2 * sizeof (char *));
3507 new_argv[0] = xstrdup (*batch_filename);
3508 new_argv[1] = NULL;
3509 return new_argv;
3510 }
3511#endif /* WINDOWS32 */
3512 /* Create an argv list for the shell command line. */
3513 {
3514 int n = 0;
3515
3516 new_argv = xmalloc ((4 + sflags_len/2) * sizeof (char *));
3517 new_argv[n++] = xstrdup (shell);
3518
3519 /* Chop up the shellflags (if any) and assign them. */
3520 if (! shellflags)
3521 new_argv[n++] = xstrdup ("");
3522 else
3523 {
3524 const char *s = shellflags;
3525 char *t;
3526 unsigned int len;
3527 while ((t = find_next_token (&s, &len)) != 0)
3528 new_argv[n++] = xstrndup (t, len);
3529 }
3530
3531 /* Set the command to invoke. */
3532 new_argv[n++] = line;
3533 new_argv[n++] = NULL;
3534 }
3535 return new_argv;
3536 }
3537
3538 new_line = xmalloc ((shell_len*2) + 1 + sflags_len + 1
3539 + (line_len*2) + 1);
3540 ap = new_line;
3541 /* Copy SHELL, escaping any characters special to the shell. If
3542 we don't escape them, construct_command_argv_internal will
3543 recursively call itself ad nauseam, or until stack overflow,
3544 whichever happens first. */
3545 for (cp = shell; *cp != '\0'; ++cp)
3546 {
3547 if (strchr (sh_chars, *cp) != 0)
3548 *(ap++) = '\\';
3549 *(ap++) = *cp;
3550 }
3551 *(ap++) = ' ';
3552 if (shellflags)
3553 memcpy (ap, shellflags, sflags_len);
3554 ap += sflags_len;
3555 *(ap++) = ' ';
3556#ifdef WINDOWS32
3557 command_ptr = ap;
3558#endif
3559 for (p = line; *p != '\0'; ++p)
3560 {
3561 if (restp != NULL && *p == '\n')
3562 {
3563 *restp = p;
3564 break;
3565 }
3566 else if (*p == '\\' && p[1] == '\n')
3567 {
3568 /* POSIX says we keep the backslash-newline. If we don't have a
3569 POSIX shell on DOS/Windows/OS2, mimic the pre-POSIX behavior
3570 and remove the backslash/newline. */
3571#if defined (__MSDOS__) || defined (__EMX__) || defined (WINDOWS32)
3572# define PRESERVE_BSNL unixy_shell
3573#else
3574# define PRESERVE_BSNL 1
3575#endif
3576 if (PRESERVE_BSNL)
3577 {
3578 *(ap++) = '\\';
3579 /* Only non-batch execution needs another backslash,
3580 because it will be passed through a recursive
3581 invocation of this function. */
3582 if (!batch_mode_shell)
3583 *(ap++) = '\\';
3584 *(ap++) = '\n';
3585 }
3586 ++p;
3587 continue;
3588 }
3589
3590 /* DOS shells don't know about backslash-escaping. */
3591 if (unixy_shell && !batch_mode_shell &&
3592 (*p == '\\' || *p == '\'' || *p == '"'
3593 || ISSPACE (*p)
3594 || strchr (sh_chars, *p) != 0))
3595 *ap++ = '\\';
3596#ifdef __MSDOS__
3597 else if (unixy_shell && strneq (p, "...", 3))
3598 {
3599 /* The case of '...' wildcard again. */
3600 strcpy (ap, "\\.\\.\\");
3601 ap += 5;
3602 p += 2;
3603 }
3604#endif
3605 *ap++ = *p;
3606 }
3607 if (ap == new_line + shell_len + sflags_len + 2)
3608 {
3609 /* Line was empty. */
3610 free (new_line);
3611 return 0;
3612 }
3613 *ap = '\0';
3614
3615#ifdef WINDOWS32
3616 /* Some shells do not work well when invoked as 'sh -c xxx' to run a
3617 command line (e.g. Cygnus GNUWIN32 sh.exe on WIN32 systems). In these
3618 cases, run commands via a script file. */
3619 if (just_print_flag && !(flags & COMMANDS_RECURSE))
3620 {
3621 /* Need to allocate new_argv, although it's unused, because
3622 start_job_command will want to free it and its 0'th element. */
3623 new_argv = xmalloc (2 * sizeof (char *));
3624 new_argv[0] = xstrdup ("");
3625 new_argv[1] = NULL;
3626 }
3627 else if ((no_default_sh_exe || batch_mode_shell) && batch_filename)
3628 {
3629 int temp_fd;
3630 FILE* batch = NULL;
3631 int id = GetCurrentProcessId ();
3632 PATH_VAR (fbuf);
3633
3634 /* create a file name */
3635 sprintf (fbuf, "make%d", id);
3636 *batch_filename = create_batch_file (fbuf, unixy_shell, &temp_fd);
3637
3638 DB (DB_JOBS, (_("Creating temporary batch file %s\n"),
3639 *batch_filename));
3640
3641 /* Create a FILE object for the batch file, and write to it the
3642 commands to be executed. Put the batch file in TEXT mode. */
3643 _setmode (temp_fd, _O_TEXT);
3644 batch = _fdopen (temp_fd, "wt");
3645 if (!unixy_shell)
3646 fputs ("@echo off\n", batch);
3647 fputs (command_ptr, batch);
3648 fputc ('\n', batch);
3649 fclose (batch);
3650 DB (DB_JOBS, (_("Batch file contents:%s\n\t%s\n"),
3651 !unixy_shell ? "\n\t@echo off" : "", command_ptr));
3652
3653 /* create argv */
3654 new_argv = xmalloc (3 * sizeof (char *));
3655 if (unixy_shell)
3656 {
3657 new_argv[0] = xstrdup (shell);
3658 new_argv[1] = *batch_filename; /* only argv[0] gets freed later */
3659 }
3660 else
3661 {
3662 new_argv[0] = xstrdup (*batch_filename);
3663 new_argv[1] = NULL;
3664 }
3665 new_argv[2] = NULL;
3666 }
3667 else
3668#endif /* WINDOWS32 */
3669
3670 if (unixy_shell)
3671 new_argv = construct_command_argv_internal (new_line, 0, 0, 0, 0,
3672 flags, 0);
3673
3674#ifdef __EMX__
3675 else if (!unixy_shell)
3676 {
3677 /* new_line is local, must not be freed therefore
3678 We use line here instead of new_line because we run the shell
3679 manually. */
3680 size_t line_len = strlen (line);
3681 char *p = new_line;
3682 char *q = new_line;
3683 memcpy (new_line, line, line_len + 1);
3684 /* Replace all backslash-newline combination and also following tabs.
3685 Important: stop at the first '\n' because that's what the loop above
3686 did. The next line starting at restp[0] will be executed during the
3687 next call of this function. */
3688 while (*q != '\0' && *q != '\n')
3689 {
3690 if (q[0] == '\\' && q[1] == '\n')
3691 q += 2; /* remove '\\' and '\n' */
3692 else
3693 *p++ = *q++;
3694 }
3695 *p = '\0';
3696
3697# ifndef NO_CMD_DEFAULT
3698 if (strnicmp (new_line, "echo", 4) == 0
3699 && (new_line[4] == ' ' || new_line[4] == '\t'))
3700 {
3701 /* the builtin echo command: handle it separately */
3702 size_t echo_len = line_len - 5;
3703 char *echo_line = new_line + 5;
3704
3705 /* special case: echo 'x="y"'
3706 cmd works this way: a string is printed as is, i.e., no quotes
3707 are removed. But autoconf uses a command like echo 'x="y"' to
3708 determine whether make works. autoconf expects the output x="y"
3709 so we will do exactly that.
3710 Note: if we do not allow cmd to be the default shell
3711 we do not need this kind of voodoo */
3712 if (echo_line[0] == '\''
3713 && echo_line[echo_len - 1] == '\''
3714 && strncmp (echo_line + 1, "ac_maketemp=",
3715 strlen ("ac_maketemp=")) == 0)
3716 {
3717 /* remove the enclosing quotes */
3718 memmove (echo_line, echo_line + 1, echo_len - 2);
3719 echo_line[echo_len - 2] = '\0';
3720 }
3721 }
3722# endif
3723
3724 {
3725 /* Let the shell decide what to do. Put the command line into the
3726 2nd command line argument and hope for the best ;-) */
3727 size_t sh_len = strlen (shell);
3728
3729 /* exactly 3 arguments + NULL */
3730 new_argv = xmalloc (4 * sizeof (char *));
3731 /* Exactly strlen(shell) + strlen("/c") + strlen(line) + 3 times
3732 the trailing '\0' */
3733 new_argv[0] = xmalloc (sh_len + line_len + 5);
3734 memcpy (new_argv[0], shell, sh_len + 1);
3735 new_argv[1] = new_argv[0] + sh_len + 1;
3736 memcpy (new_argv[1], "/c", 3);
3737 new_argv[2] = new_argv[1] + 3;
3738 memcpy (new_argv[2], new_line, line_len + 1);
3739 new_argv[3] = NULL;
3740 }
3741 }
3742#elif defined(__MSDOS__)
3743 else
3744 {
3745 /* With MSDOS shells, we must construct the command line here
3746 instead of recursively calling ourselves, because we
3747 cannot backslash-escape the special characters (see above). */
3748 new_argv = xmalloc (sizeof (char *));
3749 line_len = strlen (new_line) - shell_len - sflags_len - 2;
3750 new_argv[0] = xmalloc (line_len + 1);
3751 strncpy (new_argv[0],
3752 new_line + shell_len + sflags_len + 2, line_len);
3753 new_argv[0][line_len] = '\0';
3754 }
3755#else
3756 else
3757 fatal (NILF, CSTRLEN (__FILE__) + INTSTR_LENGTH,
3758 _("%s (line %d) Bad shell context (!unixy && !batch_mode_shell)\n"),
3759 __FILE__, __LINE__);
3760#endif
3761
3762 free (new_line);
3763 }
3764#endif /* ! AMIGA */
3765
3766 return new_argv;
3767}
3768#endif /* !VMS */
3769
3770/* Figure out the argument list necessary to run LINE as a command. Try to
3771 avoid using a shell. This routine handles only ' quoting, and " quoting
3772 when no backslash, $ or ' characters are seen in the quotes. Starting
3773 quotes may be escaped with a backslash. If any of the characters in
3774 sh_chars is seen, or any of the builtin commands listed in sh_cmds
3775 is the first word of a line, the shell is used.
3776
3777 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
3778 If *RESTP is NULL, newlines will be ignored.
3779
3780 FILE is the target whose commands these are. It is used for
3781 variable expansion for $(SHELL) and $(IFS). */
3782
3783char **
3784construct_command_argv (char *line, char **restp, struct file *file,
3785 int cmd_flags, char **batch_filename)
3786{
3787 char *shell, *ifs, *shellflags;
3788 char **argv;
3789
3790#ifdef VMS
3791 char *cptr;
3792 int argc;
3793
3794 argc = 0;
3795 cptr = line;
3796 for (;;)
3797 {
3798 while ((*cptr != 0) && (ISSPACE (*cptr)))
3799 cptr++;
3800 if (*cptr == 0)
3801 break;
3802 while ((*cptr != 0) && (!ISSPACE (*cptr)))
3803 cptr++;
3804 argc++;
3805 }
3806
3807 argv = xmalloc (argc * sizeof (char *));
3808 if (argv == 0)
3809 abort ();
3810
3811 cptr = line;
3812 argc = 0;
3813 for (;;)
3814 {
3815 while ((*cptr != 0) && (ISSPACE (*cptr)))
3816 cptr++;
3817 if (*cptr == 0)
3818 break;
3819 DB (DB_JOBS, ("argv[%d] = [%s]\n", argc, cptr));
3820 argv[argc++] = cptr;
3821 while ((*cptr != 0) && (!ISSPACE (*cptr)))
3822 cptr++;
3823 if (*cptr != 0)
3824 *cptr++ = 0;
3825 }
3826#else
3827 {
3828 /* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
3829 int save = warn_undefined_variables_flag;
3830 warn_undefined_variables_flag = 0;
3831
3832 shell = allocated_variable_expand_for_file ("$(SHELL)", file);
3833#ifdef WINDOWS32
3834 /*
3835 * Convert to forward slashes so that construct_command_argv_internal()
3836 * is not confused.
3837 */
3838 if (shell)
3839 {
3840# if 1 /* bird */
3841 unix_slashes (shell);
3842# else
3843 char *p = w32ify (shell, 0);
3844 strcpy (shell, p);
3845# endif
3846 }
3847#endif
3848#ifdef __EMX__
3849 {
3850 static const char *unixroot = NULL;
3851 static const char *last_shell = "";
3852 static int init = 0;
3853 if (init == 0)
3854 {
3855 unixroot = getenv ("UNIXROOT");
3856 /* unixroot must be NULL or not empty */
3857 if (unixroot && unixroot[0] == '\0') unixroot = NULL;
3858 init = 1;
3859 }
3860
3861 /* if we have an unixroot drive and if shell is not default_shell
3862 (which means it's either cmd.exe or the test has already been
3863 performed) and if shell is an absolute path without drive letter,
3864 try whether it exists e.g.: if "/bin/sh" does not exist use
3865 "$UNIXROOT/bin/sh" instead. */
3866 if (unixroot && shell && strcmp (shell, last_shell) != 0
3867 && (shell[0] == '/' || shell[0] == '\\'))
3868 {
3869 /* trying a new shell, check whether it exists */
3870 size_t size = strlen (shell);
3871 char *buf = xmalloc (size + 7);
3872 memcpy (buf, shell, size);
3873 memcpy (buf + size, ".exe", 5); /* including the trailing '\0' */
3874 if (access (shell, F_OK) != 0 && access (buf, F_OK) != 0)
3875 {
3876 /* try the same for the unixroot drive */
3877 memmove (buf + 2, buf, size + 5);
3878 buf[0] = unixroot[0];
3879 buf[1] = unixroot[1];
3880 if (access (buf, F_OK) == 0)
3881 /* we have found a shell! */
3882 /* free(shell); */
3883 shell = buf;
3884 else
3885 free (buf);
3886 }
3887 else
3888 free (buf);
3889 }
3890 }
3891#endif /* __EMX__ */
3892
3893 shellflags = allocated_variable_expand_for_file ("$(.SHELLFLAGS)", file);
3894 ifs = allocated_variable_expand_for_file ("$(IFS)", file);
3895
3896 warn_undefined_variables_flag = save;
3897 }
3898
3899# ifdef CONFIG_WITH_KMK_BUILTIN
3900 /* If it's a kmk_builtin command, make sure we're treated like a
3901 unix shell and and don't get batch files. */
3902 if ( ( !unixy_shell
3903 || batch_mode_shell
3904# ifdef WINDOWS32
3905 || no_default_sh_exe
3906# endif
3907 )
3908 && line
3909 && !strncmp (line, "kmk_builtin_", sizeof("kmk_builtin_") - 1))
3910 {
3911 int saved_batch_mode_shell = batch_mode_shell;
3912 int saved_unixy_shell = unixy_shell;
3913# ifdef WINDOWS32
3914 int saved_no_default_sh_exe = no_default_sh_exe;
3915 no_default_sh_exe = 0;
3916# endif
3917 unixy_shell = 1;
3918 batch_mode_shell = 0;
3919 argv = construct_command_argv_internal (line, restp, shell, shellflags, ifs,
3920 cmd_flags, batch_filename);
3921 batch_mode_shell = saved_batch_mode_shell;
3922 unixy_shell = saved_unixy_shell;
3923# ifdef WINDOWS32
3924 no_default_sh_exe = saved_no_default_sh_exe;
3925# endif
3926 }
3927 else
3928# endif /* CONFIG_WITH_KMK_BUILTIN */
3929 argv = construct_command_argv_internal (line, restp, shell, shellflags, ifs,
3930 cmd_flags, batch_filename);
3931
3932 free (shell);
3933 free (shellflags);
3934 free (ifs);
3935#endif /* !VMS */
3936 return argv;
3937}
3938
3939
3940#if !defined(HAVE_DUP2) && !defined(_AMIGA)
3941int
3942dup2 (int old, int new)
3943{
3944 int fd;
3945
3946 (void) close (new);
3947 EINTRLOOP (fd, dup (old));
3948 if (fd != new)
3949 {
3950 (void) close (fd);
3951 errno = EMFILE;
3952 return -1;
3953 }
3954
3955 return fd;
3956}
3957#endif /* !HAVE_DUP2 && !_AMIGA */
3958
3959#ifdef CONFIG_WITH_PRINT_TIME_SWITCH
3960/* Prints the time elapsed while executing the commands for the given job. */
3961void print_job_time (struct child *c)
3962{
3963 if ( !handling_fatal_signal
3964 && print_time_min != -1
3965 && c->start_ts != -1)
3966 {
3967 big_int elapsed = nano_timestamp () - c->start_ts;
3968 if (elapsed >= print_time_min * BIG_INT_C(1000000000))
3969 {
3970 char buf[64];
3971 int len = format_elapsed_nano (buf, sizeof (buf), elapsed);
3972 if (len > print_time_width)
3973 print_time_width = len;
3974 message (1, print_time_width + strlen (c->file->name),
3975 _("%*s - %s"), print_time_width, buf, c->file->name);
3976 }
3977 }
3978}
3979#endif
3980
3981/* On VMS systems, include special VMS functions. */
3982
3983#ifdef VMS
3984#include "vmsjobs.c"
3985#endif
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use