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

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