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