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

1.40      espie       1: /*     $OpenPackages$ */
1.93      espie       2: /*     $OpenBSD$       */
1.6       millert     3: /*     $NetBSD: job.c,v 1.16 1996/11/06 17:59:08 christos Exp $        */
1.1       deraadt     4:
                      5: /*
                      6:  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
                      7:  * Copyright (c) 1988, 1989 by Adam de Boor
                      8:  * Copyright (c) 1989 by Berkeley Softworks
                      9:  * All rights reserved.
                     10:  *
                     11:  * This code is derived from software contributed to Berkeley by
                     12:  * Adam de Boor.
                     13:  *
                     14:  * Redistribution and use in source and binary forms, with or without
                     15:  * modification, are permitted provided that the following conditions
                     16:  * are met:
                     17:  * 1. Redistributions of source code must retain the above copyright
                     18:  *    notice, this list of conditions and the following disclaimer.
                     19:  * 2. Redistributions in binary form must reproduce the above copyright
                     20:  *    notice, this list of conditions and the following disclaimer in the
                     21:  *    documentation and/or other materials provided with the distribution.
1.55      millert    22:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    23:  *    may be used to endorse or promote products derived from this software
                     24:  *    without specific prior written permission.
                     25:  *
                     26:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     27:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     28:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     29:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     30:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     31:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     32:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     33:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     34:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     35:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     36:  * SUCH DAMAGE.
                     37:  */
                     38:
                     39: /*-
                     40:  * job.c --
                     41:  *     handle the creation etc. of our child processes.
                     42:  *
                     43:  * Interface:
1.40      espie      44:  *     Job_Make                Start the creation of the given target.
1.1       deraadt    45:  *
1.40      espie      46:  *     Job_CatchChildren       Check for and handle the termination of any
                     47:  *                             children. This must be called reasonably
                     48:  *                             frequently to keep the whole make going at
                     49:  *                             a decent clip, since job table entries aren't
                     50:  *                             removed until their process is caught this way.
                     51:  *
                     52:  *     Job_CatchOutput         Print any output our children have produced.
                     53:  *                             Should also be called fairly frequently to
                     54:  *                             keep the user informed of what's going on.
                     55:  *                             If no output is waiting, it will block for
                     56:  *                             a time given by the SEL_* constants, below,
                     57:  *                             or until output is ready.
                     58:  *
1.53      jmc        59:  *     Job_Init                Called to initialize this module. in addition,
1.40      espie      60:  *                             any commands attached to the .BEGIN target
                     61:  *                             are executed before this function returns.
                     62:  *                             Hence, the makefile must have been parsed
                     63:  *                             before this function is called.
                     64:  *
                     65:  *     Job_End                 Cleanup any memory used.
                     66:  *
1.41      espie      67:  *     Job_Full                Return true if the job table is filled.
1.40      espie      68:  *
1.41      espie      69:  *     Job_Empty               Return true if the job table is completely
1.40      espie      70:  *                             empty.
                     71:  *
                     72:  *     Job_Finish              Perform any final processing which needs doing.
                     73:  *                             This includes the execution of any commands
                     74:  *                             which have been/were attached to the .END
                     75:  *                             target. It should only be called when the
                     76:  *                             job table is empty.
                     77:  *
                     78:  *     Job_AbortAll            Abort all currently running jobs. It doesn't
                     79:  *                             handle output or do anything for the jobs,
                     80:  *                             just kills them. It should only be called in
                     81:  *                             an emergency, as it were.
1.1       deraadt    82:  *
1.40      espie      83:  *     Job_Wait                Wait for all currently-running jobs to finish.
1.1       deraadt    84:  */
                     85:
                     86: #include <sys/types.h>
                     87: #include <sys/wait.h>
1.41      espie      88: #include <ctype.h>
                     89: #include <errno.h>
1.1       deraadt    90: #include <fcntl.h>
1.41      espie      91: #include <signal.h>
1.69      espie      92: #include <stdarg.h>
1.1       deraadt    93: #include <stdio.h>
1.42      espie      94: #include <stdlib.h>
1.1       deraadt    95: #include <string.h>
1.41      espie      96: #include <unistd.h>
                     97: #include "config.h"
                     98: #include "defines.h"
1.1       deraadt    99: #include "job.h"
1.63      espie     100: #include "engine.h"
1.1       deraadt   101: #include "pathnames.h"
1.41      espie     102: #include "var.h"
                    103: #include "targ.h"
                    104: #include "error.h"
                    105: #include "lst.h"
                    106: #include "extern.h"
                    107: #include "gnode.h"
                    108: #include "memory.h"
                    109: #include "make.h"
                    110:
1.50      espie     111: /*
                    112:  * The SEL_ constants determine the maximum amount of time spent in select
                    113:  * before coming out to see if a child has finished. SEL_SEC is the number of
                    114:  * seconds and SEL_USEC is the number of micro-seconds
                    115:  */
                    116: #define SEL_SEC        0
                    117: #define SEL_USEC       500000
                    118:
                    119:
                    120: /*-
                    121:  * Job Table definitions.
                    122:  *
                    123:  * Each job has several things associated with it:
                    124:  *     1) The process id of the child shell
                    125:  *     2) The graph node describing the target being made by this job
1.93      espie     126:  *     3) An FILE* for writing out the commands. This is only
1.50      espie     127:  *        used before the job is actually started.
1.93      espie     128:  *     4) Things used for handling the shell's output.
1.76      espie     129:  *        the output is being caught via a pipe and
1.50      espie     130:  *        the descriptors of our pipe, an array in which output is line
                    131:  *        buffered and the current position in that buffer are all
1.78      espie     132:  *        maintained for each job.
1.93      espie     133:  *     5) A word of flags which determine how the module handles errors,
1.50      espie     134:  *        echoing, etc. for the job
                    135:  *
                    136:  * The job "table" is kept as a linked Lst in 'jobs', with the number of
                    137:  * active jobs maintained in the 'nJobs' variable. At no time will this
                    138:  * exceed the value of 'maxJobs', initialized by the Job_Init function.
                    139:  *
                    140:  * When a job is finished, the Make_Update function is called on each of the
                    141:  * parents of the node which was just remade. This takes care of the upward
                    142:  * traversal of the dependency graph.
                    143:  */
                    144: #define JOB_BUFSIZE    1024
1.101     espie     145: struct job_pipe {
                    146:        int fd;
                    147:        char buffer[JOB_BUFSIZE];
                    148:        size_t pos;
                    149: };
                    150:
1.50      espie     151: typedef struct Job_ {
1.51      mpech     152:     pid_t      pid;        /* The child's process ID */
1.50      espie     153:     GNode      *node;      /* The target the child is making */
                    154:     short      flags;      /* Flags to control treatment of job */
1.83      espie     155: #define JOB_SPECIAL    0x004   /* Target is a special one. */
1.50      espie     156: #define JOB_RESTART    0x080   /* Job needs to be completely restarted */
1.79      espie     157: #define JOB_RESUME     0x100   /* Job needs to be resumed b/c it stopped,
                    158:                                 * for some reason */
                    159: #define JOB_CONTINUING 0x200   /* We are in the process of resuming this job.
                    160:                                 * Used to avoid infinite recursion between
                    161:                                 * JobFinish and JobRestart */
1.101     espie     162:     struct job_pipe in[2];
1.50      espie     163: } Job;
                    164:
1.78      espie     165:
1.40      espie     166: static int     aborting = 0;       /* why is the make aborting? */
                    167: #define ABORT_ERROR    1           /* Because of an error */
                    168: #define ABORT_INTERRUPT 2          /* Because it was interrupted */
                    169: #define ABORT_WAIT     3           /* Waiting for jobs to finish */
1.1       deraadt   170:
1.40      espie     171: static int     maxJobs;        /* The most children we can run at once */
1.83      espie     172: static int     nJobs;          /* The number of children currently running */
1.95      espie     173: static LIST    runningJobs;    /* The structures that describe them */
1.79      espie     174: static bool    jobFull;        /* Flag to tell when the job table is full. It
1.83      espie     175:                                 * is set true when nJobs equals maxJobs */
1.40      espie     176: static fd_set  *outputsp;      /* Set of descriptors of pipes connected to
1.1       deraadt   177:                                 * the output channels of children */
1.8       deraadt   178: static int     outputsn;
1.48      espie     179: static GNode   *lastNode;      /* The node for which output was most recently
1.1       deraadt   180:                                 * produced. */
                    181: /*
1.50      espie     182:  * When JobStart attempts to run a job but isn't allowed to,
1.96      espie     183:  * the job is placed on the queuedJobs queue to be run
1.6       millert   184:  * when the next job finishes.
1.1       deraadt   185:  */
1.96      espie     186: static LIST    stoppedJobs;
                    187: static LIST    queuedJobs;
1.101     espie     188: static LIST    errorsList;
                    189: static int     errors;
                    190: struct error_info {
                    191:        int status;
                    192:        char *name;
                    193: };
                    194:
1.1       deraadt   195:
                    196:
                    197: #if defined(USE_PGRP) && defined(SYSV)
1.40      espie     198: # define KILL(pid, sig)        killpg(-(pid), (sig))
1.1       deraadt   199: #else
                    200: # if defined(USE_PGRP)
1.2       deraadt   201: #  define KILL(pid, sig)       killpg((pid), (sig))
1.1       deraadt   202: # else
1.2       deraadt   203: #  define KILL(pid, sig)       kill((pid), (sig))
1.1       deraadt   204: # endif
                    205: #endif
                    206:
1.6       millert   207: /*
1.2       deraadt   208:  * Grmpf... There is no way to set bits of the wait structure
                    209:  * anymore with the stupid W*() macros. I liked the union wait
                    210:  * stuff much more. So, we devise our own macros... This is
                    211:  * really ugly, use dramamine sparingly. You have been warned.
                    212:  */
                    213: #define W_SETMASKED(st, val, fun)                              \
                    214:        {                                                       \
                    215:                int sh = (int) ~0;                              \
                    216:                int mask = fun(sh);                             \
                    217:                                                                \
                    218:                for (sh = 0; ((mask >> sh) & 1) == 0; sh++)     \
                    219:                        continue;                               \
                    220:                *(st) = (*(st) & ~mask) | ((val) << sh);        \
                    221:        }
                    222:
                    223: #define W_SETTERMSIG(st, val) W_SETMASKED(st, val, WTERMSIG)
                    224: #define W_SETEXITSTATUS(st, val) W_SETMASKED(st, val, WEXITSTATUS)
                    225:
                    226:
1.93      espie     227: static void pass_signal_to_job(void *, void *);
                    228: static void handle_all_signals(void);
                    229: static void handle_signal(int);
1.40      espie     230: static int JobCmpPid(void *, void *);
                    231: static void JobClose(Job *);
1.101     espie     232: static void JobFinish(Job *, int);
                    233: static void JobExec(Job *);
1.40      espie     234: static void JobRestart(Job *);
1.97      espie     235: static void JobStart(GNode *, int);
1.40      espie     236: static void JobInterrupt(int, int);
                    237: static void JobRestartJobs(void);
1.84      espie     238: static void debug_printf(const char *, ...);
1.98      espie     239: static Job *prepare_job(GNode *, int);
1.96      espie     240: static void start_queued_job(Job *);
1.105     espie     241: static void banner(Job *, FILE *);
1.101     espie     242: static void print_partial_buffer(struct job_pipe *, Job *, FILE *, size_t);
                    243: static void print_partial_buffer_and_shift(struct job_pipe *, Job *, FILE *,
                    244:     size_t);
                    245: static bool print_complete_lines(struct job_pipe *, Job *, FILE *, size_t);
                    246: static void prepare_pipe(struct job_pipe *, int *);
                    247: static void handle_job_output(Job *, int, bool);
                    248: static void register_error(int, Job *);
1.93      espie     249:
1.101     espie     250: static void
                    251: register_error(int status, Job *job)
                    252: {
                    253:        struct error_info *p;
1.1       deraadt   254:
1.101     espie     255:        errors++;
                    256:        p = emalloc(sizeof(struct error_info));
                    257:        p->status = status;
                    258:        p->name = job->node->name;
                    259:        if (p)
                    260:                Lst_AtEnd(&errorsList, p);
                    261: }
1.93      espie     262:
1.101     espie     263: void
                    264: print_errors()
1.88      espie     265: {
1.101     espie     266:        LstNode ln;
                    267:        struct error_info *p;
1.88      espie     268:
1.101     espie     269:        for (ln = Lst_First(&errorsList); ln != NULL; ln = Lst_Adv(ln)) {
                    270:                p = (struct error_info *)Lst_Datum(ln);
                    271:                if (WIFEXITED(p->status)) {
                    272:                        Error("\tExit status %d in target %s",
                    273:                            WEXITSTATUS(p->status), p->name);
                    274:                } else if (WIFSIGNALED(p->status)) {
                    275:                        Error("\tReceived signal %d in target s",
                    276:                            WTERMSIG(p->status), p->name);
                    277:                } else {
                    278:                        Error("\tStatus %d in target %s", p->status, p->name);
                    279:                }
                    280:        }
1.88      espie     281: }
                    282:
1.57      espie     283: static void
1.105     espie     284: banner(Job *job, FILE *out)
1.57      espie     285: {
1.101     espie     286:        if (job->node != lastNode) {
1.105     espie     287:                if (DEBUG(JOBBANNER))
1.101     espie     288:                        (void)fprintf(out, "--- %s ---\n", job->node->name);
                    289:                lastNode = job->node;
1.57      espie     290:        }
                    291: }
                    292:
                    293: static void
1.93      espie     294: handle_all_signals()
1.57      espie     295: {
1.93      espie     296:        if (got_signal)
                    297:                got_signal = 0;
                    298:        else
                    299:                return;
                    300:
1.57      espie     301:        if (got_SIGINT) {
                    302:                got_SIGINT=0;
1.93      espie     303:                handle_signal(SIGINT);
1.57      espie     304:        }
                    305:        if (got_SIGHUP) {
                    306:                got_SIGHUP=0;
1.93      espie     307:                handle_signal(SIGHUP);
1.57      espie     308:        }
                    309:        if (got_SIGQUIT) {
                    310:                got_SIGQUIT=0;
1.93      espie     311:                handle_signal(SIGQUIT);
1.57      espie     312:        }
                    313:        if (got_SIGTERM) {
                    314:                got_SIGTERM=0;
1.93      espie     315:                handle_signal(SIGTERM);
1.57      espie     316:        }
                    317:        if (got_SIGTSTP) {
                    318:                got_SIGTSTP=0;
1.93      espie     319:                handle_signal(SIGTSTP);
1.57      espie     320:        }
                    321:        if (got_SIGTTOU) {
                    322:                got_SIGTTOU=0;
1.93      espie     323:                handle_signal(SIGTTOU);
1.57      espie     324:        }
                    325:        if (got_SIGTTIN) {
                    326:                got_SIGTTIN=0;
1.93      espie     327:                handle_signal(SIGTTIN);
1.57      espie     328:        }
                    329:        if (got_SIGWINCH) {
                    330:                got_SIGWINCH=0;
1.93      espie     331:                handle_signal(SIGWINCH);
1.57      espie     332:        }
                    333: }
                    334:
1.1       deraadt   335: /*-
                    336:  *-----------------------------------------------------------------------
                    337:  * JobCondPassSig --
1.50      espie     338:  *     Pass a signal to a job if USE_PGRP
1.1       deraadt   339:  *     is defined.
                    340:  *
                    341:  * Side Effects:
                    342:  *     None, except the job may bite it.
                    343:  *-----------------------------------------------------------------------
                    344:  */
1.27      espie     345: static void
1.93      espie     346: pass_signal_to_job(void *jobp, /* Job to biff */
1.56      espie     347:     void *signop)              /* Signal to send it */
1.1       deraadt   348: {
1.66      espie     349:        Job *job = (Job *)jobp;
                    350:        int signo = *(int *)signop;
                    351:        if (DEBUG(JOB)) {
                    352:                (void)fprintf(stdout,
1.93      espie     353:                    "pass_signal_to_job passing signal %d to child %ld.\n",
1.66      espie     354:                    signo, (long)job->pid);
                    355:                (void)fflush(stdout);
                    356:        }
                    357:        KILL(job->pid, signo);
1.1       deraadt   358: }
                    359:
                    360: /*-
                    361:  *-----------------------------------------------------------------------
1.94      espie     362:  * handle_signal --
1.68      espie     363:  *     Pass a signal to all local jobs if USE_PGRP is defined,
1.50      espie     364:  *     then die ourselves.
1.1       deraadt   365:  *
                    366:  * Side Effects:
                    367:  *     We die by the same signal.
                    368:  *-----------------------------------------------------------------------
                    369:  */
                    370: static void
