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

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

GCC EMX.

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