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

1.7     ! millert     1: /*     $OpenBSD: job.c,v 1.6 1996/11/30 21:08:56 millert Exp $ */
1.6       millert     2: /*     $NetBSD: job.c,v 1.16 1996/11/06 17:59:08 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
1.6       millert    44: static char sccsid[] = "@(#)job.c      8.2 (Berkeley) 3/19/94";
1.1       deraadt    45: #else
1.7     ! millert    46: static char rcsid[] = "$OpenBSD: job.c,v 1.6 1996/11/30 21:08:56 millert 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 <stdio.h>
                    115: #include <string.h>
                    116: #include <signal.h>
                    117: #include "make.h"
                    118: #include "hash.h"
                    119: #include "dir.h"
                    120: #include "job.h"
                    121: #include "pathnames.h"
1.2       deraadt   122: #ifdef REMOTE
                    123: #include "rmt.h"
1.6       millert   124: # define STATIC
1.2       deraadt   125: #else
                    126: # define STATIC static
                    127: #endif
1.1       deraadt   128:
                    129: extern int  errno;
                    130:
                    131: /*
1.6       millert   132:  * error handling variables
1.1       deraadt   133:  */
                    134: static int             errors = 0;         /* number of errors reported */
                    135: static int     aborting = 0;       /* why is the make aborting? */
                    136: #define ABORT_ERROR    1           /* Because of an error */
                    137: #define ABORT_INTERRUPT        2           /* Because it was interrupted */
                    138: #define ABORT_WAIT     3           /* Waiting for jobs to finish */
                    139:
1.6       millert   140: /*
1.2       deraadt   141:  * XXX: Avoid SunOS bug... FILENO() is fp->_file, and file
                    142:  * is a char! So when we go above 127 we turn negative!
                    143:  */
                    144: #define FILENO(a) ((unsigned) fileno(a))
1.1       deraadt   145:
                    146: /*
                    147:  * post-make command processing. The node postCommands is really just the
                    148:  * .END target but we keep it around to avoid having to search for it
                    149:  * all the time.
                    150:  */
                    151: static GNode             *postCommands;    /* node containing commands to execute when
                    152:                                     * everything else is done */
                    153: static int               numCommands;      /* The number of commands actually printed
                    154:                                     * for a target. Should this number be
                    155:                                     * 0, no shell will be executed. */
                    156:
                    157: /*
                    158:  * Return values from JobStart.
                    159:  */
                    160: #define JOB_RUNNING    0       /* Job is running */
                    161: #define JOB_ERROR      1       /* Error in starting the job */
                    162: #define JOB_FINISHED   2       /* The job is already finished */
                    163: #define JOB_STOPPED    3       /* The job is stopped */
                    164:
                    165: /*
                    166:  * tfile is the name of a file into which all shell commands are put. It is
1.7     ! millert   167:  * used over by removing it before the child shell is executed. The XXXXXXXXXX
        !           168:  * in the string are replaced by mkstemp(3).
1.1       deraadt   169:  */
1.7     ! millert   170: static char     tfile[sizeof(TMPPAT)];
1.1       deraadt   171:
                    172:
                    173: /*
                    174:  * Descriptions for various shells.
                    175:  */
                    176: static Shell    shells[] = {
                    177:     /*
                    178:      * CSH description. The csh can do echo control by playing
                    179:      * with the setting of the 'echo' shell variable. Sadly,
                    180:      * however, it is unable to do error control nicely.
                    181:      */
                    182: {
                    183:     "csh",
                    184:     TRUE, "unset verbose", "set verbose", "unset verbose", 10,
                    185:     FALSE, "echo \"%s\"\n", "csh -c \"%s || exit 0\"",
                    186:     "v", "e",
                    187: },
                    188:     /*
                    189:      * SH description. Echo control is also possible and, under
                    190:      * sun UNIX anyway, one can even control error checking.
                    191:      */
                    192: {
                    193:     "sh",
                    194:     TRUE, "set -", "set -v", "set -", 5,
1.2       deraadt   195:     TRUE, "set -e", "set +e",
                    196: #ifdef OLDBOURNESHELL
1.1       deraadt   197:     FALSE, "echo \"%s\"\n", "sh -c '%s || exit 0'\n",
1.2       deraadt   198: #endif
1.1       deraadt   199:     "v", "e",
                    200: },
                    201:     /*
                    202:      * UNKNOWN.
                    203:      */
                    204: {
1.2       deraadt   205:     (char *) 0,
                    206:     FALSE, (char *) 0, (char *) 0, (char *) 0, 0,
                    207:     FALSE, (char *) 0, (char *) 0,
                    208:     (char *) 0, (char *) 0,
1.1       deraadt   209: }
                    210: };
                    211: static Shell   *commandShell = &shells[DEFSHELL];/* this is the shell to
                    212:                                                   * which we pass all
                    213:                                                   * commands in the Makefile.
                    214:                                                   * It is set by the
                    215:                                                   * Job_ParseShell function */
1.2       deraadt   216: static char    *shellPath = NULL,                /* full pathname of
1.1       deraadt   217:                                                   * executable image */
                    218:                        *shellName;                       /* last component of shell */
                    219:
                    220:
                    221: static int     maxJobs;        /* The most children we can run at once */
                    222: static int     maxLocal;       /* The most local ones we can have */
1.2       deraadt   223: STATIC int             nJobs;          /* The number of children currently running */
                    224: STATIC int     nLocal;         /* The number of local children */
                    225: STATIC Lst             jobs;           /* The structures that describe them */
                    226: STATIC Boolean jobFull;        /* Flag to tell when the job table is full. It
1.1       deraadt   227:                                 * is set TRUE when (1) the total number of
                    228:                                 * running jobs equals the maximum allowed or
                    229:                                 * (2) a job can only be run locally, but
                    230:                                 * nLocal equals maxLocal */
                    231: #ifndef RMT_WILL_WATCH
                    232: static fd_set          outputs;        /* Set of descriptors of pipes connected to
                    233:                                 * the output channels of children */
                    234: #endif
                    235:
1.2       deraadt   236: STATIC GNode           *lastNode;      /* The node for which output was most recently
1.1       deraadt   237:                                 * produced. */
1.2       deraadt   238: STATIC char            *targFmt;       /* Format string to use to head output from a
1.1       deraadt   239:                                 * job when it's not the most-recent job heard
                    240:                                 * from */
1.2       deraadt   241:
                    242: #ifdef REMOTE
                    243: # define TARG_FMT  "--- %s at %s ---\n" /* Default format */
                    244: # define MESSAGE(fp, gn) \
                    245:        (void) fprintf(fp, targFmt, gn->name, gn->rem.hname);
                    246: #else
                    247: # define TARG_FMT  "--- %s ---\n" /* Default format */
                    248: # define MESSAGE(fp, gn) \
                    249:        (void) fprintf(fp, targFmt, gn->name);
                    250: #endif
1.1       deraadt   251:
                    252: /*
                    253:  * When JobStart attempts to run a job remotely but can't, and isn't allowed
                    254:  * to run the job locally, or when Job_CatchChildren detects a job that has
                    255:  * been migrated home, the job is placed on the stoppedJobs queue to be run
1.6       millert   256:  * when the next job finishes.
1.1       deraadt   257:  */
1.2       deraadt   258: STATIC Lst     stoppedJobs;    /* Lst of Job structures describing
1.1       deraadt   259:                                 * jobs that were stopped due to concurrency
                    260:                                 * limits or migration home */
                    261:
                    262:
                    263: #if defined(USE_PGRP) && defined(SYSV)
1.2       deraadt   264: # define KILL(pid, sig)                killpg(-(pid), (sig))
1.1       deraadt   265: #else
                    266: # if defined(USE_PGRP)
1.2       deraadt   267: #  define KILL(pid, sig)       killpg((pid), (sig))
1.1       deraadt   268: # else
1.2       deraadt   269: #  define KILL(pid, sig)       kill((pid), (sig))
1.1       deraadt   270: # endif
                    271: #endif
                    272:
1.6       millert   273: /*
1.2       deraadt   274:  * Grmpf... There is no way to set bits of the wait structure
                    275:  * anymore with the stupid W*() macros. I liked the union wait
                    276:  * stuff much more. So, we devise our own macros... This is
                    277:  * really ugly, use dramamine sparingly. You have been warned.
                    278:  */
                    279: #define W_SETMASKED(st, val, fun)                              \
                    280:        {                                                       \
                    281:                int sh = (int) ~0;                              \
                    282:                int mask = fun(sh);                             \
                    283:                                                                \
                    284:                for (sh = 0; ((mask >> sh) & 1) == 0; sh++)     \
                    285:                        continue;                               \
                    286:                *(st) = (*(st) & ~mask) | ((val) << sh);        \
                    287:        }
                    288:
                    289: #define W_SETTERMSIG(st, val) W_SETMASKED(st, val, WTERMSIG)
                    290: #define W_SETEXITSTATUS(st, val) W_SETMASKED(st, val, WEXITSTATUS)
                    291:
                    292:
1.1       deraadt   293: static int JobCondPassSig __P((ClientData, ClientData));
                    294: static void JobPassSig __P((int));
                    295: static int JobCmpPid __P((ClientData, ClientData));
                    296: static int JobPrintCommand __P((ClientData, ClientData));
                    297: static int JobSaveCommand __P((ClientData, ClientData));
1.2       deraadt   298: static void JobClose __P((Job *));
                    299: #ifdef REMOTE
                    300: static int JobCmpRmtID __P((Job *, int));
                    301: # ifdef RMT_WILL_WATCH
                    302: static void JobLocalInput __P((int, Job *));
                    303: # endif
                    304: #else
                    305: static void JobFinish __P((Job *, int *));
1.1       deraadt   306: static void JobExec __P((Job *, char **));
1.2       deraadt   307: #endif
1.1       deraadt   308: static void JobMakeArgv __P((Job *, char **));
                    309: static void JobRestart __P((Job *));
                    310: static int JobStart __P((GNode *, int, Job *));
1.6       millert   311: static char *JobOutput __P((Job *, char *, char *, int));
1.1       deraadt   312: static void JobDoOutput __P((Job *, Boolean));
                    313: static Shell *JobMatchShell __P((char *));
1.2       deraadt   314: static void JobInterrupt __P((int, int));
                    315: static void JobRestartJobs __P((void));
1.1       deraadt   316:
                    317: /*-
                    318:  *-----------------------------------------------------------------------
                    319:  * JobCondPassSig --
                    320:  *     Pass a signal to a job if the job is remote or if USE_PGRP
                    321:  *     is defined.
                    322:  *
                    323:  * Results:
                    324:  *     === 0
                    325:  *
                    326:  * Side Effects:
                    327:  *     None, except the job may bite it.
                    328:  *
                    329:  *-----------------------------------------------------------------------
                    330:  */
                    331: static int
                    332: JobCondPassSig(jobp, signop)
                    333:     ClientData         jobp;       /* Job to biff */
                    334:     ClientData         signop;     /* Signal to send it */
                    335: {
                    336:     Job        *job = (Job *) jobp;
                    337:     int        signo = *(int *) signop;
                    338: #ifdef RMT_WANTS_SIGNALS
                    339:     if (job->flags & JOB_REMOTE) {
1.2       deraadt   340:        (void) Rmt_Signal(job, signo);
1.1       deraadt   341:     } else {
                    342:        KILL(job->pid, signo);
                    343:     }
                    344: #else
                    345:     /*
                    346:      * Assume that sending the signal to job->pid will signal any remote
                    347:      * job as well.
                    348:      */
1.2       deraadt   349:     if (DEBUG(JOB)) {
1.6       millert   350:        (void) fprintf(stdout,
1.2       deraadt   351:                       "JobCondPassSig passing signal %d to child %d.\n",
                    352:                       signo, job->pid);
                    353:        (void) fflush(stdout);
                    354:     }
1.1       deraadt   355:     KILL(job->pid, signo);
                    356: #endif
1.2       deraadt   357:     return 0;
1.1       deraadt   358: }
                    359:
                    360: /*-
                    361:  *-----------------------------------------------------------------------
                    362:  * JobPassSig --
                    363:  *     Pass a signal on to all remote jobs and to all local jobs if
                    364:  *     USE_PGRP is defined, then die ourselves.
                    365:  *
                    366:  * Results:
                    367:  *     None.
                    368:  *
                    369:  * Side Effects:
                    370:  *     We die by the same signal.
1.6       millert   371:  *
1.1       deraadt   372:  *-----------------------------------------------------------------------
                    373:  */
                    374: static void
                    375: JobPassSig(signo)
                    376:     int            signo;      /* The signal number we've received */
                    377: {
1.2       deraadt   378:     sigset_t nmask, omask;
1.3       niklas    379:     struct sigaction act;
1.6       millert   380:
1.2       deraadt   381:     if (DEBUG(JOB)) {
                    382:        (void) fprintf(stdout, "JobPassSig(%d) called.\n", signo);
                    383:        (void) fflush(stdout);
                    384:     }
                    385:     Lst_ForEach(jobs, JobCondPassSig, (ClientData) &signo);
1.1       deraadt   386:
                    387:     /*
                    388:      * Deal with proper cleanup based on the signal received. We only run
                    389:      * the .INTERRUPT target if the signal was in fact an interrupt. The other
                    390:      * three termination signals are more of a "get out *now*" command.
                    391:      */
                    392:     if (signo == SIGINT) {
1.2       deraadt   393:        JobInterrupt(TRUE, signo);
1.1       deraadt   394:     } else if ((signo == SIGHUP) || (signo == SIGTERM) || (signo == SIGQUIT)) {
1.2       deraadt   395:        JobInterrupt(FALSE, signo);
1.1       deraadt   396:     }
1.6       millert   397:
1.1       deraadt   398:     /*
                    399:      * Leave gracefully if SIGQUIT, rather than core dumping.
                    400:      */
                    401:     if (signo == SIGQUIT) {
                    402:        Finish(0);
                    403:     }
1.6       millert   404:
1.1       deraadt   405:     /*
                    406:      * Send ourselves the signal now we've given the message to everyone else.
                    407:      * Note we block everything else possible while we're getting the signal.
                    408:      * This ensures that all our jobs get continued when we wake up before
                    409:      * we take any other signal.
                    410:      */
1.3       niklas    411:     sigemptyset(&nmask);
                    412:     sigaddset(&nmask, signo);
                    413:     sigprocmask(SIG_SETMASK, &nmask, &omask);
                    414:     act.sa_handler = SIG_DFL;
                    415:     sigemptyset(&act.sa_mask);
                    416:     act.sa_flags = 0;
                    417:     sigaction(signo, &act, NULL);
1.2       deraadt   418:
                    419:     if (DEBUG(JOB)) {
                    420:        (void) fprintf(stdout,
                    421:                       "JobPassSig passing signal to self, mask = %x.\n",
                    422:                       ~0 & ~(1 << (signo-1)));
                    423:        (void) fflush(stdout);
                    424:     }
                    425:     (void) signal(signo, SIG_DFL);
1.1       deraadt   426:
1.2       deraadt   427:     (void) KILL(getpid(), signo);
1.1       deraadt   428:
                    429:     signo = SIGCONT;
                    430:     Lst_ForEach(jobs, JobCondPassSig, (ClientData) &signo);
                    431:
1.2       deraadt   432:     (void) sigprocmask(SIG_SETMASK, &omask, NULL);
1.3       niklas    433:     sigprocmask(SIG_SETMASK, &omask, NULL);
                    434:     act.sa_handler = JobPassSig;
                    435:     sigaction(signo, &act, NULL);
1.1       deraadt   436: }
                    437:
                    438: /*-
                    439:  *-----------------------------------------------------------------------
                    440:  * JobCmpPid  --
                    441:  *     Compare the pid of the job with the given pid and return 0 if they
                    442:  *     are equal. This function is called from Job_CatchChildren via
                    443:  *     Lst_Find to find the job descriptor of the finished job.
                    444:  *
                    445:  * Results:
                    446:  *     0 if the pid's match
                    447:  *
                    448:  * Side Effects:
                    449:  *     None
                    450:  *-----------------------------------------------------------------------
                    451:  */
                    452: static int
1.2       deraadt   453: JobCmpPid(job, pid)
1.1       deraadt   454:     ClientData        job;     /* job to examine */
                    455:     ClientData        pid;     /* process id desired */
                    456: {
1.6       millert   457:     return *(int *) pid - ((Job *) job)->pid;
1.1       deraadt   458: }
                    459:
1.2       deraadt   460: #ifdef REMOTE
                    461: /*-
                    462:  *-----------------------------------------------------------------------
                    463:  * JobCmpRmtID  --
                    464:  *     Compare the rmtID of the job with the given rmtID and return 0 if they
1.6       millert   465:  *     are equal.
1.2       deraadt   466:  *
                    467:  * Results:
                    468:  *     0 if the rmtID's match
                    469:  *
                    470:  * Side Effects:
                    471:  *     None.
                    472:  *-----------------------------------------------------------------------
                    473:  */
                    474: static int
                    475: JobCmpRmtID(job, rmtID)
                    476:     ClientData      job;       /* job to examine */
                    477:     ClientData      rmtID;     /* remote id desired */
                    478: {
                    479:     return(*(int *) rmtID - *(int *) job->rmtID);
                    480: }
                    481: #endif
                    482:
1.1       deraadt   483: /*-
                    484:  *-----------------------------------------------------------------------
                    485:  * JobPrintCommand  --
                    486:  *     Put out another command for the given job. If the command starts
                    487:  *     with an @ or a - we process it specially. In the former case,
                    488:  *     so long as the -s and -n flags weren't given to make, we stick
                    489:  *     a shell-specific echoOff command in the script. In the latter,
                    490:  *     we ignore errors for the entire job, unless the shell has error
                    491:  *     control.
                    492:  *     If the command is just "..." we take all future commands for this
                    493:  *     job to be commands to be executed once the entire graph has been
                    494:  *     made and return non-zero to signal that the end of the commands
                    495:  *     was reached. These commands are later attached to the postCommands
                    496:  *     node and executed by Job_End when all things are done.
                    497:  *     This function is called from JobStart via Lst_ForEach.
                    498:  *
                    499:  * Results:
                    500:  *     Always 0, unless the command was "..."
                    501:  *
                    502:  * Side Effects:
                    503:  *     If the command begins with a '-' and the shell has no error control,
                    504:  *     the JOB_IGNERR flag is set in the job descriptor.
                    505:  *     If the command is "..." and we're not ignoring such things,
                    506:  *     tailCmds is set to the successor node of the cmd.
                    507:  *     numCommands is incremented if the command is actually printed.
                    508:  *-----------------------------------------------------------------------
                    509:  */
                    510: static int
1.2       deraadt   511: JobPrintCommand(cmdp, jobp)
1.1       deraadt   512:     ClientData    cmdp;                    /* command string to print */
                    513:     ClientData    jobp;                    /* job for which to print it */
                    514: {
                    515:     Boolean      noSpecials;       /* true if we shouldn't worry about
                    516:                                     * inserting special commands into
                    517:                                     * the input stream. */
                    518:     Boolean       shutUp = FALSE;   /* true if we put a no echo command
                    519:                                     * into the command file */
                    520:     Boolean      errOff = FALSE;   /* true if we turned error checking
                    521:                                     * off before printing the command
                    522:                                     * and need to turn it back on */
                    523:     char                 *cmdTemplate;     /* Template to use when printing the
                    524:                                     * command */
                    525:     char         *cmdStart;        /* Start of expanded command */
                    526:     LstNode      cmdNode;          /* Node for replacing the command */
                    527:     char         *cmd = (char *) cmdp;
1.6       millert   528:     Job           *job = (Job *) jobp;
1.1       deraadt   529:
1.2       deraadt   530:     noSpecials = (noExecute && !(job->node->type & OP_MAKE));
1.1       deraadt   531:
1.2       deraadt   532:     if (strcmp(cmd, "...") == 0) {
1.6       millert   533:        job->node->type |= OP_SAVE_CMDS;
1.1       deraadt   534:        if ((job->flags & JOB_IGNDOTS) == 0) {
1.2       deraadt   535:            job->tailCmds = Lst_Succ(Lst_Member(job->node->commands,
                    536:                                                (ClientData)cmd));
1.6       millert   537:            return 1;
1.2       deraadt   538:        }
1.6       millert   539:        return 0;
1.1       deraadt   540:     }
                    541:
1.2       deraadt   542: #define DBPRINTF(fmt, arg) if (DEBUG(JOB)) {   \
                    543:        (void) fprintf(stdout, fmt, arg);       \
                    544:        (void) fflush(stdout);                  \
                    545:     }                                          \
                    546:    (void) fprintf(job->cmdFILE, fmt, arg);     \
                    547:    (void) fflush(job->cmdFILE);
1.1       deraadt   548:
                    549:     numCommands += 1;
                    550:
                    551:     /*
                    552:      * For debugging, we replace each command with the result of expanding
                    553:      * the variables in the command.
                    554:      */
1.2       deraadt   555:     cmdNode = Lst_Member(job->node->commands, (ClientData)cmd);
                    556:     cmdStart = cmd = Var_Subst(NULL, cmd, job->node, FALSE);
                    557:     Lst_Replace(cmdNode, (ClientData)cmdStart);
1.1       deraadt   558:
                    559:     cmdTemplate = "%s\n";
                    560:
                    561:     /*
                    562:      * Check for leading @' and -'s to control echoing and error checking.
                    563:      */
                    564:     while (*cmd == '@' || *cmd == '-') {
                    565:        if (*cmd == '@') {
                    566:            shutUp = TRUE;
                    567:        } else {
                    568:            errOff = TRUE;
                    569:        }
                    570:        cmd++;
                    571:     }
                    572:
                    573:     while (isspace((unsigned char) *cmd))
                    574:        cmd++;
                    575:
                    576:     if (shutUp) {
1.2       deraadt   577:        if (!(job->flags & JOB_SILENT) && !noSpecials &&
1.1       deraadt   578:            commandShell->hasEchoCtl) {
1.2       deraadt   579:                DBPRINTF("%s\n", commandShell->echoOff);
1.1       deraadt   580:        } else {
                    581:            shutUp = FALSE;
                    582:        }
                    583:     }
                    584:
                    585:     if (errOff) {
1.2       deraadt   586:        if ( !(job->flags & JOB_IGNERR) && !noSpecials) {
1.1       deraadt   587:            if (commandShell->hasErrCtl) {
                    588:                /*
                    589:                 * we don't want the error-control commands showing
                    590:                 * up either, so we turn off echoing while executing
                    591:                 * them. We could put another field in the shell
                    592:                 * structure to tell JobDoOutput to look for this
                    593:                 * string too, but why make it any more complex than
                    594:                 * it already is?
                    595:                 */
1.2       deraadt   596:                if (!(job->flags & JOB_SILENT) && !shutUp &&
1.1       deraadt   597:                    commandShell->hasEchoCtl) {
1.2       deraadt   598:                        DBPRINTF("%s\n", commandShell->echoOff);
                    599:                        DBPRINTF("%s\n", commandShell->ignErr);
                    600:                        DBPRINTF("%s\n", commandShell->echoOn);
1.1       deraadt   601:                } else {
1.2       deraadt   602:                    DBPRINTF("%s\n", commandShell->ignErr);
1.1       deraadt   603:                }
                    604:            } else if (commandShell->ignErr &&
1.2       deraadt   605:                      (*commandShell->ignErr != '\0'))
1.1       deraadt   606:            {
                    607:                /*
                    608:                 * The shell has no error control, so we need to be
                    609:                 * weird to get it to ignore any errors from the command.
                    610:                 * If echoing is turned on, we turn it off and use the
                    611:                 * errCheck template to echo the command. Leave echoing
                    612:                 * off so the user doesn't see the weirdness we go through
                    613:                 * to ignore errors. Set cmdTemplate to use the weirdness
                    614:                 * instead of the simple "%s\n" template.
                    615:                 */
1.2       deraadt   616:                if (!(job->flags & JOB_SILENT) && !shutUp &&
1.1       deraadt   617:                    commandShell->hasEchoCtl) {
1.2       deraadt   618:                        DBPRINTF("%s\n", commandShell->echoOff);
                    619:                        DBPRINTF(commandShell->errCheck, cmd);
1.1       deraadt   620:                        shutUp = TRUE;
                    621:                }
                    622:                cmdTemplate = commandShell->ignErr;
                    623:                /*
1.6       millert   624:                 * The error ignoration (hee hee) is already taken care
1.1       deraadt   625:                 * of by the ignErr template, so pretend error checking
                    626:                 * is still on.
                    627:                 */
                    628:                errOff = FALSE;
                    629:            } else {
                    630:                errOff = FALSE;
                    631:            }
                    632:        } else {
                    633:            errOff = FALSE;
                    634:        }
                    635:     }
1.6       millert   636:
1.2       deraadt   637:     DBPRINTF(cmdTemplate, cmd);
1.6       millert   638:
1.1       deraadt   639:     if (errOff) {
                    640:        /*
                    641:         * If echoing is already off, there's no point in issuing the
                    642:         * echoOff command. Otherwise we issue it and pretend it was on
                    643:         * for the whole command...
                    644:         */
                    645:        if (!shutUp && !(job->flags & JOB_SILENT) && commandShell->hasEchoCtl){
1.2       deraadt   646:            DBPRINTF("%s\n", commandShell->echoOff);
1.1       deraadt   647:            shutUp = TRUE;
                    648:        }
1.2       deraadt   649:        DBPRINTF("%s\n", commandShell->errCheck);
1.1       deraadt   650:     }
                    651:     if (shutUp) {
1.2       deraadt   652:        DBPRINTF("%s\n", commandShell->echoOn);
1.1       deraadt   653:     }
1.2       deraadt   654:     return 0;
1.1       deraadt   655: }
                    656:
                    657: /*-
                    658:  *-----------------------------------------------------------------------
                    659:  * JobSaveCommand --
                    660:  *     Save a command to be executed when everything else is done.
                    661:  *     Callback function for JobFinish...
                    662:  *
                    663:  * Results:
                    664:  *     Always returns 0
                    665:  *
                    666:  * Side Effects:
                    667:  *     The command is tacked onto the end of postCommands's commands list.
                    668:  *
                    669:  *-----------------------------------------------------------------------
                    670:  */
                    671: static int
