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

1.40      espie       1: /*     $OpenPackages$ */
1.78    ! espie       2: /*     $OpenBSD: job.c,v 1.74 2007/09/17 10:37:08 espie Exp $  */
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: #define TMPPAT "/tmp/makeXXXXXXXXXX"
                    112:
                    113: /*
                    114:  * The SEL_ constants determine the maximum amount of time spent in select
                    115:  * before coming out to see if a child has finished. SEL_SEC is the number of
                    116:  * seconds and SEL_USEC is the number of micro-seconds
                    117:  */
                    118: #define SEL_SEC        0
                    119: #define SEL_USEC       500000
                    120:
                    121:
                    122: /*-
                    123:  * Job Table definitions.
                    124:  *
                    125:  * Each job has several things associated with it:
                    126:  *     1) The process id of the child shell
                    127:  *     2) The graph node describing the target being made by this job
                    128:  *     3) A LstNode for the first command to be saved after the job
                    129:  *        completes. This is NULL if there was no "..." in the job's
                    130:  *        commands.
                    131:  *     4) An FILE* for writing out the commands. This is only
                    132:  *        used before the job is actually started.
1.78    ! espie     133:  *     5) Things used for handling the shell's output.
1.76      espie     134:  *        the output is being caught via a pipe and
1.50      espie     135:  *        the descriptors of our pipe, an array in which output is line
                    136:  *        buffered and the current position in that buffer are all
1.78    ! espie     137:  *        maintained for each job.
1.50      espie     138:  *     6) An identifier provided by and for the exclusive use of the
                    139:  *        Rmt module.
                    140:  *     7) A word of flags which determine how the module handles errors,
                    141:  *        echoing, etc. for the job
                    142:  *
                    143:  * The job "table" is kept as a linked Lst in 'jobs', with the number of
                    144:  * active jobs maintained in the 'nJobs' variable. At no time will this
                    145:  * exceed the value of 'maxJobs', initialized by the Job_Init function.
                    146:  *
                    147:  * When a job is finished, the Make_Update function is called on each of the
                    148:  * parents of the node which was just remade. This takes care of the upward
                    149:  * traversal of the dependency graph.
                    150:  */
                    151: #define JOB_BUFSIZE    1024
                    152: typedef struct Job_ {
1.51      mpech     153:     pid_t      pid;        /* The child's process ID */
1.50      espie     154:     GNode      *node;      /* The target the child is making */
                    155:     LstNode    tailCmds;   /* The node of the first command to be
                    156:                             * saved when the job has been run */
                    157:     FILE       *cmdFILE;   /* When creating the shell script, this is
                    158:                             * where the commands go */
                    159:     int        rmtID;     /* ID returned from Rmt module */
                    160:     short      flags;      /* Flags to control treatment of job */
                    161: #define JOB_IGNERR     0x001   /* Ignore non-zero exits */
                    162: #define JOB_SILENT     0x002   /* no output */
1.78    ! espie     163: #define JOB_SPECIAL    0x004   /* Target is a special one. i.e., always run */
        !           164:                                /* it even when the table is full */
        !           165: #define JOB_IGNDOTS    0x008   /* Ignore "..." lines when processing */
        !           166:                                /* commands */
1.50      espie     167: #define JOB_FIRST      0x020   /* Job is first job for the node */
                    168: #define JOB_RESTART    0x080   /* Job needs to be completely restarted */
1.78    ! espie     169: #define JOB_RESUME     0x100   /* Job needs to be resumed b/c it stopped, */
        !           170:                                /* for some reason */
        !           171: #define JOB_CONTINUING 0x200   /* We are in the process of resuming this job */
        !           172:                                /* Used to avoid infinite recursion between */
        !           173:                                /* JobFinish and JobRestart */
1.76      espie     174:     int        inPipe;         /* Input side of pipe associated
                    175:                                 * with job's output channel */
                    176:     int        outPipe;        /* Output side of pipe associated with
                    177:                                 * job's output channel */
                    178:     char       outBuf[JOB_BUFSIZE + 1];
                    179:                                /* Buffer for storing the output of the
                    180:                                 * job, line by line */
                    181:     int        curPos;         /* Current position in op_outBuf */
1.50      espie     182: } Job;
                    183:
1.78    ! espie     184:
1.1       deraadt   185: /*
1.6       millert   186:  * error handling variables
1.1       deraadt   187:  */
1.40      espie     188: static int     errors = 0;         /* number of errors reported */
                    189: static int     aborting = 0;       /* why is the make aborting? */
                    190: #define ABORT_ERROR    1           /* Because of an error */
                    191: #define ABORT_INTERRUPT 2          /* Because it was interrupted */
                    192: #define ABORT_WAIT     3           /* Waiting for jobs to finish */
1.1       deraadt   193:
1.6       millert   194: /*
1.2       deraadt   195:  * XXX: Avoid SunOS bug... FILENO() is fp->_file, and file
                    196:  * is a char! So when we go above 127 we turn negative!
                    197:  */
                    198: #define FILENO(a) ((unsigned) fileno(a))
1.1       deraadt   199:
1.40      espie     200: static int       numCommands;      /* The number of commands actually printed
1.1       deraadt   201:                                     * for a target. Should this number be
                    202:                                     * 0, no shell will be executed. */
                    203:
                    204: /*
                    205:  * Return values from JobStart.
                    206:  */
1.40      espie     207: #define JOB_RUNNING    0       /* Job is running */
                    208: #define JOB_ERROR      1       /* Error in starting the job */
                    209: #define JOB_FINISHED   2       /* The job is already finished */
                    210: #define JOB_STOPPED    3       /* The job is stopped */
1.1       deraadt   211:
                    212: /*
1.40      espie     213:  * tfile is the name of a file into which all shell commands are put. It is
                    214:  * used over by removing it before the child shell is executed. The XXXXXXXXXX
                    215:  * in the string are replaced by mkstemp(3).
1.1       deraadt   216:  */
1.40      espie     217: static char    tfile[sizeof(TMPPAT)];
1.1       deraadt   218:
                    219:
1.70      espie     220: #define SHELL_ECHO_OFF "set -"
                    221: #define SHELL_ECHO_ON  "set -v"
                    222: #define SHELL_ERROR_ON "set -e"
                    223: #define SHELL_ERROR_OFF        "set +e"
                    224: #define SHELL_ECHO_FLAG "v"
                    225: #define SHELL_ERROR_FLAG "e"
1.1       deraadt   226:
1.70      espie     227: static const char *shellPath = _PATH_BSHELL;
                    228: static const char *shellName = "sh";
1.1       deraadt   229:
1.78    ! espie     230:
1.40      espie     231: static int     maxJobs;        /* The most children we can run at once */
1.48      espie     232: static int     nJobs = 0;      /* The number of children currently running */
                    233: static LIST    jobs;           /* The structures that describe them */
1.78    ! espie     234: static bool    jobFull;        /* Flag to tell when the job table is full. */
1.40      espie     235: static fd_set  *outputsp;      /* Set of descriptors of pipes connected to
1.1       deraadt   236:                                 * the output channels of children */
1.8       deraadt   237: static int     outputsn;
1.48      espie     238: static GNode   *lastNode;      /* The node for which output was most recently
1.1       deraadt   239:                                 * produced. */
1.48      espie     240: static char    *targFmt;       /* Format string to use to head output from a
1.1       deraadt   241:                                 * job when it's not the most-recent job heard
                    242:                                 * from */
1.2       deraadt   243:
                    244: # define TARG_FMT  "--- %s ---\n" /* Default format */
                    245: # define MESSAGE(fp, gn) \
1.40      espie     246:        (void)fprintf(fp, targFmt, gn->name);
1.1       deraadt   247:
                    248: /*
1.50      espie     249:  * When JobStart attempts to run a job but isn't allowed to,
                    250:  * the job is placed on the stoppedJobs queue to be run
1.6       millert   251:  * when the next job finishes.
1.1       deraadt   252:  */
1.48      espie     253: static LIST    stoppedJobs;    /* Lst of Job structures describing
1.1       deraadt   254:                                 * jobs that were stopped due to concurrency
                    255:                                 * limits or migration home */
                    256:
                    257:
                    258: #if defined(USE_PGRP) && defined(SYSV)
1.40      espie     259: # define KILL(pid, sig)        killpg(-(pid), (sig))
1.1       deraadt   260: #else
                    261: # if defined(USE_PGRP)
1.2       deraadt   262: #  define KILL(pid, sig)       killpg((pid), (sig))
1.1       deraadt   263: # else
1.2       deraadt   264: #  define KILL(pid, sig)       kill((pid), (sig))
1.1       deraadt   265: # endif
                    266: #endif
                    267:
1.6       millert   268: /*
1.2       deraadt   269:  * Grmpf... There is no way to set bits of the wait structure
                    270:  * anymore with the stupid W*() macros. I liked the union wait
                    271:  * stuff much more. So, we devise our own macros... This is
                    272:  * really ugly, use dramamine sparingly. You have been warned.
                    273:  */
                    274: #define W_SETMASKED(st, val, fun)                              \
                    275:        {                                                       \
                    276:                int sh = (int) ~0;                              \
                    277:                int mask = fun(sh);                             \
                    278:                                                                \
                    279:                for (sh = 0; ((mask >> sh) & 1) == 0; sh++)     \
                    280:                        continue;                               \
                    281:                *(st) = (*(st) & ~mask) | ((val) << sh);        \
                    282:        }
                    283:
                    284: #define W_SETTERMSIG(st, val) W_SETMASKED(st, val, WTERMSIG)
                    285: #define W_SETEXITSTATUS(st, val) W_SETMASKED(st, val, WEXITSTATUS)
                    286:
                    287:
1.40      espie     288: static void JobCondPassSig(void *, void *);
1.57      espie     289: static void SigHandler(int);
                    290: static void HandleSigs(void);
1.40      espie     291: static void JobPassSig(int);
                    292: static int JobCmpPid(void *, void *);
1.60      espie     293: static int JobPrintCommand(LstNode, void *);
1.40      espie     294: static void JobSaveCommand(void *, void *);
                    295: static void JobClose(Job *);
                    296: static void JobFinish(Job *, int *);
                    297: static void JobExec(Job *, char **);
                    298: static void JobMakeArgv(Job *, char **);
                    299: static void JobRestart(Job *);
                    300: static int JobStart(GNode *, int, Job *);
                    301: static char *JobOutput(Job *, char *, char *, int);
1.41      espie     302: static void JobDoOutput(Job *, bool);
1.40      espie     303: static void JobInterrupt(int, int);
                    304: static void JobRestartJobs(void);
