[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.154

1.154   ! espie       1: /*     $OpenBSD: job.c,v 1.153 2020/01/13 15:19:04 espie 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.124     espie     124: static bool    no_new_jobs;    /* Mark recursive shit so we shouldn't start
                    125:                                 * something else at the same time
                    126:                                 */
1.148     espie     127: bool sequential;
1.124     espie     128: Job *runningJobs;              /* Jobs currently running a process */
                    129: Job *errorJobs;                        /* Jobs in error at end */
1.148     espie     130: Job *availableJobs;            /* Pool of available jobs */
1.124     espie     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.148     espie     133: static Job *extra_job;         /* Needed for .INTERRUPT */
1.153     espie     134: static bool compatMode;                /* If we're running with compat.c don't call Make_Update */
1.1       deraadt   135:
1.128     espie     136: static volatile sig_atomic_t got_fatal;
1.112     espie     137:
1.124     espie     138: static volatile sig_atomic_t got_SIGINT, got_SIGHUP, got_SIGQUIT, got_SIGTERM,
1.128     espie     139:     got_SIGINFO;
1.112     espie     140:
1.124     espie     141: static sigset_t sigset, emptyset;
1.112     espie     142:
1.126     espie     143: static void handle_fatal_signal(int);
1.124     espie     144: static void handle_siginfo(void);
1.142     espie     145: static void postprocess_job(Job *);
1.124     espie     146: static void determine_job_next_step(Job *);
                    147: static void may_continue_job(Job *);
                    148: static Job *reap_finished_job(pid_t);
                    149: static bool reap_jobs(void);
1.146     espie     150: static void may_continue_heldback_jobs();
1.112     espie     151:
1.124     espie     152: static bool expensive_job(Job *);
                    153: static bool expensive_command(const char *);
                    154: static void setup_signal(int);
                    155: static void notice_signal(int);
                    156: static void setup_all_signals(void);
1.128     espie     157: static const char *really_kill(Job *, int);
                    158: static void debug_kill_printf(const char *, ...);
                    159: static void debug_vprintf(const char *, va_list);
                    160: static void may_remove_target(Job *);
1.131     espie     161: static void print_error(Job *);
                    162: static void internal_print_errors(void);
                    163:
                    164: static int dying_signal = 0;
                    165:
                    166: const char *   basedirectory = NULL;
1.127     espie     167:
1.128     espie     168: static const char *
                    169: really_kill(Job *job, int signo)
                    170: {
                    171:        pid_t pid = job->pid;
                    172:        if (getpgid(pid) != getpgrp()) {
                    173:                if (killpg(pid, signo) == 0)
                    174:                        return "group got signal";
                    175:                pid = -pid;
                    176:        } else {
                    177:                if (kill(pid, signo) == 0)
                    178:                        return "process got signal";
                    179:        }
1.138     natano    180:        if (errno == ESRCH)
1.128     espie     181:                job->flags |= JOB_LOST;
1.138     natano    182:        return strerror(errno);
1.128     espie     183: }
                    184:
                    185: static void
                    186: may_remove_target(Job *j)
                    187: {
1.131     espie     188:        int dying = check_dying_signal();
                    189:
                    190:        if (dying && !noExecute && !Targ_Precious(j->node)) {
1.128     espie     191:                const char *file = Var(TARGET_INDEX, j->node);
                    192:                int r = eunlink(file);
                    193:
                    194:                if (DEBUG(JOB) && r == -1)
                    195:                        fprintf(stderr, " *** would unlink %s\n", file);
                    196:                if (r != -1)
                    197:                        fprintf(stderr, " *** %s removed\n", file);
1.127     espie     198:        }
                    199: }
1.126     espie     200:
1.131     espie     201: static void
                    202: buf_addcurdir(BUFFER *buf)
                    203: {
                    204:        const char *v = Var_Value(".CURDIR");
                    205:        if (basedirectory != NULL) {
                    206:                size_t len = strlen(basedirectory);
                    207:                if (strncmp(basedirectory, v, len) == 0 &&
                    208:                    v[len] == '/') {
                    209:                        v += len+1;
                    210:                } else if (strcmp(basedirectory, v) == 0) {
                    211:                        Buf_AddString(buf, ".");
                    212:                        return;
                    213:                }
                    214:        }
                    215:        Buf_AddString(buf, v);
                    216: }
                    217:
                    218: static const char *
                    219: shortened_curdir(void)
                    220: {
                    221:        static BUFFER buf;
1.140     espie     222:        static bool first = true;
1.131     espie     223:        if (first) {
                    224:                Buf_Init(&buf, 0);
                    225:                buf_addcurdir(&buf);
                    226:                first = false;
                    227:        }
                    228:        return Buf_Retrieve(&buf);
                    229: }
                    230:
                    231: static void
                    232: quick_error(Job *j, int signo, bool first)
                    233: {
                    234:        if (first) {
                    235:                fprintf(stderr, "*** Signal SIG%s", sys_signame[signo]);
                    236:                fprintf(stderr, " in %s (", shortened_curdir());
                    237:        } else
                    238:                fprintf(stderr, " ");
                    239:
                    240:        fprintf(stderr, "%s", j->node->name);
                    241:        free(j->cmd);
                    242: }
                    243:
1.126     espie     244: static void
1.130     espie     245: print_error(Job *j)
1.126     espie     246: {
1.130     espie     247:        static bool first = true;
1.131     espie     248:        BUFFER buf;
                    249:
                    250:        Buf_Init(&buf, 0);
1.126     espie     251:
1.131     espie     252:        if (j->exit_type == JOB_EXIT_BAD)
                    253:                Buf_printf(&buf, "*** Error %d", j->code);
                    254:        else if (j->exit_type == JOB_SIGNALED) {
1.128     espie     255:                if (j->code < NSIG)
1.131     espie     256:                        Buf_printf(&buf, "*** Signal SIG%s",
1.128     espie     257:                            sys_signame[j->code]);
                    258:                else
1.131     espie     259:                        Buf_printf(&buf, "*** unknown signal %d", j->code);
1.128     espie     260:        } else
1.131     espie     261:                Buf_printf(&buf, "*** Should not happen %d/%d",
                    262:                    j->exit_type, j->code);
1.128     espie     263:        if (DEBUG(KILL) && (j->flags & JOB_LOST))
1.131     espie     264:                Buf_AddChar(&buf, '!');
1.130     espie     265:        if (first) {
1.131     espie     266:                Buf_AddString(&buf, " in ");
                    267:                buf_addcurdir(&buf);
1.130     espie     268:                first = false;
                    269:        }
1.131     espie     270:        Buf_printf(&buf, " (%s:%lu", j->location->fname, j->location->lineno);
                    271:        Buf_printf(&buf, " '%s'", j->node->name);
                    272:        if ((j->flags & (JOB_SILENT | JOB_IS_EXPENSIVE)) == JOB_SILENT
1.132     espie     273:            && Buf_Size(&buf) < 140-2) {
1.131     espie     274:                size_t len = strlen(j->cmd);
                    275:                Buf_AddString(&buf, ": ");
                    276:                if (len + Buf_Size(&buf) < 140)
                    277:                        Buf_AddString(&buf, j->cmd);
                    278:                else {
                    279:                        Buf_AddChars(&buf, 140 - Buf_Size(&buf), j->cmd);
                    280:                        Buf_AddString(&buf, "...");
                    281:                }
                    282:        }
                    283:        fprintf(stderr, "%s)\n", Buf_Retrieve(&buf));
                    284:        Buf_Destroy(&buf);
1.126     espie     285:        free(j->cmd);
1.131     espie     286: }
                    287: static void
                    288: quick_summary(int signo)
                    289: {
                    290:        Job *j, *k, *jnext;
                    291:        bool first = true;
                    292:
                    293:        k = errorJobs;
                    294:        errorJobs = NULL;
                    295:        for (j = k; j != NULL; j = jnext) {
                    296:                jnext = j->next;
                    297:                if ((j->exit_type == JOB_EXIT_BAD && j->code == signo+128) ||
                    298:                    (j->exit_type == JOB_SIGNALED && j->code == signo)) {
                    299:                        quick_error(j, signo, first);
                    300:                        first = false;
                    301:                } else {
                    302:                        j->next = errorJobs;
                    303:                        errorJobs = j;
                    304:                }
                    305:        }
                    306:        if (!first)
                    307:                fprintf(stderr, ")\n");
1.126     espie     308: }
1.93      espie     309:
1.131     espie     310: static void
                    311: internal_print_errors()
1.101     espie     312: {
1.126     espie     313:        Job *j, *k, *jnext;
1.131     espie     314:        int dying;
1.124     espie     315:
1.130     espie     316:        if (!errorJobs)
1.133     espie     317:                fprintf(stderr, "Stop in %s\n", shortened_curdir());
1.1       deraadt   318:
1.131     espie     319:        for (j = errorJobs; j != NULL; j = j->next)
                    320:                may_remove_target(j);
                    321:        dying = check_dying_signal();
                    322:        if (dying)
                    323:                quick_summary(dying);
1.143     espie     324:        /* Print errors grouped by file name. */
1.126     espie     325:        while (errorJobs != NULL) {
1.143     espie     326:                /* Select the first job. */
1.126     espie     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.143     espie     332:                                /* Print errors with the same filename. */
1.130     espie     333:                                print_error(j);
1.126     espie     334:                        else {
1.143     espie     335:                                /* Keep others for the next iteration. */
1.126     espie     336:                                j->next = errorJobs;
                    337:                                errorJobs = j;
                    338:                        }
                    339:                }
1.101     espie     340:        }
1.88      espie     341: }
                    342:
