VirtualBox

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

Last change on this file since 2012 was 2001, checked in by bird, 16 years ago

kmk: pedantic warnings.

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

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