1.69      espie     305: static void DBPRINTF(Job *, const char *, ...);
1.1       deraadt   306:
1.57      espie     307: static volatile sig_atomic_t got_SIGINT, got_SIGHUP, got_SIGQUIT,
                    308:     got_SIGTERM;
                    309: #if defined(USE_PGRP)
                    310: static volatile sig_atomic_t got_SIGTSTP, got_SIGTTOU, got_SIGTTIN,
                    311:     got_SIGWINCH;
                    312: #endif
                    313:
                    314: static void
                    315: SigHandler(int sig)
                    316: {
                    317:        switch(sig) {
                    318:        case SIGINT:
                    319:                got_SIGINT++;
                    320:                break;
                    321:        case SIGHUP:
                    322:                got_SIGHUP++;
                    323:                break;
                    324:        case SIGQUIT:
                    325:                got_SIGQUIT++;
                    326:                break;
                    327:        case SIGTERM:
                    328:                got_SIGTERM++;
                    329:                break;
                    330: #if defined(USE_PGRGP)
                    331:        case SIGTSTP:
                    332:                got_SIGTSTP++;
                    333:                break;
                    334:        case SIGTTOU:
                    335:                got_SIGTTOU++;
                    336:                break;
                    337:        case SIGTTIN:
                    338:                got_SIGTTIN++;
                    339:                break;
                    340:        case SIGWINCH:
                    341:                got_SIGWINCH++;
                    342:                break;
                    343: #endif
                    344:        }
                    345: }
                    346:
                    347: static void
                    348: HandleSigs()
                    349: {
                    350:        if (got_SIGINT) {
                    351:                got_SIGINT=0;
                    352:                JobPassSig(SIGINT);
                    353:        }
                    354:        if (got_SIGHUP) {
                    355:                got_SIGHUP=0;
                    356:                JobPassSig(SIGHUP);
                    357:        }
                    358:        if (got_SIGQUIT) {
                    359:                got_SIGQUIT=0;
                    360:                JobPassSig(SIGQUIT);
                    361:        }
                    362:        if (got_SIGTERM) {
                    363:                got_SIGTERM=0;
                    364:                JobPassSig(SIGTERM);
                    365:        }
                    366: #if defined(USE_PGRP)
                    367:        if (got_SIGTSTP) {
                    368:                got_SIGTSTP=0;
                    369:                JobPassSig(SIGTSTP);
                    370:        }
                    371:        if (got_SIGTTOU) {
                    372:                got_SIGTTOU=0;
                    373:                JobPassSig(SIGTTOU);
                    374:        }
                    375:        if (got_SIGTTIN) {
                    376:                got_SIGTTIN=0;
                    377:                JobPassSig(SIGTTIN);
                    378:        }
                    379:        if (got_SIGWINCH) {
                    380:                got_SIGWINCH=0;
                    381:                JobPassSig(SIGWINCH);
                    382:        }
                    383: #endif
                    384: }
                    385:
1.1       deraadt   386: /*-
                    387:  *-----------------------------------------------------------------------
                    388:  * JobCondPassSig --
1.50      espie     389:  *     Pass a signal to a job if USE_PGRP
1.1       deraadt   390:  *     is defined.
                    391:  *
                    392:  * Side Effects:
                    393:  *     None, except the job may bite it.
                    394:  *-----------------------------------------------------------------------
                    395:  */
1.27      espie     396: static void
1.56      espie     397: JobCondPassSig(void *jobp,     /* Job to biff */
                    398:     void *signop)              /* Signal to send it */
1.1       deraadt   399: {
1.66      espie     400:        Job *job = (Job *)jobp;
                    401:        int signo = *(int *)signop;
                    402:        if (DEBUG(JOB)) {
                    403:                (void)fprintf(stdout,
                    404:                    "JobCondPassSig passing signal %d to child %ld.\n",
                    405:                    signo, (long)job->pid);
                    406:                (void)fflush(stdout);
                    407:        }
                    408:        KILL(job->pid, signo);
1.1       deraadt   409: }
                    410:
                    411: /*-
                    412:  *-----------------------------------------------------------------------
                    413:  * JobPassSig --
1.68      espie     414:  *     Pass a signal to all local jobs if USE_PGRP is defined,
1.50      espie     415:  *     then die ourselves.
1.1       deraadt   416:  *
                    417:  * Side Effects:
                    418:  *     We die by the same signal.
                    419:  *-----------------------------------------------------------------------
                    420:  */
                    421: static void
1.56      espie     422: JobPassSig(int signo) /* The signal number we've received */
1.1       deraadt   423: {
1.66      espie     424:        sigset_t nmask, omask;
                    425:        struct sigaction act;
1.6       millert   426:
1.66      espie     427:        if (DEBUG(JOB)) {
                    428:                (void)fprintf(stdout, "JobPassSig(%d) called.\n", signo);
                    429:                (void)fflush(stdout);
                    430:        }
                    431:        Lst_ForEach(&jobs, JobCondPassSig, &signo);
                    432:
                    433:        /*
                    434:         * Deal with proper cleanup based on the signal received. We only run
                    435:         * the .INTERRUPT target if the signal was in fact an interrupt. The
                    436:         * other three termination signals are more of a "get out *now*"
                    437:         * command.
                    438:         */
                    439:        if (signo == SIGINT) {
                    440:                JobInterrupt(true, signo);
                    441:        } else if (signo == SIGHUP || signo == SIGTERM || signo == SIGQUIT) {
                    442:                JobInterrupt(false, signo);
                    443:        }
1.1       deraadt   444:
1.66      espie     445:        /*
                    446:         * Leave gracefully if SIGQUIT, rather than core dumping.
                    447:         */
                    448:        if (signo == SIGQUIT) {
                    449:                Finish(0);
                    450:        }
1.6       millert   451:
1.66      espie     452:        /*
                    453:         * Send ourselves the signal now we've given the message to everyone
                    454:         * else.  Note we block everything else possible while we're getting
                    455:         * the signal.  This ensures that all our jobs get continued when we
                    456:         * wake up before we take any other signal.
                    457:         */
                    458:        sigemptyset(&nmask);
                    459:        sigaddset(&nmask, signo);
                    460:        sigprocmask(SIG_SETMASK, &nmask, &omask);
                    461:        memset(&act, 0, sizeof act);
                    462:        act.sa_handler = SIG_DFL;
                    463:        sigemptyset(&act.sa_mask);
                    464:        act.sa_flags = 0;
                    465:        sigaction(signo, &act, NULL);
1.6       millert   466:
1.66      espie     467:        if (DEBUG(JOB)) {
                    468:                (void)fprintf(stdout,
                    469:                    "JobPassSig passing signal to self, mask = %x.\n",
                    470:                    ~0 & ~(1 << (signo-1)));
                    471:                (void)fflush(stdout);
                    472:        }
                    473:        (void)signal(signo, SIG_DFL);
1.1       deraadt   474:
1.66      espie     475:        (void)KILL(getpid(), signo);
1.1       deraadt   476:
1.66      espie     477:        signo = SIGCONT;
                    478:        Lst_ForEach(&jobs, JobCondPassSig, &signo);
1.1       deraadt   479:
1.66      espie     480:        (void)sigprocmask(SIG_SETMASK, &omask, NULL);
                    481:        sigprocmask(SIG_SETMASK, &omask, NULL);
                    482:        act.sa_handler = SigHandler;
                    483:        sigaction(signo, &act, NULL);
1.1       deraadt   484: }
                    485:
                    486: /*-
                    487:  *-----------------------------------------------------------------------
                    488:  * JobCmpPid  --
                    489:  *     Compare the pid of the job with the given pid and return 0 if they
                    490:  *     are equal. This function is called from Job_CatchChildren via
                    491:  *     Lst_Find to find the job descriptor of the finished job.
                    492:  *
                    493:  * Results:
                    494:  *     0 if the pid's match
                    495:  *-----------------------------------------------------------------------
                    496:  */
                    497: static int
1.56      espie     498: JobCmpPid(void *job,   /* job to examine */
                    499:     void *pid)         /* process id desired */
1.1       deraadt   500: {
1.66      espie     501:        return *(pid_t *)pid - ((Job *)job)->pid;
1.1       deraadt   502: }
                    503:
1.69      espie     504: static void
                    505: DBPRINTF(Job *job, const char *fmt, ...)
                    506: {
                    507:        va_list va;
                    508:        va_start(va, fmt);
                    509:        if (DEBUG(JOB)) {
                    510:                (void)vfprintf(stdout, fmt, va);
                    511:                fflush(stdout);
                    512:        }
                    513:        vfprintf(job->cmdFILE, fmt, va);
                    514:        va_end(va);
                    515: }
                    516:
1.1       deraadt   517: /*-
                    518:  *-----------------------------------------------------------------------
                    519:  * JobPrintCommand  --
                    520:  *     Put out another command for the given job. If the command starts
                    521:  *     with an @ or a - we process it specially. In the former case,
                    522:  *     so long as the -s and -n flags weren't given to make, we stick
                    523:  *     a shell-specific echoOff command in the script. In the latter,
                    524:  *     we ignore errors for the entire job, unless the shell has error
                    525:  *     control.
                    526:  *     If the command is just "..." we take all future commands for this
                    527:  *     job to be commands to be executed once the entire graph has been
                    528:  *     made and return non-zero to signal that the end of the commands
1.78    ! espie     529:  *     was reached. These commands are later attached to the end_node
1.1       deraadt   530:  *     node and executed by Job_End when all things are done.
1.40      espie     531:  *     This function is called from JobStart via Lst_Find
1.1       deraadt   532:  *
                    533:  * Results:
1.26      espie     534:  *     Always 1, unless the command was "..."
1.1       deraadt   535:  *
                    536:  * Side Effects:
                    537:  *     If the command begins with a '-' and the shell has no error control,
                    538:  *     the JOB_IGNERR flag is set in the job descriptor.
                    539:  *     If the command is "..." and we're not ignoring such things,
                    540:  *     tailCmds is set to the successor node of the cmd.
                    541:  *     numCommands is incremented if the command is actually printed.
                    542:  *-----------------------------------------------------------------------
                    543:  */
                    544: static int
1.78    ! espie     545: JobPrintCommand(LstNode cmdNode,       /* command string to print */
        !           546:     void *jobp)                                /* job for which to print it */
1.1       deraadt   547: {
1.78    ! espie     548:        bool noSpecials;                /* true if we shouldn't worry about
        !           549:                                         * inserting special commands into
        !           550:                                         * the input stream. */
        !           551:        bool shutUp = false;            /* true if we put a no echo command
        !           552:                                         * into the command file */
        !           553:        bool errOff = false;            /* true if we turned error checking
        !           554:                                         * off before printing the command
        !           555:                                         * and need to turn it back on */
        !           556:        char *cmdTemplate;              /* Template to use when printing the
        !           557:                                         * command */
        !           558:        char *cmdStart;                 /* Start of expanded command */
1.66      espie     559:        char *cmd = (char *)Lst_Datum(cmdNode);
                    560:        Job *job = (Job *)jobp;
                    561:
                    562:        noSpecials = (noExecute && !(job->node->type & OP_MAKE));
                    563:
                    564:        if (strcmp(cmd, "...") == 0) {
                    565:                job->node->type |= OP_SAVE_CMDS;
                    566:                if ((job->flags & JOB_IGNDOTS) == 0) {
                    567:                        job->tailCmds = Lst_Succ(cmdNode);
                    568:                        return 0;
                    569:                }
                    570:                return 1;
1.2       deraadt   571:        }
1.1       deraadt   572:
1.78    ! espie     573:
1.67      espie     574:        numCommands++;
1.1       deraadt   575:
1.66      espie     576:        /* For debugging, we replace each command with the result of expanding
                    577:         * the variables in the command.  */
                    578:        cmdStart = cmd = Var_Subst(cmd, &job->node->context, false);
                    579:        Lst_Replace(cmdNode, cmdStart);
1.1       deraadt   580:
1.66      espie     581:        cmdTemplate = "%s\n";
1.1       deraadt   582:
1.66      espie     583:        /*
                    584:         * Check for leading @' and -'s to control echoing and error checking.
                    585:         */
                    586:        for (;; cmd++) {
                    587:                if (*cmd == '@')
                    588:                        shutUp = DEBUG(LOUD) ? false : true;
                    589:                else if (*cmd == '-')
                    590:                        errOff = true;
                    591:                else if (*cmd != '+')
                    592:                        break;
1.1       deraadt   593:        }
                    594:
1.66      espie     595:        while (isspace(*cmd))
                    596:                cmd++;
                    597:
                    598:        if (shutUp) {
1.72      espie     599:                if (!(job->flags & JOB_SILENT) && !noSpecials) {
1.71      espie     600:                        DBPRINTF(job, "%s\n", SHELL_ECHO_OFF);
1.1       deraadt   601:                } else {
1.66      espie     602:                        shutUp = false;
                    603:                }
                    604:        }
                    605:
                    606:        if (errOff) {
                    607:                if ( !(job->flags & JOB_IGNERR) && !noSpecials) {
1.72      espie     608:                        /*
1.78    ! espie     609:                         * we don't want the error-control commands showing
        !           610:                         * up either, so we turn off echoing while executing
        !           611:                         * them. We could put another field in the shell
        !           612:                         * structure to tell JobDoOutput to look for this
        !           613:                         * string too, but why make it any more complex than
        !           614:                         * it already is?
1.72      espie     615:                         */
                    616:                        if (!(job->flags & JOB_SILENT) && !shutUp) {
1.75      espie     617:                                DBPRINTF(job, "%s; %s; %s\n", SHELL_ECHO_OFF,
                    618:                                    SHELL_ERROR_OFF, SHELL_ECHO_ON);
1.66      espie     619:                        } else {
1.72      espie     620:                                DBPRINTF(job, "%s\n", SHELL_ERROR_OFF);
1.66      espie     621:                        }
                    622:                } else {
                    623:                        errOff = false;
1.1       deraadt   624:                }
1.66      espie     625:        }
                    626:
1.69      espie     627:        DBPRINTF(job, cmdTemplate, cmd);
1.66      espie     628:
                    629:        if (errOff) {
                    630:                /*
                    631:                 * If echoing is already off, there's no point in issuing the
                    632:                 * echoOff command. Otherwise we issue it and pretend it was on
                    633:                 * for the whole command...
1.1       deraadt   634:                 */
1.72      espie     635:                if (!shutUp && !(job->flags & JOB_SILENT)) {
1.71      espie     636:                        DBPRINTF(job, "%s\n", SHELL_ECHO_OFF);
1.41      espie     637:                        shutUp = true;
1.1       deraadt   638:                }
1.71      espie     639:                DBPRINTF(job, "%s\n", SHELL_ERROR_ON);
1.66      espie     640:        }
                    641:        if (shutUp) {
1.71      espie     642:                DBPRINTF(job, "%s\n", SHELL_ECHO_ON);
1.1       deraadt   643:        }
1.66      espie     644:        return 1;
1.1       deraadt   645: }
                    646:
                    647: /*-
                    648:  *-----------------------------------------------------------------------
                    649:  * JobSaveCommand --
                    650:  *     Save a command to be executed when everything else is done.
                    651:  *     Callback function for JobFinish...
                    652:  *
                    653:  * Side Effects:
1.78    ! espie     654:  *     The command is tacked onto the end of end_node's commands list.
1.1       deraadt   655:  *-----------------------------------------------------------------------
                    656:  */
