GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/make/engine.c Lines: 209 332 63.0 %
Date: 2016-12-06 Branches: 151 301 50.2 %

Line Branch Exec Source
1
/*	$OpenBSD: engine.c,v 1.50 2015/01/23 13:18:40 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
	if (s[0] == '-' && s[1] == 'l')
113
		return true;
114
115
	len = strlen(s);
116
	if (len >=2 && s[len-2] == '.' && s[len-1] == 'a')
117
		return true;
118
	return false;
119
}
120
121
bool
122
node_find_valid_commands(GNode *gn)
123
2898
{
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
2898
	if (Targ_Ignore(gn))
128
		gn->type |= OP_IGNORE;
129
2898
	if (Targ_Silent(gn))
130
123
		gn->type |= OP_SILENT;
131
132

2898
	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

2898
	if (OP_NOP(gn->type) && Lst_IsEmpty(&gn->commands)) {
136
		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
		if ((gn->type & OP_NODEFAULT) == 0 &&
147
		    (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
		} 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
			 return false;
168
	    	}
169
	}
170
2898
	return true;
171
}
172
173
static void
174
list_parents(GNode *gn, FILE *out)
175
{
176
	LstNode ln;
177
	bool first = true;
178
179
	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
	if (!first)
190
		fprintf(out, ")");
191
}
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
	if (gn->type & OP_OPTIONAL) {
205
		out = stdout;
206
		diag = "(ignored)";
207
	} else if (keepgoing) {
208
		out = stdout;
209
		diag = "(continuing)";
210
	} else {
211
		out = stderr;
212
		diag = "";
213
	}
214
	fprintf(out, "make: don't know how to make %s", gn->name);
215
	list_parents(gn, out);
216
	fprintf(out, "%s\n", diag);
217
	if (out == stdout)
218
		fflush(stdout);
219
	else {
220
		print_errors();
221
		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
11112
{
288

11112
	if (is_strictly_before(parent->youngest->mtime, child->mtime)) {
289
7424
 		parent->youngest = child;
290
	}
291
11112
}
292
293
void
294
Make_HandleUse(GNode	*cgn,	/* The .USE node */
295
    GNode	*pgn)	/* The target of the .USE node */
296
1797
{
297
	GNode	*gn;	/* A child of the .USE node */
298
	LstNode	ln;	/* An element in the children list */
299
300
301
1797
	assert(cgn->type & (OP_USE|OP_TRANSFORM));
302
303

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

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

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



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



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

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

68
			if (!silent && job->code > 128
644
			    && job->code <= 128 + _NSIG) {
645
				dying = check_dying_signal();
646
				silent = dying && job->code == dying + 128;
647
			}
648
68
			if (!silent)
649
26
				printf("*** Error %d", job->code);
650
68
			job->exit_type = JOB_EXIT_BAD;
651
		} else
652
3382
			job->exit_type = JOB_EXIT_OKAY;
653
	} else {
654
		job->exit_type = JOB_SIGNALED;
655
		job->code = WTERMSIG(status);	/* signaled */
656
		/* if we're already dying from that signal, be silent */
657
		if (!silent) {
658
			dying = check_dying_signal();
659
			silent = dying && job->code == dying;
660
		}
661
		if (!silent)
662
			printf("*** Signal %d", job->code);
663
	}
664
665
	/* if there is a problem, what's going on ? */
666
3450
	if (job->exit_type != JOB_EXIT_OKAY) {
667
68
		if (!silent)
668
26
			printf(" in target '%s'", job->node->name);
669
68
		if (job->flags & JOB_ERRCHECK) {
670
42
			job->node->built_status = ERROR;
671
			/* compute expensive status if we really want it */
672

42
			if ((job->flags & JOB_SILENT) && job == &myjob)
673
32
				determine_expensive_job(job);
674
42
			if (!keepgoing) {
675
42
				if (!silent)
676
					printf("\n");
677
42
				job->next = errorJobs;
678
42
				errorJobs = job;
679
				/* XXX don't free the command */
680
42
				return;
681
			}
682
			printf(", line %lu of %s", job->location->lineno,
683
			    job->location->fname);
684
			if ((job->flags & (JOB_SILENT | JOB_IS_EXPENSIVE))
685
			    == JOB_SILENT)
686
				printf(": %s", job->cmd);
687
			/* Abort the current target,
688
			 * but let others continue.  */
689
			printf(" (continuing)\n");
690
		} else {
691
			/* Continue executing commands for
692
			 * this target.  If we return 0,
693
			 * this will happen...  */
694
26
			printf(" (ignored)\n");
695
26
			job->exit_type = JOB_EXIT_OKAY;
696
		}
697
	}
698
3408
	free(job->cmd);