1.2       deraadt   672: JobSaveCommand(cmd, gn)
1.1       deraadt   673:     ClientData   cmd;
                    674:     ClientData   gn;
                    675: {
1.2       deraadt   676:     cmd = (ClientData) Var_Subst(NULL, (char *) cmd, (GNode *) gn, FALSE);
                    677:     (void) Lst_AtEnd(postCommands->commands, cmd);
                    678:     return(0);
                    679: }
                    680:
                    681:
                    682: /*-
                    683:  *-----------------------------------------------------------------------
                    684:  * JobClose --
                    685:  *     Called to close both input and output pipes when a job is finished.
                    686:  *
                    687:  * Results:
                    688:  *     Nada
                    689:  *
                    690:  * Side Effects:
                    691:  *     The file descriptors associated with the job are closed.
                    692:  *
                    693:  *-----------------------------------------------------------------------
                    694:  */
                    695: static void
                    696: JobClose(job)
                    697:     Job *job;
                    698: {
                    699:     if (usePipes) {
                    700: #ifdef RMT_WILL_WATCH
                    701:        Rmt_Ignore(job->inPipe);
                    702: #else
                    703:        FD_CLR(job->inPipe, &outputs);
                    704: #endif
                    705:        if (job->outPipe != job->inPipe) {
                    706:           (void) close(job->outPipe);
                    707:        }
                    708:        JobDoOutput(job, TRUE);
                    709:        (void) close(job->inPipe);
                    710:     } else {
                    711:        (void) close(job->outFd);
                    712:        JobDoOutput(job, TRUE);
                    713:     }
1.1       deraadt   714: }
                    715:
                    716: /*-
                    717:  *-----------------------------------------------------------------------
                    718:  * JobFinish  --
                    719:  *     Do final processing for the given job including updating
                    720:  *     parents and starting new jobs as available/necessary. Note
                    721:  *     that we pay no attention to the JOB_IGNERR flag here.
                    722:  *     This is because when we're called because of a noexecute flag
                    723:  *     or something, jstat.w_status is 0 and when called from
                    724:  *     Job_CatchChildren, the status is zeroed if it s/b ignored.
                    725:  *
                    726:  * Results:
                    727:  *     None
                    728:  *
                    729:  * Side Effects:
                    730:  *     Some nodes may be put on the toBeMade queue.
                    731:  *     Final commands for the job are placed on postCommands.
                    732:  *
1.6       millert   733:  *     If we got an error and are aborting (aborting == ABORT_ERROR) and
1.1       deraadt   734:  *     the job list is now empty, we are done for the day.
1.6       millert   735:  *     If we recognized an error (errors !=0), we set the aborting flag
1.1       deraadt   736:  *     to ABORT_ERROR so no more jobs will be started.
                    737:  *-----------------------------------------------------------------------
                    738:  */
                    739: /*ARGSUSED*/
                    740: static void
1.2       deraadt   741: JobFinish(job, status)
                    742:     Job         *job;            /* job to finish */
                    743:     int                *status;          /* sub-why job went away */
                    744: {
                    745:     Boolean     done;
                    746:
                    747:     if ((WIFEXITED(*status) &&
                    748:         (((WEXITSTATUS(*status) != 0) && !(job->flags & JOB_IGNERR)))) ||
                    749:        (WIFSIGNALED(*status) && (WTERMSIG(*status) != SIGCONT)))
1.1       deraadt   750:     {
                    751:        /*
                    752:         * If it exited non-zero and either we're doing things our
                    753:         * way or we're not ignoring errors, the job is finished.
                    754:         * Similarly, if the shell died because of a signal
                    755:         * the job is also finished. In these
                    756:         * cases, finish out the job's output before printing the exit
                    757:         * status...
                    758:         */
1.2       deraadt   759: #ifdef REMOTE
                    760:        KILL(job->pid, SIGCONT);
                    761: #endif
                    762:        JobClose(job);
1.1       deraadt   763:        if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
1.2       deraadt   764:           (void) fclose(job->cmdFILE);
1.1       deraadt   765:        }
                    766:        done = TRUE;
1.2       deraadt   767: #ifdef REMOTE
                    768:        if (job->flags & JOB_REMOTE)
                    769:            Rmt_Done(job->rmtID, job->node);
                    770: #endif
                    771:     } else if (WIFEXITED(*status)) {
1.1       deraadt   772:        /*
                    773:         * Deal with ignored errors in -B mode. We need to print a message
                    774:         * telling of the ignored error as well as setting status.w_status
                    775:         * to 0 so the next command gets run. To do this, we set done to be
1.2       deraadt   776:         * TRUE if in -B mode and the job exited non-zero.
                    777:         */
                    778:        done = WEXITSTATUS(*status) != 0;
1.6       millert   779:        /*
1.2       deraadt   780:         * Old comment said: "Note we don't
1.1       deraadt   781:         * want to close down any of the streams until we know we're at the
1.2       deraadt   782:         * end."
                    783:         * But we do. Otherwise when are we going to print the rest of the
                    784:         * stuff?
                    785:         */
                    786:        JobClose(job);
                    787: #ifdef REMOTE
                    788:        if (job->flags & JOB_REMOTE)
                    789:            Rmt_Done(job->rmtID, job->node);
                    790: #endif /* REMOTE */
1.1       deraadt   791:     } else {
                    792:        /*
                    793:         * No need to close things down or anything.
                    794:         */
                    795:        done = FALSE;
                    796:     }
1.6       millert   797:
1.1       deraadt   798:     if (done ||
1.2       deraadt   799:        WIFSTOPPED(*status) ||
                    800:        (WIFSIGNALED(*status) && (WTERMSIG(*status) == SIGCONT)) ||
1.1       deraadt   801:        DEBUG(JOB))
                    802:     {
                    803:        FILE      *out;
1.6       millert   804:
1.2       deraadt   805:        if (compatMake && !usePipes && (job->flags & JOB_IGNERR)) {
1.1       deraadt   806:            /*
                    807:             * If output is going to a file and this job is ignoring
                    808:             * errors, arrange to have the exit status sent to the
                    809:             * output file as well.
                    810:             */
1.2       deraadt   811:            out = fdopen(job->outFd, "w");
1.1       deraadt   812:        } else {
                    813:            out = stdout;
                    814:        }
                    815:
1.2       deraadt   816:        if (WIFEXITED(*status)) {
                    817:            if (DEBUG(JOB)) {
                    818:                (void) fprintf(stdout, "Process %d exited.\n", job->pid);
                    819:                (void) fflush(stdout);
                    820:            }
                    821:            if (WEXITSTATUS(*status) != 0) {
1.1       deraadt   822:                if (usePipes && job->node != lastNode) {
1.2       deraadt   823:                    MESSAGE(out, job->node);
1.1       deraadt   824:                    lastNode = job->node;
                    825:                }
1.2       deraadt   826:                (void) fprintf(out, "*** Error code %d%s\n",
                    827:                               WEXITSTATUS(*status),
                    828:                               (job->flags & JOB_IGNERR) ? "(ignored)" : "");
1.1       deraadt   829:
                    830:                if (job->flags & JOB_IGNERR) {
1.2       deraadt   831:                    *status = 0;
1.1       deraadt   832:                }
                    833:            } else if (DEBUG(JOB)) {
                    834:                if (usePipes && job->node != lastNode) {
1.2       deraadt   835:                    MESSAGE(out, job->node);
1.1       deraadt   836:                    lastNode = job->node;
                    837:                }
1.2       deraadt   838:                (void) fprintf(out, "*** Completed successfully\n");
                    839:            }
                    840:        } else if (WIFSTOPPED(*status)) {
                    841:            if (DEBUG(JOB)) {
                    842:                (void) fprintf(stdout, "Process %d stopped.\n", job->pid);
                    843:                (void) fflush(stdout);
1.1       deraadt   844:            }
                    845:            if (usePipes && job->node != lastNode) {
1.2       deraadt   846:                MESSAGE(out, job->node);
1.1       deraadt   847:                lastNode = job->node;
                    848:            }
1.2       deraadt   849:            if (!(job->flags & JOB_REMIGRATE)) {
1.6       millert   850:                (void) fprintf(out, "*** Stopped -- signal %d\n",
                    851:                    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.6       millert   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:     }
1.6       millert   952:
1.1       deraadt   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:     }
1.6       millert   991:
1.1       deraadt   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:     }
1.6       millert  1030:
1.1       deraadt  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:                }
1.6       millert  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 --
1.6       millert  1076:  *     Make sure the given node has all the commands it needs.
1.1       deraadt  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.6       millert  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
1.6       millert  1097:         * commands
1.1       deraadt  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
1.6       millert  1120:             * this node's parents so they never get examined.
1.1       deraadt  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.
1.6       millert  1150:  *
1.1       deraadt  1151:  *-----------------------------------------------------------------------
                   1152:  */
                   1153: /*ARGSUSED*/
                   1154: static void
                   1155: JobLocalInput(stream, job)
1.6       millert  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 */
1.6       millert  1184:
1.1       deraadt  1185:     if (DEBUG(JOB)) {
                   1186:        int       i;
1.6       millert  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:     }
1.6       millert  1197:
1.1       deraadt  1198:     /*
                   1199:      * Some jobs produce no output and it's disconcerting to have
1.6       millert  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:     }
1.6       millert  1209:
1.1       deraadt  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.6       millert  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.6       millert  1229:
1.1       deraadt  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.6       millert  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);
1.6       millert  1272:        } else
1.2       deraadt  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;
1.6       millert  1292:
1.1       deraadt  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
1.6       millert  1322: jobExecFinish:
1.1       deraadt  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.
1.6       millert  1338:  *
1.1       deraadt  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 */
1.6       millert  1353:
1.1       deraadt  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 --
1.6       millert  1392:  *     Restart a job that stopped for some reason.
1.1       deraadt  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
1.6       millert  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.6       millert  1483:
1.1       deraadt  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
1.6       millert  1500:         * as soon as it starts processing commands.
1.1       deraadt  1501:         */
                   1502:        char      *argv[4];
1.6       millert  1503:
1.1       deraadt  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) ||
1.6       millert  1512:            (nLocal < maxLocal && runLocalFirst)
1.2       deraadt  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.6       millert  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.6       millert  1591:
1.1       deraadt  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.6       millert  1607:
1.1       deraadt  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
1.6       millert  1643:  *     by the graph node gn.
1.1       deraadt  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:     Boolean      cmdsOK;     /* true if the nodes commands were all right */
                   1666:     Boolean      local;      /* Set true if the job was run locally */
                   1667:     Boolean      noExec;     /* Set true if we decide not to run the job */
                   1668:
1.2       deraadt  1669:     if (previous != NULL) {
                   1670:        previous->flags &= ~(JOB_FIRST|JOB_IGNERR|JOB_SILENT|JOB_REMOTE);
1.1       deraadt  1671:        job = previous;
                   1672:     } else {
1.2       deraadt  1673:        job = (Job *) emalloc(sizeof(Job));
                   1674:        if (job == NULL) {
1.1       deraadt  1675:            Punt("JobStart out of memory");
                   1676:        }
                   1677:        flags |= JOB_FIRST;
                   1678:     }
                   1679:
                   1680:     job->node = gn;
                   1681:     job->tailCmds = NILLNODE;
                   1682:
                   1683:     /*
                   1684:      * Set the initial value of the flags for this job based on the global
                   1685:      * ones and the node's attributes... Any flags supplied by the caller
                   1686:      * are also added to the field.
                   1687:      */
                   1688:     job->flags = 0;
1.2       deraadt  1689:     if (Targ_Ignore(gn)) {
1.1       deraadt  1690:        job->flags |= JOB_IGNERR;
                   1691:     }
1.2       deraadt  1692:     if (Targ_Silent(gn)) {
1.1       deraadt  1693:        job->flags |= JOB_SILENT;
                   1694:     }
                   1695:     job->flags |= flags;
                   1696:
                   1697:     /*
                   1698:      * Check the commands now so any attributes from .DEFAULT have a chance
                   1699:      * to migrate to the node
                   1700:      */
1.2       deraadt  1701:     if (!compatMake && job->flags & JOB_FIRST) {
1.1       deraadt  1702:        cmdsOK = Job_CheckCommands(gn, Error);
                   1703:     } else {
                   1704:        cmdsOK = TRUE;
                   1705:     }