1.27      espie     657: static void
1.56      espie     658: JobSaveCommand(void *cmd, void *gn)
1.1       deraadt   659: {
1.66      espie     660:        GNode *g = (GNode *)gn;
                    661:        char *result;
1.28      espie     662:
1.66      espie     663:        result = Var_Subst((char *)cmd, &g->context, false);
1.78    ! espie     664:        Lst_AtEnd(&end_node->commands, result);
1.2       deraadt   665: }
                    666:
                    667:
                    668: /*-
                    669:  *-----------------------------------------------------------------------
                    670:  * JobClose --
                    671:  *     Called to close both input and output pipes when a job is finished.
                    672:  *
                    673:  * Side Effects:
                    674:  *     The file descriptors associated with the job are closed.
                    675:  *-----------------------------------------------------------------------
                    676:  */
                    677: static void
1.56      espie     678: JobClose(Job *job)
1.2       deraadt   679: {
1.76      espie     680:        FD_CLR(job->inPipe, outputsp);
                    681:        if (job->outPipe != job->inPipe) {
                    682:               (void)close(job->outPipe);
1.66      espie     683:        }
1.76      espie     684:        JobDoOutput(job, true);
                    685:        (void)close(job->inPipe);
1.1       deraadt   686: }
                    687:
                    688: /*-
                    689:  *-----------------------------------------------------------------------
                    690:  * JobFinish  --
                    691:  *     Do final processing for the given job including updating
                    692:  *     parents and starting new jobs as available/necessary. Note
                    693:  *     that we pay no attention to the JOB_IGNERR flag here.
                    694:  *     This is because when we're called because of a noexecute flag
                    695:  *     or something, jstat.w_status is 0 and when called from
                    696:  *     Job_CatchChildren, the status is zeroed if it s/b ignored.
                    697:  *
                    698:  * Side Effects:
                    699:  *     Some nodes may be put on the toBeMade queue.
1.78    ! espie     700:  *     Final commands for the job are placed on end_node.
1.1       deraadt   701:  *
1.6       millert   702:  *     If we got an error and are aborting (aborting == ABORT_ERROR) and
1.1       deraadt   703:  *     the job list is now empty, we are done for the day.
1.6       millert   704:  *     If we recognized an error (errors !=0), we set the aborting flag
1.1       deraadt   705:  *     to ABORT_ERROR so no more jobs will be started.
                    706:  *-----------------------------------------------------------------------
                    707:  */
                    708: /*ARGSUSED*/
                    709: static void
1.56      espie     710: JobFinish(Job *job,            /* job to finish */
                    711:     int *status)               /* sub-why job went away */
1.2       deraadt   712: {
1.78    ! espie     713:        bool     done;
1.2       deraadt   714:
1.66      espie     715:        if ((WIFEXITED(*status) &&
                    716:             WEXITSTATUS(*status) != 0 && !(job->flags & JOB_IGNERR)) ||
                    717:            (WIFSIGNALED(*status) && WTERMSIG(*status) != SIGCONT)) {
                    718:                /*
                    719:                 * If it exited non-zero and either we're doing things our
                    720:                 * way or we're not ignoring errors, the job is finished.
                    721:                 * Similarly, if the shell died because of a signal
                    722:                 * the job is also finished. In these
                    723:                 * cases, finish out the job's output before printing the exit
                    724:                 * status...
                    725:                 */
                    726:                JobClose(job);
                    727:                if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
                    728:                       (void)fclose(job->cmdFILE);
                    729:                }
                    730:                done = true;
                    731:        } else if (WIFEXITED(*status)) {
                    732:                /*
                    733:                 * Deal with ignored errors in -B mode. We need to print a
                    734:                 * message telling of the ignored error as well as setting
                    735:                 * status.w_status to 0 so the next command gets run. To do
                    736:                 * this, we set done to be true if in -B mode and the job
                    737:                 * exited non-zero.
                    738:                 */
                    739:                done = WEXITSTATUS(*status) != 0;
                    740:                /*
                    741:                 * Old comment said: "Note we don't want to close down any of
                    742:                 * the streams until we know we're at the end." But we do.
                    743:                 * Otherwise when are we going to print the rest of the stuff?
                    744:                 */
                    745:                JobClose(job);
                    746:        } else {
                    747:                /*
                    748:                 * No need to close things down or anything.
                    749:                 */
                    750:                done = false;
1.1       deraadt   751:        }
1.6       millert   752:
1.66      espie     753:        if (done ||
                    754:            WIFSTOPPED(*status) ||
                    755:            (WIFSIGNALED(*status) && WTERMSIG(*status) == SIGCONT) ||
                    756:            DEBUG(JOB)) {
                    757:                FILE *out;
                    758:
1.76      espie     759:                out = stdout;
1.1       deraadt   760:
1.66      espie     761:                if (WIFEXITED(*status)) {
                    762:                        if (DEBUG(JOB)) {
1.78    ! espie     763:                                (void)fprintf(stdout,
        !           764:                                    "Process %ld exited.\n", (long)job->pid);
1.66      espie     765:                                (void)fflush(stdout);
                    766:                        }
                    767:                        if (WEXITSTATUS(*status) != 0) {
1.76      espie     768:                                if (job->node != lastNode) {
1.66      espie     769:                                        MESSAGE(out, job->node);
                    770:                                        lastNode = job->node;
                    771:                                }
                    772:                                (void)fprintf(out, "*** Error code %d%s\n",
                    773:                                    WEXITSTATUS(*status),
1.68      espie     774:                                    (job->flags & JOB_IGNERR) ? "(ignored)" :
1.66      espie     775:                                    "");
                    776:
                    777:                                if (job->flags & JOB_IGNERR) {
                    778:                                        *status = 0;
                    779:                                }
                    780:                        } else if (DEBUG(JOB)) {
1.76      espie     781:                                if (job->node != lastNode) {
1.66      espie     782:                                        MESSAGE(out, job->node);
                    783:                                        lastNode = job->node;
                    784:                                }
1.68      espie     785:                                (void)fprintf(out,
1.66      espie     786:                                    "*** Completed successfully\n");
                    787:                        }
                    788:                } else if (WIFSTOPPED(*status)) {
                    789:                        if (DEBUG(JOB)) {
1.78    ! espie     790:                                (void)fprintf(stdout,
        !           791:                                    "Process %ld stopped.\n", (long)job->pid);
1.66      espie     792:                                (void)fflush(stdout);
                    793:                        }
1.76      espie     794:                        if (job->node != lastNode) {
1.66      espie     795:                                MESSAGE(out, job->node);
                    796:                                lastNode = job->node;
                    797:                        }
                    798:                        (void)fprintf(out, "*** Stopped -- signal %d\n",
                    799:                            WSTOPSIG(*status));
                    800:                        job->flags |= JOB_RESUME;
                    801:                        Lst_AtEnd(&stoppedJobs, job);
                    802:                        (void)fflush(out);
                    803:                        return;
                    804:                } else if (WTERMSIG(*status) == SIGCONT) {
                    805:                        /*
                    806:                         * If the beastie has continued, shift the Job from the
                    807:                         * stopped list to the running one (or re-stop it if
                    808:                         * concurrency is exceeded) and go and get another
                    809:                         * child.
                    810:                         */
                    811:                        if (job->flags & (JOB_RESUME|JOB_RESTART)) {
1.76      espie     812:                                if (job->node != lastNode) {
1.66      espie     813:                                        MESSAGE(out, job->node);
                    814:                                        lastNode = job->node;
                    815:                                }
                    816:                                (void)fprintf(out, "*** Continued\n");
                    817:                        }
                    818:                        if (!(job->flags & JOB_CONTINUING)) {
                    819:                                if (DEBUG(JOB)) {
                    820:                                        (void)fprintf(stdout,
1.78    ! espie     821:                                            "Warning: process %ld was not continuing.\n",
1.66      espie     822:                                            (long)job->pid);
                    823:                                        (void)fflush(stdout);
                    824:                                }
                    825: #if 0
                    826:                                /*
                    827:                                 * We don't really want to restart a job from
                    828:                                 * scratch just because it continued,
                    829:                                 * especially not without killing the
                    830:                                 * continuing process!  That's why this is
                    831:                                 * ifdef'ed out.  FD - 9/17/90
                    832:                                 */
                    833:                                JobRestart(job);
1.2       deraadt   834: #endif
1.66      espie     835:                        }
                    836:                        job->flags &= ~JOB_CONTINUING;
                    837:                        Lst_AtEnd(&jobs, job);
1.67      espie     838:                        nJobs++;
1.66      espie     839:                        if (DEBUG(JOB)) {
                    840:                                (void)fprintf(stdout,
                    841:                                    "Process %ld is continuing locally.\n",
                    842:                                    (long)job->pid);
                    843:                                (void)fflush(stdout);
                    844:                        }
                    845:                        if (nJobs == maxJobs) {
                    846:                                jobFull = true;
                    847:                                if (DEBUG(JOB)) {
1.68      espie     848:                                        (void)fprintf(stdout,
1.66      espie     849:                                            "Job queue is full.\n");
                    850:                                        (void)fflush(stdout);
                    851:                                }
                    852:                        }
                    853:                        (void)fflush(out);
                    854:                        return;
                    855:                } else {
1.76      espie     856:                        if (job->node != lastNode) {
1.66      espie     857:                                MESSAGE(out, job->node);
                    858:                                lastNode = job->node;
                    859:                        }
1.68      espie     860:                        (void)fprintf(out, "*** Signal %d\n",
1.66      espie     861:                            WTERMSIG(*status));
1.40      espie     862:                }
1.66      espie     863:
                    864:                (void)fflush(out);
1.1       deraadt   865:        }
                    866:
1.66      espie     867:        /*
                    868:         * Now handle the -B-mode stuff. If the beast still isn't finished,
                    869:         * try and restart the job on the next command. If JobStart says it's
                    870:         * ok, it's ok. If there's an error, this puppy is done.
                    871:         */
                    872:        if (compatMake && WIFEXITED(*status) && job->node->current != NULL) {
                    873:                switch (JobStart(job->node, job->flags & JOB_IGNDOTS, job)) {
                    874:                case JOB_RUNNING:
                    875:                        done = false;
                    876:                        break;
                    877:                case JOB_ERROR:
                    878:                        done = true;
                    879:                        W_SETEXITSTATUS(status, 1);
                    880:                        break;
                    881:                case JOB_FINISHED:
                    882:                        /*
                    883:                         * If we got back a JOB_FINISHED code, JobStart has
                    884:                         * already called Make_Update and freed the job
                    885:                         * descriptor. We set done to false here to avoid fake
                    886:                         * cycles and double frees.  JobStart needs to do the
                    887:                         * update so we can proceed up the graph when given the
                    888:                         * -n flag..
                    889:                         */
                    890:                        done = false;
                    891:                        break;
                    892:                }
                    893:        } else
                    894:                done = true;
1.1       deraadt   895:
1.66      espie     896:        if (done &&
                    897:            aborting != ABORT_ERROR &&
                    898:            aborting != ABORT_INTERRUPT &&
                    899:            *status == 0) {
                    900:                /* As long as we aren't aborting and the job didn't return a
                    901:                 * non-zero status that we shouldn't ignore, we call
                    902:                 * Make_Update to update the parents. In addition, any saved
                    903:                 * commands for the node are placed on the .END target. */
                    904:                Lst_ForEachFrom(job->tailCmds, JobSaveCommand, job->node);
                    905:                job->node->made = MADE;
                    906:                Make_Update(job->node);
                    907:                free(job);
                    908:        } else if (*status != 0) {
1.67      espie     909:                errors++;
1.66      espie     910:                free(job);
                    911:        }
1.1       deraadt   912:
1.66      espie     913:        JobRestartJobs();
1.1       deraadt   914:
                    915:        /*
1.66      espie     916:         * Set aborting if any error.
1.1       deraadt   917:         */
1.66      espie     918:        if (errors && !keepgoing && aborting != ABORT_INTERRUPT) {
                    919:                /*
                    920:                 * If we found any errors in this batch of children and the -k
                    921:                 * flag wasn't given, we set the aborting flag so no more jobs
                    922:                 * get started.
                    923:                 */
                    924:                aborting = ABORT_ERROR;
                    925:        }
1.6       millert   926:
1.66      espie     927:        if (aborting == ABORT_ERROR && Job_Empty()) {
                    928:                /*
                    929:                 * If we are aborting and the job table is now empty, we finish.
                    930:                 */
                    931:                (void)eunlink(tfile);
                    932:                Finish(errors);
                    933:        }
1.1       deraadt   934: }
                    935:
                    936: /*-
                    937:  *-----------------------------------------------------------------------
                    938:  * JobExec --
                    939:  *     Execute the shell for the given job. Called from JobStart and
                    940:  *     JobRestart.
                    941:  *
                    942:  * Side Effects:
                    943:  *     A shell is executed, outputs is altered and the Job structure added
                    944:  *     to the job table.
                    945:  *-----------------------------------------------------------------------
                    946:  */
                    947: static void
1.56      espie     948: JobExec(Job *job, char **argv)
1.1       deraadt   949: {
1.66      espie     950:        pid_t cpid;     /* ID of new child */
1.6       millert   951:
1.66      espie     952:        if (DEBUG(JOB)) {
1.78    ! espie     953:                int i;
1.6       millert   954:
1.66      espie     955:                (void)fprintf(stdout, "Running %s\n", job->node->name);
                    956:                (void)fprintf(stdout, "\tCommand: ");
                    957:                for (i = 0; argv[i] != NULL; i++) {
                    958:                        (void)fprintf(stdout, "%s ", argv[i]);
                    959:                }
                    960:                (void)fprintf(stdout, "\n");
                    961:                (void)fflush(stdout);
1.1       deraadt   962:        }
1.6       millert   963:
1.66      espie     964:        /*
                    965:         * Some jobs produce no output and it's disconcerting to have
                    966:         * no feedback of their running (since they produce no output, the
                    967:         * banner with their name in it never appears). This is an attempt to
                    968:         * provide that feedback, even if nothing follows it.
                    969:         */
                    970:        if (lastNode != job->node && (job->flags & JOB_FIRST) &&
                    971:            !(job->flags & JOB_SILENT)) {
                    972:                MESSAGE(stdout, job->node);
                    973:                lastNode = job->node;
                    974:        }
1.1       deraadt   975:
1.66      espie     976:        if ((cpid = fork()) == -1) {
                    977:                Punt("Cannot fork");
                    978:        } else if (cpid == 0) {
1.6       millert   979:
1.66      espie     980:                /*
                    981:                 * Must duplicate the input stream down to the child's input
                    982:                 * and reset it to the beginning (again). Since the stream was
                    983:                 * marked close-on-exec, we must clear that bit in the new
                    984:                 * input.
                    985:                 */
                    986:                if (dup2(FILENO(job->cmdFILE), 0) == -1)
                    987:                        Punt("Cannot dup2: %s", strerror(errno));
                    988:                (void)fcntl(0, F_SETFD, 0);
                    989:                (void)lseek(0, 0, SEEK_SET);
                    990:
1.76      espie     991:                /*
1.78    ! espie     992:                 * Set up the child's output to be routed through the pipe
        !           993:                 * we've created for it.
1.76      espie     994:                 */
                    995:                if (dup2(job->outPipe, 1) == -1)
                    996:                        Punt("Cannot dup2: %s", strerror(errno));
1.66      espie     997:                /*
                    998:                 * The output channels are marked close on exec. This bit was
                    999:                 * duplicated by the dup2 (on some systems), so we have to
                   1000:                 * clear it before routing the shell's error output to the same
                   1001:                 * place as its standard output.
                   1002:                 */
                   1003:                (void)fcntl(1, F_SETFD, 0);
                   1004:                if (dup2(1, 2) == -1)
                   1005:                        Punt("Cannot dup2: %s", strerror(errno));
1.1       deraadt  1006:
                   1007: #ifdef USE_PGRP
1.66      espie    1008:                /*
                   1009:                 * We want to switch the child into a different process family
                   1010:                 * so we can kill it and all its descendants in one fell swoop,
                   1011:                 * by killing its process family, but not commit suicide.
                   1012:                 */
1.2       deraadt  1013: # if defined(SYSV)
1.66      espie    1014:                (void)setsid();
1.2       deraadt  1015: # else
1.66      espie    1016:                (void)setpgid(0, getpid());
1.2       deraadt  1017: # endif
                   1018: #endif /* USE_PGRP */
1.1       deraadt  1019:
1.66      espie    1020:                (void)execv(shellPath, argv);
                   1021:
                   1022:                (void)write(STDERR_FILENO, "Could not execute shell\n",
                   1023:                    sizeof("Could not execute shell"));
                   1024:                _exit(1);
                   1025:        } else {
                   1026:                job->pid = cpid;
                   1027:
1.76      espie    1028:                if (job->flags & JOB_FIRST) {
1.66      espie    1029:                        /*
                   1030:                         * The first time a job is run for a node, we set the
                   1031:                         * current position in the buffer to the beginning and
                   1032:                         * mark another stream to watch in the outputs mask
                   1033:                         */
                   1034:                        job->curPos = 0;
                   1035:
                   1036:                        if (outputsp == NULL || job->inPipe > outputsn) {
                   1037:                                int bytes, obytes;
                   1038:                                char *tmp;
                   1039:
1.68      espie    1040:                                bytes = howmany(job->inPipe+1, NFDBITS) *
1.66      espie    1041:                                    sizeof(fd_mask);
1.68      espie    1042:                                obytes = outputsn ?
                   1043:                                    howmany(outputsn+1, NFDBITS) *
1.66      espie    1044:                                    sizeof(fd_mask) : 0;
                   1045:
                   1046:                                if (bytes != obytes) {
                   1047:                                        tmp = realloc(outputsp, bytes);
                   1048:                                        if (tmp == NULL)
                   1049:                                                return;
                   1050:                                        memset(tmp + obytes, 0, bytes - obytes);
                   1051:                                        outputsp = (fd_set *)tmp;
                   1052:                                }
                   1053:                                outputsn = job->inPipe;
                   1054:                        }
                   1055:                        FD_SET(job->inPipe, outputsp);
                   1056:                }
1.2       deraadt  1057:
1.66      espie    1058:                /*
                   1059:                 * XXX: Used to not happen if REMOTE. Why?
                   1060:                 */
                   1061:                if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
                   1062:                        (void)fclose(job->cmdFILE);
                   1063:                        job->cmdFILE = NULL;
                   1064:                }
1.1       deraadt  1065:        }
                   1066:
1.48      espie    1067:        /*
1.66      espie    1068:         * Now the job is actually running, add it to the table.
1.48      espie    1069:         */
1.67      espie    1070:        nJobs++;
1.66      espie    1071:        Lst_AtEnd(&jobs, job);
                   1072:        if (nJobs == maxJobs) {
                   1073:                jobFull = true;
1.1       deraadt  1074:        }
                   1075: }
                   1076:
                   1077: /*-
                   1078:  *-----------------------------------------------------------------------
                   1079:  * JobMakeArgv --
                   1080:  *     Create the argv needed to execute the shell for a given job.
                   1081:  *-----------------------------------------------------------------------
                   1082:  */
                   1083: static void
1.56      espie    1084: JobMakeArgv(Job *job, char **argv)
1.1       deraadt  1085: {
1.66      espie    1086:        int argc;
                   1087:        static char args[10];   /* For merged arguments */
1.6       millert  1088:
1.70      espie    1089:        argv[0] = (char *)shellName;
1.66      espie    1090:        argc = 1;
1.1       deraadt  1091:
1.73      espie    1092:        (void)snprintf(args, sizeof(args), "-%s%s",
                   1093:            (job->flags & JOB_IGNERR) ? "" : SHELL_ERROR_FLAG,
                   1094:            (job->flags & JOB_SILENT) ? "" : SHELL_ECHO_FLAG);
                   1095:
                   1096:        if (args[1]) {
                   1097:                argv[argc] = args;
                   1098:                argc++;
1.78    ! espie    1099:        }
1.66      espie    1100:        argv[argc] = NULL;
1.1       deraadt  1101: }
                   1102:
                   1103: /*-
                   1104:  *-----------------------------------------------------------------------
                   1105:  * JobRestart --
1.6       millert  1106:  *     Restart a job that stopped for some reason.
1.1       deraadt  1107:  *
                   1108:  * Side Effects:
                   1109:  *     jobFull will be set if the job couldn't be run.
                   1110:  *-----------------------------------------------------------------------
                   1111:  */
                   1112: static void
