GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/make/engine.c Lines: 215 342 62.9 %
Date: 2017-11-07 Branches: 162 299 54.2 %

Line Branch Exec Source
1
/*	$OpenBSD: engine.c,v 1.54 2017/07/24 12:08:15 espie Exp $ */
2
/*
3
 * Copyright (c) 2012 Marc Espie.
4
 *
5
 * Extensive code modifications for the OpenBSD project.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
17
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
 * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENBSD
20
 * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
/*
29
 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
30
 * Copyright (c) 1988, 1989 by Adam de Boor
31
 * Copyright (c) 1989 by Berkeley Softworks
32
 * All rights reserved.
33
 *
34
 * This code is derived from software contributed to Berkeley by
35
 * Adam de Boor.
36
 *
37
 * Redistribution and use in source and binary forms, with or without
38
 * modification, are permitted provided that the following conditions
39
 * are met:
40
 * 1. Redistributions of source code must retain the above copyright
41
 *    notice, this list of conditions and the following disclaimer.
42
 * 2. Redistributions in binary form must reproduce the above copyright
43
 *    notice, this list of conditions and the following disclaimer in the
44
 *    documentation and/or other materials provided with the distribution.
45
 * 3. Neither the name of the University nor the names of its contributors
46
 *    may be used to endorse or promote products derived from this software
47
 *    without specific prior written permission.
48
 *
49
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59
 * SUCH DAMAGE.
60
 */
61
62
#include <sys/types.h>
63
#include <sys/time.h>
64
#include <sys/wait.h>
65
#include <assert.h>
66
#include <ctype.h>
67
#include <errno.h>
68
#include <fcntl.h>
69
#include <limits.h>
70
#include <signal.h>
71
#include <stdint.h>
72
#include <stdio.h>
73
#include <stdlib.h>
74
#include <string.h>
75
#include <unistd.h>
76
#include "config.h"
77
#include "defines.h"
78
#include "dir.h"
79
#include "engine.h"
80
#include "arch.h"
81
#include "gnode.h"
82
#include "targ.h"
83
#include "var.h"
84
#include "extern.h"
85
#include "lst.h"
86
#include "timestamp.h"
87
#include "make.h"
88
#include "pathnames.h"
89
#include "error.h"
90
#include "str.h"
91
#include "memory.h"
92
#include "buf.h"
93
#include "job.h"
94
#include "lowparse.h"
95
96
static void MakeTimeStamp(void *, void *);
97
static int rewrite_time(const char *);
98
static void setup_meta(void);
99
static void setup_engine(void);
100
static char **recheck_command_for_shell(char **);
101
static void list_parents(GNode *, FILE *);
102
103
/* XXX due to a bug in make's logic, targets looking like *.a or -l*
104
 * have been silently dropped when make couldn't figure them out.
105
 * Now, we warn about them until all Makefile bugs have been fixed.
106
 */
