xref: /openbsd-src/usr.bin/make/engine.c (revision 43003dfe3ad45d1698bed8a37f2b0f5b14f20d4f)
1 /*	$OpenBSD: engine.c,v 1.27 2009/08/16 09:49:22 espie Exp $ */
2 /*
3  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
4  * Copyright (c) 1988, 1989 by Adam de Boor
5  * Copyright (c) 1989 by Berkeley Softworks
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Adam de Boor.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/types.h>
37 #include <sys/wait.h>
38 #include <assert.h>
39 #include <limits.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <ctype.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <unistd.h>
46 #include <string.h>
47 #include <signal.h>
48 #include "config.h"
49 #include "defines.h"
50 #include "dir.h"
51 #include "engine.h"
52 #include "arch.h"
53 #include "gnode.h"
54 #include "targ.h"
55 #include "var.h"
56 #include "extern.h"
57 #include "lst.h"
58 #include "timestamp.h"
59 #include "make.h"
60 #include "pathnames.h"
61 #include "error.h"
62 #include "str.h"
63 #include "memory.h"
64 #include "buf.h"
65 #include "job.h"
66 
67 static void MakeTimeStamp(void *, void *);
68 static int rewrite_time(const char *);
69 static void setup_signal(int, psighandler);
70 static void setup_meta(void);
71 static char **recheck_command_for_shell(char **);
72 
73 static int setup_and_run_command(char *, GNode *, int);
74 static void run_command(const char *, bool);
75 static int run_prepared_gnode(GNode *);
76 static void handle_compat_interrupts(GNode *);
77 
78 bool
79 Job_CheckCommands(GNode *gn)
80 {
81 	/* Alter our type to tell if errors should be ignored or things
82 	 * should not be printed so setup_and_run_command knows what to do.
83 	 */
84 	if (Targ_Ignore(gn))
85 		gn->type |= OP_IGNORE;
86 	if (Targ_Silent(gn))
87 		gn->type |= OP_SILENT;
88 
89 	if (OP_NOP(gn->type) && Lst_IsEmpty(&gn->commands) &&
90 	    (gn->type & OP_LIB) == 0) {
91 		/*
92 		 * No commands. Look for .DEFAULT rule from which we might infer
93 		 * commands
94 		 */
95 		if ((gn->type & OP_NODEFAULT) == 0 &&
96 		    (DEFAULT->type & OP_DUMMY) == 0 &&
97 		    !Lst_IsEmpty(&DEFAULT->commands)) {
98 			/*
99 			 * Make only looks for a .DEFAULT if the node was never
100 			 * the target of an operator, so that's what we do too.
101 			 * If a .DEFAULT was given, we substitute its commands
102 			 * for gn's commands and set the IMPSRC variable to be
103 			 * the target's name The DEFAULT node acts like a
104 			 * transformation rule, in that gn also inherits any
105 			 * attributes or sources attached to .DEFAULT itself.
106 			 */
107 			Make_HandleUse(DEFAULT, gn);
108 			Var(IMPSRC_INDEX, gn) = Var(TARGET_INDEX, gn);
109 		} else if (is_out_of_date(Dir_MTime(gn))) {
110 			/*
111 			 * The node wasn't the target of an operator we have no
112 			 * .DEFAULT rule to go on and the target doesn't
113 			 * already exist. There's nothing more we can do for
114 			 * this branch.
115 			 */
116 			 return false;
117 	    	}
118 	}
119 	return true;
120 }
121 
122 void
123 job_failure(GNode *gn, void (*abortProc)(char *, ...))
124 {
125 	/*
126 	 If the -k flag wasn't given, we stop in
127 	 * our tracks, otherwise we just don't update this
128 	 * node's parents so they never get examined.
129 	 */
130 	static const char msg[] =
131 	    "make: don't know how to make";
132 
133 	if (gn->type & OP_OPTIONAL) {
134 		(void)fprintf(stdout, "%s %s(ignored)\n", msg,
135 		    gn->name);
136 		(void)fflush(stdout);
137 	} else if (keepgoing) {
138 		(void)fprintf(stdout, "%s %s(continuing)\n",
139 		    msg, gn->name);
140 		(void)fflush(stdout);
141 	} else {
142 		(*abortProc)("%s %s. Stop in %s.", msg,
143 		    gn->name, Var_Value(".CURDIR"));
144 	}
145 }
146 /* touch files the hard way, by writing stuff to them */
147 static int
148 rewrite_time(const char *name)
149 {
150 	int fd;
151 	char c;
152 
153 	fd = open(name, O_RDWR | O_CREAT, 0666);
154 	if (fd < 0)
155 		return -1;
156 	/*
157 	 * Read and write a byte to the file to change
158 	 * the modification time.
159 	 */
160 	if (read(fd, &c, 1) == 1) {
161 		(void)lseek(fd, 0, SEEK_SET);
162 		(void)write(fd, &c, 1);
163 	}
164 
165 	(void)close(fd);
166 	return 0;
167 }
168 
169 void
170 Job_Touch(GNode *gn)
171 {
172 	if (gn->type & (OP_JOIN|OP_USE|OP_EXEC|OP_OPTIONAL|OP_PHONY)) {
173 		/*
174 		 * .JOIN, .USE, and .OPTIONAL targets are "virtual" targets
175 		 * and, as such, shouldn't really be created.
176 		 * Likewise, .PHONY targets are not really files
177 		 */
178 		return;
179 	}
180 
181 	if (!(gn->type & OP_SILENT)) {
182 		(void)fprintf(stdout, "touch %s\n", gn->name);
183 		(void)fflush(stdout);
184 	}
185 
186 	if (noExecute) {
187 		return;
188 	}
189 
190 	if (gn->type & OP_ARCHV) {
191 		Arch_Touch(gn);
192 	} else if (gn->type & OP_LIB) {
193 		Arch_TouchLib(gn);
194 	} else {
195 		const char *file = gn->path != NULL ? gn->path : gn->name;
196 
197 		if (set_times(file) == -1){
198 			if (rewrite_time(file) == -1) {
199 				(void)fprintf(stdout,
200 				    "*** couldn't touch %s: %s", file,
201 				    strerror(errno));
202 				(void)fflush(stdout);
203 		    	}
204 		}
205 	}
206 }
207 
208 void
209 Make_TimeStamp(GNode *parent, GNode *child)
210 {
211 	if (is_strictly_before(parent->cmtime, child->mtime))
212 		parent->cmtime = child->mtime;
213 }
214 
215 void
216 Make_HandleUse(GNode	*cgn,	/* The .USE node */
217     GNode	*pgn)	/* The target of the .USE node */
218 {
219 	GNode	*gn;	/* A child of the .USE node */
220 	LstNode	ln;	/* An element in the children list */
221 
222 
223 	assert(cgn->type & (OP_USE|OP_TRANSFORM));
224 
225 	if ((cgn->type & OP_USE) || Lst_IsEmpty(&pgn->commands)) {
226 		/* .USE or transformation and target has no commands
227 		 * -- append the child's commands to the parent.  */
228 		Lst_Concat(&pgn->commands, &cgn->commands);
229 	}
230 
231 	for (ln = Lst_First(&cgn->children); ln != NULL;
232 	    ln = Lst_Adv(ln)) {
233 		gn = (GNode *)Lst_Datum(ln);
234 
235 		if (Lst_AddNew(&pgn->children, gn)) {
236 			Lst_AtEnd(&gn->parents, pgn);
237 			pgn->unmade++;
238 		}
239 	}
240 
241 	pgn->type |= cgn->type & ~(OP_OPMASK|OP_USE|OP_TRANSFORM);
242 
243 	/*
244 	 * This child node is now "made", so we decrement the count of
245 	 * unmade children in the parent... We also remove the child
246 	 * from the parent's list to accurately reflect the number of
247 	 * decent children the parent has. This is used by Make_Run to
248 	 * decide whether to queue the parent or examine its children...
249 	 */
250 	if (cgn->type & OP_USE)
251 		pgn->unmade--;
252 
253 	/* if the parent node doesn't have any location, then inherit the
254 	 * use stuff, since that gives us better error messages.
255 	 */
256 	if (!pgn->lineno) {
257 		pgn->lineno = cgn->lineno;
258 		pgn->fname = cgn->fname;
259 	}
260 }
261 
262 void
263 Make_DoAllVar(GNode *gn)
264 {
265 	GNode *child;
266 	LstNode ln;
267 	BUFFER allsrc, oodate;
268 	char *target;
269 	bool do_oodate;
270 	int oodate_count, allsrc_count = 0;
271 
272 	oodate_count = 0;
273 	allsrc_count = 0;
274 
275 	for (ln = Lst_First(&gn->children); ln != NULL; ln = Lst_Adv(ln)) {
276 		child = (GNode *)Lst_Datum(ln);
277 		if ((child->type & (OP_EXEC|OP_USE|OP_INVISIBLE)) != 0)
278 			continue;
279 		if (OP_NOP(child->type) ||
280 		    (target = Var(TARGET_INDEX, child)) == NULL) {
281 			/*
282 			 * this node is only source; use the specific pathname
283 			 * for it
284 			 */
285 			target = child->path != NULL ? child->path :
286 			    child->name;
287 		}
288 
289 		/*
290 		 * It goes in the OODATE variable if the parent is younger than
291 		 * the child or if the child has been modified more recently
292 		 * than the start of the make.  This is to keep make from
293 		 * getting confused if something else updates the parent after
294 		 * the make starts (shouldn't happen, I know, but sometimes it
295 		 * does). In such a case, if we've updated the kid, the parent
296 		 * is likely to have a modification time later than that of the
297 		 * kid and anything that relies on the OODATE variable will be
298 		 * hosed.
299 		 */
300 		do_oodate = false;
301 		if (gn->type & OP_JOIN) {
302 			if (child->built_status == MADE)
303 				do_oodate = true;
304 		} else if (is_strictly_before(gn->mtime, child->mtime) ||
305 		   (!is_strictly_before(child->mtime, now) &&
306 		   child->built_status == MADE))
307 		   	do_oodate = true;
308 		if (do_oodate) {
309 			oodate_count++;
310 			if (oodate_count == 1)
311 				Var(OODATE_INDEX, gn) = target;
312 			else {
313 				if (oodate_count == 2) {
314 					Buf_Init(&oodate, 0);
315 					Buf_AddString(&oodate,
316 					    Var(OODATE_INDEX, gn));
317 				}
318 				Buf_AddSpace(&oodate);
319 				Buf_AddString(&oodate, target);
320 			}
321 		}
322 		allsrc_count++;
323 		if (allsrc_count == 1)
324 			Var(ALLSRC_INDEX, gn) = target;
325 		else {
326 			if (allsrc_count == 2) {
327 				Buf_Init(&allsrc, 0);
328 				Buf_AddString(&allsrc,
329 				    Var(ALLSRC_INDEX, gn));
330 			}
331 			Buf_AddSpace(&allsrc);
332 			Buf_AddString(&allsrc, target);
333 		}
334 	}
335 
336 	if (allsrc_count > 1)
337 		Var(ALLSRC_INDEX, gn) = Buf_Retrieve(&allsrc);
338 	if (oodate_count > 1)
339 		Var(OODATE_INDEX, gn) = Buf_Retrieve(&oodate);
340 
341 	if (gn->impliedsrc)
342 		Var(IMPSRC_INDEX, gn) = Var(TARGET_INDEX, gn->impliedsrc);
343 
344 	if (gn->type & OP_JOIN)
345 		Var(TARGET_INDEX, gn) = Var(ALLSRC_INDEX, gn);
346 }
347 
348 /* Wrapper to call Make_TimeStamp from a forEach loop.	*/
349 static void
350 MakeTimeStamp(void *parent, void *child)
351 {
352     Make_TimeStamp((GNode *)parent, (GNode *)child);
353 }
354 
355 bool
356 Make_OODate(GNode *gn)
357 {
358 	bool	    oodate;
359 
360 	/*
361 	 * Certain types of targets needn't even be sought as their datedness
362 	 * doesn't depend on their modification time...
363 	 */
364 	if ((gn->type & (OP_JOIN|OP_USE|OP_EXEC|OP_PHONY)) == 0) {
365 		(void)Dir_MTime(gn);
366 		if (DEBUG(MAKE)) {
367 			if (!is_out_of_date(gn->mtime))
368 				printf("modified %s...",
369 				    time_to_string(gn->mtime));
370 			else
371 				printf("non-existent...");
372 		}
373 	}
374 
375 	/*
376 	 * A target is remade in one of the following circumstances:
377 	 * - its modification time is smaller than that of its youngest child
378 	 *   and it would actually be run (has commands or type OP_NOP)
379 	 * - it's the object of a force operator
380 	 * - it has no children, was on the lhs of an operator and doesn't
381 	 *   exist already.
382 	 *
383 	 * Libraries are only considered out-of-date if the archive module says
384 	 * they are.
385 	 */
386 	if (gn->type & OP_USE) {
387 		/*
388 		 * If the node is a USE node it is *never* out of date
389 		 * no matter *what*.
390 		 */
391 		if (DEBUG(MAKE))
392 			printf(".USE node...");
393 		oodate = false;
394 	} else if ((gn->type & OP_LIB) && Arch_IsLib(gn)) {
395 		if (DEBUG(MAKE))
396 		    printf("library...");
397 
398 		/* always out of date if no children and :: target */
399 		oodate = Arch_LibOODate(gn) ||
400 		    (is_out_of_date(gn->cmtime) && (gn->type & OP_DOUBLEDEP));
401 	} else if (gn->type & OP_JOIN) {
402 		/*
403 		 * A target with the .JOIN attribute is only considered
404 		 * out-of-date if any of its children was out-of-date.
405 		 */
406 		if (DEBUG(MAKE))
407 			printf(".JOIN node...");
408 		oodate = gn->childMade;
409 	} else if (gn->type & (OP_FORCE|OP_EXEC|OP_PHONY)) {
410 		/*
411 		 * A node which is the object of the force (!) operator or which
412 		 * has the .EXEC attribute is always considered out-of-date.
413 		 */
414 		if (DEBUG(MAKE)) {
415 			if (gn->type & OP_FORCE)
416 				printf("! operator...");
417 			else if (gn->type & OP_PHONY)
418 				printf(".PHONY node...");
419 			else
420 				printf(".EXEC node...");
421 		}
422 		oodate = true;
423 	} else if (is_strictly_before(gn->mtime, gn->cmtime) ||
424 	   (is_out_of_date(gn->cmtime) &&
425 	    (is_out_of_date(gn->mtime) || (gn->type & OP_DOUBLEDEP)))) {
426 		/*
427 		 * A node whose modification time is less than that of its
428 		 * youngest child or that has no children (cmtime ==
429 		 * OUT_OF_DATE) and either doesn't exist (mtime == OUT_OF_DATE)
430 		 * or was the object of a :: operator is out-of-date.
431 		 */
432 		if (DEBUG(MAKE)) {
433 			if (is_strictly_before(gn->mtime, gn->cmtime))
434 				printf("modified before source...");
435 			else if (is_out_of_date(gn->mtime))
436 				printf("non-existent and no sources...");
437 			else
438 				printf(":: operator and no sources...");
439 		}
440 		oodate = true;
441 	} else {
442 		oodate = false;
443 	}
444 
445 	/*
446 	 * If the target isn't out-of-date, the parents need to know its
447 	 * modification time. Note that targets that appear to be out-of-date
448 	 * but aren't, because they have no commands and aren't of type OP_NOP,
449 	 * have their mtime stay below their children's mtime to keep parents
450 	 * from thinking they're out-of-date.
451 	 */
452 	if (!oodate)
453 		Lst_ForEach(&gn->parents, MakeTimeStamp, gn);
454 
455 	return oodate;
456 }
457 
458 volatile sig_atomic_t got_signal;
459 
460 volatile sig_atomic_t got_SIGINT, got_SIGHUP, got_SIGQUIT, got_SIGTERM;
461 
462 static void
463 setup_signal(int sig, psighandler h)
464 {
465 	if (signal(sig, SIG_IGN) != SIG_IGN)
466 		(void)signal(sig, h);
467 }
468 
469 void
470 setup_all_signals(psighandler interrupt, psighandler jc)
471 {
472 	/*
473 	 * Catch the four signals that POSIX specifies if they aren't ignored.
474 	 * handle_signal will take care of calling JobInterrupt if appropriate.
475 	 */
476 	setup_signal(SIGINT, interrupt);
477 	setup_signal(SIGHUP, interrupt);
478 	setup_signal(SIGQUIT, interrupt);
479 	setup_signal(SIGTERM, interrupt);
480 	setup_signal(SIGTSTP, jc);
481 	setup_signal(SIGTTOU, jc);
482 	setup_signal(SIGTTIN, jc);
483 	setup_signal(SIGWINCH, jc);
484 	setup_signal(SIGCONT, jc);
485 	got_signal = 0;
486 }
487 
488 void
489 SigHandler(int sig)
490 {
491 	switch(sig) {
492 	case SIGINT:
493 		got_SIGINT++;
494 		got_signal = 1;
495 		break;
496 	case SIGHUP:
497 		got_SIGHUP++;
498 		got_signal = 1;
499 		break;
500 	case SIGQUIT:
501 		got_SIGQUIT++;
502 		got_signal = 1;
503 		break;
504 	case SIGTERM:
505 		got_SIGTERM++;
506 		got_signal = 1;
507 		break;
508 	}
509 }
510 
511 /* The following array is used to make a fast determination of which
512  * characters are interpreted specially by the shell.  If a command
513  * contains any of these characters, it is executed by the shell, not
514  * directly by us.  */
515 static char	    meta[256];
516 
517 void
518 setup_meta(void)
519 {
520 	char *p;
521 
522 	for (p = "#=|^(){};&<>*?[]:$`\\\n"; *p != '\0'; p++)
523 		meta[(unsigned char) *p] = 1;
524 	/* The null character serves as a sentinel in the string.  */
525 	meta[0] = 1;
526 }
527 
528 static char **
529 recheck_command_for_shell(char **av)
530 {
531 	char *runsh[] = {
532 		"alias", "cd", "eval", "exit", "read", "set", "ulimit",
533 		"unalias", "unset", "wait", "umask", NULL
534 	};
535 
536 	char **p;
537 
538 	/* optimization: if exec cmd, we avoid the intermediate shell */
539 	if (strcmp(av[0], "exec") == 0)
540 		av++;
541 
542 	for (p = runsh; *p; p++)
543 		if (strcmp(av[0], *p) == 0)
544 			return NULL;
545 
546 	return av;
547 }
548 
549 static void
550 run_command(const char *cmd, bool errCheck)
551 {
552 	const char *p;
553 	char *shargv[4];
554 	char **todo;
555 
556 	shargv[0] = _PATH_BSHELL;
557 
558 	shargv[1] = errCheck ? "-ec" : "-c";
559 	shargv[2] = (char *)cmd;
560 	shargv[3] = NULL;
561 
562 	todo = shargv;
563 
564 
565 	/* Search for meta characters in the command. If there are no meta
566 	 * characters, there's no need to execute a shell to execute the
567 	 * command.  */
568 	for (p = cmd; !meta[(unsigned char)*p]; p++)
569 		continue;
570 	if (*p == '\0') {
571 		char *bp;
572 		char **av;
573 		int argc;
574 		/* No meta-characters, so probably no need to exec a shell.
575 		 * Break the command into words to form an argument vector
576 		 * we can execute.  */
577 		av = brk_string(cmd, &argc, &bp);
578 		av = recheck_command_for_shell(av);
579 		if (av != NULL)
580 			todo = av;
581 	}
582 	execvp(todo[0], todo);
583 
584 	if (errno == ENOENT)
585 		fprintf(stderr, "%s: not found\n", todo[0]);
586 	else
587 		perror(todo[0]);
588 	_exit(1);
589 }
590 
591 /*-
592  *-----------------------------------------------------------------------
593  * setup_and_run_command --
594  *	Execute the next command for a target. If the command returns an
595  *	error, the node's built_status field is set to ERROR and creation stops.
596  *
597  * Results:
598  *	0 in case of error, 1 if ok.
599  *
600  * Side Effects:
601  *	The node's 'built_status' field may be set to ERROR.
602  *-----------------------------------------------------------------------
603  */
604 static int
605 setup_and_run_command(char *cmd, GNode *gn, int dont_fork)
606 {
607 	bool silent;	/* Don't print command */
608 	bool doExecute;	/* Execute the command */
609 	bool errCheck;	/* Check errors */
610 	int reason;	/* Reason for child's death */
611 	int status;	/* Description of child's death */
612 	pid_t cpid; 	/* Child actually found */
613 	pid_t stat;	/* Status of fork */
614 
615 	silent = gn->type & OP_SILENT;
616 	errCheck = !(gn->type & OP_IGNORE);
617 	doExecute = !noExecute;
618 
619 	/* How can we execute a null command ? we warn the user that the
620 	 * command expanded to nothing (is this the right thing to do?).  */
621 	if (*cmd == '\0') {
622 		Error("%s expands to empty string", cmd);
623 		return 1;
624 	}
625 
626 	for (;; cmd++) {
627 		if (*cmd == '@')
628 			silent = DEBUG(LOUD) ? false : true;
629 		else if (*cmd == '-')
630 			errCheck = false;
631 		else if (*cmd == '+')
632 			doExecute = true;
633 		else
634 			break;
635 	}
636 	while (isspace(*cmd))
637 		cmd++;
638 	/* Print the command before echoing if we're not supposed to be quiet
639 	 * for this one. We also print the command if -n given.  */
640 	if (!silent || noExecute) {
641 		printf("%s\n", cmd);
642 		fflush(stdout);
643 	}
644 	/* If we're not supposed to execute any commands, this is as far as
645 	 * we go...  */
646 	if (!doExecute)
647 		return 1;
648 
649 	/* if we're running in parallel mode, we try not to fork the last
650 	 * command, since it's exit status will be just fine... unless
651 	 * errCheck is not set, in which case we must deal with the
652 	 * status ourselves.
653 	 */
654 	if (dont_fork && errCheck)
655 		run_command(cmd, errCheck);
656 		/*NOTREACHED*/
657 
658 	/* Fork and execute the single command. If the fork fails, we abort.  */
659 	switch (cpid = fork()) {
660 	case -1:
661 		Fatal("Could not fork");
662 		/*NOTREACHED*/
663 	case 0:
664 		run_command(cmd, errCheck);
665 		/*NOTREACHED*/
666 	default:
667 		break;
668 	}
669 
670 	/* The child is off and running. Now all we can do is wait...  */
671 	while (1) {
672 
673 		while ((stat = waitpid(cpid, &reason, 0)) != cpid) {
674 			if (stat == -1 && errno != EINTR)
675 				break;
676 		}
677 
678 		if (got_signal)
679 			break;
680 
681 		if (stat != -1) {
682 			if (WIFEXITED(reason)) {
683 				status = WEXITSTATUS(reason);	/* exited */
684 				if (status != 0)
685 					printf("*** Error code %d", status);
686 			} else {
687 				status = WTERMSIG(reason);	/* signaled */
688 				printf("*** Signal %d", status);
689 			}
690 
691 
692 			if (!WIFEXITED(reason) || status != 0) {
693 				if (errCheck) {
694 					gn->built_status = ERROR;
695 					if (keepgoing)
696 						/* Abort the current target,
697 						 * but let others continue.  */
698 						printf(" (continuing)\n");
699 				} else {
700 					/* Continue executing commands for
701 					 * this target.  If we return 0,
702 					 * this will happen...  */
703 					printf(" (ignored)\n");
704 					status = 0;
705 				}
706 			}
707 			return !status;
708 		} else
709 			Fatal("error in wait: %s", strerror(errno));
710 			/*NOTREACHED*/
711 	}
712 	return 0;
713 }
714 
715 static void
716 handle_compat_interrupts(GNode *gn)
717 {
718 	if (!Targ_Precious(gn)) {
719 		char *file = Var(TARGET_INDEX, gn);
720 
721 		if (!noExecute && eunlink(file) != -1)
722 			Error("*** %s removed\n", file);
723 	}
724 	if (got_SIGINT) {
725 		signal(SIGINT, SIG_IGN);
726 		signal(SIGTERM, SIG_IGN);
727 		signal(SIGHUP, SIG_IGN);
728 		signal(SIGQUIT, SIG_IGN);
729 		got_signal = 0;
730 		got_SIGINT = 0;
731 		run_gnode(interrupt_node);
732 		exit(255);
733 	}
734 	exit(255);
735 }
736 
737 void
738 expand_commands(GNode *gn)
739 {
740 	LstNode ln;
741 	char *cmd;
742 
743 	for (ln = Lst_First(&gn->commands); ln != NULL; ln = Lst_Adv(ln)) {
744 		cmd = Var_Subst(Lst_Datum(ln), &gn->context, false);
745 		Lst_AtEnd(&gn->expanded, cmd);
746 	}
747 }
748 
749 int
750 run_gnode(GNode *gn)
751 {
752 	if (gn != NULL && (gn->type & OP_DUMMY) == 0) {
753 		expand_commands(gn);
754 		return run_prepared_gnode(gn);
755 	} else {
756 		return NOSUCHNODE;
757 	}
758 }
759 
760 static int
761 run_prepared_gnode(GNode *gn)
762 {
763 	char *cmd;
764 
765 	gn->built_status = MADE;
766 	while ((cmd = Lst_DeQueue(&gn->expanded)) != NULL) {
767 		if (setup_and_run_command(cmd, gn, 0) == 0)
768 			break;
769 		free(cmd);
770 	}
771 	free(cmd);
772 	if (got_signal)
773 		handle_compat_interrupts(gn);
774 	return gn->built_status;
775 }
776 
777 void
778 run_gnode_parallel(GNode *gn)
779 {
780 	char *cmd;
781 
782 	gn->built_status = MADE;
783 	/* XXX don't bother freeing cmd, we're dead anyways ! */
784 	while ((cmd = Lst_DeQueue(&gn->expanded)) != NULL) {
785 		if (setup_and_run_command(cmd, gn,
786 		    Lst_IsEmpty(&gn->expanded)) == 0)
787 			break;
788 	}
789 	/* Normally, we don't reach this point, unless the last command
790 	 * ignores error, in which case we interpret the status ourselves.
791 	 */
792 	switch(gn->built_status) {
793 	case MADE:
794 		exit(0);
795 	case ERROR:
796 		exit(1);
797 	default:
798 		fprintf(stderr, "Could not run gnode, returned %d\n",
799 		    gn->built_status);
800 		exit(1);
801 	}
802 }
803 
804 void
805 setup_engine(int parallel)
806 {
807 	static int already_setup = 0;
808 
809 	if (!already_setup) {
810 		setup_meta();
811 		if (parallel)
812 			setup_all_signals(parallel_handler, parallel_handler);
813 		else
814 			setup_all_signals(SigHandler, SIG_DFL);
815 		already_setup = 1;
816 	}
817 }
818