1.6       millert  1706:
1.1       deraadt  1707:     /*
1.6       millert  1708:      * If the -n flag wasn't given, we open up OUR (not the child's)
1.1       deraadt  1709:      * temporary file to stuff commands in it. The thing is rd/wr so we don't
                   1710:      * need to reopen it to feed it to the shell. If the -n flag *was* given,
                   1711:      * we just set the file to be stdout. Cute, huh?
                   1712:      */
                   1713:     if ((gn->type & OP_MAKE) || (!noExecute && !touchFlag)) {
                   1714:        /*
                   1715:         * We're serious here, but if the commands were bogus, we're
                   1716:         * also dead...
                   1717:         */
                   1718:        if (!cmdsOK) {
                   1719:            DieHorribly();
                   1720:        }
1.6       millert  1721:
1.2       deraadt  1722:        job->cmdFILE = fopen(tfile, "w+");
                   1723:        if (job->cmdFILE == NULL) {
                   1724:            Punt("Could not open %s", tfile);
1.1       deraadt  1725:        }
1.2       deraadt  1726:        (void) fcntl(FILENO(job->cmdFILE), F_SETFD, 1);
1.1       deraadt  1727:        /*
                   1728:         * Send the commands to the command file, flush all its buffers then
                   1729:         * rewind and remove the thing.
                   1730:         */
                   1731:        noExec = FALSE;
                   1732:
                   1733:        /*
                   1734:         * used to be backwards; replace when start doing multiple commands
                   1735:         * per shell.
                   1736:         */
                   1737:        if (compatMake) {
                   1738:            /*
                   1739:             * Be compatible: If this is the first time for this node,
                   1740:             * verify its commands are ok and open the commands list for
                   1741:             * sequential access by later invocations of JobStart.
                   1742:             * Once that is done, we take the next command off the list
                   1743:             * and print it to the command file. If the command was an
                   1744:             * ellipsis, note that there's nothing more to execute.
                   1745:             */
                   1746:            if ((job->flags&JOB_FIRST) && (Lst_Open(gn->commands) != SUCCESS)){
                   1747:                cmdsOK = FALSE;
                   1748:            } else {
1.2       deraadt  1749:                LstNode ln = Lst_Next(gn->commands);
1.6       millert  1750:
1.1       deraadt  1751:                if ((ln == NILLNODE) ||
1.2       deraadt  1752:                    JobPrintCommand((ClientData) Lst_Datum(ln),
                   1753:                                    (ClientData) job))
1.1       deraadt  1754:                {
                   1755:                    noExec = TRUE;
1.2       deraadt  1756:                    Lst_Close(gn->commands);
1.1       deraadt  1757:                }
                   1758:                if (noExec && !(job->flags & JOB_FIRST)) {
                   1759:                    /*
                   1760:                     * If we're not going to execute anything, the job
                   1761:                     * is done and we need to close down the various
                   1762:                     * file descriptors we've opened for output, then
                   1763:                     * call JobDoOutput to catch the final characters or
                   1764:                     * send the file to the screen... Note that the i/o streams
                   1765:                     * are only open if this isn't the first job.
                   1766:                     * Note also that this could not be done in
                   1767:                     * Job_CatchChildren b/c it wasn't clear if there were
                   1768:                     * more commands to execute or not...
                   1769:                     */
1.2       deraadt  1770:                    JobClose(job);
1.1       deraadt  1771:                }
                   1772:            }
                   1773:        } else {
                   1774:            /*
                   1775:             * We can do all the commands at once. hooray for sanity
                   1776:             */
                   1777:            numCommands = 0;
1.2       deraadt  1778:            Lst_ForEach(gn->commands, JobPrintCommand, (ClientData)job);
1.6       millert  1779:
1.1       deraadt  1780:            /*
                   1781:             * If we didn't print out any commands to the shell script,
                   1782:             * there's not much point in executing the shell, is there?
                   1783:             */
                   1784:            if (numCommands == 0) {
                   1785:                noExec = TRUE;
                   1786:            }
                   1787:        }
                   1788:     } else if (noExecute) {
                   1789:        /*
                   1790:         * Not executing anything -- just print all the commands to stdout
                   1791:         * in one fell swoop. This will still set up job->tailCmds correctly.
                   1792:         */
                   1793:        if (lastNode != gn) {
1.2       deraadt  1794:            MESSAGE(stdout, gn);
1.1       deraadt  1795:            lastNode = gn;
                   1796:        }
                   1797:        job->cmdFILE = stdout;
                   1798:        /*
                   1799:         * Only print the commands if they're ok, but don't die if they're
                   1800:         * not -- just let the user know they're bad and keep going. It
                   1801:         * doesn't do any harm in this case and may do some good.
                   1802:         */
                   1803:        if (cmdsOK) {
                   1804:            Lst_ForEach(gn->commands, JobPrintCommand, (ClientData)job);
                   1805:        }
                   1806:        /*
                   1807:         * Don't execute the shell, thank you.
                   1808:         */
                   1809:        noExec = TRUE;
                   1810:     } else {
                   1811:        /*
                   1812:         * Just touch the target and note that no shell should be executed.
                   1813:         * Set cmdFILE to stdout to make life easier. Check the commands, too,
                   1814:         * but don't die if they're no good -- it does no harm to keep working
                   1815:         * up the graph.
                   1816:         */
                   1817:        job->cmdFILE = stdout;
1.2       deraadt  1818:        Job_Touch(gn, job->flags&JOB_SILENT);
1.1       deraadt  1819:        noExec = TRUE;
                   1820:     }
                   1821:
                   1822:     /*
1.6       millert  1823:      * If we're not supposed to execute a shell, don't.
1.1       deraadt  1824:      */
                   1825:     if (noExec) {
                   1826:        /*
                   1827:         * Unlink and close the command file if we opened one
                   1828:         */
                   1829:        if (job->cmdFILE != stdout) {
1.2       deraadt  1830:            (void) eunlink(tfile);
                   1831:            if (job->cmdFILE != NULL)
                   1832:                (void) fclose(job->cmdFILE);
1.1       deraadt  1833:        } else {
1.2       deraadt  1834:             (void) fflush(stdout);
1.1       deraadt  1835:        }
                   1836:
                   1837:        /*
                   1838:         * We only want to work our way up the graph if we aren't here because
                   1839:         * the commands for the job were no good.
                   1840:         */
                   1841:        if (cmdsOK) {
                   1842:            if (aborting == 0) {
                   1843:                if (job->tailCmds != NILLNODE) {
                   1844:                    Lst_ForEachFrom(job->node->commands, job->tailCmds,
                   1845:                                    JobSaveCommand,
1.2       deraadt  1846:                                   (ClientData)job->node);
1.1       deraadt  1847:                }
                   1848:                Make_Update(job->node);
                   1849:            }
                   1850:            free((Address)job);
                   1851:            return(JOB_FINISHED);
                   1852:        } else {
                   1853:            free((Address)job);
                   1854:            return(JOB_ERROR);
                   1855:        }
                   1856:     } else {
1.2       deraadt  1857:        (void) fflush(job->cmdFILE);
                   1858:        (void) eunlink(tfile);
1.1       deraadt  1859:     }
                   1860:
                   1861:     /*
                   1862:      * Set up the control arguments to the shell. This is based on the flags
                   1863:      * set earlier for this job.
                   1864:      */
                   1865:     JobMakeArgv(job, argv);
                   1866:
                   1867:     /*
                   1868:      * If we're using pipes to catch output, create the pipe by which we'll
                   1869:      * get the shell's output. If we're using files, print out that we're
1.7     ! millert  1870:      * starting a job and then set up its temporary-file name.
1.1       deraadt  1871:      */
1.2       deraadt  1872:     if (!compatMake || (job->flags & JOB_FIRST)) {
1.1       deraadt  1873:        if (usePipes) {
                   1874:            int fd[2];
1.2       deraadt  1875:            if (pipe(fd) == -1)
                   1876:                Punt("Cannot create pipe: %s", strerror(errno));
1.1       deraadt  1877:            job->inPipe = fd[0];
                   1878:            job->outPipe = fd[1];
1.2       deraadt  1879:            (void) fcntl(job->inPipe, F_SETFD, 1);
                   1880:            (void) fcntl(job->outPipe, F_SETFD, 1);
1.1       deraadt  1881:        } else {
1.2       deraadt  1882:            (void) fprintf(stdout, "Remaking `%s'\n", gn->name);
                   1883:            (void) fflush(stdout);
1.7     ! millert  1884:            (void) strcpy(job->outFile, TMPPAT);
        !          1885:            if ((job->outFd = mkstemp(job->outFile)) == -1)
        !          1886:                Punt("Cannot create temp file: %s", strerror(errno));
1.2       deraadt  1887:            (void) fcntl(job->outFd, F_SETFD, 1);
1.1       deraadt  1888:        }
                   1889:     }
                   1890:
1.2       deraadt  1891: #ifdef REMOTE
                   1892:     if (!(gn->type & OP_NOEXPORT) && !(runLocalFirst && nLocal < maxLocal)) {
                   1893: #ifdef RMT_NO_EXEC
                   1894:        local = !Rmt_Export(shellPath, argv, job);
                   1895: #else
                   1896:        local = !Rmt_Begin(shellPath, argv, job->node);
                   1897: #endif /* RMT_NO_EXEC */
                   1898:        if (!local) {
                   1899:            job->flags |= JOB_REMOTE;
                   1900:        }
                   1901:     } else
                   1902: #endif
                   1903:        local = TRUE;
1.1       deraadt  1904:
                   1905:     if (local && (((nLocal >= maxLocal) &&
1.2       deraadt  1906:        !(job->flags & JOB_SPECIAL) &&
                   1907: #ifdef REMOTE
                   1908:        (!(gn->type & OP_NOEXPORT) || (maxLocal != 0))
                   1909: #else
                   1910:        (maxLocal != 0)
                   1911: #endif
                   1912:        )))
1.1       deraadt  1913:     {
                   1914:        /*
                   1915:         * The job can only be run locally, but we've hit the limit of
                   1916:         * local concurrency, so put the job on hold until some other job
1.6       millert  1917:         * finishes. Note that the special jobs (.BEGIN, .INTERRUPT and .END)
1.1       deraadt  1918:         * may be run locally even when the local limit has been reached
1.6       millert  1919:         * (e.g. when maxLocal == 0), though they will be exported if at
1.2       deraadt  1920:         * all possible. In addition, any target marked with .NOEXPORT will
                   1921:         * be run locally if maxLocal is 0.
1.1       deraadt  1922:         */
                   1923:        jobFull = TRUE;
1.6       millert  1924:
1.1       deraadt  1925:        if (DEBUG(JOB)) {
1.2       deraadt  1926:           (void) fprintf(stdout, "Can only run job locally.\n");
                   1927:           (void) fflush(stdout);
1.1       deraadt  1928:        }
                   1929:        job->flags |= JOB_RESTART;
1.2       deraadt  1930:        (void) Lst_AtEnd(stoppedJobs, (ClientData)job);
1.1       deraadt  1931:     } else {
                   1932:        if ((nLocal >= maxLocal) && local) {
                   1933:            /*
1.6       millert  1934:             * If we're running this job locally as a special case (see above),
1.1       deraadt  1935:             * at least say the table is full.
                   1936:             */
                   1937:            jobFull = TRUE;
                   1938:            if (DEBUG(JOB)) {
1.2       deraadt  1939:                (void) fprintf(stdout, "Local job queue is full.\n");
                   1940:                (void) fflush(stdout);
1.1       deraadt  1941:            }
                   1942:        }
                   1943:        JobExec(job, argv);
                   1944:     }
                   1945:     return(JOB_RUNNING);
                   1946: }
                   1947:
1.6       millert  1948: static char *
1.2       deraadt  1949: JobOutput(job, cp, endp, msg)
                   1950:     register Job *job;
                   1951:     register char *cp, *endp;
                   1952:     int msg;
                   1953: {
                   1954:     register char *ecp;
                   1955:
                   1956:     if (commandShell->noPrint) {
                   1957:        ecp = Str_FindSubstring(cp, commandShell->noPrint);
                   1958:        while (ecp != NULL) {
                   1959:            if (cp != ecp) {
                   1960:                *ecp = '\0';
                   1961:                if (msg && job->node != lastNode) {
                   1962:                    MESSAGE(stdout, job->node);
                   1963:                    lastNode = job->node;
                   1964:                }
                   1965:                /*
                   1966:                 * The only way there wouldn't be a newline after
                   1967:                 * this line is if it were the last in the buffer.
                   1968:                 * however, since the non-printable comes after it,
                   1969:                 * there must be a newline, so we don't print one.
                   1970:                 */
                   1971:                (void) fprintf(stdout, "%s", cp);
                   1972:                (void) fflush(stdout);
                   1973:            }
                   1974:            cp = ecp + commandShell->noPLen;
                   1975:            if (cp != endp) {
                   1976:                /*
                   1977:                 * Still more to print, look again after skipping
                   1978:                 * the whitespace following the non-printable
                   1979:                 * command....
                   1980:                 */
                   1981:                cp++;
                   1982:                while (*cp == ' ' || *cp == '\t' || *cp == '\n') {
                   1983:                    cp++;
                   1984:                }
                   1985:                ecp = Str_FindSubstring(cp, commandShell->noPrint);
                   1986:            } else {
                   1987:                return cp;
                   1988:            }
                   1989:        }
                   1990:     }
                   1991:     return cp;
                   1992: }
                   1993:
1.1       deraadt  1994: /*-
                   1995:  *-----------------------------------------------------------------------
                   1996:  * JobDoOutput  --
                   1997:  *     This function is called at different times depending on
                   1998:  *     whether the user has specified that output is to be collected
                   1999:  *     via pipes or temporary files. In the former case, we are called
                   2000:  *     whenever there is something to read on the pipe. We collect more
                   2001:  *     output from the given job and store it in the job's outBuf. If
                   2002:  *     this makes up a line, we print it tagged by the job's identifier,
                   2003:  *     as necessary.
                   2004:  *     If output has been collected in a temporary file, we open the
                   2005:  *     file and read it line by line, transfering it to our own
                   2006:  *     output channel until the file is empty. At which point we
                   2007:  *     remove the temporary file.
                   2008:  *     In both cases, however, we keep our figurative eye out for the
                   2009:  *     'noPrint' line for the shell from which the output came. If
                   2010:  *     we recognize a line, we don't print it. If the command is not
1.6       millert  2011:  *     alone on the line (the character after it is not \0 or \n), we
1.1       deraadt  2012:  *     do print whatever follows it.
                   2013:  *
                   2014:  * Results:
                   2015:  *     None
                   2016:  *
                   2017:  * Side Effects:
                   2018:  *     curPos may be shifted as may the contents of outBuf.
                   2019:  *-----------------------------------------------------------------------
                   2020:  */
1.2       deraadt  2021: STATIC void
                   2022: JobDoOutput(job, finish)
1.1       deraadt  2023:     register Job   *job;         /* the job whose output needs printing */
                   2024:     Boolean       finish;        /* TRUE if this is the last time we'll be
                   2025:                                   * called for this job */
                   2026: {
                   2027:     Boolean       gotNL = FALSE;  /* true if got a newline */
1.2       deraadt  2028:     Boolean       fbuf;          /* true if our buffer filled up */
1.1       deraadt  2029:     register int  nr;            /* number of bytes read */
                   2030:     register int  i;             /* auxiliary index into outBuf */
1.6       millert  2031:     register int  max;           /* limit for i (end of current data) */
                   2032:     int                  nRead;          /* (Temporary) number of bytes read */
1.1       deraadt  2033:
                   2034:     FILE         *oFILE;         /* Stream pointer to shell's output file */
                   2035:     char          inLine[132];
                   2036:
1.6       millert  2037:
1.1       deraadt  2038:     if (usePipes) {
                   2039:        /*
                   2040:         * Read as many bytes as will fit in the buffer.
                   2041:         */
                   2042: end_loop:
1.2       deraadt  2043:        gotNL = FALSE;
                   2044:        fbuf = FALSE;
1.6       millert  2045:
1.2       deraadt  2046:        nRead = read(job->inPipe, &job->outBuf[job->curPos],
1.1       deraadt  2047:                         JOB_BUFSIZE - job->curPos);
                   2048:        if (nRead < 0) {
                   2049:            if (DEBUG(JOB)) {
                   2050:                perror("JobDoOutput(piperead)");
                   2051:            }
                   2052:            nr = 0;
                   2053:        } else {
                   2054:            nr = nRead;
                   2055:        }
                   2056:
                   2057:        /*
1.6       millert  2058:         * If we hit the end-of-file (the job is dead), we must flush its
1.1       deraadt  2059:         * remaining output, so pretend we read a newline if there's any
                   2060:         * output remaining in the buffer.
                   2061:         * Also clear the 'finish' flag so we stop looping.
                   2062:         */
                   2063:        if ((nr == 0) && (job->curPos != 0)) {
                   2064:            job->outBuf[job->curPos] = '\n';
                   2065:            nr = 1;
                   2066:            finish = FALSE;
                   2067:        } else if (nr == 0) {
                   2068:            finish = FALSE;
                   2069:        }
1.6       millert  2070:
1.1       deraadt  2071:        /*
                   2072:         * Look for the last newline in the bytes we just got. If there is
                   2073:         * one, break out of the loop with 'i' as its index and gotNL set
1.6       millert  2074:         * TRUE.
1.1       deraadt  2075:         */
                   2076:        max = job->curPos + nr;
                   2077:        for (i = job->curPos + nr - 1; i >= job->curPos; i--) {
                   2078:            if (job->outBuf[i] == '\n') {
                   2079:                gotNL = TRUE;
                   2080:                break;
                   2081:            } else if (job->outBuf[i] == '\0') {
                   2082:                /*
                   2083:                 * Why?
                   2084:                 */
                   2085:                job->outBuf[i] = ' ';
                   2086:            }
                   2087:        }
1.6       millert  2088:
1.1       deraadt  2089:        if (!gotNL) {
                   2090:            job->curPos += nr;
                   2091:            if (job->curPos == JOB_BUFSIZE) {
                   2092:                /*
                   2093:                 * If we've run out of buffer space, we have no choice
1.6       millert  2094:                 * but to print the stuff. sigh.
1.1       deraadt  2095:                 */
1.2       deraadt  2096:                fbuf = TRUE;
1.1       deraadt  2097:                i = job->curPos;
                   2098:            }
                   2099:        }
1.2       deraadt  2100:        if (gotNL || fbuf) {
1.1       deraadt  2101:            /*
                   2102:             * Need to send the output to the screen. Null terminate it
                   2103:             * first, overwriting the newline character if there was one.
1.6       millert  2104:             * So long as the line isn't one we should filter (according
1.1       deraadt  2105:             * to the shell description), we print the line, preceeded
                   2106:             * by a target banner if this target isn't the same as the
                   2107:             * one for which we last printed something.
                   2108:             * The rest of the data in the buffer are then shifted down
1.6       millert  2109:             * to the start of the buffer and curPos is set accordingly.
1.1       deraadt  2110:             */
                   2111:            job->outBuf[i] = '\0';
                   2112:            if (i >= job->curPos) {
1.2       deraadt  2113:                char *cp;
1.6       millert  2114:
                   2115:                cp = JobOutput(job, job->outBuf, &job->outBuf[i], FALSE);
1.1       deraadt  2116:
                   2117:                /*
                   2118:                 * There's still more in that thar buffer. This time, though,
                   2119:                 * we know there's no newline at the end, so we add one of
                   2120:                 * our own free will.
                   2121:                 */
                   2122:                if (*cp != '\0') {
                   2123:                    if (job->node != lastNode) {
1.2       deraadt  2124:                        MESSAGE(stdout, job->node);
1.1       deraadt  2125:                        lastNode = job->node;
                   2126:                    }
1.2       deraadt  2127:                    (void) fprintf(stdout, "%s%s", cp, gotNL ? "\n" : "");
                   2128:                    (void) fflush(stdout);
1.1       deraadt  2129:                }
                   2130:            }
                   2131:            if (i < max - 1) {
                   2132:                /* shift the remaining characters down */
1.6       millert  2133:                (void) memcpy(job->outBuf, &job->outBuf[i + 1], max - (i + 1));
                   2134:                job->curPos = max - (i + 1);
                   2135:
1.1       deraadt  2136:            } else {
                   2137:                /*
                   2138:                 * We have written everything out, so we just start over
                   2139:                 * from the start of the buffer. No copying. No nothing.
                   2140:                 */
                   2141:                job->curPos = 0;
                   2142:            }
                   2143:        }
                   2144:        if (finish) {
                   2145:            /*
                   2146:             * If the finish flag is true, we must loop until we hit
1.6       millert  2147:             * end-of-file on the pipe. This is guaranteed to happen
                   2148:             * eventually since the other end of the pipe is now closed
                   2149:             * (we closed it explicitly and the child has exited). When
                   2150:             * we do get an EOF, finish will be set FALSE and we'll fall
                   2151:             * through and out.
1.1       deraadt  2152:             */
                   2153:            goto end_loop;
                   2154:        }
                   2155:     } else {
                   2156:        /*
                   2157:         * We've been called to retrieve the output of the job from the
                   2158:         * temporary file where it's been squirreled away. This consists of
                   2159:         * opening the file, reading the output line by line, being sure not
                   2160:         * to print the noPrint line for the shell we used, then close and
                   2161:         * remove the temporary file. Very simple.
                   2162:         *
                   2163:         * Change to read in blocks and do FindSubString type things as for
                   2164:         * pipes? That would allow for "@echo -n..."
                   2165:         */
1.2       deraadt  2166:        oFILE = fopen(job->outFile, "r");
                   2167:        if (oFILE != NULL) {
                   2168:            (void) fprintf(stdout, "Results of making %s:\n", job->node->name);
                   2169:            (void) fflush(stdout);
                   2170:            while (fgets(inLine, sizeof(inLine), oFILE) != NULL) {
                   2171:                register char   *cp, *endp, *oendp;
1.1       deraadt  2172:
                   2173:                cp = inLine;
1.2       deraadt  2174:                oendp = endp = inLine + strlen(inLine);
1.1       deraadt  2175:                if (endp[-1] == '\n') {
                   2176:                    *--endp = '\0';
                   2177:                }
1.2       deraadt  2178:                cp = JobOutput(job, inLine, endp, FALSE);
1.1       deraadt  2179:
                   2180:                /*
                   2181:                 * There's still more in that thar buffer. This time, though,
                   2182:                 * we know there's no newline at the end, so we add one of
                   2183:                 * our own free will.
                   2184:                 */
1.2       deraadt  2185:                (void) fprintf(stdout, "%s", cp);
                   2186:                (void) fflush(stdout);
                   2187:                if (endp != oendp) {
                   2188:                    (void) fprintf(stdout, "\n");
                   2189:                    (void) fflush(stdout);
1.1       deraadt  2190:                }
                   2191:            }
1.2       deraadt  2192:            (void) fclose(oFILE);
                   2193:            (void) eunlink(job->outFile);
1.1       deraadt  2194:        }
                   2195:     }
                   2196: }
                   2197:
                   2198: /*-
                   2199:  *-----------------------------------------------------------------------
                   2200:  * Job_CatchChildren --
                   2201:  *     Handle the exit of a child. Called from Make_Make.
                   2202:  *
                   2203:  * Results:
                   2204:  *     none.
                   2205:  *
                   2206:  * Side Effects:
                   2207:  *     The job descriptor is removed from the list of children.
                   2208:  *
                   2209:  * Notes:
                   2210:  *     We do waits, blocking or not, according to the wisdom of our
                   2211:  *     caller, until there are no more children to report. For each
                   2212:  *     job, call JobFinish to finish things off. This will take care of
                   2213:  *     putting jobs on the stoppedJobs queue.
                   2214:  *
                   2215:  *-----------------------------------------------------------------------
                   2216:  */
                   2217: void
1.2       deraadt  2218: Job_CatchChildren(block)
1.1       deraadt  2219:     Boolean      block;        /* TRUE if should block on the wait. */
                   2220: {
                   2221:     int          pid;          /* pid of dead child */
                   2222:     register Job  *job;                /* job descriptor for dead child */
                   2223:     LstNode       jnode;       /* list element for finding job */
1.2       deraadt  2224:     int                  status;       /* Exit/termination status */
1.1       deraadt  2225:
                   2226:     /*
                   2227:      * Don't even bother if we know there's no one around.
                   2228:      */
                   2229:     if (nLocal == 0) {
                   2230:        return;
                   2231:     }
1.6       millert  2232:
1.2       deraadt  2233:     while ((pid = waitpid((pid_t) -1, &status,
                   2234:                          (block?0:WNOHANG)|WUNTRACED)) > 0)
1.1       deraadt  2235:     {
1.2       deraadt  2236:        if (DEBUG(JOB)) {
                   2237:            (void) fprintf(stdout, "Process %d exited or stopped.\n", pid);
                   2238:            (void) fflush(stdout);
                   2239:        }
1.6       millert  2240:
1.1       deraadt  2241:
1.2       deraadt  2242:        jnode = Lst_Find(jobs, (ClientData)&pid, JobCmpPid);
1.1       deraadt  2243:
                   2244:        if (jnode == NILLNODE) {
1.2       deraadt  2245:            if (WIFSIGNALED(status) && (WTERMSIG(status) == SIGCONT)) {
1.1       deraadt  2246:                jnode = Lst_Find(stoppedJobs, (ClientData) &pid, JobCmpPid);
                   2247:                if (jnode == NILLNODE) {
1.6       millert  2248:                    Error("Resumed child (%d) not in table", pid);
1.1       deraadt  2249:                    continue;
                   2250:                }
                   2251:                job = (Job *)Lst_Datum(jnode);
1.2       deraadt  2252:                (void) Lst_Remove(stoppedJobs, jnode);
1.1       deraadt  2253:            } else {
1.6       millert  2254:                Error("Child (%d) not in table?", pid);
1.1       deraadt  2255:                continue;
                   2256:            }
                   2257:        } else {
1.2       deraadt  2258:            job = (Job *) Lst_Datum(jnode);
                   2259:            (void) Lst_Remove(jobs, jnode);
1.1       deraadt  2260:            nJobs -= 1;
                   2261:            if (jobFull && DEBUG(JOB)) {
1.2       deraadt  2262:                (void) fprintf(stdout, "Job queue is no longer full.\n");
                   2263:                (void) fflush(stdout);
1.1       deraadt  2264:            }
                   2265:            jobFull = FALSE;
1.2       deraadt  2266: #ifdef REMOTE
                   2267:            if (!(job->flags & JOB_REMOTE)) {
                   2268:                if (DEBUG(JOB)) {
                   2269:                    (void) fprintf(stdout,
                   2270:                                   "Job queue has one fewer local process.\n");
                   2271:                    (void) fflush(stdout);
                   2272:                }
                   2273:                nLocal -= 1;
                   2274:            }
                   2275: #else
1.1       deraadt  2276:            nLocal -= 1;
1.2       deraadt  2277: #endif
1.1       deraadt  2278:        }
                   2279:
1.2       deraadt  2280:        JobFinish(job, &status);
1.1       deraadt  2281:     }
                   2282: }
                   2283:
                   2284: /*-
                   2285:  *-----------------------------------------------------------------------
                   2286:  * Job_CatchOutput --
                   2287:  *     Catch the output from our children, if we're using
                   2288:  *     pipes do so. Otherwise just block time until we get a
1.6       millert  2289:  *     signal (most likely a SIGCHLD) since there's no point in
1.1       deraadt  2290:  *     just spinning when there's nothing to do and the reaping
1.6       millert  2291:  *     of a child can wait for a while.
1.1       deraadt  2292:  *
                   2293:  * Results:
1.6       millert  2294:  *     None
1.1       deraadt  2295:  *
                   2296:  * Side Effects:
                   2297:  *     Output is read from pipes if we're piping.
                   2298:  * -----------------------------------------------------------------------
                   2299:  */
                   2300: void
