job.c revision 141258
1/*- 2 * Copyright (c) 1988, 1989, 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * Copyright (c) 1988, 1989 by Adam de Boor 5 * Copyright (c) 1989 by Berkeley Softworks 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Adam de Boor. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the University of 22 * California, Berkeley and its contributors. 23 * 4. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 * 39 * @(#)job.c 8.2 (Berkeley) 3/19/94 40 */ 41 42#include <sys/cdefs.h> 43__FBSDID("$FreeBSD: head/usr.bin/make/job.c 141258 2005-02-04 08:11:58Z harti $"); 44 45#ifndef OLD_JOKE 46#define OLD_JOKE 0 47#endif /* OLD_JOKE */ 48 49/*- 50 * job.c -- 51 * handle the creation etc. of our child processes. 52 * 53 * Interface: 54 * Job_Make Start the creation of the given target. 55 * 56 * Job_CatchChildren Check for and handle the termination of any 57 * children. This must be called reasonably 58 * frequently to keep the whole make going at 59 * a decent clip, since job table entries aren't 60 * removed until their process is caught this way. 61 * Its single argument is TRUE if the function 62 * should block waiting for a child to terminate. 63 * 64 * Job_CatchOutput Print any output our children have produced. 65 * Should also be called fairly frequently to 66 * keep the user informed of what's going on. 67 * If no output is waiting, it will block for 68 * a time given by the SEL_* constants, below, 69 * or until output is ready. 70 * 71 * Job_Init Called to intialize this module. in addition, 72 * any commands attached to the .BEGIN target 73 * are executed before this function returns. 74 * Hence, the makefile must have been parsed 75 * before this function is called. 76 * 77 * Job_Full Return TRUE if the job table is filled. 78 * 79 * Job_Empty Return TRUE if the job table is completely 80 * empty. 81 * 82 * Job_ParseShell Given the line following a .SHELL target, parse 83 * the line as a shell specification. Returns 84 * FAILURE if the spec was incorrect. 85 * 86 * Job_Finish Perform any final processing which needs doing. 87 * This includes the execution of any commands 88 * which have been/were attached to the .END 89 * target. It should only be called when the 90 * job table is empty. 91 * 92 * Job_AbortAll Abort all currently running jobs. It doesn't 93 * handle output or do anything for the jobs, 94 * just kills them. It should only be called in 95 * an emergency, as it were. 96 * 97 * Job_CheckCommands Verify that the commands for a target are 98 * ok. Provide them if necessary and possible. 99 * 100 * Job_Touch Update a target without really updating it. 101 * 102 * Job_Wait Wait for all currently-running jobs to finish. 103 */ 104 105#include <sys/types.h> 106#include <sys/select.h> 107#include <sys/stat.h> 108#ifdef USE_KQUEUE 109#include <sys/event.h> 110#endif 111#include <sys/wait.h> 112#include <ctype.h> 113#include <errno.h> 114#include <fcntl.h> 115#include <string.h> 116#include <signal.h> 117#include <stdlib.h> 118#include <unistd.h> 119#include <utime.h> 120 121#include "arch.h" 122#include "compat.h" 123#include "dir.h" 124#include "globals.h" 125#include "GNode.h" 126#include "job.h" 127#include "make.h" 128#include "parse.h" 129#include "pathnames.h" 130#include "str.h" 131#include "targ.h" 132#include "util.h" 133#include "var.h" 134 135#define STATIC static 136 137/* 138 * error handling variables 139 */ 140static int errors = 0; /* number of errors reported */ 141static int aborting = 0; /* why is the make aborting? */ 142#define ABORT_ERROR 1 /* Because of an error */ 143#define ABORT_INTERRUPT 2 /* Because it was interrupted */ 144#define ABORT_WAIT 3 /* Waiting for jobs to finish */ 145 146/* 147 * XXX: Avoid SunOS bug... FILENO() is fp->_file, and file 148 * is a char! So when we go above 127 we turn negative! 149 */ 150#define FILENO(a) ((unsigned)fileno(a)) 151 152/* 153 * post-make command processing. The node postCommands is really just the 154 * .END target but we keep it around to avoid having to search for it 155 * all the time. 156 */ 157static GNode *postCommands; /* node containing commands to execute when 158 * everything else is done */ 159static int numCommands; /* The number of commands actually printed 160 * for a target. Should this number be 161 * 0, no shell will be executed. */ 162 163/* 164 * Return values from JobStart. 165 */ 166#define JOB_RUNNING 0 /* Job is running */ 167#define JOB_ERROR 1 /* Error in starting the job */ 168#define JOB_FINISHED 2 /* The job is already finished */ 169#define JOB_STOPPED 3 /* The job is stopped */ 170 171/* 172 * tfile is used to build temp file names to store shell commands to 173 * execute. 174 */ 175static char tfile[sizeof(TMPPAT)]; 176 177/* 178 * Descriptions for various shells. 179 */ 180static const DEF_SHELL_STRUCT(CShell, const) shells[] = { 181 /* 182 * CSH description. The csh can do echo control by playing 183 * with the setting of the 'echo' shell variable. Sadly, 184 * however, it is unable to do error control nicely. 185 */ 186{ 187 "csh", 188 TRUE, "unset verbose", "set verbose", "unset verbose", 13, 189 FALSE, "echo \"%s\"\n", "csh -c \"%s || exit 0\"", 190 "v", "e", 191}, 192 /* 193 * SH description. Echo control is also possible and, under 194 * sun UNIX anyway, one can even control error checking. 195 */ 196{ 197 "sh", 198 TRUE, "set -", "set -v", "set -", 5, 199 TRUE, "set -e", "set +e", 200#ifdef OLDBOURNESHELL 201 FALSE, "echo \"%s\"\n", "sh -c '%s || exit 0'\n", 202#endif 203 "v", "e", 204}, 205 /* 206 * KSH description. The Korn shell has a superset of 207 * the Bourne shell's functionality. 208 */ 209{ 210 "ksh", 211 TRUE, "set -", "set -v", "set -", 5, 212 TRUE, "set -e", "set +e", 213 "v", "e", 214}, 215}; 216static Shell *commandShell = NULL; /* this is the shell to which we pass 217 * all commands in the Makefile. It is 218 * set by the Job_ParseShell function */ 219char *shellPath = NULL, /* full pathname of executable image */ 220 *shellName = NULL; /* last component of shell */ 221 222 223int maxJobs; /* The most children we can run at once */ 224STATIC int nJobs; /* The number of children currently running */ 225 226/* The structures that describe them */ 227STATIC Lst jobs = Lst_Initializer(jobs); 228 229STATIC Boolean jobFull; /* Flag to tell when the job table is full. It 230 * is set TRUE when (1) the total number of 231 * running jobs equals the maximum allowed */ 232#ifdef USE_KQUEUE 233static int kqfd; /* File descriptor obtained by kqueue() */ 234#else 235static fd_set outputs; /* Set of descriptors of pipes connected to 236 * the output channels of children */ 237#endif 238 239STATIC GNode *lastNode; /* The node for which output was most recently 240 * produced. */ 241STATIC const char *targFmt; /* Format string to use to head output from a 242 * job when it's not the most-recent job heard 243 * from */ 244 245#define TARG_FMT "--- %s ---\n" /* Default format */ 246#define MESSAGE(fp, gn) \ 247 fprintf(fp, targFmt, gn->name); 248 249/* 250 * When JobStart attempts to run a job but isn't allowed to 251 * or when Job_CatchChildren detects a job that has 252 * been stopped somehow, the job is placed on the stoppedJobs queue to be run 253 * when the next job finishes. 254 * 255 * Lst of Job structures describing jobs that were stopped due to 256 * concurrency limits or externally 257 */ 258STATIC Lst stoppedJobs = Lst_Initializer(stoppedJobs); 259 260STATIC int fifoFd; /* Fd of our job fifo */ 261STATIC char fifoName[] = "/tmp/make_fifo_XXXXXXXXX"; 262STATIC int fifoMaster; 263 264static sig_atomic_t interrupted; 265 266 267#if defined(USE_PGRP) && defined(SYSV) 268# define KILL(pid, sig) killpg(-(pid), (sig)) 269#else 270# if defined(USE_PGRP) 271# define KILL(pid, sig) killpg((pid), (sig)) 272# else 273# define KILL(pid, sig) kill((pid), (sig)) 274# endif 275#endif 276 277/* 278 * Grmpf... There is no way to set bits of the wait structure 279 * anymore with the stupid W*() macros. I liked the union wait 280 * stuff much more. So, we devise our own macros... This is 281 * really ugly, use dramamine sparingly. You have been warned. 282 */ 283#define W_SETMASKED(st, val, fun) \ 284 { \ 285 int sh = (int)~0; \ 286 int mask = fun(sh); \ 287 \ 288 for (sh = 0; ((mask >> sh) & 1) == 0; sh++) \ 289 continue; \ 290 *(st) = (*(st) & ~mask) | ((val) << sh); \ 291 } 292 293#define W_SETTERMSIG(st, val) W_SETMASKED(st, val, WTERMSIG) 294#define W_SETEXITSTATUS(st, val) W_SETMASKED(st, val, WEXITSTATUS) 295 296 297static int JobCondPassSig(void *, void *); 298static void JobPassSig(int); 299static int JobPrintCommand(void *, void *); 300static int JobSaveCommand(void *, void *); 301static void JobClose(Job *); 302static void JobFinish(Job *, int *); 303static void JobExec(Job *, char **); 304static void JobMakeArgv(Job *, char **); 305static void JobRestart(Job *); 306static int JobStart(GNode *, int, Job *); 307static char *JobOutput(Job *, char *, char *, int); 308static void JobDoOutput(Job *, Boolean); 309static Shell *JobMatchShell(const char *); 310static void JobInterrupt(int, int); 311static void JobRestartJobs(void); 312 313/* 314 * JobCatchSignal 315 * 316 * Got a signal. Set global variables and hope that someone will 317 * handle it. 318 */ 319static void 320JobCatchSig(int signo) 321{ 322 323 interrupted = signo; 324} 325 326/*- 327 *----------------------------------------------------------------------- 328 * JobCondPassSig -- 329 * Pass a signal to a job if USE_PGRP is defined. 330 * 331 * Results: 332 * === 0 333 * 334 * Side Effects: 335 * None, except the job may bite it. 336 * 337 *----------------------------------------------------------------------- 338 */ 339static int 340JobCondPassSig(void *jobp, void *signop) 341{ 342 Job *job = jobp; 343 int signo = *(int *)signop; 344 345 DEBUGF(JOB, ("JobCondPassSig passing signal %d to child %d.\n", 346 signo, job->pid)); 347 KILL(job->pid, signo); 348 return (0); 349} 350 351/*- 352 *----------------------------------------------------------------------- 353 * JobPassSig -- 354 * Pass a signal on to all local jobs if 355 * USE_PGRP is defined, then die ourselves. 356 * 357 * Results: 358 * None. 359 * 360 * Side Effects: 361 * We die by the same signal. 362 * 363 *----------------------------------------------------------------------- 364 */ 365static void 366JobPassSig(int signo) 367{ 368 sigset_t nmask, omask; 369 struct sigaction act; 370 371 sigemptyset(&nmask); 372 sigaddset(&nmask, signo); 373 sigprocmask(SIG_SETMASK, &nmask, &omask); 374 375 DEBUGF(JOB, ("JobPassSig(%d) called.\n", signo)); 376 Lst_ForEach(&jobs, JobCondPassSig, &signo); 377 378 /* 379 * Deal with proper cleanup based on the signal received. We only run 380 * the .INTERRUPT target if the signal was in fact an interrupt. The other 381 * three termination signals are more of a "get out *now*" command. 382 */ 383 if (signo == SIGINT) { 384 JobInterrupt(TRUE, signo); 385 } else if ((signo == SIGHUP) || (signo == SIGTERM) || (signo == SIGQUIT)) { 386 JobInterrupt(FALSE, signo); 387 } 388 389 /* 390 * Leave gracefully if SIGQUIT, rather than core dumping. 391 */ 392 if (signo == SIGQUIT) { 393 signo = SIGINT; 394 } 395 396 /* 397 * Send ourselves the signal now we've given the message to everyone else. 398 * Note we block everything else possible while we're getting the signal. 399 * This ensures that all our jobs get continued when we wake up before 400 * we take any other signal. 401 * XXX this comment seems wrong. 402 */ 403 act.sa_handler = SIG_DFL; 404 sigemptyset(&act.sa_mask); 405 act.sa_flags = 0; 406 sigaction(signo, &act, NULL); 407 408 DEBUGF(JOB, ("JobPassSig passing signal to self, mask = %x.\n", 409 ~0 & ~(1 << (signo - 1)))); 410 signal(signo, SIG_DFL); 411 412 KILL(getpid(), signo); 413 414 signo = SIGCONT; 415 Lst_ForEach(&jobs, JobCondPassSig, &signo); 416 417 sigprocmask(SIG_SETMASK, &omask, NULL); 418 sigprocmask(SIG_SETMASK, &omask, NULL); 419 act.sa_handler = JobPassSig; 420 sigaction(signo, &act, NULL); 421} 422 423/*- 424 *----------------------------------------------------------------------- 425 * JobCmpPid -- 426 * Compare the pid of the job with the given pid and return 0 if they 427 * are equal. This function is called from Job_CatchChildren via 428 * Lst_Find to find the job descriptor of the finished job. 429 * 430 * Results: 431 * 0 if the pid's match 432 * 433 * Side Effects: 434 * None 435 *----------------------------------------------------------------------- 436 */ 437static int 438JobCmpPid(const void *job, const void *pid) 439{ 440 441 return (*(const int *)pid - ((const Job *)job)->pid); 442} 443 444/*- 445 *----------------------------------------------------------------------- 446 * JobPrintCommand -- 447 * Put out another command for the given job. If the command starts 448 * with an @ or a - we process it specially. In the former case, 449 * so long as the -s and -n flags weren't given to make, we stick 450 * a shell-specific echoOff command in the script. In the latter, 451 * we ignore errors for the entire job, unless the shell has error 452 * control. 453 * If the command is just "..." we take all future commands for this 454 * job to be commands to be executed once the entire graph has been 455 * made and return non-zero to signal that the end of the commands 456 * was reached. These commands are later attached to the postCommands 457 * node and executed by Job_Finish when all things are done. 458 * This function is called from JobStart via Lst_ForEach. 459 * 460 * Results: 461 * Always 0, unless the command was "..." 462 * 463 * Side Effects: 464 * If the command begins with a '-' and the shell has no error control, 465 * the JOB_IGNERR flag is set in the job descriptor. 466 * If the command is "..." and we're not ignoring such things, 467 * tailCmds is set to the successor node of the cmd. 468 * numCommands is incremented if the command is actually printed. 469 *----------------------------------------------------------------------- 470 */ 471static int 472JobPrintCommand(void *cmdp, void *jobp) 473{ 474 Boolean noSpecials; /* true if we shouldn't worry about 475 * inserting special commands into 476 * the input stream. */ 477 Boolean shutUp = FALSE; /* true if we put a no echo command 478 * into the command file */ 479 Boolean errOff = FALSE; /* true if we turned error checking 480 * off before printing the command 481 * and need to turn it back on */ 482 const char *cmdTemplate; /* Template to use when printing the 483 * command */ 484 char *cmdStart; /* Start of expanded command */ 485 LstNode *cmdNode; /* Node for replacing the command */ 486 char *cmd = cmdp; 487 Job *job = jobp; 488 489 noSpecials = (noExecute && !(job->node->type & OP_MAKE)); 490 491 if (strcmp(cmd, "...") == 0) { 492 job->node->type |= OP_SAVE_CMDS; 493 if ((job->flags & JOB_IGNDOTS) == 0) { 494 job->tailCmds = Lst_Succ(Lst_Member(&job->node->commands, cmd)); 495 return (1); 496 } 497 return (0); 498 } 499 500#define DBPRINTF(fmt, arg) \ 501 DEBUGF(JOB, (fmt, arg)); \ 502 fprintf(job->cmdFILE, fmt, arg); \ 503 fflush(job->cmdFILE); 504 505 numCommands += 1; 506 507 /* 508 * For debugging, we replace each command with the result of expanding 509 * the variables in the command. 510 */ 511 cmdNode = Lst_Member(&job->node->commands, cmd); 512 cmdStart = cmd = Var_Subst(NULL, cmd, job->node, FALSE); 513 Lst_Replace(cmdNode, cmdStart); 514 515 cmdTemplate = "%s\n"; 516 517 /* 518 * Check for leading @', -' or +'s to control echoing, error checking, 519 * and execution on -n. 520 */ 521 while (*cmd == '@' || *cmd == '-' || *cmd == '+') { 522 switch (*cmd) { 523 524 case '@': 525 shutUp = DEBUG(LOUD) ? FALSE : TRUE; 526 break; 527 528 case '-': 529 errOff = TRUE; 530 break; 531 532 case '+': 533 if (noSpecials) { 534 /* 535 * We're not actually exececuting anything... 536 * but this one needs to be - use compat mode just for it. 537 */ 538 Compat_RunCommand(cmdp, job->node); 539 return (0); 540 } 541 break; 542 } 543 cmd++; 544 } 545 546 while (isspace((unsigned char)*cmd)) 547 cmd++; 548 549 if (shutUp) { 550 if (!(job->flags & JOB_SILENT) && !noSpecials && 551 commandShell->hasEchoCtl) { 552 DBPRINTF("%s\n", commandShell->echoOff); 553 } else { 554 shutUp = FALSE; 555 } 556 } 557 558 if (errOff) { 559 if ( !(job->flags & JOB_IGNERR) && !noSpecials) { 560 if (commandShell->hasErrCtl) { 561 /* 562 * we don't want the error-control commands showing 563 * up either, so we turn off echoing while executing 564 * them. We could put another field in the shell 565 * structure to tell JobDoOutput to look for this 566 * string too, but why make it any more complex than 567 * it already is? 568 */ 569 if (!(job->flags & JOB_SILENT) && !shutUp && 570 commandShell->hasEchoCtl) { 571 DBPRINTF("%s\n", commandShell->echoOff); 572 DBPRINTF("%s\n", commandShell->ignErr); 573 DBPRINTF("%s\n", commandShell->echoOn); 574 } else { 575 DBPRINTF("%s\n", commandShell->ignErr); 576 } 577 } else if (commandShell->ignErr && 578 (*commandShell->ignErr != '\0')) 579 { 580 /* 581 * The shell has no error control, so we need to be 582 * weird to get it to ignore any errors from the command. 583 * If echoing is turned on, we turn it off and use the 584 * errCheck template to echo the command. Leave echoing 585 * off so the user doesn't see the weirdness we go through 586 * to ignore errors. Set cmdTemplate to use the weirdness 587 * instead of the simple "%s\n" template. 588 */ 589 if (!(job->flags & JOB_SILENT) && !shutUp && 590 commandShell->hasEchoCtl) { 591 DBPRINTF("%s\n", commandShell->echoOff); 592 DBPRINTF(commandShell->errCheck, cmd); 593 shutUp = TRUE; 594 } 595 cmdTemplate = commandShell->ignErr; 596 /* 597 * The error ignoration (hee hee) is already taken care 598 * of by the ignErr template, so pretend error checking 599 * is still on. 600 */ 601 errOff = FALSE; 602 } else { 603 errOff = FALSE; 604 } 605 } else { 606 errOff = FALSE; 607 } 608 } 609 610 DBPRINTF(cmdTemplate, cmd); 611 612 if (errOff) { 613 /* 614 * If echoing is already off, there's no point in issuing the 615 * echoOff command. Otherwise we issue it and pretend it was on 616 * for the whole command... 617 */ 618 if (!shutUp && !(job->flags & JOB_SILENT) && commandShell->hasEchoCtl) { 619 DBPRINTF("%s\n", commandShell->echoOff); 620 shutUp = TRUE; 621 } 622 DBPRINTF("%s\n", commandShell->errCheck); 623 } 624 if (shutUp) { 625 DBPRINTF("%s\n", commandShell->echoOn); 626 } 627 return (0); 628} 629 630/*- 631 *----------------------------------------------------------------------- 632 * JobSaveCommand -- 633 * Save a command to be executed when everything else is done. 634 * Callback function for JobFinish... 635 * 636 * Results: 637 * Always returns 0 638 * 639 * Side Effects: 640 * The command is tacked onto the end of postCommands's commands list. 641 * 642 *----------------------------------------------------------------------- 643 */ 644static int 645JobSaveCommand(void *cmd, void *gn) 646{ 647 648 cmd = Var_Subst(NULL, cmd, gn, FALSE); 649 Lst_AtEnd(&postCommands->commands, cmd); 650 return (0); 651} 652 653 654/*- 655 *----------------------------------------------------------------------- 656 * JobClose -- 657 * Called to close both input and output pipes when a job is finished. 658 * 659 * Results: 660 * Nada 661 * 662 * Side Effects: 663 * The file descriptors associated with the job are closed. 664 * 665 *----------------------------------------------------------------------- 666 */ 667static void 668JobClose(Job *job) 669{ 670 671 if (usePipes) { 672#if !defined(USE_KQUEUE) 673 FD_CLR(job->inPipe, &outputs); 674#endif 675 if (job->outPipe != job->inPipe) { 676 close(job->outPipe); 677 } 678 JobDoOutput(job, TRUE); 679 close(job->inPipe); 680 } else { 681 close(job->outFd); 682 JobDoOutput(job, TRUE); 683 } 684} 685 686/*- 687 *----------------------------------------------------------------------- 688 * JobFinish -- 689 * Do final processing for the given job including updating 690 * parents and starting new jobs as available/necessary. Note 691 * that we pay no attention to the JOB_IGNERR flag here. 692 * This is because when we're called because of a noexecute flag 693 * or something, jstat.w_status is 0 and when called from 694 * Job_CatchChildren, the status is zeroed if it s/b ignored. 695 * 696 * Results: 697 * None 698 * 699 * Side Effects: 700 * Some nodes may be put on the toBeMade queue. 701 * Final commands for the job are placed on postCommands. 702 * 703 * If we got an error and are aborting (aborting == ABORT_ERROR) and 704 * the job list is now empty, we are done for the day. 705 * If we recognized an error (errors !=0), we set the aborting flag 706 * to ABORT_ERROR so no more jobs will be started. 707 *----------------------------------------------------------------------- 708 */ 709/*ARGSUSED*/ 710static void 711JobFinish(Job *job, int *status) 712{ 713 Boolean done; 714 715 if ((WIFEXITED(*status) && 716 (((WEXITSTATUS(*status) != 0) && !(job->flags & JOB_IGNERR)))) || 717 (WIFSIGNALED(*status) && (WTERMSIG(*status) != SIGCONT))) 718 { 719 /* 720 * If it exited non-zero and either we're doing things our 721 * way or we're not ignoring errors, the job is finished. 722 * Similarly, if the shell died because of a signal 723 * the job is also finished. In these 724 * cases, finish out the job's output before printing the exit 725 * status... 726 */ 727 JobClose(job); 728 if (job->cmdFILE != NULL && job->cmdFILE != stdout) { 729 fclose(job->cmdFILE); 730 } 731 done = TRUE; 732 } else if (WIFEXITED(*status)) { 733 /* 734 * Deal with ignored errors in -B mode. We need to print a message 735 * telling of the ignored error as well as setting status.w_status 736 * to 0 so the next command gets run. To do this, we set done to be 737 * TRUE if in -B mode and the job exited non-zero. 738 */ 739 done = WEXITSTATUS(*status) != 0; 740 /* 741 * Old comment said: "Note we don't 742 * want to close down any of the streams until we know we're at the 743 * end." 744 * But we do. Otherwise when are we going to print the rest of the 745 * stuff? 746 */ 747 JobClose(job); 748 } else { 749 /* 750 * No need to close things down or anything. 751 */ 752 done = FALSE; 753 } 754 755 if (done || 756 WIFSTOPPED(*status) || 757 (WIFSIGNALED(*status) && (WTERMSIG(*status) == SIGCONT)) || 758 DEBUG(JOB)) 759 { 760 FILE *out; 761 762 if (compatMake && !usePipes && (job->flags & JOB_IGNERR)) { 763 /* 764 * If output is going to a file and this job is ignoring 765 * errors, arrange to have the exit status sent to the 766 * output file as well. 767 */ 768 out = fdopen(job->outFd, "w"); 769 if (out == NULL) 770 Punt("Cannot fdopen"); 771 } else { 772 out = stdout; 773 } 774 775 if (WIFEXITED(*status)) { 776 DEBUGF(JOB, ("Process %d exited.\n", job->pid)); 777 if (WEXITSTATUS(*status) != 0) { 778 if (usePipes && job->node != lastNode) { 779 MESSAGE(out, job->node); 780 lastNode = job->node; 781 } 782 fprintf(out, "*** Error code %d%s\n", 783 WEXITSTATUS(*status), 784 (job->flags & JOB_IGNERR) ? "(ignored)" : ""); 785 786 if (job->flags & JOB_IGNERR) { 787 *status = 0; 788 } 789 } else if (DEBUG(JOB)) { 790 if (usePipes && job->node != lastNode) { 791 MESSAGE(out, job->node); 792 lastNode = job->node; 793 } 794 fprintf(out, "*** Completed successfully\n"); 795 } 796 } else if (WIFSTOPPED(*status)) { 797 DEBUGF(JOB, ("Process %d stopped.\n", job->pid)); 798 if (usePipes && job->node != lastNode) { 799 MESSAGE(out, job->node); 800 lastNode = job->node; 801 } 802 fprintf(out, "*** Stopped -- signal %d\n", 803 WSTOPSIG(*status)); 804 job->flags |= JOB_RESUME; 805 Lst_AtEnd(&stoppedJobs, job); 806 fflush(out); 807 return; 808 } else if (WTERMSIG(*status) == SIGCONT) { 809 /* 810 * If the beastie has continued, shift the Job from the stopped 811 * list to the running one (or re-stop it if concurrency is 812 * exceeded) and go and get another child. 813 */ 814 if (job->flags & (JOB_RESUME|JOB_RESTART)) { 815 if (usePipes && job->node != lastNode) { 816 MESSAGE(out, job->node); 817 lastNode = job->node; 818 } 819 fprintf(out, "*** Continued\n"); 820 } 821 if (!(job->flags & JOB_CONTINUING)) { 822 DEBUGF(JOB, ("Warning: process %d was not continuing.\n", job->pid)); 823#ifdef notdef 824 /* 825 * We don't really want to restart a job from scratch just 826 * because it continued, especially not without killing the 827 * continuing process! That's why this is ifdef'ed out. 828 * FD - 9/17/90 829 */ 830 JobRestart(job); 831#endif 832 } 833 job->flags &= ~JOB_CONTINUING; 834 Lst_AtEnd(&jobs, job); 835 nJobs += 1; 836 DEBUGF(JOB, ("Process %d is continuing locally.\n", job->pid)); 837 if (nJobs == maxJobs) { 838 jobFull = TRUE; 839 DEBUGF(JOB, ("Job queue is full.\n")); 840 } 841 fflush(out); 842 return; 843 } else { 844 if (usePipes && job->node != lastNode) { 845 MESSAGE(out, job->node); 846 lastNode = job->node; 847 } 848 fprintf(out, "*** Signal %d\n", WTERMSIG(*status)); 849 } 850 851 fflush(out); 852 } 853 854 /* 855 * Now handle the -B-mode stuff. If the beast still isn't finished, 856 * try and restart the job on the next command. If JobStart says it's 857 * ok, it's ok. If there's an error, this puppy is done. 858 */ 859 if (compatMake && WIFEXITED(*status) && 860 Lst_Succ(job->node->compat_command) != NULL) { 861 switch (JobStart(job->node, job->flags & JOB_IGNDOTS, job)) { 862 case JOB_RUNNING: 863 done = FALSE; 864 break; 865 case JOB_ERROR: 866 done = TRUE; 867 W_SETEXITSTATUS(status, 1); 868 break; 869 case JOB_FINISHED: 870 /* 871 * If we got back a JOB_FINISHED code, JobStart has already 872 * called Make_Update and freed the job descriptor. We set 873 * done to false here to avoid fake cycles and double frees. 874 * JobStart needs to do the update so we can proceed up the 875 * graph when given the -n flag.. 876 */ 877 done = FALSE; 878 break; 879 default: 880 break; 881 } 882 } else { 883 done = TRUE; 884 } 885 886 887 if (done && 888 (aborting != ABORT_ERROR) && 889 (aborting != ABORT_INTERRUPT) && 890 (*status == 0)) 891 { 892 /* 893 * As long as we aren't aborting and the job didn't return a non-zero 894 * status that we shouldn't ignore, we call Make_Update to update 895 * the parents. In addition, any saved commands for the node are placed 896 * on the .END target. 897 */ 898 if (job->tailCmds != NULL) { 899 Lst_ForEachFrom(&job->node->commands, job->tailCmds, 900 JobSaveCommand, job->node); 901 } 902 job->node->made = MADE; 903 Make_Update(job->node); 904 free(job); 905 } else if (*status != 0) { 906 errors += 1; 907 free(job); 908 } 909 910 JobRestartJobs(); 911 912 /* 913 * Set aborting if any error. 914 */ 915 if (errors && !keepgoing && (aborting != ABORT_INTERRUPT)) { 916 /* 917 * If we found any errors in this batch of children and the -k flag 918 * wasn't given, we set the aborting flag so no more jobs get 919 * started. 920 */ 921 aborting = ABORT_ERROR; 922 } 923 924 if ((aborting == ABORT_ERROR) && Job_Empty()) 925 /* 926 * If we are aborting and the job table is now empty, we finish. 927 */ 928 Finish(errors); 929} 930 931/*- 932 *----------------------------------------------------------------------- 933 * Job_Touch -- 934 * Touch the given target. Called by JobStart when the -t flag was 935 * given. Prints messages unless told to be silent. 936 * 937 * Results: 938 * None 939 * 940 * Side Effects: 941 * The data modification of the file is changed. In addition, if the 942 * file did not exist, it is created. 943 *----------------------------------------------------------------------- 944 */ 945void 946Job_Touch(GNode *gn, Boolean silent) 947{ 948 int streamID; /* ID of stream opened to do the touch */ 949 struct utimbuf times; /* Times for utime() call */ 950 951 if (gn->type & (OP_JOIN | OP_USE | OP_EXEC | OP_OPTIONAL)) { 952 /* 953 * .JOIN, .USE, .ZEROTIME and .OPTIONAL targets are "virtual" targets 954 * and, as such, shouldn't really be created. 955 */ 956 return; 957 } 958 959 if (!silent) { 960 fprintf(stdout, "touch %s\n", gn->name); 961 fflush(stdout); 962 } 963 964 if (noExecute) { 965 return; 966 } 967 968 if (gn->type & OP_ARCHV) { 969 Arch_Touch(gn); 970 } else if (gn->type & OP_LIB) { 971 Arch_TouchLib(gn); 972 } else { 973 char *file = gn->path ? gn->path : gn->name; 974 975 times.actime = times.modtime = now; 976 if (utime(file, ×) < 0){ 977 streamID = open(file, O_RDWR | O_CREAT, 0666); 978 979 if (streamID >= 0) { 980 char c; 981 982 /* 983 * Read and write a byte to the file to change the 984 * modification time, then close the file. 985 */ 986 if (read(streamID, &c, 1) == 1) { 987 lseek(streamID, (off_t)0, SEEK_SET); 988 write(streamID, &c, 1); 989 } 990 991 close(streamID); 992 } else { 993 fprintf(stdout, "*** couldn't touch %s: %s", 994 file, strerror(errno)); 995 fflush(stdout); 996 } 997 } 998 } 999} 1000 1001/*- 1002 *----------------------------------------------------------------------- 1003 * Job_CheckCommands -- 1004 * Make sure the given node has all the commands it needs. 1005 * 1006 * Results: 1007 * TRUE if the commands list is/was ok. 1008 * 1009 * Side Effects: 1010 * The node will have commands from the .DEFAULT rule added to it 1011 * if it needs them. 1012 *----------------------------------------------------------------------- 1013 */ 1014Boolean 1015Job_CheckCommands(GNode *gn, void (*abortProc)(const char *, ...)) 1016{ 1017 1018 if (OP_NOP(gn->type) && Lst_IsEmpty(&gn->commands) && 1019 (gn->type & OP_LIB) == 0) { 1020 /* 1021 * No commands. Look for .DEFAULT rule from which we might infer 1022 * commands 1023 */ 1024 if ((DEFAULT != NULL) && !Lst_IsEmpty(&DEFAULT->commands)) { 1025 char *p1; 1026 /* 1027 * Make only looks for a .DEFAULT if the node was never the 1028 * target of an operator, so that's what we do too. If 1029 * a .DEFAULT was given, we substitute its commands for gn's 1030 * commands and set the IMPSRC variable to be the target's name 1031 * The DEFAULT node acts like a transformation rule, in that 1032 * gn also inherits any attributes or sources attached to 1033 * .DEFAULT itself. 1034 */ 1035 Make_HandleUse(DEFAULT, gn); 1036 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), gn); 1037 free(p1); 1038 } else if (Dir_MTime(gn) == 0) { 1039 /* 1040 * The node wasn't the target of an operator we have no .DEFAULT 1041 * rule to go on and the target doesn't already exist. There's 1042 * nothing more we can do for this branch. If the -k flag wasn't 1043 * given, we stop in our tracks, otherwise we just don't update 1044 * this node's parents so they never get examined. 1045 */ 1046 static const char msg[] = "make: don't know how to make"; 1047 1048 if (gn->type & OP_OPTIONAL) { 1049 fprintf(stdout, "%s %s(ignored)\n", msg, gn->name); 1050 fflush(stdout); 1051 } else if (keepgoing) { 1052 fprintf(stdout, "%s %s(continuing)\n", msg, gn->name); 1053 fflush(stdout); 1054 return (FALSE); 1055 } else { 1056#if OLD_JOKE 1057 if (strcmp(gn->name,"love") == 0) 1058 (*abortProc)("Not war."); 1059 else 1060#endif 1061 (*abortProc)("%s %s. Stop", msg, gn->name); 1062 return (FALSE); 1063 } 1064 } 1065 } 1066 return (TRUE); 1067} 1068 1069/*- 1070 *----------------------------------------------------------------------- 1071 * JobExec -- 1072 * Execute the shell for the given job. Called from JobStart and 1073 * JobRestart. 1074 * 1075 * Results: 1076 * None. 1077 * 1078 * Side Effects: 1079 * A shell is executed, outputs is altered and the Job structure added 1080 * to the job table. 1081 * 1082 *----------------------------------------------------------------------- 1083 */ 1084static void 1085JobExec(Job *job, char **argv) 1086{ 1087 int cpid; /* ID of new child */ 1088 1089 if (DEBUG(JOB)) { 1090 int i; 1091 1092 DEBUGF(JOB, ("Running %s\n", job->node->name)); 1093 DEBUGF(JOB, ("\tCommand: ")); 1094 for (i = 0; argv[i] != NULL; i++) { 1095 DEBUGF(JOB, ("%s ", argv[i])); 1096 } 1097 DEBUGF(JOB, ("\n")); 1098 } 1099 1100 /* 1101 * Some jobs produce no output and it's disconcerting to have 1102 * no feedback of their running (since they produce no output, the 1103 * banner with their name in it never appears). This is an attempt to 1104 * provide that feedback, even if nothing follows it. 1105 */ 1106 if ((lastNode != job->node) && (job->flags & JOB_FIRST) && 1107 !(job->flags & JOB_SILENT)) { 1108 MESSAGE(stdout, job->node); 1109 lastNode = job->node; 1110 } 1111 1112 if ((cpid = vfork()) == -1) { 1113 Punt("Cannot fork"); 1114 } else if (cpid == 0) { 1115 1116 if (fifoFd >= 0) 1117 close(fifoFd); 1118 /* 1119 * Must duplicate the input stream down to the child's input and 1120 * reset it to the beginning (again). Since the stream was marked 1121 * close-on-exec, we must clear that bit in the new input. 1122 */ 1123 if (dup2(FILENO(job->cmdFILE), 0) == -1) 1124 Punt("Cannot dup2: %s", strerror(errno)); 1125 fcntl(0, F_SETFD, 0); 1126 lseek(0, (off_t)0, SEEK_SET); 1127 1128 if (usePipes) { 1129 /* 1130 * Set up the child's output to be routed through the pipe 1131 * we've created for it. 1132 */ 1133 if (dup2(job->outPipe, 1) == -1) 1134 Punt("Cannot dup2: %s", strerror(errno)); 1135 } else { 1136 /* 1137 * We're capturing output in a file, so we duplicate the 1138 * descriptor to the temporary file into the standard 1139 * output. 1140 */ 1141 if (dup2(job->outFd, 1) == -1) 1142 Punt("Cannot dup2: %s", strerror(errno)); 1143 } 1144 /* 1145 * The output channels are marked close on exec. This bit was 1146 * duplicated by the dup2 (on some systems), so we have to clear 1147 * it before routing the shell's error output to the same place as 1148 * its standard output. 1149 */ 1150 fcntl(1, F_SETFD, 0); 1151 if (dup2(1, 2) == -1) 1152 Punt("Cannot dup2: %s", strerror(errno)); 1153 1154#ifdef USE_PGRP 1155 /* 1156 * We want to switch the child into a different process family so 1157 * we can kill it and all its descendants in one fell swoop, 1158 * by killing its process family, but not commit suicide. 1159 */ 1160# if defined(SYSV) 1161 setsid(); 1162# else 1163 setpgid(0, getpid()); 1164# endif 1165#endif /* USE_PGRP */ 1166 1167 execv(shellPath, argv); 1168 1169 write(STDERR_FILENO, "Could not execute shell\n", 1170 sizeof("Could not execute shell")); 1171 _exit(1); 1172 } else { 1173 job->pid = cpid; 1174 1175 if (usePipes && (job->flags & JOB_FIRST) ) { 1176 /* 1177 * The first time a job is run for a node, we set the current 1178 * position in the buffer to the beginning and mark another 1179 * stream to watch in the outputs mask 1180 */ 1181#ifdef USE_KQUEUE 1182 struct kevent kev[2]; 1183#endif 1184 job->curPos = 0; 1185 1186#if defined(USE_KQUEUE) 1187 EV_SET(&kev[0], job->inPipe, EVFILT_READ, EV_ADD, 0, 0, job); 1188 EV_SET(&kev[1], job->pid, EVFILT_PROC, EV_ADD | EV_ONESHOT, 1189 NOTE_EXIT, 0, NULL); 1190 if (kevent(kqfd, kev, 2, NULL, 0, NULL) != 0) { 1191 /* kevent() will fail if the job is already finished */ 1192 if (errno != EINTR && errno != EBADF && errno != ESRCH) 1193 Punt("kevent: %s", strerror(errno)); 1194 } 1195#else 1196 FD_SET(job->inPipe, &outputs); 1197#endif /* USE_KQUEUE */ 1198 } 1199 1200 if (job->cmdFILE != NULL && job->cmdFILE != stdout) { 1201 fclose(job->cmdFILE); 1202 job->cmdFILE = NULL; 1203 } 1204 } 1205 1206 /* 1207 * Now the job is actually running, add it to the table. 1208 */ 1209 nJobs += 1; 1210 Lst_AtEnd(&jobs, job); 1211 if (nJobs == maxJobs) { 1212 jobFull = TRUE; 1213 } 1214} 1215 1216/*- 1217 *----------------------------------------------------------------------- 1218 * JobMakeArgv -- 1219 * Create the argv needed to execute the shell for a given job. 1220 * 1221 * 1222 * Results: 1223 * 1224 * Side Effects: 1225 * 1226 *----------------------------------------------------------------------- 1227 */ 1228static void 1229JobMakeArgv(Job *job, char **argv) 1230{ 1231 int argc; 1232 static char args[10]; /* For merged arguments */ 1233 1234 argv[0] = shellName; 1235 argc = 1; 1236 1237 if ((commandShell->exit && (*commandShell->exit != '-')) || 1238 (commandShell->echo && (*commandShell->echo != '-'))) 1239 { 1240 /* 1241 * At least one of the flags doesn't have a minus before it, so 1242 * merge them together. Have to do this because the *(&(@*#*&#$# 1243 * Bourne shell thinks its second argument is a file to source. 1244 * Grrrr. Note the ten-character limitation on the combined arguments. 1245 */ 1246 sprintf(args, "-%s%s", 1247 ((job->flags & JOB_IGNERR) ? "" : 1248 (commandShell->exit ? commandShell->exit : "")), 1249 ((job->flags & JOB_SILENT) ? "" : 1250 (commandShell->echo ? commandShell->echo : ""))); 1251 1252 if (args[1]) { 1253 argv[argc] = args; 1254 argc++; 1255 } 1256 } else { 1257 if (!(job->flags & JOB_IGNERR) && commandShell->exit) { 1258 argv[argc] = commandShell->exit; 1259 argc++; 1260 } 1261 if (!(job->flags & JOB_SILENT) && commandShell->echo) { 1262 argv[argc] = commandShell->echo; 1263 argc++; 1264 } 1265 } 1266 argv[argc] = NULL; 1267} 1268 1269/*- 1270 *----------------------------------------------------------------------- 1271 * JobRestart -- 1272 * Restart a job that stopped for some reason. 1273 * 1274 * Results: 1275 * None. 1276 * 1277 * Side Effects: 1278 * jobFull will be set if the job couldn't be run. 1279 * 1280 *----------------------------------------------------------------------- 1281 */ 1282static void 1283JobRestart(Job *job) 1284{ 1285 1286 if (job->flags & JOB_RESTART) { 1287 /* 1288 * Set up the control arguments to the shell. This is based on the 1289 * flags set earlier for this job. If the JOB_IGNERR flag is clear, 1290 * the 'exit' flag of the commandShell is used to cause it to exit 1291 * upon receiving an error. If the JOB_SILENT flag is clear, the 1292 * 'echo' flag of the commandShell is used to get it to start echoing 1293 * as soon as it starts processing commands. 1294 */ 1295 char *argv[4]; 1296 1297 JobMakeArgv(job, argv); 1298 1299 DEBUGF(JOB, ("Restarting %s...", job->node->name)); 1300 if (((nJobs >= maxJobs) && !(job->flags & JOB_SPECIAL))) { 1301 /* 1302 * Can't be exported and not allowed to run locally -- put it 1303 * back on the hold queue and mark the table full 1304 */ 1305 DEBUGF(JOB, ("holding\n")); 1306 Lst_AtFront(&stoppedJobs, (void *)job); 1307 jobFull = TRUE; 1308 DEBUGF(JOB, ("Job queue is full.\n")); 1309 return; 1310 } else { 1311 /* 1312 * Job may be run locally. 1313 */ 1314 DEBUGF(JOB, ("running locally\n")); 1315 } 1316 JobExec(job, argv); 1317 } else { 1318 /* 1319 * The job has stopped and needs to be restarted. Why it stopped, 1320 * we don't know... 1321 */ 1322 DEBUGF(JOB, ("Resuming %s...", job->node->name)); 1323 if (((nJobs < maxJobs) || 1324 ((job->flags & JOB_SPECIAL) && 1325 (maxJobs == 0))) && 1326 (nJobs != maxJobs)) 1327 { 1328 /* 1329 * If we haven't reached the concurrency limit already (or the 1330 * job must be run and maxJobs is 0), it's ok to resume it. 1331 */ 1332 Boolean error; 1333 int status; 1334 1335 error = (KILL(job->pid, SIGCONT) != 0); 1336 1337 if (!error) { 1338 /* 1339 * Make sure the user knows we've continued the beast and 1340 * actually put the thing in the job table. 1341 */ 1342 job->flags |= JOB_CONTINUING; 1343 W_SETTERMSIG(&status, SIGCONT); 1344 JobFinish(job, &status); 1345 1346 job->flags &= ~(JOB_RESUME|JOB_CONTINUING); 1347 DEBUGF(JOB, ("done\n")); 1348 } else { 1349 Error("couldn't resume %s: %s", 1350 job->node->name, strerror(errno)); 1351 status = 0; 1352 W_SETEXITSTATUS(&status, 1); 1353 JobFinish(job, &status); 1354 } 1355 } else { 1356 /* 1357 * Job cannot be restarted. Mark the table as full and 1358 * place the job back on the list of stopped jobs. 1359 */ 1360 DEBUGF(JOB, ("table full\n")); 1361 Lst_AtFront(&stoppedJobs, (void *)job); 1362 jobFull = TRUE; 1363 DEBUGF(JOB, ("Job queue is full.\n")); 1364 } 1365 } 1366} 1367 1368/*- 1369 *----------------------------------------------------------------------- 1370 * JobStart -- 1371 * Start a target-creation process going for the target described 1372 * by the graph node gn. 1373 * 1374 * Results: 1375 * JOB_ERROR if there was an error in the commands, JOB_FINISHED 1376 * if there isn't actually anything left to do for the job and 1377 * JOB_RUNNING if the job has been started. 1378 * 1379 * Side Effects: 1380 * A new Job node is created and added to the list of running 1381 * jobs. PMake is forked and a child shell created. 1382 *----------------------------------------------------------------------- 1383 */ 1384static int 1385JobStart(GNode *gn, int flags, Job *previous) 1386{ 1387 Job *job; /* new job descriptor */ 1388 char *argv[4]; /* Argument vector to shell */ 1389 Boolean cmdsOK; /* true if the nodes commands were all right */ 1390 Boolean noExec; /* Set true if we decide not to run the job */ 1391 int tfd; /* File descriptor for temp file */ 1392 1393 if (interrupted) { 1394 JobPassSig(interrupted); 1395 return (JOB_ERROR); 1396 } 1397 if (previous != NULL) { 1398 previous->flags &= ~(JOB_FIRST|JOB_IGNERR|JOB_SILENT); 1399 job = previous; 1400 } else { 1401 job = emalloc(sizeof(Job)); 1402 flags |= JOB_FIRST; 1403 } 1404 1405 job->node = gn; 1406 job->tailCmds = NULL; 1407 1408 /* 1409 * Set the initial value of the flags for this job based on the global 1410 * ones and the node's attributes... Any flags supplied by the caller 1411 * are also added to the field. 1412 */ 1413 job->flags = 0; 1414 if (Targ_Ignore(gn)) { 1415 job->flags |= JOB_IGNERR; 1416 } 1417 if (Targ_Silent(gn)) { 1418 job->flags |= JOB_SILENT; 1419 } 1420 job->flags |= flags; 1421 1422 /* 1423 * Check the commands now so any attributes from .DEFAULT have a chance 1424 * to migrate to the node 1425 */ 1426 if (!compatMake && job->flags & JOB_FIRST) { 1427 cmdsOK = Job_CheckCommands(gn, Error); 1428 } else { 1429 cmdsOK = TRUE; 1430 } 1431 1432 /* 1433 * If the -n flag wasn't given, we open up OUR (not the child's) 1434 * temporary file to stuff commands in it. The thing is rd/wr so we don't 1435 * need to reopen it to feed it to the shell. If the -n flag *was* given, 1436 * we just set the file to be stdout. Cute, huh? 1437 */ 1438 if ((gn->type & OP_MAKE) || (!noExecute && !touchFlag)) { 1439 /* 1440 * We're serious here, but if the commands were bogus, we're 1441 * also dead... 1442 */ 1443 if (!cmdsOK) { 1444 DieHorribly(); 1445 } 1446 1447 strcpy(tfile, TMPPAT); 1448 if ((tfd = mkstemp(tfile)) == -1) 1449 Punt("Cannot create temp file: %s", strerror(errno)); 1450 job->cmdFILE = fdopen(tfd, "w+"); 1451 eunlink(tfile); 1452 if (job->cmdFILE == NULL) { 1453 close(tfd); 1454 Punt("Could not open %s", tfile); 1455 } 1456 fcntl(FILENO(job->cmdFILE), F_SETFD, 1); 1457 /* 1458 * Send the commands to the command file, flush all its buffers then 1459 * rewind and remove the thing. 1460 */ 1461 noExec = FALSE; 1462 1463 /* 1464 * used to be backwards; replace when start doing multiple commands 1465 * per shell. 1466 */ 1467 if (compatMake) { 1468 /* 1469 * Be compatible: If this is the first time for this node, 1470 * verify its commands are ok and open the commands list for 1471 * sequential access by later invocations of JobStart. 1472 * Once that is done, we take the next command off the list 1473 * and print it to the command file. If the command was an 1474 * ellipsis, note that there's nothing more to execute. 1475 */ 1476 if (job->flags & JOB_FIRST) 1477 gn->compat_command = Lst_First(&gn->commands); 1478 else 1479 gn->compat_command = Lst_Succ(gn->compat_command); 1480 1481 if (gn->compat_command == NULL || 1482 JobPrintCommand(Lst_Datum(gn->compat_command), job)) 1483 noExec = TRUE; 1484 1485 if (noExec && !(job->flags & JOB_FIRST)) { 1486 /* 1487 * If we're not going to execute anything, the job 1488 * is done and we need to close down the various 1489 * file descriptors we've opened for output, then 1490 * call JobDoOutput to catch the final characters or 1491 * send the file to the screen... Note that the i/o streams 1492 * are only open if this isn't the first job. 1493 * Note also that this could not be done in 1494 * Job_CatchChildren b/c it wasn't clear if there were 1495 * more commands to execute or not... 1496 */ 1497 JobClose(job); 1498 } 1499 } else { 1500 /* 1501 * We can do all the commands at once. hooray for sanity 1502 */ 1503 numCommands = 0; 1504 Lst_ForEach(&gn->commands, JobPrintCommand, job); 1505 1506 /* 1507 * If we didn't print out any commands to the shell script, 1508 * there's not much point in executing the shell, is there? 1509 */ 1510 if (numCommands == 0) { 1511 noExec = TRUE; 1512 } 1513 } 1514 } else if (noExecute) { 1515 /* 1516 * Not executing anything -- just print all the commands to stdout 1517 * in one fell swoop. This will still set up job->tailCmds correctly. 1518 */ 1519 if (lastNode != gn) { 1520 MESSAGE(stdout, gn); 1521 lastNode = gn; 1522 } 1523 job->cmdFILE = stdout; 1524 /* 1525 * Only print the commands if they're ok, but don't die if they're 1526 * not -- just let the user know they're bad and keep going. It 1527 * doesn't do any harm in this case and may do some good. 1528 */ 1529 if (cmdsOK) { 1530 Lst_ForEach(&gn->commands, JobPrintCommand, job); 1531 } 1532 /* 1533 * Don't execute the shell, thank you. 1534 */ 1535 noExec = TRUE; 1536 } else { 1537 /* 1538 * Just touch the target and note that no shell should be executed. 1539 * Set cmdFILE to stdout to make life easier. Check the commands, too, 1540 * but don't die if they're no good -- it does no harm to keep working 1541 * up the graph. 1542 */ 1543 job->cmdFILE = stdout; 1544 Job_Touch(gn, job->flags & JOB_SILENT); 1545 noExec = TRUE; 1546 } 1547 1548 /* 1549 * If we're not supposed to execute a shell, don't. 1550 */ 1551 if (noExec) { 1552 /* 1553 * Unlink and close the command file if we opened one 1554 */ 1555 if (job->cmdFILE != stdout) { 1556 if (job->cmdFILE != NULL) 1557 fclose(job->cmdFILE); 1558 } else { 1559 fflush(stdout); 1560 } 1561 1562 /* 1563 * We only want to work our way up the graph if we aren't here because 1564 * the commands for the job were no good. 1565 */ 1566 if (cmdsOK) { 1567 if (aborting == 0) { 1568 if (job->tailCmds != NULL) { 1569 Lst_ForEachFrom(&job->node->commands, job->tailCmds, 1570 JobSaveCommand, job->node); 1571 } 1572 job->node->made = MADE; 1573 Make_Update(job->node); 1574 } 1575 free(job); 1576 return(JOB_FINISHED); 1577 } else { 1578 free(job); 1579 return(JOB_ERROR); 1580 } 1581 } else { 1582 fflush(job->cmdFILE); 1583 } 1584 1585 /* 1586 * Set up the control arguments to the shell. This is based on the flags 1587 * set earlier for this job. 1588 */ 1589 JobMakeArgv(job, argv); 1590 1591 /* 1592 * If we're using pipes to catch output, create the pipe by which we'll 1593 * get the shell's output. If we're using files, print out that we're 1594 * starting a job and then set up its temporary-file name. 1595 */ 1596 if (!compatMake || (job->flags & JOB_FIRST)) { 1597 if (usePipes) { 1598 int fd[2]; 1599 1600 if (pipe(fd) == -1) 1601 Punt("Cannot create pipe: %s", strerror(errno)); 1602 job->inPipe = fd[0]; 1603 job->outPipe = fd[1]; 1604 fcntl(job->inPipe, F_SETFD, 1); 1605 fcntl(job->outPipe, F_SETFD, 1); 1606 } else { 1607 fprintf(stdout, "Remaking `%s'\n", gn->name); 1608 fflush(stdout); 1609 strcpy(job->outFile, TMPPAT); 1610 if ((job->outFd = mkstemp(job->outFile)) == -1) 1611 Punt("cannot create temp file: %s", strerror(errno)); 1612 fcntl(job->outFd, F_SETFD, 1); 1613 } 1614 } 1615 1616 if ((nJobs >= maxJobs) && !(job->flags & JOB_SPECIAL) && (maxJobs != 0)) { 1617 /* 1618 * We've hit the limit of concurrency, so put the job on hold until 1619 * some other job finishes. Note that the special jobs (.BEGIN, 1620 * .INTERRUPT and .END) may be run even when the limit has been reached 1621 * (e.g. when maxJobs == 0). 1622 */ 1623 jobFull = TRUE; 1624 1625 DEBUGF(JOB, ("Can only run job locally.\n")); 1626 job->flags |= JOB_RESTART; 1627 Lst_AtEnd(&stoppedJobs, job); 1628 } else { 1629 if (nJobs >= maxJobs) { 1630 /* 1631 * If we're running this job locally as a special case (see above), 1632 * at least say the table is full. 1633 */ 1634 jobFull = TRUE; 1635 DEBUGF(JOB, ("Local job queue is full.\n")); 1636 } 1637 JobExec(job, argv); 1638 } 1639 return (JOB_RUNNING); 1640} 1641 1642static char * 1643JobOutput(Job *job, char *cp, char *endp, int msg) 1644{ 1645 char *ecp; 1646 1647 if (commandShell->noPrint) { 1648 ecp = strstr(cp, commandShell->noPrint); 1649 while (ecp != NULL) { 1650 if (cp != ecp) { 1651 *ecp = '\0'; 1652 if (msg && job->node != lastNode) { 1653 MESSAGE(stdout, job->node); 1654 lastNode = job->node; 1655 } 1656 /* 1657 * The only way there wouldn't be a newline after 1658 * this line is if it were the last in the buffer. 1659 * however, since the non-printable comes after it, 1660 * there must be a newline, so we don't print one. 1661 */ 1662 fprintf(stdout, "%s", cp); 1663 fflush(stdout); 1664 } 1665 cp = ecp + commandShell->noPLen; 1666 if (cp != endp) { 1667 /* 1668 * Still more to print, look again after skipping 1669 * the whitespace following the non-printable 1670 * command.... 1671 */ 1672 cp++; 1673 while (*cp == ' ' || *cp == '\t' || *cp == '\n') { 1674 cp++; 1675 } 1676 ecp = strstr(cp, commandShell->noPrint); 1677 } else { 1678 return (cp); 1679 } 1680 } 1681 } 1682 return (cp); 1683} 1684 1685/*- 1686 *----------------------------------------------------------------------- 1687 * JobDoOutput -- 1688 * This function is called at different times depending on 1689 * whether the user has specified that output is to be collected 1690 * via pipes or temporary files. In the former case, we are called 1691 * whenever there is something to read on the pipe. We collect more 1692 * output from the given job and store it in the job's outBuf. If 1693 * this makes up a line, we print it tagged by the job's identifier, 1694 * as necessary. 1695 * If output has been collected in a temporary file, we open the 1696 * file and read it line by line, transfering it to our own 1697 * output channel until the file is empty. At which point we 1698 * remove the temporary file. 1699 * In both cases, however, we keep our figurative eye out for the 1700 * 'noPrint' line for the shell from which the output came. If 1701 * we recognize a line, we don't print it. If the command is not 1702 * alone on the line (the character after it is not \0 or \n), we 1703 * do print whatever follows it. 1704 * 1705 * Results: 1706 * None 1707 * 1708 * Side Effects: 1709 * curPos may be shifted as may the contents of outBuf. 1710 *----------------------------------------------------------------------- 1711 */ 1712STATIC void 1713JobDoOutput(Job *job, Boolean finish) 1714{ 1715 Boolean gotNL = FALSE; /* true if got a newline */ 1716 Boolean fbuf; /* true if our buffer filled up */ 1717 int nr; /* number of bytes read */ 1718 int i; /* auxiliary index into outBuf */ 1719 int max; /* limit for i (end of current data) */ 1720 int nRead; /* (Temporary) number of bytes read */ 1721 1722 FILE *oFILE; /* Stream pointer to shell's output file */ 1723 char inLine[132]; 1724 1725 if (usePipes) { 1726 /* 1727 * Read as many bytes as will fit in the buffer. 1728 */ 1729end_loop: 1730 gotNL = FALSE; 1731 fbuf = FALSE; 1732 1733 nRead = read(job->inPipe, &job->outBuf[job->curPos], 1734 JOB_BUFSIZE - job->curPos); 1735 /* 1736 * Check for interrupt here too, because the above read may block 1737 * when the child process is stopped. In this case the interrupt 1738 * will unblock it (we don't use SA_RESTART). 1739 */ 1740 if (interrupted) 1741 JobPassSig(interrupted); 1742 1743 if (nRead < 0) { 1744 DEBUGF(JOB, ("JobDoOutput(piperead)")); 1745 nr = 0; 1746 } else { 1747 nr = nRead; 1748 } 1749 1750 /* 1751 * If we hit the end-of-file (the job is dead), we must flush its 1752 * remaining output, so pretend we read a newline if there's any 1753 * output remaining in the buffer. 1754 * Also clear the 'finish' flag so we stop looping. 1755 */ 1756 if ((nr == 0) && (job->curPos != 0)) { 1757 job->outBuf[job->curPos] = '\n'; 1758 nr = 1; 1759 finish = FALSE; 1760 } else if (nr == 0) { 1761 finish = FALSE; 1762 } 1763 1764 /* 1765 * Look for the last newline in the bytes we just got. If there is 1766 * one, break out of the loop with 'i' as its index and gotNL set 1767 * TRUE. 1768 */ 1769 max = job->curPos + nr; 1770 for (i = job->curPos + nr - 1; i >= job->curPos; i--) { 1771 if (job->outBuf[i] == '\n') { 1772 gotNL = TRUE; 1773 break; 1774 } else if (job->outBuf[i] == '\0') { 1775 /* 1776 * Why? 1777 */ 1778 job->outBuf[i] = ' '; 1779 } 1780 } 1781 1782 if (!gotNL) { 1783 job->curPos += nr; 1784 if (job->curPos == JOB_BUFSIZE) { 1785 /* 1786 * If we've run out of buffer space, we have no choice 1787 * but to print the stuff. sigh. 1788 */ 1789 fbuf = TRUE; 1790 i = job->curPos; 1791 } 1792 } 1793 if (gotNL || fbuf) { 1794 /* 1795 * Need to send the output to the screen. Null terminate it 1796 * first, overwriting the newline character if there was one. 1797 * So long as the line isn't one we should filter (according 1798 * to the shell description), we print the line, preceded 1799 * by a target banner if this target isn't the same as the 1800 * one for which we last printed something. 1801 * The rest of the data in the buffer are then shifted down 1802 * to the start of the buffer and curPos is set accordingly. 1803 */ 1804 job->outBuf[i] = '\0'; 1805 if (i >= job->curPos) { 1806 char *cp; 1807 1808 cp = JobOutput(job, job->outBuf, &job->outBuf[i], FALSE); 1809 1810 /* 1811 * There's still more in that thar buffer. This time, though, 1812 * we know there's no newline at the end, so we add one of 1813 * our own free will. 1814 */ 1815 if (*cp != '\0') { 1816 if (job->node != lastNode) { 1817 MESSAGE(stdout, job->node); 1818 lastNode = job->node; 1819 } 1820 fprintf(stdout, "%s%s", cp, gotNL ? "\n" : ""); 1821 fflush(stdout); 1822 } 1823 } 1824 if (i < max - 1) { 1825 /* shift the remaining characters down */ 1826 memcpy(job->outBuf, &job->outBuf[i + 1], max - (i + 1)); 1827 job->curPos = max - (i + 1); 1828 1829 } else { 1830 /* 1831 * We have written everything out, so we just start over 1832 * from the start of the buffer. No copying. No nothing. 1833 */ 1834 job->curPos = 0; 1835 } 1836 } 1837 if (finish) { 1838 /* 1839 * If the finish flag is true, we must loop until we hit 1840 * end-of-file on the pipe. This is guaranteed to happen 1841 * eventually since the other end of the pipe is now closed 1842 * (we closed it explicitly and the child has exited). When 1843 * we do get an EOF, finish will be set FALSE and we'll fall 1844 * through and out. 1845 */ 1846 goto end_loop; 1847 } 1848 } else { 1849 /* 1850 * We've been called to retrieve the output of the job from the 1851 * temporary file where it's been squirreled away. This consists of 1852 * opening the file, reading the output line by line, being sure not 1853 * to print the noPrint line for the shell we used, then close and 1854 * remove the temporary file. Very simple. 1855 * 1856 * Change to read in blocks and do FindSubString type things as for 1857 * pipes? That would allow for "@echo -n..." 1858 */ 1859 oFILE = fopen(job->outFile, "r"); 1860 if (oFILE != NULL) { 1861 fprintf(stdout, "Results of making %s:\n", job->node->name); 1862 fflush(stdout); 1863 while (fgets(inLine, sizeof(inLine), oFILE) != NULL) { 1864 char *cp, *endp, *oendp; 1865 1866 cp = inLine; 1867 oendp = endp = inLine + strlen(inLine); 1868 if (endp[-1] == '\n') { 1869 *--endp = '\0'; 1870 } 1871 cp = JobOutput(job, inLine, endp, FALSE); 1872 1873 /* 1874 * There's still more in that thar buffer. This time, though, 1875 * we know there's no newline at the end, so we add one of 1876 * our own free will. 1877 */ 1878 fprintf(stdout, "%s", cp); 1879 fflush(stdout); 1880 if (endp != oendp) { 1881 fprintf(stdout, "\n"); 1882 fflush(stdout); 1883 } 1884 } 1885 fclose(oFILE); 1886 eunlink(job->outFile); 1887 } 1888 } 1889} 1890 1891/*- 1892 *----------------------------------------------------------------------- 1893 * Job_CatchChildren -- 1894 * Handle the exit of a child. Called from Make_Make. 1895 * 1896 * Results: 1897 * none. 1898 * 1899 * Side Effects: 1900 * The job descriptor is removed from the list of children. 1901 * 1902 * Notes: 1903 * We do waits, blocking or not, according to the wisdom of our 1904 * caller, until there are no more children to report. For each 1905 * job, call JobFinish to finish things off. This will take care of 1906 * putting jobs on the stoppedJobs queue. 1907 * 1908 *----------------------------------------------------------------------- 1909 */ 1910void 1911Job_CatchChildren(Boolean block) 1912{ 1913 int pid; /* pid of dead child */ 1914 Job *job; /* job descriptor for dead child */ 1915 LstNode *jnode; /* list element for finding job */ 1916 int status; /* Exit/termination status */ 1917 1918 /* 1919 * Don't even bother if we know there's no one around. 1920 */ 1921 if (nJobs == 0) { 1922 return; 1923 } 1924 1925 for (;;) { 1926 pid = waitpid((pid_t)-1, &status, (block ? 0 : WNOHANG) | WUNTRACED); 1927 if (pid <= 0) 1928 break; 1929 DEBUGF(JOB, ("Process %d exited or stopped.\n", pid)); 1930 1931 jnode = Lst_Find(&jobs, &pid, JobCmpPid); 1932 1933 if (jnode == NULL) { 1934 if (WIFSIGNALED(status) && (WTERMSIG(status) == SIGCONT)) { 1935 jnode = Lst_Find(&stoppedJobs, &pid, JobCmpPid); 1936 if (jnode == NULL) { 1937 Error("Resumed child (%d) not in table", pid); 1938 continue; 1939 } 1940 job = Lst_Datum(jnode); 1941 Lst_Remove(&stoppedJobs, jnode); 1942 } else { 1943 Error("Child (%d) not in table?", pid); 1944 continue; 1945 } 1946 } else { 1947 job = Lst_Datum(jnode); 1948 Lst_Remove(&jobs, jnode); 1949 nJobs -= 1; 1950 if (fifoFd >= 0 && maxJobs > 1) { 1951 write(fifoFd, "+", 1); 1952 maxJobs--; 1953 if (nJobs >= maxJobs) 1954 jobFull = TRUE; 1955 else 1956 jobFull = FALSE; 1957 } else { 1958 DEBUGF(JOB, ("Job queue is no longer full.\n")); 1959 jobFull = FALSE; 1960 } 1961 } 1962 1963 JobFinish(job, &status); 1964 } 1965 if (interrupted) 1966 JobPassSig(interrupted); 1967} 1968 1969/*- 1970 *----------------------------------------------------------------------- 1971 * Job_CatchOutput -- 1972 * Catch the output from our children, if we're using 1973 * pipes do so. Otherwise just block time until we get a 1974 * signal(most likely a SIGCHLD) since there's no point in 1975 * just spinning when there's nothing to do and the reaping 1976 * of a child can wait for a while. 1977 * 1978 * Results: 1979 * None 1980 * 1981 * Side Effects: 1982 * Output is read from pipes if we're piping. 1983 * ----------------------------------------------------------------------- 1984 */ 1985void 1986#ifdef USE_KQUEUE 1987Job_CatchOutput(int flag __unused) 1988#else 1989Job_CatchOutput(int flag) 1990#endif 1991{ 1992 int nfds; 1993#ifdef USE_KQUEUE 1994#define KEV_SIZE 4 1995 struct kevent kev[KEV_SIZE]; 1996 int i; 1997#else 1998 struct timeval timeout; 1999 fd_set readfds; 2000 LstNode *ln; 2001 Job *job; 2002#endif 2003 2004 fflush(stdout); 2005 2006 if (usePipes) { 2007#ifdef USE_KQUEUE 2008 if ((nfds = kevent(kqfd, NULL, 0, kev, KEV_SIZE, NULL)) == -1) { 2009 if (errno != EINTR) 2010 Punt("kevent: %s", strerror(errno)); 2011 if (interrupted) 2012 JobPassSig(interrupted); 2013 } else { 2014 for (i = 0; i < nfds; i++) { 2015 if (kev[i].flags & EV_ERROR) { 2016 warnc(kev[i].data, "kevent"); 2017 continue; 2018 } 2019 switch (kev[i].filter) { 2020 case EVFILT_READ: 2021 JobDoOutput(kev[i].udata, FALSE); 2022 break; 2023 case EVFILT_PROC: 2024 /* Just wake up and let Job_CatchChildren() collect the 2025 * terminated job. */ 2026 break; 2027 } 2028 } 2029 } 2030#else 2031 readfds = outputs; 2032 timeout.tv_sec = SEL_SEC; 2033 timeout.tv_usec = SEL_USEC; 2034 if (flag && jobFull && fifoFd >= 0) 2035 FD_SET(fifoFd, &readfds); 2036 2037 nfds = select(FD_SETSIZE, &readfds, (fd_set *)NULL, 2038 (fd_set *)NULL, &timeout); 2039 if (nfds <= 0) { 2040 if (interrupted) 2041 JobPassSig(interrupted); 2042 return; 2043 } 2044 if (fifoFd >= 0 && FD_ISSET(fifoFd, &readfds)) { 2045 if (--nfds <= 0) 2046 return; 2047 } 2048 for (ln = Lst_First(&jobs); nfds != 0 && ln != NULL; ln = Lst_Succ(ln)){ 2049 job = Lst_Datum(ln); 2050 if (FD_ISSET(job->inPipe, &readfds)) { 2051 JobDoOutput(job, FALSE); 2052 nfds -= 1; 2053 } 2054 } 2055#endif /* !USE_KQUEUE */ 2056 } 2057} 2058 2059/*- 2060 *----------------------------------------------------------------------- 2061 * Job_Make -- 2062 * Start the creation of a target. Basically a front-end for 2063 * JobStart used by the Make module. 2064 * 2065 * Results: 2066 * None. 2067 * 2068 * Side Effects: 2069 * Another job is started. 2070 * 2071 *----------------------------------------------------------------------- 2072 */ 2073void 2074Job_Make(GNode *gn) 2075{ 2076 2077 JobStart(gn, 0, NULL); 2078} 2079 2080/* 2081 * JobCopyShell: 2082 * 2083 * Make a new copy of the shell structure including a copy of the strings 2084 * in it. This also defaults some fields in case they are NULL. 2085 * 2086 * The function returns a pointer to the new shell structure otherwise. 2087 */ 2088static Shell * 2089JobCopyShell(const Shell *osh) 2090{ 2091 Shell *nsh; 2092 2093 nsh = emalloc(sizeof(*nsh)); 2094 nsh->name = estrdup(osh->name); 2095 2096 if (osh->echoOff != NULL) 2097 nsh->echoOff = estrdup(osh->echoOff); 2098 else 2099 nsh->echoOff = NULL; 2100 if (osh->echoOn != NULL) 2101 nsh->echoOn = estrdup(osh->echoOn); 2102 else 2103 nsh->echoOn = NULL; 2104 nsh->hasEchoCtl = osh->hasEchoCtl; 2105 2106 if (osh->noPrint != NULL) 2107 nsh->noPrint = estrdup(osh->noPrint); 2108 else 2109 nsh->noPrint = NULL; 2110 nsh->noPLen = osh->noPLen; 2111 2112 nsh->hasErrCtl = osh->hasErrCtl; 2113 if (osh->errCheck == NULL) 2114 nsh->errCheck = estrdup(""); 2115 else 2116 nsh->errCheck = estrdup(osh->errCheck); 2117 if (osh->ignErr == NULL) 2118 nsh->ignErr = estrdup("%s"); 2119 else 2120 nsh->ignErr = estrdup(osh->ignErr); 2121 2122 if (osh->echo == NULL) 2123 nsh->echo = estrdup(""); 2124 else 2125 nsh->echo = estrdup(osh->echo); 2126 2127 if (osh->exit == NULL) 2128 nsh->exit = estrdup(""); 2129 else 2130 nsh->exit = estrdup(osh->exit); 2131 2132 return (nsh); 2133} 2134 2135/* 2136 * JobFreeShell: 2137 * 2138 * Free a shell structure and all associated strings. 2139 */ 2140static void 2141JobFreeShell(Shell *sh) 2142{ 2143 2144 if (sh != NULL) { 2145 free(sh->name); 2146 free(sh->echoOff); 2147 free(sh->echoOn); 2148 free(sh->noPrint); 2149 free(sh->errCheck); 2150 free(sh->ignErr); 2151 free(sh->echo); 2152 free(sh->exit); 2153 free(sh); 2154 } 2155} 2156 2157void 2158Shell_Init(void) 2159{ 2160 2161 if (commandShell == NULL) 2162 commandShell = JobMatchShell(shells[DEFSHELL].name); 2163 2164 if (shellPath == NULL) { 2165 /* 2166 * The user didn't specify a shell to use, so we are using the 2167 * default one... Both the absolute path and the last component 2168 * must be set. The last component is taken from the 'name' field 2169 * of the default shell description pointed-to by commandShell. 2170 * All default shells are located in _PATH_DEFSHELLDIR. 2171 */ 2172 shellName = commandShell->name; 2173 shellPath = str_concat(_PATH_DEFSHELLDIR, shellName, STR_ADDSLASH); 2174 } 2175} 2176 2177/*- 2178 *----------------------------------------------------------------------- 2179 * Job_Init -- 2180 * Initialize the process module, given a maximum number of jobs. 2181 * 2182 * Results: 2183 * none 2184 * 2185 * Side Effects: 2186 * lists and counters are initialized 2187 *----------------------------------------------------------------------- 2188 */ 2189void 2190Job_Init(int maxproc) 2191{ 2192 GNode *begin; /* node for commands to do at the very start */ 2193 const char *env; 2194 struct sigaction sa; 2195 2196 fifoFd = -1; 2197 env = getenv("MAKE_JOBS_FIFO"); 2198 2199 if (env == NULL && maxproc > 1) { 2200 /* 2201 * We did not find the environment variable so we are the leader. 2202 * Create the fifo, open it, write one char per allowed job into 2203 * the pipe. 2204 */ 2205 mktemp(fifoName); 2206 if (!mkfifo(fifoName, 0600)) { 2207 fifoFd = open(fifoName, O_RDWR | O_NONBLOCK, 0); 2208 if (fifoFd >= 0) { 2209 fifoMaster = 1; 2210 fcntl(fifoFd, F_SETFL, O_NONBLOCK); 2211 env = fifoName; 2212 setenv("MAKE_JOBS_FIFO", env, 1); 2213 while (maxproc-- > 0) { 2214 write(fifoFd, "+", 1); 2215 } 2216 /* The master make does not get a magic token */ 2217 jobFull = TRUE; 2218 maxJobs = 0; 2219 } else { 2220 unlink(fifoName); 2221 env = NULL; 2222 } 2223 } 2224 } else if (env != NULL) { 2225 /* 2226 * We had the environment variable so we are a slave. 2227 * Open fifo and give ourselves a magic token which represents 2228 * the token our parent make has grabbed to start his make process. 2229 * Otherwise the sub-makes would gobble up tokens and the proper 2230 * number of tokens to specify to -j would depend on the depth of 2231 * the tree and the order of execution. 2232 */ 2233 fifoFd = open(env, O_RDWR, 0); 2234 if (fifoFd >= 0) { 2235 fcntl(fifoFd, F_SETFL, O_NONBLOCK); 2236 maxJobs = 1; 2237 jobFull = FALSE; 2238 } 2239 } 2240 if (fifoFd <= 0) { 2241 maxJobs = maxproc; 2242 jobFull = FALSE; 2243 } else { 2244 } 2245 nJobs = 0; 2246 2247 aborting = 0; 2248 errors = 0; 2249 2250 lastNode = NULL; 2251 2252 if ((maxJobs == 1 && fifoFd < 0) || beVerbose == 0) { 2253 /* 2254 * If only one job can run at a time, there's no need for a banner, 2255 * no is there? 2256 */ 2257 targFmt = ""; 2258 } else { 2259 targFmt = TARG_FMT; 2260 } 2261 2262 Shell_Init(); 2263 2264 /* 2265 * Catch the four signals that POSIX specifies if they aren't ignored. 2266 * JobCatchSignal will just set global variables and hope someone 2267 * else is going to handle the interrupt. 2268 */ 2269 sa.sa_handler = JobCatchSig; 2270 sigemptyset(&sa.sa_mask); 2271 sa.sa_flags = 0; 2272 2273 if (signal(SIGINT, SIG_IGN) != SIG_IGN) { 2274 sigaction(SIGINT, &sa, NULL); 2275 } 2276 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) { 2277 sigaction(SIGHUP, &sa, NULL); 2278 } 2279 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) { 2280 sigaction(SIGQUIT, &sa, NULL); 2281 } 2282 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) { 2283 sigaction(SIGTERM, &sa, NULL); 2284 } 2285 /* 2286 * There are additional signals that need to be caught and passed if 2287 * either the export system wants to be told directly of signals or if 2288 * we're giving each job its own process group (since then it won't get 2289 * signals from the terminal driver as we own the terminal) 2290 */ 2291#if defined(USE_PGRP) 2292 if (signal(SIGTSTP, SIG_IGN) != SIG_IGN) { 2293 sigaction(SIGTSTP, &sa, NULL); 2294 } 2295 if (signal(SIGTTOU, SIG_IGN) != SIG_IGN) { 2296 sigaction(SIGTTOU, &sa, NULL); 2297 } 2298 if (signal(SIGTTIN, SIG_IGN) != SIG_IGN) { 2299 sigaction(SIGTTIN, &sa, NULL); 2300 } 2301 if (signal(SIGWINCH, SIG_IGN) != SIG_IGN) { 2302 sigaction(SIGWINCH, &sa, NULL); 2303 } 2304#endif 2305 2306#ifdef USE_KQUEUE 2307 if ((kqfd = kqueue()) == -1) { 2308 Punt("kqueue: %s", strerror(errno)); 2309 } 2310#endif 2311 2312 begin = Targ_FindNode(".BEGIN", TARG_NOCREATE); 2313 2314 if (begin != NULL) { 2315 JobStart(begin, JOB_SPECIAL, (Job *)NULL); 2316 while (nJobs) { 2317 Job_CatchOutput(0); 2318 Job_CatchChildren(!usePipes); 2319 } 2320 } 2321 postCommands = Targ_FindNode(".END", TARG_CREATE); 2322} 2323 2324/*- 2325 *----------------------------------------------------------------------- 2326 * Job_Full -- 2327 * See if the job table is full. It is considered full if it is OR 2328 * if we are in the process of aborting OR if we have 2329 * reached/exceeded our local quota. This prevents any more jobs 2330 * from starting up. 2331 * 2332 * Results: 2333 * TRUE if the job table is full, FALSE otherwise 2334 * Side Effects: 2335 * None. 2336 *----------------------------------------------------------------------- 2337 */ 2338Boolean 2339Job_Full(void) 2340{ 2341 char c; 2342 int i; 2343 2344 if (aborting) 2345 return (aborting); 2346 if (fifoFd >= 0 && jobFull) { 2347 i = read(fifoFd, &c, 1); 2348 if (i > 0) { 2349 maxJobs++; 2350 jobFull = FALSE; 2351 } 2352 } 2353 return (jobFull); 2354} 2355 2356/*- 2357 *----------------------------------------------------------------------- 2358 * Job_Empty -- 2359 * See if the job table is empty. Because the local concurrency may 2360 * be set to 0, it is possible for the job table to become empty, 2361 * while the list of stoppedJobs remains non-empty. In such a case, 2362 * we want to restart as many jobs as we can. 2363 * 2364 * Results: 2365 * TRUE if it is. FALSE if it ain't. 2366 * 2367 * Side Effects: 2368 * None. 2369 * 2370 * ----------------------------------------------------------------------- 2371 */ 2372Boolean 2373Job_Empty(void) 2374{ 2375 if (nJobs == 0) { 2376 if (!Lst_IsEmpty(&stoppedJobs) && !aborting) { 2377 /* 2378 * The job table is obviously not full if it has no jobs in 2379 * it...Try and restart the stopped jobs. 2380 */ 2381 jobFull = FALSE; 2382 JobRestartJobs(); 2383 return (FALSE); 2384 } else { 2385 return (TRUE); 2386 } 2387 } else { 2388 return (FALSE); 2389 } 2390} 2391 2392/*- 2393 *----------------------------------------------------------------------- 2394 * JobMatchShell -- 2395 * Find a matching shell in 'shells' given its final component. 2396 * 2397 * Results: 2398 * A pointer to a freshly allocated Shell structure with a copy 2399 * of the static structure or NULL if no shell with the given name 2400 * is found. 2401 * 2402 * Side Effects: 2403 * None. 2404 * 2405 *----------------------------------------------------------------------- 2406 */ 2407static Shell * 2408JobMatchShell(const char *name) 2409{ 2410 const struct CShell *sh; /* Pointer into shells table */ 2411 struct Shell *nsh; 2412 2413 for (sh = shells; sh < shells + sizeof(shells) / sizeof(shells[0]); sh++) 2414 if (strcmp(sh->name, name) == 0) 2415 break; 2416 2417 if (sh == shells + sizeof(shells) / sizeof(shells[0])) 2418 return (NULL); 2419 2420 /* make a copy */ 2421 nsh = emalloc(sizeof(*nsh)); 2422 2423 nsh->name = estrdup(sh->name); 2424 nsh->echoOff = estrdup(sh->echoOff); 2425 nsh->echoOn = estrdup(sh->echoOn); 2426 nsh->hasEchoCtl = sh->hasEchoCtl; 2427 nsh->noPrint = estrdup(sh->noPrint); 2428 nsh->noPLen = sh->noPLen; 2429 nsh->hasErrCtl = sh->hasErrCtl; 2430 nsh->errCheck = estrdup(sh->errCheck); 2431 nsh->ignErr = estrdup(sh->ignErr); 2432 nsh->echo = estrdup(sh->echo); 2433 nsh->exit = estrdup(sh->exit); 2434 2435 return (nsh); 2436} 2437 2438/*- 2439 *----------------------------------------------------------------------- 2440 * Job_ParseShell -- 2441 * Parse a shell specification and set up commandShell, shellPath 2442 * and shellName appropriately. 2443 * 2444 * Results: 2445 * FAILURE if the specification was incorrect. 2446 * 2447 * Side Effects: 2448 * commandShell points to a Shell structure (either predefined or 2449 * created from the shell spec), shellPath is the full path of the 2450 * shell described by commandShell, while shellName is just the 2451 * final component of shellPath. 2452 * 2453 * Notes: 2454 * A shell specification consists of a .SHELL target, with dependency 2455 * operator, followed by a series of blank-separated words. Double 2456 * quotes can be used to use blanks in words. A backslash escapes 2457 * anything (most notably a double-quote and a space) and 2458 * provides the functionality it does in C. Each word consists of 2459 * keyword and value separated by an equal sign. There should be no 2460 * unnecessary spaces in the word. The keywords are as follows: 2461 * name Name of shell. 2462 * path Location of shell. Overrides "name" if given 2463 * quiet Command to turn off echoing. 2464 * echo Command to turn echoing on 2465 * filter Result of turning off echoing that shouldn't be 2466 * printed. 2467 * echoFlag Flag to turn echoing on at the start 2468 * errFlag Flag to turn error checking on at the start 2469 * hasErrCtl True if shell has error checking control 2470 * check Command to turn on error checking if hasErrCtl 2471 * is TRUE or template of command to echo a command 2472 * for which error checking is off if hasErrCtl is 2473 * FALSE. 2474 * ignore Command to turn off error checking if hasErrCtl 2475 * is TRUE or template of command to execute a 2476 * command so as to ignore any errors it returns if 2477 * hasErrCtl is FALSE. 2478 * 2479 *----------------------------------------------------------------------- 2480 */ 2481ReturnStatus 2482Job_ParseShell(char *line) 2483{ 2484 char **words; 2485 int wordCount; 2486 char **argv; 2487 int argc; 2488 char *path; 2489 Shell newShell; 2490 Shell *sh; 2491 Boolean fullSpec = FALSE; 2492 2493 while (isspace((unsigned char)*line)) { 2494 line++; 2495 } 2496 words = brk_string(line, &wordCount, TRUE); 2497 2498 memset(&newShell, 0, sizeof(newShell)); 2499 2500 /* 2501 * Parse the specification by keyword 2502 */ 2503 for (path = NULL, argc = wordCount - 1, argv = words + 1; 2504 argc != 0; 2505 argc--, argv++) { 2506 if (strncmp(*argv, "path=", 5) == 0) { 2507 path = &argv[0][5]; 2508 } else if (strncmp(*argv, "name=", 5) == 0) { 2509 newShell.name = &argv[0][5]; 2510 } else { 2511 if (strncmp(*argv, "quiet=", 6) == 0) { 2512 newShell.echoOff = &argv[0][6]; 2513 } else if (strncmp(*argv, "echo=", 5) == 0) { 2514 newShell.echoOn = &argv[0][5]; 2515 } else if (strncmp(*argv, "filter=", 7) == 0) { 2516 newShell.noPrint = &argv[0][7]; 2517 newShell.noPLen = strlen(newShell.noPrint); 2518 } else if (strncmp(*argv, "echoFlag=", 9) == 0) { 2519 newShell.echo = &argv[0][9]; 2520 } else if (strncmp(*argv, "errFlag=", 8) == 0) { 2521 newShell.exit = &argv[0][8]; 2522 } else if (strncmp(*argv, "hasErrCtl=", 10) == 0) { 2523 char c = argv[0][10]; 2524 newShell.hasErrCtl = !((c != 'Y') && (c != 'y') && 2525 (c != 'T') && (c != 't')); 2526 } else if (strncmp(*argv, "check=", 6) == 0) { 2527 newShell.errCheck = &argv[0][6]; 2528 } else if (strncmp(*argv, "ignore=", 7) == 0) { 2529 newShell.ignErr = &argv[0][7]; 2530 } else { 2531 Parse_Error(PARSE_FATAL, "Unknown keyword \"%s\"", 2532 *argv); 2533 return (FAILURE); 2534 } 2535 fullSpec = TRUE; 2536 } 2537 } 2538 2539 /* 2540 * Some checks (could be more) 2541 */ 2542 if (fullSpec) { 2543 if ((newShell.echoOn != NULL) ^ (newShell.echoOff != NULL)) 2544 Parse_Error(PARSE_FATAL, "Shell must have either both echoOff and " 2545 "echoOn or none of them"); 2546 2547 if (newShell.echoOn != NULL && newShell.echoOff) 2548 newShell.hasEchoCtl = TRUE; 2549 } 2550 2551 if (path == NULL) { 2552 /* 2553 * If no path was given, the user wants one of the pre-defined shells, 2554 * yes? So we find the one s/he wants with the help of JobMatchShell 2555 * and set things up the right way. shellPath will be set up by 2556 * Job_Init. 2557 */ 2558 if (newShell.name == NULL) { 2559 Parse_Error(PARSE_FATAL, "Neither path nor name specified"); 2560 return (FAILURE); 2561 } 2562 if ((sh = JobMatchShell(newShell.name)) == NULL) { 2563 Parse_Error(PARSE_FATAL, "%s: no matching shell", newShell.name); 2564 return (FAILURE); 2565 } 2566 2567 } else { 2568 /* 2569 * The user provided a path. If s/he gave nothing else (fullSpec is 2570 * FALSE), try and find a matching shell in the ones we know of. 2571 * Else we just take the specification at its word and copy it 2572 * to a new location. In either case, we need to record the 2573 * path the user gave for the shell. 2574 */ 2575 free(shellPath); 2576 shellPath = estrdup(path); 2577 if (newShell.name == NULL) { 2578 /* get the base name as the name */ 2579 path = strrchr(path, '/'); 2580 if (path == NULL) { 2581 path = shellPath; 2582 } else { 2583 path += 1; 2584 } 2585 newShell.name = path; 2586 } 2587 2588 if (!fullSpec) { 2589 if ((sh = JobMatchShell(newShell.name)) == NULL) { 2590 Parse_Error(PARSE_FATAL, "%s: no matching shell", 2591 newShell.name); 2592 return (FAILURE); 2593 } 2594 } else { 2595 sh = JobCopyShell(&newShell); 2596 } 2597 } 2598 2599 /* set the new shell */ 2600 JobFreeShell(commandShell); 2601 commandShell = sh; 2602 2603 shellName = commandShell->name; 2604 2605 return (SUCCESS); 2606} 2607 2608/*- 2609 *----------------------------------------------------------------------- 2610 * JobInterrupt -- 2611 * Handle the receipt of an interrupt. 2612 * 2613 * Results: 2614 * None 2615 * 2616 * Side Effects: 2617 * All children are killed. Another job will be started if the 2618 * .INTERRUPT target was given. 2619 *----------------------------------------------------------------------- 2620 */ 2621static void 2622JobInterrupt(int runINTERRUPT, int signo) 2623{ 2624 LstNode *ln; /* element in job table */ 2625 Job *job; /* job descriptor in that element */ 2626 GNode *interrupt; /* the node describing the .INTERRUPT target */ 2627 2628 aborting = ABORT_INTERRUPT; 2629 2630 for (ln = Lst_First(&jobs); ln != NULL; ln = Lst_Succ(ln)) { 2631 job = Lst_Datum(ln); 2632 2633 if (!Targ_Precious(job->node)) { 2634 char *file = (job->node->path == NULL ? 2635 job->node->name : 2636 job->node->path); 2637 if (!noExecute && eunlink(file) != -1) { 2638 Error("*** %s removed", file); 2639 } 2640 } 2641 if (job->pid) { 2642 DEBUGF(JOB, ("JobInterrupt passing signal to child %d.\n", 2643 job->pid)); 2644 KILL(job->pid, signo); 2645 } 2646 } 2647 2648 if (runINTERRUPT && !touchFlag) { 2649 /* clear the interrupted flag because we would get an 2650 * infinite loop otherwise */ 2651 interrupted = 0; 2652 2653 interrupt = Targ_FindNode(".INTERRUPT", TARG_NOCREATE); 2654 if (interrupt != NULL) { 2655 ignoreErrors = FALSE; 2656 2657 JobStart(interrupt, JOB_IGNDOTS, (Job *)NULL); 2658 while (nJobs) { 2659 Job_CatchOutput(0); 2660 Job_CatchChildren(!usePipes); 2661 } 2662 } 2663 } 2664} 2665 2666/* 2667 *----------------------------------------------------------------------- 2668 * Job_Finish -- 2669 * Do final processing such as the running of the commands 2670 * attached to the .END target. 2671 * 2672 * Results: 2673 * Number of errors reported. 2674 *----------------------------------------------------------------------- 2675 */ 2676int 2677Job_Finish(void) 2678{ 2679 2680 if (postCommands != NULL && !Lst_IsEmpty(&postCommands->commands)) { 2681 if (errors) { 2682 Error("Errors reported so .END ignored"); 2683 } else { 2684 JobStart(postCommands, JOB_SPECIAL | JOB_IGNDOTS, NULL); 2685 2686 while (nJobs) { 2687 Job_CatchOutput(0); 2688 Job_CatchChildren(!usePipes); 2689 } 2690 } 2691 } 2692 if (fifoFd >= 0) { 2693 close(fifoFd); 2694 fifoFd = -1; 2695 if (fifoMaster) 2696 unlink(fifoName); 2697 } 2698 return (errors); 2699} 2700 2701/*- 2702 *----------------------------------------------------------------------- 2703 * Job_Wait -- 2704 * Waits for all running jobs to finish and returns. Sets 'aborting' 2705 * to ABORT_WAIT to prevent other jobs from starting. 2706 * 2707 * Results: 2708 * None. 2709 * 2710 * Side Effects: 2711 * Currently running jobs finish. 2712 * 2713 *----------------------------------------------------------------------- 2714 */ 2715void 2716Job_Wait(void) 2717{ 2718 2719 aborting = ABORT_WAIT; 2720 while (nJobs != 0) { 2721 Job_CatchOutput(0); 2722 Job_CatchChildren(!usePipes); 2723 } 2724 aborting = 0; 2725} 2726 2727/*- 2728 *----------------------------------------------------------------------- 2729 * Job_AbortAll -- 2730 * Abort all currently running jobs without handling output or anything. 2731 * This function is to be called only in the event of a major 2732 * error. Most definitely NOT to be called from JobInterrupt. 2733 * 2734 * Results: 2735 * None 2736 * 2737 * Side Effects: 2738 * All children are killed, not just the firstborn 2739 *----------------------------------------------------------------------- 2740 */ 2741void 2742Job_AbortAll(void) 2743{ 2744 LstNode *ln; /* element in job table */ 2745 Job *job; /* the job descriptor in that element */ 2746 int foo; 2747 2748 aborting = ABORT_ERROR; 2749 2750 if (nJobs) { 2751 for (ln = Lst_First(&jobs); ln != NULL; ln = Lst_Succ(ln)) { 2752 job = Lst_Datum(ln); 2753 2754 /* 2755 * kill the child process with increasingly drastic signals to make 2756 * darn sure it's dead. 2757 */ 2758 KILL(job->pid, SIGINT); 2759 KILL(job->pid, SIGKILL); 2760 } 2761 } 2762 2763 /* 2764 * Catch as many children as want to report in at first, then give up 2765 */ 2766 while (waitpid((pid_t)-1, &foo, WNOHANG) > 0) 2767 continue; 2768} 2769 2770/*- 2771 *----------------------------------------------------------------------- 2772 * JobRestartJobs -- 2773 * Tries to restart stopped jobs if there are slots available. 2774 * Note that this tries to restart them regardless of pending errors. 2775 * It's not good to leave stopped jobs lying around! 2776 * 2777 * Results: 2778 * None. 2779 * 2780 * Side Effects: 2781 * Resumes(and possibly migrates) jobs. 2782 * 2783 *----------------------------------------------------------------------- 2784 */ 2785static void 2786JobRestartJobs(void) 2787{ 2788 while (!jobFull && !Lst_IsEmpty(&stoppedJobs)) { 2789 DEBUGF(JOB, ("Job queue is not full. Restarting a stopped job.\n")); 2790 JobRestart(Lst_DeQueue(&stoppedJobs)); 2791 } 2792} 2793