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