[BACK]Return to job.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / usr.bin / make

Annotation of src/usr.bin/make/job.c, Revision 1.34

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

CVSweb <webmaster@jp.NetBSD.org>