Annotation of src/usr.bin/make/job.c, Revision 1.79
1.40 espie 1: /* $OpenPackages$ */
1.79 ! espie 2: /* $OpenBSD: job.c,v 1.78 2007/09/17 12:42:09 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)) {
! 1143: /*
! 1144: * Can't be exported and not allowed to run locally -- put
! 1145: * it back on the hold queue and mark the table full
! 1146: */
! 1147: if (DEBUG(JOB)) {
! 1148: (void)fprintf(stdout, "holding\n");
! 1149: (void)fflush(stdout);
! 1150: }
! 1151: Lst_AtFront(&stoppedJobs, job);
! 1152: jobFull = true;
! 1153: if (DEBUG(JOB)) {
! 1154: (void)fprintf(stdout, "Job queue is full.\n");
! 1155: (void)fflush(stdout);
! 1156: }
! 1157: return;
1.66 espie 1158: } else {
1.79 ! espie 1159: /*
! 1160: * Job may be run locally.
! 1161: */
! 1162: if (DEBUG(JOB)) {
! 1163: (void)fprintf(stdout, "running locally\n");
! 1164: (void)fflush(stdout);
! 1165: }
1.1 deraadt 1166: }
1.66 espie 1167: JobExec(job, argv);
1168: } else {
1.1 deraadt 1169: /*
1.66 espie 1170: * The job has stopped and needs to be restarted. Why it
1171: * stopped, we don't know...
1.1 deraadt 1172: */
1173: if (DEBUG(JOB)) {
1.66 espie 1174: (void)fprintf(stdout, "Resuming %s...", job->node->name);
1175: (void)fflush(stdout);
1.1 deraadt 1176: }
1.79 ! espie 1177: if ((nLocal < maxLocal || ((job->flags & JOB_SPECIAL) &&
! 1178: maxLocal == 0)) && nJobs != maxJobs) {
1.66 espie 1179: /*
1.79 ! espie 1180: * If we haven't reached the concurrency limit already
! 1181: * (or maxLocal is 0), it's ok to resume the job.
1.66 espie 1182: */
1183: bool error;
1184: int status;
1185:
1186: error = KILL(job->pid, SIGCONT) != 0;
1187:
1188: if (!error) {
1189: /*
1190: * Make sure the user knows we've continued the
1191: * beast and actually put the thing in the job
1192: * table.
1193: */
1194: job->flags |= JOB_CONTINUING;
1195: W_SETTERMSIG(&status, SIGCONT);
1196: JobFinish(job, &status);
1197:
1198: job->flags &= ~(JOB_RESUME|JOB_CONTINUING);
1199: if (DEBUG(JOB)) {
1200: (void)fprintf(stdout, "done\n");
1201: (void)fflush(stdout);
1202: }
1203: } else {
1204: Error("couldn't resume %s: %s",
1205: job->node->name, strerror(errno));
1206: status = 0;
1207: W_SETEXITSTATUS(&status, 1);
1208: JobFinish(job, &status);
1209: }
1210: } else {
1211: /*
1212: * Job cannot be restarted. Mark the table as full and
1213: * place the job back on the list of stopped jobs.
1214: */
1215: if (DEBUG(JOB)) {
1216: (void)fprintf(stdout, "table full\n");
1217: (void)fflush(stdout);
1218: }
1219: Lst_AtFront(&stoppedJobs, job);
1220: jobFull = true;
1221: if (DEBUG(JOB)) {
1222: (void)fprintf(stdout, "Job queue is full.\n");
1223: (void)fflush(stdout);
1224: }
1.1 deraadt 1225: }
1226: }
1227: }
1228:
1229: /*-
1230: *-----------------------------------------------------------------------
1231: * JobStart --
1232: * Start a target-creation process going for the target described
1.6 millert 1233: * by the graph node gn.
1.1 deraadt 1234: *
1235: * Results:
1236: * JOB_ERROR if there was an error in the commands, JOB_FINISHED
1237: * if there isn't actually anything left to do for the job and
1238: * JOB_RUNNING if the job has been started.
1239: *
1240: * Side Effects:
1241: * A new Job node is created and added to the list of running
1242: * jobs. PMake is forked and a child shell created.
1243: *-----------------------------------------------------------------------
1244: */
1245: static int
1.78 espie 1246: JobStart(GNode *gn, /* target to create */
1247: int flags, /* flags for the job to override normal ones.
1248: * e.g. JOB_SPECIAL or JOB_IGNDOTS */
1249: Job *previous) /* The previous Job structure for this node,
1250: * if any. */
1251: {
1252: Job *job; /* new job descriptor */
1253: char *argv[4]; /* Argument vector to shell */
1254: bool cmdsOK; /* true if the nodes commands were all right */
1255: bool local; /* Set true if the job was run locally */
1256: bool noExec; /* Set true if we decide not to run the job */
1.66 espie 1257:
1258: if (previous != NULL) {
1259: previous->flags &= ~(JOB_FIRST|JOB_IGNERR|JOB_SILENT);
1260: job = previous;
1261: } else {
1262: job = emalloc(sizeof(Job));
1263: if (job == NULL) {
1264: Punt("JobStart out of memory");
1265: }
1266: flags |= JOB_FIRST;
1.1 deraadt 1267: }
1268:
1.66 espie 1269: job->node = gn;
1270: job->tailCmds = NULL;
1.1 deraadt 1271:
1272: /*
1.66 espie 1273: * Set the initial value of the flags for this job based on the global
1274: * ones and the node's attributes... Any flags supplied by the caller
1275: * are also added to the field.
1.1 deraadt 1276: */
1.66 espie 1277: job->flags = 0;
1278: if (Targ_Ignore(gn)) {
1279: job->flags |= JOB_IGNERR;
1280: }
1281: if (Targ_Silent(gn)) {
1282: job->flags |= JOB_SILENT;
1.1 deraadt 1283: }
1.66 espie 1284: job->flags |= flags;
1.6 millert 1285:
1.1 deraadt 1286: /*
1.66 espie 1287: * Check the commands now so any attributes from .DEFAULT have a chance
1288: * to migrate to the node
1.1 deraadt 1289: */
1.66 espie 1290: if (!compatMake && job->flags & JOB_FIRST) {
1291: cmdsOK = Job_CheckCommands(gn, Error);
1292: } else {
1293: cmdsOK = true;
1294: }
1.1 deraadt 1295:
1296: /*
1.66 espie 1297: * If the -n flag wasn't given, we open up OUR (not the child's)
1298: * temporary file to stuff commands in it. The thing is rd/wr so we
1299: * don't need to reopen it to feed it to the shell. If the -n flag
1300: * *was* given, we just set the file to be stdout. Cute, huh?
1.1 deraadt 1301: */
1.66 espie 1302: if ((gn->type & OP_MAKE) || (!noExecute && !touchFlag)) {
1303: /*
1304: * We're serious here, but if the commands were bogus, we're
1305: * also dead...
1306: */
1307: if (!cmdsOK) {
1308: DieHorribly();
1309: }
1.6 millert 1310:
1.66 espie 1311: job->cmdFILE = fopen(tfile, "w+");
1312: if (job->cmdFILE == NULL) {
1313: Punt("Could not open %s", tfile);
1314: }
1315: (void)fcntl(FILENO(job->cmdFILE), F_SETFD, 1);
1316: /*
1317: * Send the commands to the command file, flush all its buffers
1318: * then rewind and remove the thing.
1319: */
1320: noExec = false;
1321:
1322: /*
1323: * used to be backwards; replace when start doing multiple
1324: * commands per shell.
1325: */
1326: if (compatMake) {
1327: /*
1328: * Be compatible: If this is the first time for this
1329: * node, verify its commands are ok and open the
1330: * commands list for sequential access by later
1331: * invocations of JobStart. Once that is done, we take
1332: * the next command off the list and print it to the
1333: * command file. If the command was an ellipsis, note
1334: * that there's nothing more to execute.
1335: */
1336: if ((job->flags&JOB_FIRST))
1337: gn->current = Lst_First(&gn->commands);
1338: else
1339: gn->current = Lst_Succ(gn->current);
1340:
1341: if (gn->current == NULL ||
1342: !JobPrintCommand(gn->current, job)) {
1343: noExec = true;
1344: gn->current = NULL;
1345: }
1346: if (noExec && !(job->flags & JOB_FIRST)) {
1347: /*
1348: * If we're not going to execute anything, the
1349: * job is done and we need to close down the
1350: * various file descriptors we've opened for
1351: * output, then call JobDoOutput to catch the
1352: * final characters or send the file to the
1353: * screen... Note that the i/o streams are only
1354: * open if this isn't the first job. Note also
1355: * that this could not be done in
1356: * Job_CatchChildren b/c it wasn't clear if
1357: * there were more commands to execute or
1358: * not...
1359: */
1360: JobClose(job);
1361: }
1362: } else {
1363: /*
1364: * We can do all the commands at once. hooray for
1365: * sanity
1366: */
1367: numCommands = 0;
1.68 espie 1368: Lst_ForEachNodeWhile(&gn->commands, JobPrintCommand,
1.66 espie 1369: job);
1370:
1371: /*
1372: * If we didn't print out any commands to the shell
1373: * script, there's not much point in executing the
1374: * shell, is there?
1375: */
1376: if (numCommands == 0) {
1377: noExec = true;
1378: }
1379: }
1380: } else if (noExecute) {
1381: /*
1382: * Not executing anything -- just print all the commands to
1383: * stdout in one fell swoop. This will still set up
1384: * job->tailCmds correctly.
1385: */
1386: if (lastNode != gn) {
1387: MESSAGE(stdout, gn);
1388: lastNode = gn;
1389: }
1390: job->cmdFILE = stdout;
1391: /*
1392: * Only print the commands if they're ok, but don't die if
1393: * they're not -- just let the user know they're bad and keep
1394: * going. It doesn't do any harm in this case and may do some
1395: * good.
1396: */
1397: if (cmdsOK) {
1.68 espie 1398: Lst_ForEachNodeWhile(&gn->commands, JobPrintCommand,
1.66 espie 1399: job);
1400: }
1401: /*
1402: * Don't execute the shell, thank you.
1403: */
1.41 espie 1404: noExec = true;
1.66 espie 1405: } else {
1406: /*
1407: * Just touch the target and note that no shell should be
1408: * executed. Set cmdFILE to stdout to make life easier. Check
1409: * the commands, too, but don't die if they're no good -- it
1410: * does no harm to keep working up the graph.
1.30 espie 1411: */
1.66 espie 1412: job->cmdFILE = stdout;
1413: Job_Touch(gn, job->flags&JOB_SILENT);
1.41 espie 1414: noExec = true;
1.1 deraadt 1415: }
1.66 espie 1416:
1.1 deraadt 1417: /*
1.66 espie 1418: * If we're not supposed to execute a shell, don't.
1.1 deraadt 1419: */
1.66 espie 1420: if (noExec) {
1421: /*
1422: * Unlink and close the command file if we opened one
1423: */
1424: if (job->cmdFILE != stdout) {
1425: (void)eunlink(tfile);
1426: if (job->cmdFILE != NULL)
1427: (void)fclose(job->cmdFILE);
1428: } else {
1429: (void)fflush(stdout);
1430: }
1.1 deraadt 1431:
1.66 espie 1432: /*
1433: * We only want to work our way up the graph if we aren't here
1434: * because the commands for the job were no good.
1435: */
1436: if (cmdsOK) {
1437: if (aborting == 0) {
1.68 espie 1438: Lst_ForEachFrom(job->tailCmds, JobSaveCommand,
1.66 espie 1439: job->node);
1440: Make_Update(job->node);
1441: }
1442: free(job);
1443: return JOB_FINISHED;
1444: } else {
1445: free(job);
1446: return JOB_ERROR;
1447: }
1.1 deraadt 1448: } else {
1.66 espie 1449: (void)fflush(job->cmdFILE);
1450: (void)eunlink(tfile);
1.1 deraadt 1451: }
1452:
1453: /*
1.66 espie 1454: * Set up the control arguments to the shell. This is based on the flags
1455: * set earlier for this job.
1.1 deraadt 1456: */
1.66 espie 1457: JobMakeArgv(job, argv);
1.1 deraadt 1458:
1.66 espie 1459: /*
1460: * If we're using pipes to catch output, create the pipe by which we'll
1461: * get the shell's output. If we're using files, print out that we're
1462: * starting a job and then set up its temporary-file name.
1463: */
1464: if (!compatMake || (job->flags & JOB_FIRST)) {
1.76 espie 1465: int fd[2];
1466: if (pipe(fd) == -1)
1467: Punt("Cannot create pipe: %s", strerror(errno));
1468: job->inPipe = fd[0];
1469: job->outPipe = fd[1];
1470: (void)fcntl(job->inPipe, F_SETFD, 1);
1471: (void)fcntl(job->outPipe, F_SETFD, 1);
1.1 deraadt 1472: }
1473:
1.41 espie 1474: local = true;
1.1 deraadt 1475:
1.79 ! espie 1476: if (local && nLocal >= maxLocal && !(job->flags & JOB_SPECIAL) &&
! 1477: maxLocal != 0) {
! 1478: /*
! 1479: * The job can only be run locally, but we've hit the limit of
! 1480: * local concurrency, so put the job on hold until some other
! 1481: * job finishes. Note that the special jobs (.BEGIN, .INTERRUPT
! 1482: * and .END) may be run locally even when the local limit has
! 1483: * been reached (e.g. when maxLocal == 0), though they will be
! 1484: * exported if at all possible. In addition, any target marked
! 1485: * with .NOEXPORT will be run locally if maxLocal is 0.
1.66 espie 1486: */
1487: jobFull = true;
1.6 millert 1488:
1.66 espie 1489: if (DEBUG(JOB)) {
1490: (void)fprintf(stdout, "Can only run job locally.\n");
1491: (void)fflush(stdout);
1492: }
1493: job->flags |= JOB_RESTART;
1494: Lst_AtEnd(&stoppedJobs, job);
1.79 ! espie 1495: } else {
! 1496: if (nLocal >= maxLocal && local) {
! 1497: /*
! 1498: * If we're running this job locally as a special case
! 1499: * (see above), at least say the table is full.
! 1500: */
! 1501: jobFull = true;
! 1502: if (DEBUG(JOB)) {
! 1503: (void)fprintf(stdout,
! 1504: "Local job queue is full.\n");
! 1505: (void)fflush(stdout);
! 1506: }
! 1507: }
! 1508: JobExec(job, argv);
1.1 deraadt 1509: }
1.66 espie 1510: return JOB_RUNNING;
1.1 deraadt 1511: }
1512:
1.6 millert 1513: static char *
1.56 espie 1514: JobOutput(Job *job, char *cp, char *endp, int msg)
1.2 deraadt 1515: {
1.66 espie 1516: char *ecp;
1.2 deraadt 1517:
1.74 espie 1518: ecp = strstr(cp, SHELL_ECHO_OFF);
1519: while (ecp != NULL) {
1520: if (cp != ecp) {
1521: *ecp = '\0';
1522: if (msg && job->node != lastNode) {
1523: MESSAGE(stdout, job->node);
1524: lastNode = job->node;
1.66 espie 1525: }
1.74 espie 1526: /*
1.78 espie 1527: * The only way there wouldn't be a newline after
1528: * this line is if it were the last in the buffer.
1529: * however, since the non-printable comes after it,
1530: * there must be a newline, so we don't print one.
1.74 espie 1531: */
1532: (void)fprintf(stdout, "%s", cp);
1533: (void)fflush(stdout);
1534: }
1535: cp = ecp + strlen(SHELL_ECHO_OFF);
1536: if (cp != endp) {
1537: /*
1.78 espie 1538: * Still more to print, look again after skipping
1539: * the whitespace following the non-printable
1540: * command....
1.74 espie 1541: */
1542: cp++;
1.78 espie 1543: while (*cp == ' ' || *cp == '\t' || *cp == '\n') {
1.66 espie 1544: cp++;
1545: }
1.74 espie 1546: ecp = strstr(cp, SHELL_ECHO_OFF);
1547: } else {
1548: return cp;
1.2 deraadt 1549: }
1550: }
1.66 espie 1551: return cp;
1.2 deraadt 1552: }
1553:
1.1 deraadt 1554: /*-
1555: *-----------------------------------------------------------------------
1.40 espie 1556: * JobDoOutput --
1.1 deraadt 1557: * This function is called at different times depending on
1558: * whether the user has specified that output is to be collected
1559: * via pipes or temporary files. In the former case, we are called
1560: * whenever there is something to read on the pipe. We collect more
1561: * output from the given job and store it in the job's outBuf. If
1562: * this makes up a line, we print it tagged by the job's identifier,
1563: * as necessary.
1564: * If output has been collected in a temporary file, we open the
1565: * file and read it line by line, transfering it to our own
1566: * output channel until the file is empty. At which point we
1567: * remove the temporary file.
1568: * In both cases, however, we keep our figurative eye out for the
1569: * 'noPrint' line for the shell from which the output came. If
1570: * we recognize a line, we don't print it. If the command is not
1.6 millert 1571: * alone on the line (the character after it is not \0 or \n), we
1.1 deraadt 1572: * do print whatever follows it.
1573: *
1574: * Side Effects:
1575: * curPos may be shifted as may the contents of outBuf.
1576: *-----------------------------------------------------------------------
1577: */
1.48 espie 1578: static void
1.66 espie 1579: JobDoOutput(Job *job, /* the job whose output needs printing */
1580: bool finish) /* true if this is the last time we'll be
1581: * called for this job */
1582: {
1.78 espie 1583: bool gotNL = false; /* true if got a newline */
1.66 espie 1584: bool fbuf; /* true if our buffer filled up */
1585: int nr; /* number of bytes read */
1586: int i; /* auxiliary index into outBuf */
1.78 espie 1587: int max; /* limit for i (end of current data) */
1588: int nRead; /* (Temporary) number of bytes read */
1.1 deraadt 1589:
1.76 espie 1590: /*
1591: * Read as many bytes as will fit in the buffer.
1592: */
1.1 deraadt 1593: end_loop:
1.76 espie 1594: gotNL = false;
1595: fbuf = false;
1.6 millert 1596:
1.76 espie 1597: nRead = read(job->inPipe, &job->outBuf[job->curPos],
1598: JOB_BUFSIZE - job->curPos);
1599: if (nRead == -1) {
1600: if (DEBUG(JOB)) {
1601: perror("JobDoOutput(piperead)");
1.66 espie 1602: }
1.76 espie 1603: nr = 0;
1604: } else {
1605: nr = nRead;
1606: }
1.1 deraadt 1607:
1.76 espie 1608: /*
1.78 espie 1609: * If we hit the end-of-file (the job is dead), we must flush its
1610: * remaining output, so pretend we read a newline if there's any
1611: * output remaining in the buffer.
1612: * Also clear the 'finish' flag so we stop looping.
1.76 espie 1613: */
1614: if (nr == 0 && job->curPos != 0) {
1615: job->outBuf[job->curPos] = '\n';
1616: nr = 1;
1617: finish = false;
1618: } else if (nr == 0) {
1619: finish = false;
1620: }
1.6 millert 1621:
1.76 espie 1622: /*
1.78 espie 1623: * Look for the last newline in the bytes we just got. If there is
1624: * one, break out of the loop with 'i' as its index and gotNL set
1625: * true.
1.76 espie 1626: */
1627: max = job->curPos + nr;
1628: for (i = job->curPos + nr - 1; i >= job->curPos; i--) {
1629: if (job->outBuf[i] == '\n') {
1630: gotNL = true;
1631: break;
1632: } else if (job->outBuf[i] == '\0') {
1633: /*
1634: * Why?
1635: */
1636: job->outBuf[i] = ' ';
1.66 espie 1637: }
1.76 espie 1638: }
1.6 millert 1639:
1.76 espie 1640: if (!gotNL) {
1641: job->curPos += nr;
1642: if (job->curPos == JOB_BUFSIZE) {
1.66 espie 1643: /*
1.78 espie 1644: * If we've run out of buffer space, we have no choice
1645: * but to print the stuff. sigh.
1.66 espie 1646: */
1.76 espie 1647: fbuf = true;
1648: i = job->curPos;
1649: }
1650: }
1651: if (gotNL || fbuf) {
1652: /*
1.78 espie 1653: * Need to send the output to the screen. Null terminate it
1654: * first, overwriting the newline character if there was one.
1655: * So long as the line isn't one we should filter (according
1656: * to the shell description), we print the line, preceded
1657: * by a target banner if this target isn't the same as the
1658: * one for which we last printed something.
1659: * The rest of the data in the buffer are then shifted down
1660: * to the start of the buffer and curPos is set accordingly.
1.76 espie 1661: */
1662: job->outBuf[i] = '\0';
1663: if (i >= job->curPos) {
1664: char *cp;
1.66 espie 1665:
1.78 espie 1666: cp = JobOutput(job, job->outBuf, &job->outBuf[i],
1667: false);
1.66 espie 1668:
1.76 espie 1669: /*
1.78 espie 1670: * There's still more in that thar buffer. This time,
1671: * though, we know there's no newline at the end, so we
1672: * add one of our own free will.
1.76 espie 1673: */
1674: if (*cp != '\0') {
1675: if (job->node != lastNode) {
1676: MESSAGE(stdout, job->node);
1677: lastNode = job->node;
1.66 espie 1678: }
1.76 espie 1679: (void)fprintf(stdout, "%s%s", cp,
1680: gotNL ? "\n" : "");
1681: (void)fflush(stdout);
1.66 espie 1682: }
1.76 espie 1683: }
1684: if (i < max - 1) {
1685: /* shift the remaining characters down */
1686: (void)memcpy(job->outBuf, &job->outBuf[i + 1],
1687: max - (i + 1));
1688: job->curPos = max - (i + 1);
1.66 espie 1689:
1.76 espie 1690: } else {
1.66 espie 1691: /*
1.78 espie 1692: * We have written everything out, so we just start over
1693: * from the start of the buffer. No copying. No nothing.
1.66 espie 1694: */
1.76 espie 1695: job->curPos = 0;
1.66 espie 1696: }
1.76 espie 1697: }
1698: if (finish) {
1.66 espie 1699: /*
1.78 espie 1700: * If the finish flag is true, we must loop until we hit
1701: * end-of-file on the pipe. This is guaranteed to happen
1702: * eventually since the other end of the pipe is now closed
1703: * (we closed it explicitly and the child has exited). When
1704: * we do get an EOF, finish will be set false and we'll fall
1705: * through and out.
1.76 espie 1706: */
1707: goto end_loop;
1.1 deraadt 1708: }
1709: }
1710:
1711: /*-
1712: *-----------------------------------------------------------------------
1713: * Job_CatchChildren --
1714: * Handle the exit of a child. Called from Make_Make.
1715: *
1716: * Side Effects:
1717: * The job descriptor is removed from the list of children.
1718: *
1719: * Notes:
1720: * We do waits, blocking or not, according to the wisdom of our
1721: * caller, until there are no more children to report. For each
1722: * job, call JobFinish to finish things off. This will take care of
1723: * putting jobs on the stoppedJobs queue.
1724: *-----------------------------------------------------------------------
1725: */
1726: void
1.76 espie 1727: Job_CatchChildren()
1.1 deraadt 1728: {
1.78 espie 1729: pid_t pid; /* pid of dead child */
1730: Job *job; /* job descriptor for dead child */
1731: LstNode jnode; /* list element for finding job */
1732: int status; /* Exit/termination status */
1.1 deraadt 1733:
1.66 espie 1734: /*
1735: * Don't even bother if we know there's no one around.
1736: */
1.79 ! espie 1737: if (nLocal == 0) {
1.66 espie 1738: return;
1.2 deraadt 1739: }
1.6 millert 1740:
1.76 espie 1741: while ((pid = waitpid((pid_t) -1, &status, WNOHANG|WUNTRACED)) > 0) {
1.66 espie 1742: HandleSigs();
1743: if (DEBUG(JOB)) {
1.68 espie 1744: (void)fprintf(stdout,
1.66 espie 1745: "Process %ld exited or stopped.\n", (long)pid);
1746: (void)fflush(stdout);
1747: }
1748:
1.1 deraadt 1749:
1.66 espie 1750: jnode = Lst_Find(&jobs, JobCmpPid, &pid);
1.1 deraadt 1751:
1.18 espie 1752: if (jnode == NULL) {
1.68 espie 1753: if (WIFSIGNALED(status) &&
1.66 espie 1754: (WTERMSIG(status) == SIGCONT)) {
1755: jnode = Lst_Find(&stoppedJobs, JobCmpPid, &pid);
1756: if (jnode == NULL) {
1757: Error("Resumed child (%ld) not in table", (long)pid);
1758: continue;
1759: }
1760: job = (Job *)Lst_Datum(jnode);
1761: Lst_Remove(&stoppedJobs, jnode);
1762: } else {
1763: Error("Child (%ld) not in table?", (long)pid);
1764: continue;
1765: }
1766: } else {
1767: job = (Job *)Lst_Datum(jnode);
1768: Lst_Remove(&jobs, jnode);
1.67 espie 1769: nJobs--;
1.66 espie 1770: if (jobFull && DEBUG(JOB)) {
1.68 espie 1771: (void)fprintf(stdout,
1.66 espie 1772: "Job queue is no longer full.\n");
1773: (void)fflush(stdout);
1774: }
1775: jobFull = false;
1.79 ! espie 1776: nLocal--;
1.1 deraadt 1777: }
1.66 espie 1778:
1779: JobFinish(job, &status);
1.1 deraadt 1780: }
1781: }
1782:
1783: /*-
1784: *-----------------------------------------------------------------------
1785: * Job_CatchOutput --
1786: * Catch the output from our children, if we're using
1787: * pipes do so. Otherwise just block time until we get a
1.6 millert 1788: * signal (most likely a SIGCHLD) since there's no point in
1.1 deraadt 1789: * just spinning when there's nothing to do and the reaping
1.6 millert 1790: * of a child can wait for a while.
1.1 deraadt 1791: *
1792: * Side Effects:
1793: * Output is read from pipes if we're piping.
1794: * -----------------------------------------------------------------------
1795: */
1796: void
1.56 espie 1797: Job_CatchOutput(void)
1.1 deraadt 1798: {
1.66 espie 1799: int nfds;
1800: struct timeval timeout;
1801: LstNode ln;
1802: Job *job;
1803:
1.76 espie 1804: int count = howmany(outputsn+1, NFDBITS) * sizeof(fd_mask);
1805: fd_set *readfdsp = malloc(count);
1.77 espie 1806: (void)fflush(stdout);
1.76 espie 1807: if (readfdsp == NULL)
1808: return;
1809:
1810: memcpy(readfdsp, outputsp, count);
1811: timeout.tv_sec = SEL_SEC;
1812: timeout.tv_usec = SEL_USEC;
1.66 espie 1813:
1.76 espie 1814: if ((nfds = select(outputsn+1, readfdsp, (fd_set *) 0,
1815: (fd_set *) 0, &timeout)) <= 0) {
1816: HandleSigs();
1817: free(readfdsp);
1818: return;
1819: } else {
1820: HandleSigs();
1821: for (ln = Lst_First(&jobs); nfds && ln != NULL;
1822: ln = Lst_Adv(ln)) {
1823: job = (Job *)Lst_Datum(ln);
1824: if (FD_ISSET(job->inPipe, readfdsp)) {
1825: JobDoOutput(job, false);
1826: nfds--;
1.66 espie 1827: }
1.1 deraadt 1828: }
1829: }
1.76 espie 1830: free(readfdsp);
1.1 deraadt 1831: }
1832:
1833: /*-
1834: *-----------------------------------------------------------------------
1835: * Job_Make --
1836: * Start the creation of a target. Basically a front-end for
1837: * JobStart used by the Make module.
1838: *
1839: * Side Effects:
1840: * Another job is started.
1841: *-----------------------------------------------------------------------
1842: */
1843: void
1.56 espie 1844: Job_Make(GNode *gn)
1.1 deraadt 1845: {
1.66 espie 1846: (void)JobStart(gn, 0, NULL);
1.1 deraadt 1847: }
1848:
1849: /*-
1850: *-----------------------------------------------------------------------
1851: * Job_Init --
1852: * Initialize the process module
1853: *
1854: * Side Effects:
1855: * lists and counters are initialized
1856: *-----------------------------------------------------------------------
1857: */
1858: void
1.79 ! espie 1859: Job_Init(int maxproc, int maxlocal)
1.1 deraadt 1860: {
1.66 espie 1861: int tfd;
1862:
1863: (void)strlcpy(tfile, TMPPAT, sizeof(tfile));
1864: if ((tfd = mkstemp(tfile)) == -1)
1865: Punt("Cannot create temp file: %s", strerror(errno));
1866: else
1867: (void)close(tfd);
1868:
1869: Static_Lst_Init(&jobs);
1870: Static_Lst_Init(&stoppedJobs);
1871: maxJobs = maxproc;
1.79 ! espie 1872: maxLocal = maxlocal;
! 1873: nJobs = 0;
! 1874: nLocal = 0;
1.66 espie 1875: jobFull = false;
1876:
1877: aborting = 0;
1878: errors = 0;
1.40 espie 1879:
1.66 espie 1880: lastNode = NULL;
1.1 deraadt 1881:
1.66 espie 1882: if (maxJobs == 1) {
1883: /*
1884: * If only one job can run at a time, there's no need for a
1885: * banner, no is there?
1886: */
1887: targFmt = "";
1888: } else {
1889: targFmt = TARG_FMT;
1890: }
1.1 deraadt 1891:
1892: /*
1.66 espie 1893: * Catch the four signals that POSIX specifies if they aren't ignored.
1894: * JobPassSig will take care of calling JobInterrupt if appropriate.
1.1 deraadt 1895: */
1.66 espie 1896: if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
1897: (void)signal(SIGINT, SigHandler);
1898: }
1899: if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
1900: (void)signal(SIGHUP, SigHandler);
1901: }
1902: if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
1903: (void)signal(SIGQUIT, SigHandler);
1904: }
1905: if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
1906: (void)signal(SIGTERM, SigHandler);
1907: }
1.1 deraadt 1908: /*
1.66 espie 1909: * There are additional signals that need to be caught and passed if
1910: * either the export system wants to be told directly of signals or if
1911: * we're giving each job its own process group (since then it won't get
1912: * signals from the terminal driver as we own the terminal)
1.1 deraadt 1913: */
1.48 espie 1914: #if defined(USE_PGRP)
1.66 espie 1915: if (signal(SIGTSTP, SIG_IGN) != SIG_IGN) {
1916: (void)signal(SIGTSTP, SigHandler);
1917: }
1918: if (signal(SIGTTOU, SIG_IGN) != SIG_IGN) {
1919: (void)signal(SIGTTOU, SigHandler);
1920: }
1921: if (signal(SIGTTIN, SIG_IGN) != SIG_IGN) {
1922: (void)signal(SIGTTIN, SigHandler);
1923: }
1924: if (signal(SIGWINCH, SIG_IGN) != SIG_IGN) {
1925: (void)signal(SIGWINCH, SigHandler);
1926: }
1.1 deraadt 1927: #endif
1.6 millert 1928:
1.78 espie 1929: if ((begin_node->type & OP_DUMMY) == 0) {
1930: JobStart(begin_node, JOB_SPECIAL, (Job *)0);
1.66 espie 1931: while (nJobs) {
1932: Job_CatchOutput();
1.76 espie 1933: Job_CatchChildren();
1.66 espie 1934: }
1.1 deraadt 1935: }
1936: }
1937:
1938: /*-
1939: *-----------------------------------------------------------------------
1940: * Job_Full --
1941: * See if the job table is full. It is considered full if it is OR
1942: * if we are in the process of aborting OR if we have
1943: * reached/exceeded our local quota. This prevents any more jobs
1944: * from starting up.
1945: *
1946: * Results:
1.41 espie 1947: * true if the job table is full, false otherwise
1.1 deraadt 1948: *-----------------------------------------------------------------------
1949: */
1.41 espie 1950: bool
1.56 espie 1951: Job_Full(void)
1.1 deraadt 1952: {
1.66 espie 1953: return aborting || jobFull;
1.1 deraadt 1954: }
1955:
1956: /*-
1957: *-----------------------------------------------------------------------
1958: * Job_Empty --
1.40 espie 1959: * See if the job table is empty. Because the local concurrency may
1.1 deraadt 1960: * be set to 0, it is possible for the job table to become empty,
1961: * while the list of stoppedJobs remains non-empty. In such a case,
1962: * we want to restart as many jobs as we can.
1963: *
1964: * Results:
1.41 espie 1965: * true if it is. false if it ain't.
1.1 deraadt 1966: * -----------------------------------------------------------------------
1967: */
1.41 espie 1968: bool
1.56 espie 1969: Job_Empty(void)
1.1 deraadt 1970: {
1.66 espie 1971: if (nJobs == 0) {
1972: if (!Lst_IsEmpty(&stoppedJobs) && !aborting) {
1973: /*
1974: * The job table is obviously not full if it has no
1975: * jobs in it...Try and restart the stopped jobs.
1976: */
1977: jobFull = false;
1978: JobRestartJobs();
1979: return false;
1980: } else {
1981: return true;
1982: }
1.1 deraadt 1983: } else {
1.66 espie 1984: return false;
1.1 deraadt 1985: }
1986: }
1987:
1988: /*-
1989: *-----------------------------------------------------------------------
1990: * JobInterrupt --
1991: * Handle the receipt of an interrupt.
1992: *
1993: * Side Effects:
1994: * All children are killed. Another job will be started if the
1995: * .INTERRUPT target was given.
1996: *-----------------------------------------------------------------------
1997: */
1998: static void
1.56 espie 1999: JobInterrupt(int runINTERRUPT, /* Non-zero if commands for the .INTERRUPT
1.1 deraadt 2000: * target should be executed */
1.66 espie 2001: int signo) /* signal received */
1.1 deraadt 2002: {
1.66 espie 2003: LstNode ln; /* element in job table */
1.78 espie 2004: Job *job; /* job descriptor in that element */
1.66 espie 2005:
2006: aborting = ABORT_INTERRUPT;
2007:
2008: for (ln = Lst_First(&jobs); ln != NULL; ln = Lst_Adv(ln)) {
2009: job = (Job *)Lst_Datum(ln);
2010:
2011: if (!Targ_Precious(job->node)) {
2012: const char *file = job->node->path == NULL ?
2013: job->node->name : job->node->path;
2014: if (!noExecute && eunlink(file) != -1) {
2015: Error("*** %s removed", file);
2016: }
2017: }
2018: if (job->pid) {
2019: if (DEBUG(JOB)) {
2020: (void)fprintf(stdout,
2021: "JobInterrupt passing signal to child %ld.\n",
2022: (long)job->pid);
2023: (void)fflush(stdout);
2024: }
2025: KILL(job->pid, signo);
2026: }
1.2 deraadt 2027: }
1.1 deraadt 2028:
1.66 espie 2029: if (runINTERRUPT && !touchFlag) {
1.78 espie 2030: if ((interrupt_node->type & OP_DUMMY) == 0) {
1.66 espie 2031: ignoreErrors = false;
2032:
1.78 espie 2033: JobStart(interrupt_node, JOB_IGNDOTS, (Job *)0);
1.66 espie 2034: while (nJobs) {
2035: Job_CatchOutput();
1.76 espie 2036: Job_CatchChildren();
1.66 espie 2037: }
2038: }
1.1 deraadt 2039: }
1.66 espie 2040: (void)eunlink(tfile);
2041: exit(signo);
1.1 deraadt 2042: }
2043:
2044: /*
2045: *-----------------------------------------------------------------------
1.12 espie 2046: * Job_Finish --
1.1 deraadt 2047: * Do final processing such as the running of the commands
1.6 millert 2048: * attached to the .END target.
1.1 deraadt 2049: *
2050: * Results:
2051: * Number of errors reported.
1.40 espie 2052: *
2053: * Side Effects:
2054: * The process' temporary file (tfile) is removed if it still
2055: * existed.
1.1 deraadt 2056: *-----------------------------------------------------------------------
2057: */
2058: int
1.56 espie 2059: Job_Finish(void)
1.1 deraadt 2060: {
1.78 espie 2061: if (end_node != NULL && !Lst_IsEmpty(&end_node->commands)) {
1.66 espie 2062: if (errors) {
2063: Error("Errors reported so .END ignored");
2064: } else {
1.78 espie 2065: JobStart(end_node, JOB_SPECIAL | JOB_IGNDOTS, NULL);
1.1 deraadt 2066:
1.66 espie 2067: while (nJobs) {
2068: Job_CatchOutput();
1.76 espie 2069: Job_CatchChildren();
1.66 espie 2070: }
2071: }
1.1 deraadt 2072: }
1.66 espie 2073: (void)eunlink(tfile);
2074: return errors;
1.1 deraadt 2075: }
2076:
1.41 espie 2077: #ifdef CLEANUP
1.12 espie 2078: void
1.56 espie 2079: Job_End(void)
1.12 espie 2080: {
1.41 espie 2081: }
1.13 espie 2082: #endif
1.40 espie 2083:
1.1 deraadt 2084: /*-
2085: *-----------------------------------------------------------------------
2086: * Job_Wait --
2087: * Waits for all running jobs to finish and returns. Sets 'aborting'
2088: * to ABORT_WAIT to prevent other jobs from starting.
2089: *
2090: * Side Effects:
2091: * Currently running jobs finish.
2092: *
2093: *-----------------------------------------------------------------------
2094: */
2095: void
1.56 espie 2096: Job_Wait(void)
1.1 deraadt 2097: {
1.66 espie 2098: aborting = ABORT_WAIT;
2099: while (nJobs != 0) {
2100: Job_CatchOutput();
1.76 espie 2101: Job_CatchChildren();
1.66 espie 2102: }
2103: aborting = 0;
1.1 deraadt 2104: }
2105:
2106: /*-
2107: *-----------------------------------------------------------------------
2108: * Job_AbortAll --
2109: * Abort all currently running jobs without handling output or anything.
2110: * This function is to be called only in the event of a major
2111: * error. Most definitely NOT to be called from JobInterrupt.
2112: *
2113: * Side Effects:
2114: * All children are killed, not just the firstborn
2115: *-----------------------------------------------------------------------
2116: */
2117: void
1.56 espie 2118: Job_AbortAll(void)
1.1 deraadt 2119: {
1.66 espie 2120: LstNode ln; /* element in job table */
2121: Job *job; /* the job descriptor in that element */
2122: int foo;
1.6 millert 2123:
1.66 espie 2124: aborting = ABORT_ERROR;
1.6 millert 2125:
1.66 espie 2126: if (nJobs) {
2127: for (ln = Lst_First(&jobs); ln != NULL; ln = Lst_Adv(ln)) {
2128: job = (Job *)Lst_Datum(ln);
2129:
2130: /*
2131: * kill the child process with increasingly drastic
2132: * signals to make darn sure it's dead.
2133: */
2134: KILL(job->pid, SIGINT);
2135: KILL(job->pid, SIGKILL);
2136: }
1.1 deraadt 2137: }
1.6 millert 2138:
1.66 espie 2139: /*
2140: * Catch as many children as want to report in at first, then give up
2141: */
2142: while (waitpid(-1, &foo, WNOHANG) > 0)
2143: continue;
2144: (void)eunlink(tfile);
1.2 deraadt 2145: }
1.40 espie 2146:
1.2 deraadt 2147: /*-
2148: *-----------------------------------------------------------------------
2149: * JobRestartJobs --
2150: * Tries to restart stopped jobs if there are slots available.
2151: * Note that this tries to restart them regardless of pending errors.
2152: * It's not good to leave stopped jobs lying around!
2153: *
2154: * Side Effects:
2155: * Resumes(and possibly migrates) jobs.
2156: *-----------------------------------------------------------------------
2157: */
2158: static void
1.56 espie 2159: JobRestartJobs(void)
1.2 deraadt 2160: {
1.66 espie 2161: Job *job;
1.19 espie 2162:
1.66 espie 2163: while (!jobFull && (job = (Job *)Lst_DeQueue(&stoppedJobs)) != NULL) {
2164: if (DEBUG(JOB)) {
2165: (void)fprintf(stdout,
2166: "Job queue is not full. Restarting a stopped job.\n");
2167: (void)fflush(stdout);
2168: }
2169: JobRestart(job);
1.2 deraadt 2170: }
1.1 deraadt 2171: }