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