xref: /openbsd/usr.bin/make/job.c (revision 891d7ab6)
1 /*	$OpenBSD: job.c,v 1.120 2010/07/19 19:46:44 espie Exp $	*/
2 /*	$NetBSD: job.c,v 1.16 1996/11/06 17:59:08 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
6  * Copyright (c) 1988, 1989 by Adam de Boor
7  * Copyright (c) 1989 by Berkeley Softworks
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to Berkeley by
11  * Adam de Boor.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 /*-
39  * job.c --
40  *	handle the creation etc. of our child processes.
41  *
42  * Interface:
43  *	Job_Make		Start the creation of the given target.
44  *
45  *	Job_Init		Called to initialize this module. in addition,
46  *				any commands attached to the .BEGIN target
47  *				are executed before this function returns.
48  *				Hence, the makefile must have been parsed
49  *				before this function is called.
50  *
51  *	Job_End 		Cleanup any memory used.
52  *
53  *	can_start_job		Return true if we can start job
54  *
55  *	Job_Empty		Return true if the job table is completely
56  *				empty.
57  *
58  *	Job_Finish		Perform any final processing which needs doing.
59  *				This includes the execution of any commands
60  *				which have been/were attached to the .END
61  *				target. It should only be called when the
62  *				job table is empty.
63  *
64  *	Job_AbortAll		Abort all current jobs. It doesn't
65  *				handle output or do anything for the jobs,
66  *				just kills them. It should only be called in
67  *				an emergency, as it were.
68  *
69  *	Job_Wait		Wait for all running jobs to finish.
70  */
71 
72 #include <sys/types.h>
73 #include <sys/wait.h>
74 #include <ctype.h>
75 #include <errno.h>
76 #include <fcntl.h>
77 #include <signal.h>
78 #include <stdarg.h>
79 #include <stdio.h>
80 #include <stdlib.h>
81 #include <string.h>
82 #include <unistd.h>
83 #include "config.h"
84 #include "defines.h"
85 #include "job.h"
86 #include "engine.h"
87 #include "pathnames.h"
88 #include "var.h"
89 #include "targ.h"
90 #include "error.h"
91 #include "lst.h"
92 #include "extern.h"
93 #include "gnode.h"
94 #include "memory.h"
95 #include "make.h"
96 
97 /*
98  * The SEL_ constants determine the maximum amount of time spent in select
99  * before coming out to see if a child has finished. SEL_SEC is the number of
100  * seconds and SEL_USEC is the number of micro-seconds
101  */
102 #define SEL_SEC 	0
103 #define SEL_USEC	500000
104 
105 
106 /*-
107  * Job Table definitions.
108  *
109  * Each job has several things associated with it:
110  *	1) The process id of the child shell
111  *	2) The graph node describing the target being made by this job
112  *	3) An FILE* for writing out the commands. This is only
113  *	   used before the job is actually started.
114  *	4) Things used for handling the shell's output.
115  *	   the output is being caught via a pipe and
116  *	   the descriptors of our pipe, an array in which output is line
117  *	   buffered and the current position in that buffer are all
118  *	   maintained for each job.
119  *	5) A word of flags which determine how the module handles errors,
120  *	   echoing, etc. for the job
121  *
122  * The job "table" is kept as a linked Lst in 'jobs', with the number of
123  * active jobs maintained in the 'nJobs' variable. At no time will this
124  * exceed the value of 'maxJobs', initialized by the Job_Init function.
125  *
126  * When a job is finished, the Make_Update function is called on each of the
127  * parents of the node which was just remade. This takes care of the upward
128  * traversal of the dependency graph.
129  */
130 #define JOB_BUFSIZE	1024
131 struct job_pipe {
132 	int fd;
133 	char buffer[JOB_BUFSIZE];
134 	size_t pos;
135 };
136 
137 typedef struct Job_ {
138     pid_t 	pid;	    /* The child's process ID */
139     GNode	*node;	    /* The target the child is making */
140     short	flags;	    /* Flags to control treatment of job */
141     LstNode	p;
142 #define JOB_DIDOUTPUT	0x001
143 #define JOB_IS_SPECIAL	0x004	/* Target is a special one. */
144 #define JOB_IS_EXPENSIVE 0x002
145     struct job_pipe in[2];
146 } Job;
147 
148 struct job_pid {
149 	pid_t pid;
150 };
151 
152 static int	aborting = 0;	    /* why is the make aborting? */
153 #define ABORT_ERROR	1	    /* Because of an error */
154 #define ABORT_INTERRUPT 2	    /* Because it was interrupted */
155 #define ABORT_WAIT	3	    /* Waiting for jobs to finish */
156 
157 static int	maxJobs;	/* The most children we can run at once */
158 static int	nJobs;		/* The number of current children */
159 static bool	expensive_job;
160 static LIST	runningJobs;	/* The structures that describe them */
161 static GNode	*lastNode;	/* The node for which output was most recently
162 				 * produced. */
163 static LIST	job_pids;	/* a simple list that doesn't move that much */
164 
165 /* data structure linked to job handling through select */
166 static fd_set *output_mask = NULL;	/* File descriptors to look for */
167 
168 static fd_set *actual_mask = NULL;	/* actual select argument */
169 static int largest_fd = -1;
170 static size_t mask_size = 0;
171 
172 /* wait possibilities */
173 #define JOB_EXITED 0
174 #define JOB_SIGNALED 1
175 #define JOB_UNKNOWN 4
176 
177 static LIST	errorsList;
178 static int	errors;
179 struct error_info {
180 	int reason;
181 	int code;
182 	GNode *n;
183 };
184 
185 /* for blocking/unblocking */
186 static sigset_t oset, set;
187 static void block_signals(void);
188 static void unblock_signals(void);
189 
190 static void handle_all_signals(void);
191 static void handle_signal(int);
192 static int JobCmpPid(void *, void *);
193 static void process_job_status(Job *, int);
194 static void JobExec(Job *);
195 static void JobStart(GNode *, int);
196 static void JobInterrupt(bool, int);
197 static void debug_printf(const char *, ...);
198 static Job *prepare_job(GNode *, int);
199 static void banner(Job *, FILE *);
200 static bool Job_Full(void);
201 
202 /***
203  ***  Input/output from jobs
204  ***/
205 
206 /* prepare_pipe(jp, &fd):
207  *	set up pipe data structure (buffer and pos) corresponding to
208  *	pointed fd, and prepare to watch for it.
209  */
210 static void prepare_pipe(struct job_pipe *, int *);
211 
212 /* close_job_pipes(j):
213  *	handle final output from job, and close pipes properly
214  */
215 static void close_job_pipes(Job *);
216 
217 
218 static void handle_all_jobs_output(void);
219 
220 /* handle_job_output(job, n, finish):
221  *	n = 0 or 1 (stdout/stderr), set finish to retrieve everything.
222  */
223 static void handle_job_output(Job *, int, bool);
224 
225 static void print_partial_buffer(struct job_pipe *, Job *, FILE *, size_t);
226 static void print_partial_buffer_and_shift(struct job_pipe *, Job *, FILE *,
227     size_t);
228 static bool print_complete_lines(struct job_pipe *, Job *, FILE *, size_t);
229 
230 
231 static void register_error(int, int, Job *);
232 static void loop_handle_running_jobs(void);
233 static void Job_CatchChildren(void);
234 
235 static void
236 register_error(int reason, int code, Job *job)
237 {
238 	struct error_info *p;
239 
240 	errors++;
241 	p = emalloc(sizeof(struct error_info));
242 	p->reason = reason;
243 	p->code = code;
244 	p->n = job->node;
245 	Lst_AtEnd(&errorsList, p);
246 }
247 
248 void
249 print_errors()
250 {
251 	LstNode ln;
252 	struct error_info *p;
253 	const char *type;
254 
255 	for (ln = Lst_First(&errorsList); ln != NULL; ln = Lst_Adv(ln)) {
256 		p = (struct error_info *)Lst_Datum(ln);
257 		switch(p->reason) {
258 		case JOB_EXITED:
259 			type = "Exit status";
260 			break;
261 		case JOB_SIGNALED:
262 			type = "Received signal";
263 			break;
264 		default:
265 			type = "Should not happen";
266 			break;
267 		}
268 	if (p->n->lineno)
269 		Error(" %s %d (%s, line %lu of %s)",
270 		    type, p->code, p->n->name, p->n->lineno, p->n->fname);
271 	else
272 		Error(" %s %d (%s)", type, p->code, p->n->name);
273 	}
274 }
275 
276 static void
277 banner(Job *job, FILE *out)
278 {
279 	if (job->node != lastNode) {
280 		if (DEBUG(JOBBANNER))
281 			(void)fprintf(out, "--- %s ---\n", job->node->name);
282 		lastNode = job->node;
283 	}
284 }
285 
286 volatile sig_atomic_t got_SIGTSTP, got_SIGTTOU, got_SIGTTIN, got_SIGWINCH,
287     got_SIGCONT;
288 static void
289 handle_all_signals()
290 {
291 	while (got_signal) {
292 		got_signal = 0;
293 
294 		if (got_SIGINT) {
295 			got_SIGINT=0;
296 			handle_signal(SIGINT);
297 		}
298 		if (got_SIGHUP) {
299 			got_SIGHUP=0;
300 			handle_signal(SIGHUP);
301 		}
302 		if (got_SIGQUIT) {
303 			got_SIGQUIT=0;
304 			handle_signal(SIGQUIT);
305 		}
306 		if (got_SIGTERM) {
307 			got_SIGTERM=0;
308 			handle_signal(SIGTERM);
309 		}
310 		if (got_SIGTSTP) {
311 			got_SIGTSTP=0;
312 			signal(SIGTSTP, parallel_handler);
313 		}
314 		if (got_SIGTTOU) {
315 			got_SIGTTOU=0;
316 			signal(SIGTTOU, parallel_handler);
317 		}
318 		if (got_SIGTTIN) {
319 			got_SIGTTIN=0;
320 			signal(SIGTTIN, parallel_handler);
321 		}
322 		if (got_SIGWINCH) {
323 			got_SIGWINCH=0;
324 			signal(SIGWINCH, parallel_handler);
325 		}
326 		if (got_SIGCONT) {
327 			got_SIGCONT = 0;
328 			signal(SIGCONT, parallel_handler);
329 		}
330 	}
331 }
332 
333 /* this is safe from interrupts, actually */
334 void
335 parallel_handler(int signo)
336 {
337 	int save_errno = errno;
338 	LstNode ln;
339 	for (ln = Lst_First(&job_pids); ln != NULL; ln = Lst_Adv(ln)) {
340 	    	struct job_pid *p = Lst_Datum(ln);
341 		killpg(p->pid, signo);
342 	}
343 	errno = save_errno;
344 
345 	switch(signo) {
346 	case SIGINT:
347 		got_SIGINT++;
348 		got_signal = 1;
349 		return;
350 	case SIGHUP:
351 		got_SIGHUP++;
352 		got_signal = 1;
353 		return;
354 	case SIGQUIT:
355 		got_SIGQUIT++;
356 		got_signal = 1;
357 		return;
358 	case SIGTERM:
359 		got_SIGTERM++;
360 		got_signal = 1;
361 		return;
362 	case SIGTSTP:
363 		got_SIGTSTP++;
364 		got_signal = 1;
365 		break;
366 	case SIGTTOU:
367 		got_SIGTTOU++;
368 		got_signal = 1;
369 		break;
370 	case SIGTTIN:
371 		got_SIGTTIN++;
372 		got_signal = 1;
373 		break;
374 	case SIGWINCH:
375 		got_SIGWINCH++;
376 		got_signal = 1;
377 		break;
378 	case SIGCONT:
379 		got_SIGCONT++;
380 		got_signal = 1;
381 		break;
382 	}
383 	(void)killpg(getpid(), signo);
384 
385 	(void)signal(signo, SIG_DFL);
386 	errno = save_errno;
387 }
388 
389 /*-
390  *-----------------------------------------------------------------------
391  * handle_signal --
392  *	handle a signal for ourselves
393  *
394  *-----------------------------------------------------------------------
395  */
396 static void
397 handle_signal(int signo)
398 {
399 	if (DEBUG(JOB)) {
400 		(void)fprintf(stdout, "handle_signal(%d) called.\n", signo);
401 		(void)fflush(stdout);
402 	}
403 
404 	/*
405 	 * Deal with proper cleanup based on the signal received. We only run
406 	 * the .INTERRUPT target if the signal was in fact an interrupt. The
407 	 * other three termination signals are more of a "get out *now*"
408 	 * command.
409 	 */
410 	if (signo == SIGINT)
411 		JobInterrupt(true, signo);
412 	else if (signo == SIGHUP || signo == SIGTERM || signo == SIGQUIT)
413 		JobInterrupt(false, signo);
414 
415 	if (signo == SIGQUIT)
416 		Finish(0);
417 }
418 
419 /*-
420  *-----------------------------------------------------------------------
421  * JobCmpPid  --
422  *	Compare the pid of the job with the given pid and return 0 if they
423  *	are equal. This function is called from Job_CatchChildren via
424  *	Lst_Find to find the job descriptor of the finished job.
425  *
426  * Results:
427  *	0 if the pid's match
428  *-----------------------------------------------------------------------
429  */
430 static int
431 JobCmpPid(void *job,	/* job to examine */
432     void *pid)		/* process id desired */
433 {
434 	return *(pid_t *)pid - ((Job *)job)->pid;
435 }
436 
437 static void
438 debug_printf(const char *fmt, ...)
439 {
440 	if (DEBUG(JOB)) {
441 		va_list va;
442 
443 		va_start(va, fmt);
444 		(void)vfprintf(stdout, fmt, va);
445 		fflush(stdout);
446 		va_end(va);
447 	}
448 }
449 
450 static void
451 close_job_pipes(Job *job)
452 {
453 	int i;
454 
455 	for (i = 1; i >= 0; i--) {
456 		FD_CLR(job->in[i].fd, output_mask);
457 		handle_job_output(job, i, true);
458 		(void)close(job->in[i].fd);
459 	}
460 }
461 
462 /*-
463  *-----------------------------------------------------------------------
464  * process_job_status  --
465  *	Do processing for the given job including updating
466  *	parents and starting new jobs as available/necessary.
467  *
468  * Side Effects:
469  *	Some nodes may be put on the toBeMade queue.
470  *	Final commands for the job are placed on end_node.
471  *
472  *	If we got an error and are aborting (aborting == ABORT_ERROR) and
473  *	the job list is now empty, we are done for the day.
474  *	If we recognized an error we set the aborting flag
475  *	to ABORT_ERROR so no more jobs will be started.
476  *-----------------------------------------------------------------------
477  */
478 /*ARGSUSED*/
479 
480 static void
481 process_job_status(Job *job, int status)
482 {
483 	int reason, code;
484 	bool	 done;
485 
486 	debug_printf("Process %ld (%s) exited with status %d.\n",
487 	    (long)job->pid, job->node->name, status);
488 	/* parse status */
489 	if (WIFEXITED(status)) {
490 		reason = JOB_EXITED;
491 		code = WEXITSTATUS(status);
492 	} else if (WIFSIGNALED(status)) {
493 		reason = JOB_SIGNALED;
494 		code = WTERMSIG(status);
495 	} else {
496 		/* can't happen, set things to be bad. */
497 		reason = UNKNOWN;
498 		code = status;
499 	}
500 
501 	if ((reason == JOB_EXITED &&
502 	     code != 0 && !(job->node->type & OP_IGNORE)) ||
503 	    reason == JOB_SIGNALED) {
504 		/*
505 		 * If it exited non-zero and either we're doing things our
506 		 * way or we're not ignoring errors, the job is finished.
507 		 * Similarly, if the shell died because of a signal
508 		 * the job is also finished. In these
509 		 * cases, finish out the job's output before printing the exit
510 		 * status...
511 		 */
512 		close_job_pipes(job);
513 		done = true;
514 	} else if (reason == JOB_EXITED) {
515 		/*
516 		 * Deal with ignored errors. We need to print a message telling
517 		 * of the ignored error as well as setting status.w_status to 0
518 		 * so the next command gets run. To do this, we set done to be
519 		 * true and the job exited non-zero.
520 		 */
521 		done = code != 0;
522 		close_job_pipes(job);
523 	} else {
524 		/*
525 		 * No need to close things down or anything.
526 		 */
527 		done = false;
528 	}
529 
530 	if (done || DEBUG(JOB)) {
531 		if (reason == JOB_EXITED) {
532 			debug_printf("Process %ld (%s) exited.\n",
533 			    (long)job->pid, job->node->name);
534 			if (code != 0) {
535 				banner(job, stdout);
536 				(void)fprintf(stdout, "*** Error code %d %s\n",
537 				    code,
538 				    (job->node->type & OP_IGNORE) ?
539 				    "(ignored)" : "");
540 
541 				if (job->node->type & OP_IGNORE) {
542 					reason = JOB_EXITED;
543 					code = 0;
544 				}
545 			} else if (DEBUG(JOB)) {
546 				(void)fprintf(stdout,
547 				    "*** %ld (%s) Completed successfully\n",
548 				    (long)job->pid, job->node->name);
549 			}
550 		} else {
551 			banner(job, stdout);
552 			(void)fprintf(stdout, "*** Signal %d\n", code);
553 		}
554 
555 		(void)fflush(stdout);
556 	}
557 
558 	done = true;
559 
560 	if (done &&
561 	    aborting != ABORT_ERROR &&
562 	    aborting != ABORT_INTERRUPT &&
563 	    reason == JOB_EXITED && code == 0) {
564 		/* As long as we aren't aborting and the job didn't return a
565 		 * non-zero status that we shouldn't ignore, we call
566 		 * Make_Update to update the parents. */
567 		job->node->built_status = MADE;
568 		Make_Update(job->node);
569 	} else if (!(reason == JOB_EXITED && code == 0)) {
570 		register_error(reason, code, job);
571 	}
572 	free(job);
573 
574 	if (errors && !keepgoing &&
575 	    aborting != ABORT_INTERRUPT)
576 		aborting = ABORT_ERROR;
577 
578 	if (aborting == ABORT_ERROR && Job_Empty())
579 		Finish(errors);
580 }
581 
582 static void
583 prepare_pipe(struct job_pipe *p, int *fd)
584 {
585 	p->pos = 0;
586 	(void)fcntl(fd[0], F_SETFD, FD_CLOEXEC);
587 	p->fd = fd[0];
588 	close(fd[1]);
589 
590 	if (output_mask == NULL || p->fd > largest_fd) {
591 		int fdn, ofdn;
592 
593 		fdn = howmany(p->fd+1, NFDBITS);
594 		ofdn = howmany(largest_fd+1, NFDBITS);
595 
596 		if (fdn != ofdn) {
597 			output_mask = emult_realloc(output_mask, fdn,
598 			    sizeof(fd_mask));
599 			memset(((char *)output_mask) + ofdn * sizeof(fd_mask),
600 			    0, (fdn-ofdn) * sizeof(fd_mask));
601 			actual_mask = emult_realloc(actual_mask, fdn,
602 			    sizeof(fd_mask));
603 			mask_size = fdn * sizeof(fd_mask);
604 		}
605 		largest_fd = p->fd;
606 	}
607 	fcntl(p->fd, F_SETFL, O_NONBLOCK);
608 	FD_SET(p->fd, output_mask);
609 }
610 
611 /*-
612  *-----------------------------------------------------------------------
613  * JobExec --
614  *	Execute the shell for the given job. Called from JobStart
615  *
616  * Side Effects:
617  *	A shell is executed, outputs is altered and the Job structure added
618  *	to the job table.
619  *-----------------------------------------------------------------------
620  */
621 static void
622 JobExec(Job *job)
623 {
624 	pid_t cpid; 	/* ID of new child */
625 	struct job_pid *p;
626 	int fds[4];
627 	int *fdout = fds;
628 	int *fderr = fds+2;
629 	int i;
630 
631 	banner(job, stdout);
632 
633 	setup_engine(1);
634 
635 	/* Create the pipe by which we'll get the shell's output.
636 	 */
637 	if (pipe(fdout) == -1)
638 		Punt("Cannot create pipe: %s", strerror(errno));
639 
640 	if (pipe(fderr) == -1)
641 		Punt("Cannot create pipe: %s", strerror(errno));
642 
643 	block_signals();
644 	if ((cpid = fork()) == -1) {
645 		Punt("Cannot fork");
646 		unblock_signals();
647 	} else if (cpid == 0) {
648 		supervise_jobs = false;
649 		/* standard pipe code to route stdout and stderr */
650 		close(fdout[0]);
651 		if (dup2(fdout[1], 1) == -1)
652 			Punt("Cannot dup2(outPipe): %s", strerror(errno));
653 		if (fdout[1] != 1)
654 			close(fdout[1]);
655 		close(fderr[0]);
656 		if (dup2(fderr[1], 2) == -1)
657 			Punt("Cannot dup2(errPipe): %s", strerror(errno));
658 		if (fderr[1] != 2)
659 			close(fderr[1]);
660 
661 		/*
662 		 * We want to switch the child into a different process family
663 		 * so we can kill it and all its descendants in one fell swoop,
664 		 * by killing its process family, but not commit suicide.
665 		 */
666 		(void)setpgid(0, getpid());
667 
668 		if (random_delay)
669 			if (!(nJobs == 1 && no_jobs_left()))
670 				usleep(random() % random_delay);
671 
672 		setup_all_signals(SigHandler, SIG_DFL);
673 		unblock_signals();
674 		/* this exits directly */
675 		run_gnode_parallel(job->node);
676 		/*NOTREACHED*/
677 	} else {
678 		supervise_jobs = true;
679 		job->pid = cpid;
680 
681 		/* we set the current position in the buffers to the beginning
682 		 * and mark another stream to watch in the outputs mask
683 		 */
684 		for (i = 0; i < 2; i++)
685 			prepare_pipe(&job->in[i], fds+2*i);
686 	}
687 	/*
688 	 * Now the job is actually running, add it to the table.
689 	 */
690 	nJobs++;
691 	Lst_AtEnd(&runningJobs, job);
692 	if (job->flags & JOB_IS_EXPENSIVE)
693 		expensive_job = true;
694 	p = emalloc(sizeof(struct job_pid));
695 	p->pid = cpid;
696 	Lst_AtEnd(&job_pids, p);
697 	job->p = Lst_Last(&job_pids);
698 
699 	unblock_signals();
700 	if (DEBUG(JOB)) {
701 		LstNode ln;
702 
703 		(void)fprintf(stdout, "Running %ld (%s)\n", (long)cpid,
704 		    job->node->name);
705 		for (ln = Lst_First(&job->node->commands); ln != NULL ;
706 		    ln = Lst_Adv(ln))
707 		    	fprintf(stdout, "\t%s\n", (char *)Lst_Datum(ln));
708 		(void)fflush(stdout);
709 	}
710 
711 }
712 
713 static bool
714 expensive_command(const char *s)
715 {
716 	const char *p;
717 	bool include = false;
718 	bool expensive = false;
719 
720 	/* okay, comments are cheap, always */
721 	if (*s == '#')
722 		return false;
723 
724 	for (p = s; *p != '\0'; p++) {
725 		if (*p == ' ' || *p == '\t') {
726 			include = false;
727 			if (p[1] == '-' && p[2] == 'I')
728 				include = true;
729 		}
730 		if (include)
731 			continue;
732 		/* KMP variant, avoid looking twice at the same
733 		 * letter.
734 		 */
735 		if (*p != 'm')
736 			continue;
737 		if (p[1] != 'a')
738 			continue;
739 		p++;
740 		if (p[1] != 'k')
741 			continue;
742 		p++;
743 		if (p[1] != 'e')
744 			continue;
745 		p++;
746 		expensive = true;
747 		while (p[1] != '\0' && p[1] != ' ' && p[1] != '\t') {
748 			if (p[1] == '.') {
749 				expensive = false;
750 				break;
751 			}
752 		    	p++;
753 		}
754 		if (expensive)
755 			return true;
756 	}
757 	return false;
758 }
759 
760 static bool
761 expensive_commands(Lst l)
762 {
763 	LstNode ln;
764 	for (ln = Lst_First(l); ln != NULL; ln = Lst_Adv(ln))
765 		if (expensive_command(Lst_Datum(ln)))
766 			return true;
767 	return false;
768 }
769 
770 static Job *
771 prepare_job(GNode *gn, int flags)
772 {
773 	bool cmdsOK;     	/* true if the nodes commands were all right */
774 	bool noExec;     	/* Set true if we decide not to run the job */
775 
776 	/*
777 	 * Check the commands now so any attributes from .DEFAULT have a chance
778 	 * to migrate to the node
779 	 */
780 	cmdsOK = Job_CheckCommands(gn);
781 	expand_commands(gn);
782 
783 	if ((gn->type & OP_MAKE) || (!noExecute && !touchFlag)) {
784 		/*
785 		 * We're serious here, but if the commands were bogus, we're
786 		 * also dead...
787 		 */
788 		if (!cmdsOK)
789 			job_failure(gn, Punt);
790 
791 		if (Lst_IsEmpty(&gn->commands))
792 			noExec = true;
793 		else
794 			noExec = false;
795 
796 	} else if (noExecute) {
797 		if (!cmdsOK || Lst_IsEmpty(&gn->commands))
798 			noExec = true;
799 		else
800 			noExec = false;
801 	} else {
802 		/*
803 		 * Just touch the target and note that no shell should be
804 		 * executed.  Check
805 		 * the commands, too, but don't die if they're no good -- it
806 		 * does no harm to keep working up the graph.
807 		 */
808 		Job_Touch(gn);
809 		noExec = true;
810 	}
811 
812 	/*
813 	 * If we're not supposed to execute a shell, don't.
814 	 */
815 	if (noExec) {
816 		/*
817 		 * We only want to work our way up the graph if we aren't here
818 		 * because the commands for the job were no good.
819 		 */
820 		if (cmdsOK && !aborting) {
821 			gn->built_status = MADE;
822 			Make_Update(gn);
823 		}
824 		return NULL;
825 	} else {
826 		Job *job;       	/* new job descriptor */
827 		job = emalloc(sizeof(Job));
828 		if (job == NULL)
829 			Punt("JobStart out of memory");
830 
831 		job->node = gn;
832 
833 		/*
834 		 * Set the initial value of the flags for this job based on the
835 		 * global ones and the node's attributes... Any flags supplied
836 		 * by the caller are also added to the field.
837 		 */
838 		job->flags = flags;
839 		if (expensive_commands(&gn->expanded)) {
840 			job->flags |= JOB_IS_EXPENSIVE;
841 		}
842 
843 		return job;
844 	}
845 }
846 
847 /*-
848  *-----------------------------------------------------------------------
849  * JobStart  --
850  *	Start a target-creation process going for the target described
851  *	by the graph node gn.
852  *
853  * Side Effects:
854  *	A new Job node is created and added to the list of running
855  *	jobs. Make is forked and a child shell created.
856  *-----------------------------------------------------------------------
857  */
858 static void
859 JobStart(GNode *gn,	      	/* target to create */
860     int flags)      		/* flags for the job to override normal ones.
861 			       	 * e.g. JOB_IS_SPECIAL */
862 {
863 	Job *job;
864 	job = prepare_job(gn, flags);
865 	if (!job)
866 		return;
867 	JobExec(job);
868 }
869 
870 /* Helper functions for JobDoOutput */
871 
872 
873 /* output debugging banner and print characters from 0 to endpos */
874 static void
875 print_partial_buffer(struct job_pipe *p, Job *job, FILE *out, size_t endPos)
876 {
877 	size_t i;
878 
879 	banner(job, out);
880 	job->flags |= JOB_DIDOUTPUT;
881 	for (i = 0; i < endPos; i++)
882 		putc(p->buffer[i], out);
883 }
884 
885 /* print partial buffer and shift remaining contents */
886 static void
887 print_partial_buffer_and_shift(struct job_pipe *p, Job *job, FILE *out,
888     size_t endPos)
889 {
890 	size_t i;
891 
892 	print_partial_buffer(p, job, out, endPos);
893 
894 	for (i = endPos; i < p->pos; i++)
895 		p->buffer[i-endPos] = p->buffer[i];
896 	p->pos -= endPos;
897 }
898 
899 /* print complete lines, looking back to the limit position
900  * (stuff before limit was already scanned).
901  * returns true if something was printed.
902  */
903 static bool
904 print_complete_lines(struct job_pipe *p, Job *job, FILE *out, size_t limit)
905 {
906 	size_t i;
907 
908 	for (i = p->pos; i > limit; i--) {
909 		if (p->buffer[i-1] == '\n') {
910 			print_partial_buffer_and_shift(p, job, out, i);
911 			return true;
912 		}
913 	}
914 	return false;
915 }
916 
917 /*-
918  *-----------------------------------------------------------------------
919  * handle_pipe	--
920  *	This functions is called whenever there is something to read on the
921  *	pipe. We collect more output from the given job and store it in the
922  *	job's outBuf. If this makes up lines, we print it tagged by the job's
923  *	identifier, as necessary.
924  *
925  * Side Effects:
926  *	curPos may be shifted as may the contents of outBuf.
927  *-----------------------------------------------------------------------
928  */
929 static void
930 handle_pipe(struct job_pipe *p,
931 	Job *job, FILE *out, bool finish)
932 {
933 	int nr;		  	/* number of bytes read */
934 	int oldpos;		/* optimization */
935 
936 	/* want to get everything ? -> we block */
937 	if (finish)
938 		fcntl(p->fd, F_SETFL, 0);
939 
940 	do {
941 		nr = read(p->fd, &p->buffer[p->pos],
942 		    JOB_BUFSIZE - p->pos);
943 		if (nr == -1) {
944 			if (errno == EAGAIN)
945 				break;
946 			if (DEBUG(JOB)) {
947 				perror("JobDoOutput(piperead)");
948 			}
949 		}
950 		oldpos = p->pos;
951 		p->pos += nr;
952 		if (!print_complete_lines(p, job, out, oldpos))
953 			if (p->pos == JOB_BUFSIZE) {
954 				print_partial_buffer(p, job, out, p->pos);
955 				p->pos = 0;
956 			}
957 	} while (nr != 0);
958 
959 	/* at end of file, we print whatever is left */
960 	if (nr == 0) {
961 		print_partial_buffer(p, job, out, p->pos);
962 		if (p->pos > 0 && p->buffer[p->pos - 1] != '\n')
963 			putchar('\n');
964 		p->pos = 0;
965 	}
966 }
967 
968 static void
969 handle_job_output(Job *job, int i, bool finish)
970 {
971 	handle_pipe(&job->in[i], job, i == 0 ? stdout : stderr, finish);
972 }
973 
974 static void
975 remove_job(LstNode ln, int status)
976 {
977 	Job *job;
978 
979 	job = (Job *)Lst_Datum(ln);
980 	Lst_Remove(&runningJobs, ln);
981 	block_signals();
982 	free(Lst_Datum(job->p));
983 	Lst_Remove(&job_pids, job->p);
984 	unblock_signals();
985 	nJobs--;
986 	if (job->flags & JOB_IS_EXPENSIVE)
987 		expensive_job = false;
988 	process_job_status(job, status);
989 }
990 
991 /*-
992  *-----------------------------------------------------------------------
993  * Job_CatchChildren --
994  *	Handle the exit of a child. Called by handle_running_jobs
995  *
996  * Side Effects:
997  *	The job descriptor is removed from the list of children.
998  *
999  * Notes:
1000  *	We do waits, blocking or not, according to the wisdom of our
1001  *	caller, until there are no more children to report. For each
1002  *	job, call process_job_status to finish things off.
1003  *-----------------------------------------------------------------------
1004  */
1005 void
1006 Job_CatchChildren()
1007 {
1008 	pid_t pid;	/* pid of dead child */
1009 	LstNode jnode;	/* list element for finding job */
1010 	int status;	/* Exit/termination status */
1011 
1012 	/*
1013 	 * Don't even bother if we know there's no one around.
1014 	 */
1015 	if (nJobs == 0)
1016 		return;
1017 
1018 	while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0) {
1019 		handle_all_signals();
1020 
1021 		jnode = Lst_Find(&runningJobs, JobCmpPid, &pid);
1022 
1023 		if (jnode == NULL) {
1024 			Error("Child (%ld) not in table?", (long)pid);
1025 		} else {
1026 			remove_job(jnode, status);
1027 		}
1028 	}
1029 }
1030 
1031 void
1032 handle_all_jobs_output(void)
1033 {
1034 	int nfds;
1035 	struct timeval timeout;
1036 	LstNode ln, ln2;
1037 	Job *job;
1038 	int i;
1039 	int status;
1040 
1041 	/* no jobs */
1042 	if (Lst_IsEmpty(&runningJobs))
1043 		return;
1044 
1045 	(void)fflush(stdout);
1046 
1047 	memcpy(actual_mask, output_mask, mask_size);
1048 	timeout.tv_sec = SEL_SEC;
1049 	timeout.tv_usec = SEL_USEC;
1050 
1051 	nfds = select(largest_fd+1, actual_mask, NULL, NULL, &timeout);
1052 	handle_all_signals();
1053 	for (ln = Lst_First(&runningJobs); nfds && ln != NULL; ln = ln2) {
1054 	    	ln2 = Lst_Adv(ln);
1055 		job = (Job *)Lst_Datum(ln);
1056 		job->flags &= ~JOB_DIDOUTPUT;
1057 		for (i = 1; i >= 0; i--) {
1058 			if (FD_ISSET(job->in[i].fd, actual_mask)) {
1059 				nfds--;
1060 				handle_job_output(job, i, false);
1061 			}
1062 		}
1063 		if (job->flags & JOB_DIDOUTPUT) {
1064 			if (waitpid(job->pid, &status, WNOHANG) == job->pid) {
1065 				remove_job(ln, status);
1066 			} else {
1067 				Lst_Requeue(&runningJobs, ln);
1068 			}
1069 		}
1070 	}
1071 }
1072 
1073 void
1074 handle_running_jobs()
1075 {
1076 	handle_all_jobs_output();
1077 	Job_CatchChildren();
1078 }
1079 
1080 static void
1081 loop_handle_running_jobs()
1082 {
1083 	while (nJobs)
1084 		handle_running_jobs();
1085 }
1086 /*-
1087  *-----------------------------------------------------------------------
1088  * Job_Make --
1089  *	Start the creation of a target. Basically a front-end for
1090  *	JobStart used by the Make module.
1091  *
1092  * Side Effects:
1093  *	Another job is started.
1094  *-----------------------------------------------------------------------
1095  */
1096 void
1097 Job_Make(GNode *gn)
1098 {
1099 	(void)JobStart(gn, 0);
1100 }
1101 
1102 
1103 static void
1104 block_signals()
1105 {
1106 	sigprocmask(SIG_BLOCK, &set, &oset);
1107 }
1108 
1109 static void
1110 unblock_signals()
1111 {
1112 	sigprocmask(SIG_SETMASK, &oset, NULL);
1113 }
1114 
1115 /*-
1116  *-----------------------------------------------------------------------
1117  * Job_Init --
1118  *	Initialize the process module
1119  *
1120  * Side Effects:
1121  *	lists and counters are initialized
1122  *-----------------------------------------------------------------------
1123  */
1124 void
1125 Job_Init(int maxproc)
1126 {
1127 	Static_Lst_Init(&runningJobs);
1128 	Static_Lst_Init(&errorsList);
1129 	maxJobs = maxproc;
1130 	nJobs = 0;
1131 	errors = 0;
1132 	sigemptyset(&set);
1133 	sigaddset(&set, SIGINT);
1134 	sigaddset(&set, SIGHUP);
1135 	sigaddset(&set, SIGQUIT);
1136 	sigaddset(&set, SIGTERM);
1137 	sigaddset(&set, SIGTSTP);
1138 	sigaddset(&set, SIGTTOU);
1139 	sigaddset(&set, SIGTTIN);
1140 
1141 	aborting = 0;
1142 
1143 	lastNode = NULL;
1144 
1145 	if ((begin_node->type & OP_DUMMY) == 0) {
1146 		JobStart(begin_node, JOB_IS_SPECIAL);
1147 		loop_handle_running_jobs();
1148 	}
1149 }
1150 
1151 static bool
1152 Job_Full()
1153 {
1154 	return aborting || (nJobs >= maxJobs);
1155 }
1156 /*-
1157  *-----------------------------------------------------------------------
1158  * Job_Full --
1159  *	See if the job table is full. It is considered full
1160  *	if we are in the process of aborting OR if we have
1161  *	reached/exceeded our quota.
1162  *
1163  * Results:
1164  *	true if the job table is full, false otherwise
1165  *-----------------------------------------------------------------------
1166  */
1167 bool
1168 can_start_job(void)
1169 {
1170 	if (Job_Full() || expensive_job)
1171 		return false;
1172 	else
1173 		return true;
1174 }
1175 
1176 /*-
1177  *-----------------------------------------------------------------------
1178  * Job_Empty --
1179  *	See if the job table is empty.
1180  *
1181  * Results:
1182  *	true if it is. false if it ain't.
1183  * -----------------------------------------------------------------------
1184  */
1185 bool
1186 Job_Empty(void)
1187 {
1188 	if (nJobs == 0)
1189 		return true;
1190 	else
1191 		return false;
1192 }
1193 
1194 /*-
1195  *-----------------------------------------------------------------------
1196  * JobInterrupt --
1197  *	Handle the receipt of an interrupt.
1198  *
1199  * Side Effects:
1200  *	All children are killed. Another job will be started if the
1201  *	.INTERRUPT target was given.
1202  *-----------------------------------------------------------------------
1203  */
1204 static void
1205 JobInterrupt(bool runINTERRUPT,	/* true if commands for the .INTERRUPT
1206 				 * target should be executed */
1207     int signo)			/* signal received */
1208 {
1209 	LstNode ln;		/* element in job table */
1210 	Job *job; 		/* job descriptor in that element */
1211 
1212 	aborting = ABORT_INTERRUPT;
1213 
1214 	for (ln = Lst_First(&runningJobs); ln != NULL; ln = Lst_Adv(ln)) {
1215 		job = (Job *)Lst_Datum(ln);
1216 
1217 		if (!Targ_Precious(job->node)) {
1218 			const char *file = job->node->path == NULL ?
1219 			    job->node->name : job->node->path;
1220 			if (!noExecute && eunlink(file) != -1) {
1221 				Error("*** %s removed", file);
1222 			}
1223 		}
1224 		if (job->pid) {
1225 			debug_printf("JobInterrupt passing signal to "
1226 			    "child %ld.\n", (long)job->pid);
1227 			killpg(job->pid, signo);
1228 		}
1229 	}
1230 
1231 	if (runINTERRUPT && !touchFlag) {
1232 		if ((interrupt_node->type & OP_DUMMY) == 0) {
1233 			ignoreErrors = false;
1234 
1235 			JobStart(interrupt_node, 0);
1236 			loop_handle_running_jobs();
1237 		}
1238 	}
1239 	exit(signo);
1240 }
1241 
1242 /*
1243  *-----------------------------------------------------------------------
1244  * Job_Finish --
1245  *	Do final processing such as the running of the commands
1246  *	attached to the .END target.
1247  *
1248  * Results:
1249  *	Number of errors reported.
1250  *
1251  *-----------------------------------------------------------------------
1252  */
1253 int
1254 Job_Finish(void)
1255 {
1256 	if ((end_node->type & OP_DUMMY) == 0) {
1257 		if (errors) {
1258 			Error("Errors reported so .END ignored");
1259 		} else {
1260 			JobStart(end_node, JOB_IS_SPECIAL);
1261 			loop_handle_running_jobs();
1262 		}
1263 	}
1264 	return errors;
1265 }
1266 
1267 #ifdef CLEANUP
1268 void
1269 Job_End(void)
1270 {
1271 }
1272 #endif
1273 
1274 /*-
1275  *-----------------------------------------------------------------------
1276  * Job_Wait --
1277  *	Waits for all running jobs to finish and returns. Sets 'aborting'
1278  *	to ABORT_WAIT to prevent other jobs from starting.
1279  *
1280  * Side Effects:
1281  *	Currently running jobs finish.
1282  *
1283  *-----------------------------------------------------------------------
1284  */
1285 void
1286 Job_Wait(void)
1287 {
1288 	aborting = ABORT_WAIT;
1289 	loop_handle_running_jobs();
1290 	aborting = 0;
1291 }
1292 
1293 /*-
1294  *-----------------------------------------------------------------------
1295  * Job_AbortAll --
1296  *	Abort all currently running jobs without handling output or anything.
1297  *	This function is to be called only in the event of a major
1298  *	error. Most definitely NOT to be called from JobInterrupt.
1299  *
1300  * Side Effects:
1301  *	All children are killed, not just the firstborn
1302  *-----------------------------------------------------------------------
1303  */
1304 void
1305 Job_AbortAll(void)
1306 {
1307 	LstNode ln;	/* element in job table */
1308 	Job *job;	/* the job descriptor in that element */
1309 	int foo;
1310 
1311 	aborting = ABORT_ERROR;
1312 
1313 	if (nJobs) {
1314 		for (ln = Lst_First(&runningJobs); ln != NULL;
1315 		    ln = Lst_Adv(ln)) {
1316 			job = (Job *)Lst_Datum(ln);
1317 
1318 			/*
1319 			 * kill the child process with increasingly drastic
1320 			 * signals to make darn sure it's dead.
1321 			 */
1322 			killpg(job->pid, SIGINT);
1323 			killpg(job->pid, SIGKILL);
1324 		}
1325 	}
1326 
1327 	/*
1328 	 * Catch as many children as want to report in at first, then give up
1329 	 */
1330 	while (waitpid(WAIT_ANY, &foo, WNOHANG) > 0)
1331 		continue;
1332 }
1333 
1334