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

Annotation of src/usr.bin/make/job.c, Revision 1.138

1.138   ! natano      1: /*     $OpenBSD: job.c,v 1.137 2017/01/21 11:30:11 deraadt Exp $       */
1.6       millert     2: /*     $NetBSD: job.c,v 1.16 1996/11/06 17:59:08 christos Exp $        */
1.1       deraadt     3:
                      4: /*
1.124     espie       5:  * Copyright (c) 2012 Marc Espie.
                      6:  *
                      7:  * Extensive code modifications for the OpenBSD project.
                      8:  *
                      9:  * Redistribution and use in source and binary forms, with or without
                     10:  * modification, are permitted provided that the following conditions
                     11:  * are met:
                     12:  * 1. Redistributions of source code must retain the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer.
                     14:  * 2. Redistributions in binary form must reproduce the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer in the
                     16:  *    documentation and/or other materials provided with the distribution.
                     17:  *
                     18:  * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
                     19:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
                     20:  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
                     21:  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENBSD
                     22:  * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
                     23:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
                     24:  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     25:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     26:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     27:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
                     28:  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     29:  */
                     30: /*
1.1       deraadt    31:  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
                     32:  * Copyright (c) 1988, 1989 by Adam de Boor
                     33:  * Copyright (c) 1989 by Berkeley Softworks
                     34:  * All rights reserved.
                     35:  *
                     36:  * This code is derived from software contributed to Berkeley by
                     37:  * Adam de Boor.
                     38:  *
                     39:  * Redistribution and use in source and binary forms, with or without
                     40:  * modification, are permitted provided that the following conditions
                     41:  * are met:
                     42:  * 1. Redistributions of source code must retain the above copyright
                     43:  *    notice, this list of conditions and the following disclaimer.
                     44:  * 2. Redistributions in binary form must reproduce the above copyright
                     45:  *    notice, this list of conditions and the following disclaimer in the
                     46:  *    documentation and/or other materials provided with the distribution.
1.55      millert    47:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    48:  *    may be used to endorse or promote products derived from this software
                     49:  *    without specific prior written permission.
                     50:  *
                     51:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     52:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     53:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     54:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     55:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     56:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     57:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     58:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     59:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     60:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     61:  * SUCH DAMAGE.
                     62:  */
                     63:
                     64: /*-
                     65:  * job.c --
                     66:  *     handle the creation etc. of our child processes.
                     67:  *
                     68:  * Interface:
1.40      espie      69:  *     Job_Make                Start the creation of the given target.
1.1       deraadt    70:  *
1.124     espie      71:  *     Job_Init                Called to initialize this module.
                     72:  *
                     73:  *     Job_Begin               execute commands attached to the .BEGIN target
                     74:  *                             if any.
1.40      espie      75:  *
1.117     espie      76:  *     can_start_job           Return true if we can start job
1.40      espie      77:  *
1.41      espie      78:  *     Job_Empty               Return true if the job table is completely
1.40      espie      79:  *                             empty.
                     80:  *
                     81:  *     Job_Finish              Perform any final processing which needs doing.
                     82:  *                             This includes the execution of any commands
                     83:  *                             which have been/were attached to the .END
1.124     espie      84:  *                             target.
1.40      espie      85:  *
1.117     espie      86:  *     Job_AbortAll            Abort all current jobs. It doesn't
1.40      espie      87:  *                             handle output or do anything for the jobs,
1.124     espie      88:  *                             just kills them.
1.1       deraadt    89:  *
1.117     espie      90:  *     Job_Wait                Wait for all running jobs to finish.
1.1       deraadt    91:  */
                     92:
                     93: #include <sys/types.h>
                     94: #include <sys/wait.h>
1.41      espie      95: #include <ctype.h>
                     96: #include <errno.h>
1.1       deraadt    97: #include <fcntl.h>
1.41      espie      98: #include <signal.h>
1.69      espie      99: #include <stdarg.h>
1.1       deraadt   100: #include <stdio.h>
1.42      espie     101: #include <stdlib.h>
1.1       deraadt   102: #include <string.h>
1.41      espie     103: #include <unistd.h>
                    104: #include "config.h"
                    105: #include "defines.h"
1.1       deraadt   106: #include "job.h"
1.63      espie     107: #include "engine.h"
1.1       deraadt   108: #include "pathnames.h"
1.41      espie     109: #include "var.h"
                    110: #include "targ.h"
                    111: #include "error.h"
1.124     espie     112: #include "extern.h"
1.41      espie     113: #include "lst.h"
                    114: #include "gnode.h"
                    115: #include "memory.h"
                    116: #include "make.h"
1.124     espie     117: #include "buf.h"
1.78      espie     118:
1.40      espie     119: static int     aborting = 0;       /* why is the make aborting? */
                    120: #define ABORT_ERROR    1           /* Because of an error */
                    121: #define ABORT_INTERRUPT 2          /* Because it was interrupted */
                    122: #define ABORT_WAIT     3           /* Waiting for jobs to finish */
1.1       deraadt   123:
1.40      espie     124: static int     maxJobs;        /* The most children we can run at once */
1.124     espie     125: static int     nJobs;          /* Number of jobs already allocated */
                    126: static bool    no_new_jobs;    /* Mark recursive shit so we shouldn't start
                    127:                                 * something else at the same time
                    128:                                 */
                    129: Job *runningJobs;              /* Jobs currently running a process */
                    130: Job *errorJobs;                        /* Jobs in error at end */
                    131: static Job *heldJobs;          /* Jobs not running yet because of expensive */
