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

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

OS2 / VAC308

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