1.131     espie     343: void
                    344: print_errors(void)
                    345: {
                    346:        handle_all_signals();
                    347:        internal_print_errors();
                    348: }
                    349:
1.57      espie     350: static void
1.124     espie     351: setup_signal(int sig)
1.57      espie     352: {
1.124     espie     353:        if (signal(sig, SIG_IGN) != SIG_IGN) {
                    354:                (void)signal(sig, notice_signal);
                    355:                sigaddset(&sigset, sig);
1.57      espie     356:        }
                    357: }
                    358:
                    359: static void
1.124     espie     360: notice_signal(int sig)
1.1       deraadt   361: {
1.126     espie     362:
1.124     espie     363:        switch(sig) {
1.117     espie     364:        case SIGINT:
                    365:                got_SIGINT++;
1.124     espie     366:                got_fatal = 1;
                    367:                break;
1.117     espie     368:        case SIGHUP:
                    369:                got_SIGHUP++;
1.124     espie     370:                got_fatal = 1;
                    371:                break;
1.117     espie     372:        case SIGQUIT:
                    373:                got_SIGQUIT++;
1.124     espie     374:                got_fatal = 1;
                    375:                break;
1.117     espie     376:        case SIGTERM:
                    377:                got_SIGTERM++;
1.124     espie     378:                got_fatal = 1;
1.117     espie     379:                break;
1.124     espie     380:        case SIGINFO:
                    381:                got_SIGINFO++;
1.117     espie     382:                break;
1.124     espie     383:        case SIGCHLD:
1.117     espie     384:                break;
1.66      espie     385:        }
1.124     espie     386: }
1.117     espie     387:
1.135     espie     388: static void
1.124     espie     389: setup_all_signals(void)
                    390: {
                    391:        sigemptyset(&sigset);
                    392:        sigemptyset(&emptyset);
                    393:        /*
                    394:         * Catch the four signals that POSIX specifies if they aren't ignored.
                    395:         * handle_signal will take care of calling JobInterrupt if appropriate.
                    396:         */
                    397:        setup_signal(SIGINT);
                    398:        setup_signal(SIGHUP);
                    399:        setup_signal(SIGQUIT);
                    400:        setup_signal(SIGTERM);
                    401:        /* Display running jobs on SIGINFO */
                    402:        setup_signal(SIGINFO);
                    403:        /* Have to see SIGCHLD */
                    404:        setup_signal(SIGCHLD);
                    405:        got_fatal = 0;
1.1       deraadt   406: }
                    407:
1.124     espie     408: static void
                    409: handle_siginfo(void)
1.1       deraadt   410: {
1.126     espie     411:        static BUFFER buf;
                    412:        static size_t length = 0;
                    413:
1.124     espie     414:        Job *job;
                    415:        bool first = true;
1.66      espie     416:
1.124     espie     417:        got_SIGINFO = 0;
                    418:        /* we have to store the info in a buffer, because status from all
                    419:         * makes running would get intermixed otherwise
1.66      espie     420:         */
1.124     espie     421:
1.126     espie     422:        if (length == 0) {
                    423:                Buf_Init(&buf, 0);
1.131     espie     424:                Buf_printf(&buf, "%s in ", Var_Value("MAKE"));
                    425:                buf_addcurdir(&buf);
                    426:                Buf_AddString(&buf, ": ");
1.126     espie     427:                length = Buf_Size(&buf);
                    428:        } else
                    429:                Buf_Truncate(&buf, length);
1.124     espie     430:
                    431:        for (job = runningJobs; job != NULL ; job = job->next) {
                    432:                if (!first)
                    433:                        Buf_puts(&buf, ", ");
                    434:                first = false;
                    435:                Buf_puts(&buf, job->node->name);
                    436:        }
                    437:        Buf_puts(&buf, first ? "nothing running\n" : "\n");
1.1       deraadt   438:
1.124     espie     439:        fputs(Buf_Retrieve(&buf), stderr);
1.1       deraadt   440: }
                    441:
1.131     espie     442: int
                    443: check_dying_signal(void)
                    444: {
                    445:        sigset_t set;
                    446:        if (dying_signal)
                    447:                return dying_signal;
                    448:        sigpending(&set);
                    449:        if (got_SIGINT || sigismember(&set, SIGINT))
                    450:                return dying_signal = SIGINT;
                    451:        if (got_SIGHUP || sigismember(&set, SIGHUP))
                    452:                return dying_signal = SIGHUP;
                    453:        if (got_SIGQUIT || sigismember(&set, SIGQUIT))
                    454:                return dying_signal = SIGQUIT;
                    455:        if (got_SIGTERM || sigismember(&set, SIGTERM))
                    456:                return dying_signal = SIGTERM;
                    457:        return 0;
                    458: }
                    459:
1.124     espie     460: void
                    461: handle_all_signals(void)
1.1       deraadt   462: {
1.124     espie     463:        if (got_SIGINFO)
                    464:                handle_siginfo();
                    465:        while (got_fatal) {
                    466:                got_fatal = 0;
                    467:                aborting = ABORT_INTERRUPT;
                    468:
                    469:                if (got_SIGINT) {
                    470:                        got_SIGINT=0;
1.126     espie     471:                        handle_fatal_signal(SIGINT);
1.124     espie     472:                }
                    473:                if (got_SIGHUP) {
                    474:                        got_SIGHUP=0;
1.126     espie     475:                        handle_fatal_signal(SIGHUP);
1.124     espie     476:                }
                    477:                if (got_SIGQUIT) {
                    478:                        got_SIGQUIT=0;
1.126     espie     479:                        handle_fatal_signal(SIGQUIT);
1.124     espie     480:                }
                    481:                if (got_SIGTERM) {
                    482:                        got_SIGTERM=0;
1.126     espie     483:                        handle_fatal_signal(SIGTERM);
1.124     espie     484:                }
                    485:        }
1.1       deraadt   486: }
                    487:
1.128     espie     488: static void
                    489: debug_vprintf(const char *fmt, va_list va)
                    490: {
                    491:        (void)printf("[%ld] ", (long)mypid);
                    492:        (void)vprintf(fmt, va);
                    493:        fflush(stdout);
                    494: }
                    495:
1.124     espie     496: void
                    497: debug_job_printf(const char *fmt, ...)
1.84      espie     498: {
                    499:        if (DEBUG(JOB)) {
                    500:                va_list va;
                    501:                va_start(va, fmt);
1.128     espie     502:                debug_vprintf(fmt, va);
                    503:                va_end(va);
                    504:        }
                    505: }
                    506:
                    507: static void
                    508: debug_kill_printf(const char *fmt, ...)
                    509: {
                    510:        if (DEBUG(KILL)) {
                    511:                va_list va;
                    512:                va_start(va, fmt);
                    513:                debug_vprintf(fmt, va);
1.84      espie     514:                va_end(va);
                    515:        }
                    516: }
1.119     espie     517:
1.1       deraadt   518: /*-
                    519:  *-----------------------------------------------------------------------
1.124     espie     520:  * postprocess_job  --
                    521:  *     Do final processing for the given job including updating
1.119     espie     522:  *     parents and starting new jobs as available/necessary.
1.1       deraadt   523:  *
                    524:  * Side Effects:
1.6       millert   525:  *     If we got an error and are aborting (aborting == ABORT_ERROR) and
1.1       deraadt   526:  *     the job list is now empty, we are done for the day.
1.101     espie     527:  *     If we recognized an error we set the aborting flag
1.1       deraadt   528:  *     to ABORT_ERROR so no more jobs will be started.
                    529:  *-----------------------------------------------------------------------
                    530:  */
                    531: /*ARGSUSED*/
1.112     espie     532:
1.1       deraadt   533: static void
1.142     espie     534: postprocess_job(Job *job)
1.2       deraadt   535: {
1.142     espie     536:        if (job->exit_type == JOB_EXIT_OKAY &&
1.66      espie     537:            aborting != ABORT_ERROR &&
1.124     espie     538:            aborting != ABORT_INTERRUPT) {
1.66      espie     539:                /* As long as we aren't aborting and the job didn't return a
                    540:                 * non-zero status that we shouldn't ignore, we call
1.101     espie     541:                 * Make_Update to update the parents. */
1.141     espie     542:                job->node->built_status = REBUILT;
1.153     espie     543:                if (!compatMode)
                    544:                        Make_Update(job->node);
1.152     espie     545:        }
                    546:        if (job->flags & JOB_KEEPERROR) {
                    547:                job->next = errorJobs;
                    548:                errorJobs = job;
                    549:        } else {
1.148     espie     550:                job->next = availableJobs;
                    551:                availableJobs = job;
                    552:        }
1.1       deraadt   553:
1.145     espie     554:        if (errorJobs != NULL && aborting != ABORT_INTERRUPT)
1.66      espie     555:                aborting = ABORT_ERROR;
1.6       millert   556:
1.124     espie     557:        if (aborting == ABORT_ERROR && DEBUG(QUICKDEATH))
1.126     espie     558:                handle_fatal_signal(SIGINT);
1.117     espie     559:        if (aborting == ABORT_ERROR && Job_Empty())
1.124     espie     560:                Finish();
1.1       deraadt   561: }
                    562:
1.124     espie     563: /* expensive jobs handling: in order to avoid forking an exponential number
                    564:  * of jobs, make tries to figure out "recursive make" configurations.
                    565:  * It may err on the side of caution.
                    566:  * Basically, a command is "expensive" if it's likely to fork an extra
                    567:  * level of make: either by looking at the command proper, or if it has
                    568:  * some specific qualities ('+cmd' are likely to be recursive, as are
                    569:  * .MAKE: commands).  It's possible to explicitly say some targets are
                    570:  * expensive or cheap with .EXPENSIVE or .CHEAP.
                    571:  *
                    572:  * While an expensive command is running, no_new_jobs
                    573:  * is set, so jobs that would fork new processes are accumulated in the
                    574:  * heldJobs list instead.
                    575:  *
1.147     espie     576:  * XXX This heuristics is also used on error exit: we display silent commands
                    577:  * that failed, unless those ARE expensive commands: expensive commands are
                    578:  * likely to not be failing by themselves, but to be the result of a cascade of
                    579:  * failures in descendant makes.
1.124     espie     580:  */
                    581: void
                    582: determine_expensive_job(Job *job)
                    583: {
                    584:        if (expensive_job(job)) {
                    585:                job->flags |= JOB_IS_EXPENSIVE;
                    586:                no_new_jobs = true;
                    587:        } else
                    588:                job->flags &= ~JOB_IS_EXPENSIVE;
                    589:        if (DEBUG(EXPENSIVE))
                    590:                fprintf(stderr, "[%ld] Target %s running %.50s: %s\n",
                    591:                    (long)mypid, job->node->name, job->cmd,
                    592:                    job->flags & JOB_IS_EXPENSIVE ? "expensive" : "cheap");
1.101     espie     593: }
                    594:
1.124     espie     595: static bool
                    596: expensive_job(Job *job)
1.1       deraadt   597: {
1.124     espie     598:        if (job->node->type & OP_CHEAP)
                    599:                return false;
                    600:        if (job->node->type & (OP_EXPENSIVE | OP_MAKE))
                    601:                return true;
                    602:        return expensive_command(job->cmd);
1.96      espie     603: }
                    604:
1.117     espie     605: static bool
                    606: expensive_command(const char *s)
1.1       deraadt   607: {
1.117     espie     608:        const char *p;
                    609:        bool include = false;
                    610:        bool expensive = false;
1.66      espie     611:
1.117     espie     612:        /* okay, comments are cheap, always */
                    613:        if (*s == '#')
                    614:                return false;
1.124     espie     615:        /* and commands we always execute are expensive */
                    616:        if (*s == '+')
                    617:                return true;
1.66      espie     618:
1.117     espie     619:        for (p = s; *p != '\0'; p++) {
                    620:                if (*p == ' ' || *p == '\t') {
                    621:                        include = false;
                    622:                        if (p[1] == '-' && p[2] == 'I')
                    623:                                include = true;
                    624:                }
                    625:                if (include)
                    626:                        continue;
                    627:                /* KMP variant, avoid looking twice at the same
                    628:                 * letter.
                    629:                 */
                    630:                if (*p != 'm')
                    631:                        continue;
                    632:                if (p[1] != 'a')
                    633:                        continue;
                    634:                p++;
                    635:                if (p[1] != 'k')
                    636:                        continue;
                    637:                p++;
                    638:                if (p[1] != 'e')
                    639:                        continue;
                    640:                p++;
                    641:                expensive = true;
                    642:                while (p[1] != '\0' && p[1] != ' ' && p[1] != '\t') {
1.124     espie     643:                        if (p[1] == '.' || p[1] == '/') {
1.117     espie     644:                                expensive = false;
                    645:                                break;
1.66      espie     646:                        }
1.117     espie     647:                        p++;
1.1       deraadt   648:                }
1.117     espie     649:                if (expensive)
                    650:                        return true;
1.1       deraadt   651:        }
1.117     espie     652:        return false;
                    653: }
                    654:
