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

Last change on this file since 45 was 45, checked in by bird, 22 years ago

KMK changes..

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