Annotation of src/usr.bin/make/job.c, Revision 1.151
1.151 ! espie 1: /* $OpenBSD: job.c,v 1.150 2020/01/13 15:00:59 espie Exp $ */
1.6 millert 2: /* $NetBSD: job.c,v 1.16 1996/11/06 17:59:08 christos Exp $ */
1.1 deraadt 3:
4: /*
1.124 espie 5: * Copyright (c) 2012 Marc Espie.
6: *
7: * Extensive code modifications for the OpenBSD project.
8: *
9: * Redistribution and use in source and binary forms, with or without
10: * modification, are permitted provided that the following conditions
11: * are met:
12: * 1. Redistributions of source code must retain the above copyright
13: * notice, this list of conditions and the following disclaimer.
14: * 2. Redistributions in binary form must reproduce the above copyright
15: * notice, this list of conditions and the following disclaimer in the
16: * documentation and/or other materials provided with the distribution.
17: *
18: * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
19: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD
22: * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29: */
30: /*
1.1 deraadt 31: * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
32: * Copyright (c) 1988, 1989 by Adam de Boor
33: * Copyright (c) 1989 by Berkeley Softworks
34: * All rights reserved.
35: *
36: * This code is derived from software contributed to Berkeley by
37: * Adam de Boor.
38: *
39: * Redistribution and use in source and binary forms, with or without
40: * modification, are permitted provided that the following conditions
41: * are met:
42: * 1. Redistributions of source code must retain the above copyright
43: * notice, this list of conditions and the following disclaimer.
44: * 2. Redistributions in binary form must reproduce the above copyright
45: * notice, this list of conditions and the following disclaimer in the
46: * documentation and/or other materials provided with the distribution.
1.55 millert 47: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 48: * may be used to endorse or promote products derived from this software
49: * without specific prior written permission.
50: *
51: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61: * SUCH DAMAGE.
62: */
63:
64: /*-
65: * job.c --
66: * handle the creation etc. of our child processes.
67: *
68: * Interface:
1.40 espie 69: * Job_Make Start the creation of the given target.
1.1 deraadt 70: *
1.124 espie 71: * Job_Init Called to initialize this module.
72: *
73: * Job_Begin execute commands attached to the .BEGIN target
74: * if any.
1.40 espie 75: *
1.117 espie 76: * can_start_job Return true if we can start job
1.40 espie 77: *
1.41 espie 78: * Job_Empty Return true if the job table is completely
1.40 espie 79: * empty.
80: *
81: * Job_Finish Perform any final processing which needs doing.
82: * This includes the execution of any commands
83: * which have been/were attached to the .END
1.124 espie 84: * target.
1.40 espie 85: *
1.117 espie 86: * Job_AbortAll Abort all current jobs. It doesn't
1.40 espie 87: * handle output or do anything for the jobs,
1.124 espie 88: * just kills them.
1.1 deraadt 89: *
1.117 espie 90: * Job_Wait Wait for all running jobs to finish.
1.1 deraadt 91: */
92:
93: #include <sys/types.h>
94: #include <sys/wait.h>
1.41 espie 95: #include <ctype.h>
96: #include <errno.h>
1.1 deraadt 97: #include <fcntl.h>
1.41 espie 98: #include <signal.h>
1.69 espie 99: #include <stdarg.h>
1.1 deraadt 100: #include <stdio.h>
1.42 espie 101: #include <stdlib.h>
1.1 deraadt 102: #include <string.h>
1.41 espie 103: #include <unistd.h>
104: #include "config.h"
105: #include "defines.h"
1.1 deraadt 106: #include "job.h"
1.63 espie 107: #include "engine.h"
1.1 deraadt 108: #include "pathnames.h"
1.41 espie 109: #include "var.h"
110: #include "targ.h"
111: #include "error.h"
1.124 espie 112: #include "extern.h"
1.41 espie 113: #include "lst.h"
114: #include "gnode.h"
115: #include "memory.h"
116: #include "make.h"
1.124 espie 117: #include "buf.h"
1.78 espie 118:
1.40 espie 119: static int aborting = 0; /* why is the make aborting? */
120: #define ABORT_ERROR 1 /* Because of an error */
121: #define ABORT_INTERRUPT 2 /* Because it was interrupted */
122: #define ABORT_WAIT 3 /* Waiting for jobs to finish */
1.1 deraadt 123:
1.124 espie 124: static bool no_new_jobs; /* Mark recursive shit so we shouldn't start
125: * something else at the same time
126: */
1.148 espie 127: bool sequential;
1.124 espie 128: Job *runningJobs; /* Jobs currently running a process */
129: Job *errorJobs; /* Jobs in error at end */
1.148 espie 130: Job *availableJobs; /* Pool of available jobs */
1.124 espie 131: static Job *heldJobs; /* Jobs not running yet because of expensive */
1.134 espie 132: static pid_t mypid; /* Used for printing debugging messages */
1.148 espie 133: static Job *extra_job; /* Needed for .INTERRUPT */
1.1 deraadt 134:
1.128 espie 135: static volatile sig_atomic_t got_fatal;
1.112 espie 136:
1.124 espie 137: static volatile sig_atomic_t got_SIGINT, got_SIGHUP, got_SIGQUIT, got_SIGTERM,
1.128 espie 138: got_SIGINFO;
1.112 espie 139:
1.124 espie 140: static sigset_t sigset, emptyset;
1.112 espie 141:
1.126 espie 142: static void handle_fatal_signal(int);
1.124 espie 143: static void handle_siginfo(void);
1.142 espie 144: static void postprocess_job(Job *);
1.124 espie 145: static Job *prepare_job(GNode *);
146: static void determine_job_next_step(Job *);
147: static void may_continue_job(Job *);
148: static Job *reap_finished_job(pid_t);
149: static bool reap_jobs(void);
1.146 espie 150: static void may_continue_heldback_jobs();
1.112 espie 151:
1.111 espie 152: static void loop_handle_running_jobs(void);
1.124 espie 153: static bool expensive_job(Job *);
154: static bool expensive_command(const char *);
155: static void setup_signal(int);
156: static void notice_signal(int);
157: static void setup_all_signals(void);
1.128 espie 158: static const char *really_kill(Job *, int);
159: static void debug_kill_printf(const char *, ...);
160: static void debug_vprintf(const char *, va_list);
161: static void may_remove_target(Job *);
1.131 espie 162: static void print_error(Job *);
163: static void internal_print_errors(void);
164:
165: static int dying_signal = 0;
166:
167: const char * basedirectory = NULL;
1.127 espie 168:
1.128 espie 169: static const char *
170: really_kill(Job *job, int signo)
171: {
172: pid_t pid = job->pid;
173: if (getpgid(pid) != getpgrp()) {
174: if (killpg(pid, signo) == 0)
175: return "group got signal";
176: pid = -pid;
177: } else {
178: if (kill(pid, signo) == 0)
179: return "process got signal";
180: }
1.138 natano 181: if (errno == ESRCH)
1.128 espie 182: job->flags |= JOB_LOST;
1.138 natano 183: return strerror(errno);
1.128 espie 184: }
185:
186: static void
187: may_remove_target(Job *j)
188: {
1.131 espie 189: int dying = check_dying_signal();
190:
191: if (dying && !noExecute && !Targ_Precious(j->node)) {
1.128 espie 192: const char *file = Var(TARGET_INDEX, j->node);
193: int r = eunlink(file);
194:
195: if (DEBUG(JOB) && r == -1)
196: fprintf(stderr, " *** would unlink %s\n", file);
197: if (r != -1)
198: fprintf(stderr, " *** %s removed\n", file);
1.127 espie 199: }
200: }
1.126 espie 201:
1.131 espie 202: static void
203: buf_addcurdir(BUFFER *buf)
204: {
205: const char *v = Var_Value(".CURDIR");
206: if (basedirectory != NULL) {
207: size_t len = strlen(basedirectory);
208: if (strncmp(basedirectory, v, len) == 0 &&
209: v[len] == '/') {
210: v += len+1;
211: } else if (strcmp(basedirectory, v) == 0) {
212: Buf_AddString(buf, ".");
213: return;
214: }
215: }
216: Buf_AddString(buf, v);
217: }
218:
219: static const char *
220: shortened_curdir(void)
221: {
222: static BUFFER buf;
1.140 espie 223: static bool first = true;
1.131 espie 224: if (first) {
225: Buf_Init(&buf, 0);
226: buf_addcurdir(&buf);
227: first = false;
228: }
229: return Buf_Retrieve(&buf);
230: }
231:
232: static void
233: quick_error(Job *j, int signo, bool first)
234: {
235: if (first) {
236: fprintf(stderr, "*** Signal SIG%s", sys_signame[signo]);
237: fprintf(stderr, " in %s (", shortened_curdir());
238: } else
239: fprintf(stderr, " ");
240:
241: fprintf(stderr, "%s", j->node->name);
242: free(j->cmd);
243: }
244:
1.126 espie 245: static void
1.130 espie 246: print_error(Job *j)
1.126 espie 247: {
1.130 espie 248: static bool first = true;
1.131 espie 249: BUFFER buf;
250:
251: Buf_Init(&buf, 0);
1.126 espie 252:
1.131 espie 253: if (j->exit_type == JOB_EXIT_BAD)
254: Buf_printf(&buf, "*** Error %d", j->code);
255: else if (j->exit_type == JOB_SIGNALED) {
1.128 espie 256: if (j->code < NSIG)
1.131 espie 257: Buf_printf(&buf, "*** Signal SIG%s",
1.128 espie 258: sys_signame[j->code]);
259: else
1.131 espie 260: Buf_printf(&buf, "*** unknown signal %d", j->code);
1.128 espie 261: } else
1.131 espie 262: Buf_printf(&buf, "*** Should not happen %d/%d",
263: j->exit_type, j->code);
1.128 espie 264: if (DEBUG(KILL) && (j->flags & JOB_LOST))
1.131 espie 265: Buf_AddChar(&buf, '!');
1.130 espie 266: if (first) {
1.131 espie 267: Buf_AddString(&buf, " in ");
268: buf_addcurdir(&buf);
1.130 espie 269: first = false;
270: }
1.131 espie 271: Buf_printf(&buf, " (%s:%lu", j->location->fname, j->location->lineno);
272: Buf_printf(&buf, " '%s'", j->node->name);
273: if ((j->flags & (JOB_SILENT | JOB_IS_EXPENSIVE)) == JOB_SILENT
1.132 espie 274: && Buf_Size(&buf) < 140-2) {
1.131 espie 275: size_t len = strlen(j->cmd);
276: Buf_AddString(&buf, ": ");
277: if (len + Buf_Size(&buf) < 140)
278: Buf_AddString(&buf, j->cmd);
279: else {
280: Buf_AddChars(&buf, 140 - Buf_Size(&buf), j->cmd);
281: Buf_AddString(&buf, "...");
282: }
283: }
284: fprintf(stderr, "%s)\n", Buf_Retrieve(&buf));
285: Buf_Destroy(&buf);
1.126 espie 286: free(j->cmd);
1.131 espie 287: }
288: static void
289: quick_summary(int signo)
290: {
291: Job *j, *k, *jnext;
292: bool first = true;
293:
294: k = errorJobs;
295: errorJobs = NULL;
296: for (j = k; j != NULL; j = jnext) {
297: jnext = j->next;
298: if ((j->exit_type == JOB_EXIT_BAD && j->code == signo+128) ||
299: (j->exit_type == JOB_SIGNALED && j->code == signo)) {
300: quick_error(j, signo, first);
301: first = false;
302: } else {
303: j->next = errorJobs;
304: errorJobs = j;
305: }
306: }
307: if (!first)
308: fprintf(stderr, ")\n");
1.126 espie 309: }
1.93 espie 310:
1.131 espie 311: static void
312: internal_print_errors()
1.101 espie 313: {
1.126 espie 314: Job *j, *k, *jnext;
1.131 espie 315: int dying;
1.124 espie 316:
1.130 espie 317: if (!errorJobs)
1.133 espie 318: fprintf(stderr, "Stop in %s\n", shortened_curdir());
1.1 deraadt 319:
1.131 espie 320: for (j = errorJobs; j != NULL; j = j->next)
321: may_remove_target(j);
322: dying = check_dying_signal();
323: if (dying)
324: quick_summary(dying);
1.143 espie 325: /* Print errors grouped by file name. */
1.126 espie 326: while (errorJobs != NULL) {
1.143 espie 327: /* Select the first job. */
1.126 espie 328: k = errorJobs;
329: errorJobs = NULL;
330: for (j = k; j != NULL; j = jnext) {
331: jnext = j->next;
332: if (j->location->fname == k->location->fname)
1.143 espie 333: /* Print errors with the same filename. */
1.130 espie 334: print_error(j);
1.126 espie 335: else {
1.143 espie 336: /* Keep others for the next iteration. */
1.126 espie 337: j->next = errorJobs;
338: errorJobs = j;
339: }
340: }
1.101 espie 341: }
1.88 espie 342: }
343:
1.131 espie 344: void
345: print_errors(void)
346: {
347: handle_all_signals();
348: internal_print_errors();
349: }
350:
1.57 espie 351: static void
1.124 espie 352: setup_signal(int sig)
1.57 espie 353: {
1.124 espie 354: if (signal(sig, SIG_IGN) != SIG_IGN) {
355: (void)signal(sig, notice_signal);
356: sigaddset(&sigset, sig);
1.57 espie 357: }
358: }
359:
360: static void
1.124 espie 361: notice_signal(int sig)
1.1 deraadt 362: {
1.126 espie 363:
1.124 espie 364: switch(sig) {
1.117 espie 365: case SIGINT:
366: got_SIGINT++;
1.124 espie 367: got_fatal = 1;
368: break;
1.117 espie 369: case SIGHUP:
370: got_SIGHUP++;
1.124 espie 371: got_fatal = 1;
372: break;
1.117 espie 373: case SIGQUIT:
374: got_SIGQUIT++;
1.124 espie 375: got_fatal = 1;
376: break;
1.117 espie 377: case SIGTERM:
378: got_SIGTERM++;
1.124 espie 379: got_fatal = 1;
1.117 espie 380: break;
1.124 espie 381: case SIGINFO:
382: got_SIGINFO++;
1.117 espie 383: break;
1.124 espie 384: case SIGCHLD:
1.117 espie 385: break;
1.66 espie 386: }
1.124 espie 387: }
1.117 espie 388:
1.135 espie 389: static void
1.124 espie 390: setup_all_signals(void)
391: {
392: sigemptyset(&sigset);
393: sigemptyset(&emptyset);
394: /*
395: * Catch the four signals that POSIX specifies if they aren't ignored.
396: * handle_signal will take care of calling JobInterrupt if appropriate.
397: */
398: setup_signal(SIGINT);
399: setup_signal(SIGHUP);
400: setup_signal(SIGQUIT);
401: setup_signal(SIGTERM);
402: /* Display running jobs on SIGINFO */
403: setup_signal(SIGINFO);
404: /* Have to see SIGCHLD */
405: setup_signal(SIGCHLD);
406: got_fatal = 0;
1.1 deraadt 407: }
408:
1.124 espie 409: static void
410: handle_siginfo(void)
1.1 deraadt 411: {
1.126 espie 412: static BUFFER buf;
413: static size_t length = 0;
414:
1.124 espie 415: Job *job;
416: bool first = true;
1.66 espie 417:
1.124 espie 418: got_SIGINFO = 0;
419: /* we have to store the info in a buffer, because status from all
420: * makes running would get intermixed otherwise
1.66 espie 421: */
1.124 espie 422:
1.126 espie 423: if (length == 0) {
424: Buf_Init(&buf, 0);
1.131 espie 425: Buf_printf(&buf, "%s in ", Var_Value("MAKE"));
426: buf_addcurdir(&buf);
427: Buf_AddString(&buf, ": ");
1.126 espie 428: length = Buf_Size(&buf);
429: } else
430: Buf_Truncate(&buf, length);
1.124 espie 431:
432: for (job = runningJobs; job != NULL ; job = job->next) {
433: if (!first)
434: Buf_puts(&buf, ", ");
435: first = false;
436: Buf_puts(&buf, job->node->name);
437: }
438: Buf_puts(&buf, first ? "nothing running\n" : "\n");
1.1 deraadt 439:
1.124 espie 440: fputs(Buf_Retrieve(&buf), stderr);
1.1 deraadt 441: }
442:
1.131 espie 443: int
444: check_dying_signal(void)
445: {
446: sigset_t set;
447: if (dying_signal)
448: return dying_signal;
449: sigpending(&set);
450: if (got_SIGINT || sigismember(&set, SIGINT))
451: return dying_signal = SIGINT;
452: if (got_SIGHUP || sigismember(&set, SIGHUP))
453: return dying_signal = SIGHUP;
454: if (got_SIGQUIT || sigismember(&set, SIGQUIT))
455: return dying_signal = SIGQUIT;
456: if (got_SIGTERM || sigismember(&set, SIGTERM))
457: return dying_signal = SIGTERM;
458: return 0;
459: }
460:
1.124 espie 461: void
462: handle_all_signals(void)
1.1 deraadt 463: {
1.124 espie 464: if (got_SIGINFO)
465: handle_siginfo();
466: while (got_fatal) {
467: got_fatal = 0;
468: aborting = ABORT_INTERRUPT;
469:
470: if (got_SIGINT) {
471: got_SIGINT=0;
1.126 espie 472: handle_fatal_signal(SIGINT);
1.124 espie 473: }
474: if (got_SIGHUP) {
475: got_SIGHUP=0;
1.126 espie 476: handle_fatal_signal(SIGHUP);
1.124 espie 477: }
478: if (got_SIGQUIT) {
479: got_SIGQUIT=0;
1.126 espie 480: handle_fatal_signal(SIGQUIT);
1.124 espie 481: }
482: if (got_SIGTERM) {
483: got_SIGTERM=0;
1.126 espie 484: handle_fatal_signal(SIGTERM);
1.124 espie 485: }
486: }
1.1 deraadt 487: }
488:
1.128 espie 489: static void
490: debug_vprintf(const char *fmt, va_list va)
491: {
492: (void)printf("[%ld] ", (long)mypid);
493: (void)vprintf(fmt, va);
494: fflush(stdout);
495: }
496:
1.124 espie 497: void
498: debug_job_printf(const char *fmt, ...)
1.84 espie 499: {
500: if (DEBUG(JOB)) {
501: va_list va;
502: va_start(va, fmt);
1.128 espie 503: debug_vprintf(fmt, va);
504: va_end(va);
505: }
506: }
507:
508: static void
509: debug_kill_printf(const char *fmt, ...)
510: {
511: if (DEBUG(KILL)) {
512: va_list va;
513: va_start(va, fmt);
514: debug_vprintf(fmt, va);
1.84 espie 515: va_end(va);
516: }
517: }
1.119 espie 518:
1.1 deraadt 519: /*-
520: *-----------------------------------------------------------------------
1.124 espie 521: * postprocess_job --
522: * Do final processing for the given job including updating
1.119 espie 523: * parents and starting new jobs as available/necessary.
1.1 deraadt 524: *
525: * Side Effects:
1.6 millert 526: * If we got an error and are aborting (aborting == ABORT_ERROR) and
1.1 deraadt 527: * the job list is now empty, we are done for the day.
1.101 espie 528: * If we recognized an error we set the aborting flag
1.1 deraadt 529: * to ABORT_ERROR so no more jobs will be started.
530: *-----------------------------------------------------------------------
531: */
532: /*ARGSUSED*/
1.112 espie 533:
1.1 deraadt 534: static void
1.142 espie 535: postprocess_job(Job *job)
1.2 deraadt 536: {
1.142 espie 537: if (job->exit_type == JOB_EXIT_OKAY &&
1.66 espie 538: aborting != ABORT_ERROR &&
1.124 espie 539: aborting != ABORT_INTERRUPT) {
1.148 espie 540: job->next = availableJobs;
541: availableJobs = job;
1.66 espie 542: /* As long as we aren't aborting and the job didn't return a
543: * non-zero status that we shouldn't ignore, we call
1.101 espie 544: * Make_Update to update the parents. */
1.141 espie 545: job->node->built_status = REBUILT;
1.66 espie 546: Make_Update(job->node);
1.148 espie 547: job->next = availableJobs;
548: availableJobs = job;
549: } else if (job->exit_type != JOB_EXIT_OKAY && keepgoing) {
550: job->next = availableJobs;
551: availableJobs = job;
552: }
1.1 deraadt 553:
1.145 espie 554: if (errorJobs != NULL && aborting != ABORT_INTERRUPT)
1.66 espie 555: aborting = ABORT_ERROR;
1.6 millert 556:
1.124 espie 557: if (aborting == ABORT_ERROR && DEBUG(QUICKDEATH))
1.126 espie 558: handle_fatal_signal(SIGINT);
1.117 espie 559: if (aborting == ABORT_ERROR && Job_Empty())
1.124 espie 560: Finish();
1.1 deraadt 561: }
562:
1.124 espie 563: /* expensive jobs handling: in order to avoid forking an exponential number
564: * of jobs, make tries to figure out "recursive make" configurations.
565: * It may err on the side of caution.
566: * Basically, a command is "expensive" if it's likely to fork an extra
567: * level of make: either by looking at the command proper, or if it has
568: * some specific qualities ('+cmd' are likely to be recursive, as are
569: * .MAKE: commands). It's possible to explicitly say some targets are
570: * expensive or cheap with .EXPENSIVE or .CHEAP.
571: *
572: * While an expensive command is running, no_new_jobs
573: * is set, so jobs that would fork new processes are accumulated in the
574: * heldJobs list instead.
575: *
1.147 espie 576: * XXX This heuristics is also used on error exit: we display silent commands
577: * that failed, unless those ARE expensive commands: expensive commands are
578: * likely to not be failing by themselves, but to be the result of a cascade of
579: * failures in descendant makes.
1.124 espie 580: */
581: void
582: determine_expensive_job(Job *job)
583: {
584: if (expensive_job(job)) {
585: job->flags |= JOB_IS_EXPENSIVE;
586: no_new_jobs = true;
587: } else
588: job->flags &= ~JOB_IS_EXPENSIVE;
589: if (DEBUG(EXPENSIVE))
590: fprintf(stderr, "[%ld] Target %s running %.50s: %s\n",
591: (long)mypid, job->node->name, job->cmd,
592: job->flags & JOB_IS_EXPENSIVE ? "expensive" : "cheap");
1.101 espie 593: }
594:
1.124 espie 595: static bool
596: expensive_job(Job *job)
1.1 deraadt 597: {
1.124 espie 598: if (job->node->type & OP_CHEAP)
599: return false;
600: if (job->node->type & (OP_EXPENSIVE | OP_MAKE))
601: return true;
602: return expensive_command(job->cmd);
1.96 espie 603: }
604:
1.117 espie 605: static bool
606: expensive_command(const char *s)
1.1 deraadt 607: {
1.117 espie 608: const char *p;
609: bool include = false;
610: bool expensive = false;
1.66 espie 611:
1.117 espie 612: /* okay, comments are cheap, always */
613: if (*s == '#')
614: return false;
1.124 espie 615: /* and commands we always execute are expensive */
616: if (*s == '+')
617: return true;
1.66 espie 618:
1.117 espie 619: for (p = s; *p != '\0'; p++) {
620: if (*p == ' ' || *p == '\t') {
621: include = false;
622: if (p[1] == '-' && p[2] == 'I')
623: include = true;
624: }
625: if (include)
626: continue;
627: /* KMP variant, avoid looking twice at the same
628: * letter.
629: */
630: if (*p != 'm')
631: continue;
632: if (p[1] != 'a')
633: continue;
634: p++;
635: if (p[1] != 'k')
636: continue;
637: p++;
638: if (p[1] != 'e')
639: continue;
640: p++;
641: expensive = true;
642: while (p[1] != '\0' && p[1] != ' ' && p[1] != '\t') {
1.124 espie 643: if (p[1] == '.' || p[1] == '/') {
1.117 espie 644: expensive = false;
645: break;
1.66 espie 646: }
1.117 espie 647: p++;
1.1 deraadt 648: }
1.117 espie 649: if (expensive)
650: return true;
1.1 deraadt 651: }
1.117 espie 652: return false;
653: }
654:
1.98 espie 655: static Job *
1.124 espie 656: prepare_job(GNode *gn)
1.78 espie 657: {
1.124 espie 658: /* a new job is prepared unless its commands are bogus (we don't
659: * have anything for it), or if we're in touch mode.
660: *
661: * Note that even in noexec mode, some commands may still run
662: * thanks to the +cmd construct.
1.1 deraadt 663: */
1.124 espie 664: if (node_find_valid_commands(gn)) {
665: if (touchFlag) {
666: Job_Touch(gn);
667: return NULL;
668: } else {
1.148 espie 669: Job *job = availableJobs;
1.66 espie 670:
1.148 espie 671: assert(job != NULL);
672: availableJobs = availableJobs->next;
1.124 espie 673: job_attach_node(job, gn);
674: return job;
675: }
1.66 espie 676: } else {
1.124 espie 677: node_failure(gn);
678: return NULL;
1.1 deraadt 679: }
1.124 espie 680: }
1.66 espie 681:
1.124 espie 682: static void
683: may_continue_job(Job *job)
684: {
685: if (no_new_jobs) {
686: if (DEBUG(EXPENSIVE))
687: fprintf(stderr, "[%ld] expensive -> hold %s\n",
688: (long)mypid, job->node->name);
689: job->next = heldJobs;
690: heldJobs = job;
1.151 ! espie 691: } else {
! 692: bool finished = job_run_next(job);
! 693: if (finished)
! 694: postprocess_job(job);
! 695: else if (!sequential)
! 696: determine_expensive_job(job);
! 697: }
1.98 espie 698: }
1.1 deraadt 699:
1.98 espie 700: /*-
701: *-----------------------------------------------------------------------
1.124 espie 702: * Job_Make --
1.98 espie 703: * Start a target-creation process going for the target described
704: * by the graph node gn.
705: *
706: * Side Effects:
1.124 espie 707: * A new Job node is created and its commands continued, which
708: * may fork the first command of that job.
1.98 espie 709: *-----------------------------------------------------------------------
710: */
1.124 espie 711: void
712: Job_Make(GNode *gn)
1.98 espie 713: {
714: Job *job;
1.124 espie 715:
716: job = prepare_job(gn);
1.98 espie 717: if (!job)
718: return;
1.124 espie 719: may_continue_job(job);
1.1 deraadt 720: }
721:
1.101 espie 722: static void
1.124 espie 723: determine_job_next_step(Job *job)
1.2 deraadt 724: {
1.124 espie 725: bool okay;
726: if (job->flags & JOB_IS_EXPENSIVE) {
727: no_new_jobs = false;
728: if (DEBUG(EXPENSIVE))
729: fprintf(stderr, "[%ld] "
730: "Returning from expensive target %s, "
731: "allowing new jobs\n", (long)mypid,
732: job->node->name);
733: }
1.2 deraadt 734:
1.142 espie 735: if (job->exit_type != JOB_EXIT_OKAY || job->next_cmd == NULL)
1.150 espie 736: postprocess_job(job);
1.124 espie 737: else
738: may_continue_job(job);
1.101 espie 739: }
740:
741: static void
1.142 espie 742: remove_job(Job *job)
1.101 espie 743: {
1.142 espie 744: postprocess_job(job);
1.146 espie 745: }
746:
747: static void
748: may_continue_heldback_jobs()
749: {
1.124 espie 750: while (!no_new_jobs) {
751: if (heldJobs != NULL) {
1.146 espie 752: Job *job = heldJobs;
1.124 espie 753: heldJobs = heldJobs->next;
754: if (DEBUG(EXPENSIVE))
755: fprintf(stderr, "[%ld] cheap -> release %s\n",
756: (long)mypid, job->node->name);
1.151 ! espie 757: may_continue_job(job);
1.124 espie 758: } else
759: break;
1.2 deraadt 760: }
761: }
1.111 espie 762:
1.124 espie 763: /*
764: * job = reap_finished_job(pid):
765: * retrieve and remove a job from runningJobs, based on its pid
1.1 deraadt 766: *
1.124 espie 767: * Note that we remove it right away, so that handle_signals()
768: * is accurate.
1.1 deraadt 769: */
1.124 espie 770: static Job *
771: reap_finished_job(pid_t pid)
1.66 espie 772: {
1.124 espie 773: Job **j, *job;
1.1 deraadt 774:
1.124 espie 775: for (j = &runningJobs; *j != NULL; j = &((*j)->next))
776: if ((*j)->pid == pid) {
777: job = *j;
778: *j = job->next;
779: return job;
1.66 espie 780: }
1.1 deraadt 781:
1.124 espie 782: return NULL;
1.101 espie 783: }
1.6 millert 784:
1.124 espie 785: /*
786: * classic waitpid handler: retrieve as many dead children as possible.
787: * returns true if succesful
788: */
789: static bool
790: reap_jobs(void)
1.117 espie 791: {
1.124 espie 792: pid_t pid; /* pid of dead child */
793: int status; /* Exit/termination status */
794: bool reaped = false;
1.117 espie 795: Job *job;
796:
797: while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0) {
1.124 espie 798: reaped = true;
799: job = reap_finished_job(pid);
1.1 deraadt 800:
1.124 espie 801: if (job == NULL) {
802: Punt("Child (%ld) not in table?", (long)pid);
1.66 espie 803: } else {
1.149 espie 804: handle_job_status(job, status);
1.124 espie 805: determine_job_next_step(job);
1.1 deraadt 806: }
1.146 espie 807: may_continue_heldback_jobs();
1.1 deraadt 808: }
1.124 espie 809: /* sanity check, should not happen */
810: if (pid == -1 && errno == ECHILD && runningJobs != NULL)
811: Punt("Process has no children, but runningJobs is not empty ?");
812: return reaped;
1.1 deraadt 813: }
814:
815: void
1.124 espie 816: handle_running_jobs(void)
1.1 deraadt 817: {
1.124 espie 818: sigset_t old;
819: /* reaping children in the presence of caught signals */
1.91 espie 820:
1.124 espie 821: /* first, we make sure to hold on new signals, to synchronize
822: * reception of new stuff on sigsuspend
823: */
824: sigprocmask(SIG_BLOCK, &sigset, &old);
1.135 espie 825: /* note this will NOT loop until runningJobs == NULL.
826: * It's merely an optimisation, namely that we don't need to go
827: * through the logic if no job is present. As soon as a job
828: * gets reaped, we WILL exit the loop through the break.
829: */
1.124 espie 830: while (runningJobs != NULL) {
831: /* did we already have pending stuff that advances things ?
832: * then handle_all_signals() will not return
833: * or reap_jobs() will reap_jobs()
834: */
835: handle_all_signals();
836: if (reap_jobs())
837: break;
838: /* okay, so it's safe to suspend, we have nothing to do but
839: * wait...
840: */
841: sigsuspend(&emptyset);
1.1 deraadt 842: }
1.124 espie 843: sigprocmask(SIG_SETMASK, &old, NULL);
1.1 deraadt 844: }
845:
1.111 espie 846: void
1.124 espie 847: handle_one_job(Job *job)
1.111 espie 848: {
1.124 espie 849: int stat;
850: int status;
851: sigset_t old;
852:
853: sigprocmask(SIG_BLOCK, &sigset, &old);
854: while (1) {
855: handle_all_signals();
856: stat = waitpid(job->pid, &status, WNOHANG);
857: if (stat == job->pid)
858: break;
859: sigsuspend(&emptyset);
860: }
861: runningJobs = NULL;
1.149 espie 862: handle_job_status(job, status);
1.124 espie 863: sigprocmask(SIG_SETMASK, &old, NULL);
1.111 espie 864: }
865:
866: static void
867: loop_handle_running_jobs()
868: {
1.124 espie 869: while (runningJobs != NULL)
1.111 espie 870: handle_running_jobs();
871: }
1.1 deraadt 872:
873: void
1.148 espie 874: Job_Init(int maxJobs)
1.1 deraadt 875: {
1.148 espie 876: Job *j;
877: int i;
878:
1.124 espie 879: runningJobs = NULL;
880: heldJobs = NULL;
881: errorJobs = NULL;
1.148 espie 882: availableJobs = NULL;
883: sequential = maxJobs == 1;
884:
885: /* we allocate n+1 jobs, since we may need an extra job for
886: * running .INTERRUPT. */
887: j = ereallocarray(NULL, sizeof(Job), maxJobs+1);
888: for (i = 0; i != maxJobs; i++) {
889: j[i].next = availableJobs;
890: availableJobs = &j[i];
891: }
892: extra_job = &j[maxJobs];
1.124 espie 893: mypid = getpid();
894:
1.117 espie 895: aborting = 0;
1.124 espie 896: setup_all_signals();
1.1 deraadt 897: }
898:
1.41 espie 899: bool
1.117 espie 900: can_start_job(void)
1.1 deraadt 901: {
1.148 espie 902: if (aborting || availableJobs == NULL)
1.117 espie 903: return false;
1.119 espie 904: else
1.117 espie 905: return true;
1.1 deraadt 906: }
907:
1.41 espie 908: bool
1.56 espie 909: Job_Empty(void)
1.1 deraadt 910: {
1.124 espie 911: return runningJobs == NULL;
1.1 deraadt 912: }
913:
914: /*-
915: *-----------------------------------------------------------------------
1.126 espie 916: * handle_fatal_signal --
917: * Handle the receipt of a fatal interrupt
1.1 deraadt 918: *
919: * Side Effects:
1.126 espie 920: * All children are killed. Another job may be started if there
921: * is an interrupt target and the signal was SIGINT.
1.1 deraadt 922: *-----------------------------------------------------------------------
923: */
924: static void
1.126 espie 925: handle_fatal_signal(int signo)
1.1 deraadt 926: {
1.124 espie 927: Job *job;
1.66 espie 928:
1.128 espie 929: debug_kill_printf("handle_fatal_signal(%d) called.\n", signo);
1.66 espie 930:
1.131 espie 931: dying_signal = signo;
1.124 espie 932: for (job = runningJobs; job != NULL; job = job->next) {
1.128 espie 933: debug_kill_printf("passing to "
934: "child %ld running %s: %s\n", (long)job->pid,
935: job->node->name, really_kill(job, signo));
936: may_remove_target(job);
1.2 deraadt 937: }
1.1 deraadt 938:
1.124 espie 939: if (signo == SIGINT && !touchFlag) {
1.78 espie 940: if ((interrupt_node->type & OP_DUMMY) == 0) {
1.66 espie 941: ignoreErrors = false;
1.148 espie 942: extra_job->next = availableJobs;
943: availableJobs = extra_job;
1.124 espie 944: Job_Make(interrupt_node);
1.66 espie 945: }
1.1 deraadt 946: }
1.124 espie 947: loop_handle_running_jobs();
1.131 espie 948: internal_print_errors();
1.124 espie 949:
950: /* die by that signal */
951: sigprocmask(SIG_BLOCK, &sigset, NULL);
952: signal(signo, SIG_DFL);
1.126 espie 953: kill(getpid(), signo);
1.124 espie 954: sigprocmask(SIG_SETMASK, &emptyset, NULL);
955: /*NOTREACHED*/
1.131 espie 956: fprintf(stderr, "This should never happen\n");
957: exit(1);
1.1 deraadt 958: }
959:
960: /*
961: *-----------------------------------------------------------------------
1.12 espie 962: * Job_Finish --
1.1 deraadt 963: * Do final processing such as the running of the commands
1.6 millert 964: * attached to the .END target.
1.1 deraadt 965: *
1.124 espie 966: * return true if fatal errors have happened.
1.1 deraadt 967: *-----------------------------------------------------------------------
968: */
1.124 espie 969: bool
1.56 espie 970: Job_Finish(void)
1.1 deraadt 971: {
1.134 espie 972: bool problem = errorJobs != NULL;
1.124 espie 973:
1.116 espie 974: if ((end_node->type & OP_DUMMY) == 0) {
1.134 espie 975: if (problem) {
1.66 espie 976: Error("Errors reported so .END ignored");
977: } else {
1.124 espie 978: Job_Make(end_node);
1.111 espie 979: loop_handle_running_jobs();
1.66 espie 980: }
1.1 deraadt 981: }
1.134 espie 982: return problem;
1.1 deraadt 983: }
984:
1.124 espie 985: void
986: Job_Begin(void)
987: {
988: if ((begin_node->type & OP_DUMMY) == 0) {
989: Job_Make(begin_node);
990: loop_handle_running_jobs();
991: }
992: }
993:
1.1 deraadt 994: /*-
995: *-----------------------------------------------------------------------
996: * Job_Wait --
997: * Waits for all running jobs to finish and returns. Sets 'aborting'
998: * to ABORT_WAIT to prevent other jobs from starting.
999: *
1000: * Side Effects:
1001: * Currently running jobs finish.
1002: *
1003: *-----------------------------------------------------------------------
1004: */
1005: void
1.56 espie 1006: Job_Wait(void)
1.1 deraadt 1007: {
1.66 espie 1008: aborting = ABORT_WAIT;
1.111 espie 1009: loop_handle_running_jobs();
1.66 espie 1010: aborting = 0;
1.1 deraadt 1011: }
1012:
1013: /*-
1014: *-----------------------------------------------------------------------
1015: * Job_AbortAll --
1016: * Abort all currently running jobs without handling output or anything.
1017: * This function is to be called only in the event of a major
1.124 espie 1018: * error.
1.1 deraadt 1019: *
1020: * Side Effects:
1.124 espie 1021: * All children are killed
1.1 deraadt 1022: *-----------------------------------------------------------------------
1023: */
1024: void
1.56 espie 1025: Job_AbortAll(void)
1.1 deraadt 1026: {
1.66 espie 1027: Job *job; /* the job descriptor in that element */
1028: int foo;
1.6 millert 1029:
1.66 espie 1030: aborting = ABORT_ERROR;
1.6 millert 1031:
1.124 espie 1032: for (job = runningJobs; job != NULL; job = job->next) {
1.126 espie 1033: killpg(job->pid, SIGINT);
1034: killpg(job->pid, SIGKILL);
1.1 deraadt 1035: }
1.6 millert 1036:
1.66 espie 1037: /*
1038: * Catch as many children as want to report in at first, then give up
1039: */
1.117 espie 1040: while (waitpid(WAIT_ANY, &foo, WNOHANG) > 0)
1.66 espie 1041: continue;
1.2 deraadt 1042: }