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

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