Annotation of src/usr.bin/make/engine.c, Revision 1.25
1.25 ! espie 1: /* $OpenBSD: engine.c,v 1.24 2009/05/10 11:07:37 espie Exp $ */
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.3 espie 172: if (gn->type & (OP_JOIN|OP_USE|OP_EXEC|OP_OPTIONAL)) {
173: /*
1.9 espie 174: * .JOIN, .USE, and .OPTIONAL targets are "virtual" targets
175: * and, as such, shouldn't really be created.
1.3 espie 176: */
177: return;
178: }
1.1 espie 179:
1.12 espie 180: if (!(gn->type & OP_SILENT)) {
1.3 espie 181: (void)fprintf(stdout, "touch %s\n", gn->name);
182: (void)fflush(stdout);
183: }
1.1 espie 184:
1.3 espie 185: if (noExecute) {
186: return;
187: }
1.1 espie 188:
1.3 espie 189: if (gn->type & OP_ARCHV) {
190: Arch_Touch(gn);
191: } else if (gn->type & OP_LIB) {
192: Arch_TouchLib(gn);
193: } else {
194: const char *file = gn->path != NULL ? gn->path : gn->name;
195:
196: if (set_times(file) == -1){
1.6 espie 197: if (rewrite_time(file) == -1) {
1.5 espie 198: (void)fprintf(stdout,
199: "*** couldn't touch %s: %s", file,
1.3 espie 200: strerror(errno));
201: (void)fflush(stdout);
1.9 espie 202: }
1.1 espie 203: }
204: }
205: }
206:
207: void
1.7 espie 208: Make_TimeStamp(GNode *parent, GNode *child)
1.1 espie 209: {
1.8 espie 210: if (is_strictly_before(parent->cmtime, child->mtime))
211: parent->cmtime = child->mtime;
1.1 espie 212: }
213:
214: void
1.9 espie 215: Make_HandleUse(GNode *cgn, /* The .USE node */
1.1 espie 216: GNode *pgn) /* The target of the .USE node */
217: {
1.3 espie 218: GNode *gn; /* A child of the .USE node */
219: LstNode ln; /* An element in the children list */
1.1 espie 220:
1.20 espie 221:
222: assert(cgn->type & (OP_USE|OP_TRANSFORM));
223:
224: if ((cgn->type & OP_USE) || Lst_IsEmpty(&pgn->commands)) {
225: /* .USE or transformation and target has no commands
226: * -- append the child's commands to the parent. */
227: Lst_Concat(&pgn->commands, &cgn->commands);
228: }
229:
230: for (ln = Lst_First(&cgn->children); ln != NULL;
231: ln = Lst_Adv(ln)) {
232: gn = (GNode *)Lst_Datum(ln);
233:
234: if (Lst_AddNew(&pgn->children, gn)) {
235: Lst_AtEnd(&gn->parents, pgn);
236: pgn->unmade++;
1.3 espie 237: }
1.20 espie 238: }
1.1 espie 239:
1.20 espie 240: pgn->type |= cgn->type & ~(OP_OPMASK|OP_USE|OP_TRANSFORM);
1.1 espie 241:
1.20 espie 242: /*
243: * This child node is now "made", so we decrement the count of
244: * unmade children in the parent... We also remove the child
245: * from the parent's list to accurately reflect the number of
246: * decent children the parent has. This is used by Make_Run to
247: * decide whether to queue the parent or examine its children...
248: */
249: if (cgn->type & OP_USE)
250: pgn->unmade--;
1.1 espie 251:
1.17 espie 252: /* if the parent node doesn't have any location, then inherit the
253: * use stuff, since that gives us better error messages.
254: */
255: if (!pgn->lineno) {
256: pgn->lineno = cgn->lineno;
257: pgn->fname = cgn->fname;
258: }
1.1 espie 259: }
260:
1.16 espie 261: void
262: Make_DoAllVar(GNode *gn)
1.1 espie 263: {
1.16 espie 264: GNode *child;
265: LstNode ln;
266: BUFFER allsrc, oodate;
267: char *target;
268: bool do_oodate;
269: int oodate_count, allsrc_count = 0;
270:
271: oodate_count = 0;
272: allsrc_count = 0;
273:
274: for (ln = Lst_First(&gn->children); ln != NULL; ln = Lst_Adv(ln)) {
275: child = (GNode *)Lst_Datum(ln);
276: if ((child->type & (OP_EXEC|OP_USE|OP_INVISIBLE)) != 0)
277: continue;
1.9 espie 278: if (OP_NOP(child->type) ||
1.16 espie 279: (target = Var(TARGET_INDEX, child)) == NULL) {
1.3 espie 280: /*
281: * this node is only source; use the specific pathname
282: * for it
283: */
1.9 espie 284: target = child->path != NULL ? child->path :
285: child->name;
1.3 espie 286: }
1.1 espie 287:
1.16 espie 288: /*
289: * It goes in the OODATE variable if the parent is younger than
290: * the child or if the child has been modified more recently
291: * than the start of the make. This is to keep make from
292: * getting confused if something else updates the parent after
293: * the make starts (shouldn't happen, I know, but sometimes it
294: * does). In such a case, if we've updated the kid, the parent
295: * is likely to have a modification time later than that of the
296: * kid and anything that relies on the OODATE variable will be
297: * hosed.
298: */
299: do_oodate = false;
300: if (gn->type & OP_JOIN) {
1.14 espie 301: if (child->built_status == MADE)
1.16 espie 302: do_oodate = true;
303: } else if (is_strictly_before(gn->mtime, child->mtime) ||
1.9 espie 304: (!is_strictly_before(child->mtime, now) &&
1.16 espie 305: child->built_status == MADE))
306: do_oodate = true;
307: if (do_oodate) {
308: oodate_count++;
309: if (oodate_count == 1)
310: Var(OODATE_INDEX, gn) = target;
311: else {
312: if (oodate_count == 2) {
313: Buf_Init(&oodate, 0);
314: Buf_AddString(&oodate,
315: Var(OODATE_INDEX, gn));
316: }
317: Buf_AddSpace(&oodate);
318: Buf_AddString(&oodate, target);
319: }
320: }
321: allsrc_count++;
322: if (allsrc_count == 1)
323: Var(ALLSRC_INDEX, gn) = target;
324: else {
325: if (allsrc_count == 2) {
326: Buf_Init(&allsrc, 0);
327: Buf_AddString(&allsrc,
328: Var(ALLSRC_INDEX, gn));
329: }
330: Buf_AddSpace(&allsrc);
331: Buf_AddString(&allsrc, target);
1.3 espie 332: }
1.1 espie 333: }
334:
1.16 espie 335: if (allsrc_count > 1)
336: Var(ALLSRC_INDEX, gn) = Buf_Retrieve(&allsrc);
337: if (oodate_count > 1)
338: Var(OODATE_INDEX, gn) = Buf_Retrieve(&oodate);
1.1 espie 339:
1.13 espie 340: if (gn->impliedsrc)
1.16 espie 341: Var(IMPSRC_INDEX, gn) = Var(TARGET_INDEX, gn->impliedsrc);
1.13 espie 342:
1.3 espie 343: if (gn->type & OP_JOIN)
1.16 espie 344: Var(TARGET_INDEX, gn) = Var(ALLSRC_INDEX, gn);
1.1 espie 345: }
346:
347: /* Wrapper to call Make_TimeStamp from a forEach loop. */
348: static void
1.9 espie 349: MakeTimeStamp(void *parent, void *child)
1.1 espie 350: {
1.9 espie 351: Make_TimeStamp((GNode *)parent, (GNode *)child);
1.1 espie 352: }
353:
354: bool
1.9 espie 355: Make_OODate(GNode *gn)
1.1 espie 356: {
1.9 espie 357: bool oodate;
1.1 espie 358:
359: /*
1.3 espie 360: * Certain types of targets needn't even be sought as their datedness
361: * doesn't depend on their modification time...
1.1 espie 362: */
1.15 espie 363: if ((gn->type & (OP_JOIN|OP_USE|OP_EXEC|OP_PHONY)) == 0) {
1.3 espie 364: (void)Dir_MTime(gn);
365: if (DEBUG(MAKE)) {
1.9 espie 366: if (!is_out_of_date(gn->mtime))
1.5 espie 367: printf("modified %s...",
1.3 espie 368: time_to_string(gn->mtime));
1.9 espie 369: else
1.3 espie 370: printf("non-existent...");
371: }
1.1 espie 372: }
373:
374: /*
1.3 espie 375: * A target is remade in one of the following circumstances:
1.9 espie 376: * - its modification time is smaller than that of its youngest child
377: * and it would actually be run (has commands or type OP_NOP)
378: * - it's the object of a force operator
379: * - it has no children, was on the lhs of an operator and doesn't
380: * exist already.
1.3 espie 381: *
382: * Libraries are only considered out-of-date if the archive module says
383: * they are.
1.1 espie 384: */
1.3 espie 385: if (gn->type & OP_USE) {
386: /*
387: * If the node is a USE node it is *never* out of date
388: * no matter *what*.
389: */
1.9 espie 390: if (DEBUG(MAKE))
1.3 espie 391: printf(".USE node...");
392: oodate = false;
393: } else if ((gn->type & OP_LIB) && Arch_IsLib(gn)) {
1.9 espie 394: if (DEBUG(MAKE))
395: printf("library...");
1.3 espie 396:
1.9 espie 397: /* always out of date if no children and :: target */
1.3 espie 398: oodate = Arch_LibOODate(gn) ||
399: (is_out_of_date(gn->cmtime) && (gn->type & OP_DOUBLEDEP));
400: } else if (gn->type & OP_JOIN) {
401: /*
402: * A target with the .JOIN attribute is only considered
403: * out-of-date if any of its children was out-of-date.
404: */
1.9 espie 405: if (DEBUG(MAKE))
1.3 espie 406: printf(".JOIN node...");
407: oodate = gn->childMade;
408: } else if (gn->type & (OP_FORCE|OP_EXEC|OP_PHONY)) {
409: /*
1.9 espie 410: * A node which is the object of the force (!) operator or which
411: * has the .EXEC attribute is always considered out-of-date.
1.3 espie 412: */
413: if (DEBUG(MAKE)) {
1.9 espie 414: if (gn->type & OP_FORCE)
1.3 espie 415: printf("! operator...");
1.9 espie 416: else if (gn->type & OP_PHONY)
1.3 espie 417: printf(".PHONY node...");
1.9 espie 418: else
1.3 espie 419: printf(".EXEC node...");
420: }
421: oodate = true;
422: } else if (is_strictly_before(gn->mtime, gn->cmtime) ||
1.9 espie 423: (is_out_of_date(gn->cmtime) &&
1.3 espie 424: (is_out_of_date(gn->mtime) || (gn->type & OP_DOUBLEDEP)))) {
425: /*
426: * A node whose modification time is less than that of its
427: * youngest child or that has no children (cmtime ==
428: * OUT_OF_DATE) and either doesn't exist (mtime == OUT_OF_DATE)
1.9 espie 429: * or was the object of a :: operator is out-of-date.
1.3 espie 430: */
431: if (DEBUG(MAKE)) {
1.9 espie 432: if (is_strictly_before(gn->mtime, gn->cmtime))
1.3 espie 433: printf("modified before source...");
1.9 espie 434: else if (is_out_of_date(gn->mtime))
1.3 espie 435: printf("non-existent and no sources...");
1.9 espie 436: else
1.3 espie 437: printf(":: operator and no sources...");
438: }
439: oodate = true;
440: } else {
441: oodate = false;
442: }
1.1 espie 443:
1.3 espie 444: /*
445: * If the target isn't out-of-date, the parents need to know its
446: * modification time. Note that targets that appear to be out-of-date
447: * but aren't, because they have no commands and aren't of type OP_NOP,
448: * have their mtime stay below their children's mtime to keep parents
449: * from thinking they're out-of-date.
450: */
451: if (!oodate)
452: Lst_ForEach(&gn->parents, MakeTimeStamp, gn);
1.1 espie 453:
1.3 espie 454: return oodate;
1.1 espie 455: }
456:
1.10 espie 457: volatile sig_atomic_t got_signal;
458:
1.24 espie 459: volatile sig_atomic_t got_SIGINT, got_SIGHUP, got_SIGQUIT, got_SIGTERM;
1.10 espie 460:
461: static void
1.24 espie 462: setup_signal(int sig, psighandler h)
1.10 espie 463: {
1.24 espie 464: if (signal(sig, SIG_IGN) != SIG_IGN)
465: (void)signal(sig, h);
1.10 espie 466: }
467:
1.24 espie 468: void
469: setup_all_signals(psighandler interrupt, psighandler jc)
1.10 espie 470: {
471: /*
472: * Catch the four signals that POSIX specifies if they aren't ignored.
473: * handle_signal will take care of calling JobInterrupt if appropriate.
474: */
1.24 espie 475: setup_signal(SIGINT, interrupt);
476: setup_signal(SIGHUP, interrupt);
477: setup_signal(SIGQUIT, interrupt);
478: setup_signal(SIGTERM, interrupt);
479: setup_signal(SIGTSTP, jc);
480: setup_signal(SIGTTOU, jc);
481: setup_signal(SIGTTIN, jc);
482: setup_signal(SIGWINCH, jc);
483: setup_signal(SIGCONT, jc);
484: got_signal = 0;
1.10 espie 485: }
486:
487: void
488: SigHandler(int sig)
489: {
490: switch(sig) {
491: case SIGINT:
492: got_SIGINT++;
493: got_signal = 1;
494: break;
495: case SIGHUP:
496: got_SIGHUP++;
497: got_signal = 1;
498: break;
499: case SIGQUIT:
500: got_SIGQUIT++;
501: got_signal = 1;
502: break;
503: case SIGTERM:
504: got_SIGTERM++;
505: got_signal = 1;
506: break;
507: }
508: }
509:
510: /* The following array is used to make a fast determination of which
511: * characters are interpreted specially by the shell. If a command
512: * contains any of these characters, it is executed by the shell, not
513: * directly by us. */
514: static char meta[256];
515:
516: void
517: setup_meta(void)
518: {
519: char *p;
520:
521: for (p = "#=|^(){};&<>*?[]:$`\\\n"; *p != '\0'; p++)
522: meta[(unsigned char) *p] = 1;
523: /* The null character serves as a sentinel in the string. */
524: meta[0] = 1;
525: }
526:
527: static char **
528: recheck_command_for_shell(char **av)
529: {
530: char *runsh[] = {
531: "alias", "cd", "eval", "exit", "read", "set", "ulimit",
532: "unalias", "unset", "wait", "umask", NULL
533: };
534:
535: char **p;
536:
537: /* optimization: if exec cmd, we avoid the intermediate shell */
538: if (strcmp(av[0], "exec") == 0)
539: av++;
540:
541: for (p = runsh; *p; p++)
542: if (strcmp(av[0], *p) == 0)
543: return NULL;
544:
545: return av;
546: }
547:
548: static void
549: run_command(const char *cmd, bool errCheck)
550: {
551: const char *p;
552: char *shargv[4];
553: char **todo;
554:
555: shargv[0] = _PATH_BSHELL;
556:
557: shargv[1] = errCheck ? "-ec" : "-c";
558: shargv[2] = (char *)cmd;
559: shargv[3] = NULL;
560:
561: todo = shargv;
562:
563:
564: /* Search for meta characters in the command. If there are no meta
565: * characters, there's no need to execute a shell to execute the
566: * command. */
567: for (p = cmd; !meta[(unsigned char)*p]; p++)
568: continue;
569: if (*p == '\0') {
570: char *bp;
571: char **av;
572: int argc;
573: /* No meta-characters, so probably no need to exec a shell.
574: * Break the command into words to form an argument vector
575: * we can execute. */
576: av = brk_string(cmd, &argc, &bp);
577: av = recheck_command_for_shell(av);
578: if (av != NULL)
579: todo = av;
580: }
581: execvp(todo[0], todo);
582:
583: if (errno == ENOENT)
584: fprintf(stderr, "%s: not found\n", todo[0]);
585: else
586: perror(todo[0]);
587: _exit(1);
588: }
589:
590: /*-
591: *-----------------------------------------------------------------------
592: * setup_and_run_command --
593: * Execute the next command for a target. If the command returns an
1.14 espie 594: * error, the node's built_status field is set to ERROR and creation stops.
1.10 espie 595: *
596: * Results:
597: * 0 in case of error, 1 if ok.
598: *
599: * Side Effects:
1.14 espie 600: * The node's 'built_status' field may be set to ERROR.
1.10 espie 601: *-----------------------------------------------------------------------
602: */
603: static int
604: setup_and_run_command(char *cmd, GNode *gn, int dont_fork)
605: {
606: bool silent; /* Don't print command */
607: bool doExecute; /* Execute the command */
608: bool errCheck; /* Check errors */
609: int reason; /* Reason for child's death */
610: int status; /* Description of child's death */
611: pid_t cpid; /* Child actually found */
612: pid_t stat; /* Status of fork */
613:
614: silent = gn->type & OP_SILENT;
615: errCheck = !(gn->type & OP_IGNORE);
616: doExecute = !noExecute;
617:
618: /* How can we execute a null command ? we warn the user that the
619: * command expanded to nothing (is this the right thing to do?). */
1.19 espie 620: if (*cmd == '\0') {
1.10 espie 621: Error("%s expands to empty string", cmd);
622: return 1;
1.19 espie 623: }
1.10 espie 624:
625: for (;; cmd++) {
626: if (*cmd == '@')
627: silent = DEBUG(LOUD) ? false : true;
628: else if (*cmd == '-')
629: errCheck = false;
630: else if (*cmd == '+')
631: doExecute = true;
632: else
633: break;
634: }
635: while (isspace(*cmd))
636: cmd++;
637: /* Print the command before echoing if we're not supposed to be quiet
638: * for this one. We also print the command if -n given. */
639: if (!silent || noExecute) {
640: printf("%s\n", cmd);
641: fflush(stdout);
642: }
643: /* If we're not supposed to execute any commands, this is as far as
644: * we go... */
645: if (!doExecute)
646: return 1;
647:
648: /* if we're running in parallel mode, we try not to fork the last
649: * command, since it's exit status will be just fine... unless
650: * errCheck is not set, in which case we must deal with the
651: * status ourselves.
652: */
653: if (dont_fork && errCheck)
654: run_command(cmd, errCheck);
655: /*NOTREACHED*/
656:
657: /* Fork and execute the single command. If the fork fails, we abort. */
658: switch (cpid = fork()) {
659: case -1:
660: Fatal("Could not fork");
661: /*NOTREACHED*/
662: case 0:
663: run_command(cmd, errCheck);
664: /*NOTREACHED*/
665: default:
666: break;
667: }
668:
669: /* The child is off and running. Now all we can do is wait... */
670: while (1) {
671:
1.24 espie 672: while ((stat = waitpid(cpid, &reason, 0)) != cpid) {
1.10 espie 673: if (stat == -1 && errno != EINTR)
674: break;
675: }
676:
677: if (got_signal)
678: break;
679:
680: if (stat != -1) {
1.24 espie 681: if (WIFEXITED(reason)) {
1.10 espie 682: status = WEXITSTATUS(reason); /* exited */
683: if (status != 0)
1.24 espie 684: printf("*** Error code %d", status);
1.10 espie 685: } else {
686: status = WTERMSIG(reason); /* signaled */
687: printf("*** Signal %d", status);
688: }
689:
690:
691: if (!WIFEXITED(reason) || status != 0) {
692: if (errCheck) {
1.14 espie 693: gn->built_status = ERROR;
1.10 espie 694: if (keepgoing)
695: /* Abort the current target,
696: * but let others continue. */
697: printf(" (continuing)\n");
698: } else {
699: /* Continue executing commands for
700: * this target. If we return 0,
701: * this will happen... */
702: printf(" (ignored)\n");
703: status = 0;
704: }
705: }
706: return !status;
707: } else
708: Fatal("error in wait: %d", stat);
709: /*NOTREACHED*/
710: }
711: return 0;
712: }
713:
714: static void
715: handle_compat_interrupts(GNode *gn)
716: {
717: if (!Targ_Precious(gn)) {
1.16 espie 718: char *file = Var(TARGET_INDEX, gn);
1.10 espie 719:
720: if (!noExecute && eunlink(file) != -1)
721: Error("*** %s removed\n", file);
722: }
723: if (got_SIGINT) {
724: signal(SIGINT, SIG_IGN);
725: signal(SIGTERM, SIG_IGN);
726: signal(SIGHUP, SIG_IGN);
727: signal(SIGQUIT, SIG_IGN);
728: got_signal = 0;
729: got_SIGINT = 0;
1.19 espie 730: run_gnode(interrupt_node);
1.10 espie 731: exit(255);
732: }
733: exit(255);
734: }
735:
1.19 espie 736: void
737: expand_commands(GNode *gn)
738: {
739: LstNode ln;
740: char *cmd;
741:
742: for (ln = Lst_First(&gn->commands); ln != NULL; ln = Lst_Adv(ln)) {
743: cmd = Var_Subst(Lst_Datum(ln), &gn->context, false);
744: Lst_AtEnd(&gn->expanded, cmd);
745: }
746: }
747:
748: int
749: run_gnode(GNode *gn)
750: {
751: if (gn != NULL && (gn->type & OP_DUMMY) == 0) {
752: expand_commands(gn);
1.23 espie 753: return run_prepared_gnode(gn);
754: } else {
755: return NOSUCHNODE;
1.19 espie 756: }
757: }
758:
1.23 espie 759: static int
760: run_prepared_gnode(GNode *gn)
1.10 espie 761: {
1.19 espie 762: char *cmd;
1.10 espie 763:
1.23 espie 764: gn->built_status = MADE;
765: while ((cmd = Lst_DeQueue(&gn->expanded)) != NULL) {
766: if (setup_and_run_command(cmd, gn, 0) == 0)
767: break;
1.19 espie 768: free(cmd);
1.23 espie 769: }
770: free(cmd);
771: if (got_signal)
772: handle_compat_interrupts(gn);
773: return gn->built_status;
774: }
775:
776: void
777: run_gnode_parallel(GNode *gn)
778: {
779: char *cmd;
780:
781: gn->built_status = MADE;
782: /* XXX don't bother freeing cmd, we're dead anyways ! */
783: while ((cmd = Lst_DeQueue(&gn->expanded)) != NULL) {
784: if (setup_and_run_command(cmd, gn,
785: Lst_IsEmpty(&gn->expanded)) == 0)
786: break;
787: }
788: /* Normally, we don't reach this point, unless the last command
789: * ignores error, in which case we interpret the status ourselves.
790: */
791: switch(gn->built_status) {
792: case MADE:
793: exit(0);
794: case ERROR:
795: exit(1);
796: default:
797: fprintf(stderr,
798: "Could not run gnode, returned %d\n",
799: gn->built_status);
800: exit(1);
801: }
1.10 espie 802: }
803:
804: void
1.24 espie 805: setup_engine(int parallel)
1.10 espie 806: {
807: static int already_setup = 0;
808:
809: if (!already_setup) {
810: setup_meta();
1.24 espie 811: if (parallel)
812: setup_all_signals(parallel_handler, parallel_handler);
813: else
814: setup_all_signals(SigHandler, SIG_DFL);
1.10 espie 815: already_setup = 1;
816: }
817: }