1.134     espie     132: static pid_t mypid;            /* Used for printing debugging messages */
1.1       deraadt   133:
1.128     espie     134: static volatile sig_atomic_t got_fatal;
1.112     espie     135:
1.124     espie     136: static volatile sig_atomic_t got_SIGINT, got_SIGHUP, got_SIGQUIT, got_SIGTERM,
1.128     espie     137:     got_SIGINFO;
1.112     espie     138:
1.124     espie     139: static sigset_t sigset, emptyset;
1.112     espie     140:
1.126     espie     141: static void handle_fatal_signal(int);
1.124     espie     142: static void handle_siginfo(void);
                    143: static void postprocess_job(Job *, bool);
                    144: static Job *prepare_job(GNode *);
                    145: static void determine_job_next_step(Job *);
                    146: static void remove_job(Job *, bool);
                    147: static void may_continue_job(Job *);
                    148: static void continue_job(Job *);
                    149: static Job *reap_finished_job(pid_t);
                    150: static bool reap_jobs(void);
1.112     espie     151:
1.111     espie     152: static void loop_handle_running_jobs(void);
1.124     espie     153: static bool expensive_job(Job *);
                    154: static bool expensive_command(const char *);
                    155: static void setup_signal(int);
                    156: static void notice_signal(int);
                    157: static void setup_all_signals(void);
1.128     espie     158: static const char *really_kill(Job *, int);
                    159: static void debug_kill_printf(const char *, ...);
                    160: static void debug_vprintf(const char *, va_list);
                    161: static void may_remove_target(Job *);
1.131     espie     162: static const char *really_kill(Job *, int);
                    163: static void print_error(Job *);
                    164: static void internal_print_errors(void);
                    165:
                    166: static int dying_signal = 0;
                    167:
                    168: const char *   basedirectory = NULL;
1.127     espie     169:
1.128     espie     170: static const char *
                    171: really_kill(Job *job, int signo)
                    172: {
                    173:        pid_t pid = job->pid;
                    174:        if (getpgid(pid) != getpgrp()) {
                    175:                if (killpg(pid, signo) == 0)
                    176:                        return "group got signal";
                    177:                pid = -pid;
                    178:        } else {
                    179:                if (kill(pid, signo) == 0)
                    180:                        return "process got signal";
                    181:        }
1.138   ! natano    182:        if (errno == ESRCH)
1.128     espie     183:                job->flags |= JOB_LOST;
1.138   ! natano    184:        return strerror(errno);
1.128     espie     185: }
                    186:
                    187: static void
                    188: may_remove_target(Job *j)
                    189: {
1.131     espie     190:        int dying = check_dying_signal();
                    191:
                    192:        if (dying && !noExecute && !Targ_Precious(j->node)) {
1.128     espie     193:                const char *file = Var(TARGET_INDEX, j->node);
                    194:                int r = eunlink(file);
                    195:
                    196:                if (DEBUG(JOB) && r == -1)
                    197:                        fprintf(stderr, " *** would unlink %s\n", file);
                    198:                if (r != -1)
                    199:                        fprintf(stderr, " *** %s removed\n", file);
1.127     espie     200:        }
                    201: }
1.126     espie     202:
1.131     espie     203: static void
                    204: buf_addcurdir(BUFFER *buf)
                    205: {
                    206:        const char *v = Var_Value(".CURDIR");
                    207:        if (basedirectory != NULL) {
                    208:                size_t len = strlen(basedirectory);
                    209:                if (strncmp(basedirectory, v, len) == 0 &&
                    210:                    v[len] == '/') {
                    211:                        v += len+1;
                    212:                } else if (strcmp(basedirectory, v) == 0) {
                    213:                        Buf_AddString(buf, ".");
                    214:                        return;
                    215:                }
                    216:        }
                    217:        Buf_AddString(buf, v);
                    218: }
                    219:
                    220: static const char *
                    221: shortened_curdir(void)
                    222: {
                    223:        static BUFFER buf;
                    224:        bool first = true;
                    225:        if (first) {
                    226:                Buf_Init(&buf, 0);
                    227:                buf_addcurdir(&buf);
                    228:                first = false;
                    229:        }
                    230:        return Buf_Retrieve(&buf);
                    231: }
                    232:
                    233: static void
                    234: quick_error(Job *j, int signo, bool first)
                    235: {
                    236:        if (first) {
                    237:                fprintf(stderr, "*** Signal SIG%s", sys_signame[signo]);
                    238:                fprintf(stderr, " in %s (", shortened_curdir());
                    239:        } else
                    240:                fprintf(stderr, " ");
                    241:
                    242:        fprintf(stderr, "%s", j->node->name);
                    243:        free(j->cmd);
                    244: }
                    245:
1.126     espie     246: static void
1.130     espie     247: print_error(Job *j)
1.126     espie     248: {
1.130     espie     249:        static bool first = true;
1.131     espie     250:        BUFFER buf;
                    251:
                    252:        Buf_Init(&buf, 0);
1.126     espie     253:
1.131     espie     254:        if (j->exit_type == JOB_EXIT_BAD)
                    255:                Buf_printf(&buf, "*** Error %d", j->code);
                    256:        else if (j->exit_type == JOB_SIGNALED) {
1.128     espie     257:                if (j->code < NSIG)
1.131     espie     258:                        Buf_printf(&buf, "*** Signal SIG%s",
1.128     espie     259:                            sys_signame[j->code]);
                    260:                else
1.131     espie     261:                        Buf_printf(&buf, "*** unknown signal %d", j->code);
1.128     espie     262:        } else
1.131     espie     263:                Buf_printf(&buf, "*** Should not happen %d/%d",
                    264:                    j->exit_type, j->code);
1.128     espie     265:        if (DEBUG(KILL) && (j->flags & JOB_LOST))
1.131     espie     266:                Buf_AddChar(&buf, '!');
1.130     espie     267:        if (first) {
1.131     espie     268:                Buf_AddString(&buf, " in ");
                    269:                buf_addcurdir(&buf);
1.130     espie     270:                first = false;
                    271:        }
1.131     espie     272:        Buf_printf(&buf, " (%s:%lu", j->location->fname, j->location->lineno);
                    273:        Buf_printf(&buf, " '%s'", j->node->name);
                    274:        if ((j->flags & (JOB_SILENT | JOB_IS_EXPENSIVE)) == JOB_SILENT
1.132     espie     275:            && Buf_Size(&buf) < 140-2) {
1.131     espie     276:                size_t len = strlen(j->cmd);
                    277:                Buf_AddString(&buf, ": ");
                    278:                if (len + Buf_Size(&buf) < 140)
                    279:                        Buf_AddString(&buf, j->cmd);
                    280:                else {
                    281:                        Buf_AddChars(&buf, 140 - Buf_Size(&buf), j->cmd);
                    282:                        Buf_AddString(&buf, "...");
                    283:                }
                    284:        }
                    285:        fprintf(stderr, "%s)\n", Buf_Retrieve(&buf));
                    286:        Buf_Destroy(&buf);
1.126     espie     287:        free(j->cmd);
1.131     espie     288: }
                    289: static void
                    290: quick_summary(int signo)
                    291: {
                    292:        Job *j, *k, *jnext;
                    293:        bool first = true;
                    294:
                    295:        k = errorJobs;
                    296:        errorJobs = NULL;
                    297:        for (j = k; j != NULL; j = jnext) {
                    298:                jnext = j->next;
                    299:                if ((j->exit_type == JOB_EXIT_BAD && j->code == signo+128) ||
                    300:                    (j->exit_type == JOB_SIGNALED && j->code == signo)) {
                    301:                        quick_error(j, signo, first);
                    302:                        first = false;
                    303:                } else {
                    304:                        j->next = errorJobs;
                    305:                        errorJobs = j;
                    306:                }
                    307:        }
                    308:        if (!first)
                    309:                fprintf(stderr, ")\n");
1.126     espie     310: }
1.93      espie     311:
1.131     espie     312: static void
                    313: internal_print_errors()
1.101     espie     314: {
1.126     espie     315:        Job *j, *k, *jnext;
1.131     espie     316:        int dying;
1.124     espie     317:
1.130     espie     318:        if (!errorJobs)
1.133     espie     319:                fprintf(stderr, "Stop in %s\n", shortened_curdir());
1.1       deraadt   320:
1.131     espie     321:        for (j = errorJobs; j != NULL; j = j->next)
                    322:                may_remove_target(j);
                    323:        dying = check_dying_signal();
                    324:        if (dying)
                    325:                quick_summary(dying);
1.126     espie     326:        while (errorJobs != NULL) {
                    327:                k = errorJobs;
                    328:                errorJobs = NULL;
                    329:                for (j = k; j != NULL; j = jnext) {
                    330:                        jnext = j->next;
                    331:                        if (j->location->fname == k->location->fname)
1.130     espie     332:                                print_error(j);
1.126     espie     333:                        else {
                    334:                                j->next = errorJobs;
                    335:                                errorJobs = j;
                    336:                        }
                    337:                }
1.101     espie     338:        }
1.88      espie     339: }
                    340:
1.131     espie     341: void
                    342: print_errors(void)
                    343: {
                    344:        handle_all_signals();
                    345:        internal_print_errors();
                    346: }
                    347:
1.57      espie     348: static void
1.124     espie     349: setup_signal(int sig)
1.57      espie     350: {
1.124     espie     351:        if (signal(sig, SIG_IGN) != SIG_IGN) {
                    352:                (void)signal(sig, notice_signal);
                    353:                sigaddset(&sigset, sig);
1.57      espie     354:        }
                    355: }
                    356:
                    357: static void
1.124     espie     358: notice_signal(int sig)
1.1       deraadt   359: {
1.126     espie     360:
1.124     espie     361:        switch(sig) {
1.117     espie     362:        case SIGINT:
                    363:                got_SIGINT++;
1.124     espie     364:                got_fatal = 1;
                    365:                break;
1.117     espie     366:        case SIGHUP:
                    367:                got_SIGHUP++;
1.124     espie     368:                got_fatal = 1;
                    369:                break;
1.117     espie     370:        case SIGQUIT:
                    371:                got_SIGQUIT++;
1.124     espie     372:                got_fatal = 1;
                    373:                break;
1.117     espie     374:        case SIGTERM:
                    375:                got_SIGTERM++;
1.124     espie     376:                got_fatal = 1;
1.117     espie     377:                break;
1.124     espie     378:        case SIGINFO:
                    379:                got_SIGINFO++;
1.117     espie     380:                break;
1.124     espie     381:        case SIGCHLD:
1.117     espie     382:                break;
1.66      espie     383:        }
1.124     espie     384: }
1.117     espie     385:
1.135     espie     386: static void
1.124     espie     387: setup_all_signals(void)
                    388: {
                    389:        sigemptyset(&sigset);
                    390:        sigemptyset(&emptyset);
                    391:        /*
                    392:         * Catch the four signals that POSIX specifies if they aren't ignored.
                    393:         * handle_signal will take care of calling JobInterrupt if appropriate.
                    394:         */
                    395:        setup_signal(SIGINT);
                    396:        setup_signal(SIGHUP);
                    397:        setup_signal(SIGQUIT);
                    398:        setup_signal(SIGTERM);
                    399:        /* Display running jobs on SIGINFO */
                    400:        setup_signal(SIGINFO);
                    401:        /* Have to see SIGCHLD */
                    402:        setup_signal(SIGCHLD);
                    403:        got_fatal = 0;
1.1       deraadt   404: }
                    405:
