VirtualBox

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

Last change on this file since 3232 was 3192, checked in by bird, 7 years ago

kmkbuiltin: funnel output thru output.c (usually via err.c).

  • Property svn:eol-style set to native
File size: 119.9 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 if (!jobserver_tokens)
1128 ONS (fatal, NILF, "INTERNAL: Freeing child %p (%s) but no tokens left!\n",
1129 (void *)child, child->file->name);
1130
1131 /* If we're using the jobserver and this child is not the only outstanding
1132 job, put a token back into the pipe for it. */
1133
1134 if (jobserver_enabled () && jobserver_tokens > 1)
1135 {
1136 jobserver_release (1);
1137 DB (DB_JOBS, (_("Released token for child %p (%s).\n"),
1138 (void *)child, child->file->name));
1139 }
1140
1141 --jobserver_tokens;
1142
1143 if (handling_fatal_signal) /* Don't bother free'ing if about to die. */
1144 return;
1145
1146 if (child->command_lines != 0)
1147 {
1148 register unsigned int i;
1149 for (i = 0; i < child->file->cmds->ncommand_lines; ++i)
1150 free (child->command_lines[i]);
1151 free (child->command_lines);
1152 }
1153
1154 if (child->environment != 0)
1155 {
1156 register char **ep = child->environment;
1157 while (*ep != 0)
1158 free (*ep++);
1159 free (child->environment);
1160 }
1161
1162#ifdef CONFIG_WITH_MEMORY_OPTIMIZATIONS
1163 /* Free the chopped command lines for simple targets when
1164 there are no more active references to them. */
1165
1166 child->file->cmds->refs--;
1167 if ( !child->file->intermediate
1168 && !child->file->pat_variables)
1169 free_chopped_commands(child->file->cmds);
1170#endif /* CONFIG_WITH_MEMORY_OPTIMIZATIONS */
1171
1172 free (child);
1173}
1174
1175
1176#ifdef POSIX
1177extern sigset_t fatal_signal_set;
1178#endif
1179
1180void
1181block_sigs (void)
1182{
1183#ifdef POSIX
1184 (void) sigprocmask (SIG_BLOCK, &fatal_signal_set, (sigset_t *) 0);
1185#else
1186# ifdef HAVE_SIGSETMASK
1187 (void) sigblock (fatal_signal_mask);
1188# endif
1189#endif
1190}
1191
1192#ifdef POSIX
1193void
1194unblock_sigs (void)
1195{
1196 sigset_t empty;
1197 sigemptyset (&empty);
1198 sigprocmask (SIG_SETMASK, &empty, (sigset_t *) 0);
1199}
1200#endif
1201
1202/* Start a job to run the commands specified in CHILD.
1203 CHILD is updated to reflect the commands and ID of the child process.
1204
1205 NOTE: On return fatal signals are blocked! The caller is responsible
1206 for calling 'unblock_sigs', once the new child is safely on the chain so
1207 it can be cleaned up in the event of a fatal signal. */
1208
1209static void
1210start_job_command (struct child *child)
1211{
1212 int flags;
1213 char *p;
1214#ifdef VMS
1215 char *argv;
1216#else
1217 char **argv;
1218#endif
1219
1220 /* If we have a completely empty commandset, stop now. */
1221 if (!child->command_ptr)
1222 goto next_command;
1223
1224#ifdef CONFIG_WITH_PRINT_TIME_SWITCH
1225 if (child->start_ts == -1)
1226 child->start_ts = nano_timestamp ();
1227#endif
1228
1229 /* Combine the flags parsed for the line itself with
1230 the flags specified globally for this target. */
1231 flags = (child->file->command_flags
1232 | child->file->cmds->lines_flags[child->command_line - 1]);
1233
1234 p = child->command_ptr;
1235 child->noerror = ((flags & COMMANDS_NOERROR) != 0);
1236
1237 while (*p != '\0')
1238 {
1239 if (*p == '@')
1240 flags |= COMMANDS_SILENT;
1241 else if (*p == '+')
1242 flags |= COMMANDS_RECURSE;
1243 else if (*p == '-')
1244 child->noerror = 1;
1245#ifdef CONFIG_WITH_COMMANDS_FUNC
1246 else if (*p == '%')
1247 flags |= COMMAND_GETTER_SKIP_IT;
1248#endif
1249 /* Don't skip newlines. */
1250 else if (!ISBLANK (*p))
1251#ifndef CONFIG_WITH_KMK_BUILTIN
1252 break;
1253#else /* CONFIG_WITH_KMK_BUILTIN */
1254
1255 {
1256 if ( !(flags & COMMANDS_KMK_BUILTIN)
1257 && !strncmp(p, "kmk_builtin_", sizeof("kmk_builtin_") - 1))
1258 flags |= COMMANDS_KMK_BUILTIN;
1259 break;
1260 }
1261#endif /* CONFIG_WITH_KMK_BUILTIN */
1262 ++p;
1263 }
1264
1265 child->recursive = ((flags & COMMANDS_RECURSE) != 0);
1266
1267 /* Update the file's command flags with any new ones we found. We only
1268 keep the COMMANDS_RECURSE setting. Even this isn't 100% correct; we are
1269 now marking more commands recursive than should be in the case of
1270 multiline define/endef scripts where only one line is marked "+". In
1271 order to really fix this, we'll have to keep a lines_flags for every
1272 actual line, after expansion. */
1273 child->file->cmds->lines_flags[child->command_line - 1] |= flags & COMMANDS_RECURSE;
1274
1275 /* POSIX requires that a recipe prefix after a backslash-newline should
1276 be ignored. Remove it now so the output is correct. */
1277 {
1278 char prefix = child->file->cmds->recipe_prefix;
1279 char *p1, *p2;
1280 p1 = p2 = p;
1281 while (*p1 != '\0')
1282 {
1283 *(p2++) = *p1;
1284 if (p1[0] == '\n' && p1[1] == prefix)
1285 ++p1;
1286 ++p1;
1287 }
1288 *p2 = *p1;
1289 }
1290
1291 /* Figure out an argument list from this command line. */
1292 {
1293 char *end = 0;
1294#ifdef VMS
1295 /* Skip any leading whitespace */
1296 while (*p)
1297 {
1298 if (!ISSPACE (*p))
1299 {
1300 if (*p != '\\')
1301 break;
1302 if ((p[1] != '\n') && (p[1] != 'n') && (p[1] != 't'))
1303 break;
1304 }
1305 p++;
1306 }
1307
1308 argv = p;
1309 /* Although construct_command_argv contains some code for VMS, it was/is
1310 not called/used. Please note, for VMS argv is a string (not an array
1311 of strings) which contains the complete command line, which for
1312 multi-line variables still includes the newlines. So detect newlines
1313 and set 'end' (which is used for child->command_ptr) instead of
1314 (re-)writing construct_command_argv */
1315 if (!one_shell)
1316 {
1317 char *s = p;
1318 int instring = 0;
1319 while (*s)
1320 {
1321 if (*s == '"')
1322 instring = !instring;
1323 else if (*s == '\\' && !instring && *(s+1) != 0)
1324 s++;
1325 else if (*s == '\n' && !instring)
1326 {
1327 end = s;
1328 break;
1329 }
1330 ++s;
1331 }
1332 }
1333#else
1334 argv = construct_command_argv (p, &end, child->file,
1335 child->file->cmds->lines_flags[child->command_line - 1],
1336 &child->sh_batch_file);
1337#endif
1338 if (end == NULL)
1339 child->command_ptr = NULL;
1340 else
1341 {
1342 *end++ = '\0';
1343 child->command_ptr = end;
1344 }
1345 }
1346
1347 /* If -q was given, say that updating 'failed' if there was any text on the
1348 command line, or 'succeeded' otherwise. The exit status of 1 tells the
1349 user that -q is saying 'something to do'; the exit status for a random
1350 error is 2. */
1351 if (argv != 0 && question_flag && !(flags & COMMANDS_RECURSE))
1352 {
1353#ifndef VMS
1354 free (argv[0]);
1355 free (argv);
1356#endif
1357#ifdef VMS
1358 /* On VMS, argv[0] can be a null string here */
1359 if (argv[0] != 0)
1360 {
1361#endif
1362 child->file->update_status = us_question;
1363 notice_finished_file (child->file);
1364 return;
1365#ifdef VMS
1366 }
1367#endif
1368 }
1369
1370 if (touch_flag && !(flags & COMMANDS_RECURSE))
1371 {
1372 /* Go on to the next command. It might be the recursive one.
1373 We construct ARGV only to find the end of the command line. */
1374#ifndef VMS
1375 if (argv)
1376 {
1377 free (argv[0]);
1378 free (argv);
1379 }
1380#endif
1381 argv = 0;
1382 }
1383
1384 if (argv == 0)
1385 {
1386 next_command:
1387#ifdef __MSDOS__
1388 execute_by_shell = 0; /* in case construct_command_argv sets it */
1389#endif
1390 /* This line has no commands. Go to the next. */
1391 if (job_next_command (child))
1392 start_job_command (child);
1393 else
1394 {
1395 /* No more commands. Make sure we're "running"; we might not be if
1396 (e.g.) all commands were skipped due to -n. */
1397 set_command_state (child->file, cs_running);
1398 child->file->update_status = us_success;
1399 notice_finished_file (child->file);
1400 }
1401
1402 OUTPUT_UNSET();
1403 return;
1404 }
1405
1406 /* Are we going to synchronize this command's output? Do so if either we're
1407 in SYNC_RECURSE mode or this command is not recursive. We'll also check
1408 output_sync separately below in case it changes due to error. */
1409 child->output.syncout = output_sync && (output_sync == OUTPUT_SYNC_RECURSE
1410 || !(flags & COMMANDS_RECURSE));
1411 OUTPUT_SET (&child->output);
1412
1413#ifndef NO_OUTPUT_SYNC
1414 if (! child->output.syncout)
1415 /* We don't want to sync this command: to avoid misordered
1416 output ensure any already-synced content is written. */
1417 output_dump (&child->output);
1418#endif
1419
1420 /* Print the command if appropriate. */
1421#ifdef CONFIG_PRETTY_COMMAND_PRINTING
1422 if ( pretty_command_printing
1423 && (just_print_flag || (!(flags & COMMANDS_SILENT) && !silent_flag))
1424 && argv[0][0] != '\0')
1425 {
1426 unsigned i;
1427 for (i = 0; argv[i]; i++)
1428 OSSS ( message, 0, "%s'%s'%s", i ? "\t" : "> ", argv[i], argv[i + 1] ? " \\" : "");
1429 }
1430 else
1431#endif /* CONFIG_PRETTY_COMMAND_PRINTING */
1432 if (just_print_flag || trace_flag
1433 || (!(flags & COMMANDS_SILENT) && !silent_flag))
1434 OS (message, 0, "%s", p);
1435
1436 /* Tell update_goal_chain that a command has been started on behalf of
1437 this target. It is important that this happens here and not in
1438 reap_children (where we used to do it), because reap_children might be
1439 reaping children from a different target. We want this increment to
1440 guaranteedly indicate that a command was started for the dependency
1441 chain (i.e., update_file recursion chain) we are processing. */
1442
1443 ++commands_started;
1444
1445 /* Optimize an empty command. People use this for timestamp rules,
1446 so avoid forking a useless shell. Do this after we increment
1447 commands_started so make still treats this special case as if it
1448 performed some action (makes a difference as to what messages are
1449 printed, etc. */
1450
1451#if !defined(VMS) && !defined(_AMIGA)
1452 if (
1453#if defined __MSDOS__ || defined (__EMX__)
1454 unixy_shell /* the test is complicated and we already did it */
1455#else
1456 (argv[0] && is_bourne_compatible_shell (argv[0]))
1457#endif
1458 && (argv[1] && argv[1][0] == '-'
1459 &&
1460 ((argv[1][1] == 'c' && argv[1][2] == '\0')
1461 ||
1462 (argv[1][1] == 'e' && argv[1][2] == 'c' && argv[1][3] == '\0')))
1463 && (argv[2] && argv[2][0] == ':' && argv[2][1] == '\0')
1464 && argv[3] == NULL)
1465 {
1466 free (argv[0]);
1467 free (argv);
1468 goto next_command;
1469 }
1470#endif /* !VMS && !_AMIGA */
1471
1472 /* If -n was given, recurse to get the next line in the sequence. */
1473
1474 if (just_print_flag && !(flags & COMMANDS_RECURSE))
1475 {
1476#ifndef VMS
1477 free (argv[0]);
1478 free (argv);
1479#endif
1480 goto next_command;
1481 }
1482
1483 /* We're sure we're going to invoke a command: set up the output. */
1484 output_start ();
1485
1486 /* Flush the output streams so they won't have things written twice. */
1487
1488 fflush (stdout);
1489 fflush (stderr);
1490
1491#ifdef CONFIG_WITH_KMK_BUILTIN
1492 /* If builtin command then pass it on to the builtin shell interpreter. */
1493
1494 if ((flags & COMMANDS_KMK_BUILTIN) && !just_print_flag)
1495 {
1496 int rc;
1497 char **argv_spawn = NULL;
1498 char **p2 = argv;
1499 while (*p2 && strncmp (*p2, "kmk_builtin_", sizeof("kmk_builtin_") - 1))
1500 p2++;
1501 assert (*p2);
1502 set_command_state (child->file, cs_running);
1503 child->deleted = 0;
1504 child->pid = 0;
1505 if (p2 != argv)
1506 rc = kmk_builtin_command (*p2, child, &argv_spawn, &child->pid);
1507 else
1508 {
1509 int argc = 1;
1510 while (argv[argc])
1511 argc++;
1512 rc = kmk_builtin_command_parsed (argc, argv, child, &argv_spawn, &child->pid);
1513 }
1514
1515# ifndef VMS
1516 free (argv[0]);
1517 free ((char *) argv);
1518# endif
1519
1520 if (!rc)
1521 {
1522 /* spawned a child? */
1523 if (child->pid)
1524 {
1525 ++job_counter;
1526 return;
1527 }
1528
1529 /* synchronous command execution? */
1530 if (!argv_spawn)
1531 goto next_command;
1532 }
1533
1534 /* failure? */
1535 if (rc)
1536 {
1537 child->pid = (pid_t)42424242;
1538 child->status = rc << 8;
1539 child->has_status = 1;
1540 unblock_sigs();
1541 return;
1542 }
1543
1544 /* conditional check == true; kicking off a child (not kmk_builtin_*). */
1545 argv = argv_spawn;
1546 }
1547#endif /* CONFIG_WITH_KMK_BUILTIN */
1548
1549 /* Decide whether to give this child the 'good' standard input
1550 (one that points to the terminal or whatever), or the 'bad' one
1551 that points to the read side of a broken pipe. */
1552
1553 child->good_stdin = !good_stdin_used;
1554 if (child->good_stdin)
1555 good_stdin_used = 1;
1556
1557 child->deleted = 0;
1558
1559#ifndef _AMIGA
1560 /* Set up the environment for the child. */
1561 if (child->environment == 0)
1562 child->environment = target_environment (child->file);
1563#endif
1564
1565#if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
1566
1567#ifndef VMS
1568 /* start_waiting_job has set CHILD->remote if we can start a remote job. */
1569 if (child->remote)
1570 {
1571 int is_remote, id, used_stdin;
1572 if (start_remote_job (argv, child->environment,
1573 child->good_stdin ? 0 : get_bad_stdin (),
1574 &is_remote, &id, &used_stdin))
1575 /* Don't give up; remote execution may fail for various reasons. If
1576 so, simply run the job locally. */
1577 goto run_local;
1578 else
1579 {
1580 if (child->good_stdin && !used_stdin)
1581 {
1582 child->good_stdin = 0;
1583 good_stdin_used = 0;
1584 }
1585 child->remote = is_remote;
1586 child->pid = id;
1587 }
1588 }
1589 else
1590#endif /* !VMS */
1591 {
1592 /* Fork the child process. */
1593
1594 char **parent_environ;
1595
1596 run_local:
1597 block_sigs ();
1598
1599 child->remote = 0;
1600
1601#ifdef VMS
1602 if (!child_execute_job (child, argv))
1603 {
1604 /* Fork failed! */
1605 perror_with_name ("fork", "");
1606 goto error;
1607 }
1608
1609#else
1610
1611 parent_environ = environ;
1612
1613 jobserver_pre_child (flags & COMMANDS_RECURSE);
1614
1615 child->pid = child_execute_job (&child->output, child->good_stdin, argv, child->environment);
1616
1617 environ = parent_environ; /* Restore value child may have clobbered. */
1618 jobserver_post_child (flags & COMMANDS_RECURSE);
1619
1620 if (child->pid < 0)
1621 {
1622 /* Fork failed! */
1623 unblock_sigs ();
1624 perror_with_name ("fork", "");
1625 goto error;
1626 }
1627#endif /* !VMS */
1628 }
1629
1630#else /* __MSDOS__ or Amiga or WINDOWS32 */
1631#ifdef __MSDOS__
1632 {
1633 int proc_return;
1634
1635 block_sigs ();
1636 dos_status = 0;
1637
1638 /* We call 'system' to do the job of the SHELL, since stock DOS
1639 shell is too dumb. Our 'system' knows how to handle long
1640 command lines even if pipes/redirection is needed; it will only
1641 call COMMAND.COM when its internal commands are used. */
1642 if (execute_by_shell)
1643 {
1644 char *cmdline = argv[0];
1645 /* We don't have a way to pass environment to 'system',
1646 so we need to save and restore ours, sigh... */
1647 char **parent_environ = environ;
1648
1649 environ = child->environment;
1650
1651 /* If we have a *real* shell, tell 'system' to call
1652 it to do everything for us. */
1653 if (unixy_shell)
1654 {
1655 /* A *real* shell on MSDOS may not support long
1656 command lines the DJGPP way, so we must use 'system'. */
1657 cmdline = argv[2]; /* get past "shell -c" */
1658 }
1659
1660 dos_command_running = 1;
1661 proc_return = system (cmdline);
1662 environ = parent_environ;
1663 execute_by_shell = 0; /* for the next time */
1664 }
1665 else
1666 {
1667 dos_command_running = 1;
1668 proc_return = spawnvpe (P_WAIT, argv[0], argv, child->environment);
1669 }
1670
1671 /* Need to unblock signals before turning off
1672 dos_command_running, so that child's signals
1673 will be treated as such (see fatal_error_signal). */
1674 unblock_sigs ();
1675 dos_command_running = 0;
1676
1677 /* If the child got a signal, dos_status has its
1678 high 8 bits set, so be careful not to alter them. */
1679 if (proc_return == -1)
1680 dos_status |= 0xff;
1681 else
1682 dos_status |= (proc_return & 0xff);
1683 ++dead_children;
1684 child->pid = dos_pid++;
1685 }
1686#endif /* __MSDOS__ */
1687#ifdef _AMIGA
1688 amiga_status = MyExecute (argv);
1689
1690 ++dead_children;
1691 child->pid = amiga_pid++;
1692 if (amiga_batch_file)
1693 {
1694 amiga_batch_file = 0;
1695 DeleteFile (amiga_bname); /* Ignore errors. */
1696 }
1697#endif /* Amiga */
1698#ifdef WINDOWS32
1699 {
1700# ifndef CONFIG_NEW_WIN_CHILDREN
1701 HANDLE hPID;
1702 char* arg0;
1703 int outfd = FD_STDOUT;
1704 int errfd = FD_STDERR;
1705
1706 /* make UNC paths safe for CreateProcess -- backslash format */
1707 arg0 = argv[0];
1708 if (arg0 && arg0[0] == '/' && arg0[1] == '/')
1709 for ( ; arg0 && *arg0; arg0++)
1710 if (*arg0 == '/')
1711 *arg0 = '\\';
1712
1713 /* make sure CreateProcess() has Path it needs */
1714 sync_Path_environment ();
1715
1716#ifndef NO_OUTPUT_SYNC
1717 /* Divert child output if output_sync in use. */
1718 if (child->output.syncout)
1719 {
1720 if (child->output.out >= 0)
1721 outfd = child->output.out;
1722 if (child->output.err >= 0)
1723 errfd = child->output.err;
1724 }
1725#else
1726 outfd = errfd = -1;
1727#endif
1728 hPID = process_easy (argv, child->environment, outfd, errfd);
1729
1730 if (hPID != INVALID_HANDLE_VALUE)
1731 child->pid = (pid_t) hPID;
1732 else
1733 {
1734 int i;
1735 unblock_sigs ();
1736 fprintf (stderr,
1737 _("process_easy() failed to launch process (e=%ld)\n"),
1738 process_last_err (hPID));
1739 for (i = 0; argv[i]; i++)
1740 fprintf (stderr, "%s ", argv[i]);
1741 fprintf (stderr, _("\nCounted %d args in failed launch\n"), i);
1742 goto error;
1743 }
1744# else /* CONFIG_NEW_WIN_CHILDREN */
1745 struct variable *shell_var = lookup_variable("SHELL", 5);
1746 const char *shell_value = !shell_var ? NULL
1747 : !shell_var->recursive || strchr(shell_var->value, '$') == NULL
1748 ? shell_var->value : variable_expand (shell_var->value);
1749 int rc = MkWinChildCreate(argv, child->environment, shell_value, child, &child->pid);
1750 if (rc != 0)
1751 {
1752 int i;
1753 unblock_sigs ();
1754 fprintf (stderr, _("failed to launch process (rc=%d)\n"), rc);
1755 for (i = 0; argv[i]; i++)
1756 fprintf (stderr, "%s ", argv[i]);
1757 fprintf (stderr, "\n", argv[i]);
1758 goto error;
1759 }
1760#endif /* CONFIG_NEW_WIN_CHILDREN */
1761 }
1762#endif /* WINDOWS32 */
1763#endif /* __MSDOS__ or Amiga or WINDOWS32 */
1764
1765 /* Bump the number of jobs started in this second. */
1766 ++job_counter;
1767
1768 /* We are the parent side. Set the state to
1769 say the commands are running and return. */
1770
1771 set_command_state (child->file, cs_running);
1772
1773 /* Free the storage used by the child's argument list. */
1774#ifdef KMK /* leak */
1775 cleanup_argv:
1776#endif
1777#ifndef VMS
1778 free (argv[0]);
1779 free (argv);
1780#endif
1781
1782 OUTPUT_UNSET();
1783 return;
1784
1785 error:
1786 child->file->update_status = us_failed;
1787 notice_finished_file (child->file);
1788#ifdef KMK /* fix leak */
1789 goto cleanup_argv;
1790#else
1791 OUTPUT_UNSET();
1792#endif
1793}
1794
1795/* Try to start a child running.
1796 Returns nonzero if the child was started (and maybe finished), or zero if
1797 the load was too high and the child was put on the 'waiting_jobs' chain. */
1798
1799static int
1800start_waiting_job (struct child *c)
1801{
1802 struct file *f = c->file;
1803#ifdef DB_KMK
1804 DB (DB_KMK, (_("start_waiting_job %p (`%s') command_flags=%#x slots=%d/%d\n"),
1805 (void *)c, c->file->name, c->file->command_flags, job_slots_used, job_slots));
1806#endif
1807
1808 /* If we can start a job remotely, we always want to, and don't care about
1809 the local load average. We record that the job should be started
1810 remotely in C->remote for start_job_command to test. */
1811
1812 c->remote = start_remote_job_p (1);
1813
1814#ifdef CONFIG_WITH_EXTENDED_NOTPARALLEL
1815 if (c->file->command_flags & COMMANDS_NOTPARALLEL)
1816 {
1817 DB (DB_KMK, (_("not_parallel %d -> %d (file=%p `%s') [start_waiting_job]\n"),
1818 not_parallel, not_parallel + 1, (void *)c->file, c->file->name));
1819 assert(not_parallel >= 0);
1820 ++not_parallel;
1821 }
1822#endif /* CONFIG_WITH_EXTENDED_NOTPARALLEL */
1823
1824 /* If we are running at least one job already and the load average
1825 is too high, make this one wait. */
1826 if (!c->remote
1827#ifdef CONFIG_WITH_EXTENDED_NOTPARALLEL
1828 && ((job_slots_used > 0 && (not_parallel > 0 || load_too_high ()))
1829#else
1830 && ((job_slots_used > 0 && load_too_high ())
1831#endif
1832#ifdef WINDOWS32
1833# ifndef CONFIG_NEW_WIN_CHILDREN
1834 || (process_used_slots () >= MAXIMUM_WAIT_OBJECTS)
1835# endif
1836#endif
1837 ))
1838 {
1839#ifndef CONFIG_WITH_EXTENDED_NOTPARALLEL
1840 /* Put this child on the chain of children waiting for the load average
1841 to go down. */
1842 set_command_state (f, cs_running);
1843 c->next = waiting_jobs;
1844 waiting_jobs = c;
1845
1846#else /* CONFIG_WITH_EXTENDED_NOTPARALLEL */
1847
1848 /* Put this child on the chain of children waiting for the load average
1849 to go down. If not parallel, put it last. */
1850 set_command_state (f, cs_running);
1851 c->next = waiting_jobs;
1852 if (c->next && (c->file->command_flags & COMMANDS_NOTPARALLEL))
1853 {
1854 struct child *prev = waiting_jobs;
1855 while (prev->next)
1856 prev = prev->next;
1857 c->next = 0;
1858 prev->next = c;
1859 }
1860 else /* FIXME: insert after the last node with COMMANDS_NOTPARALLEL set */
1861 waiting_jobs = c;
1862 DB (DB_KMK, (_("queued child %p (`%s')\n"), (void *)c, c->file->name));
1863#endif /* CONFIG_WITH_EXTENDED_NOTPARALLEL */
1864 return 0;
1865 }
1866
1867 /* Start the first command; reap_children will run later command lines. */
1868 start_job_command (c);
1869
1870 switch (f->command_state)
1871 {
1872 case cs_running:
1873 c->next = children;
1874 DB (DB_JOBS, (_("Putting child %p (%s) PID %s%s on the chain.\n"),
1875 (void *)c, c->file->name, pid2str (c->pid),
1876 c->remote ? _(" (remote)") : ""));
1877 children = c;
1878 /* One more job slot is in use. */
1879 ++job_slots_used;
1880 unblock_sigs ();
1881 break;
1882
1883 case cs_not_started:
1884 /* All the command lines turned out to be empty. */
1885 f->update_status = us_success;
1886 /* FALLTHROUGH */
1887
1888 case cs_finished:
1889 notice_finished_file (f);
1890 free_child (c);
1891 break;
1892
1893 default:
1894 assert (f->command_state == cs_finished);
1895 break;
1896 }
1897
1898 return 1;
1899}
1900
1901/* Create a 'struct child' for FILE and start its commands running. */
1902
1903void
1904new_job (struct file *file)
1905{
1906 struct commands *cmds = file->cmds;
1907 struct child *c;
1908 char **lines;
1909 unsigned int i;
1910
1911 /* Let any previously decided-upon jobs that are waiting
1912 for the load to go down start before this new one. */
1913 start_waiting_jobs ();
1914
1915 /* Reap any children that might have finished recently. */
1916 reap_children (0, 0);
1917
1918 /* Chop the commands up into lines if they aren't already. */
1919 chop_commands (cmds);
1920#ifdef CONFIG_WITH_MEMORY_OPTIMIZATIONS
1921 cmds->refs++; /* retain the chopped lines. */
1922#endif
1923
1924 /* Start the command sequence, record it in a new
1925 'struct child', and add that to the chain. */
1926
1927 c = xcalloc (sizeof (struct child));
1928 output_init (&c->output);
1929
1930 c->file = file;
1931 c->sh_batch_file = NULL;
1932
1933 /* Cache dontcare flag because file->dontcare can be changed once we
1934 return. Check dontcare inheritance mechanism for details. */
1935 c->dontcare = file->dontcare;
1936
1937 /* Start saving output in case the expansion uses $(info ...) etc. */
1938 OUTPUT_SET (&c->output);
1939
1940 /* Expand the command lines and store the results in LINES. */
1941 lines = xmalloc (cmds->ncommand_lines * sizeof (char *));
1942 for (i = 0; i < cmds->ncommand_lines; ++i)
1943 {
1944 /* Collapse backslash-newline combinations that are inside variable
1945 or function references. These are left alone by the parser so
1946 that they will appear in the echoing of commands (where they look
1947 nice); and collapsed by construct_command_argv when it tokenizes.
1948 But letting them survive inside function invocations loses because
1949 we don't want the functions to see them as part of the text. */
1950
1951 char *in, *out, *ref;
1952
1953 /* IN points to where in the line we are scanning.
1954 OUT points to where in the line we are writing.
1955 When we collapse a backslash-newline combination,
1956 IN gets ahead of OUT. */
1957
1958 in = out = cmds->command_lines[i];
1959 while ((ref = strchr (in, '$')) != 0)
1960 {
1961 ++ref; /* Move past the $. */
1962
1963 if (out != in)
1964 /* Copy the text between the end of the last chunk
1965 we processed (where IN points) and the new chunk
1966 we are about to process (where REF points). */
1967 memmove (out, in, ref - in);
1968
1969 /* Move both pointers past the boring stuff. */
1970 out += ref - in;
1971 in = ref;
1972
1973 if (*ref == '(' || *ref == '{')
1974 {
1975 char openparen = *ref;
1976 char closeparen = openparen == '(' ? ')' : '}';
1977 char *outref;
1978 int count;
1979 char *p;
1980
1981 *out++ = *in++; /* Copy OPENPAREN. */
1982 outref = out;
1983 /* IN now points past the opening paren or brace.
1984 Count parens or braces until it is matched. */
1985 count = 0;
1986 while (*in != '\0')
1987 {
1988 if (*in == closeparen && --count < 0)
1989 break;
1990 else if (*in == '\\' && in[1] == '\n')
1991 {
1992 /* We have found a backslash-newline inside a
1993 variable or function reference. Eat it and
1994 any following whitespace. */
1995
1996 int quoted = 0;
1997 for (p = in - 1; p > ref && *p == '\\'; --p)
1998 quoted = !quoted;
1999
2000 if (quoted)
2001 /* There were two or more backslashes, so this is
2002 not really a continuation line. We don't collapse
2003 the quoting backslashes here as is done in
2004 collapse_continuations, because the line will
2005 be collapsed again after expansion. */
2006 *out++ = *in++;
2007 else
2008 {
2009 /* Skip the backslash, newline, and whitespace. */
2010 in += 2;
2011 NEXT_TOKEN (in);
2012
2013 /* Discard any preceding whitespace that has
2014 already been written to the output. */
2015 while (out > outref && ISBLANK (out[-1]))
2016 --out;
2017
2018 /* Replace it all with a single space. */
2019 *out++ = ' ';
2020 }
2021 }
2022 else
2023 {
2024 if (*in == openparen)
2025 ++count;
2026
2027 *out++ = *in++;
2028 }
2029 }
2030 }
2031 }
2032
2033 /* There are no more references in this line to worry about.
2034 Copy the remaining uninteresting text to the output. */
2035 if (out != in)
2036 memmove (out, in, strlen (in) + 1);
2037
2038 /* Finally, expand the line. */
2039 cmds->fileinfo.offset = i;
2040 lines[i] = allocated_variable_expand_for_file (cmds->command_lines[i],
2041 file);
2042 }
2043
2044 cmds->fileinfo.offset = 0;
2045 c->command_lines = lines;
2046#ifdef CONFIG_WITH_PRINT_TIME_SWITCH
2047 c->start_ts = -1;
2048#endif
2049
2050 /* Fetch the first command line to be run. */
2051 job_next_command (c);
2052
2053 /* Wait for a job slot to be freed up. If we allow an infinite number
2054 don't bother; also job_slots will == 0 if we're using the jobserver. */
2055
2056 if (job_slots != 0)
2057 while (job_slots_used == job_slots)
2058 reap_children (1, 0);
2059
2060#ifdef MAKE_JOBSERVER
2061 /* If we are controlling multiple jobs make sure we have a token before
2062 starting the child. */
2063
2064 /* This can be inefficient. There's a decent chance that this job won't
2065 actually have to run any subprocesses: the command script may be empty
2066 or otherwise optimized away. It would be nice if we could defer
2067 obtaining a token until just before we need it, in start_job_command.
2068 To do that we'd need to keep track of whether we'd already obtained a
2069 token (since start_job_command is called for each line of the job, not
2070 just once). Also more thought needs to go into the entire algorithm;
2071 this is where the old parallel job code waits, so... */
2072
2073 else if (jobserver_enabled ())
2074 while (1)
2075 {
2076 int got_token;
2077
2078 DB (DB_JOBS, ("Need a job token; we %shave children\n",
2079 children ? "" : "don't "));
2080
2081 /* If we don't already have a job started, use our "free" token. */
2082 if (!jobserver_tokens)
2083 break;
2084
2085 /* Prepare for jobserver token acquisition. */
2086 jobserver_pre_acquire ();
2087
2088 /* Reap anything that's currently waiting. */
2089 reap_children (0, 0);
2090
2091 /* Kick off any jobs we have waiting for an opportunity that
2092 can run now (i.e., waiting for load). */
2093 start_waiting_jobs ();
2094
2095 /* If our "free" slot is available, use it; we don't need a token. */
2096 if (!jobserver_tokens)
2097 break;
2098
2099 /* There must be at least one child already, or we have no business
2100 waiting for a token. */
2101 if (!children)
2102 O (fatal, NILF, "INTERNAL: no children as we go to sleep on read\n");
2103
2104 /* Get a token. */
2105 got_token = jobserver_acquire (waiting_jobs != NULL);
2106
2107 /* If we got one, we're done here. */
2108 if (got_token == 1)
2109 {
2110 DB (DB_JOBS, (_("Obtained token for child %p (%s).\n"),
2111 (void *)c, c->file->name));
2112 break;
2113 }
2114 }
2115#endif
2116
2117 ++jobserver_tokens;
2118
2119 /* Trace the build.
2120 Use message here so that changes to working directories are logged. */
2121 if (trace_flag)
2122 {
2123 char *newer = allocated_variable_expand_for_file ("$?", c->file);
2124 const char *nm;
2125
2126 if (! cmds->fileinfo.filenm)
2127 nm = _("<builtin>");
2128 else
2129 {
2130 char *n = alloca (strlen (cmds->fileinfo.filenm) + 1 + 11 + 1);
2131 sprintf (n, "%s:%lu", cmds->fileinfo.filenm, cmds->fileinfo.lineno);
2132 nm = n;
2133 }
2134
2135 if (newer[0] == '\0')
2136 OSS (message, 0,
2137 _("%s: target '%s' does not exist"), nm, c->file->name);
2138 else
2139 OSSS (message, 0,
2140 _("%s: update target '%s' due to: %s"), nm, c->file->name, newer);
2141
2142 free (newer);
2143 }
2144
2145 /* The job is now primed. Start it running.
2146 (This will notice if there is in fact no recipe.) */
2147 start_waiting_job (c);
2148
2149#ifndef CONFIG_WITH_EXTENDED_NOTPARALLEL
2150 if (job_slots == 1 || not_parallel)
2151 /* Since there is only one job slot, make things run linearly.
2152 Wait for the child to die, setting the state to 'cs_finished'. */
2153 while (file->command_state == cs_running)
2154 reap_children (1, 0);
2155
2156#else /* CONFIG_WITH_EXTENDED_NOTPARALLEL */
2157
2158 if (job_slots == 1 || not_parallel < 0)
2159 {
2160 /* Since there is only one job slot, make things run linearly.
2161 Wait for the child to die, setting the state to `cs_finished'. */
2162 while (file->command_state == cs_running)
2163 reap_children (1, 0);
2164 }
2165 else if (not_parallel > 0)
2166 {
2167 /* wait for all live children to finish and then continue
2168 with the not-parallel child(s). FIXME: this loop could be better? */
2169 while (file->command_state == cs_running
2170 && (children != 0 || shell_function_pid != 0) /* reap_child condition */
2171 && not_parallel > 0)
2172 reap_children (1, 0);
2173 }
2174#endif /* CONFIG_WITH_EXTENDED_NOTPARALLEL */
2175
2176 OUTPUT_UNSET ();
2177 return;
2178}
2179
2180
2181/* Move CHILD's pointers to the next command for it to execute.
2182 Returns nonzero if there is another command. */
2183
2184static int
2185job_next_command (struct child *child)
2186{
2187 while (child->command_ptr == 0 || *child->command_ptr == '\0')
2188 {
2189 /* There are no more lines in the expansion of this line. */
2190 if (child->command_line == child->file->cmds->ncommand_lines)
2191 {
2192 /* There are no more lines to be expanded. */
2193 child->command_ptr = 0;
2194 child->file->cmds->fileinfo.offset = 0;
2195 return 0;
2196 }
2197 else
2198 /* Get the next line to run. */
2199 child->command_ptr = child->command_lines[child->command_line++];
2200 }
2201
2202 child->file->cmds->fileinfo.offset = child->command_line - 1;
2203 return 1;
2204}
2205
2206/* Determine if the load average on the system is too high to start a new job.
2207 The real system load average is only recomputed once a second. However, a
2208 very parallel make can easily start tens or even hundreds of jobs in a
2209 second, which brings the system to its knees for a while until that first
2210 batch of jobs clears out.
2211
2212 To avoid this we use a weighted algorithm to try to account for jobs which
2213 have been started since the last second, and guess what the load average
2214 would be now if it were computed.
2215
2216 This algorithm was provided by Thomas Riedl <[email protected]>,
2217 who writes:
2218
2219! calculate something load-oid and add to the observed sys.load,
2220! so that latter can catch up:
2221! - every job started increases jobctr;
2222! - every dying job decreases a positive jobctr;
2223! - the jobctr value gets zeroed every change of seconds,
2224! after its value*weight_b is stored into the 'backlog' value last_sec
2225! - weight_a times the sum of jobctr and last_sec gets
2226! added to the observed sys.load.
2227!
2228! The two weights have been tried out on 24 and 48 proc. Sun Solaris-9
2229! machines, using a several-thousand-jobs-mix of cpp, cc, cxx and smallish
2230! sub-shelled commands (rm, echo, sed...) for tests.
2231! lowering the 'direct influence' factor weight_a (e.g. to 0.1)
2232! resulted in significant excession of the load limit, raising it
2233! (e.g. to 0.5) took bad to small, fast-executing jobs and didn't
2234! reach the limit in most test cases.
2235!
2236! lowering the 'history influence' weight_b (e.g. to 0.1) resulted in
2237! exceeding the limit for longer-running stuff (compile jobs in
2238! the .5 to 1.5 sec. range),raising it (e.g. to 0.5) overrepresented
2239! small jobs' effects.
2240
2241 */
2242
2243#define LOAD_WEIGHT_A 0.25
2244#define LOAD_WEIGHT_B 0.25
2245
2246static int
2247load_too_high (void)
2248{
2249#if defined(__MSDOS__) || defined(VMS) || defined(_AMIGA) || defined(__riscos__) || defined(__HAIKU__)
2250 return 1;
2251#else
2252 static double last_sec;
2253 static time_t last_now;
2254 double load, guess;
2255 time_t now;
2256
2257#if defined(WINDOWS32) && !defined(CONFIG_NEW_WIN_CHILDREN)
2258 /* sub_proc.c cannot wait for more than MAXIMUM_WAIT_OBJECTS children */
2259 if (process_used_slots () >= MAXIMUM_WAIT_OBJECTS)
2260 return 1;
2261#endif
2262
2263 if (max_load_average < 0)
2264 return 0;
2265
2266 /* Find the real system load average. */
2267 make_access ();
2268 if (getloadavg (&load, 1) != 1)
2269 {
2270 static int lossage = -1;
2271 /* Complain only once for the same error. */
2272 if (lossage == -1 || errno != lossage)
2273 {
2274 if (errno == 0)
2275 /* An errno value of zero means getloadavg is just unsupported. */
2276 O (error, NILF,
2277 _("cannot enforce load limits on this operating system"));
2278 else
2279 perror_with_name (_("cannot enforce load limit: "), "getloadavg");
2280 }
2281 lossage = errno;
2282 load = 0;
2283 }
2284 user_access ();
2285
2286 /* If we're in a new second zero the counter and correct the backlog
2287 value. Only keep the backlog for one extra second; after that it's 0. */
2288 now = time (NULL);
2289 if (last_now < now)
2290 {
2291 if (last_now == now - 1)
2292 last_sec = LOAD_WEIGHT_B * job_counter;
2293 else
2294 last_sec = 0.0;
2295
2296 job_counter = 0;
2297 last_now = now;
2298 }
2299
2300 /* Try to guess what the load would be right now. */
2301 guess = load + (LOAD_WEIGHT_A * (job_counter + last_sec));
2302
2303 DB (DB_JOBS, ("Estimated system load = %f (actual = %f) (max requested = %f)\n",
2304 guess, load, max_load_average));
2305
2306 return guess >= max_load_average;
2307#endif
2308}
2309
2310/* Start jobs that are waiting for the load to be lower. */
2311
2312void
2313start_waiting_jobs (void)
2314{
2315 struct child *job;
2316
2317 if (waiting_jobs == 0)
2318 return;
2319
2320 do
2321 {
2322 /* Check for recently deceased descendants. */
2323 reap_children (0, 0);
2324
2325 /* Take a job off the waiting list. */
2326 job = waiting_jobs;
2327 waiting_jobs = job->next;
2328
2329#ifdef CONFIG_WITH_EXTENDED_NOTPARALLEL
2330 /* If it's a not-parallel job, we've already counted it once
2331 when it was queued in start_waiting_job, so decrement
2332 before sending it to start_waiting_job again. */
2333 if (job->file->command_flags & COMMANDS_NOTPARALLEL)
2334 {
2335 DB (DB_KMK, (_("not_parallel %d -> %d (file=%p `%s') [start_waiting_jobs]\n"),
2336 not_parallel, not_parallel - 1, (void *) job->file, job->file->name));
2337 assert(not_parallel > 0);
2338 --not_parallel;
2339 }
2340#endif /* CONFIG_WITH_EXTENDED_NOTPARALLEL */
2341
2342 /* Try to start that job. We break out of the loop as soon
2343 as start_waiting_job puts one back on the waiting list. */
2344 }
2345 while (start_waiting_job (job) && waiting_jobs != 0);
2346
2347 return;
2348}
2349
2350
2351#ifndef WINDOWS32
2352
2353/* EMX: Start a child process. This function returns the new pid. */
2354# if defined __EMX__
2355int
2356child_execute_job (struct output *out, int good_stdin, char **argv, char **envp)
2357{
2358 int pid;
2359 int fdin = good_stdin ? FD_STDIN : get_bad_stdin ();
2360 int fdout = FD_STDOUT;
2361 int fderr = FD_STDERR;
2362 int save_fdin = -1;
2363 int save_fdout = -1;
2364 int save_fderr = -1;
2365
2366 /* Divert child output if we want to capture output. */
2367 if (out && out->syncout)
2368 {
2369 if (out->out >= 0)
2370 fdout = out->out;
2371 if (out->err >= 0)
2372 fderr = out->err;
2373 }
2374
2375 /* For each FD which needs to be redirected first make a dup of the standard
2376 FD to save and mark it close on exec so our child won't see it. Then
2377 dup2() the standard FD to the redirect FD, and also mark the redirect FD
2378 as close on exec. */
2379 if (fdin != FD_STDIN)
2380 {
2381 save_fdin = dup (FD_STDIN);
2382 if (save_fdin < 0)
2383 O (fatal, NILF, _("no more file handles: could not duplicate stdin\n"));
2384 CLOSE_ON_EXEC (save_fdin);
2385
2386 dup2 (fdin, FD_STDIN);
2387 CLOSE_ON_EXEC (fdin);
2388 }
2389
2390 if (fdout != FD_STDOUT)
2391 {
2392 save_fdout = dup (FD_STDOUT);
2393 if (save_fdout < 0)
2394 O (fatal, NILF,
2395 _("no more file handles: could not duplicate stdout\n"));
2396 CLOSE_ON_EXEC (save_fdout);
2397
2398 dup2 (fdout, FD_STDOUT);
2399 CLOSE_ON_EXEC (fdout);
2400 }
2401
2402 if (fderr != FD_STDERR)
2403 {
2404 if (fderr != fdout)
2405 {
2406 save_fderr = dup (FD_STDERR);
2407 if (save_fderr < 0)
2408 O (fatal, NILF,
2409 _("no more file handles: could not duplicate stderr\n"));
2410 CLOSE_ON_EXEC (save_fderr);
2411 }
2412
2413 dup2 (fderr, FD_STDERR);
2414 CLOSE_ON_EXEC (fderr);
2415 }
2416
2417 /* Run the command. */
2418 pid = exec_command (argv, envp);
2419
2420 /* Restore stdout/stdin/stderr of the parent and close temporary FDs. */
2421 if (save_fdin >= 0)
2422 {
2423 if (dup2 (save_fdin, FD_STDIN) != FD_STDIN)
2424 O (fatal, NILF, _("Could not restore stdin\n"));
2425 else
2426 close (save_fdin);
2427 }
2428
2429 if (save_fdout >= 0)
2430 {
2431 if (dup2 (save_fdout, FD_STDOUT) != FD_STDOUT)
2432 O (fatal, NILF, _("Could not restore stdout\n"));
2433 else
2434 close (save_fdout);
2435 }
2436
2437 if (save_fderr >= 0)
2438 {
2439 if (dup2 (save_fderr, FD_STDERR) != FD_STDERR)
2440 O (fatal, NILF, _("Could not restore stderr\n"));
2441 else
2442 close (save_fderr);
2443 }
2444
2445 return pid;
2446}
2447
2448#elif !defined (_AMIGA) && !defined (__MSDOS__) && !defined (VMS)
2449
2450/* POSIX:
2451 Create a child process executing the command in ARGV.
2452 ENVP is the environment of the new program. Returns the PID or -1. */
2453int
2454child_execute_job (struct output *out, int good_stdin, char **argv, char **envp)
2455{
2456 int r;
2457 int pid;
2458 int fdin = good_stdin ? FD_STDIN : get_bad_stdin ();
2459 int fdout = FD_STDOUT;
2460 int fderr = FD_STDERR;
2461
2462 /* Divert child output if we want to capture it. */
2463 if (out && out->syncout)
2464 {
2465 if (out->out >= 0)
2466 fdout = out->out;
2467 if (out->err >= 0)
2468 fderr = out->err;
2469 }
2470
2471 pid = vfork();
2472 if (pid != 0)
2473 return pid;
2474
2475 /* We are the child. */
2476 unblock_sigs ();
2477
2478#ifdef SET_STACK_SIZE
2479 /* Reset limits, if necessary. */
2480 if (stack_limit.rlim_cur)
2481 setrlimit (RLIMIT_STACK, &stack_limit);
2482#endif
2483
2484 /* For any redirected FD, dup2() it to the standard FD.
2485 They are all marked close-on-exec already. */
2486 if (fdin != FD_STDIN)
2487 EINTRLOOP (r, dup2 (fdin, FD_STDIN));
2488 if (fdout != FD_STDOUT)
2489 EINTRLOOP (r, dup2 (fdout, FD_STDOUT));
2490 if (fderr != FD_STDERR)
2491 EINTRLOOP (r, dup2 (fderr, FD_STDERR));
2492
2493 /* Run the command. */
2494 exec_command (argv, envp);
2495}
2496#endif /* !AMIGA && !__MSDOS__ && !VMS */
2497#endif /* !WINDOWS32 */
2498
2499
2500#if !defined(WINDOWS32) || !defined(CONFIG_NEW_WIN_CHILDREN)
2501#ifndef _AMIGA
2502/* Replace the current process with one running the command in ARGV,
2503 with environment ENVP. This function does not return. */
2504
2505/* EMX: This function returns the pid of the child process. */
2506# ifdef __EMX__
2507int
2508# else
2509void
2510# endif
2511exec_command (char **argv, char **envp)
2512{
2513#ifdef VMS
2514 /* to work around a problem with signals and execve: ignore them */
2515#ifdef SIGCHLD
2516 signal (SIGCHLD,SIG_IGN);
2517#endif
2518 /* Run the program. */
2519 execve (argv[0], argv, envp);
2520 perror_with_name ("execve: ", argv[0]);
2521 _exit (EXIT_FAILURE);
2522#else
2523#ifdef WINDOWS32
2524# ifndef CONFIG_NEW_WIN_CHILDREN
2525 HANDLE hPID;
2526 HANDLE hWaitPID;
2527 int exit_code = EXIT_FAILURE;
2528
2529 /* make sure CreateProcess() has Path it needs */
2530 sync_Path_environment ();
2531
2532 /* launch command */
2533 hPID = process_easy (argv, envp, -1, -1);
2534
2535 /* make sure launch ok */
2536 if (hPID == INVALID_HANDLE_VALUE)
2537 {
2538 int i;
2539 fprintf (stderr, _("process_easy() failed to launch process (e=%ld)\n"),
2540 process_last_err (hPID));
2541 for (i = 0; argv[i]; i++)
2542 fprintf (stderr, "%s ", argv[i]);
2543 fprintf (stderr, _("\nCounted %d args in failed launch\n"), i);
2544 exit (EXIT_FAILURE);
2545 }
2546
2547 /* wait and reap last child */
2548 hWaitPID = process_wait_for_any (1, 0);
2549 while (hWaitPID)
2550 {
2551 /* was an error found on this process? */
2552 int err = process_last_err (hWaitPID);
2553
2554 /* get exit data */
2555 exit_code = process_exit_code (hWaitPID);
2556
2557 if (err)
2558 fprintf (stderr, "make (e=%d, rc=%d): %s",
2559 err, exit_code, map_windows32_error_to_string (err));
2560
2561 /* cleanup process */
2562 process_cleanup (hWaitPID);
2563
2564 /* expect to find only last pid, warn about other pids reaped */
2565 if (hWaitPID == hPID)
2566 break;
2567 else
2568 {
2569 char *pidstr = xstrdup (pid2str ((pid_t)hWaitPID));
2570
2571 fprintf (stderr,
2572 _("make reaped child pid %s, still waiting for pid %s\n"),
2573 pidstr, pid2str ((pid_t)hPID));
2574 free (pidstr);
2575 }
2576 }
2577
2578 /* return child's exit code as our exit code */
2579 exit (exit_code);
2580# else /* CONFIG_NEW_WIN_CHILDREN */
2581
2582# endif /* CONFIG_NEW_WIN_CHILDREN */
2583#else /* !WINDOWS32 */
2584
2585# ifdef __EMX__
2586 int pid;
2587# endif
2588
2589 /* Be the user, permanently. */
2590 child_access ();
2591
2592# ifdef __EMX__
2593 /* Run the program. */
2594 pid = spawnvpe (P_NOWAIT, argv[0], argv, envp);
2595 if (pid >= 0)
2596 return pid;
2597
2598 /* the file might have a strange shell extension */
2599 if (errno == ENOENT)
2600 errno = ENOEXEC;
2601
2602# else
2603 /* Run the program. */
2604 environ = envp;
2605 execvp (argv[0], argv);
2606
2607# endif /* !__EMX__ */
2608
2609 switch (errno)
2610 {
2611 case ENOENT:
2612 /* We are in the child: don't use the output buffer.
2613 It's not right to run fprintf() here! */
2614 if (makelevel == 0)
2615 fprintf (stderr, _("%s: %s: Command not found\n"), program, argv[0]);
2616 else
2617 fprintf (stderr, _("%s[%u]: %s: Command not found\n"),
2618 program, makelevel, argv[0]);
2619 break;
2620 case ENOEXEC:
2621 {
2622 /* The file is not executable. Try it as a shell script. */
2623 const char *shell;
2624 char **new_argv;
2625 int argc;
2626 int i=1;
2627
2628# ifdef __EMX__
2629 /* Do not use $SHELL from the environment */
2630 struct variable *p = lookup_variable ("SHELL", 5);
2631 if (p)
2632 shell = p->value;
2633 else
2634 shell = 0;
2635# else
2636 shell = getenv ("SHELL");
2637# endif
2638 if (shell == 0)
2639 shell = default_shell;
2640
2641 argc = 1;
2642 while (argv[argc] != 0)
2643 ++argc;
2644
2645# ifdef __EMX__
2646 if (!unixy_shell)
2647 ++argc;
2648# endif
2649
2650 new_argv = alloca ((1 + argc + 1) * sizeof (char *));
2651 new_argv[0] = (char *)shell;
2652
2653# ifdef __EMX__
2654 if (!unixy_shell)
2655 {
2656 new_argv[1] = "/c";
2657 ++i;
2658 --argc;
2659 }
2660# endif
2661
2662 new_argv[i] = argv[0];
2663 while (argc > 0)
2664 {
2665 new_argv[i + argc] = argv[argc];
2666 --argc;
2667 }
2668
2669# ifdef __EMX__
2670 pid = spawnvpe (P_NOWAIT, shell, new_argv, envp);
2671 if (pid >= 0)
2672 break;
2673# else
2674 execvp (shell, new_argv);
2675# endif
2676 if (errno == ENOENT)
2677 OS (error, NILF, _("%s: Shell program not found"), shell);
2678 else
2679 perror_with_name ("execvp: ", shell);
2680 break;
2681 }
2682
2683# ifdef __EMX__
2684 case EINVAL:
2685 /* this nasty error was driving me nuts :-( */
2686 O (error, NILF, _("spawnvpe: environment space might be exhausted"));
2687 /* FALLTHROUGH */
2688# endif
2689
2690 default:
2691 perror_with_name ("execvp: ", argv[0]);
2692 break;
2693 }
2694
2695# ifdef __EMX__
2696 return pid;
2697# else
2698 _exit (127);
2699# endif
2700#endif /* !WINDOWS32 */
2701#endif /* !VMS */
2702}
2703#else /* On Amiga */
2704void
2705exec_command (char **argv)
2706{
2707 MyExecute (argv);
2708}
2709
2710void clean_tmp (void)
2711{
2712 DeleteFile (amiga_bname);
2713}
2714
2715#endif /* On Amiga */
2716#endif /* !defined(WINDOWS32) || !defined(CONFIG_NEW_WIN_CHILDREN) */
2717
2718
2719#ifndef VMS
2720/* Figure out the argument list necessary to run LINE as a command. Try to
2721 avoid using a shell. This routine handles only ' quoting, and " quoting
2722 when no backslash, $ or ' characters are seen in the quotes. Starting
2723 quotes may be escaped with a backslash. If any of the characters in
2724 sh_chars is seen, or any of the builtin commands listed in sh_cmds
2725 is the first word of a line, the shell is used.
2726
2727 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
2728 If *RESTP is NULL, newlines will be ignored.
2729
2730 SHELL is the shell to use, or nil to use the default shell.
2731 IFS is the value of $IFS, or nil (meaning the default).
2732
2733 FLAGS is the value of lines_flags for this command line. It is
2734 used in the WINDOWS32 port to check whether + or $(MAKE) were found
2735 in this command line, in which case the effect of just_print_flag
2736 is overridden. */
2737
2738static char **
2739construct_command_argv_internal (char *line, char **restp, const char *shell,
2740 const char *shellflags, const char *ifs,
2741 int flags, char **batch_filename UNUSED)
2742{
2743#ifdef __MSDOS__
2744 /* MSDOS supports both the stock DOS shell and ports of Unixy shells.
2745 We call 'system' for anything that requires ''slow'' processing,
2746 because DOS shells are too dumb. When $SHELL points to a real
2747 (unix-style) shell, 'system' just calls it to do everything. When
2748 $SHELL points to a DOS shell, 'system' does most of the work
2749 internally, calling the shell only for its internal commands.
2750 However, it looks on the $PATH first, so you can e.g. have an
2751 external command named 'mkdir'.
2752
2753 Since we call 'system', certain characters and commands below are
2754 actually not specific to COMMAND.COM, but to the DJGPP implementation
2755 of 'system'. In particular:
2756
2757 The shell wildcard characters are in DOS_CHARS because they will
2758 not be expanded if we call the child via 'spawnXX'.
2759
2760 The ';' is in DOS_CHARS, because our 'system' knows how to run
2761 multiple commands on a single line.
2762
2763 DOS_CHARS also include characters special to 4DOS/NDOS, so we
2764 won't have to tell one from another and have one more set of
2765 commands and special characters. */
2766 static const char *sh_chars_dos = "*?[];|<>%^&()";
2767 static const char *sh_cmds_dos[] =
2768 { "break", "call", "cd", "chcp", "chdir", "cls", "copy", "ctty", "date",
2769 "del", "dir", "echo", "erase", "exit", "for", "goto", "if", "md",
2770 "mkdir", "path", "pause", "prompt", "rd", "rmdir", "rem", "ren",
2771 "rename", "set", "shift", "time", "type", "ver", "verify", "vol", ":",
2772 0 };
2773
2774 static const char *sh_chars_sh = "#;\"*?[]&|<>(){}$`^";
2775 static const char *sh_cmds_sh[] =
2776 { "cd", "echo", "eval", "exec", "exit", "login", "logout", "set", "umask",
2777 "wait", "while", "for", "case", "if", ":", ".", "break", "continue",
2778 "export", "read", "readonly", "shift", "times", "trap", "switch",
2779 "unset", "ulimit", 0 };
2780
2781 const char *sh_chars;
2782 const char **sh_cmds;
2783
2784#elif defined (__EMX__)
2785 static const char *sh_chars_dos = "*?[];|<>%^&()";
2786 static const char *sh_cmds_dos[] =
2787 { "break", "call", "cd", "chcp", "chdir", "cls", "copy", "ctty", "date",
2788 "del", "dir", "echo", "erase", "exit", "for", "goto", "if", "md",
2789 "mkdir", "path", "pause", "prompt", "rd", "rmdir", "rem", "ren",
2790 "rename", "set", "shift", "time", "type", "ver", "verify", "vol", ":",
2791 0 };
2792
2793 static const char *sh_chars_os2 = "*?[];|<>%^()\"'&";
2794 static const char *sh_cmds_os2[] =
2795 { "call", "cd", "chcp", "chdir", "cls", "copy", "date", "del", "detach",
2796 "dir", "echo", "endlocal", "erase", "exit", "for", "goto", "if", "keys",
2797 "md", "mkdir", "move", "path", "pause", "prompt", "rd", "rem", "ren",
2798 "rename", "rmdir", "set", "setlocal", "shift", "start", "time", "type",
2799 "ver", "verify", "vol", ":", 0 };
2800
2801 static const char *sh_chars_sh = "#;\"*?[]&|<>(){}$`^~'";
2802 static const char *sh_cmds_sh[] =
2803 { "echo", "cd", "eval", "exec", "exit", "login", "logout", "set", "umask",
2804 "wait", "while", "for", "case", "if", ":", ".", "break", "continue",
2805 "export", "read", "readonly", "shift", "times", "trap", "switch",
2806 "unset", 0 };
2807
2808 const char *sh_chars;
2809 const char **sh_cmds;
2810
2811#elif defined (_AMIGA)
2812 static const char *sh_chars = "#;\"|<>()?*$`";
2813 static const char *sh_cmds[] =
2814 { "cd", "eval", "if", "delete", "echo", "copy", "rename", "set", "setenv",
2815 "date", "makedir", "skip", "else", "endif", "path", "prompt", "unset",
2816 "unsetenv", "version", 0 };
2817
2818#elif defined (WINDOWS32)
2819 /* We used to have a double quote (") in sh_chars_dos[] below, but
2820 that caused any command line with quoted file names be run
2821 through a temporary batch file, which introduces command-line
2822 limit of 4K charcaters imposed by cmd.exe. Since CreateProcess
2823 can handle quoted file names just fine, removing the quote lifts
2824 the limit from a very frequent use case, because using quoted
2825 file names is commonplace on MS-Windows. */
2826 static const char *sh_chars_dos = "|&<>";
2827 static const char *sh_cmds_dos[] =
2828 { "assoc", "break", "call", "cd", "chcp", "chdir", "cls", "color", "copy",
2829 "ctty", "date", "del", "dir", "echo", "echo.", "endlocal", "erase",
2830 "exit", "for", "ftype", "goto", "if", "if", "md", "mkdir", "move",
2831 "path", "pause", "prompt", "rd", "rem", "ren", "rename", "rmdir",
2832 "set", "setlocal", "shift", "time", "title", "type", "ver", "verify",
2833 "vol", ":", 0 };
2834
2835 static const char *sh_chars_sh = "#;\"*?[]&|<>(){}$`^";
2836 static const char *sh_cmds_sh[] =
2837 { "cd", "eval", "exec", "exit", "login", "logout", "set", "umask", "wait",
2838 "while", "for", "case", "if", ":", ".", "break", "continue", "export",
2839 "read", "readonly", "shift", "times", "trap", "switch", "test",
2840#ifdef BATCH_MODE_ONLY_SHELL
2841 "echo",
2842#endif
2843 0 };
2844
2845 const char *sh_chars;
2846 char const * const * sh_cmds; /* kmk: +_sh +const*2 */
2847#elif defined(__riscos__)
2848 static const char *sh_chars = "";
2849 static const char *sh_cmds[] = { 0 };
2850#else /* must be UNIX-ish */
2851 static const char *sh_chars_sh = "#;\"*?[]&|<>(){}$`^~!"; /* kmk: +_sh */
2852 static const char *sh_cmds_sh[] = /* kmk: +_sh */
2853 { ".", ":", "break", "case", "cd", "continue", "eval", "exec", "exit",
2854 "export", "for", "if", "login", "logout", "read", "readonly", "set",
2855 "shift", "switch", "test", "times", "trap", "ulimit", "umask", "unset",
2856 "wait", "while", 0 };
2857
2858# if 0 /*def HAVE_DOS_PATHS - kmk */
2859 /* This is required if the MSYS/Cygwin ports (which do not define
2860 WINDOWS32) are compiled with HAVE_DOS_PATHS defined, which uses
2861 sh_chars_sh directly (see below). The value must be identical
2862 to that of sh_chars immediately above. */
2863 static const char *sh_chars_sh = "#;\"*?[]&|<>(){}$`^~!";
2864# endif /* HAVE_DOS_PATHS */
2865 char const * sh_chars = sh_chars_sh; /* kmk: +_sh +const */
2866 char const * const * sh_cmds = sh_cmds_sh; /* kmk: +_sh +const*2 */
2867#endif
2868#ifdef KMK
2869 static const char sh_chars_kash[] = "#;*?[]&|<>(){}$`^~!"; /* note: no \" - good idea? */
2870 static const char * const sh_cmds_kash[] = {
2871 ".", ":", "break", "case", "cd", "continue", "echo", "eval", "exec", "exit",
2872 "export", "for", "if", "login", "logout", "read", "readonly", "set",
2873 "shift", "switch", "test", "times", "trap", "umask", "wait", "while", 0 /* +echo, -ulimit, -unset */
2874 };
2875 int is_kmk_shell = 0;
2876#endif
2877 int i;
2878 char *p;
2879#ifndef NDEBUG
2880 char *end;
2881#endif
2882 char *ap;
2883 const char *cap;
2884 const char *cp;
2885 int instring, word_has_equals, seen_nonequals, last_argument_was_empty;
2886 char **new_argv = 0;
2887 char *argstr = 0;
2888#ifdef WINDOWS32
2889 int slow_flag = 0;
2890
2891 if (!unixy_shell)
2892 {
2893 sh_cmds = sh_cmds_dos;
2894 sh_chars = sh_chars_dos;
2895 }
2896 else
2897 {
2898 sh_cmds = sh_cmds_sh;
2899 sh_chars = sh_chars_sh;
2900 }
2901#endif /* WINDOWS32 */
2902
2903 if (restp != NULL)
2904 *restp = NULL;
2905
2906 /* Make sure not to bother processing an empty line but stop at newline. */
2907 while (ISBLANK (*line))
2908 ++line;
2909 if (*line == '\0')
2910 return 0;
2911
2912 if (shellflags == 0)
2913 shellflags = posix_pedantic ? "-ec" : "-c";
2914
2915 /* See if it is safe to parse commands internally. */
2916#ifdef KMK /* kmk_ash and kmk_kash are both fine, kmk_ash is the default btw. */
2917 if (shell == 0)
2918 {
2919 is_kmk_shell = 1;
2920 shell = (char *)get_default_kbuild_shell ();
2921 }
2922 else if (!strcmp (shell, get_default_kbuild_shell()))
2923 is_kmk_shell = 1;
2924 else
2925 {
2926 const char *psz = strstr (shell, "/kmk_ash");
2927 if (psz)
2928 psz += sizeof ("/kmk_ash") - 1;
2929 else
2930 {
2931 psz = strstr (shell, "/kmk_kash");
2932 if (psz)
2933 psz += sizeof ("/kmk_kash") - 1;
2934 }
2935# if defined (__OS2__) || defined (_WIN32) || defined (WINDOWS32)
2936 is_kmk_shell = psz && (*psz == '\0' || !stricmp (psz, ".exe"));
2937# else
2938 is_kmk_shell = psz && *psz == '\0';
2939# endif
2940 }
2941 if (is_kmk_shell)
2942 {
2943 sh_chars = sh_chars_kash;
2944 sh_cmds = sh_cmds_kash;
2945 }
2946#else /* !KMK */
2947 if (shell == 0)
2948 shell = default_shell;
2949#endif /* !KMK */
2950#ifdef WINDOWS32
2951 else if (strcmp (shell, default_shell))
2952 {
2953 char *s1 = _fullpath (NULL, shell, 0);
2954 char *s2 = _fullpath (NULL, default_shell, 0);
2955
2956 slow_flag = strcmp ((s1 ? s1 : ""), (s2 ? s2 : ""));
2957
2958 free (s1);
2959 free (s2);
2960 }
2961 if (slow_flag)
2962 goto slow;
2963#else /* not WINDOWS32 */
2964#if defined (__MSDOS__) || defined (__EMX__)
2965 else if (strcasecmp (shell, default_shell))
2966 {
2967 extern int _is_unixy_shell (const char *_path);
2968
2969 DB (DB_BASIC, (_("$SHELL changed (was '%s', now '%s')\n"),
2970 default_shell, shell));
2971 unixy_shell = _is_unixy_shell (shell);
2972 /* we must allocate a copy of shell: construct_command_argv() will free
2973 * shell after this function returns. */
2974 default_shell = xstrdup (shell);
2975 }
2976# ifdef KMK
2977 if (is_kmk_shell)
2978 { /* done above already */ }
2979 else
2980# endif
2981 if (unixy_shell)
2982 {
2983 sh_chars = sh_chars_sh;
2984 sh_cmds = sh_cmds_sh;
2985 }
2986 else
2987 {
2988 sh_chars = sh_chars_dos;
2989 sh_cmds = sh_cmds_dos;
2990# ifdef __EMX__
2991 if (_osmode == OS2_MODE)
2992 {
2993 sh_chars = sh_chars_os2;
2994 sh_cmds = sh_cmds_os2;
2995 }
2996# endif
2997 }
2998#else /* !__MSDOS__ */
2999 else if (strcmp (shell, default_shell))
3000 goto slow;
3001#endif /* !__MSDOS__ && !__EMX__ */
3002#endif /* not WINDOWS32 */
3003
3004 if (ifs)
3005 for (cap = ifs; *cap != '\0'; ++cap)
3006 if (*cap != ' ' && *cap != '\t' && *cap != '\n')
3007 goto slow;
3008
3009 if (shellflags)
3010 if (shellflags[0] != '-'
3011 || ((shellflags[1] != 'c' || shellflags[2] != '\0')
3012 && (shellflags[1] != 'e' || shellflags[2] != 'c' || shellflags[3] != '\0')))
3013 goto slow;
3014
3015 i = strlen (line) + 1;
3016
3017 /* More than 1 arg per character is impossible. */
3018 new_argv = xmalloc (i * sizeof (char *));
3019
3020 /* All the args can fit in a buffer as big as LINE is. */
3021 ap = new_argv[0] = argstr = xmalloc (i);
3022#ifndef NDEBUG
3023 end = ap + i;
3024#endif
3025
3026 /* I is how many complete arguments have been found. */
3027 i = 0;
3028 instring = word_has_equals = seen_nonequals = last_argument_was_empty = 0;
3029 for (p = line; *p != '\0'; ++p)
3030 {
3031 assert (ap <= end);
3032
3033 if (instring)
3034 {
3035 /* Inside a string, just copy any char except a closing quote
3036 or a backslash-newline combination. */
3037 if (*p == instring)
3038 {
3039 instring = 0;
3040 if (ap == new_argv[0] || *(ap-1) == '\0')
3041 last_argument_was_empty = 1;
3042 }
3043 else if (*p == '\\' && p[1] == '\n')
3044 {
3045 /* Backslash-newline is handled differently depending on what
3046 kind of string we're in: inside single-quoted strings you
3047 keep them; in double-quoted strings they disappear. For
3048 DOS/Windows/OS2, if we don't have a POSIX shell, we keep the
3049 pre-POSIX behavior of removing the backslash-newline. */
3050 if (instring == '"'
3051#if defined (__MSDOS__) || defined (__EMX__) || defined (WINDOWS32)
3052 || !unixy_shell
3053#endif
3054 )
3055 ++p;
3056 else
3057 {
3058 *(ap++) = *(p++);
3059 *(ap++) = *p;
3060 }
3061 }
3062 else if (*p == '\n' && restp != NULL)
3063 {
3064 /* End of the command line. */
3065 *restp = p;
3066 goto end_of_line;
3067 }
3068 /* Backslash, $, and ` are special inside double quotes.
3069 If we see any of those, punt.
3070 But on MSDOS, if we use COMMAND.COM, double and single
3071 quotes have the same effect. */
3072 else if (instring == '"' && strchr ("\\$`", *p) != 0 && unixy_shell)
3073 goto slow;
3074#ifdef WINDOWS32
3075 /* Quoted wildcard characters must be passed quoted to the
3076 command, so give up the fast route. */
3077 else if (instring == '"' && strchr ("*?", *p) != 0 && !unixy_shell)
3078 goto slow;
3079 else if (instring == '"' && strncmp (p, "\\\"", 2) == 0)
3080 *ap++ = *++p;
3081#endif
3082 else
3083 *ap++ = *p;
3084 }
3085 else if (strchr (sh_chars, *p) != 0)
3086#ifdef KMK
3087 {
3088 /* Tilde is only special if at the start of a path spec,
3089 i.e. don't get excited when we by 8.3 files on windows. */
3090 if ( *p == '~'
3091 && p > line
3092 && !ISSPACE (p[-1])
3093 && p[-1] != '='
3094 && p[-1] != ':'
3095 && p[-1] != '"'
3096 && p[-1] != '\'')
3097 *ap++ = *p;
3098 else
3099 /* Not inside a string, but it's a special char. */
3100 goto slow;
3101 }
3102#else /* !KMK */
3103 /* Not inside a string, but it's a special char. */
3104 goto slow;
3105#endif /* !KMK */
3106 else if (one_shell && *p == '\n')
3107 /* In .ONESHELL mode \n is a separator like ; or && */
3108 goto slow;
3109#ifdef __MSDOS__
3110 else if (*p == '.' && p[1] == '.' && p[2] == '.' && p[3] != '.')
3111 /* '...' is a wildcard in DJGPP. */
3112 goto slow;
3113#endif
3114 else
3115 /* Not a special char. */
3116 switch (*p)
3117 {
3118 case '=':
3119 /* Equals is a special character in leading words before the
3120 first word with no equals sign in it. This is not the case
3121 with sh -k, but we never get here when using nonstandard
3122 shell flags. */
3123 if (! seen_nonequals && unixy_shell)
3124 goto slow;
3125 word_has_equals = 1;
3126 *ap++ = '=';
3127 break;
3128
3129 case '\\':
3130 /* Backslash-newline has special case handling, ref POSIX.
3131 We're in the fastpath, so emulate what the shell would do. */
3132 if (p[1] == '\n')
3133 {
3134 /* Throw out the backslash and newline. */
3135 ++p;
3136
3137 /* At the beginning of the argument, skip any whitespace other
3138 than newline before the start of the next word. */
3139 if (ap == new_argv[i])
3140 while (ISBLANK (p[1]))
3141 ++p;
3142 }
3143#ifdef WINDOWS32
3144 /* Backslash before whitespace is not special if our shell
3145 is not Unixy. */
3146 else if (ISSPACE (p[1]) && !unixy_shell)
3147 {
3148 *ap++ = *p;
3149 break;
3150 }
3151#endif
3152 else if (p[1] != '\0')
3153 {
3154#ifdef HAVE_DOS_PATHS
3155 /* Only remove backslashes before characters special to Unixy
3156 shells. All other backslashes are copied verbatim, since
3157 they are probably DOS-style directory separators. This
3158 still leaves a small window for problems, but at least it
3159 should work for the vast majority of naive users. */
3160
3161#ifdef __MSDOS__
3162 /* A dot is only special as part of the "..."
3163 wildcard. */
3164 if (strneq (p + 1, ".\\.\\.", 5))
3165 {
3166 *ap++ = '.';
3167 *ap++ = '.';
3168 p += 4;
3169 }
3170 else
3171#endif
3172 if (p[1] != '\\' && p[1] != '\''
3173 && !ISSPACE (p[1])
3174# ifdef KMK
3175 && strchr (sh_chars, p[1]) == 0
3176 && (p[1] != '"' || !unixy_shell))
3177# else
3178 && strchr (sh_chars_sh, p[1]) == 0)
3179# endif
3180 /* back up one notch, to copy the backslash */
3181 --p;
3182#endif /* HAVE_DOS_PATHS */
3183
3184 /* Copy and skip the following char. */
3185 *ap++ = *++p;
3186 }
3187 break;
3188
3189 case '\'':
3190 case '"':
3191 instring = *p;
3192 break;
3193
3194 case '\n':
3195 if (restp != NULL)
3196 {
3197 /* End of the command line. */
3198 *restp = p;
3199 goto end_of_line;
3200 }
3201 else
3202 /* Newlines are not special. */
3203 *ap++ = '\n';
3204 break;
3205
3206 case ' ':
3207 case '\t':
3208 /* We have the end of an argument.
3209 Terminate the text of the argument. */
3210 *ap++ = '\0';
3211 new_argv[++i] = ap;
3212 last_argument_was_empty = 0;
3213
3214 /* Update SEEN_NONEQUALS, which tells us if every word
3215 heretofore has contained an '='. */
3216 seen_nonequals |= ! word_has_equals;
3217 if (word_has_equals && ! seen_nonequals)
3218 /* An '=' in a word before the first
3219 word without one is magical. */
3220 goto slow;
3221 word_has_equals = 0; /* Prepare for the next word. */
3222
3223 /* If this argument is the command name,
3224 see if it is a built-in shell command.
3225 If so, have the shell handle it. */
3226 if (i == 1)
3227 {
3228 register int j;
3229 for (j = 0; sh_cmds[j] != 0; ++j)
3230 {
3231 if (streq (sh_cmds[j], new_argv[0]))
3232 goto slow;
3233#if defined(__EMX__) || defined(WINDOWS32)
3234 /* Non-Unix shells are case insensitive. */
3235 if (!unixy_shell
3236 && strcasecmp (sh_cmds[j], new_argv[0]) == 0)
3237 goto slow;
3238#endif
3239 }
3240 }
3241
3242 /* Skip whitespace chars, but not newlines. */
3243 while (ISBLANK (p[1]))
3244 ++p;
3245 break;
3246
3247 default:
3248 *ap++ = *p;
3249 break;
3250 }
3251 }
3252 end_of_line:
3253
3254 if (instring)
3255 /* Let the shell deal with an unterminated quote. */
3256 goto slow;
3257
3258 /* Terminate the last argument and the argument list. */
3259
3260 *ap = '\0';
3261 if (new_argv[i][0] != '\0' || last_argument_was_empty)
3262 ++i;
3263 new_argv[i] = 0;
3264
3265 if (i == 1)
3266 {
3267 register int j;
3268 for (j = 0; sh_cmds[j] != 0; ++j)
3269 if (streq (sh_cmds[j], new_argv[0]))
3270 goto slow;
3271 }
3272
3273 if (new_argv[0] == 0)
3274 {
3275 /* Line was empty. */
3276 free (argstr);
3277 free (new_argv);
3278 return 0;
3279 }
3280
3281 return new_argv;
3282
3283 slow:;
3284 /* We must use the shell. */
3285
3286 if (new_argv != 0)
3287 {
3288 /* Free the old argument list we were working on. */
3289 free (argstr);
3290 free (new_argv);
3291 }
3292
3293#ifdef __MSDOS__
3294 execute_by_shell = 1; /* actually, call 'system' if shell isn't unixy */
3295#endif
3296
3297#ifdef _AMIGA
3298 {
3299 char *ptr;
3300 char *buffer;
3301 char *dptr;
3302
3303 buffer = xmalloc (strlen (line)+1);
3304
3305 ptr = line;
3306 for (dptr=buffer; *ptr; )
3307 {
3308 if (*ptr == '\\' && ptr[1] == '\n')
3309 ptr += 2;
3310 else if (*ptr == '@') /* Kludge: multiline commands */
3311 {
3312 ptr += 2;
3313 *dptr++ = '\n';
3314 }
3315 else
3316 *dptr++ = *ptr++;
3317 }
3318 *dptr = 0;
3319
3320 new_argv = xmalloc (2 * sizeof (char *));
3321 new_argv[0] = buffer;
3322 new_argv[1] = 0;
3323 }
3324#else /* Not Amiga */
3325#ifdef WINDOWS32
3326 /*
3327 * Not eating this whitespace caused things like
3328 *
3329 * sh -c "\n"
3330 *
3331 * which gave the shell fits. I think we have to eat
3332 * whitespace here, but this code should be considered
3333 * suspicious if things start failing....
3334 */
3335
3336 /* Make sure not to bother processing an empty line. */
3337 NEXT_TOKEN (line);
3338 if (*line == '\0')
3339 return 0;
3340#endif /* WINDOWS32 */
3341
3342 {
3343 /* SHELL may be a multi-word command. Construct a command line
3344 "$(SHELL) $(.SHELLFLAGS) LINE", with all special chars in LINE escaped.
3345 Then recurse, expanding this command line to get the final
3346 argument list. */
3347
3348 char *new_line;
3349 unsigned int shell_len = strlen (shell);
3350 unsigned int line_len = strlen (line);
3351 unsigned int sflags_len = shellflags ? strlen (shellflags) : 0;
3352#ifdef WINDOWS32
3353 char *command_ptr = NULL; /* used for batch_mode_shell mode */
3354#endif
3355
3356# ifdef __EMX__ /* is this necessary? */
3357 if (!unixy_shell && shellflags)
3358 shellflags[0] = '/'; /* "/c" */
3359# endif
3360
3361 /* In .ONESHELL mode we are allowed to throw the entire current
3362 recipe string at a single shell and trust that the user
3363 has configured the shell and shell flags, and formatted
3364 the string, appropriately. */
3365 if (one_shell)
3366 {
3367 /* If the shell is Bourne compatible, we must remove and ignore
3368 interior special chars [@+-] because they're meaningless to
3369 the shell itself. If, however, we're in .ONESHELL mode and
3370 have changed SHELL to something non-standard, we should
3371 leave those alone because they could be part of the
3372 script. In this case we must also leave in place
3373 any leading [@+-] for the same reason. */
3374
3375 /* Remove and ignore interior prefix chars [@+-] because they're
3376 meaningless given a single shell. */
3377#if defined __MSDOS__ || defined (__EMX__)
3378 if (unixy_shell) /* the test is complicated and we already did it */
3379#else
3380 if (is_bourne_compatible_shell (shell)
3381#ifdef WINDOWS32
3382 /* If we didn't find any sh.exe, don't behave is if we did! */
3383 && !no_default_sh_exe
3384#endif
3385 )
3386#endif
3387 {
3388 const char *f = line;
3389 char *t = line;
3390
3391 /* Copy the recipe, removing and ignoring interior prefix chars
3392 [@+-]: they're meaningless in .ONESHELL mode. */
3393 while (f[0] != '\0')
3394 {
3395 int esc = 0;
3396
3397 /* This is the start of a new recipe line. Skip whitespace
3398 and prefix characters but not newlines. */
3399#ifndef CONFIG_WITH_COMMANDS_FUNC
3400 while (ISBLANK (*f) || *f == '-' || *f == '@' || *f == '+')
3401#else
3402 char ch;
3403 while (ISBLANK ((ch = *f)) || ch == '-' || ch == '@' || ch == '+' || ch == '%')
3404#endif
3405 ++f;
3406
3407 /* Copy until we get to the next logical recipe line. */
3408 while (*f != '\0')
3409 {
3410 *(t++) = *(f++);
3411 if (f[-1] == '\\')
3412 esc = !esc;
3413 else
3414 {
3415 /* On unescaped newline, we're done with this line. */
3416 if (f[-1] == '\n' && ! esc)
3417 break;
3418
3419 /* Something else: reset the escape sequence. */
3420 esc = 0;
3421 }
3422 }
3423 }
3424 *t = '\0';
3425 }
3426#ifdef WINDOWS32
3427 else /* non-Posix shell (cmd.exe etc.) */
3428 {
3429 const char *f = line;
3430 char *t = line;
3431 char *tstart = t;
3432 int temp_fd;
3433 FILE* batch = NULL;
3434 int id = GetCurrentProcessId ();
3435 PATH_VAR(fbuf);
3436
3437 /* Generate a file name for the temporary batch file. */
3438 sprintf (fbuf, "make%d", id);
3439 *batch_filename = create_batch_file (fbuf, 0, &temp_fd);
3440 DB (DB_JOBS, (_("Creating temporary batch file %s\n"),
3441 *batch_filename));
3442
3443 /* Create a FILE object for the batch file, and write to it the
3444 commands to be executed. Put the batch file in TEXT mode. */
3445 _setmode (temp_fd, _O_TEXT);
3446 batch = _fdopen (temp_fd, "wt");
3447 fputs ("@echo off\n", batch);
3448 DB (DB_JOBS, (_("Batch file contents:\n\t@echo off\n")));
3449
3450 /* Copy the recipe, removing and ignoring interior prefix chars
3451 [@+-]: they're meaningless in .ONESHELL mode. */
3452 while (*f != '\0')
3453 {
3454 /* This is the start of a new recipe line. Skip whitespace
3455 and prefix characters but not newlines. */
3456#ifndef CONFIG_WITH_COMMANDS_FUNC
3457 while (ISBLANK (*f) || *f == '-' || *f == '@' || *f == '+')
3458#else
3459 char ch;
3460 while (ISBLANK ((ch = *f)) || ch == '-' || ch == '@' || ch == '+' || ch == '%')
3461#endif
3462 ++f;
3463
3464 /* Copy until we get to the next logical recipe line. */
3465 while (*f != '\0')
3466 {
3467 /* Remove the escaped newlines in the command, and the
3468 blanks that follow them. Windows shells cannot handle
3469 escaped newlines. */
3470 if (*f == '\\' && f[1] == '\n')
3471 {
3472 f += 2;
3473 while (ISBLANK (*f))
3474 ++f;
3475 }
3476 *(t++) = *(f++);
3477 /* On an unescaped newline, we're done with this
3478 line. */
3479 if (f[-1] == '\n')
3480 break;
3481 }
3482 /* Write another line into the batch file. */
3483 if (t > tstart)
3484 {
3485 char c = *t;
3486 *t = '\0';
3487 fputs (tstart, batch);
3488 DB (DB_JOBS, ("\t%s", tstart));
3489 tstart = t;
3490 *t = c;
3491 }
3492 }
3493 DB (DB_JOBS, ("\n"));
3494 fclose (batch);
3495
3496 /* Create an argv list for the shell command line that
3497 will run the batch file. */
3498 new_argv = xmalloc (2 * sizeof (char *));
3499 new_argv[0] = xstrdup (*batch_filename);
3500 new_argv[1] = NULL;
3501 return new_argv;
3502 }
3503#endif /* WINDOWS32 */
3504 /* Create an argv list for the shell command line. */
3505 {
3506 int n = 0;
3507
3508 new_argv = xmalloc ((4 + sflags_len/2) * sizeof (char *));
3509 new_argv[n++] = xstrdup (shell);
3510
3511 /* Chop up the shellflags (if any) and assign them. */
3512 if (! shellflags)
3513 new_argv[n++] = xstrdup ("");
3514 else
3515 {
3516 const char *s = shellflags;
3517 char *t;
3518 unsigned int len;
3519 while ((t = find_next_token (&s, &len)) != 0)
3520 new_argv[n++] = xstrndup (t, len);
3521 }
3522
3523 /* Set the command to invoke. */
3524 new_argv[n++] = line;
3525 new_argv[n++] = NULL;
3526 }
3527 return new_argv;
3528 }
3529
3530 new_line = xmalloc ((shell_len*2) + 1 + sflags_len + 1
3531 + (line_len*2) + 1);
3532 ap = new_line;
3533 /* Copy SHELL, escaping any characters special to the shell. If
3534 we don't escape them, construct_command_argv_internal will
3535 recursively call itself ad nauseam, or until stack overflow,
3536 whichever happens first. */
3537 for (cp = shell; *cp != '\0'; ++cp)
3538 {
3539 if (strchr (sh_chars, *cp) != 0)
3540 *(ap++) = '\\';
3541 *(ap++) = *cp;
3542 }
3543 *(ap++) = ' ';
3544 if (shellflags)
3545 memcpy (ap, shellflags, sflags_len);
3546 ap += sflags_len;
3547 *(ap++) = ' ';
3548#ifdef WINDOWS32
3549 command_ptr = ap;
3550#endif
3551 for (p = line; *p != '\0'; ++p)
3552 {
3553 if (restp != NULL && *p == '\n')
3554 {
3555 *restp = p;
3556 break;
3557 }
3558 else if (*p == '\\' && p[1] == '\n')
3559 {
3560 /* POSIX says we keep the backslash-newline. If we don't have a
3561 POSIX shell on DOS/Windows/OS2, mimic the pre-POSIX behavior
3562 and remove the backslash/newline. */
3563#if defined (__MSDOS__) || defined (__EMX__) || defined (WINDOWS32)
3564# define PRESERVE_BSNL unixy_shell
3565#else
3566# define PRESERVE_BSNL 1
3567#endif
3568 if (PRESERVE_BSNL)
3569 {
3570 *(ap++) = '\\';
3571 /* Only non-batch execution needs another backslash,
3572 because it will be passed through a recursive
3573 invocation of this function. */
3574 if (!batch_mode_shell)
3575 *(ap++) = '\\';
3576 *(ap++) = '\n';
3577 }
3578 ++p;
3579 continue;
3580 }
3581
3582 /* DOS shells don't know about backslash-escaping. */
3583 if (unixy_shell && !batch_mode_shell &&
3584 (*p == '\\' || *p == '\'' || *p == '"'
3585 || ISSPACE (*p)
3586 || strchr (sh_chars, *p) != 0))
3587 *ap++ = '\\';
3588#ifdef __MSDOS__
3589 else if (unixy_shell && strneq (p, "...", 3))
3590 {
3591 /* The case of '...' wildcard again. */
3592 strcpy (ap, "\\.\\.\\");
3593 ap += 5;
3594 p += 2;
3595 }
3596#endif
3597 *ap++ = *p;
3598 }
3599 if (ap == new_line + shell_len + sflags_len + 2)
3600 {
3601 /* Line was empty. */
3602 free (new_line);
3603 return 0;
3604 }
3605 *ap = '\0';
3606
3607#ifdef WINDOWS32
3608 /* Some shells do not work well when invoked as 'sh -c xxx' to run a
3609 command line (e.g. Cygnus GNUWIN32 sh.exe on WIN32 systems). In these
3610 cases, run commands via a script file. */
3611 if (just_print_flag && !(flags & COMMANDS_RECURSE))
3612 {
3613 /* Need to allocate new_argv, although it's unused, because
3614 start_job_command will want to free it and its 0'th element. */
3615 new_argv = xmalloc (2 * sizeof (char *));
3616 new_argv[0] = xstrdup ("");
3617 new_argv[1] = NULL;
3618 }
3619 else if ((no_default_sh_exe || batch_mode_shell) && batch_filename)
3620 {
3621 int temp_fd;
3622 FILE* batch = NULL;
3623 int id = GetCurrentProcessId ();
3624 PATH_VAR (fbuf);
3625
3626 /* create a file name */
3627 sprintf (fbuf, "make%d", id);
3628 *batch_filename = create_batch_file (fbuf, unixy_shell, &temp_fd);
3629
3630 DB (DB_JOBS, (_("Creating temporary batch file %s\n"),
3631 *batch_filename));
3632
3633 /* Create a FILE object for the batch file, and write to it the
3634 commands to be executed. Put the batch file in TEXT mode. */
3635 _setmode (temp_fd, _O_TEXT);
3636 batch = _fdopen (temp_fd, "wt");
3637 if (!unixy_shell)
3638 fputs ("@echo off\n", batch);
3639 fputs (command_ptr, batch);
3640 fputc ('\n', batch);
3641 fclose (batch);
3642 DB (DB_JOBS, (_("Batch file contents:%s\n\t%s\n"),
3643 !unixy_shell ? "\n\t@echo off" : "", command_ptr));
3644
3645 /* create argv */
3646 new_argv = xmalloc (3 * sizeof (char *));
3647 if (unixy_shell)
3648 {
3649 new_argv[0] = xstrdup (shell);
3650 new_argv[1] = *batch_filename; /* only argv[0] gets freed later */
3651 }
3652 else
3653 {
3654 new_argv[0] = xstrdup (*batch_filename);
3655 new_argv[1] = NULL;
3656 }
3657 new_argv[2] = NULL;
3658 }
3659 else
3660#endif /* WINDOWS32 */
3661
3662 if (unixy_shell)
3663 new_argv = construct_command_argv_internal (new_line, 0, 0, 0, 0,
3664 flags, 0);
3665
3666#ifdef __EMX__
3667 else if (!unixy_shell)
3668 {
3669 /* new_line is local, must not be freed therefore
3670 We use line here instead of new_line because we run the shell
3671 manually. */
3672 size_t line_len = strlen (line);
3673 char *p = new_line;
3674 char *q = new_line;
3675 memcpy (new_line, line, line_len + 1);
3676 /* Replace all backslash-newline combination and also following tabs.
3677 Important: stop at the first '\n' because that's what the loop above
3678 did. The next line starting at restp[0] will be executed during the
3679 next call of this function. */
3680 while (*q != '\0' && *q != '\n')
3681 {
3682 if (q[0] == '\\' && q[1] == '\n')
3683 q += 2; /* remove '\\' and '\n' */
3684 else
3685 *p++ = *q++;
3686 }
3687 *p = '\0';
3688
3689# ifndef NO_CMD_DEFAULT
3690 if (strnicmp (new_line, "echo", 4) == 0
3691 && (new_line[4] == ' ' || new_line[4] == '\t'))
3692 {
3693 /* the builtin echo command: handle it separately */
3694 size_t echo_len = line_len - 5;
3695 char *echo_line = new_line + 5;
3696
3697 /* special case: echo 'x="y"'
3698 cmd works this way: a string is printed as is, i.e., no quotes
3699 are removed. But autoconf uses a command like echo 'x="y"' to
3700 determine whether make works. autoconf expects the output x="y"
3701 so we will do exactly that.
3702 Note: if we do not allow cmd to be the default shell
3703 we do not need this kind of voodoo */
3704 if (echo_line[0] == '\''
3705 && echo_line[echo_len - 1] == '\''
3706 && strncmp (echo_line + 1, "ac_maketemp=",
3707 strlen ("ac_maketemp=")) == 0)
3708 {
3709 /* remove the enclosing quotes */
3710 memmove (echo_line, echo_line + 1, echo_len - 2);
3711 echo_line[echo_len - 2] = '\0';
3712 }
3713 }
3714# endif
3715
3716 {
3717 /* Let the shell decide what to do. Put the command line into the
3718 2nd command line argument and hope for the best ;-) */
3719 size_t sh_len = strlen (shell);
3720
3721 /* exactly 3 arguments + NULL */
3722 new_argv = xmalloc (4 * sizeof (char *));
3723 /* Exactly strlen(shell) + strlen("/c") + strlen(line) + 3 times
3724 the trailing '\0' */
3725 new_argv[0] = xmalloc (sh_len + line_len + 5);
3726 memcpy (new_argv[0], shell, sh_len + 1);
3727 new_argv[1] = new_argv[0] + sh_len + 1;
3728 memcpy (new_argv[1], "/c", 3);
3729 new_argv[2] = new_argv[1] + 3;
3730 memcpy (new_argv[2], new_line, line_len + 1);
3731 new_argv[3] = NULL;
3732 }
3733 }
3734#elif defined(__MSDOS__)
3735 else
3736 {
3737 /* With MSDOS shells, we must construct the command line here
3738 instead of recursively calling ourselves, because we
3739 cannot backslash-escape the special characters (see above). */
3740 new_argv = xmalloc (sizeof (char *));
3741 line_len = strlen (new_line) - shell_len - sflags_len - 2;
3742 new_argv[0] = xmalloc (line_len + 1);
3743 strncpy (new_argv[0],
3744 new_line + shell_len + sflags_len + 2, line_len);
3745 new_argv[0][line_len] = '\0';
3746 }
3747#else
3748 else
3749 fatal (NILF, CSTRLEN (__FILE__) + INTSTR_LENGTH,
3750 _("%s (line %d) Bad shell context (!unixy && !batch_mode_shell)\n"),
3751 __FILE__, __LINE__);
3752#endif
3753
3754 free (new_line);
3755 }
3756#endif /* ! AMIGA */
3757
3758 return new_argv;
3759}
3760#endif /* !VMS */
3761
3762/* Figure out the argument list necessary to run LINE as a command. Try to
3763 avoid using a shell. This routine handles only ' quoting, and " quoting
3764 when no backslash, $ or ' characters are seen in the quotes. Starting
3765 quotes may be escaped with a backslash. If any of the characters in
3766 sh_chars is seen, or any of the builtin commands listed in sh_cmds
3767 is the first word of a line, the shell is used.
3768
3769 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
3770 If *RESTP is NULL, newlines will be ignored.
3771
3772 FILE is the target whose commands these are. It is used for
3773 variable expansion for $(SHELL) and $(IFS). */
3774
3775char **
3776construct_command_argv (char *line, char **restp, struct file *file,
3777 int cmd_flags, char **batch_filename)
3778{
3779 char *shell, *ifs, *shellflags;
3780 char **argv;
3781
3782#ifdef VMS
3783 char *cptr;
3784 int argc;
3785
3786 argc = 0;
3787 cptr = line;
3788 for (;;)
3789 {
3790 while ((*cptr != 0) && (ISSPACE (*cptr)))
3791 cptr++;
3792 if (*cptr == 0)
3793 break;
3794 while ((*cptr != 0) && (!ISSPACE (*cptr)))
3795 cptr++;
3796 argc++;
3797 }
3798
3799 argv = xmalloc (argc * sizeof (char *));
3800 if (argv == 0)
3801 abort ();
3802
3803 cptr = line;
3804 argc = 0;
3805 for (;;)
3806 {
3807 while ((*cptr != 0) && (ISSPACE (*cptr)))
3808 cptr++;
3809 if (*cptr == 0)
3810 break;
3811 DB (DB_JOBS, ("argv[%d] = [%s]\n", argc, cptr));
3812 argv[argc++] = cptr;
3813 while ((*cptr != 0) && (!ISSPACE (*cptr)))
3814 cptr++;
3815 if (*cptr != 0)
3816 *cptr++ = 0;
3817 }
3818#else
3819 {
3820 /* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
3821 int save = warn_undefined_variables_flag;
3822 warn_undefined_variables_flag = 0;
3823
3824 shell = allocated_variable_expand_for_file ("$(SHELL)", file);
3825#ifdef WINDOWS32
3826 /*
3827 * Convert to forward slashes so that construct_command_argv_internal()
3828 * is not confused.
3829 */
3830 if (shell)
3831 {
3832# if 1 /* bird */
3833 unix_slashes (shell);
3834# else
3835 char *p = w32ify (shell, 0);
3836 strcpy (shell, p);
3837# endif
3838 }
3839#endif
3840#ifdef __EMX__
3841 {
3842 static const char *unixroot = NULL;
3843 static const char *last_shell = "";
3844 static int init = 0;
3845 if (init == 0)
3846 {
3847 unixroot = getenv ("UNIXROOT");
3848 /* unixroot must be NULL or not empty */
3849 if (unixroot && unixroot[0] == '\0') unixroot = NULL;
3850 init = 1;
3851 }
3852
3853 /* if we have an unixroot drive and if shell is not default_shell
3854 (which means it's either cmd.exe or the test has already been
3855 performed) and if shell is an absolute path without drive letter,
3856 try whether it exists e.g.: if "/bin/sh" does not exist use
3857 "$UNIXROOT/bin/sh" instead. */
3858 if (unixroot && shell && strcmp (shell, last_shell) != 0
3859 && (shell[0] == '/' || shell[0] == '\\'))
3860 {
3861 /* trying a new shell, check whether it exists */
3862 size_t size = strlen (shell);
3863 char *buf = xmalloc (size + 7);
3864 memcpy (buf, shell, size);
3865 memcpy (buf + size, ".exe", 5); /* including the trailing '\0' */
3866 if (access (shell, F_OK) != 0 && access (buf, F_OK) != 0)
3867 {
3868 /* try the same for the unixroot drive */
3869 memmove (buf + 2, buf, size + 5);
3870 buf[0] = unixroot[0];
3871 buf[1] = unixroot[1];
3872 if (access (buf, F_OK) == 0)
3873 /* we have found a shell! */
3874 /* free(shell); */
3875 shell = buf;
3876 else
3877 free (buf);
3878 }
3879 else
3880 free (buf);
3881 }
3882 }
3883#endif /* __EMX__ */
3884
3885 shellflags = allocated_variable_expand_for_file ("$(.SHELLFLAGS)", file);
3886 ifs = allocated_variable_expand_for_file ("$(IFS)", file);
3887
3888 warn_undefined_variables_flag = save;
3889 }
3890
3891# ifdef CONFIG_WITH_KMK_BUILTIN
3892 /* If it's a kmk_builtin command, make sure we're treated like a
3893 unix shell and and don't get batch files. */
3894 if ( ( !unixy_shell
3895 || batch_mode_shell
3896# ifdef WINDOWS32
3897 || no_default_sh_exe
3898# endif
3899 )
3900 && line
3901 && !strncmp (line, "kmk_builtin_", sizeof("kmk_builtin_") - 1))
3902 {
3903 int saved_batch_mode_shell = batch_mode_shell;
3904 int saved_unixy_shell = unixy_shell;
3905# ifdef WINDOWS32
3906 int saved_no_default_sh_exe = no_default_sh_exe;
3907 no_default_sh_exe = 0;
3908# endif
3909 unixy_shell = 1;
3910 batch_mode_shell = 0;
3911 argv = construct_command_argv_internal (line, restp, shell, shellflags, ifs,
3912 cmd_flags, batch_filename);
3913 batch_mode_shell = saved_batch_mode_shell;
3914 unixy_shell = saved_unixy_shell;
3915# ifdef WINDOWS32
3916 no_default_sh_exe = saved_no_default_sh_exe;
3917# endif
3918 }
3919 else
3920# endif /* CONFIG_WITH_KMK_BUILTIN */
3921 argv = construct_command_argv_internal (line, restp, shell, shellflags, ifs,
3922 cmd_flags, batch_filename);
3923
3924 free (shell);
3925 free (shellflags);
3926 free (ifs);
3927#endif /* !VMS */
3928 return argv;
3929}
3930
3931
3932#if !defined(HAVE_DUP2) && !defined(_AMIGA)
3933int
3934dup2 (int old, int new)
3935{
3936 int fd;
3937
3938 (void) close (new);
3939 EINTRLOOP (fd, dup (old));
3940 if (fd != new)
3941 {
3942 (void) close (fd);
3943 errno = EMFILE;
3944 return -1;
3945 }
3946
3947 return fd;
3948}
3949#endif /* !HAVE_DUP2 && !_AMIGA */
3950
3951#ifdef CONFIG_WITH_PRINT_TIME_SWITCH
3952/* Prints the time elapsed while executing the commands for the given job. */
3953void print_job_time (struct child *c)
3954{
3955 if ( !handling_fatal_signal
3956 && print_time_min != -1
3957 && c->start_ts != -1)
3958 {
3959 big_int elapsed = nano_timestamp () - c->start_ts;
3960 if (elapsed >= print_time_min * BIG_INT_C(1000000000))
3961 {
3962 char buf[64];
3963 int len = format_elapsed_nano (buf, sizeof (buf), elapsed);
3964 if (len > print_time_width)
3965 print_time_width = len;
3966 message (1, print_time_width + strlen (c->file->name),
3967 _("%*s - %s"), print_time_width, buf, c->file->name);
3968 }
3969 }
3970}
3971#endif
3972
3973/* On VMS systems, include special VMS functions. */
3974
3975#ifdef VMS
3976#include "vmsjobs.c"
3977#endif
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette