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