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

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