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