job.c revision 144467
1164190Sjkoshy/*-
2164190Sjkoshy * Copyright (c) 1988, 1989, 1990, 1993
3164190Sjkoshy *	The Regents of the University of California.  All rights reserved.
4164190Sjkoshy * Copyright (c) 1988, 1989 by Adam de Boor
5164190Sjkoshy * Copyright (c) 1989 by Berkeley Softworks
6164190Sjkoshy * All rights reserved.
7164190Sjkoshy *
8164190Sjkoshy * This code is derived from software contributed to Berkeley by
9164190Sjkoshy * Adam de Boor.
10164190Sjkoshy *
11164190Sjkoshy * Redistribution and use in source and binary forms, with or without
12164190Sjkoshy * modification, are permitted provided that the following conditions
13164190Sjkoshy * are met:
14164190Sjkoshy * 1. Redistributions of source code must retain the above copyright
15164190Sjkoshy *    notice, this list of conditions and the following disclaimer.
16164190Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright
17164190Sjkoshy *    notice, this list of conditions and the following disclaimer in the
18164190Sjkoshy *    documentation and/or other materials provided with the distribution.
19164190Sjkoshy * 3. All advertising materials mentioning features or use of this software
20164190Sjkoshy *    must display the following acknowledgement:
21164190Sjkoshy *	This product includes software developed by the University of
22164190Sjkoshy *	California, Berkeley and its contributors.
23164190Sjkoshy * 4. Neither the name of the University nor the names of its contributors
24164190Sjkoshy *    may be used to endorse or promote products derived from this software
25164190Sjkoshy *    without specific prior written permission.
26164190Sjkoshy *
27206622Suqs * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28164190Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29164190Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30164190Sjkoshy * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31164190Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32164190Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33164190Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34164190Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35164190Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36164190Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37164190Sjkoshy * SUCH DAMAGE.
38164190Sjkoshy *
39164190Sjkoshy * @(#)job.c	8.2 (Berkeley) 3/19/94
40164190Sjkoshy */
41164190Sjkoshy
42164190Sjkoshy#include <sys/cdefs.h>
43164190Sjkoshy__FBSDID("$FreeBSD: head/usr.bin/make/job.c 144467 2005-04-01 10:53:43Z harti $");
44164190Sjkoshy
45164190Sjkoshy#ifndef OLD_JOKE
46164190Sjkoshy#define	OLD_JOKE 0
47164190Sjkoshy#endif /* OLD_JOKE */
48164190Sjkoshy
49164190Sjkoshy/*-
50164190Sjkoshy * job.c --
51164190Sjkoshy *	handle the creation etc. of our child processes.
52164190Sjkoshy *
53164190Sjkoshy * Interface:
54164190Sjkoshy *	Job_Make	Start the creation of the given target.
55164190Sjkoshy *
56164190Sjkoshy *	Job_CatchChildren
57164190Sjkoshy *			Check for and handle the termination of any children.
58164190Sjkoshy *			This must be called reasonably frequently to keep the
59164190Sjkoshy *			whole make going at a decent clip, since job table
60164190Sjkoshy *			entries aren't removed until their process is caught
61164190Sjkoshy *			this way. Its single argument is TRUE if the function
62164190Sjkoshy *			should block waiting for a child to terminate.
63164190Sjkoshy *
64164190Sjkoshy *	Job_CatchOutput	Print any output our children have produced. Should
65164190Sjkoshy *			also be called fairly frequently to keep the user
66164190Sjkoshy *			informed of what's going on. If no output is waiting,
67164190Sjkoshy *			it will block for a time given by the SEL_* constants,
68164190Sjkoshy *			below, or until output is ready.
69164190Sjkoshy *
70164190Sjkoshy *	Job_Init	Called to intialize this module. in addition, any
71164190Sjkoshy *			commands attached to the .BEGIN target are executed
72164190Sjkoshy *			before this function returns. Hence, the makefile must
73164190Sjkoshy *			have been parsed before this function is called.
74164190Sjkoshy *
75164190Sjkoshy *	Job_Full	Return TRUE if the job table is filled.
76164190Sjkoshy *
77164190Sjkoshy *	Job_Empty	Return TRUE if the job table is completely empty.
78164190Sjkoshy *
79164190Sjkoshy *	Job_ParseShell	Given the line following a .SHELL target, parse the
80164190Sjkoshy *			line as a shell specification. Returns FAILURE if the
81164190Sjkoshy *			spec was incorrect.
82164190Sjkoshy *
83164190Sjkoshy *	Job_Finish	Perform any final processing which needs doing. This
84164190Sjkoshy *			includes the execution of any commands which have
85164190Sjkoshy *			been/were attached to the .END target. It should only
86164190Sjkoshy *			be called when the job table is empty.
87164190Sjkoshy *
88164190Sjkoshy *	Job_AbortAll	Abort all currently running jobs. It doesn't handle
89164190Sjkoshy *			output or do anything for the jobs, just kills them.
90164190Sjkoshy *			It should only be called in an emergency, as it were.
91164190Sjkoshy *
92164190Sjkoshy *	Job_CheckCommands
93164190Sjkoshy *			Verify that the commands for a target are ok. Provide
94164190Sjkoshy *			them if necessary and possible.
95164190Sjkoshy *
96164190Sjkoshy *	Job_Touch	Update a target without really updating it.
97164190Sjkoshy *
98164190Sjkoshy *	Job_Wait	Wait for all currently-running jobs to finish.
99164190Sjkoshy */
100164190Sjkoshy
101164190Sjkoshy#include <sys/types.h>
102164190Sjkoshy#include <sys/select.h>
103164190Sjkoshy#include <sys/stat.h>
104164190Sjkoshy#ifdef USE_KQUEUE
105164190Sjkoshy#include <sys/event.h>
106164190Sjkoshy#endif
107164190Sjkoshy#include <sys/wait.h>
108164190Sjkoshy#include <ctype.h>
109164190Sjkoshy#include <errno.h>
110164190Sjkoshy#include <fcntl.h>
111164190Sjkoshy#include <string.h>
112164190Sjkoshy#include <signal.h>
113164190Sjkoshy#include <stdlib.h>
114164190Sjkoshy#include <unistd.h>
115164190Sjkoshy#include <utime.h>
116164190Sjkoshy
117164190Sjkoshy#include "arch.h"
118164190Sjkoshy#include "buf.h"
119164190Sjkoshy#include "compat.h"
120164190Sjkoshy#include "dir.h"
121164190Sjkoshy#include "globals.h"
122#include "GNode.h"
123#include "job.h"
124#include "make.h"
125#include "parse.h"
126#include "pathnames.h"
127#include "str.h"
128#include "targ.h"
129#include "util.h"
130#include "var.h"
131
132#define STATIC static
133
134/*
135 * error handling variables
136 */
137static int	errors = 0;	/* number of errors reported */
138static int	aborting = 0;	/* why is the make aborting? */
139#define	ABORT_ERROR	1	/* Because of an error */
140#define	ABORT_INTERRUPT	2	/* Because it was interrupted */
141#define	ABORT_WAIT	3	/* Waiting for jobs to finish */
142
143/*
144 * XXX: Avoid SunOS bug... FILENO() is fp->_file, and file
145 * is a char! So when we go above 127 we turn negative!
146 */
147#define	FILENO(a) ((unsigned)fileno(a))
148
149/*
150 * post-make command processing. The node postCommands is really just the
151 * .END target but we keep it around to avoid having to search for it
152 * all the time.
153 */
154static GNode	*postCommands;
155
156/*
157 * The number of commands actually printed for a target. Should this
158 * number be 0, no shell will be executed.
159 */
160static int	numCommands;
161
162/*
163 * Return values from JobStart.
164 */
165#define	JOB_RUNNING	0	/* Job is running */
166#define	JOB_ERROR 	1	/* Error in starting the job */
167#define	JOB_FINISHED	2	/* The job is already finished */
168#define	JOB_STOPPED	3	/* The job is stopped */
169
170/*
171 * tfile is used to build temp file names to store shell commands to
172 * execute.
173 */
174static char	tfile[sizeof(TMPPAT)];
175
176/*
177 * Descriptions for various shells.
178 */
179static const DEF_SHELL_STRUCT(CShell, const) shells[] = {
180	/*
181	 * CSH description. The csh can do echo control by playing
182	 * with the setting of the 'echo' shell variable. Sadly,
183	 * however, it is unable to do error control nicely.
184	 */
185	{
186		"csh",
187		TRUE, "unset verbose", "set verbose", "unset verbose", 13,
188		FALSE, "echo \"%s\"\n", "csh -c \"%s || exit 0\"",
189		"v", "e",
190	},
191	/*
192	 * SH description. Echo control is also possible and, under
193	 * sun UNIX anyway, one can even control error checking.
194	 */
195	{
196		"sh",
197		TRUE, "set -", "set -v", "set -", 5,
198		TRUE, "set -e", "set +e",
199#ifdef OLDBOURNESHELL
200		FALSE, "echo \"%s\"\n", "sh -c '%s || exit 0'\n",
201#endif
202		"v", "e",
203	},
204	/*
205	 * KSH description. The Korn shell has a superset of
206	 * the Bourne shell's functionality.
207	 */
208	{
209		"ksh",
210		TRUE, "set -", "set -v", "set -", 5,
211		TRUE, "set -e", "set +e",
212		"v", "e",
213	},
214};
215
216/*
217 * This is the shell to which we pass all commands in the Makefile.
218 * It is set by the Job_ParseShell function.
219 */
220static Shell	*commandShell = NULL;
221char		*shellPath = NULL;	/* full pathname of executable image */
222char		*shellName = NULL;	/* last component of shell */
223
224int		maxJobs;	/* The most children we can run at once */
225STATIC int	nJobs;		/* The number of children currently running */
226
227/* The structures that describe them */
228STATIC Lst	jobs = Lst_Initializer(jobs);
229
230STATIC Boolean	jobFull;    	/* Flag to tell when the job table is full. It
231				 * is set TRUE when (1) the total number of
232				 * running jobs equals the maximum allowed */
233#ifdef USE_KQUEUE
234static int	kqfd;		/* File descriptor obtained by kqueue() */
235#else
236static fd_set  	outputs;    	/* Set of descriptors of pipes connected to
237				 * the output channels of children */
238#endif
239
240STATIC GNode   	*lastNode;	/* The node for which output was most recently
241				 * produced. */
242STATIC const char *targFmt;   	/* Format string to use to head output from a
243				 * job when it's not the most-recent job heard
244				 * from */
245
246#define	TARG_FMT  "--- %s ---\n" /* Default format */
247#define	MESSAGE(fp, gn) \
248	 fprintf(fp, targFmt, gn->name);
249
250/*
251 * When JobStart attempts to run a job but isn't allowed to
252 * or when Job_CatchChildren detects a job that has
253 * been stopped somehow, the job is placed on the stoppedJobs queue to be run
254 * when the next job finishes.
255 *
256 * Lst of Job structures describing jobs that were stopped due to
257 * concurrency limits or externally
258 */
259STATIC Lst stoppedJobs = Lst_Initializer(stoppedJobs);
260
261STATIC int	fifoFd;		/* Fd of our job fifo */
262STATIC char	fifoName[] = "/tmp/make_fifo_XXXXXXXXX";
263STATIC int	fifoMaster;
264
265static sig_atomic_t interrupted;
266
267
268#if defined(USE_PGRP) && defined(SYSV)
269# define KILL(pid, sig)		killpg(-(pid), (sig))
270#else
271# if defined(USE_PGRP)
272#  define KILL(pid, sig)	killpg((pid), (sig))
273# else
274#  define KILL(pid, sig)	kill((pid), (sig))
275# endif
276#endif
277
278/*
279 * Grmpf... There is no way to set bits of the wait structure
280 * anymore with the stupid W*() macros. I liked the union wait
281 * stuff much more. So, we devise our own macros... This is
282 * really ugly, use dramamine sparingly. You have been warned.
283 */
284#define	W_SETMASKED(st, val, fun)				\
285	{							\
286		int sh = (int)~0;				\
287		int mask = fun(sh);				\
288								\
289		for (sh = 0; ((mask >> sh) & 1) == 0; sh++)	\
290			continue;				\
291		*(st) = (*(st) & ~mask) | ((val) << sh);	\
292	}
293
294#define	W_SETTERMSIG(st, val) W_SETMASKED(st, val, WTERMSIG)
295#define	W_SETEXITSTATUS(st, val) W_SETMASKED(st, val, WEXITSTATUS)
296
297static void JobPassSig(int);
298static int JobPrintCommand(void *, void *);
299static void JobClose(Job *);
300static void JobFinish(Job *, int *);
301static void JobExec(Job *, char **);
302static void JobMakeArgv(Job *, char **);
303static void JobRestart(Job *);
304static int JobStart(GNode *, int, Job *);
305static char *JobOutput(Job *, char *, char *, int);
306static void JobDoOutput(Job *, Boolean);
307static Shell *JobMatchShell(const char *);
308static void JobInterrupt(int, int);
309static void JobRestartJobs(void);
310
311/**
312 * JobCatchSignal
313 *	Got a signal. Set global variables and hope that someone will
314 *	handle it.
315 */
316static void
317JobCatchSig(int signo)
318{
319
320	interrupted = signo;
321}
322
323/**
324 * JobCondPassSig --
325 *	Pass a signal to all jobs
326 *
327 * Side Effects:
328 *	None, except the job may bite it.
329 */
330static void
331JobCondPassSig(int signo)
332{
333	LstNode	*ln;
334	Job	*job;
335
336	LST_FOREACH(ln, &jobs) {
337		job = Lst_Datum(ln);
338
339		DEBUGF(JOB, ("JobCondPassSig passing signal %d to child %d.\n",
340		    signo, job->pid));
341		KILL(job->pid, signo);
342	}
343}
344
345/**
346 * JobPassSig --
347 *	Pass a signal on to all local jobs if
348 *	USE_PGRP is defined, then die ourselves.
349 *
350 * Side Effects:
351 *	We die by the same signal.
352 */
353static void
354JobPassSig(int signo)
355{
356	sigset_t nmask, omask;
357	struct sigaction act;
358
359	sigemptyset(&nmask);
360	sigaddset(&nmask, signo);
361	sigprocmask(SIG_SETMASK, &nmask, &omask);
362
363	DEBUGF(JOB, ("JobPassSig(%d) called.\n", signo));
364	JobCondPassSig(signo);
365
366	/*
367	 * Deal with proper cleanup based on the signal received. We only run
368	 * the .INTERRUPT target if the signal was in fact an interrupt.
369	 * The other three termination signals are more of a "get out *now*"
370	 * command.
371	 */
372	if (signo == SIGINT) {
373		JobInterrupt(TRUE, signo);
374	} else if ((signo == SIGHUP) || (signo == SIGTERM) || (signo == SIGQUIT)) {
375		JobInterrupt(FALSE, signo);
376	}
377
378	/*
379	 * Leave gracefully if SIGQUIT, rather than core dumping.
380	 */
381	if (signo == SIGQUIT) {
382		signo = SIGINT;
383	}
384
385	/*
386	 * Send ourselves the signal now we've given the message to everyone
387	 * else. Note we block everything else possible while we're getting
388	 * the signal. This ensures that all our jobs get continued when we
389	 * wake up before we take any other signal.
390	 * XXX this comment seems wrong.
391	 */
392	act.sa_handler = SIG_DFL;
393	sigemptyset(&act.sa_mask);
394	act.sa_flags = 0;
395	sigaction(signo, &act, NULL);
396
397	DEBUGF(JOB, ("JobPassSig passing signal to self, mask = %x.\n",
398	    ~0 & ~(1 << (signo - 1))));
399	signal(signo, SIG_DFL);
400
401	KILL(getpid(), signo);
402
403	signo = SIGCONT;
404	JobCondPassSig(signo);
405
406	sigprocmask(SIG_SETMASK, &omask, NULL);
407	sigprocmask(SIG_SETMASK, &omask, NULL);
408	act.sa_handler = JobPassSig;
409	sigaction(signo, &act, NULL);
410}
411
412/**
413 * JobPrintCommand  --
414 *	Put out another command for the given job. If the command starts
415 *	with an @ or a - we process it specially. In the former case,
416 *	so long as the -s and -n flags weren't given to make, we stick
417 *	a shell-specific echoOff command in the script. In the latter,
418 *	we ignore errors for the entire job, unless the shell has error
419 *	control.
420 *	If the command is just "..." we take all future commands for this
421 *	job to be commands to be executed once the entire graph has been
422 *	made and return non-zero to signal that the end of the commands
423 *	was reached. These commands are later attached to the postCommands
424 *	node and executed by Job_Finish when all things are done.
425 *	This function is called from JobStart via LST_FOREACH.
426 *
427 * Results:
428 *	Always 0, unless the command was "..."
429 *
430 * Side Effects:
431 *	If the command begins with a '-' and the shell has no error control,
432 *	the JOB_IGNERR flag is set in the job descriptor.
433 *	If the command is "..." and we're not ignoring such things,
434 *	tailCmds is set to the successor node of the cmd.
435 *	numCommands is incremented if the command is actually printed.
436 */
437static int
438JobPrintCommand(void *cmdp, void *jobp)
439{
440	Boolean	noSpecials;	/* true if we shouldn't worry about
441				 * inserting special commands into
442				 * the input stream. */
443	Boolean	shutUp = FALSE;	/* true if we put a no echo command
444				 * into the command file */
445	Boolean	errOff = FALSE;	/* true if we turned error checking
446				 * off before printing the command
447				 * and need to turn it back on */
448	const char *cmdTemplate;/* Template to use when printing the command */
449	char	*cmdStart;	/* Start of expanded command */
450	LstNode	*cmdNode;	/* Node for replacing the command */
451	char	*cmd = cmdp;
452	Job	*job = jobp;
453
454	noSpecials = (noExecute && !(job->node->type & OP_MAKE));
455
456	if (strcmp(cmd, "...") == 0) {
457		job->node->type |= OP_SAVE_CMDS;
458		if ((job->flags & JOB_IGNDOTS) == 0) {
459			job->tailCmds =
460			    Lst_Succ(Lst_Member(&job->node->commands, cmd));
461			return (1);
462		}
463		return (0);
464	}
465
466#define	DBPRINTF(fmt, arg)			\
467	DEBUGF(JOB, (fmt, arg));		\
468	fprintf(job->cmdFILE, fmt, arg);	\
469	fflush(job->cmdFILE);
470
471	numCommands += 1;
472
473	/*
474	 * For debugging, we replace each command with the result of expanding
475	 * the variables in the command.
476	 */
477	cmdNode = Lst_Member(&job->node->commands, cmd);
478
479	cmd = Buf_Peel(Var_Subst(NULL, cmd, job->node, FALSE));
480	cmdStart = cmd;
481
482	Lst_Replace(cmdNode, cmdStart);
483
484	cmdTemplate = "%s\n";
485
486	/*
487	 * Check for leading @', -' or +'s to control echoing, error checking,
488	 * and execution on -n.
489	 */
490	while (*cmd == '@' || *cmd == '-' || *cmd == '+') {
491		switch (*cmd) {
492
493		  case '@':
494			shutUp = DEBUG(LOUD) ? FALSE : TRUE;
495			break;
496
497		  case '-':
498			errOff = TRUE;
499			break;
500
501		  case '+':
502			if (noSpecials) {
503				/*
504				 * We're not actually exececuting anything...
505				 * but this one needs to be - use compat mode
506				 * just for it.
507				 */
508				Compat_RunCommand(cmdp, job->node);
509				return (0);
510			}
511			break;
512		}
513		cmd++;
514	}
515
516	while (isspace((unsigned char)*cmd))
517		cmd++;
518
519	if (shutUp) {
520		if (!(job->flags & JOB_SILENT) && !noSpecials &&
521		    commandShell->hasEchoCtl) {
522			DBPRINTF("%s\n", commandShell->echoOff);
523		} else {
524			shutUp = FALSE;
525		}
526	}
527
528	if (errOff) {
529		if (!(job->flags & JOB_IGNERR) && !noSpecials) {
530			if (commandShell->hasErrCtl) {
531				/*
532				 * We don't want the error-control commands
533				 * showing up either, so we turn off echoing
534				 * while executing them. We could put another
535				 * field in the shell structure to tell
536				 * JobDoOutput to look for this string too,
537				 * but why make it any more complex than
538				 * it already is?
539				 */
540				if (!(job->flags & JOB_SILENT) && !shutUp &&
541				    commandShell->hasEchoCtl) {
542					DBPRINTF("%s\n", commandShell->echoOff);
543					DBPRINTF("%s\n", commandShell->ignErr);
544					DBPRINTF("%s\n", commandShell->echoOn);
545				} else {
546					DBPRINTF("%s\n", commandShell->ignErr);
547				}
548			} else if (commandShell->ignErr &&
549			    (*commandShell->ignErr != '\0')) {
550				/*
551				 * The shell has no error control, so we need to
552				 * be weird to get it to ignore any errors from
553				 * the command. If echoing is turned on, we turn
554				 * it off and use the errCheck template to echo
555				 * the command. Leave echoing off so the user
556				 * doesn't see the weirdness we go through to
557				 * ignore errors. Set cmdTemplate to use the
558				 * weirdness instead of the simple "%s\n"
559				 * template.
560				 */
561				if (!(job->flags & JOB_SILENT) && !shutUp &&
562				    commandShell->hasEchoCtl) {
563					DBPRINTF("%s\n", commandShell->echoOff);
564					DBPRINTF(commandShell->errCheck, cmd);
565					shutUp = TRUE;
566				}
567				cmdTemplate = commandShell->ignErr;
568				/*
569				 * The error ignoration (hee hee) is already
570				 * taken care of by the ignErr template, so
571				 * pretend error checking is still on.
572				*/
573				errOff = FALSE;
574			} else {
575				errOff = FALSE;
576			}
577		} else {
578			errOff = FALSE;
579		}
580	}
581
582	DBPRINTF(cmdTemplate, cmd);
583
584	if (errOff) {
585		/*
586		 * If echoing is already off, there's no point in issuing the
587		 * echoOff command. Otherwise we issue it and pretend it was on
588		 * for the whole command...
589		 */
590		if (!shutUp && !(job->flags & JOB_SILENT) &&
591		    commandShell->hasEchoCtl) {
592			DBPRINTF("%s\n", commandShell->echoOff);
593			shutUp = TRUE;
594		}
595		DBPRINTF("%s\n", commandShell->errCheck);
596	}
597	if (shutUp) {
598		DBPRINTF("%s\n", commandShell->echoOn);
599	}
600	return (0);
601}
602
603/**
604 * JobClose --
605 *	Called to close both input and output pipes when a job is finished.
606 *
607 * Side Effects:
608 *	The file descriptors associated with the job are closed.
609 */
610static void
611JobClose(Job *job)
612{
613
614	if (usePipes) {
615#if !defined(USE_KQUEUE)
616		FD_CLR(job->inPipe, &outputs);
617#endif
618		if (job->outPipe != job->inPipe) {
619			close(job->outPipe);
620		}
621		JobDoOutput(job, TRUE);
622		close(job->inPipe);
623	} else {
624		close(job->outFd);
625		JobDoOutput(job, TRUE);
626	}
627}
628
629/**
630 * JobFinish  --
631 *	Do final processing for the given job including updating
632 *	parents and starting new jobs as available/necessary. Note
633 *	that we pay no attention to the JOB_IGNERR flag here.
634 *	This is because when we're called because of a noexecute flag
635 *	or something, jstat.w_status is 0 and when called from
636 *	Job_CatchChildren, the status is zeroed if it s/b ignored.
637 *
638 * Side Effects:
639 *	Some nodes may be put on the toBeMade queue.
640 *	Final commands for the job are placed on postCommands.
641 *
642 *	If we got an error and are aborting (aborting == ABORT_ERROR) and
643 *	the job list is now empty, we are done for the day.
644 *	If we recognized an error (errors !=0), we set the aborting flag
645 *	to ABORT_ERROR so no more jobs will be started.
646 */
647static void
648JobFinish(Job *job, int *status)
649{
650	Boolean	done;
651	LstNode	*ln;
652
653	if ((WIFEXITED(*status) &&
654	    (((WEXITSTATUS(*status) != 0) && !(job->flags & JOB_IGNERR)))) ||
655	    (WIFSIGNALED(*status) && (WTERMSIG(*status) != SIGCONT))) {
656		/*
657		 * If it exited non-zero and either we're doing things our
658		 * way or we're not ignoring errors, the job is finished.
659		 * Similarly, if the shell died because of a signal
660		 * the job is also finished. In these cases, finish out the
661		 * job's output before printing the exit status...
662		 */
663		JobClose(job);
664		if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
665			fclose(job->cmdFILE);
666		}
667		done = TRUE;
668
669	} else if (WIFEXITED(*status)) {
670		/*
671		 * Deal with ignored errors in -B mode. We need to print a
672		 * message telling of the ignored error as well as setting
673		 * status.w_status to 0 so the next command gets run. To do
674		 * this, we set done to be TRUE if in -B mode and the job
675		 * exited non-zero.
676		 */
677		done = WEXITSTATUS(*status) != 0;
678
679		/*
680		 * Old comment said: "Note we don't want to close down any of
681		 * the streams until we know we're at the end." But we do.
682		 * Otherwise when are we going to print the rest of the stuff?
683		 */
684		JobClose(job);
685	} else {
686		/*
687		 * No need to close things down or anything.
688		 */
689		done = FALSE;
690	}
691
692	if (done || WIFSTOPPED(*status) ||
693	    (WIFSIGNALED(*status) && (WTERMSIG(*status) == SIGCONT)) ||
694	    DEBUG(JOB)) {
695		FILE	*out;
696
697		if (compatMake && !usePipes && (job->flags & JOB_IGNERR)) {
698			/*
699			 * If output is going to a file and this job is ignoring
700			 * errors, arrange to have the exit status sent to the
701			 * output file as well.
702			 */
703			out = fdopen(job->outFd, "w");
704			if (out == NULL)
705				Punt("Cannot fdopen");
706		} else {
707			out = stdout;
708		}
709
710		if (WIFEXITED(*status)) {
711			DEBUGF(JOB, ("Process %d exited.\n", job->pid));
712			if (WEXITSTATUS(*status) != 0) {
713				if (usePipes && job->node != lastNode) {
714					MESSAGE(out, job->node);
715					lastNode = job->node;
716				}
717				fprintf(out, "*** Error code %d%s\n",
718				    WEXITSTATUS(*status),
719				    (job->flags & JOB_IGNERR) ?
720				    "(ignored)" : "");
721
722				if (job->flags & JOB_IGNERR) {
723					*status = 0;
724				}
725			} else if (DEBUG(JOB)) {
726				if (usePipes && job->node != lastNode) {
727					MESSAGE(out, job->node);
728					lastNode = job->node;
729				}
730				fprintf(out, "*** Completed successfully\n");
731			}
732
733		} else if (WIFSTOPPED(*status)) {
734			DEBUGF(JOB, ("Process %d stopped.\n", job->pid));
735			if (usePipes && job->node != lastNode) {
736				MESSAGE(out, job->node);
737				lastNode = job->node;
738			}
739			fprintf(out, "*** Stopped -- signal %d\n",
740			WSTOPSIG(*status));
741			job->flags |= JOB_RESUME;
742			Lst_AtEnd(&stoppedJobs, job);
743			fflush(out);
744			return;
745
746		} else if (WTERMSIG(*status) == SIGCONT) {
747			/*
748			 * If the beastie has continued, shift the Job from
749			 * the stopped list to the running one (or re-stop it
750			 * if concurrency is exceeded) and go and get another
751			 * child.
752			 */
753			if (job->flags & (JOB_RESUME|JOB_RESTART)) {
754				if (usePipes && job->node != lastNode) {
755					MESSAGE(out, job->node);
756					lastNode = job->node;
757				}
758				fprintf(out, "*** Continued\n");
759			}
760			if (!(job->flags & JOB_CONTINUING)) {
761				DEBUGF(JOB, ("Warning: process %d was not "
762				    "continuing.\n", job->pid));
763#ifdef notdef
764				/*
765				 * We don't really want to restart a job from
766				 * scratch just because it continued, especially
767				 * not without killing the continuing process!
768				 *  That's why this is ifdef'ed out.
769				 * FD - 9/17/90
770				 */
771				JobRestart(job);
772#endif
773			}
774			job->flags &= ~JOB_CONTINUING;
775			Lst_AtEnd(&jobs, job);
776			nJobs += 1;
777			DEBUGF(JOB, ("Process %d is continuing locally.\n",
778			    job->pid));
779			if (nJobs == maxJobs) {
780				jobFull = TRUE;
781				DEBUGF(JOB, ("Job queue is full.\n"));
782			}
783			fflush(out);
784			return;
785
786		} else {
787			if (usePipes && job->node != lastNode) {
788				MESSAGE(out, job->node);
789				lastNode = job->node;
790			}
791			fprintf(out, "*** Signal %d\n", WTERMSIG(*status));
792		}
793
794		fflush(out);
795	}
796
797	/*
798	 * Now handle the -B-mode stuff. If the beast still isn't finished,
799	 * try and restart the job on the next command. If JobStart says it's
800	 * ok, it's ok. If there's an error, this puppy is done.
801	 */
802	if (compatMake && WIFEXITED(*status) &&
803	    Lst_Succ(job->node->compat_command) != NULL) {
804		switch (JobStart(job->node, job->flags & JOB_IGNDOTS, job)) {
805		  case JOB_RUNNING:
806			done = FALSE;
807			break;
808		  case JOB_ERROR:
809			done = TRUE;
810			W_SETEXITSTATUS(status, 1);
811			break;
812		  case JOB_FINISHED:
813			/*
814			 * If we got back a JOB_FINISHED code, JobStart has
815			 * already called Make_Update and freed the job
816			 * descriptor. We set done to false here to avoid fake
817			 * cycles and double frees. JobStart needs to do the
818			 * update so we can proceed up the graph when given
819			 * the -n flag..
820			 */
821			done = FALSE;
822			break;
823		  default:
824			break;
825		}
826	} else {
827		done = TRUE;
828	}
829
830	if (done && (aborting != ABORT_ERROR) &&
831	    (aborting != ABORT_INTERRUPT) && (*status == 0)) {
832		/*
833		 * As long as we aren't aborting and the job didn't return a
834		 * non-zero status that we shouldn't ignore, we call
835		 * Make_Update to update the parents. In addition, any saved
836		 * commands for the node are placed on the .END target.
837		 */
838		for (ln = job->tailCmds; ln != NULL; ln = LST_NEXT(ln)) {
839			Lst_AtEnd(&postCommands->commands,
840			    Buf_Peel(Var_Subst(NULL, Lst_Datum(ln),
841			    job->node, FALSE)));
842		}
843
844		job->node->made = MADE;
845		Make_Update(job->node);
846		free(job);
847
848	} else if (*status != 0) {
849		errors += 1;
850		free(job);
851	}
852
853	JobRestartJobs();
854
855	/*
856	 * Set aborting if any error.
857	 */
858	if (errors && !keepgoing && (aborting != ABORT_INTERRUPT)) {
859		/*
860		 * If we found any errors in this batch of children and the -k
861		 * flag wasn't given, we set the aborting flag so no more jobs
862		 * get started.
863		 */
864		aborting = ABORT_ERROR;
865	}
866
867	if ((aborting == ABORT_ERROR) && Job_Empty()) {
868		/*
869		 * If we are aborting and the job table is now empty, we finish.
870		 */
871		Finish(errors);
872	}
873}
874
875/**
876 * Job_Touch
877 *	Touch the given target. Called by JobStart when the -t flag was
878 *	given.  Prints messages unless told to be silent.
879 *
880 * Side Effects:
881 *	The data modification of the file is changed. In addition, if the
882 *	file did not exist, it is created.
883 */
884void
885Job_Touch(GNode *gn, Boolean silent)
886{
887	int	streamID;	/* ID of stream opened to do the touch */
888	struct utimbuf times;	/* Times for utime() call */
889
890	if (gn->type & (OP_JOIN | OP_USE | OP_EXEC | OP_OPTIONAL)) {
891		/*
892		 * .JOIN, .USE, .ZEROTIME and .OPTIONAL targets are "virtual"
893		 * targets and, as such, shouldn't really be created.
894		 */
895		return;
896	}
897
898	if (!silent) {
899		fprintf(stdout, "touch %s\n", gn->name);
900		fflush(stdout);
901	}
902
903	if (noExecute) {
904		return;
905	}
906
907	if (gn->type & OP_ARCHV) {
908		Arch_Touch(gn);
909	} else if (gn->type & OP_LIB) {
910		Arch_TouchLib(gn);
911	} else {
912		char	*file = gn->path ? gn->path : gn->name;
913
914		times.actime = times.modtime = now;
915		if (utime(file, &times) < 0) {
916			streamID = open(file, O_RDWR | O_CREAT, 0666);
917
918			if (streamID >= 0) {
919				char	c;
920
921				/*
922				 * Read and write a byte to the file to change
923				 * the modification time, then close the file.
924				 */
925				if (read(streamID, &c, 1) == 1) {
926					lseek(streamID, (off_t)0, SEEK_SET);
927					write(streamID, &c, 1);
928				}
929
930				close(streamID);
931			} else {
932				fprintf(stdout, "*** couldn't touch %s: %s",
933				    file, strerror(errno));
934				fflush(stdout);
935			}
936		}
937	}
938}
939
940/**
941 * Job_CheckCommands
942 *	Make sure the given node has all the commands it needs.
943 *
944 * Results:
945 *	TRUE if the commands list is/was ok.
946 *
947 * Side Effects:
948 *	The node will have commands from the .DEFAULT rule added to it
949 *	if it needs them.
950 */
951Boolean
952Job_CheckCommands(GNode *gn, void (*abortProc)(const char *, ...))
953{
954
955	if (OP_NOP(gn->type) && Lst_IsEmpty(&gn->commands) &&
956	    (gn->type & OP_LIB) == 0) {
957		/*
958		 * No commands. Look for .DEFAULT rule from which we might infer
959		 * commands.
960		 */
961		if ((DEFAULT != NULL) && !Lst_IsEmpty(&DEFAULT->commands)) {
962			char *p1;
963			/*
964			 * Make only looks for a .DEFAULT if the node was
965			 * never the target of an operator, so that's what we
966			 * do too. If a .DEFAULT was given, we substitute its
967			 * commands for gn's commands and set the IMPSRC
968			 * variable to be the target's name The DEFAULT node
969			 * acts like a transformation rule, in that gn also
970			 * inherits any attributes or sources attached to
971			 * .DEFAULT itself.
972			 */
973			Make_HandleUse(DEFAULT, gn);
974			Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), gn);
975			free(p1);
976
977		} else if (Dir_MTime(gn) == 0) {
978			/*
979			 * The node wasn't the target of an operator we have
980			 * no .DEFAULT rule to go on and the target doesn't
981			 * already exist. There's nothing more we can do for
982			 * this branch. If the -k flag wasn't given, we stop
983			 * in our tracks, otherwise we just don't update
984			 * this node's parents so they never get examined.
985			 */
986			static const char msg[] =
987			    "make: don't know how to make";
988
989			if (gn->type & OP_OPTIONAL) {
990				fprintf(stdout, "%s %s(ignored)\n",
991				    msg, gn->name);
992				fflush(stdout);
993			} else if (keepgoing) {
994				fprintf(stdout, "%s %s(continuing)\n",
995				    msg, gn->name);
996				fflush(stdout);
997				return (FALSE);
998			} else {
999#if OLD_JOKE
1000				if (strcmp(gn->name,"love") == 0)
1001					(*abortProc)("Not war.");
1002				else
1003#endif
1004					(*abortProc)("%s %s. Stop",
1005					    msg, gn->name);
1006				return (FALSE);
1007			}
1008		}
1009	}
1010	return (TRUE);
1011}
1012
1013/**
1014 * JobExec
1015 *	Execute the shell for the given job. Called from JobStart and
1016 *	JobRestart.
1017 *
1018 * Side Effects:
1019 *	A shell is executed, outputs is altered and the Job structure added
1020 *	to the job table.
1021 */
1022static void
1023JobExec(Job *job, char **argv)
1024{
1025	int	    	  cpid;	    	/* ID of new child */
1026
1027	if (DEBUG(JOB)) {
1028		int 	  i;
1029
1030		DEBUGF(JOB, ("Running %s\n", job->node->name));
1031		DEBUGF(JOB, ("\tCommand: "));
1032		for (i = 0; argv[i] != NULL; i++) {
1033			DEBUGF(JOB, ("%s ", argv[i]));
1034		}
1035		DEBUGF(JOB, ("\n"));
1036	}
1037
1038	/*
1039	 * Some jobs produce no output and it's disconcerting to have
1040	 * no feedback of their running (since they produce no output, the
1041	 * banner with their name in it never appears). This is an attempt to
1042	 * provide that feedback, even if nothing follows it.
1043	 */
1044	if ((lastNode != job->node) && (job->flags & JOB_FIRST) &&
1045	    !(job->flags & JOB_SILENT)) {
1046		MESSAGE(stdout, job->node);
1047		lastNode = job->node;
1048	}
1049
1050	if ((cpid = vfork()) == -1) {
1051		Punt("Cannot fork");
1052
1053	} else if (cpid == 0) {
1054		if (fifoFd >= 0)
1055			close(fifoFd);
1056
1057		/*
1058		 * Must duplicate the input stream down to the child's input and
1059		 * reset it to the beginning (again). Since the stream was
1060		 * marked close-on-exec, we must clear that bit in the new
1061		 * input.
1062		 */
1063		if (dup2(FILENO(job->cmdFILE), 0) == -1)
1064			Punt("Cannot dup2: %s", strerror(errno));
1065		fcntl(0, F_SETFD, 0);
1066		lseek(0, (off_t)0, SEEK_SET);
1067
1068		if (usePipes) {
1069			/*
1070			 * Set up the child's output to be routed through the
1071			 * pipe we've created for it.
1072			 */
1073			if (dup2(job->outPipe, 1) == -1)
1074				Punt("Cannot dup2: %s", strerror(errno));
1075		} else {
1076			/*
1077			 * We're capturing output in a file, so we duplicate the
1078			 * descriptor to the temporary file into the standard
1079			 * output.
1080			 */
1081			if (dup2(job->outFd, 1) == -1)
1082				Punt("Cannot dup2: %s", strerror(errno));
1083		}
1084		/*
1085		 * The output channels are marked close on exec. This bit was
1086		 * duplicated by the dup2 (on some systems), so we have to clear
1087		 * it before routing the shell's error output to the same place
1088		 * as its standard output.
1089		 */
1090		fcntl(1, F_SETFD, 0);
1091		if (dup2(1, 2) == -1)
1092			Punt("Cannot dup2: %s", strerror(errno));
1093
1094#ifdef USE_PGRP
1095		/*
1096		 * We want to switch the child into a different process family
1097		 * so we can kill it and all its descendants in one fell swoop,
1098		 * by killing its process family, but not commit suicide.
1099		 */
1100# if defined(SYSV)
1101		setsid();
1102# else
1103		setpgid(0, getpid());
1104# endif
1105#endif /* USE_PGRP */
1106
1107		execv(shellPath, argv);
1108
1109		write(STDERR_FILENO, "Could not execute shell\n",
1110		    sizeof("Could not execute shell"));
1111		_exit(1);
1112
1113	} else {
1114		job->pid = cpid;
1115
1116		if (usePipes && (job->flags & JOB_FIRST)) {
1117			/*
1118			 * The first time a job is run for a node, we set the
1119			 * current position in the buffer to the beginning and
1120			 * mark another stream to watch in the outputs mask.
1121			 */
1122#ifdef USE_KQUEUE
1123			struct kevent	kev[2];
1124#endif
1125			job->curPos = 0;
1126
1127#if defined(USE_KQUEUE)
1128			EV_SET(&kev[0], job->inPipe, EVFILT_READ,
1129			    EV_ADD, 0, 0, job);
1130			EV_SET(&kev[1], job->pid, EVFILT_PROC,
1131			    EV_ADD | EV_ONESHOT, NOTE_EXIT, 0, NULL);
1132			if (kevent(kqfd, kev, 2, NULL, 0, NULL) != 0) {
1133				/*
1134				 * kevent() will fail if the job is already
1135				 * finished
1136				 */
1137				if (errno != EINTR && errno != EBADF &&
1138				    errno != ESRCH)
1139					Punt("kevent: %s", strerror(errno));
1140			}
1141#else
1142			FD_SET(job->inPipe, &outputs);
1143#endif /* USE_KQUEUE */
1144		}
1145
1146		if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
1147			fclose(job->cmdFILE);
1148			job->cmdFILE = NULL;
1149		}
1150	}
1151
1152	/*
1153	 * Now the job is actually running, add it to the table.
1154	 */
1155	nJobs += 1;
1156	Lst_AtEnd(&jobs, job);
1157	if (nJobs == maxJobs) {
1158		jobFull = TRUE;
1159	}
1160}
1161
1162/**
1163 * JobMakeArgv
1164 *	Create the argv needed to execute the shell for a given job.
1165 */
1166static void
1167JobMakeArgv(Job *job, char **argv)
1168{
1169	int		argc;
1170	static char	args[10];	/* For merged arguments */
1171
1172	argv[0] = shellName;
1173	argc = 1;
1174
1175	if ((commandShell->exit && (*commandShell->exit != '-')) ||
1176	    (commandShell->echo && (*commandShell->echo != '-'))) {
1177		/*
1178		 * At least one of the flags doesn't have a minus before it, so
1179		 * merge them together. Have to do this because the *(&(@*#*&#$#
1180		 * Bourne shell thinks its second argument is a file to source.
1181		 * Grrrr. Note the ten-character limitation on the combined
1182		 * arguments.
1183		 */
1184		sprintf(args, "-%s%s", ((job->flags & JOB_IGNERR) ? "" :
1185		    (commandShell->exit ? commandShell->exit : "")),
1186		    ((job->flags & JOB_SILENT) ? "" :
1187		    (commandShell->echo ? commandShell->echo : "")));
1188
1189		if (args[1]) {
1190			argv[argc] = args;
1191			argc++;
1192		}
1193	} else {
1194		if (!(job->flags & JOB_IGNERR) && commandShell->exit) {
1195			argv[argc] = commandShell->exit;
1196			argc++;
1197		}
1198		if (!(job->flags & JOB_SILENT) && commandShell->echo) {
1199			argv[argc] = commandShell->echo;
1200			argc++;
1201		}
1202	}
1203	argv[argc] = NULL;
1204}
1205
1206/**
1207 * JobRestart
1208 *	Restart a job that stopped for some reason.
1209 *
1210 * Side Effects:
1211 *	jobFull will be set if the job couldn't be run.
1212 */
1213static void
1214JobRestart(Job *job)
1215{
1216
1217	if (job->flags & JOB_RESTART) {
1218		/*
1219		 * Set up the control arguments to the shell. This is based on
1220		 * the flags set earlier for this job. If the JOB_IGNERR flag
1221		 * is clear, the 'exit' flag of the commandShell is used to
1222		 * cause it to exit upon receiving an error. If the JOB_SILENT
1223		 * flag is clear, the 'echo' flag of the commandShell is used
1224		 * to get it to start echoing as soon as it starts
1225		 * processing commands.
1226		 */
1227		char	*argv[4];
1228
1229		JobMakeArgv(job, argv);
1230
1231		DEBUGF(JOB, ("Restarting %s...", job->node->name));
1232		if (((nJobs >= maxJobs) && !(job->flags & JOB_SPECIAL))) {
1233			/*
1234			 * Can't be exported and not allowed to run locally --
1235			 * put it back on the hold queue and mark the table full
1236			 */
1237			DEBUGF(JOB, ("holding\n"));
1238			Lst_AtFront(&stoppedJobs, (void *)job);
1239			jobFull = TRUE;
1240			DEBUGF(JOB, ("Job queue is full.\n"));
1241			return;
1242		} else {
1243			/*
1244			 * Job may be run locally.
1245			 */
1246			DEBUGF(JOB, ("running locally\n"));
1247		}
1248		JobExec(job, argv);
1249
1250	} else {
1251		/*
1252		 * The job has stopped and needs to be restarted.
1253		 * Why it stopped, we don't know...
1254		 */
1255		DEBUGF(JOB, ("Resuming %s...", job->node->name));
1256		if (((nJobs < maxJobs) || ((job->flags & JOB_SPECIAL) &&
1257		    (maxJobs == 0))) && (nJobs != maxJobs)) {
1258			/*
1259			 * If we haven't reached the concurrency limit already
1260			 * (or the job must be run and maxJobs is 0), it's ok
1261			 * to resume it.
1262			 */
1263			Boolean error;
1264			int status;
1265
1266			error = (KILL(job->pid, SIGCONT) != 0);
1267
1268			if (!error) {
1269				/*
1270				 * Make sure the user knows we've continued
1271				 * the beast and actually put the thing in the
1272				 * job table.
1273				 */
1274				job->flags |= JOB_CONTINUING;
1275				status = 0;
1276				W_SETTERMSIG(&status, SIGCONT);
1277				JobFinish(job, &status);
1278
1279				job->flags &= ~(JOB_RESUME|JOB_CONTINUING);
1280				DEBUGF(JOB, ("done\n"));
1281			} else {
1282				Error("couldn't resume %s: %s",
1283				job->node->name, strerror(errno));
1284				status = 0;
1285				W_SETEXITSTATUS(&status, 1);
1286				JobFinish(job, &status);
1287			}
1288		} else {
1289			/*
1290			* Job cannot be restarted. Mark the table as full and
1291			* place the job back on the list of stopped jobs.
1292			*/
1293			DEBUGF(JOB, ("table full\n"));
1294			Lst_AtFront(&stoppedJobs, (void *)job);
1295			jobFull = TRUE;
1296			DEBUGF(JOB, ("Job queue is full.\n"));
1297		}
1298	}
1299}
1300
1301/**
1302 * JobStart
1303 *	Start a target-creation process going for the target described
1304 *	by the graph node gn.
1305 *
1306 * Results:
1307 *	JOB_ERROR if there was an error in the commands, JOB_FINISHED
1308 *	if there isn't actually anything left to do for the job and
1309 *	JOB_RUNNING if the job has been started.
1310 *
1311 * Side Effects:
1312 *	A new Job node is created and added to the list of running
1313 *	jobs. PMake is forked and a child shell created.
1314 */
1315static int
1316JobStart(GNode *gn, int flags, Job *previous)
1317{
1318	Job	*job;		/* new job descriptor */
1319	char	*argv[4];	/* Argument vector to shell */
1320	Boolean	cmdsOK;		/* true if the nodes commands were all right */
1321	Boolean	noExec;		/* Set true if we decide not to run the job */
1322	int	tfd;		/* File descriptor for temp file */
1323	LstNode	*ln;
1324
1325	if (interrupted) {
1326		JobPassSig(interrupted);
1327		return (JOB_ERROR);
1328	}
1329	if (previous != NULL) {
1330		previous->flags &= ~(JOB_FIRST | JOB_IGNERR | JOB_SILENT);
1331		job = previous;
1332	} else {
1333		job = emalloc(sizeof(Job));
1334		flags |= JOB_FIRST;
1335	}
1336
1337	job->node = gn;
1338	job->tailCmds = NULL;
1339
1340	/*
1341	 * Set the initial value of the flags for this job based on the global
1342	 * ones and the node's attributes... Any flags supplied by the caller
1343	 * are also added to the field.
1344	 */
1345	job->flags = 0;
1346	if (Targ_Ignore(gn)) {
1347		job->flags |= JOB_IGNERR;
1348	}
1349	if (Targ_Silent(gn)) {
1350		job->flags |= JOB_SILENT;
1351	}
1352	job->flags |= flags;
1353
1354	/*
1355	 * Check the commands now so any attributes from .DEFAULT have a chance
1356	 * to migrate to the node.
1357	 */
1358	if (!compatMake && job->flags & JOB_FIRST) {
1359		cmdsOK = Job_CheckCommands(gn, Error);
1360	} else {
1361		cmdsOK = TRUE;
1362	}
1363
1364	/*
1365	 * If the -n flag wasn't given, we open up OUR (not the child's)
1366	 * temporary file to stuff commands in it. The thing is rd/wr so we
1367	 * don't need to reopen it to feed it to the shell. If the -n flag
1368	 * *was* given, we just set the file to be stdout. Cute, huh?
1369	 */
1370	if ((gn->type & OP_MAKE) || (!noExecute && !touchFlag)) {
1371		/*
1372		 * We're serious here, but if the commands were bogus, we're
1373		 * also dead...
1374		 */
1375		if (!cmdsOK) {
1376			DieHorribly();
1377		}
1378
1379		strcpy(tfile, TMPPAT);
1380		if ((tfd = mkstemp(tfile)) == -1)
1381			Punt("Cannot create temp file: %s", strerror(errno));
1382		job->cmdFILE = fdopen(tfd, "w+");
1383		eunlink(tfile);
1384		if (job->cmdFILE == NULL) {
1385			close(tfd);
1386			Punt("Could not open %s", tfile);
1387		}
1388		fcntl(FILENO(job->cmdFILE), F_SETFD, 1);
1389		/*
1390		 * Send the commands to the command file, flush all its
1391		 * buffers then rewind and remove the thing.
1392		 */
1393		noExec = FALSE;
1394
1395		/*
1396		 * Used to be backwards; replace when start doing multiple
1397		 * commands per shell.
1398		 */
1399		if (compatMake) {
1400			/*
1401			 * Be compatible: If this is the first time for this
1402			 * node, verify its commands are ok and open the
1403			 * commands list for sequential access by later
1404			 * invocations of JobStart. Once that is done, we take
1405			 * the next command off the list and print it to the
1406			 * command file. If the command was an ellipsis, note
1407			 * that there's nothing more to execute.
1408			 */
1409			if (job->flags & JOB_FIRST)
1410				gn->compat_command = Lst_First(&gn->commands);
1411			else
1412				gn->compat_command =
1413				    Lst_Succ(gn->compat_command);
1414
1415			if (gn->compat_command == NULL ||
1416			    JobPrintCommand(Lst_Datum(gn->compat_command), job))
1417				noExec = TRUE;
1418
1419			if (noExec && !(job->flags & JOB_FIRST)) {
1420				/*
1421				 * If we're not going to execute anything, the
1422				 * job is done and we need to close down the
1423				 * various file descriptors we've opened for
1424				 * output, then call JobDoOutput to catch the
1425				 * final characters or send the file to the
1426				 * screen... Note that the i/o streams are only
1427				 * open if this isn't the first job. Note also
1428				 * that this could not be done in
1429				 * Job_CatchChildren b/c it wasn't clear if
1430				 * there were more commands to execute or not...
1431				 */
1432				JobClose(job);
1433			}
1434		} else {
1435			/*
1436			 * We can do all the commands at once. hooray for sanity
1437			 */
1438			numCommands = 0;
1439			LST_FOREACH(ln, &gn->commands) {
1440				if (JobPrintCommand(Lst_Datum(ln), job))
1441					break;
1442			}
1443
1444			/*
1445			 * If we didn't print out any commands to the shell
1446			 * script, there's not much point in executing the
1447			 * shell, is there?
1448			 */
1449			if (numCommands == 0) {
1450				noExec = TRUE;
1451			}
1452		}
1453
1454	} else if (noExecute) {
1455		/*
1456		 * Not executing anything -- just print all the commands to
1457		 * stdout in one fell swoop. This will still set up
1458		 * job->tailCmds correctly.
1459		 */
1460		if (lastNode != gn) {
1461			MESSAGE(stdout, gn);
1462			lastNode = gn;
1463		}
1464		job->cmdFILE = stdout;
1465
1466		/*
1467		 * Only print the commands if they're ok, but don't die if
1468		 * they're not -- just let the user know they're bad and keep
1469		 * going. It doesn't do any harm in this case and may do
1470		 * some good.
1471		 */
1472		if (cmdsOK) {
1473			LST_FOREACH(ln, &gn->commands) {
1474				if (JobPrintCommand(Lst_Datum(ln), job))
1475					break;
1476			}
1477		}
1478		/*
1479		* Don't execute the shell, thank you.
1480		*/
1481		noExec = TRUE;
1482
1483	} else {
1484		/*
1485		 * Just touch the target and note that no shell should be
1486		 * executed. Set cmdFILE to stdout to make life easier. Check
1487		 * the commands, too, but don't die if they're no good -- it
1488		 * does no harm to keep working up the graph.
1489		 */
1490		job->cmdFILE = stdout;
1491		Job_Touch(gn, job->flags & JOB_SILENT);
1492		noExec = TRUE;
1493	}
1494
1495	/*
1496	 * If we're not supposed to execute a shell, don't.
1497	 */
1498	if (noExec) {
1499		/*
1500		 * Unlink and close the command file if we opened one
1501		 */
1502		if (job->cmdFILE != stdout) {
1503			if (job->cmdFILE != NULL)
1504				fclose(job->cmdFILE);
1505		} else {
1506			fflush(stdout);
1507		}
1508
1509		/*
1510		 * We only want to work our way up the graph if we aren't here
1511		 * because the commands for the job were no good.
1512		*/
1513		if (cmdsOK) {
1514			if (aborting == 0) {
1515				for (ln = job->tailCmds; ln != NULL;
1516				    ln = LST_NEXT(ln)) {
1517					Lst_AtEnd(&postCommands->commands,
1518					    Buf_Peel(Var_Subst(NULL,
1519					    Lst_Datum(ln), job->node, FALSE)));
1520				}
1521				job->node->made = MADE;
1522				Make_Update(job->node);
1523			}
1524			free(job);
1525			return(JOB_FINISHED);
1526		} else {
1527			free(job);
1528			return(JOB_ERROR);
1529		}
1530	} else {
1531		fflush(job->cmdFILE);
1532	}
1533
1534	/*
1535	 * Set up the control arguments to the shell. This is based on the flags
1536	 * set earlier for this job.
1537	 */
1538	JobMakeArgv(job, argv);
1539
1540	/*
1541	 * If we're using pipes to catch output, create the pipe by which we'll
1542	 * get the shell's output. If we're using files, print out that we're
1543	 * starting a job and then set up its temporary-file name.
1544	 */
1545	if (!compatMake || (job->flags & JOB_FIRST)) {
1546		if (usePipes) {
1547			int fd[2];
1548
1549			if (pipe(fd) == -1)
1550				Punt("Cannot create pipe: %s", strerror(errno));
1551			job->inPipe = fd[0];
1552			job->outPipe = fd[1];
1553			fcntl(job->inPipe, F_SETFD, 1);
1554			fcntl(job->outPipe, F_SETFD, 1);
1555		} else {
1556			fprintf(stdout, "Remaking `%s'\n", gn->name);
1557			fflush(stdout);
1558			strcpy(job->outFile, TMPPAT);
1559			if ((job->outFd = mkstemp(job->outFile)) == -1)
1560				Punt("cannot create temp file: %s",
1561				    strerror(errno));
1562			fcntl(job->outFd, F_SETFD, 1);
1563		}
1564	}
1565
1566	if ((nJobs >= maxJobs) && !(job->flags & JOB_SPECIAL) && (maxJobs != 0)) {
1567		/*
1568		 * We've hit the limit of concurrency, so put the job on hold
1569		 * until some other job finishes. Note that the special jobs
1570		 * (.BEGIN, .INTERRUPT and .END) may be run even when the
1571		 * limit has been reached (e.g. when maxJobs == 0).
1572		 */
1573		jobFull = TRUE;
1574
1575		DEBUGF(JOB, ("Can only run job locally.\n"));
1576		job->flags |= JOB_RESTART;
1577		Lst_AtEnd(&stoppedJobs, job);
1578	} else {
1579		if (nJobs >= maxJobs) {
1580			/*
1581			 * If we're running this job locally as a special case
1582			 * (see above), at least say the table is full.
1583			 */
1584			jobFull = TRUE;
1585			DEBUGF(JOB, ("Local job queue is full.\n"));
1586		}
1587		JobExec(job, argv);
1588	}
1589	return (JOB_RUNNING);
1590}
1591
1592static char *
1593JobOutput(Job *job, char *cp, char *endp, int msg)
1594{
1595	char *ecp;
1596
1597	if (commandShell->noPrint) {
1598		ecp = strstr(cp, commandShell->noPrint);
1599		while (ecp != NULL) {
1600			if (cp != ecp) {
1601				*ecp = '\0';
1602				if (msg && job->node != lastNode) {
1603					MESSAGE(stdout, job->node);
1604					lastNode = job->node;
1605				}
1606				/*
1607				 * The only way there wouldn't be a newline
1608				 * after this line is if it were the last in
1609				 * the buffer. However, since the non-printable
1610				 * comes after it, there must be a newline, so
1611				 * we don't print one.
1612				 */
1613				fprintf(stdout, "%s", cp);
1614				fflush(stdout);
1615			}
1616			cp = ecp + commandShell->noPLen;
1617			if (cp != endp) {
1618				/*
1619				 * Still more to print, look again after
1620				 * skipping the whitespace following the
1621				 * non-printable command....
1622				 */
1623				cp++;
1624				while (*cp == ' ' || *cp == '\t' ||
1625				    *cp == '\n') {
1626					cp++;
1627				}
1628				ecp = strstr(cp, commandShell->noPrint);
1629			} else {
1630				return (cp);
1631			}
1632		}
1633	}
1634	return (cp);
1635}
1636
1637/**
1638 * JobDoOutput
1639 *	This function is called at different times depending on
1640 *	whether the user has specified that output is to be collected
1641 *	via pipes or temporary files. In the former case, we are called
1642 *	whenever there is something to read on the pipe. We collect more
1643 *	output from the given job and store it in the job's outBuf. If
1644 *	this makes up a line, we print it tagged by the job's identifier,
1645 *	as necessary.
1646 *	If output has been collected in a temporary file, we open the
1647 *	file and read it line by line, transfering it to our own
1648 *	output channel until the file is empty. At which point we
1649 *	remove the temporary file.
1650 *	In both cases, however, we keep our figurative eye out for the
1651 *	'noPrint' line for the shell from which the output came. If
1652 *	we recognize a line, we don't print it. If the command is not
1653 *	alone on the line (the character after it is not \0 or \n), we
1654 *	do print whatever follows it.
1655 *
1656 * Side Effects:
1657 *	curPos may be shifted as may the contents of outBuf.
1658 */
1659STATIC void
1660JobDoOutput(Job *job, Boolean finish)
1661{
1662	Boolean	gotNL = FALSE;	/* true if got a newline */
1663	Boolean	fbuf;		/* true if our buffer filled up */
1664	int	nr;		/* number of bytes read */
1665	int	i;		/* auxiliary index into outBuf */
1666	int	max;		/* limit for i (end of current data) */
1667	int	nRead;		/* (Temporary) number of bytes read */
1668	FILE	*oFILE;		/* Stream pointer to shell's output file */
1669	char	inLine[132];
1670
1671	if (usePipes) {
1672		/*
1673		 * Read as many bytes as will fit in the buffer.
1674		 */
1675  end_loop:
1676		gotNL = FALSE;
1677		fbuf = FALSE;
1678
1679		nRead = read(job->inPipe, &job->outBuf[job->curPos],
1680		    JOB_BUFSIZE - job->curPos);
1681		/*
1682		 * Check for interrupt here too, because the above read may
1683		 * block when the child process is stopped. In this case the
1684		 * interrupt will unblock it (we don't use SA_RESTART).
1685		 */
1686		if (interrupted)
1687			JobPassSig(interrupted);
1688
1689		if (nRead < 0) {
1690			DEBUGF(JOB, ("JobDoOutput(piperead)"));
1691			nr = 0;
1692		} else {
1693			nr = nRead;
1694		}
1695
1696		/*
1697		 * If we hit the end-of-file (the job is dead), we must flush
1698		 * its remaining output, so pretend we read a newline if
1699		 * there's any output remaining in the buffer.
1700		 * Also clear the 'finish' flag so we stop looping.
1701		 */
1702		if ((nr == 0) && (job->curPos != 0)) {
1703			job->outBuf[job->curPos] = '\n';
1704			nr = 1;
1705			finish = FALSE;
1706		} else if (nr == 0) {
1707			finish = FALSE;
1708		}
1709
1710		/*
1711		 * Look for the last newline in the bytes we just got. If there
1712		 * is one, break out of the loop with 'i' as its index and
1713		 * gotNL set TRUE.
1714		*/
1715		max = job->curPos + nr;
1716		for (i = job->curPos + nr - 1; i >= job->curPos; i--) {
1717			if (job->outBuf[i] == '\n') {
1718				gotNL = TRUE;
1719				break;
1720			} else if (job->outBuf[i] == '\0') {
1721				/*
1722				 * Why?
1723				 */
1724				job->outBuf[i] = ' ';
1725			}
1726		}
1727
1728		if (!gotNL) {
1729			job->curPos += nr;
1730			if (job->curPos == JOB_BUFSIZE) {
1731				/*
1732				 * If we've run out of buffer space, we have
1733				 * no choice but to print the stuff. sigh.
1734				 */
1735				fbuf = TRUE;
1736				i = job->curPos;
1737			}
1738		}
1739		if (gotNL || fbuf) {
1740			/*
1741			 * Need to send the output to the screen. Null terminate
1742			 * it first, overwriting the newline character if there
1743			 * was one. So long as the line isn't one we should
1744			 * filter (according to the shell description), we print
1745			 * the line, preceded by a target banner if this target
1746			 * isn't the same as the one for which we last printed
1747			 * something. The rest of the data in the buffer are
1748			 * then shifted down to the start of the buffer and
1749			 * curPos is set accordingly.
1750			 */
1751			job->outBuf[i] = '\0';
1752			if (i >= job->curPos) {
1753				char *cp;
1754
1755				cp = JobOutput(job, job->outBuf,
1756				    &job->outBuf[i], FALSE);
1757
1758				/*
1759				 * There's still more in that buffer. This time,
1760				 * though, we know there's no newline at the
1761				 * end, so we add one of our own free will.
1762				 */
1763				if (*cp != '\0') {
1764					if (job->node != lastNode) {
1765						MESSAGE(stdout, job->node);
1766						lastNode = job->node;
1767					}
1768					fprintf(stdout, "%s%s", cp,
1769					    gotNL ? "\n" : "");
1770					fflush(stdout);
1771				}
1772			}
1773			if (i < max - 1) {
1774				/* shift the remaining characters down */
1775				memcpy(job->outBuf, &job->outBuf[i + 1],
1776				    max - (i + 1));
1777				job->curPos = max - (i + 1);
1778
1779			} else {
1780				/*
1781				 * We have written everything out, so we just
1782				 * start over from the start of the buffer.
1783				 * No copying. No nothing.
1784				 */
1785				job->curPos = 0;
1786			}
1787		}
1788		if (finish) {
1789			/*
1790			 * If the finish flag is true, we must loop until we hit
1791			 * end-of-file on the pipe. This is guaranteed to happen
1792			 * eventually since the other end of the pipe is now
1793			 * closed (we closed it explicitly and the child has
1794			 * exited). When we do get an EOF, finish will be set
1795			 * FALSE and we'll fall through and out.
1796			 */
1797			goto end_loop;
1798		}
1799
1800	} else {
1801		/*
1802		 * We've been called to retrieve the output of the job from the
1803		 * temporary file where it's been squirreled away. This consists
1804		 * of opening the file, reading the output line by line, being
1805		 * sure not to print the noPrint line for the shell we used,
1806		 * then close and remove the temporary file. Very simple.
1807		 *
1808		 * Change to read in blocks and do FindSubString type things
1809		 * as for pipes? That would allow for "@echo -n..."
1810		 */
1811		oFILE = fopen(job->outFile, "r");
1812		if (oFILE != NULL) {
1813			fprintf(stdout, "Results of making %s:\n",
1814			    job->node->name);
1815			fflush(stdout);
1816
1817			while (fgets(inLine, sizeof(inLine), oFILE) != NULL) {
1818				char	*cp, *endp, *oendp;
1819
1820				cp = inLine;
1821				oendp = endp = inLine + strlen(inLine);
1822				if (endp[-1] == '\n') {
1823					*--endp = '\0';
1824				}
1825				cp = JobOutput(job, inLine, endp, FALSE);
1826
1827				/*
1828				 * There's still more in that buffer. This time,
1829				 * though, we know there's no newline at the
1830				 * end, so we add one of our own free will.
1831				 */
1832				fprintf(stdout, "%s", cp);
1833				fflush(stdout);
1834				if (endp != oendp) {
1835					fprintf(stdout, "\n");
1836					fflush(stdout);
1837				}
1838			}
1839			fclose(oFILE);
1840			eunlink(job->outFile);
1841		}
1842	}
1843}
1844
1845/**
1846 * Job_CatchChildren
1847 *	Handle the exit of a child. Called from Make_Make.
1848 *
1849 * Side Effects:
1850 *	The job descriptor is removed from the list of children.
1851 *
1852 * Notes:
1853 *	We do waits, blocking or not, according to the wisdom of our
1854 *	caller, until there are no more children to report. For each
1855 *	job, call JobFinish to finish things off. This will take care of
1856 *	putting jobs on the stoppedJobs queue.
1857 */
1858void
1859Job_CatchChildren(Boolean block)
1860{
1861	int	pid;	/* pid of dead child */
1862	Job	*job;	/* job descriptor for dead child */
1863	LstNode	*jnode;	/* list element for finding job */
1864	int	status;	/* Exit/termination status */
1865
1866	/*
1867	 * Don't even bother if we know there's no one around.
1868	 */
1869	if (nJobs == 0) {
1870		return;
1871	}
1872
1873	for (;;) {
1874		pid = waitpid((pid_t)-1, &status,
1875		    (block ? 0 : WNOHANG) | WUNTRACED);
1876		if (pid <= 0)
1877			break;
1878
1879		DEBUGF(JOB, ("Process %d exited or stopped.\n", pid));
1880
1881		LST_FOREACH(jnode, &jobs) {
1882			if (((const Job *)Lst_Datum(jnode))->pid == pid)
1883				break;
1884		}
1885
1886		if (jnode == NULL) {
1887			if (WIFSIGNALED(status) &&
1888			    (WTERMSIG(status) == SIGCONT)) {
1889				LST_FOREACH(jnode, &stoppedJobs) {
1890					if (((const Job *)Lst_Datum(jnode))->pid == pid)
1891						break;
1892				}
1893				if (jnode == NULL) {
1894					Error("Resumed child (%d) not in table",
1895					    pid);
1896					continue;
1897				}
1898				job = Lst_Datum(jnode);
1899				Lst_Remove(&stoppedJobs, jnode);
1900			} else {
1901				Error("Child (%d) not in table?", pid);
1902				continue;
1903			}
1904		} else {
1905			job = Lst_Datum(jnode);
1906			Lst_Remove(&jobs, jnode);
1907			nJobs -= 1;
1908			if (fifoFd >= 0 && maxJobs > 1) {
1909				write(fifoFd, "+", 1);
1910				maxJobs--;
1911				if (nJobs >= maxJobs)
1912					jobFull = TRUE;
1913				else
1914					jobFull = FALSE;
1915			} else {
1916				DEBUGF(JOB, ("Job queue is no longer full.\n"));
1917				jobFull = FALSE;
1918			}
1919		}
1920
1921		JobFinish(job, &status);
1922	}
1923	if (interrupted)
1924		JobPassSig(interrupted);
1925}
1926
1927/**
1928 * Job_CatchOutput
1929 *	Catch the output from our children, if we're using
1930 *	pipes do so. Otherwise just block time until we get a
1931 *	signal(most likely a SIGCHLD) since there's no point in
1932 *	just spinning when there's nothing to do and the reaping
1933 *	of a child can wait for a while.
1934 *
1935 * Side Effects:
1936 *	Output is read from pipes if we're piping.
1937 * -----------------------------------------------------------------------
1938 */
1939void
1940#ifdef USE_KQUEUE
1941Job_CatchOutput(int flag __unused)
1942#else
1943Job_CatchOutput(int flag)
1944#endif
1945{
1946	int		nfds;
1947#ifdef USE_KQUEUE
1948#define KEV_SIZE	4
1949	struct kevent	kev[KEV_SIZE];
1950	int		i;
1951#else
1952	struct timeval	timeout;
1953	fd_set		readfds;
1954	LstNode		*ln;
1955	Job		*job;
1956#endif
1957
1958	fflush(stdout);
1959
1960	if (usePipes) {
1961#ifdef USE_KQUEUE
1962		if ((nfds = kevent(kqfd, NULL, 0, kev, KEV_SIZE, NULL)) == -1) {
1963			if (errno != EINTR)
1964				Punt("kevent: %s", strerror(errno));
1965			if (interrupted)
1966				JobPassSig(interrupted);
1967		} else {
1968			for (i = 0; i < nfds; i++) {
1969				if (kev[i].flags & EV_ERROR) {
1970					warnc(kev[i].data, "kevent");
1971					continue;
1972				}
1973				switch (kev[i].filter) {
1974				  case EVFILT_READ:
1975					JobDoOutput(kev[i].udata, FALSE);
1976					break;
1977				  case EVFILT_PROC:
1978					/*
1979					 * Just wake up and let
1980					 * Job_CatchChildren() collect the
1981					 * terminated job.
1982					 */
1983					break;
1984				}
1985			}
1986		}
1987#else
1988		readfds = outputs;
1989		timeout.tv_sec = SEL_SEC;
1990		timeout.tv_usec = SEL_USEC;
1991		if (flag && jobFull && fifoFd >= 0)
1992			FD_SET(fifoFd, &readfds);
1993
1994		nfds = select(FD_SETSIZE, &readfds, (fd_set *)NULL,
1995		    (fd_set *)NULL, &timeout);
1996		if (nfds <= 0) {
1997			if (interrupted)
1998				JobPassSig(interrupted);
1999			return;
2000		}
2001		if (fifoFd >= 0 && FD_ISSET(fifoFd, &readfds)) {
2002			if (--nfds <= 0)
2003				return;
2004		}
2005		for (ln = Lst_First(&jobs); nfds != 0 && ln != NULL;
2006		    ln = Lst_Succ(ln)){
2007			job = Lst_Datum(ln);
2008			if (FD_ISSET(job->inPipe, &readfds)) {
2009				JobDoOutput(job, FALSE);
2010				nfds -= 1;
2011			}
2012		}
2013#endif /* !USE_KQUEUE */
2014	}
2015}
2016
2017/**
2018 * Job_Make
2019 *	Start the creation of a target. Basically a front-end for
2020 *	JobStart used by the Make module.
2021 *
2022 * Side Effects:
2023 *	Another job is started.
2024 */
2025void
2026Job_Make(GNode *gn)
2027{
2028
2029	JobStart(gn, 0, NULL);
2030}
2031
2032/**
2033 * JobCopyShell
2034 *	Make a new copy of the shell structure including a copy of the strings
2035 *	in it. This also defaults some fields in case they are NULL.
2036 *
2037 * Returns:
2038 *	The function returns a pointer to the new shell structure.
2039 */
2040static Shell *
2041JobCopyShell(const Shell *osh)
2042{
2043	Shell *nsh;
2044
2045	nsh = emalloc(sizeof(*nsh));
2046	nsh->name = estrdup(osh->name);
2047
2048	if (osh->echoOff != NULL)
2049		nsh->echoOff = estrdup(osh->echoOff);
2050	else
2051		nsh->echoOff = NULL;
2052	if (osh->echoOn != NULL)
2053		nsh->echoOn = estrdup(osh->echoOn);
2054	else
2055		nsh->echoOn = NULL;
2056	nsh->hasEchoCtl = osh->hasEchoCtl;
2057
2058	if (osh->noPrint != NULL)
2059		nsh->noPrint = estrdup(osh->noPrint);
2060	else
2061		nsh->noPrint = NULL;
2062	nsh->noPLen = osh->noPLen;
2063
2064	nsh->hasErrCtl = osh->hasErrCtl;
2065	if (osh->errCheck == NULL)
2066		nsh->errCheck = estrdup("");
2067	else
2068		nsh->errCheck = estrdup(osh->errCheck);
2069	if (osh->ignErr == NULL)
2070		nsh->ignErr = estrdup("%s");
2071	else
2072		nsh->ignErr = estrdup(osh->ignErr);
2073
2074	if (osh->echo == NULL)
2075		nsh->echo = estrdup("");
2076	else
2077		nsh->echo = estrdup(osh->echo);
2078
2079	if (osh->exit == NULL)
2080		nsh->exit = estrdup("");
2081	else
2082		nsh->exit = estrdup(osh->exit);
2083
2084	return (nsh);
2085}
2086
2087/**
2088 * JobFreeShell
2089 *	Free a shell structure and all associated strings.
2090 */
2091static void
2092JobFreeShell(Shell *sh)
2093{
2094
2095	if (sh != NULL) {
2096		free(sh->name);
2097		free(sh->echoOff);
2098		free(sh->echoOn);
2099		free(sh->noPrint);
2100		free(sh->errCheck);
2101		free(sh->ignErr);
2102		free(sh->echo);
2103		free(sh->exit);
2104		free(sh);
2105	}
2106}
2107
2108void
2109Shell_Init(void)
2110{
2111
2112	if (commandShell == NULL)
2113		commandShell = JobMatchShell(shells[DEFSHELL].name);
2114
2115	if (shellPath == NULL) {
2116		/*
2117		 * The user didn't specify a shell to use, so we are using the
2118		 * default one... Both the absolute path and the last component
2119		 * must be set. The last component is taken from the 'name'
2120		 * field of the default shell description pointed-to by
2121		 * commandShell. All default shells are located in
2122		 * PATH_DEFSHELLDIR.
2123		 */
2124		shellName = commandShell->name;
2125		shellPath = str_concat(PATH_DEFSHELLDIR, shellName,
2126		    STR_ADDSLASH);
2127	}
2128}
2129
2130/**
2131 * Job_Init
2132 *	Initialize the process module, given a maximum number of jobs.
2133 *
2134 * Side Effects:
2135 *	lists and counters are initialized
2136 */
2137void
2138Job_Init(int maxproc)
2139{
2140	GNode		*begin;	/* node for commands to do at the very start */
2141	const char	*env;
2142	struct sigaction sa;
2143
2144	fifoFd = -1;
2145	env = getenv("MAKE_JOBS_FIFO");
2146
2147	if (env == NULL && maxproc > 1) {
2148		/*
2149		 * We did not find the environment variable so we are the
2150		 * leader. Create the fifo, open it, write one char per
2151		 * allowed job into the pipe.
2152		 */
2153		mktemp(fifoName);
2154		if (!mkfifo(fifoName, 0600)) {
2155			fifoFd = open(fifoName, O_RDWR | O_NONBLOCK, 0);
2156			if (fifoFd >= 0) {
2157				fifoMaster = 1;
2158				fcntl(fifoFd, F_SETFL, O_NONBLOCK);
2159				env = fifoName;
2160				setenv("MAKE_JOBS_FIFO", env, 1);
2161				while (maxproc-- > 0) {
2162					write(fifoFd, "+", 1);
2163				}
2164				/* The master make does not get a magic token */
2165				jobFull = TRUE;
2166				maxJobs = 0;
2167			} else {
2168				unlink(fifoName);
2169				env = NULL;
2170			}
2171		}
2172
2173	} else if (env != NULL) {
2174		/*
2175		 * We had the environment variable so we are a slave.
2176		 * Open fifo and give ourselves a magic token which represents
2177		 * the token our parent make has grabbed to start his make
2178		 * process. Otherwise the sub-makes would gobble up tokens and
2179		 * the proper number of tokens to specify to -j would depend
2180		 * on the depth of the tree and the order of execution.
2181		 */
2182		fifoFd = open(env, O_RDWR, 0);
2183		if (fifoFd >= 0) {
2184			fcntl(fifoFd, F_SETFL, O_NONBLOCK);
2185			maxJobs = 1;
2186			jobFull = FALSE;
2187		}
2188	}
2189	if (fifoFd <= 0) {
2190		maxJobs = maxproc;
2191		jobFull = FALSE;
2192	} else {
2193	}
2194	nJobs = 0;
2195
2196	aborting = 0;
2197	errors = 0;
2198
2199	lastNode = NULL;
2200
2201	if ((maxJobs == 1 && fifoFd < 0) || beVerbose == 0) {
2202		/*
2203		 * If only one job can run at a time, there's no need for a
2204		 * banner, no is there?
2205		 */
2206		targFmt = "";
2207	} else {
2208		targFmt = TARG_FMT;
2209	}
2210
2211	Shell_Init();
2212
2213	/*
2214	 * Catch the four signals that POSIX specifies if they aren't ignored.
2215	 * JobCatchSignal will just set global variables and hope someone
2216	 * else is going to handle the interrupt.
2217	 */
2218	sa.sa_handler = JobCatchSig;
2219	sigemptyset(&sa.sa_mask);
2220	sa.sa_flags = 0;
2221
2222	if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
2223		sigaction(SIGINT, &sa, NULL);
2224	}
2225	if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
2226		sigaction(SIGHUP, &sa, NULL);
2227	}
2228	if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
2229		sigaction(SIGQUIT, &sa, NULL);
2230	}
2231	if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
2232		sigaction(SIGTERM, &sa, NULL);
2233	}
2234	/*
2235	 * There are additional signals that need to be caught and passed if
2236	 * either the export system wants to be told directly of signals or if
2237	 * we're giving each job its own process group (since then it won't get
2238	 * signals from the terminal driver as we own the terminal)
2239	 */
2240#if defined(USE_PGRP)
2241	if (signal(SIGTSTP, SIG_IGN) != SIG_IGN) {
2242		sigaction(SIGTSTP, &sa, NULL);
2243	}
2244	if (signal(SIGTTOU, SIG_IGN) != SIG_IGN) {
2245		sigaction(SIGTTOU, &sa, NULL);
2246	}
2247	if (signal(SIGTTIN, SIG_IGN) != SIG_IGN) {
2248		sigaction(SIGTTIN, &sa, NULL);
2249	}
2250	if (signal(SIGWINCH, SIG_IGN) != SIG_IGN) {
2251		sigaction(SIGWINCH, &sa, NULL);
2252	}
2253#endif
2254
2255#ifdef USE_KQUEUE
2256	if ((kqfd = kqueue()) == -1) {
2257		Punt("kqueue: %s", strerror(errno));
2258	}
2259#endif
2260
2261	begin = Targ_FindNode(".BEGIN", TARG_NOCREATE);
2262
2263	if (begin != NULL) {
2264		JobStart(begin, JOB_SPECIAL, (Job *)NULL);
2265		while (nJobs) {
2266			Job_CatchOutput(0);
2267			Job_CatchChildren(!usePipes);
2268		}
2269	}
2270	postCommands = Targ_FindNode(".END", TARG_CREATE);
2271}
2272
2273/**
2274 * Job_Full
2275 *	See if the job table is full. It is considered full if it is OR
2276 *	if we are in the process of aborting OR if we have
2277 *	reached/exceeded our local quota. This prevents any more jobs
2278 *	from starting up.
2279 *
2280 * Results:
2281 *	TRUE if the job table is full, FALSE otherwise
2282 */
2283Boolean
2284Job_Full(void)
2285{
2286	char c;
2287	int i;
2288
2289	if (aborting)
2290		return (aborting);
2291	if (fifoFd >= 0 && jobFull) {
2292		i = read(fifoFd, &c, 1);
2293		if (i > 0) {
2294			maxJobs++;
2295			jobFull = FALSE;
2296		}
2297	}
2298	return (jobFull);
2299}
2300
2301/**
2302 * Job_Empty
2303 *	See if the job table is empty.  Because the local concurrency may
2304 *	be set to 0, it is possible for the job table to become empty,
2305 *	while the list of stoppedJobs remains non-empty. In such a case,
2306 *	we want to restart as many jobs as we can.
2307 *
2308 * Results:
2309 *	TRUE if it is. FALSE if it ain't.
2310 */
2311Boolean
2312Job_Empty(void)
2313{
2314	if (nJobs == 0) {
2315		if (!Lst_IsEmpty(&stoppedJobs) && !aborting) {
2316			/*
2317			 * The job table is obviously not full if it has no
2318			 * jobs in it...Try and restart the stopped jobs.
2319			 */
2320			jobFull = FALSE;
2321			JobRestartJobs();
2322			return (FALSE);
2323		} else {
2324			return (TRUE);
2325		}
2326	} else {
2327		return (FALSE);
2328	}
2329}
2330
2331/**
2332 * JobMatchShell
2333 *	Find a matching shell in 'shells' given its final component.
2334 *
2335 * Results:
2336 *	A pointer to a freshly allocated Shell structure with a copy
2337 *	of the static structure or NULL if no shell with the given name
2338 *	is found.
2339 */
2340static Shell *
2341JobMatchShell(const char *name)
2342{
2343	const struct CShell	*sh;	      /* Pointer into shells table */
2344	struct Shell *nsh;
2345
2346	for (sh = shells; sh < shells + sizeof(shells)/sizeof(shells[0]); sh++)
2347		if (strcmp(sh->name, name) == 0)
2348			break;
2349
2350	if (sh == shells + sizeof(shells)/sizeof(shells[0]))
2351		return (NULL);
2352
2353	/* make a copy */
2354	nsh = emalloc(sizeof(*nsh));
2355
2356	nsh->name = estrdup(sh->name);
2357	nsh->echoOff = estrdup(sh->echoOff);
2358	nsh->echoOn = estrdup(sh->echoOn);
2359	nsh->hasEchoCtl = sh->hasEchoCtl;
2360	nsh->noPrint = estrdup(sh->noPrint);
2361	nsh->noPLen = sh->noPLen;
2362	nsh->hasErrCtl = sh->hasErrCtl;
2363	nsh->errCheck = estrdup(sh->errCheck);
2364	nsh->ignErr = estrdup(sh->ignErr);
2365	nsh->echo = estrdup(sh->echo);
2366	nsh->exit = estrdup(sh->exit);
2367
2368	return (nsh);
2369}
2370
2371/**
2372 * Job_ParseShell
2373 *	Parse a shell specification and set up commandShell, shellPath
2374 *	and shellName appropriately.
2375 *
2376 * Results:
2377 *	FAILURE if the specification was incorrect.
2378 *
2379 * Side Effects:
2380 *	commandShell points to a Shell structure (either predefined or
2381 *	created from the shell spec), shellPath is the full path of the
2382 *	shell described by commandShell, while shellName is just the
2383 *	final component of shellPath.
2384 *
2385 * Notes:
2386 *	A shell specification consists of a .SHELL target, with dependency
2387 *	operator, followed by a series of blank-separated words. Double
2388 *	quotes can be used to use blanks in words. A backslash escapes
2389 *	anything (most notably a double-quote and a space) and
2390 *	provides the functionality it does in C. Each word consists of
2391 *	keyword and value separated by an equal sign. There should be no
2392 *	unnecessary spaces in the word. The keywords are as follows:
2393 *	    name  	    Name of shell.
2394 *	    path  	    Location of shell. Overrides "name" if given
2395 *	    quiet 	    Command to turn off echoing.
2396 *	    echo  	    Command to turn echoing on
2397 *	    filter	    Result of turning off echoing that shouldn't be
2398 *	    	  	    printed.
2399 *	    echoFlag	    Flag to turn echoing on at the start
2400 *	    errFlag	    Flag to turn error checking on at the start
2401 *	    hasErrCtl	    True if shell has error checking control
2402 *	    check 	    Command to turn on error checking if hasErrCtl
2403 *	    	  	    is TRUE or template of command to echo a command
2404 *	    	  	    for which error checking is off if hasErrCtl is
2405 *	    	  	    FALSE.
2406 *	    ignore	    Command to turn off error checking if hasErrCtl
2407 *	    	  	    is TRUE or template of command to execute a
2408 *	    	  	    command so as to ignore any errors it returns if
2409 *	    	  	    hasErrCtl is FALSE.
2410 */
2411ReturnStatus
2412Job_ParseShell(char *line)
2413{
2414	char	**words;
2415	int	wordCount;
2416	char	**argv;
2417	int	argc;
2418	char	*path;
2419	Shell	newShell;
2420	Shell	*sh;
2421	Boolean	fullSpec = FALSE;
2422
2423	while (isspace((unsigned char)*line)) {
2424		line++;
2425	}
2426	words = brk_string(line, &wordCount, TRUE);
2427
2428	memset(&newShell, 0, sizeof(newShell));
2429
2430	/*
2431	 * Parse the specification by keyword
2432	 */
2433	for (path = NULL, argc = wordCount - 1, argv = words + 1; argc != 0;
2434	    argc--, argv++) {
2435		if (strncmp(*argv, "path=", 5) == 0) {
2436			path = &argv[0][5];
2437		} else if (strncmp(*argv, "name=", 5) == 0) {
2438			newShell.name = &argv[0][5];
2439		} else {
2440			if (strncmp(*argv, "quiet=", 6) == 0) {
2441				newShell.echoOff = &argv[0][6];
2442			} else if (strncmp(*argv, "echo=", 5) == 0) {
2443				newShell.echoOn = &argv[0][5];
2444			} else if (strncmp(*argv, "filter=", 7) == 0) {
2445				newShell.noPrint = &argv[0][7];
2446				newShell.noPLen = strlen(newShell.noPrint);
2447			} else if (strncmp(*argv, "echoFlag=", 9) == 0) {
2448				newShell.echo = &argv[0][9];
2449			} else if (strncmp(*argv, "errFlag=", 8) == 0) {
2450				newShell.exit = &argv[0][8];
2451			} else if (strncmp(*argv, "hasErrCtl=", 10) == 0) {
2452				char c = argv[0][10];
2453				newShell.hasErrCtl = !((c != 'Y') &&
2454				    (c != 'y') && (c != 'T') && (c != 't'));
2455			} else if (strncmp(*argv, "check=", 6) == 0) {
2456				newShell.errCheck = &argv[0][6];
2457			} else if (strncmp(*argv, "ignore=", 7) == 0) {
2458				newShell.ignErr = &argv[0][7];
2459			} else {
2460				Parse_Error(PARSE_FATAL,
2461				    "Unknown keyword \"%s\"", *argv);
2462				return (FAILURE);
2463			}
2464			fullSpec = TRUE;
2465		}
2466	}
2467
2468	/*
2469	 * Some checks (could be more)
2470	 */
2471	if (fullSpec) {
2472		if ((newShell.echoOn != NULL) ^ (newShell.echoOff != NULL))
2473			Parse_Error(PARSE_FATAL, "Shell must have either both "
2474			    "echoOff and echoOn or none of them");
2475
2476		if (newShell.echoOn != NULL && newShell.echoOff)
2477			newShell.hasEchoCtl = TRUE;
2478	}
2479
2480	if (path == NULL) {
2481		/*
2482		 * If no path was given, the user wants one of the pre-defined
2483		 * shells, yes? So we find the one s/he wants with the help of
2484		 * JobMatchShell and set things up the right way. shellPath
2485		 * will be set up by Job_Init.
2486		 */
2487		if (newShell.name == NULL) {
2488			Parse_Error(PARSE_FATAL,
2489			    "Neither path nor name specified");
2490			return (FAILURE);
2491		}
2492		if ((sh = JobMatchShell(newShell.name)) == NULL) {
2493			Parse_Error(PARSE_FATAL, "%s: no matching shell",
2494			    newShell.name);
2495			return (FAILURE);
2496		}
2497
2498	} else {
2499		/*
2500		 * The user provided a path. If s/he gave nothing else
2501		 * (fullSpec is FALSE), try and find a matching shell in the
2502		 * ones we know of. Else we just take the specification at its
2503		 * word and copy it to a new location. In either case, we need
2504		 * to record the path the user gave for the shell.
2505		 */
2506		free(shellPath);
2507		shellPath = estrdup(path);
2508		if (newShell.name == NULL) {
2509			/* get the base name as the name */
2510			path = strrchr(path, '/');
2511			if (path == NULL) {
2512				path = shellPath;
2513			} else {
2514				path += 1;
2515			}
2516			newShell.name = path;
2517		}
2518
2519		if (!fullSpec) {
2520			if ((sh = JobMatchShell(newShell.name)) == NULL) {
2521				Parse_Error(PARSE_FATAL,
2522				    "%s: no matching shell", newShell.name);
2523				return (FAILURE);
2524			}
2525		} else {
2526			sh = JobCopyShell(&newShell);
2527		}
2528	}
2529
2530	/* set the new shell */
2531	JobFreeShell(commandShell);
2532	commandShell = sh;
2533
2534	shellName = commandShell->name;
2535
2536	return (SUCCESS);
2537}
2538
2539/**
2540 * JobInterrupt
2541 *	Handle the receipt of an interrupt.
2542 *
2543 * Side Effects:
2544 *	All children are killed. Another job will be started if the
2545 *	.INTERRUPT target was given.
2546 */
2547static void
2548JobInterrupt(int runINTERRUPT, int signo)
2549{
2550	LstNode	*ln;		/* element in job table */
2551	Job	*job;		/* job descriptor in that element */
2552	GNode	*interrupt;	/* the node describing the .INTERRUPT target */
2553
2554	aborting = ABORT_INTERRUPT;
2555
2556	for (ln = Lst_First(&jobs); ln != NULL; ln = Lst_Succ(ln)) {
2557		job = Lst_Datum(ln);
2558
2559		if (!Targ_Precious(job->node)) {
2560			char *file = (job->node->path == NULL ?
2561			    job->node->name : job->node->path);
2562
2563			if (!noExecute && eunlink(file) != -1) {
2564				Error("*** %s removed", file);
2565			}
2566		}
2567		if (job->pid) {
2568			DEBUGF(JOB, ("JobInterrupt passing signal to child "
2569			    "%d.\n", job->pid));
2570			KILL(job->pid, signo);
2571		}
2572	}
2573
2574	if (runINTERRUPT && !touchFlag) {
2575		/*
2576		 * clear the interrupted flag because we would get an
2577		 * infinite loop otherwise.
2578		 */
2579		interrupted = 0;
2580
2581		interrupt = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
2582		if (interrupt != NULL) {
2583			ignoreErrors = FALSE;
2584
2585			JobStart(interrupt, JOB_IGNDOTS, (Job *)NULL);
2586			while (nJobs) {
2587				Job_CatchOutput(0);
2588				Job_CatchChildren(!usePipes);
2589			}
2590		}
2591	}
2592}
2593
2594/**
2595 * Job_Finish
2596 *	Do final processing such as the running of the commands
2597 *	attached to the .END target.
2598 *
2599 * Results:
2600 *	Number of errors reported.
2601 */
2602int
2603Job_Finish(void)
2604{
2605
2606	if (postCommands != NULL && !Lst_IsEmpty(&postCommands->commands)) {
2607		if (errors) {
2608			Error("Errors reported so .END ignored");
2609		} else {
2610			JobStart(postCommands, JOB_SPECIAL | JOB_IGNDOTS, NULL);
2611
2612			while (nJobs) {
2613				Job_CatchOutput(0);
2614				Job_CatchChildren(!usePipes);
2615			}
2616		}
2617	}
2618	if (fifoFd >= 0) {
2619		close(fifoFd);
2620		fifoFd = -1;
2621		if (fifoMaster)
2622			unlink(fifoName);
2623	}
2624	return (errors);
2625}
2626
2627/**
2628 * Job_Wait
2629 *	Waits for all running jobs to finish and returns. Sets 'aborting'
2630 *	to ABORT_WAIT to prevent other jobs from starting.
2631 *
2632 * Side Effects:
2633 *	Currently running jobs finish.
2634 */
2635void
2636Job_Wait(void)
2637{
2638
2639	aborting = ABORT_WAIT;
2640	while (nJobs != 0) {
2641		Job_CatchOutput(0);
2642		Job_CatchChildren(!usePipes);
2643	}
2644	aborting = 0;
2645}
2646
2647/**
2648 * Job_AbortAll
2649 *	Abort all currently running jobs without handling output or anything.
2650 *	This function is to be called only in the event of a major
2651 *	error. Most definitely NOT to be called from JobInterrupt.
2652 *
2653 * Side Effects:
2654 *	All children are killed, not just the firstborn
2655 */
2656void
2657Job_AbortAll(void)
2658{
2659	LstNode	*ln;	/* element in job table */
2660	Job	*job;	/* the job descriptor in that element */
2661	int	foo;
2662
2663	aborting = ABORT_ERROR;
2664
2665	if (nJobs) {
2666		for (ln = Lst_First(&jobs); ln != NULL; ln = Lst_Succ(ln)) {
2667			job = Lst_Datum(ln);
2668
2669			/*
2670			 * kill the child process with increasingly drastic
2671			 * signals to make darn sure it's dead.
2672			 */
2673			KILL(job->pid, SIGINT);
2674			KILL(job->pid, SIGKILL);
2675		}
2676	}
2677
2678	/*
2679	 * Catch as many children as want to report in at first, then give up
2680	 */
2681	while (waitpid((pid_t)-1, &foo, WNOHANG) > 0)
2682		continue;
2683}
2684
2685/**
2686 * JobRestartJobs
2687 *	Tries to restart stopped jobs if there are slots available.
2688 *	Note that this tries to restart them regardless of pending errors.
2689 *	It's not good to leave stopped jobs lying around!
2690 *
2691 * Side Effects:
2692 *	Resumes(and possibly migrates) jobs.
2693 */
2694static void
2695JobRestartJobs(void)
2696{
2697	while (!jobFull && !Lst_IsEmpty(&stoppedJobs)) {
2698		DEBUGF(JOB, ("Job queue is not full. "
2699		    "Restarting a stopped job.\n"));
2700		JobRestart(Lst_DeQueue(&stoppedJobs));
2701	}
2702}
2703