1.2       deraadt  2301: Job_CatchOutput()
1.1       deraadt  2302: {
                   2303:     int                  nfds;
                   2304:     struct timeval       timeout;
                   2305:     fd_set               readfds;
                   2306:     register LstNode     ln;
                   2307:     register Job         *job;
                   2308: #ifdef RMT_WILL_WATCH
                   2309:     int                          pnJobs;       /* Previous nJobs */
                   2310: #endif
                   2311:
1.2       deraadt  2312:     (void) fflush(stdout);
1.1       deraadt  2313: #ifdef RMT_WILL_WATCH
                   2314:     pnJobs = nJobs;
                   2315:
                   2316:     /*
                   2317:      * It is possible for us to be called with nJobs equal to 0. This happens
                   2318:      * if all the jobs finish and a job that is stopped cannot be run
                   2319:      * locally (eg if maxLocal is 0) and cannot be exported. The job will
                   2320:      * be placed back on the stoppedJobs queue, Job_Empty() will return false,
                   2321:      * Make_Run will call us again when there's nothing for which to wait.
                   2322:      * nJobs never changes, so we loop forever. Hence the check. It could
                   2323:      * be argued that we should sleep for a bit so as not to swamp the
                   2324:      * exportation system with requests. Perhaps we should.
                   2325:      *
                   2326:      * NOTE: IT IS THE RESPONSIBILITY OF Rmt_Wait TO CALL Job_CatchChildren
                   2327:      * IN A TIMELY FASHION TO CATCH ANY LOCALLY RUNNING JOBS THAT EXIT.
                   2328:      * It may use the variable nLocal to determine if it needs to call
1.6       millert  2329:      * Job_CatchChildren (if nLocal is 0, there's nothing for which to
1.1       deraadt  2330:      * wait...)
                   2331:      */
                   2332:     while (nJobs != 0 && pnJobs == nJobs) {
                   2333:        Rmt_Wait();
                   2334:     }
                   2335: #else
                   2336:     if (usePipes) {
                   2337:        readfds = outputs;
                   2338:        timeout.tv_sec = SEL_SEC;
                   2339:        timeout.tv_usec = SEL_USEC;
                   2340:
1.2       deraadt  2341:        if ((nfds = select(FD_SETSIZE, &readfds, (fd_set *) 0,
                   2342:                           (fd_set *) 0, &timeout)) <= 0)
1.1       deraadt  2343:            return;
1.2       deraadt  2344:        else {
                   2345:            if (Lst_Open(jobs) == FAILURE) {
                   2346:                Punt("Cannot open job table");
1.1       deraadt  2347:            }
1.2       deraadt  2348:            while (nfds && (ln = Lst_Next(jobs)) != NILLNODE) {
                   2349:                job = (Job *) Lst_Datum(ln);
1.1       deraadt  2350:                if (FD_ISSET(job->inPipe, &readfds)) {
1.2       deraadt  2351:                    JobDoOutput(job, FALSE);
1.1       deraadt  2352:                    nfds -= 1;
                   2353:                }
                   2354:            }
1.2       deraadt  2355:            Lst_Close(jobs);
1.1       deraadt  2356:        }
                   2357:     }
                   2358: #endif /* RMT_WILL_WATCH */
                   2359: }
                   2360:
                   2361: /*-
                   2362:  *-----------------------------------------------------------------------
                   2363:  * Job_Make --
                   2364:  *     Start the creation of a target. Basically a front-end for
                   2365:  *     JobStart used by the Make module.
                   2366:  *
                   2367:  * Results:
                   2368:  *     None.
                   2369:  *
                   2370:  * Side Effects:
                   2371:  *     Another job is started.
                   2372:  *
                   2373:  *-----------------------------------------------------------------------
                   2374:  */
                   2375: void
1.2       deraadt  2376: Job_Make(gn)
1.1       deraadt  2377:     GNode   *gn;
                   2378: {
1.2       deraadt  2379:     (void) JobStart(gn, 0, NULL);
1.1       deraadt  2380: }
                   2381:
                   2382: /*-
                   2383:  *-----------------------------------------------------------------------
                   2384:  * Job_Init --
                   2385:  *     Initialize the process module
                   2386:  *
                   2387:  * Results:
                   2388:  *     none
                   2389:  *
                   2390:  * Side Effects:
                   2391:  *     lists and counters are initialized
                   2392:  *-----------------------------------------------------------------------
                   2393:  */
                   2394: void
1.2       deraadt  2395: Job_Init(maxproc, maxlocal)
1.1       deraadt  2396:     int           maxproc;  /* the greatest number of jobs which may be
                   2397:                             * running at one time */
                   2398:     int                  maxlocal; /* the greatest number of local jobs which may
                   2399:                             * be running at once. */
                   2400: {
                   2401:     GNode         *begin;     /* node for commands to do at the very start */
1.7     ! millert  2402:     int                  tfd;
1.1       deraadt  2403:
1.7     ! millert  2404:     (void) strcpy(tfile, TMPPAT);
        !          2405:     if ((tfd = mkstemp(tfile)) == -1)
        !          2406:        Punt("Cannot create temp file: %s", strerror(errno));
        !          2407:     else
        !          2408:        (void) close(tfd);
1.1       deraadt  2409:
1.2       deraadt  2410:     jobs =       Lst_Init(FALSE);
1.1       deraadt  2411:     stoppedJobs = Lst_Init(FALSE);
                   2412:     maxJobs =    maxproc;
                   2413:     maxLocal =           maxlocal;
                   2414:     nJobs =      0;
                   2415:     nLocal =     0;
                   2416:     jobFull =    FALSE;
                   2417:
                   2418:     aborting =           0;
                   2419:     errors =     0;
                   2420:
                   2421:     lastNode =   NILGNODE;
                   2422:
1.2       deraadt  2423:     if (maxJobs == 1
                   2424: #ifdef REMOTE
                   2425:        || noMessages
                   2426: #endif
                   2427:                     ) {
1.1       deraadt  2428:        /*
                   2429:         * If only one job can run at a time, there's no need for a banner,
                   2430:         * no is there?
                   2431:         */
                   2432:        targFmt = "";
                   2433:     } else {
                   2434:        targFmt = TARG_FMT;
                   2435:     }
1.6       millert  2436:
1.2       deraadt  2437:     if (shellPath == NULL) {
1.1       deraadt  2438:        /*
                   2439:         * The user didn't specify a shell to use, so we are using the
                   2440:         * default one... Both the absolute path and the last component
                   2441:         * must be set. The last component is taken from the 'name' field
                   2442:         * of the default shell description pointed-to by commandShell.
                   2443:         * All default shells are located in _PATH_DEFSHELLDIR.
                   2444:         */
                   2445:        shellName = commandShell->name;
1.2       deraadt  2446:        shellPath = str_concat(_PATH_DEFSHELLDIR, shellName, STR_ADDSLASH);
1.1       deraadt  2447:     }
                   2448:
1.2       deraadt  2449:     if (commandShell->exit == NULL) {
1.1       deraadt  2450:        commandShell->exit = "";
                   2451:     }
1.2       deraadt  2452:     if (commandShell->echo == NULL) {
1.1       deraadt  2453:        commandShell->echo = "";
                   2454:     }
                   2455:
                   2456:     /*
                   2457:      * Catch the four signals that POSIX specifies if they aren't ignored.
                   2458:      * JobPassSig will take care of calling JobInterrupt if appropriate.
                   2459:      */
1.2       deraadt  2460:     if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
                   2461:        (void) signal(SIGINT, JobPassSig);
1.1       deraadt  2462:     }
1.2       deraadt  2463:     if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
                   2464:        (void) signal(SIGHUP, JobPassSig);
1.1       deraadt  2465:     }
1.2       deraadt  2466:     if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
                   2467:        (void) signal(SIGQUIT, JobPassSig);
1.1       deraadt  2468:     }
1.2       deraadt  2469:     if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
                   2470:        (void) signal(SIGTERM, JobPassSig);
1.1       deraadt  2471:     }
                   2472:     /*
                   2473:      * There are additional signals that need to be caught and passed if
                   2474:      * either the export system wants to be told directly of signals or if
1.6       millert  2475:      * we're giving each job its own process group (since then it won't get
1.1       deraadt  2476:      * signals from the terminal driver as we own the terminal)
                   2477:      */
                   2478: #if defined(RMT_WANTS_SIGNALS) || defined(USE_PGRP)
1.2       deraadt  2479:     if (signal(SIGTSTP, SIG_IGN) != SIG_IGN) {
                   2480:        (void) signal(SIGTSTP, JobPassSig);
1.1       deraadt  2481:     }
1.2       deraadt  2482:     if (signal(SIGTTOU, SIG_IGN) != SIG_IGN) {
                   2483:        (void) signal(SIGTTOU, JobPassSig);
1.1       deraadt  2484:     }
1.2       deraadt  2485:     if (signal(SIGTTIN, SIG_IGN) != SIG_IGN) {
                   2486:        (void) signal(SIGTTIN, JobPassSig);
1.1       deraadt  2487:     }
1.2       deraadt  2488:     if (signal(SIGWINCH, SIG_IGN) != SIG_IGN) {
                   2489:        (void) signal(SIGWINCH, JobPassSig);
1.1       deraadt  2490:     }
                   2491: #endif
1.6       millert  2492:
1.2       deraadt  2493:     begin = Targ_FindNode(".BEGIN", TARG_NOCREATE);
1.1       deraadt  2494:
                   2495:     if (begin != NILGNODE) {
1.2       deraadt  2496:        JobStart(begin, JOB_SPECIAL, (Job *)0);
1.1       deraadt  2497:        while (nJobs) {
                   2498:            Job_CatchOutput();
                   2499: #ifndef RMT_WILL_WATCH
1.2       deraadt  2500:            Job_CatchChildren(!usePipes);
1.1       deraadt  2501: #endif /* RMT_WILL_WATCH */
                   2502:        }
                   2503:     }
1.2       deraadt  2504:     postCommands = Targ_FindNode(".END", TARG_CREATE);
1.1       deraadt  2505: }
                   2506:
                   2507: /*-
                   2508:  *-----------------------------------------------------------------------
                   2509:  * Job_Full --
                   2510:  *     See if the job table is full. It is considered full if it is OR
                   2511:  *     if we are in the process of aborting OR if we have
                   2512:  *     reached/exceeded our local quota. This prevents any more jobs
                   2513:  *     from starting up.
                   2514:  *
                   2515:  * Results:
                   2516:  *     TRUE if the job table is full, FALSE otherwise
                   2517:  * Side Effects:
                   2518:  *     None.
                   2519:  *-----------------------------------------------------------------------
                   2520:  */
                   2521: Boolean
1.2       deraadt  2522: Job_Full()
1.1       deraadt  2523: {
1.2       deraadt  2524:     return(aborting || jobFull);
1.1       deraadt  2525: }
                   2526:
                   2527: /*-
                   2528:  *-----------------------------------------------------------------------
                   2529:  * Job_Empty --
                   2530:  *     See if the job table is empty.  Because the local concurrency may
                   2531:  *     be set to 0, it is possible for the job table to become empty,
                   2532:  *     while the list of stoppedJobs remains non-empty. In such a case,
                   2533:  *     we want to restart as many jobs as we can.
                   2534:  *
                   2535:  * Results:
                   2536:  *     TRUE if it is. FALSE if it ain't.
                   2537:  *
                   2538:  * Side Effects:
                   2539:  *     None.
                   2540:  *
                   2541:  * -----------------------------------------------------------------------
                   2542:  */
                   2543: Boolean
