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