1.93      espie     371: handle_signal(int signo) /* The signal number we've received */
1.1       deraadt   372: {
1.66      espie     373:        sigset_t nmask, omask;
                    374:        struct sigaction act;
1.6       millert   375:
1.66      espie     376:        if (DEBUG(JOB)) {
1.93      espie     377:                (void)fprintf(stdout, "handle_signal(%d) called.\n", signo);
1.66      espie     378:                (void)fflush(stdout);
                    379:        }
1.95      espie     380:        Lst_ForEach(&runningJobs, pass_signal_to_job, &signo);
1.66      espie     381:
                    382:        /*
                    383:         * Deal with proper cleanup based on the signal received. We only run
                    384:         * the .INTERRUPT target if the signal was in fact an interrupt. The
                    385:         * other three termination signals are more of a "get out *now*"
                    386:         * command.
                    387:         */
                    388:        if (signo == SIGINT) {
                    389:                JobInterrupt(true, signo);
                    390:        } else if (signo == SIGHUP || signo == SIGTERM || signo == SIGQUIT) {
                    391:                JobInterrupt(false, signo);
                    392:        }
1.1       deraadt   393:
1.66      espie     394:        /*
                    395:         * Leave gracefully if SIGQUIT, rather than core dumping.
                    396:         */
                    397:        if (signo == SIGQUIT) {
                    398:                Finish(0);
                    399:        }
1.6       millert   400:
1.66      espie     401:        /*
                    402:         * Send ourselves the signal now we've given the message to everyone
                    403:         * else.  Note we block everything else possible while we're getting
                    404:         * the signal.  This ensures that all our jobs get continued when we
                    405:         * wake up before we take any other signal.
                    406:         */
                    407:        sigemptyset(&nmask);
                    408:        sigaddset(&nmask, signo);
                    409:        sigprocmask(SIG_SETMASK, &nmask, &omask);
                    410:        memset(&act, 0, sizeof act);
                    411:        act.sa_handler = SIG_DFL;
                    412:        sigemptyset(&act.sa_mask);
                    413:        act.sa_flags = 0;
                    414:        sigaction(signo, &act, NULL);
1.6       millert   415:
1.66      espie     416:        if (DEBUG(JOB)) {
                    417:                (void)fprintf(stdout,
1.93      espie     418:                    "handle_signal passing signal to self, mask = %x.\n",
1.66      espie     419:                    ~0 & ~(1 << (signo-1)));
                    420:                (void)fflush(stdout);
                    421:        }
                    422:        (void)signal(signo, SIG_DFL);
1.1       deraadt   423:
1.66      espie     424:        (void)KILL(getpid(), signo);
1.1       deraadt   425:
1.66      espie     426:        signo = SIGCONT;
1.95      espie     427:        Lst_ForEach(&runningJobs, pass_signal_to_job, &signo);
1.1       deraadt   428:
1.66      espie     429:        (void)sigprocmask(SIG_SETMASK, &omask, NULL);
                    430:        sigprocmask(SIG_SETMASK, &omask, NULL);
                    431:        act.sa_handler = SigHandler;
                    432:        sigaction(signo, &act, NULL);
1.1       deraadt   433: }
                    434:
                    435: /*-
                    436:  *-----------------------------------------------------------------------
                    437:  * JobCmpPid  --
                    438:  *     Compare the pid of the job with the given pid and return 0 if they
                    439:  *     are equal. This function is called from Job_CatchChildren via
                    440:  *     Lst_Find to find the job descriptor of the finished job.
                    441:  *
                    442:  * Results:
                    443:  *     0 if the pid's match
                    444:  *-----------------------------------------------------------------------
                    445:  */
                    446: static int
1.56      espie     447: JobCmpPid(void *job,   /* job to examine */
                    448:     void *pid)         /* process id desired */
1.1       deraadt   449: {
1.66      espie     450:        return *(pid_t *)pid - ((Job *)job)->pid;
1.1       deraadt   451: }
                    452:
1.69      espie     453: static void
1.84      espie     454: debug_printf(const char *fmt, ...)
                    455: {
                    456:        if (DEBUG(JOB)) {
                    457:                va_list va;
                    458:
                    459:                va_start(va, fmt);
                    460:                (void)vfprintf(stdout, fmt, va);
                    461:                fflush(stdout);
                    462:                va_end(va);
                    463:        }
                    464: }
                    465:
1.1       deraadt   466: /*-
                    467:  *-----------------------------------------------------------------------
1.2       deraadt   468:  * JobClose --
                    469:  *     Called to close both input and output pipes when a job is finished.
                    470:  *
                    471:  * Side Effects:
                    472:  *     The file descriptors associated with the job are closed.
                    473:  *-----------------------------------------------------------------------
                    474:  */
                    475: static void
1.56      espie     476: JobClose(Job *job)
1.2       deraadt   477: {
1.101     espie     478:        int i;
                    479:
                    480:        for (i = 0; i < 2; i++) {
                    481:                FD_CLR(job->in[i].fd, outputsp);
                    482:                handle_job_output(job, i, true);
                    483:                (void)close(job->in[i].fd);
1.66      espie     484:        }
1.1       deraadt   485: }
                    486:
                    487: /*-
                    488:  *-----------------------------------------------------------------------
                    489:  * JobFinish  --
                    490:  *     Do final processing for the given job including updating
1.106     espie     491:  *     parents and starting new jobs as available/necessary.
1.1       deraadt   492:  *
                    493:  * Side Effects:
                    494:  *     Some nodes may be put on the toBeMade queue.
1.78      espie     495:  *     Final commands for the job are placed on end_node.
1.1       deraadt   496:  *
1.6       millert   497:  *     If we got an error and are aborting (aborting == ABORT_ERROR) and
1.1       deraadt   498:  *     the job list is now empty, we are done for the day.
1.101     espie     499:  *     If we recognized an error we set the aborting flag
1.1       deraadt   500:  *     to ABORT_ERROR so no more jobs will be started.
                    501:  *-----------------------------------------------------------------------
                    502:  */
                    503: /*ARGSUSED*/
                    504: static void
1.101     espie     505: JobFinish(Job *job, int status)
1.2       deraadt   506: {
1.78      espie     507:        bool     done;
1.2       deraadt   508:
1.101     espie     509:        if ((WIFEXITED(status) &&
1.106     espie     510:             WEXITSTATUS(status) != 0 && !(job->node->type & OP_IGNORE)) ||
1.101     espie     511:            (WIFSIGNALED(status) && WTERMSIG(status) != SIGCONT)) {
1.66      espie     512:                /*
                    513:                 * If it exited non-zero and either we're doing things our
                    514:                 * way or we're not ignoring errors, the job is finished.
                    515:                 * Similarly, if the shell died because of a signal
                    516:                 * the job is also finished. In these
                    517:                 * cases, finish out the job's output before printing the exit
                    518:                 * status...
                    519:                 */
                    520:                JobClose(job);
                    521:                done = true;
1.101     espie     522:        } else if (WIFEXITED(status)) {
1.66      espie     523:                /*
                    524:                 * Deal with ignored errors in -B mode. We need to print a
                    525:                 * message telling of the ignored error as well as setting
                    526:                 * status.w_status to 0 so the next command gets run. To do
                    527:                 * this, we set done to be true if in -B mode and the job
                    528:                 * exited non-zero.
                    529:                 */
1.101     espie     530:                done = WEXITSTATUS(status) != 0;
1.66      espie     531:                /*
                    532:                 * Old comment said: "Note we don't want to close down any of
                    533:                 * the streams until we know we're at the end." But we do.
                    534:                 * Otherwise when are we going to print the rest of the stuff?
                    535:                 */
                    536:                JobClose(job);
                    537:        } else {
                    538:                /*
                    539:                 * No need to close things down or anything.
                    540:                 */
                    541:                done = false;
1.1       deraadt   542:        }
1.6       millert   543:
1.66      espie     544:        if (done ||
1.101     espie     545:            WIFSTOPPED(status) ||
                    546:            (WIFSIGNALED(status) && WTERMSIG(status) == SIGCONT) ||
1.66      espie     547:            DEBUG(JOB)) {
1.101     espie     548:                if (WIFEXITED(status)) {
1.84      espie     549:                        debug_printf("Process %ld exited.\n", (long)job->pid);
1.101     espie     550:                        if (WEXITSTATUS(status) != 0) {
1.105     espie     551:                                banner(job, stdout);
1.101     espie     552:                                (void)fprintf(stdout, "*** Error code %d%s\n",
                    553:                                    WEXITSTATUS(status),
1.106     espie     554:                                    (job->node->type & OP_IGNORE) ?
                    555:                                    "(ignored)" : "");
1.66      espie     556:
1.106     espie     557:                                if (job->node->type & OP_IGNORE) {
1.101     espie     558:                                        status = 0;
1.66      espie     559:                                }
                    560:                        } else if (DEBUG(JOB)) {
1.105     espie     561:                                banner(job, stdout);
1.101     espie     562:                                (void)fprintf(stdout,
1.66      espie     563:                                    "*** Completed successfully\n");
                    564:                        }
1.101     espie     565:                } else if (WIFSTOPPED(status)) {
1.84      espie     566:                        debug_printf("Process %ld stopped.\n", (long)job->pid);
1.105     espie     567:                        banner(job, stdout);
1.101     espie     568:                        (void)fprintf(stdout, "*** Stopped -- signal %d\n",
                    569:                            WSTOPSIG(status));
1.66      espie     570:                        job->flags |= JOB_RESUME;
                    571:                        Lst_AtEnd(&stoppedJobs, job);
1.101     espie     572:                        (void)fflush(stdout);
1.66      espie     573:                        return;
1.101     espie     574:                } else if (WTERMSIG(status) == SIGCONT) {
1.66      espie     575:                        /*
                    576:                         * If the beastie has continued, shift the Job from the
                    577:                         * stopped list to the running one (or re-stop it if
                    578:                         * concurrency is exceeded) and go and get another
                    579:                         * child.
                    580:                         */
                    581:                        if (job->flags & (JOB_RESUME|JOB_RESTART)) {
1.105     espie     582:                                banner(job, stdout);
1.101     espie     583:                                (void)fprintf(stdout, "*** Continued\n");
1.66      espie     584:                        }
                    585:                        if (!(job->flags & JOB_CONTINUING)) {
1.84      espie     586:                                debug_printf(
                    587:                                    "Warning: "
                    588:                                    "process %ld was not continuing.\n",
                    589:                                    (long)job->pid);
1.66      espie     590: #if 0
                    591:                                /*
                    592:                                 * We don't really want to restart a job from
                    593:                                 * scratch just because it continued,
                    594:                                 * especially not without killing the
                    595:                                 * continuing process!  That's why this is
                    596:                                 * ifdef'ed out.  FD - 9/17/90
                    597:                                 */
                    598:                                JobRestart(job);
1.2       deraadt   599: #endif
1.66      espie     600:                        }
                    601:                        job->flags &= ~JOB_CONTINUING;
1.95      espie     602:                        Lst_AtEnd(&runningJobs, job);
1.67      espie     603:                        nJobs++;
1.84      espie     604:                        debug_printf("Process %ld is continuing locally.\n",
                    605:                            (long)job->pid);
1.66      espie     606:                        if (nJobs == maxJobs) {
                    607:                                jobFull = true;
1.84      espie     608:                                debug_printf("Job queue is full.\n");
1.66      espie     609:                        }
1.101     espie     610:                        (void)fflush(stdout);
1.66      espie     611:                        return;
                    612:                } else {
1.105     espie     613:                        banner(job, stdout);
1.101     espie     614:                        (void)fprintf(stdout, "*** Signal %d\n",
                    615:                            WTERMSIG(status));
1.40      espie     616:                }
1.66      espie     617:
1.101     espie     618:                (void)fflush(stdout);
1.1       deraadt   619:        }
                    620:
1.85      espie     621:        done = true;
1.1       deraadt   622:
1.66      espie     623:        if (done &&
                    624:            aborting != ABORT_ERROR &&
                    625:            aborting != ABORT_INTERRUPT &&
1.101     espie     626:            status == 0) {
1.66      espie     627:                /* As long as we aren't aborting and the job didn't return a
                    628:                 * non-zero status that we shouldn't ignore, we call
1.101     espie     629:                 * Make_Update to update the parents. */
1.66      espie     630:                job->node->made = MADE;
                    631:                Make_Update(job->node);
                    632:                free(job);
1.101     espie     633:        } else if (status != 0) {
                    634:                register_error(status, job);
1.66      espie     635:                free(job);
                    636:        }
1.1       deraadt   637:
1.66      espie     638:        JobRestartJobs();
1.1       deraadt   639:
                    640:        /*
1.66      espie     641:         * Set aborting if any error.
1.1       deraadt   642:         */
1.101     espie     643:        if (errors && !keepgoing &&
                    644:            aborting != ABORT_INTERRUPT) {
1.66      espie     645:                /*
                    646:                 * If we found any errors in this batch of children and the -k
                    647:                 * flag wasn't given, we set the aborting flag so no more jobs
                    648:                 * get started.
                    649:                 */
                    650:                aborting = ABORT_ERROR;
                    651:        }
1.6       millert   652:
1.66      espie     653:        if (aborting == ABORT_ERROR && Job_Empty()) {
                    654:                /*
                    655:                 * If we are aborting and the job table is now empty, we finish.
                    656:                 */
                    657:                Finish(errors);
                    658:        }
1.1       deraadt   659: }
                    660:
1.101     espie     661: static void
                    662: prepare_pipe(struct job_pipe *p, int *fd)
                    663: {
                    664:        p->pos = 0;
                    665:        (void)fcntl(fd[0], F_SETFD, FD_CLOEXEC);
                    666:        p->fd = fd[0];
                    667:        close(fd[1]);
                    668:
                    669:        if (outputsp == NULL || p->fd > outputsn) {
                    670:                int fdn, ofdn;
                    671:                fd_set *tmp;
                    672:
                    673:                fdn = howmany(p->fd+1, NFDBITS);
                    674:                ofdn = outputsn ? howmany(outputsn+1, NFDBITS) : 0;
                    675:
                    676:                if (fdn != ofdn) {
                    677:                        tmp = recalloc(outputsp, fdn, sizeof(fd_mask));
                    678:                        if (tmp == NULL)
                    679:                                return;
                    680:                        outputsp = tmp;
                    681:                }
                    682:                outputsn = p->fd;
                    683:        }
                    684:        fcntl(p->fd, F_SETFL, O_NONBLOCK);
                    685:        FD_SET(p->fd, outputsp);
                    686: }
                    687:
1.1       deraadt   688: /*-
                    689:  *-----------------------------------------------------------------------
                    690:  * JobExec --
                    691:  *     Execute the shell for the given job. Called from JobStart and
                    692:  *     JobRestart.
                    693:  *
                    694:  * Side Effects:
                    695:  *     A shell is executed, outputs is altered and the Job structure added
                    696:  *     to the job table.
                    697:  *-----------------------------------------------------------------------
                    698:  */
                    699: static void