1.124     espie     406: static void
                    407: handle_siginfo(void)
1.1       deraadt   408: {
1.126     espie     409:        static BUFFER buf;
                    410:        static size_t length = 0;
                    411:
1.124     espie     412:        Job *job;
                    413:        bool first = true;
1.66      espie     414:
1.124     espie     415:        got_SIGINFO = 0;
                    416:        /* we have to store the info in a buffer, because status from all
                    417:         * makes running would get intermixed otherwise
1.66      espie     418:         */
1.124     espie     419:
1.126     espie     420:        if (length == 0) {
                    421:                Buf_Init(&buf, 0);
1.131     espie     422:                Buf_printf(&buf, "%s in ", Var_Value("MAKE"));
                    423:                buf_addcurdir(&buf);
                    424:                Buf_AddString(&buf, ": ");
1.126     espie     425:                length = Buf_Size(&buf);
                    426:        } else
                    427:                Buf_Truncate(&buf, length);
1.124     espie     428:
                    429:        for (job = runningJobs; job != NULL ; job = job->next) {
                    430:                if (!first)
                    431:                        Buf_puts(&buf, ", ");
                    432:                first = false;
                    433:                Buf_puts(&buf, job->node->name);
                    434:        }
                    435:        Buf_puts(&buf, first ? "nothing running\n" : "\n");
1.1       deraadt   436:
1.124     espie     437:        fputs(Buf_Retrieve(&buf), stderr);
1.1       deraadt   438: }
                    439:
1.131     espie     440: int
                    441: check_dying_signal(void)
                    442: {
                    443:        sigset_t set;
                    444:        if (dying_signal)
                    445:                return dying_signal;
                    446:        sigpending(&set);
                    447:        if (got_SIGINT || sigismember(&set, SIGINT))
                    448:                return dying_signal = SIGINT;
                    449:        if (got_SIGHUP || sigismember(&set, SIGHUP))
                    450:                return dying_signal = SIGHUP;
                    451:        if (got_SIGQUIT || sigismember(&set, SIGQUIT))
                    452:                return dying_signal = SIGQUIT;
                    453:        if (got_SIGTERM || sigismember(&set, SIGTERM))
                    454:                return dying_signal = SIGTERM;
                    455:        return 0;
                    456: }
                    457:
1.124     espie     458: void
                    459: handle_all_signals(void)
1.1       deraadt   460: {
1.124     espie     461:        if (got_SIGINFO)
                    462:                handle_siginfo();
                    463:        while (got_fatal) {
                    464:                got_fatal = 0;
                    465:                aborting = ABORT_INTERRUPT;
                    466:
                    467:                if (got_SIGINT) {
                    468:                        got_SIGINT=0;
1.126     espie     469:                        handle_fatal_signal(SIGINT);
1.124     espie     470:                }
                    471:                if (got_SIGHUP) {
                    472:                        got_SIGHUP=0;
1.126     espie     473:                        handle_fatal_signal(SIGHUP);
1.124     espie     474:                }
                    475:                if (got_SIGQUIT) {
                    476:                        got_SIGQUIT=0;
1.126     espie     477:                        handle_fatal_signal(SIGQUIT);
1.124     espie     478:                }
                    479:                if (got_SIGTERM) {
                    480:                        got_SIGTERM=0;
1.126     espie     481:                        handle_fatal_signal(SIGTERM);
1.124     espie     482:                }
                    483:        }
1.1       deraadt   484: }
                    485:
1.128     espie     486: static void
                    487: debug_vprintf(const char *fmt, va_list va)
                    488: {
                    489:        (void)printf("[%ld] ", (long)mypid);
                    490:        (void)vprintf(fmt, va);
                    491:        fflush(stdout);
                    492: }
                    493:
1.124     espie     494: void
                    495: debug_job_printf(const char *fmt, ...)
1.84      espie     496: {
                    497:        if (DEBUG(JOB)) {
                    498:                va_list va;
                    499:                va_start(va, fmt);
1.128     espie     500:                debug_vprintf(fmt, va);
                    501:                va_end(va);
                    502:        }
                    503: }
                    504:
                    505: static void
                    506: debug_kill_printf(const char *fmt, ...)
                    507: {
                    508:        if (DEBUG(KILL)) {
                    509:                va_list va;
                    510:                va_start(va, fmt);
                    511:                debug_vprintf(fmt, va);
1.84      espie     512:                va_end(va);
                    513:        }
                    514: }
