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