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

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

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