1.119     espie     515:
1.1       deraadt   516: /*-
                    517:  *-----------------------------------------------------------------------
1.124     espie     518:  * postprocess_job  --
                    519:  *     Do final processing for the given job including updating
1.119     espie     520:  *     parents and starting new jobs as available/necessary.
1.1       deraadt   521:  *
                    522:  * Side Effects:
1.6       millert   523:  *     If we got an error and are aborting (aborting == ABORT_ERROR) and
1.1       deraadt   524:  *     the job list is now empty, we are done for the day.
1.101     espie     525:  *     If we recognized an error we set the aborting flag
1.1       deraadt   526:  *     to ABORT_ERROR so no more jobs will be started.
                    527:  *-----------------------------------------------------------------------
                    528:  */
                    529: /*ARGSUSED*/
1.112     espie     530:
1.1       deraadt   531: static void
1.124     espie     532: postprocess_job(Job *job, bool okay)
1.2       deraadt   533: {
1.124     espie     534:        if (okay &&
1.66      espie     535:            aborting != ABORT_ERROR &&
1.124     espie     536:            aborting != ABORT_INTERRUPT) {
1.66      espie     537:                /* As long as we aren't aborting and the job didn't return a
                    538:                 * non-zero status that we shouldn't ignore, we call
1.101     espie     539:                 * Make_Update to update the parents. */
1.108     espie     540:                job->node->built_status = MADE;
1.66      espie     541:                Make_Update(job->node);
1.124     espie     542:                free(job);
1.66      espie     543:        }
1.1       deraadt   544:
1.124     espie     545:        if (errorJobs != NULL && !keepgoing &&
1.117     espie     546:            aborting != ABORT_INTERRUPT)
1.66      espie     547:                aborting = ABORT_ERROR;
1.6       millert   548:
1.124     espie     549:        if (aborting == ABORT_ERROR && DEBUG(QUICKDEATH))
1.126     espie     550:                handle_fatal_signal(SIGINT);
1.117     espie     551:        if (aborting == ABORT_ERROR && Job_Empty())
1.124     espie     552:                Finish();
1.1       deraadt   553: }
                    554:
1.124     espie     555: /* expensive jobs handling: in order to avoid forking an exponential number
                    556:  * of jobs, make tries to figure out "recursive make" configurations.
                    557:  * It may err on the side of caution.
                    558:  * Basically, a command is "expensive" if it's likely to fork an extra
                    559:  * level of make: either by looking at the command proper, or if it has
                    560:  * some specific qualities ('+cmd' are likely to be recursive, as are
                    561:  * .MAKE: commands).  It's possible to explicitly say some targets are
                    562:  * expensive or cheap with .EXPENSIVE or .CHEAP.
                    563:  *
                    564:  * While an expensive command is running, no_new_jobs
                    565:  * is set, so jobs that would fork new processes are accumulated in the
                    566:  * heldJobs list instead.
                    567:  *
                    568:  * This heuristics is also used on error exit: we display silent commands
                    569:  * that failed, unless those ARE expensive commands: expensive commands
                    570:  * are likely to not be failing by themselves, but to be the result of
                    571:  * a cascade of failures in descendant makes.
                    572:  */
                    573: void
                    574: determine_expensive_job(Job *job)
                    575: {
                    576:        if (expensive_job(job)) {
                    577:                job->flags |= JOB_IS_EXPENSIVE;
                    578:                no_new_jobs = true;
                    579:        } else
                    580:                job->flags &= ~JOB_IS_EXPENSIVE;
                    581:        if (DEBUG(EXPENSIVE))
                    582:                fprintf(stderr, "[%ld] Target %s running %.50s: %s\n",
                    583:                    (long)mypid, job->node->name, job->cmd,
                    584:                    job->flags & JOB_IS_EXPENSIVE ? "expensive" : "cheap");
1.101     espie     585: }
                    586:
1.124     espie     587: static bool
                    588: expensive_job(Job *job)
1.1       deraadt   589: {
1.124     espie     590:        if (job->node->type & OP_CHEAP)
                    591:                return false;
                    592:        if (job->node->type & (OP_EXPENSIVE | OP_MAKE))
                    593:                return true;
                    594:        return expensive_command(job->cmd);
1.96      espie     595: }
                    596:
1.117     espie     597: static bool
                    598: expensive_command(const char *s)
1.1       deraadt   599: {
1.117     espie     600:        const char *p;
                    601:        bool include = false;
                    602:        bool expensive = false;
1.66      espie     603:
1.117     espie     604:        /* okay, comments are cheap, always */
                    605:        if (*s == '#')
                    606:                return false;
1.124     espie     607:        /* and commands we always execute are expensive */
                    608:        if (*s == '+')
                    609:                return true;
1.66      espie     610:
1.117     espie     611:        for (p = s; *p != '\0'; p++) {
                    612:                if (*p == ' ' || *p == '\t') {
                    613:                        include = false;
                    614:                        if (p[1] == '-' && p[2] == 'I')
                    615:                                include = true;
                    616:                }
                    617:                if (include)
                    618:                        continue;
                    619:                /* KMP variant, avoid looking twice at the same
                    620:                 * letter.
                    621:                 */
                    622:                if (*p != 'm')
                    623:                        continue;
                    624:                if (p[1] != 'a')
                    625:                        continue;
                    626:                p++;
                    627:                if (p[1] != 'k')
                    628:                        continue;
                    629:                p++;
                    630:                if (p[1] != 'e')
                    631:                        continue;
                    632:                p++;
                    633:                expensive = true;
                    634:                while (p[1] != '\0' && p[1] != ' ' && p[1] != '\t') {
1.124     espie     635:                        if (p[1] == '.' || p[1] == '/') {
1.117     espie     636:                                expensive = false;
                    637:                                break;
1.66      espie     638:                        }
1.117     espie     639:                        p++;
1.1       deraadt   640:                }
1.117     espie     641:                if (expensive)
                    642:                        return true;
1.1       deraadt   643:        }
1.117     espie     644:        return false;
                    645: }
                    646:
