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