107
static bool
108
drop_silently(const char *s)
109
{
110
	size_t len;
111
112

12
	if (s[0] == '-' && s[1] == 'l')
113
		return true;
114
115
6
	len = strlen(s);
116

12
	if (len >=2 && s[len-2] == '.' && s[len-1] == 'a')
117
		return true;
118
6
	return false;
119
6
}
120
121
bool
122
node_find_valid_commands(GNode *gn)
123
{
124
	/* Alter our type to tell if errors should be ignored or things
125
	 * should not be printed so setup_and_run_command knows what to do.
126
	 */
127
246834
	if (Targ_Ignore(gn))
128
		gn->type |= OP_IGNORE;
129
123417
	if (Targ_Silent(gn))
130
3967
		gn->type |= OP_SILENT;
131
132

123417
	if (DEBUG(DOUBLE) && (gn->type & OP_DOUBLE))
133
		fprintf(stderr, "Warning: target %s had >1 lists of "
134
		    "shell commands (ignoring later ones)\n", gn->name);
135

146017
	if (OP_NOP(gn->type) && Lst_IsEmpty(&gn->commands)) {
136
6
		if (drop_silently(gn->name)) {
137
			printf("Warning: target %s", gn->name);
138
			list_parents(gn, stdout);
139
			printf(" does not have any command (BUG)\n");
140
			return true;
141
		}
142
		/*
143
		 * No commands. Look for .DEFAULT rule from which we might infer
144
		 * commands
145
		 */
146

6
		if ((gn->type & OP_NODEFAULT) == 0 &&
147
6
		    (DEFAULT->type & OP_DUMMY) == 0 &&
148
		    !Lst_IsEmpty(&DEFAULT->commands)) {
149
			/*
150
			 * Make only looks for a .DEFAULT if the node was never
151
			 * the target of an operator, so that's what we do too.
152
			 * If a .DEFAULT was given, we substitute its commands
153
			 * for gn's commands and set the IMPSRC variable to be
154
			 * the target's name The DEFAULT node acts like a
155
			 * transformation rule, in that gn also inherits any
156
			 * attributes or sources attached to .DEFAULT itself.
157
			 */
158
			Make_HandleUse(DEFAULT, gn);
159
			Var(IMPSRC_INDEX, gn) = Var(TARGET_INDEX, gn);
160

12
		} else if (is_out_of_date(Dir_MTime(gn))) {
161
			/*
162
			 * The node wasn't the target of an operator we have no
163
			 * .DEFAULT rule to go on and the target doesn't
164
			 * already exist. There's nothing more we can do for
165
			 * this branch.
166
			 */
167
6
			 return false;
168
	    	}
169
	}
170
123411
	return true;
171
123417
}
172
173
static void
174
list_parents(GNode *gn, FILE *out)
175
{
176
	LstNode ln;
177
	bool first = true;
178
179
18
	for (ln = Lst_First(&gn->parents); ln != NULL; ln = Lst_Adv(ln)) {
180
		GNode *p = Lst_Datum(ln);
181
		if (!p->must_make)
182
			continue;
183
		if (first) {
184
			fprintf(out, " (prerequisite of:");
185
			first = false;
186
		}
187
		fprintf(out, " %s", p->name);
188
	}
189
6
	if (!first)
190
		fprintf(out, ")");
191
6
}
192
193
void
194
node_failure(GNode *gn)
195
{
196
	/*
197
	 If the -k flag wasn't given, we stop in
198
	 * our tracks, otherwise we just don't update this
199
	 * node's parents so they never get examined.
200
	 */
201
	const char *diag;
202
	FILE *out;
203
204
12
	if (gn->type & OP_OPTIONAL) {
205
		out = stdout;
206
		diag = "(ignored)";
207
6
	} else if (keepgoing) {
208
		out = stdout;
209
		diag = "(continuing)";
210
	} else {
211
		out = stderr;
212
		diag = "";
213
	}
214
6
	fprintf(out, "make: don't know how to make %s", gn->name);
215
6
	list_parents(gn, out);
216
6
	fprintf(out, "%s\n", diag);
217
6
	if (out == stdout)
218
		fflush(stdout);
219
	else {
220
6
		print_errors();
221
6
		Punt(NULL);
222
	}
223
}
224
225
/* touch files the hard way, by writing stuff to them */
226
static int
227
rewrite_time(const char *name)
228
{
229
	int fd;
230
	char c;
231
232
	fd = open(name, O_RDWR | O_CREAT, 0666);
233
	if (fd < 0)
234
		return -1;
235
	/*
236
	 * Read and write a byte to the file to change
237
	 * the modification time.
238
	 */
239
	if (read(fd, &c, 1) == 1) {
240
		(void)lseek(fd, 0, SEEK_SET);
241
		(void)write(fd, &c, 1);
242
	}
243
244
	(void)close(fd);
245
	return 0;
246
}
247
248
void
249
Job_Touch(GNode *gn)
250
{
251
	handle_all_signals();
252
	if (gn->type & (OP_JOIN|OP_USE|OP_EXEC|OP_OPTIONAL|OP_PHONY)) {
253
		/*
254
		 * .JOIN, .USE, and .OPTIONAL targets are "virtual" targets
255
		 * and, as such, shouldn't really be created.
256
		 * Likewise, .PHONY targets are not really files
257
		 */
258
		return;
259
	}
260
261
	if (!(gn->type & OP_SILENT)) {
262
		(void)fprintf(stdout, "touch %s\n", gn->name);
263
		(void)fflush(stdout);
264
	}
265
266
	if (noExecute) {
267
		return;
268
	}
269
270
	if (gn->type & OP_ARCHV) {
271
		Arch_Touch(gn);
272
	} else {
273
		const char *file = gn->path != NULL ? gn->path : gn->name;
274
275
		if (set_times(file) == -1){
276
			if (rewrite_time(file) == -1) {
277
				(void)fprintf(stderr,
278
				    "*** couldn't touch %s: %s", file,
279
				    strerror(errno));
280
		    	}
281
		}
282
	}
283
}
284
285
void
286
Make_TimeStamp(GNode *parent, GNode *child)
287
{
288

18423477
	if (is_strictly_before(parent->youngest->mtime, child->mtime)) {
289
1194358
 		parent->youngest = child;
290
1194358
	}
291
6141159
}
292
293
void
294
Make_HandleUse(GNode	*cgn,	/* The .USE node */
295
    GNode	*pgn)	/* The target of the .USE node */
