xref: /freebsd/contrib/bmake/job.c (revision 6a7405f5a6b639682cacf01e35d561411ff556aa)
1*6a7405f5SSimon J. Gerraty /*	$NetBSD: job.c,v 1.485 2025/01/19 10:57:10 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  *
1053955d011SMarcel Moolenaar  *	Job_Finish	Perform any final processing which needs doing.
1063955d011SMarcel Moolenaar  *			This includes the execution of any commands
1073955d011SMarcel Moolenaar  *			which have been/were attached to the .END
1083955d011SMarcel Moolenaar  *			target. It should only be called when the
1093955d011SMarcel Moolenaar  *			job table is empty.
1103955d011SMarcel Moolenaar  *
111dba7b0efSSimon J. Gerraty  *	Job_AbortAll	Abort all currently running jobs. Do not handle
112dba7b0efSSimon J. Gerraty  *			output or do anything for the jobs, just kill them.
113dba7b0efSSimon J. Gerraty  *			Should only be called in an emergency.
1143955d011SMarcel Moolenaar  *
115956e45f6SSimon J. Gerraty  *	Job_CheckCommands
116956e45f6SSimon J. Gerraty  *			Verify that the commands for a target are
1173955d011SMarcel Moolenaar  *			ok. Provide them if necessary and possible.
1183955d011SMarcel Moolenaar  *
1193955d011SMarcel Moolenaar  *	Job_Touch	Update a target without really updating it.
1203955d011SMarcel Moolenaar  *
1213955d011SMarcel Moolenaar  *	Job_Wait	Wait for all currently-running jobs to finish.
1223955d011SMarcel Moolenaar  */
1233955d011SMarcel Moolenaar 
1243955d011SMarcel Moolenaar #ifdef HAVE_CONFIG_H
1253955d011SMarcel Moolenaar # include "config.h"
1263955d011SMarcel Moolenaar #endif
1273955d011SMarcel Moolenaar #include <sys/types.h>
1283955d011SMarcel Moolenaar #include <sys/stat.h>
1293955d011SMarcel Moolenaar #include <sys/file.h>
1303955d011SMarcel Moolenaar #include <sys/time.h>
1313955d011SMarcel Moolenaar #include "wait.h"
1323955d011SMarcel Moolenaar 
1333955d011SMarcel Moolenaar #include <errno.h>
1343955d011SMarcel Moolenaar #if !defined(USE_SELECT) && defined(HAVE_POLL_H)
1353955d011SMarcel Moolenaar #include <poll.h>
1363955d011SMarcel Moolenaar #else
1373955d011SMarcel Moolenaar #ifndef USE_SELECT			/* no poll.h */
1383955d011SMarcel Moolenaar # define USE_SELECT
1393955d011SMarcel Moolenaar #endif
1403955d011SMarcel Moolenaar #if defined(HAVE_SYS_SELECT_H)
1413955d011SMarcel Moolenaar # include <sys/select.h>
1423955d011SMarcel Moolenaar #endif
1433955d011SMarcel Moolenaar #endif
1443955d011SMarcel Moolenaar #include <signal.h>
1453955d011SMarcel Moolenaar #include <utime.h>
1463955d011SMarcel Moolenaar #if defined(HAVE_SYS_SOCKET_H)
1473955d011SMarcel Moolenaar # include <sys/socket.h>
1483955d011SMarcel Moolenaar #endif
1493955d011SMarcel Moolenaar 
1503955d011SMarcel Moolenaar #include "make.h"
1513955d011SMarcel Moolenaar #include "dir.h"
1523955d011SMarcel Moolenaar #include "job.h"
1533955d011SMarcel Moolenaar #include "pathnames.h"
1543955d011SMarcel Moolenaar #include "trace.h"
155956e45f6SSimon J. Gerraty 
156956e45f6SSimon J. Gerraty /*	"@(#)job.c	8.2 (Berkeley) 3/19/94"	*/
157*6a7405f5SSimon J. Gerraty MAKE_RCSID("$NetBSD: job.c,v 1.485 2025/01/19 10:57:10 rillig Exp $");
158956e45f6SSimon J. Gerraty 
15906b9b3e0SSimon J. Gerraty /*
16006b9b3e0SSimon J. Gerraty  * A shell defines how the commands are run.  All commands for a target are
161956e45f6SSimon J. Gerraty  * written into a single file, which is then given to the shell to execute
162956e45f6SSimon J. Gerraty  * the commands from it.  The commands are written to the file using a few
163956e45f6SSimon J. Gerraty  * templates for echo control and error control.
164956e45f6SSimon J. Gerraty  *
165956e45f6SSimon J. Gerraty  * The name of the shell is the basename for the predefined shells, such as
166956e45f6SSimon J. Gerraty  * "sh", "csh", "bash".  For custom shells, it is the full pathname, and its
167956e45f6SSimon J. Gerraty  * basename is used to select the type of shell; the longest match wins.
168956e45f6SSimon J. Gerraty  * So /usr/pkg/bin/bash has type sh, /usr/local/bin/tcsh has type csh.
169956e45f6SSimon J. Gerraty  *
170956e45f6SSimon J. Gerraty  * The echoing of command lines is controlled using hasEchoCtl, echoOff,
171956e45f6SSimon J. Gerraty  * echoOn, noPrint and noPrintLen.  When echoOff is executed by the shell, it
172956e45f6SSimon J. Gerraty  * still outputs something, but this something is not interesting, therefore
173956e45f6SSimon J. Gerraty  * it is filtered out using noPrint and noPrintLen.
174956e45f6SSimon J. Gerraty  *
175956e45f6SSimon J. Gerraty  * The error checking for individual commands is controlled using hasErrCtl,
17606b9b3e0SSimon J. Gerraty  * errOn, errOff and runChkTmpl.
177956e45f6SSimon J. Gerraty  *
178dba7b0efSSimon J. Gerraty  * In case a shell doesn't have error control, echoTmpl is a printf template
179dba7b0efSSimon J. Gerraty  * for echoing the command, should echoing be on; runIgnTmpl is another
180dba7b0efSSimon J. Gerraty  * printf template for executing the command while ignoring the return
18106b9b3e0SSimon J. Gerraty  * status. Finally runChkTmpl is a printf template for running the command and
182956e45f6SSimon J. Gerraty  * causing the shell to exit on error. If any of these strings are empty when
183b0c40a00SSimon J. Gerraty  * hasErrCtl is false, the command will be executed anyway as is, and if it
184956e45f6SSimon J. Gerraty  * causes an error, so be it. Any templates set up to echo the command will
185dba7b0efSSimon J. Gerraty  * escape any '$ ` \ "' characters in the command string to avoid unwanted
186dba7b0efSSimon J. Gerraty  * shell code injection, the escaped command is safe to use in double quotes.
187956e45f6SSimon J. Gerraty  *
188956e45f6SSimon J. Gerraty  * The command-line flags "echo" and "exit" also control the behavior.  The
189956e45f6SSimon J. Gerraty  * "echo" flag causes the shell to start echoing commands right away.  The
190956e45f6SSimon J. Gerraty  * "exit" flag causes the shell to exit when an error is detected in one of
191956e45f6SSimon J. Gerraty  * the commands.
192956e45f6SSimon J. Gerraty  */
193956e45f6SSimon J. Gerraty typedef struct Shell {
194956e45f6SSimon J. Gerraty 
19506b9b3e0SSimon J. Gerraty 	/*
19606b9b3e0SSimon J. Gerraty 	 * The name of the shell. For Bourne and C shells, this is used only
19706b9b3e0SSimon J. Gerraty 	 * to find the shell description when used as the single source of a
19806b9b3e0SSimon J. Gerraty 	 * .SHELL target. For user-defined shells, this is the full path of
19906b9b3e0SSimon J. Gerraty 	 * the shell.
20006b9b3e0SSimon J. Gerraty 	 */
201956e45f6SSimon J. Gerraty 	const char *name;
202956e45f6SSimon J. Gerraty 
203b0c40a00SSimon J. Gerraty 	bool hasEchoCtl;	/* whether both echoOff and echoOn are there */
20406b9b3e0SSimon J. Gerraty 	const char *echoOff;	/* command to turn echoing off */
20506b9b3e0SSimon J. Gerraty 	const char *echoOn;	/* command to turn echoing back on */
20606b9b3e0SSimon J. Gerraty 	const char *noPrint;	/* text to skip when printing output from the
207956e45f6SSimon J. Gerraty 				 * shell. This is usually the same as echoOff */
208956e45f6SSimon J. Gerraty 	size_t noPrintLen;	/* length of noPrint command */
209956e45f6SSimon J. Gerraty 
210b0c40a00SSimon J. Gerraty 	bool hasErrCtl;		/* whether error checking can be controlled
21106b9b3e0SSimon J. Gerraty 				 * for individual commands */
21206b9b3e0SSimon J. Gerraty 	const char *errOn;	/* command to turn on error checking */
21306b9b3e0SSimon J. Gerraty 	const char *errOff;	/* command to turn off error checking */
21406b9b3e0SSimon J. Gerraty 
21506b9b3e0SSimon J. Gerraty 	const char *echoTmpl;	/* template to echo a command */
2169f45a3c8SSimon J. Gerraty 	const char *runIgnTmpl;	/* template to run a command without error
2179f45a3c8SSimon J. Gerraty 				 * checking */
2189f45a3c8SSimon J. Gerraty 	const char *runChkTmpl;	/* template to run a command with error
2199f45a3c8SSimon J. Gerraty 				 * checking */
220956e45f6SSimon J. Gerraty 
2219f45a3c8SSimon J. Gerraty 	/*
2229f45a3c8SSimon J. Gerraty 	 * A string literal that results in a newline character when it
2239f45a3c8SSimon J. Gerraty 	 * occurs outside of any 'quote' or "quote" characters.
2249f45a3c8SSimon J. Gerraty 	 */
225956e45f6SSimon J. Gerraty 	const char *newline;
226956e45f6SSimon J. Gerraty 	char commentChar;	/* character used by shell for comment lines */
227956e45f6SSimon J. Gerraty 
22806b9b3e0SSimon J. Gerraty 	const char *echoFlag;	/* shell flag to echo commands */
22906b9b3e0SSimon J. Gerraty 	const char *errFlag;	/* shell flag to exit on error */
230956e45f6SSimon J. Gerraty } Shell;
2313955d011SMarcel Moolenaar 
23206b9b3e0SSimon J. Gerraty typedef struct CommandFlags {
233dba7b0efSSimon J. Gerraty 	/* Whether to echo the command before or instead of running it. */
234b0c40a00SSimon J. Gerraty 	bool echo;
23506b9b3e0SSimon J. Gerraty 
23606b9b3e0SSimon J. Gerraty 	/* Run the command even in -n or -N mode. */
237b0c40a00SSimon J. Gerraty 	bool always;
23806b9b3e0SSimon J. Gerraty 
23906b9b3e0SSimon J. Gerraty 	/*
240b0c40a00SSimon J. Gerraty 	 * true if we turned error checking off before writing the command to
241b0c40a00SSimon J. Gerraty 	 * the commands file and need to turn it back on
24206b9b3e0SSimon J. Gerraty 	 */
243b0c40a00SSimon J. Gerraty 	bool ignerr;
24406b9b3e0SSimon J. Gerraty } CommandFlags;
24506b9b3e0SSimon J. Gerraty 
24606b9b3e0SSimon J. Gerraty /*
24706b9b3e0SSimon J. Gerraty  * Write shell commands to a file.
24806b9b3e0SSimon J. Gerraty  *
24906b9b3e0SSimon J. Gerraty  * TODO: keep track of whether commands are echoed.
25006b9b3e0SSimon J. Gerraty  * TODO: keep track of whether error checking is active.
25106b9b3e0SSimon J. Gerraty  */
25206b9b3e0SSimon J. Gerraty typedef struct ShellWriter {
25306b9b3e0SSimon J. Gerraty 	FILE *f;
25406b9b3e0SSimon J. Gerraty 
25506b9b3e0SSimon J. Gerraty 	/* we've sent 'set -x' */
256b0c40a00SSimon J. Gerraty 	bool xtraced;
25706b9b3e0SSimon J. Gerraty 
25806b9b3e0SSimon J. Gerraty } ShellWriter;
25906b9b3e0SSimon J. Gerraty 
2603955d011SMarcel Moolenaar /*
2619a4bc556SSimon J. Gerraty  * FreeBSD: traditionally .MAKE is not required to
2629a4bc556SSimon J. Gerraty  * pass jobs queue to sub-makes.
2639a4bc556SSimon J. Gerraty  * Use .MAKE.ALWAYS_PASS_JOB_QUEUE=no to disable.
2649a4bc556SSimon J. Gerraty  */
2653b96abbaSSimon J. Gerraty #define MAKE_ALWAYS_PASS_JOB_QUEUE "${.MAKE.ALWAYS_PASS_JOB_QUEUE:U}"
26668c4481aSSimon J. Gerraty static bool Always_pass_job_queue = true;
2672d395cb5SSimon J. Gerraty /*
2682d395cb5SSimon J. Gerraty  * FreeBSD: aborting entire parallel make isn't always
2692d395cb5SSimon J. Gerraty  * desired. When doing tinderbox for example, failure of
2702d395cb5SSimon J. Gerraty  * one architecture should not stop all.
2712d395cb5SSimon J. Gerraty  * We still want to bail on interrupt though.
2722d395cb5SSimon J. Gerraty  */
2733b96abbaSSimon J. Gerraty #define MAKE_JOB_ERROR_TOKEN "${MAKE_JOB_ERROR_TOKEN:U}"
27468c4481aSSimon J. Gerraty static bool Job_error_token = true;
2759a4bc556SSimon J. Gerraty 
276d5e0a182SSimon J. Gerraty /* error handling variables */
27706b9b3e0SSimon J. Gerraty static int job_errors = 0;	/* number of errors reported */
278b0c40a00SSimon J. Gerraty static enum {			/* Why is the make aborting? */
279956e45f6SSimon J. Gerraty 	ABORT_NONE,
280b0c40a00SSimon J. Gerraty 	ABORT_ERROR,		/* Aborted because of an error */
281b0c40a00SSimon J. Gerraty 	ABORT_INTERRUPT,	/* Aborted because it was interrupted */
282956e45f6SSimon J. Gerraty 	ABORT_WAIT		/* Waiting for jobs to finish */
283b0c40a00SSimon J. Gerraty } aborting = ABORT_NONE;
2843955d011SMarcel Moolenaar #define JOB_TOKENS "+EI+"	/* Token to requeue for each abort state */
2853955d011SMarcel Moolenaar 
286d5e0a182SSimon J. Gerraty /* Tracks the number of tokens currently "out" to build jobs. */
2873955d011SMarcel Moolenaar int jobTokensRunning = 0;
2883955d011SMarcel Moolenaar 
2893955d011SMarcel Moolenaar /*
2903955d011SMarcel Moolenaar  * Descriptions for various shells.
2913955d011SMarcel Moolenaar  *
2923955d011SMarcel Moolenaar  * The build environment may set DEFSHELL_INDEX to one of
2933955d011SMarcel Moolenaar  * DEFSHELL_INDEX_SH, DEFSHELL_INDEX_KSH, or DEFSHELL_INDEX_CSH, to
294956e45f6SSimon J. Gerraty  * select one of the predefined shells as the default shell.
2953955d011SMarcel Moolenaar  *
2963955d011SMarcel Moolenaar  * Alternatively, the build environment may set DEFSHELL_CUSTOM to the
2973955d011SMarcel Moolenaar  * name or the full path of a sh-compatible shell, which will be used as
2983955d011SMarcel Moolenaar  * the default shell.
2993955d011SMarcel Moolenaar  *
3003955d011SMarcel Moolenaar  * ".SHELL" lines in Makefiles can choose the default shell from the
301956e45f6SSimon J. Gerraty  * set defined here, or add additional shells.
3023955d011SMarcel Moolenaar  */
3033955d011SMarcel Moolenaar 
3043955d011SMarcel Moolenaar #ifdef DEFSHELL_CUSTOM
3053955d011SMarcel Moolenaar #define DEFSHELL_INDEX_CUSTOM 0
3063955d011SMarcel Moolenaar #define DEFSHELL_INDEX_SH     1
3073955d011SMarcel Moolenaar #define DEFSHELL_INDEX_KSH    2
3083955d011SMarcel Moolenaar #define DEFSHELL_INDEX_CSH    3
309d5e0a182SSimon J. Gerraty #else
3103955d011SMarcel Moolenaar #define DEFSHELL_INDEX_SH     0
3113955d011SMarcel Moolenaar #define DEFSHELL_INDEX_KSH    1
3123955d011SMarcel Moolenaar #define DEFSHELL_INDEX_CSH    2
313d5e0a182SSimon J. Gerraty #endif
3143955d011SMarcel Moolenaar 
3153955d011SMarcel Moolenaar #ifndef DEFSHELL_INDEX
3163955d011SMarcel Moolenaar #define DEFSHELL_INDEX 0	/* DEFSHELL_INDEX_CUSTOM or DEFSHELL_INDEX_SH */
317d5e0a182SSimon J. Gerraty #endif
3183955d011SMarcel Moolenaar 
3193955d011SMarcel Moolenaar static Shell shells[] = {
3203955d011SMarcel Moolenaar #ifdef DEFSHELL_CUSTOM
3213955d011SMarcel Moolenaar     /*
3223955d011SMarcel Moolenaar      * An sh-compatible shell with a non-standard name.
3233955d011SMarcel Moolenaar      *
3243955d011SMarcel Moolenaar      * Keep this in sync with the "sh" description below, but avoid
3253955d011SMarcel Moolenaar      * non-portable features that might not be supplied by all
3263955d011SMarcel Moolenaar      * sh-compatible shells.
3273955d011SMarcel Moolenaar      */
3283955d011SMarcel Moolenaar     {
329956e45f6SSimon J. Gerraty 	DEFSHELL_CUSTOM,	/* .name */
330b0c40a00SSimon J. Gerraty 	false,			/* .hasEchoCtl */
331956e45f6SSimon J. Gerraty 	"",			/* .echoOff */
332956e45f6SSimon J. Gerraty 	"",			/* .echoOn */
333956e45f6SSimon J. Gerraty 	"",			/* .noPrint */
334956e45f6SSimon J. Gerraty 	0,			/* .noPrintLen */
335b0c40a00SSimon J. Gerraty 	false,			/* .hasErrCtl */
33606b9b3e0SSimon J. Gerraty 	"",			/* .errOn */
33706b9b3e0SSimon J. Gerraty 	"",			/* .errOff */
33806b9b3e0SSimon J. Gerraty 	"echo \"%s\"\n",	/* .echoTmpl */
33906b9b3e0SSimon J. Gerraty 	"%s\n",			/* .runIgnTmpl */
34006b9b3e0SSimon J. Gerraty 	"{ %s \n} || exit $?\n", /* .runChkTmpl */
341956e45f6SSimon J. Gerraty 	"'\n'",			/* .newline */
342956e45f6SSimon J. Gerraty 	'#',			/* .commentChar */
34306b9b3e0SSimon J. Gerraty 	"",			/* .echoFlag */
34406b9b3e0SSimon J. Gerraty 	"",			/* .errFlag */
3453955d011SMarcel Moolenaar     },
3463955d011SMarcel Moolenaar #endif /* DEFSHELL_CUSTOM */
3473955d011SMarcel Moolenaar     /*
3483955d011SMarcel Moolenaar      * SH description. Echo control is also possible and, under
3493955d011SMarcel Moolenaar      * sun UNIX anyway, one can even control error checking.
3503955d011SMarcel Moolenaar      */
3513955d011SMarcel Moolenaar     {
352956e45f6SSimon J. Gerraty 	"sh",			/* .name */
353b0c40a00SSimon J. Gerraty 	false,			/* .hasEchoCtl */
354956e45f6SSimon J. Gerraty 	"",			/* .echoOff */
355956e45f6SSimon J. Gerraty 	"",			/* .echoOn */
356956e45f6SSimon J. Gerraty 	"",			/* .noPrint */
357956e45f6SSimon J. Gerraty 	0,			/* .noPrintLen */
358b0c40a00SSimon J. Gerraty 	false,			/* .hasErrCtl */
35906b9b3e0SSimon J. Gerraty 	"",			/* .errOn */
36006b9b3e0SSimon J. Gerraty 	"",			/* .errOff */
36106b9b3e0SSimon J. Gerraty 	"echo \"%s\"\n",	/* .echoTmpl */
36206b9b3e0SSimon J. Gerraty 	"%s\n",			/* .runIgnTmpl */
36306b9b3e0SSimon J. Gerraty 	"{ %s \n} || exit $?\n", /* .runChkTmpl */
364956e45f6SSimon J. Gerraty 	"'\n'",			/* .newline */
365956e45f6SSimon J. Gerraty 	'#',			/* .commentChar*/
3663955d011SMarcel Moolenaar #if defined(MAKE_NATIVE) && defined(__NetBSD__)
36706b9b3e0SSimon J. Gerraty 	/* XXX: -q is not really echoFlag, it's more like noEchoInSysFlag. */
36806b9b3e0SSimon J. Gerraty 	"q",			/* .echoFlag */
3693955d011SMarcel Moolenaar #else
37006b9b3e0SSimon J. Gerraty 	"",			/* .echoFlag */
3713955d011SMarcel Moolenaar #endif
37206b9b3e0SSimon J. Gerraty 	"",			/* .errFlag */
3733955d011SMarcel Moolenaar     },
3743955d011SMarcel Moolenaar     /*
3753955d011SMarcel Moolenaar      * KSH description.
3763955d011SMarcel Moolenaar      */
3773955d011SMarcel Moolenaar     {
378956e45f6SSimon J. Gerraty 	"ksh",			/* .name */
379b0c40a00SSimon J. Gerraty 	true,			/* .hasEchoCtl */
380956e45f6SSimon J. Gerraty 	"set +v",		/* .echoOff */
381956e45f6SSimon J. Gerraty 	"set -v",		/* .echoOn */
382956e45f6SSimon J. Gerraty 	"set +v",		/* .noPrint */
383956e45f6SSimon J. Gerraty 	6,			/* .noPrintLen */
384b0c40a00SSimon J. Gerraty 	false,			/* .hasErrCtl */
38506b9b3e0SSimon J. Gerraty 	"",			/* .errOn */
38606b9b3e0SSimon J. Gerraty 	"",			/* .errOff */
38706b9b3e0SSimon J. Gerraty 	"echo \"%s\"\n",	/* .echoTmpl */
38806b9b3e0SSimon J. Gerraty 	"%s\n",			/* .runIgnTmpl */
38906b9b3e0SSimon J. Gerraty 	"{ %s \n} || exit $?\n", /* .runChkTmpl */
390956e45f6SSimon J. Gerraty 	"'\n'",			/* .newline */
391956e45f6SSimon J. Gerraty 	'#',			/* .commentChar */
39206b9b3e0SSimon J. Gerraty 	"v",			/* .echoFlag */
39306b9b3e0SSimon J. Gerraty 	"",			/* .errFlag */
3943955d011SMarcel Moolenaar     },
3953955d011SMarcel Moolenaar     /*
3963955d011SMarcel Moolenaar      * CSH description. The csh can do echo control by playing
3973955d011SMarcel Moolenaar      * with the setting of the 'echo' shell variable. Sadly,
3983955d011SMarcel Moolenaar      * however, it is unable to do error control nicely.
3993955d011SMarcel Moolenaar      */
4003955d011SMarcel Moolenaar     {
401956e45f6SSimon J. Gerraty 	"csh",			/* .name */
402b0c40a00SSimon J. Gerraty 	true,			/* .hasEchoCtl */
403956e45f6SSimon J. Gerraty 	"unset verbose",	/* .echoOff */
404956e45f6SSimon J. Gerraty 	"set verbose",		/* .echoOn */
405956e45f6SSimon J. Gerraty 	"unset verbose",	/* .noPrint */
406956e45f6SSimon J. Gerraty 	13,			/* .noPrintLen */
407b0c40a00SSimon J. Gerraty 	false,			/* .hasErrCtl */
40806b9b3e0SSimon J. Gerraty 	"",			/* .errOn */
40906b9b3e0SSimon J. Gerraty 	"",			/* .errOff */
41006b9b3e0SSimon J. Gerraty 	"echo \"%s\"\n",	/* .echoTmpl */
41106b9b3e0SSimon J. Gerraty 	"csh -c \"%s || exit 0\"\n", /* .runIgnTmpl */
41206b9b3e0SSimon J. Gerraty 	"",			/* .runChkTmpl */
413956e45f6SSimon J. Gerraty 	"'\\\n'",		/* .newline */
414956e45f6SSimon J. Gerraty 	'#',			/* .commentChar */
41506b9b3e0SSimon J. Gerraty 	"v",			/* .echoFlag */
41606b9b3e0SSimon J. Gerraty 	"e",			/* .errFlag */
4173955d011SMarcel Moolenaar     }
4183955d011SMarcel Moolenaar };
419956e45f6SSimon J. Gerraty 
42006b9b3e0SSimon J. Gerraty /*
42106b9b3e0SSimon J. Gerraty  * This is the shell to which we pass all commands in the Makefile.
42206b9b3e0SSimon J. Gerraty  * It is set by the Job_ParseShell function.
42306b9b3e0SSimon J. Gerraty  */
42406b9b3e0SSimon J. Gerraty static Shell *shell = &shells[DEFSHELL_INDEX];
42522619282SSimon J. Gerraty char *shellPath;		/* full pathname of executable image */
426956e45f6SSimon J. Gerraty const char *shellName = NULL;	/* last component of shellPath */
42751ee2c1cSSimon J. Gerraty char *shellErrFlag = NULL;
428dba7b0efSSimon J. Gerraty static char *shell_freeIt = NULL; /* Allocated memory for custom .SHELL */
4293955d011SMarcel Moolenaar 
4303955d011SMarcel Moolenaar 
431956e45f6SSimon J. Gerraty static Job *job_table;		/* The structures that describe them */
432956e45f6SSimon J. Gerraty static Job *job_table_end;	/* job_table + maxJobs */
433d5e0a182SSimon J. Gerraty static unsigned int wantToken;
434b0c40a00SSimon J. Gerraty static bool lurking_children = false;
435b0c40a00SSimon J. Gerraty static bool make_suspended = false; /* Whether we've seen a SIGTSTP (etc) */
4363955d011SMarcel Moolenaar 
4373955d011SMarcel Moolenaar /*
4383955d011SMarcel Moolenaar  * Set of descriptors of pipes connected to
4393955d011SMarcel Moolenaar  * the output channels of children
4403955d011SMarcel Moolenaar  */
4413955d011SMarcel Moolenaar static struct pollfd *fds = NULL;
442dba7b0efSSimon J. Gerraty static Job **jobByFdIndex = NULL;
443dba7b0efSSimon J. Gerraty static nfds_t fdsLen = 0;
4443955d011SMarcel Moolenaar static void watchfd(Job *);
4453955d011SMarcel Moolenaar static void clearfd(Job *);
446b0c40a00SSimon J. Gerraty static bool readyfd(Job *);
4473955d011SMarcel Moolenaar 
44806b9b3e0SSimon J. Gerraty static char *targPrefix = NULL;	/* To identify a job change in the output. */
4493955d011SMarcel Moolenaar static Job tokenWaitJob;	/* token wait pseudo-job */
4503955d011SMarcel Moolenaar 
4513955d011SMarcel Moolenaar static Job childExitJob;	/* child exit pseudo-job */
4523955d011SMarcel Moolenaar #define CHILD_EXIT "."
4533955d011SMarcel Moolenaar #define DO_JOB_RESUME "R"
4543955d011SMarcel Moolenaar 
45506b9b3e0SSimon J. Gerraty enum {
45606b9b3e0SSimon J. Gerraty 	npseudojobs = 2		/* number of pseudo-jobs */
45706b9b3e0SSimon J. Gerraty };
4583955d011SMarcel Moolenaar 
4593955d011SMarcel Moolenaar static sigset_t caught_signals;	/* Set of signals we handle */
460dba7b0efSSimon J. Gerraty static volatile sig_atomic_t caught_sigchld;
4613955d011SMarcel Moolenaar 
462b0c40a00SSimon J. Gerraty static void CollectOutput(Job *, bool);
463b0c40a00SSimon J. Gerraty static void JobInterrupt(bool, int) MAKE_ATTR_DEAD;
4643955d011SMarcel Moolenaar static void JobRestartJobs(void);
4653955d011SMarcel Moolenaar static void JobSigReset(void);
4663955d011SMarcel Moolenaar 
46706b9b3e0SSimon J. Gerraty static void
46806b9b3e0SSimon J. Gerraty SwitchOutputTo(GNode *gn)
46906b9b3e0SSimon J. Gerraty {
47006b9b3e0SSimon J. Gerraty 	/* The node for which output was most recently produced. */
47106b9b3e0SSimon J. Gerraty 	static GNode *lastNode = NULL;
47206b9b3e0SSimon J. Gerraty 
47306b9b3e0SSimon J. Gerraty 	if (gn == lastNode)
47406b9b3e0SSimon J. Gerraty 		return;
47506b9b3e0SSimon J. Gerraty 	lastNode = gn;
47606b9b3e0SSimon J. Gerraty 
47706b9b3e0SSimon J. Gerraty 	if (opts.maxJobs != 1 && targPrefix != NULL && targPrefix[0] != '\0')
47806b9b3e0SSimon J. Gerraty 		(void)fprintf(stdout, "%s %s ---\n", targPrefix, gn->name);
47906b9b3e0SSimon J. Gerraty }
48006b9b3e0SSimon J. Gerraty 
48149caa483SSimon J. Gerraty static unsigned
48249caa483SSimon J. Gerraty nfds_per_job(void)
48349caa483SSimon J. Gerraty {
48449caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
48549caa483SSimon J. Gerraty 	if (useMeta)
48649caa483SSimon J. Gerraty 		return 2;
48749caa483SSimon J. Gerraty #endif
48849caa483SSimon J. Gerraty 	return 1;
48949caa483SSimon J. Gerraty }
49049caa483SSimon J. Gerraty 
49106b9b3e0SSimon J. Gerraty void
49206b9b3e0SSimon J. Gerraty Job_FlagsToString(const Job *job, char *buf, size_t bufsize)
49306b9b3e0SSimon J. Gerraty {
49406b9b3e0SSimon J. Gerraty 	snprintf(buf, bufsize, "%c%c%c",
49506b9b3e0SSimon J. Gerraty 	    job->ignerr ? 'i' : '-',
49606b9b3e0SSimon J. Gerraty 	    !job->echo ? 's' : '-',
49706b9b3e0SSimon J. Gerraty 	    job->special ? 'S' : '-');
49806b9b3e0SSimon J. Gerraty }
49906b9b3e0SSimon J. Gerraty 
5003955d011SMarcel Moolenaar static void
501b0c40a00SSimon J. Gerraty DumpJobs(const char *where)
5023955d011SMarcel Moolenaar {
5033955d011SMarcel Moolenaar 	Job *job;
50406b9b3e0SSimon J. Gerraty 	char flags[4];
5053955d011SMarcel Moolenaar 
506956e45f6SSimon J. Gerraty 	debug_printf("job table @ %s\n", where);
5073955d011SMarcel Moolenaar 	for (job = job_table; job < job_table_end; job++) {
50806b9b3e0SSimon J. Gerraty 		Job_FlagsToString(job, flags, sizeof flags);
50906b9b3e0SSimon J. Gerraty 		debug_printf("job %d, status %d, flags %s, pid %d\n",
51006b9b3e0SSimon J. Gerraty 		    (int)(job - job_table), job->status, flags, job->pid);
5113955d011SMarcel Moolenaar 	}
5123955d011SMarcel Moolenaar }
5133955d011SMarcel Moolenaar 
5143955d011SMarcel Moolenaar /*
51545447996SSimon J. Gerraty  * Delete the target of a failed, interrupted, or otherwise
51645447996SSimon J. Gerraty  * unsuccessful job unless inhibited by .PRECIOUS.
51745447996SSimon J. Gerraty  */
51845447996SSimon J. Gerraty static void
51945447996SSimon J. Gerraty JobDeleteTarget(GNode *gn)
52045447996SSimon J. Gerraty {
521956e45f6SSimon J. Gerraty 	const char *file;
522956e45f6SSimon J. Gerraty 
523956e45f6SSimon J. Gerraty 	if (gn->type & OP_JOIN)
524956e45f6SSimon J. Gerraty 		return;
525956e45f6SSimon J. Gerraty 	if (gn->type & OP_PHONY)
526956e45f6SSimon J. Gerraty 		return;
5279f45a3c8SSimon J. Gerraty 	if (GNode_IsPrecious(gn))
528956e45f6SSimon J. Gerraty 		return;
529956e45f6SSimon J. Gerraty 	if (opts.noExecute)
530956e45f6SSimon J. Gerraty 		return;
531956e45f6SSimon J. Gerraty 
532956e45f6SSimon J. Gerraty 	file = GNode_Path(gn);
5334fde40d9SSimon J. Gerraty 	if (unlink_file(file) == 0)
53445447996SSimon J. Gerraty 		Error("*** %s removed", file);
53545447996SSimon J. Gerraty }
53645447996SSimon J. Gerraty 
53745447996SSimon J. Gerraty /*
5383955d011SMarcel Moolenaar  * JobSigLock/JobSigUnlock
5393955d011SMarcel Moolenaar  *
5403955d011SMarcel Moolenaar  * Signal lock routines to get exclusive access. Currently used to
5413955d011SMarcel Moolenaar  * protect `jobs' and `stoppedJobs' list manipulations.
5423955d011SMarcel Moolenaar  */
54306b9b3e0SSimon J. Gerraty static void
54406b9b3e0SSimon J. Gerraty JobSigLock(sigset_t *omaskp)
5453955d011SMarcel Moolenaar {
5463955d011SMarcel Moolenaar 	if (sigprocmask(SIG_BLOCK, &caught_signals, omaskp) != 0) {
5473955d011SMarcel Moolenaar 		Punt("JobSigLock: sigprocmask: %s", strerror(errno));
5483955d011SMarcel Moolenaar 		sigemptyset(omaskp);
5493955d011SMarcel Moolenaar 	}
5503955d011SMarcel Moolenaar }
5513955d011SMarcel Moolenaar 
55206b9b3e0SSimon J. Gerraty static void
55306b9b3e0SSimon J. Gerraty JobSigUnlock(sigset_t *omaskp)
5543955d011SMarcel Moolenaar {
5553955d011SMarcel Moolenaar 	(void)sigprocmask(SIG_SETMASK, omaskp, NULL);
5563955d011SMarcel Moolenaar }
5573955d011SMarcel Moolenaar 
5583955d011SMarcel Moolenaar static void
5593955d011SMarcel Moolenaar JobCreatePipe(Job *job, int minfd)
5603955d011SMarcel Moolenaar {
561e1cee40dSSimon J. Gerraty 	int i, fd, flags;
562956e45f6SSimon J. Gerraty 	int pipe_fds[2];
5633955d011SMarcel Moolenaar 
564956e45f6SSimon J. Gerraty 	if (pipe(pipe_fds) == -1)
5653955d011SMarcel Moolenaar 		Punt("Cannot create pipe: %s", strerror(errno));
5663955d011SMarcel Moolenaar 
56774d2e02bSSimon J. Gerraty 	for (i = 0; i < 2; i++) {
568d5e0a182SSimon J. Gerraty 		/* Avoid using low-numbered fds */
569956e45f6SSimon J. Gerraty 		fd = fcntl(pipe_fds[i], F_DUPFD, minfd);
57074d2e02bSSimon J. Gerraty 		if (fd != -1) {
571956e45f6SSimon J. Gerraty 			close(pipe_fds[i]);
572956e45f6SSimon J. Gerraty 			pipe_fds[i] = fd;
57374d2e02bSSimon J. Gerraty 		}
57474d2e02bSSimon J. Gerraty 	}
57574d2e02bSSimon J. Gerraty 
576956e45f6SSimon J. Gerraty 	job->inPipe = pipe_fds[0];
577956e45f6SSimon J. Gerraty 	job->outPipe = pipe_fds[1];
578956e45f6SSimon J. Gerraty 
579956e45f6SSimon J. Gerraty 	if (fcntl(job->inPipe, F_SETFD, FD_CLOEXEC) == -1)
580e1cee40dSSimon J. Gerraty 		Punt("Cannot set close-on-exec: %s", strerror(errno));
581956e45f6SSimon J. Gerraty 	if (fcntl(job->outPipe, F_SETFD, FD_CLOEXEC) == -1)
582e1cee40dSSimon J. Gerraty 		Punt("Cannot set close-on-exec: %s", strerror(errno));
5833955d011SMarcel Moolenaar 
5843955d011SMarcel Moolenaar 	/*
5853955d011SMarcel Moolenaar 	 * We mark the input side of the pipe non-blocking; we poll(2) the
5863955d011SMarcel Moolenaar 	 * pipe when we're waiting for a job token, but we might lose the
5873955d011SMarcel Moolenaar 	 * race for the token when a new one becomes available, so the read
5883955d011SMarcel Moolenaar 	 * from the pipe should not block.
5893955d011SMarcel Moolenaar 	 */
590956e45f6SSimon J. Gerraty 	flags = fcntl(job->inPipe, F_GETFL, 0);
591e1cee40dSSimon J. Gerraty 	if (flags == -1)
592e1cee40dSSimon J. Gerraty 		Punt("Cannot get flags: %s", strerror(errno));
593e1cee40dSSimon J. Gerraty 	flags |= O_NONBLOCK;
594956e45f6SSimon J. Gerraty 	if (fcntl(job->inPipe, F_SETFL, flags) == -1)
595e1cee40dSSimon J. Gerraty 		Punt("Cannot set flags: %s", strerror(errno));
5963955d011SMarcel Moolenaar }
5973955d011SMarcel Moolenaar 
598956e45f6SSimon J. Gerraty /* Pass the signal to each running job. */
5993955d011SMarcel Moolenaar static void
6003955d011SMarcel Moolenaar JobCondPassSig(int signo)
6013955d011SMarcel Moolenaar {
6023955d011SMarcel Moolenaar 	Job *job;
6033955d011SMarcel Moolenaar 
604956e45f6SSimon J. Gerraty 	DEBUG1(JOB, "JobCondPassSig(%d) called.\n", signo);
6053955d011SMarcel Moolenaar 
6063955d011SMarcel Moolenaar 	for (job = job_table; job < job_table_end; job++) {
607e2eeea75SSimon J. Gerraty 		if (job->status != JOB_ST_RUNNING)
6083955d011SMarcel Moolenaar 			continue;
609956e45f6SSimon J. Gerraty 		DEBUG2(JOB, "JobCondPassSig passing signal %d to child %d.\n",
6103955d011SMarcel Moolenaar 		    signo, job->pid);
6113955d011SMarcel Moolenaar 		KILLPG(job->pid, signo);
6123955d011SMarcel Moolenaar 	}
6133955d011SMarcel Moolenaar }
6143955d011SMarcel Moolenaar 
61506b9b3e0SSimon J. Gerraty /*
61606b9b3e0SSimon J. Gerraty  * SIGCHLD handler.
6173955d011SMarcel Moolenaar  *
61806b9b3e0SSimon J. Gerraty  * Sends a token on the child exit pipe to wake us up from select()/poll().
61906b9b3e0SSimon J. Gerraty  */
6203955d011SMarcel Moolenaar static void
6213955d011SMarcel Moolenaar JobChildSig(int signo MAKE_ATTR_UNUSED)
6223955d011SMarcel Moolenaar {
623dba7b0efSSimon J. Gerraty 	caught_sigchld = 1;
62406b9b3e0SSimon J. Gerraty 	while (write(childExitJob.outPipe, CHILD_EXIT, 1) == -1 &&
62506b9b3e0SSimon J. Gerraty 	       errno == EAGAIN)
6263cbdda60SSimon J. Gerraty 		continue;
6273955d011SMarcel Moolenaar }
6283955d011SMarcel Moolenaar 
6293955d011SMarcel Moolenaar 
630956e45f6SSimon J. Gerraty /* Resume all stopped jobs. */
6313955d011SMarcel Moolenaar static void
6323955d011SMarcel Moolenaar JobContinueSig(int signo MAKE_ATTR_UNUSED)
6333955d011SMarcel Moolenaar {
6343955d011SMarcel Moolenaar 	/*
635956e45f6SSimon J. Gerraty 	 * Defer sending SIGCONT to our stopped children until we return
6363955d011SMarcel Moolenaar 	 * from the signal handler.
6373955d011SMarcel Moolenaar 	 */
6383cbdda60SSimon J. Gerraty 	while (write(childExitJob.outPipe, DO_JOB_RESUME, 1) == -1 &&
6393cbdda60SSimon J. Gerraty 	       errno == EAGAIN)
6403cbdda60SSimon J. Gerraty 		continue;
6413955d011SMarcel Moolenaar }
6423955d011SMarcel Moolenaar 
64306b9b3e0SSimon J. Gerraty /*
64406b9b3e0SSimon J. Gerraty  * Pass a signal on to all jobs, then resend to ourselves.
64506b9b3e0SSimon J. Gerraty  * We die by the same signal.
64606b9b3e0SSimon J. Gerraty  */
6473955d011SMarcel Moolenaar MAKE_ATTR_DEAD static void
6483955d011SMarcel Moolenaar JobPassSig_int(int signo)
6493955d011SMarcel Moolenaar {
6503955d011SMarcel Moolenaar 	/* Run .INTERRUPT target then exit */
651b0c40a00SSimon J. Gerraty 	JobInterrupt(true, signo);
6523955d011SMarcel Moolenaar }
6533955d011SMarcel Moolenaar 
65406b9b3e0SSimon J. Gerraty /*
65506b9b3e0SSimon J. Gerraty  * Pass a signal on to all jobs, then resend to ourselves.
65606b9b3e0SSimon J. Gerraty  * We die by the same signal.
65706b9b3e0SSimon J. Gerraty  */
6583955d011SMarcel Moolenaar MAKE_ATTR_DEAD static void
6593955d011SMarcel Moolenaar JobPassSig_term(int signo)
6603955d011SMarcel Moolenaar {
6613955d011SMarcel Moolenaar 	/* Dont run .INTERRUPT target then exit */
662b0c40a00SSimon J. Gerraty 	JobInterrupt(false, signo);
6633955d011SMarcel Moolenaar }
6643955d011SMarcel Moolenaar 
6653955d011SMarcel Moolenaar static void
6663955d011SMarcel Moolenaar JobPassSig_suspend(int signo)
6673955d011SMarcel Moolenaar {
6683955d011SMarcel Moolenaar 	sigset_t nmask, omask;
6693955d011SMarcel Moolenaar 	struct sigaction act;
6703955d011SMarcel Moolenaar 
6713955d011SMarcel Moolenaar 	/* Suppress job started/continued messages */
672b0c40a00SSimon J. Gerraty 	make_suspended = true;
6733955d011SMarcel Moolenaar 
6743955d011SMarcel Moolenaar 	/* Pass the signal onto every job */
6753955d011SMarcel Moolenaar 	JobCondPassSig(signo);
6763955d011SMarcel Moolenaar 
6773955d011SMarcel Moolenaar 	/*
67806b9b3e0SSimon J. Gerraty 	 * Send ourselves the signal now we've given the message to everyone
67906b9b3e0SSimon J. Gerraty 	 * else. Note we block everything else possible while we're getting
68006b9b3e0SSimon J. Gerraty 	 * the signal. This ensures that all our jobs get continued when we
68106b9b3e0SSimon J. Gerraty 	 * wake up before we take any other signal.
6823955d011SMarcel Moolenaar 	 */
6833955d011SMarcel Moolenaar 	sigfillset(&nmask);
6843955d011SMarcel Moolenaar 	sigdelset(&nmask, signo);
6853955d011SMarcel Moolenaar 	(void)sigprocmask(SIG_SETMASK, &nmask, &omask);
6863955d011SMarcel Moolenaar 
6873955d011SMarcel Moolenaar 	act.sa_handler = SIG_DFL;
6883955d011SMarcel Moolenaar 	sigemptyset(&act.sa_mask);
6893955d011SMarcel Moolenaar 	act.sa_flags = 0;
6903955d011SMarcel Moolenaar 	(void)sigaction(signo, &act, NULL);
6913955d011SMarcel Moolenaar 
69206b9b3e0SSimon J. Gerraty 	DEBUG1(JOB, "JobPassSig passing signal %d to self.\n", signo);
6933955d011SMarcel Moolenaar 
6943955d011SMarcel Moolenaar 	(void)kill(getpid(), signo);
6953955d011SMarcel Moolenaar 
6963955d011SMarcel Moolenaar 	/*
6973955d011SMarcel Moolenaar 	 * We've been continued.
6983955d011SMarcel Moolenaar 	 *
699d5e0a182SSimon J. Gerraty 	 * A whole host of signals is going to happen!
7003955d011SMarcel Moolenaar 	 * SIGCHLD for any processes that actually suspended themselves.
701d5e0a182SSimon J. Gerraty 	 * SIGCHLD for any processes that exited while we were asleep.
7023955d011SMarcel Moolenaar 	 * The SIGCONT that actually caused us to wake up.
7033955d011SMarcel Moolenaar 	 *
7043955d011SMarcel Moolenaar 	 * Since we defer passing the SIGCONT on to our children until
7053955d011SMarcel Moolenaar 	 * the main processing loop, we can be sure that all the SIGCHLD
7063955d011SMarcel Moolenaar 	 * events will have happened by then - and that the waitpid() will
7073955d011SMarcel Moolenaar 	 * collect the child 'suspended' events.
7083955d011SMarcel Moolenaar 	 * For correct sequencing we just need to ensure we process the
709956e45f6SSimon J. Gerraty 	 * waitpid() before passing on the SIGCONT.
7103955d011SMarcel Moolenaar 	 *
7113955d011SMarcel Moolenaar 	 * In any case nothing else is needed here.
7123955d011SMarcel Moolenaar 	 */
7133955d011SMarcel Moolenaar 
7143955d011SMarcel Moolenaar 	/* Restore handler and signal mask */
7153955d011SMarcel Moolenaar 	act.sa_handler = JobPassSig_suspend;
7163955d011SMarcel Moolenaar 	(void)sigaction(signo, &act, NULL);
7173955d011SMarcel Moolenaar 	(void)sigprocmask(SIG_SETMASK, &omask, NULL);
7183955d011SMarcel Moolenaar }
7193955d011SMarcel Moolenaar 
7203955d011SMarcel Moolenaar static Job *
721b0c40a00SSimon J. Gerraty JobFindPid(int pid, JobStatus status, bool isJobs)
7223955d011SMarcel Moolenaar {
7233955d011SMarcel Moolenaar 	Job *job;
7243955d011SMarcel Moolenaar 
7253955d011SMarcel Moolenaar 	for (job = job_table; job < job_table_end; job++) {
726e2eeea75SSimon J. Gerraty 		if (job->status == status && job->pid == pid)
7273955d011SMarcel Moolenaar 			return job;
7283955d011SMarcel Moolenaar 	}
7293955d011SMarcel Moolenaar 	if (DEBUG(JOB) && isJobs)
730b0c40a00SSimon J. Gerraty 		DumpJobs("no pid");
7313955d011SMarcel Moolenaar 	return NULL;
7323955d011SMarcel Moolenaar }
7333955d011SMarcel Moolenaar 
734956e45f6SSimon J. Gerraty /* Parse leading '@', '-' and '+', which control the exact execution mode. */
735956e45f6SSimon J. Gerraty static void
73606b9b3e0SSimon J. Gerraty ParseCommandFlags(char **pp, CommandFlags *out_cmdFlags)
737956e45f6SSimon J. Gerraty {
738956e45f6SSimon J. Gerraty 	char *p = *pp;
739b0c40a00SSimon J. Gerraty 	out_cmdFlags->echo = true;
740b0c40a00SSimon J. Gerraty 	out_cmdFlags->ignerr = false;
741b0c40a00SSimon J. Gerraty 	out_cmdFlags->always = false;
742956e45f6SSimon J. Gerraty 
743956e45f6SSimon J. Gerraty 	for (;;) {
744956e45f6SSimon J. Gerraty 		if (*p == '@')
74506b9b3e0SSimon J. Gerraty 			out_cmdFlags->echo = DEBUG(LOUD);
746956e45f6SSimon J. Gerraty 		else if (*p == '-')
747b0c40a00SSimon J. Gerraty 			out_cmdFlags->ignerr = true;
748956e45f6SSimon J. Gerraty 		else if (*p == '+')
749b0c40a00SSimon J. Gerraty 			out_cmdFlags->always = true;
7504fde40d9SSimon J. Gerraty 		else if (!ch_isspace(*p))
751d5e0a182SSimon J. Gerraty 			/* Ignore whitespace for compatibility with GNU make */
752956e45f6SSimon J. Gerraty 			break;
753956e45f6SSimon J. Gerraty 		p++;
754956e45f6SSimon J. Gerraty 	}
755956e45f6SSimon J. Gerraty 
756956e45f6SSimon J. Gerraty 	pp_skip_whitespace(&p);
757956e45f6SSimon J. Gerraty 
758956e45f6SSimon J. Gerraty 	*pp = p;
759956e45f6SSimon J. Gerraty }
760956e45f6SSimon J. Gerraty 
761956e45f6SSimon J. Gerraty /* Escape a string for a double-quoted string literal in sh, csh and ksh. */
762956e45f6SSimon J. Gerraty static char *
763956e45f6SSimon J. Gerraty EscapeShellDblQuot(const char *cmd)
764956e45f6SSimon J. Gerraty {
765956e45f6SSimon J. Gerraty 	size_t i, j;
766956e45f6SSimon J. Gerraty 
767956e45f6SSimon J. Gerraty 	/* Worst that could happen is every char needs escaping. */
768956e45f6SSimon J. Gerraty 	char *esc = bmake_malloc(strlen(cmd) * 2 + 1);
769956e45f6SSimon J. Gerraty 	for (i = 0, j = 0; cmd[i] != '\0'; i++, j++) {
77006b9b3e0SSimon J. Gerraty 		if (cmd[i] == '$' || cmd[i] == '`' || cmd[i] == '\\' ||
77106b9b3e0SSimon J. Gerraty 		    cmd[i] == '"')
772956e45f6SSimon J. Gerraty 			esc[j++] = '\\';
773956e45f6SSimon J. Gerraty 		esc[j] = cmd[i];
774956e45f6SSimon J. Gerraty 	}
775956e45f6SSimon J. Gerraty 	esc[j] = '\0';
776956e45f6SSimon J. Gerraty 
777956e45f6SSimon J. Gerraty 	return esc;
778956e45f6SSimon J. Gerraty }
779956e45f6SSimon J. Gerraty 
780e2eeea75SSimon J. Gerraty static void
781b0c40a00SSimon J. Gerraty ShellWriter_WriteFmt(ShellWriter *wr, const char *fmt, const char *arg)
782e2eeea75SSimon J. Gerraty {
78306b9b3e0SSimon J. Gerraty 	DEBUG1(JOB, fmt, arg);
784e2eeea75SSimon J. Gerraty 
78506b9b3e0SSimon J. Gerraty 	(void)fprintf(wr->f, fmt, arg);
78612904384SSimon J. Gerraty 	if (wr->f == stdout)
78706b9b3e0SSimon J. Gerraty 		(void)fflush(wr->f);
788e2eeea75SSimon J. Gerraty }
789e2eeea75SSimon J. Gerraty 
790e2eeea75SSimon J. Gerraty static void
791b0c40a00SSimon J. Gerraty ShellWriter_WriteLine(ShellWriter *wr, const char *line)
792e2eeea75SSimon J. Gerraty {
793b0c40a00SSimon J. Gerraty 	ShellWriter_WriteFmt(wr, "%s\n", line);
794e2eeea75SSimon J. Gerraty }
795e2eeea75SSimon J. Gerraty 
79606b9b3e0SSimon J. Gerraty static void
79706b9b3e0SSimon J. Gerraty ShellWriter_EchoOff(ShellWriter *wr)
79806b9b3e0SSimon J. Gerraty {
79906b9b3e0SSimon J. Gerraty 	if (shell->hasEchoCtl)
800b0c40a00SSimon J. Gerraty 		ShellWriter_WriteLine(wr, shell->echoOff);
80106b9b3e0SSimon J. Gerraty }
80206b9b3e0SSimon J. Gerraty 
80306b9b3e0SSimon J. Gerraty static void
80406b9b3e0SSimon J. Gerraty ShellWriter_EchoCmd(ShellWriter *wr, const char *escCmd)
80506b9b3e0SSimon J. Gerraty {
806b0c40a00SSimon J. Gerraty 	ShellWriter_WriteFmt(wr, shell->echoTmpl, escCmd);
80706b9b3e0SSimon J. Gerraty }
80806b9b3e0SSimon J. Gerraty 
80906b9b3e0SSimon J. Gerraty static void
81006b9b3e0SSimon J. Gerraty ShellWriter_EchoOn(ShellWriter *wr)
81106b9b3e0SSimon J. Gerraty {
81206b9b3e0SSimon J. Gerraty 	if (shell->hasEchoCtl)
813b0c40a00SSimon J. Gerraty 		ShellWriter_WriteLine(wr, shell->echoOn);
81406b9b3e0SSimon J. Gerraty }
81506b9b3e0SSimon J. Gerraty 
81606b9b3e0SSimon J. Gerraty static void
81706b9b3e0SSimon J. Gerraty ShellWriter_TraceOn(ShellWriter *wr)
81806b9b3e0SSimon J. Gerraty {
81906b9b3e0SSimon J. Gerraty 	if (!wr->xtraced) {
820b0c40a00SSimon J. Gerraty 		ShellWriter_WriteLine(wr, "set -x");
821b0c40a00SSimon J. Gerraty 		wr->xtraced = true;
82206b9b3e0SSimon J. Gerraty 	}
82306b9b3e0SSimon J. Gerraty }
82406b9b3e0SSimon J. Gerraty 
82506b9b3e0SSimon J. Gerraty static void
826b0c40a00SSimon J. Gerraty ShellWriter_ErrOff(ShellWriter *wr, bool echo)
82706b9b3e0SSimon J. Gerraty {
82806b9b3e0SSimon J. Gerraty 	if (echo)
82906b9b3e0SSimon J. Gerraty 		ShellWriter_EchoOff(wr);
830b0c40a00SSimon J. Gerraty 	ShellWriter_WriteLine(wr, shell->errOff);
83106b9b3e0SSimon J. Gerraty 	if (echo)
83206b9b3e0SSimon J. Gerraty 		ShellWriter_EchoOn(wr);
83306b9b3e0SSimon J. Gerraty }
83406b9b3e0SSimon J. Gerraty 
83506b9b3e0SSimon J. Gerraty static void
836b0c40a00SSimon J. Gerraty ShellWriter_ErrOn(ShellWriter *wr, bool echo)
83706b9b3e0SSimon J. Gerraty {
83806b9b3e0SSimon J. Gerraty 	if (echo)
83906b9b3e0SSimon J. Gerraty 		ShellWriter_EchoOff(wr);
840b0c40a00SSimon J. Gerraty 	ShellWriter_WriteLine(wr, shell->errOn);
84106b9b3e0SSimon J. Gerraty 	if (echo)
84206b9b3e0SSimon J. Gerraty 		ShellWriter_EchoOn(wr);
84306b9b3e0SSimon J. Gerraty }
84406b9b3e0SSimon J. Gerraty 
84506b9b3e0SSimon J. Gerraty /*
84606b9b3e0SSimon J. Gerraty  * The shell has no built-in error control, so emulate error control by
84706b9b3e0SSimon J. Gerraty  * enclosing each shell command in a template like "{ %s \n } || exit $?"
84806b9b3e0SSimon J. Gerraty  * (configurable per shell).
8493955d011SMarcel Moolenaar  */
850956e45f6SSimon J. Gerraty static void
851b0c40a00SSimon J. Gerraty JobWriteSpecialsEchoCtl(Job *job, ShellWriter *wr, CommandFlags *inout_cmdFlags,
85206b9b3e0SSimon J. Gerraty 			const char *escCmd, const char **inout_cmdTemplate)
8533955d011SMarcel Moolenaar {
8541d3f2ddcSSimon J. Gerraty 	/* XXX: Why is the whole job modified at this point? */
855b0c40a00SSimon J. Gerraty 	job->ignerr = true;
8563955d011SMarcel Moolenaar 
85706b9b3e0SSimon J. Gerraty 	if (job->echo && inout_cmdFlags->echo) {
85806b9b3e0SSimon J. Gerraty 		ShellWriter_EchoOff(wr);
85906b9b3e0SSimon J. Gerraty 		ShellWriter_EchoCmd(wr, escCmd);
8603955d011SMarcel Moolenaar 
86106b9b3e0SSimon J. Gerraty 		/*
86206b9b3e0SSimon J. Gerraty 		 * Leave echoing off so the user doesn't see the commands
86306b9b3e0SSimon J. Gerraty 		 * for toggling the error checking.
86406b9b3e0SSimon J. Gerraty 		 */
865b0c40a00SSimon J. Gerraty 		inout_cmdFlags->echo = false;
86606b9b3e0SSimon J. Gerraty 	}
86706b9b3e0SSimon J. Gerraty 	*inout_cmdTemplate = shell->runIgnTmpl;
8683955d011SMarcel Moolenaar 
86906b9b3e0SSimon J. Gerraty 	/*
87006b9b3e0SSimon J. Gerraty 	 * The template runIgnTmpl already takes care of ignoring errors,
87106b9b3e0SSimon J. Gerraty 	 * so pretend error checking is still on.
87206b9b3e0SSimon J. Gerraty 	 * XXX: What effects does this have, and why is it necessary?
87306b9b3e0SSimon J. Gerraty 	 */
874b0c40a00SSimon J. Gerraty 	inout_cmdFlags->ignerr = false;
87506b9b3e0SSimon J. Gerraty }
87606b9b3e0SSimon J. Gerraty 
87706b9b3e0SSimon J. Gerraty static void
878b0c40a00SSimon J. Gerraty JobWriteSpecials(Job *job, ShellWriter *wr, const char *escCmd, bool run,
87906b9b3e0SSimon J. Gerraty 		 CommandFlags *inout_cmdFlags, const char **inout_cmdTemplate)
88006b9b3e0SSimon J. Gerraty {
881d5e0a182SSimon J. Gerraty 	if (!run)
882b0c40a00SSimon J. Gerraty 		inout_cmdFlags->ignerr = false;
883d5e0a182SSimon J. Gerraty 	else if (shell->hasErrCtl)
88406b9b3e0SSimon J. Gerraty 		ShellWriter_ErrOff(wr, job->echo && inout_cmdFlags->echo);
88506b9b3e0SSimon J. Gerraty 	else if (shell->runIgnTmpl != NULL && shell->runIgnTmpl[0] != '\0') {
886b0c40a00SSimon J. Gerraty 		JobWriteSpecialsEchoCtl(job, wr, inout_cmdFlags, escCmd,
88706b9b3e0SSimon J. Gerraty 		    inout_cmdTemplate);
88806b9b3e0SSimon J. Gerraty 	} else
889b0c40a00SSimon J. Gerraty 		inout_cmdFlags->ignerr = false;
89006b9b3e0SSimon J. Gerraty }
89106b9b3e0SSimon J. Gerraty 
89206b9b3e0SSimon J. Gerraty /*
893b0c40a00SSimon J. Gerraty  * Write a shell command to the job's commands file, to be run later.
89406b9b3e0SSimon J. Gerraty  *
89506b9b3e0SSimon J. Gerraty  * If the command starts with '@' and neither the -s nor the -n flag was
896b0c40a00SSimon J. Gerraty  * given to make, stick a shell-specific echoOff command in the script.
89706b9b3e0SSimon J. Gerraty  *
89806b9b3e0SSimon J. Gerraty  * If the command starts with '-' and the shell has no error control (none
899b0c40a00SSimon J. Gerraty  * of the predefined shells has that), ignore errors for the entire job.
90006b9b3e0SSimon J. Gerraty  *
901b0c40a00SSimon J. Gerraty  * XXX: Why ignore errors for the entire job?  This is even documented in the
902b0c40a00SSimon J. Gerraty  * manual page, but without any rationale since there is no known rationale.
903b0c40a00SSimon J. Gerraty  *
904b0c40a00SSimon J. Gerraty  * XXX: The manual page says the '-' "affects the entire job", but that's not
905b0c40a00SSimon J. Gerraty  * accurate.  The '-' does not affect the commands before the '-'.
906b0c40a00SSimon J. Gerraty  *
907b0c40a00SSimon J. Gerraty  * If the command is just "...", skip all further commands of this job.  These
908b0c40a00SSimon J. Gerraty  * commands are attached to the .END node instead and will be run by
909b0c40a00SSimon J. Gerraty  * Job_Finish after all other targets have been made.
91006b9b3e0SSimon J. Gerraty  */
91106b9b3e0SSimon J. Gerraty static void
912b0c40a00SSimon J. Gerraty JobWriteCommand(Job *job, ShellWriter *wr, StringListNode *ln, const char *ucmd)
91306b9b3e0SSimon J. Gerraty {
914b0c40a00SSimon J. Gerraty 	bool run;
91506b9b3e0SSimon J. Gerraty 
91606b9b3e0SSimon J. Gerraty 	CommandFlags cmdFlags;
917b0c40a00SSimon J. Gerraty 	/* Template for writing a command to the shell file */
91806b9b3e0SSimon J. Gerraty 	const char *cmdTemplate;
91906b9b3e0SSimon J. Gerraty 	char *xcmd;		/* The expanded command */
92006b9b3e0SSimon J. Gerraty 	char *xcmdStart;
92106b9b3e0SSimon J. Gerraty 	char *escCmd;		/* xcmd escaped to be used in double quotes */
92206b9b3e0SSimon J. Gerraty 
92306b9b3e0SSimon J. Gerraty 	run = GNode_ShouldExecute(job->node);
92406b9b3e0SSimon J. Gerraty 
9258d5c8e21SSimon J. Gerraty 	xcmd = Var_SubstInTarget(ucmd, job->node);
926956e45f6SSimon J. Gerraty 	/* TODO: handle errors */
92706b9b3e0SSimon J. Gerraty 	xcmdStart = xcmd;
9283955d011SMarcel Moolenaar 
9293955d011SMarcel Moolenaar 	cmdTemplate = "%s\n";
9303955d011SMarcel Moolenaar 
93106b9b3e0SSimon J. Gerraty 	ParseCommandFlags(&xcmd, &cmdFlags);
932956e45f6SSimon J. Gerraty 
93306b9b3e0SSimon J. Gerraty 	/* The '+' command flag overrides the -n or -N options. */
93406b9b3e0SSimon J. Gerraty 	if (cmdFlags.always && !run) {
9353955d011SMarcel Moolenaar 		/*
9363955d011SMarcel Moolenaar 		 * We're not actually executing anything...
9373955d011SMarcel Moolenaar 		 * but this one needs to be - use compat mode just for it.
9383955d011SMarcel Moolenaar 		 */
9399f45a3c8SSimon J. Gerraty 		(void)Compat_RunCommand(ucmd, job->node, ln);
94006b9b3e0SSimon J. Gerraty 		free(xcmdStart);
941956e45f6SSimon J. Gerraty 		return;
9423955d011SMarcel Moolenaar 	}
9433955d011SMarcel Moolenaar 
9443955d011SMarcel Moolenaar 	/*
94506b9b3e0SSimon J. Gerraty 	 * If the shell doesn't have error control, the alternate echoing
94606b9b3e0SSimon J. Gerraty 	 * will be done (to avoid showing additional error checking code)
94706b9b3e0SSimon J. Gerraty 	 * and this needs some characters escaped.
9483955d011SMarcel Moolenaar 	 */
94906b9b3e0SSimon J. Gerraty 	escCmd = shell->hasErrCtl ? NULL : EscapeShellDblQuot(xcmd);
9503955d011SMarcel Moolenaar 
95106b9b3e0SSimon J. Gerraty 	if (!cmdFlags.echo) {
952d5e0a182SSimon J. Gerraty 		if (job->echo && run && shell->hasEchoCtl)
95306b9b3e0SSimon J. Gerraty 			ShellWriter_EchoOff(wr);
954d5e0a182SSimon J. Gerraty 		else if (shell->hasErrCtl)
955b0c40a00SSimon J. Gerraty 			cmdFlags.echo = true;
9563955d011SMarcel Moolenaar 	}
9573955d011SMarcel Moolenaar 
95806b9b3e0SSimon J. Gerraty 	if (cmdFlags.ignerr) {
959b0c40a00SSimon J. Gerraty 		JobWriteSpecials(job, wr, escCmd, run, &cmdFlags, &cmdTemplate);
9603955d011SMarcel Moolenaar 	} else {
9613955d011SMarcel Moolenaar 
9623955d011SMarcel Moolenaar 		/*
96306b9b3e0SSimon J. Gerraty 		 * If errors are being checked and the shell doesn't have
96406b9b3e0SSimon J. Gerraty 		 * error control but does supply an runChkTmpl template, then
96506b9b3e0SSimon J. Gerraty 		 * set up commands to run through it.
9663955d011SMarcel Moolenaar 		 */
9673955d011SMarcel Moolenaar 
96806b9b3e0SSimon J. Gerraty 		if (!shell->hasErrCtl && shell->runChkTmpl != NULL &&
96906b9b3e0SSimon J. Gerraty 		    shell->runChkTmpl[0] != '\0') {
97006b9b3e0SSimon J. Gerraty 			if (job->echo && cmdFlags.echo) {
97106b9b3e0SSimon J. Gerraty 				ShellWriter_EchoOff(wr);
97206b9b3e0SSimon J. Gerraty 				ShellWriter_EchoCmd(wr, escCmd);
973b0c40a00SSimon J. Gerraty 				cmdFlags.echo = false;
9743955d011SMarcel Moolenaar 			}
97506b9b3e0SSimon J. Gerraty 			/*
97606b9b3e0SSimon J. Gerraty 			 * If it's a comment line or blank, avoid the possible
97706b9b3e0SSimon J. Gerraty 			 * syntax error generated by "{\n} || exit $?".
97806b9b3e0SSimon J. Gerraty 			 */
97906b9b3e0SSimon J. Gerraty 			cmdTemplate = escCmd[0] == shell->commentChar ||
98006b9b3e0SSimon J. Gerraty 				      escCmd[0] == '\0'
98106b9b3e0SSimon J. Gerraty 			    ? shell->runIgnTmpl
98206b9b3e0SSimon J. Gerraty 			    : shell->runChkTmpl;
983b0c40a00SSimon J. Gerraty 			cmdFlags.ignerr = false;
9843955d011SMarcel Moolenaar 		}
9853955d011SMarcel Moolenaar 	}
9863955d011SMarcel Moolenaar 
98706b9b3e0SSimon J. Gerraty 	if (DEBUG(SHELL) && strcmp(shellName, "sh") == 0)
98806b9b3e0SSimon J. Gerraty 		ShellWriter_TraceOn(wr);
9893955d011SMarcel Moolenaar 
990b0c40a00SSimon J. Gerraty 	ShellWriter_WriteFmt(wr, cmdTemplate, xcmd);
99106b9b3e0SSimon J. Gerraty 	free(xcmdStart);
9923955d011SMarcel Moolenaar 	free(escCmd);
99306b9b3e0SSimon J. Gerraty 
99406b9b3e0SSimon J. Gerraty 	if (cmdFlags.ignerr)
99506b9b3e0SSimon J. Gerraty 		ShellWriter_ErrOn(wr, cmdFlags.echo && job->echo);
99606b9b3e0SSimon J. Gerraty 
99706b9b3e0SSimon J. Gerraty 	if (!cmdFlags.echo)
99806b9b3e0SSimon J. Gerraty 		ShellWriter_EchoOn(wr);
9993955d011SMarcel Moolenaar }
10003955d011SMarcel Moolenaar 
100106b9b3e0SSimon J. Gerraty /*
1002b0c40a00SSimon J. Gerraty  * Write all commands to the shell file that is later executed.
10033955d011SMarcel Moolenaar  *
1004b0c40a00SSimon J. Gerraty  * The special command "..." stops writing and saves the remaining commands
1005dba7b0efSSimon J. Gerraty  * to be executed later, when the target '.END' is made.
100606b9b3e0SSimon J. Gerraty  *
100706b9b3e0SSimon J. Gerraty  * Return whether at least one command was written to the shell file.
100806b9b3e0SSimon J. Gerraty  */
1009b0c40a00SSimon J. Gerraty static bool
1010b0c40a00SSimon J. Gerraty JobWriteCommands(Job *job)
10113955d011SMarcel Moolenaar {
1012956e45f6SSimon J. Gerraty 	StringListNode *ln;
1013b0c40a00SSimon J. Gerraty 	bool seen = false;
1014b0c40a00SSimon J. Gerraty 	ShellWriter wr;
1015b0c40a00SSimon J. Gerraty 
1016b0c40a00SSimon J. Gerraty 	wr.f = job->cmdFILE;
1017b0c40a00SSimon J. Gerraty 	wr.xtraced = false;
1018956e45f6SSimon J. Gerraty 
101906b9b3e0SSimon J. Gerraty 	for (ln = job->node->commands.first; ln != NULL; ln = ln->next) {
1020956e45f6SSimon J. Gerraty 		const char *cmd = ln->datum;
1021956e45f6SSimon J. Gerraty 
1022956e45f6SSimon J. Gerraty 		if (strcmp(cmd, "...") == 0) {
1023956e45f6SSimon J. Gerraty 			job->node->type |= OP_SAVE_CMDS;
1024956e45f6SSimon J. Gerraty 			job->tailCmds = ln->next;
1025956e45f6SSimon J. Gerraty 			break;
1026956e45f6SSimon J. Gerraty 		}
1027e2eeea75SSimon J. Gerraty 
1028b0c40a00SSimon J. Gerraty 		JobWriteCommand(job, &wr, ln, ln->datum);
1029b0c40a00SSimon J. Gerraty 		seen = true;
1030956e45f6SSimon J. Gerraty 	}
103106b9b3e0SSimon J. Gerraty 
103206b9b3e0SSimon J. Gerraty 	return seen;
1033956e45f6SSimon J. Gerraty }
1034956e45f6SSimon J. Gerraty 
1035dba7b0efSSimon J. Gerraty /*
1036dba7b0efSSimon J. Gerraty  * Save the delayed commands (those after '...'), to be executed later in
1037dba7b0efSSimon J. Gerraty  * the '.END' node, when everything else is done.
1038dba7b0efSSimon J. Gerraty  */
1039956e45f6SSimon J. Gerraty static void
1040956e45f6SSimon J. Gerraty JobSaveCommands(Job *job)
1041956e45f6SSimon J. Gerraty {
104206b9b3e0SSimon J. Gerraty 	StringListNode *ln;
1043956e45f6SSimon J. Gerraty 
104406b9b3e0SSimon J. Gerraty 	for (ln = job->tailCmds; ln != NULL; ln = ln->next) {
104506b9b3e0SSimon J. Gerraty 		const char *cmd = ln->datum;
1046956e45f6SSimon J. Gerraty 		char *expanded_cmd;
1047dba7b0efSSimon J. Gerraty 		/*
1048dba7b0efSSimon J. Gerraty 		 * XXX: This Var_Subst is only intended to expand the dynamic
1049956e45f6SSimon J. Gerraty 		 * variables such as .TARGET, .IMPSRC.  It is not intended to
1050dba7b0efSSimon J. Gerraty 		 * expand the other variables as well; see deptgt-end.mk.
1051dba7b0efSSimon J. Gerraty 		 */
10528d5c8e21SSimon J. Gerraty 		expanded_cmd = Var_SubstInTarget(cmd, job->node);
1053956e45f6SSimon J. Gerraty 		/* TODO: handle errors */
105406b9b3e0SSimon J. Gerraty 		Lst_Append(&Targ_GetEndNode()->commands, expanded_cmd);
10558d5c8e21SSimon J. Gerraty 		Parse_RegisterCommand(expanded_cmd);
1056956e45f6SSimon J. Gerraty 	}
10573955d011SMarcel Moolenaar }
10583955d011SMarcel Moolenaar 
10593955d011SMarcel Moolenaar 
1060956e45f6SSimon J. Gerraty /* Called to close both input and output pipes when a job is finished. */
10613955d011SMarcel Moolenaar static void
1062e2eeea75SSimon J. Gerraty JobClosePipes(Job *job)
10633955d011SMarcel Moolenaar {
10643955d011SMarcel Moolenaar 	clearfd(job);
10653955d011SMarcel Moolenaar 	(void)close(job->outPipe);
10663955d011SMarcel Moolenaar 	job->outPipe = -1;
10673955d011SMarcel Moolenaar 
1068b0c40a00SSimon J. Gerraty 	CollectOutput(job, true);
10693955d011SMarcel Moolenaar 	(void)close(job->inPipe);
10703955d011SMarcel Moolenaar 	job->inPipe = -1;
10713955d011SMarcel Moolenaar }
10723955d011SMarcel Moolenaar 
107306b9b3e0SSimon J. Gerraty static void
1074b0c40a00SSimon J. Gerraty DebugFailedJob(const Job *job)
1075b0c40a00SSimon J. Gerraty {
1076b0c40a00SSimon J. Gerraty 	const StringListNode *ln;
1077b0c40a00SSimon J. Gerraty 
1078b0c40a00SSimon J. Gerraty 	if (!DEBUG(ERROR))
1079b0c40a00SSimon J. Gerraty 		return;
1080b0c40a00SSimon J. Gerraty 
108112904384SSimon J. Gerraty 	debug_printf("\n");
108212904384SSimon J. Gerraty 	debug_printf("*** Failed target: %s\n", job->node->name);
1083548bfc56SSimon J. Gerraty 	debug_printf("*** In directory: %s\n", curdir);
108412904384SSimon J. Gerraty 	debug_printf("*** Failed commands:\n");
108512904384SSimon J. Gerraty 	for (ln = job->node->commands.first; ln != NULL; ln = ln->next) {
108612904384SSimon J. Gerraty 		const char *cmd = ln->datum;
108712904384SSimon J. Gerraty 		debug_printf("\t%s\n", cmd);
108812904384SSimon J. Gerraty 
108912904384SSimon J. Gerraty 		if (strchr(cmd, '$') != NULL) {
10908d5c8e21SSimon J. Gerraty 			char *xcmd = Var_Subst(cmd, job->node, VARE_EVAL);
109112904384SSimon J. Gerraty 			debug_printf("\t=> %s\n", xcmd);
109212904384SSimon J. Gerraty 			free(xcmd);
109312904384SSimon J. Gerraty 		}
109412904384SSimon J. Gerraty 	}
1095b0c40a00SSimon J. Gerraty }
1096b0c40a00SSimon J. Gerraty 
1097b0c40a00SSimon J. Gerraty static void
109806b9b3e0SSimon J. Gerraty JobFinishDoneExitedError(Job *job, WAIT_T *inout_status)
109906b9b3e0SSimon J. Gerraty {
110006b9b3e0SSimon J. Gerraty 	SwitchOutputTo(job->node);
110106b9b3e0SSimon J. Gerraty #ifdef USE_META
110206b9b3e0SSimon J. Gerraty 	if (useMeta) {
110306b9b3e0SSimon J. Gerraty 		meta_job_error(job, job->node,
110406b9b3e0SSimon J. Gerraty 		    job->ignerr, WEXITSTATUS(*inout_status));
110506b9b3e0SSimon J. Gerraty 	}
110606b9b3e0SSimon J. Gerraty #endif
110706b9b3e0SSimon J. Gerraty 	if (!shouldDieQuietly(job->node, -1)) {
1108b0c40a00SSimon J. Gerraty 		DebugFailedJob(job);
110906b9b3e0SSimon J. Gerraty 		(void)printf("*** [%s] Error code %d%s\n",
111006b9b3e0SSimon J. Gerraty 		    job->node->name, WEXITSTATUS(*inout_status),
111106b9b3e0SSimon J. Gerraty 		    job->ignerr ? " (ignored)" : "");
111206b9b3e0SSimon J. Gerraty 	}
111306b9b3e0SSimon J. Gerraty 
111406b9b3e0SSimon J. Gerraty 	if (job->ignerr)
111506b9b3e0SSimon J. Gerraty 		WAIT_STATUS(*inout_status) = 0;
111606b9b3e0SSimon J. Gerraty 	else {
111706b9b3e0SSimon J. Gerraty 		if (deleteOnError)
111806b9b3e0SSimon J. Gerraty 			JobDeleteTarget(job->node);
11199f45a3c8SSimon J. Gerraty 		PrintOnError(job->node, "\n");
112006b9b3e0SSimon J. Gerraty 	}
112106b9b3e0SSimon J. Gerraty }
112206b9b3e0SSimon J. Gerraty 
112306b9b3e0SSimon J. Gerraty static void
112406b9b3e0SSimon J. Gerraty JobFinishDoneExited(Job *job, WAIT_T *inout_status)
112506b9b3e0SSimon J. Gerraty {
112606b9b3e0SSimon J. Gerraty 	DEBUG2(JOB, "Process %d [%s] exited.\n", job->pid, job->node->name);
112706b9b3e0SSimon J. Gerraty 
112806b9b3e0SSimon J. Gerraty 	if (WEXITSTATUS(*inout_status) != 0)
112906b9b3e0SSimon J. Gerraty 		JobFinishDoneExitedError(job, inout_status);
113006b9b3e0SSimon J. Gerraty 	else if (DEBUG(JOB)) {
113106b9b3e0SSimon J. Gerraty 		SwitchOutputTo(job->node);
113206b9b3e0SSimon J. Gerraty 		(void)printf("*** [%s] Completed successfully\n",
113306b9b3e0SSimon J. Gerraty 		    job->node->name);
113406b9b3e0SSimon J. Gerraty 	}
113506b9b3e0SSimon J. Gerraty }
113606b9b3e0SSimon J. Gerraty 
113706b9b3e0SSimon J. Gerraty static void
113806b9b3e0SSimon J. Gerraty JobFinishDoneSignaled(Job *job, WAIT_T status)
113906b9b3e0SSimon J. Gerraty {
114006b9b3e0SSimon J. Gerraty 	SwitchOutputTo(job->node);
1141b0c40a00SSimon J. Gerraty 	DebugFailedJob(job);
114206b9b3e0SSimon J. Gerraty 	(void)printf("*** [%s] Signal %d\n", job->node->name, WTERMSIG(status));
114306b9b3e0SSimon J. Gerraty 	if (deleteOnError)
114406b9b3e0SSimon J. Gerraty 		JobDeleteTarget(job->node);
114506b9b3e0SSimon J. Gerraty }
114606b9b3e0SSimon J. Gerraty 
114706b9b3e0SSimon J. Gerraty static void
114806b9b3e0SSimon J. Gerraty JobFinishDone(Job *job, WAIT_T *inout_status)
114906b9b3e0SSimon J. Gerraty {
115006b9b3e0SSimon J. Gerraty 	if (WIFEXITED(*inout_status))
115106b9b3e0SSimon J. Gerraty 		JobFinishDoneExited(job, inout_status);
115206b9b3e0SSimon J. Gerraty 	else
115306b9b3e0SSimon J. Gerraty 		JobFinishDoneSignaled(job, *inout_status);
115406b9b3e0SSimon J. Gerraty 
115506b9b3e0SSimon J. Gerraty 	(void)fflush(stdout);
115606b9b3e0SSimon J. Gerraty }
115706b9b3e0SSimon J. Gerraty 
115806b9b3e0SSimon J. Gerraty /*
115906b9b3e0SSimon J. Gerraty  * Do final processing for the given job including updating parent nodes and
1160e2eeea75SSimon J. Gerraty  * starting new jobs as available/necessary.
1161e2eeea75SSimon J. Gerraty  *
1162e2eeea75SSimon J. Gerraty  * Deferred commands for the job are placed on the .END node.
1163e2eeea75SSimon J. Gerraty  *
116406b9b3e0SSimon J. Gerraty  * If there was a serious error (job_errors != 0; not an ignored one), no more
1165e2eeea75SSimon J. Gerraty  * jobs will be started.
11663955d011SMarcel Moolenaar  *
11673955d011SMarcel Moolenaar  * Input:
11683955d011SMarcel Moolenaar  *	job		job to finish
11693955d011SMarcel Moolenaar  *	status		sub-why job went away
11703955d011SMarcel Moolenaar  */
11713955d011SMarcel Moolenaar static void
11723955d011SMarcel Moolenaar JobFinish (Job *job, WAIT_T status)
11733955d011SMarcel Moolenaar {
1174b0c40a00SSimon J. Gerraty 	bool done, return_job_token;
11753955d011SMarcel Moolenaar 
1176956e45f6SSimon J. Gerraty 	DEBUG3(JOB, "JobFinish: %d [%s], status %d\n",
11773955d011SMarcel Moolenaar 	    job->pid, job->node->name, status);
11783955d011SMarcel Moolenaar 
11793955d011SMarcel Moolenaar 	if ((WIFEXITED(status) &&
118006b9b3e0SSimon J. Gerraty 	     ((WEXITSTATUS(status) != 0 && !job->ignerr))) ||
118106b9b3e0SSimon J. Gerraty 	    WIFSIGNALED(status)) {
118206b9b3e0SSimon J. Gerraty 		/* Finished because of an error. */
118306b9b3e0SSimon J. Gerraty 
1184e2eeea75SSimon J. Gerraty 		JobClosePipes(job);
11853955d011SMarcel Moolenaar 		if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
118612904384SSimon J. Gerraty 			if (fclose(job->cmdFILE) != 0)
118712904384SSimon J. Gerraty 				Punt("Cannot write shell script for '%s': %s",
118812904384SSimon J. Gerraty 				    job->node->name, strerror(errno));
11893955d011SMarcel Moolenaar 			job->cmdFILE = NULL;
11903955d011SMarcel Moolenaar 		}
1191b0c40a00SSimon J. Gerraty 		done = true;
119206b9b3e0SSimon J. Gerraty 
11933955d011SMarcel Moolenaar 	} else if (WIFEXITED(status)) {
11943955d011SMarcel Moolenaar 		/*
119506b9b3e0SSimon J. Gerraty 		 * Deal with ignored errors in -B mode. We need to print a
119606b9b3e0SSimon J. Gerraty 		 * message telling of the ignored error as well as to run
119706b9b3e0SSimon J. Gerraty 		 * the next command.
11983955d011SMarcel Moolenaar 		 */
11993955d011SMarcel Moolenaar 		done = WEXITSTATUS(status) != 0;
120006b9b3e0SSimon J. Gerraty 
1201e2eeea75SSimon J. Gerraty 		JobClosePipes(job);
120206b9b3e0SSimon J. Gerraty 
12033955d011SMarcel Moolenaar 	} else {
120406b9b3e0SSimon J. Gerraty 		/* No need to close things down or anything. */
1205b0c40a00SSimon J. Gerraty 		done = false;
12063955d011SMarcel Moolenaar 	}
12073955d011SMarcel Moolenaar 
120806b9b3e0SSimon J. Gerraty 	if (done)
120906b9b3e0SSimon J. Gerraty 		JobFinishDone(job, &status);
12103955d011SMarcel Moolenaar 
12113955d011SMarcel Moolenaar #ifdef USE_META
12123955d011SMarcel Moolenaar 	if (useMeta) {
1213e2eeea75SSimon J. Gerraty 		int meta_status = meta_job_finish(job);
1214e2eeea75SSimon J. Gerraty 		if (meta_status != 0 && status == 0)
1215e2eeea75SSimon J. Gerraty 			status = meta_status;
12163955d011SMarcel Moolenaar 	}
12173955d011SMarcel Moolenaar #endif
12183955d011SMarcel Moolenaar 
1219b0c40a00SSimon J. Gerraty 	return_job_token = false;
12203955d011SMarcel Moolenaar 
12213955d011SMarcel Moolenaar 	Trace_Log(JOBEND, job);
122206b9b3e0SSimon J. Gerraty 	if (!job->special) {
1223e2eeea75SSimon J. Gerraty 		if (WAIT_STATUS(status) != 0 ||
1224e2eeea75SSimon J. Gerraty 		    (aborting == ABORT_ERROR) || aborting == ABORT_INTERRUPT)
1225b0c40a00SSimon J. Gerraty 			return_job_token = true;
12263955d011SMarcel Moolenaar 	}
12273955d011SMarcel Moolenaar 
1228e2eeea75SSimon J. Gerraty 	if (aborting != ABORT_ERROR && aborting != ABORT_INTERRUPT &&
12293955d011SMarcel Moolenaar 	    (WAIT_STATUS(status) == 0)) {
12303955d011SMarcel Moolenaar 		/*
123106b9b3e0SSimon J. Gerraty 		 * As long as we aren't aborting and the job didn't return a
123206b9b3e0SSimon J. Gerraty 		 * non-zero status that we shouldn't ignore, we call
123306b9b3e0SSimon J. Gerraty 		 * Make_Update to update the parents.
12343955d011SMarcel Moolenaar 		 */
1235956e45f6SSimon J. Gerraty 		JobSaveCommands(job);
12363955d011SMarcel Moolenaar 		job->node->made = MADE;
123706b9b3e0SSimon J. Gerraty 		if (!job->special)
1238b0c40a00SSimon J. Gerraty 			return_job_token = true;
12393955d011SMarcel Moolenaar 		Make_Update(job->node);
1240e2eeea75SSimon J. Gerraty 		job->status = JOB_ST_FREE;
124106b9b3e0SSimon J. Gerraty 	} else if (status != 0) {
124206b9b3e0SSimon J. Gerraty 		job_errors++;
1243e2eeea75SSimon J. Gerraty 		job->status = JOB_ST_FREE;
12443955d011SMarcel Moolenaar 	}
12453955d011SMarcel Moolenaar 
124606b9b3e0SSimon J. Gerraty 	if (job_errors > 0 && !opts.keepgoing && aborting != ABORT_INTERRUPT) {
124706b9b3e0SSimon J. Gerraty 		/* Prevent more jobs from getting started. */
124806b9b3e0SSimon J. Gerraty 		aborting = ABORT_ERROR;
124906b9b3e0SSimon J. Gerraty 	}
12503955d011SMarcel Moolenaar 
12513955d011SMarcel Moolenaar 	if (return_job_token)
12523955d011SMarcel Moolenaar 		Job_TokenReturn();
12533955d011SMarcel Moolenaar 
1254e2eeea75SSimon J. Gerraty 	if (aborting == ABORT_ERROR && jobTokensRunning == 0)
125506b9b3e0SSimon J. Gerraty 		Finish(job_errors);
12563955d011SMarcel Moolenaar }
1257e2eeea75SSimon J. Gerraty 
1258e2eeea75SSimon J. Gerraty static void
1259e2eeea75SSimon J. Gerraty TouchRegular(GNode *gn)
1260e2eeea75SSimon J. Gerraty {
1261e2eeea75SSimon J. Gerraty 	const char *file = GNode_Path(gn);
1262b0c40a00SSimon J. Gerraty 	struct utimbuf times;
1263e2eeea75SSimon J. Gerraty 	int fd;
1264e2eeea75SSimon J. Gerraty 	char c;
1265e2eeea75SSimon J. Gerraty 
1266b0c40a00SSimon J. Gerraty 	times.actime = now;
1267b0c40a00SSimon J. Gerraty 	times.modtime = now;
1268e2eeea75SSimon J. Gerraty 	if (utime(file, &times) >= 0)
1269e2eeea75SSimon J. Gerraty 		return;
1270e2eeea75SSimon J. Gerraty 
1271e2eeea75SSimon J. Gerraty 	fd = open(file, O_RDWR | O_CREAT, 0666);
1272e2eeea75SSimon J. Gerraty 	if (fd < 0) {
1273e2eeea75SSimon J. Gerraty 		(void)fprintf(stderr, "*** couldn't touch %s: %s\n",
1274e2eeea75SSimon J. Gerraty 		    file, strerror(errno));
1275e2eeea75SSimon J. Gerraty 		(void)fflush(stderr);
1276e2eeea75SSimon J. Gerraty 		return;		/* XXX: What about propagating the error? */
1277e2eeea75SSimon J. Gerraty 	}
1278e2eeea75SSimon J. Gerraty 
12799f45a3c8SSimon J. Gerraty 	/*
12809f45a3c8SSimon J. Gerraty 	 * Last resort: update the file's time stamps in the traditional way.
1281e2eeea75SSimon J. Gerraty 	 * XXX: This doesn't work for empty files, which are sometimes used
12829f45a3c8SSimon J. Gerraty 	 * as marker files.
12839f45a3c8SSimon J. Gerraty 	 */
1284e2eeea75SSimon J. Gerraty 	if (read(fd, &c, 1) == 1) {
1285e2eeea75SSimon J. Gerraty 		(void)lseek(fd, 0, SEEK_SET);
1286e2eeea75SSimon J. Gerraty 		while (write(fd, &c, 1) == -1 && errno == EAGAIN)
1287e2eeea75SSimon J. Gerraty 			continue;
1288e2eeea75SSimon J. Gerraty 	}
1289e2eeea75SSimon J. Gerraty 	(void)close(fd);	/* XXX: What about propagating the error? */
12903955d011SMarcel Moolenaar }
12913955d011SMarcel Moolenaar 
129206b9b3e0SSimon J. Gerraty /*
1293*6a7405f5SSimon J. Gerraty  * Touch the given target. Called by Job_Make when the -t flag was given.
12943955d011SMarcel Moolenaar  *
1295956e45f6SSimon J. Gerraty  * The modification date of the file is changed.
129606b9b3e0SSimon J. Gerraty  * If the file did not exist, it is created.
129706b9b3e0SSimon J. Gerraty  */
12983955d011SMarcel Moolenaar void
1299b0c40a00SSimon J. Gerraty Job_Touch(GNode *gn, bool echo)
13003955d011SMarcel Moolenaar {
130106b9b3e0SSimon J. Gerraty 	if (gn->type &
130206b9b3e0SSimon J. Gerraty 	    (OP_JOIN | OP_USE | OP_USEBEFORE | OP_EXEC | OP_OPTIONAL |
13033955d011SMarcel Moolenaar 	     OP_SPECIAL | OP_PHONY)) {
130406b9b3e0SSimon J. Gerraty 		/*
130506b9b3e0SSimon J. Gerraty 		 * These are "virtual" targets and should not really be
130606b9b3e0SSimon J. Gerraty 		 * created.
130706b9b3e0SSimon J. Gerraty 		 */
13083955d011SMarcel Moolenaar 		return;
13093955d011SMarcel Moolenaar 	}
13103955d011SMarcel Moolenaar 
131106b9b3e0SSimon J. Gerraty 	if (echo || !GNode_ShouldExecute(gn)) {
13123955d011SMarcel Moolenaar 		(void)fprintf(stdout, "touch %s\n", gn->name);
13133955d011SMarcel Moolenaar 		(void)fflush(stdout);
13143955d011SMarcel Moolenaar 	}
13153955d011SMarcel Moolenaar 
1316e2eeea75SSimon J. Gerraty 	if (!GNode_ShouldExecute(gn))
13173955d011SMarcel Moolenaar 		return;
13183955d011SMarcel Moolenaar 
131906b9b3e0SSimon J. Gerraty 	if (gn->type & OP_ARCHV)
13203955d011SMarcel Moolenaar 		Arch_Touch(gn);
132106b9b3e0SSimon J. Gerraty 	else if (gn->type & OP_LIB)
13223955d011SMarcel Moolenaar 		Arch_TouchLib(gn);
132306b9b3e0SSimon J. Gerraty 	else
1324e2eeea75SSimon J. Gerraty 		TouchRegular(gn);
13253955d011SMarcel Moolenaar }
13263955d011SMarcel Moolenaar 
132706b9b3e0SSimon J. Gerraty /*
132806b9b3e0SSimon J. Gerraty  * Make sure the given node has all the commands it needs.
1329956e45f6SSimon J. Gerraty  *
1330956e45f6SSimon J. Gerraty  * The node will have commands from the .DEFAULT rule added to it if it
1331956e45f6SSimon J. Gerraty  * needs them.
13323955d011SMarcel Moolenaar  *
13333955d011SMarcel Moolenaar  * Input:
13343955d011SMarcel Moolenaar  *	gn		The target whose commands need verifying
13353955d011SMarcel Moolenaar  *	abortProc	Function to abort with message
13363955d011SMarcel Moolenaar  *
13373955d011SMarcel Moolenaar  * Results:
1338b0c40a00SSimon J. Gerraty  *	true if the commands list is/was ok.
13393955d011SMarcel Moolenaar  */
1340b0c40a00SSimon J. Gerraty bool
13413955d011SMarcel Moolenaar Job_CheckCommands(GNode *gn, void (*abortProc)(const char *, ...))
13423955d011SMarcel Moolenaar {
1343956e45f6SSimon J. Gerraty 	if (GNode_IsTarget(gn))
1344b0c40a00SSimon J. Gerraty 		return true;
134506b9b3e0SSimon J. Gerraty 	if (!Lst_IsEmpty(&gn->commands))
1346b0c40a00SSimon J. Gerraty 		return true;
134706b9b3e0SSimon J. Gerraty 	if ((gn->type & OP_LIB) && !Lst_IsEmpty(&gn->children))
1348b0c40a00SSimon J. Gerraty 		return true;
1349956e45f6SSimon J. Gerraty 
13503955d011SMarcel Moolenaar 	/*
13513955d011SMarcel Moolenaar 	 * No commands. Look for .DEFAULT rule from which we might infer
1352e2eeea75SSimon J. Gerraty 	 * commands.
13533955d011SMarcel Moolenaar 	 */
135406b9b3e0SSimon J. Gerraty 	if (defaultNode != NULL && !Lst_IsEmpty(&defaultNode->commands) &&
1355e2eeea75SSimon J. Gerraty 	    !(gn->type & OP_SPECIAL)) {
13563955d011SMarcel Moolenaar 		/*
135706b9b3e0SSimon J. Gerraty 		 * The traditional Make only looks for a .DEFAULT if the node
135806b9b3e0SSimon J. Gerraty 		 * was never the target of an operator, so that's what we do
135906b9b3e0SSimon J. Gerraty 		 * too.
1360e2eeea75SSimon J. Gerraty 		 *
1361e2eeea75SSimon J. Gerraty 		 * The .DEFAULT node acts like a transformation rule, in that
13623955d011SMarcel Moolenaar 		 * gn also inherits any attributes or sources attached to
13633955d011SMarcel Moolenaar 		 * .DEFAULT itself.
13643955d011SMarcel Moolenaar 		 */
1365e2eeea75SSimon J. Gerraty 		Make_HandleUse(defaultNode, gn);
1366dba7b0efSSimon J. Gerraty 		Var_Set(gn, IMPSRC, GNode_VarTarget(gn));
1367b0c40a00SSimon J. Gerraty 		return true;
1368956e45f6SSimon J. Gerraty 	}
1369956e45f6SSimon J. Gerraty 
1370b0c40a00SSimon J. Gerraty 	Dir_UpdateMTime(gn, false);
1371e2eeea75SSimon J. Gerraty 	if (gn->mtime != 0 || (gn->type & OP_SPECIAL))
1372b0c40a00SSimon J. Gerraty 		return true;
1373956e45f6SSimon J. Gerraty 
13743955d011SMarcel Moolenaar 	/*
1375956e45f6SSimon J. Gerraty 	 * The node wasn't the target of an operator.  We have no .DEFAULT
13763955d011SMarcel Moolenaar 	 * rule to go on and the target doesn't already exist. There's
13773955d011SMarcel Moolenaar 	 * nothing more we can do for this branch. If the -k flag wasn't
13783955d011SMarcel Moolenaar 	 * given, we stop in our tracks, otherwise we just don't update
13793955d011SMarcel Moolenaar 	 * this node's parents so they never get examined.
13803955d011SMarcel Moolenaar 	 */
13813955d011SMarcel Moolenaar 
138212904384SSimon J. Gerraty 	if (gn->flags.fromDepend) {
13831748de26SSimon J. Gerraty 		if (!Job_RunTarget(".STALE", gn->fname))
138406b9b3e0SSimon J. Gerraty 			fprintf(stdout,
13859f45a3c8SSimon J. Gerraty 			    "%s: %s, %u: ignoring stale %s for %s\n",
13861748de26SSimon J. Gerraty 			    progname, gn->fname, gn->lineno, makeDependfile,
13871748de26SSimon J. Gerraty 			    gn->name);
1388b0c40a00SSimon J. Gerraty 		return true;
13893955d011SMarcel Moolenaar 	}
13903955d011SMarcel Moolenaar 
13913955d011SMarcel Moolenaar 	if (gn->type & OP_OPTIONAL) {
1392956e45f6SSimon J. Gerraty 		(void)fprintf(stdout, "%s: don't know how to make %s (%s)\n",
1393956e45f6SSimon J. Gerraty 		    progname, gn->name, "ignored");
13943955d011SMarcel Moolenaar 		(void)fflush(stdout);
1395b0c40a00SSimon J. Gerraty 		return true;
13963955d011SMarcel Moolenaar 	}
13973955d011SMarcel Moolenaar 
1398956e45f6SSimon J. Gerraty 	if (opts.keepgoing) {
1399956e45f6SSimon J. Gerraty 		(void)fprintf(stdout, "%s: don't know how to make %s (%s)\n",
1400956e45f6SSimon J. Gerraty 		    progname, gn->name, "continuing");
1401956e45f6SSimon J. Gerraty 		(void)fflush(stdout);
1402b0c40a00SSimon J. Gerraty 		return false;
1403956e45f6SSimon J. Gerraty 	}
1404956e45f6SSimon J. Gerraty 
1405d5e0a182SSimon J. Gerraty 	abortProc("don't know how to make %s. Stop", gn->name);
1406b0c40a00SSimon J. Gerraty 	return false;
1407956e45f6SSimon J. Gerraty }
1408956e45f6SSimon J. Gerraty 
140906b9b3e0SSimon J. Gerraty /*
141006b9b3e0SSimon J. Gerraty  * Execute the shell for the given job.
14113955d011SMarcel Moolenaar  *
141206b9b3e0SSimon J. Gerraty  * See Job_CatchOutput for handling the output of the shell.
141306b9b3e0SSimon J. Gerraty  */
14143955d011SMarcel Moolenaar static void
14153955d011SMarcel Moolenaar JobExec(Job *job, char **argv)
14163955d011SMarcel Moolenaar {
14173955d011SMarcel Moolenaar 	int cpid;		/* ID of new child */
14183955d011SMarcel Moolenaar 	sigset_t mask;
14193955d011SMarcel Moolenaar 
14203955d011SMarcel Moolenaar 	if (DEBUG(JOB)) {
14213955d011SMarcel Moolenaar 		int i;
14223955d011SMarcel Moolenaar 
1423e2eeea75SSimon J. Gerraty 		debug_printf("Running %s\n", job->node->name);
1424956e45f6SSimon J. Gerraty 		debug_printf("\tCommand: ");
14253955d011SMarcel Moolenaar 		for (i = 0; argv[i] != NULL; i++) {
1426956e45f6SSimon J. Gerraty 			debug_printf("%s ", argv[i]);
14273955d011SMarcel Moolenaar 		}
1428956e45f6SSimon J. Gerraty 		debug_printf("\n");
14293955d011SMarcel Moolenaar 	}
14303955d011SMarcel Moolenaar 
14313955d011SMarcel Moolenaar 	/*
1432d5e0a182SSimon J. Gerraty 	 * Some jobs produce no output, and it's disconcerting to have
14333955d011SMarcel Moolenaar 	 * no feedback of their running (since they produce no output, the
14343955d011SMarcel Moolenaar 	 * banner with their name in it never appears). This is an attempt to
14353955d011SMarcel Moolenaar 	 * provide that feedback, even if nothing follows it.
14363955d011SMarcel Moolenaar 	 */
143706b9b3e0SSimon J. Gerraty 	if (job->echo)
143806b9b3e0SSimon J. Gerraty 		SwitchOutputTo(job->node);
14393955d011SMarcel Moolenaar 
14403955d011SMarcel Moolenaar 	/* No interruptions until this job is on the `jobs' list */
14413955d011SMarcel Moolenaar 	JobSigLock(&mask);
14423955d011SMarcel Moolenaar 
14433955d011SMarcel Moolenaar 	/* Pre-emptively mark job running, pid still zero though */
1444e2eeea75SSimon J. Gerraty 	job->status = JOB_ST_RUNNING;
14453955d011SMarcel Moolenaar 
1446c59c3bf3SSimon J. Gerraty 	Var_ReexportVars(job->node);
144706b9b3e0SSimon J. Gerraty 
1448*6a7405f5SSimon J. Gerraty 	cpid = FORK_FUNCTION();
14493955d011SMarcel Moolenaar 	if (cpid == -1)
1450*6a7405f5SSimon J. Gerraty 		Punt("Cannot fork: %s", strerror(errno));
14513955d011SMarcel Moolenaar 
14523955d011SMarcel Moolenaar 	if (cpid == 0) {
14533955d011SMarcel Moolenaar 		/* Child */
14543955d011SMarcel Moolenaar 		sigset_t tmask;
14553955d011SMarcel Moolenaar 
14563955d011SMarcel Moolenaar #ifdef USE_META
14579f45a3c8SSimon J. Gerraty 		if (useMeta)
14583955d011SMarcel Moolenaar 			meta_job_child(job);
14593955d011SMarcel Moolenaar #endif
14603955d011SMarcel Moolenaar 		/*
146106b9b3e0SSimon J. Gerraty 		 * Reset all signal handlers; this is necessary because we
146206b9b3e0SSimon J. Gerraty 		 * also need to unblock signals before we exec(2).
14633955d011SMarcel Moolenaar 		 */
14643955d011SMarcel Moolenaar 		JobSigReset();
14653955d011SMarcel Moolenaar 
14663955d011SMarcel Moolenaar 		/* Now unblock signals */
14673955d011SMarcel Moolenaar 		sigemptyset(&tmask);
14683955d011SMarcel Moolenaar 		JobSigUnlock(&tmask);
14693955d011SMarcel Moolenaar 
14703955d011SMarcel Moolenaar 		/*
147106b9b3e0SSimon J. Gerraty 		 * Must duplicate the input stream down to the child's input
147206b9b3e0SSimon J. Gerraty 		 * and reset it to the beginning (again). Since the stream
147306b9b3e0SSimon J. Gerraty 		 * was marked close-on-exec, we must clear that bit in the
147406b9b3e0SSimon J. Gerraty 		 * new input.
14753955d011SMarcel Moolenaar 		 */
14768d5c8e21SSimon J. Gerraty 		if (dup2(fileno(job->cmdFILE), STDIN_FILENO) == -1)
1477956e45f6SSimon J. Gerraty 			execDie("dup2", "job->cmdFILE");
14788d5c8e21SSimon J. Gerraty 		if (fcntl(STDIN_FILENO, F_SETFD, 0) == -1)
1479956e45f6SSimon J. Gerraty 			execDie("fcntl clear close-on-exec", "stdin");
14808d5c8e21SSimon J. Gerraty 		if (lseek(STDIN_FILENO, 0, SEEK_SET) == -1)
1481956e45f6SSimon J. Gerraty 			execDie("lseek to 0", "stdin");
14823955d011SMarcel Moolenaar 
1483db29cad8SSimon J. Gerraty 		if (Always_pass_job_queue ||
1484db29cad8SSimon J. Gerraty 		    (job->node->type & (OP_MAKE | OP_SUBMAKE))) {
1485d5e0a182SSimon J. Gerraty 			/* Pass job token pipe to submakes. */
1486956e45f6SSimon J. Gerraty 			if (fcntl(tokenWaitJob.inPipe, F_SETFD, 0) == -1)
148706b9b3e0SSimon J. Gerraty 				execDie("clear close-on-exec",
148806b9b3e0SSimon J. Gerraty 				    "tokenWaitJob.inPipe");
1489956e45f6SSimon J. Gerraty 			if (fcntl(tokenWaitJob.outPipe, F_SETFD, 0) == -1)
149006b9b3e0SSimon J. Gerraty 				execDie("clear close-on-exec",
149106b9b3e0SSimon J. Gerraty 				    "tokenWaitJob.outPipe");
14923955d011SMarcel Moolenaar 		}
14933955d011SMarcel Moolenaar 
14943955d011SMarcel Moolenaar 		/*
14953955d011SMarcel Moolenaar 		 * Set up the child's output to be routed through the pipe
14963955d011SMarcel Moolenaar 		 * we've created for it.
14973955d011SMarcel Moolenaar 		 */
14988d5c8e21SSimon J. Gerraty 		if (dup2(job->outPipe, STDOUT_FILENO) == -1)
1499956e45f6SSimon J. Gerraty 			execDie("dup2", "job->outPipe");
1500956e45f6SSimon J. Gerraty 
15013955d011SMarcel Moolenaar 		/*
150206b9b3e0SSimon J. Gerraty 		 * The output channels are marked close on exec. This bit
15038d5c8e21SSimon J. Gerraty 		 * was duplicated by dup2 (on some systems), so we have
150406b9b3e0SSimon J. Gerraty 		 * to clear it before routing the shell's error output to
150506b9b3e0SSimon J. Gerraty 		 * the same place as its standard output.
15063955d011SMarcel Moolenaar 		 */
15078d5c8e21SSimon J. Gerraty 		if (fcntl(STDOUT_FILENO, F_SETFD, 0) == -1)
1508956e45f6SSimon J. Gerraty 			execDie("clear close-on-exec", "stdout");
15098d5c8e21SSimon J. Gerraty 		if (dup2(STDOUT_FILENO, STDERR_FILENO) == -1)
1510956e45f6SSimon J. Gerraty 			execDie("dup2", "1, 2");
15113955d011SMarcel Moolenaar 
15123955d011SMarcel Moolenaar 		/*
151306b9b3e0SSimon J. Gerraty 		 * We want to switch the child into a different process
151406b9b3e0SSimon J. Gerraty 		 * family so we can kill it and all its descendants in
151506b9b3e0SSimon J. Gerraty 		 * one fell swoop, by killing its process family, but not
151606b9b3e0SSimon J. Gerraty 		 * commit suicide.
15173955d011SMarcel Moolenaar 		 */
15183955d011SMarcel Moolenaar #if defined(HAVE_SETPGID)
15193955d011SMarcel Moolenaar 		(void)setpgid(0, getpid());
15203955d011SMarcel Moolenaar #else
15213955d011SMarcel Moolenaar # if defined(HAVE_SETSID)
15223955d011SMarcel Moolenaar 		/* XXX: dsl - I'm sure this should be setpgrp()... */
15233955d011SMarcel Moolenaar 		(void)setsid();
15243955d011SMarcel Moolenaar # else
15253955d011SMarcel Moolenaar 		(void)setpgrp(0, getpid());
15263955d011SMarcel Moolenaar # endif
15273955d011SMarcel Moolenaar #endif
15283955d011SMarcel Moolenaar 
15293955d011SMarcel Moolenaar 		(void)execv(shellPath, argv);
1530956e45f6SSimon J. Gerraty 		execDie("exec", shellPath);
15313955d011SMarcel Moolenaar 	}
15323955d011SMarcel Moolenaar 
15333955d011SMarcel Moolenaar 	/* Parent, continuing after the child exec */
15343955d011SMarcel Moolenaar 	job->pid = cpid;
15353955d011SMarcel Moolenaar 
15363955d011SMarcel Moolenaar 	Trace_Log(JOBSTART, job);
15373955d011SMarcel Moolenaar 
153849caa483SSimon J. Gerraty #ifdef USE_META
15399f45a3c8SSimon J. Gerraty 	if (useMeta)
154049caa483SSimon J. Gerraty 		meta_job_parent(job, cpid);
154149caa483SSimon J. Gerraty #endif
154249caa483SSimon J. Gerraty 
15433955d011SMarcel Moolenaar 	/*
15443955d011SMarcel Moolenaar 	 * Set the current position in the buffer to the beginning
15453955d011SMarcel Moolenaar 	 * and mark another stream to watch in the outputs mask
15463955d011SMarcel Moolenaar 	 */
15473955d011SMarcel Moolenaar 	job->curPos = 0;
15483955d011SMarcel Moolenaar 
15493955d011SMarcel Moolenaar 	watchfd(job);
15503955d011SMarcel Moolenaar 
15513955d011SMarcel Moolenaar 	if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
155212904384SSimon J. Gerraty 		if (fclose(job->cmdFILE) != 0)
155312904384SSimon J. Gerraty 			Punt("Cannot write shell script for '%s': %s",
155412904384SSimon J. Gerraty 			    job->node->name, strerror(errno));
15553955d011SMarcel Moolenaar 		job->cmdFILE = NULL;
15563955d011SMarcel Moolenaar 	}
15573955d011SMarcel Moolenaar 
1558dba7b0efSSimon J. Gerraty 	/* Now that the job is actually running, add it to the table. */
15593955d011SMarcel Moolenaar 	if (DEBUG(JOB)) {
1560956e45f6SSimon J. Gerraty 		debug_printf("JobExec(%s): pid %d added to jobs table\n",
15613955d011SMarcel Moolenaar 		    job->node->name, job->pid);
1562b0c40a00SSimon J. Gerraty 		DumpJobs("job started");
15633955d011SMarcel Moolenaar 	}
15643955d011SMarcel Moolenaar 	JobSigUnlock(&mask);
15653955d011SMarcel Moolenaar }
15663955d011SMarcel Moolenaar 
1567956e45f6SSimon J. Gerraty /* Create the argv needed to execute the shell for a given job. */
15683955d011SMarcel Moolenaar static void
15693955d011SMarcel Moolenaar JobMakeArgv(Job *job, char **argv)
15703955d011SMarcel Moolenaar {
15713955d011SMarcel Moolenaar 	int argc;
15723955d011SMarcel Moolenaar 	static char args[10];	/* For merged arguments */
15733955d011SMarcel Moolenaar 
15743955d011SMarcel Moolenaar 	argv[0] = UNCONST(shellName);
15753955d011SMarcel Moolenaar 	argc = 1;
15763955d011SMarcel Moolenaar 
157706b9b3e0SSimon J. Gerraty 	if ((shell->errFlag != NULL && shell->errFlag[0] != '-') ||
157806b9b3e0SSimon J. Gerraty 	    (shell->echoFlag != NULL && shell->echoFlag[0] != '-')) {
15793955d011SMarcel Moolenaar 		/*
158006b9b3e0SSimon J. Gerraty 		 * At least one of the flags doesn't have a minus before it,
158106b9b3e0SSimon J. Gerraty 		 * so merge them together. Have to do this because the Bourne
158206b9b3e0SSimon J. Gerraty 		 * shell thinks its second argument is a file to source.
158306b9b3e0SSimon J. Gerraty 		 * Grrrr. Note the ten-character limitation on the combined
158406b9b3e0SSimon J. Gerraty 		 * arguments.
158506b9b3e0SSimon J. Gerraty 		 *
158606b9b3e0SSimon J. Gerraty 		 * TODO: Research until when the above comments were
158706b9b3e0SSimon J. Gerraty 		 * practically relevant.
15883955d011SMarcel Moolenaar 		 */
1589e2eeea75SSimon J. Gerraty 		(void)snprintf(args, sizeof args, "-%s%s",
159006b9b3e0SSimon J. Gerraty 		    (job->ignerr ? "" :
159106b9b3e0SSimon J. Gerraty 			(shell->errFlag != NULL ? shell->errFlag : "")),
159206b9b3e0SSimon J. Gerraty 		    (!job->echo ? "" :
159306b9b3e0SSimon J. Gerraty 			(shell->echoFlag != NULL ? shell->echoFlag : "")));
15943955d011SMarcel Moolenaar 
159506b9b3e0SSimon J. Gerraty 		if (args[1] != '\0') {
15963955d011SMarcel Moolenaar 			argv[argc] = args;
15973955d011SMarcel Moolenaar 			argc++;
15983955d011SMarcel Moolenaar 		}
15993955d011SMarcel Moolenaar 	} else {
160006b9b3e0SSimon J. Gerraty 		if (!job->ignerr && shell->errFlag != NULL) {
160106b9b3e0SSimon J. Gerraty 			argv[argc] = UNCONST(shell->errFlag);
16023955d011SMarcel Moolenaar 			argc++;
16033955d011SMarcel Moolenaar 		}
160406b9b3e0SSimon J. Gerraty 		if (job->echo && shell->echoFlag != NULL) {
160506b9b3e0SSimon J. Gerraty 			argv[argc] = UNCONST(shell->echoFlag);
16063955d011SMarcel Moolenaar 			argc++;
16073955d011SMarcel Moolenaar 		}
16083955d011SMarcel Moolenaar 	}
16093955d011SMarcel Moolenaar 	argv[argc] = NULL;
16103955d011SMarcel Moolenaar }
16113955d011SMarcel Moolenaar 
161206b9b3e0SSimon J. Gerraty static void
1613b0c40a00SSimon J. Gerraty JobWriteShellCommands(Job *job, GNode *gn, bool *out_run)
16143955d011SMarcel Moolenaar {
16153955d011SMarcel Moolenaar 	/*
161606b9b3e0SSimon J. Gerraty 	 * tfile is the name of a file into which all shell commands
161706b9b3e0SSimon J. Gerraty 	 * are put. It is removed before the child shell is executed,
161806b9b3e0SSimon J. Gerraty 	 * unless DEBUG(SCRIPT) is set.
16193955d011SMarcel Moolenaar 	 */
1620dba7b0efSSimon J. Gerraty 	char tfile[MAXPATHLEN];
162106b9b3e0SSimon J. Gerraty 	int tfd;		/* File descriptor to the temp file */
162206b9b3e0SSimon J. Gerraty 
1623dba7b0efSSimon J. Gerraty 	tfd = Job_TempFile(TMPPAT, tfile, sizeof tfile);
16243955d011SMarcel Moolenaar 
16253955d011SMarcel Moolenaar 	job->cmdFILE = fdopen(tfd, "w+");
1626e2eeea75SSimon J. Gerraty 	if (job->cmdFILE == NULL)
16273955d011SMarcel Moolenaar 		Punt("Could not fdopen %s", tfile);
1628e2eeea75SSimon J. Gerraty 
1629956e45f6SSimon J. Gerraty 	(void)fcntl(fileno(job->cmdFILE), F_SETFD, FD_CLOEXEC);
16303955d011SMarcel Moolenaar 
16313955d011SMarcel Moolenaar #ifdef USE_META
16323955d011SMarcel Moolenaar 	if (useMeta) {
16333955d011SMarcel Moolenaar 		meta_job_start(job, gn);
163406b9b3e0SSimon J. Gerraty 		if (gn->type & OP_SILENT)	/* might have changed */
1635b0c40a00SSimon J. Gerraty 			job->echo = false;
16363955d011SMarcel Moolenaar 	}
16373955d011SMarcel Moolenaar #endif
16383955d011SMarcel Moolenaar 
1639b0c40a00SSimon J. Gerraty 	*out_run = JobWriteCommands(job);
164006b9b3e0SSimon J. Gerraty }
164106b9b3e0SSimon J. Gerraty 
1642*6a7405f5SSimon J. Gerraty void
1643*6a7405f5SSimon J. Gerraty Job_Make(GNode *gn)
164406b9b3e0SSimon J. Gerraty {
164506b9b3e0SSimon J. Gerraty 	Job *job;		/* new job descriptor */
164606b9b3e0SSimon J. Gerraty 	char *argv[10];		/* Argument vector to shell */
1647b0c40a00SSimon J. Gerraty 	bool cmdsOK;		/* true if the nodes commands were all right */
1648b0c40a00SSimon J. Gerraty 	bool run;
164906b9b3e0SSimon J. Gerraty 
165006b9b3e0SSimon J. Gerraty 	for (job = job_table; job < job_table_end; job++) {
165106b9b3e0SSimon J. Gerraty 		if (job->status == JOB_ST_FREE)
165206b9b3e0SSimon J. Gerraty 			break;
165306b9b3e0SSimon J. Gerraty 	}
165406b9b3e0SSimon J. Gerraty 	if (job >= job_table_end)
1655*6a7405f5SSimon J. Gerraty 		Punt("Job_Make no job slots vacant");
165606b9b3e0SSimon J. Gerraty 
165706b9b3e0SSimon J. Gerraty 	memset(job, 0, sizeof *job);
165806b9b3e0SSimon J. Gerraty 	job->node = gn;
165906b9b3e0SSimon J. Gerraty 	job->tailCmds = NULL;
166006b9b3e0SSimon J. Gerraty 	job->status = JOB_ST_SET_UP;
166106b9b3e0SSimon J. Gerraty 
1662*6a7405f5SSimon J. Gerraty 	job->special = (gn->type & OP_SPECIAL) != OP_NONE;
166306b9b3e0SSimon J. Gerraty 	job->ignerr = opts.ignoreErrors || gn->type & OP_IGNORE;
16649f45a3c8SSimon J. Gerraty 	job->echo = !(opts.silent || gn->type & OP_SILENT);
166506b9b3e0SSimon J. Gerraty 
166606b9b3e0SSimon J. Gerraty 	/*
166706b9b3e0SSimon J. Gerraty 	 * Check the commands now so any attributes from .DEFAULT have a
166806b9b3e0SSimon J. Gerraty 	 * chance to migrate to the node.
166906b9b3e0SSimon J. Gerraty 	 */
167006b9b3e0SSimon J. Gerraty 	cmdsOK = Job_CheckCommands(gn, Error);
167106b9b3e0SSimon J. Gerraty 
167206b9b3e0SSimon J. Gerraty 	job->inPollfd = NULL;
1673dba7b0efSSimon J. Gerraty 
1674dba7b0efSSimon J. Gerraty 	if (Lst_IsEmpty(&gn->commands)) {
1675dba7b0efSSimon J. Gerraty 		job->cmdFILE = stdout;
1676b0c40a00SSimon J. Gerraty 		run = false;
1677b0c40a00SSimon J. Gerraty 
1678b0c40a00SSimon J. Gerraty 		/*
1679b0c40a00SSimon J. Gerraty 		 * We're serious here, but if the commands were bogus, we're
1680b0c40a00SSimon J. Gerraty 		 * also dead...
1681b0c40a00SSimon J. Gerraty 		 */
1682b0c40a00SSimon J. Gerraty 		if (!cmdsOK) {
16839f45a3c8SSimon J. Gerraty 			PrintOnError(gn, "\n");	/* provide some clue */
1684b0c40a00SSimon J. Gerraty 			DieHorribly();
1685b0c40a00SSimon J. Gerraty 		}
1686dba7b0efSSimon J. Gerraty 	} else if (((gn->type & OP_MAKE) && !opts.noRecursiveExecute) ||
16879f45a3c8SSimon J. Gerraty 	    (!opts.noExecute && !opts.touch)) {
1688dba7b0efSSimon J. Gerraty 		/*
1689dba7b0efSSimon J. Gerraty 		 * The above condition looks very similar to
1690dba7b0efSSimon J. Gerraty 		 * GNode_ShouldExecute but is subtly different.  It prevents
1691dba7b0efSSimon J. Gerraty 		 * that .MAKE targets are touched since these are usually
1692dba7b0efSSimon J. Gerraty 		 * virtual targets.
1693dba7b0efSSimon J. Gerraty 		 */
1694dba7b0efSSimon J. Gerraty 
1695*6a7405f5SSimon J. Gerraty 		int parseErrorsBefore;
1696*6a7405f5SSimon J. Gerraty 
1697b0c40a00SSimon J. Gerraty 		/*
1698b0c40a00SSimon J. Gerraty 		 * We're serious here, but if the commands were bogus, we're
1699b0c40a00SSimon J. Gerraty 		 * also dead...
1700b0c40a00SSimon J. Gerraty 		 */
1701b0c40a00SSimon J. Gerraty 		if (!cmdsOK) {
17029f45a3c8SSimon J. Gerraty 			PrintOnError(gn, "\n");	/* provide some clue */
1703b0c40a00SSimon J. Gerraty 			DieHorribly();
1704b0c40a00SSimon J. Gerraty 		}
1705b0c40a00SSimon J. Gerraty 
1706*6a7405f5SSimon J. Gerraty 		parseErrorsBefore = parseErrors;
1707b0c40a00SSimon J. Gerraty 		JobWriteShellCommands(job, gn, &run);
1708*6a7405f5SSimon J. Gerraty 		if (parseErrors != parseErrorsBefore)
1709*6a7405f5SSimon J. Gerraty 			run = false;
1710dba7b0efSSimon J. Gerraty 		(void)fflush(job->cmdFILE);
1711956e45f6SSimon J. Gerraty 	} else if (!GNode_ShouldExecute(gn)) {
17123955d011SMarcel Moolenaar 		/*
1713b0c40a00SSimon J. Gerraty 		 * Just write all the commands to stdout in one fell swoop.
1714dba7b0efSSimon J. Gerraty 		 * This still sets up job->tailCmds correctly.
17153955d011SMarcel Moolenaar 		 */
171606b9b3e0SSimon J. Gerraty 		SwitchOutputTo(gn);
17173955d011SMarcel Moolenaar 		job->cmdFILE = stdout;
1718956e45f6SSimon J. Gerraty 		if (cmdsOK)
1719b0c40a00SSimon J. Gerraty 			JobWriteCommands(job);
1720b0c40a00SSimon J. Gerraty 		run = false;
1721dba7b0efSSimon J. Gerraty 		(void)fflush(job->cmdFILE);
17223955d011SMarcel Moolenaar 	} else {
172306b9b3e0SSimon J. Gerraty 		Job_Touch(gn, job->echo);
1724b0c40a00SSimon J. Gerraty 		run = false;
17253955d011SMarcel Moolenaar 	}
17263955d011SMarcel Moolenaar 
172706b9b3e0SSimon J. Gerraty 	/* If we're not supposed to execute a shell, don't. */
172806b9b3e0SSimon J. Gerraty 	if (!run) {
172906b9b3e0SSimon J. Gerraty 		if (!job->special)
17303955d011SMarcel Moolenaar 			Job_TokenReturn();
173106b9b3e0SSimon J. Gerraty 		/* Unlink and close the command file if we opened one */
1732e2eeea75SSimon J. Gerraty 		if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
17333955d011SMarcel Moolenaar 			(void)fclose(job->cmdFILE);
17343955d011SMarcel Moolenaar 			job->cmdFILE = NULL;
17353955d011SMarcel Moolenaar 		}
17363955d011SMarcel Moolenaar 
17373955d011SMarcel Moolenaar 		/*
173806b9b3e0SSimon J. Gerraty 		 * We only want to work our way up the graph if we aren't
173906b9b3e0SSimon J. Gerraty 		 * here because the commands for the job were no good.
17403955d011SMarcel Moolenaar 		 */
1741956e45f6SSimon J. Gerraty 		if (cmdsOK && aborting == ABORT_NONE) {
1742956e45f6SSimon J. Gerraty 			JobSaveCommands(job);
17433955d011SMarcel Moolenaar 			job->node->made = MADE;
17443955d011SMarcel Moolenaar 			Make_Update(job->node);
17453955d011SMarcel Moolenaar 		}
1746e2eeea75SSimon J. Gerraty 		job->status = JOB_ST_FREE;
1747*6a7405f5SSimon J. Gerraty 		return;
17483955d011SMarcel Moolenaar 	}
17493955d011SMarcel Moolenaar 
17503955d011SMarcel Moolenaar 	/*
175106b9b3e0SSimon J. Gerraty 	 * Set up the control arguments to the shell. This is based on the
175206b9b3e0SSimon J. Gerraty 	 * flags set earlier for this job.
17533955d011SMarcel Moolenaar 	 */
17543955d011SMarcel Moolenaar 	JobMakeArgv(job, argv);
17553955d011SMarcel Moolenaar 
17563955d011SMarcel Moolenaar 	/* Create the pipe by which we'll get the shell's output. */
17573955d011SMarcel Moolenaar 	JobCreatePipe(job, 3);
17583955d011SMarcel Moolenaar 
17593955d011SMarcel Moolenaar 	JobExec(job, argv);
17603955d011SMarcel Moolenaar }
17613955d011SMarcel Moolenaar 
176206b9b3e0SSimon J. Gerraty /*
1763b0c40a00SSimon J. Gerraty  * If the shell has an output filter (which only csh and ksh have by default),
1764b0c40a00SSimon J. Gerraty  * print the output of the child process, skipping the noPrint text of the
1765b0c40a00SSimon J. Gerraty  * shell.
1766b0c40a00SSimon J. Gerraty  *
1767b0c40a00SSimon J. Gerraty  * Return the part of the output that the calling function needs to output by
1768b0c40a00SSimon J. Gerraty  * itself.
176906b9b3e0SSimon J. Gerraty  */
17703955d011SMarcel Moolenaar static char *
1771548bfc56SSimon J. Gerraty PrintFilteredOutput(char *p, const char *endp)	/* XXX: p should be const */
17723955d011SMarcel Moolenaar {
1773d5e0a182SSimon J. Gerraty 	char *ep;		/* XXX: should be const */
17743955d011SMarcel Moolenaar 
177506b9b3e0SSimon J. Gerraty 	if (shell->noPrint == NULL || shell->noPrint[0] == '\0')
1776d5e0a182SSimon J. Gerraty 		return p;
1777e2eeea75SSimon J. Gerraty 
177806b9b3e0SSimon J. Gerraty 	/*
177906b9b3e0SSimon J. Gerraty 	 * XXX: What happens if shell->noPrint occurs on the boundary of
178006b9b3e0SSimon J. Gerraty 	 * the buffer?  To work correctly in all cases, this should rather
178106b9b3e0SSimon J. Gerraty 	 * be a proper stream filter instead of doing string matching on
178206b9b3e0SSimon J. Gerraty 	 * selected chunks of the output.
178306b9b3e0SSimon J. Gerraty 	 */
1784d5e0a182SSimon J. Gerraty 	while ((ep = strstr(p, shell->noPrint)) != NULL) {
1785d5e0a182SSimon J. Gerraty 		if (ep != p) {
1786d5e0a182SSimon J. Gerraty 			*ep = '\0';	/* XXX: avoid writing to the buffer */
17873955d011SMarcel Moolenaar 			/*
17883955d011SMarcel Moolenaar 			 * The only way there wouldn't be a newline after
17893955d011SMarcel Moolenaar 			 * this line is if it were the last in the buffer.
179006b9b3e0SSimon J. Gerraty 			 * however, since the noPrint output comes after it,
17913955d011SMarcel Moolenaar 			 * there must be a newline, so we don't print one.
17923955d011SMarcel Moolenaar 			 */
179306b9b3e0SSimon J. Gerraty 			/* XXX: What about null bytes in the output? */
1794d5e0a182SSimon J. Gerraty 			(void)fprintf(stdout, "%s", p);
17953955d011SMarcel Moolenaar 			(void)fflush(stdout);
17963955d011SMarcel Moolenaar 		}
1797d5e0a182SSimon J. Gerraty 		p = ep + shell->noPrintLen;
1798d5e0a182SSimon J. Gerraty 		if (p == endp)
179906b9b3e0SSimon J. Gerraty 			break;
1800d5e0a182SSimon J. Gerraty 		p++;		/* skip over the (XXX: assumed) newline */
1801d5e0a182SSimon J. Gerraty 		pp_skip_whitespace(&p);
18023955d011SMarcel Moolenaar 	}
1803d5e0a182SSimon J. Gerraty 	return p;
18043955d011SMarcel Moolenaar }
18053955d011SMarcel Moolenaar 
1806e2eeea75SSimon J. Gerraty /*
1807e2eeea75SSimon J. Gerraty  * This function is called whenever there is something to read on the pipe.
1808e2eeea75SSimon J. Gerraty  * We collect more output from the given job and store it in the job's
1809e2eeea75SSimon J. Gerraty  * outBuf. If this makes up a line, we print it tagged by the job's
1810e2eeea75SSimon J. Gerraty  * identifier, as necessary.
1811e2eeea75SSimon J. Gerraty  *
1812e2eeea75SSimon J. Gerraty  * In the output of the shell, the 'noPrint' lines are removed. If the
1813e2eeea75SSimon J. Gerraty  * command is not alone on the line (the character after it is not \0 or
1814e2eeea75SSimon J. Gerraty  * \n), we do print whatever follows it.
18153955d011SMarcel Moolenaar  *
18163955d011SMarcel Moolenaar  * Input:
18173955d011SMarcel Moolenaar  *	job		the job whose output needs printing
1818b0c40a00SSimon J. Gerraty  *	finish		true if this is the last time we'll be called
18193955d011SMarcel Moolenaar  *			for this job
18203955d011SMarcel Moolenaar  */
1821956e45f6SSimon J. Gerraty static void
1822b0c40a00SSimon J. Gerraty CollectOutput(Job *job, bool finish)
18233955d011SMarcel Moolenaar {
1824b0c40a00SSimon J. Gerraty 	bool gotNL;		/* true if got a newline */
1825b0c40a00SSimon J. Gerraty 	bool fbuf;		/* true if our buffer filled up */
1826956e45f6SSimon J. Gerraty 	size_t nr;		/* number of bytes read */
1827956e45f6SSimon J. Gerraty 	size_t i;		/* auxiliary index into outBuf */
1828956e45f6SSimon J. Gerraty 	size_t max;		/* limit for i (end of current data) */
1829956e45f6SSimon J. Gerraty 	ssize_t nRead;		/* (Temporary) number of bytes read */
18303955d011SMarcel Moolenaar 
183106b9b3e0SSimon J. Gerraty 	/* Read as many bytes as will fit in the buffer. */
1832e2eeea75SSimon J. Gerraty again:
1833b0c40a00SSimon J. Gerraty 	gotNL = false;
1834b0c40a00SSimon J. Gerraty 	fbuf = false;
18353955d011SMarcel Moolenaar 
18363955d011SMarcel Moolenaar 	nRead = read(job->inPipe, &job->outBuf[job->curPos],
18373955d011SMarcel Moolenaar 	    JOB_BUFSIZE - job->curPos);
18383955d011SMarcel Moolenaar 	if (nRead < 0) {
18393955d011SMarcel Moolenaar 		if (errno == EAGAIN)
18403955d011SMarcel Moolenaar 			return;
18414fde40d9SSimon J. Gerraty 		if (DEBUG(JOB))
1842b0c40a00SSimon J. Gerraty 			perror("CollectOutput(piperead)");
18433955d011SMarcel Moolenaar 		nr = 0;
18444fde40d9SSimon J. Gerraty 	} else
1845956e45f6SSimon J. Gerraty 		nr = (size_t)nRead;
18464fde40d9SSimon J. Gerraty 
18474fde40d9SSimon J. Gerraty 	if (nr == 0)
18484fde40d9SSimon J. Gerraty 		finish = false;	/* stop looping */
18493955d011SMarcel Moolenaar 
18503955d011SMarcel Moolenaar 	/*
18513955d011SMarcel Moolenaar 	 * If we hit the end-of-file (the job is dead), we must flush its
18523955d011SMarcel Moolenaar 	 * remaining output, so pretend we read a newline if there's any
18533955d011SMarcel Moolenaar 	 * output remaining in the buffer.
18543955d011SMarcel Moolenaar 	 */
1855e2eeea75SSimon J. Gerraty 	if (nr == 0 && job->curPos != 0) {
18563955d011SMarcel Moolenaar 		job->outBuf[job->curPos] = '\n';
18573955d011SMarcel Moolenaar 		nr = 1;
18583955d011SMarcel Moolenaar 	}
18593955d011SMarcel Moolenaar 
18603955d011SMarcel Moolenaar 	max = job->curPos + nr;
18614fde40d9SSimon J. Gerraty 	for (i = job->curPos; i < max; i++)
18624fde40d9SSimon J. Gerraty 		if (job->outBuf[i] == '\0')
18634fde40d9SSimon J. Gerraty 			job->outBuf[i] = ' ';
18644fde40d9SSimon J. Gerraty 
18654fde40d9SSimon J. Gerraty 	/* Look for the last newline in the bytes we just got. */
186606b9b3e0SSimon J. Gerraty 	for (i = job->curPos + nr - 1;
186706b9b3e0SSimon J. Gerraty 	     i >= job->curPos && i != (size_t)-1; i--) {
18683955d011SMarcel Moolenaar 		if (job->outBuf[i] == '\n') {
1869b0c40a00SSimon J. Gerraty 			gotNL = true;
18703955d011SMarcel Moolenaar 			break;
18713955d011SMarcel Moolenaar 		}
18723955d011SMarcel Moolenaar 	}
18733955d011SMarcel Moolenaar 
18743955d011SMarcel Moolenaar 	if (!gotNL) {
18753955d011SMarcel Moolenaar 		job->curPos += nr;
18763955d011SMarcel Moolenaar 		if (job->curPos == JOB_BUFSIZE) {
18773955d011SMarcel Moolenaar 			/*
18783955d011SMarcel Moolenaar 			 * If we've run out of buffer space, we have no choice
18793955d011SMarcel Moolenaar 			 * but to print the stuff. sigh.
18803955d011SMarcel Moolenaar 			 */
1881b0c40a00SSimon J. Gerraty 			fbuf = true;
18823955d011SMarcel Moolenaar 			i = job->curPos;
18833955d011SMarcel Moolenaar 		}
18843955d011SMarcel Moolenaar 	}
18853955d011SMarcel Moolenaar 	if (gotNL || fbuf) {
18863955d011SMarcel Moolenaar 		/*
18873955d011SMarcel Moolenaar 		 * Need to send the output to the screen. Null terminate it
18883955d011SMarcel Moolenaar 		 * first, overwriting the newline character if there was one.
18893955d011SMarcel Moolenaar 		 * So long as the line isn't one we should filter (according
18903955d011SMarcel Moolenaar 		 * to the shell description), we print the line, preceded
18913955d011SMarcel Moolenaar 		 * by a target banner if this target isn't the same as the
18923955d011SMarcel Moolenaar 		 * one for which we last printed something.
18933955d011SMarcel Moolenaar 		 * The rest of the data in the buffer are then shifted down
18943955d011SMarcel Moolenaar 		 * to the start of the buffer and curPos is set accordingly.
18953955d011SMarcel Moolenaar 		 */
18963955d011SMarcel Moolenaar 		job->outBuf[i] = '\0';
18973955d011SMarcel Moolenaar 		if (i >= job->curPos) {
1898d5e0a182SSimon J. Gerraty 			char *p;
18993955d011SMarcel Moolenaar 
1900b0c40a00SSimon J. Gerraty 			/*
1901b0c40a00SSimon J. Gerraty 			 * FIXME: SwitchOutputTo should be here, according to
1902b0c40a00SSimon J. Gerraty 			 * the comment above.  But since PrintOutput does not
1903b0c40a00SSimon J. Gerraty 			 * do anything in the default shell, this bug has gone
1904b0c40a00SSimon J. Gerraty 			 * unnoticed until now.
1905b0c40a00SSimon J. Gerraty 			 */
1906d5e0a182SSimon J. Gerraty 			p = PrintFilteredOutput(job->outBuf, &job->outBuf[i]);
19073955d011SMarcel Moolenaar 
19083955d011SMarcel Moolenaar 			/*
1909b0c40a00SSimon J. Gerraty 			 * There's still more in the output buffer. This time,
191006b9b3e0SSimon J. Gerraty 			 * though, we know there's no newline at the end, so
191106b9b3e0SSimon J. Gerraty 			 * we add one of our own free will.
19123955d011SMarcel Moolenaar 			 */
1913d5e0a182SSimon J. Gerraty 			if (*p != '\0') {
19149f45a3c8SSimon J. Gerraty 				if (!opts.silent)
191506b9b3e0SSimon J. Gerraty 					SwitchOutputTo(job->node);
19163955d011SMarcel Moolenaar #ifdef USE_META
19173955d011SMarcel Moolenaar 				if (useMeta) {
1918d5e0a182SSimon J. Gerraty 					meta_job_output(job, p,
191906b9b3e0SSimon J. Gerraty 					    gotNL ? "\n" : "");
19203955d011SMarcel Moolenaar 				}
19213955d011SMarcel Moolenaar #endif
1922d5e0a182SSimon J. Gerraty 				(void)fprintf(stdout, "%s%s", p,
192306b9b3e0SSimon J. Gerraty 				    gotNL ? "\n" : "");
19243955d011SMarcel Moolenaar 				(void)fflush(stdout);
19253955d011SMarcel Moolenaar 			}
19263955d011SMarcel Moolenaar 		}
19273955d011SMarcel Moolenaar 		/*
192806b9b3e0SSimon J. Gerraty 		 * max is the last offset still in the buffer. Move any
192906b9b3e0SSimon J. Gerraty 		 * remaining characters to the start of the buffer and
193006b9b3e0SSimon J. Gerraty 		 * update the end marker curPos.
19313955d011SMarcel Moolenaar 		 */
1932db29cad8SSimon J. Gerraty 		if (i < max) {
193306b9b3e0SSimon J. Gerraty 			(void)memmove(job->outBuf, &job->outBuf[i + 1],
193406b9b3e0SSimon J. Gerraty 			    max - (i + 1));
1935db29cad8SSimon J. Gerraty 			job->curPos = max - (i + 1);
1936db29cad8SSimon J. Gerraty 		} else {
1937db29cad8SSimon J. Gerraty 			assert(i == max);
19383955d011SMarcel Moolenaar 			job->curPos = 0;
19393955d011SMarcel Moolenaar 		}
19403955d011SMarcel Moolenaar 	}
19413955d011SMarcel Moolenaar 	if (finish) {
19423955d011SMarcel Moolenaar 		/*
19433955d011SMarcel Moolenaar 		 * If the finish flag is true, we must loop until we hit
19443955d011SMarcel Moolenaar 		 * end-of-file on the pipe. This is guaranteed to happen
19453955d011SMarcel Moolenaar 		 * eventually since the other end of the pipe is now closed
19463955d011SMarcel Moolenaar 		 * (we closed it explicitly and the child has exited). When
1947b0c40a00SSimon J. Gerraty 		 * we do get an EOF, finish will be set false and we'll fall
19483955d011SMarcel Moolenaar 		 * through and out.
19493955d011SMarcel Moolenaar 		 */
1950e2eeea75SSimon J. Gerraty 		goto again;
19513955d011SMarcel Moolenaar 	}
19523955d011SMarcel Moolenaar }
19533955d011SMarcel Moolenaar 
19543955d011SMarcel Moolenaar static void
19553955d011SMarcel Moolenaar JobRun(GNode *targ)
19563955d011SMarcel Moolenaar {
1957*6a7405f5SSimon J. Gerraty 	/* Don't let these special jobs overlap with other unrelated jobs. */
19583955d011SMarcel Moolenaar 	Compat_Make(targ, targ);
1959*6a7405f5SSimon J. Gerraty 	if (GNode_IsError(targ)) {
19609f45a3c8SSimon J. Gerraty 		PrintOnError(targ, "\n\nStop.\n");
19613955d011SMarcel Moolenaar 		exit(1);
19623955d011SMarcel Moolenaar 	}
19633955d011SMarcel Moolenaar }
19643955d011SMarcel Moolenaar 
196506b9b3e0SSimon J. Gerraty /*
196606b9b3e0SSimon J. Gerraty  * Handle the exit of a child. Called from Make_Make.
19673955d011SMarcel Moolenaar  *
19683955d011SMarcel Moolenaar  * The job descriptor is removed from the list of children.
19693955d011SMarcel Moolenaar  *
19703955d011SMarcel Moolenaar  * Notes:
19713955d011SMarcel Moolenaar  *	We do waits, blocking or not, according to the wisdom of our
19723955d011SMarcel Moolenaar  *	caller, until there are no more children to report. For each
19733955d011SMarcel Moolenaar  *	job, call JobFinish to finish things off.
19743955d011SMarcel Moolenaar  */
19753955d011SMarcel Moolenaar void
19763955d011SMarcel Moolenaar Job_CatchChildren(void)
19773955d011SMarcel Moolenaar {
19783955d011SMarcel Moolenaar 	int pid;		/* pid of dead child */
19793955d011SMarcel Moolenaar 	WAIT_T status;		/* Exit/termination status */
19803955d011SMarcel Moolenaar 
198106b9b3e0SSimon J. Gerraty 	/* Don't even bother if we know there's no one around. */
19823955d011SMarcel Moolenaar 	if (jobTokensRunning == 0)
19833955d011SMarcel Moolenaar 		return;
19843955d011SMarcel Moolenaar 
1985dba7b0efSSimon J. Gerraty 	/* Have we received SIGCHLD since last call? */
1986dba7b0efSSimon J. Gerraty 	if (caught_sigchld == 0)
1987dba7b0efSSimon J. Gerraty 		return;
1988dba7b0efSSimon J. Gerraty 	caught_sigchld = 0;
1989dba7b0efSSimon J. Gerraty 
19903955d011SMarcel Moolenaar 	while ((pid = waitpid((pid_t)-1, &status, WNOHANG | WUNTRACED)) > 0) {
199106b9b3e0SSimon J. Gerraty 		DEBUG2(JOB, "Process %d exited/stopped status %x.\n",
199206b9b3e0SSimon J. Gerraty 		    pid, WAIT_STATUS(status));
1993b0c40a00SSimon J. Gerraty 		JobReapChild(pid, status, true);
19943955d011SMarcel Moolenaar 	}
19953955d011SMarcel Moolenaar }
19963955d011SMarcel Moolenaar 
19973955d011SMarcel Moolenaar /*
19983955d011SMarcel Moolenaar  * It is possible that wait[pid]() was called from elsewhere,
19993955d011SMarcel Moolenaar  * this lets us reap jobs regardless.
20003955d011SMarcel Moolenaar  */
20013955d011SMarcel Moolenaar void
2002b0c40a00SSimon J. Gerraty JobReapChild(pid_t pid, WAIT_T status, bool isJobs)
20033955d011SMarcel Moolenaar {
20043955d011SMarcel Moolenaar 	Job *job;		/* job descriptor for dead child */
20053955d011SMarcel Moolenaar 
200606b9b3e0SSimon J. Gerraty 	/* Don't even bother if we know there's no one around. */
20073955d011SMarcel Moolenaar 	if (jobTokensRunning == 0)
20083955d011SMarcel Moolenaar 		return;
20093955d011SMarcel Moolenaar 
20103955d011SMarcel Moolenaar 	job = JobFindPid(pid, JOB_ST_RUNNING, isJobs);
20113955d011SMarcel Moolenaar 	if (job == NULL) {
20123955d011SMarcel Moolenaar 		if (isJobs) {
20133955d011SMarcel Moolenaar 			if (!lurking_children)
201406b9b3e0SSimon J. Gerraty 				Error("Child (%d) status %x not in table?",
201506b9b3e0SSimon J. Gerraty 				    pid, status);
20163955d011SMarcel Moolenaar 		}
20173955d011SMarcel Moolenaar 		return;		/* not ours */
20183955d011SMarcel Moolenaar 	}
20193955d011SMarcel Moolenaar 	if (WIFSTOPPED(status)) {
202006b9b3e0SSimon J. Gerraty 		DEBUG2(JOB, "Process %d (%s) stopped.\n",
202106b9b3e0SSimon J. Gerraty 		    job->pid, job->node->name);
20223955d011SMarcel Moolenaar 		if (!make_suspended) {
20233955d011SMarcel Moolenaar 			switch (WSTOPSIG(status)) {
20243955d011SMarcel Moolenaar 			case SIGTSTP:
202506b9b3e0SSimon J. Gerraty 				(void)printf("*** [%s] Suspended\n",
202606b9b3e0SSimon J. Gerraty 				    job->node->name);
20273955d011SMarcel Moolenaar 				break;
20283955d011SMarcel Moolenaar 			case SIGSTOP:
202906b9b3e0SSimon J. Gerraty 				(void)printf("*** [%s] Stopped\n",
203006b9b3e0SSimon J. Gerraty 				    job->node->name);
20313955d011SMarcel Moolenaar 				break;
20323955d011SMarcel Moolenaar 			default:
20333955d011SMarcel Moolenaar 				(void)printf("*** [%s] Stopped -- signal %d\n",
20343955d011SMarcel Moolenaar 				    job->node->name, WSTOPSIG(status));
20353955d011SMarcel Moolenaar 			}
2036b0c40a00SSimon J. Gerraty 			job->suspended = true;
20373955d011SMarcel Moolenaar 		}
20383955d011SMarcel Moolenaar 		(void)fflush(stdout);
20393955d011SMarcel Moolenaar 		return;
20403955d011SMarcel Moolenaar 	}
20413955d011SMarcel Moolenaar 
2042e2eeea75SSimon J. Gerraty 	job->status = JOB_ST_FINISHED;
20433955d011SMarcel Moolenaar 	job->exit_status = WAIT_STATUS(status);
2044c59c3bf3SSimon J. Gerraty 	if (WIFEXITED(status))
2045c59c3bf3SSimon J. Gerraty 		job->node->exit_status = WEXITSTATUS(status);
20463955d011SMarcel Moolenaar 
20473955d011SMarcel Moolenaar 	JobFinish(job, status);
20483955d011SMarcel Moolenaar }
20493955d011SMarcel Moolenaar 
205006b9b3e0SSimon J. Gerraty /*
205106b9b3e0SSimon J. Gerraty  * Catch the output from our children, if we're using pipes do so. Otherwise
2052956e45f6SSimon J. Gerraty  * just block time until we get a signal(most likely a SIGCHLD) since there's
2053956e45f6SSimon J. Gerraty  * no point in just spinning when there's nothing to do and the reaping of a
205406b9b3e0SSimon J. Gerraty  * child can wait for a while.
205506b9b3e0SSimon J. Gerraty  */
20563955d011SMarcel Moolenaar void
20573955d011SMarcel Moolenaar Job_CatchOutput(void)
20583955d011SMarcel Moolenaar {
20593955d011SMarcel Moolenaar 	int nready;
20603955d011SMarcel Moolenaar 	Job *job;
2061956e45f6SSimon J. Gerraty 	unsigned int i;
20623955d011SMarcel Moolenaar 
20633955d011SMarcel Moolenaar 	(void)fflush(stdout);
20643955d011SMarcel Moolenaar 
20653955d011SMarcel Moolenaar 	/* The first fd in the list is the job token pipe */
20661748de26SSimon J. Gerraty 	do {
2067dba7b0efSSimon J. Gerraty 		nready = poll(fds + 1 - wantToken, fdsLen - 1 + wantToken,
206806b9b3e0SSimon J. Gerraty 		    POLL_MSEC);
20691748de26SSimon J. Gerraty 	} while (nready < 0 && errno == EINTR);
20703955d011SMarcel Moolenaar 
20711748de26SSimon J. Gerraty 	if (nready < 0)
20721748de26SSimon J. Gerraty 		Punt("poll: %s", strerror(errno));
20731748de26SSimon J. Gerraty 
20741748de26SSimon J. Gerraty 	if (nready > 0 && readyfd(&childExitJob)) {
20753955d011SMarcel Moolenaar 		char token = 0;
20761748de26SSimon J. Gerraty 		ssize_t count;
20771748de26SSimon J. Gerraty 		count = read(childExitJob.inPipe, &token, 1);
2078dba7b0efSSimon J. Gerraty 		if (count == 1) {
20793955d011SMarcel Moolenaar 			if (token == DO_JOB_RESUME[0])
208006b9b3e0SSimon J. Gerraty 				/*
208106b9b3e0SSimon J. Gerraty 				 * Complete relay requested from our SIGCONT
208206b9b3e0SSimon J. Gerraty 				 * handler
208306b9b3e0SSimon J. Gerraty 				 */
20843955d011SMarcel Moolenaar 				JobRestartJobs();
2085dba7b0efSSimon J. Gerraty 		} else if (count == 0)
2086dba7b0efSSimon J. Gerraty 			Punt("unexpected eof on token pipe");
208712904384SSimon J. Gerraty 		else if (errno != EAGAIN)
2088dba7b0efSSimon J. Gerraty 			Punt("token pipe read: %s", strerror(errno));
2089e2eeea75SSimon J. Gerraty 		nready--;
20903955d011SMarcel Moolenaar 	}
20913955d011SMarcel Moolenaar 
20921748de26SSimon J. Gerraty 	Job_CatchChildren();
20931748de26SSimon J. Gerraty 	if (nready == 0)
20943955d011SMarcel Moolenaar 		return;
20953955d011SMarcel Moolenaar 
2096dba7b0efSSimon J. Gerraty 	for (i = npseudojobs * nfds_per_job(); i < fdsLen; i++) {
209706b9b3e0SSimon J. Gerraty 		if (fds[i].revents == 0)
20983955d011SMarcel Moolenaar 			continue;
2099dba7b0efSSimon J. Gerraty 		job = jobByFdIndex[i];
2100e2eeea75SSimon J. Gerraty 		if (job->status == JOB_ST_RUNNING)
2101b0c40a00SSimon J. Gerraty 			CollectOutput(job, false);
210249caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
210349caa483SSimon J. Gerraty 		/*
210449caa483SSimon J. Gerraty 		 * With meta mode, we may have activity on the job's filemon
210506b9b3e0SSimon J. Gerraty 		 * descriptor too, which at the moment is any pollfd other
210606b9b3e0SSimon J. Gerraty 		 * than job->inPollfd.
210749caa483SSimon J. Gerraty 		 */
210849caa483SSimon J. Gerraty 		if (useMeta && job->inPollfd != &fds[i]) {
21099f45a3c8SSimon J. Gerraty 			if (meta_job_event(job) <= 0)
211049caa483SSimon J. Gerraty 				fds[i].events = 0;	/* never mind */
211149caa483SSimon J. Gerraty 		}
211249caa483SSimon J. Gerraty #endif
21131748de26SSimon J. Gerraty 		if (--nready == 0)
21141748de26SSimon J. Gerraty 			return;
21153955d011SMarcel Moolenaar 	}
21163955d011SMarcel Moolenaar }
21173955d011SMarcel Moolenaar 
211806b9b3e0SSimon J. Gerraty static void
211906b9b3e0SSimon J. Gerraty InitShellNameAndPath(void)
212006b9b3e0SSimon J. Gerraty {
212106b9b3e0SSimon J. Gerraty 	shellName = shell->name;
212206b9b3e0SSimon J. Gerraty 
212306b9b3e0SSimon J. Gerraty #ifdef DEFSHELL_CUSTOM
212406b9b3e0SSimon J. Gerraty 	if (shellName[0] == '/') {
21258d5c8e21SSimon J. Gerraty 		shellPath = bmake_strdup(shellName);
212606b9b3e0SSimon J. Gerraty 		shellName = str_basename(shellPath);
212706b9b3e0SSimon J. Gerraty 		return;
212806b9b3e0SSimon J. Gerraty 	}
212906b9b3e0SSimon J. Gerraty #endif
213012904384SSimon J. Gerraty #ifdef DEFSHELL_PATH
213122619282SSimon J. Gerraty 	shellPath = bmake_strdup(DEFSHELL_PATH);
213212904384SSimon J. Gerraty #else
213306b9b3e0SSimon J. Gerraty 	shellPath = str_concat3(_PATH_DEFSHELLDIR, "/", shellName);
213412904384SSimon J. Gerraty #endif
21353955d011SMarcel Moolenaar }
21363955d011SMarcel Moolenaar 
21373955d011SMarcel Moolenaar void
21383955d011SMarcel Moolenaar Shell_Init(void)
21393955d011SMarcel Moolenaar {
214006b9b3e0SSimon J. Gerraty 	if (shellPath == NULL)
214106b9b3e0SSimon J. Gerraty 		InitShellNameAndPath();
214206b9b3e0SSimon J. Gerraty 
2143c9f4001fSSimon J. Gerraty 	Var_SetWithFlags(SCOPE_CMDLINE, ".SHELL", shellPath,
2144c9f4001fSSimon J. Gerraty 			 VAR_SET_INTERNAL|VAR_SET_READONLY);
214506b9b3e0SSimon J. Gerraty 	if (shell->errFlag == NULL)
214606b9b3e0SSimon J. Gerraty 		shell->errFlag = "";
214706b9b3e0SSimon J. Gerraty 	if (shell->echoFlag == NULL)
214806b9b3e0SSimon J. Gerraty 		shell->echoFlag = "";
214906b9b3e0SSimon J. Gerraty 	if (shell->hasErrCtl && shell->errFlag[0] != '\0') {
215006b9b3e0SSimon J. Gerraty 		if (shellErrFlag != NULL &&
215106b9b3e0SSimon J. Gerraty 		    strcmp(shell->errFlag, &shellErrFlag[1]) != 0) {
215251ee2c1cSSimon J. Gerraty 			free(shellErrFlag);
215351ee2c1cSSimon J. Gerraty 			shellErrFlag = NULL;
215451ee2c1cSSimon J. Gerraty 		}
21559f45a3c8SSimon J. Gerraty 		if (shellErrFlag == NULL)
21569f45a3c8SSimon J. Gerraty 			shellErrFlag = str_concat2("-", shell->errFlag);
215706b9b3e0SSimon J. Gerraty 	} else if (shellErrFlag != NULL) {
215851ee2c1cSSimon J. Gerraty 		free(shellErrFlag);
215951ee2c1cSSimon J. Gerraty 		shellErrFlag = NULL;
216051ee2c1cSSimon J. Gerraty 	}
21613955d011SMarcel Moolenaar }
21623955d011SMarcel Moolenaar 
216306b9b3e0SSimon J. Gerraty /*
216406b9b3e0SSimon J. Gerraty  * Return the string literal that is used in the current command shell
216506b9b3e0SSimon J. Gerraty  * to produce a newline character.
216606b9b3e0SSimon J. Gerraty  */
21673955d011SMarcel Moolenaar const char *
21683955d011SMarcel Moolenaar Shell_GetNewline(void)
21693955d011SMarcel Moolenaar {
217006b9b3e0SSimon J. Gerraty 	return shell->newline;
21713955d011SMarcel Moolenaar }
21723955d011SMarcel Moolenaar 
21733955d011SMarcel Moolenaar void
21743955d011SMarcel Moolenaar Job_SetPrefix(void)
21753955d011SMarcel Moolenaar {
2176d5e0a182SSimon J. Gerraty 	if (targPrefix != NULL)
21773955d011SMarcel Moolenaar 		free(targPrefix);
2178d5e0a182SSimon J. Gerraty 	else if (!Var_Exists(SCOPE_GLOBAL, ".MAKE.JOB.PREFIX"))
21798c973ee2SSimon J. Gerraty 		Global_Set(".MAKE.JOB.PREFIX", "---");
21803955d011SMarcel Moolenaar 
21818c973ee2SSimon J. Gerraty 	targPrefix = Var_Subst("${.MAKE.JOB.PREFIX}",
21828d5c8e21SSimon J. Gerraty 	    SCOPE_GLOBAL, VARE_EVAL);
2183956e45f6SSimon J. Gerraty 	/* TODO: handle errors */
21843955d011SMarcel Moolenaar }
21853955d011SMarcel Moolenaar 
218606b9b3e0SSimon J. Gerraty static void
218706b9b3e0SSimon J. Gerraty AddSig(int sig, SignalProc handler)
218806b9b3e0SSimon J. Gerraty {
218906b9b3e0SSimon J. Gerraty 	if (bmake_signal(sig, SIG_IGN) != SIG_IGN) {
219006b9b3e0SSimon J. Gerraty 		sigaddset(&caught_signals, sig);
219106b9b3e0SSimon J. Gerraty 		(void)bmake_signal(sig, handler);
219206b9b3e0SSimon J. Gerraty 	}
219306b9b3e0SSimon J. Gerraty }
219406b9b3e0SSimon J. Gerraty 
21952c3632d1SSimon J. Gerraty /* Initialize the process module. */
21963955d011SMarcel Moolenaar void
21973955d011SMarcel Moolenaar Job_Init(void)
21983955d011SMarcel Moolenaar {
2199d191243dSSimon J. Gerraty 	Job_SetPrefix();
22003955d011SMarcel Moolenaar 	/* Allocate space for all the job info */
2201956e45f6SSimon J. Gerraty 	job_table = bmake_malloc((size_t)opts.maxJobs * sizeof *job_table);
2202956e45f6SSimon J. Gerraty 	memset(job_table, 0, (size_t)opts.maxJobs * sizeof *job_table);
2203956e45f6SSimon J. Gerraty 	job_table_end = job_table + opts.maxJobs;
22043955d011SMarcel Moolenaar 	wantToken = 0;
2205dba7b0efSSimon J. Gerraty 	caught_sigchld = 0;
22063955d011SMarcel Moolenaar 
2207956e45f6SSimon J. Gerraty 	aborting = ABORT_NONE;
220806b9b3e0SSimon J. Gerraty 	job_errors = 0;
22093955d011SMarcel Moolenaar 
221068c4481aSSimon J. Gerraty 	Always_pass_job_queue = GetBooleanExpr(MAKE_ALWAYS_PASS_JOB_QUEUE,
22119a4bc556SSimon J. Gerraty 	    Always_pass_job_queue);
22129a4bc556SSimon J. Gerraty 
221368c4481aSSimon J. Gerraty 	Job_error_token = GetBooleanExpr(MAKE_JOB_ERROR_TOKEN, Job_error_token);
221468c4481aSSimon J. Gerraty 
22152d395cb5SSimon J. Gerraty 
22163955d011SMarcel Moolenaar 	/*
22173955d011SMarcel Moolenaar 	 * There is a non-zero chance that we already have children.
22183955d011SMarcel Moolenaar 	 * eg after 'make -f- <<EOF'
221906b9b3e0SSimon J. Gerraty 	 * Since their termination causes a 'Child (pid) not in table'
222006b9b3e0SSimon J. Gerraty 	 * message, Collect the status of any that are already dead, and
222106b9b3e0SSimon J. Gerraty 	 * suppress the error message if there are any undead ones.
22223955d011SMarcel Moolenaar 	 */
22233955d011SMarcel Moolenaar 	for (;;) {
222406b9b3e0SSimon J. Gerraty 		int rval;
222506b9b3e0SSimon J. Gerraty 		WAIT_T status;
222606b9b3e0SSimon J. Gerraty 
22273955d011SMarcel Moolenaar 		rval = waitpid((pid_t)-1, &status, WNOHANG);
22283955d011SMarcel Moolenaar 		if (rval > 0)
22293955d011SMarcel Moolenaar 			continue;
22303955d011SMarcel Moolenaar 		if (rval == 0)
2231b0c40a00SSimon J. Gerraty 			lurking_children = true;
22323955d011SMarcel Moolenaar 		break;
22333955d011SMarcel Moolenaar 	}
22343955d011SMarcel Moolenaar 
22353955d011SMarcel Moolenaar 	Shell_Init();
22363955d011SMarcel Moolenaar 
22373955d011SMarcel Moolenaar 	JobCreatePipe(&childExitJob, 3);
22383955d011SMarcel Moolenaar 
2239dba7b0efSSimon J. Gerraty 	{
224049caa483SSimon J. Gerraty 		/* Preallocate enough for the maximum number of jobs. */
2241dba7b0efSSimon J. Gerraty 		size_t nfds = (npseudojobs + (size_t)opts.maxJobs) *
2242dba7b0efSSimon J. Gerraty 			      nfds_per_job();
2243dba7b0efSSimon J. Gerraty 		fds = bmake_malloc(sizeof *fds * nfds);
2244dba7b0efSSimon J. Gerraty 		jobByFdIndex = bmake_malloc(sizeof *jobByFdIndex * nfds);
2245dba7b0efSSimon J. Gerraty 	}
22463955d011SMarcel Moolenaar 
22473955d011SMarcel Moolenaar 	/* These are permanent entries and take slots 0 and 1 */
22483955d011SMarcel Moolenaar 	watchfd(&tokenWaitJob);
22493955d011SMarcel Moolenaar 	watchfd(&childExitJob);
22503955d011SMarcel Moolenaar 
22513955d011SMarcel Moolenaar 	sigemptyset(&caught_signals);
2252d5e0a182SSimon J. Gerraty 	/* Install a SIGCHLD handler. */
22533955d011SMarcel Moolenaar 	(void)bmake_signal(SIGCHLD, JobChildSig);
22543955d011SMarcel Moolenaar 	sigaddset(&caught_signals, SIGCHLD);
22553955d011SMarcel Moolenaar 
22563955d011SMarcel Moolenaar 	/*
22573955d011SMarcel Moolenaar 	 * Catch the four signals that POSIX specifies if they aren't ignored.
22583955d011SMarcel Moolenaar 	 * JobPassSig will take care of calling JobInterrupt if appropriate.
22593955d011SMarcel Moolenaar 	 */
226006b9b3e0SSimon J. Gerraty 	AddSig(SIGINT, JobPassSig_int);
226106b9b3e0SSimon J. Gerraty 	AddSig(SIGHUP, JobPassSig_term);
226206b9b3e0SSimon J. Gerraty 	AddSig(SIGTERM, JobPassSig_term);
226306b9b3e0SSimon J. Gerraty 	AddSig(SIGQUIT, JobPassSig_term);
22643955d011SMarcel Moolenaar 
22653955d011SMarcel Moolenaar 	/*
22663955d011SMarcel Moolenaar 	 * There are additional signals that need to be caught and passed if
22673955d011SMarcel Moolenaar 	 * either the export system wants to be told directly of signals or if
22683955d011SMarcel Moolenaar 	 * we're giving each job its own process group (since then it won't get
22693955d011SMarcel Moolenaar 	 * signals from the terminal driver as we own the terminal)
22703955d011SMarcel Moolenaar 	 */
227106b9b3e0SSimon J. Gerraty 	AddSig(SIGTSTP, JobPassSig_suspend);
227206b9b3e0SSimon J. Gerraty 	AddSig(SIGTTOU, JobPassSig_suspend);
227306b9b3e0SSimon J. Gerraty 	AddSig(SIGTTIN, JobPassSig_suspend);
227406b9b3e0SSimon J. Gerraty 	AddSig(SIGWINCH, JobCondPassSig);
227506b9b3e0SSimon J. Gerraty 	AddSig(SIGCONT, JobContinueSig);
22763955d011SMarcel Moolenaar 
22771748de26SSimon J. Gerraty 	(void)Job_RunTarget(".BEGIN", NULL);
22789f45a3c8SSimon J. Gerraty 	/*
22799f45a3c8SSimon J. Gerraty 	 * Create the .END node now, even though no code in the unit tests
2280954401e6SSimon J. Gerraty 	 * depends on it.  See also Targ_GetEndNode in Compat_MakeAll.
22819f45a3c8SSimon J. Gerraty 	 */
2282956e45f6SSimon J. Gerraty 	(void)Targ_GetEndNode();
22833955d011SMarcel Moolenaar }
22843955d011SMarcel Moolenaar 
228506b9b3e0SSimon J. Gerraty static void
228606b9b3e0SSimon J. Gerraty DelSig(int sig)
22873955d011SMarcel Moolenaar {
228806b9b3e0SSimon J. Gerraty 	if (sigismember(&caught_signals, sig) != 0)
228906b9b3e0SSimon J. Gerraty 		(void)bmake_signal(sig, SIG_DFL);
22903955d011SMarcel Moolenaar }
22913955d011SMarcel Moolenaar 
229206b9b3e0SSimon J. Gerraty static void
229306b9b3e0SSimon J. Gerraty JobSigReset(void)
229406b9b3e0SSimon J. Gerraty {
229506b9b3e0SSimon J. Gerraty 	DelSig(SIGINT);
229606b9b3e0SSimon J. Gerraty 	DelSig(SIGHUP);
229706b9b3e0SSimon J. Gerraty 	DelSig(SIGQUIT);
229806b9b3e0SSimon J. Gerraty 	DelSig(SIGTERM);
229906b9b3e0SSimon J. Gerraty 	DelSig(SIGTSTP);
230006b9b3e0SSimon J. Gerraty 	DelSig(SIGTTOU);
230106b9b3e0SSimon J. Gerraty 	DelSig(SIGTTIN);
230206b9b3e0SSimon J. Gerraty 	DelSig(SIGWINCH);
230306b9b3e0SSimon J. Gerraty 	DelSig(SIGCONT);
23043955d011SMarcel Moolenaar 	(void)bmake_signal(SIGCHLD, SIG_DFL);
23053955d011SMarcel Moolenaar }
23063955d011SMarcel Moolenaar 
23072c3632d1SSimon J. Gerraty /* Find a shell in 'shells' given its name, or return NULL. */
23083955d011SMarcel Moolenaar static Shell *
2309956e45f6SSimon J. Gerraty FindShellByName(const char *name)
23103955d011SMarcel Moolenaar {
2311956e45f6SSimon J. Gerraty 	Shell *sh = shells;
2312956e45f6SSimon J. Gerraty 	const Shell *shellsEnd = sh + sizeof shells / sizeof shells[0];
23133955d011SMarcel Moolenaar 
2314956e45f6SSimon J. Gerraty 	for (sh = shells; sh < shellsEnd; sh++) {
23153955d011SMarcel Moolenaar 		if (strcmp(name, sh->name) == 0)
23163841c287SSimon J. Gerraty 			return sh;
23173955d011SMarcel Moolenaar 	}
23183955d011SMarcel Moolenaar 	return NULL;
23193955d011SMarcel Moolenaar }
23203955d011SMarcel Moolenaar 
232106b9b3e0SSimon J. Gerraty /*
232206b9b3e0SSimon J. Gerraty  * Parse a shell specification and set up 'shell', shellPath and
232306b9b3e0SSimon J. Gerraty  * shellName appropriately.
23243955d011SMarcel Moolenaar  *
23253955d011SMarcel Moolenaar  * Input:
23263955d011SMarcel Moolenaar  *	line		The shell spec
23273955d011SMarcel Moolenaar  *
23283955d011SMarcel Moolenaar  * Results:
2329b0c40a00SSimon J. Gerraty  *	false if the specification was incorrect.
23303955d011SMarcel Moolenaar  *
23313955d011SMarcel Moolenaar  * Side Effects:
233206b9b3e0SSimon J. Gerraty  *	'shell' points to a Shell structure (either predefined or
23333955d011SMarcel Moolenaar  *	created from the shell spec), shellPath is the full path of the
233406b9b3e0SSimon J. Gerraty  *	shell described by 'shell', while shellName is just the
23353955d011SMarcel Moolenaar  *	final component of shellPath.
23363955d011SMarcel Moolenaar  *
23373955d011SMarcel Moolenaar  * Notes:
23383955d011SMarcel Moolenaar  *	A shell specification consists of a .SHELL target, with dependency
23393955d011SMarcel Moolenaar  *	operator, followed by a series of blank-separated words. Double
23403955d011SMarcel Moolenaar  *	quotes can be used to use blanks in words. A backslash escapes
23413955d011SMarcel Moolenaar  *	anything (most notably a double-quote and a space) and
23423955d011SMarcel Moolenaar  *	provides the functionality it does in C. Each word consists of
23433955d011SMarcel Moolenaar  *	keyword and value separated by an equal sign. There should be no
23443955d011SMarcel Moolenaar  *	unnecessary spaces in the word. The keywords are as follows:
23453955d011SMarcel Moolenaar  *	    name	Name of shell.
23463955d011SMarcel Moolenaar  *	    path	Location of shell.
23473955d011SMarcel Moolenaar  *	    quiet	Command to turn off echoing.
23483955d011SMarcel Moolenaar  *	    echo	Command to turn echoing on
23493955d011SMarcel Moolenaar  *	    filter	Result of turning off echoing that shouldn't be
23503955d011SMarcel Moolenaar  *			printed.
23513955d011SMarcel Moolenaar  *	    echoFlag	Flag to turn echoing on at the start
23523955d011SMarcel Moolenaar  *	    errFlag	Flag to turn error checking on at the start
23533955d011SMarcel Moolenaar  *	    hasErrCtl	True if shell has error checking control
23543955d011SMarcel Moolenaar  *	    newline	String literal to represent a newline char
23553955d011SMarcel Moolenaar  *	    check	Command to turn on error checking if hasErrCtl
2356b0c40a00SSimon J. Gerraty  *			is true or template of command to echo a command
23573955d011SMarcel Moolenaar  *			for which error checking is off if hasErrCtl is
2358b0c40a00SSimon J. Gerraty  *			false.
23593955d011SMarcel Moolenaar  *	    ignore	Command to turn off error checking if hasErrCtl
2360b0c40a00SSimon J. Gerraty  *			is true or template of command to execute a
23613955d011SMarcel Moolenaar  *			command so as to ignore any errors it returns if
2362b0c40a00SSimon J. Gerraty  *			hasErrCtl is false.
23633955d011SMarcel Moolenaar  */
2364b0c40a00SSimon J. Gerraty bool
23653955d011SMarcel Moolenaar Job_ParseShell(char *line)
23663955d011SMarcel Moolenaar {
23672c3632d1SSimon J. Gerraty 	Words wordsList;
23683955d011SMarcel Moolenaar 	char **words;
23693955d011SMarcel Moolenaar 	char **argv;
23702c3632d1SSimon J. Gerraty 	size_t argc;
23713955d011SMarcel Moolenaar 	char *path;
23723955d011SMarcel Moolenaar 	Shell newShell;
2373b0c40a00SSimon J. Gerraty 	bool fullSpec = false;
23743955d011SMarcel Moolenaar 	Shell *sh;
23753955d011SMarcel Moolenaar 
237606b9b3e0SSimon J. Gerraty 	/* XXX: don't use line as an iterator variable */
2377956e45f6SSimon J. Gerraty 	pp_skip_whitespace(&line);
23783955d011SMarcel Moolenaar 
2379dba7b0efSSimon J. Gerraty 	free(shell_freeIt);
23803955d011SMarcel Moolenaar 
2381e2eeea75SSimon J. Gerraty 	memset(&newShell, 0, sizeof newShell);
23823955d011SMarcel Moolenaar 
2383d5e0a182SSimon J. Gerraty 	/* Parse the specification by keyword. */
2384b0c40a00SSimon J. Gerraty 	wordsList = Str_Words(line, true);
23852c3632d1SSimon J. Gerraty 	words = wordsList.words;
23862c3632d1SSimon J. Gerraty 	argc = wordsList.len;
23872c3632d1SSimon J. Gerraty 	path = wordsList.freeIt;
23883955d011SMarcel Moolenaar 	if (words == NULL) {
23893955d011SMarcel Moolenaar 		Error("Unterminated quoted string [%s]", line);
2390b0c40a00SSimon J. Gerraty 		return false;
23913955d011SMarcel Moolenaar 	}
2392dba7b0efSSimon J. Gerraty 	shell_freeIt = path;
23933955d011SMarcel Moolenaar 
23943955d011SMarcel Moolenaar 	for (path = NULL, argv = words; argc != 0; argc--, argv++) {
2395956e45f6SSimon J. Gerraty 		char *arg = *argv;
2396956e45f6SSimon J. Gerraty 		if (strncmp(arg, "path=", 5) == 0) {
2397956e45f6SSimon J. Gerraty 			path = arg + 5;
2398956e45f6SSimon J. Gerraty 		} else if (strncmp(arg, "name=", 5) == 0) {
2399956e45f6SSimon J. Gerraty 			newShell.name = arg + 5;
24003955d011SMarcel Moolenaar 		} else {
2401956e45f6SSimon J. Gerraty 			if (strncmp(arg, "quiet=", 6) == 0) {
2402956e45f6SSimon J. Gerraty 				newShell.echoOff = arg + 6;
2403956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "echo=", 5) == 0) {
2404956e45f6SSimon J. Gerraty 				newShell.echoOn = arg + 5;
2405956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "filter=", 7) == 0) {
2406956e45f6SSimon J. Gerraty 				newShell.noPrint = arg + 7;
2407956e45f6SSimon J. Gerraty 				newShell.noPrintLen = strlen(newShell.noPrint);
2408956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "echoFlag=", 9) == 0) {
240906b9b3e0SSimon J. Gerraty 				newShell.echoFlag = arg + 9;
2410956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "errFlag=", 8) == 0) {
241106b9b3e0SSimon J. Gerraty 				newShell.errFlag = arg + 8;
2412956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "hasErrCtl=", 10) == 0) {
2413956e45f6SSimon J. Gerraty 				char c = arg[10];
2414956e45f6SSimon J. Gerraty 				newShell.hasErrCtl = c == 'Y' || c == 'y' ||
2415956e45f6SSimon J. Gerraty 						     c == 'T' || c == 't';
2416956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "newline=", 8) == 0) {
2417956e45f6SSimon J. Gerraty 				newShell.newline = arg + 8;
2418956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "check=", 6) == 0) {
24199f45a3c8SSimon J. Gerraty 				/*
24209f45a3c8SSimon J. Gerraty 				 * Before 2020-12-10, these two variables had
24219f45a3c8SSimon J. Gerraty 				 * been a single variable.
24229f45a3c8SSimon J. Gerraty 				 */
242306b9b3e0SSimon J. Gerraty 				newShell.errOn = arg + 6;
242406b9b3e0SSimon J. Gerraty 				newShell.echoTmpl = arg + 6;
2425956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "ignore=", 7) == 0) {
24269f45a3c8SSimon J. Gerraty 				/*
24279f45a3c8SSimon J. Gerraty 				 * Before 2020-12-10, these two variables had
24289f45a3c8SSimon J. Gerraty 				 * been a single variable.
24299f45a3c8SSimon J. Gerraty 				 */
243006b9b3e0SSimon J. Gerraty 				newShell.errOff = arg + 7;
243106b9b3e0SSimon J. Gerraty 				newShell.runIgnTmpl = arg + 7;
2432956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "errout=", 7) == 0) {
243306b9b3e0SSimon J. Gerraty 				newShell.runChkTmpl = arg + 7;
2434956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "comment=", 8) == 0) {
2435956e45f6SSimon J. Gerraty 				newShell.commentChar = arg[8];
24363955d011SMarcel Moolenaar 			} else {
243706b9b3e0SSimon J. Gerraty 				Parse_Error(PARSE_FATAL,
243806b9b3e0SSimon J. Gerraty 				    "Unknown keyword \"%s\"", arg);
24393955d011SMarcel Moolenaar 				free(words);
2440b0c40a00SSimon J. Gerraty 				return false;
24413955d011SMarcel Moolenaar 			}
2442b0c40a00SSimon J. Gerraty 			fullSpec = true;
24433955d011SMarcel Moolenaar 		}
24443955d011SMarcel Moolenaar 	}
24453955d011SMarcel Moolenaar 
24463955d011SMarcel Moolenaar 	if (path == NULL) {
24473955d011SMarcel Moolenaar 		/*
244806b9b3e0SSimon J. Gerraty 		 * If no path was given, the user wants one of the
244906b9b3e0SSimon J. Gerraty 		 * pre-defined shells, yes? So we find the one s/he wants
245006b9b3e0SSimon J. Gerraty 		 * with the help of FindShellByName and set things up the
245106b9b3e0SSimon J. Gerraty 		 * right way. shellPath will be set up by Shell_Init.
24523955d011SMarcel Moolenaar 		 */
24533955d011SMarcel Moolenaar 		if (newShell.name == NULL) {
245406b9b3e0SSimon J. Gerraty 			Parse_Error(PARSE_FATAL,
245506b9b3e0SSimon J. Gerraty 			    "Neither path nor name specified");
24563955d011SMarcel Moolenaar 			free(words);
2457b0c40a00SSimon J. Gerraty 			return false;
24583955d011SMarcel Moolenaar 		} else {
2459956e45f6SSimon J. Gerraty 			if ((sh = FindShellByName(newShell.name)) == NULL) {
246006b9b3e0SSimon J. Gerraty 				Parse_Error(PARSE_WARNING,
246106b9b3e0SSimon J. Gerraty 				    "%s: No matching shell", newShell.name);
24623955d011SMarcel Moolenaar 				free(words);
2463b0c40a00SSimon J. Gerraty 				return false;
24643955d011SMarcel Moolenaar 			}
246506b9b3e0SSimon J. Gerraty 			shell = sh;
24663955d011SMarcel Moolenaar 			shellName = newShell.name;
246706b9b3e0SSimon J. Gerraty 			if (shellPath != NULL) {
246806b9b3e0SSimon J. Gerraty 				/*
246906b9b3e0SSimon J. Gerraty 				 * Shell_Init has already been called!
247006b9b3e0SSimon J. Gerraty 				 * Do it again.
247106b9b3e0SSimon J. Gerraty 				 */
247222619282SSimon J. Gerraty 				free(shellPath);
24733955d011SMarcel Moolenaar 				shellPath = NULL;
24743955d011SMarcel Moolenaar 				Shell_Init();
24753955d011SMarcel Moolenaar 			}
24763955d011SMarcel Moolenaar 		}
24773955d011SMarcel Moolenaar 	} else {
247822619282SSimon J. Gerraty 		free(shellPath);
24798d5c8e21SSimon J. Gerraty 		shellPath = bmake_strdup(path);
2480d5e0a182SSimon J. Gerraty 		shellName = newShell.name != NULL ? newShell.name
2481d5e0a182SSimon J. Gerraty 		    : str_basename(path);
24823955d011SMarcel Moolenaar 		if (!fullSpec) {
2483956e45f6SSimon J. Gerraty 			if ((sh = FindShellByName(shellName)) == NULL) {
248406b9b3e0SSimon J. Gerraty 				Parse_Error(PARSE_WARNING,
248506b9b3e0SSimon J. Gerraty 				    "%s: No matching shell", shellName);
24863955d011SMarcel Moolenaar 				free(words);
2487b0c40a00SSimon J. Gerraty 				return false;
24883955d011SMarcel Moolenaar 			}
248906b9b3e0SSimon J. Gerraty 			shell = sh;
24903955d011SMarcel Moolenaar 		} else {
249106b9b3e0SSimon J. Gerraty 			shell = bmake_malloc(sizeof *shell);
249206b9b3e0SSimon J. Gerraty 			*shell = newShell;
24933955d011SMarcel Moolenaar 		}
249451ee2c1cSSimon J. Gerraty 		/* this will take care of shellErrFlag */
249551ee2c1cSSimon J. Gerraty 		Shell_Init();
24963955d011SMarcel Moolenaar 	}
24973955d011SMarcel Moolenaar 
249806b9b3e0SSimon J. Gerraty 	if (shell->echoOn != NULL && shell->echoOff != NULL)
2499b0c40a00SSimon J. Gerraty 		shell->hasEchoCtl = true;
25003955d011SMarcel Moolenaar 
250106b9b3e0SSimon J. Gerraty 	if (!shell->hasErrCtl) {
250206b9b3e0SSimon J. Gerraty 		if (shell->echoTmpl == NULL)
250306b9b3e0SSimon J. Gerraty 			shell->echoTmpl = "";
250406b9b3e0SSimon J. Gerraty 		if (shell->runIgnTmpl == NULL)
250506b9b3e0SSimon J. Gerraty 			shell->runIgnTmpl = "%s\n";
25063955d011SMarcel Moolenaar 	}
25073955d011SMarcel Moolenaar 
25083955d011SMarcel Moolenaar 	/*
250906b9b3e0SSimon J. Gerraty 	 * Do not free up the words themselves, since they might be in use
251006b9b3e0SSimon J. Gerraty 	 * by the shell specification.
25113955d011SMarcel Moolenaar 	 */
25123955d011SMarcel Moolenaar 	free(words);
2513b0c40a00SSimon J. Gerraty 	return true;
25143955d011SMarcel Moolenaar }
25153955d011SMarcel Moolenaar 
251606b9b3e0SSimon J. Gerraty /*
251706b9b3e0SSimon J. Gerraty  * Handle the receipt of an interrupt.
2518956e45f6SSimon J. Gerraty  *
2519956e45f6SSimon J. Gerraty  * All children are killed. Another job will be started if the .INTERRUPT
2520956e45f6SSimon J. Gerraty  * target is defined.
25213955d011SMarcel Moolenaar  *
25223955d011SMarcel Moolenaar  * Input:
25233955d011SMarcel Moolenaar  *	runINTERRUPT	Non-zero if commands for the .INTERRUPT target
25243955d011SMarcel Moolenaar  *			should be executed
25253955d011SMarcel Moolenaar  *	signo		signal received
25263955d011SMarcel Moolenaar  */
25273955d011SMarcel Moolenaar static void
2528b0c40a00SSimon J. Gerraty JobInterrupt(bool runINTERRUPT, int signo)
25293955d011SMarcel Moolenaar {
25303955d011SMarcel Moolenaar 	Job *job;		/* job descriptor in that element */
25313955d011SMarcel Moolenaar 	GNode *interrupt;	/* the node describing the .INTERRUPT target */
25323955d011SMarcel Moolenaar 	sigset_t mask;
25333955d011SMarcel Moolenaar 	GNode *gn;
25343955d011SMarcel Moolenaar 
25353955d011SMarcel Moolenaar 	aborting = ABORT_INTERRUPT;
25363955d011SMarcel Moolenaar 
25373955d011SMarcel Moolenaar 	JobSigLock(&mask);
25383955d011SMarcel Moolenaar 
25393955d011SMarcel Moolenaar 	for (job = job_table; job < job_table_end; job++) {
2540e2eeea75SSimon J. Gerraty 		if (job->status != JOB_ST_RUNNING)
25413955d011SMarcel Moolenaar 			continue;
25423955d011SMarcel Moolenaar 
25433955d011SMarcel Moolenaar 		gn = job->node;
25443955d011SMarcel Moolenaar 
254545447996SSimon J. Gerraty 		JobDeleteTarget(gn);
254606b9b3e0SSimon J. Gerraty 		if (job->pid != 0) {
254706b9b3e0SSimon J. Gerraty 			DEBUG2(JOB,
254806b9b3e0SSimon J. Gerraty 			    "JobInterrupt passing signal %d to child %d.\n",
25493955d011SMarcel Moolenaar 			    signo, job->pid);
25503955d011SMarcel Moolenaar 			KILLPG(job->pid, signo);
25513955d011SMarcel Moolenaar 		}
25523955d011SMarcel Moolenaar 	}
25533955d011SMarcel Moolenaar 
25543955d011SMarcel Moolenaar 	JobSigUnlock(&mask);
25553955d011SMarcel Moolenaar 
25569f45a3c8SSimon J. Gerraty 	if (runINTERRUPT && !opts.touch) {
2557956e45f6SSimon J. Gerraty 		interrupt = Targ_FindNode(".INTERRUPT");
25583955d011SMarcel Moolenaar 		if (interrupt != NULL) {
2559b0c40a00SSimon J. Gerraty 			opts.ignoreErrors = false;
25603955d011SMarcel Moolenaar 			JobRun(interrupt);
25613955d011SMarcel Moolenaar 		}
25623955d011SMarcel Moolenaar 	}
2563e2eeea75SSimon J. Gerraty 	Trace_Log(MAKEINTR, NULL);
256406b9b3e0SSimon J. Gerraty 	exit(signo);		/* XXX: why signo? */
25653955d011SMarcel Moolenaar }
25663955d011SMarcel Moolenaar 
256706b9b3e0SSimon J. Gerraty /*
256806b9b3e0SSimon J. Gerraty  * Do the final processing, i.e. run the commands attached to the .END target.
25693955d011SMarcel Moolenaar  *
257006b9b3e0SSimon J. Gerraty  * Return the number of errors reported.
257106b9b3e0SSimon J. Gerraty  */
25723955d011SMarcel Moolenaar int
25733955d011SMarcel Moolenaar Job_Finish(void)
25743955d011SMarcel Moolenaar {
2575956e45f6SSimon J. Gerraty 	GNode *endNode = Targ_GetEndNode();
257606b9b3e0SSimon J. Gerraty 	if (!Lst_IsEmpty(&endNode->commands) ||
257706b9b3e0SSimon J. Gerraty 	    !Lst_IsEmpty(&endNode->children)) {
2578d5e0a182SSimon J. Gerraty 		if (job_errors != 0)
25793955d011SMarcel Moolenaar 			Error("Errors reported so .END ignored");
2580d5e0a182SSimon J. Gerraty 		else
2581956e45f6SSimon J. Gerraty 			JobRun(endNode);
25823955d011SMarcel Moolenaar 	}
258306b9b3e0SSimon J. Gerraty 	return job_errors;
25843955d011SMarcel Moolenaar }
25853955d011SMarcel Moolenaar 
258622619282SSimon J. Gerraty #ifdef CLEANUP
2587956e45f6SSimon J. Gerraty /* Clean up any memory used by the jobs module. */
25883955d011SMarcel Moolenaar void
25893955d011SMarcel Moolenaar Job_End(void)
25903955d011SMarcel Moolenaar {
2591dba7b0efSSimon J. Gerraty 	free(shell_freeIt);
25923955d011SMarcel Moolenaar }
259322619282SSimon J. Gerraty #endif
25943955d011SMarcel Moolenaar 
259506b9b3e0SSimon J. Gerraty /*
259606b9b3e0SSimon J. Gerraty  * Waits for all running jobs to finish and returns.
259706b9b3e0SSimon J. Gerraty  * Sets 'aborting' to ABORT_WAIT to prevent other jobs from starting.
259806b9b3e0SSimon J. Gerraty  */
25993955d011SMarcel Moolenaar void
26003955d011SMarcel Moolenaar Job_Wait(void)
26013955d011SMarcel Moolenaar {
26023955d011SMarcel Moolenaar 	aborting = ABORT_WAIT;
26033955d011SMarcel Moolenaar 	while (jobTokensRunning != 0) {
26043955d011SMarcel Moolenaar 		Job_CatchOutput();
26053955d011SMarcel Moolenaar 	}
2606956e45f6SSimon J. Gerraty 	aborting = ABORT_NONE;
26073955d011SMarcel Moolenaar }
26083955d011SMarcel Moolenaar 
260906b9b3e0SSimon J. Gerraty /*
261006b9b3e0SSimon J. Gerraty  * Abort all currently running jobs without handling output or anything.
2611956e45f6SSimon J. Gerraty  * This function is to be called only in the event of a major error.
2612956e45f6SSimon J. Gerraty  * Most definitely NOT to be called from JobInterrupt.
26133955d011SMarcel Moolenaar  *
261406b9b3e0SSimon J. Gerraty  * All children are killed, not just the firstborn.
261506b9b3e0SSimon J. Gerraty  */
26163955d011SMarcel Moolenaar void
26173955d011SMarcel Moolenaar Job_AbortAll(void)
26183955d011SMarcel Moolenaar {
26193955d011SMarcel Moolenaar 	Job *job;		/* the job descriptor in that element */
26203955d011SMarcel Moolenaar 	WAIT_T foo;
26213955d011SMarcel Moolenaar 
26223955d011SMarcel Moolenaar 	aborting = ABORT_ERROR;
26233955d011SMarcel Moolenaar 
262406b9b3e0SSimon J. Gerraty 	if (jobTokensRunning != 0) {
26253955d011SMarcel Moolenaar 		for (job = job_table; job < job_table_end; job++) {
2626e2eeea75SSimon J. Gerraty 			if (job->status != JOB_ST_RUNNING)
26273955d011SMarcel Moolenaar 				continue;
26283955d011SMarcel Moolenaar 			/*
262906b9b3e0SSimon J. Gerraty 			 * kill the child process with increasingly drastic
263006b9b3e0SSimon J. Gerraty 			 * signals to make darn sure it's dead.
26313955d011SMarcel Moolenaar 			 */
26323955d011SMarcel Moolenaar 			KILLPG(job->pid, SIGINT);
26333955d011SMarcel Moolenaar 			KILLPG(job->pid, SIGKILL);
26343955d011SMarcel Moolenaar 		}
26353955d011SMarcel Moolenaar 	}
26363955d011SMarcel Moolenaar 
26373955d011SMarcel Moolenaar 	/*
26383955d011SMarcel Moolenaar 	 * Catch as many children as want to report in at first, then give up
26393955d011SMarcel Moolenaar 	 */
26403955d011SMarcel Moolenaar 	while (waitpid((pid_t)-1, &foo, WNOHANG) > 0)
26413955d011SMarcel Moolenaar 		continue;
26423955d011SMarcel Moolenaar }
26433955d011SMarcel Moolenaar 
264406b9b3e0SSimon J. Gerraty /*
264506b9b3e0SSimon J. Gerraty  * Tries to restart stopped jobs if there are slots available.
264606b9b3e0SSimon J. Gerraty  * Called in process context in response to a SIGCONT.
264706b9b3e0SSimon J. Gerraty  */
26483955d011SMarcel Moolenaar static void
26493955d011SMarcel Moolenaar JobRestartJobs(void)
26503955d011SMarcel Moolenaar {
26513955d011SMarcel Moolenaar 	Job *job;
26523955d011SMarcel Moolenaar 
26533955d011SMarcel Moolenaar 	for (job = job_table; job < job_table_end; job++) {
2654e2eeea75SSimon J. Gerraty 		if (job->status == JOB_ST_RUNNING &&
2655e2eeea75SSimon J. Gerraty 		    (make_suspended || job->suspended)) {
265606b9b3e0SSimon J. Gerraty 			DEBUG1(JOB, "Restarting stopped job pid %d.\n",
265706b9b3e0SSimon J. Gerraty 			    job->pid);
2658e2eeea75SSimon J. Gerraty 			if (job->suspended) {
265906b9b3e0SSimon J. Gerraty 				(void)printf("*** [%s] Continued\n",
266006b9b3e0SSimon J. Gerraty 				    job->node->name);
26613955d011SMarcel Moolenaar 				(void)fflush(stdout);
26623955d011SMarcel Moolenaar 			}
2663b0c40a00SSimon J. Gerraty 			job->suspended = false;
26643955d011SMarcel Moolenaar 			if (KILLPG(job->pid, SIGCONT) != 0 && DEBUG(JOB)) {
266506b9b3e0SSimon J. Gerraty 				debug_printf("Failed to send SIGCONT to %d\n",
266606b9b3e0SSimon J. Gerraty 				    job->pid);
26673955d011SMarcel Moolenaar 			}
26683955d011SMarcel Moolenaar 		}
266906b9b3e0SSimon J. Gerraty 		if (job->status == JOB_ST_FINISHED) {
267006b9b3e0SSimon J. Gerraty 			/*
267106b9b3e0SSimon J. Gerraty 			 * Job exit deferred after calling waitpid() in a
267206b9b3e0SSimon J. Gerraty 			 * signal handler
267306b9b3e0SSimon J. Gerraty 			 */
26743955d011SMarcel Moolenaar 			JobFinish(job, job->exit_status);
26753955d011SMarcel Moolenaar 		}
267606b9b3e0SSimon J. Gerraty 	}
2677b0c40a00SSimon J. Gerraty 	make_suspended = false;
26783955d011SMarcel Moolenaar }
26793955d011SMarcel Moolenaar 
26803955d011SMarcel Moolenaar static void
26813955d011SMarcel Moolenaar watchfd(Job *job)
26823955d011SMarcel Moolenaar {
26833955d011SMarcel Moolenaar 	if (job->inPollfd != NULL)
26843955d011SMarcel Moolenaar 		Punt("Watching watched job");
26853955d011SMarcel Moolenaar 
2686dba7b0efSSimon J. Gerraty 	fds[fdsLen].fd = job->inPipe;
2687dba7b0efSSimon J. Gerraty 	fds[fdsLen].events = POLLIN;
2688dba7b0efSSimon J. Gerraty 	jobByFdIndex[fdsLen] = job;
2689dba7b0efSSimon J. Gerraty 	job->inPollfd = &fds[fdsLen];
2690dba7b0efSSimon J. Gerraty 	fdsLen++;
269149caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
269249caa483SSimon J. Gerraty 	if (useMeta) {
2693dba7b0efSSimon J. Gerraty 		fds[fdsLen].fd = meta_job_fd(job);
2694dba7b0efSSimon J. Gerraty 		fds[fdsLen].events = fds[fdsLen].fd == -1 ? 0 : POLLIN;
2695dba7b0efSSimon J. Gerraty 		jobByFdIndex[fdsLen] = job;
2696dba7b0efSSimon J. Gerraty 		fdsLen++;
269749caa483SSimon J. Gerraty 	}
269849caa483SSimon J. Gerraty #endif
26993955d011SMarcel Moolenaar }
27003955d011SMarcel Moolenaar 
27013955d011SMarcel Moolenaar static void
27023955d011SMarcel Moolenaar clearfd(Job *job)
27033955d011SMarcel Moolenaar {
2704956e45f6SSimon J. Gerraty 	size_t i;
27053955d011SMarcel Moolenaar 	if (job->inPollfd == NULL)
27063955d011SMarcel Moolenaar 		Punt("Unwatching unwatched job");
2707956e45f6SSimon J. Gerraty 	i = (size_t)(job->inPollfd - fds);
2708dba7b0efSSimon J. Gerraty 	fdsLen--;
270949caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
271049caa483SSimon J. Gerraty 	if (useMeta) {
271149caa483SSimon J. Gerraty 		/*
271249caa483SSimon J. Gerraty 		 * Sanity check: there should be two fds per job, so the job's
271349caa483SSimon J. Gerraty 		 * pollfd number should be even.
271449caa483SSimon J. Gerraty 		 */
271549caa483SSimon J. Gerraty 		assert(nfds_per_job() == 2);
271606b9b3e0SSimon J. Gerraty 		if (i % 2 != 0)
271749caa483SSimon J. Gerraty 			Punt("odd-numbered fd with meta");
2718dba7b0efSSimon J. Gerraty 		fdsLen--;
271949caa483SSimon J. Gerraty 	}
272049caa483SSimon J. Gerraty #endif
2721d5e0a182SSimon J. Gerraty 	/* Move last job in table into hole made by dead job. */
2722dba7b0efSSimon J. Gerraty 	if (fdsLen != i) {
2723dba7b0efSSimon J. Gerraty 		fds[i] = fds[fdsLen];
2724dba7b0efSSimon J. Gerraty 		jobByFdIndex[i] = jobByFdIndex[fdsLen];
2725dba7b0efSSimon J. Gerraty 		jobByFdIndex[i]->inPollfd = &fds[i];
272649caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
272749caa483SSimon J. Gerraty 		if (useMeta) {
2728dba7b0efSSimon J. Gerraty 			fds[i + 1] = fds[fdsLen + 1];
2729dba7b0efSSimon J. Gerraty 			jobByFdIndex[i + 1] = jobByFdIndex[fdsLen + 1];
273049caa483SSimon J. Gerraty 		}
273149caa483SSimon J. Gerraty #endif
27323955d011SMarcel Moolenaar 	}
27333955d011SMarcel Moolenaar 	job->inPollfd = NULL;
27343955d011SMarcel Moolenaar }
27353955d011SMarcel Moolenaar 
2736b0c40a00SSimon J. Gerraty static bool
27373955d011SMarcel Moolenaar readyfd(Job *job)
27383955d011SMarcel Moolenaar {
27393955d011SMarcel Moolenaar 	if (job->inPollfd == NULL)
27403955d011SMarcel Moolenaar 		Punt("Polling unwatched job");
27413955d011SMarcel Moolenaar 	return (job->inPollfd->revents & POLLIN) != 0;
27423955d011SMarcel Moolenaar }
27433955d011SMarcel Moolenaar 
274406b9b3e0SSimon J. Gerraty /*
274506b9b3e0SSimon J. Gerraty  * Put a token (back) into the job pipe.
274606b9b3e0SSimon J. Gerraty  * This allows a make process to start a build job.
274706b9b3e0SSimon J. Gerraty  */
27483955d011SMarcel Moolenaar static void
27493955d011SMarcel Moolenaar JobTokenAdd(void)
27503955d011SMarcel Moolenaar {
27513955d011SMarcel Moolenaar 	char tok = JOB_TOKENS[aborting], tok1;
27523955d011SMarcel Moolenaar 
27532d395cb5SSimon J. Gerraty 	if (!Job_error_token && aborting == ABORT_ERROR) {
27542d395cb5SSimon J. Gerraty 		if (jobTokensRunning == 0)
27552d395cb5SSimon J. Gerraty 			return;
27562d395cb5SSimon J. Gerraty 		tok = '+';		/* no error token */
27572d395cb5SSimon J. Gerraty 	}
27582d395cb5SSimon J. Gerraty 
27593955d011SMarcel Moolenaar 	/* If we are depositing an error token flush everything else */
27603955d011SMarcel Moolenaar 	while (tok != '+' && read(tokenWaitJob.inPipe, &tok1, 1) == 1)
27613955d011SMarcel Moolenaar 		continue;
27623955d011SMarcel Moolenaar 
2763956e45f6SSimon J. Gerraty 	DEBUG3(JOB, "(%d) aborting %d, deposit token %c\n",
27642d395cb5SSimon J. Gerraty 	    getpid(), aborting, tok);
27653cbdda60SSimon J. Gerraty 	while (write(tokenWaitJob.outPipe, &tok, 1) == -1 && errno == EAGAIN)
27663cbdda60SSimon J. Gerraty 		continue;
27673955d011SMarcel Moolenaar }
27683955d011SMarcel Moolenaar 
2769dba7b0efSSimon J. Gerraty /* Get a temp file */
2770dba7b0efSSimon J. Gerraty int
2771dba7b0efSSimon J. Gerraty Job_TempFile(const char *pattern, char *tfile, size_t tfile_sz)
2772dba7b0efSSimon J. Gerraty {
2773dba7b0efSSimon J. Gerraty 	int fd;
2774dba7b0efSSimon J. Gerraty 	sigset_t mask;
2775dba7b0efSSimon J. Gerraty 
2776dba7b0efSSimon J. Gerraty 	JobSigLock(&mask);
2777dba7b0efSSimon J. Gerraty 	fd = mkTempFile(pattern, tfile, tfile_sz);
2778dba7b0efSSimon J. Gerraty 	if (tfile != NULL && !DEBUG(SCRIPT))
2779dba7b0efSSimon J. Gerraty 		unlink(tfile);
2780dba7b0efSSimon J. Gerraty 	JobSigUnlock(&mask);
2781dba7b0efSSimon J. Gerraty 
2782dba7b0efSSimon J. Gerraty 	return fd;
2783dba7b0efSSimon J. Gerraty }
2784dba7b0efSSimon J. Gerraty 
2785956e45f6SSimon J. Gerraty /* Prep the job token pipe in the root make process. */
27863955d011SMarcel Moolenaar void
27873955d011SMarcel Moolenaar Job_ServerStart(int max_tokens, int jp_0, int jp_1)
27883955d011SMarcel Moolenaar {
27893955d011SMarcel Moolenaar 	int i;
27903955d011SMarcel Moolenaar 	char jobarg[64];
27913955d011SMarcel Moolenaar 
27923955d011SMarcel Moolenaar 	if (jp_0 >= 0 && jp_1 >= 0) {
27933955d011SMarcel Moolenaar 		/* Pipe passed in from parent */
27943955d011SMarcel Moolenaar 		tokenWaitJob.inPipe = jp_0;
27953955d011SMarcel Moolenaar 		tokenWaitJob.outPipe = jp_1;
2796be19d90bSSimon J. Gerraty 		(void)fcntl(jp_0, F_SETFD, FD_CLOEXEC);
2797be19d90bSSimon J. Gerraty 		(void)fcntl(jp_1, F_SETFD, FD_CLOEXEC);
27983955d011SMarcel Moolenaar 		return;
27993955d011SMarcel Moolenaar 	}
28003955d011SMarcel Moolenaar 
28013955d011SMarcel Moolenaar 	JobCreatePipe(&tokenWaitJob, 15);
28023955d011SMarcel Moolenaar 
2803e2eeea75SSimon J. Gerraty 	snprintf(jobarg, sizeof jobarg, "%d,%d",
28043955d011SMarcel Moolenaar 	    tokenWaitJob.inPipe, tokenWaitJob.outPipe);
28053955d011SMarcel Moolenaar 
2806dba7b0efSSimon J. Gerraty 	Global_Append(MAKEFLAGS, "-J");
2807dba7b0efSSimon J. Gerraty 	Global_Append(MAKEFLAGS, jobarg);
28083955d011SMarcel Moolenaar 
28093955d011SMarcel Moolenaar 	/*
28103955d011SMarcel Moolenaar 	 * Preload the job pipe with one token per job, save the one
28113955d011SMarcel Moolenaar 	 * "extra" token for the primary job.
28123955d011SMarcel Moolenaar 	 *
28133955d011SMarcel Moolenaar 	 * XXX should clip maxJobs against PIPE_BUF -- if max_tokens is
28143955d011SMarcel Moolenaar 	 * larger than the write buffer size of the pipe, we will
28153955d011SMarcel Moolenaar 	 * deadlock here.
28163955d011SMarcel Moolenaar 	 */
28173955d011SMarcel Moolenaar 	for (i = 1; i < max_tokens; i++)
28183955d011SMarcel Moolenaar 		JobTokenAdd();
28193955d011SMarcel Moolenaar }
28203955d011SMarcel Moolenaar 
2821956e45f6SSimon J. Gerraty /* Return a withdrawn token to the pool. */
28223955d011SMarcel Moolenaar void
28233955d011SMarcel Moolenaar Job_TokenReturn(void)
28243955d011SMarcel Moolenaar {
28253955d011SMarcel Moolenaar 	jobTokensRunning--;
28263955d011SMarcel Moolenaar 	if (jobTokensRunning < 0)
28273955d011SMarcel Moolenaar 		Punt("token botch");
282806b9b3e0SSimon J. Gerraty 	if (jobTokensRunning != 0 || JOB_TOKENS[aborting] != '+')
28293955d011SMarcel Moolenaar 		JobTokenAdd();
28303955d011SMarcel Moolenaar }
28313955d011SMarcel Moolenaar 
283206b9b3e0SSimon J. Gerraty /*
283306b9b3e0SSimon J. Gerraty  * Attempt to withdraw a token from the pool.
28343955d011SMarcel Moolenaar  *
2835956e45f6SSimon J. Gerraty  * If pool is empty, set wantToken so that we wake up when a token is
2836956e45f6SSimon J. Gerraty  * released.
28373955d011SMarcel Moolenaar  *
2838b0c40a00SSimon J. Gerraty  * Returns true if a token was withdrawn, and false if the pool is currently
283906b9b3e0SSimon J. Gerraty  * empty.
284006b9b3e0SSimon J. Gerraty  */
2841b0c40a00SSimon J. Gerraty bool
28423955d011SMarcel Moolenaar Job_TokenWithdraw(void)
28433955d011SMarcel Moolenaar {
28443955d011SMarcel Moolenaar 	char tok, tok1;
2845956e45f6SSimon J. Gerraty 	ssize_t count;
28463955d011SMarcel Moolenaar 
28473955d011SMarcel Moolenaar 	wantToken = 0;
2848956e45f6SSimon J. Gerraty 	DEBUG3(JOB, "Job_TokenWithdraw(%d): aborting %d, running %d\n",
28493955d011SMarcel Moolenaar 	    getpid(), aborting, jobTokensRunning);
28503955d011SMarcel Moolenaar 
2851956e45f6SSimon J. Gerraty 	if (aborting != ABORT_NONE || (jobTokensRunning >= opts.maxJobs))
2852b0c40a00SSimon J. Gerraty 		return false;
28533955d011SMarcel Moolenaar 
28543955d011SMarcel Moolenaar 	count = read(tokenWaitJob.inPipe, &tok, 1);
28553955d011SMarcel Moolenaar 	if (count == 0)
28563955d011SMarcel Moolenaar 		Fatal("eof on job pipe!");
28573955d011SMarcel Moolenaar 	if (count < 0 && jobTokensRunning != 0) {
2858d5e0a182SSimon J. Gerraty 		if (errno != EAGAIN)
28593955d011SMarcel Moolenaar 			Fatal("job pipe read: %s", strerror(errno));
2860956e45f6SSimon J. Gerraty 		DEBUG1(JOB, "(%d) blocked for token\n", getpid());
286106b9b3e0SSimon J. Gerraty 		wantToken = 1;
2862b0c40a00SSimon J. Gerraty 		return false;
28633955d011SMarcel Moolenaar 	}
28643955d011SMarcel Moolenaar 
28653955d011SMarcel Moolenaar 	if (count == 1 && tok != '+') {
286606b9b3e0SSimon J. Gerraty 		/* make being aborted - remove any other job tokens */
2867956e45f6SSimon J. Gerraty 		DEBUG2(JOB, "(%d) aborted by token %c\n", getpid(), tok);
28683955d011SMarcel Moolenaar 		while (read(tokenWaitJob.inPipe, &tok1, 1) == 1)
28693955d011SMarcel Moolenaar 			continue;
28703955d011SMarcel Moolenaar 		/* And put the stopper back */
287106b9b3e0SSimon J. Gerraty 		while (write(tokenWaitJob.outPipe, &tok, 1) == -1 &&
287206b9b3e0SSimon J. Gerraty 		       errno == EAGAIN)
28733cbdda60SSimon J. Gerraty 			continue;
2874e2eeea75SSimon J. Gerraty 		if (shouldDieQuietly(NULL, 1))
287506b9b3e0SSimon J. Gerraty 			exit(6);	/* we aborted */
287606b9b3e0SSimon J. Gerraty 		Fatal("A failure has been detected "
287706b9b3e0SSimon J. Gerraty 		      "in another branch of the parallel make");
28783955d011SMarcel Moolenaar 	}
28793955d011SMarcel Moolenaar 
28803955d011SMarcel Moolenaar 	if (count == 1 && jobTokensRunning == 0)
28813955d011SMarcel Moolenaar 		/* We didn't want the token really */
288206b9b3e0SSimon J. Gerraty 		while (write(tokenWaitJob.outPipe, &tok, 1) == -1 &&
288306b9b3e0SSimon J. Gerraty 		       errno == EAGAIN)
28843cbdda60SSimon J. Gerraty 			continue;
28853955d011SMarcel Moolenaar 
28863955d011SMarcel Moolenaar 	jobTokensRunning++;
2887956e45f6SSimon J. Gerraty 	DEBUG1(JOB, "(%d) withdrew token\n", getpid());
2888b0c40a00SSimon J. Gerraty 	return true;
28893955d011SMarcel Moolenaar }
28903955d011SMarcel Moolenaar 
289106b9b3e0SSimon J. Gerraty /*
289206b9b3e0SSimon J. Gerraty  * Run the named target if found. If a filename is specified, then set that
2893956e45f6SSimon J. Gerraty  * to the sources.
28941748de26SSimon J. Gerraty  *
289506b9b3e0SSimon J. Gerraty  * Exits if the target fails.
289606b9b3e0SSimon J. Gerraty  */
2897b0c40a00SSimon J. Gerraty bool
289806b9b3e0SSimon J. Gerraty Job_RunTarget(const char *target, const char *fname)
289906b9b3e0SSimon J. Gerraty {
2900956e45f6SSimon J. Gerraty 	GNode *gn = Targ_FindNode(target);
29011748de26SSimon J. Gerraty 	if (gn == NULL)
2902b0c40a00SSimon J. Gerraty 		return false;
29031748de26SSimon J. Gerraty 
290406b9b3e0SSimon J. Gerraty 	if (fname != NULL)
2905dba7b0efSSimon J. Gerraty 		Var_Set(gn, ALLSRC, fname);
29061748de26SSimon J. Gerraty 
29071748de26SSimon J. Gerraty 	JobRun(gn);
2908b0c40a00SSimon J. Gerraty 	return true;
29091748de26SSimon J. Gerraty }
29101748de26SSimon J. Gerraty 
29113955d011SMarcel Moolenaar #ifdef USE_SELECT
29123955d011SMarcel Moolenaar int
29133955d011SMarcel Moolenaar emul_poll(struct pollfd *fd, int nfd, int timeout)
29143955d011SMarcel Moolenaar {
29153955d011SMarcel Moolenaar 	fd_set rfds, wfds;
29163955d011SMarcel Moolenaar 	int i, maxfd, nselect, npoll;
29173955d011SMarcel Moolenaar 	struct timeval tv, *tvp;
29183955d011SMarcel Moolenaar 	long usecs;
29193955d011SMarcel Moolenaar 
29203955d011SMarcel Moolenaar 	FD_ZERO(&rfds);
29213955d011SMarcel Moolenaar 	FD_ZERO(&wfds);
29223955d011SMarcel Moolenaar 
29233955d011SMarcel Moolenaar 	maxfd = -1;
29243955d011SMarcel Moolenaar 	for (i = 0; i < nfd; i++) {
29253955d011SMarcel Moolenaar 		fd[i].revents = 0;
29263955d011SMarcel Moolenaar 
29273955d011SMarcel Moolenaar 		if (fd[i].events & POLLIN)
29283955d011SMarcel Moolenaar 			FD_SET(fd[i].fd, &rfds);
29293955d011SMarcel Moolenaar 
29303955d011SMarcel Moolenaar 		if (fd[i].events & POLLOUT)
29313955d011SMarcel Moolenaar 			FD_SET(fd[i].fd, &wfds);
29323955d011SMarcel Moolenaar 
29333955d011SMarcel Moolenaar 		if (fd[i].fd > maxfd)
29343955d011SMarcel Moolenaar 			maxfd = fd[i].fd;
29353955d011SMarcel Moolenaar 	}
29363955d011SMarcel Moolenaar 
29373955d011SMarcel Moolenaar 	if (maxfd >= FD_SETSIZE) {
29383955d011SMarcel Moolenaar 		Punt("Ran out of fd_set slots; "
29393955d011SMarcel Moolenaar 		     "recompile with a larger FD_SETSIZE.");
29403955d011SMarcel Moolenaar 	}
29413955d011SMarcel Moolenaar 
29423955d011SMarcel Moolenaar 	if (timeout < 0) {
29433955d011SMarcel Moolenaar 		tvp = NULL;
29443955d011SMarcel Moolenaar 	} else {
29453955d011SMarcel Moolenaar 		usecs = timeout * 1000;
29463955d011SMarcel Moolenaar 		tv.tv_sec = usecs / 1000000;
29473955d011SMarcel Moolenaar 		tv.tv_usec = usecs % 1000000;
29483955d011SMarcel Moolenaar 		tvp = &tv;
29493955d011SMarcel Moolenaar 	}
29503955d011SMarcel Moolenaar 
2951e2eeea75SSimon J. Gerraty 	nselect = select(maxfd + 1, &rfds, &wfds, NULL, tvp);
29523955d011SMarcel Moolenaar 
29533955d011SMarcel Moolenaar 	if (nselect <= 0)
29543955d011SMarcel Moolenaar 		return nselect;
29553955d011SMarcel Moolenaar 
29563955d011SMarcel Moolenaar 	npoll = 0;
29573955d011SMarcel Moolenaar 	for (i = 0; i < nfd; i++) {
29583955d011SMarcel Moolenaar 		if (FD_ISSET(fd[i].fd, &rfds))
29593955d011SMarcel Moolenaar 			fd[i].revents |= POLLIN;
29603955d011SMarcel Moolenaar 
29613955d011SMarcel Moolenaar 		if (FD_ISSET(fd[i].fd, &wfds))
29623955d011SMarcel Moolenaar 			fd[i].revents |= POLLOUT;
29633955d011SMarcel Moolenaar 
29643955d011SMarcel Moolenaar 		if (fd[i].revents)
29653955d011SMarcel Moolenaar 			npoll++;
29663955d011SMarcel Moolenaar 	}
29673955d011SMarcel Moolenaar 
29683955d011SMarcel Moolenaar 	return npoll;
29693955d011SMarcel Moolenaar }
29703955d011SMarcel Moolenaar #endif				/* USE_SELECT */
2971