xref: /freebsd/contrib/bmake/job.c (revision 1d3f2ddc32fc37e4835aa5a51eabc8696c1e8114)
1*1d3f2ddcSSimon J. Gerraty /*	$NetBSD: job.c,v 1.452 2022/02/12 11:14:48 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 /*
733955d011SMarcel Moolenaar  * job.c --
743955d011SMarcel Moolenaar  *	handle the creation etc. of our child processes.
753955d011SMarcel Moolenaar  *
763955d011SMarcel Moolenaar  * Interface:
77956e45f6SSimon J. Gerraty  *	Job_Init	Called to initialize this module. In addition,
78dba7b0efSSimon J. Gerraty  *			the .BEGIN target is made including all of its
79dba7b0efSSimon J. Gerraty  *			dependencies before this function returns.
80956e45f6SSimon J. Gerraty  *			Hence, the makefiles must have been parsed
81956e45f6SSimon J. Gerraty  *			before this function is called.
82956e45f6SSimon J. Gerraty  *
83956e45f6SSimon J. Gerraty  *	Job_End		Clean up any memory used.
84956e45f6SSimon J. Gerraty  *
853955d011SMarcel Moolenaar  *	Job_Make	Start the creation of the given target.
863955d011SMarcel Moolenaar  *
87956e45f6SSimon J. Gerraty  *	Job_CatchChildren
88956e45f6SSimon J. Gerraty  *			Check for and handle the termination of any
893955d011SMarcel Moolenaar  *			children. This must be called reasonably
903955d011SMarcel Moolenaar  *			frequently to keep the whole make going at
913955d011SMarcel Moolenaar  *			a decent clip, since job table entries aren't
923955d011SMarcel Moolenaar  *			removed until their process is caught this way.
933955d011SMarcel Moolenaar  *
94956e45f6SSimon J. Gerraty  *	Job_CatchOutput
95956e45f6SSimon J. Gerraty  *			Print any output our children have produced.
963955d011SMarcel Moolenaar  *			Should also be called fairly frequently to
973955d011SMarcel Moolenaar  *			keep the user informed of what's going on.
983955d011SMarcel Moolenaar  *			If no output is waiting, it will block for
993955d011SMarcel Moolenaar  *			a time given by the SEL_* constants, below,
1003955d011SMarcel Moolenaar  *			or until output is ready.
1013955d011SMarcel Moolenaar  *
102dba7b0efSSimon J. Gerraty  *	Job_ParseShell	Given a special dependency line with target '.SHELL',
103dba7b0efSSimon J. Gerraty  *			define the shell that is used for the creation
104dba7b0efSSimon J. Gerraty  *			commands in jobs mode.
1053955d011SMarcel Moolenaar  *
1063955d011SMarcel Moolenaar  *	Job_Finish	Perform any final processing which needs doing.
1073955d011SMarcel Moolenaar  *			This includes the execution of any commands
1083955d011SMarcel Moolenaar  *			which have been/were attached to the .END
1093955d011SMarcel Moolenaar  *			target. It should only be called when the
1103955d011SMarcel Moolenaar  *			job table is empty.
1113955d011SMarcel Moolenaar  *
112dba7b0efSSimon J. Gerraty  *	Job_AbortAll	Abort all currently running jobs. Do not handle
113dba7b0efSSimon J. Gerraty  *			output or do anything for the jobs, just kill them.
114dba7b0efSSimon J. Gerraty  *			Should only be called in an emergency.
1153955d011SMarcel Moolenaar  *
116956e45f6SSimon J. Gerraty  *	Job_CheckCommands
117956e45f6SSimon J. Gerraty  *			Verify that the commands for a target are
1183955d011SMarcel Moolenaar  *			ok. Provide them if necessary and possible.
1193955d011SMarcel Moolenaar  *
1203955d011SMarcel Moolenaar  *	Job_Touch	Update a target without really updating it.
1213955d011SMarcel Moolenaar  *
1223955d011SMarcel Moolenaar  *	Job_Wait	Wait for all currently-running jobs to finish.
1233955d011SMarcel Moolenaar  */
1243955d011SMarcel Moolenaar 
1253955d011SMarcel Moolenaar #ifdef HAVE_CONFIG_H
1263955d011SMarcel Moolenaar # include "config.h"
1273955d011SMarcel Moolenaar #endif
1283955d011SMarcel Moolenaar #include <sys/types.h>
1293955d011SMarcel Moolenaar #include <sys/stat.h>
1303955d011SMarcel Moolenaar #include <sys/file.h>
1313955d011SMarcel Moolenaar #include <sys/time.h>
1323955d011SMarcel Moolenaar #include "wait.h"
1333955d011SMarcel Moolenaar 
1343955d011SMarcel Moolenaar #include <errno.h>
1353955d011SMarcel Moolenaar #if !defined(USE_SELECT) && defined(HAVE_POLL_H)
1363955d011SMarcel Moolenaar #include <poll.h>
1373955d011SMarcel Moolenaar #else
1383955d011SMarcel Moolenaar #ifndef USE_SELECT			/* no poll.h */
1393955d011SMarcel Moolenaar # define USE_SELECT
1403955d011SMarcel Moolenaar #endif
1413955d011SMarcel Moolenaar #if defined(HAVE_SYS_SELECT_H)
1423955d011SMarcel Moolenaar # include <sys/select.h>
1433955d011SMarcel Moolenaar #endif
1443955d011SMarcel Moolenaar #endif
1453955d011SMarcel Moolenaar #include <signal.h>
1463955d011SMarcel Moolenaar #include <utime.h>
1473955d011SMarcel Moolenaar #if defined(HAVE_SYS_SOCKET_H)
1483955d011SMarcel Moolenaar # include <sys/socket.h>
1493955d011SMarcel Moolenaar #endif
1503955d011SMarcel Moolenaar 
1513955d011SMarcel Moolenaar #include "make.h"
1523955d011SMarcel Moolenaar #include "dir.h"
1533955d011SMarcel Moolenaar #include "job.h"
1543955d011SMarcel Moolenaar #include "pathnames.h"
1553955d011SMarcel Moolenaar #include "trace.h"
156956e45f6SSimon J. Gerraty 
157956e45f6SSimon J. Gerraty /*	"@(#)job.c	8.2 (Berkeley) 3/19/94"	*/
158*1d3f2ddcSSimon J. Gerraty MAKE_RCSID("$NetBSD: job.c,v 1.452 2022/02/12 11:14:48 rillig Exp $");
159956e45f6SSimon J. Gerraty 
16006b9b3e0SSimon J. Gerraty /*
16106b9b3e0SSimon J. Gerraty  * A shell defines how the commands are run.  All commands for a target are
162956e45f6SSimon J. Gerraty  * written into a single file, which is then given to the shell to execute
163956e45f6SSimon J. Gerraty  * the commands from it.  The commands are written to the file using a few
164956e45f6SSimon J. Gerraty  * templates for echo control and error control.
165956e45f6SSimon J. Gerraty  *
166956e45f6SSimon J. Gerraty  * The name of the shell is the basename for the predefined shells, such as
167956e45f6SSimon J. Gerraty  * "sh", "csh", "bash".  For custom shells, it is the full pathname, and its
168956e45f6SSimon J. Gerraty  * basename is used to select the type of shell; the longest match wins.
169956e45f6SSimon J. Gerraty  * So /usr/pkg/bin/bash has type sh, /usr/local/bin/tcsh has type csh.
170956e45f6SSimon J. Gerraty  *
171956e45f6SSimon J. Gerraty  * The echoing of command lines is controlled using hasEchoCtl, echoOff,
172956e45f6SSimon J. Gerraty  * echoOn, noPrint and noPrintLen.  When echoOff is executed by the shell, it
173956e45f6SSimon J. Gerraty  * still outputs something, but this something is not interesting, therefore
174956e45f6SSimon J. Gerraty  * it is filtered out using noPrint and noPrintLen.
175956e45f6SSimon J. Gerraty  *
176956e45f6SSimon J. Gerraty  * The error checking for individual commands is controlled using hasErrCtl,
17706b9b3e0SSimon J. Gerraty  * errOn, errOff and runChkTmpl.
178956e45f6SSimon J. Gerraty  *
179dba7b0efSSimon J. Gerraty  * In case a shell doesn't have error control, echoTmpl is a printf template
180dba7b0efSSimon J. Gerraty  * for echoing the command, should echoing be on; runIgnTmpl is another
181dba7b0efSSimon J. Gerraty  * printf template for executing the command while ignoring the return
18206b9b3e0SSimon J. Gerraty  * status. Finally runChkTmpl is a printf template for running the command and
183956e45f6SSimon J. Gerraty  * causing the shell to exit on error. If any of these strings are empty when
184b0c40a00SSimon J. Gerraty  * hasErrCtl is false, the command will be executed anyway as is, and if it
185956e45f6SSimon J. Gerraty  * causes an error, so be it. Any templates set up to echo the command will
186dba7b0efSSimon J. Gerraty  * escape any '$ ` \ "' characters in the command string to avoid unwanted
187dba7b0efSSimon J. Gerraty  * shell code injection, the escaped command is safe to use in double quotes.
188956e45f6SSimon J. Gerraty  *
189956e45f6SSimon J. Gerraty  * The command-line flags "echo" and "exit" also control the behavior.  The
190956e45f6SSimon J. Gerraty  * "echo" flag causes the shell to start echoing commands right away.  The
191956e45f6SSimon J. Gerraty  * "exit" flag causes the shell to exit when an error is detected in one of
192956e45f6SSimon J. Gerraty  * the commands.
193956e45f6SSimon J. Gerraty  */
194956e45f6SSimon J. Gerraty typedef struct Shell {
195956e45f6SSimon J. Gerraty 
19606b9b3e0SSimon J. Gerraty 	/*
19706b9b3e0SSimon J. Gerraty 	 * The name of the shell. For Bourne and C shells, this is used only
19806b9b3e0SSimon J. Gerraty 	 * to find the shell description when used as the single source of a
19906b9b3e0SSimon J. Gerraty 	 * .SHELL target. For user-defined shells, this is the full path of
20006b9b3e0SSimon J. Gerraty 	 * the shell.
20106b9b3e0SSimon J. Gerraty 	 */
202956e45f6SSimon J. Gerraty 	const char *name;
203956e45f6SSimon J. Gerraty 
204b0c40a00SSimon J. Gerraty 	bool hasEchoCtl;	/* whether both echoOff and echoOn are there */
20506b9b3e0SSimon J. Gerraty 	const char *echoOff;	/* command to turn echoing off */
20606b9b3e0SSimon J. Gerraty 	const char *echoOn;	/* command to turn echoing back on */
20706b9b3e0SSimon J. Gerraty 	const char *noPrint;	/* text to skip when printing output from the
208956e45f6SSimon J. Gerraty 				 * shell. This is usually the same as echoOff */
209956e45f6SSimon J. Gerraty 	size_t noPrintLen;	/* length of noPrint command */
210956e45f6SSimon J. Gerraty 
211b0c40a00SSimon J. Gerraty 	bool hasErrCtl;		/* whether error checking can be controlled
21206b9b3e0SSimon J. Gerraty 				 * for individual commands */
21306b9b3e0SSimon J. Gerraty 	const char *errOn;	/* command to turn on error checking */
21406b9b3e0SSimon J. Gerraty 	const char *errOff;	/* command to turn off error checking */
21506b9b3e0SSimon J. Gerraty 
21606b9b3e0SSimon J. Gerraty 	const char *echoTmpl;	/* template to echo a command */
2179f45a3c8SSimon J. Gerraty 	const char *runIgnTmpl;	/* template to run a command without error
2189f45a3c8SSimon J. Gerraty 				 * checking */
2199f45a3c8SSimon J. Gerraty 	const char *runChkTmpl;	/* template to run a command with error
2209f45a3c8SSimon J. Gerraty 				 * checking */
221956e45f6SSimon J. Gerraty 
2229f45a3c8SSimon J. Gerraty 	/*
2239f45a3c8SSimon J. Gerraty 	 * A string literal that results in a newline character when it
2249f45a3c8SSimon J. Gerraty 	 * occurs outside of any 'quote' or "quote" characters.
2259f45a3c8SSimon J. Gerraty 	 */
226956e45f6SSimon J. Gerraty 	const char *newline;
227956e45f6SSimon J. Gerraty 	char commentChar;	/* character used by shell for comment lines */
228956e45f6SSimon J. Gerraty 
22906b9b3e0SSimon J. Gerraty 	const char *echoFlag;	/* shell flag to echo commands */
23006b9b3e0SSimon J. Gerraty 	const char *errFlag;	/* shell flag to exit on error */
231956e45f6SSimon J. Gerraty } Shell;
2323955d011SMarcel Moolenaar 
23306b9b3e0SSimon J. Gerraty typedef struct CommandFlags {
234dba7b0efSSimon J. Gerraty 	/* Whether to echo the command before or instead of running it. */
235b0c40a00SSimon J. Gerraty 	bool echo;
23606b9b3e0SSimon J. Gerraty 
23706b9b3e0SSimon J. Gerraty 	/* Run the command even in -n or -N mode. */
238b0c40a00SSimon J. Gerraty 	bool always;
23906b9b3e0SSimon J. Gerraty 
24006b9b3e0SSimon J. Gerraty 	/*
241b0c40a00SSimon J. Gerraty 	 * true if we turned error checking off before writing the command to
242b0c40a00SSimon J. Gerraty 	 * the commands file and need to turn it back on
24306b9b3e0SSimon J. Gerraty 	 */
244b0c40a00SSimon J. Gerraty 	bool ignerr;
24506b9b3e0SSimon J. Gerraty } CommandFlags;
24606b9b3e0SSimon J. Gerraty 
24706b9b3e0SSimon J. Gerraty /*
24806b9b3e0SSimon J. Gerraty  * Write shell commands to a file.
24906b9b3e0SSimon J. Gerraty  *
25006b9b3e0SSimon J. Gerraty  * TODO: keep track of whether commands are echoed.
25106b9b3e0SSimon J. Gerraty  * TODO: keep track of whether error checking is active.
25206b9b3e0SSimon J. Gerraty  */
25306b9b3e0SSimon J. Gerraty typedef struct ShellWriter {
25406b9b3e0SSimon J. Gerraty 	FILE *f;
25506b9b3e0SSimon J. Gerraty 
25606b9b3e0SSimon J. Gerraty 	/* we've sent 'set -x' */
257b0c40a00SSimon J. Gerraty 	bool xtraced;
25806b9b3e0SSimon J. Gerraty 
25906b9b3e0SSimon J. Gerraty } ShellWriter;
26006b9b3e0SSimon J. Gerraty 
2613955d011SMarcel Moolenaar /*
2629a4bc556SSimon J. Gerraty  * FreeBSD: traditionally .MAKE is not required to
2639a4bc556SSimon J. Gerraty  * pass jobs queue to sub-makes.
2649a4bc556SSimon J. Gerraty  * Use .MAKE.ALWAYS_PASS_JOB_QUEUE=no to disable.
2659a4bc556SSimon J. Gerraty  */
2663b96abbaSSimon J. Gerraty #define MAKE_ALWAYS_PASS_JOB_QUEUE "${.MAKE.ALWAYS_PASS_JOB_QUEUE:U}"
26768c4481aSSimon J. Gerraty static bool Always_pass_job_queue = true;
2682d395cb5SSimon J. Gerraty /*
2692d395cb5SSimon J. Gerraty  * FreeBSD: aborting entire parallel make isn't always
2702d395cb5SSimon J. Gerraty  * desired. When doing tinderbox for example, failure of
2712d395cb5SSimon J. Gerraty  * one architecture should not stop all.
2722d395cb5SSimon J. Gerraty  * We still want to bail on interrupt though.
2732d395cb5SSimon J. Gerraty  */
2743b96abbaSSimon J. Gerraty #define MAKE_JOB_ERROR_TOKEN "${MAKE_JOB_ERROR_TOKEN:U}"
27568c4481aSSimon J. Gerraty static bool Job_error_token = true;
2769a4bc556SSimon J. Gerraty 
2779a4bc556SSimon J. Gerraty /*
2783955d011SMarcel Moolenaar  * error handling variables
2793955d011SMarcel Moolenaar  */
28006b9b3e0SSimon J. Gerraty static int job_errors = 0;	/* number of errors reported */
281b0c40a00SSimon J. Gerraty static enum {			/* Why is the make aborting? */
282956e45f6SSimon J. Gerraty 	ABORT_NONE,
283b0c40a00SSimon J. Gerraty 	ABORT_ERROR,		/* Aborted because of an error */
284b0c40a00SSimon J. Gerraty 	ABORT_INTERRUPT,	/* Aborted because it was interrupted */
285956e45f6SSimon J. Gerraty 	ABORT_WAIT		/* Waiting for jobs to finish */
286b0c40a00SSimon J. Gerraty } aborting = ABORT_NONE;
2873955d011SMarcel Moolenaar #define JOB_TOKENS "+EI+"	/* Token to requeue for each abort state */
2883955d011SMarcel Moolenaar 
2893955d011SMarcel Moolenaar /*
2903955d011SMarcel Moolenaar  * this tracks the number of tokens currently "out" to build jobs.
2913955d011SMarcel Moolenaar  */
2923955d011SMarcel Moolenaar int jobTokensRunning = 0;
2933955d011SMarcel Moolenaar 
294956e45f6SSimon J. Gerraty typedef enum JobStartResult {
295956e45f6SSimon J. Gerraty 	JOB_RUNNING,		/* Job is running */
296956e45f6SSimon J. Gerraty 	JOB_ERROR,		/* Error in starting the job */
297956e45f6SSimon J. Gerraty 	JOB_FINISHED		/* The job is already finished */
298956e45f6SSimon J. Gerraty } JobStartResult;
2993955d011SMarcel Moolenaar 
3003955d011SMarcel Moolenaar /*
3013955d011SMarcel Moolenaar  * Descriptions for various shells.
3023955d011SMarcel Moolenaar  *
3033955d011SMarcel Moolenaar  * The build environment may set DEFSHELL_INDEX to one of
3043955d011SMarcel Moolenaar  * DEFSHELL_INDEX_SH, DEFSHELL_INDEX_KSH, or DEFSHELL_INDEX_CSH, to
305956e45f6SSimon J. Gerraty  * select one of the predefined shells as the default shell.
3063955d011SMarcel Moolenaar  *
3073955d011SMarcel Moolenaar  * Alternatively, the build environment may set DEFSHELL_CUSTOM to the
3083955d011SMarcel Moolenaar  * name or the full path of a sh-compatible shell, which will be used as
3093955d011SMarcel Moolenaar  * the default shell.
3103955d011SMarcel Moolenaar  *
3113955d011SMarcel Moolenaar  * ".SHELL" lines in Makefiles can choose the default shell from the
312956e45f6SSimon J. Gerraty  * set defined here, or add additional shells.
3133955d011SMarcel Moolenaar  */
3143955d011SMarcel Moolenaar 
3153955d011SMarcel Moolenaar #ifdef DEFSHELL_CUSTOM
3163955d011SMarcel Moolenaar #define DEFSHELL_INDEX_CUSTOM 0
3173955d011SMarcel Moolenaar #define DEFSHELL_INDEX_SH     1
3183955d011SMarcel Moolenaar #define DEFSHELL_INDEX_KSH    2
3193955d011SMarcel Moolenaar #define DEFSHELL_INDEX_CSH    3
3203955d011SMarcel Moolenaar #else /* !DEFSHELL_CUSTOM */
3213955d011SMarcel Moolenaar #define DEFSHELL_INDEX_SH     0
3223955d011SMarcel Moolenaar #define DEFSHELL_INDEX_KSH    1
3233955d011SMarcel Moolenaar #define DEFSHELL_INDEX_CSH    2
3243955d011SMarcel Moolenaar #endif /* !DEFSHELL_CUSTOM */
3253955d011SMarcel Moolenaar 
3263955d011SMarcel Moolenaar #ifndef DEFSHELL_INDEX
3273955d011SMarcel Moolenaar #define DEFSHELL_INDEX 0	/* DEFSHELL_INDEX_CUSTOM or DEFSHELL_INDEX_SH */
3283955d011SMarcel Moolenaar #endif /* !DEFSHELL_INDEX */
3293955d011SMarcel Moolenaar 
3303955d011SMarcel Moolenaar static Shell shells[] = {
3313955d011SMarcel Moolenaar #ifdef DEFSHELL_CUSTOM
3323955d011SMarcel Moolenaar     /*
3333955d011SMarcel Moolenaar      * An sh-compatible shell with a non-standard name.
3343955d011SMarcel Moolenaar      *
3353955d011SMarcel Moolenaar      * Keep this in sync with the "sh" description below, but avoid
3363955d011SMarcel Moolenaar      * non-portable features that might not be supplied by all
3373955d011SMarcel Moolenaar      * sh-compatible shells.
3383955d011SMarcel Moolenaar      */
3393955d011SMarcel Moolenaar     {
340956e45f6SSimon J. Gerraty 	DEFSHELL_CUSTOM,	/* .name */
341b0c40a00SSimon J. Gerraty 	false,			/* .hasEchoCtl */
342956e45f6SSimon J. Gerraty 	"",			/* .echoOff */
343956e45f6SSimon J. Gerraty 	"",			/* .echoOn */
344956e45f6SSimon J. Gerraty 	"",			/* .noPrint */
345956e45f6SSimon J. Gerraty 	0,			/* .noPrintLen */
346b0c40a00SSimon J. Gerraty 	false,			/* .hasErrCtl */
34706b9b3e0SSimon J. Gerraty 	"",			/* .errOn */
34806b9b3e0SSimon J. Gerraty 	"",			/* .errOff */
34906b9b3e0SSimon J. Gerraty 	"echo \"%s\"\n",	/* .echoTmpl */
35006b9b3e0SSimon J. Gerraty 	"%s\n",			/* .runIgnTmpl */
35106b9b3e0SSimon J. Gerraty 	"{ %s \n} || exit $?\n", /* .runChkTmpl */
352956e45f6SSimon J. Gerraty 	"'\n'",			/* .newline */
353956e45f6SSimon J. Gerraty 	'#',			/* .commentChar */
35406b9b3e0SSimon J. Gerraty 	"",			/* .echoFlag */
35506b9b3e0SSimon J. Gerraty 	"",			/* .errFlag */
3563955d011SMarcel Moolenaar     },
3573955d011SMarcel Moolenaar #endif /* DEFSHELL_CUSTOM */
3583955d011SMarcel Moolenaar     /*
3593955d011SMarcel Moolenaar      * SH description. Echo control is also possible and, under
3603955d011SMarcel Moolenaar      * sun UNIX anyway, one can even control error checking.
3613955d011SMarcel Moolenaar      */
3623955d011SMarcel Moolenaar     {
363956e45f6SSimon J. Gerraty 	"sh",			/* .name */
364b0c40a00SSimon J. Gerraty 	false,			/* .hasEchoCtl */
365956e45f6SSimon J. Gerraty 	"",			/* .echoOff */
366956e45f6SSimon J. Gerraty 	"",			/* .echoOn */
367956e45f6SSimon J. Gerraty 	"",			/* .noPrint */
368956e45f6SSimon J. Gerraty 	0,			/* .noPrintLen */
369b0c40a00SSimon J. Gerraty 	false,			/* .hasErrCtl */
37006b9b3e0SSimon J. Gerraty 	"",			/* .errOn */
37106b9b3e0SSimon J. Gerraty 	"",			/* .errOff */
37206b9b3e0SSimon J. Gerraty 	"echo \"%s\"\n",	/* .echoTmpl */
37306b9b3e0SSimon J. Gerraty 	"%s\n",			/* .runIgnTmpl */
37406b9b3e0SSimon J. Gerraty 	"{ %s \n} || exit $?\n", /* .runChkTmpl */
375956e45f6SSimon J. Gerraty 	"'\n'",			/* .newline */
376956e45f6SSimon J. Gerraty 	'#',			/* .commentChar*/
3773955d011SMarcel Moolenaar #if defined(MAKE_NATIVE) && defined(__NetBSD__)
37806b9b3e0SSimon J. Gerraty 	/* XXX: -q is not really echoFlag, it's more like noEchoInSysFlag. */
37906b9b3e0SSimon J. Gerraty 	"q",			/* .echoFlag */
3803955d011SMarcel Moolenaar #else
38106b9b3e0SSimon J. Gerraty 	"",			/* .echoFlag */
3823955d011SMarcel Moolenaar #endif
38306b9b3e0SSimon J. Gerraty 	"",			/* .errFlag */
3843955d011SMarcel Moolenaar     },
3853955d011SMarcel Moolenaar     /*
3863955d011SMarcel Moolenaar      * KSH description.
3873955d011SMarcel Moolenaar      */
3883955d011SMarcel Moolenaar     {
389956e45f6SSimon J. Gerraty 	"ksh",			/* .name */
390b0c40a00SSimon J. Gerraty 	true,			/* .hasEchoCtl */
391956e45f6SSimon J. Gerraty 	"set +v",		/* .echoOff */
392956e45f6SSimon J. Gerraty 	"set -v",		/* .echoOn */
393956e45f6SSimon J. Gerraty 	"set +v",		/* .noPrint */
394956e45f6SSimon J. Gerraty 	6,			/* .noPrintLen */
395b0c40a00SSimon J. Gerraty 	false,			/* .hasErrCtl */
39606b9b3e0SSimon J. Gerraty 	"",			/* .errOn */
39706b9b3e0SSimon J. Gerraty 	"",			/* .errOff */
39806b9b3e0SSimon J. Gerraty 	"echo \"%s\"\n",	/* .echoTmpl */
39906b9b3e0SSimon J. Gerraty 	"%s\n",			/* .runIgnTmpl */
40006b9b3e0SSimon J. Gerraty 	"{ %s \n} || exit $?\n", /* .runChkTmpl */
401956e45f6SSimon J. Gerraty 	"'\n'",			/* .newline */
402956e45f6SSimon J. Gerraty 	'#',			/* .commentChar */
40306b9b3e0SSimon J. Gerraty 	"v",			/* .echoFlag */
40406b9b3e0SSimon J. Gerraty 	"",			/* .errFlag */
4053955d011SMarcel Moolenaar     },
4063955d011SMarcel Moolenaar     /*
4073955d011SMarcel Moolenaar      * CSH description. The csh can do echo control by playing
4083955d011SMarcel Moolenaar      * with the setting of the 'echo' shell variable. Sadly,
4093955d011SMarcel Moolenaar      * however, it is unable to do error control nicely.
4103955d011SMarcel Moolenaar      */
4113955d011SMarcel Moolenaar     {
412956e45f6SSimon J. Gerraty 	"csh",			/* .name */
413b0c40a00SSimon J. Gerraty 	true,			/* .hasEchoCtl */
414956e45f6SSimon J. Gerraty 	"unset verbose",	/* .echoOff */
415956e45f6SSimon J. Gerraty 	"set verbose",		/* .echoOn */
416956e45f6SSimon J. Gerraty 	"unset verbose",	/* .noPrint */
417956e45f6SSimon J. Gerraty 	13,			/* .noPrintLen */
418b0c40a00SSimon J. Gerraty 	false,			/* .hasErrCtl */
41906b9b3e0SSimon J. Gerraty 	"",			/* .errOn */
42006b9b3e0SSimon J. Gerraty 	"",			/* .errOff */
42106b9b3e0SSimon J. Gerraty 	"echo \"%s\"\n",	/* .echoTmpl */
42206b9b3e0SSimon J. Gerraty 	"csh -c \"%s || exit 0\"\n", /* .runIgnTmpl */
42306b9b3e0SSimon J. Gerraty 	"",			/* .runChkTmpl */
424956e45f6SSimon J. Gerraty 	"'\\\n'",		/* .newline */
425956e45f6SSimon J. Gerraty 	'#',			/* .commentChar */
42606b9b3e0SSimon J. Gerraty 	"v",			/* .echoFlag */
42706b9b3e0SSimon J. Gerraty 	"e",			/* .errFlag */
4283955d011SMarcel Moolenaar     }
4293955d011SMarcel Moolenaar };
430956e45f6SSimon J. Gerraty 
43106b9b3e0SSimon J. Gerraty /*
43206b9b3e0SSimon J. Gerraty  * This is the shell to which we pass all commands in the Makefile.
43306b9b3e0SSimon J. Gerraty  * It is set by the Job_ParseShell function.
43406b9b3e0SSimon J. Gerraty  */
43506b9b3e0SSimon J. Gerraty static Shell *shell = &shells[DEFSHELL_INDEX];
436956e45f6SSimon J. Gerraty const char *shellPath = NULL;	/* full pathname of executable image */
437956e45f6SSimon J. Gerraty const char *shellName = NULL;	/* last component of shellPath */
43851ee2c1cSSimon J. Gerraty char *shellErrFlag = NULL;
439dba7b0efSSimon J. Gerraty static char *shell_freeIt = NULL; /* Allocated memory for custom .SHELL */
4403955d011SMarcel Moolenaar 
4413955d011SMarcel Moolenaar 
442956e45f6SSimon J. Gerraty static Job *job_table;		/* The structures that describe them */
443956e45f6SSimon J. Gerraty static Job *job_table_end;	/* job_table + maxJobs */
444956e45f6SSimon J. Gerraty static unsigned int wantToken;	/* we want a token */
445b0c40a00SSimon J. Gerraty static bool lurking_children = false;
446b0c40a00SSimon J. Gerraty static bool make_suspended = false; /* Whether we've seen a SIGTSTP (etc) */
4473955d011SMarcel Moolenaar 
4483955d011SMarcel Moolenaar /*
4493955d011SMarcel Moolenaar  * Set of descriptors of pipes connected to
4503955d011SMarcel Moolenaar  * the output channels of children
4513955d011SMarcel Moolenaar  */
4523955d011SMarcel Moolenaar static struct pollfd *fds = NULL;
453dba7b0efSSimon J. Gerraty static Job **jobByFdIndex = NULL;
454dba7b0efSSimon J. Gerraty static nfds_t fdsLen = 0;
4553955d011SMarcel Moolenaar static void watchfd(Job *);
4563955d011SMarcel Moolenaar static void clearfd(Job *);
457b0c40a00SSimon J. Gerraty static bool readyfd(Job *);
4583955d011SMarcel Moolenaar 
45906b9b3e0SSimon J. Gerraty static char *targPrefix = NULL;	/* To identify a job change in the output. */
4603955d011SMarcel Moolenaar static Job tokenWaitJob;	/* token wait pseudo-job */
4613955d011SMarcel Moolenaar 
4623955d011SMarcel Moolenaar static Job childExitJob;	/* child exit pseudo-job */
4633955d011SMarcel Moolenaar #define CHILD_EXIT "."
4643955d011SMarcel Moolenaar #define DO_JOB_RESUME "R"
4653955d011SMarcel Moolenaar 
46606b9b3e0SSimon J. Gerraty enum {
46706b9b3e0SSimon J. Gerraty 	npseudojobs = 2		/* number of pseudo-jobs */
46806b9b3e0SSimon J. Gerraty };
4693955d011SMarcel Moolenaar 
4703955d011SMarcel Moolenaar static sigset_t caught_signals;	/* Set of signals we handle */
471dba7b0efSSimon J. Gerraty static volatile sig_atomic_t caught_sigchld;
4723955d011SMarcel Moolenaar 
473b0c40a00SSimon J. Gerraty static void CollectOutput(Job *, bool);
474b0c40a00SSimon J. Gerraty static void JobInterrupt(bool, int) MAKE_ATTR_DEAD;
4753955d011SMarcel Moolenaar static void JobRestartJobs(void);
4763955d011SMarcel Moolenaar static void JobSigReset(void);
4773955d011SMarcel Moolenaar 
47806b9b3e0SSimon J. Gerraty static void
47906b9b3e0SSimon J. Gerraty SwitchOutputTo(GNode *gn)
48006b9b3e0SSimon J. Gerraty {
48106b9b3e0SSimon J. Gerraty 	/* The node for which output was most recently produced. */
48206b9b3e0SSimon J. Gerraty 	static GNode *lastNode = NULL;
48306b9b3e0SSimon J. Gerraty 
48406b9b3e0SSimon J. Gerraty 	if (gn == lastNode)
48506b9b3e0SSimon J. Gerraty 		return;
48606b9b3e0SSimon J. Gerraty 	lastNode = gn;
48706b9b3e0SSimon J. Gerraty 
48806b9b3e0SSimon J. Gerraty 	if (opts.maxJobs != 1 && targPrefix != NULL && targPrefix[0] != '\0')
48906b9b3e0SSimon J. Gerraty 		(void)fprintf(stdout, "%s %s ---\n", targPrefix, gn->name);
49006b9b3e0SSimon J. Gerraty }
49106b9b3e0SSimon J. Gerraty 
49249caa483SSimon J. Gerraty static unsigned
49349caa483SSimon J. Gerraty nfds_per_job(void)
49449caa483SSimon J. Gerraty {
49549caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
49649caa483SSimon J. Gerraty 	if (useMeta)
49749caa483SSimon J. Gerraty 		return 2;
49849caa483SSimon J. Gerraty #endif
49949caa483SSimon J. Gerraty 	return 1;
50049caa483SSimon J. Gerraty }
50149caa483SSimon J. Gerraty 
50206b9b3e0SSimon J. Gerraty void
50306b9b3e0SSimon J. Gerraty Job_FlagsToString(const Job *job, char *buf, size_t bufsize)
50406b9b3e0SSimon J. Gerraty {
50506b9b3e0SSimon J. Gerraty 	snprintf(buf, bufsize, "%c%c%c",
50606b9b3e0SSimon J. Gerraty 	    job->ignerr ? 'i' : '-',
50706b9b3e0SSimon J. Gerraty 	    !job->echo ? 's' : '-',
50806b9b3e0SSimon J. Gerraty 	    job->special ? 'S' : '-');
50906b9b3e0SSimon J. Gerraty }
51006b9b3e0SSimon J. Gerraty 
5113955d011SMarcel Moolenaar static void
512b0c40a00SSimon J. Gerraty DumpJobs(const char *where)
5133955d011SMarcel Moolenaar {
5143955d011SMarcel Moolenaar 	Job *job;
51506b9b3e0SSimon J. Gerraty 	char flags[4];
5163955d011SMarcel Moolenaar 
517956e45f6SSimon J. Gerraty 	debug_printf("job table @ %s\n", where);
5183955d011SMarcel Moolenaar 	for (job = job_table; job < job_table_end; job++) {
51906b9b3e0SSimon J. Gerraty 		Job_FlagsToString(job, flags, sizeof flags);
52006b9b3e0SSimon J. Gerraty 		debug_printf("job %d, status %d, flags %s, pid %d\n",
52106b9b3e0SSimon J. Gerraty 		    (int)(job - job_table), job->status, flags, job->pid);
5223955d011SMarcel Moolenaar 	}
5233955d011SMarcel Moolenaar }
5243955d011SMarcel Moolenaar 
5253955d011SMarcel Moolenaar /*
52645447996SSimon J. Gerraty  * Delete the target of a failed, interrupted, or otherwise
52745447996SSimon J. Gerraty  * unsuccessful job unless inhibited by .PRECIOUS.
52845447996SSimon J. Gerraty  */
52945447996SSimon J. Gerraty static void
53045447996SSimon J. Gerraty JobDeleteTarget(GNode *gn)
53145447996SSimon J. Gerraty {
532956e45f6SSimon J. Gerraty 	const char *file;
533956e45f6SSimon J. Gerraty 
534956e45f6SSimon J. Gerraty 	if (gn->type & OP_JOIN)
535956e45f6SSimon J. Gerraty 		return;
536956e45f6SSimon J. Gerraty 	if (gn->type & OP_PHONY)
537956e45f6SSimon J. Gerraty 		return;
5389f45a3c8SSimon J. Gerraty 	if (GNode_IsPrecious(gn))
539956e45f6SSimon J. Gerraty 		return;
540956e45f6SSimon J. Gerraty 	if (opts.noExecute)
541956e45f6SSimon J. Gerraty 		return;
542956e45f6SSimon J. Gerraty 
543956e45f6SSimon J. Gerraty 	file = GNode_Path(gn);
5449f45a3c8SSimon J. Gerraty 	if (unlink_file(file))
54545447996SSimon J. Gerraty 		Error("*** %s removed", file);
54645447996SSimon J. Gerraty }
54745447996SSimon J. Gerraty 
54845447996SSimon J. Gerraty /*
5493955d011SMarcel Moolenaar  * JobSigLock/JobSigUnlock
5503955d011SMarcel Moolenaar  *
5513955d011SMarcel Moolenaar  * Signal lock routines to get exclusive access. Currently used to
5523955d011SMarcel Moolenaar  * protect `jobs' and `stoppedJobs' list manipulations.
5533955d011SMarcel Moolenaar  */
55406b9b3e0SSimon J. Gerraty static void
55506b9b3e0SSimon J. Gerraty JobSigLock(sigset_t *omaskp)
5563955d011SMarcel Moolenaar {
5573955d011SMarcel Moolenaar 	if (sigprocmask(SIG_BLOCK, &caught_signals, omaskp) != 0) {
5583955d011SMarcel Moolenaar 		Punt("JobSigLock: sigprocmask: %s", strerror(errno));
5593955d011SMarcel Moolenaar 		sigemptyset(omaskp);
5603955d011SMarcel Moolenaar 	}
5613955d011SMarcel Moolenaar }
5623955d011SMarcel Moolenaar 
56306b9b3e0SSimon J. Gerraty static void
56406b9b3e0SSimon J. Gerraty JobSigUnlock(sigset_t *omaskp)
5653955d011SMarcel Moolenaar {
5663955d011SMarcel Moolenaar 	(void)sigprocmask(SIG_SETMASK, omaskp, NULL);
5673955d011SMarcel Moolenaar }
5683955d011SMarcel Moolenaar 
5693955d011SMarcel Moolenaar static void
5703955d011SMarcel Moolenaar JobCreatePipe(Job *job, int minfd)
5713955d011SMarcel Moolenaar {
572e1cee40dSSimon J. Gerraty 	int i, fd, flags;
573956e45f6SSimon J. Gerraty 	int pipe_fds[2];
5743955d011SMarcel Moolenaar 
575956e45f6SSimon J. Gerraty 	if (pipe(pipe_fds) == -1)
5763955d011SMarcel Moolenaar 		Punt("Cannot create pipe: %s", strerror(errno));
5773955d011SMarcel Moolenaar 
57874d2e02bSSimon J. Gerraty 	for (i = 0; i < 2; i++) {
57974d2e02bSSimon J. Gerraty 		/* Avoid using low numbered fds */
580956e45f6SSimon J. Gerraty 		fd = fcntl(pipe_fds[i], F_DUPFD, minfd);
58174d2e02bSSimon J. Gerraty 		if (fd != -1) {
582956e45f6SSimon J. Gerraty 			close(pipe_fds[i]);
583956e45f6SSimon J. Gerraty 			pipe_fds[i] = fd;
58474d2e02bSSimon J. Gerraty 		}
58574d2e02bSSimon J. Gerraty 	}
58674d2e02bSSimon J. Gerraty 
587956e45f6SSimon J. Gerraty 	job->inPipe = pipe_fds[0];
588956e45f6SSimon J. Gerraty 	job->outPipe = pipe_fds[1];
589956e45f6SSimon J. Gerraty 
5903955d011SMarcel Moolenaar 	/* Set close-on-exec flag for both */
591956e45f6SSimon J. Gerraty 	if (fcntl(job->inPipe, F_SETFD, FD_CLOEXEC) == -1)
592e1cee40dSSimon J. Gerraty 		Punt("Cannot set close-on-exec: %s", strerror(errno));
593956e45f6SSimon J. Gerraty 	if (fcntl(job->outPipe, F_SETFD, FD_CLOEXEC) == -1)
594e1cee40dSSimon J. Gerraty 		Punt("Cannot set close-on-exec: %s", strerror(errno));
5953955d011SMarcel Moolenaar 
5963955d011SMarcel Moolenaar 	/*
5973955d011SMarcel Moolenaar 	 * We mark the input side of the pipe non-blocking; we poll(2) the
5983955d011SMarcel Moolenaar 	 * pipe when we're waiting for a job token, but we might lose the
5993955d011SMarcel Moolenaar 	 * race for the token when a new one becomes available, so the read
6003955d011SMarcel Moolenaar 	 * from the pipe should not block.
6013955d011SMarcel Moolenaar 	 */
602956e45f6SSimon J. Gerraty 	flags = fcntl(job->inPipe, F_GETFL, 0);
603e1cee40dSSimon J. Gerraty 	if (flags == -1)
604e1cee40dSSimon J. Gerraty 		Punt("Cannot get flags: %s", strerror(errno));
605e1cee40dSSimon J. Gerraty 	flags |= O_NONBLOCK;
606956e45f6SSimon J. Gerraty 	if (fcntl(job->inPipe, F_SETFL, flags) == -1)
607e1cee40dSSimon J. Gerraty 		Punt("Cannot set flags: %s", strerror(errno));
6083955d011SMarcel Moolenaar }
6093955d011SMarcel Moolenaar 
610956e45f6SSimon J. Gerraty /* Pass the signal to each running job. */
6113955d011SMarcel Moolenaar static void
6123955d011SMarcel Moolenaar JobCondPassSig(int signo)
6133955d011SMarcel Moolenaar {
6143955d011SMarcel Moolenaar 	Job *job;
6153955d011SMarcel Moolenaar 
616956e45f6SSimon J. Gerraty 	DEBUG1(JOB, "JobCondPassSig(%d) called.\n", signo);
6173955d011SMarcel Moolenaar 
6183955d011SMarcel Moolenaar 	for (job = job_table; job < job_table_end; job++) {
619e2eeea75SSimon J. Gerraty 		if (job->status != JOB_ST_RUNNING)
6203955d011SMarcel Moolenaar 			continue;
621956e45f6SSimon J. Gerraty 		DEBUG2(JOB, "JobCondPassSig passing signal %d to child %d.\n",
6223955d011SMarcel Moolenaar 		    signo, job->pid);
6233955d011SMarcel Moolenaar 		KILLPG(job->pid, signo);
6243955d011SMarcel Moolenaar 	}
6253955d011SMarcel Moolenaar }
6263955d011SMarcel Moolenaar 
62706b9b3e0SSimon J. Gerraty /*
62806b9b3e0SSimon J. Gerraty  * SIGCHLD handler.
6293955d011SMarcel Moolenaar  *
63006b9b3e0SSimon J. Gerraty  * Sends a token on the child exit pipe to wake us up from select()/poll().
63106b9b3e0SSimon J. Gerraty  */
63206b9b3e0SSimon J. Gerraty /*ARGSUSED*/
6333955d011SMarcel Moolenaar static void
6343955d011SMarcel Moolenaar JobChildSig(int signo MAKE_ATTR_UNUSED)
6353955d011SMarcel Moolenaar {
636dba7b0efSSimon J. Gerraty 	caught_sigchld = 1;
63706b9b3e0SSimon J. Gerraty 	while (write(childExitJob.outPipe, CHILD_EXIT, 1) == -1 &&
63806b9b3e0SSimon J. Gerraty 	       errno == EAGAIN)
6393cbdda60SSimon J. Gerraty 		continue;
6403955d011SMarcel Moolenaar }
6413955d011SMarcel Moolenaar 
6423955d011SMarcel Moolenaar 
643956e45f6SSimon J. Gerraty /* Resume all stopped jobs. */
64406b9b3e0SSimon J. Gerraty /*ARGSUSED*/
6453955d011SMarcel Moolenaar static void
6463955d011SMarcel Moolenaar JobContinueSig(int signo MAKE_ATTR_UNUSED)
6473955d011SMarcel Moolenaar {
6483955d011SMarcel Moolenaar 	/*
649956e45f6SSimon J. Gerraty 	 * Defer sending SIGCONT to our stopped children until we return
6503955d011SMarcel Moolenaar 	 * from the signal handler.
6513955d011SMarcel Moolenaar 	 */
6523cbdda60SSimon J. Gerraty 	while (write(childExitJob.outPipe, DO_JOB_RESUME, 1) == -1 &&
6533cbdda60SSimon J. Gerraty 	       errno == EAGAIN)
6543cbdda60SSimon J. Gerraty 		continue;
6553955d011SMarcel Moolenaar }
6563955d011SMarcel Moolenaar 
65706b9b3e0SSimon J. Gerraty /*
65806b9b3e0SSimon J. Gerraty  * Pass a signal on to all jobs, then resend to ourselves.
65906b9b3e0SSimon J. Gerraty  * We die by the same signal.
66006b9b3e0SSimon J. Gerraty  */
6613955d011SMarcel Moolenaar MAKE_ATTR_DEAD static void
6623955d011SMarcel Moolenaar JobPassSig_int(int signo)
6633955d011SMarcel Moolenaar {
6643955d011SMarcel Moolenaar 	/* Run .INTERRUPT target then exit */
665b0c40a00SSimon J. Gerraty 	JobInterrupt(true, signo);
6663955d011SMarcel Moolenaar }
6673955d011SMarcel Moolenaar 
66806b9b3e0SSimon J. Gerraty /*
66906b9b3e0SSimon J. Gerraty  * Pass a signal on to all jobs, then resend to ourselves.
67006b9b3e0SSimon J. Gerraty  * We die by the same signal.
67106b9b3e0SSimon J. Gerraty  */
6723955d011SMarcel Moolenaar MAKE_ATTR_DEAD static void
6733955d011SMarcel Moolenaar JobPassSig_term(int signo)
6743955d011SMarcel Moolenaar {
6753955d011SMarcel Moolenaar 	/* Dont run .INTERRUPT target then exit */
676b0c40a00SSimon J. Gerraty 	JobInterrupt(false, signo);
6773955d011SMarcel Moolenaar }
6783955d011SMarcel Moolenaar 
6793955d011SMarcel Moolenaar static void
6803955d011SMarcel Moolenaar JobPassSig_suspend(int signo)
6813955d011SMarcel Moolenaar {
6823955d011SMarcel Moolenaar 	sigset_t nmask, omask;
6833955d011SMarcel Moolenaar 	struct sigaction act;
6843955d011SMarcel Moolenaar 
6853955d011SMarcel Moolenaar 	/* Suppress job started/continued messages */
686b0c40a00SSimon J. Gerraty 	make_suspended = true;
6873955d011SMarcel Moolenaar 
6883955d011SMarcel Moolenaar 	/* Pass the signal onto every job */
6893955d011SMarcel Moolenaar 	JobCondPassSig(signo);
6903955d011SMarcel Moolenaar 
6913955d011SMarcel Moolenaar 	/*
69206b9b3e0SSimon J. Gerraty 	 * Send ourselves the signal now we've given the message to everyone
69306b9b3e0SSimon J. Gerraty 	 * else. Note we block everything else possible while we're getting
69406b9b3e0SSimon J. Gerraty 	 * the signal. This ensures that all our jobs get continued when we
69506b9b3e0SSimon J. Gerraty 	 * wake up before we take any other signal.
6963955d011SMarcel Moolenaar 	 */
6973955d011SMarcel Moolenaar 	sigfillset(&nmask);
6983955d011SMarcel Moolenaar 	sigdelset(&nmask, signo);
6993955d011SMarcel Moolenaar 	(void)sigprocmask(SIG_SETMASK, &nmask, &omask);
7003955d011SMarcel Moolenaar 
7013955d011SMarcel Moolenaar 	act.sa_handler = SIG_DFL;
7023955d011SMarcel Moolenaar 	sigemptyset(&act.sa_mask);
7033955d011SMarcel Moolenaar 	act.sa_flags = 0;
7043955d011SMarcel Moolenaar 	(void)sigaction(signo, &act, NULL);
7053955d011SMarcel Moolenaar 
70606b9b3e0SSimon J. Gerraty 	DEBUG1(JOB, "JobPassSig passing signal %d to self.\n", signo);
7073955d011SMarcel Moolenaar 
7083955d011SMarcel Moolenaar 	(void)kill(getpid(), signo);
7093955d011SMarcel Moolenaar 
7103955d011SMarcel Moolenaar 	/*
7113955d011SMarcel Moolenaar 	 * We've been continued.
7123955d011SMarcel Moolenaar 	 *
7133955d011SMarcel Moolenaar 	 * A whole host of signals continue to happen!
7143955d011SMarcel Moolenaar 	 * SIGCHLD for any processes that actually suspended themselves.
7153955d011SMarcel Moolenaar 	 * SIGCHLD for any processes that exited while we were alseep.
7163955d011SMarcel Moolenaar 	 * The SIGCONT that actually caused us to wakeup.
7173955d011SMarcel Moolenaar 	 *
7183955d011SMarcel Moolenaar 	 * Since we defer passing the SIGCONT on to our children until
7193955d011SMarcel Moolenaar 	 * the main processing loop, we can be sure that all the SIGCHLD
7203955d011SMarcel Moolenaar 	 * events will have happened by then - and that the waitpid() will
7213955d011SMarcel Moolenaar 	 * collect the child 'suspended' events.
7223955d011SMarcel Moolenaar 	 * For correct sequencing we just need to ensure we process the
723956e45f6SSimon J. Gerraty 	 * waitpid() before passing on the SIGCONT.
7243955d011SMarcel Moolenaar 	 *
7253955d011SMarcel Moolenaar 	 * In any case nothing else is needed here.
7263955d011SMarcel Moolenaar 	 */
7273955d011SMarcel Moolenaar 
7283955d011SMarcel Moolenaar 	/* Restore handler and signal mask */
7293955d011SMarcel Moolenaar 	act.sa_handler = JobPassSig_suspend;
7303955d011SMarcel Moolenaar 	(void)sigaction(signo, &act, NULL);
7313955d011SMarcel Moolenaar 	(void)sigprocmask(SIG_SETMASK, &omask, NULL);
7323955d011SMarcel Moolenaar }
7333955d011SMarcel Moolenaar 
7343955d011SMarcel Moolenaar static Job *
735b0c40a00SSimon J. Gerraty JobFindPid(int pid, JobStatus status, bool isJobs)
7363955d011SMarcel Moolenaar {
7373955d011SMarcel Moolenaar 	Job *job;
7383955d011SMarcel Moolenaar 
7393955d011SMarcel Moolenaar 	for (job = job_table; job < job_table_end; job++) {
740e2eeea75SSimon J. Gerraty 		if (job->status == status && job->pid == pid)
7413955d011SMarcel Moolenaar 			return job;
7423955d011SMarcel Moolenaar 	}
7433955d011SMarcel Moolenaar 	if (DEBUG(JOB) && isJobs)
744b0c40a00SSimon J. Gerraty 		DumpJobs("no pid");
7453955d011SMarcel Moolenaar 	return NULL;
7463955d011SMarcel Moolenaar }
7473955d011SMarcel Moolenaar 
748956e45f6SSimon J. Gerraty /* Parse leading '@', '-' and '+', which control the exact execution mode. */
749956e45f6SSimon J. Gerraty static void
75006b9b3e0SSimon J. Gerraty ParseCommandFlags(char **pp, CommandFlags *out_cmdFlags)
751956e45f6SSimon J. Gerraty {
752956e45f6SSimon J. Gerraty 	char *p = *pp;
753b0c40a00SSimon J. Gerraty 	out_cmdFlags->echo = true;
754b0c40a00SSimon J. Gerraty 	out_cmdFlags->ignerr = false;
755b0c40a00SSimon J. Gerraty 	out_cmdFlags->always = false;
756956e45f6SSimon J. Gerraty 
757956e45f6SSimon J. Gerraty 	for (;;) {
758956e45f6SSimon J. Gerraty 		if (*p == '@')
75906b9b3e0SSimon J. Gerraty 			out_cmdFlags->echo = DEBUG(LOUD);
760956e45f6SSimon J. Gerraty 		else if (*p == '-')
761b0c40a00SSimon J. Gerraty 			out_cmdFlags->ignerr = true;
762956e45f6SSimon J. Gerraty 		else if (*p == '+')
763b0c40a00SSimon J. Gerraty 			out_cmdFlags->always = true;
764956e45f6SSimon J. Gerraty 		else
765956e45f6SSimon J. Gerraty 			break;
766956e45f6SSimon J. Gerraty 		p++;
767956e45f6SSimon J. Gerraty 	}
768956e45f6SSimon J. Gerraty 
769956e45f6SSimon J. Gerraty 	pp_skip_whitespace(&p);
770956e45f6SSimon J. Gerraty 
771956e45f6SSimon J. Gerraty 	*pp = p;
772956e45f6SSimon J. Gerraty }
773956e45f6SSimon J. Gerraty 
774956e45f6SSimon J. Gerraty /* Escape a string for a double-quoted string literal in sh, csh and ksh. */
775956e45f6SSimon J. Gerraty static char *
776956e45f6SSimon J. Gerraty EscapeShellDblQuot(const char *cmd)
777956e45f6SSimon J. Gerraty {
778956e45f6SSimon J. Gerraty 	size_t i, j;
779956e45f6SSimon J. Gerraty 
780956e45f6SSimon J. Gerraty 	/* Worst that could happen is every char needs escaping. */
781956e45f6SSimon J. Gerraty 	char *esc = bmake_malloc(strlen(cmd) * 2 + 1);
782956e45f6SSimon J. Gerraty 	for (i = 0, j = 0; cmd[i] != '\0'; i++, j++) {
78306b9b3e0SSimon J. Gerraty 		if (cmd[i] == '$' || cmd[i] == '`' || cmd[i] == '\\' ||
78406b9b3e0SSimon J. Gerraty 		    cmd[i] == '"')
785956e45f6SSimon J. Gerraty 			esc[j++] = '\\';
786956e45f6SSimon J. Gerraty 		esc[j] = cmd[i];
787956e45f6SSimon J. Gerraty 	}
788956e45f6SSimon J. Gerraty 	esc[j] = '\0';
789956e45f6SSimon J. Gerraty 
790956e45f6SSimon J. Gerraty 	return esc;
791956e45f6SSimon J. Gerraty }
792956e45f6SSimon J. Gerraty 
793e2eeea75SSimon J. Gerraty static void
794b0c40a00SSimon J. Gerraty ShellWriter_WriteFmt(ShellWriter *wr, const char *fmt, const char *arg)
795e2eeea75SSimon J. Gerraty {
79606b9b3e0SSimon J. Gerraty 	DEBUG1(JOB, fmt, arg);
797e2eeea75SSimon J. Gerraty 
79806b9b3e0SSimon J. Gerraty 	(void)fprintf(wr->f, fmt, arg);
79912904384SSimon J. Gerraty 	if (wr->f == stdout)
80006b9b3e0SSimon J. Gerraty 		(void)fflush(wr->f);
801e2eeea75SSimon J. Gerraty }
802e2eeea75SSimon J. Gerraty 
803e2eeea75SSimon J. Gerraty static void
804b0c40a00SSimon J. Gerraty ShellWriter_WriteLine(ShellWriter *wr, const char *line)
805e2eeea75SSimon J. Gerraty {
806b0c40a00SSimon J. Gerraty 	ShellWriter_WriteFmt(wr, "%s\n", line);
807e2eeea75SSimon J. Gerraty }
808e2eeea75SSimon J. Gerraty 
80906b9b3e0SSimon J. Gerraty static void
81006b9b3e0SSimon J. Gerraty ShellWriter_EchoOff(ShellWriter *wr)
81106b9b3e0SSimon J. Gerraty {
81206b9b3e0SSimon J. Gerraty 	if (shell->hasEchoCtl)
813b0c40a00SSimon J. Gerraty 		ShellWriter_WriteLine(wr, shell->echoOff);
81406b9b3e0SSimon J. Gerraty }
81506b9b3e0SSimon J. Gerraty 
81606b9b3e0SSimon J. Gerraty static void
81706b9b3e0SSimon J. Gerraty ShellWriter_EchoCmd(ShellWriter *wr, const char *escCmd)
81806b9b3e0SSimon J. Gerraty {
819b0c40a00SSimon J. Gerraty 	ShellWriter_WriteFmt(wr, shell->echoTmpl, escCmd);
82006b9b3e0SSimon J. Gerraty }
82106b9b3e0SSimon J. Gerraty 
82206b9b3e0SSimon J. Gerraty static void
82306b9b3e0SSimon J. Gerraty ShellWriter_EchoOn(ShellWriter *wr)
82406b9b3e0SSimon J. Gerraty {
82506b9b3e0SSimon J. Gerraty 	if (shell->hasEchoCtl)
826b0c40a00SSimon J. Gerraty 		ShellWriter_WriteLine(wr, shell->echoOn);
82706b9b3e0SSimon J. Gerraty }
82806b9b3e0SSimon J. Gerraty 
82906b9b3e0SSimon J. Gerraty static void
83006b9b3e0SSimon J. Gerraty ShellWriter_TraceOn(ShellWriter *wr)
83106b9b3e0SSimon J. Gerraty {
83206b9b3e0SSimon J. Gerraty 	if (!wr->xtraced) {
833b0c40a00SSimon J. Gerraty 		ShellWriter_WriteLine(wr, "set -x");
834b0c40a00SSimon J. Gerraty 		wr->xtraced = true;
83506b9b3e0SSimon J. Gerraty 	}
83606b9b3e0SSimon J. Gerraty }
83706b9b3e0SSimon J. Gerraty 
83806b9b3e0SSimon J. Gerraty static void
839b0c40a00SSimon J. Gerraty ShellWriter_ErrOff(ShellWriter *wr, bool echo)
84006b9b3e0SSimon J. Gerraty {
84106b9b3e0SSimon J. Gerraty 	if (echo)
84206b9b3e0SSimon J. Gerraty 		ShellWriter_EchoOff(wr);
843b0c40a00SSimon J. Gerraty 	ShellWriter_WriteLine(wr, shell->errOff);
84406b9b3e0SSimon J. Gerraty 	if (echo)
84506b9b3e0SSimon J. Gerraty 		ShellWriter_EchoOn(wr);
84606b9b3e0SSimon J. Gerraty }
84706b9b3e0SSimon J. Gerraty 
84806b9b3e0SSimon J. Gerraty static void
849b0c40a00SSimon J. Gerraty ShellWriter_ErrOn(ShellWriter *wr, bool echo)
85006b9b3e0SSimon J. Gerraty {
85106b9b3e0SSimon J. Gerraty 	if (echo)
85206b9b3e0SSimon J. Gerraty 		ShellWriter_EchoOff(wr);
853b0c40a00SSimon J. Gerraty 	ShellWriter_WriteLine(wr, shell->errOn);
85406b9b3e0SSimon J. Gerraty 	if (echo)
85506b9b3e0SSimon J. Gerraty 		ShellWriter_EchoOn(wr);
85606b9b3e0SSimon J. Gerraty }
85706b9b3e0SSimon J. Gerraty 
85806b9b3e0SSimon J. Gerraty /*
85906b9b3e0SSimon J. Gerraty  * The shell has no built-in error control, so emulate error control by
86006b9b3e0SSimon J. Gerraty  * enclosing each shell command in a template like "{ %s \n } || exit $?"
86106b9b3e0SSimon J. Gerraty  * (configurable per shell).
8623955d011SMarcel Moolenaar  */
863956e45f6SSimon J. Gerraty static void
864b0c40a00SSimon J. Gerraty JobWriteSpecialsEchoCtl(Job *job, ShellWriter *wr, CommandFlags *inout_cmdFlags,
86506b9b3e0SSimon J. Gerraty 			const char *escCmd, const char **inout_cmdTemplate)
8663955d011SMarcel Moolenaar {
867*1d3f2ddcSSimon J. Gerraty 	/* XXX: Why is the whole job modified at this point? */
868b0c40a00SSimon J. Gerraty 	job->ignerr = true;
8693955d011SMarcel Moolenaar 
87006b9b3e0SSimon J. Gerraty 	if (job->echo && inout_cmdFlags->echo) {
87106b9b3e0SSimon J. Gerraty 		ShellWriter_EchoOff(wr);
87206b9b3e0SSimon J. Gerraty 		ShellWriter_EchoCmd(wr, escCmd);
8733955d011SMarcel Moolenaar 
87406b9b3e0SSimon J. Gerraty 		/*
87506b9b3e0SSimon J. Gerraty 		 * Leave echoing off so the user doesn't see the commands
87606b9b3e0SSimon J. Gerraty 		 * for toggling the error checking.
87706b9b3e0SSimon J. Gerraty 		 */
878b0c40a00SSimon J. Gerraty 		inout_cmdFlags->echo = false;
87906b9b3e0SSimon J. Gerraty 	}
88006b9b3e0SSimon J. Gerraty 	*inout_cmdTemplate = shell->runIgnTmpl;
8813955d011SMarcel Moolenaar 
88206b9b3e0SSimon J. Gerraty 	/*
88306b9b3e0SSimon J. Gerraty 	 * The template runIgnTmpl already takes care of ignoring errors,
88406b9b3e0SSimon J. Gerraty 	 * so pretend error checking is still on.
88506b9b3e0SSimon J. Gerraty 	 * XXX: What effects does this have, and why is it necessary?
88606b9b3e0SSimon J. Gerraty 	 */
887b0c40a00SSimon J. Gerraty 	inout_cmdFlags->ignerr = false;
88806b9b3e0SSimon J. Gerraty }
88906b9b3e0SSimon J. Gerraty 
89006b9b3e0SSimon J. Gerraty static void
891b0c40a00SSimon J. Gerraty JobWriteSpecials(Job *job, ShellWriter *wr, const char *escCmd, bool run,
89206b9b3e0SSimon J. Gerraty 		 CommandFlags *inout_cmdFlags, const char **inout_cmdTemplate)
89306b9b3e0SSimon J. Gerraty {
89406b9b3e0SSimon J. Gerraty 	if (!run) {
89506b9b3e0SSimon J. Gerraty 		/*
89606b9b3e0SSimon J. Gerraty 		 * If there is no command to run, there is no need to switch
89706b9b3e0SSimon J. Gerraty 		 * error checking off and on again for nothing.
89806b9b3e0SSimon J. Gerraty 		 */
899b0c40a00SSimon J. Gerraty 		inout_cmdFlags->ignerr = false;
90006b9b3e0SSimon J. Gerraty 	} else if (shell->hasErrCtl)
90106b9b3e0SSimon J. Gerraty 		ShellWriter_ErrOff(wr, job->echo && inout_cmdFlags->echo);
90206b9b3e0SSimon J. Gerraty 	else if (shell->runIgnTmpl != NULL && shell->runIgnTmpl[0] != '\0') {
903b0c40a00SSimon J. Gerraty 		JobWriteSpecialsEchoCtl(job, wr, inout_cmdFlags, escCmd,
90406b9b3e0SSimon J. Gerraty 		    inout_cmdTemplate);
90506b9b3e0SSimon J. Gerraty 	} else
906b0c40a00SSimon J. Gerraty 		inout_cmdFlags->ignerr = false;
90706b9b3e0SSimon J. Gerraty }
90806b9b3e0SSimon J. Gerraty 
90906b9b3e0SSimon J. Gerraty /*
910b0c40a00SSimon J. Gerraty  * Write a shell command to the job's commands file, to be run later.
91106b9b3e0SSimon J. Gerraty  *
91206b9b3e0SSimon J. Gerraty  * If the command starts with '@' and neither the -s nor the -n flag was
913b0c40a00SSimon J. Gerraty  * given to make, stick a shell-specific echoOff command in the script.
91406b9b3e0SSimon J. Gerraty  *
91506b9b3e0SSimon J. Gerraty  * If the command starts with '-' and the shell has no error control (none
916b0c40a00SSimon J. Gerraty  * of the predefined shells has that), ignore errors for the entire job.
91706b9b3e0SSimon J. Gerraty  *
918b0c40a00SSimon J. Gerraty  * XXX: Why ignore errors for the entire job?  This is even documented in the
919b0c40a00SSimon J. Gerraty  * manual page, but without any rationale since there is no known rationale.
920b0c40a00SSimon J. Gerraty  *
921b0c40a00SSimon J. Gerraty  * XXX: The manual page says the '-' "affects the entire job", but that's not
922b0c40a00SSimon J. Gerraty  * accurate.  The '-' does not affect the commands before the '-'.
923b0c40a00SSimon J. Gerraty  *
924b0c40a00SSimon J. Gerraty  * If the command is just "...", skip all further commands of this job.  These
925b0c40a00SSimon J. Gerraty  * commands are attached to the .END node instead and will be run by
926b0c40a00SSimon J. Gerraty  * Job_Finish after all other targets have been made.
92706b9b3e0SSimon J. Gerraty  */
92806b9b3e0SSimon J. Gerraty static void
929b0c40a00SSimon J. Gerraty JobWriteCommand(Job *job, ShellWriter *wr, StringListNode *ln, const char *ucmd)
93006b9b3e0SSimon J. Gerraty {
931b0c40a00SSimon J. Gerraty 	bool run;
93206b9b3e0SSimon J. Gerraty 
93306b9b3e0SSimon J. Gerraty 	CommandFlags cmdFlags;
934b0c40a00SSimon J. Gerraty 	/* Template for writing a command to the shell file */
93506b9b3e0SSimon J. Gerraty 	const char *cmdTemplate;
93606b9b3e0SSimon J. Gerraty 	char *xcmd;		/* The expanded command */
93706b9b3e0SSimon J. Gerraty 	char *xcmdStart;
93806b9b3e0SSimon J. Gerraty 	char *escCmd;		/* xcmd escaped to be used in double quotes */
93906b9b3e0SSimon J. Gerraty 
94006b9b3e0SSimon J. Gerraty 	run = GNode_ShouldExecute(job->node);
94106b9b3e0SSimon J. Gerraty 
9429f45a3c8SSimon J. Gerraty 	(void)Var_Subst(ucmd, job->node, VARE_WANTRES, &xcmd);
943956e45f6SSimon J. Gerraty 	/* TODO: handle errors */
94406b9b3e0SSimon J. Gerraty 	xcmdStart = xcmd;
9453955d011SMarcel Moolenaar 
9463955d011SMarcel Moolenaar 	cmdTemplate = "%s\n";
9473955d011SMarcel Moolenaar 
94806b9b3e0SSimon J. Gerraty 	ParseCommandFlags(&xcmd, &cmdFlags);
949956e45f6SSimon J. Gerraty 
95006b9b3e0SSimon J. Gerraty 	/* The '+' command flag overrides the -n or -N options. */
95106b9b3e0SSimon J. Gerraty 	if (cmdFlags.always && !run) {
9523955d011SMarcel Moolenaar 		/*
9533955d011SMarcel Moolenaar 		 * We're not actually executing anything...
9543955d011SMarcel Moolenaar 		 * but this one needs to be - use compat mode just for it.
9553955d011SMarcel Moolenaar 		 */
9569f45a3c8SSimon J. Gerraty 		(void)Compat_RunCommand(ucmd, job->node, ln);
95706b9b3e0SSimon J. Gerraty 		free(xcmdStart);
958956e45f6SSimon J. Gerraty 		return;
9593955d011SMarcel Moolenaar 	}
9603955d011SMarcel Moolenaar 
9613955d011SMarcel Moolenaar 	/*
96206b9b3e0SSimon J. Gerraty 	 * If the shell doesn't have error control, the alternate echoing
96306b9b3e0SSimon J. Gerraty 	 * will be done (to avoid showing additional error checking code)
96406b9b3e0SSimon J. Gerraty 	 * and this needs some characters escaped.
9653955d011SMarcel Moolenaar 	 */
96606b9b3e0SSimon J. Gerraty 	escCmd = shell->hasErrCtl ? NULL : EscapeShellDblQuot(xcmd);
9673955d011SMarcel Moolenaar 
96806b9b3e0SSimon J. Gerraty 	if (!cmdFlags.echo) {
96906b9b3e0SSimon J. Gerraty 		if (job->echo && run && shell->hasEchoCtl) {
97006b9b3e0SSimon J. Gerraty 			ShellWriter_EchoOff(wr);
9713955d011SMarcel Moolenaar 		} else {
97206b9b3e0SSimon J. Gerraty 			if (shell->hasErrCtl)
973b0c40a00SSimon J. Gerraty 				cmdFlags.echo = true;
9743955d011SMarcel Moolenaar 		}
9753955d011SMarcel Moolenaar 	}
9763955d011SMarcel Moolenaar 
97706b9b3e0SSimon J. Gerraty 	if (cmdFlags.ignerr) {
978b0c40a00SSimon J. Gerraty 		JobWriteSpecials(job, wr, escCmd, run, &cmdFlags, &cmdTemplate);
9793955d011SMarcel Moolenaar 	} else {
9803955d011SMarcel Moolenaar 
9813955d011SMarcel Moolenaar 		/*
98206b9b3e0SSimon J. Gerraty 		 * If errors are being checked and the shell doesn't have
98306b9b3e0SSimon J. Gerraty 		 * error control but does supply an runChkTmpl template, then
98406b9b3e0SSimon J. Gerraty 		 * set up commands to run through it.
9853955d011SMarcel Moolenaar 		 */
9863955d011SMarcel Moolenaar 
98706b9b3e0SSimon J. Gerraty 		if (!shell->hasErrCtl && shell->runChkTmpl != NULL &&
98806b9b3e0SSimon J. Gerraty 		    shell->runChkTmpl[0] != '\0') {
98906b9b3e0SSimon J. Gerraty 			if (job->echo && cmdFlags.echo) {
99006b9b3e0SSimon J. Gerraty 				ShellWriter_EchoOff(wr);
99106b9b3e0SSimon J. Gerraty 				ShellWriter_EchoCmd(wr, escCmd);
992b0c40a00SSimon J. Gerraty 				cmdFlags.echo = false;
9933955d011SMarcel Moolenaar 			}
99406b9b3e0SSimon J. Gerraty 			/*
99506b9b3e0SSimon J. Gerraty 			 * If it's a comment line or blank, avoid the possible
99606b9b3e0SSimon J. Gerraty 			 * syntax error generated by "{\n} || exit $?".
99706b9b3e0SSimon J. Gerraty 			 */
99806b9b3e0SSimon J. Gerraty 			cmdTemplate = escCmd[0] == shell->commentChar ||
99906b9b3e0SSimon J. Gerraty 				      escCmd[0] == '\0'
100006b9b3e0SSimon J. Gerraty 			    ? shell->runIgnTmpl
100106b9b3e0SSimon J. Gerraty 			    : shell->runChkTmpl;
1002b0c40a00SSimon J. Gerraty 			cmdFlags.ignerr = false;
10033955d011SMarcel Moolenaar 		}
10043955d011SMarcel Moolenaar 	}
10053955d011SMarcel Moolenaar 
100606b9b3e0SSimon J. Gerraty 	if (DEBUG(SHELL) && strcmp(shellName, "sh") == 0)
100706b9b3e0SSimon J. Gerraty 		ShellWriter_TraceOn(wr);
10083955d011SMarcel Moolenaar 
1009b0c40a00SSimon J. Gerraty 	ShellWriter_WriteFmt(wr, cmdTemplate, xcmd);
101006b9b3e0SSimon J. Gerraty 	free(xcmdStart);
10113955d011SMarcel Moolenaar 	free(escCmd);
101206b9b3e0SSimon J. Gerraty 
101306b9b3e0SSimon J. Gerraty 	if (cmdFlags.ignerr)
101406b9b3e0SSimon J. Gerraty 		ShellWriter_ErrOn(wr, cmdFlags.echo && job->echo);
101506b9b3e0SSimon J. Gerraty 
101606b9b3e0SSimon J. Gerraty 	if (!cmdFlags.echo)
101706b9b3e0SSimon J. Gerraty 		ShellWriter_EchoOn(wr);
10183955d011SMarcel Moolenaar }
10193955d011SMarcel Moolenaar 
102006b9b3e0SSimon J. Gerraty /*
1021b0c40a00SSimon J. Gerraty  * Write all commands to the shell file that is later executed.
10223955d011SMarcel Moolenaar  *
1023b0c40a00SSimon J. Gerraty  * The special command "..." stops writing and saves the remaining commands
1024dba7b0efSSimon J. Gerraty  * to be executed later, when the target '.END' is made.
102506b9b3e0SSimon J. Gerraty  *
102606b9b3e0SSimon J. Gerraty  * Return whether at least one command was written to the shell file.
102706b9b3e0SSimon J. Gerraty  */
1028b0c40a00SSimon J. Gerraty static bool
1029b0c40a00SSimon J. Gerraty JobWriteCommands(Job *job)
10303955d011SMarcel Moolenaar {
1031956e45f6SSimon J. Gerraty 	StringListNode *ln;
1032b0c40a00SSimon J. Gerraty 	bool seen = false;
1033b0c40a00SSimon J. Gerraty 	ShellWriter wr;
1034b0c40a00SSimon J. Gerraty 
1035b0c40a00SSimon J. Gerraty 	wr.f = job->cmdFILE;
1036b0c40a00SSimon J. Gerraty 	wr.xtraced = false;
1037956e45f6SSimon J. Gerraty 
103806b9b3e0SSimon J. Gerraty 	for (ln = job->node->commands.first; ln != NULL; ln = ln->next) {
1039956e45f6SSimon J. Gerraty 		const char *cmd = ln->datum;
1040956e45f6SSimon J. Gerraty 
1041956e45f6SSimon J. Gerraty 		if (strcmp(cmd, "...") == 0) {
1042956e45f6SSimon J. Gerraty 			job->node->type |= OP_SAVE_CMDS;
1043956e45f6SSimon J. Gerraty 			job->tailCmds = ln->next;
1044956e45f6SSimon J. Gerraty 			break;
1045956e45f6SSimon J. Gerraty 		}
1046e2eeea75SSimon J. Gerraty 
1047b0c40a00SSimon J. Gerraty 		JobWriteCommand(job, &wr, ln, ln->datum);
1048b0c40a00SSimon J. Gerraty 		seen = true;
1049956e45f6SSimon J. Gerraty 	}
105006b9b3e0SSimon J. Gerraty 
105106b9b3e0SSimon J. Gerraty 	return seen;
1052956e45f6SSimon J. Gerraty }
1053956e45f6SSimon J. Gerraty 
1054dba7b0efSSimon J. Gerraty /*
1055dba7b0efSSimon J. Gerraty  * Save the delayed commands (those after '...'), to be executed later in
1056dba7b0efSSimon J. Gerraty  * the '.END' node, when everything else is done.
1057dba7b0efSSimon J. Gerraty  */
1058956e45f6SSimon J. Gerraty static void
1059956e45f6SSimon J. Gerraty JobSaveCommands(Job *job)
1060956e45f6SSimon J. Gerraty {
106106b9b3e0SSimon J. Gerraty 	StringListNode *ln;
1062956e45f6SSimon J. Gerraty 
106306b9b3e0SSimon J. Gerraty 	for (ln = job->tailCmds; ln != NULL; ln = ln->next) {
106406b9b3e0SSimon J. Gerraty 		const char *cmd = ln->datum;
1065956e45f6SSimon J. Gerraty 		char *expanded_cmd;
1066dba7b0efSSimon J. Gerraty 		/*
1067dba7b0efSSimon J. Gerraty 		 * XXX: This Var_Subst is only intended to expand the dynamic
1068956e45f6SSimon J. Gerraty 		 * variables such as .TARGET, .IMPSRC.  It is not intended to
1069dba7b0efSSimon J. Gerraty 		 * expand the other variables as well; see deptgt-end.mk.
1070dba7b0efSSimon J. Gerraty 		 */
1071956e45f6SSimon J. Gerraty 		(void)Var_Subst(cmd, job->node, VARE_WANTRES, &expanded_cmd);
1072956e45f6SSimon J. Gerraty 		/* TODO: handle errors */
107306b9b3e0SSimon J. Gerraty 		Lst_Append(&Targ_GetEndNode()->commands, expanded_cmd);
1074956e45f6SSimon J. Gerraty 	}
10753955d011SMarcel Moolenaar }
10763955d011SMarcel Moolenaar 
10773955d011SMarcel Moolenaar 
1078956e45f6SSimon J. Gerraty /* Called to close both input and output pipes when a job is finished. */
10793955d011SMarcel Moolenaar static void
1080e2eeea75SSimon J. Gerraty JobClosePipes(Job *job)
10813955d011SMarcel Moolenaar {
10823955d011SMarcel Moolenaar 	clearfd(job);
10833955d011SMarcel Moolenaar 	(void)close(job->outPipe);
10843955d011SMarcel Moolenaar 	job->outPipe = -1;
10853955d011SMarcel Moolenaar 
1086b0c40a00SSimon J. Gerraty 	CollectOutput(job, true);
10873955d011SMarcel Moolenaar 	(void)close(job->inPipe);
10883955d011SMarcel Moolenaar 	job->inPipe = -1;
10893955d011SMarcel Moolenaar }
10903955d011SMarcel Moolenaar 
109106b9b3e0SSimon J. Gerraty static void
1092b0c40a00SSimon J. Gerraty DebugFailedJob(const Job *job)
1093b0c40a00SSimon J. Gerraty {
1094b0c40a00SSimon J. Gerraty 	const StringListNode *ln;
1095b0c40a00SSimon J. Gerraty 
1096b0c40a00SSimon J. Gerraty 	if (!DEBUG(ERROR))
1097b0c40a00SSimon J. Gerraty 		return;
1098b0c40a00SSimon J. Gerraty 
109912904384SSimon J. Gerraty 	debug_printf("\n");
110012904384SSimon J. Gerraty 	debug_printf("*** Failed target: %s\n", job->node->name);
110112904384SSimon J. Gerraty 	debug_printf("*** Failed commands:\n");
110212904384SSimon J. Gerraty 	for (ln = job->node->commands.first; ln != NULL; ln = ln->next) {
110312904384SSimon J. Gerraty 		const char *cmd = ln->datum;
110412904384SSimon J. Gerraty 		debug_printf("\t%s\n", cmd);
110512904384SSimon J. Gerraty 
110612904384SSimon J. Gerraty 		if (strchr(cmd, '$') != NULL) {
110712904384SSimon J. Gerraty 			char *xcmd;
110812904384SSimon J. Gerraty 			(void)Var_Subst(cmd, job->node, VARE_WANTRES, &xcmd);
110912904384SSimon J. Gerraty 			debug_printf("\t=> %s\n", xcmd);
111012904384SSimon J. Gerraty 			free(xcmd);
111112904384SSimon J. Gerraty 		}
111212904384SSimon J. Gerraty 	}
1113b0c40a00SSimon J. Gerraty }
1114b0c40a00SSimon J. Gerraty 
1115b0c40a00SSimon J. Gerraty static void
111606b9b3e0SSimon J. Gerraty JobFinishDoneExitedError(Job *job, WAIT_T *inout_status)
111706b9b3e0SSimon J. Gerraty {
111806b9b3e0SSimon J. Gerraty 	SwitchOutputTo(job->node);
111906b9b3e0SSimon J. Gerraty #ifdef USE_META
112006b9b3e0SSimon J. Gerraty 	if (useMeta) {
112106b9b3e0SSimon J. Gerraty 		meta_job_error(job, job->node,
112206b9b3e0SSimon J. Gerraty 		    job->ignerr, WEXITSTATUS(*inout_status));
112306b9b3e0SSimon J. Gerraty 	}
112406b9b3e0SSimon J. Gerraty #endif
112506b9b3e0SSimon J. Gerraty 	if (!shouldDieQuietly(job->node, -1)) {
1126b0c40a00SSimon J. Gerraty 		DebugFailedJob(job);
112706b9b3e0SSimon J. Gerraty 		(void)printf("*** [%s] Error code %d%s\n",
112806b9b3e0SSimon J. Gerraty 		    job->node->name, WEXITSTATUS(*inout_status),
112906b9b3e0SSimon J. Gerraty 		    job->ignerr ? " (ignored)" : "");
113006b9b3e0SSimon J. Gerraty 	}
113106b9b3e0SSimon J. Gerraty 
113206b9b3e0SSimon J. Gerraty 	if (job->ignerr)
113306b9b3e0SSimon J. Gerraty 		WAIT_STATUS(*inout_status) = 0;
113406b9b3e0SSimon J. Gerraty 	else {
113506b9b3e0SSimon J. Gerraty 		if (deleteOnError)
113606b9b3e0SSimon J. Gerraty 			JobDeleteTarget(job->node);
11379f45a3c8SSimon J. Gerraty 		PrintOnError(job->node, "\n");
113806b9b3e0SSimon J. Gerraty 	}
113906b9b3e0SSimon J. Gerraty }
114006b9b3e0SSimon J. Gerraty 
114106b9b3e0SSimon J. Gerraty static void
114206b9b3e0SSimon J. Gerraty JobFinishDoneExited(Job *job, WAIT_T *inout_status)
114306b9b3e0SSimon J. Gerraty {
114406b9b3e0SSimon J. Gerraty 	DEBUG2(JOB, "Process %d [%s] exited.\n", job->pid, job->node->name);
114506b9b3e0SSimon J. Gerraty 
114606b9b3e0SSimon J. Gerraty 	if (WEXITSTATUS(*inout_status) != 0)
114706b9b3e0SSimon J. Gerraty 		JobFinishDoneExitedError(job, inout_status);
114806b9b3e0SSimon J. Gerraty 	else if (DEBUG(JOB)) {
114906b9b3e0SSimon J. Gerraty 		SwitchOutputTo(job->node);
115006b9b3e0SSimon J. Gerraty 		(void)printf("*** [%s] Completed successfully\n",
115106b9b3e0SSimon J. Gerraty 		    job->node->name);
115206b9b3e0SSimon J. Gerraty 	}
115306b9b3e0SSimon J. Gerraty }
115406b9b3e0SSimon J. Gerraty 
115506b9b3e0SSimon J. Gerraty static void
115606b9b3e0SSimon J. Gerraty JobFinishDoneSignaled(Job *job, WAIT_T status)
115706b9b3e0SSimon J. Gerraty {
115806b9b3e0SSimon J. Gerraty 	SwitchOutputTo(job->node);
1159b0c40a00SSimon J. Gerraty 	DebugFailedJob(job);
116006b9b3e0SSimon J. Gerraty 	(void)printf("*** [%s] Signal %d\n", job->node->name, WTERMSIG(status));
116106b9b3e0SSimon J. Gerraty 	if (deleteOnError)
116206b9b3e0SSimon J. Gerraty 		JobDeleteTarget(job->node);
116306b9b3e0SSimon J. Gerraty }
116406b9b3e0SSimon J. Gerraty 
116506b9b3e0SSimon J. Gerraty static void
116606b9b3e0SSimon J. Gerraty JobFinishDone(Job *job, WAIT_T *inout_status)
116706b9b3e0SSimon J. Gerraty {
116806b9b3e0SSimon J. Gerraty 	if (WIFEXITED(*inout_status))
116906b9b3e0SSimon J. Gerraty 		JobFinishDoneExited(job, inout_status);
117006b9b3e0SSimon J. Gerraty 	else
117106b9b3e0SSimon J. Gerraty 		JobFinishDoneSignaled(job, *inout_status);
117206b9b3e0SSimon J. Gerraty 
117306b9b3e0SSimon J. Gerraty 	(void)fflush(stdout);
117406b9b3e0SSimon J. Gerraty }
117506b9b3e0SSimon J. Gerraty 
117606b9b3e0SSimon J. Gerraty /*
117706b9b3e0SSimon J. Gerraty  * Do final processing for the given job including updating parent nodes and
1178e2eeea75SSimon J. Gerraty  * starting new jobs as available/necessary.
1179e2eeea75SSimon J. Gerraty  *
1180e2eeea75SSimon J. Gerraty  * Deferred commands for the job are placed on the .END node.
1181e2eeea75SSimon J. Gerraty  *
118206b9b3e0SSimon J. Gerraty  * If there was a serious error (job_errors != 0; not an ignored one), no more
1183e2eeea75SSimon J. Gerraty  * jobs will be started.
11843955d011SMarcel Moolenaar  *
11853955d011SMarcel Moolenaar  * Input:
11863955d011SMarcel Moolenaar  *	job		job to finish
11873955d011SMarcel Moolenaar  *	status		sub-why job went away
11883955d011SMarcel Moolenaar  */
11893955d011SMarcel Moolenaar static void
11903955d011SMarcel Moolenaar JobFinish (Job *job, WAIT_T status)
11913955d011SMarcel Moolenaar {
1192b0c40a00SSimon J. Gerraty 	bool done, return_job_token;
11933955d011SMarcel Moolenaar 
1194956e45f6SSimon J. Gerraty 	DEBUG3(JOB, "JobFinish: %d [%s], status %d\n",
11953955d011SMarcel Moolenaar 	    job->pid, job->node->name, status);
11963955d011SMarcel Moolenaar 
11973955d011SMarcel Moolenaar 	if ((WIFEXITED(status) &&
119806b9b3e0SSimon J. Gerraty 	     ((WEXITSTATUS(status) != 0 && !job->ignerr))) ||
119906b9b3e0SSimon J. Gerraty 	    WIFSIGNALED(status)) {
120006b9b3e0SSimon J. Gerraty 		/* Finished because of an error. */
120106b9b3e0SSimon J. Gerraty 
1202e2eeea75SSimon J. Gerraty 		JobClosePipes(job);
12033955d011SMarcel Moolenaar 		if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
120412904384SSimon J. Gerraty 			if (fclose(job->cmdFILE) != 0)
120512904384SSimon J. Gerraty 				Punt("Cannot write shell script for '%s': %s",
120612904384SSimon J. Gerraty 				    job->node->name, strerror(errno));
12073955d011SMarcel Moolenaar 			job->cmdFILE = NULL;
12083955d011SMarcel Moolenaar 		}
1209b0c40a00SSimon J. Gerraty 		done = true;
121006b9b3e0SSimon J. Gerraty 
12113955d011SMarcel Moolenaar 	} else if (WIFEXITED(status)) {
12123955d011SMarcel Moolenaar 		/*
121306b9b3e0SSimon J. Gerraty 		 * Deal with ignored errors in -B mode. We need to print a
121406b9b3e0SSimon J. Gerraty 		 * message telling of the ignored error as well as to run
121506b9b3e0SSimon J. Gerraty 		 * the next command.
12163955d011SMarcel Moolenaar 		 */
12173955d011SMarcel Moolenaar 		done = WEXITSTATUS(status) != 0;
121806b9b3e0SSimon J. Gerraty 
1219e2eeea75SSimon J. Gerraty 		JobClosePipes(job);
122006b9b3e0SSimon J. Gerraty 
12213955d011SMarcel Moolenaar 	} else {
122206b9b3e0SSimon J. Gerraty 		/* No need to close things down or anything. */
1223b0c40a00SSimon J. Gerraty 		done = false;
12243955d011SMarcel Moolenaar 	}
12253955d011SMarcel Moolenaar 
122606b9b3e0SSimon J. Gerraty 	if (done)
122706b9b3e0SSimon J. Gerraty 		JobFinishDone(job, &status);
12283955d011SMarcel Moolenaar 
12293955d011SMarcel Moolenaar #ifdef USE_META
12303955d011SMarcel Moolenaar 	if (useMeta) {
1231e2eeea75SSimon J. Gerraty 		int meta_status = meta_job_finish(job);
1232e2eeea75SSimon J. Gerraty 		if (meta_status != 0 && status == 0)
1233e2eeea75SSimon J. Gerraty 			status = meta_status;
12343955d011SMarcel Moolenaar 	}
12353955d011SMarcel Moolenaar #endif
12363955d011SMarcel Moolenaar 
1237b0c40a00SSimon J. Gerraty 	return_job_token = false;
12383955d011SMarcel Moolenaar 
12393955d011SMarcel Moolenaar 	Trace_Log(JOBEND, job);
124006b9b3e0SSimon J. Gerraty 	if (!job->special) {
1241e2eeea75SSimon J. Gerraty 		if (WAIT_STATUS(status) != 0 ||
1242e2eeea75SSimon J. Gerraty 		    (aborting == ABORT_ERROR) || aborting == ABORT_INTERRUPT)
1243b0c40a00SSimon J. Gerraty 			return_job_token = true;
12443955d011SMarcel Moolenaar 	}
12453955d011SMarcel Moolenaar 
1246e2eeea75SSimon J. Gerraty 	if (aborting != ABORT_ERROR && aborting != ABORT_INTERRUPT &&
12473955d011SMarcel Moolenaar 	    (WAIT_STATUS(status) == 0)) {
12483955d011SMarcel Moolenaar 		/*
124906b9b3e0SSimon J. Gerraty 		 * As long as we aren't aborting and the job didn't return a
125006b9b3e0SSimon J. Gerraty 		 * non-zero status that we shouldn't ignore, we call
125106b9b3e0SSimon J. Gerraty 		 * Make_Update to update the parents.
12523955d011SMarcel Moolenaar 		 */
1253956e45f6SSimon J. Gerraty 		JobSaveCommands(job);
12543955d011SMarcel Moolenaar 		job->node->made = MADE;
125506b9b3e0SSimon J. Gerraty 		if (!job->special)
1256b0c40a00SSimon J. Gerraty 			return_job_token = true;
12573955d011SMarcel Moolenaar 		Make_Update(job->node);
1258e2eeea75SSimon J. Gerraty 		job->status = JOB_ST_FREE;
125906b9b3e0SSimon J. Gerraty 	} else if (status != 0) {
126006b9b3e0SSimon J. Gerraty 		job_errors++;
1261e2eeea75SSimon J. Gerraty 		job->status = JOB_ST_FREE;
12623955d011SMarcel Moolenaar 	}
12633955d011SMarcel Moolenaar 
126406b9b3e0SSimon J. Gerraty 	if (job_errors > 0 && !opts.keepgoing && aborting != ABORT_INTERRUPT) {
126506b9b3e0SSimon J. Gerraty 		/* Prevent more jobs from getting started. */
126606b9b3e0SSimon J. Gerraty 		aborting = ABORT_ERROR;
126706b9b3e0SSimon J. Gerraty 	}
12683955d011SMarcel Moolenaar 
12693955d011SMarcel Moolenaar 	if (return_job_token)
12703955d011SMarcel Moolenaar 		Job_TokenReturn();
12713955d011SMarcel Moolenaar 
1272e2eeea75SSimon J. Gerraty 	if (aborting == ABORT_ERROR && jobTokensRunning == 0)
127306b9b3e0SSimon J. Gerraty 		Finish(job_errors);
12743955d011SMarcel Moolenaar }
1275e2eeea75SSimon J. Gerraty 
1276e2eeea75SSimon J. Gerraty static void
1277e2eeea75SSimon J. Gerraty TouchRegular(GNode *gn)
1278e2eeea75SSimon J. Gerraty {
1279e2eeea75SSimon J. Gerraty 	const char *file = GNode_Path(gn);
1280b0c40a00SSimon J. Gerraty 	struct utimbuf times;
1281e2eeea75SSimon J. Gerraty 	int fd;
1282e2eeea75SSimon J. Gerraty 	char c;
1283e2eeea75SSimon J. Gerraty 
1284b0c40a00SSimon J. Gerraty 	times.actime = now;
1285b0c40a00SSimon J. Gerraty 	times.modtime = now;
1286e2eeea75SSimon J. Gerraty 	if (utime(file, &times) >= 0)
1287e2eeea75SSimon J. Gerraty 		return;
1288e2eeea75SSimon J. Gerraty 
1289e2eeea75SSimon J. Gerraty 	fd = open(file, O_RDWR | O_CREAT, 0666);
1290e2eeea75SSimon J. Gerraty 	if (fd < 0) {
1291e2eeea75SSimon J. Gerraty 		(void)fprintf(stderr, "*** couldn't touch %s: %s\n",
1292e2eeea75SSimon J. Gerraty 		    file, strerror(errno));
1293e2eeea75SSimon J. Gerraty 		(void)fflush(stderr);
1294e2eeea75SSimon J. Gerraty 		return;		/* XXX: What about propagating the error? */
1295e2eeea75SSimon J. Gerraty 	}
1296e2eeea75SSimon J. Gerraty 
12979f45a3c8SSimon J. Gerraty 	/*
12989f45a3c8SSimon J. Gerraty 	 * Last resort: update the file's time stamps in the traditional way.
1299e2eeea75SSimon J. Gerraty 	 * XXX: This doesn't work for empty files, which are sometimes used
13009f45a3c8SSimon J. Gerraty 	 * as marker files.
13019f45a3c8SSimon J. Gerraty 	 */
1302e2eeea75SSimon J. Gerraty 	if (read(fd, &c, 1) == 1) {
1303e2eeea75SSimon J. Gerraty 		(void)lseek(fd, 0, SEEK_SET);
1304e2eeea75SSimon J. Gerraty 		while (write(fd, &c, 1) == -1 && errno == EAGAIN)
1305e2eeea75SSimon J. Gerraty 			continue;
1306e2eeea75SSimon J. Gerraty 	}
1307e2eeea75SSimon J. Gerraty 	(void)close(fd);	/* XXX: What about propagating the error? */
13083955d011SMarcel Moolenaar }
13093955d011SMarcel Moolenaar 
131006b9b3e0SSimon J. Gerraty /*
131106b9b3e0SSimon J. Gerraty  * Touch the given target. Called by JobStart when the -t flag was given.
13123955d011SMarcel Moolenaar  *
1313956e45f6SSimon J. Gerraty  * The modification date of the file is changed.
131406b9b3e0SSimon J. Gerraty  * If the file did not exist, it is created.
131506b9b3e0SSimon J. Gerraty  */
13163955d011SMarcel Moolenaar void
1317b0c40a00SSimon J. Gerraty Job_Touch(GNode *gn, bool echo)
13183955d011SMarcel Moolenaar {
131906b9b3e0SSimon J. Gerraty 	if (gn->type &
132006b9b3e0SSimon J. Gerraty 	    (OP_JOIN | OP_USE | OP_USEBEFORE | OP_EXEC | OP_OPTIONAL |
13213955d011SMarcel Moolenaar 	     OP_SPECIAL | OP_PHONY)) {
132206b9b3e0SSimon J. Gerraty 		/*
132306b9b3e0SSimon J. Gerraty 		 * These are "virtual" targets and should not really be
132406b9b3e0SSimon J. Gerraty 		 * created.
132506b9b3e0SSimon J. Gerraty 		 */
13263955d011SMarcel Moolenaar 		return;
13273955d011SMarcel Moolenaar 	}
13283955d011SMarcel Moolenaar 
132906b9b3e0SSimon J. Gerraty 	if (echo || !GNode_ShouldExecute(gn)) {
13303955d011SMarcel Moolenaar 		(void)fprintf(stdout, "touch %s\n", gn->name);
13313955d011SMarcel Moolenaar 		(void)fflush(stdout);
13323955d011SMarcel Moolenaar 	}
13333955d011SMarcel Moolenaar 
1334e2eeea75SSimon J. Gerraty 	if (!GNode_ShouldExecute(gn))
13353955d011SMarcel Moolenaar 		return;
13363955d011SMarcel Moolenaar 
133706b9b3e0SSimon J. Gerraty 	if (gn->type & OP_ARCHV)
13383955d011SMarcel Moolenaar 		Arch_Touch(gn);
133906b9b3e0SSimon J. Gerraty 	else if (gn->type & OP_LIB)
13403955d011SMarcel Moolenaar 		Arch_TouchLib(gn);
134106b9b3e0SSimon J. Gerraty 	else
1342e2eeea75SSimon J. Gerraty 		TouchRegular(gn);
13433955d011SMarcel Moolenaar }
13443955d011SMarcel Moolenaar 
134506b9b3e0SSimon J. Gerraty /*
134606b9b3e0SSimon J. Gerraty  * Make sure the given node has all the commands it needs.
1347956e45f6SSimon J. Gerraty  *
1348956e45f6SSimon J. Gerraty  * The node will have commands from the .DEFAULT rule added to it if it
1349956e45f6SSimon J. Gerraty  * needs them.
13503955d011SMarcel Moolenaar  *
13513955d011SMarcel Moolenaar  * Input:
13523955d011SMarcel Moolenaar  *	gn		The target whose commands need verifying
13533955d011SMarcel Moolenaar  *	abortProc	Function to abort with message
13543955d011SMarcel Moolenaar  *
13553955d011SMarcel Moolenaar  * Results:
1356b0c40a00SSimon J. Gerraty  *	true if the commands list is/was ok.
13573955d011SMarcel Moolenaar  */
1358b0c40a00SSimon J. Gerraty bool
13593955d011SMarcel Moolenaar Job_CheckCommands(GNode *gn, void (*abortProc)(const char *, ...))
13603955d011SMarcel Moolenaar {
1361956e45f6SSimon J. Gerraty 	if (GNode_IsTarget(gn))
1362b0c40a00SSimon J. Gerraty 		return true;
136306b9b3e0SSimon J. Gerraty 	if (!Lst_IsEmpty(&gn->commands))
1364b0c40a00SSimon J. Gerraty 		return true;
136506b9b3e0SSimon J. Gerraty 	if ((gn->type & OP_LIB) && !Lst_IsEmpty(&gn->children))
1366b0c40a00SSimon J. Gerraty 		return true;
1367956e45f6SSimon J. Gerraty 
13683955d011SMarcel Moolenaar 	/*
13693955d011SMarcel Moolenaar 	 * No commands. Look for .DEFAULT rule from which we might infer
1370e2eeea75SSimon J. Gerraty 	 * commands.
13713955d011SMarcel Moolenaar 	 */
137206b9b3e0SSimon J. Gerraty 	if (defaultNode != NULL && !Lst_IsEmpty(&defaultNode->commands) &&
1373e2eeea75SSimon J. Gerraty 	    !(gn->type & OP_SPECIAL)) {
13743955d011SMarcel Moolenaar 		/*
137506b9b3e0SSimon J. Gerraty 		 * The traditional Make only looks for a .DEFAULT if the node
137606b9b3e0SSimon J. Gerraty 		 * was never the target of an operator, so that's what we do
137706b9b3e0SSimon J. Gerraty 		 * too.
1378e2eeea75SSimon J. Gerraty 		 *
1379e2eeea75SSimon J. Gerraty 		 * The .DEFAULT node acts like a transformation rule, in that
13803955d011SMarcel Moolenaar 		 * gn also inherits any attributes or sources attached to
13813955d011SMarcel Moolenaar 		 * .DEFAULT itself.
13823955d011SMarcel Moolenaar 		 */
1383e2eeea75SSimon J. Gerraty 		Make_HandleUse(defaultNode, gn);
1384dba7b0efSSimon J. Gerraty 		Var_Set(gn, IMPSRC, GNode_VarTarget(gn));
1385b0c40a00SSimon J. Gerraty 		return true;
1386956e45f6SSimon J. Gerraty 	}
1387956e45f6SSimon J. Gerraty 
1388b0c40a00SSimon J. Gerraty 	Dir_UpdateMTime(gn, false);
1389e2eeea75SSimon J. Gerraty 	if (gn->mtime != 0 || (gn->type & OP_SPECIAL))
1390b0c40a00SSimon J. Gerraty 		return true;
1391956e45f6SSimon J. Gerraty 
13923955d011SMarcel Moolenaar 	/*
1393956e45f6SSimon J. Gerraty 	 * The node wasn't the target of an operator.  We have no .DEFAULT
13943955d011SMarcel Moolenaar 	 * rule to go on and the target doesn't already exist. There's
13953955d011SMarcel Moolenaar 	 * nothing more we can do for this branch. If the -k flag wasn't
13963955d011SMarcel Moolenaar 	 * given, we stop in our tracks, otherwise we just don't update
13973955d011SMarcel Moolenaar 	 * this node's parents so they never get examined.
13983955d011SMarcel Moolenaar 	 */
13993955d011SMarcel Moolenaar 
140012904384SSimon J. Gerraty 	if (gn->flags.fromDepend) {
14011748de26SSimon J. Gerraty 		if (!Job_RunTarget(".STALE", gn->fname))
140206b9b3e0SSimon J. Gerraty 			fprintf(stdout,
14039f45a3c8SSimon J. Gerraty 			    "%s: %s, %u: ignoring stale %s for %s\n",
14041748de26SSimon J. Gerraty 			    progname, gn->fname, gn->lineno, makeDependfile,
14051748de26SSimon J. Gerraty 			    gn->name);
1406b0c40a00SSimon J. Gerraty 		return true;
14073955d011SMarcel Moolenaar 	}
14083955d011SMarcel Moolenaar 
14093955d011SMarcel Moolenaar 	if (gn->type & OP_OPTIONAL) {
1410956e45f6SSimon J. Gerraty 		(void)fprintf(stdout, "%s: don't know how to make %s (%s)\n",
1411956e45f6SSimon J. Gerraty 		    progname, gn->name, "ignored");
14123955d011SMarcel Moolenaar 		(void)fflush(stdout);
1413b0c40a00SSimon J. Gerraty 		return true;
14143955d011SMarcel Moolenaar 	}
14153955d011SMarcel Moolenaar 
1416956e45f6SSimon J. Gerraty 	if (opts.keepgoing) {
1417956e45f6SSimon J. Gerraty 		(void)fprintf(stdout, "%s: don't know how to make %s (%s)\n",
1418956e45f6SSimon J. Gerraty 		    progname, gn->name, "continuing");
1419956e45f6SSimon J. Gerraty 		(void)fflush(stdout);
1420b0c40a00SSimon J. Gerraty 		return false;
1421956e45f6SSimon J. Gerraty 	}
1422956e45f6SSimon J. Gerraty 
1423956e45f6SSimon J. Gerraty 	abortProc("%s: don't know how to make %s. Stop", progname, gn->name);
1424b0c40a00SSimon J. Gerraty 	return false;
1425956e45f6SSimon J. Gerraty }
1426956e45f6SSimon J. Gerraty 
142706b9b3e0SSimon J. Gerraty /*
142806b9b3e0SSimon J. Gerraty  * Execute the shell for the given job.
14293955d011SMarcel Moolenaar  *
143006b9b3e0SSimon J. Gerraty  * See Job_CatchOutput for handling the output of the shell.
143106b9b3e0SSimon J. Gerraty  */
14323955d011SMarcel Moolenaar static void
14333955d011SMarcel Moolenaar JobExec(Job *job, char **argv)
14343955d011SMarcel Moolenaar {
14353955d011SMarcel Moolenaar 	int cpid;		/* ID of new child */
14363955d011SMarcel Moolenaar 	sigset_t mask;
14373955d011SMarcel Moolenaar 
14383955d011SMarcel Moolenaar 	if (DEBUG(JOB)) {
14393955d011SMarcel Moolenaar 		int i;
14403955d011SMarcel Moolenaar 
1441e2eeea75SSimon J. Gerraty 		debug_printf("Running %s\n", job->node->name);
1442956e45f6SSimon J. Gerraty 		debug_printf("\tCommand: ");
14433955d011SMarcel Moolenaar 		for (i = 0; argv[i] != NULL; i++) {
1444956e45f6SSimon J. Gerraty 			debug_printf("%s ", argv[i]);
14453955d011SMarcel Moolenaar 		}
1446956e45f6SSimon J. Gerraty 		debug_printf("\n");
14473955d011SMarcel Moolenaar 	}
14483955d011SMarcel Moolenaar 
14493955d011SMarcel Moolenaar 	/*
14503955d011SMarcel Moolenaar 	 * Some jobs produce no output and it's disconcerting to have
14513955d011SMarcel Moolenaar 	 * no feedback of their running (since they produce no output, the
14523955d011SMarcel Moolenaar 	 * banner with their name in it never appears). This is an attempt to
14533955d011SMarcel Moolenaar 	 * provide that feedback, even if nothing follows it.
14543955d011SMarcel Moolenaar 	 */
145506b9b3e0SSimon J. Gerraty 	if (job->echo)
145606b9b3e0SSimon J. Gerraty 		SwitchOutputTo(job->node);
14573955d011SMarcel Moolenaar 
14583955d011SMarcel Moolenaar 	/* No interruptions until this job is on the `jobs' list */
14593955d011SMarcel Moolenaar 	JobSigLock(&mask);
14603955d011SMarcel Moolenaar 
14613955d011SMarcel Moolenaar 	/* Pre-emptively mark job running, pid still zero though */
1462e2eeea75SSimon J. Gerraty 	job->status = JOB_ST_RUNNING;
14633955d011SMarcel Moolenaar 
146406b9b3e0SSimon J. Gerraty 	Var_ReexportVars();
146506b9b3e0SSimon J. Gerraty 
1466dba7b0efSSimon J. Gerraty 	cpid = vfork();
14673955d011SMarcel Moolenaar 	if (cpid == -1)
14683955d011SMarcel Moolenaar 		Punt("Cannot vfork: %s", strerror(errno));
14693955d011SMarcel Moolenaar 
14703955d011SMarcel Moolenaar 	if (cpid == 0) {
14713955d011SMarcel Moolenaar 		/* Child */
14723955d011SMarcel Moolenaar 		sigset_t tmask;
14733955d011SMarcel Moolenaar 
14743955d011SMarcel Moolenaar #ifdef USE_META
14759f45a3c8SSimon J. Gerraty 		if (useMeta)
14763955d011SMarcel Moolenaar 			meta_job_child(job);
14773955d011SMarcel Moolenaar #endif
14783955d011SMarcel Moolenaar 		/*
147906b9b3e0SSimon J. Gerraty 		 * Reset all signal handlers; this is necessary because we
148006b9b3e0SSimon J. Gerraty 		 * also need to unblock signals before we exec(2).
14813955d011SMarcel Moolenaar 		 */
14823955d011SMarcel Moolenaar 		JobSigReset();
14833955d011SMarcel Moolenaar 
14843955d011SMarcel Moolenaar 		/* Now unblock signals */
14853955d011SMarcel Moolenaar 		sigemptyset(&tmask);
14863955d011SMarcel Moolenaar 		JobSigUnlock(&tmask);
14873955d011SMarcel Moolenaar 
14883955d011SMarcel Moolenaar 		/*
148906b9b3e0SSimon J. Gerraty 		 * Must duplicate the input stream down to the child's input
149006b9b3e0SSimon J. Gerraty 		 * and reset it to the beginning (again). Since the stream
149106b9b3e0SSimon J. Gerraty 		 * was marked close-on-exec, we must clear that bit in the
149206b9b3e0SSimon J. Gerraty 		 * new input.
14933955d011SMarcel Moolenaar 		 */
1494956e45f6SSimon J. Gerraty 		if (dup2(fileno(job->cmdFILE), 0) == -1)
1495956e45f6SSimon J. Gerraty 			execDie("dup2", "job->cmdFILE");
1496956e45f6SSimon J. Gerraty 		if (fcntl(0, F_SETFD, 0) == -1)
1497956e45f6SSimon J. Gerraty 			execDie("fcntl clear close-on-exec", "stdin");
1498e2eeea75SSimon J. Gerraty 		if (lseek(0, 0, SEEK_SET) == -1)
1499956e45f6SSimon J. Gerraty 			execDie("lseek to 0", "stdin");
15003955d011SMarcel Moolenaar 
1501db29cad8SSimon J. Gerraty 		if (Always_pass_job_queue ||
1502db29cad8SSimon J. Gerraty 		    (job->node->type & (OP_MAKE | OP_SUBMAKE))) {
15033955d011SMarcel Moolenaar 			/*
15043955d011SMarcel Moolenaar 			 * Pass job token pipe to submakes.
15053955d011SMarcel Moolenaar 			 */
1506956e45f6SSimon J. Gerraty 			if (fcntl(tokenWaitJob.inPipe, F_SETFD, 0) == -1)
150706b9b3e0SSimon J. Gerraty 				execDie("clear close-on-exec",
150806b9b3e0SSimon J. Gerraty 				    "tokenWaitJob.inPipe");
1509956e45f6SSimon J. Gerraty 			if (fcntl(tokenWaitJob.outPipe, F_SETFD, 0) == -1)
151006b9b3e0SSimon J. Gerraty 				execDie("clear close-on-exec",
151106b9b3e0SSimon J. Gerraty 				    "tokenWaitJob.outPipe");
15123955d011SMarcel Moolenaar 		}
15133955d011SMarcel Moolenaar 
15143955d011SMarcel Moolenaar 		/*
15153955d011SMarcel Moolenaar 		 * Set up the child's output to be routed through the pipe
15163955d011SMarcel Moolenaar 		 * we've created for it.
15173955d011SMarcel Moolenaar 		 */
1518956e45f6SSimon J. Gerraty 		if (dup2(job->outPipe, 1) == -1)
1519956e45f6SSimon J. Gerraty 			execDie("dup2", "job->outPipe");
1520956e45f6SSimon J. Gerraty 
15213955d011SMarcel Moolenaar 		/*
152206b9b3e0SSimon J. Gerraty 		 * The output channels are marked close on exec. This bit
152306b9b3e0SSimon J. Gerraty 		 * was duplicated by the dup2(on some systems), so we have
152406b9b3e0SSimon J. Gerraty 		 * to clear it before routing the shell's error output to
152506b9b3e0SSimon J. Gerraty 		 * the same place as its standard output.
15263955d011SMarcel Moolenaar 		 */
1527956e45f6SSimon J. Gerraty 		if (fcntl(1, F_SETFD, 0) == -1)
1528956e45f6SSimon J. Gerraty 			execDie("clear close-on-exec", "stdout");
1529956e45f6SSimon J. Gerraty 		if (dup2(1, 2) == -1)
1530956e45f6SSimon J. Gerraty 			execDie("dup2", "1, 2");
15313955d011SMarcel Moolenaar 
15323955d011SMarcel Moolenaar 		/*
153306b9b3e0SSimon J. Gerraty 		 * We want to switch the child into a different process
153406b9b3e0SSimon J. Gerraty 		 * family so we can kill it and all its descendants in
153506b9b3e0SSimon J. Gerraty 		 * one fell swoop, by killing its process family, but not
153606b9b3e0SSimon J. Gerraty 		 * commit suicide.
15373955d011SMarcel Moolenaar 		 */
15383955d011SMarcel Moolenaar #if defined(HAVE_SETPGID)
15393955d011SMarcel Moolenaar 		(void)setpgid(0, getpid());
15403955d011SMarcel Moolenaar #else
15413955d011SMarcel Moolenaar # if defined(HAVE_SETSID)
15423955d011SMarcel Moolenaar 		/* XXX: dsl - I'm sure this should be setpgrp()... */
15433955d011SMarcel Moolenaar 		(void)setsid();
15443955d011SMarcel Moolenaar # else
15453955d011SMarcel Moolenaar 		(void)setpgrp(0, getpid());
15463955d011SMarcel Moolenaar # endif
15473955d011SMarcel Moolenaar #endif
15483955d011SMarcel Moolenaar 
15493955d011SMarcel Moolenaar 		(void)execv(shellPath, argv);
1550956e45f6SSimon J. Gerraty 		execDie("exec", shellPath);
15513955d011SMarcel Moolenaar 	}
15523955d011SMarcel Moolenaar 
15533955d011SMarcel Moolenaar 	/* Parent, continuing after the child exec */
15543955d011SMarcel Moolenaar 	job->pid = cpid;
15553955d011SMarcel Moolenaar 
15563955d011SMarcel Moolenaar 	Trace_Log(JOBSTART, job);
15573955d011SMarcel Moolenaar 
155849caa483SSimon J. Gerraty #ifdef USE_META
15599f45a3c8SSimon J. Gerraty 	if (useMeta)
156049caa483SSimon J. Gerraty 		meta_job_parent(job, cpid);
156149caa483SSimon J. Gerraty #endif
156249caa483SSimon J. Gerraty 
15633955d011SMarcel Moolenaar 	/*
15643955d011SMarcel Moolenaar 	 * Set the current position in the buffer to the beginning
15653955d011SMarcel Moolenaar 	 * and mark another stream to watch in the outputs mask
15663955d011SMarcel Moolenaar 	 */
15673955d011SMarcel Moolenaar 	job->curPos = 0;
15683955d011SMarcel Moolenaar 
15693955d011SMarcel Moolenaar 	watchfd(job);
15703955d011SMarcel Moolenaar 
15713955d011SMarcel Moolenaar 	if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
157212904384SSimon J. Gerraty 		if (fclose(job->cmdFILE) != 0)
157312904384SSimon J. Gerraty 			Punt("Cannot write shell script for '%s': %s",
157412904384SSimon J. Gerraty 			    job->node->name, strerror(errno));
15753955d011SMarcel Moolenaar 		job->cmdFILE = NULL;
15763955d011SMarcel Moolenaar 	}
15773955d011SMarcel Moolenaar 
1578dba7b0efSSimon J. Gerraty 	/* Now that the job is actually running, add it to the table. */
15793955d011SMarcel Moolenaar 	if (DEBUG(JOB)) {
1580956e45f6SSimon J. Gerraty 		debug_printf("JobExec(%s): pid %d added to jobs table\n",
15813955d011SMarcel Moolenaar 		    job->node->name, job->pid);
1582b0c40a00SSimon J. Gerraty 		DumpJobs("job started");
15833955d011SMarcel Moolenaar 	}
15843955d011SMarcel Moolenaar 	JobSigUnlock(&mask);
15853955d011SMarcel Moolenaar }
15863955d011SMarcel Moolenaar 
1587956e45f6SSimon J. Gerraty /* Create the argv needed to execute the shell for a given job. */
15883955d011SMarcel Moolenaar static void
15893955d011SMarcel Moolenaar JobMakeArgv(Job *job, char **argv)
15903955d011SMarcel Moolenaar {
15913955d011SMarcel Moolenaar 	int argc;
15923955d011SMarcel Moolenaar 	static char args[10];	/* For merged arguments */
15933955d011SMarcel Moolenaar 
15943955d011SMarcel Moolenaar 	argv[0] = UNCONST(shellName);
15953955d011SMarcel Moolenaar 	argc = 1;
15963955d011SMarcel Moolenaar 
159706b9b3e0SSimon J. Gerraty 	if ((shell->errFlag != NULL && shell->errFlag[0] != '-') ||
159806b9b3e0SSimon J. Gerraty 	    (shell->echoFlag != NULL && shell->echoFlag[0] != '-')) {
15993955d011SMarcel Moolenaar 		/*
160006b9b3e0SSimon J. Gerraty 		 * At least one of the flags doesn't have a minus before it,
160106b9b3e0SSimon J. Gerraty 		 * so merge them together. Have to do this because the Bourne
160206b9b3e0SSimon J. Gerraty 		 * shell thinks its second argument is a file to source.
160306b9b3e0SSimon J. Gerraty 		 * Grrrr. Note the ten-character limitation on the combined
160406b9b3e0SSimon J. Gerraty 		 * arguments.
160506b9b3e0SSimon J. Gerraty 		 *
160606b9b3e0SSimon J. Gerraty 		 * TODO: Research until when the above comments were
160706b9b3e0SSimon J. Gerraty 		 * practically relevant.
16083955d011SMarcel Moolenaar 		 */
1609e2eeea75SSimon J. Gerraty 		(void)snprintf(args, sizeof args, "-%s%s",
161006b9b3e0SSimon J. Gerraty 		    (job->ignerr ? "" :
161106b9b3e0SSimon J. Gerraty 			(shell->errFlag != NULL ? shell->errFlag : "")),
161206b9b3e0SSimon J. Gerraty 		    (!job->echo ? "" :
161306b9b3e0SSimon J. Gerraty 			(shell->echoFlag != NULL ? shell->echoFlag : "")));
16143955d011SMarcel Moolenaar 
161506b9b3e0SSimon J. Gerraty 		if (args[1] != '\0') {
16163955d011SMarcel Moolenaar 			argv[argc] = args;
16173955d011SMarcel Moolenaar 			argc++;
16183955d011SMarcel Moolenaar 		}
16193955d011SMarcel Moolenaar 	} else {
162006b9b3e0SSimon J. Gerraty 		if (!job->ignerr && shell->errFlag != NULL) {
162106b9b3e0SSimon J. Gerraty 			argv[argc] = UNCONST(shell->errFlag);
16223955d011SMarcel Moolenaar 			argc++;
16233955d011SMarcel Moolenaar 		}
162406b9b3e0SSimon J. Gerraty 		if (job->echo && shell->echoFlag != NULL) {
162506b9b3e0SSimon J. Gerraty 			argv[argc] = UNCONST(shell->echoFlag);
16263955d011SMarcel Moolenaar 			argc++;
16273955d011SMarcel Moolenaar 		}
16283955d011SMarcel Moolenaar 	}
16293955d011SMarcel Moolenaar 	argv[argc] = NULL;
16303955d011SMarcel Moolenaar }
16313955d011SMarcel Moolenaar 
163206b9b3e0SSimon J. Gerraty static void
1633b0c40a00SSimon J. Gerraty JobWriteShellCommands(Job *job, GNode *gn, bool *out_run)
16343955d011SMarcel Moolenaar {
16353955d011SMarcel Moolenaar 	/*
163606b9b3e0SSimon J. Gerraty 	 * tfile is the name of a file into which all shell commands
163706b9b3e0SSimon J. Gerraty 	 * are put. It is removed before the child shell is executed,
163806b9b3e0SSimon J. Gerraty 	 * unless DEBUG(SCRIPT) is set.
16393955d011SMarcel Moolenaar 	 */
1640dba7b0efSSimon J. Gerraty 	char tfile[MAXPATHLEN];
164106b9b3e0SSimon J. Gerraty 	int tfd;		/* File descriptor to the temp file */
164206b9b3e0SSimon J. Gerraty 
1643dba7b0efSSimon J. Gerraty 	tfd = Job_TempFile(TMPPAT, tfile, sizeof tfile);
16443955d011SMarcel Moolenaar 
16453955d011SMarcel Moolenaar 	job->cmdFILE = fdopen(tfd, "w+");
1646e2eeea75SSimon J. Gerraty 	if (job->cmdFILE == NULL)
16473955d011SMarcel Moolenaar 		Punt("Could not fdopen %s", tfile);
1648e2eeea75SSimon J. Gerraty 
1649956e45f6SSimon J. Gerraty 	(void)fcntl(fileno(job->cmdFILE), F_SETFD, FD_CLOEXEC);
16503955d011SMarcel Moolenaar 
16513955d011SMarcel Moolenaar #ifdef USE_META
16523955d011SMarcel Moolenaar 	if (useMeta) {
16533955d011SMarcel Moolenaar 		meta_job_start(job, gn);
165406b9b3e0SSimon J. Gerraty 		if (gn->type & OP_SILENT)	/* might have changed */
1655b0c40a00SSimon J. Gerraty 			job->echo = false;
16563955d011SMarcel Moolenaar 	}
16573955d011SMarcel Moolenaar #endif
16583955d011SMarcel Moolenaar 
1659b0c40a00SSimon J. Gerraty 	*out_run = JobWriteCommands(job);
166006b9b3e0SSimon J. Gerraty }
166106b9b3e0SSimon J. Gerraty 
166206b9b3e0SSimon J. Gerraty /*
1663b0c40a00SSimon J. Gerraty  * Start a target-creation process going for the target described by gn.
166406b9b3e0SSimon J. Gerraty  *
166506b9b3e0SSimon J. Gerraty  * Results:
166606b9b3e0SSimon J. Gerraty  *	JOB_ERROR if there was an error in the commands, JOB_FINISHED
166706b9b3e0SSimon J. Gerraty  *	if there isn't actually anything left to do for the job and
166806b9b3e0SSimon J. Gerraty  *	JOB_RUNNING if the job has been started.
166906b9b3e0SSimon J. Gerraty  *
1670b0c40a00SSimon J. Gerraty  * Details:
167106b9b3e0SSimon J. Gerraty  *	A new Job node is created and added to the list of running
167206b9b3e0SSimon J. Gerraty  *	jobs. PMake is forked and a child shell created.
167306b9b3e0SSimon J. Gerraty  *
167406b9b3e0SSimon J. Gerraty  * NB: The return value is ignored by everyone.
167506b9b3e0SSimon J. Gerraty  */
167606b9b3e0SSimon J. Gerraty static JobStartResult
1677b0c40a00SSimon J. Gerraty JobStart(GNode *gn, bool special)
167806b9b3e0SSimon J. Gerraty {
167906b9b3e0SSimon J. Gerraty 	Job *job;		/* new job descriptor */
168006b9b3e0SSimon J. Gerraty 	char *argv[10];		/* Argument vector to shell */
1681b0c40a00SSimon J. Gerraty 	bool cmdsOK;		/* true if the nodes commands were all right */
1682b0c40a00SSimon J. Gerraty 	bool run;
168306b9b3e0SSimon J. Gerraty 
168406b9b3e0SSimon J. Gerraty 	for (job = job_table; job < job_table_end; job++) {
168506b9b3e0SSimon J. Gerraty 		if (job->status == JOB_ST_FREE)
168606b9b3e0SSimon J. Gerraty 			break;
168706b9b3e0SSimon J. Gerraty 	}
168806b9b3e0SSimon J. Gerraty 	if (job >= job_table_end)
168906b9b3e0SSimon J. Gerraty 		Punt("JobStart no job slots vacant");
169006b9b3e0SSimon J. Gerraty 
169106b9b3e0SSimon J. Gerraty 	memset(job, 0, sizeof *job);
169206b9b3e0SSimon J. Gerraty 	job->node = gn;
169306b9b3e0SSimon J. Gerraty 	job->tailCmds = NULL;
169406b9b3e0SSimon J. Gerraty 	job->status = JOB_ST_SET_UP;
169506b9b3e0SSimon J. Gerraty 
169606b9b3e0SSimon J. Gerraty 	job->special = special || gn->type & OP_SPECIAL;
169706b9b3e0SSimon J. Gerraty 	job->ignerr = opts.ignoreErrors || gn->type & OP_IGNORE;
16989f45a3c8SSimon J. Gerraty 	job->echo = !(opts.silent || gn->type & OP_SILENT);
169906b9b3e0SSimon J. Gerraty 
170006b9b3e0SSimon J. Gerraty 	/*
170106b9b3e0SSimon J. Gerraty 	 * Check the commands now so any attributes from .DEFAULT have a
170206b9b3e0SSimon J. Gerraty 	 * chance to migrate to the node.
170306b9b3e0SSimon J. Gerraty 	 */
170406b9b3e0SSimon J. Gerraty 	cmdsOK = Job_CheckCommands(gn, Error);
170506b9b3e0SSimon J. Gerraty 
170606b9b3e0SSimon J. Gerraty 	job->inPollfd = NULL;
1707dba7b0efSSimon J. Gerraty 
1708dba7b0efSSimon J. Gerraty 	if (Lst_IsEmpty(&gn->commands)) {
1709dba7b0efSSimon J. Gerraty 		job->cmdFILE = stdout;
1710b0c40a00SSimon J. Gerraty 		run = false;
1711b0c40a00SSimon J. Gerraty 
1712b0c40a00SSimon J. Gerraty 		/*
1713b0c40a00SSimon J. Gerraty 		 * We're serious here, but if the commands were bogus, we're
1714b0c40a00SSimon J. Gerraty 		 * also dead...
1715b0c40a00SSimon J. Gerraty 		 */
1716b0c40a00SSimon J. Gerraty 		if (!cmdsOK) {
17179f45a3c8SSimon J. Gerraty 			PrintOnError(gn, "\n");	/* provide some clue */
1718b0c40a00SSimon J. Gerraty 			DieHorribly();
1719b0c40a00SSimon J. Gerraty 		}
1720dba7b0efSSimon J. Gerraty 	} else if (((gn->type & OP_MAKE) && !opts.noRecursiveExecute) ||
17219f45a3c8SSimon J. Gerraty 	    (!opts.noExecute && !opts.touch)) {
1722dba7b0efSSimon J. Gerraty 		/*
1723dba7b0efSSimon J. Gerraty 		 * The above condition looks very similar to
1724dba7b0efSSimon J. Gerraty 		 * GNode_ShouldExecute but is subtly different.  It prevents
1725dba7b0efSSimon J. Gerraty 		 * that .MAKE targets are touched since these are usually
1726dba7b0efSSimon J. Gerraty 		 * virtual targets.
1727dba7b0efSSimon J. Gerraty 		 */
1728dba7b0efSSimon J. Gerraty 
1729b0c40a00SSimon J. Gerraty 		/*
1730b0c40a00SSimon J. Gerraty 		 * We're serious here, but if the commands were bogus, we're
1731b0c40a00SSimon J. Gerraty 		 * also dead...
1732b0c40a00SSimon J. Gerraty 		 */
1733b0c40a00SSimon J. Gerraty 		if (!cmdsOK) {
17349f45a3c8SSimon J. Gerraty 			PrintOnError(gn, "\n");	/* provide some clue */
1735b0c40a00SSimon J. Gerraty 			DieHorribly();
1736b0c40a00SSimon J. Gerraty 		}
1737b0c40a00SSimon J. Gerraty 
1738b0c40a00SSimon J. Gerraty 		JobWriteShellCommands(job, gn, &run);
1739dba7b0efSSimon J. Gerraty 		(void)fflush(job->cmdFILE);
1740956e45f6SSimon J. Gerraty 	} else if (!GNode_ShouldExecute(gn)) {
17413955d011SMarcel Moolenaar 		/*
1742b0c40a00SSimon J. Gerraty 		 * Just write all the commands to stdout in one fell swoop.
1743dba7b0efSSimon J. Gerraty 		 * This still sets up job->tailCmds correctly.
17443955d011SMarcel Moolenaar 		 */
174506b9b3e0SSimon J. Gerraty 		SwitchOutputTo(gn);
17463955d011SMarcel Moolenaar 		job->cmdFILE = stdout;
1747956e45f6SSimon J. Gerraty 		if (cmdsOK)
1748b0c40a00SSimon J. Gerraty 			JobWriteCommands(job);
1749b0c40a00SSimon J. Gerraty 		run = false;
1750dba7b0efSSimon J. Gerraty 		(void)fflush(job->cmdFILE);
17513955d011SMarcel Moolenaar 	} else {
175206b9b3e0SSimon J. Gerraty 		Job_Touch(gn, job->echo);
1753b0c40a00SSimon J. Gerraty 		run = false;
17543955d011SMarcel Moolenaar 	}
17553955d011SMarcel Moolenaar 
175606b9b3e0SSimon J. Gerraty 	/* If we're not supposed to execute a shell, don't. */
175706b9b3e0SSimon J. Gerraty 	if (!run) {
175806b9b3e0SSimon J. Gerraty 		if (!job->special)
17593955d011SMarcel Moolenaar 			Job_TokenReturn();
176006b9b3e0SSimon J. Gerraty 		/* Unlink and close the command file if we opened one */
1761e2eeea75SSimon J. Gerraty 		if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
17623955d011SMarcel Moolenaar 			(void)fclose(job->cmdFILE);
17633955d011SMarcel Moolenaar 			job->cmdFILE = NULL;
17643955d011SMarcel Moolenaar 		}
17653955d011SMarcel Moolenaar 
17663955d011SMarcel Moolenaar 		/*
176706b9b3e0SSimon J. Gerraty 		 * We only want to work our way up the graph if we aren't
176806b9b3e0SSimon J. Gerraty 		 * here because the commands for the job were no good.
17693955d011SMarcel Moolenaar 		 */
1770956e45f6SSimon J. Gerraty 		if (cmdsOK && aborting == ABORT_NONE) {
1771956e45f6SSimon J. Gerraty 			JobSaveCommands(job);
17723955d011SMarcel Moolenaar 			job->node->made = MADE;
17733955d011SMarcel Moolenaar 			Make_Update(job->node);
17743955d011SMarcel Moolenaar 		}
1775e2eeea75SSimon J. Gerraty 		job->status = JOB_ST_FREE;
17763955d011SMarcel Moolenaar 		return cmdsOK ? JOB_FINISHED : JOB_ERROR;
17773955d011SMarcel Moolenaar 	}
17783955d011SMarcel Moolenaar 
17793955d011SMarcel Moolenaar 	/*
178006b9b3e0SSimon J. Gerraty 	 * Set up the control arguments to the shell. This is based on the
178106b9b3e0SSimon J. Gerraty 	 * flags set earlier for this job.
17823955d011SMarcel Moolenaar 	 */
17833955d011SMarcel Moolenaar 	JobMakeArgv(job, argv);
17843955d011SMarcel Moolenaar 
17853955d011SMarcel Moolenaar 	/* Create the pipe by which we'll get the shell's output. */
17863955d011SMarcel Moolenaar 	JobCreatePipe(job, 3);
17873955d011SMarcel Moolenaar 
17883955d011SMarcel Moolenaar 	JobExec(job, argv);
17893841c287SSimon J. Gerraty 	return JOB_RUNNING;
17903955d011SMarcel Moolenaar }
17913955d011SMarcel Moolenaar 
179206b9b3e0SSimon J. Gerraty /*
1793b0c40a00SSimon J. Gerraty  * If the shell has an output filter (which only csh and ksh have by default),
1794b0c40a00SSimon J. Gerraty  * print the output of the child process, skipping the noPrint text of the
1795b0c40a00SSimon J. Gerraty  * shell.
1796b0c40a00SSimon J. Gerraty  *
1797b0c40a00SSimon J. Gerraty  * Return the part of the output that the calling function needs to output by
1798b0c40a00SSimon J. Gerraty  * itself.
179906b9b3e0SSimon J. Gerraty  */
18003955d011SMarcel Moolenaar static char *
1801b0c40a00SSimon J. Gerraty PrintFilteredOutput(char *cp, char *endp)	/* XXX: should all be const */
18023955d011SMarcel Moolenaar {
180306b9b3e0SSimon J. Gerraty 	char *ecp;		/* XXX: should be const */
18043955d011SMarcel Moolenaar 
180506b9b3e0SSimon J. Gerraty 	if (shell->noPrint == NULL || shell->noPrint[0] == '\0')
1806e2eeea75SSimon J. Gerraty 		return cp;
1807e2eeea75SSimon J. Gerraty 
180806b9b3e0SSimon J. Gerraty 	/*
180906b9b3e0SSimon J. Gerraty 	 * XXX: What happens if shell->noPrint occurs on the boundary of
181006b9b3e0SSimon J. Gerraty 	 * the buffer?  To work correctly in all cases, this should rather
181106b9b3e0SSimon J. Gerraty 	 * be a proper stream filter instead of doing string matching on
181206b9b3e0SSimon J. Gerraty 	 * selected chunks of the output.
181306b9b3e0SSimon J. Gerraty 	 */
181406b9b3e0SSimon J. Gerraty 	while ((ecp = strstr(cp, shell->noPrint)) != NULL) {
1815e2eeea75SSimon J. Gerraty 		if (ecp != cp) {
181606b9b3e0SSimon J. Gerraty 			*ecp = '\0';	/* XXX: avoid writing to the buffer */
18173955d011SMarcel Moolenaar 			/*
18183955d011SMarcel Moolenaar 			 * The only way there wouldn't be a newline after
18193955d011SMarcel Moolenaar 			 * this line is if it were the last in the buffer.
182006b9b3e0SSimon J. Gerraty 			 * however, since the noPrint output comes after it,
18213955d011SMarcel Moolenaar 			 * there must be a newline, so we don't print one.
18223955d011SMarcel Moolenaar 			 */
182306b9b3e0SSimon J. Gerraty 			/* XXX: What about null bytes in the output? */
18243955d011SMarcel Moolenaar 			(void)fprintf(stdout, "%s", cp);
18253955d011SMarcel Moolenaar 			(void)fflush(stdout);
18263955d011SMarcel Moolenaar 		}
182706b9b3e0SSimon J. Gerraty 		cp = ecp + shell->noPrintLen;
182806b9b3e0SSimon J. Gerraty 		if (cp == endp)
182906b9b3e0SSimon J. Gerraty 			break;
183006b9b3e0SSimon J. Gerraty 		cp++;		/* skip over the (XXX: assumed) newline */
1831e2eeea75SSimon J. Gerraty 		pp_skip_whitespace(&cp);
18323955d011SMarcel Moolenaar 	}
18333955d011SMarcel Moolenaar 	return cp;
18343955d011SMarcel Moolenaar }
18353955d011SMarcel Moolenaar 
1836e2eeea75SSimon J. Gerraty /*
1837e2eeea75SSimon J. Gerraty  * This function is called whenever there is something to read on the pipe.
1838e2eeea75SSimon J. Gerraty  * We collect more output from the given job and store it in the job's
1839e2eeea75SSimon J. Gerraty  * outBuf. If this makes up a line, we print it tagged by the job's
1840e2eeea75SSimon J. Gerraty  * identifier, as necessary.
1841e2eeea75SSimon J. Gerraty  *
1842e2eeea75SSimon J. Gerraty  * In the output of the shell, the 'noPrint' lines are removed. If the
1843e2eeea75SSimon J. Gerraty  * command is not alone on the line (the character after it is not \0 or
1844e2eeea75SSimon J. Gerraty  * \n), we do print whatever follows it.
18453955d011SMarcel Moolenaar  *
18463955d011SMarcel Moolenaar  * Input:
18473955d011SMarcel Moolenaar  *	job		the job whose output needs printing
1848b0c40a00SSimon J. Gerraty  *	finish		true if this is the last time we'll be called
18493955d011SMarcel Moolenaar  *			for this job
18503955d011SMarcel Moolenaar  */
1851956e45f6SSimon J. Gerraty static void
1852b0c40a00SSimon J. Gerraty CollectOutput(Job *job, bool finish)
18533955d011SMarcel Moolenaar {
1854b0c40a00SSimon J. Gerraty 	bool gotNL;		/* true if got a newline */
1855b0c40a00SSimon J. Gerraty 	bool fbuf;		/* true if our buffer filled up */
1856956e45f6SSimon J. Gerraty 	size_t nr;		/* number of bytes read */
1857956e45f6SSimon J. Gerraty 	size_t i;		/* auxiliary index into outBuf */
1858956e45f6SSimon J. Gerraty 	size_t max;		/* limit for i (end of current data) */
1859956e45f6SSimon J. Gerraty 	ssize_t nRead;		/* (Temporary) number of bytes read */
18603955d011SMarcel Moolenaar 
186106b9b3e0SSimon J. Gerraty 	/* Read as many bytes as will fit in the buffer. */
1862e2eeea75SSimon J. Gerraty again:
1863b0c40a00SSimon J. Gerraty 	gotNL = false;
1864b0c40a00SSimon J. Gerraty 	fbuf = false;
18653955d011SMarcel Moolenaar 
18663955d011SMarcel Moolenaar 	nRead = read(job->inPipe, &job->outBuf[job->curPos],
18673955d011SMarcel Moolenaar 	    JOB_BUFSIZE - job->curPos);
18683955d011SMarcel Moolenaar 	if (nRead < 0) {
18693955d011SMarcel Moolenaar 		if (errno == EAGAIN)
18703955d011SMarcel Moolenaar 			return;
18713955d011SMarcel Moolenaar 		if (DEBUG(JOB)) {
1872b0c40a00SSimon J. Gerraty 			perror("CollectOutput(piperead)");
18733955d011SMarcel Moolenaar 		}
18743955d011SMarcel Moolenaar 		nr = 0;
18753955d011SMarcel Moolenaar 	} else {
1876956e45f6SSimon J. Gerraty 		nr = (size_t)nRead;
18773955d011SMarcel Moolenaar 	}
18783955d011SMarcel Moolenaar 
18793955d011SMarcel Moolenaar 	/*
18803955d011SMarcel Moolenaar 	 * If we hit the end-of-file (the job is dead), we must flush its
18813955d011SMarcel Moolenaar 	 * remaining output, so pretend we read a newline if there's any
18823955d011SMarcel Moolenaar 	 * output remaining in the buffer.
18833955d011SMarcel Moolenaar 	 * Also clear the 'finish' flag so we stop looping.
18843955d011SMarcel Moolenaar 	 */
1885e2eeea75SSimon J. Gerraty 	if (nr == 0 && job->curPos != 0) {
18863955d011SMarcel Moolenaar 		job->outBuf[job->curPos] = '\n';
18873955d011SMarcel Moolenaar 		nr = 1;
1888b0c40a00SSimon J. Gerraty 		finish = false;
18893955d011SMarcel Moolenaar 	} else if (nr == 0) {
1890b0c40a00SSimon J. Gerraty 		finish = false;
18913955d011SMarcel Moolenaar 	}
18923955d011SMarcel Moolenaar 
18933955d011SMarcel Moolenaar 	/*
18943955d011SMarcel Moolenaar 	 * Look for the last newline in the bytes we just got. If there is
18953955d011SMarcel Moolenaar 	 * one, break out of the loop with 'i' as its index and gotNL set
1896b0c40a00SSimon J. Gerraty 	 * true.
18973955d011SMarcel Moolenaar 	 */
18983955d011SMarcel Moolenaar 	max = job->curPos + nr;
189906b9b3e0SSimon J. Gerraty 	for (i = job->curPos + nr - 1;
190006b9b3e0SSimon J. Gerraty 	     i >= job->curPos && i != (size_t)-1; i--) {
19013955d011SMarcel Moolenaar 		if (job->outBuf[i] == '\n') {
1902b0c40a00SSimon J. Gerraty 			gotNL = true;
19033955d011SMarcel Moolenaar 			break;
19043955d011SMarcel Moolenaar 		} else if (job->outBuf[i] == '\0') {
19053955d011SMarcel Moolenaar 			/*
1906b0c40a00SSimon J. Gerraty 			 * FIXME: The null characters are only replaced with
1907b0c40a00SSimon J. Gerraty 			 * space _after_ the last '\n'.  Everywhere else they
1908b0c40a00SSimon J. Gerraty 			 * hide the rest of the command output.
19093955d011SMarcel Moolenaar 			 */
19103955d011SMarcel Moolenaar 			job->outBuf[i] = ' ';
19113955d011SMarcel Moolenaar 		}
19123955d011SMarcel Moolenaar 	}
19133955d011SMarcel Moolenaar 
19143955d011SMarcel Moolenaar 	if (!gotNL) {
19153955d011SMarcel Moolenaar 		job->curPos += nr;
19163955d011SMarcel Moolenaar 		if (job->curPos == JOB_BUFSIZE) {
19173955d011SMarcel Moolenaar 			/*
19183955d011SMarcel Moolenaar 			 * If we've run out of buffer space, we have no choice
19193955d011SMarcel Moolenaar 			 * but to print the stuff. sigh.
19203955d011SMarcel Moolenaar 			 */
1921b0c40a00SSimon J. Gerraty 			fbuf = true;
19223955d011SMarcel Moolenaar 			i = job->curPos;
19233955d011SMarcel Moolenaar 		}
19243955d011SMarcel Moolenaar 	}
19253955d011SMarcel Moolenaar 	if (gotNL || fbuf) {
19263955d011SMarcel Moolenaar 		/*
19273955d011SMarcel Moolenaar 		 * Need to send the output to the screen. Null terminate it
19283955d011SMarcel Moolenaar 		 * first, overwriting the newline character if there was one.
19293955d011SMarcel Moolenaar 		 * So long as the line isn't one we should filter (according
19303955d011SMarcel Moolenaar 		 * to the shell description), we print the line, preceded
19313955d011SMarcel Moolenaar 		 * by a target banner if this target isn't the same as the
19323955d011SMarcel Moolenaar 		 * one for which we last printed something.
19333955d011SMarcel Moolenaar 		 * The rest of the data in the buffer are then shifted down
19343955d011SMarcel Moolenaar 		 * to the start of the buffer and curPos is set accordingly.
19353955d011SMarcel Moolenaar 		 */
19363955d011SMarcel Moolenaar 		job->outBuf[i] = '\0';
19373955d011SMarcel Moolenaar 		if (i >= job->curPos) {
19383955d011SMarcel Moolenaar 			char *cp;
19393955d011SMarcel Moolenaar 
1940b0c40a00SSimon J. Gerraty 			/*
1941b0c40a00SSimon J. Gerraty 			 * FIXME: SwitchOutputTo should be here, according to
1942b0c40a00SSimon J. Gerraty 			 * the comment above.  But since PrintOutput does not
1943b0c40a00SSimon J. Gerraty 			 * do anything in the default shell, this bug has gone
1944b0c40a00SSimon J. Gerraty 			 * unnoticed until now.
1945b0c40a00SSimon J. Gerraty 			 */
1946b0c40a00SSimon J. Gerraty 			cp = PrintFilteredOutput(job->outBuf, &job->outBuf[i]);
19473955d011SMarcel Moolenaar 
19483955d011SMarcel Moolenaar 			/*
1949b0c40a00SSimon J. Gerraty 			 * There's still more in the output buffer. This time,
195006b9b3e0SSimon J. Gerraty 			 * though, we know there's no newline at the end, so
195106b9b3e0SSimon J. Gerraty 			 * we add one of our own free will.
19523955d011SMarcel Moolenaar 			 */
19533955d011SMarcel Moolenaar 			if (*cp != '\0') {
19549f45a3c8SSimon J. Gerraty 				if (!opts.silent)
195506b9b3e0SSimon J. Gerraty 					SwitchOutputTo(job->node);
19563955d011SMarcel Moolenaar #ifdef USE_META
19573955d011SMarcel Moolenaar 				if (useMeta) {
195806b9b3e0SSimon J. Gerraty 					meta_job_output(job, cp,
195906b9b3e0SSimon J. Gerraty 					    gotNL ? "\n" : "");
19603955d011SMarcel Moolenaar 				}
19613955d011SMarcel Moolenaar #endif
196206b9b3e0SSimon J. Gerraty 				(void)fprintf(stdout, "%s%s", cp,
196306b9b3e0SSimon J. Gerraty 				    gotNL ? "\n" : "");
19643955d011SMarcel Moolenaar 				(void)fflush(stdout);
19653955d011SMarcel Moolenaar 			}
19663955d011SMarcel Moolenaar 		}
19673955d011SMarcel Moolenaar 		/*
196806b9b3e0SSimon J. Gerraty 		 * max is the last offset still in the buffer. Move any
196906b9b3e0SSimon J. Gerraty 		 * remaining characters to the start of the buffer and
197006b9b3e0SSimon J. Gerraty 		 * update the end marker curPos.
19713955d011SMarcel Moolenaar 		 */
1972db29cad8SSimon J. Gerraty 		if (i < max) {
197306b9b3e0SSimon J. Gerraty 			(void)memmove(job->outBuf, &job->outBuf[i + 1],
197406b9b3e0SSimon J. Gerraty 			    max - (i + 1));
1975db29cad8SSimon J. Gerraty 			job->curPos = max - (i + 1);
1976db29cad8SSimon J. Gerraty 		} else {
1977db29cad8SSimon J. Gerraty 			assert(i == max);
19783955d011SMarcel Moolenaar 			job->curPos = 0;
19793955d011SMarcel Moolenaar 		}
19803955d011SMarcel Moolenaar 	}
19813955d011SMarcel Moolenaar 	if (finish) {
19823955d011SMarcel Moolenaar 		/*
19833955d011SMarcel Moolenaar 		 * If the finish flag is true, we must loop until we hit
19843955d011SMarcel Moolenaar 		 * end-of-file on the pipe. This is guaranteed to happen
19853955d011SMarcel Moolenaar 		 * eventually since the other end of the pipe is now closed
19863955d011SMarcel Moolenaar 		 * (we closed it explicitly and the child has exited). When
1987b0c40a00SSimon J. Gerraty 		 * we do get an EOF, finish will be set false and we'll fall
19883955d011SMarcel Moolenaar 		 * through and out.
19893955d011SMarcel Moolenaar 		 */
1990e2eeea75SSimon J. Gerraty 		goto again;
19913955d011SMarcel Moolenaar 	}
19923955d011SMarcel Moolenaar }
19933955d011SMarcel Moolenaar 
19943955d011SMarcel Moolenaar static void
19953955d011SMarcel Moolenaar JobRun(GNode *targ)
19963955d011SMarcel Moolenaar {
1997956e45f6SSimon J. Gerraty #if 0
19983955d011SMarcel Moolenaar 	/*
1999956e45f6SSimon J. Gerraty 	 * Unfortunately it is too complicated to run .BEGIN, .END, and
2000956e45f6SSimon J. Gerraty 	 * .INTERRUPT job in the parallel job module.  As of 2020-09-25,
2001956e45f6SSimon J. Gerraty 	 * unit-tests/deptgt-end-jobs.mk hangs in an endless loop.
2002956e45f6SSimon J. Gerraty 	 *
2003956e45f6SSimon J. Gerraty 	 * Running these jobs in compat mode also guarantees that these
2004956e45f6SSimon J. Gerraty 	 * jobs do not overlap with other unrelated jobs.
20053955d011SMarcel Moolenaar 	 */
2006dba7b0efSSimon J. Gerraty 	GNodeList lst = LST_INIT;
2007dba7b0efSSimon J. Gerraty 	Lst_Append(&lst, targ);
2008dba7b0efSSimon J. Gerraty 	(void)Make_Run(&lst);
2009dba7b0efSSimon J. Gerraty 	Lst_Done(&lst);
2010b0c40a00SSimon J. Gerraty 	JobStart(targ, true);
201106b9b3e0SSimon J. Gerraty 	while (jobTokensRunning != 0) {
20123955d011SMarcel Moolenaar 		Job_CatchOutput();
20133955d011SMarcel Moolenaar 	}
20143955d011SMarcel Moolenaar #else
20153955d011SMarcel Moolenaar 	Compat_Make(targ, targ);
201606b9b3e0SSimon J. Gerraty 	/* XXX: Replace with GNode_IsError(gn) */
20173955d011SMarcel Moolenaar 	if (targ->made == ERROR) {
20189f45a3c8SSimon J. Gerraty 		PrintOnError(targ, "\n\nStop.\n");
20193955d011SMarcel Moolenaar 		exit(1);
20203955d011SMarcel Moolenaar 	}
20213955d011SMarcel Moolenaar #endif
20223955d011SMarcel Moolenaar }
20233955d011SMarcel Moolenaar 
202406b9b3e0SSimon J. Gerraty /*
202506b9b3e0SSimon J. Gerraty  * Handle the exit of a child. Called from Make_Make.
20263955d011SMarcel Moolenaar  *
20273955d011SMarcel Moolenaar  * The job descriptor is removed from the list of children.
20283955d011SMarcel Moolenaar  *
20293955d011SMarcel Moolenaar  * Notes:
20303955d011SMarcel Moolenaar  *	We do waits, blocking or not, according to the wisdom of our
20313955d011SMarcel Moolenaar  *	caller, until there are no more children to report. For each
20323955d011SMarcel Moolenaar  *	job, call JobFinish to finish things off.
20333955d011SMarcel Moolenaar  */
20343955d011SMarcel Moolenaar void
20353955d011SMarcel Moolenaar Job_CatchChildren(void)
20363955d011SMarcel Moolenaar {
20373955d011SMarcel Moolenaar 	int pid;		/* pid of dead child */
20383955d011SMarcel Moolenaar 	WAIT_T status;		/* Exit/termination status */
20393955d011SMarcel Moolenaar 
204006b9b3e0SSimon J. Gerraty 	/* Don't even bother if we know there's no one around. */
20413955d011SMarcel Moolenaar 	if (jobTokensRunning == 0)
20423955d011SMarcel Moolenaar 		return;
20433955d011SMarcel Moolenaar 
2044dba7b0efSSimon J. Gerraty 	/* Have we received SIGCHLD since last call? */
2045dba7b0efSSimon J. Gerraty 	if (caught_sigchld == 0)
2046dba7b0efSSimon J. Gerraty 		return;
2047dba7b0efSSimon J. Gerraty 	caught_sigchld = 0;
2048dba7b0efSSimon J. Gerraty 
20493955d011SMarcel Moolenaar 	while ((pid = waitpid((pid_t)-1, &status, WNOHANG | WUNTRACED)) > 0) {
205006b9b3e0SSimon J. Gerraty 		DEBUG2(JOB, "Process %d exited/stopped status %x.\n",
205106b9b3e0SSimon J. Gerraty 		    pid, WAIT_STATUS(status));
2052b0c40a00SSimon J. Gerraty 		JobReapChild(pid, status, true);
20533955d011SMarcel Moolenaar 	}
20543955d011SMarcel Moolenaar }
20553955d011SMarcel Moolenaar 
20563955d011SMarcel Moolenaar /*
20573955d011SMarcel Moolenaar  * It is possible that wait[pid]() was called from elsewhere,
20583955d011SMarcel Moolenaar  * this lets us reap jobs regardless.
20593955d011SMarcel Moolenaar  */
20603955d011SMarcel Moolenaar void
2061b0c40a00SSimon J. Gerraty JobReapChild(pid_t pid, WAIT_T status, bool isJobs)
20623955d011SMarcel Moolenaar {
20633955d011SMarcel Moolenaar 	Job *job;		/* job descriptor for dead child */
20643955d011SMarcel Moolenaar 
206506b9b3e0SSimon J. Gerraty 	/* Don't even bother if we know there's no one around. */
20663955d011SMarcel Moolenaar 	if (jobTokensRunning == 0)
20673955d011SMarcel Moolenaar 		return;
20683955d011SMarcel Moolenaar 
20693955d011SMarcel Moolenaar 	job = JobFindPid(pid, JOB_ST_RUNNING, isJobs);
20703955d011SMarcel Moolenaar 	if (job == NULL) {
20713955d011SMarcel Moolenaar 		if (isJobs) {
20723955d011SMarcel Moolenaar 			if (!lurking_children)
207306b9b3e0SSimon J. Gerraty 				Error("Child (%d) status %x not in table?",
207406b9b3e0SSimon J. Gerraty 				    pid, status);
20753955d011SMarcel Moolenaar 		}
20763955d011SMarcel Moolenaar 		return;		/* not ours */
20773955d011SMarcel Moolenaar 	}
20783955d011SMarcel Moolenaar 	if (WIFSTOPPED(status)) {
207906b9b3e0SSimon J. Gerraty 		DEBUG2(JOB, "Process %d (%s) stopped.\n",
208006b9b3e0SSimon J. Gerraty 		    job->pid, job->node->name);
20813955d011SMarcel Moolenaar 		if (!make_suspended) {
20823955d011SMarcel Moolenaar 			switch (WSTOPSIG(status)) {
20833955d011SMarcel Moolenaar 			case SIGTSTP:
208406b9b3e0SSimon J. Gerraty 				(void)printf("*** [%s] Suspended\n",
208506b9b3e0SSimon J. Gerraty 				    job->node->name);
20863955d011SMarcel Moolenaar 				break;
20873955d011SMarcel Moolenaar 			case SIGSTOP:
208806b9b3e0SSimon J. Gerraty 				(void)printf("*** [%s] Stopped\n",
208906b9b3e0SSimon J. Gerraty 				    job->node->name);
20903955d011SMarcel Moolenaar 				break;
20913955d011SMarcel Moolenaar 			default:
20923955d011SMarcel Moolenaar 				(void)printf("*** [%s] Stopped -- signal %d\n",
20933955d011SMarcel Moolenaar 				    job->node->name, WSTOPSIG(status));
20943955d011SMarcel Moolenaar 			}
2095b0c40a00SSimon J. Gerraty 			job->suspended = true;
20963955d011SMarcel Moolenaar 		}
20973955d011SMarcel Moolenaar 		(void)fflush(stdout);
20983955d011SMarcel Moolenaar 		return;
20993955d011SMarcel Moolenaar 	}
21003955d011SMarcel Moolenaar 
2101e2eeea75SSimon J. Gerraty 	job->status = JOB_ST_FINISHED;
21023955d011SMarcel Moolenaar 	job->exit_status = WAIT_STATUS(status);
21033955d011SMarcel Moolenaar 
21043955d011SMarcel Moolenaar 	JobFinish(job, status);
21053955d011SMarcel Moolenaar }
21063955d011SMarcel Moolenaar 
210706b9b3e0SSimon J. Gerraty /*
210806b9b3e0SSimon J. Gerraty  * Catch the output from our children, if we're using pipes do so. Otherwise
2109956e45f6SSimon J. Gerraty  * just block time until we get a signal(most likely a SIGCHLD) since there's
2110956e45f6SSimon J. Gerraty  * no point in just spinning when there's nothing to do and the reaping of a
211106b9b3e0SSimon J. Gerraty  * child can wait for a while.
211206b9b3e0SSimon J. Gerraty  */
21133955d011SMarcel Moolenaar void
21143955d011SMarcel Moolenaar Job_CatchOutput(void)
21153955d011SMarcel Moolenaar {
21163955d011SMarcel Moolenaar 	int nready;
21173955d011SMarcel Moolenaar 	Job *job;
2118956e45f6SSimon J. Gerraty 	unsigned int i;
21193955d011SMarcel Moolenaar 
21203955d011SMarcel Moolenaar 	(void)fflush(stdout);
21213955d011SMarcel Moolenaar 
21223955d011SMarcel Moolenaar 	/* The first fd in the list is the job token pipe */
21231748de26SSimon J. Gerraty 	do {
2124dba7b0efSSimon J. Gerraty 		nready = poll(fds + 1 - wantToken, fdsLen - 1 + wantToken,
212506b9b3e0SSimon J. Gerraty 		    POLL_MSEC);
21261748de26SSimon J. Gerraty 	} while (nready < 0 && errno == EINTR);
21273955d011SMarcel Moolenaar 
21281748de26SSimon J. Gerraty 	if (nready < 0)
21291748de26SSimon J. Gerraty 		Punt("poll: %s", strerror(errno));
21301748de26SSimon J. Gerraty 
21311748de26SSimon J. Gerraty 	if (nready > 0 && readyfd(&childExitJob)) {
21323955d011SMarcel Moolenaar 		char token = 0;
21331748de26SSimon J. Gerraty 		ssize_t count;
21341748de26SSimon J. Gerraty 		count = read(childExitJob.inPipe, &token, 1);
2135dba7b0efSSimon J. Gerraty 		if (count == 1) {
21363955d011SMarcel Moolenaar 			if (token == DO_JOB_RESUME[0])
213706b9b3e0SSimon J. Gerraty 				/*
213806b9b3e0SSimon J. Gerraty 				 * Complete relay requested from our SIGCONT
213906b9b3e0SSimon J. Gerraty 				 * handler
214006b9b3e0SSimon J. Gerraty 				 */
21413955d011SMarcel Moolenaar 				JobRestartJobs();
2142dba7b0efSSimon J. Gerraty 		} else if (count == 0)
2143dba7b0efSSimon J. Gerraty 			Punt("unexpected eof on token pipe");
214412904384SSimon J. Gerraty 		else if (errno != EAGAIN)
2145dba7b0efSSimon J. Gerraty 			Punt("token pipe read: %s", strerror(errno));
2146e2eeea75SSimon J. Gerraty 		nready--;
21473955d011SMarcel Moolenaar 	}
21483955d011SMarcel Moolenaar 
21491748de26SSimon J. Gerraty 	Job_CatchChildren();
21501748de26SSimon J. Gerraty 	if (nready == 0)
21513955d011SMarcel Moolenaar 		return;
21523955d011SMarcel Moolenaar 
2153dba7b0efSSimon J. Gerraty 	for (i = npseudojobs * nfds_per_job(); i < fdsLen; i++) {
215406b9b3e0SSimon J. Gerraty 		if (fds[i].revents == 0)
21553955d011SMarcel Moolenaar 			continue;
2156dba7b0efSSimon J. Gerraty 		job = jobByFdIndex[i];
2157e2eeea75SSimon J. Gerraty 		if (job->status == JOB_ST_RUNNING)
2158b0c40a00SSimon J. Gerraty 			CollectOutput(job, false);
215949caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
216049caa483SSimon J. Gerraty 		/*
216149caa483SSimon J. Gerraty 		 * With meta mode, we may have activity on the job's filemon
216206b9b3e0SSimon J. Gerraty 		 * descriptor too, which at the moment is any pollfd other
216306b9b3e0SSimon J. Gerraty 		 * than job->inPollfd.
216449caa483SSimon J. Gerraty 		 */
216549caa483SSimon J. Gerraty 		if (useMeta && job->inPollfd != &fds[i]) {
21669f45a3c8SSimon J. Gerraty 			if (meta_job_event(job) <= 0)
216749caa483SSimon J. Gerraty 				fds[i].events = 0;	/* never mind */
216849caa483SSimon J. Gerraty 		}
216949caa483SSimon J. Gerraty #endif
21701748de26SSimon J. Gerraty 		if (--nready == 0)
21711748de26SSimon J. Gerraty 			return;
21723955d011SMarcel Moolenaar 	}
21733955d011SMarcel Moolenaar }
21743955d011SMarcel Moolenaar 
217506b9b3e0SSimon J. Gerraty /*
217606b9b3e0SSimon J. Gerraty  * Start the creation of a target. Basically a front-end for JobStart used by
217706b9b3e0SSimon J. Gerraty  * the Make module.
217806b9b3e0SSimon J. Gerraty  */
21793955d011SMarcel Moolenaar void
21803955d011SMarcel Moolenaar Job_Make(GNode *gn)
21813955d011SMarcel Moolenaar {
2182b0c40a00SSimon J. Gerraty 	(void)JobStart(gn, false);
218306b9b3e0SSimon J. Gerraty }
218406b9b3e0SSimon J. Gerraty 
218506b9b3e0SSimon J. Gerraty static void
218606b9b3e0SSimon J. Gerraty InitShellNameAndPath(void)
218706b9b3e0SSimon J. Gerraty {
218806b9b3e0SSimon J. Gerraty 	shellName = shell->name;
218906b9b3e0SSimon J. Gerraty 
219006b9b3e0SSimon J. Gerraty #ifdef DEFSHELL_CUSTOM
219106b9b3e0SSimon J. Gerraty 	if (shellName[0] == '/') {
219206b9b3e0SSimon J. Gerraty 		shellPath = shellName;
219306b9b3e0SSimon J. Gerraty 		shellName = str_basename(shellPath);
219406b9b3e0SSimon J. Gerraty 		return;
219506b9b3e0SSimon J. Gerraty 	}
219606b9b3e0SSimon J. Gerraty #endif
219712904384SSimon J. Gerraty #ifdef DEFSHELL_PATH
219812904384SSimon J. Gerraty 	shellPath = DEFSHELL_PATH;
219912904384SSimon J. Gerraty #else
220006b9b3e0SSimon J. Gerraty 	shellPath = str_concat3(_PATH_DEFSHELLDIR, "/", shellName);
220112904384SSimon J. Gerraty #endif
22023955d011SMarcel Moolenaar }
22033955d011SMarcel Moolenaar 
22043955d011SMarcel Moolenaar void
22053955d011SMarcel Moolenaar Shell_Init(void)
22063955d011SMarcel Moolenaar {
220706b9b3e0SSimon J. Gerraty 	if (shellPath == NULL)
220806b9b3e0SSimon J. Gerraty 		InitShellNameAndPath();
220906b9b3e0SSimon J. Gerraty 
2210dba7b0efSSimon J. Gerraty 	Var_SetWithFlags(SCOPE_CMDLINE, ".SHELL", shellPath, VAR_SET_READONLY);
221106b9b3e0SSimon J. Gerraty 	if (shell->errFlag == NULL)
221206b9b3e0SSimon J. Gerraty 		shell->errFlag = "";
221306b9b3e0SSimon J. Gerraty 	if (shell->echoFlag == NULL)
221406b9b3e0SSimon J. Gerraty 		shell->echoFlag = "";
221506b9b3e0SSimon J. Gerraty 	if (shell->hasErrCtl && shell->errFlag[0] != '\0') {
221606b9b3e0SSimon J. Gerraty 		if (shellErrFlag != NULL &&
221706b9b3e0SSimon J. Gerraty 		    strcmp(shell->errFlag, &shellErrFlag[1]) != 0) {
221851ee2c1cSSimon J. Gerraty 			free(shellErrFlag);
221951ee2c1cSSimon J. Gerraty 			shellErrFlag = NULL;
222051ee2c1cSSimon J. Gerraty 		}
22219f45a3c8SSimon J. Gerraty 		if (shellErrFlag == NULL)
22229f45a3c8SSimon J. Gerraty 			shellErrFlag = str_concat2("-", shell->errFlag);
222306b9b3e0SSimon J. Gerraty 	} else if (shellErrFlag != NULL) {
222451ee2c1cSSimon J. Gerraty 		free(shellErrFlag);
222551ee2c1cSSimon J. Gerraty 		shellErrFlag = NULL;
222651ee2c1cSSimon J. Gerraty 	}
22273955d011SMarcel Moolenaar }
22283955d011SMarcel Moolenaar 
222906b9b3e0SSimon J. Gerraty /*
223006b9b3e0SSimon J. Gerraty  * Return the string literal that is used in the current command shell
223106b9b3e0SSimon J. Gerraty  * to produce a newline character.
223206b9b3e0SSimon J. Gerraty  */
22333955d011SMarcel Moolenaar const char *
22343955d011SMarcel Moolenaar Shell_GetNewline(void)
22353955d011SMarcel Moolenaar {
223606b9b3e0SSimon J. Gerraty 	return shell->newline;
22373955d011SMarcel Moolenaar }
22383955d011SMarcel Moolenaar 
22393955d011SMarcel Moolenaar void
22403955d011SMarcel Moolenaar Job_SetPrefix(void)
22413955d011SMarcel Moolenaar {
224206b9b3e0SSimon J. Gerraty 	if (targPrefix != NULL) {
22433955d011SMarcel Moolenaar 		free(targPrefix);
2244dba7b0efSSimon J. Gerraty 	} else if (!Var_Exists(SCOPE_GLOBAL, MAKE_JOB_PREFIX)) {
2245dba7b0efSSimon J. Gerraty 		Global_Set(MAKE_JOB_PREFIX, "---");
22463955d011SMarcel Moolenaar 	}
22473955d011SMarcel Moolenaar 
2248956e45f6SSimon J. Gerraty 	(void)Var_Subst("${" MAKE_JOB_PREFIX "}",
2249dba7b0efSSimon J. Gerraty 	    SCOPE_GLOBAL, VARE_WANTRES, &targPrefix);
2250956e45f6SSimon J. Gerraty 	/* TODO: handle errors */
22513955d011SMarcel Moolenaar }
22523955d011SMarcel Moolenaar 
225306b9b3e0SSimon J. Gerraty static void
225406b9b3e0SSimon J. Gerraty AddSig(int sig, SignalProc handler)
225506b9b3e0SSimon J. Gerraty {
225606b9b3e0SSimon J. Gerraty 	if (bmake_signal(sig, SIG_IGN) != SIG_IGN) {
225706b9b3e0SSimon J. Gerraty 		sigaddset(&caught_signals, sig);
225806b9b3e0SSimon J. Gerraty 		(void)bmake_signal(sig, handler);
225906b9b3e0SSimon J. Gerraty 	}
226006b9b3e0SSimon J. Gerraty }
226106b9b3e0SSimon J. Gerraty 
22622c3632d1SSimon J. Gerraty /* Initialize the process module. */
22633955d011SMarcel Moolenaar void
22643955d011SMarcel Moolenaar Job_Init(void)
22653955d011SMarcel Moolenaar {
2266d191243dSSimon J. Gerraty 	Job_SetPrefix();
22673955d011SMarcel Moolenaar 	/* Allocate space for all the job info */
2268956e45f6SSimon J. Gerraty 	job_table = bmake_malloc((size_t)opts.maxJobs * sizeof *job_table);
2269956e45f6SSimon J. Gerraty 	memset(job_table, 0, (size_t)opts.maxJobs * sizeof *job_table);
2270956e45f6SSimon J. Gerraty 	job_table_end = job_table + opts.maxJobs;
22713955d011SMarcel Moolenaar 	wantToken = 0;
2272dba7b0efSSimon J. Gerraty 	caught_sigchld = 0;
22733955d011SMarcel Moolenaar 
2274956e45f6SSimon J. Gerraty 	aborting = ABORT_NONE;
227506b9b3e0SSimon J. Gerraty 	job_errors = 0;
22763955d011SMarcel Moolenaar 
227768c4481aSSimon J. Gerraty 	Always_pass_job_queue = GetBooleanExpr(MAKE_ALWAYS_PASS_JOB_QUEUE,
22789a4bc556SSimon J. Gerraty 	    Always_pass_job_queue);
22799a4bc556SSimon J. Gerraty 
228068c4481aSSimon J. Gerraty 	Job_error_token = GetBooleanExpr(MAKE_JOB_ERROR_TOKEN, Job_error_token);
228168c4481aSSimon J. Gerraty 
22822d395cb5SSimon J. Gerraty 
22833955d011SMarcel Moolenaar 	/*
22843955d011SMarcel Moolenaar 	 * There is a non-zero chance that we already have children.
22853955d011SMarcel Moolenaar 	 * eg after 'make -f- <<EOF'
228606b9b3e0SSimon J. Gerraty 	 * Since their termination causes a 'Child (pid) not in table'
228706b9b3e0SSimon J. Gerraty 	 * message, Collect the status of any that are already dead, and
228806b9b3e0SSimon J. Gerraty 	 * suppress the error message if there are any undead ones.
22893955d011SMarcel Moolenaar 	 */
22903955d011SMarcel Moolenaar 	for (;;) {
229106b9b3e0SSimon J. Gerraty 		int rval;
229206b9b3e0SSimon J. Gerraty 		WAIT_T status;
229306b9b3e0SSimon J. Gerraty 
22943955d011SMarcel Moolenaar 		rval = waitpid((pid_t)-1, &status, WNOHANG);
22953955d011SMarcel Moolenaar 		if (rval > 0)
22963955d011SMarcel Moolenaar 			continue;
22973955d011SMarcel Moolenaar 		if (rval == 0)
2298b0c40a00SSimon J. Gerraty 			lurking_children = true;
22993955d011SMarcel Moolenaar 		break;
23003955d011SMarcel Moolenaar 	}
23013955d011SMarcel Moolenaar 
23023955d011SMarcel Moolenaar 	Shell_Init();
23033955d011SMarcel Moolenaar 
23043955d011SMarcel Moolenaar 	JobCreatePipe(&childExitJob, 3);
23053955d011SMarcel Moolenaar 
2306dba7b0efSSimon J. Gerraty 	{
230749caa483SSimon J. Gerraty 		/* Preallocate enough for the maximum number of jobs. */
2308dba7b0efSSimon J. Gerraty 		size_t nfds = (npseudojobs + (size_t)opts.maxJobs) *
2309dba7b0efSSimon J. Gerraty 			      nfds_per_job();
2310dba7b0efSSimon J. Gerraty 		fds = bmake_malloc(sizeof *fds * nfds);
2311dba7b0efSSimon J. Gerraty 		jobByFdIndex = bmake_malloc(sizeof *jobByFdIndex * nfds);
2312dba7b0efSSimon J. Gerraty 	}
23133955d011SMarcel Moolenaar 
23143955d011SMarcel Moolenaar 	/* These are permanent entries and take slots 0 and 1 */
23153955d011SMarcel Moolenaar 	watchfd(&tokenWaitJob);
23163955d011SMarcel Moolenaar 	watchfd(&childExitJob);
23173955d011SMarcel Moolenaar 
23183955d011SMarcel Moolenaar 	sigemptyset(&caught_signals);
23193955d011SMarcel Moolenaar 	/*
23203955d011SMarcel Moolenaar 	 * Install a SIGCHLD handler.
23213955d011SMarcel Moolenaar 	 */
23223955d011SMarcel Moolenaar 	(void)bmake_signal(SIGCHLD, JobChildSig);
23233955d011SMarcel Moolenaar 	sigaddset(&caught_signals, SIGCHLD);
23243955d011SMarcel Moolenaar 
23253955d011SMarcel Moolenaar 	/*
23263955d011SMarcel Moolenaar 	 * Catch the four signals that POSIX specifies if they aren't ignored.
23273955d011SMarcel Moolenaar 	 * JobPassSig will take care of calling JobInterrupt if appropriate.
23283955d011SMarcel Moolenaar 	 */
232906b9b3e0SSimon J. Gerraty 	AddSig(SIGINT, JobPassSig_int);
233006b9b3e0SSimon J. Gerraty 	AddSig(SIGHUP, JobPassSig_term);
233106b9b3e0SSimon J. Gerraty 	AddSig(SIGTERM, JobPassSig_term);
233206b9b3e0SSimon J. Gerraty 	AddSig(SIGQUIT, JobPassSig_term);
23333955d011SMarcel Moolenaar 
23343955d011SMarcel Moolenaar 	/*
23353955d011SMarcel Moolenaar 	 * There are additional signals that need to be caught and passed if
23363955d011SMarcel Moolenaar 	 * either the export system wants to be told directly of signals or if
23373955d011SMarcel Moolenaar 	 * we're giving each job its own process group (since then it won't get
23383955d011SMarcel Moolenaar 	 * signals from the terminal driver as we own the terminal)
23393955d011SMarcel Moolenaar 	 */
234006b9b3e0SSimon J. Gerraty 	AddSig(SIGTSTP, JobPassSig_suspend);
234106b9b3e0SSimon J. Gerraty 	AddSig(SIGTTOU, JobPassSig_suspend);
234206b9b3e0SSimon J. Gerraty 	AddSig(SIGTTIN, JobPassSig_suspend);
234306b9b3e0SSimon J. Gerraty 	AddSig(SIGWINCH, JobCondPassSig);
234406b9b3e0SSimon J. Gerraty 	AddSig(SIGCONT, JobContinueSig);
23453955d011SMarcel Moolenaar 
23461748de26SSimon J. Gerraty 	(void)Job_RunTarget(".BEGIN", NULL);
23479f45a3c8SSimon J. Gerraty 	/*
23489f45a3c8SSimon J. Gerraty 	 * Create the .END node now, even though no code in the unit tests
23499f45a3c8SSimon J. Gerraty 	 * depends on it.  See also Targ_GetEndNode in Compat_Run.
23509f45a3c8SSimon J. Gerraty 	 */
2351956e45f6SSimon J. Gerraty 	(void)Targ_GetEndNode();
23523955d011SMarcel Moolenaar }
23533955d011SMarcel Moolenaar 
235406b9b3e0SSimon J. Gerraty static void
235506b9b3e0SSimon J. Gerraty DelSig(int sig)
23563955d011SMarcel Moolenaar {
235706b9b3e0SSimon J. Gerraty 	if (sigismember(&caught_signals, sig) != 0)
235806b9b3e0SSimon J. Gerraty 		(void)bmake_signal(sig, SIG_DFL);
23593955d011SMarcel Moolenaar }
23603955d011SMarcel Moolenaar 
236106b9b3e0SSimon J. Gerraty static void
236206b9b3e0SSimon J. Gerraty JobSigReset(void)
236306b9b3e0SSimon J. Gerraty {
236406b9b3e0SSimon J. Gerraty 	DelSig(SIGINT);
236506b9b3e0SSimon J. Gerraty 	DelSig(SIGHUP);
236606b9b3e0SSimon J. Gerraty 	DelSig(SIGQUIT);
236706b9b3e0SSimon J. Gerraty 	DelSig(SIGTERM);
236806b9b3e0SSimon J. Gerraty 	DelSig(SIGTSTP);
236906b9b3e0SSimon J. Gerraty 	DelSig(SIGTTOU);
237006b9b3e0SSimon J. Gerraty 	DelSig(SIGTTIN);
237106b9b3e0SSimon J. Gerraty 	DelSig(SIGWINCH);
237206b9b3e0SSimon J. Gerraty 	DelSig(SIGCONT);
23733955d011SMarcel Moolenaar 	(void)bmake_signal(SIGCHLD, SIG_DFL);
23743955d011SMarcel Moolenaar }
23753955d011SMarcel Moolenaar 
23762c3632d1SSimon J. Gerraty /* Find a shell in 'shells' given its name, or return NULL. */
23773955d011SMarcel Moolenaar static Shell *
2378956e45f6SSimon J. Gerraty FindShellByName(const char *name)
23793955d011SMarcel Moolenaar {
2380956e45f6SSimon J. Gerraty 	Shell *sh = shells;
2381956e45f6SSimon J. Gerraty 	const Shell *shellsEnd = sh + sizeof shells / sizeof shells[0];
23823955d011SMarcel Moolenaar 
2383956e45f6SSimon J. Gerraty 	for (sh = shells; sh < shellsEnd; sh++) {
23843955d011SMarcel Moolenaar 		if (strcmp(name, sh->name) == 0)
23853841c287SSimon J. Gerraty 			return sh;
23863955d011SMarcel Moolenaar 	}
23873955d011SMarcel Moolenaar 	return NULL;
23883955d011SMarcel Moolenaar }
23893955d011SMarcel Moolenaar 
239006b9b3e0SSimon J. Gerraty /*
239106b9b3e0SSimon J. Gerraty  * Parse a shell specification and set up 'shell', shellPath and
239206b9b3e0SSimon J. Gerraty  * shellName appropriately.
23933955d011SMarcel Moolenaar  *
23943955d011SMarcel Moolenaar  * Input:
23953955d011SMarcel Moolenaar  *	line		The shell spec
23963955d011SMarcel Moolenaar  *
23973955d011SMarcel Moolenaar  * Results:
2398b0c40a00SSimon J. Gerraty  *	false if the specification was incorrect.
23993955d011SMarcel Moolenaar  *
24003955d011SMarcel Moolenaar  * Side Effects:
240106b9b3e0SSimon J. Gerraty  *	'shell' points to a Shell structure (either predefined or
24023955d011SMarcel Moolenaar  *	created from the shell spec), shellPath is the full path of the
240306b9b3e0SSimon J. Gerraty  *	shell described by 'shell', while shellName is just the
24043955d011SMarcel Moolenaar  *	final component of shellPath.
24053955d011SMarcel Moolenaar  *
24063955d011SMarcel Moolenaar  * Notes:
24073955d011SMarcel Moolenaar  *	A shell specification consists of a .SHELL target, with dependency
24083955d011SMarcel Moolenaar  *	operator, followed by a series of blank-separated words. Double
24093955d011SMarcel Moolenaar  *	quotes can be used to use blanks in words. A backslash escapes
24103955d011SMarcel Moolenaar  *	anything (most notably a double-quote and a space) and
24113955d011SMarcel Moolenaar  *	provides the functionality it does in C. Each word consists of
24123955d011SMarcel Moolenaar  *	keyword and value separated by an equal sign. There should be no
24133955d011SMarcel Moolenaar  *	unnecessary spaces in the word. The keywords are as follows:
24143955d011SMarcel Moolenaar  *	    name	Name of shell.
24153955d011SMarcel Moolenaar  *	    path	Location of shell.
24163955d011SMarcel Moolenaar  *	    quiet	Command to turn off echoing.
24173955d011SMarcel Moolenaar  *	    echo	Command to turn echoing on
24183955d011SMarcel Moolenaar  *	    filter	Result of turning off echoing that shouldn't be
24193955d011SMarcel Moolenaar  *			printed.
24203955d011SMarcel Moolenaar  *	    echoFlag	Flag to turn echoing on at the start
24213955d011SMarcel Moolenaar  *	    errFlag	Flag to turn error checking on at the start
24223955d011SMarcel Moolenaar  *	    hasErrCtl	True if shell has error checking control
24233955d011SMarcel Moolenaar  *	    newline	String literal to represent a newline char
24243955d011SMarcel Moolenaar  *	    check	Command to turn on error checking if hasErrCtl
2425b0c40a00SSimon J. Gerraty  *			is true or template of command to echo a command
24263955d011SMarcel Moolenaar  *			for which error checking is off if hasErrCtl is
2427b0c40a00SSimon J. Gerraty  *			false.
24283955d011SMarcel Moolenaar  *	    ignore	Command to turn off error checking if hasErrCtl
2429b0c40a00SSimon J. Gerraty  *			is true or template of command to execute a
24303955d011SMarcel Moolenaar  *			command so as to ignore any errors it returns if
2431b0c40a00SSimon J. Gerraty  *			hasErrCtl is false.
24323955d011SMarcel Moolenaar  */
2433b0c40a00SSimon J. Gerraty bool
24343955d011SMarcel Moolenaar Job_ParseShell(char *line)
24353955d011SMarcel Moolenaar {
24362c3632d1SSimon J. Gerraty 	Words wordsList;
24373955d011SMarcel Moolenaar 	char **words;
24383955d011SMarcel Moolenaar 	char **argv;
24392c3632d1SSimon J. Gerraty 	size_t argc;
24403955d011SMarcel Moolenaar 	char *path;
24413955d011SMarcel Moolenaar 	Shell newShell;
2442b0c40a00SSimon J. Gerraty 	bool fullSpec = false;
24433955d011SMarcel Moolenaar 	Shell *sh;
24443955d011SMarcel Moolenaar 
244506b9b3e0SSimon J. Gerraty 	/* XXX: don't use line as an iterator variable */
2446956e45f6SSimon J. Gerraty 	pp_skip_whitespace(&line);
24473955d011SMarcel Moolenaar 
2448dba7b0efSSimon J. Gerraty 	free(shell_freeIt);
24493955d011SMarcel Moolenaar 
2450e2eeea75SSimon J. Gerraty 	memset(&newShell, 0, sizeof newShell);
24513955d011SMarcel Moolenaar 
24523955d011SMarcel Moolenaar 	/*
24533955d011SMarcel Moolenaar 	 * Parse the specification by keyword
24543955d011SMarcel Moolenaar 	 */
2455b0c40a00SSimon J. Gerraty 	wordsList = Str_Words(line, true);
24562c3632d1SSimon J. Gerraty 	words = wordsList.words;
24572c3632d1SSimon J. Gerraty 	argc = wordsList.len;
24582c3632d1SSimon J. Gerraty 	path = wordsList.freeIt;
24593955d011SMarcel Moolenaar 	if (words == NULL) {
24603955d011SMarcel Moolenaar 		Error("Unterminated quoted string [%s]", line);
2461b0c40a00SSimon J. Gerraty 		return false;
24623955d011SMarcel Moolenaar 	}
2463dba7b0efSSimon J. Gerraty 	shell_freeIt = path;
24643955d011SMarcel Moolenaar 
24653955d011SMarcel Moolenaar 	for (path = NULL, argv = words; argc != 0; argc--, argv++) {
2466956e45f6SSimon J. Gerraty 		char *arg = *argv;
2467956e45f6SSimon J. Gerraty 		if (strncmp(arg, "path=", 5) == 0) {
2468956e45f6SSimon J. Gerraty 			path = arg + 5;
2469956e45f6SSimon J. Gerraty 		} else if (strncmp(arg, "name=", 5) == 0) {
2470956e45f6SSimon J. Gerraty 			newShell.name = arg + 5;
24713955d011SMarcel Moolenaar 		} else {
2472956e45f6SSimon J. Gerraty 			if (strncmp(arg, "quiet=", 6) == 0) {
2473956e45f6SSimon J. Gerraty 				newShell.echoOff = arg + 6;
2474956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "echo=", 5) == 0) {
2475956e45f6SSimon J. Gerraty 				newShell.echoOn = arg + 5;
2476956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "filter=", 7) == 0) {
2477956e45f6SSimon J. Gerraty 				newShell.noPrint = arg + 7;
2478956e45f6SSimon J. Gerraty 				newShell.noPrintLen = strlen(newShell.noPrint);
2479956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "echoFlag=", 9) == 0) {
248006b9b3e0SSimon J. Gerraty 				newShell.echoFlag = arg + 9;
2481956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "errFlag=", 8) == 0) {
248206b9b3e0SSimon J. Gerraty 				newShell.errFlag = arg + 8;
2483956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "hasErrCtl=", 10) == 0) {
2484956e45f6SSimon J. Gerraty 				char c = arg[10];
2485956e45f6SSimon J. Gerraty 				newShell.hasErrCtl = c == 'Y' || c == 'y' ||
2486956e45f6SSimon J. Gerraty 						     c == 'T' || c == 't';
2487956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "newline=", 8) == 0) {
2488956e45f6SSimon J. Gerraty 				newShell.newline = arg + 8;
2489956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "check=", 6) == 0) {
24909f45a3c8SSimon J. Gerraty 				/*
24919f45a3c8SSimon J. Gerraty 				 * Before 2020-12-10, these two variables had
24929f45a3c8SSimon J. Gerraty 				 * been a single variable.
24939f45a3c8SSimon J. Gerraty 				 */
249406b9b3e0SSimon J. Gerraty 				newShell.errOn = arg + 6;
249506b9b3e0SSimon J. Gerraty 				newShell.echoTmpl = arg + 6;
2496956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "ignore=", 7) == 0) {
24979f45a3c8SSimon J. Gerraty 				/*
24989f45a3c8SSimon J. Gerraty 				 * Before 2020-12-10, these two variables had
24999f45a3c8SSimon J. Gerraty 				 * been a single variable.
25009f45a3c8SSimon J. Gerraty 				 */
250106b9b3e0SSimon J. Gerraty 				newShell.errOff = arg + 7;
250206b9b3e0SSimon J. Gerraty 				newShell.runIgnTmpl = arg + 7;
2503956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "errout=", 7) == 0) {
250406b9b3e0SSimon J. Gerraty 				newShell.runChkTmpl = arg + 7;
2505956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "comment=", 8) == 0) {
2506956e45f6SSimon J. Gerraty 				newShell.commentChar = arg[8];
25073955d011SMarcel Moolenaar 			} else {
250806b9b3e0SSimon J. Gerraty 				Parse_Error(PARSE_FATAL,
250906b9b3e0SSimon J. Gerraty 				    "Unknown keyword \"%s\"", arg);
25103955d011SMarcel Moolenaar 				free(words);
2511b0c40a00SSimon J. Gerraty 				return false;
25123955d011SMarcel Moolenaar 			}
2513b0c40a00SSimon J. Gerraty 			fullSpec = true;
25143955d011SMarcel Moolenaar 		}
25153955d011SMarcel Moolenaar 	}
25163955d011SMarcel Moolenaar 
25173955d011SMarcel Moolenaar 	if (path == NULL) {
25183955d011SMarcel Moolenaar 		/*
251906b9b3e0SSimon J. Gerraty 		 * If no path was given, the user wants one of the
252006b9b3e0SSimon J. Gerraty 		 * pre-defined shells, yes? So we find the one s/he wants
252106b9b3e0SSimon J. Gerraty 		 * with the help of FindShellByName and set things up the
252206b9b3e0SSimon J. Gerraty 		 * right way. shellPath will be set up by Shell_Init.
25233955d011SMarcel Moolenaar 		 */
25243955d011SMarcel Moolenaar 		if (newShell.name == NULL) {
252506b9b3e0SSimon J. Gerraty 			Parse_Error(PARSE_FATAL,
252606b9b3e0SSimon J. Gerraty 			    "Neither path nor name specified");
25273955d011SMarcel Moolenaar 			free(words);
2528b0c40a00SSimon J. Gerraty 			return false;
25293955d011SMarcel Moolenaar 		} else {
2530956e45f6SSimon J. Gerraty 			if ((sh = FindShellByName(newShell.name)) == NULL) {
253106b9b3e0SSimon J. Gerraty 				Parse_Error(PARSE_WARNING,
253206b9b3e0SSimon J. Gerraty 				    "%s: No matching shell", newShell.name);
25333955d011SMarcel Moolenaar 				free(words);
2534b0c40a00SSimon J. Gerraty 				return false;
25353955d011SMarcel Moolenaar 			}
253606b9b3e0SSimon J. Gerraty 			shell = sh;
25373955d011SMarcel Moolenaar 			shellName = newShell.name;
253806b9b3e0SSimon J. Gerraty 			if (shellPath != NULL) {
253906b9b3e0SSimon J. Gerraty 				/*
254006b9b3e0SSimon J. Gerraty 				 * Shell_Init has already been called!
254106b9b3e0SSimon J. Gerraty 				 * Do it again.
254206b9b3e0SSimon J. Gerraty 				 */
25433955d011SMarcel Moolenaar 				free(UNCONST(shellPath));
25443955d011SMarcel Moolenaar 				shellPath = NULL;
25453955d011SMarcel Moolenaar 				Shell_Init();
25463955d011SMarcel Moolenaar 			}
25473955d011SMarcel Moolenaar 		}
25483955d011SMarcel Moolenaar 	} else {
25493955d011SMarcel Moolenaar 		/*
255006b9b3e0SSimon J. Gerraty 		 * The user provided a path. If s/he gave nothing else
2551b0c40a00SSimon J. Gerraty 		 * (fullSpec is false), try and find a matching shell in the
255206b9b3e0SSimon J. Gerraty 		 * ones we know of. Else we just take the specification at
255306b9b3e0SSimon J. Gerraty 		 * its word and copy it to a new location. In either case,
255406b9b3e0SSimon J. Gerraty 		 * we need to record the path the user gave for the shell.
25553955d011SMarcel Moolenaar 		 */
25563955d011SMarcel Moolenaar 		shellPath = path;
25573955d011SMarcel Moolenaar 		path = strrchr(path, '/');
25583955d011SMarcel Moolenaar 		if (path == NULL) {
25593955d011SMarcel Moolenaar 			path = UNCONST(shellPath);
25603955d011SMarcel Moolenaar 		} else {
2561956e45f6SSimon J. Gerraty 			path++;
25623955d011SMarcel Moolenaar 		}
25633955d011SMarcel Moolenaar 		if (newShell.name != NULL) {
25643955d011SMarcel Moolenaar 			shellName = newShell.name;
25653955d011SMarcel Moolenaar 		} else {
25663955d011SMarcel Moolenaar 			shellName = path;
25673955d011SMarcel Moolenaar 		}
25683955d011SMarcel Moolenaar 		if (!fullSpec) {
2569956e45f6SSimon J. Gerraty 			if ((sh = FindShellByName(shellName)) == NULL) {
257006b9b3e0SSimon J. Gerraty 				Parse_Error(PARSE_WARNING,
257106b9b3e0SSimon J. Gerraty 				    "%s: No matching shell", shellName);
25723955d011SMarcel Moolenaar 				free(words);
2573b0c40a00SSimon J. Gerraty 				return false;
25743955d011SMarcel Moolenaar 			}
257506b9b3e0SSimon J. Gerraty 			shell = sh;
25763955d011SMarcel Moolenaar 		} else {
257706b9b3e0SSimon J. Gerraty 			shell = bmake_malloc(sizeof *shell);
257806b9b3e0SSimon J. Gerraty 			*shell = newShell;
25793955d011SMarcel Moolenaar 		}
258051ee2c1cSSimon J. Gerraty 		/* this will take care of shellErrFlag */
258151ee2c1cSSimon J. Gerraty 		Shell_Init();
25823955d011SMarcel Moolenaar 	}
25833955d011SMarcel Moolenaar 
258406b9b3e0SSimon J. Gerraty 	if (shell->echoOn != NULL && shell->echoOff != NULL)
2585b0c40a00SSimon J. Gerraty 		shell->hasEchoCtl = true;
25863955d011SMarcel Moolenaar 
258706b9b3e0SSimon J. Gerraty 	if (!shell->hasErrCtl) {
258806b9b3e0SSimon J. Gerraty 		if (shell->echoTmpl == NULL)
258906b9b3e0SSimon J. Gerraty 			shell->echoTmpl = "";
259006b9b3e0SSimon J. Gerraty 		if (shell->runIgnTmpl == NULL)
259106b9b3e0SSimon J. Gerraty 			shell->runIgnTmpl = "%s\n";
25923955d011SMarcel Moolenaar 	}
25933955d011SMarcel Moolenaar 
25943955d011SMarcel Moolenaar 	/*
259506b9b3e0SSimon J. Gerraty 	 * Do not free up the words themselves, since they might be in use
259606b9b3e0SSimon J. Gerraty 	 * by the shell specification.
25973955d011SMarcel Moolenaar 	 */
25983955d011SMarcel Moolenaar 	free(words);
2599b0c40a00SSimon J. Gerraty 	return true;
26003955d011SMarcel Moolenaar }
26013955d011SMarcel Moolenaar 
260206b9b3e0SSimon J. Gerraty /*
260306b9b3e0SSimon J. Gerraty  * Handle the receipt of an interrupt.
2604956e45f6SSimon J. Gerraty  *
2605956e45f6SSimon J. Gerraty  * All children are killed. Another job will be started if the .INTERRUPT
2606956e45f6SSimon J. Gerraty  * target is defined.
26073955d011SMarcel Moolenaar  *
26083955d011SMarcel Moolenaar  * Input:
26093955d011SMarcel Moolenaar  *	runINTERRUPT	Non-zero if commands for the .INTERRUPT target
26103955d011SMarcel Moolenaar  *			should be executed
26113955d011SMarcel Moolenaar  *	signo		signal received
26123955d011SMarcel Moolenaar  */
26133955d011SMarcel Moolenaar static void
2614b0c40a00SSimon J. Gerraty JobInterrupt(bool runINTERRUPT, int signo)
26153955d011SMarcel Moolenaar {
26163955d011SMarcel Moolenaar 	Job *job;		/* job descriptor in that element */
26173955d011SMarcel Moolenaar 	GNode *interrupt;	/* the node describing the .INTERRUPT target */
26183955d011SMarcel Moolenaar 	sigset_t mask;
26193955d011SMarcel Moolenaar 	GNode *gn;
26203955d011SMarcel Moolenaar 
26213955d011SMarcel Moolenaar 	aborting = ABORT_INTERRUPT;
26223955d011SMarcel Moolenaar 
26233955d011SMarcel Moolenaar 	JobSigLock(&mask);
26243955d011SMarcel Moolenaar 
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 
26293955d011SMarcel Moolenaar 		gn = job->node;
26303955d011SMarcel Moolenaar 
263145447996SSimon J. Gerraty 		JobDeleteTarget(gn);
263206b9b3e0SSimon J. Gerraty 		if (job->pid != 0) {
263306b9b3e0SSimon J. Gerraty 			DEBUG2(JOB,
263406b9b3e0SSimon J. Gerraty 			    "JobInterrupt passing signal %d to child %d.\n",
26353955d011SMarcel Moolenaar 			    signo, job->pid);
26363955d011SMarcel Moolenaar 			KILLPG(job->pid, signo);
26373955d011SMarcel Moolenaar 		}
26383955d011SMarcel Moolenaar 	}
26393955d011SMarcel Moolenaar 
26403955d011SMarcel Moolenaar 	JobSigUnlock(&mask);
26413955d011SMarcel Moolenaar 
26429f45a3c8SSimon J. Gerraty 	if (runINTERRUPT && !opts.touch) {
2643956e45f6SSimon J. Gerraty 		interrupt = Targ_FindNode(".INTERRUPT");
26443955d011SMarcel Moolenaar 		if (interrupt != NULL) {
2645b0c40a00SSimon J. Gerraty 			opts.ignoreErrors = false;
26463955d011SMarcel Moolenaar 			JobRun(interrupt);
26473955d011SMarcel Moolenaar 		}
26483955d011SMarcel Moolenaar 	}
2649e2eeea75SSimon J. Gerraty 	Trace_Log(MAKEINTR, NULL);
265006b9b3e0SSimon J. Gerraty 	exit(signo);		/* XXX: why signo? */
26513955d011SMarcel Moolenaar }
26523955d011SMarcel Moolenaar 
265306b9b3e0SSimon J. Gerraty /*
265406b9b3e0SSimon J. Gerraty  * Do the final processing, i.e. run the commands attached to the .END target.
26553955d011SMarcel Moolenaar  *
265606b9b3e0SSimon J. Gerraty  * Return the number of errors reported.
265706b9b3e0SSimon J. Gerraty  */
26583955d011SMarcel Moolenaar int
26593955d011SMarcel Moolenaar Job_Finish(void)
26603955d011SMarcel Moolenaar {
2661956e45f6SSimon J. Gerraty 	GNode *endNode = Targ_GetEndNode();
266206b9b3e0SSimon J. Gerraty 	if (!Lst_IsEmpty(&endNode->commands) ||
266306b9b3e0SSimon J. Gerraty 	    !Lst_IsEmpty(&endNode->children)) {
266406b9b3e0SSimon J. Gerraty 		if (job_errors != 0) {
26653955d011SMarcel Moolenaar 			Error("Errors reported so .END ignored");
26663955d011SMarcel Moolenaar 		} else {
2667956e45f6SSimon J. Gerraty 			JobRun(endNode);
26683955d011SMarcel Moolenaar 		}
26693955d011SMarcel Moolenaar 	}
267006b9b3e0SSimon J. Gerraty 	return job_errors;
26713955d011SMarcel Moolenaar }
26723955d011SMarcel Moolenaar 
2673956e45f6SSimon J. Gerraty /* Clean up any memory used by the jobs module. */
26743955d011SMarcel Moolenaar void
26753955d011SMarcel Moolenaar Job_End(void)
26763955d011SMarcel Moolenaar {
26773955d011SMarcel Moolenaar #ifdef CLEANUP
2678dba7b0efSSimon J. Gerraty 	free(shell_freeIt);
26793955d011SMarcel Moolenaar #endif
26803955d011SMarcel Moolenaar }
26813955d011SMarcel Moolenaar 
268206b9b3e0SSimon J. Gerraty /*
268306b9b3e0SSimon J. Gerraty  * Waits for all running jobs to finish and returns.
268406b9b3e0SSimon J. Gerraty  * Sets 'aborting' to ABORT_WAIT to prevent other jobs from starting.
268506b9b3e0SSimon J. Gerraty  */
26863955d011SMarcel Moolenaar void
26873955d011SMarcel Moolenaar Job_Wait(void)
26883955d011SMarcel Moolenaar {
26893955d011SMarcel Moolenaar 	aborting = ABORT_WAIT;
26903955d011SMarcel Moolenaar 	while (jobTokensRunning != 0) {
26913955d011SMarcel Moolenaar 		Job_CatchOutput();
26923955d011SMarcel Moolenaar 	}
2693956e45f6SSimon J. Gerraty 	aborting = ABORT_NONE;
26943955d011SMarcel Moolenaar }
26953955d011SMarcel Moolenaar 
269606b9b3e0SSimon J. Gerraty /*
269706b9b3e0SSimon J. Gerraty  * Abort all currently running jobs without handling output or anything.
2698956e45f6SSimon J. Gerraty  * This function is to be called only in the event of a major error.
2699956e45f6SSimon J. Gerraty  * Most definitely NOT to be called from JobInterrupt.
27003955d011SMarcel Moolenaar  *
270106b9b3e0SSimon J. Gerraty  * All children are killed, not just the firstborn.
270206b9b3e0SSimon J. Gerraty  */
27033955d011SMarcel Moolenaar void
27043955d011SMarcel Moolenaar Job_AbortAll(void)
27053955d011SMarcel Moolenaar {
27063955d011SMarcel Moolenaar 	Job *job;		/* the job descriptor in that element */
27073955d011SMarcel Moolenaar 	WAIT_T foo;
27083955d011SMarcel Moolenaar 
27093955d011SMarcel Moolenaar 	aborting = ABORT_ERROR;
27103955d011SMarcel Moolenaar 
271106b9b3e0SSimon J. Gerraty 	if (jobTokensRunning != 0) {
27123955d011SMarcel Moolenaar 		for (job = job_table; job < job_table_end; job++) {
2713e2eeea75SSimon J. Gerraty 			if (job->status != JOB_ST_RUNNING)
27143955d011SMarcel Moolenaar 				continue;
27153955d011SMarcel Moolenaar 			/*
271606b9b3e0SSimon J. Gerraty 			 * kill the child process with increasingly drastic
271706b9b3e0SSimon J. Gerraty 			 * signals to make darn sure it's dead.
27183955d011SMarcel Moolenaar 			 */
27193955d011SMarcel Moolenaar 			KILLPG(job->pid, SIGINT);
27203955d011SMarcel Moolenaar 			KILLPG(job->pid, SIGKILL);
27213955d011SMarcel Moolenaar 		}
27223955d011SMarcel Moolenaar 	}
27233955d011SMarcel Moolenaar 
27243955d011SMarcel Moolenaar 	/*
27253955d011SMarcel Moolenaar 	 * Catch as many children as want to report in at first, then give up
27263955d011SMarcel Moolenaar 	 */
27273955d011SMarcel Moolenaar 	while (waitpid((pid_t)-1, &foo, WNOHANG) > 0)
27283955d011SMarcel Moolenaar 		continue;
27293955d011SMarcel Moolenaar }
27303955d011SMarcel Moolenaar 
273106b9b3e0SSimon J. Gerraty /*
273206b9b3e0SSimon J. Gerraty  * Tries to restart stopped jobs if there are slots available.
273306b9b3e0SSimon J. Gerraty  * Called in process context in response to a SIGCONT.
273406b9b3e0SSimon J. Gerraty  */
27353955d011SMarcel Moolenaar static void
27363955d011SMarcel Moolenaar JobRestartJobs(void)
27373955d011SMarcel Moolenaar {
27383955d011SMarcel Moolenaar 	Job *job;
27393955d011SMarcel Moolenaar 
27403955d011SMarcel Moolenaar 	for (job = job_table; job < job_table_end; job++) {
2741e2eeea75SSimon J. Gerraty 		if (job->status == JOB_ST_RUNNING &&
2742e2eeea75SSimon J. Gerraty 		    (make_suspended || job->suspended)) {
274306b9b3e0SSimon J. Gerraty 			DEBUG1(JOB, "Restarting stopped job pid %d.\n",
274406b9b3e0SSimon J. Gerraty 			    job->pid);
2745e2eeea75SSimon J. Gerraty 			if (job->suspended) {
274606b9b3e0SSimon J. Gerraty 				(void)printf("*** [%s] Continued\n",
274706b9b3e0SSimon J. Gerraty 				    job->node->name);
27483955d011SMarcel Moolenaar 				(void)fflush(stdout);
27493955d011SMarcel Moolenaar 			}
2750b0c40a00SSimon J. Gerraty 			job->suspended = false;
27513955d011SMarcel Moolenaar 			if (KILLPG(job->pid, SIGCONT) != 0 && DEBUG(JOB)) {
275206b9b3e0SSimon J. Gerraty 				debug_printf("Failed to send SIGCONT to %d\n",
275306b9b3e0SSimon J. Gerraty 				    job->pid);
27543955d011SMarcel Moolenaar 			}
27553955d011SMarcel Moolenaar 		}
275606b9b3e0SSimon J. Gerraty 		if (job->status == JOB_ST_FINISHED) {
275706b9b3e0SSimon J. Gerraty 			/*
275806b9b3e0SSimon J. Gerraty 			 * Job exit deferred after calling waitpid() in a
275906b9b3e0SSimon J. Gerraty 			 * signal handler
276006b9b3e0SSimon J. Gerraty 			 */
27613955d011SMarcel Moolenaar 			JobFinish(job, job->exit_status);
27623955d011SMarcel Moolenaar 		}
276306b9b3e0SSimon J. Gerraty 	}
2764b0c40a00SSimon J. Gerraty 	make_suspended = false;
27653955d011SMarcel Moolenaar }
27663955d011SMarcel Moolenaar 
27673955d011SMarcel Moolenaar static void
27683955d011SMarcel Moolenaar watchfd(Job *job)
27693955d011SMarcel Moolenaar {
27703955d011SMarcel Moolenaar 	if (job->inPollfd != NULL)
27713955d011SMarcel Moolenaar 		Punt("Watching watched job");
27723955d011SMarcel Moolenaar 
2773dba7b0efSSimon J. Gerraty 	fds[fdsLen].fd = job->inPipe;
2774dba7b0efSSimon J. Gerraty 	fds[fdsLen].events = POLLIN;
2775dba7b0efSSimon J. Gerraty 	jobByFdIndex[fdsLen] = job;
2776dba7b0efSSimon J. Gerraty 	job->inPollfd = &fds[fdsLen];
2777dba7b0efSSimon J. Gerraty 	fdsLen++;
277849caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
277949caa483SSimon J. Gerraty 	if (useMeta) {
2780dba7b0efSSimon J. Gerraty 		fds[fdsLen].fd = meta_job_fd(job);
2781dba7b0efSSimon J. Gerraty 		fds[fdsLen].events = fds[fdsLen].fd == -1 ? 0 : POLLIN;
2782dba7b0efSSimon J. Gerraty 		jobByFdIndex[fdsLen] = job;
2783dba7b0efSSimon J. Gerraty 		fdsLen++;
278449caa483SSimon J. Gerraty 	}
278549caa483SSimon J. Gerraty #endif
27863955d011SMarcel Moolenaar }
27873955d011SMarcel Moolenaar 
27883955d011SMarcel Moolenaar static void
27893955d011SMarcel Moolenaar clearfd(Job *job)
27903955d011SMarcel Moolenaar {
2791956e45f6SSimon J. Gerraty 	size_t i;
27923955d011SMarcel Moolenaar 	if (job->inPollfd == NULL)
27933955d011SMarcel Moolenaar 		Punt("Unwatching unwatched job");
2794956e45f6SSimon J. Gerraty 	i = (size_t)(job->inPollfd - fds);
2795dba7b0efSSimon J. Gerraty 	fdsLen--;
279649caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
279749caa483SSimon J. Gerraty 	if (useMeta) {
279849caa483SSimon J. Gerraty 		/*
279949caa483SSimon J. Gerraty 		 * Sanity check: there should be two fds per job, so the job's
280049caa483SSimon J. Gerraty 		 * pollfd number should be even.
280149caa483SSimon J. Gerraty 		 */
280249caa483SSimon J. Gerraty 		assert(nfds_per_job() == 2);
280306b9b3e0SSimon J. Gerraty 		if (i % 2 != 0)
280449caa483SSimon J. Gerraty 			Punt("odd-numbered fd with meta");
2805dba7b0efSSimon J. Gerraty 		fdsLen--;
280649caa483SSimon J. Gerraty 	}
280749caa483SSimon J. Gerraty #endif
28083955d011SMarcel Moolenaar 	/*
28093955d011SMarcel Moolenaar 	 * Move last job in table into hole made by dead job.
28103955d011SMarcel Moolenaar 	 */
2811dba7b0efSSimon J. Gerraty 	if (fdsLen != i) {
2812dba7b0efSSimon J. Gerraty 		fds[i] = fds[fdsLen];
2813dba7b0efSSimon J. Gerraty 		jobByFdIndex[i] = jobByFdIndex[fdsLen];
2814dba7b0efSSimon J. Gerraty 		jobByFdIndex[i]->inPollfd = &fds[i];
281549caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
281649caa483SSimon J. Gerraty 		if (useMeta) {
2817dba7b0efSSimon J. Gerraty 			fds[i + 1] = fds[fdsLen + 1];
2818dba7b0efSSimon J. Gerraty 			jobByFdIndex[i + 1] = jobByFdIndex[fdsLen + 1];
281949caa483SSimon J. Gerraty 		}
282049caa483SSimon J. Gerraty #endif
28213955d011SMarcel Moolenaar 	}
28223955d011SMarcel Moolenaar 	job->inPollfd = NULL;
28233955d011SMarcel Moolenaar }
28243955d011SMarcel Moolenaar 
2825b0c40a00SSimon J. Gerraty static bool
28263955d011SMarcel Moolenaar readyfd(Job *job)
28273955d011SMarcel Moolenaar {
28283955d011SMarcel Moolenaar 	if (job->inPollfd == NULL)
28293955d011SMarcel Moolenaar 		Punt("Polling unwatched job");
28303955d011SMarcel Moolenaar 	return (job->inPollfd->revents & POLLIN) != 0;
28313955d011SMarcel Moolenaar }
28323955d011SMarcel Moolenaar 
283306b9b3e0SSimon J. Gerraty /*
283406b9b3e0SSimon J. Gerraty  * Put a token (back) into the job pipe.
283506b9b3e0SSimon J. Gerraty  * This allows a make process to start a build job.
283606b9b3e0SSimon J. Gerraty  */
28373955d011SMarcel Moolenaar static void
28383955d011SMarcel Moolenaar JobTokenAdd(void)
28393955d011SMarcel Moolenaar {
28403955d011SMarcel Moolenaar 	char tok = JOB_TOKENS[aborting], tok1;
28413955d011SMarcel Moolenaar 
28422d395cb5SSimon J. Gerraty 	if (!Job_error_token && aborting == ABORT_ERROR) {
28432d395cb5SSimon J. Gerraty 		if (jobTokensRunning == 0)
28442d395cb5SSimon J. Gerraty 			return;
28452d395cb5SSimon J. Gerraty 		tok = '+';		/* no error token */
28462d395cb5SSimon J. Gerraty 	}
28472d395cb5SSimon J. Gerraty 
28483955d011SMarcel Moolenaar 	/* If we are depositing an error token flush everything else */
28493955d011SMarcel Moolenaar 	while (tok != '+' && read(tokenWaitJob.inPipe, &tok1, 1) == 1)
28503955d011SMarcel Moolenaar 		continue;
28513955d011SMarcel Moolenaar 
2852956e45f6SSimon J. Gerraty 	DEBUG3(JOB, "(%d) aborting %d, deposit token %c\n",
28532d395cb5SSimon J. Gerraty 	    getpid(), aborting, tok);
28543cbdda60SSimon J. Gerraty 	while (write(tokenWaitJob.outPipe, &tok, 1) == -1 && errno == EAGAIN)
28553cbdda60SSimon J. Gerraty 		continue;
28563955d011SMarcel Moolenaar }
28573955d011SMarcel Moolenaar 
2858dba7b0efSSimon J. Gerraty /* Get a temp file */
2859dba7b0efSSimon J. Gerraty int
2860dba7b0efSSimon J. Gerraty Job_TempFile(const char *pattern, char *tfile, size_t tfile_sz)
2861dba7b0efSSimon J. Gerraty {
2862dba7b0efSSimon J. Gerraty 	int fd;
2863dba7b0efSSimon J. Gerraty 	sigset_t mask;
2864dba7b0efSSimon J. Gerraty 
2865dba7b0efSSimon J. Gerraty 	JobSigLock(&mask);
2866dba7b0efSSimon J. Gerraty 	fd = mkTempFile(pattern, tfile, tfile_sz);
2867dba7b0efSSimon J. Gerraty 	if (tfile != NULL && !DEBUG(SCRIPT))
2868dba7b0efSSimon J. Gerraty 		unlink(tfile);
2869dba7b0efSSimon J. Gerraty 	JobSigUnlock(&mask);
2870dba7b0efSSimon J. Gerraty 
2871dba7b0efSSimon J. Gerraty 	return fd;
2872dba7b0efSSimon J. Gerraty }
2873dba7b0efSSimon J. Gerraty 
2874956e45f6SSimon J. Gerraty /* Prep the job token pipe in the root make process. */
28753955d011SMarcel Moolenaar void
28763955d011SMarcel Moolenaar Job_ServerStart(int max_tokens, int jp_0, int jp_1)
28773955d011SMarcel Moolenaar {
28783955d011SMarcel Moolenaar 	int i;
28793955d011SMarcel Moolenaar 	char jobarg[64];
28803955d011SMarcel Moolenaar 
28813955d011SMarcel Moolenaar 	if (jp_0 >= 0 && jp_1 >= 0) {
28823955d011SMarcel Moolenaar 		/* Pipe passed in from parent */
28833955d011SMarcel Moolenaar 		tokenWaitJob.inPipe = jp_0;
28843955d011SMarcel Moolenaar 		tokenWaitJob.outPipe = jp_1;
2885be19d90bSSimon J. Gerraty 		(void)fcntl(jp_0, F_SETFD, FD_CLOEXEC);
2886be19d90bSSimon J. Gerraty 		(void)fcntl(jp_1, F_SETFD, FD_CLOEXEC);
28873955d011SMarcel Moolenaar 		return;
28883955d011SMarcel Moolenaar 	}
28893955d011SMarcel Moolenaar 
28903955d011SMarcel Moolenaar 	JobCreatePipe(&tokenWaitJob, 15);
28913955d011SMarcel Moolenaar 
2892e2eeea75SSimon J. Gerraty 	snprintf(jobarg, sizeof jobarg, "%d,%d",
28933955d011SMarcel Moolenaar 	    tokenWaitJob.inPipe, tokenWaitJob.outPipe);
28943955d011SMarcel Moolenaar 
2895dba7b0efSSimon J. Gerraty 	Global_Append(MAKEFLAGS, "-J");
2896dba7b0efSSimon J. Gerraty 	Global_Append(MAKEFLAGS, jobarg);
28973955d011SMarcel Moolenaar 
28983955d011SMarcel Moolenaar 	/*
28993955d011SMarcel Moolenaar 	 * Preload the job pipe with one token per job, save the one
29003955d011SMarcel Moolenaar 	 * "extra" token for the primary job.
29013955d011SMarcel Moolenaar 	 *
29023955d011SMarcel Moolenaar 	 * XXX should clip maxJobs against PIPE_BUF -- if max_tokens is
29033955d011SMarcel Moolenaar 	 * larger than the write buffer size of the pipe, we will
29043955d011SMarcel Moolenaar 	 * deadlock here.
29053955d011SMarcel Moolenaar 	 */
29063955d011SMarcel Moolenaar 	for (i = 1; i < max_tokens; i++)
29073955d011SMarcel Moolenaar 		JobTokenAdd();
29083955d011SMarcel Moolenaar }
29093955d011SMarcel Moolenaar 
2910956e45f6SSimon J. Gerraty /* Return a withdrawn token to the pool. */
29113955d011SMarcel Moolenaar void
29123955d011SMarcel Moolenaar Job_TokenReturn(void)
29133955d011SMarcel Moolenaar {
29143955d011SMarcel Moolenaar 	jobTokensRunning--;
29153955d011SMarcel Moolenaar 	if (jobTokensRunning < 0)
29163955d011SMarcel Moolenaar 		Punt("token botch");
291706b9b3e0SSimon J. Gerraty 	if (jobTokensRunning != 0 || JOB_TOKENS[aborting] != '+')
29183955d011SMarcel Moolenaar 		JobTokenAdd();
29193955d011SMarcel Moolenaar }
29203955d011SMarcel Moolenaar 
292106b9b3e0SSimon J. Gerraty /*
292206b9b3e0SSimon J. Gerraty  * Attempt to withdraw a token from the pool.
29233955d011SMarcel Moolenaar  *
2924956e45f6SSimon J. Gerraty  * If pool is empty, set wantToken so that we wake up when a token is
2925956e45f6SSimon J. Gerraty  * released.
29263955d011SMarcel Moolenaar  *
2927b0c40a00SSimon J. Gerraty  * Returns true if a token was withdrawn, and false if the pool is currently
292806b9b3e0SSimon J. Gerraty  * empty.
292906b9b3e0SSimon J. Gerraty  */
2930b0c40a00SSimon J. Gerraty bool
29313955d011SMarcel Moolenaar Job_TokenWithdraw(void)
29323955d011SMarcel Moolenaar {
29333955d011SMarcel Moolenaar 	char tok, tok1;
2934956e45f6SSimon J. Gerraty 	ssize_t count;
29353955d011SMarcel Moolenaar 
29363955d011SMarcel Moolenaar 	wantToken = 0;
2937956e45f6SSimon J. Gerraty 	DEBUG3(JOB, "Job_TokenWithdraw(%d): aborting %d, running %d\n",
29383955d011SMarcel Moolenaar 	    getpid(), aborting, jobTokensRunning);
29393955d011SMarcel Moolenaar 
2940956e45f6SSimon J. Gerraty 	if (aborting != ABORT_NONE || (jobTokensRunning >= opts.maxJobs))
2941b0c40a00SSimon J. Gerraty 		return false;
29423955d011SMarcel Moolenaar 
29433955d011SMarcel Moolenaar 	count = read(tokenWaitJob.inPipe, &tok, 1);
29443955d011SMarcel Moolenaar 	if (count == 0)
29453955d011SMarcel Moolenaar 		Fatal("eof on job pipe!");
29463955d011SMarcel Moolenaar 	if (count < 0 && jobTokensRunning != 0) {
29473955d011SMarcel Moolenaar 		if (errno != EAGAIN) {
29483955d011SMarcel Moolenaar 			Fatal("job pipe read: %s", strerror(errno));
29493955d011SMarcel Moolenaar 		}
2950956e45f6SSimon J. Gerraty 		DEBUG1(JOB, "(%d) blocked for token\n", getpid());
295106b9b3e0SSimon J. Gerraty 		wantToken = 1;
2952b0c40a00SSimon J. Gerraty 		return false;
29533955d011SMarcel Moolenaar 	}
29543955d011SMarcel Moolenaar 
29553955d011SMarcel Moolenaar 	if (count == 1 && tok != '+') {
295606b9b3e0SSimon J. Gerraty 		/* make being aborted - remove any other job tokens */
2957956e45f6SSimon J. Gerraty 		DEBUG2(JOB, "(%d) aborted by token %c\n", getpid(), tok);
29583955d011SMarcel Moolenaar 		while (read(tokenWaitJob.inPipe, &tok1, 1) == 1)
29593955d011SMarcel Moolenaar 			continue;
29603955d011SMarcel Moolenaar 		/* And put the stopper back */
296106b9b3e0SSimon J. Gerraty 		while (write(tokenWaitJob.outPipe, &tok, 1) == -1 &&
296206b9b3e0SSimon J. Gerraty 		       errno == EAGAIN)
29633cbdda60SSimon J. Gerraty 			continue;
2964e2eeea75SSimon J. Gerraty 		if (shouldDieQuietly(NULL, 1))
296506b9b3e0SSimon J. Gerraty 			exit(6);	/* we aborted */
296606b9b3e0SSimon J. Gerraty 		Fatal("A failure has been detected "
296706b9b3e0SSimon J. Gerraty 		      "in another branch of the parallel make");
29683955d011SMarcel Moolenaar 	}
29693955d011SMarcel Moolenaar 
29703955d011SMarcel Moolenaar 	if (count == 1 && jobTokensRunning == 0)
29713955d011SMarcel Moolenaar 		/* We didn't want the token really */
297206b9b3e0SSimon J. Gerraty 		while (write(tokenWaitJob.outPipe, &tok, 1) == -1 &&
297306b9b3e0SSimon J. Gerraty 		       errno == EAGAIN)
29743cbdda60SSimon J. Gerraty 			continue;
29753955d011SMarcel Moolenaar 
29763955d011SMarcel Moolenaar 	jobTokensRunning++;
2977956e45f6SSimon J. Gerraty 	DEBUG1(JOB, "(%d) withdrew token\n", getpid());
2978b0c40a00SSimon J. Gerraty 	return true;
29793955d011SMarcel Moolenaar }
29803955d011SMarcel Moolenaar 
298106b9b3e0SSimon J. Gerraty /*
298206b9b3e0SSimon J. Gerraty  * Run the named target if found. If a filename is specified, then set that
2983956e45f6SSimon J. Gerraty  * to the sources.
29841748de26SSimon J. Gerraty  *
298506b9b3e0SSimon J. Gerraty  * Exits if the target fails.
298606b9b3e0SSimon J. Gerraty  */
2987b0c40a00SSimon J. Gerraty bool
298806b9b3e0SSimon J. Gerraty Job_RunTarget(const char *target, const char *fname)
298906b9b3e0SSimon J. Gerraty {
2990956e45f6SSimon J. Gerraty 	GNode *gn = Targ_FindNode(target);
29911748de26SSimon J. Gerraty 	if (gn == NULL)
2992b0c40a00SSimon J. Gerraty 		return false;
29931748de26SSimon J. Gerraty 
299406b9b3e0SSimon J. Gerraty 	if (fname != NULL)
2995dba7b0efSSimon J. Gerraty 		Var_Set(gn, ALLSRC, fname);
29961748de26SSimon J. Gerraty 
29971748de26SSimon J. Gerraty 	JobRun(gn);
299806b9b3e0SSimon J. Gerraty 	/* XXX: Replace with GNode_IsError(gn) */
29991748de26SSimon J. Gerraty 	if (gn->made == ERROR) {
30009f45a3c8SSimon J. Gerraty 		PrintOnError(gn, "\n\nStop.\n");
30011748de26SSimon J. Gerraty 		exit(1);
30021748de26SSimon J. Gerraty 	}
3003b0c40a00SSimon J. Gerraty 	return true;
30041748de26SSimon J. Gerraty }
30051748de26SSimon J. Gerraty 
30063955d011SMarcel Moolenaar #ifdef USE_SELECT
30073955d011SMarcel Moolenaar int
30083955d011SMarcel Moolenaar emul_poll(struct pollfd *fd, int nfd, int timeout)
30093955d011SMarcel Moolenaar {
30103955d011SMarcel Moolenaar 	fd_set rfds, wfds;
30113955d011SMarcel Moolenaar 	int i, maxfd, nselect, npoll;
30123955d011SMarcel Moolenaar 	struct timeval tv, *tvp;
30133955d011SMarcel Moolenaar 	long usecs;
30143955d011SMarcel Moolenaar 
30153955d011SMarcel Moolenaar 	FD_ZERO(&rfds);
30163955d011SMarcel Moolenaar 	FD_ZERO(&wfds);
30173955d011SMarcel Moolenaar 
30183955d011SMarcel Moolenaar 	maxfd = -1;
30193955d011SMarcel Moolenaar 	for (i = 0; i < nfd; i++) {
30203955d011SMarcel Moolenaar 		fd[i].revents = 0;
30213955d011SMarcel Moolenaar 
30223955d011SMarcel Moolenaar 		if (fd[i].events & POLLIN)
30233955d011SMarcel Moolenaar 			FD_SET(fd[i].fd, &rfds);
30243955d011SMarcel Moolenaar 
30253955d011SMarcel Moolenaar 		if (fd[i].events & POLLOUT)
30263955d011SMarcel Moolenaar 			FD_SET(fd[i].fd, &wfds);
30273955d011SMarcel Moolenaar 
30283955d011SMarcel Moolenaar 		if (fd[i].fd > maxfd)
30293955d011SMarcel Moolenaar 			maxfd = fd[i].fd;
30303955d011SMarcel Moolenaar 	}
30313955d011SMarcel Moolenaar 
30323955d011SMarcel Moolenaar 	if (maxfd >= FD_SETSIZE) {
30333955d011SMarcel Moolenaar 		Punt("Ran out of fd_set slots; "
30343955d011SMarcel Moolenaar 		     "recompile with a larger FD_SETSIZE.");
30353955d011SMarcel Moolenaar 	}
30363955d011SMarcel Moolenaar 
30373955d011SMarcel Moolenaar 	if (timeout < 0) {
30383955d011SMarcel Moolenaar 		tvp = NULL;
30393955d011SMarcel Moolenaar 	} else {
30403955d011SMarcel Moolenaar 		usecs = timeout * 1000;
30413955d011SMarcel Moolenaar 		tv.tv_sec = usecs / 1000000;
30423955d011SMarcel Moolenaar 		tv.tv_usec = usecs % 1000000;
30433955d011SMarcel Moolenaar 		tvp = &tv;
30443955d011SMarcel Moolenaar 	}
30453955d011SMarcel Moolenaar 
3046e2eeea75SSimon J. Gerraty 	nselect = select(maxfd + 1, &rfds, &wfds, NULL, tvp);
30473955d011SMarcel Moolenaar 
30483955d011SMarcel Moolenaar 	if (nselect <= 0)
30493955d011SMarcel Moolenaar 		return nselect;
30503955d011SMarcel Moolenaar 
30513955d011SMarcel Moolenaar 	npoll = 0;
30523955d011SMarcel Moolenaar 	for (i = 0; i < nfd; i++) {
30533955d011SMarcel Moolenaar 		if (FD_ISSET(fd[i].fd, &rfds))
30543955d011SMarcel Moolenaar 			fd[i].revents |= POLLIN;
30553955d011SMarcel Moolenaar 
30563955d011SMarcel Moolenaar 		if (FD_ISSET(fd[i].fd, &wfds))
30573955d011SMarcel Moolenaar 			fd[i].revents |= POLLOUT;
30583955d011SMarcel Moolenaar 
30593955d011SMarcel Moolenaar 		if (fd[i].revents)
30603955d011SMarcel Moolenaar 			npoll++;
30613955d011SMarcel Moolenaar 	}
30623955d011SMarcel Moolenaar 
30633955d011SMarcel Moolenaar 	return npoll;
30643955d011SMarcel Moolenaar }
30653955d011SMarcel Moolenaar #endif				/* USE_SELECT */
3066