Annotation of src/usr.bin/make/job.c, Revision 1.80
1.40 espie 1: /* $OpenPackages$ */
1.80 ! espie 2: /* $OpenBSD: job.c,v 1.79 2007/09/18 07:45:25 espie Exp $ */
1.6 millert 3: /* $NetBSD: job.c,v 1.16 1996/11/06 17:59:08 christos Exp $ */
1.1 deraadt 4:
5: /*
6: * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
7: * Copyright (c) 1988, 1989 by Adam de Boor
8: * Copyright (c) 1989 by Berkeley Softworks
9: * All rights reserved.
10: *
11: * This code is derived from software contributed to Berkeley by
12: * Adam de Boor.
13: *
14: * Redistribution and use in source and binary forms, with or without
15: * modification, are permitted provided that the following conditions
16: * are met:
17: * 1. Redistributions of source code must retain the above copyright
18: * notice, this list of conditions and the following disclaimer.
19: * 2. Redistributions in binary form must reproduce the above copyright
20: * notice, this list of conditions and the following disclaimer in the
21: * documentation and/or other materials provided with the distribution.
1.55 millert 22: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 23: * may be used to endorse or promote products derived from this software
24: * without specific prior written permission.
25: *
26: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36: * SUCH DAMAGE.
37: */
38:
39: /*-
40: * job.c --
41: * handle the creation etc. of our child processes.
42: *
43: * Interface:
1.40 espie 44: * Job_Make Start the creation of the given target.
1.1 deraadt 45: *
1.40 espie 46: * Job_CatchChildren Check for and handle the termination of any
47: * children. This must be called reasonably
48: * frequently to keep the whole make going at
49: * a decent clip, since job table entries aren't
50: * removed until their process is caught this way.
51: *
52: * Job_CatchOutput Print any output our children have produced.
53: * Should also be called fairly frequently to
54: * keep the user informed of what's going on.
55: * If no output is waiting, it will block for
56: * a time given by the SEL_* constants, below,
57: * or until output is ready.
58: *
1.53 jmc 59: * Job_Init Called to initialize this module. in addition,
1.40 espie 60: * any commands attached to the .BEGIN target
61: * are executed before this function returns.
62: * Hence, the makefile must have been parsed
63: * before this function is called.
64: *
65: * Job_End Cleanup any memory used.
66: *
1.41 espie 67: * Job_Full Return true if the job table is filled.
1.40 espie 68: *
1.41 espie 69: * Job_Empty Return true if the job table is completely
1.40 espie 70: * empty.
71: *
72: * Job_Finish Perform any final processing which needs doing.
73: * This includes the execution of any commands
74: * which have been/were attached to the .END
75: * target. It should only be called when the
76: * job table is empty.
77: *
78: * Job_AbortAll Abort all currently running jobs. It doesn't
79: * handle output or do anything for the jobs,
80: * just kills them. It should only be called in
81: * an emergency, as it were.
1.1 deraadt 82: *
1.40 espie 83: * Job_Wait Wait for all currently-running jobs to finish.
1.1 deraadt 84: */
85:
86: #include <sys/types.h>
87: #include <sys/wait.h>
1.41 espie 88: #include <ctype.h>
89: #include <errno.h>
1.1 deraadt 90: #include <fcntl.h>
1.41 espie 91: #include <signal.h>
1.69 espie 92: #include <stdarg.h>
1.1 deraadt 93: #include <stdio.h>
1.42 espie 94: #include <stdlib.h>
1.1 deraadt 95: #include <string.h>
1.41 espie 96: #include <unistd.h>
97: #include "config.h"
98: #include "defines.h"
1.1 deraadt 99: #include "job.h"
1.63 espie 100: #include "engine.h"
1.1 deraadt 101: #include "pathnames.h"
1.41 espie 102: #include "var.h"
103: #include "targ.h"
104: #include "error.h"
105: #include "lst.h"
106: #include "extern.h"
107: #include "gnode.h"
108: #include "memory.h"
109: #include "make.h"
110:
1.50 espie 111: #define TMPPAT "/tmp/makeXXXXXXXXXX"
112:
113: /*
114: * The SEL_ constants determine the maximum amount of time spent in select
115: * before coming out to see if a child has finished. SEL_SEC is the number of
116: * seconds and SEL_USEC is the number of micro-seconds
117: */
118: #define SEL_SEC 0
119: #define SEL_USEC 500000
120:
121:
122: /*-
123: * Job Table definitions.
124: *
125: * Each job has several things associated with it:
126: * 1) The process id of the child shell
127: * 2) The graph node describing the target being made by this job
128: * 3) A LstNode for the first command to be saved after the job
129: * completes. This is NULL if there was no "..." in the job's
130: * commands.
131: * 4) An FILE* for writing out the commands. This is only
132: * used before the job is actually started.
1.78 espie 133: * 5) Things used for handling the shell's output.
1.76 espie 134: * the output is being caught via a pipe and
1.50 espie 135: * the descriptors of our pipe, an array in which output is line
136: * buffered and the current position in that buffer are all
1.78 espie 137: * maintained for each job.
1.50 espie 138: * 6) An identifier provided by and for the exclusive use of the
139: * Rmt module.
140: * 7) A word of flags which determine how the module handles errors,
141: * echoing, etc. for the job
142: *
143: * The job "table" is kept as a linked Lst in 'jobs', with the number of
144: * active jobs maintained in the 'nJobs' variable. At no time will this
145: * exceed the value of 'maxJobs', initialized by the Job_Init function.
146: *
147: * When a job is finished, the Make_Update function is called on each of the
148: * parents of the node which was just remade. This takes care of the upward
149: * traversal of the dependency graph.
150: */
151: #define JOB_BUFSIZE 1024
152: typedef struct Job_ {
1.51 mpech 153: pid_t pid; /* The child's process ID */
1.50 espie 154: GNode *node; /* The target the child is making */
155: LstNode tailCmds; /* The node of the first command to be
156: * saved when the job has been run */
157: FILE *cmdFILE; /* When creating the shell script, this is
158: * where the commands go */
159: int rmtID; /* ID returned from Rmt module */
160: short flags; /* Flags to control treatment of job */
161: #define JOB_IGNERR 0x001 /* Ignore non-zero exits */
162: #define JOB_SILENT 0x002 /* no output */
1.79 espie 163: #define JOB_SPECIAL 0x004 /* Target is a special one. i.e. run it locally
164: * if we can't export it and maxLocal is 0 */
165: #define JOB_IGNDOTS 0x008 /* Ignore "..." lines when processing
166: * commands */
1.50 espie 167: #define JOB_FIRST 0x020 /* Job is first job for the node */
168: #define JOB_RESTART 0x080 /* Job needs to be completely restarted */
1.79 espie 169: #define JOB_RESUME 0x100 /* Job needs to be resumed b/c it stopped,
170: * for some reason */
171: #define JOB_CONTINUING 0x200 /* We are in the process of resuming this job.
172: * Used to avoid infinite recursion between
173: * JobFinish and JobRestart */
1.76 espie 174: int inPipe; /* Input side of pipe associated
175: * with job's output channel */
176: int outPipe; /* Output side of pipe associated with
177: * job's output channel */
178: char outBuf[JOB_BUFSIZE + 1];
179: /* Buffer for storing the output of the
180: * job, line by line */
181: int curPos; /* Current position in op_outBuf */
1.50 espie 182: } Job;
183:
1.78 espie 184:
1.1 deraadt 185: /*
1.6 millert 186: * error handling variables
1.1 deraadt 187: */
1.40 espie 188: static int errors = 0; /* number of errors reported */
189: static int aborting = 0; /* why is the make aborting? */
190: #define ABORT_ERROR 1 /* Because of an error */
191: #define ABORT_INTERRUPT 2 /* Because it was interrupted */
192: #define ABORT_WAIT 3 /* Waiting for jobs to finish */
1.1 deraadt 193:
1.6 millert 194: /*
1.2 deraadt 195: * XXX: Avoid SunOS bug... FILENO() is fp->_file, and file
196: * is a char! So when we go above 127 we turn negative!
197: */
198: #define FILENO(a) ((unsigned) fileno(a))
1.1 deraadt 199:
1.40 espie 200: static int numCommands; /* The number of commands actually printed
1.1 deraadt 201: * for a target. Should this number be
202: * 0, no shell will be executed. */
203:
204: /*
205: * Return values from JobStart.
206: */
1.40 espie 207: #define JOB_RUNNING 0 /* Job is running */
208: #define JOB_ERROR 1 /* Error in starting the job */
209: #define JOB_FINISHED 2 /* The job is already finished */
210: #define JOB_STOPPED 3 /* The job is stopped */
1.1 deraadt 211:
212: /*
1.40 espie 213: * tfile is the name of a file into which all shell commands are put. It is
214: * used over by removing it before the child shell is executed. The XXXXXXXXXX
215: * in the string are replaced by mkstemp(3).
1.1 deraadt 216: */
1.40 espie 217: static char tfile[sizeof(TMPPAT)];
1.1 deraadt 218:
219:
1.70 espie 220: #define SHELL_ECHO_OFF "set -"
221: #define SHELL_ECHO_ON "set -v"
222: #define SHELL_ERROR_ON "set -e"
223: #define SHELL_ERROR_OFF "set +e"
224: #define SHELL_ECHO_FLAG "v"
225: #define SHELL_ERROR_FLAG "e"
1.1 deraadt 226:
1.70 espie 227: static const char *shellPath = _PATH_BSHELL;
228: static const char *shellName = "sh";
1.1 deraadt 229:
1.78 espie 230:
1.40 espie 231: static int maxJobs; /* The most children we can run at once */
1.79 espie 232: static int maxLocal; /* The most local ones we can have */
1.48 espie 233: static int nJobs = 0; /* The number of children currently running */
1.79 espie 234: static int nLocal; /* The number of local children */
1.48 espie 235: static LIST jobs; /* The structures that describe them */
1.79 espie 236: static bool jobFull; /* Flag to tell when the job table is full. It
237: * is set true when (1) the total number of
238: * running jobs equals the maximum allowed or
239: * (2) a job can only be run locally, but
240: * nLocal equals maxLocal */
1.40 espie 241: static fd_set *outputsp; /* Set of descriptors of pipes connected to
1.1 deraadt 242: * the output channels of children */
1.8 deraadt 243: static int outputsn;
1.48 espie 244: static GNode *lastNode; /* The node for which output was most recently
1.1 deraadt 245: * produced. */
1.48 espie 246: static char *targFmt; /* Format string to use to head output from a
1.1 deraadt 247: * job when it's not the most-recent job heard
248: * from */
1.2 deraadt 249:
250: # define TARG_FMT "--- %s ---\n" /* Default format */
251: # define MESSAGE(fp, gn) \
1.40 espie 252: (void)fprintf(fp, targFmt, gn->name);
1.1 deraadt 253:
254: /*
1.50 espie 255: * When JobStart attempts to run a job but isn't allowed to,
256: * 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.48 espie 259: static LIST 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.40 espie 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.40 espie 294: static void JobCondPassSig(void *, void *);
1.57 espie 295: static void SigHandler(int);
296: static void HandleSigs(void);
1.40 espie 297: static void JobPassSig(int);
298: static int JobCmpPid(void *, void *);
1.60 espie 299: static int JobPrintCommand(LstNode, void *);
1.40 espie 300: static void JobSaveCommand(void *, void *);
301: static void JobClose(Job *);
302: static void JobFinish(Job *, int *);
303: static void JobExec(Job *, char **);
304: static void JobMakeArgv(Job *, char **);
305: static void JobRestart(Job *);
306: static int JobStart(GNode *, int, Job *);
307: static char *JobOutput(Job *, char *, char *, int);
1.41 espie 308: static void JobDoOutput(Job *, bool);
1.40 espie 309: static void JobInterrupt(int, int);
310: static void JobRestartJobs(void);
1.69 espie 311: static void DBPRINTF(Job *, const char *, ...);
1.1 deraadt 312:
1.57 espie 313: static volatile sig_atomic_t got_SIGINT, got_SIGHUP, got_SIGQUIT,
314: got_SIGTERM;
315: #if defined(USE_PGRP)
316: static volatile sig_atomic_t got_SIGTSTP, got_SIGTTOU, got_SIGTTIN,
317: got_SIGWINCH;
318: #endif
319:
320: static void
321: SigHandler(int sig)
322: {
323: switch(sig) {
324: case SIGINT:
325: got_SIGINT++;
326: break;
327: case SIGHUP:
328: got_SIGHUP++;
329: break;
330: case SIGQUIT:
331: got_SIGQUIT++;
332: break;
333: case SIGTERM:
334: got_SIGTERM++;
335: break;
336: #if defined(USE_PGRGP)
337: case SIGTSTP:
338: got_SIGTSTP++;
339: break;
340: case SIGTTOU:
341: got_SIGTTOU++;
342: break;
343: case SIGTTIN:
344: got_SIGTTIN++;
345: break;
346: case SIGWINCH:
347: got_SIGWINCH++;
348: break;
349: #endif
350: }
351: }
352:
353: static void
354: HandleSigs()
355: {
356: if (got_SIGINT) {
357: got_SIGINT=0;
358: JobPassSig(SIGINT);
359: }
360: if (got_SIGHUP) {
361: got_SIGHUP=0;
362: JobPassSig(SIGHUP);
363: }
364: if (got_SIGQUIT) {
365: got_SIGQUIT=0;
366: JobPassSig(SIGQUIT);
367: }
368: if (got_SIGTERM) {
369: got_SIGTERM=0;
370: JobPassSig(SIGTERM);
371: }
372: #if defined(USE_PGRP)
373: if (got_SIGTSTP) {
374: got_SIGTSTP=0;
375: JobPassSig(SIGTSTP);
376: }
377: if (got_SIGTTOU) {
378: got_SIGTTOU=0;
379: JobPassSig(SIGTTOU);
380: }
381: if (got_SIGTTIN) {
382: got_SIGTTIN=0;
383: JobPassSig(SIGTTIN);
384: }
385: if (got_SIGWINCH) {
386: got_SIGWINCH=0;
387: JobPassSig(SIGWINCH);
388: }
389: #endif
390: }
391:
1.1 deraadt 392: /*-
393: *-----------------------------------------------------------------------
394: * JobCondPassSig --
1.50 espie 395: * Pass a signal to a job if USE_PGRP
1.1 deraadt 396: * is defined.
397: *
398: * Side Effects:
399: * None, except the job may bite it.
400: *-----------------------------------------------------------------------
401: */
1.27 espie 402: static void
1.56 espie 403: JobCondPassSig(void *jobp, /* Job to biff */
404: void *signop) /* Signal to send it */
1.1 deraadt 405: {
1.66 espie 406: Job *job = (Job *)jobp;
407: int signo = *(int *)signop;
408: if (DEBUG(JOB)) {
409: (void)fprintf(stdout,
410: "JobCondPassSig passing signal %d to child %ld.\n",
411: signo, (long)job->pid);
412: (void)fflush(stdout);
413: }
414: KILL(job->pid, signo);
1.1 deraadt 415: }
416:
417: /*-
418: *-----------------------------------------------------------------------
419: * JobPassSig --
1.68 espie 420: * Pass a signal to all local jobs if USE_PGRP is defined,
1.50 espie 421: * then die ourselves.
1.1 deraadt 422: *
423: * Side Effects:
424: * We die by the same signal.
425: *-----------------------------------------------------------------------
426: */
427: static void
1.56 espie 428: JobPassSig(int signo) /* The signal number we've received */
1.1 deraadt 429: {
1.66 espie 430: sigset_t nmask, omask;
431: struct sigaction act;
1.6 millert 432:
1.66 espie 433: if (DEBUG(JOB)) {
434: (void)fprintf(stdout, "JobPassSig(%d) called.\n", signo);
435: (void)fflush(stdout);
436: }
437: Lst_ForEach(&jobs, JobCondPassSig, &signo);
438:
439: /*
440: * Deal with proper cleanup based on the signal received. We only run
441: * the .INTERRUPT target if the signal was in fact an interrupt. The
442: * other three termination signals are more of a "get out *now*"
443: * command.
444: */
445: if (signo == SIGINT) {
446: JobInterrupt(true, signo);
447: } else if (signo == SIGHUP || signo == SIGTERM || signo == SIGQUIT) {
448: JobInterrupt(false, signo);
449: }
1.1 deraadt 450:
1.66 espie 451: /*
452: * Leave gracefully if SIGQUIT, rather than core dumping.
453: */
454: if (signo == SIGQUIT) {
455: Finish(0);
456: }
1.6 millert 457:
1.66 espie 458: /*
459: * Send ourselves the signal now we've given the message to everyone
460: * else. Note we block everything else possible while we're getting
461: * the signal. This ensures that all our jobs get continued when we
462: * wake up before we take any other signal.
463: */
464: sigemptyset(&nmask);
465: sigaddset(&nmask, signo);
466: sigprocmask(SIG_SETMASK, &nmask, &omask);
467: memset(&act, 0, sizeof act);
468: act.sa_handler = SIG_DFL;
469: sigemptyset(&act.sa_mask);
470: act.sa_flags = 0;
471: sigaction(signo, &act, NULL);
1.6 millert 472:
1.66 espie 473: if (DEBUG(JOB)) {
474: (void)fprintf(stdout,
475: "JobPassSig passing signal to self, mask = %x.\n",
476: ~0 & ~(1 << (signo-1)));
477: (void)fflush(stdout);
478: }
479: (void)signal(signo, SIG_DFL);
1.1 deraadt 480:
1.66 espie 481: (void)KILL(getpid(), signo);
1.1 deraadt 482:
1.66 espie 483: signo = SIGCONT;
484: Lst_ForEach(&jobs, JobCondPassSig, &signo);
1.1 deraadt 485:
1.66 espie 486: (void)sigprocmask(SIG_SETMASK, &omask, NULL);
487: sigprocmask(SIG_SETMASK, &omask, NULL);
488: act.sa_handler = SigHandler;
489: sigaction(signo, &act, NULL);
1.1 deraadt 490: }
491:
492: /*-
493: *-----------------------------------------------------------------------
494: * JobCmpPid --
495: * Compare the pid of the job with the given pid and return 0 if they
496: * are equal. This function is called from Job_CatchChildren via
497: * Lst_Find to find the job descriptor of the finished job.
498: *
499: * Results:
500: * 0 if the pid's match
501: *-----------------------------------------------------------------------
502: */
503: static int
1.56 espie 504: JobCmpPid(void *job, /* job to examine */
505: void *pid) /* process id desired */
1.1 deraadt 506: {
1.66 espie 507: return *(pid_t *)pid - ((Job *)job)->pid;
1.1 deraadt 508: }
509:
1.69 espie 510: static void
511: DBPRINTF(Job *job, const char *fmt, ...)
512: {
513: va_list va;
514: va_start(va, fmt);
515: if (DEBUG(JOB)) {
516: (void)vfprintf(stdout, fmt, va);
517: fflush(stdout);
518: }
519: vfprintf(job->cmdFILE, fmt, va);
520: va_end(va);
521: }
522:
1.1 deraadt 523: /*-
524: *-----------------------------------------------------------------------
525: * JobPrintCommand --
526: * Put out another command for the given job. If the command starts
527: * with an @ or a - we process it specially. In the former case,
528: * so long as the -s and -n flags weren't given to make, we stick
529: * a shell-specific echoOff command in the script. In the latter,
530: * we ignore errors for the entire job, unless the shell has error
531: * control.
532: * If the command is just "..." we take all future commands for this
533: * job to be commands to be executed once the entire graph has been
534: * made and return non-zero to signal that the end of the commands
1.78 espie 535: * was reached. These commands are later attached to the end_node
1.1 deraadt 536: * node and executed by Job_End when all things are done.
1.40 espie 537: * This function is called from JobStart via Lst_Find
1.1 deraadt 538: *
539: * Results:
1.26 espie 540: * Always 1, unless the command was "..."
1.1 deraadt 541: *
542: * Side Effects:
543: * If the command begins with a '-' and the shell has no error control,
544: * the JOB_IGNERR flag is set in the job descriptor.
545: * If the command is "..." and we're not ignoring such things,
546: * tailCmds is set to the successor node of the cmd.
547: * numCommands is incremented if the command is actually printed.
548: *-----------------------------------------------------------------------
549: */
550: static int
1.78 espie 551: JobPrintCommand(LstNode cmdNode, /* command string to print */
552: void *jobp) /* job for which to print it */
1.1 deraadt 553: {
1.78 espie 554: bool noSpecials; /* true if we shouldn't worry about
555: * inserting special commands into
556: * the input stream. */
557: bool shutUp = false; /* true if we put a no echo command
558: * into the command file */
559: bool errOff = false; /* true if we turned error checking
560: * off before printing the command
561: * and need to turn it back on */
562: char *cmdTemplate; /* Template to use when printing the
563: * command */
564: char *cmdStart; /* Start of expanded command */
1.66 espie 565: char *cmd = (char *)Lst_Datum(cmdNode);
566: Job *job = (Job *)jobp;
567:
568: noSpecials = (noExecute && !(job->node->type & OP_MAKE));
569:
570: if (strcmp(cmd, "...") == 0) {
571: job->node->type |= OP_SAVE_CMDS;
572: if ((job->flags & JOB_IGNDOTS) == 0) {
573: job->tailCmds = Lst_Succ(cmdNode);
574: return 0;
575: }
576: return 1;
1.2 deraadt 577: }
1.1 deraadt 578:
1.78 espie 579:
1.67 espie 580: numCommands++;
1.1 deraadt 581:
1.66 espie 582: /* For debugging, we replace each command with the result of expanding
583: * the variables in the command. */
584: cmdStart = cmd = Var_Subst(cmd, &job->node->context, false);
585: Lst_Replace(cmdNode, cmdStart);
1.1 deraadt 586:
1.66 espie 587: cmdTemplate = "%s\n";
1.1 deraadt 588:
1.66 espie 589: /*
590: * Check for leading @' and -'s to control echoing and error checking.
591: */
592: for (;; cmd++) {
593: if (*cmd == '@')
594: shutUp = DEBUG(LOUD) ? false : true;
595: else if (*cmd == '-')
596: errOff = true;
597: else if (*cmd != '+')
598: break;
1.1 deraadt 599: }
600:
1.66 espie 601: while (isspace(*cmd))
602: cmd++;
603:
604: if (shutUp) {
1.72 espie 605: if (!(job->flags & JOB_SILENT) && !noSpecials) {
1.71 espie 606: DBPRINTF(job, "%s\n", SHELL_ECHO_OFF);
1.1 deraadt 607: } else {
1.66 espie 608: shutUp = false;
609: }
610: }
611:
612: if (errOff) {
613: if ( !(job->flags & JOB_IGNERR) && !noSpecials) {
1.72 espie 614: /*
1.78 espie 615: * we don't want the error-control commands showing
616: * up either, so we turn off echoing while executing
617: * them. We could put another field in the shell
618: * structure to tell JobDoOutput to look for this
619: * string too, but why make it any more complex than
620: * it already is?
1.72 espie 621: */
622: if (!(job->flags & JOB_SILENT) && !shutUp) {
1.75 espie 623: DBPRINTF(job, "%s; %s; %s\n", SHELL_ECHO_OFF,
624: SHELL_ERROR_OFF, SHELL_ECHO_ON);
1.66 espie 625: } else {
1.72 espie 626: DBPRINTF(job, "%s\n", SHELL_ERROR_OFF);
1.66 espie 627: }
628: } else {
629: errOff = false;
1.1 deraadt 630: }
1.66 espie 631: }
632:
1.69 espie 633: DBPRINTF(job, cmdTemplate, cmd);
1.66 espie 634:
635: if (errOff) {
636: /*
637: * If echoing is already off, there's no point in issuing the
638: * echoOff command. Otherwise we issue it and pretend it was on
639: * for the whole command...
1.1 deraadt 640: */
1.72 espie 641: if (!shutUp && !(job->flags & JOB_SILENT)) {
1.71 espie 642: DBPRINTF(job, "%s\n", SHELL_ECHO_OFF);
1.41 espie 643: shutUp = true;
1.1 deraadt 644: }
1.71 espie 645: DBPRINTF(job, "%s\n", SHELL_ERROR_ON);
1.66 espie 646: }
647: if (shutUp) {
1.71 espie 648: DBPRINTF(job, "%s\n", SHELL_ECHO_ON);
1.1 deraadt 649: }
1.66 espie 650: return 1;
1.1 deraadt 651: }
652:
653: /*-
654: *-----------------------------------------------------------------------
655: * JobSaveCommand --
656: * Save a command to be executed when everything else is done.
657: * Callback function for JobFinish...
658: *
659: * Side Effects:
1.78 espie 660: * The command is tacked onto the end of end_node's commands list.
1.1 deraadt 661: *-----------------------------------------------------------------------
662: */
1.27 espie 663: static void
1.56 espie 664: JobSaveCommand(void *cmd, void *gn)
1.1 deraadt 665: {
1.66 espie 666: GNode *g = (GNode *)gn;
667: char *result;
1.28 espie 668:
1.66 espie 669: result = Var_Subst((char *)cmd, &g->context, false);
1.78 espie 670: Lst_AtEnd(&end_node->commands, result);
1.2 deraadt 671: }
672:
673:
674: /*-
675: *-----------------------------------------------------------------------
676: * JobClose --
677: * Called to close both input and output pipes when a job is finished.
678: *
679: * Side Effects:
680: * The file descriptors associated with the job are closed.
681: *-----------------------------------------------------------------------
682: */
683: static void
1.56 espie 684: JobClose(Job *job)
1.2 deraadt 685: {
1.76 espie 686: FD_CLR(job->inPipe, outputsp);
687: if (job->outPipe != job->inPipe) {
688: (void)close(job->outPipe);
1.66 espie 689: }
1.76 espie 690: JobDoOutput(job, true);
691: (void)close(job->inPipe);
1.1 deraadt 692: }
693:
694: /*-
695: *-----------------------------------------------------------------------
696: * JobFinish --
697: * Do final processing for the given job including updating
698: * parents and starting new jobs as available/necessary. Note
699: * that we pay no attention to the JOB_IGNERR flag here.
700: * This is because when we're called because of a noexecute flag
701: * or something, jstat.w_status is 0 and when called from
702: * Job_CatchChildren, the status is zeroed if it s/b ignored.
703: *
704: * Side Effects:
705: * Some nodes may be put on the toBeMade queue.
1.78 espie 706: * Final commands for the job are placed on end_node.
1.1 deraadt 707: *
1.6 millert 708: * If we got an error and are aborting (aborting == ABORT_ERROR) and
1.1 deraadt 709: * the job list is now empty, we are done for the day.
1.6 millert 710: * If we recognized an error (errors !=0), we set the aborting flag
1.1 deraadt 711: * to ABORT_ERROR so no more jobs will be started.
712: *-----------------------------------------------------------------------
713: */
714: /*ARGSUSED*/
715: static void
1.56 espie 716: JobFinish(Job *job, /* job to finish */
717: int *status) /* sub-why job went away */
1.2 deraadt 718: {
1.78 espie 719: bool done;
1.2 deraadt 720:
1.66 espie 721: if ((WIFEXITED(*status) &&
722: WEXITSTATUS(*status) != 0 && !(job->flags & JOB_IGNERR)) ||
723: (WIFSIGNALED(*status) && WTERMSIG(*status) != SIGCONT)) {
724: /*
725: * If it exited non-zero and either we're doing things our
726: * way or we're not ignoring errors, the job is finished.
727: * Similarly, if the shell died because of a signal
728: * the job is also finished. In these
729: * cases, finish out the job's output before printing the exit
730: * status...
731: */
732: JobClose(job);
733: if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
734: (void)fclose(job->cmdFILE);
735: }
736: done = true;
737: } else if (WIFEXITED(*status)) {
738: /*
739: * Deal with ignored errors in -B mode. We need to print a
740: * message telling of the ignored error as well as setting
741: * status.w_status to 0 so the next command gets run. To do
742: * this, we set done to be true if in -B mode and the job
743: * exited non-zero.
744: */
745: done = WEXITSTATUS(*status) != 0;
746: /*
747: * Old comment said: "Note we don't want to close down any of
748: * the streams until we know we're at the end." But we do.
749: * Otherwise when are we going to print the rest of the stuff?
750: */
751: JobClose(job);
752: } else {
753: /*
754: * No need to close things down or anything.
755: */
756: done = false;
1.1 deraadt 757: }
1.6 millert 758:
1.66 espie 759: if (done ||
760: WIFSTOPPED(*status) ||
761: (WIFSIGNALED(*status) && WTERMSIG(*status) == SIGCONT) ||
762: DEBUG(JOB)) {
763: FILE *out;
764:
1.76 espie 765: out = stdout;
1.1 deraadt 766:
1.66 espie 767: if (WIFEXITED(*status)) {
768: if (DEBUG(JOB)) {
1.78 espie 769: (void)fprintf(stdout,
770: "Process %ld exited.\n", (long)job->pid);
1.66 espie 771: (void)fflush(stdout);
772: }
773: if (WEXITSTATUS(*status) != 0) {
1.76 espie 774: if (job->node != lastNode) {
1.66 espie 775: MESSAGE(out, job->node);
776: lastNode = job->node;
777: }
778: (void)fprintf(out, "*** Error code %d%s\n",
779: WEXITSTATUS(*status),
1.68 espie 780: (job->flags & JOB_IGNERR) ? "(ignored)" :
1.66 espie 781: "");
782:
783: if (job->flags & JOB_IGNERR) {
784: *status = 0;
785: }
786: } else if (DEBUG(JOB)) {
1.76 espie 787: if (job->node != lastNode) {
1.66 espie 788: MESSAGE(out, job->node);
789: lastNode = job->node;
790: }
1.68 espie 791: (void)fprintf(out,
1.66 espie 792: "*** Completed successfully\n");
793: }
794: } else if (WIFSTOPPED(*status)) {
795: if (DEBUG(JOB)) {
1.78 espie 796: (void)fprintf(stdout,
797: "Process %ld stopped.\n", (long)job->pid);
1.66 espie 798: (void)fflush(stdout);
799: }
1.76 espie 800: if (job->node != lastNode) {
1.66 espie 801: MESSAGE(out, job->node);
802: lastNode = job->node;
803: }
804: (void)fprintf(out, "*** Stopped -- signal %d\n",
805: WSTOPSIG(*status));
806: job->flags |= JOB_RESUME;
807: Lst_AtEnd(&stoppedJobs, job);
808: (void)fflush(out);
809: return;
810: } else if (WTERMSIG(*status) == SIGCONT) {
811: /*
812: * If the beastie has continued, shift the Job from the
813: * stopped list to the running one (or re-stop it if
814: * concurrency is exceeded) and go and get another
815: * child.
816: */
817: if (job->flags & (JOB_RESUME|JOB_RESTART)) {
1.76 espie 818: if (job->node != lastNode) {
1.66 espie 819: MESSAGE(out, job->node);
820: lastNode = job->node;
821: }
822: (void)fprintf(out, "*** Continued\n");
823: }
824: if (!(job->flags & JOB_CONTINUING)) {
825: if (DEBUG(JOB)) {
826: (void)fprintf(stdout,
1.78 espie 827: "Warning: process %ld was not continuing.\n",
1.66 espie 828: (long)job->pid);
829: (void)fflush(stdout);
830: }
831: #if 0
832: /*
833: * We don't really want to restart a job from
834: * scratch just because it continued,
835: * especially not without killing the
836: * continuing process! That's why this is
837: * ifdef'ed out. FD - 9/17/90
838: */
839: JobRestart(job);
1.2 deraadt 840: #endif
1.66 espie 841: }
842: job->flags &= ~JOB_CONTINUING;
843: Lst_AtEnd(&jobs, job);
1.67 espie 844: nJobs++;
1.66 espie 845: if (DEBUG(JOB)) {
846: (void)fprintf(stdout,
847: "Process %ld is continuing locally.\n",
848: (long)job->pid);
849: (void)fflush(stdout);
850: }
1.79 espie 851: nLocal++;
1.66 espie 852: if (nJobs == maxJobs) {
853: jobFull = true;
854: if (DEBUG(JOB)) {
1.68 espie 855: (void)fprintf(stdout,
1.66 espie 856: "Job queue is full.\n");
857: (void)fflush(stdout);
858: }
859: }
860: (void)fflush(out);
861: return;
862: } else {
1.76 espie 863: if (job->node != lastNode) {
1.66 espie 864: MESSAGE(out, job->node);
865: lastNode = job->node;
866: }
1.68 espie 867: (void)fprintf(out, "*** Signal %d\n",
1.66 espie 868: WTERMSIG(*status));
1.40 espie 869: }
1.66 espie 870:
871: (void)fflush(out);
1.1 deraadt 872: }
873:
1.66 espie 874: /*
875: * Now handle the -B-mode stuff. If the beast still isn't finished,
876: * try and restart the job on the next command. If JobStart says it's
877: * ok, it's ok. If there's an error, this puppy is done.
878: */
879: if (compatMake && WIFEXITED(*status) && job->node->current != NULL) {
880: switch (JobStart(job->node, job->flags & JOB_IGNDOTS, job)) {
881: case JOB_RUNNING:
882: done = false;
883: break;
884: case JOB_ERROR:
885: done = true;
886: W_SETEXITSTATUS(status, 1);
887: break;
888: case JOB_FINISHED:
889: /*
890: * If we got back a JOB_FINISHED code, JobStart has
891: * already called Make_Update and freed the job
892: * descriptor. We set done to false here to avoid fake
893: * cycles and double frees. JobStart needs to do the
894: * update so we can proceed up the graph when given the
895: * -n flag..
896: */
897: done = false;
898: break;
899: }
900: } else
901: done = true;
1.1 deraadt 902:
1.66 espie 903: if (done &&
904: aborting != ABORT_ERROR &&
905: aborting != ABORT_INTERRUPT &&
906: *status == 0) {
907: /* As long as we aren't aborting and the job didn't return a
908: * non-zero status that we shouldn't ignore, we call
909: * Make_Update to update the parents. In addition, any saved
910: * commands for the node are placed on the .END target. */
911: Lst_ForEachFrom(job->tailCmds, JobSaveCommand, job->node);
912: job->node->made = MADE;
913: Make_Update(job->node);
914: free(job);
915: } else if (*status != 0) {
1.67 espie 916: errors++;
1.66 espie 917: free(job);
918: }
1.1 deraadt 919:
1.66 espie 920: JobRestartJobs();
1.1 deraadt 921:
922: /*
1.66 espie 923: * Set aborting if any error.
1.1 deraadt 924: */
1.66 espie 925: if (errors && !keepgoing && aborting != ABORT_INTERRUPT) {
926: /*
927: * If we found any errors in this batch of children and the -k
928: * flag wasn't given, we set the aborting flag so no more jobs
929: * get started.
930: */
931: aborting = ABORT_ERROR;
932: }
1.6 millert 933:
1.66 espie 934: if (aborting == ABORT_ERROR && Job_Empty()) {
935: /*
936: * If we are aborting and the job table is now empty, we finish.
937: */
938: (void)eunlink(tfile);
939: Finish(errors);
940: }
1.1 deraadt 941: }
942:
943: /*-
944: *-----------------------------------------------------------------------
945: * JobExec --
946: * Execute the shell for the given job. Called from JobStart and
947: * JobRestart.
948: *
949: * Side Effects:
950: * A shell is executed, outputs is altered and the Job structure added
951: * to the job table.
952: *-----------------------------------------------------------------------
953: */
954: static void
1.56 espie 955: JobExec(Job *job, char **argv)
1.1 deraadt 956: {
1.66 espie 957: pid_t cpid; /* ID of new child */
1.6 millert 958:
1.66 espie 959: if (DEBUG(JOB)) {
1.78 espie 960: int i;
1.6 millert 961:
1.66 espie 962: (void)fprintf(stdout, "Running %s\n", job->node->name);
963: (void)fprintf(stdout, "\tCommand: ");
964: for (i = 0; argv[i] != NULL; i++) {
965: (void)fprintf(stdout, "%s ", argv[i]);
966: }
967: (void)fprintf(stdout, "\n");
968: (void)fflush(stdout);
1.1 deraadt 969: }
1.6 millert 970:
1.66 espie 971: /*
972: * Some jobs produce no output and it's disconcerting to have
973: * no feedback of their running (since they produce no output, the
974: * banner with their name in it never appears). This is an attempt to
975: * provide that feedback, even if nothing follows it.
976: */
977: if (lastNode != job->node && (job->flags & JOB_FIRST) &&
978: !(job->flags & JOB_SILENT)) {
979: MESSAGE(stdout, job->node);
980: lastNode = job->node;
981: }
1.1 deraadt 982:
1.66 espie 983: if ((cpid = fork()) == -1) {
984: Punt("Cannot fork");
985: } else if (cpid == 0) {
1.6 millert 986:
1.66 espie 987: /*
988: * Must duplicate the input stream down to the child's input
989: * and reset it to the beginning (again). Since the stream was
990: * marked close-on-exec, we must clear that bit in the new
991: * input.
992: */
993: if (dup2(FILENO(job->cmdFILE), 0) == -1)
994: Punt("Cannot dup2: %s", strerror(errno));
995: (void)fcntl(0, F_SETFD, 0);
996: (void)lseek(0, 0, SEEK_SET);
997:
1.76 espie 998: /*
1.78 espie 999: * Set up the child's output to be routed through the pipe
1000: * we've created for it.
1.76 espie 1001: */
1002: if (dup2(job->outPipe, 1) == -1)
1003: Punt("Cannot dup2: %s", strerror(errno));
1.66 espie 1004: /*
1005: * The output channels are marked close on exec. This bit was
1006: * duplicated by the dup2 (on some systems), so we have to
1007: * clear it before routing the shell's error output to the same
1008: * place as its standard output.
1009: */
1010: (void)fcntl(1, F_SETFD, 0);
1011: if (dup2(1, 2) == -1)
1012: Punt("Cannot dup2: %s", strerror(errno));
1.1 deraadt 1013:
1014: #ifdef USE_PGRP
1.66 espie 1015: /*
1016: * We want to switch the child into a different process family
1017: * so we can kill it and all its descendants in one fell swoop,
1018: * by killing its process family, but not commit suicide.
1019: */
1.2 deraadt 1020: # if defined(SYSV)
1.66 espie 1021: (void)setsid();
1.2 deraadt 1022: # else
1.66 espie 1023: (void)setpgid(0, getpid());
1.2 deraadt 1024: # endif
1025: #endif /* USE_PGRP */
1.1 deraadt 1026:
1.66 espie 1027: (void)execv(shellPath, argv);
1028:
1029: (void)write(STDERR_FILENO, "Could not execute shell\n",
1030: sizeof("Could not execute shell"));
1031: _exit(1);
1032: } else {
1033: job->pid = cpid;
1034:
1.76 espie 1035: if (job->flags & JOB_FIRST) {
1.66 espie 1036: /*
1037: * The first time a job is run for a node, we set the
1038: * current position in the buffer to the beginning and
1039: * mark another stream to watch in the outputs mask
1040: */
1041: job->curPos = 0;
1042:
1043: if (outputsp == NULL || job->inPipe > outputsn) {
1044: int bytes, obytes;
1045: char *tmp;
1046:
1.68 espie 1047: bytes = howmany(job->inPipe+1, NFDBITS) *
1.66 espie 1048: sizeof(fd_mask);
1.68 espie 1049: obytes = outputsn ?
1050: howmany(outputsn+1, NFDBITS) *
1.66 espie 1051: sizeof(fd_mask) : 0;
1052:
1053: if (bytes != obytes) {
1054: tmp = realloc(outputsp, bytes);
1055: if (tmp == NULL)
1056: return;
1057: memset(tmp + obytes, 0, bytes - obytes);
1058: outputsp = (fd_set *)tmp;
1059: }
1060: outputsn = job->inPipe;
1061: }
1062: FD_SET(job->inPipe, outputsp);
1063: }
1.2 deraadt 1064:
1.66 espie 1065: /*
1066: * XXX: Used to not happen if REMOTE. Why?
1067: */
1068: if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
1069: (void)fclose(job->cmdFILE);
1070: job->cmdFILE = NULL;
1071: }
1.1 deraadt 1072: }
1073:
1.79 espie 1074: nLocal += 1;
1.48 espie 1075: /*
1.66 espie 1076: * Now the job is actually running, add it to the table.
1.48 espie 1077: */
1.67 espie 1078: nJobs++;
1.66 espie 1079: Lst_AtEnd(&jobs, job);
1080: if (nJobs == maxJobs) {
1081: jobFull = true;
1.1 deraadt 1082: }
1083: }
1084:
1085: /*-
1086: *-----------------------------------------------------------------------
1087: * JobMakeArgv --
1088: * Create the argv needed to execute the shell for a given job.
1089: *-----------------------------------------------------------------------
1090: */
1091: static void
1.56 espie 1092: JobMakeArgv(Job *job, char **argv)
1.1 deraadt 1093: {
1.66 espie 1094: int argc;
1095: static char args[10]; /* For merged arguments */
1.6 millert 1096:
1.70 espie 1097: argv[0] = (char *)shellName;
1.66 espie 1098: argc = 1;
1.1 deraadt 1099:
1.73 espie 1100: (void)snprintf(args, sizeof(args), "-%s%s",
1101: (job->flags & JOB_IGNERR) ? "" : SHELL_ERROR_FLAG,
1102: (job->flags & JOB_SILENT) ? "" : SHELL_ECHO_FLAG);
1103:
1104: if (args[1]) {
1105: argv[argc] = args;
1106: argc++;
1.78 espie 1107: }
1.66 espie 1108: argv[argc] = NULL;
1.1 deraadt 1109: }
1110:
1111: /*-
1112: *-----------------------------------------------------------------------
1113: * JobRestart --
1.6 millert 1114: * Restart a job that stopped for some reason.
1.1 deraadt 1115: *
1116: * Side Effects:
1117: * jobFull will be set if the job couldn't be run.
1118: *-----------------------------------------------------------------------
1119: */
1120: static void
1.56 espie 1121: JobRestart(Job *job)
1.1 deraadt 1122: {
1.66 espie 1123: if (job->flags & JOB_RESTART) {
1124: /*
1125: * Set up the control arguments to the shell. This is based on
1126: * the flags set earlier for this job. If the JOB_IGNERR flag
1127: * is clear, the 'exit' flag of the commandShell is used to
1128: * cause it to exit upon receiving an error. If the JOB_SILENT
1129: * flag is clear, the 'echo' flag of the commandShell is used
1130: * to get it to start echoing as soon as it starts processing
1131: * commands.
1132: */
1133: char *argv[4];
1.6 millert 1134:
1.66 espie 1135: JobMakeArgv(job, argv);
1.2 deraadt 1136:
1.1 deraadt 1137: if (DEBUG(JOB)) {
1.68 espie 1138: (void)fprintf(stdout, "Restarting %s...",
1.66 espie 1139: job->node->name);
1140: (void)fflush(stdout);
1.1 deraadt 1141: }
1.79 espie 1142: if (nLocal >= maxLocal && !(job->flags & JOB_SPECIAL)) {
1.80 ! espie 1143: /*
! 1144: * Can't be exported and not allowed to run locally --
! 1145: * put it back on the hold queue and mark the table
! 1146: * full
! 1147: */
! 1148: if (DEBUG(JOB)) {
! 1149: (void)fprintf(stdout, "holding\n");
! 1150: (void)fflush(stdout);
! 1151: }
! 1152: Lst_AtFront(&stoppedJobs, job);
! 1153: jobFull = true;
! 1154: if (DEBUG(JOB)) {
! 1155: (void)fprintf(stdout, "Job queue is full.\n");
! 1156: (void)fflush(stdout);
! 1157: }
! 1158: return;
1.66 espie 1159: } else {
1.80 ! espie 1160: /*
! 1161: * Job may be run locally.
! 1162: */
! 1163: if (DEBUG(JOB)) {
! 1164: (void)fprintf(stdout, "running locally\n");
! 1165: (void)fflush(stdout);
! 1166: }
1.1 deraadt 1167: }
1.66 espie 1168: JobExec(job, argv);
1169: } else {
1.1 deraadt 1170: /*
1.66 espie 1171: * The job has stopped and needs to be restarted. Why it
1172: * stopped, we don't know...
1.1 deraadt 1173: */
1174: if (DEBUG(JOB)) {
1.66 espie 1175: (void)fprintf(stdout, "Resuming %s...", job->node->name);
1176: (void)fflush(stdout);
1.1 deraadt 1177: }
1.79 espie 1178: if ((nLocal < maxLocal || ((job->flags & JOB_SPECIAL) &&
1179: maxLocal == 0)) && nJobs != maxJobs) {
1.66 espie 1180: /*
1.79 espie 1181: * If we haven't reached the concurrency limit already
1182: * (or maxLocal is 0), it's ok to resume the job.
1.66 espie 1183: */
1184: bool error;
1185: int status;
1186:
1187: error = KILL(job->pid, SIGCONT) != 0;
1188:
1189: if (!error) {
1190: /*
1191: * Make sure the user knows we've continued the
1192: * beast and actually put the thing in the job
1193: * table.
1194: */
1195: job->flags |= JOB_CONTINUING;
1196: W_SETTERMSIG(&status, SIGCONT);
1197: JobFinish(job, &status);
1198:
1199: job->flags &= ~(JOB_RESUME|JOB_CONTINUING);
1200: if (DEBUG(JOB)) {
1201: (void)fprintf(stdout, "done\n");
1202: (void)fflush(stdout);
1203: }
1204: } else {
1205: Error("couldn't resume %s: %s",
1206: job->node->name, strerror(errno));
1207: status = 0;
1208: W_SETEXITSTATUS(&status, 1);
1209: JobFinish(job, &status);
1210: }
1211: } else {
1212: /*
1213: * Job cannot be restarted. Mark the table as full and
1214: * place the job back on the list of stopped jobs.
1215: */
1216: if (DEBUG(JOB)) {
1217: (void)fprintf(stdout, "table full\n");
1218: (void)fflush(stdout);
1219: }
1220: Lst_AtFront(&stoppedJobs, job);
1221: jobFull = true;
1222: if (DEBUG(JOB)) {
1223: (void)fprintf(stdout, "Job queue is full.\n");
1224: (void)fflush(stdout);
1225: }
1.1 deraadt 1226: }
1227: }
1228: }
1229:
1230: /*-
1231: *-----------------------------------------------------------------------
1232: * JobStart --
1233: * Start a target-creation process going for the target described
1.6 millert 1234: * by the graph node gn.
1.1 deraadt 1235: *
1236: * Results:
1237: * JOB_ERROR if there was an error in the commands, JOB_FINISHED
1238: * if there isn't actually anything left to do for the job and
1239: * JOB_RUNNING if the job has been started.
1240: *
1241: * Side Effects:
1242: * A new Job node is created and added to the list of running
1243: * jobs. PMake is forked and a child shell created.
1244: *-----------------------------------------------------------------------
1245: */
1246: static int
1.78 espie 1247: JobStart(GNode *gn, /* target to create */
1248: int flags, /* flags for the job to override normal ones.
1249: * e.g. JOB_SPECIAL or JOB_IGNDOTS */
1250: Job *previous) /* The previous Job structure for this node,
1251: * if any. */
1252: {
1253: Job *job; /* new job descriptor */
1254: char *argv[4]; /* Argument vector to shell */
1255: bool cmdsOK; /* true if the nodes commands were all right */
1256: bool local; /* Set true if the job was run locally */
1257: bool noExec; /* Set true if we decide not to run the job */
1.66 espie 1258:
1259: if (previous != NULL) {
1260: previous->flags &= ~(JOB_FIRST|JOB_IGNERR|JOB_SILENT);
1261: job = previous;
1262: } else {
1263: job = emalloc(sizeof(Job));
1264: if (job == NULL) {
1265: Punt("JobStart out of memory");
1266: }
1267: flags |= JOB_FIRST;
1.1 deraadt 1268: }
1269:
1.66 espie 1270: job->node = gn;
1271: job->tailCmds = NULL;
1.1 deraadt 1272:
1273: /*
1.66 espie 1274: * Set the initial value of the flags for this job based on the global
1275: * ones and the node's attributes... Any flags supplied by the caller
1276: * are also added to the field.
1.1 deraadt 1277: */
1.66 espie 1278: job->flags = 0;
1279: if (Targ_Ignore(gn)) {
1280: job->flags |= JOB_IGNERR;
1281: }
1282: if (Targ_Silent(gn)) {
1283: job->flags |= JOB_SILENT;
1.1 deraadt 1284: }
1.66 espie 1285: job->flags |= flags;
1.6 millert 1286:
1.1 deraadt 1287: /*
1.66 espie 1288: * Check the commands now so any attributes from .DEFAULT have a chance
1289: * to migrate to the node
1.1 deraadt 1290: */
1.66 espie 1291: if (!compatMake && job->flags & JOB_FIRST) {
1292: cmdsOK = Job_CheckCommands(gn, Error);
1293: } else {
1294: cmdsOK = true;
1295: }
1.1 deraadt 1296:
1297: /*
1.66 espie 1298: * If the -n flag wasn't given, we open up OUR (not the child's)
1299: * temporary file to stuff commands in it. The thing is rd/wr so we
1300: * don't need to reopen it to feed it to the shell. If the -n flag
1301: * *was* given, we just set the file to be stdout. Cute, huh?
1.1 deraadt 1302: */
1.66 espie 1303: if ((gn->type & OP_MAKE) || (!noExecute && !touchFlag)) {
1304: /*
1305: * We're serious here, but if the commands were bogus, we're
1306: * also dead...
1307: */
1308: if (!cmdsOK) {
1309: DieHorribly();
1310: }
1.6 millert 1311:
1.66 espie 1312: job->cmdFILE = fopen(tfile, "w+");
1313: if (job->cmdFILE == NULL) {
1314: Punt("Could not open %s", tfile);
1315: }
1316: (void)fcntl(FILENO(job->cmdFILE), F_SETFD, 1);
1317: /*
1318: * Send the commands to the command file, flush all its buffers
1319: * then rewind and remove the thing.
1320: */
1321: noExec = false;
1322:
1323: /*
1324: * used to be backwards; replace when start doing multiple
1325: * commands per shell.
1326: */
1327: if (compatMake) {
1328: /*
1329: * Be compatible: If this is the first time for this
1330: * node, verify its commands are ok and open the
1331: * commands list for sequential access by later
1332: * invocations of JobStart. Once that is done, we take
1333: * the next command off the list and print it to the
1334: * command file. If the command was an ellipsis, note
1335: * that there's nothing more to execute.
1336: */
1337: if ((job->flags&JOB_FIRST))
1338: gn->current = Lst_First(&gn->commands);
1339: else
1340: gn->current = Lst_Succ(gn->current);
1341:
1342: if (gn->current == NULL ||
1343: !JobPrintCommand(gn->current, job)) {
1344: noExec = true;
1345: gn->current = NULL;
1346: }
1347: if (noExec && !(job->flags & JOB_FIRST)) {
1348: /*
1349: * If we're not going to execute anything, the
1350: * job is done and we need to close down the
1351: * various file descriptors we've opened for
1352: * output, then call JobDoOutput to catch the
1353: * final characters or send the file to the
1354: * screen... Note that the i/o streams are only
1355: * open if this isn't the first job. Note also
1356: * that this could not be done in
1357: * Job_CatchChildren b/c it wasn't clear if
1358: * there were more commands to execute or
1359: * not...
1360: */
1361: JobClose(job);
1362: }
1363: } else {
1364: /*
1365: * We can do all the commands at once. hooray for
1366: * sanity
1367: */
1368: numCommands = 0;
1.68 espie 1369: Lst_ForEachNodeWhile(&gn->commands, JobPrintCommand,
1.66 espie 1370: job);
1371:
1372: /*
1373: * If we didn't print out any commands to the shell
1374: * script, there's not much point in executing the
1375: * shell, is there?
1376: */
1377: if (numCommands == 0) {
1378: noExec = true;
1379: }
1380: }
1381: } else if (noExecute) {
1382: /*
1383: * Not executing anything -- just print all the commands to
1384: * stdout in one fell swoop. This will still set up
1385: * job->tailCmds correctly.
1386: */
1387: if (lastNode != gn) {
1388: MESSAGE(stdout, gn);
1389: lastNode = gn;
1390: }
1391: job->cmdFILE = stdout;
1392: /*
1393: * Only print the commands if they're ok, but don't die if
1394: * they're not -- just let the user know they're bad and keep
1395: * going. It doesn't do any harm in this case and may do some
1396: * good.
1397: */
1398: if (cmdsOK) {
1.68 espie 1399: Lst_ForEachNodeWhile(&gn->commands, JobPrintCommand,
1.66 espie 1400: job);
1401: }
1402: /*
1403: * Don't execute the shell, thank you.
1404: */
1.41 espie 1405: noExec = true;
1.66 espie 1406: } else {
1407: /*
1408: * Just touch the target and note that no shell should be
1409: * executed. Set cmdFILE to stdout to make life easier. Check
1410: * the commands, too, but don't die if they're no good -- it
1411: * does no harm to keep working up the graph.
1.30 espie 1412: */
1.66 espie 1413: job->cmdFILE = stdout;
1414: Job_Touch(gn, job->flags&JOB_SILENT);
1.41 espie 1415: noExec = true;
1.1 deraadt 1416: }
1.66 espie 1417:
1.1 deraadt 1418: /*
1.66 espie 1419: * If we're not supposed to execute a shell, don't.
1.1 deraadt 1420: */
1.66 espie 1421: if (noExec) {
1422: /*
1423: * Unlink and close the command file if we opened one
1424: */
1425: if (job->cmdFILE != stdout) {
1426: (void)eunlink(tfile);
1427: if (job->cmdFILE != NULL)
1428: (void)fclose(job->cmdFILE);
1429: } else {
1430: (void)fflush(stdout);
1431: }
1.1 deraadt 1432:
1.66 espie 1433: /*
1434: * We only want to work our way up the graph if we aren't here
1435: * because the commands for the job were no good.
1436: */
1437: if (cmdsOK) {
1438: if (aborting == 0) {
1.68 espie 1439: Lst_ForEachFrom(job->tailCmds, JobSaveCommand,
1.66 espie 1440: job->node);
1441: Make_Update(job->node);
1442: }
1443: free(job);
1444: return JOB_FINISHED;
1445: } else {
1446: free(job);
1447: return JOB_ERROR;
1448: }
1.1 deraadt 1449: } else {
1.66 espie 1450: (void)fflush(job->cmdFILE);
1451: (void)eunlink(tfile);
1.1 deraadt 1452: }
1453:
1454: /*
1.66 espie 1455: * Set up the control arguments to the shell. This is based on the flags
1456: * set earlier for this job.
1.1 deraadt 1457: */
1.66 espie 1458: JobMakeArgv(job, argv);
1.1 deraadt 1459:
1.66 espie 1460: /*
1461: * If we're using pipes to catch output, create the pipe by which we'll
1462: * get the shell's output. If we're using files, print out that we're
1463: * starting a job and then set up its temporary-file name.
1464: */
1465: if (!compatMake || (job->flags & JOB_FIRST)) {
1.76 espie 1466: int fd[2];
1467: if (pipe(fd) == -1)
1468: Punt("Cannot create pipe: %s", strerror(errno));
1469: job->inPipe = fd[0];
1470: job->outPipe = fd[1];
1471: (void)fcntl(job->inPipe, F_SETFD, 1);
1472: (void)fcntl(job->outPipe, F_SETFD, 1);
1.1 deraadt 1473: }
1474:
1.41 espie 1475: local = true;
1.1 deraadt 1476:
1.79 espie 1477: if (local && nLocal >= maxLocal && !(job->flags & JOB_SPECIAL) &&
1478: maxLocal != 0) {
1479: /*
1480: * The job can only be run locally, but we've hit the limit of
1481: * local concurrency, so put the job on hold until some other
1482: * job finishes. Note that the special jobs (.BEGIN, .INTERRUPT
1483: * and .END) may be run locally even when the local limit has
1484: * been reached (e.g. when maxLocal == 0), though they will be
1485: * exported if at all possible. In addition, any target marked
1486: * with .NOEXPORT will be run locally if maxLocal is 0.
1.66 espie 1487: */
1488: jobFull = true;
1.6 millert 1489:
1.66 espie 1490: if (DEBUG(JOB)) {
1491: (void)fprintf(stdout, "Can only run job locally.\n");
1492: (void)fflush(stdout);
1493: }
1494: job->flags |= JOB_RESTART;
1495: Lst_AtEnd(&stoppedJobs, job);
1.79 espie 1496: } else {
1497: if (nLocal >= maxLocal && local) {
1498: /*
1499: * If we're running this job locally as a special case
1500: * (see above), at least say the table is full.
1501: */
1502: jobFull = true;
1503: if (DEBUG(JOB)) {
1504: (void)fprintf(stdout,
1505: "Local job queue is full.\n");
1506: (void)fflush(stdout);
1507: }
1508: }
1509: JobExec(job, argv);
1.1 deraadt 1510: }
1.66 espie 1511: return JOB_RUNNING;
1.1 deraadt 1512: }
1513:
1.6 millert 1514: static char *
1.56 espie 1515: JobOutput(Job *job, char *cp, char *endp, int msg)
1.2 deraadt 1516: {
1.66 espie 1517: char *ecp;
1.2 deraadt 1518:
1.74 espie 1519: ecp = strstr(cp, SHELL_ECHO_OFF);
1520: while (ecp != NULL) {
1521: if (cp != ecp) {
1522: *ecp = '\0';
1523: if (msg && job->node != lastNode) {
1524: MESSAGE(stdout, job->node);
1525: lastNode = job->node;
1.66 espie 1526: }
1.74 espie 1527: /*
1.78 espie 1528: * The only way there wouldn't be a newline after
1529: * this line is if it were the last in the buffer.
1530: * however, since the non-printable comes after it,
1531: * there must be a newline, so we don't print one.
1.74 espie 1532: */
1533: (void)fprintf(stdout, "%s", cp);
1534: (void)fflush(stdout);
1535: }
1536: cp = ecp + strlen(SHELL_ECHO_OFF);
1537: if (cp != endp) {
1538: /*
1.78 espie 1539: * Still more to print, look again after skipping
1540: * the whitespace following the non-printable
1541: * command....
1.74 espie 1542: */
1543: cp++;
1.78 espie 1544: while (*cp == ' ' || *cp == '\t' || *cp == '\n') {
1.66 espie 1545: cp++;
1546: }
1.74 espie 1547: ecp = strstr(cp, SHELL_ECHO_OFF);
1548: } else {
1549: return cp;
1.2 deraadt 1550: }
1551: }
1.66 espie 1552: return cp;
1.2 deraadt 1553: }
1554:
1.1 deraadt 1555: /*-
1556: *-----------------------------------------------------------------------
1.40 espie 1557: * JobDoOutput --
1.1 deraadt 1558: * This function is called at different times depending on
1559: * whether the user has specified that output is to be collected
1560: * via pipes or temporary files. In the former case, we are called
1561: * whenever there is something to read on the pipe. We collect more
1562: * output from the given job and store it in the job's outBuf. If
1563: * this makes up a line, we print it tagged by the job's identifier,
1564: * as necessary.
1565: * If output has been collected in a temporary file, we open the
1566: * file and read it line by line, transfering it to our own
1567: * output channel until the file is empty. At which point we
1568: * remove the temporary file.
1569: * In both cases, however, we keep our figurative eye out for the
1570: * 'noPrint' line for the shell from which the output came. If
1571: * we recognize a line, we don't print it. If the command is not
1.6 millert 1572: * alone on the line (the character after it is not \0 or \n), we
1.1 deraadt 1573: * do print whatever follows it.
1574: *
1575: * Side Effects:
1576: * curPos may be shifted as may the contents of outBuf.
1577: *-----------------------------------------------------------------------
1578: */
1.48 espie 1579: static void
1.66 espie 1580: JobDoOutput(Job *job, /* the job whose output needs printing */
1581: bool finish) /* true if this is the last time we'll be
1582: * called for this job */
1583: {
1.78 espie 1584: bool gotNL = false; /* true if got a newline */
1.66 espie 1585: bool fbuf; /* true if our buffer filled up */
1586: int nr; /* number of bytes read */
1587: int i; /* auxiliary index into outBuf */
1.78 espie 1588: int max; /* limit for i (end of current data) */
1589: int nRead; /* (Temporary) number of bytes read */
1.1 deraadt 1590:
1.76 espie 1591: /*
1592: * Read as many bytes as will fit in the buffer.
1593: */
1.1 deraadt 1594: end_loop:
1.76 espie 1595: gotNL = false;
1596: fbuf = false;
1.6 millert 1597:
1.76 espie 1598: nRead = read(job->inPipe, &job->outBuf[job->curPos],
1599: JOB_BUFSIZE - job->curPos);
1600: if (nRead == -1) {
1601: if (DEBUG(JOB)) {
1602: perror("JobDoOutput(piperead)");
1.66 espie 1603: }
1.76 espie 1604: nr = 0;
1605: } else {
1606: nr = nRead;
1607: }
1.1 deraadt 1608:
1.76 espie 1609: /*
1.78 espie 1610: * If we hit the end-of-file (the job is dead), we must flush its
1611: * remaining output, so pretend we read a newline if there's any
1612: * output remaining in the buffer.
1613: * Also clear the 'finish' flag so we stop looping.
1.76 espie 1614: */
1615: if (nr == 0 && job->curPos != 0) {
1616: job->outBuf[job->curPos] = '\n';
1617: nr = 1;
1618: finish = false;
1619: } else if (nr == 0) {
1620: finish = false;
1621: }
1.6 millert 1622:
1.76 espie 1623: /*
1.78 espie 1624: * Look for the last newline in the bytes we just got. If there is
1625: * one, break out of the loop with 'i' as its index and gotNL set
1626: * true.
1.76 espie 1627: */
1628: max = job->curPos + nr;
1629: for (i = job->curPos + nr - 1; i >= job->curPos; i--) {
1630: if (job->outBuf[i] == '\n') {
1631: gotNL = true;
1632: break;
1633: } else if (job->outBuf[i] == '\0') {
1634: /*
1635: * Why?
1636: */
1637: job->outBuf[i] = ' ';
1.66 espie 1638: }
1.76 espie 1639: }
1.6 millert 1640:
1.76 espie 1641: if (!gotNL) {
1642: job->curPos += nr;
1643: if (job->curPos == JOB_BUFSIZE) {
1.66 espie 1644: /*
1.78 espie 1645: * If we've run out of buffer space, we have no choice
1646: * but to print the stuff. sigh.
1.66 espie 1647: */
1.76 espie 1648: fbuf = true;
1649: i = job->curPos;
1650: }
1651: }
1652: if (gotNL || fbuf) {
1653: /*
1.78 espie 1654: * Need to send the output to the screen. Null terminate it
1655: * first, overwriting the newline character if there was one.
1656: * So long as the line isn't one we should filter (according
1657: * to the shell description), we print the line, preceded
1658: * by a target banner if this target isn't the same as the
1659: * one for which we last printed something.
1660: * The rest of the data in the buffer are then shifted down
1661: * to the start of the buffer and curPos is set accordingly.
1.76 espie 1662: */
1663: job->outBuf[i] = '\0';
1664: if (i >= job->curPos) {
1665: char *cp;
1.66 espie 1666:
1.78 espie 1667: cp = JobOutput(job, job->outBuf, &job->outBuf[i],
1668: false);
1.66 espie 1669:
1.76 espie 1670: /*
1.78 espie 1671: * There's still more in that thar buffer. This time,
1672: * though, we know there's no newline at the end, so we
1673: * add one of our own free will.
1.76 espie 1674: */
1675: if (*cp != '\0') {
1676: if (job->node != lastNode) {
1677: MESSAGE(stdout, job->node);
1678: lastNode = job->node;
1.66 espie 1679: }
1.76 espie 1680: (void)fprintf(stdout, "%s%s", cp,
1681: gotNL ? "\n" : "");
1682: (void)fflush(stdout);
1.66 espie 1683: }
1.76 espie 1684: }
1685: if (i < max - 1) {
1686: /* shift the remaining characters down */
1687: (void)memcpy(job->outBuf, &job->outBuf[i + 1],
1688: max - (i + 1));
1689: job->curPos = max - (i + 1);
1.66 espie 1690:
1.76 espie 1691: } else {
1.66 espie 1692: /*
1.78 espie 1693: * We have written everything out, so we just start over
1694: * from the start of the buffer. No copying. No nothing.
1.66 espie 1695: */
1.76 espie 1696: job->curPos = 0;
1.66 espie 1697: }
1.76 espie 1698: }
1699: if (finish) {
1.66 espie 1700: /*
1.78 espie 1701: * If the finish flag is true, we must loop until we hit
1702: * end-of-file on the pipe. This is guaranteed to happen
1703: * eventually since the other end of the pipe is now closed
1704: * (we closed it explicitly and the child has exited). When
1705: * we do get an EOF, finish will be set false and we'll fall
1706: * through and out.
1.76 espie 1707: */
1708: goto end_loop;
1.1 deraadt 1709: }
1710: }
1711:
1712: /*-
1713: *-----------------------------------------------------------------------
1714: * Job_CatchChildren --
1715: * Handle the exit of a child. Called from Make_Make.
1716: *
1717: * Side Effects:
1718: * The job descriptor is removed from the list of children.
1719: *
1720: * Notes:
1721: * We do waits, blocking or not, according to the wisdom of our
1722: * caller, until there are no more children to report. For each
1723: * job, call JobFinish to finish things off. This will take care of
1724: * putting jobs on the stoppedJobs queue.
1725: *-----------------------------------------------------------------------
1726: */
1727: void
1.76 espie 1728: Job_CatchChildren()
1.1 deraadt 1729: {
1.78 espie 1730: pid_t pid; /* pid of dead child */
1731: Job *job; /* job descriptor for dead child */
1732: LstNode jnode; /* list element for finding job */
1733: int status; /* Exit/termination status */
1.1 deraadt 1734:
1.66 espie 1735: /*
1736: * Don't even bother if we know there's no one around.
1737: */
1.79 espie 1738: if (nLocal == 0) {
1.66 espie 1739: return;
1.2 deraadt 1740: }
1.6 millert 1741:
1.76 espie 1742: while ((pid = waitpid((pid_t) -1, &status, WNOHANG|WUNTRACED)) > 0) {
1.66 espie 1743: HandleSigs();
1744: if (DEBUG(JOB)) {
1.68 espie 1745: (void)fprintf(stdout,
1.66 espie 1746: "Process %ld exited or stopped.\n", (long)pid);
1747: (void)fflush(stdout);
1748: }
1749:
1.1 deraadt 1750:
1.66 espie 1751: jnode = Lst_Find(&jobs, JobCmpPid, &pid);
1.1 deraadt 1752:
1.18 espie 1753: if (jnode == NULL) {
1.68 espie 1754: if (WIFSIGNALED(status) &&
1.66 espie 1755: (WTERMSIG(status) == SIGCONT)) {
1756: jnode = Lst_Find(&stoppedJobs, JobCmpPid, &pid);
1757: if (jnode == NULL) {
1758: Error("Resumed child (%ld) not in table", (long)pid);
1759: continue;
1760: }
1761: job = (Job *)Lst_Datum(jnode);
1762: Lst_Remove(&stoppedJobs, jnode);
1763: } else {
1764: Error("Child (%ld) not in table?", (long)pid);
1765: continue;
1766: }
1767: } else {
1768: job = (Job *)Lst_Datum(jnode);
1769: Lst_Remove(&jobs, jnode);
1.67 espie 1770: nJobs--;
1.66 espie 1771: if (jobFull && DEBUG(JOB)) {
1.68 espie 1772: (void)fprintf(stdout,
1.66 espie 1773: "Job queue is no longer full.\n");
1774: (void)fflush(stdout);
1775: }
1776: jobFull = false;
1.79 espie 1777: nLocal--;
1.1 deraadt 1778: }
1.66 espie 1779:
1780: JobFinish(job, &status);
1.1 deraadt 1781: }
1782: }
1783:
1784: /*-
1785: *-----------------------------------------------------------------------
1786: * Job_CatchOutput --
1787: * Catch the output from our children, if we're using
1788: * pipes do so. Otherwise just block time until we get a
1.6 millert 1789: * signal (most likely a SIGCHLD) since there's no point in
1.1 deraadt 1790: * just spinning when there's nothing to do and the reaping
1.6 millert 1791: * of a child can wait for a while.
1.1 deraadt 1792: *
1793: * Side Effects:
1794: * Output is read from pipes if we're piping.
1795: * -----------------------------------------------------------------------
1796: */
1797: void
1.56 espie 1798: Job_CatchOutput(void)
1.1 deraadt 1799: {
1.66 espie 1800: int nfds;
1801: struct timeval timeout;
1802: LstNode ln;
1803: Job *job;
1804:
1.76 espie 1805: int count = howmany(outputsn+1, NFDBITS) * sizeof(fd_mask);
1806: fd_set *readfdsp = malloc(count);
1.77 espie 1807: (void)fflush(stdout);
1.76 espie 1808: if (readfdsp == NULL)
1809: return;
1810:
1811: memcpy(readfdsp, outputsp, count);
1812: timeout.tv_sec = SEL_SEC;
1813: timeout.tv_usec = SEL_USEC;
1.66 espie 1814:
1.76 espie 1815: if ((nfds = select(outputsn+1, readfdsp, (fd_set *) 0,
1816: (fd_set *) 0, &timeout)) <= 0) {
1817: HandleSigs();
1818: free(readfdsp);
1819: return;
1820: } else {
1821: HandleSigs();
1822: for (ln = Lst_First(&jobs); nfds && ln != NULL;
1823: ln = Lst_Adv(ln)) {
1824: job = (Job *)Lst_Datum(ln);
1825: if (FD_ISSET(job->inPipe, readfdsp)) {
1826: JobDoOutput(job, false);
1827: nfds--;
1.66 espie 1828: }
1.1 deraadt 1829: }
1830: }
1.76 espie 1831: free(readfdsp);
1.1 deraadt 1832: }
1833:
1834: /*-
1835: *-----------------------------------------------------------------------
1836: * Job_Make --
1837: * Start the creation of a target. Basically a front-end for
1838: * JobStart used by the Make module.
1839: *
1840: * Side Effects:
1841: * Another job is started.
1842: *-----------------------------------------------------------------------
1843: */
1844: void
1.56 espie 1845: Job_Make(GNode *gn)
1.1 deraadt 1846: {
1.66 espie 1847: (void)JobStart(gn, 0, NULL);
1.1 deraadt 1848: }
1849:
1850: /*-
1851: *-----------------------------------------------------------------------
1852: * Job_Init --
1853: * Initialize the process module
1854: *
1855: * Side Effects:
1856: * lists and counters are initialized
1857: *-----------------------------------------------------------------------
1858: */
1859: void
1.79 espie 1860: Job_Init(int maxproc, int maxlocal)
1.1 deraadt 1861: {
1.66 espie 1862: int tfd;
1863:
1864: (void)strlcpy(tfile, TMPPAT, sizeof(tfile));
1865: if ((tfd = mkstemp(tfile)) == -1)
1866: Punt("Cannot create temp file: %s", strerror(errno));
1867: else
1868: (void)close(tfd);
1869:
1870: Static_Lst_Init(&jobs);
1871: Static_Lst_Init(&stoppedJobs);
1872: maxJobs = maxproc;
1.79 espie 1873: maxLocal = maxlocal;
1874: nJobs = 0;
1875: nLocal = 0;
1.66 espie 1876: jobFull = false;
1877:
1878: aborting = 0;
1879: errors = 0;
1.40 espie 1880:
1.66 espie 1881: lastNode = NULL;
1.1 deraadt 1882:
1.66 espie 1883: if (maxJobs == 1) {
1884: /*
1885: * If only one job can run at a time, there's no need for a
1886: * banner, no is there?
1887: */
1888: targFmt = "";
1889: } else {
1890: targFmt = TARG_FMT;
1891: }
1.1 deraadt 1892:
1893: /*
1.66 espie 1894: * Catch the four signals that POSIX specifies if they aren't ignored.
1895: * JobPassSig will take care of calling JobInterrupt if appropriate.
1.1 deraadt 1896: */
1.66 espie 1897: if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
1898: (void)signal(SIGINT, SigHandler);
1899: }
1900: if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
1901: (void)signal(SIGHUP, SigHandler);
1902: }
1903: if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
1904: (void)signal(SIGQUIT, SigHandler);
1905: }
1906: if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
1907: (void)signal(SIGTERM, SigHandler);
1908: }
1.1 deraadt 1909: /*
1.66 espie 1910: * There are additional signals that need to be caught and passed if
1911: * either the export system wants to be told directly of signals or if
1912: * we're giving each job its own process group (since then it won't get
1913: * signals from the terminal driver as we own the terminal)
1.1 deraadt 1914: */
1.48 espie 1915: #if defined(USE_PGRP)
1.66 espie 1916: if (signal(SIGTSTP, SIG_IGN) != SIG_IGN) {
1917: (void)signal(SIGTSTP, SigHandler);
1918: }
1919: if (signal(SIGTTOU, SIG_IGN) != SIG_IGN) {
1920: (void)signal(SIGTTOU, SigHandler);
1921: }
1922: if (signal(SIGTTIN, SIG_IGN) != SIG_IGN) {
1923: (void)signal(SIGTTIN, SigHandler);
1924: }
1925: if (signal(SIGWINCH, SIG_IGN) != SIG_IGN) {
1926: (void)signal(SIGWINCH, SigHandler);
1927: }
1.1 deraadt 1928: #endif
1.6 millert 1929:
1.78 espie 1930: if ((begin_node->type & OP_DUMMY) == 0) {
1931: JobStart(begin_node, JOB_SPECIAL, (Job *)0);
1.66 espie 1932: while (nJobs) {
1933: Job_CatchOutput();
1.76 espie 1934: Job_CatchChildren();
1.66 espie 1935: }
1.1 deraadt 1936: }
1937: }
1938:
1939: /*-
1940: *-----------------------------------------------------------------------
1941: * Job_Full --
1942: * See if the job table is full. It is considered full if it is OR
1943: * if we are in the process of aborting OR if we have
1944: * reached/exceeded our local quota. This prevents any more jobs
1945: * from starting up.
1946: *
1947: * Results:
1.41 espie 1948: * true if the job table is full, false otherwise
1.1 deraadt 1949: *-----------------------------------------------------------------------
1950: */
1.41 espie 1951: bool
1.56 espie 1952: Job_Full(void)
1.1 deraadt 1953: {
1.66 espie 1954: return aborting || jobFull;
1.1 deraadt 1955: }
1956:
1957: /*-
1958: *-----------------------------------------------------------------------
1959: * Job_Empty --
1.40 espie 1960: * See if the job table is empty. Because the local concurrency may
1.1 deraadt 1961: * be set to 0, it is possible for the job table to become empty,
1962: * while the list of stoppedJobs remains non-empty. In such a case,
1963: * we want to restart as many jobs as we can.
1964: *
1965: * Results:
1.41 espie 1966: * true if it is. false if it ain't.
1.1 deraadt 1967: * -----------------------------------------------------------------------
1968: */
1.41 espie 1969: bool
1.56 espie 1970: Job_Empty(void)
1.1 deraadt 1971: {
1.66 espie 1972: if (nJobs == 0) {
1973: if (!Lst_IsEmpty(&stoppedJobs) && !aborting) {
1974: /*
1975: * The job table is obviously not full if it has no
1976: * jobs in it...Try and restart the stopped jobs.
1977: */
1978: jobFull = false;
1979: JobRestartJobs();
1980: return false;
1981: } else {
1982: return true;
1983: }
1.1 deraadt 1984: } else {
1.66 espie 1985: return false;
1.1 deraadt 1986: }
1987: }
1988:
1989: /*-
1990: *-----------------------------------------------------------------------
1991: * JobInterrupt --
1992: * Handle the receipt of an interrupt.
1993: *
1994: * Side Effects:
1995: * All children are killed. Another job will be started if the
1996: * .INTERRUPT target was given.
1997: *-----------------------------------------------------------------------
1998: */
1999: static void
1.56 espie 2000: JobInterrupt(int runINTERRUPT, /* Non-zero if commands for the .INTERRUPT
1.1 deraadt 2001: * target should be executed */
1.66 espie 2002: int signo) /* signal received */
1.1 deraadt 2003: {
1.66 espie 2004: LstNode ln; /* element in job table */
1.78 espie 2005: Job *job; /* job descriptor in that element */
1.66 espie 2006:
2007: aborting = ABORT_INTERRUPT;
2008:
2009: for (ln = Lst_First(&jobs); ln != NULL; ln = Lst_Adv(ln)) {
2010: job = (Job *)Lst_Datum(ln);
2011:
2012: if (!Targ_Precious(job->node)) {
2013: const char *file = job->node->path == NULL ?
2014: job->node->name : job->node->path;
2015: if (!noExecute && eunlink(file) != -1) {
2016: Error("*** %s removed", file);
2017: }
2018: }
2019: if (job->pid) {
2020: if (DEBUG(JOB)) {
2021: (void)fprintf(stdout,
2022: "JobInterrupt passing signal to child %ld.\n",
2023: (long)job->pid);
2024: (void)fflush(stdout);
2025: }
2026: KILL(job->pid, signo);
2027: }
1.2 deraadt 2028: }
1.1 deraadt 2029:
1.66 espie 2030: if (runINTERRUPT && !touchFlag) {
1.78 espie 2031: if ((interrupt_node->type & OP_DUMMY) == 0) {
1.66 espie 2032: ignoreErrors = false;
2033:
1.78 espie 2034: JobStart(interrupt_node, JOB_IGNDOTS, (Job *)0);
1.66 espie 2035: while (nJobs) {
2036: Job_CatchOutput();
1.76 espie 2037: Job_CatchChildren();
1.66 espie 2038: }
2039: }
1.1 deraadt 2040: }
1.66 espie 2041: (void)eunlink(tfile);
2042: exit(signo);
1.1 deraadt 2043: }
2044:
2045: /*
2046: *-----------------------------------------------------------------------
1.12 espie 2047: * Job_Finish --
1.1 deraadt 2048: * Do final processing such as the running of the commands
1.6 millert 2049: * attached to the .END target.
1.1 deraadt 2050: *
2051: * Results:
2052: * Number of errors reported.
1.40 espie 2053: *
2054: * Side Effects:
2055: * The process' temporary file (tfile) is removed if it still
2056: * existed.
1.1 deraadt 2057: *-----------------------------------------------------------------------
2058: */
2059: int
1.56 espie 2060: Job_Finish(void)
1.1 deraadt 2061: {
1.78 espie 2062: if (end_node != NULL && !Lst_IsEmpty(&end_node->commands)) {
1.66 espie 2063: if (errors) {
2064: Error("Errors reported so .END ignored");
2065: } else {
1.78 espie 2066: JobStart(end_node, JOB_SPECIAL | JOB_IGNDOTS, NULL);
1.1 deraadt 2067:
1.66 espie 2068: while (nJobs) {
2069: Job_CatchOutput();
1.76 espie 2070: Job_CatchChildren();
1.66 espie 2071: }
2072: }
1.1 deraadt 2073: }
1.66 espie 2074: (void)eunlink(tfile);
2075: return errors;
1.1 deraadt 2076: }
2077:
1.41 espie 2078: #ifdef CLEANUP
1.12 espie 2079: void
1.56 espie 2080: Job_End(void)
1.12 espie 2081: {
1.41 espie 2082: }
1.13 espie 2083: #endif
1.40 espie 2084:
1.1 deraadt 2085: /*-
2086: *-----------------------------------------------------------------------
2087: * Job_Wait --
2088: * Waits for all running jobs to finish and returns. Sets 'aborting'
2089: * to ABORT_WAIT to prevent other jobs from starting.
2090: *
2091: * Side Effects:
2092: * Currently running jobs finish.
2093: *
2094: *-----------------------------------------------------------------------
2095: */
2096: void
1.56 espie 2097: Job_Wait(void)
1.1 deraadt 2098: {
1.66 espie 2099: aborting = ABORT_WAIT;
2100: while (nJobs != 0) {
2101: Job_CatchOutput();
1.76 espie 2102: Job_CatchChildren();
1.66 espie 2103: }
2104: aborting = 0;
1.1 deraadt 2105: }
2106:
2107: /*-
2108: *-----------------------------------------------------------------------
2109: * Job_AbortAll --
2110: * Abort all currently running jobs without handling output or anything.
2111: * This function is to be called only in the event of a major
2112: * error. Most definitely NOT to be called from JobInterrupt.
2113: *
2114: * Side Effects:
2115: * All children are killed, not just the firstborn
2116: *-----------------------------------------------------------------------
2117: */
2118: void
1.56 espie 2119: Job_AbortAll(void)
1.1 deraadt 2120: {
1.66 espie 2121: LstNode ln; /* element in job table */
2122: Job *job; /* the job descriptor in that element */
2123: int foo;
1.6 millert 2124:
1.66 espie 2125: aborting = ABORT_ERROR;
1.6 millert 2126:
1.66 espie 2127: if (nJobs) {
2128: for (ln = Lst_First(&jobs); ln != NULL; ln = Lst_Adv(ln)) {
2129: job = (Job *)Lst_Datum(ln);
2130:
2131: /*
2132: * kill the child process with increasingly drastic
2133: * signals to make darn sure it's dead.
2134: */
2135: KILL(job->pid, SIGINT);
2136: KILL(job->pid, SIGKILL);
2137: }
1.1 deraadt 2138: }
1.6 millert 2139:
1.66 espie 2140: /*
2141: * Catch as many children as want to report in at first, then give up
2142: */
2143: while (waitpid(-1, &foo, WNOHANG) > 0)
2144: continue;
2145: (void)eunlink(tfile);
1.2 deraadt 2146: }
1.40 espie 2147:
1.2 deraadt 2148: /*-
2149: *-----------------------------------------------------------------------
2150: * JobRestartJobs --
2151: * Tries to restart stopped jobs if there are slots available.
2152: * Note that this tries to restart them regardless of pending errors.
2153: * It's not good to leave stopped jobs lying around!
2154: *
2155: * Side Effects:
2156: * Resumes(and possibly migrates) jobs.
2157: *-----------------------------------------------------------------------
2158: */
2159: static void
1.56 espie 2160: JobRestartJobs(void)
1.2 deraadt 2161: {
1.66 espie 2162: Job *job;
1.19 espie 2163:
1.66 espie 2164: while (!jobFull && (job = (Job *)Lst_DeQueue(&stoppedJobs)) != NULL) {
2165: if (DEBUG(JOB)) {
2166: (void)fprintf(stdout,
2167: "Job queue is not full. Restarting a stopped job.\n");
2168: (void)fflush(stdout);
2169: }
2170: JobRestart(job);
1.2 deraadt 2171: }
1.1 deraadt 2172: }