job.c revision 146142
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 146142 2005-05-12 14:33:24Z 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 * compat.c -- 101 * The routines in this file implement the full-compatibility 102 * mode of PMake. Most of the special functionality of PMake 103 * is available in this mode. Things not supported: 104 * - different shells. 105 * - friendly variable substitution. 106 * 107 * Interface: 108 * Compat_Run Initialize things for this module and recreate 109 * thems as need creatin' 110 */ 111 112#include <sys/queue.h> 113#include <sys/types.h> 114#include <sys/select.h> 115#include <sys/stat.h> 116#ifdef USE_KQUEUE 117#include <sys/event.h> 118#endif 119#include <sys/wait.h> 120#include <ctype.h> 121#include <errno.h> 122#include <fcntl.h> 123#include <inttypes.h> 124#include <string.h> 125#include <signal.h> 126#include <stdlib.h> 127#include <unistd.h> 128#include <utime.h> 129 130#include "arch.h" 131#include "buf.h" 132#include "config.h" 133#include "dir.h" 134#include "globals.h" 135#include "GNode.h" 136#include "job.h" 137#include "make.h" 138#include "parse.h" 139#include "pathnames.h" 140#include "str.h" 141#include "suff.h" 142#include "targ.h" 143#include "util.h" 144#include "var.h" 145 146#define TMPPAT "/tmp/makeXXXXXXXXXX" 147 148#ifndef USE_KQUEUE 149/* 150 * The SEL_ constants determine the maximum amount of time spent in select 151 * before coming out to see if a child has finished. SEL_SEC is the number of 152 * seconds and SEL_USEC is the number of micro-seconds 153 */ 154#define SEL_SEC 2 155#define SEL_USEC 0 156#endif /* !USE_KQUEUE */ 157 158/* 159 * Job Table definitions. 160 * 161 * The job "table" is kept as a linked Lst in 'jobs', with the number of 162 * active jobs maintained in the 'nJobs' variable. At no time will this 163 * exceed the value of 'maxJobs', initialized by the Job_Init function. 164 * 165 * When a job is finished, the Make_Update function is called on each of the 166 * parents of the node which was just remade. This takes care of the upward 167 * traversal of the dependency graph. 168 */ 169#define JOB_BUFSIZE 1024 170typedef struct Job { 171 pid_t pid; /* The child's process ID */ 172 173 struct GNode *node; /* The target the child is making */ 174 175 /* 176 * A LstNode for the first command to be saved after the job completes. 177 * This is NULL if there was no "..." in the job's commands. 178 */ 179 LstNode *tailCmds; 180 181 /* 182 * An FILE* for writing out the commands. This is only 183 * used before the job is actually started. 184 */ 185 FILE *cmdFILE; 186 187 /* 188 * A word of flags which determine how the module handles errors, 189 * echoing, etc. for the job 190 */ 191 short flags; /* Flags to control treatment of job */ 192#define JOB_IGNERR 0x001 /* Ignore non-zero exits */ 193#define JOB_SILENT 0x002 /* no output */ 194#define JOB_SPECIAL 0x004 /* Target is a special one. i.e. run it locally 195 * if we can't export it and maxLocal is 0 */ 196#define JOB_IGNDOTS 0x008 /* Ignore "..." lines when processing 197 * commands */ 198#define JOB_FIRST 0x020 /* Job is first job for the node */ 199#define JOB_RESTART 0x080 /* Job needs to be completely restarted */ 200#define JOB_RESUME 0x100 /* Job needs to be resumed b/c it stopped, 201 * for some reason */ 202#define JOB_CONTINUING 0x200 /* We are in the process of resuming this job. 203 * Used to avoid infinite recursion between 204 * JobFinish and JobRestart */ 205 206 /* union for handling shell's output */ 207 union { 208 /* 209 * This part is used when usePipes is true. 210 * The output is being caught via a pipe and the descriptors 211 * of our pipe, an array in which output is line buffered and 212 * the current position in that buffer are all maintained for 213 * each job. 214 */ 215 struct { 216 /* 217 * Input side of pipe associated with 218 * job's output channel 219 */ 220 int op_inPipe; 221 222 /* 223 * Output side of pipe associated with job's 224 * output channel 225 */ 226 int op_outPipe; 227 228 /* 229 * Buffer for storing the output of the 230 * job, line by line 231 */ 232 char op_outBuf[JOB_BUFSIZE + 1]; 233 234 /* Current position in op_outBuf */ 235 int op_curPos; 236 } o_pipe; 237 238 /* 239 * If usePipes is false the output is routed to a temporary 240 * file and all that is kept is the name of the file and the 241 * descriptor open to the file. 242 */ 243 struct { 244 /* Name of file to which shell output was rerouted */ 245 char of_outFile[sizeof(TMPPAT)]; 246 247 /* 248 * Stream open to the output file. Used to funnel all 249 * from a single job to one file while still allowing 250 * multiple shell invocations 251 */ 252 int of_outFd; 253 } o_file; 254 255 } output; /* Data for tracking a shell's output */ 256 257 TAILQ_ENTRY(Job) link; /* list link */ 258} Job; 259 260#define outPipe output.o_pipe.op_outPipe 261#define inPipe output.o_pipe.op_inPipe 262#define outBuf output.o_pipe.op_outBuf 263#define curPos output.o_pipe.op_curPos 264#define outFile output.o_file.of_outFile 265#define outFd output.o_file.of_outFd 266 267TAILQ_HEAD(JobList, Job); 268 269/* 270 * Shell Specifications: 271 * 272 * Some special stuff goes on if a shell doesn't have error control. In such 273 * a case, errCheck becomes a printf template for echoing the command, 274 * should echoing be on and ignErr becomes another printf template for 275 * executing the command while ignoring the return status. If either of these 276 * strings is empty when hasErrCtl is FALSE, the command will be executed 277 * anyway as is and if it causes an error, so be it. 278 */ 279#define DEF_SHELL_STRUCT(TAG, CONST) \ 280struct TAG { \ 281 /* \ 282 * the name of the shell. For Bourne and C shells, this is used \ 283 * only to find the shell description when used as the single \ 284 * source of a .SHELL target. For user-defined shells, this is \ 285 * the full path of the shell. \ 286 */ \ 287 CONST char *name; \ 288 \ 289 /* True if both echoOff and echoOn defined */ \ 290 Boolean hasEchoCtl; \ 291 \ 292 CONST char *echoOff; /* command to turn off echo */ \ 293 CONST char *echoOn; /* command to turn it back on */\ 294 \ 295 /* \ 296 * What the shell prints, and its length, when given the \ 297 * echo-off command. This line will not be printed when \ 298 * received from the shell. This is usually the command which \ 299 * was executed to turn off echoing \ 300 */ \ 301 CONST char *noPrint; \ 302 \ 303 /* set if can control error checking for individual commands */ \ 304 Boolean hasErrCtl; \ 305 \ 306 /* string to turn error checking on */ \ 307 CONST char *errCheck; \ 308 \ 309 /* string to turn off error checking */ \ 310 CONST char *ignErr; \ 311 \ 312 CONST char *echo; /* command line flag: echo commands */ \ 313 CONST char *exit; /* command line flag: exit on error */ \ 314} 315 316DEF_SHELL_STRUCT(Shell,); 317DEF_SHELL_STRUCT(CShell, const); 318 319/* 320 * error handling variables 321 */ 322static int errors = 0; /* number of errors reported */ 323static int aborting = 0; /* why is the make aborting? */ 324#define ABORT_ERROR 1 /* Because of an error */ 325#define ABORT_INTERRUPT 2 /* Because it was interrupted */ 326#define ABORT_WAIT 3 /* Waiting for jobs to finish */ 327 328/* 329 * XXX: Avoid SunOS bug... FILENO() is fp->_file, and file 330 * is a char! So when we go above 127 we turn negative! 331 */ 332#define FILENO(a) ((unsigned)fileno(a)) 333 334/* 335 * post-make command processing. The node postCommands is really just the 336 * .END target but we keep it around to avoid having to search for it 337 * all the time. 338 */ 339static GNode *postCommands; 340 341/* 342 * The number of commands actually printed for a target. Should this 343 * number be 0, no shell will be executed. 344 */ 345static int numCommands; 346 347/* 348 * Return values from JobStart. 349 */ 350#define JOB_RUNNING 0 /* Job is running */ 351#define JOB_ERROR 1 /* Error in starting the job */ 352#define JOB_FINISHED 2 /* The job is already finished */ 353#define JOB_STOPPED 3 /* The job is stopped */ 354 355/* 356 * Descriptions for various shells. 357 */ 358static const struct CShell shells[] = { 359 /* 360 * CSH description. The csh can do echo control by playing 361 * with the setting of the 'echo' shell variable. Sadly, 362 * however, it is unable to do error control nicely. 363 */ 364 { 365 "csh", 366 TRUE, "unset verbose", "set verbose", "unset verbose", 367 FALSE, "echo \"%s\"\n", "csh -c \"%s || exit 0\"", 368 "v", "e", 369 }, 370 /* 371 * SH description. Echo control is also possible and, under 372 * sun UNIX anyway, one can even control error checking. 373 */ 374 { 375 "sh", 376 TRUE, "set -", "set -v", "set -", 377 TRUE, "set -e", "set +e", 378#ifdef OLDBOURNESHELL 379 FALSE, "echo \"%s\"\n", "sh -c '%s || exit 0'\n", 380#endif 381 "v", "e", 382 }, 383 /* 384 * KSH description. The Korn shell has a superset of 385 * the Bourne shell's functionality. 386 */ 387 { 388 "ksh", 389 TRUE, "set -", "set -v", "set -", 390 TRUE, "set -e", "set +e", 391 "v", "e", 392 }, 393}; 394 395/* 396 * This is the shell to which we pass all commands in the Makefile. 397 * It is set by the Job_ParseShell function. 398 */ 399static struct Shell *commandShell = NULL; 400static char *shellPath = NULL; /* full pathname of executable image */ 401static char *shellName = NULL; /* last component of shell */ 402 403/* 404 * The maximum number of jobs that may run. This is initialize from the 405 * -j argument for the leading make and from the FIFO for sub-makes. 406 */ 407static int maxJobs; 408 409static int nJobs; /* The number of children currently running */ 410 411/* The structures that describe them */ 412static struct JobList jobs = TAILQ_HEAD_INITIALIZER(jobs); 413 414static Boolean jobFull; /* Flag to tell when the job table is full. It 415 * is set TRUE when (1) the total number of 416 * running jobs equals the maximum allowed */ 417#ifdef USE_KQUEUE 418static int kqfd; /* File descriptor obtained by kqueue() */ 419#else 420static fd_set outputs; /* Set of descriptors of pipes connected to 421 * the output channels of children */ 422#endif 423 424static GNode *lastNode; /* The node for which output was most recently 425 * produced. */ 426static const char *targFmt; /* Format string to use to head output from a 427 * job when it's not the most-recent job heard 428 * from */ 429 430#define TARG_FMT "--- %s ---\n" /* Default format */ 431#define MESSAGE(fp, gn) \ 432 fprintf(fp, targFmt, gn->name); 433 434/* 435 * When JobStart attempts to run a job but isn't allowed to 436 * or when Job_CatchChildren detects a job that has 437 * been stopped somehow, the job is placed on the stoppedJobs queue to be run 438 * when the next job finishes. 439 * 440 * Lst of Job structures describing jobs that were stopped due to 441 * concurrency limits or externally 442 */ 443static struct JobList stoppedJobs = TAILQ_HEAD_INITIALIZER(stoppedJobs); 444 445static int fifoFd; /* Fd of our job fifo */ 446static char fifoName[] = "/tmp/make_fifo_XXXXXXXXX"; 447static int fifoMaster; 448 449static sig_atomic_t interrupted; 450 451 452#if defined(USE_PGRP) && defined(SYSV) 453# define KILL(pid, sig) killpg(-(pid), (sig)) 454#else 455# if defined(USE_PGRP) 456# define KILL(pid, sig) killpg((pid), (sig)) 457# else 458# define KILL(pid, sig) kill((pid), (sig)) 459# endif 460#endif 461 462/* 463 * Grmpf... There is no way to set bits of the wait structure 464 * anymore with the stupid W*() macros. I liked the union wait 465 * stuff much more. So, we devise our own macros... This is 466 * really ugly, use dramamine sparingly. You have been warned. 467 */ 468#define W_SETMASKED(st, val, fun) \ 469 { \ 470 int sh = (int)~0; \ 471 int mask = fun(sh); \ 472 \ 473 for (sh = 0; ((mask >> sh) & 1) == 0; sh++) \ 474 continue; \ 475 *(st) = (*(st) & ~mask) | ((val) << sh); \ 476 } 477 478#define W_SETTERMSIG(st, val) W_SETMASKED(st, val, WTERMSIG) 479#define W_SETEXITSTATUS(st, val) W_SETMASKED(st, val, WEXITSTATUS) 480 481/** 482 * Information used to create a new process. 483 */ 484typedef struct ProcStuff { 485 int in; /* stdin for new process */ 486 int out; /* stdout for new process */ 487 int err; /* stderr for new process */ 488 489 int merge_errors; /* true if stderr is redirected to stdin */ 490 int pgroup; /* true if new process a process leader */ 491 int searchpath; /* true if binary should be found via $PATH */ 492 493 char **argv; 494 495 pid_t child_pid; 496} ProcStuff; 497 498static void JobRestart(Job *); 499static int JobStart(GNode *, int, Job *); 500static void JobDoOutput(Job *, Boolean); 501static struct Shell *JobMatchShell(const char *); 502static void JobInterrupt(int, int); 503static void JobRestartJobs(void); 504static void ProcExec(const ProcStuff *) __dead2; 505static int Compat_RunCommand(char *, struct GNode *); 506 507/* 508 * The following array is used to make a fast determination of which 509 * commands and characters are interpreted specially by the shell. 510 * If a command is one of these or contains any of these characters, 511 * it is executed by the shell, not directly by us. 512 * XXX Both of these arrays should be configurable via .SHELL 513 */ 514static const char const* sh_builtin[] = { 515 "alias", "cd", "eval", "exec", 516 "exit", "read", "set", "ulimit", 517 "unalias", "umask", "unset", "wait", 518 ":", NULL 519}; 520static const char *sh_meta = "#=|^(){};&<>*?[]:$`\\\n"; 521 522static GNode *curTarg = NULL; 523static GNode *ENDNode; 524 525/** 526 * Replace the current process. 527 */ 528static void 529ProcExec(const ProcStuff *ps) 530{ 531 532 if (ps->in != STDIN_FILENO) { 533 /* 534 * Redirect the child's stdin to the input fd 535 * and reset it to the beginning (again). 536 */ 537 if (dup2(ps->in, STDIN_FILENO) == -1) 538 Punt("Cannot dup2: %s", strerror(errno)); 539 lseek(STDIN_FILENO, (off_t)0, SEEK_SET); 540 } 541 542 if (ps->out != STDOUT_FILENO) { 543 /* 544 * Redirect the child's stdout to the output fd. 545 */ 546 if (dup2(ps->out, STDOUT_FILENO) == -1) 547 Punt("Cannot dup2: %s", strerror(errno)); 548 close(ps->out); 549 } 550 551 if (ps->err != STDERR_FILENO) { 552 /* 553 * Redirect the child's stderr to the err fd. 554 */ 555 if (dup2(ps->err, STDERR_FILENO) == -1) 556 Punt("Cannot dup2: %s", strerror(errno)); 557 close(ps->err); 558 } 559 560 if (ps->merge_errors) { 561 /* 562 * Send stderr to parent process too. 563 */ 564 if (dup2(STDOUT_FILENO, STDERR_FILENO) == -1) 565 Punt("Cannot dup2: %s", strerror(errno)); 566 } 567 568 /* 569 * The file descriptors for stdin, stdout, or stderr might 570 * have been marked close-on-exec. Clear the flag on all 571 * of them. 572 */ 573 fcntl(STDIN_FILENO, F_SETFD, 574 fcntl(STDIN_FILENO, F_GETFD) & (~FD_CLOEXEC)); 575 fcntl(STDOUT_FILENO, F_SETFD, 576 fcntl(STDOUT_FILENO, F_GETFD) & (~FD_CLOEXEC)); 577 fcntl(STDERR_FILENO, F_SETFD, 578 fcntl(STDERR_FILENO, F_GETFD) & (~FD_CLOEXEC)); 579 580 if (ps->pgroup) { 581#ifdef USE_PGRP 582 /* 583 * Become a process group leader, so we can kill it and all 584 * its descendants in one fell swoop, by killing its process 585 * family, but not commit suicide. 586 */ 587#if defined(SYSV) 588 setsid(); 589#else 590 setpgid(0, getpid()); 591#endif 592#endif /* USE_PGRP */ 593 } 594 595 if (ps->searchpath) { 596 execvp(ps->argv[0], ps->argv); 597 598 write(STDERR_FILENO, ps->argv[0], strlen(ps->argv[0])); 599 write(STDERR_FILENO, ":", 1); 600 write(STDERR_FILENO, strerror(errno), strlen(strerror(errno))); 601 write(STDERR_FILENO, "\n", 1); 602 } else { 603 execv(shellPath, ps->argv); 604 605 write(STDERR_FILENO, 606 "Could not execute shell\n", 607 sizeof("Could not execute shell")); 608 } 609 610 /* 611 * Since we are the child process, exit without flushing buffers. 612 */ 613 _exit(1); 614 /* NOTREACHED */ 615} 616 617/** 618 * Wait for child process to terminate. 619 */ 620static int 621ProcWait(ProcStuff *ps) 622{ 623 pid_t pid; 624 int status; 625 626 /* 627 * Wait for the process to exit. 628 */ 629 for (;;) { 630 pid = wait(&status); 631 if (pid == -1 && errno != EINTR) { 632 Fatal("error in wait: %d", pid); 633 /* NOTREACHED */ 634 } 635 if (pid == ps->child_pid) { 636 break; 637 } 638 if (interrupted) { 639 break; 640 } 641 } 642 643 return (status); 644} 645 646/** 647 * JobCatchSignal 648 * Got a signal. Set global variables and hope that someone will 649 * handle it. 650 */ 651static void 652JobCatchSig(int signo) 653{ 654 655 interrupted = signo; 656} 657 658/** 659 * JobPassSig -- 660 * Pass a signal on to all local jobs if 661 * USE_PGRP is defined, then die ourselves. 662 * 663 * Side Effects: 664 * We die by the same signal. 665 */ 666static void 667JobPassSig(int signo) 668{ 669 Job *job; 670 sigset_t nmask, omask; 671 struct sigaction act; 672 673 sigemptyset(&nmask); 674 sigaddset(&nmask, signo); 675 sigprocmask(SIG_SETMASK, &nmask, &omask); 676 677 DEBUGF(JOB, ("JobPassSig(%d) called.\n", signo)); 678 TAILQ_FOREACH(job, &jobs, link) { 679 DEBUGF(JOB, ("JobPassSig passing signal %d to child %jd.\n", 680 signo, (intmax_t)job->pid)); 681 KILL(job->pid, signo); 682 } 683 684 /* 685 * Deal with proper cleanup based on the signal received. We only run 686 * the .INTERRUPT target if the signal was in fact an interrupt. 687 * The other three termination signals are more of a "get out *now*" 688 * command. 689 */ 690 if (signo == SIGINT) { 691 JobInterrupt(TRUE, signo); 692 } else if (signo == SIGHUP || signo == SIGTERM || signo == SIGQUIT) { 693 JobInterrupt(FALSE, signo); 694 } 695 696 /* 697 * Leave gracefully if SIGQUIT, rather than core dumping. 698 */ 699 if (signo == SIGQUIT) { 700 signo = SIGINT; 701 } 702 703 /* 704 * Send ourselves the signal now we've given the message to everyone 705 * else. Note we block everything else possible while we're getting 706 * the signal. This ensures that all our jobs get continued when we 707 * wake up before we take any other signal. 708 * XXX this comment seems wrong. 709 */ 710 act.sa_handler = SIG_DFL; 711 sigemptyset(&act.sa_mask); 712 act.sa_flags = 0; 713 sigaction(signo, &act, NULL); 714 715 DEBUGF(JOB, ("JobPassSig passing signal to self, mask = %x.\n", 716 ~0 & ~(1 << (signo - 1)))); 717 signal(signo, SIG_DFL); 718 719 KILL(getpid(), signo); 720 721 signo = SIGCONT; 722 TAILQ_FOREACH(job, &jobs, link) { 723 DEBUGF(JOB, ("JobPassSig passing signal %d to child %jd.\n", 724 signo, (intmax_t)job->pid)); 725 KILL(job->pid, signo); 726 } 727 728 sigprocmask(SIG_SETMASK, &omask, NULL); 729 sigprocmask(SIG_SETMASK, &omask, NULL); 730 act.sa_handler = JobPassSig; 731 sigaction(signo, &act, NULL); 732} 733 734/** 735 * JobPrintCommand -- 736 * Put out another command for the given job. If the command starts 737 * with an @ or a - we process it specially. In the former case, 738 * so long as the -s and -n flags weren't given to make, we stick 739 * a shell-specific echoOff command in the script. In the latter, 740 * we ignore errors for the entire job, unless the shell has error 741 * control. 742 * If the command is just "..." we take all future commands for this 743 * job to be commands to be executed once the entire graph has been 744 * made and return non-zero to signal that the end of the commands 745 * was reached. These commands are later attached to the postCommands 746 * node and executed by Job_Finish when all things are done. 747 * This function is called from JobStart via LST_FOREACH. 748 * 749 * Results: 750 * Always 0, unless the command was "..." 751 * 752 * Side Effects: 753 * If the command begins with a '-' and the shell has no error control, 754 * the JOB_IGNERR flag is set in the job descriptor. 755 * If the command is "..." and we're not ignoring such things, 756 * tailCmds is set to the successor node of the cmd. 757 * numCommands is incremented if the command is actually printed. 758 */ 759static int 760JobPrintCommand(char *cmd, Job *job) 761{ 762 Boolean noSpecials; /* true if we shouldn't worry about 763 * inserting special commands into 764 * the input stream. */ 765 Boolean shutUp = FALSE; /* true if we put a no echo command 766 * into the command file */ 767 Boolean errOff = FALSE; /* true if we turned error checking 768 * off before printing the command 769 * and need to turn it back on */ 770 const char *cmdTemplate;/* Template to use when printing the command */ 771 char *cmdStart; /* Start of expanded command */ 772 LstNode *cmdNode; /* Node for replacing the command */ 773 774 noSpecials = (noExecute && !(job->node->type & OP_MAKE)); 775 776 if (strcmp(cmd, "...") == 0) { 777 job->node->type |= OP_SAVE_CMDS; 778 if ((job->flags & JOB_IGNDOTS) == 0) { 779 job->tailCmds = 780 Lst_Succ(Lst_Member(&job->node->commands, cmd)); 781 return (1); 782 } 783 return (0); 784 } 785 786#define DBPRINTF(fmt, arg) \ 787 DEBUGF(JOB, (fmt, arg)); \ 788 fprintf(job->cmdFILE, fmt, arg); \ 789 fflush(job->cmdFILE); 790 791 numCommands += 1; 792 793 /* 794 * For debugging, we replace each command with the result of expanding 795 * the variables in the command. 796 */ 797 cmdNode = Lst_Member(&job->node->commands, cmd); 798 799 cmd = Buf_Peel(Var_Subst(cmd, job->node, FALSE)); 800 cmdStart = cmd; 801 802 Lst_Replace(cmdNode, cmdStart); 803 804 cmdTemplate = "%s\n"; 805 806 /* 807 * Check for leading @', -' or +'s to control echoing, error checking, 808 * and execution on -n. 809 */ 810 while (*cmd == '@' || *cmd == '-' || *cmd == '+') { 811 switch (*cmd) { 812 813 case '@': 814 shutUp = DEBUG(LOUD) ? FALSE : TRUE; 815 break; 816 817 case '-': 818 errOff = TRUE; 819 break; 820 821 case '+': 822 if (noSpecials) { 823 /* 824 * We're not actually exececuting anything... 825 * but this one needs to be - use compat mode 826 * just for it. 827 */ 828 Compat_RunCommand(cmd, job->node); 829 return (0); 830 } 831 break; 832 } 833 cmd++; 834 } 835 836 while (isspace((unsigned char)*cmd)) 837 cmd++; 838 839 if (shutUp) { 840 if (!(job->flags & JOB_SILENT) && !noSpecials && 841 commandShell->hasEchoCtl) { 842 DBPRINTF("%s\n", commandShell->echoOff); 843 } else { 844 shutUp = FALSE; 845 } 846 } 847 848 if (errOff) { 849 if (!(job->flags & JOB_IGNERR) && !noSpecials) { 850 if (commandShell->hasErrCtl) { 851 /* 852 * We don't want the error-control commands 853 * showing up either, so we turn off echoing 854 * while executing them. We could put another 855 * field in the shell structure to tell 856 * JobDoOutput to look for this string too, 857 * but why make it any more complex than 858 * it already is? 859 */ 860 if (!(job->flags & JOB_SILENT) && !shutUp && 861 commandShell->hasEchoCtl) { 862 DBPRINTF("%s\n", commandShell->echoOff); 863 DBPRINTF("%s\n", commandShell->ignErr); 864 DBPRINTF("%s\n", commandShell->echoOn); 865 } else { 866 DBPRINTF("%s\n", commandShell->ignErr); 867 } 868 } else if (commandShell->ignErr && 869 *commandShell->ignErr != '\0') { 870 /* 871 * The shell has no error control, so we need to 872 * be weird to get it to ignore any errors from 873 * the command. If echoing is turned on, we turn 874 * it off and use the errCheck template to echo 875 * the command. Leave echoing off so the user 876 * doesn't see the weirdness we go through to 877 * ignore errors. Set cmdTemplate to use the 878 * weirdness instead of the simple "%s\n" 879 * template. 880 */ 881 if (!(job->flags & JOB_SILENT) && !shutUp && 882 commandShell->hasEchoCtl) { 883 DBPRINTF("%s\n", commandShell->echoOff); 884 DBPRINTF(commandShell->errCheck, cmd); 885 shutUp = TRUE; 886 } 887 cmdTemplate = commandShell->ignErr; 888 /* 889 * The error ignoration (hee hee) is already 890 * taken care of by the ignErr template, so 891 * pretend error checking is still on. 892 */ 893 errOff = FALSE; 894 } else { 895 errOff = FALSE; 896 } 897 } else { 898 errOff = FALSE; 899 } 900 } 901 902 DBPRINTF(cmdTemplate, cmd); 903 904 if (errOff) { 905 /* 906 * If echoing is already off, there's no point in issuing the 907 * echoOff command. Otherwise we issue it and pretend it was on 908 * for the whole command... 909 */ 910 if (!shutUp && !(job->flags & JOB_SILENT) && 911 commandShell->hasEchoCtl) { 912 DBPRINTF("%s\n", commandShell->echoOff); 913 shutUp = TRUE; 914 } 915 DBPRINTF("%s\n", commandShell->errCheck); 916 } 917 if (shutUp) { 918 DBPRINTF("%s\n", commandShell->echoOn); 919 } 920 return (0); 921} 922 923/** 924 * JobClose -- 925 * Called to close both input and output pipes when a job is finished. 926 * 927 * Side Effects: 928 * The file descriptors associated with the job are closed. 929 */ 930static void 931JobClose(Job *job) 932{ 933 934 if (usePipes) { 935#if !defined(USE_KQUEUE) 936 FD_CLR(job->inPipe, &outputs); 937#endif 938 if (job->outPipe != job->inPipe) { 939 close(job->outPipe); 940 } 941 JobDoOutput(job, TRUE); 942 close(job->inPipe); 943 } else { 944 close(job->outFd); 945 JobDoOutput(job, TRUE); 946 } 947} 948 949/** 950 * JobFinish -- 951 * Do final processing for the given job including updating 952 * parents and starting new jobs as available/necessary. Note 953 * that we pay no attention to the JOB_IGNERR flag here. 954 * This is because when we're called because of a noexecute flag 955 * or something, jstat.w_status is 0 and when called from 956 * Job_CatchChildren, the status is zeroed if it s/b ignored. 957 * 958 * Side Effects: 959 * Some nodes may be put on the toBeMade queue. 960 * Final commands for the job are placed on postCommands. 961 * 962 * If we got an error and are aborting (aborting == ABORT_ERROR) and 963 * the job list is now empty, we are done for the day. 964 * If we recognized an error (errors !=0), we set the aborting flag 965 * to ABORT_ERROR so no more jobs will be started. 966 */ 967static void 968JobFinish(Job *job, int *status) 969{ 970 Boolean done; 971 LstNode *ln; 972 973 if (WIFEXITED(*status)) { 974 int job_status = WEXITSTATUS(*status); 975 976 JobClose(job); 977 /* 978 * Deal with ignored errors in -B mode. We need to 979 * print a message telling of the ignored error as 980 * well as setting status.w_status to 0 so the next 981 * command gets run. To do this, we set done to be 982 * TRUE if in -B mode and the job exited non-zero. 983 */ 984 if (job_status == 0) { 985 done = FALSE; 986 } else { 987 if (job->flags & JOB_IGNERR) { 988 done = TRUE; 989 } else { 990 /* 991 * If it exited non-zero and either we're 992 * doing things our way or we're not ignoring 993 * errors, the job is finished. Similarly, if 994 * the shell died because of a signal the job 995 * is also finished. In these cases, finish 996 * out the job's output before printing the 997 * exit status... 998 */ 999 done = TRUE; 1000 if (job->cmdFILE != NULL && 1001 job->cmdFILE != stdout) { 1002 fclose(job->cmdFILE); 1003 } 1004 1005 } 1006 } 1007 } else if (WIFSIGNALED(*status)) { 1008 if (WTERMSIG(*status) == SIGCONT) { 1009 /* 1010 * No need to close things down or anything. 1011 */ 1012 done = FALSE; 1013 } else { 1014 /* 1015 * If it exited non-zero and either we're 1016 * doing things our way or we're not ignoring 1017 * errors, the job is finished. Similarly, if 1018 * the shell died because of a signal the job 1019 * is also finished. In these cases, finish 1020 * out the job's output before printing the 1021 * exit status... 1022 */ 1023 JobClose(job); 1024 if (job->cmdFILE != NULL && 1025 job->cmdFILE != stdout) { 1026 fclose(job->cmdFILE); 1027 } 1028 done = TRUE; 1029 } 1030 } else { 1031 /* 1032 * No need to close things down or anything. 1033 */ 1034 done = FALSE; 1035 } 1036 1037 if (WIFEXITED(*status)) { 1038 if (done || DEBUG(JOB)) { 1039 FILE *out; 1040 1041 if (compatMake && 1042 !usePipes && 1043 (job->flags & JOB_IGNERR)) { 1044 /* 1045 * If output is going to a file and this job 1046 * is ignoring errors, arrange to have the 1047 * exit status sent to the output file as 1048 * well. 1049 */ 1050 out = fdopen(job->outFd, "w"); 1051 if (out == NULL) 1052 Punt("Cannot fdopen"); 1053 } else { 1054 out = stdout; 1055 } 1056 1057 DEBUGF(JOB, ("Process %jd exited.\n", 1058 (intmax_t)job->pid)); 1059 1060 if (WEXITSTATUS(*status) == 0) { 1061 if (DEBUG(JOB)) { 1062 if (usePipes && job->node != lastNode) { 1063 MESSAGE(out, job->node); 1064 lastNode = job->node; 1065 } 1066 fprintf(out, 1067 "*** Completed successfully\n"); 1068 } 1069 } else { 1070 if (usePipes && job->node != lastNode) { 1071 MESSAGE(out, job->node); 1072 lastNode = job->node; 1073 } 1074 fprintf(out, "*** Error code %d%s\n", 1075 WEXITSTATUS(*status), 1076 (job->flags & JOB_IGNERR) ? 1077 "(ignored)" : ""); 1078 1079 if (job->flags & JOB_IGNERR) { 1080 *status = 0; 1081 } 1082 } 1083 1084 fflush(out); 1085 } 1086 } else if (WIFSIGNALED(*status)) { 1087 if (done || DEBUG(JOB) || (WTERMSIG(*status) == SIGCONT)) { 1088 FILE *out; 1089 1090 if (compatMake && 1091 !usePipes && 1092 (job->flags & JOB_IGNERR)) { 1093 /* 1094 * If output is going to a file and this job 1095 * is ignoring errors, arrange to have the 1096 * exit status sent to the output file as 1097 * well. 1098 */ 1099 out = fdopen(job->outFd, "w"); 1100 if (out == NULL) 1101 Punt("Cannot fdopen"); 1102 } else { 1103 out = stdout; 1104 } 1105 1106 if (WTERMSIG(*status) == SIGCONT) { 1107 /* 1108 * If the beastie has continued, shift the 1109 * Job from the stopped list to the running 1110 * one (or re-stop it if concurrency is 1111 * exceeded) and go and get another child. 1112 */ 1113 if (job->flags & (JOB_RESUME | JOB_RESTART)) { 1114 if (usePipes && job->node != lastNode) { 1115 MESSAGE(out, job->node); 1116 lastNode = job->node; 1117 } 1118 fprintf(out, "*** Continued\n"); 1119 } 1120 if (!(job->flags & JOB_CONTINUING)) { 1121 DEBUGF(JOB, ("Warning: process %jd was not " 1122 "continuing.\n", (intmax_t) job->pid)); 1123#ifdef notdef 1124 /* 1125 * We don't really want to restart a 1126 * job from scratch just because it 1127 * continued, especially not without 1128 * killing the continuing process! 1129 * That's why this is ifdef'ed out. 1130 * FD - 9/17/90 1131 */ 1132 JobRestart(job); 1133#endif 1134 } 1135 job->flags &= ~JOB_CONTINUING; 1136 TAILQ_INSERT_TAIL(&jobs, job, link); 1137 nJobs += 1; 1138 DEBUGF(JOB, ("Process %jd is continuing locally.\n", 1139 (intmax_t) job->pid)); 1140 if (nJobs == maxJobs) { 1141 jobFull = TRUE; 1142 DEBUGF(JOB, ("Job queue is full.\n")); 1143 } 1144 fflush(out); 1145 return; 1146 1147 } else { 1148 if (usePipes && job->node != lastNode) { 1149 MESSAGE(out, job->node); 1150 lastNode = job->node; 1151 } 1152 fprintf(out, 1153 "*** Signal %d\n", WTERMSIG(*status)); 1154 fflush(out); 1155 } 1156 } 1157 } else { 1158 /* STOPPED */ 1159 FILE *out; 1160 1161 if (compatMake && !usePipes && (job->flags & JOB_IGNERR)) { 1162 /* 1163 * If output is going to a file and this job 1164 * is ignoring errors, arrange to have the 1165 * exit status sent to the output file as 1166 * well. 1167 */ 1168 out = fdopen(job->outFd, "w"); 1169 if (out == NULL) 1170 Punt("Cannot fdopen"); 1171 } else { 1172 out = stdout; 1173 } 1174 1175 DEBUGF(JOB, ("Process %jd stopped.\n", (intmax_t) job->pid)); 1176 if (usePipes && job->node != lastNode) { 1177 MESSAGE(out, job->node); 1178 lastNode = job->node; 1179 } 1180 fprintf(out, "*** Stopped -- signal %d\n", WSTOPSIG(*status)); 1181 job->flags |= JOB_RESUME; 1182 TAILQ_INSERT_TAIL(&stoppedJobs, job, link); 1183 fflush(out); 1184 return; 1185 } 1186 1187 /* 1188 * Now handle the -B-mode stuff. If the beast still isn't finished, 1189 * try and restart the job on the next command. If JobStart says it's 1190 * ok, it's ok. If there's an error, this puppy is done. 1191 */ 1192 if (compatMake && WIFEXITED(*status) && 1193 Lst_Succ(job->node->compat_command) != NULL) { 1194 switch (JobStart(job->node, job->flags & JOB_IGNDOTS, job)) { 1195 case JOB_RUNNING: 1196 done = FALSE; 1197 break; 1198 case JOB_ERROR: 1199 done = TRUE; 1200 W_SETEXITSTATUS(status, 1); 1201 break; 1202 case JOB_FINISHED: 1203 /* 1204 * If we got back a JOB_FINISHED code, JobStart has 1205 * already called Make_Update and freed the job 1206 * descriptor. We set done to false here to avoid fake 1207 * cycles and double frees. JobStart needs to do the 1208 * update so we can proceed up the graph when given 1209 * the -n flag.. 1210 */ 1211 done = FALSE; 1212 break; 1213 default: 1214 break; 1215 } 1216 } else { 1217 done = TRUE; 1218 } 1219 1220 if (done && aborting != ABORT_ERROR && 1221 aborting != ABORT_INTERRUPT && *status == 0) { 1222 /* 1223 * As long as we aren't aborting and the job didn't return a 1224 * non-zero status that we shouldn't ignore, we call 1225 * Make_Update to update the parents. In addition, any saved 1226 * commands for the node are placed on the .END target. 1227 */ 1228 for (ln = job->tailCmds; ln != NULL; ln = LST_NEXT(ln)) { 1229 Lst_AtEnd(&postCommands->commands, 1230 Buf_Peel( 1231 Var_Subst(Lst_Datum(ln), job->node, FALSE))); 1232 } 1233 1234 job->node->made = MADE; 1235 Make_Update(job->node); 1236 free(job); 1237 1238 } else if (*status != 0) { 1239 errors += 1; 1240 free(job); 1241 } 1242 1243 JobRestartJobs(); 1244 1245 /* 1246 * Set aborting if any error. 1247 */ 1248 if (errors && !keepgoing && aborting != ABORT_INTERRUPT) { 1249 /* 1250 * If we found any errors in this batch of children and the -k 1251 * flag wasn't given, we set the aborting flag so no more jobs 1252 * get started. 1253 */ 1254 aborting = ABORT_ERROR; 1255 } 1256 1257 if (aborting == ABORT_ERROR && Job_Empty()) { 1258 /* 1259 * If we are aborting and the job table is now empty, we finish. 1260 */ 1261 Finish(errors); 1262 } 1263} 1264 1265/** 1266 * Job_Touch 1267 * Touch the given target. Called by JobStart when the -t flag was 1268 * given. Prints messages unless told to be silent. 1269 * 1270 * Side Effects: 1271 * The data modification of the file is changed. In addition, if the 1272 * file did not exist, it is created. 1273 */ 1274void 1275Job_Touch(GNode *gn, Boolean silent) 1276{ 1277 int streamID; /* ID of stream opened to do the touch */ 1278 struct utimbuf times; /* Times for utime() call */ 1279 1280 if (gn->type & (OP_JOIN | OP_USE | OP_EXEC | OP_OPTIONAL)) { 1281 /* 1282 * .JOIN, .USE, .ZEROTIME and .OPTIONAL targets are "virtual" 1283 * targets and, as such, shouldn't really be created. 1284 */ 1285 return; 1286 } 1287 1288 if (!silent) { 1289 fprintf(stdout, "touch %s\n", gn->name); 1290 fflush(stdout); 1291 } 1292 1293 if (noExecute) { 1294 return; 1295 } 1296 1297 if (gn->type & OP_ARCHV) { 1298 Arch_Touch(gn); 1299 } else if (gn->type & OP_LIB) { 1300 Arch_TouchLib(gn); 1301 } else { 1302 char *file = gn->path ? gn->path : gn->name; 1303 1304 times.actime = times.modtime = now; 1305 if (utime(file, ×) < 0) { 1306 streamID = open(file, O_RDWR | O_CREAT, 0666); 1307 1308 if (streamID >= 0) { 1309 char c; 1310 1311 /* 1312 * Read and write a byte to the file to change 1313 * the modification time, then close the file. 1314 */ 1315 if (read(streamID, &c, 1) == 1) { 1316 lseek(streamID, (off_t)0, SEEK_SET); 1317 write(streamID, &c, 1); 1318 } 1319 1320 close(streamID); 1321 } else { 1322 fprintf(stdout, "*** couldn't touch %s: %s", 1323 file, strerror(errno)); 1324 fflush(stdout); 1325 } 1326 } 1327 } 1328} 1329 1330/** 1331 * Job_CheckCommands 1332 * Make sure the given node has all the commands it needs. 1333 * 1334 * Results: 1335 * TRUE if the commands list is/was ok. 1336 * 1337 * Side Effects: 1338 * The node will have commands from the .DEFAULT rule added to it 1339 * if it needs them. 1340 */ 1341Boolean 1342Job_CheckCommands(GNode *gn, void (*abortProc)(const char *, ...)) 1343{ 1344 1345 if (OP_NOP(gn->type) && Lst_IsEmpty(&gn->commands) && 1346 (gn->type & OP_LIB) == 0) { 1347 /* 1348 * No commands. Look for .DEFAULT rule from which we might infer 1349 * commands. 1350 */ 1351 if (DEFAULT != NULL && !Lst_IsEmpty(&DEFAULT->commands)) { 1352 char *p1; 1353 /* 1354 * Make only looks for a .DEFAULT if the node was 1355 * never the target of an operator, so that's what we 1356 * do too. If a .DEFAULT was given, we substitute its 1357 * commands for gn's commands and set the IMPSRC 1358 * variable to be the target's name The DEFAULT node 1359 * acts like a transformation rule, in that gn also 1360 * inherits any attributes or sources attached to 1361 * .DEFAULT itself. 1362 */ 1363 Make_HandleUse(DEFAULT, gn); 1364 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), gn); 1365 free(p1); 1366 1367 } else if (Dir_MTime(gn) == 0) { 1368 /* 1369 * The node wasn't the target of an operator we have 1370 * no .DEFAULT rule to go on and the target doesn't 1371 * already exist. There's nothing more we can do for 1372 * this branch. If the -k flag wasn't given, we stop 1373 * in our tracks, otherwise we just don't update 1374 * this node's parents so they never get examined. 1375 */ 1376 static const char msg[] = 1377 "make: don't know how to make"; 1378 1379 if (gn->type & OP_OPTIONAL) { 1380 fprintf(stdout, "%s %s(ignored)\n", 1381 msg, gn->name); 1382 fflush(stdout); 1383 } else if (keepgoing) { 1384 fprintf(stdout, "%s %s(continuing)\n", 1385 msg, gn->name); 1386 fflush(stdout); 1387 return (FALSE); 1388 } else { 1389#if OLD_JOKE 1390 if (strcmp(gn->name,"love") == 0) 1391 (*abortProc)("Not war."); 1392 else 1393#endif 1394 (*abortProc)("%s %s. Stop", 1395 msg, gn->name); 1396 return (FALSE); 1397 } 1398 } 1399 } 1400 return (TRUE); 1401} 1402 1403/** 1404 * JobExec 1405 * Execute the shell for the given job. Called from JobStart and 1406 * JobRestart. 1407 * 1408 * Side Effects: 1409 * A shell is executed, outputs is altered and the Job structure added 1410 * to the job table. 1411 */ 1412static void 1413JobExec(Job *job, char **argv) 1414{ 1415 ProcStuff ps; 1416 1417 if (DEBUG(JOB)) { 1418 int i; 1419 1420 DEBUGF(JOB, ("Running %s\n", job->node->name)); 1421 DEBUGF(JOB, ("\tCommand: ")); 1422 for (i = 0; argv[i] != NULL; i++) { 1423 DEBUGF(JOB, ("%s ", argv[i])); 1424 } 1425 DEBUGF(JOB, ("\n")); 1426 } 1427 1428 /* 1429 * Some jobs produce no output and it's disconcerting to have 1430 * no feedback of their running (since they produce no output, the 1431 * banner with their name in it never appears). This is an attempt to 1432 * provide that feedback, even if nothing follows it. 1433 */ 1434 if (lastNode != job->node && (job->flags & JOB_FIRST) && 1435 !(job->flags & JOB_SILENT)) { 1436 MESSAGE(stdout, job->node); 1437 lastNode = job->node; 1438 } 1439 1440 ps.in = FILENO(job->cmdFILE); 1441 if (usePipes) { 1442 /* 1443 * Set up the child's output to be routed through the 1444 * pipe we've created for it. 1445 */ 1446 ps.out = job->outPipe; 1447 } else { 1448 /* 1449 * We're capturing output in a file, so we duplicate 1450 * the descriptor to the temporary file into the 1451 * standard output. 1452 */ 1453 ps.out = job->outFd; 1454 } 1455 ps.err = STDERR_FILENO; 1456 1457 ps.merge_errors = 1; 1458 ps.pgroup = 1; 1459 ps.searchpath = 0; 1460 1461 ps.argv = argv; 1462 1463 /* 1464 * Fork. Warning since we are doing vfork() instead of fork(), 1465 * do not allocate memory in the child process! 1466 */ 1467 if ((ps.child_pid = vfork()) == -1) { 1468 Punt("Cannot fork"); 1469 1470 1471 } else if (ps.child_pid == 0) { 1472 /* 1473 * Child 1474 */ 1475 if (fifoFd >= 0) 1476 close(fifoFd); 1477 1478 ProcExec(&ps); 1479 /* NOTREACHED */ 1480 } 1481 1482 /* 1483 * Parent 1484 */ 1485 job->pid = ps.child_pid; 1486 1487 if (usePipes && (job->flags & JOB_FIRST)) { 1488 /* 1489 * The first time a job is run for a node, we set the 1490 * current position in the buffer to the beginning and 1491 * mark another stream to watch in the outputs mask. 1492 */ 1493#ifdef USE_KQUEUE 1494 struct kevent kev[2]; 1495#endif 1496 job->curPos = 0; 1497 1498#if defined(USE_KQUEUE) 1499 EV_SET(&kev[0], job->inPipe, EVFILT_READ, EV_ADD, 0, 0, job); 1500 EV_SET(&kev[1], job->pid, EVFILT_PROC, 1501 EV_ADD | EV_ONESHOT, NOTE_EXIT, 0, NULL); 1502 if (kevent(kqfd, kev, 2, NULL, 0, NULL) != 0) { 1503 /* 1504 * kevent() will fail if the job is already 1505 * finished 1506 */ 1507 if (errno != EINTR && errno != EBADF && errno != ESRCH) 1508 Punt("kevent: %s", strerror(errno)); 1509 } 1510#else 1511 FD_SET(job->inPipe, &outputs); 1512#endif /* USE_KQUEUE */ 1513 } 1514 1515 if (job->cmdFILE != NULL && job->cmdFILE != stdout) { 1516 fclose(job->cmdFILE); 1517 job->cmdFILE = NULL; 1518 } 1519 1520 /* 1521 * Now the job is actually running, add it to the table. 1522 */ 1523 nJobs += 1; 1524 TAILQ_INSERT_TAIL(&jobs, job, link); 1525 if (nJobs == maxJobs) { 1526 jobFull = TRUE; 1527 } 1528} 1529 1530/** 1531 * JobMakeArgv 1532 * Create the argv needed to execute the shell for a given job. 1533 */ 1534static void 1535JobMakeArgv(Job *job, char **argv) 1536{ 1537 int argc; 1538 static char args[10]; /* For merged arguments */ 1539 1540 argv[0] = shellName; 1541 argc = 1; 1542 1543 if ((commandShell->exit && *commandShell->exit != '-') || 1544 (commandShell->echo && *commandShell->echo != '-')) { 1545 /* 1546 * At least one of the flags doesn't have a minus before it, so 1547 * merge them together. Have to do this because the *(&(@*#*&#$# 1548 * Bourne shell thinks its second argument is a file to source. 1549 * Grrrr. Note the ten-character limitation on the combined 1550 * arguments. 1551 */ 1552 sprintf(args, "-%s%s", (job->flags & JOB_IGNERR) ? "" : 1553 commandShell->exit ? commandShell->exit : "", 1554 (job->flags & JOB_SILENT) ? "" : 1555 commandShell->echo ? commandShell->echo : ""); 1556 1557 if (args[1]) { 1558 argv[argc] = args; 1559 argc++; 1560 } 1561 } else { 1562 if (!(job->flags & JOB_IGNERR) && commandShell->exit) { 1563 argv[argc] = commandShell->exit; 1564 argc++; 1565 } 1566 if (!(job->flags & JOB_SILENT) && commandShell->echo) { 1567 argv[argc] = commandShell->echo; 1568 argc++; 1569 } 1570 } 1571 argv[argc] = NULL; 1572} 1573 1574/** 1575 * JobRestart 1576 * Restart a job that stopped for some reason. The job must be neither 1577 * on the jobs nor on the stoppedJobs list. 1578 * 1579 * Side Effects: 1580 * jobFull will be set if the job couldn't be run. 1581 */ 1582static void 1583JobRestart(Job *job) 1584{ 1585 1586 if (job->flags & JOB_RESTART) { 1587 /* 1588 * Set up the control arguments to the shell. This is based on 1589 * the flags set earlier for this job. If the JOB_IGNERR flag 1590 * is clear, the 'exit' flag of the commandShell is used to 1591 * cause it to exit upon receiving an error. If the JOB_SILENT 1592 * flag is clear, the 'echo' flag of the commandShell is used 1593 * to get it to start echoing as soon as it starts 1594 * processing commands. 1595 */ 1596 char *argv[4]; 1597 1598 JobMakeArgv(job, argv); 1599 1600 DEBUGF(JOB, ("Restarting %s...", job->node->name)); 1601 if (nJobs >= maxJobs && !(job->flags & JOB_SPECIAL)) { 1602 /* 1603 * Not allowed to run -- put it back on the hold 1604 * queue and mark the table full 1605 */ 1606 DEBUGF(JOB, ("holding\n")); 1607 TAILQ_INSERT_HEAD(&stoppedJobs, job, link); 1608 jobFull = TRUE; 1609 DEBUGF(JOB, ("Job queue is full.\n")); 1610 return; 1611 } else { 1612 /* 1613 * Job may be run locally. 1614 */ 1615 DEBUGF(JOB, ("running locally\n")); 1616 } 1617 JobExec(job, argv); 1618 1619 } else { 1620 /* 1621 * The job has stopped and needs to be restarted. 1622 * Why it stopped, we don't know... 1623 */ 1624 DEBUGF(JOB, ("Resuming %s...", job->node->name)); 1625 if ((nJobs < maxJobs || ((job->flags & JOB_SPECIAL) && 1626 maxJobs == 0)) && nJobs != maxJobs) { 1627 /* 1628 * If we haven't reached the concurrency limit already 1629 * (or the job must be run and maxJobs is 0), it's ok 1630 * to resume it. 1631 */ 1632 Boolean error; 1633 int status; 1634 1635 error = (KILL(job->pid, SIGCONT) != 0); 1636 1637 if (!error) { 1638 /* 1639 * Make sure the user knows we've continued 1640 * the beast and actually put the thing in the 1641 * job table. 1642 */ 1643 job->flags |= JOB_CONTINUING; 1644 status = 0; 1645 W_SETTERMSIG(&status, SIGCONT); 1646 JobFinish(job, &status); 1647 1648 job->flags &= ~(JOB_RESUME|JOB_CONTINUING); 1649 DEBUGF(JOB, ("done\n")); 1650 } else { 1651 Error("couldn't resume %s: %s", 1652 job->node->name, strerror(errno)); 1653 status = 0; 1654 W_SETEXITSTATUS(&status, 1); 1655 JobFinish(job, &status); 1656 } 1657 } else { 1658 /* 1659 * Job cannot be restarted. Mark the table as full and 1660 * place the job back on the list of stopped jobs. 1661 */ 1662 DEBUGF(JOB, ("table full\n")); 1663 TAILQ_INSERT_HEAD(&stoppedJobs, job, link); 1664 jobFull = TRUE; 1665 DEBUGF(JOB, ("Job queue is full.\n")); 1666 } 1667 } 1668} 1669 1670/** 1671 * JobStart 1672 * Start a target-creation process going for the target described 1673 * by the graph node gn. 1674 * 1675 * Results: 1676 * JOB_ERROR if there was an error in the commands, JOB_FINISHED 1677 * if there isn't actually anything left to do for the job and 1678 * JOB_RUNNING if the job has been started. 1679 * 1680 * Side Effects: 1681 * A new Job node is created and added to the list of running 1682 * jobs. PMake is forked and a child shell created. 1683 */ 1684static int 1685JobStart(GNode *gn, int flags, Job *previous) 1686{ 1687 Job *job; /* new job descriptor */ 1688 char *argv[4]; /* Argument vector to shell */ 1689 Boolean cmdsOK; /* true if the nodes commands were all right */ 1690 Boolean noExec; /* Set true if we decide not to run the job */ 1691 int tfd; /* File descriptor for temp file */ 1692 LstNode *ln; 1693 char tfile[sizeof(TMPPAT)]; 1694 1695 if (interrupted) { 1696 JobPassSig(interrupted); 1697 return (JOB_ERROR); 1698 } 1699 if (previous != NULL) { 1700 previous->flags &= ~(JOB_FIRST | JOB_IGNERR | JOB_SILENT); 1701 job = previous; 1702 } else { 1703 job = emalloc(sizeof(Job)); 1704 flags |= JOB_FIRST; 1705 } 1706 1707 job->node = gn; 1708 job->tailCmds = NULL; 1709 1710 /* 1711 * Set the initial value of the flags for this job based on the global 1712 * ones and the node's attributes... Any flags supplied by the caller 1713 * are also added to the field. 1714 */ 1715 job->flags = 0; 1716 if (Targ_Ignore(gn)) { 1717 job->flags |= JOB_IGNERR; 1718 } 1719 if (Targ_Silent(gn)) { 1720 job->flags |= JOB_SILENT; 1721 } 1722 job->flags |= flags; 1723 1724 /* 1725 * Check the commands now so any attributes from .DEFAULT have a chance 1726 * to migrate to the node. 1727 */ 1728 if (!compatMake && (job->flags & JOB_FIRST)) { 1729 cmdsOK = Job_CheckCommands(gn, Error); 1730 } else { 1731 cmdsOK = TRUE; 1732 } 1733 1734 /* 1735 * If the -n flag wasn't given, we open up OUR (not the child's) 1736 * temporary file to stuff commands in it. The thing is rd/wr so we 1737 * don't need to reopen it to feed it to the shell. If the -n flag 1738 * *was* given, we just set the file to be stdout. Cute, huh? 1739 */ 1740 if ((gn->type & OP_MAKE) || (!noExecute && !touchFlag)) { 1741 /* 1742 * We're serious here, but if the commands were bogus, we're 1743 * also dead... 1744 */ 1745 if (!cmdsOK) { 1746 DieHorribly(); 1747 } 1748 1749 strcpy(tfile, TMPPAT); 1750 if ((tfd = mkstemp(tfile)) == -1) 1751 Punt("Cannot create temp file: %s", strerror(errno)); 1752 job->cmdFILE = fdopen(tfd, "w+"); 1753 eunlink(tfile); 1754 if (job->cmdFILE == NULL) { 1755 close(tfd); 1756 Punt("Could not open %s", tfile); 1757 } 1758 fcntl(FILENO(job->cmdFILE), F_SETFD, 1); 1759 /* 1760 * Send the commands to the command file, flush all its 1761 * buffers then rewind and remove the thing. 1762 */ 1763 noExec = FALSE; 1764 1765 /* 1766 * Used to be backwards; replace when start doing multiple 1767 * commands per shell. 1768 */ 1769 if (compatMake) { 1770 /* 1771 * Be compatible: If this is the first time for this 1772 * node, verify its commands are ok and open the 1773 * commands list for sequential access by later 1774 * invocations of JobStart. Once that is done, we take 1775 * the next command off the list and print it to the 1776 * command file. If the command was an ellipsis, note 1777 * that there's nothing more to execute. 1778 */ 1779 if (job->flags & JOB_FIRST) 1780 gn->compat_command = Lst_First(&gn->commands); 1781 else 1782 gn->compat_command = 1783 Lst_Succ(gn->compat_command); 1784 1785 if (gn->compat_command == NULL || 1786 JobPrintCommand(Lst_Datum(gn->compat_command), job)) 1787 noExec = TRUE; 1788 1789 if (noExec && !(job->flags & JOB_FIRST)) { 1790 /* 1791 * If we're not going to execute anything, the 1792 * job is done and we need to close down the 1793 * various file descriptors we've opened for 1794 * output, then call JobDoOutput to catch the 1795 * final characters or send the file to the 1796 * screen... Note that the i/o streams are only 1797 * open if this isn't the first job. Note also 1798 * that this could not be done in 1799 * Job_CatchChildren b/c it wasn't clear if 1800 * there were more commands to execute or not... 1801 */ 1802 JobClose(job); 1803 } 1804 } else { 1805 /* 1806 * We can do all the commands at once. hooray for sanity 1807 */ 1808 numCommands = 0; 1809 LST_FOREACH(ln, &gn->commands) { 1810 if (JobPrintCommand(Lst_Datum(ln), job)) 1811 break; 1812 } 1813 1814 /* 1815 * If we didn't print out any commands to the shell 1816 * script, there's not much point in executing the 1817 * shell, is there? 1818 */ 1819 if (numCommands == 0) { 1820 noExec = TRUE; 1821 } 1822 } 1823 1824 } else if (noExecute) { 1825 /* 1826 * Not executing anything -- just print all the commands to 1827 * stdout in one fell swoop. This will still set up 1828 * job->tailCmds correctly. 1829 */ 1830 if (lastNode != gn) { 1831 MESSAGE(stdout, gn); 1832 lastNode = gn; 1833 } 1834 job->cmdFILE = stdout; 1835 1836 /* 1837 * Only print the commands if they're ok, but don't die if 1838 * they're not -- just let the user know they're bad and keep 1839 * going. It doesn't do any harm in this case and may do 1840 * some good. 1841 */ 1842 if (cmdsOK) { 1843 LST_FOREACH(ln, &gn->commands) { 1844 if (JobPrintCommand(Lst_Datum(ln), job)) 1845 break; 1846 } 1847 } 1848 /* 1849 * Don't execute the shell, thank you. 1850 */ 1851 noExec = TRUE; 1852 1853 } else { 1854 /* 1855 * Just touch the target and note that no shell should be 1856 * executed. Set cmdFILE to stdout to make life easier. Check 1857 * the commands, too, but don't die if they're no good -- it 1858 * does no harm to keep working up the graph. 1859 */ 1860 job->cmdFILE = stdout; 1861 Job_Touch(gn, job->flags & JOB_SILENT); 1862 noExec = TRUE; 1863 } 1864 1865 /* 1866 * If we're not supposed to execute a shell, don't. 1867 */ 1868 if (noExec) { 1869 /* 1870 * Unlink and close the command file if we opened one 1871 */ 1872 if (job->cmdFILE != stdout) { 1873 if (job->cmdFILE != NULL) 1874 fclose(job->cmdFILE); 1875 } else { 1876 fflush(stdout); 1877 } 1878 1879 /* 1880 * We only want to work our way up the graph if we aren't here 1881 * because the commands for the job were no good. 1882 */ 1883 if (cmdsOK) { 1884 if (aborting == 0) { 1885 for (ln = job->tailCmds; ln != NULL; 1886 ln = LST_NEXT(ln)) { 1887 Lst_AtEnd(&postCommands->commands, 1888 Buf_Peel(Var_Subst(Lst_Datum(ln), 1889 job->node, FALSE))); 1890 } 1891 job->node->made = MADE; 1892 Make_Update(job->node); 1893 } 1894 free(job); 1895 return(JOB_FINISHED); 1896 } else { 1897 free(job); 1898 return(JOB_ERROR); 1899 } 1900 } else { 1901 fflush(job->cmdFILE); 1902 } 1903 1904 /* 1905 * Set up the control arguments to the shell. This is based on the flags 1906 * set earlier for this job. 1907 */ 1908 JobMakeArgv(job, argv); 1909 1910 /* 1911 * If we're using pipes to catch output, create the pipe by which we'll 1912 * get the shell's output. If we're using files, print out that we're 1913 * starting a job and then set up its temporary-file name. 1914 */ 1915 if (!compatMake || (job->flags & JOB_FIRST)) { 1916 if (usePipes) { 1917 int fd[2]; 1918 1919 if (pipe(fd) == -1) 1920 Punt("Cannot create pipe: %s", strerror(errno)); 1921 job->inPipe = fd[0]; 1922 job->outPipe = fd[1]; 1923 fcntl(job->inPipe, F_SETFD, 1); 1924 fcntl(job->outPipe, F_SETFD, 1); 1925 } else { 1926 fprintf(stdout, "Remaking `%s'\n", gn->name); 1927 fflush(stdout); 1928 strcpy(job->outFile, TMPPAT); 1929 if ((job->outFd = mkstemp(job->outFile)) == -1) 1930 Punt("cannot create temp file: %s", 1931 strerror(errno)); 1932 fcntl(job->outFd, F_SETFD, 1); 1933 } 1934 } 1935 1936 if (nJobs >= maxJobs && !(job->flags & JOB_SPECIAL) && maxJobs != 0) { 1937 /* 1938 * We've hit the limit of concurrency, so put the job on hold 1939 * until some other job finishes. Note that the special jobs 1940 * (.BEGIN, .INTERRUPT and .END) may be run even when the 1941 * limit has been reached (e.g. when maxJobs == 0). 1942 */ 1943 jobFull = TRUE; 1944 1945 DEBUGF(JOB, ("Can only run job locally.\n")); 1946 job->flags |= JOB_RESTART; 1947 TAILQ_INSERT_TAIL(&stoppedJobs, job, link); 1948 } else { 1949 if (nJobs >= maxJobs) { 1950 /* 1951 * If we're running this job as a special case 1952 * (see above), at least say the table is full. 1953 */ 1954 jobFull = TRUE; 1955 DEBUGF(JOB, ("Local job queue is full.\n")); 1956 } 1957 JobExec(job, argv); 1958 } 1959 return (JOB_RUNNING); 1960} 1961 1962static char * 1963JobOutput(Job *job, char *cp, char *endp, int msg) 1964{ 1965 char *ecp; 1966 1967 if (commandShell->noPrint) { 1968 ecp = strstr(cp, commandShell->noPrint); 1969 while (ecp != NULL) { 1970 if (cp != ecp) { 1971 *ecp = '\0'; 1972 if (msg && job->node != lastNode) { 1973 MESSAGE(stdout, job->node); 1974 lastNode = job->node; 1975 } 1976 /* 1977 * The only way there wouldn't be a newline 1978 * after this line is if it were the last in 1979 * the buffer. However, since the non-printable 1980 * comes after it, there must be a newline, so 1981 * we don't print one. 1982 */ 1983 fprintf(stdout, "%s", cp); 1984 fflush(stdout); 1985 } 1986 cp = ecp + strlen(commandShell->noPrint); 1987 if (cp != endp) { 1988 /* 1989 * Still more to print, look again after 1990 * skipping the whitespace following the 1991 * non-printable command.... 1992 */ 1993 cp++; 1994 while (*cp == ' ' || *cp == '\t' || 1995 *cp == '\n') { 1996 cp++; 1997 } 1998 ecp = strstr(cp, commandShell->noPrint); 1999 } else { 2000 return (cp); 2001 } 2002 } 2003 } 2004 return (cp); 2005} 2006 2007/** 2008 * JobDoOutput 2009 * This function is called at different times depending on 2010 * whether the user has specified that output is to be collected 2011 * via pipes or temporary files. In the former case, we are called 2012 * whenever there is something to read on the pipe. We collect more 2013 * output from the given job and store it in the job's outBuf. If 2014 * this makes up a line, we print it tagged by the job's identifier, 2015 * as necessary. 2016 * If output has been collected in a temporary file, we open the 2017 * file and read it line by line, transfering it to our own 2018 * output channel until the file is empty. At which point we 2019 * remove the temporary file. 2020 * In both cases, however, we keep our figurative eye out for the 2021 * 'noPrint' line for the shell from which the output came. If 2022 * we recognize a line, we don't print it. If the command is not 2023 * alone on the line (the character after it is not \0 or \n), we 2024 * do print whatever follows it. 2025 * 2026 * Side Effects: 2027 * curPos may be shifted as may the contents of outBuf. 2028 */ 2029static void 2030JobDoOutput(Job *job, Boolean finish) 2031{ 2032 Boolean gotNL = FALSE; /* true if got a newline */ 2033 Boolean fbuf; /* true if our buffer filled up */ 2034 int nr; /* number of bytes read */ 2035 int i; /* auxiliary index into outBuf */ 2036 int max; /* limit for i (end of current data) */ 2037 int nRead; /* (Temporary) number of bytes read */ 2038 FILE *oFILE; /* Stream pointer to shell's output file */ 2039 char inLine[132]; 2040 2041 if (usePipes) { 2042 /* 2043 * Read as many bytes as will fit in the buffer. 2044 */ 2045 end_loop: 2046 gotNL = FALSE; 2047 fbuf = FALSE; 2048 2049 nRead = read(job->inPipe, &job->outBuf[job->curPos], 2050 JOB_BUFSIZE - job->curPos); 2051 /* 2052 * Check for interrupt here too, because the above read may 2053 * block when the child process is stopped. In this case the 2054 * interrupt will unblock it (we don't use SA_RESTART). 2055 */ 2056 if (interrupted) 2057 JobPassSig(interrupted); 2058 2059 if (nRead < 0) { 2060 DEBUGF(JOB, ("JobDoOutput(piperead)")); 2061 nr = 0; 2062 } else { 2063 nr = nRead; 2064 } 2065 2066 /* 2067 * If we hit the end-of-file (the job is dead), we must flush 2068 * its remaining output, so pretend we read a newline if 2069 * there's any output remaining in the buffer. 2070 * Also clear the 'finish' flag so we stop looping. 2071 */ 2072 if (nr == 0 && job->curPos != 0) { 2073 job->outBuf[job->curPos] = '\n'; 2074 nr = 1; 2075 finish = FALSE; 2076 } else if (nr == 0) { 2077 finish = FALSE; 2078 } 2079 2080 /* 2081 * Look for the last newline in the bytes we just got. If there 2082 * is one, break out of the loop with 'i' as its index and 2083 * gotNL set TRUE. 2084 */ 2085 max = job->curPos + nr; 2086 for (i = job->curPos + nr - 1; i >= job->curPos; i--) { 2087 if (job->outBuf[i] == '\n') { 2088 gotNL = TRUE; 2089 break; 2090 } else if (job->outBuf[i] == '\0') { 2091 /* 2092 * Why? 2093 */ 2094 job->outBuf[i] = ' '; 2095 } 2096 } 2097 2098 if (!gotNL) { 2099 job->curPos += nr; 2100 if (job->curPos == JOB_BUFSIZE) { 2101 /* 2102 * If we've run out of buffer space, we have 2103 * no choice but to print the stuff. sigh. 2104 */ 2105 fbuf = TRUE; 2106 i = job->curPos; 2107 } 2108 } 2109 if (gotNL || fbuf) { 2110 /* 2111 * Need to send the output to the screen. Null terminate 2112 * it first, overwriting the newline character if there 2113 * was one. So long as the line isn't one we should 2114 * filter (according to the shell description), we print 2115 * the line, preceded by a target banner if this target 2116 * isn't the same as the one for which we last printed 2117 * something. The rest of the data in the buffer are 2118 * then shifted down to the start of the buffer and 2119 * curPos is set accordingly. 2120 */ 2121 job->outBuf[i] = '\0'; 2122 if (i >= job->curPos) { 2123 char *cp; 2124 2125 cp = JobOutput(job, job->outBuf, 2126 &job->outBuf[i], FALSE); 2127 2128 /* 2129 * There's still more in that buffer. This time, 2130 * though, we know there's no newline at the 2131 * end, so we add one of our own free will. 2132 */ 2133 if (*cp != '\0') { 2134 if (job->node != lastNode) { 2135 MESSAGE(stdout, job->node); 2136 lastNode = job->node; 2137 } 2138 fprintf(stdout, "%s%s", cp, 2139 gotNL ? "\n" : ""); 2140 fflush(stdout); 2141 } 2142 } 2143 if (i < max - 1) { 2144 /* shift the remaining characters down */ 2145 memcpy(job->outBuf, &job->outBuf[i + 1], 2146 max - (i + 1)); 2147 job->curPos = max - (i + 1); 2148 2149 } else { 2150 /* 2151 * We have written everything out, so we just 2152 * start over from the start of the buffer. 2153 * No copying. No nothing. 2154 */ 2155 job->curPos = 0; 2156 } 2157 } 2158 if (finish) { 2159 /* 2160 * If the finish flag is true, we must loop until we hit 2161 * end-of-file on the pipe. This is guaranteed to happen 2162 * eventually since the other end of the pipe is now 2163 * closed (we closed it explicitly and the child has 2164 * exited). When we do get an EOF, finish will be set 2165 * FALSE and we'll fall through and out. 2166 */ 2167 goto end_loop; 2168 } 2169 2170 } else { 2171 /* 2172 * We've been called to retrieve the output of the job from the 2173 * temporary file where it's been squirreled away. This consists 2174 * of opening the file, reading the output line by line, being 2175 * sure not to print the noPrint line for the shell we used, 2176 * then close and remove the temporary file. Very simple. 2177 * 2178 * Change to read in blocks and do FindSubString type things 2179 * as for pipes? That would allow for "@echo -n..." 2180 */ 2181 oFILE = fopen(job->outFile, "r"); 2182 if (oFILE != NULL) { 2183 fprintf(stdout, "Results of making %s:\n", 2184 job->node->name); 2185 fflush(stdout); 2186 2187 while (fgets(inLine, sizeof(inLine), oFILE) != NULL) { 2188 char *cp, *endp, *oendp; 2189 2190 cp = inLine; 2191 oendp = endp = inLine + strlen(inLine); 2192 if (endp[-1] == '\n') { 2193 *--endp = '\0'; 2194 } 2195 cp = JobOutput(job, inLine, endp, FALSE); 2196 2197 /* 2198 * There's still more in that buffer. This time, 2199 * though, we know there's no newline at the 2200 * end, so we add one of our own free will. 2201 */ 2202 fprintf(stdout, "%s", cp); 2203 fflush(stdout); 2204 if (endp != oendp) { 2205 fprintf(stdout, "\n"); 2206 fflush(stdout); 2207 } 2208 } 2209 fclose(oFILE); 2210 eunlink(job->outFile); 2211 } 2212 } 2213} 2214 2215/** 2216 * Job_CatchChildren 2217 * Handle the exit of a child. Called from Make_Make. 2218 * 2219 * Side Effects: 2220 * The job descriptor is removed from the list of children. 2221 * 2222 * Notes: 2223 * We do waits, blocking or not, according to the wisdom of our 2224 * caller, until there are no more children to report. For each 2225 * job, call JobFinish to finish things off. This will take care of 2226 * putting jobs on the stoppedJobs queue. 2227 */ 2228void 2229Job_CatchChildren(Boolean block) 2230{ 2231 pid_t pid; /* pid of dead child */ 2232 Job *job; /* job descriptor for dead child */ 2233 int status; /* Exit/termination status */ 2234 2235 /* 2236 * Don't even bother if we know there's no one around. 2237 */ 2238 if (nJobs == 0) { 2239 return; 2240 } 2241 2242 for (;;) { 2243 pid = waitpid((pid_t)-1, &status, 2244 (block ? 0 : WNOHANG) | WUNTRACED); 2245 if (pid <= 0) 2246 break; 2247 2248 DEBUGF(JOB, ("Process %jd exited or stopped.\n", 2249 (intmax_t)pid)); 2250 2251 TAILQ_FOREACH(job, &jobs, link) { 2252 if (job->pid == pid) 2253 break; 2254 } 2255 2256 if (job == NULL) { 2257 if (WIFSIGNALED(status) && 2258 (WTERMSIG(status) == SIGCONT)) { 2259 TAILQ_FOREACH(job, &jobs, link) { 2260 if (job->pid == pid) 2261 break; 2262 } 2263 if (job == NULL) { 2264 Error("Resumed child (%jd) " 2265 "not in table", (intmax_t)pid); 2266 continue; 2267 } 2268 TAILQ_REMOVE(&stoppedJobs, job, link); 2269 } else { 2270 Error("Child (%jd) not in table?", 2271 (intmax_t)pid); 2272 continue; 2273 } 2274 } else { 2275 TAILQ_REMOVE(&jobs, job, link); 2276 nJobs -= 1; 2277 if (fifoFd >= 0 && maxJobs > 1) { 2278 write(fifoFd, "+", 1); 2279 maxJobs--; 2280 if (nJobs >= maxJobs) 2281 jobFull = TRUE; 2282 else 2283 jobFull = FALSE; 2284 } else { 2285 DEBUGF(JOB, ("Job queue is no longer full.\n")); 2286 jobFull = FALSE; 2287 } 2288 } 2289 2290 JobFinish(job, &status); 2291 } 2292 if (interrupted) 2293 JobPassSig(interrupted); 2294} 2295 2296/** 2297 * Job_CatchOutput 2298 * Catch the output from our children, if we're using 2299 * pipes do so. Otherwise just block time until we get a 2300 * signal(most likely a SIGCHLD) since there's no point in 2301 * just spinning when there's nothing to do and the reaping 2302 * of a child can wait for a while. 2303 * 2304 * Side Effects: 2305 * Output is read from pipes if we're piping. 2306 * ----------------------------------------------------------------------- 2307 */ 2308void 2309#ifdef USE_KQUEUE 2310Job_CatchOutput(int flag __unused) 2311#else 2312Job_CatchOutput(int flag) 2313#endif 2314{ 2315 int nfds; 2316#ifdef USE_KQUEUE 2317#define KEV_SIZE 4 2318 struct kevent kev[KEV_SIZE]; 2319 int i; 2320#else 2321 struct timeval timeout; 2322 fd_set readfds; 2323 Job *job; 2324#endif 2325 2326 fflush(stdout); 2327 2328 if (usePipes) { 2329#ifdef USE_KQUEUE 2330 if ((nfds = kevent(kqfd, NULL, 0, kev, KEV_SIZE, NULL)) == -1) { 2331 if (errno != EINTR) 2332 Punt("kevent: %s", strerror(errno)); 2333 if (interrupted) 2334 JobPassSig(interrupted); 2335 } else { 2336 for (i = 0; i < nfds; i++) { 2337 if (kev[i].flags & EV_ERROR) { 2338 warnc(kev[i].data, "kevent"); 2339 continue; 2340 } 2341 switch (kev[i].filter) { 2342 case EVFILT_READ: 2343 JobDoOutput(kev[i].udata, FALSE); 2344 break; 2345 case EVFILT_PROC: 2346 /* 2347 * Just wake up and let 2348 * Job_CatchChildren() collect the 2349 * terminated job. 2350 */ 2351 break; 2352 } 2353 } 2354 } 2355#else 2356 readfds = outputs; 2357 timeout.tv_sec = SEL_SEC; 2358 timeout.tv_usec = SEL_USEC; 2359 if (flag && jobFull && fifoFd >= 0) 2360 FD_SET(fifoFd, &readfds); 2361 2362 nfds = select(FD_SETSIZE, &readfds, (fd_set *)NULL, 2363 (fd_set *)NULL, &timeout); 2364 if (nfds <= 0) { 2365 if (interrupted) 2366 JobPassSig(interrupted); 2367 return; 2368 } 2369 if (fifoFd >= 0 && FD_ISSET(fifoFd, &readfds)) { 2370 if (--nfds <= 0) 2371 return; 2372 } 2373 job = TAILQ_FIRST(&jobs); 2374 while (nfds != 0 && job != NULL) { 2375 if (FD_ISSET(job->inPipe, &readfds)) { 2376 JobDoOutput(job, FALSE); 2377 nfds--; 2378 } 2379 job = TAILQ_NEXT(job, link); 2380 } 2381#endif /* !USE_KQUEUE */ 2382 } 2383} 2384 2385/** 2386 * Job_Make 2387 * Start the creation of a target. Basically a front-end for 2388 * JobStart used by the Make module. 2389 * 2390 * Side Effects: 2391 * Another job is started. 2392 */ 2393void 2394Job_Make(GNode *gn) 2395{ 2396 2397 JobStart(gn, 0, NULL); 2398} 2399 2400/** 2401 * JobCopyShell 2402 * Make a new copy of the shell structure including a copy of the strings 2403 * in it. This also defaults some fields in case they are NULL. 2404 * 2405 * Returns: 2406 * The function returns a pointer to the new shell structure. 2407 */ 2408static struct Shell * 2409JobCopyShell(const struct Shell *osh) 2410{ 2411 struct Shell *nsh; 2412 2413 nsh = emalloc(sizeof(*nsh)); 2414 nsh->name = estrdup(osh->name); 2415 2416 if (osh->echoOff != NULL) 2417 nsh->echoOff = estrdup(osh->echoOff); 2418 else 2419 nsh->echoOff = NULL; 2420 if (osh->echoOn != NULL) 2421 nsh->echoOn = estrdup(osh->echoOn); 2422 else 2423 nsh->echoOn = NULL; 2424 nsh->hasEchoCtl = osh->hasEchoCtl; 2425 2426 if (osh->noPrint != NULL) 2427 nsh->noPrint = estrdup(osh->noPrint); 2428 else 2429 nsh->noPrint = NULL; 2430 2431 nsh->hasErrCtl = osh->hasErrCtl; 2432 if (osh->errCheck == NULL) 2433 nsh->errCheck = estrdup(""); 2434 else 2435 nsh->errCheck = estrdup(osh->errCheck); 2436 if (osh->ignErr == NULL) 2437 nsh->ignErr = estrdup("%s"); 2438 else 2439 nsh->ignErr = estrdup(osh->ignErr); 2440 2441 if (osh->echo == NULL) 2442 nsh->echo = estrdup(""); 2443 else 2444 nsh->echo = estrdup(osh->echo); 2445 2446 if (osh->exit == NULL) 2447 nsh->exit = estrdup(""); 2448 else 2449 nsh->exit = estrdup(osh->exit); 2450 2451 return (nsh); 2452} 2453 2454/** 2455 * JobFreeShell 2456 * Free a shell structure and all associated strings. 2457 */ 2458static void 2459JobFreeShell(struct Shell *sh) 2460{ 2461 2462 if (sh != NULL) { 2463 free(sh->name); 2464 free(sh->echoOff); 2465 free(sh->echoOn); 2466 free(sh->noPrint); 2467 free(sh->errCheck); 2468 free(sh->ignErr); 2469 free(sh->echo); 2470 free(sh->exit); 2471 free(sh); 2472 } 2473} 2474 2475void 2476Shell_Init(void) 2477{ 2478 2479 if (commandShell == NULL) 2480 commandShell = JobMatchShell(shells[DEFSHELL].name); 2481 2482 if (shellPath == NULL) { 2483 /* 2484 * The user didn't specify a shell to use, so we are using the 2485 * default one... Both the absolute path and the last component 2486 * must be set. The last component is taken from the 'name' 2487 * field of the default shell description pointed-to by 2488 * commandShell. All default shells are located in 2489 * PATH_DEFSHELLDIR. 2490 */ 2491 shellName = commandShell->name; 2492 shellPath = str_concat(PATH_DEFSHELLDIR, shellName, 2493 STR_ADDSLASH); 2494 } 2495} 2496 2497/** 2498 * Job_Init 2499 * Initialize the process module, given a maximum number of jobs. 2500 * 2501 * Side Effects: 2502 * lists and counters are initialized 2503 */ 2504void 2505Job_Init(int maxproc) 2506{ 2507 GNode *begin; /* node for commands to do at the very start */ 2508 const char *env; 2509 struct sigaction sa; 2510 2511 fifoFd = -1; 2512 env = getenv("MAKE_JOBS_FIFO"); 2513 2514 if (env == NULL && maxproc > 1) { 2515 /* 2516 * We did not find the environment variable so we are the 2517 * leader. Create the fifo, open it, write one char per 2518 * allowed job into the pipe. 2519 */ 2520 mktemp(fifoName); 2521 if (!mkfifo(fifoName, 0600)) { 2522 fifoFd = open(fifoName, O_RDWR | O_NONBLOCK, 0); 2523 if (fifoFd >= 0) { 2524 fifoMaster = 1; 2525 fcntl(fifoFd, F_SETFL, O_NONBLOCK); 2526 env = fifoName; 2527 setenv("MAKE_JOBS_FIFO", env, 1); 2528 while (maxproc-- > 0) { 2529 write(fifoFd, "+", 1); 2530 } 2531 /* The master make does not get a magic token */ 2532 jobFull = TRUE; 2533 maxJobs = 0; 2534 } else { 2535 unlink(fifoName); 2536 env = NULL; 2537 } 2538 } 2539 2540 } else if (env != NULL) { 2541 /* 2542 * We had the environment variable so we are a slave. 2543 * Open fifo and give ourselves a magic token which represents 2544 * the token our parent make has grabbed to start his make 2545 * process. Otherwise the sub-makes would gobble up tokens and 2546 * the proper number of tokens to specify to -j would depend 2547 * on the depth of the tree and the order of execution. 2548 */ 2549 fifoFd = open(env, O_RDWR, 0); 2550 if (fifoFd >= 0) { 2551 fcntl(fifoFd, F_SETFL, O_NONBLOCK); 2552 maxJobs = 1; 2553 jobFull = FALSE; 2554 } 2555 } 2556 if (fifoFd <= 0) { 2557 maxJobs = maxproc; 2558 jobFull = FALSE; 2559 } else { 2560 } 2561 nJobs = 0; 2562 2563 aborting = 0; 2564 errors = 0; 2565 2566 lastNode = NULL; 2567 2568 if ((maxJobs == 1 && fifoFd < 0) || beVerbose == 0) { 2569 /* 2570 * If only one job can run at a time, there's no need for a 2571 * banner, no is there? 2572 */ 2573 targFmt = ""; 2574 } else { 2575 targFmt = TARG_FMT; 2576 } 2577 2578 Shell_Init(); 2579 2580 /* 2581 * Catch the four signals that POSIX specifies if they aren't ignored. 2582 * JobCatchSignal will just set global variables and hope someone 2583 * else is going to handle the interrupt. 2584 */ 2585 sa.sa_handler = JobCatchSig; 2586 sigemptyset(&sa.sa_mask); 2587 sa.sa_flags = 0; 2588 2589 if (signal(SIGINT, SIG_IGN) != SIG_IGN) { 2590 sigaction(SIGINT, &sa, NULL); 2591 } 2592 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) { 2593 sigaction(SIGHUP, &sa, NULL); 2594 } 2595 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) { 2596 sigaction(SIGQUIT, &sa, NULL); 2597 } 2598 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) { 2599 sigaction(SIGTERM, &sa, NULL); 2600 } 2601 /* 2602 * There are additional signals that need to be caught and passed if 2603 * either the export system wants to be told directly of signals or if 2604 * we're giving each job its own process group (since then it won't get 2605 * signals from the terminal driver as we own the terminal) 2606 */ 2607#if defined(USE_PGRP) 2608 if (signal(SIGTSTP, SIG_IGN) != SIG_IGN) { 2609 sigaction(SIGTSTP, &sa, NULL); 2610 } 2611 if (signal(SIGTTOU, SIG_IGN) != SIG_IGN) { 2612 sigaction(SIGTTOU, &sa, NULL); 2613 } 2614 if (signal(SIGTTIN, SIG_IGN) != SIG_IGN) { 2615 sigaction(SIGTTIN, &sa, NULL); 2616 } 2617 if (signal(SIGWINCH, SIG_IGN) != SIG_IGN) { 2618 sigaction(SIGWINCH, &sa, NULL); 2619 } 2620#endif 2621 2622#ifdef USE_KQUEUE 2623 if ((kqfd = kqueue()) == -1) { 2624 Punt("kqueue: %s", strerror(errno)); 2625 } 2626#endif 2627 2628 begin = Targ_FindNode(".BEGIN", TARG_NOCREATE); 2629 2630 if (begin != NULL) { 2631 JobStart(begin, JOB_SPECIAL, (Job *)NULL); 2632 while (nJobs) { 2633 Job_CatchOutput(0); 2634 Job_CatchChildren(!usePipes); 2635 } 2636 } 2637 postCommands = Targ_FindNode(".END", TARG_CREATE); 2638} 2639 2640/** 2641 * Job_Full 2642 * See if the job table is full. It is considered full if it is OR 2643 * if we are in the process of aborting OR if we have 2644 * reached/exceeded our local quota. This prevents any more jobs 2645 * from starting up. 2646 * 2647 * Results: 2648 * TRUE if the job table is full, FALSE otherwise 2649 */ 2650Boolean 2651Job_Full(void) 2652{ 2653 char c; 2654 int i; 2655 2656 if (aborting) 2657 return (aborting); 2658 if (fifoFd >= 0 && jobFull) { 2659 i = read(fifoFd, &c, 1); 2660 if (i > 0) { 2661 maxJobs++; 2662 jobFull = FALSE; 2663 } 2664 } 2665 return (jobFull); 2666} 2667 2668/** 2669 * Job_Empty 2670 * See if the job table is empty. Because the local concurrency may 2671 * be set to 0, it is possible for the job table to become empty, 2672 * while the list of stoppedJobs remains non-empty. In such a case, 2673 * we want to restart as many jobs as we can. 2674 * 2675 * Results: 2676 * TRUE if it is. FALSE if it ain't. 2677 */ 2678Boolean 2679Job_Empty(void) 2680{ 2681 if (nJobs == 0) { 2682 if (!TAILQ_EMPTY(&stoppedJobs) && !aborting) { 2683 /* 2684 * The job table is obviously not full if it has no 2685 * jobs in it...Try and restart the stopped jobs. 2686 */ 2687 jobFull = FALSE; 2688 JobRestartJobs(); 2689 return (FALSE); 2690 } else { 2691 return (TRUE); 2692 } 2693 } else { 2694 return (FALSE); 2695 } 2696} 2697 2698/** 2699 * JobMatchShell 2700 * Find a matching shell in 'shells' given its final component. 2701 * 2702 * Results: 2703 * A pointer to a freshly allocated Shell structure with a copy 2704 * of the static structure or NULL if no shell with the given name 2705 * is found. 2706 */ 2707static struct Shell * 2708JobMatchShell(const char *name) 2709{ 2710 const struct CShell *sh; /* Pointer into shells table */ 2711 struct Shell *nsh; 2712 2713 for (sh = shells; sh < shells + sizeof(shells)/sizeof(shells[0]); sh++) 2714 if (strcmp(sh->name, name) == 0) 2715 break; 2716 2717 if (sh == shells + sizeof(shells)/sizeof(shells[0])) 2718 return (NULL); 2719 2720 /* make a copy */ 2721 nsh = emalloc(sizeof(*nsh)); 2722 2723 nsh->name = estrdup(sh->name); 2724 nsh->echoOff = estrdup(sh->echoOff); 2725 nsh->echoOn = estrdup(sh->echoOn); 2726 nsh->hasEchoCtl = sh->hasEchoCtl; 2727 nsh->noPrint = estrdup(sh->noPrint); 2728 nsh->hasErrCtl = sh->hasErrCtl; 2729 nsh->errCheck = estrdup(sh->errCheck); 2730 nsh->ignErr = estrdup(sh->ignErr); 2731 nsh->echo = estrdup(sh->echo); 2732 nsh->exit = estrdup(sh->exit); 2733 2734 return (nsh); 2735} 2736 2737/** 2738 * Job_ParseShell 2739 * Parse a shell specification and set up commandShell, shellPath 2740 * and shellName appropriately. 2741 * 2742 * Results: 2743 * FAILURE if the specification was incorrect. 2744 * 2745 * Side Effects: 2746 * commandShell points to a Shell structure (either predefined or 2747 * created from the shell spec), shellPath is the full path of the 2748 * shell described by commandShell, while shellName is just the 2749 * final component of shellPath. 2750 * 2751 * Notes: 2752 * A shell specification consists of a .SHELL target, with dependency 2753 * operator, followed by a series of blank-separated words. Double 2754 * quotes can be used to use blanks in words. A backslash escapes 2755 * anything (most notably a double-quote and a space) and 2756 * provides the functionality it does in C. Each word consists of 2757 * keyword and value separated by an equal sign. There should be no 2758 * unnecessary spaces in the word. The keywords are as follows: 2759 * name Name of shell. 2760 * path Location of shell. Overrides "name" if given 2761 * quiet Command to turn off echoing. 2762 * echo Command to turn echoing on 2763 * filter Result of turning off echoing that shouldn't be 2764 * printed. 2765 * echoFlag Flag to turn echoing on at the start 2766 * errFlag Flag to turn error checking on at the start 2767 * hasErrCtl True if shell has error checking control 2768 * check Command to turn on error checking if hasErrCtl 2769 * is TRUE or template of command to echo a command 2770 * for which error checking is off if hasErrCtl is 2771 * FALSE. 2772 * ignore Command to turn off error checking if hasErrCtl 2773 * is TRUE or template of command to execute a 2774 * command so as to ignore any errors it returns if 2775 * hasErrCtl is FALSE. 2776 */ 2777ReturnStatus 2778Job_ParseShell(char *line) 2779{ 2780 char **words; 2781 int wordCount; 2782 char **argv; 2783 int argc; 2784 char *path; 2785 char *eq; 2786 Boolean fullSpec = FALSE; 2787 struct Shell newShell; 2788 struct Shell *sh; 2789 2790 while (isspace((unsigned char)*line)) { 2791 line++; 2792 } 2793 words = brk_string(line, &wordCount, TRUE); 2794 2795 memset(&newShell, 0, sizeof(newShell)); 2796 path = NULL; 2797 2798 /* 2799 * Parse the specification by keyword but skip the first word - it 2800 * is not set by brk_string. 2801 */ 2802 wordCount--; 2803 words++; 2804 2805 for (argc = wordCount, argv = words; argc != 0; argc--, argv++) { 2806 /* 2807 * Split keyword and value 2808 */ 2809 if ((eq = strchr(*argv, '=')) == NULL) { 2810 Parse_Error(PARSE_FATAL, "missing '=' in shell " 2811 "specification keyword '%s'", *argv); 2812 return (FAILURE); 2813 } 2814 *eq++ = '\0'; 2815 2816 if (strcmp(*argv, "path") == 0) { 2817 path = eq; 2818 } else if (strcmp(*argv, "name") == 0) { 2819 newShell.name = eq; 2820 } else if (strcmp(*argv, "quiet") == 0) { 2821 newShell.echoOff = eq; 2822 fullSpec = TRUE; 2823 } else if (strcmp(*argv, "echo") == 0) { 2824 newShell.echoOn = eq; 2825 fullSpec = TRUE; 2826 } else if (strcmp(*argv, "filter") == 0) { 2827 newShell.noPrint = eq; 2828 fullSpec = TRUE; 2829 } else if (strcmp(*argv, "echoFlag") == 0) { 2830 newShell.echo = eq; 2831 fullSpec = TRUE; 2832 } else if (strcmp(*argv, "errFlag") == 0) { 2833 newShell.exit = eq; 2834 fullSpec = TRUE; 2835 } else if (strcmp(*argv, "hasErrCtl") == 0) { 2836 newShell.hasErrCtl = (*eq == 'Y' || *eq == 'y' || 2837 *eq == 'T' || *eq == 't'); 2838 fullSpec = TRUE; 2839 } else if (strcmp(*argv, "check") == 0) { 2840 newShell.errCheck = eq; 2841 fullSpec = TRUE; 2842 } else if (strcmp(*argv, "ignore") == 0) { 2843 newShell.ignErr = eq; 2844 fullSpec = TRUE; 2845 } else { 2846 Parse_Error(PARSE_FATAL, "unknown keyword in shell " 2847 "specification '%s'", *argv); 2848 return (FAILURE); 2849 } 2850 } 2851 2852 /* 2853 * Some checks (could be more) 2854 */ 2855 if (fullSpec) { 2856 if ((newShell.echoOn != NULL) ^ (newShell.echoOff != NULL)) 2857 Parse_Error(PARSE_FATAL, "Shell must have either both " 2858 "echoOff and echoOn or none of them"); 2859 2860 if (newShell.echoOn != NULL && newShell.echoOff) 2861 newShell.hasEchoCtl = TRUE; 2862 } 2863 2864 if (path == NULL) { 2865 /* 2866 * If no path was given, the user wants one of the pre-defined 2867 * shells, yes? So we find the one s/he wants with the help of 2868 * JobMatchShell and set things up the right way. shellPath 2869 * will be set up by Job_Init. 2870 */ 2871 if (newShell.name == NULL) { 2872 Parse_Error(PARSE_FATAL, 2873 "Neither path nor name specified"); 2874 return (FAILURE); 2875 } 2876 if ((sh = JobMatchShell(newShell.name)) == NULL) { 2877 Parse_Error(PARSE_FATAL, "%s: no matching shell", 2878 newShell.name); 2879 return (FAILURE); 2880 } 2881 2882 } else { 2883 /* 2884 * The user provided a path. If s/he gave nothing else 2885 * (fullSpec is FALSE), try and find a matching shell in the 2886 * ones we know of. Else we just take the specification at its 2887 * word and copy it to a new location. In either case, we need 2888 * to record the path the user gave for the shell. 2889 */ 2890 free(shellPath); 2891 shellPath = estrdup(path); 2892 if (newShell.name == NULL) { 2893 /* get the base name as the name */ 2894 path = strrchr(path, '/'); 2895 if (path == NULL) { 2896 path = shellPath; 2897 } else { 2898 path += 1; 2899 } 2900 newShell.name = path; 2901 } 2902 2903 if (!fullSpec) { 2904 if ((sh = JobMatchShell(newShell.name)) == NULL) { 2905 Parse_Error(PARSE_FATAL, 2906 "%s: no matching shell", newShell.name); 2907 return (FAILURE); 2908 } 2909 } else { 2910 sh = JobCopyShell(&newShell); 2911 } 2912 } 2913 2914 /* set the new shell */ 2915 JobFreeShell(commandShell); 2916 commandShell = sh; 2917 2918 shellName = commandShell->name; 2919 2920 return (SUCCESS); 2921} 2922 2923/** 2924 * JobInterrupt 2925 * Handle the receipt of an interrupt. 2926 * 2927 * Side Effects: 2928 * All children are killed. Another job will be started if the 2929 * .INTERRUPT target was given. 2930 */ 2931static void 2932JobInterrupt(int runINTERRUPT, int signo) 2933{ 2934 Job *job; /* job descriptor in that element */ 2935 GNode *interrupt; /* the node describing the .INTERRUPT target */ 2936 2937 aborting = ABORT_INTERRUPT; 2938 2939 TAILQ_FOREACH(job, &jobs, link) { 2940 if (!Targ_Precious(job->node)) { 2941 char *file = (job->node->path == NULL ? 2942 job->node->name : job->node->path); 2943 2944 if (!noExecute && eunlink(file) != -1) { 2945 Error("*** %s removed", file); 2946 } 2947 } 2948 if (job->pid) { 2949 DEBUGF(JOB, ("JobInterrupt passing signal to child " 2950 "%jd.\n", (intmax_t)job->pid)); 2951 KILL(job->pid, signo); 2952 } 2953 } 2954 2955 if (runINTERRUPT && !touchFlag) { 2956 /* 2957 * clear the interrupted flag because we would get an 2958 * infinite loop otherwise. 2959 */ 2960 interrupted = 0; 2961 2962 interrupt = Targ_FindNode(".INTERRUPT", TARG_NOCREATE); 2963 if (interrupt != NULL) { 2964 ignoreErrors = FALSE; 2965 2966 JobStart(interrupt, JOB_IGNDOTS, (Job *)NULL); 2967 while (nJobs) { 2968 Job_CatchOutput(0); 2969 Job_CatchChildren(!usePipes); 2970 } 2971 } 2972 } 2973} 2974 2975/** 2976 * Job_Finish 2977 * Do final processing such as the running of the commands 2978 * attached to the .END target. 2979 * 2980 * Results: 2981 * Number of errors reported. 2982 */ 2983int 2984Job_Finish(void) 2985{ 2986 2987 if (postCommands != NULL && !Lst_IsEmpty(&postCommands->commands)) { 2988 if (errors) { 2989 Error("Errors reported so .END ignored"); 2990 } else { 2991 JobStart(postCommands, JOB_SPECIAL | JOB_IGNDOTS, NULL); 2992 2993 while (nJobs) { 2994 Job_CatchOutput(0); 2995 Job_CatchChildren(!usePipes); 2996 } 2997 } 2998 } 2999 if (fifoFd >= 0) { 3000 close(fifoFd); 3001 fifoFd = -1; 3002 if (fifoMaster) 3003 unlink(fifoName); 3004 } 3005 return (errors); 3006} 3007 3008/** 3009 * Job_Wait 3010 * Waits for all running jobs to finish and returns. Sets 'aborting' 3011 * to ABORT_WAIT to prevent other jobs from starting. 3012 * 3013 * Side Effects: 3014 * Currently running jobs finish. 3015 */ 3016void 3017Job_Wait(void) 3018{ 3019 3020 aborting = ABORT_WAIT; 3021 while (nJobs != 0) { 3022 Job_CatchOutput(0); 3023 Job_CatchChildren(!usePipes); 3024 } 3025 aborting = 0; 3026} 3027 3028/** 3029 * Job_AbortAll 3030 * Abort all currently running jobs without handling output or anything. 3031 * This function is to be called only in the event of a major 3032 * error. Most definitely NOT to be called from JobInterrupt. 3033 * 3034 * Side Effects: 3035 * All children are killed, not just the firstborn 3036 */ 3037void 3038Job_AbortAll(void) 3039{ 3040 Job *job; /* the job descriptor in that element */ 3041 int foo; 3042 3043 aborting = ABORT_ERROR; 3044 3045 if (nJobs) { 3046 TAILQ_FOREACH(job, &jobs, link) { 3047 /* 3048 * kill the child process with increasingly drastic 3049 * signals to make darn sure it's dead. 3050 */ 3051 KILL(job->pid, SIGINT); 3052 KILL(job->pid, SIGKILL); 3053 } 3054 } 3055 3056 /* 3057 * Catch as many children as want to report in at first, then give up 3058 */ 3059 while (waitpid((pid_t)-1, &foo, WNOHANG) > 0) 3060 ; 3061} 3062 3063/** 3064 * JobRestartJobs 3065 * Tries to restart stopped jobs if there are slots available. 3066 * Note that this tries to restart them regardless of pending errors. 3067 * It's not good to leave stopped jobs lying around! 3068 * 3069 * Side Effects: 3070 * Resumes(and possibly migrates) jobs. 3071 */ 3072static void 3073JobRestartJobs(void) 3074{ 3075 Job *job; 3076 3077 while (!jobFull && (job = TAILQ_FIRST(&stoppedJobs)) != NULL) { 3078 DEBUGF(JOB, ("Job queue is not full. " 3079 "Restarting a stopped job.\n")); 3080 TAILQ_REMOVE(&stoppedJobs, job, link); 3081 JobRestart(job); 3082 } 3083} 3084 3085/** 3086 * Cmd_Exec 3087 * Execute the command in cmd, and return the output of that command 3088 * in a string. 3089 * 3090 * Results: 3091 * A string containing the output of the command, or the empty string 3092 * If error is not NULL, it contains the reason for the command failure 3093 * Any output sent to stderr in the child process is passed to stderr, 3094 * and not captured in the string. 3095 * 3096 * Side Effects: 3097 * The string must be freed by the caller. 3098 */ 3099Buffer * 3100Cmd_Exec(const char *cmd, const char **error) 3101{ 3102 int fds[2]; /* Pipe streams */ 3103 int status; /* command exit status */ 3104 Buffer *buf; /* buffer to store the result */ 3105 ssize_t rcnt; 3106 ProcStuff ps; 3107 3108 *error = NULL; 3109 buf = Buf_Init(0); 3110 3111 if (shellPath == NULL) 3112 Shell_Init(); 3113 /* 3114 * Open a pipe for fetching its output 3115 */ 3116 if (pipe(fds) == -1) { 3117 *error = "Couldn't create pipe for \"%s\""; 3118 return (buf); 3119 } 3120 3121 /* Set close-on-exec on read side of pipe. */ 3122 fcntl(fds[0], F_SETFD, fcntl(fds[0], F_GETFD) | FD_CLOEXEC); 3123 3124 ps.in = STDIN_FILENO; 3125 ps.out = fds[1]; 3126 ps.err = STDERR_FILENO; 3127 3128 ps.merge_errors = 0; 3129 ps.pgroup = 0; 3130 ps.searchpath = 0; 3131 3132 /* Set up arguments for shell */ 3133 ps.argv = emalloc(4 * sizeof(char *)); 3134 ps.argv[0] = strdup(shellName); 3135 ps.argv[1] = strdup("-c"); 3136 ps.argv[2] = strdup(cmd); 3137 ps.argv[3] = NULL; 3138 3139 /* 3140 * Fork. Warning since we are doing vfork() instead of fork(), 3141 * do not allocate memory in the child process! 3142 */ 3143 if ((ps.child_pid = vfork()) == -1) { 3144 *error = "Couldn't exec \"%s\""; 3145 return (buf); 3146 3147 } else if (ps.child_pid == 0) { 3148 /* 3149 * Child 3150 */ 3151 ProcExec(&ps); 3152 /* NOTREACHED */ 3153 } 3154 3155 free(ps.argv[2]); 3156 free(ps.argv[1]); 3157 free(ps.argv[0]); 3158 free(ps.argv); 3159 3160 close(fds[1]); /* No need for the writing half of the pipe. */ 3161 3162 do { 3163 char result[BUFSIZ]; 3164 3165 rcnt = read(fds[0], result, sizeof(result)); 3166 if (rcnt != -1) 3167 Buf_AddBytes(buf, (size_t)rcnt, (Byte *)result); 3168 } while (rcnt > 0 || (rcnt == -1 && errno == EINTR)); 3169 3170 if (rcnt == -1) 3171 *error = "Error reading shell's output for \"%s\""; 3172 3173 /* 3174 * Close the input side of the pipe. 3175 */ 3176 close(fds[0]); 3177 3178 status = ProcWait(&ps); 3179 3180 if (status) 3181 *error = "\"%s\" returned non-zero status"; 3182 3183 Buf_StripNewlines(buf); 3184 3185 return (buf); 3186} 3187 3188 3189/* 3190 * Interrupt handler - set flag and defer handling to the main code 3191 */ 3192static void 3193CompatCatchSig(int signo) 3194{ 3195 3196 interrupted = signo; 3197} 3198 3199/*- 3200 *----------------------------------------------------------------------- 3201 * CompatInterrupt -- 3202 * Interrupt the creation of the current target and remove it if 3203 * it ain't precious. 3204 * 3205 * Results: 3206 * None. 3207 * 3208 * Side Effects: 3209 * The target is removed and the process exits. If .INTERRUPT exists, 3210 * its commands are run first WITH INTERRUPTS IGNORED.. 3211 * 3212 *----------------------------------------------------------------------- 3213 */ 3214static void 3215CompatInterrupt(int signo) 3216{ 3217 GNode *gn; 3218 sigset_t nmask, omask; 3219 LstNode *ln; 3220 3221 sigemptyset(&nmask); 3222 sigaddset(&nmask, SIGINT); 3223 sigaddset(&nmask, SIGTERM); 3224 sigaddset(&nmask, SIGHUP); 3225 sigaddset(&nmask, SIGQUIT); 3226 sigprocmask(SIG_SETMASK, &nmask, &omask); 3227 3228 /* prevent recursion in evaluation of .INTERRUPT */ 3229 interrupted = 0; 3230 3231 if (curTarg != NULL && !Targ_Precious(curTarg)) { 3232 char *p1; 3233 char *file = Var_Value(TARGET, curTarg, &p1); 3234 3235 if (!noExecute && eunlink(file) != -1) { 3236 printf("*** %s removed\n", file); 3237 } 3238 free(p1); 3239 } 3240 3241 /* 3242 * Run .INTERRUPT only if hit with interrupt signal 3243 */ 3244 if (signo == SIGINT) { 3245 gn = Targ_FindNode(".INTERRUPT", TARG_NOCREATE); 3246 if (gn != NULL) { 3247 LST_FOREACH(ln, &gn->commands) { 3248 if (Compat_RunCommand(Lst_Datum(ln), gn)) 3249 break; 3250 } 3251 } 3252 } 3253 3254 sigprocmask(SIG_SETMASK, &omask, NULL); 3255 3256 if (signo == SIGQUIT) 3257 exit(signo); 3258 signal(signo, SIG_DFL); 3259 kill(getpid(), signo); 3260} 3261 3262/** 3263 * shellneed 3264 * 3265 * Results: 3266 * Returns NULL if a specified line must be executed by the shell, 3267 * and an argument vector if it can be run via execvp(). 3268 * 3269 * Side Effects: 3270 * Uses brk_string so destroys the contents of argv. 3271 */ 3272static char ** 3273shellneed(char *cmd) 3274{ 3275 char **av; 3276 const char **p; 3277 3278 if (strpbrk(cmd, sh_meta) != NULL) 3279 return (NULL); 3280 3281 av = brk_string(cmd, NULL, TRUE); 3282 for (p = sh_builtin; *p != 0; p++) 3283 if (strcmp(av[1], *p) == 0) 3284 return (NULL); 3285 return (av + 1); 3286} 3287 3288/*- 3289 *----------------------------------------------------------------------- 3290 * Compat_RunCommand -- 3291 * Execute the next command for a target. If the command returns an 3292 * error, the node's made field is set to ERROR and creation stops. 3293 * The node from which the command came is also given. 3294 * 3295 * Results: 3296 * 0 if the command succeeded, 1 if an error occurred. 3297 * 3298 * Side Effects: 3299 * The node's 'made' field may be set to ERROR. 3300 * 3301 *----------------------------------------------------------------------- 3302 */ 3303static int 3304Compat_RunCommand(char *cmd, GNode *gn) 3305{ 3306 char *cmdStart; /* Start of expanded command */ 3307 Boolean silent; /* Don't print command */ 3308 Boolean doit; /* Execute even in -n */ 3309 Boolean errCheck; /* Check errors */ 3310 int reason; /* Reason for child's death */ 3311 int status; /* Description of child's death */ 3312 LstNode *cmdNode; /* Node where current command is located */ 3313 char **av; /* Argument vector for thing to exec */ 3314 char *cmd_save; /* saved cmd */ 3315 ProcStuff ps; 3316 3317 silent = gn->type & OP_SILENT; 3318 errCheck = !(gn->type & OP_IGNORE); 3319 doit = FALSE; 3320 3321 cmdNode = Lst_Member(&gn->commands, cmd); 3322 cmdStart = Buf_Peel(Var_Subst(cmd, gn, FALSE)); 3323 3324 /* 3325 * brk_string will return an argv with a NULL in av[0], thus causing 3326 * execvp() to choke and die horribly. Besides, how can we execute a 3327 * null command? In any case, we warn the user that the command 3328 * expanded to nothing (is this the right thing to do?). 3329 */ 3330 if (*cmdStart == '\0') { 3331 free(cmdStart); 3332 Error("%s expands to empty string", cmd); 3333 return (0); 3334 } else { 3335 cmd = cmdStart; 3336 } 3337 Lst_Replace(cmdNode, cmdStart); 3338 3339 if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) { 3340 Lst_AtEnd(&ENDNode->commands, cmdStart); 3341 return (0); 3342 } else if (strcmp(cmdStart, "...") == 0) { 3343 gn->type |= OP_SAVE_CMDS; 3344 return (0); 3345 } 3346 3347 while (*cmd == '@' || *cmd == '-' || *cmd == '+') { 3348 switch (*cmd) { 3349 3350 case '@': 3351 silent = DEBUG(LOUD) ? FALSE : TRUE; 3352 break; 3353 3354 case '-': 3355 errCheck = FALSE; 3356 break; 3357 3358 case '+': 3359 doit = TRUE; 3360 break; 3361 } 3362 cmd++; 3363 } 3364 3365 while (isspace((unsigned char)*cmd)) 3366 cmd++; 3367 3368 /* 3369 * Print the command before echoing if we're not supposed to be quiet 3370 * for this one. We also print the command if -n given, but not if '+'. 3371 */ 3372 if (!silent || (noExecute && !doit)) { 3373 printf("%s\n", cmd); 3374 fflush(stdout); 3375 } 3376 3377 /* 3378 * If we're not supposed to execute any commands, this is as far as 3379 * we go... 3380 */ 3381 if (!doit && noExecute) { 3382 return (0); 3383 } 3384 3385 ps.in = STDIN_FILENO; 3386 ps.out = STDOUT_FILENO; 3387 ps.err = STDERR_FILENO; 3388 3389 ps.merge_errors = 0; 3390 ps.pgroup = 0; 3391 ps.searchpath = 1; 3392 3393 if ((av = shellneed(cmd)) == NULL) { 3394 /* 3395 * Shell meta character or shell builtin found - pass 3396 * command to shell. We give the shell the -e flag as 3397 * well as -c if it is supposed to exit when it hits an error. 3398 */ 3399 ps.argv = emalloc(4 * sizeof(char *)); 3400 ps.argv[0] = strdup(shellName); 3401 ps.argv[1] = strdup(errCheck ? "-ec" : "-c"); 3402 ps.argv[2] = strdup(cmd); 3403 ps.argv[3] = NULL; 3404 } else { 3405 ps.argv = av; 3406 } 3407 3408 /* 3409 * Warning since we are doing vfork() instead of fork(), 3410 * do not allocate memory in the child process! 3411 */ 3412 if ((ps.child_pid = vfork()) == -1) { 3413 Fatal("Could not fork"); 3414 3415 } else if (ps.child_pid == 0) { 3416 /* 3417 * Child 3418 */ 3419 ProcExec(&ps); 3420 /* NOTREACHED */ 3421 3422 } else { 3423 if (av == NULL) { 3424 free(ps.argv[2]); 3425 free(ps.argv[1]); 3426 free(ps.argv[0]); 3427 free(ps.argv); 3428 } 3429 3430 /* 3431 * we need to print out the command associated with this 3432 * Gnode in Targ_PrintCmd from Targ_PrintGraph when debugging 3433 * at level g2, in main(), Fatal() and DieHorribly(), 3434 * therefore do not free it when debugging. 3435 */ 3436 if (!DEBUG(GRAPH2)) { 3437 free(cmdStart); 3438 Lst_Replace(cmdNode, cmd_save); 3439 } 3440 3441 /* 3442 * The child is off and running. Now all we can do is wait... 3443 */ 3444 reason = ProcWait(&ps); 3445 3446 if (interrupted) 3447 CompatInterrupt(interrupted); 3448 3449 /* 3450 * Decode and report the reason child exited, then 3451 * indicate how we handled it. 3452 */ 3453 if (WIFEXITED(reason)) { 3454 status = WEXITSTATUS(reason); 3455 if (status == 0) { 3456 return (0); 3457 } else { 3458 printf("*** Error code %d", status); 3459 } 3460 } else if (WIFSTOPPED(reason)) { 3461 status = WSTOPSIG(reason); 3462 } else { 3463 status = WTERMSIG(reason); 3464 printf("*** Signal %d", status); 3465 } 3466 3467 if (errCheck) { 3468 gn->made = ERROR; 3469 if (keepgoing) { 3470 /* 3471 * Abort the current 3472 * target, but let 3473 * others continue. 3474 */ 3475 printf(" (continuing)\n"); 3476 } 3477 return (status); 3478 } else { 3479 /* 3480 * Continue executing 3481 * commands for this target. 3482 * If we return 0, this will 3483 * happen... 3484 */ 3485 printf(" (ignored)\n"); 3486 return (0); 3487 } 3488 } 3489} 3490 3491/*- 3492 *----------------------------------------------------------------------- 3493 * CompatMake -- 3494 * Make a target, given the parent, to abort if necessary. 3495 * 3496 * Side Effects: 3497 * If an error is detected and not being ignored, the process exits. 3498 * 3499 *----------------------------------------------------------------------- 3500 */ 3501static int 3502CompatMake(GNode *gn, GNode *pgn) 3503{ 3504 LstNode *ln; 3505 3506 if (gn->type & OP_USE) { 3507 Make_HandleUse(gn, pgn); 3508 3509 } else if (gn->made == UNMADE) { 3510 /* 3511 * First mark ourselves to be made, then apply whatever 3512 * transformations the suffix module thinks are necessary. 3513 * Once that's done, we can descend and make all our children. 3514 * If any of them has an error but the -k flag was given, our 3515 * 'make' field will be set FALSE again. This is our signal to 3516 * not attempt to do anything but abort our parent as well. 3517 */ 3518 gn->make = TRUE; 3519 gn->made = BEINGMADE; 3520 Suff_FindDeps(gn); 3521 LST_FOREACH(ln, &gn->children) 3522 CompatMake(Lst_Datum(ln), gn); 3523 if (!gn->make) { 3524 gn->made = ABORTED; 3525 pgn->make = FALSE; 3526 return (0); 3527 } 3528 3529 if (Lst_Member(&gn->iParents, pgn) != NULL) { 3530 char *p1; 3531 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), pgn); 3532 free(p1); 3533 } 3534 3535 /* 3536 * All the children were made ok. Now cmtime contains the 3537 * modification time of the newest child, we need to find out 3538 * if we exist and when we were modified last. The criteria for 3539 * datedness are defined by the Make_OODate function. 3540 */ 3541 DEBUGF(MAKE, ("Examining %s...", gn->name)); 3542 if (!Make_OODate(gn)) { 3543 gn->made = UPTODATE; 3544 DEBUGF(MAKE, ("up-to-date.\n")); 3545 return (0); 3546 } else { 3547 DEBUGF(MAKE, ("out-of-date.\n")); 3548 } 3549 3550 /* 3551 * If the user is just seeing if something is out-of-date, 3552 * exit now to tell him/her "yes". 3553 */ 3554 if (queryFlag) { 3555 exit(1); 3556 } 3557 3558 /* 3559 * We need to be re-made. We also have to make sure we've got 3560 * a $? variable. To be nice, we also define the $> variable 3561 * using Make_DoAllVar(). 3562 */ 3563 Make_DoAllVar(gn); 3564 3565 /* 3566 * Alter our type to tell if errors should be ignored or things 3567 * should not be printed so Compat_RunCommand knows what to do. 3568 */ 3569 if (Targ_Ignore(gn)) { 3570 gn->type |= OP_IGNORE; 3571 } 3572 if (Targ_Silent(gn)) { 3573 gn->type |= OP_SILENT; 3574 } 3575 3576 if (Job_CheckCommands(gn, Fatal)) { 3577 /* 3578 * Our commands are ok, but we still have to worry 3579 * about the -t flag... 3580 */ 3581 if (!touchFlag) { 3582 curTarg = gn; 3583 LST_FOREACH(ln, &gn->commands) { 3584 if (Compat_RunCommand(Lst_Datum(ln), 3585 gn)) 3586 break; 3587 } 3588 curTarg = NULL; 3589 } else { 3590 Job_Touch(gn, gn->type & OP_SILENT); 3591 } 3592 } else { 3593 gn->made = ERROR; 3594 } 3595 3596 if (gn->made != ERROR) { 3597 /* 3598 * If the node was made successfully, mark it so, update 3599 * its modification time and timestamp all its parents. 3600 * Note that for .ZEROTIME targets, the timestamping 3601 * isn't done. This is to keep its state from affecting 3602 * that of its parent. 3603 */ 3604 gn->made = MADE; 3605#ifndef RECHECK 3606 /* 3607 * We can't re-stat the thing, but we can at least take 3608 * care of rules where a target depends on a source that 3609 * actually creates the target, but only if it has 3610 * changed, e.g. 3611 * 3612 * parse.h : parse.o 3613 * 3614 * parse.o : parse.y 3615 * yacc -d parse.y 3616 * cc -c y.tab.c 3617 * mv y.tab.o parse.o 3618 * cmp -s y.tab.h parse.h || mv y.tab.h parse.h 3619 * 3620 * In this case, if the definitions produced by yacc 3621 * haven't changed from before, parse.h won't have been 3622 * updated and gn->mtime will reflect the current 3623 * modification time for parse.h. This is something of a 3624 * kludge, I admit, but it's a useful one.. 3625 * 3626 * XXX: People like to use a rule like 3627 * 3628 * FRC: 3629 * 3630 * To force things that depend on FRC to be made, so we 3631 * have to check for gn->children being empty as well... 3632 */ 3633 if (!Lst_IsEmpty(&gn->commands) || 3634 Lst_IsEmpty(&gn->children)) { 3635 gn->mtime = now; 3636 } 3637#else 3638 /* 3639 * This is what Make does and it's actually a good 3640 * thing, as it allows rules like 3641 * 3642 * cmp -s y.tab.h parse.h || cp y.tab.h parse.h 3643 * 3644 * to function as intended. Unfortunately, thanks to 3645 * the stateless nature of NFS (and the speed of this 3646 * program), there are times when the modification time 3647 * of a file created on a remote machine will not be 3648 * modified before the stat() implied by the Dir_MTime 3649 * occurs, thus leading us to believe that the file 3650 * is unchanged, wreaking havoc with files that depend 3651 * on this one. 3652 * 3653 * I have decided it is better to make too much than to 3654 * make too little, so this stuff is commented out 3655 * unless you're sure it's ok. 3656 * -- ardeb 1/12/88 3657 */ 3658 if (noExecute || Dir_MTime(gn) == 0) { 3659 gn->mtime = now; 3660 } 3661 if (gn->cmtime > gn->mtime) 3662 gn->mtime = gn->cmtime; 3663 DEBUGF(MAKE, ("update time: %s\n", 3664 Targ_FmtTime(gn->mtime))); 3665#endif 3666 if (!(gn->type & OP_EXEC)) { 3667 pgn->childMade = TRUE; 3668 Make_TimeStamp(pgn, gn); 3669 } 3670 3671 } else if (keepgoing) { 3672 pgn->make = FALSE; 3673 3674 } else { 3675 char *p1; 3676 3677 printf("\n\nStop in %s.\n", 3678 Var_Value(".CURDIR", gn, &p1)); 3679 free(p1); 3680 exit(1); 3681 } 3682 } else if (gn->made == ERROR) { 3683 /* 3684 * Already had an error when making this beastie. Tell the 3685 * parent to abort. 3686 */ 3687 pgn->make = FALSE; 3688 } else { 3689 if (Lst_Member(&gn->iParents, pgn) != NULL) { 3690 char *p1; 3691 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), pgn); 3692 free(p1); 3693 } 3694 switch(gn->made) { 3695 case BEINGMADE: 3696 Error("Graph cycles through %s\n", gn->name); 3697 gn->made = ERROR; 3698 pgn->make = FALSE; 3699 break; 3700 case MADE: 3701 if ((gn->type & OP_EXEC) == 0) { 3702 pgn->childMade = TRUE; 3703 Make_TimeStamp(pgn, gn); 3704 } 3705 break; 3706 case UPTODATE: 3707 if ((gn->type & OP_EXEC) == 0) { 3708 Make_TimeStamp(pgn, gn); 3709 } 3710 break; 3711 default: 3712 break; 3713 } 3714 } 3715 3716 return (0); 3717} 3718 3719/*- 3720 *----------------------------------------------------------------------- 3721 * Compat_Run -- 3722 * Start making again, given a list of target nodes. 3723 * 3724 * Results: 3725 * None. 3726 * 3727 * Side Effects: 3728 * Guess what? 3729 * 3730 *----------------------------------------------------------------------- 3731 */ 3732void 3733Compat_Run(Lst *targs) 3734{ 3735 GNode *gn = NULL; /* Current root target */ 3736 int error_cnt; /* Number of targets not remade due to errors */ 3737 LstNode *ln; 3738 3739 Shell_Init(); /* Set up shell. */ 3740 3741 if (signal(SIGINT, SIG_IGN) != SIG_IGN) { 3742 signal(SIGINT, CompatCatchSig); 3743 } 3744 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) { 3745 signal(SIGTERM, CompatCatchSig); 3746 } 3747 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) { 3748 signal(SIGHUP, CompatCatchSig); 3749 } 3750 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) { 3751 signal(SIGQUIT, CompatCatchSig); 3752 } 3753 3754 ENDNode = Targ_FindNode(".END", TARG_CREATE); 3755 /* 3756 * If the user has defined a .BEGIN target, execute the commands 3757 * attached to it. 3758 */ 3759 if (!queryFlag) { 3760 gn = Targ_FindNode(".BEGIN", TARG_NOCREATE); 3761 if (gn != NULL) { 3762 LST_FOREACH(ln, &gn->commands) { 3763 if (Compat_RunCommand(Lst_Datum(ln), gn)) 3764 break; 3765 } 3766 if (gn->made == ERROR) { 3767 printf("\n\nStop.\n"); 3768 exit(1); 3769 } 3770 } 3771 } 3772 3773 /* 3774 * For each entry in the list of targets to create, call CompatMake on 3775 * it to create the thing. CompatMake will leave the 'made' field of gn 3776 * in one of several states: 3777 * UPTODATE gn was already up-to-date 3778 * MADE gn was recreated successfully 3779 * ERROR An error occurred while gn was being created 3780 * ABORTED gn was not remade because one of its inferiors 3781 * could not be made due to errors. 3782 */ 3783 error_cnt = 0; 3784 while (!Lst_IsEmpty(targs)) { 3785 gn = Lst_DeQueue(targs); 3786 CompatMake(gn, gn); 3787 3788 if (gn->made == UPTODATE) { 3789 printf("`%s' is up to date.\n", gn->name); 3790 } else if (gn->made == ABORTED) { 3791 printf("`%s' not remade because of errors.\n", 3792 gn->name); 3793 error_cnt += 1; 3794 } 3795 } 3796 3797 /* 3798 * If the user has defined a .END target, run its commands. 3799 */ 3800 if (error_cnt == 0) { 3801 LST_FOREACH(ln, &ENDNode->commands) { 3802 if (Compat_RunCommand(Lst_Datum(ln), gn)) 3803 break; 3804 } 3805 } 3806} 3807