699
}
700
701
int
702
run_gnode(GNode *gn)
703
3585
{
704

3585
	if (!gn || (gn->type & OP_DUMMY))
705
687
		return NOSUCHNODE;
706
707
2898
	gn->built_status = MADE;
708
709
2898
	job_attach_node(&myjob, gn);
710
9246
	while (myjob.exit_type == JOB_EXIT_OKAY) {
711
6306
		bool finished = job_run_next(&myjob);
712
6306
		if (finished)
713
2856
			break;
714
3450
		handle_one_job(&myjob);
715
	}
716
717
2898
	return gn->built_status;
718
}
719
720
721
static void
722
setup_engine(void)
723
6306
{
724
	static int already_setup = 0;
725
726
6306
	if (!already_setup) {
727
729
		setup_meta();
728
729
		already_setup = 1;
729
	}
730
6306
}
731
732
static bool
733
do_run_command(Job *job)
734
3450
{
735
	bool silent;	/* Don't print command */
736
	bool doExecute;	/* Execute the command */
737
	bool errCheck;	/* Check errors */
738
	pid_t cpid; 	/* Child pid */
739
740
3450
	const char *cmd = job->cmd;
741
3450
	silent = job->node->type & OP_SILENT;
742
3450
	errCheck = !(job->node->type & OP_IGNORE);
743
3450
	if (job->node->type & OP_MAKE)
744
		doExecute = true;
745
	else
746
3450
		doExecute = !noExecute;
747
748
	/* How can we execute a null command ? we warn the user that the
749
	 * command expanded to nothing (is this the right thing to do?).  */
750
3450
	if (*cmd == '\0') {
751
		Error("%s expands to empty string", cmd);
752
		return false;
753
	}
754
755
1670
	for (;; cmd++) {
756
5120
		if (*cmd == '@')
757
1407
			silent = DEBUG(LOUD) ? false : true;
758
3713
		else if (*cmd == '-')
759
263
			errCheck = false;
760
3450
		else if (*cmd == '+')
761
			doExecute = true;
762
		else
763
3450
			break;
764
1670
	}
765
7654
	while (ISSPACE(*cmd))
766
377
		cmd++;
767
	/* Print the command before fork if make -n or !silent*/
768

3450
	if ( noExecute || !silent)
769
1781
		printf("%s\n", cmd);
770
771
3450
	if (silent)
772
1669
		job->flags |= JOB_SILENT;
773
	else
774
1781
		job->flags &= ~JOB_SILENT;
775
776
	/* If we're not supposed to execute any commands, this is as far as
777
	 * we go...  */
778
3450
	if (!doExecute)
779
		return false;
780
	/* always flush for other stuff */
781
3450
	fflush(stdout);
782
783
	/* Fork and execute the single command. If the fork fails, we abort.  */
784
3450
	switch (cpid = fork()) {
785
	case -1:
786
		Punt("Could not fork");
787
		/*NOTREACHED*/
788
	case 0:
789
		/* put a random delay unless we're the only job running
790
		 * and there's nothing left to do.
791
		 */
792
3450
		if (random_delay)
793
			if (!(runningJobs == NULL && no_jobs_left()))
794
				usleep(arc4random_uniform(random_delay));
795
3450
		run_command(cmd, errCheck);
796
		/*NOTREACHED*/
797
	default:
798
3450
		job->pid = cpid;
799
3450
		job->next = runningJobs;
800
3450
		runningJobs = job;
801
3450
		if (errCheck)
802
3187
			job->flags |= JOB_ERRCHECK;
803
		else
804
263
			job->flags &= ~JOB_ERRCHECK;
805

3450
		debug_job_printf("Running %ld (%s) %s\n", (long)job->pid,
806
		    job->node->name, (noExecute || !silent) ? "" : cmd);
807
3450
		return true;
808
	}
809
}
810
811
bool
812
job_run_next(Job *job)
813
6306
{
814
	bool started;
815
6306
	GNode *gn = job->node;
816
817
6306
	setup_engine();
818
12612
	while (job->next_cmd != NULL) {
819
3450
		struct command *command = Lst_Datum(job->next_cmd);
820
821
3450
		handle_all_signals();
822
3450
		job->location = &command->location;
823
3450
		Parse_SetLocation(job->location);
824
3450
		job->cmd = Var_Subst(command->string, &gn->context, false);
825
3450
		job->next_cmd = Lst_Adv(job->next_cmd);
826
3450
		if (fatal_errors)
827
			Punt(NULL);
828
3450
		started = do_run_command(job);
829
3450
		if (started)
830
3450
			return false;
831
		else
832
			free(job->cmd);
833
	}
834
2856
	job->exit_type = JOB_EXIT_OKAY;
835
2856
	return true;
836
}
837