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