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