o Pacify ``make -f /dev/null -V FOO''.
[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.18 2004/11/24 07:19:14 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         default:
900             break;
901         }
902     } else {
903         done = TRUE;
904     }
905
906
907     if (done &&
908         (aborting != ABORT_ERROR) &&
909         (aborting != ABORT_INTERRUPT) &&
910         (*status == 0))
911     {
912         /*
913          * As long as we aren't aborting and the job didn't return a non-zero
914          * status that we shouldn't ignore, we call Make_Update to update
915          * the parents. In addition, any saved commands for the node are placed
916          * on the .END target.
917          */
918         if (job->tailCmds != NULL) {
919             Lst_ForEachFrom(job->node->commands, job->tailCmds,
920                              JobSaveCommand,
921                             (void *)job->node);
922         }
923         job->node->made = MADE;
924         Make_Update(job->node);
925         free(job);
926     } else if (*status != 0) {
927         errors += 1;
928         free(job);
929     }
930
931     JobRestartJobs();
932
933     /*
934      * Set aborting if any error.
935      */
936     if (errors && !keepgoing && (aborting != ABORT_INTERRUPT)) {
937         /*
938          * If we found any errors in this batch of children and the -k flag
939          * wasn't given, we set the aborting flag so no more jobs get
940          * started.
941          */
942         aborting = ABORT_ERROR;
943     }
944
945     if ((aborting == ABORT_ERROR) && Job_Empty())
946         /*
947          * If we are aborting and the job table is now empty, we finish.
948          */
949         Finish(errors);
950 }
951
952 /*-
953  *-----------------------------------------------------------------------
954  * Job_Touch --
955  *      Touch the given target. Called by JobStart when the -t flag was
956  *      given.  Prints messages unless told to be silent.
957  *
958  * Results:
959  *      None
960  *
961  * Side Effects:
962  *      The data modification of the file is changed. In addition, if the
963  *      file did not exist, it is created.
964  *-----------------------------------------------------------------------
965  */
966 void
967 Job_Touch(GNode *gn, Boolean silent)
968 {
969     int           streamID;     /* ID of stream opened to do the touch */
970     struct utimbuf times;       /* Times for utime() call */
971
972     if (gn->type & (OP_JOIN|OP_USE|OP_EXEC|OP_OPTIONAL)) {
973         /*
974          * .JOIN, .USE, .ZEROTIME and .OPTIONAL targets are "virtual" targets
975          * and, as such, shouldn't really be created.
976          */
977         return;
978     }
979
980     if (!silent) {
981         (void) fprintf(stdout, "touch %s\n", gn->name);
982         (void) fflush(stdout);
983     }
984
985     if (noExecute) {
986         return;
987     }
988
989     if (gn->type & OP_ARCHV) {
990         Arch_Touch(gn);
991     } else if (gn->type & OP_LIB) {
992         Arch_TouchLib(gn);
993     } else {
994         char    *file = gn->path ? gn->path : gn->name;
995
996         times.actime = times.modtime = now;
997         if (utime(file, &times) < 0){
998             streamID = open(file, O_RDWR | O_CREAT, 0666);
999
1000             if (streamID >= 0) {
1001                 char    c;
1002
1003                 /*
1004                  * Read and write a byte to the file to change the
1005                  * modification time, then close the file.
1006                  */
1007                 if (read(streamID, &c, 1) == 1) {
1008                     (void) lseek(streamID, (off_t)0, SEEK_SET);
1009                     (void) write(streamID, &c, 1);
1010                 }
1011
1012                 (void) close(streamID);
1013             } else {
1014                 (void) fprintf(stdout, "*** couldn't touch %s: %s",
1015                                file, strerror(errno));
1016                 (void) fflush(stdout);
1017             }
1018         }
1019     }
1020 }
1021
1022 /*-
1023  *-----------------------------------------------------------------------
1024  * Job_CheckCommands --
1025  *      Make sure the given node has all the commands it needs.
1026  *
1027  * Results:
1028  *      TRUE if the commands list is/was ok.
1029  *
1030  * Side Effects:
1031  *      The node will have commands from the .DEFAULT rule added to it
1032  *      if it needs them.
1033  *-----------------------------------------------------------------------
1034  */
1035 Boolean
1036 Job_CheckCommands(GNode *gn, void (*abortProc)(const char *, ...))
1037 {
1038     if (OP_NOP(gn->type) && Lst_IsEmpty(gn->commands) &&
1039         (gn->type & OP_LIB) == 0) {
1040         /*
1041          * No commands. Look for .DEFAULT rule from which we might infer
1042          * commands
1043          */
1044         if ((DEFAULT != NULL) && !Lst_IsEmpty(DEFAULT->commands)) {
1045             char *p1;
1046             /*
1047              * Make only looks for a .DEFAULT if the node was never the
1048              * target of an operator, so that's what we do too. If
1049              * a .DEFAULT was given, we substitute its commands for gn's
1050              * commands and set the IMPSRC variable to be the target's name
1051              * The DEFAULT node acts like a transformation rule, in that
1052              * gn also inherits any attributes or sources attached to
1053              * .DEFAULT itself.
1054              */
1055             Make_HandleUse(DEFAULT, gn);
1056             Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), gn);
1057             free(p1);
1058         } else if (Dir_MTime(gn) == 0) {
1059             /*
1060              * The node wasn't the target of an operator we have no .DEFAULT
1061              * rule to go on and the target doesn't already exist. There's
1062              * nothing more we can do for this branch. If the -k flag wasn't
1063              * given, we stop in our tracks, otherwise we just don't update
1064              * this node's parents so they never get examined.
1065              */
1066             static const char msg[] = "make: don't know how to make";
1067
1068             if (gn->type & OP_OPTIONAL) {
1069                 (void) fprintf(stdout, "%s %s(ignored)\n", msg, gn->name);
1070                 (void) fflush(stdout);
1071             } else if (keepgoing) {
1072                 (void) fprintf(stdout, "%s %s(continuing)\n", msg, gn->name);
1073                 (void) fflush(stdout);
1074                 return FALSE;
1075             } else {
1076 #if OLD_JOKE
1077                 if (strcmp(gn->name,"love") == 0)
1078                     (*abortProc)("Not war.");
1079                 else
1080 #endif
1081                     (*abortProc)("%s %s. Stop", msg, gn->name);
1082                 return FALSE;
1083             }
1084         }
1085     }
1086     return TRUE;
1087 }
1088 #ifdef RMT_WILL_WATCH
1089 /*-
1090  *-----------------------------------------------------------------------
1091  * JobLocalInput --
1092  *      Handle a pipe becoming readable. Callback function for Rmt_Watch
1093  *
1094  * Results:
1095  *      None
1096  *
1097  * Side Effects:
1098  *      JobDoOutput is called.
1099  *
1100  *-----------------------------------------------------------------------
1101  */
1102 /*ARGSUSED*/
1103 static void
1104 JobLocalInput(int stream, Job *job)
1105 {
1106     JobDoOutput(job, FALSE);
1107 }
1108 #endif /* RMT_WILL_WATCH */
1109
1110 /*-
1111  *-----------------------------------------------------------------------
1112  * JobExec --
1113  *      Execute the shell for the given job. Called from JobStart and
1114  *      JobRestart.
1115  *
1116  * Results:
1117  *      None.
1118  *
1119  * Side Effects:
1120  *      A shell is executed, outputs is altered and the Job structure added
1121  *      to the job table.
1122  *
1123  *-----------------------------------------------------------------------
1124  */
1125 static void
1126 JobExec(Job *job, char **argv)
1127 {
1128     int           cpid;         /* ID of new child */
1129
1130     if (DEBUG(JOB)) {
1131         int       i;
1132
1133         DEBUGF(JOB, ("Running %s %sly\n", job->node->name,
1134                job->flags&JOB_REMOTE?"remote":"local"));
1135         DEBUGF(JOB, ("\tCommand: "));
1136         for (i = 0; argv[i] != NULL; i++) {
1137             DEBUGF(JOB, ("%s ", argv[i]));
1138         }
1139         DEBUGF(JOB, ("\n"));
1140     }
1141
1142     /*
1143      * Some jobs produce no output and it's disconcerting to have
1144      * no feedback of their running (since they produce no output, the
1145      * banner with their name in it never appears). This is an attempt to
1146      * provide that feedback, even if nothing follows it.
1147      */
1148     if ((lastNode != job->node) && (job->flags & JOB_FIRST) &&
1149         !(job->flags & JOB_SILENT)) {
1150         MESSAGE(stdout, job->node);
1151         lastNode = job->node;
1152     }
1153
1154 #ifdef RMT_NO_EXEC
1155     if (job->flags & JOB_REMOTE) {
1156         goto jobExecFinish;
1157     }
1158 #endif /* RMT_NO_EXEC */
1159
1160     if ((cpid = vfork()) == -1) {
1161         Punt("Cannot fork");
1162     } else if (cpid == 0) {
1163
1164         /*
1165          * Must duplicate the input stream down to the child's input and
1166          * reset it to the beginning (again). Since the stream was marked
1167          * close-on-exec, we must clear that bit in the new input.
1168          */
1169         if (dup2(FILENO(job->cmdFILE), 0) == -1)
1170             Punt("Cannot dup2: %s", strerror(errno));
1171         (void) fcntl(0, F_SETFD, 0);
1172         (void) lseek(0, (off_t)0, SEEK_SET);
1173
1174         if (usePipes) {
1175             /*
1176              * Set up the child's output to be routed through the pipe
1177              * we've created for it.
1178              */
1179             if (dup2(job->outPipe, 1) == -1)
1180                 Punt("Cannot dup2: %s", strerror(errno));
1181         } else {
1182             /*
1183              * We're capturing output in a file, so we duplicate the
1184              * descriptor to the temporary file into the standard
1185              * output.
1186              */
1187             if (dup2(job->outFd, 1) == -1)
1188                 Punt("Cannot dup2: %s", strerror(errno));
1189         }
1190         /*
1191          * The output channels are marked close on exec. This bit was
1192          * duplicated by the dup2 (on some systems), so we have to clear
1193          * it before routing the shell's error output to the same place as
1194          * its standard output.
1195          */
1196         (void) fcntl(1, F_SETFD, 0);
1197         if (dup2(1, 2) == -1)
1198             Punt("Cannot dup2: %s", strerror(errno));
1199
1200 #ifdef USE_PGRP
1201         /*
1202          * We want to switch the child into a different process family so
1203          * we can kill it and all its descendants in one fell swoop,
1204          * by killing its process family, but not commit suicide.
1205          */
1206 # if defined(SYSV)
1207         (void) setsid();
1208 # else
1209         (void) setpgid(0, getpid());
1210 # endif
1211 #endif /* USE_PGRP */
1212
1213 #ifdef REMOTE
1214         if (job->flags & JOB_REMOTE) {
1215             Rmt_Exec(shellPath, argv, FALSE);
1216         } else
1217 #endif /* REMOTE */
1218            (void) execv(shellPath, argv);
1219
1220         (void) write(STDERR_FILENO, "Could not execute shell\n",
1221                      sizeof("Could not execute shell"));
1222         _exit(1);
1223     } else {
1224 #ifdef REMOTE
1225         long omask = sigblock(sigmask(SIGCHLD));
1226 #endif
1227         job->pid = cpid;
1228
1229         if (usePipes && (job->flags & JOB_FIRST) ) {
1230             /*
1231              * The first time a job is run for a node, we set the current
1232              * position in the buffer to the beginning and mark another
1233              * stream to watch in the outputs mask
1234              */
1235             job->curPos = 0;
1236
1237 #ifdef RMT_WILL_WATCH
1238             Rmt_Watch(job->inPipe, JobLocalInput, job);
1239 #else
1240             FD_SET(job->inPipe, &outputs);
1241 #endif /* RMT_WILL_WATCH */
1242         }
1243
1244         if (job->flags & JOB_REMOTE) {
1245 #ifndef REMOTE
1246             job->rmtID = 0;
1247 #else
1248             job->rmtID = Rmt_LastID(job->pid);
1249 #endif /* REMOTE */
1250         } else {
1251             nLocal += 1;
1252             /*
1253              * XXX: Used to not happen if REMOTE. Why?
1254              */
1255             if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
1256                 (void) fclose(job->cmdFILE);
1257                 job->cmdFILE = NULL;
1258             }
1259         }
1260 #ifdef REMOTE
1261         (void) sigsetmask(omask);
1262 #endif
1263     }
1264
1265 #ifdef RMT_NO_EXEC
1266 jobExecFinish:
1267 #endif
1268     /*
1269      * Now the job is actually running, add it to the table.
1270      */
1271     nJobs += 1;
1272     (void) Lst_AtEnd(jobs, (void *)job);
1273     if (nJobs == maxJobs) {
1274         jobFull = TRUE;
1275     }
1276 }
1277
1278 /*-
1279  *-----------------------------------------------------------------------
1280  * JobMakeArgv --
1281  *      Create the argv needed to execute the shell for a given job.
1282  *
1283  *
1284  * Results:
1285  *
1286  * Side Effects:
1287  *
1288  *-----------------------------------------------------------------------
1289  */
1290 static void
1291 JobMakeArgv(Job *job, char **argv)
1292 {
1293     int           argc;
1294     static char   args[10];     /* For merged arguments */
1295
1296     argv[0] = shellName;
1297     argc = 1;
1298
1299     if ((commandShell->exit && (*commandShell->exit != '-')) ||
1300         (commandShell->echo && (*commandShell->echo != '-')))
1301     {
1302         /*
1303          * At least one of the flags doesn't have a minus before it, so
1304          * merge them together. Have to do this because the *(&(@*#*&#$#
1305          * Bourne shell thinks its second argument is a file to source.
1306          * Grrrr. Note the ten-character limitation on the combined arguments.
1307          */
1308         (void)sprintf(args, "-%s%s",
1309                       ((job->flags & JOB_IGNERR) ? "" :
1310                        (commandShell->exit ? commandShell->exit : "")),
1311                       ((job->flags & JOB_SILENT) ? "" :
1312                        (commandShell->echo ? commandShell->echo : "")));
1313
1314         if (args[1]) {
1315             argv[argc] = args;
1316             argc++;
1317         }
1318     } else {
1319         if (!(job->flags & JOB_IGNERR) && commandShell->exit) {
1320             argv[argc] = commandShell->exit;
1321             argc++;
1322         }
1323         if (!(job->flags & JOB_SILENT) && commandShell->echo) {
1324             argv[argc] = commandShell->echo;
1325             argc++;
1326         }
1327     }
1328     argv[argc] = NULL;
1329 }
1330
1331 /*-
1332  *-----------------------------------------------------------------------
1333  * JobRestart --
1334  *      Restart a job that stopped for some reason.
1335  *
1336  * Results:
1337  *      None.
1338  *
1339  * Side Effects:
1340  *      jobFull will be set if the job couldn't be run.
1341  *
1342  *-----------------------------------------------------------------------
1343  */
1344 static void
1345 JobRestart(Job *job)
1346 {
1347 #ifdef REMOTE
1348     int host;
1349 #endif
1350
1351     if (job->flags & JOB_REMIGRATE) {
1352         if (
1353 #ifdef REMOTE
1354             verboseRemigrates ||
1355 #endif
1356             DEBUG(JOB)) {
1357            (void) fprintf(stdout, "*** remigrating %x(%s)\n",
1358                            job->pid, job->node->name);
1359            (void) fflush(stdout);
1360         }
1361
1362 #ifdef REMOTE
1363         if (!Rmt_ReExport(job->pid, job->node, &host)) {
1364             if (verboseRemigrates || DEBUG(JOB)) {
1365                 (void) fprintf(stdout, "*** couldn't migrate...\n");
1366                 (void) fflush(stdout);
1367             }
1368 #endif
1369             if (nLocal != maxLocal) {
1370                 /*
1371                  * Job cannot be remigrated, but there's room on the local
1372                  * machine, so resume the job and note that another
1373                  * local job has started.
1374                  */
1375                 if (
1376 #ifdef REMOTE
1377                     verboseRemigrates ||
1378 #endif
1379                     DEBUG(JOB)) {
1380                     (void) fprintf(stdout, "*** resuming on local machine\n");
1381                     (void) fflush(stdout);
1382                 }
1383                 KILL(job->pid, SIGCONT);
1384                 nLocal +=1;
1385 #ifdef REMOTE
1386                 job->flags &= ~(JOB_REMIGRATE|JOB_RESUME|JOB_REMOTE);
1387                 job->flags |= JOB_CONTINUING;
1388 #else
1389                 job->flags &= ~(JOB_REMIGRATE|JOB_RESUME);
1390 #endif
1391         } else {
1392                 /*
1393                  * Job cannot be restarted. Mark the table as full and
1394                  * place the job back on the list of stopped jobs.
1395                  */
1396                 if (
1397 #ifdef REMOTE
1398                     verboseRemigrates ||
1399 #endif
1400                     DEBUG(JOB)) {
1401                    (void) fprintf(stdout, "*** holding\n");
1402                    (void) fflush(stdout);
1403                 }
1404                 (void)Lst_AtFront(stoppedJobs, (void *)job);
1405                 jobFull = TRUE;
1406                 DEBUGF(JOB, ("Job queue is full.\n"));
1407                 return;
1408             }
1409 #ifdef REMOTE
1410         } else {
1411             /*
1412              * Clear out the remigrate and resume flags. Set the continuing
1413              * flag so we know later on that the process isn't exiting just
1414              * because of a signal.
1415              */
1416             job->flags &= ~(JOB_REMIGRATE|JOB_RESUME);
1417             job->flags |= JOB_CONTINUING;
1418             job->rmtID = host;
1419         }
1420 #endif
1421
1422         (void)Lst_AtEnd(jobs, (void *)job);
1423         nJobs += 1;
1424         if (nJobs == maxJobs) {
1425             jobFull = TRUE;
1426             DEBUGF(JOB, ("Job queue is full.\n"));
1427         }
1428     } else if (job->flags & JOB_RESTART) {
1429         /*
1430          * Set up the control arguments to the shell. This is based on the
1431          * flags set earlier for this job. If the JOB_IGNERR flag is clear,
1432          * the 'exit' flag of the commandShell is used to cause it to exit
1433          * upon receiving an error. If the JOB_SILENT flag is clear, the
1434          * 'echo' flag of the commandShell is used to get it to start echoing
1435          * as soon as it starts processing commands.
1436          */
1437         char      *argv[4];
1438
1439         JobMakeArgv(job, argv);
1440
1441         DEBUGF(JOB, ("Restarting %s...", job->node->name));
1442 #ifdef REMOTE
1443         if ((job->node->type&OP_NOEXPORT) ||
1444             (nLocal < maxLocal && runLocalFirst)
1445 # ifdef RMT_NO_EXEC
1446             || !Rmt_Export(shellPath, argv, job)
1447 # else
1448             || !Rmt_Begin(shellPath, argv, job->node)
1449 # endif
1450 #endif
1451         {
1452             if (((nLocal >= maxLocal) && !(job->flags & JOB_SPECIAL))) {
1453                 /*
1454                  * Can't be exported and not allowed to run locally -- put it
1455                  * back on the hold queue and mark the table full
1456                  */
1457                 DEBUGF(JOB, ("holding\n"));
1458                 (void)Lst_AtFront(stoppedJobs, (void *)job);
1459                 jobFull = TRUE;
1460                 DEBUGF(JOB, ("Job queue is full.\n"));
1461                 return;
1462             } else {
1463                 /*
1464                  * Job may be run locally.
1465                  */
1466                 DEBUGF(JOB, ("running locally\n"));
1467                 job->flags &= ~JOB_REMOTE;
1468             }
1469         }
1470 #ifdef REMOTE
1471         else {
1472             /*
1473              * Can be exported. Hooray!
1474              */
1475             DEBUGF(JOB, ("exporting\n"));
1476             job->flags |= JOB_REMOTE;
1477         }
1478 #endif
1479         JobExec(job, argv);
1480     } else {
1481         /*
1482          * The job has stopped and needs to be restarted. Why it stopped,
1483          * we don't know...
1484          */
1485         DEBUGF(JOB, ("Resuming %s...", job->node->name));
1486         if (((job->flags & JOB_REMOTE) ||
1487             (nLocal < maxLocal) ||
1488 #ifdef REMOTE
1489             (((job->flags & JOB_SPECIAL) &&
1490               (job->node->type & OP_NOEXPORT)) &&
1491              (maxLocal == 0))) &&
1492 #else
1493             ((job->flags & JOB_SPECIAL) &&
1494              (maxLocal == 0))) &&
1495 #endif
1496            (nJobs != maxJobs))
1497         {
1498             /*
1499              * If the job is remote, it's ok to resume it as long as the
1500              * maximum concurrency won't be exceeded. If it's local and
1501              * we haven't reached the local concurrency limit already (or the
1502              * job must be run locally and maxLocal is 0), it's also ok to
1503              * resume it.
1504              */
1505             Boolean error;
1506             int status;
1507
1508 #ifdef RMT_WANTS_SIGNALS
1509             if (job->flags & JOB_REMOTE) {
1510                 error = !Rmt_Signal(job, SIGCONT);
1511             } else
1512 #endif  /* RMT_WANTS_SIGNALS */
1513                 error = (KILL(job->pid, SIGCONT) != 0);
1514
1515             if (!error) {
1516                 /*
1517                  * Make sure the user knows we've continued the beast and
1518                  * actually put the thing in the job table.
1519                  */
1520                 job->flags |= JOB_CONTINUING;
1521                 W_SETTERMSIG(&status, SIGCONT);
1522                 JobFinish(job, &status);
1523
1524                 job->flags &= ~(JOB_RESUME|JOB_CONTINUING);
1525                 DEBUGF(JOB, ("done\n"));
1526             } else {
1527                 Error("couldn't resume %s: %s",
1528                     job->node->name, strerror(errno));
1529                 status = 0;
1530                 W_SETEXITSTATUS(&status, 1);
1531                 JobFinish(job, &status);
1532             }
1533         } else {
1534             /*
1535              * Job cannot be restarted. Mark the table as full and
1536              * place the job back on the list of stopped jobs.
1537              */
1538             DEBUGF(JOB, ("table full\n"));
1539             (void) Lst_AtFront(stoppedJobs, (void *)job);
1540             jobFull = TRUE;
1541             DEBUGF(JOB, ("Job queue is full.\n"));
1542         }
1543     }
1544 }
1545
1546 /*-
1547  *-----------------------------------------------------------------------
1548  * JobStart  --
1549  *      Start a target-creation process going for the target described
1550  *      by the graph node gn.
1551  *
1552  * Results:
1553  *      JOB_ERROR if there was an error in the commands, JOB_FINISHED
1554  *      if there isn't actually anything left to do for the job and
1555  *      JOB_RUNNING if the job has been started.
1556  *
1557  * Side Effects:
1558  *      A new Job node is created and added to the list of running
1559  *      jobs. PMake is forked and a child shell created.
1560  *-----------------------------------------------------------------------
1561  */
1562 static int
1563 JobStart(GNode *gn, int flags, Job *previous)
1564 {
1565     Job           *job;       /* new job descriptor */
1566     char          *argv[4];   /* Argument vector to shell */
1567     Boolean       cmdsOK;     /* true if the nodes commands were all right */
1568     Boolean       local;      /* Set true if the job was run locally */
1569     Boolean       noExec;     /* Set true if we decide not to run the job */
1570     int           tfd;        /* File descriptor for temp file */
1571
1572     if (previous != NULL) {
1573         previous->flags &= ~(JOB_FIRST|JOB_IGNERR|JOB_SILENT|JOB_REMOTE);
1574         job = previous;
1575     } else {
1576         job = (Job *) emalloc(sizeof(Job));
1577         flags |= JOB_FIRST;
1578     }
1579
1580     job->node = gn;
1581     job->tailCmds = NULL;
1582
1583     /*
1584      * Set the initial value of the flags for this job based on the global
1585      * ones and the node's attributes... Any flags supplied by the caller
1586      * are also added to the field.
1587      */
1588     job->flags = 0;
1589     if (Targ_Ignore(gn)) {
1590         job->flags |= JOB_IGNERR;
1591     }
1592     if (Targ_Silent(gn)) {
1593         job->flags |= JOB_SILENT;
1594     }
1595     job->flags |= flags;
1596
1597     /*
1598      * Check the commands now so any attributes from .DEFAULT have a chance
1599      * to migrate to the node
1600      */
1601     if (!compatMake && job->flags & JOB_FIRST) {
1602         cmdsOK = Job_CheckCommands(gn, Error);
1603     } else {
1604         cmdsOK = TRUE;
1605     }
1606
1607     /*
1608      * If the -n flag wasn't given, we open up OUR (not the child's)
1609      * temporary file to stuff commands in it. The thing is rd/wr so we don't
1610      * need to reopen it to feed it to the shell. If the -n flag *was* given,
1611      * we just set the file to be stdout. Cute, huh?
1612      */
1613     if ((gn->type & OP_MAKE) || (!noExecute && !touchFlag)) {
1614         /*
1615          * We're serious here, but if the commands were bogus, we're
1616          * also dead...
1617          */
1618         if (!cmdsOK) {
1619             DieHorribly();
1620         }
1621
1622         (void) strcpy(tfile, TMPPAT);
1623         if ((tfd = mkstemp(tfile)) == -1)
1624             Punt("Cannot create temp file: %s", strerror(errno));
1625         job->cmdFILE = fdopen(tfd, "w+");
1626         eunlink(tfile);
1627         if (job->cmdFILE == NULL) {
1628             close(tfd);
1629             Punt("Could not open %s", tfile);
1630         }
1631         (void) fcntl(FILENO(job->cmdFILE), F_SETFD, 1);
1632         /*
1633          * Send the commands to the command file, flush all its buffers then
1634          * rewind and remove the thing.
1635          */
1636         noExec = FALSE;
1637
1638         /*
1639          * used to be backwards; replace when start doing multiple commands
1640          * per shell.
1641          */
1642         if (compatMake) {
1643             /*
1644              * Be compatible: If this is the first time for this node,
1645              * verify its commands are ok and open the commands list for
1646              * sequential access by later invocations of JobStart.
1647              * Once that is done, we take the next command off the list
1648              * and print it to the command file. If the command was an
1649              * ellipsis, note that there's nothing more to execute.
1650              */
1651             if ((job->flags&JOB_FIRST) && (Lst_Open(gn->commands) != SUCCESS)){
1652                 cmdsOK = FALSE;
1653             } else {
1654                 LstNode ln = Lst_Next(gn->commands);
1655
1656                 if ((ln == NULL) ||
1657                     JobPrintCommand((void *) Lst_Datum(ln),
1658                                     (void *) job))
1659                 {
1660                     noExec = TRUE;
1661                     Lst_Close(gn->commands);
1662                 }
1663                 if (noExec && !(job->flags & JOB_FIRST)) {
1664                     /*
1665                      * If we're not going to execute anything, the job
1666                      * is done and we need to close down the various
1667                      * file descriptors we've opened for output, then
1668                      * call JobDoOutput to catch the final characters or
1669                      * send the file to the screen... Note that the i/o streams
1670                      * are only open if this isn't the first job.
1671                      * Note also that this could not be done in
1672                      * Job_CatchChildren b/c it wasn't clear if there were
1673                      * more commands to execute or not...
1674                      */
1675                     JobClose(job);
1676                 }
1677             }
1678         } else {
1679             /*
1680              * We can do all the commands at once. hooray for sanity
1681              */
1682             numCommands = 0;
1683             Lst_ForEach(gn->commands, JobPrintCommand, (void *)job);
1684
1685             /*
1686              * If we didn't print out any commands to the shell script,
1687              * there's not much point in executing the shell, is there?
1688              */
1689             if (numCommands == 0) {
1690                 noExec = TRUE;
1691             }
1692         }
1693     } else if (noExecute) {
1694         /*
1695          * Not executing anything -- just print all the commands to stdout
1696          * in one fell swoop. This will still set up job->tailCmds correctly.
1697          */
1698         if (lastNode != gn) {
1699             MESSAGE(stdout, gn);
1700             lastNode = gn;
1701         }
1702         job->cmdFILE = stdout;
1703         /*
1704          * Only print the commands if they're ok, but don't die if they're
1705          * not -- just let the user know they're bad and keep going. It
1706          * doesn't do any harm in this case and may do some good.
1707          */
1708         if (cmdsOK) {
1709             Lst_ForEach(gn->commands, JobPrintCommand, (void *)job);
1710         }
1711         /*
1712          * Don't execute the shell, thank you.
1713          */
1714         noExec = TRUE;
1715     } else {
1716         /*
1717          * Just touch the target and note that no shell should be executed.
1718          * Set cmdFILE to stdout to make life easier. Check the commands, too,
1719          * but don't die if they're no good -- it does no harm to keep working
1720          * up the graph.
1721          */
1722         job->cmdFILE = stdout;
1723         Job_Touch(gn, job->flags&JOB_SILENT);
1724         noExec = TRUE;
1725     }
1726
1727     /*
1728      * If we're not supposed to execute a shell, don't.
1729      */
1730     if (noExec) {
1731         /*
1732          * Unlink and close the command file if we opened one
1733          */
1734         if (job->cmdFILE != stdout) {
1735             if (job->cmdFILE != NULL)
1736                 (void) fclose(job->cmdFILE);
1737         } else {
1738              (void) fflush(stdout);
1739         }
1740
1741         /*
1742          * We only want to work our way up the graph if we aren't here because
1743          * the commands for the job were no good.
1744          */
1745         if (cmdsOK) {
1746             if (aborting == 0) {
1747                 if (job->tailCmds != NULL) {
1748                     Lst_ForEachFrom(job->node->commands, job->tailCmds,
1749                                     JobSaveCommand,
1750                                    (void *)job->node);
1751                 }
1752                 job->node->made = MADE;
1753                 Make_Update(job->node);
1754             }
1755             free(job);
1756             return(JOB_FINISHED);
1757         } else {
1758             free(job);
1759             return(JOB_ERROR);
1760         }
1761     } else {
1762         (void) fflush(job->cmdFILE);
1763     }
1764
1765     /*
1766      * Set up the control arguments to the shell. This is based on the flags
1767      * set earlier for this job.
1768      */
1769     JobMakeArgv(job, argv);
1770
1771     /*
1772      * If we're using pipes to catch output, create the pipe by which we'll
1773      * get the shell's output. If we're using files, print out that we're
1774      * starting a job and then set up its temporary-file name.
1775      */
1776     if (!compatMake || (job->flags & JOB_FIRST)) {
1777         if (usePipes) {
1778             int fd[2];
1779             if (pipe(fd) == -1)
1780                 Punt("Cannot create pipe: %s", strerror(errno));
1781             job->inPipe = fd[0];
1782             job->outPipe = fd[1];
1783             (void) fcntl(job->inPipe, F_SETFD, 1);
1784             (void) fcntl(job->outPipe, F_SETFD, 1);
1785         } else {
1786             (void) fprintf(stdout, "Remaking `%s'\n", gn->name);
1787             (void) fflush(stdout);
1788             (void) strcpy(job->outFile, TMPPAT);
1789             if ((job->outFd = mkstemp(job->outFile)) == -1)
1790                 Punt("cannot create temp file: %s", strerror(errno));
1791             (void) fcntl(job->outFd, F_SETFD, 1);
1792         }
1793     }
1794
1795 #ifdef REMOTE
1796     if (!(gn->type & OP_NOEXPORT) && !(runLocalFirst && nLocal < maxLocal)) {
1797 #ifdef RMT_NO_EXEC
1798         local = !Rmt_Export(shellPath, argv, job);
1799 #else
1800         local = !Rmt_Begin(shellPath, argv, job->node);
1801 #endif /* RMT_NO_EXEC */
1802         if (!local) {
1803             job->flags |= JOB_REMOTE;
1804         }
1805     } else
1806 #endif
1807         local = TRUE;
1808
1809     if (local && (((nLocal >= maxLocal) &&
1810         !(job->flags & JOB_SPECIAL) &&
1811 #ifdef REMOTE
1812         (!(gn->type & OP_NOEXPORT) || (maxLocal != 0))
1813 #else
1814         (maxLocal != 0)
1815 #endif
1816         )))
1817     {
1818         /*
1819          * The job can only be run locally, but we've hit the limit of
1820          * local concurrency, so put the job on hold until some other job
1821          * finishes. Note that the special jobs (.BEGIN, .INTERRUPT and .END)
1822          * may be run locally even when the local limit has been reached
1823          * (e.g. when maxLocal == 0), though they will be exported if at
1824          * all possible. In addition, any target marked with .NOEXPORT will
1825          * be run locally if maxLocal is 0.
1826          */
1827         jobFull = TRUE;
1828
1829         DEBUGF(JOB, ("Can only run job locally.\n"));
1830         job->flags |= JOB_RESTART;
1831         (void) Lst_AtEnd(stoppedJobs, (void *)job);
1832     } else {
1833         if ((nLocal >= maxLocal) && local) {
1834             /*
1835              * If we're running this job locally as a special case (see above),
1836              * at least say the table is full.
1837              */
1838             jobFull = TRUE;
1839             DEBUGF(JOB, ("Local job queue is full.\n"));
1840         }
1841         JobExec(job, argv);
1842     }
1843     return(JOB_RUNNING);
1844 }
1845
1846 static char *
1847 JobOutput(Job *job, char *cp, char *endp, int msg)
1848 {
1849     char *ecp;
1850
1851     if (commandShell->noPrint) {
1852         ecp = strstr(cp, commandShell->noPrint);
1853         while (ecp != NULL) {
1854             if (cp != ecp) {
1855                 *ecp = '\0';
1856                 if (msg && job->node != lastNode) {
1857                     MESSAGE(stdout, job->node);
1858                     lastNode = job->node;
1859                 }
1860                 /*
1861                  * The only way there wouldn't be a newline after
1862                  * this line is if it were the last in the buffer.
1863                  * however, since the non-printable comes after it,
1864                  * there must be a newline, so we don't print one.
1865                  */
1866                 (void) fprintf(stdout, "%s", cp);
1867                 (void) fflush(stdout);
1868             }
1869             cp = ecp + commandShell->noPLen;
1870             if (cp != endp) {
1871                 /*
1872                  * Still more to print, look again after skipping
1873                  * the whitespace following the non-printable
1874                  * command....
1875                  */
1876                 cp++;
1877                 while (*cp == ' ' || *cp == '\t' || *cp == '\n') {
1878                     cp++;
1879                 }
1880                 ecp = strstr(cp, commandShell->noPrint);
1881             } else {
1882                 return cp;
1883             }
1884         }
1885     }
1886     return cp;
1887 }
1888
1889 /*-
1890  *-----------------------------------------------------------------------
1891  * JobDoOutput  --
1892  *      This function is called at different times depending on
1893  *      whether the user has specified that output is to be collected
1894  *      via pipes or temporary files. In the former case, we are called
1895  *      whenever there is something to read on the pipe. We collect more
1896  *      output from the given job and store it in the job's outBuf. If
1897  *      this makes up a line, we print it tagged by the job's identifier,
1898  *      as necessary.
1899  *      If output has been collected in a temporary file, we open the
1900  *      file and read it line by line, transfering it to our own
1901  *      output channel until the file is empty. At which point we
1902  *      remove the temporary file.
1903  *      In both cases, however, we keep our figurative eye out for the
1904  *      'noPrint' line for the shell from which the output came. If
1905  *      we recognize a line, we don't print it. If the command is not
1906  *      alone on the line (the character after it is not \0 or \n), we
1907  *      do print whatever follows it.
1908  *
1909  * Results:
1910  *      None
1911  *
1912  * Side Effects:
1913  *      curPos may be shifted as may the contents of outBuf.
1914  *-----------------------------------------------------------------------
1915  */
1916 STATIC void
1917 JobDoOutput(Job *job, Boolean finish)
1918 {
1919     Boolean       gotNL = FALSE;  /* true if got a newline */
1920     Boolean       fbuf;           /* true if our buffer filled up */
1921     int           nr;             /* number of bytes read */
1922     int           i;              /* auxiliary index into outBuf */
1923     int           max;            /* limit for i (end of current data) */
1924     int           nRead;          /* (Temporary) number of bytes read */
1925
1926     FILE          *oFILE;         /* Stream pointer to shell's output file */
1927     char          inLine[132];
1928
1929
1930     if (usePipes) {
1931         /*
1932          * Read as many bytes as will fit in the buffer.
1933          */
1934 end_loop:
1935         gotNL = FALSE;
1936         fbuf = FALSE;
1937
1938         nRead = read(job->inPipe, &job->outBuf[job->curPos],
1939                          JOB_BUFSIZE - job->curPos);
1940         if (nRead < 0) {
1941             DEBUGF(JOB, ("JobDoOutput(piperead)"));
1942             nr = 0;
1943         } else {
1944             nr = nRead;
1945         }
1946
1947         /*
1948          * If we hit the end-of-file (the job is dead), we must flush its
1949          * remaining output, so pretend we read a newline if there's any
1950          * output remaining in the buffer.
1951          * Also clear the 'finish' flag so we stop looping.
1952          */
1953         if ((nr == 0) && (job->curPos != 0)) {
1954             job->outBuf[job->curPos] = '\n';
1955             nr = 1;
1956             finish = FALSE;
1957         } else if (nr == 0) {
1958             finish = FALSE;
1959         }
1960
1961         /*
1962          * Look for the last newline in the bytes we just got. If there is
1963          * one, break out of the loop with 'i' as its index and gotNL set
1964          * TRUE.
1965          */
1966         max = job->curPos + nr;
1967         for (i = job->curPos + nr - 1; i >= job->curPos; i--) {
1968             if (job->outBuf[i] == '\n') {
1969                 gotNL = TRUE;
1970                 break;
1971             } else if (job->outBuf[i] == '\0') {
1972                 /*
1973                  * Why?
1974                  */
1975                 job->outBuf[i] = ' ';
1976             }
1977         }
1978
1979         if (!gotNL) {
1980             job->curPos += nr;
1981             if (job->curPos == JOB_BUFSIZE) {
1982                 /*
1983                  * If we've run out of buffer space, we have no choice
1984                  * but to print the stuff. sigh.
1985                  */
1986                 fbuf = TRUE;
1987                 i = job->curPos;
1988             }
1989         }
1990         if (gotNL || fbuf) {
1991             /*
1992              * Need to send the output to the screen. Null terminate it
1993              * first, overwriting the newline character if there was one.
1994              * So long as the line isn't one we should filter (according
1995              * to the shell description), we print the line, preceded
1996              * by a target banner if this target isn't the same as the
1997              * one for which we last printed something.
1998              * The rest of the data in the buffer are then shifted down
1999              * to the start of the buffer and curPos is set accordingly.
2000              */
2001             job->outBuf[i] = '\0';
2002             if (i >= job->curPos) {
2003                 char *cp;
2004
2005                 cp = JobOutput(job, job->outBuf, &job->outBuf[i], FALSE);
2006
2007                 /*
2008                  * There's still more in that thar buffer. This time, though,
2009                  * we know there's no newline at the end, so we add one of
2010                  * our own free will.
2011                  */
2012                 if (*cp != '\0') {
2013                     if (job->node != lastNode) {
2014                         MESSAGE(stdout, job->node);
2015                         lastNode = job->node;
2016                     }
2017                     (void) fprintf(stdout, "%s%s", cp, gotNL ? "\n" : "");
2018                     (void) fflush(stdout);
2019                 }
2020             }
2021             if (i < max - 1) {
2022                 /* shift the remaining characters down */
2023                 (void) memcpy(job->outBuf, &job->outBuf[i + 1], max - (i + 1));
2024                 job->curPos = max - (i + 1);
2025
2026             } else {
2027                 /*
2028                  * We have written everything out, so we just start over
2029                  * from the start of the buffer. No copying. No nothing.
2030                  */
2031                 job->curPos = 0;
2032             }
2033         }
2034         if (finish) {
2035             /*
2036              * If the finish flag is true, we must loop until we hit
2037              * end-of-file on the pipe. This is guaranteed to happen
2038              * eventually since the other end of the pipe is now closed
2039              * (we closed it explicitly and the child has exited). When
2040              * we do get an EOF, finish will be set FALSE and we'll fall
2041              * through and out.
2042              */
2043             goto end_loop;
2044         }
2045     } else {
2046         /*
2047          * We've been called to retrieve the output of the job from the
2048          * temporary file where it's been squirreled away. This consists of
2049          * opening the file, reading the output line by line, being sure not
2050          * to print the noPrint line for the shell we used, then close and
2051          * remove the temporary file. Very simple.
2052          *
2053          * Change to read in blocks and do FindSubString type things as for
2054          * pipes? That would allow for "@echo -n..."
2055          */
2056         oFILE = fopen(job->outFile, "r");
2057         if (oFILE != NULL) {
2058             (void) fprintf(stdout, "Results of making %s:\n", job->node->name);
2059             (void) fflush(stdout);
2060             while (fgets(inLine, sizeof(inLine), oFILE) != NULL) {
2061                 char    *cp, *endp, *oendp;
2062
2063                 cp = inLine;
2064                 oendp = endp = inLine + strlen(inLine);
2065                 if (endp[-1] == '\n') {
2066                     *--endp = '\0';
2067                 }
2068                 cp = JobOutput(job, inLine, endp, FALSE);
2069
2070                 /*
2071                  * There's still more in that thar buffer. This time, though,
2072                  * we know there's no newline at the end, so we add one of
2073                  * our own free will.
2074                  */
2075                 (void) fprintf(stdout, "%s", cp);
2076                 (void) fflush(stdout);
2077                 if (endp != oendp) {
2078                     (void) fprintf(stdout, "\n");
2079                     (void) fflush(stdout);
2080                 }
2081             }
2082             (void) fclose(oFILE);
2083             (void) eunlink(job->outFile);
2084         }
2085     }
2086 }
2087
2088 /*-
2089  *-----------------------------------------------------------------------
2090  * Job_CatchChildren --
2091  *      Handle the exit of a child. Called from Make_Make.
2092  *
2093  * Results:
2094  *      none.
2095  *
2096  * Side Effects:
2097  *      The job descriptor is removed from the list of children.
2098  *
2099  * Notes:
2100  *      We do waits, blocking or not, according to the wisdom of our
2101  *      caller, until there are no more children to report. For each
2102  *      job, call JobFinish to finish things off. This will take care of
2103  *      putting jobs on the stoppedJobs queue.
2104  *
2105  *-----------------------------------------------------------------------
2106  */
2107 void
2108 Job_CatchChildren(Boolean block)
2109 {
2110     int           pid;          /* pid of dead child */
2111     Job           *job;         /* job descriptor for dead child */
2112     LstNode       jnode;        /* list element for finding job */
2113     int           status;       /* Exit/termination status */
2114
2115     /*
2116      * Don't even bother if we know there's no one around.
2117      */
2118     if (nLocal == 0) {
2119         return;
2120     }
2121
2122     while ((pid = waitpid((pid_t) -1, &status,
2123                           (block?0:WNOHANG)|WUNTRACED)) > 0)
2124     {
2125         DEBUGF(JOB, ("Process %d exited or stopped.\n", pid));
2126
2127         jnode = Lst_Find(jobs, (void *)&pid, JobCmpPid);
2128
2129         if (jnode == NULL) {
2130             if (WIFSIGNALED(status) && (WTERMSIG(status) == SIGCONT)) {
2131                 jnode = Lst_Find(stoppedJobs, (void *) &pid, JobCmpPid);
2132                 if (jnode == NULL) {
2133                     Error("Resumed child (%d) not in table", pid);
2134                     continue;
2135                 }
2136                 job = (Job *)Lst_Datum(jnode);
2137                 (void) Lst_Remove(stoppedJobs, jnode);
2138             } else {
2139                 Error("Child (%d) not in table?", pid);
2140                 continue;
2141             }
2142         } else {
2143             job = (Job *) Lst_Datum(jnode);
2144             (void) Lst_Remove(jobs, jnode);
2145             nJobs -= 1;
2146             DEBUGF(JOB, ("Job queue is no longer full.\n"));
2147             jobFull = FALSE;
2148 #ifdef REMOTE
2149             if (!(job->flags & JOB_REMOTE)) {
2150                 DEBUGF(JOB, ("Job queue has one fewer local process.\n"));
2151                 nLocal -= 1;
2152             }
2153 #else
2154             nLocal -= 1;
2155 #endif
2156         }
2157
2158         JobFinish(job, &status);
2159     }
2160 }
2161
2162 /*-
2163  *-----------------------------------------------------------------------
2164  * Job_CatchOutput --
2165  *      Catch the output from our children, if we're using
2166  *      pipes do so. Otherwise just block time until we get a
2167  *      signal (most likely a SIGCHLD) since there's no point in
2168  *      just spinning when there's nothing to do and the reaping
2169  *      of a child can wait for a while.
2170  *
2171  * Results:
2172  *      None
2173  *
2174  * Side Effects:
2175  *      Output is read from pipes if we're piping.
2176  * -----------------------------------------------------------------------
2177  */
2178 void
2179 Job_CatchOutput(void)
2180 {
2181     int                   nfds;
2182     struct timeval        timeout;
2183     fd_set                readfds;
2184     LstNode               ln;
2185     Job                   *job;
2186 #ifdef RMT_WILL_WATCH
2187     int                   pnJobs;       /* Previous nJobs */
2188 #endif
2189
2190     (void) fflush(stdout);
2191 #ifdef RMT_WILL_WATCH
2192     pnJobs = nJobs;
2193
2194     /*
2195      * It is possible for us to be called with nJobs equal to 0. This happens
2196      * if all the jobs finish and a job that is stopped cannot be run
2197      * locally (eg if maxLocal is 0) and cannot be exported. The job will
2198      * be placed back on the stoppedJobs queue, Job_Empty() will return false,
2199      * Make_Run will call us again when there's nothing for which to wait.
2200      * nJobs never changes, so we loop forever. Hence the check. It could
2201      * be argued that we should sleep for a bit so as not to swamp the
2202      * exportation system with requests. Perhaps we should.
2203      *
2204      * NOTE: IT IS THE RESPONSIBILITY OF Rmt_Wait TO CALL Job_CatchChildren
2205      * IN A TIMELY FASHION TO CATCH ANY LOCALLY RUNNING JOBS THAT EXIT.
2206      * It may use the variable nLocal to determine if it needs to call
2207      * Job_CatchChildren (if nLocal is 0, there's nothing for which to
2208      * wait...)
2209      */
2210     while (nJobs != 0 && pnJobs == nJobs) {
2211         Rmt_Wait();
2212     }
2213 #else
2214     if (usePipes) {
2215         readfds = outputs;
2216         timeout.tv_sec = SEL_SEC;
2217         timeout.tv_usec = SEL_USEC;
2218
2219         if ((nfds = select(FD_SETSIZE, &readfds, (fd_set *) 0,
2220                            (fd_set *) 0, &timeout)) <= 0)
2221             return;
2222         else {
2223             if (Lst_Open(jobs) == FAILURE) {
2224                 Punt("Cannot open job table");
2225             }
2226             while (nfds && (ln = Lst_Next(jobs)) != NULL) {
2227                 job = (Job *) Lst_Datum(ln);
2228                 if (FD_ISSET(job->inPipe, &readfds)) {
2229                     JobDoOutput(job, FALSE);
2230                     nfds -= 1;
2231                 }
2232             }
2233             Lst_Close(jobs);
2234         }
2235     }
2236 #endif /* RMT_WILL_WATCH */
2237 }
2238
2239 /*-
2240  *-----------------------------------------------------------------------
2241  * Job_Make --
2242  *      Start the creation of a target. Basically a front-end for
2243  *      JobStart used by the Make module.
2244  *
2245  * Results:
2246  *      None.
2247  *
2248  * Side Effects:
2249  *      Another job is started.
2250  *
2251  *-----------------------------------------------------------------------
2252  */
2253 void
2254 Job_Make(GNode *gn)
2255 {
2256     (void) JobStart(gn, 0, NULL);
2257 }
2258
2259 /*-
2260  *-----------------------------------------------------------------------
2261  * Job_Init --
2262  *      Initialize the process module, given a maximum number of jobs, and
2263  *      a maximum number of local jobs.
2264  *
2265  * Results:
2266  *      none
2267  *
2268  * Side Effects:
2269  *      lists and counters are initialized
2270  *-----------------------------------------------------------------------
2271  */
2272 void
2273 Job_Init(int maxproc, int maxlocal)
2274 {
2275     GNode         *begin;     /* node for commands to do at the very start */
2276
2277     jobs =        Lst_Init(FALSE);
2278     stoppedJobs = Lst_Init(FALSE);
2279     maxJobs =     maxproc;
2280     maxLocal =    maxlocal;
2281     nJobs =       0;
2282     nLocal =      0;
2283     jobFull =     FALSE;
2284
2285     aborting =    0;
2286     errors =      0;
2287
2288     lastNode =    NULL;
2289
2290     if (maxJobs == 1 || beVerbose == 0
2291 #ifdef REMOTE
2292         || noMessages
2293 #endif
2294                      ) {
2295         /*
2296          * If only one job can run at a time, there's no need for a banner,
2297          * no is there?
2298          */
2299         targFmt = "";
2300     } else {
2301         targFmt = TARG_FMT;
2302     }
2303
2304     if (shellPath == NULL) {
2305         /*
2306          * The user didn't specify a shell to use, so we are using the
2307          * default one... Both the absolute path and the last component
2308          * must be set. The last component is taken from the 'name' field
2309          * of the default shell description pointed-to by commandShell.
2310          * All default shells are located in _PATH_DEFSHELLDIR.
2311          */
2312         shellName = commandShell->name;
2313         shellPath = str_concat(_PATH_DEFSHELLDIR, shellName, STR_ADDSLASH);
2314     }
2315
2316     if (commandShell->exit == NULL) {
2317         commandShell->exit = "";
2318     }
2319     if (commandShell->echo == NULL) {
2320         commandShell->echo = "";
2321     }
2322
2323     /*
2324      * Catch the four signals that POSIX specifies if they aren't ignored.
2325      * JobPassSig will take care of calling JobInterrupt if appropriate.
2326      */
2327     if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
2328         (void) signal(SIGINT, JobPassSig);
2329     }
2330     if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
2331         (void) signal(SIGHUP, JobPassSig);
2332     }
2333     if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
2334         (void) signal(SIGQUIT, JobPassSig);
2335     }
2336     if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
2337         (void) signal(SIGTERM, JobPassSig);
2338     }
2339     /*
2340      * There are additional signals that need to be caught and passed if
2341      * either the export system wants to be told directly of signals or if
2342      * we're giving each job its own process group (since then it won't get
2343      * signals from the terminal driver as we own the terminal)
2344      */
2345 #if defined(RMT_WANTS_SIGNALS) || defined(USE_PGRP)
2346     if (signal(SIGTSTP, SIG_IGN) != SIG_IGN) {
2347         (void) signal(SIGTSTP, JobPassSig);
2348     }
2349     if (signal(SIGTTOU, SIG_IGN) != SIG_IGN) {
2350         (void) signal(SIGTTOU, JobPassSig);
2351     }
2352     if (signal(SIGTTIN, SIG_IGN) != SIG_IGN) {
2353         (void) signal(SIGTTIN, JobPassSig);
2354     }
2355     if (signal(SIGWINCH, SIG_IGN) != SIG_IGN) {
2356         (void) signal(SIGWINCH, JobPassSig);
2357     }
2358 #endif
2359
2360     begin = Targ_FindNode(".BEGIN", TARG_NOCREATE);
2361
2362     if (begin != NULL) {
2363         JobStart(begin, JOB_SPECIAL, (Job *)0);
2364         while (nJobs) {
2365             Job_CatchOutput();
2366 #ifndef RMT_WILL_WATCH
2367             Job_CatchChildren(!usePipes);
2368 #endif /* RMT_WILL_WATCH */
2369         }
2370     }
2371     postCommands = Targ_FindNode(".END", TARG_CREATE);
2372 }
2373
2374 /*-
2375  *-----------------------------------------------------------------------
2376  * Job_Full --
2377  *      See if the job table is full. It is considered full if it is OR
2378  *      if we are in the process of aborting OR if we have
2379  *      reached/exceeded our local quota. This prevents any more jobs
2380  *      from starting up.
2381  *
2382  * Results:
2383  *      TRUE if the job table is full, FALSE otherwise
2384  * Side Effects:
2385  *      None.
2386  *-----------------------------------------------------------------------
2387  */
2388 Boolean
2389 Job_Full(void)
2390 {
2391     return(aborting || jobFull);
2392 }
2393
2394 /*-
2395  *-----------------------------------------------------------------------
2396  * Job_Empty --
2397  *      See if the job table is empty.  Because the local concurrency may
2398  *      be set to 0, it is possible for the job table to become empty,
2399  *      while the list of stoppedJobs remains non-empty. In such a case,
2400  *      we want to restart as many jobs as we can.
2401  *
2402  * Results:
2403  *      TRUE if it is. FALSE if it ain't.
2404  *
2405  * Side Effects:
2406  *      None.
2407  *
2408  * -----------------------------------------------------------------------
2409  */
2410 Boolean
2411 Job_Empty(void)
2412 {
2413     if (nJobs == 0) {
2414         if (!Lst_IsEmpty(stoppedJobs) && !aborting) {
2415             /*
2416              * The job table is obviously not full if it has no jobs in
2417              * it...Try and restart the stopped jobs.
2418              */
2419             jobFull = FALSE;
2420             JobRestartJobs();
2421             return(FALSE);
2422         } else {
2423             return(TRUE);
2424         }
2425     } else {
2426         return(FALSE);
2427     }
2428 }
2429
2430 /*-
2431  *-----------------------------------------------------------------------
2432  * JobMatchShell --
2433  *      Find a matching shell in 'shells' given its final component.
2434  *
2435  * Results:
2436  *      A pointer to the Shell structure.
2437  *
2438  * Side Effects:
2439  *      None.
2440  *
2441  *-----------------------------------------------------------------------
2442  */
2443 static Shell *
2444 JobMatchShell(char *name)
2445 {
2446     Shell         *sh;        /* Pointer into shells table */
2447     Shell         *match;     /* Longest-matching shell */
2448     char          *cp1,
2449                   *cp2;
2450     char          *eoname;
2451
2452     eoname = name + strlen(name);
2453
2454     match = NULL;
2455
2456     for (sh = shells; sh->name != NULL; sh++) {
2457         for (cp1 = eoname - strlen(sh->name), cp2 = sh->name;
2458              *cp1 != '\0' && *cp1 == *cp2;
2459              cp1++, cp2++) {
2460                  continue;
2461         }
2462         if (*cp1 != *cp2) {
2463             continue;
2464         } else if (match == NULL || strlen(match->name) < strlen(sh->name)) {
2465            match = sh;
2466         }
2467     }
2468     return(match == NULL ? sh : match);
2469 }
2470
2471 /*-
2472  *-----------------------------------------------------------------------
2473  * Job_ParseShell --
2474  *      Parse a shell specification and set up commandShell, shellPath
2475  *      and shellName appropriately.
2476  *
2477  * Results:
2478  *      FAILURE if the specification was incorrect.
2479  *
2480  * Side Effects:
2481  *      commandShell points to a Shell structure (either predefined or
2482  *      created from the shell spec), shellPath is the full path of the
2483  *      shell described by commandShell, while shellName is just the
2484  *      final component of shellPath.
2485  *
2486  * Notes:
2487  *      A shell specification consists of a .SHELL target, with dependency
2488  *      operator, followed by a series of blank-separated words. Double
2489  *      quotes can be used to use blanks in words. A backslash escapes
2490  *      anything (most notably a double-quote and a space) and
2491  *      provides the functionality it does in C. Each word consists of
2492  *      keyword and value separated by an equal sign. There should be no
2493  *      unnecessary spaces in the word. The keywords are as follows:
2494  *          name            Name of shell.
2495  *          path            Location of shell. Overrides "name" if given
2496  *          quiet           Command to turn off echoing.
2497  *          echo            Command to turn echoing on
2498  *          filter          Result of turning off echoing that shouldn't be
2499  *                          printed.
2500  *          echoFlag        Flag to turn echoing on at the start
2501  *          errFlag         Flag to turn error checking on at the start
2502  *          hasErrCtl       True if shell has error checking control
2503  *          check           Command to turn on error checking if hasErrCtl
2504  *                          is TRUE or template of command to echo a command
2505  *                          for which error checking is off if hasErrCtl is
2506  *                          FALSE.
2507  *          ignore          Command to turn off error checking if hasErrCtl
2508  *                          is TRUE or template of command to execute a
2509  *                          command so as to ignore any errors it returns if
2510  *                          hasErrCtl is FALSE.
2511  *
2512  *-----------------------------------------------------------------------
2513  */
2514 ReturnStatus
2515 Job_ParseShell(char *line)
2516 {
2517     char          **words;
2518     int           wordCount;
2519     char          **argv;
2520     int           argc;
2521     char          *path;
2522     Shell         newShell;
2523     Boolean       fullSpec = FALSE;
2524
2525     while (isspace((unsigned char) *line)) {
2526         line++;
2527     }
2528     words = brk_string(line, &wordCount, TRUE);
2529
2530     memset(&newShell, 0, sizeof(newShell));
2531
2532     /*
2533      * Parse the specification by keyword
2534      */
2535     for (path = NULL, argc = wordCount - 1, argv = words + 1;
2536          argc != 0;
2537          argc--, argv++) {
2538              if (strncmp(*argv, "path=", 5) == 0) {
2539                  path = &argv[0][5];
2540              } else if (strncmp(*argv, "name=", 5) == 0) {
2541                  newShell.name = &argv[0][5];
2542              } else {
2543                  if (strncmp(*argv, "quiet=", 6) == 0) {
2544                      newShell.echoOff = &argv[0][6];
2545                  } else if (strncmp(*argv, "echo=", 5) == 0) {
2546                      newShell.echoOn = &argv[0][5];
2547                  } else if (strncmp(*argv, "filter=", 7) == 0) {
2548                      newShell.noPrint = &argv[0][7];
2549                      newShell.noPLen = strlen(newShell.noPrint);
2550                  } else if (strncmp(*argv, "echoFlag=", 9) == 0) {
2551                      newShell.echo = &argv[0][9];
2552                  } else if (strncmp(*argv, "errFlag=", 8) == 0) {
2553                      newShell.exit = &argv[0][8];
2554                  } else if (strncmp(*argv, "hasErrCtl=", 10) == 0) {
2555                      char c = argv[0][10];
2556                      newShell.hasErrCtl = !((c != 'Y') && (c != 'y') &&
2557                                            (c != 'T') && (c != 't'));
2558                  } else if (strncmp(*argv, "check=", 6) == 0) {
2559                      newShell.errCheck = &argv[0][6];
2560                  } else if (strncmp(*argv, "ignore=", 7) == 0) {
2561                      newShell.ignErr = &argv[0][7];
2562                  } else {
2563                      Parse_Error(PARSE_FATAL, "Unknown keyword \"%s\"",
2564                                   *argv);
2565                      return(FAILURE);
2566                  }
2567                  fullSpec = TRUE;
2568              }
2569     }
2570
2571     if (path == NULL) {
2572         /*
2573          * If no path was given, the user wants one of the pre-defined shells,
2574          * yes? So we find the one s/he wants with the help of JobMatchShell
2575          * and set things up the right way. shellPath will be set up by
2576          * Job_Init.
2577          */
2578         if (newShell.name == NULL) {
2579             Parse_Error(PARSE_FATAL, "Neither path nor name specified");
2580             return(FAILURE);
2581         } else {
2582             commandShell = JobMatchShell(newShell.name);
2583             shellName = newShell.name;
2584         }
2585     } else {
2586         /*
2587          * The user provided a path. If s/he gave nothing else (fullSpec is
2588          * FALSE), try and find a matching shell in the ones we know of.
2589          * Else we just take the specification at its word and copy it
2590          * to a new location. In either case, we need to record the
2591          * path the user gave for the shell.
2592          */
2593         shellPath = path;
2594         path = strrchr(path, '/');
2595         if (path == NULL) {
2596             path = shellPath;
2597         } else {
2598             path += 1;
2599         }
2600         if (newShell.name != NULL) {
2601             shellName = newShell.name;
2602         } else {
2603             shellName = path;
2604         }
2605         if (!fullSpec) {
2606             commandShell = JobMatchShell(shellName);
2607         } else {
2608             commandShell = (Shell *) emalloc(sizeof(Shell));
2609             *commandShell = newShell;
2610         }
2611     }
2612
2613     if (commandShell->echoOn && commandShell->echoOff) {
2614         commandShell->hasEchoCtl = TRUE;
2615     }
2616
2617     if (!commandShell->hasErrCtl) {
2618         if (commandShell->errCheck == NULL) {
2619             commandShell->errCheck = "";
2620         }
2621         if (commandShell->ignErr == NULL) {
2622             commandShell->ignErr = "%s\n";
2623         }
2624     }
2625     return SUCCESS;
2626 }
2627
2628 /*-
2629  *-----------------------------------------------------------------------
2630  * JobInterrupt --
2631  *      Handle the receipt of an interrupt.
2632  *
2633  * Results:
2634  *      None
2635  *
2636  * Side Effects:
2637  *      All children are killed. Another job will be started if the
2638  *      .INTERRUPT target was given.
2639  *-----------------------------------------------------------------------
2640  */
2641 static void
2642 JobInterrupt(int runINTERRUPT, int signo)
2643 {
2644     LstNode       ln;           /* element in job table */
2645     Job           *job = NULL;  /* job descriptor in that element */
2646     GNode         *interrupt;   /* the node describing the .INTERRUPT target */
2647
2648     aborting = ABORT_INTERRUPT;
2649
2650     (void) Lst_Open(jobs);
2651     while ((ln = Lst_Next(jobs)) != NULL) {
2652         job = (Job *) Lst_Datum(ln);
2653
2654         if (!Targ_Precious(job->node)) {
2655             char        *file = (job->node->path == NULL ?
2656                                  job->node->name :
2657                                  job->node->path);
2658             if (!noExecute && eunlink(file) != -1) {
2659                 Error("*** %s removed", file);
2660             }
2661         }
2662 #ifdef RMT_WANTS_SIGNALS
2663         if (job->flags & JOB_REMOTE) {
2664             /*
2665              * If job is remote, let the Rmt module do the killing.
2666              */
2667             if (!Rmt_Signal(job, signo)) {
2668                 /*
2669                  * If couldn't kill the thing, finish it out now with an
2670                  * error code, since no exit report will come in likely.
2671                  */
2672                 int status;
2673
2674                 status.w_status = 0;
2675                 status.w_retcode = 1;
2676                 JobFinish(job, &status);
2677             }
2678         } else if (job->pid) {
2679             KILL(job->pid, signo);
2680         }
2681 #else
2682         if (job->pid) {
2683             DEBUGF(JOB, ("JobInterrupt passing signal to child %d.\n",
2684                    job->pid));
2685             KILL(job->pid, signo);
2686         }
2687 #endif /* RMT_WANTS_SIGNALS */
2688     }
2689
2690 #ifdef REMOTE
2691     (void)Lst_Open(stoppedJobs);
2692     while ((ln = Lst_Next(stoppedJobs)) != NULL) {
2693         job = (Job *) Lst_Datum(ln);
2694
2695         if (job->flags & JOB_RESTART) {
2696             DEBUGF(JOB, "JobInterrupt skipping job on stopped queue"
2697                    "-- it was waiting to be restarted.\n");
2698             continue;
2699         }
2700         if (!Targ_Precious(job->node)) {
2701             char        *file = (job->node->path == NULL ?
2702                                  job->node->name :
2703                                  job->node->path);
2704             if (eunlink(file) == 0) {
2705                 Error("*** %s removed", file);
2706             }
2707         }
2708         /*
2709          * Resume the thing so it will take the signal.
2710          */
2711         DEBUGF(JOB, ("JobInterrupt passing CONT to stopped child %d.\n", job->pid));
2712         KILL(job->pid, SIGCONT);
2713 #ifdef RMT_WANTS_SIGNALS
2714         if (job->flags & JOB_REMOTE) {
2715             /*
2716              * If job is remote, let the Rmt module do the killing.
2717              */
2718             if (!Rmt_Signal(job, SIGINT)) {
2719                 /*
2720                  * If couldn't kill the thing, finish it out now with an
2721                  * error code, since no exit report will come in likely.
2722                  */
2723                 int status;
2724                 status.w_status = 0;
2725                 status.w_retcode = 1;
2726                 JobFinish(job, &status);
2727             }
2728         } else if (job->pid) {
2729             DEBUGF(JOB, "JobInterrupt passing interrupt to stopped child %d.\n",
2730                    job->pid);
2731             KILL(job->pid, SIGINT);
2732         }
2733 #endif /* RMT_WANTS_SIGNALS */
2734     }
2735 #endif
2736     Lst_Close(stoppedJobs);
2737
2738     if (runINTERRUPT && !touchFlag) {
2739         interrupt = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
2740         if (interrupt != NULL) {
2741             ignoreErrors = FALSE;
2742
2743             JobStart(interrupt, JOB_IGNDOTS, (Job *)0);
2744             while (nJobs) {
2745                 Job_CatchOutput();
2746 #ifndef RMT_WILL_WATCH
2747                 Job_CatchChildren(!usePipes);
2748 #endif /* RMT_WILL_WATCH */
2749             }
2750         }
2751     }
2752 }
2753
2754 /*
2755  *-----------------------------------------------------------------------
2756  * Job_Finish --
2757  *      Do final processing such as the running of the commands
2758  *      attached to the .END target.
2759  *
2760  * Results:
2761  *      Number of errors reported.
2762  *-----------------------------------------------------------------------
2763  */
2764 int
2765 Job_Finish(void)
2766 {
2767     if (postCommands != NULL && !Lst_IsEmpty(postCommands->commands)) {
2768         if (errors) {
2769             Error("Errors reported so .END ignored");
2770         } else {
2771             JobStart(postCommands, JOB_SPECIAL | JOB_IGNDOTS, NULL);
2772
2773             while (nJobs) {
2774                 Job_CatchOutput();
2775 #ifndef RMT_WILL_WATCH
2776                 Job_CatchChildren(!usePipes);
2777 #endif /* RMT_WILL_WATCH */
2778             }
2779         }
2780     }
2781     return(errors);
2782 }
2783
2784 /*-
2785  *-----------------------------------------------------------------------
2786  * Job_Wait --
2787  *      Waits for all running jobs to finish and returns. Sets 'aborting'
2788  *      to ABORT_WAIT to prevent other jobs from starting.
2789  *
2790  * Results:
2791  *      None.
2792  *
2793  * Side Effects:
2794  *      Currently running jobs finish.
2795  *
2796  *-----------------------------------------------------------------------
2797  */
2798 void
2799 Job_Wait(void)
2800 {
2801     aborting = ABORT_WAIT;
2802     while (nJobs != 0) {
2803         Job_CatchOutput();
2804 #ifndef RMT_WILL_WATCH
2805         Job_CatchChildren(!usePipes);
2806 #endif /* RMT_WILL_WATCH */
2807     }
2808     aborting = 0;
2809 }
2810
2811 /*-
2812  *-----------------------------------------------------------------------
2813  * Job_AbortAll --
2814  *      Abort all currently running jobs without handling output or anything.
2815  *      This function is to be called only in the event of a major
2816  *      error. Most definitely NOT to be called from JobInterrupt.
2817  *
2818  * Results:
2819  *      None
2820  *
2821  * Side Effects:
2822  *      All children are killed, not just the firstborn
2823  *-----------------------------------------------------------------------
2824  */
2825 void
2826 Job_AbortAll(void)
2827 {
2828     LstNode             ln;     /* element in job table */
2829     Job                 *job;   /* the job descriptor in that element */
2830     int                 foo;
2831
2832     aborting = ABORT_ERROR;
2833
2834     if (nJobs) {
2835
2836         (void) Lst_Open(jobs);
2837         while ((ln = Lst_Next(jobs)) != NULL) {
2838             job = (Job *) Lst_Datum(ln);
2839
2840             /*
2841              * kill the child process with increasingly drastic signals to make
2842              * darn sure it's dead.
2843              */
2844 #ifdef RMT_WANTS_SIGNALS
2845             if (job->flags & JOB_REMOTE) {
2846                 Rmt_Signal(job, SIGINT);
2847                 Rmt_Signal(job, SIGKILL);
2848             } else {
2849                 KILL(job->pid, SIGINT);
2850                 KILL(job->pid, SIGKILL);
2851             }
2852 #else
2853             KILL(job->pid, SIGINT);
2854             KILL(job->pid, SIGKILL);
2855 #endif /* RMT_WANTS_SIGNALS */
2856         }
2857     }
2858
2859     /*
2860      * Catch as many children as want to report in at first, then give up
2861      */
2862     while (waitpid((pid_t) -1, &foo, WNOHANG) > 0)
2863         continue;
2864 }
2865
2866 #ifdef REMOTE
2867 /*-
2868  *-----------------------------------------------------------------------
2869  * JobFlagForMigration --
2870  *      Handle the eviction of a child. Called from RmtStatusChange.
2871  *      Flags the child as remigratable and then suspends it.  Takes
2872  *      the ID of the host we used, for matching children.
2873  *
2874  * Results:
2875  *      none.
2876  *
2877  * Side Effects:
2878  *      The job descriptor is flagged for remigration.
2879  *
2880  *-----------------------------------------------------------------------
2881  */
2882 void
2883 JobFlagForMigration(int hostID)
2884 {
2885     Job           *job;         /* job descriptor for dead child */
2886     LstNode       jnode;        /* list element for finding job */
2887
2888     DEBUGF(JOB, ("JobFlagForMigration(%d) called.\n", hostID));
2889     jnode = Lst_Find(jobs, (void *)hostID, JobCmpRmtID);
2890
2891     if (jnode == NULL) {
2892         jnode = Lst_Find(stoppedJobs, (void *)hostID, JobCmpRmtID);
2893                 if (jnode == NULL) {
2894                     if (DEBUG(JOB)) {
2895                         Error("Evicting host(%d) not in table", hostID);
2896                     }
2897                     return;
2898                 }
2899     }
2900     job = (Job *) Lst_Datum(jnode);
2901
2902     DEBUGF(JOB, ("JobFlagForMigration(%d) found job '%s'.\n", hostID, job->node->name));
2903
2904     KILL(job->pid, SIGSTOP);
2905
2906     job->flags |= JOB_REMIGRATE;
2907 }
2908
2909 #endif
2910 \f
2911 /*-
2912  *-----------------------------------------------------------------------
2913  * JobRestartJobs --
2914  *      Tries to restart stopped jobs if there are slots available.
2915  *      Note that this tries to restart them regardless of pending errors.
2916  *      It's not good to leave stopped jobs lying around!
2917  *
2918  * Results:
2919  *      None.
2920  *
2921  * Side Effects:
2922  *      Resumes(and possibly migrates) jobs.
2923  *
2924  *-----------------------------------------------------------------------
2925  */
2926 static void
2927 JobRestartJobs(void)
2928 {
2929     while (!jobFull && !Lst_IsEmpty(stoppedJobs)) {
2930         DEBUGF(JOB, ("Job queue is not full. Restarting a stopped job.\n"));
2931         JobRestart((Job *)Lst_DeQueue(stoppedJobs));
2932     }
2933 }