source: trunk/src/kmk/job.c@ 33

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

Basic nmake emulation.

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