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