[BACK]Return to engine.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / make

Annotation of src/usr.bin/make/engine.c, Revision 1.31

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