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