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