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