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