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