Lines Matching refs:job

1 /*	$NetBSD: job.c,v 1.187 2016/05/12 20:28:34 sjg Exp $	*/
73 static char rcsid[] = "$NetBSD: job.c,v 1.187 2016/05/12 20:28:34 sjg Exp $";
78 static char sccsid[] = "@(#)job.c 8.2 (Berkeley) 3/19/94";
80 __RCSID("$NetBSD: job.c,v 1.187 2016/05/12 20:28:34 sjg Exp $");
86 * job.c --
95 * a decent clip, since job table entries aren't
121 * job table is empty.
168 #include "job.h"
227 #define JOB_ERROR 1 /* Error in starting the job */
228 #define JOB_FINISHED 2 /* The job is already finished */
355 static Job tokenWaitJob; /* token wait pseudo-job */
357 static Job childExitJob; /* child exit pseudo-job */
397 Job *job;
399 fprintf(debug_file, "job table @ %s\n", where);
400 for (job = job_table; job < job_table_end; job++) {
401 fprintf(debug_file, "job %d, status %d, flags %d, pid %d\n",
402 (int)(job - job_table), job->job_state, job->flags, job->pid);
426 JobCreatePipe(Job *job, int minfd)
430 if (pipe(job->jobPipe) == -1)
435 fd = fcntl(job->jobPipe[i], F_DUPFD, minfd);
437 close(job->jobPipe[i]);
438 job->jobPipe[i] = fd;
443 (void)fcntl(job->jobPipe[0], F_SETFD, FD_CLOEXEC);
444 (void)fcntl(job->jobPipe[1], F_SETFD, FD_CLOEXEC);
448 * pipe when we're waiting for a job token, but we might lose the
452 fcntl(job->jobPipe[0], F_SETFL,
453 fcntl(job->jobPipe[0], F_GETFL, 0) | O_NONBLOCK);
459 * Pass a signal to a job
465 * None, except the job may bite it.
472 Job *job;
478 for (job = job_table; job < job_table_end; job++) {
479 if (job->job_state != JOB_ST_RUNNING)
484 signo, job->pid);
486 KILLPG(job->pid, signo);
579 /* Suppress job started/continued messages */
582 /* Pass the signal onto every job */
634 * Compare the pid of the job with the given pid and return 0 if they
636 * to find the job descriptor of the finished job.
639 * job job to examine
652 Job *job;
654 for (job = job_table; job < job_table_end; job++) {
655 if ((job->job_state == status) && job->pid == pid)
656 return job;
666 * Put out another command for the given job. If the command starts
670 * we ignore errors for the entire job, unless the shell has error
673 * job to be commands to be executed once the entire graph has been
681 * jobp job for which to print it
688 * the JOB_IGNERR flag is set in the job descriptor.
710 Job *job = (Job *)jobp;
713 noSpecials = NoExecute(job->node);
716 job->node->type |= OP_SAVE_CMDS;
717 if ((job->flags & JOB_IGNDOTS) == 0) {
718 job->tailCmds = Lst_Succ(Lst_Member(job->node->commands,
728 (void)fprintf(job->cmdFILE, fmt, arg); \
729 (void)fflush(job->cmdFILE);
733 cmdStart = cmd = Var_Subst(NULL, cmd, job->node, VARF_WANTRES);
754 CompatRunCommand(cmdp, job->node);
784 if (!(job->flags & JOB_SILENT) && !noSpecials &&
804 if (!(job->flags & JOB_SILENT) && !shutUp &&
824 job->flags |= JOB_IGNERR;
825 if (!(job->flags & JOB_SILENT) && !shutUp) {
859 if (!(job->flags & JOB_SILENT) && !shutUp) {
877 (job->flags & JOB_TRACED) == 0) {
879 job->flags |= JOB_TRACED;
891 if (!shutUp && !(job->flags & JOB_SILENT) && commandShell->hasEchoCtl){
929 * Called to close both input and output pipes when a job is finished.
935 * The file descriptors associated with the job are closed.
940 JobClose(Job *job)
942 clearfd(job);
943 (void)close(job->outPipe);
944 job->outPipe = -1;
946 JobDoOutput(job, TRUE);
947 (void)close(job->inPipe);
948 job->inPipe = -1;
954 * Do final processing for the given job including updating
962 * job job to finish
963 * status sub-why job went away
969 * Final commands for the job are placed on postCommands.
972 * the job list is now empty, we are done for the day.
979 JobFinish (Job *job, WAIT_T status)
985 job->pid, job->node->name, status);
989 (((WEXITSTATUS(status) != 0) && !(job->flags & JOB_IGNERR)))) ||
994 * way or we're not ignoring errors, the job is finished.
996 * the job is also finished. In these
997 * cases, finish out the job's output before printing the exit
1000 JobClose(job);
1001 if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
1002 (void)fclose(job->cmdFILE);
1003 job->cmdFILE = NULL;
1011 * TRUE if in -B mode and the job exited non-zero.
1021 JobClose(job);
1033 job->pid, job->node->name);
1036 if (job->node != lastNode) {
1037 MESSAGE(stdout, job->node);
1038 lastNode = job->node;
1042 meta_job_error(job, job->node, job->flags, WEXITSTATUS(status));
1046 job->node->name,
1048 (job->flags & JOB_IGNERR) ? " (ignored)" : "");
1049 if (job->flags & JOB_IGNERR) {
1052 PrintOnError(job->node, NULL);
1055 if (job->node != lastNode) {
1056 MESSAGE(stdout, job->node);
1057 lastNode = job->node;
1060 job->node->name);
1063 if (job->node != lastNode) {
1064 MESSAGE(stdout, job->node);
1065 lastNode = job->node;
1068 job->node->name, WTERMSIG(status));
1077 if ((x = meta_job_finish(job)) != 0 && status == 0) {
1085 Trace_Log(JOBEND, job);
1086 if (!(job->flags & JOB_SPECIAL)) {
1096 * As long as we aren't aborting and the job didn't return a non-zero
1101 if (job->tailCmds != NULL) {
1102 Lst_ForEachFrom(job->node->commands, job->tailCmds,
1104 job->node);
1106 job->node->made = MADE;
1107 if (!(job->flags & JOB_SPECIAL))
1109 Make_Update(job->node);
1110 job->job_state = JOB_ST_FREE;
1113 job->job_state = JOB_ST_FREE;
1133 * If we are aborting and the job table is now empty, we finish.
1295 * Execute the shell for the given job. Called from JobStart
1298 * job Job to execute
1305 * to the job table.
1310 JobExec(Job *job, char **argv)
1315 job->flags &= ~JOB_TRACED;
1320 (void)fprintf(debug_file, "Running %s %sly\n", job->node->name, "local");
1334 if ((lastNode != job->node) && !(job->flags & JOB_SILENT)) {
1335 MESSAGE(stdout, job->node);
1336 lastNode = job->node;
1339 /* No interruptions until this job is on the `jobs' list */
1342 /* Pre-emptively mark job running, pid still zero though */
1343 job->job_state = JOB_ST_RUNNING;
1355 meta_job_child(job);
1373 if (dup2(FILENO(job->cmdFILE), 0) == -1) {
1374 execError("dup2", "job->cmdFILE");
1381 (job->node->type & (OP_MAKE | OP_SUBMAKE))) {
1383 * Pass job token pipe to submakes.
1393 if (dup2(job->outPipe, 1) == -1) {
1394 execError("dup2", "job->outPipe");
1433 job->pid = cpid;
1435 Trace_Log(JOBSTART, job);
1441 job->curPos = 0;
1443 watchfd(job);
1445 if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
1446 (void)fclose(job->cmdFILE);
1447 job->cmdFILE = NULL;
1451 * Now the job is actually running, add it to the table.
1455 job->node->name, job->pid);
1456 job_table_dump("job started");
1464 * Create the argv needed to execute the shell for a given job.
1474 JobMakeArgv(Job *job, char **argv)
1492 ((job->flags & JOB_IGNERR) ? "" :
1494 ((job->flags & JOB_SILENT) ? "" :
1502 if (!(job->flags & JOB_IGNERR) && commandShell->exit) {
1506 if (!(job->flags & JOB_SILENT) && commandShell->echo) {
1522 * flags flags for the job to override normal ones.
1528 * if there isn't actually anything left to do for the job and
1529 * JOB_RUNNING if the job has been started.
1543 Job *job; /* new job descriptor */
1546 Boolean noExec; /* Set true if we decide not to run the job */
1549 for (job = job_table; job < job_table_end; job++) {
1550 if (job->job_state == JOB_ST_FREE)
1553 if (job >= job_table_end)
1554 Punt("JobStart no job slots vacant");
1556 memset(job, 0, sizeof *job);
1557 job->job_state = JOB_ST_SETUP;
1561 job->node = gn;
1562 job->tailCmds = NULL;
1565 * Set the initial value of the flags for this job based on the global
1569 job->flags = 0;
1571 job->flags |= JOB_IGNERR;
1574 job->flags |= JOB_SILENT;
1576 job->flags |= flags;
1584 job->inPollfd = NULL;
1615 job->cmdFILE = fdopen(tfd, "w+");
1616 if (job->cmdFILE == NULL) {
1619 (void)fcntl(FILENO(job->cmdFILE), F_SETFD, FD_CLOEXEC);
1628 meta_job_start(job, gn);
1630 job->flags |= JOB_SILENT;
1638 Lst_ForEach(gn->commands, JobPrintCommand, job);
1652 * in one fell swoop. This will still set up job->tailCmds correctly.
1658 job->cmdFILE = stdout;
1665 Lst_ForEach(gn->commands, JobPrintCommand, job);
1678 job->cmdFILE = stdout;
1679 Job_Touch(gn, job->flags&JOB_SILENT);
1683 (void)fflush(job->cmdFILE);
1689 if (!(job->flags & JOB_SPECIAL))
1694 if (job->cmdFILE != stdout) {
1695 if (job->cmdFILE != NULL) {
1696 (void)fclose(job->cmdFILE);
1697 job->cmdFILE = NULL;
1703 * the commands for the job were no good.
1706 if (job->tailCmds != NULL) {
1707 Lst_ForEachFrom(job->node->commands, job->tailCmds,
1709 job->node);
1711 job->node->made = MADE;
1712 Make_Update(job->node);
1714 job->job_state = JOB_ST_FREE;
1720 * set earlier for this job.
1722 JobMakeArgv(job, argv);
1725 JobCreatePipe(job, 3);
1727 JobExec(job, argv);
1732 JobOutput(Job *job, char *cp, char *endp, int msg)
1741 if (!beSilent && msg && job->node != lastNode) {
1742 MESSAGE(stdout, job->node);
1743 lastNode = job->node;
1781 * output from the given job and store it in the job's outBuf. If
1782 * this makes up a line, we print it tagged by the job's identifier,
1795 * job the job whose output needs printing
1797 * for this job
1807 JobDoOutput(Job *job, Boolean finish)
1823 nRead = read(job->inPipe, &job->outBuf[job->curPos],
1824 JOB_BUFSIZE - job->curPos);
1837 * If we hit the end-of-file (the job is dead), we must flush its
1842 if ((nr == 0) && (job->curPos != 0)) {
1843 job->outBuf[job->curPos] = '\n';
1855 max = job->curPos + nr;
1856 for (i = job->curPos + nr - 1; i >= job->curPos; i--) {
1857 if (job->outBuf[i] == '\n') {
1860 } else if (job->outBuf[i] == '\0') {
1864 job->outBuf[i] = ' ';
1869 job->curPos += nr;
1870 if (job->curPos == JOB_BUFSIZE) {
1876 i = job->curPos;
1890 job->outBuf[i] = '\0';
1891 if (i >= job->curPos) {
1894 cp = JobOutput(job, job->outBuf, &job->outBuf[i], FALSE);
1902 if (!beSilent && job->node != lastNode) {
1903 MESSAGE(stdout, job->node);
1904 lastNode = job->node;
1908 meta_job_output(job, cp, gotNL ? "\n" : "");
1921 (void)memmove(job->outBuf, &job->outBuf[i + 1], max - (i + 1));
1922 job->curPos = max - (i + 1);
1925 job->curPos = 0;
1947 * and .INTERRUPT job in the parallel job module. This has
1979 * The job descriptor is removed from the list of children.
1984 * job, call JobFinish to finish things off.
2017 Job *job; /* job descriptor for dead child */
2025 job = JobFindPid(pid, JOB_ST_RUNNING, isJobs);
2026 if (job == NULL) {
2036 job->pid, job->node->name);
2041 (void)printf("*** [%s] Suspended\n", job->node->name);
2044 (void)printf("*** [%s] Stopped\n", job->node->name);
2048 job->node->name, WSTOPSIG(status));
2050 job->job_suspended = 1;
2056 job->job_state = JOB_ST_FINISHED;
2057 job->exit_status = WAIT_STATUS(status);
2059 JobFinish(job, status);
2082 Job *job;
2087 /* The first fd in the list is the job token pipe */
2122 job = jobfds[i];
2123 if (job->job_state == JOB_ST_RUNNING)
2124 JobDoOutput(job, FALSE);
2140 * Another job is started.
2237 /* Allocate space for all the job info */
2275 /* We can only need to wait for tokens, children and output from each job */
2308 * we're giving each job its own process group (since then it won't get
2582 * All children are killed. Another job will be started if the
2589 Job *job; /* job descriptor in that element */
2598 for (job = job_table; job < job_table_end; job++) {
2599 if (job->job_state != JOB_ST_RUNNING)
2602 gn = job->node;
2610 if (job->pid) {
2614 signo, job->pid);
2616 KILLPG(job->pid, signo);
2722 Job *job; /* the job descriptor in that element */
2728 for (job = job_table; job < job_table_end; job++) {
2729 if (job->job_state != JOB_ST_RUNNING)
2735 KILLPG(job->pid, SIGINT);
2736 KILLPG(job->pid, SIGKILL);
2766 Job *job;
2768 for (job = job_table; job < job_table_end; job++) {
2769 if (job->job_state == JOB_ST_RUNNING &&
2770 (make_suspended || job->job_suspended)) {
2772 (void)fprintf(debug_file, "Restarting stopped job pid %d.\n",
2773 job->pid);
2775 if (job->job_suspended) {
2776 (void)printf("*** [%s] Continued\n", job->node->name);
2779 job->job_suspended = 0;
2780 if (KILLPG(job->pid, SIGCONT) != 0 && DEBUG(JOB)) {
2781 fprintf(debug_file, "Failed to send SIGCONT to %d\n", job->pid);
2784 if (job->job_state == JOB_ST_FINISHED)
2786 JobFinish(job, job->exit_status);
2792 watchfd(Job *job)
2794 if (job->inPollfd != NULL)
2795 Punt("Watching watched job");
2797 fds[nfds].fd = job->inPipe;
2799 jobfds[nfds] = job;
2800 job->inPollfd = &fds[nfds];
2805 clearfd(Job *job)
2808 if (job->inPollfd == NULL)
2809 Punt("Unwatching unwatched job");
2810 i = job->inPollfd - fds;
2813 * Move last job in table into hole made by dead job.
2820 job->inPollfd = NULL;
2824 readyfd(Job *job)
2826 if (job->inPollfd == NULL)
2827 Punt("Polling unwatched job");
2828 return (job->inPollfd->revents & POLLIN) != 0;
2834 * Put a token into the job pipe so that some make process can start
2835 * another job.
2868 * Prep the job token pipe in the root make process.
2897 * Preload the job pipe with one token per job, save the one
2898 * "extra" token for the primary job.
2959 Fatal("eof on job pipe!");
2962 Fatal("job pipe read: %s", strerror(errno));
2971 /* make being abvorted - remove any other job tokens */