1.101     espie     700: JobExec(Job *job)
1.1       deraadt   701: {
1.66      espie     702:        pid_t cpid;     /* ID of new child */
1.101     espie     703:        int fds[4];
                    704:        int *fdout = fds;
                    705:        int *fderr = fds+2;
                    706:        int result;
                    707:        int i;
1.6       millert   708:
1.66      espie     709:        if (DEBUG(JOB)) {
1.107   ! espie     710:                LstNode ln;
        !           711:
1.66      espie     712:                (void)fprintf(stdout, "Running %s\n", job->node->name);
1.107   ! espie     713:                for (ln = Lst_First(&job->node->commands); ln != NULL ;
        !           714:                    ln = Lst_Adv(ln))
        !           715:                        fprintf(stdout, "\t%s\n", (char *)Lst_Datum(ln));
1.66      espie     716:                (void)fflush(stdout);
1.1       deraadt   717:        }
1.6       millert   718:
1.66      espie     719:        /*
                    720:         * Some jobs produce no output and it's disconcerting to have
                    721:         * no feedback of their running (since they produce no output, the
                    722:         * banner with their name in it never appears). This is an attempt to
                    723:         * provide that feedback, even if nothing follows it.
                    724:         */
1.105     espie     725:        banner(job, stdout);
1.101     espie     726:
                    727:        setup_engine();
                    728:
                    729:        /* Create the pipe by which we'll get the shell's output.
                    730:         */
                    731:        if (pipe(fdout) == -1)
                    732:                Punt("Cannot create pipe: %s", strerror(errno));
1.1       deraadt   733:
1.101     espie     734:        if (pipe(fderr) == -1)
                    735:                Punt("Cannot create pipe: %s", strerror(errno));
1.94      espie     736:
1.66      espie     737:        if ((cpid = fork()) == -1) {
                    738:                Punt("Cannot fork");
                    739:        } else if (cpid == 0) {
1.104     espie     740:                supervise_jobs = false;
1.101     espie     741:                /* standard pipe code to route stdout and stderr */
                    742:                close(fdout[0]);
                    743:                if (dup2(fdout[1], 1) == -1)
                    744:                        Punt("Cannot dup2(outPipe): %s", strerror(errno));
                    745:                if (fdout[1] != 1)
                    746:                        close(fdout[1]);
                    747:                close(fderr[0]);
                    748:                if (dup2(fderr[1], 2) == -1)
                    749:                        Punt("Cannot dup2(errPipe): %s", strerror(errno));
                    750:                if (fderr[1] != 2)
                    751:                        close(fderr[1]);
1.1       deraadt   752:
                    753: #ifdef USE_PGRP
1.66      espie     754:                /*
                    755:                 * We want to switch the child into a different process family
                    756:                 * so we can kill it and all its descendants in one fell swoop,
                    757:                 * by killing its process family, but not commit suicide.
                    758:                 */
1.2       deraadt   759: # if defined(SYSV)
1.66      espie     760:                (void)setsid();
1.2       deraadt   761: # else
1.66      espie     762:                (void)setpgid(0, getpid());
1.2       deraadt   763: # endif
                    764: #endif /* USE_PGRP */
1.1       deraadt   765:
1.101     espie     766:                /* most cases won't return, but will exit directly */
                    767:                result = run_gnode(job->node, 1);
                    768:                switch(result) {
                    769:                case MADE:
                    770:                        exit(0);
                    771:                case ERROR:
                    772:                        exit(1);
                    773:                default:
                    774:                        fprintf(stderr,
                    775:                            "Could not run gnode, returned %d\n", result);
                    776:                        exit(1);
                    777:                }
1.66      espie     778:        } else {
1.104     espie     779:                supervise_jobs = true;
1.66      espie     780:                job->pid = cpid;
                    781:
1.101     espie     782:                /* we set the current position in the buffers to the beginning
1.87      espie     783:                 * and mark another stream to watch in the outputs mask
                    784:                 */
1.101     espie     785:                for (i = 0; i < 2; i++)
                    786:                        prepare_pipe(&job->in[i], fds+2*i);
1.1       deraadt   787:        }
                    788:
1.48      espie     789:        /*
1.66      espie     790:         * Now the job is actually running, add it to the table.
1.48      espie     791:         */
1.67      espie     792:        nJobs++;
1.95      espie     793:        Lst_AtEnd(&runningJobs, job);
1.66      espie     794:        if (nJobs == maxJobs) {
                    795:                jobFull = true;
1.1       deraadt   796:        }
                    797: }
                    798:
1.96      espie     799: static void
                    800: start_queued_job(Job *job)
                    801: {
                    802:        if (DEBUG(JOB)) {
                    803:                (void)fprintf(stdout, "Restarting %s...",
                    804:                    job->node->name);
                    805:                (void)fflush(stdout);
                    806:        }
                    807:        if (nJobs >= maxJobs && !(job->flags & JOB_SPECIAL)) {
                    808:                /*
                    809:                 * Can't be exported and not allowed to run locally --
                    810:                 * put it back on the hold queue and mark the table
                    811:                 * full
                    812:                 */
                    813:                debug_printf("holding\n");
                    814:                Lst_AtFront(&stoppedJobs, job);
                    815:                jobFull = true;
                    816:                debug_printf("Job queue is full.\n");
                    817:                return;
                    818:        } else {
                    819:                /*
                    820:                 * Job may be run locally.
                    821:                 */
                    822:                debug_printf("running locally\n");
                    823:        }
1.101     espie     824:        JobExec(job);
1.96      espie     825: }
                    826:
1.1       deraadt   827: /*-
                    828:  *-----------------------------------------------------------------------
                    829:  * JobRestart --
1.6       millert   830:  *     Restart a job that stopped for some reason.
1.1       deraadt   831:  *
                    832:  * Side Effects:
                    833:  *     jobFull will be set if the job couldn't be run.
                    834:  *-----------------------------------------------------------------------
                    835:  */
                    836: static void
1.56      espie     837: JobRestart(Job *job)
1.1       deraadt   838: {
1.66      espie     839:        if (job->flags & JOB_RESTART) {
1.96      espie     840:                start_queued_job(job);
1.66      espie     841:        } else {
1.1       deraadt   842:                /*
1.66      espie     843:                 * The job has stopped and needs to be restarted. Why it
                    844:                 * stopped, we don't know...
1.1       deraadt   845:                 */
1.84      espie     846:                debug_printf("Resuming %s...", job->node->name);
1.83      espie     847:                if ((nJobs < maxJobs || ((job->flags & JOB_SPECIAL) &&
                    848:                    maxJobs == 0)) && nJobs != maxJobs) {
1.66      espie     849:                        /*
1.79      espie     850:                         * If we haven't reached the concurrency limit already
1.83      espie     851:                         * (or maxJobs is 0), it's ok to resume the job.
1.66      espie     852:                         */
                    853:                        bool error;
1.101     espie     854:                        int status = 0;
1.66      espie     855:
                    856:                        error = KILL(job->pid, SIGCONT) != 0;
                    857:
                    858:                        if (!error) {
                    859:                                /*
                    860:                                 * Make sure the user knows we've continued the
                    861:                                 * beast and actually put the thing in the job
                    862:                                 * table.
                    863:                                 */
                    864:                                job->flags |= JOB_CONTINUING;
                    865:                                W_SETTERMSIG(&status, SIGCONT);
1.101     espie     866:                                JobFinish(job, status);
1.66      espie     867:
                    868:                                job->flags &= ~(JOB_RESUME|JOB_CONTINUING);
1.84      espie     869:                                debug_printf("done\n");
1.66      espie     870:                        } else {
                    871:                                Error("couldn't resume %s: %s",
                    872:                                    job->node->name, strerror(errno));
                    873:                                W_SETEXITSTATUS(&status, 1);
1.101     espie     874:                                JobFinish(job, status);
1.66      espie     875:                        }
                    876:                } else {
                    877:                        /*
                    878:                         * Job cannot be restarted. Mark the table as full and
                    879:                         * place the job back on the list of stopped jobs.
                    880:                         */
1.84      espie     881:                        debug_printf("table full\n");
1.66      espie     882:                        Lst_AtFront(&stoppedJobs, job);
                    883:                        jobFull = true;
1.84      espie     884:                        debug_printf("Job queue is full.\n");
1.1       deraadt   885:                }
                    886:        }
                    887: }
                    888:
1.98      espie     889: static Job *
                    890: prepare_job(GNode *gn, int flags)
