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