1.2       deraadt  2544: Job_Empty()
1.1       deraadt  2545: {
                   2546:     if (nJobs == 0) {
                   2547:        if (!Lst_IsEmpty(stoppedJobs) && !aborting) {
                   2548:            /*
                   2549:             * The job table is obviously not full if it has no jobs in
                   2550:             * it...Try and restart the stopped jobs.
                   2551:             */
                   2552:            jobFull = FALSE;
1.2       deraadt  2553:            JobRestartJobs();
1.1       deraadt  2554:            return(FALSE);
                   2555:        } else {
                   2556:            return(TRUE);
                   2557:        }
                   2558:     } else {
                   2559:        return(FALSE);
                   2560:     }
                   2561: }
                   2562:
                   2563: /*-
                   2564:  *-----------------------------------------------------------------------
                   2565:  * JobMatchShell --
                   2566:  *     Find a matching shell in 'shells' given its final component.
                   2567:  *
                   2568:  * Results:
                   2569:  *     A pointer to the Shell structure.
                   2570:  *
                   2571:  * Side Effects:
                   2572:  *     None.
                   2573:  *
                   2574:  *-----------------------------------------------------------------------
                   2575:  */
                   2576: static Shell *
1.2       deraadt  2577: JobMatchShell(name)
1.1       deraadt  2578:     char         *name;      /* Final component of shell path */
                   2579: {
                   2580:     register Shell *sh;              /* Pointer into shells table */
                   2581:     Shell         *match;    /* Longest-matching shell */
                   2582:     register char *cp1,
                   2583:                  *cp2;
                   2584:     char         *eoname;
                   2585:
1.2       deraadt  2586:     eoname = name + strlen(name);
1.1       deraadt  2587:
1.2       deraadt  2588:     match = NULL;
1.1       deraadt  2589:
                   2590:     for (sh = shells; sh->name != NULL; sh++) {
1.2       deraadt  2591:        for (cp1 = eoname - strlen(sh->name), cp2 = sh->name;
1.1       deraadt  2592:             *cp1 != '\0' && *cp1 == *cp2;
                   2593:             cp1++, cp2++) {
                   2594:                 continue;
                   2595:        }
                   2596:        if (*cp1 != *cp2) {
                   2597:            continue;
1.2       deraadt  2598:        } else if (match == NULL || strlen(match->name) < strlen(sh->name)) {
                   2599:           match = sh;
1.1       deraadt  2600:        }
                   2601:     }
1.2       deraadt  2602:     return(match == NULL ? sh : match);
1.1       deraadt  2603: }
                   2604:
                   2605: /*-
                   2606:  *-----------------------------------------------------------------------
                   2607:  * Job_ParseShell --
                   2608:  *     Parse a shell specification and set up commandShell, shellPath
                   2609:  *     and shellName appropriately.
                   2610:  *
                   2611:  * Results:
                   2612:  *     FAILURE if the specification was incorrect.
                   2613:  *
                   2614:  * Side Effects:
                   2615:  *     commandShell points to a Shell structure (either predefined or
                   2616:  *     created from the shell spec), shellPath is the full path of the
                   2617:  *     shell described by commandShell, while shellName is just the
                   2618:  *     final component of shellPath.
                   2619:  *
                   2620:  * Notes:
                   2621:  *     A shell specification consists of a .SHELL target, with dependency
                   2622:  *     operator, followed by a series of blank-separated words. Double
                   2623:  *     quotes can be used to use blanks in words. A backslash escapes
                   2624:  *     anything (most notably a double-quote and a space) and
                   2625:  *     provides the functionality it does in C. Each word consists of
                   2626:  *     keyword and value separated by an equal sign. There should be no
                   2627:  *     unnecessary spaces in the word. The keywords are as follows:
                   2628:  *         name            Name of shell.
                   2629:  *         path            Location of shell. Overrides "name" if given
                   2630:  *         quiet           Command to turn off echoing.
                   2631:  *         echo            Command to turn echoing on
                   2632:  *         filter          Result of turning off echoing that shouldn't be
                   2633:  *                         printed.
                   2634:  *         echoFlag        Flag to turn echoing on at the start
                   2635:  *         errFlag         Flag to turn error checking on at the start
                   2636:  *         hasErrCtl       True if shell has error checking control
                   2637:  *         check           Command to turn on error checking if hasErrCtl
                   2638:  *                         is TRUE or template of command to echo a command
                   2639:  *                         for which error checking is off if hasErrCtl is
                   2640:  *                         FALSE.
                   2641:  *         ignore          Command to turn off error checking if hasErrCtl
                   2642:  *                         is TRUE or template of command to execute a
                   2643:  *                         command so as to ignore any errors it returns if
                   2644:  *                         hasErrCtl is FALSE.
                   2645:  *
                   2646:  *-----------------------------------------------------------------------
                   2647:  */
                   2648: ReturnStatus
1.2       deraadt  2649: Job_ParseShell(line)
1.1       deraadt  2650:     char         *line;  /* The shell spec */
                   2651: {
                   2652:     char         **words;
                   2653:     int                  wordCount;
                   2654:     register char **argv;
                   2655:     register int  argc;
                   2656:     char         *path;
                   2657:     Shell        newShell;
                   2658:     Boolean      fullSpec = FALSE;
                   2659:
1.2       deraadt  2660:     while (isspace(*line)) {
1.1       deraadt  2661:        line++;
                   2662:     }
1.2       deraadt  2663:     words = brk_string(line, &wordCount, TRUE);
1.1       deraadt  2664:
1.2       deraadt  2665:     memset((Address)&newShell, 0, sizeof(newShell));
1.6       millert  2666:
1.1       deraadt  2667:     /*
                   2668:      * Parse the specification by keyword
                   2669:      */
1.2       deraadt  2670:     for (path = NULL, argc = wordCount - 1, argv = words + 1;
1.1       deraadt  2671:         argc != 0;
                   2672:         argc--, argv++) {
1.2       deraadt  2673:             if (strncmp(*argv, "path=", 5) == 0) {
1.1       deraadt  2674:                 path = &argv[0][5];
1.2       deraadt  2675:             } else if (strncmp(*argv, "name=", 5) == 0) {
1.1       deraadt  2676:                 newShell.name = &argv[0][5];
                   2677:             } else {
1.2       deraadt  2678:                 if (strncmp(*argv, "quiet=", 6) == 0) {
1.1       deraadt  2679:                     newShell.echoOff = &argv[0][6];
1.2       deraadt  2680:                 } else if (strncmp(*argv, "echo=", 5) == 0) {
1.1       deraadt  2681:                     newShell.echoOn = &argv[0][5];
1.2       deraadt  2682:                 } else if (strncmp(*argv, "filter=", 7) == 0) {
1.1       deraadt  2683:                     newShell.noPrint = &argv[0][7];
                   2684:                     newShell.noPLen = strlen(newShell.noPrint);
1.2       deraadt  2685:                 } else if (strncmp(*argv, "echoFlag=", 9) == 0) {
1.1       deraadt  2686:                     newShell.echo = &argv[0][9];
1.2       deraadt  2687:                 } else if (strncmp(*argv, "errFlag=", 8) == 0) {
1.1       deraadt  2688:                     newShell.exit = &argv[0][8];
1.2       deraadt  2689:                 } else if (strncmp(*argv, "hasErrCtl=", 10) == 0) {
1.1       deraadt  2690:                     char c = argv[0][10];
                   2691:                     newShell.hasErrCtl = !((c != 'Y') && (c != 'y') &&
1.2       deraadt  2692:                                           (c != 'T') && (c != 't'));
                   2693:                 } else if (strncmp(*argv, "check=", 6) == 0) {
1.1       deraadt  2694:                     newShell.errCheck = &argv[0][6];
1.2       deraadt  2695:                 } else if (strncmp(*argv, "ignore=", 7) == 0) {
1.1       deraadt  2696:                     newShell.ignErr = &argv[0][7];
                   2697:                 } else {
1.2       deraadt  2698:                     Parse_Error(PARSE_FATAL, "Unknown keyword \"%s\"",
1.1       deraadt  2699:                                  *argv);
1.2       deraadt  2700:                     return(FAILURE);
1.1       deraadt  2701:                 }
                   2702:                 fullSpec = TRUE;
                   2703:             }
                   2704:     }
                   2705:
1.2       deraadt  2706:     if (path == NULL) {
1.1       deraadt  2707:        /*
                   2708:         * If no path was given, the user wants one of the pre-defined shells,
                   2709:         * yes? So we find the one s/he wants with the help of JobMatchShell
                   2710:         * and set things up the right way. shellPath will be set up by
                   2711:         * Job_Init.
                   2712:         */
1.2       deraadt  2713:        if (newShell.name == NULL) {
                   2714:            Parse_Error(PARSE_FATAL, "Neither path nor name specified");
                   2715:            return(FAILURE);
1.1       deraadt  2716:        } else {
1.2       deraadt  2717:            commandShell = JobMatchShell(newShell.name);
1.1       deraadt  2718:            shellName = newShell.name;
                   2719:        }
                   2720:     } else {
                   2721:        /*
1.6       millert  2722:         * The user provided a path. If s/he gave nothing else (fullSpec is
1.1       deraadt  2723:         * FALSE), try and find a matching shell in the ones we know of.
                   2724:         * Else we just take the specification at its word and copy it
                   2725:         * to a new location. In either case, we need to record the
                   2726:         * path the user gave for the shell.
                   2727:         */
                   2728:        shellPath = path;
1.2       deraadt  2729:        path = strrchr(path, '/');
                   2730:        if (path == NULL) {
1.1       deraadt  2731:            path = shellPath;
                   2732:        } else {
                   2733:            path += 1;
                   2734:        }
1.2       deraadt  2735:        if (newShell.name != NULL) {
1.1       deraadt  2736:            shellName = newShell.name;
                   2737:        } else {
                   2738:            shellName = path;
                   2739:        }
                   2740:        if (!fullSpec) {
1.2       deraadt  2741:            commandShell = JobMatchShell(shellName);
1.1       deraadt  2742:        } else {
                   2743:            commandShell = (Shell *) emalloc(sizeof(Shell));
                   2744:            *commandShell = newShell;
                   2745:        }
                   2746:     }
                   2747:
                   2748:     if (commandShell->echoOn && commandShell->echoOff) {
                   2749:        commandShell->hasEchoCtl = TRUE;
                   2750:     }
1.6       millert  2751:
1.1       deraadt  2752:     if (!commandShell->hasErrCtl) {
1.2       deraadt  2753:        if (commandShell->errCheck == NULL) {
1.1       deraadt  2754:            commandShell->errCheck = "";
                   2755:        }
1.2       deraadt  2756:        if (commandShell->ignErr == NULL) {
1.1       deraadt  2757:            commandShell->ignErr = "%s\n";
                   2758:        }
                   2759:     }
1.6       millert  2760:
1.1       deraadt  2761:     /*
                   2762:      * Do not free up the words themselves, since they might be in use by the
                   2763:      * shell specification...
                   2764:      */
1.2       deraadt  2765:     free(words);
1.1       deraadt  2766:     return SUCCESS;
                   2767: }
                   2768:
                   2769: /*-
                   2770:  *-----------------------------------------------------------------------
                   2771:  * JobInterrupt --
                   2772:  *     Handle the receipt of an interrupt.
                   2773:  *
                   2774:  * Results:
                   2775:  *     None
                   2776:  *
                   2777:  * Side Effects:
                   2778:  *     All children are killed. Another job will be started if the
                   2779:  *     .INTERRUPT target was given.
                   2780:  *-----------------------------------------------------------------------
                   2781:  */
                   2782: static void
1.2       deraadt  2783: JobInterrupt(runINTERRUPT, signo)
1.1       deraadt  2784:     int            runINTERRUPT;       /* Non-zero if commands for the .INTERRUPT
                   2785:                                 * target should be executed */
