1129473Spjd/*- 2142727Spjd * Copyright (c) 1988, 1989, 1990, 1993 3129473Spjd * The Regents of the University of California. All rights reserved. 4129473Spjd * Copyright (c) 1988, 1989 by Adam de Boor 5129473Spjd * Copyright (c) 1989 by Berkeley Softworks 6129473Spjd * All rights reserved. 7129473Spjd * 8129473Spjd * This code is derived from software contributed to Berkeley by 9129473Spjd * Adam de Boor. 10129473Spjd * 11129473Spjd * Redistribution and use in source and binary forms, with or without 12129473Spjd * modification, are permitted provided that the following conditions 13155174Spjd * are met: 14129473Spjd * 1. Redistributions of source code must retain the above copyright 15129473Spjd * notice, this list of conditions and the following disclaimer. 16129473Spjd * 2. Redistributions in binary form must reproduce the above copyright 17129473Spjd * notice, this list of conditions and the following disclaimer in the 18129473Spjd * documentation and/or other materials provided with the distribution. 19129473Spjd * 3. All advertising materials mentioning features or use of this software 20129473Spjd * must display the following acknowledgement: 21129473Spjd * This product includes software developed by the University of 22129473Spjd * California, Berkeley and its contributors. 23129473Spjd * 4. Neither the name of the University nor the names of its contributors 24129473Spjd * may be used to endorse or promote products derived from this software 25129473Spjd * without specific prior written permission. 26129473Spjd * 27129473Spjd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28129473Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29129473Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30129473Spjd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31129473Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32129473Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33129473Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34129473Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35129473Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36129473Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37129473Spjd * SUCH DAMAGE. 38129473Spjd * 39131878Spjd * @(#)job.c 8.2 (Berkeley) 3/19/94 40129473Spjd */ 41129473Spjd 42129473Spjd#include <sys/cdefs.h> 43129473Spjd__FBSDID("$FreeBSD: releng/10.2/usr.bin/make/job.c 252679 2013-07-04 03:24:58Z kevlo $"); 44131878Spjd 45151897Srwatson/*- 46129473Spjd * job.c -- 47131878Spjd * handle the creation etc. of our child processes. 48129473Spjd * 49129473Spjd * Interface: 50129473Spjd * Job_Make Start the creation of the given target. 51129473Spjd * 52129473Spjd * Job_CatchChildren 53129473Spjd * Check for and handle the termination of any children. 54129473Spjd * This must be called reasonably frequently to keep the 55129473Spjd * whole make going at a decent clip, since job table 56131878Spjd * entries aren't removed until their process is caught 57131878Spjd * this way. Its single argument is TRUE if the function 58129473Spjd * should block waiting for a child to terminate. 59129473Spjd * 60129473Spjd * Job_CatchOutput Print any output our children have produced. Should 61133318Sphk * also be called fairly frequently to keep the user 62129473Spjd * informed of what's going on. If no output is waiting, 63129473Spjd * it will block for a time given by the SEL_* constants, 64131878Spjd * below, or until output is ready. 65131878Spjd * 66131878Spjd * Job_Init Called to intialize this module. in addition, any 67129473Spjd * commands attached to the .BEGIN target are executed 68129473Spjd * before this function returns. Hence, the makefile must 69131878Spjd * have been parsed before this function is called. 70131878Spjd * 71131878Spjd * Job_Full Return TRUE if the job table is filled. 72134528Spjd * 73131878Spjd * Job_Empty Return TRUE if the job table is completely empty. 74131878Spjd * 75138623Spjd * Job_Finish Perform any final processing which needs doing. This 76131878Spjd * includes the execution of any commands which have 77131878Spjd * been/were attached to the .END target. It should only 78131878Spjd * be called when the job table is empty. 79131878Spjd * 80131878Spjd * Job_AbortAll Abort all currently running jobs. It doesn't handle 81129473Spjd * output or do anything for the jobs, just kills them. 82131878Spjd * It should only be called in an emergency, as it were. 83131878Spjd * 84131878Spjd * Job_CheckCommands 85131878Spjd * Verify that the commands for a target are ok. Provide 86131878Spjd * them if necessary and possible. 87131878Spjd * 88131878Spjd * Job_Touch Update a target without really updating it. 89132095Spjd * 90138623Spjd * Job_Wait Wait for all currently-running jobs to finish. 91131878Spjd * 92131878Spjd * compat.c -- 93132095Spjd * The routines in this file implement the full-compatibility 94133205Spjd * mode of PMake. Most of the special functionality of PMake 95133205Spjd * is available in this mode. Things not supported: 96133205Spjd * - different shells. 97131878Spjd * - friendly variable substitution. 98129473Spjd * 99129473Spjd * Interface: 100129473Spjd * Compat_Run Initialize things for this module and recreate 101129473Spjd * thems as need creatin' 102129473Spjd */ 103129473Spjd 104129473Spjd#include <sys/queue.h> 105129473Spjd#include <sys/types.h> 106129473Spjd#include <sys/select.h> 107129473Spjd#include <sys/stat.h> 108129473Spjd#ifdef USE_KQUEUE 109129473Spjd#include <sys/event.h> 110129473Spjd#endif 111129473Spjd#include <sys/wait.h> 112129473Spjd#include <ctype.h> 113129473Spjd#include <err.h> 114129473Spjd#include <errno.h> 115129473Spjd#include <fcntl.h> 116129473Spjd#include <inttypes.h> 117129473Spjd#include <limits.h> 118129473Spjd#include <paths.h> 119129473Spjd#include <string.h> 120129473Spjd#include <signal.h> 121129473Spjd#include <stdlib.h> 122129473Spjd#include <unistd.h> 123129473Spjd#include <utime.h> 124131878Spjd 125131878Spjd#include "arch.h" 126131878Spjd#include "buf.h" 127131878Spjd#include "config.h" 128131878Spjd#include "dir.h" 129131878Spjd#include "globals.h" 130131878Spjd#include "GNode.h" 131131878Spjd#include "job.h" 132131878Spjd#include "make.h" 133131878Spjd#include "parse.h" 134131878Spjd#include "proc.h" 135131878Spjd#include "shell.h" 136131878Spjd#include "str.h" 137131878Spjd#include "suff.h" 138131878Spjd#include "targ.h" 139131878Spjd#include "util.h" 140131878Spjd#include "var.h" 141129473Spjd 142129473Spjd#define TMPPAT "makeXXXXXXXXXX" 143129473Spjd 144129473Spjd#ifndef USE_KQUEUE 145129473Spjd/* 146129473Spjd * The SEL_ constants determine the maximum amount of time spent in select 147129473Spjd * before coming out to see if a child has finished. SEL_SEC is the number of 148129473Spjd * seconds and SEL_USEC is the number of micro-seconds 149129473Spjd */ 150129473Spjd#define SEL_SEC 2 151129473Spjd#define SEL_USEC 0 152129473Spjd#endif /* !USE_KQUEUE */ 153129473Spjd 154129473Spjd/* 155129473Spjd * Job Table definitions. 156129473Spjd * 157129473Spjd * The job "table" is kept as a linked Lst in 'jobs', with the number of 158129473Spjd * active jobs maintained in the 'nJobs' variable. At no time will this 159129473Spjd * exceed the value of 'maxJobs', initialized by the Job_Init function. 160129473Spjd * 161129473Spjd * When a job is finished, the Make_Update function is called on each of the 162129473Spjd * parents of the node which was just remade. This takes care of the upward 163129473Spjd * traversal of the dependency graph. 164129473Spjd */ 165129473Spjd#define JOB_BUFSIZE 1024 166129473Spjdtypedef struct Job { 167129473Spjd pid_t pid; /* The child's process ID */ 168129473Spjd 169129473Spjd struct GNode *node; /* The target the child is making */ 170132664Spjd 171129473Spjd /* 172129473Spjd * A LstNode for the first command to be saved after the job completes. 173129473Spjd * This is NULL if there was no "..." in the job's commands. 174148092Spjd */ 175129473Spjd LstNode *tailCmds; 176129473Spjd 177132664Spjd /* 178129473Spjd * An FILE* for writing out the commands. This is only 179129473Spjd * used before the job is actually started. 180129473Spjd */ 181129473Spjd FILE *cmdFILE; 182129473Spjd 183129473Spjd /* 184129473Spjd * A word of flags which determine how the module handles errors, 185129473Spjd * echoing, etc. for the job 186129473Spjd */ 187129473Spjd short flags; /* Flags to control treatment of job */ 188129473Spjd#define JOB_IGNERR 0x001 /* Ignore non-zero exits */ 189129473Spjd#define JOB_SILENT 0x002 /* no output */ 190129473Spjd#define JOB_SPECIAL 0x004 /* Target is a special one. i.e. run it locally 191129473Spjd * if we can't export it and maxLocal is 0 */ 192129473Spjd#define JOB_IGNDOTS 0x008 /* Ignore "..." lines when processing 193129473Spjd * commands */ 194129473Spjd#define JOB_FIRST 0x020 /* Job is first job for the node */ 195129473Spjd#define JOB_RESTART 0x080 /* Job needs to be completely restarted */ 196129473Spjd#define JOB_RESUME 0x100 /* Job needs to be resumed b/c it stopped, 197129473Spjd * for some reason */ 198129473Spjd#define JOB_CONTINUING 0x200 /* We are in the process of resuming this job. 199129473Spjd * Used to avoid infinite recursion between 200129473Spjd * JobFinish and JobRestart */ 201129473Spjd 202129473Spjd /* union for handling shell's output */ 203129473Spjd union { 204129473Spjd /* 205129473Spjd * This part is used when usePipes is true. 206129473Spjd * The output is being caught via a pipe and the descriptors 207129473Spjd * of our pipe, an array in which output is line buffered and 208129473Spjd * the current position in that buffer are all maintained for 209129473Spjd * each job. 210129473Spjd */ 211129473Spjd struct { 212129473Spjd /* 213129473Spjd * Input side of pipe associated with 214129473Spjd * job's output channel 215129473Spjd */ 216129473Spjd int op_inPipe; 217129473Spjd 218129473Spjd /* 219129473Spjd * Output side of pipe associated with job's 220129473Spjd * output channel 221129473Spjd */ 222129473Spjd int op_outPipe; 223129473Spjd 224129473Spjd /* 225129473Spjd * Buffer for storing the output of the 226129473Spjd * job, line by line 227129473Spjd */ 228129473Spjd char op_outBuf[JOB_BUFSIZE + 1]; 229129473Spjd 230129473Spjd /* Current position in op_outBuf */ 231129473Spjd int op_curPos; 232129473Spjd } o_pipe; 233129473Spjd 234129473Spjd /* 235129473Spjd * If usePipes is false the output is routed to a temporary 236129473Spjd * file and all that is kept is the name of the file and the 237129473Spjd * descriptor open to the file. 238129473Spjd */ 239129473Spjd struct { 240129473Spjd /* Name of file to which shell output was rerouted */ 241129473Spjd char of_outFile[PATH_MAX]; 242129473Spjd 243129473Spjd /* 244129473Spjd * Stream open to the output file. Used to funnel all 245129473Spjd * from a single job to one file while still allowing 246129473Spjd * multiple shell invocations 247129473Spjd */ 248129473Spjd int of_outFd; 249129473Spjd } o_file; 250129473Spjd 251129473Spjd } output; /* Data for tracking a shell's output */ 252129473Spjd 253129473Spjd TAILQ_ENTRY(Job) link; /* list link */ 254129473Spjd} Job; 255129473Spjd 256129473Spjd#define outPipe output.o_pipe.op_outPipe 257131878Spjd#define inPipe output.o_pipe.op_inPipe 258131878Spjd#define outBuf output.o_pipe.op_outBuf 259129473Spjd#define curPos output.o_pipe.op_curPos 260131878Spjd#define outFile output.o_file.of_outFile 261131878Spjd#define outFd output.o_file.of_outFd 262131878Spjd 263131878SpjdTAILQ_HEAD(JobList, Job); 264131878Spjd 265131878Spjd/* 266131878Spjd * error handling variables 267131878Spjd */ 268131878Spjdstatic int aborting = 0; /* why is the make aborting? */ 269131878Spjd#define ABORT_ERROR 1 /* Because of an error */ 270131878Spjd#define ABORT_INTERRUPT 2 /* Because it was interrupted */ 271131878Spjd#define ABORT_WAIT 3 /* Waiting for jobs to finish */ 272131878Spjd 273131878Spjd/* 274131878Spjd * XXX: Avoid SunOS bug... FILENO() is fp->_file, and file 275131878Spjd * is a char! So when we go above 127 we turn negative! 276131878Spjd */ 277131878Spjd#define FILENO(a) ((unsigned)fileno(a)) 278131878Spjd 279131878Spjd/* 280131878Spjd * post-make command processing. The node postCommands is really just the 281131878Spjd * .END target but we keep it around to avoid having to search for it 282131878Spjd * all the time. 283131878Spjd */ 284131878Spjdstatic GNode *postCommands; 285131878Spjd 286131878Spjd/* 287131878Spjd * The number of commands actually printed for a target. Should this 288129473Spjd * number be 0, no shell will be executed. 289131878Spjd */ 290131878Spjdstatic int numCommands; 291131878Spjd 292131878Spjd/* 293131878Spjd * Return values from JobStart. 294131878Spjd */ 295131878Spjd#define JOB_RUNNING 0 /* Job is running */ 296133204Spjd#define JOB_ERROR 1 /* Error in starting the job */ 297133204Spjd#define JOB_FINISHED 2 /* The job is already finished */ 298131878Spjd#define JOB_STOPPED 3 /* The job is stopped */ 299133204Spjd 300133204Spjd/* 301131878Spjd * The maximum number of jobs that may run. This is initialize from the 302131878Spjd * -j argument for the leading make and from the FIFO for sub-makes. 303131878Spjd */ 304131878Spjdstatic int maxJobs; 305133204Spjd 306133204Spjdstatic int nJobs; /* The number of children currently running */ 307131878Spjd 308131878Spjd/* The structures that describe them */ 309131878Spjdstatic struct JobList jobs = TAILQ_HEAD_INITIALIZER(jobs); 310131878Spjd 311131878Spjdstatic Boolean jobFull; /* Flag to tell when the job table is full. It 312131878Spjd * is set TRUE when (1) the total number of 313131878Spjd * running jobs equals the maximum allowed */ 314131878Spjd#ifdef USE_KQUEUE 315131878Spjdstatic int kqfd; /* File descriptor obtained by kqueue() */ 316131878Spjd#else 317131878Spjdstatic fd_set outputs; /* Set of descriptors of pipes connected to 318129473Spjd * the output channels of children */ 319131878Spjd#endif 320131878Spjd 321131878Spjdstatic GNode *lastNode; /* The node for which output was most recently 322131878Spjd * produced. */ 323131878Spjdstatic const char *targFmt; /* Format string to use to head output from a 324131878Spjd * job when it's not the most-recent job heard 325131878Spjd * from */ 326131878Spjdstatic char *targPrefix = NULL; /* What we print at the start of targFmt */ 327131878Spjd 328131878Spjd#define TARG_FMT "%s %s ---\n" /* Default format */ 329131878Spjd#define MESSAGE(fp, gn) \ 330131878Spjd fprintf(fp, targFmt, targPrefix, gn->name); 331131878Spjd 332131878Spjd/* 333131878Spjd * When JobStart attempts to run a job but isn't allowed to 334131878Spjd * or when Job_CatchChildren detects a job that has 335131878Spjd * been stopped somehow, the job is placed on the stoppedJobs queue to be run 336131878Spjd * when the next job finishes. 337131878Spjd * 338131878Spjd * Lst of Job structures describing jobs that were stopped due to 339133204Spjd * concurrency limits or externally 340131878Spjd */ 341133204Spjdstatic struct JobList stoppedJobs = TAILQ_HEAD_INITIALIZER(stoppedJobs); 342131878Spjd 343131878Spjdstatic int fifoFd; /* Fd of our job fifo */ 344131878Spjdstatic char fifoName[] = "/tmp/make_fifo_XXXXXXXXX"; 345131878Spjdstatic int fifoMaster; 346131878Spjd 347131878Spjdstatic volatile sig_atomic_t interrupted; 348131878Spjd 349131878Spjd 350131878Spjd#if defined(USE_PGRP) && defined(SYSV) 351131878Spjd# define KILL(pid, sig) killpg(-(pid), (sig)) 352131878Spjd#else 353131878Spjd# if defined(USE_PGRP) 354131878Spjd# define KILL(pid, sig) killpg((pid), (sig)) 355131878Spjd# else 356131878Spjd# define KILL(pid, sig) kill((pid), (sig)) 357131878Spjd# endif 358131878Spjd#endif 359131878Spjd 360131878Spjd/* 361131878Spjd * Grmpf... There is no way to set bits of the wait structure 362131878Spjd * anymore with the stupid W*() macros. I liked the union wait 363131878Spjd * stuff much more. So, we devise our own macros... This is 364131878Spjd * really ugly, use dramamine sparingly. You have been warned. 365133204Spjd */ 366133204Spjd#define W_SETMASKED(st, val, fun) \ 367131878Spjd { \ 368131878Spjd int sh = (int)~0; \ 369131878Spjd int mask = fun(sh); \ 370131878Spjd \ 371131878Spjd for (sh = 0; ((mask >> sh) & 1) == 0; sh++) \ 372131878Spjd continue; \ 373131878Spjd *(st) = (*(st) & ~mask) | ((val) << sh); \ 374131878Spjd } 375131878Spjd 376131878Spjd#define W_SETTERMSIG(st, val) W_SETMASKED(st, val, WTERMSIG) 377131878Spjd#define W_SETEXITSTATUS(st, val) W_SETMASKED(st, val, WEXITSTATUS) 378131878Spjd 379131878Spjdstatic void JobRestart(Job *); 380131878Spjdstatic int JobStart(GNode *, int, Job *); 381131878Spjdstatic void JobDoOutput(Job *, Boolean); 382131878Spjdstatic void JobInterrupt(int, int); 383131878Spjdstatic void JobRestartJobs(void); 384131878Spjdstatic int Compat_RunCommand(LstNode *, struct GNode *); 385131878Spjd 386131878Spjdstatic GNode *curTarg = NULL; 387131878Spjdstatic GNode *ENDNode; 388131878Spjd 389131878Spjd/** 390131878Spjd * Create a fifo file with a uniq filename, and returns a file 391133204Spjd * descriptor to that fifo. 392131878Spjd */ 393131878Spjdstatic int 394131878Spjdmkfifotemp(char *template) 395131878Spjd{ 396131878Spjd char *start; 397133204Spjd char *pathend; 398131878Spjd char *ptr; 399131878Spjd const unsigned char padchar[] = 400131878Spjd "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 401133444Spjd 402131878Spjd if (template[0] == '\0') { 403131878Spjd errno = EINVAL; /* bad input string */ 404131878Spjd return (-1); 405131878Spjd } 406131878Spjd 407131878Spjd /* Find end of template string. */ 408131878Spjd pathend = strchr(template, '\0'); 409133204Spjd ptr = pathend - 1; 410133204Spjd 411131878Spjd /* 412133204Spjd * Starting from the end of the template replace spaces with 'X' in 413131878Spjd * them with random characters until there are no more 'X'. 414131878Spjd */ 415133204Spjd while (ptr >= template && *ptr == 'X') { 416131878Spjd uint32_t rand_num = 417131878Spjd#if __FreeBSD_version < 800041 418131878Spjd arc4random() % (sizeof(padchar) - 1); 419131878Spjd#else 420131878Spjd arc4random_uniform(sizeof(padchar) - 1); 421131878Spjd#endif 422131878Spjd *ptr-- = padchar[rand_num]; 423131878Spjd } 424131878Spjd start = ptr + 1; 425131878Spjd 426131878Spjd /* Check the target directory. */ 427131878Spjd for (; ptr > template; --ptr) { 428131878Spjd if (*ptr == '/') { 429133204Spjd struct stat sbuf; 430133204Spjd 431133204Spjd *ptr = '\0'; 432131878Spjd if (stat(template, &sbuf) != 0) 433133201Spjd return (-1); 434131878Spjd 435131878Spjd if (!S_ISDIR(sbuf.st_mode)) { 436131878Spjd errno = ENOTDIR; 437131878Spjd return (-1); 438131878Spjd } 439131878Spjd *ptr = '/'; 440131878Spjd break; 441131878Spjd } 442131878Spjd } 443131878Spjd 444129473Spjd for (;;) { 445131878Spjd if (mkfifo(template, 0600) == 0) { 446129473Spjd int fd; 447131878Spjd 448129473Spjd if ((fd = open(template, O_RDWR, 0600)) < 0) { 449131878Spjd unlink(template); 450131878Spjd return (-1); 451131878Spjd } else { 452131878Spjd return (fd); 453131878Spjd } 454131878Spjd } else { 455131878Spjd if (errno != EEXIST) { 456131878Spjd return (-1); 457131878Spjd } 458131878Spjd } 459131878Spjd 460129473Spjd /* 461131878Spjd * If we have a collision, cycle through the space of 462131878Spjd * filenames. 463131878Spjd */ 464131878Spjd for (ptr = start;;) { 465131878Spjd char *pad; 466131878Spjd 467133204Spjd if (*ptr == '\0' || ptr == pathend) 468131878Spjd return (-1); 469131878Spjd 470131878Spjd pad = strchr(padchar, *ptr); 471131878Spjd if (pad == NULL || *++pad == '\0') { 472131878Spjd *ptr++ = padchar[0]; 473131878Spjd } else { 474131878Spjd *ptr++ = *pad; 475131878Spjd break; 476131878Spjd } 477131878Spjd } 478131878Spjd } 479131878Spjd /*NOTREACHED*/ 480131878Spjd} 481131878Spjd 482131878Spjdstatic void 483131878Spjdcatch_child(int sig __unused) 484131878Spjd{ 485131878Spjd} 486131878Spjd 487131878Spjd/** 488131878Spjd */ 489131878Spjdvoid 490131878SpjdProc_Init(void) 491131878Spjd{ 492131878Spjd /* 493131878Spjd * Catch SIGCHLD so that we get kicked out of select() when we 494131878Spjd * need to look at a child. This is only known to matter for the 495131878Spjd * -j case (perhaps without -P). 496131878Spjd * 497133204Spjd * XXX this is intentionally misplaced. 498131878Spjd */ 499131878Spjd struct sigaction sa; 500131878Spjd 501131878Spjd sigemptyset(&sa.sa_mask); 502131878Spjd sa.sa_flags = SA_RESTART | SA_NOCLDSTOP; 503131878Spjd sa.sa_handler = catch_child; 504131878Spjd sigaction(SIGCHLD, &sa, NULL); 505131878Spjd} 506133204Spjd 507133204Spjd/** 508131878Spjd * Wait for child process to terminate. 509131878Spjd */ 510131878Spjdstatic int 511131878SpjdProcWait(ProcStuff *ps) 512131878Spjd{ 513131878Spjd pid_t pid; 514131878Spjd int status; 515131878Spjd 516133201Spjd /* 517131878Spjd * Wait for the process to exit. 518131878Spjd */ 519131878Spjd for (;;) { 520131878Spjd pid = wait(&status); 521131878Spjd if (pid == -1 && errno != EINTR) { 522131878Spjd Fatal("error in wait: %d", pid); 523163836Spjd /* NOTREACHED */ 524163836Spjd } 525163836Spjd if (pid == ps->child_pid) { 526163836Spjd break; 527163836Spjd } 528163836Spjd if (interrupted) { 529163836Spjd break; 530163836Spjd } 531163836Spjd } 532163836Spjd 533163836Spjd return (status); 534163836Spjd} 535163836Spjd 536163836Spjd/** 537163836Spjd * JobCatchSignal 538163836Spjd * Got a signal. Set global variables and hope that someone will 539163836Spjd * handle it. 540163836Spjd */ 541163836Spjdstatic void 542163836SpjdJobCatchSig(int signo) 543163836Spjd{ 544163836Spjd 545163836Spjd interrupted = signo; 546163836Spjd} 547163836Spjd 548163836Spjd/** 549163836Spjd * JobPassSig -- 550163836Spjd * Pass a signal on to all local jobs if 551163836Spjd * USE_PGRP is defined, then die ourselves. 552163836Spjd * 553163836Spjd * Side Effects: 554163836Spjd * We die by the same signal. 555163836Spjd */ 556163836Spjdstatic void 557163836SpjdJobPassSig(int signo) 558163836Spjd{ 559131878Spjd Job *job; 560131878Spjd sigset_t nmask, omask; 561131878Spjd struct sigaction act; 562131878Spjd 563131878Spjd sigemptyset(&nmask); 564131878Spjd sigaddset(&nmask, signo); 565131878Spjd sigprocmask(SIG_SETMASK, &nmask, &omask); 566131878Spjd 567131878Spjd DEBUGF(JOB, ("JobPassSig(%d) called.\n", signo)); 568129473Spjd TAILQ_FOREACH(job, &jobs, link) { 569129473Spjd DEBUGF(JOB, ("JobPassSig passing signal %d to child %jd.\n", 570129473Spjd signo, (intmax_t)job->pid)); 571129473Spjd KILL(job->pid, signo); 572129473Spjd } 573129473Spjd 574129473Spjd /* 575129473Spjd * Deal with proper cleanup based on the signal received. We only run 576129473Spjd * the .INTERRUPT target if the signal was in fact an interrupt. 577129473Spjd * The other three termination signals are more of a "get out *now*" 578129473Spjd * command. 579129473Spjd */ 580129473Spjd if (signo == SIGINT) { 581129473Spjd JobInterrupt(TRUE, signo); 582163886Spjd } else if (signo == SIGHUP || signo == SIGTERM || signo == SIGQUIT) { 583163886Spjd JobInterrupt(FALSE, signo); 584163886Spjd } 585129473Spjd 586129473Spjd /* 587129473Spjd * Leave gracefully if SIGQUIT, rather than core dumping. 588129473Spjd */ 589129473Spjd if (signo == SIGQUIT) { 590129473Spjd signo = SIGINT; 591129473Spjd } 592129473Spjd 593129473Spjd /* 594129473Spjd * Send ourselves the signal now we've given the message to everyone 595131878Spjd * else. Note we block everything else possible while we're getting 596129473Spjd * the signal. This ensures that all our jobs get continued when we 597129473Spjd * wake up before we take any other signal. 598129473Spjd * XXX this comment seems wrong. 599129473Spjd */ 600129473Spjd act.sa_handler = SIG_DFL; 601129473Spjd sigemptyset(&act.sa_mask); 602129473Spjd act.sa_flags = 0; 603129473Spjd sigaction(signo, &act, NULL); 604129473Spjd 605129473Spjd DEBUGF(JOB, ("JobPassSig passing signal to self, mask = %x.\n", 606129473Spjd ~0 & ~(1 << (signo - 1)))); 607131878Spjd signal(signo, SIG_DFL); 608131878Spjd 609129473Spjd KILL(getpid(), signo); 610129473Spjd 611129473Spjd signo = SIGCONT; 612131878Spjd TAILQ_FOREACH(job, &jobs, link) { 613131878Spjd DEBUGF(JOB, ("JobPassSig passing signal %d to child %jd.\n", 614131878Spjd signo, (intmax_t)job->pid)); 615131878Spjd KILL(job->pid, signo); 616131878Spjd } 617131878Spjd 618131878Spjd sigprocmask(SIG_SETMASK, &omask, NULL); 619131878Spjd sigprocmask(SIG_SETMASK, &omask, NULL); 620131878Spjd act.sa_handler = JobPassSig; 621131878Spjd sigaction(signo, &act, NULL); 622131878Spjd} 623131878Spjd 624131878Spjd/** 625131878Spjd * JobPrintCommand -- 626129473Spjd * Put out another command for the given job. If the command starts 627131878Spjd * with an @ or a - we process it specially. In the former case, 628133205Spjd * so long as the -s and -n flags weren't given to make, we stick 629131878Spjd * a shell-specific echoOff command in the script. In the latter, 630133205Spjd * we ignore errors for the entire job, unless the shell has error 631133205Spjd * control. 632133205Spjd * If the command is just "..." we take all future commands for this 633129473Spjd * job to be commands to be executed once the entire graph has been 634131878Spjd * made and return non-zero to signal that the end of the commands 635131878Spjd * was reached. These commands are later attached to the postCommands 636131878Spjd * node and executed by Job_Finish when all things are done. 637131878Spjd * This function is called from JobStart via LST_FOREACH. 638129473Spjd * 639131878Spjd * Results: 640131878Spjd * Always 0, unless the command was "..." 641131878Spjd * 642131878Spjd * Side Effects: 643131878Spjd * If the command begins with a '-' and the shell has no error control, 644131878Spjd * the JOB_IGNERR flag is set in the job descriptor. 645129473Spjd * If the command is "..." and we're not ignoring such things, 646129473Spjd * tailCmds is set to the successor node of the cmd. 647129473Spjd * numCommands is incremented if the command is actually printed. 648129473Spjd */ 649129473Spjdstatic int 650129473SpjdJobPrintCommand(LstNode *cmdNode, Job *job) 651129473Spjd{ 652129473Spjd Boolean noSpecials; /* true if we shouldn't worry about 653129473Spjd * inserting special commands into 654129473Spjd * the input stream. */ 655129473Spjd Boolean shutUp = FALSE; /* true if we put a no echo command 656129473Spjd * into the command file */ 657132664Spjd Boolean errOff = FALSE; /* true if we turned error checking 658132664Spjd * off before printing the command 659129473Spjd * and need to turn it back on */ 660129473Spjd const char *cmdTemplate;/* Template to use when printing the command */ 661129473Spjd char *cmd; /* Expanded command */ 662129473Spjd 663129473Spjd noSpecials = (noExecute && !(job->node->type & OP_MAKE)); 664129473Spjd 665129473Spjd#define DBPRINTF(fmt, arg) \ 666129473Spjd DEBUGF(JOB, (fmt, arg)); \ 667129473Spjd fprintf(job->cmdFILE, fmt, arg); \ 668129473Spjd fflush(job->cmdFILE); 669129473Spjd 670129473Spjd /* 671129473Spjd * For debugging, we replace each command with the result of expanding 672129473Spjd * the variables in the command. 673129473Spjd */ 674129473Spjd cmd = Buf_Peel(Var_Subst(Lst_Datum(cmdNode), job->node, FALSE)); 675129473Spjd if (strcmp(cmd, "...") == 0) { 676129473Spjd free(cmd); 677129473Spjd job->node->type |= OP_SAVE_CMDS; 678129473Spjd if ((job->flags & JOB_IGNDOTS) == 0) { 679129473Spjd job->tailCmds = Lst_Succ(cmdNode); 680129473Spjd return (1); 681132664Spjd } 682129473Spjd return (0); 683129473Spjd } 684129473Spjd Lst_Replace(cmdNode, cmd); 685129473Spjd 686129473Spjd /* 687129473Spjd * Check for leading @', -' or +'s to control echoing, error checking, 688129473Spjd * and execution on -n. 689129473Spjd */ 690129473Spjd while (*cmd == '@' || *cmd == '-' || *cmd == '+') { 691129473Spjd switch (*cmd) { 692129473Spjd 693129473Spjd case '@': 694129473Spjd shutUp = DEBUG(LOUD) ? FALSE : TRUE; 695129473Spjd break; 696129473Spjd 697129473Spjd case '-': 698129473Spjd errOff = TRUE; 699129473Spjd break; 700129473Spjd 701129473Spjd case '+': 702129473Spjd if (noSpecials) { 703129473Spjd /* 704129473Spjd * We're not actually exececuting anything... 705129473Spjd * but this one needs to be - use compat mode 706129473Spjd * just for it. 707129473Spjd */ 708129473Spjd Compat_RunCommand(cmdNode, job->node); 709129473Spjd return (0); 710129473Spjd } 711129473Spjd break; 712129473Spjd } 713129473Spjd cmd++; 714129473Spjd } 715129473Spjd 716129473Spjd while (isspace((unsigned char)*cmd)) 717129473Spjd cmd++; 718129473Spjd 719129473Spjd /* 720129473Spjd * Ignore empty commands 721129473Spjd */ 722129473Spjd if (*cmd == '\0') { 723129473Spjd return (0); 724129473Spjd } 725129473Spjd 726129473Spjd cmdTemplate = "%s\n"; 727129473Spjd numCommands += 1; 728129473Spjd 729129473Spjd if (shutUp) { 730129473Spjd if (!(job->flags & JOB_SILENT) && !noSpecials && 731129473Spjd commandShell->hasEchoCtl) { 732129473Spjd DBPRINTF("%s\n", commandShell->echoOff); 733129473Spjd } else { 734129473Spjd shutUp = FALSE; 735129473Spjd } 736129473Spjd } 737129473Spjd 738129473Spjd if (errOff) { 739129473Spjd if (!(job->flags & JOB_IGNERR) && !noSpecials) { 740129473Spjd if (commandShell->hasErrCtl) { 741129473Spjd /* 742129473Spjd * We don't want the error-control commands 743129473Spjd * showing up either, so we turn off echoing 744129473Spjd * while executing them. We could put another 745129473Spjd * field in the shell structure to tell 746129473Spjd * JobDoOutput to look for this string too, 747129473Spjd * but why make it any more complex than 748129473Spjd * it already is? 749129473Spjd */ 750129473Spjd if (!(job->flags & JOB_SILENT) && !shutUp && 751129473Spjd commandShell->hasEchoCtl) { 752129473Spjd DBPRINTF("%s\n", commandShell->echoOff); 753129473Spjd DBPRINTF("%s\n", commandShell->ignErr); 754129473Spjd DBPRINTF("%s\n", commandShell->echoOn); 755129473Spjd } else { 756129473Spjd DBPRINTF("%s\n", commandShell->ignErr); 757129473Spjd } 758129473Spjd } else if (commandShell->ignErr && 759129473Spjd *commandShell->ignErr != '\0') { 760129473Spjd /* 761129473Spjd * The shell has no error control, so we need to 762129473Spjd * be weird to get it to ignore any errors from 763129473Spjd * the command. If echoing is turned on, we turn 764129473Spjd * it off and use the errCheck template to echo 765129473Spjd * the command. Leave echoing off so the user 766129473Spjd * doesn't see the weirdness we go through to 767129473Spjd * ignore errors. Set cmdTemplate to use the 768132664Spjd * weirdness instead of the simple "%s\n" 769129473Spjd * template. 770129473Spjd */ 771129473Spjd if (!(job->flags & JOB_SILENT) && !shutUp && 772129473Spjd commandShell->hasEchoCtl) { 773129473Spjd DBPRINTF("%s\n", commandShell->echoOff); 774129473Spjd DBPRINTF(commandShell->errCheck, cmd); 775129473Spjd shutUp = TRUE; 776129473Spjd } 777129473Spjd cmdTemplate = commandShell->ignErr; 778129473Spjd /* 779129473Spjd * The error ignoration (hee hee) is already 780129473Spjd * taken care of by the ignErr template, so 781129473Spjd * pretend error checking is still on. 782129473Spjd */ 783129473Spjd errOff = FALSE; 784129473Spjd } else { 785129473Spjd errOff = FALSE; 786129473Spjd } 787129473Spjd } else { 788129473Spjd errOff = FALSE; 789132664Spjd } 790129473Spjd } 791129473Spjd 792129473Spjd DBPRINTF(cmdTemplate, cmd); 793132664Spjd 794132664Spjd if (errOff) { 795129473Spjd /* 796129473Spjd * If echoing is already off, there's no point in issuing the 797129473Spjd * echoOff command. Otherwise we issue it and pretend it was on 798129473Spjd * for the whole command... 799129473Spjd */ 800132664Spjd if (!shutUp && !(job->flags & JOB_SILENT) && 801129473Spjd commandShell->hasEchoCtl) { 802129473Spjd DBPRINTF("%s\n", commandShell->echoOff); 803129473Spjd shutUp = TRUE; 804129473Spjd } 805129473Spjd DBPRINTF("%s\n", commandShell->errCheck); 806132664Spjd } 807129473Spjd if (shutUp) { 808129473Spjd DBPRINTF("%s\n", commandShell->echoOn); 809129473Spjd } 810129473Spjd return (0); 811129473Spjd} 812129473Spjd 813129473Spjd/** 814129473Spjd * JobClose -- 815132664Spjd * Called to close both input and output pipes when a job is finished. 816129473Spjd * 817129473Spjd * Side Effects: 818129473Spjd * The file descriptors associated with the job are closed. 819132664Spjd */ 820129473Spjdstatic void 821129473SpjdJobClose(Job *job) 822132662Spjd{ 823129473Spjd 824129473Spjd if (usePipes) { 825129473Spjd#if !defined(USE_KQUEUE) 826129473Spjd FD_CLR(job->inPipe, &outputs); 827129473Spjd#endif 828129473Spjd if (job->outPipe != job->inPipe) { 829129473Spjd close(job->outPipe); 830129473Spjd } 831149300Spjd JobDoOutput(job, TRUE); 832129473Spjd close(job->inPipe); 833129473Spjd } else { 834129473Spjd close(job->outFd); 835129473Spjd JobDoOutput(job, TRUE); 836129473Spjd } 837129473Spjd} 838129473Spjd 839129473Spjd/** 840129473Spjd * JobFinish -- 841129473Spjd * Do final processing for the given job including updating 842129473Spjd * parents and starting new jobs as available/necessary. Note 843132664Spjd * that we pay no attention to the JOB_IGNERR flag here. 844129473Spjd * This is because when we're called because of a noexecute flag 845129473Spjd * or something, jstat.w_status is 0 and when called from 846129473Spjd * Job_CatchChildren, the status is zeroed if it s/b ignored. 847129473Spjd * 848129473Spjd * Side Effects: 849129473Spjd * Some nodes may be put on the toBeMade queue. 850129473Spjd * Final commands for the job are placed on postCommands. 851129473Spjd * 852129473Spjd * If we got an error and are aborting (aborting == ABORT_ERROR) and 853129473Spjd * the job list is now empty, we are done for the day. 854129473Spjd * If we recognized an error (makeErrors !=0), we set the aborting flag 855129473Spjd * to ABORT_ERROR so no more jobs will be started. 856129473Spjd */ 857129473Spjdstatic void 858129473SpjdJobFinish(Job *job, int *status) 859129473Spjd{ 860129473Spjd Boolean done; 861129473Spjd LstNode *ln; 862129473Spjd 863129473Spjd if (WIFEXITED(*status)) { 864129473Spjd int job_status = WEXITSTATUS(*status); 865129473Spjd 866129473Spjd JobClose(job); 867129473Spjd /* 868129473Spjd * Deal with ignored errors in -B mode. We need to 869129473Spjd * print a message telling of the ignored error as 870129473Spjd * well as setting status.w_status to 0 so the next 871129473Spjd * command gets run. To do this, we set done to be 872129473Spjd * TRUE if in -B mode and the job exited non-zero. 873129473Spjd */ 874129473Spjd if (job_status == 0) { 875129473Spjd done = FALSE; 876129473Spjd } else { 877129473Spjd if (job->flags & JOB_IGNERR) { 878129473Spjd done = TRUE; 879129473Spjd } else { 880129473Spjd /* 881129473Spjd * If it exited non-zero and either we're 882129473Spjd * doing things our way or we're not ignoring 883129473Spjd * errors, the job is finished. Similarly, if 884129473Spjd * the shell died because of a signal the job 885155174Spjd * is also finished. In these cases, finish 886129473Spjd * out the job's output before printing the 887129473Spjd * exit status... 888129473Spjd */ 889129473Spjd done = TRUE; 890129473Spjd if (job->cmdFILE != NULL && 891129473Spjd job->cmdFILE != stdout) { 892129473Spjd fclose(job->cmdFILE); 893129473Spjd } 894129473Spjd 895129473Spjd } 896129473Spjd } 897129473Spjd } else if (WIFSIGNALED(*status)) { 898129473Spjd if (WTERMSIG(*status) == SIGCONT) { 899129473Spjd /* 900129473Spjd * No need to close things down or anything. 901129473Spjd */ 902129473Spjd done = FALSE; 903129473Spjd } else { 904129473Spjd /* 905129473Spjd * If it exited non-zero and either we're 906129473Spjd * doing things our way or we're not ignoring 907129473Spjd * errors, the job is finished. Similarly, if 908129473Spjd * the shell died because of a signal the job 909129473Spjd * is also finished. In these cases, finish 910129473Spjd * out the job's output before printing the 911129473Spjd * exit status... 912129473Spjd */ 913129473Spjd JobClose(job); 914129473Spjd if (job->cmdFILE != NULL && 915129473Spjd job->cmdFILE != stdout) { 916129473Spjd fclose(job->cmdFILE); 917129473Spjd } 918129473Spjd done = TRUE; 919129473Spjd } 920129473Spjd } else { 921129473Spjd /* 922129473Spjd * No need to close things down or anything. 923129473Spjd */ 924129473Spjd done = FALSE; 925133371Spjd } 926133371Spjd 927133371Spjd if (WIFEXITED(*status)) { 928129473Spjd if (done || DEBUG(JOB)) { 929129473Spjd FILE *out; 930129473Spjd 931129473Spjd if (compatMake && 932129473Spjd !usePipes && 933129473Spjd (job->flags & JOB_IGNERR)) { 934129473Spjd /* 935129473Spjd * If output is going to a file and this job 936129473Spjd * is ignoring errors, arrange to have the 937129473Spjd * exit status sent to the output file as 938129473Spjd * well. 939133373Spjd */ 940133373Spjd out = fdopen(job->outFd, "w"); 941133373Spjd if (out == NULL) 942142727Spjd Punt("Cannot fdopen"); 943133373Spjd } else { 944133373Spjd out = stdout; 945142727Spjd } 946142727Spjd 947142727Spjd DEBUGF(JOB, ("Process %jd exited.\n", 948129473Spjd (intmax_t)job->pid)); 949133373Spjd 950133373Spjd if (WEXITSTATUS(*status) == 0) { 951142727Spjd if (DEBUG(JOB)) { 952142727Spjd if (usePipes && job->node != lastNode) { 953133373Spjd MESSAGE(out, job->node); 954129473Spjd lastNode = job->node; 955129473Spjd } 956129473Spjd fprintf(out, 957129473Spjd "*** [%s] Completed successfully\n", 958129473Spjd job->node->name); 959129473Spjd } 960129473Spjd } else { 961129473Spjd if (usePipes && job->node != lastNode) { 962129473Spjd MESSAGE(out, job->node); 963129473Spjd lastNode = job->node; 964129473Spjd } 965129473Spjd fprintf(out, "*** [%s] Error code %d%s\n", 966129473Spjd job->node->name, 967129473Spjd WEXITSTATUS(*status), 968129473Spjd (job->flags & JOB_IGNERR) ? 969129473Spjd " (ignored)" : ""); 970129473Spjd 971129473Spjd if (job->flags & JOB_IGNERR) { 972129473Spjd *status = 0; 973129473Spjd } 974129473Spjd } 975129473Spjd 976129473Spjd fflush(out); 977129473Spjd } 978129473Spjd } else if (WIFSIGNALED(*status)) { 979129473Spjd if (done || DEBUG(JOB) || (WTERMSIG(*status) == SIGCONT)) { 980129473Spjd FILE *out; 981129473Spjd 982132664Spjd if (compatMake && 983129473Spjd !usePipes && 984129473Spjd (job->flags & JOB_IGNERR)) { 985129473Spjd /* 986129473Spjd * If output is going to a file and this job 987129473Spjd * is ignoring errors, arrange to have the 988129473Spjd * exit status sent to the output file as 989129473Spjd * well. 990129473Spjd */ 991129473Spjd out = fdopen(job->outFd, "w"); 992129473Spjd if (out == NULL) 993129473Spjd Punt("Cannot fdopen"); 994129473Spjd } else { 995129473Spjd out = stdout; 996129473Spjd } 997129473Spjd 998129473Spjd if (WTERMSIG(*status) == SIGCONT) { 999129473Spjd /* 1000129473Spjd * If the beastie has continued, shift the 1001129473Spjd * Job from the stopped list to the running 1002129473Spjd * one (or re-stop it if concurrency is 1003129473Spjd * exceeded) and go and get another child. 1004129473Spjd */ 1005129473Spjd if (job->flags & (JOB_RESUME | JOB_RESTART)) { 1006129473Spjd if (usePipes && job->node != lastNode) { 1007129473Spjd MESSAGE(out, job->node); 1008129473Spjd lastNode = job->node; 1009129473Spjd } 1010129473Spjd fprintf(out, "*** [%s] Continued\n", 1011129473Spjd job->node->name); 1012129473Spjd } 1013129473Spjd if (!(job->flags & JOB_CONTINUING)) { 1014129473Spjd DEBUGF(JOB, ("Warning: process %jd was not " 1015129473Spjd "continuing.\n", (intmax_t) job->pid)); 1016129473Spjd } 1017129473Spjd job->flags &= ~JOB_CONTINUING; 1018129473Spjd TAILQ_INSERT_TAIL(&jobs, job, link); 1019129473Spjd nJobs += 1; 1020129473Spjd DEBUGF(JOB, ("Process %jd is continuing locally.\n", 1021129473Spjd (intmax_t) job->pid)); 1022129473Spjd if (nJobs == maxJobs) { 1023129473Spjd jobFull = TRUE; 1024129473Spjd DEBUGF(JOB, ("Job queue is full.\n")); 1025129473Spjd } 1026129473Spjd fflush(out); 1027129473Spjd return; 1028129473Spjd 1029129473Spjd } else { 1030129473Spjd if (usePipes && job->node != lastNode) { 1031129473Spjd MESSAGE(out, job->node); 1032129473Spjd lastNode = job->node; 1033129473Spjd } 1034129473Spjd fprintf(out, 1035129473Spjd "*** [%s] Signal %d\n", job->node->name, 1036129473Spjd WTERMSIG(*status)); 1037129473Spjd fflush(out); 1038129473Spjd } 1039129473Spjd } 1040129473Spjd } else { 1041129473Spjd /* STOPPED */ 1042129473Spjd FILE *out; 1043133373Spjd 1044142727Spjd if (compatMake && !usePipes && (job->flags & JOB_IGNERR)) { 1045142727Spjd /* 1046129473Spjd * If output is going to a file and this job 1047129473Spjd * is ignoring errors, arrange to have the 1048129473Spjd * exit status sent to the output file as 1049129473Spjd * well. 1050129473Spjd */ 1051129473Spjd out = fdopen(job->outFd, "w"); 1052129473Spjd if (out == NULL) 1053129473Spjd Punt("Cannot fdopen"); 1054129473Spjd } else { 1055129473Spjd out = stdout; 1056129473Spjd } 1057129473Spjd 1058129473Spjd DEBUGF(JOB, ("Process %jd stopped.\n", (intmax_t) job->pid)); 1059129473Spjd if (usePipes && job->node != lastNode) { 1060129473Spjd MESSAGE(out, job->node); 1061129473Spjd lastNode = job->node; 1062129473Spjd } 1063129473Spjd fprintf(out, "*** [%s] Stopped -- signal %d\n", 1064129473Spjd job->node->name, WSTOPSIG(*status)); 1065129473Spjd job->flags |= JOB_RESUME; 1066129473Spjd TAILQ_INSERT_TAIL(&stoppedJobs, job, link); 1067132664Spjd fflush(out); 1068129473Spjd return; 1069129473Spjd } 1070129473Spjd 1071129473Spjd /* 1072129473Spjd * Now handle the -B-mode stuff. If the beast still isn't finished, 1073129473Spjd * try and restart the job on the next command. If JobStart says it's 1074129473Spjd * ok, it's ok. If there's an error, this puppy is done. 1075129473Spjd */ 1076129473Spjd if (compatMake && WIFEXITED(*status) && 1077146109Spjd Lst_Succ(job->node->compat_command) != NULL) { 1078146109Spjd switch (JobStart(job->node, job->flags & JOB_IGNDOTS, job)) { 1079146109Spjd case JOB_RUNNING: 1080146109Spjd done = FALSE; 1081129473Spjd break; 1082129473Spjd case JOB_ERROR: 1083129473Spjd done = TRUE; 1084129473Spjd W_SETEXITSTATUS(status, 1); 1085129473Spjd break; 1086129473Spjd case JOB_FINISHED: 1087129473Spjd /* 1088129473Spjd * If we got back a JOB_FINISHED code, JobStart has 1089129473Spjd * already called Make_Update and freed the job 1090129473Spjd * descriptor. We set done to false here to avoid fake 1091129473Spjd * cycles and double frees. JobStart needs to do the 1092129473Spjd * update so we can proceed up the graph when given 1093129473Spjd * the -n flag.. 1094129473Spjd */ 1095129473Spjd done = FALSE; 1096129473Spjd break; 1097129473Spjd default: 1098129473Spjd break; 1099129473Spjd } 1100129473Spjd } else { 1101129473Spjd done = TRUE; 1102129473Spjd } 1103129473Spjd 1104129473Spjd if (done && aborting != ABORT_ERROR && 1105129473Spjd aborting != ABORT_INTERRUPT && *status == 0) { 1106129473Spjd /* 1107129473Spjd * As long as we aren't aborting and the job didn't return a 1108129473Spjd * non-zero status that we shouldn't ignore, we call 1109129473Spjd * Make_Update to update the parents. In addition, any saved 1110129473Spjd * commands for the node are placed on the .END target. 1111132664Spjd */ 1112129473Spjd for (ln = job->tailCmds; ln != NULL; ln = LST_NEXT(ln)) { 1113129473Spjd Lst_AtEnd(&postCommands->commands, 1114129473Spjd Buf_Peel( 1115129473Spjd Var_Subst(Lst_Datum(ln), job->node, FALSE))); 1116129473Spjd } 1117129473Spjd 1118129473Spjd job->node->made = MADE; 1119129473Spjd Make_Update(job->node); 1120129473Spjd free(job); 1121129473Spjd 1122129473Spjd } else if (*status != 0) { 1123129473Spjd makeErrors++; 1124129473Spjd free(job); 1125129473Spjd } 1126129473Spjd 1127129473Spjd JobRestartJobs(); 1128129473Spjd 1129129473Spjd /* 1130129473Spjd * Set aborting if any error. 1131129473Spjd */ 1132129473Spjd if (makeErrors && !keepgoing && aborting != ABORT_INTERRUPT) { 1133129473Spjd /* 1134129473Spjd * If we found any errors in this batch of children and the -k 1135129473Spjd * flag wasn't given, we set the aborting flag so no more jobs 1136129473Spjd * get started. 1137129473Spjd */ 1138129473Spjd aborting = ABORT_ERROR; 1139129473Spjd } 1140129473Spjd 1141129473Spjd if (aborting == ABORT_ERROR && Job_Empty()) { 1142129473Spjd /* 1143129473Spjd * If we are aborting and the job table is now empty, we finish. 1144129473Spjd */ 1145129473Spjd Finish(makeErrors); 1146129473Spjd } 1147129473Spjd} 1148129473Spjd 1149129473Spjd/** 1150129473Spjd * Job_Touch 1151129473Spjd * Touch the given target. Called by JobStart when the -t flag was 1152129473Spjd * given. Prints messages unless told to be silent. 1153129473Spjd * 1154129473Spjd * Side Effects: 1155129473Spjd * The data modification of the file is changed. In addition, if the 1156129473Spjd * file did not exist, it is created. 1157129473Spjd */ 1158132664Spjdvoid 1159129473SpjdJob_Touch(GNode *gn, Boolean silent) 1160129473Spjd{ 1161129473Spjd int streamID; /* ID of stream opened to do the touch */ 1162129473Spjd struct utimbuf times; /* Times for utime() call */ 1163129473Spjd 1164129473Spjd if (gn->type & (OP_JOIN | OP_USE | OP_EXEC | OP_OPTIONAL)) { 1165129473Spjd /* 1166129473Spjd * .JOIN, .USE, .ZEROTIME and .OPTIONAL targets are "virtual" 1167129473Spjd * targets and, as such, shouldn't really be created. 1168129473Spjd */ 1169129473Spjd return; 1170129473Spjd } 1171129473Spjd 1172129473Spjd if (!silent) { 1173129473Spjd fprintf(stdout, "touch %s\n", gn->name); 1174129473Spjd fflush(stdout); 1175129473Spjd } 1176129473Spjd 1177129473Spjd if (noExecute) { 1178129473Spjd return; 1179129473Spjd } 1180129473Spjd 1181129473Spjd if (gn->type & OP_ARCHV) { 1182129473Spjd Arch_Touch(gn); 1183129473Spjd } else if (gn->type & OP_LIB) { 1184131649Spjd Arch_TouchLib(gn); 1185131649Spjd } else { 1186129473Spjd char *file = gn->path ? gn->path : gn->name; 1187129473Spjd 1188129473Spjd times.actime = times.modtime = now; 1189129473Spjd if (utime(file, ×) < 0) { 1190129473Spjd streamID = open(file, O_RDWR | O_CREAT, 0666); 1191129473Spjd 1192129473Spjd if (streamID >= 0) { 1193129473Spjd char c; 1194129473Spjd 1195129473Spjd /* 1196129473Spjd * Read and write a byte to the file to change 1197129473Spjd * the modification time, then close the file. 1198129473Spjd */ 1199129473Spjd if (read(streamID, &c, 1) == 1) { 1200132665Spjd lseek(streamID, (off_t)0, SEEK_SET); 1201129473Spjd write(streamID, &c, 1); 1202132665Spjd } 1203132665Spjd 1204132665Spjd close(streamID); 1205134292Spjd } else { 1206134292Spjd fprintf(stdout, "*** couldn't touch %s: %s", 1207132665Spjd file, strerror(errno)); 1208132665Spjd fflush(stdout); 1209132665Spjd } 1210132665Spjd } 1211132665Spjd } 1212132665Spjd} 1213132665Spjd 1214132665Spjd/** 1215132665Spjd * Job_CheckCommands 1216132665Spjd * Make sure the given node has all the commands it needs. 1217132665Spjd * 1218132665Spjd * Results: 1219132665Spjd * TRUE if the commands list is/was ok. 1220132665Spjd * 1221132665Spjd * Side Effects: 1222132665Spjd * The node will have commands from the .DEFAULT rule added to it 1223132665Spjd * if it needs them. 1224132665Spjd */ 1225132665SpjdBoolean 1226132665SpjdJob_CheckCommands(GNode *gn, void (*abortProc)(const char *, ...)) 1227132665Spjd{ 1228132665Spjd 1229132665Spjd if (OP_NOP(gn->type) && Lst_IsEmpty(&gn->commands) && 1230132665Spjd (gn->type & OP_LIB) == 0) { 1231132665Spjd /* 1232129473Spjd * No commands. Look for .DEFAULT rule from which we might infer 1233129473Spjd * commands. 1234129473Spjd */ 1235129473Spjd if (DEFAULT != NULL && !Lst_IsEmpty(&DEFAULT->commands)) { 1236 /* 1237 * Make only looks for a .DEFAULT if the node was 1238 * never the target of an operator, so that's what we 1239 * do too. If a .DEFAULT was given, we substitute its 1240 * commands for gn's commands and set the IMPSRC 1241 * variable to be the target's name The DEFAULT node 1242 * acts like a transformation rule, in that gn also 1243 * inherits any attributes or sources attached to 1244 * .DEFAULT itself. 1245 */ 1246 Make_HandleUse(DEFAULT, gn); 1247 Var_Set(IMPSRC, Var_Value(TARGET, gn), gn); 1248 1249 } else if (Dir_MTime(gn) == 0) { 1250 /* 1251 * The node wasn't the target of an operator we have 1252 * no .DEFAULT rule to go on and the target doesn't 1253 * already exist. There's nothing more we can do for 1254 * this branch. If the -k flag wasn't given, we stop 1255 * in our tracks, otherwise we just don't update 1256 * this node's parents so they never get examined. 1257 */ 1258 static const char msg[] = 1259 "make: don't know how to make"; 1260 1261 if (gn->type & OP_OPTIONAL) { 1262 fprintf(stdout, "%s %s(ignored)\n", 1263 msg, gn->name); 1264 fflush(stdout); 1265 } else if (keepgoing) { 1266 fprintf(stdout, "%s %s(continuing)\n", 1267 msg, gn->name); 1268 fflush(stdout); 1269 return (FALSE); 1270 } else { 1271#ifndef WITHOUT_OLD_JOKE 1272 if (strcmp(gn->name,"love") == 0) 1273 (*abortProc)("Not war."); 1274 else 1275#endif 1276 (*abortProc)("%s %s. Stop", 1277 msg, gn->name); 1278 return (FALSE); 1279 } 1280 } 1281 } 1282 return (TRUE); 1283} 1284 1285/** 1286 * JobExec 1287 * Execute the shell for the given job. Called from JobStart and 1288 * JobRestart. 1289 * 1290 * Side Effects: 1291 * A shell is executed, outputs is altered and the Job structure added 1292 * to the job table. 1293 */ 1294static void 1295JobExec(Job *job, char **argv) 1296{ 1297 ProcStuff ps; 1298 1299 if (DEBUG(JOB)) { 1300 int i; 1301 1302 DEBUGF(JOB, ("Running %s\n", job->node->name)); 1303 DEBUGF(JOB, ("\tCommand: ")); 1304 for (i = 0; argv[i] != NULL; i++) { 1305 DEBUGF(JOB, ("%s ", argv[i])); 1306 } 1307 DEBUGF(JOB, ("\n")); 1308 } 1309 1310 /* 1311 * Some jobs produce no output and it's disconcerting to have 1312 * no feedback of their running (since they produce no output, the 1313 * banner with their name in it never appears). This is an attempt to 1314 * provide that feedback, even if nothing follows it. 1315 */ 1316 if (lastNode != job->node && (job->flags & JOB_FIRST) && 1317 !(job->flags & JOB_SILENT)) { 1318 MESSAGE(stdout, job->node); 1319 lastNode = job->node; 1320 } 1321 1322 ps.in = FILENO(job->cmdFILE); 1323 if (usePipes) { 1324 /* 1325 * Set up the child's output to be routed through the 1326 * pipe we've created for it. 1327 */ 1328 ps.out = job->outPipe; 1329 } else { 1330 /* 1331 * We're capturing output in a file, so we duplicate 1332 * the descriptor to the temporary file into the 1333 * standard output. 1334 */ 1335 ps.out = job->outFd; 1336 } 1337 ps.err = STDERR_FILENO; 1338 1339 ps.merge_errors = 1; 1340 ps.pgroup = 1; 1341 ps.searchpath = 0; 1342 1343 ps.argv = argv; 1344 ps.argv_free = 0; 1345 1346 /* 1347 * Fork. Warning since we are doing vfork() instead of fork(), 1348 * do not allocate memory in the child process! 1349 */ 1350 if ((ps.child_pid = vfork()) == -1) { 1351 Punt("Cannot fork"); 1352 1353 1354 } else if (ps.child_pid == 0) { 1355 /* 1356 * Child 1357 */ 1358 if (fifoFd >= 0) 1359 close(fifoFd); 1360 1361 Proc_Exec(&ps); 1362 /* NOTREACHED */ 1363 } 1364 1365 /* 1366 * Parent 1367 */ 1368 job->pid = ps.child_pid; 1369 1370 if (usePipes && (job->flags & JOB_FIRST)) { 1371 /* 1372 * The first time a job is run for a node, we set the 1373 * current position in the buffer to the beginning and 1374 * mark another stream to watch in the outputs mask. 1375 */ 1376#ifdef USE_KQUEUE 1377 struct kevent kev[2]; 1378#endif 1379 job->curPos = 0; 1380 1381#if defined(USE_KQUEUE) 1382 EV_SET(&kev[0], job->inPipe, EVFILT_READ, EV_ADD, 0, 0, job); 1383 EV_SET(&kev[1], job->pid, EVFILT_PROC, 1384 EV_ADD | EV_ONESHOT, NOTE_EXIT, 0, NULL); 1385 if (kevent(kqfd, kev, 2, NULL, 0, NULL) != 0) { 1386 /* 1387 * kevent() will fail if the job is already 1388 * finished 1389 */ 1390 if (errno != EINTR && errno != EBADF && errno != ESRCH) 1391 Punt("kevent: %s", strerror(errno)); 1392 } 1393#else 1394 FD_SET(job->inPipe, &outputs); 1395#endif /* USE_KQUEUE */ 1396 } 1397 1398 if (job->cmdFILE != NULL && job->cmdFILE != stdout) { 1399 fclose(job->cmdFILE); 1400 job->cmdFILE = NULL; 1401 } 1402 1403 /* 1404 * Now the job is actually running, add it to the table. 1405 */ 1406 nJobs += 1; 1407 TAILQ_INSERT_TAIL(&jobs, job, link); 1408 if (nJobs == maxJobs) { 1409 jobFull = TRUE; 1410 } 1411} 1412 1413/** 1414 * JobMakeArgv 1415 * Create the argv needed to execute the shell for a given job. 1416 */ 1417static void 1418JobMakeArgv(Job *job, char **argv) 1419{ 1420 int argc; 1421 static char args[10]; /* For merged arguments */ 1422 1423 argv[0] = commandShell->name; 1424 argc = 1; 1425 1426 if ((commandShell->exit && *commandShell->exit != '-') || 1427 (commandShell->echo && *commandShell->echo != '-')) { 1428 /* 1429 * At least one of the flags doesn't have a minus before it, so 1430 * merge them together. Have to do this because the *(&(@*#*&#$# 1431 * Bourne shell thinks its second argument is a file to source. 1432 * Grrrr. Note the ten-character limitation on the combined 1433 * arguments. 1434 */ 1435 sprintf(args, "-%s%s", (job->flags & JOB_IGNERR) ? "" : 1436 commandShell->exit ? commandShell->exit : "", 1437 (job->flags & JOB_SILENT) ? "" : 1438 commandShell->echo ? commandShell->echo : ""); 1439 1440 if (args[1]) { 1441 argv[argc] = args; 1442 argc++; 1443 } 1444 } else { 1445 if (!(job->flags & JOB_IGNERR) && commandShell->exit) { 1446 argv[argc] = commandShell->exit; 1447 argc++; 1448 } 1449 if (!(job->flags & JOB_SILENT) && commandShell->echo) { 1450 argv[argc] = commandShell->echo; 1451 argc++; 1452 } 1453 } 1454 argv[argc] = NULL; 1455} 1456 1457/** 1458 * JobRestart 1459 * Restart a job that stopped for some reason. The job must be neither 1460 * on the jobs nor on the stoppedJobs list. 1461 * 1462 * Side Effects: 1463 * jobFull will be set if the job couldn't be run. 1464 */ 1465static void 1466JobRestart(Job *job) 1467{ 1468 1469 if (job->flags & JOB_RESTART) { 1470 /* 1471 * Set up the control arguments to the shell. This is based on 1472 * the flags set earlier for this job. If the JOB_IGNERR flag 1473 * is clear, the 'exit' flag of the commandShell is used to 1474 * cause it to exit upon receiving an error. If the JOB_SILENT 1475 * flag is clear, the 'echo' flag of the commandShell is used 1476 * to get it to start echoing as soon as it starts 1477 * processing commands. 1478 */ 1479 char *argv[4]; 1480 1481 JobMakeArgv(job, argv); 1482 1483 DEBUGF(JOB, ("Restarting %s...", job->node->name)); 1484 if (nJobs >= maxJobs && !(job->flags & JOB_SPECIAL)) { 1485 /* 1486 * Not allowed to run -- put it back on the hold 1487 * queue and mark the table full 1488 */ 1489 DEBUGF(JOB, ("holding\n")); 1490 TAILQ_INSERT_HEAD(&stoppedJobs, job, link); 1491 jobFull = TRUE; 1492 DEBUGF(JOB, ("Job queue is full.\n")); 1493 return; 1494 } else { 1495 /* 1496 * Job may be run locally. 1497 */ 1498 DEBUGF(JOB, ("running locally\n")); 1499 } 1500 JobExec(job, argv); 1501 1502 } else { 1503 /* 1504 * The job has stopped and needs to be restarted. 1505 * Why it stopped, we don't know... 1506 */ 1507 DEBUGF(JOB, ("Resuming %s...", job->node->name)); 1508 if ((nJobs < maxJobs || ((job->flags & JOB_SPECIAL) && 1509 maxJobs == 0)) && nJobs != maxJobs) { 1510 /* 1511 * If we haven't reached the concurrency limit already 1512 * (or the job must be run and maxJobs is 0), it's ok 1513 * to resume it. 1514 */ 1515 Boolean error; 1516 int status; 1517 1518 error = (KILL(job->pid, SIGCONT) != 0); 1519 1520 if (!error) { 1521 /* 1522 * Make sure the user knows we've continued 1523 * the beast and actually put the thing in the 1524 * job table. 1525 */ 1526 job->flags |= JOB_CONTINUING; 1527 status = 0; 1528 W_SETTERMSIG(&status, SIGCONT); 1529 JobFinish(job, &status); 1530 1531 job->flags &= ~(JOB_RESUME|JOB_CONTINUING); 1532 DEBUGF(JOB, ("done\n")); 1533 } else { 1534 Error("couldn't resume %s: %s", 1535 job->node->name, strerror(errno)); 1536 status = 0; 1537 W_SETEXITSTATUS(&status, 1); 1538 JobFinish(job, &status); 1539 } 1540 } else { 1541 /* 1542 * Job cannot be restarted. Mark the table as full and 1543 * place the job back on the list of stopped jobs. 1544 */ 1545 DEBUGF(JOB, ("table full\n")); 1546 TAILQ_INSERT_HEAD(&stoppedJobs, job, link); 1547 jobFull = TRUE; 1548 DEBUGF(JOB, ("Job queue is full.\n")); 1549 } 1550 } 1551} 1552 1553/** 1554 * JobStart 1555 * Start a target-creation process going for the target described 1556 * by the graph node gn. 1557 * 1558 * Results: 1559 * JOB_ERROR if there was an error in the commands, JOB_FINISHED 1560 * if there isn't actually anything left to do for the job and 1561 * JOB_RUNNING if the job has been started. 1562 * 1563 * Side Effects: 1564 * A new Job node is created and added to the list of running 1565 * jobs. PMake is forked and a child shell created. 1566 */ 1567static int 1568JobStart(GNode *gn, int flags, Job *previous) 1569{ 1570 Job *job; /* new job descriptor */ 1571 char *argv[4]; /* Argument vector to shell */ 1572 Boolean cmdsOK; /* true if the nodes commands were all right */ 1573 Boolean noExec; /* Set true if we decide not to run the job */ 1574 int tfd; /* File descriptor for temp file */ 1575 LstNode *ln; 1576 char tfile[PATH_MAX]; 1577 const char *tdir; 1578 1579 if (interrupted) { 1580 JobPassSig(interrupted); 1581 return (JOB_ERROR); 1582 } 1583 if (previous != NULL) { 1584 previous->flags &= ~(JOB_FIRST | JOB_IGNERR | JOB_SILENT); 1585 job = previous; 1586 } else { 1587 job = emalloc(sizeof(Job)); 1588 flags |= JOB_FIRST; 1589 } 1590 1591 job->node = gn; 1592 job->tailCmds = NULL; 1593 1594 /* 1595 * Set the initial value of the flags for this job based on the global 1596 * ones and the node's attributes... Any flags supplied by the caller 1597 * are also added to the field. 1598 */ 1599 job->flags = 0; 1600 if (Targ_Ignore(gn)) { 1601 job->flags |= JOB_IGNERR; 1602 } 1603 if (Targ_Silent(gn)) { 1604 job->flags |= JOB_SILENT; 1605 } 1606 job->flags |= flags; 1607 1608 /* 1609 * Check the commands now so any attributes from .DEFAULT have a chance 1610 * to migrate to the node. 1611 */ 1612 if (!compatMake && (job->flags & JOB_FIRST)) { 1613 cmdsOK = Job_CheckCommands(gn, Error); 1614 } else { 1615 cmdsOK = TRUE; 1616 } 1617 1618 if ((tdir = getenv("TMPDIR")) == NULL) 1619 tdir = _PATH_TMP; 1620 1621 /* 1622 * If the -n flag wasn't given, we open up OUR (not the child's) 1623 * temporary file to stuff commands in it. The thing is rd/wr so we 1624 * don't need to reopen it to feed it to the shell. If the -n flag 1625 * *was* given, we just set the file to be stdout. Cute, huh? 1626 */ 1627 if ((gn->type & OP_MAKE) || (!noExecute && !touchFlag)) { 1628 /* 1629 * We're serious here, but if the commands were bogus, we're 1630 * also dead... 1631 */ 1632 if (!cmdsOK) { 1633 DieHorribly(); 1634 } 1635 1636 snprintf(tfile, sizeof(tfile), "%s/%s", tdir, TMPPAT); 1637 if ((tfd = mkstemp(tfile)) == -1) 1638 Punt("Cannot create temp file: %s", strerror(errno)); 1639 job->cmdFILE = fdopen(tfd, "w+"); 1640 eunlink(tfile); 1641 if (job->cmdFILE == NULL) { 1642 close(tfd); 1643 Punt("Could not open %s", tfile); 1644 } 1645 fcntl(FILENO(job->cmdFILE), F_SETFD, 1); 1646 /* 1647 * Send the commands to the command file, flush all its 1648 * buffers then rewind and remove the thing. 1649 */ 1650 noExec = FALSE; 1651 1652 /* 1653 * Used to be backwards; replace when start doing multiple 1654 * commands per shell. 1655 */ 1656 if (compatMake) { 1657 /* 1658 * Be compatible: If this is the first time for this 1659 * node, verify its commands are ok and open the 1660 * commands list for sequential access by later 1661 * invocations of JobStart. Once that is done, we take 1662 * the next command off the list and print it to the 1663 * command file. If the command was an ellipsis, note 1664 * that there's nothing more to execute. 1665 */ 1666 if (job->flags & JOB_FIRST) 1667 gn->compat_command = Lst_First(&gn->commands); 1668 else 1669 gn->compat_command = 1670 Lst_Succ(gn->compat_command); 1671 1672 if (gn->compat_command == NULL || 1673 JobPrintCommand(gn->compat_command, job)) 1674 noExec = TRUE; 1675 1676 if (noExec && !(job->flags & JOB_FIRST)) { 1677 /* 1678 * If we're not going to execute anything, the 1679 * job is done and we need to close down the 1680 * various file descriptors we've opened for 1681 * output, then call JobDoOutput to catch the 1682 * final characters or send the file to the 1683 * screen... Note that the i/o streams are only 1684 * open if this isn't the first job. Note also 1685 * that this could not be done in 1686 * Job_CatchChildren b/c it wasn't clear if 1687 * there were more commands to execute or not... 1688 */ 1689 JobClose(job); 1690 } 1691 } else { 1692 /* 1693 * We can do all the commands at once. hooray for sanity 1694 */ 1695 numCommands = 0; 1696 LST_FOREACH(ln, &gn->commands) { 1697 if (JobPrintCommand(ln, job)) 1698 break; 1699 } 1700 1701 /* 1702 * If we didn't print out any commands to the shell 1703 * script, there's not much point in executing the 1704 * shell, is there? 1705 */ 1706 if (numCommands == 0) { 1707 noExec = TRUE; 1708 } 1709 } 1710 1711 } else if (noExecute) { 1712 /* 1713 * Not executing anything -- just print all the commands to 1714 * stdout in one fell swoop. This will still set up 1715 * job->tailCmds correctly. 1716 */ 1717 if (lastNode != gn) { 1718 MESSAGE(stdout, gn); 1719 lastNode = gn; 1720 } 1721 job->cmdFILE = stdout; 1722 1723 /* 1724 * Only print the commands if they're ok, but don't die if 1725 * they're not -- just let the user know they're bad and keep 1726 * going. It doesn't do any harm in this case and may do 1727 * some good. 1728 */ 1729 if (cmdsOK) { 1730 LST_FOREACH(ln, &gn->commands) { 1731 if (JobPrintCommand(ln, job)) 1732 break; 1733 } 1734 } 1735 /* 1736 * Don't execute the shell, thank you. 1737 */ 1738 noExec = TRUE; 1739 1740 } else { 1741 /* 1742 * Just touch the target and note that no shell should be 1743 * executed. Set cmdFILE to stdout to make life easier. Check 1744 * the commands, too, but don't die if they're no good -- it 1745 * does no harm to keep working up the graph. 1746 */ 1747 job->cmdFILE = stdout; 1748 Job_Touch(gn, job->flags & JOB_SILENT); 1749 noExec = TRUE; 1750 } 1751 1752 /* 1753 * If we're not supposed to execute a shell, don't. 1754 */ 1755 if (noExec) { 1756 /* 1757 * Unlink and close the command file if we opened one 1758 */ 1759 if (job->cmdFILE != stdout) { 1760 if (job->cmdFILE != NULL) 1761 fclose(job->cmdFILE); 1762 } else { 1763 fflush(stdout); 1764 } 1765 1766 /* 1767 * We only want to work our way up the graph if we aren't here 1768 * because the commands for the job were no good. 1769 */ 1770 if (cmdsOK) { 1771 if (aborting == 0) { 1772 for (ln = job->tailCmds; ln != NULL; 1773 ln = LST_NEXT(ln)) { 1774 Lst_AtEnd(&postCommands->commands, 1775 Buf_Peel(Var_Subst(Lst_Datum(ln), 1776 job->node, FALSE))); 1777 } 1778 job->node->made = MADE; 1779 Make_Update(job->node); 1780 } 1781 free(job); 1782 return(JOB_FINISHED); 1783 } else { 1784 free(job); 1785 return(JOB_ERROR); 1786 } 1787 } else { 1788 fflush(job->cmdFILE); 1789 } 1790 1791 /* 1792 * Set up the control arguments to the shell. This is based on the flags 1793 * set earlier for this job. 1794 */ 1795 JobMakeArgv(job, argv); 1796 1797 /* 1798 * If we're using pipes to catch output, create the pipe by which we'll 1799 * get the shell's output. If we're using files, print out that we're 1800 * starting a job and then set up its temporary-file name. 1801 */ 1802 if (!compatMake || (job->flags & JOB_FIRST)) { 1803 if (usePipes) { 1804 int fd[2]; 1805 1806 if (pipe(fd) == -1) 1807 Punt("Cannot create pipe: %s", strerror(errno)); 1808 job->inPipe = fd[0]; 1809 job->outPipe = fd[1]; 1810 fcntl(job->inPipe, F_SETFD, 1); 1811 fcntl(job->outPipe, F_SETFD, 1); 1812 } else { 1813 fprintf(stdout, "Remaking `%s'\n", gn->name); 1814 fflush(stdout); 1815 snprintf(job->outFile, sizeof(job->outFile), "%s/%s", 1816 tdir, TMPPAT); 1817 if ((job->outFd = mkstemp(job->outFile)) == -1) 1818 Punt("cannot create temp file: %s", 1819 strerror(errno)); 1820 fcntl(job->outFd, F_SETFD, 1); 1821 } 1822 } 1823 1824 if (nJobs >= maxJobs && !(job->flags & JOB_SPECIAL) && maxJobs != 0) { 1825 /* 1826 * We've hit the limit of concurrency, so put the job on hold 1827 * until some other job finishes. Note that the special jobs 1828 * (.BEGIN, .INTERRUPT and .END) may be run even when the 1829 * limit has been reached (e.g. when maxJobs == 0). 1830 */ 1831 jobFull = TRUE; 1832 1833 DEBUGF(JOB, ("Can only run job locally.\n")); 1834 job->flags |= JOB_RESTART; 1835 TAILQ_INSERT_TAIL(&stoppedJobs, job, link); 1836 } else { 1837 if (nJobs >= maxJobs) { 1838 /* 1839 * If we're running this job as a special case 1840 * (see above), at least say the table is full. 1841 */ 1842 jobFull = TRUE; 1843 DEBUGF(JOB, ("Local job queue is full.\n")); 1844 } 1845 JobExec(job, argv); 1846 } 1847 return (JOB_RUNNING); 1848} 1849 1850static char * 1851JobOutput(Job *job, char *cp, char *endp, int msg) 1852{ 1853 char *ecp; 1854 1855 if (commandShell->noPrint) { 1856 ecp = strstr(cp, commandShell->noPrint); 1857 while (ecp != NULL) { 1858 if (cp != ecp) { 1859 *ecp = '\0'; 1860 if (msg && job->node != lastNode) { 1861 MESSAGE(stdout, job->node); 1862 lastNode = job->node; 1863 } 1864 /* 1865 * The only way there wouldn't be a newline 1866 * after this line is if it were the last in 1867 * the buffer. However, since the non-printable 1868 * comes after it, there must be a newline, so 1869 * we don't print one. 1870 */ 1871 fprintf(stdout, "%s", cp); 1872 fflush(stdout); 1873 } 1874 cp = ecp + strlen(commandShell->noPrint); 1875 if (cp != endp) { 1876 /* 1877 * Still more to print, look again after 1878 * skipping the whitespace following the 1879 * non-printable command.... 1880 */ 1881 cp++; 1882 while (*cp == ' ' || *cp == '\t' || 1883 *cp == '\n') { 1884 cp++; 1885 } 1886 ecp = strstr(cp, commandShell->noPrint); 1887 } else { 1888 return (cp); 1889 } 1890 } 1891 } 1892 return (cp); 1893} 1894 1895/** 1896 * JobDoOutput 1897 * This function is called at different times depending on 1898 * whether the user has specified that output is to be collected 1899 * via pipes or temporary files. In the former case, we are called 1900 * whenever there is something to read on the pipe. We collect more 1901 * output from the given job and store it in the job's outBuf. If 1902 * this makes up a line, we print it tagged by the job's identifier, 1903 * as necessary. 1904 * If output has been collected in a temporary file, we open the 1905 * file and read it line by line, transferring it to our own 1906 * output channel until the file is empty. At which point we 1907 * remove the temporary file. 1908 * In both cases, however, we keep our figurative eye out for the 1909 * 'noPrint' line for the shell from which the output came. If 1910 * we recognize a line, we don't print it. If the command is not 1911 * alone on the line (the character after it is not \0 or \n), we 1912 * do print whatever follows it. 1913 * 1914 * Side Effects: 1915 * curPos may be shifted as may the contents of outBuf. 1916 */ 1917static void 1918JobDoOutput(Job *job, Boolean finish) 1919{ 1920 Boolean gotNL = FALSE; /* true if got a newline */ 1921 Boolean fbuf; /* true if our buffer filled up */ 1922 int nr; /* number of bytes read */ 1923 int i; /* auxiliary index into outBuf */ 1924 int max; /* limit for i (end of current data) */ 1925 int nRead; /* (Temporary) number of bytes read */ 1926 FILE *oFILE; /* Stream pointer to shell's output file */ 1927 char inLine[132]; 1928 1929 if (usePipes) { 1930 /* 1931 * Read as many bytes as will fit in the buffer. 1932 */ 1933 end_loop: 1934 gotNL = FALSE; 1935 fbuf = FALSE; 1936 1937 nRead = read(job->inPipe, &job->outBuf[job->curPos], 1938 JOB_BUFSIZE - job->curPos); 1939 /* 1940 * Check for interrupt here too, because the above read may 1941 * block when the child process is stopped. In this case the 1942 * interrupt will unblock it (we don't use SA_RESTART). 1943 */ 1944 if (interrupted) 1945 JobPassSig(interrupted); 1946 1947 if (nRead < 0) { 1948 DEBUGF(JOB, ("JobDoOutput(piperead)")); 1949 nr = 0; 1950 } else { 1951 nr = nRead; 1952 } 1953 1954 /* 1955 * If we hit the end-of-file (the job is dead), we must flush 1956 * its remaining output, so pretend we read a newline if 1957 * there's any output remaining in the buffer. 1958 * Also clear the 'finish' flag so we stop looping. 1959 */ 1960 if (nr == 0 && job->curPos != 0) { 1961 job->outBuf[job->curPos] = '\n'; 1962 nr = 1; 1963 finish = FALSE; 1964 } else if (nr == 0) { 1965 finish = FALSE; 1966 } 1967 1968 /* 1969 * Look for the last newline in the bytes we just got. If there 1970 * is one, break out of the loop with 'i' as its index and 1971 * gotNL set TRUE. 1972 */ 1973 max = job->curPos + nr; 1974 for (i = job->curPos + nr - 1; i >= job->curPos; i--) { 1975 if (job->outBuf[i] == '\n') { 1976 gotNL = TRUE; 1977 break; 1978 } else if (job->outBuf[i] == '\0') { 1979 /* 1980 * Why? 1981 */ 1982 job->outBuf[i] = ' '; 1983 } 1984 } 1985 1986 if (!gotNL) { 1987 job->curPos += nr; 1988 if (job->curPos == JOB_BUFSIZE) { 1989 /* 1990 * If we've run out of buffer space, we have 1991 * no choice but to print the stuff. sigh. 1992 */ 1993 fbuf = TRUE; 1994 i = job->curPos; 1995 } 1996 } 1997 if (gotNL || fbuf) { 1998 /* 1999 * Need to send the output to the screen. Null terminate 2000 * it first, overwriting the newline character if there 2001 * was one. So long as the line isn't one we should 2002 * filter (according to the shell description), we print 2003 * the line, preceded by a target banner if this target 2004 * isn't the same as the one for which we last printed 2005 * something. The rest of the data in the buffer are 2006 * then shifted down to the start of the buffer and 2007 * curPos is set accordingly. 2008 */ 2009 job->outBuf[i] = '\0'; 2010 if (i >= job->curPos) { 2011 char *cp; 2012 2013 cp = JobOutput(job, job->outBuf, 2014 &job->outBuf[i], FALSE); 2015 2016 /* 2017 * There's still more in that buffer. This time, 2018 * though, we know there's no newline at the 2019 * end, so we add one of our own free will. 2020 */ 2021 if (*cp != '\0') { 2022 if (job->node != lastNode) { 2023 MESSAGE(stdout, job->node); 2024 lastNode = job->node; 2025 } 2026 fprintf(stdout, "%s%s", cp, 2027 gotNL ? "\n" : ""); 2028 fflush(stdout); 2029 } 2030 } 2031 if (i < max - 1) { 2032 /* shift the remaining characters down */ 2033 memcpy(job->outBuf, &job->outBuf[i + 1], 2034 max - (i + 1)); 2035 job->curPos = max - (i + 1); 2036 2037 } else { 2038 /* 2039 * We have written everything out, so we just 2040 * start over from the start of the buffer. 2041 * No copying. No nothing. 2042 */ 2043 job->curPos = 0; 2044 } 2045 } 2046 if (finish) { 2047 /* 2048 * If the finish flag is true, we must loop until we hit 2049 * end-of-file on the pipe. This is guaranteed to happen 2050 * eventually since the other end of the pipe is now 2051 * closed (we closed it explicitly and the child has 2052 * exited). When we do get an EOF, finish will be set 2053 * FALSE and we'll fall through and out. 2054 */ 2055 goto end_loop; 2056 } 2057 2058 } else { 2059 /* 2060 * We've been called to retrieve the output of the job from the 2061 * temporary file where it's been squirreled away. This consists 2062 * of opening the file, reading the output line by line, being 2063 * sure not to print the noPrint line for the shell we used, 2064 * then close and remove the temporary file. Very simple. 2065 * 2066 * Change to read in blocks and do FindSubString type things 2067 * as for pipes? That would allow for "@echo -n..." 2068 */ 2069 oFILE = fopen(job->outFile, "r"); 2070 if (oFILE != NULL) { 2071 fprintf(stdout, "Results of making %s:\n", 2072 job->node->name); 2073 fflush(stdout); 2074 2075 while (fgets(inLine, sizeof(inLine), oFILE) != NULL) { 2076 char *cp, *endp, *oendp; 2077 2078 cp = inLine; 2079 oendp = endp = inLine + strlen(inLine); 2080 if (endp[-1] == '\n') { 2081 *--endp = '\0'; 2082 } 2083 cp = JobOutput(job, inLine, endp, FALSE); 2084 2085 /* 2086 * There's still more in that buffer. This time, 2087 * though, we know there's no newline at the 2088 * end, so we add one of our own free will. 2089 */ 2090 fprintf(stdout, "%s", cp); 2091 fflush(stdout); 2092 if (endp != oendp) { 2093 fprintf(stdout, "\n"); 2094 fflush(stdout); 2095 } 2096 } 2097 fclose(oFILE); 2098 eunlink(job->outFile); 2099 } 2100 } 2101} 2102 2103/** 2104 * Job_CatchChildren 2105 * Handle the exit of a child. Called from Make_Make. 2106 * 2107 * Side Effects: 2108 * The job descriptor is removed from the list of children. 2109 * 2110 * Notes: 2111 * We do waits, blocking or not, according to the wisdom of our 2112 * caller, until there are no more children to report. For each 2113 * job, call JobFinish to finish things off. This will take care of 2114 * putting jobs on the stoppedJobs queue. 2115 */ 2116void 2117Job_CatchChildren(Boolean block) 2118{ 2119 pid_t pid; /* pid of dead child */ 2120 Job *job; /* job descriptor for dead child */ 2121 int status; /* Exit/termination status */ 2122 2123 /* 2124 * Don't even bother if we know there's no one around. 2125 */ 2126 if (nJobs == 0) { 2127 return; 2128 } 2129 2130 for (;;) { 2131 pid = waitpid(-1, &status, 2132 (block ? 0 : WNOHANG) | WUNTRACED); 2133 if (pid <= 0) 2134 break; 2135 2136 DEBUGF(JOB, ("Process %jd exited or stopped.\n", 2137 (intmax_t)pid)); 2138 2139 TAILQ_FOREACH(job, &jobs, link) { 2140 if (job->pid == pid) 2141 break; 2142 } 2143 2144 if (job == NULL) { 2145 if (WIFSIGNALED(status) && 2146 (WTERMSIG(status) == SIGCONT)) { 2147 TAILQ_FOREACH(job, &jobs, link) { 2148 if (job->pid == pid) 2149 break; 2150 } 2151 if (job == NULL) { 2152 Error("Resumed child (%jd) " 2153 "not in table", (intmax_t)pid); 2154 continue; 2155 } 2156 TAILQ_REMOVE(&stoppedJobs, job, link); 2157 } else { 2158 Error("Child (%jd) not in table?", 2159 (intmax_t)pid); 2160 continue; 2161 } 2162 } else { 2163 TAILQ_REMOVE(&jobs, job, link); 2164 nJobs -= 1; 2165 if (fifoFd >= 0 && maxJobs > 1) { 2166 write(fifoFd, "+", 1); 2167 maxJobs--; 2168 if (nJobs >= maxJobs) 2169 jobFull = TRUE; 2170 else 2171 jobFull = FALSE; 2172 } else { 2173 DEBUGF(JOB, ("Job queue is no longer full.\n")); 2174 jobFull = FALSE; 2175 } 2176 } 2177 2178 JobFinish(job, &status); 2179 } 2180 if (interrupted) 2181 JobPassSig(interrupted); 2182} 2183 2184/** 2185 * Job_CatchOutput 2186 * Catch the output from our children, if we're using 2187 * pipes do so. Otherwise just block time until we get a 2188 * signal(most likely a SIGCHLD) since there's no point in 2189 * just spinning when there's nothing to do and the reaping 2190 * of a child can wait for a while. 2191 * 2192 * Side Effects: 2193 * Output is read from pipes if we're piping. 2194 * ----------------------------------------------------------------------- 2195 */ 2196void 2197#ifdef USE_KQUEUE 2198Job_CatchOutput(int flag __unused) 2199#else 2200Job_CatchOutput(int flag) 2201#endif 2202{ 2203 int nfds; 2204#ifdef USE_KQUEUE 2205#define KEV_SIZE 4 2206 struct kevent kev[KEV_SIZE]; 2207 int i; 2208#else 2209 struct timeval timeout; 2210 fd_set readfds; 2211 Job *job; 2212#endif 2213 2214 fflush(stdout); 2215 2216 if (usePipes) { 2217#ifdef USE_KQUEUE 2218 if ((nfds = kevent(kqfd, NULL, 0, kev, KEV_SIZE, NULL)) == -1) { 2219 if (errno != EINTR) 2220 Punt("kevent: %s", strerror(errno)); 2221 if (interrupted) 2222 JobPassSig(interrupted); 2223 } else { 2224 for (i = 0; i < nfds; i++) { 2225 if (kev[i].flags & EV_ERROR) { 2226 warnc(kev[i].data, "kevent"); 2227 continue; 2228 } 2229 switch (kev[i].filter) { 2230 case EVFILT_READ: 2231 JobDoOutput(kev[i].udata, FALSE); 2232 break; 2233 case EVFILT_PROC: 2234 /* 2235 * Just wake up and let 2236 * Job_CatchChildren() collect the 2237 * terminated job. 2238 */ 2239 break; 2240 } 2241 } 2242 } 2243#else 2244 readfds = outputs; 2245 timeout.tv_sec = SEL_SEC; 2246 timeout.tv_usec = SEL_USEC; 2247 if (flag && jobFull && fifoFd >= 0) 2248 FD_SET(fifoFd, &readfds); 2249 2250 nfds = select(FD_SETSIZE, &readfds, (fd_set *)NULL, 2251 (fd_set *)NULL, &timeout); 2252 if (nfds <= 0) { 2253 if (interrupted) 2254 JobPassSig(interrupted); 2255 return; 2256 } 2257 if (fifoFd >= 0 && FD_ISSET(fifoFd, &readfds)) { 2258 if (--nfds <= 0) 2259 return; 2260 } 2261 job = TAILQ_FIRST(&jobs); 2262 while (nfds != 0 && job != NULL) { 2263 if (FD_ISSET(job->inPipe, &readfds)) { 2264 JobDoOutput(job, FALSE); 2265 nfds--; 2266 } 2267 job = TAILQ_NEXT(job, link); 2268 } 2269#endif /* !USE_KQUEUE */ 2270 } 2271} 2272 2273/** 2274 * Job_Make 2275 * Start the creation of a target. Basically a front-end for 2276 * JobStart used by the Make module. 2277 * 2278 * Side Effects: 2279 * Another job is started. 2280 */ 2281void 2282Job_Make(GNode *gn) 2283{ 2284 2285 JobStart(gn, 0, NULL); 2286} 2287 2288void 2289Job_SetPrefix(void) 2290{ 2291 2292 if (targPrefix) { 2293 free(targPrefix); 2294 } else if (!Var_Exists(MAKE_JOB_PREFIX, VAR_GLOBAL)) { 2295 Var_SetGlobal(MAKE_JOB_PREFIX, "---"); 2296 } 2297 targPrefix = Var_Subst("${" MAKE_JOB_PREFIX "}", VAR_GLOBAL, 0)->buf; 2298} 2299 2300/** 2301 * Job_Init 2302 * Initialize the process module, given a maximum number of jobs. 2303 * 2304 * Side Effects: 2305 * lists and counters are initialized 2306 */ 2307void 2308Job_Init(int maxproc) 2309{ 2310 GNode *begin; /* node for commands to do at the very start */ 2311 const char *env; 2312 struct sigaction sa; 2313 2314 fifoFd = -1; 2315 env = getenv("MAKE_JOBS_FIFO"); 2316 2317 if (env == NULL && maxproc > 1) { 2318 /* 2319 * We did not find the environment variable so we are the 2320 * leader. Create the fifo, open it, write one char per 2321 * allowed job into the pipe. 2322 */ 2323 fifoFd = mkfifotemp(fifoName); 2324 if (fifoFd < 0) { 2325 env = NULL; 2326 } else { 2327 fifoMaster = 1; 2328 fcntl(fifoFd, F_SETFL, O_NONBLOCK); 2329 env = fifoName; 2330 setenv("MAKE_JOBS_FIFO", env, 1); 2331 while (maxproc-- > 0) { 2332 write(fifoFd, "+", 1); 2333 } 2334 /* The master make does not get a magic token */ 2335 jobFull = TRUE; 2336 maxJobs = 0; 2337 } 2338 2339 } else if (env != NULL) { 2340 /* 2341 * We had the environment variable so we are a slave. 2342 * Open fifo and give ourselves a magic token which represents 2343 * the token our parent make has grabbed to start his make 2344 * process. Otherwise the sub-makes would gobble up tokens and 2345 * the proper number of tokens to specify to -j would depend 2346 * on the depth of the tree and the order of execution. 2347 */ 2348 fifoFd = open(env, O_RDWR, 0); 2349 if (fifoFd >= 0) { 2350 fcntl(fifoFd, F_SETFL, O_NONBLOCK); 2351 maxJobs = 1; 2352 jobFull = FALSE; 2353 } 2354 } 2355 if (fifoFd < 0) { 2356 maxJobs = maxproc; 2357 jobFull = FALSE; 2358 } else { 2359 } 2360 nJobs = 0; 2361 2362 aborting = 0; 2363 makeErrors = 0; 2364 2365 lastNode = NULL; 2366 if ((maxJobs == 1 && fifoFd < 0) || !beVerbose || is_posix || beQuiet) { 2367 /* 2368 * If only one job can run at a time, there's no need for a 2369 * banner, no is there? 2370 */ 2371 targFmt = ""; 2372 } else { 2373 targFmt = TARG_FMT; 2374 } 2375 2376 /* 2377 * Catch the four signals that POSIX specifies if they aren't ignored. 2378 * JobCatchSignal will just set global variables and hope someone 2379 * else is going to handle the interrupt. 2380 */ 2381 sa.sa_handler = JobCatchSig; 2382 sigemptyset(&sa.sa_mask); 2383 sa.sa_flags = 0; 2384 2385 if (signal(SIGINT, SIG_IGN) != SIG_IGN) { 2386 sigaction(SIGINT, &sa, NULL); 2387 } 2388 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) { 2389 sigaction(SIGHUP, &sa, NULL); 2390 } 2391 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) { 2392 sigaction(SIGQUIT, &sa, NULL); 2393 } 2394 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) { 2395 sigaction(SIGTERM, &sa, NULL); 2396 } 2397 /* 2398 * There are additional signals that need to be caught and passed if 2399 * either the export system wants to be told directly of signals or if 2400 * we're giving each job its own process group (since then it won't get 2401 * signals from the terminal driver as we own the terminal) 2402 */ 2403#if defined(USE_PGRP) 2404 if (signal(SIGTSTP, SIG_IGN) != SIG_IGN) { 2405 sigaction(SIGTSTP, &sa, NULL); 2406 } 2407 if (signal(SIGTTOU, SIG_IGN) != SIG_IGN) { 2408 sigaction(SIGTTOU, &sa, NULL); 2409 } 2410 if (signal(SIGTTIN, SIG_IGN) != SIG_IGN) { 2411 sigaction(SIGTTIN, &sa, NULL); 2412 } 2413 if (signal(SIGWINCH, SIG_IGN) != SIG_IGN) { 2414 sigaction(SIGWINCH, &sa, NULL); 2415 } 2416#endif 2417 2418#ifdef USE_KQUEUE 2419 if ((kqfd = kqueue()) == -1) { 2420 Punt("kqueue: %s", strerror(errno)); 2421 } 2422#endif 2423 2424 begin = Targ_FindNode(".BEGIN", TARG_NOCREATE); 2425 2426 if (begin != NULL) { 2427 JobStart(begin, JOB_SPECIAL, (Job *)NULL); 2428 while (nJobs) { 2429 Job_CatchOutput(0); 2430 Job_CatchChildren(!usePipes); 2431 } 2432 } 2433 postCommands = Targ_FindNode(".END", TARG_CREATE); 2434} 2435 2436/** 2437 * Job_Full 2438 * See if the job table is full. It is considered full if it is OR 2439 * if we are in the process of aborting OR if we have 2440 * reached/exceeded our local quota. This prevents any more jobs 2441 * from starting up. 2442 * 2443 * Results: 2444 * TRUE if the job table is full, FALSE otherwise 2445 */ 2446Boolean 2447Job_Full(void) 2448{ 2449 char c; 2450 int i; 2451 2452 if (aborting) 2453 return (aborting); 2454 if (fifoFd >= 0 && jobFull) { 2455 i = read(fifoFd, &c, 1); 2456 if (i > 0) { 2457 maxJobs++; 2458 jobFull = FALSE; 2459 } 2460 } 2461 return (jobFull); 2462} 2463 2464/** 2465 * Job_Empty 2466 * See if the job table is empty. Because the local concurrency may 2467 * be set to 0, it is possible for the job table to become empty, 2468 * while the list of stoppedJobs remains non-empty. In such a case, 2469 * we want to restart as many jobs as we can. 2470 * 2471 * Results: 2472 * TRUE if it is. FALSE if it ain't. 2473 */ 2474Boolean 2475Job_Empty(void) 2476{ 2477 if (nJobs == 0) { 2478 if (!TAILQ_EMPTY(&stoppedJobs) && !aborting) { 2479 /* 2480 * The job table is obviously not full if it has no 2481 * jobs in it...Try and restart the stopped jobs. 2482 */ 2483 jobFull = FALSE; 2484 JobRestartJobs(); 2485 return (FALSE); 2486 } else { 2487 return (TRUE); 2488 } 2489 } else { 2490 return (FALSE); 2491 } 2492} 2493 2494/** 2495 * JobInterrupt 2496 * Handle the receipt of an interrupt. 2497 * 2498 * Side Effects: 2499 * All children are killed. Another job will be started if the 2500 * .INTERRUPT target was given. 2501 */ 2502static void 2503JobInterrupt(int runINTERRUPT, int signo) 2504{ 2505 Job *job; /* job descriptor in that element */ 2506 GNode *interrupt; /* the node describing the .INTERRUPT target */ 2507 2508 aborting = ABORT_INTERRUPT; 2509 2510 TAILQ_FOREACH(job, &jobs, link) { 2511 if (!Targ_Precious(job->node)) { 2512 char *file = (job->node->path == NULL ? 2513 job->node->name : job->node->path); 2514 2515 if (!noExecute && eunlink(file) != -1) { 2516 Error("*** %s removed", file); 2517 } 2518 } 2519 if (job->pid) { 2520 DEBUGF(JOB, ("JobInterrupt passing signal to child " 2521 "%jd.\n", (intmax_t)job->pid)); 2522 KILL(job->pid, signo); 2523 } 2524 } 2525 2526 if (runINTERRUPT && !touchFlag) { 2527 /* 2528 * clear the interrupted flag because we would get an 2529 * infinite loop otherwise. 2530 */ 2531 interrupted = 0; 2532 2533 interrupt = Targ_FindNode(".INTERRUPT", TARG_NOCREATE); 2534 if (interrupt != NULL) { 2535 ignoreErrors = FALSE; 2536 2537 JobStart(interrupt, JOB_IGNDOTS, (Job *)NULL); 2538 while (nJobs) { 2539 Job_CatchOutput(0); 2540 Job_CatchChildren(!usePipes); 2541 } 2542 } 2543 } 2544 if (fifoMaster) 2545 unlink(fifoName); 2546} 2547 2548/** 2549 * Job_Finish 2550 * Do final processing such as the running of the commands 2551 * attached to the .END target. 2552 * 2553 * Results: 2554 * None. 2555 */ 2556void 2557Job_Finish(void) 2558{ 2559 2560 if (postCommands != NULL && !Lst_IsEmpty(&postCommands->commands)) { 2561 if (makeErrors) { 2562 Error("Errors reported so .END ignored"); 2563 } else { 2564 JobStart(postCommands, JOB_SPECIAL | JOB_IGNDOTS, NULL); 2565 2566 while (nJobs) { 2567 Job_CatchOutput(0); 2568 Job_CatchChildren(!usePipes); 2569 } 2570 } 2571 } 2572 if (fifoFd >= 0) { 2573 close(fifoFd); 2574 fifoFd = -1; 2575 if (fifoMaster) 2576 unlink(fifoName); 2577 } 2578} 2579 2580/** 2581 * Job_Wait 2582 * Waits for all running jobs to finish and returns. Sets 'aborting' 2583 * to ABORT_WAIT to prevent other jobs from starting. 2584 * 2585 * Side Effects: 2586 * Currently running jobs finish. 2587 */ 2588void 2589Job_Wait(void) 2590{ 2591 2592 aborting = ABORT_WAIT; 2593 while (nJobs != 0) { 2594 Job_CatchOutput(0); 2595 Job_CatchChildren(!usePipes); 2596 } 2597 aborting = 0; 2598} 2599 2600/** 2601 * Job_AbortAll 2602 * Abort all currently running jobs without handling output or anything. 2603 * This function is to be called only in the event of a major 2604 * error. Most definitely NOT to be called from JobInterrupt. 2605 * 2606 * Side Effects: 2607 * All children are killed, not just the firstborn 2608 */ 2609void 2610Job_AbortAll(void) 2611{ 2612 Job *job; /* the job descriptor in that element */ 2613 int foo; 2614 2615 aborting = ABORT_ERROR; 2616 2617 if (nJobs) { 2618 TAILQ_FOREACH(job, &jobs, link) { 2619 /* 2620 * kill the child process with increasingly drastic 2621 * signals to make darn sure it's dead. 2622 */ 2623 KILL(job->pid, SIGINT); 2624 KILL(job->pid, SIGKILL); 2625 } 2626 } 2627 2628 /* 2629 * Catch as many children as want to report in at first, then give up 2630 */ 2631 while (waitpid(-1, &foo, WNOHANG) > 0) 2632 ; 2633} 2634 2635/** 2636 * JobRestartJobs 2637 * Tries to restart stopped jobs if there are slots available. 2638 * Note that this tries to restart them regardless of pending errors. 2639 * It's not good to leave stopped jobs lying around! 2640 * 2641 * Side Effects: 2642 * Resumes(and possibly migrates) jobs. 2643 */ 2644static void 2645JobRestartJobs(void) 2646{ 2647 Job *job; 2648 2649 while (!jobFull && (job = TAILQ_FIRST(&stoppedJobs)) != NULL) { 2650 DEBUGF(JOB, ("Job queue is not full. " 2651 "Restarting a stopped job.\n")); 2652 TAILQ_REMOVE(&stoppedJobs, job, link); 2653 JobRestart(job); 2654 } 2655} 2656 2657/** 2658 * Cmd_Exec 2659 * Execute the command in cmd, and return the output of that command 2660 * in a string. 2661 * 2662 * Results: 2663 * A string containing the output of the command, or the empty string 2664 * If error is not NULL, it contains the reason for the command failure 2665 * Any output sent to stderr in the child process is passed to stderr, 2666 * and not captured in the string. 2667 * 2668 * Side Effects: 2669 * The string must be freed by the caller. 2670 */ 2671Buffer * 2672Cmd_Exec(const char *cmd, const char **error) 2673{ 2674 int fds[2]; /* Pipe streams */ 2675 int status; /* command exit status */ 2676 Buffer *buf; /* buffer to store the result */ 2677 ssize_t rcnt; 2678 ProcStuff ps; 2679 2680 *error = NULL; 2681 buf = Buf_Init(0); 2682 2683 /* 2684 * Open a pipe for fetching its output 2685 */ 2686 if (pipe(fds) == -1) { 2687 *error = "Couldn't create pipe for \"%s\""; 2688 return (buf); 2689 } 2690 2691 /* Set close-on-exec on read side of pipe. */ 2692 fcntl(fds[0], F_SETFD, fcntl(fds[0], F_GETFD) | FD_CLOEXEC); 2693 2694 ps.in = STDIN_FILENO; 2695 ps.out = fds[1]; 2696 ps.err = STDERR_FILENO; 2697 2698 ps.merge_errors = 0; 2699 ps.pgroup = 0; 2700 ps.searchpath = 0; 2701 2702 /* Set up arguments for shell */ 2703 ps.argv = emalloc(4 * sizeof(char *)); 2704 ps.argv[0] = strdup(commandShell->name); 2705 ps.argv[1] = strdup("-c"); 2706 ps.argv[2] = strdup(cmd); 2707 ps.argv[3] = NULL; 2708 ps.argv_free = 1; 2709 2710 /* 2711 * Fork. Warning since we are doing vfork() instead of fork(), 2712 * do not allocate memory in the child process! 2713 */ 2714 if ((ps.child_pid = vfork()) == -1) { 2715 *error = "Couldn't exec \"%s\""; 2716 return (buf); 2717 2718 } else if (ps.child_pid == 0) { 2719 /* 2720 * Child 2721 */ 2722 Proc_Exec(&ps); 2723 /* NOTREACHED */ 2724 } 2725 2726 free(ps.argv[2]); 2727 free(ps.argv[1]); 2728 free(ps.argv[0]); 2729 free(ps.argv); 2730 2731 close(fds[1]); /* No need for the writing half of the pipe. */ 2732 2733 do { 2734 char result[BUFSIZ]; 2735 2736 rcnt = read(fds[0], result, sizeof(result)); 2737 if (rcnt != -1) 2738 Buf_AddBytes(buf, (size_t)rcnt, (Byte *)result); 2739 } while (rcnt > 0 || (rcnt == -1 && errno == EINTR)); 2740 2741 if (rcnt == -1) 2742 *error = "Error reading shell's output for \"%s\""; 2743 2744 /* 2745 * Close the input side of the pipe. 2746 */ 2747 close(fds[0]); 2748 2749 status = ProcWait(&ps); 2750 2751 if (status) 2752 *error = "\"%s\" returned non-zero status"; 2753 2754 Buf_StripNewlines(buf); 2755 2756 return (buf); 2757} 2758 2759 2760/* 2761 * Interrupt handler - set flag and defer handling to the main code 2762 */ 2763static void 2764CompatCatchSig(int signo) 2765{ 2766 2767 interrupted = signo; 2768} 2769 2770/*- 2771 *----------------------------------------------------------------------- 2772 * CompatInterrupt -- 2773 * Interrupt the creation of the current target and remove it if 2774 * it ain't precious. 2775 * 2776 * Results: 2777 * None. 2778 * 2779 * Side Effects: 2780 * The target is removed and the process exits. If .INTERRUPT exists, 2781 * its commands are run first WITH INTERRUPTS IGNORED.. 2782 * 2783 *----------------------------------------------------------------------- 2784 */ 2785static void 2786CompatInterrupt(int signo) 2787{ 2788 GNode *gn; 2789 sigset_t nmask, omask; 2790 LstNode *ln; 2791 2792 sigemptyset(&nmask); 2793 sigaddset(&nmask, SIGINT); 2794 sigaddset(&nmask, SIGTERM); 2795 sigaddset(&nmask, SIGHUP); 2796 sigaddset(&nmask, SIGQUIT); 2797 sigprocmask(SIG_SETMASK, &nmask, &omask); 2798 2799 /* prevent recursion in evaluation of .INTERRUPT */ 2800 interrupted = 0; 2801 2802 if (curTarg != NULL && !Targ_Precious(curTarg)) { 2803 const char *file = Var_Value(TARGET, curTarg); 2804 2805 if (!noExecute && eunlink(file) != -1) { 2806 printf("*** %s removed\n", file); 2807 } 2808 } 2809 2810 /* 2811 * Run .INTERRUPT only if hit with interrupt signal 2812 */ 2813 if (signo == SIGINT) { 2814 gn = Targ_FindNode(".INTERRUPT", TARG_NOCREATE); 2815 if (gn != NULL) { 2816 LST_FOREACH(ln, &gn->commands) { 2817 if (Compat_RunCommand(ln, gn)) 2818 break; 2819 } 2820 } 2821 } 2822 2823 sigprocmask(SIG_SETMASK, &omask, NULL); 2824 2825 if (signo == SIGQUIT) 2826 exit(signo); 2827 signal(signo, SIG_DFL); 2828 kill(getpid(), signo); 2829} 2830 2831/** 2832 * shellneed 2833 * 2834 * Results: 2835 * Returns NULL if a specified line must be executed by the shell, 2836 * and an argument vector if it can be run via execvp(). 2837 * 2838 * Side Effects: 2839 * Uses brk_string so destroys the contents of argv. 2840 */ 2841static char ** 2842shellneed(ArgArray *aa, char *cmd) 2843{ 2844 char **p; 2845 int ret; 2846 2847 if (commandShell->meta == NULL || commandShell->builtins.argc <= 1) 2848 /* use shell */ 2849 return (NULL); 2850 2851 if (strpbrk(cmd, commandShell->meta) != NULL) 2852 return (NULL); 2853 2854 /* 2855 * Break the command into words to form an argument 2856 * vector we can execute. 2857 */ 2858 brk_string(aa, cmd, TRUE); 2859 for (p = commandShell->builtins.argv + 1; *p != 0; p++) { 2860 if ((ret = strcmp(aa->argv[1], *p)) == 0) { 2861 /* found - use shell */ 2862 ArgArray_Done(aa); 2863 return (NULL); 2864 } 2865 if (ret < 0) { 2866 /* not found */ 2867 break; 2868 } 2869 } 2870 return (aa->argv + 1); 2871} 2872 2873/** 2874 * Execute the next command for a target. If the command returns an 2875 * error, the node's made field is set to ERROR and creation stops. 2876 * The node from which the command came is also given. This is used 2877 * to execute the commands in compat mode and when executing commands 2878 * with the '+' flag in non-compat mode. In these modes each command 2879 * line should be executed by its own shell. We do some optimisation here: 2880 * if the shell description defines both a string of meta characters and 2881 * a list of builtins and the command line neither contains a meta character 2882 * nor starts with one of the builtins then we execute the command directly 2883 * without invoking a shell. 2884 * 2885 * Results: 2886 * 0 if the command succeeded, 1 if an error occurred. 2887 * 2888 * Side Effects: 2889 * The node's 'made' field may be set to ERROR. 2890 */ 2891static int 2892Compat_RunCommand(LstNode *cmdNode, GNode *gn) 2893{ 2894 ArgArray aa; 2895 char *cmd; /* Expanded command */ 2896 Boolean silent; /* Don't print command */ 2897 Boolean doit; /* Execute even in -n */ 2898 Boolean errCheck; /* Check errors */ 2899 int reason; /* Reason for child's death */ 2900 int status; /* Description of child's death */ 2901 char **av; /* Argument vector for thing to exec */ 2902 ProcStuff ps; 2903 2904 silent = gn->type & OP_SILENT; 2905 errCheck = !(gn->type & OP_IGNORE); 2906 doit = FALSE; 2907 2908 cmd = Buf_Peel(Var_Subst(Lst_Datum(cmdNode), gn, FALSE)); 2909 if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) { 2910 Lst_AtEnd(&ENDNode->commands, cmd); 2911 return (0); 2912 } else if (strcmp(cmd, "...") == 0) { 2913 free(cmd); 2914 gn->type |= OP_SAVE_CMDS; 2915 return (0); 2916 } 2917 Lst_Replace(cmdNode, cmd); 2918 2919 while (*cmd == '@' || *cmd == '-' || *cmd == '+') { 2920 switch (*cmd) { 2921 2922 case '@': 2923 silent = DEBUG(LOUD) ? FALSE : TRUE; 2924 break; 2925 2926 case '-': 2927 errCheck = FALSE; 2928 break; 2929 2930 case '+': 2931 doit = TRUE; 2932 break; 2933 } 2934 cmd++; 2935 } 2936 2937 while (isspace((unsigned char)*cmd)) 2938 cmd++; 2939 2940 /* 2941 * Ignore empty commands 2942 */ 2943 if (*cmd == '\0') { 2944 return (0); 2945 } 2946 2947 /* 2948 * Print the command before echoing if we're not supposed to be quiet 2949 * for this one. We also print the command if -n given, but not if '+'. 2950 */ 2951 if (!silent || (noExecute && !doit)) { 2952 printf("%s\n", cmd); 2953 fflush(stdout); 2954 } 2955 2956 /* 2957 * If we're not supposed to execute any commands, this is as far as 2958 * we go... 2959 */ 2960 if (!doit && noExecute) { 2961 return (0); 2962 } 2963 2964 ps.in = STDIN_FILENO; 2965 ps.out = STDOUT_FILENO; 2966 ps.err = STDERR_FILENO; 2967 2968 ps.merge_errors = 0; 2969 ps.pgroup = 0; 2970 ps.searchpath = 1; 2971 2972 if ((av = shellneed(&aa, cmd)) == NULL) { 2973 /* 2974 * Shell meta character or shell builtin found - pass 2975 * command to shell. We give the shell the -e flag as 2976 * well as -c if it is supposed to exit when it hits an error. 2977 */ 2978 ps.argv = emalloc(4 * sizeof(char *)); 2979 ps.argv[0] = strdup(commandShell->path); 2980 ps.argv[1] = strdup(errCheck ? "-ec" : "-c"); 2981 ps.argv[2] = strdup(cmd); 2982 ps.argv[3] = NULL; 2983 ps.argv_free = 1; 2984 } else { 2985 ps.argv = av; 2986 ps.argv_free = 0; 2987 } 2988 ps.errCheck = errCheck; 2989 2990 /* 2991 * Warning since we are doing vfork() instead of fork(), 2992 * do not allocate memory in the child process! 2993 */ 2994 if ((ps.child_pid = vfork()) == -1) { 2995 Fatal("Could not fork"); 2996 2997 } else if (ps.child_pid == 0) { 2998 /* 2999 * Child 3000 */ 3001 Proc_Exec(&ps); 3002 /* NOTREACHED */ 3003 3004 } else { 3005 if (ps.argv_free) { 3006 free(ps.argv[2]); 3007 free(ps.argv[1]); 3008 free(ps.argv[0]); 3009 free(ps.argv); 3010 } else { 3011 ArgArray_Done(&aa); 3012 } 3013 3014 /* 3015 * we need to print out the command associated with this 3016 * Gnode in Targ_PrintCmd from Targ_PrintGraph when debugging 3017 * at level g2, in main(), Fatal() and DieHorribly(), 3018 * therefore do not free it when debugging. 3019 */ 3020 if (!DEBUG(GRAPH2)) { 3021 free(Lst_Datum(cmdNode)); 3022 Lst_Replace(cmdNode, NULL); 3023 } 3024 3025 /* 3026 * The child is off and running. Now all we can do is wait... 3027 */ 3028 reason = ProcWait(&ps); 3029 3030 if (interrupted) 3031 CompatInterrupt(interrupted); 3032 3033 /* 3034 * Decode and report the reason child exited, then 3035 * indicate how we handled it. 3036 */ 3037 if (WIFEXITED(reason)) { 3038 status = WEXITSTATUS(reason); 3039 if (status == 0) { 3040 return (0); 3041 } else { 3042 printf("*** [%s] Error code %d", 3043 gn->name, status); 3044 } 3045 } else if (WIFSTOPPED(reason)) { 3046 status = WSTOPSIG(reason); 3047 } else { 3048 status = WTERMSIG(reason); 3049 printf("*** [%s] Signal %d", 3050 gn->name, status); 3051 } 3052 3053 if (ps.errCheck) { 3054 gn->made = ERROR; 3055 if (keepgoing) { 3056 /* 3057 * Abort the current 3058 * target, but let 3059 * others continue. 3060 */ 3061 printf(" (continuing)\n"); 3062 } 3063 return (status); 3064 } else { 3065 /* 3066 * Continue executing 3067 * commands for this target. 3068 * If we return 0, this will 3069 * happen... 3070 */ 3071 printf(" (ignored)\n"); 3072 return (0); 3073 } 3074 } 3075} 3076 3077/*- 3078 *----------------------------------------------------------------------- 3079 * Compat_Make -- 3080 * Make a target, given the parent, to abort if necessary. 3081 * 3082 * Side Effects: 3083 * If an error is detected and not being ignored, the process exits. 3084 * 3085 *----------------------------------------------------------------------- 3086 */ 3087int 3088Compat_Make(GNode *gn, GNode *pgn) 3089{ 3090 LstNode *ln; 3091 3092 if (gn->type & OP_USE) { 3093 Make_HandleUse(gn, pgn); 3094 3095 } else if (gn->made == UNMADE) { 3096 /* 3097 * First mark ourselves to be made, then apply whatever 3098 * transformations the suffix module thinks are necessary. 3099 * Once that's done, we can descend and make all our children. 3100 * If any of them has an error but the -k flag was given, our 3101 * 'make' field will be set FALSE again. This is our signal to 3102 * not attempt to do anything but abort our parent as well. 3103 */ 3104 gn->make = TRUE; 3105 gn->made = BEINGMADE; 3106 Suff_FindDeps(gn); 3107 LST_FOREACH(ln, &gn->children) 3108 Compat_Make(Lst_Datum(ln), gn); 3109 if (!gn->make) { 3110 gn->made = ABORTED; 3111 pgn->make = FALSE; 3112 return (0); 3113 } 3114 3115 if (Lst_Member(&gn->iParents, pgn) != NULL) { 3116 Var_Set(IMPSRC, Var_Value(TARGET, gn), pgn); 3117 } 3118 3119 /* 3120 * All the children were made ok. Now cmtime contains the 3121 * modification time of the newest child, we need to find out 3122 * if we exist and when we were modified last. The criteria for 3123 * datedness are defined by the Make_OODate function. 3124 */ 3125 DEBUGF(MAKE, ("Examining %s...", gn->name)); 3126 if (!Make_OODate(gn)) { 3127 gn->made = UPTODATE; 3128 DEBUGF(MAKE, ("up-to-date.\n")); 3129 return (0); 3130 } else { 3131 DEBUGF(MAKE, ("out-of-date.\n")); 3132 } 3133 3134 /* 3135 * If the user is just seeing if something is out-of-date, 3136 * exit now to tell him/her "yes". 3137 */ 3138 if (queryFlag) { 3139 exit(1); 3140 } 3141 3142 /* 3143 * We need to be re-made. We also have to make sure we've got 3144 * a $? variable. To be nice, we also define the $> variable 3145 * using Make_DoAllVar(). 3146 */ 3147 Make_DoAllVar(gn); 3148 3149 /* 3150 * Alter our type to tell if errors should be ignored or things 3151 * should not be printed so Compat_RunCommand knows what to do. 3152 */ 3153 if (Targ_Ignore(gn)) { 3154 gn->type |= OP_IGNORE; 3155 } 3156 if (Targ_Silent(gn)) { 3157 gn->type |= OP_SILENT; 3158 } 3159 3160 if (Job_CheckCommands(gn, Fatal)) { 3161 /* 3162 * Our commands are ok, but we still have to worry 3163 * about the -t flag... 3164 */ 3165 if (!touchFlag) { 3166 curTarg = gn; 3167 LST_FOREACH(ln, &gn->commands) { 3168 if (Compat_RunCommand(ln, gn)) 3169 break; 3170 } 3171 curTarg = NULL; 3172 } else { 3173 Job_Touch(gn, gn->type & OP_SILENT); 3174 } 3175 } else { 3176 gn->made = ERROR; 3177 } 3178 3179 if (gn->made != ERROR) { 3180 /* 3181 * If the node was made successfully, mark it so, update 3182 * its modification time and timestamp all its parents. 3183 * Note that for .ZEROTIME targets, the timestamping 3184 * isn't done. This is to keep its state from affecting 3185 * that of its parent. 3186 */ 3187 gn->made = MADE; 3188#ifndef RECHECK 3189 /* 3190 * We can't re-stat the thing, but we can at least take 3191 * care of rules where a target depends on a source that 3192 * actually creates the target, but only if it has 3193 * changed, e.g. 3194 * 3195 * parse.h : parse.o 3196 * 3197 * parse.o : parse.y 3198 * yacc -d parse.y 3199 * cc -c y.tab.c 3200 * mv y.tab.o parse.o 3201 * cmp -s y.tab.h parse.h || mv y.tab.h parse.h 3202 * 3203 * In this case, if the definitions produced by yacc 3204 * haven't changed from before, parse.h won't have been 3205 * updated and gn->mtime will reflect the current 3206 * modification time for parse.h. This is something of a 3207 * kludge, I admit, but it's a useful one.. 3208 * 3209 * XXX: People like to use a rule like 3210 * 3211 * FRC: 3212 * 3213 * To force things that depend on FRC to be made, so we 3214 * have to check for gn->children being empty as well... 3215 */ 3216 if (!Lst_IsEmpty(&gn->commands) || 3217 Lst_IsEmpty(&gn->children)) { 3218 gn->mtime = now; 3219 } 3220#else 3221 /* 3222 * This is what Make does and it's actually a good 3223 * thing, as it allows rules like 3224 * 3225 * cmp -s y.tab.h parse.h || cp y.tab.h parse.h 3226 * 3227 * to function as intended. Unfortunately, thanks to 3228 * the stateless nature of NFS (and the speed of this 3229 * program), there are times when the modification time 3230 * of a file created on a remote machine will not be 3231 * modified before the stat() implied by the Dir_MTime 3232 * occurs, thus leading us to believe that the file 3233 * is unchanged, wreaking havoc with files that depend 3234 * on this one. 3235 * 3236 * I have decided it is better to make too much than to 3237 * make too little, so this stuff is commented out 3238 * unless you're sure it's ok. 3239 * -- ardeb 1/12/88 3240 */ 3241 if (noExecute || Dir_MTime(gn) == 0) { 3242 gn->mtime = now; 3243 } 3244 if (gn->cmtime > gn->mtime) 3245 gn->mtime = gn->cmtime; 3246 DEBUGF(MAKE, ("update time: %s\n", 3247 Targ_FmtTime(gn->mtime))); 3248#endif 3249 if (!(gn->type & OP_EXEC)) { 3250 pgn->childMade = TRUE; 3251 Make_TimeStamp(pgn, gn); 3252 } 3253 3254 } else if (keepgoing) { 3255 pgn->make = FALSE; 3256 3257 } else { 3258 printf("\n\nStop in %s.\n", Var_Value(".CURDIR", gn)); 3259 exit(1); 3260 } 3261 } else if (gn->made == ERROR) { 3262 /* 3263 * Already had an error when making this beastie. Tell the 3264 * parent to abort. 3265 */ 3266 pgn->make = FALSE; 3267 } else { 3268 if (Lst_Member(&gn->iParents, pgn) != NULL) { 3269 Var_Set(IMPSRC, Var_Value(TARGET, gn), pgn); 3270 } 3271 switch(gn->made) { 3272 case BEINGMADE: 3273 Error("Graph cycles through %s\n", gn->name); 3274 gn->made = ERROR; 3275 pgn->make = FALSE; 3276 break; 3277 case MADE: 3278 if ((gn->type & OP_EXEC) == 0) { 3279 pgn->childMade = TRUE; 3280 Make_TimeStamp(pgn, gn); 3281 } 3282 break; 3283 case UPTODATE: 3284 if ((gn->type & OP_EXEC) == 0) { 3285 Make_TimeStamp(pgn, gn); 3286 } 3287 break; 3288 default: 3289 break; 3290 } 3291 } 3292 3293 return (0); 3294} 3295 3296/*- 3297 * Install signal handlers for Compat_Run 3298 */ 3299void 3300Compat_InstallSignalHandlers(void) 3301{ 3302 3303 if (signal(SIGINT, SIG_IGN) != SIG_IGN) { 3304 signal(SIGINT, CompatCatchSig); 3305 } 3306 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) { 3307 signal(SIGTERM, CompatCatchSig); 3308 } 3309 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) { 3310 signal(SIGHUP, CompatCatchSig); 3311 } 3312 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) { 3313 signal(SIGQUIT, CompatCatchSig); 3314 } 3315} 3316 3317/*- 3318 *----------------------------------------------------------------------- 3319 * Compat_Run -- 3320 * Start making again, given a list of target nodes. 3321 * 3322 * Results: 3323 * None. 3324 * 3325 * Side Effects: 3326 * Guess what? 3327 * 3328 *----------------------------------------------------------------------- 3329 */ 3330void 3331Compat_Run(Lst *targs) 3332{ 3333 GNode *gn = NULL; /* Current root target */ 3334 LstNode *ln; 3335 3336 Compat_InstallSignalHandlers(); 3337 ENDNode = Targ_FindNode(".END", TARG_CREATE); 3338 /* 3339 * If the user has defined a .BEGIN target, execute the commands 3340 * attached to it. 3341 */ 3342 if (!queryFlag) { 3343 gn = Targ_FindNode(".BEGIN", TARG_NOCREATE); 3344 if (gn != NULL) { 3345 LST_FOREACH(ln, &gn->commands) { 3346 if (Compat_RunCommand(ln, gn)) 3347 break; 3348 } 3349 if (gn->made == ERROR) { 3350 printf("\n\nStop.\n"); 3351 exit(1); 3352 } 3353 } 3354 } 3355 3356 /* 3357 * For each entry in the list of targets to create, call Compat_Make on 3358 * it to create the thing. Compat_Make will leave the 'made' field of gn 3359 * in one of several states: 3360 * UPTODATE gn was already up-to-date 3361 * MADE gn was recreated successfully 3362 * ERROR An error occurred while gn was being created 3363 * ABORTED gn was not remade because one of its inferiors 3364 * could not be made due to errors. 3365 */ 3366 makeErrors = 0; 3367 while (!Lst_IsEmpty(targs)) { 3368 gn = Lst_DeQueue(targs); 3369 Compat_Make(gn, gn); 3370 3371 if (gn->made == UPTODATE) { 3372 printf("`%s' is up to date.\n", gn->name); 3373 } else if (gn->made == ABORTED) { 3374 printf("`%s' not remade because of errors.\n", 3375 gn->name); 3376 makeErrors++; 3377 } else if (gn->made == ERROR) { 3378 makeErrors++; 3379 } 3380 } 3381 3382 /* 3383 * If the user has defined a .END target, run its commands. 3384 */ 3385 if (makeErrors == 0) { 3386 LST_FOREACH(ln, &ENDNode->commands) { 3387 if (Compat_RunCommand(ln, ENDNode)) 3388 break; 3389 } 3390 } 3391} 3392