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