1.78      espie     891: {
                    892:        Job *job;               /* new job descriptor */
                    893:        bool cmdsOK;            /* true if the nodes commands were all right */
                    894:        bool noExec;            /* Set true if we decide not to run the job */
1.66      espie     895:
1.86      espie     896:        job = emalloc(sizeof(Job));
                    897:        if (job == NULL) {
                    898:                Punt("JobStart out of memory");
1.1       deraadt   899:        }
                    900:
1.66      espie     901:        job->node = gn;
1.1       deraadt   902:
                    903:        /*
1.66      espie     904:         * Set the initial value of the flags for this job based on the global
                    905:         * ones and the node's attributes... Any flags supplied by the caller
                    906:         * are also added to the field.
1.1       deraadt   907:         */
1.87      espie     908:        job->flags = flags;
1.6       millert   909:
1.1       deraadt   910:        /*
1.66      espie     911:         * Check the commands now so any attributes from .DEFAULT have a chance
                    912:         * to migrate to the node
1.1       deraadt   913:         */
1.87      espie     914:        cmdsOK = Job_CheckCommands(gn, Error);
1.1       deraadt   915:
1.66      espie     916:        if ((gn->type & OP_MAKE) || (!noExecute && !touchFlag)) {
                    917:                /*
                    918:                 * We're serious here, but if the commands were bogus, we're
                    919:                 * also dead...
                    920:                 */
                    921:                if (!cmdsOK) {
                    922:                        DieHorribly();
                    923:                }
1.6       millert   924:
1.101     espie     925:                if (Lst_IsEmpty(&gn->commands))
                    926:                        noExec = true;
                    927:                else
                    928:                        noExec = false;
1.66      espie     929:
1.101     espie     930:        } else if (noExecute) {
                    931:                if (!cmdsOK || Lst_IsEmpty(&gn->commands))
1.85      espie     932:                        noExec = true;
1.101     espie     933:                else
                    934:                        noExec = false;
1.66      espie     935:        } else {
                    936:                /*
                    937:                 * Just touch the target and note that no shell should be
1.101     espie     938:                 * executed.  Check
1.66      espie     939:                 * the commands, too, but don't die if they're no good -- it
                    940:                 * does no harm to keep working up the graph.
1.30      espie     941:                 */
1.106     espie     942:                Job_Touch(gn);
1.41      espie     943:                noExec = true;
1.1       deraadt   944:        }
1.66      espie     945:
1.1       deraadt   946:        /*
1.66      espie     947:         * If we're not supposed to execute a shell, don't.
1.1       deraadt   948:         */
1.66      espie     949:        if (noExec) {
                    950:                /*
                    951:                 * We only want to work our way up the graph if we aren't here
                    952:                 * because the commands for the job were no good.
                    953:                 */
                    954:                if (cmdsOK) {
                    955:                        if (aborting == 0) {
1.102     espie     956:                                job->node->made = MADE;
1.66      espie     957:                                Make_Update(job->node);
                    958:                        }
                    959:                }
1.99      espie     960:                free(job);
                    961:                return NULL;
1.1       deraadt   962:        } else {
1.98      espie     963:                return job;
1.1       deraadt   964:        }
1.98      espie     965: }
1.1       deraadt   966:
1.98      espie     967: /*-
                    968:  *-----------------------------------------------------------------------
                    969:  * JobStart  --
                    970:  *     Start a target-creation process going for the target described
                    971:  *     by the graph node gn.
                    972:  *
                    973:  * Side Effects:
                    974:  *     A new Job node is created and added to the list of running
                    975:  *     jobs. PMake is forked and a child shell created.
                    976:  *-----------------------------------------------------------------------
                    977:  */
                    978: static void
                    979: JobStart(GNode *gn,            /* target to create */
                    980:     int flags)                 /* flags for the job to override normal ones.
                    981:                                 * e.g. JOB_SPECIAL */
                    982: {
                    983:        Job *job;
                    984:        job = prepare_job(gn, flags);
                    985:        if (!job)
                    986:                return;
1.83      espie     987:        if (nJobs >= maxJobs && !(job->flags & JOB_SPECIAL) &&
                    988:            maxJobs != 0) {
1.79      espie     989:                /*
                    990:                 * The job can only be run locally, but we've hit the limit of
                    991:                 * local concurrency, so put the job on hold until some other
                    992:                 * job finishes. Note that the special jobs (.BEGIN, .INTERRUPT
                    993:                 * and .END) may be run locally even when the local limit has
1.83      espie     994:                 * been reached (e.g. when maxJobs == 0), though they will be
1.79      espie     995:                 * exported if at all possible. In addition, any target marked
1.83      espie     996:                 * with .NOEXPORT will be run locally if maxJobs is 0.
1.66      espie     997:                 */
                    998:                jobFull = true;
1.6       millert   999:
1.84      espie    1000:                debug_printf("Can only run job locally.\n");
1.66      espie    1001:                job->flags |= JOB_RESTART;
                   1002:                Lst_AtEnd(&stoppedJobs, job);
1.79      espie    1003:        } else {
1.83      espie    1004:                if (nJobs >= maxJobs) {
1.79      espie    1005:                        /*
                   1006:                         * If we're running this job locally as a special case
                   1007:                         * (see above), at least say the table is full.
                   1008:                         */
                   1009:                        jobFull = true;
1.84      espie    1010:                        debug_printf("Local job queue is full.\n");
1.79      espie    1011:                }
1.101     espie    1012:                JobExec(job);
1.1       deraadt  1013:        }
                   1014: }
                   1015:
1.101     espie    1016: /* Helper functions for JobDoOutput */
                   1017:
                   1018:
1.105     espie    1019: /* output debugging banner and print characters from 0 to endpos */
1.101     espie    1020: static void
                   1021: print_partial_buffer(struct job_pipe *p, Job *job, FILE *out, size_t endPos)
1.2       deraadt  1022: {
1.101     espie    1023:        size_t i;
1.2       deraadt  1024:
1.105     espie    1025:        banner(job, out);
1.101     espie    1026:        for (i = 0; i < endPos; i++)
                   1027:                putc(p->buffer[i], out);
                   1028: }
                   1029:
                   1030: /* print partial buffer and shift remaining contents */
                   1031: static void
                   1032: print_partial_buffer_and_shift(struct job_pipe *p, Job *job, FILE *out,
                   1033:     size_t endPos)
                   1034: {
                   1035:        size_t i;
                   1036:
                   1037:        print_partial_buffer(p, job, out, endPos);
                   1038:
                   1039:        for (i = endPos; i < p->pos; i++)
                   1040:                p->buffer[i-endPos] = p->buffer[i];
                   1041:        p->pos -= endPos;
                   1042: }
                   1043:
                   1044: /* print complete lines, looking back to the limit position
                   1045:  * (stuff before limit was already scanned).
                   1046:  * returns true if something was printed.
                   1047:  */
                   1048: static bool
                   1049: print_complete_lines(struct job_pipe *p, Job *job, FILE *out, size_t limit)
                   1050: {
                   1051:        size_t i;
                   1052:
                   1053:        for (i = p->pos; i > limit; i--) {
                   1054:                if (p->buffer[i-1] == '\n') {
                   1055:                        print_partial_buffer_and_shift(p, job, out, i);
                   1056:                        return true;
1.2       deraadt  1057:                }
                   1058:        }
1.101     espie    1059:        return false;
1.2       deraadt  1060: }
1.1       deraadt  1061: /*-
                   1062:  *-----------------------------------------------------------------------
1.101     espie    1063:  * handle_pipe --
1.89      espie    1064:  *     This functions is called whenever there is something to read on the
                   1065:  *     pipe. We collect more output from the given job and store it in the
1.101     espie    1066:  *     job's outBuf. If this makes up lines, we print it tagged by the job's
1.89      espie    1067:  *     identifier, as necessary.
1.1       deraadt  1068:  *
                   1069:  * Side Effects:
                   1070:  *     curPos may be shifted as may the contents of outBuf.
                   1071:  *-----------------------------------------------------------------------
                   1072:  */
1.48      espie    1073: static void
1.101     espie    1074: handle_pipe(struct job_pipe *p,
                   1075:        Job *job, FILE *out, bool finish)
1.66      espie    1076: {
                   1077:        int nr;                 /* number of bytes read */
1.101     espie    1078:        int oldpos;             /* optimization */
1.1       deraadt  1079:
1.101     espie    1080:        /* want to get everything ? -> we block */
                   1081:        if (finish)
                   1082:                fcntl(p->fd, F_SETFL, 0);
                   1083:
                   1084:        do {
                   1085:                nr = read(p->fd, &p->buffer[p->pos],
                   1086:                    JOB_BUFSIZE - p->pos);
                   1087:                if (nr == -1) {
                   1088:                        if (errno == EAGAIN)
                   1089:                                break;
                   1090:                        if (DEBUG(JOB)) {
                   1091:                                perror("JobDoOutput(piperead)");
                   1092:                        }
1.66      espie    1093:                }
1.101     espie    1094:                oldpos = p->pos;
                   1095:                p->pos += nr;
                   1096:                if (!print_complete_lines(p, job, out, oldpos))
                   1097:                        if (p->pos == JOB_BUFSIZE) {
                   1098:                                print_partial_buffer(p, job, out, p->pos);
                   1099:                                p->pos = 0;
                   1100:                        }
                   1101:        } while (nr != 0);
1.1       deraadt  1102:
1.101     espie    1103:        /* at end of file, we print whatever is left */
                   1104:        if (nr == 0) {
                   1105:                print_partial_buffer(p, job, out, p->pos);
                   1106:                if (p->pos > 0 && p->buffer[p->pos - 1] != '\n')
                   1107:                        putchar('\n');
                   1108:                p->pos = 0;
1.76      espie    1109:        }
1.101     espie    1110: }
1.6       millert  1111:
1.101     espie    1112: static void
                   1113: handle_job_output(Job *job, int i, bool finish)
                   1114: {
                   1115:        handle_pipe(&job->in[i], job, i == 0 ? stdout : stderr, finish);
1.1       deraadt  1116: }
                   1117:
                   1118: /*-
                   1119:  *-----------------------------------------------------------------------
                   1120:  * Job_CatchChildren --
                   1121:  *     Handle the exit of a child. Called from Make_Make.
                   1122:  *
                   1123:  * Side Effects:
                   1124:  *     The job descriptor is removed from the list of children.
                   1125:  *
                   1126:  * Notes:
                   1127:  *     We do waits, blocking or not, according to the wisdom of our
                   1128:  *     caller, until there are no more children to report. For each
                   1129:  *     job, call JobFinish to finish things off. This will take care of
                   1130:  *     putting jobs on the stoppedJobs queue.
                   1131:  *-----------------------------------------------------------------------
                   1132:  */
                   1133: void
