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

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