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

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