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