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