VirtualBox

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

Last change on this file since 2754 was 2754, checked in by bird, 10 years ago

kmk: Save 20+ MB of memory for chopped receipt command lines by freeing them after we're done evaluating a target.

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

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