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