1.124     espie     655: static void
                    656: may_continue_job(Job *job)
                    657: {
                    658:        if (no_new_jobs) {
                    659:                if (DEBUG(EXPENSIVE))
                    660:                        fprintf(stderr, "[%ld] expensive -> hold %s\n",
                    661:                            (long)mypid, job->node->name);
                    662:                job->next = heldJobs;
                    663:                heldJobs = job;
1.151     espie     664:        } else {
                    665:                bool finished = job_run_next(job);
                    666:                if (finished)
                    667:                        postprocess_job(job);
                    668:                else if (!sequential)
                    669:                        determine_expensive_job(job);
                    670:        }
1.98      espie     671: }
1.1       deraadt   672:
1.98      espie     673: /*-
                    674:  *-----------------------------------------------------------------------
1.124     espie     675:  * Job_Make  --
1.98      espie     676:  *     Start a target-creation process going for the target described
                    677:  *     by the graph node gn.
                    678:  *
                    679:  * Side Effects:
1.124     espie     680:  *     A new Job node is created and  its commands continued, which
                    681:  *     may fork the first command of that job.
1.98      espie     682:  *-----------------------------------------------------------------------
                    683:  */
1.124     espie     684: void
                    685: Job_Make(GNode *gn)
1.98      espie     686: {
1.154   ! espie     687:        Job *job = availableJobs;
1.124     espie     688:
1.154   ! espie     689:        assert(job != NULL);
        !           690:        availableJobs = availableJobs->next;
        !           691:        job_attach_node(job, gn);
1.124     espie     692:        may_continue_job(job);
1.1       deraadt   693: }
                    694:
1.101     espie     695: static void
1.124     espie     696: determine_job_next_step(Job *job)
1.2       deraadt   697: {
1.124     espie     698:        if (job->flags & JOB_IS_EXPENSIVE) {
                    699:                no_new_jobs = false;
                    700:                if (DEBUG(EXPENSIVE))
                    701:                        fprintf(stderr, "[%ld] "
                    702:                            "Returning from expensive target %s, "
                    703:                            "allowing new jobs\n", (long)mypid,
                    704:                            job->node->name);
                    705:        }
1.2       deraadt   706:
1.142     espie     707:        if (job->exit_type != JOB_EXIT_OKAY || job->next_cmd == NULL)
1.150     espie     708:                postprocess_job(job);
1.124     espie     709:        else
                    710:                may_continue_job(job);
1.101     espie     711: }
                    712:
                    713: static void
1.142     espie     714: remove_job(Job *job)
1.101     espie     715: {
1.142     espie     716:        postprocess_job(job);
1.146     espie     717: }
                    718:
                    719: static void
                    720: may_continue_heldback_jobs()
                    721: {
1.124     espie     722:        while (!no_new_jobs) {
                    723:                if (heldJobs != NULL) {
1.146     espie     724:                        Job *job = heldJobs;
1.124     espie     725:                        heldJobs = heldJobs->next;
                    726:                        if (DEBUG(EXPENSIVE))
                    727:                                fprintf(stderr, "[%ld] cheap -> release %s\n",
                    728:                                    (long)mypid, job->node->name);
1.151     espie     729:                        may_continue_job(job);
1.124     espie     730:                } else
                    731:                        break;
1.2       deraadt   732:        }
                    733: }
1.111     espie     734:
1.124     espie     735: /*
                    736:  * job = reap_finished_job(pid):
                    737:  *     retrieve and remove a job from runningJobs, based on its pid
1.1       deraadt   738:  *
1.124     espie     739:  *     Note that we remove it right away, so that handle_signals()
                    740:  *     is accurate.
1.1       deraadt   741:  */
1.124     espie     742: static Job *
                    743: reap_finished_job(pid_t pid)
1.66      espie     744: {
1.124     espie     745:        Job **j, *job;
1.1       deraadt   746:
1.124     espie     747:        for (j = &runningJobs; *j != NULL; j = &((*j)->next))
                    748:                if ((*j)->pid == pid) {
                    749:                        job = *j;
                    750:                        *j = job->next;
                    751:                        return job;
1.66      espie     752:                }
1.1       deraadt   753:
1.124     espie     754:        return NULL;
1.101     espie     755: }
1.6       millert   756:
1.124     espie     757: /*
                    758:  * classic waitpid handler: retrieve as many dead children as possible.
                    759:  * returns true if succesful
                    760:  */
                    761: static bool
                    762: reap_jobs(void)
1.117     espie     763: {
1.124     espie     764:        pid_t pid;      /* pid of dead child */
                    765:        int status;     /* Exit/termination status */
                    766:        bool reaped = false;
1.117     espie     767:        Job *job;
                    768:
                    769:        while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0) {
1.124     espie     770:                reaped = true;
                    771:                job = reap_finished_job(pid);
1.1       deraadt   772:
1.124     espie     773:                if (job == NULL) {
                    774:                        Punt("Child (%ld) not in table?", (long)pid);
1.66      espie     775:                } else {
1.149     espie     776:                        handle_job_status(job, status);
1.124     espie     777:                        determine_job_next_step(job);
1.1       deraadt   778:                }
1.146     espie     779:                may_continue_heldback_jobs();
1.1       deraadt   780:        }
1.124     espie     781:        /* sanity check, should not happen */
                    782:        if (pid == -1 && errno == ECHILD && runningJobs != NULL)
                    783:                Punt("Process has no children, but runningJobs is not empty ?");
                    784:        return reaped;
