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