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