1.2       deraadt  2786:     int            signo;              /* signal received */
1.1       deraadt  2787: {
                   2788:     LstNode      ln;           /* element in job table */
                   2789:     Job           *job;                /* job descriptor in that element */
                   2790:     GNode         *interrupt;  /* the node describing the .INTERRUPT target */
1.6       millert  2791:
1.1       deraadt  2792:     aborting = ABORT_INTERRUPT;
                   2793:
1.2       deraadt  2794:    (void) Lst_Open(jobs);
                   2795:     while ((ln = Lst_Next(jobs)) != NILLNODE) {
                   2796:        job = (Job *) Lst_Datum(ln);
1.1       deraadt  2797:
1.2       deraadt  2798:        if (!Targ_Precious(job->node)) {
                   2799:            char        *file = (job->node->path == NULL ?
1.1       deraadt  2800:                                 job->node->name :
                   2801:                                 job->node->path);
1.2       deraadt  2802:            if (!noExecute && eunlink(file) != -1) {
                   2803:                Error("*** %s removed", file);
1.1       deraadt  2804:            }
                   2805:        }
                   2806: #ifdef RMT_WANTS_SIGNALS
                   2807:        if (job->flags & JOB_REMOTE) {
                   2808:            /*
                   2809:             * If job is remote, let the Rmt module do the killing.
                   2810:             */
1.2       deraadt  2811:            if (!Rmt_Signal(job, signo)) {
1.1       deraadt  2812:                /*
                   2813:                 * If couldn't kill the thing, finish it out now with an
                   2814:                 * error code, since no exit report will come in likely.
                   2815:                 */
1.2       deraadt  2816:                int status;
1.1       deraadt  2817:
                   2818:                status.w_status = 0;
                   2819:                status.w_retcode = 1;
1.2       deraadt  2820:                JobFinish(job, &status);
1.1       deraadt  2821:            }
                   2822:        } else if (job->pid) {
1.2       deraadt  2823:            KILL(job->pid, signo);
1.1       deraadt  2824:        }
                   2825: #else
                   2826:        if (job->pid) {
1.2       deraadt  2827:            if (DEBUG(JOB)) {
                   2828:                (void) fprintf(stdout,
1.6       millert  2829:                               "JobInterrupt passing signal to child %d.\n",
1.2       deraadt  2830:                               job->pid);
                   2831:                (void) fflush(stdout);
                   2832:            }
                   2833:            KILL(job->pid, signo);
                   2834:        }
                   2835: #endif /* RMT_WANTS_SIGNALS */
                   2836:     }
                   2837:
                   2838: #ifdef REMOTE
                   2839:    (void)Lst_Open(stoppedJobs);
                   2840:     while ((ln = Lst_Next(stoppedJobs)) != NILLNODE) {
                   2841:        job = (Job *) Lst_Datum(ln);
                   2842:
                   2843:        if (job->flags & JOB_RESTART) {
                   2844:            if (DEBUG(JOB)) {
                   2845:                (void) fprintf(stdout, "%s%s",
                   2846:                               "JobInterrupt skipping job on stopped queue",
                   2847:                               "-- it was waiting to be restarted.\n");
                   2848:                (void) fflush(stdout);
                   2849:            }
                   2850:            continue;
                   2851:        }
                   2852:        if (!Targ_Precious(job->node)) {
                   2853:            char        *file = (job->node->path == NULL ?
                   2854:                                 job->node->name :
                   2855:                                 job->node->path);
                   2856:            if (eunlink(file) == 0) {
                   2857:                Error("*** %s removed", file);
                   2858:            }
                   2859:        }
                   2860:        /*
                   2861:         * Resume the thing so it will take the signal.
                   2862:         */
                   2863:        if (DEBUG(JOB)) {
                   2864:            (void) fprintf(stdout,
1.6       millert  2865:                           "JobInterrupt passing CONT to stopped child %d.\n",
1.2       deraadt  2866:                           job->pid);
                   2867:            (void) fflush(stdout);
                   2868:        }
                   2869:        KILL(job->pid, SIGCONT);
                   2870: #ifdef RMT_WANTS_SIGNALS
                   2871:        if (job->flags & JOB_REMOTE) {
                   2872:            /*
                   2873:             * If job is remote, let the Rmt module do the killing.
                   2874:             */
                   2875:            if (!Rmt_Signal(job, SIGINT)) {
                   2876:                /*
                   2877:                 * If couldn't kill the thing, finish it out now with an
                   2878:                 * error code, since no exit report will come in likely.
                   2879:                 */
                   2880:                int status;
                   2881:                status.w_status = 0;
                   2882:                status.w_retcode = 1;
                   2883:                JobFinish(job, &status);
                   2884:            }
                   2885:        } else if (job->pid) {
                   2886:            if (DEBUG(JOB)) {
                   2887:                (void) fprintf(stdout,
                   2888:                       "JobInterrupt passing interrupt to stopped child %d.\n",
                   2889:                               job->pid);
                   2890:                (void) fflush(stdout);
                   2891:            }
1.1       deraadt  2892:            KILL(job->pid, SIGINT);
                   2893:        }
                   2894: #endif /* RMT_WANTS_SIGNALS */
                   2895:     }
1.2       deraadt  2896: #endif
                   2897:     Lst_Close(stoppedJobs);
1.1       deraadt  2898:
                   2899:     if (runINTERRUPT && !touchFlag) {
1.2       deraadt  2900:        interrupt = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
1.1       deraadt  2901:        if (interrupt != NILGNODE) {
                   2902:            ignoreErrors = FALSE;
                   2903:
1.2       deraadt  2904:            JobStart(interrupt, JOB_IGNDOTS, (Job *)0);
1.1       deraadt  2905:            while (nJobs) {
                   2906:                Job_CatchOutput();
                   2907: #ifndef RMT_WILL_WATCH
1.2       deraadt  2908:                Job_CatchChildren(!usePipes);
1.1       deraadt  2909: #endif /* RMT_WILL_WATCH */
                   2910:            }
                   2911:        }
                   2912:     }
1.2       deraadt  2913:     (void) eunlink(tfile);
                   2914:     exit(signo);
1.1       deraadt  2915: }
                   2916:
                   2917: /*
                   2918:  *-----------------------------------------------------------------------
                   2919:  * Job_End --
                   2920:  *     Do final processing such as the running of the commands
1.6       millert  2921:  *     attached to the .END target.
1.1       deraadt  2922:  *
                   2923:  * Results:
                   2924:  *     Number of errors reported.
                   2925:  *
                   2926:  * Side Effects:
1.6       millert  2927:  *     The process' temporary file (tfile) is removed if it still
1.1       deraadt  2928:  *     existed.
                   2929:  *-----------------------------------------------------------------------
                   2930:  */
                   2931: int
1.2       deraadt  2932: Job_End()
1.1       deraadt  2933: {
1.2       deraadt  2934:     if (postCommands != NILGNODE && !Lst_IsEmpty(postCommands->commands)) {
1.1       deraadt  2935:        if (errors) {
1.2       deraadt  2936:            Error("Errors reported so .END ignored");
1.1       deraadt  2937:        } else {
1.2       deraadt  2938:            JobStart(postCommands, JOB_SPECIAL | JOB_IGNDOTS, NULL);
1.1       deraadt  2939:
                   2940:            while (nJobs) {
                   2941:                Job_CatchOutput();
                   2942: #ifndef RMT_WILL_WATCH
1.2       deraadt  2943:                Job_CatchChildren(!usePipes);
1.1       deraadt  2944: #endif /* RMT_WILL_WATCH */
                   2945:            }
                   2946:        }
                   2947:     }
1.2       deraadt  2948:     (void) eunlink(tfile);
1.1       deraadt  2949:     return(errors);
                   2950: }
                   2951:
                   2952: /*-
                   2953:  *-----------------------------------------------------------------------
                   2954:  * Job_Wait --
                   2955:  *     Waits for all running jobs to finish and returns. Sets 'aborting'
                   2956:  *     to ABORT_WAIT to prevent other jobs from starting.
                   2957:  *
                   2958:  * Results:
                   2959:  *     None.
                   2960:  *
                   2961:  * Side Effects:
                   2962:  *     Currently running jobs finish.
                   2963:  *
                   2964:  *-----------------------------------------------------------------------
                   2965:  */
                   2966: void
                   2967: Job_Wait()
                   2968: {
                   2969:     aborting = ABORT_WAIT;
                   2970:     while (nJobs != 0) {
                   2971:        Job_CatchOutput();
                   2972: #ifndef RMT_WILL_WATCH
                   2973:        Job_CatchChildren(!usePipes);
                   2974: #endif /* RMT_WILL_WATCH */
                   2975:     }
                   2976:     aborting = 0;
                   2977: }
                   2978:
                   2979: /*-
                   2980:  *-----------------------------------------------------------------------
                   2981:  * Job_AbortAll --
                   2982:  *     Abort all currently running jobs without handling output or anything.
                   2983:  *     This function is to be called only in the event of a major
                   2984:  *     error. Most definitely NOT to be called from JobInterrupt.
                   2985:  *
                   2986:  * Results:
                   2987:  *     None
                   2988:  *
                   2989:  * Side Effects:
                   2990:  *     All children are killed, not just the firstborn
                   2991:  *-----------------------------------------------------------------------
                   2992:  */
                   2993: void
1.2       deraadt  2994: Job_AbortAll()
1.1       deraadt  2995: {
1.2       deraadt  2996:     LstNode            ln;     /* element in job table */
1.1       deraadt  2997:     Job                *job;   /* the job descriptor in that element */
                   2998:     int                foo;
1.6       millert  2999:
1.1       deraadt  3000:     aborting = ABORT_ERROR;
1.6       millert  3001:
1.1       deraadt  3002:     if (nJobs) {
                   3003:
1.2       deraadt  3004:        (void) Lst_Open(jobs);
                   3005:        while ((ln = Lst_Next(jobs)) != NILLNODE) {
                   3006:            job = (Job *) Lst_Datum(ln);
1.1       deraadt  3007:
                   3008:            /*
                   3009:             * kill the child process with increasingly drastic signals to make
1.6       millert  3010:             * darn sure it's dead.
1.1       deraadt  3011:             */
                   3012: #ifdef RMT_WANTS_SIGNALS
                   3013:            if (job->flags & JOB_REMOTE) {
                   3014:                Rmt_Signal(job, SIGINT);
                   3015:                Rmt_Signal(job, SIGKILL);
                   3016:            } else {
                   3017:                KILL(job->pid, SIGINT);
                   3018:                KILL(job->pid, SIGKILL);
                   3019:            }
                   3020: #else
                   3021:            KILL(job->pid, SIGINT);
                   3022:            KILL(job->pid, SIGKILL);
                   3023: #endif /* RMT_WANTS_SIGNALS */
                   3024:        }
                   3025:     }
1.6       millert  3026:
1.1       deraadt  3027:     /*
                   3028:      * Catch as many children as want to report in at first, then give up
                   3029:      */
1.2       deraadt  3030:     while (waitpid((pid_t) -1, &foo, WNOHANG) > 0)
1.1       deraadt  3031:        continue;
1.2       deraadt  3032:     (void) eunlink(tfile);
                   3033: }
                   3034:
                   3035: #ifdef REMOTE
                   3036: /*-
                   3037:  *-----------------------------------------------------------------------
                   3038:  * JobFlagForMigration --
                   3039:  *     Handle the eviction of a child. Called from RmtStatusChange.
                   3040:  *     Flags the child as remigratable and then suspends it.
                   3041:  *
                   3042:  * Results:
                   3043:  *     none.
                   3044:  *
                   3045:  * Side Effects:
                   3046:  *     The job descriptor is flagged for remigration.
                   3047:  *
                   3048:  *-----------------------------------------------------------------------
                   3049:  */
                   3050: void
                   3051: JobFlagForMigration(hostID)
                   3052:     int          hostID;       /* ID of host we used, for matching children. */
                   3053: {
                   3054:     register Job  *job;                /* job descriptor for dead child */
                   3055:     LstNode       jnode;       /* list element for finding job */
                   3056:
                   3057:     if (DEBUG(JOB)) {
                   3058:        (void) fprintf(stdout, "JobFlagForMigration(%d) called.\n", hostID);
                   3059:        (void) fflush(stdout);
                   3060:     }
                   3061:     jnode = Lst_Find(jobs, (ClientData)hostID, JobCmpRmtID);
                   3062:
                   3063:     if (jnode == NILLNODE) {
                   3064:        jnode = Lst_Find(stoppedJobs, (ClientData)hostID, JobCmpRmtID);
                   3065:                if (jnode == NILLNODE) {
                   3066:                    if (DEBUG(JOB)) {
                   3067:                        Error("Evicting host(%d) not in table", hostID);
                   3068:                    }
                   3069:                    return;
                   3070:                }
                   3071:     }
                   3072:     job = (Job *) Lst_Datum(jnode);
                   3073:
                   3074:     if (DEBUG(JOB)) {
                   3075:        (void) fprintf(stdout,
                   3076:                       "JobFlagForMigration(%d) found job '%s'.\n", hostID,
                   3077:                       job->node->name);
                   3078:        (void) fflush(stdout);
                   3079:     }
                   3080:
                   3081:     KILL(job->pid, SIGSTOP);
                   3082:
                   3083:     job->flags |= JOB_REMIGRATE;
                   3084: }
                   3085:
                   3086: #endif
                   3087: 
                   3088: /*-
                   3089:  *-----------------------------------------------------------------------
                   3090:  * JobRestartJobs --
                   3091:  *     Tries to restart stopped jobs if there are slots available.
                   3092:  *     Note that this tries to restart them regardless of pending errors.
                   3093:  *     It's not good to leave stopped jobs lying around!
                   3094:  *
                   3095:  * Results:
                   3096:  *     None.
                   3097:  *
                   3098:  * Side Effects:
                   3099:  *     Resumes(and possibly migrates) jobs.
                   3100:  *
                   3101:  *-----------------------------------------------------------------------
                   3102:  */
                   3103: static void
                   3104: JobRestartJobs()
                   3105: {
                   3106:     while (!jobFull && !Lst_IsEmpty(stoppedJobs)) {
                   3107:        if (DEBUG(JOB)) {
                   3108:            (void) fprintf(stdout,
                   3109:                       "Job queue is not full. Restarting a stopped job.\n");
                   3110:            (void) fflush(stdout);
                   3111:        }
                   3112:        JobRestart((Job *)Lst_DeQueue(stoppedJobs));
                   3113:     }
1.1       deraadt  3114: }