1255253Ssjg/* $NetBSD: job.c,v 1.176 2013/08/04 16:48:15 sjg 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 73255253Ssjgstatic char rcsid[] = "$NetBSD: job.c,v 1.176 2013/08/04 16:48:15 sjg 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 80255253Ssjg__RCSID("$NetBSD: job.c,v 1.176 2013/08/04 16:48:15 sjg 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 * 105236769Sobrien * Job_Init Called to intialize 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#include <fcntl.h> 148236769Sobrien#if !defined(USE_SELECT) && defined(HAVE_POLL_H) 149236769Sobrien#include <poll.h> 150236769Sobrien#else 151236769Sobrien#ifndef USE_SELECT /* no poll.h */ 152236769Sobrien# define USE_SELECT 153236769Sobrien#endif 154236769Sobrien#if defined(HAVE_SYS_SELECT_H) 155236769Sobrien# include <sys/select.h> 156236769Sobrien#endif 157236769Sobrien#endif 158236769Sobrien#include <signal.h> 159236769Sobrien#include <stdio.h> 160236769Sobrien#include <string.h> 161236769Sobrien#include <utime.h> 162236769Sobrien#if defined(HAVE_SYS_SOCKET_H) 163236769Sobrien# include <sys/socket.h> 164236769Sobrien#endif 165236769Sobrien 166236769Sobrien#include "make.h" 167236769Sobrien#include "hash.h" 168236769Sobrien#include "dir.h" 169236769Sobrien#include "job.h" 170236769Sobrien#include "pathnames.h" 171236769Sobrien#include "trace.h" 172236769Sobrien# define STATIC static 173236769Sobrien 174236769Sobrien/* 175254419Ssjg * FreeBSD: traditionally .MAKE is not required to 176254419Ssjg * pass jobs queue to sub-makes. 177254419Ssjg * Use .MAKE.ALWAYS_PASS_JOB_QUEUE=no to disable. 178254419Ssjg */ 179254419Ssjg#define MAKE_ALWAYS_PASS_JOB_QUEUE ".MAKE.ALWAYS_PASS_JOB_QUEUE" 180254419Ssjgstatic int Always_pass_job_queue = TRUE; 181255285Ssjg/* 182255285Ssjg * FreeBSD: aborting entire parallel make isn't always 183255285Ssjg * desired. When doing tinderbox for example, failure of 184255285Ssjg * one architecture should not stop all. 185255285Ssjg * We still want to bail on interrupt though. 186255285Ssjg */ 187255285Ssjg#define MAKE_JOB_ERROR_TOKEN "MAKE_JOB_ERROR_TOKEN" 188255285Ssjgstatic int Job_error_token = TRUE; 189254419Ssjg 190254419Ssjg/* 191236769Sobrien * error handling variables 192236769Sobrien */ 193236769Sobrienstatic int errors = 0; /* number of errors reported */ 194236769Sobrienstatic int aborting = 0; /* why is the make aborting? */ 195236769Sobrien#define ABORT_ERROR 1 /* Because of an error */ 196236769Sobrien#define ABORT_INTERRUPT 2 /* Because it was interrupted */ 197236769Sobrien#define ABORT_WAIT 3 /* Waiting for jobs to finish */ 198236769Sobrien#define JOB_TOKENS "+EI+" /* Token to requeue for each abort state */ 199236769Sobrien 200236769Sobrien/* 201236769Sobrien * this tracks the number of tokens currently "out" to build jobs. 202236769Sobrien */ 203236769Sobrienint jobTokensRunning = 0; 204236769Sobrienint not_parallel = 0; /* set if .NOT_PARALLEL */ 205236769Sobrien 206236769Sobrien/* 207236769Sobrien * XXX: Avoid SunOS bug... FILENO() is fp->_file, and file 208236769Sobrien * is a char! So when we go above 127 we turn negative! 209236769Sobrien */ 210236769Sobrien#define FILENO(a) ((unsigned) fileno(a)) 211236769Sobrien 212236769Sobrien/* 213236769Sobrien * post-make command processing. The node postCommands is really just the 214236769Sobrien * .END target but we keep it around to avoid having to search for it 215236769Sobrien * all the time. 216236769Sobrien */ 217236769Sobrienstatic GNode *postCommands = NULL; 218236769Sobrien /* node containing commands to execute when 219236769Sobrien * everything else is done */ 220236769Sobrienstatic int numCommands; /* The number of commands actually printed 221236769Sobrien * for a target. Should this number be 222236769Sobrien * 0, no shell will be executed. */ 223236769Sobrien 224236769Sobrien/* 225236769Sobrien * Return values from JobStart. 226236769Sobrien */ 227236769Sobrien#define JOB_RUNNING 0 /* Job is running */ 228236769Sobrien#define JOB_ERROR 1 /* Error in starting the job */ 229236769Sobrien#define JOB_FINISHED 2 /* The job is already finished */ 230236769Sobrien 231236769Sobrien/* 232236769Sobrien * Descriptions for various shells. 233236769Sobrien * 234236769Sobrien * The build environment may set DEFSHELL_INDEX to one of 235236769Sobrien * DEFSHELL_INDEX_SH, DEFSHELL_INDEX_KSH, or DEFSHELL_INDEX_CSH, to 236236769Sobrien * select one of the prefedined shells as the default shell. 237236769Sobrien * 238236769Sobrien * Alternatively, the build environment may set DEFSHELL_CUSTOM to the 239236769Sobrien * name or the full path of a sh-compatible shell, which will be used as 240236769Sobrien * the default shell. 241236769Sobrien * 242236769Sobrien * ".SHELL" lines in Makefiles can choose the default shell from the 243236769Sobrien # set defined here, or add additional shells. 244236769Sobrien */ 245236769Sobrien 246236769Sobrien#ifdef DEFSHELL_CUSTOM 247236769Sobrien#define DEFSHELL_INDEX_CUSTOM 0 248236769Sobrien#define DEFSHELL_INDEX_SH 1 249236769Sobrien#define DEFSHELL_INDEX_KSH 2 250236769Sobrien#define DEFSHELL_INDEX_CSH 3 251236769Sobrien#else /* !DEFSHELL_CUSTOM */ 252236769Sobrien#define DEFSHELL_INDEX_SH 0 253236769Sobrien#define DEFSHELL_INDEX_KSH 1 254236769Sobrien#define DEFSHELL_INDEX_CSH 2 255236769Sobrien#endif /* !DEFSHELL_CUSTOM */ 256236769Sobrien 257236769Sobrien#ifndef DEFSHELL_INDEX 258236769Sobrien#define DEFSHELL_INDEX 0 /* DEFSHELL_INDEX_CUSTOM or DEFSHELL_INDEX_SH */ 259236769Sobrien#endif /* !DEFSHELL_INDEX */ 260236769Sobrien 261236769Sobrienstatic Shell shells[] = { 262236769Sobrien#ifdef DEFSHELL_CUSTOM 263236769Sobrien /* 264236769Sobrien * An sh-compatible shell with a non-standard name. 265236769Sobrien * 266236769Sobrien * Keep this in sync with the "sh" description below, but avoid 267236769Sobrien * non-portable features that might not be supplied by all 268236769Sobrien * sh-compatible shells. 269236769Sobrien */ 270236769Sobrien{ 271236769Sobrien DEFSHELL_CUSTOM, 272236769Sobrien FALSE, "", "", "", 0, 273236769Sobrien FALSE, "echo \"%s\"\n", "%s\n", "{ %s \n} || exit $?\n", "'\n'", '#', 274236769Sobrien "", 275236769Sobrien "", 276236769Sobrien}, 277236769Sobrien#endif /* DEFSHELL_CUSTOM */ 278236769Sobrien /* 279236769Sobrien * SH description. Echo control is also possible and, under 280236769Sobrien * sun UNIX anyway, one can even control error checking. 281236769Sobrien */ 282236769Sobrien{ 283236769Sobrien "sh", 284236769Sobrien FALSE, "", "", "", 0, 285236769Sobrien FALSE, "echo \"%s\"\n", "%s\n", "{ %s \n} || exit $?\n", "'\n'", '#', 286236769Sobrien#if defined(MAKE_NATIVE) && defined(__NetBSD__) 287236769Sobrien "q", 288236769Sobrien#else 289236769Sobrien "", 290236769Sobrien#endif 291236769Sobrien "", 292236769Sobrien}, 293236769Sobrien /* 294236769Sobrien * KSH description. 295236769Sobrien */ 296236769Sobrien{ 297236769Sobrien "ksh", 298236769Sobrien TRUE, "set +v", "set -v", "set +v", 6, 299236769Sobrien FALSE, "echo \"%s\"\n", "%s\n", "{ %s \n} || exit $?\n", "'\n'", '#', 300236769Sobrien "v", 301236769Sobrien "", 302236769Sobrien}, 303236769Sobrien /* 304236769Sobrien * CSH description. The csh can do echo control by playing 305236769Sobrien * with the setting of the 'echo' shell variable. Sadly, 306236769Sobrien * however, it is unable to do error control nicely. 307236769Sobrien */ 308236769Sobrien{ 309236769Sobrien "csh", 310236769Sobrien TRUE, "unset verbose", "set verbose", "unset verbose", 10, 311236769Sobrien FALSE, "echo \"%s\"\n", "csh -c \"%s || exit 0\"\n", "", "'\\\n'", '#', 312236769Sobrien "v", "e", 313236769Sobrien}, 314236769Sobrien /* 315236769Sobrien * UNKNOWN. 316236769Sobrien */ 317236769Sobrien{ 318236769Sobrien NULL, 319236769Sobrien FALSE, NULL, NULL, NULL, 0, 320236769Sobrien FALSE, NULL, NULL, NULL, NULL, 0, 321236769Sobrien NULL, NULL, 322236769Sobrien} 323236769Sobrien}; 324236769Sobrienstatic Shell *commandShell = &shells[DEFSHELL_INDEX]; /* this is the shell to 325236769Sobrien * which we pass all 326236769Sobrien * commands in the Makefile. 327236769Sobrien * It is set by the 328236769Sobrien * Job_ParseShell function */ 329236769Sobrienconst char *shellPath = NULL, /* full pathname of 330236769Sobrien * executable image */ 331236769Sobrien *shellName = NULL; /* last component of shell */ 332253883Ssjgchar *shellErrFlag = NULL; 333236769Sobrienstatic const char *shellArgv = NULL; /* Custom shell args */ 334236769Sobrien 335236769Sobrien 336236769SobrienSTATIC Job *job_table; /* The structures that describe them */ 337236769SobrienSTATIC Job *job_table_end; /* job_table + maxJobs */ 338236769Sobrienstatic int wantToken; /* we want a token */ 339236769Sobrienstatic int lurking_children = 0; 340236769Sobrienstatic int make_suspended = 0; /* non-zero if we've seen a SIGTSTP (etc) */ 341236769Sobrien 342236769Sobrien/* 343236769Sobrien * Set of descriptors of pipes connected to 344236769Sobrien * the output channels of children 345236769Sobrien */ 346236769Sobrienstatic struct pollfd *fds = NULL; 347236769Sobrienstatic Job **jobfds = NULL; 348236769Sobrienstatic int nfds = 0; 349236769Sobrienstatic void watchfd(Job *); 350236769Sobrienstatic void clearfd(Job *); 351236769Sobrienstatic int readyfd(Job *); 352236769Sobrien 353236769SobrienSTATIC GNode *lastNode; /* The node for which output was most recently 354236769Sobrien * produced. */ 355236769Sobrienstatic char *targPrefix = NULL; /* What we print at the start of TARG_FMT */ 356236769Sobrienstatic Job tokenWaitJob; /* token wait pseudo-job */ 357236769Sobrien 358236769Sobrienstatic Job childExitJob; /* child exit pseudo-job */ 359236769Sobrien#define CHILD_EXIT "." 360236769Sobrien#define DO_JOB_RESUME "R" 361236769Sobrien 362236769Sobrien#define TARG_FMT "%s %s ---\n" /* Default format */ 363236769Sobrien#define MESSAGE(fp, gn) \ 364253883Ssjg if (maxJobs != 1 && targPrefix && *targPrefix) \ 365236769Sobrien (void)fprintf(fp, TARG_FMT, targPrefix, gn->name) 366236769Sobrien 367236769Sobrienstatic sigset_t caught_signals; /* Set of signals we handle */ 368236769Sobrien#if defined(SYSV) 369236769Sobrien#define KILLPG(pid, sig) kill(-(pid), (sig)) 370236769Sobrien#else 371236769Sobrien#define KILLPG(pid, sig) killpg((pid), (sig)) 372236769Sobrien#endif 373236769Sobrien 374236769Sobrienstatic void JobChildSig(int); 375236769Sobrienstatic void JobContinueSig(int); 376236769Sobrienstatic Job *JobFindPid(int, int, Boolean); 377236769Sobrienstatic int JobPrintCommand(void *, void *); 378236769Sobrienstatic int JobSaveCommand(void *, void *); 379236769Sobrienstatic void JobClose(Job *); 380236769Sobrienstatic void JobExec(Job *, char **); 381236769Sobrienstatic void JobMakeArgv(Job *, char **); 382236769Sobrienstatic int JobStart(GNode *, int); 383236769Sobrienstatic char *JobOutput(Job *, char *, char *, int); 384236769Sobrienstatic void JobDoOutput(Job *, Boolean); 385236769Sobrienstatic Shell *JobMatchShell(const char *); 386237578Sobrienstatic void JobInterrupt(int, int) MAKE_ATTR_DEAD; 387236769Sobrienstatic void JobRestartJobs(void); 388236769Sobrienstatic void JobTokenAdd(void); 389236769Sobrienstatic void JobSigLock(sigset_t *); 390236769Sobrienstatic void JobSigUnlock(sigset_t *); 391236769Sobrienstatic void JobSigReset(void); 392236769Sobrien 393236769Sobrienconst char *malloc_options="A"; 394236769Sobrien 395236769Sobrienstatic void 396236769Sobrienjob_table_dump(const char *where) 397236769Sobrien{ 398236769Sobrien Job *job; 399236769Sobrien 400236769Sobrien fprintf(debug_file, "job table @ %s\n", where); 401236769Sobrien for (job = job_table; job < job_table_end; job++) { 402236769Sobrien fprintf(debug_file, "job %d, status %d, flags %d, pid %d\n", 403236769Sobrien (int)(job - job_table), job->job_state, job->flags, job->pid); 404236769Sobrien } 405236769Sobrien} 406236769Sobrien 407236769Sobrien/* 408236769Sobrien * JobSigLock/JobSigUnlock 409236769Sobrien * 410236769Sobrien * Signal lock routines to get exclusive access. Currently used to 411236769Sobrien * protect `jobs' and `stoppedJobs' list manipulations. 412236769Sobrien */ 413236769Sobrienstatic void JobSigLock(sigset_t *omaskp) 414236769Sobrien{ 415236769Sobrien if (sigprocmask(SIG_BLOCK, &caught_signals, omaskp) != 0) { 416236769Sobrien Punt("JobSigLock: sigprocmask: %s", strerror(errno)); 417236769Sobrien sigemptyset(omaskp); 418236769Sobrien } 419236769Sobrien} 420236769Sobrien 421236769Sobrienstatic void JobSigUnlock(sigset_t *omaskp) 422236769Sobrien{ 423236769Sobrien (void)sigprocmask(SIG_SETMASK, omaskp, NULL); 424236769Sobrien} 425236769Sobrien 426236769Sobrienstatic void 427236769SobrienJobCreatePipe(Job *job, int minfd) 428236769Sobrien{ 429236769Sobrien int i, fd; 430236769Sobrien 431236769Sobrien if (pipe(job->jobPipe) == -1) 432236769Sobrien Punt("Cannot create pipe: %s", strerror(errno)); 433236769Sobrien 434251422Ssjg for (i = 0; i < 2; i++) { 435251422Ssjg /* Avoid using low numbered fds */ 436251422Ssjg fd = fcntl(job->jobPipe[i], F_DUPFD, minfd); 437251422Ssjg if (fd != -1) { 438251422Ssjg close(job->jobPipe[i]); 439251422Ssjg job->jobPipe[i] = fd; 440251422Ssjg } 441251422Ssjg } 442251422Ssjg 443236769Sobrien /* Set close-on-exec flag for both */ 444236769Sobrien (void)fcntl(job->jobPipe[0], F_SETFD, 1); 445236769Sobrien (void)fcntl(job->jobPipe[1], F_SETFD, 1); 446236769Sobrien 447236769Sobrien /* 448236769Sobrien * We mark the input side of the pipe non-blocking; we poll(2) the 449236769Sobrien * pipe when we're waiting for a job token, but we might lose the 450236769Sobrien * race for the token when a new one becomes available, so the read 451236769Sobrien * from the pipe should not block. 452236769Sobrien */ 453236769Sobrien fcntl(job->jobPipe[0], F_SETFL, 454236769Sobrien fcntl(job->jobPipe[0], F_GETFL, 0) | O_NONBLOCK); 455236769Sobrien} 456236769Sobrien 457236769Sobrien/*- 458236769Sobrien *----------------------------------------------------------------------- 459236769Sobrien * JobCondPassSig -- 460236769Sobrien * Pass a signal to a job 461236769Sobrien * 462236769Sobrien * Input: 463236769Sobrien * signop Signal to send it 464236769Sobrien * 465236769Sobrien * Side Effects: 466236769Sobrien * None, except the job may bite it. 467236769Sobrien * 468236769Sobrien *----------------------------------------------------------------------- 469236769Sobrien */ 470236769Sobrienstatic void 471236769SobrienJobCondPassSig(int signo) 472236769Sobrien{ 473236769Sobrien Job *job; 474236769Sobrien 475236769Sobrien if (DEBUG(JOB)) { 476236769Sobrien (void)fprintf(debug_file, "JobCondPassSig(%d) called.\n", signo); 477236769Sobrien } 478236769Sobrien 479236769Sobrien for (job = job_table; job < job_table_end; job++) { 480236769Sobrien if (job->job_state != JOB_ST_RUNNING) 481236769Sobrien continue; 482236769Sobrien if (DEBUG(JOB)) { 483236769Sobrien (void)fprintf(debug_file, 484236769Sobrien "JobCondPassSig passing signal %d to child %d.\n", 485236769Sobrien signo, job->pid); 486236769Sobrien } 487236769Sobrien KILLPG(job->pid, signo); 488236769Sobrien } 489236769Sobrien} 490236769Sobrien 491236769Sobrien/*- 492236769Sobrien *----------------------------------------------------------------------- 493236769Sobrien * JobChldSig -- 494236769Sobrien * SIGCHLD handler. 495236769Sobrien * 496236769Sobrien * Input: 497236769Sobrien * signo The signal number we've received 498236769Sobrien * 499236769Sobrien * Results: 500236769Sobrien * None. 501236769Sobrien * 502236769Sobrien * Side Effects: 503236769Sobrien * Sends a token on the child exit pipe to wake us up from 504236769Sobrien * select()/poll(). 505236769Sobrien * 506236769Sobrien *----------------------------------------------------------------------- 507236769Sobrien */ 508236769Sobrienstatic void 509237578SobrienJobChildSig(int signo MAKE_ATTR_UNUSED) 510236769Sobrien{ 511246223Ssjg while (write(childExitJob.outPipe, CHILD_EXIT, 1) == -1 && errno == EAGAIN) 512246223Ssjg continue; 513236769Sobrien} 514236769Sobrien 515236769Sobrien 516236769Sobrien/*- 517236769Sobrien *----------------------------------------------------------------------- 518236769Sobrien * JobContinueSig -- 519236769Sobrien * Resume all stopped jobs. 520236769Sobrien * 521236769Sobrien * Input: 522236769Sobrien * signo The signal number we've received 523236769Sobrien * 524236769Sobrien * Results: 525236769Sobrien * None. 526236769Sobrien * 527236769Sobrien * Side Effects: 528236769Sobrien * Jobs start running again. 529236769Sobrien * 530236769Sobrien *----------------------------------------------------------------------- 531236769Sobrien */ 532236769Sobrienstatic void 533237578SobrienJobContinueSig(int signo MAKE_ATTR_UNUSED) 534236769Sobrien{ 535236769Sobrien /* 536236769Sobrien * Defer sending to SIGCONT to our stopped children until we return 537236769Sobrien * from the signal handler. 538236769Sobrien */ 539246223Ssjg while (write(childExitJob.outPipe, DO_JOB_RESUME, 1) == -1 && 540246223Ssjg errno == EAGAIN) 541246223Ssjg continue; 542236769Sobrien} 543236769Sobrien 544236769Sobrien/*- 545236769Sobrien *----------------------------------------------------------------------- 546236769Sobrien * JobPassSig -- 547236769Sobrien * Pass a signal on to all jobs, then resend to ourselves. 548236769Sobrien * 549236769Sobrien * Input: 550236769Sobrien * signo The signal number we've received 551236769Sobrien * 552236769Sobrien * Results: 553236769Sobrien * None. 554236769Sobrien * 555236769Sobrien * Side Effects: 556236769Sobrien * We die by the same signal. 557236769Sobrien * 558236769Sobrien *----------------------------------------------------------------------- 559236769Sobrien */ 560237578SobrienMAKE_ATTR_DEAD static void 561236769SobrienJobPassSig_int(int signo) 562236769Sobrien{ 563236769Sobrien /* Run .INTERRUPT target then exit */ 564236769Sobrien JobInterrupt(TRUE, signo); 565236769Sobrien} 566236769Sobrien 567237578SobrienMAKE_ATTR_DEAD static void 568236769SobrienJobPassSig_term(int signo) 569236769Sobrien{ 570236769Sobrien /* Dont run .INTERRUPT target then exit */ 571236769Sobrien JobInterrupt(FALSE, signo); 572236769Sobrien} 573236769Sobrien 574236769Sobrienstatic void 575236769SobrienJobPassSig_suspend(int signo) 576236769Sobrien{ 577236769Sobrien sigset_t nmask, omask; 578236769Sobrien struct sigaction act; 579236769Sobrien 580236769Sobrien /* Suppress job started/continued messages */ 581236769Sobrien make_suspended = 1; 582236769Sobrien 583236769Sobrien /* Pass the signal onto every job */ 584236769Sobrien JobCondPassSig(signo); 585236769Sobrien 586236769Sobrien /* 587236769Sobrien * Send ourselves the signal now we've given the message to everyone else. 588236769Sobrien * Note we block everything else possible while we're getting the signal. 589236769Sobrien * This ensures that all our jobs get continued when we wake up before 590236769Sobrien * we take any other signal. 591236769Sobrien */ 592236769Sobrien sigfillset(&nmask); 593236769Sobrien sigdelset(&nmask, signo); 594236769Sobrien (void)sigprocmask(SIG_SETMASK, &nmask, &omask); 595236769Sobrien 596236769Sobrien act.sa_handler = SIG_DFL; 597236769Sobrien sigemptyset(&act.sa_mask); 598236769Sobrien act.sa_flags = 0; 599236769Sobrien (void)sigaction(signo, &act, NULL); 600236769Sobrien 601236769Sobrien if (DEBUG(JOB)) { 602236769Sobrien (void)fprintf(debug_file, 603236769Sobrien "JobPassSig passing signal %d to self.\n", signo); 604236769Sobrien } 605236769Sobrien 606236769Sobrien (void)kill(getpid(), signo); 607236769Sobrien 608236769Sobrien /* 609236769Sobrien * We've been continued. 610236769Sobrien * 611236769Sobrien * A whole host of signals continue to happen! 612236769Sobrien * SIGCHLD for any processes that actually suspended themselves. 613236769Sobrien * SIGCHLD for any processes that exited while we were alseep. 614236769Sobrien * The SIGCONT that actually caused us to wakeup. 615236769Sobrien * 616236769Sobrien * Since we defer passing the SIGCONT on to our children until 617236769Sobrien * the main processing loop, we can be sure that all the SIGCHLD 618236769Sobrien * events will have happened by then - and that the waitpid() will 619236769Sobrien * collect the child 'suspended' events. 620236769Sobrien * For correct sequencing we just need to ensure we process the 621236769Sobrien * waitpid() before passign on the SIGCONT. 622236769Sobrien * 623236769Sobrien * In any case nothing else is needed here. 624236769Sobrien */ 625236769Sobrien 626236769Sobrien /* Restore handler and signal mask */ 627236769Sobrien act.sa_handler = JobPassSig_suspend; 628236769Sobrien (void)sigaction(signo, &act, NULL); 629236769Sobrien (void)sigprocmask(SIG_SETMASK, &omask, NULL); 630236769Sobrien} 631236769Sobrien 632236769Sobrien/*- 633236769Sobrien *----------------------------------------------------------------------- 634236769Sobrien * JobFindPid -- 635236769Sobrien * Compare the pid of the job with the given pid and return 0 if they 636236769Sobrien * are equal. This function is called from Job_CatchChildren 637236769Sobrien * to find the job descriptor of the finished job. 638236769Sobrien * 639236769Sobrien * Input: 640236769Sobrien * job job to examine 641236769Sobrien * pid process id desired 642236769Sobrien * 643236769Sobrien * Results: 644236769Sobrien * Job with matching pid 645236769Sobrien * 646236769Sobrien * Side Effects: 647236769Sobrien * None 648236769Sobrien *----------------------------------------------------------------------- 649236769Sobrien */ 650236769Sobrienstatic Job * 651236769SobrienJobFindPid(int pid, int status, Boolean isJobs) 652236769Sobrien{ 653236769Sobrien Job *job; 654236769Sobrien 655236769Sobrien for (job = job_table; job < job_table_end; job++) { 656236769Sobrien if ((job->job_state == status) && job->pid == pid) 657236769Sobrien return job; 658236769Sobrien } 659236769Sobrien if (DEBUG(JOB) && isJobs) 660236769Sobrien job_table_dump("no pid"); 661236769Sobrien return NULL; 662236769Sobrien} 663236769Sobrien 664236769Sobrien/*- 665236769Sobrien *----------------------------------------------------------------------- 666236769Sobrien * JobPrintCommand -- 667236769Sobrien * Put out another command for the given job. If the command starts 668236769Sobrien * with an @ or a - we process it specially. In the former case, 669236769Sobrien * so long as the -s and -n flags weren't given to make, we stick 670236769Sobrien * a shell-specific echoOff command in the script. In the latter, 671236769Sobrien * we ignore errors for the entire job, unless the shell has error 672236769Sobrien * control. 673236769Sobrien * If the command is just "..." we take all future commands for this 674236769Sobrien * job to be commands to be executed once the entire graph has been 675236769Sobrien * made and return non-zero to signal that the end of the commands 676236769Sobrien * was reached. These commands are later attached to the postCommands 677236769Sobrien * node and executed by Job_End when all things are done. 678236769Sobrien * This function is called from JobStart via Lst_ForEach. 679236769Sobrien * 680236769Sobrien * Input: 681236769Sobrien * cmdp command string to print 682236769Sobrien * jobp job for which to print it 683236769Sobrien * 684236769Sobrien * Results: 685236769Sobrien * Always 0, unless the command was "..." 686236769Sobrien * 687236769Sobrien * Side Effects: 688236769Sobrien * If the command begins with a '-' and the shell has no error control, 689236769Sobrien * the JOB_IGNERR flag is set in the job descriptor. 690236769Sobrien * If the command is "..." and we're not ignoring such things, 691236769Sobrien * tailCmds is set to the successor node of the cmd. 692236769Sobrien * numCommands is incremented if the command is actually printed. 693236769Sobrien *----------------------------------------------------------------------- 694236769Sobrien */ 695236769Sobrienstatic int 696236769SobrienJobPrintCommand(void *cmdp, void *jobp) 697236769Sobrien{ 698236769Sobrien Boolean noSpecials; /* true if we shouldn't worry about 699236769Sobrien * inserting special commands into 700236769Sobrien * the input stream. */ 701236769Sobrien Boolean shutUp = FALSE; /* true if we put a no echo command 702236769Sobrien * into the command file */ 703236769Sobrien Boolean errOff = FALSE; /* true if we turned error checking 704236769Sobrien * off before printing the command 705236769Sobrien * and need to turn it back on */ 706236769Sobrien const char *cmdTemplate; /* Template to use when printing the 707236769Sobrien * command */ 708236769Sobrien char *cmdStart; /* Start of expanded command */ 709236769Sobrien char *escCmd = NULL; /* Command with quotes/backticks escaped */ 710236769Sobrien char *cmd = (char *)cmdp; 711236769Sobrien Job *job = (Job *)jobp; 712236769Sobrien int i, j; 713236769Sobrien 714236769Sobrien noSpecials = NoExecute(job->node); 715236769Sobrien 716236769Sobrien if (strcmp(cmd, "...") == 0) { 717236769Sobrien job->node->type |= OP_SAVE_CMDS; 718236769Sobrien if ((job->flags & JOB_IGNDOTS) == 0) { 719236769Sobrien job->tailCmds = Lst_Succ(Lst_Member(job->node->commands, 720236769Sobrien cmd)); 721236769Sobrien return 1; 722236769Sobrien } 723236769Sobrien return 0; 724236769Sobrien } 725236769Sobrien 726236769Sobrien#define DBPRINTF(fmt, arg) if (DEBUG(JOB)) { \ 727236769Sobrien (void)fprintf(debug_file, fmt, arg); \ 728236769Sobrien } \ 729236769Sobrien (void)fprintf(job->cmdFILE, fmt, arg); \ 730236769Sobrien (void)fflush(job->cmdFILE); 731236769Sobrien 732236769Sobrien numCommands += 1; 733236769Sobrien 734236769Sobrien cmdStart = cmd = Var_Subst(NULL, cmd, job->node, FALSE); 735236769Sobrien 736236769Sobrien cmdTemplate = "%s\n"; 737236769Sobrien 738236769Sobrien /* 739236769Sobrien * Check for leading @' and -'s to control echoing and error checking. 740236769Sobrien */ 741236769Sobrien while (*cmd == '@' || *cmd == '-' || (*cmd == '+')) { 742236769Sobrien switch (*cmd) { 743236769Sobrien case '@': 744236769Sobrien shutUp = DEBUG(LOUD) ? FALSE : TRUE; 745236769Sobrien break; 746236769Sobrien case '-': 747236769Sobrien job->flags |= JOB_IGNERR; 748236769Sobrien errOff = TRUE; 749236769Sobrien break; 750236769Sobrien case '+': 751236769Sobrien if (noSpecials) { 752236769Sobrien /* 753236769Sobrien * We're not actually executing anything... 754236769Sobrien * but this one needs to be - use compat mode just for it. 755236769Sobrien */ 756236769Sobrien CompatRunCommand(cmdp, job->node); 757236769Sobrien return 0; 758236769Sobrien } 759236769Sobrien break; 760236769Sobrien } 761236769Sobrien cmd++; 762236769Sobrien } 763236769Sobrien 764236769Sobrien while (isspace((unsigned char) *cmd)) 765236769Sobrien cmd++; 766236769Sobrien 767236769Sobrien /* 768236769Sobrien * If the shell doesn't have error control the alternate echo'ing will 769236769Sobrien * be done (to avoid showing additional error checking code) 770236769Sobrien * and this will need the characters '$ ` \ "' escaped 771236769Sobrien */ 772236769Sobrien 773236769Sobrien if (!commandShell->hasErrCtl) { 774236769Sobrien /* Worst that could happen is every char needs escaping. */ 775236769Sobrien escCmd = bmake_malloc((strlen(cmd) * 2) + 1); 776236769Sobrien for (i = 0, j= 0; cmd[i] != '\0'; i++, j++) { 777236769Sobrien if (cmd[i] == '$' || cmd[i] == '`' || cmd[i] == '\\' || 778236769Sobrien cmd[i] == '"') 779236769Sobrien escCmd[j++] = '\\'; 780236769Sobrien escCmd[j] = cmd[i]; 781236769Sobrien } 782236769Sobrien escCmd[j] = 0; 783236769Sobrien } 784236769Sobrien 785236769Sobrien if (shutUp) { 786236769Sobrien if (!(job->flags & JOB_SILENT) && !noSpecials && 787236769Sobrien commandShell->hasEchoCtl) { 788236769Sobrien DBPRINTF("%s\n", commandShell->echoOff); 789236769Sobrien } else { 790236769Sobrien if (commandShell->hasErrCtl) 791236769Sobrien shutUp = FALSE; 792236769Sobrien } 793236769Sobrien } 794236769Sobrien 795236769Sobrien if (errOff) { 796236769Sobrien if (!noSpecials) { 797236769Sobrien if (commandShell->hasErrCtl) { 798236769Sobrien /* 799236769Sobrien * we don't want the error-control commands showing 800236769Sobrien * up either, so we turn off echoing while executing 801236769Sobrien * them. We could put another field in the shell 802236769Sobrien * structure to tell JobDoOutput to look for this 803236769Sobrien * string too, but why make it any more complex than 804236769Sobrien * it already is? 805236769Sobrien */ 806236769Sobrien if (!(job->flags & JOB_SILENT) && !shutUp && 807236769Sobrien commandShell->hasEchoCtl) { 808236769Sobrien DBPRINTF("%s\n", commandShell->echoOff); 809236769Sobrien DBPRINTF("%s\n", commandShell->ignErr); 810236769Sobrien DBPRINTF("%s\n", commandShell->echoOn); 811236769Sobrien } else { 812236769Sobrien DBPRINTF("%s\n", commandShell->ignErr); 813236769Sobrien } 814236769Sobrien } else if (commandShell->ignErr && 815236769Sobrien (*commandShell->ignErr != '\0')) 816236769Sobrien { 817236769Sobrien /* 818236769Sobrien * The shell has no error control, so we need to be 819236769Sobrien * weird to get it to ignore any errors from the command. 820236769Sobrien * If echoing is turned on, we turn it off and use the 821236769Sobrien * errCheck template to echo the command. Leave echoing 822236769Sobrien * off so the user doesn't see the weirdness we go through 823236769Sobrien * to ignore errors. Set cmdTemplate to use the weirdness 824236769Sobrien * instead of the simple "%s\n" template. 825236769Sobrien */ 826236769Sobrien if (!(job->flags & JOB_SILENT) && !shutUp) { 827236769Sobrien if (commandShell->hasEchoCtl) { 828236769Sobrien DBPRINTF("%s\n", commandShell->echoOff); 829236769Sobrien } 830236769Sobrien DBPRINTF(commandShell->errCheck, escCmd); 831236769Sobrien shutUp = TRUE; 832236769Sobrien } else { 833236769Sobrien if (!shutUp) { 834236769Sobrien DBPRINTF(commandShell->errCheck, escCmd); 835236769Sobrien } 836236769Sobrien } 837236769Sobrien cmdTemplate = commandShell->ignErr; 838236769Sobrien /* 839236769Sobrien * The error ignoration (hee hee) is already taken care 840236769Sobrien * of by the ignErr template, so pretend error checking 841236769Sobrien * is still on. 842236769Sobrien */ 843236769Sobrien errOff = FALSE; 844236769Sobrien } else { 845236769Sobrien errOff = FALSE; 846236769Sobrien } 847236769Sobrien } else { 848236769Sobrien errOff = FALSE; 849236769Sobrien } 850236769Sobrien } else { 851236769Sobrien 852236769Sobrien /* 853236769Sobrien * If errors are being checked and the shell doesn't have error control 854236769Sobrien * but does supply an errOut template, then setup commands to run 855236769Sobrien * through it. 856236769Sobrien */ 857236769Sobrien 858236769Sobrien if (!commandShell->hasErrCtl && commandShell->errOut && 859236769Sobrien (*commandShell->errOut != '\0')) { 860236769Sobrien if (!(job->flags & JOB_SILENT) && !shutUp) { 861236769Sobrien if (commandShell->hasEchoCtl) { 862236769Sobrien DBPRINTF("%s\n", commandShell->echoOff); 863236769Sobrien } 864236769Sobrien DBPRINTF(commandShell->errCheck, escCmd); 865236769Sobrien shutUp = TRUE; 866236769Sobrien } 867236769Sobrien /* If it's a comment line or blank, treat as an ignored error */ 868236769Sobrien if ((escCmd[0] == commandShell->commentChar) || 869236769Sobrien (escCmd[0] == 0)) 870236769Sobrien cmdTemplate = commandShell->ignErr; 871236769Sobrien else 872236769Sobrien cmdTemplate = commandShell->errOut; 873236769Sobrien errOff = FALSE; 874236769Sobrien } 875236769Sobrien } 876236769Sobrien 877236769Sobrien if (DEBUG(SHELL) && strcmp(shellName, "sh") == 0 && 878236769Sobrien (job->flags & JOB_TRACED) == 0) { 879236769Sobrien DBPRINTF("set -%s\n", "x"); 880236769Sobrien job->flags |= JOB_TRACED; 881236769Sobrien } 882236769Sobrien 883236769Sobrien DBPRINTF(cmdTemplate, cmd); 884236769Sobrien free(cmdStart); 885236769Sobrien if (escCmd) 886236769Sobrien free(escCmd); 887236769Sobrien if (errOff) { 888236769Sobrien /* 889236769Sobrien * If echoing is already off, there's no point in issuing the 890236769Sobrien * echoOff command. Otherwise we issue it and pretend it was on 891236769Sobrien * for the whole command... 892236769Sobrien */ 893236769Sobrien if (!shutUp && !(job->flags & JOB_SILENT) && commandShell->hasEchoCtl){ 894236769Sobrien DBPRINTF("%s\n", commandShell->echoOff); 895236769Sobrien shutUp = TRUE; 896236769Sobrien } 897236769Sobrien DBPRINTF("%s\n", commandShell->errCheck); 898236769Sobrien } 899236769Sobrien if (shutUp && commandShell->hasEchoCtl) { 900236769Sobrien DBPRINTF("%s\n", commandShell->echoOn); 901236769Sobrien } 902236769Sobrien return 0; 903236769Sobrien} 904236769Sobrien 905236769Sobrien/*- 906236769Sobrien *----------------------------------------------------------------------- 907236769Sobrien * JobSaveCommand -- 908236769Sobrien * Save a command to be executed when everything else is done. 909236769Sobrien * Callback function for JobFinish... 910236769Sobrien * 911236769Sobrien * Results: 912236769Sobrien * Always returns 0 913236769Sobrien * 914236769Sobrien * Side Effects: 915236769Sobrien * The command is tacked onto the end of postCommands's commands list. 916236769Sobrien * 917236769Sobrien *----------------------------------------------------------------------- 918236769Sobrien */ 919236769Sobrienstatic int 920236769SobrienJobSaveCommand(void *cmd, void *gn) 921236769Sobrien{ 922236769Sobrien cmd = Var_Subst(NULL, (char *)cmd, (GNode *)gn, FALSE); 923236769Sobrien (void)Lst_AtEnd(postCommands->commands, cmd); 924236769Sobrien return(0); 925236769Sobrien} 926236769Sobrien 927236769Sobrien 928236769Sobrien/*- 929236769Sobrien *----------------------------------------------------------------------- 930236769Sobrien * JobClose -- 931236769Sobrien * Called to close both input and output pipes when a job is finished. 932236769Sobrien * 933236769Sobrien * Results: 934236769Sobrien * Nada 935236769Sobrien * 936236769Sobrien * Side Effects: 937236769Sobrien * The file descriptors associated with the job are closed. 938236769Sobrien * 939236769Sobrien *----------------------------------------------------------------------- 940236769Sobrien */ 941236769Sobrienstatic void 942236769SobrienJobClose(Job *job) 943236769Sobrien{ 944236769Sobrien clearfd(job); 945236769Sobrien (void)close(job->outPipe); 946236769Sobrien job->outPipe = -1; 947236769Sobrien 948236769Sobrien JobDoOutput(job, TRUE); 949236769Sobrien (void)close(job->inPipe); 950236769Sobrien job->inPipe = -1; 951236769Sobrien} 952236769Sobrien 953236769Sobrien/*- 954236769Sobrien *----------------------------------------------------------------------- 955236769Sobrien * JobFinish -- 956236769Sobrien * Do final processing for the given job including updating 957236769Sobrien * parents and starting new jobs as available/necessary. Note 958236769Sobrien * that we pay no attention to the JOB_IGNERR flag here. 959236769Sobrien * This is because when we're called because of a noexecute flag 960236769Sobrien * or something, jstat.w_status is 0 and when called from 961236769Sobrien * Job_CatchChildren, the status is zeroed if it s/b ignored. 962236769Sobrien * 963236769Sobrien * Input: 964236769Sobrien * job job to finish 965236769Sobrien * status sub-why job went away 966236769Sobrien * 967236769Sobrien * Results: 968236769Sobrien * None 969236769Sobrien * 970236769Sobrien * Side Effects: 971236769Sobrien * Final commands for the job are placed on postCommands. 972236769Sobrien * 973236769Sobrien * If we got an error and are aborting (aborting == ABORT_ERROR) and 974236769Sobrien * the job list is now empty, we are done for the day. 975236769Sobrien * If we recognized an error (errors !=0), we set the aborting flag 976236769Sobrien * to ABORT_ERROR so no more jobs will be started. 977236769Sobrien *----------------------------------------------------------------------- 978236769Sobrien */ 979236769Sobrien/*ARGSUSED*/ 980236769Sobrienstatic void 981236769SobrienJobFinish (Job *job, WAIT_T status) 982236769Sobrien{ 983236769Sobrien Boolean done, return_job_token; 984236769Sobrien 985236769Sobrien if (DEBUG(JOB)) { 986236769Sobrien fprintf(debug_file, "Jobfinish: %d [%s], status %d\n", 987236769Sobrien job->pid, job->node->name, status); 988236769Sobrien } 989236769Sobrien 990236769Sobrien if ((WIFEXITED(status) && 991236769Sobrien (((WEXITSTATUS(status) != 0) && !(job->flags & JOB_IGNERR)))) || 992236769Sobrien WIFSIGNALED(status)) 993236769Sobrien { 994236769Sobrien /* 995236769Sobrien * If it exited non-zero and either we're doing things our 996236769Sobrien * way or we're not ignoring errors, the job is finished. 997236769Sobrien * Similarly, if the shell died because of a signal 998236769Sobrien * the job is also finished. In these 999236769Sobrien * cases, finish out the job's output before printing the exit 1000236769Sobrien * status... 1001236769Sobrien */ 1002236769Sobrien JobClose(job); 1003236769Sobrien if (job->cmdFILE != NULL && job->cmdFILE != stdout) { 1004236769Sobrien (void)fclose(job->cmdFILE); 1005236769Sobrien job->cmdFILE = NULL; 1006236769Sobrien } 1007236769Sobrien done = TRUE; 1008236769Sobrien } else if (WIFEXITED(status)) { 1009236769Sobrien /* 1010236769Sobrien * Deal with ignored errors in -B mode. We need to print a message 1011236769Sobrien * telling of the ignored error as well as setting status.w_status 1012236769Sobrien * to 0 so the next command gets run. To do this, we set done to be 1013236769Sobrien * TRUE if in -B mode and the job exited non-zero. 1014236769Sobrien */ 1015236769Sobrien done = WEXITSTATUS(status) != 0; 1016236769Sobrien /* 1017236769Sobrien * Old comment said: "Note we don't 1018236769Sobrien * want to close down any of the streams until we know we're at the 1019236769Sobrien * end." 1020236769Sobrien * But we do. Otherwise when are we going to print the rest of the 1021236769Sobrien * stuff? 1022236769Sobrien */ 1023236769Sobrien JobClose(job); 1024236769Sobrien } else { 1025236769Sobrien /* 1026236769Sobrien * No need to close things down or anything. 1027236769Sobrien */ 1028236769Sobrien done = FALSE; 1029236769Sobrien } 1030236769Sobrien 1031236769Sobrien if (done) { 1032236769Sobrien if (WIFEXITED(status)) { 1033236769Sobrien if (DEBUG(JOB)) { 1034236769Sobrien (void)fprintf(debug_file, "Process %d [%s] exited.\n", 1035236769Sobrien job->pid, job->node->name); 1036236769Sobrien } 1037236769Sobrien if (WEXITSTATUS(status) != 0) { 1038236769Sobrien if (job->node != lastNode) { 1039236769Sobrien MESSAGE(stdout, job->node); 1040236769Sobrien lastNode = job->node; 1041236769Sobrien } 1042236769Sobrien#ifdef USE_META 1043236769Sobrien if (useMeta) { 1044236769Sobrien meta_job_error(job, job->node, job->flags, WEXITSTATUS(status)); 1045236769Sobrien } 1046236769Sobrien#endif 1047236769Sobrien (void)printf("*** [%s] Error code %d%s\n", 1048236769Sobrien job->node->name, 1049236769Sobrien WEXITSTATUS(status), 1050236769Sobrien (job->flags & JOB_IGNERR) ? " (ignored)" : ""); 1051236769Sobrien if (job->flags & JOB_IGNERR) { 1052236769Sobrien WAIT_STATUS(status) = 0; 1053236769Sobrien } else { 1054236769Sobrien PrintOnError(job->node, NULL); 1055236769Sobrien } 1056236769Sobrien } else if (DEBUG(JOB)) { 1057236769Sobrien if (job->node != lastNode) { 1058236769Sobrien MESSAGE(stdout, job->node); 1059236769Sobrien lastNode = job->node; 1060236769Sobrien } 1061236769Sobrien (void)printf("*** [%s] Completed successfully\n", 1062236769Sobrien job->node->name); 1063236769Sobrien } 1064236769Sobrien } else { 1065236769Sobrien if (job->node != lastNode) { 1066236769Sobrien MESSAGE(stdout, job->node); 1067236769Sobrien lastNode = job->node; 1068236769Sobrien } 1069236769Sobrien (void)printf("*** [%s] Signal %d\n", 1070236769Sobrien job->node->name, WTERMSIG(status)); 1071236769Sobrien } 1072236769Sobrien (void)fflush(stdout); 1073236769Sobrien } 1074236769Sobrien 1075236769Sobrien#ifdef USE_META 1076236769Sobrien if (useMeta) { 1077236769Sobrien meta_job_finish(job); 1078236769Sobrien } 1079236769Sobrien#endif 1080236769Sobrien 1081236769Sobrien return_job_token = FALSE; 1082236769Sobrien 1083236769Sobrien Trace_Log(JOBEND, job); 1084236769Sobrien if (!(job->flags & JOB_SPECIAL)) { 1085236769Sobrien if ((WAIT_STATUS(status) != 0) || 1086236769Sobrien (aborting == ABORT_ERROR) || 1087236769Sobrien (aborting == ABORT_INTERRUPT)) 1088236769Sobrien return_job_token = TRUE; 1089236769Sobrien } 1090236769Sobrien 1091236769Sobrien if ((aborting != ABORT_ERROR) && (aborting != ABORT_INTERRUPT) && 1092236769Sobrien (WAIT_STATUS(status) == 0)) { 1093236769Sobrien /* 1094236769Sobrien * As long as we aren't aborting and the job didn't return a non-zero 1095236769Sobrien * status that we shouldn't ignore, we call Make_Update to update 1096236769Sobrien * the parents. In addition, any saved commands for the node are placed 1097236769Sobrien * on the .END target. 1098236769Sobrien */ 1099236769Sobrien if (job->tailCmds != NULL) { 1100236769Sobrien Lst_ForEachFrom(job->node->commands, job->tailCmds, 1101236769Sobrien JobSaveCommand, 1102236769Sobrien job->node); 1103236769Sobrien } 1104236769Sobrien job->node->made = MADE; 1105236769Sobrien if (!(job->flags & JOB_SPECIAL)) 1106236769Sobrien return_job_token = TRUE; 1107236769Sobrien Make_Update(job->node); 1108236769Sobrien job->job_state = JOB_ST_FREE; 1109236769Sobrien } else if (WAIT_STATUS(status)) { 1110236769Sobrien errors += 1; 1111236769Sobrien job->job_state = JOB_ST_FREE; 1112236769Sobrien } 1113236769Sobrien 1114236769Sobrien /* 1115236769Sobrien * Set aborting if any error. 1116236769Sobrien */ 1117236769Sobrien if (errors && !keepgoing && (aborting != ABORT_INTERRUPT)) { 1118236769Sobrien /* 1119236769Sobrien * If we found any errors in this batch of children and the -k flag 1120236769Sobrien * wasn't given, we set the aborting flag so no more jobs get 1121236769Sobrien * started. 1122236769Sobrien */ 1123236769Sobrien aborting = ABORT_ERROR; 1124236769Sobrien } 1125236769Sobrien 1126236769Sobrien if (return_job_token) 1127236769Sobrien Job_TokenReturn(); 1128236769Sobrien 1129236769Sobrien if (aborting == ABORT_ERROR && jobTokensRunning == 0) { 1130236769Sobrien /* 1131236769Sobrien * If we are aborting and the job table is now empty, we finish. 1132236769Sobrien */ 1133236769Sobrien Finish(errors); 1134236769Sobrien } 1135236769Sobrien} 1136236769Sobrien 1137236769Sobrien/*- 1138236769Sobrien *----------------------------------------------------------------------- 1139236769Sobrien * Job_Touch -- 1140236769Sobrien * Touch the given target. Called by JobStart when the -t flag was 1141236769Sobrien * given 1142236769Sobrien * 1143236769Sobrien * Input: 1144236769Sobrien * gn the node of the file to touch 1145236769Sobrien * silent TRUE if should not print message 1146236769Sobrien * 1147236769Sobrien * Results: 1148236769Sobrien * None 1149236769Sobrien * 1150236769Sobrien * Side Effects: 1151236769Sobrien * The data modification of the file is changed. In addition, if the 1152236769Sobrien * file did not exist, it is created. 1153236769Sobrien *----------------------------------------------------------------------- 1154236769Sobrien */ 1155236769Sobrienvoid 1156236769SobrienJob_Touch(GNode *gn, Boolean silent) 1157236769Sobrien{ 1158236769Sobrien int streamID; /* ID of stream opened to do the touch */ 1159236769Sobrien struct utimbuf times; /* Times for utime() call */ 1160236769Sobrien 1161236769Sobrien if (gn->type & (OP_JOIN|OP_USE|OP_USEBEFORE|OP_EXEC|OP_OPTIONAL| 1162236769Sobrien OP_SPECIAL|OP_PHONY)) { 1163236769Sobrien /* 1164236769Sobrien * .JOIN, .USE, .ZEROTIME and .OPTIONAL targets are "virtual" targets 1165236769Sobrien * and, as such, shouldn't really be created. 1166236769Sobrien */ 1167236769Sobrien return; 1168236769Sobrien } 1169236769Sobrien 1170236769Sobrien if (!silent || NoExecute(gn)) { 1171236769Sobrien (void)fprintf(stdout, "touch %s\n", gn->name); 1172236769Sobrien (void)fflush(stdout); 1173236769Sobrien } 1174236769Sobrien 1175236769Sobrien if (NoExecute(gn)) { 1176236769Sobrien return; 1177236769Sobrien } 1178236769Sobrien 1179236769Sobrien if (gn->type & OP_ARCHV) { 1180236769Sobrien Arch_Touch(gn); 1181236769Sobrien } else if (gn->type & OP_LIB) { 1182236769Sobrien Arch_TouchLib(gn); 1183236769Sobrien } else { 1184236769Sobrien char *file = gn->path ? gn->path : gn->name; 1185236769Sobrien 1186236769Sobrien times.actime = times.modtime = now; 1187236769Sobrien if (utime(file, ×) < 0){ 1188236769Sobrien streamID = open(file, O_RDWR | O_CREAT, 0666); 1189236769Sobrien 1190236769Sobrien if (streamID >= 0) { 1191236769Sobrien char c; 1192236769Sobrien 1193236769Sobrien /* 1194236769Sobrien * Read and write a byte to the file to change the 1195236769Sobrien * modification time, then close the file. 1196236769Sobrien */ 1197236769Sobrien if (read(streamID, &c, 1) == 1) { 1198236769Sobrien (void)lseek(streamID, (off_t)0, SEEK_SET); 1199246223Ssjg while (write(streamID, &c, 1) == -1 && errno == EAGAIN) 1200246223Ssjg continue; 1201236769Sobrien } 1202236769Sobrien 1203236769Sobrien (void)close(streamID); 1204236769Sobrien } else { 1205236769Sobrien (void)fprintf(stdout, "*** couldn't touch %s: %s", 1206236769Sobrien file, strerror(errno)); 1207236769Sobrien (void)fflush(stdout); 1208236769Sobrien } 1209236769Sobrien } 1210236769Sobrien } 1211236769Sobrien} 1212236769Sobrien 1213236769Sobrien/*- 1214236769Sobrien *----------------------------------------------------------------------- 1215236769Sobrien * Job_CheckCommands -- 1216236769Sobrien * Make sure the given node has all the commands it needs. 1217236769Sobrien * 1218236769Sobrien * Input: 1219236769Sobrien * gn The target whose commands need verifying 1220236769Sobrien * abortProc Function to abort with message 1221236769Sobrien * 1222236769Sobrien * Results: 1223236769Sobrien * TRUE if the commands list is/was ok. 1224236769Sobrien * 1225236769Sobrien * Side Effects: 1226236769Sobrien * The node will have commands from the .DEFAULT rule added to it 1227236769Sobrien * if it needs them. 1228236769Sobrien *----------------------------------------------------------------------- 1229236769Sobrien */ 1230236769SobrienBoolean 1231236769SobrienJob_CheckCommands(GNode *gn, void (*abortProc)(const char *, ...)) 1232236769Sobrien{ 1233236769Sobrien if (OP_NOP(gn->type) && Lst_IsEmpty(gn->commands) && 1234236769Sobrien ((gn->type & OP_LIB) == 0 || Lst_IsEmpty(gn->children))) { 1235236769Sobrien /* 1236236769Sobrien * No commands. Look for .DEFAULT rule from which we might infer 1237236769Sobrien * commands 1238236769Sobrien */ 1239236769Sobrien if ((DEFAULT != NULL) && !Lst_IsEmpty(DEFAULT->commands) && 1240236769Sobrien (gn->type & OP_SPECIAL) == 0) { 1241236769Sobrien char *p1; 1242236769Sobrien /* 1243236769Sobrien * Make only looks for a .DEFAULT if the node was never the 1244236769Sobrien * target of an operator, so that's what we do too. If 1245236769Sobrien * a .DEFAULT was given, we substitute its commands for gn's 1246236769Sobrien * commands and set the IMPSRC variable to be the target's name 1247236769Sobrien * The DEFAULT node acts like a transformation rule, in that 1248236769Sobrien * gn also inherits any attributes or sources attached to 1249236769Sobrien * .DEFAULT itself. 1250236769Sobrien */ 1251236769Sobrien Make_HandleUse(DEFAULT, gn); 1252236769Sobrien Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), gn, 0); 1253236769Sobrien if (p1) 1254236769Sobrien free(p1); 1255236769Sobrien } else if (Dir_MTime(gn, 0) == 0 && (gn->type & OP_SPECIAL) == 0) { 1256236769Sobrien /* 1257236769Sobrien * The node wasn't the target of an operator we have no .DEFAULT 1258236769Sobrien * rule to go on and the target doesn't already exist. There's 1259236769Sobrien * nothing more we can do for this branch. If the -k flag wasn't 1260236769Sobrien * given, we stop in our tracks, otherwise we just don't update 1261236769Sobrien * this node's parents so they never get examined. 1262236769Sobrien */ 1263236769Sobrien static const char msg[] = ": don't know how to make"; 1264236769Sobrien 1265236769Sobrien if (gn->flags & FROM_DEPEND) { 1266249033Ssjg if (!Job_RunTarget(".STALE", gn->fname)) 1267249033Ssjg fprintf(stdout, "%s: %s, %d: ignoring stale %s for %s\n", 1268249033Ssjg progname, gn->fname, gn->lineno, makeDependfile, 1269249033Ssjg gn->name); 1270236769Sobrien return TRUE; 1271236769Sobrien } 1272236769Sobrien 1273236769Sobrien if (gn->type & OP_OPTIONAL) { 1274236769Sobrien (void)fprintf(stdout, "%s%s %s (ignored)\n", progname, 1275236769Sobrien msg, gn->name); 1276236769Sobrien (void)fflush(stdout); 1277236769Sobrien } else if (keepgoing) { 1278236769Sobrien (void)fprintf(stdout, "%s%s %s (continuing)\n", progname, 1279236769Sobrien msg, gn->name); 1280236769Sobrien (void)fflush(stdout); 1281236769Sobrien return FALSE; 1282236769Sobrien } else { 1283236769Sobrien (*abortProc)("%s%s %s. Stop", progname, msg, gn->name); 1284236769Sobrien return FALSE; 1285236769Sobrien } 1286236769Sobrien } 1287236769Sobrien } 1288236769Sobrien return TRUE; 1289236769Sobrien} 1290236769Sobrien 1291236769Sobrien/*- 1292236769Sobrien *----------------------------------------------------------------------- 1293236769Sobrien * JobExec -- 1294236769Sobrien * Execute the shell for the given job. Called from JobStart 1295236769Sobrien * 1296236769Sobrien * Input: 1297236769Sobrien * job Job to execute 1298236769Sobrien * 1299236769Sobrien * Results: 1300236769Sobrien * None. 1301236769Sobrien * 1302236769Sobrien * Side Effects: 1303236769Sobrien * A shell is executed, outputs is altered and the Job structure added 1304236769Sobrien * to the job table. 1305236769Sobrien * 1306236769Sobrien *----------------------------------------------------------------------- 1307236769Sobrien */ 1308236769Sobrienstatic void 1309236769SobrienJobExec(Job *job, char **argv) 1310236769Sobrien{ 1311236769Sobrien int cpid; /* ID of new child */ 1312236769Sobrien sigset_t mask; 1313236769Sobrien 1314236769Sobrien job->flags &= ~JOB_TRACED; 1315236769Sobrien 1316236769Sobrien if (DEBUG(JOB)) { 1317236769Sobrien int i; 1318236769Sobrien 1319236769Sobrien (void)fprintf(debug_file, "Running %s %sly\n", job->node->name, "local"); 1320236769Sobrien (void)fprintf(debug_file, "\tCommand: "); 1321236769Sobrien for (i = 0; argv[i] != NULL; i++) { 1322236769Sobrien (void)fprintf(debug_file, "%s ", argv[i]); 1323236769Sobrien } 1324236769Sobrien (void)fprintf(debug_file, "\n"); 1325236769Sobrien } 1326236769Sobrien 1327236769Sobrien /* 1328236769Sobrien * Some jobs produce no output and it's disconcerting to have 1329236769Sobrien * no feedback of their running (since they produce no output, the 1330236769Sobrien * banner with their name in it never appears). This is an attempt to 1331236769Sobrien * provide that feedback, even if nothing follows it. 1332236769Sobrien */ 1333236769Sobrien if ((lastNode != job->node) && !(job->flags & JOB_SILENT)) { 1334236769Sobrien MESSAGE(stdout, job->node); 1335236769Sobrien lastNode = job->node; 1336236769Sobrien } 1337236769Sobrien 1338236769Sobrien /* No interruptions until this job is on the `jobs' list */ 1339236769Sobrien JobSigLock(&mask); 1340236769Sobrien 1341236769Sobrien /* Pre-emptively mark job running, pid still zero though */ 1342236769Sobrien job->job_state = JOB_ST_RUNNING; 1343236769Sobrien 1344236769Sobrien cpid = vFork(); 1345236769Sobrien if (cpid == -1) 1346236769Sobrien Punt("Cannot vfork: %s", strerror(errno)); 1347236769Sobrien 1348236769Sobrien if (cpid == 0) { 1349236769Sobrien /* Child */ 1350236769Sobrien sigset_t tmask; 1351236769Sobrien 1352236769Sobrien#ifdef USE_META 1353236769Sobrien if (useMeta) { 1354236769Sobrien meta_job_child(job); 1355236769Sobrien } 1356236769Sobrien#endif 1357236769Sobrien /* 1358236769Sobrien * Reset all signal handlers; this is necessary because we also 1359236769Sobrien * need to unblock signals before we exec(2). 1360236769Sobrien */ 1361236769Sobrien JobSigReset(); 1362236769Sobrien 1363236769Sobrien /* Now unblock signals */ 1364236769Sobrien sigemptyset(&tmask); 1365236769Sobrien JobSigUnlock(&tmask); 1366236769Sobrien 1367236769Sobrien /* 1368236769Sobrien * Must duplicate the input stream down to the child's input and 1369236769Sobrien * reset it to the beginning (again). Since the stream was marked 1370236769Sobrien * close-on-exec, we must clear that bit in the new input. 1371236769Sobrien */ 1372236769Sobrien if (dup2(FILENO(job->cmdFILE), 0) == -1) { 1373236769Sobrien execError("dup2", "job->cmdFILE"); 1374236769Sobrien _exit(1); 1375236769Sobrien } 1376236769Sobrien (void)fcntl(0, F_SETFD, 0); 1377236769Sobrien (void)lseek(0, (off_t)0, SEEK_SET); 1378236769Sobrien 1379254419Ssjg if (Always_pass_job_queue || (job->node->type & OP_MAKE)) { 1380236769Sobrien /* 1381236769Sobrien * Pass job token pipe to submakes. 1382236769Sobrien */ 1383236769Sobrien fcntl(tokenWaitJob.inPipe, F_SETFD, 0); 1384236769Sobrien fcntl(tokenWaitJob.outPipe, F_SETFD, 0); 1385236769Sobrien } 1386236769Sobrien 1387236769Sobrien /* 1388236769Sobrien * Set up the child's output to be routed through the pipe 1389236769Sobrien * we've created for it. 1390236769Sobrien */ 1391236769Sobrien if (dup2(job->outPipe, 1) == -1) { 1392236769Sobrien execError("dup2", "job->outPipe"); 1393236769Sobrien _exit(1); 1394236769Sobrien } 1395236769Sobrien /* 1396236769Sobrien * The output channels are marked close on exec. This bit was 1397236769Sobrien * duplicated by the dup2(on some systems), so we have to clear 1398236769Sobrien * it before routing the shell's error output to the same place as 1399236769Sobrien * its standard output. 1400236769Sobrien */ 1401236769Sobrien (void)fcntl(1, F_SETFD, 0); 1402236769Sobrien if (dup2(1, 2) == -1) { 1403236769Sobrien execError("dup2", "1, 2"); 1404236769Sobrien _exit(1); 1405236769Sobrien } 1406236769Sobrien 1407236769Sobrien /* 1408236769Sobrien * We want to switch the child into a different process family so 1409236769Sobrien * we can kill it and all its descendants in one fell swoop, 1410236769Sobrien * by killing its process family, but not commit suicide. 1411236769Sobrien */ 1412236769Sobrien#if defined(HAVE_SETPGID) 1413236769Sobrien (void)setpgid(0, getpid()); 1414236769Sobrien#else 1415236769Sobrien#if defined(HAVE_SETSID) 1416236769Sobrien /* XXX: dsl - I'm sure this should be setpgrp()... */ 1417236769Sobrien (void)setsid(); 1418236769Sobrien#else 1419236769Sobrien (void)setpgrp(0, getpid()); 1420236769Sobrien#endif 1421236769Sobrien#endif 1422236769Sobrien 1423236769Sobrien Var_ExportVars(); 1424236769Sobrien 1425236769Sobrien (void)execv(shellPath, argv); 1426236769Sobrien execError("exec", shellPath); 1427236769Sobrien _exit(1); 1428236769Sobrien } 1429236769Sobrien 1430236769Sobrien /* Parent, continuing after the child exec */ 1431236769Sobrien job->pid = cpid; 1432236769Sobrien 1433236769Sobrien Trace_Log(JOBSTART, job); 1434236769Sobrien 1435236769Sobrien /* 1436236769Sobrien * Set the current position in the buffer to the beginning 1437236769Sobrien * and mark another stream to watch in the outputs mask 1438236769Sobrien */ 1439236769Sobrien job->curPos = 0; 1440236769Sobrien 1441236769Sobrien watchfd(job); 1442236769Sobrien 1443236769Sobrien if (job->cmdFILE != NULL && job->cmdFILE != stdout) { 1444236769Sobrien (void)fclose(job->cmdFILE); 1445236769Sobrien job->cmdFILE = NULL; 1446236769Sobrien } 1447236769Sobrien 1448236769Sobrien /* 1449236769Sobrien * Now the job is actually running, add it to the table. 1450236769Sobrien */ 1451236769Sobrien if (DEBUG(JOB)) { 1452236769Sobrien fprintf(debug_file, "JobExec(%s): pid %d added to jobs table\n", 1453236769Sobrien job->node->name, job->pid); 1454236769Sobrien job_table_dump("job started"); 1455236769Sobrien } 1456236769Sobrien JobSigUnlock(&mask); 1457236769Sobrien} 1458236769Sobrien 1459236769Sobrien/*- 1460236769Sobrien *----------------------------------------------------------------------- 1461236769Sobrien * JobMakeArgv -- 1462236769Sobrien * Create the argv needed to execute the shell for a given job. 1463236769Sobrien * 1464236769Sobrien * 1465236769Sobrien * Results: 1466236769Sobrien * 1467236769Sobrien * Side Effects: 1468236769Sobrien * 1469236769Sobrien *----------------------------------------------------------------------- 1470236769Sobrien */ 1471236769Sobrienstatic void 1472236769SobrienJobMakeArgv(Job *job, char **argv) 1473236769Sobrien{ 1474236769Sobrien int argc; 1475236769Sobrien static char args[10]; /* For merged arguments */ 1476236769Sobrien 1477236769Sobrien argv[0] = UNCONST(shellName); 1478236769Sobrien argc = 1; 1479236769Sobrien 1480236769Sobrien if ((commandShell->exit && (*commandShell->exit != '-')) || 1481236769Sobrien (commandShell->echo && (*commandShell->echo != '-'))) 1482236769Sobrien { 1483236769Sobrien /* 1484236769Sobrien * At least one of the flags doesn't have a minus before it, so 1485236769Sobrien * merge them together. Have to do this because the *(&(@*#*&#$# 1486236769Sobrien * Bourne shell thinks its second argument is a file to source. 1487236769Sobrien * Grrrr. Note the ten-character limitation on the combined arguments. 1488236769Sobrien */ 1489236769Sobrien (void)snprintf(args, sizeof(args), "-%s%s", 1490236769Sobrien ((job->flags & JOB_IGNERR) ? "" : 1491236769Sobrien (commandShell->exit ? commandShell->exit : "")), 1492236769Sobrien ((job->flags & JOB_SILENT) ? "" : 1493236769Sobrien (commandShell->echo ? commandShell->echo : ""))); 1494236769Sobrien 1495236769Sobrien if (args[1]) { 1496236769Sobrien argv[argc] = args; 1497236769Sobrien argc++; 1498236769Sobrien } 1499236769Sobrien } else { 1500236769Sobrien if (!(job->flags & JOB_IGNERR) && commandShell->exit) { 1501236769Sobrien argv[argc] = UNCONST(commandShell->exit); 1502236769Sobrien argc++; 1503236769Sobrien } 1504236769Sobrien if (!(job->flags & JOB_SILENT) && commandShell->echo) { 1505236769Sobrien argv[argc] = UNCONST(commandShell->echo); 1506236769Sobrien argc++; 1507236769Sobrien } 1508236769Sobrien } 1509236769Sobrien argv[argc] = NULL; 1510236769Sobrien} 1511236769Sobrien 1512236769Sobrien/*- 1513236769Sobrien *----------------------------------------------------------------------- 1514236769Sobrien * JobStart -- 1515236769Sobrien * Start a target-creation process going for the target described 1516236769Sobrien * by the graph node gn. 1517236769Sobrien * 1518236769Sobrien * Input: 1519236769Sobrien * gn target to create 1520236769Sobrien * flags flags for the job to override normal ones. 1521236769Sobrien * e.g. JOB_SPECIAL or JOB_IGNDOTS 1522236769Sobrien * previous The previous Job structure for this node, if any. 1523236769Sobrien * 1524236769Sobrien * Results: 1525236769Sobrien * JOB_ERROR if there was an error in the commands, JOB_FINISHED 1526236769Sobrien * if there isn't actually anything left to do for the job and 1527236769Sobrien * JOB_RUNNING if the job has been started. 1528236769Sobrien * 1529236769Sobrien * Side Effects: 1530236769Sobrien * A new Job node is created and added to the list of running 1531236769Sobrien * jobs. PMake is forked and a child shell created. 1532236769Sobrien * 1533236769Sobrien * NB: I'm fairly sure that this code is never called with JOB_SPECIAL set 1534236769Sobrien * JOB_IGNDOTS is never set (dsl) 1535236769Sobrien * Also the return value is ignored by everyone. 1536236769Sobrien *----------------------------------------------------------------------- 1537236769Sobrien */ 1538236769Sobrienstatic int 1539236769SobrienJobStart(GNode *gn, int flags) 1540236769Sobrien{ 1541236769Sobrien Job *job; /* new job descriptor */ 1542236769Sobrien char *argv[10]; /* Argument vector to shell */ 1543236769Sobrien Boolean cmdsOK; /* true if the nodes commands were all right */ 1544236769Sobrien Boolean noExec; /* Set true if we decide not to run the job */ 1545236769Sobrien int tfd; /* File descriptor to the temp file */ 1546236769Sobrien 1547236769Sobrien for (job = job_table; job < job_table_end; job++) { 1548236769Sobrien if (job->job_state == JOB_ST_FREE) 1549236769Sobrien break; 1550236769Sobrien } 1551236769Sobrien if (job >= job_table_end) 1552236769Sobrien Punt("JobStart no job slots vacant"); 1553236769Sobrien 1554236769Sobrien memset(job, 0, sizeof *job); 1555236769Sobrien job->job_state = JOB_ST_SETUP; 1556236769Sobrien if (gn->type & OP_SPECIAL) 1557236769Sobrien flags |= JOB_SPECIAL; 1558236769Sobrien 1559236769Sobrien job->node = gn; 1560236769Sobrien job->tailCmds = NULL; 1561236769Sobrien 1562236769Sobrien /* 1563236769Sobrien * Set the initial value of the flags for this job based on the global 1564236769Sobrien * ones and the node's attributes... Any flags supplied by the caller 1565236769Sobrien * are also added to the field. 1566236769Sobrien */ 1567236769Sobrien job->flags = 0; 1568236769Sobrien if (Targ_Ignore(gn)) { 1569236769Sobrien job->flags |= JOB_IGNERR; 1570236769Sobrien } 1571236769Sobrien if (Targ_Silent(gn)) { 1572236769Sobrien job->flags |= JOB_SILENT; 1573236769Sobrien } 1574236769Sobrien job->flags |= flags; 1575236769Sobrien 1576236769Sobrien /* 1577236769Sobrien * Check the commands now so any attributes from .DEFAULT have a chance 1578236769Sobrien * to migrate to the node 1579236769Sobrien */ 1580236769Sobrien cmdsOK = Job_CheckCommands(gn, Error); 1581236769Sobrien 1582236769Sobrien job->inPollfd = NULL; 1583236769Sobrien /* 1584236769Sobrien * If the -n flag wasn't given, we open up OUR (not the child's) 1585236769Sobrien * temporary file to stuff commands in it. The thing is rd/wr so we don't 1586236769Sobrien * need to reopen it to feed it to the shell. If the -n flag *was* given, 1587236769Sobrien * we just set the file to be stdout. Cute, huh? 1588236769Sobrien */ 1589236769Sobrien if (((gn->type & OP_MAKE) && !(noRecursiveExecute)) || 1590236769Sobrien (!noExecute && !touchFlag)) { 1591236769Sobrien /* 1592236769Sobrien * tfile is the name of a file into which all shell commands are 1593236769Sobrien * put. It is removed before the child shell is executed, unless 1594236769Sobrien * DEBUG(SCRIPT) is set. 1595236769Sobrien */ 1596236769Sobrien char *tfile; 1597236769Sobrien sigset_t mask; 1598236769Sobrien /* 1599236769Sobrien * We're serious here, but if the commands were bogus, we're 1600236769Sobrien * also dead... 1601236769Sobrien */ 1602236769Sobrien if (!cmdsOK) { 1603236769Sobrien PrintOnError(gn, NULL); /* provide some clue */ 1604236769Sobrien DieHorribly(); 1605236769Sobrien } 1606236769Sobrien 1607236769Sobrien JobSigLock(&mask); 1608236769Sobrien tfd = mkTempFile(TMPPAT, &tfile); 1609236769Sobrien if (!DEBUG(SCRIPT)) 1610236769Sobrien (void)eunlink(tfile); 1611236769Sobrien JobSigUnlock(&mask); 1612236769Sobrien 1613236769Sobrien job->cmdFILE = fdopen(tfd, "w+"); 1614236769Sobrien if (job->cmdFILE == NULL) { 1615236769Sobrien Punt("Could not fdopen %s", tfile); 1616236769Sobrien } 1617236769Sobrien (void)fcntl(FILENO(job->cmdFILE), F_SETFD, 1); 1618236769Sobrien /* 1619236769Sobrien * Send the commands to the command file, flush all its buffers then 1620236769Sobrien * rewind and remove the thing. 1621236769Sobrien */ 1622236769Sobrien noExec = FALSE; 1623236769Sobrien 1624236769Sobrien#ifdef USE_META 1625236769Sobrien if (useMeta) { 1626236769Sobrien meta_job_start(job, gn); 1627236769Sobrien if (Targ_Silent(gn)) { /* might have changed */ 1628236769Sobrien job->flags |= JOB_SILENT; 1629236769Sobrien } 1630236769Sobrien } 1631236769Sobrien#endif 1632236769Sobrien /* 1633236769Sobrien * We can do all the commands at once. hooray for sanity 1634236769Sobrien */ 1635236769Sobrien numCommands = 0; 1636236769Sobrien Lst_ForEach(gn->commands, JobPrintCommand, job); 1637236769Sobrien 1638236769Sobrien /* 1639236769Sobrien * If we didn't print out any commands to the shell script, 1640236769Sobrien * there's not much point in executing the shell, is there? 1641236769Sobrien */ 1642236769Sobrien if (numCommands == 0) { 1643236769Sobrien noExec = TRUE; 1644236769Sobrien } 1645236769Sobrien 1646236769Sobrien free(tfile); 1647236769Sobrien } else if (NoExecute(gn)) { 1648236769Sobrien /* 1649236769Sobrien * Not executing anything -- just print all the commands to stdout 1650236769Sobrien * in one fell swoop. This will still set up job->tailCmds correctly. 1651236769Sobrien */ 1652236769Sobrien if (lastNode != gn) { 1653236769Sobrien MESSAGE(stdout, gn); 1654236769Sobrien lastNode = gn; 1655236769Sobrien } 1656236769Sobrien job->cmdFILE = stdout; 1657236769Sobrien /* 1658236769Sobrien * Only print the commands if they're ok, but don't die if they're 1659236769Sobrien * not -- just let the user know they're bad and keep going. It 1660236769Sobrien * doesn't do any harm in this case and may do some good. 1661236769Sobrien */ 1662236769Sobrien if (cmdsOK) { 1663236769Sobrien Lst_ForEach(gn->commands, JobPrintCommand, job); 1664236769Sobrien } 1665236769Sobrien /* 1666236769Sobrien * Don't execute the shell, thank you. 1667236769Sobrien */ 1668236769Sobrien noExec = TRUE; 1669236769Sobrien } else { 1670236769Sobrien /* 1671236769Sobrien * Just touch the target and note that no shell should be executed. 1672236769Sobrien * Set cmdFILE to stdout to make life easier. Check the commands, too, 1673236769Sobrien * but don't die if they're no good -- it does no harm to keep working 1674236769Sobrien * up the graph. 1675236769Sobrien */ 1676236769Sobrien job->cmdFILE = stdout; 1677236769Sobrien Job_Touch(gn, job->flags&JOB_SILENT); 1678236769Sobrien noExec = TRUE; 1679236769Sobrien } 1680236769Sobrien /* Just in case it isn't already... */ 1681236769Sobrien (void)fflush(job->cmdFILE); 1682236769Sobrien 1683236769Sobrien /* 1684236769Sobrien * If we're not supposed to execute a shell, don't. 1685236769Sobrien */ 1686236769Sobrien if (noExec) { 1687236769Sobrien if (!(job->flags & JOB_SPECIAL)) 1688236769Sobrien Job_TokenReturn(); 1689236769Sobrien /* 1690236769Sobrien * Unlink and close the command file if we opened one 1691236769Sobrien */ 1692236769Sobrien if (job->cmdFILE != stdout) { 1693236769Sobrien if (job->cmdFILE != NULL) { 1694236769Sobrien (void)fclose(job->cmdFILE); 1695236769Sobrien job->cmdFILE = NULL; 1696236769Sobrien } 1697236769Sobrien } 1698236769Sobrien 1699236769Sobrien /* 1700236769Sobrien * We only want to work our way up the graph if we aren't here because 1701236769Sobrien * the commands for the job were no good. 1702236769Sobrien */ 1703236769Sobrien if (cmdsOK && aborting == 0) { 1704236769Sobrien if (job->tailCmds != NULL) { 1705236769Sobrien Lst_ForEachFrom(job->node->commands, job->tailCmds, 1706236769Sobrien JobSaveCommand, 1707236769Sobrien job->node); 1708236769Sobrien } 1709236769Sobrien job->node->made = MADE; 1710236769Sobrien Make_Update(job->node); 1711236769Sobrien } 1712236769Sobrien job->job_state = JOB_ST_FREE; 1713236769Sobrien return cmdsOK ? JOB_FINISHED : JOB_ERROR; 1714236769Sobrien } 1715236769Sobrien 1716236769Sobrien /* 1717236769Sobrien * Set up the control arguments to the shell. This is based on the flags 1718236769Sobrien * set earlier for this job. 1719236769Sobrien */ 1720236769Sobrien JobMakeArgv(job, argv); 1721236769Sobrien 1722236769Sobrien /* Create the pipe by which we'll get the shell's output. */ 1723236769Sobrien JobCreatePipe(job, 3); 1724236769Sobrien 1725236769Sobrien JobExec(job, argv); 1726236769Sobrien return(JOB_RUNNING); 1727236769Sobrien} 1728236769Sobrien 1729236769Sobrienstatic char * 1730236769SobrienJobOutput(Job *job, char *cp, char *endp, int msg) 1731236769Sobrien{ 1732236769Sobrien char *ecp; 1733236769Sobrien 1734236769Sobrien if (commandShell->noPrint) { 1735236769Sobrien ecp = Str_FindSubstring(cp, commandShell->noPrint); 1736236769Sobrien while (ecp != NULL) { 1737236769Sobrien if (cp != ecp) { 1738236769Sobrien *ecp = '\0'; 1739236769Sobrien if (!beSilent && msg && job->node != lastNode) { 1740236769Sobrien MESSAGE(stdout, job->node); 1741236769Sobrien lastNode = job->node; 1742236769Sobrien } 1743236769Sobrien /* 1744236769Sobrien * The only way there wouldn't be a newline after 1745236769Sobrien * this line is if it were the last in the buffer. 1746236769Sobrien * however, since the non-printable comes after it, 1747236769Sobrien * there must be a newline, so we don't print one. 1748236769Sobrien */ 1749236769Sobrien (void)fprintf(stdout, "%s", cp); 1750236769Sobrien (void)fflush(stdout); 1751236769Sobrien } 1752236769Sobrien cp = ecp + commandShell->noPLen; 1753236769Sobrien if (cp != endp) { 1754236769Sobrien /* 1755236769Sobrien * Still more to print, look again after skipping 1756236769Sobrien * the whitespace following the non-printable 1757236769Sobrien * command.... 1758236769Sobrien */ 1759236769Sobrien cp++; 1760236769Sobrien while (*cp == ' ' || *cp == '\t' || *cp == '\n') { 1761236769Sobrien cp++; 1762236769Sobrien } 1763236769Sobrien ecp = Str_FindSubstring(cp, commandShell->noPrint); 1764236769Sobrien } else { 1765236769Sobrien return cp; 1766236769Sobrien } 1767236769Sobrien } 1768236769Sobrien } 1769236769Sobrien return cp; 1770236769Sobrien} 1771236769Sobrien 1772236769Sobrien/*- 1773236769Sobrien *----------------------------------------------------------------------- 1774236769Sobrien * JobDoOutput -- 1775236769Sobrien * This function is called at different times depending on 1776236769Sobrien * whether the user has specified that output is to be collected 1777236769Sobrien * via pipes or temporary files. In the former case, we are called 1778236769Sobrien * whenever there is something to read on the pipe. We collect more 1779236769Sobrien * output from the given job and store it in the job's outBuf. If 1780236769Sobrien * this makes up a line, we print it tagged by the job's identifier, 1781236769Sobrien * as necessary. 1782236769Sobrien * If output has been collected in a temporary file, we open the 1783236769Sobrien * file and read it line by line, transfering it to our own 1784236769Sobrien * output channel until the file is empty. At which point we 1785236769Sobrien * remove the temporary file. 1786236769Sobrien * In both cases, however, we keep our figurative eye out for the 1787236769Sobrien * 'noPrint' line for the shell from which the output came. If 1788236769Sobrien * we recognize a line, we don't print it. If the command is not 1789236769Sobrien * alone on the line (the character after it is not \0 or \n), we 1790236769Sobrien * do print whatever follows it. 1791236769Sobrien * 1792236769Sobrien * Input: 1793236769Sobrien * job the job whose output needs printing 1794236769Sobrien * finish TRUE if this is the last time we'll be called 1795236769Sobrien * for this job 1796236769Sobrien * 1797236769Sobrien * Results: 1798236769Sobrien * None 1799236769Sobrien * 1800236769Sobrien * Side Effects: 1801236769Sobrien * curPos may be shifted as may the contents of outBuf. 1802236769Sobrien *----------------------------------------------------------------------- 1803236769Sobrien */ 1804236769SobrienSTATIC void 1805236769SobrienJobDoOutput(Job *job, Boolean finish) 1806236769Sobrien{ 1807236769Sobrien Boolean gotNL = FALSE; /* true if got a newline */ 1808236769Sobrien Boolean fbuf; /* true if our buffer filled up */ 1809236769Sobrien int nr; /* number of bytes read */ 1810236769Sobrien int i; /* auxiliary index into outBuf */ 1811236769Sobrien int max; /* limit for i (end of current data) */ 1812236769Sobrien int nRead; /* (Temporary) number of bytes read */ 1813236769Sobrien 1814236769Sobrien /* 1815236769Sobrien * Read as many bytes as will fit in the buffer. 1816236769Sobrien */ 1817236769Sobrienend_loop: 1818236769Sobrien gotNL = FALSE; 1819236769Sobrien fbuf = FALSE; 1820236769Sobrien 1821236769Sobrien nRead = read(job->inPipe, &job->outBuf[job->curPos], 1822236769Sobrien JOB_BUFSIZE - job->curPos); 1823236769Sobrien if (nRead < 0) { 1824236769Sobrien if (errno == EAGAIN) 1825236769Sobrien return; 1826236769Sobrien if (DEBUG(JOB)) { 1827236769Sobrien perror("JobDoOutput(piperead)"); 1828236769Sobrien } 1829236769Sobrien nr = 0; 1830236769Sobrien } else { 1831236769Sobrien nr = nRead; 1832236769Sobrien } 1833236769Sobrien 1834236769Sobrien /* 1835236769Sobrien * If we hit the end-of-file (the job is dead), we must flush its 1836236769Sobrien * remaining output, so pretend we read a newline if there's any 1837236769Sobrien * output remaining in the buffer. 1838236769Sobrien * Also clear the 'finish' flag so we stop looping. 1839236769Sobrien */ 1840236769Sobrien if ((nr == 0) && (job->curPos != 0)) { 1841236769Sobrien job->outBuf[job->curPos] = '\n'; 1842236769Sobrien nr = 1; 1843236769Sobrien finish = FALSE; 1844236769Sobrien } else if (nr == 0) { 1845236769Sobrien finish = FALSE; 1846236769Sobrien } 1847236769Sobrien 1848236769Sobrien /* 1849236769Sobrien * Look for the last newline in the bytes we just got. If there is 1850236769Sobrien * one, break out of the loop with 'i' as its index and gotNL set 1851236769Sobrien * TRUE. 1852236769Sobrien */ 1853236769Sobrien max = job->curPos + nr; 1854236769Sobrien for (i = job->curPos + nr - 1; i >= job->curPos; i--) { 1855236769Sobrien if (job->outBuf[i] == '\n') { 1856236769Sobrien gotNL = TRUE; 1857236769Sobrien break; 1858236769Sobrien } else if (job->outBuf[i] == '\0') { 1859236769Sobrien /* 1860236769Sobrien * Why? 1861236769Sobrien */ 1862236769Sobrien job->outBuf[i] = ' '; 1863236769Sobrien } 1864236769Sobrien } 1865236769Sobrien 1866236769Sobrien if (!gotNL) { 1867236769Sobrien job->curPos += nr; 1868236769Sobrien if (job->curPos == JOB_BUFSIZE) { 1869236769Sobrien /* 1870236769Sobrien * If we've run out of buffer space, we have no choice 1871236769Sobrien * but to print the stuff. sigh. 1872236769Sobrien */ 1873236769Sobrien fbuf = TRUE; 1874236769Sobrien i = job->curPos; 1875236769Sobrien } 1876236769Sobrien } 1877236769Sobrien if (gotNL || fbuf) { 1878236769Sobrien /* 1879236769Sobrien * Need to send the output to the screen. Null terminate it 1880236769Sobrien * first, overwriting the newline character if there was one. 1881236769Sobrien * So long as the line isn't one we should filter (according 1882236769Sobrien * to the shell description), we print the line, preceded 1883236769Sobrien * by a target banner if this target isn't the same as the 1884236769Sobrien * one for which we last printed something. 1885236769Sobrien * The rest of the data in the buffer are then shifted down 1886236769Sobrien * to the start of the buffer and curPos is set accordingly. 1887236769Sobrien */ 1888236769Sobrien job->outBuf[i] = '\0'; 1889236769Sobrien if (i >= job->curPos) { 1890236769Sobrien char *cp; 1891236769Sobrien 1892236769Sobrien cp = JobOutput(job, job->outBuf, &job->outBuf[i], FALSE); 1893236769Sobrien 1894236769Sobrien /* 1895236769Sobrien * There's still more in that thar buffer. This time, though, 1896236769Sobrien * we know there's no newline at the end, so we add one of 1897236769Sobrien * our own free will. 1898236769Sobrien */ 1899236769Sobrien if (*cp != '\0') { 1900236769Sobrien if (!beSilent && job->node != lastNode) { 1901236769Sobrien MESSAGE(stdout, job->node); 1902236769Sobrien lastNode = job->node; 1903236769Sobrien } 1904236769Sobrien#ifdef USE_META 1905236769Sobrien if (useMeta) { 1906236769Sobrien meta_job_output(job, cp, gotNL ? "\n" : ""); 1907236769Sobrien } 1908236769Sobrien#endif 1909236769Sobrien (void)fprintf(stdout, "%s%s", cp, gotNL ? "\n" : ""); 1910236769Sobrien (void)fflush(stdout); 1911236769Sobrien } 1912236769Sobrien } 1913236769Sobrien if (i < max - 1) { 1914236769Sobrien /* shift the remaining characters down */ 1915236769Sobrien (void)memcpy(job->outBuf, &job->outBuf[i + 1], max - (i + 1)); 1916236769Sobrien job->curPos = max - (i + 1); 1917236769Sobrien 1918236769Sobrien } else { 1919236769Sobrien /* 1920236769Sobrien * We have written everything out, so we just start over 1921236769Sobrien * from the start of the buffer. No copying. No nothing. 1922236769Sobrien */ 1923236769Sobrien job->curPos = 0; 1924236769Sobrien } 1925236769Sobrien } 1926236769Sobrien if (finish) { 1927236769Sobrien /* 1928236769Sobrien * If the finish flag is true, we must loop until we hit 1929236769Sobrien * end-of-file on the pipe. This is guaranteed to happen 1930236769Sobrien * eventually since the other end of the pipe is now closed 1931236769Sobrien * (we closed it explicitly and the child has exited). When 1932236769Sobrien * we do get an EOF, finish will be set FALSE and we'll fall 1933236769Sobrien * through and out. 1934236769Sobrien */ 1935236769Sobrien goto end_loop; 1936236769Sobrien } 1937236769Sobrien} 1938236769Sobrien 1939236769Sobrienstatic void 1940236769SobrienJobRun(GNode *targ) 1941236769Sobrien{ 1942236769Sobrien#ifdef notyet 1943236769Sobrien /* 1944236769Sobrien * Unfortunately it is too complicated to run .BEGIN, .END, 1945236769Sobrien * and .INTERRUPT job in the parallel job module. This has 1946236769Sobrien * the nice side effect that it avoids a lot of other problems. 1947236769Sobrien */ 1948236769Sobrien Lst lst = Lst_Init(FALSE); 1949236769Sobrien Lst_AtEnd(lst, targ); 1950236769Sobrien (void)Make_Run(lst); 1951236769Sobrien Lst_Destroy(lst, NULL); 1952236769Sobrien JobStart(targ, JOB_SPECIAL); 1953236769Sobrien while (jobTokensRunning) { 1954236769Sobrien Job_CatchOutput(); 1955236769Sobrien } 1956236769Sobrien#else 1957236769Sobrien Compat_Make(targ, targ); 1958236769Sobrien if (targ->made == ERROR) { 1959236769Sobrien PrintOnError(targ, "\n\nStop."); 1960236769Sobrien exit(1); 1961236769Sobrien } 1962236769Sobrien#endif 1963236769Sobrien} 1964236769Sobrien 1965236769Sobrien/*- 1966236769Sobrien *----------------------------------------------------------------------- 1967236769Sobrien * Job_CatchChildren -- 1968236769Sobrien * Handle the exit of a child. Called from Make_Make. 1969236769Sobrien * 1970236769Sobrien * Input: 1971236769Sobrien * block TRUE if should block on the wait 1972236769Sobrien * 1973236769Sobrien * Results: 1974236769Sobrien * none. 1975236769Sobrien * 1976236769Sobrien * Side Effects: 1977236769Sobrien * The job descriptor is removed from the list of children. 1978236769Sobrien * 1979236769Sobrien * Notes: 1980236769Sobrien * We do waits, blocking or not, according to the wisdom of our 1981236769Sobrien * caller, until there are no more children to report. For each 1982236769Sobrien * job, call JobFinish to finish things off. 1983236769Sobrien * 1984236769Sobrien *----------------------------------------------------------------------- 1985236769Sobrien */ 1986236769Sobrien 1987236769Sobrienvoid 1988236769SobrienJob_CatchChildren(void) 1989236769Sobrien{ 1990236769Sobrien int pid; /* pid of dead child */ 1991236769Sobrien WAIT_T status; /* Exit/termination status */ 1992236769Sobrien 1993236769Sobrien /* 1994236769Sobrien * Don't even bother if we know there's no one around. 1995236769Sobrien */ 1996236769Sobrien if (jobTokensRunning == 0) 1997236769Sobrien return; 1998236769Sobrien 1999236769Sobrien while ((pid = waitpid((pid_t) -1, &status, WNOHANG | WUNTRACED)) > 0) { 2000236769Sobrien if (DEBUG(JOB)) { 2001236769Sobrien (void)fprintf(debug_file, "Process %d exited/stopped status %x.\n", pid, 2002236769Sobrien WAIT_STATUS(status)); 2003236769Sobrien } 2004236769Sobrien JobReapChild(pid, status, TRUE); 2005236769Sobrien } 2006236769Sobrien} 2007236769Sobrien 2008236769Sobrien/* 2009236769Sobrien * It is possible that wait[pid]() was called from elsewhere, 2010236769Sobrien * this lets us reap jobs regardless. 2011236769Sobrien */ 2012236769Sobrienvoid 2013236769SobrienJobReapChild(pid_t pid, WAIT_T status, Boolean isJobs) 2014236769Sobrien{ 2015236769Sobrien Job *job; /* job descriptor for dead child */ 2016236769Sobrien 2017236769Sobrien /* 2018236769Sobrien * Don't even bother if we know there's no one around. 2019236769Sobrien */ 2020236769Sobrien if (jobTokensRunning == 0) 2021236769Sobrien return; 2022236769Sobrien 2023236769Sobrien job = JobFindPid(pid, JOB_ST_RUNNING, isJobs); 2024236769Sobrien if (job == NULL) { 2025236769Sobrien if (isJobs) { 2026236769Sobrien if (!lurking_children) 2027236769Sobrien Error("Child (%d) status %x not in table?", pid, status); 2028236769Sobrien } 2029236769Sobrien return; /* not ours */ 2030236769Sobrien } 2031236769Sobrien if (WIFSTOPPED(status)) { 2032236769Sobrien if (DEBUG(JOB)) { 2033236769Sobrien (void)fprintf(debug_file, "Process %d (%s) stopped.\n", 2034236769Sobrien job->pid, job->node->name); 2035236769Sobrien } 2036236769Sobrien if (!make_suspended) { 2037236769Sobrien switch (WSTOPSIG(status)) { 2038236769Sobrien case SIGTSTP: 2039236769Sobrien (void)printf("*** [%s] Suspended\n", job->node->name); 2040236769Sobrien break; 2041236769Sobrien case SIGSTOP: 2042236769Sobrien (void)printf("*** [%s] Stopped\n", job->node->name); 2043236769Sobrien break; 2044236769Sobrien default: 2045236769Sobrien (void)printf("*** [%s] Stopped -- signal %d\n", 2046236769Sobrien job->node->name, WSTOPSIG(status)); 2047236769Sobrien } 2048236769Sobrien job->job_suspended = 1; 2049236769Sobrien } 2050236769Sobrien (void)fflush(stdout); 2051236769Sobrien return; 2052236769Sobrien } 2053236769Sobrien 2054236769Sobrien job->job_state = JOB_ST_FINISHED; 2055236769Sobrien job->exit_status = WAIT_STATUS(status); 2056236769Sobrien 2057236769Sobrien JobFinish(job, status); 2058236769Sobrien} 2059236769Sobrien 2060236769Sobrien/*- 2061236769Sobrien *----------------------------------------------------------------------- 2062236769Sobrien * Job_CatchOutput -- 2063236769Sobrien * Catch the output from our children, if we're using 2064236769Sobrien * pipes do so. Otherwise just block time until we get a 2065236769Sobrien * signal(most likely a SIGCHLD) since there's no point in 2066236769Sobrien * just spinning when there's nothing to do and the reaping 2067236769Sobrien * of a child can wait for a while. 2068236769Sobrien * 2069236769Sobrien * Results: 2070236769Sobrien * None 2071236769Sobrien * 2072236769Sobrien * Side Effects: 2073236769Sobrien * Output is read from pipes if we're piping. 2074236769Sobrien * ----------------------------------------------------------------------- 2075236769Sobrien */ 2076236769Sobrienvoid 2077236769SobrienJob_CatchOutput(void) 2078236769Sobrien{ 2079236769Sobrien int nready; 2080236769Sobrien Job *job; 2081236769Sobrien int i; 2082236769Sobrien 2083236769Sobrien (void)fflush(stdout); 2084236769Sobrien 2085236769Sobrien /* The first fd in the list is the job token pipe */ 2086249033Ssjg do { 2087249033Ssjg nready = poll(fds + 1 - wantToken, nfds - 1 + wantToken, POLL_MSEC); 2088249033Ssjg } while (nready < 0 && errno == EINTR); 2089236769Sobrien 2090249033Ssjg if (nready < 0) 2091249033Ssjg Punt("poll: %s", strerror(errno)); 2092249033Ssjg 2093249033Ssjg if (nready > 0 && readyfd(&childExitJob)) { 2094236769Sobrien char token = 0; 2095249033Ssjg ssize_t count; 2096249033Ssjg count = read(childExitJob.inPipe, &token, 1); 2097249033Ssjg switch (count) { 2098249033Ssjg case 0: 2099249033Ssjg Punt("unexpected eof on token pipe"); 2100249033Ssjg case -1: 2101249033Ssjg Punt("token pipe read: %s", strerror(errno)); 2102249033Ssjg case 1: 2103249033Ssjg if (token == DO_JOB_RESUME[0]) 2104249033Ssjg /* Complete relay requested from our SIGCONT handler */ 2105249033Ssjg JobRestartJobs(); 2106249033Ssjg break; 2107249033Ssjg default: 2108249033Ssjg abort(); 2109249033Ssjg } 2110249033Ssjg --nready; 2111236769Sobrien } 2112236769Sobrien 2113249033Ssjg Job_CatchChildren(); 2114249033Ssjg if (nready == 0) 2115249033Ssjg return; 2116236769Sobrien 2117236769Sobrien for (i = 2; i < nfds; i++) { 2118236769Sobrien if (!fds[i].revents) 2119236769Sobrien continue; 2120236769Sobrien job = jobfds[i]; 2121249033Ssjg if (job->job_state == JOB_ST_RUNNING) 2122249033Ssjg JobDoOutput(job, FALSE); 2123249033Ssjg if (--nready == 0) 2124249033Ssjg return; 2125236769Sobrien } 2126236769Sobrien} 2127236769Sobrien 2128236769Sobrien/*- 2129236769Sobrien *----------------------------------------------------------------------- 2130236769Sobrien * Job_Make -- 2131236769Sobrien * Start the creation of a target. Basically a front-end for 2132236769Sobrien * JobStart used by the Make module. 2133236769Sobrien * 2134236769Sobrien * Results: 2135236769Sobrien * None. 2136236769Sobrien * 2137236769Sobrien * Side Effects: 2138236769Sobrien * Another job is started. 2139236769Sobrien * 2140236769Sobrien *----------------------------------------------------------------------- 2141236769Sobrien */ 2142236769Sobrienvoid 2143236769SobrienJob_Make(GNode *gn) 2144236769Sobrien{ 2145236769Sobrien (void)JobStart(gn, 0); 2146236769Sobrien} 2147236769Sobrien 2148236769Sobrienvoid 2149236769SobrienShell_Init(void) 2150236769Sobrien{ 2151236769Sobrien if (shellPath == NULL) { 2152236769Sobrien /* 2153236769Sobrien * We are using the default shell, which may be an absolute 2154236769Sobrien * path if DEFSHELL_CUSTOM is defined. 2155236769Sobrien */ 2156236769Sobrien shellName = commandShell->name; 2157236769Sobrien#ifdef DEFSHELL_CUSTOM 2158236769Sobrien if (*shellName == '/') { 2159236769Sobrien shellPath = shellName; 2160236769Sobrien shellName = strrchr(shellPath, '/'); 2161236769Sobrien shellName++; 2162236769Sobrien } else 2163236769Sobrien#endif 2164236769Sobrien shellPath = str_concat(_PATH_DEFSHELLDIR, shellName, STR_ADDSLASH); 2165236769Sobrien } 2166236769Sobrien if (commandShell->exit == NULL) { 2167236769Sobrien commandShell->exit = ""; 2168236769Sobrien } 2169236769Sobrien if (commandShell->echo == NULL) { 2170236769Sobrien commandShell->echo = ""; 2171236769Sobrien } 2172253883Ssjg if (commandShell->hasErrCtl && *commandShell->exit) { 2173253883Ssjg if (shellErrFlag && 2174253883Ssjg strcmp(commandShell->exit, &shellErrFlag[1]) != 0) { 2175253883Ssjg free(shellErrFlag); 2176253883Ssjg shellErrFlag = NULL; 2177253883Ssjg } 2178253883Ssjg if (!shellErrFlag) { 2179253883Ssjg int n = strlen(commandShell->exit) + 2; 2180253883Ssjg 2181253883Ssjg shellErrFlag = bmake_malloc(n); 2182253883Ssjg if (shellErrFlag) { 2183253883Ssjg snprintf(shellErrFlag, n, "-%s", commandShell->exit); 2184253883Ssjg } 2185253883Ssjg } 2186253883Ssjg } else if (shellErrFlag) { 2187253883Ssjg free(shellErrFlag); 2188253883Ssjg shellErrFlag = NULL; 2189253883Ssjg } 2190236769Sobrien} 2191236769Sobrien 2192236769Sobrien/*- 2193236769Sobrien * Returns the string literal that is used in the current command shell 2194236769Sobrien * to produce a newline character. 2195236769Sobrien */ 2196236769Sobrienconst char * 2197236769SobrienShell_GetNewline(void) 2198236769Sobrien{ 2199236769Sobrien 2200236769Sobrien return commandShell->newline; 2201236769Sobrien} 2202236769Sobrien 2203236769Sobrienvoid 2204236769SobrienJob_SetPrefix(void) 2205236769Sobrien{ 2206236769Sobrien 2207236769Sobrien if (targPrefix) { 2208236769Sobrien free(targPrefix); 2209236769Sobrien } else if (!Var_Exists(MAKE_JOB_PREFIX, VAR_GLOBAL)) { 2210236769Sobrien Var_Set(MAKE_JOB_PREFIX, "---", VAR_GLOBAL, 0); 2211236769Sobrien } 2212236769Sobrien 2213236769Sobrien targPrefix = Var_Subst(NULL, "${" MAKE_JOB_PREFIX "}", VAR_GLOBAL, 0); 2214236769Sobrien} 2215236769Sobrien 2216236769Sobrien/*- 2217236769Sobrien *----------------------------------------------------------------------- 2218236769Sobrien * Job_Init -- 2219236769Sobrien * Initialize the process module 2220236769Sobrien * 2221236769Sobrien * Input: 2222236769Sobrien * 2223236769Sobrien * Results: 2224236769Sobrien * none 2225236769Sobrien * 2226236769Sobrien * Side Effects: 2227236769Sobrien * lists and counters are initialized 2228236769Sobrien *----------------------------------------------------------------------- 2229236769Sobrien */ 2230236769Sobrienvoid 2231236769SobrienJob_Init(void) 2232236769Sobrien{ 2233253925Ssjg Job_SetPrefix(); 2234236769Sobrien /* Allocate space for all the job info */ 2235236769Sobrien job_table = bmake_malloc(maxJobs * sizeof *job_table); 2236236769Sobrien memset(job_table, 0, maxJobs * sizeof *job_table); 2237236769Sobrien job_table_end = job_table + maxJobs; 2238236769Sobrien wantToken = 0; 2239236769Sobrien 2240236769Sobrien aborting = 0; 2241236769Sobrien errors = 0; 2242236769Sobrien 2243236769Sobrien lastNode = NULL; 2244236769Sobrien 2245254419Ssjg Always_pass_job_queue = getBoolean(MAKE_ALWAYS_PASS_JOB_QUEUE, 2246254419Ssjg Always_pass_job_queue); 2247254419Ssjg 2248255285Ssjg Job_error_token = getBoolean(MAKE_JOB_ERROR_TOKEN, Job_error_token); 2249255285Ssjg 2250255285Ssjg 2251236769Sobrien /* 2252236769Sobrien * There is a non-zero chance that we already have children. 2253236769Sobrien * eg after 'make -f- <<EOF' 2254236769Sobrien * Since their termination causes a 'Child (pid) not in table' message, 2255236769Sobrien * Collect the status of any that are already dead, and suppress the 2256236769Sobrien * error message if there are any undead ones. 2257236769Sobrien */ 2258236769Sobrien for (;;) { 2259236769Sobrien int rval, status; 2260236769Sobrien rval = waitpid((pid_t) -1, &status, WNOHANG); 2261236769Sobrien if (rval > 0) 2262236769Sobrien continue; 2263236769Sobrien if (rval == 0) 2264236769Sobrien lurking_children = 1; 2265236769Sobrien break; 2266236769Sobrien } 2267236769Sobrien 2268236769Sobrien Shell_Init(); 2269236769Sobrien 2270236769Sobrien JobCreatePipe(&childExitJob, 3); 2271236769Sobrien 2272236769Sobrien /* We can only need to wait for tokens, children and output from each job */ 2273236769Sobrien fds = bmake_malloc(sizeof (*fds) * (2 + maxJobs)); 2274236769Sobrien jobfds = bmake_malloc(sizeof (*jobfds) * (2 + maxJobs)); 2275236769Sobrien 2276236769Sobrien /* These are permanent entries and take slots 0 and 1 */ 2277236769Sobrien watchfd(&tokenWaitJob); 2278236769Sobrien watchfd(&childExitJob); 2279236769Sobrien 2280236769Sobrien sigemptyset(&caught_signals); 2281236769Sobrien /* 2282236769Sobrien * Install a SIGCHLD handler. 2283236769Sobrien */ 2284236769Sobrien (void)bmake_signal(SIGCHLD, JobChildSig); 2285236769Sobrien sigaddset(&caught_signals, SIGCHLD); 2286236769Sobrien 2287236769Sobrien#define ADDSIG(s,h) \ 2288236769Sobrien if (bmake_signal(s, SIG_IGN) != SIG_IGN) { \ 2289236769Sobrien sigaddset(&caught_signals, s); \ 2290236769Sobrien (void)bmake_signal(s, h); \ 2291236769Sobrien } 2292236769Sobrien 2293236769Sobrien /* 2294236769Sobrien * Catch the four signals that POSIX specifies if they aren't ignored. 2295236769Sobrien * JobPassSig will take care of calling JobInterrupt if appropriate. 2296236769Sobrien */ 2297236769Sobrien ADDSIG(SIGINT, JobPassSig_int) 2298236769Sobrien ADDSIG(SIGHUP, JobPassSig_term) 2299236769Sobrien ADDSIG(SIGTERM, JobPassSig_term) 2300236769Sobrien ADDSIG(SIGQUIT, JobPassSig_term) 2301236769Sobrien 2302236769Sobrien /* 2303236769Sobrien * There are additional signals that need to be caught and passed if 2304236769Sobrien * either the export system wants to be told directly of signals or if 2305236769Sobrien * we're giving each job its own process group (since then it won't get 2306236769Sobrien * signals from the terminal driver as we own the terminal) 2307236769Sobrien */ 2308236769Sobrien ADDSIG(SIGTSTP, JobPassSig_suspend) 2309236769Sobrien ADDSIG(SIGTTOU, JobPassSig_suspend) 2310236769Sobrien ADDSIG(SIGTTIN, JobPassSig_suspend) 2311236769Sobrien ADDSIG(SIGWINCH, JobCondPassSig) 2312236769Sobrien ADDSIG(SIGCONT, JobContinueSig) 2313236769Sobrien#undef ADDSIG 2314236769Sobrien 2315249033Ssjg (void)Job_RunTarget(".BEGIN", NULL); 2316236769Sobrien postCommands = Targ_FindNode(".END", TARG_CREATE); 2317236769Sobrien} 2318236769Sobrien 2319236769Sobrienstatic void JobSigReset(void) 2320236769Sobrien{ 2321236769Sobrien#define DELSIG(s) \ 2322236769Sobrien if (sigismember(&caught_signals, s)) { \ 2323236769Sobrien (void)bmake_signal(s, SIG_DFL); \ 2324236769Sobrien } 2325236769Sobrien 2326236769Sobrien DELSIG(SIGINT) 2327236769Sobrien DELSIG(SIGHUP) 2328236769Sobrien DELSIG(SIGQUIT) 2329236769Sobrien DELSIG(SIGTERM) 2330236769Sobrien DELSIG(SIGTSTP) 2331236769Sobrien DELSIG(SIGTTOU) 2332236769Sobrien DELSIG(SIGTTIN) 2333236769Sobrien DELSIG(SIGWINCH) 2334236769Sobrien DELSIG(SIGCONT) 2335236769Sobrien#undef DELSIG 2336236769Sobrien (void)bmake_signal(SIGCHLD, SIG_DFL); 2337236769Sobrien} 2338236769Sobrien 2339236769Sobrien/*- 2340236769Sobrien *----------------------------------------------------------------------- 2341236769Sobrien * JobMatchShell -- 2342236769Sobrien * Find a shell in 'shells' given its name. 2343236769Sobrien * 2344236769Sobrien * Results: 2345236769Sobrien * A pointer to the Shell structure. 2346236769Sobrien * 2347236769Sobrien * Side Effects: 2348236769Sobrien * None. 2349236769Sobrien * 2350236769Sobrien *----------------------------------------------------------------------- 2351236769Sobrien */ 2352236769Sobrienstatic Shell * 2353236769SobrienJobMatchShell(const char *name) 2354236769Sobrien{ 2355236769Sobrien Shell *sh; 2356236769Sobrien 2357236769Sobrien for (sh = shells; sh->name != NULL; sh++) { 2358236769Sobrien if (strcmp(name, sh->name) == 0) 2359236769Sobrien return (sh); 2360236769Sobrien } 2361236769Sobrien return NULL; 2362236769Sobrien} 2363236769Sobrien 2364236769Sobrien/*- 2365236769Sobrien *----------------------------------------------------------------------- 2366236769Sobrien * Job_ParseShell -- 2367236769Sobrien * Parse a shell specification and set up commandShell, shellPath 2368236769Sobrien * and shellName appropriately. 2369236769Sobrien * 2370236769Sobrien * Input: 2371236769Sobrien * line The shell spec 2372236769Sobrien * 2373236769Sobrien * Results: 2374236769Sobrien * FAILURE if the specification was incorrect. 2375236769Sobrien * 2376236769Sobrien * Side Effects: 2377236769Sobrien * commandShell points to a Shell structure (either predefined or 2378236769Sobrien * created from the shell spec), shellPath is the full path of the 2379236769Sobrien * shell described by commandShell, while shellName is just the 2380236769Sobrien * final component of shellPath. 2381236769Sobrien * 2382236769Sobrien * Notes: 2383236769Sobrien * A shell specification consists of a .SHELL target, with dependency 2384236769Sobrien * operator, followed by a series of blank-separated words. Double 2385236769Sobrien * quotes can be used to use blanks in words. A backslash escapes 2386236769Sobrien * anything (most notably a double-quote and a space) and 2387236769Sobrien * provides the functionality it does in C. Each word consists of 2388236769Sobrien * keyword and value separated by an equal sign. There should be no 2389236769Sobrien * unnecessary spaces in the word. The keywords are as follows: 2390236769Sobrien * name Name of shell. 2391236769Sobrien * path Location of shell. 2392236769Sobrien * quiet Command to turn off echoing. 2393236769Sobrien * echo Command to turn echoing on 2394236769Sobrien * filter Result of turning off echoing that shouldn't be 2395236769Sobrien * printed. 2396236769Sobrien * echoFlag Flag to turn echoing on at the start 2397236769Sobrien * errFlag Flag to turn error checking on at the start 2398236769Sobrien * hasErrCtl True if shell has error checking control 2399236769Sobrien * newline String literal to represent a newline char 2400236769Sobrien * check Command to turn on error checking if hasErrCtl 2401236769Sobrien * is TRUE or template of command to echo a command 2402236769Sobrien * for which error checking is off if hasErrCtl is 2403236769Sobrien * FALSE. 2404236769Sobrien * ignore Command to turn off error checking if hasErrCtl 2405236769Sobrien * is TRUE or template of command to execute a 2406236769Sobrien * command so as to ignore any errors it returns if 2407236769Sobrien * hasErrCtl is FALSE. 2408236769Sobrien * 2409236769Sobrien *----------------------------------------------------------------------- 2410236769Sobrien */ 2411236769SobrienReturnStatus 2412236769SobrienJob_ParseShell(char *line) 2413236769Sobrien{ 2414236769Sobrien char **words; 2415236769Sobrien char **argv; 2416236769Sobrien int argc; 2417236769Sobrien char *path; 2418236769Sobrien Shell newShell; 2419236769Sobrien Boolean fullSpec = FALSE; 2420236769Sobrien Shell *sh; 2421236769Sobrien 2422236769Sobrien while (isspace((unsigned char)*line)) { 2423236769Sobrien line++; 2424236769Sobrien } 2425236769Sobrien 2426236769Sobrien if (shellArgv) 2427236769Sobrien free(UNCONST(shellArgv)); 2428236769Sobrien 2429236769Sobrien memset(&newShell, 0, sizeof(newShell)); 2430236769Sobrien 2431236769Sobrien /* 2432236769Sobrien * Parse the specification by keyword 2433236769Sobrien */ 2434236769Sobrien words = brk_string(line, &argc, TRUE, &path); 2435236769Sobrien if (words == NULL) { 2436236769Sobrien Error("Unterminated quoted string [%s]", line); 2437236769Sobrien return FAILURE; 2438236769Sobrien } 2439236769Sobrien shellArgv = path; 2440236769Sobrien 2441236769Sobrien for (path = NULL, argv = words; argc != 0; argc--, argv++) { 2442236769Sobrien if (strncmp(*argv, "path=", 5) == 0) { 2443236769Sobrien path = &argv[0][5]; 2444236769Sobrien } else if (strncmp(*argv, "name=", 5) == 0) { 2445236769Sobrien newShell.name = &argv[0][5]; 2446236769Sobrien } else { 2447236769Sobrien if (strncmp(*argv, "quiet=", 6) == 0) { 2448236769Sobrien newShell.echoOff = &argv[0][6]; 2449236769Sobrien } else if (strncmp(*argv, "echo=", 5) == 0) { 2450236769Sobrien newShell.echoOn = &argv[0][5]; 2451236769Sobrien } else if (strncmp(*argv, "filter=", 7) == 0) { 2452236769Sobrien newShell.noPrint = &argv[0][7]; 2453236769Sobrien newShell.noPLen = strlen(newShell.noPrint); 2454236769Sobrien } else if (strncmp(*argv, "echoFlag=", 9) == 0) { 2455236769Sobrien newShell.echo = &argv[0][9]; 2456236769Sobrien } else if (strncmp(*argv, "errFlag=", 8) == 0) { 2457236769Sobrien newShell.exit = &argv[0][8]; 2458236769Sobrien } else if (strncmp(*argv, "hasErrCtl=", 10) == 0) { 2459236769Sobrien char c = argv[0][10]; 2460236769Sobrien newShell.hasErrCtl = !((c != 'Y') && (c != 'y') && 2461236769Sobrien (c != 'T') && (c != 't')); 2462236769Sobrien } else if (strncmp(*argv, "newline=", 8) == 0) { 2463236769Sobrien newShell.newline = &argv[0][8]; 2464236769Sobrien } else if (strncmp(*argv, "check=", 6) == 0) { 2465236769Sobrien newShell.errCheck = &argv[0][6]; 2466236769Sobrien } else if (strncmp(*argv, "ignore=", 7) == 0) { 2467236769Sobrien newShell.ignErr = &argv[0][7]; 2468236769Sobrien } else if (strncmp(*argv, "errout=", 7) == 0) { 2469236769Sobrien newShell.errOut = &argv[0][7]; 2470236769Sobrien } else if (strncmp(*argv, "comment=", 8) == 0) { 2471236769Sobrien newShell.commentChar = argv[0][8]; 2472236769Sobrien } else { 2473236769Sobrien Parse_Error(PARSE_FATAL, "Unknown keyword \"%s\"", 2474236769Sobrien *argv); 2475236769Sobrien free(words); 2476236769Sobrien return(FAILURE); 2477236769Sobrien } 2478236769Sobrien fullSpec = TRUE; 2479236769Sobrien } 2480236769Sobrien } 2481236769Sobrien 2482236769Sobrien if (path == NULL) { 2483236769Sobrien /* 2484236769Sobrien * If no path was given, the user wants one of the pre-defined shells, 2485236769Sobrien * yes? So we find the one s/he wants with the help of JobMatchShell 2486236769Sobrien * and set things up the right way. shellPath will be set up by 2487238152Sobrien * Shell_Init. 2488236769Sobrien */ 2489236769Sobrien if (newShell.name == NULL) { 2490236769Sobrien Parse_Error(PARSE_FATAL, "Neither path nor name specified"); 2491236769Sobrien free(words); 2492236769Sobrien return(FAILURE); 2493236769Sobrien } else { 2494236769Sobrien if ((sh = JobMatchShell(newShell.name)) == NULL) { 2495236769Sobrien Parse_Error(PARSE_WARNING, "%s: No matching shell", 2496236769Sobrien newShell.name); 2497236769Sobrien free(words); 2498236769Sobrien return(FAILURE); 2499236769Sobrien } 2500236769Sobrien commandShell = sh; 2501236769Sobrien shellName = newShell.name; 2502238152Sobrien if (shellPath) { 2503238152Sobrien /* Shell_Init has already been called! Do it again. */ 2504238152Sobrien free(UNCONST(shellPath)); 2505238152Sobrien shellPath = NULL; 2506238152Sobrien Shell_Init(); 2507238152Sobrien } 2508236769Sobrien } 2509236769Sobrien } else { 2510236769Sobrien /* 2511236769Sobrien * The user provided a path. If s/he gave nothing else (fullSpec is 2512236769Sobrien * FALSE), try and find a matching shell in the ones we know of. 2513236769Sobrien * Else we just take the specification at its word and copy it 2514236769Sobrien * to a new location. In either case, we need to record the 2515236769Sobrien * path the user gave for the shell. 2516236769Sobrien */ 2517236769Sobrien shellPath = path; 2518236769Sobrien path = strrchr(path, '/'); 2519236769Sobrien if (path == NULL) { 2520236769Sobrien path = UNCONST(shellPath); 2521236769Sobrien } else { 2522236769Sobrien path += 1; 2523236769Sobrien } 2524236769Sobrien if (newShell.name != NULL) { 2525236769Sobrien shellName = newShell.name; 2526236769Sobrien } else { 2527236769Sobrien shellName = path; 2528236769Sobrien } 2529236769Sobrien if (!fullSpec) { 2530236769Sobrien if ((sh = JobMatchShell(shellName)) == NULL) { 2531236769Sobrien Parse_Error(PARSE_WARNING, "%s: No matching shell", 2532236769Sobrien shellName); 2533236769Sobrien free(words); 2534236769Sobrien return(FAILURE); 2535236769Sobrien } 2536236769Sobrien commandShell = sh; 2537236769Sobrien } else { 2538236769Sobrien commandShell = bmake_malloc(sizeof(Shell)); 2539236769Sobrien *commandShell = newShell; 2540236769Sobrien } 2541253883Ssjg /* this will take care of shellErrFlag */ 2542253883Ssjg Shell_Init(); 2543236769Sobrien } 2544236769Sobrien 2545236769Sobrien if (commandShell->echoOn && commandShell->echoOff) { 2546236769Sobrien commandShell->hasEchoCtl = TRUE; 2547236769Sobrien } 2548236769Sobrien 2549236769Sobrien if (!commandShell->hasErrCtl) { 2550236769Sobrien if (commandShell->errCheck == NULL) { 2551236769Sobrien commandShell->errCheck = ""; 2552236769Sobrien } 2553236769Sobrien if (commandShell->ignErr == NULL) { 2554236769Sobrien commandShell->ignErr = "%s\n"; 2555236769Sobrien } 2556236769Sobrien } 2557236769Sobrien 2558236769Sobrien /* 2559236769Sobrien * Do not free up the words themselves, since they might be in use by the 2560236769Sobrien * shell specification. 2561236769Sobrien */ 2562236769Sobrien free(words); 2563236769Sobrien return SUCCESS; 2564236769Sobrien} 2565236769Sobrien 2566236769Sobrien/*- 2567236769Sobrien *----------------------------------------------------------------------- 2568236769Sobrien * JobInterrupt -- 2569236769Sobrien * Handle the receipt of an interrupt. 2570236769Sobrien * 2571236769Sobrien * Input: 2572236769Sobrien * runINTERRUPT Non-zero if commands for the .INTERRUPT target 2573236769Sobrien * should be executed 2574236769Sobrien * signo signal received 2575236769Sobrien * 2576236769Sobrien * Results: 2577236769Sobrien * None 2578236769Sobrien * 2579236769Sobrien * Side Effects: 2580236769Sobrien * All children are killed. Another job will be started if the 2581236769Sobrien * .INTERRUPT target was given. 2582236769Sobrien *----------------------------------------------------------------------- 2583236769Sobrien */ 2584236769Sobrienstatic void 2585236769SobrienJobInterrupt(int runINTERRUPT, int signo) 2586236769Sobrien{ 2587236769Sobrien Job *job; /* job descriptor in that element */ 2588236769Sobrien GNode *interrupt; /* the node describing the .INTERRUPT target */ 2589236769Sobrien sigset_t mask; 2590236769Sobrien GNode *gn; 2591236769Sobrien 2592236769Sobrien aborting = ABORT_INTERRUPT; 2593236769Sobrien 2594236769Sobrien JobSigLock(&mask); 2595236769Sobrien 2596236769Sobrien for (job = job_table; job < job_table_end; job++) { 2597236769Sobrien if (job->job_state != JOB_ST_RUNNING) 2598236769Sobrien continue; 2599236769Sobrien 2600236769Sobrien gn = job->node; 2601236769Sobrien 2602236769Sobrien if ((gn->type & (OP_JOIN|OP_PHONY)) == 0 && !Targ_Precious(gn)) { 2603236769Sobrien char *file = (gn->path == NULL ? gn->name : gn->path); 2604236769Sobrien if (!noExecute && eunlink(file) != -1) { 2605236769Sobrien Error("*** %s removed", file); 2606236769Sobrien } 2607236769Sobrien } 2608236769Sobrien if (job->pid) { 2609236769Sobrien if (DEBUG(JOB)) { 2610236769Sobrien (void)fprintf(debug_file, 2611236769Sobrien "JobInterrupt passing signal %d to child %d.\n", 2612236769Sobrien signo, job->pid); 2613236769Sobrien } 2614236769Sobrien KILLPG(job->pid, signo); 2615236769Sobrien } 2616236769Sobrien } 2617236769Sobrien 2618236769Sobrien JobSigUnlock(&mask); 2619236769Sobrien 2620236769Sobrien if (runINTERRUPT && !touchFlag) { 2621236769Sobrien interrupt = Targ_FindNode(".INTERRUPT", TARG_NOCREATE); 2622236769Sobrien if (interrupt != NULL) { 2623236769Sobrien ignoreErrors = FALSE; 2624236769Sobrien JobRun(interrupt); 2625236769Sobrien } 2626236769Sobrien } 2627236769Sobrien Trace_Log(MAKEINTR, 0); 2628236769Sobrien exit(signo); 2629236769Sobrien} 2630236769Sobrien 2631236769Sobrien/* 2632236769Sobrien *----------------------------------------------------------------------- 2633236769Sobrien * Job_Finish -- 2634236769Sobrien * Do final processing such as the running of the commands 2635236769Sobrien * attached to the .END target. 2636236769Sobrien * 2637236769Sobrien * Results: 2638236769Sobrien * Number of errors reported. 2639236769Sobrien * 2640236769Sobrien * Side Effects: 2641236769Sobrien * None. 2642236769Sobrien *----------------------------------------------------------------------- 2643236769Sobrien */ 2644236769Sobrienint 2645236769SobrienJob_Finish(void) 2646236769Sobrien{ 2647236769Sobrien if (postCommands != NULL && 2648236769Sobrien (!Lst_IsEmpty(postCommands->commands) || 2649236769Sobrien !Lst_IsEmpty(postCommands->children))) { 2650236769Sobrien if (errors) { 2651236769Sobrien Error("Errors reported so .END ignored"); 2652236769Sobrien } else { 2653236769Sobrien JobRun(postCommands); 2654236769Sobrien } 2655236769Sobrien } 2656236769Sobrien return(errors); 2657236769Sobrien} 2658236769Sobrien 2659236769Sobrien/*- 2660236769Sobrien *----------------------------------------------------------------------- 2661236769Sobrien * Job_End -- 2662236769Sobrien * Cleanup any memory used by the jobs module 2663236769Sobrien * 2664236769Sobrien * Results: 2665236769Sobrien * None. 2666236769Sobrien * 2667236769Sobrien * Side Effects: 2668236769Sobrien * Memory is freed 2669236769Sobrien *----------------------------------------------------------------------- 2670236769Sobrien */ 2671236769Sobrienvoid 2672236769SobrienJob_End(void) 2673236769Sobrien{ 2674236769Sobrien#ifdef CLEANUP 2675236769Sobrien if (shellArgv) 2676236769Sobrien free(shellArgv); 2677236769Sobrien#endif 2678236769Sobrien} 2679236769Sobrien 2680236769Sobrien/*- 2681236769Sobrien *----------------------------------------------------------------------- 2682236769Sobrien * Job_Wait -- 2683236769Sobrien * Waits for all running jobs to finish and returns. Sets 'aborting' 2684236769Sobrien * to ABORT_WAIT to prevent other jobs from starting. 2685236769Sobrien * 2686236769Sobrien * Results: 2687236769Sobrien * None. 2688236769Sobrien * 2689236769Sobrien * Side Effects: 2690236769Sobrien * Currently running jobs finish. 2691236769Sobrien * 2692236769Sobrien *----------------------------------------------------------------------- 2693236769Sobrien */ 2694236769Sobrienvoid 2695236769SobrienJob_Wait(void) 2696236769Sobrien{ 2697236769Sobrien aborting = ABORT_WAIT; 2698236769Sobrien while (jobTokensRunning != 0) { 2699236769Sobrien Job_CatchOutput(); 2700236769Sobrien } 2701236769Sobrien aborting = 0; 2702236769Sobrien} 2703236769Sobrien 2704236769Sobrien/*- 2705236769Sobrien *----------------------------------------------------------------------- 2706236769Sobrien * Job_AbortAll -- 2707236769Sobrien * Abort all currently running jobs without handling output or anything. 2708236769Sobrien * This function is to be called only in the event of a major 2709236769Sobrien * error. Most definitely NOT to be called from JobInterrupt. 2710236769Sobrien * 2711236769Sobrien * Results: 2712236769Sobrien * None 2713236769Sobrien * 2714236769Sobrien * Side Effects: 2715236769Sobrien * All children are killed, not just the firstborn 2716236769Sobrien *----------------------------------------------------------------------- 2717236769Sobrien */ 2718236769Sobrienvoid 2719236769SobrienJob_AbortAll(void) 2720236769Sobrien{ 2721236769Sobrien Job *job; /* the job descriptor in that element */ 2722236769Sobrien WAIT_T foo; 2723236769Sobrien 2724236769Sobrien aborting = ABORT_ERROR; 2725236769Sobrien 2726236769Sobrien if (jobTokensRunning) { 2727236769Sobrien for (job = job_table; job < job_table_end; job++) { 2728236769Sobrien if (job->job_state != JOB_ST_RUNNING) 2729236769Sobrien continue; 2730236769Sobrien /* 2731236769Sobrien * kill the child process with increasingly drastic signals to make 2732236769Sobrien * darn sure it's dead. 2733236769Sobrien */ 2734236769Sobrien KILLPG(job->pid, SIGINT); 2735236769Sobrien KILLPG(job->pid, SIGKILL); 2736236769Sobrien } 2737236769Sobrien } 2738236769Sobrien 2739236769Sobrien /* 2740236769Sobrien * Catch as many children as want to report in at first, then give up 2741236769Sobrien */ 2742236769Sobrien while (waitpid((pid_t) -1, &foo, WNOHANG) > 0) 2743236769Sobrien continue; 2744236769Sobrien} 2745236769Sobrien 2746236769Sobrien 2747236769Sobrien/*- 2748236769Sobrien *----------------------------------------------------------------------- 2749236769Sobrien * JobRestartJobs -- 2750236769Sobrien * Tries to restart stopped jobs if there are slots available. 2751236769Sobrien * Called in process context in response to a SIGCONT. 2752236769Sobrien * 2753236769Sobrien * Results: 2754236769Sobrien * None. 2755236769Sobrien * 2756236769Sobrien * Side Effects: 2757236769Sobrien * Resumes jobs. 2758236769Sobrien * 2759236769Sobrien *----------------------------------------------------------------------- 2760236769Sobrien */ 2761236769Sobrienstatic void 2762236769SobrienJobRestartJobs(void) 2763236769Sobrien{ 2764236769Sobrien Job *job; 2765236769Sobrien 2766236769Sobrien for (job = job_table; job < job_table_end; job++) { 2767236769Sobrien if (job->job_state == JOB_ST_RUNNING && 2768236769Sobrien (make_suspended || job->job_suspended)) { 2769236769Sobrien if (DEBUG(JOB)) { 2770236769Sobrien (void)fprintf(debug_file, "Restarting stopped job pid %d.\n", 2771236769Sobrien job->pid); 2772236769Sobrien } 2773236769Sobrien if (job->job_suspended) { 2774236769Sobrien (void)printf("*** [%s] Continued\n", job->node->name); 2775236769Sobrien (void)fflush(stdout); 2776236769Sobrien } 2777236769Sobrien job->job_suspended = 0; 2778236769Sobrien if (KILLPG(job->pid, SIGCONT) != 0 && DEBUG(JOB)) { 2779236769Sobrien fprintf(debug_file, "Failed to send SIGCONT to %d\n", job->pid); 2780236769Sobrien } 2781236769Sobrien } 2782236769Sobrien if (job->job_state == JOB_ST_FINISHED) 2783236769Sobrien /* Job exit deferred after calling waitpid() in a signal handler */ 2784236769Sobrien JobFinish(job, job->exit_status); 2785236769Sobrien } 2786236769Sobrien make_suspended = 0; 2787236769Sobrien} 2788236769Sobrien 2789236769Sobrienstatic void 2790236769Sobrienwatchfd(Job *job) 2791236769Sobrien{ 2792236769Sobrien if (job->inPollfd != NULL) 2793236769Sobrien Punt("Watching watched job"); 2794236769Sobrien 2795236769Sobrien fds[nfds].fd = job->inPipe; 2796236769Sobrien fds[nfds].events = POLLIN; 2797236769Sobrien jobfds[nfds] = job; 2798236769Sobrien job->inPollfd = &fds[nfds]; 2799236769Sobrien nfds++; 2800236769Sobrien} 2801236769Sobrien 2802236769Sobrienstatic void 2803236769Sobrienclearfd(Job *job) 2804236769Sobrien{ 2805236769Sobrien int i; 2806236769Sobrien if (job->inPollfd == NULL) 2807236769Sobrien Punt("Unwatching unwatched job"); 2808236769Sobrien i = job->inPollfd - fds; 2809236769Sobrien nfds--; 2810236769Sobrien /* 2811236769Sobrien * Move last job in table into hole made by dead job. 2812236769Sobrien */ 2813236769Sobrien if (nfds != i) { 2814236769Sobrien fds[i] = fds[nfds]; 2815236769Sobrien jobfds[i] = jobfds[nfds]; 2816236769Sobrien jobfds[i]->inPollfd = &fds[i]; 2817236769Sobrien } 2818236769Sobrien job->inPollfd = NULL; 2819236769Sobrien} 2820236769Sobrien 2821236769Sobrienstatic int 2822236769Sobrienreadyfd(Job *job) 2823236769Sobrien{ 2824236769Sobrien if (job->inPollfd == NULL) 2825236769Sobrien Punt("Polling unwatched job"); 2826236769Sobrien return (job->inPollfd->revents & POLLIN) != 0; 2827236769Sobrien} 2828236769Sobrien 2829236769Sobrien/*- 2830236769Sobrien *----------------------------------------------------------------------- 2831236769Sobrien * JobTokenAdd -- 2832236769Sobrien * Put a token into the job pipe so that some make process can start 2833236769Sobrien * another job. 2834236769Sobrien * 2835236769Sobrien * Side Effects: 2836236769Sobrien * Allows more build jobs to be spawned somewhere. 2837236769Sobrien * 2838236769Sobrien *----------------------------------------------------------------------- 2839236769Sobrien */ 2840236769Sobrien 2841236769Sobrienstatic void 2842236769SobrienJobTokenAdd(void) 2843236769Sobrien{ 2844236769Sobrien char tok = JOB_TOKENS[aborting], tok1; 2845236769Sobrien 2846255285Ssjg if (!Job_error_token && aborting == ABORT_ERROR) { 2847255285Ssjg if (jobTokensRunning == 0) 2848255285Ssjg return; 2849255285Ssjg tok = '+'; /* no error token */ 2850255285Ssjg } 2851255285Ssjg 2852236769Sobrien /* If we are depositing an error token flush everything else */ 2853236769Sobrien while (tok != '+' && read(tokenWaitJob.inPipe, &tok1, 1) == 1) 2854236769Sobrien continue; 2855236769Sobrien 2856236769Sobrien if (DEBUG(JOB)) 2857236769Sobrien fprintf(debug_file, "(%d) aborting %d, deposit token %c\n", 2858255285Ssjg getpid(), aborting, tok); 2859246223Ssjg while (write(tokenWaitJob.outPipe, &tok, 1) == -1 && errno == EAGAIN) 2860246223Ssjg continue; 2861236769Sobrien} 2862236769Sobrien 2863236769Sobrien/*- 2864236769Sobrien *----------------------------------------------------------------------- 2865236769Sobrien * Job_ServerStartTokenAdd -- 2866236769Sobrien * Prep the job token pipe in the root make process. 2867236769Sobrien * 2868236769Sobrien *----------------------------------------------------------------------- 2869236769Sobrien */ 2870236769Sobrien 2871236769Sobrienvoid 2872236769SobrienJob_ServerStart(int max_tokens, int jp_0, int jp_1) 2873236769Sobrien{ 2874236769Sobrien int i; 2875236769Sobrien char jobarg[64]; 2876236769Sobrien 2877236769Sobrien if (jp_0 >= 0 && jp_1 >= 0) { 2878236769Sobrien /* Pipe passed in from parent */ 2879236769Sobrien tokenWaitJob.inPipe = jp_0; 2880236769Sobrien tokenWaitJob.outPipe = jp_1; 2881251422Ssjg (void)fcntl(jp_0, F_SETFD, 1); 2882251422Ssjg (void)fcntl(jp_1, F_SETFD, 1); 2883236769Sobrien return; 2884236769Sobrien } 2885236769Sobrien 2886236769Sobrien JobCreatePipe(&tokenWaitJob, 15); 2887236769Sobrien 2888236769Sobrien snprintf(jobarg, sizeof(jobarg), "%d,%d", 2889236769Sobrien tokenWaitJob.inPipe, tokenWaitJob.outPipe); 2890236769Sobrien 2891236769Sobrien Var_Append(MAKEFLAGS, "-J", VAR_GLOBAL); 2892236769Sobrien Var_Append(MAKEFLAGS, jobarg, VAR_GLOBAL); 2893236769Sobrien 2894236769Sobrien /* 2895236769Sobrien * Preload the job pipe with one token per job, save the one 2896236769Sobrien * "extra" token for the primary job. 2897236769Sobrien * 2898236769Sobrien * XXX should clip maxJobs against PIPE_BUF -- if max_tokens is 2899236769Sobrien * larger than the write buffer size of the pipe, we will 2900236769Sobrien * deadlock here. 2901236769Sobrien */ 2902236769Sobrien for (i = 1; i < max_tokens; i++) 2903236769Sobrien JobTokenAdd(); 2904236769Sobrien} 2905236769Sobrien 2906236769Sobrien/*- 2907236769Sobrien *----------------------------------------------------------------------- 2908236769Sobrien * Job_TokenReturn -- 2909236769Sobrien * Return a withdrawn token to the pool. 2910236769Sobrien * 2911236769Sobrien *----------------------------------------------------------------------- 2912236769Sobrien */ 2913236769Sobrien 2914236769Sobrienvoid 2915236769SobrienJob_TokenReturn(void) 2916236769Sobrien{ 2917236769Sobrien jobTokensRunning--; 2918236769Sobrien if (jobTokensRunning < 0) 2919236769Sobrien Punt("token botch"); 2920236769Sobrien if (jobTokensRunning || JOB_TOKENS[aborting] != '+') 2921236769Sobrien JobTokenAdd(); 2922236769Sobrien} 2923236769Sobrien 2924236769Sobrien/*- 2925236769Sobrien *----------------------------------------------------------------------- 2926236769Sobrien * Job_TokenWithdraw -- 2927236769Sobrien * Attempt to withdraw a token from the pool. 2928236769Sobrien * 2929236769Sobrien * Results: 2930236769Sobrien * Returns TRUE if a token was withdrawn, and FALSE if the pool 2931236769Sobrien * is currently empty. 2932236769Sobrien * 2933236769Sobrien * Side Effects: 2934236769Sobrien * If pool is empty, set wantToken so that we wake up 2935236769Sobrien * when a token is released. 2936236769Sobrien * 2937236769Sobrien *----------------------------------------------------------------------- 2938236769Sobrien */ 2939236769Sobrien 2940236769Sobrien 2941236769SobrienBoolean 2942236769SobrienJob_TokenWithdraw(void) 2943236769Sobrien{ 2944236769Sobrien char tok, tok1; 2945236769Sobrien int count; 2946236769Sobrien 2947236769Sobrien wantToken = 0; 2948236769Sobrien if (DEBUG(JOB)) 2949236769Sobrien fprintf(debug_file, "Job_TokenWithdraw(%d): aborting %d, running %d\n", 2950236769Sobrien getpid(), aborting, jobTokensRunning); 2951236769Sobrien 2952236769Sobrien if (aborting || (jobTokensRunning >= maxJobs)) 2953236769Sobrien return FALSE; 2954236769Sobrien 2955236769Sobrien count = read(tokenWaitJob.inPipe, &tok, 1); 2956236769Sobrien if (count == 0) 2957236769Sobrien Fatal("eof on job pipe!"); 2958236769Sobrien if (count < 0 && jobTokensRunning != 0) { 2959236769Sobrien if (errno != EAGAIN) { 2960236769Sobrien Fatal("job pipe read: %s", strerror(errno)); 2961236769Sobrien } 2962236769Sobrien if (DEBUG(JOB)) 2963236769Sobrien fprintf(debug_file, "(%d) blocked for token\n", getpid()); 2964236769Sobrien wantToken = 1; 2965236769Sobrien return FALSE; 2966236769Sobrien } 2967236769Sobrien 2968236769Sobrien if (count == 1 && tok != '+') { 2969236769Sobrien /* make being abvorted - remove any other job tokens */ 2970236769Sobrien if (DEBUG(JOB)) 2971236769Sobrien fprintf(debug_file, "(%d) aborted by token %c\n", getpid(), tok); 2972236769Sobrien while (read(tokenWaitJob.inPipe, &tok1, 1) == 1) 2973236769Sobrien continue; 2974236769Sobrien /* And put the stopper back */ 2975246223Ssjg while (write(tokenWaitJob.outPipe, &tok, 1) == -1 && errno == EAGAIN) 2976246223Ssjg continue; 2977236769Sobrien Fatal("A failure has been detected in another branch of the parallel make"); 2978236769Sobrien } 2979236769Sobrien 2980236769Sobrien if (count == 1 && jobTokensRunning == 0) 2981236769Sobrien /* We didn't want the token really */ 2982246223Ssjg while (write(tokenWaitJob.outPipe, &tok, 1) == -1 && errno == EAGAIN) 2983246223Ssjg continue; 2984236769Sobrien 2985236769Sobrien jobTokensRunning++; 2986236769Sobrien if (DEBUG(JOB)) 2987236769Sobrien fprintf(debug_file, "(%d) withdrew token\n", getpid()); 2988236769Sobrien return TRUE; 2989236769Sobrien} 2990236769Sobrien 2991249033Ssjg/*- 2992249033Ssjg *----------------------------------------------------------------------- 2993249033Ssjg * Job_RunTarget -- 2994249033Ssjg * Run the named target if found. If a filename is specified, then 2995249033Ssjg * set that to the sources. 2996249033Ssjg * 2997249033Ssjg * Results: 2998249033Ssjg * None 2999249033Ssjg * 3000249033Ssjg * Side Effects: 3001249033Ssjg * exits if the target fails. 3002249033Ssjg * 3003249033Ssjg *----------------------------------------------------------------------- 3004249033Ssjg */ 3005249033SsjgBoolean 3006249033SsjgJob_RunTarget(const char *target, const char *fname) { 3007249033Ssjg GNode *gn = Targ_FindNode(target, TARG_NOCREATE); 3008249033Ssjg 3009249033Ssjg if (gn == NULL) 3010249033Ssjg return FALSE; 3011249033Ssjg 3012249033Ssjg if (fname) 3013249033Ssjg Var_Set(ALLSRC, fname, gn, 0); 3014249033Ssjg 3015249033Ssjg JobRun(gn); 3016249033Ssjg if (gn->made == ERROR) { 3017249033Ssjg PrintOnError(gn, "\n\nStop."); 3018249033Ssjg exit(1); 3019249033Ssjg } 3020249033Ssjg return TRUE; 3021249033Ssjg} 3022249033Ssjg 3023236769Sobrien#ifdef USE_SELECT 3024236769Sobrienint 3025236769Sobrienemul_poll(struct pollfd *fd, int nfd, int timeout) 3026236769Sobrien{ 3027236769Sobrien fd_set rfds, wfds; 3028236769Sobrien int i, maxfd, nselect, npoll; 3029236769Sobrien struct timeval tv, *tvp; 3030236769Sobrien long usecs; 3031236769Sobrien 3032236769Sobrien FD_ZERO(&rfds); 3033236769Sobrien FD_ZERO(&wfds); 3034236769Sobrien 3035236769Sobrien maxfd = -1; 3036236769Sobrien for (i = 0; i < nfd; i++) { 3037236769Sobrien fd[i].revents = 0; 3038236769Sobrien 3039236769Sobrien if (fd[i].events & POLLIN) 3040236769Sobrien FD_SET(fd[i].fd, &rfds); 3041236769Sobrien 3042236769Sobrien if (fd[i].events & POLLOUT) 3043236769Sobrien FD_SET(fd[i].fd, &wfds); 3044236769Sobrien 3045236769Sobrien if (fd[i].fd > maxfd) 3046236769Sobrien maxfd = fd[i].fd; 3047236769Sobrien } 3048236769Sobrien 3049236769Sobrien if (maxfd >= FD_SETSIZE) { 3050236769Sobrien Punt("Ran out of fd_set slots; " 3051236769Sobrien "recompile with a larger FD_SETSIZE."); 3052236769Sobrien } 3053236769Sobrien 3054236769Sobrien if (timeout < 0) { 3055236769Sobrien tvp = NULL; 3056236769Sobrien } else { 3057236769Sobrien usecs = timeout * 1000; 3058236769Sobrien tv.tv_sec = usecs / 1000000; 3059236769Sobrien tv.tv_usec = usecs % 1000000; 3060236769Sobrien tvp = &tv; 3061236769Sobrien } 3062236769Sobrien 3063236769Sobrien nselect = select(maxfd + 1, &rfds, &wfds, 0, tvp); 3064236769Sobrien 3065236769Sobrien if (nselect <= 0) 3066236769Sobrien return nselect; 3067236769Sobrien 3068236769Sobrien npoll = 0; 3069236769Sobrien for (i = 0; i < nfd; i++) { 3070236769Sobrien if (FD_ISSET(fd[i].fd, &rfds)) 3071236769Sobrien fd[i].revents |= POLLIN; 3072236769Sobrien 3073236769Sobrien if (FD_ISSET(fd[i].fd, &wfds)) 3074236769Sobrien fd[i].revents |= POLLOUT; 3075236769Sobrien 3076236769Sobrien if (fd[i].revents) 3077236769Sobrien npoll++; 3078236769Sobrien } 3079236769Sobrien 3080236769Sobrien return npoll; 3081236769Sobrien} 3082236769Sobrien#endif /* USE_SELECT */ 3083