Lines Matching refs:job

39  * @(#)job.c	8.2 (Berkeley) 3/19/94
46 * job.c --
55 * whole make going at a decent clip, since job table
71 * Job_Full Return TRUE if the job table is filled.
73 * Job_Empty Return TRUE if the job table is completely empty.
78 * be called when the job table is empty.
131 #include "job.h"
157 * The job "table" is kept as a linked Lst in 'jobs', with the number of
161 * When a job is finished, the Make_Update function is called on each of the
172 * A LstNode for the first command to be saved after the job completes.
173 * This is NULL if there was no "..." in the job's commands.
179 * used before the job is actually started.
185 * echoing, etc. for the job
187 short flags; /* Flags to control treatment of job */
194 #define JOB_FIRST 0x020 /* Job is first job for the node */
198 #define JOB_CONTINUING 0x200 /* We are in the process of resuming this job.
209 * each job.
214 * job's output channel
219 * Output side of pipe associated with job's
226 * job, line by line
245 * from a single job to one file while still allowing
296 #define JOB_ERROR 1 /* Error in starting the job */
297 #define JOB_FINISHED 2 /* The job is already finished */
298 #define JOB_STOPPED 3 /* The job is stopped */
311 static Boolean jobFull; /* Flag to tell when the job table is full. It
324 * job when it's not the most-recent job heard
333 * When JobStart attempts to run a job but isn't allowed to
334 * or when Job_CatchChildren detects a job that has
335 * been stopped somehow, the job is placed on the stoppedJobs queue to be run
336 * when the next job finishes.
343 static int fifoFd; /* Fd of our job fifo */
559 Job *job;
568 TAILQ_FOREACH(job, &jobs, link) {
570 signo, (intmax_t)job->pid));
571 KILL(job->pid, signo);
612 TAILQ_FOREACH(job, &jobs, link) {
614 signo, (intmax_t)job->pid));
615 KILL(job->pid, signo);
626 * Put out another command for the given job. If the command starts
630 * we ignore errors for the entire job, unless the shell has error
633 * job to be commands to be executed once the entire graph has been
644 * the JOB_IGNERR flag is set in the job descriptor.
650 JobPrintCommand(LstNode *cmdNode, Job *job)
663 noSpecials = (noExecute && !(job->node->type & OP_MAKE));
667 fprintf(job->cmdFILE, fmt, arg); \
668 fflush(job->cmdFILE);
674 cmd = Buf_Peel(Var_Subst(Lst_Datum(cmdNode), job->node, FALSE));
677 job->node->type |= OP_SAVE_CMDS;
678 if ((job->flags & JOB_IGNDOTS) == 0) {
679 job->tailCmds = Lst_Succ(cmdNode);
708 Compat_RunCommand(cmdNode, job->node);
730 if (!(job->flags & JOB_SILENT) && !noSpecials &&
739 if (!(job->flags & JOB_IGNERR) && !noSpecials) {
750 if (!(job->flags & JOB_SILENT) && !shutUp &&
771 if (!(job->flags & JOB_SILENT) && !shutUp &&
800 if (!shutUp && !(job->flags & JOB_SILENT) &&
815 * Called to close both input and output pipes when a job is finished.
818 * The file descriptors associated with the job are closed.
821 JobClose(Job *job)
826 FD_CLR(job->inPipe, &outputs);
828 if (job->outPipe != job->inPipe) {
829 close(job->outPipe);
831 JobDoOutput(job, TRUE);
832 close(job->inPipe);
834 close(job->outFd);
835 JobDoOutput(job, TRUE);
841 * Do final processing for the given job including updating
850 * Final commands for the job are placed on postCommands.
853 * the job list is now empty, we are done for the day.
858 JobFinish(Job *job, int *status)
866 JobClose(job);
872 * TRUE if in -B mode and the job exited non-zero.
877 if (job->flags & JOB_IGNERR) {
883 * errors, the job is finished. Similarly, if
884 * the shell died because of a signal the job
886 * out the job's output before printing the
890 if (job->cmdFILE != NULL &&
891 job->cmdFILE != stdout) {
892 fclose(job->cmdFILE);
907 * errors, the job is finished. Similarly, if
908 * the shell died because of a signal the job
910 * out the job's output before printing the
913 JobClose(job);
914 if (job->cmdFILE != NULL &&
915 job->cmdFILE != stdout) {
916 fclose(job->cmdFILE);
933 (job->flags & JOB_IGNERR)) {
935 * If output is going to a file and this job
940 out = fdopen(job->outFd, "w");
948 (intmax_t)job->pid));
952 if (usePipes && job->node != lastNode) {
953 MESSAGE(out, job->node);
954 lastNode = job->node;
958 job->node->name);
961 if (usePipes && job->node != lastNode) {
962 MESSAGE(out, job->node);
963 lastNode = job->node;
966 job->node->name,
968 (job->flags & JOB_IGNERR) ?
971 if (job->flags & JOB_IGNERR) {
984 (job->flags & JOB_IGNERR)) {
986 * If output is going to a file and this job
991 out = fdopen(job->outFd, "w");
1005 if (job->flags & (JOB_RESUME | JOB_RESTART)) {
1006 if (usePipes && job->node != lastNode) {
1007 MESSAGE(out, job->node);
1008 lastNode = job->node;
1011 job->node->name);
1013 if (!(job->flags & JOB_CONTINUING)) {
1015 "continuing.\n", (intmax_t) job->pid));
1017 job->flags &= ~JOB_CONTINUING;
1018 TAILQ_INSERT_TAIL(&jobs, job, link);
1021 (intmax_t) job->pid));
1030 if (usePipes && job->node != lastNode) {
1031 MESSAGE(out, job->node);
1032 lastNode = job->node;
1035 "*** [%s] Signal %d\n", job->node->name,
1044 if (compatMake && !usePipes && (job->flags & JOB_IGNERR)) {
1046 * If output is going to a file and this job
1051 out = fdopen(job->outFd, "w");
1058 DEBUGF(JOB, ("Process %jd stopped.\n", (intmax_t) job->pid));
1059 if (usePipes && job->node != lastNode) {
1060 MESSAGE(out, job->node);
1061 lastNode = job->node;
1064 job->node->name, WSTOPSIG(*status));
1065 job->flags |= JOB_RESUME;
1066 TAILQ_INSERT_TAIL(&stoppedJobs, job, link);
1073 * try and restart the job on the next command. If JobStart says it's
1077 Lst_Succ(job->node->compat_command) != NULL) {
1078 switch (JobStart(job->node, job->flags & JOB_IGNDOTS, job)) {
1089 * already called Make_Update and freed the job
1107 * As long as we aren't aborting and the job didn't return a
1112 for (ln = job->tailCmds; ln != NULL; ln = LST_NEXT(ln)) {
1115 Var_Subst(Lst_Datum(ln), job->node, FALSE)));
1118 job->node->made = MADE;
1119 Make_Update(job->node);
1120 free(job);
1124 free(job);
1143 * If we are aborting and the job table is now empty, we finish.
1287 * Execute the shell for the given job. Called from JobStart and
1292 * to the job table.
1295 JobExec(Job *job, char **argv)
1302 DEBUGF(JOB, ("Running %s\n", job->node->name));
1316 if (lastNode != job->node && (job->flags & JOB_FIRST) &&
1317 !(job->flags & JOB_SILENT)) {
1318 MESSAGE(stdout, job->node);
1319 lastNode = job->node;
1322 ps.in = FILENO(job->cmdFILE);
1328 ps.out = job->outPipe;
1335 ps.out = job->outFd;
1368 job->pid = ps.child_pid;
1370 if (usePipes && (job->flags & JOB_FIRST)) {
1372 * The first time a job is run for a node, we set the
1379 job->curPos = 0;
1382 EV_SET(&kev[0], job->inPipe, EVFILT_READ, EV_ADD, 0, 0, job);
1383 EV_SET(&kev[1], job->pid, EVFILT_PROC,
1387 * kevent() will fail if the job is already
1394 FD_SET(job->inPipe, &outputs);
1398 if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
1399 fclose(job->cmdFILE);
1400 job->cmdFILE = NULL;
1404 * Now the job is actually running, add it to the table.
1407 TAILQ_INSERT_TAIL(&jobs, job, link);
1415 * Create the argv needed to execute the shell for a given job.
1418 JobMakeArgv(Job *job, char **argv)
1435 sprintf(args, "-%s%s", (job->flags & JOB_IGNERR) ? "" :
1437 (job->flags & JOB_SILENT) ? "" :
1445 if (!(job->flags & JOB_IGNERR) && commandShell->exit) {
1449 if (!(job->flags & JOB_SILENT) && commandShell->echo) {
1459 * Restart a job that stopped for some reason. The job must be neither
1463 * jobFull will be set if the job couldn't be run.
1466 JobRestart(Job *job)
1469 if (job->flags & JOB_RESTART) {
1472 * the flags set earlier for this job. If the JOB_IGNERR flag
1481 JobMakeArgv(job, argv);
1483 DEBUGF(JOB, ("Restarting %s...", job->node->name));
1484 if (nJobs >= maxJobs && !(job->flags & JOB_SPECIAL)) {
1490 TAILQ_INSERT_HEAD(&stoppedJobs, job, link);
1500 JobExec(job, argv);
1504 * The job has stopped and needs to be restarted.
1507 DEBUGF(JOB, ("Resuming %s...", job->node->name));
1508 if ((nJobs < maxJobs || ((job->flags & JOB_SPECIAL) &&
1512 * (or the job must be run and maxJobs is 0), it's ok
1518 error = (KILL(job->pid, SIGCONT) != 0);
1524 * job table.
1526 job->flags |= JOB_CONTINUING;
1529 JobFinish(job, &status);
1531 job->flags &= ~(JOB_RESUME|JOB_CONTINUING);
1535 job->node->name, strerror(errno));
1538 JobFinish(job, &status);
1543 * place the job back on the list of stopped jobs.
1546 TAILQ_INSERT_HEAD(&stoppedJobs, job, link);
1560 * if there isn't actually anything left to do for the job and
1561 * JOB_RUNNING if the job has been started.
1570 Job *job; /* new job descriptor */
1573 Boolean noExec; /* Set true if we decide not to run the job */
1585 job = previous;
1587 job = emalloc(sizeof(Job));
1591 job->node = gn;
1592 job->tailCmds = NULL;
1595 * Set the initial value of the flags for this job based on the global
1599 job->flags = 0;
1601 job->flags |= JOB_IGNERR;
1604 job->flags |= JOB_SILENT;
1606 job->flags |= flags;
1612 if (!compatMake && (job->flags & JOB_FIRST)) {
1639 job->cmdFILE = fdopen(tfd, "w+");
1641 if (job->cmdFILE == NULL) {
1645 fcntl(FILENO(job->cmdFILE), F_SETFD, 1);
1666 if (job->flags & JOB_FIRST)
1673 JobPrintCommand(gn->compat_command, job))
1676 if (noExec && !(job->flags & JOB_FIRST)) {
1679 * job is done and we need to close down the
1684 * open if this isn't the first job. Note also
1689 JobClose(job);
1697 if (JobPrintCommand(ln, job))
1715 * job->tailCmds correctly.
1721 job->cmdFILE = stdout;
1731 if (JobPrintCommand(ln, job))
1747 job->cmdFILE = stdout;
1748 Job_Touch(gn, job->flags & JOB_SILENT);
1759 if (job->cmdFILE != stdout) {
1760 if (job->cmdFILE != NULL)
1761 fclose(job->cmdFILE);
1768 * because the commands for the job were no good.
1772 for (ln = job->tailCmds; ln != NULL;
1776 job->node, FALSE)));
1778 job->node->made = MADE;
1779 Make_Update(job->node);
1781 free(job);
1784 free(job);
1788 fflush(job->cmdFILE);
1793 * set earlier for this job.
1795 JobMakeArgv(job, argv);
1800 * starting a job and then set up its temporary-file name.
1802 if (!compatMake || (job->flags & JOB_FIRST)) {
1808 job->inPipe = fd[0];
1809 job->outPipe = fd[1];
1810 fcntl(job->inPipe, F_SETFD, 1);
1811 fcntl(job->outPipe, F_SETFD, 1);
1815 snprintf(job->outFile, sizeof(job->outFile), "%s/%s",
1817 if ((job->outFd = mkstemp(job->outFile)) == -1)
1820 fcntl(job->outFd, F_SETFD, 1);
1824 if (nJobs >= maxJobs && !(job->flags & JOB_SPECIAL) && maxJobs != 0) {
1826 * We've hit the limit of concurrency, so put the job on hold
1827 * until some other job finishes. Note that the special jobs
1833 DEBUGF(JOB, ("Can only run job locally.\n"));
1834 job->flags |= JOB_RESTART;
1835 TAILQ_INSERT_TAIL(&stoppedJobs, job, link);
1839 * If we're running this job as a special case
1843 DEBUGF(JOB, ("Local job queue is full.\n"));
1845 JobExec(job, argv);
1851 JobOutput(Job *job, char *cp, char *endp, int msg)
1860 if (msg && job->node != lastNode) {
1861 MESSAGE(stdout, job->node);
1862 lastNode = job->node;
1901 * output from the given job and store it in the job's outBuf. If
1902 * this makes up a line, we print it tagged by the job's identifier,
1918 JobDoOutput(Job *job, Boolean finish)
1937 nRead = read(job->inPipe, &job->outBuf[job->curPos],
1938 JOB_BUFSIZE - job->curPos);
1955 * If we hit the end-of-file (the job is dead), we must flush
1960 if (nr == 0 && job->curPos != 0) {
1961 job->outBuf[job->curPos] = '\n';
1973 max = job->curPos + nr;
1974 for (i = job->curPos + nr - 1; i >= job->curPos; i--) {
1975 if (job->outBuf[i] == '\n') {
1978 } else if (job->outBuf[i] == '\0') {
1982 job->outBuf[i] = ' ';
1987 job->curPos += nr;
1988 if (job->curPos == JOB_BUFSIZE) {
1994 i = job->curPos;
2009 job->outBuf[i] = '\0';
2010 if (i >= job->curPos) {
2013 cp = JobOutput(job, job->outBuf,
2014 &job->outBuf[i], FALSE);
2022 if (job->node != lastNode) {
2023 MESSAGE(stdout, job->node);
2024 lastNode = job->node;
2033 memcpy(job->outBuf, &job->outBuf[i + 1],
2035 job->curPos = max - (i + 1);
2043 job->curPos = 0;
2060 * We've been called to retrieve the output of the job from the
2069 oFILE = fopen(job->outFile, "r");
2072 job->node->name);
2083 cp = JobOutput(job, inLine, endp, FALSE);
2098 eunlink(job->outFile);
2108 * The job descriptor is removed from the list of children.
2113 * job, call JobFinish to finish things off. This will take care of
2120 Job *job; /* job descriptor for dead child */
2139 TAILQ_FOREACH(job, &jobs, link) {
2140 if (job->pid == pid)
2144 if (job == NULL) {
2147 TAILQ_FOREACH(job, &jobs, link) {
2148 if (job->pid == pid)
2151 if (job == NULL) {
2156 TAILQ_REMOVE(&stoppedJobs, job, link);
2163 TAILQ_REMOVE(&jobs, job, link);
2178 JobFinish(job, &status);
2211 Job *job;
2237 * terminated job.
2261 job = TAILQ_FIRST(&jobs);
2262 while (nfds != 0 && job != NULL) {
2263 if (FD_ISSET(job->inPipe, &readfds)) {
2264 JobDoOutput(job, FALSE);
2267 job = TAILQ_NEXT(job, link);
2279 * Another job is started.
2321 * allowed job into the pipe.
2368 * If only one job can run at a time, there's no need for a
2400 * we're giving each job its own process group (since then it won't get
2438 * See if the job table is full. It is considered full if it is OR
2444 * TRUE if the job table is full, FALSE otherwise
2466 * See if the job table is empty. Because the local concurrency may
2467 * be set to 0, it is possible for the job table to become empty,
2480 * The job table is obviously not full if it has no
2499 * All children are killed. Another job will be started if the
2505 Job *job; /* job descriptor in that element */
2510 TAILQ_FOREACH(job, &jobs, link) {
2511 if (!Targ_Precious(job->node)) {
2512 char *file = (job->node->path == NULL ?
2513 job->node->name : job->node->path);
2519 if (job->pid) {
2521 "%jd.\n", (intmax_t)job->pid));
2522 KILL(job->pid, signo);
2612 Job *job; /* the job descriptor in that element */
2618 TAILQ_FOREACH(job, &jobs, link) {
2623 KILL(job->pid, SIGINT);
2624 KILL(job->pid, SIGKILL);
2647 Job *job;
2649 while (!jobFull && (job = TAILQ_FIRST(&stoppedJobs)) != NULL) {
2651 "Restarting a stopped job.\n"));
2652 TAILQ_REMOVE(&stoppedJobs, job, link);
2653 JobRestart(job);