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