296
{
297
	GNode	*gn;	/* A child of the .USE node */
298
	LstNode	ln;	/* An element in the children list */
299
300
301
256654
	assert(cgn->type & (OP_USE|OP_TRANSFORM));
302
303
128327
	if (pgn == NULL)
304
		Fatal("Trying to apply .USE to '%s' without a parent",
305
		    cgn->name);
306
307

234390
	if ((cgn->type & OP_USE) || Lst_IsEmpty(&pgn->commands)) {
308
		/* .USE or transformation and target has no commands
309
		 * -- append the child's commands to the parent.  */
310
128104
		Lst_Concat(&pgn->commands, &cgn->commands);
311
128104
	}
312
313
256654
	for (ln = Lst_First(&cgn->children); ln != NULL;
314
	    ln = Lst_Adv(ln)) {
315
		gn = Lst_Datum(ln);
316
317
		if (Lst_AddNew(&pgn->children, gn)) {
318
			Lst_AtEnd(&gn->parents, pgn);
319
			pgn->unmade++;
320
		}
321
	}
322
323

128327
	if (DEBUG(DOUBLE) && (cgn->type & OP_DOUBLE))
324
		fprintf(stderr,
325
		    "Warning: .USE %s expanded in %s had >1 lists of "
326
		    "shell commands (ignoring later ones)\n",
327
		    cgn->name, pgn->name);
328
128327
	pgn->type |= cgn->type & ~(OP_OPMASK|OP_USE|OP_TRANSFORM|OP_DOUBLE);
329
330
	/*
331
	 * This child node is now "made", so we decrement the count of
332
	 * unmade children in the parent... We also remove the child
333
	 * from the parent's list to accurately reflect the number of
334
	 * decent children the parent has. This is used by Make_Run to
335
	 * decide whether to queue the parent or examine its children...
336
	 */
337
128327
	if (cgn->type & OP_USE)
338
22264
		pgn->unmade--;
339
128327
}
340
341
void
342
Make_DoAllVar(GNode *gn)
343
{
344
	GNode *child;
345
	LstNode ln;
346
246062
	BUFFER allsrc, oodate;
347
	char *target;
348
	bool do_oodate;
349
	int oodate_count, allsrc_count = 0;
350
351
	oodate_count = 0;
352
	allsrc_count = 0;
353
354
123031
	Var(OODATE_INDEX, gn) = "";
355
123031
	Var(ALLSRC_INDEX, gn) = "";
356
357
1260046
	for (ln = Lst_First(&gn->children); ln != NULL; ln = Lst_Adv(ln)) {
358
506992
		child = Lst_Datum(ln);
359
506992
		if ((child->type & (OP_EXEC|OP_USE|OP_INVISIBLE)) != 0)
360
			continue;
361

863762
		if (OP_NOP(child->type) ||
362
379027
		    (target = Var(TARGET_INDEX, child)) == NULL) {
363
			/*
364
			 * this node is only source; use the specific pathname
365
			 * for it
366
			 */
367
317124
			target = child->path != NULL ? child->path :
368
			    child->name;
369
105708
		}
370
371
		/*
372
		 * It goes in the OODATE variable if the parent is younger than
373
		 * the child or if the child has been modified more recently
374
		 * than the start of the make.  This is to keep make from
375
		 * getting confused if something else updates the parent after
376
		 * the make starts (shouldn't happen, I know, but sometimes it
377
		 * does). In such a case, if we've updated the kid, the parent
378
		 * is likely to have a modification time later than that of the
379
		 * kid and anything that relies on the OODATE variable will be
380
		 * hosed.
381
		 */
382
		do_oodate = false;
383
484735
		if (gn->type & OP_JOIN) {
384
			if (child->built_status == MADE)
385
				do_oodate = true;
386


969475
		} else if (is_strictly_before(gn->mtime, child->mtime) ||
387

402464
		   (!is_strictly_before(child->mtime, starttime) &&
388
5
		   child->built_status == MADE))
389
283508
		   	do_oodate = true;
390
484735
		if (do_oodate) {
391
283508
			oodate_count++;
392
283508
			if (oodate_count == 1)
393
88466
				Var(OODATE_INDEX, gn) = target;
394
			else {
395
195042
				if (oodate_count == 2) {
396
30748
					Buf_Init(&oodate, 0);
397
30748
					Buf_AddString(&oodate,
398
					    Var(OODATE_INDEX, gn));
399
30748
				}
400
390802
				Buf_AddSpace(&oodate);
401
195042
				Buf_AddString(&oodate, target);
402
			}
403
		}
404
484735
		allsrc_count++;
405
484735
		if (allsrc_count == 1)
406
88466
			Var(ALLSRC_INDEX, gn) = target;
407
		else {
408
396269
			if (allsrc_count == 2) {
409
31802
				Buf_Init(&allsrc, 0);
410
31802
				Buf_AddString(&allsrc,
411
				    Var(ALLSRC_INDEX, gn));
412
31802
			}
413
793524
			Buf_AddSpace(&allsrc);
414
396269
			Buf_AddString(&allsrc, target);
415
		}
416
	}
417
418
123031
	if (allsrc_count > 1)
419
31802
		Var(ALLSRC_INDEX, gn) = Buf_Retrieve(&allsrc);
420
123031
	if (oodate_count > 1)
421
30748
		Var(OODATE_INDEX, gn) = Buf_Retrieve(&oodate);
422
423
123031
	if (gn->impliedsrc)
424
43556
		Var(IMPSRC_INDEX, gn) = Var(TARGET_INDEX, gn->impliedsrc);
425
426
123031
	if (gn->type & OP_JOIN)
427
		Var(TARGET_INDEX, gn) = Var(ALLSRC_INDEX, gn);
428
123031
}
429
430
/* Wrapper to call Make_TimeStamp from a forEach loop.	*/
431
static void
432
MakeTimeStamp(void *parent, void *child)
433
{
434
7757932
    Make_TimeStamp(parent, child);
435
3878966
}
436
437
bool
438
Make_OODate(GNode *gn)
439
{
440
	bool	    oodate;
441
442
	/*
443
	 * Certain types of targets needn't even be sought as their datedness
444
	 * doesn't depend on their modification time...
445
	 */
446
1173234
	if ((gn->type & (OP_JOIN|OP_USE|OP_EXEC|OP_PHONY)) == 0) {
447
519596
		(void)Dir_MTime(gn);
448
519596
		if (DEBUG(MAKE)) {
449
			if (!is_out_of_date(gn->mtime))
450
				printf("modified %s...",
451
				    time_to_string(&gn->mtime));
452
			else
453
				printf("non-existent...");
454
		}
455
	}
456
457
	/*
458
	 * A target is remade in one of the following circumstances:
459
	 * - its modification time is smaller than that of its youngest child
460
	 *   and it would actually be run (has commands or type OP_NOP)
461
	 * - it's the object of a force operator
462
	 * - it has no children, was on the lhs of an operator and doesn't
463
	 *   exist already.
464
	 *
465
	 */
466
586617
	if (gn->type & OP_USE) {
467
		/*
468
		 * If the node is a USE node it is *never* out of date
469
		 * no matter *what*.
470
		 */
471
		if (DEBUG(MAKE))
472
			printf(".USE node...");
473
		oodate = false;
474
586617
	} else if (gn->type & OP_JOIN) {
475
		/*
476
		 * A target with the .JOIN attribute is only considered
477
		 * out-of-date if any of its children was out-of-date.
478
		 */
479
		if (DEBUG(MAKE))
480
			printf(".JOIN node...");
481
		oodate = gn->childMade;
482
586617
	} else if (gn->type & (OP_FORCE|OP_EXEC|OP_PHONY)) {
483
		/*
484
		 * A node which is the object of the force (!) operator or which
485
		 * has the .EXEC attribute is always considered out-of-date.
486
		 */
487
67021
		if (DEBUG(MAKE)) {
488
			if (gn->type & OP_FORCE)
489
				printf("! operator...");
490
			else if (gn->type & OP_PHONY)
491
				printf(".PHONY node...");
492
			else
493
				printf(".EXEC node...");
494
		}
495
		oodate = true;
496


1491392
	} else if (is_strictly_before(gn->mtime, gn->youngest->mtime) ||
497
469158
	   (gn == gn->youngest &&
498

781502
	    (is_out_of_date(gn->mtime) || (gn->type & OP_DOUBLEDEP)))) {
499
		/*
500
		 * A node whose modification time is less than that of its
501
		 * youngest child or that has no children (gn->youngest == gn)
502
		 * and either doesn't exist (mtime == OUT_OF_DATE)
503
		 * or was the object of a :: operator is out-of-date.
504
		 */
505
56010
		if (DEBUG(MAKE)) {
506
			if (is_strictly_before(gn->mtime, gn->youngest->mtime))
507
				printf("modified before source(%s)...",
508
				    gn->youngest->name);
509
			else if (is_out_of_date(gn->mtime))
510
				printf("non-existent and no sources...");
511
			else
512
				printf(":: operator and no sources...");
513
		}
514
		oodate = true;
515
56010
	} else {
516
		oodate = false;
517
	}
518
519
	/*
520
	 * If the target isn't out-of-date, the parents need to know its
521
	 * modification time. Note that targets that appear to be out-of-date
522
	 * but aren't, because they have no commands and aren't of type OP_NOP,
523
	 * have their mtime stay below their children's mtime to keep parents
524
	 * from thinking they're out-of-date.
525
	 */
526
586617
	if (!oodate)
527
463586
		Lst_ForEach(&gn->parents, MakeTimeStamp, gn);
528
529
586617
	return oodate;
530
}
531
532
/* The following array is used to make a fast determination of which
533
 * characters are interpreted specially by the shell.  If a command
534
 * contains any of these characters, it is executed by the shell, not
535
 * directly by us.  */