1.56      espie    1113: JobRestart(Job *job)
1.1       deraadt  1114: {
1.66      espie    1115:        if (job->flags & JOB_RESTART) {
                   1116:                /*
                   1117:                 * Set up the control arguments to the shell. This is based on
                   1118:                 * the flags set earlier for this job. If the JOB_IGNERR flag
                   1119:                 * is clear, the 'exit' flag of the commandShell is used to
                   1120:                 * cause it to exit upon receiving an error. If the JOB_SILENT
                   1121:                 * flag is clear, the 'echo' flag of the commandShell is used
                   1122:                 * to get it to start echoing as soon as it starts processing
                   1123:                 * commands.
                   1124:                 */
                   1125:                char *argv[4];
1.6       millert  1126:
1.66      espie    1127:                JobMakeArgv(job, argv);
1.2       deraadt  1128:
1.1       deraadt  1129:                if (DEBUG(JOB)) {
1.68      espie    1130:                        (void)fprintf(stdout, "Restarting %s...",
1.66      espie    1131:                            job->node->name);
                   1132:                        (void)fflush(stdout);
1.1       deraadt  1133:                }
1.78    ! espie    1134:                if (nJobs >= maxJobs && !(job->flags & JOB_SPECIAL)) {
1.66      espie    1135:                        /*
                   1136:                         * Can't be exported and not allowed to run locally --
                   1137:                         * put it back on the hold queue and mark the table
                   1138:                         * full
                   1139:                         */
                   1140:                        if (DEBUG(JOB)) {
                   1141:                                (void)fprintf(stdout, "holding\n");
                   1142:                                (void)fflush(stdout);
                   1143:                        }
                   1144:                        Lst_AtFront(&stoppedJobs, job);
                   1145:                        jobFull = true;
                   1146:                        if (DEBUG(JOB)) {
                   1147:                                (void)fprintf(stdout, "Job queue is full.\n");
                   1148:                                (void)fflush(stdout);
                   1149:                        }
                   1150:                        return;
                   1151:                } else {
                   1152:                        /*
                   1153:                         * Job may be run locally.
                   1154:                         */
                   1155:                        if (DEBUG(JOB)) {
                   1156:                                (void)fprintf(stdout, "running locally\n");
                   1157:                                (void)fflush(stdout);
                   1158:                        }
1.1       deraadt  1159:                }
1.66      espie    1160:                JobExec(job, argv);
                   1161:        } else {
1.1       deraadt  1162:                /*
1.66      espie    1163:                 * The job has stopped and needs to be restarted. Why it
                   1164:                 * stopped, we don't know...
1.1       deraadt  1165:                 */
                   1166:                if (DEBUG(JOB)) {
1.66      espie    1167:                       (void)fprintf(stdout, "Resuming %s...", job->node->name);
                   1168:                       (void)fflush(stdout);
1.1       deraadt  1169:                }
1.78    ! espie    1170:                if (nJobs != maxJobs || (job->flags & JOB_SPECIAL)) {
1.66      espie    1171:                        /*
1.78    ! espie    1172:                         * If we haven't reached the concurrency limit already,
        !          1173:                         * it's ok to resume the job.
1.66      espie    1174:                         */
                   1175:                        bool error;
                   1176:                        int status;
                   1177:
                   1178:                        error = KILL(job->pid, SIGCONT) != 0;
                   1179:
                   1180:                        if (!error) {
                   1181:                                /*
                   1182:                                 * Make sure the user knows we've continued the
                   1183:                                 * beast and actually put the thing in the job
                   1184:                                 * table.
                   1185:                                 */
                   1186:                                job->flags |= JOB_CONTINUING;
                   1187:                                W_SETTERMSIG(&status, SIGCONT);
                   1188:                                JobFinish(job, &status);
                   1189:
                   1190:                                job->flags &= ~(JOB_RESUME|JOB_CONTINUING);
                   1191:                                if (DEBUG(JOB)) {
                   1192:                                       (void)fprintf(stdout, "done\n");
                   1193:                                       (void)fflush(stdout);
                   1194:                                }
                   1195:                        } else {
                   1196:                                Error("couldn't resume %s: %s",
                   1197:                                    job->node->name, strerror(errno));
                   1198:                                status = 0;
                   1199:                                W_SETEXITSTATUS(&status, 1);
                   1200:                                JobFinish(job, &status);
                   1201:                        }
                   1202:                } else {
                   1203:                        /*
                   1204:                         * Job cannot be restarted. Mark the table as full and
                   1205:                         * place the job back on the list of stopped jobs.
                   1206:                         */
                   1207:                        if (DEBUG(JOB)) {
                   1208:                                (void)fprintf(stdout, "table full\n");
                   1209:                                (void)fflush(stdout);
                   1210:                        }
                   1211:                        Lst_AtFront(&stoppedJobs, job);
                   1212:                        jobFull = true;
                   1213:                        if (DEBUG(JOB)) {
                   1214:                                (void)fprintf(stdout, "Job queue is full.\n");
                   1215:                                (void)fflush(stdout);
                   1216:                        }
1.1       deraadt  1217:                }
                   1218:        }
                   1219: }
                   1220:
                   1221: /*-
                   1222:  *-----------------------------------------------------------------------
                   1223:  * JobStart  --
                   1224:  *     Start a target-creation process going for the target described
1.6       millert  1225:  *     by the graph node gn.
1.1       deraadt  1226:  *
                   1227:  * Results:
                   1228:  *     JOB_ERROR if there was an error in the commands, JOB_FINISHED
                   1229:  *     if there isn't actually anything left to do for the job and
                   1230:  *     JOB_RUNNING if the job has been started.
                   1231:  *
                   1232:  * Side Effects:
                   1233:  *     A new Job node is created and added to the list of running
                   1234:  *     jobs. PMake is forked and a child shell created.
                   1235:  *-----------------------------------------------------------------------
                   1236:  */
                   1237: static int
1.78    ! espie    1238: JobStart(GNode *gn,            /* target to create */
        !          1239:     int flags,                 /* flags for the job to override normal ones.
        !          1240:                                 * e.g. JOB_SPECIAL or JOB_IGNDOTS */
        !          1241:     Job *previous)             /* The previous Job structure for this node,
        !          1242:                                 * if any. */
        !          1243: {
        !          1244:        Job *job;               /* new job descriptor */
        !          1245:        char *argv[4];          /* Argument vector to shell */
        !          1246:        bool cmdsOK;            /* true if the nodes commands were all right */
        !          1247:        bool local;             /* Set true if the job was run locally */
        !          1248:        bool noExec;            /* Set true if we decide not to run the job */
1.66      espie    1249:
                   1250:        if (previous != NULL) {
                   1251:                previous->flags &= ~(JOB_FIRST|JOB_IGNERR|JOB_SILENT);
                   1252:                job = previous;
                   1253:        } else {
                   1254:                job = emalloc(sizeof(Job));
                   1255:                if (job == NULL) {
                   1256:                        Punt("JobStart out of memory");
                   1257:                }
                   1258:                flags |= JOB_FIRST;
1.1       deraadt  1259:        }
                   1260:
1.66      espie    1261:        job->node = gn;
                   1262:        job->tailCmds = NULL;
1.1       deraadt  1263:
                   1264:        /*
1.66      espie    1265:         * Set the initial value of the flags for this job based on the global
                   1266:         * ones and the node's attributes... Any flags supplied by the caller
                   1267:         * are also added to the field.
1.1       deraadt  1268:         */
1.66      espie    1269:        job->flags = 0;
                   1270:        if (Targ_Ignore(gn)) {
                   1271:                job->flags |= JOB_IGNERR;
                   1272:        }
                   1273:        if (Targ_Silent(gn)) {
                   1274:                job->flags |= JOB_SILENT;
1.1       deraadt  1275:        }
1.66      espie    1276:        job->flags |= flags;
1.6       millert  1277:
1.1       deraadt  1278:        /*
1.66      espie    1279:         * Check the commands now so any attributes from .DEFAULT have a chance
                   1280:         * to migrate to the node
1.1       deraadt  1281:         */
1.66      espie    1282:        if (!compatMake && job->flags & JOB_FIRST) {
                   1283:                cmdsOK = Job_CheckCommands(gn, Error);
                   1284:        } else {
                   1285:                cmdsOK = true;
                   1286:        }
1.1       deraadt  1287:
                   1288:        /*
1.66      espie    1289:         * If the -n flag wasn't given, we open up OUR (not the child's)
                   1290:         * temporary file to stuff commands in it. The thing is rd/wr so we
                   1291:         * don't need to reopen it to feed it to the shell. If the -n flag
                   1292:         * *was* given, we just set the file to be stdout. Cute, huh?
1.1       deraadt  1293:         */
1.66      espie    1294:        if ((gn->type & OP_MAKE) || (!noExecute && !touchFlag)) {
                   1295:                /*
                   1296:                 * We're serious here, but if the commands were bogus, we're
                   1297:                 * also dead...
                   1298:                 */
                   1299:                if (!cmdsOK) {
                   1300:                        DieHorribly();
                   1301:                }
1.6       millert  1302:
1.66      espie    1303:                job->cmdFILE = fopen(tfile, "w+");
                   1304:                if (job->cmdFILE == NULL) {
                   1305:                        Punt("Could not open %s", tfile);
                   1306:                }
                   1307:                (void)fcntl(FILENO(job->cmdFILE), F_SETFD, 1);
                   1308:                /*
                   1309:                 * Send the commands to the command file, flush all its buffers
                   1310:                 * then rewind and remove the thing.
                   1311:                 */
                   1312:                noExec = false;
                   1313:
                   1314:                /*
                   1315:                 * used to be backwards; replace when start doing multiple
                   1316:                 * commands per shell.
                   1317:                 */
                   1318:                if (compatMake) {
                   1319:                        /*
                   1320:                         * Be compatible: If this is the first time for this
                   1321:                         * node, verify its commands are ok and open the
                   1322:                         * commands list for sequential access by later
                   1323:                         * invocations of JobStart.  Once that is done, we take
                   1324:                         * the next command off the list and print it to the
                   1325:                         * command file. If the command was an ellipsis, note
                   1326:                         * that there's nothing more to execute.
                   1327:                         */
                   1328:                        if ((job->flags&JOB_FIRST))
                   1329:                                gn->current = Lst_First(&gn->commands);
                   1330:                        else
                   1331:                                gn->current = Lst_Succ(gn->current);
                   1332:
                   1333:                        if (gn->current == NULL ||
                   1334:                            !JobPrintCommand(gn->current, job)) {
                   1335:                                noExec = true;
                   1336:                                gn->current = NULL;
                   1337:                        }
                   1338:                        if (noExec && !(job->flags & JOB_FIRST)) {
                   1339:                                /*
                   1340:                                 * If we're not going to execute anything, the
                   1341:                                 * job is done and we need to close down the
                   1342:                                 * various file descriptors we've opened for
                   1343:                                 * output, then call JobDoOutput to catch the
                   1344:                                 * final characters or send the file to the
                   1345:                                 * screen... Note that the i/o streams are only
                   1346:                                 * open if this isn't the first job.  Note also
                   1347:                                 * that this could not be done in
                   1348:                                 * Job_CatchChildren b/c it wasn't clear if
                   1349:                                 * there were more commands to execute or
                   1350:                                 * not...
                   1351:                                 */
                   1352:                                JobClose(job);
                   1353:                        }
                   1354:                } else {
                   1355:                        /*
                   1356:                         * We can do all the commands at once. hooray for
                   1357:                         * sanity
                   1358:                         */
                   1359:                        numCommands = 0;
1.68      espie    1360:                        Lst_ForEachNodeWhile(&gn->commands, JobPrintCommand,
1.66      espie    1361:                            job);
                   1362:
                   1363:                        /*
                   1364:                         * If we didn't print out any commands to the shell
                   1365:                         * script, there's not much point in executing the
                   1366:                         * shell, is there?
                   1367:                         */
                   1368:                        if (numCommands == 0) {
                   1369:                                noExec = true;
                   1370:                        }
                   1371:                }
                   1372:        } else if (noExecute) {
                   1373:                /*
                   1374:                 * Not executing anything -- just print all the commands to
                   1375:                 * stdout in one fell swoop. This will still set up
                   1376:                 * job->tailCmds correctly.
                   1377:                 */
                   1378:                if (lastNode != gn) {
                   1379:                        MESSAGE(stdout, gn);
                   1380:                        lastNode = gn;
                   1381:                }
                   1382:                job->cmdFILE = stdout;
                   1383:                /*
                   1384:                 * Only print the commands if they're ok, but don't die if
                   1385:                 * they're not -- just let the user know they're bad and keep
                   1386:                 * going. It doesn't do any harm in this case and may do some
                   1387:                 * good.
                   1388:                 */
                   1389:                if (cmdsOK) {
1.68      espie    1390:                        Lst_ForEachNodeWhile(&gn->commands, JobPrintCommand,
1.66      espie    1391:                            job);
                   1392:                }
                   1393:                /*
                   1394:                 * Don't execute the shell, thank you.
                   1395:                 */
1.41      espie    1396:                noExec = true;
1.66      espie    1397:        } else {
                   1398:                /*
                   1399:                 * Just touch the target and note that no shell should be
                   1400:                 * executed.  Set cmdFILE to stdout to make life easier. Check
                   1401:                 * the commands, too, but don't die if they're no good -- it
                   1402:                 * does no harm to keep working up the graph.
1.30      espie    1403:                 */
1.66      espie    1404:                job->cmdFILE = stdout;
                   1405:                Job_Touch(gn, job->flags&JOB_SILENT);
1.41      espie    1406:                noExec = true;
1.1       deraadt  1407:        }
1.66      espie    1408:
1.1       deraadt  1409:        /*
1.66      espie    1410:         * If we're not supposed to execute a shell, don't.
1.1       deraadt  1411:         */
1.66      espie    1412:        if (noExec) {
                   1413:                /*
                   1414:                 * Unlink and close the command file if we opened one
                   1415:                 */
                   1416:                if (job->cmdFILE != stdout) {
                   1417:                        (void)eunlink(tfile);
                   1418:                        if (job->cmdFILE != NULL)
                   1419:                                (void)fclose(job->cmdFILE);
                   1420:                } else {
                   1421:                         (void)fflush(stdout);
                   1422:                }
1.1       deraadt  1423:
1.66      espie    1424:                /*
                   1425:                 * We only want to work our way up the graph if we aren't here
                   1426:                 * because the commands for the job were no good.
                   1427:                 */
                   1428:                if (cmdsOK) {
                   1429:                        if (aborting == 0) {
1.68      espie    1430:                                Lst_ForEachFrom(job->tailCmds, JobSaveCommand,
1.66      espie    1431:                                    job->node);
                   1432:                                Make_Update(job->node);
                   1433:                        }
                   1434:                        free(job);
                   1435:                        return JOB_FINISHED;
                   1436:                } else {
                   1437:                        free(job);
                   1438:                        return JOB_ERROR;
                   1439:                }
1.1       deraadt  1440:        } else {
1.66      espie    1441:                (void)fflush(job->cmdFILE);
                   1442:                (void)eunlink(tfile);
1.1       deraadt  1443:        }
                   1444:
                   1445:        /*
1.66      espie    1446:         * Set up the control arguments to the shell. This is based on the flags
                   1447:         * set earlier for this job.
1.1       deraadt  1448:         */
1.66      espie    1449:        JobMakeArgv(job, argv);
1.1       deraadt  1450:
1.66      espie    1451:        /*
                   1452:         * If we're using pipes to catch output, create the pipe by which we'll
                   1453:         * get the shell's output. If we're using files, print out that we're
                   1454:         * starting a job and then set up its temporary-file name.
                   1455:         */
                   1456:        if (!compatMake || (job->flags & JOB_FIRST)) {
1.76      espie    1457:                int fd[2];
                   1458:                if (pipe(fd) == -1)
                   1459:                        Punt("Cannot create pipe: %s", strerror(errno));
                   1460:                job->inPipe = fd[0];
                   1461:                job->outPipe = fd[1];
                   1462:                (void)fcntl(job->inPipe, F_SETFD, 1);
                   1463:                (void)fcntl(job->outPipe, F_SETFD, 1);
1.1       deraadt  1464:        }
                   1465:
1.41      espie    1466:        local = true;
1.1       deraadt  1467:
1.78    ! espie    1468:        if (nJobs >= maxJobs && !(job->flags & JOB_SPECIAL)) {
        !          1469:                /* we've hit the limit of concurrency, so put the job on hold
        !          1470:                 * until some other job finishes.
1.66      espie    1471:                 */
                   1472:                jobFull = true;
1.6       millert  1473:
1.66      espie    1474:                if (DEBUG(JOB)) {
                   1475:                       (void)fprintf(stdout, "Can only run job locally.\n");
                   1476:                       (void)fflush(stdout);
                   1477:                }
                   1478:                job->flags |= JOB_RESTART;
                   1479:                Lst_AtEnd(&stoppedJobs, job);
1.1       deraadt  1480:        }
1.66      espie    1481:        return JOB_RUNNING;
1.1       deraadt  1482: }
                   1483:
