GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/make/job.c Lines: 225 403 55.8 %
Date: 2017-11-07 Branches: 109 250 43.6 %

Line Branch Exec Source
1
/*	$OpenBSD: job.c,v 1.139 2017/01/21 12:35:40 natano Exp $	*/
2
/*	$NetBSD: job.c,v 1.16 1996/11/06 17:59:08 christos Exp $	*/
3
4
/*
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
/*
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.
47
 * 3. Neither the name of the University nor the names of its contributors
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:
69
 *	Job_Make		Start the creation of the given target.
70
 *
71
 *	Job_Init		Called to initialize this module.
72
 *
73
 *	Job_Begin		execute commands attached to the .BEGIN target
74
 *				if any.
75
 *
76
 *	can_start_job		Return true if we can start job
77
 *
78
 *	Job_Empty		Return true if the job table is completely
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
84
 *				target.
85
 *
86
 *	Job_AbortAll		Abort all current jobs. It doesn't
87
 *				handle output or do anything for the jobs,
88
 *				just kills them.
89
 *
90
 *	Job_Wait		Wait for all running jobs to finish.
91
 */
92
93
#include <sys/types.h>
94
#include <sys/wait.h>
95
#include <ctype.h>
96
#include <errno.h>
97
#include <fcntl.h>
98
#include <signal.h>
99
#include <stdarg.h>
100
#include <stdio.h>
101
#include <stdlib.h>
102
#include <string.h>
103
#include <unistd.h>
104
#include "config.h"
105
#include "defines.h"
106
#include "job.h"
107
#include "engine.h"
108
#include "pathnames.h"
109
#include "var.h"
110
#include "targ.h"
111
#include "error.h"
112
#include "extern.h"
113
#include "lst.h"
114
#include "gnode.h"
115
#include "memory.h"
116
#include "make.h"
117
#include "buf.h"
118
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 */
123
124
static int	maxJobs;	/* The most children we can run at once */
125
static int	nJobs;		/* Number of jobs already allocated */
126
static bool	no_new_jobs;	/* Mark recursive shit so we shouldn't start
127
				 * something else at the same time
128
				 */
129
Job *runningJobs;		/* Jobs currently running a process */
130
Job *errorJobs;			/* Jobs in error at end */
131
static Job *heldJobs;		/* Jobs not running yet because of expensive */
132
static pid_t mypid;		/* Used for printing debugging messages */
133
134
static volatile sig_atomic_t got_fatal;
135
136
static volatile sig_atomic_t got_SIGINT, got_SIGHUP, got_SIGQUIT, got_SIGTERM,
137
    got_SIGINFO;
138
139
static sigset_t sigset, emptyset;
140
141
static void handle_fatal_signal(int);
142
static void handle_siginfo(void);
143
static void postprocess_job(Job *, bool);
144
static Job *prepare_job(GNode *);
145
static void determine_job_next_step(Job *);
146
static void remove_job(Job *, bool);
147
static void may_continue_job(Job *);
148
static void continue_job(Job *);
149
static Job *reap_finished_job(pid_t);
150
static bool reap_jobs(void);
151
152
static void loop_handle_running_jobs(void);
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);
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 *);
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;
168
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
	}
181
	if (errno == ESRCH)
182
		job->flags |= JOB_LOST;
183
	return strerror(errno);
184
}
185
186
static void
187
may_remove_target(Job *j)
188
{
189
1470
	int dying = check_dying_signal();
190
191

735
	if (dying && !noExecute && !Targ_Precious(j->node)) {
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);
199
	}
200
735
}
201
202
static void
203
buf_addcurdir(BUFFER *buf)
204
{
205
1482
	const char *v = Var_Value(".CURDIR");
206
741
	if (basedirectory != NULL) {
207
675
		size_t len = strlen(basedirectory);
208

1350
		if (strncmp(basedirectory, v, len) == 0 &&
209
675
		    v[len] == '/') {
210
610
			v += len+1;
211
675
		} else if (strcmp(basedirectory, v) == 0) {
212
65
			Buf_AddString(buf, ".");
213
65
			return;
214
		}
215
610
	}
216
676
	Buf_AddString(buf, v);
217
1417
}
218
219
static const char *
220
shortened_curdir(void)
221
{
222
	static BUFFER buf;
223
	bool first = true;
224
12
	if (first) {
225
6
		Buf_Init(&buf, 0);
226
6
		buf_addcurdir(&buf);
227
		first = false;
228
6
	}
229
6
	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
245
static void
246
print_error(Job *j)
247
{
248
	static bool first = true;
249
1470
	BUFFER buf;
250
251
735
	Buf_Init(&buf, 0);
252
253
735
	if (j->exit_type == JOB_EXIT_BAD)
254
734
		Buf_printf(&buf, "*** Error %d", j->code);
255
1
	else if (j->exit_type == JOB_SIGNALED) {
256
1
		if (j->code < NSIG)
257
1
			Buf_printf(&buf, "*** Signal SIG%s",
258
1
			    sys_signame[j->code]);
259
		else
260
			Buf_printf(&buf, "*** unknown signal %d", j->code);
261
	} else
262
		Buf_printf(&buf, "*** Should not happen %d/%d",
263
		    j->exit_type, j->code);
264

735
	if (DEBUG(KILL) && (j->flags & JOB_LOST))
265
		Buf_AddChar(&buf, '!');
266
735
	if (first) {
267
735
		Buf_AddString(&buf, " in ");
268
735
		buf_addcurdir(&buf);
269
735
		first = false;
270
735
	}
271
735
	Buf_printf(&buf, " (%s:%lu", j->location->fname, j->location->lineno);
272
735
	Buf_printf(&buf, " '%s'", j->node->name);
273
873
	if ((j->flags & (JOB_SILENT | JOB_IS_EXPENSIVE)) == JOB_SILENT
274
873
	    && Buf_Size(&buf) < 140-2) {
275
138
		size_t len = strlen(j->cmd);
276
138
		Buf_AddString(&buf, ": ");
277
138
		if (len + Buf_Size(&buf) < 140)
278
138
			Buf_AddString(&buf, j->cmd);
279
		else {
280
			Buf_AddChars(&buf, 140 - Buf_Size(&buf), j->cmd);
281
			Buf_AddString(&buf, "...");
282
		}
283
138
	}
284
735
	fprintf(stderr, "%s)\n", Buf_Retrieve(&buf));
285
735
	Buf_Destroy(&buf);
286
735
	free(j->cmd);
287
735
}
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");
309
}
310
311
static void
312
internal_print_errors()
313
{
314
	Job *j, *k, *jnext;
315
	int dying;
316
317
1482
	if (!errorJobs)
318
6
		fprintf(stderr, "Stop in %s\n", shortened_curdir());
319
320
2952
	for (j = errorJobs; j != NULL; j = j->next)
321
735
		may_remove_target(j);
322
741
	dying = check_dying_signal();
323
741
	if (dying)
324
		quick_summary(dying);
325
1476
	while (errorJobs != NULL) {
326
		k = errorJobs;
327
735
		errorJobs = NULL;
328
2940
		for (j = k; j != NULL; j = jnext) {
329
735
			jnext = j->next;
330
735
			if (j->location->fname == k->location->fname)
331
735
				print_error(j);
332
			else {
333
				j->next = errorJobs;
334
				errorJobs = j;
335
			}
336
		}
337
	}
338
741
}
339
340
void
341
print_errors(void)
342
{
343
1482
	handle_all_signals();
344
741
	internal_print_errors();
345
741
}
346
347
static void
348
setup_signal(int sig)
349
{
350
408816
	if (signal(sig, SIG_IGN) != SIG_IGN) {
351
204408
		(void)signal(sig, notice_signal);
352
204408
		sigaddset(&sigset, sig);
353
204408
	}
354
204408
}
355
356
static void
357
notice_signal(int sig)
358
{
359
360

510998
	switch(sig) {
361
	case SIGINT:
362
		got_SIGINT++;
363
		got_fatal = 1;
364
		break;
365
	case SIGHUP:
366
		got_SIGHUP++;
367
		got_fatal = 1;
368
		break;
369
	case SIGQUIT:
370
		got_SIGQUIT++;
371
		got_fatal = 1;
372
		break;
373
	case SIGTERM:
374
		got_SIGTERM++;
375
		got_fatal = 1;
376
		break;
377
	case SIGINFO:
378
		got_SIGINFO++;
379
		break;
380
	case SIGCHLD:
381
		break;
382
	}
383
255499
}
384
385
static void
386
setup_all_signals(void)
387
{
388
68136
	sigemptyset(&sigset);
389
34068
	sigemptyset(&emptyset);
390
	/*
391
	 * Catch the four signals that POSIX specifies if they aren't ignored.
392
	 * handle_signal will take care of calling JobInterrupt if appropriate.
393
	 */
394
34068
	setup_signal(SIGINT);
395
34068
	setup_signal(SIGHUP);
396
34068
	setup_signal(SIGQUIT);
397
34068
	setup_signal(SIGTERM);
398
	/* Display running jobs on SIGINFO */
399
34068
	setup_signal(SIGINFO);
400
	/* Have to see SIGCHLD */
401
34068
	setup_signal(SIGCHLD);
402
34068
	got_fatal = 0;
403
34068
}
404
405
static void
406
handle_siginfo(void)
407
{
408
	static BUFFER buf;
409
	static size_t length = 0;
410
411
	Job *job;
412
	bool first = true;
413
414
	got_SIGINFO = 0;
415
	/* we have to store the info in a buffer, because status from all
416
	 * makes running would get intermixed otherwise
417
	 */
418
419
	if (length == 0) {
420
		Buf_Init(&buf, 0);
421
		Buf_printf(&buf, "%s in ", Var_Value("MAKE"));
422
		buf_addcurdir(&buf);
423
		Buf_AddString(&buf, ": ");
424
		length = Buf_Size(&buf);
425
	} else
426
		Buf_Truncate(&buf, length);
427
428
	for (job = runningJobs; job != NULL ; job = job->next) {
429
		if (!first)
430
			Buf_puts(&buf, ", ");
431
		first = false;
432
		Buf_puts(&buf, job->node->name);
433
	}
434
	Buf_puts(&buf, first ? "nothing running\n" : "\n");
435
436
	fputs(Buf_Retrieve(&buf), stderr);
437
}
438
439
int
440
check_dying_signal(void)
441
{
442
2952
	sigset_t set;
443
1476
	if (dying_signal)
444
		return dying_signal;
445
1476
	sigpending(&set);
446

2952
	if (got_SIGINT || sigismember(&set, SIGINT))
447
		return dying_signal = SIGINT;
448

2952
	if (got_SIGHUP || sigismember(&set, SIGHUP))
449
		return dying_signal = SIGHUP;
450

2952
	if (got_SIGQUIT || sigismember(&set, SIGQUIT))
451
		return dying_signal = SIGQUIT;
452

2952
	if (got_SIGTERM || sigismember(&set, SIGTERM))
453
		return dying_signal = SIGTERM;
454
1476
	return 0;
455
1476
}
456
457
void
458
handle_all_signals(void)
459
{
460
1533784
	if (got_SIGINFO)
461
		handle_siginfo();
462
766892
	while (got_fatal) {
463
		got_fatal = 0;
464
		aborting = ABORT_INTERRUPT;
465
466
		if (got_SIGINT) {
467
			got_SIGINT=0;
468
			handle_fatal_signal(SIGINT);
469
		}
470
		if (got_SIGHUP) {
471
			got_SIGHUP=0;
472
			handle_fatal_signal(SIGHUP);
473
		}
474
		if (got_SIGQUIT) {
475
			got_SIGQUIT=0;
476
			handle_fatal_signal(SIGQUIT);
477
		}
478
		if (got_SIGTERM) {
479
			got_SIGTERM=0;
480
			handle_fatal_signal(SIGTERM);
481
		}
482
	}
483
766892
}
484
485
static void
486
debug_vprintf(const char *fmt, va_list va)
487
{
488
	(void)printf("[%ld] ", (long)mypid);
489
	(void)vprintf(fmt, va);
490
	fflush(stdout);
491
}
492
493
void
494
debug_job_printf(const char *fmt, ...)
495
{
496
1021792
	if (DEBUG(JOB)) {
497
		va_list va;
498
		va_start(va, fmt);
499
		debug_vprintf(fmt, va);
500
		va_end(va);
501
	}
502
510896
}
503
504
static void
505
debug_kill_printf(const char *fmt, ...)
506
{
507
	if (DEBUG(KILL)) {
508
		va_list va;
509
		va_start(va, fmt);
510
		debug_vprintf(fmt, va);
511
		va_end(va);
512
	}
513
}
514
515
/*-
516
 *-----------------------------------------------------------------------
517
 * postprocess_job  --
518
 *	Do final processing for the given job including updating
519
 *	parents and starting new jobs as available/necessary.
520
 *
521
 * Side Effects:
522
 *	If we got an error and are aborting (aborting == ABORT_ERROR) and
523
 *	the job list is now empty, we are done for the day.
524
 *	If we recognized an error we set the aborting flag
525
 *	to ABORT_ERROR so no more jobs will be started.
526
 *-----------------------------------------------------------------------
527
 */