1.98      espie     647: static Job *
1.124     espie     648: prepare_job(GNode *gn)
1.78      espie     649: {
1.124     espie     650:        /* a new job is prepared unless its commands are bogus (we don't
                    651:         * have anything for it), or if we're in touch mode.
                    652:         *
                    653:         * Note that even in noexec mode, some commands may still run
                    654:         * thanks to the +cmd construct.
1.1       deraadt   655:         */
1.124     espie     656:        if (node_find_valid_commands(gn)) {
                    657:                if (touchFlag) {
                    658:                        Job_Touch(gn);
                    659:                        return NULL;
                    660:                } else {
                    661:                        Job *job;
1.6       millert   662:
1.124     espie     663:                        job = emalloc(sizeof(Job));
                    664:                        if (job == NULL)
                    665:                                Punt("can't create job: out of memory");
1.66      espie     666:
1.124     espie     667:                        job_attach_node(job, gn);
                    668:                        return job;
                    669:                }
1.66      espie     670:        } else {
1.124     espie     671:                node_failure(gn);
                    672:                return NULL;
1.1       deraadt   673:        }
1.124     espie     674: }
1.66      espie     675:
1.124     espie     676: static void
                    677: may_continue_job(Job *job)
                    678: {
                    679:        if (no_new_jobs) {
                    680:                if (DEBUG(EXPENSIVE))
                    681:                        fprintf(stderr, "[%ld] expensive -> hold %s\n",
                    682:                            (long)mypid, job->node->name);
                    683:                job->next = heldJobs;
                    684:                heldJobs = job;
                    685:        } else
                    686:                continue_job(job);
                    687: }
1.122     espie     688:
1.124     espie     689: static void
                    690: continue_job(Job *job)
                    691: {
                    692:        bool finished = job_run_next(job);
                    693:        if (finished)
                    694:                remove_job(job, true);
                    695:        else
                    696:                determine_expensive_job(job);
1.98      espie     697: }
1.1       deraadt   698:
1.98      espie     699: /*-
                    700:  *-----------------------------------------------------------------------
1.124     espie     701:  * Job_Make  --
1.98      espie     702:  *     Start a target-creation process going for the target described
                    703:  *     by the graph node gn.
                    704:  *
                    705:  * Side Effects:
1.124     espie     706:  *     A new Job node is created and  its commands continued, which
                    707:  *     may fork the first command of that job.
1.98      espie     708:  *-----------------------------------------------------------------------
                    709:  */
1.124     espie     710: void
                    711: Job_Make(GNode *gn)
1.98      espie     712: {
                    713:        Job *job;
1.124     espie     714:
                    715:        job = prepare_job(gn);
1.98      espie     716:        if (!job)
                    717:                return;
1.124     espie     718:        nJobs++;
                    719:        may_continue_job(job);
1.1       deraadt   720: }
                    721:
1.101     espie     722: static void
1.124     espie     723: determine_job_next_step(Job *job)
1.2       deraadt   724: {
1.124     espie     725:        bool okay;
                    726:        if (job->flags & JOB_IS_EXPENSIVE) {
                    727:                no_new_jobs = false;
                    728:                if (DEBUG(EXPENSIVE))
                    729:                        fprintf(stderr, "[%ld] "
                    730:                            "Returning from expensive target %s, "
                    731:                            "allowing new jobs\n", (long)mypid,
                    732:                            job->node->name);
                    733:        }
1.2       deraadt   734:
1.124     espie     735:        okay = job->exit_type == JOB_EXIT_OKAY;
                    736:        if (!okay || job->next_cmd == NULL)
                    737:                remove_job(job, okay);
                    738:        else
                    739:                may_continue_job(job);
1.101     espie     740: }
                    741:
                    742: static void
1.124     espie     743: remove_job(Job *job, bool okay)
1.101     espie     744: {
1.124     espie     745:        nJobs--;
                    746:        postprocess_job(job, okay);
                    747:        while (!no_new_jobs) {
                    748:                if (heldJobs != NULL) {
                    749:                        job = heldJobs;
                    750:                        heldJobs = heldJobs->next;
                    751:                        if (DEBUG(EXPENSIVE))
                    752:                                fprintf(stderr, "[%ld] cheap -> release %s\n",
                    753:                                    (long)mypid, job->node->name);
                    754:                        continue_job(job);
                    755:                } else
                    756:                        break;
1.2       deraadt   757:        }
                    758: }
1.111     espie     759:
1.124     espie     760: /*
                    761:  * job = reap_finished_job(pid):
                    762:  *     retrieve and remove a job from runningJobs, based on its pid
1.1       deraadt   763:  *
1.124     espie     764:  *     Note that we remove it right away, so that handle_signals()
                    765:  *     is accurate.
1.1       deraadt   766:  */
1.124     espie     767: static Job *
                    768: reap_finished_job(pid_t pid)
