xref: /freebsd/contrib/bmake/job.c (revision d9a65c5de1c9f30ae71ce0be8fb88be9d20d216d)
1*d9a65c5dSSimon J. Gerraty /*	$NetBSD: job.c,v 1.489 2025/03/08 20:15:03 rillig Exp $	*/
23955d011SMarcel Moolenaar 
33955d011SMarcel Moolenaar /*
43955d011SMarcel Moolenaar  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
53955d011SMarcel Moolenaar  * All rights reserved.
63955d011SMarcel Moolenaar  *
73955d011SMarcel Moolenaar  * This code is derived from software contributed to Berkeley by
83955d011SMarcel Moolenaar  * Adam de Boor.
93955d011SMarcel Moolenaar  *
103955d011SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
113955d011SMarcel Moolenaar  * modification, are permitted provided that the following conditions
123955d011SMarcel Moolenaar  * are met:
133955d011SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
143955d011SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
153955d011SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
163955d011SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
173955d011SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
183955d011SMarcel Moolenaar  * 3. Neither the name of the University nor the names of its contributors
193955d011SMarcel Moolenaar  *    may be used to endorse or promote products derived from this software
203955d011SMarcel Moolenaar  *    without specific prior written permission.
213955d011SMarcel Moolenaar  *
223955d011SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
233955d011SMarcel Moolenaar  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
243955d011SMarcel Moolenaar  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
253955d011SMarcel Moolenaar  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
263955d011SMarcel Moolenaar  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
273955d011SMarcel Moolenaar  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
283955d011SMarcel Moolenaar  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
293955d011SMarcel Moolenaar  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
303955d011SMarcel Moolenaar  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
313955d011SMarcel Moolenaar  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
323955d011SMarcel Moolenaar  * SUCH DAMAGE.
333955d011SMarcel Moolenaar  */
343955d011SMarcel Moolenaar 
353955d011SMarcel Moolenaar /*
363955d011SMarcel Moolenaar  * Copyright (c) 1988, 1989 by Adam de Boor
373955d011SMarcel Moolenaar  * Copyright (c) 1989 by Berkeley Softworks
383955d011SMarcel Moolenaar  * All rights reserved.
393955d011SMarcel Moolenaar  *
403955d011SMarcel Moolenaar  * This code is derived from software contributed to Berkeley by
413955d011SMarcel Moolenaar  * Adam de Boor.
423955d011SMarcel Moolenaar  *
433955d011SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
443955d011SMarcel Moolenaar  * modification, are permitted provided that the following conditions
453955d011SMarcel Moolenaar  * are met:
463955d011SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
473955d011SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
483955d011SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
493955d011SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
503955d011SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
513955d011SMarcel Moolenaar  * 3. All advertising materials mentioning features or use of this software
523955d011SMarcel Moolenaar  *    must display the following acknowledgement:
533955d011SMarcel Moolenaar  *	This product includes software developed by the University of
543955d011SMarcel Moolenaar  *	California, Berkeley and its contributors.
553955d011SMarcel Moolenaar  * 4. Neither the name of the University nor the names of its contributors
563955d011SMarcel Moolenaar  *    may be used to endorse or promote products derived from this software
573955d011SMarcel Moolenaar  *    without specific prior written permission.
583955d011SMarcel Moolenaar  *
593955d011SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
603955d011SMarcel Moolenaar  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
613955d011SMarcel Moolenaar  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
623955d011SMarcel Moolenaar  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
633955d011SMarcel Moolenaar  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
643955d011SMarcel Moolenaar  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
653955d011SMarcel Moolenaar  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
663955d011SMarcel Moolenaar  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
673955d011SMarcel Moolenaar  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
683955d011SMarcel Moolenaar  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
693955d011SMarcel Moolenaar  * SUCH DAMAGE.
703955d011SMarcel Moolenaar  */
713955d011SMarcel Moolenaar 
72dba7b0efSSimon J. Gerraty /*
73d5e0a182SSimon J. Gerraty  * Create child processes and collect their output.
743955d011SMarcel Moolenaar  *
753955d011SMarcel Moolenaar  * Interface:
76956e45f6SSimon J. Gerraty  *	Job_Init	Called to initialize this module. In addition,
77d5e0a182SSimon J. Gerraty  *			the .BEGIN target is made, including all of its
78dba7b0efSSimon J. Gerraty  *			dependencies before this function returns.
79956e45f6SSimon J. Gerraty  *			Hence, the makefiles must have been parsed
80956e45f6SSimon J. Gerraty  *			before this function is called.
81956e45f6SSimon J. Gerraty  *
82956e45f6SSimon J. Gerraty  *	Job_End		Clean up any memory used.
83956e45f6SSimon J. Gerraty  *
843955d011SMarcel Moolenaar  *	Job_Make	Start the creation of the given target.
853955d011SMarcel Moolenaar  *
86956e45f6SSimon J. Gerraty  *	Job_CatchChildren
87956e45f6SSimon J. Gerraty  *			Check for and handle the termination of any
883955d011SMarcel Moolenaar  *			children. This must be called reasonably
893955d011SMarcel Moolenaar  *			frequently to keep the whole make going at
903955d011SMarcel Moolenaar  *			a decent clip, since job table entries aren't
913955d011SMarcel Moolenaar  *			removed until their process is caught this way.
923955d011SMarcel Moolenaar  *
93956e45f6SSimon J. Gerraty  *	Job_CatchOutput
94956e45f6SSimon J. Gerraty  *			Print any output our children have produced.
953955d011SMarcel Moolenaar  *			Should also be called fairly frequently to
963955d011SMarcel Moolenaar  *			keep the user informed of what's going on.
973955d011SMarcel Moolenaar  *			If no output is waiting, it will block for
983955d011SMarcel Moolenaar  *			a time given by the SEL_* constants, below,
993955d011SMarcel Moolenaar  *			or until output is ready.
1003955d011SMarcel Moolenaar  *
101dba7b0efSSimon J. Gerraty  *	Job_ParseShell	Given a special dependency line with target '.SHELL',
102dba7b0efSSimon J. Gerraty  *			define the shell that is used for the creation
103dba7b0efSSimon J. Gerraty  *			commands in jobs mode.
1043955d011SMarcel Moolenaar  *
105*d9a65c5dSSimon J. Gerraty  *	Job_Finish	Make the .END target. Should only be called when the
1063955d011SMarcel Moolenaar  *			job table is empty.
1073955d011SMarcel Moolenaar  *
108dba7b0efSSimon J. Gerraty  *	Job_AbortAll	Abort all currently running jobs. Do not handle
109dba7b0efSSimon J. Gerraty  *			output or do anything for the jobs, just kill them.
110dba7b0efSSimon J. Gerraty  *			Should only be called in an emergency.
1113955d011SMarcel Moolenaar  *
112956e45f6SSimon J. Gerraty  *	Job_CheckCommands
113956e45f6SSimon J. Gerraty  *			Verify that the commands for a target are
1143955d011SMarcel Moolenaar  *			ok. Provide them if necessary and possible.
1153955d011SMarcel Moolenaar  *
1163955d011SMarcel Moolenaar  *	Job_Touch	Update a target without really updating it.
1173955d011SMarcel Moolenaar  *
1183955d011SMarcel Moolenaar  *	Job_Wait	Wait for all currently-running jobs to finish.
1193955d011SMarcel Moolenaar  */
1203955d011SMarcel Moolenaar 
1213955d011SMarcel Moolenaar #ifdef HAVE_CONFIG_H
1223955d011SMarcel Moolenaar # include "config.h"
1233955d011SMarcel Moolenaar #endif
1243955d011SMarcel Moolenaar #include <sys/types.h>
1253955d011SMarcel Moolenaar #include <sys/stat.h>
1263955d011SMarcel Moolenaar #include <sys/file.h>
1273955d011SMarcel Moolenaar #include <sys/time.h>
1283955d011SMarcel Moolenaar #include "wait.h"
1293955d011SMarcel Moolenaar 
1303955d011SMarcel Moolenaar #include <errno.h>
1313955d011SMarcel Moolenaar #if !defined(USE_SELECT) && defined(HAVE_POLL_H)
1323955d011SMarcel Moolenaar #include <poll.h>
1333955d011SMarcel Moolenaar #else
1343955d011SMarcel Moolenaar #ifndef USE_SELECT			/* no poll.h */
1353955d011SMarcel Moolenaar # define USE_SELECT
1363955d011SMarcel Moolenaar #endif
1373955d011SMarcel Moolenaar #if defined(HAVE_SYS_SELECT_H)
1383955d011SMarcel Moolenaar # include <sys/select.h>
1393955d011SMarcel Moolenaar #endif
1403955d011SMarcel Moolenaar #endif
1413955d011SMarcel Moolenaar #include <signal.h>
1423955d011SMarcel Moolenaar #include <utime.h>
1433955d011SMarcel Moolenaar #if defined(HAVE_SYS_SOCKET_H)
1443955d011SMarcel Moolenaar # include <sys/socket.h>
1453955d011SMarcel Moolenaar #endif
1463955d011SMarcel Moolenaar 
1473955d011SMarcel Moolenaar #include "make.h"
1483955d011SMarcel Moolenaar #include "dir.h"
1493955d011SMarcel Moolenaar #include "job.h"
1503955d011SMarcel Moolenaar #include "pathnames.h"
1513955d011SMarcel Moolenaar #include "trace.h"
152956e45f6SSimon J. Gerraty 
153956e45f6SSimon J. Gerraty /*	"@(#)job.c	8.2 (Berkeley) 3/19/94"	*/
154*d9a65c5dSSimon J. Gerraty MAKE_RCSID("$NetBSD: job.c,v 1.489 2025/03/08 20:15:03 rillig Exp $");
155956e45f6SSimon J. Gerraty 
15606b9b3e0SSimon J. Gerraty /*
15706b9b3e0SSimon J. Gerraty  * A shell defines how the commands are run.  All commands for a target are
158956e45f6SSimon J. Gerraty  * written into a single file, which is then given to the shell to execute
159956e45f6SSimon J. Gerraty  * the commands from it.  The commands are written to the file using a few
160956e45f6SSimon J. Gerraty  * templates for echo control and error control.
161956e45f6SSimon J. Gerraty  *
162956e45f6SSimon J. Gerraty  * The name of the shell is the basename for the predefined shells, such as
163956e45f6SSimon J. Gerraty  * "sh", "csh", "bash".  For custom shells, it is the full pathname, and its
164956e45f6SSimon J. Gerraty  * basename is used to select the type of shell; the longest match wins.
165956e45f6SSimon J. Gerraty  * So /usr/pkg/bin/bash has type sh, /usr/local/bin/tcsh has type csh.
166956e45f6SSimon J. Gerraty  *
167956e45f6SSimon J. Gerraty  * The echoing of command lines is controlled using hasEchoCtl, echoOff,
168956e45f6SSimon J. Gerraty  * echoOn, noPrint and noPrintLen.  When echoOff is executed by the shell, it
169956e45f6SSimon J. Gerraty  * still outputs something, but this something is not interesting, therefore
170956e45f6SSimon J. Gerraty  * it is filtered out using noPrint and noPrintLen.
171956e45f6SSimon J. Gerraty  *
172956e45f6SSimon J. Gerraty  * The error checking for individual commands is controlled using hasErrCtl,
17306b9b3e0SSimon J. Gerraty  * errOn, errOff and runChkTmpl.
174956e45f6SSimon J. Gerraty  *
175dba7b0efSSimon J. Gerraty  * In case a shell doesn't have error control, echoTmpl is a printf template
176dba7b0efSSimon J. Gerraty  * for echoing the command, should echoing be on; runIgnTmpl is another
177dba7b0efSSimon J. Gerraty  * printf template for executing the command while ignoring the return
17806b9b3e0SSimon J. Gerraty  * status. Finally runChkTmpl is a printf template for running the command and
179956e45f6SSimon J. Gerraty  * causing the shell to exit on error. If any of these strings are empty when
180b0c40a00SSimon J. Gerraty  * hasErrCtl is false, the command will be executed anyway as is, and if it
181956e45f6SSimon J. Gerraty  * causes an error, so be it. Any templates set up to echo the command will
182dba7b0efSSimon J. Gerraty  * escape any '$ ` \ "' characters in the command string to avoid unwanted
183dba7b0efSSimon J. Gerraty  * shell code injection, the escaped command is safe to use in double quotes.
184956e45f6SSimon J. Gerraty  *
185956e45f6SSimon J. Gerraty  * The command-line flags "echo" and "exit" also control the behavior.  The
186956e45f6SSimon J. Gerraty  * "echo" flag causes the shell to start echoing commands right away.  The
187956e45f6SSimon J. Gerraty  * "exit" flag causes the shell to exit when an error is detected in one of
188956e45f6SSimon J. Gerraty  * the commands.
189956e45f6SSimon J. Gerraty  */
190956e45f6SSimon J. Gerraty typedef struct Shell {
191956e45f6SSimon J. Gerraty 
19206b9b3e0SSimon J. Gerraty 	/*
19306b9b3e0SSimon J. Gerraty 	 * The name of the shell. For Bourne and C shells, this is used only
19406b9b3e0SSimon J. Gerraty 	 * to find the shell description when used as the single source of a
19506b9b3e0SSimon J. Gerraty 	 * .SHELL target. For user-defined shells, this is the full path of
19606b9b3e0SSimon J. Gerraty 	 * the shell.
19706b9b3e0SSimon J. Gerraty 	 */
198956e45f6SSimon J. Gerraty 	const char *name;
199956e45f6SSimon J. Gerraty 
200b0c40a00SSimon J. Gerraty 	bool hasEchoCtl;	/* whether both echoOff and echoOn are there */
20106b9b3e0SSimon J. Gerraty 	const char *echoOff;	/* command to turn echoing off */
20206b9b3e0SSimon J. Gerraty 	const char *echoOn;	/* command to turn echoing back on */
20306b9b3e0SSimon J. Gerraty 	const char *noPrint;	/* text to skip when printing output from the
204956e45f6SSimon J. Gerraty 				 * shell. This is usually the same as echoOff */
205956e45f6SSimon J. Gerraty 	size_t noPrintLen;	/* length of noPrint command */
206956e45f6SSimon J. Gerraty 
207b0c40a00SSimon J. Gerraty 	bool hasErrCtl;		/* whether error checking can be controlled
20806b9b3e0SSimon J. Gerraty 				 * for individual commands */
20906b9b3e0SSimon J. Gerraty 	const char *errOn;	/* command to turn on error checking */
21006b9b3e0SSimon J. Gerraty 	const char *errOff;	/* command to turn off error checking */
21106b9b3e0SSimon J. Gerraty 
21206b9b3e0SSimon J. Gerraty 	const char *echoTmpl;	/* template to echo a command */
2139f45a3c8SSimon J. Gerraty 	const char *runIgnTmpl;	/* template to run a command without error
2149f45a3c8SSimon J. Gerraty 				 * checking */
2159f45a3c8SSimon J. Gerraty 	const char *runChkTmpl;	/* template to run a command with error
2169f45a3c8SSimon J. Gerraty 				 * checking */
217956e45f6SSimon J. Gerraty 
2189f45a3c8SSimon J. Gerraty 	/*
2199f45a3c8SSimon J. Gerraty 	 * A string literal that results in a newline character when it
2209f45a3c8SSimon J. Gerraty 	 * occurs outside of any 'quote' or "quote" characters.
2219f45a3c8SSimon J. Gerraty 	 */
222956e45f6SSimon J. Gerraty 	const char *newline;
223956e45f6SSimon J. Gerraty 	char commentChar;	/* character used by shell for comment lines */
224956e45f6SSimon J. Gerraty 
22506b9b3e0SSimon J. Gerraty 	const char *echoFlag;	/* shell flag to echo commands */
22606b9b3e0SSimon J. Gerraty 	const char *errFlag;	/* shell flag to exit on error */
227956e45f6SSimon J. Gerraty } Shell;
2283955d011SMarcel Moolenaar 
22906b9b3e0SSimon J. Gerraty typedef struct CommandFlags {
230dba7b0efSSimon J. Gerraty 	/* Whether to echo the command before or instead of running it. */
231b0c40a00SSimon J. Gerraty 	bool echo;
23206b9b3e0SSimon J. Gerraty 
23306b9b3e0SSimon J. Gerraty 	/* Run the command even in -n or -N mode. */
234b0c40a00SSimon J. Gerraty 	bool always;
23506b9b3e0SSimon J. Gerraty 
23606b9b3e0SSimon J. Gerraty 	/*
237b0c40a00SSimon J. Gerraty 	 * true if we turned error checking off before writing the command to
238b0c40a00SSimon J. Gerraty 	 * the commands file and need to turn it back on
23906b9b3e0SSimon J. Gerraty 	 */
240b0c40a00SSimon J. Gerraty 	bool ignerr;
24106b9b3e0SSimon J. Gerraty } CommandFlags;
24206b9b3e0SSimon J. Gerraty 
24306b9b3e0SSimon J. Gerraty /*
24406b9b3e0SSimon J. Gerraty  * Write shell commands to a file.
24506b9b3e0SSimon J. Gerraty  *
24606b9b3e0SSimon J. Gerraty  * TODO: keep track of whether commands are echoed.
24706b9b3e0SSimon J. Gerraty  * TODO: keep track of whether error checking is active.
24806b9b3e0SSimon J. Gerraty  */
24906b9b3e0SSimon J. Gerraty typedef struct ShellWriter {
25006b9b3e0SSimon J. Gerraty 	FILE *f;
25106b9b3e0SSimon J. Gerraty 
25206b9b3e0SSimon J. Gerraty 	/* we've sent 'set -x' */
253b0c40a00SSimon J. Gerraty 	bool xtraced;
25406b9b3e0SSimon J. Gerraty 
25506b9b3e0SSimon J. Gerraty } ShellWriter;
25606b9b3e0SSimon J. Gerraty 
2573955d011SMarcel Moolenaar /*
2589a4bc556SSimon J. Gerraty  * FreeBSD: traditionally .MAKE is not required to
2599a4bc556SSimon J. Gerraty  * pass jobs queue to sub-makes.
2609a4bc556SSimon J. Gerraty  * Use .MAKE.ALWAYS_PASS_JOB_QUEUE=no to disable.
2619a4bc556SSimon J. Gerraty  */
2623b96abbaSSimon J. Gerraty #define MAKE_ALWAYS_PASS_JOB_QUEUE "${.MAKE.ALWAYS_PASS_JOB_QUEUE:U}"
26368c4481aSSimon J. Gerraty static bool Always_pass_job_queue = true;
2642d395cb5SSimon J. Gerraty /*
2652d395cb5SSimon J. Gerraty  * FreeBSD: aborting entire parallel make isn't always
2662d395cb5SSimon J. Gerraty  * desired. When doing tinderbox for example, failure of
2672d395cb5SSimon J. Gerraty  * one architecture should not stop all.
2682d395cb5SSimon J. Gerraty  * We still want to bail on interrupt though.
2692d395cb5SSimon J. Gerraty  */
2703b96abbaSSimon J. Gerraty #define MAKE_JOB_ERROR_TOKEN "${MAKE_JOB_ERROR_TOKEN:U}"
27168c4481aSSimon J. Gerraty static bool Job_error_token = true;
2729a4bc556SSimon J. Gerraty 
273d5e0a182SSimon J. Gerraty /* error handling variables */
27406b9b3e0SSimon J. Gerraty static int job_errors = 0;	/* number of errors reported */
275b0c40a00SSimon J. Gerraty static enum {			/* Why is the make aborting? */
276956e45f6SSimon J. Gerraty 	ABORT_NONE,
277b0c40a00SSimon J. Gerraty 	ABORT_ERROR,		/* Aborted because of an error */
278b0c40a00SSimon J. Gerraty 	ABORT_INTERRUPT,	/* Aborted because it was interrupted */
279956e45f6SSimon J. Gerraty 	ABORT_WAIT		/* Waiting for jobs to finish */
280b0c40a00SSimon J. Gerraty } aborting = ABORT_NONE;
2813955d011SMarcel Moolenaar #define JOB_TOKENS "+EI+"	/* Token to requeue for each abort state */
2823955d011SMarcel Moolenaar 
283d5e0a182SSimon J. Gerraty /* Tracks the number of tokens currently "out" to build jobs. */
2843955d011SMarcel Moolenaar int jobTokensRunning = 0;
2853955d011SMarcel Moolenaar 
2863955d011SMarcel Moolenaar /*
2873955d011SMarcel Moolenaar  * Descriptions for various shells.
2883955d011SMarcel Moolenaar  *
2893955d011SMarcel Moolenaar  * The build environment may set DEFSHELL_INDEX to one of
2903955d011SMarcel Moolenaar  * DEFSHELL_INDEX_SH, DEFSHELL_INDEX_KSH, or DEFSHELL_INDEX_CSH, to
291956e45f6SSimon J. Gerraty  * select one of the predefined shells as the default shell.
2923955d011SMarcel Moolenaar  *
2933955d011SMarcel Moolenaar  * Alternatively, the build environment may set DEFSHELL_CUSTOM to the
2943955d011SMarcel Moolenaar  * name or the full path of a sh-compatible shell, which will be used as
2953955d011SMarcel Moolenaar  * the default shell.
2963955d011SMarcel Moolenaar  *
2973955d011SMarcel Moolenaar  * ".SHELL" lines in Makefiles can choose the default shell from the
298956e45f6SSimon J. Gerraty  * set defined here, or add additional shells.
2993955d011SMarcel Moolenaar  */
3003955d011SMarcel Moolenaar 
3013955d011SMarcel Moolenaar #ifdef DEFSHELL_CUSTOM
3023955d011SMarcel Moolenaar #define DEFSHELL_INDEX_CUSTOM 0
3033955d011SMarcel Moolenaar #define DEFSHELL_INDEX_SH     1
3043955d011SMarcel Moolenaar #define DEFSHELL_INDEX_KSH    2
3053955d011SMarcel Moolenaar #define DEFSHELL_INDEX_CSH    3
306d5e0a182SSimon J. Gerraty #else
3073955d011SMarcel Moolenaar #define DEFSHELL_INDEX_SH     0
3083955d011SMarcel Moolenaar #define DEFSHELL_INDEX_KSH    1
3093955d011SMarcel Moolenaar #define DEFSHELL_INDEX_CSH    2
310d5e0a182SSimon J. Gerraty #endif
3113955d011SMarcel Moolenaar 
3123955d011SMarcel Moolenaar #ifndef DEFSHELL_INDEX
3133955d011SMarcel Moolenaar #define DEFSHELL_INDEX 0	/* DEFSHELL_INDEX_CUSTOM or DEFSHELL_INDEX_SH */
314d5e0a182SSimon J. Gerraty #endif
3153955d011SMarcel Moolenaar 
3163955d011SMarcel Moolenaar static Shell shells[] = {
3173955d011SMarcel Moolenaar #ifdef DEFSHELL_CUSTOM
3183955d011SMarcel Moolenaar     /*
3193955d011SMarcel Moolenaar      * An sh-compatible shell with a non-standard name.
3203955d011SMarcel Moolenaar      *
3213955d011SMarcel Moolenaar      * Keep this in sync with the "sh" description below, but avoid
3223955d011SMarcel Moolenaar      * non-portable features that might not be supplied by all
3233955d011SMarcel Moolenaar      * sh-compatible shells.
3243955d011SMarcel Moolenaar      */
3253955d011SMarcel Moolenaar     {
326956e45f6SSimon J. Gerraty 	DEFSHELL_CUSTOM,	/* .name */
327b0c40a00SSimon J. Gerraty 	false,			/* .hasEchoCtl */
328956e45f6SSimon J. Gerraty 	"",			/* .echoOff */
329956e45f6SSimon J. Gerraty 	"",			/* .echoOn */
330956e45f6SSimon J. Gerraty 	"",			/* .noPrint */
331956e45f6SSimon J. Gerraty 	0,			/* .noPrintLen */
332b0c40a00SSimon J. Gerraty 	false,			/* .hasErrCtl */
33306b9b3e0SSimon J. Gerraty 	"",			/* .errOn */
33406b9b3e0SSimon J. Gerraty 	"",			/* .errOff */
33506b9b3e0SSimon J. Gerraty 	"echo \"%s\"\n",	/* .echoTmpl */
33606b9b3e0SSimon J. Gerraty 	"%s\n",			/* .runIgnTmpl */
33706b9b3e0SSimon J. Gerraty 	"{ %s \n} || exit $?\n", /* .runChkTmpl */
338956e45f6SSimon J. Gerraty 	"'\n'",			/* .newline */
339956e45f6SSimon J. Gerraty 	'#',			/* .commentChar */
34006b9b3e0SSimon J. Gerraty 	"",			/* .echoFlag */
34106b9b3e0SSimon J. Gerraty 	"",			/* .errFlag */
3423955d011SMarcel Moolenaar     },
3433955d011SMarcel Moolenaar #endif /* DEFSHELL_CUSTOM */
3443955d011SMarcel Moolenaar     /*
3453955d011SMarcel Moolenaar      * SH description. Echo control is also possible and, under
3463955d011SMarcel Moolenaar      * sun UNIX anyway, one can even control error checking.
3473955d011SMarcel Moolenaar      */
3483955d011SMarcel Moolenaar     {
349956e45f6SSimon J. Gerraty 	"sh",			/* .name */
350b0c40a00SSimon J. Gerraty 	false,			/* .hasEchoCtl */
351956e45f6SSimon J. Gerraty 	"",			/* .echoOff */
352956e45f6SSimon J. Gerraty 	"",			/* .echoOn */
353956e45f6SSimon J. Gerraty 	"",			/* .noPrint */
354956e45f6SSimon J. Gerraty 	0,			/* .noPrintLen */
355b0c40a00SSimon J. Gerraty 	false,			/* .hasErrCtl */
35606b9b3e0SSimon J. Gerraty 	"",			/* .errOn */
35706b9b3e0SSimon J. Gerraty 	"",			/* .errOff */
35806b9b3e0SSimon J. Gerraty 	"echo \"%s\"\n",	/* .echoTmpl */
35906b9b3e0SSimon J. Gerraty 	"%s\n",			/* .runIgnTmpl */
36006b9b3e0SSimon J. Gerraty 	"{ %s \n} || exit $?\n", /* .runChkTmpl */
361956e45f6SSimon J. Gerraty 	"'\n'",			/* .newline */
362956e45f6SSimon J. Gerraty 	'#',			/* .commentChar*/
3633955d011SMarcel Moolenaar #if defined(MAKE_NATIVE) && defined(__NetBSD__)
36406b9b3e0SSimon J. Gerraty 	/* XXX: -q is not really echoFlag, it's more like noEchoInSysFlag. */
36506b9b3e0SSimon J. Gerraty 	"q",			/* .echoFlag */
3663955d011SMarcel Moolenaar #else
36706b9b3e0SSimon J. Gerraty 	"",			/* .echoFlag */
3683955d011SMarcel Moolenaar #endif
36906b9b3e0SSimon J. Gerraty 	"",			/* .errFlag */
3703955d011SMarcel Moolenaar     },
3713955d011SMarcel Moolenaar     /*
3723955d011SMarcel Moolenaar      * KSH description.
3733955d011SMarcel Moolenaar      */
3743955d011SMarcel Moolenaar     {
375956e45f6SSimon J. Gerraty 	"ksh",			/* .name */
376b0c40a00SSimon J. Gerraty 	true,			/* .hasEchoCtl */
377956e45f6SSimon J. Gerraty 	"set +v",		/* .echoOff */
378956e45f6SSimon J. Gerraty 	"set -v",		/* .echoOn */
379956e45f6SSimon J. Gerraty 	"set +v",		/* .noPrint */
380956e45f6SSimon J. Gerraty 	6,			/* .noPrintLen */
381b0c40a00SSimon J. Gerraty 	false,			/* .hasErrCtl */
38206b9b3e0SSimon J. Gerraty 	"",			/* .errOn */
38306b9b3e0SSimon J. Gerraty 	"",			/* .errOff */
38406b9b3e0SSimon J. Gerraty 	"echo \"%s\"\n",	/* .echoTmpl */
38506b9b3e0SSimon J. Gerraty 	"%s\n",			/* .runIgnTmpl */
38606b9b3e0SSimon J. Gerraty 	"{ %s \n} || exit $?\n", /* .runChkTmpl */
387956e45f6SSimon J. Gerraty 	"'\n'",			/* .newline */
388956e45f6SSimon J. Gerraty 	'#',			/* .commentChar */
38906b9b3e0SSimon J. Gerraty 	"v",			/* .echoFlag */
39006b9b3e0SSimon J. Gerraty 	"",			/* .errFlag */
3913955d011SMarcel Moolenaar     },
3923955d011SMarcel Moolenaar     /*
3933955d011SMarcel Moolenaar      * CSH description. The csh can do echo control by playing
3943955d011SMarcel Moolenaar      * with the setting of the 'echo' shell variable. Sadly,
3953955d011SMarcel Moolenaar      * however, it is unable to do error control nicely.
3963955d011SMarcel Moolenaar      */
3973955d011SMarcel Moolenaar     {
398956e45f6SSimon J. Gerraty 	"csh",			/* .name */
399b0c40a00SSimon J. Gerraty 	true,			/* .hasEchoCtl */
400956e45f6SSimon J. Gerraty 	"unset verbose",	/* .echoOff */
401956e45f6SSimon J. Gerraty 	"set verbose",		/* .echoOn */
402956e45f6SSimon J. Gerraty 	"unset verbose",	/* .noPrint */
403956e45f6SSimon J. Gerraty 	13,			/* .noPrintLen */
404b0c40a00SSimon J. Gerraty 	false,			/* .hasErrCtl */
40506b9b3e0SSimon J. Gerraty 	"",			/* .errOn */
40606b9b3e0SSimon J. Gerraty 	"",			/* .errOff */
40706b9b3e0SSimon J. Gerraty 	"echo \"%s\"\n",	/* .echoTmpl */
40806b9b3e0SSimon J. Gerraty 	"csh -c \"%s || exit 0\"\n", /* .runIgnTmpl */
40906b9b3e0SSimon J. Gerraty 	"",			/* .runChkTmpl */
410956e45f6SSimon J. Gerraty 	"'\\\n'",		/* .newline */
411956e45f6SSimon J. Gerraty 	'#',			/* .commentChar */
41206b9b3e0SSimon J. Gerraty 	"v",			/* .echoFlag */
41306b9b3e0SSimon J. Gerraty 	"e",			/* .errFlag */
4143955d011SMarcel Moolenaar     }
4153955d011SMarcel Moolenaar };
416956e45f6SSimon J. Gerraty 
41706b9b3e0SSimon J. Gerraty /*
41806b9b3e0SSimon J. Gerraty  * This is the shell to which we pass all commands in the Makefile.
41906b9b3e0SSimon J. Gerraty  * It is set by the Job_ParseShell function.
42006b9b3e0SSimon J. Gerraty  */
42106b9b3e0SSimon J. Gerraty static Shell *shell = &shells[DEFSHELL_INDEX];
42222619282SSimon J. Gerraty char *shellPath;		/* full pathname of executable image */
423956e45f6SSimon J. Gerraty const char *shellName = NULL;	/* last component of shellPath */
42451ee2c1cSSimon J. Gerraty char *shellErrFlag = NULL;
425dba7b0efSSimon J. Gerraty static char *shell_freeIt = NULL; /* Allocated memory for custom .SHELL */
4263955d011SMarcel Moolenaar 
4273955d011SMarcel Moolenaar 
428956e45f6SSimon J. Gerraty static Job *job_table;		/* The structures that describe them */
429956e45f6SSimon J. Gerraty static Job *job_table_end;	/* job_table + maxJobs */
430d5e0a182SSimon J. Gerraty static unsigned int wantToken;
431b0c40a00SSimon J. Gerraty static bool lurking_children = false;
432b0c40a00SSimon J. Gerraty static bool make_suspended = false; /* Whether we've seen a SIGTSTP (etc) */
4333955d011SMarcel Moolenaar 
4343955d011SMarcel Moolenaar /*
4353955d011SMarcel Moolenaar  * Set of descriptors of pipes connected to
4363955d011SMarcel Moolenaar  * the output channels of children
4373955d011SMarcel Moolenaar  */
4383955d011SMarcel Moolenaar static struct pollfd *fds = NULL;
439dba7b0efSSimon J. Gerraty static Job **jobByFdIndex = NULL;
440dba7b0efSSimon J. Gerraty static nfds_t fdsLen = 0;
4413955d011SMarcel Moolenaar static void watchfd(Job *);
4423955d011SMarcel Moolenaar static void clearfd(Job *);
443b0c40a00SSimon J. Gerraty static bool readyfd(Job *);
4443955d011SMarcel Moolenaar 
44506b9b3e0SSimon J. Gerraty static char *targPrefix = NULL;	/* To identify a job change in the output. */
4463955d011SMarcel Moolenaar static Job tokenWaitJob;	/* token wait pseudo-job */
4473955d011SMarcel Moolenaar 
4483955d011SMarcel Moolenaar static Job childExitJob;	/* child exit pseudo-job */
4493955d011SMarcel Moolenaar #define CHILD_EXIT "."
4503955d011SMarcel Moolenaar #define DO_JOB_RESUME "R"
4513955d011SMarcel Moolenaar 
45206b9b3e0SSimon J. Gerraty enum {
45306b9b3e0SSimon J. Gerraty 	npseudojobs = 2		/* number of pseudo-jobs */
45406b9b3e0SSimon J. Gerraty };
4553955d011SMarcel Moolenaar 
4563955d011SMarcel Moolenaar static sigset_t caught_signals;	/* Set of signals we handle */
457dba7b0efSSimon J. Gerraty static volatile sig_atomic_t caught_sigchld;
4583955d011SMarcel Moolenaar 
459b0c40a00SSimon J. Gerraty static void CollectOutput(Job *, bool);
460b0c40a00SSimon J. Gerraty static void JobInterrupt(bool, int) MAKE_ATTR_DEAD;
4613955d011SMarcel Moolenaar static void JobRestartJobs(void);
4623955d011SMarcel Moolenaar static void JobSigReset(void);
4633955d011SMarcel Moolenaar 
46406b9b3e0SSimon J. Gerraty static void
46506b9b3e0SSimon J. Gerraty SwitchOutputTo(GNode *gn)
46606b9b3e0SSimon J. Gerraty {
46706b9b3e0SSimon J. Gerraty 	/* The node for which output was most recently produced. */
46806b9b3e0SSimon J. Gerraty 	static GNode *lastNode = NULL;
46906b9b3e0SSimon J. Gerraty 
47006b9b3e0SSimon J. Gerraty 	if (gn == lastNode)
47106b9b3e0SSimon J. Gerraty 		return;
47206b9b3e0SSimon J. Gerraty 	lastNode = gn;
47306b9b3e0SSimon J. Gerraty 
47406b9b3e0SSimon J. Gerraty 	if (opts.maxJobs != 1 && targPrefix != NULL && targPrefix[0] != '\0')
47506b9b3e0SSimon J. Gerraty 		(void)fprintf(stdout, "%s %s ---\n", targPrefix, gn->name);
47606b9b3e0SSimon J. Gerraty }
47706b9b3e0SSimon J. Gerraty 
47849caa483SSimon J. Gerraty static unsigned
47949caa483SSimon J. Gerraty nfds_per_job(void)
48049caa483SSimon J. Gerraty {
48149caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
48249caa483SSimon J. Gerraty 	if (useMeta)
48349caa483SSimon J. Gerraty 		return 2;
48449caa483SSimon J. Gerraty #endif
48549caa483SSimon J. Gerraty 	return 1;
48649caa483SSimon J. Gerraty }
48749caa483SSimon J. Gerraty 
48806b9b3e0SSimon J. Gerraty void
48906b9b3e0SSimon J. Gerraty Job_FlagsToString(const Job *job, char *buf, size_t bufsize)
49006b9b3e0SSimon J. Gerraty {
49106b9b3e0SSimon J. Gerraty 	snprintf(buf, bufsize, "%c%c%c",
49206b9b3e0SSimon J. Gerraty 	    job->ignerr ? 'i' : '-',
49306b9b3e0SSimon J. Gerraty 	    !job->echo ? 's' : '-',
49406b9b3e0SSimon J. Gerraty 	    job->special ? 'S' : '-');
49506b9b3e0SSimon J. Gerraty }
49606b9b3e0SSimon J. Gerraty 
4973955d011SMarcel Moolenaar static void
498b0c40a00SSimon J. Gerraty DumpJobs(const char *where)
4993955d011SMarcel Moolenaar {
5003955d011SMarcel Moolenaar 	Job *job;
50106b9b3e0SSimon J. Gerraty 	char flags[4];
5023955d011SMarcel Moolenaar 
503956e45f6SSimon J. Gerraty 	debug_printf("job table @ %s\n", where);
5043955d011SMarcel Moolenaar 	for (job = job_table; job < job_table_end; job++) {
50506b9b3e0SSimon J. Gerraty 		Job_FlagsToString(job, flags, sizeof flags);
50606b9b3e0SSimon J. Gerraty 		debug_printf("job %d, status %d, flags %s, pid %d\n",
50706b9b3e0SSimon J. Gerraty 		    (int)(job - job_table), job->status, flags, job->pid);
5083955d011SMarcel Moolenaar 	}
5093955d011SMarcel Moolenaar }
5103955d011SMarcel Moolenaar 
5113955d011SMarcel Moolenaar /*
51245447996SSimon J. Gerraty  * Delete the target of a failed, interrupted, or otherwise
51345447996SSimon J. Gerraty  * unsuccessful job unless inhibited by .PRECIOUS.
51445447996SSimon J. Gerraty  */
51545447996SSimon J. Gerraty static void
51645447996SSimon J. Gerraty JobDeleteTarget(GNode *gn)
51745447996SSimon J. Gerraty {
518956e45f6SSimon J. Gerraty 	const char *file;
519956e45f6SSimon J. Gerraty 
520956e45f6SSimon J. Gerraty 	if (gn->type & OP_JOIN)
521956e45f6SSimon J. Gerraty 		return;
522956e45f6SSimon J. Gerraty 	if (gn->type & OP_PHONY)
523956e45f6SSimon J. Gerraty 		return;
5249f45a3c8SSimon J. Gerraty 	if (GNode_IsPrecious(gn))
525956e45f6SSimon J. Gerraty 		return;
526956e45f6SSimon J. Gerraty 	if (opts.noExecute)
527956e45f6SSimon J. Gerraty 		return;
528956e45f6SSimon J. Gerraty 
529956e45f6SSimon J. Gerraty 	file = GNode_Path(gn);
5304fde40d9SSimon J. Gerraty 	if (unlink_file(file) == 0)
53145447996SSimon J. Gerraty 		Error("*** %s removed", file);
53245447996SSimon J. Gerraty }
53345447996SSimon J. Gerraty 
53445447996SSimon J. Gerraty /*
5353955d011SMarcel Moolenaar  * JobSigLock/JobSigUnlock
5363955d011SMarcel Moolenaar  *
5373955d011SMarcel Moolenaar  * Signal lock routines to get exclusive access. Currently used to
5383955d011SMarcel Moolenaar  * protect `jobs' and `stoppedJobs' list manipulations.
5393955d011SMarcel Moolenaar  */
54006b9b3e0SSimon J. Gerraty static void
54106b9b3e0SSimon J. Gerraty JobSigLock(sigset_t *omaskp)
5423955d011SMarcel Moolenaar {
5433955d011SMarcel Moolenaar 	if (sigprocmask(SIG_BLOCK, &caught_signals, omaskp) != 0) {
5443955d011SMarcel Moolenaar 		Punt("JobSigLock: sigprocmask: %s", strerror(errno));
5453955d011SMarcel Moolenaar 		sigemptyset(omaskp);
5463955d011SMarcel Moolenaar 	}
5473955d011SMarcel Moolenaar }
5483955d011SMarcel Moolenaar 
54906b9b3e0SSimon J. Gerraty static void
55006b9b3e0SSimon J. Gerraty JobSigUnlock(sigset_t *omaskp)
5513955d011SMarcel Moolenaar {
5523955d011SMarcel Moolenaar 	(void)sigprocmask(SIG_SETMASK, omaskp, NULL);
5533955d011SMarcel Moolenaar }
5543955d011SMarcel Moolenaar 
5553955d011SMarcel Moolenaar static void
5563955d011SMarcel Moolenaar JobCreatePipe(Job *job, int minfd)
5573955d011SMarcel Moolenaar {
558e1cee40dSSimon J. Gerraty 	int i, fd, flags;
559956e45f6SSimon J. Gerraty 	int pipe_fds[2];
5603955d011SMarcel Moolenaar 
561956e45f6SSimon J. Gerraty 	if (pipe(pipe_fds) == -1)
5623955d011SMarcel Moolenaar 		Punt("Cannot create pipe: %s", strerror(errno));
5633955d011SMarcel Moolenaar 
56474d2e02bSSimon J. Gerraty 	for (i = 0; i < 2; i++) {
565d5e0a182SSimon J. Gerraty 		/* Avoid using low-numbered fds */
566956e45f6SSimon J. Gerraty 		fd = fcntl(pipe_fds[i], F_DUPFD, minfd);
56774d2e02bSSimon J. Gerraty 		if (fd != -1) {
568956e45f6SSimon J. Gerraty 			close(pipe_fds[i]);
569956e45f6SSimon J. Gerraty 			pipe_fds[i] = fd;
57074d2e02bSSimon J. Gerraty 		}
57174d2e02bSSimon J. Gerraty 	}
57274d2e02bSSimon J. Gerraty 
573956e45f6SSimon J. Gerraty 	job->inPipe = pipe_fds[0];
574956e45f6SSimon J. Gerraty 	job->outPipe = pipe_fds[1];
575956e45f6SSimon J. Gerraty 
576956e45f6SSimon J. Gerraty 	if (fcntl(job->inPipe, F_SETFD, FD_CLOEXEC) == -1)
577e1cee40dSSimon J. Gerraty 		Punt("Cannot set close-on-exec: %s", strerror(errno));
578956e45f6SSimon J. Gerraty 	if (fcntl(job->outPipe, F_SETFD, FD_CLOEXEC) == -1)
579e1cee40dSSimon J. Gerraty 		Punt("Cannot set close-on-exec: %s", strerror(errno));
5803955d011SMarcel Moolenaar 
5813955d011SMarcel Moolenaar 	/*
5823955d011SMarcel Moolenaar 	 * We mark the input side of the pipe non-blocking; we poll(2) the
5833955d011SMarcel Moolenaar 	 * pipe when we're waiting for a job token, but we might lose the
5843955d011SMarcel Moolenaar 	 * race for the token when a new one becomes available, so the read
5853955d011SMarcel Moolenaar 	 * from the pipe should not block.
5863955d011SMarcel Moolenaar 	 */
587956e45f6SSimon J. Gerraty 	flags = fcntl(job->inPipe, F_GETFL, 0);
588e1cee40dSSimon J. Gerraty 	if (flags == -1)
589e1cee40dSSimon J. Gerraty 		Punt("Cannot get flags: %s", strerror(errno));
590e1cee40dSSimon J. Gerraty 	flags |= O_NONBLOCK;
591956e45f6SSimon J. Gerraty 	if (fcntl(job->inPipe, F_SETFL, flags) == -1)
592e1cee40dSSimon J. Gerraty 		Punt("Cannot set flags: %s", strerror(errno));
5933955d011SMarcel Moolenaar }
5943955d011SMarcel Moolenaar 
595956e45f6SSimon J. Gerraty /* Pass the signal to each running job. */
5963955d011SMarcel Moolenaar static void
5973955d011SMarcel Moolenaar JobCondPassSig(int signo)
5983955d011SMarcel Moolenaar {
5993955d011SMarcel Moolenaar 	Job *job;
6003955d011SMarcel Moolenaar 
601956e45f6SSimon J. Gerraty 	DEBUG1(JOB, "JobCondPassSig(%d) called.\n", signo);
6023955d011SMarcel Moolenaar 
6033955d011SMarcel Moolenaar 	for (job = job_table; job < job_table_end; job++) {
604e2eeea75SSimon J. Gerraty 		if (job->status != JOB_ST_RUNNING)
6053955d011SMarcel Moolenaar 			continue;
606956e45f6SSimon J. Gerraty 		DEBUG2(JOB, "JobCondPassSig passing signal %d to child %d.\n",
6073955d011SMarcel Moolenaar 		    signo, job->pid);
6083955d011SMarcel Moolenaar 		KILLPG(job->pid, signo);
6093955d011SMarcel Moolenaar 	}
6103955d011SMarcel Moolenaar }
6113955d011SMarcel Moolenaar 
61206b9b3e0SSimon J. Gerraty /*
61306b9b3e0SSimon J. Gerraty  * SIGCHLD handler.
6143955d011SMarcel Moolenaar  *
61506b9b3e0SSimon J. Gerraty  * Sends a token on the child exit pipe to wake us up from select()/poll().
61606b9b3e0SSimon J. Gerraty  */
6173955d011SMarcel Moolenaar static void
6183955d011SMarcel Moolenaar JobChildSig(int signo MAKE_ATTR_UNUSED)
6193955d011SMarcel Moolenaar {
620dba7b0efSSimon J. Gerraty 	caught_sigchld = 1;
62106b9b3e0SSimon J. Gerraty 	while (write(childExitJob.outPipe, CHILD_EXIT, 1) == -1 &&
62206b9b3e0SSimon J. Gerraty 	       errno == EAGAIN)
6233cbdda60SSimon J. Gerraty 		continue;
6243955d011SMarcel Moolenaar }
6253955d011SMarcel Moolenaar 
6263955d011SMarcel Moolenaar 
627956e45f6SSimon J. Gerraty /* Resume all stopped jobs. */
6283955d011SMarcel Moolenaar static void
6293955d011SMarcel Moolenaar JobContinueSig(int signo MAKE_ATTR_UNUSED)
6303955d011SMarcel Moolenaar {
6313955d011SMarcel Moolenaar 	/*
632956e45f6SSimon J. Gerraty 	 * Defer sending SIGCONT to our stopped children until we return
6333955d011SMarcel Moolenaar 	 * from the signal handler.
6343955d011SMarcel Moolenaar 	 */
6353cbdda60SSimon J. Gerraty 	while (write(childExitJob.outPipe, DO_JOB_RESUME, 1) == -1 &&
6363cbdda60SSimon J. Gerraty 	       errno == EAGAIN)
6373cbdda60SSimon J. Gerraty 		continue;
6383955d011SMarcel Moolenaar }
6393955d011SMarcel Moolenaar 
64006b9b3e0SSimon J. Gerraty /*
64106b9b3e0SSimon J. Gerraty  * Pass a signal on to all jobs, then resend to ourselves.
64206b9b3e0SSimon J. Gerraty  * We die by the same signal.
64306b9b3e0SSimon J. Gerraty  */
6443955d011SMarcel Moolenaar MAKE_ATTR_DEAD static void
6453955d011SMarcel Moolenaar JobPassSig_int(int signo)
6463955d011SMarcel Moolenaar {
6473955d011SMarcel Moolenaar 	/* Run .INTERRUPT target then exit */
648b0c40a00SSimon J. Gerraty 	JobInterrupt(true, signo);
6493955d011SMarcel Moolenaar }
6503955d011SMarcel Moolenaar 
65106b9b3e0SSimon J. Gerraty /*
65206b9b3e0SSimon J. Gerraty  * Pass a signal on to all jobs, then resend to ourselves.
65306b9b3e0SSimon J. Gerraty  * We die by the same signal.
65406b9b3e0SSimon J. Gerraty  */
6553955d011SMarcel Moolenaar MAKE_ATTR_DEAD static void
6563955d011SMarcel Moolenaar JobPassSig_term(int signo)
6573955d011SMarcel Moolenaar {
6583955d011SMarcel Moolenaar 	/* Dont run .INTERRUPT target then exit */
659b0c40a00SSimon J. Gerraty 	JobInterrupt(false, signo);
6603955d011SMarcel Moolenaar }
6613955d011SMarcel Moolenaar 
6623955d011SMarcel Moolenaar static void
6633955d011SMarcel Moolenaar JobPassSig_suspend(int signo)
6643955d011SMarcel Moolenaar {
6653955d011SMarcel Moolenaar 	sigset_t nmask, omask;
6663955d011SMarcel Moolenaar 	struct sigaction act;
6673955d011SMarcel Moolenaar 
6683955d011SMarcel Moolenaar 	/* Suppress job started/continued messages */
669b0c40a00SSimon J. Gerraty 	make_suspended = true;
6703955d011SMarcel Moolenaar 
6713955d011SMarcel Moolenaar 	/* Pass the signal onto every job */
6723955d011SMarcel Moolenaar 	JobCondPassSig(signo);
6733955d011SMarcel Moolenaar 
6743955d011SMarcel Moolenaar 	/*
67506b9b3e0SSimon J. Gerraty 	 * Send ourselves the signal now we've given the message to everyone
67606b9b3e0SSimon J. Gerraty 	 * else. Note we block everything else possible while we're getting
67706b9b3e0SSimon J. Gerraty 	 * the signal. This ensures that all our jobs get continued when we
67806b9b3e0SSimon J. Gerraty 	 * wake up before we take any other signal.
6793955d011SMarcel Moolenaar 	 */
6803955d011SMarcel Moolenaar 	sigfillset(&nmask);
6813955d011SMarcel Moolenaar 	sigdelset(&nmask, signo);
6823955d011SMarcel Moolenaar 	(void)sigprocmask(SIG_SETMASK, &nmask, &omask);
6833955d011SMarcel Moolenaar 
6843955d011SMarcel Moolenaar 	act.sa_handler = SIG_DFL;
6853955d011SMarcel Moolenaar 	sigemptyset(&act.sa_mask);
6863955d011SMarcel Moolenaar 	act.sa_flags = 0;
6873955d011SMarcel Moolenaar 	(void)sigaction(signo, &act, NULL);
6883955d011SMarcel Moolenaar 
689*d9a65c5dSSimon J. Gerraty 	DEBUG1(JOB, "JobPassSig_suspend passing signal %d to self.\n", signo);
6903955d011SMarcel Moolenaar 
6913955d011SMarcel Moolenaar 	(void)kill(getpid(), signo);
6923955d011SMarcel Moolenaar 
6933955d011SMarcel Moolenaar 	/*
6943955d011SMarcel Moolenaar 	 * We've been continued.
6953955d011SMarcel Moolenaar 	 *
696d5e0a182SSimon J. Gerraty 	 * A whole host of signals is going to happen!
6973955d011SMarcel Moolenaar 	 * SIGCHLD for any processes that actually suspended themselves.
698d5e0a182SSimon J. Gerraty 	 * SIGCHLD for any processes that exited while we were asleep.
6993955d011SMarcel Moolenaar 	 * The SIGCONT that actually caused us to wake up.
7003955d011SMarcel Moolenaar 	 *
7013955d011SMarcel Moolenaar 	 * Since we defer passing the SIGCONT on to our children until
7023955d011SMarcel Moolenaar 	 * the main processing loop, we can be sure that all the SIGCHLD
7033955d011SMarcel Moolenaar 	 * events will have happened by then - and that the waitpid() will
7043955d011SMarcel Moolenaar 	 * collect the child 'suspended' events.
7053955d011SMarcel Moolenaar 	 * For correct sequencing we just need to ensure we process the
706956e45f6SSimon J. Gerraty 	 * waitpid() before passing on the SIGCONT.
7073955d011SMarcel Moolenaar 	 *
7083955d011SMarcel Moolenaar 	 * In any case nothing else is needed here.
7093955d011SMarcel Moolenaar 	 */
7103955d011SMarcel Moolenaar 
7113955d011SMarcel Moolenaar 	/* Restore handler and signal mask */
7123955d011SMarcel Moolenaar 	act.sa_handler = JobPassSig_suspend;
7133955d011SMarcel Moolenaar 	(void)sigaction(signo, &act, NULL);
7143955d011SMarcel Moolenaar 	(void)sigprocmask(SIG_SETMASK, &omask, NULL);
7153955d011SMarcel Moolenaar }
7163955d011SMarcel Moolenaar 
7173955d011SMarcel Moolenaar static Job *
718b0c40a00SSimon J. Gerraty JobFindPid(int pid, JobStatus status, bool isJobs)
7193955d011SMarcel Moolenaar {
7203955d011SMarcel Moolenaar 	Job *job;
7213955d011SMarcel Moolenaar 
7223955d011SMarcel Moolenaar 	for (job = job_table; job < job_table_end; job++) {
723e2eeea75SSimon J. Gerraty 		if (job->status == status && job->pid == pid)
7243955d011SMarcel Moolenaar 			return job;
7253955d011SMarcel Moolenaar 	}
7263955d011SMarcel Moolenaar 	if (DEBUG(JOB) && isJobs)
727b0c40a00SSimon J. Gerraty 		DumpJobs("no pid");
7283955d011SMarcel Moolenaar 	return NULL;
7293955d011SMarcel Moolenaar }
7303955d011SMarcel Moolenaar 
731956e45f6SSimon J. Gerraty /* Parse leading '@', '-' and '+', which control the exact execution mode. */
732956e45f6SSimon J. Gerraty static void
73306b9b3e0SSimon J. Gerraty ParseCommandFlags(char **pp, CommandFlags *out_cmdFlags)
734956e45f6SSimon J. Gerraty {
735956e45f6SSimon J. Gerraty 	char *p = *pp;
736b0c40a00SSimon J. Gerraty 	out_cmdFlags->echo = true;
737b0c40a00SSimon J. Gerraty 	out_cmdFlags->ignerr = false;
738b0c40a00SSimon J. Gerraty 	out_cmdFlags->always = false;
739956e45f6SSimon J. Gerraty 
740956e45f6SSimon J. Gerraty 	for (;;) {
741956e45f6SSimon J. Gerraty 		if (*p == '@')
74206b9b3e0SSimon J. Gerraty 			out_cmdFlags->echo = DEBUG(LOUD);
743956e45f6SSimon J. Gerraty 		else if (*p == '-')
744b0c40a00SSimon J. Gerraty 			out_cmdFlags->ignerr = true;
745956e45f6SSimon J. Gerraty 		else if (*p == '+')
746b0c40a00SSimon J. Gerraty 			out_cmdFlags->always = true;
7474fde40d9SSimon J. Gerraty 		else if (!ch_isspace(*p))
748d5e0a182SSimon J. Gerraty 			/* Ignore whitespace for compatibility with GNU make */
749956e45f6SSimon J. Gerraty 			break;
750956e45f6SSimon J. Gerraty 		p++;
751956e45f6SSimon J. Gerraty 	}
752956e45f6SSimon J. Gerraty 
753956e45f6SSimon J. Gerraty 	pp_skip_whitespace(&p);
754956e45f6SSimon J. Gerraty 
755956e45f6SSimon J. Gerraty 	*pp = p;
756956e45f6SSimon J. Gerraty }
757956e45f6SSimon J. Gerraty 
758956e45f6SSimon J. Gerraty /* Escape a string for a double-quoted string literal in sh, csh and ksh. */
759956e45f6SSimon J. Gerraty static char *
760956e45f6SSimon J. Gerraty EscapeShellDblQuot(const char *cmd)
761956e45f6SSimon J. Gerraty {
762956e45f6SSimon J. Gerraty 	size_t i, j;
763956e45f6SSimon J. Gerraty 
764956e45f6SSimon J. Gerraty 	/* Worst that could happen is every char needs escaping. */
765956e45f6SSimon J. Gerraty 	char *esc = bmake_malloc(strlen(cmd) * 2 + 1);
766956e45f6SSimon J. Gerraty 	for (i = 0, j = 0; cmd[i] != '\0'; i++, j++) {
76706b9b3e0SSimon J. Gerraty 		if (cmd[i] == '$' || cmd[i] == '`' || cmd[i] == '\\' ||
76806b9b3e0SSimon J. Gerraty 		    cmd[i] == '"')
769956e45f6SSimon J. Gerraty 			esc[j++] = '\\';
770956e45f6SSimon J. Gerraty 		esc[j] = cmd[i];
771956e45f6SSimon J. Gerraty 	}
772956e45f6SSimon J. Gerraty 	esc[j] = '\0';
773956e45f6SSimon J. Gerraty 
774956e45f6SSimon J. Gerraty 	return esc;
775956e45f6SSimon J. Gerraty }
776956e45f6SSimon J. Gerraty 
777e2eeea75SSimon J. Gerraty static void
778b0c40a00SSimon J. Gerraty ShellWriter_WriteFmt(ShellWriter *wr, const char *fmt, const char *arg)
779e2eeea75SSimon J. Gerraty {
78006b9b3e0SSimon J. Gerraty 	DEBUG1(JOB, fmt, arg);
781e2eeea75SSimon J. Gerraty 
78206b9b3e0SSimon J. Gerraty 	(void)fprintf(wr->f, fmt, arg);
78312904384SSimon J. Gerraty 	if (wr->f == stdout)
78406b9b3e0SSimon J. Gerraty 		(void)fflush(wr->f);
785e2eeea75SSimon J. Gerraty }
786e2eeea75SSimon J. Gerraty 
787e2eeea75SSimon J. Gerraty static void
788b0c40a00SSimon J. Gerraty ShellWriter_WriteLine(ShellWriter *wr, const char *line)
789e2eeea75SSimon J. Gerraty {
790b0c40a00SSimon J. Gerraty 	ShellWriter_WriteFmt(wr, "%s\n", line);
791e2eeea75SSimon J. Gerraty }
792e2eeea75SSimon J. Gerraty 
79306b9b3e0SSimon J. Gerraty static void
79406b9b3e0SSimon J. Gerraty ShellWriter_EchoOff(ShellWriter *wr)
79506b9b3e0SSimon J. Gerraty {
79606b9b3e0SSimon J. Gerraty 	if (shell->hasEchoCtl)
797b0c40a00SSimon J. Gerraty 		ShellWriter_WriteLine(wr, shell->echoOff);
79806b9b3e0SSimon J. Gerraty }
79906b9b3e0SSimon J. Gerraty 
80006b9b3e0SSimon J. Gerraty static void
80106b9b3e0SSimon J. Gerraty ShellWriter_EchoCmd(ShellWriter *wr, const char *escCmd)
80206b9b3e0SSimon J. Gerraty {
803b0c40a00SSimon J. Gerraty 	ShellWriter_WriteFmt(wr, shell->echoTmpl, escCmd);
80406b9b3e0SSimon J. Gerraty }
80506b9b3e0SSimon J. Gerraty 
80606b9b3e0SSimon J. Gerraty static void
80706b9b3e0SSimon J. Gerraty ShellWriter_EchoOn(ShellWriter *wr)
80806b9b3e0SSimon J. Gerraty {
80906b9b3e0SSimon J. Gerraty 	if (shell->hasEchoCtl)
810b0c40a00SSimon J. Gerraty 		ShellWriter_WriteLine(wr, shell->echoOn);
81106b9b3e0SSimon J. Gerraty }
81206b9b3e0SSimon J. Gerraty 
81306b9b3e0SSimon J. Gerraty static void
81406b9b3e0SSimon J. Gerraty ShellWriter_TraceOn(ShellWriter *wr)
81506b9b3e0SSimon J. Gerraty {
81606b9b3e0SSimon J. Gerraty 	if (!wr->xtraced) {
817b0c40a00SSimon J. Gerraty 		ShellWriter_WriteLine(wr, "set -x");
818b0c40a00SSimon J. Gerraty 		wr->xtraced = true;
81906b9b3e0SSimon J. Gerraty 	}
82006b9b3e0SSimon J. Gerraty }
82106b9b3e0SSimon J. Gerraty 
82206b9b3e0SSimon J. Gerraty static void
823b0c40a00SSimon J. Gerraty ShellWriter_ErrOff(ShellWriter *wr, bool echo)
82406b9b3e0SSimon J. Gerraty {
82506b9b3e0SSimon J. Gerraty 	if (echo)
82606b9b3e0SSimon J. Gerraty 		ShellWriter_EchoOff(wr);
827b0c40a00SSimon J. Gerraty 	ShellWriter_WriteLine(wr, shell->errOff);
82806b9b3e0SSimon J. Gerraty 	if (echo)
82906b9b3e0SSimon J. Gerraty 		ShellWriter_EchoOn(wr);
83006b9b3e0SSimon J. Gerraty }
83106b9b3e0SSimon J. Gerraty 
83206b9b3e0SSimon J. Gerraty static void
833b0c40a00SSimon J. Gerraty ShellWriter_ErrOn(ShellWriter *wr, bool echo)
83406b9b3e0SSimon J. Gerraty {
83506b9b3e0SSimon J. Gerraty 	if (echo)
83606b9b3e0SSimon J. Gerraty 		ShellWriter_EchoOff(wr);
837b0c40a00SSimon J. Gerraty 	ShellWriter_WriteLine(wr, shell->errOn);
83806b9b3e0SSimon J. Gerraty 	if (echo)
83906b9b3e0SSimon J. Gerraty 		ShellWriter_EchoOn(wr);
84006b9b3e0SSimon J. Gerraty }
84106b9b3e0SSimon J. Gerraty 
84206b9b3e0SSimon J. Gerraty /*
84306b9b3e0SSimon J. Gerraty  * The shell has no built-in error control, so emulate error control by
84406b9b3e0SSimon J. Gerraty  * enclosing each shell command in a template like "{ %s \n } || exit $?"
84506b9b3e0SSimon J. Gerraty  * (configurable per shell).
8463955d011SMarcel Moolenaar  */
847956e45f6SSimon J. Gerraty static void
848b0c40a00SSimon J. Gerraty JobWriteSpecialsEchoCtl(Job *job, ShellWriter *wr, CommandFlags *inout_cmdFlags,
84906b9b3e0SSimon J. Gerraty 			const char *escCmd, const char **inout_cmdTemplate)
8503955d011SMarcel Moolenaar {
8511d3f2ddcSSimon J. Gerraty 	/* XXX: Why is the whole job modified at this point? */
852b0c40a00SSimon J. Gerraty 	job->ignerr = true;
8533955d011SMarcel Moolenaar 
85406b9b3e0SSimon J. Gerraty 	if (job->echo && inout_cmdFlags->echo) {
85506b9b3e0SSimon J. Gerraty 		ShellWriter_EchoOff(wr);
85606b9b3e0SSimon J. Gerraty 		ShellWriter_EchoCmd(wr, escCmd);
8573955d011SMarcel Moolenaar 
85806b9b3e0SSimon J. Gerraty 		/*
85906b9b3e0SSimon J. Gerraty 		 * Leave echoing off so the user doesn't see the commands
86006b9b3e0SSimon J. Gerraty 		 * for toggling the error checking.
86106b9b3e0SSimon J. Gerraty 		 */
862b0c40a00SSimon J. Gerraty 		inout_cmdFlags->echo = false;
86306b9b3e0SSimon J. Gerraty 	}
86406b9b3e0SSimon J. Gerraty 	*inout_cmdTemplate = shell->runIgnTmpl;
8653955d011SMarcel Moolenaar 
86606b9b3e0SSimon J. Gerraty 	/*
86706b9b3e0SSimon J. Gerraty 	 * The template runIgnTmpl already takes care of ignoring errors,
86806b9b3e0SSimon J. Gerraty 	 * so pretend error checking is still on.
86906b9b3e0SSimon J. Gerraty 	 * XXX: What effects does this have, and why is it necessary?
87006b9b3e0SSimon J. Gerraty 	 */
871b0c40a00SSimon J. Gerraty 	inout_cmdFlags->ignerr = false;
87206b9b3e0SSimon J. Gerraty }
87306b9b3e0SSimon J. Gerraty 
87406b9b3e0SSimon J. Gerraty static void
875b0c40a00SSimon J. Gerraty JobWriteSpecials(Job *job, ShellWriter *wr, const char *escCmd, bool run,
87606b9b3e0SSimon J. Gerraty 		 CommandFlags *inout_cmdFlags, const char **inout_cmdTemplate)
87706b9b3e0SSimon J. Gerraty {
878d5e0a182SSimon J. Gerraty 	if (!run)
879b0c40a00SSimon J. Gerraty 		inout_cmdFlags->ignerr = false;
880d5e0a182SSimon J. Gerraty 	else if (shell->hasErrCtl)
88106b9b3e0SSimon J. Gerraty 		ShellWriter_ErrOff(wr, job->echo && inout_cmdFlags->echo);
88206b9b3e0SSimon J. Gerraty 	else if (shell->runIgnTmpl != NULL && shell->runIgnTmpl[0] != '\0') {
883b0c40a00SSimon J. Gerraty 		JobWriteSpecialsEchoCtl(job, wr, inout_cmdFlags, escCmd,
88406b9b3e0SSimon J. Gerraty 		    inout_cmdTemplate);
88506b9b3e0SSimon J. Gerraty 	} else
886b0c40a00SSimon J. Gerraty 		inout_cmdFlags->ignerr = false;
88706b9b3e0SSimon J. Gerraty }
88806b9b3e0SSimon J. Gerraty 
88906b9b3e0SSimon J. Gerraty /*
890b0c40a00SSimon J. Gerraty  * Write a shell command to the job's commands file, to be run later.
89106b9b3e0SSimon J. Gerraty  *
89206b9b3e0SSimon J. Gerraty  * If the command starts with '@' and neither the -s nor the -n flag was
893b0c40a00SSimon J. Gerraty  * given to make, stick a shell-specific echoOff command in the script.
89406b9b3e0SSimon J. Gerraty  *
89506b9b3e0SSimon J. Gerraty  * If the command starts with '-' and the shell has no error control (none
896b0c40a00SSimon J. Gerraty  * of the predefined shells has that), ignore errors for the entire job.
89706b9b3e0SSimon J. Gerraty  *
898b0c40a00SSimon J. Gerraty  * XXX: Why ignore errors for the entire job?  This is even documented in the
899b0c40a00SSimon J. Gerraty  * manual page, but without any rationale since there is no known rationale.
900b0c40a00SSimon J. Gerraty  *
901b0c40a00SSimon J. Gerraty  * XXX: The manual page says the '-' "affects the entire job", but that's not
902b0c40a00SSimon J. Gerraty  * accurate.  The '-' does not affect the commands before the '-'.
903b0c40a00SSimon J. Gerraty  *
904b0c40a00SSimon J. Gerraty  * If the command is just "...", skip all further commands of this job.  These
905b0c40a00SSimon J. Gerraty  * commands are attached to the .END node instead and will be run by
906b0c40a00SSimon J. Gerraty  * Job_Finish after all other targets have been made.
90706b9b3e0SSimon J. Gerraty  */
90806b9b3e0SSimon J. Gerraty static void
909b0c40a00SSimon J. Gerraty JobWriteCommand(Job *job, ShellWriter *wr, StringListNode *ln, const char *ucmd)
91006b9b3e0SSimon J. Gerraty {
911b0c40a00SSimon J. Gerraty 	bool run;
91206b9b3e0SSimon J. Gerraty 
91306b9b3e0SSimon J. Gerraty 	CommandFlags cmdFlags;
914b0c40a00SSimon J. Gerraty 	/* Template for writing a command to the shell file */
91506b9b3e0SSimon J. Gerraty 	const char *cmdTemplate;
91606b9b3e0SSimon J. Gerraty 	char *xcmd;		/* The expanded command */
91706b9b3e0SSimon J. Gerraty 	char *xcmdStart;
91806b9b3e0SSimon J. Gerraty 	char *escCmd;		/* xcmd escaped to be used in double quotes */
91906b9b3e0SSimon J. Gerraty 
92006b9b3e0SSimon J. Gerraty 	run = GNode_ShouldExecute(job->node);
92106b9b3e0SSimon J. Gerraty 
9228d5c8e21SSimon J. Gerraty 	xcmd = Var_SubstInTarget(ucmd, job->node);
923956e45f6SSimon J. Gerraty 	/* TODO: handle errors */
92406b9b3e0SSimon J. Gerraty 	xcmdStart = xcmd;
9253955d011SMarcel Moolenaar 
9263955d011SMarcel Moolenaar 	cmdTemplate = "%s\n";
9273955d011SMarcel Moolenaar 
92806b9b3e0SSimon J. Gerraty 	ParseCommandFlags(&xcmd, &cmdFlags);
929956e45f6SSimon J. Gerraty 
93006b9b3e0SSimon J. Gerraty 	/* The '+' command flag overrides the -n or -N options. */
93106b9b3e0SSimon J. Gerraty 	if (cmdFlags.always && !run) {
9323955d011SMarcel Moolenaar 		/*
9333955d011SMarcel Moolenaar 		 * We're not actually executing anything...
9343955d011SMarcel Moolenaar 		 * but this one needs to be - use compat mode just for it.
9353955d011SMarcel Moolenaar 		 */
9369f45a3c8SSimon J. Gerraty 		(void)Compat_RunCommand(ucmd, job->node, ln);
93706b9b3e0SSimon J. Gerraty 		free(xcmdStart);
938956e45f6SSimon J. Gerraty 		return;
9393955d011SMarcel Moolenaar 	}
9403955d011SMarcel Moolenaar 
9413955d011SMarcel Moolenaar 	/*
94206b9b3e0SSimon J. Gerraty 	 * If the shell doesn't have error control, the alternate echoing
94306b9b3e0SSimon J. Gerraty 	 * will be done (to avoid showing additional error checking code)
94406b9b3e0SSimon J. Gerraty 	 * and this needs some characters escaped.
9453955d011SMarcel Moolenaar 	 */
94606b9b3e0SSimon J. Gerraty 	escCmd = shell->hasErrCtl ? NULL : EscapeShellDblQuot(xcmd);
9473955d011SMarcel Moolenaar 
94806b9b3e0SSimon J. Gerraty 	if (!cmdFlags.echo) {
949d5e0a182SSimon J. Gerraty 		if (job->echo && run && shell->hasEchoCtl)
95006b9b3e0SSimon J. Gerraty 			ShellWriter_EchoOff(wr);
951d5e0a182SSimon J. Gerraty 		else if (shell->hasErrCtl)
952b0c40a00SSimon J. Gerraty 			cmdFlags.echo = true;
9533955d011SMarcel Moolenaar 	}
9543955d011SMarcel Moolenaar 
95506b9b3e0SSimon J. Gerraty 	if (cmdFlags.ignerr) {
956b0c40a00SSimon J. Gerraty 		JobWriteSpecials(job, wr, escCmd, run, &cmdFlags, &cmdTemplate);
9573955d011SMarcel Moolenaar 	} else {
9583955d011SMarcel Moolenaar 
9593955d011SMarcel Moolenaar 		/*
96006b9b3e0SSimon J. Gerraty 		 * If errors are being checked and the shell doesn't have
96106b9b3e0SSimon J. Gerraty 		 * error control but does supply an runChkTmpl template, then
96206b9b3e0SSimon J. Gerraty 		 * set up commands to run through it.
9633955d011SMarcel Moolenaar 		 */
9643955d011SMarcel Moolenaar 
96506b9b3e0SSimon J. Gerraty 		if (!shell->hasErrCtl && shell->runChkTmpl != NULL &&
96606b9b3e0SSimon J. Gerraty 		    shell->runChkTmpl[0] != '\0') {
96706b9b3e0SSimon J. Gerraty 			if (job->echo && cmdFlags.echo) {
96806b9b3e0SSimon J. Gerraty 				ShellWriter_EchoOff(wr);
96906b9b3e0SSimon J. Gerraty 				ShellWriter_EchoCmd(wr, escCmd);
970b0c40a00SSimon J. Gerraty 				cmdFlags.echo = false;
9713955d011SMarcel Moolenaar 			}
97206b9b3e0SSimon J. Gerraty 			/*
97306b9b3e0SSimon J. Gerraty 			 * If it's a comment line or blank, avoid the possible
97406b9b3e0SSimon J. Gerraty 			 * syntax error generated by "{\n} || exit $?".
97506b9b3e0SSimon J. Gerraty 			 */
97606b9b3e0SSimon J. Gerraty 			cmdTemplate = escCmd[0] == shell->commentChar ||
97706b9b3e0SSimon J. Gerraty 				      escCmd[0] == '\0'
97806b9b3e0SSimon J. Gerraty 			    ? shell->runIgnTmpl
97906b9b3e0SSimon J. Gerraty 			    : shell->runChkTmpl;
980b0c40a00SSimon J. Gerraty 			cmdFlags.ignerr = false;
9813955d011SMarcel Moolenaar 		}
9823955d011SMarcel Moolenaar 	}
9833955d011SMarcel Moolenaar 
98406b9b3e0SSimon J. Gerraty 	if (DEBUG(SHELL) && strcmp(shellName, "sh") == 0)
98506b9b3e0SSimon J. Gerraty 		ShellWriter_TraceOn(wr);
9863955d011SMarcel Moolenaar 
987b0c40a00SSimon J. Gerraty 	ShellWriter_WriteFmt(wr, cmdTemplate, xcmd);
98806b9b3e0SSimon J. Gerraty 	free(xcmdStart);
9893955d011SMarcel Moolenaar 	free(escCmd);
99006b9b3e0SSimon J. Gerraty 
99106b9b3e0SSimon J. Gerraty 	if (cmdFlags.ignerr)
99206b9b3e0SSimon J. Gerraty 		ShellWriter_ErrOn(wr, cmdFlags.echo && job->echo);
99306b9b3e0SSimon J. Gerraty 
99406b9b3e0SSimon J. Gerraty 	if (!cmdFlags.echo)
99506b9b3e0SSimon J. Gerraty 		ShellWriter_EchoOn(wr);
9963955d011SMarcel Moolenaar }
9973955d011SMarcel Moolenaar 
99806b9b3e0SSimon J. Gerraty /*
999b0c40a00SSimon J. Gerraty  * Write all commands to the shell file that is later executed.
10003955d011SMarcel Moolenaar  *
1001b0c40a00SSimon J. Gerraty  * The special command "..." stops writing and saves the remaining commands
1002dba7b0efSSimon J. Gerraty  * to be executed later, when the target '.END' is made.
100306b9b3e0SSimon J. Gerraty  *
100406b9b3e0SSimon J. Gerraty  * Return whether at least one command was written to the shell file.
100506b9b3e0SSimon J. Gerraty  */
1006b0c40a00SSimon J. Gerraty static bool
1007b0c40a00SSimon J. Gerraty JobWriteCommands(Job *job)
10083955d011SMarcel Moolenaar {
1009956e45f6SSimon J. Gerraty 	StringListNode *ln;
1010b0c40a00SSimon J. Gerraty 	bool seen = false;
1011b0c40a00SSimon J. Gerraty 	ShellWriter wr;
1012b0c40a00SSimon J. Gerraty 
1013b0c40a00SSimon J. Gerraty 	wr.f = job->cmdFILE;
1014b0c40a00SSimon J. Gerraty 	wr.xtraced = false;
1015956e45f6SSimon J. Gerraty 
101606b9b3e0SSimon J. Gerraty 	for (ln = job->node->commands.first; ln != NULL; ln = ln->next) {
1017956e45f6SSimon J. Gerraty 		const char *cmd = ln->datum;
1018956e45f6SSimon J. Gerraty 
1019956e45f6SSimon J. Gerraty 		if (strcmp(cmd, "...") == 0) {
1020956e45f6SSimon J. Gerraty 			job->node->type |= OP_SAVE_CMDS;
1021956e45f6SSimon J. Gerraty 			job->tailCmds = ln->next;
1022956e45f6SSimon J. Gerraty 			break;
1023956e45f6SSimon J. Gerraty 		}
1024e2eeea75SSimon J. Gerraty 
1025b0c40a00SSimon J. Gerraty 		JobWriteCommand(job, &wr, ln, ln->datum);
1026b0c40a00SSimon J. Gerraty 		seen = true;
1027956e45f6SSimon J. Gerraty 	}
102806b9b3e0SSimon J. Gerraty 
102906b9b3e0SSimon J. Gerraty 	return seen;
1030956e45f6SSimon J. Gerraty }
1031956e45f6SSimon J. Gerraty 
1032dba7b0efSSimon J. Gerraty /*
1033dba7b0efSSimon J. Gerraty  * Save the delayed commands (those after '...'), to be executed later in
1034dba7b0efSSimon J. Gerraty  * the '.END' node, when everything else is done.
1035dba7b0efSSimon J. Gerraty  */
1036956e45f6SSimon J. Gerraty static void
1037956e45f6SSimon J. Gerraty JobSaveCommands(Job *job)
1038956e45f6SSimon J. Gerraty {
103906b9b3e0SSimon J. Gerraty 	StringListNode *ln;
1040956e45f6SSimon J. Gerraty 
104106b9b3e0SSimon J. Gerraty 	for (ln = job->tailCmds; ln != NULL; ln = ln->next) {
104206b9b3e0SSimon J. Gerraty 		const char *cmd = ln->datum;
1043956e45f6SSimon J. Gerraty 		char *expanded_cmd;
1044dba7b0efSSimon J. Gerraty 		/*
1045dba7b0efSSimon J. Gerraty 		 * XXX: This Var_Subst is only intended to expand the dynamic
1046956e45f6SSimon J. Gerraty 		 * variables such as .TARGET, .IMPSRC.  It is not intended to
1047dba7b0efSSimon J. Gerraty 		 * expand the other variables as well; see deptgt-end.mk.
1048dba7b0efSSimon J. Gerraty 		 */
10498d5c8e21SSimon J. Gerraty 		expanded_cmd = Var_SubstInTarget(cmd, job->node);
1050956e45f6SSimon J. Gerraty 		/* TODO: handle errors */
105106b9b3e0SSimon J. Gerraty 		Lst_Append(&Targ_GetEndNode()->commands, expanded_cmd);
10528d5c8e21SSimon J. Gerraty 		Parse_RegisterCommand(expanded_cmd);
1053956e45f6SSimon J. Gerraty 	}
10543955d011SMarcel Moolenaar }
10553955d011SMarcel Moolenaar 
10563955d011SMarcel Moolenaar 
1057956e45f6SSimon J. Gerraty /* Called to close both input and output pipes when a job is finished. */
10583955d011SMarcel Moolenaar static void
1059e2eeea75SSimon J. Gerraty JobClosePipes(Job *job)
10603955d011SMarcel Moolenaar {
10613955d011SMarcel Moolenaar 	clearfd(job);
10623955d011SMarcel Moolenaar 	(void)close(job->outPipe);
10633955d011SMarcel Moolenaar 	job->outPipe = -1;
10643955d011SMarcel Moolenaar 
1065b0c40a00SSimon J. Gerraty 	CollectOutput(job, true);
10663955d011SMarcel Moolenaar 	(void)close(job->inPipe);
10673955d011SMarcel Moolenaar 	job->inPipe = -1;
10683955d011SMarcel Moolenaar }
10693955d011SMarcel Moolenaar 
107006b9b3e0SSimon J. Gerraty static void
1071b0c40a00SSimon J. Gerraty DebugFailedJob(const Job *job)
1072b0c40a00SSimon J. Gerraty {
1073b0c40a00SSimon J. Gerraty 	const StringListNode *ln;
1074b0c40a00SSimon J. Gerraty 
1075b0c40a00SSimon J. Gerraty 	if (!DEBUG(ERROR))
1076b0c40a00SSimon J. Gerraty 		return;
1077b0c40a00SSimon J. Gerraty 
107812904384SSimon J. Gerraty 	debug_printf("\n");
107912904384SSimon J. Gerraty 	debug_printf("*** Failed target: %s\n", job->node->name);
1080548bfc56SSimon J. Gerraty 	debug_printf("*** In directory: %s\n", curdir);
108112904384SSimon J. Gerraty 	debug_printf("*** Failed commands:\n");
108212904384SSimon J. Gerraty 	for (ln = job->node->commands.first; ln != NULL; ln = ln->next) {
108312904384SSimon J. Gerraty 		const char *cmd = ln->datum;
108412904384SSimon J. Gerraty 		debug_printf("\t%s\n", cmd);
108512904384SSimon J. Gerraty 
108612904384SSimon J. Gerraty 		if (strchr(cmd, '$') != NULL) {
10878d5c8e21SSimon J. Gerraty 			char *xcmd = Var_Subst(cmd, job->node, VARE_EVAL);
108812904384SSimon J. Gerraty 			debug_printf("\t=> %s\n", xcmd);
108912904384SSimon J. Gerraty 			free(xcmd);
109012904384SSimon J. Gerraty 		}
109112904384SSimon J. Gerraty 	}
1092b0c40a00SSimon J. Gerraty }
1093b0c40a00SSimon J. Gerraty 
1094b0c40a00SSimon J. Gerraty static void
109506b9b3e0SSimon J. Gerraty JobFinishDoneExitedError(Job *job, WAIT_T *inout_status)
109606b9b3e0SSimon J. Gerraty {
109706b9b3e0SSimon J. Gerraty 	SwitchOutputTo(job->node);
109806b9b3e0SSimon J. Gerraty #ifdef USE_META
109906b9b3e0SSimon J. Gerraty 	if (useMeta) {
110006b9b3e0SSimon J. Gerraty 		meta_job_error(job, job->node,
110106b9b3e0SSimon J. Gerraty 		    job->ignerr, WEXITSTATUS(*inout_status));
110206b9b3e0SSimon J. Gerraty 	}
110306b9b3e0SSimon J. Gerraty #endif
110406b9b3e0SSimon J. Gerraty 	if (!shouldDieQuietly(job->node, -1)) {
1105b0c40a00SSimon J. Gerraty 		DebugFailedJob(job);
110606b9b3e0SSimon J. Gerraty 		(void)printf("*** [%s] Error code %d%s\n",
110706b9b3e0SSimon J. Gerraty 		    job->node->name, WEXITSTATUS(*inout_status),
110806b9b3e0SSimon J. Gerraty 		    job->ignerr ? " (ignored)" : "");
110906b9b3e0SSimon J. Gerraty 	}
111006b9b3e0SSimon J. Gerraty 
111106b9b3e0SSimon J. Gerraty 	if (job->ignerr)
111206b9b3e0SSimon J. Gerraty 		WAIT_STATUS(*inout_status) = 0;
111306b9b3e0SSimon J. Gerraty 	else {
111406b9b3e0SSimon J. Gerraty 		if (deleteOnError)
111506b9b3e0SSimon J. Gerraty 			JobDeleteTarget(job->node);
11169f45a3c8SSimon J. Gerraty 		PrintOnError(job->node, "\n");
111706b9b3e0SSimon J. Gerraty 	}
111806b9b3e0SSimon J. Gerraty }
111906b9b3e0SSimon J. Gerraty 
112006b9b3e0SSimon J. Gerraty static void
112106b9b3e0SSimon J. Gerraty JobFinishDoneExited(Job *job, WAIT_T *inout_status)
112206b9b3e0SSimon J. Gerraty {
112306b9b3e0SSimon J. Gerraty 	DEBUG2(JOB, "Process %d [%s] exited.\n", job->pid, job->node->name);
112406b9b3e0SSimon J. Gerraty 
112506b9b3e0SSimon J. Gerraty 	if (WEXITSTATUS(*inout_status) != 0)
112606b9b3e0SSimon J. Gerraty 		JobFinishDoneExitedError(job, inout_status);
112706b9b3e0SSimon J. Gerraty 	else if (DEBUG(JOB)) {
112806b9b3e0SSimon J. Gerraty 		SwitchOutputTo(job->node);
112906b9b3e0SSimon J. Gerraty 		(void)printf("*** [%s] Completed successfully\n",
113006b9b3e0SSimon J. Gerraty 		    job->node->name);
113106b9b3e0SSimon J. Gerraty 	}
113206b9b3e0SSimon J. Gerraty }
113306b9b3e0SSimon J. Gerraty 
113406b9b3e0SSimon J. Gerraty static void
113506b9b3e0SSimon J. Gerraty JobFinishDoneSignaled(Job *job, WAIT_T status)
113606b9b3e0SSimon J. Gerraty {
113706b9b3e0SSimon J. Gerraty 	SwitchOutputTo(job->node);
1138b0c40a00SSimon J. Gerraty 	DebugFailedJob(job);
113906b9b3e0SSimon J. Gerraty 	(void)printf("*** [%s] Signal %d\n", job->node->name, WTERMSIG(status));
114006b9b3e0SSimon J. Gerraty 	if (deleteOnError)
114106b9b3e0SSimon J. Gerraty 		JobDeleteTarget(job->node);
114206b9b3e0SSimon J. Gerraty }
114306b9b3e0SSimon J. Gerraty 
114406b9b3e0SSimon J. Gerraty static void
114506b9b3e0SSimon J. Gerraty JobFinishDone(Job *job, WAIT_T *inout_status)
114606b9b3e0SSimon J. Gerraty {
114706b9b3e0SSimon J. Gerraty 	if (WIFEXITED(*inout_status))
114806b9b3e0SSimon J. Gerraty 		JobFinishDoneExited(job, inout_status);
114906b9b3e0SSimon J. Gerraty 	else
115006b9b3e0SSimon J. Gerraty 		JobFinishDoneSignaled(job, *inout_status);
115106b9b3e0SSimon J. Gerraty 
115206b9b3e0SSimon J. Gerraty 	(void)fflush(stdout);
115306b9b3e0SSimon J. Gerraty }
115406b9b3e0SSimon J. Gerraty 
115506b9b3e0SSimon J. Gerraty /*
115606b9b3e0SSimon J. Gerraty  * Do final processing for the given job including updating parent nodes and
1157e2eeea75SSimon J. Gerraty  * starting new jobs as available/necessary.
1158e2eeea75SSimon J. Gerraty  *
1159e2eeea75SSimon J. Gerraty  * Deferred commands for the job are placed on the .END node.
1160e2eeea75SSimon J. Gerraty  *
116106b9b3e0SSimon J. Gerraty  * If there was a serious error (job_errors != 0; not an ignored one), no more
1162e2eeea75SSimon J. Gerraty  * jobs will be started.
11633955d011SMarcel Moolenaar  *
11643955d011SMarcel Moolenaar  * Input:
11653955d011SMarcel Moolenaar  *	job		job to finish
11663955d011SMarcel Moolenaar  *	status		sub-why job went away
11673955d011SMarcel Moolenaar  */
11683955d011SMarcel Moolenaar static void
11693955d011SMarcel Moolenaar JobFinish (Job *job, WAIT_T status)
11703955d011SMarcel Moolenaar {
1171b0c40a00SSimon J. Gerraty 	bool done, return_job_token;
11723955d011SMarcel Moolenaar 
1173956e45f6SSimon J. Gerraty 	DEBUG3(JOB, "JobFinish: %d [%s], status %d\n",
11743955d011SMarcel Moolenaar 	    job->pid, job->node->name, status);
11753955d011SMarcel Moolenaar 
11763955d011SMarcel Moolenaar 	if ((WIFEXITED(status) &&
117706b9b3e0SSimon J. Gerraty 	     ((WEXITSTATUS(status) != 0 && !job->ignerr))) ||
117806b9b3e0SSimon J. Gerraty 	    WIFSIGNALED(status)) {
117906b9b3e0SSimon J. Gerraty 		/* Finished because of an error. */
118006b9b3e0SSimon J. Gerraty 
1181e2eeea75SSimon J. Gerraty 		JobClosePipes(job);
11823955d011SMarcel Moolenaar 		if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
118312904384SSimon J. Gerraty 			if (fclose(job->cmdFILE) != 0)
118412904384SSimon J. Gerraty 				Punt("Cannot write shell script for '%s': %s",
118512904384SSimon J. Gerraty 				    job->node->name, strerror(errno));
11863955d011SMarcel Moolenaar 			job->cmdFILE = NULL;
11873955d011SMarcel Moolenaar 		}
1188b0c40a00SSimon J. Gerraty 		done = true;
118906b9b3e0SSimon J. Gerraty 
11903955d011SMarcel Moolenaar 	} else if (WIFEXITED(status)) {
11913955d011SMarcel Moolenaar 		/*
119206b9b3e0SSimon J. Gerraty 		 * Deal with ignored errors in -B mode. We need to print a
119306b9b3e0SSimon J. Gerraty 		 * message telling of the ignored error as well as to run
119406b9b3e0SSimon J. Gerraty 		 * the next command.
11953955d011SMarcel Moolenaar 		 */
11963955d011SMarcel Moolenaar 		done = WEXITSTATUS(status) != 0;
119706b9b3e0SSimon J. Gerraty 
1198e2eeea75SSimon J. Gerraty 		JobClosePipes(job);
119906b9b3e0SSimon J. Gerraty 
12003955d011SMarcel Moolenaar 	} else {
120106b9b3e0SSimon J. Gerraty 		/* No need to close things down or anything. */
1202b0c40a00SSimon J. Gerraty 		done = false;
12033955d011SMarcel Moolenaar 	}
12043955d011SMarcel Moolenaar 
120506b9b3e0SSimon J. Gerraty 	if (done)
120606b9b3e0SSimon J. Gerraty 		JobFinishDone(job, &status);
12073955d011SMarcel Moolenaar 
12083955d011SMarcel Moolenaar #ifdef USE_META
12093955d011SMarcel Moolenaar 	if (useMeta) {
1210e2eeea75SSimon J. Gerraty 		int meta_status = meta_job_finish(job);
1211e2eeea75SSimon J. Gerraty 		if (meta_status != 0 && status == 0)
1212e2eeea75SSimon J. Gerraty 			status = meta_status;
12133955d011SMarcel Moolenaar 	}
12143955d011SMarcel Moolenaar #endif
12153955d011SMarcel Moolenaar 
1216b0c40a00SSimon J. Gerraty 	return_job_token = false;
12173955d011SMarcel Moolenaar 
12183955d011SMarcel Moolenaar 	Trace_Log(JOBEND, job);
121906b9b3e0SSimon J. Gerraty 	if (!job->special) {
1220e2eeea75SSimon J. Gerraty 		if (WAIT_STATUS(status) != 0 ||
1221e2eeea75SSimon J. Gerraty 		    (aborting == ABORT_ERROR) || aborting == ABORT_INTERRUPT)
1222b0c40a00SSimon J. Gerraty 			return_job_token = true;
12233955d011SMarcel Moolenaar 	}
12243955d011SMarcel Moolenaar 
1225e2eeea75SSimon J. Gerraty 	if (aborting != ABORT_ERROR && aborting != ABORT_INTERRUPT &&
12263955d011SMarcel Moolenaar 	    (WAIT_STATUS(status) == 0)) {
12273955d011SMarcel Moolenaar 		/*
122806b9b3e0SSimon J. Gerraty 		 * As long as we aren't aborting and the job didn't return a
122906b9b3e0SSimon J. Gerraty 		 * non-zero status that we shouldn't ignore, we call
123006b9b3e0SSimon J. Gerraty 		 * Make_Update to update the parents.
12313955d011SMarcel Moolenaar 		 */
1232956e45f6SSimon J. Gerraty 		JobSaveCommands(job);
12333955d011SMarcel Moolenaar 		job->node->made = MADE;
123406b9b3e0SSimon J. Gerraty 		if (!job->special)
1235b0c40a00SSimon J. Gerraty 			return_job_token = true;
12363955d011SMarcel Moolenaar 		Make_Update(job->node);
1237e2eeea75SSimon J. Gerraty 		job->status = JOB_ST_FREE;
123806b9b3e0SSimon J. Gerraty 	} else if (status != 0) {
123906b9b3e0SSimon J. Gerraty 		job_errors++;
1240e2eeea75SSimon J. Gerraty 		job->status = JOB_ST_FREE;
12413955d011SMarcel Moolenaar 	}
12423955d011SMarcel Moolenaar 
124306b9b3e0SSimon J. Gerraty 	if (job_errors > 0 && !opts.keepgoing && aborting != ABORT_INTERRUPT) {
124406b9b3e0SSimon J. Gerraty 		/* Prevent more jobs from getting started. */
124506b9b3e0SSimon J. Gerraty 		aborting = ABORT_ERROR;
124606b9b3e0SSimon J. Gerraty 	}
12473955d011SMarcel Moolenaar 
12483955d011SMarcel Moolenaar 	if (return_job_token)
12493955d011SMarcel Moolenaar 		Job_TokenReturn();
12503955d011SMarcel Moolenaar 
1251*d9a65c5dSSimon J. Gerraty 	if (aborting == ABORT_ERROR && jobTokensRunning == 0) {
1252*d9a65c5dSSimon J. Gerraty 		if (shouldDieQuietly(NULL, -1)) {
1253*d9a65c5dSSimon J. Gerraty 			/*
1254*d9a65c5dSSimon J. Gerraty 			 * TODO: better clean up properly, to avoid killing
1255*d9a65c5dSSimon J. Gerraty 			 *  child processes by SIGPIPE.
1256*d9a65c5dSSimon J. Gerraty 			 */
1257*d9a65c5dSSimon J. Gerraty 			exit(2);
1258*d9a65c5dSSimon J. Gerraty 		}
1259*d9a65c5dSSimon J. Gerraty 		Fatal("%d error%s", job_errors, job_errors == 1 ? "" : "s");
1260*d9a65c5dSSimon J. Gerraty 	}
12613955d011SMarcel Moolenaar }
1262e2eeea75SSimon J. Gerraty 
1263e2eeea75SSimon J. Gerraty static void
1264e2eeea75SSimon J. Gerraty TouchRegular(GNode *gn)
1265e2eeea75SSimon J. Gerraty {
1266e2eeea75SSimon J. Gerraty 	const char *file = GNode_Path(gn);
1267b0c40a00SSimon J. Gerraty 	struct utimbuf times;
1268e2eeea75SSimon J. Gerraty 	int fd;
1269e2eeea75SSimon J. Gerraty 	char c;
1270e2eeea75SSimon J. Gerraty 
1271b0c40a00SSimon J. Gerraty 	times.actime = now;
1272b0c40a00SSimon J. Gerraty 	times.modtime = now;
1273e2eeea75SSimon J. Gerraty 	if (utime(file, &times) >= 0)
1274e2eeea75SSimon J. Gerraty 		return;
1275e2eeea75SSimon J. Gerraty 
1276e2eeea75SSimon J. Gerraty 	fd = open(file, O_RDWR | O_CREAT, 0666);
1277e2eeea75SSimon J. Gerraty 	if (fd < 0) {
1278e2eeea75SSimon J. Gerraty 		(void)fprintf(stderr, "*** couldn't touch %s: %s\n",
1279e2eeea75SSimon J. Gerraty 		    file, strerror(errno));
1280e2eeea75SSimon J. Gerraty 		(void)fflush(stderr);
1281e2eeea75SSimon J. Gerraty 		return;		/* XXX: What about propagating the error? */
1282e2eeea75SSimon J. Gerraty 	}
1283e2eeea75SSimon J. Gerraty 
12849f45a3c8SSimon J. Gerraty 	/*
12859f45a3c8SSimon J. Gerraty 	 * Last resort: update the file's time stamps in the traditional way.
1286e2eeea75SSimon J. Gerraty 	 * XXX: This doesn't work for empty files, which are sometimes used
12879f45a3c8SSimon J. Gerraty 	 * as marker files.
12889f45a3c8SSimon J. Gerraty 	 */
1289e2eeea75SSimon J. Gerraty 	if (read(fd, &c, 1) == 1) {
1290e2eeea75SSimon J. Gerraty 		(void)lseek(fd, 0, SEEK_SET);
1291e2eeea75SSimon J. Gerraty 		while (write(fd, &c, 1) == -1 && errno == EAGAIN)
1292e2eeea75SSimon J. Gerraty 			continue;
1293e2eeea75SSimon J. Gerraty 	}
1294e2eeea75SSimon J. Gerraty 	(void)close(fd);	/* XXX: What about propagating the error? */
12953955d011SMarcel Moolenaar }
12963955d011SMarcel Moolenaar 
129706b9b3e0SSimon J. Gerraty /*
12986a7405f5SSimon J. Gerraty  * Touch the given target. Called by Job_Make when the -t flag was given.
12993955d011SMarcel Moolenaar  *
1300956e45f6SSimon J. Gerraty  * The modification date of the file is changed.
130106b9b3e0SSimon J. Gerraty  * If the file did not exist, it is created.
130206b9b3e0SSimon J. Gerraty  */
13033955d011SMarcel Moolenaar void
1304b0c40a00SSimon J. Gerraty Job_Touch(GNode *gn, bool echo)
13053955d011SMarcel Moolenaar {
130606b9b3e0SSimon J. Gerraty 	if (gn->type &
130706b9b3e0SSimon J. Gerraty 	    (OP_JOIN | OP_USE | OP_USEBEFORE | OP_EXEC | OP_OPTIONAL |
13083955d011SMarcel Moolenaar 	     OP_SPECIAL | OP_PHONY)) {
130906b9b3e0SSimon J. Gerraty 		/*
131006b9b3e0SSimon J. Gerraty 		 * These are "virtual" targets and should not really be
131106b9b3e0SSimon J. Gerraty 		 * created.
131206b9b3e0SSimon J. Gerraty 		 */
13133955d011SMarcel Moolenaar 		return;
13143955d011SMarcel Moolenaar 	}
13153955d011SMarcel Moolenaar 
131606b9b3e0SSimon J. Gerraty 	if (echo || !GNode_ShouldExecute(gn)) {
13173955d011SMarcel Moolenaar 		(void)fprintf(stdout, "touch %s\n", gn->name);
13183955d011SMarcel Moolenaar 		(void)fflush(stdout);
13193955d011SMarcel Moolenaar 	}
13203955d011SMarcel Moolenaar 
1321e2eeea75SSimon J. Gerraty 	if (!GNode_ShouldExecute(gn))
13223955d011SMarcel Moolenaar 		return;
13233955d011SMarcel Moolenaar 
132406b9b3e0SSimon J. Gerraty 	if (gn->type & OP_ARCHV)
13253955d011SMarcel Moolenaar 		Arch_Touch(gn);
132606b9b3e0SSimon J. Gerraty 	else if (gn->type & OP_LIB)
13273955d011SMarcel Moolenaar 		Arch_TouchLib(gn);
132806b9b3e0SSimon J. Gerraty 	else
1329e2eeea75SSimon J. Gerraty 		TouchRegular(gn);
13303955d011SMarcel Moolenaar }
13313955d011SMarcel Moolenaar 
133206b9b3e0SSimon J. Gerraty /*
133306b9b3e0SSimon J. Gerraty  * Make sure the given node has all the commands it needs.
1334956e45f6SSimon J. Gerraty  *
1335956e45f6SSimon J. Gerraty  * The node will have commands from the .DEFAULT rule added to it if it
1336956e45f6SSimon J. Gerraty  * needs them.
13373955d011SMarcel Moolenaar  *
13383955d011SMarcel Moolenaar  * Input:
13393955d011SMarcel Moolenaar  *	gn		The target whose commands need verifying
13403955d011SMarcel Moolenaar  *	abortProc	Function to abort with message
13413955d011SMarcel Moolenaar  *
13423955d011SMarcel Moolenaar  * Results:
1343b0c40a00SSimon J. Gerraty  *	true if the commands list is/was ok.
13443955d011SMarcel Moolenaar  */
1345b0c40a00SSimon J. Gerraty bool
13463955d011SMarcel Moolenaar Job_CheckCommands(GNode *gn, void (*abortProc)(const char *, ...))
13473955d011SMarcel Moolenaar {
1348956e45f6SSimon J. Gerraty 	if (GNode_IsTarget(gn))
1349b0c40a00SSimon J. Gerraty 		return true;
135006b9b3e0SSimon J. Gerraty 	if (!Lst_IsEmpty(&gn->commands))
1351b0c40a00SSimon J. Gerraty 		return true;
135206b9b3e0SSimon J. Gerraty 	if ((gn->type & OP_LIB) && !Lst_IsEmpty(&gn->children))
1353b0c40a00SSimon J. Gerraty 		return true;
1354956e45f6SSimon J. Gerraty 
13553955d011SMarcel Moolenaar 	/*
13563955d011SMarcel Moolenaar 	 * No commands. Look for .DEFAULT rule from which we might infer
1357e2eeea75SSimon J. Gerraty 	 * commands.
13583955d011SMarcel Moolenaar 	 */
135906b9b3e0SSimon J. Gerraty 	if (defaultNode != NULL && !Lst_IsEmpty(&defaultNode->commands) &&
1360e2eeea75SSimon J. Gerraty 	    !(gn->type & OP_SPECIAL)) {
13613955d011SMarcel Moolenaar 		/*
136206b9b3e0SSimon J. Gerraty 		 * The traditional Make only looks for a .DEFAULT if the node
136306b9b3e0SSimon J. Gerraty 		 * was never the target of an operator, so that's what we do
136406b9b3e0SSimon J. Gerraty 		 * too.
1365e2eeea75SSimon J. Gerraty 		 *
1366e2eeea75SSimon J. Gerraty 		 * The .DEFAULT node acts like a transformation rule, in that
13673955d011SMarcel Moolenaar 		 * gn also inherits any attributes or sources attached to
13683955d011SMarcel Moolenaar 		 * .DEFAULT itself.
13693955d011SMarcel Moolenaar 		 */
1370e2eeea75SSimon J. Gerraty 		Make_HandleUse(defaultNode, gn);
1371dba7b0efSSimon J. Gerraty 		Var_Set(gn, IMPSRC, GNode_VarTarget(gn));
1372b0c40a00SSimon J. Gerraty 		return true;
1373956e45f6SSimon J. Gerraty 	}
1374956e45f6SSimon J. Gerraty 
1375b0c40a00SSimon J. Gerraty 	Dir_UpdateMTime(gn, false);
1376e2eeea75SSimon J. Gerraty 	if (gn->mtime != 0 || (gn->type & OP_SPECIAL))
1377b0c40a00SSimon J. Gerraty 		return true;
1378956e45f6SSimon J. Gerraty 
13793955d011SMarcel Moolenaar 	/*
1380956e45f6SSimon J. Gerraty 	 * The node wasn't the target of an operator.  We have no .DEFAULT
13813955d011SMarcel Moolenaar 	 * rule to go on and the target doesn't already exist. There's
13823955d011SMarcel Moolenaar 	 * nothing more we can do for this branch. If the -k flag wasn't
13833955d011SMarcel Moolenaar 	 * given, we stop in our tracks, otherwise we just don't update
13843955d011SMarcel Moolenaar 	 * this node's parents so they never get examined.
13853955d011SMarcel Moolenaar 	 */
13863955d011SMarcel Moolenaar 
138712904384SSimon J. Gerraty 	if (gn->flags.fromDepend) {
13881748de26SSimon J. Gerraty 		if (!Job_RunTarget(".STALE", gn->fname))
138906b9b3e0SSimon J. Gerraty 			fprintf(stdout,
13909f45a3c8SSimon J. Gerraty 			    "%s: %s, %u: ignoring stale %s for %s\n",
13911748de26SSimon J. Gerraty 			    progname, gn->fname, gn->lineno, makeDependfile,
13921748de26SSimon J. Gerraty 			    gn->name);
1393b0c40a00SSimon J. Gerraty 		return true;
13943955d011SMarcel Moolenaar 	}
13953955d011SMarcel Moolenaar 
13963955d011SMarcel Moolenaar 	if (gn->type & OP_OPTIONAL) {
1397956e45f6SSimon J. Gerraty 		(void)fprintf(stdout, "%s: don't know how to make %s (%s)\n",
1398956e45f6SSimon J. Gerraty 		    progname, gn->name, "ignored");
13993955d011SMarcel Moolenaar 		(void)fflush(stdout);
1400b0c40a00SSimon J. Gerraty 		return true;
14013955d011SMarcel Moolenaar 	}
14023955d011SMarcel Moolenaar 
1403956e45f6SSimon J. Gerraty 	if (opts.keepgoing) {
1404956e45f6SSimon J. Gerraty 		(void)fprintf(stdout, "%s: don't know how to make %s (%s)\n",
1405956e45f6SSimon J. Gerraty 		    progname, gn->name, "continuing");
1406956e45f6SSimon J. Gerraty 		(void)fflush(stdout);
1407b0c40a00SSimon J. Gerraty 		return false;
1408956e45f6SSimon J. Gerraty 	}
1409956e45f6SSimon J. Gerraty 
1410d5e0a182SSimon J. Gerraty 	abortProc("don't know how to make %s. Stop", gn->name);
1411b0c40a00SSimon J. Gerraty 	return false;
1412956e45f6SSimon J. Gerraty }
1413956e45f6SSimon J. Gerraty 
141406b9b3e0SSimon J. Gerraty /*
141506b9b3e0SSimon J. Gerraty  * Execute the shell for the given job.
14163955d011SMarcel Moolenaar  *
141706b9b3e0SSimon J. Gerraty  * See Job_CatchOutput for handling the output of the shell.
141806b9b3e0SSimon J. Gerraty  */
14193955d011SMarcel Moolenaar static void
14203955d011SMarcel Moolenaar JobExec(Job *job, char **argv)
14213955d011SMarcel Moolenaar {
14223955d011SMarcel Moolenaar 	int cpid;		/* ID of new child */
14233955d011SMarcel Moolenaar 	sigset_t mask;
14243955d011SMarcel Moolenaar 
14253955d011SMarcel Moolenaar 	if (DEBUG(JOB)) {
14263955d011SMarcel Moolenaar 		int i;
14273955d011SMarcel Moolenaar 
1428e2eeea75SSimon J. Gerraty 		debug_printf("Running %s\n", job->node->name);
1429956e45f6SSimon J. Gerraty 		debug_printf("\tCommand: ");
14303955d011SMarcel Moolenaar 		for (i = 0; argv[i] != NULL; i++) {
1431956e45f6SSimon J. Gerraty 			debug_printf("%s ", argv[i]);
14323955d011SMarcel Moolenaar 		}
1433956e45f6SSimon J. Gerraty 		debug_printf("\n");
14343955d011SMarcel Moolenaar 	}
14353955d011SMarcel Moolenaar 
14363955d011SMarcel Moolenaar 	/*
1437d5e0a182SSimon J. Gerraty 	 * Some jobs produce no output, and it's disconcerting to have
14383955d011SMarcel Moolenaar 	 * no feedback of their running (since they produce no output, the
14393955d011SMarcel Moolenaar 	 * banner with their name in it never appears). This is an attempt to
14403955d011SMarcel Moolenaar 	 * provide that feedback, even if nothing follows it.
14413955d011SMarcel Moolenaar 	 */
144206b9b3e0SSimon J. Gerraty 	if (job->echo)
144306b9b3e0SSimon J. Gerraty 		SwitchOutputTo(job->node);
14443955d011SMarcel Moolenaar 
14453955d011SMarcel Moolenaar 	/* No interruptions until this job is on the `jobs' list */
14463955d011SMarcel Moolenaar 	JobSigLock(&mask);
14473955d011SMarcel Moolenaar 
14483955d011SMarcel Moolenaar 	/* Pre-emptively mark job running, pid still zero though */
1449e2eeea75SSimon J. Gerraty 	job->status = JOB_ST_RUNNING;
14503955d011SMarcel Moolenaar 
1451c59c3bf3SSimon J. Gerraty 	Var_ReexportVars(job->node);
145206b9b3e0SSimon J. Gerraty 
14536a7405f5SSimon J. Gerraty 	cpid = FORK_FUNCTION();
14543955d011SMarcel Moolenaar 	if (cpid == -1)
14556a7405f5SSimon J. Gerraty 		Punt("Cannot fork: %s", strerror(errno));
14563955d011SMarcel Moolenaar 
14573955d011SMarcel Moolenaar 	if (cpid == 0) {
14583955d011SMarcel Moolenaar 		/* Child */
14593955d011SMarcel Moolenaar 		sigset_t tmask;
14603955d011SMarcel Moolenaar 
14613955d011SMarcel Moolenaar #ifdef USE_META
14629f45a3c8SSimon J. Gerraty 		if (useMeta)
14633955d011SMarcel Moolenaar 			meta_job_child(job);
14643955d011SMarcel Moolenaar #endif
14653955d011SMarcel Moolenaar 		/*
146606b9b3e0SSimon J. Gerraty 		 * Reset all signal handlers; this is necessary because we
146706b9b3e0SSimon J. Gerraty 		 * also need to unblock signals before we exec(2).
14683955d011SMarcel Moolenaar 		 */
14693955d011SMarcel Moolenaar 		JobSigReset();
14703955d011SMarcel Moolenaar 
14713955d011SMarcel Moolenaar 		/* Now unblock signals */
14723955d011SMarcel Moolenaar 		sigemptyset(&tmask);
14733955d011SMarcel Moolenaar 		JobSigUnlock(&tmask);
14743955d011SMarcel Moolenaar 
14753955d011SMarcel Moolenaar 		/*
147606b9b3e0SSimon J. Gerraty 		 * Must duplicate the input stream down to the child's input
147706b9b3e0SSimon J. Gerraty 		 * and reset it to the beginning (again). Since the stream
147806b9b3e0SSimon J. Gerraty 		 * was marked close-on-exec, we must clear that bit in the
147906b9b3e0SSimon J. Gerraty 		 * new input.
14803955d011SMarcel Moolenaar 		 */
14818d5c8e21SSimon J. Gerraty 		if (dup2(fileno(job->cmdFILE), STDIN_FILENO) == -1)
1482956e45f6SSimon J. Gerraty 			execDie("dup2", "job->cmdFILE");
14838d5c8e21SSimon J. Gerraty 		if (fcntl(STDIN_FILENO, F_SETFD, 0) == -1)
1484956e45f6SSimon J. Gerraty 			execDie("fcntl clear close-on-exec", "stdin");
14858d5c8e21SSimon J. Gerraty 		if (lseek(STDIN_FILENO, 0, SEEK_SET) == -1)
1486956e45f6SSimon J. Gerraty 			execDie("lseek to 0", "stdin");
14873955d011SMarcel Moolenaar 
1488db29cad8SSimon J. Gerraty 		if (Always_pass_job_queue ||
1489db29cad8SSimon J. Gerraty 		    (job->node->type & (OP_MAKE | OP_SUBMAKE))) {
1490d5e0a182SSimon J. Gerraty 			/* Pass job token pipe to submakes. */
1491956e45f6SSimon J. Gerraty 			if (fcntl(tokenWaitJob.inPipe, F_SETFD, 0) == -1)
149206b9b3e0SSimon J. Gerraty 				execDie("clear close-on-exec",
149306b9b3e0SSimon J. Gerraty 				    "tokenWaitJob.inPipe");
1494956e45f6SSimon J. Gerraty 			if (fcntl(tokenWaitJob.outPipe, F_SETFD, 0) == -1)
149506b9b3e0SSimon J. Gerraty 				execDie("clear close-on-exec",
149606b9b3e0SSimon J. Gerraty 				    "tokenWaitJob.outPipe");
14973955d011SMarcel Moolenaar 		}
14983955d011SMarcel Moolenaar 
14993955d011SMarcel Moolenaar 		/*
15003955d011SMarcel Moolenaar 		 * Set up the child's output to be routed through the pipe
15013955d011SMarcel Moolenaar 		 * we've created for it.
15023955d011SMarcel Moolenaar 		 */
15038d5c8e21SSimon J. Gerraty 		if (dup2(job->outPipe, STDOUT_FILENO) == -1)
1504956e45f6SSimon J. Gerraty 			execDie("dup2", "job->outPipe");
1505956e45f6SSimon J. Gerraty 
15063955d011SMarcel Moolenaar 		/*
150706b9b3e0SSimon J. Gerraty 		 * The output channels are marked close on exec. This bit
15088d5c8e21SSimon J. Gerraty 		 * was duplicated by dup2 (on some systems), so we have
150906b9b3e0SSimon J. Gerraty 		 * to clear it before routing the shell's error output to
151006b9b3e0SSimon J. Gerraty 		 * the same place as its standard output.
15113955d011SMarcel Moolenaar 		 */
15128d5c8e21SSimon J. Gerraty 		if (fcntl(STDOUT_FILENO, F_SETFD, 0) == -1)
1513956e45f6SSimon J. Gerraty 			execDie("clear close-on-exec", "stdout");
15148d5c8e21SSimon J. Gerraty 		if (dup2(STDOUT_FILENO, STDERR_FILENO) == -1)
1515956e45f6SSimon J. Gerraty 			execDie("dup2", "1, 2");
15163955d011SMarcel Moolenaar 
15173955d011SMarcel Moolenaar 		/*
151806b9b3e0SSimon J. Gerraty 		 * We want to switch the child into a different process
151906b9b3e0SSimon J. Gerraty 		 * family so we can kill it and all its descendants in
152006b9b3e0SSimon J. Gerraty 		 * one fell swoop, by killing its process family, but not
152106b9b3e0SSimon J. Gerraty 		 * commit suicide.
15223955d011SMarcel Moolenaar 		 */
15233955d011SMarcel Moolenaar #if defined(HAVE_SETPGID)
15243955d011SMarcel Moolenaar 		(void)setpgid(0, getpid());
15253955d011SMarcel Moolenaar #else
15263955d011SMarcel Moolenaar # if defined(HAVE_SETSID)
15273955d011SMarcel Moolenaar 		/* XXX: dsl - I'm sure this should be setpgrp()... */
15283955d011SMarcel Moolenaar 		(void)setsid();
15293955d011SMarcel Moolenaar # else
15303955d011SMarcel Moolenaar 		(void)setpgrp(0, getpid());
15313955d011SMarcel Moolenaar # endif
15323955d011SMarcel Moolenaar #endif
15333955d011SMarcel Moolenaar 
15343955d011SMarcel Moolenaar 		(void)execv(shellPath, argv);
1535956e45f6SSimon J. Gerraty 		execDie("exec", shellPath);
15363955d011SMarcel Moolenaar 	}
15373955d011SMarcel Moolenaar 
15383955d011SMarcel Moolenaar 	/* Parent, continuing after the child exec */
15393955d011SMarcel Moolenaar 	job->pid = cpid;
15403955d011SMarcel Moolenaar 
15413955d011SMarcel Moolenaar 	Trace_Log(JOBSTART, job);
15423955d011SMarcel Moolenaar 
154349caa483SSimon J. Gerraty #ifdef USE_META
15449f45a3c8SSimon J. Gerraty 	if (useMeta)
154549caa483SSimon J. Gerraty 		meta_job_parent(job, cpid);
154649caa483SSimon J. Gerraty #endif
154749caa483SSimon J. Gerraty 
15483955d011SMarcel Moolenaar 	/*
15493955d011SMarcel Moolenaar 	 * Set the current position in the buffer to the beginning
15503955d011SMarcel Moolenaar 	 * and mark another stream to watch in the outputs mask
15513955d011SMarcel Moolenaar 	 */
15523955d011SMarcel Moolenaar 	job->curPos = 0;
15533955d011SMarcel Moolenaar 
15543955d011SMarcel Moolenaar 	watchfd(job);
15553955d011SMarcel Moolenaar 
15563955d011SMarcel Moolenaar 	if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
155712904384SSimon J. Gerraty 		if (fclose(job->cmdFILE) != 0)
155812904384SSimon J. Gerraty 			Punt("Cannot write shell script for '%s': %s",
155912904384SSimon J. Gerraty 			    job->node->name, strerror(errno));
15603955d011SMarcel Moolenaar 		job->cmdFILE = NULL;
15613955d011SMarcel Moolenaar 	}
15623955d011SMarcel Moolenaar 
1563dba7b0efSSimon J. Gerraty 	/* Now that the job is actually running, add it to the table. */
15643955d011SMarcel Moolenaar 	if (DEBUG(JOB)) {
1565956e45f6SSimon J. Gerraty 		debug_printf("JobExec(%s): pid %d added to jobs table\n",
15663955d011SMarcel Moolenaar 		    job->node->name, job->pid);
1567b0c40a00SSimon J. Gerraty 		DumpJobs("job started");
15683955d011SMarcel Moolenaar 	}
15693955d011SMarcel Moolenaar 	JobSigUnlock(&mask);
15703955d011SMarcel Moolenaar }
15713955d011SMarcel Moolenaar 
1572956e45f6SSimon J. Gerraty /* Create the argv needed to execute the shell for a given job. */
15733955d011SMarcel Moolenaar static void
15743955d011SMarcel Moolenaar JobMakeArgv(Job *job, char **argv)
15753955d011SMarcel Moolenaar {
15763955d011SMarcel Moolenaar 	int argc;
15773955d011SMarcel Moolenaar 	static char args[10];	/* For merged arguments */
15783955d011SMarcel Moolenaar 
15793955d011SMarcel Moolenaar 	argv[0] = UNCONST(shellName);
15803955d011SMarcel Moolenaar 	argc = 1;
15813955d011SMarcel Moolenaar 
158206b9b3e0SSimon J. Gerraty 	if ((shell->errFlag != NULL && shell->errFlag[0] != '-') ||
158306b9b3e0SSimon J. Gerraty 	    (shell->echoFlag != NULL && shell->echoFlag[0] != '-')) {
15843955d011SMarcel Moolenaar 		/*
158506b9b3e0SSimon J. Gerraty 		 * At least one of the flags doesn't have a minus before it,
158606b9b3e0SSimon J. Gerraty 		 * so merge them together. Have to do this because the Bourne
158706b9b3e0SSimon J. Gerraty 		 * shell thinks its second argument is a file to source.
158806b9b3e0SSimon J. Gerraty 		 * Grrrr. Note the ten-character limitation on the combined
158906b9b3e0SSimon J. Gerraty 		 * arguments.
159006b9b3e0SSimon J. Gerraty 		 *
159106b9b3e0SSimon J. Gerraty 		 * TODO: Research until when the above comments were
159206b9b3e0SSimon J. Gerraty 		 * practically relevant.
15933955d011SMarcel Moolenaar 		 */
1594e2eeea75SSimon J. Gerraty 		(void)snprintf(args, sizeof args, "-%s%s",
159506b9b3e0SSimon J. Gerraty 		    (job->ignerr ? "" :
159606b9b3e0SSimon J. Gerraty 			(shell->errFlag != NULL ? shell->errFlag : "")),
159706b9b3e0SSimon J. Gerraty 		    (!job->echo ? "" :
159806b9b3e0SSimon J. Gerraty 			(shell->echoFlag != NULL ? shell->echoFlag : "")));
15993955d011SMarcel Moolenaar 
160006b9b3e0SSimon J. Gerraty 		if (args[1] != '\0') {
16013955d011SMarcel Moolenaar 			argv[argc] = args;
16023955d011SMarcel Moolenaar 			argc++;
16033955d011SMarcel Moolenaar 		}
16043955d011SMarcel Moolenaar 	} else {
160506b9b3e0SSimon J. Gerraty 		if (!job->ignerr && shell->errFlag != NULL) {
160606b9b3e0SSimon J. Gerraty 			argv[argc] = UNCONST(shell->errFlag);
16073955d011SMarcel Moolenaar 			argc++;
16083955d011SMarcel Moolenaar 		}
160906b9b3e0SSimon J. Gerraty 		if (job->echo && shell->echoFlag != NULL) {
161006b9b3e0SSimon J. Gerraty 			argv[argc] = UNCONST(shell->echoFlag);
16113955d011SMarcel Moolenaar 			argc++;
16123955d011SMarcel Moolenaar 		}
16133955d011SMarcel Moolenaar 	}
16143955d011SMarcel Moolenaar 	argv[argc] = NULL;
16153955d011SMarcel Moolenaar }
16163955d011SMarcel Moolenaar 
161706b9b3e0SSimon J. Gerraty static void
1618b0c40a00SSimon J. Gerraty JobWriteShellCommands(Job *job, GNode *gn, bool *out_run)
16193955d011SMarcel Moolenaar {
16203955d011SMarcel Moolenaar 	/*
162106b9b3e0SSimon J. Gerraty 	 * tfile is the name of a file into which all shell commands
162206b9b3e0SSimon J. Gerraty 	 * are put. It is removed before the child shell is executed,
162306b9b3e0SSimon J. Gerraty 	 * unless DEBUG(SCRIPT) is set.
16243955d011SMarcel Moolenaar 	 */
1625dba7b0efSSimon J. Gerraty 	char tfile[MAXPATHLEN];
162606b9b3e0SSimon J. Gerraty 	int tfd;		/* File descriptor to the temp file */
162706b9b3e0SSimon J. Gerraty 
1628dba7b0efSSimon J. Gerraty 	tfd = Job_TempFile(TMPPAT, tfile, sizeof tfile);
16293955d011SMarcel Moolenaar 
16303955d011SMarcel Moolenaar 	job->cmdFILE = fdopen(tfd, "w+");
1631e2eeea75SSimon J. Gerraty 	if (job->cmdFILE == NULL)
16323955d011SMarcel Moolenaar 		Punt("Could not fdopen %s", tfile);
1633e2eeea75SSimon J. Gerraty 
1634956e45f6SSimon J. Gerraty 	(void)fcntl(fileno(job->cmdFILE), F_SETFD, FD_CLOEXEC);
16353955d011SMarcel Moolenaar 
16363955d011SMarcel Moolenaar #ifdef USE_META
16373955d011SMarcel Moolenaar 	if (useMeta) {
16383955d011SMarcel Moolenaar 		meta_job_start(job, gn);
163906b9b3e0SSimon J. Gerraty 		if (gn->type & OP_SILENT)	/* might have changed */
1640b0c40a00SSimon J. Gerraty 			job->echo = false;
16413955d011SMarcel Moolenaar 	}
16423955d011SMarcel Moolenaar #endif
16433955d011SMarcel Moolenaar 
1644b0c40a00SSimon J. Gerraty 	*out_run = JobWriteCommands(job);
164506b9b3e0SSimon J. Gerraty }
164606b9b3e0SSimon J. Gerraty 
16476a7405f5SSimon J. Gerraty void
16486a7405f5SSimon J. Gerraty Job_Make(GNode *gn)
164906b9b3e0SSimon J. Gerraty {
165006b9b3e0SSimon J. Gerraty 	Job *job;		/* new job descriptor */
165106b9b3e0SSimon J. Gerraty 	char *argv[10];		/* Argument vector to shell */
1652b0c40a00SSimon J. Gerraty 	bool cmdsOK;		/* true if the nodes commands were all right */
1653b0c40a00SSimon J. Gerraty 	bool run;
165406b9b3e0SSimon J. Gerraty 
165506b9b3e0SSimon J. Gerraty 	for (job = job_table; job < job_table_end; job++) {
165606b9b3e0SSimon J. Gerraty 		if (job->status == JOB_ST_FREE)
165706b9b3e0SSimon J. Gerraty 			break;
165806b9b3e0SSimon J. Gerraty 	}
165906b9b3e0SSimon J. Gerraty 	if (job >= job_table_end)
16606a7405f5SSimon J. Gerraty 		Punt("Job_Make no job slots vacant");
166106b9b3e0SSimon J. Gerraty 
166206b9b3e0SSimon J. Gerraty 	memset(job, 0, sizeof *job);
166306b9b3e0SSimon J. Gerraty 	job->node = gn;
166406b9b3e0SSimon J. Gerraty 	job->tailCmds = NULL;
166506b9b3e0SSimon J. Gerraty 	job->status = JOB_ST_SET_UP;
166606b9b3e0SSimon J. Gerraty 
16676a7405f5SSimon J. Gerraty 	job->special = (gn->type & OP_SPECIAL) != OP_NONE;
166806b9b3e0SSimon J. Gerraty 	job->ignerr = opts.ignoreErrors || gn->type & OP_IGNORE;
16699f45a3c8SSimon J. Gerraty 	job->echo = !(opts.silent || gn->type & OP_SILENT);
167006b9b3e0SSimon J. Gerraty 
167106b9b3e0SSimon J. Gerraty 	/*
167206b9b3e0SSimon J. Gerraty 	 * Check the commands now so any attributes from .DEFAULT have a
167306b9b3e0SSimon J. Gerraty 	 * chance to migrate to the node.
167406b9b3e0SSimon J. Gerraty 	 */
167506b9b3e0SSimon J. Gerraty 	cmdsOK = Job_CheckCommands(gn, Error);
167606b9b3e0SSimon J. Gerraty 
167706b9b3e0SSimon J. Gerraty 	job->inPollfd = NULL;
1678dba7b0efSSimon J. Gerraty 
1679dba7b0efSSimon J. Gerraty 	if (Lst_IsEmpty(&gn->commands)) {
1680dba7b0efSSimon J. Gerraty 		job->cmdFILE = stdout;
1681b0c40a00SSimon J. Gerraty 		run = false;
1682b0c40a00SSimon J. Gerraty 
1683b0c40a00SSimon J. Gerraty 		/*
1684b0c40a00SSimon J. Gerraty 		 * We're serious here, but if the commands were bogus, we're
1685b0c40a00SSimon J. Gerraty 		 * also dead...
1686b0c40a00SSimon J. Gerraty 		 */
1687b0c40a00SSimon J. Gerraty 		if (!cmdsOK) {
16889f45a3c8SSimon J. Gerraty 			PrintOnError(gn, "\n");	/* provide some clue */
1689b0c40a00SSimon J. Gerraty 			DieHorribly();
1690b0c40a00SSimon J. Gerraty 		}
1691dba7b0efSSimon J. Gerraty 	} else if (((gn->type & OP_MAKE) && !opts.noRecursiveExecute) ||
16929f45a3c8SSimon J. Gerraty 	    (!opts.noExecute && !opts.touch)) {
1693dba7b0efSSimon J. Gerraty 		/*
1694dba7b0efSSimon J. Gerraty 		 * The above condition looks very similar to
1695dba7b0efSSimon J. Gerraty 		 * GNode_ShouldExecute but is subtly different.  It prevents
1696dba7b0efSSimon J. Gerraty 		 * that .MAKE targets are touched since these are usually
1697dba7b0efSSimon J. Gerraty 		 * virtual targets.
1698dba7b0efSSimon J. Gerraty 		 */
1699dba7b0efSSimon J. Gerraty 
17006a7405f5SSimon J. Gerraty 		int parseErrorsBefore;
17016a7405f5SSimon J. Gerraty 
1702b0c40a00SSimon J. Gerraty 		/*
1703b0c40a00SSimon J. Gerraty 		 * We're serious here, but if the commands were bogus, we're
1704b0c40a00SSimon J. Gerraty 		 * also dead...
1705b0c40a00SSimon J. Gerraty 		 */
1706b0c40a00SSimon J. Gerraty 		if (!cmdsOK) {
17079f45a3c8SSimon J. Gerraty 			PrintOnError(gn, "\n");	/* provide some clue */
1708b0c40a00SSimon J. Gerraty 			DieHorribly();
1709b0c40a00SSimon J. Gerraty 		}
1710b0c40a00SSimon J. Gerraty 
17116a7405f5SSimon J. Gerraty 		parseErrorsBefore = parseErrors;
1712b0c40a00SSimon J. Gerraty 		JobWriteShellCommands(job, gn, &run);
17136a7405f5SSimon J. Gerraty 		if (parseErrors != parseErrorsBefore)
17146a7405f5SSimon J. Gerraty 			run = false;
1715dba7b0efSSimon J. Gerraty 		(void)fflush(job->cmdFILE);
1716956e45f6SSimon J. Gerraty 	} else if (!GNode_ShouldExecute(gn)) {
17173955d011SMarcel Moolenaar 		/*
1718b0c40a00SSimon J. Gerraty 		 * Just write all the commands to stdout in one fell swoop.
1719dba7b0efSSimon J. Gerraty 		 * This still sets up job->tailCmds correctly.
17203955d011SMarcel Moolenaar 		 */
172106b9b3e0SSimon J. Gerraty 		SwitchOutputTo(gn);
17223955d011SMarcel Moolenaar 		job->cmdFILE = stdout;
1723956e45f6SSimon J. Gerraty 		if (cmdsOK)
1724b0c40a00SSimon J. Gerraty 			JobWriteCommands(job);
1725b0c40a00SSimon J. Gerraty 		run = false;
1726dba7b0efSSimon J. Gerraty 		(void)fflush(job->cmdFILE);
17273955d011SMarcel Moolenaar 	} else {
172806b9b3e0SSimon J. Gerraty 		Job_Touch(gn, job->echo);
1729b0c40a00SSimon J. Gerraty 		run = false;
17303955d011SMarcel Moolenaar 	}
17313955d011SMarcel Moolenaar 
173206b9b3e0SSimon J. Gerraty 	/* If we're not supposed to execute a shell, don't. */
173306b9b3e0SSimon J. Gerraty 	if (!run) {
173406b9b3e0SSimon J. Gerraty 		if (!job->special)
17353955d011SMarcel Moolenaar 			Job_TokenReturn();
173606b9b3e0SSimon J. Gerraty 		/* Unlink and close the command file if we opened one */
1737e2eeea75SSimon J. Gerraty 		if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
17383955d011SMarcel Moolenaar 			(void)fclose(job->cmdFILE);
17393955d011SMarcel Moolenaar 			job->cmdFILE = NULL;
17403955d011SMarcel Moolenaar 		}
17413955d011SMarcel Moolenaar 
17423955d011SMarcel Moolenaar 		/*
174306b9b3e0SSimon J. Gerraty 		 * We only want to work our way up the graph if we aren't
174406b9b3e0SSimon J. Gerraty 		 * here because the commands for the job were no good.
17453955d011SMarcel Moolenaar 		 */
1746956e45f6SSimon J. Gerraty 		if (cmdsOK && aborting == ABORT_NONE) {
1747956e45f6SSimon J. Gerraty 			JobSaveCommands(job);
17483955d011SMarcel Moolenaar 			job->node->made = MADE;
17493955d011SMarcel Moolenaar 			Make_Update(job->node);
17503955d011SMarcel Moolenaar 		}
1751e2eeea75SSimon J. Gerraty 		job->status = JOB_ST_FREE;
17526a7405f5SSimon J. Gerraty 		return;
17533955d011SMarcel Moolenaar 	}
17543955d011SMarcel Moolenaar 
17553955d011SMarcel Moolenaar 	/*
175606b9b3e0SSimon J. Gerraty 	 * Set up the control arguments to the shell. This is based on the
175706b9b3e0SSimon J. Gerraty 	 * flags set earlier for this job.
17583955d011SMarcel Moolenaar 	 */
17593955d011SMarcel Moolenaar 	JobMakeArgv(job, argv);
17603955d011SMarcel Moolenaar 
17613955d011SMarcel Moolenaar 	/* Create the pipe by which we'll get the shell's output. */
17623955d011SMarcel Moolenaar 	JobCreatePipe(job, 3);
17633955d011SMarcel Moolenaar 
17643955d011SMarcel Moolenaar 	JobExec(job, argv);
17653955d011SMarcel Moolenaar }
17663955d011SMarcel Moolenaar 
176706b9b3e0SSimon J. Gerraty /*
1768b0c40a00SSimon J. Gerraty  * If the shell has an output filter (which only csh and ksh have by default),
1769b0c40a00SSimon J. Gerraty  * print the output of the child process, skipping the noPrint text of the
1770b0c40a00SSimon J. Gerraty  * shell.
1771b0c40a00SSimon J. Gerraty  *
1772b0c40a00SSimon J. Gerraty  * Return the part of the output that the calling function needs to output by
1773b0c40a00SSimon J. Gerraty  * itself.
177406b9b3e0SSimon J. Gerraty  */
17753955d011SMarcel Moolenaar static char *
1776548bfc56SSimon J. Gerraty PrintFilteredOutput(char *p, const char *endp)	/* XXX: p should be const */
17773955d011SMarcel Moolenaar {
1778d5e0a182SSimon J. Gerraty 	char *ep;		/* XXX: should be const */
17793955d011SMarcel Moolenaar 
178006b9b3e0SSimon J. Gerraty 	if (shell->noPrint == NULL || shell->noPrint[0] == '\0')
1781d5e0a182SSimon J. Gerraty 		return p;
1782e2eeea75SSimon J. Gerraty 
178306b9b3e0SSimon J. Gerraty 	/*
178406b9b3e0SSimon J. Gerraty 	 * XXX: What happens if shell->noPrint occurs on the boundary of
178506b9b3e0SSimon J. Gerraty 	 * the buffer?  To work correctly in all cases, this should rather
178606b9b3e0SSimon J. Gerraty 	 * be a proper stream filter instead of doing string matching on
178706b9b3e0SSimon J. Gerraty 	 * selected chunks of the output.
178806b9b3e0SSimon J. Gerraty 	 */
1789d5e0a182SSimon J. Gerraty 	while ((ep = strstr(p, shell->noPrint)) != NULL) {
1790d5e0a182SSimon J. Gerraty 		if (ep != p) {
1791d5e0a182SSimon J. Gerraty 			*ep = '\0';	/* XXX: avoid writing to the buffer */
17923955d011SMarcel Moolenaar 			/*
17933955d011SMarcel Moolenaar 			 * The only way there wouldn't be a newline after
17943955d011SMarcel Moolenaar 			 * this line is if it were the last in the buffer.
179506b9b3e0SSimon J. Gerraty 			 * however, since the noPrint output comes after it,
17963955d011SMarcel Moolenaar 			 * there must be a newline, so we don't print one.
17973955d011SMarcel Moolenaar 			 */
179806b9b3e0SSimon J. Gerraty 			/* XXX: What about null bytes in the output? */
1799d5e0a182SSimon J. Gerraty 			(void)fprintf(stdout, "%s", p);
18003955d011SMarcel Moolenaar 			(void)fflush(stdout);
18013955d011SMarcel Moolenaar 		}
1802d5e0a182SSimon J. Gerraty 		p = ep + shell->noPrintLen;
1803d5e0a182SSimon J. Gerraty 		if (p == endp)
180406b9b3e0SSimon J. Gerraty 			break;
1805d5e0a182SSimon J. Gerraty 		p++;		/* skip over the (XXX: assumed) newline */
1806d5e0a182SSimon J. Gerraty 		pp_skip_whitespace(&p);
18073955d011SMarcel Moolenaar 	}
1808d5e0a182SSimon J. Gerraty 	return p;
18093955d011SMarcel Moolenaar }
18103955d011SMarcel Moolenaar 
1811e2eeea75SSimon J. Gerraty /*
1812e2eeea75SSimon J. Gerraty  * This function is called whenever there is something to read on the pipe.
1813e2eeea75SSimon J. Gerraty  * We collect more output from the given job and store it in the job's
1814e2eeea75SSimon J. Gerraty  * outBuf. If this makes up a line, we print it tagged by the job's
1815e2eeea75SSimon J. Gerraty  * identifier, as necessary.
1816e2eeea75SSimon J. Gerraty  *
1817e2eeea75SSimon J. Gerraty  * In the output of the shell, the 'noPrint' lines are removed. If the
1818e2eeea75SSimon J. Gerraty  * command is not alone on the line (the character after it is not \0 or
1819e2eeea75SSimon J. Gerraty  * \n), we do print whatever follows it.
18203955d011SMarcel Moolenaar  *
18213955d011SMarcel Moolenaar  * Input:
18223955d011SMarcel Moolenaar  *	job		the job whose output needs printing
1823b0c40a00SSimon J. Gerraty  *	finish		true if this is the last time we'll be called
18243955d011SMarcel Moolenaar  *			for this job
18253955d011SMarcel Moolenaar  */
1826956e45f6SSimon J. Gerraty static void
1827b0c40a00SSimon J. Gerraty CollectOutput(Job *job, bool finish)
18283955d011SMarcel Moolenaar {
1829b0c40a00SSimon J. Gerraty 	bool gotNL;		/* true if got a newline */
1830b0c40a00SSimon J. Gerraty 	bool fbuf;		/* true if our buffer filled up */
1831956e45f6SSimon J. Gerraty 	size_t nr;		/* number of bytes read */
1832956e45f6SSimon J. Gerraty 	size_t i;		/* auxiliary index into outBuf */
1833956e45f6SSimon J. Gerraty 	size_t max;		/* limit for i (end of current data) */
1834956e45f6SSimon J. Gerraty 	ssize_t nRead;		/* (Temporary) number of bytes read */
18353955d011SMarcel Moolenaar 
183606b9b3e0SSimon J. Gerraty 	/* Read as many bytes as will fit in the buffer. */
1837e2eeea75SSimon J. Gerraty again:
1838b0c40a00SSimon J. Gerraty 	gotNL = false;
1839b0c40a00SSimon J. Gerraty 	fbuf = false;
18403955d011SMarcel Moolenaar 
18413955d011SMarcel Moolenaar 	nRead = read(job->inPipe, &job->outBuf[job->curPos],
18423955d011SMarcel Moolenaar 	    JOB_BUFSIZE - job->curPos);
18433955d011SMarcel Moolenaar 	if (nRead < 0) {
18443955d011SMarcel Moolenaar 		if (errno == EAGAIN)
18453955d011SMarcel Moolenaar 			return;
18464fde40d9SSimon J. Gerraty 		if (DEBUG(JOB))
1847b0c40a00SSimon J. Gerraty 			perror("CollectOutput(piperead)");
18483955d011SMarcel Moolenaar 		nr = 0;
18494fde40d9SSimon J. Gerraty 	} else
1850956e45f6SSimon J. Gerraty 		nr = (size_t)nRead;
18514fde40d9SSimon J. Gerraty 
18524fde40d9SSimon J. Gerraty 	if (nr == 0)
18534fde40d9SSimon J. Gerraty 		finish = false;	/* stop looping */
18543955d011SMarcel Moolenaar 
18553955d011SMarcel Moolenaar 	/*
18563955d011SMarcel Moolenaar 	 * If we hit the end-of-file (the job is dead), we must flush its
18573955d011SMarcel Moolenaar 	 * remaining output, so pretend we read a newline if there's any
18583955d011SMarcel Moolenaar 	 * output remaining in the buffer.
18593955d011SMarcel Moolenaar 	 */
1860e2eeea75SSimon J. Gerraty 	if (nr == 0 && job->curPos != 0) {
18613955d011SMarcel Moolenaar 		job->outBuf[job->curPos] = '\n';
18623955d011SMarcel Moolenaar 		nr = 1;
18633955d011SMarcel Moolenaar 	}
18643955d011SMarcel Moolenaar 
18653955d011SMarcel Moolenaar 	max = job->curPos + nr;
18664fde40d9SSimon J. Gerraty 	for (i = job->curPos; i < max; i++)
18674fde40d9SSimon J. Gerraty 		if (job->outBuf[i] == '\0')
18684fde40d9SSimon J. Gerraty 			job->outBuf[i] = ' ';
18694fde40d9SSimon J. Gerraty 
18704fde40d9SSimon J. Gerraty 	/* Look for the last newline in the bytes we just got. */
187106b9b3e0SSimon J. Gerraty 	for (i = job->curPos + nr - 1;
187206b9b3e0SSimon J. Gerraty 	     i >= job->curPos && i != (size_t)-1; i--) {
18733955d011SMarcel Moolenaar 		if (job->outBuf[i] == '\n') {
1874b0c40a00SSimon J. Gerraty 			gotNL = true;
18753955d011SMarcel Moolenaar 			break;
18763955d011SMarcel Moolenaar 		}
18773955d011SMarcel Moolenaar 	}
18783955d011SMarcel Moolenaar 
18793955d011SMarcel Moolenaar 	if (!gotNL) {
18803955d011SMarcel Moolenaar 		job->curPos += nr;
18813955d011SMarcel Moolenaar 		if (job->curPos == JOB_BUFSIZE) {
18823955d011SMarcel Moolenaar 			/*
18833955d011SMarcel Moolenaar 			 * If we've run out of buffer space, we have no choice
18843955d011SMarcel Moolenaar 			 * but to print the stuff. sigh.
18853955d011SMarcel Moolenaar 			 */
1886b0c40a00SSimon J. Gerraty 			fbuf = true;
18873955d011SMarcel Moolenaar 			i = job->curPos;
18883955d011SMarcel Moolenaar 		}
18893955d011SMarcel Moolenaar 	}
18903955d011SMarcel Moolenaar 	if (gotNL || fbuf) {
18913955d011SMarcel Moolenaar 		/*
18923955d011SMarcel Moolenaar 		 * Need to send the output to the screen. Null terminate it
18933955d011SMarcel Moolenaar 		 * first, overwriting the newline character if there was one.
18943955d011SMarcel Moolenaar 		 * So long as the line isn't one we should filter (according
18953955d011SMarcel Moolenaar 		 * to the shell description), we print the line, preceded
18963955d011SMarcel Moolenaar 		 * by a target banner if this target isn't the same as the
18973955d011SMarcel Moolenaar 		 * one for which we last printed something.
18983955d011SMarcel Moolenaar 		 * The rest of the data in the buffer are then shifted down
18993955d011SMarcel Moolenaar 		 * to the start of the buffer and curPos is set accordingly.
19003955d011SMarcel Moolenaar 		 */
19013955d011SMarcel Moolenaar 		job->outBuf[i] = '\0';
19023955d011SMarcel Moolenaar 		if (i >= job->curPos) {
1903d5e0a182SSimon J. Gerraty 			char *p;
19043955d011SMarcel Moolenaar 
1905b0c40a00SSimon J. Gerraty 			/*
1906b0c40a00SSimon J. Gerraty 			 * FIXME: SwitchOutputTo should be here, according to
1907b0c40a00SSimon J. Gerraty 			 * the comment above.  But since PrintOutput does not
1908b0c40a00SSimon J. Gerraty 			 * do anything in the default shell, this bug has gone
1909b0c40a00SSimon J. Gerraty 			 * unnoticed until now.
1910b0c40a00SSimon J. Gerraty 			 */
1911d5e0a182SSimon J. Gerraty 			p = PrintFilteredOutput(job->outBuf, &job->outBuf[i]);
19123955d011SMarcel Moolenaar 
19133955d011SMarcel Moolenaar 			/*
1914b0c40a00SSimon J. Gerraty 			 * There's still more in the output buffer. This time,
191506b9b3e0SSimon J. Gerraty 			 * though, we know there's no newline at the end, so
191606b9b3e0SSimon J. Gerraty 			 * we add one of our own free will.
19173955d011SMarcel Moolenaar 			 */
1918d5e0a182SSimon J. Gerraty 			if (*p != '\0') {
19199f45a3c8SSimon J. Gerraty 				if (!opts.silent)
192006b9b3e0SSimon J. Gerraty 					SwitchOutputTo(job->node);
19213955d011SMarcel Moolenaar #ifdef USE_META
19223955d011SMarcel Moolenaar 				if (useMeta) {
1923d5e0a182SSimon J. Gerraty 					meta_job_output(job, p,
192406b9b3e0SSimon J. Gerraty 					    gotNL ? "\n" : "");
19253955d011SMarcel Moolenaar 				}
19263955d011SMarcel Moolenaar #endif
1927d5e0a182SSimon J. Gerraty 				(void)fprintf(stdout, "%s%s", p,
192806b9b3e0SSimon J. Gerraty 				    gotNL ? "\n" : "");
19293955d011SMarcel Moolenaar 				(void)fflush(stdout);
19303955d011SMarcel Moolenaar 			}
19313955d011SMarcel Moolenaar 		}
19323955d011SMarcel Moolenaar 		/*
193306b9b3e0SSimon J. Gerraty 		 * max is the last offset still in the buffer. Move any
193406b9b3e0SSimon J. Gerraty 		 * remaining characters to the start of the buffer and
193506b9b3e0SSimon J. Gerraty 		 * update the end marker curPos.
19363955d011SMarcel Moolenaar 		 */
1937db29cad8SSimon J. Gerraty 		if (i < max) {
193806b9b3e0SSimon J. Gerraty 			(void)memmove(job->outBuf, &job->outBuf[i + 1],
193906b9b3e0SSimon J. Gerraty 			    max - (i + 1));
1940db29cad8SSimon J. Gerraty 			job->curPos = max - (i + 1);
1941db29cad8SSimon J. Gerraty 		} else {
1942db29cad8SSimon J. Gerraty 			assert(i == max);
19433955d011SMarcel Moolenaar 			job->curPos = 0;
19443955d011SMarcel Moolenaar 		}
19453955d011SMarcel Moolenaar 	}
19463955d011SMarcel Moolenaar 	if (finish) {
19473955d011SMarcel Moolenaar 		/*
19483955d011SMarcel Moolenaar 		 * If the finish flag is true, we must loop until we hit
19493955d011SMarcel Moolenaar 		 * end-of-file on the pipe. This is guaranteed to happen
19503955d011SMarcel Moolenaar 		 * eventually since the other end of the pipe is now closed
19513955d011SMarcel Moolenaar 		 * (we closed it explicitly and the child has exited). When
1952b0c40a00SSimon J. Gerraty 		 * we do get an EOF, finish will be set false and we'll fall
19533955d011SMarcel Moolenaar 		 * through and out.
19543955d011SMarcel Moolenaar 		 */
1955e2eeea75SSimon J. Gerraty 		goto again;
19563955d011SMarcel Moolenaar 	}
19573955d011SMarcel Moolenaar }
19583955d011SMarcel Moolenaar 
19593955d011SMarcel Moolenaar static void
19603955d011SMarcel Moolenaar JobRun(GNode *targ)
19613955d011SMarcel Moolenaar {
19626a7405f5SSimon J. Gerraty 	/* Don't let these special jobs overlap with other unrelated jobs. */
19633955d011SMarcel Moolenaar 	Compat_Make(targ, targ);
19646a7405f5SSimon J. Gerraty 	if (GNode_IsError(targ)) {
19659f45a3c8SSimon J. Gerraty 		PrintOnError(targ, "\n\nStop.\n");
19663955d011SMarcel Moolenaar 		exit(1);
19673955d011SMarcel Moolenaar 	}
19683955d011SMarcel Moolenaar }
19693955d011SMarcel Moolenaar 
19703955d011SMarcel Moolenaar void
19713955d011SMarcel Moolenaar Job_CatchChildren(void)
19723955d011SMarcel Moolenaar {
1973*d9a65c5dSSimon J. Gerraty 	int pid;
1974*d9a65c5dSSimon J. Gerraty 	WAIT_T status;
19753955d011SMarcel Moolenaar 
19763955d011SMarcel Moolenaar 	if (jobTokensRunning == 0)
19773955d011SMarcel Moolenaar 		return;
1978dba7b0efSSimon J. Gerraty 	if (caught_sigchld == 0)
1979dba7b0efSSimon J. Gerraty 		return;
1980dba7b0efSSimon J. Gerraty 	caught_sigchld = 0;
1981dba7b0efSSimon J. Gerraty 
19823955d011SMarcel Moolenaar 	while ((pid = waitpid((pid_t)-1, &status, WNOHANG | WUNTRACED)) > 0) {
198306b9b3e0SSimon J. Gerraty 		DEBUG2(JOB, "Process %d exited/stopped status %x.\n",
198406b9b3e0SSimon J. Gerraty 		    pid, WAIT_STATUS(status));
1985b0c40a00SSimon J. Gerraty 		JobReapChild(pid, status, true);
19863955d011SMarcel Moolenaar 	}
19873955d011SMarcel Moolenaar }
19883955d011SMarcel Moolenaar 
19893955d011SMarcel Moolenaar /*
19903955d011SMarcel Moolenaar  * It is possible that wait[pid]() was called from elsewhere,
19913955d011SMarcel Moolenaar  * this lets us reap jobs regardless.
19923955d011SMarcel Moolenaar  */
19933955d011SMarcel Moolenaar void
1994b0c40a00SSimon J. Gerraty JobReapChild(pid_t pid, WAIT_T status, bool isJobs)
19953955d011SMarcel Moolenaar {
1996*d9a65c5dSSimon J. Gerraty 	Job *job;
19973955d011SMarcel Moolenaar 
19983955d011SMarcel Moolenaar 	if (jobTokensRunning == 0)
19993955d011SMarcel Moolenaar 		return;
20003955d011SMarcel Moolenaar 
20013955d011SMarcel Moolenaar 	job = JobFindPid(pid, JOB_ST_RUNNING, isJobs);
20023955d011SMarcel Moolenaar 	if (job == NULL) {
2003*d9a65c5dSSimon J. Gerraty 		if (isJobs && !lurking_children)
200406b9b3e0SSimon J. Gerraty 			Error("Child (%d) status %x not in table?",
200506b9b3e0SSimon J. Gerraty 			    pid, status);
2006*d9a65c5dSSimon J. Gerraty 		return;
20073955d011SMarcel Moolenaar 	}
2008*d9a65c5dSSimon J. Gerraty 
20093955d011SMarcel Moolenaar 	if (WIFSTOPPED(status)) {
201006b9b3e0SSimon J. Gerraty 		DEBUG2(JOB, "Process %d (%s) stopped.\n",
201106b9b3e0SSimon J. Gerraty 		    job->pid, job->node->name);
20123955d011SMarcel Moolenaar 		if (!make_suspended) {
20133955d011SMarcel Moolenaar 			switch (WSTOPSIG(status)) {
20143955d011SMarcel Moolenaar 			case SIGTSTP:
201506b9b3e0SSimon J. Gerraty 				(void)printf("*** [%s] Suspended\n",
201606b9b3e0SSimon J. Gerraty 				    job->node->name);
20173955d011SMarcel Moolenaar 				break;
20183955d011SMarcel Moolenaar 			case SIGSTOP:
201906b9b3e0SSimon J. Gerraty 				(void)printf("*** [%s] Stopped\n",
202006b9b3e0SSimon J. Gerraty 				    job->node->name);
20213955d011SMarcel Moolenaar 				break;
20223955d011SMarcel Moolenaar 			default:
20233955d011SMarcel Moolenaar 				(void)printf("*** [%s] Stopped -- signal %d\n",
20243955d011SMarcel Moolenaar 				    job->node->name, WSTOPSIG(status));
20253955d011SMarcel Moolenaar 			}
2026b0c40a00SSimon J. Gerraty 			job->suspended = true;
20273955d011SMarcel Moolenaar 		}
20283955d011SMarcel Moolenaar 		(void)fflush(stdout);
20293955d011SMarcel Moolenaar 		return;
20303955d011SMarcel Moolenaar 	}
20313955d011SMarcel Moolenaar 
2032e2eeea75SSimon J. Gerraty 	job->status = JOB_ST_FINISHED;
20333955d011SMarcel Moolenaar 	job->exit_status = WAIT_STATUS(status);
2034c59c3bf3SSimon J. Gerraty 	if (WIFEXITED(status))
2035c59c3bf3SSimon J. Gerraty 		job->node->exit_status = WEXITSTATUS(status);
20363955d011SMarcel Moolenaar 
20373955d011SMarcel Moolenaar 	JobFinish(job, status);
20383955d011SMarcel Moolenaar }
20393955d011SMarcel Moolenaar 
20403955d011SMarcel Moolenaar void
20413955d011SMarcel Moolenaar Job_CatchOutput(void)
20423955d011SMarcel Moolenaar {
20433955d011SMarcel Moolenaar 	int nready;
20443955d011SMarcel Moolenaar 	Job *job;
2045956e45f6SSimon J. Gerraty 	unsigned int i;
20463955d011SMarcel Moolenaar 
20473955d011SMarcel Moolenaar 	(void)fflush(stdout);
20483955d011SMarcel Moolenaar 
2049*d9a65c5dSSimon J. Gerraty 	/* Skip the first fd in the list, as it is the job token pipe. */
20501748de26SSimon J. Gerraty 	do {
2051dba7b0efSSimon J. Gerraty 		nready = poll(fds + 1 - wantToken, fdsLen - 1 + wantToken,
205206b9b3e0SSimon J. Gerraty 		    POLL_MSEC);
20531748de26SSimon J. Gerraty 	} while (nready < 0 && errno == EINTR);
20543955d011SMarcel Moolenaar 
20551748de26SSimon J. Gerraty 	if (nready < 0)
20561748de26SSimon J. Gerraty 		Punt("poll: %s", strerror(errno));
20571748de26SSimon J. Gerraty 
20581748de26SSimon J. Gerraty 	if (nready > 0 && readyfd(&childExitJob)) {
20593955d011SMarcel Moolenaar 		char token = 0;
2060*d9a65c5dSSimon J. Gerraty 		ssize_t count = read(childExitJob.inPipe, &token, 1);
2061dba7b0efSSimon J. Gerraty 		if (count == 1) {
20623955d011SMarcel Moolenaar 			if (token == DO_JOB_RESUME[0])
206306b9b3e0SSimon J. Gerraty 				/*
206406b9b3e0SSimon J. Gerraty 				 * Complete relay requested from our SIGCONT
2065*d9a65c5dSSimon J. Gerraty 				 * handler.
206606b9b3e0SSimon J. Gerraty 				 */
20673955d011SMarcel Moolenaar 				JobRestartJobs();
2068dba7b0efSSimon J. Gerraty 		} else if (count == 0)
2069dba7b0efSSimon J. Gerraty 			Punt("unexpected eof on token pipe");
207012904384SSimon J. Gerraty 		else if (errno != EAGAIN)
2071dba7b0efSSimon J. Gerraty 			Punt("token pipe read: %s", strerror(errno));
2072e2eeea75SSimon J. Gerraty 		nready--;
20733955d011SMarcel Moolenaar 	}
20743955d011SMarcel Moolenaar 
20751748de26SSimon J. Gerraty 	Job_CatchChildren();
20761748de26SSimon J. Gerraty 	if (nready == 0)
20773955d011SMarcel Moolenaar 		return;
20783955d011SMarcel Moolenaar 
2079dba7b0efSSimon J. Gerraty 	for (i = npseudojobs * nfds_per_job(); i < fdsLen; i++) {
208006b9b3e0SSimon J. Gerraty 		if (fds[i].revents == 0)
20813955d011SMarcel Moolenaar 			continue;
2082dba7b0efSSimon J. Gerraty 		job = jobByFdIndex[i];
2083e2eeea75SSimon J. Gerraty 		if (job->status == JOB_ST_RUNNING)
2084b0c40a00SSimon J. Gerraty 			CollectOutput(job, false);
208549caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
208649caa483SSimon J. Gerraty 		/*
208749caa483SSimon J. Gerraty 		 * With meta mode, we may have activity on the job's filemon
208806b9b3e0SSimon J. Gerraty 		 * descriptor too, which at the moment is any pollfd other
208906b9b3e0SSimon J. Gerraty 		 * than job->inPollfd.
209049caa483SSimon J. Gerraty 		 */
209149caa483SSimon J. Gerraty 		if (useMeta && job->inPollfd != &fds[i]) {
20929f45a3c8SSimon J. Gerraty 			if (meta_job_event(job) <= 0)
209349caa483SSimon J. Gerraty 				fds[i].events = 0;	/* never mind */
209449caa483SSimon J. Gerraty 		}
209549caa483SSimon J. Gerraty #endif
20961748de26SSimon J. Gerraty 		if (--nready == 0)
20971748de26SSimon J. Gerraty 			return;
20983955d011SMarcel Moolenaar 	}
20993955d011SMarcel Moolenaar }
21003955d011SMarcel Moolenaar 
210106b9b3e0SSimon J. Gerraty static void
210206b9b3e0SSimon J. Gerraty InitShellNameAndPath(void)
210306b9b3e0SSimon J. Gerraty {
210406b9b3e0SSimon J. Gerraty 	shellName = shell->name;
210506b9b3e0SSimon J. Gerraty 
210606b9b3e0SSimon J. Gerraty #ifdef DEFSHELL_CUSTOM
210706b9b3e0SSimon J. Gerraty 	if (shellName[0] == '/') {
21088d5c8e21SSimon J. Gerraty 		shellPath = bmake_strdup(shellName);
210906b9b3e0SSimon J. Gerraty 		shellName = str_basename(shellPath);
211006b9b3e0SSimon J. Gerraty 		return;
211106b9b3e0SSimon J. Gerraty 	}
211206b9b3e0SSimon J. Gerraty #endif
211312904384SSimon J. Gerraty #ifdef DEFSHELL_PATH
211422619282SSimon J. Gerraty 	shellPath = bmake_strdup(DEFSHELL_PATH);
211512904384SSimon J. Gerraty #else
211606b9b3e0SSimon J. Gerraty 	shellPath = str_concat3(_PATH_DEFSHELLDIR, "/", shellName);
211712904384SSimon J. Gerraty #endif
21183955d011SMarcel Moolenaar }
21193955d011SMarcel Moolenaar 
21203955d011SMarcel Moolenaar void
21213955d011SMarcel Moolenaar Shell_Init(void)
21223955d011SMarcel Moolenaar {
212306b9b3e0SSimon J. Gerraty 	if (shellPath == NULL)
212406b9b3e0SSimon J. Gerraty 		InitShellNameAndPath();
212506b9b3e0SSimon J. Gerraty 
2126c9f4001fSSimon J. Gerraty 	Var_SetWithFlags(SCOPE_CMDLINE, ".SHELL", shellPath,
2127c9f4001fSSimon J. Gerraty 			 VAR_SET_INTERNAL|VAR_SET_READONLY);
212806b9b3e0SSimon J. Gerraty 	if (shell->errFlag == NULL)
212906b9b3e0SSimon J. Gerraty 		shell->errFlag = "";
213006b9b3e0SSimon J. Gerraty 	if (shell->echoFlag == NULL)
213106b9b3e0SSimon J. Gerraty 		shell->echoFlag = "";
213206b9b3e0SSimon J. Gerraty 	if (shell->hasErrCtl && shell->errFlag[0] != '\0') {
213306b9b3e0SSimon J. Gerraty 		if (shellErrFlag != NULL &&
213406b9b3e0SSimon J. Gerraty 		    strcmp(shell->errFlag, &shellErrFlag[1]) != 0) {
213551ee2c1cSSimon J. Gerraty 			free(shellErrFlag);
213651ee2c1cSSimon J. Gerraty 			shellErrFlag = NULL;
213751ee2c1cSSimon J. Gerraty 		}
21389f45a3c8SSimon J. Gerraty 		if (shellErrFlag == NULL)
21399f45a3c8SSimon J. Gerraty 			shellErrFlag = str_concat2("-", shell->errFlag);
214006b9b3e0SSimon J. Gerraty 	} else if (shellErrFlag != NULL) {
214151ee2c1cSSimon J. Gerraty 		free(shellErrFlag);
214251ee2c1cSSimon J. Gerraty 		shellErrFlag = NULL;
214351ee2c1cSSimon J. Gerraty 	}
21443955d011SMarcel Moolenaar }
21453955d011SMarcel Moolenaar 
2146*d9a65c5dSSimon J. Gerraty /* Return the shell string literal that results in a newline character. */
21473955d011SMarcel Moolenaar const char *
21483955d011SMarcel Moolenaar Shell_GetNewline(void)
21493955d011SMarcel Moolenaar {
215006b9b3e0SSimon J. Gerraty 	return shell->newline;
21513955d011SMarcel Moolenaar }
21523955d011SMarcel Moolenaar 
21533955d011SMarcel Moolenaar void
21543955d011SMarcel Moolenaar Job_SetPrefix(void)
21553955d011SMarcel Moolenaar {
2156d5e0a182SSimon J. Gerraty 	if (targPrefix != NULL)
21573955d011SMarcel Moolenaar 		free(targPrefix);
2158d5e0a182SSimon J. Gerraty 	else if (!Var_Exists(SCOPE_GLOBAL, ".MAKE.JOB.PREFIX"))
21598c973ee2SSimon J. Gerraty 		Global_Set(".MAKE.JOB.PREFIX", "---");
21603955d011SMarcel Moolenaar 
21618c973ee2SSimon J. Gerraty 	targPrefix = Var_Subst("${.MAKE.JOB.PREFIX}",
21628d5c8e21SSimon J. Gerraty 	    SCOPE_GLOBAL, VARE_EVAL);
2163956e45f6SSimon J. Gerraty 	/* TODO: handle errors */
21643955d011SMarcel Moolenaar }
21653955d011SMarcel Moolenaar 
216606b9b3e0SSimon J. Gerraty static void
216706b9b3e0SSimon J. Gerraty AddSig(int sig, SignalProc handler)
216806b9b3e0SSimon J. Gerraty {
216906b9b3e0SSimon J. Gerraty 	if (bmake_signal(sig, SIG_IGN) != SIG_IGN) {
217006b9b3e0SSimon J. Gerraty 		sigaddset(&caught_signals, sig);
217106b9b3e0SSimon J. Gerraty 		(void)bmake_signal(sig, handler);
217206b9b3e0SSimon J. Gerraty 	}
217306b9b3e0SSimon J. Gerraty }
217406b9b3e0SSimon J. Gerraty 
21753955d011SMarcel Moolenaar void
21763955d011SMarcel Moolenaar Job_Init(void)
21773955d011SMarcel Moolenaar {
2178d191243dSSimon J. Gerraty 	Job_SetPrefix();
2179*d9a65c5dSSimon J. Gerraty 
2180956e45f6SSimon J. Gerraty 	job_table = bmake_malloc((size_t)opts.maxJobs * sizeof *job_table);
2181956e45f6SSimon J. Gerraty 	memset(job_table, 0, (size_t)opts.maxJobs * sizeof *job_table);
2182956e45f6SSimon J. Gerraty 	job_table_end = job_table + opts.maxJobs;
21833955d011SMarcel Moolenaar 	wantToken = 0;
2184dba7b0efSSimon J. Gerraty 	caught_sigchld = 0;
21853955d011SMarcel Moolenaar 
2186956e45f6SSimon J. Gerraty 	aborting = ABORT_NONE;
218706b9b3e0SSimon J. Gerraty 	job_errors = 0;
21883955d011SMarcel Moolenaar 
218968c4481aSSimon J. Gerraty 	Always_pass_job_queue = GetBooleanExpr(MAKE_ALWAYS_PASS_JOB_QUEUE,
21909a4bc556SSimon J. Gerraty 	    Always_pass_job_queue);
21919a4bc556SSimon J. Gerraty 
219268c4481aSSimon J. Gerraty 	Job_error_token = GetBooleanExpr(MAKE_JOB_ERROR_TOKEN, Job_error_token);
219368c4481aSSimon J. Gerraty 
21942d395cb5SSimon J. Gerraty 
21953955d011SMarcel Moolenaar 	/*
2196*d9a65c5dSSimon J. Gerraty 	 * There is a non-zero chance that we already have children,
2197*d9a65c5dSSimon J. Gerraty 	 * e.g. after 'make -f- <<EOF'.
219806b9b3e0SSimon J. Gerraty 	 * Since their termination causes a 'Child (pid) not in table'
219906b9b3e0SSimon J. Gerraty 	 * message, Collect the status of any that are already dead, and
220006b9b3e0SSimon J. Gerraty 	 * suppress the error message if there are any undead ones.
22013955d011SMarcel Moolenaar 	 */
22023955d011SMarcel Moolenaar 	for (;;) {
220306b9b3e0SSimon J. Gerraty 		int rval;
220406b9b3e0SSimon J. Gerraty 		WAIT_T status;
220506b9b3e0SSimon J. Gerraty 
22063955d011SMarcel Moolenaar 		rval = waitpid((pid_t)-1, &status, WNOHANG);
22073955d011SMarcel Moolenaar 		if (rval > 0)
22083955d011SMarcel Moolenaar 			continue;
22093955d011SMarcel Moolenaar 		if (rval == 0)
2210b0c40a00SSimon J. Gerraty 			lurking_children = true;
22113955d011SMarcel Moolenaar 		break;
22123955d011SMarcel Moolenaar 	}
22133955d011SMarcel Moolenaar 
22143955d011SMarcel Moolenaar 	Shell_Init();
22153955d011SMarcel Moolenaar 
22163955d011SMarcel Moolenaar 	JobCreatePipe(&childExitJob, 3);
22173955d011SMarcel Moolenaar 
2218dba7b0efSSimon J. Gerraty 	{
2219dba7b0efSSimon J. Gerraty 		size_t nfds = (npseudojobs + (size_t)opts.maxJobs) *
2220dba7b0efSSimon J. Gerraty 			      nfds_per_job();
2221dba7b0efSSimon J. Gerraty 		fds = bmake_malloc(sizeof *fds * nfds);
2222dba7b0efSSimon J. Gerraty 		jobByFdIndex = bmake_malloc(sizeof *jobByFdIndex * nfds);
2223dba7b0efSSimon J. Gerraty 	}
22243955d011SMarcel Moolenaar 
22253955d011SMarcel Moolenaar 	/* These are permanent entries and take slots 0 and 1 */
22263955d011SMarcel Moolenaar 	watchfd(&tokenWaitJob);
22273955d011SMarcel Moolenaar 	watchfd(&childExitJob);
22283955d011SMarcel Moolenaar 
22293955d011SMarcel Moolenaar 	sigemptyset(&caught_signals);
22303955d011SMarcel Moolenaar 	(void)bmake_signal(SIGCHLD, JobChildSig);
22313955d011SMarcel Moolenaar 	sigaddset(&caught_signals, SIGCHLD);
22323955d011SMarcel Moolenaar 
2233*d9a65c5dSSimon J. Gerraty 	/* Handle the signals specified by POSIX. */
223406b9b3e0SSimon J. Gerraty 	AddSig(SIGINT, JobPassSig_int);
223506b9b3e0SSimon J. Gerraty 	AddSig(SIGHUP, JobPassSig_term);
223606b9b3e0SSimon J. Gerraty 	AddSig(SIGTERM, JobPassSig_term);
223706b9b3e0SSimon J. Gerraty 	AddSig(SIGQUIT, JobPassSig_term);
22383955d011SMarcel Moolenaar 
22393955d011SMarcel Moolenaar 	/*
2240*d9a65c5dSSimon J. Gerraty 	 * These signals need to be passed to the jobs, as each job has its
2241*d9a65c5dSSimon J. Gerraty 	 * own process group and thus the terminal driver doesn't forward the
2242*d9a65c5dSSimon J. Gerraty 	 * signals itself.
22433955d011SMarcel Moolenaar 	 */
224406b9b3e0SSimon J. Gerraty 	AddSig(SIGTSTP, JobPassSig_suspend);
224506b9b3e0SSimon J. Gerraty 	AddSig(SIGTTOU, JobPassSig_suspend);
224606b9b3e0SSimon J. Gerraty 	AddSig(SIGTTIN, JobPassSig_suspend);
224706b9b3e0SSimon J. Gerraty 	AddSig(SIGWINCH, JobCondPassSig);
224806b9b3e0SSimon J. Gerraty 	AddSig(SIGCONT, JobContinueSig);
22493955d011SMarcel Moolenaar 
22501748de26SSimon J. Gerraty 	(void)Job_RunTarget(".BEGIN", NULL);
2251*d9a65c5dSSimon J. Gerraty 	/* Create the .END node, see Targ_GetEndNode in Compat_MakeAll. */
2252956e45f6SSimon J. Gerraty 	(void)Targ_GetEndNode();
22533955d011SMarcel Moolenaar }
22543955d011SMarcel Moolenaar 
225506b9b3e0SSimon J. Gerraty static void
225606b9b3e0SSimon J. Gerraty DelSig(int sig)
22573955d011SMarcel Moolenaar {
225806b9b3e0SSimon J. Gerraty 	if (sigismember(&caught_signals, sig) != 0)
225906b9b3e0SSimon J. Gerraty 		(void)bmake_signal(sig, SIG_DFL);
22603955d011SMarcel Moolenaar }
22613955d011SMarcel Moolenaar 
226206b9b3e0SSimon J. Gerraty static void
226306b9b3e0SSimon J. Gerraty JobSigReset(void)
226406b9b3e0SSimon J. Gerraty {
226506b9b3e0SSimon J. Gerraty 	DelSig(SIGINT);
226606b9b3e0SSimon J. Gerraty 	DelSig(SIGHUP);
226706b9b3e0SSimon J. Gerraty 	DelSig(SIGQUIT);
226806b9b3e0SSimon J. Gerraty 	DelSig(SIGTERM);
226906b9b3e0SSimon J. Gerraty 	DelSig(SIGTSTP);
227006b9b3e0SSimon J. Gerraty 	DelSig(SIGTTOU);
227106b9b3e0SSimon J. Gerraty 	DelSig(SIGTTIN);
227206b9b3e0SSimon J. Gerraty 	DelSig(SIGWINCH);
227306b9b3e0SSimon J. Gerraty 	DelSig(SIGCONT);
22743955d011SMarcel Moolenaar 	(void)bmake_signal(SIGCHLD, SIG_DFL);
22753955d011SMarcel Moolenaar }
22763955d011SMarcel Moolenaar 
22773955d011SMarcel Moolenaar static Shell *
2278956e45f6SSimon J. Gerraty FindShellByName(const char *name)
22793955d011SMarcel Moolenaar {
2280956e45f6SSimon J. Gerraty 	Shell *sh = shells;
2281956e45f6SSimon J. Gerraty 	const Shell *shellsEnd = sh + sizeof shells / sizeof shells[0];
22823955d011SMarcel Moolenaar 
2283956e45f6SSimon J. Gerraty 	for (sh = shells; sh < shellsEnd; sh++) {
22843955d011SMarcel Moolenaar 		if (strcmp(name, sh->name) == 0)
22853841c287SSimon J. Gerraty 			return sh;
22863955d011SMarcel Moolenaar 	}
22873955d011SMarcel Moolenaar 	return NULL;
22883955d011SMarcel Moolenaar }
22893955d011SMarcel Moolenaar 
229006b9b3e0SSimon J. Gerraty /*
229106b9b3e0SSimon J. Gerraty  * Parse a shell specification and set up 'shell', shellPath and
229206b9b3e0SSimon J. Gerraty  * shellName appropriately.
22933955d011SMarcel Moolenaar  *
22943955d011SMarcel Moolenaar  * Input:
22953955d011SMarcel Moolenaar  *	line		The shell spec
22963955d011SMarcel Moolenaar  *
22973955d011SMarcel Moolenaar  * Results:
2298*d9a65c5dSSimon J. Gerraty  *	Returns false if the specification was incorrect.
2299*d9a65c5dSSimon J. Gerraty  *	If successful, 'shell' is usable, shellPath is the full path of the
2300*d9a65c5dSSimon J. Gerraty  *	shell described by 'shell', and shellName is the final component of
2301*d9a65c5dSSimon J. Gerraty  *	shellPath.
23023955d011SMarcel Moolenaar  *
23033955d011SMarcel Moolenaar  * Notes:
2304*d9a65c5dSSimon J. Gerraty  *	A shell specification has the form ".SHELL: keyword=value...". Double
2305*d9a65c5dSSimon J. Gerraty  *	quotes can be used to enclose blanks in words. A backslash escapes
23063955d011SMarcel Moolenaar  *	anything (most notably a double-quote and a space) and
2307*d9a65c5dSSimon J. Gerraty  *	provides the usual escape sequences from C. There should be no
2308*d9a65c5dSSimon J. Gerraty  *	unnecessary spaces in the word. The keywords are:
23093955d011SMarcel Moolenaar  *	    name	Name of shell.
23103955d011SMarcel Moolenaar  *	    path	Location of shell.
23113955d011SMarcel Moolenaar  *	    quiet	Command to turn off echoing.
23123955d011SMarcel Moolenaar  *	    echo	Command to turn echoing on
2313*d9a65c5dSSimon J. Gerraty  *	    filter	The output from the shell command that turns off
2314*d9a65c5dSSimon J. Gerraty  *			echoing, to be filtered from the final output.
2315*d9a65c5dSSimon J. Gerraty  *	    echoFlag	Flag to turn echoing on at the start.
2316*d9a65c5dSSimon J. Gerraty  *	    errFlag	Flag to turn error checking on at the start.
2317*d9a65c5dSSimon J. Gerraty  *	    hasErrCtl	True if the shell has error checking control.
2318*d9a65c5dSSimon J. Gerraty  *	    newline	String literal to represent a newline character.
2319*d9a65c5dSSimon J. Gerraty  *	    check	If hasErrCtl is true: The command to turn on error
2320*d9a65c5dSSimon J. Gerraty  *			checking. If hasErrCtl is false: The template for a
2321*d9a65c5dSSimon J. Gerraty  *			shell command that echoes a command for which error
2322*d9a65c5dSSimon J. Gerraty  *			checking is off.
2323*d9a65c5dSSimon J. Gerraty  *	    ignore	If hasErrCtl is true: The command to turn off error
2324*d9a65c5dSSimon J. Gerraty  *			checking. If hasErrCtl is false: The template for a
2325*d9a65c5dSSimon J. Gerraty  *			shell command that executes a command so as to ignore
2326*d9a65c5dSSimon J. Gerraty  *			any errors it returns.
23273955d011SMarcel Moolenaar  */
2328b0c40a00SSimon J. Gerraty bool
23293955d011SMarcel Moolenaar Job_ParseShell(char *line)
23303955d011SMarcel Moolenaar {
23312c3632d1SSimon J. Gerraty 	Words wordsList;
23323955d011SMarcel Moolenaar 	char **words;
23333955d011SMarcel Moolenaar 	char **argv;
23342c3632d1SSimon J. Gerraty 	size_t argc;
23353955d011SMarcel Moolenaar 	char *path;
23363955d011SMarcel Moolenaar 	Shell newShell;
2337b0c40a00SSimon J. Gerraty 	bool fullSpec = false;
23383955d011SMarcel Moolenaar 	Shell *sh;
23393955d011SMarcel Moolenaar 
234006b9b3e0SSimon J. Gerraty 	/* XXX: don't use line as an iterator variable */
2341956e45f6SSimon J. Gerraty 	pp_skip_whitespace(&line);
23423955d011SMarcel Moolenaar 
2343dba7b0efSSimon J. Gerraty 	free(shell_freeIt);
23443955d011SMarcel Moolenaar 
2345e2eeea75SSimon J. Gerraty 	memset(&newShell, 0, sizeof newShell);
23463955d011SMarcel Moolenaar 
2347b0c40a00SSimon J. Gerraty 	wordsList = Str_Words(line, true);
23482c3632d1SSimon J. Gerraty 	words = wordsList.words;
23492c3632d1SSimon J. Gerraty 	argc = wordsList.len;
23502c3632d1SSimon J. Gerraty 	path = wordsList.freeIt;
23513955d011SMarcel Moolenaar 	if (words == NULL) {
23523955d011SMarcel Moolenaar 		Error("Unterminated quoted string [%s]", line);
2353b0c40a00SSimon J. Gerraty 		return false;
23543955d011SMarcel Moolenaar 	}
2355dba7b0efSSimon J. Gerraty 	shell_freeIt = path;
23563955d011SMarcel Moolenaar 
23573955d011SMarcel Moolenaar 	for (path = NULL, argv = words; argc != 0; argc--, argv++) {
2358956e45f6SSimon J. Gerraty 		char *arg = *argv;
2359956e45f6SSimon J. Gerraty 		if (strncmp(arg, "path=", 5) == 0) {
2360956e45f6SSimon J. Gerraty 			path = arg + 5;
2361956e45f6SSimon J. Gerraty 		} else if (strncmp(arg, "name=", 5) == 0) {
2362956e45f6SSimon J. Gerraty 			newShell.name = arg + 5;
23633955d011SMarcel Moolenaar 		} else {
2364956e45f6SSimon J. Gerraty 			if (strncmp(arg, "quiet=", 6) == 0) {
2365956e45f6SSimon J. Gerraty 				newShell.echoOff = arg + 6;
2366956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "echo=", 5) == 0) {
2367956e45f6SSimon J. Gerraty 				newShell.echoOn = arg + 5;
2368956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "filter=", 7) == 0) {
2369956e45f6SSimon J. Gerraty 				newShell.noPrint = arg + 7;
2370956e45f6SSimon J. Gerraty 				newShell.noPrintLen = strlen(newShell.noPrint);
2371956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "echoFlag=", 9) == 0) {
237206b9b3e0SSimon J. Gerraty 				newShell.echoFlag = arg + 9;
2373956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "errFlag=", 8) == 0) {
237406b9b3e0SSimon J. Gerraty 				newShell.errFlag = arg + 8;
2375956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "hasErrCtl=", 10) == 0) {
2376956e45f6SSimon J. Gerraty 				char c = arg[10];
2377956e45f6SSimon J. Gerraty 				newShell.hasErrCtl = c == 'Y' || c == 'y' ||
2378956e45f6SSimon J. Gerraty 						     c == 'T' || c == 't';
2379956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "newline=", 8) == 0) {
2380956e45f6SSimon J. Gerraty 				newShell.newline = arg + 8;
2381956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "check=", 6) == 0) {
23829f45a3c8SSimon J. Gerraty 				/*
23839f45a3c8SSimon J. Gerraty 				 * Before 2020-12-10, these two variables had
23849f45a3c8SSimon J. Gerraty 				 * been a single variable.
23859f45a3c8SSimon J. Gerraty 				 */
238606b9b3e0SSimon J. Gerraty 				newShell.errOn = arg + 6;
238706b9b3e0SSimon J. Gerraty 				newShell.echoTmpl = arg + 6;
2388956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "ignore=", 7) == 0) {
23899f45a3c8SSimon J. Gerraty 				/*
23909f45a3c8SSimon J. Gerraty 				 * Before 2020-12-10, these two variables had
23919f45a3c8SSimon J. Gerraty 				 * been a single variable.
23929f45a3c8SSimon J. Gerraty 				 */
239306b9b3e0SSimon J. Gerraty 				newShell.errOff = arg + 7;
239406b9b3e0SSimon J. Gerraty 				newShell.runIgnTmpl = arg + 7;
2395956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "errout=", 7) == 0) {
239606b9b3e0SSimon J. Gerraty 				newShell.runChkTmpl = arg + 7;
2397956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "comment=", 8) == 0) {
2398956e45f6SSimon J. Gerraty 				newShell.commentChar = arg[8];
23993955d011SMarcel Moolenaar 			} else {
240006b9b3e0SSimon J. Gerraty 				Parse_Error(PARSE_FATAL,
240106b9b3e0SSimon J. Gerraty 				    "Unknown keyword \"%s\"", arg);
24023955d011SMarcel Moolenaar 				free(words);
2403b0c40a00SSimon J. Gerraty 				return false;
24043955d011SMarcel Moolenaar 			}
2405b0c40a00SSimon J. Gerraty 			fullSpec = true;
24063955d011SMarcel Moolenaar 		}
24073955d011SMarcel Moolenaar 	}
24083955d011SMarcel Moolenaar 
24093955d011SMarcel Moolenaar 	if (path == NULL) {
24103955d011SMarcel Moolenaar 		if (newShell.name == NULL) {
241106b9b3e0SSimon J. Gerraty 			Parse_Error(PARSE_FATAL,
241206b9b3e0SSimon J. Gerraty 			    "Neither path nor name specified");
24133955d011SMarcel Moolenaar 			free(words);
2414b0c40a00SSimon J. Gerraty 			return false;
24153955d011SMarcel Moolenaar 		} else {
2416956e45f6SSimon J. Gerraty 			if ((sh = FindShellByName(newShell.name)) == NULL) {
241706b9b3e0SSimon J. Gerraty 				Parse_Error(PARSE_WARNING,
241806b9b3e0SSimon J. Gerraty 				    "%s: No matching shell", newShell.name);
24193955d011SMarcel Moolenaar 				free(words);
2420b0c40a00SSimon J. Gerraty 				return false;
24213955d011SMarcel Moolenaar 			}
242206b9b3e0SSimon J. Gerraty 			shell = sh;
24233955d011SMarcel Moolenaar 			shellName = newShell.name;
242406b9b3e0SSimon J. Gerraty 			if (shellPath != NULL) {
242522619282SSimon J. Gerraty 				free(shellPath);
24263955d011SMarcel Moolenaar 				shellPath = NULL;
24273955d011SMarcel Moolenaar 				Shell_Init();
24283955d011SMarcel Moolenaar 			}
24293955d011SMarcel Moolenaar 		}
24303955d011SMarcel Moolenaar 	} else {
243122619282SSimon J. Gerraty 		free(shellPath);
24328d5c8e21SSimon J. Gerraty 		shellPath = bmake_strdup(path);
2433d5e0a182SSimon J. Gerraty 		shellName = newShell.name != NULL ? newShell.name
2434d5e0a182SSimon J. Gerraty 		    : str_basename(path);
24353955d011SMarcel Moolenaar 		if (!fullSpec) {
2436956e45f6SSimon J. Gerraty 			if ((sh = FindShellByName(shellName)) == NULL) {
243706b9b3e0SSimon J. Gerraty 				Parse_Error(PARSE_WARNING,
243806b9b3e0SSimon J. Gerraty 				    "%s: No matching shell", shellName);
24393955d011SMarcel Moolenaar 				free(words);
2440b0c40a00SSimon J. Gerraty 				return false;
24413955d011SMarcel Moolenaar 			}
244206b9b3e0SSimon J. Gerraty 			shell = sh;
24433955d011SMarcel Moolenaar 		} else {
244406b9b3e0SSimon J. Gerraty 			shell = bmake_malloc(sizeof *shell);
244506b9b3e0SSimon J. Gerraty 			*shell = newShell;
24463955d011SMarcel Moolenaar 		}
2447*d9a65c5dSSimon J. Gerraty 		/* This will take care of shellErrFlag. */
244851ee2c1cSSimon J. Gerraty 		Shell_Init();
24493955d011SMarcel Moolenaar 	}
24503955d011SMarcel Moolenaar 
245106b9b3e0SSimon J. Gerraty 	if (shell->echoOn != NULL && shell->echoOff != NULL)
2452b0c40a00SSimon J. Gerraty 		shell->hasEchoCtl = true;
24533955d011SMarcel Moolenaar 
245406b9b3e0SSimon J. Gerraty 	if (!shell->hasErrCtl) {
245506b9b3e0SSimon J. Gerraty 		if (shell->echoTmpl == NULL)
245606b9b3e0SSimon J. Gerraty 			shell->echoTmpl = "";
245706b9b3e0SSimon J. Gerraty 		if (shell->runIgnTmpl == NULL)
245806b9b3e0SSimon J. Gerraty 			shell->runIgnTmpl = "%s\n";
24593955d011SMarcel Moolenaar 	}
24603955d011SMarcel Moolenaar 
24613955d011SMarcel Moolenaar 	/*
2462*d9a65c5dSSimon J. Gerraty 	 * Do not free up the words themselves, since they may be in use
246306b9b3e0SSimon J. Gerraty 	 * by the shell specification.
24643955d011SMarcel Moolenaar 	 */
24653955d011SMarcel Moolenaar 	free(words);
2466b0c40a00SSimon J. Gerraty 	return true;
24673955d011SMarcel Moolenaar }
24683955d011SMarcel Moolenaar 
246906b9b3e0SSimon J. Gerraty /*
2470*d9a65c5dSSimon J. Gerraty  * After receiving an interrupt signal, terminate all child processes and if
2471*d9a65c5dSSimon J. Gerraty  * necessary make the .INTERRUPT target.
24723955d011SMarcel Moolenaar  */
24733955d011SMarcel Moolenaar static void
2474b0c40a00SSimon J. Gerraty JobInterrupt(bool runINTERRUPT, int signo)
24753955d011SMarcel Moolenaar {
2476*d9a65c5dSSimon J. Gerraty 	Job *job;
2477*d9a65c5dSSimon J. Gerraty 	GNode *interrupt;
24783955d011SMarcel Moolenaar 	sigset_t mask;
24793955d011SMarcel Moolenaar 
24803955d011SMarcel Moolenaar 	aborting = ABORT_INTERRUPT;
24813955d011SMarcel Moolenaar 
24823955d011SMarcel Moolenaar 	JobSigLock(&mask);
24833955d011SMarcel Moolenaar 
24843955d011SMarcel Moolenaar 	for (job = job_table; job < job_table_end; job++) {
2485*d9a65c5dSSimon J. Gerraty 		if (job->status == JOB_ST_RUNNING && job->pid != 0) {
248606b9b3e0SSimon J. Gerraty 			DEBUG2(JOB,
248706b9b3e0SSimon J. Gerraty 			    "JobInterrupt passing signal %d to child %d.\n",
24883955d011SMarcel Moolenaar 			    signo, job->pid);
24893955d011SMarcel Moolenaar 			KILLPG(job->pid, signo);
24903955d011SMarcel Moolenaar 		}
24913955d011SMarcel Moolenaar 	}
24923955d011SMarcel Moolenaar 
2493*d9a65c5dSSimon J. Gerraty 	for (job = job_table; job < job_table_end; job++) {
2494*d9a65c5dSSimon J. Gerraty 		if (job->status == JOB_ST_RUNNING && job->pid != 0) {
2495*d9a65c5dSSimon J. Gerraty 			int status;
2496*d9a65c5dSSimon J. Gerraty 			(void)waitpid(job->pid, &status, 0);
2497*d9a65c5dSSimon J. Gerraty 			JobDeleteTarget(job->node);
2498*d9a65c5dSSimon J. Gerraty 		}
2499*d9a65c5dSSimon J. Gerraty 	}
2500*d9a65c5dSSimon J. Gerraty 
25013955d011SMarcel Moolenaar 	JobSigUnlock(&mask);
25023955d011SMarcel Moolenaar 
25039f45a3c8SSimon J. Gerraty 	if (runINTERRUPT && !opts.touch) {
2504956e45f6SSimon J. Gerraty 		interrupt = Targ_FindNode(".INTERRUPT");
25053955d011SMarcel Moolenaar 		if (interrupt != NULL) {
2506b0c40a00SSimon J. Gerraty 			opts.ignoreErrors = false;
25073955d011SMarcel Moolenaar 			JobRun(interrupt);
25083955d011SMarcel Moolenaar 		}
25093955d011SMarcel Moolenaar 	}
2510e2eeea75SSimon J. Gerraty 	Trace_Log(MAKEINTR, NULL);
251106b9b3e0SSimon J. Gerraty 	exit(signo);		/* XXX: why signo? */
25123955d011SMarcel Moolenaar }
25133955d011SMarcel Moolenaar 
2514*d9a65c5dSSimon J. Gerraty /* Make the .END target, returning the number of job-related errors. */
25153955d011SMarcel Moolenaar int
25163955d011SMarcel Moolenaar Job_Finish(void)
25173955d011SMarcel Moolenaar {
2518956e45f6SSimon J. Gerraty 	GNode *endNode = Targ_GetEndNode();
251906b9b3e0SSimon J. Gerraty 	if (!Lst_IsEmpty(&endNode->commands) ||
252006b9b3e0SSimon J. Gerraty 	    !Lst_IsEmpty(&endNode->children)) {
2521d5e0a182SSimon J. Gerraty 		if (job_errors != 0)
25223955d011SMarcel Moolenaar 			Error("Errors reported so .END ignored");
2523d5e0a182SSimon J. Gerraty 		else
2524956e45f6SSimon J. Gerraty 			JobRun(endNode);
25253955d011SMarcel Moolenaar 	}
252606b9b3e0SSimon J. Gerraty 	return job_errors;
25273955d011SMarcel Moolenaar }
25283955d011SMarcel Moolenaar 
252922619282SSimon J. Gerraty #ifdef CLEANUP
25303955d011SMarcel Moolenaar void
25313955d011SMarcel Moolenaar Job_End(void)
25323955d011SMarcel Moolenaar {
2533dba7b0efSSimon J. Gerraty 	free(shell_freeIt);
25343955d011SMarcel Moolenaar }
253522619282SSimon J. Gerraty #endif
25363955d011SMarcel Moolenaar 
2537*d9a65c5dSSimon J. Gerraty /* Waits for all running jobs to finish. */
25383955d011SMarcel Moolenaar void
25393955d011SMarcel Moolenaar Job_Wait(void)
25403955d011SMarcel Moolenaar {
2541*d9a65c5dSSimon J. Gerraty 	aborting = ABORT_WAIT;	/* Prevent other jobs from starting. */
25423955d011SMarcel Moolenaar 	while (jobTokensRunning != 0) {
25433955d011SMarcel Moolenaar 		Job_CatchOutput();
25443955d011SMarcel Moolenaar 	}
2545956e45f6SSimon J. Gerraty 	aborting = ABORT_NONE;
25463955d011SMarcel Moolenaar }
25473955d011SMarcel Moolenaar 
254806b9b3e0SSimon J. Gerraty /*
254906b9b3e0SSimon J. Gerraty  * Abort all currently running jobs without handling output or anything.
2550956e45f6SSimon J. Gerraty  * This function is to be called only in the event of a major error.
2551956e45f6SSimon J. Gerraty  * Most definitely NOT to be called from JobInterrupt.
255206b9b3e0SSimon J. Gerraty  */
25533955d011SMarcel Moolenaar void
25543955d011SMarcel Moolenaar Job_AbortAll(void)
25553955d011SMarcel Moolenaar {
2556*d9a65c5dSSimon J. Gerraty 	Job *job;
2557*d9a65c5dSSimon J. Gerraty 	WAIT_T status;
25583955d011SMarcel Moolenaar 
25593955d011SMarcel Moolenaar 	aborting = ABORT_ERROR;
25603955d011SMarcel Moolenaar 
256106b9b3e0SSimon J. Gerraty 	if (jobTokensRunning != 0) {
25623955d011SMarcel Moolenaar 		for (job = job_table; job < job_table_end; job++) {
2563e2eeea75SSimon J. Gerraty 			if (job->status != JOB_ST_RUNNING)
25643955d011SMarcel Moolenaar 				continue;
25653955d011SMarcel Moolenaar 			KILLPG(job->pid, SIGINT);
25663955d011SMarcel Moolenaar 			KILLPG(job->pid, SIGKILL);
25673955d011SMarcel Moolenaar 		}
25683955d011SMarcel Moolenaar 	}
25693955d011SMarcel Moolenaar 
2570*d9a65c5dSSimon J. Gerraty 	while (waitpid((pid_t)-1, &status, WNOHANG) > 0)
25713955d011SMarcel Moolenaar 		continue;
25723955d011SMarcel Moolenaar }
25733955d011SMarcel Moolenaar 
257406b9b3e0SSimon J. Gerraty /*
257506b9b3e0SSimon J. Gerraty  * Tries to restart stopped jobs if there are slots available.
2576*d9a65c5dSSimon J. Gerraty  * Called in response to a SIGCONT.
257706b9b3e0SSimon J. Gerraty  */
25783955d011SMarcel Moolenaar static void
25793955d011SMarcel Moolenaar JobRestartJobs(void)
25803955d011SMarcel Moolenaar {
25813955d011SMarcel Moolenaar 	Job *job;
25823955d011SMarcel Moolenaar 
25833955d011SMarcel Moolenaar 	for (job = job_table; job < job_table_end; job++) {
2584e2eeea75SSimon J. Gerraty 		if (job->status == JOB_ST_RUNNING &&
2585e2eeea75SSimon J. Gerraty 		    (make_suspended || job->suspended)) {
258606b9b3e0SSimon J. Gerraty 			DEBUG1(JOB, "Restarting stopped job pid %d.\n",
258706b9b3e0SSimon J. Gerraty 			    job->pid);
2588e2eeea75SSimon J. Gerraty 			if (job->suspended) {
258906b9b3e0SSimon J. Gerraty 				(void)printf("*** [%s] Continued\n",
259006b9b3e0SSimon J. Gerraty 				    job->node->name);
25913955d011SMarcel Moolenaar 				(void)fflush(stdout);
25923955d011SMarcel Moolenaar 			}
2593b0c40a00SSimon J. Gerraty 			job->suspended = false;
25943955d011SMarcel Moolenaar 			if (KILLPG(job->pid, SIGCONT) != 0 && DEBUG(JOB)) {
259506b9b3e0SSimon J. Gerraty 				debug_printf("Failed to send SIGCONT to %d\n",
259606b9b3e0SSimon J. Gerraty 				    job->pid);
25973955d011SMarcel Moolenaar 			}
25983955d011SMarcel Moolenaar 		}
259906b9b3e0SSimon J. Gerraty 		if (job->status == JOB_ST_FINISHED) {
260006b9b3e0SSimon J. Gerraty 			/*
260106b9b3e0SSimon J. Gerraty 			 * Job exit deferred after calling waitpid() in a
260206b9b3e0SSimon J. Gerraty 			 * signal handler
260306b9b3e0SSimon J. Gerraty 			 */
26043955d011SMarcel Moolenaar 			JobFinish(job, job->exit_status);
26053955d011SMarcel Moolenaar 		}
260606b9b3e0SSimon J. Gerraty 	}
2607b0c40a00SSimon J. Gerraty 	make_suspended = false;
26083955d011SMarcel Moolenaar }
26093955d011SMarcel Moolenaar 
26103955d011SMarcel Moolenaar static void
26113955d011SMarcel Moolenaar watchfd(Job *job)
26123955d011SMarcel Moolenaar {
26133955d011SMarcel Moolenaar 	if (job->inPollfd != NULL)
26143955d011SMarcel Moolenaar 		Punt("Watching watched job");
26153955d011SMarcel Moolenaar 
2616dba7b0efSSimon J. Gerraty 	fds[fdsLen].fd = job->inPipe;
2617dba7b0efSSimon J. Gerraty 	fds[fdsLen].events = POLLIN;
2618dba7b0efSSimon J. Gerraty 	jobByFdIndex[fdsLen] = job;
2619dba7b0efSSimon J. Gerraty 	job->inPollfd = &fds[fdsLen];
2620dba7b0efSSimon J. Gerraty 	fdsLen++;
262149caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
262249caa483SSimon J. Gerraty 	if (useMeta) {
2623dba7b0efSSimon J. Gerraty 		fds[fdsLen].fd = meta_job_fd(job);
2624dba7b0efSSimon J. Gerraty 		fds[fdsLen].events = fds[fdsLen].fd == -1 ? 0 : POLLIN;
2625dba7b0efSSimon J. Gerraty 		jobByFdIndex[fdsLen] = job;
2626dba7b0efSSimon J. Gerraty 		fdsLen++;
262749caa483SSimon J. Gerraty 	}
262849caa483SSimon J. Gerraty #endif
26293955d011SMarcel Moolenaar }
26303955d011SMarcel Moolenaar 
26313955d011SMarcel Moolenaar static void
26323955d011SMarcel Moolenaar clearfd(Job *job)
26333955d011SMarcel Moolenaar {
2634956e45f6SSimon J. Gerraty 	size_t i;
26353955d011SMarcel Moolenaar 	if (job->inPollfd == NULL)
26363955d011SMarcel Moolenaar 		Punt("Unwatching unwatched job");
2637956e45f6SSimon J. Gerraty 	i = (size_t)(job->inPollfd - fds);
2638dba7b0efSSimon J. Gerraty 	fdsLen--;
263949caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
264049caa483SSimon J. Gerraty 	if (useMeta) {
264149caa483SSimon J. Gerraty 		assert(nfds_per_job() == 2);
264206b9b3e0SSimon J. Gerraty 		if (i % 2 != 0)
264349caa483SSimon J. Gerraty 			Punt("odd-numbered fd with meta");
2644dba7b0efSSimon J. Gerraty 		fdsLen--;
264549caa483SSimon J. Gerraty 	}
264649caa483SSimon J. Gerraty #endif
2647d5e0a182SSimon J. Gerraty 	/* Move last job in table into hole made by dead job. */
2648dba7b0efSSimon J. Gerraty 	if (fdsLen != i) {
2649dba7b0efSSimon J. Gerraty 		fds[i] = fds[fdsLen];
2650dba7b0efSSimon J. Gerraty 		jobByFdIndex[i] = jobByFdIndex[fdsLen];
2651dba7b0efSSimon J. Gerraty 		jobByFdIndex[i]->inPollfd = &fds[i];
265249caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
265349caa483SSimon J. Gerraty 		if (useMeta) {
2654dba7b0efSSimon J. Gerraty 			fds[i + 1] = fds[fdsLen + 1];
2655dba7b0efSSimon J. Gerraty 			jobByFdIndex[i + 1] = jobByFdIndex[fdsLen + 1];
265649caa483SSimon J. Gerraty 		}
265749caa483SSimon J. Gerraty #endif
26583955d011SMarcel Moolenaar 	}
26593955d011SMarcel Moolenaar 	job->inPollfd = NULL;
26603955d011SMarcel Moolenaar }
26613955d011SMarcel Moolenaar 
2662b0c40a00SSimon J. Gerraty static bool
26633955d011SMarcel Moolenaar readyfd(Job *job)
26643955d011SMarcel Moolenaar {
26653955d011SMarcel Moolenaar 	if (job->inPollfd == NULL)
26663955d011SMarcel Moolenaar 		Punt("Polling unwatched job");
26673955d011SMarcel Moolenaar 	return (job->inPollfd->revents & POLLIN) != 0;
26683955d011SMarcel Moolenaar }
26693955d011SMarcel Moolenaar 
267006b9b3e0SSimon J. Gerraty /*
267106b9b3e0SSimon J. Gerraty  * Put a token (back) into the job pipe.
267206b9b3e0SSimon J. Gerraty  * This allows a make process to start a build job.
267306b9b3e0SSimon J. Gerraty  */
26743955d011SMarcel Moolenaar static void
26753955d011SMarcel Moolenaar JobTokenAdd(void)
26763955d011SMarcel Moolenaar {
26773955d011SMarcel Moolenaar 	char tok = JOB_TOKENS[aborting], tok1;
26783955d011SMarcel Moolenaar 
26792d395cb5SSimon J. Gerraty 	if (!Job_error_token && aborting == ABORT_ERROR) {
26802d395cb5SSimon J. Gerraty 		if (jobTokensRunning == 0)
26812d395cb5SSimon J. Gerraty 			return;
26822d395cb5SSimon J. Gerraty 		tok = '+';		/* no error token */
26832d395cb5SSimon J. Gerraty 	}
26842d395cb5SSimon J. Gerraty 
2685*d9a65c5dSSimon J. Gerraty 	/* If we are depositing an error token, flush everything else. */
26863955d011SMarcel Moolenaar 	while (tok != '+' && read(tokenWaitJob.inPipe, &tok1, 1) == 1)
26873955d011SMarcel Moolenaar 		continue;
26883955d011SMarcel Moolenaar 
2689956e45f6SSimon J. Gerraty 	DEBUG3(JOB, "(%d) aborting %d, deposit token %c\n",
26902d395cb5SSimon J. Gerraty 	    getpid(), aborting, tok);
26913cbdda60SSimon J. Gerraty 	while (write(tokenWaitJob.outPipe, &tok, 1) == -1 && errno == EAGAIN)
26923cbdda60SSimon J. Gerraty 		continue;
26933955d011SMarcel Moolenaar }
26943955d011SMarcel Moolenaar 
2695dba7b0efSSimon J. Gerraty int
2696dba7b0efSSimon J. Gerraty Job_TempFile(const char *pattern, char *tfile, size_t tfile_sz)
2697dba7b0efSSimon J. Gerraty {
2698dba7b0efSSimon J. Gerraty 	int fd;
2699dba7b0efSSimon J. Gerraty 	sigset_t mask;
2700dba7b0efSSimon J. Gerraty 
2701dba7b0efSSimon J. Gerraty 	JobSigLock(&mask);
2702dba7b0efSSimon J. Gerraty 	fd = mkTempFile(pattern, tfile, tfile_sz);
2703dba7b0efSSimon J. Gerraty 	if (tfile != NULL && !DEBUG(SCRIPT))
2704dba7b0efSSimon J. Gerraty 		unlink(tfile);
2705dba7b0efSSimon J. Gerraty 	JobSigUnlock(&mask);
2706dba7b0efSSimon J. Gerraty 
2707dba7b0efSSimon J. Gerraty 	return fd;
2708dba7b0efSSimon J. Gerraty }
2709dba7b0efSSimon J. Gerraty 
2710*d9a65c5dSSimon J. Gerraty /* Prepare the job token pipe in the root make process. */
27113955d011SMarcel Moolenaar void
27123955d011SMarcel Moolenaar Job_ServerStart(int max_tokens, int jp_0, int jp_1)
27133955d011SMarcel Moolenaar {
27143955d011SMarcel Moolenaar 	int i;
27153955d011SMarcel Moolenaar 	char jobarg[64];
27163955d011SMarcel Moolenaar 
27173955d011SMarcel Moolenaar 	if (jp_0 >= 0 && jp_1 >= 0) {
27183955d011SMarcel Moolenaar 		tokenWaitJob.inPipe = jp_0;
27193955d011SMarcel Moolenaar 		tokenWaitJob.outPipe = jp_1;
2720be19d90bSSimon J. Gerraty 		(void)fcntl(jp_0, F_SETFD, FD_CLOEXEC);
2721be19d90bSSimon J. Gerraty 		(void)fcntl(jp_1, F_SETFD, FD_CLOEXEC);
27223955d011SMarcel Moolenaar 		return;
27233955d011SMarcel Moolenaar 	}
27243955d011SMarcel Moolenaar 
27253955d011SMarcel Moolenaar 	JobCreatePipe(&tokenWaitJob, 15);
27263955d011SMarcel Moolenaar 
2727e2eeea75SSimon J. Gerraty 	snprintf(jobarg, sizeof jobarg, "%d,%d",
27283955d011SMarcel Moolenaar 	    tokenWaitJob.inPipe, tokenWaitJob.outPipe);
27293955d011SMarcel Moolenaar 
2730dba7b0efSSimon J. Gerraty 	Global_Append(MAKEFLAGS, "-J");
2731dba7b0efSSimon J. Gerraty 	Global_Append(MAKEFLAGS, jobarg);
27323955d011SMarcel Moolenaar 
27333955d011SMarcel Moolenaar 	/*
27343955d011SMarcel Moolenaar 	 * Preload the job pipe with one token per job, save the one
27353955d011SMarcel Moolenaar 	 * "extra" token for the primary job.
27363955d011SMarcel Moolenaar 	 *
27373955d011SMarcel Moolenaar 	 * XXX should clip maxJobs against PIPE_BUF -- if max_tokens is
27383955d011SMarcel Moolenaar 	 * larger than the write buffer size of the pipe, we will
27393955d011SMarcel Moolenaar 	 * deadlock here.
27403955d011SMarcel Moolenaar 	 */
27413955d011SMarcel Moolenaar 	for (i = 1; i < max_tokens; i++)
27423955d011SMarcel Moolenaar 		JobTokenAdd();
27433955d011SMarcel Moolenaar }
27443955d011SMarcel Moolenaar 
2745956e45f6SSimon J. Gerraty /* Return a withdrawn token to the pool. */
27463955d011SMarcel Moolenaar void
27473955d011SMarcel Moolenaar Job_TokenReturn(void)
27483955d011SMarcel Moolenaar {
27493955d011SMarcel Moolenaar 	jobTokensRunning--;
27503955d011SMarcel Moolenaar 	if (jobTokensRunning < 0)
27513955d011SMarcel Moolenaar 		Punt("token botch");
275206b9b3e0SSimon J. Gerraty 	if (jobTokensRunning != 0 || JOB_TOKENS[aborting] != '+')
27533955d011SMarcel Moolenaar 		JobTokenAdd();
27543955d011SMarcel Moolenaar }
27553955d011SMarcel Moolenaar 
275606b9b3e0SSimon J. Gerraty /*
275706b9b3e0SSimon J. Gerraty  * Attempt to withdraw a token from the pool.
27583955d011SMarcel Moolenaar  *
2759*d9a65c5dSSimon J. Gerraty  * If the pool is empty, set wantToken so that we wake up when a token is
2760956e45f6SSimon J. Gerraty  * released.
27613955d011SMarcel Moolenaar  *
2762b0c40a00SSimon J. Gerraty  * Returns true if a token was withdrawn, and false if the pool is currently
276306b9b3e0SSimon J. Gerraty  * empty.
276406b9b3e0SSimon J. Gerraty  */
2765b0c40a00SSimon J. Gerraty bool
27663955d011SMarcel Moolenaar Job_TokenWithdraw(void)
27673955d011SMarcel Moolenaar {
27683955d011SMarcel Moolenaar 	char tok, tok1;
2769956e45f6SSimon J. Gerraty 	ssize_t count;
27703955d011SMarcel Moolenaar 
27713955d011SMarcel Moolenaar 	wantToken = 0;
2772956e45f6SSimon J. Gerraty 	DEBUG3(JOB, "Job_TokenWithdraw(%d): aborting %d, running %d\n",
27733955d011SMarcel Moolenaar 	    getpid(), aborting, jobTokensRunning);
27743955d011SMarcel Moolenaar 
2775956e45f6SSimon J. Gerraty 	if (aborting != ABORT_NONE || (jobTokensRunning >= opts.maxJobs))
2776b0c40a00SSimon J. Gerraty 		return false;
27773955d011SMarcel Moolenaar 
27783955d011SMarcel Moolenaar 	count = read(tokenWaitJob.inPipe, &tok, 1);
27793955d011SMarcel Moolenaar 	if (count == 0)
27803955d011SMarcel Moolenaar 		Fatal("eof on job pipe!");
27813955d011SMarcel Moolenaar 	if (count < 0 && jobTokensRunning != 0) {
2782d5e0a182SSimon J. Gerraty 		if (errno != EAGAIN)
27833955d011SMarcel Moolenaar 			Fatal("job pipe read: %s", strerror(errno));
2784956e45f6SSimon J. Gerraty 		DEBUG1(JOB, "(%d) blocked for token\n", getpid());
278506b9b3e0SSimon J. Gerraty 		wantToken = 1;
2786b0c40a00SSimon J. Gerraty 		return false;
27873955d011SMarcel Moolenaar 	}
27883955d011SMarcel Moolenaar 
27893955d011SMarcel Moolenaar 	if (count == 1 && tok != '+') {
279006b9b3e0SSimon J. Gerraty 		/* make being aborted - remove any other job tokens */
2791956e45f6SSimon J. Gerraty 		DEBUG2(JOB, "(%d) aborted by token %c\n", getpid(), tok);
27923955d011SMarcel Moolenaar 		while (read(tokenWaitJob.inPipe, &tok1, 1) == 1)
27933955d011SMarcel Moolenaar 			continue;
27943955d011SMarcel Moolenaar 		/* And put the stopper back */
279506b9b3e0SSimon J. Gerraty 		while (write(tokenWaitJob.outPipe, &tok, 1) == -1 &&
279606b9b3e0SSimon J. Gerraty 		       errno == EAGAIN)
27973cbdda60SSimon J. Gerraty 			continue;
2798*d9a65c5dSSimon J. Gerraty 		if (shouldDieQuietly(NULL, 1)) {
2799*d9a65c5dSSimon J. Gerraty 			Job_Wait();
2800*d9a65c5dSSimon J. Gerraty 			exit(6);
2801*d9a65c5dSSimon J. Gerraty 		}
280206b9b3e0SSimon J. Gerraty 		Fatal("A failure has been detected "
280306b9b3e0SSimon J. Gerraty 		      "in another branch of the parallel make");
28043955d011SMarcel Moolenaar 	}
28053955d011SMarcel Moolenaar 
28063955d011SMarcel Moolenaar 	if (count == 1 && jobTokensRunning == 0)
28073955d011SMarcel Moolenaar 		/* We didn't want the token really */
280806b9b3e0SSimon J. Gerraty 		while (write(tokenWaitJob.outPipe, &tok, 1) == -1 &&
280906b9b3e0SSimon J. Gerraty 		       errno == EAGAIN)
28103cbdda60SSimon J. Gerraty 			continue;
28113955d011SMarcel Moolenaar 
28123955d011SMarcel Moolenaar 	jobTokensRunning++;
2813956e45f6SSimon J. Gerraty 	DEBUG1(JOB, "(%d) withdrew token\n", getpid());
2814b0c40a00SSimon J. Gerraty 	return true;
28153955d011SMarcel Moolenaar }
28163955d011SMarcel Moolenaar 
2817*d9a65c5dSSimon J. Gerraty /* Make the named target if found, exit if the target fails. */
2818b0c40a00SSimon J. Gerraty bool
281906b9b3e0SSimon J. Gerraty Job_RunTarget(const char *target, const char *fname)
282006b9b3e0SSimon J. Gerraty {
2821956e45f6SSimon J. Gerraty 	GNode *gn = Targ_FindNode(target);
28221748de26SSimon J. Gerraty 	if (gn == NULL)
2823b0c40a00SSimon J. Gerraty 		return false;
28241748de26SSimon J. Gerraty 
282506b9b3e0SSimon J. Gerraty 	if (fname != NULL)
2826dba7b0efSSimon J. Gerraty 		Var_Set(gn, ALLSRC, fname);
28271748de26SSimon J. Gerraty 
28281748de26SSimon J. Gerraty 	JobRun(gn);
2829b0c40a00SSimon J. Gerraty 	return true;
28301748de26SSimon J. Gerraty }
28311748de26SSimon J. Gerraty 
28323955d011SMarcel Moolenaar #ifdef USE_SELECT
28333955d011SMarcel Moolenaar int
28343955d011SMarcel Moolenaar emul_poll(struct pollfd *fd, int nfd, int timeout)
28353955d011SMarcel Moolenaar {
28363955d011SMarcel Moolenaar 	fd_set rfds, wfds;
28373955d011SMarcel Moolenaar 	int i, maxfd, nselect, npoll;
28383955d011SMarcel Moolenaar 	struct timeval tv, *tvp;
28393955d011SMarcel Moolenaar 	long usecs;
28403955d011SMarcel Moolenaar 
28413955d011SMarcel Moolenaar 	FD_ZERO(&rfds);
28423955d011SMarcel Moolenaar 	FD_ZERO(&wfds);
28433955d011SMarcel Moolenaar 
28443955d011SMarcel Moolenaar 	maxfd = -1;
28453955d011SMarcel Moolenaar 	for (i = 0; i < nfd; i++) {
28463955d011SMarcel Moolenaar 		fd[i].revents = 0;
28473955d011SMarcel Moolenaar 
28483955d011SMarcel Moolenaar 		if (fd[i].events & POLLIN)
28493955d011SMarcel Moolenaar 			FD_SET(fd[i].fd, &rfds);
28503955d011SMarcel Moolenaar 
28513955d011SMarcel Moolenaar 		if (fd[i].events & POLLOUT)
28523955d011SMarcel Moolenaar 			FD_SET(fd[i].fd, &wfds);
28533955d011SMarcel Moolenaar 
28543955d011SMarcel Moolenaar 		if (fd[i].fd > maxfd)
28553955d011SMarcel Moolenaar 			maxfd = fd[i].fd;
28563955d011SMarcel Moolenaar 	}
28573955d011SMarcel Moolenaar 
28583955d011SMarcel Moolenaar 	if (maxfd >= FD_SETSIZE) {
28593955d011SMarcel Moolenaar 		Punt("Ran out of fd_set slots; "
28603955d011SMarcel Moolenaar 		     "recompile with a larger FD_SETSIZE.");
28613955d011SMarcel Moolenaar 	}
28623955d011SMarcel Moolenaar 
28633955d011SMarcel Moolenaar 	if (timeout < 0) {
28643955d011SMarcel Moolenaar 		tvp = NULL;
28653955d011SMarcel Moolenaar 	} else {
28663955d011SMarcel Moolenaar 		usecs = timeout * 1000;
28673955d011SMarcel Moolenaar 		tv.tv_sec = usecs / 1000000;
28683955d011SMarcel Moolenaar 		tv.tv_usec = usecs % 1000000;
28693955d011SMarcel Moolenaar 		tvp = &tv;
28703955d011SMarcel Moolenaar 	}
28713955d011SMarcel Moolenaar 
2872e2eeea75SSimon J. Gerraty 	nselect = select(maxfd + 1, &rfds, &wfds, NULL, tvp);
28733955d011SMarcel Moolenaar 
28743955d011SMarcel Moolenaar 	if (nselect <= 0)
28753955d011SMarcel Moolenaar 		return nselect;
28763955d011SMarcel Moolenaar 
28773955d011SMarcel Moolenaar 	npoll = 0;
28783955d011SMarcel Moolenaar 	for (i = 0; i < nfd; i++) {
28793955d011SMarcel Moolenaar 		if (FD_ISSET(fd[i].fd, &rfds))
28803955d011SMarcel Moolenaar 			fd[i].revents |= POLLIN;
28813955d011SMarcel Moolenaar 
28823955d011SMarcel Moolenaar 		if (FD_ISSET(fd[i].fd, &wfds))
28833955d011SMarcel Moolenaar 			fd[i].revents |= POLLOUT;
28843955d011SMarcel Moolenaar 
28853955d011SMarcel Moolenaar 		if (fd[i].revents)
28863955d011SMarcel Moolenaar 			npoll++;
28873955d011SMarcel Moolenaar 	}
28883955d011SMarcel Moolenaar 
28893955d011SMarcel Moolenaar 	return npoll;
28903955d011SMarcel Moolenaar }
28913955d011SMarcel Moolenaar #endif				/* USE_SELECT */
2892