536
static char	    meta[256];
537
538
void
539
setup_meta(void)
540
{
541
	char *p;
542
543
1595650
	for (p = "#=|^(){};&<>*?[]:$`\\\n~"; *p != '\0'; p++)
544
746900
		meta[(unsigned char) *p] = 1;
545
	/* The null character serves as a sentinel in the string.  */
546
33950
	meta[0] = 1;
547
33950
}
548
549
static char **
550
recheck_command_for_shell(char **av)
551
{
552
	char *runsh[] = {
553
		"!", "alias", "cd", "eval", "exit", "read", "set", "ulimit",
554
		"unalias", "unset", "wait", "umask", NULL
555
	};
556
557
	char **p;
558
559
	/* optimization: if exec cmd, we avoid the intermediate shell */
560
	if (strcmp(av[0], "exec") == 0)
561
		av++;
562
563
	for (p = runsh; *p; p++)
564
		if (strcmp(av[0], *p) == 0)
565
			return NULL;
566
567
	return av;
568
}
569
570
static void
571
run_command(const char *cmd, bool errCheck)
572
{
573
	const char *p;
574
	char *shargv[4];
575
	char **todo;
576
577
	shargv[0] = _PATH_BSHELL;
578
579
	shargv[1] = errCheck ? "-ec" : "-c";
580
	shargv[2] = (char *)cmd;
581
	shargv[3] = NULL;
582
583
	todo = shargv;
584
585
586
	/* Search for meta characters in the command. If there are no meta
587
	 * characters, there's no need to execute a shell to execute the
588
	 * command.  */
589
	for (p = cmd; !meta[(unsigned char)*p]; p++)
590
		continue;
591
	if (*p == '\0') {
592
		char *bp;
593
		char **av;
594
		int argc;
595
		/* No meta-characters, so probably no need to exec a shell.
596
		 * Break the command into words to form an argument vector
597
		 * we can execute.  */
598
		av = brk_string(cmd, &argc, &bp);
599
		av = recheck_command_for_shell(av);
600
		if (av != NULL)
601
			todo = av;
602
	}
603
	execvp(todo[0], todo);
604
605
	if (errno == ENOENT)
606
		fprintf(stderr, "%s: not found\n", todo[0]);
607
	else
608
		perror(todo[0]);
609
	_exit(1);
610
}
611
612
static Job myjob;
613
614
void
615
job_attach_node(Job *job, GNode *node)
616
{
617
247648
	job->node = node;
618
123824
	job->node->built_status = BUILDING;
619
123824
	job->next_cmd = Lst_First(&node->commands);
620
123824
	job->exit_type = JOB_EXIT_OKAY;
621
123824
	job->location = NULL;
622
123824
	job->flags = 0;
623
123824
}
624
625
void
626
job_handle_status(Job *job, int status)
627
{
628
	bool silent;
629
	int dying;
630
631
	/* if there's one job running and we don't keep going, no need
632
	 * to report right now.
633
	 */
634

721426
	if ((job->flags & JOB_ERRCHECK) && !keepgoing && runningJobs == NULL)
635
210530
		silent = !DEBUG(JOB);
636
	else
637
		silent = false;
638
639
255448
	debug_job_printf("Process %ld (%s) exited with status %d.\n",
640
255448
	    (long)job->pid, job->node->name, status);
641
642
	/* classify status */
643
255448
	if (WIFEXITED(status)) {
644
255447
		job->code = WEXITSTATUS(status);/* exited */
645
255447
		if (job->code != 0) {
646
			/* if we're already dying from that signal, be silent */
647

2005
			if (!silent && job->code > 128
648
631
			    && job->code <= 128 + _NSIG) {
649
				dying = check_dying_signal();
650
				silent = dying && job->code == dying + 128;
651
			}
652
1374
			if (!silent)
653
631
				printf("*** Error %d", job->code);
654
			job->exit_type = JOB_EXIT_BAD;
655
1374
		} else
656
			job->exit_type = JOB_EXIT_OKAY;
657
255447
	} else {
658
1
		job->exit_type = JOB_SIGNALED;
659
1
		job->code = WTERMSIG(status);	/* signaled */
660
		/* if we're already dying from that signal, be silent */
661
1
		if (!silent) {
662
			dying = check_dying_signal();
663
			silent = dying && job->code == dying;
664
		}
665
1
		if (!silent)
666
			printf("*** Signal %d", job->code);
667
	}
668
669
	/* if there is a problem, what's going on ? */
670
255448
	if (job->exit_type != JOB_EXIT_OKAY) {
671
1375
		if (!silent)
672
631
			printf(" in target '%s'", job->node->name);
673
1375
		if (job->flags & JOB_ERRCHECK) {
674
744
			job->node->built_status = ERROR;
675
			/* compute expensive status if we really want it */
676
744
			if ((job->flags & JOB_SILENT) && job == &myjob)
677
213
				determine_expensive_job(job);
678
744
			if (!keepgoing) {
679
744
				if (!silent)
680
					printf("\n");
681
744
				job->next = errorJobs;
682
744
				errorJobs = job;
683
				/* XXX don't free the command */
684
744
				return;
685
			}
686
			printf(", line %lu of %s", job->location->lineno,
687
			    job->location->fname);
688
			if ((job->flags & (JOB_SILENT | JOB_IS_EXPENSIVE))
689
			    == JOB_SILENT)
690
				printf(": %s", job->cmd);
691
			/* Abort the current target,
692
			 * but let others continue.  */
693
			printf(" (continuing)\n");
694
		} else {
695
			/* Continue executing commands for
696
			 * this target.  If we return 0,
697
			 * this will happen...  */
698
631
			printf(" (ignored)\n");
699
631
			job->exit_type = JOB_EXIT_OKAY;
700
		}
701
	}
702
254704
	free(job->cmd);
703
510152
}
704
705
int
706
run_gnode(GNode *gn)
707
{
708

469038
	if (!gn || (gn->type & OP_DUMMY))
709
32908
		return NOSUCHNODE;
710
711
123438
	gn->built_status = MADE;
712
713
123438
	job_attach_node(&myjob, gn);
714

500228
	while (myjob.exit_type == JOB_EXIT_OKAY) {
715
376790
		bool finished = job_run_next(&myjob);
716
376790
		if (finished)
717
122688
			break;
718
254102
		handle_one_job(&myjob);
719
254102
	}
720
721
123432
	return gn->built_status;
722
156340
}
723
724
725
static void
726
setup_engine(void)
727
{
728
	static int already_setup = 0;
729
730
756284
	if (!already_setup) {
731
33950
		setup_meta();
732
33950
		already_setup = 1;
733
33950
	}
734
378142
}
735
736
static bool
737
do_run_command(Job *job, const char *pre)
738
{
739
	bool silent;	/* Don't print command */
740
	bool doExecute;	/* Execute the command */
741
	bool errCheck;	/* Check errors */
742
	pid_t cpid; 	/* Child pid */
743
744
511546
	const char *cmd = job->cmd;
745
255773
	silent = job->node->type & OP_SILENT;
746
255773
	errCheck = !(job->node->type & OP_IGNORE);
747
255773
	if (job->node->type & OP_MAKE)
748
		doExecute = true;
749
	else
750
255773
		doExecute = !noExecute;
751
752
	/* How can we execute a null command ? we warn the user that the
753
	 * command expanded to nothing (is this the right thing to do?).  */
754
255773
	if (*cmd == '\0') {
755
		Parse_Error(PARSE_WARNING,
756
		    "'%s' expands to '' while building %s",
757
		    pre, job->node->name);
758
		return false;
759
	}
760
761
238870
	for (;; cmd++) {
762
494643
		if (*cmd == '@')
763
193952
			silent = DEBUG(LOUD) ? false : true;
764
300691
		else if (*cmd == '-')
765
44918
			errCheck = false;
766
255773
		else if (*cmd == '+')
767
			doExecute = true;
768
		else
769
			break;
770
	}
771
265615
	while (ISSPACE(*cmd))
772
4921
		cmd++;
773
	/* Print the command before fork if make -n or !silent*/
774

511546
	if ( noExecute || !silent)
775
38035
		printf("%s\n", cmd);
776
777
511546
	if (silent)
778
255773
		job->flags |= JOB_SILENT;
779
	else
780
255773
		job->flags &= ~JOB_SILENT;
781
782
	/* If we're not supposed to execute any commands, this is as far as
783
	 * we go...  */
784
255773
	if (!doExecute)
785
		return false;
786
	/* always flush for other stuff */
787
255773
	fflush(stdout);
788
789
	/* Optimization: bypass comments entirely */
790
255773
	if (*cmd == '#')
791
325
		return false;
792
793
	/* Fork and execute the single command. If the fork fails, we abort.  */
794
255448
	switch (cpid = fork()) {
795
	case -1:
796
		Punt("Could not fork");
797
		/*NOTREACHED*/
798
	case 0:
799
		/* put a random delay unless we're the only job running
800
		 * and there's nothing left to do.
801
		 */
802
		if (random_delay)
803
			if (!(runningJobs == NULL && no_jobs_left()))
804
				usleep(arc4random_uniform(random_delay));
805
		run_command(cmd, errCheck);
806
		/*NOTREACHED*/
807
	default:
808
255448
		job->pid = cpid;
809
255448
		job->next = runningJobs;
810
255448
		runningJobs = job;
811
510896
		if (errCheck)
812
255448
			job->flags |= JOB_ERRCHECK;
813
		else
814
255448
			job->flags &= ~JOB_ERRCHECK;
815
510896
		debug_job_printf("Running %ld (%s) %s\n", (long)job->pid,
816

984081
		    job->node->name, (noExecute || !silent) ? "" : cmd);
817
255448
		return true;
818
	}
819
255773
}
820
821
bool
822
job_run_next(Job *job)
823
{
824
	bool started;
825
756284
	GNode *gn = job->node;
826
827
378142
	setup_engine();
828
756609
	while (job->next_cmd != NULL) {
829
255773
		struct command *command = Lst_Datum(job->next_cmd);
830
831
255773
		handle_all_signals();
832
255773
		job->location = &command->location;
833
255773
		Parse_SetLocation(job->location);
834
255773
		job->cmd = Var_Subst(command->string, &gn->context, false);
835
255773
		job->next_cmd = Lst_Adv(job->next_cmd);
836
255773
		if (fatal_errors)
837
			Punt(NULL);
838
255773
		started = do_run_command(job, command->string);
839
255773
		if (started)
840
255448
			return false;
841
		else
842
325
			free(job->cmd);
843
325
	}
844
122688
	job->exit_type = JOB_EXIT_OKAY;
845
122688
	return true;
846
378136
}
847