1 /* $NetBSD: job.c,v 1.420 2021/02/05 22:15:44 sjg Exp $ */ 2 3 /* 4 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Adam de Boor. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 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. 21 * 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 32 * SUCH DAMAGE. 33 */ 34 35 /* 36 * Copyright (c) 1988, 1989 by Adam de Boor 37 * Copyright (c) 1989 by Berkeley Softworks 38 * All rights reserved. 39 * 40 * This code is derived from software contributed to Berkeley by 41 * Adam de Boor. 42 * 43 * Redistribution and use in source and binary forms, with or without 44 * modification, are permitted provided that the following conditions 45 * are met: 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. 58 * 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 69 * SUCH DAMAGE. 70 */ 71 72 /* 73 * job.c -- 74 * handle the creation etc. of our child processes. 75 * 76 * Interface: 77 * Job_Init Called to initialize this module. In addition, 78 * the .BEGIN target is made including all of its 79 * dependencies before this function returns. 80 * Hence, the makefiles must have been parsed 81 * before this function is called. 82 * 83 * Job_End Clean up any memory used. 84 * 85 * Job_Make Start the creation of the given target. 86 * 87 * Job_CatchChildren 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. 93 * 94 * Job_CatchOutput 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. 101 * 102 * Job_ParseShell Given a special dependency line with target '.SHELL', 103 * define the shell that is used for the creation 104 * commands in jobs mode. 105 * 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. 111 * 112 * Job_AbortAll Abort all currently running jobs. Do not handle 113 * output or do anything for the jobs, just kill them. 114 * Should only be called in an emergency. 115 * 116 * Job_CheckCommands 117 * Verify that the commands for a target are 118 * ok. Provide them if necessary and possible. 119 * 120 * Job_Touch Update a target without really updating it. 121 * 122 * Job_Wait Wait for all currently-running jobs to finish. 123 */ 124 125 #ifdef HAVE_CONFIG_H 126 # include "config.h" 127 #endif 128 #include <sys/types.h> 129 #include <sys/stat.h> 130 #include <sys/file.h> 131 #include <sys/time.h> 132 #include "wait.h" 133 134 #include <errno.h> 135 #if !defined(USE_SELECT) && defined(HAVE_POLL_H) 136 #include <poll.h> 137 #else 138 #ifndef USE_SELECT /* no poll.h */ 139 # define USE_SELECT 140 #endif 141 #if defined(HAVE_SYS_SELECT_H) 142 # include <sys/select.h> 143 #endif 144 #endif 145 #include <signal.h> 146 #include <utime.h> 147 #if defined(HAVE_SYS_SOCKET_H) 148 # include <sys/socket.h> 149 #endif 150 151 #include "make.h" 152 #include "dir.h" 153 #include "job.h" 154 #include "pathnames.h" 155 #include "trace.h" 156 157 /* "@(#)job.c 8.2 (Berkeley) 3/19/94" */ 158 MAKE_RCSID("$NetBSD: job.c,v 1.420 2021/02/05 22:15:44 sjg Exp $"); 159 160 /* 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. 165 * 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. 170 * 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. 175 * 176 * The error checking for individual commands is controlled using hasErrCtl, 177 * errOn, errOff and runChkTmpl. 178 * 179 * In case a shell doesn't have error control, echoTmpl is a printf template 180 * for echoing the command, should echoing be on; runIgnTmpl is another 181 * printf template for executing the command while ignoring the return 182 * status. Finally runChkTmpl 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 unwanted 187 * shell code injection, the escaped command is safe to use in double quotes. 188 * 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 192 * the commands. 193 */ 194 typedef struct Shell { 195 196 /* 197 * The name of the shell. For Bourne and C shells, this is used only 198 * to find the shell description when used as the single source of a 199 * .SHELL target. For user-defined shells, this is the full path of 200 * the shell. 201 */ 202 const char *name; 203 204 Boolean hasEchoCtl; /* whether both echoOff and echoOn are there */ 205 const char *echoOff; /* command to turn echoing off */ 206 const char *echoOn; /* command to turn echoing back on */ 207 const char *noPrint; /* text to skip when printing output from the 208 * shell. This is usually the same as echoOff */ 209 size_t noPrintLen; /* length of noPrint command */ 210 211 Boolean hasErrCtl; /* whether error checking can be controlled 212 * for individual commands */ 213 const char *errOn; /* command to turn on error checking */ 214 const char *errOff; /* command to turn off error checking */ 215 216 const char *echoTmpl; /* template to echo a command */ 217 const char *runIgnTmpl; /* template to run a command 218 * without error checking */ 219 const char *runChkTmpl; /* template to run a command 220 * with error checking */ 221 222 /* string literal that results in a newline character when it appears 223 * outside of any 'quote' or "quote" characters */ 224 const char *newline; 225 char commentChar; /* character used by shell for comment lines */ 226 227 const char *echoFlag; /* shell flag to echo commands */ 228 const char *errFlag; /* shell flag to exit on error */ 229 } Shell; 230 231 typedef struct CommandFlags { 232 /* Whether to echo the command before or instead of running it. */ 233 Boolean echo; 234 235 /* Run the command even in -n or -N mode. */ 236 Boolean always; 237 238 /* 239 * true if we turned error checking off before printing the command 240 * and need to turn it back on 241 */ 242 Boolean ignerr; 243 } CommandFlags; 244 245 /* 246 * Write shell commands to a file. 247 * 248 * TODO: keep track of whether commands are echoed. 249 * TODO: keep track of whether error checking is active. 250 */ 251 typedef struct ShellWriter { 252 FILE *f; 253 254 /* we've sent 'set -x' */ 255 Boolean xtraced; 256 257 } ShellWriter; 258 259 /* 260 * error handling variables 261 */ 262 static int job_errors = 0; /* number of errors reported */ 263 typedef enum AbortReason { /* why is the make aborting? */ 264 ABORT_NONE, 265 ABORT_ERROR, /* Because of an error */ 266 ABORT_INTERRUPT, /* Because it was interrupted */ 267 ABORT_WAIT /* Waiting for jobs to finish */ 268 /* XXX: "WAIT" is not a _reason_ for aborting, it's rather a status. */ 269 } AbortReason; 270 static AbortReason aborting = ABORT_NONE; 271 #define JOB_TOKENS "+EI+" /* Token to requeue for each abort state */ 272 273 /* 274 * this tracks the number of tokens currently "out" to build jobs. 275 */ 276 int jobTokensRunning = 0; 277 278 typedef enum JobStartResult { 279 JOB_RUNNING, /* Job is running */ 280 JOB_ERROR, /* Error in starting the job */ 281 JOB_FINISHED /* The job is already finished */ 282 } JobStartResult; 283 284 /* 285 * Descriptions for various shells. 286 * 287 * The build environment may set DEFSHELL_INDEX to one of 288 * DEFSHELL_INDEX_SH, DEFSHELL_INDEX_KSH, or DEFSHELL_INDEX_CSH, to 289 * select one of the predefined shells as the default shell. 290 * 291 * Alternatively, the build environment may set DEFSHELL_CUSTOM to the 292 * name or the full path of a sh-compatible shell, which will be used as 293 * the default shell. 294 * 295 * ".SHELL" lines in Makefiles can choose the default shell from the 296 * set defined here, or add additional shells. 297 */ 298 299 #ifdef DEFSHELL_CUSTOM 300 #define DEFSHELL_INDEX_CUSTOM 0 301 #define DEFSHELL_INDEX_SH 1 302 #define DEFSHELL_INDEX_KSH 2 303 #define DEFSHELL_INDEX_CSH 3 304 #else /* !DEFSHELL_CUSTOM */ 305 #define DEFSHELL_INDEX_SH 0 306 #define DEFSHELL_INDEX_KSH 1 307 #define DEFSHELL_INDEX_CSH 2 308 #endif /* !DEFSHELL_CUSTOM */ 309 310 #ifndef DEFSHELL_INDEX 311 #define DEFSHELL_INDEX 0 /* DEFSHELL_INDEX_CUSTOM or DEFSHELL_INDEX_SH */ 312 #endif /* !DEFSHELL_INDEX */ 313 314 static Shell shells[] = { 315 #ifdef DEFSHELL_CUSTOM 316 /* 317 * An sh-compatible shell with a non-standard name. 318 * 319 * Keep this in sync with the "sh" description below, but avoid 320 * non-portable features that might not be supplied by all 321 * sh-compatible shells. 322 */ 323 { 324 DEFSHELL_CUSTOM, /* .name */ 325 FALSE, /* .hasEchoCtl */ 326 "", /* .echoOff */ 327 "", /* .echoOn */ 328 "", /* .noPrint */ 329 0, /* .noPrintLen */ 330 FALSE, /* .hasErrCtl */ 331 "", /* .errOn */ 332 "", /* .errOff */ 333 "echo \"%s\"\n", /* .echoTmpl */ 334 "%s\n", /* .runIgnTmpl */ 335 "{ %s \n} || exit $?\n", /* .runChkTmpl */ 336 "'\n'", /* .newline */ 337 '#', /* .commentChar */ 338 "", /* .echoFlag */ 339 "", /* .errFlag */ 340 }, 341 #endif /* DEFSHELL_CUSTOM */ 342 /* 343 * SH description. Echo control is also possible and, under 344 * sun UNIX anyway, one can even control error checking. 345 */ 346 { 347 "sh", /* .name */ 348 FALSE, /* .hasEchoCtl */ 349 "", /* .echoOff */ 350 "", /* .echoOn */ 351 "", /* .noPrint */ 352 0, /* .noPrintLen */ 353 FALSE, /* .hasErrCtl */ 354 "", /* .errOn */ 355 "", /* .errOff */ 356 "echo \"%s\"\n", /* .echoTmpl */ 357 "%s\n", /* .runIgnTmpl */ 358 "{ %s \n} || exit $?\n", /* .runChkTmpl */ 359 "'\n'", /* .newline */ 360 '#', /* .commentChar*/ 361 #if defined(MAKE_NATIVE) && defined(__NetBSD__) 362 /* XXX: -q is not really echoFlag, it's more like noEchoInSysFlag. */ 363 "q", /* .echoFlag */ 364 #else 365 "", /* .echoFlag */ 366 #endif 367 "", /* .errFlag */ 368 }, 369 /* 370 * KSH description. 371 */ 372 { 373 "ksh", /* .name */ 374 TRUE, /* .hasEchoCtl */ 375 "set +v", /* .echoOff */ 376 "set -v", /* .echoOn */ 377 "set +v", /* .noPrint */ 378 6, /* .noPrintLen */ 379 FALSE, /* .hasErrCtl */ 380 "", /* .errOn */ 381 "", /* .errOff */ 382 "echo \"%s\"\n", /* .echoTmpl */ 383 "%s\n", /* .runIgnTmpl */ 384 "{ %s \n} || exit $?\n", /* .runChkTmpl */ 385 "'\n'", /* .newline */ 386 '#', /* .commentChar */ 387 "v", /* .echoFlag */ 388 "", /* .errFlag */ 389 }, 390 /* 391 * CSH description. The csh can do echo control by playing 392 * with the setting of the 'echo' shell variable. Sadly, 393 * however, it is unable to do error control nicely. 394 */ 395 { 396 "csh", /* .name */ 397 TRUE, /* .hasEchoCtl */ 398 "unset verbose", /* .echoOff */ 399 "set verbose", /* .echoOn */ 400 "unset verbose", /* .noPrint */ 401 13, /* .noPrintLen */ 402 FALSE, /* .hasErrCtl */ 403 "", /* .errOn */ 404 "", /* .errOff */ 405 "echo \"%s\"\n", /* .echoTmpl */ 406 "csh -c \"%s || exit 0\"\n", /* .runIgnTmpl */ 407 "", /* .runChkTmpl */ 408 "'\\\n'", /* .newline */ 409 '#', /* .commentChar */ 410 "v", /* .echoFlag */ 411 "e", /* .errFlag */ 412 } 413 }; 414 415 /* 416 * This is the shell to which we pass all commands in the Makefile. 417 * It is set by the Job_ParseShell function. 418 */ 419 static Shell *shell = &shells[DEFSHELL_INDEX]; 420 const char *shellPath = NULL; /* full pathname of executable image */ 421 const char *shellName = NULL; /* last component of shellPath */ 422 char *shellErrFlag = NULL; 423 static char *shell_freeIt = NULL; /* Allocated memory for custom .SHELL */ 424 425 426 static Job *job_table; /* The structures that describe them */ 427 static Job *job_table_end; /* job_table + maxJobs */ 428 static unsigned int wantToken; /* we want a token */ 429 static Boolean lurking_children = FALSE; 430 static Boolean make_suspended = FALSE; /* Whether we've seen a SIGTSTP (etc) */ 431 432 /* 433 * Set of descriptors of pipes connected to 434 * the output channels of children 435 */ 436 static struct pollfd *fds = NULL; 437 static Job **jobByFdIndex = NULL; 438 static nfds_t fdsLen = 0; 439 static void watchfd(Job *); 440 static void clearfd(Job *); 441 static Boolean readyfd(Job *); 442 443 static char *targPrefix = NULL; /* To identify a job change in the output. */ 444 static Job tokenWaitJob; /* token wait pseudo-job */ 445 446 static Job childExitJob; /* child exit pseudo-job */ 447 #define CHILD_EXIT "." 448 #define DO_JOB_RESUME "R" 449 450 enum { 451 npseudojobs = 2 /* number of pseudo-jobs */ 452 }; 453 454 static sigset_t caught_signals; /* Set of signals we handle */ 455 static volatile sig_atomic_t caught_sigchld; 456 457 static void JobDoOutput(Job *, Boolean); 458 static void JobInterrupt(Boolean, int) MAKE_ATTR_DEAD; 459 static void JobRestartJobs(void); 460 static void JobSigReset(void); 461 462 static void 463 SwitchOutputTo(GNode *gn) 464 { 465 /* The node for which output was most recently produced. */ 466 static GNode *lastNode = NULL; 467 468 if (gn == lastNode) 469 return; 470 lastNode = gn; 471 472 if (opts.maxJobs != 1 && targPrefix != NULL && targPrefix[0] != '\0') 473 (void)fprintf(stdout, "%s %s ---\n", targPrefix, gn->name); 474 } 475 476 static unsigned 477 nfds_per_job(void) 478 { 479 #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV) 480 if (useMeta) 481 return 2; 482 #endif 483 return 1; 484 } 485 486 void 487 Job_FlagsToString(const Job *job, char *buf, size_t bufsize) 488 { 489 snprintf(buf, bufsize, "%c%c%c", 490 job->ignerr ? 'i' : '-', 491 !job->echo ? 's' : '-', 492 job->special ? 'S' : '-'); 493 } 494 495 static void 496 job_table_dump(const char *where) 497 { 498 Job *job; 499 char flags[4]; 500 501 debug_printf("job table @ %s\n", where); 502 for (job = job_table; job < job_table_end; job++) { 503 Job_FlagsToString(job, flags, sizeof flags); 504 debug_printf("job %d, status %d, flags %s, pid %d\n", 505 (int)(job - job_table), job->status, flags, job->pid); 506 } 507 } 508 509 /* 510 * Delete the target of a failed, interrupted, or otherwise 511 * unsuccessful job unless inhibited by .PRECIOUS. 512 */ 513 static void 514 JobDeleteTarget(GNode *gn) 515 { 516 const char *file; 517 518 if (gn->type & OP_JOIN) 519 return; 520 if (gn->type & OP_PHONY) 521 return; 522 if (Targ_Precious(gn)) 523 return; 524 if (opts.noExecute) 525 return; 526 527 file = GNode_Path(gn); 528 if (eunlink(file) != -1) 529 Error("*** %s removed", file); 530 } 531 532 /* 533 * JobSigLock/JobSigUnlock 534 * 535 * Signal lock routines to get exclusive access. Currently used to 536 * protect `jobs' and `stoppedJobs' list manipulations. 537 */ 538 static void 539 JobSigLock(sigset_t *omaskp) 540 { 541 if (sigprocmask(SIG_BLOCK, &caught_signals, omaskp) != 0) { 542 Punt("JobSigLock: sigprocmask: %s", strerror(errno)); 543 sigemptyset(omaskp); 544 } 545 } 546 547 static void 548 JobSigUnlock(sigset_t *omaskp) 549 { 550 (void)sigprocmask(SIG_SETMASK, omaskp, NULL); 551 } 552 553 static void 554 JobCreatePipe(Job *job, int minfd) 555 { 556 int i, fd, flags; 557 int pipe_fds[2]; 558 559 if (pipe(pipe_fds) == -1) 560 Punt("Cannot create pipe: %s", strerror(errno)); 561 562 for (i = 0; i < 2; i++) { 563 /* Avoid using low numbered fds */ 564 fd = fcntl(pipe_fds[i], F_DUPFD, minfd); 565 if (fd != -1) { 566 close(pipe_fds[i]); 567 pipe_fds[i] = fd; 568 } 569 } 570 571 job->inPipe = pipe_fds[0]; 572 job->outPipe = pipe_fds[1]; 573 574 /* Set close-on-exec flag for both */ 575 if (fcntl(job->inPipe, F_SETFD, FD_CLOEXEC) == -1) 576 Punt("Cannot set close-on-exec: %s", strerror(errno)); 577 if (fcntl(job->outPipe, F_SETFD, FD_CLOEXEC) == -1) 578 Punt("Cannot set close-on-exec: %s", strerror(errno)); 579 580 /* 581 * We mark the input side of the pipe non-blocking; we poll(2) the 582 * pipe when we're waiting for a job token, but we might lose the 583 * race for the token when a new one becomes available, so the read 584 * from the pipe should not block. 585 */ 586 flags = fcntl(job->inPipe, F_GETFL, 0); 587 if (flags == -1) 588 Punt("Cannot get flags: %s", strerror(errno)); 589 flags |= O_NONBLOCK; 590 if (fcntl(job->inPipe, F_SETFL, flags) == -1) 591 Punt("Cannot set flags: %s", strerror(errno)); 592 } 593 594 /* Pass the signal to each running job. */ 595 static void 596 JobCondPassSig(int signo) 597 { 598 Job *job; 599 600 DEBUG1(JOB, "JobCondPassSig(%d) called.\n", signo); 601 602 for (job = job_table; job < job_table_end; job++) { 603 if (job->status != JOB_ST_RUNNING) 604 continue; 605 DEBUG2(JOB, "JobCondPassSig passing signal %d to child %d.\n", 606 signo, job->pid); 607 KILLPG(job->pid, signo); 608 } 609 } 610 611 /* 612 * SIGCHLD handler. 613 * 614 * Sends a token on the child exit pipe to wake us up from select()/poll(). 615 */ 616 /*ARGSUSED*/ 617 static void 618 JobChildSig(int signo MAKE_ATTR_UNUSED) 619 { 620 caught_sigchld = 1; 621 while (write(childExitJob.outPipe, CHILD_EXIT, 1) == -1 && 622 errno == EAGAIN) 623 continue; 624 } 625 626 627 /* Resume all stopped jobs. */ 628 /*ARGSUSED*/ 629 static void 630 JobContinueSig(int signo MAKE_ATTR_UNUSED) 631 { 632 /* 633 * Defer sending SIGCONT to our stopped children until we return 634 * from the signal handler. 635 */ 636 while (write(childExitJob.outPipe, DO_JOB_RESUME, 1) == -1 && 637 errno == EAGAIN) 638 continue; 639 } 640 641 /* 642 * Pass a signal on to all jobs, then resend to ourselves. 643 * We die by the same signal. 644 */ 645 MAKE_ATTR_DEAD static void 646 JobPassSig_int(int signo) 647 { 648 /* Run .INTERRUPT target then exit */ 649 JobInterrupt(TRUE, signo); 650 } 651 652 /* 653 * Pass a signal on to all jobs, then resend to ourselves. 654 * We die by the same signal. 655 */ 656 MAKE_ATTR_DEAD static void 657 JobPassSig_term(int signo) 658 { 659 /* Dont run .INTERRUPT target then exit */ 660 JobInterrupt(FALSE, signo); 661 } 662 663 static void 664 JobPassSig_suspend(int signo) 665 { 666 sigset_t nmask, omask; 667 struct sigaction act; 668 669 /* Suppress job started/continued messages */ 670 make_suspended = TRUE; 671 672 /* Pass the signal onto every job */ 673 JobCondPassSig(signo); 674 675 /* 676 * Send ourselves the signal now we've given the message to everyone 677 * else. Note we block everything else possible while we're getting 678 * the signal. This ensures that all our jobs get continued when we 679 * wake up before we take any other signal. 680 */ 681 sigfillset(&nmask); 682 sigdelset(&nmask, signo); 683 (void)sigprocmask(SIG_SETMASK, &nmask, &omask); 684 685 act.sa_handler = SIG_DFL; 686 sigemptyset(&act.sa_mask); 687 act.sa_flags = 0; 688 (void)sigaction(signo, &act, NULL); 689 690 DEBUG1(JOB, "JobPassSig passing signal %d to self.\n", signo); 691 692 (void)kill(getpid(), signo); 693 694 /* 695 * We've been continued. 696 * 697 * A whole host of signals continue to happen! 698 * SIGCHLD for any processes that actually suspended themselves. 699 * SIGCHLD for any processes that exited while we were alseep. 700 * The SIGCONT that actually caused us to wakeup. 701 * 702 * Since we defer passing the SIGCONT on to our children until 703 * the main processing loop, we can be sure that all the SIGCHLD 704 * events will have happened by then - and that the waitpid() will 705 * collect the child 'suspended' events. 706 * For correct sequencing we just need to ensure we process the 707 * waitpid() before passing on the SIGCONT. 708 * 709 * In any case nothing else is needed here. 710 */ 711 712 /* Restore handler and signal mask */ 713 act.sa_handler = JobPassSig_suspend; 714 (void)sigaction(signo, &act, NULL); 715 (void)sigprocmask(SIG_SETMASK, &omask, NULL); 716 } 717 718 static Job * 719 JobFindPid(int pid, JobStatus status, Boolean isJobs) 720 { 721 Job *job; 722 723 for (job = job_table; job < job_table_end; job++) { 724 if (job->status == status && job->pid == pid) 725 return job; 726 } 727 if (DEBUG(JOB) && isJobs) 728 job_table_dump("no pid"); 729 return NULL; 730 } 731 732 /* Parse leading '@', '-' and '+', which control the exact execution mode. */ 733 static void 734 ParseCommandFlags(char **pp, CommandFlags *out_cmdFlags) 735 { 736 char *p = *pp; 737 out_cmdFlags->echo = TRUE; 738 out_cmdFlags->ignerr = FALSE; 739 out_cmdFlags->always = FALSE; 740 741 for (;;) { 742 if (*p == '@') 743 out_cmdFlags->echo = DEBUG(LOUD); 744 else if (*p == '-') 745 out_cmdFlags->ignerr = TRUE; 746 else if (*p == '+') 747 out_cmdFlags->always = TRUE; 748 else 749 break; 750 p++; 751 } 752 753 pp_skip_whitespace(&p); 754 755 *pp = p; 756 } 757 758 /* Escape a string for a double-quoted string literal in sh, csh and ksh. */ 759 static char * 760 EscapeShellDblQuot(const char *cmd) 761 { 762 size_t i, j; 763 764 /* Worst that could happen is every char needs escaping. */ 765 char *esc = bmake_malloc(strlen(cmd) * 2 + 1); 766 for (i = 0, j = 0; cmd[i] != '\0'; i++, j++) { 767 if (cmd[i] == '$' || cmd[i] == '`' || cmd[i] == '\\' || 768 cmd[i] == '"') 769 esc[j++] = '\\'; 770 esc[j] = cmd[i]; 771 } 772 esc[j] = '\0'; 773 774 return esc; 775 } 776 777 static void 778 ShellWriter_PrintFmt(ShellWriter *wr, const char *fmt, const char *arg) 779 { 780 DEBUG1(JOB, fmt, arg); 781 782 (void)fprintf(wr->f, fmt, arg); 783 /* XXX: Is flushing needed in any case, or only if f == stdout? */ 784 (void)fflush(wr->f); 785 } 786 787 static void 788 ShellWriter_Println(ShellWriter *wr, const char *line) 789 { 790 ShellWriter_PrintFmt(wr, "%s\n", line); 791 } 792 793 static void 794 ShellWriter_EchoOff(ShellWriter *wr) 795 { 796 if (shell->hasEchoCtl) 797 ShellWriter_Println(wr, shell->echoOff); 798 } 799 800 static void 801 ShellWriter_EchoCmd(ShellWriter *wr, const char *escCmd) 802 { 803 ShellWriter_PrintFmt(wr, shell->echoTmpl, escCmd); 804 } 805 806 static void 807 ShellWriter_EchoOn(ShellWriter *wr) 808 { 809 if (shell->hasEchoCtl) 810 ShellWriter_Println(wr, shell->echoOn); 811 } 812 813 static void 814 ShellWriter_TraceOn(ShellWriter *wr) 815 { 816 if (!wr->xtraced) { 817 ShellWriter_Println(wr, "set -x"); 818 wr->xtraced = TRUE; 819 } 820 } 821 822 static void 823 ShellWriter_ErrOff(ShellWriter *wr, Boolean echo) 824 { 825 if (echo) 826 ShellWriter_EchoOff(wr); 827 ShellWriter_Println(wr, shell->errOff); 828 if (echo) 829 ShellWriter_EchoOn(wr); 830 } 831 832 static void 833 ShellWriter_ErrOn(ShellWriter *wr, Boolean echo) 834 { 835 if (echo) 836 ShellWriter_EchoOff(wr); 837 ShellWriter_Println(wr, shell->errOn); 838 if (echo) 839 ShellWriter_EchoOn(wr); 840 } 841 842 /* 843 * The shell has no built-in error control, so emulate error control by 844 * enclosing each shell command in a template like "{ %s \n } || exit $?" 845 * (configurable per shell). 846 */ 847 static void 848 JobPrintSpecialsEchoCtl(Job *job, ShellWriter *wr, CommandFlags *inout_cmdFlags, 849 const char *escCmd, const char **inout_cmdTemplate) 850 { 851 /* XXX: Why is the job modified at this point? */ 852 job->ignerr = TRUE; 853 854 if (job->echo && inout_cmdFlags->echo) { 855 ShellWriter_EchoOff(wr); 856 ShellWriter_EchoCmd(wr, escCmd); 857 858 /* 859 * Leave echoing off so the user doesn't see the commands 860 * for toggling the error checking. 861 */ 862 inout_cmdFlags->echo = FALSE; 863 } else { 864 if (inout_cmdFlags->echo) 865 ShellWriter_EchoCmd(wr, escCmd); 866 } 867 *inout_cmdTemplate = shell->runIgnTmpl; 868 869 /* 870 * The template runIgnTmpl already takes care of ignoring errors, 871 * so pretend error checking is still on. 872 * XXX: What effects does this have, and why is it necessary? 873 */ 874 inout_cmdFlags->ignerr = FALSE; 875 } 876 877 static void 878 JobPrintSpecials(Job *job, ShellWriter *wr, const char *escCmd, Boolean run, 879 CommandFlags *inout_cmdFlags, const char **inout_cmdTemplate) 880 { 881 if (!run) { 882 /* 883 * If there is no command to run, there is no need to switch 884 * error checking off and on again for nothing. 885 */ 886 inout_cmdFlags->ignerr = FALSE; 887 } else if (shell->hasErrCtl) 888 ShellWriter_ErrOff(wr, job->echo && inout_cmdFlags->echo); 889 else if (shell->runIgnTmpl != NULL && shell->runIgnTmpl[0] != '\0') { 890 JobPrintSpecialsEchoCtl(job, wr, inout_cmdFlags, escCmd, 891 inout_cmdTemplate); 892 } else 893 inout_cmdFlags->ignerr = FALSE; 894 } 895 896 /* 897 * Put out another command for the given job. 898 * 899 * If the command starts with '@' and neither the -s nor the -n flag was 900 * given to make, we stick a shell-specific echoOff command in the script. 901 * 902 * If the command starts with '-' and the shell has no error control (none 903 * of the predefined shells has that), we ignore errors for the entire job. 904 * XXX: Why ignore errors for the entire job? 905 * XXX: Even ignore errors for the commands before this command? 906 * 907 * If the command is just "...", all further commands of this job are skipped 908 * for now. They are attached to the .END node and will be run by Job_Finish 909 * after all other targets have been made. 910 */ 911 static void 912 JobPrintCommand(Job *job, ShellWriter *wr, StringListNode *ln, const char *ucmd) 913 { 914 Boolean run; 915 916 CommandFlags cmdFlags; 917 /* Template for printing a command to the shell file */ 918 const char *cmdTemplate; 919 char *xcmd; /* The expanded command */ 920 char *xcmdStart; 921 char *escCmd; /* xcmd escaped to be used in double quotes */ 922 923 run = GNode_ShouldExecute(job->node); 924 925 Var_Subst(ucmd, job->node, VARE_WANTRES, &xcmd); 926 /* TODO: handle errors */ 927 xcmdStart = xcmd; 928 929 cmdTemplate = "%s\n"; 930 931 ParseCommandFlags(&xcmd, &cmdFlags); 932 933 /* The '+' command flag overrides the -n or -N options. */ 934 if (cmdFlags.always && !run) { 935 /* 936 * We're not actually executing anything... 937 * but this one needs to be - use compat mode just for it. 938 */ 939 Compat_RunCommand(ucmd, job->node, ln); 940 free(xcmdStart); 941 return; 942 } 943 944 /* 945 * If the shell doesn't have error control, the alternate echoing 946 * will be done (to avoid showing additional error checking code) 947 * and this needs some characters escaped. 948 */ 949 escCmd = shell->hasErrCtl ? NULL : EscapeShellDblQuot(xcmd); 950 951 if (!cmdFlags.echo) { 952 if (job->echo && run && shell->hasEchoCtl) { 953 ShellWriter_EchoOff(wr); 954 } else { 955 if (shell->hasErrCtl) 956 cmdFlags.echo = TRUE; 957 } 958 } 959 960 if (cmdFlags.ignerr) { 961 JobPrintSpecials(job, wr, escCmd, run, &cmdFlags, &cmdTemplate); 962 } else { 963 964 /* 965 * If errors are being checked and the shell doesn't have 966 * error control but does supply an runChkTmpl template, then 967 * set up commands to run through it. 968 */ 969 970 if (!shell->hasErrCtl && shell->runChkTmpl != NULL && 971 shell->runChkTmpl[0] != '\0') { 972 if (job->echo && cmdFlags.echo) { 973 ShellWriter_EchoOff(wr); 974 ShellWriter_EchoCmd(wr, escCmd); 975 cmdFlags.echo = FALSE; 976 } 977 /* 978 * If it's a comment line or blank, avoid the possible 979 * syntax error generated by "{\n} || exit $?". 980 */ 981 cmdTemplate = escCmd[0] == shell->commentChar || 982 escCmd[0] == '\0' 983 ? shell->runIgnTmpl 984 : shell->runChkTmpl; 985 cmdFlags.ignerr = FALSE; 986 } 987 } 988 989 if (DEBUG(SHELL) && strcmp(shellName, "sh") == 0) 990 ShellWriter_TraceOn(wr); 991 992 ShellWriter_PrintFmt(wr, cmdTemplate, xcmd); 993 free(xcmdStart); 994 free(escCmd); 995 996 if (cmdFlags.ignerr) 997 ShellWriter_ErrOn(wr, cmdFlags.echo && job->echo); 998 999 if (!cmdFlags.echo) 1000 ShellWriter_EchoOn(wr); 1001 } 1002 1003 /* 1004 * Print all commands to the shell file that is later executed. 1005 * 1006 * The special command "..." stops printing and saves the remaining commands 1007 * to be executed later, when the target '.END' is made. 1008 * 1009 * Return whether at least one command was written to the shell file. 1010 */ 1011 static Boolean 1012 JobPrintCommands(Job *job) 1013 { 1014 StringListNode *ln; 1015 Boolean seen = FALSE; 1016 ShellWriter wr = { job->cmdFILE, FALSE }; 1017 1018 for (ln = job->node->commands.first; ln != NULL; ln = ln->next) { 1019 const char *cmd = ln->datum; 1020 1021 if (strcmp(cmd, "...") == 0) { 1022 job->node->type |= OP_SAVE_CMDS; 1023 job->tailCmds = ln->next; 1024 break; 1025 } 1026 1027 JobPrintCommand(job, &wr, ln, ln->datum); 1028 seen = TRUE; 1029 } 1030 1031 return seen; 1032 } 1033 1034 /* 1035 * Save the delayed commands (those after '...'), to be executed later in 1036 * the '.END' node, when everything else is done. 1037 */ 1038 static void 1039 JobSaveCommands(Job *job) 1040 { 1041 StringListNode *ln; 1042 1043 for (ln = job->tailCmds; ln != NULL; ln = ln->next) { 1044 const char *cmd = ln->datum; 1045 char *expanded_cmd; 1046 /* 1047 * XXX: This Var_Subst is only intended to expand the dynamic 1048 * variables such as .TARGET, .IMPSRC. It is not intended to 1049 * expand the other variables as well; see deptgt-end.mk. 1050 */ 1051 (void)Var_Subst(cmd, job->node, VARE_WANTRES, &expanded_cmd); 1052 /* TODO: handle errors */ 1053 Lst_Append(&Targ_GetEndNode()->commands, expanded_cmd); 1054 } 1055 } 1056 1057 1058 /* Called to close both input and output pipes when a job is finished. */ 1059 static void 1060 JobClosePipes(Job *job) 1061 { 1062 clearfd(job); 1063 (void)close(job->outPipe); 1064 job->outPipe = -1; 1065 1066 JobDoOutput(job, TRUE); 1067 (void)close(job->inPipe); 1068 job->inPipe = -1; 1069 } 1070 1071 static void 1072 JobFinishDoneExitedError(Job *job, WAIT_T *inout_status) 1073 { 1074 SwitchOutputTo(job->node); 1075 #ifdef USE_META 1076 if (useMeta) { 1077 meta_job_error(job, job->node, 1078 job->ignerr, WEXITSTATUS(*inout_status)); 1079 } 1080 #endif 1081 if (!shouldDieQuietly(job->node, -1)) { 1082 (void)printf("*** [%s] Error code %d%s\n", 1083 job->node->name, WEXITSTATUS(*inout_status), 1084 job->ignerr ? " (ignored)" : ""); 1085 } 1086 1087 if (job->ignerr) 1088 WAIT_STATUS(*inout_status) = 0; 1089 else { 1090 if (deleteOnError) 1091 JobDeleteTarget(job->node); 1092 PrintOnError(job->node, NULL); 1093 } 1094 } 1095 1096 static void 1097 JobFinishDoneExited(Job *job, WAIT_T *inout_status) 1098 { 1099 DEBUG2(JOB, "Process %d [%s] exited.\n", job->pid, job->node->name); 1100 1101 if (WEXITSTATUS(*inout_status) != 0) 1102 JobFinishDoneExitedError(job, inout_status); 1103 else if (DEBUG(JOB)) { 1104 SwitchOutputTo(job->node); 1105 (void)printf("*** [%s] Completed successfully\n", 1106 job->node->name); 1107 } 1108 } 1109 1110 static void 1111 JobFinishDoneSignaled(Job *job, WAIT_T status) 1112 { 1113 SwitchOutputTo(job->node); 1114 (void)printf("*** [%s] Signal %d\n", job->node->name, WTERMSIG(status)); 1115 if (deleteOnError) 1116 JobDeleteTarget(job->node); 1117 } 1118 1119 static void 1120 JobFinishDone(Job *job, WAIT_T *inout_status) 1121 { 1122 if (WIFEXITED(*inout_status)) 1123 JobFinishDoneExited(job, inout_status); 1124 else 1125 JobFinishDoneSignaled(job, *inout_status); 1126 1127 (void)fflush(stdout); 1128 } 1129 1130 /* 1131 * Do final processing for the given job including updating parent nodes and 1132 * starting new jobs as available/necessary. 1133 * 1134 * Deferred commands for the job are placed on the .END node. 1135 * 1136 * If there was a serious error (job_errors != 0; not an ignored one), no more 1137 * jobs will be started. 1138 * 1139 * Input: 1140 * job job to finish 1141 * status sub-why job went away 1142 */ 1143 static void 1144 JobFinish (Job *job, WAIT_T status) 1145 { 1146 Boolean done, return_job_token; 1147 1148 DEBUG3(JOB, "JobFinish: %d [%s], status %d\n", 1149 job->pid, job->node->name, status); 1150 1151 if ((WIFEXITED(status) && 1152 ((WEXITSTATUS(status) != 0 && !job->ignerr))) || 1153 WIFSIGNALED(status)) { 1154 /* Finished because of an error. */ 1155 1156 JobClosePipes(job); 1157 if (job->cmdFILE != NULL && job->cmdFILE != stdout) { 1158 (void)fclose(job->cmdFILE); 1159 job->cmdFILE = NULL; 1160 } 1161 done = TRUE; 1162 1163 } else if (WIFEXITED(status)) { 1164 /* 1165 * Deal with ignored errors in -B mode. We need to print a 1166 * message telling of the ignored error as well as to run 1167 * the next command. 1168 */ 1169 done = WEXITSTATUS(status) != 0; 1170 1171 JobClosePipes(job); 1172 1173 } else { 1174 /* No need to close things down or anything. */ 1175 done = FALSE; 1176 } 1177 1178 if (done) 1179 JobFinishDone(job, &status); 1180 1181 #ifdef USE_META 1182 if (useMeta) { 1183 int meta_status = meta_job_finish(job); 1184 if (meta_status != 0 && status == 0) 1185 status = meta_status; 1186 } 1187 #endif 1188 1189 return_job_token = FALSE; 1190 1191 Trace_Log(JOBEND, job); 1192 if (!job->special) { 1193 if (WAIT_STATUS(status) != 0 || 1194 (aborting == ABORT_ERROR) || aborting == ABORT_INTERRUPT) 1195 return_job_token = TRUE; 1196 } 1197 1198 if (aborting != ABORT_ERROR && aborting != ABORT_INTERRUPT && 1199 (WAIT_STATUS(status) == 0)) { 1200 /* 1201 * As long as we aren't aborting and the job didn't return a 1202 * non-zero status that we shouldn't ignore, we call 1203 * Make_Update to update the parents. 1204 */ 1205 JobSaveCommands(job); 1206 job->node->made = MADE; 1207 if (!job->special) 1208 return_job_token = TRUE; 1209 Make_Update(job->node); 1210 job->status = JOB_ST_FREE; 1211 } else if (status != 0) { 1212 job_errors++; 1213 job->status = JOB_ST_FREE; 1214 } 1215 1216 if (job_errors > 0 && !opts.keepgoing && aborting != ABORT_INTERRUPT) { 1217 /* Prevent more jobs from getting started. */ 1218 aborting = ABORT_ERROR; 1219 } 1220 1221 if (return_job_token) 1222 Job_TokenReturn(); 1223 1224 if (aborting == ABORT_ERROR && jobTokensRunning == 0) 1225 Finish(job_errors); 1226 } 1227 1228 static void 1229 TouchRegular(GNode *gn) 1230 { 1231 const char *file = GNode_Path(gn); 1232 struct utimbuf times = { now, now }; 1233 int fd; 1234 char c; 1235 1236 if (utime(file, ×) >= 0) 1237 return; 1238 1239 fd = open(file, O_RDWR | O_CREAT, 0666); 1240 if (fd < 0) { 1241 (void)fprintf(stderr, "*** couldn't touch %s: %s\n", 1242 file, strerror(errno)); 1243 (void)fflush(stderr); 1244 return; /* XXX: What about propagating the error? */ 1245 } 1246 1247 /* Last resort: update the file's time stamps in the traditional way. 1248 * XXX: This doesn't work for empty files, which are sometimes used 1249 * as marker files. */ 1250 if (read(fd, &c, 1) == 1) { 1251 (void)lseek(fd, 0, SEEK_SET); 1252 while (write(fd, &c, 1) == -1 && errno == EAGAIN) 1253 continue; 1254 } 1255 (void)close(fd); /* XXX: What about propagating the error? */ 1256 } 1257 1258 /* 1259 * Touch the given target. Called by JobStart when the -t flag was given. 1260 * 1261 * The modification date of the file is changed. 1262 * If the file did not exist, it is created. 1263 */ 1264 void 1265 Job_Touch(GNode *gn, Boolean echo) 1266 { 1267 if (gn->type & 1268 (OP_JOIN | OP_USE | OP_USEBEFORE | OP_EXEC | OP_OPTIONAL | 1269 OP_SPECIAL | OP_PHONY)) { 1270 /* 1271 * These are "virtual" targets and should not really be 1272 * created. 1273 */ 1274 return; 1275 } 1276 1277 if (echo || !GNode_ShouldExecute(gn)) { 1278 (void)fprintf(stdout, "touch %s\n", gn->name); 1279 (void)fflush(stdout); 1280 } 1281 1282 if (!GNode_ShouldExecute(gn)) 1283 return; 1284 1285 if (gn->type & OP_ARCHV) 1286 Arch_Touch(gn); 1287 else if (gn->type & OP_LIB) 1288 Arch_TouchLib(gn); 1289 else 1290 TouchRegular(gn); 1291 } 1292 1293 /* 1294 * Make sure the given node has all the commands it needs. 1295 * 1296 * The node will have commands from the .DEFAULT rule added to it if it 1297 * needs them. 1298 * 1299 * Input: 1300 * gn The target whose commands need verifying 1301 * abortProc Function to abort with message 1302 * 1303 * Results: 1304 * TRUE if the commands list is/was ok. 1305 */ 1306 Boolean 1307 Job_CheckCommands(GNode *gn, void (*abortProc)(const char *, ...)) 1308 { 1309 if (GNode_IsTarget(gn)) 1310 return TRUE; 1311 if (!Lst_IsEmpty(&gn->commands)) 1312 return TRUE; 1313 if ((gn->type & OP_LIB) && !Lst_IsEmpty(&gn->children)) 1314 return TRUE; 1315 1316 /* 1317 * No commands. Look for .DEFAULT rule from which we might infer 1318 * commands. 1319 */ 1320 if (defaultNode != NULL && !Lst_IsEmpty(&defaultNode->commands) && 1321 !(gn->type & OP_SPECIAL)) { 1322 /* 1323 * The traditional Make only looks for a .DEFAULT if the node 1324 * was never the target of an operator, so that's what we do 1325 * too. 1326 * 1327 * The .DEFAULT node acts like a transformation rule, in that 1328 * gn also inherits any attributes or sources attached to 1329 * .DEFAULT itself. 1330 */ 1331 Make_HandleUse(defaultNode, gn); 1332 Var_Set(gn, IMPSRC, GNode_VarTarget(gn)); 1333 return TRUE; 1334 } 1335 1336 Dir_UpdateMTime(gn, FALSE); 1337 if (gn->mtime != 0 || (gn->type & OP_SPECIAL)) 1338 return TRUE; 1339 1340 /* 1341 * The node wasn't the target of an operator. We have no .DEFAULT 1342 * rule to go on and the target doesn't already exist. There's 1343 * nothing more we can do for this branch. If the -k flag wasn't 1344 * given, we stop in our tracks, otherwise we just don't update 1345 * this node's parents so they never get examined. 1346 */ 1347 1348 if (gn->flags & FROM_DEPEND) { 1349 if (!Job_RunTarget(".STALE", gn->fname)) 1350 fprintf(stdout, 1351 "%s: %s, %d: ignoring stale %s for %s\n", 1352 progname, gn->fname, gn->lineno, makeDependfile, 1353 gn->name); 1354 return TRUE; 1355 } 1356 1357 if (gn->type & OP_OPTIONAL) { 1358 (void)fprintf(stdout, "%s: don't know how to make %s (%s)\n", 1359 progname, gn->name, "ignored"); 1360 (void)fflush(stdout); 1361 return TRUE; 1362 } 1363 1364 if (opts.keepgoing) { 1365 (void)fprintf(stdout, "%s: don't know how to make %s (%s)\n", 1366 progname, gn->name, "continuing"); 1367 (void)fflush(stdout); 1368 return FALSE; 1369 } 1370 1371 abortProc("%s: don't know how to make %s. Stop", progname, gn->name); 1372 return FALSE; 1373 } 1374 1375 /* 1376 * Execute the shell for the given job. 1377 * 1378 * See Job_CatchOutput for handling the output of the shell. 1379 */ 1380 static void 1381 JobExec(Job *job, char **argv) 1382 { 1383 int cpid; /* ID of new child */ 1384 sigset_t mask; 1385 1386 if (DEBUG(JOB)) { 1387 int i; 1388 1389 debug_printf("Running %s\n", job->node->name); 1390 debug_printf("\tCommand: "); 1391 for (i = 0; argv[i] != NULL; i++) { 1392 debug_printf("%s ", argv[i]); 1393 } 1394 debug_printf("\n"); 1395 } 1396 1397 /* 1398 * Some jobs produce no output and it's disconcerting to have 1399 * no feedback of their running (since they produce no output, the 1400 * banner with their name in it never appears). This is an attempt to 1401 * provide that feedback, even if nothing follows it. 1402 */ 1403 if (job->echo) 1404 SwitchOutputTo(job->node); 1405 1406 /* No interruptions until this job is on the `jobs' list */ 1407 JobSigLock(&mask); 1408 1409 /* Pre-emptively mark job running, pid still zero though */ 1410 job->status = JOB_ST_RUNNING; 1411 1412 Var_ReexportVars(); 1413 1414 cpid = vfork(); 1415 if (cpid == -1) 1416 Punt("Cannot vfork: %s", strerror(errno)); 1417 1418 if (cpid == 0) { 1419 /* Child */ 1420 sigset_t tmask; 1421 1422 #ifdef USE_META 1423 if (useMeta) { 1424 meta_job_child(job); 1425 } 1426 #endif 1427 /* 1428 * Reset all signal handlers; this is necessary because we 1429 * also need to unblock signals before we exec(2). 1430 */ 1431 JobSigReset(); 1432 1433 /* Now unblock signals */ 1434 sigemptyset(&tmask); 1435 JobSigUnlock(&tmask); 1436 1437 /* 1438 * Must duplicate the input stream down to the child's input 1439 * and reset it to the beginning (again). Since the stream 1440 * was marked close-on-exec, we must clear that bit in the 1441 * new input. 1442 */ 1443 if (dup2(fileno(job->cmdFILE), 0) == -1) 1444 execDie("dup2", "job->cmdFILE"); 1445 if (fcntl(0, F_SETFD, 0) == -1) 1446 execDie("fcntl clear close-on-exec", "stdin"); 1447 if (lseek(0, 0, SEEK_SET) == -1) 1448 execDie("lseek to 0", "stdin"); 1449 1450 if (job->node->type & (OP_MAKE | OP_SUBMAKE)) { 1451 /* 1452 * Pass job token pipe to submakes. 1453 */ 1454 if (fcntl(tokenWaitJob.inPipe, F_SETFD, 0) == -1) 1455 execDie("clear close-on-exec", 1456 "tokenWaitJob.inPipe"); 1457 if (fcntl(tokenWaitJob.outPipe, F_SETFD, 0) == -1) 1458 execDie("clear close-on-exec", 1459 "tokenWaitJob.outPipe"); 1460 } 1461 1462 /* 1463 * Set up the child's output to be routed through the pipe 1464 * we've created for it. 1465 */ 1466 if (dup2(job->outPipe, 1) == -1) 1467 execDie("dup2", "job->outPipe"); 1468 1469 /* 1470 * The output channels are marked close on exec. This bit 1471 * was duplicated by the dup2(on some systems), so we have 1472 * to clear it before routing the shell's error output to 1473 * the same place as its standard output. 1474 */ 1475 if (fcntl(1, F_SETFD, 0) == -1) 1476 execDie("clear close-on-exec", "stdout"); 1477 if (dup2(1, 2) == -1) 1478 execDie("dup2", "1, 2"); 1479 1480 /* 1481 * We want to switch the child into a different process 1482 * family so we can kill it and all its descendants in 1483 * one fell swoop, by killing its process family, but not 1484 * commit suicide. 1485 */ 1486 #if defined(HAVE_SETPGID) 1487 (void)setpgid(0, getpid()); 1488 #else 1489 # if defined(HAVE_SETSID) 1490 /* XXX: dsl - I'm sure this should be setpgrp()... */ 1491 (void)setsid(); 1492 # else 1493 (void)setpgrp(0, getpid()); 1494 # endif 1495 #endif 1496 1497 (void)execv(shellPath, argv); 1498 execDie("exec", shellPath); 1499 } 1500 1501 /* Parent, continuing after the child exec */ 1502 job->pid = cpid; 1503 1504 Trace_Log(JOBSTART, job); 1505 1506 #ifdef USE_META 1507 if (useMeta) { 1508 meta_job_parent(job, cpid); 1509 } 1510 #endif 1511 1512 /* 1513 * Set the current position in the buffer to the beginning 1514 * and mark another stream to watch in the outputs mask 1515 */ 1516 job->curPos = 0; 1517 1518 watchfd(job); 1519 1520 if (job->cmdFILE != NULL && job->cmdFILE != stdout) { 1521 (void)fclose(job->cmdFILE); 1522 job->cmdFILE = NULL; 1523 } 1524 1525 /* Now that the job is actually running, add it to the table. */ 1526 if (DEBUG(JOB)) { 1527 debug_printf("JobExec(%s): pid %d added to jobs table\n", 1528 job->node->name, job->pid); 1529 job_table_dump("job started"); 1530 } 1531 JobSigUnlock(&mask); 1532 } 1533 1534 /* Create the argv needed to execute the shell for a given job. */ 1535 static void 1536 JobMakeArgv(Job *job, char **argv) 1537 { 1538 int argc; 1539 static char args[10]; /* For merged arguments */ 1540 1541 argv[0] = UNCONST(shellName); 1542 argc = 1; 1543 1544 if ((shell->errFlag != NULL && shell->errFlag[0] != '-') || 1545 (shell->echoFlag != NULL && shell->echoFlag[0] != '-')) { 1546 /* 1547 * At least one of the flags doesn't have a minus before it, 1548 * so merge them together. Have to do this because the Bourne 1549 * shell thinks its second argument is a file to source. 1550 * Grrrr. Note the ten-character limitation on the combined 1551 * arguments. 1552 * 1553 * TODO: Research until when the above comments were 1554 * practically relevant. 1555 */ 1556 (void)snprintf(args, sizeof args, "-%s%s", 1557 (job->ignerr ? "" : 1558 (shell->errFlag != NULL ? shell->errFlag : "")), 1559 (!job->echo ? "" : 1560 (shell->echoFlag != NULL ? shell->echoFlag : ""))); 1561 1562 if (args[1] != '\0') { 1563 argv[argc] = args; 1564 argc++; 1565 } 1566 } else { 1567 if (!job->ignerr && shell->errFlag != NULL) { 1568 argv[argc] = UNCONST(shell->errFlag); 1569 argc++; 1570 } 1571 if (job->echo && shell->echoFlag != NULL) { 1572 argv[argc] = UNCONST(shell->echoFlag); 1573 argc++; 1574 } 1575 } 1576 argv[argc] = NULL; 1577 } 1578 1579 static void 1580 JobWriteShellCommands(Job *job, GNode *gn, Boolean cmdsOK, Boolean *out_run) 1581 { 1582 /* 1583 * tfile is the name of a file into which all shell commands 1584 * are put. It is removed before the child shell is executed, 1585 * unless DEBUG(SCRIPT) is set. 1586 */ 1587 char tfile[MAXPATHLEN]; 1588 int tfd; /* File descriptor to the temp file */ 1589 1590 /* 1591 * We're serious here, but if the commands were bogus, we're 1592 * also dead... 1593 */ 1594 if (!cmdsOK) { 1595 PrintOnError(gn, NULL); /* provide some clue */ 1596 DieHorribly(); 1597 } 1598 1599 tfd = Job_TempFile(TMPPAT, tfile, sizeof tfile); 1600 1601 job->cmdFILE = fdopen(tfd, "w+"); 1602 if (job->cmdFILE == NULL) 1603 Punt("Could not fdopen %s", tfile); 1604 1605 (void)fcntl(fileno(job->cmdFILE), F_SETFD, FD_CLOEXEC); 1606 1607 #ifdef USE_META 1608 if (useMeta) { 1609 meta_job_start(job, gn); 1610 if (gn->type & OP_SILENT) /* might have changed */ 1611 job->echo = FALSE; 1612 } 1613 #endif 1614 1615 *out_run = JobPrintCommands(job); 1616 } 1617 1618 /* 1619 * Start a target-creation process going for the target described by the 1620 * graph node gn. 1621 * 1622 * Input: 1623 * gn target to create 1624 * flags flags for the job to override normal ones. 1625 * previous The previous Job structure for this node, if any. 1626 * 1627 * Results: 1628 * JOB_ERROR if there was an error in the commands, JOB_FINISHED 1629 * if there isn't actually anything left to do for the job and 1630 * JOB_RUNNING if the job has been started. 1631 * 1632 * Side Effects: 1633 * A new Job node is created and added to the list of running 1634 * jobs. PMake is forked and a child shell created. 1635 * 1636 * NB: The return value is ignored by everyone. 1637 */ 1638 static JobStartResult 1639 JobStart(GNode *gn, Boolean special) 1640 { 1641 Job *job; /* new job descriptor */ 1642 char *argv[10]; /* Argument vector to shell */ 1643 Boolean cmdsOK; /* true if the nodes commands were all right */ 1644 Boolean run; 1645 1646 for (job = job_table; job < job_table_end; job++) { 1647 if (job->status == JOB_ST_FREE) 1648 break; 1649 } 1650 if (job >= job_table_end) 1651 Punt("JobStart no job slots vacant"); 1652 1653 memset(job, 0, sizeof *job); 1654 job->node = gn; 1655 job->tailCmds = NULL; 1656 job->status = JOB_ST_SET_UP; 1657 1658 job->special = special || gn->type & OP_SPECIAL; 1659 job->ignerr = opts.ignoreErrors || gn->type & OP_IGNORE; 1660 job->echo = !(opts.beSilent || gn->type & OP_SILENT); 1661 1662 /* 1663 * Check the commands now so any attributes from .DEFAULT have a 1664 * chance to migrate to the node. 1665 */ 1666 cmdsOK = Job_CheckCommands(gn, Error); 1667 1668 job->inPollfd = NULL; 1669 1670 if (Lst_IsEmpty(&gn->commands)) { 1671 job->cmdFILE = stdout; 1672 run = FALSE; 1673 } else if (((gn->type & OP_MAKE) && !opts.noRecursiveExecute) || 1674 (!opts.noExecute && !opts.touchFlag)) { 1675 /* 1676 * The above condition looks very similar to 1677 * GNode_ShouldExecute but is subtly different. It prevents 1678 * that .MAKE targets are touched since these are usually 1679 * virtual targets. 1680 */ 1681 1682 JobWriteShellCommands(job, gn, cmdsOK, &run); 1683 (void)fflush(job->cmdFILE); 1684 } else if (!GNode_ShouldExecute(gn)) { 1685 /* 1686 * Just print all the commands to stdout in one fell swoop. 1687 * This still sets up job->tailCmds correctly. 1688 */ 1689 SwitchOutputTo(gn); 1690 job->cmdFILE = stdout; 1691 if (cmdsOK) 1692 JobPrintCommands(job); 1693 run = FALSE; 1694 (void)fflush(job->cmdFILE); 1695 } else { 1696 Job_Touch(gn, job->echo); 1697 run = FALSE; 1698 } 1699 1700 /* If we're not supposed to execute a shell, don't. */ 1701 if (!run) { 1702 if (!job->special) 1703 Job_TokenReturn(); 1704 /* Unlink and close the command file if we opened one */ 1705 if (job->cmdFILE != NULL && job->cmdFILE != stdout) { 1706 (void)fclose(job->cmdFILE); 1707 job->cmdFILE = NULL; 1708 } 1709 1710 /* 1711 * We only want to work our way up the graph if we aren't 1712 * here because the commands for the job were no good. 1713 */ 1714 if (cmdsOK && aborting == ABORT_NONE) { 1715 JobSaveCommands(job); 1716 job->node->made = MADE; 1717 Make_Update(job->node); 1718 } 1719 job->status = JOB_ST_FREE; 1720 return cmdsOK ? JOB_FINISHED : JOB_ERROR; 1721 } 1722 1723 /* 1724 * Set up the control arguments to the shell. This is based on the 1725 * flags set earlier for this job. 1726 */ 1727 JobMakeArgv(job, argv); 1728 1729 /* Create the pipe by which we'll get the shell's output. */ 1730 JobCreatePipe(job, 3); 1731 1732 JobExec(job, argv); 1733 return JOB_RUNNING; 1734 } 1735 1736 /* 1737 * Print the output of the shell command, skipping the noPrint text of the 1738 * shell, if any. The default shell does not have noPrint though, which means 1739 * that in all practical cases, handling the output is left to the caller. 1740 */ 1741 static char * 1742 JobOutput(char *cp, char *endp) /* XXX: should all be const */ 1743 { 1744 char *ecp; /* XXX: should be const */ 1745 1746 if (shell->noPrint == NULL || shell->noPrint[0] == '\0') 1747 return cp; 1748 1749 /* 1750 * XXX: What happens if shell->noPrint occurs on the boundary of 1751 * the buffer? To work correctly in all cases, this should rather 1752 * be a proper stream filter instead of doing string matching on 1753 * selected chunks of the output. 1754 */ 1755 while ((ecp = strstr(cp, shell->noPrint)) != NULL) { 1756 if (ecp != cp) { 1757 *ecp = '\0'; /* XXX: avoid writing to the buffer */ 1758 /* 1759 * The only way there wouldn't be a newline after 1760 * this line is if it were the last in the buffer. 1761 * however, since the noPrint output comes after it, 1762 * there must be a newline, so we don't print one. 1763 */ 1764 /* XXX: What about null bytes in the output? */ 1765 (void)fprintf(stdout, "%s", cp); 1766 (void)fflush(stdout); 1767 } 1768 cp = ecp + shell->noPrintLen; 1769 if (cp == endp) 1770 break; 1771 cp++; /* skip over the (XXX: assumed) newline */ 1772 pp_skip_whitespace(&cp); 1773 } 1774 return cp; 1775 } 1776 1777 /* 1778 * This function is called whenever there is something to read on the pipe. 1779 * We collect more output from the given job and store it in the job's 1780 * outBuf. If this makes up a line, we print it tagged by the job's 1781 * identifier, as necessary. 1782 * 1783 * In the output of the shell, the 'noPrint' lines are removed. If the 1784 * command is not alone on the line (the character after it is not \0 or 1785 * \n), we do print whatever follows it. 1786 * 1787 * Input: 1788 * job the job whose output needs printing 1789 * finish TRUE if this is the last time we'll be called 1790 * for this job 1791 */ 1792 static void 1793 JobDoOutput(Job *job, Boolean finish) 1794 { 1795 Boolean gotNL; /* true if got a newline */ 1796 Boolean fbuf; /* true if our buffer filled up */ 1797 size_t nr; /* number of bytes read */ 1798 size_t i; /* auxiliary index into outBuf */ 1799 size_t max; /* limit for i (end of current data) */ 1800 ssize_t nRead; /* (Temporary) number of bytes read */ 1801 1802 /* Read as many bytes as will fit in the buffer. */ 1803 again: 1804 gotNL = FALSE; 1805 fbuf = FALSE; 1806 1807 nRead = read(job->inPipe, &job->outBuf[job->curPos], 1808 JOB_BUFSIZE - job->curPos); 1809 if (nRead < 0) { 1810 if (errno == EAGAIN) 1811 return; 1812 if (DEBUG(JOB)) { 1813 perror("JobDoOutput(piperead)"); 1814 } 1815 nr = 0; 1816 } else { 1817 nr = (size_t)nRead; 1818 } 1819 1820 /* 1821 * If we hit the end-of-file (the job is dead), we must flush its 1822 * remaining output, so pretend we read a newline if there's any 1823 * output remaining in the buffer. 1824 * Also clear the 'finish' flag so we stop looping. 1825 */ 1826 if (nr == 0 && job->curPos != 0) { 1827 job->outBuf[job->curPos] = '\n'; 1828 nr = 1; 1829 finish = FALSE; 1830 } else if (nr == 0) { 1831 finish = FALSE; 1832 } 1833 1834 /* 1835 * Look for the last newline in the bytes we just got. If there is 1836 * one, break out of the loop with 'i' as its index and gotNL set 1837 * TRUE. 1838 */ 1839 max = job->curPos + nr; 1840 for (i = job->curPos + nr - 1; 1841 i >= job->curPos && i != (size_t)-1; i--) { 1842 if (job->outBuf[i] == '\n') { 1843 gotNL = TRUE; 1844 break; 1845 } else if (job->outBuf[i] == '\0') { 1846 /* 1847 * Why? 1848 */ 1849 job->outBuf[i] = ' '; 1850 } 1851 } 1852 1853 if (!gotNL) { 1854 job->curPos += nr; 1855 if (job->curPos == JOB_BUFSIZE) { 1856 /* 1857 * If we've run out of buffer space, we have no choice 1858 * but to print the stuff. sigh. 1859 */ 1860 fbuf = TRUE; 1861 i = job->curPos; 1862 } 1863 } 1864 if (gotNL || fbuf) { 1865 /* 1866 * Need to send the output to the screen. Null terminate it 1867 * first, overwriting the newline character if there was one. 1868 * So long as the line isn't one we should filter (according 1869 * to the shell description), we print the line, preceded 1870 * by a target banner if this target isn't the same as the 1871 * one for which we last printed something. 1872 * The rest of the data in the buffer are then shifted down 1873 * to the start of the buffer and curPos is set accordingly. 1874 */ 1875 job->outBuf[i] = '\0'; 1876 if (i >= job->curPos) { 1877 char *cp; 1878 1879 cp = JobOutput(job->outBuf, &job->outBuf[i]); 1880 1881 /* 1882 * There's still more in that thar buffer. This time, 1883 * though, we know there's no newline at the end, so 1884 * we add one of our own free will. 1885 */ 1886 if (*cp != '\0') { 1887 if (!opts.beSilent) 1888 SwitchOutputTo(job->node); 1889 #ifdef USE_META 1890 if (useMeta) { 1891 meta_job_output(job, cp, 1892 gotNL ? "\n" : ""); 1893 } 1894 #endif 1895 (void)fprintf(stdout, "%s%s", cp, 1896 gotNL ? "\n" : ""); 1897 (void)fflush(stdout); 1898 } 1899 } 1900 /* 1901 * max is the last offset still in the buffer. Move any 1902 * remaining characters to the start of the buffer and 1903 * update the end marker curPos. 1904 */ 1905 if (i < max) { 1906 (void)memmove(job->outBuf, &job->outBuf[i + 1], 1907 max - (i + 1)); 1908 job->curPos = max - (i + 1); 1909 } else { 1910 assert(i == max); 1911 job->curPos = 0; 1912 } 1913 } 1914 if (finish) { 1915 /* 1916 * If the finish flag is true, we must loop until we hit 1917 * end-of-file on the pipe. This is guaranteed to happen 1918 * eventually since the other end of the pipe is now closed 1919 * (we closed it explicitly and the child has exited). When 1920 * we do get an EOF, finish will be set FALSE and we'll fall 1921 * through and out. 1922 */ 1923 goto again; 1924 } 1925 } 1926 1927 static void 1928 JobRun(GNode *targ) 1929 { 1930 #if 0 1931 /* 1932 * Unfortunately it is too complicated to run .BEGIN, .END, and 1933 * .INTERRUPT job in the parallel job module. As of 2020-09-25, 1934 * unit-tests/deptgt-end-jobs.mk hangs in an endless loop. 1935 * 1936 * Running these jobs in compat mode also guarantees that these 1937 * jobs do not overlap with other unrelated jobs. 1938 */ 1939 GNodeList lst = LST_INIT; 1940 Lst_Append(&lst, targ); 1941 (void)Make_Run(&lst); 1942 Lst_Done(&lst); 1943 JobStart(targ, TRUE); 1944 while (jobTokensRunning != 0) { 1945 Job_CatchOutput(); 1946 } 1947 #else 1948 Compat_Make(targ, targ); 1949 /* XXX: Replace with GNode_IsError(gn) */ 1950 if (targ->made == ERROR) { 1951 PrintOnError(targ, "\n\nStop."); 1952 exit(1); 1953 } 1954 #endif 1955 } 1956 1957 /* 1958 * Handle the exit of a child. Called from Make_Make. 1959 * 1960 * The job descriptor is removed from the list of children. 1961 * 1962 * Notes: 1963 * We do waits, blocking or not, according to the wisdom of our 1964 * caller, until there are no more children to report. For each 1965 * job, call JobFinish to finish things off. 1966 */ 1967 void 1968 Job_CatchChildren(void) 1969 { 1970 int pid; /* pid of dead child */ 1971 WAIT_T status; /* Exit/termination status */ 1972 1973 /* Don't even bother if we know there's no one around. */ 1974 if (jobTokensRunning == 0) 1975 return; 1976 1977 /* Have we received SIGCHLD since last call? */ 1978 if (caught_sigchld == 0) 1979 return; 1980 caught_sigchld = 0; 1981 1982 while ((pid = waitpid((pid_t)-1, &status, WNOHANG | WUNTRACED)) > 0) { 1983 DEBUG2(JOB, "Process %d exited/stopped status %x.\n", 1984 pid, WAIT_STATUS(status)); 1985 JobReapChild(pid, status, TRUE); 1986 } 1987 } 1988 1989 /* 1990 * It is possible that wait[pid]() was called from elsewhere, 1991 * this lets us reap jobs regardless. 1992 */ 1993 void 1994 JobReapChild(pid_t pid, WAIT_T status, Boolean isJobs) 1995 { 1996 Job *job; /* job descriptor for dead child */ 1997 1998 /* Don't even bother if we know there's no one around. */ 1999 if (jobTokensRunning == 0) 2000 return; 2001 2002 job = JobFindPid(pid, JOB_ST_RUNNING, isJobs); 2003 if (job == NULL) { 2004 if (isJobs) { 2005 if (!lurking_children) 2006 Error("Child (%d) status %x not in table?", 2007 pid, status); 2008 } 2009 return; /* not ours */ 2010 } 2011 if (WIFSTOPPED(status)) { 2012 DEBUG2(JOB, "Process %d (%s) stopped.\n", 2013 job->pid, job->node->name); 2014 if (!make_suspended) { 2015 switch (WSTOPSIG(status)) { 2016 case SIGTSTP: 2017 (void)printf("*** [%s] Suspended\n", 2018 job->node->name); 2019 break; 2020 case SIGSTOP: 2021 (void)printf("*** [%s] Stopped\n", 2022 job->node->name); 2023 break; 2024 default: 2025 (void)printf("*** [%s] Stopped -- signal %d\n", 2026 job->node->name, WSTOPSIG(status)); 2027 } 2028 job->suspended = TRUE; 2029 } 2030 (void)fflush(stdout); 2031 return; 2032 } 2033 2034 job->status = JOB_ST_FINISHED; 2035 job->exit_status = WAIT_STATUS(status); 2036 2037 JobFinish(job, status); 2038 } 2039 2040 /* 2041 * Catch the output from our children, if we're using pipes do so. Otherwise 2042 * just block time until we get a signal(most likely a SIGCHLD) since there's 2043 * no point in just spinning when there's nothing to do and the reaping of a 2044 * child can wait for a while. 2045 */ 2046 void 2047 Job_CatchOutput(void) 2048 { 2049 int nready; 2050 Job *job; 2051 unsigned int i; 2052 2053 (void)fflush(stdout); 2054 2055 /* The first fd in the list is the job token pipe */ 2056 do { 2057 nready = poll(fds + 1 - wantToken, fdsLen - 1 + wantToken, 2058 POLL_MSEC); 2059 } while (nready < 0 && errno == EINTR); 2060 2061 if (nready < 0) 2062 Punt("poll: %s", strerror(errno)); 2063 2064 if (nready > 0 && readyfd(&childExitJob)) { 2065 char token = 0; 2066 ssize_t count; 2067 count = read(childExitJob.inPipe, &token, 1); 2068 if (count == 1) { 2069 if (token == DO_JOB_RESUME[0]) 2070 /* 2071 * Complete relay requested from our SIGCONT 2072 * handler 2073 */ 2074 JobRestartJobs(); 2075 } else if (count == 0) 2076 Punt("unexpected eof on token pipe"); 2077 else 2078 Punt("token pipe read: %s", strerror(errno)); 2079 nready--; 2080 } 2081 2082 Job_CatchChildren(); 2083 if (nready == 0) 2084 return; 2085 2086 for (i = npseudojobs * nfds_per_job(); i < fdsLen; i++) { 2087 if (fds[i].revents == 0) 2088 continue; 2089 job = jobByFdIndex[i]; 2090 if (job->status == JOB_ST_RUNNING) 2091 JobDoOutput(job, FALSE); 2092 #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV) 2093 /* 2094 * With meta mode, we may have activity on the job's filemon 2095 * descriptor too, which at the moment is any pollfd other 2096 * than job->inPollfd. 2097 */ 2098 if (useMeta && job->inPollfd != &fds[i]) { 2099 if (meta_job_event(job) <= 0) { 2100 fds[i].events = 0; /* never mind */ 2101 } 2102 } 2103 #endif 2104 if (--nready == 0) 2105 return; 2106 } 2107 } 2108 2109 /* 2110 * Start the creation of a target. Basically a front-end for JobStart used by 2111 * the Make module. 2112 */ 2113 void 2114 Job_Make(GNode *gn) 2115 { 2116 (void)JobStart(gn, FALSE); 2117 } 2118 2119 static void 2120 InitShellNameAndPath(void) 2121 { 2122 shellName = shell->name; 2123 2124 #ifdef DEFSHELL_CUSTOM 2125 if (shellName[0] == '/') { 2126 shellPath = shellName; 2127 shellName = str_basename(shellPath); 2128 return; 2129 } 2130 #endif 2131 2132 shellPath = str_concat3(_PATH_DEFSHELLDIR, "/", shellName); 2133 } 2134 2135 void 2136 Shell_Init(void) 2137 { 2138 if (shellPath == NULL) 2139 InitShellNameAndPath(); 2140 2141 Var_SetWithFlags(SCOPE_CMDLINE, ".SHELL", shellPath, VAR_SET_READONLY); 2142 if (shell->errFlag == NULL) 2143 shell->errFlag = ""; 2144 if (shell->echoFlag == NULL) 2145 shell->echoFlag = ""; 2146 if (shell->hasErrCtl && shell->errFlag[0] != '\0') { 2147 if (shellErrFlag != NULL && 2148 strcmp(shell->errFlag, &shellErrFlag[1]) != 0) { 2149 free(shellErrFlag); 2150 shellErrFlag = NULL; 2151 } 2152 if (shellErrFlag == NULL) { 2153 size_t n = strlen(shell->errFlag) + 2; 2154 2155 shellErrFlag = bmake_malloc(n); 2156 if (shellErrFlag != NULL) 2157 snprintf(shellErrFlag, n, "-%s", 2158 shell->errFlag); 2159 } 2160 } else if (shellErrFlag != NULL) { 2161 free(shellErrFlag); 2162 shellErrFlag = NULL; 2163 } 2164 } 2165 2166 /* 2167 * Return the string literal that is used in the current command shell 2168 * to produce a newline character. 2169 */ 2170 const char * 2171 Shell_GetNewline(void) 2172 { 2173 return shell->newline; 2174 } 2175 2176 void 2177 Job_SetPrefix(void) 2178 { 2179 if (targPrefix != NULL) { 2180 free(targPrefix); 2181 } else if (!Var_Exists(SCOPE_GLOBAL, MAKE_JOB_PREFIX)) { 2182 Global_Set(MAKE_JOB_PREFIX, "---"); 2183 } 2184 2185 (void)Var_Subst("${" MAKE_JOB_PREFIX "}", 2186 SCOPE_GLOBAL, VARE_WANTRES, &targPrefix); 2187 /* TODO: handle errors */ 2188 } 2189 2190 static void 2191 AddSig(int sig, SignalProc handler) 2192 { 2193 if (bmake_signal(sig, SIG_IGN) != SIG_IGN) { 2194 sigaddset(&caught_signals, sig); 2195 (void)bmake_signal(sig, handler); 2196 } 2197 } 2198 2199 /* Initialize the process module. */ 2200 void 2201 Job_Init(void) 2202 { 2203 Job_SetPrefix(); 2204 /* Allocate space for all the job info */ 2205 job_table = bmake_malloc((size_t)opts.maxJobs * sizeof *job_table); 2206 memset(job_table, 0, (size_t)opts.maxJobs * sizeof *job_table); 2207 job_table_end = job_table + opts.maxJobs; 2208 wantToken = 0; 2209 caught_sigchld = 0; 2210 2211 aborting = ABORT_NONE; 2212 job_errors = 0; 2213 2214 /* 2215 * There is a non-zero chance that we already have children. 2216 * eg after 'make -f- <<EOF' 2217 * Since their termination causes a 'Child (pid) not in table' 2218 * message, Collect the status of any that are already dead, and 2219 * suppress the error message if there are any undead ones. 2220 */ 2221 for (;;) { 2222 int rval; 2223 WAIT_T status; 2224 2225 rval = waitpid((pid_t)-1, &status, WNOHANG); 2226 if (rval > 0) 2227 continue; 2228 if (rval == 0) 2229 lurking_children = TRUE; 2230 break; 2231 } 2232 2233 Shell_Init(); 2234 2235 JobCreatePipe(&childExitJob, 3); 2236 2237 { 2238 /* Preallocate enough for the maximum number of jobs. */ 2239 size_t nfds = (npseudojobs + (size_t)opts.maxJobs) * 2240 nfds_per_job(); 2241 fds = bmake_malloc(sizeof *fds * nfds); 2242 jobByFdIndex = bmake_malloc(sizeof *jobByFdIndex * nfds); 2243 } 2244 2245 /* These are permanent entries and take slots 0 and 1 */ 2246 watchfd(&tokenWaitJob); 2247 watchfd(&childExitJob); 2248 2249 sigemptyset(&caught_signals); 2250 /* 2251 * Install a SIGCHLD handler. 2252 */ 2253 (void)bmake_signal(SIGCHLD, JobChildSig); 2254 sigaddset(&caught_signals, SIGCHLD); 2255 2256 /* 2257 * Catch the four signals that POSIX specifies if they aren't ignored. 2258 * JobPassSig will take care of calling JobInterrupt if appropriate. 2259 */ 2260 AddSig(SIGINT, JobPassSig_int); 2261 AddSig(SIGHUP, JobPassSig_term); 2262 AddSig(SIGTERM, JobPassSig_term); 2263 AddSig(SIGQUIT, JobPassSig_term); 2264 2265 /* 2266 * There are additional signals that need to be caught and passed if 2267 * either the export system wants to be told directly of signals or if 2268 * we're giving each job its own process group (since then it won't get 2269 * signals from the terminal driver as we own the terminal) 2270 */ 2271 AddSig(SIGTSTP, JobPassSig_suspend); 2272 AddSig(SIGTTOU, JobPassSig_suspend); 2273 AddSig(SIGTTIN, JobPassSig_suspend); 2274 AddSig(SIGWINCH, JobCondPassSig); 2275 AddSig(SIGCONT, JobContinueSig); 2276 2277 (void)Job_RunTarget(".BEGIN", NULL); 2278 /* Create the .END node now, even though no code in the unit tests 2279 * depends on it. See also Targ_GetEndNode in Compat_Run. */ 2280 (void)Targ_GetEndNode(); 2281 } 2282 2283 static void 2284 DelSig(int sig) 2285 { 2286 if (sigismember(&caught_signals, sig) != 0) 2287 (void)bmake_signal(sig, SIG_DFL); 2288 } 2289 2290 static void 2291 JobSigReset(void) 2292 { 2293 DelSig(SIGINT); 2294 DelSig(SIGHUP); 2295 DelSig(SIGQUIT); 2296 DelSig(SIGTERM); 2297 DelSig(SIGTSTP); 2298 DelSig(SIGTTOU); 2299 DelSig(SIGTTIN); 2300 DelSig(SIGWINCH); 2301 DelSig(SIGCONT); 2302 (void)bmake_signal(SIGCHLD, SIG_DFL); 2303 } 2304 2305 /* Find a shell in 'shells' given its name, or return NULL. */ 2306 static Shell * 2307 FindShellByName(const char *name) 2308 { 2309 Shell *sh = shells; 2310 const Shell *shellsEnd = sh + sizeof shells / sizeof shells[0]; 2311 2312 for (sh = shells; sh < shellsEnd; sh++) { 2313 if (strcmp(name, sh->name) == 0) 2314 return sh; 2315 } 2316 return NULL; 2317 } 2318 2319 /* 2320 * Parse a shell specification and set up 'shell', shellPath and 2321 * shellName appropriately. 2322 * 2323 * Input: 2324 * line The shell spec 2325 * 2326 * Results: 2327 * FALSE if the specification was incorrect. 2328 * 2329 * Side Effects: 2330 * 'shell' points to a Shell structure (either predefined or 2331 * created from the shell spec), shellPath is the full path of the 2332 * shell described by 'shell', while shellName is just the 2333 * final component of shellPath. 2334 * 2335 * Notes: 2336 * A shell specification consists of a .SHELL target, with dependency 2337 * operator, followed by a series of blank-separated words. Double 2338 * quotes can be used to use blanks in words. A backslash escapes 2339 * anything (most notably a double-quote and a space) and 2340 * provides the functionality it does in C. Each word consists of 2341 * keyword and value separated by an equal sign. There should be no 2342 * unnecessary spaces in the word. The keywords are as follows: 2343 * name Name of shell. 2344 * path Location of shell. 2345 * quiet Command to turn off echoing. 2346 * echo Command to turn echoing on 2347 * filter Result of turning off echoing that shouldn't be 2348 * printed. 2349 * echoFlag Flag to turn echoing on at the start 2350 * errFlag Flag to turn error checking on at the start 2351 * hasErrCtl True if shell has error checking control 2352 * newline String literal to represent a newline char 2353 * check Command to turn on error checking if hasErrCtl 2354 * is TRUE or template of command to echo a command 2355 * for which error checking is off if hasErrCtl is 2356 * FALSE. 2357 * ignore Command to turn off error checking if hasErrCtl 2358 * is TRUE or template of command to execute a 2359 * command so as to ignore any errors it returns if 2360 * hasErrCtl is FALSE. 2361 */ 2362 Boolean 2363 Job_ParseShell(char *line) 2364 { 2365 Words wordsList; 2366 char **words; 2367 char **argv; 2368 size_t argc; 2369 char *path; 2370 Shell newShell; 2371 Boolean fullSpec = FALSE; 2372 Shell *sh; 2373 2374 /* XXX: don't use line as an iterator variable */ 2375 pp_skip_whitespace(&line); 2376 2377 free(shell_freeIt); 2378 2379 memset(&newShell, 0, sizeof newShell); 2380 2381 /* 2382 * Parse the specification by keyword 2383 */ 2384 wordsList = Str_Words(line, TRUE); 2385 words = wordsList.words; 2386 argc = wordsList.len; 2387 path = wordsList.freeIt; 2388 if (words == NULL) { 2389 Error("Unterminated quoted string [%s]", line); 2390 return FALSE; 2391 } 2392 shell_freeIt = path; 2393 2394 for (path = NULL, argv = words; argc != 0; argc--, argv++) { 2395 char *arg = *argv; 2396 if (strncmp(arg, "path=", 5) == 0) { 2397 path = arg + 5; 2398 } else if (strncmp(arg, "name=", 5) == 0) { 2399 newShell.name = arg + 5; 2400 } else { 2401 if (strncmp(arg, "quiet=", 6) == 0) { 2402 newShell.echoOff = arg + 6; 2403 } else if (strncmp(arg, "echo=", 5) == 0) { 2404 newShell.echoOn = arg + 5; 2405 } else if (strncmp(arg, "filter=", 7) == 0) { 2406 newShell.noPrint = arg + 7; 2407 newShell.noPrintLen = strlen(newShell.noPrint); 2408 } else if (strncmp(arg, "echoFlag=", 9) == 0) { 2409 newShell.echoFlag = arg + 9; 2410 } else if (strncmp(arg, "errFlag=", 8) == 0) { 2411 newShell.errFlag = arg + 8; 2412 } else if (strncmp(arg, "hasErrCtl=", 10) == 0) { 2413 char c = arg[10]; 2414 newShell.hasErrCtl = c == 'Y' || c == 'y' || 2415 c == 'T' || c == 't'; 2416 } else if (strncmp(arg, "newline=", 8) == 0) { 2417 newShell.newline = arg + 8; 2418 } else if (strncmp(arg, "check=", 6) == 0) { 2419 /* Before 2020-12-10, these two variables 2420 * had been a single variable. */ 2421 newShell.errOn = arg + 6; 2422 newShell.echoTmpl = arg + 6; 2423 } else if (strncmp(arg, "ignore=", 7) == 0) { 2424 /* Before 2020-12-10, these two variables 2425 * had been a single variable. */ 2426 newShell.errOff = arg + 7; 2427 newShell.runIgnTmpl = arg + 7; 2428 } else if (strncmp(arg, "errout=", 7) == 0) { 2429 newShell.runChkTmpl = arg + 7; 2430 } else if (strncmp(arg, "comment=", 8) == 0) { 2431 newShell.commentChar = arg[8]; 2432 } else { 2433 Parse_Error(PARSE_FATAL, 2434 "Unknown keyword \"%s\"", arg); 2435 free(words); 2436 return FALSE; 2437 } 2438 fullSpec = TRUE; 2439 } 2440 } 2441 2442 if (path == NULL) { 2443 /* 2444 * If no path was given, the user wants one of the 2445 * pre-defined shells, yes? So we find the one s/he wants 2446 * with the help of FindShellByName and set things up the 2447 * right way. shellPath will be set up by Shell_Init. 2448 */ 2449 if (newShell.name == NULL) { 2450 Parse_Error(PARSE_FATAL, 2451 "Neither path nor name specified"); 2452 free(words); 2453 return FALSE; 2454 } else { 2455 if ((sh = FindShellByName(newShell.name)) == NULL) { 2456 Parse_Error(PARSE_WARNING, 2457 "%s: No matching shell", newShell.name); 2458 free(words); 2459 return FALSE; 2460 } 2461 shell = sh; 2462 shellName = newShell.name; 2463 if (shellPath != NULL) { 2464 /* 2465 * Shell_Init has already been called! 2466 * Do it again. 2467 */ 2468 free(UNCONST(shellPath)); 2469 shellPath = NULL; 2470 Shell_Init(); 2471 } 2472 } 2473 } else { 2474 /* 2475 * The user provided a path. If s/he gave nothing else 2476 * (fullSpec is FALSE), try and find a matching shell in the 2477 * ones we know of. Else we just take the specification at 2478 * its word and copy it to a new location. In either case, 2479 * we need to record the path the user gave for the shell. 2480 */ 2481 shellPath = path; 2482 path = strrchr(path, '/'); 2483 if (path == NULL) { 2484 path = UNCONST(shellPath); 2485 } else { 2486 path++; 2487 } 2488 if (newShell.name != NULL) { 2489 shellName = newShell.name; 2490 } else { 2491 shellName = path; 2492 } 2493 if (!fullSpec) { 2494 if ((sh = FindShellByName(shellName)) == NULL) { 2495 Parse_Error(PARSE_WARNING, 2496 "%s: No matching shell", shellName); 2497 free(words); 2498 return FALSE; 2499 } 2500 shell = sh; 2501 } else { 2502 shell = bmake_malloc(sizeof *shell); 2503 *shell = newShell; 2504 } 2505 /* this will take care of shellErrFlag */ 2506 Shell_Init(); 2507 } 2508 2509 if (shell->echoOn != NULL && shell->echoOff != NULL) 2510 shell->hasEchoCtl = TRUE; 2511 2512 if (!shell->hasErrCtl) { 2513 if (shell->echoTmpl == NULL) 2514 shell->echoTmpl = ""; 2515 if (shell->runIgnTmpl == NULL) 2516 shell->runIgnTmpl = "%s\n"; 2517 } 2518 2519 /* 2520 * Do not free up the words themselves, since they might be in use 2521 * by the shell specification. 2522 */ 2523 free(words); 2524 return TRUE; 2525 } 2526 2527 /* 2528 * Handle the receipt of an interrupt. 2529 * 2530 * All children are killed. Another job will be started if the .INTERRUPT 2531 * target is defined. 2532 * 2533 * Input: 2534 * runINTERRUPT Non-zero if commands for the .INTERRUPT target 2535 * should be executed 2536 * signo signal received 2537 */ 2538 static void 2539 JobInterrupt(Boolean runINTERRUPT, int signo) 2540 { 2541 Job *job; /* job descriptor in that element */ 2542 GNode *interrupt; /* the node describing the .INTERRUPT target */ 2543 sigset_t mask; 2544 GNode *gn; 2545 2546 aborting = ABORT_INTERRUPT; 2547 2548 JobSigLock(&mask); 2549 2550 for (job = job_table; job < job_table_end; job++) { 2551 if (job->status != JOB_ST_RUNNING) 2552 continue; 2553 2554 gn = job->node; 2555 2556 JobDeleteTarget(gn); 2557 if (job->pid != 0) { 2558 DEBUG2(JOB, 2559 "JobInterrupt passing signal %d to child %d.\n", 2560 signo, job->pid); 2561 KILLPG(job->pid, signo); 2562 } 2563 } 2564 2565 JobSigUnlock(&mask); 2566 2567 if (runINTERRUPT && !opts.touchFlag) { 2568 interrupt = Targ_FindNode(".INTERRUPT"); 2569 if (interrupt != NULL) { 2570 opts.ignoreErrors = FALSE; 2571 JobRun(interrupt); 2572 } 2573 } 2574 Trace_Log(MAKEINTR, NULL); 2575 exit(signo); /* XXX: why signo? */ 2576 } 2577 2578 /* 2579 * Do the final processing, i.e. run the commands attached to the .END target. 2580 * 2581 * Return the number of errors reported. 2582 */ 2583 int 2584 Job_Finish(void) 2585 { 2586 GNode *endNode = Targ_GetEndNode(); 2587 if (!Lst_IsEmpty(&endNode->commands) || 2588 !Lst_IsEmpty(&endNode->children)) { 2589 if (job_errors != 0) { 2590 Error("Errors reported so .END ignored"); 2591 } else { 2592 JobRun(endNode); 2593 } 2594 } 2595 return job_errors; 2596 } 2597 2598 /* Clean up any memory used by the jobs module. */ 2599 void 2600 Job_End(void) 2601 { 2602 #ifdef CLEANUP 2603 free(shell_freeIt); 2604 #endif 2605 } 2606 2607 /* 2608 * Waits for all running jobs to finish and returns. 2609 * Sets 'aborting' to ABORT_WAIT to prevent other jobs from starting. 2610 */ 2611 void 2612 Job_Wait(void) 2613 { 2614 aborting = ABORT_WAIT; 2615 while (jobTokensRunning != 0) { 2616 Job_CatchOutput(); 2617 } 2618 aborting = ABORT_NONE; 2619 } 2620 2621 /* 2622 * Abort all currently running jobs without handling output or anything. 2623 * This function is to be called only in the event of a major error. 2624 * Most definitely NOT to be called from JobInterrupt. 2625 * 2626 * All children are killed, not just the firstborn. 2627 */ 2628 void 2629 Job_AbortAll(void) 2630 { 2631 Job *job; /* the job descriptor in that element */ 2632 WAIT_T foo; 2633 2634 aborting = ABORT_ERROR; 2635 2636 if (jobTokensRunning != 0) { 2637 for (job = job_table; job < job_table_end; job++) { 2638 if (job->status != JOB_ST_RUNNING) 2639 continue; 2640 /* 2641 * kill the child process with increasingly drastic 2642 * signals to make darn sure it's dead. 2643 */ 2644 KILLPG(job->pid, SIGINT); 2645 KILLPG(job->pid, SIGKILL); 2646 } 2647 } 2648 2649 /* 2650 * Catch as many children as want to report in at first, then give up 2651 */ 2652 while (waitpid((pid_t)-1, &foo, WNOHANG) > 0) 2653 continue; 2654 } 2655 2656 /* 2657 * Tries to restart stopped jobs if there are slots available. 2658 * Called in process context in response to a SIGCONT. 2659 */ 2660 static void 2661 JobRestartJobs(void) 2662 { 2663 Job *job; 2664 2665 for (job = job_table; job < job_table_end; job++) { 2666 if (job->status == JOB_ST_RUNNING && 2667 (make_suspended || job->suspended)) { 2668 DEBUG1(JOB, "Restarting stopped job pid %d.\n", 2669 job->pid); 2670 if (job->suspended) { 2671 (void)printf("*** [%s] Continued\n", 2672 job->node->name); 2673 (void)fflush(stdout); 2674 } 2675 job->suspended = FALSE; 2676 if (KILLPG(job->pid, SIGCONT) != 0 && DEBUG(JOB)) { 2677 debug_printf("Failed to send SIGCONT to %d\n", 2678 job->pid); 2679 } 2680 } 2681 if (job->status == JOB_ST_FINISHED) { 2682 /* 2683 * Job exit deferred after calling waitpid() in a 2684 * signal handler 2685 */ 2686 JobFinish(job, job->exit_status); 2687 } 2688 } 2689 make_suspended = FALSE; 2690 } 2691 2692 static void 2693 watchfd(Job *job) 2694 { 2695 if (job->inPollfd != NULL) 2696 Punt("Watching watched job"); 2697 2698 fds[fdsLen].fd = job->inPipe; 2699 fds[fdsLen].events = POLLIN; 2700 jobByFdIndex[fdsLen] = job; 2701 job->inPollfd = &fds[fdsLen]; 2702 fdsLen++; 2703 #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV) 2704 if (useMeta) { 2705 fds[fdsLen].fd = meta_job_fd(job); 2706 fds[fdsLen].events = fds[fdsLen].fd == -1 ? 0 : POLLIN; 2707 jobByFdIndex[fdsLen] = job; 2708 fdsLen++; 2709 } 2710 #endif 2711 } 2712 2713 static void 2714 clearfd(Job *job) 2715 { 2716 size_t i; 2717 if (job->inPollfd == NULL) 2718 Punt("Unwatching unwatched job"); 2719 i = (size_t)(job->inPollfd - fds); 2720 fdsLen--; 2721 #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV) 2722 if (useMeta) { 2723 /* 2724 * Sanity check: there should be two fds per job, so the job's 2725 * pollfd number should be even. 2726 */ 2727 assert(nfds_per_job() == 2); 2728 if (i % 2 != 0) 2729 Punt("odd-numbered fd with meta"); 2730 fdsLen--; 2731 } 2732 #endif 2733 /* 2734 * Move last job in table into hole made by dead job. 2735 */ 2736 if (fdsLen != i) { 2737 fds[i] = fds[fdsLen]; 2738 jobByFdIndex[i] = jobByFdIndex[fdsLen]; 2739 jobByFdIndex[i]->inPollfd = &fds[i]; 2740 #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV) 2741 if (useMeta) { 2742 fds[i + 1] = fds[fdsLen + 1]; 2743 jobByFdIndex[i + 1] = jobByFdIndex[fdsLen + 1]; 2744 } 2745 #endif 2746 } 2747 job->inPollfd = NULL; 2748 } 2749 2750 static Boolean 2751 readyfd(Job *job) 2752 { 2753 if (job->inPollfd == NULL) 2754 Punt("Polling unwatched job"); 2755 return (job->inPollfd->revents & POLLIN) != 0; 2756 } 2757 2758 /* 2759 * Put a token (back) into the job pipe. 2760 * This allows a make process to start a build job. 2761 */ 2762 static void 2763 JobTokenAdd(void) 2764 { 2765 char tok = JOB_TOKENS[aborting], tok1; 2766 2767 /* If we are depositing an error token flush everything else */ 2768 while (tok != '+' && read(tokenWaitJob.inPipe, &tok1, 1) == 1) 2769 continue; 2770 2771 DEBUG3(JOB, "(%d) aborting %d, deposit token %c\n", 2772 getpid(), aborting, tok); 2773 while (write(tokenWaitJob.outPipe, &tok, 1) == -1 && errno == EAGAIN) 2774 continue; 2775 } 2776 2777 /* Get a temp file */ 2778 int 2779 Job_TempFile(const char *pattern, char *tfile, size_t tfile_sz) 2780 { 2781 int fd; 2782 sigset_t mask; 2783 2784 JobSigLock(&mask); 2785 fd = mkTempFile(pattern, tfile, tfile_sz); 2786 if (tfile != NULL && !DEBUG(SCRIPT)) 2787 unlink(tfile); 2788 JobSigUnlock(&mask); 2789 2790 return fd; 2791 } 2792 2793 /* Prep the job token pipe in the root make process. */ 2794 void 2795 Job_ServerStart(int max_tokens, int jp_0, int jp_1) 2796 { 2797 int i; 2798 char jobarg[64]; 2799 2800 if (jp_0 >= 0 && jp_1 >= 0) { 2801 /* Pipe passed in from parent */ 2802 tokenWaitJob.inPipe = jp_0; 2803 tokenWaitJob.outPipe = jp_1; 2804 (void)fcntl(jp_0, F_SETFD, FD_CLOEXEC); 2805 (void)fcntl(jp_1, F_SETFD, FD_CLOEXEC); 2806 return; 2807 } 2808 2809 JobCreatePipe(&tokenWaitJob, 15); 2810 2811 snprintf(jobarg, sizeof jobarg, "%d,%d", 2812 tokenWaitJob.inPipe, tokenWaitJob.outPipe); 2813 2814 Global_Append(MAKEFLAGS, "-J"); 2815 Global_Append(MAKEFLAGS, jobarg); 2816 2817 /* 2818 * Preload the job pipe with one token per job, save the one 2819 * "extra" token for the primary job. 2820 * 2821 * XXX should clip maxJobs against PIPE_BUF -- if max_tokens is 2822 * larger than the write buffer size of the pipe, we will 2823 * deadlock here. 2824 */ 2825 for (i = 1; i < max_tokens; i++) 2826 JobTokenAdd(); 2827 } 2828 2829 /* Return a withdrawn token to the pool. */ 2830 void 2831 Job_TokenReturn(void) 2832 { 2833 jobTokensRunning--; 2834 if (jobTokensRunning < 0) 2835 Punt("token botch"); 2836 if (jobTokensRunning != 0 || JOB_TOKENS[aborting] != '+') 2837 JobTokenAdd(); 2838 } 2839 2840 /* 2841 * Attempt to withdraw a token from the pool. 2842 * 2843 * If pool is empty, set wantToken so that we wake up when a token is 2844 * released. 2845 * 2846 * Returns TRUE if a token was withdrawn, and FALSE if the pool is currently 2847 * empty. 2848 */ 2849 Boolean 2850 Job_TokenWithdraw(void) 2851 { 2852 char tok, tok1; 2853 ssize_t count; 2854 2855 wantToken = 0; 2856 DEBUG3(JOB, "Job_TokenWithdraw(%d): aborting %d, running %d\n", 2857 getpid(), aborting, jobTokensRunning); 2858 2859 if (aborting != ABORT_NONE || (jobTokensRunning >= opts.maxJobs)) 2860 return FALSE; 2861 2862 count = read(tokenWaitJob.inPipe, &tok, 1); 2863 if (count == 0) 2864 Fatal("eof on job pipe!"); 2865 if (count < 0 && jobTokensRunning != 0) { 2866 if (errno != EAGAIN) { 2867 Fatal("job pipe read: %s", strerror(errno)); 2868 } 2869 DEBUG1(JOB, "(%d) blocked for token\n", getpid()); 2870 wantToken = 1; 2871 return FALSE; 2872 } 2873 2874 if (count == 1 && tok != '+') { 2875 /* make being aborted - remove any other job tokens */ 2876 DEBUG2(JOB, "(%d) aborted by token %c\n", getpid(), tok); 2877 while (read(tokenWaitJob.inPipe, &tok1, 1) == 1) 2878 continue; 2879 /* And put the stopper back */ 2880 while (write(tokenWaitJob.outPipe, &tok, 1) == -1 && 2881 errno == EAGAIN) 2882 continue; 2883 if (shouldDieQuietly(NULL, 1)) 2884 exit(6); /* we aborted */ 2885 Fatal("A failure has been detected " 2886 "in another branch of the parallel make"); 2887 } 2888 2889 if (count == 1 && jobTokensRunning == 0) 2890 /* We didn't want the token really */ 2891 while (write(tokenWaitJob.outPipe, &tok, 1) == -1 && 2892 errno == EAGAIN) 2893 continue; 2894 2895 jobTokensRunning++; 2896 DEBUG1(JOB, "(%d) withdrew token\n", getpid()); 2897 return TRUE; 2898 } 2899 2900 /* 2901 * Run the named target if found. If a filename is specified, then set that 2902 * to the sources. 2903 * 2904 * Exits if the target fails. 2905 */ 2906 Boolean 2907 Job_RunTarget(const char *target, const char *fname) 2908 { 2909 GNode *gn = Targ_FindNode(target); 2910 if (gn == NULL) 2911 return FALSE; 2912 2913 if (fname != NULL) 2914 Var_Set(gn, ALLSRC, fname); 2915 2916 JobRun(gn); 2917 /* XXX: Replace with GNode_IsError(gn) */ 2918 if (gn->made == ERROR) { 2919 PrintOnError(gn, "\n\nStop."); 2920 exit(1); 2921 } 2922 return TRUE; 2923 } 2924 2925 #ifdef USE_SELECT 2926 int 2927 emul_poll(struct pollfd *fd, int nfd, int timeout) 2928 { 2929 fd_set rfds, wfds; 2930 int i, maxfd, nselect, npoll; 2931 struct timeval tv, *tvp; 2932 long usecs; 2933 2934 FD_ZERO(&rfds); 2935 FD_ZERO(&wfds); 2936 2937 maxfd = -1; 2938 for (i = 0; i < nfd; i++) { 2939 fd[i].revents = 0; 2940 2941 if (fd[i].events & POLLIN) 2942 FD_SET(fd[i].fd, &rfds); 2943 2944 if (fd[i].events & POLLOUT) 2945 FD_SET(fd[i].fd, &wfds); 2946 2947 if (fd[i].fd > maxfd) 2948 maxfd = fd[i].fd; 2949 } 2950 2951 if (maxfd >= FD_SETSIZE) { 2952 Punt("Ran out of fd_set slots; " 2953 "recompile with a larger FD_SETSIZE."); 2954 } 2955 2956 if (timeout < 0) { 2957 tvp = NULL; 2958 } else { 2959 usecs = timeout * 1000; 2960 tv.tv_sec = usecs / 1000000; 2961 tv.tv_usec = usecs % 1000000; 2962 tvp = &tv; 2963 } 2964 2965 nselect = select(maxfd + 1, &rfds, &wfds, NULL, tvp); 2966 2967 if (nselect <= 0) 2968 return nselect; 2969 2970 npoll = 0; 2971 for (i = 0; i < nfd; i++) { 2972 if (FD_ISSET(fd[i].fd, &rfds)) 2973 fd[i].revents |= POLLIN; 2974 2975 if (FD_ISSET(fd[i].fd, &wfds)) 2976 fd[i].revents |= POLLOUT; 2977 2978 if (fd[i].revents) 2979 npoll++; 2980 } 2981 2982 return npoll; 2983 } 2984 #endif /* USE_SELECT */ 2985