1.1       deraadt   785: }
                    786:
                    787: void
1.124     espie     788: handle_running_jobs(void)
1.1       deraadt   789: {
1.124     espie     790:        sigset_t old;
                    791:        /* reaping children in the presence of caught signals */
1.91      espie     792:
1.124     espie     793:        /* first, we make sure to hold on new signals, to synchronize
                    794:         * reception of new stuff on sigsuspend
                    795:         */
                    796:        sigprocmask(SIG_BLOCK, &sigset, &old);
1.135     espie     797:        /* note this will NOT loop until runningJobs == NULL.
                    798:         * It's merely an optimisation, namely that we don't need to go
                    799:         * through the logic if no job is present. As soon as a job
                    800:         * gets reaped, we WILL exit the loop through the break.
                    801:         */
1.124     espie     802:        while (runningJobs != NULL) {
                    803:                /* did we already have pending stuff that advances things ?
                    804:                 * then handle_all_signals() will not return
                    805:                 * or reap_jobs() will reap_jobs()
                    806:                 */
                    807:                handle_all_signals();
                    808:                if (reap_jobs())
                    809:                        break;
                    810:                /* okay, so it's safe to suspend, we have nothing to do but
                    811:                 * wait...
                    812:                 */
                    813:                sigsuspend(&emptyset);
1.1       deraadt   814:        }
1.124     espie     815:        sigprocmask(SIG_SETMASK, &old, NULL);
1.1       deraadt   816: }
                    817:
1.111     espie     818: void
                    819: loop_handle_running_jobs()
                    820: {
1.124     espie     821:        while (runningJobs != NULL)
1.111     espie     822:                handle_running_jobs();
                    823: }
1.1       deraadt   824:
                    825: void
1.153     espie     826: Job_Init(int maxJobs, bool compat)
1.1       deraadt   827: {
1.148     espie     828:        Job *j;
                    829:        int i;
                    830:
1.124     espie     831:        runningJobs = NULL;
                    832:        heldJobs = NULL;
                    833:        errorJobs = NULL;
1.148     espie     834:        availableJobs = NULL;
                    835:        sequential = maxJobs == 1;
1.153     espie     836:        compatMode = compat;
1.148     espie     837:
                    838:        /* we allocate n+1 jobs, since we may need an extra job for
                    839:         * running .INTERRUPT.  */
                    840:        j = ereallocarray(NULL, sizeof(Job), maxJobs+1);
                    841:        for (i = 0; i != maxJobs; i++) {
                    842:                j[i].next = availableJobs;
                    843:                availableJobs = &j[i];
                    844:        }
                    845:        extra_job = &j[maxJobs];
1.124     espie     846:        mypid = getpid();
                    847:
1.117     espie     848:        aborting = 0;
1.124     espie     849:        setup_all_signals();
1.1       deraadt   850: }
                    851:
1.41      espie     852: bool
1.117     espie     853: can_start_job(void)
1.1       deraadt   854: {
1.148     espie     855:        if (aborting || availableJobs == NULL)
1.117     espie     856:                return false;
1.119     espie     857:        else
1.117     espie     858:                return true;
1.1       deraadt   859: }
                    860:
1.41      espie     861: bool
1.56      espie     862: Job_Empty(void)
1.1       deraadt   863: {
1.124     espie     864:        return runningJobs == NULL;
1.1       deraadt   865: }
                    866:
                    867: /*-
                    868:  *-----------------------------------------------------------------------
1.126     espie     869:  * handle_fatal_signal --
                    870:  *     Handle the receipt of a fatal interrupt
1.1       deraadt   871:  *
                    872:  * Side Effects:
1.126     espie     873:  *     All children are killed. Another job may be started if there
                    874:  *     is an interrupt target and the signal was SIGINT.
1.1       deraadt   875:  *-----------------------------------------------------------------------
                    876:  */
                    877: static void