1.6       millert  1484: static char *
1.56      espie    1485: JobOutput(Job *job, char *cp, char *endp, int msg)
1.2       deraadt  1486: {
1.66      espie    1487:        char *ecp;
1.2       deraadt  1488:
1.74      espie    1489:        ecp = strstr(cp, SHELL_ECHO_OFF);
                   1490:        while (ecp != NULL) {
                   1491:                if (cp != ecp) {
                   1492:                        *ecp = '\0';
                   1493:                        if (msg && job->node != lastNode) {
                   1494:                                MESSAGE(stdout, job->node);
                   1495:                                lastNode = job->node;
1.66      espie    1496:                        }
1.74      espie    1497:                        /*
1.78    ! espie    1498:                         * The only way there wouldn't be a newline after
        !          1499:                         * this line is if it were the last in the buffer.
        !          1500:                         * however, since the non-printable comes after it,
        !          1501:                         * there must be a newline, so we don't print one.
1.74      espie    1502:                         */
                   1503:                        (void)fprintf(stdout, "%s", cp);
                   1504:                        (void)fflush(stdout);
                   1505:                }
                   1506:                cp = ecp + strlen(SHELL_ECHO_OFF);
                   1507:                if (cp != endp) {
                   1508:                        /*
1.78    ! espie    1509:                         * Still more to print, look again after skipping
        !          1510:                         * the whitespace following the non-printable
        !          1511:                         * command....
1.74      espie    1512:                         */
                   1513:                        cp++;
1.78    ! espie    1514:                        while (*cp == ' ' || *cp == '\t' || *cp == '\n') {
1.66      espie    1515:                                cp++;
                   1516:                        }
1.74      espie    1517:                        ecp = strstr(cp, SHELL_ECHO_OFF);
                   1518:                } else {
                   1519:                        return cp;
1.2       deraadt  1520:                }
                   1521:        }
1.66      espie    1522:        return cp;
1.2       deraadt  1523: }
                   1524:
1.1       deraadt  1525: /*-
                   1526:  *-----------------------------------------------------------------------
1.40      espie    1527:  * JobDoOutput --
1.1       deraadt  1528:  *     This function is called at different times depending on
                   1529:  *     whether the user has specified that output is to be collected
                   1530:  *     via pipes or temporary files. In the former case, we are called
                   1531:  *     whenever there is something to read on the pipe. We collect more
                   1532:  *     output from the given job and store it in the job's outBuf. If
                   1533:  *     this makes up a line, we print it tagged by the job's identifier,
                   1534:  *     as necessary.
                   1535:  *     If output has been collected in a temporary file, we open the
                   1536:  *     file and read it line by line, transfering it to our own
                   1537:  *     output channel until the file is empty. At which point we
                   1538:  *     remove the temporary file.
                   1539:  *     In both cases, however, we keep our figurative eye out for the
                   1540:  *     'noPrint' line for the shell from which the output came. If
                   1541:  *     we recognize a line, we don't print it. If the command is not
1.6       millert  1542:  *     alone on the line (the character after it is not \0 or \n), we
1.1       deraadt  1543:  *     do print whatever follows it.
                   1544:  *
                   1545:  * Side Effects:
                   1546:  *     curPos may be shifted as may the contents of outBuf.
                   1547:  *-----------------------------------------------------------------------
                   1548:  */
