xref: /freebsd/contrib/bmake/job.c (revision 129043849f62f9cfa72f6fae68417d9995860f3f)
1*12904384SSimon J. Gerraty /*	$NetBSD: job.c,v 1.440 2021/11/28 19:51:06 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*12904384SSimon J. Gerraty MAKE_RCSID("$NetBSD: job.c,v 1.440 2021/11/28 19:51:06 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 */
21706b9b3e0SSimon J. Gerraty 	const char *runIgnTmpl;	/* template to run a command
21806b9b3e0SSimon J. Gerraty 				 * without error checking */
21906b9b3e0SSimon J. Gerraty 	const char *runChkTmpl;	/* template to run a command
22006b9b3e0SSimon J. Gerraty 				 * with error checking */
221956e45f6SSimon J. Gerraty 
222956e45f6SSimon J. Gerraty 	/* string literal that results in a newline character when it appears
223956e45f6SSimon J. Gerraty 	 * outside of any 'quote' or "quote" characters */
224956e45f6SSimon J. Gerraty 	const char *newline;
225956e45f6SSimon J. Gerraty 	char commentChar;	/* character used by shell for comment lines */
226956e45f6SSimon J. Gerraty 
22706b9b3e0SSimon J. Gerraty 	const char *echoFlag;	/* shell flag to echo commands */
22806b9b3e0SSimon J. Gerraty 	const char *errFlag;	/* shell flag to exit on error */
229956e45f6SSimon J. Gerraty } Shell;
2303955d011SMarcel Moolenaar 
23106b9b3e0SSimon J. Gerraty typedef struct CommandFlags {
232dba7b0efSSimon J. Gerraty 	/* Whether to echo the command before or instead of running it. */
233b0c40a00SSimon J. Gerraty 	bool echo;
23406b9b3e0SSimon J. Gerraty 
23506b9b3e0SSimon J. Gerraty 	/* Run the command even in -n or -N mode. */
236b0c40a00SSimon J. Gerraty 	bool always;
23706b9b3e0SSimon J. Gerraty 
23806b9b3e0SSimon J. Gerraty 	/*
239b0c40a00SSimon J. Gerraty 	 * true if we turned error checking off before writing the command to
240b0c40a00SSimon J. Gerraty 	 * the commands file and need to turn it back on
24106b9b3e0SSimon J. Gerraty 	 */
242b0c40a00SSimon J. Gerraty 	bool ignerr;
24306b9b3e0SSimon J. Gerraty } CommandFlags;
24406b9b3e0SSimon J. Gerraty 
24506b9b3e0SSimon J. Gerraty /*
24606b9b3e0SSimon J. Gerraty  * Write shell commands to a file.
24706b9b3e0SSimon J. Gerraty  *
24806b9b3e0SSimon J. Gerraty  * TODO: keep track of whether commands are echoed.
24906b9b3e0SSimon J. Gerraty  * TODO: keep track of whether error checking is active.
25006b9b3e0SSimon J. Gerraty  */
25106b9b3e0SSimon J. Gerraty typedef struct ShellWriter {
25206b9b3e0SSimon J. Gerraty 	FILE *f;
25306b9b3e0SSimon J. Gerraty 
25406b9b3e0SSimon J. Gerraty 	/* we've sent 'set -x' */
255b0c40a00SSimon J. Gerraty 	bool xtraced;
25606b9b3e0SSimon J. Gerraty 
25706b9b3e0SSimon J. Gerraty } ShellWriter;
25806b9b3e0SSimon J. Gerraty 
2593955d011SMarcel Moolenaar /*
2609a4bc556SSimon J. Gerraty  * FreeBSD: traditionally .MAKE is not required to
2619a4bc556SSimon J. Gerraty  * pass jobs queue to sub-makes.
2629a4bc556SSimon J. Gerraty  * Use .MAKE.ALWAYS_PASS_JOB_QUEUE=no to disable.
2639a4bc556SSimon J. Gerraty  */
2643b96abbaSSimon J. Gerraty #define MAKE_ALWAYS_PASS_JOB_QUEUE "${.MAKE.ALWAYS_PASS_JOB_QUEUE:U}"
26568c4481aSSimon J. Gerraty static bool Always_pass_job_queue = true;
2662d395cb5SSimon J. Gerraty /*
2672d395cb5SSimon J. Gerraty  * FreeBSD: aborting entire parallel make isn't always
2682d395cb5SSimon J. Gerraty  * desired. When doing tinderbox for example, failure of
2692d395cb5SSimon J. Gerraty  * one architecture should not stop all.
2702d395cb5SSimon J. Gerraty  * We still want to bail on interrupt though.
2712d395cb5SSimon J. Gerraty  */
2723b96abbaSSimon J. Gerraty #define MAKE_JOB_ERROR_TOKEN "${MAKE_JOB_ERROR_TOKEN:U}"
27368c4481aSSimon J. Gerraty static bool Job_error_token = true;
2749a4bc556SSimon J. Gerraty 
2759a4bc556SSimon J. Gerraty /*
2763955d011SMarcel Moolenaar  * error handling variables
2773955d011SMarcel Moolenaar  */
27806b9b3e0SSimon J. Gerraty static int job_errors = 0;	/* number of errors reported */
279b0c40a00SSimon J. Gerraty static enum {			/* Why is the make aborting? */
280956e45f6SSimon J. Gerraty 	ABORT_NONE,
281b0c40a00SSimon J. Gerraty 	ABORT_ERROR,		/* Aborted because of an error */
282b0c40a00SSimon J. Gerraty 	ABORT_INTERRUPT,	/* Aborted because it was interrupted */
283956e45f6SSimon J. Gerraty 	ABORT_WAIT		/* Waiting for jobs to finish */
284b0c40a00SSimon J. Gerraty } aborting = ABORT_NONE;
2853955d011SMarcel Moolenaar #define JOB_TOKENS "+EI+"	/* Token to requeue for each abort state */
2863955d011SMarcel Moolenaar 
2873955d011SMarcel Moolenaar /*
2883955d011SMarcel Moolenaar  * this tracks the number of tokens currently "out" to build jobs.
2893955d011SMarcel Moolenaar  */
2903955d011SMarcel Moolenaar int jobTokensRunning = 0;
2913955d011SMarcel Moolenaar 
292956e45f6SSimon J. Gerraty typedef enum JobStartResult {
293956e45f6SSimon J. Gerraty 	JOB_RUNNING,		/* Job is running */
294956e45f6SSimon J. Gerraty 	JOB_ERROR,		/* Error in starting the job */
295956e45f6SSimon J. Gerraty 	JOB_FINISHED		/* The job is already finished */
296956e45f6SSimon J. Gerraty } JobStartResult;
2973955d011SMarcel Moolenaar 
2983955d011SMarcel Moolenaar /*
2993955d011SMarcel Moolenaar  * Descriptions for various shells.
3003955d011SMarcel Moolenaar  *
3013955d011SMarcel Moolenaar  * The build environment may set DEFSHELL_INDEX to one of
3023955d011SMarcel Moolenaar  * DEFSHELL_INDEX_SH, DEFSHELL_INDEX_KSH, or DEFSHELL_INDEX_CSH, to
303956e45f6SSimon J. Gerraty  * select one of the predefined shells as the default shell.
3043955d011SMarcel Moolenaar  *
3053955d011SMarcel Moolenaar  * Alternatively, the build environment may set DEFSHELL_CUSTOM to the
3063955d011SMarcel Moolenaar  * name or the full path of a sh-compatible shell, which will be used as
3073955d011SMarcel Moolenaar  * the default shell.
3083955d011SMarcel Moolenaar  *
3093955d011SMarcel Moolenaar  * ".SHELL" lines in Makefiles can choose the default shell from the
310956e45f6SSimon J. Gerraty  * set defined here, or add additional shells.
3113955d011SMarcel Moolenaar  */
3123955d011SMarcel Moolenaar 
3133955d011SMarcel Moolenaar #ifdef DEFSHELL_CUSTOM
3143955d011SMarcel Moolenaar #define DEFSHELL_INDEX_CUSTOM 0
3153955d011SMarcel Moolenaar #define DEFSHELL_INDEX_SH     1
3163955d011SMarcel Moolenaar #define DEFSHELL_INDEX_KSH    2
3173955d011SMarcel Moolenaar #define DEFSHELL_INDEX_CSH    3
3183955d011SMarcel Moolenaar #else /* !DEFSHELL_CUSTOM */
3193955d011SMarcel Moolenaar #define DEFSHELL_INDEX_SH     0
3203955d011SMarcel Moolenaar #define DEFSHELL_INDEX_KSH    1
3213955d011SMarcel Moolenaar #define DEFSHELL_INDEX_CSH    2
3223955d011SMarcel Moolenaar #endif /* !DEFSHELL_CUSTOM */
3233955d011SMarcel Moolenaar 
3243955d011SMarcel Moolenaar #ifndef DEFSHELL_INDEX
3253955d011SMarcel Moolenaar #define DEFSHELL_INDEX 0	/* DEFSHELL_INDEX_CUSTOM or DEFSHELL_INDEX_SH */
3263955d011SMarcel Moolenaar #endif /* !DEFSHELL_INDEX */
3273955d011SMarcel Moolenaar 
3283955d011SMarcel Moolenaar static Shell shells[] = {
3293955d011SMarcel Moolenaar #ifdef DEFSHELL_CUSTOM
3303955d011SMarcel Moolenaar     /*
3313955d011SMarcel Moolenaar      * An sh-compatible shell with a non-standard name.
3323955d011SMarcel Moolenaar      *
3333955d011SMarcel Moolenaar      * Keep this in sync with the "sh" description below, but avoid
3343955d011SMarcel Moolenaar      * non-portable features that might not be supplied by all
3353955d011SMarcel Moolenaar      * sh-compatible shells.
3363955d011SMarcel Moolenaar      */
3373955d011SMarcel Moolenaar     {
338956e45f6SSimon J. Gerraty 	DEFSHELL_CUSTOM,	/* .name */
339b0c40a00SSimon J. Gerraty 	false,			/* .hasEchoCtl */
340956e45f6SSimon J. Gerraty 	"",			/* .echoOff */
341956e45f6SSimon J. Gerraty 	"",			/* .echoOn */
342956e45f6SSimon J. Gerraty 	"",			/* .noPrint */
343956e45f6SSimon J. Gerraty 	0,			/* .noPrintLen */
344b0c40a00SSimon J. Gerraty 	false,			/* .hasErrCtl */
34506b9b3e0SSimon J. Gerraty 	"",			/* .errOn */
34606b9b3e0SSimon J. Gerraty 	"",			/* .errOff */
34706b9b3e0SSimon J. Gerraty 	"echo \"%s\"\n",	/* .echoTmpl */
34806b9b3e0SSimon J. Gerraty 	"%s\n",			/* .runIgnTmpl */
34906b9b3e0SSimon J. Gerraty 	"{ %s \n} || exit $?\n", /* .runChkTmpl */
350956e45f6SSimon J. Gerraty 	"'\n'",			/* .newline */
351956e45f6SSimon J. Gerraty 	'#',			/* .commentChar */
35206b9b3e0SSimon J. Gerraty 	"",			/* .echoFlag */
35306b9b3e0SSimon J. Gerraty 	"",			/* .errFlag */
3543955d011SMarcel Moolenaar     },
3553955d011SMarcel Moolenaar #endif /* DEFSHELL_CUSTOM */
3563955d011SMarcel Moolenaar     /*
3573955d011SMarcel Moolenaar      * SH description. Echo control is also possible and, under
3583955d011SMarcel Moolenaar      * sun UNIX anyway, one can even control error checking.
3593955d011SMarcel Moolenaar      */
3603955d011SMarcel Moolenaar     {
361956e45f6SSimon J. Gerraty 	"sh",			/* .name */
362b0c40a00SSimon J. Gerraty 	false,			/* .hasEchoCtl */
363956e45f6SSimon J. Gerraty 	"",			/* .echoOff */
364956e45f6SSimon J. Gerraty 	"",			/* .echoOn */
365956e45f6SSimon J. Gerraty 	"",			/* .noPrint */
366956e45f6SSimon J. Gerraty 	0,			/* .noPrintLen */
367b0c40a00SSimon J. Gerraty 	false,			/* .hasErrCtl */
36806b9b3e0SSimon J. Gerraty 	"",			/* .errOn */
36906b9b3e0SSimon J. Gerraty 	"",			/* .errOff */
37006b9b3e0SSimon J. Gerraty 	"echo \"%s\"\n",	/* .echoTmpl */
37106b9b3e0SSimon J. Gerraty 	"%s\n",			/* .runIgnTmpl */
37206b9b3e0SSimon J. Gerraty 	"{ %s \n} || exit $?\n", /* .runChkTmpl */
373956e45f6SSimon J. Gerraty 	"'\n'",			/* .newline */
374956e45f6SSimon J. Gerraty 	'#',			/* .commentChar*/
3753955d011SMarcel Moolenaar #if defined(MAKE_NATIVE) && defined(__NetBSD__)
37606b9b3e0SSimon J. Gerraty 	/* XXX: -q is not really echoFlag, it's more like noEchoInSysFlag. */
37706b9b3e0SSimon J. Gerraty 	"q",			/* .echoFlag */
3783955d011SMarcel Moolenaar #else
37906b9b3e0SSimon J. Gerraty 	"",			/* .echoFlag */
3803955d011SMarcel Moolenaar #endif
38106b9b3e0SSimon J. Gerraty 	"",			/* .errFlag */
3823955d011SMarcel Moolenaar     },
3833955d011SMarcel Moolenaar     /*
3843955d011SMarcel Moolenaar      * KSH description.
3853955d011SMarcel Moolenaar      */
3863955d011SMarcel Moolenaar     {
387956e45f6SSimon J. Gerraty 	"ksh",			/* .name */
388b0c40a00SSimon J. Gerraty 	true,			/* .hasEchoCtl */
389956e45f6SSimon J. Gerraty 	"set +v",		/* .echoOff */
390956e45f6SSimon J. Gerraty 	"set -v",		/* .echoOn */
391956e45f6SSimon J. Gerraty 	"set +v",		/* .noPrint */
392956e45f6SSimon J. Gerraty 	6,			/* .noPrintLen */
393b0c40a00SSimon J. Gerraty 	false,			/* .hasErrCtl */
39406b9b3e0SSimon J. Gerraty 	"",			/* .errOn */
39506b9b3e0SSimon J. Gerraty 	"",			/* .errOff */
39606b9b3e0SSimon J. Gerraty 	"echo \"%s\"\n",	/* .echoTmpl */
39706b9b3e0SSimon J. Gerraty 	"%s\n",			/* .runIgnTmpl */
39806b9b3e0SSimon J. Gerraty 	"{ %s \n} || exit $?\n", /* .runChkTmpl */
399956e45f6SSimon J. Gerraty 	"'\n'",			/* .newline */
400956e45f6SSimon J. Gerraty 	'#',			/* .commentChar */
40106b9b3e0SSimon J. Gerraty 	"v",			/* .echoFlag */
40206b9b3e0SSimon J. Gerraty 	"",			/* .errFlag */
4033955d011SMarcel Moolenaar     },
4043955d011SMarcel Moolenaar     /*
4053955d011SMarcel Moolenaar      * CSH description. The csh can do echo control by playing
4063955d011SMarcel Moolenaar      * with the setting of the 'echo' shell variable. Sadly,
4073955d011SMarcel Moolenaar      * however, it is unable to do error control nicely.
4083955d011SMarcel Moolenaar      */
4093955d011SMarcel Moolenaar     {
410956e45f6SSimon J. Gerraty 	"csh",			/* .name */
411b0c40a00SSimon J. Gerraty 	true,			/* .hasEchoCtl */
412956e45f6SSimon J. Gerraty 	"unset verbose",	/* .echoOff */
413956e45f6SSimon J. Gerraty 	"set verbose",		/* .echoOn */
414956e45f6SSimon J. Gerraty 	"unset verbose",	/* .noPrint */
415956e45f6SSimon J. Gerraty 	13,			/* .noPrintLen */
416b0c40a00SSimon J. Gerraty 	false,			/* .hasErrCtl */
41706b9b3e0SSimon J. Gerraty 	"",			/* .errOn */
41806b9b3e0SSimon J. Gerraty 	"",			/* .errOff */
41906b9b3e0SSimon J. Gerraty 	"echo \"%s\"\n",	/* .echoTmpl */
42006b9b3e0SSimon J. Gerraty 	"csh -c \"%s || exit 0\"\n", /* .runIgnTmpl */
42106b9b3e0SSimon J. Gerraty 	"",			/* .runChkTmpl */
422956e45f6SSimon J. Gerraty 	"'\\\n'",		/* .newline */
423956e45f6SSimon J. Gerraty 	'#',			/* .commentChar */
42406b9b3e0SSimon J. Gerraty 	"v",			/* .echoFlag */
42506b9b3e0SSimon J. Gerraty 	"e",			/* .errFlag */
4263955d011SMarcel Moolenaar     }
4273955d011SMarcel Moolenaar };
428956e45f6SSimon J. Gerraty 
42906b9b3e0SSimon J. Gerraty /*
43006b9b3e0SSimon J. Gerraty  * This is the shell to which we pass all commands in the Makefile.
43106b9b3e0SSimon J. Gerraty  * It is set by the Job_ParseShell function.
43206b9b3e0SSimon J. Gerraty  */
43306b9b3e0SSimon J. Gerraty static Shell *shell = &shells[DEFSHELL_INDEX];
434956e45f6SSimon J. Gerraty const char *shellPath = NULL;	/* full pathname of executable image */
435956e45f6SSimon J. Gerraty const char *shellName = NULL;	/* last component of shellPath */
43651ee2c1cSSimon J. Gerraty char *shellErrFlag = NULL;
437dba7b0efSSimon J. Gerraty static char *shell_freeIt = NULL; /* Allocated memory for custom .SHELL */
4383955d011SMarcel Moolenaar 
4393955d011SMarcel Moolenaar 
440956e45f6SSimon J. Gerraty static Job *job_table;		/* The structures that describe them */
441956e45f6SSimon J. Gerraty static Job *job_table_end;	/* job_table + maxJobs */
442956e45f6SSimon J. Gerraty static unsigned int wantToken;	/* we want a token */
443b0c40a00SSimon J. Gerraty static bool lurking_children = false;
444b0c40a00SSimon J. Gerraty static bool make_suspended = false; /* Whether we've seen a SIGTSTP (etc) */
4453955d011SMarcel Moolenaar 
4463955d011SMarcel Moolenaar /*
4473955d011SMarcel Moolenaar  * Set of descriptors of pipes connected to
4483955d011SMarcel Moolenaar  * the output channels of children
4493955d011SMarcel Moolenaar  */
4503955d011SMarcel Moolenaar static struct pollfd *fds = NULL;
451dba7b0efSSimon J. Gerraty static Job **jobByFdIndex = NULL;
452dba7b0efSSimon J. Gerraty static nfds_t fdsLen = 0;
4533955d011SMarcel Moolenaar static void watchfd(Job *);
4543955d011SMarcel Moolenaar static void clearfd(Job *);
455b0c40a00SSimon J. Gerraty static bool readyfd(Job *);
4563955d011SMarcel Moolenaar 
45706b9b3e0SSimon J. Gerraty static char *targPrefix = NULL; /* To identify a job change in the output. */
4583955d011SMarcel Moolenaar static Job tokenWaitJob;	/* token wait pseudo-job */
4593955d011SMarcel Moolenaar 
4603955d011SMarcel Moolenaar static Job childExitJob;	/* child exit pseudo-job */
4613955d011SMarcel Moolenaar #define CHILD_EXIT "."
4623955d011SMarcel Moolenaar #define DO_JOB_RESUME "R"
4633955d011SMarcel Moolenaar 
46406b9b3e0SSimon J. Gerraty enum {
46506b9b3e0SSimon J. Gerraty 	npseudojobs = 2		/* number of pseudo-jobs */
46606b9b3e0SSimon J. Gerraty };
4673955d011SMarcel Moolenaar 
4683955d011SMarcel Moolenaar static sigset_t caught_signals;	/* Set of signals we handle */
469dba7b0efSSimon J. Gerraty static volatile sig_atomic_t caught_sigchld;
4703955d011SMarcel Moolenaar 
471b0c40a00SSimon J. Gerraty static void CollectOutput(Job *, bool);
472b0c40a00SSimon J. Gerraty static void JobInterrupt(bool, int) MAKE_ATTR_DEAD;
4733955d011SMarcel Moolenaar static void JobRestartJobs(void);
4743955d011SMarcel Moolenaar static void JobSigReset(void);
4753955d011SMarcel Moolenaar 
47606b9b3e0SSimon J. Gerraty static void
47706b9b3e0SSimon J. Gerraty SwitchOutputTo(GNode *gn)
47806b9b3e0SSimon J. Gerraty {
47906b9b3e0SSimon J. Gerraty 	/* The node for which output was most recently produced. */
48006b9b3e0SSimon J. Gerraty 	static GNode *lastNode = NULL;
48106b9b3e0SSimon J. Gerraty 
48206b9b3e0SSimon J. Gerraty 	if (gn == lastNode)
48306b9b3e0SSimon J. Gerraty 		return;
48406b9b3e0SSimon J. Gerraty 	lastNode = gn;
48506b9b3e0SSimon J. Gerraty 
48606b9b3e0SSimon J. Gerraty 	if (opts.maxJobs != 1 && targPrefix != NULL && targPrefix[0] != '\0')
48706b9b3e0SSimon J. Gerraty 		(void)fprintf(stdout, "%s %s ---\n", targPrefix, gn->name);
48806b9b3e0SSimon J. Gerraty }
48906b9b3e0SSimon J. Gerraty 
49049caa483SSimon J. Gerraty static unsigned
49149caa483SSimon J. Gerraty nfds_per_job(void)
49249caa483SSimon J. Gerraty {
49349caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
49449caa483SSimon J. Gerraty 	if (useMeta)
49549caa483SSimon J. Gerraty 		return 2;
49649caa483SSimon J. Gerraty #endif
49749caa483SSimon J. Gerraty 	return 1;
49849caa483SSimon J. Gerraty }
49949caa483SSimon J. Gerraty 
50006b9b3e0SSimon J. Gerraty void
50106b9b3e0SSimon J. Gerraty Job_FlagsToString(const Job *job, char *buf, size_t bufsize)
50206b9b3e0SSimon J. Gerraty {
50306b9b3e0SSimon J. Gerraty 	snprintf(buf, bufsize, "%c%c%c",
50406b9b3e0SSimon J. Gerraty 	    job->ignerr ? 'i' : '-',
50506b9b3e0SSimon J. Gerraty 	    !job->echo ? 's' : '-',
50606b9b3e0SSimon J. Gerraty 	    job->special ? 'S' : '-');
50706b9b3e0SSimon J. Gerraty }
50806b9b3e0SSimon J. Gerraty 
5093955d011SMarcel Moolenaar static void
510b0c40a00SSimon J. Gerraty DumpJobs(const char *where)
5113955d011SMarcel Moolenaar {
5123955d011SMarcel Moolenaar 	Job *job;
51306b9b3e0SSimon J. Gerraty 	char flags[4];
5143955d011SMarcel Moolenaar 
515956e45f6SSimon J. Gerraty 	debug_printf("job table @ %s\n", where);
5163955d011SMarcel Moolenaar 	for (job = job_table; job < job_table_end; job++) {
51706b9b3e0SSimon J. Gerraty 		Job_FlagsToString(job, flags, sizeof flags);
51806b9b3e0SSimon J. Gerraty 		debug_printf("job %d, status %d, flags %s, pid %d\n",
51906b9b3e0SSimon J. Gerraty 		    (int)(job - job_table), job->status, flags, job->pid);
5203955d011SMarcel Moolenaar 	}
5213955d011SMarcel Moolenaar }
5223955d011SMarcel Moolenaar 
5233955d011SMarcel Moolenaar /*
52445447996SSimon J. Gerraty  * Delete the target of a failed, interrupted, or otherwise
52545447996SSimon J. Gerraty  * unsuccessful job unless inhibited by .PRECIOUS.
52645447996SSimon J. Gerraty  */
52745447996SSimon J. Gerraty static void
52845447996SSimon J. Gerraty JobDeleteTarget(GNode *gn)
52945447996SSimon J. Gerraty {
530956e45f6SSimon J. Gerraty 	const char *file;
531956e45f6SSimon J. Gerraty 
532956e45f6SSimon J. Gerraty 	if (gn->type & OP_JOIN)
533956e45f6SSimon J. Gerraty 		return;
534956e45f6SSimon J. Gerraty 	if (gn->type & OP_PHONY)
535956e45f6SSimon J. Gerraty 		return;
536956e45f6SSimon J. Gerraty 	if (Targ_Precious(gn))
537956e45f6SSimon J. Gerraty 		return;
538956e45f6SSimon J. Gerraty 	if (opts.noExecute)
539956e45f6SSimon J. Gerraty 		return;
540956e45f6SSimon J. Gerraty 
541956e45f6SSimon J. Gerraty 	file = GNode_Path(gn);
542956e45f6SSimon J. Gerraty 	if (eunlink(file) != -1)
54345447996SSimon J. Gerraty 		Error("*** %s removed", file);
54445447996SSimon J. Gerraty }
54545447996SSimon J. Gerraty 
54645447996SSimon J. Gerraty /*
5473955d011SMarcel Moolenaar  * JobSigLock/JobSigUnlock
5483955d011SMarcel Moolenaar  *
5493955d011SMarcel Moolenaar  * Signal lock routines to get exclusive access. Currently used to
5503955d011SMarcel Moolenaar  * protect `jobs' and `stoppedJobs' list manipulations.
5513955d011SMarcel Moolenaar  */
55206b9b3e0SSimon J. Gerraty static void
55306b9b3e0SSimon J. Gerraty JobSigLock(sigset_t *omaskp)
5543955d011SMarcel Moolenaar {
5553955d011SMarcel Moolenaar 	if (sigprocmask(SIG_BLOCK, &caught_signals, omaskp) != 0) {
5563955d011SMarcel Moolenaar 		Punt("JobSigLock: sigprocmask: %s", strerror(errno));
5573955d011SMarcel Moolenaar 		sigemptyset(omaskp);
5583955d011SMarcel Moolenaar 	}
5593955d011SMarcel Moolenaar }
5603955d011SMarcel Moolenaar 
56106b9b3e0SSimon J. Gerraty static void
56206b9b3e0SSimon J. Gerraty JobSigUnlock(sigset_t *omaskp)
5633955d011SMarcel Moolenaar {
5643955d011SMarcel Moolenaar 	(void)sigprocmask(SIG_SETMASK, omaskp, NULL);
5653955d011SMarcel Moolenaar }
5663955d011SMarcel Moolenaar 
5673955d011SMarcel Moolenaar static void
5683955d011SMarcel Moolenaar JobCreatePipe(Job *job, int minfd)
5693955d011SMarcel Moolenaar {
570e1cee40dSSimon J. Gerraty 	int i, fd, flags;
571956e45f6SSimon J. Gerraty 	int pipe_fds[2];
5723955d011SMarcel Moolenaar 
573956e45f6SSimon J. Gerraty 	if (pipe(pipe_fds) == -1)
5743955d011SMarcel Moolenaar 		Punt("Cannot create pipe: %s", strerror(errno));
5753955d011SMarcel Moolenaar 
57674d2e02bSSimon J. Gerraty 	for (i = 0; i < 2; i++) {
57774d2e02bSSimon J. Gerraty 		/* Avoid using low numbered fds */
578956e45f6SSimon J. Gerraty 		fd = fcntl(pipe_fds[i], F_DUPFD, minfd);
57974d2e02bSSimon J. Gerraty 		if (fd != -1) {
580956e45f6SSimon J. Gerraty 			close(pipe_fds[i]);
581956e45f6SSimon J. Gerraty 			pipe_fds[i] = fd;
58274d2e02bSSimon J. Gerraty 		}
58374d2e02bSSimon J. Gerraty 	}
58474d2e02bSSimon J. Gerraty 
585956e45f6SSimon J. Gerraty 	job->inPipe = pipe_fds[0];
586956e45f6SSimon J. Gerraty 	job->outPipe = pipe_fds[1];
587956e45f6SSimon J. Gerraty 
5883955d011SMarcel Moolenaar 	/* Set close-on-exec flag for both */
589956e45f6SSimon J. Gerraty 	if (fcntl(job->inPipe, F_SETFD, FD_CLOEXEC) == -1)
590e1cee40dSSimon J. Gerraty 		Punt("Cannot set close-on-exec: %s", strerror(errno));
591956e45f6SSimon J. Gerraty 	if (fcntl(job->outPipe, F_SETFD, FD_CLOEXEC) == -1)
592e1cee40dSSimon J. Gerraty 		Punt("Cannot set close-on-exec: %s", strerror(errno));
5933955d011SMarcel Moolenaar 
5943955d011SMarcel Moolenaar 	/*
5953955d011SMarcel Moolenaar 	 * We mark the input side of the pipe non-blocking; we poll(2) the
5963955d011SMarcel Moolenaar 	 * pipe when we're waiting for a job token, but we might lose the
5973955d011SMarcel Moolenaar 	 * race for the token when a new one becomes available, so the read
5983955d011SMarcel Moolenaar 	 * from the pipe should not block.
5993955d011SMarcel Moolenaar 	 */
600956e45f6SSimon J. Gerraty 	flags = fcntl(job->inPipe, F_GETFL, 0);
601e1cee40dSSimon J. Gerraty 	if (flags == -1)
602e1cee40dSSimon J. Gerraty 		Punt("Cannot get flags: %s", strerror(errno));
603e1cee40dSSimon J. Gerraty 	flags |= O_NONBLOCK;
604956e45f6SSimon J. Gerraty 	if (fcntl(job->inPipe, F_SETFL, flags) == -1)
605e1cee40dSSimon J. Gerraty 		Punt("Cannot set flags: %s", strerror(errno));
6063955d011SMarcel Moolenaar }
6073955d011SMarcel Moolenaar 
608956e45f6SSimon J. Gerraty /* Pass the signal to each running job. */
6093955d011SMarcel Moolenaar static void
6103955d011SMarcel Moolenaar JobCondPassSig(int signo)
6113955d011SMarcel Moolenaar {
6123955d011SMarcel Moolenaar 	Job *job;
6133955d011SMarcel Moolenaar 
614956e45f6SSimon J. Gerraty 	DEBUG1(JOB, "JobCondPassSig(%d) called.\n", signo);
6153955d011SMarcel Moolenaar 
6163955d011SMarcel Moolenaar 	for (job = job_table; job < job_table_end; job++) {
617e2eeea75SSimon J. Gerraty 		if (job->status != JOB_ST_RUNNING)
6183955d011SMarcel Moolenaar 			continue;
619956e45f6SSimon J. Gerraty 		DEBUG2(JOB, "JobCondPassSig passing signal %d to child %d.\n",
6203955d011SMarcel Moolenaar 		    signo, job->pid);
6213955d011SMarcel Moolenaar 		KILLPG(job->pid, signo);
6223955d011SMarcel Moolenaar 	}
6233955d011SMarcel Moolenaar }
6243955d011SMarcel Moolenaar 
62506b9b3e0SSimon J. Gerraty /*
62606b9b3e0SSimon J. Gerraty  * SIGCHLD handler.
6273955d011SMarcel Moolenaar  *
62806b9b3e0SSimon J. Gerraty  * Sends a token on the child exit pipe to wake us up from select()/poll().
62906b9b3e0SSimon J. Gerraty  */
63006b9b3e0SSimon J. Gerraty /*ARGSUSED*/
6313955d011SMarcel Moolenaar static void
6323955d011SMarcel Moolenaar JobChildSig(int signo MAKE_ATTR_UNUSED)
6333955d011SMarcel Moolenaar {
634dba7b0efSSimon J. Gerraty 	caught_sigchld = 1;
63506b9b3e0SSimon J. Gerraty 	while (write(childExitJob.outPipe, CHILD_EXIT, 1) == -1 &&
63606b9b3e0SSimon J. Gerraty 	       errno == EAGAIN)
6373cbdda60SSimon J. Gerraty 		continue;
6383955d011SMarcel Moolenaar }
6393955d011SMarcel Moolenaar 
6403955d011SMarcel Moolenaar 
641956e45f6SSimon J. Gerraty /* Resume all stopped jobs. */
64206b9b3e0SSimon J. Gerraty /*ARGSUSED*/
6433955d011SMarcel Moolenaar static void
6443955d011SMarcel Moolenaar JobContinueSig(int signo MAKE_ATTR_UNUSED)
6453955d011SMarcel Moolenaar {
6463955d011SMarcel Moolenaar 	/*
647956e45f6SSimon J. Gerraty 	 * Defer sending SIGCONT to our stopped children until we return
6483955d011SMarcel Moolenaar 	 * from the signal handler.
6493955d011SMarcel Moolenaar 	 */
6503cbdda60SSimon J. Gerraty 	while (write(childExitJob.outPipe, DO_JOB_RESUME, 1) == -1 &&
6513cbdda60SSimon J. Gerraty 	       errno == EAGAIN)
6523cbdda60SSimon J. Gerraty 		continue;
6533955d011SMarcel Moolenaar }
6543955d011SMarcel Moolenaar 
65506b9b3e0SSimon J. Gerraty /*
65606b9b3e0SSimon J. Gerraty  * Pass a signal on to all jobs, then resend to ourselves.
65706b9b3e0SSimon J. Gerraty  * We die by the same signal.
65806b9b3e0SSimon J. Gerraty  */
6593955d011SMarcel Moolenaar MAKE_ATTR_DEAD static void
6603955d011SMarcel Moolenaar JobPassSig_int(int signo)
6613955d011SMarcel Moolenaar {
6623955d011SMarcel Moolenaar 	/* Run .INTERRUPT target then exit */
663b0c40a00SSimon J. Gerraty 	JobInterrupt(true, signo);
6643955d011SMarcel Moolenaar }
6653955d011SMarcel Moolenaar 
66606b9b3e0SSimon J. Gerraty /*
66706b9b3e0SSimon J. Gerraty  * Pass a signal on to all jobs, then resend to ourselves.
66806b9b3e0SSimon J. Gerraty  * We die by the same signal.
66906b9b3e0SSimon J. Gerraty  */
6703955d011SMarcel Moolenaar MAKE_ATTR_DEAD static void
6713955d011SMarcel Moolenaar JobPassSig_term(int signo)
6723955d011SMarcel Moolenaar {
6733955d011SMarcel Moolenaar 	/* Dont run .INTERRUPT target then exit */
674b0c40a00SSimon J. Gerraty 	JobInterrupt(false, signo);
6753955d011SMarcel Moolenaar }
6763955d011SMarcel Moolenaar 
6773955d011SMarcel Moolenaar static void
6783955d011SMarcel Moolenaar JobPassSig_suspend(int signo)
6793955d011SMarcel Moolenaar {
6803955d011SMarcel Moolenaar 	sigset_t nmask, omask;
6813955d011SMarcel Moolenaar 	struct sigaction act;
6823955d011SMarcel Moolenaar 
6833955d011SMarcel Moolenaar 	/* Suppress job started/continued messages */
684b0c40a00SSimon J. Gerraty 	make_suspended = true;
6853955d011SMarcel Moolenaar 
6863955d011SMarcel Moolenaar 	/* Pass the signal onto every job */
6873955d011SMarcel Moolenaar 	JobCondPassSig(signo);
6883955d011SMarcel Moolenaar 
6893955d011SMarcel Moolenaar 	/*
69006b9b3e0SSimon J. Gerraty 	 * Send ourselves the signal now we've given the message to everyone
69106b9b3e0SSimon J. Gerraty 	 * else. Note we block everything else possible while we're getting
69206b9b3e0SSimon J. Gerraty 	 * the signal. This ensures that all our jobs get continued when we
69306b9b3e0SSimon J. Gerraty 	 * wake up before we take any other signal.
6943955d011SMarcel Moolenaar 	 */
6953955d011SMarcel Moolenaar 	sigfillset(&nmask);
6963955d011SMarcel Moolenaar 	sigdelset(&nmask, signo);
6973955d011SMarcel Moolenaar 	(void)sigprocmask(SIG_SETMASK, &nmask, &omask);
6983955d011SMarcel Moolenaar 
6993955d011SMarcel Moolenaar 	act.sa_handler = SIG_DFL;
7003955d011SMarcel Moolenaar 	sigemptyset(&act.sa_mask);
7013955d011SMarcel Moolenaar 	act.sa_flags = 0;
7023955d011SMarcel Moolenaar 	(void)sigaction(signo, &act, NULL);
7033955d011SMarcel Moolenaar 
70406b9b3e0SSimon J. Gerraty 	DEBUG1(JOB, "JobPassSig passing signal %d to self.\n", signo);
7053955d011SMarcel Moolenaar 
7063955d011SMarcel Moolenaar 	(void)kill(getpid(), signo);
7073955d011SMarcel Moolenaar 
7083955d011SMarcel Moolenaar 	/*
7093955d011SMarcel Moolenaar 	 * We've been continued.
7103955d011SMarcel Moolenaar 	 *
7113955d011SMarcel Moolenaar 	 * A whole host of signals continue to happen!
7123955d011SMarcel Moolenaar 	 * SIGCHLD for any processes that actually suspended themselves.
7133955d011SMarcel Moolenaar 	 * SIGCHLD for any processes that exited while we were alseep.
7143955d011SMarcel Moolenaar 	 * The SIGCONT that actually caused us to wakeup.
7153955d011SMarcel Moolenaar 	 *
7163955d011SMarcel Moolenaar 	 * Since we defer passing the SIGCONT on to our children until
7173955d011SMarcel Moolenaar 	 * the main processing loop, we can be sure that all the SIGCHLD
7183955d011SMarcel Moolenaar 	 * events will have happened by then - and that the waitpid() will
7193955d011SMarcel Moolenaar 	 * collect the child 'suspended' events.
7203955d011SMarcel Moolenaar 	 * For correct sequencing we just need to ensure we process the
721956e45f6SSimon J. Gerraty 	 * waitpid() before passing on the SIGCONT.
7223955d011SMarcel Moolenaar 	 *
7233955d011SMarcel Moolenaar 	 * In any case nothing else is needed here.
7243955d011SMarcel Moolenaar 	 */
7253955d011SMarcel Moolenaar 
7263955d011SMarcel Moolenaar 	/* Restore handler and signal mask */
7273955d011SMarcel Moolenaar 	act.sa_handler = JobPassSig_suspend;
7283955d011SMarcel Moolenaar 	(void)sigaction(signo, &act, NULL);
7293955d011SMarcel Moolenaar 	(void)sigprocmask(SIG_SETMASK, &omask, NULL);
7303955d011SMarcel Moolenaar }
7313955d011SMarcel Moolenaar 
7323955d011SMarcel Moolenaar static Job *
733b0c40a00SSimon J. Gerraty JobFindPid(int pid, JobStatus status, bool isJobs)
7343955d011SMarcel Moolenaar {
7353955d011SMarcel Moolenaar 	Job *job;
7363955d011SMarcel Moolenaar 
7373955d011SMarcel Moolenaar 	for (job = job_table; job < job_table_end; job++) {
738e2eeea75SSimon J. Gerraty 		if (job->status == status && job->pid == pid)
7393955d011SMarcel Moolenaar 			return job;
7403955d011SMarcel Moolenaar 	}
7413955d011SMarcel Moolenaar 	if (DEBUG(JOB) && isJobs)
742b0c40a00SSimon J. Gerraty 		DumpJobs("no pid");
7433955d011SMarcel Moolenaar 	return NULL;
7443955d011SMarcel Moolenaar }
7453955d011SMarcel Moolenaar 
746956e45f6SSimon J. Gerraty /* Parse leading '@', '-' and '+', which control the exact execution mode. */
747956e45f6SSimon J. Gerraty static void
74806b9b3e0SSimon J. Gerraty ParseCommandFlags(char **pp, CommandFlags *out_cmdFlags)
749956e45f6SSimon J. Gerraty {
750956e45f6SSimon J. Gerraty 	char *p = *pp;
751b0c40a00SSimon J. Gerraty 	out_cmdFlags->echo = true;
752b0c40a00SSimon J. Gerraty 	out_cmdFlags->ignerr = false;
753b0c40a00SSimon J. Gerraty 	out_cmdFlags->always = false;
754956e45f6SSimon J. Gerraty 
755956e45f6SSimon J. Gerraty 	for (;;) {
756956e45f6SSimon J. Gerraty 		if (*p == '@')
75706b9b3e0SSimon J. Gerraty 			out_cmdFlags->echo = DEBUG(LOUD);
758956e45f6SSimon J. Gerraty 		else if (*p == '-')
759b0c40a00SSimon J. Gerraty 			out_cmdFlags->ignerr = true;
760956e45f6SSimon J. Gerraty 		else if (*p == '+')
761b0c40a00SSimon J. Gerraty 			out_cmdFlags->always = true;
762956e45f6SSimon J. Gerraty 		else
763956e45f6SSimon J. Gerraty 			break;
764956e45f6SSimon J. Gerraty 		p++;
765956e45f6SSimon J. Gerraty 	}
766956e45f6SSimon J. Gerraty 
767956e45f6SSimon J. Gerraty 	pp_skip_whitespace(&p);
768956e45f6SSimon J. Gerraty 
769956e45f6SSimon J. Gerraty 	*pp = p;
770956e45f6SSimon J. Gerraty }
771956e45f6SSimon J. Gerraty 
772956e45f6SSimon J. Gerraty /* Escape a string for a double-quoted string literal in sh, csh and ksh. */
773956e45f6SSimon J. Gerraty static char *
774956e45f6SSimon J. Gerraty EscapeShellDblQuot(const char *cmd)
775956e45f6SSimon J. Gerraty {
776956e45f6SSimon J. Gerraty 	size_t i, j;
777956e45f6SSimon J. Gerraty 
778956e45f6SSimon J. Gerraty 	/* Worst that could happen is every char needs escaping. */
779956e45f6SSimon J. Gerraty 	char *esc = bmake_malloc(strlen(cmd) * 2 + 1);
780956e45f6SSimon J. Gerraty 	for (i = 0, j = 0; cmd[i] != '\0'; i++, j++) {
78106b9b3e0SSimon J. Gerraty 		if (cmd[i] == '$' || cmd[i] == '`' || cmd[i] == '\\' ||
78206b9b3e0SSimon J. Gerraty 		    cmd[i] == '"')
783956e45f6SSimon J. Gerraty 			esc[j++] = '\\';
784956e45f6SSimon J. Gerraty 		esc[j] = cmd[i];
785956e45f6SSimon J. Gerraty 	}
786956e45f6SSimon J. Gerraty 	esc[j] = '\0';
787956e45f6SSimon J. Gerraty 
788956e45f6SSimon J. Gerraty 	return esc;
789956e45f6SSimon J. Gerraty }
790956e45f6SSimon J. Gerraty 
791e2eeea75SSimon J. Gerraty static void
792b0c40a00SSimon J. Gerraty ShellWriter_WriteFmt(ShellWriter *wr, const char *fmt, const char *arg)
793e2eeea75SSimon J. Gerraty {
79406b9b3e0SSimon J. Gerraty 	DEBUG1(JOB, fmt, arg);
795e2eeea75SSimon J. Gerraty 
79606b9b3e0SSimon J. Gerraty 	(void)fprintf(wr->f, fmt, arg);
797*12904384SSimon J. Gerraty 	if (wr->f == stdout)
79806b9b3e0SSimon J. Gerraty 		(void)fflush(wr->f);
799e2eeea75SSimon J. Gerraty }
800e2eeea75SSimon J. Gerraty 
801e2eeea75SSimon J. Gerraty static void
802b0c40a00SSimon J. Gerraty ShellWriter_WriteLine(ShellWriter *wr, const char *line)
803e2eeea75SSimon J. Gerraty {
804b0c40a00SSimon J. Gerraty 	ShellWriter_WriteFmt(wr, "%s\n", line);
805e2eeea75SSimon J. Gerraty }
806e2eeea75SSimon J. Gerraty 
80706b9b3e0SSimon J. Gerraty static void
80806b9b3e0SSimon J. Gerraty ShellWriter_EchoOff(ShellWriter *wr)
80906b9b3e0SSimon J. Gerraty {
81006b9b3e0SSimon J. Gerraty 	if (shell->hasEchoCtl)
811b0c40a00SSimon J. Gerraty 		ShellWriter_WriteLine(wr, shell->echoOff);
81206b9b3e0SSimon J. Gerraty }
81306b9b3e0SSimon J. Gerraty 
81406b9b3e0SSimon J. Gerraty static void
81506b9b3e0SSimon J. Gerraty ShellWriter_EchoCmd(ShellWriter *wr, const char *escCmd)
81606b9b3e0SSimon J. Gerraty {
817b0c40a00SSimon J. Gerraty 	ShellWriter_WriteFmt(wr, shell->echoTmpl, escCmd);
81806b9b3e0SSimon J. Gerraty }
81906b9b3e0SSimon J. Gerraty 
82006b9b3e0SSimon J. Gerraty static void
82106b9b3e0SSimon J. Gerraty ShellWriter_EchoOn(ShellWriter *wr)
82206b9b3e0SSimon J. Gerraty {
82306b9b3e0SSimon J. Gerraty 	if (shell->hasEchoCtl)
824b0c40a00SSimon J. Gerraty 		ShellWriter_WriteLine(wr, shell->echoOn);
82506b9b3e0SSimon J. Gerraty }
82606b9b3e0SSimon J. Gerraty 
82706b9b3e0SSimon J. Gerraty static void
82806b9b3e0SSimon J. Gerraty ShellWriter_TraceOn(ShellWriter *wr)
82906b9b3e0SSimon J. Gerraty {
83006b9b3e0SSimon J. Gerraty 	if (!wr->xtraced) {
831b0c40a00SSimon J. Gerraty 		ShellWriter_WriteLine(wr, "set -x");
832b0c40a00SSimon J. Gerraty 		wr->xtraced = true;
83306b9b3e0SSimon J. Gerraty 	}
83406b9b3e0SSimon J. Gerraty }
83506b9b3e0SSimon J. Gerraty 
83606b9b3e0SSimon J. Gerraty static void
837b0c40a00SSimon J. Gerraty ShellWriter_ErrOff(ShellWriter *wr, bool echo)
83806b9b3e0SSimon J. Gerraty {
83906b9b3e0SSimon J. Gerraty 	if (echo)
84006b9b3e0SSimon J. Gerraty 		ShellWriter_EchoOff(wr);
841b0c40a00SSimon J. Gerraty 	ShellWriter_WriteLine(wr, shell->errOff);
84206b9b3e0SSimon J. Gerraty 	if (echo)
84306b9b3e0SSimon J. Gerraty 		ShellWriter_EchoOn(wr);
84406b9b3e0SSimon J. Gerraty }
84506b9b3e0SSimon J. Gerraty 
84606b9b3e0SSimon J. Gerraty static void
847b0c40a00SSimon J. Gerraty ShellWriter_ErrOn(ShellWriter *wr, bool echo)
84806b9b3e0SSimon J. Gerraty {
84906b9b3e0SSimon J. Gerraty 	if (echo)
85006b9b3e0SSimon J. Gerraty 		ShellWriter_EchoOff(wr);
851b0c40a00SSimon J. Gerraty 	ShellWriter_WriteLine(wr, shell->errOn);
85206b9b3e0SSimon J. Gerraty 	if (echo)
85306b9b3e0SSimon J. Gerraty 		ShellWriter_EchoOn(wr);
85406b9b3e0SSimon J. Gerraty }
85506b9b3e0SSimon J. Gerraty 
85606b9b3e0SSimon J. Gerraty /*
85706b9b3e0SSimon J. Gerraty  * The shell has no built-in error control, so emulate error control by
85806b9b3e0SSimon J. Gerraty  * enclosing each shell command in a template like "{ %s \n } || exit $?"
85906b9b3e0SSimon J. Gerraty  * (configurable per shell).
8603955d011SMarcel Moolenaar  */
861956e45f6SSimon J. Gerraty static void
862b0c40a00SSimon J. Gerraty JobWriteSpecialsEchoCtl(Job *job, ShellWriter *wr, CommandFlags *inout_cmdFlags,
86306b9b3e0SSimon J. Gerraty 			const char *escCmd, const char **inout_cmdTemplate)
8643955d011SMarcel Moolenaar {
86506b9b3e0SSimon J. Gerraty 	/* XXX: Why is the job modified at this point? */
866b0c40a00SSimon J. Gerraty 	job->ignerr = true;
8673955d011SMarcel Moolenaar 
86806b9b3e0SSimon J. Gerraty 	if (job->echo && inout_cmdFlags->echo) {
86906b9b3e0SSimon J. Gerraty 		ShellWriter_EchoOff(wr);
87006b9b3e0SSimon J. Gerraty 		ShellWriter_EchoCmd(wr, escCmd);
8713955d011SMarcel Moolenaar 
87206b9b3e0SSimon J. Gerraty 		/*
87306b9b3e0SSimon J. Gerraty 		 * Leave echoing off so the user doesn't see the commands
87406b9b3e0SSimon J. Gerraty 		 * for toggling the error checking.
87506b9b3e0SSimon J. Gerraty 		 */
876b0c40a00SSimon J. Gerraty 		inout_cmdFlags->echo = false;
87706b9b3e0SSimon J. Gerraty 	} else {
87806b9b3e0SSimon J. Gerraty 		if (inout_cmdFlags->echo)
87906b9b3e0SSimon J. Gerraty 			ShellWriter_EchoCmd(wr, escCmd);
88006b9b3e0SSimon J. Gerraty 	}
88106b9b3e0SSimon J. Gerraty 	*inout_cmdTemplate = shell->runIgnTmpl;
8823955d011SMarcel Moolenaar 
88306b9b3e0SSimon J. Gerraty 	/*
88406b9b3e0SSimon J. Gerraty 	 * The template runIgnTmpl already takes care of ignoring errors,
88506b9b3e0SSimon J. Gerraty 	 * so pretend error checking is still on.
88606b9b3e0SSimon J. Gerraty 	 * XXX: What effects does this have, and why is it necessary?
88706b9b3e0SSimon J. Gerraty 	 */
888b0c40a00SSimon J. Gerraty 	inout_cmdFlags->ignerr = false;
88906b9b3e0SSimon J. Gerraty }
89006b9b3e0SSimon J. Gerraty 
89106b9b3e0SSimon J. Gerraty static void
892b0c40a00SSimon J. Gerraty JobWriteSpecials(Job *job, ShellWriter *wr, const char *escCmd, bool run,
89306b9b3e0SSimon J. Gerraty 		 CommandFlags *inout_cmdFlags, const char **inout_cmdTemplate)
89406b9b3e0SSimon J. Gerraty {
89506b9b3e0SSimon J. Gerraty 	if (!run) {
89606b9b3e0SSimon J. Gerraty 		/*
89706b9b3e0SSimon J. Gerraty 		 * If there is no command to run, there is no need to switch
89806b9b3e0SSimon J. Gerraty 		 * error checking off and on again for nothing.
89906b9b3e0SSimon J. Gerraty 		 */
900b0c40a00SSimon J. Gerraty 		inout_cmdFlags->ignerr = false;
90106b9b3e0SSimon J. Gerraty 	} else if (shell->hasErrCtl)
90206b9b3e0SSimon J. Gerraty 		ShellWriter_ErrOff(wr, job->echo && inout_cmdFlags->echo);
90306b9b3e0SSimon J. Gerraty 	else if (shell->runIgnTmpl != NULL && shell->runIgnTmpl[0] != '\0') {
904b0c40a00SSimon J. Gerraty 		JobWriteSpecialsEchoCtl(job, wr, inout_cmdFlags, escCmd,
90506b9b3e0SSimon J. Gerraty 		    inout_cmdTemplate);
90606b9b3e0SSimon J. Gerraty 	} else
907b0c40a00SSimon J. Gerraty 		inout_cmdFlags->ignerr = false;
90806b9b3e0SSimon J. Gerraty }
90906b9b3e0SSimon J. Gerraty 
91006b9b3e0SSimon J. Gerraty /*
911b0c40a00SSimon J. Gerraty  * Write a shell command to the job's commands file, to be run later.
91206b9b3e0SSimon J. Gerraty  *
91306b9b3e0SSimon J. Gerraty  * If the command starts with '@' and neither the -s nor the -n flag was
914b0c40a00SSimon J. Gerraty  * given to make, stick a shell-specific echoOff command in the script.
91506b9b3e0SSimon J. Gerraty  *
91606b9b3e0SSimon J. Gerraty  * If the command starts with '-' and the shell has no error control (none
917b0c40a00SSimon J. Gerraty  * of the predefined shells has that), ignore errors for the entire job.
91806b9b3e0SSimon J. Gerraty  *
919b0c40a00SSimon J. Gerraty  * XXX: Why ignore errors for the entire job?  This is even documented in the
920b0c40a00SSimon J. Gerraty  * manual page, but without any rationale since there is no known rationale.
921b0c40a00SSimon J. Gerraty  *
922b0c40a00SSimon J. Gerraty  * XXX: The manual page says the '-' "affects the entire job", but that's not
923b0c40a00SSimon J. Gerraty  * accurate.  The '-' does not affect the commands before the '-'.
924b0c40a00SSimon J. Gerraty  *
925b0c40a00SSimon J. Gerraty  * If the command is just "...", skip all further commands of this job.  These
926b0c40a00SSimon J. Gerraty  * commands are attached to the .END node instead and will be run by
927b0c40a00SSimon J. Gerraty  * Job_Finish after all other targets have been made.
92806b9b3e0SSimon J. Gerraty  */
92906b9b3e0SSimon J. Gerraty static void
930b0c40a00SSimon J. Gerraty JobWriteCommand(Job *job, ShellWriter *wr, StringListNode *ln, const char *ucmd)
93106b9b3e0SSimon J. Gerraty {
932b0c40a00SSimon J. Gerraty 	bool run;
93306b9b3e0SSimon J. Gerraty 
93406b9b3e0SSimon J. Gerraty 	CommandFlags cmdFlags;
935b0c40a00SSimon J. Gerraty 	/* Template for writing a command to the shell file */
93606b9b3e0SSimon J. Gerraty 	const char *cmdTemplate;
93706b9b3e0SSimon J. Gerraty 	char *xcmd;		/* The expanded command */
93806b9b3e0SSimon J. Gerraty 	char *xcmdStart;
93906b9b3e0SSimon J. Gerraty 	char *escCmd;		/* xcmd escaped to be used in double quotes */
94006b9b3e0SSimon J. Gerraty 
94106b9b3e0SSimon J. Gerraty 	run = GNode_ShouldExecute(job->node);
94206b9b3e0SSimon J. Gerraty 
94306b9b3e0SSimon J. Gerraty 	Var_Subst(ucmd, job->node, VARE_WANTRES, &xcmd);
944956e45f6SSimon J. Gerraty 	/* TODO: handle errors */
94506b9b3e0SSimon J. Gerraty 	xcmdStart = xcmd;
9463955d011SMarcel Moolenaar 
9473955d011SMarcel Moolenaar 	cmdTemplate = "%s\n";
9483955d011SMarcel Moolenaar 
94906b9b3e0SSimon J. Gerraty 	ParseCommandFlags(&xcmd, &cmdFlags);
950956e45f6SSimon J. Gerraty 
95106b9b3e0SSimon J. Gerraty 	/* The '+' command flag overrides the -n or -N options. */
95206b9b3e0SSimon J. Gerraty 	if (cmdFlags.always && !run) {
9533955d011SMarcel Moolenaar 		/*
9543955d011SMarcel Moolenaar 		 * We're not actually executing anything...
9553955d011SMarcel Moolenaar 		 * but this one needs to be - use compat mode just for it.
9563955d011SMarcel Moolenaar 		 */
95706b9b3e0SSimon J. Gerraty 		Compat_RunCommand(ucmd, job->node, ln);
95806b9b3e0SSimon J. Gerraty 		free(xcmdStart);
959956e45f6SSimon J. Gerraty 		return;
9603955d011SMarcel Moolenaar 	}
9613955d011SMarcel Moolenaar 
9623955d011SMarcel Moolenaar 	/*
96306b9b3e0SSimon J. Gerraty 	 * If the shell doesn't have error control, the alternate echoing
96406b9b3e0SSimon J. Gerraty 	 * will be done (to avoid showing additional error checking code)
96506b9b3e0SSimon J. Gerraty 	 * and this needs some characters escaped.
9663955d011SMarcel Moolenaar 	 */
96706b9b3e0SSimon J. Gerraty 	escCmd = shell->hasErrCtl ? NULL : EscapeShellDblQuot(xcmd);
9683955d011SMarcel Moolenaar 
96906b9b3e0SSimon J. Gerraty 	if (!cmdFlags.echo) {
97006b9b3e0SSimon J. Gerraty 		if (job->echo && run && shell->hasEchoCtl) {
97106b9b3e0SSimon J. Gerraty 			ShellWriter_EchoOff(wr);
9723955d011SMarcel Moolenaar 		} else {
97306b9b3e0SSimon J. Gerraty 			if (shell->hasErrCtl)
974b0c40a00SSimon J. Gerraty 				cmdFlags.echo = true;
9753955d011SMarcel Moolenaar 		}
9763955d011SMarcel Moolenaar 	}
9773955d011SMarcel Moolenaar 
97806b9b3e0SSimon J. Gerraty 	if (cmdFlags.ignerr) {
979b0c40a00SSimon J. Gerraty 		JobWriteSpecials(job, wr, escCmd, run, &cmdFlags, &cmdTemplate);
9803955d011SMarcel Moolenaar 	} else {
9813955d011SMarcel Moolenaar 
9823955d011SMarcel Moolenaar 		/*
98306b9b3e0SSimon J. Gerraty 		 * If errors are being checked and the shell doesn't have
98406b9b3e0SSimon J. Gerraty 		 * error control but does supply an runChkTmpl template, then
98506b9b3e0SSimon J. Gerraty 		 * set up commands to run through it.
9863955d011SMarcel Moolenaar 		 */
9873955d011SMarcel Moolenaar 
98806b9b3e0SSimon J. Gerraty 		if (!shell->hasErrCtl && shell->runChkTmpl != NULL &&
98906b9b3e0SSimon J. Gerraty 		    shell->runChkTmpl[0] != '\0') {
99006b9b3e0SSimon J. Gerraty 			if (job->echo && cmdFlags.echo) {
99106b9b3e0SSimon J. Gerraty 				ShellWriter_EchoOff(wr);
99206b9b3e0SSimon J. Gerraty 				ShellWriter_EchoCmd(wr, escCmd);
993b0c40a00SSimon J. Gerraty 				cmdFlags.echo = false;
9943955d011SMarcel Moolenaar 			}
99506b9b3e0SSimon J. Gerraty 			/*
99606b9b3e0SSimon J. Gerraty 			 * If it's a comment line or blank, avoid the possible
99706b9b3e0SSimon J. Gerraty 			 * syntax error generated by "{\n} || exit $?".
99806b9b3e0SSimon J. Gerraty 			 */
99906b9b3e0SSimon J. Gerraty 			cmdTemplate = escCmd[0] == shell->commentChar ||
100006b9b3e0SSimon J. Gerraty 				      escCmd[0] == '\0'
100106b9b3e0SSimon J. Gerraty 			    ? shell->runIgnTmpl
100206b9b3e0SSimon J. Gerraty 			    : shell->runChkTmpl;
1003b0c40a00SSimon J. Gerraty 			cmdFlags.ignerr = false;
10043955d011SMarcel Moolenaar 		}
10053955d011SMarcel Moolenaar 	}
10063955d011SMarcel Moolenaar 
100706b9b3e0SSimon J. Gerraty 	if (DEBUG(SHELL) && strcmp(shellName, "sh") == 0)
100806b9b3e0SSimon J. Gerraty 		ShellWriter_TraceOn(wr);
10093955d011SMarcel Moolenaar 
1010b0c40a00SSimon J. Gerraty 	ShellWriter_WriteFmt(wr, cmdTemplate, xcmd);
101106b9b3e0SSimon J. Gerraty 	free(xcmdStart);
10123955d011SMarcel Moolenaar 	free(escCmd);
101306b9b3e0SSimon J. Gerraty 
101406b9b3e0SSimon J. Gerraty 	if (cmdFlags.ignerr)
101506b9b3e0SSimon J. Gerraty 		ShellWriter_ErrOn(wr, cmdFlags.echo && job->echo);
101606b9b3e0SSimon J. Gerraty 
101706b9b3e0SSimon J. Gerraty 	if (!cmdFlags.echo)
101806b9b3e0SSimon J. Gerraty 		ShellWriter_EchoOn(wr);
10193955d011SMarcel Moolenaar }
10203955d011SMarcel Moolenaar 
102106b9b3e0SSimon J. Gerraty /*
1022b0c40a00SSimon J. Gerraty  * Write all commands to the shell file that is later executed.
10233955d011SMarcel Moolenaar  *
1024b0c40a00SSimon J. Gerraty  * The special command "..." stops writing and saves the remaining commands
1025dba7b0efSSimon J. Gerraty  * to be executed later, when the target '.END' is made.
102606b9b3e0SSimon J. Gerraty  *
102706b9b3e0SSimon J. Gerraty  * Return whether at least one command was written to the shell file.
102806b9b3e0SSimon J. Gerraty  */
1029b0c40a00SSimon J. Gerraty static bool
1030b0c40a00SSimon J. Gerraty JobWriteCommands(Job *job)
10313955d011SMarcel Moolenaar {
1032956e45f6SSimon J. Gerraty 	StringListNode *ln;
1033b0c40a00SSimon J. Gerraty 	bool seen = false;
1034b0c40a00SSimon J. Gerraty 	ShellWriter wr;
1035b0c40a00SSimon J. Gerraty 
1036b0c40a00SSimon J. Gerraty 	wr.f = job->cmdFILE;
1037b0c40a00SSimon J. Gerraty 	wr.xtraced = false;
1038956e45f6SSimon J. Gerraty 
103906b9b3e0SSimon J. Gerraty 	for (ln = job->node->commands.first; ln != NULL; ln = ln->next) {
1040956e45f6SSimon J. Gerraty 		const char *cmd = ln->datum;
1041956e45f6SSimon J. Gerraty 
1042956e45f6SSimon J. Gerraty 		if (strcmp(cmd, "...") == 0) {
1043956e45f6SSimon J. Gerraty 			job->node->type |= OP_SAVE_CMDS;
1044956e45f6SSimon J. Gerraty 			job->tailCmds = ln->next;
1045956e45f6SSimon J. Gerraty 			break;
1046956e45f6SSimon J. Gerraty 		}
1047e2eeea75SSimon J. Gerraty 
1048b0c40a00SSimon J. Gerraty 		JobWriteCommand(job, &wr, ln, ln->datum);
1049b0c40a00SSimon J. Gerraty 		seen = true;
1050956e45f6SSimon J. Gerraty 	}
105106b9b3e0SSimon J. Gerraty 
105206b9b3e0SSimon J. Gerraty 	return seen;
1053956e45f6SSimon J. Gerraty }
1054956e45f6SSimon J. Gerraty 
1055dba7b0efSSimon J. Gerraty /*
1056dba7b0efSSimon J. Gerraty  * Save the delayed commands (those after '...'), to be executed later in
1057dba7b0efSSimon J. Gerraty  * the '.END' node, when everything else is done.
1058dba7b0efSSimon J. Gerraty  */
1059956e45f6SSimon J. Gerraty static void
1060956e45f6SSimon J. Gerraty JobSaveCommands(Job *job)
1061956e45f6SSimon J. Gerraty {
106206b9b3e0SSimon J. Gerraty 	StringListNode *ln;
1063956e45f6SSimon J. Gerraty 
106406b9b3e0SSimon J. Gerraty 	for (ln = job->tailCmds; ln != NULL; ln = ln->next) {
106506b9b3e0SSimon J. Gerraty 		const char *cmd = ln->datum;
1066956e45f6SSimon J. Gerraty 		char *expanded_cmd;
1067dba7b0efSSimon J. Gerraty 		/*
1068dba7b0efSSimon J. Gerraty 		 * XXX: This Var_Subst is only intended to expand the dynamic
1069956e45f6SSimon J. Gerraty 		 * variables such as .TARGET, .IMPSRC.  It is not intended to
1070dba7b0efSSimon J. Gerraty 		 * expand the other variables as well; see deptgt-end.mk.
1071dba7b0efSSimon J. Gerraty 		 */
1072956e45f6SSimon J. Gerraty 		(void)Var_Subst(cmd, job->node, VARE_WANTRES, &expanded_cmd);
1073956e45f6SSimon J. Gerraty 		/* TODO: handle errors */
107406b9b3e0SSimon J. Gerraty 		Lst_Append(&Targ_GetEndNode()->commands, expanded_cmd);
1075956e45f6SSimon J. Gerraty 	}
10763955d011SMarcel Moolenaar }
10773955d011SMarcel Moolenaar 
10783955d011SMarcel Moolenaar 
1079956e45f6SSimon J. Gerraty /* Called to close both input and output pipes when a job is finished. */
10803955d011SMarcel Moolenaar static void
1081e2eeea75SSimon J. Gerraty JobClosePipes(Job *job)
10823955d011SMarcel Moolenaar {
10833955d011SMarcel Moolenaar 	clearfd(job);
10843955d011SMarcel Moolenaar 	(void)close(job->outPipe);
10853955d011SMarcel Moolenaar 	job->outPipe = -1;
10863955d011SMarcel Moolenaar 
1087b0c40a00SSimon J. Gerraty 	CollectOutput(job, true);
10883955d011SMarcel Moolenaar 	(void)close(job->inPipe);
10893955d011SMarcel Moolenaar 	job->inPipe = -1;
10903955d011SMarcel Moolenaar }
10913955d011SMarcel Moolenaar 
109206b9b3e0SSimon J. Gerraty static void
1093b0c40a00SSimon J. Gerraty DebugFailedJob(const Job *job)
1094b0c40a00SSimon J. Gerraty {
1095b0c40a00SSimon J. Gerraty 	const StringListNode *ln;
1096b0c40a00SSimon J. Gerraty 
1097b0c40a00SSimon J. Gerraty 	if (!DEBUG(ERROR))
1098b0c40a00SSimon J. Gerraty 		return;
1099b0c40a00SSimon J. Gerraty 
1100*12904384SSimon J. Gerraty 	debug_printf("\n");
1101*12904384SSimon J. Gerraty 	debug_printf("*** Failed target: %s\n", job->node->name);
1102*12904384SSimon J. Gerraty 	debug_printf("*** Failed commands:\n");
1103*12904384SSimon J. Gerraty 	for (ln = job->node->commands.first; ln != NULL; ln = ln->next) {
1104*12904384SSimon J. Gerraty 		const char *cmd = ln->datum;
1105*12904384SSimon J. Gerraty 		debug_printf("\t%s\n", cmd);
1106*12904384SSimon J. Gerraty 
1107*12904384SSimon J. Gerraty 		if (strchr(cmd, '$') != NULL) {
1108*12904384SSimon J. Gerraty 			char *xcmd;
1109*12904384SSimon J. Gerraty 			(void)Var_Subst(cmd, job->node, VARE_WANTRES, &xcmd);
1110*12904384SSimon J. Gerraty 			debug_printf("\t=> %s\n", xcmd);
1111*12904384SSimon J. Gerraty 			free(xcmd);
1112*12904384SSimon J. Gerraty 		}
1113*12904384SSimon J. Gerraty 	}
1114b0c40a00SSimon J. Gerraty }
1115b0c40a00SSimon J. Gerraty 
1116b0c40a00SSimon J. Gerraty static void
111706b9b3e0SSimon J. Gerraty JobFinishDoneExitedError(Job *job, WAIT_T *inout_status)
111806b9b3e0SSimon J. Gerraty {
111906b9b3e0SSimon J. Gerraty 	SwitchOutputTo(job->node);
112006b9b3e0SSimon J. Gerraty #ifdef USE_META
112106b9b3e0SSimon J. Gerraty 	if (useMeta) {
112206b9b3e0SSimon J. Gerraty 		meta_job_error(job, job->node,
112306b9b3e0SSimon J. Gerraty 		    job->ignerr, WEXITSTATUS(*inout_status));
112406b9b3e0SSimon J. Gerraty 	}
112506b9b3e0SSimon J. Gerraty #endif
112606b9b3e0SSimon J. Gerraty 	if (!shouldDieQuietly(job->node, -1)) {
1127b0c40a00SSimon J. Gerraty 		DebugFailedJob(job);
112806b9b3e0SSimon J. Gerraty 		(void)printf("*** [%s] Error code %d%s\n",
112906b9b3e0SSimon J. Gerraty 		    job->node->name, WEXITSTATUS(*inout_status),
113006b9b3e0SSimon J. Gerraty 		    job->ignerr ? " (ignored)" : "");
113106b9b3e0SSimon J. Gerraty 	}
113206b9b3e0SSimon J. Gerraty 
113306b9b3e0SSimon J. Gerraty 	if (job->ignerr)
113406b9b3e0SSimon J. Gerraty 		WAIT_STATUS(*inout_status) = 0;
113506b9b3e0SSimon J. Gerraty 	else {
113606b9b3e0SSimon J. Gerraty 		if (deleteOnError)
113706b9b3e0SSimon J. Gerraty 			JobDeleteTarget(job->node);
113806b9b3e0SSimon J. Gerraty 		PrintOnError(job->node, NULL);
113906b9b3e0SSimon J. Gerraty 	}
114006b9b3e0SSimon J. Gerraty }
114106b9b3e0SSimon J. Gerraty 
114206b9b3e0SSimon J. Gerraty static void
114306b9b3e0SSimon J. Gerraty JobFinishDoneExited(Job *job, WAIT_T *inout_status)
114406b9b3e0SSimon J. Gerraty {
114506b9b3e0SSimon J. Gerraty 	DEBUG2(JOB, "Process %d [%s] exited.\n", job->pid, job->node->name);
114606b9b3e0SSimon J. Gerraty 
114706b9b3e0SSimon J. Gerraty 	if (WEXITSTATUS(*inout_status) != 0)
114806b9b3e0SSimon J. Gerraty 		JobFinishDoneExitedError(job, inout_status);
114906b9b3e0SSimon J. Gerraty 	else if (DEBUG(JOB)) {
115006b9b3e0SSimon J. Gerraty 		SwitchOutputTo(job->node);
115106b9b3e0SSimon J. Gerraty 		(void)printf("*** [%s] Completed successfully\n",
115206b9b3e0SSimon J. Gerraty 		    job->node->name);
115306b9b3e0SSimon J. Gerraty 	}
115406b9b3e0SSimon J. Gerraty }
115506b9b3e0SSimon J. Gerraty 
115606b9b3e0SSimon J. Gerraty static void
115706b9b3e0SSimon J. Gerraty JobFinishDoneSignaled(Job *job, WAIT_T status)
115806b9b3e0SSimon J. Gerraty {
115906b9b3e0SSimon J. Gerraty 	SwitchOutputTo(job->node);
1160b0c40a00SSimon J. Gerraty 	DebugFailedJob(job);
116106b9b3e0SSimon J. Gerraty 	(void)printf("*** [%s] Signal %d\n", job->node->name, WTERMSIG(status));
116206b9b3e0SSimon J. Gerraty 	if (deleteOnError)
116306b9b3e0SSimon J. Gerraty 		JobDeleteTarget(job->node);
116406b9b3e0SSimon J. Gerraty }
116506b9b3e0SSimon J. Gerraty 
116606b9b3e0SSimon J. Gerraty static void
116706b9b3e0SSimon J. Gerraty JobFinishDone(Job *job, WAIT_T *inout_status)
116806b9b3e0SSimon J. Gerraty {
116906b9b3e0SSimon J. Gerraty 	if (WIFEXITED(*inout_status))
117006b9b3e0SSimon J. Gerraty 		JobFinishDoneExited(job, inout_status);
117106b9b3e0SSimon J. Gerraty 	else
117206b9b3e0SSimon J. Gerraty 		JobFinishDoneSignaled(job, *inout_status);
117306b9b3e0SSimon J. Gerraty 
117406b9b3e0SSimon J. Gerraty 	(void)fflush(stdout);
117506b9b3e0SSimon J. Gerraty }
117606b9b3e0SSimon J. Gerraty 
117706b9b3e0SSimon J. Gerraty /*
117806b9b3e0SSimon J. Gerraty  * Do final processing for the given job including updating parent nodes and
1179e2eeea75SSimon J. Gerraty  * starting new jobs as available/necessary.
1180e2eeea75SSimon J. Gerraty  *
1181e2eeea75SSimon J. Gerraty  * Deferred commands for the job are placed on the .END node.
1182e2eeea75SSimon J. Gerraty  *
118306b9b3e0SSimon J. Gerraty  * If there was a serious error (job_errors != 0; not an ignored one), no more
1184e2eeea75SSimon J. Gerraty  * jobs will be started.
11853955d011SMarcel Moolenaar  *
11863955d011SMarcel Moolenaar  * Input:
11873955d011SMarcel Moolenaar  *	job		job to finish
11883955d011SMarcel Moolenaar  *	status		sub-why job went away
11893955d011SMarcel Moolenaar  */
11903955d011SMarcel Moolenaar static void
11913955d011SMarcel Moolenaar JobFinish (Job *job, WAIT_T status)
11923955d011SMarcel Moolenaar {
1193b0c40a00SSimon J. Gerraty 	bool done, return_job_token;
11943955d011SMarcel Moolenaar 
1195956e45f6SSimon J. Gerraty 	DEBUG3(JOB, "JobFinish: %d [%s], status %d\n",
11963955d011SMarcel Moolenaar 	    job->pid, job->node->name, status);
11973955d011SMarcel Moolenaar 
11983955d011SMarcel Moolenaar 	if ((WIFEXITED(status) &&
119906b9b3e0SSimon J. Gerraty 	     ((WEXITSTATUS(status) != 0 && !job->ignerr))) ||
120006b9b3e0SSimon J. Gerraty 	    WIFSIGNALED(status)) {
120106b9b3e0SSimon J. Gerraty 		/* Finished because of an error. */
120206b9b3e0SSimon J. Gerraty 
1203e2eeea75SSimon J. Gerraty 		JobClosePipes(job);
12043955d011SMarcel Moolenaar 		if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
1205*12904384SSimon J. Gerraty 			if (fclose(job->cmdFILE) != 0)
1206*12904384SSimon J. Gerraty 				Punt("Cannot write shell script for '%s': %s",
1207*12904384SSimon J. Gerraty 				    job->node->name, strerror(errno));
12083955d011SMarcel Moolenaar 			job->cmdFILE = NULL;
12093955d011SMarcel Moolenaar 		}
1210b0c40a00SSimon J. Gerraty 		done = true;
121106b9b3e0SSimon J. Gerraty 
12123955d011SMarcel Moolenaar 	} else if (WIFEXITED(status)) {
12133955d011SMarcel Moolenaar 		/*
121406b9b3e0SSimon J. Gerraty 		 * Deal with ignored errors in -B mode. We need to print a
121506b9b3e0SSimon J. Gerraty 		 * message telling of the ignored error as well as to run
121606b9b3e0SSimon J. Gerraty 		 * the next command.
12173955d011SMarcel Moolenaar 		 */
12183955d011SMarcel Moolenaar 		done = WEXITSTATUS(status) != 0;
121906b9b3e0SSimon J. Gerraty 
1220e2eeea75SSimon J. Gerraty 		JobClosePipes(job);
122106b9b3e0SSimon J. Gerraty 
12223955d011SMarcel Moolenaar 	} else {
122306b9b3e0SSimon J. Gerraty 		/* No need to close things down or anything. */
1224b0c40a00SSimon J. Gerraty 		done = false;
12253955d011SMarcel Moolenaar 	}
12263955d011SMarcel Moolenaar 
122706b9b3e0SSimon J. Gerraty 	if (done)
122806b9b3e0SSimon J. Gerraty 		JobFinishDone(job, &status);
12293955d011SMarcel Moolenaar 
12303955d011SMarcel Moolenaar #ifdef USE_META
12313955d011SMarcel Moolenaar 	if (useMeta) {
1232e2eeea75SSimon J. Gerraty 		int meta_status = meta_job_finish(job);
1233e2eeea75SSimon J. Gerraty 		if (meta_status != 0 && status == 0)
1234e2eeea75SSimon J. Gerraty 			status = meta_status;
12353955d011SMarcel Moolenaar 	}
12363955d011SMarcel Moolenaar #endif
12373955d011SMarcel Moolenaar 
1238b0c40a00SSimon J. Gerraty 	return_job_token = false;
12393955d011SMarcel Moolenaar 
12403955d011SMarcel Moolenaar 	Trace_Log(JOBEND, job);
124106b9b3e0SSimon J. Gerraty 	if (!job->special) {
1242e2eeea75SSimon J. Gerraty 		if (WAIT_STATUS(status) != 0 ||
1243e2eeea75SSimon J. Gerraty 		    (aborting == ABORT_ERROR) || aborting == ABORT_INTERRUPT)
1244b0c40a00SSimon J. Gerraty 			return_job_token = true;
12453955d011SMarcel Moolenaar 	}
12463955d011SMarcel Moolenaar 
1247e2eeea75SSimon J. Gerraty 	if (aborting != ABORT_ERROR && aborting != ABORT_INTERRUPT &&
12483955d011SMarcel Moolenaar 	    (WAIT_STATUS(status) == 0)) {
12493955d011SMarcel Moolenaar 		/*
125006b9b3e0SSimon J. Gerraty 		 * As long as we aren't aborting and the job didn't return a
125106b9b3e0SSimon J. Gerraty 		 * non-zero status that we shouldn't ignore, we call
125206b9b3e0SSimon J. Gerraty 		 * Make_Update to update the parents.
12533955d011SMarcel Moolenaar 		 */
1254956e45f6SSimon J. Gerraty 		JobSaveCommands(job);
12553955d011SMarcel Moolenaar 		job->node->made = MADE;
125606b9b3e0SSimon J. Gerraty 		if (!job->special)
1257b0c40a00SSimon J. Gerraty 			return_job_token = true;
12583955d011SMarcel Moolenaar 		Make_Update(job->node);
1259e2eeea75SSimon J. Gerraty 		job->status = JOB_ST_FREE;
126006b9b3e0SSimon J. Gerraty 	} else if (status != 0) {
126106b9b3e0SSimon J. Gerraty 		job_errors++;
1262e2eeea75SSimon J. Gerraty 		job->status = JOB_ST_FREE;
12633955d011SMarcel Moolenaar 	}
12643955d011SMarcel Moolenaar 
126506b9b3e0SSimon J. Gerraty 	if (job_errors > 0 && !opts.keepgoing && aborting != ABORT_INTERRUPT) {
126606b9b3e0SSimon J. Gerraty 		/* Prevent more jobs from getting started. */
126706b9b3e0SSimon J. Gerraty 		aborting = ABORT_ERROR;
126806b9b3e0SSimon J. Gerraty 	}
12693955d011SMarcel Moolenaar 
12703955d011SMarcel Moolenaar 	if (return_job_token)
12713955d011SMarcel Moolenaar 		Job_TokenReturn();
12723955d011SMarcel Moolenaar 
1273e2eeea75SSimon J. Gerraty 	if (aborting == ABORT_ERROR && jobTokensRunning == 0)
127406b9b3e0SSimon J. Gerraty 		Finish(job_errors);
12753955d011SMarcel Moolenaar }
1276e2eeea75SSimon J. Gerraty 
1277e2eeea75SSimon J. Gerraty static void
1278e2eeea75SSimon J. Gerraty TouchRegular(GNode *gn)
1279e2eeea75SSimon J. Gerraty {
1280e2eeea75SSimon J. Gerraty 	const char *file = GNode_Path(gn);
1281b0c40a00SSimon J. Gerraty 	struct utimbuf times;
1282e2eeea75SSimon J. Gerraty 	int fd;
1283e2eeea75SSimon J. Gerraty 	char c;
1284e2eeea75SSimon J. Gerraty 
1285b0c40a00SSimon J. Gerraty 	times.actime = now;
1286b0c40a00SSimon J. Gerraty 	times.modtime = now;
1287e2eeea75SSimon J. Gerraty 	if (utime(file, &times) >= 0)
1288e2eeea75SSimon J. Gerraty 		return;
1289e2eeea75SSimon J. Gerraty 
1290e2eeea75SSimon J. Gerraty 	fd = open(file, O_RDWR | O_CREAT, 0666);
1291e2eeea75SSimon J. Gerraty 	if (fd < 0) {
1292e2eeea75SSimon J. Gerraty 		(void)fprintf(stderr, "*** couldn't touch %s: %s\n",
1293e2eeea75SSimon J. Gerraty 		    file, strerror(errno));
1294e2eeea75SSimon J. Gerraty 		(void)fflush(stderr);
1295e2eeea75SSimon J. Gerraty 		return;		/* XXX: What about propagating the error? */
1296e2eeea75SSimon J. Gerraty 	}
1297e2eeea75SSimon J. Gerraty 
1298e2eeea75SSimon 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
1300e2eeea75SSimon J. Gerraty 	 * as marker files. */
1301e2eeea75SSimon J. Gerraty 	if (read(fd, &c, 1) == 1) {
1302e2eeea75SSimon J. Gerraty 		(void)lseek(fd, 0, SEEK_SET);
1303e2eeea75SSimon J. Gerraty 		while (write(fd, &c, 1) == -1 && errno == EAGAIN)
1304e2eeea75SSimon J. Gerraty 			continue;
1305e2eeea75SSimon J. Gerraty 	}
1306e2eeea75SSimon J. Gerraty 	(void)close(fd);	/* XXX: What about propagating the error? */
13073955d011SMarcel Moolenaar }
13083955d011SMarcel Moolenaar 
130906b9b3e0SSimon J. Gerraty /*
131006b9b3e0SSimon J. Gerraty  * Touch the given target. Called by JobStart when the -t flag was given.
13113955d011SMarcel Moolenaar  *
1312956e45f6SSimon J. Gerraty  * The modification date of the file is changed.
131306b9b3e0SSimon J. Gerraty  * If the file did not exist, it is created.
131406b9b3e0SSimon J. Gerraty  */
13153955d011SMarcel Moolenaar void
1316b0c40a00SSimon J. Gerraty Job_Touch(GNode *gn, bool echo)
13173955d011SMarcel Moolenaar {
131806b9b3e0SSimon J. Gerraty 	if (gn->type &
131906b9b3e0SSimon J. Gerraty 	    (OP_JOIN | OP_USE | OP_USEBEFORE | OP_EXEC | OP_OPTIONAL |
13203955d011SMarcel Moolenaar 	     OP_SPECIAL | OP_PHONY)) {
132106b9b3e0SSimon J. Gerraty 		/*
132206b9b3e0SSimon J. Gerraty 		 * These are "virtual" targets and should not really be
132306b9b3e0SSimon J. Gerraty 		 * created.
132406b9b3e0SSimon J. Gerraty 		 */
13253955d011SMarcel Moolenaar 		return;
13263955d011SMarcel Moolenaar 	}
13273955d011SMarcel Moolenaar 
132806b9b3e0SSimon J. Gerraty 	if (echo || !GNode_ShouldExecute(gn)) {
13293955d011SMarcel Moolenaar 		(void)fprintf(stdout, "touch %s\n", gn->name);
13303955d011SMarcel Moolenaar 		(void)fflush(stdout);
13313955d011SMarcel Moolenaar 	}
13323955d011SMarcel Moolenaar 
1333e2eeea75SSimon J. Gerraty 	if (!GNode_ShouldExecute(gn))
13343955d011SMarcel Moolenaar 		return;
13353955d011SMarcel Moolenaar 
133606b9b3e0SSimon J. Gerraty 	if (gn->type & OP_ARCHV)
13373955d011SMarcel Moolenaar 		Arch_Touch(gn);
133806b9b3e0SSimon J. Gerraty 	else if (gn->type & OP_LIB)
13393955d011SMarcel Moolenaar 		Arch_TouchLib(gn);
134006b9b3e0SSimon J. Gerraty 	else
1341e2eeea75SSimon J. Gerraty 		TouchRegular(gn);
13423955d011SMarcel Moolenaar }
13433955d011SMarcel Moolenaar 
134406b9b3e0SSimon J. Gerraty /*
134506b9b3e0SSimon J. Gerraty  * Make sure the given node has all the commands it needs.
1346956e45f6SSimon J. Gerraty  *
1347956e45f6SSimon J. Gerraty  * The node will have commands from the .DEFAULT rule added to it if it
1348956e45f6SSimon J. Gerraty  * needs them.
13493955d011SMarcel Moolenaar  *
13503955d011SMarcel Moolenaar  * Input:
13513955d011SMarcel Moolenaar  *	gn		The target whose commands need verifying
13523955d011SMarcel Moolenaar  *	abortProc	Function to abort with message
13533955d011SMarcel Moolenaar  *
13543955d011SMarcel Moolenaar  * Results:
1355b0c40a00SSimon J. Gerraty  *	true if the commands list is/was ok.
13563955d011SMarcel Moolenaar  */
1357b0c40a00SSimon J. Gerraty bool
13583955d011SMarcel Moolenaar Job_CheckCommands(GNode *gn, void (*abortProc)(const char *, ...))
13593955d011SMarcel Moolenaar {
1360956e45f6SSimon J. Gerraty 	if (GNode_IsTarget(gn))
1361b0c40a00SSimon J. Gerraty 		return true;
136206b9b3e0SSimon J. Gerraty 	if (!Lst_IsEmpty(&gn->commands))
1363b0c40a00SSimon J. Gerraty 		return true;
136406b9b3e0SSimon J. Gerraty 	if ((gn->type & OP_LIB) && !Lst_IsEmpty(&gn->children))
1365b0c40a00SSimon J. Gerraty 		return true;
1366956e45f6SSimon J. Gerraty 
13673955d011SMarcel Moolenaar 	/*
13683955d011SMarcel Moolenaar 	 * No commands. Look for .DEFAULT rule from which we might infer
1369e2eeea75SSimon J. Gerraty 	 * commands.
13703955d011SMarcel Moolenaar 	 */
137106b9b3e0SSimon J. Gerraty 	if (defaultNode != NULL && !Lst_IsEmpty(&defaultNode->commands) &&
1372e2eeea75SSimon J. Gerraty 	    !(gn->type & OP_SPECIAL)) {
13733955d011SMarcel Moolenaar 		/*
137406b9b3e0SSimon J. Gerraty 		 * The traditional Make only looks for a .DEFAULT if the node
137506b9b3e0SSimon J. Gerraty 		 * was never the target of an operator, so that's what we do
137606b9b3e0SSimon J. Gerraty 		 * too.
1377e2eeea75SSimon J. Gerraty 		 *
1378e2eeea75SSimon J. Gerraty 		 * The .DEFAULT node acts like a transformation rule, in that
13793955d011SMarcel Moolenaar 		 * gn also inherits any attributes or sources attached to
13803955d011SMarcel Moolenaar 		 * .DEFAULT itself.
13813955d011SMarcel Moolenaar 		 */
1382e2eeea75SSimon J. Gerraty 		Make_HandleUse(defaultNode, gn);
1383dba7b0efSSimon J. Gerraty 		Var_Set(gn, IMPSRC, GNode_VarTarget(gn));
1384b0c40a00SSimon J. Gerraty 		return true;
1385956e45f6SSimon J. Gerraty 	}
1386956e45f6SSimon J. Gerraty 
1387b0c40a00SSimon J. Gerraty 	Dir_UpdateMTime(gn, false);
1388e2eeea75SSimon J. Gerraty 	if (gn->mtime != 0 || (gn->type & OP_SPECIAL))
1389b0c40a00SSimon J. Gerraty 		return true;
1390956e45f6SSimon J. Gerraty 
13913955d011SMarcel Moolenaar 	/*
1392956e45f6SSimon J. Gerraty 	 * The node wasn't the target of an operator.  We have no .DEFAULT
13933955d011SMarcel Moolenaar 	 * rule to go on and the target doesn't already exist. There's
13943955d011SMarcel Moolenaar 	 * nothing more we can do for this branch. If the -k flag wasn't
13953955d011SMarcel Moolenaar 	 * given, we stop in our tracks, otherwise we just don't update
13963955d011SMarcel Moolenaar 	 * this node's parents so they never get examined.
13973955d011SMarcel Moolenaar 	 */
13983955d011SMarcel Moolenaar 
1399*12904384SSimon J. Gerraty 	if (gn->flags.fromDepend) {
14001748de26SSimon J. Gerraty 		if (!Job_RunTarget(".STALE", gn->fname))
140106b9b3e0SSimon J. Gerraty 			fprintf(stdout,
140206b9b3e0SSimon J. Gerraty 			    "%s: %s, %d: ignoring stale %s for %s\n",
14031748de26SSimon J. Gerraty 			    progname, gn->fname, gn->lineno, makeDependfile,
14041748de26SSimon J. Gerraty 			    gn->name);
1405b0c40a00SSimon J. Gerraty 		return true;
14063955d011SMarcel Moolenaar 	}
14073955d011SMarcel Moolenaar 
14083955d011SMarcel Moolenaar 	if (gn->type & OP_OPTIONAL) {
1409956e45f6SSimon J. Gerraty 		(void)fprintf(stdout, "%s: don't know how to make %s (%s)\n",
1410956e45f6SSimon J. Gerraty 		    progname, gn->name, "ignored");
14113955d011SMarcel Moolenaar 		(void)fflush(stdout);
1412b0c40a00SSimon J. Gerraty 		return true;
14133955d011SMarcel Moolenaar 	}
14143955d011SMarcel Moolenaar 
1415956e45f6SSimon J. Gerraty 	if (opts.keepgoing) {
1416956e45f6SSimon J. Gerraty 		(void)fprintf(stdout, "%s: don't know how to make %s (%s)\n",
1417956e45f6SSimon J. Gerraty 		    progname, gn->name, "continuing");
1418956e45f6SSimon J. Gerraty 		(void)fflush(stdout);
1419b0c40a00SSimon J. Gerraty 		return false;
1420956e45f6SSimon J. Gerraty 	}
1421956e45f6SSimon J. Gerraty 
1422956e45f6SSimon J. Gerraty 	abortProc("%s: don't know how to make %s. Stop", progname, gn->name);
1423b0c40a00SSimon J. Gerraty 	return false;
1424956e45f6SSimon J. Gerraty }
1425956e45f6SSimon J. Gerraty 
142606b9b3e0SSimon J. Gerraty /*
142706b9b3e0SSimon J. Gerraty  * Execute the shell for the given job.
14283955d011SMarcel Moolenaar  *
142906b9b3e0SSimon J. Gerraty  * See Job_CatchOutput for handling the output of the shell.
143006b9b3e0SSimon J. Gerraty  */
14313955d011SMarcel Moolenaar static void
14323955d011SMarcel Moolenaar JobExec(Job *job, char **argv)
14333955d011SMarcel Moolenaar {
14343955d011SMarcel Moolenaar 	int cpid;		/* ID of new child */
14353955d011SMarcel Moolenaar 	sigset_t mask;
14363955d011SMarcel Moolenaar 
14373955d011SMarcel Moolenaar 	if (DEBUG(JOB)) {
14383955d011SMarcel Moolenaar 		int i;
14393955d011SMarcel Moolenaar 
1440e2eeea75SSimon J. Gerraty 		debug_printf("Running %s\n", job->node->name);
1441956e45f6SSimon J. Gerraty 		debug_printf("\tCommand: ");
14423955d011SMarcel Moolenaar 		for (i = 0; argv[i] != NULL; i++) {
1443956e45f6SSimon J. Gerraty 			debug_printf("%s ", argv[i]);
14443955d011SMarcel Moolenaar 		}
1445956e45f6SSimon J. Gerraty 		debug_printf("\n");
14463955d011SMarcel Moolenaar 	}
14473955d011SMarcel Moolenaar 
14483955d011SMarcel Moolenaar 	/*
14493955d011SMarcel Moolenaar 	 * Some jobs produce no output and it's disconcerting to have
14503955d011SMarcel Moolenaar 	 * no feedback of their running (since they produce no output, the
14513955d011SMarcel Moolenaar 	 * banner with their name in it never appears). This is an attempt to
14523955d011SMarcel Moolenaar 	 * provide that feedback, even if nothing follows it.
14533955d011SMarcel Moolenaar 	 */
145406b9b3e0SSimon J. Gerraty 	if (job->echo)
145506b9b3e0SSimon J. Gerraty 		SwitchOutputTo(job->node);
14563955d011SMarcel Moolenaar 
14573955d011SMarcel Moolenaar 	/* No interruptions until this job is on the `jobs' list */
14583955d011SMarcel Moolenaar 	JobSigLock(&mask);
14593955d011SMarcel Moolenaar 
14603955d011SMarcel Moolenaar 	/* Pre-emptively mark job running, pid still zero though */
1461e2eeea75SSimon J. Gerraty 	job->status = JOB_ST_RUNNING;
14623955d011SMarcel Moolenaar 
146306b9b3e0SSimon J. Gerraty 	Var_ReexportVars();
146406b9b3e0SSimon J. Gerraty 
1465dba7b0efSSimon J. Gerraty 	cpid = vfork();
14663955d011SMarcel Moolenaar 	if (cpid == -1)
14673955d011SMarcel Moolenaar 		Punt("Cannot vfork: %s", strerror(errno));
14683955d011SMarcel Moolenaar 
14693955d011SMarcel Moolenaar 	if (cpid == 0) {
14703955d011SMarcel Moolenaar 		/* Child */
14713955d011SMarcel Moolenaar 		sigset_t tmask;
14723955d011SMarcel Moolenaar 
14733955d011SMarcel Moolenaar #ifdef USE_META
14743955d011SMarcel Moolenaar 		if (useMeta) {
14753955d011SMarcel Moolenaar 			meta_job_child(job);
14763955d011SMarcel Moolenaar 		}
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
155949caa483SSimon J. Gerraty 	if (useMeta) {
156049caa483SSimon J. Gerraty 		meta_job_parent(job, cpid);
156149caa483SSimon J. Gerraty 	}
156249caa483SSimon J. Gerraty #endif
156349caa483SSimon J. Gerraty 
15643955d011SMarcel Moolenaar 	/*
15653955d011SMarcel Moolenaar 	 * Set the current position in the buffer to the beginning
15663955d011SMarcel Moolenaar 	 * and mark another stream to watch in the outputs mask
15673955d011SMarcel Moolenaar 	 */
15683955d011SMarcel Moolenaar 	job->curPos = 0;
15693955d011SMarcel Moolenaar 
15703955d011SMarcel Moolenaar 	watchfd(job);
15713955d011SMarcel Moolenaar 
15723955d011SMarcel Moolenaar 	if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
1573*12904384SSimon J. Gerraty 		if (fclose(job->cmdFILE) != 0)
1574*12904384SSimon J. Gerraty 			Punt("Cannot write shell script for '%s': %s",
1575*12904384SSimon J. Gerraty 			    job->node->name, strerror(errno));
15763955d011SMarcel Moolenaar 		job->cmdFILE = NULL;
15773955d011SMarcel Moolenaar 	}
15783955d011SMarcel Moolenaar 
1579dba7b0efSSimon J. Gerraty 	/* Now that the job is actually running, add it to the table. */
15803955d011SMarcel Moolenaar 	if (DEBUG(JOB)) {
1581956e45f6SSimon J. Gerraty 		debug_printf("JobExec(%s): pid %d added to jobs table\n",
15823955d011SMarcel Moolenaar 		    job->node->name, job->pid);
1583b0c40a00SSimon J. Gerraty 		DumpJobs("job started");
15843955d011SMarcel Moolenaar 	}
15853955d011SMarcel Moolenaar 	JobSigUnlock(&mask);
15863955d011SMarcel Moolenaar }
15873955d011SMarcel Moolenaar 
1588956e45f6SSimon J. Gerraty /* Create the argv needed to execute the shell for a given job. */
15893955d011SMarcel Moolenaar static void
15903955d011SMarcel Moolenaar JobMakeArgv(Job *job, char **argv)
15913955d011SMarcel Moolenaar {
15923955d011SMarcel Moolenaar 	int argc;
15933955d011SMarcel Moolenaar 	static char args[10];	/* For merged arguments */
15943955d011SMarcel Moolenaar 
15953955d011SMarcel Moolenaar 	argv[0] = UNCONST(shellName);
15963955d011SMarcel Moolenaar 	argc = 1;
15973955d011SMarcel Moolenaar 
159806b9b3e0SSimon J. Gerraty 	if ((shell->errFlag != NULL && shell->errFlag[0] != '-') ||
159906b9b3e0SSimon J. Gerraty 	    (shell->echoFlag != NULL && shell->echoFlag[0] != '-')) {
16003955d011SMarcel Moolenaar 		/*
160106b9b3e0SSimon J. Gerraty 		 * At least one of the flags doesn't have a minus before it,
160206b9b3e0SSimon J. Gerraty 		 * so merge them together. Have to do this because the Bourne
160306b9b3e0SSimon J. Gerraty 		 * shell thinks its second argument is a file to source.
160406b9b3e0SSimon J. Gerraty 		 * Grrrr. Note the ten-character limitation on the combined
160506b9b3e0SSimon J. Gerraty 		 * arguments.
160606b9b3e0SSimon J. Gerraty 		 *
160706b9b3e0SSimon J. Gerraty 		 * TODO: Research until when the above comments were
160806b9b3e0SSimon J. Gerraty 		 * practically relevant.
16093955d011SMarcel Moolenaar 		 */
1610e2eeea75SSimon J. Gerraty 		(void)snprintf(args, sizeof args, "-%s%s",
161106b9b3e0SSimon J. Gerraty 		    (job->ignerr ? "" :
161206b9b3e0SSimon J. Gerraty 			(shell->errFlag != NULL ? shell->errFlag : "")),
161306b9b3e0SSimon J. Gerraty 		    (!job->echo ? "" :
161406b9b3e0SSimon J. Gerraty 			(shell->echoFlag != NULL ? shell->echoFlag : "")));
16153955d011SMarcel Moolenaar 
161606b9b3e0SSimon J. Gerraty 		if (args[1] != '\0') {
16173955d011SMarcel Moolenaar 			argv[argc] = args;
16183955d011SMarcel Moolenaar 			argc++;
16193955d011SMarcel Moolenaar 		}
16203955d011SMarcel Moolenaar 	} else {
162106b9b3e0SSimon J. Gerraty 		if (!job->ignerr && shell->errFlag != NULL) {
162206b9b3e0SSimon J. Gerraty 			argv[argc] = UNCONST(shell->errFlag);
16233955d011SMarcel Moolenaar 			argc++;
16243955d011SMarcel Moolenaar 		}
162506b9b3e0SSimon J. Gerraty 		if (job->echo && shell->echoFlag != NULL) {
162606b9b3e0SSimon J. Gerraty 			argv[argc] = UNCONST(shell->echoFlag);
16273955d011SMarcel Moolenaar 			argc++;
16283955d011SMarcel Moolenaar 		}
16293955d011SMarcel Moolenaar 	}
16303955d011SMarcel Moolenaar 	argv[argc] = NULL;
16313955d011SMarcel Moolenaar }
16323955d011SMarcel Moolenaar 
163306b9b3e0SSimon J. Gerraty static void
1634b0c40a00SSimon J. Gerraty JobWriteShellCommands(Job *job, GNode *gn, bool *out_run)
16353955d011SMarcel Moolenaar {
16363955d011SMarcel Moolenaar 	/*
163706b9b3e0SSimon J. Gerraty 	 * tfile is the name of a file into which all shell commands
163806b9b3e0SSimon J. Gerraty 	 * are put. It is removed before the child shell is executed,
163906b9b3e0SSimon J. Gerraty 	 * unless DEBUG(SCRIPT) is set.
16403955d011SMarcel Moolenaar 	 */
1641dba7b0efSSimon J. Gerraty 	char tfile[MAXPATHLEN];
164206b9b3e0SSimon J. Gerraty 	int tfd;		/* File descriptor to the temp file */
164306b9b3e0SSimon J. Gerraty 
1644dba7b0efSSimon J. Gerraty 	tfd = Job_TempFile(TMPPAT, tfile, sizeof tfile);
16453955d011SMarcel Moolenaar 
16463955d011SMarcel Moolenaar 	job->cmdFILE = fdopen(tfd, "w+");
1647e2eeea75SSimon J. Gerraty 	if (job->cmdFILE == NULL)
16483955d011SMarcel Moolenaar 		Punt("Could not fdopen %s", tfile);
1649e2eeea75SSimon J. Gerraty 
1650956e45f6SSimon J. Gerraty 	(void)fcntl(fileno(job->cmdFILE), F_SETFD, FD_CLOEXEC);
16513955d011SMarcel Moolenaar 
16523955d011SMarcel Moolenaar #ifdef USE_META
16533955d011SMarcel Moolenaar 	if (useMeta) {
16543955d011SMarcel Moolenaar 		meta_job_start(job, gn);
165506b9b3e0SSimon J. Gerraty 		if (gn->type & OP_SILENT) /* might have changed */
1656b0c40a00SSimon J. Gerraty 			job->echo = false;
16573955d011SMarcel Moolenaar 	}
16583955d011SMarcel Moolenaar #endif
16593955d011SMarcel Moolenaar 
1660b0c40a00SSimon J. Gerraty 	*out_run = JobWriteCommands(job);
166106b9b3e0SSimon J. Gerraty }
166206b9b3e0SSimon J. Gerraty 
166306b9b3e0SSimon J. Gerraty /*
1664b0c40a00SSimon J. Gerraty  * Start a target-creation process going for the target described by gn.
166506b9b3e0SSimon J. Gerraty  *
166606b9b3e0SSimon J. Gerraty  * Results:
166706b9b3e0SSimon J. Gerraty  *	JOB_ERROR if there was an error in the commands, JOB_FINISHED
166806b9b3e0SSimon J. Gerraty  *	if there isn't actually anything left to do for the job and
166906b9b3e0SSimon J. Gerraty  *	JOB_RUNNING if the job has been started.
167006b9b3e0SSimon J. Gerraty  *
1671b0c40a00SSimon J. Gerraty  * Details:
167206b9b3e0SSimon J. Gerraty  *	A new Job node is created and added to the list of running
167306b9b3e0SSimon J. Gerraty  *	jobs. PMake is forked and a child shell created.
167406b9b3e0SSimon J. Gerraty  *
167506b9b3e0SSimon J. Gerraty  * NB: The return value is ignored by everyone.
167606b9b3e0SSimon J. Gerraty  */
167706b9b3e0SSimon J. Gerraty static JobStartResult
1678b0c40a00SSimon J. Gerraty JobStart(GNode *gn, bool special)
167906b9b3e0SSimon J. Gerraty {
168006b9b3e0SSimon J. Gerraty 	Job *job;		/* new job descriptor */
168106b9b3e0SSimon J. Gerraty 	char *argv[10];		/* Argument vector to shell */
1682b0c40a00SSimon J. Gerraty 	bool cmdsOK;		/* true if the nodes commands were all right */
1683b0c40a00SSimon J. Gerraty 	bool run;
168406b9b3e0SSimon J. Gerraty 
168506b9b3e0SSimon J. Gerraty 	for (job = job_table; job < job_table_end; job++) {
168606b9b3e0SSimon J. Gerraty 		if (job->status == JOB_ST_FREE)
168706b9b3e0SSimon J. Gerraty 			break;
168806b9b3e0SSimon J. Gerraty 	}
168906b9b3e0SSimon J. Gerraty 	if (job >= job_table_end)
169006b9b3e0SSimon J. Gerraty 		Punt("JobStart no job slots vacant");
169106b9b3e0SSimon J. Gerraty 
169206b9b3e0SSimon J. Gerraty 	memset(job, 0, sizeof *job);
169306b9b3e0SSimon J. Gerraty 	job->node = gn;
169406b9b3e0SSimon J. Gerraty 	job->tailCmds = NULL;
169506b9b3e0SSimon J. Gerraty 	job->status = JOB_ST_SET_UP;
169606b9b3e0SSimon J. Gerraty 
169706b9b3e0SSimon J. Gerraty 	job->special = special || gn->type & OP_SPECIAL;
169806b9b3e0SSimon J. Gerraty 	job->ignerr = opts.ignoreErrors || gn->type & OP_IGNORE;
169906b9b3e0SSimon J. Gerraty 	job->echo = !(opts.beSilent || gn->type & OP_SILENT);
170006b9b3e0SSimon J. Gerraty 
170106b9b3e0SSimon J. Gerraty 	/*
170206b9b3e0SSimon J. Gerraty 	 * Check the commands now so any attributes from .DEFAULT have a
170306b9b3e0SSimon J. Gerraty 	 * chance to migrate to the node.
170406b9b3e0SSimon J. Gerraty 	 */
170506b9b3e0SSimon J. Gerraty 	cmdsOK = Job_CheckCommands(gn, Error);
170606b9b3e0SSimon J. Gerraty 
170706b9b3e0SSimon J. Gerraty 	job->inPollfd = NULL;
1708dba7b0efSSimon J. Gerraty 
1709dba7b0efSSimon J. Gerraty 	if (Lst_IsEmpty(&gn->commands)) {
1710dba7b0efSSimon J. Gerraty 		job->cmdFILE = stdout;
1711b0c40a00SSimon J. Gerraty 		run = false;
1712b0c40a00SSimon J. Gerraty 
1713b0c40a00SSimon J. Gerraty 		/*
1714b0c40a00SSimon J. Gerraty 		 * We're serious here, but if the commands were bogus, we're
1715b0c40a00SSimon J. Gerraty 		 * also dead...
1716b0c40a00SSimon J. Gerraty 		 */
1717b0c40a00SSimon J. Gerraty 		if (!cmdsOK) {
1718b0c40a00SSimon J. Gerraty 			PrintOnError(gn, NULL); /* provide some clue */
1719b0c40a00SSimon J. Gerraty 			DieHorribly();
1720b0c40a00SSimon J. Gerraty 		}
1721dba7b0efSSimon J. Gerraty 	} else if (((gn->type & OP_MAKE) && !opts.noRecursiveExecute) ||
172206b9b3e0SSimon J. Gerraty 	    (!opts.noExecute && !opts.touchFlag)) {
1723dba7b0efSSimon J. Gerraty 		/*
1724dba7b0efSSimon J. Gerraty 		 * The above condition looks very similar to
1725dba7b0efSSimon J. Gerraty 		 * GNode_ShouldExecute but is subtly different.  It prevents
1726dba7b0efSSimon J. Gerraty 		 * that .MAKE targets are touched since these are usually
1727dba7b0efSSimon J. Gerraty 		 * virtual targets.
1728dba7b0efSSimon J. Gerraty 		 */
1729dba7b0efSSimon J. Gerraty 
1730b0c40a00SSimon J. Gerraty 		/*
1731b0c40a00SSimon J. Gerraty 		 * We're serious here, but if the commands were bogus, we're
1732b0c40a00SSimon J. Gerraty 		 * also dead...
1733b0c40a00SSimon J. Gerraty 		 */
1734b0c40a00SSimon J. Gerraty 		if (!cmdsOK) {
1735b0c40a00SSimon J. Gerraty 			PrintOnError(gn, NULL); /* provide some clue */
1736b0c40a00SSimon J. Gerraty 			DieHorribly();
1737b0c40a00SSimon J. Gerraty 		}
1738b0c40a00SSimon J. Gerraty 
1739b0c40a00SSimon J. Gerraty 		JobWriteShellCommands(job, gn, &run);
1740dba7b0efSSimon J. Gerraty 		(void)fflush(job->cmdFILE);
1741956e45f6SSimon J. Gerraty 	} else if (!GNode_ShouldExecute(gn)) {
17423955d011SMarcel Moolenaar 		/*
1743b0c40a00SSimon J. Gerraty 		 * Just write all the commands to stdout in one fell swoop.
1744dba7b0efSSimon J. Gerraty 		 * This still sets up job->tailCmds correctly.
17453955d011SMarcel Moolenaar 		 */
174606b9b3e0SSimon J. Gerraty 		SwitchOutputTo(gn);
17473955d011SMarcel Moolenaar 		job->cmdFILE = stdout;
1748956e45f6SSimon J. Gerraty 		if (cmdsOK)
1749b0c40a00SSimon J. Gerraty 			JobWriteCommands(job);
1750b0c40a00SSimon J. Gerraty 		run = false;
1751dba7b0efSSimon J. Gerraty 		(void)fflush(job->cmdFILE);
17523955d011SMarcel Moolenaar 	} else {
175306b9b3e0SSimon J. Gerraty 		Job_Touch(gn, job->echo);
1754b0c40a00SSimon J. Gerraty 		run = false;
17553955d011SMarcel Moolenaar 	}
17563955d011SMarcel Moolenaar 
175706b9b3e0SSimon J. Gerraty 	/* If we're not supposed to execute a shell, don't. */
175806b9b3e0SSimon J. Gerraty 	if (!run) {
175906b9b3e0SSimon J. Gerraty 		if (!job->special)
17603955d011SMarcel Moolenaar 			Job_TokenReturn();
176106b9b3e0SSimon J. Gerraty 		/* Unlink and close the command file if we opened one */
1762e2eeea75SSimon J. Gerraty 		if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
17633955d011SMarcel Moolenaar 			(void)fclose(job->cmdFILE);
17643955d011SMarcel Moolenaar 			job->cmdFILE = NULL;
17653955d011SMarcel Moolenaar 		}
17663955d011SMarcel Moolenaar 
17673955d011SMarcel Moolenaar 		/*
176806b9b3e0SSimon J. Gerraty 		 * We only want to work our way up the graph if we aren't
176906b9b3e0SSimon J. Gerraty 		 * here because the commands for the job were no good.
17703955d011SMarcel Moolenaar 		 */
1771956e45f6SSimon J. Gerraty 		if (cmdsOK && aborting == ABORT_NONE) {
1772956e45f6SSimon J. Gerraty 			JobSaveCommands(job);
17733955d011SMarcel Moolenaar 			job->node->made = MADE;
17743955d011SMarcel Moolenaar 			Make_Update(job->node);
17753955d011SMarcel Moolenaar 		}
1776e2eeea75SSimon J. Gerraty 		job->status = JOB_ST_FREE;
17773955d011SMarcel Moolenaar 		return cmdsOK ? JOB_FINISHED : JOB_ERROR;
17783955d011SMarcel Moolenaar 	}
17793955d011SMarcel Moolenaar 
17803955d011SMarcel Moolenaar 	/*
178106b9b3e0SSimon J. Gerraty 	 * Set up the control arguments to the shell. This is based on the
178206b9b3e0SSimon J. Gerraty 	 * flags set earlier for this job.
17833955d011SMarcel Moolenaar 	 */
17843955d011SMarcel Moolenaar 	JobMakeArgv(job, argv);
17853955d011SMarcel Moolenaar 
17863955d011SMarcel Moolenaar 	/* Create the pipe by which we'll get the shell's output. */
17873955d011SMarcel Moolenaar 	JobCreatePipe(job, 3);
17883955d011SMarcel Moolenaar 
17893955d011SMarcel Moolenaar 	JobExec(job, argv);
17903841c287SSimon J. Gerraty 	return JOB_RUNNING;
17913955d011SMarcel Moolenaar }
17923955d011SMarcel Moolenaar 
179306b9b3e0SSimon J. Gerraty /*
1794b0c40a00SSimon J. Gerraty  * If the shell has an output filter (which only csh and ksh have by default),
1795b0c40a00SSimon J. Gerraty  * print the output of the child process, skipping the noPrint text of the
1796b0c40a00SSimon J. Gerraty  * shell.
1797b0c40a00SSimon J. Gerraty  *
1798b0c40a00SSimon J. Gerraty  * Return the part of the output that the calling function needs to output by
1799b0c40a00SSimon J. Gerraty  * itself.
180006b9b3e0SSimon J. Gerraty  */
18013955d011SMarcel Moolenaar static char *
1802b0c40a00SSimon J. Gerraty PrintFilteredOutput(char *cp, char *endp)	/* XXX: should all be const */
18033955d011SMarcel Moolenaar {
180406b9b3e0SSimon J. Gerraty 	char *ecp;		/* XXX: should be const */
18053955d011SMarcel Moolenaar 
180606b9b3e0SSimon J. Gerraty 	if (shell->noPrint == NULL || shell->noPrint[0] == '\0')
1807e2eeea75SSimon J. Gerraty 		return cp;
1808e2eeea75SSimon J. Gerraty 
180906b9b3e0SSimon J. Gerraty 	/*
181006b9b3e0SSimon J. Gerraty 	 * XXX: What happens if shell->noPrint occurs on the boundary of
181106b9b3e0SSimon J. Gerraty 	 * the buffer?  To work correctly in all cases, this should rather
181206b9b3e0SSimon J. Gerraty 	 * be a proper stream filter instead of doing string matching on
181306b9b3e0SSimon J. Gerraty 	 * selected chunks of the output.
181406b9b3e0SSimon J. Gerraty 	 */
181506b9b3e0SSimon J. Gerraty 	while ((ecp = strstr(cp, shell->noPrint)) != NULL) {
1816e2eeea75SSimon J. Gerraty 		if (ecp != cp) {
181706b9b3e0SSimon J. Gerraty 			*ecp = '\0';	/* XXX: avoid writing to the buffer */
18183955d011SMarcel Moolenaar 			/*
18193955d011SMarcel Moolenaar 			 * The only way there wouldn't be a newline after
18203955d011SMarcel Moolenaar 			 * this line is if it were the last in the buffer.
182106b9b3e0SSimon J. Gerraty 			 * however, since the noPrint output comes after it,
18223955d011SMarcel Moolenaar 			 * there must be a newline, so we don't print one.
18233955d011SMarcel Moolenaar 			 */
182406b9b3e0SSimon J. Gerraty 			/* XXX: What about null bytes in the output? */
18253955d011SMarcel Moolenaar 			(void)fprintf(stdout, "%s", cp);
18263955d011SMarcel Moolenaar 			(void)fflush(stdout);
18273955d011SMarcel Moolenaar 		}
182806b9b3e0SSimon J. Gerraty 		cp = ecp + shell->noPrintLen;
182906b9b3e0SSimon J. Gerraty 		if (cp == endp)
183006b9b3e0SSimon J. Gerraty 			break;
183106b9b3e0SSimon J. Gerraty 		cp++;		/* skip over the (XXX: assumed) newline */
1832e2eeea75SSimon J. Gerraty 		pp_skip_whitespace(&cp);
18333955d011SMarcel Moolenaar 	}
18343955d011SMarcel Moolenaar 	return cp;
18353955d011SMarcel Moolenaar }
18363955d011SMarcel Moolenaar 
1837e2eeea75SSimon J. Gerraty /*
1838e2eeea75SSimon J. Gerraty  * This function is called whenever there is something to read on the pipe.
1839e2eeea75SSimon J. Gerraty  * We collect more output from the given job and store it in the job's
1840e2eeea75SSimon J. Gerraty  * outBuf. If this makes up a line, we print it tagged by the job's
1841e2eeea75SSimon J. Gerraty  * identifier, as necessary.
1842e2eeea75SSimon J. Gerraty  *
1843e2eeea75SSimon J. Gerraty  * In the output of the shell, the 'noPrint' lines are removed. If the
1844e2eeea75SSimon J. Gerraty  * command is not alone on the line (the character after it is not \0 or
1845e2eeea75SSimon J. Gerraty  * \n), we do print whatever follows it.
18463955d011SMarcel Moolenaar  *
18473955d011SMarcel Moolenaar  * Input:
18483955d011SMarcel Moolenaar  *	job		the job whose output needs printing
1849b0c40a00SSimon J. Gerraty  *	finish		true if this is the last time we'll be called
18503955d011SMarcel Moolenaar  *			for this job
18513955d011SMarcel Moolenaar  */
1852956e45f6SSimon J. Gerraty static void
1853b0c40a00SSimon J. Gerraty CollectOutput(Job *job, bool finish)
18543955d011SMarcel Moolenaar {
1855b0c40a00SSimon J. Gerraty 	bool gotNL;		/* true if got a newline */
1856b0c40a00SSimon J. Gerraty 	bool fbuf;		/* true if our buffer filled up */
1857956e45f6SSimon J. Gerraty 	size_t nr;		/* number of bytes read */
1858956e45f6SSimon J. Gerraty 	size_t i;		/* auxiliary index into outBuf */
1859956e45f6SSimon J. Gerraty 	size_t max;		/* limit for i (end of current data) */
1860956e45f6SSimon J. Gerraty 	ssize_t nRead;		/* (Temporary) number of bytes read */
18613955d011SMarcel Moolenaar 
186206b9b3e0SSimon J. Gerraty 	/* Read as many bytes as will fit in the buffer. */
1863e2eeea75SSimon J. Gerraty again:
1864b0c40a00SSimon J. Gerraty 	gotNL = false;
1865b0c40a00SSimon J. Gerraty 	fbuf = false;
18663955d011SMarcel Moolenaar 
18673955d011SMarcel Moolenaar 	nRead = read(job->inPipe, &job->outBuf[job->curPos],
18683955d011SMarcel Moolenaar 	    JOB_BUFSIZE - job->curPos);
18693955d011SMarcel Moolenaar 	if (nRead < 0) {
18703955d011SMarcel Moolenaar 		if (errno == EAGAIN)
18713955d011SMarcel Moolenaar 			return;
18723955d011SMarcel Moolenaar 		if (DEBUG(JOB)) {
1873b0c40a00SSimon J. Gerraty 			perror("CollectOutput(piperead)");
18743955d011SMarcel Moolenaar 		}
18753955d011SMarcel Moolenaar 		nr = 0;
18763955d011SMarcel Moolenaar 	} else {
1877956e45f6SSimon J. Gerraty 		nr = (size_t)nRead;
18783955d011SMarcel Moolenaar 	}
18793955d011SMarcel Moolenaar 
18803955d011SMarcel Moolenaar 	/*
18813955d011SMarcel Moolenaar 	 * If we hit the end-of-file (the job is dead), we must flush its
18823955d011SMarcel Moolenaar 	 * remaining output, so pretend we read a newline if there's any
18833955d011SMarcel Moolenaar 	 * output remaining in the buffer.
18843955d011SMarcel Moolenaar 	 * Also clear the 'finish' flag so we stop looping.
18853955d011SMarcel Moolenaar 	 */
1886e2eeea75SSimon J. Gerraty 	if (nr == 0 && job->curPos != 0) {
18873955d011SMarcel Moolenaar 		job->outBuf[job->curPos] = '\n';
18883955d011SMarcel Moolenaar 		nr = 1;
1889b0c40a00SSimon J. Gerraty 		finish = false;
18903955d011SMarcel Moolenaar 	} else if (nr == 0) {
1891b0c40a00SSimon J. Gerraty 		finish = false;
18923955d011SMarcel Moolenaar 	}
18933955d011SMarcel Moolenaar 
18943955d011SMarcel Moolenaar 	/*
18953955d011SMarcel Moolenaar 	 * Look for the last newline in the bytes we just got. If there is
18963955d011SMarcel Moolenaar 	 * one, break out of the loop with 'i' as its index and gotNL set
1897b0c40a00SSimon J. Gerraty 	 * true.
18983955d011SMarcel Moolenaar 	 */
18993955d011SMarcel Moolenaar 	max = job->curPos + nr;
190006b9b3e0SSimon J. Gerraty 	for (i = job->curPos + nr - 1;
190106b9b3e0SSimon J. Gerraty 	     i >= job->curPos && i != (size_t)-1; i--) {
19023955d011SMarcel Moolenaar 		if (job->outBuf[i] == '\n') {
1903b0c40a00SSimon J. Gerraty 			gotNL = true;
19043955d011SMarcel Moolenaar 			break;
19053955d011SMarcel Moolenaar 		} else if (job->outBuf[i] == '\0') {
19063955d011SMarcel Moolenaar 			/*
1907b0c40a00SSimon J. Gerraty 			 * FIXME: The null characters are only replaced with
1908b0c40a00SSimon J. Gerraty 			 * space _after_ the last '\n'.  Everywhere else they
1909b0c40a00SSimon J. Gerraty 			 * hide the rest of the command output.
19103955d011SMarcel Moolenaar 			 */
19113955d011SMarcel Moolenaar 			job->outBuf[i] = ' ';
19123955d011SMarcel Moolenaar 		}
19133955d011SMarcel Moolenaar 	}
19143955d011SMarcel Moolenaar 
19153955d011SMarcel Moolenaar 	if (!gotNL) {
19163955d011SMarcel Moolenaar 		job->curPos += nr;
19173955d011SMarcel Moolenaar 		if (job->curPos == JOB_BUFSIZE) {
19183955d011SMarcel Moolenaar 			/*
19193955d011SMarcel Moolenaar 			 * If we've run out of buffer space, we have no choice
19203955d011SMarcel Moolenaar 			 * but to print the stuff. sigh.
19213955d011SMarcel Moolenaar 			 */
1922b0c40a00SSimon J. Gerraty 			fbuf = true;
19233955d011SMarcel Moolenaar 			i = job->curPos;
19243955d011SMarcel Moolenaar 		}
19253955d011SMarcel Moolenaar 	}
19263955d011SMarcel Moolenaar 	if (gotNL || fbuf) {
19273955d011SMarcel Moolenaar 		/*
19283955d011SMarcel Moolenaar 		 * Need to send the output to the screen. Null terminate it
19293955d011SMarcel Moolenaar 		 * first, overwriting the newline character if there was one.
19303955d011SMarcel Moolenaar 		 * So long as the line isn't one we should filter (according
19313955d011SMarcel Moolenaar 		 * to the shell description), we print the line, preceded
19323955d011SMarcel Moolenaar 		 * by a target banner if this target isn't the same as the
19333955d011SMarcel Moolenaar 		 * one for which we last printed something.
19343955d011SMarcel Moolenaar 		 * The rest of the data in the buffer are then shifted down
19353955d011SMarcel Moolenaar 		 * to the start of the buffer and curPos is set accordingly.
19363955d011SMarcel Moolenaar 		 */
19373955d011SMarcel Moolenaar 		job->outBuf[i] = '\0';
19383955d011SMarcel Moolenaar 		if (i >= job->curPos) {
19393955d011SMarcel Moolenaar 			char *cp;
19403955d011SMarcel Moolenaar 
1941b0c40a00SSimon J. Gerraty 			/*
1942b0c40a00SSimon J. Gerraty 			 * FIXME: SwitchOutputTo should be here, according to
1943b0c40a00SSimon J. Gerraty 			 * the comment above.  But since PrintOutput does not
1944b0c40a00SSimon J. Gerraty 			 * do anything in the default shell, this bug has gone
1945b0c40a00SSimon J. Gerraty 			 * unnoticed until now.
1946b0c40a00SSimon J. Gerraty 			 */
1947b0c40a00SSimon J. Gerraty 			cp = PrintFilteredOutput(job->outBuf, &job->outBuf[i]);
19483955d011SMarcel Moolenaar 
19493955d011SMarcel Moolenaar 			/*
1950b0c40a00SSimon J. Gerraty 			 * There's still more in the output buffer. This time,
195106b9b3e0SSimon J. Gerraty 			 * though, we know there's no newline at the end, so
195206b9b3e0SSimon J. Gerraty 			 * we add one of our own free will.
19533955d011SMarcel Moolenaar 			 */
19543955d011SMarcel Moolenaar 			if (*cp != '\0') {
195506b9b3e0SSimon J. Gerraty 				if (!opts.beSilent)
195606b9b3e0SSimon J. Gerraty 					SwitchOutputTo(job->node);
19573955d011SMarcel Moolenaar #ifdef USE_META
19583955d011SMarcel Moolenaar 				if (useMeta) {
195906b9b3e0SSimon J. Gerraty 					meta_job_output(job, cp,
196006b9b3e0SSimon J. Gerraty 					    gotNL ? "\n" : "");
19613955d011SMarcel Moolenaar 				}
19623955d011SMarcel Moolenaar #endif
196306b9b3e0SSimon J. Gerraty 				(void)fprintf(stdout, "%s%s", cp,
196406b9b3e0SSimon J. Gerraty 				    gotNL ? "\n" : "");
19653955d011SMarcel Moolenaar 				(void)fflush(stdout);
19663955d011SMarcel Moolenaar 			}
19673955d011SMarcel Moolenaar 		}
19683955d011SMarcel Moolenaar 		/*
196906b9b3e0SSimon J. Gerraty 		 * max is the last offset still in the buffer. Move any
197006b9b3e0SSimon J. Gerraty 		 * remaining characters to the start of the buffer and
197106b9b3e0SSimon J. Gerraty 		 * update the end marker curPos.
19723955d011SMarcel Moolenaar 		 */
1973db29cad8SSimon J. Gerraty 		if (i < max) {
197406b9b3e0SSimon J. Gerraty 			(void)memmove(job->outBuf, &job->outBuf[i + 1],
197506b9b3e0SSimon J. Gerraty 			    max - (i + 1));
1976db29cad8SSimon J. Gerraty 			job->curPos = max - (i + 1);
1977db29cad8SSimon J. Gerraty 		} else {
1978db29cad8SSimon J. Gerraty 			assert(i == max);
19793955d011SMarcel Moolenaar 			job->curPos = 0;
19803955d011SMarcel Moolenaar 		}
19813955d011SMarcel Moolenaar 	}
19823955d011SMarcel Moolenaar 	if (finish) {
19833955d011SMarcel Moolenaar 		/*
19843955d011SMarcel Moolenaar 		 * If the finish flag is true, we must loop until we hit
19853955d011SMarcel Moolenaar 		 * end-of-file on the pipe. This is guaranteed to happen
19863955d011SMarcel Moolenaar 		 * eventually since the other end of the pipe is now closed
19873955d011SMarcel Moolenaar 		 * (we closed it explicitly and the child has exited). When
1988b0c40a00SSimon J. Gerraty 		 * we do get an EOF, finish will be set false and we'll fall
19893955d011SMarcel Moolenaar 		 * through and out.
19903955d011SMarcel Moolenaar 		 */
1991e2eeea75SSimon J. Gerraty 		goto again;
19923955d011SMarcel Moolenaar 	}
19933955d011SMarcel Moolenaar }
19943955d011SMarcel Moolenaar 
19953955d011SMarcel Moolenaar static void
19963955d011SMarcel Moolenaar JobRun(GNode *targ)
19973955d011SMarcel Moolenaar {
1998956e45f6SSimon J. Gerraty #if 0
19993955d011SMarcel Moolenaar 	/*
2000956e45f6SSimon J. Gerraty 	 * Unfortunately it is too complicated to run .BEGIN, .END, and
2001956e45f6SSimon J. Gerraty 	 * .INTERRUPT job in the parallel job module.  As of 2020-09-25,
2002956e45f6SSimon J. Gerraty 	 * unit-tests/deptgt-end-jobs.mk hangs in an endless loop.
2003956e45f6SSimon J. Gerraty 	 *
2004956e45f6SSimon J. Gerraty 	 * Running these jobs in compat mode also guarantees that these
2005956e45f6SSimon J. Gerraty 	 * jobs do not overlap with other unrelated jobs.
20063955d011SMarcel Moolenaar 	 */
2007dba7b0efSSimon J. Gerraty 	GNodeList lst = LST_INIT;
2008dba7b0efSSimon J. Gerraty 	Lst_Append(&lst, targ);
2009dba7b0efSSimon J. Gerraty 	(void)Make_Run(&lst);
2010dba7b0efSSimon J. Gerraty 	Lst_Done(&lst);
2011b0c40a00SSimon J. Gerraty 	JobStart(targ, true);
201206b9b3e0SSimon J. Gerraty 	while (jobTokensRunning != 0) {
20133955d011SMarcel Moolenaar 		Job_CatchOutput();
20143955d011SMarcel Moolenaar 	}
20153955d011SMarcel Moolenaar #else
20163955d011SMarcel Moolenaar 	Compat_Make(targ, targ);
201706b9b3e0SSimon J. Gerraty 	/* XXX: Replace with GNode_IsError(gn) */
20183955d011SMarcel Moolenaar 	if (targ->made == ERROR) {
20193955d011SMarcel Moolenaar 		PrintOnError(targ, "\n\nStop.");
20203955d011SMarcel Moolenaar 		exit(1);
20213955d011SMarcel Moolenaar 	}
20223955d011SMarcel Moolenaar #endif
20233955d011SMarcel Moolenaar }
20243955d011SMarcel Moolenaar 
202506b9b3e0SSimon J. Gerraty /*
202606b9b3e0SSimon J. Gerraty  * Handle the exit of a child. Called from Make_Make.
20273955d011SMarcel Moolenaar  *
20283955d011SMarcel Moolenaar  * The job descriptor is removed from the list of children.
20293955d011SMarcel Moolenaar  *
20303955d011SMarcel Moolenaar  * Notes:
20313955d011SMarcel Moolenaar  *	We do waits, blocking or not, according to the wisdom of our
20323955d011SMarcel Moolenaar  *	caller, until there are no more children to report. For each
20333955d011SMarcel Moolenaar  *	job, call JobFinish to finish things off.
20343955d011SMarcel Moolenaar  */
20353955d011SMarcel Moolenaar void
20363955d011SMarcel Moolenaar Job_CatchChildren(void)
20373955d011SMarcel Moolenaar {
20383955d011SMarcel Moolenaar 	int pid;		/* pid of dead child */
20393955d011SMarcel Moolenaar 	WAIT_T status;		/* Exit/termination status */
20403955d011SMarcel Moolenaar 
204106b9b3e0SSimon J. Gerraty 	/* Don't even bother if we know there's no one around. */
20423955d011SMarcel Moolenaar 	if (jobTokensRunning == 0)
20433955d011SMarcel Moolenaar 		return;
20443955d011SMarcel Moolenaar 
2045dba7b0efSSimon J. Gerraty 	/* Have we received SIGCHLD since last call? */
2046dba7b0efSSimon J. Gerraty 	if (caught_sigchld == 0)
2047dba7b0efSSimon J. Gerraty 		return;
2048dba7b0efSSimon J. Gerraty 	caught_sigchld = 0;
2049dba7b0efSSimon J. Gerraty 
20503955d011SMarcel Moolenaar 	while ((pid = waitpid((pid_t)-1, &status, WNOHANG | WUNTRACED)) > 0) {
205106b9b3e0SSimon J. Gerraty 		DEBUG2(JOB, "Process %d exited/stopped status %x.\n",
205206b9b3e0SSimon J. Gerraty 		    pid, WAIT_STATUS(status));
2053b0c40a00SSimon J. Gerraty 		JobReapChild(pid, status, true);
20543955d011SMarcel Moolenaar 	}
20553955d011SMarcel Moolenaar }
20563955d011SMarcel Moolenaar 
20573955d011SMarcel Moolenaar /*
20583955d011SMarcel Moolenaar  * It is possible that wait[pid]() was called from elsewhere,
20593955d011SMarcel Moolenaar  * this lets us reap jobs regardless.
20603955d011SMarcel Moolenaar  */
20613955d011SMarcel Moolenaar void
2062b0c40a00SSimon J. Gerraty JobReapChild(pid_t pid, WAIT_T status, bool isJobs)
20633955d011SMarcel Moolenaar {
20643955d011SMarcel Moolenaar 	Job *job;		/* job descriptor for dead child */
20653955d011SMarcel Moolenaar 
206606b9b3e0SSimon J. Gerraty 	/* Don't even bother if we know there's no one around. */
20673955d011SMarcel Moolenaar 	if (jobTokensRunning == 0)
20683955d011SMarcel Moolenaar 		return;
20693955d011SMarcel Moolenaar 
20703955d011SMarcel Moolenaar 	job = JobFindPid(pid, JOB_ST_RUNNING, isJobs);
20713955d011SMarcel Moolenaar 	if (job == NULL) {
20723955d011SMarcel Moolenaar 		if (isJobs) {
20733955d011SMarcel Moolenaar 			if (!lurking_children)
207406b9b3e0SSimon J. Gerraty 				Error("Child (%d) status %x not in table?",
207506b9b3e0SSimon J. Gerraty 				    pid, status);
20763955d011SMarcel Moolenaar 		}
20773955d011SMarcel Moolenaar 		return;		/* not ours */
20783955d011SMarcel Moolenaar 	}
20793955d011SMarcel Moolenaar 	if (WIFSTOPPED(status)) {
208006b9b3e0SSimon J. Gerraty 		DEBUG2(JOB, "Process %d (%s) stopped.\n",
208106b9b3e0SSimon J. Gerraty 		    job->pid, job->node->name);
20823955d011SMarcel Moolenaar 		if (!make_suspended) {
20833955d011SMarcel Moolenaar 			switch (WSTOPSIG(status)) {
20843955d011SMarcel Moolenaar 			case SIGTSTP:
208506b9b3e0SSimon J. Gerraty 				(void)printf("*** [%s] Suspended\n",
208606b9b3e0SSimon J. Gerraty 				    job->node->name);
20873955d011SMarcel Moolenaar 				break;
20883955d011SMarcel Moolenaar 			case SIGSTOP:
208906b9b3e0SSimon J. Gerraty 				(void)printf("*** [%s] Stopped\n",
209006b9b3e0SSimon J. Gerraty 				    job->node->name);
20913955d011SMarcel Moolenaar 				break;
20923955d011SMarcel Moolenaar 			default:
20933955d011SMarcel Moolenaar 				(void)printf("*** [%s] Stopped -- signal %d\n",
20943955d011SMarcel Moolenaar 				    job->node->name, WSTOPSIG(status));
20953955d011SMarcel Moolenaar 			}
2096b0c40a00SSimon J. Gerraty 			job->suspended = true;
20973955d011SMarcel Moolenaar 		}
20983955d011SMarcel Moolenaar 		(void)fflush(stdout);
20993955d011SMarcel Moolenaar 		return;
21003955d011SMarcel Moolenaar 	}
21013955d011SMarcel Moolenaar 
2102e2eeea75SSimon J. Gerraty 	job->status = JOB_ST_FINISHED;
21033955d011SMarcel Moolenaar 	job->exit_status = WAIT_STATUS(status);
21043955d011SMarcel Moolenaar 
21053955d011SMarcel Moolenaar 	JobFinish(job, status);
21063955d011SMarcel Moolenaar }
21073955d011SMarcel Moolenaar 
210806b9b3e0SSimon J. Gerraty /*
210906b9b3e0SSimon J. Gerraty  * Catch the output from our children, if we're using pipes do so. Otherwise
2110956e45f6SSimon J. Gerraty  * just block time until we get a signal(most likely a SIGCHLD) since there's
2111956e45f6SSimon J. Gerraty  * no point in just spinning when there's nothing to do and the reaping of a
211206b9b3e0SSimon J. Gerraty  * child can wait for a while.
211306b9b3e0SSimon J. Gerraty  */
21143955d011SMarcel Moolenaar void
21153955d011SMarcel Moolenaar Job_CatchOutput(void)
21163955d011SMarcel Moolenaar {
21173955d011SMarcel Moolenaar 	int nready;
21183955d011SMarcel Moolenaar 	Job *job;
2119956e45f6SSimon J. Gerraty 	unsigned int i;
21203955d011SMarcel Moolenaar 
21213955d011SMarcel Moolenaar 	(void)fflush(stdout);
21223955d011SMarcel Moolenaar 
21233955d011SMarcel Moolenaar 	/* The first fd in the list is the job token pipe */
21241748de26SSimon J. Gerraty 	do {
2125dba7b0efSSimon J. Gerraty 		nready = poll(fds + 1 - wantToken, fdsLen - 1 + wantToken,
212606b9b3e0SSimon J. Gerraty 		    POLL_MSEC);
21271748de26SSimon J. Gerraty 	} while (nready < 0 && errno == EINTR);
21283955d011SMarcel Moolenaar 
21291748de26SSimon J. Gerraty 	if (nready < 0)
21301748de26SSimon J. Gerraty 		Punt("poll: %s", strerror(errno));
21311748de26SSimon J. Gerraty 
21321748de26SSimon J. Gerraty 	if (nready > 0 && readyfd(&childExitJob)) {
21333955d011SMarcel Moolenaar 		char token = 0;
21341748de26SSimon J. Gerraty 		ssize_t count;
21351748de26SSimon J. Gerraty 		count = read(childExitJob.inPipe, &token, 1);
2136dba7b0efSSimon J. Gerraty 		if (count == 1) {
21373955d011SMarcel Moolenaar 			if (token == DO_JOB_RESUME[0])
213806b9b3e0SSimon J. Gerraty 				/*
213906b9b3e0SSimon J. Gerraty 				 * Complete relay requested from our SIGCONT
214006b9b3e0SSimon J. Gerraty 				 * handler
214106b9b3e0SSimon J. Gerraty 				 */
21423955d011SMarcel Moolenaar 				JobRestartJobs();
2143dba7b0efSSimon J. Gerraty 		} else if (count == 0)
2144dba7b0efSSimon J. Gerraty 			Punt("unexpected eof on token pipe");
2145*12904384SSimon J. Gerraty 		else if (errno != EAGAIN)
2146dba7b0efSSimon J. Gerraty 			Punt("token pipe read: %s", strerror(errno));
2147e2eeea75SSimon J. Gerraty 		nready--;
21483955d011SMarcel Moolenaar 	}
21493955d011SMarcel Moolenaar 
21501748de26SSimon J. Gerraty 	Job_CatchChildren();
21511748de26SSimon J. Gerraty 	if (nready == 0)
21523955d011SMarcel Moolenaar 		return;
21533955d011SMarcel Moolenaar 
2154dba7b0efSSimon J. Gerraty 	for (i = npseudojobs * nfds_per_job(); i < fdsLen; i++) {
215506b9b3e0SSimon J. Gerraty 		if (fds[i].revents == 0)
21563955d011SMarcel Moolenaar 			continue;
2157dba7b0efSSimon J. Gerraty 		job = jobByFdIndex[i];
2158e2eeea75SSimon J. Gerraty 		if (job->status == JOB_ST_RUNNING)
2159b0c40a00SSimon J. Gerraty 			CollectOutput(job, false);
216049caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
216149caa483SSimon J. Gerraty 		/*
216249caa483SSimon J. Gerraty 		 * With meta mode, we may have activity on the job's filemon
216306b9b3e0SSimon J. Gerraty 		 * descriptor too, which at the moment is any pollfd other
216406b9b3e0SSimon J. Gerraty 		 * than job->inPollfd.
216549caa483SSimon J. Gerraty 		 */
216649caa483SSimon J. Gerraty 		if (useMeta && job->inPollfd != &fds[i]) {
216749caa483SSimon J. Gerraty 			if (meta_job_event(job) <= 0) {
216849caa483SSimon J. Gerraty 				fds[i].events = 0; /* never mind */
216949caa483SSimon J. Gerraty 			}
217049caa483SSimon J. Gerraty 		}
217149caa483SSimon J. Gerraty #endif
21721748de26SSimon J. Gerraty 		if (--nready == 0)
21731748de26SSimon J. Gerraty 			return;
21743955d011SMarcel Moolenaar 	}
21753955d011SMarcel Moolenaar }
21763955d011SMarcel Moolenaar 
217706b9b3e0SSimon J. Gerraty /*
217806b9b3e0SSimon J. Gerraty  * Start the creation of a target. Basically a front-end for JobStart used by
217906b9b3e0SSimon J. Gerraty  * the Make module.
218006b9b3e0SSimon J. Gerraty  */
21813955d011SMarcel Moolenaar void
21823955d011SMarcel Moolenaar Job_Make(GNode *gn)
21833955d011SMarcel Moolenaar {
2184b0c40a00SSimon J. Gerraty 	(void)JobStart(gn, false);
218506b9b3e0SSimon J. Gerraty }
218606b9b3e0SSimon J. Gerraty 
218706b9b3e0SSimon J. Gerraty static void
218806b9b3e0SSimon J. Gerraty InitShellNameAndPath(void)
218906b9b3e0SSimon J. Gerraty {
219006b9b3e0SSimon J. Gerraty 	shellName = shell->name;
219106b9b3e0SSimon J. Gerraty 
219206b9b3e0SSimon J. Gerraty #ifdef DEFSHELL_CUSTOM
219306b9b3e0SSimon J. Gerraty 	if (shellName[0] == '/') {
219406b9b3e0SSimon J. Gerraty 		shellPath = shellName;
219506b9b3e0SSimon J. Gerraty 		shellName = str_basename(shellPath);
219606b9b3e0SSimon J. Gerraty 		return;
219706b9b3e0SSimon J. Gerraty 	}
219806b9b3e0SSimon J. Gerraty #endif
2199*12904384SSimon J. Gerraty #ifdef DEFSHELL_PATH
2200*12904384SSimon J. Gerraty 	shellPath = DEFSHELL_PATH;
2201*12904384SSimon J. Gerraty #else
220206b9b3e0SSimon J. Gerraty 	shellPath = str_concat3(_PATH_DEFSHELLDIR, "/", shellName);
2203*12904384SSimon J. Gerraty #endif
22043955d011SMarcel Moolenaar }
22053955d011SMarcel Moolenaar 
22063955d011SMarcel Moolenaar void
22073955d011SMarcel Moolenaar Shell_Init(void)
22083955d011SMarcel Moolenaar {
220906b9b3e0SSimon J. Gerraty 	if (shellPath == NULL)
221006b9b3e0SSimon J. Gerraty 		InitShellNameAndPath();
221106b9b3e0SSimon J. Gerraty 
2212dba7b0efSSimon J. Gerraty 	Var_SetWithFlags(SCOPE_CMDLINE, ".SHELL", shellPath, VAR_SET_READONLY);
221306b9b3e0SSimon J. Gerraty 	if (shell->errFlag == NULL)
221406b9b3e0SSimon J. Gerraty 		shell->errFlag = "";
221506b9b3e0SSimon J. Gerraty 	if (shell->echoFlag == NULL)
221606b9b3e0SSimon J. Gerraty 		shell->echoFlag = "";
221706b9b3e0SSimon J. Gerraty 	if (shell->hasErrCtl && shell->errFlag[0] != '\0') {
221806b9b3e0SSimon J. Gerraty 		if (shellErrFlag != NULL &&
221906b9b3e0SSimon J. Gerraty 		    strcmp(shell->errFlag, &shellErrFlag[1]) != 0) {
222051ee2c1cSSimon J. Gerraty 			free(shellErrFlag);
222151ee2c1cSSimon J. Gerraty 			shellErrFlag = NULL;
222251ee2c1cSSimon J. Gerraty 		}
222306b9b3e0SSimon J. Gerraty 		if (shellErrFlag == NULL) {
222406b9b3e0SSimon J. Gerraty 			size_t n = strlen(shell->errFlag) + 2;
222551ee2c1cSSimon J. Gerraty 
222651ee2c1cSSimon J. Gerraty 			shellErrFlag = bmake_malloc(n);
222706b9b3e0SSimon J. Gerraty 			if (shellErrFlag != NULL)
222806b9b3e0SSimon J. Gerraty 				snprintf(shellErrFlag, n, "-%s",
222906b9b3e0SSimon J. Gerraty 				    shell->errFlag);
223051ee2c1cSSimon J. Gerraty 		}
223106b9b3e0SSimon J. Gerraty 	} else if (shellErrFlag != NULL) {
223251ee2c1cSSimon J. Gerraty 		free(shellErrFlag);
223351ee2c1cSSimon J. Gerraty 		shellErrFlag = NULL;
223451ee2c1cSSimon J. Gerraty 	}
22353955d011SMarcel Moolenaar }
22363955d011SMarcel Moolenaar 
223706b9b3e0SSimon J. Gerraty /*
223806b9b3e0SSimon J. Gerraty  * Return the string literal that is used in the current command shell
223906b9b3e0SSimon J. Gerraty  * to produce a newline character.
224006b9b3e0SSimon J. Gerraty  */
22413955d011SMarcel Moolenaar const char *
22423955d011SMarcel Moolenaar Shell_GetNewline(void)
22433955d011SMarcel Moolenaar {
224406b9b3e0SSimon J. Gerraty 	return shell->newline;
22453955d011SMarcel Moolenaar }
22463955d011SMarcel Moolenaar 
22473955d011SMarcel Moolenaar void
22483955d011SMarcel Moolenaar Job_SetPrefix(void)
22493955d011SMarcel Moolenaar {
225006b9b3e0SSimon J. Gerraty 	if (targPrefix != NULL) {
22513955d011SMarcel Moolenaar 		free(targPrefix);
2252dba7b0efSSimon J. Gerraty 	} else if (!Var_Exists(SCOPE_GLOBAL, MAKE_JOB_PREFIX)) {
2253dba7b0efSSimon J. Gerraty 		Global_Set(MAKE_JOB_PREFIX, "---");
22543955d011SMarcel Moolenaar 	}
22553955d011SMarcel Moolenaar 
2256956e45f6SSimon J. Gerraty 	(void)Var_Subst("${" MAKE_JOB_PREFIX "}",
2257dba7b0efSSimon J. Gerraty 	    SCOPE_GLOBAL, VARE_WANTRES, &targPrefix);
2258956e45f6SSimon J. Gerraty 	/* TODO: handle errors */
22593955d011SMarcel Moolenaar }
22603955d011SMarcel Moolenaar 
226106b9b3e0SSimon J. Gerraty static void
226206b9b3e0SSimon J. Gerraty AddSig(int sig, SignalProc handler)
226306b9b3e0SSimon J. Gerraty {
226406b9b3e0SSimon J. Gerraty 	if (bmake_signal(sig, SIG_IGN) != SIG_IGN) {
226506b9b3e0SSimon J. Gerraty 		sigaddset(&caught_signals, sig);
226606b9b3e0SSimon J. Gerraty 		(void)bmake_signal(sig, handler);
226706b9b3e0SSimon J. Gerraty 	}
226806b9b3e0SSimon J. Gerraty }
226906b9b3e0SSimon J. Gerraty 
22702c3632d1SSimon J. Gerraty /* Initialize the process module. */
22713955d011SMarcel Moolenaar void
22723955d011SMarcel Moolenaar Job_Init(void)
22733955d011SMarcel Moolenaar {
2274d191243dSSimon J. Gerraty 	Job_SetPrefix();
22753955d011SMarcel Moolenaar 	/* Allocate space for all the job info */
2276956e45f6SSimon J. Gerraty 	job_table = bmake_malloc((size_t)opts.maxJobs * sizeof *job_table);
2277956e45f6SSimon J. Gerraty 	memset(job_table, 0, (size_t)opts.maxJobs * sizeof *job_table);
2278956e45f6SSimon J. Gerraty 	job_table_end = job_table + opts.maxJobs;
22793955d011SMarcel Moolenaar 	wantToken = 0;
2280dba7b0efSSimon J. Gerraty 	caught_sigchld = 0;
22813955d011SMarcel Moolenaar 
2282956e45f6SSimon J. Gerraty 	aborting = ABORT_NONE;
228306b9b3e0SSimon J. Gerraty 	job_errors = 0;
22843955d011SMarcel Moolenaar 
228568c4481aSSimon J. Gerraty 	Always_pass_job_queue = GetBooleanExpr(MAKE_ALWAYS_PASS_JOB_QUEUE,
22869a4bc556SSimon J. Gerraty 	    Always_pass_job_queue);
22879a4bc556SSimon J. Gerraty 
228868c4481aSSimon J. Gerraty 	Job_error_token = GetBooleanExpr(MAKE_JOB_ERROR_TOKEN, Job_error_token);
228968c4481aSSimon J. Gerraty 
22902d395cb5SSimon J. Gerraty 
22913955d011SMarcel Moolenaar 	/*
22923955d011SMarcel Moolenaar 	 * There is a non-zero chance that we already have children.
22933955d011SMarcel Moolenaar 	 * eg after 'make -f- <<EOF'
229406b9b3e0SSimon J. Gerraty 	 * Since their termination causes a 'Child (pid) not in table'
229506b9b3e0SSimon J. Gerraty 	 * message, Collect the status of any that are already dead, and
229606b9b3e0SSimon J. Gerraty 	 * suppress the error message if there are any undead ones.
22973955d011SMarcel Moolenaar 	 */
22983955d011SMarcel Moolenaar 	for (;;) {
229906b9b3e0SSimon J. Gerraty 		int rval;
230006b9b3e0SSimon J. Gerraty 		WAIT_T status;
230106b9b3e0SSimon J. Gerraty 
23023955d011SMarcel Moolenaar 		rval = waitpid((pid_t)-1, &status, WNOHANG);
23033955d011SMarcel Moolenaar 		if (rval > 0)
23043955d011SMarcel Moolenaar 			continue;
23053955d011SMarcel Moolenaar 		if (rval == 0)
2306b0c40a00SSimon J. Gerraty 			lurking_children = true;
23073955d011SMarcel Moolenaar 		break;
23083955d011SMarcel Moolenaar 	}
23093955d011SMarcel Moolenaar 
23103955d011SMarcel Moolenaar 	Shell_Init();
23113955d011SMarcel Moolenaar 
23123955d011SMarcel Moolenaar 	JobCreatePipe(&childExitJob, 3);
23133955d011SMarcel Moolenaar 
2314dba7b0efSSimon J. Gerraty 	{
231549caa483SSimon J. Gerraty 		/* Preallocate enough for the maximum number of jobs. */
2316dba7b0efSSimon J. Gerraty 		size_t nfds = (npseudojobs + (size_t)opts.maxJobs) *
2317dba7b0efSSimon J. Gerraty 			      nfds_per_job();
2318dba7b0efSSimon J. Gerraty 		fds = bmake_malloc(sizeof *fds * nfds);
2319dba7b0efSSimon J. Gerraty 		jobByFdIndex = bmake_malloc(sizeof *jobByFdIndex * nfds);
2320dba7b0efSSimon J. Gerraty 	}
23213955d011SMarcel Moolenaar 
23223955d011SMarcel Moolenaar 	/* These are permanent entries and take slots 0 and 1 */
23233955d011SMarcel Moolenaar 	watchfd(&tokenWaitJob);
23243955d011SMarcel Moolenaar 	watchfd(&childExitJob);
23253955d011SMarcel Moolenaar 
23263955d011SMarcel Moolenaar 	sigemptyset(&caught_signals);
23273955d011SMarcel Moolenaar 	/*
23283955d011SMarcel Moolenaar 	 * Install a SIGCHLD handler.
23293955d011SMarcel Moolenaar 	 */
23303955d011SMarcel Moolenaar 	(void)bmake_signal(SIGCHLD, JobChildSig);
23313955d011SMarcel Moolenaar 	sigaddset(&caught_signals, SIGCHLD);
23323955d011SMarcel Moolenaar 
23333955d011SMarcel Moolenaar 	/*
23343955d011SMarcel Moolenaar 	 * Catch the four signals that POSIX specifies if they aren't ignored.
23353955d011SMarcel Moolenaar 	 * JobPassSig will take care of calling JobInterrupt if appropriate.
23363955d011SMarcel Moolenaar 	 */
233706b9b3e0SSimon J. Gerraty 	AddSig(SIGINT, JobPassSig_int);
233806b9b3e0SSimon J. Gerraty 	AddSig(SIGHUP, JobPassSig_term);
233906b9b3e0SSimon J. Gerraty 	AddSig(SIGTERM, JobPassSig_term);
234006b9b3e0SSimon J. Gerraty 	AddSig(SIGQUIT, JobPassSig_term);
23413955d011SMarcel Moolenaar 
23423955d011SMarcel Moolenaar 	/*
23433955d011SMarcel Moolenaar 	 * There are additional signals that need to be caught and passed if
23443955d011SMarcel Moolenaar 	 * either the export system wants to be told directly of signals or if
23453955d011SMarcel Moolenaar 	 * we're giving each job its own process group (since then it won't get
23463955d011SMarcel Moolenaar 	 * signals from the terminal driver as we own the terminal)
23473955d011SMarcel Moolenaar 	 */
234806b9b3e0SSimon J. Gerraty 	AddSig(SIGTSTP, JobPassSig_suspend);
234906b9b3e0SSimon J. Gerraty 	AddSig(SIGTTOU, JobPassSig_suspend);
235006b9b3e0SSimon J. Gerraty 	AddSig(SIGTTIN, JobPassSig_suspend);
235106b9b3e0SSimon J. Gerraty 	AddSig(SIGWINCH, JobCondPassSig);
235206b9b3e0SSimon J. Gerraty 	AddSig(SIGCONT, JobContinueSig);
23533955d011SMarcel Moolenaar 
23541748de26SSimon J. Gerraty 	(void)Job_RunTarget(".BEGIN", NULL);
2355956e45f6SSimon J. Gerraty 	/* Create the .END node now, even though no code in the unit tests
2356956e45f6SSimon J. Gerraty 	 * depends on it.  See also Targ_GetEndNode in Compat_Run. */
2357956e45f6SSimon J. Gerraty 	(void)Targ_GetEndNode();
23583955d011SMarcel Moolenaar }
23593955d011SMarcel Moolenaar 
236006b9b3e0SSimon J. Gerraty static void
236106b9b3e0SSimon J. Gerraty DelSig(int sig)
23623955d011SMarcel Moolenaar {
236306b9b3e0SSimon J. Gerraty 	if (sigismember(&caught_signals, sig) != 0)
236406b9b3e0SSimon J. Gerraty 		(void)bmake_signal(sig, SIG_DFL);
23653955d011SMarcel Moolenaar }
23663955d011SMarcel Moolenaar 
236706b9b3e0SSimon J. Gerraty static void
236806b9b3e0SSimon J. Gerraty JobSigReset(void)
236906b9b3e0SSimon J. Gerraty {
237006b9b3e0SSimon J. Gerraty 	DelSig(SIGINT);
237106b9b3e0SSimon J. Gerraty 	DelSig(SIGHUP);
237206b9b3e0SSimon J. Gerraty 	DelSig(SIGQUIT);
237306b9b3e0SSimon J. Gerraty 	DelSig(SIGTERM);
237406b9b3e0SSimon J. Gerraty 	DelSig(SIGTSTP);
237506b9b3e0SSimon J. Gerraty 	DelSig(SIGTTOU);
237606b9b3e0SSimon J. Gerraty 	DelSig(SIGTTIN);
237706b9b3e0SSimon J. Gerraty 	DelSig(SIGWINCH);
237806b9b3e0SSimon J. Gerraty 	DelSig(SIGCONT);
23793955d011SMarcel Moolenaar 	(void)bmake_signal(SIGCHLD, SIG_DFL);
23803955d011SMarcel Moolenaar }
23813955d011SMarcel Moolenaar 
23822c3632d1SSimon J. Gerraty /* Find a shell in 'shells' given its name, or return NULL. */
23833955d011SMarcel Moolenaar static Shell *
2384956e45f6SSimon J. Gerraty FindShellByName(const char *name)
23853955d011SMarcel Moolenaar {
2386956e45f6SSimon J. Gerraty 	Shell *sh = shells;
2387956e45f6SSimon J. Gerraty 	const Shell *shellsEnd = sh + sizeof shells / sizeof shells[0];
23883955d011SMarcel Moolenaar 
2389956e45f6SSimon J. Gerraty 	for (sh = shells; sh < shellsEnd; sh++) {
23903955d011SMarcel Moolenaar 		if (strcmp(name, sh->name) == 0)
23913841c287SSimon J. Gerraty 			return sh;
23923955d011SMarcel Moolenaar 	}
23933955d011SMarcel Moolenaar 	return NULL;
23943955d011SMarcel Moolenaar }
23953955d011SMarcel Moolenaar 
239606b9b3e0SSimon J. Gerraty /*
239706b9b3e0SSimon J. Gerraty  * Parse a shell specification and set up 'shell', shellPath and
239806b9b3e0SSimon J. Gerraty  * shellName appropriately.
23993955d011SMarcel Moolenaar  *
24003955d011SMarcel Moolenaar  * Input:
24013955d011SMarcel Moolenaar  *	line		The shell spec
24023955d011SMarcel Moolenaar  *
24033955d011SMarcel Moolenaar  * Results:
2404b0c40a00SSimon J. Gerraty  *	false if the specification was incorrect.
24053955d011SMarcel Moolenaar  *
24063955d011SMarcel Moolenaar  * Side Effects:
240706b9b3e0SSimon J. Gerraty  *	'shell' points to a Shell structure (either predefined or
24083955d011SMarcel Moolenaar  *	created from the shell spec), shellPath is the full path of the
240906b9b3e0SSimon J. Gerraty  *	shell described by 'shell', while shellName is just the
24103955d011SMarcel Moolenaar  *	final component of shellPath.
24113955d011SMarcel Moolenaar  *
24123955d011SMarcel Moolenaar  * Notes:
24133955d011SMarcel Moolenaar  *	A shell specification consists of a .SHELL target, with dependency
24143955d011SMarcel Moolenaar  *	operator, followed by a series of blank-separated words. Double
24153955d011SMarcel Moolenaar  *	quotes can be used to use blanks in words. A backslash escapes
24163955d011SMarcel Moolenaar  *	anything (most notably a double-quote and a space) and
24173955d011SMarcel Moolenaar  *	provides the functionality it does in C. Each word consists of
24183955d011SMarcel Moolenaar  *	keyword and value separated by an equal sign. There should be no
24193955d011SMarcel Moolenaar  *	unnecessary spaces in the word. The keywords are as follows:
24203955d011SMarcel Moolenaar  *	    name	Name of shell.
24213955d011SMarcel Moolenaar  *	    path	Location of shell.
24223955d011SMarcel Moolenaar  *	    quiet	Command to turn off echoing.
24233955d011SMarcel Moolenaar  *	    echo	Command to turn echoing on
24243955d011SMarcel Moolenaar  *	    filter	Result of turning off echoing that shouldn't be
24253955d011SMarcel Moolenaar  *			printed.
24263955d011SMarcel Moolenaar  *	    echoFlag	Flag to turn echoing on at the start
24273955d011SMarcel Moolenaar  *	    errFlag	Flag to turn error checking on at the start
24283955d011SMarcel Moolenaar  *	    hasErrCtl	True if shell has error checking control
24293955d011SMarcel Moolenaar  *	    newline	String literal to represent a newline char
24303955d011SMarcel Moolenaar  *	    check	Command to turn on error checking if hasErrCtl
2431b0c40a00SSimon J. Gerraty  *			is true or template of command to echo a command
24323955d011SMarcel Moolenaar  *			for which error checking is off if hasErrCtl is
2433b0c40a00SSimon J. Gerraty  *			false.
24343955d011SMarcel Moolenaar  *	    ignore	Command to turn off error checking if hasErrCtl
2435b0c40a00SSimon J. Gerraty  *			is true or template of command to execute a
24363955d011SMarcel Moolenaar  *			command so as to ignore any errors it returns if
2437b0c40a00SSimon J. Gerraty  *			hasErrCtl is false.
24383955d011SMarcel Moolenaar  */
2439b0c40a00SSimon J. Gerraty bool
24403955d011SMarcel Moolenaar Job_ParseShell(char *line)
24413955d011SMarcel Moolenaar {
24422c3632d1SSimon J. Gerraty 	Words wordsList;
24433955d011SMarcel Moolenaar 	char **words;
24443955d011SMarcel Moolenaar 	char **argv;
24452c3632d1SSimon J. Gerraty 	size_t argc;
24463955d011SMarcel Moolenaar 	char *path;
24473955d011SMarcel Moolenaar 	Shell newShell;
2448b0c40a00SSimon J. Gerraty 	bool fullSpec = false;
24493955d011SMarcel Moolenaar 	Shell *sh;
24503955d011SMarcel Moolenaar 
245106b9b3e0SSimon J. Gerraty 	/* XXX: don't use line as an iterator variable */
2452956e45f6SSimon J. Gerraty 	pp_skip_whitespace(&line);
24533955d011SMarcel Moolenaar 
2454dba7b0efSSimon J. Gerraty 	free(shell_freeIt);
24553955d011SMarcel Moolenaar 
2456e2eeea75SSimon J. Gerraty 	memset(&newShell, 0, sizeof newShell);
24573955d011SMarcel Moolenaar 
24583955d011SMarcel Moolenaar 	/*
24593955d011SMarcel Moolenaar 	 * Parse the specification by keyword
24603955d011SMarcel Moolenaar 	 */
2461b0c40a00SSimon J. Gerraty 	wordsList = Str_Words(line, true);
24622c3632d1SSimon J. Gerraty 	words = wordsList.words;
24632c3632d1SSimon J. Gerraty 	argc = wordsList.len;
24642c3632d1SSimon J. Gerraty 	path = wordsList.freeIt;
24653955d011SMarcel Moolenaar 	if (words == NULL) {
24663955d011SMarcel Moolenaar 		Error("Unterminated quoted string [%s]", line);
2467b0c40a00SSimon J. Gerraty 		return false;
24683955d011SMarcel Moolenaar 	}
2469dba7b0efSSimon J. Gerraty 	shell_freeIt = path;
24703955d011SMarcel Moolenaar 
24713955d011SMarcel Moolenaar 	for (path = NULL, argv = words; argc != 0; argc--, argv++) {
2472956e45f6SSimon J. Gerraty 		char *arg = *argv;
2473956e45f6SSimon J. Gerraty 		if (strncmp(arg, "path=", 5) == 0) {
2474956e45f6SSimon J. Gerraty 			path = arg + 5;
2475956e45f6SSimon J. Gerraty 		} else if (strncmp(arg, "name=", 5) == 0) {
2476956e45f6SSimon J. Gerraty 			newShell.name = arg + 5;
24773955d011SMarcel Moolenaar 		} else {
2478956e45f6SSimon J. Gerraty 			if (strncmp(arg, "quiet=", 6) == 0) {
2479956e45f6SSimon J. Gerraty 				newShell.echoOff = arg + 6;
2480956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "echo=", 5) == 0) {
2481956e45f6SSimon J. Gerraty 				newShell.echoOn = arg + 5;
2482956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "filter=", 7) == 0) {
2483956e45f6SSimon J. Gerraty 				newShell.noPrint = arg + 7;
2484956e45f6SSimon J. Gerraty 				newShell.noPrintLen = strlen(newShell.noPrint);
2485956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "echoFlag=", 9) == 0) {
248606b9b3e0SSimon J. Gerraty 				newShell.echoFlag = arg + 9;
2487956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "errFlag=", 8) == 0) {
248806b9b3e0SSimon J. Gerraty 				newShell.errFlag = arg + 8;
2489956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "hasErrCtl=", 10) == 0) {
2490956e45f6SSimon J. Gerraty 				char c = arg[10];
2491956e45f6SSimon J. Gerraty 				newShell.hasErrCtl = c == 'Y' || c == 'y' ||
2492956e45f6SSimon J. Gerraty 						     c == 'T' || c == 't';
2493956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "newline=", 8) == 0) {
2494956e45f6SSimon J. Gerraty 				newShell.newline = arg + 8;
2495956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "check=", 6) == 0) {
249606b9b3e0SSimon J. Gerraty 				/* Before 2020-12-10, these two variables
249706b9b3e0SSimon J. Gerraty 				 * had been a single variable. */
249806b9b3e0SSimon J. Gerraty 				newShell.errOn = arg + 6;
249906b9b3e0SSimon J. Gerraty 				newShell.echoTmpl = arg + 6;
2500956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "ignore=", 7) == 0) {
250106b9b3e0SSimon J. Gerraty 				/* Before 2020-12-10, these two variables
250206b9b3e0SSimon J. Gerraty 				 * had been a single variable. */
250306b9b3e0SSimon J. Gerraty 				newShell.errOff = arg + 7;
250406b9b3e0SSimon J. Gerraty 				newShell.runIgnTmpl = arg + 7;
2505956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "errout=", 7) == 0) {
250606b9b3e0SSimon J. Gerraty 				newShell.runChkTmpl = arg + 7;
2507956e45f6SSimon J. Gerraty 			} else if (strncmp(arg, "comment=", 8) == 0) {
2508956e45f6SSimon J. Gerraty 				newShell.commentChar = arg[8];
25093955d011SMarcel Moolenaar 			} else {
251006b9b3e0SSimon J. Gerraty 				Parse_Error(PARSE_FATAL,
251106b9b3e0SSimon J. Gerraty 				    "Unknown keyword \"%s\"", arg);
25123955d011SMarcel Moolenaar 				free(words);
2513b0c40a00SSimon J. Gerraty 				return false;
25143955d011SMarcel Moolenaar 			}
2515b0c40a00SSimon J. Gerraty 			fullSpec = true;
25163955d011SMarcel Moolenaar 		}
25173955d011SMarcel Moolenaar 	}
25183955d011SMarcel Moolenaar 
25193955d011SMarcel Moolenaar 	if (path == NULL) {
25203955d011SMarcel Moolenaar 		/*
252106b9b3e0SSimon J. Gerraty 		 * If no path was given, the user wants one of the
252206b9b3e0SSimon J. Gerraty 		 * pre-defined shells, yes? So we find the one s/he wants
252306b9b3e0SSimon J. Gerraty 		 * with the help of FindShellByName and set things up the
252406b9b3e0SSimon J. Gerraty 		 * right way. shellPath will be set up by Shell_Init.
25253955d011SMarcel Moolenaar 		 */
25263955d011SMarcel Moolenaar 		if (newShell.name == NULL) {
252706b9b3e0SSimon J. Gerraty 			Parse_Error(PARSE_FATAL,
252806b9b3e0SSimon J. Gerraty 			    "Neither path nor name specified");
25293955d011SMarcel Moolenaar 			free(words);
2530b0c40a00SSimon J. Gerraty 			return false;
25313955d011SMarcel Moolenaar 		} else {
2532956e45f6SSimon J. Gerraty 			if ((sh = FindShellByName(newShell.name)) == NULL) {
253306b9b3e0SSimon J. Gerraty 				Parse_Error(PARSE_WARNING,
253406b9b3e0SSimon J. Gerraty 				    "%s: No matching shell", newShell.name);
25353955d011SMarcel Moolenaar 				free(words);
2536b0c40a00SSimon J. Gerraty 				return false;
25373955d011SMarcel Moolenaar 			}
253806b9b3e0SSimon J. Gerraty 			shell = sh;
25393955d011SMarcel Moolenaar 			shellName = newShell.name;
254006b9b3e0SSimon J. Gerraty 			if (shellPath != NULL) {
254106b9b3e0SSimon J. Gerraty 				/*
254206b9b3e0SSimon J. Gerraty 				 * Shell_Init has already been called!
254306b9b3e0SSimon J. Gerraty 				 * Do it again.
254406b9b3e0SSimon J. Gerraty 				 */
25453955d011SMarcel Moolenaar 				free(UNCONST(shellPath));
25463955d011SMarcel Moolenaar 				shellPath = NULL;
25473955d011SMarcel Moolenaar 				Shell_Init();
25483955d011SMarcel Moolenaar 			}
25493955d011SMarcel Moolenaar 		}
25503955d011SMarcel Moolenaar 	} else {
25513955d011SMarcel Moolenaar 		/*
255206b9b3e0SSimon J. Gerraty 		 * The user provided a path. If s/he gave nothing else
2553b0c40a00SSimon J. Gerraty 		 * (fullSpec is false), try and find a matching shell in the
255406b9b3e0SSimon J. Gerraty 		 * ones we know of. Else we just take the specification at
255506b9b3e0SSimon J. Gerraty 		 * its word and copy it to a new location. In either case,
255606b9b3e0SSimon J. Gerraty 		 * we need to record the path the user gave for the shell.
25573955d011SMarcel Moolenaar 		 */
25583955d011SMarcel Moolenaar 		shellPath = path;
25593955d011SMarcel Moolenaar 		path = strrchr(path, '/');
25603955d011SMarcel Moolenaar 		if (path == NULL) {
25613955d011SMarcel Moolenaar 			path = UNCONST(shellPath);
25623955d011SMarcel Moolenaar 		} else {
2563956e45f6SSimon J. Gerraty 			path++;
25643955d011SMarcel Moolenaar 		}
25653955d011SMarcel Moolenaar 		if (newShell.name != NULL) {
25663955d011SMarcel Moolenaar 			shellName = newShell.name;
25673955d011SMarcel Moolenaar 		} else {
25683955d011SMarcel Moolenaar 			shellName = path;
25693955d011SMarcel Moolenaar 		}
25703955d011SMarcel Moolenaar 		if (!fullSpec) {
2571956e45f6SSimon J. Gerraty 			if ((sh = FindShellByName(shellName)) == NULL) {
257206b9b3e0SSimon J. Gerraty 				Parse_Error(PARSE_WARNING,
257306b9b3e0SSimon J. Gerraty 				    "%s: No matching shell", shellName);
25743955d011SMarcel Moolenaar 				free(words);
2575b0c40a00SSimon J. Gerraty 				return false;
25763955d011SMarcel Moolenaar 			}
257706b9b3e0SSimon J. Gerraty 			shell = sh;
25783955d011SMarcel Moolenaar 		} else {
257906b9b3e0SSimon J. Gerraty 			shell = bmake_malloc(sizeof *shell);
258006b9b3e0SSimon J. Gerraty 			*shell = newShell;
25813955d011SMarcel Moolenaar 		}
258251ee2c1cSSimon J. Gerraty 		/* this will take care of shellErrFlag */
258351ee2c1cSSimon J. Gerraty 		Shell_Init();
25843955d011SMarcel Moolenaar 	}
25853955d011SMarcel Moolenaar 
258606b9b3e0SSimon J. Gerraty 	if (shell->echoOn != NULL && shell->echoOff != NULL)
2587b0c40a00SSimon J. Gerraty 		shell->hasEchoCtl = true;
25883955d011SMarcel Moolenaar 
258906b9b3e0SSimon J. Gerraty 	if (!shell->hasErrCtl) {
259006b9b3e0SSimon J. Gerraty 		if (shell->echoTmpl == NULL)
259106b9b3e0SSimon J. Gerraty 			shell->echoTmpl = "";
259206b9b3e0SSimon J. Gerraty 		if (shell->runIgnTmpl == NULL)
259306b9b3e0SSimon J. Gerraty 			shell->runIgnTmpl = "%s\n";
25943955d011SMarcel Moolenaar 	}
25953955d011SMarcel Moolenaar 
25963955d011SMarcel Moolenaar 	/*
259706b9b3e0SSimon J. Gerraty 	 * Do not free up the words themselves, since they might be in use
259806b9b3e0SSimon J. Gerraty 	 * by the shell specification.
25993955d011SMarcel Moolenaar 	 */
26003955d011SMarcel Moolenaar 	free(words);
2601b0c40a00SSimon J. Gerraty 	return true;
26023955d011SMarcel Moolenaar }
26033955d011SMarcel Moolenaar 
260406b9b3e0SSimon J. Gerraty /*
260506b9b3e0SSimon J. Gerraty  * Handle the receipt of an interrupt.
2606956e45f6SSimon J. Gerraty  *
2607956e45f6SSimon J. Gerraty  * All children are killed. Another job will be started if the .INTERRUPT
2608956e45f6SSimon J. Gerraty  * target is defined.
26093955d011SMarcel Moolenaar  *
26103955d011SMarcel Moolenaar  * Input:
26113955d011SMarcel Moolenaar  *	runINTERRUPT	Non-zero if commands for the .INTERRUPT target
26123955d011SMarcel Moolenaar  *			should be executed
26133955d011SMarcel Moolenaar  *	signo		signal received
26143955d011SMarcel Moolenaar  */
26153955d011SMarcel Moolenaar static void
2616b0c40a00SSimon J. Gerraty JobInterrupt(bool runINTERRUPT, int signo)
26173955d011SMarcel Moolenaar {
26183955d011SMarcel Moolenaar 	Job *job;		/* job descriptor in that element */
26193955d011SMarcel Moolenaar 	GNode *interrupt;	/* the node describing the .INTERRUPT target */
26203955d011SMarcel Moolenaar 	sigset_t mask;
26213955d011SMarcel Moolenaar 	GNode *gn;
26223955d011SMarcel Moolenaar 
26233955d011SMarcel Moolenaar 	aborting = ABORT_INTERRUPT;
26243955d011SMarcel Moolenaar 
26253955d011SMarcel Moolenaar 	JobSigLock(&mask);
26263955d011SMarcel Moolenaar 
26273955d011SMarcel Moolenaar 	for (job = job_table; job < job_table_end; job++) {
2628e2eeea75SSimon J. Gerraty 		if (job->status != JOB_ST_RUNNING)
26293955d011SMarcel Moolenaar 			continue;
26303955d011SMarcel Moolenaar 
26313955d011SMarcel Moolenaar 		gn = job->node;
26323955d011SMarcel Moolenaar 
263345447996SSimon J. Gerraty 		JobDeleteTarget(gn);
263406b9b3e0SSimon J. Gerraty 		if (job->pid != 0) {
263506b9b3e0SSimon J. Gerraty 			DEBUG2(JOB,
263606b9b3e0SSimon J. Gerraty 			    "JobInterrupt passing signal %d to child %d.\n",
26373955d011SMarcel Moolenaar 			    signo, job->pid);
26383955d011SMarcel Moolenaar 			KILLPG(job->pid, signo);
26393955d011SMarcel Moolenaar 		}
26403955d011SMarcel Moolenaar 	}
26413955d011SMarcel Moolenaar 
26423955d011SMarcel Moolenaar 	JobSigUnlock(&mask);
26433955d011SMarcel Moolenaar 
2644956e45f6SSimon J. Gerraty 	if (runINTERRUPT && !opts.touchFlag) {
2645956e45f6SSimon J. Gerraty 		interrupt = Targ_FindNode(".INTERRUPT");
26463955d011SMarcel Moolenaar 		if (interrupt != NULL) {
2647b0c40a00SSimon J. Gerraty 			opts.ignoreErrors = false;
26483955d011SMarcel Moolenaar 			JobRun(interrupt);
26493955d011SMarcel Moolenaar 		}
26503955d011SMarcel Moolenaar 	}
2651e2eeea75SSimon J. Gerraty 	Trace_Log(MAKEINTR, NULL);
265206b9b3e0SSimon J. Gerraty 	exit(signo);		/* XXX: why signo? */
26533955d011SMarcel Moolenaar }
26543955d011SMarcel Moolenaar 
265506b9b3e0SSimon J. Gerraty /*
265606b9b3e0SSimon J. Gerraty  * Do the final processing, i.e. run the commands attached to the .END target.
26573955d011SMarcel Moolenaar  *
265806b9b3e0SSimon J. Gerraty  * Return the number of errors reported.
265906b9b3e0SSimon J. Gerraty  */
26603955d011SMarcel Moolenaar int
26613955d011SMarcel Moolenaar Job_Finish(void)
26623955d011SMarcel Moolenaar {
2663956e45f6SSimon J. Gerraty 	GNode *endNode = Targ_GetEndNode();
266406b9b3e0SSimon J. Gerraty 	if (!Lst_IsEmpty(&endNode->commands) ||
266506b9b3e0SSimon J. Gerraty 	    !Lst_IsEmpty(&endNode->children)) {
266606b9b3e0SSimon J. Gerraty 		if (job_errors != 0) {
26673955d011SMarcel Moolenaar 			Error("Errors reported so .END ignored");
26683955d011SMarcel Moolenaar 		} else {
2669956e45f6SSimon J. Gerraty 			JobRun(endNode);
26703955d011SMarcel Moolenaar 		}
26713955d011SMarcel Moolenaar 	}
267206b9b3e0SSimon J. Gerraty 	return job_errors;
26733955d011SMarcel Moolenaar }
26743955d011SMarcel Moolenaar 
2675956e45f6SSimon J. Gerraty /* Clean up any memory used by the jobs module. */
26763955d011SMarcel Moolenaar void
26773955d011SMarcel Moolenaar Job_End(void)
26783955d011SMarcel Moolenaar {
26793955d011SMarcel Moolenaar #ifdef CLEANUP
2680dba7b0efSSimon J. Gerraty 	free(shell_freeIt);
26813955d011SMarcel Moolenaar #endif
26823955d011SMarcel Moolenaar }
26833955d011SMarcel Moolenaar 
268406b9b3e0SSimon J. Gerraty /*
268506b9b3e0SSimon J. Gerraty  * Waits for all running jobs to finish and returns.
268606b9b3e0SSimon J. Gerraty  * Sets 'aborting' to ABORT_WAIT to prevent other jobs from starting.
268706b9b3e0SSimon J. Gerraty  */
26883955d011SMarcel Moolenaar void
26893955d011SMarcel Moolenaar Job_Wait(void)
26903955d011SMarcel Moolenaar {
26913955d011SMarcel Moolenaar 	aborting = ABORT_WAIT;
26923955d011SMarcel Moolenaar 	while (jobTokensRunning != 0) {
26933955d011SMarcel Moolenaar 		Job_CatchOutput();
26943955d011SMarcel Moolenaar 	}
2695956e45f6SSimon J. Gerraty 	aborting = ABORT_NONE;
26963955d011SMarcel Moolenaar }
26973955d011SMarcel Moolenaar 
269806b9b3e0SSimon J. Gerraty /*
269906b9b3e0SSimon J. Gerraty  * Abort all currently running jobs without handling output or anything.
2700956e45f6SSimon J. Gerraty  * This function is to be called only in the event of a major error.
2701956e45f6SSimon J. Gerraty  * Most definitely NOT to be called from JobInterrupt.
27023955d011SMarcel Moolenaar  *
270306b9b3e0SSimon J. Gerraty  * All children are killed, not just the firstborn.
270406b9b3e0SSimon J. Gerraty  */
27053955d011SMarcel Moolenaar void
27063955d011SMarcel Moolenaar Job_AbortAll(void)
27073955d011SMarcel Moolenaar {
27083955d011SMarcel Moolenaar 	Job *job;		/* the job descriptor in that element */
27093955d011SMarcel Moolenaar 	WAIT_T foo;
27103955d011SMarcel Moolenaar 
27113955d011SMarcel Moolenaar 	aborting = ABORT_ERROR;
27123955d011SMarcel Moolenaar 
271306b9b3e0SSimon J. Gerraty 	if (jobTokensRunning != 0) {
27143955d011SMarcel Moolenaar 		for (job = job_table; job < job_table_end; job++) {
2715e2eeea75SSimon J. Gerraty 			if (job->status != JOB_ST_RUNNING)
27163955d011SMarcel Moolenaar 				continue;
27173955d011SMarcel Moolenaar 			/*
271806b9b3e0SSimon J. Gerraty 			 * kill the child process with increasingly drastic
271906b9b3e0SSimon J. Gerraty 			 * signals to make darn sure it's dead.
27203955d011SMarcel Moolenaar 			 */
27213955d011SMarcel Moolenaar 			KILLPG(job->pid, SIGINT);
27223955d011SMarcel Moolenaar 			KILLPG(job->pid, SIGKILL);
27233955d011SMarcel Moolenaar 		}
27243955d011SMarcel Moolenaar 	}
27253955d011SMarcel Moolenaar 
27263955d011SMarcel Moolenaar 	/*
27273955d011SMarcel Moolenaar 	 * Catch as many children as want to report in at first, then give up
27283955d011SMarcel Moolenaar 	 */
27293955d011SMarcel Moolenaar 	while (waitpid((pid_t)-1, &foo, WNOHANG) > 0)
27303955d011SMarcel Moolenaar 		continue;
27313955d011SMarcel Moolenaar }
27323955d011SMarcel Moolenaar 
273306b9b3e0SSimon J. Gerraty /*
273406b9b3e0SSimon J. Gerraty  * Tries to restart stopped jobs if there are slots available.
273506b9b3e0SSimon J. Gerraty  * Called in process context in response to a SIGCONT.
273606b9b3e0SSimon J. Gerraty  */
27373955d011SMarcel Moolenaar static void
27383955d011SMarcel Moolenaar JobRestartJobs(void)
27393955d011SMarcel Moolenaar {
27403955d011SMarcel Moolenaar 	Job *job;
27413955d011SMarcel Moolenaar 
27423955d011SMarcel Moolenaar 	for (job = job_table; job < job_table_end; job++) {
2743e2eeea75SSimon J. Gerraty 		if (job->status == JOB_ST_RUNNING &&
2744e2eeea75SSimon J. Gerraty 		    (make_suspended || job->suspended)) {
274506b9b3e0SSimon J. Gerraty 			DEBUG1(JOB, "Restarting stopped job pid %d.\n",
274606b9b3e0SSimon J. Gerraty 			    job->pid);
2747e2eeea75SSimon J. Gerraty 			if (job->suspended) {
274806b9b3e0SSimon J. Gerraty 				(void)printf("*** [%s] Continued\n",
274906b9b3e0SSimon J. Gerraty 				    job->node->name);
27503955d011SMarcel Moolenaar 				(void)fflush(stdout);
27513955d011SMarcel Moolenaar 			}
2752b0c40a00SSimon J. Gerraty 			job->suspended = false;
27533955d011SMarcel Moolenaar 			if (KILLPG(job->pid, SIGCONT) != 0 && DEBUG(JOB)) {
275406b9b3e0SSimon J. Gerraty 				debug_printf("Failed to send SIGCONT to %d\n",
275506b9b3e0SSimon J. Gerraty 				    job->pid);
27563955d011SMarcel Moolenaar 			}
27573955d011SMarcel Moolenaar 		}
275806b9b3e0SSimon J. Gerraty 		if (job->status == JOB_ST_FINISHED) {
275906b9b3e0SSimon J. Gerraty 			/*
276006b9b3e0SSimon J. Gerraty 			 * Job exit deferred after calling waitpid() in a
276106b9b3e0SSimon J. Gerraty 			 * signal handler
276206b9b3e0SSimon J. Gerraty 			 */
27633955d011SMarcel Moolenaar 			JobFinish(job, job->exit_status);
27643955d011SMarcel Moolenaar 		}
276506b9b3e0SSimon J. Gerraty 	}
2766b0c40a00SSimon J. Gerraty 	make_suspended = false;
27673955d011SMarcel Moolenaar }
27683955d011SMarcel Moolenaar 
27693955d011SMarcel Moolenaar static void
27703955d011SMarcel Moolenaar watchfd(Job *job)
27713955d011SMarcel Moolenaar {
27723955d011SMarcel Moolenaar 	if (job->inPollfd != NULL)
27733955d011SMarcel Moolenaar 		Punt("Watching watched job");
27743955d011SMarcel Moolenaar 
2775dba7b0efSSimon J. Gerraty 	fds[fdsLen].fd = job->inPipe;
2776dba7b0efSSimon J. Gerraty 	fds[fdsLen].events = POLLIN;
2777dba7b0efSSimon J. Gerraty 	jobByFdIndex[fdsLen] = job;
2778dba7b0efSSimon J. Gerraty 	job->inPollfd = &fds[fdsLen];
2779dba7b0efSSimon J. Gerraty 	fdsLen++;
278049caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
278149caa483SSimon J. Gerraty 	if (useMeta) {
2782dba7b0efSSimon J. Gerraty 		fds[fdsLen].fd = meta_job_fd(job);
2783dba7b0efSSimon J. Gerraty 		fds[fdsLen].events = fds[fdsLen].fd == -1 ? 0 : POLLIN;
2784dba7b0efSSimon J. Gerraty 		jobByFdIndex[fdsLen] = job;
2785dba7b0efSSimon J. Gerraty 		fdsLen++;
278649caa483SSimon J. Gerraty 	}
278749caa483SSimon J. Gerraty #endif
27883955d011SMarcel Moolenaar }
27893955d011SMarcel Moolenaar 
27903955d011SMarcel Moolenaar static void
27913955d011SMarcel Moolenaar clearfd(Job *job)
27923955d011SMarcel Moolenaar {
2793956e45f6SSimon J. Gerraty 	size_t i;
27943955d011SMarcel Moolenaar 	if (job->inPollfd == NULL)
27953955d011SMarcel Moolenaar 		Punt("Unwatching unwatched job");
2796956e45f6SSimon J. Gerraty 	i = (size_t)(job->inPollfd - fds);
2797dba7b0efSSimon J. Gerraty 	fdsLen--;
279849caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
279949caa483SSimon J. Gerraty 	if (useMeta) {
280049caa483SSimon J. Gerraty 		/*
280149caa483SSimon J. Gerraty 		 * Sanity check: there should be two fds per job, so the job's
280249caa483SSimon J. Gerraty 		 * pollfd number should be even.
280349caa483SSimon J. Gerraty 		 */
280449caa483SSimon J. Gerraty 		assert(nfds_per_job() == 2);
280506b9b3e0SSimon J. Gerraty 		if (i % 2 != 0)
280649caa483SSimon J. Gerraty 			Punt("odd-numbered fd with meta");
2807dba7b0efSSimon J. Gerraty 		fdsLen--;
280849caa483SSimon J. Gerraty 	}
280949caa483SSimon J. Gerraty #endif
28103955d011SMarcel Moolenaar 	/*
28113955d011SMarcel Moolenaar 	 * Move last job in table into hole made by dead job.
28123955d011SMarcel Moolenaar 	 */
2813dba7b0efSSimon J. Gerraty 	if (fdsLen != i) {
2814dba7b0efSSimon J. Gerraty 		fds[i] = fds[fdsLen];
2815dba7b0efSSimon J. Gerraty 		jobByFdIndex[i] = jobByFdIndex[fdsLen];
2816dba7b0efSSimon J. Gerraty 		jobByFdIndex[i]->inPollfd = &fds[i];
281749caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
281849caa483SSimon J. Gerraty 		if (useMeta) {
2819dba7b0efSSimon J. Gerraty 			fds[i + 1] = fds[fdsLen + 1];
2820dba7b0efSSimon J. Gerraty 			jobByFdIndex[i + 1] = jobByFdIndex[fdsLen + 1];
282149caa483SSimon J. Gerraty 		}
282249caa483SSimon J. Gerraty #endif
28233955d011SMarcel Moolenaar 	}
28243955d011SMarcel Moolenaar 	job->inPollfd = NULL;
28253955d011SMarcel Moolenaar }
28263955d011SMarcel Moolenaar 
2827b0c40a00SSimon J. Gerraty static bool
28283955d011SMarcel Moolenaar readyfd(Job *job)
28293955d011SMarcel Moolenaar {
28303955d011SMarcel Moolenaar 	if (job->inPollfd == NULL)
28313955d011SMarcel Moolenaar 		Punt("Polling unwatched job");
28323955d011SMarcel Moolenaar 	return (job->inPollfd->revents & POLLIN) != 0;
28333955d011SMarcel Moolenaar }
28343955d011SMarcel Moolenaar 
283506b9b3e0SSimon J. Gerraty /*
283606b9b3e0SSimon J. Gerraty  * Put a token (back) into the job pipe.
283706b9b3e0SSimon J. Gerraty  * This allows a make process to start a build job.
283806b9b3e0SSimon J. Gerraty  */
28393955d011SMarcel Moolenaar static void
28403955d011SMarcel Moolenaar JobTokenAdd(void)
28413955d011SMarcel Moolenaar {
28423955d011SMarcel Moolenaar 	char tok = JOB_TOKENS[aborting], tok1;
28433955d011SMarcel Moolenaar 
28442d395cb5SSimon J. Gerraty 	if (!Job_error_token && aborting == ABORT_ERROR) {
28452d395cb5SSimon J. Gerraty 		if (jobTokensRunning == 0)
28462d395cb5SSimon J. Gerraty 			return;
28472d395cb5SSimon J. Gerraty 		tok = '+';		/* no error token */
28482d395cb5SSimon J. Gerraty 	}
28492d395cb5SSimon J. Gerraty 
28503955d011SMarcel Moolenaar 	/* If we are depositing an error token flush everything else */
28513955d011SMarcel Moolenaar 	while (tok != '+' && read(tokenWaitJob.inPipe, &tok1, 1) == 1)
28523955d011SMarcel Moolenaar 		continue;
28533955d011SMarcel Moolenaar 
2854956e45f6SSimon J. Gerraty 	DEBUG3(JOB, "(%d) aborting %d, deposit token %c\n",
28552d395cb5SSimon J. Gerraty 	    getpid(), aborting, tok);
28563cbdda60SSimon J. Gerraty 	while (write(tokenWaitJob.outPipe, &tok, 1) == -1 && errno == EAGAIN)
28573cbdda60SSimon J. Gerraty 		continue;
28583955d011SMarcel Moolenaar }
28593955d011SMarcel Moolenaar 
2860dba7b0efSSimon J. Gerraty /* Get a temp file */
2861dba7b0efSSimon J. Gerraty int
2862dba7b0efSSimon J. Gerraty Job_TempFile(const char *pattern, char *tfile, size_t tfile_sz)
2863dba7b0efSSimon J. Gerraty {
2864dba7b0efSSimon J. Gerraty 	int fd;
2865dba7b0efSSimon J. Gerraty 	sigset_t mask;
2866dba7b0efSSimon J. Gerraty 
2867dba7b0efSSimon J. Gerraty 	JobSigLock(&mask);
2868dba7b0efSSimon J. Gerraty 	fd = mkTempFile(pattern, tfile, tfile_sz);
2869dba7b0efSSimon J. Gerraty 	if (tfile != NULL && !DEBUG(SCRIPT))
2870dba7b0efSSimon J. Gerraty 	    unlink(tfile);
2871dba7b0efSSimon J. Gerraty 	JobSigUnlock(&mask);
2872dba7b0efSSimon J. Gerraty 
2873dba7b0efSSimon J. Gerraty 	return fd;
2874dba7b0efSSimon J. Gerraty }
2875dba7b0efSSimon J. Gerraty 
2876956e45f6SSimon J. Gerraty /* Prep the job token pipe in the root make process. */
28773955d011SMarcel Moolenaar void
28783955d011SMarcel Moolenaar Job_ServerStart(int max_tokens, int jp_0, int jp_1)
28793955d011SMarcel Moolenaar {
28803955d011SMarcel Moolenaar 	int i;
28813955d011SMarcel Moolenaar 	char jobarg[64];
28823955d011SMarcel Moolenaar 
28833955d011SMarcel Moolenaar 	if (jp_0 >= 0 && jp_1 >= 0) {
28843955d011SMarcel Moolenaar 		/* Pipe passed in from parent */
28853955d011SMarcel Moolenaar 		tokenWaitJob.inPipe = jp_0;
28863955d011SMarcel Moolenaar 		tokenWaitJob.outPipe = jp_1;
2887be19d90bSSimon J. Gerraty 		(void)fcntl(jp_0, F_SETFD, FD_CLOEXEC);
2888be19d90bSSimon J. Gerraty 		(void)fcntl(jp_1, F_SETFD, FD_CLOEXEC);
28893955d011SMarcel Moolenaar 		return;
28903955d011SMarcel Moolenaar 	}
28913955d011SMarcel Moolenaar 
28923955d011SMarcel Moolenaar 	JobCreatePipe(&tokenWaitJob, 15);
28933955d011SMarcel Moolenaar 
2894e2eeea75SSimon J. Gerraty 	snprintf(jobarg, sizeof jobarg, "%d,%d",
28953955d011SMarcel Moolenaar 	    tokenWaitJob.inPipe, tokenWaitJob.outPipe);
28963955d011SMarcel Moolenaar 
2897dba7b0efSSimon J. Gerraty 	Global_Append(MAKEFLAGS, "-J");
2898dba7b0efSSimon J. Gerraty 	Global_Append(MAKEFLAGS, jobarg);
28993955d011SMarcel Moolenaar 
29003955d011SMarcel Moolenaar 	/*
29013955d011SMarcel Moolenaar 	 * Preload the job pipe with one token per job, save the one
29023955d011SMarcel Moolenaar 	 * "extra" token for the primary job.
29033955d011SMarcel Moolenaar 	 *
29043955d011SMarcel Moolenaar 	 * XXX should clip maxJobs against PIPE_BUF -- if max_tokens is
29053955d011SMarcel Moolenaar 	 * larger than the write buffer size of the pipe, we will
29063955d011SMarcel Moolenaar 	 * deadlock here.
29073955d011SMarcel Moolenaar 	 */
29083955d011SMarcel Moolenaar 	for (i = 1; i < max_tokens; i++)
29093955d011SMarcel Moolenaar 		JobTokenAdd();
29103955d011SMarcel Moolenaar }
29113955d011SMarcel Moolenaar 
2912956e45f6SSimon J. Gerraty /* Return a withdrawn token to the pool. */
29133955d011SMarcel Moolenaar void
29143955d011SMarcel Moolenaar Job_TokenReturn(void)
29153955d011SMarcel Moolenaar {
29163955d011SMarcel Moolenaar 	jobTokensRunning--;
29173955d011SMarcel Moolenaar 	if (jobTokensRunning < 0)
29183955d011SMarcel Moolenaar 		Punt("token botch");
291906b9b3e0SSimon J. Gerraty 	if (jobTokensRunning != 0 || JOB_TOKENS[aborting] != '+')
29203955d011SMarcel Moolenaar 		JobTokenAdd();
29213955d011SMarcel Moolenaar }
29223955d011SMarcel Moolenaar 
292306b9b3e0SSimon J. Gerraty /*
292406b9b3e0SSimon J. Gerraty  * Attempt to withdraw a token from the pool.
29253955d011SMarcel Moolenaar  *
2926956e45f6SSimon J. Gerraty  * If pool is empty, set wantToken so that we wake up when a token is
2927956e45f6SSimon J. Gerraty  * released.
29283955d011SMarcel Moolenaar  *
2929b0c40a00SSimon J. Gerraty  * Returns true if a token was withdrawn, and false if the pool is currently
293006b9b3e0SSimon J. Gerraty  * empty.
293106b9b3e0SSimon J. Gerraty  */
2932b0c40a00SSimon J. Gerraty bool
29333955d011SMarcel Moolenaar Job_TokenWithdraw(void)
29343955d011SMarcel Moolenaar {
29353955d011SMarcel Moolenaar 	char tok, tok1;
2936956e45f6SSimon J. Gerraty 	ssize_t count;
29373955d011SMarcel Moolenaar 
29383955d011SMarcel Moolenaar 	wantToken = 0;
2939956e45f6SSimon J. Gerraty 	DEBUG3(JOB, "Job_TokenWithdraw(%d): aborting %d, running %d\n",
29403955d011SMarcel Moolenaar 	    getpid(), aborting, jobTokensRunning);
29413955d011SMarcel Moolenaar 
2942956e45f6SSimon J. Gerraty 	if (aborting != ABORT_NONE || (jobTokensRunning >= opts.maxJobs))
2943b0c40a00SSimon J. Gerraty 		return false;
29443955d011SMarcel Moolenaar 
29453955d011SMarcel Moolenaar 	count = read(tokenWaitJob.inPipe, &tok, 1);
29463955d011SMarcel Moolenaar 	if (count == 0)
29473955d011SMarcel Moolenaar 		Fatal("eof on job pipe!");
29483955d011SMarcel Moolenaar 	if (count < 0 && jobTokensRunning != 0) {
29493955d011SMarcel Moolenaar 		if (errno != EAGAIN) {
29503955d011SMarcel Moolenaar 			Fatal("job pipe read: %s", strerror(errno));
29513955d011SMarcel Moolenaar 		}
2952956e45f6SSimon J. Gerraty 		DEBUG1(JOB, "(%d) blocked for token\n", getpid());
295306b9b3e0SSimon J. Gerraty 		wantToken = 1;
2954b0c40a00SSimon J. Gerraty 		return false;
29553955d011SMarcel Moolenaar 	}
29563955d011SMarcel Moolenaar 
29573955d011SMarcel Moolenaar 	if (count == 1 && tok != '+') {
295806b9b3e0SSimon J. Gerraty 		/* make being aborted - remove any other job tokens */
2959956e45f6SSimon J. Gerraty 		DEBUG2(JOB, "(%d) aborted by token %c\n", getpid(), tok);
29603955d011SMarcel Moolenaar 		while (read(tokenWaitJob.inPipe, &tok1, 1) == 1)
29613955d011SMarcel Moolenaar 			continue;
29623955d011SMarcel Moolenaar 		/* And put the stopper back */
296306b9b3e0SSimon J. Gerraty 		while (write(tokenWaitJob.outPipe, &tok, 1) == -1 &&
296406b9b3e0SSimon J. Gerraty 		       errno == EAGAIN)
29653cbdda60SSimon J. Gerraty 			continue;
2966e2eeea75SSimon J. Gerraty 		if (shouldDieQuietly(NULL, 1))
296706b9b3e0SSimon J. Gerraty 			exit(6);	/* we aborted */
296806b9b3e0SSimon J. Gerraty 		Fatal("A failure has been detected "
296906b9b3e0SSimon J. Gerraty 		      "in another branch of the parallel make");
29703955d011SMarcel Moolenaar 	}
29713955d011SMarcel Moolenaar 
29723955d011SMarcel Moolenaar 	if (count == 1 && jobTokensRunning == 0)
29733955d011SMarcel Moolenaar 		/* We didn't want the token really */
297406b9b3e0SSimon J. Gerraty 		while (write(tokenWaitJob.outPipe, &tok, 1) == -1 &&
297506b9b3e0SSimon J. Gerraty 		       errno == EAGAIN)
29763cbdda60SSimon J. Gerraty 			continue;
29773955d011SMarcel Moolenaar 
29783955d011SMarcel Moolenaar 	jobTokensRunning++;
2979956e45f6SSimon J. Gerraty 	DEBUG1(JOB, "(%d) withdrew token\n", getpid());
2980b0c40a00SSimon J. Gerraty 	return true;
29813955d011SMarcel Moolenaar }
29823955d011SMarcel Moolenaar 
298306b9b3e0SSimon J. Gerraty /*
298406b9b3e0SSimon J. Gerraty  * Run the named target if found. If a filename is specified, then set that
2985956e45f6SSimon J. Gerraty  * to the sources.
29861748de26SSimon J. Gerraty  *
298706b9b3e0SSimon J. Gerraty  * Exits if the target fails.
298806b9b3e0SSimon J. Gerraty  */
2989b0c40a00SSimon J. Gerraty bool
299006b9b3e0SSimon J. Gerraty Job_RunTarget(const char *target, const char *fname)
299106b9b3e0SSimon J. Gerraty {
2992956e45f6SSimon J. Gerraty 	GNode *gn = Targ_FindNode(target);
29931748de26SSimon J. Gerraty 	if (gn == NULL)
2994b0c40a00SSimon J. Gerraty 		return false;
29951748de26SSimon J. Gerraty 
299606b9b3e0SSimon J. Gerraty 	if (fname != NULL)
2997dba7b0efSSimon J. Gerraty 		Var_Set(gn, ALLSRC, fname);
29981748de26SSimon J. Gerraty 
29991748de26SSimon J. Gerraty 	JobRun(gn);
300006b9b3e0SSimon J. Gerraty 	/* XXX: Replace with GNode_IsError(gn) */
30011748de26SSimon J. Gerraty 	if (gn->made == ERROR) {
30021748de26SSimon J. Gerraty 		PrintOnError(gn, "\n\nStop.");
30031748de26SSimon J. Gerraty 		exit(1);
30041748de26SSimon J. Gerraty 	}
3005b0c40a00SSimon J. Gerraty 	return true;
30061748de26SSimon J. Gerraty }
30071748de26SSimon J. Gerraty 
30083955d011SMarcel Moolenaar #ifdef USE_SELECT
30093955d011SMarcel Moolenaar int
30103955d011SMarcel Moolenaar emul_poll(struct pollfd *fd, int nfd, int timeout)
30113955d011SMarcel Moolenaar {
30123955d011SMarcel Moolenaar 	fd_set rfds, wfds;
30133955d011SMarcel Moolenaar 	int i, maxfd, nselect, npoll;
30143955d011SMarcel Moolenaar 	struct timeval tv, *tvp;
30153955d011SMarcel Moolenaar 	long usecs;
30163955d011SMarcel Moolenaar 
30173955d011SMarcel Moolenaar 	FD_ZERO(&rfds);
30183955d011SMarcel Moolenaar 	FD_ZERO(&wfds);
30193955d011SMarcel Moolenaar 
30203955d011SMarcel Moolenaar 	maxfd = -1;
30213955d011SMarcel Moolenaar 	for (i = 0; i < nfd; i++) {
30223955d011SMarcel Moolenaar 		fd[i].revents = 0;
30233955d011SMarcel Moolenaar 
30243955d011SMarcel Moolenaar 		if (fd[i].events & POLLIN)
30253955d011SMarcel Moolenaar 			FD_SET(fd[i].fd, &rfds);
30263955d011SMarcel Moolenaar 
30273955d011SMarcel Moolenaar 		if (fd[i].events & POLLOUT)
30283955d011SMarcel Moolenaar 			FD_SET(fd[i].fd, &wfds);
30293955d011SMarcel Moolenaar 
30303955d011SMarcel Moolenaar 		if (fd[i].fd > maxfd)
30313955d011SMarcel Moolenaar 			maxfd = fd[i].fd;
30323955d011SMarcel Moolenaar 	}
30333955d011SMarcel Moolenaar 
30343955d011SMarcel Moolenaar 	if (maxfd >= FD_SETSIZE) {
30353955d011SMarcel Moolenaar 		Punt("Ran out of fd_set slots; "
30363955d011SMarcel Moolenaar 		     "recompile with a larger FD_SETSIZE.");
30373955d011SMarcel Moolenaar 	}
30383955d011SMarcel Moolenaar 
30393955d011SMarcel Moolenaar 	if (timeout < 0) {
30403955d011SMarcel Moolenaar 		tvp = NULL;
30413955d011SMarcel Moolenaar 	} else {
30423955d011SMarcel Moolenaar 		usecs = timeout * 1000;
30433955d011SMarcel Moolenaar 		tv.tv_sec = usecs / 1000000;
30443955d011SMarcel Moolenaar 		tv.tv_usec = usecs % 1000000;
30453955d011SMarcel Moolenaar 		tvp = &tv;
30463955d011SMarcel Moolenaar 	}
30473955d011SMarcel Moolenaar 
3048e2eeea75SSimon J. Gerraty 	nselect = select(maxfd + 1, &rfds, &wfds, NULL, tvp);
30493955d011SMarcel Moolenaar 
30503955d011SMarcel Moolenaar 	if (nselect <= 0)
30513955d011SMarcel Moolenaar 		return nselect;
30523955d011SMarcel Moolenaar 
30533955d011SMarcel Moolenaar 	npoll = 0;
30543955d011SMarcel Moolenaar 	for (i = 0; i < nfd; i++) {
30553955d011SMarcel Moolenaar 		if (FD_ISSET(fd[i].fd, &rfds))
30563955d011SMarcel Moolenaar 			fd[i].revents |= POLLIN;
30573955d011SMarcel Moolenaar 
30583955d011SMarcel Moolenaar 		if (FD_ISSET(fd[i].fd, &wfds))
30593955d011SMarcel Moolenaar 			fd[i].revents |= POLLOUT;
30603955d011SMarcel Moolenaar 
30613955d011SMarcel Moolenaar 		if (fd[i].revents)
30623955d011SMarcel Moolenaar 			npoll++;
30633955d011SMarcel Moolenaar 	}
30643955d011SMarcel Moolenaar 
30653955d011SMarcel Moolenaar 	return npoll;
30663955d011SMarcel Moolenaar }
30673955d011SMarcel Moolenaar #endif /* USE_SELECT */
3068