1.66      espie     769: {
1.124     espie     770:        Job **j, *job;
1.1       deraadt   771:
1.124     espie     772:        for (j = &runningJobs; *j != NULL; j = &((*j)->next))
                    773:                if ((*j)->pid == pid) {
                    774:                        job = *j;
                    775:                        *j = job->next;
                    776:                        return job;
1.66      espie     777:                }
1.1       deraadt   778:
1.124     espie     779:        return NULL;
1.101     espie     780: }
1.6       millert   781:
1.124     espie     782: /*
                    783:  * classic waitpid handler: retrieve as many dead children as possible.
                    784:  * returns true if succesful
                    785:  */
                    786: static bool
                    787: reap_jobs(void)
1.117     espie     788: {
1.124     espie     789:        pid_t pid;      /* pid of dead child */
                    790:        int status;     /* Exit/termination status */
                    791:        bool reaped = false;
1.117     espie     792:        Job *job;
                    793:
                    794:        while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0) {
1.124     espie     795:                reaped = true;
                    796:                job = reap_finished_job(pid);
1.1       deraadt   797:
1.124     espie     798:                if (job == NULL) {
                    799:                        Punt("Child (%ld) not in table?", (long)pid);
1.66      espie     800:                } else {
1.124     espie     801:                        job_handle_status(job, status);
                    802:                        determine_job_next_step(job);
1.1       deraadt   803:                }
                    804:        }
1.124     espie     805:        /* sanity check, should not happen */
                    806:        if (pid == -1 && errno == ECHILD && runningJobs != NULL)
                    807:                Punt("Process has no children, but runningJobs is not empty ?");
                    808:        return reaped;
1.1       deraadt   809: }
                    810:
                    811: void
1.124     espie     812: handle_running_jobs(void)
1.1       deraadt   813: {
1.124     espie     814:        sigset_t old;
                    815:        /* reaping children in the presence of caught signals */
1.91      espie     816:
1.124     espie     817:        /* first, we make sure to hold on new signals, to synchronize
                    818:         * reception of new stuff on sigsuspend
                    819:         */
                    820:        sigprocmask(SIG_BLOCK, &sigset, &old);
1.135     espie     821:        /* note this will NOT loop until runningJobs == NULL.
                    822:         * It's merely an optimisation, namely that we don't need to go
                    823:         * through the logic if no job is present. As soon as a job
                    824:         * gets reaped, we WILL exit the loop through the break.
                    825:         */
1.124     espie     826:        while (runningJobs != NULL) {
                    827:                /* did we already have pending stuff that advances things ?
                    828:                 * then handle_all_signals() will not return
                    829:                 * or reap_jobs() will reap_jobs()
                    830:                 */
                    831:                handle_all_signals();
                    832:                if (reap_jobs())
                    833:                        break;
                    834:                /* okay, so it's safe to suspend, we have nothing to do but
                    835:                 * wait...
                    836:                 */
                    837:                sigsuspend(&emptyset);
1.1       deraadt   838:        }
1.124     espie     839:        sigprocmask(SIG_SETMASK, &old, NULL);
1.1       deraadt   840: }
                    841:
1.111     espie     842: void
1.124     espie     843: handle_one_job(Job *job)
1.111     espie     844: {
1.124     espie     845:        int stat;
                    846:        int status;
                    847:        sigset_t old;
                    848:
                    849:        sigprocmask(SIG_BLOCK, &sigset, &old);
                    850:        while (1) {
                    851:                handle_all_signals();
                    852:                stat = waitpid(job->pid, &status, WNOHANG);
                    853:                if (stat == job->pid)
                    854:                        break;
                    855:                sigsuspend(&emptyset);
                    856:        }
                    857:        runningJobs = NULL;
                    858:        job_handle_status(job, status);
                    859:        sigprocmask(SIG_SETMASK, &old, NULL);
1.111     espie     860: }
                    861:
                    862: static void
                    863: loop_handle_running_jobs()
                    864: {
1.124     espie     865:        while (runningJobs != NULL)
1.111     espie     866:                handle_running_jobs();
                    867: }
1.1       deraadt   868:
                    869: void
1.83      espie     870: Job_Init(int maxproc)
1.1       deraadt   871: {
1.124     espie     872:        runningJobs = NULL;
                    873:        heldJobs = NULL;
                    874:        errorJobs = NULL;
1.117     espie     875:        maxJobs = maxproc;
1.124     espie     876:        mypid = getpid();
                    877:
1.117     espie     878:        nJobs = 0;
1.66      espie     879:
1.117     espie     880:        aborting = 0;
1.124     espie     881:        setup_all_signals();
1.1       deraadt   882: }
                    883:
1.41      espie     884: bool
1.117     espie     885: can_start_job(void)
1.1       deraadt   886: {
1.124     espie     887:        if (aborting || nJobs >= maxJobs)
1.117     espie     888:                return false;
1.119     espie     889:        else
1.117     espie     890:                return true;
1.1       deraadt   891: }
                    892:
1.41      espie     893: bool
1.56      espie     894: Job_Empty(void)
1.1       deraadt   895: {
1.124     espie     896:        return runningJobs == NULL;
1.1       deraadt   897: }
                    898:
                    899: /*-
                    900:  *-----------------------------------------------------------------------
1.126     espie     901:  * handle_fatal_signal --
                    902:  *     Handle the receipt of a fatal interrupt
1.1       deraadt   903:  *
                    904:  * Side Effects:
1.126     espie     905:  *     All children are killed. Another job may be started if there
                    906:  *     is an interrupt target and the signal was SIGINT.
1.1       deraadt   907:  *-----------------------------------------------------------------------
                    908:  */
                    909: static void