1.48      espie    1549: static void
1.66      espie    1550: JobDoOutput(Job *job,          /* the job whose output needs printing */
                   1551:     bool finish)               /* true if this is the last time we'll be
                   1552:                                 * called for this job */
                   1553: {
1.78    ! espie    1554:        bool gotNL = false;     /* true if got a newline */
1.66      espie    1555:        bool fbuf;              /* true if our buffer filled up */
                   1556:        int nr;                 /* number of bytes read */
                   1557:        int i;                  /* auxiliary index into outBuf */
1.78    ! espie    1558:        int max;                /* limit for i (end of current data) */
        !          1559:        int nRead;              /* (Temporary) number of bytes read */
1.1       deraadt  1560:
1.76      espie    1561:        /*
                   1562:         * Read as many bytes as will fit in the buffer.
                   1563:         */
1.1       deraadt  1564: end_loop:
1.76      espie    1565:        gotNL = false;
                   1566:        fbuf = false;
1.6       millert  1567:
1.76      espie    1568:        nRead = read(job->inPipe, &job->outBuf[job->curPos],
                   1569:            JOB_BUFSIZE - job->curPos);
                   1570:        if (nRead == -1) {
                   1571:                if (DEBUG(JOB)) {
                   1572:                        perror("JobDoOutput(piperead)");
1.66      espie    1573:                }
1.76      espie    1574:                nr = 0;
                   1575:        } else {
                   1576:                nr = nRead;
                   1577:        }
1.1       deraadt  1578:
1.76      espie    1579:        /*
1.78    ! espie    1580:         * If we hit the end-of-file (the job is dead), we must flush its
        !          1581:         * remaining output, so pretend we read a newline if there's any
        !          1582:         * output remaining in the buffer.
        !          1583:         * Also clear the 'finish' flag so we stop looping.
1.76      espie    1584:         */
                   1585:        if (nr == 0 && job->curPos != 0) {
                   1586:                job->outBuf[job->curPos] = '\n';
                   1587:                nr = 1;
                   1588:                finish = false;
                   1589:        } else if (nr == 0) {
                   1590:                finish = false;
                   1591:        }
1.6       millert  1592:
1.76      espie    1593:        /*
1.78    ! espie    1594:         * Look for the last newline in the bytes we just got. If there is
        !          1595:         * one, break out of the loop with 'i' as its index and gotNL set
        !          1596:         * true.
1.76      espie    1597:         */
                   1598:        max = job->curPos + nr;
                   1599:        for (i = job->curPos + nr - 1; i >= job->curPos; i--) {
                   1600:                if (job->outBuf[i] == '\n') {
                   1601:                        gotNL = true;
                   1602:                        break;
                   1603:                } else if (job->outBuf[i] == '\0') {
                   1604:                        /*
                   1605:                         * Why?
                   1606:                         */
                   1607:                        job->outBuf[i] = ' ';
1.66      espie    1608:                }
1.76      espie    1609:        }
1.6       millert  1610:
1.76      espie    1611:        if (!gotNL) {
                   1612:                job->curPos += nr;
                   1613:                if (job->curPos == JOB_BUFSIZE) {
1.66      espie    1614:                        /*
1.78    ! espie    1615:                         * If we've run out of buffer space, we have no choice
        !          1616:                         * but to print the stuff. sigh.
1.66      espie    1617:                         */
1.76      espie    1618:                        fbuf = true;
                   1619:                        i = job->curPos;
                   1620:                }
                   1621:        }
                   1622:        if (gotNL || fbuf) {
                   1623:                /*
1.78    ! espie    1624:                 * Need to send the output to the screen. Null terminate it
        !          1625:                 * first, overwriting the newline character if there was one.
        !          1626:                 * So long as the line isn't one we should filter (according
        !          1627:                 * to the shell description), we print the line, preceded
        !          1628:                 * by a target banner if this target isn't the same as the
        !          1629:                 * one for which we last printed something.
        !          1630:                 * The rest of the data in the buffer are then shifted down
        !          1631:                 * to the start of the buffer and curPos is set accordingly.
1.76      espie    1632:                 */
                   1633:                job->outBuf[i] = '\0';
                   1634:                if (i >= job->curPos) {
                   1635:                        char *cp;
1.66      espie    1636:
1.78    ! espie    1637:                        cp = JobOutput(job, job->outBuf, &job->outBuf[i],
        !          1638:                            false);
1.66      espie    1639:
1.76      espie    1640:                        /*
1.78    ! espie    1641:                         * There's still more in that thar buffer. This time,
        !          1642:                         * though, we know there's no newline at the end, so we
        !          1643:                         * add one of our own free will.
1.76      espie    1644:                         */
                   1645:                        if (*cp != '\0') {
                   1646:                                if (job->node != lastNode) {
                   1647:                                        MESSAGE(stdout, job->node);
                   1648:                                        lastNode = job->node;
1.66      espie    1649:                                }
1.76      espie    1650:                                (void)fprintf(stdout, "%s%s", cp,
                   1651:                                    gotNL ? "\n" : "");
                   1652:                                (void)fflush(stdout);
1.66      espie    1653:                        }
1.76      espie    1654:                }
                   1655:                if (i < max - 1) {
                   1656:                        /* shift the remaining characters down */
                   1657:                        (void)memcpy(job->outBuf, &job->outBuf[i + 1],
                   1658:                            max - (i + 1));
                   1659:                        job->curPos = max - (i + 1);
1.66      espie    1660:
1.76      espie    1661:                } else {
1.66      espie    1662:                        /*
1.78    ! espie    1663:                         * We have written everything out, so we just start over
        !          1664:                         * from the start of the buffer. No copying. No nothing.
1.66      espie    1665:                         */
1.76      espie    1666:                        job->curPos = 0;
1.66      espie    1667:                }
1.76      espie    1668:        }
                   1669:        if (finish) {
1.66      espie    1670:                /*
1.78    ! espie    1671:                 * If the finish flag is true, we must loop until we hit
        !          1672:                 * end-of-file on the pipe. This is guaranteed to happen
        !          1673:                 * eventually since the other end of the pipe is now closed
        !          1674:                 * (we closed it explicitly and the child has exited). When
        !          1675:                 * we do get an EOF, finish will be set false and we'll fall
        !          1676:                 * through and out.
1.76      espie    1677:                 */
                   1678:                goto end_loop;
1.1       deraadt  1679:        }
                   1680: }
                   1681:
                   1682: /*-
                   1683:  *-----------------------------------------------------------------------
                   1684:  * Job_CatchChildren --
                   1685:  *     Handle the exit of a child. Called from Make_Make.
                   1686:  *
                   1687:  * Side Effects:
                   1688:  *     The job descriptor is removed from the list of children.
                   1689:  *
                   1690:  * Notes:
                   1691:  *     We do waits, blocking or not, according to the wisdom of our
                   1692:  *     caller, until there are no more children to report. For each
                   1693:  *     job, call JobFinish to finish things off. This will take care of
                   1694:  *     putting jobs on the stoppedJobs queue.
                   1695:  *-----------------------------------------------------------------------
                   1696:  */
                   1697: void
1.76      espie    1698: Job_CatchChildren()
1.1       deraadt  1699: {
1.78    ! espie    1700:        pid_t pid;      /* pid of dead child */
        !          1701:        Job *job;       /* job descriptor for dead child */
        !          1702:        LstNode jnode;  /* list element for finding job */
        !          1703:        int status;     /* Exit/termination status */
1.1       deraadt  1704:
1.66      espie    1705:        /*
                   1706:         * Don't even bother if we know there's no one around.
                   1707:         */
1.78    ! espie    1708:        if (nJobs == 0) {
1.66      espie    1709:                return;
1.2       deraadt  1710:        }
1.6       millert  1711:
1.76      espie    1712:        while ((pid = waitpid((pid_t) -1, &status, WNOHANG|WUNTRACED)) > 0) {
1.66      espie    1713:                HandleSigs();
                   1714:                if (DEBUG(JOB)) {
1.68      espie    1715:                        (void)fprintf(stdout,
1.66      espie    1716:                            "Process %ld exited or stopped.\n", (long)pid);
                   1717:                        (void)fflush(stdout);
                   1718:                }
                   1719:
1.1       deraadt  1720:
1.66      espie    1721:                jnode = Lst_Find(&jobs, JobCmpPid, &pid);
1.1       deraadt  1722:
1.18      espie    1723:                if (jnode == NULL) {
1.68      espie    1724:                        if (WIFSIGNALED(status) &&
1.66      espie    1725:                            (WTERMSIG(status) == SIGCONT)) {
                   1726:                                jnode = Lst_Find(&stoppedJobs, JobCmpPid, &pid);
                   1727:                                if (jnode == NULL) {
                   1728:                                        Error("Resumed child (%ld) not in table", (long)pid);
                   1729:                                        continue;
                   1730:                                }
                   1731:                                job = (Job *)Lst_Datum(jnode);
                   1732:                                Lst_Remove(&stoppedJobs, jnode);
                   1733:                        } else {
                   1734:                                Error("Child (%ld) not in table?", (long)pid);
                   1735:                                continue;
                   1736:                        }
                   1737:                } else {
                   1738:                        job = (Job *)Lst_Datum(jnode);
                   1739:                        Lst_Remove(&jobs, jnode);
1.67      espie    1740:                        nJobs--;
1.66      espie    1741:                        if (jobFull && DEBUG(JOB)) {
1.68      espie    1742:                                (void)fprintf(stdout,
1.66      espie    1743:                                    "Job queue is no longer full.\n");
                   1744:                                (void)fflush(stdout);
                   1745:                        }
                   1746:                        jobFull = false;
1.1       deraadt  1747:                }
1.66      espie    1748:
                   1749:                JobFinish(job, &status);
1.1       deraadt  1750:        }
                   1751: }
                   1752:
                   1753: /*-
                   1754:  *-----------------------------------------------------------------------
                   1755:  * Job_CatchOutput --
                   1756:  *     Catch the output from our children, if we're using
                   1757:  *     pipes do so. Otherwise just block time until we get a
1.6       millert  1758:  *     signal (most likely a SIGCHLD) since there's no point in
1.1       deraadt  1759:  *     just spinning when there's nothing to do and the reaping
1.6       millert  1760:  *     of a child can wait for a while.
1.1       deraadt  1761:  *
                   1762:  * Side Effects:
                   1763:  *     Output is read from pipes if we're piping.
                   1764:  * -----------------------------------------------------------------------
                   1765:  */
                   1766: void
1.56      espie    1767: Job_CatchOutput(void)
1.1       deraadt  1768: {
1.66      espie    1769:        int nfds;
                   1770:        struct timeval timeout;
                   1771:        LstNode ln;
                   1772:        Job *job;
                   1773:
1.76      espie    1774:        int count = howmany(outputsn+1, NFDBITS) * sizeof(fd_mask);
                   1775:        fd_set *readfdsp = malloc(count);
1.77      espie    1776:        (void)fflush(stdout);
1.76      espie    1777:        if (readfdsp == NULL)
                   1778:                return;
                   1779:
                   1780:        memcpy(readfdsp, outputsp, count);
                   1781:        timeout.tv_sec = SEL_SEC;
                   1782:        timeout.tv_usec = SEL_USEC;
1.66      espie    1783:
1.76      espie    1784:        if ((nfds = select(outputsn+1, readfdsp, (fd_set *) 0,
                   1785:            (fd_set *) 0, &timeout)) <= 0) {
                   1786:                HandleSigs();
                   1787:                free(readfdsp);
                   1788:                return;
                   1789:        } else {
                   1790:                HandleSigs();
                   1791:                for (ln = Lst_First(&jobs); nfds && ln != NULL;
                   1792:                    ln = Lst_Adv(ln)) {
                   1793:                        job = (Job *)Lst_Datum(ln);
                   1794:                        if (FD_ISSET(job->inPipe, readfdsp)) {
                   1795:                                JobDoOutput(job, false);
                   1796:                                nfds--;
1.66      espie    1797:                        }
1.1       deraadt  1798:                }
                   1799:        }
1.76      espie    1800:        free(readfdsp);
1.1       deraadt  1801: }
                   1802:
                   1803: /*-
                   1804:  *-----------------------------------------------------------------------
                   1805:  * Job_Make --
                   1806:  *     Start the creation of a target. Basically a front-end for
                   1807:  *     JobStart used by the Make module.
                   1808:  *
                   1809:  * Side Effects:
                   1810:  *     Another job is started.
                   1811:  *-----------------------------------------------------------------------
                   1812:  */
                   1813: void
1.56      espie    1814: Job_Make(GNode *gn)
1.1       deraadt  1815: {
1.66      espie    1816:        (void)JobStart(gn, 0, NULL);
1.1       deraadt  1817: }
                   1818:
                   1819: /*-
                   1820:  *-----------------------------------------------------------------------
                   1821:  * Job_Init --
                   1822:  *     Initialize the process module
                   1823:  *
                   1824:  * Side Effects:
                   1825:  *     lists and counters are initialized
                   1826:  *-----------------------------------------------------------------------
                   1827:  */
                   1828: void
1.78    ! espie    1829: Job_Init(int maxproc)
1.1       deraadt  1830: {
1.66      espie    1831:        int tfd;
                   1832:
                   1833:        (void)strlcpy(tfile, TMPPAT, sizeof(tfile));
                   1834:        if ((tfd = mkstemp(tfile)) == -1)
                   1835:                Punt("Cannot create temp file: %s", strerror(errno));
                   1836:        else
                   1837:                (void)close(tfd);
                   1838:
                   1839:        Static_Lst_Init(&jobs);
                   1840:        Static_Lst_Init(&stoppedJobs);
                   1841:        maxJobs =         maxproc;
                   1842:        jobFull =         false;
                   1843:
                   1844:        aborting =        0;
                   1845:        errors =          0;
1.40      espie    1846:
1.66      espie    1847:        lastNode =        NULL;
1.1       deraadt  1848:
1.66      espie    1849:        if (maxJobs == 1) {
                   1850:                /*
                   1851:                 * If only one job can run at a time, there's no need for a
                   1852:                 * banner, no is there?
                   1853:                 */
                   1854:                targFmt = "";
                   1855:        } else {
                   1856:                targFmt = TARG_FMT;
                   1857:        }
1.1       deraadt  1858:
                   1859:        /*
1.66      espie    1860:         * Catch the four signals that POSIX specifies if they aren't ignored.
                   1861:         * JobPassSig will take care of calling JobInterrupt if appropriate.
1.1       deraadt  1862:         */
1.66      espie    1863:        if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
                   1864:                (void)signal(SIGINT, SigHandler);
                   1865:        }
                   1866:        if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
                   1867:                (void)signal(SIGHUP, SigHandler);
                   1868:        }
                   1869:        if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
                   1870:                (void)signal(SIGQUIT, SigHandler);
                   1871:        }
                   1872:        if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
                   1873:                (void)signal(SIGTERM, SigHandler);
                   1874:        }