528
/*ARGSUSED*/
529
530
static void
531
postprocess_job(Job *job, bool okay)
532
{
533
772
	if (okay &&
534
386
	    aborting != ABORT_ERROR &&
535
386
	    aborting != ABORT_INTERRUPT) {
536
		/* As long as we aren't aborting and the job didn't return a
537
		 * non-zero status that we shouldn't ignore, we call
538
		 * Make_Update to update the parents. */
539
386
		job->node->built_status = MADE;
540
386
		Make_Update(job->node);
541
386
		free(job);
542
386
	}
543
544

386
	if (errorJobs != NULL && !keepgoing &&
545
	    aborting != ABORT_INTERRUPT)
546
		aborting = ABORT_ERROR;
547
548

386
	if (aborting == ABORT_ERROR && DEBUG(QUICKDEATH))
549
		handle_fatal_signal(SIGINT);
550

386
	if (aborting == ABORT_ERROR && Job_Empty())
551
		Finish();
552
386
}
553
554
/* expensive jobs handling: in order to avoid forking an exponential number
555
 * of jobs, make tries to figure out "recursive make" configurations.
556
 * It may err on the side of caution.
557
 * Basically, a command is "expensive" if it's likely to fork an extra
558
 * level of make: either by looking at the command proper, or if it has
559
 * some specific qualities ('+cmd' are likely to be recursive, as are
560
 * .MAKE: commands).  It's possible to explicitly say some targets are
561
 * expensive or cheap with .EXPENSIVE or .CHEAP.
562
 *
563
 * While an expensive command is running, no_new_jobs
564
 * is set, so jobs that would fork new processes are accumulated in the
565
 * heldJobs list instead.
566
 *
567
 * This heuristics is also used on error exit: we display silent commands
568
 * that failed, unless those ARE expensive commands: expensive commands
569
 * are likely to not be failing by themselves, but to be the result of
570
 * a cascade of failures in descendant makes.
571
 */