1.126     espie     910: handle_fatal_signal(int signo)
1.1       deraadt   911: {
1.124     espie     912:        Job *job;
1.66      espie     913:
1.128     espie     914:        debug_kill_printf("handle_fatal_signal(%d) called.\n", signo);
1.66      espie     915:
1.131     espie     916:        dying_signal = signo;
1.124     espie     917:        for (job = runningJobs; job != NULL; job = job->next) {
1.128     espie     918:                debug_kill_printf("passing to "
                    919:                    "child %ld running %s: %s\n", (long)job->pid,
                    920:                    job->node->name, really_kill(job, signo));
                    921:                may_remove_target(job);
1.2       deraadt   922:        }
1.1       deraadt   923:
1.124     espie     924:        if (signo == SIGINT && !touchFlag) {
1.78      espie     925:                if ((interrupt_node->type & OP_DUMMY) == 0) {
1.66      espie     926:                        ignoreErrors = false;
                    927:
1.124     espie     928:                        Job_Make(interrupt_node);
1.66      espie     929:                }
1.1       deraadt   930:        }
1.124     espie     931:        loop_handle_running_jobs();
1.131     espie     932:        internal_print_errors();
1.124     espie     933:
                    934:        /* die by that signal */
                    935:        sigprocmask(SIG_BLOCK, &sigset, NULL);
                    936:        signal(signo, SIG_DFL);
1.126     espie     937:        kill(getpid(), signo);
1.124     espie     938:        sigprocmask(SIG_SETMASK, &emptyset, NULL);
                    939:        /*NOTREACHED*/
1.131     espie     940:        fprintf(stderr, "This should never happen\n");
                    941:        exit(1);
1.1       deraadt   942: }
                    943:
                    944: /*
                    945:  *-----------------------------------------------------------------------
1.12      espie     946:  * Job_Finish --
1.1       deraadt   947:  *     Do final processing such as the running of the commands
1.6       millert   948:  *     attached to the .END target.
1.1       deraadt   949:  *
1.124     espie     950:  *     return true if fatal errors have happened.
1.1       deraadt   951:  *-----------------------------------------------------------------------
                    952:  */
1.124     espie     953: bool
1.56      espie     954: Job_Finish(void)
1.1       deraadt   955: {
1.134     espie     956:        bool problem = errorJobs != NULL;
1.124     espie     957:
1.116     espie     958:        if ((end_node->type & OP_DUMMY) == 0) {
1.134     espie     959:                if (problem) {
1.66      espie     960:                        Error("Errors reported so .END ignored");
                    961:                } else {
1.124     espie     962:                        Job_Make(end_node);
1.111     espie     963:                        loop_handle_running_jobs();
1.66      espie     964:                }
1.1       deraadt   965:        }
1.134     espie     966:        return problem;
1.1       deraadt   967: }
                    968:
1.124     espie     969: void
                    970: Job_Begin(void)
                    971: {
                    972:        if ((begin_node->type & OP_DUMMY) == 0) {
                    973:                Job_Make(begin_node);
                    974:                loop_handle_running_jobs();
                    975:        }
                    976: }
                    977:
1.1       deraadt   978: /*-
                    979:  *-----------------------------------------------------------------------
                    980:  * Job_Wait --
                    981:  *     Waits for all running jobs to finish and returns. Sets 'aborting'
                    982:  *     to ABORT_WAIT to prevent other jobs from starting.
                    983:  *
                    984:  * Side Effects:
                    985:  *     Currently running jobs finish.
                    986:  *
                    987:  *-----------------------------------------------------------------------
                    988:  */
                    989: void
1.56      espie     990: Job_Wait(void)
1.1       deraadt   991: {
1.66      espie     992:        aborting = ABORT_WAIT;
1.111     espie     993:        loop_handle_running_jobs();
1.66      espie     994:        aborting = 0;
1.1       deraadt   995: }
                    996:
                    997: /*-
                    998:  *-----------------------------------------------------------------------
                    999:  * Job_AbortAll --
                   1000:  *     Abort all currently running jobs without handling output or anything.
                   1001:  *     This function is to be called only in the event of a major
1.124     espie    1002:  *     error.
1.1       deraadt  1003:  *
                   1004:  * Side Effects:
1.124     espie    1005:  *     All children are killed
1.1       deraadt  1006:  *-----------------------------------------------------------------------
                   1007:  */
                   1008: void
1.56      espie    1009: Job_AbortAll(void)
1.1       deraadt  1010: {
1.66      espie    1011:        Job *job;       /* the job descriptor in that element */
                   1012:        int foo;
1.6       millert  1013:
1.66      espie    1014:        aborting = ABORT_ERROR;
1.6       millert  1015:
1.124     espie    1016:        for (job = runningJobs; job != NULL; job = job->next) {
1.126     espie    1017:                killpg(job->pid, SIGINT);
                   1018:                killpg(job->pid, SIGKILL);
1.1       deraadt  1019:        }
1.6       millert  1020:
1.66      espie    1021:        /*
                   1022:         * Catch as many children as want to report in at first, then give up
                   1023:         */
1.117     espie    1024:        while (waitpid(WAIT_ANY, &foo, WNOHANG) > 0)
1.66      espie    1025:                continue;
1.2       deraadt  1026: }