1.126     espie     878: handle_fatal_signal(int signo)
1.1       deraadt   879: {
1.124     espie     880:        Job *job;
1.66      espie     881:
1.128     espie     882:        debug_kill_printf("handle_fatal_signal(%d) called.\n", signo);
1.66      espie     883:
1.131     espie     884:        dying_signal = signo;
1.124     espie     885:        for (job = runningJobs; job != NULL; job = job->next) {
1.128     espie     886:                debug_kill_printf("passing to "
                    887:                    "child %ld running %s: %s\n", (long)job->pid,
                    888:                    job->node->name, really_kill(job, signo));
                    889:                may_remove_target(job);
1.2       deraadt   890:        }
1.1       deraadt   891:
1.124     espie     892:        if (signo == SIGINT && !touchFlag) {
1.78      espie     893:                if ((interrupt_node->type & OP_DUMMY) == 0) {
1.66      espie     894:                        ignoreErrors = false;
1.148     espie     895:                        extra_job->next = availableJobs;
                    896:                        availableJobs = extra_job;
1.124     espie     897:                        Job_Make(interrupt_node);
1.66      espie     898:                }
1.1       deraadt   899:        }
1.124     espie     900:        loop_handle_running_jobs();
1.131     espie     901:        internal_print_errors();
1.124     espie     902:
                    903:        /* die by that signal */
                    904:        sigprocmask(SIG_BLOCK, &sigset, NULL);
                    905:        signal(signo, SIG_DFL);
1.126     espie     906:        kill(getpid(), signo);
1.124     espie     907:        sigprocmask(SIG_SETMASK, &emptyset, NULL);
                    908:        /*NOTREACHED*/
1.131     espie     909:        fprintf(stderr, "This should never happen\n");
                    910:        exit(1);
1.1       deraadt   911: }
                    912:
                    913: /*
                    914:  *-----------------------------------------------------------------------
1.12      espie     915:  * Job_Finish --
1.1       deraadt   916:  *     Do final processing such as the running of the commands
1.6       millert   917:  *     attached to the .END target.
1.1       deraadt   918:  *
1.124     espie     919:  *     return true if fatal errors have happened.
1.1       deraadt   920:  *-----------------------------------------------------------------------
                    921:  */
1.124     espie     922: bool
1.56      espie     923: Job_Finish(void)
1.1       deraadt   924: {
1.134     espie     925:        bool problem = errorJobs != NULL;
1.124     espie     926:
1.116     espie     927:        if ((end_node->type & OP_DUMMY) == 0) {
1.134     espie     928:                if (problem) {
1.66      espie     929:                        Error("Errors reported so .END ignored");
                    930:                } else {
1.124     espie     931:                        Job_Make(end_node);
1.111     espie     932:                        loop_handle_running_jobs();
1.66      espie     933:                }
1.1       deraadt   934:        }
1.134     espie     935:        return problem;
1.1       deraadt   936: }
                    937:
1.124     espie     938: void
                    939: Job_Begin(void)
                    940: {
                    941:        if ((begin_node->type & OP_DUMMY) == 0) {
                    942:                Job_Make(begin_node);
                    943:                loop_handle_running_jobs();
                    944:        }
                    945: }
                    946:
1.1       deraadt   947: /*-
                    948:  *-----------------------------------------------------------------------
                    949:  * Job_Wait --
                    950:  *     Waits for all running jobs to finish and returns. Sets 'aborting'
                    951:  *     to ABORT_WAIT to prevent other jobs from starting.
                    952:  *
                    953:  * Side Effects:
                    954:  *     Currently running jobs finish.
                    955:  *
                    956:  *-----------------------------------------------------------------------
                    957:  */
                    958: void
1.56      espie     959: Job_Wait(void)
1.1       deraadt   960: {
1.66      espie     961:        aborting = ABORT_WAIT;
1.111     espie     962:        loop_handle_running_jobs();
1.66      espie     963:        aborting = 0;
1.1       deraadt   964: }
                    965:
                    966: /*-
                    967:  *-----------------------------------------------------------------------
                    968:  * Job_AbortAll --
                    969:  *     Abort all currently running jobs without handling output or anything.
                    970:  *     This function is to be called only in the event of a major
1.124     espie     971:  *     error.
1.1       deraadt   972:  *
                    973:  * Side Effects:
1.124     espie     974:  *     All children are killed
1.1       deraadt   975:  *-----------------------------------------------------------------------
                    976:  */
                    977: void
1.56      espie     978: Job_AbortAll(void)
1.1       deraadt   979: {
1.66      espie     980:        Job *job;       /* the job descriptor in that element */
                    981:        int foo;
1.6       millert   982:
1.66      espie     983:        aborting = ABORT_ERROR;
1.6       millert   984:
1.124     espie     985:        for (job = runningJobs; job != NULL; job = job->next) {
1.126     espie     986:                killpg(job->pid, SIGINT);
                    987:                killpg(job->pid, SIGKILL);
1.1       deraadt   988:        }
1.6       millert   989:
1.66      espie     990:        /*
                    991:         * Catch as many children as want to report in at first, then give up
                    992:         */
1.117     espie     993:        while (waitpid(WAIT_ANY, &foo, WNOHANG) > 0)
1.66      espie     994:                continue;
1.2       deraadt   995: }