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

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