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