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