572
void
573
determine_expensive_job(Job *job)
574
{
575
3118
	if (expensive_job(job)) {
576
66
		job->flags |= JOB_IS_EXPENSIVE;
577
66
		no_new_jobs = true;
578
66
	} else
579
1493
		job->flags &= ~JOB_IS_EXPENSIVE;
580
1559
	if (DEBUG(EXPENSIVE))
581
		fprintf(stderr, "[%ld] Target %s running %.50s: %s\n",
582
		    (long)mypid, job->node->name, job->cmd,
583
		    job->flags & JOB_IS_EXPENSIVE ? "expensive" : "cheap");
584
1559
}
585
586
static bool
587
expensive_job(Job *job)
588
{
589
3118
	if (job->node->type & OP_CHEAP)
590
		return false;
591
1559
	if (job->node->type & (OP_EXPENSIVE | OP_MAKE))
592
		return true;
593
1559
	return expensive_command(job->cmd);
594
1559
}
595
596
static bool
597
expensive_command(const char *s)
598
{
599
	const char *p;
600
	bool include = false;
601
	bool expensive = false;
602
603
	/* okay, comments are cheap, always */
604
3118
	if (*s == '#')
605
		return false;
606
	/* and commands we always execute are expensive */
607
1559
	if (*s == '+')
608
		return true;
609
610
183674
	for (p = s; *p != '\0'; p++) {
611

168551
		if (*p == ' ' || *p == '\t') {
612
			include = false;
613

12626
			if (p[1] == '-' && p[2] == 'I')
614
				include = true;
615
		}
616
90344
		if (include)
617
			continue;
618
		/* KMP variant, avoid looking twice at the same
619
		 * letter.
620
		 */
621
90344
		if (*p != 'm')
622
			continue;
623
1725
		if (p[1] != 'a')
624
			continue;
625
1318
		p++;
626
1318
		if (p[1] != 'k')
627
			continue;
628
66
		p++;
629
66
		if (p[1] != 'e')
630
			continue;
631
66
		p++;
632
		expensive = true;
633

3550
		while (p[1] != '\0' && p[1] != ' ' && p[1] != '\t') {
634

1676
			if (p[1] == '.' || p[1] == '/') {
635
				expensive = false;
636
				break;
637
			}
638
838
		    	p++;
639
		}
640
66
		if (expensive)
641
66
			return true;
642
	}
643
1493
	return false;
644
1559
}
645
646
static Job *
647
prepare_job(GNode *gn)
648
{
649
	/* a new job is prepared unless its commands are bogus (we don't
650
	 * have anything for it), or if we're in touch mode.
651
	 *
652
	 * Note that even in noexec mode, some commands may still run
653
	 * thanks to the +cmd construct.
654
	 */
655
772
	if (node_find_valid_commands(gn)) {
656
386
		if (touchFlag) {
657
			Job_Touch(gn);
658
			return NULL;
659
		} else {
660
			Job *job;
661
662
386
			job = emalloc(sizeof(Job));
663
386
			if (job == NULL)
664
				Punt("can't create job: out of memory");
665
666
386
			job_attach_node(job, gn);
667
			return job;
668
		}
669
	} else {
670
		node_failure(gn);
671
		return NULL;
672
	}
673
386
}
674
675
static void
676
may_continue_job(Job *job)
677
{
678
2692
	if (no_new_jobs) {
679
		if (DEBUG(EXPENSIVE))
680
			fprintf(stderr, "[%ld] expensive -> hold %s\n",
681
			    (long)mypid, job->node->name);
682
		job->next = heldJobs;
683
		heldJobs = job;
684
	} else
685
1346
		continue_job(job);
686
1346
}
687
688
static void
689
continue_job(Job *job)
690
{
691
2692
	bool finished = job_run_next(job);
692
1346
	if (finished)
693
		remove_job(job, true);
694
	else
695
1346
		determine_expensive_job(job);
696
1346
}
697
698
/*-
699
 *-----------------------------------------------------------------------
700
 * Job_Make  --
701
 *	Start a target-creation process going for the target described
702
 *	by the graph node gn.
703
 *
704
 * Side Effects:
705
 *	A new Job node is created and  its commands continued, which
706
 *	may fork the first command of that job.
707
 *-----------------------------------------------------------------------
708
 */