1.76      espie    1134: Job_CatchChildren()
1.1       deraadt  1135: {
1.78      espie    1136:        pid_t pid;      /* pid of dead child */
                   1137:        Job *job;       /* job descriptor for dead child */
                   1138:        LstNode jnode;  /* list element for finding job */
                   1139:        int status;     /* Exit/termination status */
1.1       deraadt  1140:
1.66      espie    1141:        /*
                   1142:         * Don't even bother if we know there's no one around.
                   1143:         */
1.83      espie    1144:        if (nJobs == 0) {
1.66      espie    1145:                return;
1.2       deraadt  1146:        }
1.6       millert  1147:
1.76      espie    1148:        while ((pid = waitpid((pid_t) -1, &status, WNOHANG|WUNTRACED)) > 0) {
1.93      espie    1149:                handle_all_signals();
1.84      espie    1150:                debug_printf("Process %ld exited or stopped.\n", (long)pid);
1.1       deraadt  1151:
1.95      espie    1152:                jnode = Lst_Find(&runningJobs, JobCmpPid, &pid);
1.1       deraadt  1153:
1.18      espie    1154:                if (jnode == NULL) {
1.68      espie    1155:                        if (WIFSIGNALED(status) &&
1.66      espie    1156:                            (WTERMSIG(status) == SIGCONT)) {
                   1157:                                jnode = Lst_Find(&stoppedJobs, JobCmpPid, &pid);
                   1158:                                if (jnode == NULL) {
                   1159:                                        Error("Resumed child (%ld) not in table", (long)pid);
                   1160:                                        continue;
                   1161:                                }
                   1162:                                job = (Job *)Lst_Datum(jnode);
                   1163:                                Lst_Remove(&stoppedJobs, jnode);
                   1164:                        } else {
                   1165:                                Error("Child (%ld) not in table?", (long)pid);
                   1166:                                continue;
                   1167:                        }
                   1168:                } else {
                   1169:                        job = (Job *)Lst_Datum(jnode);
1.95      espie    1170:                        Lst_Remove(&runningJobs, jnode);
1.67      espie    1171:                        nJobs--;
1.84      espie    1172:                        if (jobFull)
                   1173:                                debug_printf("Job queue is no longer full.\n");
1.66      espie    1174:                        jobFull = false;
1.1       deraadt  1175:                }
1.66      espie    1176:
1.101     espie    1177:                JobFinish(job, status);
1.1       deraadt  1178:        }
                   1179: }
                   1180:
                   1181: /*-
                   1182:  *-----------------------------------------------------------------------
                   1183:  * Job_CatchOutput --
                   1184:  *     Catch the output from our children, if we're using
                   1185:  *     pipes do so. Otherwise just block time until we get a
1.6       millert  1186:  *     signal (most likely a SIGCHLD) since there's no point in
1.1       deraadt  1187:  *     just spinning when there's nothing to do and the reaping
1.6       millert  1188:  *     of a child can wait for a while.
1.1       deraadt  1189:  *
                   1190:  * Side Effects:
                   1191:  *     Output is read from pipes if we're piping.
                   1192:  * -----------------------------------------------------------------------
                   1193:  */
                   1194: void
1.56      espie    1195: Job_CatchOutput(void)
1.1       deraadt  1196: {
1.66      espie    1197:        int nfds;
                   1198:        struct timeval timeout;
                   1199:        LstNode ln;
                   1200:        Job *job;
1.101     espie    1201:        int i;
1.66      espie    1202:
1.76      espie    1203:        int count = howmany(outputsn+1, NFDBITS) * sizeof(fd_mask);
                   1204:        fd_set *readfdsp = malloc(count);
1.91      espie    1205:
1.77      espie    1206:        (void)fflush(stdout);
1.76      espie    1207:        if (readfdsp == NULL)
                   1208:                return;
                   1209:
                   1210:        memcpy(readfdsp, outputsp, count);
                   1211:        timeout.tv_sec = SEL_SEC;
                   1212:        timeout.tv_usec = SEL_USEC;
1.66      espie    1213:
1.91      espie    1214:        nfds = select(outputsn+1, readfdsp, NULL, NULL, &timeout);
1.93      espie    1215:        handle_all_signals();
1.91      espie    1216:        if (nfds > 0) {
1.95      espie    1217:                for (ln = Lst_First(&runningJobs); nfds && ln != NULL;
1.76      espie    1218:                    ln = Lst_Adv(ln)) {
                   1219:                        job = (Job *)Lst_Datum(ln);
1.101     espie    1220:                        for (i = 0; i < 2; i++) {
                   1221:                                if (FD_ISSET(job->in[i].fd, readfdsp)) {
                   1222:                                        handle_job_output(job, i, false);
                   1223:                                        nfds--;
                   1224:                                }
1.66      espie    1225:                        }
1.1       deraadt  1226:                }
                   1227:        }
1.76      espie    1228:        free(readfdsp);
1.1       deraadt  1229: }
                   1230:
                   1231: /*-
                   1232:  *-----------------------------------------------------------------------
                   1233:  * Job_Make --
                   1234:  *     Start the creation of a target. Basically a front-end for
                   1235:  *     JobStart used by the Make module.
                   1236:  *
                   1237:  * Side Effects:
                   1238:  *     Another job is started.
                   1239:  *-----------------------------------------------------------------------
                   1240:  */
                   1241: void
1.56      espie    1242: Job_Make(GNode *gn)
1.1       deraadt  1243: {
1.86      espie    1244:        (void)JobStart(gn, 0);
1.1       deraadt  1245: }
                   1246:
                   1247: /*-
                   1248:  *-----------------------------------------------------------------------
                   1249:  * Job_Init --
                   1250:  *     Initialize the process module
                   1251:  *
                   1252:  * Side Effects:
                   1253:  *     lists and counters are initialized
                   1254:  *-----------------------------------------------------------------------
                   1255:  */
                   1256: void
1.83      espie    1257: Job_Init(int maxproc)
1.1       deraadt  1258: {
1.95      espie    1259:        Static_Lst_Init(&runningJobs);
1.66      espie    1260:        Static_Lst_Init(&stoppedJobs);
1.96      espie    1261:        Static_Lst_Init(&queuedJobs);
1.101     espie    1262:        Static_Lst_Init(&errorsList);
1.66      espie    1263:        maxJobs =         maxproc;
1.79      espie    1264:        nJobs =           0;
1.66      espie    1265:        jobFull =         false;
1.101     espie    1266:        errors = 0;
1.66      espie    1267:
                   1268:        aborting =        0;
1.40      espie    1269:
1.66      espie    1270:        lastNode =        NULL;
1.1       deraadt  1271:
1.78      espie    1272:        if ((begin_node->type & OP_DUMMY) == 0) {
1.86      espie    1273:                JobStart(begin_node, JOB_SPECIAL);
1.66      espie    1274:                while (nJobs) {
                   1275:                        Job_CatchOutput();
1.76      espie    1276:                        Job_CatchChildren();
1.66      espie    1277:                }
1.1       deraadt  1278:        }
                   1279: }
                   1280:
                   1281: /*-
                   1282:  *-----------------------------------------------------------------------
                   1283:  * Job_Full --
                   1284:  *     See if the job table is full. It is considered full if it is OR
                   1285:  *     if we are in the process of aborting OR if we have
                   1286:  *     reached/exceeded our local quota. This prevents any more jobs
                   1287:  *     from starting up.
                   1288:  *
                   1289:  * Results:
1.41      espie    1290:  *     true if the job table is full, false otherwise
1.1       deraadt  1291:  *-----------------------------------------------------------------------
                   1292:  */
1.41      espie    1293: bool
1.56      espie    1294: Job_Full(void)
1.1       deraadt  1295: {
1.66      espie    1296:        return aborting || jobFull;
1.1       deraadt  1297: }
                   1298:
                   1299: /*-
                   1300:  *-----------------------------------------------------------------------
                   1301:  * Job_Empty --
1.40      espie    1302:  *     See if the job table is empty.  Because the local concurrency may
1.1       deraadt  1303:  *     be set to 0, it is possible for the job table to become empty,
                   1304:  *     while the list of stoppedJobs remains non-empty. In such a case,
                   1305:  *     we want to restart as many jobs as we can.
                   1306:  *
                   1307:  * Results:
1.41      espie    1308:  *     true if it is. false if it ain't.
1.1       deraadt  1309:  * -----------------------------------------------------------------------
                   1310:  */
