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

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