Annotation of src/usr.bin/make/engine.c, Revision 1.26
1.26 ! espie 1: /* $OpenBSD$ */
1.1 espie 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:
1.10 espie 36: #include <sys/types.h>
37: #include <sys/wait.h>
1.20 espie 38: #include <assert.h>
1.1 espie 39: #include <limits.h>
40: #include <stdio.h>
1.10 espie 41: #include <stdlib.h>
42: #include <ctype.h>
1.1 espie 43: #include <errno.h>
44: #include <fcntl.h>
45: #include <unistd.h>
46: #include <string.h>
1.10 espie 47: #include <signal.h>
1.1 espie 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"
1.10 espie 60: #include "pathnames.h"
61: #include "error.h"
62: #include "str.h"
63: #include "memory.h"
1.16 espie 64: #include "buf.h"
1.24 espie 65: #include "job.h"
1.1 espie 66:
67: static void MakeTimeStamp(void *, void *);
1.6 espie 68: static int rewrite_time(const char *);
1.24 espie 69: static void setup_signal(int, psighandler);
1.10 espie 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);
1.23 espie 75: static int run_prepared_gnode(GNode *);
1.10 espie 76: static void handle_compat_interrupts(GNode *);
1.1 espie 77:
78: bool
1.20 espie 79: Job_CheckCommands(GNode *gn)
1.1 espie 80: {
1.11 espie 81: /* Alter our type to tell if errors should be ignored or things
1.21 espie 82: * should not be printed so setup_and_run_command knows what to do.
1.11 espie 83: */
84: if (Targ_Ignore(gn))
85: gn->type |= OP_IGNORE;
86: if (Targ_Silent(gn))
87: gn->type |= OP_SILENT;
88:
1.3 espie 89: if (OP_NOP(gn->type) && Lst_IsEmpty(&gn->commands) &&
1.18 espie 90: (gn->type & OP_LIB) == 0) {
1.3 espie 91: /*
92: * No commands. Look for .DEFAULT rule from which we might infer
93: * commands
94: */
1.18 espie 95: if ((gn->type & OP_NODEFAULT) == 0 &&
96: (DEFAULT->type & OP_DUMMY) == 0 &&
1.9 espie 97: !Lst_IsEmpty(&DEFAULT->commands)) {
1.3 espie 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);
1.16 espie 108: Var(IMPSRC_INDEX, gn) = Var(TARGET_INDEX, gn);
1.3 espie 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
1.20 espie 114: * this branch.
1.3 espie 115: */
1.20 espie 116: return false;
117: }
1.1 espie 118: }
1.3 espie 119: return true;
1.1 espie 120: }
121:
1.20 espie 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: }
1.6 espie 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:
1.1 espie 169: void
1.12 espie 170: Job_Touch(GNode *gn)
1.1 espie 171: {
1.26 ! espie 172: if (gn->type & (OP_JOIN|OP_USE|OP_EXEC|OP_OPTIONAL|OP_PHONY)) {
1.3 espie 173: /*
1.9 espie 174: * .JOIN, .USE, and .OPTIONAL targets are "virtual" targets
175: * and, as such, shouldn't really be created.
1.26 ! espie 176: * Likewise, .PHONY targets are not really files
1.3 espie 177: */
178: return;
179: }
1.1 espie 180:
1.12 espie 181: if (!(gn->type & OP_SILENT)) {
1.3 espie 182: (void)fprintf(stdout, "touch %s\n", gn->name);
183: (void)fflush(stdout);
184: }
1.1 espie 185:
1.3 espie 186: if (noExecute) {
187: return;
188: }
1.1 espie 189:
1.3 espie 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){
1.6 espie 198: if (rewrite_time(file) == -1) {
1.5 espie 199: (void)fprintf(stdout,
200: "*** couldn't touch %s: %s", file,
1.3 espie 201: strerror(errno));
202: (void)fflush(stdout);
1.9 espie 203: }
1.1 espie 204: }
205: }
206: }
207:
208: void
1.7 espie 209: Make_TimeStamp(GNode *parent, GNode *child)
1.1 espie 210: {
1.8 espie 211: if (is_strictly_before(parent->cmtime, child->mtime))
212: parent->cmtime = child->mtime;
1.1 espie 213: }
214:
215: void
1.9 espie 216: Make_HandleUse(GNode *cgn, /* The .USE node */
1.1 espie 217: GNode *pgn) /* The target of the .USE node */
218: {
1.3 espie 219: GNode *gn; /* A child of the .USE node */
220: LstNode ln; /* An element in the children list */
1.1 espie 221:
1.20 espie 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++;
1.3 espie 238: }
1.20 espie 239: }
1.1 espie 240:
1.20 espie 241: pgn->type |= cgn->type & ~(OP_OPMASK|OP_USE|OP_TRANSFORM);
1.1 espie 242:
1.20 espie 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--;
1.1 espie 252:
1.17 espie 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: }
1.1 espie 260: }
261:
1.16 espie 262: void
263: Make_DoAllVar(GNode *gn)
1.1 espie 264: {
1.16 espie 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;
1.9 espie 279: if (OP_NOP(child->type) ||
1.16 espie 280: (target = Var(TARGET_INDEX, child)) == NULL) {
1.3 espie 281: /*
282: * this node is only source; use the specific pathname
283: * for it
284: */
1.9 espie 285: target = child->path != NULL ? child->path :
286: child->name;
1.3 espie 287: }
1.1 espie 288:
1.16 espie 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) {
1.14 espie 302: if (child->built_status == MADE)
1.16 espie 303: do_oodate = true;
304: } else if (is_strictly_before(gn->mtime, child->mtime) ||
1.9 espie 305: (!is_strictly_before(child->mtime, now) &&
1.16 espie 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);
1.3 espie 333: }
1.1 espie 334: }
335:
1.16 espie 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);
1.1 espie 340:
1.13 espie 341: if (gn->impliedsrc)
1.16 espie 342: Var(IMPSRC_INDEX, gn) = Var(TARGET_INDEX, gn->impliedsrc);
1.13 espie 343:
1.3 espie 344: if (gn->type & OP_JOIN)
1.16 espie 345: Var(TARGET_INDEX, gn) = Var(ALLSRC_INDEX, gn);
1.1 espie 346: }
347:
348: /* Wrapper to call Make_TimeStamp from a forEach loop. */
349: static void
1.9 espie 350: MakeTimeStamp(void *parent, void *child)
1.1 espie 351: {
1.9 espie 352: Make_TimeStamp((GNode *)parent, (GNode *)child);
1.1 espie 353: }
354:
355: bool
1.9 espie 356: Make_OODate(GNode *gn)
1.1 espie 357: {
1.9 espie 358: bool oodate;
1.1 espie 359:
360: /*
1.3 espie 361: * Certain types of targets needn't even be sought as their datedness
362: * doesn't depend on their modification time...
1.1 espie 363: */
1.15 espie 364: if ((gn->type & (OP_JOIN|OP_USE|OP_EXEC|OP_PHONY)) == 0) {
1.3 espie 365: (void)Dir_MTime(gn);
366: if (DEBUG(MAKE)) {
1.9 espie 367: if (!is_out_of_date(gn->mtime))
1.5 espie 368: printf("modified %s...",
1.3 espie 369: time_to_string(gn->mtime));
1.9 espie 370: else
1.3 espie 371: printf("non-existent...");
372: }
1.1 espie 373: }
374:
375: /*
1.3 espie 376: * A target is remade in one of the following circumstances:
1.9 espie 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.
1.3 espie 382: *
383: * Libraries are only considered out-of-date if the archive module says
384: * they are.
1.1 espie 385: */
1.3 espie 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: */
1.9 espie 391: if (DEBUG(MAKE))
1.3 espie 392: printf(".USE node...");
393: oodate = false;
394: } else if ((gn->type & OP_LIB) && Arch_IsLib(gn)) {
1.9 espie 395: if (DEBUG(MAKE))
396: printf("library...");
1.3 espie 397:
1.9 espie 398: /* always out of date if no children and :: target */
1.3 espie 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: */
1.9 espie 406: if (DEBUG(MAKE))
1.3 espie 407: printf(".JOIN node...");
408: oodate = gn->childMade;
409: } else if (gn->type & (OP_FORCE|OP_EXEC|OP_PHONY)) {
410: /*
1.9 espie 411: * A node which is the object of the force (!) operator or which
412: * has the .EXEC attribute is always considered out-of-date.
1.3 espie 413: */
414: if (DEBUG(MAKE)) {
1.9 espie 415: if (gn->type & OP_FORCE)
1.3 espie 416: printf("! operator...");
1.9 espie 417: else if (gn->type & OP_PHONY)
1.3 espie 418: printf(".PHONY node...");
1.9 espie 419: else
1.3 espie 420: printf(".EXEC node...");
421: }
422: oodate = true;
423: } else if (is_strictly_before(gn->mtime, gn->cmtime) ||
1.9 espie 424: (is_out_of_date(gn->cmtime) &&
1.3 espie 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)
1.9 espie 430: * or was the object of a :: operator is out-of-date.
1.3 espie 431: */
432: if (DEBUG(MAKE)) {
1.9 espie 433: if (is_strictly_before(gn->mtime, gn->cmtime))
1.3 espie 434: printf("modified before source...");
1.9 espie 435: else if (is_out_of_date(gn->mtime))
1.3 espie 436: printf("non-existent and no sources...");
1.9 espie 437: else
1.3 espie 438: printf(":: operator and no sources...");
439: }
440: oodate = true;
441: } else {
442: oodate = false;
443: }
1.1 espie 444:
1.3 espie 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);
1.1 espie 454:
1.3 espie 455: return oodate;
1.1 espie 456: }
457:
1.10 espie 458: volatile sig_atomic_t got_signal;
459:
1.24 espie 460: volatile sig_atomic_t got_SIGINT, got_SIGHUP, got_SIGQUIT, got_SIGTERM;
1.10 espie 461:
462: static void
1.24 espie 463: setup_signal(int sig, psighandler h)
1.10 espie 464: {
1.24 espie 465: if (signal(sig, SIG_IGN) != SIG_IGN)
466: (void)signal(sig, h);
1.10 espie 467: }
468:
1.24 espie 469: void
470: setup_all_signals(psighandler interrupt, psighandler jc)
1.10 espie 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: */
1.24 espie 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;
1.10 espie 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
1.14 espie 595: * error, the node's built_status field is set to ERROR and creation stops.
1.10 espie 596: *
597: * Results:
598: * 0 in case of error, 1 if ok.
599: *
600: * Side Effects:
1.14 espie 601: * The node's 'built_status' field may be set to ERROR.
1.10 espie 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?). */
1.19 espie 621: if (*cmd == '\0') {
1.10 espie 622: Error("%s expands to empty string", cmd);
623: return 1;
1.19 espie 624: }
1.10 espie 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:
1.24 espie 673: while ((stat = waitpid(cpid, &reason, 0)) != cpid) {
1.10 espie 674: if (stat == -1 && errno != EINTR)
675: break;
676: }
677:
678: if (got_signal)
679: break;
680:
681: if (stat != -1) {
1.24 espie 682: if (WIFEXITED(reason)) {
1.10 espie 683: status = WEXITSTATUS(reason); /* exited */
684: if (status != 0)
1.24 espie 685: printf("*** Error code %d", status);
1.10 espie 686: } else {
687: status = WTERMSIG(reason); /* signaled */
688: printf("*** Signal %d", status);
689: }
690:
691:
692: if (!WIFEXITED(reason) || status != 0) {
693: if (errCheck) {
1.14 espie 694: gn->built_status = ERROR;
1.10 espie 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: %d", stat);
710: /*NOTREACHED*/
711: }
712: return 0;
713: }
714:
715: static void
716: handle_compat_interrupts(GNode *gn)
717: {
718: if (!Targ_Precious(gn)) {
1.16 espie 719: char *file = Var(TARGET_INDEX, gn);
1.10 espie 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;
1.19 espie 731: run_gnode(interrupt_node);
1.10 espie 732: exit(255);
733: }
734: exit(255);
735: }
736:
1.19 espie 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);
1.23 espie 754: return run_prepared_gnode(gn);
755: } else {
756: return NOSUCHNODE;
1.19 espie 757: }
758: }
759:
1.23 espie 760: static int
761: run_prepared_gnode(GNode *gn)
1.10 espie 762: {
1.19 espie 763: char *cmd;
1.10 espie 764:
1.23 espie 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;
1.19 espie 769: free(cmd);
1.23 espie 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,
799: "Could not run gnode, returned %d\n",
800: gn->built_status);
801: exit(1);
802: }
1.10 espie 803: }
804:
805: void
1.24 espie 806: setup_engine(int parallel)
1.10 espie 807: {
808: static int already_setup = 0;
809:
810: if (!already_setup) {
811: setup_meta();
1.24 espie 812: if (parallel)
813: setup_all_signals(parallel_handler, parallel_handler);
814: else
815: setup_all_signals(SigHandler, SIG_DFL);
1.10 espie 816: already_setup = 1;
817: }
818: }