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

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