]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/bmake/job.c
THIS BRANCH IS OBSOLETE, PLEASE READ:
[FreeBSD/FreeBSD.git] / contrib / bmake / job.c
1 /*      $NetBSD: job.c,v 1.326 2020/11/16 18:28:27 rillig Exp $ */
2
3 /*
4  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Adam de Boor.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35 /*
36  * Copyright (c) 1988, 1989 by Adam de Boor
37  * Copyright (c) 1989 by Berkeley Softworks
38  * All rights reserved.
39  *
40  * This code is derived from software contributed to Berkeley by
41  * Adam de Boor.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. All advertising materials mentioning features or use of this software
52  *    must display the following acknowledgement:
53  *      This product includes software developed by the University of
54  *      California, Berkeley and its contributors.
55  * 4. Neither the name of the University nor the names of its contributors
56  *    may be used to endorse or promote products derived from this software
57  *    without specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  */
71
72 /*-
73  * job.c --
74  *      handle the creation etc. of our child processes.
75  *
76  * Interface:
77  *      Job_Init        Called to initialize this module. In addition,
78  *                      any commands attached to the .BEGIN target
79  *                      are executed before this function returns.
80  *                      Hence, the makefiles must have been parsed
81  *                      before this function is called.
82  *
83  *      Job_End         Clean up any memory used.
84  *
85  *      Job_Make        Start the creation of the given target.
86  *
87  *      Job_CatchChildren
88  *                      Check for and handle the termination of any
89  *                      children. This must be called reasonably
90  *                      frequently to keep the whole make going at
91  *                      a decent clip, since job table entries aren't
92  *                      removed until their process is caught this way.
93  *
94  *      Job_CatchOutput
95  *                      Print any output our children have produced.
96  *                      Should also be called fairly frequently to
97  *                      keep the user informed of what's going on.
98  *                      If no output is waiting, it will block for
99  *                      a time given by the SEL_* constants, below,
100  *                      or until output is ready.
101  *
102  *      Job_ParseShell  Given the line following a .SHELL target, parse
103  *                      the line as a shell specification. Returns
104  *                      FALSE if the spec was incorrect.
105  *
106  *      Job_Finish      Perform any final processing which needs doing.
107  *                      This includes the execution of any commands
108  *                      which have been/were attached to the .END
109  *                      target. It should only be called when the
110  *                      job table is empty.
111  *
112  *      Job_AbortAll    Abort all currently running jobs. It doesn't
113  *                      handle output or do anything for the jobs,
114  *                      just kills them. It should only be called in
115  *                      an emergency.
116  *
117  *      Job_CheckCommands
118  *                      Verify that the commands for a target are
119  *                      ok. Provide them if necessary and possible.
120  *
121  *      Job_Touch       Update a target without really updating it.
122  *
123  *      Job_Wait        Wait for all currently-running jobs to finish.
124  */
125
126 #ifdef HAVE_CONFIG_H
127 # include "config.h"
128 #endif
129 #include <sys/types.h>
130 #include <sys/stat.h>
131 #include <sys/file.h>
132 #include <sys/time.h>
133 #include "wait.h"
134
135 #include <errno.h>
136 #if !defined(USE_SELECT) && defined(HAVE_POLL_H)
137 #include <poll.h>
138 #else
139 #ifndef USE_SELECT                      /* no poll.h */
140 # define USE_SELECT
141 #endif
142 #if defined(HAVE_SYS_SELECT_H)
143 # include <sys/select.h>
144 #endif
145 #endif
146 #include <signal.h>
147 #include <utime.h>
148 #if defined(HAVE_SYS_SOCKET_H)
149 # include <sys/socket.h>
150 #endif
151
152 #include "make.h"
153 #include "dir.h"
154 #include "job.h"
155 #include "pathnames.h"
156 #include "trace.h"
157
158 /*      "@(#)job.c      8.2 (Berkeley) 3/19/94" */
159 MAKE_RCSID("$NetBSD: job.c,v 1.326 2020/11/16 18:28:27 rillig Exp $");
160
161 /* A shell defines how the commands are run.  All commands for a target are
162  * written into a single file, which is then given to the shell to execute
163  * the commands from it.  The commands are written to the file using a few
164  * templates for echo control and error control.
165  *
166  * The name of the shell is the basename for the predefined shells, such as
167  * "sh", "csh", "bash".  For custom shells, it is the full pathname, and its
168  * basename is used to select the type of shell; the longest match wins.
169  * So /usr/pkg/bin/bash has type sh, /usr/local/bin/tcsh has type csh.
170  *
171  * The echoing of command lines is controlled using hasEchoCtl, echoOff,
172  * echoOn, noPrint and noPrintLen.  When echoOff is executed by the shell, it
173  * still outputs something, but this something is not interesting, therefore
174  * it is filtered out using noPrint and noPrintLen.
175  *
176  * The error checking for individual commands is controlled using hasErrCtl,
177  * errOnOrEcho, errOffOrExecIgnore and errExit.
178  *
179  * If a shell doesn't have error control, errOnOrEcho becomes a printf template
180  * for echoing the command, should echoing be on; errOffOrExecIgnore becomes
181  * another printf template for executing the command while ignoring the return
182  * status. Finally errExit is a printf template for running the command and
183  * causing the shell to exit on error. If any of these strings are empty when
184  * hasErrCtl is FALSE, the command will be executed anyway as is, and if it
185  * causes an error, so be it. Any templates set up to echo the command will
186  * escape any '$ ` \ "' characters in the command string to avoid common
187  * problems with echo "%s\n" as a template.
188  *
189  * The command-line flags "echo" and "exit" also control the behavior.  The
190  * "echo" flag causes the shell to start echoing commands right away.  The
191  * "exit" flag causes the shell to exit when an error is detected in one of
192  * the commands.
193  */
194 typedef struct Shell {
195
196     /* The name of the shell. For Bourne and C shells, this is used only to
197      * find the shell description when used as the single source of a .SHELL
198      * target. For user-defined shells, this is the full path of the shell. */
199     const char *name;
200
201     Boolean hasEchoCtl;         /* True if both echoOff and echoOn defined */
202     const char *echoOff;        /* command to turn off echo */
203     const char *echoOn;         /* command to turn it back on again */
204     const char *noPrint;        /* text to skip when printing output from
205                                  * shell. This is usually the same as echoOff */
206     size_t noPrintLen;          /* length of noPrint command */
207
208     Boolean hasErrCtl;          /* set if can control error checking for
209                                  * individual commands */
210     /* XXX: split into errOn and echoCmd */
211     const char *errOnOrEcho;    /* template to turn on error checking */
212     /* XXX: split into errOff and execIgnore */
213     const char *errOffOrExecIgnore; /* template to turn off error checking */
214     const char *errExit;        /* template to use for testing exit code */
215
216     /* string literal that results in a newline character when it appears
217      * outside of any 'quote' or "quote" characters */
218     const char *newline;
219     char commentChar;           /* character used by shell for comment lines */
220
221     /*
222      * command-line flags
223      */
224     const char *echo;           /* echo commands */
225     const char *exit;           /* exit on error */
226 } Shell;
227
228 /*
229  * FreeBSD: traditionally .MAKE is not required to
230  * pass jobs queue to sub-makes.
231  * Use .MAKE.ALWAYS_PASS_JOB_QUEUE=no to disable.
232  */
233 #define MAKE_ALWAYS_PASS_JOB_QUEUE ".MAKE.ALWAYS_PASS_JOB_QUEUE"
234 static int Always_pass_job_queue = TRUE;
235 /*
236  * FreeBSD: aborting entire parallel make isn't always
237  * desired. When doing tinderbox for example, failure of
238  * one architecture should not stop all.
239  * We still want to bail on interrupt though.
240  */
241 #define MAKE_JOB_ERROR_TOKEN "MAKE_JOB_ERROR_TOKEN"
242 static int Job_error_token = TRUE;
243
244 /*
245  * error handling variables
246  */
247 static int errors = 0;          /* number of errors reported */
248 typedef enum AbortReason {      /* why is the make aborting? */
249     ABORT_NONE,
250     ABORT_ERROR,                /* Because of an error */
251     ABORT_INTERRUPT,            /* Because it was interrupted */
252     ABORT_WAIT                  /* Waiting for jobs to finish */
253 } AbortReason;
254 static AbortReason aborting = ABORT_NONE;
255 #define JOB_TOKENS      "+EI+"  /* Token to requeue for each abort state */
256
257 /*
258  * this tracks the number of tokens currently "out" to build jobs.
259  */
260 int jobTokensRunning = 0;
261
262 /* The number of commands actually printed to the shell commands file for
263  * the current job.  Should this number be 0, no shell will be executed. */
264 static int numCommands;
265
266 typedef enum JobStartResult {
267     JOB_RUNNING,                /* Job is running */
268     JOB_ERROR,                  /* Error in starting the job */
269     JOB_FINISHED                /* The job is already finished */
270 } JobStartResult;
271
272 /*
273  * Descriptions for various shells.
274  *
275  * The build environment may set DEFSHELL_INDEX to one of
276  * DEFSHELL_INDEX_SH, DEFSHELL_INDEX_KSH, or DEFSHELL_INDEX_CSH, to
277  * select one of the predefined shells as the default shell.
278  *
279  * Alternatively, the build environment may set DEFSHELL_CUSTOM to the
280  * name or the full path of a sh-compatible shell, which will be used as
281  * the default shell.
282  *
283  * ".SHELL" lines in Makefiles can choose the default shell from the
284  * set defined here, or add additional shells.
285  */
286
287 #ifdef DEFSHELL_CUSTOM
288 #define DEFSHELL_INDEX_CUSTOM 0
289 #define DEFSHELL_INDEX_SH     1
290 #define DEFSHELL_INDEX_KSH    2
291 #define DEFSHELL_INDEX_CSH    3
292 #else /* !DEFSHELL_CUSTOM */
293 #define DEFSHELL_INDEX_SH     0
294 #define DEFSHELL_INDEX_KSH    1
295 #define DEFSHELL_INDEX_CSH    2
296 #endif /* !DEFSHELL_CUSTOM */
297
298 #ifndef DEFSHELL_INDEX
299 #define DEFSHELL_INDEX 0        /* DEFSHELL_INDEX_CUSTOM or DEFSHELL_INDEX_SH */
300 #endif /* !DEFSHELL_INDEX */
301
302 static Shell    shells[] = {
303 #ifdef DEFSHELL_CUSTOM
304     /*
305      * An sh-compatible shell with a non-standard name.
306      *
307      * Keep this in sync with the "sh" description below, but avoid
308      * non-portable features that might not be supplied by all
309      * sh-compatible shells.
310      */
311 {
312     DEFSHELL_CUSTOM,            /* .name */
313     FALSE,                      /* .hasEchoCtl */
314     "",                         /* .echoOff */
315     "",                         /* .echoOn */
316     "",                         /* .noPrint */
317     0,                          /* .noPrintLen */
318     FALSE,                      /* .hasErrCtl */
319     "echo \"%s\"\n",            /* .errOnOrEcho */
320     "%s\n",                     /* .errOffOrExecIgnore */
321     "{ %s \n} || exit $?\n",    /* .errExit */
322     "'\n'",                     /* .newline */
323     '#',                        /* .commentChar */
324     "",                         /* .echo */
325     "",                         /* .exit */
326 },
327 #endif /* DEFSHELL_CUSTOM */
328     /*
329      * SH description. Echo control is also possible and, under
330      * sun UNIX anyway, one can even control error checking.
331      */
332 {
333     "sh",                       /* .name */
334     FALSE,                      /* .hasEchoCtl */
335     "",                         /* .echoOff */
336     "",                         /* .echoOn */
337     "",                         /* .noPrint */
338     0,                          /* .noPrintLen */
339     FALSE,                      /* .hasErrCtl */
340     "echo \"%s\"\n",            /* .errOnOrEcho */
341     "%s\n",                     /* .errOffOrExecIgnore */
342     "{ %s \n} || exit $?\n",    /* .errExit */
343     "'\n'",                     /* .newline */
344     '#',                        /* .commentChar*/
345 #if defined(MAKE_NATIVE) && defined(__NetBSD__)
346     "q",                        /* .echo */
347 #else
348     "",                         /* .echo */
349 #endif
350     "",                         /* .exit */
351 },
352     /*
353      * KSH description.
354      */
355 {
356     "ksh",                      /* .name */
357     TRUE,                       /* .hasEchoCtl */
358     "set +v",                   /* .echoOff */
359     "set -v",                   /* .echoOn */
360     "set +v",                   /* .noPrint */
361     6,                          /* .noPrintLen */
362     FALSE,                      /* .hasErrCtl */
363     "echo \"%s\"\n",            /* .errOnOrEcho */
364     "%s\n",                     /* .errOffOrExecIgnore */
365     "{ %s \n} || exit $?\n",    /* .errExit */
366     "'\n'",                     /* .newline */
367     '#',                        /* .commentChar */
368     "v",                        /* .echo */
369     "",                         /* .exit */
370 },
371     /*
372      * CSH description. The csh can do echo control by playing
373      * with the setting of the 'echo' shell variable. Sadly,
374      * however, it is unable to do error control nicely.
375      */
376 {
377     "csh",                      /* .name */
378     TRUE,                       /* .hasEchoCtl */
379     "unset verbose",            /* .echoOff */
380     "set verbose",              /* .echoOn */
381     "unset verbose",            /* .noPrint */
382     13,                         /* .noPrintLen */
383     FALSE,                      /* .hasErrCtl */
384     "echo \"%s\"\n",            /* .errOnOrEcho */
385     /* XXX: Mismatch between errOn and execIgnore */
386     "csh -c \"%s || exit 0\"\n", /* .errOffOrExecIgnore */
387     "",                         /* .errExit */
388     "'\\\n'",                   /* .newline */
389     '#',                        /* .commentChar */
390     "v",                        /* .echo */
391     "e",                        /* .exit */
392 }
393 };
394
395 /* This is the shell to which we pass all commands in the Makefile.
396  * It is set by the Job_ParseShell function. */
397 static Shell *commandShell = &shells[DEFSHELL_INDEX];
398 const char *shellPath = NULL;   /* full pathname of executable image */
399 const char *shellName = NULL;   /* last component of shellPath */
400 char *shellErrFlag = NULL;
401 static char *shellArgv = NULL;  /* Custom shell args */
402
403
404 static Job *job_table;          /* The structures that describe them */
405 static Job *job_table_end;      /* job_table + maxJobs */
406 static unsigned int wantToken;  /* we want a token */
407 static Boolean lurking_children = FALSE;
408 static Boolean make_suspended = FALSE;  /* Whether we've seen a SIGTSTP (etc) */
409
410 /*
411  * Set of descriptors of pipes connected to
412  * the output channels of children
413  */
414 static struct pollfd *fds = NULL;
415 static Job **jobfds = NULL;
416 static nfds_t nfds = 0;
417 static void watchfd(Job *);
418 static void clearfd(Job *);
419 static int readyfd(Job *);
420
421 static GNode *lastNode;         /* The node for which output was most recently
422                                  * produced. */
423 static char *targPrefix = NULL; /* What we print at the start of TARG_FMT */
424 static Job tokenWaitJob;        /* token wait pseudo-job */
425
426 static Job childExitJob;        /* child exit pseudo-job */
427 #define CHILD_EXIT      "."
428 #define DO_JOB_RESUME   "R"
429
430 enum { npseudojobs = 2 };       /* number of pseudo-jobs */
431
432 #define TARG_FMT  "%s %s ---\n" /* Default format */
433 #define MESSAGE(fp, gn) \
434         if (opts.maxJobs != 1 && targPrefix && *targPrefix) \
435             (void)fprintf(fp, TARG_FMT, targPrefix, gn->name)
436
437 static sigset_t caught_signals; /* Set of signals we handle */
438
439 static void JobDoOutput(Job *, Boolean);
440 static void JobInterrupt(int, int) MAKE_ATTR_DEAD;
441 static void JobRestartJobs(void);
442 static void JobSigReset(void);
443
444 static unsigned
445 nfds_per_job(void)
446 {
447 #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
448     if (useMeta)
449         return 2;
450 #endif
451     return 1;
452 }
453
454 static void
455 job_table_dump(const char *where)
456 {
457     Job *job;
458
459     debug_printf("job table @ %s\n", where);
460     for (job = job_table; job < job_table_end; job++) {
461         debug_printf("job %d, status %d, flags %d, pid %d\n",
462                      (int)(job - job_table), job->status, job->flags, job->pid);
463     }
464 }
465
466 /*
467  * Delete the target of a failed, interrupted, or otherwise
468  * unsuccessful job unless inhibited by .PRECIOUS.
469  */
470 static void
471 JobDeleteTarget(GNode *gn)
472 {
473     const char *file;
474
475     if (gn->type & OP_JOIN)
476         return;
477     if (gn->type & OP_PHONY)
478         return;
479     if (Targ_Precious(gn))
480         return;
481     if (opts.noExecute)
482         return;
483
484     file = GNode_Path(gn);
485     if (eunlink(file) != -1)
486         Error("*** %s removed", file);
487 }
488
489 /*
490  * JobSigLock/JobSigUnlock
491  *
492  * Signal lock routines to get exclusive access. Currently used to
493  * protect `jobs' and `stoppedJobs' list manipulations.
494  */
495 static void JobSigLock(sigset_t *omaskp)
496 {
497         if (sigprocmask(SIG_BLOCK, &caught_signals, omaskp) != 0) {
498                 Punt("JobSigLock: sigprocmask: %s", strerror(errno));
499                 sigemptyset(omaskp);
500         }
501 }
502
503 static void JobSigUnlock(sigset_t *omaskp)
504 {
505         (void)sigprocmask(SIG_SETMASK, omaskp, NULL);
506 }
507
508 static void
509 JobCreatePipe(Job *job, int minfd)
510 {
511     int i, fd, flags;
512     int pipe_fds[2];
513
514     if (pipe(pipe_fds) == -1)
515         Punt("Cannot create pipe: %s", strerror(errno));
516
517     for (i = 0; i < 2; i++) {
518         /* Avoid using low numbered fds */
519         fd = fcntl(pipe_fds[i], F_DUPFD, minfd);
520         if (fd != -1) {
521             close(pipe_fds[i]);
522             pipe_fds[i] = fd;
523         }
524     }
525
526     job->inPipe = pipe_fds[0];
527     job->outPipe = pipe_fds[1];
528
529     /* Set close-on-exec flag for both */
530     if (fcntl(job->inPipe, F_SETFD, FD_CLOEXEC) == -1)
531         Punt("Cannot set close-on-exec: %s", strerror(errno));
532     if (fcntl(job->outPipe, F_SETFD, FD_CLOEXEC) == -1)
533         Punt("Cannot set close-on-exec: %s", strerror(errno));
534
535     /*
536      * We mark the input side of the pipe non-blocking; we poll(2) the
537      * pipe when we're waiting for a job token, but we might lose the
538      * race for the token when a new one becomes available, so the read
539      * from the pipe should not block.
540      */
541     flags = fcntl(job->inPipe, F_GETFL, 0);
542     if (flags == -1)
543         Punt("Cannot get flags: %s", strerror(errno));
544     flags |= O_NONBLOCK;
545     if (fcntl(job->inPipe, F_SETFL, flags) == -1)
546         Punt("Cannot set flags: %s", strerror(errno));
547 }
548
549 /* Pass the signal to each running job. */
550 static void
551 JobCondPassSig(int signo)
552 {
553     Job *job;
554
555     DEBUG1(JOB, "JobCondPassSig(%d) called.\n", signo);
556
557     for (job = job_table; job < job_table_end; job++) {
558         if (job->status != JOB_ST_RUNNING)
559             continue;
560         DEBUG2(JOB, "JobCondPassSig passing signal %d to child %d.\n",
561                signo, job->pid);
562         KILLPG(job->pid, signo);
563     }
564 }
565
566 /* SIGCHLD handler.
567  *
568  * Sends a token on the child exit pipe to wake us up from select()/poll(). */
569 static void
570 JobChildSig(int signo MAKE_ATTR_UNUSED)
571 {
572     while (write(childExitJob.outPipe, CHILD_EXIT, 1) == -1 && errno == EAGAIN)
573         continue;
574 }
575
576
577 /* Resume all stopped jobs. */
578 static void
579 JobContinueSig(int signo MAKE_ATTR_UNUSED)
580 {
581     /*
582      * Defer sending SIGCONT to our stopped children until we return
583      * from the signal handler.
584      */
585     while (write(childExitJob.outPipe, DO_JOB_RESUME, 1) == -1 &&
586         errno == EAGAIN)
587         continue;
588 }
589
590 /* Pass a signal on to all jobs, then resend to ourselves.
591  * We die by the same signal. */
592 MAKE_ATTR_DEAD static void
593 JobPassSig_int(int signo)
594 {
595     /* Run .INTERRUPT target then exit */
596     JobInterrupt(TRUE, signo);
597 }
598
599 /* Pass a signal on to all jobs, then resend to ourselves.
600  * We die by the same signal. */
601 MAKE_ATTR_DEAD static void
602 JobPassSig_term(int signo)
603 {
604     /* Dont run .INTERRUPT target then exit */
605     JobInterrupt(FALSE, signo);
606 }
607
608 static void
609 JobPassSig_suspend(int signo)
610 {
611     sigset_t nmask, omask;
612     struct sigaction act;
613
614     /* Suppress job started/continued messages */
615     make_suspended = TRUE;
616
617     /* Pass the signal onto every job */
618     JobCondPassSig(signo);
619
620     /*
621      * Send ourselves the signal now we've given the message to everyone else.
622      * Note we block everything else possible while we're getting the signal.
623      * This ensures that all our jobs get continued when we wake up before
624      * we take any other signal.
625      */
626     sigfillset(&nmask);
627     sigdelset(&nmask, signo);
628     (void)sigprocmask(SIG_SETMASK, &nmask, &omask);
629
630     act.sa_handler = SIG_DFL;
631     sigemptyset(&act.sa_mask);
632     act.sa_flags = 0;
633     (void)sigaction(signo, &act, NULL);
634
635     if (DEBUG(JOB))
636         debug_printf("JobPassSig passing signal %d to self.\n", signo);
637
638     (void)kill(getpid(), signo);
639
640     /*
641      * We've been continued.
642      *
643      * A whole host of signals continue to happen!
644      * SIGCHLD for any processes that actually suspended themselves.
645      * SIGCHLD for any processes that exited while we were alseep.
646      * The SIGCONT that actually caused us to wakeup.
647      *
648      * Since we defer passing the SIGCONT on to our children until
649      * the main processing loop, we can be sure that all the SIGCHLD
650      * events will have happened by then - and that the waitpid() will
651      * collect the child 'suspended' events.
652      * For correct sequencing we just need to ensure we process the
653      * waitpid() before passing on the SIGCONT.
654      *
655      * In any case nothing else is needed here.
656      */
657
658     /* Restore handler and signal mask */
659     act.sa_handler = JobPassSig_suspend;
660     (void)sigaction(signo, &act, NULL);
661     (void)sigprocmask(SIG_SETMASK, &omask, NULL);
662 }
663
664 static Job *
665 JobFindPid(int pid, JobStatus status, Boolean isJobs)
666 {
667     Job *job;
668
669     for (job = job_table; job < job_table_end; job++) {
670         if (job->status == status && job->pid == pid)
671             return job;
672     }
673     if (DEBUG(JOB) && isJobs)
674         job_table_dump("no pid");
675     return NULL;
676 }
677
678 /* Parse leading '@', '-' and '+', which control the exact execution mode. */
679 static void
680 ParseRunOptions(
681         char **pp,
682         Boolean *out_shutUp, Boolean *out_errOff, Boolean *out_runAlways)
683 {
684     char *p = *pp;
685     *out_shutUp = FALSE;
686     *out_errOff = FALSE;
687     *out_runAlways = FALSE;
688
689     for (;;) {
690         if (*p == '@')
691             *out_shutUp = !DEBUG(LOUD);
692         else if (*p == '-')
693             *out_errOff = TRUE;
694         else if (*p == '+')
695             *out_runAlways = TRUE;
696         else
697             break;
698         p++;
699     }
700
701     pp_skip_whitespace(&p);
702
703     *pp = p;
704 }
705
706 /* Escape a string for a double-quoted string literal in sh, csh and ksh. */
707 static char *
708 EscapeShellDblQuot(const char *cmd)
709 {
710     size_t i, j;
711
712     /* Worst that could happen is every char needs escaping. */
713     char *esc = bmake_malloc(strlen(cmd) * 2 + 1);
714     for (i = 0, j = 0; cmd[i] != '\0'; i++, j++) {
715         if (cmd[i] == '$' || cmd[i] == '`' || cmd[i] == '\\' || cmd[i] == '"')
716             esc[j++] = '\\';
717         esc[j] = cmd[i];
718     }
719     esc[j] = '\0';
720
721     return esc;
722 }
723
724 static void
725 JobPrintf(Job *job, const char *fmt, const char *arg)
726 {
727     if (DEBUG(JOB))
728         debug_printf(fmt, arg);
729
730     (void)fprintf(job->cmdFILE, fmt, arg);
731     (void)fflush(job->cmdFILE);
732 }
733
734 static void
735 JobPrintln(Job *job, const char *line)
736 {
737     JobPrintf(job, "%s\n", line);
738 }
739
740 /*-
741  *-----------------------------------------------------------------------
742  * JobPrintCommand  --
743  *      Put out another command for the given job. If the command starts
744  *      with an @ or a - we process it specially. In the former case,
745  *      so long as the -s and -n flags weren't given to make, we stick
746  *      a shell-specific echoOff command in the script. In the latter,
747  *      we ignore errors for the entire job, unless the shell has error
748  *      control.
749  *      If the command is just "..." we take all future commands for this
750  *      job to be commands to be executed once the entire graph has been
751  *      made and return non-zero to signal that the end of the commands
752  *      was reached. These commands are later attached to the .END
753  *      node and executed by Job_End when all things are done.
754  *
755  * Side Effects:
756  *      If the command begins with a '-' and the shell has no error control,
757  *      the JOB_IGNERR flag is set in the job descriptor.
758  *      numCommands is incremented if the command is actually printed.
759  *-----------------------------------------------------------------------
760  */
761 static void
762 JobPrintCommand(Job *job, char *cmd)
763 {
764     const char *const cmdp = cmd;
765     Boolean noSpecials;         /* true if we shouldn't worry about
766                                  * inserting special commands into
767                                  * the input stream. */
768     Boolean shutUp;             /* true if we put a no echo command
769                                  * into the command file */
770     Boolean errOff;             /* true if we turned error checking
771                                  * off before printing the command
772                                  * and need to turn it back on */
773     Boolean runAlways;
774     const char *cmdTemplate;    /* Template to use when printing the
775                                  * command */
776     char *cmdStart;             /* Start of expanded command */
777     char *escCmd = NULL;        /* Command with quotes/backticks escaped */
778
779     noSpecials = !GNode_ShouldExecute(job->node);
780
781     numCommands++;
782
783     Var_Subst(cmd, job->node, VARE_WANTRES, &cmd);
784     /* TODO: handle errors */
785     cmdStart = cmd;
786
787     cmdTemplate = "%s\n";
788
789     ParseRunOptions(&cmd, &shutUp, &errOff, &runAlways);
790
791     if (runAlways && noSpecials) {
792         /*
793          * We're not actually executing anything...
794          * but this one needs to be - use compat mode just for it.
795          */
796         Compat_RunCommand(cmdp, job->node);
797         free(cmdStart);
798         return;
799     }
800
801     /*
802      * If the shell doesn't have error control the alternate echo'ing will
803      * be done (to avoid showing additional error checking code)
804      * and this will need the characters '$ ` \ "' escaped
805      */
806
807     if (!commandShell->hasErrCtl)
808         escCmd = EscapeShellDblQuot(cmd);
809
810     if (shutUp) {
811         if (!(job->flags & JOB_SILENT) && !noSpecials &&
812             (commandShell->hasEchoCtl)) {
813             JobPrintln(job, commandShell->echoOff);
814         } else {
815             if (commandShell->hasErrCtl)
816                 shutUp = FALSE;
817         }
818     }
819
820     if (errOff) {
821         if (!noSpecials) {
822             if (commandShell->hasErrCtl) {
823                 /*
824                  * we don't want the error-control commands showing
825                  * up either, so we turn off echoing while executing
826                  * them. We could put another field in the shell
827                  * structure to tell JobDoOutput to look for this
828                  * string too, but why make it any more complex than
829                  * it already is?
830                  */
831                 if (!(job->flags & JOB_SILENT) && !shutUp &&
832                     (commandShell->hasEchoCtl)) {
833                     JobPrintln(job, commandShell->echoOff);
834                     JobPrintln(job, commandShell->errOffOrExecIgnore);
835                     JobPrintln(job,  commandShell->echoOn);
836                 } else {
837                     JobPrintln(job, commandShell->errOffOrExecIgnore);
838                 }
839             } else if (commandShell->errOffOrExecIgnore &&
840                        commandShell->errOffOrExecIgnore[0] != '\0') {
841                 /*
842                  * The shell has no error control, so we need to be
843                  * weird to get it to ignore any errors from the command.
844                  * If echoing is turned on, we turn it off and use the
845                  * errOnOrEcho template to echo the command. Leave echoing
846                  * off so the user doesn't see the weirdness we go through
847                  * to ignore errors. Set cmdTemplate to use the weirdness
848                  * instead of the simple "%s\n" template.
849                  */
850                 job->flags |= JOB_IGNERR;
851                 if (!(job->flags & JOB_SILENT) && !shutUp) {
852                     if (commandShell->hasEchoCtl) {
853                         JobPrintln(job, commandShell->echoOff);
854                     }
855                     JobPrintf(job, commandShell->errOnOrEcho, escCmd);
856                     shutUp = TRUE;
857                 } else {
858                     if (!shutUp)
859                         JobPrintf(job, commandShell->errOnOrEcho, escCmd);
860                 }
861                 cmdTemplate = commandShell->errOffOrExecIgnore;
862                 /*
863                  * The error ignoration (hee hee) is already taken care
864                  * of by the errOffOrExecIgnore template, so pretend error
865                  * checking is still on.
866                  */
867                 errOff = FALSE;
868             } else {
869                 errOff = FALSE;
870             }
871         } else {
872             errOff = FALSE;
873         }
874     } else {
875
876         /*
877          * If errors are being checked and the shell doesn't have error control
878          * but does supply an errExit template, then set up commands to run
879          * through it.
880          */
881
882         if (!commandShell->hasErrCtl && commandShell->errExit &&
883             commandShell->errExit[0] != '\0') {
884             if (!(job->flags & JOB_SILENT) && !shutUp) {
885                 if (commandShell->hasEchoCtl)
886                     JobPrintln(job, commandShell->echoOff);
887                 JobPrintf(job, commandShell->errOnOrEcho, escCmd);
888                 shutUp = TRUE;
889             }
890             /* If it's a comment line or blank, treat as an ignored error */
891             if (escCmd[0] == commandShell->commentChar ||
892                 (escCmd[0] == '\0'))
893                 cmdTemplate = commandShell->errOffOrExecIgnore;
894             else
895                 cmdTemplate = commandShell->errExit;
896             errOff = FALSE;
897         }
898     }
899
900     if (DEBUG(SHELL) && strcmp(shellName, "sh") == 0 &&
901         !(job->flags & JOB_TRACED)) {
902         JobPrintln(job, "set -x");
903         job->flags |= JOB_TRACED;
904     }
905
906     JobPrintf(job, cmdTemplate, cmd);
907     free(cmdStart);
908     free(escCmd);
909     if (errOff) {
910         /*
911          * If echoing is already off, there's no point in issuing the
912          * echoOff command. Otherwise we issue it and pretend it was on
913          * for the whole command...
914          */
915         if (!shutUp && !(job->flags & JOB_SILENT) && commandShell->hasEchoCtl) {
916             JobPrintln(job, commandShell->echoOff);
917             shutUp = TRUE;
918         }
919         JobPrintln(job, commandShell->errOnOrEcho);
920     }
921     if (shutUp && commandShell->hasEchoCtl)
922         JobPrintln(job, commandShell->echoOn);
923 }
924
925 /* Print all commands to the shell file that is later executed.
926  *
927  * The special command "..." stops printing and saves the remaining commands
928  * to be executed later. */
929 static void
930 JobPrintCommands(Job *job)
931 {
932     StringListNode *ln;
933
934     for (ln = job->node->commands->first; ln != NULL; ln = ln->next) {
935         const char *cmd = ln->datum;
936
937         if (strcmp(cmd, "...") == 0) {
938             job->node->type |= OP_SAVE_CMDS;
939             job->tailCmds = ln->next;
940             break;
941         }
942
943         JobPrintCommand(job, ln->datum);
944     }
945 }
946
947 /* Save the delayed commands, to be executed when everything else is done. */
948 static void
949 JobSaveCommands(Job *job)
950 {
951     StringListNode *node;
952
953     for (node = job->tailCmds; node != NULL; node = node->next) {
954         const char *cmd = node->datum;
955         char *expanded_cmd;
956         /* XXX: This Var_Subst is only intended to expand the dynamic
957          * variables such as .TARGET, .IMPSRC.  It is not intended to
958          * expand the other variables as well; see deptgt-end.mk. */
959         (void)Var_Subst(cmd, job->node, VARE_WANTRES, &expanded_cmd);
960         /* TODO: handle errors */
961         Lst_Append(Targ_GetEndNode()->commands, expanded_cmd);
962     }
963 }
964
965
966 /* Called to close both input and output pipes when a job is finished. */
967 static void
968 JobClosePipes(Job *job)
969 {
970     clearfd(job);
971     (void)close(job->outPipe);
972     job->outPipe = -1;
973
974     JobDoOutput(job, TRUE);
975     (void)close(job->inPipe);
976     job->inPipe = -1;
977 }
978
979 /* Do final processing for the given job including updating parent nodes and
980  * starting new jobs as available/necessary.
981  *
982  * Deferred commands for the job are placed on the .END node.
983  *
984  * If there was a serious error (errors != 0; not an ignored one), no more
985  * jobs will be started.
986  *
987  * Input:
988  *      job             job to finish
989  *      status          sub-why job went away
990  */
991 static void
992 JobFinish (Job *job, WAIT_T status)
993 {
994     Boolean done, return_job_token;
995
996     DEBUG3(JOB, "JobFinish: %d [%s], status %d\n",
997            job->pid, job->node->name, status);
998
999     if ((WIFEXITED(status) &&
1000          ((WEXITSTATUS(status) != 0 && !(job->flags & JOB_IGNERR)))) ||
1001         WIFSIGNALED(status))
1002     {
1003         /*
1004          * If it exited non-zero and either we're doing things our
1005          * way or we're not ignoring errors, the job is finished.
1006          * Similarly, if the shell died because of a signal
1007          * the job is also finished. In these
1008          * cases, finish out the job's output before printing the exit
1009          * status...
1010          */
1011         JobClosePipes(job);
1012         if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
1013            (void)fclose(job->cmdFILE);
1014            job->cmdFILE = NULL;
1015         }
1016         done = TRUE;
1017     } else if (WIFEXITED(status)) {
1018         /*
1019          * Deal with ignored errors in -B mode. We need to print a message
1020          * telling of the ignored error as well as to run the next command.
1021          *
1022          */
1023         done = WEXITSTATUS(status) != 0;
1024         JobClosePipes(job);
1025     } else {
1026         /*
1027          * No need to close things down or anything.
1028          */
1029         done = FALSE;
1030     }
1031
1032     if (done) {
1033         if (WIFEXITED(status)) {
1034             DEBUG2(JOB, "Process %d [%s] exited.\n",
1035                    job->pid, job->node->name);
1036             if (WEXITSTATUS(status) != 0) {
1037                 if (job->node != lastNode) {
1038                     MESSAGE(stdout, job->node);
1039                     lastNode = job->node;
1040                 }
1041 #ifdef USE_META
1042                 if (useMeta) {
1043                     meta_job_error(job, job->node, job->flags, WEXITSTATUS(status));
1044                 }
1045 #endif
1046                 if (!shouldDieQuietly(job->node, -1))
1047                     (void)printf("*** [%s] Error code %d%s\n",
1048                                  job->node->name,
1049                                  WEXITSTATUS(status),
1050                                  (job->flags & JOB_IGNERR) ? " (ignored)" : "");
1051                 if (job->flags & JOB_IGNERR) {
1052                     WAIT_STATUS(status) = 0;
1053                 } else {
1054                     if (deleteOnError) {
1055                         JobDeleteTarget(job->node);
1056                     }
1057                     PrintOnError(job->node, NULL);
1058                 }
1059             } else if (DEBUG(JOB)) {
1060                 if (job->node != lastNode) {
1061                     MESSAGE(stdout, job->node);
1062                     lastNode = job->node;
1063                 }
1064                 (void)printf("*** [%s] Completed successfully\n",
1065                                 job->node->name);
1066             }
1067         } else {
1068             if (job->node != lastNode) {
1069                 MESSAGE(stdout, job->node);
1070                 lastNode = job->node;
1071             }
1072             (void)printf("*** [%s] Signal %d\n",
1073                         job->node->name, WTERMSIG(status));
1074             if (deleteOnError) {
1075                 JobDeleteTarget(job->node);
1076             }
1077         }
1078         (void)fflush(stdout);
1079     }
1080
1081 #ifdef USE_META
1082     if (useMeta) {
1083         int meta_status = meta_job_finish(job);
1084         if (meta_status != 0 && status == 0)
1085             status = meta_status;
1086     }
1087 #endif
1088
1089     return_job_token = FALSE;
1090
1091     Trace_Log(JOBEND, job);
1092     if (!(job->flags & JOB_SPECIAL)) {
1093             if (WAIT_STATUS(status) != 0 ||
1094                 (aborting == ABORT_ERROR) || aborting == ABORT_INTERRUPT)
1095             return_job_token = TRUE;
1096     }
1097
1098     if (aborting != ABORT_ERROR && aborting != ABORT_INTERRUPT &&
1099         (WAIT_STATUS(status) == 0)) {
1100         /*
1101          * As long as we aren't aborting and the job didn't return a non-zero
1102          * status that we shouldn't ignore, we call Make_Update to update
1103          * the parents.
1104          */
1105         JobSaveCommands(job);
1106         job->node->made = MADE;
1107         if (!(job->flags & JOB_SPECIAL))
1108             return_job_token = TRUE;
1109         Make_Update(job->node);
1110         job->status = JOB_ST_FREE;
1111     } else if (WAIT_STATUS(status)) {
1112         errors++;
1113         job->status = JOB_ST_FREE;
1114     }
1115
1116     if (errors > 0 && !opts.keepgoing && aborting != ABORT_INTERRUPT)
1117         aborting = ABORT_ERROR; /* Prevent more jobs from getting started. */
1118
1119     if (return_job_token)
1120         Job_TokenReturn();
1121
1122     if (aborting == ABORT_ERROR && jobTokensRunning == 0)
1123         Finish(errors);
1124 }
1125
1126 static void
1127 TouchRegular(GNode *gn)
1128 {
1129     const char *file = GNode_Path(gn);
1130     struct utimbuf times = { now, now };
1131     int fd;
1132     char c;
1133
1134     if (utime(file, &times) >= 0)
1135         return;
1136
1137     fd = open(file, O_RDWR | O_CREAT, 0666);
1138     if (fd < 0) {
1139         (void)fprintf(stderr, "*** couldn't touch %s: %s\n",
1140                       file, strerror(errno));
1141         (void)fflush(stderr);
1142         return;                /* XXX: What about propagating the error? */
1143     }
1144
1145     /* Last resort: update the file's time stamps in the traditional way.
1146      * XXX: This doesn't work for empty files, which are sometimes used
1147      * as marker files. */
1148     if (read(fd, &c, 1) == 1) {
1149         (void)lseek(fd, 0, SEEK_SET);
1150         while (write(fd, &c, 1) == -1 && errno == EAGAIN)
1151             continue;
1152     }
1153     (void)close(fd);            /* XXX: What about propagating the error? */
1154 }
1155
1156 /* Touch the given target. Called by JobStart when the -t flag was given.
1157  *
1158  * The modification date of the file is changed.
1159  * If the file did not exist, it is created. */
1160 void
1161 Job_Touch(GNode *gn, Boolean silent)
1162 {
1163     if (gn->type & (OP_JOIN|OP_USE|OP_USEBEFORE|OP_EXEC|OP_OPTIONAL|
1164         OP_SPECIAL|OP_PHONY)) {
1165         /* These are "virtual" targets and should not really be created. */
1166         return;
1167     }
1168
1169     if (!silent || !GNode_ShouldExecute(gn)) {
1170         (void)fprintf(stdout, "touch %s\n", gn->name);
1171         (void)fflush(stdout);
1172     }
1173
1174     if (!GNode_ShouldExecute(gn))
1175         return;
1176
1177     if (gn->type & OP_ARCHV) {
1178         Arch_Touch(gn);
1179         return;
1180     }
1181
1182     if (gn->type & OP_LIB) {
1183         Arch_TouchLib(gn);
1184         return;
1185     }
1186
1187     TouchRegular(gn);
1188 }
1189
1190 /* Make sure the given node has all the commands it needs.
1191  *
1192  * The node will have commands from the .DEFAULT rule added to it if it
1193  * needs them.
1194  *
1195  * Input:
1196  *      gn              The target whose commands need verifying
1197  *      abortProc       Function to abort with message
1198  *
1199  * Results:
1200  *      TRUE if the commands list is/was ok.
1201  */
1202 Boolean
1203 Job_CheckCommands(GNode *gn, void (*abortProc)(const char *, ...))
1204 {
1205     if (GNode_IsTarget(gn))
1206         return TRUE;
1207     if (!Lst_IsEmpty(gn->commands))
1208         return TRUE;
1209     if ((gn->type & OP_LIB) && !Lst_IsEmpty(gn->children))
1210         return TRUE;
1211
1212     /*
1213      * No commands. Look for .DEFAULT rule from which we might infer
1214      * commands.
1215      */
1216     if (defaultNode != NULL && !Lst_IsEmpty(defaultNode->commands) &&
1217         !(gn->type & OP_SPECIAL)) {
1218         /*
1219          * The traditional Make only looks for a .DEFAULT if the node was
1220          * never the target of an operator, so that's what we do too.
1221          *
1222          * The .DEFAULT node acts like a transformation rule, in that
1223          * gn also inherits any attributes or sources attached to
1224          * .DEFAULT itself.
1225          */
1226         Make_HandleUse(defaultNode, gn);
1227         Var_Set(IMPSRC, GNode_VarTarget(gn), gn);
1228         return TRUE;
1229     }
1230
1231     Dir_UpdateMTime(gn, FALSE);
1232     if (gn->mtime != 0 || (gn->type & OP_SPECIAL))
1233         return TRUE;
1234
1235     /*
1236      * The node wasn't the target of an operator.  We have no .DEFAULT
1237      * rule to go on and the target doesn't already exist. There's
1238      * nothing more we can do for this branch. If the -k flag wasn't
1239      * given, we stop in our tracks, otherwise we just don't update
1240      * this node's parents so they never get examined.
1241      */
1242
1243     if (gn->flags & FROM_DEPEND) {
1244         if (!Job_RunTarget(".STALE", gn->fname))
1245             fprintf(stdout, "%s: %s, %d: ignoring stale %s for %s\n",
1246                     progname, gn->fname, gn->lineno, makeDependfile,
1247                     gn->name);
1248         return TRUE;
1249     }
1250
1251     if (gn->type & OP_OPTIONAL) {
1252         (void)fprintf(stdout, "%s: don't know how to make %s (%s)\n",
1253                       progname, gn->name, "ignored");
1254         (void)fflush(stdout);
1255         return TRUE;
1256     }
1257
1258     if (opts.keepgoing) {
1259         (void)fprintf(stdout, "%s: don't know how to make %s (%s)\n",
1260                       progname, gn->name, "continuing");
1261         (void)fflush(stdout);
1262         return FALSE;
1263     }
1264
1265     abortProc("%s: don't know how to make %s. Stop", progname, gn->name);
1266     return FALSE;
1267 }
1268
1269 /* Execute the shell for the given job.
1270  *
1271  * See Job_CatchOutput for handling the output of the shell. */
1272 static void
1273 JobExec(Job *job, char **argv)
1274 {
1275     int cpid;                   /* ID of new child */
1276     sigset_t mask;
1277
1278     job->flags &= ~JOB_TRACED;
1279
1280     if (DEBUG(JOB)) {
1281         int i;
1282
1283         debug_printf("Running %s\n", job->node->name);
1284         debug_printf("\tCommand: ");
1285         for (i = 0; argv[i] != NULL; i++) {
1286             debug_printf("%s ", argv[i]);
1287         }
1288         debug_printf("\n");
1289     }
1290
1291     /*
1292      * Some jobs produce no output and it's disconcerting to have
1293      * no feedback of their running (since they produce no output, the
1294      * banner with their name in it never appears). This is an attempt to
1295      * provide that feedback, even if nothing follows it.
1296      */
1297     if ((lastNode != job->node) && !(job->flags & JOB_SILENT)) {
1298         MESSAGE(stdout, job->node);
1299         lastNode = job->node;
1300     }
1301
1302     /* No interruptions until this job is on the `jobs' list */
1303     JobSigLock(&mask);
1304
1305     /* Pre-emptively mark job running, pid still zero though */
1306     job->status = JOB_ST_RUNNING;
1307
1308     cpid = vFork();
1309     if (cpid == -1)
1310         Punt("Cannot vfork: %s", strerror(errno));
1311
1312     if (cpid == 0) {
1313         /* Child */
1314         sigset_t tmask;
1315
1316 #ifdef USE_META
1317         if (useMeta) {
1318             meta_job_child(job);
1319         }
1320 #endif
1321         /*
1322          * Reset all signal handlers; this is necessary because we also
1323          * need to unblock signals before we exec(2).
1324          */
1325         JobSigReset();
1326
1327         /* Now unblock signals */
1328         sigemptyset(&tmask);
1329         JobSigUnlock(&tmask);
1330
1331         /*
1332          * Must duplicate the input stream down to the child's input and
1333          * reset it to the beginning (again). Since the stream was marked
1334          * close-on-exec, we must clear that bit in the new input.
1335          */
1336         if (dup2(fileno(job->cmdFILE), 0) == -1)
1337             execDie("dup2", "job->cmdFILE");
1338         if (fcntl(0, F_SETFD, 0) == -1)
1339             execDie("fcntl clear close-on-exec", "stdin");
1340         if (lseek(0, 0, SEEK_SET) == -1)
1341             execDie("lseek to 0", "stdin");
1342
1343         if (Always_pass_job_queue ||
1344             (job->node->type & (OP_MAKE | OP_SUBMAKE))) {
1345             /*
1346              * Pass job token pipe to submakes.
1347              */
1348             if (fcntl(tokenWaitJob.inPipe, F_SETFD, 0) == -1)
1349                 execDie("clear close-on-exec", "tokenWaitJob.inPipe");
1350             if (fcntl(tokenWaitJob.outPipe, F_SETFD, 0) == -1)
1351                 execDie("clear close-on-exec", "tokenWaitJob.outPipe");
1352         }
1353
1354         /*
1355          * Set up the child's output to be routed through the pipe
1356          * we've created for it.
1357          */
1358         if (dup2(job->outPipe, 1) == -1)
1359             execDie("dup2", "job->outPipe");
1360
1361         /*
1362          * The output channels are marked close on exec. This bit was
1363          * duplicated by the dup2(on some systems), so we have to clear
1364          * it before routing the shell's error output to the same place as
1365          * its standard output.
1366          */
1367         if (fcntl(1, F_SETFD, 0) == -1)
1368             execDie("clear close-on-exec", "stdout");
1369         if (dup2(1, 2) == -1)
1370             execDie("dup2", "1, 2");
1371
1372         /*
1373          * We want to switch the child into a different process family so
1374          * we can kill it and all its descendants in one fell swoop,
1375          * by killing its process family, but not commit suicide.
1376          */
1377 #if defined(HAVE_SETPGID)
1378         (void)setpgid(0, getpid());
1379 #else
1380 #if defined(HAVE_SETSID)
1381         /* XXX: dsl - I'm sure this should be setpgrp()... */
1382         (void)setsid();
1383 #else
1384         (void)setpgrp(0, getpid());
1385 #endif
1386 #endif
1387
1388         Var_ExportVars();
1389
1390         (void)execv(shellPath, argv);
1391         execDie("exec", shellPath);
1392     }
1393
1394     /* Parent, continuing after the child exec */
1395     job->pid = cpid;
1396
1397     Trace_Log(JOBSTART, job);
1398
1399 #ifdef USE_META
1400     if (useMeta) {
1401         meta_job_parent(job, cpid);
1402     }
1403 #endif
1404
1405     /*
1406      * Set the current position in the buffer to the beginning
1407      * and mark another stream to watch in the outputs mask
1408      */
1409     job->curPos = 0;
1410
1411     watchfd(job);
1412
1413     if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
1414         (void)fclose(job->cmdFILE);
1415         job->cmdFILE = NULL;
1416     }
1417
1418     /*
1419      * Now the job is actually running, add it to the table.
1420      */
1421     if (DEBUG(JOB)) {
1422         debug_printf("JobExec(%s): pid %d added to jobs table\n",
1423                      job->node->name, job->pid);
1424         job_table_dump("job started");
1425     }
1426     JobSigUnlock(&mask);
1427 }
1428
1429 /* Create the argv needed to execute the shell for a given job. */
1430 static void
1431 JobMakeArgv(Job *job, char **argv)
1432 {
1433     int argc;
1434     static char args[10];       /* For merged arguments */
1435
1436     argv[0] = UNCONST(shellName);
1437     argc = 1;
1438
1439     if ((commandShell->exit && commandShell->exit[0] != '-') ||
1440         (commandShell->echo && commandShell->echo[0] != '-'))
1441     {
1442         /*
1443          * At least one of the flags doesn't have a minus before it, so
1444          * merge them together. Have to do this because the *(&(@*#*&#$#
1445          * Bourne shell thinks its second argument is a file to source.
1446          * Grrrr. Note the ten-character limitation on the combined arguments.
1447          */
1448         (void)snprintf(args, sizeof args, "-%s%s",
1449                       ((job->flags & JOB_IGNERR) ? "" :
1450                        (commandShell->exit ? commandShell->exit : "")),
1451                       ((job->flags & JOB_SILENT) ? "" :
1452                        (commandShell->echo ? commandShell->echo : "")));
1453
1454         if (args[1]) {
1455             argv[argc] = args;
1456             argc++;
1457         }
1458     } else {
1459         if (!(job->flags & JOB_IGNERR) && commandShell->exit) {
1460             argv[argc] = UNCONST(commandShell->exit);
1461             argc++;
1462         }
1463         if (!(job->flags & JOB_SILENT) && commandShell->echo) {
1464             argv[argc] = UNCONST(commandShell->echo);
1465             argc++;
1466         }
1467     }
1468     argv[argc] = NULL;
1469 }
1470
1471 /*-
1472  *-----------------------------------------------------------------------
1473  * JobStart  --
1474  *      Start a target-creation process going for the target described
1475  *      by the graph node gn.
1476  *
1477  * Input:
1478  *      gn              target to create
1479  *      flags           flags for the job to override normal ones.
1480  *      previous        The previous Job structure for this node, if any.
1481  *
1482  * Results:
1483  *      JOB_ERROR if there was an error in the commands, JOB_FINISHED
1484  *      if there isn't actually anything left to do for the job and
1485  *      JOB_RUNNING if the job has been started.
1486  *
1487  * Side Effects:
1488  *      A new Job node is created and added to the list of running
1489  *      jobs. PMake is forked and a child shell created.
1490  *
1491  * NB: The return value is ignored by everyone.
1492  *-----------------------------------------------------------------------
1493  */
1494 static JobStartResult
1495 JobStart(GNode *gn, JobFlags flags)
1496 {
1497     Job *job;                   /* new job descriptor */
1498     char *argv[10];             /* Argument vector to shell */
1499     Boolean cmdsOK;             /* true if the nodes commands were all right */
1500     Boolean noExec;             /* Set true if we decide not to run the job */
1501     int tfd;                    /* File descriptor to the temp file */
1502
1503     for (job = job_table; job < job_table_end; job++) {
1504         if (job->status == JOB_ST_FREE)
1505             break;
1506     }
1507     if (job >= job_table_end)
1508         Punt("JobStart no job slots vacant");
1509
1510     memset(job, 0, sizeof *job);
1511     job->node = gn;
1512     job->tailCmds = NULL;
1513     job->status = JOB_ST_SET_UP;
1514
1515     if (gn->type & OP_SPECIAL)
1516         flags |= JOB_SPECIAL;
1517     if (Targ_Ignore(gn))
1518         flags |= JOB_IGNERR;
1519     if (Targ_Silent(gn))
1520         flags |= JOB_SILENT;
1521     job->flags = flags;
1522
1523     /*
1524      * Check the commands now so any attributes from .DEFAULT have a chance
1525      * to migrate to the node
1526      */
1527     cmdsOK = Job_CheckCommands(gn, Error);
1528
1529     job->inPollfd = NULL;
1530     /*
1531      * If the -n flag wasn't given, we open up OUR (not the child's)
1532      * temporary file to stuff commands in it. The thing is rd/wr so we don't
1533      * need to reopen it to feed it to the shell. If the -n flag *was* given,
1534      * we just set the file to be stdout. Cute, huh?
1535      */
1536     if (((gn->type & OP_MAKE) && !opts.noRecursiveExecute) ||
1537         (!opts.noExecute && !opts.touchFlag)) {
1538         /*
1539          * tfile is the name of a file into which all shell commands are
1540          * put. It is removed before the child shell is executed, unless
1541          * DEBUG(SCRIPT) is set.
1542          */
1543         char *tfile;
1544         sigset_t mask;
1545         /*
1546          * We're serious here, but if the commands were bogus, we're
1547          * also dead...
1548          */
1549         if (!cmdsOK) {
1550             PrintOnError(gn, NULL);     /* provide some clue */
1551             DieHorribly();
1552         }
1553
1554         JobSigLock(&mask);
1555         tfd = mkTempFile(TMPPAT, &tfile);
1556         if (!DEBUG(SCRIPT))
1557             (void)eunlink(tfile);
1558         JobSigUnlock(&mask);
1559
1560         job->cmdFILE = fdopen(tfd, "w+");
1561         if (job->cmdFILE == NULL)
1562             Punt("Could not fdopen %s", tfile);
1563
1564         (void)fcntl(fileno(job->cmdFILE), F_SETFD, FD_CLOEXEC);
1565         /*
1566          * Send the commands to the command file, flush all its buffers then
1567          * rewind and remove the thing.
1568          */
1569         noExec = FALSE;
1570
1571 #ifdef USE_META
1572         if (useMeta) {
1573             meta_job_start(job, gn);
1574             if (Targ_Silent(gn))        /* might have changed */
1575                 job->flags |= JOB_SILENT;
1576         }
1577 #endif
1578         /*
1579          * We can do all the commands at once. hooray for sanity
1580          */
1581         numCommands = 0;
1582         JobPrintCommands(job);
1583
1584         /*
1585          * If we didn't print out any commands to the shell script,
1586          * there's not much point in executing the shell, is there?
1587          */
1588         if (numCommands == 0) {
1589             noExec = TRUE;
1590         }
1591
1592         free(tfile);
1593     } else if (!GNode_ShouldExecute(gn)) {
1594         /*
1595          * Not executing anything -- just print all the commands to stdout
1596          * in one fell swoop. This will still set up job->tailCmds correctly.
1597          */
1598         if (lastNode != gn) {
1599             MESSAGE(stdout, gn);
1600             lastNode = gn;
1601         }
1602         job->cmdFILE = stdout;
1603         /*
1604          * Only print the commands if they're ok, but don't die if they're
1605          * not -- just let the user know they're bad and keep going. It
1606          * doesn't do any harm in this case and may do some good.
1607          */
1608         if (cmdsOK)
1609             JobPrintCommands(job);
1610         /*
1611          * Don't execute the shell, thank you.
1612          */
1613         noExec = TRUE;
1614     } else {
1615         /*
1616          * Just touch the target and note that no shell should be executed.
1617          * Set cmdFILE to stdout to make life easier. Check the commands, too,
1618          * but don't die if they're no good -- it does no harm to keep working
1619          * up the graph.
1620          */
1621         job->cmdFILE = stdout;
1622         Job_Touch(gn, job->flags & JOB_SILENT);
1623         noExec = TRUE;
1624     }
1625     /* Just in case it isn't already... */
1626     (void)fflush(job->cmdFILE);
1627
1628     /*
1629      * If we're not supposed to execute a shell, don't.
1630      */
1631     if (noExec) {
1632         if (!(job->flags & JOB_SPECIAL))
1633             Job_TokenReturn();
1634         /*
1635          * Unlink and close the command file if we opened one
1636          */
1637         if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
1638             (void)fclose(job->cmdFILE);
1639             job->cmdFILE = NULL;
1640         }
1641
1642         /*
1643          * We only want to work our way up the graph if we aren't here because
1644          * the commands for the job were no good.
1645          */
1646         if (cmdsOK && aborting == ABORT_NONE) {
1647             JobSaveCommands(job);
1648             job->node->made = MADE;
1649             Make_Update(job->node);
1650         }
1651         job->status = JOB_ST_FREE;
1652         return cmdsOK ? JOB_FINISHED : JOB_ERROR;
1653     }
1654
1655     /*
1656      * Set up the control arguments to the shell. This is based on the flags
1657      * set earlier for this job.
1658      */
1659     JobMakeArgv(job, argv);
1660
1661     /* Create the pipe by which we'll get the shell's output.  */
1662     JobCreatePipe(job, 3);
1663
1664     JobExec(job, argv);
1665     return JOB_RUNNING;
1666 }
1667
1668 /* Print the output of the shell command, skipping the noPrint command of
1669  * the shell, if any. */
1670 static char *
1671 JobOutput(Job *job, char *cp, char *endp)
1672 {
1673     char *ecp;
1674
1675     if (commandShell->noPrint == NULL || commandShell->noPrint[0] == '\0')
1676         return cp;
1677
1678     while ((ecp = strstr(cp, commandShell->noPrint)) != NULL) {
1679         if (ecp != cp) {
1680             *ecp = '\0';
1681             /*
1682              * The only way there wouldn't be a newline after
1683              * this line is if it were the last in the buffer.
1684              * however, since the non-printable comes after it,
1685              * there must be a newline, so we don't print one.
1686              */
1687             (void)fprintf(stdout, "%s", cp);
1688             (void)fflush(stdout);
1689         }
1690         cp = ecp + commandShell->noPrintLen;
1691         if (cp != endp) {
1692             /*
1693              * Still more to print, look again after skipping
1694              * the whitespace following the non-printable
1695              * command....
1696              */
1697             cp++;
1698             pp_skip_whitespace(&cp);
1699         } else {
1700             return cp;
1701         }
1702     }
1703     return cp;
1704 }
1705
1706 /*
1707  * This function is called whenever there is something to read on the pipe.
1708  * We collect more output from the given job and store it in the job's
1709  * outBuf. If this makes up a line, we print it tagged by the job's
1710  * identifier, as necessary.
1711  *
1712  * In the output of the shell, the 'noPrint' lines are removed. If the
1713  * command is not alone on the line (the character after it is not \0 or
1714  * \n), we do print whatever follows it.
1715  *
1716  * Input:
1717  *      job             the job whose output needs printing
1718  *      finish          TRUE if this is the last time we'll be called
1719  *                      for this job
1720  */
1721 static void
1722 JobDoOutput(Job *job, Boolean finish)
1723 {
1724     Boolean gotNL = FALSE;      /* true if got a newline */
1725     Boolean fbuf;               /* true if our buffer filled up */
1726     size_t nr;                  /* number of bytes read */
1727     size_t i;                   /* auxiliary index into outBuf */
1728     size_t max;                 /* limit for i (end of current data) */
1729     ssize_t nRead;              /* (Temporary) number of bytes read */
1730
1731     /*
1732      * Read as many bytes as will fit in the buffer.
1733      */
1734 again:
1735     gotNL = FALSE;
1736     fbuf = FALSE;
1737
1738     nRead = read(job->inPipe, &job->outBuf[job->curPos],
1739                  JOB_BUFSIZE - job->curPos);
1740     if (nRead < 0) {
1741         if (errno == EAGAIN)
1742             return;
1743         if (DEBUG(JOB)) {
1744             perror("JobDoOutput(piperead)");
1745         }
1746         nr = 0;
1747     } else {
1748         nr = (size_t)nRead;
1749     }
1750
1751     /*
1752      * If we hit the end-of-file (the job is dead), we must flush its
1753      * remaining output, so pretend we read a newline if there's any
1754      * output remaining in the buffer.
1755      * Also clear the 'finish' flag so we stop looping.
1756      */
1757     if (nr == 0 && job->curPos != 0) {
1758         job->outBuf[job->curPos] = '\n';
1759         nr = 1;
1760         finish = FALSE;
1761     } else if (nr == 0) {
1762         finish = FALSE;
1763     }
1764
1765     /*
1766      * Look for the last newline in the bytes we just got. If there is
1767      * one, break out of the loop with 'i' as its index and gotNL set
1768      * TRUE.
1769      */
1770     max = job->curPos + nr;
1771     for (i = job->curPos + nr - 1; i >= job->curPos && i != (size_t)-1; i--) {
1772         if (job->outBuf[i] == '\n') {
1773             gotNL = TRUE;
1774             break;
1775         } else if (job->outBuf[i] == '\0') {
1776             /*
1777              * Why?
1778              */
1779             job->outBuf[i] = ' ';
1780         }
1781     }
1782
1783     if (!gotNL) {
1784         job->curPos += nr;
1785         if (job->curPos == JOB_BUFSIZE) {
1786             /*
1787              * If we've run out of buffer space, we have no choice
1788              * but to print the stuff. sigh.
1789              */
1790             fbuf = TRUE;
1791             i = job->curPos;
1792         }
1793     }
1794     if (gotNL || fbuf) {
1795         /*
1796          * Need to send the output to the screen. Null terminate it
1797          * first, overwriting the newline character if there was one.
1798          * So long as the line isn't one we should filter (according
1799          * to the shell description), we print the line, preceded
1800          * by a target banner if this target isn't the same as the
1801          * one for which we last printed something.
1802          * The rest of the data in the buffer are then shifted down
1803          * to the start of the buffer and curPos is set accordingly.
1804          */
1805         job->outBuf[i] = '\0';
1806         if (i >= job->curPos) {
1807             char *cp;
1808
1809             cp = JobOutput(job, job->outBuf, &job->outBuf[i]);
1810
1811             /*
1812              * There's still more in that thar buffer. This time, though,
1813              * we know there's no newline at the end, so we add one of
1814              * our own free will.
1815              */
1816             if (*cp != '\0') {
1817                 if (!opts.beSilent && job->node != lastNode) {
1818                     MESSAGE(stdout, job->node);
1819                     lastNode = job->node;
1820                 }
1821 #ifdef USE_META
1822                 if (useMeta) {
1823                     meta_job_output(job, cp, gotNL ? "\n" : "");
1824                 }
1825 #endif
1826                 (void)fprintf(stdout, "%s%s", cp, gotNL ? "\n" : "");
1827                 (void)fflush(stdout);
1828             }
1829         }
1830         /*
1831          * max is the last offset still in the buffer. Move any remaining
1832          * characters to the start of the buffer and update the end marker
1833          * curPos.
1834          */
1835         if (i < max) {
1836             (void)memmove(job->outBuf, &job->outBuf[i + 1], max - (i + 1));
1837             job->curPos = max - (i + 1);
1838         } else {
1839             assert(i == max);
1840             job->curPos = 0;
1841         }
1842     }
1843     if (finish) {
1844         /*
1845          * If the finish flag is true, we must loop until we hit
1846          * end-of-file on the pipe. This is guaranteed to happen
1847          * eventually since the other end of the pipe is now closed
1848          * (we closed it explicitly and the child has exited). When
1849          * we do get an EOF, finish will be set FALSE and we'll fall
1850          * through and out.
1851          */
1852         goto again;
1853     }
1854 }
1855
1856 static void
1857 JobRun(GNode *targ)
1858 {
1859 #if 0
1860     /*
1861      * Unfortunately it is too complicated to run .BEGIN, .END, and
1862      * .INTERRUPT job in the parallel job module.  As of 2020-09-25,
1863      * unit-tests/deptgt-end-jobs.mk hangs in an endless loop.
1864      *
1865      * Running these jobs in compat mode also guarantees that these
1866      * jobs do not overlap with other unrelated jobs.
1867      */
1868     List *lst = Lst_New();
1869     Lst_Append(lst, targ);
1870     (void)Make_Run(lst);
1871     Lst_Destroy(lst, NULL);
1872     JobStart(targ, JOB_SPECIAL);
1873     while (jobTokensRunning) {
1874         Job_CatchOutput();
1875     }
1876 #else
1877     Compat_Make(targ, targ);
1878     if (targ->made == ERROR) {
1879         PrintOnError(targ, "\n\nStop.");
1880         exit(1);
1881     }
1882 #endif
1883 }
1884
1885 /* Handle the exit of a child. Called from Make_Make.
1886  *
1887  * The job descriptor is removed from the list of children.
1888  *
1889  * Notes:
1890  *      We do waits, blocking or not, according to the wisdom of our
1891  *      caller, until there are no more children to report. For each
1892  *      job, call JobFinish to finish things off.
1893  */
1894 void
1895 Job_CatchChildren(void)
1896 {
1897     int pid;                    /* pid of dead child */
1898     WAIT_T status;              /* Exit/termination status */
1899
1900     /*
1901      * Don't even bother if we know there's no one around.
1902      */
1903     if (jobTokensRunning == 0)
1904         return;
1905
1906     while ((pid = waitpid((pid_t) -1, &status, WNOHANG | WUNTRACED)) > 0) {
1907         DEBUG2(JOB, "Process %d exited/stopped status %x.\n", pid,
1908           WAIT_STATUS(status));
1909         JobReapChild(pid, status, TRUE);
1910     }
1911 }
1912
1913 /*
1914  * It is possible that wait[pid]() was called from elsewhere,
1915  * this lets us reap jobs regardless.
1916  */
1917 void
1918 JobReapChild(pid_t pid, WAIT_T status, Boolean isJobs)
1919 {
1920     Job *job;                   /* job descriptor for dead child */
1921
1922     /*
1923      * Don't even bother if we know there's no one around.
1924      */
1925     if (jobTokensRunning == 0)
1926         return;
1927
1928     job = JobFindPid(pid, JOB_ST_RUNNING, isJobs);
1929     if (job == NULL) {
1930         if (isJobs) {
1931             if (!lurking_children)
1932                 Error("Child (%d) status %x not in table?", pid, status);
1933         }
1934         return;                         /* not ours */
1935     }
1936     if (WIFSTOPPED(status)) {
1937         DEBUG2(JOB, "Process %d (%s) stopped.\n", job->pid, job->node->name);
1938         if (!make_suspended) {
1939             switch (WSTOPSIG(status)) {
1940             case SIGTSTP:
1941                 (void)printf("*** [%s] Suspended\n", job->node->name);
1942                 break;
1943             case SIGSTOP:
1944                 (void)printf("*** [%s] Stopped\n", job->node->name);
1945                 break;
1946             default:
1947                 (void)printf("*** [%s] Stopped -- signal %d\n",
1948                              job->node->name, WSTOPSIG(status));
1949             }
1950             job->suspended = TRUE;
1951         }
1952         (void)fflush(stdout);
1953         return;
1954     }
1955
1956     job->status = JOB_ST_FINISHED;
1957     job->exit_status = WAIT_STATUS(status);
1958
1959     JobFinish(job, status);
1960 }
1961
1962 /* Catch the output from our children, if we're using pipes do so. Otherwise
1963  * just block time until we get a signal(most likely a SIGCHLD) since there's
1964  * no point in just spinning when there's nothing to do and the reaping of a
1965  * child can wait for a while. */
1966 void
1967 Job_CatchOutput(void)
1968 {
1969     int nready;
1970     Job *job;
1971     unsigned int i;
1972
1973     (void)fflush(stdout);
1974
1975     /* The first fd in the list is the job token pipe */
1976     do {
1977         nready = poll(fds + 1 - wantToken, nfds - 1 + wantToken, POLL_MSEC);
1978     } while (nready < 0 && errno == EINTR);
1979
1980     if (nready < 0)
1981         Punt("poll: %s", strerror(errno));
1982
1983     if (nready > 0 && readyfd(&childExitJob)) {
1984         char token = 0;
1985         ssize_t count;
1986         count = read(childExitJob.inPipe, &token, 1);
1987         switch (count) {
1988         case 0:
1989             Punt("unexpected eof on token pipe");
1990         case -1:
1991             Punt("token pipe read: %s", strerror(errno));
1992         case 1:
1993             if (token == DO_JOB_RESUME[0])
1994                 /* Complete relay requested from our SIGCONT handler */
1995                 JobRestartJobs();
1996             break;
1997         default:
1998             abort();
1999         }
2000         nready--;
2001     }
2002
2003     Job_CatchChildren();
2004     if (nready == 0)
2005         return;
2006
2007     for (i = npseudojobs * nfds_per_job(); i < nfds; i++) {
2008         if (!fds[i].revents)
2009             continue;
2010         job = jobfds[i];
2011         if (job->status == JOB_ST_RUNNING)
2012             JobDoOutput(job, FALSE);
2013 #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
2014         /*
2015          * With meta mode, we may have activity on the job's filemon
2016          * descriptor too, which at the moment is any pollfd other than
2017          * job->inPollfd.
2018          */
2019         if (useMeta && job->inPollfd != &fds[i]) {
2020             if (meta_job_event(job) <= 0) {
2021                 fds[i].events = 0; /* never mind */
2022             }
2023         }
2024 #endif
2025         if (--nready == 0)
2026             return;
2027     }
2028 }
2029
2030 /* Start the creation of a target. Basically a front-end for JobStart used by
2031  * the Make module. */
2032 void
2033 Job_Make(GNode *gn)
2034 {
2035     (void)JobStart(gn, JOB_NONE);
2036 }
2037
2038 void
2039 Shell_Init(void)
2040 {
2041     if (shellPath == NULL) {
2042         /*
2043          * We are using the default shell, which may be an absolute
2044          * path if DEFSHELL_CUSTOM is defined.
2045          */
2046         shellName = commandShell->name;
2047 #ifdef DEFSHELL_CUSTOM
2048         if (*shellName == '/') {
2049             shellPath = shellName;
2050             shellName = strrchr(shellPath, '/');
2051             shellName++;
2052         } else
2053 #endif
2054         shellPath = str_concat3(_PATH_DEFSHELLDIR, "/", shellName);
2055     }
2056     Var_SetWithFlags(".SHELL", shellPath, VAR_CMDLINE, VAR_SET_READONLY);
2057     if (commandShell->exit == NULL) {
2058         commandShell->exit = "";
2059     }
2060     if (commandShell->echo == NULL) {
2061         commandShell->echo = "";
2062     }
2063     if (commandShell->hasErrCtl && commandShell->exit[0] != '\0') {
2064         if (shellErrFlag &&
2065             strcmp(commandShell->exit, &shellErrFlag[1]) != 0) {
2066             free(shellErrFlag);
2067             shellErrFlag = NULL;
2068         }
2069         if (!shellErrFlag) {
2070             size_t n = strlen(commandShell->exit) + 2;
2071
2072             shellErrFlag = bmake_malloc(n);
2073             if (shellErrFlag) {
2074                 snprintf(shellErrFlag, n, "-%s", commandShell->exit);
2075             }
2076         }
2077     } else if (shellErrFlag) {
2078         free(shellErrFlag);
2079         shellErrFlag = NULL;
2080     }
2081 }
2082
2083 /* Return the string literal that is used in the current command shell
2084  * to produce a newline character. */
2085 const char *
2086 Shell_GetNewline(void)
2087 {
2088     return commandShell->newline;
2089 }
2090
2091 void
2092 Job_SetPrefix(void)
2093 {
2094     if (targPrefix) {
2095         free(targPrefix);
2096     } else if (!Var_Exists(MAKE_JOB_PREFIX, VAR_GLOBAL)) {
2097         Var_Set(MAKE_JOB_PREFIX, "---", VAR_GLOBAL);
2098     }
2099
2100     (void)Var_Subst("${" MAKE_JOB_PREFIX "}",
2101                     VAR_GLOBAL, VARE_WANTRES, &targPrefix);
2102     /* TODO: handle errors */
2103 }
2104
2105 /* Initialize the process module. */
2106 void
2107 Job_Init(void)
2108 {
2109     Job_SetPrefix();
2110     /* Allocate space for all the job info */
2111     job_table = bmake_malloc((size_t)opts.maxJobs * sizeof *job_table);
2112     memset(job_table, 0, (size_t)opts.maxJobs * sizeof *job_table);
2113     job_table_end = job_table + opts.maxJobs;
2114     wantToken = 0;
2115
2116     aborting = ABORT_NONE;
2117     errors = 0;
2118
2119     lastNode = NULL;
2120
2121     Always_pass_job_queue = GetBooleanVar(MAKE_ALWAYS_PASS_JOB_QUEUE,
2122                                        Always_pass_job_queue);
2123
2124     Job_error_token = GetBooleanVar(MAKE_JOB_ERROR_TOKEN, Job_error_token);
2125
2126
2127     /*
2128      * There is a non-zero chance that we already have children.
2129      * eg after 'make -f- <<EOF'
2130      * Since their termination causes a 'Child (pid) not in table' message,
2131      * Collect the status of any that are already dead, and suppress the
2132      * error message if there are any undead ones.
2133      */
2134     for (;;) {
2135         int rval, status;
2136         rval = waitpid((pid_t) -1, &status, WNOHANG);
2137         if (rval > 0)
2138             continue;
2139         if (rval == 0)
2140             lurking_children = TRUE;
2141         break;
2142     }
2143
2144     Shell_Init();
2145
2146     JobCreatePipe(&childExitJob, 3);
2147
2148     /* Preallocate enough for the maximum number of jobs.  */
2149     fds = bmake_malloc(sizeof *fds *
2150         (npseudojobs + (size_t)opts.maxJobs) * nfds_per_job());
2151     jobfds = bmake_malloc(sizeof *jobfds *
2152         (npseudojobs + (size_t)opts.maxJobs) * nfds_per_job());
2153
2154     /* These are permanent entries and take slots 0 and 1 */
2155     watchfd(&tokenWaitJob);
2156     watchfd(&childExitJob);
2157
2158     sigemptyset(&caught_signals);
2159     /*
2160      * Install a SIGCHLD handler.
2161      */
2162     (void)bmake_signal(SIGCHLD, JobChildSig);
2163     sigaddset(&caught_signals, SIGCHLD);
2164
2165 #define ADDSIG(s,h)                             \
2166     if (bmake_signal(s, SIG_IGN) != SIG_IGN) {  \
2167         sigaddset(&caught_signals, s);          \
2168         (void)bmake_signal(s, h);               \
2169     }
2170
2171     /*
2172      * Catch the four signals that POSIX specifies if they aren't ignored.
2173      * JobPassSig will take care of calling JobInterrupt if appropriate.
2174      */
2175     ADDSIG(SIGINT, JobPassSig_int)
2176     ADDSIG(SIGHUP, JobPassSig_term)
2177     ADDSIG(SIGTERM, JobPassSig_term)
2178     ADDSIG(SIGQUIT, JobPassSig_term)
2179
2180     /*
2181      * There are additional signals that need to be caught and passed if
2182      * either the export system wants to be told directly of signals or if
2183      * we're giving each job its own process group (since then it won't get
2184      * signals from the terminal driver as we own the terminal)
2185      */
2186     ADDSIG(SIGTSTP, JobPassSig_suspend)
2187     ADDSIG(SIGTTOU, JobPassSig_suspend)
2188     ADDSIG(SIGTTIN, JobPassSig_suspend)
2189     ADDSIG(SIGWINCH, JobCondPassSig)
2190     ADDSIG(SIGCONT, JobContinueSig)
2191 #undef ADDSIG
2192
2193     (void)Job_RunTarget(".BEGIN", NULL);
2194     /* Create the .END node now, even though no code in the unit tests
2195      * depends on it.  See also Targ_GetEndNode in Compat_Run. */
2196     (void)Targ_GetEndNode();
2197 }
2198
2199 static void JobSigReset(void)
2200 {
2201 #define DELSIG(s)                                       \
2202     if (sigismember(&caught_signals, s)) {              \
2203         (void)bmake_signal(s, SIG_DFL);                 \
2204     }
2205
2206     DELSIG(SIGINT)
2207     DELSIG(SIGHUP)
2208     DELSIG(SIGQUIT)
2209     DELSIG(SIGTERM)
2210     DELSIG(SIGTSTP)
2211     DELSIG(SIGTTOU)
2212     DELSIG(SIGTTIN)
2213     DELSIG(SIGWINCH)
2214     DELSIG(SIGCONT)
2215 #undef DELSIG
2216     (void)bmake_signal(SIGCHLD, SIG_DFL);
2217 }
2218
2219 /* Find a shell in 'shells' given its name, or return NULL. */
2220 static Shell *
2221 FindShellByName(const char *name)
2222 {
2223     Shell *sh = shells;
2224     const Shell *shellsEnd = sh + sizeof shells / sizeof shells[0];
2225
2226     for (sh = shells; sh < shellsEnd; sh++) {
2227         if (strcmp(name, sh->name) == 0)
2228                 return sh;
2229     }
2230     return NULL;
2231 }
2232
2233 /*-
2234  *-----------------------------------------------------------------------
2235  * Job_ParseShell --
2236  *      Parse a shell specification and set up commandShell, shellPath
2237  *      and shellName appropriately.
2238  *
2239  * Input:
2240  *      line            The shell spec
2241  *
2242  * Results:
2243  *      FALSE if the specification was incorrect.
2244  *
2245  * Side Effects:
2246  *      commandShell points to a Shell structure (either predefined or
2247  *      created from the shell spec), shellPath is the full path of the
2248  *      shell described by commandShell, while shellName is just the
2249  *      final component of shellPath.
2250  *
2251  * Notes:
2252  *      A shell specification consists of a .SHELL target, with dependency
2253  *      operator, followed by a series of blank-separated words. Double
2254  *      quotes can be used to use blanks in words. A backslash escapes
2255  *      anything (most notably a double-quote and a space) and
2256  *      provides the functionality it does in C. Each word consists of
2257  *      keyword and value separated by an equal sign. There should be no
2258  *      unnecessary spaces in the word. The keywords are as follows:
2259  *          name        Name of shell.
2260  *          path        Location of shell.
2261  *          quiet       Command to turn off echoing.
2262  *          echo        Command to turn echoing on
2263  *          filter      Result of turning off echoing that shouldn't be
2264  *                      printed.
2265  *          echoFlag    Flag to turn echoing on at the start
2266  *          errFlag     Flag to turn error checking on at the start
2267  *          hasErrCtl   True if shell has error checking control
2268  *          newline     String literal to represent a newline char
2269  *          check       Command to turn on error checking if hasErrCtl
2270  *                      is TRUE or template of command to echo a command
2271  *                      for which error checking is off if hasErrCtl is
2272  *                      FALSE.
2273  *          ignore      Command to turn off error checking if hasErrCtl
2274  *                      is TRUE or template of command to execute a
2275  *                      command so as to ignore any errors it returns if
2276  *                      hasErrCtl is FALSE.
2277  *
2278  *-----------------------------------------------------------------------
2279  */
2280 Boolean
2281 Job_ParseShell(char *line)
2282 {
2283     Words       wordsList;
2284     char        **words;
2285     char        **argv;
2286     size_t      argc;
2287     char        *path;
2288     Shell       newShell;
2289     Boolean     fullSpec = FALSE;
2290     Shell       *sh;
2291
2292     pp_skip_whitespace(&line);
2293
2294     free(shellArgv);
2295
2296     memset(&newShell, 0, sizeof newShell);
2297
2298     /*
2299      * Parse the specification by keyword
2300      */
2301     wordsList = Str_Words(line, TRUE);
2302     words = wordsList.words;
2303     argc = wordsList.len;
2304     path = wordsList.freeIt;
2305     if (words == NULL) {
2306         Error("Unterminated quoted string [%s]", line);
2307         return FALSE;
2308     }
2309     shellArgv = path;
2310
2311     for (path = NULL, argv = words; argc != 0; argc--, argv++) {
2312         char *arg = *argv;
2313         if (strncmp(arg, "path=", 5) == 0) {
2314             path = arg + 5;
2315         } else if (strncmp(arg, "name=", 5) == 0) {
2316             newShell.name = arg + 5;
2317         } else {
2318             if (strncmp(arg, "quiet=", 6) == 0) {
2319                 newShell.echoOff = arg + 6;
2320             } else if (strncmp(arg, "echo=", 5) == 0) {
2321                 newShell.echoOn = arg + 5;
2322             } else if (strncmp(arg, "filter=", 7) == 0) {
2323                 newShell.noPrint = arg + 7;
2324                 newShell.noPrintLen = strlen(newShell.noPrint);
2325             } else if (strncmp(arg, "echoFlag=", 9) == 0) {
2326                 newShell.echo = arg + 9;
2327             } else if (strncmp(arg, "errFlag=", 8) == 0) {
2328                 newShell.exit = arg + 8;
2329             } else if (strncmp(arg, "hasErrCtl=", 10) == 0) {
2330                 char c = arg[10];
2331                 newShell.hasErrCtl = c == 'Y' || c == 'y' ||
2332                                      c == 'T' || c == 't';
2333             } else if (strncmp(arg, "newline=", 8) == 0) {
2334                 newShell.newline = arg + 8;
2335             } else if (strncmp(arg, "check=", 6) == 0) {
2336                 newShell.errOnOrEcho = arg + 6;
2337             } else if (strncmp(arg, "ignore=", 7) == 0) {
2338                 newShell.errOffOrExecIgnore = arg + 7;
2339             } else if (strncmp(arg, "errout=", 7) == 0) {
2340                 newShell.errExit = arg + 7;
2341             } else if (strncmp(arg, "comment=", 8) == 0) {
2342                 newShell.commentChar = arg[8];
2343             } else {
2344                 Parse_Error(PARSE_FATAL, "Unknown keyword \"%s\"", arg);
2345                 free(words);
2346                 return FALSE;
2347             }
2348             fullSpec = TRUE;
2349         }
2350     }
2351
2352     if (path == NULL) {
2353         /*
2354          * If no path was given, the user wants one of the pre-defined shells,
2355          * yes? So we find the one s/he wants with the help of FindShellByName
2356          * and set things up the right way. shellPath will be set up by
2357          * Shell_Init.
2358          */
2359         if (newShell.name == NULL) {
2360             Parse_Error(PARSE_FATAL, "Neither path nor name specified");
2361             free(words);
2362             return FALSE;
2363         } else {
2364             if ((sh = FindShellByName(newShell.name)) == NULL) {
2365                     Parse_Error(PARSE_WARNING, "%s: No matching shell",
2366                                 newShell.name);
2367                     free(words);
2368                     return FALSE;
2369             }
2370             commandShell = sh;
2371             shellName = newShell.name;
2372             if (shellPath) {
2373                 /* Shell_Init has already been called!  Do it again. */
2374                 free(UNCONST(shellPath));
2375                 shellPath = NULL;
2376                 Shell_Init();
2377             }
2378         }
2379     } else {
2380         /*
2381          * The user provided a path. If s/he gave nothing else (fullSpec is
2382          * FALSE), try and find a matching shell in the ones we know of.
2383          * Else we just take the specification at its word and copy it
2384          * to a new location. In either case, we need to record the
2385          * path the user gave for the shell.
2386          */
2387         shellPath = path;
2388         path = strrchr(path, '/');
2389         if (path == NULL) {
2390             path = UNCONST(shellPath);
2391         } else {
2392             path++;
2393         }
2394         if (newShell.name != NULL) {
2395             shellName = newShell.name;
2396         } else {
2397             shellName = path;
2398         }
2399         if (!fullSpec) {
2400             if ((sh = FindShellByName(shellName)) == NULL) {
2401                     Parse_Error(PARSE_WARNING, "%s: No matching shell",
2402                                 shellName);
2403                     free(words);
2404                     return FALSE;
2405             }
2406             commandShell = sh;
2407         } else {
2408             commandShell = bmake_malloc(sizeof *commandShell);
2409             *commandShell = newShell;
2410         }
2411         /* this will take care of shellErrFlag */
2412         Shell_Init();
2413     }
2414
2415     if (commandShell->echoOn && commandShell->echoOff) {
2416         commandShell->hasEchoCtl = TRUE;
2417     }
2418
2419     if (!commandShell->hasErrCtl) {
2420         if (commandShell->errOnOrEcho == NULL) {
2421             commandShell->errOnOrEcho = "";
2422         }
2423         if (commandShell->errOffOrExecIgnore == NULL) {
2424             commandShell->errOffOrExecIgnore = "%s\n";
2425         }
2426     }
2427
2428     /*
2429      * Do not free up the words themselves, since they might be in use by the
2430      * shell specification.
2431      */
2432     free(words);
2433     return TRUE;
2434 }
2435
2436 /* Handle the receipt of an interrupt.
2437  *
2438  * All children are killed. Another job will be started if the .INTERRUPT
2439  * target is defined.
2440  *
2441  * Input:
2442  *      runINTERRUPT    Non-zero if commands for the .INTERRUPT target
2443  *                      should be executed
2444  *      signo           signal received
2445  */
2446 static void
2447 JobInterrupt(int runINTERRUPT, int signo)
2448 {
2449     Job         *job;           /* job descriptor in that element */
2450     GNode       *interrupt;     /* the node describing the .INTERRUPT target */
2451     sigset_t    mask;
2452     GNode       *gn;
2453
2454     aborting = ABORT_INTERRUPT;
2455
2456     JobSigLock(&mask);
2457
2458     for (job = job_table; job < job_table_end; job++) {
2459         if (job->status != JOB_ST_RUNNING)
2460             continue;
2461
2462         gn = job->node;
2463
2464         JobDeleteTarget(gn);
2465         if (job->pid) {
2466             DEBUG2(JOB, "JobInterrupt passing signal %d to child %d.\n",
2467                    signo, job->pid);
2468             KILLPG(job->pid, signo);
2469         }
2470     }
2471
2472     JobSigUnlock(&mask);
2473
2474     if (runINTERRUPT && !opts.touchFlag) {
2475         interrupt = Targ_FindNode(".INTERRUPT");
2476         if (interrupt != NULL) {
2477             opts.ignoreErrors = FALSE;
2478             JobRun(interrupt);
2479         }
2480     }
2481     Trace_Log(MAKEINTR, NULL);
2482     exit(signo);
2483 }
2484
2485 /* Do the final processing, i.e. run the commands attached to the .END target.
2486  *
2487  * Return the number of errors reported. */
2488 int
2489 Job_Finish(void)
2490 {
2491     GNode *endNode = Targ_GetEndNode();
2492     if (!Lst_IsEmpty(endNode->commands) || !Lst_IsEmpty(endNode->children)) {
2493         if (errors) {
2494             Error("Errors reported so .END ignored");
2495         } else {
2496             JobRun(endNode);
2497         }
2498     }
2499     return errors;
2500 }
2501
2502 /* Clean up any memory used by the jobs module. */
2503 void
2504 Job_End(void)
2505 {
2506 #ifdef CLEANUP
2507     free(shellArgv);
2508 #endif
2509 }
2510
2511 /* Waits for all running jobs to finish and returns.
2512  * Sets 'aborting' to ABORT_WAIT to prevent other jobs from starting. */
2513 void
2514 Job_Wait(void)
2515 {
2516     aborting = ABORT_WAIT;
2517     while (jobTokensRunning != 0) {
2518         Job_CatchOutput();
2519     }
2520     aborting = ABORT_NONE;
2521 }
2522
2523 /* Abort all currently running jobs without handling output or anything.
2524  * This function is to be called only in the event of a major error.
2525  * Most definitely NOT to be called from JobInterrupt.
2526  *
2527  * All children are killed, not just the firstborn. */
2528 void
2529 Job_AbortAll(void)
2530 {
2531     Job         *job;   /* the job descriptor in that element */
2532     WAIT_T      foo;
2533
2534     aborting = ABORT_ERROR;
2535
2536     if (jobTokensRunning) {
2537         for (job = job_table; job < job_table_end; job++) {
2538             if (job->status != JOB_ST_RUNNING)
2539                 continue;
2540             /*
2541              * kill the child process with increasingly drastic signals to make
2542              * darn sure it's dead.
2543              */
2544             KILLPG(job->pid, SIGINT);
2545             KILLPG(job->pid, SIGKILL);
2546         }
2547     }
2548
2549     /*
2550      * Catch as many children as want to report in at first, then give up
2551      */
2552     while (waitpid((pid_t) -1, &foo, WNOHANG) > 0)
2553         continue;
2554 }
2555
2556 /* Tries to restart stopped jobs if there are slots available.
2557  * Called in process context in response to a SIGCONT. */
2558 static void
2559 JobRestartJobs(void)
2560 {
2561     Job *job;
2562
2563     for (job = job_table; job < job_table_end; job++) {
2564         if (job->status == JOB_ST_RUNNING &&
2565             (make_suspended || job->suspended)) {
2566             DEBUG1(JOB, "Restarting stopped job pid %d.\n", job->pid);
2567             if (job->suspended) {
2568                     (void)printf("*** [%s] Continued\n", job->node->name);
2569                     (void)fflush(stdout);
2570             }
2571             job->suspended = FALSE;
2572             if (KILLPG(job->pid, SIGCONT) != 0 && DEBUG(JOB)) {
2573                 debug_printf("Failed to send SIGCONT to %d\n", job->pid);
2574             }
2575         }
2576         if (job->status == JOB_ST_FINISHED)
2577             /* Job exit deferred after calling waitpid() in a signal handler */
2578             JobFinish(job, job->exit_status);
2579     }
2580     make_suspended = FALSE;
2581 }
2582
2583 static void
2584 watchfd(Job *job)
2585 {
2586     if (job->inPollfd != NULL)
2587         Punt("Watching watched job");
2588
2589     fds[nfds].fd = job->inPipe;
2590     fds[nfds].events = POLLIN;
2591     jobfds[nfds] = job;
2592     job->inPollfd = &fds[nfds];
2593     nfds++;
2594 #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
2595     if (useMeta) {
2596         fds[nfds].fd = meta_job_fd(job);
2597         fds[nfds].events = fds[nfds].fd == -1 ? 0 : POLLIN;
2598         jobfds[nfds] = job;
2599         nfds++;
2600     }
2601 #endif
2602 }
2603
2604 static void
2605 clearfd(Job *job)
2606 {
2607     size_t i;
2608     if (job->inPollfd == NULL)
2609         Punt("Unwatching unwatched job");
2610     i = (size_t)(job->inPollfd - fds);
2611     nfds--;
2612 #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
2613     if (useMeta) {
2614         /*
2615          * Sanity check: there should be two fds per job, so the job's
2616          * pollfd number should be even.
2617          */
2618         assert(nfds_per_job() == 2);
2619         if (i % 2)
2620             Punt("odd-numbered fd with meta");
2621         nfds--;
2622     }
2623 #endif
2624     /*
2625      * Move last job in table into hole made by dead job.
2626      */
2627     if (nfds != i) {
2628         fds[i] = fds[nfds];
2629         jobfds[i] = jobfds[nfds];
2630         jobfds[i]->inPollfd = &fds[i];
2631 #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
2632         if (useMeta) {
2633             fds[i + 1] = fds[nfds + 1];
2634             jobfds[i + 1] = jobfds[nfds + 1];
2635         }
2636 #endif
2637     }
2638     job->inPollfd = NULL;
2639 }
2640
2641 static int
2642 readyfd(Job *job)
2643 {
2644     if (job->inPollfd == NULL)
2645         Punt("Polling unwatched job");
2646     return (job->inPollfd->revents & POLLIN) != 0;
2647 }
2648
2649 /* Put a token (back) into the job pipe.
2650  * This allows a make process to start a build job. */
2651 static void
2652 JobTokenAdd(void)
2653 {
2654     char tok = JOB_TOKENS[aborting], tok1;
2655
2656     if (!Job_error_token && aborting == ABORT_ERROR) {
2657         if (jobTokensRunning == 0)
2658             return;
2659         tok = '+';                      /* no error token */
2660     }
2661
2662     /* If we are depositing an error token flush everything else */
2663     while (tok != '+' && read(tokenWaitJob.inPipe, &tok1, 1) == 1)
2664         continue;
2665
2666     DEBUG3(JOB, "(%d) aborting %d, deposit token %c\n",
2667            getpid(), aborting, tok);
2668     while (write(tokenWaitJob.outPipe, &tok, 1) == -1 && errno == EAGAIN)
2669         continue;
2670 }
2671
2672 /* Prep the job token pipe in the root make process. */
2673 void
2674 Job_ServerStart(int max_tokens, int jp_0, int jp_1)
2675 {
2676     int i;
2677     char jobarg[64];
2678
2679     if (jp_0 >= 0 && jp_1 >= 0) {
2680         /* Pipe passed in from parent */
2681         tokenWaitJob.inPipe = jp_0;
2682         tokenWaitJob.outPipe = jp_1;
2683         (void)fcntl(jp_0, F_SETFD, FD_CLOEXEC);
2684         (void)fcntl(jp_1, F_SETFD, FD_CLOEXEC);
2685         return;
2686     }
2687
2688     JobCreatePipe(&tokenWaitJob, 15);
2689
2690     snprintf(jobarg, sizeof jobarg, "%d,%d",
2691             tokenWaitJob.inPipe, tokenWaitJob.outPipe);
2692
2693     Var_Append(MAKEFLAGS, "-J", VAR_GLOBAL);
2694     Var_Append(MAKEFLAGS, jobarg, VAR_GLOBAL);
2695
2696     /*
2697      * Preload the job pipe with one token per job, save the one
2698      * "extra" token for the primary job.
2699      *
2700      * XXX should clip maxJobs against PIPE_BUF -- if max_tokens is
2701      * larger than the write buffer size of the pipe, we will
2702      * deadlock here.
2703      */
2704     for (i = 1; i < max_tokens; i++)
2705         JobTokenAdd();
2706 }
2707
2708 /* Return a withdrawn token to the pool. */
2709 void
2710 Job_TokenReturn(void)
2711 {
2712     jobTokensRunning--;
2713     if (jobTokensRunning < 0)
2714         Punt("token botch");
2715     if (jobTokensRunning || JOB_TOKENS[aborting] != '+')
2716         JobTokenAdd();
2717 }
2718
2719 /* Attempt to withdraw a token from the pool.
2720  *
2721  * If pool is empty, set wantToken so that we wake up when a token is
2722  * released.
2723  *
2724  * Returns TRUE if a token was withdrawn, and FALSE if the pool is currently
2725  * empty. */
2726 Boolean
2727 Job_TokenWithdraw(void)
2728 {
2729     char tok, tok1;
2730     ssize_t count;
2731
2732     wantToken = 0;
2733     DEBUG3(JOB, "Job_TokenWithdraw(%d): aborting %d, running %d\n",
2734            getpid(), aborting, jobTokensRunning);
2735
2736     if (aborting != ABORT_NONE || (jobTokensRunning >= opts.maxJobs))
2737         return FALSE;
2738
2739     count = read(tokenWaitJob.inPipe, &tok, 1);
2740     if (count == 0)
2741         Fatal("eof on job pipe!");
2742     if (count < 0 && jobTokensRunning != 0) {
2743         if (errno != EAGAIN) {
2744             Fatal("job pipe read: %s", strerror(errno));
2745         }
2746         DEBUG1(JOB, "(%d) blocked for token\n", getpid());
2747         return FALSE;
2748     }
2749
2750     if (count == 1 && tok != '+') {
2751         /* make being abvorted - remove any other job tokens */
2752         DEBUG2(JOB, "(%d) aborted by token %c\n", getpid(), tok);
2753         while (read(tokenWaitJob.inPipe, &tok1, 1) == 1)
2754             continue;
2755         /* And put the stopper back */
2756         while (write(tokenWaitJob.outPipe, &tok, 1) == -1 && errno == EAGAIN)
2757             continue;
2758         if (shouldDieQuietly(NULL, 1))
2759             exit(2);
2760         Fatal("A failure has been detected in another branch of the parallel make");
2761     }
2762
2763     if (count == 1 && jobTokensRunning == 0)
2764         /* We didn't want the token really */
2765         while (write(tokenWaitJob.outPipe, &tok, 1) == -1 && errno == EAGAIN)
2766             continue;
2767
2768     jobTokensRunning++;
2769     DEBUG1(JOB, "(%d) withdrew token\n", getpid());
2770     return TRUE;
2771 }
2772
2773 /* Run the named target if found. If a filename is specified, then set that
2774  * to the sources.
2775  *
2776  * Exits if the target fails. */
2777 Boolean
2778 Job_RunTarget(const char *target, const char *fname) {
2779     GNode *gn = Targ_FindNode(target);
2780     if (gn == NULL)
2781         return FALSE;
2782
2783     if (fname)
2784         Var_Set(ALLSRC, fname, gn);
2785
2786     JobRun(gn);
2787     if (gn->made == ERROR) {
2788         PrintOnError(gn, "\n\nStop.");
2789         exit(1);
2790     }
2791     return TRUE;
2792 }
2793
2794 #ifdef USE_SELECT
2795 int
2796 emul_poll(struct pollfd *fd, int nfd, int timeout)
2797 {
2798     fd_set rfds, wfds;
2799     int i, maxfd, nselect, npoll;
2800     struct timeval tv, *tvp;
2801     long usecs;
2802
2803     FD_ZERO(&rfds);
2804     FD_ZERO(&wfds);
2805
2806     maxfd = -1;
2807     for (i = 0; i < nfd; i++) {
2808         fd[i].revents = 0;
2809
2810         if (fd[i].events & POLLIN)
2811             FD_SET(fd[i].fd, &rfds);
2812
2813         if (fd[i].events & POLLOUT)
2814             FD_SET(fd[i].fd, &wfds);
2815
2816         if (fd[i].fd > maxfd)
2817             maxfd = fd[i].fd;
2818     }
2819
2820     if (maxfd >= FD_SETSIZE) {
2821         Punt("Ran out of fd_set slots; "
2822              "recompile with a larger FD_SETSIZE.");
2823     }
2824
2825     if (timeout < 0) {
2826         tvp = NULL;
2827     } else {
2828         usecs = timeout * 1000;
2829         tv.tv_sec = usecs / 1000000;
2830         tv.tv_usec = usecs % 1000000;
2831         tvp = &tv;
2832     }
2833
2834     nselect = select(maxfd + 1, &rfds, &wfds, NULL, tvp);
2835
2836     if (nselect <= 0)
2837         return nselect;
2838
2839     npoll = 0;
2840     for (i = 0; i < nfd; i++) {
2841         if (FD_ISSET(fd[i].fd, &rfds))
2842             fd[i].revents |= POLLIN;
2843
2844         if (FD_ISSET(fd[i].fd, &wfds))
2845             fd[i].revents |= POLLOUT;
2846
2847         if (fd[i].revents)
2848             npoll++;
2849     }
2850
2851     return npoll;
2852 }
2853 #endif /* USE_SELECT */