1.1       deraadt  1875:        /*
1.66      espie    1876:         * There are additional signals that need to be caught and passed if
                   1877:         * either the export system wants to be told directly of signals or if
                   1878:         * we're giving each job its own process group (since then it won't get
                   1879:         * signals from the terminal driver as we own the terminal)
1.1       deraadt  1880:         */
1.48      espie    1881: #if defined(USE_PGRP)
1.66      espie    1882:        if (signal(SIGTSTP, SIG_IGN) != SIG_IGN) {
                   1883:                (void)signal(SIGTSTP, SigHandler);
                   1884:        }
                   1885:        if (signal(SIGTTOU, SIG_IGN) != SIG_IGN) {
                   1886:                (void)signal(SIGTTOU, SigHandler);
                   1887:        }
                   1888:        if (signal(SIGTTIN, SIG_IGN) != SIG_IGN) {
                   1889:                (void)signal(SIGTTIN, SigHandler);
                   1890:        }
                   1891:        if (signal(SIGWINCH, SIG_IGN) != SIG_IGN) {
                   1892:                (void)signal(SIGWINCH, SigHandler);
                   1893:        }
1.1       deraadt  1894: #endif
1.6       millert  1895:
1.78    ! espie    1896:        if ((begin_node->type & OP_DUMMY) == 0) {
        !          1897:                JobStart(begin_node, JOB_SPECIAL, (Job *)0);
1.66      espie    1898:                while (nJobs) {
                   1899:                        Job_CatchOutput();
1.76      espie    1900:                        Job_CatchChildren();
1.66      espie    1901:                }
1.1       deraadt  1902:        }
                   1903: }
                   1904:
                   1905: /*-
                   1906:  *-----------------------------------------------------------------------
                   1907:  * Job_Full --
                   1908:  *     See if the job table is full. It is considered full if it is OR
                   1909:  *     if we are in the process of aborting OR if we have
                   1910:  *     reached/exceeded our local quota. This prevents any more jobs
                   1911:  *     from starting up.
                   1912:  *
                   1913:  * Results:
1.41      espie    1914:  *     true if the job table is full, false otherwise
1.1       deraadt  1915:  *-----------------------------------------------------------------------
                   1916:  */
1.41      espie    1917: bool
1.56      espie    1918: Job_Full(void)
1.1       deraadt  1919: {
1.66      espie    1920:        return aborting || jobFull;
1.1       deraadt  1921: }
                   1922:
                   1923: /*-
                   1924:  *-----------------------------------------------------------------------
                   1925:  * Job_Empty --
1.40      espie    1926:  *     See if the job table is empty.  Because the local concurrency may
1.1       deraadt  1927:  *     be set to 0, it is possible for the job table to become empty,
                   1928:  *     while the list of stoppedJobs remains non-empty. In such a case,
                   1929:  *     we want to restart as many jobs as we can.
                   1930:  *
                   1931:  * Results:
1.41      espie    1932:  *     true if it is. false if it ain't.
1.1       deraadt  1933:  * -----------------------------------------------------------------------
                   1934:  */
1.41      espie    1935: bool
1.56      espie    1936: Job_Empty(void)
1.1       deraadt  1937: {
1.66      espie    1938:        if (nJobs == 0) {
                   1939:                if (!Lst_IsEmpty(&stoppedJobs) && !aborting) {
                   1940:                        /*
                   1941:                         * The job table is obviously not full if it has no
                   1942:                         * jobs in it...Try and restart the stopped jobs.
                   1943:                         */
                   1944:                        jobFull = false;
                   1945:                        JobRestartJobs();
                   1946:                        return false;
                   1947:                } else {
                   1948:                        return true;
                   1949:                }
1.1       deraadt  1950:        } else {
1.66      espie    1951:                return false;
1.1       deraadt  1952:        }
                   1953: }
                   1954:
                   1955: /*-
                   1956:  *-----------------------------------------------------------------------
                   1957:  * JobInterrupt --
                   1958:  *     Handle the receipt of an interrupt.
                   1959:  *
                   1960:  * Side Effects:
                   1961:  *     All children are killed. Another job will be started if the
                   1962:  *     .INTERRUPT target was given.
                   1963:  *-----------------------------------------------------------------------
                   1964:  */
                   1965: static void
1.56      espie    1966: JobInterrupt(int runINTERRUPT, /* Non-zero if commands for the .INTERRUPT
1.1       deraadt  1967:                                 * target should be executed */
1.66      espie    1968:     int signo)                 /* signal received */
1.1       deraadt  1969: {
1.66      espie    1970:        LstNode ln;             /* element in job table */
1.78    ! espie    1971:        Job *job;               /* job descriptor in that element */
1.66      espie    1972:
                   1973:        aborting = ABORT_INTERRUPT;
                   1974:
                   1975:        for (ln = Lst_First(&jobs); ln != NULL; ln = Lst_Adv(ln)) {
                   1976:                job = (Job *)Lst_Datum(ln);
                   1977:
                   1978:                if (!Targ_Precious(job->node)) {
                   1979:                        const char *file = job->node->path == NULL ?
                   1980:                            job->node->name : job->node->path;
                   1981:                        if (!noExecute && eunlink(file) != -1) {
                   1982:                                Error("*** %s removed", file);
                   1983:                        }
                   1984:                }
                   1985:                if (job->pid) {
                   1986:                        if (DEBUG(JOB)) {
                   1987:                                (void)fprintf(stdout,
                   1988:                                    "JobInterrupt passing signal to child %ld.\n",
                   1989:                                    (long)job->pid);
                   1990:                                (void)fflush(stdout);
                   1991:                        }
                   1992:                        KILL(job->pid, signo);
                   1993:                }
1.2       deraadt  1994:        }
1.1       deraadt  1995:
1.66      espie    1996:        if (runINTERRUPT && !touchFlag) {
1.78    ! espie    1997:                if ((interrupt_node->type & OP_DUMMY) == 0) {
1.66      espie    1998:                        ignoreErrors = false;
                   1999:
1.78    ! espie    2000:                        JobStart(interrupt_node, JOB_IGNDOTS, (Job *)0);
1.66      espie    2001:                        while (nJobs) {
                   2002:                                Job_CatchOutput();
1.76      espie    2003:                                Job_CatchChildren();
1.66      espie    2004:                        }
                   2005:                }
1.1       deraadt  2006:        }
1.66      espie    2007:        (void)eunlink(tfile);
                   2008:        exit(signo);
1.1       deraadt  2009: }
                   2010:
                   2011: /*
                   2012:  *-----------------------------------------------------------------------
1.12      espie    2013:  * Job_Finish --
1.1       deraadt  2014:  *     Do final processing such as the running of the commands
1.6       millert  2015:  *     attached to the .END target.
1.1       deraadt  2016:  *
                   2017:  * Results:
                   2018:  *     Number of errors reported.
1.40      espie    2019:  *
                   2020:  * Side Effects:
                   2021:  *     The process' temporary file (tfile) is removed if it still
                   2022:  *     existed.
1.1       deraadt  2023:  *-----------------------------------------------------------------------
                   2024:  */
                   2025: int
1.56      espie    2026: Job_Finish(void)
1.1       deraadt  2027: {
1.78    ! espie    2028:        if (end_node != NULL && !Lst_IsEmpty(&end_node->commands)) {
1.66      espie    2029:                if (errors) {
                   2030:                        Error("Errors reported so .END ignored");
                   2031:                } else {
1.78    ! espie    2032:                        JobStart(end_node, JOB_SPECIAL | JOB_IGNDOTS, NULL);
1.1       deraadt  2033:
1.66      espie    2034:                        while (nJobs) {
                   2035:                                Job_CatchOutput();
1.76      espie    2036:                                Job_CatchChildren();
1.66      espie    2037:                        }
                   2038:                }
1.1       deraadt  2039:        }
1.66      espie    2040:        (void)eunlink(tfile);
                   2041:        return errors;
1.1       deraadt  2042: }
                   2043:
1.41      espie    2044: #ifdef CLEANUP
1.12      espie    2045: void
1.56      espie    2046: Job_End(void)
1.12      espie    2047: {
1.41      espie    2048: }
1.13      espie    2049: #endif
1.40      espie    2050:
1.1       deraadt  2051: /*-
                   2052:  *-----------------------------------------------------------------------
                   2053:  * Job_Wait --
                   2054:  *     Waits for all running jobs to finish and returns. Sets 'aborting'
                   2055:  *     to ABORT_WAIT to prevent other jobs from starting.
                   2056:  *
                   2057:  * Side Effects:
                   2058:  *     Currently running jobs finish.
                   2059:  *
                   2060:  *-----------------------------------------------------------------------
                   2061:  */
                   2062: void
1.56      espie    2063: Job_Wait(void)
1.1       deraadt  2064: {
1.66      espie    2065:        aborting = ABORT_WAIT;
                   2066:        while (nJobs != 0) {
                   2067:                Job_CatchOutput();
1.76      espie    2068:                Job_CatchChildren();
1.66      espie    2069:        }
                   2070:        aborting = 0;
1.1       deraadt  2071: }
                   2072:
                   2073: /*-
                   2074:  *-----------------------------------------------------------------------
                   2075:  * Job_AbortAll --
                   2076:  *     Abort all currently running jobs without handling output or anything.
                   2077:  *     This function is to be called only in the event of a major
                   2078:  *     error. Most definitely NOT to be called from JobInterrupt.
                   2079:  *
                   2080:  * Side Effects:
                   2081:  *     All children are killed, not just the firstborn
                   2082:  *-----------------------------------------------------------------------
                   2083:  */
                   2084: void
1.56      espie    2085: Job_AbortAll(void)
1.1       deraadt  2086: {
1.66      espie    2087:        LstNode ln;     /* element in job table */
                   2088:        Job *job;       /* the job descriptor in that element */
                   2089:        int foo;
1.6       millert  2090:
1.66      espie    2091:        aborting = ABORT_ERROR;
1.6       millert  2092:
1.66      espie    2093:        if (nJobs) {
                   2094:                for (ln = Lst_First(&jobs); ln != NULL; ln = Lst_Adv(ln)) {
                   2095:                        job = (Job *)Lst_Datum(ln);
                   2096:
                   2097:                        /*
                   2098:                         * kill the child process with increasingly drastic
                   2099:                         * signals to make darn sure it's dead.
                   2100:                         */
                   2101:                        KILL(job->pid, SIGINT);
                   2102:                        KILL(job->pid, SIGKILL);
                   2103:                }
1.1       deraadt  2104:        }
1.6       millert  2105:
1.66      espie    2106:        /*
                   2107:         * Catch as many children as want to report in at first, then give up
                   2108:         */
                   2109:        while (waitpid(-1, &foo, WNOHANG) > 0)
                   2110:                continue;
                   2111:        (void)eunlink(tfile);
1.2       deraadt  2112: }
1.40      espie    2113:
1.2       deraadt  2114: /*-
                   2115:  *-----------------------------------------------------------------------
                   2116:  * JobRestartJobs --
                   2117:  *     Tries to restart stopped jobs if there are slots available.
                   2118:  *     Note that this tries to restart them regardless of pending errors.
                   2119:  *     It's not good to leave stopped jobs lying around!
                   2120:  *
                   2121:  * Side Effects:
                   2122:  *     Resumes(and possibly migrates) jobs.
                   2123:  *-----------------------------------------------------------------------
                   2124:  */
                   2125: static void
1.56      espie    2126: JobRestartJobs(void)
1.2       deraadt  2127: {
1.66      espie    2128:        Job *job;
1.19      espie    2129:
1.66      espie    2130:        while (!jobFull && (job = (Job *)Lst_DeQueue(&stoppedJobs)) != NULL) {
                   2131:                if (DEBUG(JOB)) {
                   2132:                        (void)fprintf(stdout,
                   2133:                            "Job queue is not full. Restarting a stopped job.\n");
                   2134:                        (void)fflush(stdout);
                   2135:                }
                   2136:                JobRestart(job);
1.2       deraadt  2137:        }
1.1       deraadt  2138: }