job.c revision 1.100 1 /* $NetBSD: job.c,v 1.100 2006/01/04 21:25:03 dsl Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Adam de Boor.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 /*
36 * Copyright (c) 1988, 1989 by Adam de Boor
37 * Copyright (c) 1989 by Berkeley Softworks
38 * All rights reserved.
39 *
40 * This code is derived from software contributed to Berkeley by
41 * Adam de Boor.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 */
71
72 #ifndef MAKE_NATIVE
73 static char rcsid[] = "$NetBSD: job.c,v 1.100 2006/01/04 21:25:03 dsl Exp $";
74 #else
75 #include <sys/cdefs.h>
76 #ifndef lint
77 #if 0
78 static char sccsid[] = "@(#)job.c 8.2 (Berkeley) 3/19/94";
79 #else
80 __RCSID("$NetBSD: job.c,v 1.100 2006/01/04 21:25:03 dsl Exp $");
81 #endif
82 #endif /* not lint */
83 #endif
84
85 /*-
86 * job.c --
87 * handle the creation etc. of our child processes.
88 *
89 * Interface:
90 * Job_Make Start the creation of the given target.
91 *
92 * Job_CatchChildren Check for and handle the termination of any
93 * children. This must be called reasonably
94 * frequently to keep the whole make going at
95 * a decent clip, since job table entries aren't
96 * removed until their process is caught this way.
97 * Its single argument is TRUE if the function
98 * should block waiting for a child to terminate.
99 *
100 * Job_CatchOutput Print any output our children have produced.
101 * Should also be called fairly frequently to
102 * keep the user informed of what's going on.
103 * If no output is waiting, it will block for
104 * a time given by the SEL_* constants, below,
105 * or until output is ready.
106 *
107 * Job_Init Called to intialize this module. in addition,
108 * any commands attached to the .BEGIN target
109 * are executed before this function returns.
110 * Hence, the makefile must have been parsed
111 * before this function is called.
112 *
113 * Job_End Cleanup any memory used.
114 *
115 * Job_Empty Return TRUE if the job table is completely
116 * empty.
117 *
118 * Job_ParseShell Given the line following a .SHELL target, parse
119 * the line as a shell specification. Returns
120 * FAILURE if the spec was incorrect.
121 *
122 * Job_Finish Perform any final processing which needs doing.
123 * This includes the execution of any commands
124 * which have been/were attached to the .END
125 * target. It should only be called when the
126 * job table is empty.
127 *
128 * Job_AbortAll Abort all currently running jobs. It doesn't
129 * handle output or do anything for the jobs,
130 * just kills them. It should only be called in
131 * an emergency, as it were.
132 *
133 * Job_CheckCommands Verify that the commands for a target are
134 * ok. Provide them if necessary and possible.
135 *
136 * Job_Touch Update a target without really updating it.
137 *
138 * Job_Wait Wait for all currently-running jobs to finish.
139 */
140
141 #include <sys/types.h>
142 #include <sys/stat.h>
143 #include <sys/file.h>
144 #include <sys/time.h>
145 #include <sys/wait.h>
146
147 #include <errno.h>
148 #include <fcntl.h>
149 #ifndef RMT_WILL_WATCH
150 #ifndef USE_SELECT
151 #include <poll.h>
152 #endif
153 #endif
154 #include <signal.h>
155 #include <stdio.h>
156 #include <string.h>
157 #include <utime.h>
158
159 #include "make.h"
160 #include "hash.h"
161 #include "dir.h"
162 #include "job.h"
163 #include "pathnames.h"
164 #include "trace.h"
165 #ifdef REMOTE
166 #include "rmt.h"
167 # define STATIC
168 #else
169 # define STATIC static
170 #endif
171
172 /*
173 * error handling variables
174 */
175 static int errors = 0; /* number of errors reported */
176 static int aborting = 0; /* why is the make aborting? */
177 #define ABORT_ERROR 1 /* Because of an error */
178 #define ABORT_INTERRUPT 2 /* Because it was interrupted */
179 #define ABORT_WAIT 3 /* Waiting for jobs to finish */
180
181 /*
182 * XXX: Avoid SunOS bug... FILENO() is fp->_file, and file
183 * is a char! So when we go above 127 we turn negative!
184 */
185 #define FILENO(a) ((unsigned) fileno(a))
186
187 /*
188 * post-make command processing. The node postCommands is really just the
189 * .END target but we keep it around to avoid having to search for it
190 * all the time.
191 */
192 static GNode *postCommands = NILGNODE;
193 /* node containing commands to execute when
194 * everything else is done */
195 static int numCommands; /* The number of commands actually printed
196 * for a target. Should this number be
197 * 0, no shell will be executed. */
198
199 /*
200 * Return values from JobStart.
201 */
202 #define JOB_RUNNING 0 /* Job is running */
203 #define JOB_ERROR 1 /* Error in starting the job */
204 #define JOB_FINISHED 2 /* The job is already finished */
205 #define JOB_STOPPED 3 /* The job is stopped */
206
207
208
209 /*
210 * Descriptions for various shells.
211 */
212 static Shell shells[] = {
213 /*
214 * CSH description. The csh can do echo control by playing
215 * with the setting of the 'echo' shell variable. Sadly,
216 * however, it is unable to do error control nicely.
217 */
218 {
219 "csh",
220 TRUE, "unset verbose", "set verbose", "unset verbose", 10,
221 FALSE, "echo \"%s\"\n", "csh -c \"%s || exit 0\"\n", "", '#',
222 "v", "e",
223 },
224 /*
225 * SH description. Echo control is also possible and, under
226 * sun UNIX anyway, one can even control error checking.
227 */
228 {
229 "sh",
230 FALSE, "", "", "", 0,
231 FALSE, "echo \"%s\"\n", "%s\n", "{ %s \n} || exit $?\n", '#',
232 #ifdef __NetBSD__
233 "q",
234 #else
235 "",
236 #endif
237 "",
238 },
239 /*
240 * KSH description.
241 */
242 {
243 "ksh",
244 TRUE, "set +v", "set -v", "set +v", 6,
245 FALSE, "echo \"%s\"\n", "%s\n", "{ %s \n} || exit $?\n", '#',
246 "v",
247 "",
248 },
249 /*
250 * UNKNOWN.
251 */
252 {
253 NULL,
254 FALSE, NULL, NULL, NULL, 0,
255 FALSE, NULL, NULL, NULL, 0,
256 NULL, NULL,
257 }
258 };
259 static Shell *commandShell = &shells[DEFSHELL];/* this is the shell to
260 * which we pass all
261 * commands in the Makefile.
262 * It is set by the
263 * Job_ParseShell function */
264 const char *shellPath = NULL, /* full pathname of
265 * executable image */
266 *shellName = NULL; /* last component of shell */
267 static const char *shellArgv = NULL; /* Custom shell args */
268
269
270 static int maxJobs; /* The most children we can run at once */
271 static int maxLocal; /* The most local ones we can have */
272 STATIC int nJobs; /* The number of children currently running */
273 STATIC int nLocal; /* The number of local children */
274 STATIC Lst jobs; /* The structures that describe them */
275 static Boolean wantToken; /* we want a token */
276
277 /*
278 * Set of descriptors of pipes connected to
279 * the output channels of children
280 */
281 #ifndef RMT_WILL_WATCH
282 static struct pollfd *fds = NULL;
283 static Job **jobfds = NULL;
284 static int nfds = 0;
285 static int maxfds = 0;
286 static void watchfd(Job *);
287 static void clearfd(Job *);
288 static int readyfd(Job *);
289 #define JBSTART 256
290 #define JBFACTOR 2
291 #endif
292
293 STATIC GNode *lastNode; /* The node for which output was most recently
294 * produced. */
295 STATIC const char *targFmt; /* Format string to use to head output from a
296 * job when it's not the most-recent job heard
297 * from */
298 static Job tokenWaitJob; /* token wait pseudo-job */
299 int job_pipe[2] = { -1, -1 }; /* job server pipes. */
300
301 static Job childExitJob; /* child exit pseudo-job */
302 int exit_pipe[2] = { -1, -1 }; /* child exit signal pipe. */
303
304 #ifdef REMOTE
305 # define TARG_FMT "--- %s at %s ---\n" /* Default format */
306 # define MESSAGE(fp, gn) \
307 (void)fprintf(fp, targFmt, gn->name, gn->rem.hname)
308 #else
309 # define TARG_FMT "--- %s ---\n" /* Default format */
310 # define MESSAGE(fp, gn) \
311 (void)fprintf(fp, targFmt, gn->name)
312 #endif
313
314 /*
315 * When JobStart attempts to run a job remotely but can't, and isn't allowed
316 * to run the job locally, or when Job_CatchChildren detects a job that has
317 * been migrated home, the job is placed on the stoppedJobs queue to be run
318 * when the next job finishes.
319 */
320 STATIC Lst stoppedJobs; /* Lst of Job structures describing
321 * jobs that were stopped due to concurrency
322 * limits or migration home */
323
324
325 sigset_t caught_signals; /* Set of signals we handle */
326 #if defined(USE_PGRP) && defined(SYSV)
327 # define KILL(pid, sig) kill(-(pid), (sig))
328 #else
329 # if defined(USE_PGRP)
330 # define KILL(pid, sig) killpg((pid), (sig))
331 # else
332 # define KILL(pid, sig) kill((pid), (sig))
333 # endif
334 #endif
335
336 /*
337 * Grmpf... There is no way to set bits of the wait structure
338 * anymore with the stupid W*() macros. I liked the union wait
339 * stuff much more. So, we devise our own macros... This is
340 * really ugly, use dramamine sparingly. You have been warned.
341 */
342 #ifndef W_STOPCODE
343 #define W_STOPCODE(sig) (((sig) << 8) | 0177)
344 #endif
345 #ifndef W_EXITCODE
346 #define W_EXITCODE(ret, sig) ((ret << 8) | (sig))
347 #endif
348
349 static int JobCondPassSig(ClientData, ClientData);
350 static void JobPassSig(int);
351 static void JobChildSig(int);
352 #ifdef USE_PGRP
353 static void JobContinueSig(int);
354 #endif
355 static int JobCmpPid(ClientData, ClientData);
356 static int JobPrintCommand(ClientData, ClientData);
357 static int JobSaveCommand(ClientData, ClientData);
358 static void JobClose(Job *);
359 #ifdef REMOTE
360 static int JobCmpRmtID(ClientData, ClientData);
361 # ifdef RMT_WILL_WATCH
362 static void JobLocalInput(int, Job *);
363 # endif
364 #else
365 static void JobFinish(Job *, int *);
366 static void JobExec(Job *, char **);
367 #endif
368 static void JobMakeArgv(Job *, char **);
369 static int JobRestart(Job *);
370 static int JobStart(GNode *, int, Job *);
371 static char *JobOutput(Job *, char *, char *, int);
372 static void JobDoOutput(Job *, Boolean);
373 static Shell *JobMatchShell(const char *);
374 static void JobInterrupt(int, int);
375 static void JobRestartJobs(void);
376 static void JobTokenAdd(void);
377 static void JobSigLock(sigset_t *);
378 static void JobSigUnlock(sigset_t *);
379 static void JobSigReset(void);
380
381
382
383 /*
384 * JobSigLock/JobSigUnlock
385 *
386 * Signal lock routines to get exclusive access. Currently used to
387 * protect `jobs' and `stoppedJobs' list manipulations.
388 */
389 static void JobSigLock(sigset_t *omaskp)
390 {
391 if (sigprocmask(SIG_BLOCK, &caught_signals, omaskp) != 0) {
392 Punt("JobSigLock: sigprocmask: %s", strerror(errno));
393 sigemptyset(omaskp);
394 }
395 }
396
397 static void JobSigUnlock(sigset_t *omaskp)
398 {
399 (void)sigprocmask(SIG_SETMASK, omaskp, NULL);
400 }
401
402 /*-
403 *-----------------------------------------------------------------------
404 * JobCondPassSig --
405 * Pass a signal to a job if the job is remote or if USE_PGRP
406 * is defined.
407 *
408 * Input:
409 * jobp Job to biff
410 * signop Signal to send it
411 *
412 * Results:
413 * === 0
414 *
415 * Side Effects:
416 * None, except the job may bite it.
417 *
418 *-----------------------------------------------------------------------
419 */
420 static int
421 JobCondPassSig(ClientData jobp, ClientData signop)
422 {
423 Job *job = (Job *)jobp;
424 int signo = *(int *)signop;
425 #ifdef RMT_WANTS_SIGNALS
426 if (job->flags & JOB_REMOTE) {
427 (void)Rmt_Signal(job, signo);
428 } else {
429 KILL(job->pid, signo);
430 }
431 #else
432 /*
433 * Assume that sending the signal to job->pid will signal any remote
434 * job as well.
435 */
436 if (DEBUG(JOB)) {
437 (void)fprintf(stdout,
438 "JobCondPassSig passing signal %d to child %d.\n",
439 signo, job->pid);
440 (void)fflush(stdout);
441 }
442 KILL(job->pid, signo);
443 #endif
444 return 0;
445 }
446
447 /*-
448 *-----------------------------------------------------------------------
449 * JobChldSig --
450 * SIGCHLD handler.
451 *
452 * Input:
453 * signo The signal number we've received
454 *
455 * Results:
456 * None.
457 *
458 * Side Effects:
459 * Sends a token on the child exit pipe to wake us up from
460 * select()/poll().
461 *
462 *-----------------------------------------------------------------------
463 */
464 static void
465 JobChildSig(int signo __unused)
466 {
467 write(exit_pipe[1], ".", 1);
468 }
469
470
471 #ifdef USE_PGRP
472 /*-
473 *-----------------------------------------------------------------------
474 * JobContinueSig --
475 * Resume all stopped jobs.
476 *
477 * Input:
478 * signo The signal number we've received
479 *
480 * Results:
481 * None.
482 *
483 * Side Effects:
484 * Jobs start running again.
485 *
486 *-----------------------------------------------------------------------
487 */
488 static void
489 JobContinueSig(int signo __unused)
490 {
491 JobRestartJobs();
492 }
493 #endif
494
495 /*-
496 *-----------------------------------------------------------------------
497 * JobPassSig --
498 * Pass a signal on to all remote jobs and to all local jobs if
499 * USE_PGRP is defined, then die ourselves.
500 *
501 * Input:
502 * signo The signal number we've received
503 *
504 * Results:
505 * None.
506 *
507 * Side Effects:
508 * We die by the same signal.
509 *
510 *-----------------------------------------------------------------------
511 */
512 static void
513 JobPassSig(int signo)
514 {
515 sigset_t nmask, omask;
516 struct sigaction act;
517 int sigcont;
518
519 if (DEBUG(JOB)) {
520 (void)fprintf(stdout, "JobPassSig(%d) called.\n", signo);
521 (void)fflush(stdout);
522 }
523 Lst_ForEach(jobs, JobCondPassSig, (ClientData) &signo);
524
525 /*
526 * Deal with proper cleanup based on the signal received. We only run
527 * the .INTERRUPT target if the signal was in fact an interrupt. The other
528 * three termination signals are more of a "get out *now*" command.
529 */
530 if (signo == SIGINT) {
531 JobInterrupt(TRUE, signo);
532 } else if ((signo == SIGHUP) || (signo == SIGTERM) || (signo == SIGQUIT)) {
533 JobInterrupt(FALSE, signo);
534 }
535
536 /*
537 * Leave gracefully if SIGQUIT, rather than core dumping.
538 */
539 if (signo == SIGQUIT) {
540 Finish(0);
541 }
542
543 if (signo == SIGTSTP) {
544 Job_CatchChildren(FALSE);
545 }
546 /*
547 * Send ourselves the signal now we've given the message to everyone else.
548 * Note we block everything else possible while we're getting the signal.
549 * This ensures that all our jobs get continued when we wake up before
550 * we take any other signal.
551 */
552 sigfillset(&nmask);
553 sigdelset(&nmask, signo);
554 (void)sigprocmask(SIG_SETMASK, &nmask, &omask);
555
556 act.sa_handler = SIG_DFL;
557 sigemptyset(&act.sa_mask);
558 act.sa_flags = 0;
559 (void)sigaction(signo, &act, NULL);
560
561 if (DEBUG(JOB)) {
562 (void)fprintf(stdout,
563 "JobPassSig passing signal %d to self.\n", signo);
564 (void)fflush(stdout);
565 }
566
567 (void)kill(getpid(), signo);
568 if (signo != SIGTSTP) {
569 sigcont = SIGCONT;
570 Lst_ForEach(jobs, JobCondPassSig, (ClientData) &sigcont);
571 }
572
573 /* Restore handler and signal mask */
574 act.sa_handler = JobPassSig;
575 (void)sigaction(signo, &act, NULL);
576 (void)sigprocmask(SIG_SETMASK, &omask, NULL);
577 }
578
579 /*-
580 *-----------------------------------------------------------------------
581 * JobCmpPid --
582 * Compare the pid of the job with the given pid and return 0 if they
583 * are equal. This function is called from Job_CatchChildren via
584 * Lst_Find to find the job descriptor of the finished job.
585 *
586 * Input:
587 * job job to examine
588 * pid process id desired
589 *
590 * Results:
591 * 0 if the pid's match
592 *
593 * Side Effects:
594 * None
595 *-----------------------------------------------------------------------
596 */
597 static int
598 JobCmpPid(ClientData job, ClientData pid)
599 {
600 return *(int *)pid - ((Job *)job)->pid;
601 }
602
603 #ifdef REMOTE
604 /*-
605 *-----------------------------------------------------------------------
606 * JobCmpRmtID --
607 * Compare the rmtID of the job with the given rmtID and return 0 if they
608 * are equal.
609 *
610 * Input:
611 * job job to examine
612 * rmtID remote id desired
613 *
614 * Results:
615 * 0 if the rmtID's match
616 *
617 * Side Effects:
618 * None.
619 *-----------------------------------------------------------------------
620 */
621 static int
622 JobCmpRmtID(ClientData job, ClientData rmtID)
623 {
624 return(*(int *)rmtID - ((Job *)job)->rmtID);
625 }
626 #endif
627
628 /*-
629 *-----------------------------------------------------------------------
630 * JobPrintCommand --
631 * Put out another command for the given job. If the command starts
632 * with an @ or a - we process it specially. In the former case,
633 * so long as the -s and -n flags weren't given to make, we stick
634 * a shell-specific echoOff command in the script. In the latter,
635 * we ignore errors for the entire job, unless the shell has error
636 * control.
637 * If the command is just "..." we take all future commands for this
638 * job to be commands to be executed once the entire graph has been
639 * made and return non-zero to signal that the end of the commands
640 * was reached. These commands are later attached to the postCommands
641 * node and executed by Job_End when all things are done.
642 * This function is called from JobStart via Lst_ForEach.
643 *
644 * Input:
645 * cmdp command string to print
646 * jobp job for which to print it
647 *
648 * Results:
649 * Always 0, unless the command was "..."
650 *
651 * Side Effects:
652 * If the command begins with a '-' and the shell has no error control,
653 * the JOB_IGNERR flag is set in the job descriptor.
654 * If the command is "..." and we're not ignoring such things,
655 * tailCmds is set to the successor node of the cmd.
656 * numCommands is incremented if the command is actually printed.
657 *-----------------------------------------------------------------------
658 */
659 static int
660 JobPrintCommand(ClientData cmdp, ClientData jobp)
661 {
662 Boolean noSpecials; /* true if we shouldn't worry about
663 * inserting special commands into
664 * the input stream. */
665 Boolean shutUp = FALSE; /* true if we put a no echo command
666 * into the command file */
667 Boolean errOff = FALSE; /* true if we turned error checking
668 * off before printing the command
669 * and need to turn it back on */
670 const char *cmdTemplate; /* Template to use when printing the
671 * command */
672 char *cmdStart; /* Start of expanded command */
673 char *escCmd = NULL; /* Command with quotes/backticks escaped */
674 char *cmd = (char *)cmdp;
675 Job *job = (Job *)jobp;
676 char *cp, *tmp;
677 int i, j;
678
679 noSpecials = NoExecute(job->node);
680
681 if (strcmp(cmd, "...") == 0) {
682 job->node->type |= OP_SAVE_CMDS;
683 if ((job->flags & JOB_IGNDOTS) == 0) {
684 job->tailCmds = Lst_Succ(Lst_Member(job->node->commands,
685 (ClientData)cmd));
686 return 1;
687 }
688 return 0;
689 }
690
691 #define DBPRINTF(fmt, arg) if (DEBUG(JOB)) { \
692 (void)fprintf(stdout, fmt, arg); \
693 (void)fflush(stdout); \
694 } \
695 (void)fprintf(job->cmdFILE, fmt, arg); \
696 (void)fflush(job->cmdFILE);
697
698 numCommands += 1;
699
700 cmdStart = cmd = Var_Subst(NULL, cmd, job->node, FALSE);
701
702 cmdTemplate = "%s\n";
703
704 /*
705 * Check for leading @' and -'s to control echoing and error checking.
706 */
707 while (*cmd == '@' || *cmd == '-' || (*cmd == '+')) {
708 switch (*cmd) {
709 case '@':
710 shutUp = TRUE;
711 break;
712 case '-':
713 errOff = TRUE;
714 break;
715 case '+':
716 if (noSpecials) {
717 /*
718 * We're not actually executing anything...
719 * but this one needs to be - use compat mode just for it.
720 */
721 CompatRunCommand(cmdp, (ClientData)job->node);
722 return 0;
723 }
724 break;
725 }
726 cmd++;
727 }
728
729 while (isspace((unsigned char) *cmd))
730 cmd++;
731
732 /*
733 * If the shell doesn't have error control the alternate echo'ing will
734 * be done (to avoid showing additional error checking code)
735 * and this will need the characters '$ ` \ "' escaped
736 */
737
738 if (!commandShell->hasErrCtl) {
739 /* Worst that could happen is every char needs escaping. */
740 escCmd = emalloc((strlen(cmd) * 2) + 1);
741 for (i = 0, j= 0; cmd[i] != '\0'; i++, j++) {
742 if (cmd[i] == '$' || cmd[i] == '`' || cmd[i] == '\\' ||
743 cmd[i] == '"')
744 escCmd[j++] = '\\';
745 escCmd[j] = cmd[i];
746 }
747 escCmd[j] = 0;
748 }
749
750 if (shutUp) {
751 if (!(job->flags & JOB_SILENT) && !noSpecials &&
752 commandShell->hasEchoCtl) {
753 DBPRINTF("%s\n", commandShell->echoOff);
754 } else {
755 if (commandShell->hasErrCtl)
756 shutUp = FALSE;
757 }
758 }
759
760 if (errOff) {
761 if ( !(job->flags & JOB_IGNERR) && !noSpecials) {
762 if (commandShell->hasErrCtl) {
763 /*
764 * we don't want the error-control commands showing
765 * up either, so we turn off echoing while executing
766 * them. We could put another field in the shell
767 * structure to tell JobDoOutput to look for this
768 * string too, but why make it any more complex than
769 * it already is?
770 */
771 if (!(job->flags & JOB_SILENT) && !shutUp &&
772 commandShell->hasEchoCtl) {
773 DBPRINTF("%s\n", commandShell->echoOff);
774 DBPRINTF("%s\n", commandShell->ignErr);
775 DBPRINTF("%s\n", commandShell->echoOn);
776 } else {
777 DBPRINTF("%s\n", commandShell->ignErr);
778 }
779 } else if (commandShell->ignErr &&
780 (*commandShell->ignErr != '\0'))
781 {
782 /*
783 * The shell has no error control, so we need to be
784 * weird to get it to ignore any errors from the command.
785 * If echoing is turned on, we turn it off and use the
786 * errCheck template to echo the command. Leave echoing
787 * off so the user doesn't see the weirdness we go through
788 * to ignore errors. Set cmdTemplate to use the weirdness
789 * instead of the simple "%s\n" template.
790 */
791 if (!(job->flags & JOB_SILENT) && !shutUp) {
792 if (commandShell->hasEchoCtl) {
793 DBPRINTF("%s\n", commandShell->echoOff);
794 }
795 DBPRINTF(commandShell->errCheck, escCmd);
796 shutUp = TRUE;
797 } else {
798 if (!shutUp) {
799 DBPRINTF(commandShell->errCheck, escCmd);
800 }
801 }
802 cmdTemplate = commandShell->ignErr;
803 /*
804 * The error ignoration (hee hee) is already taken care
805 * of by the ignErr template, so pretend error checking
806 * is still on.
807 */
808 errOff = FALSE;
809 } else {
810 errOff = FALSE;
811 }
812 } else {
813 errOff = FALSE;
814 }
815 } else {
816
817 /*
818 * If errors are being checked and the shell doesn't have error control
819 * but does supply an errOut template, then setup commands to run
820 * through it.
821 */
822
823 if (!commandShell->hasErrCtl && commandShell->errOut &&
824 (*commandShell->errOut != '\0')) {
825 if (!(job->flags & JOB_SILENT) && !shutUp) {
826 if (commandShell->hasEchoCtl) {
827 DBPRINTF("%s\n", commandShell->echoOff);
828 }
829 DBPRINTF(commandShell->errCheck, escCmd);
830 shutUp = TRUE;
831 }
832 /* If it's a comment line or blank, treat as an ignored error */
833 if ((escCmd[0] == commandShell->commentChar) ||
834 (escCmd[0] == 0))
835 cmdTemplate = commandShell->ignErr;
836 else
837 cmdTemplate = commandShell->errOut;
838 errOff = FALSE;
839 }
840 }
841
842 if (DEBUG(SHELL) && strcmp(shellName, "sh") == 0 &&
843 (job->flags & JOB_TRACED) == 0) {
844 DBPRINTF("set -%s\n", "x");
845 job->flags |= JOB_TRACED;
846 }
847
848 if ((cp = Check_Cwd_Cmd(cmd)) != NULL) {
849 DBPRINTF("test -d %s && ", cp);
850 DBPRINTF("cd %s\n", cp);
851 }
852
853 DBPRINTF(cmdTemplate, cmd);
854 free(cmdStart);
855 if (escCmd)
856 free(escCmd);
857 if (errOff) {
858 /*
859 * If echoing is already off, there's no point in issuing the
860 * echoOff command. Otherwise we issue it and pretend it was on
861 * for the whole command...
862 */
863 if (!shutUp && !(job->flags & JOB_SILENT) && commandShell->hasEchoCtl){
864 DBPRINTF("%s\n", commandShell->echoOff);
865 shutUp = TRUE;
866 }
867 DBPRINTF("%s\n", commandShell->errCheck);
868 }
869 if (shutUp && commandShell->hasEchoCtl) {
870 DBPRINTF("%s\n", commandShell->echoOn);
871 }
872 if (cp != NULL) {
873 DBPRINTF("test -d %s && ", cp);
874 DBPRINTF("cd %s\n", Var_Value(".OBJDIR", VAR_GLOBAL, &tmp));
875 }
876 return 0;
877 }
878
879 /*-
880 *-----------------------------------------------------------------------
881 * JobSaveCommand --
882 * Save a command to be executed when everything else is done.
883 * Callback function for JobFinish...
884 *
885 * Results:
886 * Always returns 0
887 *
888 * Side Effects:
889 * The command is tacked onto the end of postCommands's commands list.
890 *
891 *-----------------------------------------------------------------------
892 */
893 static int
894 JobSaveCommand(ClientData cmd, ClientData gn)
895 {
896 cmd = (ClientData)Var_Subst(NULL, (char *)cmd, (GNode *)gn, FALSE);
897 (void)Lst_AtEnd(postCommands->commands, cmd);
898 return(0);
899 }
900
901
902 /*-
903 *-----------------------------------------------------------------------
904 * JobClose --
905 * Called to close both input and output pipes when a job is finished.
906 *
907 * Results:
908 * Nada
909 *
910 * Side Effects:
911 * The file descriptors associated with the job are closed.
912 *
913 *-----------------------------------------------------------------------
914 */
915 static void
916 JobClose(Job *job)
917 {
918 if (usePipes && (job->flags & JOB_FIRST)) {
919 #ifdef RMT_WILL_WATCH
920 Rmt_Ignore(job->inPipe);
921 #else
922 clearfd(job);
923 #endif
924 if (job->outPipe != job->inPipe) {
925 (void)close(job->outPipe);
926 }
927 JobDoOutput(job, TRUE);
928 (void)close(job->inPipe);
929 } else {
930 (void)close(job->outFd);
931 JobDoOutput(job, TRUE);
932 }
933 }
934
935 /*-
936 *-----------------------------------------------------------------------
937 * JobFinish --
938 * Do final processing for the given job including updating
939 * parents and starting new jobs as available/necessary. Note
940 * that we pay no attention to the JOB_IGNERR flag here.
941 * This is because when we're called because of a noexecute flag
942 * or something, jstat.w_status is 0 and when called from
943 * Job_CatchChildren, the status is zeroed if it s/b ignored.
944 *
945 * Input:
946 * job job to finish
947 * status sub-why job went away
948 *
949 * Results:
950 * None
951 *
952 * Side Effects:
953 * Final commands for the job are placed on postCommands.
954 *
955 * If we got an error and are aborting (aborting == ABORT_ERROR) and
956 * the job list is now empty, we are done for the day.
957 * If we recognized an error (errors !=0), we set the aborting flag
958 * to ABORT_ERROR so no more jobs will be started.
959 *-----------------------------------------------------------------------
960 */
961 /*ARGSUSED*/
962 static void
963 JobFinish(Job *job, int *status)
964 {
965 Boolean done;
966
967 if ((WIFEXITED(*status) &&
968 (((WEXITSTATUS(*status) != 0) && !(job->flags & JOB_IGNERR)))) ||
969 WIFSIGNALED(*status))
970 {
971 /*
972 * If it exited non-zero and either we're doing things our
973 * way or we're not ignoring errors, the job is finished.
974 * Similarly, if the shell died because of a signal
975 * the job is also finished. In these
976 * cases, finish out the job's output before printing the exit
977 * status...
978 */
979 #ifdef REMOTE
980 KILL(job->pid, SIGCONT);
981 #endif
982 JobClose(job);
983 if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
984 (void)fclose(job->cmdFILE);
985 job->cmdFILE = NULL;
986 }
987 done = TRUE;
988 #ifdef REMOTE
989 if (job->flags & JOB_REMOTE)
990 Rmt_Done(job->rmtID, job->node);
991 #endif
992 } else if (WIFEXITED(*status)) {
993 /*
994 * Deal with ignored errors in -B mode. We need to print a message
995 * telling of the ignored error as well as setting status.w_status
996 * to 0 so the next command gets run. To do this, we set done to be
997 * TRUE if in -B mode and the job exited non-zero.
998 */
999 done = WEXITSTATUS(*status) != 0;
1000 /*
1001 * Old comment said: "Note we don't
1002 * want to close down any of the streams until we know we're at the
1003 * end."
1004 * But we do. Otherwise when are we going to print the rest of the
1005 * stuff?
1006 */
1007 JobClose(job);
1008 #ifdef REMOTE
1009 if (job->flags & JOB_REMOTE)
1010 Rmt_Done(job->rmtID, job->node);
1011 #endif /* REMOTE */
1012 } else {
1013 /*
1014 * No need to close things down or anything.
1015 */
1016 done = FALSE;
1017 }
1018
1019 if (done ||
1020 WIFSTOPPED(*status) ||
1021 (WIFSIGNALED(*status) && (WTERMSIG(*status) == SIGCONT)))
1022 {
1023 FILE *out;
1024
1025 if (compatMake && !usePipes && (job->flags & JOB_IGNERR)) {
1026 /*
1027 * If output is going to a file and this job is ignoring
1028 * errors, arrange to have the exit status sent to the
1029 * output file as well.
1030 */
1031 out = fdopen(job->outFd, "w");
1032 if (out == NULL)
1033 Punt("Cannot fdopen");
1034 } else {
1035 out = stdout;
1036 }
1037
1038 if (WIFEXITED(*status)) {
1039 if (DEBUG(JOB)) {
1040 (void)fprintf(stdout, "Process %d [%s] exited.\n",
1041 job->pid, job->node->name);
1042 (void)fflush(stdout);
1043 }
1044 if (WEXITSTATUS(*status) != 0) {
1045 if (usePipes && job->node != lastNode) {
1046 MESSAGE(out, job->node);
1047 lastNode = job->node;
1048 }
1049 (void)fprintf(out, "*** [%s] Error code %d%s\n",
1050 job->node->name,
1051 WEXITSTATUS(*status),
1052 (job->flags & JOB_IGNERR) ? "(ignored)" : "");
1053
1054 if (job->flags & JOB_IGNERR) {
1055 *status = 0;
1056 }
1057 } else if (DEBUG(JOB)) {
1058 if (usePipes && job->node != lastNode) {
1059 MESSAGE(out, job->node);
1060 lastNode = job->node;
1061 }
1062 (void)fprintf(out, "*** [%s] Completed successfully\n",
1063 job->node->name);
1064 }
1065 } else if (WIFSTOPPED(*status) && WSTOPSIG(*status) != SIGCONT) {
1066 if (DEBUG(JOB)) {
1067 (void)fprintf(stdout, "Process %d (%s) stopped.\n",
1068 job->pid, job->node->name);
1069 (void)fflush(stdout);
1070 }
1071 if (usePipes && job->node != lastNode) {
1072 MESSAGE(out, job->node);
1073 lastNode = job->node;
1074 }
1075 if (!(job->flags & JOB_REMIGRATE)) {
1076 switch (WSTOPSIG(*status)) {
1077 case SIGTSTP:
1078 (void)fprintf(out, "*** [%s] Suspended\n",
1079 job->node->name);
1080 break;
1081 case SIGSTOP:
1082 (void)fprintf(out, "*** [%s] Stopped\n",
1083 job->node->name);
1084 break;
1085 default:
1086 (void)fprintf(out, "*** [%s] Stopped -- signal %d\n",
1087 job->node->name, WSTOPSIG(*status));
1088 }
1089 }
1090 job->flags |= JOB_RESUME;
1091 (void)Lst_AtEnd(stoppedJobs, (ClientData)job);
1092 #ifdef REMOTE
1093 if (job->flags & JOB_REMIGRATE)
1094 JobRestart(job);
1095 #endif
1096 (void)fflush(out);
1097 return;
1098 } else if (WIFSTOPPED(*status) && WSTOPSIG(*status) == SIGCONT) {
1099 /*
1100 * If the beastie has continued, shift the Job from the stopped
1101 * list to the running one (or re-stop it if concurrency is
1102 * exceeded) and go and get another child.
1103 */
1104 if (job->flags & (JOB_RESUME|JOB_REMIGRATE|JOB_RESTART)) {
1105 if (usePipes && job->node != lastNode) {
1106 MESSAGE(out, job->node);
1107 lastNode = job->node;
1108 }
1109 (void)fprintf(out, "*** [%s] Continued\n", job->node->name);
1110 }
1111 if (!(job->flags & JOB_CONTINUING)) {
1112 if (DEBUG(JOB)) {
1113 (void)fprintf(stdout,
1114 "Warning: process %d [%s] was not continuing.\n",
1115 job->pid, job->node->name);
1116 (void)fflush(stdout);
1117 }
1118 #ifdef notdef
1119 /*
1120 * We don't really want to restart a job from scratch just
1121 * because it continued, especially not without killing the
1122 * continuing process! That's why this is ifdef'ed out.
1123 * FD - 9/17/90
1124 */
1125 JobRestart(job);
1126 #endif
1127 }
1128 job->flags &= ~JOB_CONTINUING;
1129 Lst_AtEnd(jobs, (ClientData)job);
1130 nJobs += 1;
1131 if (!(job->flags & JOB_REMOTE)) {
1132 if (DEBUG(JOB)) {
1133 (void)fprintf(stdout,
1134 "Process %d is continuing locally.\n",
1135 job->pid);
1136 (void)fflush(stdout);
1137 }
1138 nLocal += 1;
1139 }
1140 (void)fflush(out);
1141 return;
1142 } else {
1143 if (usePipes && job->node != lastNode) {
1144 MESSAGE(out, job->node);
1145 lastNode = job->node;
1146 }
1147 (void)fprintf(out, "*** [%s] Signal %d\n",
1148 job->node->name, WTERMSIG(*status));
1149 }
1150
1151 (void)fflush(out);
1152 }
1153
1154 /*
1155 * Now handle the -B-mode stuff. If the beast still isn't finished,
1156 * try and restart the job on the next command. If JobStart says it's
1157 * ok, it's ok. If there's an error, this puppy is done.
1158 */
1159 if (compatMake && (WIFEXITED(*status) &&
1160 !Lst_IsAtEnd(job->node->commands))) {
1161 switch (JobStart(job->node, job->flags & JOB_IGNDOTS, job)) {
1162 case JOB_RUNNING:
1163 done = FALSE;
1164 break;
1165 case JOB_ERROR:
1166 done = TRUE;
1167 *status = W_EXITCODE(1, 0);
1168 break;
1169 case JOB_FINISHED:
1170 /*
1171 * If we got back a JOB_FINISHED code, JobStart has already
1172 * called Make_Update and freed the job descriptor. We set
1173 * done to false here to avoid fake cycles and double frees.
1174 * JobStart needs to do the update so we can proceed up the
1175 * graph when given the -n flag..
1176 */
1177 done = FALSE;
1178 break;
1179 }
1180 } else {
1181 done = TRUE;
1182 }
1183
1184 if (done) {
1185 Trace_Log(JOBEND, job);
1186 if (!compatMake && !(job->flags & JOB_SPECIAL)) {
1187 if ((*status != 0) ||
1188 (aborting == ABORT_ERROR) ||
1189 (aborting == ABORT_INTERRUPT))
1190 Job_TokenReturn();
1191 }
1192
1193 }
1194
1195 if (done &&
1196 (aborting != ABORT_ERROR) &&
1197 (aborting != ABORT_INTERRUPT) &&
1198 (*status == 0))
1199 {
1200 /*
1201 * As long as we aren't aborting and the job didn't return a non-zero
1202 * status that we shouldn't ignore, we call Make_Update to update
1203 * the parents. In addition, any saved commands for the node are placed
1204 * on the .END target.
1205 */
1206 if (job->tailCmds != NILLNODE) {
1207 Lst_ForEachFrom(job->node->commands, job->tailCmds,
1208 JobSaveCommand,
1209 (ClientData)job->node);
1210 }
1211 job->node->made = MADE;
1212 if (!(job->flags & JOB_SPECIAL))
1213 Job_TokenReturn();
1214 Make_Update(job->node);
1215 free(job);
1216 } else if (*status != 0) {
1217 errors += 1;
1218 free(job);
1219 }
1220 JobRestartJobs();
1221
1222 /*
1223 * Set aborting if any error.
1224 */
1225 if (errors && !keepgoing && (aborting != ABORT_INTERRUPT)) {
1226 /*
1227 * If we found any errors in this batch of children and the -k flag
1228 * wasn't given, we set the aborting flag so no more jobs get
1229 * started.
1230 */
1231 aborting = ABORT_ERROR;
1232 }
1233
1234 if ((aborting == ABORT_ERROR) && Job_Empty()) {
1235 /*
1236 * If we are aborting and the job table is now empty, we finish.
1237 */
1238 Finish(errors);
1239 }
1240 }
1241
1242 /*-
1243 *-----------------------------------------------------------------------
1244 * Job_Touch --
1245 * Touch the given target. Called by JobStart when the -t flag was
1246 * given
1247 *
1248 * Input:
1249 * gn the node of the file to touch
1250 * silent TRUE if should not print message
1251 *
1252 * Results:
1253 * None
1254 *
1255 * Side Effects:
1256 * The data modification of the file is changed. In addition, if the
1257 * file did not exist, it is created.
1258 *-----------------------------------------------------------------------
1259 */
1260 void
1261 Job_Touch(GNode *gn, Boolean silent)
1262 {
1263 int streamID; /* ID of stream opened to do the touch */
1264 struct utimbuf times; /* Times for utime() call */
1265
1266 if (gn->type & (OP_JOIN|OP_USE|OP_USEBEFORE|OP_EXEC|OP_OPTIONAL|OP_PHONY)) {
1267 /*
1268 * .JOIN, .USE, .ZEROTIME and .OPTIONAL targets are "virtual" targets
1269 * and, as such, shouldn't really be created.
1270 */
1271 return;
1272 }
1273
1274 if (!silent || NoExecute(gn)) {
1275 (void)fprintf(stdout, "touch %s\n", gn->name);
1276 (void)fflush(stdout);
1277 }
1278
1279 if (NoExecute(gn)) {
1280 return;
1281 }
1282
1283 if (gn->type & OP_ARCHV) {
1284 Arch_Touch(gn);
1285 } else if (gn->type & OP_LIB) {
1286 Arch_TouchLib(gn);
1287 } else {
1288 char *file = gn->path ? gn->path : gn->name;
1289
1290 times.actime = times.modtime = now;
1291 if (utime(file, ×) < 0){
1292 streamID = open(file, O_RDWR | O_CREAT, 0666);
1293
1294 if (streamID >= 0) {
1295 char c;
1296
1297 /*
1298 * Read and write a byte to the file to change the
1299 * modification time, then close the file.
1300 */
1301 if (read(streamID, &c, 1) == 1) {
1302 (void)lseek(streamID, (off_t)0, SEEK_SET);
1303 (void)write(streamID, &c, 1);
1304 }
1305
1306 (void)close(streamID);
1307 } else {
1308 (void)fprintf(stdout, "*** couldn't touch %s: %s",
1309 file, strerror(errno));
1310 (void)fflush(stdout);
1311 }
1312 }
1313 }
1314 }
1315
1316 /*-
1317 *-----------------------------------------------------------------------
1318 * Job_CheckCommands --
1319 * Make sure the given node has all the commands it needs.
1320 *
1321 * Input:
1322 * gn The target whose commands need verifying
1323 * abortProc Function to abort with message
1324 *
1325 * Results:
1326 * TRUE if the commands list is/was ok.
1327 *
1328 * Side Effects:
1329 * The node will have commands from the .DEFAULT rule added to it
1330 * if it needs them.
1331 *-----------------------------------------------------------------------
1332 */
1333 Boolean
1334 Job_CheckCommands(GNode *gn, void (*abortProc)(const char *, ...))
1335 {
1336 if (OP_NOP(gn->type) && Lst_IsEmpty(gn->commands) &&
1337 ((gn->type & OP_LIB) == 0 || Lst_IsEmpty(gn->children))) {
1338 /*
1339 * No commands. Look for .DEFAULT rule from which we might infer
1340 * commands
1341 */
1342 if ((DEFAULT != NILGNODE) && !Lst_IsEmpty(DEFAULT->commands) &&
1343 (gn->type & OP_SPECIAL) == 0) {
1344 char *p1;
1345 /*
1346 * Make only looks for a .DEFAULT if the node was never the
1347 * target of an operator, so that's what we do too. If
1348 * a .DEFAULT was given, we substitute its commands for gn's
1349 * commands and set the IMPSRC variable to be the target's name
1350 * The DEFAULT node acts like a transformation rule, in that
1351 * gn also inherits any attributes or sources attached to
1352 * .DEFAULT itself.
1353 */
1354 Make_HandleUse(DEFAULT, gn);
1355 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), gn, 0);
1356 if (p1)
1357 free(p1);
1358 } else if (Dir_MTime(gn) == 0 && (gn->type & OP_SPECIAL) == 0) {
1359 /*
1360 * The node wasn't the target of an operator we have no .DEFAULT
1361 * rule to go on and the target doesn't already exist. There's
1362 * nothing more we can do for this branch. If the -k flag wasn't
1363 * given, we stop in our tracks, otherwise we just don't update
1364 * this node's parents so they never get examined.
1365 */
1366 static const char msg[] = ": don't know how to make";
1367
1368 if (gn->type & OP_OPTIONAL) {
1369 (void)fprintf(stdout, "%s%s %s(ignored)\n", progname,
1370 msg, gn->name);
1371 (void)fflush(stdout);
1372 } else if (keepgoing) {
1373 (void)fprintf(stdout, "%s%s %s(continuing)\n", progname,
1374 msg, gn->name);
1375 (void)fflush(stdout);
1376 return FALSE;
1377 } else {
1378 (*abortProc)("%s%s %s. Stop", progname, msg, gn->name);
1379 return FALSE;
1380 }
1381 }
1382 }
1383 return TRUE;
1384 }
1385 #ifdef RMT_WILL_WATCH
1386 /*-
1387 *-----------------------------------------------------------------------
1388 * JobLocalInput --
1389 * Handle a pipe becoming readable. Callback function for Rmt_Watch
1390 *
1391 * Input:
1392 * stream Stream that's ready (ignored)
1393 * job Job to which the stream belongs
1394 *
1395 * Results:
1396 * None
1397 *
1398 * Side Effects:
1399 * JobDoOutput is called.
1400 *
1401 *-----------------------------------------------------------------------
1402 */
1403 /*ARGSUSED*/
1404 static void
1405 JobLocalInput(int stream, Job *job)
1406 {
1407 JobDoOutput(job, FALSE);
1408 }
1409 #endif /* RMT_WILL_WATCH */
1410
1411 /*-
1412 *-----------------------------------------------------------------------
1413 * JobExec --
1414 * Execute the shell for the given job. Called from JobStart and
1415 * JobRestart.
1416 *
1417 * Input:
1418 * job Job to execute
1419 *
1420 * Results:
1421 * None.
1422 *
1423 * Side Effects:
1424 * A shell is executed, outputs is altered and the Job structure added
1425 * to the job table.
1426 *
1427 *-----------------------------------------------------------------------
1428 */
1429 static void
1430 JobExec(Job *job, char **argv)
1431 {
1432 int cpid; /* ID of new child */
1433 sigset_t mask;
1434
1435 job->flags &= ~JOB_TRACED;
1436
1437 if (DEBUG(JOB)) {
1438 int i;
1439
1440 (void)fprintf(stdout, "Running %s %sly\n", job->node->name,
1441 job->flags&JOB_REMOTE?"remote":"local");
1442 (void)fprintf(stdout, "\tCommand: ");
1443 for (i = 0; argv[i] != NULL; i++) {
1444 (void)fprintf(stdout, "%s ", argv[i]);
1445 }
1446 (void)fprintf(stdout, "\n");
1447 (void)fflush(stdout);
1448 }
1449
1450 /*
1451 * Some jobs produce no output and it's disconcerting to have
1452 * no feedback of their running (since they produce no output, the
1453 * banner with their name in it never appears). This is an attempt to
1454 * provide that feedback, even if nothing follows it.
1455 */
1456 if ((lastNode != job->node) && (job->flags & JOB_FIRST) &&
1457 !(job->flags & JOB_SILENT)) {
1458 MESSAGE(stdout, job->node);
1459 lastNode = job->node;
1460 }
1461
1462 #ifdef RMT_NO_EXEC
1463 if (job->flags & JOB_REMOTE) {
1464 goto jobExecFinish;
1465 }
1466 #endif /* RMT_NO_EXEC */
1467
1468 /* No interruptions until this job is on the `jobs' list */
1469 JobSigLock(&mask);
1470
1471 if ((cpid = vfork()) == -1) {
1472 Punt("Cannot vfork: %s", strerror(errno));
1473 } else if (cpid == 0) {
1474
1475 /*
1476 * Reset all signal handlers; this is necessary because we also
1477 * need to unblock signals before we exec(2).
1478 */
1479 JobSigReset();
1480
1481 /* Now unblock signals */
1482 sigemptyset(&mask);
1483 JobSigUnlock(&mask);
1484
1485 /*
1486 * Must duplicate the input stream down to the child's input and
1487 * reset it to the beginning (again). Since the stream was marked
1488 * close-on-exec, we must clear that bit in the new input.
1489 */
1490 if (dup2(FILENO(job->cmdFILE), 0) == -1) {
1491 execError("dup2", "job->cmdFILE");
1492 _exit(1);
1493 }
1494 (void)fcntl(0, F_SETFD, 0);
1495 (void)lseek(0, (off_t)0, SEEK_SET);
1496
1497 if (job->node->type & OP_MAKE) {
1498 /*
1499 * Pass job token pipe to submakes.
1500 */
1501 fcntl(job_pipe[0], F_SETFD, 0);
1502 fcntl(job_pipe[1], F_SETFD, 0);
1503 }
1504
1505 if (usePipes) {
1506 /*
1507 * Set up the child's output to be routed through the pipe
1508 * we've created for it.
1509 */
1510 if (dup2(job->outPipe, 1) == -1) {
1511 execError("dup2", "job->outPipe");
1512 _exit(1);
1513 }
1514 } else {
1515 /*
1516 * We're capturing output in a file, so we duplicate the
1517 * descriptor to the temporary file into the standard
1518 * output.
1519 */
1520 if (dup2(job->outFd, 1) == -1) {
1521 execError("dup2", "job->outFd");
1522 _exit(1);
1523 }
1524 }
1525 /*
1526 * The output channels are marked close on exec. This bit was
1527 * duplicated by the dup2(on some systems), so we have to clear
1528 * it before routing the shell's error output to the same place as
1529 * its standard output.
1530 */
1531 (void)fcntl(1, F_SETFD, 0);
1532 if (dup2(1, 2) == -1) {
1533 execError("dup2", "1, 2");
1534 _exit(1);
1535 }
1536
1537 #ifdef USE_PGRP
1538 /*
1539 * We want to switch the child into a different process family so
1540 * we can kill it and all its descendants in one fell swoop,
1541 * by killing its process family, but not commit suicide.
1542 */
1543 # if defined(SYSV)
1544 (void)setsid();
1545 # else
1546 (void)setpgid(0, getpid());
1547 # endif
1548 #endif /* USE_PGRP */
1549
1550 #ifdef REMOTE
1551 if (job->flags & JOB_REMOTE) {
1552 Rmt_Exec(shellPath, argv, FALSE);
1553 } else
1554 #endif /* REMOTE */
1555 {
1556 (void)execv(shellPath, argv);
1557 execError("exec", shellPath);
1558 }
1559 _exit(1);
1560 } else {
1561 job->pid = cpid;
1562
1563 Trace_Log(JOBSTART, job);
1564
1565 if (usePipes && (job->flags & JOB_FIRST)) {
1566 /*
1567 * The first time a job is run for a node, we set the current
1568 * position in the buffer to the beginning and mark another
1569 * stream to watch in the outputs mask
1570 */
1571 job->curPos = 0;
1572
1573 #ifdef RMT_WILL_WATCH
1574 Rmt_Watch(job->inPipe, JobLocalInput, job);
1575 #else
1576 watchfd(job);
1577 #endif /* RMT_WILL_WATCH */
1578 }
1579
1580 if (job->flags & JOB_REMOTE) {
1581 #ifndef REMOTE
1582 job->rmtID = 0;
1583 #else
1584 job->rmtID = Rmt_LastID(job->pid);
1585 #endif /* REMOTE */
1586 } else {
1587 nLocal += 1;
1588 /*
1589 * XXX: Used to not happen if REMOTE. Why?
1590 */
1591 if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
1592 (void)fclose(job->cmdFILE);
1593 job->cmdFILE = NULL;
1594 }
1595 }
1596 }
1597
1598 #ifdef RMT_NO_EXEC
1599 jobExecFinish:
1600 #endif
1601 /*
1602 * Now the job is actually running, add it to the table.
1603 */
1604 if (DEBUG(JOB)) {
1605 printf("JobExec(%s): pid %d added to jobs table\n",
1606 job->node->name, job->pid);
1607 }
1608 nJobs += 1;
1609 (void)Lst_AtEnd(jobs, (ClientData)job);
1610 JobSigUnlock(&mask);
1611 }
1612
1613 /*-
1614 *-----------------------------------------------------------------------
1615 * JobMakeArgv --
1616 * Create the argv needed to execute the shell for a given job.
1617 *
1618 *
1619 * Results:
1620 *
1621 * Side Effects:
1622 *
1623 *-----------------------------------------------------------------------
1624 */
1625 static void
1626 JobMakeArgv(Job *job, char **argv)
1627 {
1628 int argc;
1629 static char args[10]; /* For merged arguments */
1630
1631 argv[0] = UNCONST(shellName);
1632 argc = 1;
1633
1634 if ((commandShell->exit && (*commandShell->exit != '-')) ||
1635 (commandShell->echo && (*commandShell->echo != '-')))
1636 {
1637 /*
1638 * At least one of the flags doesn't have a minus before it, so
1639 * merge them together. Have to do this because the *(&(@*#*&#$#
1640 * Bourne shell thinks its second argument is a file to source.
1641 * Grrrr. Note the ten-character limitation on the combined arguments.
1642 */
1643 (void)snprintf(args, sizeof(args), "-%s%s",
1644 ((job->flags & JOB_IGNERR) ? "" :
1645 (commandShell->exit ? commandShell->exit : "")),
1646 ((job->flags & JOB_SILENT) ? "" :
1647 (commandShell->echo ? commandShell->echo : "")));
1648
1649 if (args[1]) {
1650 argv[argc] = args;
1651 argc++;
1652 }
1653 } else {
1654 if (!(job->flags & JOB_IGNERR) && commandShell->exit) {
1655 argv[argc] = UNCONST(commandShell->exit);
1656 argc++;
1657 }
1658 if (!(job->flags & JOB_SILENT) && commandShell->echo) {
1659 argv[argc] = UNCONST(commandShell->echo);
1660 argc++;
1661 }
1662 }
1663 argv[argc] = NULL;
1664 }
1665
1666 /*-
1667 *-----------------------------------------------------------------------
1668 * JobRestart --
1669 * Restart a job that stopped for some reason.
1670 *
1671 * Input:
1672 * job Job to restart
1673 *
1674 * Results:
1675 * 1 if max number of running jobs has been reached, 0 otherwise.
1676 *
1677 *-----------------------------------------------------------------------
1678 */
1679 static int
1680 JobRestart(Job *job)
1681 {
1682 #ifdef REMOTE
1683 int host;
1684 #endif
1685
1686 if (job->flags & JOB_REMIGRATE) {
1687 if (
1688 #ifdef REMOTE
1689 verboseRemigrates ||
1690 #endif
1691 DEBUG(JOB)) {
1692 (void)fprintf(stdout, "*** remigrating %x(%s)\n",
1693 job->pid, job->node->name);
1694 (void)fflush(stdout);
1695 }
1696
1697 #ifdef REMOTE
1698 if (!Rmt_ReExport(job->pid, job->node, &host)) {
1699 if (verboseRemigrates || DEBUG(JOB)) {
1700 (void)fprintf(stdout, "*** couldn't migrate...\n");
1701 (void)fflush(stdout);
1702 }
1703 #endif
1704 if (nLocal != maxLocal) {
1705 /*
1706 * Job cannot be remigrated, but there's room on the local
1707 * machine, so resume the job and note that another
1708 * local job has started.
1709 */
1710 if (
1711 #ifdef REMOTE
1712 verboseRemigrates ||
1713 #endif
1714 DEBUG(JOB)) {
1715 (void)fprintf(stdout, "*** resuming on local machine\n");
1716 (void)fflush(stdout);
1717 }
1718 KILL(job->pid, SIGCONT);
1719 nLocal +=1;
1720 #ifdef REMOTE
1721 job->flags &= ~(JOB_REMIGRATE|JOB_RESUME|JOB_REMOTE);
1722 job->flags |= JOB_CONTINUING;
1723 #else
1724 job->flags &= ~(JOB_REMIGRATE|JOB_RESUME);
1725 #endif
1726 } else {
1727 /*
1728 * Job cannot be restarted. Mark the table as full and
1729 * place the job back on the list of stopped jobs.
1730 */
1731 if (
1732 #ifdef REMOTE
1733 verboseRemigrates ||
1734 #endif
1735 DEBUG(JOB)) {
1736 (void)fprintf(stdout, "*** holding\n");
1737 (void)fflush(stdout);
1738 }
1739 (void)Lst_AtFront(stoppedJobs, (ClientData)job);
1740 return 1;
1741 }
1742 #ifdef REMOTE
1743 } else {
1744 /*
1745 * Clear out the remigrate and resume flags. Set the continuing
1746 * flag so we know later on that the process isn't exiting just
1747 * because of a signal.
1748 */
1749 job->flags &= ~(JOB_REMIGRATE|JOB_RESUME);
1750 job->flags |= JOB_CONTINUING;
1751 job->rmtID = host;
1752 }
1753 #endif
1754
1755 (void)Lst_AtEnd(jobs, (ClientData)job);
1756 nJobs += 1;
1757 } else if (job->flags & JOB_RESTART) {
1758 /*
1759 * Set up the control arguments to the shell. This is based on the
1760 * flags set earlier for this job. If the JOB_IGNERR flag is clear,
1761 * the 'exit' flag of the commandShell is used to cause it to exit
1762 * upon receiving an error. If the JOB_SILENT flag is clear, the
1763 * 'echo' flag of the commandShell is used to get it to start echoing
1764 * as soon as it starts processing commands.
1765 */
1766 char *argv[10];
1767
1768 JobMakeArgv(job, argv);
1769
1770 if (DEBUG(JOB)) {
1771 (void)fprintf(stdout, "Restarting %s...", job->node->name);
1772 (void)fflush(stdout);
1773 }
1774 #ifdef REMOTE
1775 if ((job->node->type & OP_NOEXPORT) ||
1776 (nLocal < maxLocal && runLocalFirst)
1777 # ifdef RMT_NO_EXEC
1778 || !Rmt_Export(shellPath, argv, job)
1779 # else
1780 || !Rmt_Begin(shellPath, argv, job->node)
1781 # endif
1782 )
1783 #endif
1784 {
1785 if (((nLocal >= maxLocal) && !(job->flags & JOB_SPECIAL))) {
1786 /*
1787 * Can't be exported and not allowed to run locally -- put it
1788 * back on the hold queue and mark the table full
1789 */
1790 if (DEBUG(JOB)) {
1791 (void)fprintf(stdout, "holding\n");
1792 (void)fflush(stdout);
1793 }
1794 (void)Lst_AtFront(stoppedJobs, (ClientData)job);
1795 return 1;
1796 } else {
1797 /*
1798 * Job may be run locally.
1799 */
1800 if (DEBUG(JOB)) {
1801 (void)fprintf(stdout, "running locally\n");
1802 (void)fflush(stdout);
1803 }
1804 job->flags &= ~JOB_REMOTE;
1805 }
1806 }
1807 #ifdef REMOTE
1808 else {
1809 /*
1810 * Can be exported. Hooray!
1811 */
1812 if (DEBUG(JOB)) {
1813 (void)fprintf(stdout, "exporting\n");
1814 (void)fflush(stdout);
1815 }
1816 job->flags |= JOB_REMOTE;
1817 }
1818 #endif
1819 JobExec(job, argv);
1820 } else {
1821 /*
1822 * The job has stopped and needs to be restarted. Why it stopped,
1823 * we don't know...
1824 */
1825 if (DEBUG(JOB)) {
1826 (void)fprintf(stdout, "Resuming %s...", job->node->name);
1827 (void)fflush(stdout);
1828 }
1829 if ((nJobs != maxJobs) &&
1830 ((job->flags & JOB_REMOTE) ||
1831 (nLocal < maxLocal) ||
1832 ((maxLocal == 0) &&
1833 ((job->flags & JOB_SPECIAL)
1834 #ifdef REMOTE
1835 && (job->node->type & OP_NOEXPORT)
1836 #endif
1837 ))))
1838 {
1839 /*
1840 * If the job is remote, it's ok to resume it as long as the
1841 * maximum concurrency won't be exceeded. If it's local and
1842 * we haven't reached the local concurrency limit already (or the
1843 * job must be run locally and maxLocal is 0), it's also ok to
1844 * resume it.
1845 */
1846 Boolean error;
1847 int status;
1848
1849 #ifdef RMT_WANTS_SIGNALS
1850 if (job->flags & JOB_REMOTE) {
1851 error = !Rmt_Signal(job, SIGCONT);
1852 } else
1853 #endif /* RMT_WANTS_SIGNALS */
1854 error = (KILL(job->pid, SIGCONT) != 0);
1855
1856 if (!error) {
1857 /*
1858 * Make sure the user knows we've continued the beast and
1859 * actually put the thing in the job table.
1860 */
1861 job->flags |= JOB_CONTINUING;
1862 status = W_STOPCODE(SIGCONT);
1863 JobFinish(job, &status);
1864
1865 job->flags &= ~(JOB_RESUME|JOB_CONTINUING);
1866 if (DEBUG(JOB)) {
1867 (void)fprintf(stdout, "done\n");
1868 (void)fflush(stdout);
1869 }
1870 } else {
1871 Error("couldn't resume %s: %s",
1872 job->node->name, strerror(errno));
1873 status = W_EXITCODE(1, 0);
1874 JobFinish(job, &status);
1875 }
1876 } else {
1877 /*
1878 * Job cannot be restarted. Mark the table as full and
1879 * place the job back on the list of stopped jobs.
1880 */
1881 if (DEBUG(JOB)) {
1882 (void)fprintf(stdout, "table full\n");
1883 (void)fflush(stdout);
1884 }
1885 (void)Lst_AtFront(stoppedJobs, (ClientData)job);
1886 return 1;
1887 }
1888 }
1889 return 0;
1890 }
1891
1892 /*-
1893 *-----------------------------------------------------------------------
1894 * JobStart --
1895 * Start a target-creation process going for the target described
1896 * by the graph node gn.
1897 *
1898 * Input:
1899 * gn target to create
1900 * flags flags for the job to override normal ones.
1901 * e.g. JOB_SPECIAL or JOB_IGNDOTS
1902 * previous The previous Job structure for this node, if any.
1903 *
1904 * Results:
1905 * JOB_ERROR if there was an error in the commands, JOB_FINISHED
1906 * if there isn't actually anything left to do for the job and
1907 * JOB_RUNNING if the job has been started.
1908 *
1909 * Side Effects:
1910 * A new Job node is created and added to the list of running
1911 * jobs. PMake is forked and a child shell created.
1912 *-----------------------------------------------------------------------
1913 */
1914 static int
1915 JobStart(GNode *gn, int flags, Job *previous)
1916 {
1917 Job *job; /* new job descriptor */
1918 char *argv[10]; /* Argument vector to shell */
1919 Boolean cmdsOK; /* true if the nodes commands were all right */
1920 Boolean local; /* Set true if the job was run locally */
1921 Boolean noExec; /* Set true if we decide not to run the job */
1922 int tfd; /* File descriptor to the temp file */
1923
1924 if (previous != NULL) {
1925 previous->flags &= ~(JOB_FIRST|JOB_IGNERR|JOB_SILENT|JOB_REMOTE);
1926 job = previous;
1927 } else {
1928 job = emalloc(sizeof(Job));
1929 if (job == NULL) {
1930 Punt("JobStart out of memory");
1931 }
1932 flags |= JOB_FIRST;
1933 }
1934 if (gn->type & OP_SPECIAL)
1935 flags |= JOB_SPECIAL;
1936
1937 job->node = gn;
1938 job->tailCmds = NILLNODE;
1939
1940 /*
1941 * Set the initial value of the flags for this job based on the global
1942 * ones and the node's attributes... Any flags supplied by the caller
1943 * are also added to the field.
1944 */
1945 job->flags = 0;
1946 if (Targ_Ignore(gn)) {
1947 job->flags |= JOB_IGNERR;
1948 }
1949 if (Targ_Silent(gn)) {
1950 job->flags |= JOB_SILENT;
1951 }
1952 job->flags |= flags;
1953
1954 /*
1955 * Check the commands now so any attributes from .DEFAULT have a chance
1956 * to migrate to the node
1957 */
1958 if (!compatMake && job->flags & JOB_FIRST) {
1959 cmdsOK = Job_CheckCommands(gn, Error);
1960 } else {
1961 cmdsOK = TRUE;
1962 }
1963
1964 #ifndef RMT_WILL_WATCH
1965 job->inPollfd = NULL;
1966 #endif
1967 /*
1968 * If the -n flag wasn't given, we open up OUR (not the child's)
1969 * temporary file to stuff commands in it. The thing is rd/wr so we don't
1970 * need to reopen it to feed it to the shell. If the -n flag *was* given,
1971 * we just set the file to be stdout. Cute, huh?
1972 */
1973 if (((gn->type & OP_MAKE) && !(noRecursiveExecute)) ||
1974 (!noExecute && !touchFlag)) {
1975 /*
1976 * tfile is the name of a file into which all shell commands are
1977 * put. It is used over by removing it before the child shell is
1978 * executed. The XXXXXX in the string are replaced by the pid of
1979 * the make process in a 6-character field with leading zeroes.
1980 */
1981 char tfile[sizeof(TMPPAT)];
1982 sigset_t mask;
1983 /*
1984 * We're serious here, but if the commands were bogus, we're
1985 * also dead...
1986 */
1987 if (!cmdsOK) {
1988 DieHorribly();
1989 }
1990
1991 JobSigLock(&mask);
1992 (void)strcpy(tfile, TMPPAT);
1993 if ((tfd = mkstemp(tfile)) == -1)
1994 Punt("Could not create temporary file %s", strerror(errno));
1995 if (!DEBUG(SCRIPT))
1996 (void)eunlink(tfile);
1997 JobSigUnlock(&mask);
1998
1999 job->cmdFILE = fdopen(tfd, "w+");
2000 if (job->cmdFILE == NULL) {
2001 Punt("Could not fdopen %s", tfile);
2002 }
2003 (void)fcntl(FILENO(job->cmdFILE), F_SETFD, 1);
2004 /*
2005 * Send the commands to the command file, flush all its buffers then
2006 * rewind and remove the thing.
2007 */
2008 noExec = FALSE;
2009
2010 /*
2011 * used to be backwards; replace when start doing multiple commands
2012 * per shell.
2013 */
2014 if (compatMake) {
2015 /*
2016 * Be compatible: If this is the first time for this node,
2017 * verify its commands are ok and open the commands list for
2018 * sequential access by later invocations of JobStart.
2019 * Once that is done, we take the next command off the list
2020 * and print it to the command file. If the command was an
2021 * ellipsis, note that there's nothing more to execute.
2022 */
2023 if ((job->flags&JOB_FIRST) && (Lst_Open(gn->commands) != SUCCESS)){
2024 cmdsOK = FALSE;
2025 } else {
2026 LstNode ln = Lst_Next(gn->commands);
2027
2028 if ((ln == NILLNODE) ||
2029 JobPrintCommand((ClientData)Lst_Datum(ln),
2030 (ClientData) job))
2031 {
2032 noExec = TRUE;
2033 Lst_Close(gn->commands);
2034 }
2035 if (noExec && !(job->flags & JOB_FIRST)) {
2036 /*
2037 * If we're not going to execute anything, the job
2038 * is done and we need to close down the various
2039 * file descriptors we've opened for output, then
2040 * call JobDoOutput to catch the final characters or
2041 * send the file to the screen... Note that the i/o streams
2042 * are only open if this isn't the first job.
2043 * Note also that this could not be done in
2044 * Job_CatchChildren b/c it wasn't clear if there were
2045 * more commands to execute or not...
2046 */
2047 JobClose(job);
2048 }
2049 }
2050 } else {
2051 /*
2052 * We can do all the commands at once. hooray for sanity
2053 */
2054 numCommands = 0;
2055 Lst_ForEach(gn->commands, JobPrintCommand, (ClientData)job);
2056
2057 /*
2058 * If we didn't print out any commands to the shell script,
2059 * there's not much point in executing the shell, is there?
2060 */
2061 if (numCommands == 0) {
2062 noExec = TRUE;
2063 }
2064 }
2065 } else if (NoExecute(gn)) {
2066 /*
2067 * Not executing anything -- just print all the commands to stdout
2068 * in one fell swoop. This will still set up job->tailCmds correctly.
2069 */
2070 if (lastNode != gn) {
2071 MESSAGE(stdout, gn);
2072 lastNode = gn;
2073 }
2074 job->cmdFILE = stdout;
2075 /*
2076 * Only print the commands if they're ok, but don't die if they're
2077 * not -- just let the user know they're bad and keep going. It
2078 * doesn't do any harm in this case and may do some good.
2079 */
2080 if (cmdsOK) {
2081 Lst_ForEach(gn->commands, JobPrintCommand, (ClientData)job);
2082 }
2083 /*
2084 * Don't execute the shell, thank you.
2085 */
2086 noExec = TRUE;
2087 } else {
2088 /*
2089 * Just touch the target and note that no shell should be executed.
2090 * Set cmdFILE to stdout to make life easier. Check the commands, too,
2091 * but don't die if they're no good -- it does no harm to keep working
2092 * up the graph.
2093 */
2094 job->cmdFILE = stdout;
2095 Job_Touch(gn, job->flags&JOB_SILENT);
2096 noExec = TRUE;
2097 }
2098
2099 /*
2100 * If we're not supposed to execute a shell, don't.
2101 */
2102 if (noExec) {
2103 /*
2104 * Unlink and close the command file if we opened one
2105 */
2106 if (job->cmdFILE != stdout) {
2107 if (job->cmdFILE != NULL) {
2108 (void)fclose(job->cmdFILE);
2109 job->cmdFILE = NULL;
2110 }
2111 } else {
2112 (void)fflush(stdout);
2113 }
2114
2115 /*
2116 * We only want to work our way up the graph if we aren't here because
2117 * the commands for the job were no good.
2118 */
2119 if (cmdsOK) {
2120 if (aborting == 0) {
2121 if (job->tailCmds != NILLNODE) {
2122 Lst_ForEachFrom(job->node->commands, job->tailCmds,
2123 JobSaveCommand,
2124 (ClientData)job->node);
2125 }
2126 if (!(job->flags & JOB_SPECIAL))
2127 Job_TokenReturn();
2128 job->node->made = MADE;
2129 Make_Update(job->node);
2130 }
2131 free(job);
2132 return(JOB_FINISHED);
2133 } else {
2134 free(job);
2135 return(JOB_ERROR);
2136 }
2137 } else {
2138 (void)fflush(job->cmdFILE);
2139 }
2140
2141 /*
2142 * Set up the control arguments to the shell. This is based on the flags
2143 * set earlier for this job.
2144 */
2145 JobMakeArgv(job, argv);
2146
2147 /*
2148 * If we're using pipes to catch output, create the pipe by which we'll
2149 * get the shell's output. If we're using files, print out that we're
2150 * starting a job and then set up its temporary-file name.
2151 */
2152 if (!compatMake || (job->flags & JOB_FIRST)) {
2153 if (usePipes) {
2154 int fd[2];
2155 if (pipe(fd) == -1)
2156 Punt("Cannot create pipe: %s", strerror(errno));
2157 job->inPipe = fd[0];
2158 job->outPipe = fd[1];
2159 (void)fcntl(job->inPipe, F_SETFD, 1);
2160 (void)fcntl(job->outPipe, F_SETFD, 1);
2161 } else {
2162 (void)fprintf(stdout, "Remaking `%s'\n", gn->name);
2163 (void)fflush(stdout);
2164 (void)strcpy(job->outFile, TMPPAT);
2165 job->outFd = mkstemp(job->outFile);
2166 (void)fcntl(job->outFd, F_SETFD, 1);
2167 }
2168 }
2169
2170 #ifdef REMOTE
2171 if (!(gn->type & OP_NOEXPORT) && !(runLocalFirst && nLocal < maxLocal)) {
2172 #ifdef RMT_NO_EXEC
2173 local = !Rmt_Export(shellPath, argv, job);
2174 #else
2175 local = !Rmt_Begin(shellPath, argv, job->node);
2176 #endif /* RMT_NO_EXEC */
2177 if (!local) {
2178 job->flags |= JOB_REMOTE;
2179 }
2180 } else
2181 #endif
2182 local = TRUE;
2183
2184 if (local && (((nLocal >= maxLocal) &&
2185 !(job->flags & JOB_SPECIAL) &&
2186 #ifdef REMOTE
2187 (!(gn->type & OP_NOEXPORT) || (maxLocal != 0))
2188 #else
2189 (maxLocal != 0)
2190 #endif
2191 )))
2192 {
2193 /*
2194 * The job can only be run locally, but we've hit the limit of
2195 * local concurrency, so put the job on hold until some other job
2196 * finishes. Note that the special jobs (.BEGIN, .INTERRUPT and .END)
2197 * may be run locally even when the local limit has been reached
2198 * (e.g. when maxLocal == 0), though they will be exported if at
2199 * all possible. In addition, any target marked with .NOEXPORT will
2200 * be run locally if maxLocal is 0.
2201 */
2202 job->flags |= JOB_RESTART;
2203 (void)Lst_AtEnd(stoppedJobs, (ClientData)job);
2204 } else {
2205 JobExec(job, argv);
2206 }
2207 return(JOB_RUNNING);
2208 }
2209
2210 static char *
2211 JobOutput(Job *job, char *cp, char *endp, int msg)
2212 {
2213 char *ecp;
2214
2215 if (commandShell->noPrint) {
2216 ecp = Str_FindSubstring(cp, commandShell->noPrint);
2217 while (ecp != NULL) {
2218 if (cp != ecp) {
2219 *ecp = '\0';
2220 if (!beSilent && msg && job->node != lastNode) {
2221 MESSAGE(stdout, job->node);
2222 lastNode = job->node;
2223 }
2224 /*
2225 * The only way there wouldn't be a newline after
2226 * this line is if it were the last in the buffer.
2227 * however, since the non-printable comes after it,
2228 * there must be a newline, so we don't print one.
2229 */
2230 (void)fprintf(stdout, "%s", cp);
2231 (void)fflush(stdout);
2232 }
2233 cp = ecp + commandShell->noPLen;
2234 if (cp != endp) {
2235 /*
2236 * Still more to print, look again after skipping
2237 * the whitespace following the non-printable
2238 * command....
2239 */
2240 cp++;
2241 while (*cp == ' ' || *cp == '\t' || *cp == '\n') {
2242 cp++;
2243 }
2244 ecp = Str_FindSubstring(cp, commandShell->noPrint);
2245 } else {
2246 return cp;
2247 }
2248 }
2249 }
2250 return cp;
2251 }
2252
2253 /*-
2254 *-----------------------------------------------------------------------
2255 * JobDoOutput --
2256 * This function is called at different times depending on
2257 * whether the user has specified that output is to be collected
2258 * via pipes or temporary files. In the former case, we are called
2259 * whenever there is something to read on the pipe. We collect more
2260 * output from the given job and store it in the job's outBuf. If
2261 * this makes up a line, we print it tagged by the job's identifier,
2262 * as necessary.
2263 * If output has been collected in a temporary file, we open the
2264 * file and read it line by line, transfering it to our own
2265 * output channel until the file is empty. At which point we
2266 * remove the temporary file.
2267 * In both cases, however, we keep our figurative eye out for the
2268 * 'noPrint' line for the shell from which the output came. If
2269 * we recognize a line, we don't print it. If the command is not
2270 * alone on the line (the character after it is not \0 or \n), we
2271 * do print whatever follows it.
2272 *
2273 * Input:
2274 * job the job whose output needs printing
2275 * finish TRUE if this is the last time we'll be called
2276 * for this job
2277 *
2278 * Results:
2279 * None
2280 *
2281 * Side Effects:
2282 * curPos may be shifted as may the contents of outBuf.
2283 *-----------------------------------------------------------------------
2284 */
2285 STATIC void
2286 JobDoOutput(Job *job, Boolean finish)
2287 {
2288 Boolean gotNL = FALSE; /* true if got a newline */
2289 Boolean fbuf; /* true if our buffer filled up */
2290 int nr; /* number of bytes read */
2291 int i; /* auxiliary index into outBuf */
2292 int max; /* limit for i (end of current data) */
2293 int nRead; /* (Temporary) number of bytes read */
2294
2295 FILE *oFILE; /* Stream pointer to shell's output file */
2296 char inLine[132];
2297
2298
2299 if (usePipes) {
2300 /*
2301 * Read as many bytes as will fit in the buffer.
2302 */
2303 end_loop:
2304 gotNL = FALSE;
2305 fbuf = FALSE;
2306
2307 nRead = read(job->inPipe, &job->outBuf[job->curPos],
2308 JOB_BUFSIZE - job->curPos);
2309 if (nRead < 0) {
2310 if (DEBUG(JOB)) {
2311 perror("JobDoOutput(piperead)");
2312 }
2313 nr = 0;
2314 } else {
2315 nr = nRead;
2316 }
2317
2318 /*
2319 * If we hit the end-of-file (the job is dead), we must flush its
2320 * remaining output, so pretend we read a newline if there's any
2321 * output remaining in the buffer.
2322 * Also clear the 'finish' flag so we stop looping.
2323 */
2324 if ((nr == 0) && (job->curPos != 0)) {
2325 job->outBuf[job->curPos] = '\n';
2326 nr = 1;
2327 finish = FALSE;
2328 } else if (nr == 0) {
2329 finish = FALSE;
2330 }
2331
2332 /*
2333 * Look for the last newline in the bytes we just got. If there is
2334 * one, break out of the loop with 'i' as its index and gotNL set
2335 * TRUE.
2336 */
2337 max = job->curPos + nr;
2338 for (i = job->curPos + nr - 1; i >= job->curPos; i--) {
2339 if (job->outBuf[i] == '\n') {
2340 gotNL = TRUE;
2341 break;
2342 } else if (job->outBuf[i] == '\0') {
2343 /*
2344 * Why?
2345 */
2346 job->outBuf[i] = ' ';
2347 }
2348 }
2349
2350 if (!gotNL) {
2351 job->curPos += nr;
2352 if (job->curPos == JOB_BUFSIZE) {
2353 /*
2354 * If we've run out of buffer space, we have no choice
2355 * but to print the stuff. sigh.
2356 */
2357 fbuf = TRUE;
2358 i = job->curPos;
2359 }
2360 }
2361 if (gotNL || fbuf) {
2362 /*
2363 * Need to send the output to the screen. Null terminate it
2364 * first, overwriting the newline character if there was one.
2365 * So long as the line isn't one we should filter (according
2366 * to the shell description), we print the line, preceded
2367 * by a target banner if this target isn't the same as the
2368 * one for which we last printed something.
2369 * The rest of the data in the buffer are then shifted down
2370 * to the start of the buffer and curPos is set accordingly.
2371 */
2372 job->outBuf[i] = '\0';
2373 if (i >= job->curPos) {
2374 char *cp;
2375
2376 cp = JobOutput(job, job->outBuf, &job->outBuf[i], FALSE);
2377
2378 /*
2379 * There's still more in that thar buffer. This time, though,
2380 * we know there's no newline at the end, so we add one of
2381 * our own free will.
2382 */
2383 if (*cp != '\0') {
2384 if (!beSilent && job->node != lastNode) {
2385 MESSAGE(stdout, job->node);
2386 lastNode = job->node;
2387 }
2388 (void)fprintf(stdout, "%s%s", cp, gotNL ? "\n" : "");
2389 (void)fflush(stdout);
2390 }
2391 }
2392 if (i < max - 1) {
2393 /* shift the remaining characters down */
2394 (void)memcpy(job->outBuf, &job->outBuf[i + 1], max - (i + 1));
2395 job->curPos = max - (i + 1);
2396
2397 } else {
2398 /*
2399 * We have written everything out, so we just start over
2400 * from the start of the buffer. No copying. No nothing.
2401 */
2402 job->curPos = 0;
2403 }
2404 }
2405 if (finish) {
2406 /*
2407 * If the finish flag is true, we must loop until we hit
2408 * end-of-file on the pipe. This is guaranteed to happen
2409 * eventually since the other end of the pipe is now closed
2410 * (we closed it explicitly and the child has exited). When
2411 * we do get an EOF, finish will be set FALSE and we'll fall
2412 * through and out.
2413 */
2414 goto end_loop;
2415 }
2416 } else {
2417 /*
2418 * We've been called to retrieve the output of the job from the
2419 * temporary file where it's been squirreled away. This consists of
2420 * opening the file, reading the output line by line, being sure not
2421 * to print the noPrint line for the shell we used, then close and
2422 * remove the temporary file. Very simple.
2423 *
2424 * Change to read in blocks and do FindSubString type things as for
2425 * pipes? That would allow for "@echo -n..."
2426 */
2427 oFILE = fopen(job->outFile, "r");
2428 if (oFILE != NULL) {
2429 (void)fprintf(stdout, "Results of making %s:\n", job->node->name);
2430 (void)fflush(stdout);
2431 while (fgets(inLine, sizeof(inLine), oFILE) != NULL) {
2432 char *cp, *endp, *oendp;
2433
2434 cp = inLine;
2435 oendp = endp = inLine + strlen(inLine);
2436 if (endp[-1] == '\n') {
2437 *--endp = '\0';
2438 }
2439 cp = JobOutput(job, inLine, endp, FALSE);
2440
2441 /*
2442 * There's still more in that thar buffer. This time, though,
2443 * we know there's no newline at the end, so we add one of
2444 * our own free will.
2445 */
2446 (void)fprintf(stdout, "%s", cp);
2447 (void)fflush(stdout);
2448 if (endp != oendp) {
2449 (void)fprintf(stdout, "\n");
2450 (void)fflush(stdout);
2451 }
2452 }
2453 (void)fclose(oFILE);
2454 (void)eunlink(job->outFile);
2455 } else {
2456 Punt("Cannot open `%s'", job->outFile);
2457 }
2458 }
2459 }
2460
2461 static void
2462 JobRun(GNode *targ)
2463 {
2464 #ifdef notyet
2465 /*
2466 * Unfortunately it is too complicated to run .BEGIN, .END,
2467 * and .INTERRUPT job in the parallel job module. This has
2468 * the nice side effect that it avoids a lot of other problems.
2469 */
2470 Lst lst = Lst_Init(FALSE);
2471 Lst_AtEnd(lst, targ);
2472 (void)Make_Run(lst);
2473 Lst_Destroy(lst, NOFREE);
2474 JobStart(targ, JOB_SPECIAL, NULL);
2475 while (nJobs) {
2476 Job_CatchOutput();
2477 #ifndef RMT_WILL_WATCH
2478 Job_CatchChildren(!usePipes);
2479 #endif /* RMT_WILL_WATCH */
2480 }
2481 #else
2482 Compat_Make(targ, targ);
2483 if (targ->made == ERROR) {
2484 PrintOnError("\n\nStop.");
2485 exit(1);
2486 }
2487 #endif
2488 }
2489
2490 /*-
2491 *-----------------------------------------------------------------------
2492 * Job_CatchChildren --
2493 * Handle the exit of a child. Called from Make_Make.
2494 *
2495 * Input:
2496 * block TRUE if should block on the wait
2497 *
2498 * Results:
2499 * none.
2500 *
2501 * Side Effects:
2502 * The job descriptor is removed from the list of children.
2503 *
2504 * Notes:
2505 * We do waits, blocking or not, according to the wisdom of our
2506 * caller, until there are no more children to report. For each
2507 * job, call JobFinish to finish things off. This will take care of
2508 * putting jobs on the stoppedJobs queue.
2509 *
2510 *-----------------------------------------------------------------------
2511 */
2512 void
2513 Job_CatchChildren(Boolean block)
2514 {
2515 int pid; /* pid of dead child */
2516 Job *job; /* job descriptor for dead child */
2517 LstNode jnode; /* list element for finding job */
2518 int status; /* Exit/termination status */
2519
2520 /*
2521 * Don't even bother if we know there's no one around.
2522 */
2523 if (nLocal == 0) {
2524 return;
2525 }
2526
2527 while ((pid = waitpid((pid_t) -1, &status,
2528 (block?0:WNOHANG)|WUNTRACED)) > 0)
2529 {
2530 if (DEBUG(JOB)) {
2531 (void)fprintf(stdout, "Process %d exited or stopped %x.\n", pid,
2532 status);
2533 (void)fflush(stdout);
2534 }
2535
2536 jnode = Lst_Find(jobs, (ClientData)&pid, JobCmpPid);
2537 if (jnode == NILLNODE) {
2538 if (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGCONT)) {
2539 jnode = Lst_Find(stoppedJobs, (ClientData) &pid, JobCmpPid);
2540 if (jnode == NILLNODE) {
2541 Error("Resumed child (%d) not in table", pid);
2542 continue;
2543 }
2544 job = (Job *)Lst_Datum(jnode);
2545 (void)Lst_Remove(stoppedJobs, jnode);
2546 } else {
2547 Error("Child (%d) not in table?", pid);
2548 continue;
2549 }
2550 } else {
2551 job = (Job *)Lst_Datum(jnode);
2552 (void)Lst_Remove(jobs, jnode);
2553 nJobs -= 1;
2554 #ifdef REMOTE
2555 if (!(job->flags & JOB_REMOTE)) {
2556 if (DEBUG(JOB)) {
2557 (void)fprintf(stdout,
2558 "Job queue has one fewer local process.\n");
2559 (void)fflush(stdout);
2560 }
2561 nLocal -= 1;
2562 }
2563 #else
2564 nLocal -= 1;
2565 #endif
2566 }
2567
2568 JobFinish(job, &status);
2569 }
2570 }
2571
2572 /*-
2573 *-----------------------------------------------------------------------
2574 * Job_CatchOutput --
2575 * Catch the output from our children, if we're using
2576 * pipes do so. Otherwise just block time until we get a
2577 * signal(most likely a SIGCHLD) since there's no point in
2578 * just spinning when there's nothing to do and the reaping
2579 * of a child can wait for a while.
2580 *
2581 * Results:
2582 * None
2583 *
2584 * Side Effects:
2585 * Output is read from pipes if we're piping.
2586 * -----------------------------------------------------------------------
2587 */
2588 void
2589 Job_CatchOutput(void)
2590 {
2591 int nready;
2592 LstNode ln;
2593 Job *job;
2594 #ifdef RMT_WILL_WATCH
2595 int pnJobs; /* Previous nJobs */
2596 #endif
2597
2598 (void)fflush(stdout);
2599 Job_TokenFlush();
2600 #ifdef RMT_WILL_WATCH
2601 pnJobs = nJobs;
2602
2603 /*
2604 * It is possible for us to be called with nJobs equal to 0. This happens
2605 * if all the jobs finish and a job that is stopped cannot be run
2606 * locally (eg if maxLocal is 0) and cannot be exported. The job will
2607 * be placed back on the stoppedJobs queue, Job_Empty() will return false,
2608 * Make_Run will call us again when there's nothing for which to wait.
2609 * nJobs never changes, so we loop forever. Hence the check. It could
2610 * be argued that we should sleep for a bit so as not to swamp the
2611 * exportation system with requests. Perhaps we should.
2612 *
2613 * NOTE: IT IS THE RESPONSIBILITY OF Rmt_Wait TO CALL Job_CatchChildren
2614 * IN A TIMELY FASHION TO CATCH ANY LOCALLY RUNNING JOBS THAT EXIT.
2615 * It may use the variable nLocal to determine if it needs to call
2616 * Job_CatchChildren(if nLocal is 0, there's nothing for which to
2617 * wait...)
2618 */
2619 while (nJobs != 0 && pnJobs == nJobs) {
2620 Rmt_Wait();
2621 }
2622 #else
2623 if (usePipes) {
2624 if ((nready = poll((wantToken ? fds : (fds + 1)),
2625 (wantToken ? nfds : (nfds - 1)), POLL_MSEC)) <= 0) {
2626 return;
2627 } else {
2628 sigset_t mask;
2629 JobSigLock(&mask);
2630 if (Lst_Open(jobs) == FAILURE) {
2631 Punt("Cannot open job table");
2632 }
2633
2634 if (readyfd(&childExitJob)) {
2635 char token;
2636 (void)read(childExitJob.inPipe, &token, 1);
2637 nready -= 1;
2638 }
2639
2640 while (nready && (ln = Lst_Next(jobs)) != NILLNODE) {
2641 job = (Job *)Lst_Datum(ln);
2642 if (readyfd(job)) {
2643 JobDoOutput(job, FALSE);
2644 nready -= 1;
2645 }
2646 }
2647 Lst_Close(jobs);
2648 JobSigUnlock(&mask);
2649 }
2650 }
2651 #endif /* RMT_WILL_WATCH */
2652 }
2653
2654 /*-
2655 *-----------------------------------------------------------------------
2656 * Job_Make --
2657 * Start the creation of a target. Basically a front-end for
2658 * JobStart used by the Make module.
2659 *
2660 * Results:
2661 * None.
2662 *
2663 * Side Effects:
2664 * Another job is started.
2665 *
2666 *-----------------------------------------------------------------------
2667 */
2668 void
2669 Job_Make(GNode *gn)
2670 {
2671 (void)JobStart(gn, 0, NULL);
2672 }
2673
2674 void
2675 Shell_Init()
2676 {
2677 if (shellPath == NULL) {
2678 /*
2679 * The user didn't specify a shell to use, so we are using the
2680 * default one... Both the absolute path and the last component
2681 * must be set. The last component is taken from the 'name' field
2682 * of the default shell description pointed-to by commandShell.
2683 * All default shells are located in _PATH_DEFSHELLDIR.
2684 */
2685 shellName = commandShell->name;
2686 shellPath = str_concat(_PATH_DEFSHELLDIR, shellName, STR_ADDSLASH);
2687 }
2688 if (commandShell->exit == NULL) {
2689 commandShell->exit = "";
2690 }
2691 if (commandShell->echo == NULL) {
2692 commandShell->echo = "";
2693 }
2694 }
2695
2696 /*-
2697 *-----------------------------------------------------------------------
2698 * Job_Init --
2699 * Initialize the process module
2700 *
2701 * Input:
2702 * maxproc the greatest number of jobs which may be running
2703 * at one time
2704 * maxlocal the greatest number of jobs which may be running
2705 * at once
2706 *
2707 * Results:
2708 * none
2709 *
2710 * Side Effects:
2711 * lists and counters are initialized
2712 *-----------------------------------------------------------------------
2713 */
2714 void
2715 Job_Init(int maxproc, int maxlocal)
2716 {
2717 GNode *begin; /* node for commands to do at the very start */
2718
2719 jobs = Lst_Init(FALSE);
2720 stoppedJobs = Lst_Init(FALSE);
2721 maxJobs = maxproc;
2722 maxLocal = maxlocal;
2723 nJobs = 0;
2724 nLocal = 0;
2725 wantToken = FALSE;
2726
2727 aborting = 0;
2728 errors = 0;
2729
2730 lastNode = NILGNODE;
2731
2732 if (maxJobs == 1
2733 #ifdef REMOTE
2734 || noMessages
2735 #endif
2736 ) {
2737 /*
2738 * If only one job can run at a time, there's no need for a banner,
2739 * is there?
2740 */
2741 targFmt = "";
2742 } else {
2743 targFmt = TARG_FMT;
2744 }
2745
2746 Shell_Init();
2747
2748 if (pipe(exit_pipe) < 0)
2749 Fatal("error in pipe: %s", strerror(errno));
2750 fcntl(exit_pipe[0], F_SETFD, 1);
2751 fcntl(exit_pipe[1], F_SETFD, 1);
2752
2753 childExitJob.inPipe = exit_pipe[0];
2754
2755 sigemptyset(&caught_signals);
2756 /*
2757 * Install a SIGCHLD handler.
2758 */
2759 (void)signal(SIGCHLD, JobChildSig);
2760 sigaddset(&caught_signals, SIGCHLD);
2761
2762 #define ADDSIG(s,h) \
2763 if (signal(s, SIG_IGN) != SIG_IGN) { \
2764 sigaddset(&caught_signals, s); \
2765 (void)signal(s, h); \
2766 }
2767
2768 /*
2769 * Catch the four signals that POSIX specifies if they aren't ignored.
2770 * JobPassSig will take care of calling JobInterrupt if appropriate.
2771 */
2772 ADDSIG(SIGINT, JobPassSig)
2773 ADDSIG(SIGHUP, JobPassSig)
2774 ADDSIG(SIGTERM, JobPassSig)
2775 ADDSIG(SIGQUIT, JobPassSig)
2776
2777 /*
2778 * There are additional signals that need to be caught and passed if
2779 * either the export system wants to be told directly of signals or if
2780 * we're giving each job its own process group (since then it won't get
2781 * signals from the terminal driver as we own the terminal)
2782 */
2783 #if defined(RMT_WANTS_SIGNALS) || defined(USE_PGRP)
2784 ADDSIG(SIGTSTP, JobPassSig)
2785 ADDSIG(SIGTTOU, JobPassSig)
2786 ADDSIG(SIGTTIN, JobPassSig)
2787 ADDSIG(SIGWINCH, JobPassSig)
2788 ADDSIG(SIGCONT, JobContinueSig)
2789 #endif
2790 #undef ADDSIG
2791
2792 begin = Targ_FindNode(".BEGIN", TARG_NOCREATE);
2793
2794 if (begin != NILGNODE) {
2795 JobRun(begin);
2796 if (begin->made == ERROR) {
2797 PrintOnError("\n\nStop.");
2798 exit(1);
2799 }
2800 }
2801 postCommands = Targ_FindNode(".END", TARG_CREATE);
2802 }
2803
2804 static void JobSigReset(void)
2805 {
2806 #define DELSIG(s) \
2807 if (sigismember(&caught_signals, s)) { \
2808 (void)signal(SIGINT, SIG_DFL); \
2809 }
2810
2811 DELSIG(SIGINT)
2812 DELSIG(SIGHUP)
2813 DELSIG(SIGQUIT)
2814 DELSIG(SIGTERM)
2815 #if defined(RMT_WANTS_SIGNALS) || defined(USE_PGRP)
2816 DELSIG(SIGTSTP)
2817 DELSIG(SIGTTOU)
2818 DELSIG(SIGTTIN)
2819 DELSIG(SIGWINCH)
2820 DELSIG(SIGCONT)
2821 #endif
2822 #undef DELSIG
2823 (void)signal(SIGCHLD, SIG_DFL);
2824 }
2825
2826 /*-
2827 *-----------------------------------------------------------------------
2828 * Job_Empty --
2829 * See if the job table is empty. Because the local concurrency may
2830 * be set to 0, it is possible for the job table to become empty,
2831 * while the list of stoppedJobs remains non-empty. In such a case,
2832 * we want to restart as many jobs as we can.
2833 *
2834 * Results:
2835 * TRUE if it is. FALSE if it ain't.
2836 *
2837 * Side Effects:
2838 * None.
2839 *
2840 * -----------------------------------------------------------------------
2841 */
2842 Boolean
2843 Job_Empty(void)
2844 {
2845 if (nJobs == 0) {
2846 if (!Lst_IsEmpty(stoppedJobs) && !aborting) {
2847 /*
2848 * The job table is obviously not full if it has no jobs in
2849 * it...Try and restart the stopped jobs.
2850 */
2851 JobRestartJobs();
2852 return(FALSE);
2853 } else {
2854 return(TRUE);
2855 }
2856 } else {
2857 return(FALSE);
2858 }
2859 }
2860
2861 /*-
2862 *-----------------------------------------------------------------------
2863 * JobMatchShell --
2864 * Find a shell in 'shells' given its name.
2865 *
2866 * Results:
2867 * A pointer to the Shell structure.
2868 *
2869 * Side Effects:
2870 * None.
2871 *
2872 *-----------------------------------------------------------------------
2873 */
2874 static Shell *
2875 JobMatchShell(const char *name)
2876 {
2877 Shell *sh;
2878
2879 for (sh = shells; sh->name != NULL; sh++) {
2880 if (strcmp(name, sh->name) == 0)
2881 return (sh);
2882 }
2883 return (NULL);
2884 }
2885
2886 /*-
2887 *-----------------------------------------------------------------------
2888 * Job_ParseShell --
2889 * Parse a shell specification and set up commandShell, shellPath
2890 * and shellName appropriately.
2891 *
2892 * Input:
2893 * line The shell spec
2894 *
2895 * Results:
2896 * FAILURE if the specification was incorrect.
2897 *
2898 * Side Effects:
2899 * commandShell points to a Shell structure (either predefined or
2900 * created from the shell spec), shellPath is the full path of the
2901 * shell described by commandShell, while shellName is just the
2902 * final component of shellPath.
2903 *
2904 * Notes:
2905 * A shell specification consists of a .SHELL target, with dependency
2906 * operator, followed by a series of blank-separated words. Double
2907 * quotes can be used to use blanks in words. A backslash escapes
2908 * anything (most notably a double-quote and a space) and
2909 * provides the functionality it does in C. Each word consists of
2910 * keyword and value separated by an equal sign. There should be no
2911 * unnecessary spaces in the word. The keywords are as follows:
2912 * name Name of shell.
2913 * path Location of shell.
2914 * quiet Command to turn off echoing.
2915 * echo Command to turn echoing on
2916 * filter Result of turning off echoing that shouldn't be
2917 * printed.
2918 * echoFlag Flag to turn echoing on at the start
2919 * errFlag Flag to turn error checking on at the start
2920 * hasErrCtl True if shell has error checking control
2921 * check Command to turn on error checking if hasErrCtl
2922 * is TRUE or template of command to echo a command
2923 * for which error checking is off if hasErrCtl is
2924 * FALSE.
2925 * ignore Command to turn off error checking if hasErrCtl
2926 * is TRUE or template of command to execute a
2927 * command so as to ignore any errors it returns if
2928 * hasErrCtl is FALSE.
2929 *
2930 *-----------------------------------------------------------------------
2931 */
2932 ReturnStatus
2933 Job_ParseShell(char *line)
2934 {
2935 char **words;
2936 char **argv;
2937 int argc;
2938 char *path;
2939 Shell newShell;
2940 Boolean fullSpec = FALSE;
2941 Shell *sh;
2942
2943 while (isspace((unsigned char)*line)) {
2944 line++;
2945 }
2946
2947 if (shellArgv)
2948 free(UNCONST(shellArgv));
2949
2950 memset(&newShell, 0, sizeof(newShell));
2951
2952 /*
2953 * Parse the specification by keyword
2954 */
2955 words = brk_string(line, &argc, TRUE, &path);
2956 shellArgv = path;
2957
2958 for (path = NULL, argv = words; argc != 0; argc--, argv++) {
2959 if (strncmp(*argv, "path=", 5) == 0) {
2960 path = &argv[0][5];
2961 } else if (strncmp(*argv, "name=", 5) == 0) {
2962 newShell.name = &argv[0][5];
2963 } else {
2964 if (strncmp(*argv, "quiet=", 6) == 0) {
2965 newShell.echoOff = &argv[0][6];
2966 } else if (strncmp(*argv, "echo=", 5) == 0) {
2967 newShell.echoOn = &argv[0][5];
2968 } else if (strncmp(*argv, "filter=", 7) == 0) {
2969 newShell.noPrint = &argv[0][7];
2970 newShell.noPLen = strlen(newShell.noPrint);
2971 } else if (strncmp(*argv, "echoFlag=", 9) == 0) {
2972 newShell.echo = &argv[0][9];
2973 } else if (strncmp(*argv, "errFlag=", 8) == 0) {
2974 newShell.exit = &argv[0][8];
2975 } else if (strncmp(*argv, "hasErrCtl=", 10) == 0) {
2976 char c = argv[0][10];
2977 newShell.hasErrCtl = !((c != 'Y') && (c != 'y') &&
2978 (c != 'T') && (c != 't'));
2979 } else if (strncmp(*argv, "check=", 6) == 0) {
2980 newShell.errCheck = &argv[0][6];
2981 } else if (strncmp(*argv, "ignore=", 7) == 0) {
2982 newShell.ignErr = &argv[0][7];
2983 } else if (strncmp(*argv, "errout=", 7) == 0) {
2984 newShell.errOut = &argv[0][7];
2985 } else if (strncmp(*argv, "comment=", 8) == 0) {
2986 newShell.commentChar = argv[0][8];
2987 } else {
2988 Parse_Error(PARSE_FATAL, "Unknown keyword \"%s\"",
2989 *argv);
2990 free(words);
2991 return(FAILURE);
2992 }
2993 fullSpec = TRUE;
2994 }
2995 }
2996
2997 if (path == NULL) {
2998 /*
2999 * If no path was given, the user wants one of the pre-defined shells,
3000 * yes? So we find the one s/he wants with the help of JobMatchShell
3001 * and set things up the right way. shellPath will be set up by
3002 * Job_Init.
3003 */
3004 if (newShell.name == NULL) {
3005 Parse_Error(PARSE_FATAL, "Neither path nor name specified");
3006 free(words);
3007 return(FAILURE);
3008 } else {
3009 if ((sh = JobMatchShell(newShell.name)) == NULL) {
3010 Parse_Error(PARSE_WARNING, "%s: No matching shell",
3011 newShell.name);
3012 free(words);
3013 return(FAILURE);
3014 }
3015 commandShell = sh;
3016 shellName = newShell.name;
3017 }
3018 } else {
3019 /*
3020 * The user provided a path. If s/he gave nothing else (fullSpec is
3021 * FALSE), try and find a matching shell in the ones we know of.
3022 * Else we just take the specification at its word and copy it
3023 * to a new location. In either case, we need to record the
3024 * path the user gave for the shell.
3025 */
3026 shellPath = path;
3027 path = strrchr(path, '/');
3028 if (path == NULL) {
3029 path = UNCONST(shellPath);
3030 } else {
3031 path += 1;
3032 }
3033 if (newShell.name != NULL) {
3034 shellName = newShell.name;
3035 } else {
3036 shellName = path;
3037 }
3038 if (!fullSpec) {
3039 if ((sh = JobMatchShell(shellName)) == NULL) {
3040 Parse_Error(PARSE_WARNING, "%s: No matching shell",
3041 shellName);
3042 free(words);
3043 return(FAILURE);
3044 }
3045 commandShell = sh;
3046 } else {
3047 commandShell = emalloc(sizeof(Shell));
3048 *commandShell = newShell;
3049 }
3050 }
3051
3052 if (commandShell->echoOn && commandShell->echoOff) {
3053 commandShell->hasEchoCtl = TRUE;
3054 }
3055
3056 if (!commandShell->hasErrCtl) {
3057 if (commandShell->errCheck == NULL) {
3058 commandShell->errCheck = "";
3059 }
3060 if (commandShell->ignErr == NULL) {
3061 commandShell->ignErr = "%s\n";
3062 }
3063 }
3064
3065 /*
3066 * Do not free up the words themselves, since they might be in use by the
3067 * shell specification.
3068 */
3069 free(words);
3070 return SUCCESS;
3071 }
3072
3073 /*-
3074 *-----------------------------------------------------------------------
3075 * JobInterrupt --
3076 * Handle the receipt of an interrupt.
3077 *
3078 * Input:
3079 * runINTERRUPT Non-zero if commands for the .INTERRUPT target
3080 * should be executed
3081 * signo signal received
3082 *
3083 * Results:
3084 * None
3085 *
3086 * Side Effects:
3087 * All children are killed. Another job will be started if the
3088 * .INTERRUPT target was given.
3089 *-----------------------------------------------------------------------
3090 */
3091 static void
3092 JobInterrupt(int runINTERRUPT, int signo)
3093 {
3094 LstNode ln; /* element in job table */
3095 Job *job; /* job descriptor in that element */
3096 GNode *interrupt; /* the node describing the .INTERRUPT target */
3097 sigset_t mask;
3098
3099 aborting = ABORT_INTERRUPT;
3100
3101 JobSigLock(&mask);
3102
3103 (void)Lst_Open(jobs);
3104 while ((ln = Lst_Next(jobs)) != NILLNODE) {
3105 GNode *gn;
3106
3107 job = (Job *)Lst_Datum(ln);
3108 gn = job->node;
3109
3110 if ((gn->type & (OP_JOIN|OP_PHONY)) == 0 && !Targ_Precious(gn)) {
3111 char *file = (gn->path == NULL ? gn->name : gn->path);
3112 if (!noExecute && eunlink(file) != -1) {
3113 Error("*** %s removed", file);
3114 }
3115 }
3116 #ifdef RMT_WANTS_SIGNALS
3117 if (job->flags & JOB_REMOTE) {
3118 /*
3119 * If job is remote, let the Rmt module do the killing.
3120 */
3121 if (!Rmt_Signal(job, signo)) {
3122 /*
3123 * If couldn't kill the thing, finish it out now with an
3124 * error code, since no exit report will come in likely.
3125 */
3126 int status;
3127
3128 status.w_status = 0;
3129 status.w_retcode = 1;
3130 JobFinish(job, &status);
3131 }
3132 } else if (job->pid) {
3133 KILL(job->pid, signo);
3134 }
3135 #else
3136 if (job->pid) {
3137 if (DEBUG(JOB)) {
3138 (void)fprintf(stdout,
3139 "JobInterrupt passing signal %d to child %d.\n",
3140 signo, job->pid);
3141 (void)fflush(stdout);
3142 }
3143 KILL(job->pid, signo);
3144 }
3145 #endif /* RMT_WANTS_SIGNALS */
3146 }
3147 Lst_Close(jobs);
3148
3149 #ifdef REMOTE
3150 (void)Lst_Open(stoppedJobs);
3151 while ((ln = Lst_Next(stoppedJobs)) != NILLNODE) {
3152 GNode *gn;
3153
3154 job = (Job *)Lst_Datum(ln);
3155 gn = job->node;
3156
3157 if (job->flags & JOB_RESTART) {
3158 if (DEBUG(JOB)) {
3159 (void)fprintf(stdout, "%s%s",
3160 "JobInterrupt skipping job on stopped queue",
3161 "-- it was waiting to be restarted.\n");
3162 (void)fflush(stdout);
3163 }
3164 continue;
3165 }
3166 if ((gn->type & (OP_JOIN|OP_PHONY)) == 0 && !Targ_Precious(gn)) {
3167 char *file = (gn->path == NULL ? gn->name : gn->path);
3168 if (eunlink(file) == 0) {
3169 Error("*** %s removed", file);
3170 }
3171 }
3172 /*
3173 * Resume the thing so it will take the signal.
3174 */
3175 if (DEBUG(JOB)) {
3176 (void)fprintf(stdout,
3177 "JobInterrupt passing CONT to stopped child %d.\n",
3178 job->pid);
3179 (void)fflush(stdout);
3180 }
3181 KILL(job->pid, SIGCONT);
3182 #ifdef RMT_WANTS_SIGNALS
3183 if (job->flags & JOB_REMOTE) {
3184 /*
3185 * If job is remote, let the Rmt module do the killing.
3186 */
3187 if (!Rmt_Signal(job, SIGINT)) {
3188 /*
3189 * If couldn't kill the thing, finish it out now with an
3190 * error code, since no exit report will come in likely.
3191 */
3192 int status;
3193 status.w_status = 0;
3194 status.w_retcode = 1;
3195 JobFinish(job, &status);
3196 }
3197 } else if (job->pid) {
3198 if (DEBUG(JOB)) {
3199 (void)fprintf(stdout,
3200 "JobInterrupt passing interrupt to stopped child %d.\n",
3201 job->pid);
3202 (void)fflush(stdout);
3203 }
3204 KILL(job->pid, SIGINT);
3205 }
3206 #endif /* RMT_WANTS_SIGNALS */
3207 }
3208 Lst_Close(stoppedJobs);
3209 #endif /* REMOTE */
3210
3211 JobSigUnlock(&mask);
3212
3213 if (runINTERRUPT && !touchFlag) {
3214 interrupt = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
3215 if (interrupt != NILGNODE) {
3216 ignoreErrors = FALSE;
3217 JobRun(interrupt);
3218 }
3219 }
3220 Trace_Log(MAKEINTR, 0);
3221 exit(signo);
3222 }
3223
3224 /*
3225 *-----------------------------------------------------------------------
3226 * Job_Finish --
3227 * Do final processing such as the running of the commands
3228 * attached to the .END target.
3229 *
3230 * Results:
3231 * Number of errors reported.
3232 *
3233 * Side Effects:
3234 * None.
3235 *-----------------------------------------------------------------------
3236 */
3237 int
3238 Job_Finish(void)
3239 {
3240 if (postCommands != NILGNODE && !Lst_IsEmpty(postCommands->commands)) {
3241 if (errors) {
3242 Error("Errors reported so .END ignored");
3243 } else {
3244 JobRun(postCommands);
3245 }
3246 }
3247 Job_TokenFlush();
3248 return(errors);
3249 }
3250
3251 /*-
3252 *-----------------------------------------------------------------------
3253 * Job_End --
3254 * Cleanup any memory used by the jobs module
3255 *
3256 * Results:
3257 * None.
3258 *
3259 * Side Effects:
3260 * Memory is freed
3261 *-----------------------------------------------------------------------
3262 */
3263 void
3264 Job_End(void)
3265 {
3266 #ifdef CLEANUP
3267 if (shellArgv)
3268 free(shellArgv);
3269 #endif
3270 }
3271
3272 /*-
3273 *-----------------------------------------------------------------------
3274 * Job_Wait --
3275 * Waits for all running jobs to finish and returns. Sets 'aborting'
3276 * to ABORT_WAIT to prevent other jobs from starting.
3277 *
3278 * Results:
3279 * None.
3280 *
3281 * Side Effects:
3282 * Currently running jobs finish.
3283 *
3284 *-----------------------------------------------------------------------
3285 */
3286 void
3287 Job_Wait(void)
3288 {
3289 aborting = ABORT_WAIT;
3290 while (nJobs != 0) {
3291 Job_CatchOutput();
3292 #ifndef RMT_WILL_WATCH
3293 Job_CatchChildren(!usePipes);
3294 #endif /* RMT_WILL_WATCH */
3295 }
3296 Job_TokenFlush();
3297 aborting = 0;
3298 }
3299
3300 /*-
3301 *-----------------------------------------------------------------------
3302 * Job_AbortAll --
3303 * Abort all currently running jobs without handling output or anything.
3304 * This function is to be called only in the event of a major
3305 * error. Most definitely NOT to be called from JobInterrupt.
3306 *
3307 * Results:
3308 * None
3309 *
3310 * Side Effects:
3311 * All children are killed, not just the firstborn
3312 *-----------------------------------------------------------------------
3313 */
3314 void
3315 Job_AbortAll(void)
3316 {
3317 LstNode ln; /* element in job table */
3318 Job *job; /* the job descriptor in that element */
3319 int foo;
3320 sigset_t mask;
3321
3322 aborting = ABORT_ERROR;
3323
3324 if (nJobs) {
3325
3326 JobSigLock(&mask);
3327 (void)Lst_Open(jobs);
3328 while ((ln = Lst_Next(jobs)) != NILLNODE) {
3329 job = (Job *)Lst_Datum(ln);
3330
3331 /*
3332 * kill the child process with increasingly drastic signals to make
3333 * darn sure it's dead.
3334 */
3335 #ifdef RMT_WANTS_SIGNALS
3336 if (job->flags & JOB_REMOTE) {
3337 (void)Rmt_Signal(job, SIGINT);
3338 (void)Rmt_Signal(job, SIGKILL);
3339 } else {
3340 KILL(job->pid, SIGINT);
3341 KILL(job->pid, SIGKILL);
3342 }
3343 #else
3344 KILL(job->pid, SIGINT);
3345 KILL(job->pid, SIGKILL);
3346 #endif /* RMT_WANTS_SIGNALS */
3347 }
3348 Lst_Close(jobs);
3349 JobSigUnlock(&mask);
3350 }
3351
3352 /*
3353 * Catch as many children as want to report in at first, then give up
3354 */
3355 while (waitpid((pid_t) -1, &foo, WNOHANG) > 0)
3356 continue;
3357 }
3358
3359 #ifdef REMOTE
3360 /*-
3361 *-----------------------------------------------------------------------
3362 * JobFlagForMigration --
3363 * Handle the eviction of a child. Called from RmtStatusChange.
3364 * Flags the child as remigratable and then suspends it.
3365 *
3366 * Input:
3367 * hostID ID of host we used, for matching children
3368 *
3369 * Results:
3370 * none.
3371 *
3372 * Side Effects:
3373 * The job descriptor is flagged for remigration.
3374 *
3375 *-----------------------------------------------------------------------
3376 */
3377 void
3378 JobFlagForMigration(int hostID)
3379 {
3380 Job *job; /* job descriptor for dead child */
3381 LstNode jnode; /* list element for finding job */
3382
3383 if (DEBUG(JOB)) {
3384 (void)fprintf(stdout, "JobFlagForMigration(%d) called.\n", hostID);
3385 (void)fflush(stdout);
3386 }
3387 jnode = Lst_Find(jobs, (ClientData)&hostID, JobCmpRmtID);
3388
3389 if (jnode == NILLNODE) {
3390 jnode = Lst_Find(stoppedJobs, (ClientData)hostID, JobCmpRmtID);
3391 if (jnode == NILLNODE) {
3392 if (DEBUG(JOB)) {
3393 Error("Evicting host(%d) not in table", hostID);
3394 }
3395 return;
3396 }
3397 }
3398 job = (Job *)Lst_Datum(jnode);
3399
3400 if (DEBUG(JOB)) {
3401 (void)fprintf(stdout,
3402 "JobFlagForMigration(%d) found job '%s'.\n", hostID,
3403 job->node->name);
3404 (void)fflush(stdout);
3405 }
3406
3407 KILL(job->pid, SIGSTOP);
3408
3409 job->flags |= JOB_REMIGRATE;
3410 }
3411
3412 #endif
3413
3414 /*-
3416 *-----------------------------------------------------------------------
3417 * JobRestartJobs --
3418 * Tries to restart stopped jobs if there are slots available.
3419 * Note that this tries to restart them regardless of pending errors.
3420 * It's not good to leave stopped jobs lying around!
3421 *
3422 * Results:
3423 * None.
3424 *
3425 * Side Effects:
3426 * Resumes(and possibly migrates) jobs.
3427 *
3428 *-----------------------------------------------------------------------
3429 */
3430 static void
3431 JobRestartJobs(void)
3432 {
3433 sigset_t mask;
3434
3435 JobSigLock(&mask);
3436 while (!Lst_IsEmpty(stoppedJobs)) {
3437 if (DEBUG(JOB)) {
3438 (void)fprintf(stdout, "Restarting a stopped job.\n");
3439 (void)fflush(stdout);
3440 }
3441 if (JobRestart((Job *)Lst_DeQueue(stoppedJobs)) != 0)
3442 break;
3443 }
3444 JobSigUnlock(&mask);
3445 }
3446
3447 #ifndef RMT_WILL_WATCH
3448 static void
3449 watchfd(Job *job)
3450 {
3451 int i;
3452 if (job->inPollfd != NULL)
3453 Punt("Watching watched job");
3454 if (fds == NULL) {
3455 maxfds = JBSTART;
3456 fds = emalloc(sizeof(struct pollfd) * maxfds);
3457 jobfds = emalloc(sizeof(Job **) * maxfds);
3458
3459 fds[0].fd = job_pipe[0];
3460 fds[0].events = POLLIN;
3461 jobfds[0] = &tokenWaitJob;
3462 tokenWaitJob.inPollfd = &fds[0];
3463 nfds++;
3464
3465 fds[1].fd = exit_pipe[0];
3466 fds[1].events = POLLIN;
3467 jobfds[1] = &childExitJob;
3468 childExitJob.inPollfd = &fds[1];
3469 nfds++;
3470 } else if (nfds == maxfds) {
3471 maxfds *= JBFACTOR;
3472 fds = erealloc(fds, sizeof(struct pollfd) * maxfds);
3473 jobfds = erealloc(jobfds, sizeof(Job **) * maxfds);
3474 for (i = 0; i < nfds; i++)
3475 jobfds[i]->inPollfd = &fds[i];
3476 }
3477
3478 fds[nfds].fd = job->inPipe;
3479 fds[nfds].events = POLLIN;
3480 jobfds[nfds] = job;
3481 job->inPollfd = &fds[nfds];
3482 nfds++;
3483 }
3484
3485 static void
3486 clearfd(Job *job)
3487 {
3488 int i;
3489 if (job->inPollfd == NULL)
3490 Punt("Unwatching unwatched job");
3491 i = job->inPollfd - fds;
3492 nfds--;
3493 /*
3494 * Move last job in table into hole made by dead job.
3495 */
3496 if (nfds != i) {
3497 fds[i] = fds[nfds];
3498 jobfds[i] = jobfds[nfds];
3499 jobfds[i]->inPollfd = &fds[i];
3500 }
3501 job->inPollfd = NULL;
3502 }
3503
3504 static int
3505 readyfd(Job *job)
3506 {
3507 if (job->inPollfd == NULL)
3508 Punt("Polling unwatched job");
3509 return (job->inPollfd->revents & POLLIN) != 0;
3510 }
3511 #endif
3512
3513 /*-
3514 *-----------------------------------------------------------------------
3515 * JobTokenAdd --
3516 * Put a token into the job pipe so that some make process can start
3517 * another job.
3518 *
3519 * Side Effects:
3520 * Allows more build jobs to be spawned somewhere.
3521 *
3522 *-----------------------------------------------------------------------
3523 */
3524
3525 static void
3526 JobTokenAdd(void)
3527 {
3528
3529 if (DEBUG(JOB))
3530 printf("deposit token\n");
3531 write(job_pipe[1], "+", 1);
3532 }
3533
3534 /*-
3535 *-----------------------------------------------------------------------
3536 * Job_ServerStartTokenAdd --
3537 * Prep the job token pipe in the root make process.
3538 *
3539 *-----------------------------------------------------------------------
3540 */
3541
3542 void
3543 Job_ServerStart(int maxproc)
3544 {
3545 int i, fd, flags;
3546 char jobarg[64];
3547
3548 if (pipe(job_pipe) < 0)
3549 Fatal("error in pipe: %s", strerror(errno));
3550
3551 for (i = 0; i < 2; i++) {
3552 /* Avoid using low numbered fds */
3553 fd = fcntl(job_pipe[i], F_DUPFD, 15);
3554 if (fd != -1) {
3555 close(job_pipe[i]);
3556 job_pipe[i] = fd;
3557 }
3558 }
3559
3560 /*
3561 * We mark the input side of the pipe non-blocking; we poll(2) the
3562 * pipe when we're waiting for a job token, but we might lose the
3563 * race for the token when a new one becomes available, so the read
3564 * from the pipe should not block.
3565 */
3566 flags = fcntl(job_pipe[0], F_GETFL, 0);
3567 flags |= O_NONBLOCK;
3568 fcntl(job_pipe[0], F_SETFL, flags);
3569
3570 /*
3571 * Mark job pipes as close-on-exec.
3572 * Note that we will clear this when executing submakes.
3573 */
3574 fcntl(job_pipe[0], F_SETFD, 1);
3575 fcntl(job_pipe[1], F_SETFD, 1);
3576
3577 snprintf(jobarg, sizeof(jobarg), "%d,%d", job_pipe[0], job_pipe[1]);
3578
3579 Var_Append(MAKEFLAGS, "-J", VAR_GLOBAL);
3580 Var_Append(MAKEFLAGS, jobarg, VAR_GLOBAL);
3581
3582 /*
3583 * Preload job_pipe with one token per job, save the one
3584 * "extra" token for the primary job.
3585 *
3586 * XXX should clip maxJobs against PIPE_BUF -- if maxJobs is
3587 * larger than the write buffer size of the pipe, we will
3588 * deadlock here.
3589 */
3590 for (i=1; i < maxproc; i++)
3591 JobTokenAdd();
3592 }
3593
3594 /*
3595 * this tracks the number of tokens currently "out" to build jobs.
3596 */
3597 int jobTokensRunning = 0;
3598 int jobTokensFree = 0;
3599 /*-
3600 *-----------------------------------------------------------------------
3601 * Job_TokenReturn --
3602 * Return a withdrawn token to the pool.
3603 *
3604 *-----------------------------------------------------------------------
3605 */
3606
3607 void
3608 Job_TokenReturn(void)
3609 {
3610 jobTokensRunning--;
3611 if (jobTokensRunning < 0)
3612 Punt("token botch");
3613 if (jobTokensRunning)
3614 jobTokensFree++;
3615 }
3616
3617 /*-
3618 *-----------------------------------------------------------------------
3619 * Job_TokenWithdraw --
3620 * Attempt to withdraw a token from the pool.
3621 *
3622 * Results:
3623 * Returns TRUE if a token was withdrawn, and FALSE if the pool
3624 * is currently empty.
3625 *
3626 * Side Effects:
3627 * If pool is empty, set wantToken so that we wake up
3628 * when a token is released.
3629 *
3630 *-----------------------------------------------------------------------
3631 */
3632
3633
3634 Boolean
3635 Job_TokenWithdraw(void)
3636 {
3637 char tok;
3638 int count;
3639
3640 wantToken = FALSE;
3641
3642 if (aborting)
3643 return FALSE;
3644
3645 if (jobTokensRunning == 0) {
3646 if (DEBUG(JOB))
3647 printf("first one's free\n");
3648 jobTokensRunning++;
3649 return TRUE;
3650 }
3651 if (jobTokensFree > 0) {
3652 jobTokensFree--;
3653 jobTokensRunning++;
3654 return TRUE;
3655 }
3656 count = read(job_pipe[0], &tok, 1);
3657 if (count == 0)
3658 Fatal("eof on job pipe!");
3659 else if (count < 0) {
3660 if (errno != EAGAIN) {
3661 Fatal("job pipe read: %s", strerror(errno));
3662 }
3663 if (DEBUG(JOB))
3664 printf("blocked for token\n");
3665 wantToken = TRUE;
3666 return FALSE;
3667 }
3668 jobTokensRunning++;
3669 if (DEBUG(JOB))
3670 printf("withdrew token\n");
3671 return TRUE;
3672 }
3673
3674 /*-
3675 *-----------------------------------------------------------------------
3676 * Job_TokenFlush --
3677 * Return free tokens to the pool.
3678 *
3679 *-----------------------------------------------------------------------
3680 */
3681
3682 void
3683 Job_TokenFlush(void)
3684 {
3685 if (compatMake) return;
3686
3687 while (jobTokensFree > 0) {
3688 JobTokenAdd();
3689 jobTokensFree--;
3690 }
3691 }
3692
3693 #ifdef USE_SELECT
3694 int
3695 emul_poll(struct pollfd *fd, int nfd, int timeout)
3696 {
3697 fd_set rfds, wfds;
3698 int i, maxfd, nselect, npoll;
3699 struct timeval tv, *tvp;
3700 long usecs;
3701
3702 FD_ZERO(&rfds);
3703 FD_ZERO(&wfds);
3704
3705 maxfd = -1;
3706 for (i = 0; i < nfd; i++) {
3707 fd[i].revents = 0;
3708
3709 if (fd[i].events & POLLIN)
3710 FD_SET(fd[i].fd, &rfds);
3711
3712 if (fd[i].events & POLLOUT)
3713 FD_SET(fd[i].fd, &wfds);
3714
3715 if (fd[i].fd > maxfd)
3716 maxfd = fd[i].fd;
3717 }
3718
3719 if (maxfd >= FD_SETSIZE) {
3720 Punt("Ran out of fd_set slots; "
3721 "recompile with a larger FD_SETSIZE.");
3722 }
3723
3724 if (timeout < 0) {
3725 tvp = NULL;
3726 } else {
3727 usecs = timeout * 1000;
3728 tv.tv_sec = usecs / 1000000;
3729 tv.tv_usec = usecs % 1000000;
3730 tvp = &tv;
3731 }
3732
3733 nselect = select(maxfd + 1, &rfds, &wfds, 0, tvp);
3734
3735 if (nselect <= 0)
3736 return nselect;
3737
3738 npoll = 0;
3739 for (i = 0; i < nfd; i++) {
3740 if (FD_ISSET(fd[i].fd, &rfds))
3741 fd[i].revents |= POLLIN;
3742
3743 if (FD_ISSET(fd[i].fd, &wfds))
3744 fd[i].revents |= POLLOUT;
3745
3746 if (fd[i].revents)
3747 npoll++;
3748 }
3749
3750 return npoll;
3751 }
3752 #endif /* USE_SELECT */
3753