Annotation of src/usr.bin/make/job.c, Revision 1.106
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
1.101 espie 145: struct job_pipe {
146: int fd;
147: char buffer[JOB_BUFSIZE];
148: size_t pos;
149: };
150:
1.50 espie 151: typedef struct Job_ {
1.51 mpech 152: pid_t pid; /* The child's process ID */
1.50 espie 153: GNode *node; /* The target the child is making */
154: short flags; /* Flags to control treatment of job */
1.83 espie 155: #define JOB_SPECIAL 0x004 /* Target is a special one. */
1.50 espie 156: #define JOB_RESTART 0x080 /* Job needs to be completely restarted */
1.79 espie 157: #define JOB_RESUME 0x100 /* Job needs to be resumed b/c it stopped,
158: * for some reason */
159: #define JOB_CONTINUING 0x200 /* We are in the process of resuming this job.
160: * Used to avoid infinite recursion between
161: * JobFinish and JobRestart */
1.101 espie 162: struct job_pipe in[2];
1.50 espie 163: } Job;
164:
1.78 espie 165:
1.40 espie 166: static int aborting = 0; /* why is the make aborting? */
167: #define ABORT_ERROR 1 /* Because of an error */
168: #define ABORT_INTERRUPT 2 /* Because it was interrupted */
169: #define ABORT_WAIT 3 /* Waiting for jobs to finish */
1.1 deraadt 170:
1.40 espie 171: static int maxJobs; /* The most children we can run at once */
1.83 espie 172: static int nJobs; /* The number of children currently running */
1.95 espie 173: static LIST runningJobs; /* The structures that describe them */
1.79 espie 174: static bool jobFull; /* Flag to tell when the job table is full. It
1.83 espie 175: * is set true when nJobs equals maxJobs */
1.40 espie 176: static fd_set *outputsp; /* Set of descriptors of pipes connected to
1.1 deraadt 177: * the output channels of children */
1.8 deraadt 178: static int outputsn;
1.48 espie 179: static GNode *lastNode; /* The node for which output was most recently
1.1 deraadt 180: * produced. */
181: /*
1.50 espie 182: * When JobStart attempts to run a job but isn't allowed to,
1.96 espie 183: * the job is placed on the queuedJobs queue to be run
1.6 millert 184: * when the next job finishes.
1.1 deraadt 185: */
1.96 espie 186: static LIST stoppedJobs;
187: static LIST queuedJobs;
1.101 espie 188: static LIST errorsList;
189: static int errors;
190: struct error_info {
191: int status;
192: char *name;
193: };
194:
1.1 deraadt 195:
196:
197: #if defined(USE_PGRP) && defined(SYSV)
1.40 espie 198: # define KILL(pid, sig) killpg(-(pid), (sig))
1.1 deraadt 199: #else
200: # if defined(USE_PGRP)
1.2 deraadt 201: # define KILL(pid, sig) killpg((pid), (sig))
1.1 deraadt 202: # else
1.2 deraadt 203: # define KILL(pid, sig) kill((pid), (sig))
1.1 deraadt 204: # endif
205: #endif
206:
1.6 millert 207: /*
1.2 deraadt 208: * Grmpf... There is no way to set bits of the wait structure
209: * anymore with the stupid W*() macros. I liked the union wait
210: * stuff much more. So, we devise our own macros... This is
211: * really ugly, use dramamine sparingly. You have been warned.
212: */
213: #define W_SETMASKED(st, val, fun) \
214: { \
215: int sh = (int) ~0; \
216: int mask = fun(sh); \
217: \
218: for (sh = 0; ((mask >> sh) & 1) == 0; sh++) \
219: continue; \
220: *(st) = (*(st) & ~mask) | ((val) << sh); \
221: }
222:
223: #define W_SETTERMSIG(st, val) W_SETMASKED(st, val, WTERMSIG)
224: #define W_SETEXITSTATUS(st, val) W_SETMASKED(st, val, WEXITSTATUS)
225:
226:
1.93 espie 227: static void pass_signal_to_job(void *, void *);
228: static void handle_all_signals(void);
229: static void handle_signal(int);
1.40 espie 230: static int JobCmpPid(void *, void *);
231: static void JobClose(Job *);
1.101 espie 232: static void JobFinish(Job *, int);
233: static void JobExec(Job *);
1.40 espie 234: static void JobRestart(Job *);
1.97 espie 235: static void JobStart(GNode *, int);
1.40 espie 236: static void JobInterrupt(int, int);
237: static void JobRestartJobs(void);
1.84 espie 238: static void debug_printf(const char *, ...);
1.98 espie 239: static Job *prepare_job(GNode *, int);
1.96 espie 240: static void start_queued_job(Job *);
1.105 espie 241: static void banner(Job *, FILE *);
1.101 espie 242: static void print_partial_buffer(struct job_pipe *, Job *, FILE *, size_t);
243: static void print_partial_buffer_and_shift(struct job_pipe *, Job *, FILE *,
244: size_t);
245: static bool print_complete_lines(struct job_pipe *, Job *, FILE *, size_t);
246: static void prepare_pipe(struct job_pipe *, int *);
247: static void handle_job_output(Job *, int, bool);
248: static void register_error(int, Job *);
1.93 espie 249:
1.101 espie 250: static void
251: register_error(int status, Job *job)
252: {
253: struct error_info *p;
1.1 deraadt 254:
1.101 espie 255: errors++;
256: p = emalloc(sizeof(struct error_info));
257: p->status = status;
258: p->name = job->node->name;
259: if (p)
260: Lst_AtEnd(&errorsList, p);
261: }
1.93 espie 262:
1.101 espie 263: void
264: print_errors()
1.88 espie 265: {
1.101 espie 266: LstNode ln;
267: struct error_info *p;
1.88 espie 268:
1.101 espie 269: for (ln = Lst_First(&errorsList); ln != NULL; ln = Lst_Adv(ln)) {
270: p = (struct error_info *)Lst_Datum(ln);
271: if (WIFEXITED(p->status)) {
272: Error("\tExit status %d in target %s",
273: WEXITSTATUS(p->status), p->name);
274: } else if (WIFSIGNALED(p->status)) {
275: Error("\tReceived signal %d in target s",
276: WTERMSIG(p->status), p->name);
277: } else {
278: Error("\tStatus %d in target %s", p->status, p->name);
279: }
280: }
1.88 espie 281: }
282:
1.57 espie 283: static void
1.105 espie 284: banner(Job *job, FILE *out)
1.57 espie 285: {
1.101 espie 286: if (job->node != lastNode) {
1.105 espie 287: if (DEBUG(JOBBANNER))
1.101 espie 288: (void)fprintf(out, "--- %s ---\n", job->node->name);
289: lastNode = job->node;
1.57 espie 290: }
291: }
292:
293: static void
1.93 espie 294: handle_all_signals()
1.57 espie 295: {
1.93 espie 296: if (got_signal)
297: got_signal = 0;
298: else
299: return;
300:
1.57 espie 301: if (got_SIGINT) {
302: got_SIGINT=0;
1.93 espie 303: handle_signal(SIGINT);
1.57 espie 304: }
305: if (got_SIGHUP) {
306: got_SIGHUP=0;
1.93 espie 307: handle_signal(SIGHUP);
1.57 espie 308: }
309: if (got_SIGQUIT) {
310: got_SIGQUIT=0;
1.93 espie 311: handle_signal(SIGQUIT);
1.57 espie 312: }
313: if (got_SIGTERM) {
314: got_SIGTERM=0;
1.93 espie 315: handle_signal(SIGTERM);
1.57 espie 316: }
317: if (got_SIGTSTP) {
318: got_SIGTSTP=0;
1.93 espie 319: handle_signal(SIGTSTP);
1.57 espie 320: }
321: if (got_SIGTTOU) {
322: got_SIGTTOU=0;
1.93 espie 323: handle_signal(SIGTTOU);
1.57 espie 324: }
325: if (got_SIGTTIN) {
326: got_SIGTTIN=0;
1.93 espie 327: handle_signal(SIGTTIN);
1.57 espie 328: }
329: if (got_SIGWINCH) {
330: got_SIGWINCH=0;
1.93 espie 331: handle_signal(SIGWINCH);
1.57 espie 332: }
333: }
334:
1.1 deraadt 335: /*-
336: *-----------------------------------------------------------------------
337: * JobCondPassSig --
1.50 espie 338: * Pass a signal to a job if USE_PGRP
1.1 deraadt 339: * is defined.
340: *
341: * Side Effects:
342: * None, except the job may bite it.
343: *-----------------------------------------------------------------------
344: */
1.27 espie 345: static void
1.93 espie 346: pass_signal_to_job(void *jobp, /* Job to biff */
1.56 espie 347: void *signop) /* Signal to send it */
1.1 deraadt 348: {
1.66 espie 349: Job *job = (Job *)jobp;
350: int signo = *(int *)signop;
351: if (DEBUG(JOB)) {
352: (void)fprintf(stdout,
1.93 espie 353: "pass_signal_to_job passing signal %d to child %ld.\n",
1.66 espie 354: signo, (long)job->pid);
355: (void)fflush(stdout);
356: }
357: KILL(job->pid, signo);
1.1 deraadt 358: }
359:
360: /*-
361: *-----------------------------------------------------------------------
1.94 espie 362: * handle_signal --
1.68 espie 363: * Pass a signal to all local jobs if USE_PGRP is defined,
1.50 espie 364: * then die ourselves.
1.1 deraadt 365: *
366: * Side Effects:
367: * We die by the same signal.
368: *-----------------------------------------------------------------------
369: */
370: static void
1.93 espie 371: handle_signal(int signo) /* The signal number we've received */
1.1 deraadt 372: {
1.66 espie 373: sigset_t nmask, omask;
374: struct sigaction act;
1.6 millert 375:
1.66 espie 376: if (DEBUG(JOB)) {
1.93 espie 377: (void)fprintf(stdout, "handle_signal(%d) called.\n", signo);
1.66 espie 378: (void)fflush(stdout);
379: }
1.95 espie 380: Lst_ForEach(&runningJobs, pass_signal_to_job, &signo);
1.66 espie 381:
382: /*
383: * Deal with proper cleanup based on the signal received. We only run
384: * the .INTERRUPT target if the signal was in fact an interrupt. The
385: * other three termination signals are more of a "get out *now*"
386: * command.
387: */
388: if (signo == SIGINT) {
389: JobInterrupt(true, signo);
390: } else if (signo == SIGHUP || signo == SIGTERM || signo == SIGQUIT) {
391: JobInterrupt(false, signo);
392: }
1.1 deraadt 393:
1.66 espie 394: /*
395: * Leave gracefully if SIGQUIT, rather than core dumping.
396: */
397: if (signo == SIGQUIT) {
398: Finish(0);
399: }
1.6 millert 400:
1.66 espie 401: /*
402: * Send ourselves the signal now we've given the message to everyone
403: * else. Note we block everything else possible while we're getting
404: * the signal. This ensures that all our jobs get continued when we
405: * wake up before we take any other signal.
406: */
407: sigemptyset(&nmask);
408: sigaddset(&nmask, signo);
409: sigprocmask(SIG_SETMASK, &nmask, &omask);
410: memset(&act, 0, sizeof act);
411: act.sa_handler = SIG_DFL;
412: sigemptyset(&act.sa_mask);
413: act.sa_flags = 0;
414: sigaction(signo, &act, NULL);
1.6 millert 415:
1.66 espie 416: if (DEBUG(JOB)) {
417: (void)fprintf(stdout,
1.93 espie 418: "handle_signal passing signal to self, mask = %x.\n",
1.66 espie 419: ~0 & ~(1 << (signo-1)));
420: (void)fflush(stdout);
421: }
422: (void)signal(signo, SIG_DFL);
1.1 deraadt 423:
1.66 espie 424: (void)KILL(getpid(), signo);
1.1 deraadt 425:
1.66 espie 426: signo = SIGCONT;
1.95 espie 427: Lst_ForEach(&runningJobs, pass_signal_to_job, &signo);
1.1 deraadt 428:
1.66 espie 429: (void)sigprocmask(SIG_SETMASK, &omask, NULL);
430: sigprocmask(SIG_SETMASK, &omask, NULL);
431: act.sa_handler = SigHandler;
432: sigaction(signo, &act, NULL);
1.1 deraadt 433: }
434:
435: /*-
436: *-----------------------------------------------------------------------
437: * JobCmpPid --
438: * Compare the pid of the job with the given pid and return 0 if they
439: * are equal. This function is called from Job_CatchChildren via
440: * Lst_Find to find the job descriptor of the finished job.
441: *
442: * Results:
443: * 0 if the pid's match
444: *-----------------------------------------------------------------------
445: */
446: static int
1.56 espie 447: JobCmpPid(void *job, /* job to examine */
448: void *pid) /* process id desired */
1.1 deraadt 449: {
1.66 espie 450: return *(pid_t *)pid - ((Job *)job)->pid;
1.1 deraadt 451: }
452:
1.69 espie 453: static void
1.84 espie 454: debug_printf(const char *fmt, ...)
455: {
456: if (DEBUG(JOB)) {
457: va_list va;
458:
459: va_start(va, fmt);
460: (void)vfprintf(stdout, fmt, va);
461: fflush(stdout);
462: va_end(va);
463: }
464: }
465:
1.1 deraadt 466: /*-
467: *-----------------------------------------------------------------------
1.2 deraadt 468: * JobClose --
469: * Called to close both input and output pipes when a job is finished.
470: *
471: * Side Effects:
472: * The file descriptors associated with the job are closed.
473: *-----------------------------------------------------------------------
474: */
475: static void
1.56 espie 476: JobClose(Job *job)
1.2 deraadt 477: {
1.101 espie 478: int i;
479:
480: for (i = 0; i < 2; i++) {
481: FD_CLR(job->in[i].fd, outputsp);
482: handle_job_output(job, i, true);
483: (void)close(job->in[i].fd);
1.66 espie 484: }
1.1 deraadt 485: }
486:
487: /*-
488: *-----------------------------------------------------------------------
489: * JobFinish --
490: * Do final processing for the given job including updating
1.106 ! espie 491: * parents and starting new jobs as available/necessary.
1.1 deraadt 492: *
493: * Side Effects:
494: * Some nodes may be put on the toBeMade queue.
1.78 espie 495: * Final commands for the job are placed on end_node.
1.1 deraadt 496: *
1.6 millert 497: * If we got an error and are aborting (aborting == ABORT_ERROR) and
1.1 deraadt 498: * the job list is now empty, we are done for the day.
1.101 espie 499: * If we recognized an error we set the aborting flag
1.1 deraadt 500: * to ABORT_ERROR so no more jobs will be started.
501: *-----------------------------------------------------------------------
502: */
503: /*ARGSUSED*/
504: static void
1.101 espie 505: JobFinish(Job *job, int status)
1.2 deraadt 506: {
1.78 espie 507: bool done;
1.2 deraadt 508:
1.101 espie 509: if ((WIFEXITED(status) &&
1.106 ! espie 510: WEXITSTATUS(status) != 0 && !(job->node->type & OP_IGNORE)) ||
1.101 espie 511: (WIFSIGNALED(status) && WTERMSIG(status) != SIGCONT)) {
1.66 espie 512: /*
513: * If it exited non-zero and either we're doing things our
514: * way or we're not ignoring errors, the job is finished.
515: * Similarly, if the shell died because of a signal
516: * the job is also finished. In these
517: * cases, finish out the job's output before printing the exit
518: * status...
519: */
520: JobClose(job);
521: done = true;
1.101 espie 522: } else if (WIFEXITED(status)) {
1.66 espie 523: /*
524: * Deal with ignored errors in -B mode. We need to print a
525: * message telling of the ignored error as well as setting
526: * status.w_status to 0 so the next command gets run. To do
527: * this, we set done to be true if in -B mode and the job
528: * exited non-zero.
529: */
1.101 espie 530: done = WEXITSTATUS(status) != 0;
1.66 espie 531: /*
532: * Old comment said: "Note we don't want to close down any of
533: * the streams until we know we're at the end." But we do.
534: * Otherwise when are we going to print the rest of the stuff?
535: */
536: JobClose(job);
537: } else {
538: /*
539: * No need to close things down or anything.
540: */
541: done = false;
1.1 deraadt 542: }
1.6 millert 543:
1.66 espie 544: if (done ||
1.101 espie 545: WIFSTOPPED(status) ||
546: (WIFSIGNALED(status) && WTERMSIG(status) == SIGCONT) ||
1.66 espie 547: DEBUG(JOB)) {
1.101 espie 548: if (WIFEXITED(status)) {
1.84 espie 549: debug_printf("Process %ld exited.\n", (long)job->pid);
1.101 espie 550: if (WEXITSTATUS(status) != 0) {
1.105 espie 551: banner(job, stdout);
1.101 espie 552: (void)fprintf(stdout, "*** Error code %d%s\n",
553: WEXITSTATUS(status),
1.106 ! espie 554: (job->node->type & OP_IGNORE) ?
! 555: "(ignored)" : "");
1.66 espie 556:
1.106 ! espie 557: if (job->node->type & OP_IGNORE) {
1.101 espie 558: status = 0;
1.66 espie 559: }
560: } else if (DEBUG(JOB)) {
1.105 espie 561: banner(job, stdout);
1.101 espie 562: (void)fprintf(stdout,
1.66 espie 563: "*** Completed successfully\n");
564: }
1.101 espie 565: } else if (WIFSTOPPED(status)) {
1.84 espie 566: debug_printf("Process %ld stopped.\n", (long)job->pid);
1.105 espie 567: banner(job, stdout);
1.101 espie 568: (void)fprintf(stdout, "*** Stopped -- signal %d\n",
569: WSTOPSIG(status));
1.66 espie 570: job->flags |= JOB_RESUME;
571: Lst_AtEnd(&stoppedJobs, job);
1.101 espie 572: (void)fflush(stdout);
1.66 espie 573: return;
1.101 espie 574: } else if (WTERMSIG(status) == SIGCONT) {
1.66 espie 575: /*
576: * If the beastie has continued, shift the Job from the
577: * stopped list to the running one (or re-stop it if
578: * concurrency is exceeded) and go and get another
579: * child.
580: */
581: if (job->flags & (JOB_RESUME|JOB_RESTART)) {
1.105 espie 582: banner(job, stdout);
1.101 espie 583: (void)fprintf(stdout, "*** Continued\n");
1.66 espie 584: }
585: if (!(job->flags & JOB_CONTINUING)) {
1.84 espie 586: debug_printf(
587: "Warning: "
588: "process %ld was not continuing.\n",
589: (long)job->pid);
1.66 espie 590: #if 0
591: /*
592: * We don't really want to restart a job from
593: * scratch just because it continued,
594: * especially not without killing the
595: * continuing process! That's why this is
596: * ifdef'ed out. FD - 9/17/90
597: */
598: JobRestart(job);
1.2 deraadt 599: #endif
1.66 espie 600: }
601: job->flags &= ~JOB_CONTINUING;
1.95 espie 602: Lst_AtEnd(&runningJobs, job);
1.67 espie 603: nJobs++;
1.84 espie 604: debug_printf("Process %ld is continuing locally.\n",
605: (long)job->pid);
1.66 espie 606: if (nJobs == maxJobs) {
607: jobFull = true;
1.84 espie 608: debug_printf("Job queue is full.\n");
1.66 espie 609: }
1.101 espie 610: (void)fflush(stdout);
1.66 espie 611: return;
612: } else {
1.105 espie 613: banner(job, stdout);
1.101 espie 614: (void)fprintf(stdout, "*** Signal %d\n",
615: WTERMSIG(status));
1.40 espie 616: }
1.66 espie 617:
1.101 espie 618: (void)fflush(stdout);
1.1 deraadt 619: }
620:
1.85 espie 621: done = true;
1.1 deraadt 622:
1.66 espie 623: if (done &&
624: aborting != ABORT_ERROR &&
625: aborting != ABORT_INTERRUPT &&
1.101 espie 626: status == 0) {
1.66 espie 627: /* As long as we aren't aborting and the job didn't return a
628: * non-zero status that we shouldn't ignore, we call
1.101 espie 629: * Make_Update to update the parents. */
1.66 espie 630: job->node->made = MADE;
631: Make_Update(job->node);
632: free(job);
1.101 espie 633: } else if (status != 0) {
634: register_error(status, job);
1.66 espie 635: free(job);
636: }
1.1 deraadt 637:
1.66 espie 638: JobRestartJobs();
1.1 deraadt 639:
640: /*
1.66 espie 641: * Set aborting if any error.
1.1 deraadt 642: */
1.101 espie 643: if (errors && !keepgoing &&
644: aborting != ABORT_INTERRUPT) {
1.66 espie 645: /*
646: * If we found any errors in this batch of children and the -k
647: * flag wasn't given, we set the aborting flag so no more jobs
648: * get started.
649: */
650: aborting = ABORT_ERROR;
651: }
1.6 millert 652:
1.66 espie 653: if (aborting == ABORT_ERROR && Job_Empty()) {
654: /*
655: * If we are aborting and the job table is now empty, we finish.
656: */
657: Finish(errors);
658: }
1.1 deraadt 659: }
660:
1.101 espie 661: static void
662: prepare_pipe(struct job_pipe *p, int *fd)
663: {
664: p->pos = 0;
665: (void)fcntl(fd[0], F_SETFD, FD_CLOEXEC);
666: p->fd = fd[0];
667: close(fd[1]);
668:
669: if (outputsp == NULL || p->fd > outputsn) {
670: int fdn, ofdn;
671: fd_set *tmp;
672:
673: fdn = howmany(p->fd+1, NFDBITS);
674: ofdn = outputsn ? howmany(outputsn+1, NFDBITS) : 0;
675:
676: if (fdn != ofdn) {
677: tmp = recalloc(outputsp, fdn, sizeof(fd_mask));
678: if (tmp == NULL)
679: return;
680: outputsp = tmp;
681: }
682: outputsn = p->fd;
683: }
684: fcntl(p->fd, F_SETFL, O_NONBLOCK);
685: FD_SET(p->fd, outputsp);
686: }
687:
1.1 deraadt 688: /*-
689: *-----------------------------------------------------------------------
690: * JobExec --
691: * Execute the shell for the given job. Called from JobStart and
692: * JobRestart.
693: *
694: * Side Effects:
695: * A shell is executed, outputs is altered and the Job structure added
696: * to the job table.
697: *-----------------------------------------------------------------------
698: */
699: static void
1.101 espie 700: JobExec(Job *job)
1.1 deraadt 701: {
1.66 espie 702: pid_t cpid; /* ID of new child */
1.101 espie 703: int fds[4];
704: int *fdout = fds;
705: int *fderr = fds+2;
706: int result;
707: int i;
1.6 millert 708:
1.66 espie 709: if (DEBUG(JOB)) {
710: (void)fprintf(stdout, "Running %s\n", job->node->name);
711: (void)fflush(stdout);
1.1 deraadt 712: }
1.6 millert 713:
1.66 espie 714: /*
715: * Some jobs produce no output and it's disconcerting to have
716: * no feedback of their running (since they produce no output, the
717: * banner with their name in it never appears). This is an attempt to
718: * provide that feedback, even if nothing follows it.
719: */
1.105 espie 720: banner(job, stdout);
1.101 espie 721:
722: setup_engine();
723:
724: /* Create the pipe by which we'll get the shell's output.
725: */
726: if (pipe(fdout) == -1)
727: Punt("Cannot create pipe: %s", strerror(errno));
1.1 deraadt 728:
1.101 espie 729: if (pipe(fderr) == -1)
730: Punt("Cannot create pipe: %s", strerror(errno));
1.94 espie 731:
1.66 espie 732: if ((cpid = fork()) == -1) {
733: Punt("Cannot fork");
734: } else if (cpid == 0) {
1.104 espie 735: supervise_jobs = false;
1.101 espie 736: /* standard pipe code to route stdout and stderr */
737: close(fdout[0]);
738: if (dup2(fdout[1], 1) == -1)
739: Punt("Cannot dup2(outPipe): %s", strerror(errno));
740: if (fdout[1] != 1)
741: close(fdout[1]);
742: close(fderr[0]);
743: if (dup2(fderr[1], 2) == -1)
744: Punt("Cannot dup2(errPipe): %s", strerror(errno));
745: if (fderr[1] != 2)
746: close(fderr[1]);
1.1 deraadt 747:
748: #ifdef USE_PGRP
1.66 espie 749: /*
750: * We want to switch the child into a different process family
751: * so we can kill it and all its descendants in one fell swoop,
752: * by killing its process family, but not commit suicide.
753: */
1.2 deraadt 754: # if defined(SYSV)
1.66 espie 755: (void)setsid();
1.2 deraadt 756: # else
1.66 espie 757: (void)setpgid(0, getpid());
1.2 deraadt 758: # endif
759: #endif /* USE_PGRP */
1.1 deraadt 760:
1.101 espie 761: /* most cases won't return, but will exit directly */
762: result = run_gnode(job->node, 1);
763: switch(result) {
764: case MADE:
765: exit(0);
766: case ERROR:
767: exit(1);
768: default:
769: fprintf(stderr,
770: "Could not run gnode, returned %d\n", result);
771: exit(1);
772: }
1.66 espie 773: } else {
1.104 espie 774: supervise_jobs = true;
1.66 espie 775: job->pid = cpid;
776:
1.101 espie 777: /* we set the current position in the buffers to the beginning
1.87 espie 778: * and mark another stream to watch in the outputs mask
779: */
1.101 espie 780: for (i = 0; i < 2; i++)
781: prepare_pipe(&job->in[i], fds+2*i);
1.1 deraadt 782: }
783:
1.48 espie 784: /*
1.66 espie 785: * Now the job is actually running, add it to the table.
1.48 espie 786: */
1.67 espie 787: nJobs++;
1.95 espie 788: Lst_AtEnd(&runningJobs, job);
1.66 espie 789: if (nJobs == maxJobs) {
790: jobFull = true;
1.1 deraadt 791: }
792: }
793:
1.96 espie 794: static void
795: start_queued_job(Job *job)
796: {
797: if (DEBUG(JOB)) {
798: (void)fprintf(stdout, "Restarting %s...",
799: job->node->name);
800: (void)fflush(stdout);
801: }
802: if (nJobs >= maxJobs && !(job->flags & JOB_SPECIAL)) {
803: /*
804: * Can't be exported and not allowed to run locally --
805: * put it back on the hold queue and mark the table
806: * full
807: */
808: debug_printf("holding\n");
809: Lst_AtFront(&stoppedJobs, job);
810: jobFull = true;
811: debug_printf("Job queue is full.\n");
812: return;
813: } else {
814: /*
815: * Job may be run locally.
816: */
817: debug_printf("running locally\n");
818: }
1.101 espie 819: JobExec(job);
1.96 espie 820: }
821:
1.1 deraadt 822: /*-
823: *-----------------------------------------------------------------------
824: * JobRestart --
1.6 millert 825: * Restart a job that stopped for some reason.
1.1 deraadt 826: *
827: * Side Effects:
828: * jobFull will be set if the job couldn't be run.
829: *-----------------------------------------------------------------------
830: */
831: static void
1.56 espie 832: JobRestart(Job *job)
1.1 deraadt 833: {
1.66 espie 834: if (job->flags & JOB_RESTART) {
1.96 espie 835: start_queued_job(job);
1.66 espie 836: } else {
1.1 deraadt 837: /*
1.66 espie 838: * The job has stopped and needs to be restarted. Why it
839: * stopped, we don't know...
1.1 deraadt 840: */
1.84 espie 841: debug_printf("Resuming %s...", job->node->name);
1.83 espie 842: if ((nJobs < maxJobs || ((job->flags & JOB_SPECIAL) &&
843: maxJobs == 0)) && nJobs != maxJobs) {
1.66 espie 844: /*
1.79 espie 845: * If we haven't reached the concurrency limit already
1.83 espie 846: * (or maxJobs is 0), it's ok to resume the job.
1.66 espie 847: */
848: bool error;
1.101 espie 849: int status = 0;
1.66 espie 850:
851: error = KILL(job->pid, SIGCONT) != 0;
852:
853: if (!error) {
854: /*
855: * Make sure the user knows we've continued the
856: * beast and actually put the thing in the job
857: * table.
858: */
859: job->flags |= JOB_CONTINUING;
860: W_SETTERMSIG(&status, SIGCONT);
1.101 espie 861: JobFinish(job, status);
1.66 espie 862:
863: job->flags &= ~(JOB_RESUME|JOB_CONTINUING);
1.84 espie 864: debug_printf("done\n");
1.66 espie 865: } else {
866: Error("couldn't resume %s: %s",
867: job->node->name, strerror(errno));
868: W_SETEXITSTATUS(&status, 1);
1.101 espie 869: JobFinish(job, status);
1.66 espie 870: }
871: } else {
872: /*
873: * Job cannot be restarted. Mark the table as full and
874: * place the job back on the list of stopped jobs.
875: */
1.84 espie 876: debug_printf("table full\n");
1.66 espie 877: Lst_AtFront(&stoppedJobs, job);
878: jobFull = true;
1.84 espie 879: debug_printf("Job queue is full.\n");
1.1 deraadt 880: }
881: }
882: }
883:
1.98 espie 884: static Job *
885: prepare_job(GNode *gn, int flags)
1.78 espie 886: {
887: Job *job; /* new job descriptor */
888: bool cmdsOK; /* true if the nodes commands were all right */
889: bool noExec; /* Set true if we decide not to run the job */
1.66 espie 890:
1.86 espie 891: job = emalloc(sizeof(Job));
892: if (job == NULL) {
893: Punt("JobStart out of memory");
1.1 deraadt 894: }
895:
1.66 espie 896: job->node = gn;
1.1 deraadt 897:
898: /*
1.66 espie 899: * Set the initial value of the flags for this job based on the global
900: * ones and the node's attributes... Any flags supplied by the caller
901: * are also added to the field.
1.1 deraadt 902: */
1.87 espie 903: job->flags = flags;
1.6 millert 904:
1.1 deraadt 905: /*
1.66 espie 906: * Check the commands now so any attributes from .DEFAULT have a chance
907: * to migrate to the node
1.1 deraadt 908: */
1.87 espie 909: cmdsOK = Job_CheckCommands(gn, Error);
1.1 deraadt 910:
1.66 espie 911: if ((gn->type & OP_MAKE) || (!noExecute && !touchFlag)) {
912: /*
913: * We're serious here, but if the commands were bogus, we're
914: * also dead...
915: */
916: if (!cmdsOK) {
917: DieHorribly();
918: }
1.6 millert 919:
1.101 espie 920: if (Lst_IsEmpty(&gn->commands))
921: noExec = true;
922: else
923: noExec = false;
1.66 espie 924:
1.101 espie 925: } else if (noExecute) {
926: if (!cmdsOK || Lst_IsEmpty(&gn->commands))
1.85 espie 927: noExec = true;
1.101 espie 928: else
929: noExec = false;
1.66 espie 930: } else {
931: /*
932: * Just touch the target and note that no shell should be
1.101 espie 933: * executed. Check
1.66 espie 934: * the commands, too, but don't die if they're no good -- it
935: * does no harm to keep working up the graph.
1.30 espie 936: */
1.106 ! espie 937: Job_Touch(gn);
1.41 espie 938: noExec = true;
1.1 deraadt 939: }
1.66 espie 940:
1.1 deraadt 941: /*
1.66 espie 942: * If we're not supposed to execute a shell, don't.
1.1 deraadt 943: */
1.66 espie 944: if (noExec) {
945: /*
946: * We only want to work our way up the graph if we aren't here
947: * because the commands for the job were no good.
948: */
949: if (cmdsOK) {
950: if (aborting == 0) {
1.102 espie 951: job->node->made = MADE;
1.66 espie 952: Make_Update(job->node);
953: }
954: }
1.99 espie 955: free(job);
956: return NULL;
1.1 deraadt 957: } else {
1.98 espie 958: return job;
1.1 deraadt 959: }
1.98 espie 960: }
1.1 deraadt 961:
1.98 espie 962: /*-
963: *-----------------------------------------------------------------------
964: * JobStart --
965: * Start a target-creation process going for the target described
966: * by the graph node gn.
967: *
968: * Side Effects:
969: * A new Job node is created and added to the list of running
970: * jobs. PMake is forked and a child shell created.
971: *-----------------------------------------------------------------------
972: */
973: static void
974: JobStart(GNode *gn, /* target to create */
975: int flags) /* flags for the job to override normal ones.
976: * e.g. JOB_SPECIAL */
977: {
978: Job *job;
979: job = prepare_job(gn, flags);
980: if (!job)
981: return;
1.83 espie 982: if (nJobs >= maxJobs && !(job->flags & JOB_SPECIAL) &&
983: maxJobs != 0) {
1.79 espie 984: /*
985: * The job can only be run locally, but we've hit the limit of
986: * local concurrency, so put the job on hold until some other
987: * job finishes. Note that the special jobs (.BEGIN, .INTERRUPT
988: * and .END) may be run locally even when the local limit has
1.83 espie 989: * been reached (e.g. when maxJobs == 0), though they will be
1.79 espie 990: * exported if at all possible. In addition, any target marked
1.83 espie 991: * with .NOEXPORT will be run locally if maxJobs is 0.
1.66 espie 992: */
993: jobFull = true;
1.6 millert 994:
1.84 espie 995: debug_printf("Can only run job locally.\n");
1.66 espie 996: job->flags |= JOB_RESTART;
997: Lst_AtEnd(&stoppedJobs, job);
1.79 espie 998: } else {
1.83 espie 999: if (nJobs >= maxJobs) {
1.79 espie 1000: /*
1001: * If we're running this job locally as a special case
1002: * (see above), at least say the table is full.
1003: */
1004: jobFull = true;
1.84 espie 1005: debug_printf("Local job queue is full.\n");
1.79 espie 1006: }
1.101 espie 1007: JobExec(job);
1.1 deraadt 1008: }
1009: }
1010:
1.101 espie 1011: /* Helper functions for JobDoOutput */
1012:
1013:
1.105 espie 1014: /* output debugging banner and print characters from 0 to endpos */
1.101 espie 1015: static void
1016: print_partial_buffer(struct job_pipe *p, Job *job, FILE *out, size_t endPos)
1.2 deraadt 1017: {
1.101 espie 1018: size_t i;
1.2 deraadt 1019:
1.105 espie 1020: banner(job, out);
1.101 espie 1021: for (i = 0; i < endPos; i++)
1022: putc(p->buffer[i], out);
1023: }
1024:
1025: /* print partial buffer and shift remaining contents */
1026: static void
1027: print_partial_buffer_and_shift(struct job_pipe *p, Job *job, FILE *out,
1028: size_t endPos)
1029: {
1030: size_t i;
1031:
1032: print_partial_buffer(p, job, out, endPos);
1033:
1034: for (i = endPos; i < p->pos; i++)
1035: p->buffer[i-endPos] = p->buffer[i];
1036: p->pos -= endPos;
1037: }
1038:
1039: /* print complete lines, looking back to the limit position
1040: * (stuff before limit was already scanned).
1041: * returns true if something was printed.
1042: */
1043: static bool
1044: print_complete_lines(struct job_pipe *p, Job *job, FILE *out, size_t limit)
1045: {
1046: size_t i;
1047:
1048: for (i = p->pos; i > limit; i--) {
1049: if (p->buffer[i-1] == '\n') {
1050: print_partial_buffer_and_shift(p, job, out, i);
1051: return true;
1.2 deraadt 1052: }
1053: }
1.101 espie 1054: return false;
1.2 deraadt 1055: }
1.1 deraadt 1056: /*-
1057: *-----------------------------------------------------------------------
1.101 espie 1058: * handle_pipe --
1.89 espie 1059: * This functions is called whenever there is something to read on the
1060: * pipe. We collect more output from the given job and store it in the
1.101 espie 1061: * job's outBuf. If this makes up lines, we print it tagged by the job's
1.89 espie 1062: * identifier, as necessary.
1.1 deraadt 1063: *
1064: * Side Effects:
1065: * curPos may be shifted as may the contents of outBuf.
1066: *-----------------------------------------------------------------------
1067: */
1.48 espie 1068: static void
1.101 espie 1069: handle_pipe(struct job_pipe *p,
1070: Job *job, FILE *out, bool finish)
1.66 espie 1071: {
1072: int nr; /* number of bytes read */
1.101 espie 1073: int oldpos; /* optimization */
1.1 deraadt 1074:
1.101 espie 1075: /* want to get everything ? -> we block */
1076: if (finish)
1077: fcntl(p->fd, F_SETFL, 0);
1078:
1079: do {
1080: nr = read(p->fd, &p->buffer[p->pos],
1081: JOB_BUFSIZE - p->pos);
1082: if (nr == -1) {
1083: if (errno == EAGAIN)
1084: break;
1085: if (DEBUG(JOB)) {
1086: perror("JobDoOutput(piperead)");
1087: }
1.66 espie 1088: }
1.101 espie 1089: oldpos = p->pos;
1090: p->pos += nr;
1091: if (!print_complete_lines(p, job, out, oldpos))
1092: if (p->pos == JOB_BUFSIZE) {
1093: print_partial_buffer(p, job, out, p->pos);
1094: p->pos = 0;
1095: }
1096: } while (nr != 0);
1.1 deraadt 1097:
1.101 espie 1098: /* at end of file, we print whatever is left */
1099: if (nr == 0) {
1100: print_partial_buffer(p, job, out, p->pos);
1101: if (p->pos > 0 && p->buffer[p->pos - 1] != '\n')
1102: putchar('\n');
1103: p->pos = 0;
1.76 espie 1104: }
1.101 espie 1105: }
1.6 millert 1106:
1.101 espie 1107: static void
1108: handle_job_output(Job *job, int i, bool finish)
1109: {
1110: handle_pipe(&job->in[i], job, i == 0 ? stdout : stderr, finish);
1.1 deraadt 1111: }
1112:
1113: /*-
1114: *-----------------------------------------------------------------------
1115: * Job_CatchChildren --
1116: * Handle the exit of a child. Called from Make_Make.
1117: *
1118: * Side Effects:
1119: * The job descriptor is removed from the list of children.
1120: *
1121: * Notes:
1122: * We do waits, blocking or not, according to the wisdom of our
1123: * caller, until there are no more children to report. For each
1124: * job, call JobFinish to finish things off. This will take care of
1125: * putting jobs on the stoppedJobs queue.
1126: *-----------------------------------------------------------------------
1127: */
1128: void
1.76 espie 1129: Job_CatchChildren()
1.1 deraadt 1130: {
1.78 espie 1131: pid_t pid; /* pid of dead child */
1132: Job *job; /* job descriptor for dead child */
1133: LstNode jnode; /* list element for finding job */
1134: int status; /* Exit/termination status */
1.1 deraadt 1135:
1.66 espie 1136: /*
1137: * Don't even bother if we know there's no one around.
1138: */
1.83 espie 1139: if (nJobs == 0) {
1.66 espie 1140: return;
1.2 deraadt 1141: }
1.6 millert 1142:
1.76 espie 1143: while ((pid = waitpid((pid_t) -1, &status, WNOHANG|WUNTRACED)) > 0) {
1.93 espie 1144: handle_all_signals();
1.84 espie 1145: debug_printf("Process %ld exited or stopped.\n", (long)pid);
1.1 deraadt 1146:
1.95 espie 1147: jnode = Lst_Find(&runningJobs, JobCmpPid, &pid);
1.1 deraadt 1148:
1.18 espie 1149: if (jnode == NULL) {
1.68 espie 1150: if (WIFSIGNALED(status) &&
1.66 espie 1151: (WTERMSIG(status) == SIGCONT)) {
1152: jnode = Lst_Find(&stoppedJobs, JobCmpPid, &pid);
1153: if (jnode == NULL) {
1154: Error("Resumed child (%ld) not in table", (long)pid);
1155: continue;
1156: }
1157: job = (Job *)Lst_Datum(jnode);
1158: Lst_Remove(&stoppedJobs, jnode);
1159: } else {
1160: Error("Child (%ld) not in table?", (long)pid);
1161: continue;
1162: }
1163: } else {
1164: job = (Job *)Lst_Datum(jnode);
1.95 espie 1165: Lst_Remove(&runningJobs, jnode);
1.67 espie 1166: nJobs--;
1.84 espie 1167: if (jobFull)
1168: debug_printf("Job queue is no longer full.\n");
1.66 espie 1169: jobFull = false;
1.1 deraadt 1170: }
1.66 espie 1171:
1.101 espie 1172: JobFinish(job, status);
1.1 deraadt 1173: }
1174: }
1175:
1176: /*-
1177: *-----------------------------------------------------------------------
1178: * Job_CatchOutput --
1179: * Catch the output from our children, if we're using
1180: * pipes do so. Otherwise just block time until we get a
1.6 millert 1181: * signal (most likely a SIGCHLD) since there's no point in
1.1 deraadt 1182: * just spinning when there's nothing to do and the reaping
1.6 millert 1183: * of a child can wait for a while.
1.1 deraadt 1184: *
1185: * Side Effects:
1186: * Output is read from pipes if we're piping.
1187: * -----------------------------------------------------------------------
1188: */
1189: void
1.56 espie 1190: Job_CatchOutput(void)
1.1 deraadt 1191: {
1.66 espie 1192: int nfds;
1193: struct timeval timeout;
1194: LstNode ln;
1195: Job *job;
1.101 espie 1196: int i;
1.66 espie 1197:
1.76 espie 1198: int count = howmany(outputsn+1, NFDBITS) * sizeof(fd_mask);
1199: fd_set *readfdsp = malloc(count);
1.91 espie 1200:
1.77 espie 1201: (void)fflush(stdout);
1.76 espie 1202: if (readfdsp == NULL)
1203: return;
1204:
1205: memcpy(readfdsp, outputsp, count);
1206: timeout.tv_sec = SEL_SEC;
1207: timeout.tv_usec = SEL_USEC;
1.66 espie 1208:
1.91 espie 1209: nfds = select(outputsn+1, readfdsp, NULL, NULL, &timeout);
1.93 espie 1210: handle_all_signals();
1.91 espie 1211: if (nfds > 0) {
1.95 espie 1212: for (ln = Lst_First(&runningJobs); nfds && ln != NULL;
1.76 espie 1213: ln = Lst_Adv(ln)) {
1214: job = (Job *)Lst_Datum(ln);
1.101 espie 1215: for (i = 0; i < 2; i++) {
1216: if (FD_ISSET(job->in[i].fd, readfdsp)) {
1217: handle_job_output(job, i, false);
1218: nfds--;
1219: }
1.66 espie 1220: }
1.1 deraadt 1221: }
1222: }
1.76 espie 1223: free(readfdsp);
1.1 deraadt 1224: }
1225:
1226: /*-
1227: *-----------------------------------------------------------------------
1228: * Job_Make --
1229: * Start the creation of a target. Basically a front-end for
1230: * JobStart used by the Make module.
1231: *
1232: * Side Effects:
1233: * Another job is started.
1234: *-----------------------------------------------------------------------
1235: */
1236: void
1.56 espie 1237: Job_Make(GNode *gn)
1.1 deraadt 1238: {
1.86 espie 1239: (void)JobStart(gn, 0);
1.1 deraadt 1240: }
1241:
1242: /*-
1243: *-----------------------------------------------------------------------
1244: * Job_Init --
1245: * Initialize the process module
1246: *
1247: * Side Effects:
1248: * lists and counters are initialized
1249: *-----------------------------------------------------------------------
1250: */
1251: void
1.83 espie 1252: Job_Init(int maxproc)
1.1 deraadt 1253: {
1.95 espie 1254: Static_Lst_Init(&runningJobs);
1.66 espie 1255: Static_Lst_Init(&stoppedJobs);
1.96 espie 1256: Static_Lst_Init(&queuedJobs);
1.101 espie 1257: Static_Lst_Init(&errorsList);
1.66 espie 1258: maxJobs = maxproc;
1.79 espie 1259: nJobs = 0;
1.66 espie 1260: jobFull = false;
1.101 espie 1261: errors = 0;
1.66 espie 1262:
1263: aborting = 0;
1.40 espie 1264:
1.66 espie 1265: lastNode = NULL;
1.1 deraadt 1266:
1.78 espie 1267: if ((begin_node->type & OP_DUMMY) == 0) {
1.86 espie 1268: JobStart(begin_node, JOB_SPECIAL);
1.66 espie 1269: while (nJobs) {
1270: Job_CatchOutput();
1.76 espie 1271: Job_CatchChildren();
1.66 espie 1272: }
1.1 deraadt 1273: }
1274: }
1275:
1276: /*-
1277: *-----------------------------------------------------------------------
1278: * Job_Full --
1279: * See if the job table is full. It is considered full if it is OR
1280: * if we are in the process of aborting OR if we have
1281: * reached/exceeded our local quota. This prevents any more jobs
1282: * from starting up.
1283: *
1284: * Results:
1.41 espie 1285: * true if the job table is full, false otherwise
1.1 deraadt 1286: *-----------------------------------------------------------------------
1287: */
1.41 espie 1288: bool
1.56 espie 1289: Job_Full(void)
1.1 deraadt 1290: {
1.66 espie 1291: return aborting || jobFull;
1.1 deraadt 1292: }
1293:
1294: /*-
1295: *-----------------------------------------------------------------------
1296: * Job_Empty --
1.40 espie 1297: * See if the job table is empty. Because the local concurrency may
1.1 deraadt 1298: * be set to 0, it is possible for the job table to become empty,
1299: * while the list of stoppedJobs remains non-empty. In such a case,
1300: * we want to restart as many jobs as we can.
1301: *
1302: * Results:
1.41 espie 1303: * true if it is. false if it ain't.
1.1 deraadt 1304: * -----------------------------------------------------------------------
1305: */
1.41 espie 1306: bool
1.56 espie 1307: Job_Empty(void)
1.1 deraadt 1308: {
1.66 espie 1309: if (nJobs == 0) {
1310: if (!Lst_IsEmpty(&stoppedJobs) && !aborting) {
1311: /*
1312: * The job table is obviously not full if it has no
1313: * jobs in it...Try and restart the stopped jobs.
1314: */
1315: jobFull = false;
1316: JobRestartJobs();
1317: return false;
1318: } else {
1319: return true;
1320: }
1.1 deraadt 1321: } else {
1.66 espie 1322: return false;
1.1 deraadt 1323: }
1324: }
1325:
1326: /*-
1327: *-----------------------------------------------------------------------
1328: * JobInterrupt --
1329: * Handle the receipt of an interrupt.
1330: *
1331: * Side Effects:
1332: * All children are killed. Another job will be started if the
1333: * .INTERRUPT target was given.
1334: *-----------------------------------------------------------------------
1335: */
1336: static void
1.56 espie 1337: JobInterrupt(int runINTERRUPT, /* Non-zero if commands for the .INTERRUPT
1.1 deraadt 1338: * target should be executed */
1.66 espie 1339: int signo) /* signal received */
1.1 deraadt 1340: {
1.66 espie 1341: LstNode ln; /* element in job table */
1.78 espie 1342: Job *job; /* job descriptor in that element */
1.66 espie 1343:
1344: aborting = ABORT_INTERRUPT;
1345:
1.95 espie 1346: for (ln = Lst_First(&runningJobs); ln != NULL; ln = Lst_Adv(ln)) {
1.66 espie 1347: job = (Job *)Lst_Datum(ln);
1348:
1349: if (!Targ_Precious(job->node)) {
1350: const char *file = job->node->path == NULL ?
1351: job->node->name : job->node->path;
1352: if (!noExecute && eunlink(file) != -1) {
1353: Error("*** %s removed", file);
1354: }
1355: }
1356: if (job->pid) {
1.84 espie 1357: debug_printf("JobInterrupt passing signal to "
1358: "child %ld.\n", (long)job->pid);
1.66 espie 1359: KILL(job->pid, signo);
1360: }
1.2 deraadt 1361: }
1.1 deraadt 1362:
1.66 espie 1363: if (runINTERRUPT && !touchFlag) {
1.78 espie 1364: if ((interrupt_node->type & OP_DUMMY) == 0) {
1.66 espie 1365: ignoreErrors = false;
1366:
1.101 espie 1367: JobStart(interrupt_node, 0);
1.66 espie 1368: while (nJobs) {
1369: Job_CatchOutput();
1.76 espie 1370: Job_CatchChildren();
1.66 espie 1371: }
1372: }
1.1 deraadt 1373: }
1.66 espie 1374: exit(signo);
1.1 deraadt 1375: }
1376:
1377: /*
1378: *-----------------------------------------------------------------------
1.12 espie 1379: * Job_Finish --
1.1 deraadt 1380: * Do final processing such as the running of the commands
1.6 millert 1381: * attached to the .END target.
1.1 deraadt 1382: *
1383: * Results:
1384: * Number of errors reported.
1.40 espie 1385: *
1.1 deraadt 1386: *-----------------------------------------------------------------------
1387: */
1388: int
1.56 espie 1389: Job_Finish(void)
1.1 deraadt 1390: {
1.78 espie 1391: if (end_node != NULL && !Lst_IsEmpty(&end_node->commands)) {
1.66 espie 1392: if (errors) {
1393: Error("Errors reported so .END ignored");
1394: } else {
1.101 espie 1395: JobStart(end_node, JOB_SPECIAL);
1.1 deraadt 1396:
1.66 espie 1397: while (nJobs) {
1398: Job_CatchOutput();
1.76 espie 1399: Job_CatchChildren();
1.66 espie 1400: }
1401: }
1.1 deraadt 1402: }
1.66 espie 1403: return errors;
1.1 deraadt 1404: }
1405:
1.41 espie 1406: #ifdef CLEANUP
1.12 espie 1407: void
1.56 espie 1408: Job_End(void)
1.12 espie 1409: {
1.41 espie 1410: }
1.13 espie 1411: #endif
1.40 espie 1412:
1.1 deraadt 1413: /*-
1414: *-----------------------------------------------------------------------
1415: * Job_Wait --
1416: * Waits for all running jobs to finish and returns. Sets 'aborting'
1417: * to ABORT_WAIT to prevent other jobs from starting.
1418: *
1419: * Side Effects:
1420: * Currently running jobs finish.
1421: *
1422: *-----------------------------------------------------------------------
1423: */
1424: void
1.56 espie 1425: Job_Wait(void)
1.1 deraadt 1426: {
1.66 espie 1427: aborting = ABORT_WAIT;
1428: while (nJobs != 0) {
1429: Job_CatchOutput();
1.76 espie 1430: Job_CatchChildren();
1.66 espie 1431: }
1432: aborting = 0;
1.1 deraadt 1433: }
1434:
1435: /*-
1436: *-----------------------------------------------------------------------
1437: * Job_AbortAll --
1438: * Abort all currently running jobs without handling output or anything.
1439: * This function is to be called only in the event of a major
1440: * error. Most definitely NOT to be called from JobInterrupt.
1441: *
1442: * Side Effects:
1443: * All children are killed, not just the firstborn
1444: *-----------------------------------------------------------------------
1445: */
1446: void
1.56 espie 1447: Job_AbortAll(void)
1.1 deraadt 1448: {
1.66 espie 1449: LstNode ln; /* element in job table */
1450: Job *job; /* the job descriptor in that element */
1451: int foo;
1.6 millert 1452:
1.66 espie 1453: aborting = ABORT_ERROR;
1.6 millert 1454:
1.66 espie 1455: if (nJobs) {
1.95 espie 1456: for (ln = Lst_First(&runningJobs); ln != NULL;
1457: ln = Lst_Adv(ln)) {
1.66 espie 1458: job = (Job *)Lst_Datum(ln);
1459:
1460: /*
1461: * kill the child process with increasingly drastic
1462: * signals to make darn sure it's dead.
1463: */
1464: KILL(job->pid, SIGINT);
1465: KILL(job->pid, SIGKILL);
1466: }
1.1 deraadt 1467: }
1.6 millert 1468:
1.66 espie 1469: /*
1470: * Catch as many children as want to report in at first, then give up
1471: */
1472: while (waitpid(-1, &foo, WNOHANG) > 0)
1473: continue;
1.2 deraadt 1474: }
1.40 espie 1475:
1.2 deraadt 1476: /*-
1477: *-----------------------------------------------------------------------
1478: * JobRestartJobs --
1479: * Tries to restart stopped jobs if there are slots available.
1480: * Note that this tries to restart them regardless of pending errors.
1481: * It's not good to leave stopped jobs lying around!
1482: *
1483: * Side Effects:
1484: * Resumes(and possibly migrates) jobs.
1485: *-----------------------------------------------------------------------
1486: */
1487: static void
1.56 espie 1488: JobRestartJobs(void)
1.2 deraadt 1489: {
1.66 espie 1490: Job *job;
1.19 espie 1491:
1.66 espie 1492: while (!jobFull && (job = (Job *)Lst_DeQueue(&stoppedJobs)) != NULL) {
1.84 espie 1493: debug_printf("Job queue is not full. "
1494: "Restarting a stopped job.\n");
1.66 espie 1495: JobRestart(job);
1.2 deraadt 1496: }
1.1 deraadt 1497: }