VirtualBox

source: kBuild/trunk/src/kmk/job.c@ 9

Last change on this file since 9 was 9, checked in by bird, 23 years ago

Initial revision

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette