1331246Ssjg/* $NetBSD: job.c,v 1.192 2018/02/08 09:05:21 dholland Exp $ */ 2236769Sobrien 3236769Sobrien/* 4236769Sobrien * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. 5236769Sobrien * All rights reserved. 6236769Sobrien * 7236769Sobrien * This code is derived from software contributed to Berkeley by 8236769Sobrien * Adam de Boor. 9236769Sobrien * 10236769Sobrien * Redistribution and use in source and binary forms, with or without 11236769Sobrien * modification, are permitted provided that the following conditions 12236769Sobrien * are met: 13236769Sobrien * 1. Redistributions of source code must retain the above copyright 14236769Sobrien * notice, this list of conditions and the following disclaimer. 15236769Sobrien * 2. Redistributions in binary form must reproduce the above copyright 16236769Sobrien * notice, this list of conditions and the following disclaimer in the 17236769Sobrien * documentation and/or other materials provided with the distribution. 18236769Sobrien * 3. Neither the name of the University nor the names of its contributors 19236769Sobrien * may be used to endorse or promote products derived from this software 20236769Sobrien * without specific prior written permission. 21236769Sobrien * 22236769Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23236769Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24236769Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25236769Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26236769Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27236769Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28236769Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29236769Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30236769Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31236769Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32236769Sobrien * SUCH DAMAGE. 33236769Sobrien */ 34236769Sobrien 35236769Sobrien/* 36236769Sobrien * Copyright (c) 1988, 1989 by Adam de Boor 37236769Sobrien * Copyright (c) 1989 by Berkeley Softworks 38236769Sobrien * All rights reserved. 39236769Sobrien * 40236769Sobrien * This code is derived from software contributed to Berkeley by 41236769Sobrien * Adam de Boor. 42236769Sobrien * 43236769Sobrien * Redistribution and use in source and binary forms, with or without 44236769Sobrien * modification, are permitted provided that the following conditions 45236769Sobrien * are met: 46236769Sobrien * 1. Redistributions of source code must retain the above copyright 47236769Sobrien * notice, this list of conditions and the following disclaimer. 48236769Sobrien * 2. Redistributions in binary form must reproduce the above copyright 49236769Sobrien * notice, this list of conditions and the following disclaimer in the 50236769Sobrien * documentation and/or other materials provided with the distribution. 51236769Sobrien * 3. All advertising materials mentioning features or use of this software 52236769Sobrien * must display the following acknowledgement: 53236769Sobrien * This product includes software developed by the University of 54236769Sobrien * California, Berkeley and its contributors. 55236769Sobrien * 4. Neither the name of the University nor the names of its contributors 56236769Sobrien * may be used to endorse or promote products derived from this software 57236769Sobrien * without specific prior written permission. 58236769Sobrien * 59236769Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 60236769Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 61236769Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 62236769Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 63236769Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 64236769Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 65236769Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 66236769Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 67236769Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 68236769Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 69236769Sobrien * SUCH DAMAGE. 70236769Sobrien */ 71236769Sobrien 72236769Sobrien#ifndef MAKE_NATIVE 73331246Ssjgstatic char rcsid[] = "$NetBSD: job.c,v 1.192 2018/02/08 09:05:21 dholland Exp $"; 74236769Sobrien#else 75236769Sobrien#include <sys/cdefs.h> 76236769Sobrien#ifndef lint 77236769Sobrien#if 0 78236769Sobrienstatic char sccsid[] = "@(#)job.c 8.2 (Berkeley) 3/19/94"; 79236769Sobrien#else 80331246Ssjg__RCSID("$NetBSD: job.c,v 1.192 2018/02/08 09:05:21 dholland Exp $"); 81236769Sobrien#endif 82236769Sobrien#endif /* not lint */ 83236769Sobrien#endif 84236769Sobrien 85236769Sobrien/*- 86236769Sobrien * job.c -- 87236769Sobrien * handle the creation etc. of our child processes. 88236769Sobrien * 89236769Sobrien * Interface: 90236769Sobrien * Job_Make Start the creation of the given target. 91236769Sobrien * 92236769Sobrien * Job_CatchChildren Check for and handle the termination of any 93236769Sobrien * children. This must be called reasonably 94236769Sobrien * frequently to keep the whole make going at 95236769Sobrien * a decent clip, since job table entries aren't 96236769Sobrien * removed until their process is caught this way. 97236769Sobrien * 98236769Sobrien * Job_CatchOutput Print any output our children have produced. 99236769Sobrien * Should also be called fairly frequently to 100236769Sobrien * keep the user informed of what's going on. 101236769Sobrien * If no output is waiting, it will block for 102236769Sobrien * a time given by the SEL_* constants, below, 103236769Sobrien * or until output is ready. 104236769Sobrien * 105331246Ssjg * Job_Init Called to initialize this module. in addition, 106236769Sobrien * any commands attached to the .BEGIN target 107236769Sobrien * are executed before this function returns. 108236769Sobrien * Hence, the makefile must have been parsed 109236769Sobrien * before this function is called. 110236769Sobrien * 111236769Sobrien * Job_End Cleanup any memory used. 112236769Sobrien * 113236769Sobrien * Job_ParseShell Given the line following a .SHELL target, parse 114236769Sobrien * the line as a shell specification. Returns 115236769Sobrien * FAILURE if the spec was incorrect. 116236769Sobrien * 117236769Sobrien * Job_Finish Perform any final processing which needs doing. 118236769Sobrien * This includes the execution of any commands 119236769Sobrien * which have been/were attached to the .END 120236769Sobrien * target. It should only be called when the 121236769Sobrien * job table is empty. 122236769Sobrien * 123236769Sobrien * Job_AbortAll Abort all currently running jobs. It doesn't 124236769Sobrien * handle output or do anything for the jobs, 125236769Sobrien * just kills them. It should only be called in 126236769Sobrien * an emergency, as it were. 127236769Sobrien * 128236769Sobrien * Job_CheckCommands Verify that the commands for a target are 129236769Sobrien * ok. Provide them if necessary and possible. 130236769Sobrien * 131236769Sobrien * Job_Touch Update a target without really updating it. 132236769Sobrien * 133236769Sobrien * Job_Wait Wait for all currently-running jobs to finish. 134236769Sobrien */ 135236769Sobrien 136236769Sobrien#ifdef HAVE_CONFIG_H 137236769Sobrien# include "config.h" 138236769Sobrien#endif 139236769Sobrien#include <sys/types.h> 140236769Sobrien#include <sys/stat.h> 141236769Sobrien#include <sys/file.h> 142236769Sobrien#include <sys/time.h> 143236769Sobrien#include "wait.h" 144236769Sobrien 145249033Ssjg#include <assert.h> 146236769Sobrien#include <errno.h> 147236769Sobrien#if !defined(USE_SELECT) && defined(HAVE_POLL_H) 148236769Sobrien#include <poll.h> 149236769Sobrien#else 150236769Sobrien#ifndef USE_SELECT /* no poll.h */ 151236769Sobrien# define USE_SELECT 152236769Sobrien#endif 153236769Sobrien#if defined(HAVE_SYS_SELECT_H) 154236769Sobrien# include <sys/select.h> 155236769Sobrien#endif 156236769Sobrien#endif 157236769Sobrien#include <signal.h> 158236769Sobrien#include <stdio.h> 159236769Sobrien#include <string.h> 160236769Sobrien#include <utime.h> 161236769Sobrien#if defined(HAVE_SYS_SOCKET_H) 162236769Sobrien# include <sys/socket.h> 163236769Sobrien#endif 164236769Sobrien 165236769Sobrien#include "make.h" 166236769Sobrien#include "hash.h" 167236769Sobrien#include "dir.h" 168236769Sobrien#include "job.h" 169236769Sobrien#include "pathnames.h" 170236769Sobrien#include "trace.h" 171236769Sobrien# define STATIC static 172236769Sobrien 173236769Sobrien/* 174254419Ssjg * FreeBSD: traditionally .MAKE is not required to 175254419Ssjg * pass jobs queue to sub-makes. 176254419Ssjg * Use .MAKE.ALWAYS_PASS_JOB_QUEUE=no to disable. 177254419Ssjg */ 178254419Ssjg#define MAKE_ALWAYS_PASS_JOB_QUEUE ".MAKE.ALWAYS_PASS_JOB_QUEUE" 179254419Ssjgstatic int Always_pass_job_queue = TRUE; 180255285Ssjg/* 181255285Ssjg * FreeBSD: aborting entire parallel make isn't always 182255285Ssjg * desired. When doing tinderbox for example, failure of 183255285Ssjg * one architecture should not stop all. 184255285Ssjg * We still want to bail on interrupt though. 185255285Ssjg */ 186255285Ssjg#define MAKE_JOB_ERROR_TOKEN "MAKE_JOB_ERROR_TOKEN" 187255285Ssjgstatic int Job_error_token = TRUE; 188254419Ssjg 189254419Ssjg/* 190236769Sobrien * error handling variables 191236769Sobrien */ 192236769Sobrienstatic int errors = 0; /* number of errors reported */ 193236769Sobrienstatic int aborting = 0; /* why is the make aborting? */ 194236769Sobrien#define ABORT_ERROR 1 /* Because of an error */ 195236769Sobrien#define ABORT_INTERRUPT 2 /* Because it was interrupted */ 196236769Sobrien#define ABORT_WAIT 3 /* Waiting for jobs to finish */ 197236769Sobrien#define JOB_TOKENS "+EI+" /* Token to requeue for each abort state */ 198236769Sobrien 199236769Sobrien/* 200236769Sobrien * this tracks the number of tokens currently "out" to build jobs. 201236769Sobrien */ 202236769Sobrienint jobTokensRunning = 0; 203236769Sobrienint not_parallel = 0; /* set if .NOT_PARALLEL */ 204236769Sobrien 205236769Sobrien/* 206236769Sobrien * XXX: Avoid SunOS bug... FILENO() is fp->_file, and file 207236769Sobrien * is a char! So when we go above 127 we turn negative! 208236769Sobrien */ 209236769Sobrien#define FILENO(a) ((unsigned) fileno(a)) 210236769Sobrien 211236769Sobrien/* 212236769Sobrien * post-make command processing. The node postCommands is really just the 213236769Sobrien * .END target but we keep it around to avoid having to search for it 214236769Sobrien * all the time. 215236769Sobrien */ 216236769Sobrienstatic GNode *postCommands = NULL; 217236769Sobrien /* node containing commands to execute when 218236769Sobrien * everything else is done */ 219236769Sobrienstatic int numCommands; /* The number of commands actually printed 220236769Sobrien * for a target. Should this number be 221236769Sobrien * 0, no shell will be executed. */ 222236769Sobrien 223236769Sobrien/* 224236769Sobrien * Return values from JobStart. 225236769Sobrien */ 226236769Sobrien#define JOB_RUNNING 0 /* Job is running */ 227236769Sobrien#define JOB_ERROR 1 /* Error in starting the job */ 228236769Sobrien#define JOB_FINISHED 2 /* The job is already finished */ 229236769Sobrien 230236769Sobrien/* 231236769Sobrien * Descriptions for various shells. 232236769Sobrien * 233236769Sobrien * The build environment may set DEFSHELL_INDEX to one of 234236769Sobrien * DEFSHELL_INDEX_SH, DEFSHELL_INDEX_KSH, or DEFSHELL_INDEX_CSH, to 235236769Sobrien * select one of the prefedined shells as the default shell. 236236769Sobrien * 237236769Sobrien * Alternatively, the build environment may set DEFSHELL_CUSTOM to the 238236769Sobrien * name or the full path of a sh-compatible shell, which will be used as 239236769Sobrien * the default shell. 240236769Sobrien * 241236769Sobrien * ".SHELL" lines in Makefiles can choose the default shell from the 242236769Sobrien # set defined here, or add additional shells. 243236769Sobrien */ 244236769Sobrien 245236769Sobrien#ifdef DEFSHELL_CUSTOM 246236769Sobrien#define DEFSHELL_INDEX_CUSTOM 0 247236769Sobrien#define DEFSHELL_INDEX_SH 1 248236769Sobrien#define DEFSHELL_INDEX_KSH 2 249236769Sobrien#define DEFSHELL_INDEX_CSH 3 250236769Sobrien#else /* !DEFSHELL_CUSTOM */ 251236769Sobrien#define DEFSHELL_INDEX_SH 0 252236769Sobrien#define DEFSHELL_INDEX_KSH 1 253236769Sobrien#define DEFSHELL_INDEX_CSH 2 254236769Sobrien#endif /* !DEFSHELL_CUSTOM */ 255236769Sobrien 256236769Sobrien#ifndef DEFSHELL_INDEX 257236769Sobrien#define DEFSHELL_INDEX 0 /* DEFSHELL_INDEX_CUSTOM or DEFSHELL_INDEX_SH */ 258236769Sobrien#endif /* !DEFSHELL_INDEX */ 259236769Sobrien 260236769Sobrienstatic Shell shells[] = { 261236769Sobrien#ifdef DEFSHELL_CUSTOM 262236769Sobrien /* 263236769Sobrien * An sh-compatible shell with a non-standard name. 264236769Sobrien * 265236769Sobrien * Keep this in sync with the "sh" description below, but avoid 266236769Sobrien * non-portable features that might not be supplied by all 267236769Sobrien * sh-compatible shells. 268236769Sobrien */ 269236769Sobrien{ 270236769Sobrien DEFSHELL_CUSTOM, 271236769Sobrien FALSE, "", "", "", 0, 272236769Sobrien FALSE, "echo \"%s\"\n", "%s\n", "{ %s \n} || exit $?\n", "'\n'", '#', 273236769Sobrien "", 274236769Sobrien "", 275236769Sobrien}, 276236769Sobrien#endif /* DEFSHELL_CUSTOM */ 277236769Sobrien /* 278236769Sobrien * SH description. Echo control is also possible and, under 279236769Sobrien * sun UNIX anyway, one can even control error checking. 280236769Sobrien */ 281236769Sobrien{ 282236769Sobrien "sh", 283236769Sobrien FALSE, "", "", "", 0, 284236769Sobrien FALSE, "echo \"%s\"\n", "%s\n", "{ %s \n} || exit $?\n", "'\n'", '#', 285236769Sobrien#if defined(MAKE_NATIVE) && defined(__NetBSD__) 286236769Sobrien "q", 287236769Sobrien#else 288236769Sobrien "", 289236769Sobrien#endif 290236769Sobrien "", 291236769Sobrien}, 292236769Sobrien /* 293236769Sobrien * KSH description. 294236769Sobrien */ 295236769Sobrien{ 296236769Sobrien "ksh", 297236769Sobrien TRUE, "set +v", "set -v", "set +v", 6, 298236769Sobrien FALSE, "echo \"%s\"\n", "%s\n", "{ %s \n} || exit $?\n", "'\n'", '#', 299236769Sobrien "v", 300236769Sobrien "", 301236769Sobrien}, 302236769Sobrien /* 303236769Sobrien * CSH description. The csh can do echo control by playing 304236769Sobrien * with the setting of the 'echo' shell variable. Sadly, 305236769Sobrien * however, it is unable to do error control nicely. 306236769Sobrien */ 307236769Sobrien{ 308236769Sobrien "csh", 309236769Sobrien TRUE, "unset verbose", "set verbose", "unset verbose", 10, 310236769Sobrien FALSE, "echo \"%s\"\n", "csh -c \"%s || exit 0\"\n", "", "'\\\n'", '#', 311236769Sobrien "v", "e", 312236769Sobrien}, 313236769Sobrien /* 314236769Sobrien * UNKNOWN. 315236769Sobrien */ 316236769Sobrien{ 317236769Sobrien NULL, 318236769Sobrien FALSE, NULL, NULL, NULL, 0, 319236769Sobrien FALSE, NULL, NULL, NULL, NULL, 0, 320236769Sobrien NULL, NULL, 321236769Sobrien} 322236769Sobrien}; 323236769Sobrienstatic Shell *commandShell = &shells[DEFSHELL_INDEX]; /* this is the shell to 324236769Sobrien * which we pass all 325236769Sobrien * commands in the Makefile. 326236769Sobrien * It is set by the 327236769Sobrien * Job_ParseShell function */ 328236769Sobrienconst char *shellPath = NULL, /* full pathname of 329236769Sobrien * executable image */ 330236769Sobrien *shellName = NULL; /* last component of shell */ 331253883Ssjgchar *shellErrFlag = NULL; 332236769Sobrienstatic const char *shellArgv = NULL; /* Custom shell args */ 333236769Sobrien 334236769Sobrien 335236769SobrienSTATIC Job *job_table; /* The structures that describe them */ 336236769SobrienSTATIC Job *job_table_end; /* job_table + maxJobs */ 337236769Sobrienstatic int wantToken; /* we want a token */ 338236769Sobrienstatic int lurking_children = 0; 339236769Sobrienstatic int make_suspended = 0; /* non-zero if we've seen a SIGTSTP (etc) */ 340236769Sobrien 341236769Sobrien/* 342236769Sobrien * Set of descriptors of pipes connected to 343236769Sobrien * the output channels of children 344236769Sobrien */ 345236769Sobrienstatic struct pollfd *fds = NULL; 346236769Sobrienstatic Job **jobfds = NULL; 347236769Sobrienstatic int nfds = 0; 348236769Sobrienstatic void watchfd(Job *); 349236769Sobrienstatic void clearfd(Job *); 350236769Sobrienstatic int readyfd(Job *); 351236769Sobrien 352236769SobrienSTATIC GNode *lastNode; /* The node for which output was most recently 353236769Sobrien * produced. */ 354236769Sobrienstatic char *targPrefix = NULL; /* What we print at the start of TARG_FMT */ 355236769Sobrienstatic Job tokenWaitJob; /* token wait pseudo-job */ 356236769Sobrien 357236769Sobrienstatic Job childExitJob; /* child exit pseudo-job */ 358236769Sobrien#define CHILD_EXIT "." 359236769Sobrien#define DO_JOB_RESUME "R" 360236769Sobrien 361236769Sobrien#define TARG_FMT "%s %s ---\n" /* Default format */ 362236769Sobrien#define MESSAGE(fp, gn) \ 363253883Ssjg if (maxJobs != 1 && targPrefix && *targPrefix) \ 364236769Sobrien (void)fprintf(fp, TARG_FMT, targPrefix, gn->name) 365236769Sobrien 366236769Sobrienstatic sigset_t caught_signals; /* Set of signals we handle */ 367236769Sobrien 368236769Sobrienstatic void JobChildSig(int); 369236769Sobrienstatic void JobContinueSig(int); 370236769Sobrienstatic Job *JobFindPid(int, int, Boolean); 371236769Sobrienstatic int JobPrintCommand(void *, void *); 372236769Sobrienstatic int JobSaveCommand(void *, void *); 373236769Sobrienstatic void JobClose(Job *); 374236769Sobrienstatic void JobExec(Job *, char **); 375236769Sobrienstatic void JobMakeArgv(Job *, char **); 376236769Sobrienstatic int JobStart(GNode *, int); 377236769Sobrienstatic char *JobOutput(Job *, char *, char *, int); 378236769Sobrienstatic void JobDoOutput(Job *, Boolean); 379236769Sobrienstatic Shell *JobMatchShell(const char *); 380237578Sobrienstatic void JobInterrupt(int, int) MAKE_ATTR_DEAD; 381236769Sobrienstatic void JobRestartJobs(void); 382236769Sobrienstatic void JobTokenAdd(void); 383236769Sobrienstatic void JobSigLock(sigset_t *); 384236769Sobrienstatic void JobSigUnlock(sigset_t *); 385236769Sobrienstatic void JobSigReset(void); 386236769Sobrien 387319884Ssjg#if !defined(MALLOC_OPTIONS) 388319884Ssjg# define MALLOC_OPTIONS "A" 389319884Ssjg#endif 390319884Ssjgconst char *malloc_options= MALLOC_OPTIONS; 391236769Sobrien 392236769Sobrienstatic void 393236769Sobrienjob_table_dump(const char *where) 394236769Sobrien{ 395236769Sobrien Job *job; 396236769Sobrien 397236769Sobrien fprintf(debug_file, "job table @ %s\n", where); 398236769Sobrien for (job = job_table; job < job_table_end; job++) { 399236769Sobrien fprintf(debug_file, "job %d, status %d, flags %d, pid %d\n", 400236769Sobrien (int)(job - job_table), job->job_state, job->flags, job->pid); 401236769Sobrien } 402236769Sobrien} 403236769Sobrien 404236769Sobrien/* 405319884Ssjg * Delete the target of a failed, interrupted, or otherwise 406319884Ssjg * unsuccessful job unless inhibited by .PRECIOUS. 407319884Ssjg */ 408319884Ssjgstatic void 409319884SsjgJobDeleteTarget(GNode *gn) 410319884Ssjg{ 411319884Ssjg if ((gn->type & (OP_JOIN|OP_PHONY)) == 0 && !Targ_Precious(gn)) { 412319884Ssjg char *file = (gn->path == NULL ? gn->name : gn->path); 413319884Ssjg if (!noExecute && eunlink(file) != -1) { 414319884Ssjg Error("*** %s removed", file); 415319884Ssjg } 416319884Ssjg } 417319884Ssjg} 418319884Ssjg 419319884Ssjg/* 420236769Sobrien * JobSigLock/JobSigUnlock 421236769Sobrien * 422236769Sobrien * Signal lock routines to get exclusive access. Currently used to 423236769Sobrien * protect `jobs' and `stoppedJobs' list manipulations. 424236769Sobrien */ 425236769Sobrienstatic void JobSigLock(sigset_t *omaskp) 426236769Sobrien{ 427236769Sobrien if (sigprocmask(SIG_BLOCK, &caught_signals, omaskp) != 0) { 428236769Sobrien Punt("JobSigLock: sigprocmask: %s", strerror(errno)); 429236769Sobrien sigemptyset(omaskp); 430236769Sobrien } 431236769Sobrien} 432236769Sobrien 433236769Sobrienstatic void JobSigUnlock(sigset_t *omaskp) 434236769Sobrien{ 435236769Sobrien (void)sigprocmask(SIG_SETMASK, omaskp, NULL); 436236769Sobrien} 437236769Sobrien 438236769Sobrienstatic void 439236769SobrienJobCreatePipe(Job *job, int minfd) 440236769Sobrien{ 441319884Ssjg int i, fd, flags; 442236769Sobrien 443236769Sobrien if (pipe(job->jobPipe) == -1) 444236769Sobrien Punt("Cannot create pipe: %s", strerror(errno)); 445236769Sobrien 446251422Ssjg for (i = 0; i < 2; i++) { 447251422Ssjg /* Avoid using low numbered fds */ 448251422Ssjg fd = fcntl(job->jobPipe[i], F_DUPFD, minfd); 449251422Ssjg if (fd != -1) { 450251422Ssjg close(job->jobPipe[i]); 451251422Ssjg job->jobPipe[i] = fd; 452251422Ssjg } 453251422Ssjg } 454251422Ssjg 455236769Sobrien /* Set close-on-exec flag for both */ 456319884Ssjg if (fcntl(job->jobPipe[0], F_SETFD, FD_CLOEXEC) == -1) 457319884Ssjg Punt("Cannot set close-on-exec: %s", strerror(errno)); 458319884Ssjg if (fcntl(job->jobPipe[1], F_SETFD, FD_CLOEXEC) == -1) 459319884Ssjg Punt("Cannot set close-on-exec: %s", strerror(errno)); 460236769Sobrien 461236769Sobrien /* 462236769Sobrien * We mark the input side of the pipe non-blocking; we poll(2) the 463236769Sobrien * pipe when we're waiting for a job token, but we might lose the 464236769Sobrien * race for the token when a new one becomes available, so the read 465236769Sobrien * from the pipe should not block. 466236769Sobrien */ 467319884Ssjg flags = fcntl(job->jobPipe[0], F_GETFL, 0); 468319884Ssjg if (flags == -1) 469319884Ssjg Punt("Cannot get flags: %s", strerror(errno)); 470319884Ssjg flags |= O_NONBLOCK; 471319884Ssjg if (fcntl(job->jobPipe[0], F_SETFL, flags) == -1) 472319884Ssjg Punt("Cannot set flags: %s", strerror(errno)); 473236769Sobrien} 474236769Sobrien 475236769Sobrien/*- 476236769Sobrien *----------------------------------------------------------------------- 477236769Sobrien * JobCondPassSig -- 478236769Sobrien * Pass a signal to a job 479236769Sobrien * 480236769Sobrien * Input: 481236769Sobrien * signop Signal to send it 482236769Sobrien * 483236769Sobrien * Side Effects: 484236769Sobrien * None, except the job may bite it. 485236769Sobrien * 486236769Sobrien *----------------------------------------------------------------------- 487236769Sobrien */ 488236769Sobrienstatic void 489236769SobrienJobCondPassSig(int signo) 490236769Sobrien{ 491236769Sobrien Job *job; 492236769Sobrien 493236769Sobrien if (DEBUG(JOB)) { 494236769Sobrien (void)fprintf(debug_file, "JobCondPassSig(%d) called.\n", signo); 495236769Sobrien } 496236769Sobrien 497236769Sobrien for (job = job_table; job < job_table_end; job++) { 498236769Sobrien if (job->job_state != JOB_ST_RUNNING) 499236769Sobrien continue; 500236769Sobrien if (DEBUG(JOB)) { 501236769Sobrien (void)fprintf(debug_file, 502236769Sobrien "JobCondPassSig passing signal %d to child %d.\n", 503236769Sobrien signo, job->pid); 504236769Sobrien } 505236769Sobrien KILLPG(job->pid, signo); 506236769Sobrien } 507236769Sobrien} 508236769Sobrien 509236769Sobrien/*- 510236769Sobrien *----------------------------------------------------------------------- 511236769Sobrien * JobChldSig -- 512236769Sobrien * SIGCHLD handler. 513236769Sobrien * 514236769Sobrien * Input: 515236769Sobrien * signo The signal number we've received 516236769Sobrien * 517236769Sobrien * Results: 518236769Sobrien * None. 519236769Sobrien * 520236769Sobrien * Side Effects: 521236769Sobrien * Sends a token on the child exit pipe to wake us up from 522236769Sobrien * select()/poll(). 523236769Sobrien * 524236769Sobrien *----------------------------------------------------------------------- 525236769Sobrien */ 526236769Sobrienstatic void 527237578SobrienJobChildSig(int signo MAKE_ATTR_UNUSED) 528236769Sobrien{ 529246223Ssjg while (write(childExitJob.outPipe, CHILD_EXIT, 1) == -1 && errno == EAGAIN) 530246223Ssjg continue; 531236769Sobrien} 532236769Sobrien 533236769Sobrien 534236769Sobrien/*- 535236769Sobrien *----------------------------------------------------------------------- 536236769Sobrien * JobContinueSig -- 537236769Sobrien * Resume all stopped jobs. 538236769Sobrien * 539236769Sobrien * Input: 540236769Sobrien * signo The signal number we've received 541236769Sobrien * 542236769Sobrien * Results: 543236769Sobrien * None. 544236769Sobrien * 545236769Sobrien * Side Effects: 546236769Sobrien * Jobs start running again. 547236769Sobrien * 548236769Sobrien *----------------------------------------------------------------------- 549236769Sobrien */ 550236769Sobrienstatic void 551237578SobrienJobContinueSig(int signo MAKE_ATTR_UNUSED) 552236769Sobrien{ 553236769Sobrien /* 554236769Sobrien * Defer sending to SIGCONT to our stopped children until we return 555236769Sobrien * from the signal handler. 556236769Sobrien */ 557246223Ssjg while (write(childExitJob.outPipe, DO_JOB_RESUME, 1) == -1 && 558246223Ssjg errno == EAGAIN) 559246223Ssjg continue; 560236769Sobrien} 561236769Sobrien 562236769Sobrien/*- 563236769Sobrien *----------------------------------------------------------------------- 564236769Sobrien * JobPassSig -- 565236769Sobrien * Pass a signal on to all jobs, then resend to ourselves. 566236769Sobrien * 567236769Sobrien * Input: 568236769Sobrien * signo The signal number we've received 569236769Sobrien * 570236769Sobrien * Results: 571236769Sobrien * None. 572236769Sobrien * 573236769Sobrien * Side Effects: 574236769Sobrien * We die by the same signal. 575236769Sobrien * 576236769Sobrien *----------------------------------------------------------------------- 577236769Sobrien */ 578237578SobrienMAKE_ATTR_DEAD static void 579236769SobrienJobPassSig_int(int signo) 580236769Sobrien{ 581236769Sobrien /* Run .INTERRUPT target then exit */ 582236769Sobrien JobInterrupt(TRUE, signo); 583236769Sobrien} 584236769Sobrien 585237578SobrienMAKE_ATTR_DEAD static void 586236769SobrienJobPassSig_term(int signo) 587236769Sobrien{ 588236769Sobrien /* Dont run .INTERRUPT target then exit */ 589236769Sobrien JobInterrupt(FALSE, signo); 590236769Sobrien} 591236769Sobrien 592236769Sobrienstatic void 593236769SobrienJobPassSig_suspend(int signo) 594236769Sobrien{ 595236769Sobrien sigset_t nmask, omask; 596236769Sobrien struct sigaction act; 597236769Sobrien 598236769Sobrien /* Suppress job started/continued messages */ 599236769Sobrien make_suspended = 1; 600236769Sobrien 601236769Sobrien /* Pass the signal onto every job */ 602236769Sobrien JobCondPassSig(signo); 603236769Sobrien 604236769Sobrien /* 605236769Sobrien * Send ourselves the signal now we've given the message to everyone else. 606236769Sobrien * Note we block everything else possible while we're getting the signal. 607236769Sobrien * This ensures that all our jobs get continued when we wake up before 608236769Sobrien * we take any other signal. 609236769Sobrien */ 610236769Sobrien sigfillset(&nmask); 611236769Sobrien sigdelset(&nmask, signo); 612236769Sobrien (void)sigprocmask(SIG_SETMASK, &nmask, &omask); 613236769Sobrien 614236769Sobrien act.sa_handler = SIG_DFL; 615236769Sobrien sigemptyset(&act.sa_mask); 616236769Sobrien act.sa_flags = 0; 617236769Sobrien (void)sigaction(signo, &act, NULL); 618236769Sobrien 619236769Sobrien if (DEBUG(JOB)) { 620236769Sobrien (void)fprintf(debug_file, 621236769Sobrien "JobPassSig passing signal %d to self.\n", signo); 622236769Sobrien } 623236769Sobrien 624236769Sobrien (void)kill(getpid(), signo); 625236769Sobrien 626236769Sobrien /* 627236769Sobrien * We've been continued. 628236769Sobrien * 629236769Sobrien * A whole host of signals continue to happen! 630236769Sobrien * SIGCHLD for any processes that actually suspended themselves. 631236769Sobrien * SIGCHLD for any processes that exited while we were alseep. 632236769Sobrien * The SIGCONT that actually caused us to wakeup. 633236769Sobrien * 634236769Sobrien * Since we defer passing the SIGCONT on to our children until 635236769Sobrien * the main processing loop, we can be sure that all the SIGCHLD 636236769Sobrien * events will have happened by then - and that the waitpid() will 637236769Sobrien * collect the child 'suspended' events. 638236769Sobrien * For correct sequencing we just need to ensure we process the 639236769Sobrien * waitpid() before passign on the SIGCONT. 640236769Sobrien * 641236769Sobrien * In any case nothing else is needed here. 642236769Sobrien */ 643236769Sobrien 644236769Sobrien /* Restore handler and signal mask */ 645236769Sobrien act.sa_handler = JobPassSig_suspend; 646236769Sobrien (void)sigaction(signo, &act, NULL); 647236769Sobrien (void)sigprocmask(SIG_SETMASK, &omask, NULL); 648236769Sobrien} 649236769Sobrien 650236769Sobrien/*- 651236769Sobrien *----------------------------------------------------------------------- 652236769Sobrien * JobFindPid -- 653236769Sobrien * Compare the pid of the job with the given pid and return 0 if they 654236769Sobrien * are equal. This function is called from Job_CatchChildren 655236769Sobrien * to find the job descriptor of the finished job. 656236769Sobrien * 657236769Sobrien * Input: 658236769Sobrien * job job to examine 659236769Sobrien * pid process id desired 660236769Sobrien * 661236769Sobrien * Results: 662236769Sobrien * Job with matching pid 663236769Sobrien * 664236769Sobrien * Side Effects: 665236769Sobrien * None 666236769Sobrien *----------------------------------------------------------------------- 667236769Sobrien */ 668236769Sobrienstatic Job * 669236769SobrienJobFindPid(int pid, int status, Boolean isJobs) 670236769Sobrien{ 671236769Sobrien Job *job; 672236769Sobrien 673236769Sobrien for (job = job_table; job < job_table_end; job++) { 674236769Sobrien if ((job->job_state == status) && job->pid == pid) 675236769Sobrien return job; 676236769Sobrien } 677236769Sobrien if (DEBUG(JOB) && isJobs) 678236769Sobrien job_table_dump("no pid"); 679236769Sobrien return NULL; 680236769Sobrien} 681236769Sobrien 682236769Sobrien/*- 683236769Sobrien *----------------------------------------------------------------------- 684236769Sobrien * JobPrintCommand -- 685236769Sobrien * Put out another command for the given job. If the command starts 686236769Sobrien * with an @ or a - we process it specially. In the former case, 687236769Sobrien * so long as the -s and -n flags weren't given to make, we stick 688236769Sobrien * a shell-specific echoOff command in the script. In the latter, 689236769Sobrien * we ignore errors for the entire job, unless the shell has error 690236769Sobrien * control. 691236769Sobrien * If the command is just "..." we take all future commands for this 692236769Sobrien * job to be commands to be executed once the entire graph has been 693236769Sobrien * made and return non-zero to signal that the end of the commands 694236769Sobrien * was reached. These commands are later attached to the postCommands 695236769Sobrien * node and executed by Job_End when all things are done. 696236769Sobrien * This function is called from JobStart via Lst_ForEach. 697236769Sobrien * 698236769Sobrien * Input: 699236769Sobrien * cmdp command string to print 700236769Sobrien * jobp job for which to print it 701236769Sobrien * 702236769Sobrien * Results: 703236769Sobrien * Always 0, unless the command was "..." 704236769Sobrien * 705236769Sobrien * Side Effects: 706236769Sobrien * If the command begins with a '-' and the shell has no error control, 707236769Sobrien * the JOB_IGNERR flag is set in the job descriptor. 708236769Sobrien * If the command is "..." and we're not ignoring such things, 709236769Sobrien * tailCmds is set to the successor node of the cmd. 710236769Sobrien * numCommands is incremented if the command is actually printed. 711236769Sobrien *----------------------------------------------------------------------- 712236769Sobrien */ 713236769Sobrienstatic int 714236769SobrienJobPrintCommand(void *cmdp, void *jobp) 715236769Sobrien{ 716236769Sobrien Boolean noSpecials; /* true if we shouldn't worry about 717236769Sobrien * inserting special commands into 718236769Sobrien * the input stream. */ 719236769Sobrien Boolean shutUp = FALSE; /* true if we put a no echo command 720236769Sobrien * into the command file */ 721236769Sobrien Boolean errOff = FALSE; /* true if we turned error checking 722236769Sobrien * off before printing the command 723236769Sobrien * and need to turn it back on */ 724236769Sobrien const char *cmdTemplate; /* Template to use when printing the 725236769Sobrien * command */ 726236769Sobrien char *cmdStart; /* Start of expanded command */ 727236769Sobrien char *escCmd = NULL; /* Command with quotes/backticks escaped */ 728236769Sobrien char *cmd = (char *)cmdp; 729236769Sobrien Job *job = (Job *)jobp; 730236769Sobrien int i, j; 731236769Sobrien 732236769Sobrien noSpecials = NoExecute(job->node); 733236769Sobrien 734236769Sobrien if (strcmp(cmd, "...") == 0) { 735236769Sobrien job->node->type |= OP_SAVE_CMDS; 736236769Sobrien if ((job->flags & JOB_IGNDOTS) == 0) { 737236769Sobrien job->tailCmds = Lst_Succ(Lst_Member(job->node->commands, 738236769Sobrien cmd)); 739236769Sobrien return 1; 740236769Sobrien } 741236769Sobrien return 0; 742236769Sobrien } 743236769Sobrien 744236769Sobrien#define DBPRINTF(fmt, arg) if (DEBUG(JOB)) { \ 745236769Sobrien (void)fprintf(debug_file, fmt, arg); \ 746236769Sobrien } \ 747236769Sobrien (void)fprintf(job->cmdFILE, fmt, arg); \ 748236769Sobrien (void)fflush(job->cmdFILE); 749236769Sobrien 750236769Sobrien numCommands += 1; 751236769Sobrien 752296637Ssjg cmdStart = cmd = Var_Subst(NULL, cmd, job->node, VARF_WANTRES); 753236769Sobrien 754236769Sobrien cmdTemplate = "%s\n"; 755236769Sobrien 756236769Sobrien /* 757236769Sobrien * Check for leading @' and -'s to control echoing and error checking. 758236769Sobrien */ 759236769Sobrien while (*cmd == '@' || *cmd == '-' || (*cmd == '+')) { 760236769Sobrien switch (*cmd) { 761236769Sobrien case '@': 762236769Sobrien shutUp = DEBUG(LOUD) ? FALSE : TRUE; 763236769Sobrien break; 764236769Sobrien case '-': 765236769Sobrien errOff = TRUE; 766236769Sobrien break; 767236769Sobrien case '+': 768236769Sobrien if (noSpecials) { 769236769Sobrien /* 770236769Sobrien * We're not actually executing anything... 771236769Sobrien * but this one needs to be - use compat mode just for it. 772236769Sobrien */ 773236769Sobrien CompatRunCommand(cmdp, job->node); 774319884Ssjg free(cmdStart); 775236769Sobrien return 0; 776236769Sobrien } 777236769Sobrien break; 778236769Sobrien } 779236769Sobrien cmd++; 780236769Sobrien } 781236769Sobrien 782236769Sobrien while (isspace((unsigned char) *cmd)) 783236769Sobrien cmd++; 784236769Sobrien 785236769Sobrien /* 786236769Sobrien * If the shell doesn't have error control the alternate echo'ing will 787236769Sobrien * be done (to avoid showing additional error checking code) 788236769Sobrien * and this will need the characters '$ ` \ "' escaped 789236769Sobrien */ 790236769Sobrien 791236769Sobrien if (!commandShell->hasErrCtl) { 792236769Sobrien /* Worst that could happen is every char needs escaping. */ 793236769Sobrien escCmd = bmake_malloc((strlen(cmd) * 2) + 1); 794236769Sobrien for (i = 0, j= 0; cmd[i] != '\0'; i++, j++) { 795236769Sobrien if (cmd[i] == '$' || cmd[i] == '`' || cmd[i] == '\\' || 796236769Sobrien cmd[i] == '"') 797236769Sobrien escCmd[j++] = '\\'; 798236769Sobrien escCmd[j] = cmd[i]; 799236769Sobrien } 800236769Sobrien escCmd[j] = 0; 801236769Sobrien } 802236769Sobrien 803236769Sobrien if (shutUp) { 804236769Sobrien if (!(job->flags & JOB_SILENT) && !noSpecials && 805236769Sobrien commandShell->hasEchoCtl) { 806236769Sobrien DBPRINTF("%s\n", commandShell->echoOff); 807236769Sobrien } else { 808236769Sobrien if (commandShell->hasErrCtl) 809236769Sobrien shutUp = FALSE; 810236769Sobrien } 811236769Sobrien } 812236769Sobrien 813236769Sobrien if (errOff) { 814236769Sobrien if (!noSpecials) { 815236769Sobrien if (commandShell->hasErrCtl) { 816236769Sobrien /* 817236769Sobrien * we don't want the error-control commands showing 818236769Sobrien * up either, so we turn off echoing while executing 819236769Sobrien * them. We could put another field in the shell 820236769Sobrien * structure to tell JobDoOutput to look for this 821236769Sobrien * string too, but why make it any more complex than 822236769Sobrien * it already is? 823236769Sobrien */ 824236769Sobrien if (!(job->flags & JOB_SILENT) && !shutUp && 825236769Sobrien commandShell->hasEchoCtl) { 826236769Sobrien DBPRINTF("%s\n", commandShell->echoOff); 827236769Sobrien DBPRINTF("%s\n", commandShell->ignErr); 828236769Sobrien DBPRINTF("%s\n", commandShell->echoOn); 829236769Sobrien } else { 830236769Sobrien DBPRINTF("%s\n", commandShell->ignErr); 831236769Sobrien } 832236769Sobrien } else if (commandShell->ignErr && 833236769Sobrien (*commandShell->ignErr != '\0')) 834236769Sobrien { 835236769Sobrien /* 836236769Sobrien * The shell has no error control, so we need to be 837236769Sobrien * weird to get it to ignore any errors from the command. 838236769Sobrien * If echoing is turned on, we turn it off and use the 839236769Sobrien * errCheck template to echo the command. Leave echoing 840236769Sobrien * off so the user doesn't see the weirdness we go through 841236769Sobrien * to ignore errors. Set cmdTemplate to use the weirdness 842236769Sobrien * instead of the simple "%s\n" template. 843236769Sobrien */ 844281812Ssjg job->flags |= JOB_IGNERR; 845236769Sobrien if (!(job->flags & JOB_SILENT) && !shutUp) { 846236769Sobrien if (commandShell->hasEchoCtl) { 847236769Sobrien DBPRINTF("%s\n", commandShell->echoOff); 848236769Sobrien } 849236769Sobrien DBPRINTF(commandShell->errCheck, escCmd); 850236769Sobrien shutUp = TRUE; 851236769Sobrien } else { 852236769Sobrien if (!shutUp) { 853236769Sobrien DBPRINTF(commandShell->errCheck, escCmd); 854236769Sobrien } 855236769Sobrien } 856236769Sobrien cmdTemplate = commandShell->ignErr; 857236769Sobrien /* 858236769Sobrien * The error ignoration (hee hee) is already taken care 859236769Sobrien * of by the ignErr template, so pretend error checking 860236769Sobrien * is still on. 861236769Sobrien */ 862236769Sobrien errOff = FALSE; 863236769Sobrien } else { 864236769Sobrien errOff = FALSE; 865236769Sobrien } 866236769Sobrien } else { 867236769Sobrien errOff = FALSE; 868236769Sobrien } 869236769Sobrien } else { 870236769Sobrien 871236769Sobrien /* 872236769Sobrien * If errors are being checked and the shell doesn't have error control 873236769Sobrien * but does supply an errOut template, then setup commands to run 874236769Sobrien * through it. 875236769Sobrien */ 876236769Sobrien 877236769Sobrien if (!commandShell->hasErrCtl && commandShell->errOut && 878236769Sobrien (*commandShell->errOut != '\0')) { 879236769Sobrien if (!(job->flags & JOB_SILENT) && !shutUp) { 880236769Sobrien if (commandShell->hasEchoCtl) { 881236769Sobrien DBPRINTF("%s\n", commandShell->echoOff); 882236769Sobrien } 883236769Sobrien DBPRINTF(commandShell->errCheck, escCmd); 884236769Sobrien shutUp = TRUE; 885236769Sobrien } 886236769Sobrien /* If it's a comment line or blank, treat as an ignored error */ 887236769Sobrien if ((escCmd[0] == commandShell->commentChar) || 888236769Sobrien (escCmd[0] == 0)) 889236769Sobrien cmdTemplate = commandShell->ignErr; 890236769Sobrien else 891236769Sobrien cmdTemplate = commandShell->errOut; 892236769Sobrien errOff = FALSE; 893236769Sobrien } 894236769Sobrien } 895236769Sobrien 896236769Sobrien if (DEBUG(SHELL) && strcmp(shellName, "sh") == 0 && 897236769Sobrien (job->flags & JOB_TRACED) == 0) { 898236769Sobrien DBPRINTF("set -%s\n", "x"); 899236769Sobrien job->flags |= JOB_TRACED; 900236769Sobrien } 901236769Sobrien 902236769Sobrien DBPRINTF(cmdTemplate, cmd); 903236769Sobrien free(cmdStart); 904296637Ssjg free(escCmd); 905236769Sobrien if (errOff) { 906236769Sobrien /* 907236769Sobrien * If echoing is already off, there's no point in issuing the 908236769Sobrien * echoOff command. Otherwise we issue it and pretend it was on 909236769Sobrien * for the whole command... 910236769Sobrien */ 911236769Sobrien if (!shutUp && !(job->flags & JOB_SILENT) && commandShell->hasEchoCtl){ 912236769Sobrien DBPRINTF("%s\n", commandShell->echoOff); 913236769Sobrien shutUp = TRUE; 914236769Sobrien } 915236769Sobrien DBPRINTF("%s\n", commandShell->errCheck); 916236769Sobrien } 917236769Sobrien if (shutUp && commandShell->hasEchoCtl) { 918236769Sobrien DBPRINTF("%s\n", commandShell->echoOn); 919236769Sobrien } 920236769Sobrien return 0; 921236769Sobrien} 922236769Sobrien 923236769Sobrien/*- 924236769Sobrien *----------------------------------------------------------------------- 925236769Sobrien * JobSaveCommand -- 926236769Sobrien * Save a command to be executed when everything else is done. 927236769Sobrien * Callback function for JobFinish... 928236769Sobrien * 929236769Sobrien * Results: 930236769Sobrien * Always returns 0 931236769Sobrien * 932236769Sobrien * Side Effects: 933236769Sobrien * The command is tacked onto the end of postCommands's commands list. 934236769Sobrien * 935236769Sobrien *----------------------------------------------------------------------- 936236769Sobrien */ 937236769Sobrienstatic int 938236769SobrienJobSaveCommand(void *cmd, void *gn) 939236769Sobrien{ 940296637Ssjg cmd = Var_Subst(NULL, (char *)cmd, (GNode *)gn, VARF_WANTRES); 941236769Sobrien (void)Lst_AtEnd(postCommands->commands, cmd); 942236769Sobrien return(0); 943236769Sobrien} 944236769Sobrien 945236769Sobrien 946236769Sobrien/*- 947236769Sobrien *----------------------------------------------------------------------- 948236769Sobrien * JobClose -- 949236769Sobrien * Called to close both input and output pipes when a job is finished. 950236769Sobrien * 951236769Sobrien * Results: 952236769Sobrien * Nada 953236769Sobrien * 954236769Sobrien * Side Effects: 955236769Sobrien * The file descriptors associated with the job are closed. 956236769Sobrien * 957236769Sobrien *----------------------------------------------------------------------- 958236769Sobrien */ 959236769Sobrienstatic void 960236769SobrienJobClose(Job *job) 961236769Sobrien{ 962236769Sobrien clearfd(job); 963236769Sobrien (void)close(job->outPipe); 964236769Sobrien job->outPipe = -1; 965236769Sobrien 966236769Sobrien JobDoOutput(job, TRUE); 967236769Sobrien (void)close(job->inPipe); 968236769Sobrien job->inPipe = -1; 969236769Sobrien} 970236769Sobrien 971236769Sobrien/*- 972236769Sobrien *----------------------------------------------------------------------- 973236769Sobrien * JobFinish -- 974236769Sobrien * Do final processing for the given job including updating 975236769Sobrien * parents and starting new jobs as available/necessary. Note 976236769Sobrien * that we pay no attention to the JOB_IGNERR flag here. 977236769Sobrien * This is because when we're called because of a noexecute flag 978236769Sobrien * or something, jstat.w_status is 0 and when called from 979236769Sobrien * Job_CatchChildren, the status is zeroed if it s/b ignored. 980236769Sobrien * 981236769Sobrien * Input: 982236769Sobrien * job job to finish 983236769Sobrien * status sub-why job went away 984236769Sobrien * 985236769Sobrien * Results: 986236769Sobrien * None 987236769Sobrien * 988236769Sobrien * Side Effects: 989236769Sobrien * Final commands for the job are placed on postCommands. 990236769Sobrien * 991236769Sobrien * If we got an error and are aborting (aborting == ABORT_ERROR) and 992236769Sobrien * the job list is now empty, we are done for the day. 993236769Sobrien * If we recognized an error (errors !=0), we set the aborting flag 994236769Sobrien * to ABORT_ERROR so no more jobs will be started. 995236769Sobrien *----------------------------------------------------------------------- 996236769Sobrien */ 997236769Sobrien/*ARGSUSED*/ 998236769Sobrienstatic void 999236769SobrienJobFinish (Job *job, WAIT_T status) 1000236769Sobrien{ 1001236769Sobrien Boolean done, return_job_token; 1002236769Sobrien 1003236769Sobrien if (DEBUG(JOB)) { 1004236769Sobrien fprintf(debug_file, "Jobfinish: %d [%s], status %d\n", 1005236769Sobrien job->pid, job->node->name, status); 1006236769Sobrien } 1007236769Sobrien 1008236769Sobrien if ((WIFEXITED(status) && 1009236769Sobrien (((WEXITSTATUS(status) != 0) && !(job->flags & JOB_IGNERR)))) || 1010236769Sobrien WIFSIGNALED(status)) 1011236769Sobrien { 1012236769Sobrien /* 1013236769Sobrien * If it exited non-zero and either we're doing things our 1014236769Sobrien * way or we're not ignoring errors, the job is finished. 1015236769Sobrien * Similarly, if the shell died because of a signal 1016236769Sobrien * the job is also finished. In these 1017236769Sobrien * cases, finish out the job's output before printing the exit 1018236769Sobrien * status... 1019236769Sobrien */ 1020236769Sobrien JobClose(job); 1021236769Sobrien if (job->cmdFILE != NULL && job->cmdFILE != stdout) { 1022236769Sobrien (void)fclose(job->cmdFILE); 1023236769Sobrien job->cmdFILE = NULL; 1024236769Sobrien } 1025236769Sobrien done = TRUE; 1026236769Sobrien } else if (WIFEXITED(status)) { 1027236769Sobrien /* 1028236769Sobrien * Deal with ignored errors in -B mode. We need to print a message 1029236769Sobrien * telling of the ignored error as well as setting status.w_status 1030236769Sobrien * to 0 so the next command gets run. To do this, we set done to be 1031236769Sobrien * TRUE if in -B mode and the job exited non-zero. 1032236769Sobrien */ 1033236769Sobrien done = WEXITSTATUS(status) != 0; 1034236769Sobrien /* 1035236769Sobrien * Old comment said: "Note we don't 1036236769Sobrien * want to close down any of the streams until we know we're at the 1037236769Sobrien * end." 1038236769Sobrien * But we do. Otherwise when are we going to print the rest of the 1039236769Sobrien * stuff? 1040236769Sobrien */ 1041236769Sobrien JobClose(job); 1042236769Sobrien } else { 1043236769Sobrien /* 1044236769Sobrien * No need to close things down or anything. 1045236769Sobrien */ 1046236769Sobrien done = FALSE; 1047236769Sobrien } 1048236769Sobrien 1049236769Sobrien if (done) { 1050236769Sobrien if (WIFEXITED(status)) { 1051236769Sobrien if (DEBUG(JOB)) { 1052236769Sobrien (void)fprintf(debug_file, "Process %d [%s] exited.\n", 1053236769Sobrien job->pid, job->node->name); 1054236769Sobrien } 1055236769Sobrien if (WEXITSTATUS(status) != 0) { 1056236769Sobrien if (job->node != lastNode) { 1057236769Sobrien MESSAGE(stdout, job->node); 1058236769Sobrien lastNode = job->node; 1059236769Sobrien } 1060236769Sobrien#ifdef USE_META 1061236769Sobrien if (useMeta) { 1062236769Sobrien meta_job_error(job, job->node, job->flags, WEXITSTATUS(status)); 1063236769Sobrien } 1064236769Sobrien#endif 1065236769Sobrien (void)printf("*** [%s] Error code %d%s\n", 1066236769Sobrien job->node->name, 1067236769Sobrien WEXITSTATUS(status), 1068236769Sobrien (job->flags & JOB_IGNERR) ? " (ignored)" : ""); 1069236769Sobrien if (job->flags & JOB_IGNERR) { 1070236769Sobrien WAIT_STATUS(status) = 0; 1071236769Sobrien } else { 1072319884Ssjg if (deleteOnError) { 1073319884Ssjg JobDeleteTarget(job->node); 1074319884Ssjg } 1075236769Sobrien PrintOnError(job->node, NULL); 1076236769Sobrien } 1077236769Sobrien } else if (DEBUG(JOB)) { 1078236769Sobrien if (job->node != lastNode) { 1079236769Sobrien MESSAGE(stdout, job->node); 1080236769Sobrien lastNode = job->node; 1081236769Sobrien } 1082236769Sobrien (void)printf("*** [%s] Completed successfully\n", 1083236769Sobrien job->node->name); 1084236769Sobrien } 1085236769Sobrien } else { 1086236769Sobrien if (job->node != lastNode) { 1087236769Sobrien MESSAGE(stdout, job->node); 1088236769Sobrien lastNode = job->node; 1089236769Sobrien } 1090236769Sobrien (void)printf("*** [%s] Signal %d\n", 1091236769Sobrien job->node->name, WTERMSIG(status)); 1092319884Ssjg if (deleteOnError) { 1093319884Ssjg JobDeleteTarget(job->node); 1094319884Ssjg } 1095236769Sobrien } 1096236769Sobrien (void)fflush(stdout); 1097236769Sobrien } 1098236769Sobrien 1099236769Sobrien#ifdef USE_META 1100236769Sobrien if (useMeta) { 1101300313Ssjg int x; 1102300313Ssjg 1103300313Ssjg if ((x = meta_job_finish(job)) != 0 && status == 0) { 1104300313Ssjg status = x; 1105300313Ssjg } 1106236769Sobrien } 1107236769Sobrien#endif 1108236769Sobrien 1109236769Sobrien return_job_token = FALSE; 1110236769Sobrien 1111236769Sobrien Trace_Log(JOBEND, job); 1112236769Sobrien if (!(job->flags & JOB_SPECIAL)) { 1113236769Sobrien if ((WAIT_STATUS(status) != 0) || 1114236769Sobrien (aborting == ABORT_ERROR) || 1115236769Sobrien (aborting == ABORT_INTERRUPT)) 1116236769Sobrien return_job_token = TRUE; 1117236769Sobrien } 1118236769Sobrien 1119236769Sobrien if ((aborting != ABORT_ERROR) && (aborting != ABORT_INTERRUPT) && 1120236769Sobrien (WAIT_STATUS(status) == 0)) { 1121236769Sobrien /* 1122236769Sobrien * As long as we aren't aborting and the job didn't return a non-zero 1123236769Sobrien * status that we shouldn't ignore, we call Make_Update to update 1124236769Sobrien * the parents. In addition, any saved commands for the node are placed 1125236769Sobrien * on the .END target. 1126236769Sobrien */ 1127236769Sobrien if (job->tailCmds != NULL) { 1128236769Sobrien Lst_ForEachFrom(job->node->commands, job->tailCmds, 1129236769Sobrien JobSaveCommand, 1130236769Sobrien job->node); 1131236769Sobrien } 1132236769Sobrien job->node->made = MADE; 1133236769Sobrien if (!(job->flags & JOB_SPECIAL)) 1134236769Sobrien return_job_token = TRUE; 1135236769Sobrien Make_Update(job->node); 1136236769Sobrien job->job_state = JOB_ST_FREE; 1137236769Sobrien } else if (WAIT_STATUS(status)) { 1138236769Sobrien errors += 1; 1139236769Sobrien job->job_state = JOB_ST_FREE; 1140236769Sobrien } 1141236769Sobrien 1142236769Sobrien /* 1143236769Sobrien * Set aborting if any error. 1144236769Sobrien */ 1145236769Sobrien if (errors && !keepgoing && (aborting != ABORT_INTERRUPT)) { 1146236769Sobrien /* 1147236769Sobrien * If we found any errors in this batch of children and the -k flag 1148236769Sobrien * wasn't given, we set the aborting flag so no more jobs get 1149236769Sobrien * started. 1150236769Sobrien */ 1151236769Sobrien aborting = ABORT_ERROR; 1152236769Sobrien } 1153236769Sobrien 1154236769Sobrien if (return_job_token) 1155236769Sobrien Job_TokenReturn(); 1156236769Sobrien 1157236769Sobrien if (aborting == ABORT_ERROR && jobTokensRunning == 0) { 1158236769Sobrien /* 1159236769Sobrien * If we are aborting and the job table is now empty, we finish. 1160236769Sobrien */ 1161236769Sobrien Finish(errors); 1162236769Sobrien } 1163236769Sobrien} 1164236769Sobrien 1165236769Sobrien/*- 1166236769Sobrien *----------------------------------------------------------------------- 1167236769Sobrien * Job_Touch -- 1168236769Sobrien * Touch the given target. Called by JobStart when the -t flag was 1169236769Sobrien * given 1170236769Sobrien * 1171236769Sobrien * Input: 1172236769Sobrien * gn the node of the file to touch 1173236769Sobrien * silent TRUE if should not print message 1174236769Sobrien * 1175236769Sobrien * Results: 1176236769Sobrien * None 1177236769Sobrien * 1178236769Sobrien * Side Effects: 1179236769Sobrien * The data modification of the file is changed. In addition, if the 1180236769Sobrien * file did not exist, it is created. 1181236769Sobrien *----------------------------------------------------------------------- 1182236769Sobrien */ 1183236769Sobrienvoid 1184236769SobrienJob_Touch(GNode *gn, Boolean silent) 1185236769Sobrien{ 1186236769Sobrien int streamID; /* ID of stream opened to do the touch */ 1187236769Sobrien struct utimbuf times; /* Times for utime() call */ 1188236769Sobrien 1189236769Sobrien if (gn->type & (OP_JOIN|OP_USE|OP_USEBEFORE|OP_EXEC|OP_OPTIONAL| 1190236769Sobrien OP_SPECIAL|OP_PHONY)) { 1191236769Sobrien /* 1192236769Sobrien * .JOIN, .USE, .ZEROTIME and .OPTIONAL targets are "virtual" targets 1193236769Sobrien * and, as such, shouldn't really be created. 1194236769Sobrien */ 1195236769Sobrien return; 1196236769Sobrien } 1197236769Sobrien 1198236769Sobrien if (!silent || NoExecute(gn)) { 1199236769Sobrien (void)fprintf(stdout, "touch %s\n", gn->name); 1200236769Sobrien (void)fflush(stdout); 1201236769Sobrien } 1202236769Sobrien 1203236769Sobrien if (NoExecute(gn)) { 1204236769Sobrien return; 1205236769Sobrien } 1206236769Sobrien 1207236769Sobrien if (gn->type & OP_ARCHV) { 1208236769Sobrien Arch_Touch(gn); 1209236769Sobrien } else if (gn->type & OP_LIB) { 1210236769Sobrien Arch_TouchLib(gn); 1211236769Sobrien } else { 1212236769Sobrien char *file = gn->path ? gn->path : gn->name; 1213236769Sobrien 1214236769Sobrien times.actime = times.modtime = now; 1215236769Sobrien if (utime(file, ×) < 0){ 1216236769Sobrien streamID = open(file, O_RDWR | O_CREAT, 0666); 1217236769Sobrien 1218236769Sobrien if (streamID >= 0) { 1219236769Sobrien char c; 1220236769Sobrien 1221236769Sobrien /* 1222236769Sobrien * Read and write a byte to the file to change the 1223236769Sobrien * modification time, then close the file. 1224236769Sobrien */ 1225236769Sobrien if (read(streamID, &c, 1) == 1) { 1226236769Sobrien (void)lseek(streamID, (off_t)0, SEEK_SET); 1227246223Ssjg while (write(streamID, &c, 1) == -1 && errno == EAGAIN) 1228246223Ssjg continue; 1229236769Sobrien } 1230236769Sobrien 1231236769Sobrien (void)close(streamID); 1232236769Sobrien } else { 1233236769Sobrien (void)fprintf(stdout, "*** couldn't touch %s: %s", 1234236769Sobrien file, strerror(errno)); 1235236769Sobrien (void)fflush(stdout); 1236236769Sobrien } 1237236769Sobrien } 1238236769Sobrien } 1239236769Sobrien} 1240236769Sobrien 1241236769Sobrien/*- 1242236769Sobrien *----------------------------------------------------------------------- 1243236769Sobrien * Job_CheckCommands -- 1244236769Sobrien * Make sure the given node has all the commands it needs. 1245236769Sobrien * 1246236769Sobrien * Input: 1247236769Sobrien * gn The target whose commands need verifying 1248236769Sobrien * abortProc Function to abort with message 1249236769Sobrien * 1250236769Sobrien * Results: 1251236769Sobrien * TRUE if the commands list is/was ok. 1252236769Sobrien * 1253236769Sobrien * Side Effects: 1254236769Sobrien * The node will have commands from the .DEFAULT rule added to it 1255236769Sobrien * if it needs them. 1256236769Sobrien *----------------------------------------------------------------------- 1257236769Sobrien */ 1258236769SobrienBoolean 1259236769SobrienJob_CheckCommands(GNode *gn, void (*abortProc)(const char *, ...)) 1260236769Sobrien{ 1261236769Sobrien if (OP_NOP(gn->type) && Lst_IsEmpty(gn->commands) && 1262236769Sobrien ((gn->type & OP_LIB) == 0 || Lst_IsEmpty(gn->children))) { 1263236769Sobrien /* 1264236769Sobrien * No commands. Look for .DEFAULT rule from which we might infer 1265236769Sobrien * commands 1266236769Sobrien */ 1267236769Sobrien if ((DEFAULT != NULL) && !Lst_IsEmpty(DEFAULT->commands) && 1268236769Sobrien (gn->type & OP_SPECIAL) == 0) { 1269236769Sobrien char *p1; 1270236769Sobrien /* 1271236769Sobrien * Make only looks for a .DEFAULT if the node was never the 1272236769Sobrien * target of an operator, so that's what we do too. If 1273236769Sobrien * a .DEFAULT was given, we substitute its commands for gn's 1274236769Sobrien * commands and set the IMPSRC variable to be the target's name 1275236769Sobrien * The DEFAULT node acts like a transformation rule, in that 1276236769Sobrien * gn also inherits any attributes or sources attached to 1277236769Sobrien * .DEFAULT itself. 1278236769Sobrien */ 1279236769Sobrien Make_HandleUse(DEFAULT, gn); 1280236769Sobrien Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), gn, 0); 1281296637Ssjg free(p1); 1282236769Sobrien } else if (Dir_MTime(gn, 0) == 0 && (gn->type & OP_SPECIAL) == 0) { 1283236769Sobrien /* 1284236769Sobrien * The node wasn't the target of an operator we have no .DEFAULT 1285236769Sobrien * rule to go on and the target doesn't already exist. There's 1286236769Sobrien * nothing more we can do for this branch. If the -k flag wasn't 1287236769Sobrien * given, we stop in our tracks, otherwise we just don't update 1288236769Sobrien * this node's parents so they never get examined. 1289236769Sobrien */ 1290236769Sobrien static const char msg[] = ": don't know how to make"; 1291236769Sobrien 1292236769Sobrien if (gn->flags & FROM_DEPEND) { 1293249033Ssjg if (!Job_RunTarget(".STALE", gn->fname)) 1294249033Ssjg fprintf(stdout, "%s: %s, %d: ignoring stale %s for %s\n", 1295249033Ssjg progname, gn->fname, gn->lineno, makeDependfile, 1296249033Ssjg gn->name); 1297236769Sobrien return TRUE; 1298236769Sobrien } 1299236769Sobrien 1300236769Sobrien if (gn->type & OP_OPTIONAL) { 1301236769Sobrien (void)fprintf(stdout, "%s%s %s (ignored)\n", progname, 1302236769Sobrien msg, gn->name); 1303236769Sobrien (void)fflush(stdout); 1304236769Sobrien } else if (keepgoing) { 1305236769Sobrien (void)fprintf(stdout, "%s%s %s (continuing)\n", progname, 1306236769Sobrien msg, gn->name); 1307236769Sobrien (void)fflush(stdout); 1308236769Sobrien return FALSE; 1309236769Sobrien } else { 1310236769Sobrien (*abortProc)("%s%s %s. Stop", progname, msg, gn->name); 1311236769Sobrien return FALSE; 1312236769Sobrien } 1313236769Sobrien } 1314236769Sobrien } 1315236769Sobrien return TRUE; 1316236769Sobrien} 1317236769Sobrien 1318236769Sobrien/*- 1319236769Sobrien *----------------------------------------------------------------------- 1320236769Sobrien * JobExec -- 1321236769Sobrien * Execute the shell for the given job. Called from JobStart 1322236769Sobrien * 1323236769Sobrien * Input: 1324236769Sobrien * job Job to execute 1325236769Sobrien * 1326236769Sobrien * Results: 1327236769Sobrien * None. 1328236769Sobrien * 1329236769Sobrien * Side Effects: 1330236769Sobrien * A shell is executed, outputs is altered and the Job structure added 1331236769Sobrien * to the job table. 1332236769Sobrien * 1333236769Sobrien *----------------------------------------------------------------------- 1334236769Sobrien */ 1335236769Sobrienstatic void 1336236769SobrienJobExec(Job *job, char **argv) 1337236769Sobrien{ 1338236769Sobrien int cpid; /* ID of new child */ 1339236769Sobrien sigset_t mask; 1340236769Sobrien 1341236769Sobrien job->flags &= ~JOB_TRACED; 1342236769Sobrien 1343236769Sobrien if (DEBUG(JOB)) { 1344236769Sobrien int i; 1345236769Sobrien 1346236769Sobrien (void)fprintf(debug_file, "Running %s %sly\n", job->node->name, "local"); 1347236769Sobrien (void)fprintf(debug_file, "\tCommand: "); 1348236769Sobrien for (i = 0; argv[i] != NULL; i++) { 1349236769Sobrien (void)fprintf(debug_file, "%s ", argv[i]); 1350236769Sobrien } 1351236769Sobrien (void)fprintf(debug_file, "\n"); 1352236769Sobrien } 1353236769Sobrien 1354236769Sobrien /* 1355236769Sobrien * Some jobs produce no output and it's disconcerting to have 1356236769Sobrien * no feedback of their running (since they produce no output, the 1357236769Sobrien * banner with their name in it never appears). This is an attempt to 1358236769Sobrien * provide that feedback, even if nothing follows it. 1359236769Sobrien */ 1360236769Sobrien if ((lastNode != job->node) && !(job->flags & JOB_SILENT)) { 1361236769Sobrien MESSAGE(stdout, job->node); 1362236769Sobrien lastNode = job->node; 1363236769Sobrien } 1364236769Sobrien 1365236769Sobrien /* No interruptions until this job is on the `jobs' list */ 1366236769Sobrien JobSigLock(&mask); 1367236769Sobrien 1368236769Sobrien /* Pre-emptively mark job running, pid still zero though */ 1369236769Sobrien job->job_state = JOB_ST_RUNNING; 1370236769Sobrien 1371236769Sobrien cpid = vFork(); 1372236769Sobrien if (cpid == -1) 1373236769Sobrien Punt("Cannot vfork: %s", strerror(errno)); 1374236769Sobrien 1375236769Sobrien if (cpid == 0) { 1376236769Sobrien /* Child */ 1377236769Sobrien sigset_t tmask; 1378236769Sobrien 1379236769Sobrien#ifdef USE_META 1380236769Sobrien if (useMeta) { 1381236769Sobrien meta_job_child(job); 1382236769Sobrien } 1383236769Sobrien#endif 1384236769Sobrien /* 1385236769Sobrien * Reset all signal handlers; this is necessary because we also 1386236769Sobrien * need to unblock signals before we exec(2). 1387236769Sobrien */ 1388236769Sobrien JobSigReset(); 1389236769Sobrien 1390236769Sobrien /* Now unblock signals */ 1391236769Sobrien sigemptyset(&tmask); 1392236769Sobrien JobSigUnlock(&tmask); 1393236769Sobrien 1394236769Sobrien /* 1395236769Sobrien * Must duplicate the input stream down to the child's input and 1396236769Sobrien * reset it to the beginning (again). Since the stream was marked 1397236769Sobrien * close-on-exec, we must clear that bit in the new input. 1398236769Sobrien */ 1399236769Sobrien if (dup2(FILENO(job->cmdFILE), 0) == -1) { 1400236769Sobrien execError("dup2", "job->cmdFILE"); 1401236769Sobrien _exit(1); 1402236769Sobrien } 1403319884Ssjg if (fcntl(0, F_SETFD, 0) == -1) { 1404319884Ssjg execError("fcntl clear close-on-exec", "stdin"); 1405319884Ssjg _exit(1); 1406319884Ssjg } 1407319884Ssjg if (lseek(0, (off_t)0, SEEK_SET) == -1) { 1408319884Ssjg execError("lseek to 0", "stdin"); 1409319884Ssjg _exit(1); 1410319884Ssjg } 1411236769Sobrien 1412281812Ssjg if (Always_pass_job_queue || 1413281812Ssjg (job->node->type & (OP_MAKE | OP_SUBMAKE))) { 1414236769Sobrien /* 1415236769Sobrien * Pass job token pipe to submakes. 1416236769Sobrien */ 1417319884Ssjg if (fcntl(tokenWaitJob.inPipe, F_SETFD, 0) == -1) { 1418319884Ssjg execError("clear close-on-exec", "tokenWaitJob.inPipe"); 1419319884Ssjg _exit(1); 1420319884Ssjg } 1421319884Ssjg if (fcntl(tokenWaitJob.outPipe, F_SETFD, 0) == -1) { 1422319884Ssjg execError("clear close-on-exec", "tokenWaitJob.outPipe"); 1423319884Ssjg _exit(1); 1424319884Ssjg } 1425236769Sobrien } 1426236769Sobrien 1427236769Sobrien /* 1428236769Sobrien * Set up the child's output to be routed through the pipe 1429236769Sobrien * we've created for it. 1430236769Sobrien */ 1431236769Sobrien if (dup2(job->outPipe, 1) == -1) { 1432236769Sobrien execError("dup2", "job->outPipe"); 1433236769Sobrien _exit(1); 1434236769Sobrien } 1435236769Sobrien /* 1436236769Sobrien * The output channels are marked close on exec. This bit was 1437236769Sobrien * duplicated by the dup2(on some systems), so we have to clear 1438236769Sobrien * it before routing the shell's error output to the same place as 1439236769Sobrien * its standard output. 1440236769Sobrien */ 1441319884Ssjg if (fcntl(1, F_SETFD, 0) == -1) { 1442319884Ssjg execError("clear close-on-exec", "stdout"); 1443319884Ssjg _exit(1); 1444319884Ssjg } 1445236769Sobrien if (dup2(1, 2) == -1) { 1446236769Sobrien execError("dup2", "1, 2"); 1447236769Sobrien _exit(1); 1448236769Sobrien } 1449236769Sobrien 1450236769Sobrien /* 1451236769Sobrien * We want to switch the child into a different process family so 1452236769Sobrien * we can kill it and all its descendants in one fell swoop, 1453236769Sobrien * by killing its process family, but not commit suicide. 1454236769Sobrien */ 1455236769Sobrien#if defined(HAVE_SETPGID) 1456236769Sobrien (void)setpgid(0, getpid()); 1457236769Sobrien#else 1458236769Sobrien#if defined(HAVE_SETSID) 1459236769Sobrien /* XXX: dsl - I'm sure this should be setpgrp()... */ 1460236769Sobrien (void)setsid(); 1461236769Sobrien#else 1462236769Sobrien (void)setpgrp(0, getpid()); 1463236769Sobrien#endif 1464236769Sobrien#endif 1465236769Sobrien 1466236769Sobrien Var_ExportVars(); 1467236769Sobrien 1468236769Sobrien (void)execv(shellPath, argv); 1469236769Sobrien execError("exec", shellPath); 1470236769Sobrien _exit(1); 1471236769Sobrien } 1472236769Sobrien 1473236769Sobrien /* Parent, continuing after the child exec */ 1474236769Sobrien job->pid = cpid; 1475236769Sobrien 1476236769Sobrien Trace_Log(JOBSTART, job); 1477236769Sobrien 1478236769Sobrien /* 1479236769Sobrien * Set the current position in the buffer to the beginning 1480236769Sobrien * and mark another stream to watch in the outputs mask 1481236769Sobrien */ 1482236769Sobrien job->curPos = 0; 1483236769Sobrien 1484236769Sobrien watchfd(job); 1485236769Sobrien 1486236769Sobrien if (job->cmdFILE != NULL && job->cmdFILE != stdout) { 1487236769Sobrien (void)fclose(job->cmdFILE); 1488236769Sobrien job->cmdFILE = NULL; 1489236769Sobrien } 1490236769Sobrien 1491236769Sobrien /* 1492236769Sobrien * Now the job is actually running, add it to the table. 1493236769Sobrien */ 1494236769Sobrien if (DEBUG(JOB)) { 1495236769Sobrien fprintf(debug_file, "JobExec(%s): pid %d added to jobs table\n", 1496236769Sobrien job->node->name, job->pid); 1497236769Sobrien job_table_dump("job started"); 1498236769Sobrien } 1499236769Sobrien JobSigUnlock(&mask); 1500236769Sobrien} 1501236769Sobrien 1502236769Sobrien/*- 1503236769Sobrien *----------------------------------------------------------------------- 1504236769Sobrien * JobMakeArgv -- 1505236769Sobrien * Create the argv needed to execute the shell for a given job. 1506236769Sobrien * 1507236769Sobrien * 1508236769Sobrien * Results: 1509236769Sobrien * 1510236769Sobrien * Side Effects: 1511236769Sobrien * 1512236769Sobrien *----------------------------------------------------------------------- 1513236769Sobrien */ 1514236769Sobrienstatic void 1515236769SobrienJobMakeArgv(Job *job, char **argv) 1516236769Sobrien{ 1517236769Sobrien int argc; 1518236769Sobrien static char args[10]; /* For merged arguments */ 1519236769Sobrien 1520236769Sobrien argv[0] = UNCONST(shellName); 1521236769Sobrien argc = 1; 1522236769Sobrien 1523236769Sobrien if ((commandShell->exit && (*commandShell->exit != '-')) || 1524236769Sobrien (commandShell->echo && (*commandShell->echo != '-'))) 1525236769Sobrien { 1526236769Sobrien /* 1527236769Sobrien * At least one of the flags doesn't have a minus before it, so 1528236769Sobrien * merge them together. Have to do this because the *(&(@*#*&#$# 1529236769Sobrien * Bourne shell thinks its second argument is a file to source. 1530236769Sobrien * Grrrr. Note the ten-character limitation on the combined arguments. 1531236769Sobrien */ 1532236769Sobrien (void)snprintf(args, sizeof(args), "-%s%s", 1533236769Sobrien ((job->flags & JOB_IGNERR) ? "" : 1534236769Sobrien (commandShell->exit ? commandShell->exit : "")), 1535236769Sobrien ((job->flags & JOB_SILENT) ? "" : 1536236769Sobrien (commandShell->echo ? commandShell->echo : ""))); 1537236769Sobrien 1538236769Sobrien if (args[1]) { 1539236769Sobrien argv[argc] = args; 1540236769Sobrien argc++; 1541236769Sobrien } 1542236769Sobrien } else { 1543236769Sobrien if (!(job->flags & JOB_IGNERR) && commandShell->exit) { 1544236769Sobrien argv[argc] = UNCONST(commandShell->exit); 1545236769Sobrien argc++; 1546236769Sobrien } 1547236769Sobrien if (!(job->flags & JOB_SILENT) && commandShell->echo) { 1548236769Sobrien argv[argc] = UNCONST(commandShell->echo); 1549236769Sobrien argc++; 1550236769Sobrien } 1551236769Sobrien } 1552236769Sobrien argv[argc] = NULL; 1553236769Sobrien} 1554236769Sobrien 1555236769Sobrien/*- 1556236769Sobrien *----------------------------------------------------------------------- 1557236769Sobrien * JobStart -- 1558236769Sobrien * Start a target-creation process going for the target described 1559236769Sobrien * by the graph node gn. 1560236769Sobrien * 1561236769Sobrien * Input: 1562236769Sobrien * gn target to create 1563236769Sobrien * flags flags for the job to override normal ones. 1564236769Sobrien * e.g. JOB_SPECIAL or JOB_IGNDOTS 1565236769Sobrien * previous The previous Job structure for this node, if any. 1566236769Sobrien * 1567236769Sobrien * Results: 1568236769Sobrien * JOB_ERROR if there was an error in the commands, JOB_FINISHED 1569236769Sobrien * if there isn't actually anything left to do for the job and 1570236769Sobrien * JOB_RUNNING if the job has been started. 1571236769Sobrien * 1572236769Sobrien * Side Effects: 1573236769Sobrien * A new Job node is created and added to the list of running 1574236769Sobrien * jobs. PMake is forked and a child shell created. 1575236769Sobrien * 1576236769Sobrien * NB: I'm fairly sure that this code is never called with JOB_SPECIAL set 1577236769Sobrien * JOB_IGNDOTS is never set (dsl) 1578236769Sobrien * Also the return value is ignored by everyone. 1579236769Sobrien *----------------------------------------------------------------------- 1580236769Sobrien */ 1581236769Sobrienstatic int 1582236769SobrienJobStart(GNode *gn, int flags) 1583236769Sobrien{ 1584236769Sobrien Job *job; /* new job descriptor */ 1585236769Sobrien char *argv[10]; /* Argument vector to shell */ 1586236769Sobrien Boolean cmdsOK; /* true if the nodes commands were all right */ 1587236769Sobrien Boolean noExec; /* Set true if we decide not to run the job */ 1588236769Sobrien int tfd; /* File descriptor to the temp file */ 1589236769Sobrien 1590236769Sobrien for (job = job_table; job < job_table_end; job++) { 1591236769Sobrien if (job->job_state == JOB_ST_FREE) 1592236769Sobrien break; 1593236769Sobrien } 1594236769Sobrien if (job >= job_table_end) 1595236769Sobrien Punt("JobStart no job slots vacant"); 1596236769Sobrien 1597236769Sobrien memset(job, 0, sizeof *job); 1598236769Sobrien job->job_state = JOB_ST_SETUP; 1599236769Sobrien if (gn->type & OP_SPECIAL) 1600236769Sobrien flags |= JOB_SPECIAL; 1601236769Sobrien 1602236769Sobrien job->node = gn; 1603236769Sobrien job->tailCmds = NULL; 1604236769Sobrien 1605236769Sobrien /* 1606236769Sobrien * Set the initial value of the flags for this job based on the global 1607236769Sobrien * ones and the node's attributes... Any flags supplied by the caller 1608236769Sobrien * are also added to the field. 1609236769Sobrien */ 1610236769Sobrien job->flags = 0; 1611236769Sobrien if (Targ_Ignore(gn)) { 1612236769Sobrien job->flags |= JOB_IGNERR; 1613236769Sobrien } 1614236769Sobrien if (Targ_Silent(gn)) { 1615236769Sobrien job->flags |= JOB_SILENT; 1616236769Sobrien } 1617236769Sobrien job->flags |= flags; 1618236769Sobrien 1619236769Sobrien /* 1620236769Sobrien * Check the commands now so any attributes from .DEFAULT have a chance 1621236769Sobrien * to migrate to the node 1622236769Sobrien */ 1623236769Sobrien cmdsOK = Job_CheckCommands(gn, Error); 1624236769Sobrien 1625236769Sobrien job->inPollfd = NULL; 1626236769Sobrien /* 1627236769Sobrien * If the -n flag wasn't given, we open up OUR (not the child's) 1628236769Sobrien * temporary file to stuff commands in it. The thing is rd/wr so we don't 1629236769Sobrien * need to reopen it to feed it to the shell. If the -n flag *was* given, 1630236769Sobrien * we just set the file to be stdout. Cute, huh? 1631236769Sobrien */ 1632236769Sobrien if (((gn->type & OP_MAKE) && !(noRecursiveExecute)) || 1633236769Sobrien (!noExecute && !touchFlag)) { 1634236769Sobrien /* 1635236769Sobrien * tfile is the name of a file into which all shell commands are 1636236769Sobrien * put. It is removed before the child shell is executed, unless 1637236769Sobrien * DEBUG(SCRIPT) is set. 1638236769Sobrien */ 1639236769Sobrien char *tfile; 1640236769Sobrien sigset_t mask; 1641236769Sobrien /* 1642236769Sobrien * We're serious here, but if the commands were bogus, we're 1643236769Sobrien * also dead... 1644236769Sobrien */ 1645236769Sobrien if (!cmdsOK) { 1646236769Sobrien PrintOnError(gn, NULL); /* provide some clue */ 1647236769Sobrien DieHorribly(); 1648236769Sobrien } 1649236769Sobrien 1650236769Sobrien JobSigLock(&mask); 1651236769Sobrien tfd = mkTempFile(TMPPAT, &tfile); 1652236769Sobrien if (!DEBUG(SCRIPT)) 1653236769Sobrien (void)eunlink(tfile); 1654236769Sobrien JobSigUnlock(&mask); 1655236769Sobrien 1656236769Sobrien job->cmdFILE = fdopen(tfd, "w+"); 1657236769Sobrien if (job->cmdFILE == NULL) { 1658236769Sobrien Punt("Could not fdopen %s", tfile); 1659236769Sobrien } 1660296637Ssjg (void)fcntl(FILENO(job->cmdFILE), F_SETFD, FD_CLOEXEC); 1661236769Sobrien /* 1662236769Sobrien * Send the commands to the command file, flush all its buffers then 1663236769Sobrien * rewind and remove the thing. 1664236769Sobrien */ 1665236769Sobrien noExec = FALSE; 1666236769Sobrien 1667236769Sobrien#ifdef USE_META 1668236769Sobrien if (useMeta) { 1669236769Sobrien meta_job_start(job, gn); 1670236769Sobrien if (Targ_Silent(gn)) { /* might have changed */ 1671236769Sobrien job->flags |= JOB_SILENT; 1672236769Sobrien } 1673236769Sobrien } 1674236769Sobrien#endif 1675236769Sobrien /* 1676236769Sobrien * We can do all the commands at once. hooray for sanity 1677236769Sobrien */ 1678236769Sobrien numCommands = 0; 1679236769Sobrien Lst_ForEach(gn->commands, JobPrintCommand, job); 1680236769Sobrien 1681236769Sobrien /* 1682236769Sobrien * If we didn't print out any commands to the shell script, 1683236769Sobrien * there's not much point in executing the shell, is there? 1684236769Sobrien */ 1685236769Sobrien if (numCommands == 0) { 1686236769Sobrien noExec = TRUE; 1687236769Sobrien } 1688236769Sobrien 1689236769Sobrien free(tfile); 1690236769Sobrien } else if (NoExecute(gn)) { 1691236769Sobrien /* 1692236769Sobrien * Not executing anything -- just print all the commands to stdout 1693236769Sobrien * in one fell swoop. This will still set up job->tailCmds correctly. 1694236769Sobrien */ 1695236769Sobrien if (lastNode != gn) { 1696236769Sobrien MESSAGE(stdout, gn); 1697236769Sobrien lastNode = gn; 1698236769Sobrien } 1699236769Sobrien job->cmdFILE = stdout; 1700236769Sobrien /* 1701236769Sobrien * Only print the commands if they're ok, but don't die if they're 1702236769Sobrien * not -- just let the user know they're bad and keep going. It 1703236769Sobrien * doesn't do any harm in this case and may do some good. 1704236769Sobrien */ 1705236769Sobrien if (cmdsOK) { 1706236769Sobrien Lst_ForEach(gn->commands, JobPrintCommand, job); 1707236769Sobrien } 1708236769Sobrien /* 1709236769Sobrien * Don't execute the shell, thank you. 1710236769Sobrien */ 1711236769Sobrien noExec = TRUE; 1712236769Sobrien } else { 1713236769Sobrien /* 1714236769Sobrien * Just touch the target and note that no shell should be executed. 1715236769Sobrien * Set cmdFILE to stdout to make life easier. Check the commands, too, 1716236769Sobrien * but don't die if they're no good -- it does no harm to keep working 1717236769Sobrien * up the graph. 1718236769Sobrien */ 1719236769Sobrien job->cmdFILE = stdout; 1720236769Sobrien Job_Touch(gn, job->flags&JOB_SILENT); 1721236769Sobrien noExec = TRUE; 1722236769Sobrien } 1723236769Sobrien /* Just in case it isn't already... */ 1724236769Sobrien (void)fflush(job->cmdFILE); 1725236769Sobrien 1726236769Sobrien /* 1727236769Sobrien * If we're not supposed to execute a shell, don't. 1728236769Sobrien */ 1729236769Sobrien if (noExec) { 1730236769Sobrien if (!(job->flags & JOB_SPECIAL)) 1731236769Sobrien Job_TokenReturn(); 1732236769Sobrien /* 1733236769Sobrien * Unlink and close the command file if we opened one 1734236769Sobrien */ 1735236769Sobrien if (job->cmdFILE != stdout) { 1736236769Sobrien if (job->cmdFILE != NULL) { 1737236769Sobrien (void)fclose(job->cmdFILE); 1738236769Sobrien job->cmdFILE = NULL; 1739236769Sobrien } 1740236769Sobrien } 1741236769Sobrien 1742236769Sobrien /* 1743236769Sobrien * We only want to work our way up the graph if we aren't here because 1744236769Sobrien * the commands for the job were no good. 1745236769Sobrien */ 1746236769Sobrien if (cmdsOK && aborting == 0) { 1747236769Sobrien if (job->tailCmds != NULL) { 1748236769Sobrien Lst_ForEachFrom(job->node->commands, job->tailCmds, 1749236769Sobrien JobSaveCommand, 1750236769Sobrien job->node); 1751236769Sobrien } 1752236769Sobrien job->node->made = MADE; 1753236769Sobrien Make_Update(job->node); 1754236769Sobrien } 1755236769Sobrien job->job_state = JOB_ST_FREE; 1756236769Sobrien return cmdsOK ? JOB_FINISHED : JOB_ERROR; 1757236769Sobrien } 1758236769Sobrien 1759236769Sobrien /* 1760236769Sobrien * Set up the control arguments to the shell. This is based on the flags 1761236769Sobrien * set earlier for this job. 1762236769Sobrien */ 1763236769Sobrien JobMakeArgv(job, argv); 1764236769Sobrien 1765236769Sobrien /* Create the pipe by which we'll get the shell's output. */ 1766236769Sobrien JobCreatePipe(job, 3); 1767236769Sobrien 1768236769Sobrien JobExec(job, argv); 1769236769Sobrien return(JOB_RUNNING); 1770236769Sobrien} 1771236769Sobrien 1772236769Sobrienstatic char * 1773236769SobrienJobOutput(Job *job, char *cp, char *endp, int msg) 1774236769Sobrien{ 1775236769Sobrien char *ecp; 1776236769Sobrien 1777236769Sobrien if (commandShell->noPrint) { 1778236769Sobrien ecp = Str_FindSubstring(cp, commandShell->noPrint); 1779236769Sobrien while (ecp != NULL) { 1780236769Sobrien if (cp != ecp) { 1781236769Sobrien *ecp = '\0'; 1782236769Sobrien if (!beSilent && msg && job->node != lastNode) { 1783236769Sobrien MESSAGE(stdout, job->node); 1784236769Sobrien lastNode = job->node; 1785236769Sobrien } 1786236769Sobrien /* 1787236769Sobrien * The only way there wouldn't be a newline after 1788236769Sobrien * this line is if it were the last in the buffer. 1789236769Sobrien * however, since the non-printable comes after it, 1790236769Sobrien * there must be a newline, so we don't print one. 1791236769Sobrien */ 1792236769Sobrien (void)fprintf(stdout, "%s", cp); 1793236769Sobrien (void)fflush(stdout); 1794236769Sobrien } 1795236769Sobrien cp = ecp + commandShell->noPLen; 1796236769Sobrien if (cp != endp) { 1797236769Sobrien /* 1798236769Sobrien * Still more to print, look again after skipping 1799236769Sobrien * the whitespace following the non-printable 1800236769Sobrien * command.... 1801236769Sobrien */ 1802236769Sobrien cp++; 1803236769Sobrien while (*cp == ' ' || *cp == '\t' || *cp == '\n') { 1804236769Sobrien cp++; 1805236769Sobrien } 1806236769Sobrien ecp = Str_FindSubstring(cp, commandShell->noPrint); 1807236769Sobrien } else { 1808236769Sobrien return cp; 1809236769Sobrien } 1810236769Sobrien } 1811236769Sobrien } 1812236769Sobrien return cp; 1813236769Sobrien} 1814236769Sobrien 1815236769Sobrien/*- 1816236769Sobrien *----------------------------------------------------------------------- 1817236769Sobrien * JobDoOutput -- 1818236769Sobrien * This function is called at different times depending on 1819236769Sobrien * whether the user has specified that output is to be collected 1820236769Sobrien * via pipes or temporary files. In the former case, we are called 1821236769Sobrien * whenever there is something to read on the pipe. We collect more 1822236769Sobrien * output from the given job and store it in the job's outBuf. If 1823236769Sobrien * this makes up a line, we print it tagged by the job's identifier, 1824236769Sobrien * as necessary. 1825236769Sobrien * If output has been collected in a temporary file, we open the 1826236769Sobrien * file and read it line by line, transfering it to our own 1827236769Sobrien * output channel until the file is empty. At which point we 1828236769Sobrien * remove the temporary file. 1829236769Sobrien * In both cases, however, we keep our figurative eye out for the 1830236769Sobrien * 'noPrint' line for the shell from which the output came. If 1831236769Sobrien * we recognize a line, we don't print it. If the command is not 1832236769Sobrien * alone on the line (the character after it is not \0 or \n), we 1833236769Sobrien * do print whatever follows it. 1834236769Sobrien * 1835236769Sobrien * Input: 1836236769Sobrien * job the job whose output needs printing 1837236769Sobrien * finish TRUE if this is the last time we'll be called 1838236769Sobrien * for this job 1839236769Sobrien * 1840236769Sobrien * Results: 1841236769Sobrien * None 1842236769Sobrien * 1843236769Sobrien * Side Effects: 1844236769Sobrien * curPos may be shifted as may the contents of outBuf. 1845236769Sobrien *----------------------------------------------------------------------- 1846236769Sobrien */ 1847236769SobrienSTATIC void 1848236769SobrienJobDoOutput(Job *job, Boolean finish) 1849236769Sobrien{ 1850236769Sobrien Boolean gotNL = FALSE; /* true if got a newline */ 1851236769Sobrien Boolean fbuf; /* true if our buffer filled up */ 1852236769Sobrien int nr; /* number of bytes read */ 1853236769Sobrien int i; /* auxiliary index into outBuf */ 1854236769Sobrien int max; /* limit for i (end of current data) */ 1855236769Sobrien int nRead; /* (Temporary) number of bytes read */ 1856236769Sobrien 1857236769Sobrien /* 1858236769Sobrien * Read as many bytes as will fit in the buffer. 1859236769Sobrien */ 1860236769Sobrienend_loop: 1861236769Sobrien gotNL = FALSE; 1862236769Sobrien fbuf = FALSE; 1863236769Sobrien 1864236769Sobrien nRead = read(job->inPipe, &job->outBuf[job->curPos], 1865236769Sobrien JOB_BUFSIZE - job->curPos); 1866236769Sobrien if (nRead < 0) { 1867236769Sobrien if (errno == EAGAIN) 1868236769Sobrien return; 1869236769Sobrien if (DEBUG(JOB)) { 1870236769Sobrien perror("JobDoOutput(piperead)"); 1871236769Sobrien } 1872236769Sobrien nr = 0; 1873236769Sobrien } else { 1874236769Sobrien nr = nRead; 1875236769Sobrien } 1876236769Sobrien 1877236769Sobrien /* 1878236769Sobrien * If we hit the end-of-file (the job is dead), we must flush its 1879236769Sobrien * remaining output, so pretend we read a newline if there's any 1880236769Sobrien * output remaining in the buffer. 1881236769Sobrien * Also clear the 'finish' flag so we stop looping. 1882236769Sobrien */ 1883236769Sobrien if ((nr == 0) && (job->curPos != 0)) { 1884236769Sobrien job->outBuf[job->curPos] = '\n'; 1885236769Sobrien nr = 1; 1886236769Sobrien finish = FALSE; 1887236769Sobrien } else if (nr == 0) { 1888236769Sobrien finish = FALSE; 1889236769Sobrien } 1890236769Sobrien 1891236769Sobrien /* 1892236769Sobrien * Look for the last newline in the bytes we just got. If there is 1893236769Sobrien * one, break out of the loop with 'i' as its index and gotNL set 1894236769Sobrien * TRUE. 1895236769Sobrien */ 1896236769Sobrien max = job->curPos + nr; 1897236769Sobrien for (i = job->curPos + nr - 1; i >= job->curPos; i--) { 1898236769Sobrien if (job->outBuf[i] == '\n') { 1899236769Sobrien gotNL = TRUE; 1900236769Sobrien break; 1901236769Sobrien } else if (job->outBuf[i] == '\0') { 1902236769Sobrien /* 1903236769Sobrien * Why? 1904236769Sobrien */ 1905236769Sobrien job->outBuf[i] = ' '; 1906236769Sobrien } 1907236769Sobrien } 1908236769Sobrien 1909236769Sobrien if (!gotNL) { 1910236769Sobrien job->curPos += nr; 1911236769Sobrien if (job->curPos == JOB_BUFSIZE) { 1912236769Sobrien /* 1913236769Sobrien * If we've run out of buffer space, we have no choice 1914236769Sobrien * but to print the stuff. sigh. 1915236769Sobrien */ 1916236769Sobrien fbuf = TRUE; 1917236769Sobrien i = job->curPos; 1918236769Sobrien } 1919236769Sobrien } 1920236769Sobrien if (gotNL || fbuf) { 1921236769Sobrien /* 1922236769Sobrien * Need to send the output to the screen. Null terminate it 1923236769Sobrien * first, overwriting the newline character if there was one. 1924236769Sobrien * So long as the line isn't one we should filter (according 1925236769Sobrien * to the shell description), we print the line, preceded 1926236769Sobrien * by a target banner if this target isn't the same as the 1927236769Sobrien * one for which we last printed something. 1928236769Sobrien * The rest of the data in the buffer are then shifted down 1929236769Sobrien * to the start of the buffer and curPos is set accordingly. 1930236769Sobrien */ 1931236769Sobrien job->outBuf[i] = '\0'; 1932236769Sobrien if (i >= job->curPos) { 1933236769Sobrien char *cp; 1934236769Sobrien 1935236769Sobrien cp = JobOutput(job, job->outBuf, &job->outBuf[i], FALSE); 1936236769Sobrien 1937236769Sobrien /* 1938236769Sobrien * There's still more in that thar buffer. This time, though, 1939236769Sobrien * we know there's no newline at the end, so we add one of 1940236769Sobrien * our own free will. 1941236769Sobrien */ 1942236769Sobrien if (*cp != '\0') { 1943236769Sobrien if (!beSilent && job->node != lastNode) { 1944236769Sobrien MESSAGE(stdout, job->node); 1945236769Sobrien lastNode = job->node; 1946236769Sobrien } 1947236769Sobrien#ifdef USE_META 1948236769Sobrien if (useMeta) { 1949236769Sobrien meta_job_output(job, cp, gotNL ? "\n" : ""); 1950236769Sobrien } 1951236769Sobrien#endif 1952236769Sobrien (void)fprintf(stdout, "%s%s", cp, gotNL ? "\n" : ""); 1953236769Sobrien (void)fflush(stdout); 1954236769Sobrien } 1955236769Sobrien } 1956281812Ssjg /* 1957281812Ssjg * max is the last offset still in the buffer. Move any remaining 1958281812Ssjg * characters to the start of the buffer and update the end marker 1959281812Ssjg * curPos. 1960281812Ssjg */ 1961281812Ssjg if (i < max) { 1962281812Ssjg (void)memmove(job->outBuf, &job->outBuf[i + 1], max - (i + 1)); 1963236769Sobrien job->curPos = max - (i + 1); 1964236769Sobrien } else { 1965281812Ssjg assert(i == max); 1966236769Sobrien job->curPos = 0; 1967236769Sobrien } 1968236769Sobrien } 1969236769Sobrien if (finish) { 1970236769Sobrien /* 1971236769Sobrien * If the finish flag is true, we must loop until we hit 1972236769Sobrien * end-of-file on the pipe. This is guaranteed to happen 1973236769Sobrien * eventually since the other end of the pipe is now closed 1974236769Sobrien * (we closed it explicitly and the child has exited). When 1975236769Sobrien * we do get an EOF, finish will be set FALSE and we'll fall 1976236769Sobrien * through and out. 1977236769Sobrien */ 1978236769Sobrien goto end_loop; 1979236769Sobrien } 1980236769Sobrien} 1981236769Sobrien 1982236769Sobrienstatic void 1983236769SobrienJobRun(GNode *targ) 1984236769Sobrien{ 1985236769Sobrien#ifdef notyet 1986236769Sobrien /* 1987236769Sobrien * Unfortunately it is too complicated to run .BEGIN, .END, 1988236769Sobrien * and .INTERRUPT job in the parallel job module. This has 1989236769Sobrien * the nice side effect that it avoids a lot of other problems. 1990236769Sobrien */ 1991236769Sobrien Lst lst = Lst_Init(FALSE); 1992236769Sobrien Lst_AtEnd(lst, targ); 1993236769Sobrien (void)Make_Run(lst); 1994236769Sobrien Lst_Destroy(lst, NULL); 1995236769Sobrien JobStart(targ, JOB_SPECIAL); 1996236769Sobrien while (jobTokensRunning) { 1997236769Sobrien Job_CatchOutput(); 1998236769Sobrien } 1999236769Sobrien#else 2000236769Sobrien Compat_Make(targ, targ); 2001236769Sobrien if (targ->made == ERROR) { 2002236769Sobrien PrintOnError(targ, "\n\nStop."); 2003236769Sobrien exit(1); 2004236769Sobrien } 2005236769Sobrien#endif 2006236769Sobrien} 2007236769Sobrien 2008236769Sobrien/*- 2009236769Sobrien *----------------------------------------------------------------------- 2010236769Sobrien * Job_CatchChildren -- 2011236769Sobrien * Handle the exit of a child. Called from Make_Make. 2012236769Sobrien * 2013236769Sobrien * Input: 2014236769Sobrien * block TRUE if should block on the wait 2015236769Sobrien * 2016236769Sobrien * Results: 2017236769Sobrien * none. 2018236769Sobrien * 2019236769Sobrien * Side Effects: 2020236769Sobrien * The job descriptor is removed from the list of children. 2021236769Sobrien * 2022236769Sobrien * Notes: 2023236769Sobrien * We do waits, blocking or not, according to the wisdom of our 2024236769Sobrien * caller, until there are no more children to report. For each 2025236769Sobrien * job, call JobFinish to finish things off. 2026236769Sobrien * 2027236769Sobrien *----------------------------------------------------------------------- 2028236769Sobrien */ 2029236769Sobrien 2030236769Sobrienvoid 2031236769SobrienJob_CatchChildren(void) 2032236769Sobrien{ 2033236769Sobrien int pid; /* pid of dead child */ 2034236769Sobrien WAIT_T status; /* Exit/termination status */ 2035236769Sobrien 2036236769Sobrien /* 2037236769Sobrien * Don't even bother if we know there's no one around. 2038236769Sobrien */ 2039236769Sobrien if (jobTokensRunning == 0) 2040236769Sobrien return; 2041236769Sobrien 2042236769Sobrien while ((pid = waitpid((pid_t) -1, &status, WNOHANG | WUNTRACED)) > 0) { 2043236769Sobrien if (DEBUG(JOB)) { 2044236769Sobrien (void)fprintf(debug_file, "Process %d exited/stopped status %x.\n", pid, 2045236769Sobrien WAIT_STATUS(status)); 2046236769Sobrien } 2047236769Sobrien JobReapChild(pid, status, TRUE); 2048236769Sobrien } 2049236769Sobrien} 2050236769Sobrien 2051236769Sobrien/* 2052236769Sobrien * It is possible that wait[pid]() was called from elsewhere, 2053236769Sobrien * this lets us reap jobs regardless. 2054236769Sobrien */ 2055236769Sobrienvoid 2056236769SobrienJobReapChild(pid_t pid, WAIT_T status, Boolean isJobs) 2057236769Sobrien{ 2058236769Sobrien Job *job; /* job descriptor for dead child */ 2059236769Sobrien 2060236769Sobrien /* 2061236769Sobrien * Don't even bother if we know there's no one around. 2062236769Sobrien */ 2063236769Sobrien if (jobTokensRunning == 0) 2064236769Sobrien return; 2065236769Sobrien 2066236769Sobrien job = JobFindPid(pid, JOB_ST_RUNNING, isJobs); 2067236769Sobrien if (job == NULL) { 2068236769Sobrien if (isJobs) { 2069236769Sobrien if (!lurking_children) 2070236769Sobrien Error("Child (%d) status %x not in table?", pid, status); 2071236769Sobrien } 2072236769Sobrien return; /* not ours */ 2073236769Sobrien } 2074236769Sobrien if (WIFSTOPPED(status)) { 2075236769Sobrien if (DEBUG(JOB)) { 2076236769Sobrien (void)fprintf(debug_file, "Process %d (%s) stopped.\n", 2077236769Sobrien job->pid, job->node->name); 2078236769Sobrien } 2079236769Sobrien if (!make_suspended) { 2080236769Sobrien switch (WSTOPSIG(status)) { 2081236769Sobrien case SIGTSTP: 2082236769Sobrien (void)printf("*** [%s] Suspended\n", job->node->name); 2083236769Sobrien break; 2084236769Sobrien case SIGSTOP: 2085236769Sobrien (void)printf("*** [%s] Stopped\n", job->node->name); 2086236769Sobrien break; 2087236769Sobrien default: 2088236769Sobrien (void)printf("*** [%s] Stopped -- signal %d\n", 2089236769Sobrien job->node->name, WSTOPSIG(status)); 2090236769Sobrien } 2091236769Sobrien job->job_suspended = 1; 2092236769Sobrien } 2093236769Sobrien (void)fflush(stdout); 2094236769Sobrien return; 2095236769Sobrien } 2096236769Sobrien 2097236769Sobrien job->job_state = JOB_ST_FINISHED; 2098236769Sobrien job->exit_status = WAIT_STATUS(status); 2099236769Sobrien 2100236769Sobrien JobFinish(job, status); 2101236769Sobrien} 2102236769Sobrien 2103236769Sobrien/*- 2104236769Sobrien *----------------------------------------------------------------------- 2105236769Sobrien * Job_CatchOutput -- 2106236769Sobrien * Catch the output from our children, if we're using 2107236769Sobrien * pipes do so. Otherwise just block time until we get a 2108236769Sobrien * signal(most likely a SIGCHLD) since there's no point in 2109236769Sobrien * just spinning when there's nothing to do and the reaping 2110236769Sobrien * of a child can wait for a while. 2111236769Sobrien * 2112236769Sobrien * Results: 2113236769Sobrien * None 2114236769Sobrien * 2115236769Sobrien * Side Effects: 2116236769Sobrien * Output is read from pipes if we're piping. 2117236769Sobrien * ----------------------------------------------------------------------- 2118236769Sobrien */ 2119236769Sobrienvoid 2120236769SobrienJob_CatchOutput(void) 2121236769Sobrien{ 2122236769Sobrien int nready; 2123236769Sobrien Job *job; 2124236769Sobrien int i; 2125236769Sobrien 2126236769Sobrien (void)fflush(stdout); 2127236769Sobrien 2128236769Sobrien /* The first fd in the list is the job token pipe */ 2129249033Ssjg do { 2130249033Ssjg nready = poll(fds + 1 - wantToken, nfds - 1 + wantToken, POLL_MSEC); 2131249033Ssjg } while (nready < 0 && errno == EINTR); 2132236769Sobrien 2133249033Ssjg if (nready < 0) 2134249033Ssjg Punt("poll: %s", strerror(errno)); 2135249033Ssjg 2136249033Ssjg if (nready > 0 && readyfd(&childExitJob)) { 2137236769Sobrien char token = 0; 2138249033Ssjg ssize_t count; 2139249033Ssjg count = read(childExitJob.inPipe, &token, 1); 2140249033Ssjg switch (count) { 2141249033Ssjg case 0: 2142249033Ssjg Punt("unexpected eof on token pipe"); 2143249033Ssjg case -1: 2144249033Ssjg Punt("token pipe read: %s", strerror(errno)); 2145249033Ssjg case 1: 2146249033Ssjg if (token == DO_JOB_RESUME[0]) 2147249033Ssjg /* Complete relay requested from our SIGCONT handler */ 2148249033Ssjg JobRestartJobs(); 2149249033Ssjg break; 2150249033Ssjg default: 2151249033Ssjg abort(); 2152249033Ssjg } 2153249033Ssjg --nready; 2154236769Sobrien } 2155236769Sobrien 2156249033Ssjg Job_CatchChildren(); 2157249033Ssjg if (nready == 0) 2158249033Ssjg return; 2159236769Sobrien 2160236769Sobrien for (i = 2; i < nfds; i++) { 2161236769Sobrien if (!fds[i].revents) 2162236769Sobrien continue; 2163236769Sobrien job = jobfds[i]; 2164249033Ssjg if (job->job_state == JOB_ST_RUNNING) 2165249033Ssjg JobDoOutput(job, FALSE); 2166249033Ssjg if (--nready == 0) 2167249033Ssjg return; 2168236769Sobrien } 2169236769Sobrien} 2170236769Sobrien 2171236769Sobrien/*- 2172236769Sobrien *----------------------------------------------------------------------- 2173236769Sobrien * Job_Make -- 2174236769Sobrien * Start the creation of a target. Basically a front-end for 2175236769Sobrien * JobStart used by the Make module. 2176236769Sobrien * 2177236769Sobrien * Results: 2178236769Sobrien * None. 2179236769Sobrien * 2180236769Sobrien * Side Effects: 2181236769Sobrien * Another job is started. 2182236769Sobrien * 2183236769Sobrien *----------------------------------------------------------------------- 2184236769Sobrien */ 2185236769Sobrienvoid 2186236769SobrienJob_Make(GNode *gn) 2187236769Sobrien{ 2188236769Sobrien (void)JobStart(gn, 0); 2189236769Sobrien} 2190236769Sobrien 2191236769Sobrienvoid 2192236769SobrienShell_Init(void) 2193236769Sobrien{ 2194236769Sobrien if (shellPath == NULL) { 2195236769Sobrien /* 2196236769Sobrien * We are using the default shell, which may be an absolute 2197236769Sobrien * path if DEFSHELL_CUSTOM is defined. 2198236769Sobrien */ 2199236769Sobrien shellName = commandShell->name; 2200236769Sobrien#ifdef DEFSHELL_CUSTOM 2201236769Sobrien if (*shellName == '/') { 2202236769Sobrien shellPath = shellName; 2203236769Sobrien shellName = strrchr(shellPath, '/'); 2204236769Sobrien shellName++; 2205236769Sobrien } else 2206236769Sobrien#endif 2207236769Sobrien shellPath = str_concat(_PATH_DEFSHELLDIR, shellName, STR_ADDSLASH); 2208236769Sobrien } 2209236769Sobrien if (commandShell->exit == NULL) { 2210236769Sobrien commandShell->exit = ""; 2211236769Sobrien } 2212236769Sobrien if (commandShell->echo == NULL) { 2213236769Sobrien commandShell->echo = ""; 2214236769Sobrien } 2215253883Ssjg if (commandShell->hasErrCtl && *commandShell->exit) { 2216253883Ssjg if (shellErrFlag && 2217253883Ssjg strcmp(commandShell->exit, &shellErrFlag[1]) != 0) { 2218253883Ssjg free(shellErrFlag); 2219253883Ssjg shellErrFlag = NULL; 2220253883Ssjg } 2221253883Ssjg if (!shellErrFlag) { 2222253883Ssjg int n = strlen(commandShell->exit) + 2; 2223253883Ssjg 2224253883Ssjg shellErrFlag = bmake_malloc(n); 2225253883Ssjg if (shellErrFlag) { 2226253883Ssjg snprintf(shellErrFlag, n, "-%s", commandShell->exit); 2227253883Ssjg } 2228253883Ssjg } 2229253883Ssjg } else if (shellErrFlag) { 2230253883Ssjg free(shellErrFlag); 2231253883Ssjg shellErrFlag = NULL; 2232253883Ssjg } 2233236769Sobrien} 2234236769Sobrien 2235236769Sobrien/*- 2236236769Sobrien * Returns the string literal that is used in the current command shell 2237236769Sobrien * to produce a newline character. 2238236769Sobrien */ 2239236769Sobrienconst char * 2240236769SobrienShell_GetNewline(void) 2241236769Sobrien{ 2242236769Sobrien 2243236769Sobrien return commandShell->newline; 2244236769Sobrien} 2245236769Sobrien 2246236769Sobrienvoid 2247236769SobrienJob_SetPrefix(void) 2248236769Sobrien{ 2249236769Sobrien 2250236769Sobrien if (targPrefix) { 2251236769Sobrien free(targPrefix); 2252236769Sobrien } else if (!Var_Exists(MAKE_JOB_PREFIX, VAR_GLOBAL)) { 2253236769Sobrien Var_Set(MAKE_JOB_PREFIX, "---", VAR_GLOBAL, 0); 2254236769Sobrien } 2255236769Sobrien 2256289842Ssjg targPrefix = Var_Subst(NULL, "${" MAKE_JOB_PREFIX "}", 2257296637Ssjg VAR_GLOBAL, VARF_WANTRES); 2258236769Sobrien} 2259236769Sobrien 2260236769Sobrien/*- 2261236769Sobrien *----------------------------------------------------------------------- 2262236769Sobrien * Job_Init -- 2263236769Sobrien * Initialize the process module 2264236769Sobrien * 2265236769Sobrien * Input: 2266236769Sobrien * 2267236769Sobrien * Results: 2268236769Sobrien * none 2269236769Sobrien * 2270236769Sobrien * Side Effects: 2271236769Sobrien * lists and counters are initialized 2272236769Sobrien *----------------------------------------------------------------------- 2273236769Sobrien */ 2274236769Sobrienvoid 2275236769SobrienJob_Init(void) 2276236769Sobrien{ 2277253925Ssjg Job_SetPrefix(); 2278236769Sobrien /* Allocate space for all the job info */ 2279236769Sobrien job_table = bmake_malloc(maxJobs * sizeof *job_table); 2280236769Sobrien memset(job_table, 0, maxJobs * sizeof *job_table); 2281236769Sobrien job_table_end = job_table + maxJobs; 2282236769Sobrien wantToken = 0; 2283236769Sobrien 2284236769Sobrien aborting = 0; 2285236769Sobrien errors = 0; 2286236769Sobrien 2287236769Sobrien lastNode = NULL; 2288236769Sobrien 2289254419Ssjg Always_pass_job_queue = getBoolean(MAKE_ALWAYS_PASS_JOB_QUEUE, 2290254419Ssjg Always_pass_job_queue); 2291254419Ssjg 2292255285Ssjg Job_error_token = getBoolean(MAKE_JOB_ERROR_TOKEN, Job_error_token); 2293255285Ssjg 2294255285Ssjg 2295236769Sobrien /* 2296236769Sobrien * There is a non-zero chance that we already have children. 2297236769Sobrien * eg after 'make -f- <<EOF' 2298236769Sobrien * Since their termination causes a 'Child (pid) not in table' message, 2299236769Sobrien * Collect the status of any that are already dead, and suppress the 2300236769Sobrien * error message if there are any undead ones. 2301236769Sobrien */ 2302236769Sobrien for (;;) { 2303236769Sobrien int rval, status; 2304236769Sobrien rval = waitpid((pid_t) -1, &status, WNOHANG); 2305236769Sobrien if (rval > 0) 2306236769Sobrien continue; 2307236769Sobrien if (rval == 0) 2308236769Sobrien lurking_children = 1; 2309236769Sobrien break; 2310236769Sobrien } 2311236769Sobrien 2312236769Sobrien Shell_Init(); 2313236769Sobrien 2314236769Sobrien JobCreatePipe(&childExitJob, 3); 2315236769Sobrien 2316236769Sobrien /* We can only need to wait for tokens, children and output from each job */ 2317236769Sobrien fds = bmake_malloc(sizeof (*fds) * (2 + maxJobs)); 2318236769Sobrien jobfds = bmake_malloc(sizeof (*jobfds) * (2 + maxJobs)); 2319236769Sobrien 2320236769Sobrien /* These are permanent entries and take slots 0 and 1 */ 2321236769Sobrien watchfd(&tokenWaitJob); 2322236769Sobrien watchfd(&childExitJob); 2323236769Sobrien 2324236769Sobrien sigemptyset(&caught_signals); 2325236769Sobrien /* 2326236769Sobrien * Install a SIGCHLD handler. 2327236769Sobrien */ 2328236769Sobrien (void)bmake_signal(SIGCHLD, JobChildSig); 2329236769Sobrien sigaddset(&caught_signals, SIGCHLD); 2330236769Sobrien 2331236769Sobrien#define ADDSIG(s,h) \ 2332236769Sobrien if (bmake_signal(s, SIG_IGN) != SIG_IGN) { \ 2333236769Sobrien sigaddset(&caught_signals, s); \ 2334236769Sobrien (void)bmake_signal(s, h); \ 2335236769Sobrien } 2336236769Sobrien 2337236769Sobrien /* 2338236769Sobrien * Catch the four signals that POSIX specifies if they aren't ignored. 2339236769Sobrien * JobPassSig will take care of calling JobInterrupt if appropriate. 2340236769Sobrien */ 2341236769Sobrien ADDSIG(SIGINT, JobPassSig_int) 2342236769Sobrien ADDSIG(SIGHUP, JobPassSig_term) 2343236769Sobrien ADDSIG(SIGTERM, JobPassSig_term) 2344236769Sobrien ADDSIG(SIGQUIT, JobPassSig_term) 2345236769Sobrien 2346236769Sobrien /* 2347236769Sobrien * There are additional signals that need to be caught and passed if 2348236769Sobrien * either the export system wants to be told directly of signals or if 2349236769Sobrien * we're giving each job its own process group (since then it won't get 2350236769Sobrien * signals from the terminal driver as we own the terminal) 2351236769Sobrien */ 2352236769Sobrien ADDSIG(SIGTSTP, JobPassSig_suspend) 2353236769Sobrien ADDSIG(SIGTTOU, JobPassSig_suspend) 2354236769Sobrien ADDSIG(SIGTTIN, JobPassSig_suspend) 2355236769Sobrien ADDSIG(SIGWINCH, JobCondPassSig) 2356236769Sobrien ADDSIG(SIGCONT, JobContinueSig) 2357236769Sobrien#undef ADDSIG 2358236769Sobrien 2359249033Ssjg (void)Job_RunTarget(".BEGIN", NULL); 2360236769Sobrien postCommands = Targ_FindNode(".END", TARG_CREATE); 2361236769Sobrien} 2362236769Sobrien 2363236769Sobrienstatic void JobSigReset(void) 2364236769Sobrien{ 2365236769Sobrien#define DELSIG(s) \ 2366236769Sobrien if (sigismember(&caught_signals, s)) { \ 2367236769Sobrien (void)bmake_signal(s, SIG_DFL); \ 2368236769Sobrien } 2369236769Sobrien 2370236769Sobrien DELSIG(SIGINT) 2371236769Sobrien DELSIG(SIGHUP) 2372236769Sobrien DELSIG(SIGQUIT) 2373236769Sobrien DELSIG(SIGTERM) 2374236769Sobrien DELSIG(SIGTSTP) 2375236769Sobrien DELSIG(SIGTTOU) 2376236769Sobrien DELSIG(SIGTTIN) 2377236769Sobrien DELSIG(SIGWINCH) 2378236769Sobrien DELSIG(SIGCONT) 2379236769Sobrien#undef DELSIG 2380236769Sobrien (void)bmake_signal(SIGCHLD, SIG_DFL); 2381236769Sobrien} 2382236769Sobrien 2383236769Sobrien/*- 2384236769Sobrien *----------------------------------------------------------------------- 2385236769Sobrien * JobMatchShell -- 2386236769Sobrien * Find a shell in 'shells' given its name. 2387236769Sobrien * 2388236769Sobrien * Results: 2389236769Sobrien * A pointer to the Shell structure. 2390236769Sobrien * 2391236769Sobrien * Side Effects: 2392236769Sobrien * None. 2393236769Sobrien * 2394236769Sobrien *----------------------------------------------------------------------- 2395236769Sobrien */ 2396236769Sobrienstatic Shell * 2397236769SobrienJobMatchShell(const char *name) 2398236769Sobrien{ 2399236769Sobrien Shell *sh; 2400236769Sobrien 2401236769Sobrien for (sh = shells; sh->name != NULL; sh++) { 2402236769Sobrien if (strcmp(name, sh->name) == 0) 2403236769Sobrien return (sh); 2404236769Sobrien } 2405236769Sobrien return NULL; 2406236769Sobrien} 2407236769Sobrien 2408236769Sobrien/*- 2409236769Sobrien *----------------------------------------------------------------------- 2410236769Sobrien * Job_ParseShell -- 2411236769Sobrien * Parse a shell specification and set up commandShell, shellPath 2412236769Sobrien * and shellName appropriately. 2413236769Sobrien * 2414236769Sobrien * Input: 2415236769Sobrien * line The shell spec 2416236769Sobrien * 2417236769Sobrien * Results: 2418236769Sobrien * FAILURE if the specification was incorrect. 2419236769Sobrien * 2420236769Sobrien * Side Effects: 2421236769Sobrien * commandShell points to a Shell structure (either predefined or 2422236769Sobrien * created from the shell spec), shellPath is the full path of the 2423236769Sobrien * shell described by commandShell, while shellName is just the 2424236769Sobrien * final component of shellPath. 2425236769Sobrien * 2426236769Sobrien * Notes: 2427236769Sobrien * A shell specification consists of a .SHELL target, with dependency 2428236769Sobrien * operator, followed by a series of blank-separated words. Double 2429236769Sobrien * quotes can be used to use blanks in words. A backslash escapes 2430236769Sobrien * anything (most notably a double-quote and a space) and 2431236769Sobrien * provides the functionality it does in C. Each word consists of 2432236769Sobrien * keyword and value separated by an equal sign. There should be no 2433236769Sobrien * unnecessary spaces in the word. The keywords are as follows: 2434236769Sobrien * name Name of shell. 2435236769Sobrien * path Location of shell. 2436236769Sobrien * quiet Command to turn off echoing. 2437236769Sobrien * echo Command to turn echoing on 2438236769Sobrien * filter Result of turning off echoing that shouldn't be 2439236769Sobrien * printed. 2440236769Sobrien * echoFlag Flag to turn echoing on at the start 2441236769Sobrien * errFlag Flag to turn error checking on at the start 2442236769Sobrien * hasErrCtl True if shell has error checking control 2443236769Sobrien * newline String literal to represent a newline char 2444236769Sobrien * check Command to turn on error checking if hasErrCtl 2445236769Sobrien * is TRUE or template of command to echo a command 2446236769Sobrien * for which error checking is off if hasErrCtl is 2447236769Sobrien * FALSE. 2448236769Sobrien * ignore Command to turn off error checking if hasErrCtl 2449236769Sobrien * is TRUE or template of command to execute a 2450236769Sobrien * command so as to ignore any errors it returns if 2451236769Sobrien * hasErrCtl is FALSE. 2452236769Sobrien * 2453236769Sobrien *----------------------------------------------------------------------- 2454236769Sobrien */ 2455236769SobrienReturnStatus 2456236769SobrienJob_ParseShell(char *line) 2457236769Sobrien{ 2458236769Sobrien char **words; 2459236769Sobrien char **argv; 2460236769Sobrien int argc; 2461236769Sobrien char *path; 2462236769Sobrien Shell newShell; 2463236769Sobrien Boolean fullSpec = FALSE; 2464236769Sobrien Shell *sh; 2465236769Sobrien 2466236769Sobrien while (isspace((unsigned char)*line)) { 2467236769Sobrien line++; 2468236769Sobrien } 2469236769Sobrien 2470296637Ssjg free(UNCONST(shellArgv)); 2471236769Sobrien 2472236769Sobrien memset(&newShell, 0, sizeof(newShell)); 2473236769Sobrien 2474236769Sobrien /* 2475236769Sobrien * Parse the specification by keyword 2476236769Sobrien */ 2477236769Sobrien words = brk_string(line, &argc, TRUE, &path); 2478236769Sobrien if (words == NULL) { 2479236769Sobrien Error("Unterminated quoted string [%s]", line); 2480236769Sobrien return FAILURE; 2481236769Sobrien } 2482236769Sobrien shellArgv = path; 2483236769Sobrien 2484236769Sobrien for (path = NULL, argv = words; argc != 0; argc--, argv++) { 2485236769Sobrien if (strncmp(*argv, "path=", 5) == 0) { 2486236769Sobrien path = &argv[0][5]; 2487236769Sobrien } else if (strncmp(*argv, "name=", 5) == 0) { 2488236769Sobrien newShell.name = &argv[0][5]; 2489236769Sobrien } else { 2490236769Sobrien if (strncmp(*argv, "quiet=", 6) == 0) { 2491236769Sobrien newShell.echoOff = &argv[0][6]; 2492236769Sobrien } else if (strncmp(*argv, "echo=", 5) == 0) { 2493236769Sobrien newShell.echoOn = &argv[0][5]; 2494236769Sobrien } else if (strncmp(*argv, "filter=", 7) == 0) { 2495236769Sobrien newShell.noPrint = &argv[0][7]; 2496236769Sobrien newShell.noPLen = strlen(newShell.noPrint); 2497236769Sobrien } else if (strncmp(*argv, "echoFlag=", 9) == 0) { 2498236769Sobrien newShell.echo = &argv[0][9]; 2499236769Sobrien } else if (strncmp(*argv, "errFlag=", 8) == 0) { 2500236769Sobrien newShell.exit = &argv[0][8]; 2501236769Sobrien } else if (strncmp(*argv, "hasErrCtl=", 10) == 0) { 2502236769Sobrien char c = argv[0][10]; 2503236769Sobrien newShell.hasErrCtl = !((c != 'Y') && (c != 'y') && 2504236769Sobrien (c != 'T') && (c != 't')); 2505236769Sobrien } else if (strncmp(*argv, "newline=", 8) == 0) { 2506236769Sobrien newShell.newline = &argv[0][8]; 2507236769Sobrien } else if (strncmp(*argv, "check=", 6) == 0) { 2508236769Sobrien newShell.errCheck = &argv[0][6]; 2509236769Sobrien } else if (strncmp(*argv, "ignore=", 7) == 0) { 2510236769Sobrien newShell.ignErr = &argv[0][7]; 2511236769Sobrien } else if (strncmp(*argv, "errout=", 7) == 0) { 2512236769Sobrien newShell.errOut = &argv[0][7]; 2513236769Sobrien } else if (strncmp(*argv, "comment=", 8) == 0) { 2514236769Sobrien newShell.commentChar = argv[0][8]; 2515236769Sobrien } else { 2516236769Sobrien Parse_Error(PARSE_FATAL, "Unknown keyword \"%s\"", 2517236769Sobrien *argv); 2518236769Sobrien free(words); 2519236769Sobrien return(FAILURE); 2520236769Sobrien } 2521236769Sobrien fullSpec = TRUE; 2522236769Sobrien } 2523236769Sobrien } 2524236769Sobrien 2525236769Sobrien if (path == NULL) { 2526236769Sobrien /* 2527236769Sobrien * If no path was given, the user wants one of the pre-defined shells, 2528236769Sobrien * yes? So we find the one s/he wants with the help of JobMatchShell 2529236769Sobrien * and set things up the right way. shellPath will be set up by 2530238152Sobrien * Shell_Init. 2531236769Sobrien */ 2532236769Sobrien if (newShell.name == NULL) { 2533236769Sobrien Parse_Error(PARSE_FATAL, "Neither path nor name specified"); 2534236769Sobrien free(words); 2535236769Sobrien return(FAILURE); 2536236769Sobrien } else { 2537236769Sobrien if ((sh = JobMatchShell(newShell.name)) == NULL) { 2538236769Sobrien Parse_Error(PARSE_WARNING, "%s: No matching shell", 2539236769Sobrien newShell.name); 2540236769Sobrien free(words); 2541236769Sobrien return(FAILURE); 2542236769Sobrien } 2543236769Sobrien commandShell = sh; 2544236769Sobrien shellName = newShell.name; 2545238152Sobrien if (shellPath) { 2546238152Sobrien /* Shell_Init has already been called! Do it again. */ 2547238152Sobrien free(UNCONST(shellPath)); 2548238152Sobrien shellPath = NULL; 2549238152Sobrien Shell_Init(); 2550238152Sobrien } 2551236769Sobrien } 2552236769Sobrien } else { 2553236769Sobrien /* 2554236769Sobrien * The user provided a path. If s/he gave nothing else (fullSpec is 2555236769Sobrien * FALSE), try and find a matching shell in the ones we know of. 2556236769Sobrien * Else we just take the specification at its word and copy it 2557236769Sobrien * to a new location. In either case, we need to record the 2558236769Sobrien * path the user gave for the shell. 2559236769Sobrien */ 2560236769Sobrien shellPath = path; 2561236769Sobrien path = strrchr(path, '/'); 2562236769Sobrien if (path == NULL) { 2563236769Sobrien path = UNCONST(shellPath); 2564236769Sobrien } else { 2565236769Sobrien path += 1; 2566236769Sobrien } 2567236769Sobrien if (newShell.name != NULL) { 2568236769Sobrien shellName = newShell.name; 2569236769Sobrien } else { 2570236769Sobrien shellName = path; 2571236769Sobrien } 2572236769Sobrien if (!fullSpec) { 2573236769Sobrien if ((sh = JobMatchShell(shellName)) == NULL) { 2574236769Sobrien Parse_Error(PARSE_WARNING, "%s: No matching shell", 2575236769Sobrien shellName); 2576236769Sobrien free(words); 2577236769Sobrien return(FAILURE); 2578236769Sobrien } 2579236769Sobrien commandShell = sh; 2580236769Sobrien } else { 2581236769Sobrien commandShell = bmake_malloc(sizeof(Shell)); 2582236769Sobrien *commandShell = newShell; 2583236769Sobrien } 2584253883Ssjg /* this will take care of shellErrFlag */ 2585253883Ssjg Shell_Init(); 2586236769Sobrien } 2587236769Sobrien 2588236769Sobrien if (commandShell->echoOn && commandShell->echoOff) { 2589236769Sobrien commandShell->hasEchoCtl = TRUE; 2590236769Sobrien } 2591236769Sobrien 2592236769Sobrien if (!commandShell->hasErrCtl) { 2593236769Sobrien if (commandShell->errCheck == NULL) { 2594236769Sobrien commandShell->errCheck = ""; 2595236769Sobrien } 2596236769Sobrien if (commandShell->ignErr == NULL) { 2597236769Sobrien commandShell->ignErr = "%s\n"; 2598236769Sobrien } 2599236769Sobrien } 2600236769Sobrien 2601236769Sobrien /* 2602236769Sobrien * Do not free up the words themselves, since they might be in use by the 2603236769Sobrien * shell specification. 2604236769Sobrien */ 2605236769Sobrien free(words); 2606236769Sobrien return SUCCESS; 2607236769Sobrien} 2608236769Sobrien 2609236769Sobrien/*- 2610236769Sobrien *----------------------------------------------------------------------- 2611236769Sobrien * JobInterrupt -- 2612236769Sobrien * Handle the receipt of an interrupt. 2613236769Sobrien * 2614236769Sobrien * Input: 2615236769Sobrien * runINTERRUPT Non-zero if commands for the .INTERRUPT target 2616236769Sobrien * should be executed 2617236769Sobrien * signo signal received 2618236769Sobrien * 2619236769Sobrien * Results: 2620236769Sobrien * None 2621236769Sobrien * 2622236769Sobrien * Side Effects: 2623236769Sobrien * All children are killed. Another job will be started if the 2624236769Sobrien * .INTERRUPT target was given. 2625236769Sobrien *----------------------------------------------------------------------- 2626236769Sobrien */ 2627236769Sobrienstatic void 2628236769SobrienJobInterrupt(int runINTERRUPT, int signo) 2629236769Sobrien{ 2630236769Sobrien Job *job; /* job descriptor in that element */ 2631236769Sobrien GNode *interrupt; /* the node describing the .INTERRUPT target */ 2632236769Sobrien sigset_t mask; 2633236769Sobrien GNode *gn; 2634236769Sobrien 2635236769Sobrien aborting = ABORT_INTERRUPT; 2636236769Sobrien 2637236769Sobrien JobSigLock(&mask); 2638236769Sobrien 2639236769Sobrien for (job = job_table; job < job_table_end; job++) { 2640236769Sobrien if (job->job_state != JOB_ST_RUNNING) 2641236769Sobrien continue; 2642236769Sobrien 2643236769Sobrien gn = job->node; 2644236769Sobrien 2645319884Ssjg JobDeleteTarget(gn); 2646236769Sobrien if (job->pid) { 2647236769Sobrien if (DEBUG(JOB)) { 2648236769Sobrien (void)fprintf(debug_file, 2649236769Sobrien "JobInterrupt passing signal %d to child %d.\n", 2650236769Sobrien signo, job->pid); 2651236769Sobrien } 2652236769Sobrien KILLPG(job->pid, signo); 2653236769Sobrien } 2654236769Sobrien } 2655236769Sobrien 2656236769Sobrien JobSigUnlock(&mask); 2657236769Sobrien 2658236769Sobrien if (runINTERRUPT && !touchFlag) { 2659236769Sobrien interrupt = Targ_FindNode(".INTERRUPT", TARG_NOCREATE); 2660236769Sobrien if (interrupt != NULL) { 2661236769Sobrien ignoreErrors = FALSE; 2662236769Sobrien JobRun(interrupt); 2663236769Sobrien } 2664236769Sobrien } 2665236769Sobrien Trace_Log(MAKEINTR, 0); 2666236769Sobrien exit(signo); 2667236769Sobrien} 2668236769Sobrien 2669236769Sobrien/* 2670236769Sobrien *----------------------------------------------------------------------- 2671236769Sobrien * Job_Finish -- 2672236769Sobrien * Do final processing such as the running of the commands 2673236769Sobrien * attached to the .END target. 2674236769Sobrien * 2675236769Sobrien * Results: 2676236769Sobrien * Number of errors reported. 2677236769Sobrien * 2678236769Sobrien * Side Effects: 2679236769Sobrien * None. 2680236769Sobrien *----------------------------------------------------------------------- 2681236769Sobrien */ 2682236769Sobrienint 2683236769SobrienJob_Finish(void) 2684236769Sobrien{ 2685236769Sobrien if (postCommands != NULL && 2686236769Sobrien (!Lst_IsEmpty(postCommands->commands) || 2687236769Sobrien !Lst_IsEmpty(postCommands->children))) { 2688236769Sobrien if (errors) { 2689236769Sobrien Error("Errors reported so .END ignored"); 2690236769Sobrien } else { 2691236769Sobrien JobRun(postCommands); 2692236769Sobrien } 2693236769Sobrien } 2694236769Sobrien return(errors); 2695236769Sobrien} 2696236769Sobrien 2697236769Sobrien/*- 2698236769Sobrien *----------------------------------------------------------------------- 2699236769Sobrien * Job_End -- 2700236769Sobrien * Cleanup any memory used by the jobs module 2701236769Sobrien * 2702236769Sobrien * Results: 2703236769Sobrien * None. 2704236769Sobrien * 2705236769Sobrien * Side Effects: 2706236769Sobrien * Memory is freed 2707236769Sobrien *----------------------------------------------------------------------- 2708236769Sobrien */ 2709236769Sobrienvoid 2710236769SobrienJob_End(void) 2711236769Sobrien{ 2712236769Sobrien#ifdef CLEANUP 2713296637Ssjg free(shellArgv); 2714236769Sobrien#endif 2715236769Sobrien} 2716236769Sobrien 2717236769Sobrien/*- 2718236769Sobrien *----------------------------------------------------------------------- 2719236769Sobrien * Job_Wait -- 2720236769Sobrien * Waits for all running jobs to finish and returns. Sets 'aborting' 2721236769Sobrien * to ABORT_WAIT to prevent other jobs from starting. 2722236769Sobrien * 2723236769Sobrien * Results: 2724236769Sobrien * None. 2725236769Sobrien * 2726236769Sobrien * Side Effects: 2727236769Sobrien * Currently running jobs finish. 2728236769Sobrien * 2729236769Sobrien *----------------------------------------------------------------------- 2730236769Sobrien */ 2731236769Sobrienvoid 2732236769SobrienJob_Wait(void) 2733236769Sobrien{ 2734236769Sobrien aborting = ABORT_WAIT; 2735236769Sobrien while (jobTokensRunning != 0) { 2736236769Sobrien Job_CatchOutput(); 2737236769Sobrien } 2738236769Sobrien aborting = 0; 2739236769Sobrien} 2740236769Sobrien 2741236769Sobrien/*- 2742236769Sobrien *----------------------------------------------------------------------- 2743236769Sobrien * Job_AbortAll -- 2744236769Sobrien * Abort all currently running jobs without handling output or anything. 2745236769Sobrien * This function is to be called only in the event of a major 2746236769Sobrien * error. Most definitely NOT to be called from JobInterrupt. 2747236769Sobrien * 2748236769Sobrien * Results: 2749236769Sobrien * None 2750236769Sobrien * 2751236769Sobrien * Side Effects: 2752236769Sobrien * All children are killed, not just the firstborn 2753236769Sobrien *----------------------------------------------------------------------- 2754236769Sobrien */ 2755236769Sobrienvoid 2756236769SobrienJob_AbortAll(void) 2757236769Sobrien{ 2758236769Sobrien Job *job; /* the job descriptor in that element */ 2759236769Sobrien WAIT_T foo; 2760236769Sobrien 2761236769Sobrien aborting = ABORT_ERROR; 2762236769Sobrien 2763236769Sobrien if (jobTokensRunning) { 2764236769Sobrien for (job = job_table; job < job_table_end; job++) { 2765236769Sobrien if (job->job_state != JOB_ST_RUNNING) 2766236769Sobrien continue; 2767236769Sobrien /* 2768236769Sobrien * kill the child process with increasingly drastic signals to make 2769236769Sobrien * darn sure it's dead. 2770236769Sobrien */ 2771236769Sobrien KILLPG(job->pid, SIGINT); 2772236769Sobrien KILLPG(job->pid, SIGKILL); 2773236769Sobrien } 2774236769Sobrien } 2775236769Sobrien 2776236769Sobrien /* 2777236769Sobrien * Catch as many children as want to report in at first, then give up 2778236769Sobrien */ 2779236769Sobrien while (waitpid((pid_t) -1, &foo, WNOHANG) > 0) 2780236769Sobrien continue; 2781236769Sobrien} 2782236769Sobrien 2783236769Sobrien 2784236769Sobrien/*- 2785236769Sobrien *----------------------------------------------------------------------- 2786236769Sobrien * JobRestartJobs -- 2787236769Sobrien * Tries to restart stopped jobs if there are slots available. 2788236769Sobrien * Called in process context in response to a SIGCONT. 2789236769Sobrien * 2790236769Sobrien * Results: 2791236769Sobrien * None. 2792236769Sobrien * 2793236769Sobrien * Side Effects: 2794236769Sobrien * Resumes jobs. 2795236769Sobrien * 2796236769Sobrien *----------------------------------------------------------------------- 2797236769Sobrien */ 2798236769Sobrienstatic void 2799236769SobrienJobRestartJobs(void) 2800236769Sobrien{ 2801236769Sobrien Job *job; 2802236769Sobrien 2803236769Sobrien for (job = job_table; job < job_table_end; job++) { 2804236769Sobrien if (job->job_state == JOB_ST_RUNNING && 2805236769Sobrien (make_suspended || job->job_suspended)) { 2806236769Sobrien if (DEBUG(JOB)) { 2807236769Sobrien (void)fprintf(debug_file, "Restarting stopped job pid %d.\n", 2808236769Sobrien job->pid); 2809236769Sobrien } 2810236769Sobrien if (job->job_suspended) { 2811236769Sobrien (void)printf("*** [%s] Continued\n", job->node->name); 2812236769Sobrien (void)fflush(stdout); 2813236769Sobrien } 2814236769Sobrien job->job_suspended = 0; 2815236769Sobrien if (KILLPG(job->pid, SIGCONT) != 0 && DEBUG(JOB)) { 2816236769Sobrien fprintf(debug_file, "Failed to send SIGCONT to %d\n", job->pid); 2817236769Sobrien } 2818236769Sobrien } 2819236769Sobrien if (job->job_state == JOB_ST_FINISHED) 2820236769Sobrien /* Job exit deferred after calling waitpid() in a signal handler */ 2821236769Sobrien JobFinish(job, job->exit_status); 2822236769Sobrien } 2823236769Sobrien make_suspended = 0; 2824236769Sobrien} 2825236769Sobrien 2826236769Sobrienstatic void 2827236769Sobrienwatchfd(Job *job) 2828236769Sobrien{ 2829236769Sobrien if (job->inPollfd != NULL) 2830236769Sobrien Punt("Watching watched job"); 2831236769Sobrien 2832236769Sobrien fds[nfds].fd = job->inPipe; 2833236769Sobrien fds[nfds].events = POLLIN; 2834236769Sobrien jobfds[nfds] = job; 2835236769Sobrien job->inPollfd = &fds[nfds]; 2836236769Sobrien nfds++; 2837236769Sobrien} 2838236769Sobrien 2839236769Sobrienstatic void 2840236769Sobrienclearfd(Job *job) 2841236769Sobrien{ 2842236769Sobrien int i; 2843236769Sobrien if (job->inPollfd == NULL) 2844236769Sobrien Punt("Unwatching unwatched job"); 2845236769Sobrien i = job->inPollfd - fds; 2846236769Sobrien nfds--; 2847236769Sobrien /* 2848236769Sobrien * Move last job in table into hole made by dead job. 2849236769Sobrien */ 2850236769Sobrien if (nfds != i) { 2851236769Sobrien fds[i] = fds[nfds]; 2852236769Sobrien jobfds[i] = jobfds[nfds]; 2853236769Sobrien jobfds[i]->inPollfd = &fds[i]; 2854236769Sobrien } 2855236769Sobrien job->inPollfd = NULL; 2856236769Sobrien} 2857236769Sobrien 2858236769Sobrienstatic int 2859236769Sobrienreadyfd(Job *job) 2860236769Sobrien{ 2861236769Sobrien if (job->inPollfd == NULL) 2862236769Sobrien Punt("Polling unwatched job"); 2863236769Sobrien return (job->inPollfd->revents & POLLIN) != 0; 2864236769Sobrien} 2865236769Sobrien 2866236769Sobrien/*- 2867236769Sobrien *----------------------------------------------------------------------- 2868236769Sobrien * JobTokenAdd -- 2869236769Sobrien * Put a token into the job pipe so that some make process can start 2870236769Sobrien * another job. 2871236769Sobrien * 2872236769Sobrien * Side Effects: 2873236769Sobrien * Allows more build jobs to be spawned somewhere. 2874236769Sobrien * 2875236769Sobrien *----------------------------------------------------------------------- 2876236769Sobrien */ 2877236769Sobrien 2878236769Sobrienstatic void 2879236769SobrienJobTokenAdd(void) 2880236769Sobrien{ 2881236769Sobrien char tok = JOB_TOKENS[aborting], tok1; 2882236769Sobrien 2883255285Ssjg if (!Job_error_token && aborting == ABORT_ERROR) { 2884255285Ssjg if (jobTokensRunning == 0) 2885255285Ssjg return; 2886255285Ssjg tok = '+'; /* no error token */ 2887255285Ssjg } 2888255285Ssjg 2889236769Sobrien /* If we are depositing an error token flush everything else */ 2890236769Sobrien while (tok != '+' && read(tokenWaitJob.inPipe, &tok1, 1) == 1) 2891236769Sobrien continue; 2892236769Sobrien 2893236769Sobrien if (DEBUG(JOB)) 2894236769Sobrien fprintf(debug_file, "(%d) aborting %d, deposit token %c\n", 2895255285Ssjg getpid(), aborting, tok); 2896246223Ssjg while (write(tokenWaitJob.outPipe, &tok, 1) == -1 && errno == EAGAIN) 2897246223Ssjg continue; 2898236769Sobrien} 2899236769Sobrien 2900236769Sobrien/*- 2901236769Sobrien *----------------------------------------------------------------------- 2902236769Sobrien * Job_ServerStartTokenAdd -- 2903236769Sobrien * Prep the job token pipe in the root make process. 2904236769Sobrien * 2905236769Sobrien *----------------------------------------------------------------------- 2906236769Sobrien */ 2907236769Sobrien 2908236769Sobrienvoid 2909236769SobrienJob_ServerStart(int max_tokens, int jp_0, int jp_1) 2910236769Sobrien{ 2911236769Sobrien int i; 2912236769Sobrien char jobarg[64]; 2913236769Sobrien 2914236769Sobrien if (jp_0 >= 0 && jp_1 >= 0) { 2915236769Sobrien /* Pipe passed in from parent */ 2916236769Sobrien tokenWaitJob.inPipe = jp_0; 2917236769Sobrien tokenWaitJob.outPipe = jp_1; 2918296637Ssjg (void)fcntl(jp_0, F_SETFD, FD_CLOEXEC); 2919296637Ssjg (void)fcntl(jp_1, F_SETFD, FD_CLOEXEC); 2920236769Sobrien return; 2921236769Sobrien } 2922236769Sobrien 2923236769Sobrien JobCreatePipe(&tokenWaitJob, 15); 2924236769Sobrien 2925236769Sobrien snprintf(jobarg, sizeof(jobarg), "%d,%d", 2926236769Sobrien tokenWaitJob.inPipe, tokenWaitJob.outPipe); 2927236769Sobrien 2928236769Sobrien Var_Append(MAKEFLAGS, "-J", VAR_GLOBAL); 2929236769Sobrien Var_Append(MAKEFLAGS, jobarg, VAR_GLOBAL); 2930236769Sobrien 2931236769Sobrien /* 2932236769Sobrien * Preload the job pipe with one token per job, save the one 2933236769Sobrien * "extra" token for the primary job. 2934236769Sobrien * 2935236769Sobrien * XXX should clip maxJobs against PIPE_BUF -- if max_tokens is 2936236769Sobrien * larger than the write buffer size of the pipe, we will 2937236769Sobrien * deadlock here. 2938236769Sobrien */ 2939236769Sobrien for (i = 1; i < max_tokens; i++) 2940236769Sobrien JobTokenAdd(); 2941236769Sobrien} 2942236769Sobrien 2943236769Sobrien/*- 2944236769Sobrien *----------------------------------------------------------------------- 2945236769Sobrien * Job_TokenReturn -- 2946236769Sobrien * Return a withdrawn token to the pool. 2947236769Sobrien * 2948236769Sobrien *----------------------------------------------------------------------- 2949236769Sobrien */ 2950236769Sobrien 2951236769Sobrienvoid 2952236769SobrienJob_TokenReturn(void) 2953236769Sobrien{ 2954236769Sobrien jobTokensRunning--; 2955236769Sobrien if (jobTokensRunning < 0) 2956236769Sobrien Punt("token botch"); 2957236769Sobrien if (jobTokensRunning || JOB_TOKENS[aborting] != '+') 2958236769Sobrien JobTokenAdd(); 2959236769Sobrien} 2960236769Sobrien 2961236769Sobrien/*- 2962236769Sobrien *----------------------------------------------------------------------- 2963236769Sobrien * Job_TokenWithdraw -- 2964236769Sobrien * Attempt to withdraw a token from the pool. 2965236769Sobrien * 2966236769Sobrien * Results: 2967236769Sobrien * Returns TRUE if a token was withdrawn, and FALSE if the pool 2968236769Sobrien * is currently empty. 2969236769Sobrien * 2970236769Sobrien * Side Effects: 2971236769Sobrien * If pool is empty, set wantToken so that we wake up 2972236769Sobrien * when a token is released. 2973236769Sobrien * 2974236769Sobrien *----------------------------------------------------------------------- 2975236769Sobrien */ 2976236769Sobrien 2977236769Sobrien 2978236769SobrienBoolean 2979236769SobrienJob_TokenWithdraw(void) 2980236769Sobrien{ 2981236769Sobrien char tok, tok1; 2982236769Sobrien int count; 2983236769Sobrien 2984236769Sobrien wantToken = 0; 2985236769Sobrien if (DEBUG(JOB)) 2986236769Sobrien fprintf(debug_file, "Job_TokenWithdraw(%d): aborting %d, running %d\n", 2987236769Sobrien getpid(), aborting, jobTokensRunning); 2988236769Sobrien 2989236769Sobrien if (aborting || (jobTokensRunning >= maxJobs)) 2990236769Sobrien return FALSE; 2991236769Sobrien 2992236769Sobrien count = read(tokenWaitJob.inPipe, &tok, 1); 2993236769Sobrien if (count == 0) 2994236769Sobrien Fatal("eof on job pipe!"); 2995236769Sobrien if (count < 0 && jobTokensRunning != 0) { 2996236769Sobrien if (errno != EAGAIN) { 2997236769Sobrien Fatal("job pipe read: %s", strerror(errno)); 2998236769Sobrien } 2999236769Sobrien if (DEBUG(JOB)) 3000236769Sobrien fprintf(debug_file, "(%d) blocked for token\n", getpid()); 3001236769Sobrien wantToken = 1; 3002236769Sobrien return FALSE; 3003236769Sobrien } 3004236769Sobrien 3005236769Sobrien if (count == 1 && tok != '+') { 3006236769Sobrien /* make being abvorted - remove any other job tokens */ 3007236769Sobrien if (DEBUG(JOB)) 3008236769Sobrien fprintf(debug_file, "(%d) aborted by token %c\n", getpid(), tok); 3009236769Sobrien while (read(tokenWaitJob.inPipe, &tok1, 1) == 1) 3010236769Sobrien continue; 3011236769Sobrien /* And put the stopper back */ 3012246223Ssjg while (write(tokenWaitJob.outPipe, &tok, 1) == -1 && errno == EAGAIN) 3013246223Ssjg continue; 3014236769Sobrien Fatal("A failure has been detected in another branch of the parallel make"); 3015236769Sobrien } 3016236769Sobrien 3017236769Sobrien if (count == 1 && jobTokensRunning == 0) 3018236769Sobrien /* We didn't want the token really */ 3019246223Ssjg while (write(tokenWaitJob.outPipe, &tok, 1) == -1 && errno == EAGAIN) 3020246223Ssjg continue; 3021236769Sobrien 3022236769Sobrien jobTokensRunning++; 3023236769Sobrien if (DEBUG(JOB)) 3024236769Sobrien fprintf(debug_file, "(%d) withdrew token\n", getpid()); 3025236769Sobrien return TRUE; 3026236769Sobrien} 3027236769Sobrien 3028249033Ssjg/*- 3029249033Ssjg *----------------------------------------------------------------------- 3030249033Ssjg * Job_RunTarget -- 3031249033Ssjg * Run the named target if found. If a filename is specified, then 3032249033Ssjg * set that to the sources. 3033249033Ssjg * 3034249033Ssjg * Results: 3035249033Ssjg * None 3036249033Ssjg * 3037249033Ssjg * Side Effects: 3038249033Ssjg * exits if the target fails. 3039249033Ssjg * 3040249033Ssjg *----------------------------------------------------------------------- 3041249033Ssjg */ 3042249033SsjgBoolean 3043249033SsjgJob_RunTarget(const char *target, const char *fname) { 3044249033Ssjg GNode *gn = Targ_FindNode(target, TARG_NOCREATE); 3045249033Ssjg 3046249033Ssjg if (gn == NULL) 3047249033Ssjg return FALSE; 3048249033Ssjg 3049249033Ssjg if (fname) 3050249033Ssjg Var_Set(ALLSRC, fname, gn, 0); 3051249033Ssjg 3052249033Ssjg JobRun(gn); 3053249033Ssjg if (gn->made == ERROR) { 3054249033Ssjg PrintOnError(gn, "\n\nStop."); 3055249033Ssjg exit(1); 3056249033Ssjg } 3057249033Ssjg return TRUE; 3058249033Ssjg} 3059249033Ssjg 3060236769Sobrien#ifdef USE_SELECT 3061236769Sobrienint 3062236769Sobrienemul_poll(struct pollfd *fd, int nfd, int timeout) 3063236769Sobrien{ 3064236769Sobrien fd_set rfds, wfds; 3065236769Sobrien int i, maxfd, nselect, npoll; 3066236769Sobrien struct timeval tv, *tvp; 3067236769Sobrien long usecs; 3068236769Sobrien 3069236769Sobrien FD_ZERO(&rfds); 3070236769Sobrien FD_ZERO(&wfds); 3071236769Sobrien 3072236769Sobrien maxfd = -1; 3073236769Sobrien for (i = 0; i < nfd; i++) { 3074236769Sobrien fd[i].revents = 0; 3075236769Sobrien 3076236769Sobrien if (fd[i].events & POLLIN) 3077236769Sobrien FD_SET(fd[i].fd, &rfds); 3078236769Sobrien 3079236769Sobrien if (fd[i].events & POLLOUT) 3080236769Sobrien FD_SET(fd[i].fd, &wfds); 3081236769Sobrien 3082236769Sobrien if (fd[i].fd > maxfd) 3083236769Sobrien maxfd = fd[i].fd; 3084236769Sobrien } 3085236769Sobrien 3086236769Sobrien if (maxfd >= FD_SETSIZE) { 3087236769Sobrien Punt("Ran out of fd_set slots; " 3088236769Sobrien "recompile with a larger FD_SETSIZE."); 3089236769Sobrien } 3090236769Sobrien 3091236769Sobrien if (timeout < 0) { 3092236769Sobrien tvp = NULL; 3093236769Sobrien } else { 3094236769Sobrien usecs = timeout * 1000; 3095236769Sobrien tv.tv_sec = usecs / 1000000; 3096236769Sobrien tv.tv_usec = usecs % 1000000; 3097236769Sobrien tvp = &tv; 3098236769Sobrien } 3099236769Sobrien 3100236769Sobrien nselect = select(maxfd + 1, &rfds, &wfds, 0, tvp); 3101236769Sobrien 3102236769Sobrien if (nselect <= 0) 3103236769Sobrien return nselect; 3104236769Sobrien 3105236769Sobrien npoll = 0; 3106236769Sobrien for (i = 0; i < nfd; i++) { 3107236769Sobrien if (FD_ISSET(fd[i].fd, &rfds)) 3108236769Sobrien fd[i].revents |= POLLIN; 3109236769Sobrien 3110236769Sobrien if (FD_ISSET(fd[i].fd, &wfds)) 3111236769Sobrien fd[i].revents |= POLLOUT; 3112236769Sobrien 3113236769Sobrien if (fd[i].revents) 3114236769Sobrien npoll++; 3115236769Sobrien } 3116236769Sobrien 3117236769Sobrien return npoll; 3118236769Sobrien} 3119236769Sobrien#endif /* USE_SELECT */ 3120