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

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