709
void
710
Job_Make(GNode *gn)
711
{
712
	Job *job;
713
714
772
	job = prepare_job(gn);
715
386
	if (!job)
716
		return;
717
386
	nJobs++;
718
386
	may_continue_job(job);
719
772
}
720
721
static void
722
determine_job_next_step(Job *job)
723
{
724
	bool okay;
725
2692
	if (job->flags & JOB_IS_EXPENSIVE) {
726
		no_new_jobs = false;
727
		if (DEBUG(EXPENSIVE))
728
			fprintf(stderr, "[%ld] "
729
			    "Returning from expensive target %s, "
730
			    "allowing new jobs\n", (long)mypid,
731
			    job->node->name);
732
	}
733
734
1346
	okay = job->exit_type == JOB_EXIT_OKAY;
735

2692
	if (!okay || job->next_cmd == NULL)
736
386
		remove_job(job, okay);
737
	else
738
960
		may_continue_job(job);
739
1346
}
740
741
static void
742
remove_job(Job *job, bool okay)
743
{
744
386
	nJobs--;
745
386
	postprocess_job(job, okay);
746
1158
	while (!no_new_jobs) {
747
386
		if (heldJobs != NULL) {
748
			job = heldJobs;
749
			heldJobs = heldJobs->next;
750
			if (DEBUG(EXPENSIVE))
751
				fprintf(stderr, "[%ld] cheap -> release %s\n",
752
				    (long)mypid, job->node->name);
753
			continue_job(job);
754
		} else
755
			break;
756
	}
757
386
}
758
759
/*
760
 * job = reap_finished_job(pid):
761
 * 	retrieve and remove a job from runningJobs, based on its pid
762
 *
763
 *	Note that we remove it right away, so that handle_signals()
764
 *	is accurate.
765
 */
766
static Job *
767
reap_finished_job(pid_t pid)
768
{
769
	Job **j, *job;
770
771
4038
	for (j = &runningJobs; *j != NULL; j = &((*j)->next))
772
1346
		if ((*j)->pid == pid) {
773
			job = *j;
774
1346
			*j = job->next;
775
1346
			return job;
776
		}
777
778
	return NULL;
779
1346
}
780
781
/*
782
 * classic waitpid handler: retrieve as many dead children as possible.
783
 * returns true if succesful
784
 */
785
static bool
786
reap_jobs(void)
787
{
788
 	pid_t pid;	/* pid of dead child */
789
5380
 	int status;	/* Exit/termination status */
790
	bool reaped = false;
791
	Job *job;
792
793
6726
	while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0) {
794
		reaped = true;
795
1346
		job = reap_finished_job(pid);
796
797
1346
		if (job == NULL) {
798
			Punt("Child (%ld) not in table?", (long)pid);
799
		} else {
800
1346
			job_handle_status(job, status);
801
1346
			determine_job_next_step(job);
802
		}
803
	}
804
	/* sanity check, should not happen */
805

3076
	if (pid == -1 && errno == ECHILD && runningJobs != NULL)
806
		Punt("Process has no children, but runningJobs is not empty ?");
807
5380
	return reaped;
808
2690
}
809
810
void
811
handle_running_jobs(void)
812
{
813
2690
	sigset_t old;
814
	/* reaping children in the presence of caught signals */
815
816
	/* first, we make sure to hold on new signals, to synchronize
817
	 * reception of new stuff on sigsuspend
818
	 */
819
1345
	sigprocmask(SIG_BLOCK, &sigset, &old);
820
	/* note this will NOT loop until runningJobs == NULL.
821
	 * It's merely an optimisation, namely that we don't need to go
822
	 * through the logic if no job is present. As soon as a job
823
	 * gets reaped, we WILL exit the loop through the break.
824
	 */
825
5380
	while (runningJobs != NULL) {
826
		/* did we already have pending stuff that advances things ?
827
		 * then handle_all_signals() will not return
828
		 * or reap_jobs() will reap_jobs()
829
		 */
830
2690
		handle_all_signals();
831
2690
		if (reap_jobs())
832
			break;
833
		/* okay, so it's safe to suspend, we have nothing to do but
834
		 * wait...
835
		 */
836
1345
		sigsuspend(&emptyset);
837
	}
838
1345
	sigprocmask(SIG_SETMASK, &old, NULL);
839
1345
}
840
841
void
842
handle_one_job(Job *job)
843
{
844
	int stat;
845
508204
	int status;
846
254102
	sigset_t old;
847
848
254102
	sigprocmask(SIG_BLOCK, &sigset, &old);
849
507682
	while (1) {
850
507682
		handle_all_signals();
851
507682
		stat = waitpid(job->pid, &status, WNOHANG);
852
507682
		if (stat == job->pid)
853
			break;
854
253580
		sigsuspend(&emptyset);
855
	}
856
254102
	runningJobs = NULL;
857
254102
	job_handle_status(job, status);
858
254102
	sigprocmask(SIG_SETMASK, &old, NULL);
859
254102
}
860
861
static void
862
loop_handle_running_jobs()
863
{
864
3866
	while (runningJobs != NULL)
865
1345
		handle_running_jobs();
866
392
}
867
868
void
869
Job_Init(int maxproc)
870
{
871
68136
	runningJobs = NULL;
872
34068
	heldJobs = NULL;
873
34068
	errorJobs = NULL;
874
34068
	maxJobs = maxproc;
875
34068
	mypid = getpid();
876
877
34068
	nJobs = 0;
878
879
34068
	aborting = 0;
880
34068
	setup_all_signals();
881
34068
}
882
883
bool
884
can_start_job(void)
885
{
886
	if (aborting || nJobs >= maxJobs)
887
		return false;
888
	else
889
		return true;
890
}
891
892
bool
893
Job_Empty(void)
894
{
895
	return runningJobs == NULL;
896
}
897
898
/*-
899
 *-----------------------------------------------------------------------
900
 * handle_fatal_signal --
901
 *	Handle the receipt of a fatal interrupt
902
 *
903
 * Side Effects:
904
 *	All children are killed. Another job may be started if there
905
 *	is an interrupt target and the signal was SIGINT.
906
 *-----------------------------------------------------------------------
907
 */
