Annotation of src/usr.bin/make/job.c, Revision 1.97
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.96 espie 279: static void start_queued_job(Job *);
1.93 espie 280:
281: static volatile sig_atomic_t got_signal;
1.1 deraadt 282:
1.57 espie 283: static volatile sig_atomic_t got_SIGINT, got_SIGHUP, got_SIGQUIT,
1.93 espie 284: got_SIGTERM, got_SIGTSTP, got_SIGTTOU, got_SIGTTIN, got_SIGWINCH;
285:
1.57 espie 286:
1.88 espie 287: #define TMPPAT "/tmp/makeXXXXXXXXXX"
288:
289: static FILE *
290: new_command_file()
291: {
292: int fd;
293: FILE *f;
294: char tmp[] = TMPPAT;
295:
296: fd = mkstemp(tmp);
297: if (fd == -1)
298: return NULL;
299: f = fdopen(fd, "w");
300: if (f == NULL)
301: close(fd);
302: eunlink(tmp);
303: return f;
304: }
305:
1.57 espie 306: static void
307: SigHandler(int sig)
308: {
309: switch(sig) {
310: case SIGINT:
311: got_SIGINT++;
1.93 espie 312: got_signal = 1;
1.57 espie 313: break;
314: case SIGHUP:
315: got_SIGHUP++;
1.93 espie 316: got_signal = 1;
1.57 espie 317: break;
318: case SIGQUIT:
319: got_SIGQUIT++;
1.93 espie 320: got_signal = 1;
1.57 espie 321: break;
322: case SIGTERM:
323: got_SIGTERM++;
1.93 espie 324: got_signal = 1;
1.57 espie 325: break;
1.93 espie 326: #ifdef USE_PGRP
1.57 espie 327: case SIGTSTP:
328: got_SIGTSTP++;
1.93 espie 329: got_signal = 1;
1.57 espie 330: break;
331: case SIGTTOU:
332: got_SIGTTOU++;
1.93 espie 333: got_signal = 1;
1.57 espie 334: break;
335: case SIGTTIN:
336: got_SIGTTIN++;
1.93 espie 337: got_signal = 1;
1.57 espie 338: break;
339: case SIGWINCH:
340: got_SIGWINCH++;
1.93 espie 341: got_signal = 1;
1.57 espie 342: break;
343: #endif
344: }
345: }
346:
347: static void
1.93 espie 348: handle_all_signals()
1.57 espie 349: {
1.93 espie 350: if (got_signal)
351: got_signal = 0;
352: else
353: return;
354:
1.57 espie 355: if (got_SIGINT) {
356: got_SIGINT=0;
1.93 espie 357: handle_signal(SIGINT);
1.57 espie 358: }
359: if (got_SIGHUP) {
360: got_SIGHUP=0;
1.93 espie 361: handle_signal(SIGHUP);
1.57 espie 362: }
363: if (got_SIGQUIT) {
364: got_SIGQUIT=0;
1.93 espie 365: handle_signal(SIGQUIT);
1.57 espie 366: }
367: if (got_SIGTERM) {
368: got_SIGTERM=0;
1.93 espie 369: handle_signal(SIGTERM);
1.57 espie 370: }
371: if (got_SIGTSTP) {
372: got_SIGTSTP=0;
1.93 espie 373: handle_signal(SIGTSTP);
1.57 espie 374: }
375: if (got_SIGTTOU) {
376: got_SIGTTOU=0;
1.93 espie 377: handle_signal(SIGTTOU);
1.57 espie 378: }
379: if (got_SIGTTIN) {
380: got_SIGTTIN=0;
1.93 espie 381: handle_signal(SIGTTIN);
1.57 espie 382: }
383: if (got_SIGWINCH) {
384: got_SIGWINCH=0;
1.93 espie 385: handle_signal(SIGWINCH);
1.57 espie 386: }
387: }
388:
1.1 deraadt 389: /*-
390: *-----------------------------------------------------------------------
391: * JobCondPassSig --
1.50 espie 392: * Pass a signal to a job if USE_PGRP
1.1 deraadt 393: * is defined.
394: *
395: * Side Effects:
396: * None, except the job may bite it.
397: *-----------------------------------------------------------------------
398: */
1.27 espie 399: static void
1.93 espie 400: pass_signal_to_job(void *jobp, /* Job to biff */
1.56 espie 401: void *signop) /* Signal to send it */
1.1 deraadt 402: {
1.66 espie 403: Job *job = (Job *)jobp;
404: int signo = *(int *)signop;
405: if (DEBUG(JOB)) {
406: (void)fprintf(stdout,
1.93 espie 407: "pass_signal_to_job passing signal %d to child %ld.\n",
1.66 espie 408: signo, (long)job->pid);
409: (void)fflush(stdout);
410: }
411: KILL(job->pid, signo);
1.1 deraadt 412: }
413:
414: /*-
415: *-----------------------------------------------------------------------
1.94 espie 416: * handle_signal --
1.68 espie 417: * Pass a signal to all local jobs if USE_PGRP is defined,
1.50 espie 418: * then die ourselves.
1.1 deraadt 419: *
420: * Side Effects:
421: * We die by the same signal.
422: *-----------------------------------------------------------------------
423: */
424: static void
1.93 espie 425: handle_signal(int signo) /* The signal number we've received */
1.1 deraadt 426: {
1.66 espie 427: sigset_t nmask, omask;
428: struct sigaction act;
1.6 millert 429:
1.66 espie 430: if (DEBUG(JOB)) {
1.93 espie 431: (void)fprintf(stdout, "handle_signal(%d) called.\n", signo);
1.66 espie 432: (void)fflush(stdout);
433: }
1.95 espie 434: Lst_ForEach(&runningJobs, pass_signal_to_job, &signo);
1.66 espie 435:
436: /*
437: * Deal with proper cleanup based on the signal received. We only run
438: * the .INTERRUPT target if the signal was in fact an interrupt. The
439: * other three termination signals are more of a "get out *now*"
440: * command.
441: */
442: if (signo == SIGINT) {
443: JobInterrupt(true, signo);
444: } else if (signo == SIGHUP || signo == SIGTERM || signo == SIGQUIT) {
445: JobInterrupt(false, signo);
446: }
1.1 deraadt 447:
1.66 espie 448: /*
449: * Leave gracefully if SIGQUIT, rather than core dumping.
450: */
451: if (signo == SIGQUIT) {
452: Finish(0);
453: }
1.6 millert 454:
1.66 espie 455: /*
456: * Send ourselves the signal now we've given the message to everyone
457: * else. Note we block everything else possible while we're getting
458: * the signal. This ensures that all our jobs get continued when we
459: * wake up before we take any other signal.
460: */
461: sigemptyset(&nmask);
462: sigaddset(&nmask, signo);
463: sigprocmask(SIG_SETMASK, &nmask, &omask);
464: memset(&act, 0, sizeof act);
465: act.sa_handler = SIG_DFL;
466: sigemptyset(&act.sa_mask);
467: act.sa_flags = 0;
468: sigaction(signo, &act, NULL);
1.6 millert 469:
1.66 espie 470: if (DEBUG(JOB)) {
471: (void)fprintf(stdout,
1.93 espie 472: "handle_signal passing signal to self, mask = %x.\n",
1.66 espie 473: ~0 & ~(1 << (signo-1)));
474: (void)fflush(stdout);
475: }
476: (void)signal(signo, SIG_DFL);
1.1 deraadt 477:
1.66 espie 478: (void)KILL(getpid(), signo);
1.1 deraadt 479:
1.66 espie 480: signo = SIGCONT;
1.95 espie 481: Lst_ForEach(&runningJobs, pass_signal_to_job, &signo);
1.1 deraadt 482:
1.66 espie 483: (void)sigprocmask(SIG_SETMASK, &omask, NULL);
484: sigprocmask(SIG_SETMASK, &omask, NULL);
485: act.sa_handler = SigHandler;
486: sigaction(signo, &act, NULL);
1.1 deraadt 487: }
488:
489: /*-
490: *-----------------------------------------------------------------------
491: * JobCmpPid --
492: * Compare the pid of the job with the given pid and return 0 if they
493: * are equal. This function is called from Job_CatchChildren via
494: * Lst_Find to find the job descriptor of the finished job.
495: *
496: * Results:
497: * 0 if the pid's match
498: *-----------------------------------------------------------------------
499: */
500: static int
1.56 espie 501: JobCmpPid(void *job, /* job to examine */
502: void *pid) /* process id desired */
1.1 deraadt 503: {
1.66 espie 504: return *(pid_t *)pid - ((Job *)job)->pid;
1.1 deraadt 505: }
506:
1.69 espie 507: static void
508: DBPRINTF(Job *job, const char *fmt, ...)
509: {
510: va_list va;
511: va_start(va, fmt);
512: if (DEBUG(JOB)) {
513: (void)vfprintf(stdout, fmt, va);
514: fflush(stdout);
515: }
516: vfprintf(job->cmdFILE, fmt, va);
517: va_end(va);
518: }
519:
1.84 espie 520: static void
521: debug_printf(const char *fmt, ...)
522: {
523: if (DEBUG(JOB)) {
524: va_list va;
525:
526: va_start(va, fmt);
527: (void)vfprintf(stdout, fmt, va);
528: fflush(stdout);
529: va_end(va);
530: }
531: }
532:
1.1 deraadt 533: /*-
534: *-----------------------------------------------------------------------
535: * JobPrintCommand --
536: * Put out another command for the given job. If the command starts
537: * with an @ or a - we process it specially. In the former case,
538: * so long as the -s and -n flags weren't given to make, we stick
539: * a shell-specific echoOff command in the script. In the latter,
540: * we ignore errors for the entire job, unless the shell has error
541: * control.
542: * If the command is just "..." we take all future commands for this
543: * job to be commands to be executed once the entire graph has been
544: * made and return non-zero to signal that the end of the commands
1.78 espie 545: * was reached. These commands are later attached to the end_node
1.1 deraadt 546: * node and executed by Job_End when all things are done.
1.40 espie 547: * This function is called from JobStart via Lst_Find
1.1 deraadt 548: *
549: * Results:
1.26 espie 550: * Always 1, unless the command was "..."
1.1 deraadt 551: *
552: * Side Effects:
553: * If the command begins with a '-' and the shell has no error control,
554: * the JOB_IGNERR flag is set in the job descriptor.
555: * If the command is "..." and we're not ignoring such things,
556: * tailCmds is set to the successor node of the cmd.
557: * numCommands is incremented if the command is actually printed.
558: *-----------------------------------------------------------------------
559: */
560: static int
1.78 espie 561: JobPrintCommand(LstNode cmdNode, /* command string to print */
562: void *jobp) /* job for which to print it */
1.1 deraadt 563: {
1.78 espie 564: bool noSpecials; /* true if we shouldn't worry about
565: * inserting special commands into
566: * the input stream. */
567: bool shutUp = false; /* true if we put a no echo command
568: * into the command file */
569: bool errOff = false; /* true if we turned error checking
570: * off before printing the command
571: * and need to turn it back on */
572: char *cmdTemplate; /* Template to use when printing the
573: * command */
574: char *cmdStart; /* Start of expanded command */
1.66 espie 575: char *cmd = (char *)Lst_Datum(cmdNode);
576: Job *job = (Job *)jobp;
577:
578: noSpecials = (noExecute && !(job->node->type & OP_MAKE));
579:
580: if (strcmp(cmd, "...") == 0) {
581: job->node->type |= OP_SAVE_CMDS;
582: if ((job->flags & JOB_IGNDOTS) == 0) {
583: job->tailCmds = Lst_Succ(cmdNode);
584: return 0;
585: }
586: return 1;
1.2 deraadt 587: }
1.1 deraadt 588:
1.78 espie 589:
1.67 espie 590: numCommands++;
1.1 deraadt 591:
1.66 espie 592: /* For debugging, we replace each command with the result of expanding
593: * the variables in the command. */
594: cmdStart = cmd = Var_Subst(cmd, &job->node->context, false);
595: Lst_Replace(cmdNode, cmdStart);
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:
1174: /*-
1175: *-----------------------------------------------------------------------
1176: * JobStart --
1177: * Start a target-creation process going for the target described
1.6 millert 1178: * by the graph node gn.
1.1 deraadt 1179: *
1180: * Side Effects:
1181: * A new Job node is created and added to the list of running
1182: * jobs. PMake is forked and a child shell created.
1183: *-----------------------------------------------------------------------
1184: */
1.97 ! espie 1185: static void
1.78 espie 1186: JobStart(GNode *gn, /* target to create */
1.86 espie 1187: int flags) /* flags for the job to override normal ones.
1188: * e.g. JOB_SPECIAL */
1.78 espie 1189: {
1190: Job *job; /* new job descriptor */
1191: char *argv[4]; /* Argument vector to shell */
1192: bool cmdsOK; /* true if the nodes commands were all right */
1193: bool noExec; /* Set true if we decide not to run the job */
1.87 espie 1194: int fd[2];
1.66 espie 1195:
1.86 espie 1196: job = emalloc(sizeof(Job));
1197: if (job == NULL) {
1198: Punt("JobStart out of memory");
1.1 deraadt 1199: }
1200:
1.66 espie 1201: job->node = gn;
1202: job->tailCmds = NULL;
1.1 deraadt 1203:
1204: /*
1.66 espie 1205: * Set the initial value of the flags for this job based on the global
1206: * ones and the node's attributes... Any flags supplied by the caller
1207: * are also added to the field.
1.1 deraadt 1208: */
1.87 espie 1209: job->flags = flags;
1.66 espie 1210: if (Targ_Ignore(gn)) {
1211: job->flags |= JOB_IGNERR;
1212: }
1213: if (Targ_Silent(gn)) {
1214: job->flags |= JOB_SILENT;
1.1 deraadt 1215: }
1.6 millert 1216:
1.1 deraadt 1217: /*
1.66 espie 1218: * Check the commands now so any attributes from .DEFAULT have a chance
1219: * to migrate to the node
1.1 deraadt 1220: */
1.87 espie 1221: cmdsOK = Job_CheckCommands(gn, Error);
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.88 espie 1238: job->cmdFILE = new_command_file();
1.66 espie 1239: if (job->cmdFILE == NULL) {
1.88 espie 1240: Punt("Error creating command file");
1.66 espie 1241: }
1.90 espie 1242: (void)fcntl(fileno(job->cmdFILE), F_SETFD, FD_CLOEXEC);
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;
1.87 espie 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: if (job->cmdFILE != NULL)
1311: (void)fclose(job->cmdFILE);
1312: } else {
1313: (void)fflush(stdout);
1314: }
1.1 deraadt 1315:
1.66 espie 1316: /*
1317: * We only want to work our way up the graph if we aren't here
1318: * because the commands for the job were no good.
1319: */
1320: if (cmdsOK) {
1321: if (aborting == 0) {
1.68 espie 1322: Lst_ForEachFrom(job->tailCmds, JobSaveCommand,
1.66 espie 1323: job->node);
1324: Make_Update(job->node);
1325: }
1326: free(job);
1.97 ! espie 1327: return;
1.66 espie 1328: } else {
1329: free(job);
1.97 ! espie 1330: return;
1.66 espie 1331: }
1.1 deraadt 1332: } else {
1.66 espie 1333: (void)fflush(job->cmdFILE);
1.1 deraadt 1334: }
1335:
1.90 espie 1336: /* Create the pipe by which we'll get the shell's output.
1.66 espie 1337: */
1.87 espie 1338: if (pipe(fd) == -1)
1339: Punt("Cannot create pipe: %s", strerror(errno));
1340: job->inPipe = fd[0];
1341: job->outPipe = fd[1];
1.90 espie 1342: (void)fcntl(job->inPipe, F_SETFD, FD_CLOEXEC);
1343: (void)fcntl(job->outPipe, F_SETFD, FD_CLOEXEC);
1.1 deraadt 1344:
1.96 espie 1345: /*
1346: * Set up the control arguments to the shell. This is based on the flags
1347: * set earlier for this job.
1348: */
1349: JobMakeArgv(job, argv);
1350:
1.83 espie 1351: if (nJobs >= maxJobs && !(job->flags & JOB_SPECIAL) &&
1352: maxJobs != 0) {
1.79 espie 1353: /*
1354: * The job can only be run locally, but we've hit the limit of
1355: * local concurrency, so put the job on hold until some other
1356: * job finishes. Note that the special jobs (.BEGIN, .INTERRUPT
1357: * and .END) may be run locally even when the local limit has
1.83 espie 1358: * been reached (e.g. when maxJobs == 0), though they will be
1.79 espie 1359: * exported if at all possible. In addition, any target marked
1.83 espie 1360: * with .NOEXPORT will be run locally if maxJobs is 0.
1.66 espie 1361: */
1362: jobFull = true;
1.6 millert 1363:
1.84 espie 1364: debug_printf("Can only run job locally.\n");
1.66 espie 1365: job->flags |= JOB_RESTART;
1366: Lst_AtEnd(&stoppedJobs, job);
1.79 espie 1367: } else {
1.83 espie 1368: if (nJobs >= maxJobs) {
1.79 espie 1369: /*
1370: * If we're running this job locally as a special case
1371: * (see above), at least say the table is full.
1372: */
1373: jobFull = true;
1.84 espie 1374: debug_printf("Local job queue is full.\n");
1.79 espie 1375: }
1376: JobExec(job, argv);
1.1 deraadt 1377: }
1378: }
1379:
1.6 millert 1380: static char *
1.56 espie 1381: JobOutput(Job *job, char *cp, char *endp, int msg)
1.2 deraadt 1382: {
1.66 espie 1383: char *ecp;
1.2 deraadt 1384:
1.74 espie 1385: ecp = strstr(cp, SHELL_ECHO_OFF);
1386: while (ecp != NULL) {
1387: if (cp != ecp) {
1388: *ecp = '\0';
1389: if (msg && job->node != lastNode) {
1390: MESSAGE(stdout, job->node);
1391: lastNode = job->node;
1.66 espie 1392: }
1.74 espie 1393: /*
1.78 espie 1394: * The only way there wouldn't be a newline after
1395: * this line is if it were the last in the buffer.
1396: * however, since the non-printable comes after it,
1397: * there must be a newline, so we don't print one.
1.74 espie 1398: */
1399: (void)fprintf(stdout, "%s", cp);
1400: (void)fflush(stdout);
1401: }
1402: cp = ecp + strlen(SHELL_ECHO_OFF);
1403: if (cp != endp) {
1404: /*
1.78 espie 1405: * Still more to print, look again after skipping
1406: * the whitespace following the non-printable
1407: * command....
1.74 espie 1408: */
1409: cp++;
1.78 espie 1410: while (*cp == ' ' || *cp == '\t' || *cp == '\n') {
1.66 espie 1411: cp++;
1412: }
1.74 espie 1413: ecp = strstr(cp, SHELL_ECHO_OFF);
1414: } else {
1415: return cp;
1.2 deraadt 1416: }
1417: }
1.66 espie 1418: return cp;
1.2 deraadt 1419: }
1420:
1.1 deraadt 1421: /*-
1422: *-----------------------------------------------------------------------
1.40 espie 1423: * JobDoOutput --
1.89 espie 1424: * This functions is called whenever there is something to read on the
1425: * pipe. We collect more output from the given job and store it in the
1426: * job's outBuf. If this makes up a line, we print it tagged by the job's
1427: * identifier, as necessary.
1428: * We also keep our figurative eye out for the
1.1 deraadt 1429: * 'noPrint' line for the shell from which the output came. If
1430: * we recognize a line, we don't print it. If the command is not
1.6 millert 1431: * alone on the line (the character after it is not \0 or \n), we
1.1 deraadt 1432: * do print whatever follows it.
1433: *
1434: * Side Effects:
1435: * curPos may be shifted as may the contents of outBuf.
1436: *-----------------------------------------------------------------------
1437: */
1.48 espie 1438: static void
1.66 espie 1439: JobDoOutput(Job *job, /* the job whose output needs printing */
1440: bool finish) /* true if this is the last time we'll be
1441: * called for this job */
1442: {
1.78 espie 1443: bool gotNL = false; /* true if got a newline */
1.66 espie 1444: bool fbuf; /* true if our buffer filled up */
1445: int nr; /* number of bytes read */
1446: int i; /* auxiliary index into outBuf */
1.78 espie 1447: int max; /* limit for i (end of current data) */
1448: int nRead; /* (Temporary) number of bytes read */
1.1 deraadt 1449:
1.76 espie 1450: /*
1451: * Read as many bytes as will fit in the buffer.
1452: */
1.1 deraadt 1453: end_loop:
1.76 espie 1454: gotNL = false;
1455: fbuf = false;
1.6 millert 1456:
1.76 espie 1457: nRead = read(job->inPipe, &job->outBuf[job->curPos],
1458: JOB_BUFSIZE - job->curPos);
1459: if (nRead == -1) {
1460: if (DEBUG(JOB)) {
1461: perror("JobDoOutput(piperead)");
1.66 espie 1462: }
1.76 espie 1463: nr = 0;
1464: } else {
1465: nr = nRead;
1466: }
1.1 deraadt 1467:
1.76 espie 1468: /*
1.78 espie 1469: * If we hit the end-of-file (the job is dead), we must flush its
1470: * remaining output, so pretend we read a newline if there's any
1471: * output remaining in the buffer.
1472: * Also clear the 'finish' flag so we stop looping.
1.76 espie 1473: */
1474: if (nr == 0 && job->curPos != 0) {
1475: job->outBuf[job->curPos] = '\n';
1476: nr = 1;
1477: finish = false;
1478: } else if (nr == 0) {
1479: finish = false;
1480: }
1.6 millert 1481:
1.76 espie 1482: /*
1.78 espie 1483: * Look for the last newline in the bytes we just got. If there is
1484: * one, break out of the loop with 'i' as its index and gotNL set
1485: * true.
1.76 espie 1486: */
1487: max = job->curPos + nr;
1488: for (i = job->curPos + nr - 1; i >= job->curPos; i--) {
1489: if (job->outBuf[i] == '\n') {
1490: gotNL = true;
1491: break;
1492: } else if (job->outBuf[i] == '\0') {
1493: /*
1.89 espie 1494: * To be fixed: don't use printf, it stops at NUL bytes.
1.76 espie 1495: */
1496: job->outBuf[i] = ' ';
1.66 espie 1497: }
1.76 espie 1498: }
1.6 millert 1499:
1.76 espie 1500: if (!gotNL) {
1501: job->curPos += nr;
1502: if (job->curPos == JOB_BUFSIZE) {
1.66 espie 1503: /*
1.78 espie 1504: * If we've run out of buffer space, we have no choice
1505: * but to print the stuff. sigh.
1.66 espie 1506: */
1.76 espie 1507: fbuf = true;
1508: i = job->curPos;
1509: }
1510: }
1511: if (gotNL || fbuf) {
1512: /*
1.78 espie 1513: * Need to send the output to the screen. Null terminate it
1514: * first, overwriting the newline character if there was one.
1515: * So long as the line isn't one we should filter (according
1516: * to the shell description), we print the line, preceded
1517: * by a target banner if this target isn't the same as the
1518: * one for which we last printed something.
1519: * The rest of the data in the buffer are then shifted down
1520: * to the start of the buffer and curPos is set accordingly.
1.76 espie 1521: */
1522: job->outBuf[i] = '\0';
1523: if (i >= job->curPos) {
1524: char *cp;
1.66 espie 1525:
1.78 espie 1526: cp = JobOutput(job, job->outBuf, &job->outBuf[i],
1527: false);
1.66 espie 1528:
1.76 espie 1529: /*
1.78 espie 1530: * There's still more in that thar buffer. This time,
1531: * though, we know there's no newline at the end, so we
1532: * add one of our own free will.
1.76 espie 1533: */
1534: if (*cp != '\0') {
1535: if (job->node != lastNode) {
1536: MESSAGE(stdout, job->node);
1537: lastNode = job->node;
1.66 espie 1538: }
1.76 espie 1539: (void)fprintf(stdout, "%s%s", cp,
1540: gotNL ? "\n" : "");
1541: (void)fflush(stdout);
1.66 espie 1542: }
1.76 espie 1543: }
1544: if (i < max - 1) {
1545: /* shift the remaining characters down */
1546: (void)memcpy(job->outBuf, &job->outBuf[i + 1],
1547: max - (i + 1));
1548: job->curPos = max - (i + 1);
1.66 espie 1549:
1.76 espie 1550: } else {
1.66 espie 1551: /*
1.78 espie 1552: * We have written everything out, so we just start over
1553: * from the start of the buffer. No copying. No nothing.
1.66 espie 1554: */
1.76 espie 1555: job->curPos = 0;
1.66 espie 1556: }
1.76 espie 1557: }
1558: if (finish) {
1.66 espie 1559: /*
1.78 espie 1560: * If the finish flag is true, we must loop until we hit
1561: * end-of-file on the pipe. This is guaranteed to happen
1562: * eventually since the other end of the pipe is now closed
1563: * (we closed it explicitly and the child has exited). When
1564: * we do get an EOF, finish will be set false and we'll fall
1565: * through and out.
1.76 espie 1566: */
1567: goto end_loop;
1.1 deraadt 1568: }
1569: }
1570:
1571: /*-
1572: *-----------------------------------------------------------------------
1573: * Job_CatchChildren --
1574: * Handle the exit of a child. Called from Make_Make.
1575: *
1576: * Side Effects:
1577: * The job descriptor is removed from the list of children.
1578: *
1579: * Notes:
1580: * We do waits, blocking or not, according to the wisdom of our
1581: * caller, until there are no more children to report. For each
1582: * job, call JobFinish to finish things off. This will take care of
1583: * putting jobs on the stoppedJobs queue.
1584: *-----------------------------------------------------------------------
1585: */
1586: void
1.76 espie 1587: Job_CatchChildren()
1.1 deraadt 1588: {
1.78 espie 1589: pid_t pid; /* pid of dead child */
1590: Job *job; /* job descriptor for dead child */
1591: LstNode jnode; /* list element for finding job */
1592: int status; /* Exit/termination status */
1.1 deraadt 1593:
1.66 espie 1594: /*
1595: * Don't even bother if we know there's no one around.
1596: */
1.83 espie 1597: if (nJobs == 0) {
1.66 espie 1598: return;
1.2 deraadt 1599: }
1.6 millert 1600:
1.76 espie 1601: while ((pid = waitpid((pid_t) -1, &status, WNOHANG|WUNTRACED)) > 0) {
1.93 espie 1602: handle_all_signals();
1.84 espie 1603: debug_printf("Process %ld exited or stopped.\n", (long)pid);
1.1 deraadt 1604:
1.95 espie 1605: jnode = Lst_Find(&runningJobs, JobCmpPid, &pid);
1.1 deraadt 1606:
1.18 espie 1607: if (jnode == NULL) {
1.68 espie 1608: if (WIFSIGNALED(status) &&
1.66 espie 1609: (WTERMSIG(status) == SIGCONT)) {
1610: jnode = Lst_Find(&stoppedJobs, JobCmpPid, &pid);
1611: if (jnode == NULL) {
1612: Error("Resumed child (%ld) not in table", (long)pid);
1613: continue;
1614: }
1615: job = (Job *)Lst_Datum(jnode);
1616: Lst_Remove(&stoppedJobs, jnode);
1617: } else {
1618: Error("Child (%ld) not in table?", (long)pid);
1619: continue;
1620: }
1621: } else {
1622: job = (Job *)Lst_Datum(jnode);
1.95 espie 1623: Lst_Remove(&runningJobs, jnode);
1.67 espie 1624: nJobs--;
1.84 espie 1625: if (jobFull)
1626: debug_printf("Job queue is no longer full.\n");
1.66 espie 1627: jobFull = false;
1.1 deraadt 1628: }
1.66 espie 1629:
1630: JobFinish(job, &status);
1.1 deraadt 1631: }
1632: }
1633:
1634: /*-
1635: *-----------------------------------------------------------------------
1636: * Job_CatchOutput --
1637: * Catch the output from our children, if we're using
1638: * pipes do so. Otherwise just block time until we get a
1.6 millert 1639: * signal (most likely a SIGCHLD) since there's no point in
1.1 deraadt 1640: * just spinning when there's nothing to do and the reaping
1.6 millert 1641: * of a child can wait for a while.
1.1 deraadt 1642: *
1643: * Side Effects:
1644: * Output is read from pipes if we're piping.
1645: * -----------------------------------------------------------------------
1646: */
1647: void
1.56 espie 1648: Job_CatchOutput(void)
1.1 deraadt 1649: {
1.66 espie 1650: int nfds;
1651: struct timeval timeout;
1652: LstNode ln;
1653: Job *job;
1654:
1.76 espie 1655: int count = howmany(outputsn+1, NFDBITS) * sizeof(fd_mask);
1656: fd_set *readfdsp = malloc(count);
1.91 espie 1657:
1.77 espie 1658: (void)fflush(stdout);
1.76 espie 1659: if (readfdsp == NULL)
1660: return;
1661:
1662: memcpy(readfdsp, outputsp, count);
1663: timeout.tv_sec = SEL_SEC;
1664: timeout.tv_usec = SEL_USEC;
1.66 espie 1665:
1.91 espie 1666: nfds = select(outputsn+1, readfdsp, NULL, NULL, &timeout);
1.93 espie 1667: handle_all_signals();
1.91 espie 1668: if (nfds > 0) {
1.95 espie 1669: for (ln = Lst_First(&runningJobs); nfds && ln != NULL;
1.76 espie 1670: ln = Lst_Adv(ln)) {
1671: job = (Job *)Lst_Datum(ln);
1672: if (FD_ISSET(job->inPipe, readfdsp)) {
1673: JobDoOutput(job, false);
1674: nfds--;
1.66 espie 1675: }
1.1 deraadt 1676: }
1677: }
1.76 espie 1678: free(readfdsp);
1.1 deraadt 1679: }
1680:
1681: /*-
1682: *-----------------------------------------------------------------------
1683: * Job_Make --
1684: * Start the creation of a target. Basically a front-end for
1685: * JobStart used by the Make module.
1686: *
1687: * Side Effects:
1688: * Another job is started.
1689: *-----------------------------------------------------------------------
1690: */
1691: void
1.56 espie 1692: Job_Make(GNode *gn)
1.1 deraadt 1693: {
1.86 espie 1694: (void)JobStart(gn, 0);
1.1 deraadt 1695: }
1696:
1.93 espie 1697: static void
1698: setup_signal(int sig)
1699: {
1700: if (signal(sig, SIG_IGN) != SIG_IGN) {
1701: (void)signal(sig, SigHandler);
1702: }
1703: }
1704:
1.94 espie 1705: static void
1706: setup_all_signals()
1707: {
1708: /*
1709: * Catch the four signals that POSIX specifies if they aren't ignored.
1710: * handle_signal will take care of calling JobInterrupt if appropriate.
1711: */
1712: setup_signal(SIGINT);
1713: setup_signal(SIGHUP);
1714: setup_signal(SIGQUIT);
1715: setup_signal(SIGTERM);
1716: /*
1717: * There are additional signals that need to be caught and passed if
1718: * either the export system wants to be told directly of signals or if
1719: * we're giving each job its own process group (since then it won't get
1720: * signals from the terminal driver as we own the terminal)
1721: */
1722: #if defined(USE_PGRP)
1723: setup_signal(SIGTSTP);
1724: setup_signal(SIGTTOU);
1725: setup_signal(SIGTTIN);
1726: setup_signal(SIGWINCH);
1727: #endif
1728: }
1729:
1.1 deraadt 1730: /*-
1731: *-----------------------------------------------------------------------
1732: * Job_Init --
1733: * Initialize the process module
1734: *
1735: * Side Effects:
1736: * lists and counters are initialized
1737: *-----------------------------------------------------------------------
1738: */
1739: void
1.83 espie 1740: Job_Init(int maxproc)
1.1 deraadt 1741: {
1.95 espie 1742: Static_Lst_Init(&runningJobs);
1.66 espie 1743: Static_Lst_Init(&stoppedJobs);
1.96 espie 1744: Static_Lst_Init(&queuedJobs);
1.66 espie 1745: maxJobs = maxproc;
1.79 espie 1746: nJobs = 0;
1.66 espie 1747: jobFull = false;
1748:
1749: aborting = 0;
1750: errors = 0;
1.40 espie 1751:
1.66 espie 1752: lastNode = NULL;
1.1 deraadt 1753:
1.66 espie 1754: if (maxJobs == 1) {
1755: /*
1756: * If only one job can run at a time, there's no need for a
1757: * banner, no is there?
1758: */
1759: targFmt = "";
1760: } else {
1761: targFmt = TARG_FMT;
1762: }
1.6 millert 1763:
1.78 espie 1764: if ((begin_node->type & OP_DUMMY) == 0) {
1.86 espie 1765: JobStart(begin_node, JOB_SPECIAL);
1.66 espie 1766: while (nJobs) {
1767: Job_CatchOutput();
1.76 espie 1768: Job_CatchChildren();
1.66 espie 1769: }
1.1 deraadt 1770: }
1771: }
1772:
1773: /*-
1774: *-----------------------------------------------------------------------
1775: * Job_Full --
1776: * See if the job table is full. It is considered full if it is OR
1777: * if we are in the process of aborting OR if we have
1778: * reached/exceeded our local quota. This prevents any more jobs
1779: * from starting up.
1780: *
1781: * Results:
1.41 espie 1782: * true if the job table is full, false otherwise
1.1 deraadt 1783: *-----------------------------------------------------------------------
1784: */
1.41 espie 1785: bool
1.56 espie 1786: Job_Full(void)
1.1 deraadt 1787: {
1.66 espie 1788: return aborting || jobFull;
1.1 deraadt 1789: }
1790:
1791: /*-
1792: *-----------------------------------------------------------------------
1793: * Job_Empty --
1.40 espie 1794: * See if the job table is empty. Because the local concurrency may
1.1 deraadt 1795: * be set to 0, it is possible for the job table to become empty,
1796: * while the list of stoppedJobs remains non-empty. In such a case,
1797: * we want to restart as many jobs as we can.
1798: *
1799: * Results:
1.41 espie 1800: * true if it is. false if it ain't.
1.1 deraadt 1801: * -----------------------------------------------------------------------
1802: */
1.41 espie 1803: bool
1.56 espie 1804: Job_Empty(void)
1.1 deraadt 1805: {
1.66 espie 1806: if (nJobs == 0) {
1807: if (!Lst_IsEmpty(&stoppedJobs) && !aborting) {
1808: /*
1809: * The job table is obviously not full if it has no
1810: * jobs in it...Try and restart the stopped jobs.
1811: */
1812: jobFull = false;
1813: JobRestartJobs();
1814: return false;
1815: } else {
1816: return true;
1817: }
1.1 deraadt 1818: } else {
1.66 espie 1819: return false;
1.1 deraadt 1820: }
1821: }
1822:
1823: /*-
1824: *-----------------------------------------------------------------------
1825: * JobInterrupt --
1826: * Handle the receipt of an interrupt.
1827: *
1828: * Side Effects:
1829: * All children are killed. Another job will be started if the
1830: * .INTERRUPT target was given.
1831: *-----------------------------------------------------------------------
1832: */
1833: static void
1.56 espie 1834: JobInterrupt(int runINTERRUPT, /* Non-zero if commands for the .INTERRUPT
1.1 deraadt 1835: * target should be executed */
1.66 espie 1836: int signo) /* signal received */
1.1 deraadt 1837: {
1.66 espie 1838: LstNode ln; /* element in job table */
1.78 espie 1839: Job *job; /* job descriptor in that element */
1.66 espie 1840:
1841: aborting = ABORT_INTERRUPT;
1842:
1.95 espie 1843: for (ln = Lst_First(&runningJobs); ln != NULL; ln = Lst_Adv(ln)) {
1.66 espie 1844: job = (Job *)Lst_Datum(ln);
1845:
1846: if (!Targ_Precious(job->node)) {
1847: const char *file = job->node->path == NULL ?
1848: job->node->name : job->node->path;
1849: if (!noExecute && eunlink(file) != -1) {
1850: Error("*** %s removed", file);
1851: }
1852: }
1853: if (job->pid) {
1.84 espie 1854: debug_printf("JobInterrupt passing signal to "
1855: "child %ld.\n", (long)job->pid);
1.66 espie 1856: KILL(job->pid, signo);
1857: }
1.2 deraadt 1858: }
1.1 deraadt 1859:
1.66 espie 1860: if (runINTERRUPT && !touchFlag) {
1.78 espie 1861: if ((interrupt_node->type & OP_DUMMY) == 0) {
1.66 espie 1862: ignoreErrors = false;
1863:
1.86 espie 1864: JobStart(interrupt_node, JOB_IGNDOTS);
1.66 espie 1865: while (nJobs) {
1866: Job_CatchOutput();
1.76 espie 1867: Job_CatchChildren();
1.66 espie 1868: }
1869: }
1.1 deraadt 1870: }
1.66 espie 1871: exit(signo);
1.1 deraadt 1872: }
1873:
1874: /*
1875: *-----------------------------------------------------------------------
1.12 espie 1876: * Job_Finish --
1.1 deraadt 1877: * Do final processing such as the running of the commands
1.6 millert 1878: * attached to the .END target.
1.1 deraadt 1879: *
1880: * Results:
1881: * Number of errors reported.
1.40 espie 1882: *
1.1 deraadt 1883: *-----------------------------------------------------------------------
1884: */
1885: int
1.56 espie 1886: Job_Finish(void)
1.1 deraadt 1887: {
1.78 espie 1888: if (end_node != NULL && !Lst_IsEmpty(&end_node->commands)) {
1.66 espie 1889: if (errors) {
1890: Error("Errors reported so .END ignored");
1891: } else {
1.86 espie 1892: JobStart(end_node, JOB_SPECIAL | JOB_IGNDOTS);
1.1 deraadt 1893:
1.66 espie 1894: while (nJobs) {
1895: Job_CatchOutput();
1.76 espie 1896: Job_CatchChildren();
1.66 espie 1897: }
1898: }
1.1 deraadt 1899: }
1.66 espie 1900: return errors;
1.1 deraadt 1901: }
1902:
1.41 espie 1903: #ifdef CLEANUP
1.12 espie 1904: void
1.56 espie 1905: Job_End(void)
1.12 espie 1906: {
1.41 espie 1907: }
1.13 espie 1908: #endif
1.40 espie 1909:
1.1 deraadt 1910: /*-
1911: *-----------------------------------------------------------------------
1912: * Job_Wait --
1913: * Waits for all running jobs to finish and returns. Sets 'aborting'
1914: * to ABORT_WAIT to prevent other jobs from starting.
1915: *
1916: * Side Effects:
1917: * Currently running jobs finish.
1918: *
1919: *-----------------------------------------------------------------------
1920: */
1921: void
1.56 espie 1922: Job_Wait(void)
1.1 deraadt 1923: {
1.66 espie 1924: aborting = ABORT_WAIT;
1925: while (nJobs != 0) {
1926: Job_CatchOutput();
1.76 espie 1927: Job_CatchChildren();
1.66 espie 1928: }
1929: aborting = 0;
1.1 deraadt 1930: }
1931:
1932: /*-
1933: *-----------------------------------------------------------------------
1934: * Job_AbortAll --
1935: * Abort all currently running jobs without handling output or anything.
1936: * This function is to be called only in the event of a major
1937: * error. Most definitely NOT to be called from JobInterrupt.
1938: *
1939: * Side Effects:
1940: * All children are killed, not just the firstborn
1941: *-----------------------------------------------------------------------
1942: */
1943: void
1.56 espie 1944: Job_AbortAll(void)
1.1 deraadt 1945: {
1.66 espie 1946: LstNode ln; /* element in job table */
1947: Job *job; /* the job descriptor in that element */
1948: int foo;
1.6 millert 1949:
1.66 espie 1950: aborting = ABORT_ERROR;
1.6 millert 1951:
1.66 espie 1952: if (nJobs) {
1.95 espie 1953: for (ln = Lst_First(&runningJobs); ln != NULL;
1954: ln = Lst_Adv(ln)) {
1.66 espie 1955: job = (Job *)Lst_Datum(ln);
1956:
1957: /*
1958: * kill the child process with increasingly drastic
1959: * signals to make darn sure it's dead.
1960: */
1961: KILL(job->pid, SIGINT);
1962: KILL(job->pid, SIGKILL);
1963: }
1.1 deraadt 1964: }
1.6 millert 1965:
1.66 espie 1966: /*
1967: * Catch as many children as want to report in at first, then give up
1968: */
1969: while (waitpid(-1, &foo, WNOHANG) > 0)
1970: continue;
1.2 deraadt 1971: }
1.40 espie 1972:
1.2 deraadt 1973: /*-
1974: *-----------------------------------------------------------------------
1975: * JobRestartJobs --
1976: * Tries to restart stopped jobs if there are slots available.
1977: * Note that this tries to restart them regardless of pending errors.
1978: * It's not good to leave stopped jobs lying around!
1979: *
1980: * Side Effects:
1981: * Resumes(and possibly migrates) jobs.
1982: *-----------------------------------------------------------------------
1983: */
1984: static void
1.56 espie 1985: JobRestartJobs(void)
1.2 deraadt 1986: {
1.66 espie 1987: Job *job;
1.19 espie 1988:
1.66 espie 1989: while (!jobFull && (job = (Job *)Lst_DeQueue(&stoppedJobs)) != NULL) {
1.84 espie 1990: debug_printf("Job queue is not full. "
1991: "Restarting a stopped job.\n");
1.66 espie 1992: JobRestart(job);
1.2 deraadt 1993: }
1.1 deraadt 1994: }