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