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