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