Annotation of src/usr.bin/make/job.c, Revision 1.39
1.39 ! espie 1: /* $OpenBSD: job.c,v 1.38 2000/11/24 14:36:34 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.39 ! espie 129: static char rcsid[] = "$OpenBSD: job.c,v 1.38 2000/11/24 14:36:34 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 */
1012:
1013: if (gn->type & (OP_JOIN|OP_USE|OP_EXEC|OP_OPTIONAL)) {
1014: /*
1015: * .JOIN, .USE, .ZEROTIME and .OPTIONAL targets are "virtual" targets
1016: * and, as such, shouldn't really be created.
1017: */
1018: return;
1019: }
1.6 millert 1020:
1.1 deraadt 1021: if (!silent) {
1.2 deraadt 1022: (void) fprintf(stdout, "touch %s\n", gn->name);
1023: (void) fflush(stdout);
1.1 deraadt 1024: }
1025:
1026: if (noExecute) {
1027: return;
1028: }
1029:
1030: if (gn->type & OP_ARCHV) {
1.2 deraadt 1031: Arch_Touch(gn);
1.1 deraadt 1032: } else if (gn->type & OP_LIB) {
1.2 deraadt 1033: Arch_TouchLib(gn);
1.1 deraadt 1034: } else {
1035: char *file = gn->path ? gn->path : gn->name;
1036:
1.38 espie 1037: if (set_times(file) == -1) {
1.2 deraadt 1038: streamID = open(file, O_RDWR | O_CREAT, 0666);
1.1 deraadt 1039:
1040: if (streamID >= 0) {
1041: char c;
1042:
1043: /*
1044: * Read and write a byte to the file to change the
1045: * modification time, then close the file.
1046: */
1047: if (read(streamID, &c, 1) == 1) {
1.11 millert 1048: (void) lseek(streamID, 0, SEEK_SET);
1.2 deraadt 1049: (void) write(streamID, &c, 1);
1.1 deraadt 1050: }
1.6 millert 1051:
1.2 deraadt 1052: (void) close(streamID);
1053: } else {
1054: (void) fprintf(stdout, "*** couldn't touch %s: %s",
1055: file, strerror(errno));
1056: (void) fflush(stdout);
1057: }
1.1 deraadt 1058: }
1059: }
1060: }
1061:
1062: /*-
1063: *-----------------------------------------------------------------------
1064: * Job_CheckCommands --
1.6 millert 1065: * Make sure the given node has all the commands it needs.
1.1 deraadt 1066: *
1067: * Results:
1068: * TRUE if the commands list is/was ok.
1069: *
1070: * Side Effects:
1071: * The node will have commands from the .DEFAULT rule added to it
1072: * if it needs them.
1073: *-----------------------------------------------------------------------
1074: */
1075: Boolean
1.2 deraadt 1076: Job_CheckCommands(gn, abortProc)
1.1 deraadt 1077: GNode *gn; /* The target whose commands need
1078: * verifying */
1.6 millert 1079: void (*abortProc) __P((char *, ...));
1.1 deraadt 1080: /* Function to abort with message */
1081: {
1.29 espie 1082: if (OP_NOP(gn->type) && Lst_IsEmpty(&gn->commands) &&
1.1 deraadt 1083: (gn->type & OP_LIB) == 0) {
1084: /*
1085: * No commands. Look for .DEFAULT rule from which we might infer
1.6 millert 1086: * commands
1.1 deraadt 1087: */
1.29 espie 1088: if ((DEFAULT != NULL) && !Lst_IsEmpty(&DEFAULT->commands)) {
1.1 deraadt 1089: /*
1090: * Make only looks for a .DEFAULT if the node was never the
1091: * target of an operator, so that's what we do too. If
1092: * a .DEFAULT was given, we substitute its commands for gn's
1093: * commands and set the IMPSRC variable to be the target's name
1094: * The DEFAULT node acts like a transformation rule, in that
1095: * gn also inherits any attributes or sources attached to
1096: * .DEFAULT itself.
1097: */
1098: Make_HandleUse(DEFAULT, gn);
1.31 espie 1099: Varq_Set(IMPSRC_INDEX, Varq_Value(TARGET_INDEX, gn), gn);
1.37 espie 1100: } else if (is_out_of_date(Dir_MTime(gn))) {
1.1 deraadt 1101: /*
1102: * The node wasn't the target of an operator we have no .DEFAULT
1103: * rule to go on and the target doesn't already exist. There's
1104: * nothing more we can do for this branch. If the -k flag wasn't
1105: * given, we stop in our tracks, otherwise we just don't update
1.6 millert 1106: * this node's parents so they never get examined.
1.1 deraadt 1107: */
1.2 deraadt 1108: static const char msg[] = "make: don't know how to make";
1109:
1.1 deraadt 1110: if (gn->type & OP_OPTIONAL) {
1.2 deraadt 1111: (void) fprintf(stdout, "%s %s(ignored)\n", msg, gn->name);
1112: (void) fflush(stdout);
1.1 deraadt 1113: } else if (keepgoing) {
1.2 deraadt 1114: (void) fprintf(stdout, "%s %s(continuing)\n", msg, gn->name);
1115: (void) fflush(stdout);
1116: return FALSE;
1.1 deraadt 1117: } else {
1.39 ! espie 1118: (*abortProc)("%s %s. Stop in %s.", msg, gn->name,
! 1119: Var_Value(".CURDIR", VAR_GLOBAL));
1.2 deraadt 1120: return FALSE;
1.1 deraadt 1121: }
1122: }
1123: }
1.2 deraadt 1124: return TRUE;
1.1 deraadt 1125: }
1126: #ifdef RMT_WILL_WATCH
1127: /*-
1128: *-----------------------------------------------------------------------
1129: * JobLocalInput --
1130: * Handle a pipe becoming readable. Callback function for Rmt_Watch
1131: *
1132: * Results:
1133: * None
1134: *
1135: * Side Effects:
1136: * JobDoOutput is called.
1.6 millert 1137: *
1.1 deraadt 1138: *-----------------------------------------------------------------------
1139: */
1140: /*ARGSUSED*/
1141: static void
1142: JobLocalInput(stream, job)
1.6 millert 1143: int stream; /* Stream that's ready (ignored) */
1.1 deraadt 1144: Job *job; /* Job to which the stream belongs */
1145: {
1146: JobDoOutput(job, FALSE);
1147: }
1148: #endif /* RMT_WILL_WATCH */
1149:
1150: /*-
1151: *-----------------------------------------------------------------------
1152: * JobExec --
1153: * Execute the shell for the given job. Called from JobStart and
1154: * JobRestart.
1155: *
1156: * Results:
1157: * None.
1158: *
1159: * Side Effects:
1160: * A shell is executed, outputs is altered and the Job structure added
1161: * to the job table.
1162: *
1163: *-----------------------------------------------------------------------
1164: */
1165: static void
1166: JobExec(job, argv)
1167: Job *job; /* Job to execute */
1168: char **argv;
1169: {
1170: int cpid; /* ID of new child */
1.6 millert 1171:
1.1 deraadt 1172: if (DEBUG(JOB)) {
1173: int i;
1.6 millert 1174:
1.2 deraadt 1175: (void) fprintf(stdout, "Running %s %sly\n", job->node->name,
1176: job->flags&JOB_REMOTE?"remote":"local");
1177: (void) fprintf(stdout, "\tCommand: ");
1178: for (i = 0; argv[i] != NULL; i++) {
1179: (void) fprintf(stdout, "%s ", argv[i]);
1.1 deraadt 1180: }
1.2 deraadt 1181: (void) fprintf(stdout, "\n");
1182: (void) fflush(stdout);
1.1 deraadt 1183: }
1.6 millert 1184:
1.1 deraadt 1185: /*
1186: * Some jobs produce no output and it's disconcerting to have
1.6 millert 1187: * no feedback of their running (since they produce no output, the
1.1 deraadt 1188: * banner with their name in it never appears). This is an attempt to
1189: * provide that feedback, even if nothing follows it.
1190: */
1191: if ((lastNode != job->node) && (job->flags & JOB_FIRST) &&
1.2 deraadt 1192: !(job->flags & JOB_SILENT)) {
1193: MESSAGE(stdout, job->node);
1.1 deraadt 1194: lastNode = job->node;
1195: }
1.6 millert 1196:
1.1 deraadt 1197: #ifdef RMT_NO_EXEC
1198: if (job->flags & JOB_REMOTE) {
1199: goto jobExecFinish;
1200: }
1201: #endif /* RMT_NO_EXEC */
1202:
1.2 deraadt 1203: if ((cpid = vfork()) == -1) {
1204: Punt("Cannot fork");
1.1 deraadt 1205: } else if (cpid == 0) {
1206:
1207: /*
1208: * Must duplicate the input stream down to the child's input and
1.6 millert 1209: * reset it to the beginning (again). Since the stream was marked
1.1 deraadt 1210: * close-on-exec, we must clear that bit in the new input.
1211: */
1.2 deraadt 1212: if (dup2(FILENO(job->cmdFILE), 0) == -1)
1213: Punt("Cannot dup2: %s", strerror(errno));
1214: (void) fcntl(0, F_SETFD, 0);
1.11 millert 1215: (void) lseek(0, 0, SEEK_SET);
1.6 millert 1216:
1.1 deraadt 1217: if (usePipes) {
1218: /*
1219: * Set up the child's output to be routed through the pipe
1220: * we've created for it.
1221: */
1.2 deraadt 1222: if (dup2(job->outPipe, 1) == -1)
1223: Punt("Cannot dup2: %s", strerror(errno));
1.1 deraadt 1224: } else {
1225: /*
1226: * We're capturing output in a file, so we duplicate the
1227: * descriptor to the temporary file into the standard
1228: * output.
1229: */
1.2 deraadt 1230: if (dup2(job->outFd, 1) == -1)
1231: Punt("Cannot dup2: %s", strerror(errno));
1.1 deraadt 1232: }
1233: /*
1234: * The output channels are marked close on exec. This bit was
1.6 millert 1235: * duplicated by the dup2 (on some systems), so we have to clear
1.1 deraadt 1236: * it before routing the shell's error output to the same place as
1237: * its standard output.
1238: */
1.2 deraadt 1239: (void) fcntl(1, F_SETFD, 0);
1240: if (dup2(1, 2) == -1)
1241: Punt("Cannot dup2: %s", strerror(errno));
1.1 deraadt 1242:
1243: #ifdef USE_PGRP
1244: /*
1245: * We want to switch the child into a different process family so
1246: * we can kill it and all its descendants in one fell swoop,
1247: * by killing its process family, but not commit suicide.
1248: */
1.2 deraadt 1249: # if defined(SYSV)
1250: (void) setsid();
1251: # else
1252: (void) setpgid(0, getpid());
1253: # endif
1254: #endif /* USE_PGRP */
1.1 deraadt 1255:
1.2 deraadt 1256: #ifdef REMOTE
1257: if (job->flags & JOB_REMOTE) {
1258: Rmt_Exec(shellPath, argv, FALSE);
1.6 millert 1259: } else
1.2 deraadt 1260: #endif /* REMOTE */
1261: (void) execv(shellPath, argv);
1262:
1263: (void) write(2, "Could not execute shell\n",
1264: sizeof("Could not execute shell"));
1265: _exit(1);
1.1 deraadt 1266: } else {
1.2 deraadt 1267: #ifdef REMOTE
1.8 deraadt 1268: int omask = sigblock(sigmask(SIGCHLD));
1.2 deraadt 1269: #endif
1.1 deraadt 1270: job->pid = cpid;
1271:
1272: if (usePipes && (job->flags & JOB_FIRST) ) {
1273: /*
1274: * The first time a job is run for a node, we set the current
1275: * position in the buffer to the beginning and mark another
1276: * stream to watch in the outputs mask
1277: */
1278: job->curPos = 0;
1.6 millert 1279:
1.1 deraadt 1280: #ifdef RMT_WILL_WATCH
1281: Rmt_Watch(job->inPipe, JobLocalInput, job);
1282: #else
1.8 deraadt 1283: if (outputsp == NULL || job->inPipe > outputsn) {
1284: int bytes = howmany(job->inPipe+1, NFDBITS) * sizeof(fd_mask);
1285: int obytes = howmany(outputsn+1, NFDBITS) * sizeof(fd_mask);
1286:
1.9 flipk 1287: if (outputsp == NULL || obytes != bytes) {
1.8 deraadt 1288: outputsp = realloc(outputsp, bytes);
1289: if (outputsp == NULL)
1290: return;
1291: memset(outputsp + obytes, 0, bytes - obytes);
1292: }
1293: outputsn = job->inPipe;
1294: }
1295: FD_SET(job->inPipe, outputsp);
1.1 deraadt 1296: #endif /* RMT_WILL_WATCH */
1297: }
1298:
1299: if (job->flags & JOB_REMOTE) {
1.2 deraadt 1300: #ifndef REMOTE
1.1 deraadt 1301: job->rmtID = 0;
1.2 deraadt 1302: #else
1303: job->rmtID = Rmt_LastID(job->pid);
1304: #endif /* REMOTE */
1.1 deraadt 1305: } else {
1306: nLocal += 1;
1307: /*
1.2 deraadt 1308: * XXX: Used to not happen if REMOTE. Why?
1.1 deraadt 1309: */
1.2 deraadt 1310: if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
1311: (void) fclose(job->cmdFILE);
1.1 deraadt 1312: job->cmdFILE = NULL;
1313: }
1314: }
1.2 deraadt 1315: #ifdef REMOTE
1316: (void) sigsetmask(omask);
1317: #endif
1.1 deraadt 1318: }
1319:
1320: #ifdef RMT_NO_EXEC
1.6 millert 1321: jobExecFinish:
1.1 deraadt 1322: #endif
1323: /*
1324: * Now the job is actually running, add it to the table.
1325: */
1326: nJobs += 1;
1.29 espie 1327: Lst_AtEnd(&jobs, job);
1.1 deraadt 1328: if (nJobs == maxJobs) {
1329: jobFull = TRUE;
1330: }
1331: }
1332:
1333: /*-
1334: *-----------------------------------------------------------------------
1335: * JobMakeArgv --
1336: * Create the argv needed to execute the shell for a given job.
1.6 millert 1337: *
1.1 deraadt 1338: *
1339: * Results:
1340: *
1341: * Side Effects:
1342: *
1343: *-----------------------------------------------------------------------
1344: */
1345: static void
1346: JobMakeArgv(job, argv)
1347: Job *job;
1348: char **argv;
1349: {
1350: int argc;
1351: static char args[10]; /* For merged arguments */
1.6 millert 1352:
1.1 deraadt 1353: argv[0] = shellName;
1354: argc = 1;
1355:
1356: if ((commandShell->exit && (*commandShell->exit != '-')) ||
1357: (commandShell->echo && (*commandShell->echo != '-')))
1358: {
1359: /*
1360: * At least one of the flags doesn't have a minus before it, so
1361: * merge them together. Have to do this because the *(&(@*#*&#$#
1362: * Bourne shell thinks its second argument is a file to source.
1363: * Grrrr. Note the ten-character limitation on the combined arguments.
1364: */
1365: (void)sprintf(args, "-%s%s",
1366: ((job->flags & JOB_IGNERR) ? "" :
1367: (commandShell->exit ? commandShell->exit : "")),
1368: ((job->flags & JOB_SILENT) ? "" :
1369: (commandShell->echo ? commandShell->echo : "")));
1370:
1371: if (args[1]) {
1372: argv[argc] = args;
1373: argc++;
1374: }
1375: } else {
1376: if (!(job->flags & JOB_IGNERR) && commandShell->exit) {
1377: argv[argc] = commandShell->exit;
1378: argc++;
1379: }
1380: if (!(job->flags & JOB_SILENT) && commandShell->echo) {
1381: argv[argc] = commandShell->echo;
1382: argc++;
1383: }
1384: }
1.2 deraadt 1385: argv[argc] = NULL;
1.1 deraadt 1386: }
1387:
1388: /*-
1389: *-----------------------------------------------------------------------
1390: * JobRestart --
1.6 millert 1391: * Restart a job that stopped for some reason.
1.1 deraadt 1392: *
1393: * Results:
1394: * None.
1395: *
1396: * Side Effects:
1397: * jobFull will be set if the job couldn't be run.
1398: *
1399: *-----------------------------------------------------------------------
1400: */
1401: static void
1402: JobRestart(job)
1403: Job *job; /* Job to restart */
1404: {
1.2 deraadt 1405: #ifdef REMOTE
1406: int host;
1407: #endif
1.6 millert 1408:
1.1 deraadt 1409: if (job->flags & JOB_REMIGRATE) {
1.2 deraadt 1410: if (
1411: #ifdef REMOTE
1412: verboseRemigrates ||
1413: #endif
1414: DEBUG(JOB)) {
1415: (void) fprintf(stdout, "*** remigrating %x(%s)\n",
1416: job->pid, job->node->name);
1417: (void) fflush(stdout);
1.1 deraadt 1418: }
1.2 deraadt 1419:
1420: #ifdef REMOTE
1421: if (!Rmt_ReExport(job->pid, job->node, &host)) {
1422: if (verboseRemigrates || DEBUG(JOB)) {
1423: (void) fprintf(stdout, "*** couldn't migrate...\n");
1424: (void) fflush(stdout);
1425: }
1426: #endif
1427: if (nLocal != maxLocal) {
1.1 deraadt 1428: /*
1429: * Job cannot be remigrated, but there's room on the local
1430: * machine, so resume the job and note that another
1431: * local job has started.
1432: */
1.2 deraadt 1433: if (
1434: #ifdef REMOTE
1435: verboseRemigrates ||
1436: #endif
1437: DEBUG(JOB)) {
1438: (void) fprintf(stdout, "*** resuming on local machine\n");
1439: (void) fflush(stdout);
1440: }
1.1 deraadt 1441: KILL(job->pid, SIGCONT);
1442: nLocal +=1;
1.2 deraadt 1443: #ifdef REMOTE
1444: job->flags &= ~(JOB_REMIGRATE|JOB_RESUME|JOB_REMOTE);
1445: job->flags |= JOB_CONTINUING;
1446: #else
1.1 deraadt 1447: job->flags &= ~(JOB_REMIGRATE|JOB_RESUME);
1.2 deraadt 1448: #endif
1.1 deraadt 1449: } else {
1450: /*
1451: * Job cannot be restarted. Mark the table as full and
1452: * place the job back on the list of stopped jobs.
1453: */
1.2 deraadt 1454: if (
1455: #ifdef REMOTE
1456: verboseRemigrates ||
1457: #endif
1458: DEBUG(JOB)) {
1459: (void) fprintf(stdout, "*** holding\n");
1460: (void) fflush(stdout);
1461: }
1.29 espie 1462: Lst_AtFront(&stoppedJobs, job);
1.1 deraadt 1463: jobFull = TRUE;
1464: if (DEBUG(JOB)) {
1.2 deraadt 1465: (void) fprintf(stdout, "Job queue is full.\n");
1466: (void) fflush(stdout);
1.1 deraadt 1467: }
1468: return;
1.2 deraadt 1469: }
1470: #ifdef REMOTE
1471: } else {
1472: /*
1473: * Clear out the remigrate and resume flags. Set the continuing
1474: * flag so we know later on that the process isn't exiting just
1475: * because of a signal.
1476: */
1477: job->flags &= ~(JOB_REMIGRATE|JOB_RESUME);
1478: job->flags |= JOB_CONTINUING;
1479: job->rmtID = host;
1.1 deraadt 1480: }
1.2 deraadt 1481: #endif
1.6 millert 1482:
1.29 espie 1483: Lst_AtEnd(&jobs, job);
1.1 deraadt 1484: nJobs += 1;
1485: if (nJobs == maxJobs) {
1486: jobFull = TRUE;
1487: if (DEBUG(JOB)) {
1.2 deraadt 1488: (void) fprintf(stdout, "Job queue is full.\n");
1489: (void) fflush(stdout);
1.1 deraadt 1490: }
1491: }
1492: } else if (job->flags & JOB_RESTART) {
1493: /*
1494: * Set up the control arguments to the shell. This is based on the
1495: * flags set earlier for this job. If the JOB_IGNERR flag is clear,
1496: * the 'exit' flag of the commandShell is used to cause it to exit
1497: * upon receiving an error. If the JOB_SILENT flag is clear, the
1498: * 'echo' flag of the commandShell is used to get it to start echoing
1.6 millert 1499: * as soon as it starts processing commands.
1.1 deraadt 1500: */
1501: char *argv[4];
1.6 millert 1502:
1.1 deraadt 1503: JobMakeArgv(job, argv);
1.2 deraadt 1504:
1.1 deraadt 1505: if (DEBUG(JOB)) {
1.2 deraadt 1506: (void) fprintf(stdout, "Restarting %s...", job->node->name);
1507: (void) fflush(stdout);
1.1 deraadt 1508: }
1.2 deraadt 1509: #ifdef REMOTE
1510: if ((job->node->type&OP_NOEXPORT) ||
1.6 millert 1511: (nLocal < maxLocal && runLocalFirst)
1.2 deraadt 1512: # ifdef RMT_NO_EXEC
1513: || !Rmt_Export(shellPath, argv, job)
1514: # else
1515: || !Rmt_Begin(shellPath, argv, job->node)
1516: # endif
1517: #endif
1518: {
1519: if (((nLocal >= maxLocal) && !(job->flags & JOB_SPECIAL))) {
1.1 deraadt 1520: /*
1521: * Can't be exported and not allowed to run locally -- put it
1522: * back on the hold queue and mark the table full
1523: */
1524: if (DEBUG(JOB)) {
1.2 deraadt 1525: (void) fprintf(stdout, "holding\n");
1526: (void) fflush(stdout);
1.1 deraadt 1527: }
1.29 espie 1528: Lst_AtFront(&stoppedJobs, job);
1.1 deraadt 1529: jobFull = TRUE;
1530: if (DEBUG(JOB)) {
1.2 deraadt 1531: (void) fprintf(stdout, "Job queue is full.\n");
1532: (void) fflush(stdout);
1.1 deraadt 1533: }
1534: return;
1.2 deraadt 1535: } else {
1.1 deraadt 1536: /*
1537: * Job may be run locally.
1538: */
1539: if (DEBUG(JOB)) {
1.2 deraadt 1540: (void) fprintf(stdout, "running locally\n");
1541: (void) fflush(stdout);
1.1 deraadt 1542: }
1543: job->flags &= ~JOB_REMOTE;
1.2 deraadt 1544: }
1545: }
1546: #ifdef REMOTE
1547: else {
1548: /*
1549: * Can be exported. Hooray!
1550: */
1551: if (DEBUG(JOB)) {
1552: (void) fprintf(stdout, "exporting\n");
1553: (void) fflush(stdout);
1554: }
1555: job->flags |= JOB_REMOTE;
1.1 deraadt 1556: }
1.2 deraadt 1557: #endif
1.1 deraadt 1558: JobExec(job, argv);
1559: } else {
1560: /*
1561: * The job has stopped and needs to be restarted. Why it stopped,
1562: * we don't know...
1563: */
1564: if (DEBUG(JOB)) {
1.2 deraadt 1565: (void) fprintf(stdout, "Resuming %s...", job->node->name);
1566: (void) fflush(stdout);
1.1 deraadt 1567: }
1568: if (((job->flags & JOB_REMOTE) ||
1.2 deraadt 1569: (nLocal < maxLocal) ||
1570: #ifdef REMOTE
1571: (((job->flags & JOB_SPECIAL) &&
1572: (job->node->type & OP_NOEXPORT)) &&
1573: (maxLocal == 0))) &&
1574: #else
1575: ((job->flags & JOB_SPECIAL) &&
1576: (maxLocal == 0))) &&
1577: #endif
1578: (nJobs != maxJobs))
1.1 deraadt 1579: {
1580: /*
1581: * If the job is remote, it's ok to resume it as long as the
1582: * maximum concurrency won't be exceeded. If it's local and
1.6 millert 1583: * we haven't reached the local concurrency limit already (or the
1.1 deraadt 1584: * job must be run locally and maxLocal is 0), it's also ok to
1585: * resume it.
1586: */
1587: Boolean error;
1.2 deraadt 1588: int status;
1.6 millert 1589:
1.1 deraadt 1590: #ifdef RMT_WANTS_SIGNALS
1591: if (job->flags & JOB_REMOTE) {
1592: error = !Rmt_Signal(job, SIGCONT);
1593: } else
1594: #endif /* RMT_WANTS_SIGNALS */
1595: error = (KILL(job->pid, SIGCONT) != 0);
1596:
1597: if (!error) {
1598: /*
1599: * Make sure the user knows we've continued the beast and
1600: * actually put the thing in the job table.
1601: */
1602: job->flags |= JOB_CONTINUING;
1.2 deraadt 1603: W_SETTERMSIG(&status, SIGCONT);
1604: JobFinish(job, &status);
1.6 millert 1605:
1.1 deraadt 1606: job->flags &= ~(JOB_RESUME|JOB_CONTINUING);
1607: if (DEBUG(JOB)) {
1.2 deraadt 1608: (void) fprintf(stdout, "done\n");
1609: (void) fflush(stdout);
1.1 deraadt 1610: }
1611: } else {
1612: Error("couldn't resume %s: %s",
1613: job->node->name, strerror(errno));
1.2 deraadt 1614: status = 0;
1615: W_SETEXITSTATUS(&status, 1);
1616: JobFinish(job, &status);
1.1 deraadt 1617: }
1618: } else {
1619: /*
1620: * Job cannot be restarted. Mark the table as full and
1621: * place the job back on the list of stopped jobs.
1622: */
1623: if (DEBUG(JOB)) {
1.2 deraadt 1624: (void) fprintf(stdout, "table full\n");
1625: (void) fflush(stdout);
1.1 deraadt 1626: }
1.29 espie 1627: Lst_AtFront(&stoppedJobs, job);
1.1 deraadt 1628: jobFull = TRUE;
1629: if (DEBUG(JOB)) {
1.2 deraadt 1630: (void) fprintf(stdout, "Job queue is full.\n");
1631: (void) fflush(stdout);
1.1 deraadt 1632: }
1633: }
1634: }
1635: }
1636:
1637: /*-
1638: *-----------------------------------------------------------------------
1639: * JobStart --
1640: * Start a target-creation process going for the target described
1.6 millert 1641: * by the graph node gn.
1.1 deraadt 1642: *
1643: * Results:
1644: * JOB_ERROR if there was an error in the commands, JOB_FINISHED
1645: * if there isn't actually anything left to do for the job and
1646: * JOB_RUNNING if the job has been started.
1647: *
1648: * Side Effects:
1649: * A new Job node is created and added to the list of running
1650: * jobs. PMake is forked and a child shell created.
1651: *-----------------------------------------------------------------------
1652: */
1653: static int
1.2 deraadt 1654: JobStart(gn, flags, previous)
1.1 deraadt 1655: GNode *gn; /* target to create */
1656: int flags; /* flags for the job to override normal ones.
1657: * e.g. JOB_SPECIAL or JOB_IGNDOTS */
1658: Job *previous; /* The previous Job structure for this node,
1659: * if any. */
1660: {
1661: register Job *job; /* new job descriptor */
1662: char *argv[4]; /* Argument vector to shell */
1663: Boolean cmdsOK; /* true if the nodes commands were all right */
1664: Boolean local; /* Set true if the job was run locally */
1665: Boolean noExec; /* Set true if we decide not to run the job */
1.22 espie 1666: int tfd; /* where to stash those pesky commands */
1.1 deraadt 1667:
1.2 deraadt 1668: if (previous != NULL) {
1669: previous->flags &= ~(JOB_FIRST|JOB_IGNERR|JOB_SILENT|JOB_REMOTE);
1.1 deraadt 1670: job = previous;
1671: } else {
1.2 deraadt 1672: job = (Job *) emalloc(sizeof(Job));
1673: if (job == NULL) {
1.1 deraadt 1674: Punt("JobStart out of memory");
1675: }
1676: flags |= JOB_FIRST;
1677: }
1678:
1679: job->node = gn;
1.18 espie 1680: job->tailCmds = NULL;
1.1 deraadt 1681:
1682: /*
1683: * Set the initial value of the flags for this job based on the global
1684: * ones and the node's attributes... Any flags supplied by the caller
1685: * are also added to the field.
1686: */
1687: job->flags = 0;
1.2 deraadt 1688: if (Targ_Ignore(gn)) {
1.1 deraadt 1689: job->flags |= JOB_IGNERR;
1690: }
1.2 deraadt 1691: if (Targ_Silent(gn)) {
1.1 deraadt 1692: job->flags |= JOB_SILENT;
1693: }
1694: job->flags |= flags;
1695:
1696: /*
1697: * Check the commands now so any attributes from .DEFAULT have a chance
1698: * to migrate to the node
1699: */
1.2 deraadt 1700: if (!compatMake && job->flags & JOB_FIRST) {
1.1 deraadt 1701: cmdsOK = Job_CheckCommands(gn, Error);
1702: } else {
1703: cmdsOK = TRUE;
1704: }
1.6 millert 1705:
1.1 deraadt 1706: /*
1.6 millert 1707: * If the -n flag wasn't given, we open up OUR (not the child's)
1.1 deraadt 1708: * temporary file to stuff commands in it. The thing is rd/wr so we don't
1709: * need to reopen it to feed it to the shell. If the -n flag *was* given,
1710: * we just set the file to be stdout. Cute, huh?
1711: */
1712: if ((gn->type & OP_MAKE) || (!noExecute && !touchFlag)) {
1713: /*
1714: * We're serious here, but if the commands were bogus, we're
1715: * also dead...
1716: */
1717: if (!cmdsOK) {
1718: DieHorribly();
1719: }
1.6 millert 1720:
1.22 espie 1721: (void) strcpy(tfile, TMPPAT);
1722: if ((tfd = mkstemp(tfile)) == -1)
1723: Punt("Cannot create temp file: %s", strerror(errno));
1724: job->cmdFILE = fdopen(tfd, "w+");
1725: eunlink(tfile);
1.2 deraadt 1726: if (job->cmdFILE == NULL) {
1.22 espie 1727: close(tfd);
1.2 deraadt 1728: Punt("Could not open %s", tfile);
1.1 deraadt 1729: }
1.2 deraadt 1730: (void) fcntl(FILENO(job->cmdFILE), F_SETFD, 1);
1.1 deraadt 1731: /*
1732: * Send the commands to the command file, flush all its buffers then
1733: * rewind and remove the thing.
1734: */
1735: noExec = FALSE;
1736:
1737: /*
1738: * used to be backwards; replace when start doing multiple commands
1739: * per shell.
1740: */
1741: if (compatMake) {
1.30 espie 1742: LstNode ln;
1.1 deraadt 1743: /*
1744: * Be compatible: If this is the first time for this node,
1745: * verify its commands are ok and open the commands list for
1746: * sequential access by later invocations of JobStart.
1747: * Once that is done, we take the next command off the list
1748: * and print it to the command file. If the command was an
1749: * ellipsis, note that there's nothing more to execute.
1750: */
1.30 espie 1751: if ((job->flags&JOB_FIRST))
1752: Lst_Open(&gn->commands);
1753: ln = Lst_Next(&gn->commands);
1.6 millert 1754:
1.30 espie 1755: if ((ln == NULL) || !JobPrintCommand(Lst_Datum(ln), job)) {
1756: noExec = TRUE;
1757: Lst_Close(&gn->commands);
1758: }
1759: if (noExec && !(job->flags & JOB_FIRST)) {
1760: /*
1761: * If we're not going to execute anything, the job
1762: * is done and we need to close down the various
1763: * file descriptors we've opened for output, then
1764: * call JobDoOutput to catch the final characters or
1765: * send the file to the screen... Note that the i/o streams
1766: * are only open if this isn't the first job.
1767: * Note also that this could not be done in
1768: * Job_CatchChildren b/c it wasn't clear if there were
1769: * more commands to execute or not...
1770: */
1771: JobClose(job);
1.1 deraadt 1772: }
1773: } else {
1774: /*
1775: * We can do all the commands at once. hooray for sanity
1776: */
1777: numCommands = 0;
1.29 espie 1778: Lst_Find(&gn->commands, JobPrintCommand, job);
1.6 millert 1779:
1.1 deraadt 1780: /*
1781: * If we didn't print out any commands to the shell script,
1782: * there's not much point in executing the shell, is there?
1783: */
1784: if (numCommands == 0) {
1785: noExec = TRUE;
1786: }
1787: }
1788: } else if (noExecute) {
1789: /*
1790: * Not executing anything -- just print all the commands to stdout
1791: * in one fell swoop. This will still set up job->tailCmds correctly.
1792: */
1793: if (lastNode != gn) {
1.2 deraadt 1794: MESSAGE(stdout, gn);
1.1 deraadt 1795: lastNode = gn;
1796: }
1797: job->cmdFILE = stdout;
1798: /*
1799: * Only print the commands if they're ok, but don't die if they're
1800: * not -- just let the user know they're bad and keep going. It
1801: * doesn't do any harm in this case and may do some good.
1802: */
1.26 espie 1803: if (cmdsOK)
1.29 espie 1804: Lst_Find(&gn->commands, JobPrintCommand, job);
1.1 deraadt 1805: /*
1806: * Don't execute the shell, thank you.
1807: */
1808: noExec = TRUE;
1809: } else {
1810: /*
1811: * Just touch the target and note that no shell should be executed.
1812: * Set cmdFILE to stdout to make life easier. Check the commands, too,
1813: * but don't die if they're no good -- it does no harm to keep working
1814: * up the graph.
1815: */
1816: job->cmdFILE = stdout;
1.2 deraadt 1817: Job_Touch(gn, job->flags&JOB_SILENT);
1.1 deraadt 1818: noExec = TRUE;
1819: }
1820:
1821: /*
1.6 millert 1822: * If we're not supposed to execute a shell, don't.
1.1 deraadt 1823: */
1824: if (noExec) {
1825: /*
1826: * Unlink and close the command file if we opened one
1827: */
1828: if (job->cmdFILE != stdout) {
1.2 deraadt 1829: if (job->cmdFILE != NULL)
1830: (void) fclose(job->cmdFILE);
1.1 deraadt 1831: } else {
1.2 deraadt 1832: (void) fflush(stdout);
1.1 deraadt 1833: }
1834:
1835: /*
1836: * We only want to work our way up the graph if we aren't here because
1837: * the commands for the job were no good.
1838: */
1839: if (cmdsOK) {
1840: if (aborting == 0) {
1.27 espie 1841: Lst_ForEachFrom(job->tailCmds, JobSaveCommand, job->node);
1.1 deraadt 1842: Make_Update(job->node);
1843: }
1.28 espie 1844: free(job);
1845: return JOB_FINISHED;
1.1 deraadt 1846: } else {
1.28 espie 1847: free(job);
1848: return JOB_ERROR;
1.1 deraadt 1849: }
1850: } else {
1.2 deraadt 1851: (void) fflush(job->cmdFILE);
1.1 deraadt 1852: }
1853:
1854: /*
1855: * Set up the control arguments to the shell. This is based on the flags
1856: * set earlier for this job.
1857: */
1858: JobMakeArgv(job, argv);
1859:
1860: /*
1861: * If we're using pipes to catch output, create the pipe by which we'll
1862: * get the shell's output. If we're using files, print out that we're
1.7 millert 1863: * starting a job and then set up its temporary-file name.
1.1 deraadt 1864: */
1.2 deraadt 1865: if (!compatMake || (job->flags & JOB_FIRST)) {
1.1 deraadt 1866: if (usePipes) {
1867: int fd[2];
1.2 deraadt 1868: if (pipe(fd) == -1)
1869: Punt("Cannot create pipe: %s", strerror(errno));
1.1 deraadt 1870: job->inPipe = fd[0];
1871: job->outPipe = fd[1];
1.2 deraadt 1872: (void) fcntl(job->inPipe, F_SETFD, 1);
1873: (void) fcntl(job->outPipe, F_SETFD, 1);
1.1 deraadt 1874: } else {
1.2 deraadt 1875: (void) fprintf(stdout, "Remaking `%s'\n", gn->name);
1876: (void) fflush(stdout);
1.7 millert 1877: (void) strcpy(job->outFile, TMPPAT);
1878: if ((job->outFd = mkstemp(job->outFile)) == -1)
1879: Punt("Cannot create temp file: %s", strerror(errno));
1.2 deraadt 1880: (void) fcntl(job->outFd, F_SETFD, 1);
1.1 deraadt 1881: }
1882: }
1883:
1.2 deraadt 1884: #ifdef REMOTE
1885: if (!(gn->type & OP_NOEXPORT) && !(runLocalFirst && nLocal < maxLocal)) {
1886: #ifdef RMT_NO_EXEC
1887: local = !Rmt_Export(shellPath, argv, job);
1888: #else
1889: local = !Rmt_Begin(shellPath, argv, job->node);
1890: #endif /* RMT_NO_EXEC */
1891: if (!local) {
1892: job->flags |= JOB_REMOTE;
1893: }
1894: } else
1895: #endif
1896: local = TRUE;
1.1 deraadt 1897:
1898: if (local && (((nLocal >= maxLocal) &&
1.2 deraadt 1899: !(job->flags & JOB_SPECIAL) &&
1900: #ifdef REMOTE
1901: (!(gn->type & OP_NOEXPORT) || (maxLocal != 0))
1902: #else
1903: (maxLocal != 0)
1904: #endif
1905: )))
1.1 deraadt 1906: {
1907: /*
1908: * The job can only be run locally, but we've hit the limit of
1909: * local concurrency, so put the job on hold until some other job
1.6 millert 1910: * finishes. Note that the special jobs (.BEGIN, .INTERRUPT and .END)
1.1 deraadt 1911: * may be run locally even when the local limit has been reached
1.6 millert 1912: * (e.g. when maxLocal == 0), though they will be exported if at
1.2 deraadt 1913: * all possible. In addition, any target marked with .NOEXPORT will
1914: * be run locally if maxLocal is 0.
1.1 deraadt 1915: */
1916: jobFull = TRUE;
1.6 millert 1917:
1.1 deraadt 1918: if (DEBUG(JOB)) {
1.2 deraadt 1919: (void) fprintf(stdout, "Can only run job locally.\n");
1920: (void) fflush(stdout);
1.1 deraadt 1921: }
1922: job->flags |= JOB_RESTART;
1.29 espie 1923: Lst_AtEnd(&stoppedJobs, job);
1.1 deraadt 1924: } else {
1925: if ((nLocal >= maxLocal) && local) {
1926: /*
1.6 millert 1927: * If we're running this job locally as a special case (see above),
1.1 deraadt 1928: * at least say the table is full.
1929: */
1930: jobFull = TRUE;
1931: if (DEBUG(JOB)) {
1.2 deraadt 1932: (void) fprintf(stdout, "Local job queue is full.\n");
1933: (void) fflush(stdout);
1.1 deraadt 1934: }
1935: }
1936: JobExec(job, argv);
1937: }
1938: return(JOB_RUNNING);
1939: }
1940:
1.6 millert 1941: static char *
1.2 deraadt 1942: JobOutput(job, cp, endp, msg)
1943: register Job *job;
1944: register char *cp, *endp;
1945: int msg;
1946: {
1947: register char *ecp;
1948:
1949: if (commandShell->noPrint) {
1.14 espie 1950: ecp = strstr(cp, commandShell->noPrint);
1.2 deraadt 1951: while (ecp != NULL) {
1952: if (cp != ecp) {
1953: *ecp = '\0';
1954: if (msg && job->node != lastNode) {
1955: MESSAGE(stdout, job->node);
1956: lastNode = job->node;
1957: }
1958: /*
1959: * The only way there wouldn't be a newline after
1960: * this line is if it were the last in the buffer.
1961: * however, since the non-printable comes after it,
1962: * there must be a newline, so we don't print one.
1963: */
1964: (void) fprintf(stdout, "%s", cp);
1965: (void) fflush(stdout);
1966: }
1967: cp = ecp + commandShell->noPLen;
1968: if (cp != endp) {
1969: /*
1970: * Still more to print, look again after skipping
1971: * the whitespace following the non-printable
1972: * command....
1973: */
1974: cp++;
1975: while (*cp == ' ' || *cp == '\t' || *cp == '\n') {
1976: cp++;
1977: }
1.14 espie 1978: ecp = strstr(cp, commandShell->noPrint);
1.2 deraadt 1979: } else {
1980: return cp;
1981: }
1982: }
1983: }
1984: return cp;
1985: }
1986:
1.1 deraadt 1987: /*-
1988: *-----------------------------------------------------------------------
1989: * JobDoOutput --
1990: * This function is called at different times depending on
1991: * whether the user has specified that output is to be collected
1992: * via pipes or temporary files. In the former case, we are called
1993: * whenever there is something to read on the pipe. We collect more
1994: * output from the given job and store it in the job's outBuf. If
1995: * this makes up a line, we print it tagged by the job's identifier,
1996: * as necessary.
1997: * If output has been collected in a temporary file, we open the
1998: * file and read it line by line, transfering it to our own
1999: * output channel until the file is empty. At which point we
2000: * remove the temporary file.
2001: * In both cases, however, we keep our figurative eye out for the
2002: * 'noPrint' line for the shell from which the output came. If
2003: * we recognize a line, we don't print it. If the command is not
1.6 millert 2004: * alone on the line (the character after it is not \0 or \n), we
1.1 deraadt 2005: * do print whatever follows it.
2006: *
2007: * Results:
2008: * None
2009: *
2010: * Side Effects:
2011: * curPos may be shifted as may the contents of outBuf.
2012: *-----------------------------------------------------------------------
2013: */
1.2 deraadt 2014: STATIC void
2015: JobDoOutput(job, finish)
1.1 deraadt 2016: register Job *job; /* the job whose output needs printing */
2017: Boolean finish; /* TRUE if this is the last time we'll be
2018: * called for this job */
2019: {
2020: Boolean gotNL = FALSE; /* true if got a newline */
1.2 deraadt 2021: Boolean fbuf; /* true if our buffer filled up */
1.1 deraadt 2022: register int nr; /* number of bytes read */
2023: register int i; /* auxiliary index into outBuf */
1.6 millert 2024: register int max; /* limit for i (end of current data) */
2025: int nRead; /* (Temporary) number of bytes read */
1.1 deraadt 2026:
2027: FILE *oFILE; /* Stream pointer to shell's output file */
2028: char inLine[132];
2029:
1.6 millert 2030:
1.1 deraadt 2031: if (usePipes) {
2032: /*
2033: * Read as many bytes as will fit in the buffer.
2034: */
2035: end_loop:
1.2 deraadt 2036: gotNL = FALSE;
2037: fbuf = FALSE;
1.6 millert 2038:
1.2 deraadt 2039: nRead = read(job->inPipe, &job->outBuf[job->curPos],
1.1 deraadt 2040: JOB_BUFSIZE - job->curPos);
2041: if (nRead < 0) {
2042: if (DEBUG(JOB)) {
2043: perror("JobDoOutput(piperead)");
2044: }
2045: nr = 0;
2046: } else {
2047: nr = nRead;
2048: }
2049:
2050: /*
1.6 millert 2051: * If we hit the end-of-file (the job is dead), we must flush its
1.1 deraadt 2052: * remaining output, so pretend we read a newline if there's any
2053: * output remaining in the buffer.
2054: * Also clear the 'finish' flag so we stop looping.
2055: */
2056: if ((nr == 0) && (job->curPos != 0)) {
2057: job->outBuf[job->curPos] = '\n';
2058: nr = 1;
2059: finish = FALSE;
2060: } else if (nr == 0) {
2061: finish = FALSE;
2062: }
1.6 millert 2063:
1.1 deraadt 2064: /*
2065: * Look for the last newline in the bytes we just got. If there is
2066: * one, break out of the loop with 'i' as its index and gotNL set
1.6 millert 2067: * TRUE.
1.1 deraadt 2068: */
2069: max = job->curPos + nr;
2070: for (i = job->curPos + nr - 1; i >= job->curPos; i--) {
2071: if (job->outBuf[i] == '\n') {
2072: gotNL = TRUE;
2073: break;
2074: } else if (job->outBuf[i] == '\0') {
2075: /*
2076: * Why?
2077: */
2078: job->outBuf[i] = ' ';
2079: }
2080: }
1.6 millert 2081:
1.1 deraadt 2082: if (!gotNL) {
2083: job->curPos += nr;
2084: if (job->curPos == JOB_BUFSIZE) {
2085: /*
2086: * If we've run out of buffer space, we have no choice
1.6 millert 2087: * but to print the stuff. sigh.
1.1 deraadt 2088: */
1.2 deraadt 2089: fbuf = TRUE;
1.1 deraadt 2090: i = job->curPos;
2091: }
2092: }
1.2 deraadt 2093: if (gotNL || fbuf) {
1.1 deraadt 2094: /*
2095: * Need to send the output to the screen. Null terminate it
2096: * first, overwriting the newline character if there was one.
1.6 millert 2097: * So long as the line isn't one we should filter (according
1.1 deraadt 2098: * to the shell description), we print the line, preceeded
2099: * by a target banner if this target isn't the same as the
2100: * one for which we last printed something.
2101: * The rest of the data in the buffer are then shifted down
1.6 millert 2102: * to the start of the buffer and curPos is set accordingly.
1.1 deraadt 2103: */
2104: job->outBuf[i] = '\0';
2105: if (i >= job->curPos) {
1.2 deraadt 2106: char *cp;
1.6 millert 2107:
2108: cp = JobOutput(job, job->outBuf, &job->outBuf[i], FALSE);
1.1 deraadt 2109:
2110: /*
2111: * There's still more in that thar buffer. This time, though,
2112: * we know there's no newline at the end, so we add one of
2113: * our own free will.
2114: */
2115: if (*cp != '\0') {
2116: if (job->node != lastNode) {
1.2 deraadt 2117: MESSAGE(stdout, job->node);
1.1 deraadt 2118: lastNode = job->node;
2119: }
1.2 deraadt 2120: (void) fprintf(stdout, "%s%s", cp, gotNL ? "\n" : "");
2121: (void) fflush(stdout);
1.1 deraadt 2122: }
2123: }
2124: if (i < max - 1) {
2125: /* shift the remaining characters down */
1.6 millert 2126: (void) memcpy(job->outBuf, &job->outBuf[i + 1], max - (i + 1));
2127: job->curPos = max - (i + 1);
2128:
1.1 deraadt 2129: } else {
2130: /*
2131: * We have written everything out, so we just start over
2132: * from the start of the buffer. No copying. No nothing.
2133: */
2134: job->curPos = 0;
2135: }
2136: }
2137: if (finish) {
2138: /*
2139: * If the finish flag is true, we must loop until we hit
1.6 millert 2140: * end-of-file on the pipe. This is guaranteed to happen
2141: * eventually since the other end of the pipe is now closed
2142: * (we closed it explicitly and the child has exited). When
2143: * we do get an EOF, finish will be set FALSE and we'll fall
2144: * through and out.
1.1 deraadt 2145: */
2146: goto end_loop;
2147: }
2148: } else {
2149: /*
2150: * We've been called to retrieve the output of the job from the
2151: * temporary file where it's been squirreled away. This consists of
2152: * opening the file, reading the output line by line, being sure not
2153: * to print the noPrint line for the shell we used, then close and
2154: * remove the temporary file. Very simple.
2155: *
2156: * Change to read in blocks and do FindSubString type things as for
2157: * pipes? That would allow for "@echo -n..."
2158: */
1.2 deraadt 2159: oFILE = fopen(job->outFile, "r");
2160: if (oFILE != NULL) {
2161: (void) fprintf(stdout, "Results of making %s:\n", job->node->name);
2162: (void) fflush(stdout);
2163: while (fgets(inLine, sizeof(inLine), oFILE) != NULL) {
2164: register char *cp, *endp, *oendp;
1.1 deraadt 2165:
2166: cp = inLine;
1.2 deraadt 2167: oendp = endp = inLine + strlen(inLine);
1.1 deraadt 2168: if (endp[-1] == '\n') {
2169: *--endp = '\0';
2170: }
1.2 deraadt 2171: cp = JobOutput(job, inLine, endp, FALSE);
1.1 deraadt 2172:
2173: /*
2174: * There's still more in that thar buffer. This time, though,
2175: * we know there's no newline at the end, so we add one of
2176: * our own free will.
2177: */
1.2 deraadt 2178: (void) fprintf(stdout, "%s", cp);
2179: (void) fflush(stdout);
2180: if (endp != oendp) {
2181: (void) fprintf(stdout, "\n");
2182: (void) fflush(stdout);
1.1 deraadt 2183: }
2184: }
1.2 deraadt 2185: (void) fclose(oFILE);
2186: (void) eunlink(job->outFile);
1.1 deraadt 2187: }
2188: }
2189: }
2190:
2191: /*-
2192: *-----------------------------------------------------------------------
2193: * Job_CatchChildren --
2194: * Handle the exit of a child. Called from Make_Make.
2195: *
2196: * Results:
2197: * none.
2198: *
2199: * Side Effects:
2200: * The job descriptor is removed from the list of children.
2201: *
2202: * Notes:
2203: * We do waits, blocking or not, according to the wisdom of our
2204: * caller, until there are no more children to report. For each
2205: * job, call JobFinish to finish things off. This will take care of
2206: * putting jobs on the stoppedJobs queue.
2207: *
2208: *-----------------------------------------------------------------------
2209: */
2210: void
1.2 deraadt 2211: Job_CatchChildren(block)
1.1 deraadt 2212: Boolean block; /* TRUE if should block on the wait. */
2213: {
2214: int pid; /* pid of dead child */
2215: register Job *job; /* job descriptor for dead child */
2216: LstNode jnode; /* list element for finding job */
1.2 deraadt 2217: int status; /* Exit/termination status */
1.1 deraadt 2218:
2219: /*
2220: * Don't even bother if we know there's no one around.
2221: */
2222: if (nLocal == 0) {
2223: return;
2224: }
1.6 millert 2225:
1.2 deraadt 2226: while ((pid = waitpid((pid_t) -1, &status,
2227: (block?0:WNOHANG)|WUNTRACED)) > 0)
1.1 deraadt 2228: {
1.2 deraadt 2229: if (DEBUG(JOB)) {
2230: (void) fprintf(stdout, "Process %d exited or stopped.\n", pid);
2231: (void) fflush(stdout);
2232: }
1.6 millert 2233:
1.1 deraadt 2234:
1.29 espie 2235: jnode = Lst_Find(&jobs, JobCmpPid, &pid);
1.1 deraadt 2236:
1.18 espie 2237: if (jnode == NULL) {
1.2 deraadt 2238: if (WIFSIGNALED(status) && (WTERMSIG(status) == SIGCONT)) {
1.29 espie 2239: jnode = Lst_Find(&stoppedJobs, JobCmpPid, &pid);
1.18 espie 2240: if (jnode == NULL) {
1.6 millert 2241: Error("Resumed child (%d) not in table", pid);
1.1 deraadt 2242: continue;
2243: }
2244: job = (Job *)Lst_Datum(jnode);
1.29 espie 2245: Lst_Remove(&stoppedJobs, jnode);
1.1 deraadt 2246: } else {
1.6 millert 2247: Error("Child (%d) not in table?", pid);
1.1 deraadt 2248: continue;
2249: }
2250: } else {
1.30 espie 2251: job = (Job *)Lst_Datum(jnode);
1.29 espie 2252: Lst_Remove(&jobs, jnode);
1.1 deraadt 2253: nJobs -= 1;
2254: if (jobFull && DEBUG(JOB)) {
1.2 deraadt 2255: (void) fprintf(stdout, "Job queue is no longer full.\n");
2256: (void) fflush(stdout);
1.1 deraadt 2257: }
2258: jobFull = FALSE;
1.2 deraadt 2259: #ifdef REMOTE
2260: if (!(job->flags & JOB_REMOTE)) {
2261: if (DEBUG(JOB)) {
2262: (void) fprintf(stdout,
2263: "Job queue has one fewer local process.\n");
2264: (void) fflush(stdout);
2265: }
2266: nLocal -= 1;
2267: }
2268: #else
1.1 deraadt 2269: nLocal -= 1;
1.2 deraadt 2270: #endif
1.1 deraadt 2271: }
2272:
1.2 deraadt 2273: JobFinish(job, &status);
1.1 deraadt 2274: }
2275: }
2276:
2277: /*-
2278: *-----------------------------------------------------------------------
2279: * Job_CatchOutput --
2280: * Catch the output from our children, if we're using
2281: * pipes do so. Otherwise just block time until we get a
1.6 millert 2282: * signal (most likely a SIGCHLD) since there's no point in
1.1 deraadt 2283: * just spinning when there's nothing to do and the reaping
1.6 millert 2284: * of a child can wait for a while.
1.1 deraadt 2285: *
2286: * Results:
1.6 millert 2287: * None
1.1 deraadt 2288: *
2289: * Side Effects:
2290: * Output is read from pipes if we're piping.
2291: * -----------------------------------------------------------------------
2292: */
2293: void
1.2 deraadt 2294: Job_CatchOutput()
1.1 deraadt 2295: {
2296: int nfds;
2297: struct timeval timeout;
2298: register LstNode ln;
2299: register Job *job;
2300: #ifdef RMT_WILL_WATCH
2301: int pnJobs; /* Previous nJobs */
2302: #endif
2303:
1.2 deraadt 2304: (void) fflush(stdout);
1.1 deraadt 2305: #ifdef RMT_WILL_WATCH
2306: pnJobs = nJobs;
2307:
2308: /*
2309: * It is possible for us to be called with nJobs equal to 0. This happens
2310: * if all the jobs finish and a job that is stopped cannot be run
2311: * locally (eg if maxLocal is 0) and cannot be exported. The job will
2312: * be placed back on the stoppedJobs queue, Job_Empty() will return false,
2313: * Make_Run will call us again when there's nothing for which to wait.
2314: * nJobs never changes, so we loop forever. Hence the check. It could
2315: * be argued that we should sleep for a bit so as not to swamp the
2316: * exportation system with requests. Perhaps we should.
2317: *
2318: * NOTE: IT IS THE RESPONSIBILITY OF Rmt_Wait TO CALL Job_CatchChildren
2319: * IN A TIMELY FASHION TO CATCH ANY LOCALLY RUNNING JOBS THAT EXIT.
2320: * It may use the variable nLocal to determine if it needs to call
1.6 millert 2321: * Job_CatchChildren (if nLocal is 0, there's nothing for which to
1.1 deraadt 2322: * wait...)
2323: */
2324: while (nJobs != 0 && pnJobs == nJobs) {
2325: Rmt_Wait();
2326: }
2327: #else
2328: if (usePipes) {
1.8 deraadt 2329: int count = howmany(outputsn+1, NFDBITS) * sizeof(fd_mask);
2330: fd_set *readfdsp = malloc(count);
2331: if (readfdsp == NULL)
2332: return;
2333:
2334: memcpy(readfdsp, outputsp, count);
1.1 deraadt 2335: timeout.tv_sec = SEL_SEC;
2336: timeout.tv_usec = SEL_USEC;
2337:
1.8 deraadt 2338: if ((nfds = select(outputsn+1, readfdsp, (fd_set *) 0,
2339: (fd_set *) 0, &timeout)) <= 0) {
2340: free(readfdsp);
1.1 deraadt 2341: return;
1.8 deraadt 2342: } else {
1.30 espie 2343: Lst_Open(&jobs);
1.29 espie 2344: while (nfds && (ln = Lst_Next(&jobs)) != NULL) {
1.30 espie 2345: job = (Job *)Lst_Datum(ln);
1.8 deraadt 2346: if (FD_ISSET(job->inPipe, readfdsp)) {
1.2 deraadt 2347: JobDoOutput(job, FALSE);
1.1 deraadt 2348: nfds -= 1;
2349: }
2350: }
1.29 espie 2351: Lst_Close(&jobs);
1.1 deraadt 2352: }
1.8 deraadt 2353: free(readfdsp);
1.1 deraadt 2354: }
2355: #endif /* RMT_WILL_WATCH */
2356: }
2357:
2358: /*-
2359: *-----------------------------------------------------------------------
2360: * Job_Make --
2361: * Start the creation of a target. Basically a front-end for
2362: * JobStart used by the Make module.
2363: *
2364: * Results:
2365: * None.
2366: *
2367: * Side Effects:
2368: * Another job is started.
2369: *
2370: *-----------------------------------------------------------------------
2371: */
2372: void
1.2 deraadt 2373: Job_Make(gn)
1.1 deraadt 2374: GNode *gn;
2375: {
1.2 deraadt 2376: (void) JobStart(gn, 0, NULL);
1.1 deraadt 2377: }
2378:
2379: /*-
2380: *-----------------------------------------------------------------------
2381: * Job_Init --
2382: * Initialize the process module
2383: *
2384: * Results:
2385: * none
2386: *
2387: * Side Effects:
2388: * lists and counters are initialized
2389: *-----------------------------------------------------------------------
2390: */
2391: void
1.2 deraadt 2392: Job_Init(maxproc, maxlocal)
1.1 deraadt 2393: int maxproc; /* the greatest number of jobs which may be
2394: * running at one time */
2395: int maxlocal; /* the greatest number of local jobs which may
2396: * be running at once. */
2397: {
2398: GNode *begin; /* node for commands to do at the very start */
2399:
1.29 espie 2400: Lst_Init(&jobs);
2401: Lst_Init(&stoppedJobs);
1.1 deraadt 2402: maxJobs = maxproc;
2403: maxLocal = maxlocal;
2404: nJobs = 0;
2405: nLocal = 0;
2406: jobFull = FALSE;
2407:
2408: aborting = 0;
2409: errors = 0;
2410:
1.18 espie 2411: lastNode = NULL;
1.1 deraadt 2412:
1.2 deraadt 2413: if (maxJobs == 1
2414: #ifdef REMOTE
2415: || noMessages
2416: #endif
2417: ) {
1.1 deraadt 2418: /*
2419: * If only one job can run at a time, there's no need for a banner,
2420: * no is there?
2421: */
2422: targFmt = "";
2423: } else {
2424: targFmt = TARG_FMT;
2425: }
1.6 millert 2426:
1.2 deraadt 2427: if (shellPath == NULL) {
1.1 deraadt 2428: /*
2429: * The user didn't specify a shell to use, so we are using the
2430: * default one... Both the absolute path and the last component
2431: * must be set. The last component is taken from the 'name' field
2432: * of the default shell description pointed-to by commandShell.
2433: * All default shells are located in _PATH_DEFSHELLDIR.
2434: */
2435: shellName = commandShell->name;
1.33 espie 2436: shellPath = str_concat(_PATH_DEFSHELLDIR, shellName, '/');
1.1 deraadt 2437: }
2438:
1.2 deraadt 2439: if (commandShell->exit == NULL) {
1.1 deraadt 2440: commandShell->exit = "";
2441: }
1.2 deraadt 2442: if (commandShell->echo == NULL) {
1.1 deraadt 2443: commandShell->echo = "";
2444: }
2445:
2446: /*
2447: * Catch the four signals that POSIX specifies if they aren't ignored.
2448: * JobPassSig will take care of calling JobInterrupt if appropriate.
2449: */
1.2 deraadt 2450: if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
2451: (void) signal(SIGINT, JobPassSig);
1.1 deraadt 2452: }
1.2 deraadt 2453: if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
2454: (void) signal(SIGHUP, JobPassSig);
1.1 deraadt 2455: }
1.2 deraadt 2456: if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
2457: (void) signal(SIGQUIT, JobPassSig);
1.1 deraadt 2458: }
1.2 deraadt 2459: if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
2460: (void) signal(SIGTERM, JobPassSig);
1.1 deraadt 2461: }
2462: /*
2463: * There are additional signals that need to be caught and passed if
2464: * either the export system wants to be told directly of signals or if
1.6 millert 2465: * we're giving each job its own process group (since then it won't get
1.1 deraadt 2466: * signals from the terminal driver as we own the terminal)
2467: */
2468: #if defined(RMT_WANTS_SIGNALS) || defined(USE_PGRP)
1.2 deraadt 2469: if (signal(SIGTSTP, SIG_IGN) != SIG_IGN) {
2470: (void) signal(SIGTSTP, JobPassSig);
1.1 deraadt 2471: }
1.2 deraadt 2472: if (signal(SIGTTOU, SIG_IGN) != SIG_IGN) {
2473: (void) signal(SIGTTOU, JobPassSig);
1.1 deraadt 2474: }
1.2 deraadt 2475: if (signal(SIGTTIN, SIG_IGN) != SIG_IGN) {
2476: (void) signal(SIGTTIN, JobPassSig);
1.1 deraadt 2477: }
1.2 deraadt 2478: if (signal(SIGWINCH, SIG_IGN) != SIG_IGN) {
2479: (void) signal(SIGWINCH, JobPassSig);
1.1 deraadt 2480: }
2481: #endif
1.6 millert 2482:
1.2 deraadt 2483: begin = Targ_FindNode(".BEGIN", TARG_NOCREATE);
1.1 deraadt 2484:
1.18 espie 2485: if (begin != NULL) {
1.2 deraadt 2486: JobStart(begin, JOB_SPECIAL, (Job *)0);
1.1 deraadt 2487: while (nJobs) {
2488: Job_CatchOutput();
2489: #ifndef RMT_WILL_WATCH
1.2 deraadt 2490: Job_CatchChildren(!usePipes);
1.1 deraadt 2491: #endif /* RMT_WILL_WATCH */
2492: }
2493: }
1.2 deraadt 2494: postCommands = Targ_FindNode(".END", TARG_CREATE);
1.1 deraadt 2495: }
2496:
2497: /*-
2498: *-----------------------------------------------------------------------
2499: * Job_Full --
2500: * See if the job table is full. It is considered full if it is OR
2501: * if we are in the process of aborting OR if we have
2502: * reached/exceeded our local quota. This prevents any more jobs
2503: * from starting up.
2504: *
2505: * Results:
2506: * TRUE if the job table is full, FALSE otherwise
2507: * Side Effects:
2508: * None.
2509: *-----------------------------------------------------------------------
2510: */
2511: Boolean
1.2 deraadt 2512: Job_Full()
1.1 deraadt 2513: {
1.2 deraadt 2514: return(aborting || jobFull);
1.1 deraadt 2515: }
2516:
2517: /*-
2518: *-----------------------------------------------------------------------
2519: * Job_Empty --
2520: * See if the job table is empty. Because the local concurrency may
2521: * be set to 0, it is possible for the job table to become empty,
2522: * while the list of stoppedJobs remains non-empty. In such a case,
2523: * we want to restart as many jobs as we can.
2524: *
2525: * Results:
2526: * TRUE if it is. FALSE if it ain't.
2527: *
2528: * Side Effects:
2529: * None.
2530: *
2531: * -----------------------------------------------------------------------
2532: */
2533: Boolean
1.2 deraadt 2534: Job_Empty()
1.1 deraadt 2535: {
2536: if (nJobs == 0) {
1.29 espie 2537: if (!Lst_IsEmpty(&stoppedJobs) && !aborting) {
1.1 deraadt 2538: /*
2539: * The job table is obviously not full if it has no jobs in
2540: * it...Try and restart the stopped jobs.
2541: */
2542: jobFull = FALSE;
1.2 deraadt 2543: JobRestartJobs();
1.1 deraadt 2544: return(FALSE);
2545: } else {
2546: return(TRUE);
2547: }
2548: } else {
2549: return(FALSE);
2550: }
2551: }
2552:
2553: /*-
2554: *-----------------------------------------------------------------------
2555: * JobMatchShell --
2556: * Find a matching shell in 'shells' given its final component.
2557: *
2558: * Results:
2559: * A pointer to the Shell structure.
2560: *
2561: * Side Effects:
2562: * None.
2563: *
2564: *-----------------------------------------------------------------------
2565: */
2566: static Shell *
1.2 deraadt 2567: JobMatchShell(name)
1.1 deraadt 2568: char *name; /* Final component of shell path */
2569: {
2570: register Shell *sh; /* Pointer into shells table */
2571: Shell *match; /* Longest-matching shell */
2572: register char *cp1,
2573: *cp2;
2574: char *eoname;
2575:
1.2 deraadt 2576: eoname = name + strlen(name);
1.1 deraadt 2577:
1.2 deraadt 2578: match = NULL;
1.1 deraadt 2579:
2580: for (sh = shells; sh->name != NULL; sh++) {
1.2 deraadt 2581: for (cp1 = eoname - strlen(sh->name), cp2 = sh->name;
1.1 deraadt 2582: *cp1 != '\0' && *cp1 == *cp2;
2583: cp1++, cp2++) {
2584: continue;
2585: }
2586: if (*cp1 != *cp2) {
2587: continue;
1.2 deraadt 2588: } else if (match == NULL || strlen(match->name) < strlen(sh->name)) {
2589: match = sh;
1.1 deraadt 2590: }
2591: }
1.2 deraadt 2592: return(match == NULL ? sh : match);
1.1 deraadt 2593: }
2594:
2595: /*-
2596: *-----------------------------------------------------------------------
2597: * Job_ParseShell --
2598: * Parse a shell specification and set up commandShell, shellPath
2599: * and shellName appropriately.
2600: *
2601: * Results:
2602: * FAILURE if the specification was incorrect.
2603: *
2604: * Side Effects:
2605: * commandShell points to a Shell structure (either predefined or
2606: * created from the shell spec), shellPath is the full path of the
2607: * shell described by commandShell, while shellName is just the
2608: * final component of shellPath.
2609: *
2610: * Notes:
2611: * A shell specification consists of a .SHELL target, with dependency
2612: * operator, followed by a series of blank-separated words. Double
2613: * quotes can be used to use blanks in words. A backslash escapes
2614: * anything (most notably a double-quote and a space) and
2615: * provides the functionality it does in C. Each word consists of
2616: * keyword and value separated by an equal sign. There should be no
2617: * unnecessary spaces in the word. The keywords are as follows:
2618: * name Name of shell.
2619: * path Location of shell. Overrides "name" if given
2620: * quiet Command to turn off echoing.
2621: * echo Command to turn echoing on
2622: * filter Result of turning off echoing that shouldn't be
2623: * printed.
2624: * echoFlag Flag to turn echoing on at the start
2625: * errFlag Flag to turn error checking on at the start
2626: * hasErrCtl True if shell has error checking control
2627: * check Command to turn on error checking if hasErrCtl
2628: * is TRUE or template of command to echo a command
2629: * for which error checking is off if hasErrCtl is
2630: * FALSE.
2631: * ignore Command to turn off error checking if hasErrCtl
2632: * is TRUE or template of command to execute a
2633: * command so as to ignore any errors it returns if
2634: * hasErrCtl is FALSE.
2635: *
2636: *-----------------------------------------------------------------------
2637: */
2638: ReturnStatus
1.2 deraadt 2639: Job_ParseShell(line)
1.1 deraadt 2640: char *line; /* The shell spec */
2641: {
2642: char **words;
2643: int wordCount;
2644: register char **argv;
2645: register int argc;
2646: char *path;
2647: Shell newShell;
2648: Boolean fullSpec = FALSE;
2649:
1.2 deraadt 2650: while (isspace(*line)) {
1.1 deraadt 2651: line++;
2652: }
1.12 espie 2653:
2654: efree(shellArgv);
2655:
2656: words = brk_string(line, &wordCount, TRUE, &shellArgv);
1.1 deraadt 2657:
1.28 espie 2658: memset(&newShell, 0, sizeof(newShell));
1.6 millert 2659:
1.1 deraadt 2660: /*
2661: * Parse the specification by keyword
2662: */
1.12 espie 2663: for (path = NULL, argc = wordCount - 1, argv = words;
1.1 deraadt 2664: argc != 0;
2665: argc--, argv++) {
1.2 deraadt 2666: if (strncmp(*argv, "path=", 5) == 0) {
1.1 deraadt 2667: path = &argv[0][5];
1.2 deraadt 2668: } else if (strncmp(*argv, "name=", 5) == 0) {
1.1 deraadt 2669: newShell.name = &argv[0][5];
2670: } else {
1.2 deraadt 2671: if (strncmp(*argv, "quiet=", 6) == 0) {
1.1 deraadt 2672: newShell.echoOff = &argv[0][6];
1.2 deraadt 2673: } else if (strncmp(*argv, "echo=", 5) == 0) {
1.1 deraadt 2674: newShell.echoOn = &argv[0][5];
1.2 deraadt 2675: } else if (strncmp(*argv, "filter=", 7) == 0) {
1.1 deraadt 2676: newShell.noPrint = &argv[0][7];
2677: newShell.noPLen = strlen(newShell.noPrint);
1.2 deraadt 2678: } else if (strncmp(*argv, "echoFlag=", 9) == 0) {
1.1 deraadt 2679: newShell.echo = &argv[0][9];
1.2 deraadt 2680: } else if (strncmp(*argv, "errFlag=", 8) == 0) {
1.1 deraadt 2681: newShell.exit = &argv[0][8];
1.2 deraadt 2682: } else if (strncmp(*argv, "hasErrCtl=", 10) == 0) {
1.1 deraadt 2683: char c = argv[0][10];
2684: newShell.hasErrCtl = !((c != 'Y') && (c != 'y') &&
1.2 deraadt 2685: (c != 'T') && (c != 't'));
2686: } else if (strncmp(*argv, "check=", 6) == 0) {
1.1 deraadt 2687: newShell.errCheck = &argv[0][6];
1.2 deraadt 2688: } else if (strncmp(*argv, "ignore=", 7) == 0) {
1.1 deraadt 2689: newShell.ignErr = &argv[0][7];
2690: } else {
1.2 deraadt 2691: Parse_Error(PARSE_FATAL, "Unknown keyword \"%s\"",
1.1 deraadt 2692: *argv);
1.12 espie 2693: free(words);
1.2 deraadt 2694: return(FAILURE);
1.1 deraadt 2695: }
2696: fullSpec = TRUE;
2697: }
2698: }
2699:
1.2 deraadt 2700: if (path == NULL) {
1.1 deraadt 2701: /*
2702: * If no path was given, the user wants one of the pre-defined shells,
2703: * yes? So we find the one s/he wants with the help of JobMatchShell
2704: * and set things up the right way. shellPath will be set up by
2705: * Job_Init.
2706: */
1.2 deraadt 2707: if (newShell.name == NULL) {
2708: Parse_Error(PARSE_FATAL, "Neither path nor name specified");
2709: return(FAILURE);
1.1 deraadt 2710: } else {
1.2 deraadt 2711: commandShell = JobMatchShell(newShell.name);
1.1 deraadt 2712: shellName = newShell.name;
2713: }
2714: } else {
2715: /*
1.6 millert 2716: * The user provided a path. If s/he gave nothing else (fullSpec is
1.1 deraadt 2717: * FALSE), try and find a matching shell in the ones we know of.
2718: * Else we just take the specification at its word and copy it
2719: * to a new location. In either case, we need to record the
2720: * path the user gave for the shell.
2721: */
2722: shellPath = path;
1.2 deraadt 2723: path = strrchr(path, '/');
2724: if (path == NULL) {
1.1 deraadt 2725: path = shellPath;
2726: } else {
2727: path += 1;
2728: }
1.2 deraadt 2729: if (newShell.name != NULL) {
1.1 deraadt 2730: shellName = newShell.name;
2731: } else {
2732: shellName = path;
2733: }
2734: if (!fullSpec) {
1.2 deraadt 2735: commandShell = JobMatchShell(shellName);
1.1 deraadt 2736: } else {
2737: commandShell = (Shell *) emalloc(sizeof(Shell));
2738: *commandShell = newShell;
2739: }
2740: }
2741:
2742: if (commandShell->echoOn && commandShell->echoOff) {
2743: commandShell->hasEchoCtl = TRUE;
2744: }
1.6 millert 2745:
1.1 deraadt 2746: if (!commandShell->hasErrCtl) {
1.2 deraadt 2747: if (commandShell->errCheck == NULL) {
1.1 deraadt 2748: commandShell->errCheck = "";
2749: }
1.2 deraadt 2750: if (commandShell->ignErr == NULL) {
1.1 deraadt 2751: commandShell->ignErr = "%s\n";
2752: }
2753: }
1.6 millert 2754:
1.1 deraadt 2755: /*
2756: * Do not free up the words themselves, since they might be in use by the
2757: * shell specification...
2758: */
1.2 deraadt 2759: free(words);
1.1 deraadt 2760: return SUCCESS;
2761: }
2762:
2763: /*-
2764: *-----------------------------------------------------------------------
2765: * JobInterrupt --
2766: * Handle the receipt of an interrupt.
2767: *
2768: * Results:
2769: * None
2770: *
2771: * Side Effects:
2772: * All children are killed. Another job will be started if the
2773: * .INTERRUPT target was given.
2774: *-----------------------------------------------------------------------
2775: */
2776: static void
1.2 deraadt 2777: JobInterrupt(runINTERRUPT, signo)
1.1 deraadt 2778: int runINTERRUPT; /* Non-zero if commands for the .INTERRUPT
2779: * target should be executed */
1.2 deraadt 2780: int signo; /* signal received */
1.1 deraadt 2781: {
2782: LstNode ln; /* element in job table */
2783: Job *job; /* job descriptor in that element */
2784: GNode *interrupt; /* the node describing the .INTERRUPT target */
1.6 millert 2785:
1.1 deraadt 2786: aborting = ABORT_INTERRUPT;
2787:
1.30 espie 2788: Lst_Open(&jobs);
1.29 espie 2789: while ((ln = Lst_Next(&jobs)) != NULL) {
1.30 espie 2790: job = (Job *)Lst_Datum(ln);
1.1 deraadt 2791:
1.2 deraadt 2792: if (!Targ_Precious(job->node)) {
2793: char *file = (job->node->path == NULL ?
1.1 deraadt 2794: job->node->name :
2795: job->node->path);
1.2 deraadt 2796: if (!noExecute && eunlink(file) != -1) {
2797: Error("*** %s removed", file);
1.1 deraadt 2798: }
2799: }
2800: #ifdef RMT_WANTS_SIGNALS
2801: if (job->flags & JOB_REMOTE) {
2802: /*
2803: * If job is remote, let the Rmt module do the killing.
2804: */
1.2 deraadt 2805: if (!Rmt_Signal(job, signo)) {
1.1 deraadt 2806: /*
2807: * If couldn't kill the thing, finish it out now with an
2808: * error code, since no exit report will come in likely.
2809: */
1.2 deraadt 2810: int status;
1.1 deraadt 2811:
2812: status.w_status = 0;
2813: status.w_retcode = 1;
1.2 deraadt 2814: JobFinish(job, &status);
1.1 deraadt 2815: }
2816: } else if (job->pid) {
1.2 deraadt 2817: KILL(job->pid, signo);
1.1 deraadt 2818: }
2819: #else
2820: if (job->pid) {
1.2 deraadt 2821: if (DEBUG(JOB)) {
2822: (void) fprintf(stdout,
1.6 millert 2823: "JobInterrupt passing signal to child %d.\n",
1.2 deraadt 2824: job->pid);
2825: (void) fflush(stdout);
2826: }
2827: KILL(job->pid, signo);
2828: }
2829: #endif /* RMT_WANTS_SIGNALS */
2830: }
2831:
2832: #ifdef REMOTE
1.30 espie 2833: Lst_Open(&stoppedJobs);
1.29 espie 2834: while ((ln = Lst_Next(&stoppedJobs)) != NULL) {
1.30 espie 2835: job = (Job *)Lst_Datum(ln);
1.2 deraadt 2836:
2837: if (job->flags & JOB_RESTART) {
2838: if (DEBUG(JOB)) {
2839: (void) fprintf(stdout, "%s%s",
2840: "JobInterrupt skipping job on stopped queue",
2841: "-- it was waiting to be restarted.\n");
2842: (void) fflush(stdout);
2843: }
2844: continue;
2845: }
2846: if (!Targ_Precious(job->node)) {
2847: char *file = (job->node->path == NULL ?
2848: job->node->name :
2849: job->node->path);
2850: if (eunlink(file) == 0) {
2851: Error("*** %s removed", file);
2852: }
2853: }
2854: /*
2855: * Resume the thing so it will take the signal.
2856: */
2857: if (DEBUG(JOB)) {
2858: (void) fprintf(stdout,
1.6 millert 2859: "JobInterrupt passing CONT to stopped child %d.\n",
1.2 deraadt 2860: job->pid);
2861: (void) fflush(stdout);
2862: }
2863: KILL(job->pid, SIGCONT);
2864: #ifdef RMT_WANTS_SIGNALS
2865: if (job->flags & JOB_REMOTE) {
2866: /*
2867: * If job is remote, let the Rmt module do the killing.
2868: */
2869: if (!Rmt_Signal(job, SIGINT)) {
2870: /*
2871: * If couldn't kill the thing, finish it out now with an
2872: * error code, since no exit report will come in likely.
2873: */
2874: int status;
2875: status.w_status = 0;
2876: status.w_retcode = 1;
2877: JobFinish(job, &status);
2878: }
2879: } else if (job->pid) {
2880: if (DEBUG(JOB)) {
2881: (void) fprintf(stdout,
2882: "JobInterrupt passing interrupt to stopped child %d.\n",
2883: job->pid);
2884: (void) fflush(stdout);
2885: }
1.1 deraadt 2886: KILL(job->pid, SIGINT);
2887: }
2888: #endif /* RMT_WANTS_SIGNALS */
2889: }
1.2 deraadt 2890: #endif
1.29 espie 2891: Lst_Close(&stoppedJobs);
1.1 deraadt 2892:
2893: if (runINTERRUPT && !touchFlag) {
1.2 deraadt 2894: interrupt = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
1.18 espie 2895: if (interrupt != NULL) {
1.1 deraadt 2896: ignoreErrors = FALSE;
2897:
1.2 deraadt 2898: JobStart(interrupt, JOB_IGNDOTS, (Job *)0);
1.1 deraadt 2899: while (nJobs) {
2900: Job_CatchOutput();
2901: #ifndef RMT_WILL_WATCH
1.2 deraadt 2902: Job_CatchChildren(!usePipes);
1.1 deraadt 2903: #endif /* RMT_WILL_WATCH */
2904: }
2905: }
2906: }
1.2 deraadt 2907: exit(signo);
1.1 deraadt 2908: }
2909:
2910: /*
2911: *-----------------------------------------------------------------------
1.12 espie 2912: * Job_Finish --
1.1 deraadt 2913: * Do final processing such as the running of the commands
1.6 millert 2914: * attached to the .END target.
1.1 deraadt 2915: *
2916: * Results:
2917: * Number of errors reported.
2918: *-----------------------------------------------------------------------
2919: */
2920: int
1.12 espie 2921: Job_Finish()
1.1 deraadt 2922: {
1.29 espie 2923: if (postCommands != NULL && !Lst_IsEmpty(&postCommands->commands)) {
1.1 deraadt 2924: if (errors) {
1.2 deraadt 2925: Error("Errors reported so .END ignored");
1.1 deraadt 2926: } else {
1.2 deraadt 2927: JobStart(postCommands, JOB_SPECIAL | JOB_IGNDOTS, NULL);
1.1 deraadt 2928:
2929: while (nJobs) {
2930: Job_CatchOutput();
2931: #ifndef RMT_WILL_WATCH
1.2 deraadt 2932: Job_CatchChildren(!usePipes);
1.1 deraadt 2933: #endif /* RMT_WILL_WATCH */
2934: }
2935: }
2936: }
2937: return(errors);
2938: }
2939:
1.12 espie 2940: /*-
2941: *-----------------------------------------------------------------------
2942: * Job_End --
2943: * Cleanup any memory used by the jobs module
2944: *
2945: * Results:
2946: * None.
2947: *
2948: * Side Effects:
2949: * Memory is freed
2950: *-----------------------------------------------------------------------
2951: */
2952: void
2953: Job_End()
2954: {
1.13 espie 2955: #ifdef CLEANUP
1.12 espie 2956: efree(shellArgv);
1.13 espie 2957: #endif
1.12 espie 2958: }
2959:
1.1 deraadt 2960: /*-
2961: *-----------------------------------------------------------------------
2962: * Job_Wait --
2963: * Waits for all running jobs to finish and returns. Sets 'aborting'
2964: * to ABORT_WAIT to prevent other jobs from starting.
2965: *
2966: * Results:
2967: * None.
2968: *
2969: * Side Effects:
2970: * Currently running jobs finish.
2971: *
2972: *-----------------------------------------------------------------------
2973: */
2974: void
2975: Job_Wait()
2976: {
2977: aborting = ABORT_WAIT;
2978: while (nJobs != 0) {
2979: Job_CatchOutput();
2980: #ifndef RMT_WILL_WATCH
2981: Job_CatchChildren(!usePipes);
2982: #endif /* RMT_WILL_WATCH */
2983: }
2984: aborting = 0;
2985: }
2986:
2987: /*-
2988: *-----------------------------------------------------------------------
2989: * Job_AbortAll --
2990: * Abort all currently running jobs without handling output or anything.
2991: * This function is to be called only in the event of a major
2992: * error. Most definitely NOT to be called from JobInterrupt.
2993: *
2994: * Results:
2995: * None
2996: *
2997: * Side Effects:
2998: * All children are killed, not just the firstborn
2999: *-----------------------------------------------------------------------
3000: */
3001: void
1.2 deraadt 3002: Job_AbortAll()
1.1 deraadt 3003: {
1.2 deraadt 3004: LstNode ln; /* element in job table */
1.1 deraadt 3005: Job *job; /* the job descriptor in that element */
3006: int foo;
1.6 millert 3007:
1.1 deraadt 3008: aborting = ABORT_ERROR;
1.6 millert 3009:
1.1 deraadt 3010: if (nJobs) {
3011:
1.30 espie 3012: Lst_Open(&jobs);
1.29 espie 3013: while ((ln = Lst_Next(&jobs)) != NULL) {
1.30 espie 3014: job = (Job *)Lst_Datum(ln);
1.1 deraadt 3015:
3016: /*
3017: * kill the child process with increasingly drastic signals to make
1.6 millert 3018: * darn sure it's dead.
1.1 deraadt 3019: */
3020: #ifdef RMT_WANTS_SIGNALS
3021: if (job->flags & JOB_REMOTE) {
3022: Rmt_Signal(job, SIGINT);
3023: Rmt_Signal(job, SIGKILL);
3024: } else {
3025: KILL(job->pid, SIGINT);
3026: KILL(job->pid, SIGKILL);
3027: }
3028: #else
3029: KILL(job->pid, SIGINT);
3030: KILL(job->pid, SIGKILL);
3031: #endif /* RMT_WANTS_SIGNALS */
3032: }
3033: }
1.6 millert 3034:
1.1 deraadt 3035: /*
3036: * Catch as many children as want to report in at first, then give up
3037: */
1.2 deraadt 3038: while (waitpid((pid_t) -1, &foo, WNOHANG) > 0)
1.1 deraadt 3039: continue;
1.2 deraadt 3040: }
3041:
3042: #ifdef REMOTE
3043: /*-
3044: *-----------------------------------------------------------------------
3045: * JobFlagForMigration --
3046: * Handle the eviction of a child. Called from RmtStatusChange.
3047: * Flags the child as remigratable and then suspends it.
3048: *
3049: * Results:
3050: * none.
3051: *
3052: * Side Effects:
3053: * The job descriptor is flagged for remigration.
3054: *
3055: *-----------------------------------------------------------------------
3056: */
3057: void
3058: JobFlagForMigration(hostID)
3059: int hostID; /* ID of host we used, for matching children. */
3060: {
3061: register Job *job; /* job descriptor for dead child */
3062: LstNode jnode; /* list element for finding job */
3063:
3064: if (DEBUG(JOB)) {
3065: (void) fprintf(stdout, "JobFlagForMigration(%d) called.\n", hostID);
3066: (void) fflush(stdout);
3067: }
1.29 espie 3068: jnode = Lst_Find(&jobs, JobCmpRmtID, &hostID);
1.2 deraadt 3069:
1.18 espie 3070: if (jnode == NULL) {
1.29 espie 3071: jnode = Lst_Find(&stoppedJobs, JobCmpRmtID, &hostID);
1.18 espie 3072: if (jnode == NULL) {
1.2 deraadt 3073: if (DEBUG(JOB)) {
3074: Error("Evicting host(%d) not in table", hostID);
3075: }
3076: return;
3077: }
3078: }
1.30 espie 3079: job = (Job *)Lst_Datum(jnode);
1.2 deraadt 3080:
3081: if (DEBUG(JOB)) {
3082: (void) fprintf(stdout,
3083: "JobFlagForMigration(%d) found job '%s'.\n", hostID,
3084: job->node->name);
3085: (void) fflush(stdout);
3086: }
3087:
3088: KILL(job->pid, SIGSTOP);
3089:
3090: job->flags |= JOB_REMIGRATE;
3091: }
3092:
3093: #endif
3094:
3095: /*-
3096: *-----------------------------------------------------------------------
3097: * JobRestartJobs --
3098: * Tries to restart stopped jobs if there are slots available.
3099: * Note that this tries to restart them regardless of pending errors.
3100: * It's not good to leave stopped jobs lying around!
3101: *
3102: * Results:
3103: * None.
3104: *
3105: * Side Effects:
3106: * Resumes(and possibly migrates) jobs.
3107: *
3108: *-----------------------------------------------------------------------
3109: */
3110: static void
3111: JobRestartJobs()
3112: {
1.19 espie 3113: Job *job;
3114:
1.29 espie 3115: while (!jobFull && (job = (Job *)Lst_DeQueue(&stoppedJobs)) != NULL) {
1.2 deraadt 3116: if (DEBUG(JOB)) {
3117: (void) fprintf(stdout,
3118: "Job queue is not full. Restarting a stopped job.\n");
3119: (void) fflush(stdout);
3120: }
1.19 espie 3121: JobRestart(job);
1.2 deraadt 3122: }
1.1 deraadt 3123: }