908
static void
909
handle_fatal_signal(int signo)
910
{
911
	Job *job;
912
913
	debug_kill_printf("handle_fatal_signal(%d) called.\n", signo);
914
915
	dying_signal = signo;
916
	for (job = runningJobs; job != NULL; job = job->next) {
917
		debug_kill_printf("passing to "
918
		    "child %ld running %s: %s\n", (long)job->pid,
919
		    job->node->name, really_kill(job, signo));
920
		may_remove_target(job);
921
	}
922
923
	if (signo == SIGINT && !touchFlag) {
924
		if ((interrupt_node->type & OP_DUMMY) == 0) {
925
			ignoreErrors = false;
926
927
			Job_Make(interrupt_node);
928
		}
929
	}
930
	loop_handle_running_jobs();
931
	internal_print_errors();
932
933
	/* die by that signal */
934
	sigprocmask(SIG_BLOCK, &sigset, NULL);
935
	signal(signo, SIG_DFL);
936
	kill(getpid(), signo);
937
	sigprocmask(SIG_SETMASK, &emptyset, NULL);
938
	/*NOTREACHED*/
939
	fprintf(stderr, "This should never happen\n");
940
	exit(1);
941
}
942
943
/*
944
 *-----------------------------------------------------------------------
945
 * Job_Finish --
946
 *	Do final processing such as the running of the commands
947
 *	attached to the .END target.
948
 *
949
 *	return true if fatal errors have happened.
950
 *-----------------------------------------------------------------------
951
 */
952
bool
953
Job_Finish(void)
954
{
955
	bool problem = errorJobs != NULL;
956
957
	if ((end_node->type & OP_DUMMY) == 0) {
958
		if (problem) {
959
			Error("Errors reported so .END ignored");
960
		} else {
961
			Job_Make(end_node);
962
			loop_handle_running_jobs();
963
		}
964
	}
965
	return problem;
966
}
967
968
void
969
Job_Begin(void)
970
{
971
68136
	if ((begin_node->type & OP_DUMMY) == 0) {
972
386
		Job_Make(begin_node);
973
386
		loop_handle_running_jobs();
974
386
	}
975
34068
}
976
977
/*-
978
 *-----------------------------------------------------------------------
979
 * Job_Wait --
980
 *	Waits for all running jobs to finish and returns. Sets 'aborting'
981
 *	to ABORT_WAIT to prevent other jobs from starting.
982
 *
983
 * Side Effects:
984
 *	Currently running jobs finish.
985
 *
986
 *-----------------------------------------------------------------------
987
 */
988
void
989
Job_Wait(void)
990
{
991
12
	aborting = ABORT_WAIT;
992
6
	loop_handle_running_jobs();
993
6
	aborting = 0;
994
6
}
995
996
/*-
997
 *-----------------------------------------------------------------------
998
 * Job_AbortAll --
999
 *	Abort all currently running jobs without handling output or anything.
1000
 *	This function is to be called only in the event of a major
1001
 *	error.
1002
 *
1003
 * Side Effects:
1004
 *	All children are killed
1005
 *-----------------------------------------------------------------------
1006
 */
1007
void
1008
Job_AbortAll(void)
1009
{
1010
	Job *job;	/* the job descriptor in that element */
1011
18
	int foo;
1012
1013
9
	aborting = ABORT_ERROR;
1014
1015
18
	for (job = runningJobs; job != NULL; job = job->next) {
1016
		killpg(job->pid, SIGINT);
1017
		killpg(job->pid, SIGKILL);
1018
	}
1019
1020
	/*
1021
	 * Catch as many children as want to report in at first, then give up
1022
	 */
1023
9
	while (waitpid(WAIT_ANY, &foo, WNOHANG) > 0)
1024
		continue;
1025
9
}