#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include "wait.h"
#include <errno.h>
#if !defined(USE_SELECT) && defined(HAVE_POLL_H)
#include <poll.h>
#else
#ifndef USE_SELECT
# define USE_SELECT
#endif
#if defined(HAVE_SYS_SELECT_H)
# include <sys/select.h>
#endif
#endif
#include <signal.h>
#include <utime.h>
#if defined(HAVE_SYS_SOCKET_H)
# include <sys/socket.h>
#endif
#include "make.h"
#include "dir.h"
#include "job.h"
#ifdef USE_META
# include "meta.h"
#endif
#include "pathnames.h"
#include "trace.h"
MAKE_RCSID("$NetBSD: job.c,v 1.519 2025/08/04 15:40:39 sjg Exp $");
#ifdef USE_SELECT
#define poll emul_poll
#define pollfd emul_pollfd
struct emul_pollfd {
int fd;
short events;
short revents;
};
#define POLLIN 0x0001
#define POLLOUT 0x0004
int emul_poll(struct pollfd *, int, int);
#endif
struct pollfd;
enum JobStatus {
JOB_ST_FREE,
JOB_ST_SET_UP,
JOB_ST_RUNNING,
JOB_ST_FINISHED
};
static const char JobStatus_Name[][9] = {
"free",
"set-up",
"running",
"finished",
};
struct Job {
int pid;
GNode *node;
StringListNode *tailCmds;
FILE *cmdFILE;
int exit_status;
enum JobStatus status;
bool suspended;
bool ignerr;
bool echo;
bool special;
int inPipe;
int outPipe;
struct pollfd *inPollfd;
#define JOB_BUFSIZE 1024
char outBuf[JOB_BUFSIZE + 1];
size_t outBufLen;
#ifdef USE_META
struct BuildMon bm;
#endif
};
typedef struct Shell {
const char *name;
bool hasEchoCtl;
const char *echoOff;
const char *echoOn;
const char *noPrint;
size_t noPrintLen;
bool hasErrCtl;
const char *errOn;
const char *errOff;
const char *echoTmpl;
const char *runIgnTmpl;
const char *runChkTmpl;
const char *newline;
char commentChar;
const char *echoFlag;
const char *errFlag;
} Shell;
typedef struct CommandFlags {
bool echo;
bool always;
bool ignerr;
} CommandFlags;
typedef struct ShellWriter {
FILE *f;
bool xtraced;
} ShellWriter;
#define MAKE_ALWAYS_PASS_JOB_QUEUE "${.MAKE.ALWAYS_PASS_JOB_QUEUE:U}"
static bool Always_pass_job_queue = true;
#define MAKE_JOB_ERROR_TOKEN "${MAKE_JOB_ERROR_TOKEN:U}"
static bool Job_error_token = true;
static int job_errors = 0;
static enum {
ABORT_NONE,
ABORT_ERROR,
ABORT_INTERRUPT,
ABORT_WAIT
} aborting = ABORT_NONE;
#define JOB_TOKENS "+EI+"
static const char aborting_name[][6] = { "NONE", "ERROR", "INTR", "WAIT" };
int jobTokensRunning = 0;
#ifdef DEFSHELL_CUSTOM
#define DEFSHELL_INDEX_CUSTOM 0
#define DEFSHELL_INDEX_SH 1
#define DEFSHELL_INDEX_KSH 2
#define DEFSHELL_INDEX_CSH 3
#else
#define DEFSHELL_INDEX_SH 0
#define DEFSHELL_INDEX_KSH 1
#define DEFSHELL_INDEX_CSH 2
#endif
#ifndef DEFSHELL_INDEX
#define DEFSHELL_INDEX 0
#endif
static Shell shells[] = {
#ifdef DEFSHELL_CUSTOM
{
DEFSHELL_CUSTOM,
false,
"",
"",
"",
0,
false,
"",
"",
"echo \"%s\"\n",
"%s\n",
"{ %s \n} || exit $?\n",
"'\n'",
'#',
"",
"",
},
#endif
{
"sh",
false,
"",
"",
"",
0,
false,
"",
"",
"echo \"%s\"\n",
"%s\n",
"{ %s \n} || exit $?\n",
"'\n'",
'#',
#if defined(MAKE_NATIVE) && defined(__NetBSD__)
"q",
#else
"",
#endif
"",
},
{
"ksh",
true,
"set +v",
"set -v",
"set +v",
6,
false,
"",
"",
"echo \"%s\"\n",
"%s\n",
"{ %s \n} || exit $?\n",
"'\n'",
'#',
"v",
"",
},
{
"csh",
true,
"unset verbose",
"set verbose",
"unset verbose",
13,
false,
"",
"",
"echo \"%s\"\n",
"csh -c \"%s || exit 0\"\n",
"",
"'\\\n'",
'#',
"v",
"e",
}
};
static Shell *shell = &shells[DEFSHELL_INDEX];
char *shellPath;
const char *shellName = NULL;
char *shellErrFlag = NULL;
static char *shell_freeIt = NULL;
static Job *job_table;
static Job *job_table_end;
static bool wantToken;
static bool lurking_children = false;
static bool make_suspended = false;
static struct pollfd *fds = NULL;
static Job **jobByFdIndex = NULL;
static nfds_t fdsLen = 0;
static void watchfd(Job *);
static void clearfd(Job *);
static char *targPrefix = NULL;
static Job tokenPoolJob;
static Job childExitJob;
#define CEJ_CHILD_EXITED '.'
#define CEJ_RESUME_JOBS 'R'
enum {
npseudojobs = 2
};
static sigset_t caught_signals;
static volatile sig_atomic_t caught_sigchld;
static void CollectOutput(Job *, bool);
static void JobInterrupt(bool, int) MAKE_ATTR_DEAD;
static void JobSigReset(void);
static void
SwitchOutputTo(GNode *gn)
{
static GNode *lastNode = NULL;
if (gn == lastNode)
return;
lastNode = gn;
if (opts.maxJobs != 1 && targPrefix != NULL && targPrefix[0] != '\0')
(void)fprintf(stdout, "%s %s ---\n", targPrefix, gn->name);
}
static unsigned
nfds_per_job(void)
{
#if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
if (useMeta)
return 2;
#endif
return 1;
}
void
Job_FlagsToString(const Job *job, char *buf, size_t bufsize)
{
snprintf(buf, bufsize, "%c%c%c",
job->ignerr ? 'i' : '-',
!job->echo ? 's' : '-',
job->special ? 'S' : '-');
}
#ifdef USE_META
struct BuildMon *
Job_BuildMon(Job *job)
{
return &job->bm;
}
#endif
GNode *
Job_Node(Job *job)
{
return job->node;
}
int
Job_Pid(Job *job)
{
return job->pid;
}
static void
JobTable_Dump(const char *where)
{
const Job *job;
char flags[4];
debug_printf("%s, job table:\n", where);
for (job = job_table; job < job_table_end; job++) {
Job_FlagsToString(job, flags, sizeof flags);
debug_printf("job %d, status %s, flags %s, pid %d\n",
(int)(job - job_table), JobStatus_Name[job->status],
flags, job->pid);
}
}
static void
JobDeleteTarget(GNode *gn)
{
const char *file;
if (gn->type & OP_JOIN)
return;
if (gn->type & OP_PHONY)
return;
if (GNode_IsPrecious(gn))
return;
if (opts.noExecute)
return;
file = GNode_Path(gn);
if (unlink_file(file) == 0)
Error("*** %s removed", file);
}
static void
JobsTable_Lock(sigset_t *omaskp)
{
if (sigprocmask(SIG_BLOCK, &caught_signals, omaskp) != 0)
Punt("JobsTable_Lock: %s", strerror(errno));
}
static void
JobsTable_Unlock(sigset_t *omaskp)
{
(void)sigprocmask(SIG_SETMASK, omaskp, NULL);
}
static void
SetNonblocking(int fd)
{
int flags = fcntl(fd, F_GETFL, 0);
if (flags == -1)
Punt("SetNonblocking.get: %s", strerror(errno));
flags |= O_NONBLOCK;
if (fcntl(fd, F_SETFL, flags) == -1)
Punt("SetNonblocking.set: %s", strerror(errno));
}
static void
SetCloseOnExec(int fd)
{
if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
Punt("SetCloseOnExec: %s", strerror(errno));
}
static void
JobCreatePipe(Job *job, int minfd)
{
int i;
int pipe_fds[2];
if (pipe(pipe_fds) == -1)
Punt("JobCreatePipe: %s", strerror(errno));
for (i = 0; i < 2; i++) {
int fd = fcntl(pipe_fds[i], F_DUPFD, minfd);
if (fd != -1) {
close(pipe_fds[i]);
pipe_fds[i] = fd;
}
}
job->inPipe = pipe_fds[0];
job->outPipe = pipe_fds[1];
SetCloseOnExec(job->inPipe);
SetCloseOnExec(job->outPipe);
SetNonblocking(job->inPipe);
}
static void
JobCondPassSig(int signo)
{
Job *job;
DEBUG1(JOB, "JobCondPassSig: signal %d\n", signo);
for (job = job_table; job < job_table_end; job++) {
if (job->status != JOB_ST_RUNNING)
continue;
DEBUG2(JOB, "JobCondPassSig passing signal %d to pid %d\n",
signo, job->pid);
KILLPG(job->pid, signo);
}
}
static void
WriteOrDie(int fd, char ch)
{
if (write(fd, &ch, 1) != 1)
execDie("write", "child");
}
static void
HandleSIGCHLD(int signo MAKE_ATTR_UNUSED)
{
caught_sigchld = 1;
WriteOrDie(childExitJob.outPipe, CEJ_CHILD_EXITED);
}
static void
HandleSIGCONT(int signo MAKE_ATTR_UNUSED)
{
WriteOrDie(childExitJob.outPipe, CEJ_RESUME_JOBS);
}
MAKE_ATTR_DEAD static void
JobPassSig_int(int signo)
{
JobInterrupt(true, signo);
}
MAKE_ATTR_DEAD static void
JobPassSig_term(int signo)
{
JobInterrupt(false, signo);
}
static void
JobPassSig_suspend(int signo)
{
sigset_t nmask, omask;
struct sigaction act;
make_suspended = true;
JobCondPassSig(signo);
sigfillset(&nmask);
sigdelset(&nmask, signo);
(void)sigprocmask(SIG_SETMASK, &nmask, &omask);
act.sa_handler = SIG_DFL;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
(void)sigaction(signo, &act, NULL);
DEBUG1(JOB, "JobPassSig_suspend passing signal %d to self\n", signo);
(void)kill(getpid(), signo);
act.sa_handler = JobPassSig_suspend;
(void)sigaction(signo, &act, NULL);
(void)sigprocmask(SIG_SETMASK, &omask, NULL);
}
static Job *
JobFindPid(int pid, enum JobStatus status, bool isJobs)
{
Job *job;
for (job = job_table; job < job_table_end; job++) {
if (job->status == status && job->pid == pid)
return job;
}
if (DEBUG(JOB) && isJobs)
JobTable_Dump("no pid");
return NULL;
}
static void
ParseCommandFlags(char **pp, CommandFlags *out_cmdFlags)
{
char *p = *pp;
out_cmdFlags->echo = true;
out_cmdFlags->ignerr = false;
out_cmdFlags->always = false;
for (;;) {
if (*p == '@')
out_cmdFlags->echo = DEBUG(LOUD);
else if (*p == '-')
out_cmdFlags->ignerr = true;
else if (*p == '+')
out_cmdFlags->always = true;
else if (!ch_isspace(*p))
break;
p++;
}
*pp = p;
}
static char *
EscapeShellDblQuot(const char *cmd)
{
size_t i, j;
char *esc = bmake_malloc(strlen(cmd) * 2 + 1);
for (i = 0, j = 0; cmd[i] != '\0'; i++, j++) {
if (cmd[i] == '$' || cmd[i] == '`' || cmd[i] == '\\' ||
cmd[i] == '"')
esc[j++] = '\\';
esc[j] = cmd[i];
}
esc[j] = '\0';
return esc;
}
static void
ShellWriter_WriteFmt(ShellWriter *wr, const char *fmt, const char *arg)
{
DEBUG1(JOB, fmt, arg);
(void)fprintf(wr->f, fmt, arg);
if (wr->f == stdout)
(void)fflush(wr->f);
}
static void
ShellWriter_WriteLine(ShellWriter *wr, const char *line)
{
ShellWriter_WriteFmt(wr, "%s\n", line);
}
static void
ShellWriter_EchoOff(ShellWriter *wr)
{
if (shell->hasEchoCtl)
ShellWriter_WriteLine(wr, shell->echoOff);
}
static void
ShellWriter_EchoCmd(ShellWriter *wr, const char *escCmd)
{
ShellWriter_WriteFmt(wr, shell->echoTmpl, escCmd);
}
static void
ShellWriter_EchoOn(ShellWriter *wr)
{
if (shell->hasEchoCtl)
ShellWriter_WriteLine(wr, shell->echoOn);
}
static void
ShellWriter_TraceOn(ShellWriter *wr)
{
if (!wr->xtraced) {
ShellWriter_WriteLine(wr, "set -x");
wr->xtraced = true;
}
}
static void
ShellWriter_ErrOff(ShellWriter *wr, bool echo)
{
if (echo)
ShellWriter_EchoOff(wr);
ShellWriter_WriteLine(wr, shell->errOff);
if (echo)
ShellWriter_EchoOn(wr);
}
static void
ShellWriter_ErrOn(ShellWriter *wr, bool echo)
{
if (echo)
ShellWriter_EchoOff(wr);
ShellWriter_WriteLine(wr, shell->errOn);
if (echo)
ShellWriter_EchoOn(wr);
}
static void
JobWriteSpecialsEchoCtl(Job *job, ShellWriter *wr, CommandFlags *inout_cmdFlags,
const char *escCmd, const char **inout_cmdTemplate)
{
job->ignerr = true;
if (job->echo && inout_cmdFlags->echo) {
ShellWriter_EchoOff(wr);
ShellWriter_EchoCmd(wr, escCmd);
inout_cmdFlags->echo = false;
}
*inout_cmdTemplate = shell->runIgnTmpl;
inout_cmdFlags->ignerr = false;
}
static void
JobWriteSpecials(Job *job, ShellWriter *wr, const char *escCmd, bool run,
CommandFlags *inout_cmdFlags, const char **inout_cmdTemplate)
{
if (!run)
inout_cmdFlags->ignerr = false;
else if (shell->hasErrCtl)
ShellWriter_ErrOff(wr, job->echo && inout_cmdFlags->echo);
else if (shell->runIgnTmpl != NULL && shell->runIgnTmpl[0] != '\0') {
JobWriteSpecialsEchoCtl(job, wr, inout_cmdFlags, escCmd,
inout_cmdTemplate);
} else
inout_cmdFlags->ignerr = false;
}
static void
JobWriteCommand(Job *job, ShellWriter *wr, StringListNode *ln, const char *ucmd)
{
bool run;
CommandFlags cmdFlags;
const char *cmdTemplate;
char *xcmd;
char *xcmdStart;
char *escCmd;
run = GNode_ShouldExecute(job->node);
xcmd = Var_SubstInTarget(ucmd, job->node);
xcmdStart = xcmd;
cmdTemplate = "%s\n";
ParseCommandFlags(&xcmd, &cmdFlags);
if (cmdFlags.always && !run) {
(void)Compat_RunCommand(ucmd, job->node, ln);
free(xcmdStart);
return;
}
escCmd = shell->hasErrCtl ? NULL : EscapeShellDblQuot(xcmd);
if (!cmdFlags.echo) {
if (job->echo && run && shell->hasEchoCtl)
ShellWriter_EchoOff(wr);
else if (shell->hasErrCtl)
cmdFlags.echo = true;
}
if (cmdFlags.ignerr) {
JobWriteSpecials(job, wr, escCmd, run, &cmdFlags, &cmdTemplate);
} else {
if (!shell->hasErrCtl && shell->runChkTmpl != NULL &&
shell->runChkTmpl[0] != '\0') {
if (job->echo && cmdFlags.echo) {
ShellWriter_EchoOff(wr);
ShellWriter_EchoCmd(wr, escCmd);
cmdFlags.echo = false;
}
cmdTemplate = escCmd[0] == shell->commentChar ||
escCmd[0] == '\0'
? shell->runIgnTmpl
: shell->runChkTmpl;
cmdFlags.ignerr = false;
}
}
if (DEBUG(SHELL) && strcmp(shellName, "sh") == 0)
ShellWriter_TraceOn(wr);
ShellWriter_WriteFmt(wr, cmdTemplate, xcmd);
free(xcmdStart);
free(escCmd);
if (cmdFlags.ignerr)
ShellWriter_ErrOn(wr, cmdFlags.echo && job->echo);
if (!cmdFlags.echo)
ShellWriter_EchoOn(wr);
}
static bool
JobWriteCommands(Job *job)
{
StringListNode *ln;
bool seen = false;
ShellWriter wr;
wr.f = job->cmdFILE;
wr.xtraced = false;
for (ln = job->node->commands.first; ln != NULL; ln = ln->next) {
const char *cmd = ln->datum;
if (strcmp(cmd, "...") == 0) {
job->node->type |= OP_SAVE_CMDS;
job->tailCmds = ln->next;
break;
}
JobWriteCommand(job, &wr, ln, ln->datum);
seen = true;
}
return seen;
}
static void
JobSaveCommands(Job *job)
{
StringListNode *ln;
for (ln = job->tailCmds; ln != NULL; ln = ln->next) {
const char *cmd = ln->datum;
char *expanded_cmd;
expanded_cmd = Var_SubstInTarget(cmd, job->node);
Lst_Append(&Targ_GetEndNode()->commands, expanded_cmd);
Parse_RegisterCommand(expanded_cmd);
}
}
static void
JobClosePipes(Job *job)
{
clearfd(job);
(void)close(job->outPipe);
job->outPipe = -1;
CollectOutput(job, true);
(void)close(job->inPipe);
job->inPipe = -1;
}
static void
DebugFailedJob(const Job *job)
{
const StringListNode *ln;
if (!DEBUG(ERROR))
return;
debug_printf("\n");
debug_printf("*** Failed target: %s\n", job->node->name);
debug_printf("*** In directory: %s\n", curdir);
debug_printf("*** Failed commands:\n");
for (ln = job->node->commands.first; ln != NULL; ln = ln->next) {
const char *cmd = ln->datum;
debug_printf("\t%s\n", cmd);
if (strchr(cmd, '$') != NULL) {
char *xcmd = Var_Subst(cmd, job->node, VARE_EVAL);
debug_printf("\t=> %s\n", xcmd);
free(xcmd);
}
}
}
static void
JobFinishDoneExitedError(Job *job, WAIT_T *inout_status)
{
SwitchOutputTo(job->node);
#ifdef USE_META
if (useMeta) {
meta_job_error(job, job->node,
job->ignerr, WEXITSTATUS(*inout_status));
}
#endif
if (!shouldDieQuietly(job->node, -1)) {
DebugFailedJob(job);
(void)printf("*** [%s] Error code %d%s\n",
job->node->name, WEXITSTATUS(*inout_status),
job->ignerr ? " (ignored)" : "");
}
if (job->ignerr)
WAIT_STATUS(*inout_status) = 0;
else {
if (deleteOnError)
JobDeleteTarget(job->node);
PrintOnError(job->node, "\n");
}
}
static void
JobFinishDoneExited(Job *job, WAIT_T *inout_status)
{
DEBUG2(JOB, "Target %s, pid %d exited\n",
job->node->name, job->pid);
if (WEXITSTATUS(*inout_status) != 0)
JobFinishDoneExitedError(job, inout_status);
else if (DEBUG(JOB)) {
SwitchOutputTo(job->node);
(void)printf("Target %s, pid %d exited successfully\n",
job->node->name, job->pid);
}
}
static void
JobFinishDoneSignaled(Job *job, WAIT_T status)
{
SwitchOutputTo(job->node);
DebugFailedJob(job);
(void)printf("*** [%s] Signal %d\n", job->node->name, WTERMSIG(status));
if (deleteOnError)
JobDeleteTarget(job->node);
}
static void
JobFinishDone(Job *job, WAIT_T *inout_status)
{
if (WIFEXITED(*inout_status))
JobFinishDoneExited(job, inout_status);
else
JobFinishDoneSignaled(job, *inout_status);
(void)fflush(stdout);
}
static void
JobFinish (Job *job, WAIT_T status)
{
bool done, return_job_token;
DEBUG3(JOB, "JobFinish: target %s, pid %d, status %#x\n",
job->node->name, job->pid, status);
if ((WIFEXITED(status) &&
((WEXITSTATUS(status) != 0 && !job->ignerr))) ||
WIFSIGNALED(status)) {
JobClosePipes(job);
if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
if (fclose(job->cmdFILE) != 0)
Punt("Cannot write shell script for \"%s\": %s",
job->node->name, strerror(errno));
job->cmdFILE = NULL;
}
done = true;
} else if (WIFEXITED(status)) {
done = WEXITSTATUS(status) != 0;
JobClosePipes(job);
} else {
done = false;
}
if (done)
JobFinishDone(job, &status);
#ifdef USE_META
if (useMeta) {
int meta_status = meta_job_finish(job);
if (meta_status != 0 && status == 0)
status = meta_status;
}
#endif
return_job_token = false;
Trace_Log(JOBEND, job);
if (!job->special) {
if (WAIT_STATUS(status) != 0 ||
(aborting == ABORT_ERROR) || aborting == ABORT_INTERRUPT)
return_job_token = true;
}
if (aborting != ABORT_ERROR && aborting != ABORT_INTERRUPT &&
(WAIT_STATUS(status) == 0)) {
JobSaveCommands(job);
job->node->made = MADE;
if (!job->special)
return_job_token = true;
Make_Update(job->node);
job->status = JOB_ST_FREE;
} else if (status != 0) {
job_errors++;
job->status = JOB_ST_FREE;
}
if (job_errors > 0 && !opts.keepgoing && aborting != ABORT_INTERRUPT) {
aborting = ABORT_ERROR;
}
if (return_job_token)
TokenPool_Return();
if (aborting == ABORT_ERROR && jobTokensRunning == 0) {
if (shouldDieQuietly(NULL, -1))
exit(2);
Fatal("%d error%s", job_errors, job_errors == 1 ? "" : "s");
}
}
static void
TouchRegular(GNode *gn)
{
const char *file = GNode_Path(gn);
struct utimbuf times;
int fd;
char c;
times.actime = now;
times.modtime = now;
if (utime(file, ×) >= 0)
return;
fd = open(file, O_RDWR | O_CREAT, 0666);
if (fd < 0) {
(void)fprintf(stderr, "*** couldn't touch %s: %s\n",
file, strerror(errno));
(void)fflush(stderr);
return;
}
if (read(fd, &c, 1) == 1) {
(void)lseek(fd, 0, SEEK_SET);
while (write(fd, &c, 1) == -1 && errno == EAGAIN)
continue;
}
(void)close(fd);
}
void
Job_Touch(GNode *gn, bool echo)
{
if (gn->type &
(OP_JOIN | OP_USE | OP_USEBEFORE | OP_EXEC | OP_OPTIONAL |
OP_SPECIAL | OP_PHONY)) {
return;
}
if (echo || !GNode_ShouldExecute(gn)) {
(void)fprintf(stdout, "touch %s\n", gn->name);
(void)fflush(stdout);
}
if (!GNode_ShouldExecute(gn))
return;
if (gn->type & OP_ARCHV)
Arch_Touch(gn);
else if (gn->type & OP_LIB)
Arch_TouchLib(gn);
else
TouchRegular(gn);
}
bool
Job_CheckCommands(GNode *gn, void (*abortProc)(const char *, ...))
{
if (GNode_IsTarget(gn))
return true;
if (!Lst_IsEmpty(&gn->commands))
return true;
if ((gn->type & OP_LIB) && !Lst_IsEmpty(&gn->children))
return true;
if (defaultNode != NULL && !Lst_IsEmpty(&defaultNode->commands) &&
!(gn->type & OP_SPECIAL)) {
Make_HandleUse(defaultNode, gn);
Var_Set(gn, IMPSRC, GNode_VarTarget(gn));
return true;
}
Dir_UpdateMTime(gn, false);
if (gn->mtime != 0 || (gn->type & OP_SPECIAL))
return true;
if (gn->flags.fromDepend) {
if (!Job_RunTarget(".STALE", gn->fname))
fprintf(stdout,
"%s: %s:%u: ignoring stale %s for %s\n",
progname, gn->fname, gn->lineno, makeDependfile,
gn->name);
return true;
}
if (gn->type & OP_OPTIONAL) {
(void)fprintf(stdout, "%s: don't know how to make %s (%s)\n",
progname, gn->name, "ignored");
(void)fflush(stdout);
return true;
}
if (opts.keepgoing) {
(void)fprintf(stdout, "%s: don't know how to make %s (%s)\n",
progname, gn->name, "continuing");
(void)fflush(stdout);
return false;
}
abortProc("don't know how to make %s. Stop", gn->name);
return false;
}
static void
JobExec(Job *job, char **argv)
{
int cpid;
sigset_t mask;
if (DEBUG(JOB)) {
int i;
debug_printf("Running %s\n", job->node->name);
debug_printf("\tCommand:");
for (i = 0; argv[i] != NULL; i++) {
debug_printf(" %s", argv[i]);
}
debug_printf("\n");
}
if (job->echo)
SwitchOutputTo(job->node);
JobsTable_Lock(&mask);
job->status = JOB_ST_RUNNING;
Var_ReexportVars(job->node);
Var_ExportStackTrace(job->node->name, NULL);
cpid = FORK_FUNCTION();
if (cpid == -1)
Punt("fork: %s", strerror(errno));
if (cpid == 0) {
sigset_t tmask;
#ifdef USE_META
if (useMeta)
meta_job_child(job);
#endif
JobSigReset();
sigemptyset(&tmask);
JobsTable_Unlock(&tmask);
if (dup2(fileno(job->cmdFILE), STDIN_FILENO) == -1)
execDie("dup2", "job->cmdFILE");
if (fcntl(STDIN_FILENO, F_SETFD, 0) == -1)
execDie("clear close-on-exec", "stdin");
if (lseek(STDIN_FILENO, 0, SEEK_SET) == -1)
execDie("lseek to 0", "stdin");
if (Always_pass_job_queue ||
(job->node->type & (OP_MAKE | OP_SUBMAKE))) {
if (fcntl(tokenPoolJob.inPipe, F_SETFD, 0) == -1)
execDie("clear close-on-exec",
"tokenPoolJob.inPipe");
if (fcntl(tokenPoolJob.outPipe, F_SETFD, 0) == -1)
execDie("clear close-on-exec",
"tokenPoolJob.outPipe");
}
if (dup2(job->outPipe, STDOUT_FILENO) == -1)
execDie("dup2", "job->outPipe");
if (fcntl(STDOUT_FILENO, F_SETFD, 0) == -1)
execDie("clear close-on-exec", "stdout");
if (dup2(STDOUT_FILENO, STDERR_FILENO) == -1)
execDie("dup2", "1, 2");
#if defined(HAVE_SETPGID)
(void)setpgid(0, getpid());
#else
# if defined(HAVE_SETSID)
(void)setsid();
# else
(void)setpgrp(0, getpid());
# endif
#endif
(void)execv(shellPath, argv);
execDie("exec", shellPath);
}
job->pid = cpid;
Trace_Log(JOBSTART, job);
#ifdef USE_META
if (useMeta)
meta_job_parent(job, cpid);
#endif
job->outBufLen = 0;
watchfd(job);
if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
if (fclose(job->cmdFILE) != 0)
Punt("Cannot write shell script for \"%s\": %s",
job->node->name, strerror(errno));
job->cmdFILE = NULL;
}
if (DEBUG(JOB)) {
debug_printf(
"JobExec: target %s, pid %d added to jobs table\n",
job->node->name, job->pid);
JobTable_Dump("job started");
}
JobsTable_Unlock(&mask);
}
static void
BuildArgv(Job *job, char **argv)
{
int argc;
static char args[10];
argv[0] = UNCONST(shellName);
argc = 1;
if ((shell->errFlag != NULL && shell->errFlag[0] != '-') ||
(shell->echoFlag != NULL && shell->echoFlag[0] != '-')) {
(void)snprintf(args, sizeof args, "-%s%s",
!job->ignerr && shell->errFlag != NULL
? shell->errFlag : "",
job->echo && shell->echoFlag != NULL
? shell->echoFlag : "");
if (args[1] != '\0') {
argv[argc] = args;
argc++;
}
} else {
if (!job->ignerr && shell->errFlag != NULL) {
argv[argc] = UNCONST(shell->errFlag);
argc++;
}
if (job->echo && shell->echoFlag != NULL) {
argv[argc] = UNCONST(shell->echoFlag);
argc++;
}
}
argv[argc] = NULL;
}
static void
JobWriteShellCommands(Job *job, GNode *gn, bool *out_run)
{
char fname[MAXPATHLEN];
int fd;
fd = Job_TempFile(NULL, fname, sizeof fname);
job->cmdFILE = fdopen(fd, "w+");
if (job->cmdFILE == NULL)
Punt("Could not fdopen %s", fname);
(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
#ifdef USE_META
if (useMeta) {
meta_job_start(job, gn);
if (gn->type & OP_SILENT)
job->echo = false;
}
#endif
*out_run = JobWriteCommands(job);
}
void
Job_Make(GNode *gn)
{
Job *job;
char *argv[10];
bool cmdsOK;
bool run;
for (job = job_table; job < job_table_end; job++) {
if (job->status == JOB_ST_FREE)
break;
}
if (job >= job_table_end)
Punt("Job_Make no job slots vacant");
memset(job, 0, sizeof *job);
job->node = gn;
job->tailCmds = NULL;
job->status = JOB_ST_SET_UP;
job->special = (gn->type & OP_SPECIAL) != OP_NONE;
job->ignerr = opts.ignoreErrors || gn->type & OP_IGNORE;
job->echo = !(opts.silent || gn->type & OP_SILENT);
cmdsOK = Job_CheckCommands(gn, Error);
job->inPollfd = NULL;
if (Lst_IsEmpty(&gn->commands)) {
job->cmdFILE = stdout;
run = false;
if (!cmdsOK) {
PrintOnError(gn, "\n");
DieHorribly();
}
} else if (((gn->type & OP_MAKE) && !opts.noRecursiveExecute) ||
(!opts.noExecute && !opts.touch)) {
int parseErrorsBefore;
if (!cmdsOK) {
PrintOnError(gn, "\n");
DieHorribly();
}
parseErrorsBefore = parseErrors;
JobWriteShellCommands(job, gn, &run);
if (parseErrors != parseErrorsBefore)
run = false;
(void)fflush(job->cmdFILE);
} else if (!GNode_ShouldExecute(gn)) {
SwitchOutputTo(gn);
job->cmdFILE = stdout;
if (cmdsOK)
JobWriteCommands(job);
run = false;
(void)fflush(job->cmdFILE);
} else {
Job_Touch(gn, job->echo);
run = false;
}
if (!run) {
if (!job->special)
TokenPool_Return();
if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
(void)fclose(job->cmdFILE);
job->cmdFILE = NULL;
}
if (cmdsOK && aborting == ABORT_NONE) {
JobSaveCommands(job);
job->node->made = MADE;
Make_Update(job->node);
}
job->status = JOB_ST_FREE;
return;
}
BuildArgv(job, argv);
JobCreatePipe(job, 3);
JobExec(job, argv);
}
static const char *
PrintFilteredOutput(Job *job, size_t len)
{
const char *p = job->outBuf, *ep, *endp;
if (shell->noPrint == NULL || shell->noPrint[0] == '\0')
return p;
endp = p + len;
while ((ep = strstr(p, shell->noPrint)) != NULL && ep < endp) {
if (ep > p) {
if (!opts.silent)
SwitchOutputTo(job->node);
(void)fwrite(p, 1, (size_t)(ep - p), stdout);
(void)fflush(stdout);
}
p = ep + shell->noPrintLen;
if (p == endp)
break;
p++;
cpp_skip_whitespace(&p);
}
return p;
}
static void
CollectOutput(Job *job, bool finish)
{
const char *p;
size_t nr;
size_t i;
size_t max;
again:
nr = (size_t)read(job->inPipe, job->outBuf + job->outBufLen,
JOB_BUFSIZE - job->outBufLen);
if (nr == (size_t)-1) {
if (errno == EAGAIN)
return;
if (DEBUG(JOB))
perror("CollectOutput(piperead)");
nr = 0;
}
if (nr == 0)
finish = false;
if (nr == 0 && job->outBufLen > 0) {
job->outBuf[job->outBufLen] = '\n';
nr = 1;
}
max = job->outBufLen + nr;
job->outBuf[max] = '\0';
for (i = job->outBufLen; i < max; i++)
if (job->outBuf[i] == '\0')
job->outBuf[i] = ' ';
for (i = max; i > job->outBufLen; i--)
if (job->outBuf[i - 1] == '\n')
break;
if (i == job->outBufLen) {
job->outBufLen = max;
if (max < JOB_BUFSIZE)
goto unfinished_line;
i = max;
}
p = PrintFilteredOutput(job, i);
if (*p != '\0') {
if (!opts.silent)
SwitchOutputTo(job->node);
#ifdef USE_META
if (useMeta)
meta_job_output(job, p, (i < max) ? i : max);
#endif
(void)fwrite(p, 1, (size_t)(job->outBuf + i - p), stdout);
(void)fflush(stdout);
}
memmove(job->outBuf, job->outBuf + i, max - i);
job->outBufLen = max - i;
unfinished_line:
if (finish)
goto again;
}
static void
JobRun(GNode *target)
{
Compat_Make(target, target);
if (GNode_IsError(target)) {
PrintOnError(target, "\n\nStop.\n");
exit(1);
}
}
void
Job_CatchChildren(void)
{
int pid;
WAIT_T status;
if (jobTokensRunning == 0)
return;
if (caught_sigchld == 0)
return;
caught_sigchld = 0;
while ((pid = waitpid((pid_t)-1, &status, WNOHANG | WUNTRACED)) > 0) {
DEBUG2(JOB,
"Process with pid %d exited/stopped with status %#x.\n",
pid, WAIT_STATUS(status));
JobReapChild(pid, status, true);
}
}
void
JobReapChild(pid_t pid, WAIT_T status, bool isJobs)
{
Job *job;
if (jobTokensRunning == 0)
return;
job = JobFindPid(pid, JOB_ST_RUNNING, isJobs);
if (job == NULL) {
if (isJobs && !lurking_children)
Error("Child with pid %d and status %#x not in table?",
pid, status);
return;
}
if (WIFSTOPPED(status)) {
DEBUG2(JOB, "Process for target %s, pid %d stopped\n",
job->node->name, job->pid);
if (!make_suspended) {
switch (WSTOPSIG(status)) {
case SIGTSTP:
(void)printf("*** [%s] Suspended\n",
job->node->name);
break;
case SIGSTOP:
(void)printf("*** [%s] Stopped\n",
job->node->name);
break;
default:
(void)printf("*** [%s] Stopped -- signal %d\n",
job->node->name, WSTOPSIG(status));
}
job->suspended = true;
}
(void)fflush(stdout);
return;
}
job->status = JOB_ST_FINISHED;
job->exit_status = WAIT_STATUS(status);
if (WIFEXITED(status))
job->node->exit_status = WEXITSTATUS(status);
JobFinish(job, status);
}
static void
Job_Continue(Job *job)
{
DEBUG1(JOB, "Continuing pid %d\n", job->pid);
if (job->suspended) {
(void)printf("*** [%s] Continued\n", job->node->name);
(void)fflush(stdout);
job->suspended = false;
}
if (KILLPG(job->pid, SIGCONT) != 0)
DEBUG1(JOB, "Failed to send SIGCONT to pid %d\n", job->pid);
}
static void
ContinueJobs(void)
{
Job *job;
for (job = job_table; job < job_table_end; job++) {
if (job->status == JOB_ST_RUNNING &&
(make_suspended || job->suspended))
Job_Continue(job);
else if (job->status == JOB_ST_FINISHED)
JobFinish(job, job->exit_status);
}
make_suspended = false;
}
void
Job_CatchOutput(void)
{
int nready;
Job *job;
unsigned i;
(void)fflush(stdout);
do {
nfds_t skip = wantToken ? 0 : 1;
nready = poll(fds + skip, fdsLen - skip, -1);
} while (nready < 0 && errno == EINTR);
if (nready < 0)
Punt("poll: %s", strerror(errno));
if (nready > 0 && childExitJob.inPollfd->revents & POLLIN) {
char token;
ssize_t count = read(childExitJob.inPipe, &token, 1);
if (count != 1)
Punt("childExitJob.read: %s",
count == 0 ? "EOF" : strerror(errno));
if (token == CEJ_RESUME_JOBS)
ContinueJobs();
nready--;
}
Job_CatchChildren();
if (nready == 0)
return;
for (i = npseudojobs * nfds_per_job(); i < fdsLen; i++) {
if (fds[i].revents == 0)
continue;
job = jobByFdIndex[i];
if (job->status == JOB_ST_RUNNING)
CollectOutput(job, false);
#if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
if (useMeta && job->inPollfd != &fds[i]) {
if (meta_job_event(job) <= 0)
fds[i].events = 0;
}
#endif
if (--nready == 0)
return;
}
}
static void
InitShellNameAndPath(void)
{
shellName = shell->name;
#ifdef DEFSHELL_CUSTOM
if (shellName[0] == '/') {
shellPath = bmake_strdup(shellName);
shellName = str_basename(shellPath);
return;
}
#endif
#ifdef DEFSHELL_PATH
shellPath = bmake_strdup(DEFSHELL_PATH);
#else
shellPath = str_concat3(_PATH_DEFSHELLDIR, "/", shellName);
#endif
}
void
Shell_Init(void)
{
if (shellPath == NULL)
InitShellNameAndPath();
Var_SetWithFlags(SCOPE_CMDLINE, ".SHELL", shellPath,
VAR_SET_INTERNAL|VAR_SET_READONLY);
if (shell->errFlag == NULL)
shell->errFlag = "";
if (shell->echoFlag == NULL)
shell->echoFlag = "";
if (shell->hasErrCtl && shell->errFlag[0] != '\0') {
if (shellErrFlag != NULL &&
strcmp(shell->errFlag, &shellErrFlag[1]) != 0) {
free(shellErrFlag);
shellErrFlag = NULL;
}
if (shellErrFlag == NULL)
shellErrFlag = str_concat2("-", shell->errFlag);
} else if (shellErrFlag != NULL) {
free(shellErrFlag);
shellErrFlag = NULL;
}
}
const char *
Shell_GetNewline(void)
{
return shell->newline;
}
void
Job_SetPrefix(void)
{
if (targPrefix != NULL)
free(targPrefix);
else if (!Var_Exists(SCOPE_GLOBAL, ".MAKE.JOB.PREFIX"))
Global_Set(".MAKE.JOB.PREFIX", "---");
targPrefix = Var_Subst("${.MAKE.JOB.PREFIX}",
SCOPE_GLOBAL, VARE_EVAL);
}
static void
AddSig(int sig, SignalProc handler)
{
if (bmake_signal(sig, SIG_IGN) != SIG_IGN) {
sigaddset(&caught_signals, sig);
(void)bmake_signal(sig, handler);
}
}
void
Job_Init(void)
{
Job_SetPrefix();
job_table = bmake_malloc((size_t)opts.maxJobs * sizeof *job_table);
memset(job_table, 0, (size_t)opts.maxJobs * sizeof *job_table);
job_table_end = job_table + opts.maxJobs;
wantToken = false;
caught_sigchld = 0;
aborting = ABORT_NONE;
job_errors = 0;
Always_pass_job_queue = GetBooleanExpr(MAKE_ALWAYS_PASS_JOB_QUEUE,
Always_pass_job_queue);
Job_error_token = GetBooleanExpr(MAKE_JOB_ERROR_TOKEN, Job_error_token);
for (;;) {
int rval;
WAIT_T status;
rval = waitpid((pid_t)-1, &status, WNOHANG);
if (rval > 0)
continue;
if (rval == 0)
lurking_children = true;
break;
}
Shell_Init();
JobCreatePipe(&childExitJob, 3);
{
size_t nfds = (npseudojobs + (size_t)opts.maxJobs) *
nfds_per_job();
fds = bmake_malloc(sizeof *fds * nfds);
jobByFdIndex = bmake_malloc(sizeof *jobByFdIndex * nfds);
}
watchfd(&tokenPoolJob);
watchfd(&childExitJob);
sigemptyset(&caught_signals);
(void)bmake_signal(SIGCHLD, HandleSIGCHLD);
sigaddset(&caught_signals, SIGCHLD);
AddSig(SIGINT, JobPassSig_int);
AddSig(SIGHUP, JobPassSig_term);
AddSig(SIGTERM, JobPassSig_term);
AddSig(SIGQUIT, JobPassSig_term);
AddSig(SIGTSTP, JobPassSig_suspend);
AddSig(SIGTTOU, JobPassSig_suspend);
AddSig(SIGTTIN, JobPassSig_suspend);
AddSig(SIGWINCH, JobCondPassSig);
AddSig(SIGCONT, HandleSIGCONT);
(void)Job_RunTarget(".BEGIN", NULL);
(void)Targ_GetEndNode();
}
static void
DelSig(int sig)
{
if (sigismember(&caught_signals, sig) != 0)
(void)bmake_signal(sig, SIG_DFL);
}
static void
JobSigReset(void)
{
DelSig(SIGINT);
DelSig(SIGHUP);
DelSig(SIGQUIT);
DelSig(SIGTERM);
DelSig(SIGTSTP);
DelSig(SIGTTOU);
DelSig(SIGTTIN);
DelSig(SIGWINCH);
DelSig(SIGCONT);
(void)bmake_signal(SIGCHLD, SIG_DFL);
}
static Shell *
FindShellByName(const char *name)
{
Shell *sh = shells;
const Shell *shellsEnd = sh + sizeof shells / sizeof shells[0];
for (sh = shells; sh < shellsEnd; sh++) {
if (strcmp(name, sh->name) == 0)
return sh;
}
return NULL;
}
bool
Job_ParseShell(char *line)
{
Words wordsList;
char **words;
char **argv;
size_t argc;
char *path;
Shell newShell;
bool fullSpec = false;
Shell *sh;
pp_skip_whitespace(&line);
free(shell_freeIt);
memset(&newShell, 0, sizeof newShell);
wordsList = Str_Words(line, true);
words = wordsList.words;
argc = wordsList.len;
path = wordsList.freeIt;
if (words == NULL) {
Error("Unterminated quoted string [%s]", line);
return false;
}
shell_freeIt = path;
for (path = NULL, argv = words; argc != 0; argc--, argv++) {
char *arg = *argv;
if (strncmp(arg, "path=", 5) == 0) {
path = arg + 5;
} else if (strncmp(arg, "name=", 5) == 0) {
newShell.name = arg + 5;
} else {
if (strncmp(arg, "quiet=", 6) == 0) {
newShell.echoOff = arg + 6;
} else if (strncmp(arg, "echo=", 5) == 0) {
newShell.echoOn = arg + 5;
} else if (strncmp(arg, "filter=", 7) == 0) {
newShell.noPrint = arg + 7;
newShell.noPrintLen = strlen(newShell.noPrint);
} else if (strncmp(arg, "echoFlag=", 9) == 0) {
newShell.echoFlag = arg + 9;
} else if (strncmp(arg, "errFlag=", 8) == 0) {
newShell.errFlag = arg + 8;
} else if (strncmp(arg, "hasErrCtl=", 10) == 0) {
char c = arg[10];
newShell.hasErrCtl = c == 'Y' || c == 'y' ||
c == 'T' || c == 't';
} else if (strncmp(arg, "newline=", 8) == 0) {
newShell.newline = arg + 8;
} else if (strncmp(arg, "check=", 6) == 0) {
newShell.errOn = arg + 6;
newShell.echoTmpl = arg + 6;
} else if (strncmp(arg, "ignore=", 7) == 0) {
newShell.errOff = arg + 7;
newShell.runIgnTmpl = arg + 7;
} else if (strncmp(arg, "errout=", 7) == 0) {
newShell.runChkTmpl = arg + 7;
} else if (strncmp(arg, "comment=", 8) == 0) {
newShell.commentChar = arg[8];
} else {
Parse_Error(PARSE_FATAL,
"Unknown keyword \"%s\"", arg);
free(words);
return false;
}
fullSpec = true;
}
}
if (path == NULL) {
if (newShell.name == NULL) {
Parse_Error(PARSE_FATAL,
"Neither path nor name specified");
free(words);
return false;
} else {
if ((sh = FindShellByName(newShell.name)) == NULL) {
Parse_Error(PARSE_WARNING,
"%s: No matching shell", newShell.name);
free(words);
return false;
}
shell = sh;
shellName = newShell.name;
if (shellPath != NULL) {
free(shellPath);
shellPath = NULL;
Shell_Init();
}
}
} else {
free(shellPath);
shellPath = bmake_strdup(path);
shellName = newShell.name != NULL ? newShell.name
: str_basename(path);
if (!fullSpec) {
if ((sh = FindShellByName(shellName)) == NULL) {
Parse_Error(PARSE_WARNING,
"%s: No matching shell", shellName);
free(words);
return false;
}
shell = sh;
} else {
shell = bmake_malloc(sizeof *shell);
*shell = newShell;
}
Shell_Init();
}
if (shell->echoOn != NULL && shell->echoOff != NULL)
shell->hasEchoCtl = true;
if (!shell->hasErrCtl) {
if (shell->echoTmpl == NULL)
shell->echoTmpl = "";
if (shell->runIgnTmpl == NULL)
shell->runIgnTmpl = "%s\n";
}
free(words);
return true;
}
static void
JobInterrupt(bool runINTERRUPT, int signo)
{
Job *job;
sigset_t mask;
aborting = ABORT_INTERRUPT;
JobsTable_Lock(&mask);
for (job = job_table; job < job_table_end; job++) {
if (job->status == JOB_ST_RUNNING && job->pid != 0) {
DEBUG2(JOB,
"JobInterrupt passing signal %d to child %d.\n",
signo, job->pid);
KILLPG(job->pid, signo);
}
}
for (job = job_table; job < job_table_end; job++) {
if (job->status == JOB_ST_RUNNING && job->pid != 0) {
int status;
(void)waitpid(job->pid, &status, 0);
JobDeleteTarget(job->node);
}
}
JobsTable_Unlock(&mask);
if (runINTERRUPT && !opts.touch) {
GNode *dotInterrupt = Targ_FindNode(".INTERRUPT");
if (dotInterrupt != NULL) {
opts.ignoreErrors = false;
JobRun(dotInterrupt);
}
}
Trace_Log(MAKEINTR, NULL);
exit(signo);
}
int
Job_MakeDotEnd(void)
{
GNode *dotEnd = Targ_GetEndNode();
if (!Lst_IsEmpty(&dotEnd->commands) ||
!Lst_IsEmpty(&dotEnd->children)) {
if (job_errors != 0)
Error("Errors reported so .END ignored");
else
JobRun(dotEnd);
}
return job_errors;
}
#ifdef CLEANUP
void
Job_End(void)
{
free(shell_freeIt);
}
#endif
void
Job_Wait(void)
{
aborting = ABORT_WAIT;
while (jobTokensRunning != 0)
Job_CatchOutput();
aborting = ABORT_NONE;
}
void
Job_AbortAll(void)
{
Job *job;
WAIT_T status;
aborting = ABORT_ERROR;
if (jobTokensRunning != 0) {
for (job = job_table; job < job_table_end; job++) {
if (job->status != JOB_ST_RUNNING)
continue;
KILLPG(job->pid, SIGINT);
KILLPG(job->pid, SIGKILL);
}
}
while (waitpid((pid_t)-1, &status, WNOHANG) > 0)
continue;
}
static void
watchfd(Job *job)
{
if (job->inPollfd != NULL)
Punt("Watching watched job");
fds[fdsLen].fd = job->inPipe;
fds[fdsLen].events = POLLIN;
jobByFdIndex[fdsLen] = job;
job->inPollfd = &fds[fdsLen];
fdsLen++;
#if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
if (useMeta) {
fds[fdsLen].fd = meta_job_fd(job);
fds[fdsLen].events = fds[fdsLen].fd == -1 ? 0 : POLLIN;
jobByFdIndex[fdsLen] = job;
fdsLen++;
}
#endif
}
static void
clearfd(Job *job)
{
size_t i;
if (job->inPollfd == NULL)
Punt("Unwatching unwatched job");
i = (size_t)(job->inPollfd - fds);
fdsLen--;
#if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
if (useMeta) {
assert(nfds_per_job() == 2);
if (i % 2 != 0)
Punt("odd-numbered fd with meta");
fdsLen--;
}
#endif
if (fdsLen != i) {
fds[i] = fds[fdsLen];
jobByFdIndex[i] = jobByFdIndex[fdsLen];
jobByFdIndex[i]->inPollfd = &fds[i];
#if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
if (useMeta) {
fds[i + 1] = fds[fdsLen + 1];
jobByFdIndex[i + 1] = jobByFdIndex[fdsLen + 1];
}
#endif
}
job->inPollfd = NULL;
}
int
Job_TempFile(const char *pattern, char *tfile, size_t tfile_sz)
{
int fd;
sigset_t mask;
JobsTable_Lock(&mask);
fd = mkTempFile(pattern, tfile, tfile_sz);
if (tfile != NULL && !DEBUG(SCRIPT))
unlink(tfile);
JobsTable_Unlock(&mask);
return fd;
}
static void
TokenPool_Write(char tok)
{
if (write(tokenPoolJob.outPipe, &tok, 1) != 1)
Punt("Cannot write \"%c\" to the token pool: %s",
tok, strerror(errno));
}
static void
TokenPool_Add(void)
{
char tok = JOB_TOKENS[aborting], tok1;
if (!Job_error_token && aborting == ABORT_ERROR) {
if (jobTokensRunning == 0)
return;
tok = '+';
}
while (tok != '+' && read(tokenPoolJob.inPipe, &tok1, 1) == 1)
continue;
DEBUG3(JOB, "TokenPool_Add: pid %d, aborting %s, token %c\n",
getpid(), aborting_name[aborting], tok);
TokenPool_Write(tok);
}
static void
TokenPool_InitClient(int tokenPoolReader, int tokenPoolWriter)
{
tokenPoolJob.inPipe = tokenPoolReader;
tokenPoolJob.outPipe = tokenPoolWriter;
(void)fcntl(tokenPoolReader, F_SETFD, FD_CLOEXEC);
(void)fcntl(tokenPoolWriter, F_SETFD, FD_CLOEXEC);
}
static void
TokenPool_InitServer(int maxJobTokens)
{
int i;
char jobarg[64];
JobCreatePipe(&tokenPoolJob, 15);
snprintf(jobarg, sizeof jobarg, "%d,%d",
tokenPoolJob.inPipe, tokenPoolJob.outPipe);
Global_Append(MAKEFLAGS, "-J");
Global_Append(MAKEFLAGS, jobarg);
SetNonblocking(tokenPoolJob.outPipe);
for (i = 1; i < maxJobTokens; i++)
TokenPool_Add();
}
void
TokenPool_Init(int maxJobTokens, int tokenPoolReader, int tokenPoolWriter)
{
if (tokenPoolReader >= 0 && tokenPoolWriter >= 0)
TokenPool_InitClient(tokenPoolReader, tokenPoolWriter);
else
TokenPool_InitServer(maxJobTokens);
}
void
TokenPool_Return(void)
{
jobTokensRunning--;
if (jobTokensRunning < 0)
Punt("token botch");
if (jobTokensRunning != 0 || JOB_TOKENS[aborting] != '+')
TokenPool_Add();
}
bool
TokenPool_Take(void)
{
char tok, tok1;
ssize_t count;
wantToken = false;
DEBUG3(JOB, "TokenPool_Take: pid %d, aborting %s, running %d\n",
getpid(), aborting_name[aborting], jobTokensRunning);
if (aborting != ABORT_NONE || jobTokensRunning >= opts.maxJobs)
return false;
count = read(tokenPoolJob.inPipe, &tok, 1);
if (count == 0)
Fatal("eof on job pipe");
if (count < 0 && jobTokensRunning != 0) {
if (errno != EAGAIN)
Fatal("job pipe read: %s", strerror(errno));
DEBUG1(JOB, "TokenPool_Take: pid %d blocked for token\n",
getpid());
wantToken = true;
return false;
}
if (count == 1 && tok != '+') {
DEBUG2(JOB, "TokenPool_Take: pid %d aborted by token %c\n",
getpid(), tok);
while (read(tokenPoolJob.inPipe, &tok1, 1) == 1)
continue;
TokenPool_Write(tok);
if (shouldDieQuietly(NULL, 1)) {
Job_Wait();
exit(6);
}
Fatal("A failure has been detected "
"in another branch of the parallel make");
}
if (count == 1 && jobTokensRunning == 0)
TokenPool_Write(tok);
jobTokensRunning++;
DEBUG1(JOB, "TokenPool_Take: pid %d took a token\n", getpid());
return true;
}
bool
Job_RunTarget(const char *target, const char *fname)
{
GNode *gn = Targ_FindNode(target);
if (gn == NULL)
return false;
if (fname != NULL)
Var_Set(gn, ALLSRC, fname);
JobRun(gn);
return true;
}
#ifdef USE_SELECT
int
emul_poll(struct pollfd *fd, int nfd, int timeout)
{
fd_set rfds, wfds;
int i, maxfd, nselect, npoll;
struct timeval tv, *tvp;
long usecs;
FD_ZERO(&rfds);
FD_ZERO(&wfds);
maxfd = -1;
for (i = 0; i < nfd; i++) {
fd[i].revents = 0;
if (fd[i].events & POLLIN)
FD_SET(fd[i].fd, &rfds);
if (fd[i].events & POLLOUT)
FD_SET(fd[i].fd, &wfds);
if (fd[i].fd > maxfd)
maxfd = fd[i].fd;
}
if (maxfd >= FD_SETSIZE) {
Punt("Ran out of fd_set slots; "
"recompile with a larger FD_SETSIZE.");
}
if (timeout < 0) {
tvp = NULL;
} else {
usecs = timeout * 1000;
tv.tv_sec = usecs / 1000000;
tv.tv_usec = usecs % 1000000;
tvp = &tv;
}
nselect = select(maxfd + 1, &rfds, &wfds, NULL, tvp);
if (nselect <= 0)
return nselect;
npoll = 0;
for (i = 0; i < nfd; i++) {
if (FD_ISSET(fd[i].fd, &rfds))
fd[i].revents |= POLLIN;
if (FD_ISSET(fd[i].fd, &wfds))
fd[i].revents |= POLLOUT;
if (fd[i].revents)
npoll++;
}
return npoll;
}
#endif