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