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

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