1 /* $NetBSD: job.c,v 1.326 2020/11/16 18:28:27 rillig Exp $ */
4 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * Copyright (c) 1988, 1989 by Adam de Boor
37 * Copyright (c) 1989 by Berkeley Softworks
38 * All rights reserved.
40 * This code is derived from software contributed to Berkeley by
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
74 * handle the creation etc. of our child processes.
77 * Job_Init Called to initialize this module. In addition,
78 * any commands attached to the .BEGIN target
79 * are executed before this function returns.
80 * Hence, the makefiles must have been parsed
81 * before this function is called.
83 * Job_End Clean up any memory used.
85 * Job_Make Start the creation of the given target.
88 * Check for and handle the termination of any
89 * children. This must be called reasonably
90 * frequently to keep the whole make going at
91 * a decent clip, since job table entries aren't
92 * removed until their process is caught this way.
95 * Print any output our children have produced.
96 * Should also be called fairly frequently to
97 * keep the user informed of what's going on.
98 * If no output is waiting, it will block for
99 * a time given by the SEL_* constants, below,
100 * or until output is ready.
102 * Job_ParseShell Given the line following a .SHELL target, parse
103 * the line as a shell specification. Returns
104 * FALSE if the spec was incorrect.
106 * Job_Finish Perform any final processing which needs doing.
107 * This includes the execution of any commands
108 * which have been/were attached to the .END
109 * target. It should only be called when the
110 * job table is empty.
112 * Job_AbortAll Abort all currently running jobs. It doesn't
113 * handle output or do anything for the jobs,
114 * just kills them. It should only be called in
118 * Verify that the commands for a target are
119 * ok. Provide them if necessary and possible.
121 * Job_Touch Update a target without really updating it.
123 * Job_Wait Wait for all currently-running jobs to finish.
129 #include <sys/types.h>
130 #include <sys/stat.h>
131 #include <sys/file.h>
132 #include <sys/time.h>
136 #if !defined(USE_SELECT) && defined(HAVE_POLL_H)
139 #ifndef USE_SELECT /* no poll.h */
142 #if defined(HAVE_SYS_SELECT_H)
143 # include <sys/select.h>
148 #if defined(HAVE_SYS_SOCKET_H)
149 # include <sys/socket.h>
155 #include "pathnames.h"
158 /* "@(#)job.c 8.2 (Berkeley) 3/19/94" */
159 MAKE_RCSID("$NetBSD: job.c,v 1.326 2020/11/16 18:28:27 rillig Exp $");
161 /* A shell defines how the commands are run. All commands for a target are
162 * written into a single file, which is then given to the shell to execute
163 * the commands from it. The commands are written to the file using a few
164 * templates for echo control and error control.
166 * The name of the shell is the basename for the predefined shells, such as
167 * "sh", "csh", "bash". For custom shells, it is the full pathname, and its
168 * basename is used to select the type of shell; the longest match wins.
169 * So /usr/pkg/bin/bash has type sh, /usr/local/bin/tcsh has type csh.
171 * The echoing of command lines is controlled using hasEchoCtl, echoOff,
172 * echoOn, noPrint and noPrintLen. When echoOff is executed by the shell, it
173 * still outputs something, but this something is not interesting, therefore
174 * it is filtered out using noPrint and noPrintLen.
176 * The error checking for individual commands is controlled using hasErrCtl,
177 * errOnOrEcho, errOffOrExecIgnore and errExit.
179 * If a shell doesn't have error control, errOnOrEcho becomes a printf template
180 * for echoing the command, should echoing be on; errOffOrExecIgnore becomes
181 * another printf template for executing the command while ignoring the return
182 * status. Finally errExit is a printf template for running the command and
183 * causing the shell to exit on error. If any of these strings are empty when
184 * hasErrCtl is FALSE, the command will be executed anyway as is, and if it
185 * causes an error, so be it. Any templates set up to echo the command will
186 * escape any '$ ` \ "' characters in the command string to avoid common
187 * problems with echo "%s\n" as a template.
189 * The command-line flags "echo" and "exit" also control the behavior. The
190 * "echo" flag causes the shell to start echoing commands right away. The
191 * "exit" flag causes the shell to exit when an error is detected in one of
194 typedef struct Shell {
196 /* The name of the shell. For Bourne and C shells, this is used only to
197 * find the shell description when used as the single source of a .SHELL
198 * target. For user-defined shells, this is the full path of the shell. */
201 Boolean hasEchoCtl; /* True if both echoOff and echoOn defined */
202 const char *echoOff; /* command to turn off echo */
203 const char *echoOn; /* command to turn it back on again */
204 const char *noPrint; /* text to skip when printing output from
205 * shell. This is usually the same as echoOff */
206 size_t noPrintLen; /* length of noPrint command */
208 Boolean hasErrCtl; /* set if can control error checking for
209 * individual commands */
210 /* XXX: split into errOn and echoCmd */
211 const char *errOnOrEcho; /* template to turn on error checking */
212 /* XXX: split into errOff and execIgnore */
213 const char *errOffOrExecIgnore; /* template to turn off error checking */
214 const char *errExit; /* template to use for testing exit code */
216 /* string literal that results in a newline character when it appears
217 * outside of any 'quote' or "quote" characters */
219 char commentChar; /* character used by shell for comment lines */
224 const char *echo; /* echo commands */
225 const char *exit; /* exit on error */
229 * FreeBSD: traditionally .MAKE is not required to
230 * pass jobs queue to sub-makes.
231 * Use .MAKE.ALWAYS_PASS_JOB_QUEUE=no to disable.
233 #define MAKE_ALWAYS_PASS_JOB_QUEUE ".MAKE.ALWAYS_PASS_JOB_QUEUE"
234 static int Always_pass_job_queue = TRUE;
236 * FreeBSD: aborting entire parallel make isn't always
237 * desired. When doing tinderbox for example, failure of
238 * one architecture should not stop all.
239 * We still want to bail on interrupt though.
241 #define MAKE_JOB_ERROR_TOKEN "MAKE_JOB_ERROR_TOKEN"
242 static int Job_error_token = TRUE;
245 * error handling variables
247 static int errors = 0; /* number of errors reported */
248 typedef enum AbortReason { /* why is the make aborting? */
250 ABORT_ERROR, /* Because of an error */
251 ABORT_INTERRUPT, /* Because it was interrupted */
252 ABORT_WAIT /* Waiting for jobs to finish */
254 static AbortReason aborting = ABORT_NONE;
255 #define JOB_TOKENS "+EI+" /* Token to requeue for each abort state */
258 * this tracks the number of tokens currently "out" to build jobs.
260 int jobTokensRunning = 0;
262 /* The number of commands actually printed to the shell commands file for
263 * the current job. Should this number be 0, no shell will be executed. */
264 static int numCommands;
266 typedef enum JobStartResult {
267 JOB_RUNNING, /* Job is running */
268 JOB_ERROR, /* Error in starting the job */
269 JOB_FINISHED /* The job is already finished */
273 * Descriptions for various shells.
275 * The build environment may set DEFSHELL_INDEX to one of
276 * DEFSHELL_INDEX_SH, DEFSHELL_INDEX_KSH, or DEFSHELL_INDEX_CSH, to
277 * select one of the predefined shells as the default shell.
279 * Alternatively, the build environment may set DEFSHELL_CUSTOM to the
280 * name or the full path of a sh-compatible shell, which will be used as
283 * ".SHELL" lines in Makefiles can choose the default shell from the
284 * set defined here, or add additional shells.
287 #ifdef DEFSHELL_CUSTOM
288 #define DEFSHELL_INDEX_CUSTOM 0
289 #define DEFSHELL_INDEX_SH 1
290 #define DEFSHELL_INDEX_KSH 2
291 #define DEFSHELL_INDEX_CSH 3
292 #else /* !DEFSHELL_CUSTOM */
293 #define DEFSHELL_INDEX_SH 0
294 #define DEFSHELL_INDEX_KSH 1
295 #define DEFSHELL_INDEX_CSH 2
296 #endif /* !DEFSHELL_CUSTOM */
298 #ifndef DEFSHELL_INDEX
299 #define DEFSHELL_INDEX 0 /* DEFSHELL_INDEX_CUSTOM or DEFSHELL_INDEX_SH */
300 #endif /* !DEFSHELL_INDEX */
302 static Shell shells[] = {
303 #ifdef DEFSHELL_CUSTOM
305 * An sh-compatible shell with a non-standard name.
307 * Keep this in sync with the "sh" description below, but avoid
308 * non-portable features that might not be supplied by all
309 * sh-compatible shells.
312 DEFSHELL_CUSTOM, /* .name */
313 FALSE, /* .hasEchoCtl */
318 FALSE, /* .hasErrCtl */
319 "echo \"%s\"\n", /* .errOnOrEcho */
320 "%s\n", /* .errOffOrExecIgnore */
321 "{ %s \n} || exit $?\n", /* .errExit */
322 "'\n'", /* .newline */
323 '#', /* .commentChar */
327 #endif /* DEFSHELL_CUSTOM */
329 * SH description. Echo control is also possible and, under
330 * sun UNIX anyway, one can even control error checking.
334 FALSE, /* .hasEchoCtl */
339 FALSE, /* .hasErrCtl */
340 "echo \"%s\"\n", /* .errOnOrEcho */
341 "%s\n", /* .errOffOrExecIgnore */
342 "{ %s \n} || exit $?\n", /* .errExit */
343 "'\n'", /* .newline */
344 '#', /* .commentChar*/
345 #if defined(MAKE_NATIVE) && defined(__NetBSD__)
357 TRUE, /* .hasEchoCtl */
358 "set +v", /* .echoOff */
359 "set -v", /* .echoOn */
360 "set +v", /* .noPrint */
362 FALSE, /* .hasErrCtl */
363 "echo \"%s\"\n", /* .errOnOrEcho */
364 "%s\n", /* .errOffOrExecIgnore */
365 "{ %s \n} || exit $?\n", /* .errExit */
366 "'\n'", /* .newline */
367 '#', /* .commentChar */
372 * CSH description. The csh can do echo control by playing
373 * with the setting of the 'echo' shell variable. Sadly,
374 * however, it is unable to do error control nicely.
378 TRUE, /* .hasEchoCtl */
379 "unset verbose", /* .echoOff */
380 "set verbose", /* .echoOn */
381 "unset verbose", /* .noPrint */
382 13, /* .noPrintLen */
383 FALSE, /* .hasErrCtl */
384 "echo \"%s\"\n", /* .errOnOrEcho */
385 /* XXX: Mismatch between errOn and execIgnore */
386 "csh -c \"%s || exit 0\"\n", /* .errOffOrExecIgnore */
388 "'\\\n'", /* .newline */
389 '#', /* .commentChar */
395 /* This is the shell to which we pass all commands in the Makefile.
396 * It is set by the Job_ParseShell function. */
397 static Shell *commandShell = &shells[DEFSHELL_INDEX];
398 const char *shellPath = NULL; /* full pathname of executable image */
399 const char *shellName = NULL; /* last component of shellPath */
400 char *shellErrFlag = NULL;
401 static char *shellArgv = NULL; /* Custom shell args */
404 static Job *job_table; /* The structures that describe them */
405 static Job *job_table_end; /* job_table + maxJobs */
406 static unsigned int wantToken; /* we want a token */
407 static Boolean lurking_children = FALSE;
408 static Boolean make_suspended = FALSE; /* Whether we've seen a SIGTSTP (etc) */
411 * Set of descriptors of pipes connected to
412 * the output channels of children
414 static struct pollfd *fds = NULL;
415 static Job **jobfds = NULL;
416 static nfds_t nfds = 0;
417 static void watchfd(Job *);
418 static void clearfd(Job *);
419 static int readyfd(Job *);
421 static GNode *lastNode; /* The node for which output was most recently
423 static char *targPrefix = NULL; /* What we print at the start of TARG_FMT */
424 static Job tokenWaitJob; /* token wait pseudo-job */
426 static Job childExitJob; /* child exit pseudo-job */
427 #define CHILD_EXIT "."
428 #define DO_JOB_RESUME "R"
430 enum { npseudojobs = 2 }; /* number of pseudo-jobs */
432 #define TARG_FMT "%s %s ---\n" /* Default format */
433 #define MESSAGE(fp, gn) \
434 if (opts.maxJobs != 1 && targPrefix && *targPrefix) \
435 (void)fprintf(fp, TARG_FMT, targPrefix, gn->name)
437 static sigset_t caught_signals; /* Set of signals we handle */
439 static void JobDoOutput(Job *, Boolean);
440 static void JobInterrupt(int, int) MAKE_ATTR_DEAD;
441 static void JobRestartJobs(void);
442 static void JobSigReset(void);
447 #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
455 job_table_dump(const char *where)
459 debug_printf("job table @ %s\n", where);
460 for (job = job_table; job < job_table_end; job++) {
461 debug_printf("job %d, status %d, flags %d, pid %d\n",
462 (int)(job - job_table), job->status, job->flags, job->pid);
467 * Delete the target of a failed, interrupted, or otherwise
468 * unsuccessful job unless inhibited by .PRECIOUS.
471 JobDeleteTarget(GNode *gn)
475 if (gn->type & OP_JOIN)
477 if (gn->type & OP_PHONY)
479 if (Targ_Precious(gn))
484 file = GNode_Path(gn);
485 if (eunlink(file) != -1)
486 Error("*** %s removed", file);
490 * JobSigLock/JobSigUnlock
492 * Signal lock routines to get exclusive access. Currently used to
493 * protect `jobs' and `stoppedJobs' list manipulations.
495 static void JobSigLock(sigset_t *omaskp)
497 if (sigprocmask(SIG_BLOCK, &caught_signals, omaskp) != 0) {
498 Punt("JobSigLock: sigprocmask: %s", strerror(errno));
503 static void JobSigUnlock(sigset_t *omaskp)
505 (void)sigprocmask(SIG_SETMASK, omaskp, NULL);
509 JobCreatePipe(Job *job, int minfd)
514 if (pipe(pipe_fds) == -1)
515 Punt("Cannot create pipe: %s", strerror(errno));
517 for (i = 0; i < 2; i++) {
518 /* Avoid using low numbered fds */
519 fd = fcntl(pipe_fds[i], F_DUPFD, minfd);
526 job->inPipe = pipe_fds[0];
527 job->outPipe = pipe_fds[1];
529 /* Set close-on-exec flag for both */
530 if (fcntl(job->inPipe, F_SETFD, FD_CLOEXEC) == -1)
531 Punt("Cannot set close-on-exec: %s", strerror(errno));
532 if (fcntl(job->outPipe, F_SETFD, FD_CLOEXEC) == -1)
533 Punt("Cannot set close-on-exec: %s", strerror(errno));
536 * We mark the input side of the pipe non-blocking; we poll(2) the
537 * pipe when we're waiting for a job token, but we might lose the
538 * race for the token when a new one becomes available, so the read
539 * from the pipe should not block.
541 flags = fcntl(job->inPipe, F_GETFL, 0);
543 Punt("Cannot get flags: %s", strerror(errno));
545 if (fcntl(job->inPipe, F_SETFL, flags) == -1)
546 Punt("Cannot set flags: %s", strerror(errno));
549 /* Pass the signal to each running job. */
551 JobCondPassSig(int signo)
555 DEBUG1(JOB, "JobCondPassSig(%d) called.\n", signo);
557 for (job = job_table; job < job_table_end; job++) {
558 if (job->status != JOB_ST_RUNNING)
560 DEBUG2(JOB, "JobCondPassSig passing signal %d to child %d.\n",
562 KILLPG(job->pid, signo);
568 * Sends a token on the child exit pipe to wake us up from select()/poll(). */
570 JobChildSig(int signo MAKE_ATTR_UNUSED)
572 while (write(childExitJob.outPipe, CHILD_EXIT, 1) == -1 && errno == EAGAIN)
577 /* Resume all stopped jobs. */
579 JobContinueSig(int signo MAKE_ATTR_UNUSED)
582 * Defer sending SIGCONT to our stopped children until we return
583 * from the signal handler.
585 while (write(childExitJob.outPipe, DO_JOB_RESUME, 1) == -1 &&
590 /* Pass a signal on to all jobs, then resend to ourselves.
591 * We die by the same signal. */
592 MAKE_ATTR_DEAD static void
593 JobPassSig_int(int signo)
595 /* Run .INTERRUPT target then exit */
596 JobInterrupt(TRUE, signo);
599 /* Pass a signal on to all jobs, then resend to ourselves.
600 * We die by the same signal. */
601 MAKE_ATTR_DEAD static void
602 JobPassSig_term(int signo)
604 /* Dont run .INTERRUPT target then exit */
605 JobInterrupt(FALSE, signo);
609 JobPassSig_suspend(int signo)
611 sigset_t nmask, omask;
612 struct sigaction act;
614 /* Suppress job started/continued messages */
615 make_suspended = TRUE;
617 /* Pass the signal onto every job */
618 JobCondPassSig(signo);
621 * Send ourselves the signal now we've given the message to everyone else.
622 * Note we block everything else possible while we're getting the signal.
623 * This ensures that all our jobs get continued when we wake up before
624 * we take any other signal.
627 sigdelset(&nmask, signo);
628 (void)sigprocmask(SIG_SETMASK, &nmask, &omask);
630 act.sa_handler = SIG_DFL;
631 sigemptyset(&act.sa_mask);
633 (void)sigaction(signo, &act, NULL);
636 debug_printf("JobPassSig passing signal %d to self.\n", signo);
638 (void)kill(getpid(), signo);
641 * We've been continued.
643 * A whole host of signals continue to happen!
644 * SIGCHLD for any processes that actually suspended themselves.
645 * SIGCHLD for any processes that exited while we were alseep.
646 * The SIGCONT that actually caused us to wakeup.
648 * Since we defer passing the SIGCONT on to our children until
649 * the main processing loop, we can be sure that all the SIGCHLD
650 * events will have happened by then - and that the waitpid() will
651 * collect the child 'suspended' events.
652 * For correct sequencing we just need to ensure we process the
653 * waitpid() before passing on the SIGCONT.
655 * In any case nothing else is needed here.
658 /* Restore handler and signal mask */
659 act.sa_handler = JobPassSig_suspend;
660 (void)sigaction(signo, &act, NULL);
661 (void)sigprocmask(SIG_SETMASK, &omask, NULL);
665 JobFindPid(int pid, JobStatus status, Boolean isJobs)
669 for (job = job_table; job < job_table_end; job++) {
670 if (job->status == status && job->pid == pid)
673 if (DEBUG(JOB) && isJobs)
674 job_table_dump("no pid");
678 /* Parse leading '@', '-' and '+', which control the exact execution mode. */
682 Boolean *out_shutUp, Boolean *out_errOff, Boolean *out_runAlways)
687 *out_runAlways = FALSE;
691 *out_shutUp = !DEBUG(LOUD);
695 *out_runAlways = TRUE;
701 pp_skip_whitespace(&p);
706 /* Escape a string for a double-quoted string literal in sh, csh and ksh. */
708 EscapeShellDblQuot(const char *cmd)
712 /* Worst that could happen is every char needs escaping. */
713 char *esc = bmake_malloc(strlen(cmd) * 2 + 1);
714 for (i = 0, j = 0; cmd[i] != '\0'; i++, j++) {
715 if (cmd[i] == '$' || cmd[i] == '`' || cmd[i] == '\\' || cmd[i] == '"')
725 JobPrintf(Job *job, const char *fmt, const char *arg)
728 debug_printf(fmt, arg);
730 (void)fprintf(job->cmdFILE, fmt, arg);
731 (void)fflush(job->cmdFILE);
735 JobPrintln(Job *job, const char *line)
737 JobPrintf(job, "%s\n", line);
741 *-----------------------------------------------------------------------
743 * Put out another command for the given job. If the command starts
744 * with an @ or a - we process it specially. In the former case,
745 * so long as the -s and -n flags weren't given to make, we stick
746 * a shell-specific echoOff command in the script. In the latter,
747 * we ignore errors for the entire job, unless the shell has error
749 * If the command is just "..." we take all future commands for this
750 * job to be commands to be executed once the entire graph has been
751 * made and return non-zero to signal that the end of the commands
752 * was reached. These commands are later attached to the .END
753 * node and executed by Job_End when all things are done.
756 * If the command begins with a '-' and the shell has no error control,
757 * the JOB_IGNERR flag is set in the job descriptor.
758 * numCommands is incremented if the command is actually printed.
759 *-----------------------------------------------------------------------
762 JobPrintCommand(Job *job, char *cmd)
764 const char *const cmdp = cmd;
765 Boolean noSpecials; /* true if we shouldn't worry about
766 * inserting special commands into
767 * the input stream. */
768 Boolean shutUp; /* true if we put a no echo command
769 * into the command file */
770 Boolean errOff; /* true if we turned error checking
771 * off before printing the command
772 * and need to turn it back on */
774 const char *cmdTemplate; /* Template to use when printing the
776 char *cmdStart; /* Start of expanded command */
777 char *escCmd = NULL; /* Command with quotes/backticks escaped */
779 noSpecials = !GNode_ShouldExecute(job->node);
783 Var_Subst(cmd, job->node, VARE_WANTRES, &cmd);
784 /* TODO: handle errors */
787 cmdTemplate = "%s\n";
789 ParseRunOptions(&cmd, &shutUp, &errOff, &runAlways);
791 if (runAlways && noSpecials) {
793 * We're not actually executing anything...
794 * but this one needs to be - use compat mode just for it.
796 Compat_RunCommand(cmdp, job->node);
802 * If the shell doesn't have error control the alternate echo'ing will
803 * be done (to avoid showing additional error checking code)
804 * and this will need the characters '$ ` \ "' escaped
807 if (!commandShell->hasErrCtl)
808 escCmd = EscapeShellDblQuot(cmd);
811 if (!(job->flags & JOB_SILENT) && !noSpecials &&
812 (commandShell->hasEchoCtl)) {
813 JobPrintln(job, commandShell->echoOff);
815 if (commandShell->hasErrCtl)
822 if (commandShell->hasErrCtl) {
824 * we don't want the error-control commands showing
825 * up either, so we turn off echoing while executing
826 * them. We could put another field in the shell
827 * structure to tell JobDoOutput to look for this
828 * string too, but why make it any more complex than
831 if (!(job->flags & JOB_SILENT) && !shutUp &&
832 (commandShell->hasEchoCtl)) {
833 JobPrintln(job, commandShell->echoOff);
834 JobPrintln(job, commandShell->errOffOrExecIgnore);
835 JobPrintln(job, commandShell->echoOn);
837 JobPrintln(job, commandShell->errOffOrExecIgnore);
839 } else if (commandShell->errOffOrExecIgnore &&
840 commandShell->errOffOrExecIgnore[0] != '\0') {
842 * The shell has no error control, so we need to be
843 * weird to get it to ignore any errors from the command.
844 * If echoing is turned on, we turn it off and use the
845 * errOnOrEcho template to echo the command. Leave echoing
846 * off so the user doesn't see the weirdness we go through
847 * to ignore errors. Set cmdTemplate to use the weirdness
848 * instead of the simple "%s\n" template.
850 job->flags |= JOB_IGNERR;
851 if (!(job->flags & JOB_SILENT) && !shutUp) {
852 if (commandShell->hasEchoCtl) {
853 JobPrintln(job, commandShell->echoOff);
855 JobPrintf(job, commandShell->errOnOrEcho, escCmd);
859 JobPrintf(job, commandShell->errOnOrEcho, escCmd);
861 cmdTemplate = commandShell->errOffOrExecIgnore;
863 * The error ignoration (hee hee) is already taken care
864 * of by the errOffOrExecIgnore template, so pretend error
865 * checking is still on.
877 * If errors are being checked and the shell doesn't have error control
878 * but does supply an errExit template, then set up commands to run
882 if (!commandShell->hasErrCtl && commandShell->errExit &&
883 commandShell->errExit[0] != '\0') {
884 if (!(job->flags & JOB_SILENT) && !shutUp) {
885 if (commandShell->hasEchoCtl)
886 JobPrintln(job, commandShell->echoOff);
887 JobPrintf(job, commandShell->errOnOrEcho, escCmd);
890 /* If it's a comment line or blank, treat as an ignored error */
891 if (escCmd[0] == commandShell->commentChar ||
893 cmdTemplate = commandShell->errOffOrExecIgnore;
895 cmdTemplate = commandShell->errExit;
900 if (DEBUG(SHELL) && strcmp(shellName, "sh") == 0 &&
901 !(job->flags & JOB_TRACED)) {
902 JobPrintln(job, "set -x");
903 job->flags |= JOB_TRACED;
906 JobPrintf(job, cmdTemplate, cmd);
911 * If echoing is already off, there's no point in issuing the
912 * echoOff command. Otherwise we issue it and pretend it was on
913 * for the whole command...
915 if (!shutUp && !(job->flags & JOB_SILENT) && commandShell->hasEchoCtl) {
916 JobPrintln(job, commandShell->echoOff);
919 JobPrintln(job, commandShell->errOnOrEcho);
921 if (shutUp && commandShell->hasEchoCtl)
922 JobPrintln(job, commandShell->echoOn);
925 /* Print all commands to the shell file that is later executed.
927 * The special command "..." stops printing and saves the remaining commands
928 * to be executed later. */
930 JobPrintCommands(Job *job)
934 for (ln = job->node->commands->first; ln != NULL; ln = ln->next) {
935 const char *cmd = ln->datum;
937 if (strcmp(cmd, "...") == 0) {
938 job->node->type |= OP_SAVE_CMDS;
939 job->tailCmds = ln->next;
943 JobPrintCommand(job, ln->datum);
947 /* Save the delayed commands, to be executed when everything else is done. */
949 JobSaveCommands(Job *job)
951 StringListNode *node;
953 for (node = job->tailCmds; node != NULL; node = node->next) {
954 const char *cmd = node->datum;
956 /* XXX: This Var_Subst is only intended to expand the dynamic
957 * variables such as .TARGET, .IMPSRC. It is not intended to
958 * expand the other variables as well; see deptgt-end.mk. */
959 (void)Var_Subst(cmd, job->node, VARE_WANTRES, &expanded_cmd);
960 /* TODO: handle errors */
961 Lst_Append(Targ_GetEndNode()->commands, expanded_cmd);
966 /* Called to close both input and output pipes when a job is finished. */
968 JobClosePipes(Job *job)
971 (void)close(job->outPipe);
974 JobDoOutput(job, TRUE);
975 (void)close(job->inPipe);
979 /* Do final processing for the given job including updating parent nodes and
980 * starting new jobs as available/necessary.
982 * Deferred commands for the job are placed on the .END node.
984 * If there was a serious error (errors != 0; not an ignored one), no more
985 * jobs will be started.
989 * status sub-why job went away
992 JobFinish (Job *job, WAIT_T status)
994 Boolean done, return_job_token;
996 DEBUG3(JOB, "JobFinish: %d [%s], status %d\n",
997 job->pid, job->node->name, status);
999 if ((WIFEXITED(status) &&
1000 ((WEXITSTATUS(status) != 0 && !(job->flags & JOB_IGNERR)))) ||
1001 WIFSIGNALED(status))
1004 * If it exited non-zero and either we're doing things our
1005 * way or we're not ignoring errors, the job is finished.
1006 * Similarly, if the shell died because of a signal
1007 * the job is also finished. In these
1008 * cases, finish out the job's output before printing the exit
1012 if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
1013 (void)fclose(job->cmdFILE);
1014 job->cmdFILE = NULL;
1017 } else if (WIFEXITED(status)) {
1019 * Deal with ignored errors in -B mode. We need to print a message
1020 * telling of the ignored error as well as to run the next command.
1023 done = WEXITSTATUS(status) != 0;
1027 * No need to close things down or anything.
1033 if (WIFEXITED(status)) {
1034 DEBUG2(JOB, "Process %d [%s] exited.\n",
1035 job->pid, job->node->name);
1036 if (WEXITSTATUS(status) != 0) {
1037 if (job->node != lastNode) {
1038 MESSAGE(stdout, job->node);
1039 lastNode = job->node;
1043 meta_job_error(job, job->node, job->flags, WEXITSTATUS(status));
1046 if (!shouldDieQuietly(job->node, -1))
1047 (void)printf("*** [%s] Error code %d%s\n",
1049 WEXITSTATUS(status),
1050 (job->flags & JOB_IGNERR) ? " (ignored)" : "");
1051 if (job->flags & JOB_IGNERR) {
1052 WAIT_STATUS(status) = 0;
1054 if (deleteOnError) {
1055 JobDeleteTarget(job->node);
1057 PrintOnError(job->node, NULL);
1059 } else if (DEBUG(JOB)) {
1060 if (job->node != lastNode) {
1061 MESSAGE(stdout, job->node);
1062 lastNode = job->node;
1064 (void)printf("*** [%s] Completed successfully\n",
1068 if (job->node != lastNode) {
1069 MESSAGE(stdout, job->node);
1070 lastNode = job->node;
1072 (void)printf("*** [%s] Signal %d\n",
1073 job->node->name, WTERMSIG(status));
1074 if (deleteOnError) {
1075 JobDeleteTarget(job->node);
1078 (void)fflush(stdout);
1083 int meta_status = meta_job_finish(job);
1084 if (meta_status != 0 && status == 0)
1085 status = meta_status;
1089 return_job_token = FALSE;
1091 Trace_Log(JOBEND, job);
1092 if (!(job->flags & JOB_SPECIAL)) {
1093 if (WAIT_STATUS(status) != 0 ||
1094 (aborting == ABORT_ERROR) || aborting == ABORT_INTERRUPT)
1095 return_job_token = TRUE;
1098 if (aborting != ABORT_ERROR && aborting != ABORT_INTERRUPT &&
1099 (WAIT_STATUS(status) == 0)) {
1101 * As long as we aren't aborting and the job didn't return a non-zero
1102 * status that we shouldn't ignore, we call Make_Update to update
1105 JobSaveCommands(job);
1106 job->node->made = MADE;
1107 if (!(job->flags & JOB_SPECIAL))
1108 return_job_token = TRUE;
1109 Make_Update(job->node);
1110 job->status = JOB_ST_FREE;
1111 } else if (WAIT_STATUS(status)) {
1113 job->status = JOB_ST_FREE;
1116 if (errors > 0 && !opts.keepgoing && aborting != ABORT_INTERRUPT)
1117 aborting = ABORT_ERROR; /* Prevent more jobs from getting started. */
1119 if (return_job_token)
1122 if (aborting == ABORT_ERROR && jobTokensRunning == 0)
1127 TouchRegular(GNode *gn)
1129 const char *file = GNode_Path(gn);
1130 struct utimbuf times = { now, now };
1134 if (utime(file, ×) >= 0)
1137 fd = open(file, O_RDWR | O_CREAT, 0666);
1139 (void)fprintf(stderr, "*** couldn't touch %s: %s\n",
1140 file, strerror(errno));
1141 (void)fflush(stderr);
1142 return; /* XXX: What about propagating the error? */
1145 /* Last resort: update the file's time stamps in the traditional way.
1146 * XXX: This doesn't work for empty files, which are sometimes used
1147 * as marker files. */
1148 if (read(fd, &c, 1) == 1) {
1149 (void)lseek(fd, 0, SEEK_SET);
1150 while (write(fd, &c, 1) == -1 && errno == EAGAIN)
1153 (void)close(fd); /* XXX: What about propagating the error? */
1156 /* Touch the given target. Called by JobStart when the -t flag was given.
1158 * The modification date of the file is changed.
1159 * If the file did not exist, it is created. */
1161 Job_Touch(GNode *gn, Boolean silent)
1163 if (gn->type & (OP_JOIN|OP_USE|OP_USEBEFORE|OP_EXEC|OP_OPTIONAL|
1164 OP_SPECIAL|OP_PHONY)) {
1165 /* These are "virtual" targets and should not really be created. */
1169 if (!silent || !GNode_ShouldExecute(gn)) {
1170 (void)fprintf(stdout, "touch %s\n", gn->name);
1171 (void)fflush(stdout);
1174 if (!GNode_ShouldExecute(gn))
1177 if (gn->type & OP_ARCHV) {
1182 if (gn->type & OP_LIB) {
1190 /* Make sure the given node has all the commands it needs.
1192 * The node will have commands from the .DEFAULT rule added to it if it
1196 * gn The target whose commands need verifying
1197 * abortProc Function to abort with message
1200 * TRUE if the commands list is/was ok.
1203 Job_CheckCommands(GNode *gn, void (*abortProc)(const char *, ...))
1205 if (GNode_IsTarget(gn))
1207 if (!Lst_IsEmpty(gn->commands))
1209 if ((gn->type & OP_LIB) && !Lst_IsEmpty(gn->children))
1213 * No commands. Look for .DEFAULT rule from which we might infer
1216 if (defaultNode != NULL && !Lst_IsEmpty(defaultNode->commands) &&
1217 !(gn->type & OP_SPECIAL)) {
1219 * The traditional Make only looks for a .DEFAULT if the node was
1220 * never the target of an operator, so that's what we do too.
1222 * The .DEFAULT node acts like a transformation rule, in that
1223 * gn also inherits any attributes or sources attached to
1226 Make_HandleUse(defaultNode, gn);
1227 Var_Set(IMPSRC, GNode_VarTarget(gn), gn);
1231 Dir_UpdateMTime(gn, FALSE);
1232 if (gn->mtime != 0 || (gn->type & OP_SPECIAL))
1236 * The node wasn't the target of an operator. We have no .DEFAULT
1237 * rule to go on and the target doesn't already exist. There's
1238 * nothing more we can do for this branch. If the -k flag wasn't
1239 * given, we stop in our tracks, otherwise we just don't update
1240 * this node's parents so they never get examined.
1243 if (gn->flags & FROM_DEPEND) {
1244 if (!Job_RunTarget(".STALE", gn->fname))
1245 fprintf(stdout, "%s: %s, %d: ignoring stale %s for %s\n",
1246 progname, gn->fname, gn->lineno, makeDependfile,
1251 if (gn->type & OP_OPTIONAL) {
1252 (void)fprintf(stdout, "%s: don't know how to make %s (%s)\n",
1253 progname, gn->name, "ignored");
1254 (void)fflush(stdout);
1258 if (opts.keepgoing) {
1259 (void)fprintf(stdout, "%s: don't know how to make %s (%s)\n",
1260 progname, gn->name, "continuing");
1261 (void)fflush(stdout);
1265 abortProc("%s: don't know how to make %s. Stop", progname, gn->name);
1269 /* Execute the shell for the given job.
1271 * See Job_CatchOutput for handling the output of the shell. */
1273 JobExec(Job *job, char **argv)
1275 int cpid; /* ID of new child */
1278 job->flags &= ~JOB_TRACED;
1283 debug_printf("Running %s\n", job->node->name);
1284 debug_printf("\tCommand: ");
1285 for (i = 0; argv[i] != NULL; i++) {
1286 debug_printf("%s ", argv[i]);
1292 * Some jobs produce no output and it's disconcerting to have
1293 * no feedback of their running (since they produce no output, the
1294 * banner with their name in it never appears). This is an attempt to
1295 * provide that feedback, even if nothing follows it.
1297 if ((lastNode != job->node) && !(job->flags & JOB_SILENT)) {
1298 MESSAGE(stdout, job->node);
1299 lastNode = job->node;
1302 /* No interruptions until this job is on the `jobs' list */
1305 /* Pre-emptively mark job running, pid still zero though */
1306 job->status = JOB_ST_RUNNING;
1310 Punt("Cannot vfork: %s", strerror(errno));
1318 meta_job_child(job);
1322 * Reset all signal handlers; this is necessary because we also
1323 * need to unblock signals before we exec(2).
1327 /* Now unblock signals */
1328 sigemptyset(&tmask);
1329 JobSigUnlock(&tmask);
1332 * Must duplicate the input stream down to the child's input and
1333 * reset it to the beginning (again). Since the stream was marked
1334 * close-on-exec, we must clear that bit in the new input.
1336 if (dup2(fileno(job->cmdFILE), 0) == -1)
1337 execDie("dup2", "job->cmdFILE");
1338 if (fcntl(0, F_SETFD, 0) == -1)
1339 execDie("fcntl clear close-on-exec", "stdin");
1340 if (lseek(0, 0, SEEK_SET) == -1)
1341 execDie("lseek to 0", "stdin");
1343 if (Always_pass_job_queue ||
1344 (job->node->type & (OP_MAKE | OP_SUBMAKE))) {
1346 * Pass job token pipe to submakes.
1348 if (fcntl(tokenWaitJob.inPipe, F_SETFD, 0) == -1)
1349 execDie("clear close-on-exec", "tokenWaitJob.inPipe");
1350 if (fcntl(tokenWaitJob.outPipe, F_SETFD, 0) == -1)
1351 execDie("clear close-on-exec", "tokenWaitJob.outPipe");
1355 * Set up the child's output to be routed through the pipe
1356 * we've created for it.
1358 if (dup2(job->outPipe, 1) == -1)
1359 execDie("dup2", "job->outPipe");
1362 * The output channels are marked close on exec. This bit was
1363 * duplicated by the dup2(on some systems), so we have to clear
1364 * it before routing the shell's error output to the same place as
1365 * its standard output.
1367 if (fcntl(1, F_SETFD, 0) == -1)
1368 execDie("clear close-on-exec", "stdout");
1369 if (dup2(1, 2) == -1)
1370 execDie("dup2", "1, 2");
1373 * We want to switch the child into a different process family so
1374 * we can kill it and all its descendants in one fell swoop,
1375 * by killing its process family, but not commit suicide.
1377 #if defined(HAVE_SETPGID)
1378 (void)setpgid(0, getpid());
1380 #if defined(HAVE_SETSID)
1381 /* XXX: dsl - I'm sure this should be setpgrp()... */
1384 (void)setpgrp(0, getpid());
1390 (void)execv(shellPath, argv);
1391 execDie("exec", shellPath);
1394 /* Parent, continuing after the child exec */
1397 Trace_Log(JOBSTART, job);
1401 meta_job_parent(job, cpid);
1406 * Set the current position in the buffer to the beginning
1407 * and mark another stream to watch in the outputs mask
1413 if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
1414 (void)fclose(job->cmdFILE);
1415 job->cmdFILE = NULL;
1419 * Now the job is actually running, add it to the table.
1422 debug_printf("JobExec(%s): pid %d added to jobs table\n",
1423 job->node->name, job->pid);
1424 job_table_dump("job started");
1426 JobSigUnlock(&mask);
1429 /* Create the argv needed to execute the shell for a given job. */
1431 JobMakeArgv(Job *job, char **argv)
1434 static char args[10]; /* For merged arguments */
1436 argv[0] = UNCONST(shellName);
1439 if ((commandShell->exit && commandShell->exit[0] != '-') ||
1440 (commandShell->echo && commandShell->echo[0] != '-'))
1443 * At least one of the flags doesn't have a minus before it, so
1444 * merge them together. Have to do this because the *(&(@*#*&#$#
1445 * Bourne shell thinks its second argument is a file to source.
1446 * Grrrr. Note the ten-character limitation on the combined arguments.
1448 (void)snprintf(args, sizeof args, "-%s%s",
1449 ((job->flags & JOB_IGNERR) ? "" :
1450 (commandShell->exit ? commandShell->exit : "")),
1451 ((job->flags & JOB_SILENT) ? "" :
1452 (commandShell->echo ? commandShell->echo : "")));
1459 if (!(job->flags & JOB_IGNERR) && commandShell->exit) {
1460 argv[argc] = UNCONST(commandShell->exit);
1463 if (!(job->flags & JOB_SILENT) && commandShell->echo) {
1464 argv[argc] = UNCONST(commandShell->echo);
1472 *-----------------------------------------------------------------------
1474 * Start a target-creation process going for the target described
1475 * by the graph node gn.
1478 * gn target to create
1479 * flags flags for the job to override normal ones.
1480 * previous The previous Job structure for this node, if any.
1483 * JOB_ERROR if there was an error in the commands, JOB_FINISHED
1484 * if there isn't actually anything left to do for the job and
1485 * JOB_RUNNING if the job has been started.
1488 * A new Job node is created and added to the list of running
1489 * jobs. PMake is forked and a child shell created.
1491 * NB: The return value is ignored by everyone.
1492 *-----------------------------------------------------------------------
1494 static JobStartResult
1495 JobStart(GNode *gn, JobFlags flags)
1497 Job *job; /* new job descriptor */
1498 char *argv[10]; /* Argument vector to shell */
1499 Boolean cmdsOK; /* true if the nodes commands were all right */
1500 Boolean noExec; /* Set true if we decide not to run the job */
1501 int tfd; /* File descriptor to the temp file */
1503 for (job = job_table; job < job_table_end; job++) {
1504 if (job->status == JOB_ST_FREE)
1507 if (job >= job_table_end)
1508 Punt("JobStart no job slots vacant");
1510 memset(job, 0, sizeof *job);
1512 job->tailCmds = NULL;
1513 job->status = JOB_ST_SET_UP;
1515 if (gn->type & OP_SPECIAL)
1516 flags |= JOB_SPECIAL;
1517 if (Targ_Ignore(gn))
1518 flags |= JOB_IGNERR;
1519 if (Targ_Silent(gn))
1520 flags |= JOB_SILENT;
1524 * Check the commands now so any attributes from .DEFAULT have a chance
1525 * to migrate to the node
1527 cmdsOK = Job_CheckCommands(gn, Error);
1529 job->inPollfd = NULL;
1531 * If the -n flag wasn't given, we open up OUR (not the child's)
1532 * temporary file to stuff commands in it. The thing is rd/wr so we don't
1533 * need to reopen it to feed it to the shell. If the -n flag *was* given,
1534 * we just set the file to be stdout. Cute, huh?
1536 if (((gn->type & OP_MAKE) && !opts.noRecursiveExecute) ||
1537 (!opts.noExecute && !opts.touchFlag)) {
1539 * tfile is the name of a file into which all shell commands are
1540 * put. It is removed before the child shell is executed, unless
1541 * DEBUG(SCRIPT) is set.
1546 * We're serious here, but if the commands were bogus, we're
1550 PrintOnError(gn, NULL); /* provide some clue */
1555 tfd = mkTempFile(TMPPAT, &tfile);
1557 (void)eunlink(tfile);
1558 JobSigUnlock(&mask);
1560 job->cmdFILE = fdopen(tfd, "w+");
1561 if (job->cmdFILE == NULL)
1562 Punt("Could not fdopen %s", tfile);
1564 (void)fcntl(fileno(job->cmdFILE), F_SETFD, FD_CLOEXEC);
1566 * Send the commands to the command file, flush all its buffers then
1567 * rewind and remove the thing.
1573 meta_job_start(job, gn);
1574 if (Targ_Silent(gn)) /* might have changed */
1575 job->flags |= JOB_SILENT;
1579 * We can do all the commands at once. hooray for sanity
1582 JobPrintCommands(job);
1585 * If we didn't print out any commands to the shell script,
1586 * there's not much point in executing the shell, is there?
1588 if (numCommands == 0) {
1593 } else if (!GNode_ShouldExecute(gn)) {
1595 * Not executing anything -- just print all the commands to stdout
1596 * in one fell swoop. This will still set up job->tailCmds correctly.
1598 if (lastNode != gn) {
1599 MESSAGE(stdout, gn);
1602 job->cmdFILE = stdout;
1604 * Only print the commands if they're ok, but don't die if they're
1605 * not -- just let the user know they're bad and keep going. It
1606 * doesn't do any harm in this case and may do some good.
1609 JobPrintCommands(job);
1611 * Don't execute the shell, thank you.
1616 * Just touch the target and note that no shell should be executed.
1617 * Set cmdFILE to stdout to make life easier. Check the commands, too,
1618 * but don't die if they're no good -- it does no harm to keep working
1621 job->cmdFILE = stdout;
1622 Job_Touch(gn, job->flags & JOB_SILENT);
1625 /* Just in case it isn't already... */
1626 (void)fflush(job->cmdFILE);
1629 * If we're not supposed to execute a shell, don't.
1632 if (!(job->flags & JOB_SPECIAL))
1635 * Unlink and close the command file if we opened one
1637 if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
1638 (void)fclose(job->cmdFILE);
1639 job->cmdFILE = NULL;
1643 * We only want to work our way up the graph if we aren't here because
1644 * the commands for the job were no good.
1646 if (cmdsOK && aborting == ABORT_NONE) {
1647 JobSaveCommands(job);
1648 job->node->made = MADE;
1649 Make_Update(job->node);
1651 job->status = JOB_ST_FREE;
1652 return cmdsOK ? JOB_FINISHED : JOB_ERROR;
1656 * Set up the control arguments to the shell. This is based on the flags
1657 * set earlier for this job.
1659 JobMakeArgv(job, argv);
1661 /* Create the pipe by which we'll get the shell's output. */
1662 JobCreatePipe(job, 3);
1668 /* Print the output of the shell command, skipping the noPrint command of
1669 * the shell, if any. */
1671 JobOutput(Job *job, char *cp, char *endp)
1675 if (commandShell->noPrint == NULL || commandShell->noPrint[0] == '\0')
1678 while ((ecp = strstr(cp, commandShell->noPrint)) != NULL) {
1682 * The only way there wouldn't be a newline after
1683 * this line is if it were the last in the buffer.
1684 * however, since the non-printable comes after it,
1685 * there must be a newline, so we don't print one.
1687 (void)fprintf(stdout, "%s", cp);
1688 (void)fflush(stdout);
1690 cp = ecp + commandShell->noPrintLen;
1693 * Still more to print, look again after skipping
1694 * the whitespace following the non-printable
1698 pp_skip_whitespace(&cp);
1707 * This function is called whenever there is something to read on the pipe.
1708 * We collect more output from the given job and store it in the job's
1709 * outBuf. If this makes up a line, we print it tagged by the job's
1710 * identifier, as necessary.
1712 * In the output of the shell, the 'noPrint' lines are removed. If the
1713 * command is not alone on the line (the character after it is not \0 or
1714 * \n), we do print whatever follows it.
1717 * job the job whose output needs printing
1718 * finish TRUE if this is the last time we'll be called
1722 JobDoOutput(Job *job, Boolean finish)
1724 Boolean gotNL = FALSE; /* true if got a newline */
1725 Boolean fbuf; /* true if our buffer filled up */
1726 size_t nr; /* number of bytes read */
1727 size_t i; /* auxiliary index into outBuf */
1728 size_t max; /* limit for i (end of current data) */
1729 ssize_t nRead; /* (Temporary) number of bytes read */
1732 * Read as many bytes as will fit in the buffer.
1738 nRead = read(job->inPipe, &job->outBuf[job->curPos],
1739 JOB_BUFSIZE - job->curPos);
1741 if (errno == EAGAIN)
1744 perror("JobDoOutput(piperead)");
1752 * If we hit the end-of-file (the job is dead), we must flush its
1753 * remaining output, so pretend we read a newline if there's any
1754 * output remaining in the buffer.
1755 * Also clear the 'finish' flag so we stop looping.
1757 if (nr == 0 && job->curPos != 0) {
1758 job->outBuf[job->curPos] = '\n';
1761 } else if (nr == 0) {
1766 * Look for the last newline in the bytes we just got. If there is
1767 * one, break out of the loop with 'i' as its index and gotNL set
1770 max = job->curPos + nr;
1771 for (i = job->curPos + nr - 1; i >= job->curPos && i != (size_t)-1; i--) {
1772 if (job->outBuf[i] == '\n') {
1775 } else if (job->outBuf[i] == '\0') {
1779 job->outBuf[i] = ' ';
1785 if (job->curPos == JOB_BUFSIZE) {
1787 * If we've run out of buffer space, we have no choice
1788 * but to print the stuff. sigh.
1794 if (gotNL || fbuf) {
1796 * Need to send the output to the screen. Null terminate it
1797 * first, overwriting the newline character if there was one.
1798 * So long as the line isn't one we should filter (according
1799 * to the shell description), we print the line, preceded
1800 * by a target banner if this target isn't the same as the
1801 * one for which we last printed something.
1802 * The rest of the data in the buffer are then shifted down
1803 * to the start of the buffer and curPos is set accordingly.
1805 job->outBuf[i] = '\0';
1806 if (i >= job->curPos) {
1809 cp = JobOutput(job, job->outBuf, &job->outBuf[i]);
1812 * There's still more in that thar buffer. This time, though,
1813 * we know there's no newline at the end, so we add one of
1814 * our own free will.
1817 if (!opts.beSilent && job->node != lastNode) {
1818 MESSAGE(stdout, job->node);
1819 lastNode = job->node;
1823 meta_job_output(job, cp, gotNL ? "\n" : "");
1826 (void)fprintf(stdout, "%s%s", cp, gotNL ? "\n" : "");
1827 (void)fflush(stdout);
1831 * max is the last offset still in the buffer. Move any remaining
1832 * characters to the start of the buffer and update the end marker
1836 (void)memmove(job->outBuf, &job->outBuf[i + 1], max - (i + 1));
1837 job->curPos = max - (i + 1);
1845 * If the finish flag is true, we must loop until we hit
1846 * end-of-file on the pipe. This is guaranteed to happen
1847 * eventually since the other end of the pipe is now closed
1848 * (we closed it explicitly and the child has exited). When
1849 * we do get an EOF, finish will be set FALSE and we'll fall
1861 * Unfortunately it is too complicated to run .BEGIN, .END, and
1862 * .INTERRUPT job in the parallel job module. As of 2020-09-25,
1863 * unit-tests/deptgt-end-jobs.mk hangs in an endless loop.
1865 * Running these jobs in compat mode also guarantees that these
1866 * jobs do not overlap with other unrelated jobs.
1868 List *lst = Lst_New();
1869 Lst_Append(lst, targ);
1870 (void)Make_Run(lst);
1871 Lst_Destroy(lst, NULL);
1872 JobStart(targ, JOB_SPECIAL);
1873 while (jobTokensRunning) {
1877 Compat_Make(targ, targ);
1878 if (targ->made == ERROR) {
1879 PrintOnError(targ, "\n\nStop.");
1885 /* Handle the exit of a child. Called from Make_Make.
1887 * The job descriptor is removed from the list of children.
1890 * We do waits, blocking or not, according to the wisdom of our
1891 * caller, until there are no more children to report. For each
1892 * job, call JobFinish to finish things off.
1895 Job_CatchChildren(void)
1897 int pid; /* pid of dead child */
1898 WAIT_T status; /* Exit/termination status */
1901 * Don't even bother if we know there's no one around.
1903 if (jobTokensRunning == 0)
1906 while ((pid = waitpid((pid_t) -1, &status, WNOHANG | WUNTRACED)) > 0) {
1907 DEBUG2(JOB, "Process %d exited/stopped status %x.\n", pid,
1908 WAIT_STATUS(status));
1909 JobReapChild(pid, status, TRUE);
1914 * It is possible that wait[pid]() was called from elsewhere,
1915 * this lets us reap jobs regardless.
1918 JobReapChild(pid_t pid, WAIT_T status, Boolean isJobs)
1920 Job *job; /* job descriptor for dead child */
1923 * Don't even bother if we know there's no one around.
1925 if (jobTokensRunning == 0)
1928 job = JobFindPid(pid, JOB_ST_RUNNING, isJobs);
1931 if (!lurking_children)
1932 Error("Child (%d) status %x not in table?", pid, status);
1934 return; /* not ours */
1936 if (WIFSTOPPED(status)) {
1937 DEBUG2(JOB, "Process %d (%s) stopped.\n", job->pid, job->node->name);
1938 if (!make_suspended) {
1939 switch (WSTOPSIG(status)) {
1941 (void)printf("*** [%s] Suspended\n", job->node->name);
1944 (void)printf("*** [%s] Stopped\n", job->node->name);
1947 (void)printf("*** [%s] Stopped -- signal %d\n",
1948 job->node->name, WSTOPSIG(status));
1950 job->suspended = TRUE;
1952 (void)fflush(stdout);
1956 job->status = JOB_ST_FINISHED;
1957 job->exit_status = WAIT_STATUS(status);
1959 JobFinish(job, status);
1962 /* Catch the output from our children, if we're using pipes do so. Otherwise
1963 * just block time until we get a signal(most likely a SIGCHLD) since there's
1964 * no point in just spinning when there's nothing to do and the reaping of a
1965 * child can wait for a while. */
1967 Job_CatchOutput(void)
1973 (void)fflush(stdout);
1975 /* The first fd in the list is the job token pipe */
1977 nready = poll(fds + 1 - wantToken, nfds - 1 + wantToken, POLL_MSEC);
1978 } while (nready < 0 && errno == EINTR);
1981 Punt("poll: %s", strerror(errno));
1983 if (nready > 0 && readyfd(&childExitJob)) {
1986 count = read(childExitJob.inPipe, &token, 1);
1989 Punt("unexpected eof on token pipe");
1991 Punt("token pipe read: %s", strerror(errno));
1993 if (token == DO_JOB_RESUME[0])
1994 /* Complete relay requested from our SIGCONT handler */
2003 Job_CatchChildren();
2007 for (i = npseudojobs * nfds_per_job(); i < nfds; i++) {
2008 if (!fds[i].revents)
2011 if (job->status == JOB_ST_RUNNING)
2012 JobDoOutput(job, FALSE);
2013 #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
2015 * With meta mode, we may have activity on the job's filemon
2016 * descriptor too, which at the moment is any pollfd other than
2019 if (useMeta && job->inPollfd != &fds[i]) {
2020 if (meta_job_event(job) <= 0) {
2021 fds[i].events = 0; /* never mind */
2030 /* Start the creation of a target. Basically a front-end for JobStart used by
2031 * the Make module. */
2035 (void)JobStart(gn, JOB_NONE);
2041 if (shellPath == NULL) {
2043 * We are using the default shell, which may be an absolute
2044 * path if DEFSHELL_CUSTOM is defined.
2046 shellName = commandShell->name;
2047 #ifdef DEFSHELL_CUSTOM
2048 if (*shellName == '/') {
2049 shellPath = shellName;
2050 shellName = strrchr(shellPath, '/');
2054 shellPath = str_concat3(_PATH_DEFSHELLDIR, "/", shellName);
2056 Var_SetWithFlags(".SHELL", shellPath, VAR_CMDLINE, VAR_SET_READONLY);
2057 if (commandShell->exit == NULL) {
2058 commandShell->exit = "";
2060 if (commandShell->echo == NULL) {
2061 commandShell->echo = "";
2063 if (commandShell->hasErrCtl && commandShell->exit[0] != '\0') {
2065 strcmp(commandShell->exit, &shellErrFlag[1]) != 0) {
2067 shellErrFlag = NULL;
2069 if (!shellErrFlag) {
2070 size_t n = strlen(commandShell->exit) + 2;
2072 shellErrFlag = bmake_malloc(n);
2074 snprintf(shellErrFlag, n, "-%s", commandShell->exit);
2077 } else if (shellErrFlag) {
2079 shellErrFlag = NULL;
2083 /* Return the string literal that is used in the current command shell
2084 * to produce a newline character. */
2086 Shell_GetNewline(void)
2088 return commandShell->newline;
2096 } else if (!Var_Exists(MAKE_JOB_PREFIX, VAR_GLOBAL)) {
2097 Var_Set(MAKE_JOB_PREFIX, "---", VAR_GLOBAL);
2100 (void)Var_Subst("${" MAKE_JOB_PREFIX "}",
2101 VAR_GLOBAL, VARE_WANTRES, &targPrefix);
2102 /* TODO: handle errors */
2105 /* Initialize the process module. */
2110 /* Allocate space for all the job info */
2111 job_table = bmake_malloc((size_t)opts.maxJobs * sizeof *job_table);
2112 memset(job_table, 0, (size_t)opts.maxJobs * sizeof *job_table);
2113 job_table_end = job_table + opts.maxJobs;
2116 aborting = ABORT_NONE;
2121 Always_pass_job_queue = GetBooleanVar(MAKE_ALWAYS_PASS_JOB_QUEUE,
2122 Always_pass_job_queue);
2124 Job_error_token = GetBooleanVar(MAKE_JOB_ERROR_TOKEN, Job_error_token);
2128 * There is a non-zero chance that we already have children.
2129 * eg after 'make -f- <<EOF'
2130 * Since their termination causes a 'Child (pid) not in table' message,
2131 * Collect the status of any that are already dead, and suppress the
2132 * error message if there are any undead ones.
2136 rval = waitpid((pid_t) -1, &status, WNOHANG);
2140 lurking_children = TRUE;
2146 JobCreatePipe(&childExitJob, 3);
2148 /* Preallocate enough for the maximum number of jobs. */
2149 fds = bmake_malloc(sizeof *fds *
2150 (npseudojobs + (size_t)opts.maxJobs) * nfds_per_job());
2151 jobfds = bmake_malloc(sizeof *jobfds *
2152 (npseudojobs + (size_t)opts.maxJobs) * nfds_per_job());
2154 /* These are permanent entries and take slots 0 and 1 */
2155 watchfd(&tokenWaitJob);
2156 watchfd(&childExitJob);
2158 sigemptyset(&caught_signals);
2160 * Install a SIGCHLD handler.
2162 (void)bmake_signal(SIGCHLD, JobChildSig);
2163 sigaddset(&caught_signals, SIGCHLD);
2165 #define ADDSIG(s,h) \
2166 if (bmake_signal(s, SIG_IGN) != SIG_IGN) { \
2167 sigaddset(&caught_signals, s); \
2168 (void)bmake_signal(s, h); \
2172 * Catch the four signals that POSIX specifies if they aren't ignored.
2173 * JobPassSig will take care of calling JobInterrupt if appropriate.
2175 ADDSIG(SIGINT, JobPassSig_int)
2176 ADDSIG(SIGHUP, JobPassSig_term)
2177 ADDSIG(SIGTERM, JobPassSig_term)
2178 ADDSIG(SIGQUIT, JobPassSig_term)
2181 * There are additional signals that need to be caught and passed if
2182 * either the export system wants to be told directly of signals or if
2183 * we're giving each job its own process group (since then it won't get
2184 * signals from the terminal driver as we own the terminal)
2186 ADDSIG(SIGTSTP, JobPassSig_suspend)
2187 ADDSIG(SIGTTOU, JobPassSig_suspend)
2188 ADDSIG(SIGTTIN, JobPassSig_suspend)
2189 ADDSIG(SIGWINCH, JobCondPassSig)
2190 ADDSIG(SIGCONT, JobContinueSig)
2193 (void)Job_RunTarget(".BEGIN", NULL);
2194 /* Create the .END node now, even though no code in the unit tests
2195 * depends on it. See also Targ_GetEndNode in Compat_Run. */
2196 (void)Targ_GetEndNode();
2199 static void JobSigReset(void)
2202 if (sigismember(&caught_signals, s)) { \
2203 (void)bmake_signal(s, SIG_DFL); \
2216 (void)bmake_signal(SIGCHLD, SIG_DFL);
2219 /* Find a shell in 'shells' given its name, or return NULL. */
2221 FindShellByName(const char *name)
2224 const Shell *shellsEnd = sh + sizeof shells / sizeof shells[0];
2226 for (sh = shells; sh < shellsEnd; sh++) {
2227 if (strcmp(name, sh->name) == 0)
2234 *-----------------------------------------------------------------------
2236 * Parse a shell specification and set up commandShell, shellPath
2237 * and shellName appropriately.
2240 * line The shell spec
2243 * FALSE if the specification was incorrect.
2246 * commandShell points to a Shell structure (either predefined or
2247 * created from the shell spec), shellPath is the full path of the
2248 * shell described by commandShell, while shellName is just the
2249 * final component of shellPath.
2252 * A shell specification consists of a .SHELL target, with dependency
2253 * operator, followed by a series of blank-separated words. Double
2254 * quotes can be used to use blanks in words. A backslash escapes
2255 * anything (most notably a double-quote and a space) and
2256 * provides the functionality it does in C. Each word consists of
2257 * keyword and value separated by an equal sign. There should be no
2258 * unnecessary spaces in the word. The keywords are as follows:
2259 * name Name of shell.
2260 * path Location of shell.
2261 * quiet Command to turn off echoing.
2262 * echo Command to turn echoing on
2263 * filter Result of turning off echoing that shouldn't be
2265 * echoFlag Flag to turn echoing on at the start
2266 * errFlag Flag to turn error checking on at the start
2267 * hasErrCtl True if shell has error checking control
2268 * newline String literal to represent a newline char
2269 * check Command to turn on error checking if hasErrCtl
2270 * is TRUE or template of command to echo a command
2271 * for which error checking is off if hasErrCtl is
2273 * ignore Command to turn off error checking if hasErrCtl
2274 * is TRUE or template of command to execute a
2275 * command so as to ignore any errors it returns if
2276 * hasErrCtl is FALSE.
2278 *-----------------------------------------------------------------------
2281 Job_ParseShell(char *line)
2289 Boolean fullSpec = FALSE;
2292 pp_skip_whitespace(&line);
2296 memset(&newShell, 0, sizeof newShell);
2299 * Parse the specification by keyword
2301 wordsList = Str_Words(line, TRUE);
2302 words = wordsList.words;
2303 argc = wordsList.len;
2304 path = wordsList.freeIt;
2305 if (words == NULL) {
2306 Error("Unterminated quoted string [%s]", line);
2311 for (path = NULL, argv = words; argc != 0; argc--, argv++) {
2313 if (strncmp(arg, "path=", 5) == 0) {
2315 } else if (strncmp(arg, "name=", 5) == 0) {
2316 newShell.name = arg + 5;
2318 if (strncmp(arg, "quiet=", 6) == 0) {
2319 newShell.echoOff = arg + 6;
2320 } else if (strncmp(arg, "echo=", 5) == 0) {
2321 newShell.echoOn = arg + 5;
2322 } else if (strncmp(arg, "filter=", 7) == 0) {
2323 newShell.noPrint = arg + 7;
2324 newShell.noPrintLen = strlen(newShell.noPrint);
2325 } else if (strncmp(arg, "echoFlag=", 9) == 0) {
2326 newShell.echo = arg + 9;
2327 } else if (strncmp(arg, "errFlag=", 8) == 0) {
2328 newShell.exit = arg + 8;
2329 } else if (strncmp(arg, "hasErrCtl=", 10) == 0) {
2331 newShell.hasErrCtl = c == 'Y' || c == 'y' ||
2332 c == 'T' || c == 't';
2333 } else if (strncmp(arg, "newline=", 8) == 0) {
2334 newShell.newline = arg + 8;
2335 } else if (strncmp(arg, "check=", 6) == 0) {
2336 newShell.errOnOrEcho = arg + 6;
2337 } else if (strncmp(arg, "ignore=", 7) == 0) {
2338 newShell.errOffOrExecIgnore = arg + 7;
2339 } else if (strncmp(arg, "errout=", 7) == 0) {
2340 newShell.errExit = arg + 7;
2341 } else if (strncmp(arg, "comment=", 8) == 0) {
2342 newShell.commentChar = arg[8];
2344 Parse_Error(PARSE_FATAL, "Unknown keyword \"%s\"", arg);
2354 * If no path was given, the user wants one of the pre-defined shells,
2355 * yes? So we find the one s/he wants with the help of FindShellByName
2356 * and set things up the right way. shellPath will be set up by
2359 if (newShell.name == NULL) {
2360 Parse_Error(PARSE_FATAL, "Neither path nor name specified");
2364 if ((sh = FindShellByName(newShell.name)) == NULL) {
2365 Parse_Error(PARSE_WARNING, "%s: No matching shell",
2371 shellName = newShell.name;
2373 /* Shell_Init has already been called! Do it again. */
2374 free(UNCONST(shellPath));
2381 * The user provided a path. If s/he gave nothing else (fullSpec is
2382 * FALSE), try and find a matching shell in the ones we know of.
2383 * Else we just take the specification at its word and copy it
2384 * to a new location. In either case, we need to record the
2385 * path the user gave for the shell.
2388 path = strrchr(path, '/');
2390 path = UNCONST(shellPath);
2394 if (newShell.name != NULL) {
2395 shellName = newShell.name;
2400 if ((sh = FindShellByName(shellName)) == NULL) {
2401 Parse_Error(PARSE_WARNING, "%s: No matching shell",
2408 commandShell = bmake_malloc(sizeof *commandShell);
2409 *commandShell = newShell;
2411 /* this will take care of shellErrFlag */
2415 if (commandShell->echoOn && commandShell->echoOff) {
2416 commandShell->hasEchoCtl = TRUE;
2419 if (!commandShell->hasErrCtl) {
2420 if (commandShell->errOnOrEcho == NULL) {
2421 commandShell->errOnOrEcho = "";
2423 if (commandShell->errOffOrExecIgnore == NULL) {
2424 commandShell->errOffOrExecIgnore = "%s\n";
2429 * Do not free up the words themselves, since they might be in use by the
2430 * shell specification.
2436 /* Handle the receipt of an interrupt.
2438 * All children are killed. Another job will be started if the .INTERRUPT
2439 * target is defined.
2442 * runINTERRUPT Non-zero if commands for the .INTERRUPT target
2443 * should be executed
2444 * signo signal received
2447 JobInterrupt(int runINTERRUPT, int signo)
2449 Job *job; /* job descriptor in that element */
2450 GNode *interrupt; /* the node describing the .INTERRUPT target */
2454 aborting = ABORT_INTERRUPT;
2458 for (job = job_table; job < job_table_end; job++) {
2459 if (job->status != JOB_ST_RUNNING)
2464 JobDeleteTarget(gn);
2466 DEBUG2(JOB, "JobInterrupt passing signal %d to child %d.\n",
2468 KILLPG(job->pid, signo);
2472 JobSigUnlock(&mask);
2474 if (runINTERRUPT && !opts.touchFlag) {
2475 interrupt = Targ_FindNode(".INTERRUPT");
2476 if (interrupt != NULL) {
2477 opts.ignoreErrors = FALSE;
2481 Trace_Log(MAKEINTR, NULL);
2485 /* Do the final processing, i.e. run the commands attached to the .END target.
2487 * Return the number of errors reported. */
2491 GNode *endNode = Targ_GetEndNode();
2492 if (!Lst_IsEmpty(endNode->commands) || !Lst_IsEmpty(endNode->children)) {
2494 Error("Errors reported so .END ignored");
2502 /* Clean up any memory used by the jobs module. */
2511 /* Waits for all running jobs to finish and returns.
2512 * Sets 'aborting' to ABORT_WAIT to prevent other jobs from starting. */
2516 aborting = ABORT_WAIT;
2517 while (jobTokensRunning != 0) {
2520 aborting = ABORT_NONE;
2523 /* Abort all currently running jobs without handling output or anything.
2524 * This function is to be called only in the event of a major error.
2525 * Most definitely NOT to be called from JobInterrupt.
2527 * All children are killed, not just the firstborn. */
2531 Job *job; /* the job descriptor in that element */
2534 aborting = ABORT_ERROR;
2536 if (jobTokensRunning) {
2537 for (job = job_table; job < job_table_end; job++) {
2538 if (job->status != JOB_ST_RUNNING)
2541 * kill the child process with increasingly drastic signals to make
2542 * darn sure it's dead.
2544 KILLPG(job->pid, SIGINT);
2545 KILLPG(job->pid, SIGKILL);
2550 * Catch as many children as want to report in at first, then give up
2552 while (waitpid((pid_t) -1, &foo, WNOHANG) > 0)
2556 /* Tries to restart stopped jobs if there are slots available.
2557 * Called in process context in response to a SIGCONT. */
2559 JobRestartJobs(void)
2563 for (job = job_table; job < job_table_end; job++) {
2564 if (job->status == JOB_ST_RUNNING &&
2565 (make_suspended || job->suspended)) {
2566 DEBUG1(JOB, "Restarting stopped job pid %d.\n", job->pid);
2567 if (job->suspended) {
2568 (void)printf("*** [%s] Continued\n", job->node->name);
2569 (void)fflush(stdout);
2571 job->suspended = FALSE;
2572 if (KILLPG(job->pid, SIGCONT) != 0 && DEBUG(JOB)) {
2573 debug_printf("Failed to send SIGCONT to %d\n", job->pid);
2576 if (job->status == JOB_ST_FINISHED)
2577 /* Job exit deferred after calling waitpid() in a signal handler */
2578 JobFinish(job, job->exit_status);
2580 make_suspended = FALSE;
2586 if (job->inPollfd != NULL)
2587 Punt("Watching watched job");
2589 fds[nfds].fd = job->inPipe;
2590 fds[nfds].events = POLLIN;
2592 job->inPollfd = &fds[nfds];
2594 #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
2596 fds[nfds].fd = meta_job_fd(job);
2597 fds[nfds].events = fds[nfds].fd == -1 ? 0 : POLLIN;
2608 if (job->inPollfd == NULL)
2609 Punt("Unwatching unwatched job");
2610 i = (size_t)(job->inPollfd - fds);
2612 #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
2615 * Sanity check: there should be two fds per job, so the job's
2616 * pollfd number should be even.
2618 assert(nfds_per_job() == 2);
2620 Punt("odd-numbered fd with meta");
2625 * Move last job in table into hole made by dead job.
2629 jobfds[i] = jobfds[nfds];
2630 jobfds[i]->inPollfd = &fds[i];
2631 #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
2633 fds[i + 1] = fds[nfds + 1];
2634 jobfds[i + 1] = jobfds[nfds + 1];
2638 job->inPollfd = NULL;
2644 if (job->inPollfd == NULL)
2645 Punt("Polling unwatched job");
2646 return (job->inPollfd->revents & POLLIN) != 0;
2649 /* Put a token (back) into the job pipe.
2650 * This allows a make process to start a build job. */
2654 char tok = JOB_TOKENS[aborting], tok1;
2656 if (!Job_error_token && aborting == ABORT_ERROR) {
2657 if (jobTokensRunning == 0)
2659 tok = '+'; /* no error token */
2662 /* If we are depositing an error token flush everything else */
2663 while (tok != '+' && read(tokenWaitJob.inPipe, &tok1, 1) == 1)
2666 DEBUG3(JOB, "(%d) aborting %d, deposit token %c\n",
2667 getpid(), aborting, tok);
2668 while (write(tokenWaitJob.outPipe, &tok, 1) == -1 && errno == EAGAIN)
2672 /* Prep the job token pipe in the root make process. */
2674 Job_ServerStart(int max_tokens, int jp_0, int jp_1)
2679 if (jp_0 >= 0 && jp_1 >= 0) {
2680 /* Pipe passed in from parent */
2681 tokenWaitJob.inPipe = jp_0;
2682 tokenWaitJob.outPipe = jp_1;
2683 (void)fcntl(jp_0, F_SETFD, FD_CLOEXEC);
2684 (void)fcntl(jp_1, F_SETFD, FD_CLOEXEC);
2688 JobCreatePipe(&tokenWaitJob, 15);
2690 snprintf(jobarg, sizeof jobarg, "%d,%d",
2691 tokenWaitJob.inPipe, tokenWaitJob.outPipe);
2693 Var_Append(MAKEFLAGS, "-J", VAR_GLOBAL);
2694 Var_Append(MAKEFLAGS, jobarg, VAR_GLOBAL);
2697 * Preload the job pipe with one token per job, save the one
2698 * "extra" token for the primary job.
2700 * XXX should clip maxJobs against PIPE_BUF -- if max_tokens is
2701 * larger than the write buffer size of the pipe, we will
2704 for (i = 1; i < max_tokens; i++)
2708 /* Return a withdrawn token to the pool. */
2710 Job_TokenReturn(void)
2713 if (jobTokensRunning < 0)
2714 Punt("token botch");
2715 if (jobTokensRunning || JOB_TOKENS[aborting] != '+')
2719 /* Attempt to withdraw a token from the pool.
2721 * If pool is empty, set wantToken so that we wake up when a token is
2724 * Returns TRUE if a token was withdrawn, and FALSE if the pool is currently
2727 Job_TokenWithdraw(void)
2733 DEBUG3(JOB, "Job_TokenWithdraw(%d): aborting %d, running %d\n",
2734 getpid(), aborting, jobTokensRunning);
2736 if (aborting != ABORT_NONE || (jobTokensRunning >= opts.maxJobs))
2739 count = read(tokenWaitJob.inPipe, &tok, 1);
2741 Fatal("eof on job pipe!");
2742 if (count < 0 && jobTokensRunning != 0) {
2743 if (errno != EAGAIN) {
2744 Fatal("job pipe read: %s", strerror(errno));
2746 DEBUG1(JOB, "(%d) blocked for token\n", getpid());
2750 if (count == 1 && tok != '+') {
2751 /* make being abvorted - remove any other job tokens */
2752 DEBUG2(JOB, "(%d) aborted by token %c\n", getpid(), tok);
2753 while (read(tokenWaitJob.inPipe, &tok1, 1) == 1)
2755 /* And put the stopper back */
2756 while (write(tokenWaitJob.outPipe, &tok, 1) == -1 && errno == EAGAIN)
2758 if (shouldDieQuietly(NULL, 1))
2760 Fatal("A failure has been detected in another branch of the parallel make");
2763 if (count == 1 && jobTokensRunning == 0)
2764 /* We didn't want the token really */
2765 while (write(tokenWaitJob.outPipe, &tok, 1) == -1 && errno == EAGAIN)
2769 DEBUG1(JOB, "(%d) withdrew token\n", getpid());
2773 /* Run the named target if found. If a filename is specified, then set that
2776 * Exits if the target fails. */
2778 Job_RunTarget(const char *target, const char *fname) {
2779 GNode *gn = Targ_FindNode(target);
2784 Var_Set(ALLSRC, fname, gn);
2787 if (gn->made == ERROR) {
2788 PrintOnError(gn, "\n\nStop.");
2796 emul_poll(struct pollfd *fd, int nfd, int timeout)
2799 int i, maxfd, nselect, npoll;
2800 struct timeval tv, *tvp;
2807 for (i = 0; i < nfd; i++) {
2810 if (fd[i].events & POLLIN)
2811 FD_SET(fd[i].fd, &rfds);
2813 if (fd[i].events & POLLOUT)
2814 FD_SET(fd[i].fd, &wfds);
2816 if (fd[i].fd > maxfd)
2820 if (maxfd >= FD_SETSIZE) {
2821 Punt("Ran out of fd_set slots; "
2822 "recompile with a larger FD_SETSIZE.");
2828 usecs = timeout * 1000;
2829 tv.tv_sec = usecs / 1000000;
2830 tv.tv_usec = usecs % 1000000;
2834 nselect = select(maxfd + 1, &rfds, &wfds, NULL, tvp);
2840 for (i = 0; i < nfd; i++) {
2841 if (FD_ISSET(fd[i].fd, &rfds))
2842 fd[i].revents |= POLLIN;
2844 if (FD_ISSET(fd[i].fd, &wfds))
2845 fd[i].revents |= POLLOUT;
2853 #endif /* USE_SELECT */