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