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