1.41      espie    1311: bool
1.56      espie    1312: Job_Empty(void)
1.1       deraadt  1313: {
1.66      espie    1314:        if (nJobs == 0) {
                   1315:                if (!Lst_IsEmpty(&stoppedJobs) && !aborting) {
                   1316:                        /*
                   1317:                         * The job table is obviously not full if it has no
                   1318:                         * jobs in it...Try and restart the stopped jobs.
                   1319:                         */
                   1320:                        jobFull = false;
                   1321:                        JobRestartJobs();
                   1322:                        return false;
                   1323:                } else {
                   1324:                        return true;
                   1325:                }
1.1       deraadt  1326:        } else {
1.66      espie    1327:                return false;
1.1       deraadt  1328:        }
                   1329: }
                   1330:
                   1331: /*-
                   1332:  *-----------------------------------------------------------------------
                   1333:  * JobInterrupt --
                   1334:  *     Handle the receipt of an interrupt.
                   1335:  *
                   1336:  * Side Effects:
                   1337:  *     All children are killed. Another job will be started if the
                   1338:  *     .INTERRUPT target was given.
                   1339:  *-----------------------------------------------------------------------
                   1340:  */
                   1341: static void
1.56      espie    1342: JobInterrupt(int runINTERRUPT, /* Non-zero if commands for the .INTERRUPT
1.1       deraadt  1343:                                 * target should be executed */
1.66      espie    1344:     int signo)                 /* signal received */
1.1       deraadt  1345: {
1.66      espie    1346:        LstNode ln;             /* element in job table */
1.78      espie    1347:        Job *job;               /* job descriptor in that element */
1.66      espie    1348:
                   1349:        aborting = ABORT_INTERRUPT;
                   1350:
1.95      espie    1351:        for (ln = Lst_First(&runningJobs); ln != NULL; ln = Lst_Adv(ln)) {
1.66      espie    1352:                job = (Job *)Lst_Datum(ln);
                   1353:
                   1354:                if (!Targ_Precious(job->node)) {
                   1355:                        const char *file = job->node->path == NULL ?
                   1356:                            job->node->name : job->node->path;
                   1357:                        if (!noExecute && eunlink(file) != -1) {
                   1358:                                Error("*** %s removed", file);
                   1359:                        }
                   1360:                }
                   1361:                if (job->pid) {
1.84      espie    1362:                        debug_printf("JobInterrupt passing signal to "
                   1363:                            "child %ld.\n", (long)job->pid);
1.66      espie    1364:                        KILL(job->pid, signo);
                   1365:                }
1.2       deraadt  1366:        }
1.1       deraadt  1367:
1.66      espie    1368:        if (runINTERRUPT && !touchFlag) {
1.78      espie    1369:                if ((interrupt_node->type & OP_DUMMY) == 0) {
1.66      espie    1370:                        ignoreErrors = false;
                   1371:
1.101     espie    1372:                        JobStart(interrupt_node, 0);
1.66      espie    1373:                        while (nJobs) {
                   1374:                                Job_CatchOutput();
1.76      espie    1375:                                Job_CatchChildren();
1.66      espie    1376:                        }
                   1377:                }
1.1       deraadt  1378:        }
1.66      espie    1379:        exit(signo);
1.1       deraadt  1380: }
                   1381:
                   1382: /*
                   1383:  *-----------------------------------------------------------------------
1.12      espie    1384:  * Job_Finish --
1.1       deraadt  1385:  *     Do final processing such as the running of the commands
1.6       millert  1386:  *     attached to the .END target.
1.1       deraadt  1387:  *
                   1388:  * Results:
                   1389:  *     Number of errors reported.
1.40      espie    1390:  *
1.1       deraadt  1391:  *-----------------------------------------------------------------------
                   1392:  */
                   1393: int
1.56      espie    1394: Job_Finish(void)
1.1       deraadt  1395: {
1.78      espie    1396:        if (end_node != NULL && !Lst_IsEmpty(&end_node->commands)) {
1.66      espie    1397:                if (errors) {
                   1398:                        Error("Errors reported so .END ignored");
                   1399:                } else {
1.101     espie    1400:                        JobStart(end_node, JOB_SPECIAL);
1.1       deraadt  1401:
1.66      espie    1402:                        while (nJobs) {
                   1403:                                Job_CatchOutput();
1.76      espie    1404:                                Job_CatchChildren();
1.66      espie    1405:                        }
                   1406:                }
1.1       deraadt  1407:        }
1.66      espie    1408:        return errors;
1.1       deraadt  1409: }
                   1410:
1.41      espie    1411: #ifdef CLEANUP
1.12      espie    1412: void
1.56      espie    1413: Job_End(void)
1.12      espie    1414: {
1.41      espie    1415: }
1.13      espie    1416: #endif
1.40      espie    1417:
1.1       deraadt  1418: /*-
                   1419:  *-----------------------------------------------------------------------
                   1420:  * Job_Wait --
                   1421:  *     Waits for all running jobs to finish and returns. Sets 'aborting'
                   1422:  *     to ABORT_WAIT to prevent other jobs from starting.
                   1423:  *
                   1424:  * Side Effects:
                   1425:  *     Currently running jobs finish.
                   1426:  *
                   1427:  *-----------------------------------------------------------------------
                   1428:  */
                   1429: void
1.56      espie    1430: Job_Wait(void)
1.1       deraadt  1431: {
1.66      espie    1432:        aborting = ABORT_WAIT;
                   1433:        while (nJobs != 0) {
                   1434:                Job_CatchOutput();
1.76      espie    1435:                Job_CatchChildren();
1.66      espie    1436:        }
                   1437:        aborting = 0;
1.1       deraadt  1438: }
                   1439:
                   1440: /*-
                   1441:  *-----------------------------------------------------------------------
                   1442:  * Job_AbortAll --
                   1443:  *     Abort all currently running jobs without handling output or anything.
                   1444:  *     This function is to be called only in the event of a major
                   1445:  *     error. Most definitely NOT to be called from JobInterrupt.
                   1446:  *
                   1447:  * Side Effects:
                   1448:  *     All children are killed, not just the firstborn
                   1449:  *-----------------------------------------------------------------------
                   1450:  */
                   1451: void
1.56      espie    1452: Job_AbortAll(void)
1.1       deraadt  1453: {
1.66      espie    1454:        LstNode ln;     /* element in job table */
                   1455:        Job *job;       /* the job descriptor in that element */
                   1456:        int foo;
1.6       millert  1457:
1.66      espie    1458:        aborting = ABORT_ERROR;
1.6       millert  1459:
1.66      espie    1460:        if (nJobs) {
1.95      espie    1461:                for (ln = Lst_First(&runningJobs); ln != NULL;
                   1462:                    ln = Lst_Adv(ln)) {
1.66      espie    1463:                        job = (Job *)Lst_Datum(ln);
                   1464:
                   1465:                        /*
                   1466:                         * kill the child process with increasingly drastic
                   1467:                         * signals to make darn sure it's dead.
                   1468:                         */
                   1469:                        KILL(job->pid, SIGINT);
                   1470:                        KILL(job->pid, SIGKILL);
                   1471:                }
1.1       deraadt  1472:        }
1.6       millert  1473:
1.66      espie    1474:        /*
                   1475:         * Catch as many children as want to report in at first, then give up
                   1476:         */
                   1477:        while (waitpid(-1, &foo, WNOHANG) > 0)
                   1478:                continue;
1.2       deraadt  1479: }
1.40      espie    1480:
1.2       deraadt  1481: /*-
                   1482:  *-----------------------------------------------------------------------
                   1483:  * JobRestartJobs --
                   1484:  *     Tries to restart stopped jobs if there are slots available.
                   1485:  *     Note that this tries to restart them regardless of pending errors.
                   1486:  *     It's not good to leave stopped jobs lying around!
                   1487:  *
                   1488:  * Side Effects:
                   1489:  *     Resumes(and possibly migrates) jobs.
                   1490:  *-----------------------------------------------------------------------
                   1491:  */
                   1492: static void
1.56      espie    1493: JobRestartJobs(void)
1.2       deraadt  1494: {
1.66      espie    1495:        Job *job;
1.19      espie    1496:
1.66      espie    1497:        while (!jobFull && (job = (Job *)Lst_DeQueue(&stoppedJobs)) != NULL) {
1.84      espie    1498:                debug_printf("Job queue is not full. "
                   1499:                    "Restarting a stopped job.\n");
1.66      espie    1500:                JobRestart(job);
1.2       deraadt  1501:        }
1.1       deraadt  1502: }