VirtualBox

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

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

kmk/win: Reworking child process handling. This effort will hopefully handle processor groups better and allow executing internal function off the main thread.

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