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

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

CVSweb <webmaster@jp.NetBSD.org>