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