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