job.c revision 1.25.2.2 1 /* $NetBSD: job.c,v 1.25.2.2 2000/01/23 13:35:44 he 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.25.2.2 2000/01/23 13:35:44 he 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.25.2.2 2000/01/23 13:35:44 he Exp $");
50 #endif
51 #endif /* not lint */
52 #endif
53
54 /*-
55 * job.c --
56 * handle the creation etc. of our child processes.
57 *
58 * Interface:
59 * Job_Make Start the creation of the given target.
60 *
61 * Job_CatchChildren Check for and handle the termination of any
62 * children. This must be called reasonably
63 * frequently to keep the whole make going at
64 * a decent clip, since job table entries aren't
65 * removed until their process is caught this way.
66 * Its single argument is TRUE if the function
67 * should block waiting for a child to terminate.
68 *
69 * Job_CatchOutput Print any output our children have produced.
70 * Should also be called fairly frequently to
71 * keep the user informed of what's going on.
72 * If no output is waiting, it will block for
73 * a time given by the SEL_* constants, below,
74 * or until output is ready.
75 *
76 * Job_Init Called to intialize this module. in addition,
77 * any commands attached to the .BEGIN target
78 * are executed before this function returns.
79 * Hence, the makefile must have been parsed
80 * before this function is called.
81 *
82 * Job_End Cleanup any memory used.
83 *
84 * Job_Full Return TRUE if the job table is filled.
85 *
86 * Job_Empty Return TRUE if the job table is completely
87 * empty.
88 *
89 * Job_ParseShell Given the line following a .SHELL target, parse
90 * the line as a shell specification. Returns
91 * FAILURE if the spec was incorrect.
92 *
93 * Job_Finish Perform any final processing which needs doing.
94 * This includes the execution of any commands
95 * which have been/were attached to the .END
96 * target. It should only be called when the
97 * job table is empty.
98 *
99 * Job_AbortAll Abort all currently running jobs. It doesn't
100 * handle output or do anything for the jobs,
101 * just kills them. It should only be called in
102 * an emergency, as it were.
103 *
104 * Job_CheckCommands Verify that the commands for a target are
105 * ok. Provide them if necessary and possible.
106 *
107 * Job_Touch Update a target without really updating it.
108 *
109 * Job_Wait Wait for all currently-running jobs to finish.
110 */
111
112 #include <sys/types.h>
113 #include <sys/stat.h>
114 #include <sys/file.h>
115 #include <sys/time.h>
116 #include <sys/wait.h>
117 #include <fcntl.h>
118 #include <errno.h>
119 #include <utime.h>
120 #include <stdio.h>
121 #include <string.h>
122 #include <signal.h>
123 #include "make.h"
124 #include "hash.h"
125 #include "dir.h"
126 #include "job.h"
127 #include "pathnames.h"
128 #ifdef REMOTE
129 #include "rmt.h"
130 # define STATIC
131 #else
132 # define STATIC static
133 #endif
134
135 /*
136 * error handling variables
137 */
138 static int errors = 0; /* number of errors reported */
139 static int aborting = 0; /* why is the make aborting? */
140 #define ABORT_ERROR 1 /* Because of an error */
141 #define ABORT_INTERRUPT 2 /* Because it was interrupted */
142 #define ABORT_WAIT 3 /* Waiting for jobs to finish */
143
144 /*
145 * XXX: Avoid SunOS bug... FILENO() is fp->_file, and file
146 * is a char! So when we go above 127 we turn negative!
147 */
148 #define FILENO(a) ((unsigned) fileno(a))
149
150 /*
151 * post-make command processing. The node postCommands is really just the
152 * .END target but we keep it around to avoid having to search for it
153 * all the time.
154 */
155 static GNode *postCommands; /* node containing commands to execute when
156 * everything else is done */
157 static int numCommands; /* The number of commands actually printed
158 * for a target. Should this number be
159 * 0, no shell will be executed. */
160
161 /*
162 * Return values from JobStart.
163 */
164 #define JOB_RUNNING 0 /* Job is running */
165 #define JOB_ERROR 1 /* Error in starting the job */
166 #define JOB_FINISHED 2 /* The job is already finished */
167 #define JOB_STOPPED 3 /* The job is stopped */
168
169 /*
170 * tfile is the name of a file into which all shell commands are put. It is
171 * used over by removing it before the child shell is executed. The XXXXX in
172 * the string are replaced by the pid of the make process in a 5-character
173 * field with leading zeroes.
174 */
175 static char tfile[] = TMPPAT;
176
177
178 /*
179 * Descriptions for various shells.
180 */
181 static Shell shells[] = {
182 /*
183 * CSH description. The csh can do echo control by playing
184 * with the setting of the 'echo' shell variable. Sadly,
185 * however, it is unable to do error control nicely.
186 */
187 {
188 "csh",
189 TRUE, "unset verbose", "set verbose", "unset verbose", 10,
190 FALSE, "echo \"%s\"\n", "csh -c \"%s || exit 0\"",
191 "v", "e",
192 },
193 /*
194 * SH description. Echo control is also possible and, under
195 * sun UNIX anyway, one can even control error checking.
196 */
197 {
198 "sh",
199 TRUE, "set -", "set -v", "set -", 5,
200 TRUE, "set -e", "set +e",
201 #ifdef OLDBOURNESHELL
202 FALSE, "echo \"%s\"\n", "sh -c '%s || exit 0'\n",
203 #endif
204 #ifdef __NetBSD__
205 "vq",
206 #else
207 "v",
208 #endif
209 "e",
210 },
211 /*
212 * UNKNOWN.
213 */
214 {
215 (char *) 0,
216 FALSE, (char *) 0, (char *) 0, (char *) 0, 0,
217 FALSE, (char *) 0, (char *) 0,
218 (char *) 0, (char *) 0,
219 }
220 };
221 static Shell *commandShell = &shells[DEFSHELL];/* this is the shell to
222 * which we pass all
223 * commands in the Makefile.
224 * It is set by the
225 * Job_ParseShell function */
226 static char *shellPath = NULL, /* full pathname of
227 * executable image */
228 *shellName = NULL, /* last component of shell */
229 *shellArgv = NULL; /* Custom shell args */
230
231
232 static int maxJobs; /* The most children we can run at once */
233 static int maxLocal; /* The most local ones we can have */
234 STATIC int nJobs; /* The number of children currently running */
235 STATIC int nLocal; /* The number of local children */
236 STATIC Lst jobs; /* The structures that describe them */
237 STATIC Boolean jobFull; /* Flag to tell when the job table is full. It
238 * is set TRUE when (1) the total number of
239 * running jobs equals the maximum allowed or
240 * (2) a job can only be run locally, but
241 * nLocal equals maxLocal */
242 #ifndef RMT_WILL_WATCH
243 static fd_set outputs; /* Set of descriptors of pipes connected to
244 * the output channels of children */
245 #endif
246
247 STATIC GNode *lastNode; /* The node for which output was most recently
248 * produced. */
249 STATIC char *targFmt; /* Format string to use to head output from a
250 * job when it's not the most-recent job heard
251 * from */
252
253 #ifdef REMOTE
254 # define TARG_FMT "--- %s at %s ---\n" /* Default format */
255 # define MESSAGE(fp, gn) \
256 (void) fprintf(fp, targFmt, gn->name, gn->rem.hname);
257 #else
258 # define TARG_FMT "--- %s ---\n" /* Default format */
259 # define MESSAGE(fp, gn) \
260 (void) fprintf(fp, targFmt, gn->name);
261 #endif
262
263 /*
264 * When JobStart attempts to run a job remotely but can't, and isn't allowed
265 * to run the job locally, or when Job_CatchChildren detects a job that has
266 * been migrated home, the job is placed on the stoppedJobs queue to be run
267 * when the next job finishes.
268 */
269 STATIC Lst stoppedJobs; /* Lst of Job structures describing
270 * jobs that were stopped due to concurrency
271 * limits or migration home */
272
273
274 #if defined(USE_PGRP) && defined(SYSV)
275 # define KILL(pid, sig) kill(-(pid), (sig))
276 #else
277 # if defined(USE_PGRP)
278 # define KILL(pid, sig) killpg((pid), (sig))
279 # else
280 # define KILL(pid, sig) kill((pid), (sig))
281 # endif
282 #endif
283
284 /*
285 * Grmpf... There is no way to set bits of the wait structure
286 * anymore with the stupid W*() macros. I liked the union wait
287 * stuff much more. So, we devise our own macros... This is
288 * really ugly, use dramamine sparingly. You have been warned.
289 */
290 #ifndef W_STOPCODE
291 #define W_STOPCODE(sig) (((sig) << 8) | 0177)
292 #endif
293 #ifndef W_EXITCODE
294 #define W_EXITCODE(ret, sig) ((ret << 8) | (sig))
295 #endif
296
297 static int JobCondPassSig __P((ClientData, ClientData));
298 static void JobPassSig __P((int));
299 static int JobCmpPid __P((ClientData, ClientData));
300 static int JobPrintCommand __P((ClientData, ClientData));
301 static int JobSaveCommand __P((ClientData, ClientData));
302 static void JobClose __P((Job *));
303 #ifdef REMOTE
304 static int JobCmpRmtID __P((Job *, int));
305 # ifdef RMT_WILL_WATCH
306 static void JobLocalInput __P((int, Job *));
307 # endif
308 #else
309 static void JobFinish __P((Job *, int *));
310 static void JobExec __P((Job *, char **));
311 #endif
312 static void JobMakeArgv __P((Job *, char **));
313 static void JobRestart __P((Job *));
314 static int JobStart __P((GNode *, int, Job *));
315 static char *JobOutput __P((Job *, char *, char *, int));
316 static void JobDoOutput __P((Job *, Boolean));
317 static Shell *JobMatchShell __P((char *));
318 static void JobInterrupt __P((int, int));
319 static void JobRestartJobs __P((void));
320
321 /*-
322 *-----------------------------------------------------------------------
323 * JobCondPassSig --
324 * Pass a signal to a job if the job is remote or if USE_PGRP
325 * is defined.
326 *
327 * Results:
328 * === 0
329 *
330 * Side Effects:
331 * None, except the job may bite it.
332 *
333 *-----------------------------------------------------------------------
334 */
335 static int
336 JobCondPassSig(jobp, signop)
337 ClientData jobp; /* Job to biff */
338 ClientData signop; /* Signal to send it */
339 {
340 Job *job = (Job *) jobp;
341 int signo = *(int *) signop;
342 #ifdef RMT_WANTS_SIGNALS
343 if (job->flags & JOB_REMOTE) {
344 (void) Rmt_Signal(job, signo);
345 } else {
346 KILL(job->pid, signo);
347 }
348 #else
349 /*
350 * Assume that sending the signal to job->pid will signal any remote
351 * job as well.
352 */
353 if (DEBUG(JOB)) {
354 (void) fprintf(stdout,
355 "JobCondPassSig passing signal %d to child %d.\n",
356 signo, job->pid);
357 (void) fflush(stdout);
358 }
359 KILL(job->pid, signo);
360 #endif
361 return 0;
362 }
363
364 /*-
365 *-----------------------------------------------------------------------
366 * JobPassSig --
367 * Pass a signal on to all remote jobs and to all local jobs if
368 * USE_PGRP is defined, then die ourselves.
369 *
370 * Results:
371 * None.
372 *
373 * Side Effects:
374 * We die by the same signal.
375 *
376 *-----------------------------------------------------------------------
377 */
378 static void
379 JobPassSig(signo)
380 int signo; /* The signal number we've received */
381 {
382 sigset_t nmask, omask;
383 struct sigaction act;
384 int sigcont;
385
386 if (DEBUG(JOB)) {
387 (void) fprintf(stdout, "JobPassSig(%d) called.\n", signo);
388 (void) fflush(stdout);
389 }
390 Lst_ForEach(jobs, JobCondPassSig, (ClientData) &signo);
391
392 /*
393 * Deal with proper cleanup based on the signal received. We only run
394 * the .INTERRUPT target if the signal was in fact an interrupt. The other
395 * three termination signals are more of a "get out *now*" command.
396 */
397 if (signo == SIGINT) {
398 JobInterrupt(TRUE, signo);
399 } else if ((signo == SIGHUP) || (signo == SIGTERM) || (signo == SIGQUIT)) {
400 JobInterrupt(FALSE, signo);
401 }
402
403 /*
404 * Leave gracefully if SIGQUIT, rather than core dumping.
405 */
406 if (signo == SIGQUIT) {
407 Finish(0);
408 }
409
410 /*
411 * Send ourselves the signal now we've given the message to everyone else.
412 * Note we block everything else possible while we're getting the signal.
413 * This ensures that all our jobs get continued when we wake up before
414 * we take any other signal.
415 */
416 sigfillset(&nmask);
417 sigprocmask(SIG_SETMASK, &nmask, &omask);
418 act.sa_handler = SIG_DFL;
419 sigemptyset(&act.sa_mask);
420 act.sa_flags = 0;
421 sigaction(signo, &act, NULL);
422
423 if (DEBUG(JOB)) {
424 (void) fprintf(stdout,
425 "JobPassSig passing signal to self, mask = %x.\n",
426 ~0 & ~(1 << (signo-1)));
427 (void) fflush(stdout);
428 }
429
430 (void) kill(getpid(), signo);
431 if (signo != SIGTSTP) {
432 sigcont = SIGCONT;
433 Lst_ForEach(jobs, JobCondPassSig, (ClientData) &sigcont);
434 }
435
436 (void) sigprocmask(SIG_SETMASK, &omask, NULL);
437 sigprocmask(SIG_SETMASK, &omask, NULL);
438 if (signo != SIGCONT && signo != SIGTSTP) {
439 act.sa_handler = JobPassSig;
440 sigaction(sigcont, &act, NULL);
441 }
442 }
443
444 /*-
445 *-----------------------------------------------------------------------
446 * JobCmpPid --
447 * Compare the pid of the job with the given pid and return 0 if they
448 * are equal. This function is called from Job_CatchChildren via
449 * Lst_Find to find the job descriptor of the finished job.
450 *
451 * Results:
452 * 0 if the pid's match
453 *
454 * Side Effects:
455 * None
456 *-----------------------------------------------------------------------
457 */
458 static int
459 JobCmpPid(job, pid)
460 ClientData job; /* job to examine */
461 ClientData pid; /* process id desired */
462 {
463 return *(int *) pid - ((Job *) job)->pid;
464 }
465
466 #ifdef REMOTE
467 /*-
468 *-----------------------------------------------------------------------
469 * JobCmpRmtID --
470 * Compare the rmtID of the job with the given rmtID and return 0 if they
471 * are equal.
472 *
473 * Results:
474 * 0 if the rmtID's match
475 *
476 * Side Effects:
477 * None.
478 *-----------------------------------------------------------------------
479 */
480 static int
481 JobCmpRmtID(job, rmtID)
482 ClientData job; /* job to examine */
483 ClientData rmtID; /* remote id desired */
484 {
485 return(*(int *) rmtID - *(int *) job->rmtID);
486 }
487 #endif
488
489 /*-
490 *-----------------------------------------------------------------------
491 * JobPrintCommand --
492 * Put out another command for the given job. If the command starts
493 * with an @ or a - we process it specially. In the former case,
494 * so long as the -s and -n flags weren't given to make, we stick
495 * a shell-specific echoOff command in the script. In the latter,
496 * we ignore errors for the entire job, unless the shell has error
497 * control.
498 * If the command is just "..." we take all future commands for this
499 * job to be commands to be executed once the entire graph has been
500 * made and return non-zero to signal that the end of the commands
501 * was reached. These commands are later attached to the postCommands
502 * node and executed by Job_End when all things are done.
503 * This function is called from JobStart via Lst_ForEach.
504 *
505 * Results:
506 * Always 0, unless the command was "..."
507 *
508 * Side Effects:
509 * If the command begins with a '-' and the shell has no error control,
510 * the JOB_IGNERR flag is set in the job descriptor.
511 * If the command is "..." and we're not ignoring such things,
512 * tailCmds is set to the successor node of the cmd.
513 * numCommands is incremented if the command is actually printed.
514 *-----------------------------------------------------------------------
515 */
516 static int
517 JobPrintCommand(cmdp, jobp)
518 ClientData cmdp; /* command string to print */
519 ClientData jobp; /* job for which to print it */
520 {
521 Boolean noSpecials; /* true if we shouldn't worry about
522 * inserting special commands into
523 * the input stream. */
524 Boolean shutUp = FALSE; /* true if we put a no echo command
525 * into the command file */
526 Boolean errOff = FALSE; /* true if we turned error checking
527 * off before printing the command
528 * and need to turn it back on */
529 char *cmdTemplate; /* Template to use when printing the
530 * command */
531 char *cmdStart; /* Start of expanded command */
532 LstNode cmdNode; /* Node for replacing the command */
533 char *cmd = (char *) cmdp;
534 Job *job = (Job *) jobp;
535
536 noSpecials = noExecute && !(job->node->type & OP_MAKE);
537
538 if (strcmp(cmd, "...") == 0) {
539 job->node->type |= OP_SAVE_CMDS;
540 if ((job->flags & JOB_IGNDOTS) == 0) {
541 job->tailCmds = Lst_Succ(Lst_Member(job->node->commands,
542 (ClientData)cmd));
543 return 1;
544 }
545 return 0;
546 }
547
548 #define DBPRINTF(fmt, arg) if (DEBUG(JOB)) { \
549 (void) fprintf(stdout, fmt, arg); \
550 (void) fflush(stdout); \
551 } \
552 (void) fprintf(job->cmdFILE, fmt, arg); \
553 (void) fflush(job->cmdFILE);
554
555 numCommands += 1;
556
557 /*
558 * For debugging, we replace each command with the result of expanding
559 * the variables in the command.
560 */
561 cmdNode = Lst_Member(job->node->commands, (ClientData)cmd);
562 cmdStart = cmd = Var_Subst(NULL, cmd, job->node, FALSE);
563 Lst_Replace(cmdNode, (ClientData)cmdStart);
564
565 cmdTemplate = "%s\n";
566
567 /*
568 * Check for leading @' and -'s to control echoing and error checking.
569 */
570 while (*cmd == '@' || *cmd == '-') {
571 if (*cmd == '@') {
572 shutUp = TRUE;
573 } else {
574 errOff = TRUE;
575 }
576 cmd++;
577 }
578
579 while (isspace((unsigned char) *cmd))
580 cmd++;
581
582 if (shutUp) {
583 if (!(job->flags & JOB_SILENT) && !noSpecials &&
584 commandShell->hasEchoCtl) {
585 DBPRINTF("%s\n", commandShell->echoOff);
586 } else {
587 shutUp = FALSE;
588 }
589 }
590
591 if (errOff) {
592 if ( !(job->flags & JOB_IGNERR) && !noSpecials) {
593 if (commandShell->hasErrCtl) {
594 /*
595 * we don't want the error-control commands showing
596 * up either, so we turn off echoing while executing
597 * them. We could put another field in the shell
598 * structure to tell JobDoOutput to look for this
599 * string too, but why make it any more complex than
600 * it already is?
601 */
602 if (!(job->flags & JOB_SILENT) && !shutUp &&
603 commandShell->hasEchoCtl) {
604 DBPRINTF("%s\n", commandShell->echoOff);
605 DBPRINTF("%s\n", commandShell->ignErr);
606 DBPRINTF("%s\n", commandShell->echoOn);
607 } else {
608 DBPRINTF("%s\n", commandShell->ignErr);
609 }
610 } else if (commandShell->ignErr &&
611 (*commandShell->ignErr != '\0'))
612 {
613 /*
614 * The shell has no error control, so we need to be
615 * weird to get it to ignore any errors from the command.
616 * If echoing is turned on, we turn it off and use the
617 * errCheck template to echo the command. Leave echoing
618 * off so the user doesn't see the weirdness we go through
619 * to ignore errors. Set cmdTemplate to use the weirdness
620 * instead of the simple "%s\n" template.
621 */
622 if (!(job->flags & JOB_SILENT) && !shutUp &&
623 commandShell->hasEchoCtl) {
624 DBPRINTF("%s\n", commandShell->echoOff);
625 DBPRINTF(commandShell->errCheck, cmd);
626 shutUp = TRUE;
627 }
628 cmdTemplate = commandShell->ignErr;
629 /*
630 * The error ignoration (hee hee) is already taken care
631 * of by the ignErr template, so pretend error checking
632 * is still on.
633 */
634 errOff = FALSE;
635 } else {
636 errOff = FALSE;
637 }
638 } else {
639 errOff = FALSE;
640 }
641 }
642
643 DBPRINTF(cmdTemplate, cmd);
644 free(cmdStart);
645
646 if (errOff) {
647 /*
648 * If echoing is already off, there's no point in issuing the
649 * echoOff command. Otherwise we issue it and pretend it was on
650 * for the whole command...
651 */
652 if (!shutUp && !(job->flags & JOB_SILENT) && commandShell->hasEchoCtl){
653 DBPRINTF("%s\n", commandShell->echoOff);
654 shutUp = TRUE;
655 }
656 DBPRINTF("%s\n", commandShell->errCheck);
657 }
658 if (shutUp) {
659 DBPRINTF("%s\n", commandShell->echoOn);
660 }
661 return 0;
662 }
663
664 /*-
665 *-----------------------------------------------------------------------
666 * JobSaveCommand --
667 * Save a command to be executed when everything else is done.
668 * Callback function for JobFinish...
669 *
670 * Results:
671 * Always returns 0
672 *
673 * Side Effects:
674 * The command is tacked onto the end of postCommands's commands list.
675 *
676 *-----------------------------------------------------------------------
677 */
678 static int
679 JobSaveCommand(cmd, gn)
680 ClientData cmd;
681 ClientData gn;
682 {
683 cmd = (ClientData) Var_Subst(NULL, (char *) cmd, (GNode *) gn, FALSE);
684 (void) Lst_AtEnd(postCommands->commands, cmd);
685 return(0);
686 }
687
688
689 /*-
690 *-----------------------------------------------------------------------
691 * JobClose --
692 * Called to close both input and output pipes when a job is finished.
693 *
694 * Results:
695 * Nada
696 *
697 * Side Effects:
698 * The file descriptors associated with the job are closed.
699 *
700 *-----------------------------------------------------------------------
701 */
702 static void
703 JobClose(job)
704 Job *job;
705 {
706 if (usePipes) {
707 #ifdef RMT_WILL_WATCH
708 Rmt_Ignore(job->inPipe);
709 #else
710 FD_CLR(job->inPipe, &outputs);
711 #endif
712 if (job->outPipe != job->inPipe) {
713 (void) close(job->outPipe);
714 }
715 JobDoOutput(job, TRUE);
716 (void) close(job->inPipe);
717 } else {
718 (void) close(job->outFd);
719 JobDoOutput(job, TRUE);
720 }
721 }
722
723 /*-
724 *-----------------------------------------------------------------------
725 * JobFinish --
726 * Do final processing for the given job including updating
727 * parents and starting new jobs as available/necessary. Note
728 * that we pay no attention to the JOB_IGNERR flag here.
729 * This is because when we're called because of a noexecute flag
730 * or something, jstat.w_status is 0 and when called from
731 * Job_CatchChildren, the status is zeroed if it s/b ignored.
732 *
733 * Results:
734 * None
735 *
736 * Side Effects:
737 * Some nodes may be put on the toBeMade queue.
738 * Final commands for the job are placed on postCommands.
739 *
740 * If we got an error and are aborting (aborting == ABORT_ERROR) and
741 * the job list is now empty, we are done for the day.
742 * If we recognized an error (errors !=0), we set the aborting flag
743 * to ABORT_ERROR so no more jobs will be started.
744 *-----------------------------------------------------------------------
745 */
746 /*ARGSUSED*/
747 static void
748 JobFinish(job, status)
749 Job *job; /* job to finish */
750 int *status; /* sub-why job went away */
751 {
752 Boolean done;
753
754 if ((WIFEXITED(*status) &&
755 (((WEXITSTATUS(*status) != 0) && !(job->flags & JOB_IGNERR)))) ||
756 WIFSIGNALED(*status))
757 {
758 /*
759 * If it exited non-zero and either we're doing things our
760 * way or we're not ignoring errors, the job is finished.
761 * Similarly, if the shell died because of a signal
762 * the job is also finished. In these
763 * cases, finish out the job's output before printing the exit
764 * status...
765 */
766 #ifdef REMOTE
767 KILL(job->pid, SIGCONT);
768 #endif
769 JobClose(job);
770 if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
771 (void) fclose(job->cmdFILE);
772 }
773 done = TRUE;
774 #ifdef REMOTE
775 if (job->flags & JOB_REMOTE)
776 Rmt_Done(job->rmtID, job->node);
777 #endif
778 } else if (WIFEXITED(*status)) {
779 /*
780 * Deal with ignored errors in -B mode. We need to print a message
781 * telling of the ignored error as well as setting status.w_status
782 * to 0 so the next command gets run. To do this, we set done to be
783 * TRUE if in -B mode and the job exited non-zero.
784 */
785 done = WEXITSTATUS(*status) != 0;
786 /*
787 * Old comment said: "Note we don't
788 * want to close down any of the streams until we know we're at the
789 * end."
790 * But we do. Otherwise when are we going to print the rest of the
791 * stuff?
792 */
793 JobClose(job);
794 #ifdef REMOTE
795 if (job->flags & JOB_REMOTE)
796 Rmt_Done(job->rmtID, job->node);
797 #endif /* REMOTE */
798 } else {
799 /*
800 * No need to close things down or anything.
801 */
802 done = FALSE;
803 }
804
805 if (done ||
806 WIFSTOPPED(*status) ||
807 (WIFSIGNALED(*status) && (WTERMSIG(*status) == SIGCONT)))
808 {
809 FILE *out;
810
811 if (compatMake && !usePipes && (job->flags & JOB_IGNERR)) {
812 /*
813 * If output is going to a file and this job is ignoring
814 * errors, arrange to have the exit status sent to the
815 * output file as well.
816 */
817 out = fdopen(job->outFd, "w");
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 fork");
1232 } else if (cpid == 0) {
1233
1234 /*
1235 * Must duplicate the input stream down to the child's input and
1236 * reset it to the beginning (again). Since the stream was marked
1237 * close-on-exec, we must clear that bit in the new input.
1238 */
1239 if (dup2(FILENO(job->cmdFILE), 0) == -1)
1240 Punt("Cannot dup2: %s", strerror(errno));
1241 (void) fcntl(0, F_SETFD, 0);
1242 (void) lseek(0, (off_t)0, SEEK_SET);
1243
1244 if (usePipes) {
1245 /*
1246 * Set up the child's output to be routed through the pipe
1247 * we've created for it.
1248 */
1249 if (dup2(job->outPipe, 1) == -1)
1250 Punt("Cannot dup2: %s", strerror(errno));
1251 } else {
1252 /*
1253 * We're capturing output in a file, so we duplicate the
1254 * descriptor to the temporary file into the standard
1255 * output.
1256 */
1257 if (dup2(job->outFd, 1) == -1)
1258 Punt("Cannot dup2: %s", strerror(errno));
1259 }
1260 /*
1261 * The output channels are marked close on exec. This bit was
1262 * duplicated by the dup2 (on some systems), so we have to clear
1263 * it before routing the shell's error output to the same place as
1264 * its standard output.
1265 */
1266 (void) fcntl(1, F_SETFD, 0);
1267 if (dup2(1, 2) == -1)
1268 Punt("Cannot dup2: %s", strerror(errno));
1269
1270 #ifdef USE_PGRP
1271 /*
1272 * We want to switch the child into a different process family so
1273 * we can kill it and all its descendants in one fell swoop,
1274 * by killing its process family, but not commit suicide.
1275 */
1276 # if defined(SYSV)
1277 (void) setsid();
1278 # else
1279 (void) setpgid(0, getpid());
1280 # endif
1281 #endif /* USE_PGRP */
1282
1283 #ifdef REMOTE
1284 if (job->flags & JOB_REMOTE) {
1285 Rmt_Exec(shellPath, argv, FALSE);
1286 } else
1287 #endif /* REMOTE */
1288 (void) execv(shellPath, argv);
1289
1290 (void) write(2, "Could not execute shell\n",
1291 sizeof("Could not execute shell"));
1292 _exit(1);
1293 } else {
1294 #ifdef REMOTE
1295 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 * We're serious here, but if the commands were bogus, we're
1732 * also dead...
1733 */
1734 if (!cmdsOK) {
1735 DieHorribly();
1736 }
1737
1738 if ((tfd = mkstemp(tfile)) == -1)
1739 Punt("Could not create temporary file %s", strerror(errno));
1740 (void) eunlink(tfile);
1741
1742 job->cmdFILE = fdopen(tfd, "w+");
1743 if (job->cmdFILE == NULL) {
1744 Punt("Could not fdopen %s", tfile);
1745 }
1746 (void) fcntl(FILENO(job->cmdFILE), F_SETFD, 1);
1747 /*
1748 * Send the commands to the command file, flush all its buffers then
1749 * rewind and remove the thing.
1750 */
1751 noExec = FALSE;
1752
1753 /*
1754 * used to be backwards; replace when start doing multiple commands
1755 * per shell.
1756 */
1757 if (compatMake) {
1758 /*
1759 * Be compatible: If this is the first time for this node,
1760 * verify its commands are ok and open the commands list for
1761 * sequential access by later invocations of JobStart.
1762 * Once that is done, we take the next command off the list
1763 * and print it to the command file. If the command was an
1764 * ellipsis, note that there's nothing more to execute.
1765 */
1766 if ((job->flags&JOB_FIRST) && (Lst_Open(gn->commands) != SUCCESS)){
1767 cmdsOK = FALSE;
1768 } else {
1769 LstNode ln = Lst_Next(gn->commands);
1770
1771 if ((ln == NILLNODE) ||
1772 JobPrintCommand((ClientData) Lst_Datum(ln),
1773 (ClientData) job))
1774 {
1775 noExec = TRUE;
1776 Lst_Close(gn->commands);
1777 }
1778 if (noExec && !(job->flags & JOB_FIRST)) {
1779 /*
1780 * If we're not going to execute anything, the job
1781 * is done and we need to close down the various
1782 * file descriptors we've opened for output, then
1783 * call JobDoOutput to catch the final characters or
1784 * send the file to the screen... Note that the i/o streams
1785 * are only open if this isn't the first job.
1786 * Note also that this could not be done in
1787 * Job_CatchChildren b/c it wasn't clear if there were
1788 * more commands to execute or not...
1789 */
1790 JobClose(job);
1791 }
1792 }
1793 } else {
1794 /*
1795 * We can do all the commands at once. hooray for sanity
1796 */
1797 numCommands = 0;
1798 Lst_ForEach(gn->commands, JobPrintCommand, (ClientData)job);
1799
1800 /*
1801 * If we didn't print out any commands to the shell script,
1802 * there's not much point in executing the shell, is there?
1803 */
1804 if (numCommands == 0) {
1805 noExec = TRUE;
1806 }
1807 }
1808 } else if (noExecute) {
1809 /*
1810 * Not executing anything -- just print all the commands to stdout
1811 * in one fell swoop. This will still set up job->tailCmds correctly.
1812 */
1813 if (lastNode != gn) {
1814 MESSAGE(stdout, gn);
1815 lastNode = gn;
1816 }
1817 job->cmdFILE = stdout;
1818 /*
1819 * Only print the commands if they're ok, but don't die if they're
1820 * not -- just let the user know they're bad and keep going. It
1821 * doesn't do any harm in this case and may do some good.
1822 */
1823 if (cmdsOK) {
1824 Lst_ForEach(gn->commands, JobPrintCommand, (ClientData)job);
1825 }
1826 /*
1827 * Don't execute the shell, thank you.
1828 */
1829 noExec = TRUE;
1830 } else {
1831 /*
1832 * Just touch the target and note that no shell should be executed.
1833 * Set cmdFILE to stdout to make life easier. Check the commands, too,
1834 * but don't die if they're no good -- it does no harm to keep working
1835 * up the graph.
1836 */
1837 job->cmdFILE = stdout;
1838 Job_Touch(gn, job->flags&JOB_SILENT);
1839 noExec = TRUE;
1840 }
1841
1842 /*
1843 * If we're not supposed to execute a shell, don't.
1844 */
1845 if (noExec) {
1846 /*
1847 * Unlink and close the command file if we opened one
1848 */
1849 if (job->cmdFILE != stdout) {
1850 if (job->cmdFILE != NULL)
1851 (void) fclose(job->cmdFILE);
1852 } else {
1853 (void) fflush(stdout);
1854 }
1855
1856 /*
1857 * We only want to work our way up the graph if we aren't here because
1858 * the commands for the job were no good.
1859 */
1860 if (cmdsOK) {
1861 if (aborting == 0) {
1862 if (job->tailCmds != NILLNODE) {
1863 Lst_ForEachFrom(job->node->commands, job->tailCmds,
1864 JobSaveCommand,
1865 (ClientData)job->node);
1866 }
1867 Make_Update(job->node);
1868 }
1869 free((Address)job);
1870 return(JOB_FINISHED);
1871 } else {
1872 free((Address)job);
1873 return(JOB_ERROR);
1874 }
1875 } else {
1876 (void) fflush(job->cmdFILE);
1877 }
1878
1879 /*
1880 * Set up the control arguments to the shell. This is based on the flags
1881 * set earlier for this job.
1882 */
1883 JobMakeArgv(job, argv);
1884
1885 /*
1886 * If we're using pipes to catch output, create the pipe by which we'll
1887 * get the shell's output. If we're using files, print out that we're
1888 * starting a job and then set up its temporary-file name.
1889 */
1890 if (!compatMake || (job->flags & JOB_FIRST)) {
1891 if (usePipes) {
1892 int fd[2];
1893 if (pipe(fd) == -1)
1894 Punt("Cannot create pipe: %s", strerror(errno));
1895 job->inPipe = fd[0];
1896 job->outPipe = fd[1];
1897 (void) fcntl(job->inPipe, F_SETFD, 1);
1898 (void) fcntl(job->outPipe, F_SETFD, 1);
1899 } else {
1900 (void) fprintf(stdout, "Remaking `%s'\n", gn->name);
1901 (void) fflush(stdout);
1902 (void) strcpy(job->outFile, TMPPAT);
1903 job->outFd = mkstemp(job->outFile);
1904 (void) fcntl(job->outFd, F_SETFD, 1);
1905 }
1906 }
1907
1908 #ifdef REMOTE
1909 if (!(gn->type & OP_NOEXPORT) && !(runLocalFirst && nLocal < maxLocal)) {
1910 #ifdef RMT_NO_EXEC
1911 local = !Rmt_Export(shellPath, argv, job);
1912 #else
1913 local = !Rmt_Begin(shellPath, argv, job->node);
1914 #endif /* RMT_NO_EXEC */
1915 if (!local) {
1916 job->flags |= JOB_REMOTE;
1917 }
1918 } else
1919 #endif
1920 local = TRUE;
1921
1922 if (local && (((nLocal >= maxLocal) &&
1923 !(job->flags & JOB_SPECIAL) &&
1924 #ifdef REMOTE
1925 (!(gn->type & OP_NOEXPORT) || (maxLocal != 0))
1926 #else
1927 (maxLocal != 0)
1928 #endif
1929 )))
1930 {
1931 /*
1932 * The job can only be run locally, but we've hit the limit of
1933 * local concurrency, so put the job on hold until some other job
1934 * finishes. Note that the special jobs (.BEGIN, .INTERRUPT and .END)
1935 * may be run locally even when the local limit has been reached
1936 * (e.g. when maxLocal == 0), though they will be exported if at
1937 * all possible. In addition, any target marked with .NOEXPORT will
1938 * be run locally if maxLocal is 0.
1939 */
1940 jobFull = TRUE;
1941
1942 if (DEBUG(JOB)) {
1943 (void) fprintf(stdout, "Can only run job locally.\n");
1944 (void) fflush(stdout);
1945 }
1946 job->flags |= JOB_RESTART;
1947 (void) Lst_AtEnd(stoppedJobs, (ClientData)job);
1948 } else {
1949 if ((nLocal >= maxLocal) && local) {
1950 /*
1951 * If we're running this job locally as a special case (see above),
1952 * at least say the table is full.
1953 */
1954 jobFull = TRUE;
1955 if (DEBUG(JOB)) {
1956 (void) fprintf(stdout, "Local job queue is full.\n");
1957 (void) fflush(stdout);
1958 }
1959 }
1960 JobExec(job, argv);
1961 }
1962 return(JOB_RUNNING);
1963 }
1964
1965 static char *
1966 JobOutput(job, cp, endp, msg)
1967 register Job *job;
1968 register char *cp, *endp;
1969 int msg;
1970 {
1971 register char *ecp;
1972
1973 if (commandShell->noPrint) {
1974 ecp = Str_FindSubstring(cp, commandShell->noPrint);
1975 while (ecp != NULL) {
1976 if (cp != ecp) {
1977 *ecp = '\0';
1978 if (msg && job->node != lastNode) {
1979 MESSAGE(stdout, job->node);
1980 lastNode = job->node;
1981 }
1982 /*
1983 * The only way there wouldn't be a newline after
1984 * this line is if it were the last in the buffer.
1985 * however, since the non-printable comes after it,
1986 * there must be a newline, so we don't print one.
1987 */
1988 (void) fprintf(stdout, "%s", cp);
1989 (void) fflush(stdout);
1990 }
1991 cp = ecp + commandShell->noPLen;
1992 if (cp != endp) {
1993 /*
1994 * Still more to print, look again after skipping
1995 * the whitespace following the non-printable
1996 * command....
1997 */
1998 cp++;
1999 while (*cp == ' ' || *cp == '\t' || *cp == '\n') {
2000 cp++;
2001 }
2002 ecp = Str_FindSubstring(cp, commandShell->noPrint);
2003 } else {
2004 return cp;
2005 }
2006 }
2007 }
2008 return cp;
2009 }
2010
2011 /*-
2012 *-----------------------------------------------------------------------
2013 * JobDoOutput --
2014 * This function is called at different times depending on
2015 * whether the user has specified that output is to be collected
2016 * via pipes or temporary files. In the former case, we are called
2017 * whenever there is something to read on the pipe. We collect more
2018 * output from the given job and store it in the job's outBuf. If
2019 * this makes up a line, we print it tagged by the job's identifier,
2020 * as necessary.
2021 * If output has been collected in a temporary file, we open the
2022 * file and read it line by line, transfering it to our own
2023 * output channel until the file is empty. At which point we
2024 * remove the temporary file.
2025 * In both cases, however, we keep our figurative eye out for the
2026 * 'noPrint' line for the shell from which the output came. If
2027 * we recognize a line, we don't print it. If the command is not
2028 * alone on the line (the character after it is not \0 or \n), we
2029 * do print whatever follows it.
2030 *
2031 * Results:
2032 * None
2033 *
2034 * Side Effects:
2035 * curPos may be shifted as may the contents of outBuf.
2036 *-----------------------------------------------------------------------
2037 */
2038 STATIC void
2039 JobDoOutput(job, finish)
2040 register Job *job; /* the job whose output needs printing */
2041 Boolean finish; /* TRUE if this is the last time we'll be
2042 * called for this job */
2043 {
2044 Boolean gotNL = FALSE; /* true if got a newline */
2045 Boolean fbuf; /* true if our buffer filled up */
2046 register int nr; /* number of bytes read */
2047 register int i; /* auxiliary index into outBuf */
2048 register int max; /* limit for i (end of current data) */
2049 int nRead; /* (Temporary) number of bytes read */
2050
2051 FILE *oFILE; /* Stream pointer to shell's output file */
2052 char inLine[132];
2053
2054
2055 if (usePipes) {
2056 /*
2057 * Read as many bytes as will fit in the buffer.
2058 */
2059 end_loop:
2060 gotNL = FALSE;
2061 fbuf = FALSE;
2062
2063 nRead = read(job->inPipe, &job->outBuf[job->curPos],
2064 JOB_BUFSIZE - job->curPos);
2065 if (nRead < 0) {
2066 if (DEBUG(JOB)) {
2067 perror("JobDoOutput(piperead)");
2068 }
2069 nr = 0;
2070 } else {
2071 nr = nRead;
2072 }
2073
2074 /*
2075 * If we hit the end-of-file (the job is dead), we must flush its
2076 * remaining output, so pretend we read a newline if there's any
2077 * output remaining in the buffer.
2078 * Also clear the 'finish' flag so we stop looping.
2079 */
2080 if ((nr == 0) && (job->curPos != 0)) {
2081 job->outBuf[job->curPos] = '\n';
2082 nr = 1;
2083 finish = FALSE;
2084 } else if (nr == 0) {
2085 finish = FALSE;
2086 }
2087
2088 /*
2089 * Look for the last newline in the bytes we just got. If there is
2090 * one, break out of the loop with 'i' as its index and gotNL set
2091 * TRUE.
2092 */
2093 max = job->curPos + nr;
2094 for (i = job->curPos + nr - 1; i >= job->curPos; i--) {
2095 if (job->outBuf[i] == '\n') {
2096 gotNL = TRUE;
2097 break;
2098 } else if (job->outBuf[i] == '\0') {
2099 /*
2100 * Why?
2101 */
2102 job->outBuf[i] = ' ';
2103 }
2104 }
2105
2106 if (!gotNL) {
2107 job->curPos += nr;
2108 if (job->curPos == JOB_BUFSIZE) {
2109 /*
2110 * If we've run out of buffer space, we have no choice
2111 * but to print the stuff. sigh.
2112 */
2113 fbuf = TRUE;
2114 i = job->curPos;
2115 }
2116 }
2117 if (gotNL || fbuf) {
2118 /*
2119 * Need to send the output to the screen. Null terminate it
2120 * first, overwriting the newline character if there was one.
2121 * So long as the line isn't one we should filter (according
2122 * to the shell description), we print the line, preceeded
2123 * by a target banner if this target isn't the same as the
2124 * one for which we last printed something.
2125 * The rest of the data in the buffer are then shifted down
2126 * to the start of the buffer and curPos is set accordingly.
2127 */
2128 job->outBuf[i] = '\0';
2129 if (i >= job->curPos) {
2130 char *cp;
2131
2132 cp = JobOutput(job, job->outBuf, &job->outBuf[i], FALSE);
2133
2134 /*
2135 * There's still more in that thar buffer. This time, though,
2136 * we know there's no newline at the end, so we add one of
2137 * our own free will.
2138 */
2139 if (*cp != '\0') {
2140 if (job->node != lastNode) {
2141 MESSAGE(stdout, job->node);
2142 lastNode = job->node;
2143 }
2144 (void) fprintf(stdout, "%s%s", cp, gotNL ? "\n" : "");
2145 (void) fflush(stdout);
2146 }
2147 }
2148 if (i < max - 1) {
2149 /* shift the remaining characters down */
2150 (void) memcpy(job->outBuf, &job->outBuf[i + 1], max - (i + 1));
2151 job->curPos = max - (i + 1);
2152
2153 } else {
2154 /*
2155 * We have written everything out, so we just start over
2156 * from the start of the buffer. No copying. No nothing.
2157 */
2158 job->curPos = 0;
2159 }
2160 }
2161 if (finish) {
2162 /*
2163 * If the finish flag is true, we must loop until we hit
2164 * end-of-file on the pipe. This is guaranteed to happen
2165 * eventually since the other end of the pipe is now closed
2166 * (we closed it explicitly and the child has exited). When
2167 * we do get an EOF, finish will be set FALSE and we'll fall
2168 * through and out.
2169 */
2170 goto end_loop;
2171 }
2172 } else {
2173 /*
2174 * We've been called to retrieve the output of the job from the
2175 * temporary file where it's been squirreled away. This consists of
2176 * opening the file, reading the output line by line, being sure not
2177 * to print the noPrint line for the shell we used, then close and
2178 * remove the temporary file. Very simple.
2179 *
2180 * Change to read in blocks and do FindSubString type things as for
2181 * pipes? That would allow for "@echo -n..."
2182 */
2183 oFILE = fopen(job->outFile, "r");
2184 if (oFILE != NULL) {
2185 (void) fprintf(stdout, "Results of making %s:\n", job->node->name);
2186 (void) fflush(stdout);
2187 while (fgets(inLine, sizeof(inLine), oFILE) != NULL) {
2188 register char *cp, *endp, *oendp;
2189
2190 cp = inLine;
2191 oendp = endp = inLine + strlen(inLine);
2192 if (endp[-1] == '\n') {
2193 *--endp = '\0';
2194 }
2195 cp = JobOutput(job, inLine, endp, FALSE);
2196
2197 /*
2198 * There's still more in that thar buffer. This time, though,
2199 * we know there's no newline at the end, so we add one of
2200 * our own free will.
2201 */
2202 (void) fprintf(stdout, "%s", cp);
2203 (void) fflush(stdout);
2204 if (endp != oendp) {
2205 (void) fprintf(stdout, "\n");
2206 (void) fflush(stdout);
2207 }
2208 }
2209 (void) fclose(oFILE);
2210 (void) eunlink(job->outFile);
2211 }
2212 }
2213 }
2214
2215 /*-
2216 *-----------------------------------------------------------------------
2217 * Job_CatchChildren --
2218 * Handle the exit of a child. Called from Make_Make.
2219 *
2220 * Results:
2221 * none.
2222 *
2223 * Side Effects:
2224 * The job descriptor is removed from the list of children.
2225 *
2226 * Notes:
2227 * We do waits, blocking or not, according to the wisdom of our
2228 * caller, until there are no more children to report. For each
2229 * job, call JobFinish to finish things off. This will take care of
2230 * putting jobs on the stoppedJobs queue.
2231 *
2232 *-----------------------------------------------------------------------
2233 */
2234 void
2235 Job_CatchChildren(block)
2236 Boolean block; /* TRUE if should block on the wait. */
2237 {
2238 int pid; /* pid of dead child */
2239 register Job *job; /* job descriptor for dead child */
2240 LstNode jnode; /* list element for finding job */
2241 int status; /* Exit/termination status */
2242
2243 /*
2244 * Don't even bother if we know there's no one around.
2245 */
2246 if (nLocal == 0) {
2247 return;
2248 }
2249
2250 while ((pid = waitpid((pid_t) -1, &status,
2251 (block?0:WNOHANG)|WUNTRACED)) > 0)
2252 {
2253 if (DEBUG(JOB)) {
2254 (void) fprintf(stdout, "Process %d exited or stopped %x.\n", pid,
2255 status);
2256 (void) fflush(stdout);
2257 }
2258
2259
2260 jnode = Lst_Find(jobs, (ClientData)&pid, JobCmpPid);
2261
2262 if (jnode == NILLNODE) {
2263 if (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGCONT)) {
2264 jnode = Lst_Find(stoppedJobs, (ClientData) &pid, JobCmpPid);
2265 if (jnode == NILLNODE) {
2266 Error("Resumed child (%d) not in table", pid);
2267 continue;
2268 }
2269 job = (Job *)Lst_Datum(jnode);
2270 (void) Lst_Remove(stoppedJobs, jnode);
2271 } else {
2272 Error("Child (%d) not in table?", pid);
2273 continue;
2274 }
2275 } else {
2276 job = (Job *) Lst_Datum(jnode);
2277 (void) Lst_Remove(jobs, jnode);
2278 nJobs -= 1;
2279 if (jobFull && DEBUG(JOB)) {
2280 (void) fprintf(stdout, "Job queue is no longer full.\n");
2281 (void) fflush(stdout);
2282 }
2283 jobFull = FALSE;
2284 #ifdef REMOTE
2285 if (!(job->flags & JOB_REMOTE)) {
2286 if (DEBUG(JOB)) {
2287 (void) fprintf(stdout,
2288 "Job queue has one fewer local process.\n");
2289 (void) fflush(stdout);
2290 }
2291 nLocal -= 1;
2292 }
2293 #else
2294 nLocal -= 1;
2295 #endif
2296 }
2297
2298 JobFinish(job, &status);
2299 }
2300 }
2301
2302 /*-
2303 *-----------------------------------------------------------------------
2304 * Job_CatchOutput --
2305 * Catch the output from our children, if we're using
2306 * pipes do so. Otherwise just block time until we get a
2307 * signal (most likely a SIGCHLD) since there's no point in
2308 * just spinning when there's nothing to do and the reaping
2309 * of a child can wait for a while.
2310 *
2311 * Results:
2312 * None
2313 *
2314 * Side Effects:
2315 * Output is read from pipes if we're piping.
2316 * -----------------------------------------------------------------------
2317 */
2318 void
2319 Job_CatchOutput()
2320 {
2321 int nfds;
2322 struct timeval timeout;
2323 fd_set readfds;
2324 register LstNode ln;
2325 register Job *job;
2326 #ifdef RMT_WILL_WATCH
2327 int pnJobs; /* Previous nJobs */
2328 #endif
2329
2330 (void) fflush(stdout);
2331 #ifdef RMT_WILL_WATCH
2332 pnJobs = nJobs;
2333
2334 /*
2335 * It is possible for us to be called with nJobs equal to 0. This happens
2336 * if all the jobs finish and a job that is stopped cannot be run
2337 * locally (eg if maxLocal is 0) and cannot be exported. The job will
2338 * be placed back on the stoppedJobs queue, Job_Empty() will return false,
2339 * Make_Run will call us again when there's nothing for which to wait.
2340 * nJobs never changes, so we loop forever. Hence the check. It could
2341 * be argued that we should sleep for a bit so as not to swamp the
2342 * exportation system with requests. Perhaps we should.
2343 *
2344 * NOTE: IT IS THE RESPONSIBILITY OF Rmt_Wait TO CALL Job_CatchChildren
2345 * IN A TIMELY FASHION TO CATCH ANY LOCALLY RUNNING JOBS THAT EXIT.
2346 * It may use the variable nLocal to determine if it needs to call
2347 * Job_CatchChildren (if nLocal is 0, there's nothing for which to
2348 * wait...)
2349 */
2350 while (nJobs != 0 && pnJobs == nJobs) {
2351 Rmt_Wait();
2352 }
2353 #else
2354 if (usePipes) {
2355 readfds = outputs;
2356 timeout.tv_sec = SEL_SEC;
2357 timeout.tv_usec = SEL_USEC;
2358
2359 if ((nfds = select(FD_SETSIZE, &readfds, (fd_set *) 0,
2360 (fd_set *) 0, &timeout)) <= 0)
2361 return;
2362 else {
2363 if (Lst_Open(jobs) == FAILURE) {
2364 Punt("Cannot open job table");
2365 }
2366 while (nfds && (ln = Lst_Next(jobs)) != NILLNODE) {
2367 job = (Job *) Lst_Datum(ln);
2368 if (FD_ISSET(job->inPipe, &readfds)) {
2369 JobDoOutput(job, FALSE);
2370 nfds -= 1;
2371 }
2372 }
2373 Lst_Close(jobs);
2374 }
2375 }
2376 #endif /* RMT_WILL_WATCH */
2377 }
2378
2379 /*-
2380 *-----------------------------------------------------------------------
2381 * Job_Make --
2382 * Start the creation of a target. Basically a front-end for
2383 * JobStart used by the Make module.
2384 *
2385 * Results:
2386 * None.
2387 *
2388 * Side Effects:
2389 * Another job is started.
2390 *
2391 *-----------------------------------------------------------------------
2392 */
2393 void
2394 Job_Make(gn)
2395 GNode *gn;
2396 {
2397 (void) JobStart(gn, 0, NULL);
2398 }
2399
2400 /*-
2401 *-----------------------------------------------------------------------
2402 * Job_Init --
2403 * Initialize the process module
2404 *
2405 * Results:
2406 * none
2407 *
2408 * Side Effects:
2409 * lists and counters are initialized
2410 *-----------------------------------------------------------------------
2411 */
2412 void
2413 Job_Init(maxproc, maxlocal)
2414 int maxproc; /* the greatest number of jobs which may be
2415 * running at one time */
2416 int maxlocal; /* the greatest number of local jobs which may
2417 * be running at once. */
2418 {
2419 GNode *begin; /* node for commands to do at the very start */
2420
2421 jobs = Lst_Init(FALSE);
2422 stoppedJobs = Lst_Init(FALSE);
2423 maxJobs = maxproc;
2424 maxLocal = maxlocal;
2425 nJobs = 0;
2426 nLocal = 0;
2427 jobFull = FALSE;
2428
2429 aborting = 0;
2430 errors = 0;
2431
2432 lastNode = NILGNODE;
2433
2434 if (maxJobs == 1
2435 #ifdef REMOTE
2436 || noMessages
2437 #endif
2438 ) {
2439 /*
2440 * If only one job can run at a time, there's no need for a banner,
2441 * no is there?
2442 */
2443 targFmt = "";
2444 } else {
2445 targFmt = TARG_FMT;
2446 }
2447
2448 if (shellPath == NULL) {
2449 /*
2450 * The user didn't specify a shell to use, so we are using the
2451 * default one... Both the absolute path and the last component
2452 * must be set. The last component is taken from the 'name' field
2453 * of the default shell description pointed-to by commandShell.
2454 * All default shells are located in _PATH_DEFSHELLDIR.
2455 */
2456 shellName = commandShell->name;
2457 shellPath = str_concat(_PATH_DEFSHELLDIR, shellName, STR_ADDSLASH);
2458 }
2459
2460 if (commandShell->exit == NULL) {
2461 commandShell->exit = "";
2462 }
2463 if (commandShell->echo == NULL) {
2464 commandShell->echo = "";
2465 }
2466
2467 /*
2468 * Catch the four signals that POSIX specifies if they aren't ignored.
2469 * JobPassSig will take care of calling JobInterrupt if appropriate.
2470 */
2471 if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
2472 (void) signal(SIGINT, JobPassSig);
2473 }
2474 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
2475 (void) signal(SIGHUP, JobPassSig);
2476 }
2477 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
2478 (void) signal(SIGQUIT, JobPassSig);
2479 }
2480 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
2481 (void) signal(SIGTERM, JobPassSig);
2482 }
2483 /*
2484 * There are additional signals that need to be caught and passed if
2485 * either the export system wants to be told directly of signals or if
2486 * we're giving each job its own process group (since then it won't get
2487 * signals from the terminal driver as we own the terminal)
2488 */
2489 #if defined(RMT_WANTS_SIGNALS) || defined(USE_PGRP)
2490 if (signal(SIGTSTP, SIG_IGN) != SIG_IGN) {
2491 (void) signal(SIGTSTP, JobPassSig);
2492 }
2493 if (signal(SIGTTOU, SIG_IGN) != SIG_IGN) {
2494 (void) signal(SIGTTOU, JobPassSig);
2495 }
2496 if (signal(SIGTTIN, SIG_IGN) != SIG_IGN) {
2497 (void) signal(SIGTTIN, JobPassSig);
2498 }
2499 if (signal(SIGWINCH, SIG_IGN) != SIG_IGN) {
2500 (void) signal(SIGWINCH, JobPassSig);
2501 }
2502 #endif
2503
2504 begin = Targ_FindNode(".BEGIN", TARG_NOCREATE);
2505
2506 if (begin != NILGNODE) {
2507 JobStart(begin, JOB_SPECIAL, (Job *)0);
2508 while (nJobs) {
2509 Job_CatchOutput();
2510 #ifndef RMT_WILL_WATCH
2511 Job_CatchChildren(!usePipes);
2512 #endif /* RMT_WILL_WATCH */
2513 }
2514 }
2515 postCommands = Targ_FindNode(".END", TARG_CREATE);
2516 }
2517
2518 /*-
2519 *-----------------------------------------------------------------------
2520 * Job_Full --
2521 * See if the job table is full. It is considered full if it is OR
2522 * if we are in the process of aborting OR if we have
2523 * reached/exceeded our local quota. This prevents any more jobs
2524 * from starting up.
2525 *
2526 * Results:
2527 * TRUE if the job table is full, FALSE otherwise
2528 * Side Effects:
2529 * None.
2530 *-----------------------------------------------------------------------
2531 */
2532 Boolean
2533 Job_Full()
2534 {
2535 return(aborting || jobFull);
2536 }
2537
2538 /*-
2539 *-----------------------------------------------------------------------
2540 * Job_Empty --
2541 * See if the job table is empty. Because the local concurrency may
2542 * be set to 0, it is possible for the job table to become empty,
2543 * while the list of stoppedJobs remains non-empty. In such a case,
2544 * we want to restart as many jobs as we can.
2545 *
2546 * Results:
2547 * TRUE if it is. FALSE if it ain't.
2548 *
2549 * Side Effects:
2550 * None.
2551 *
2552 * -----------------------------------------------------------------------
2553 */
2554 Boolean
2555 Job_Empty()
2556 {
2557 if (nJobs == 0) {
2558 if (!Lst_IsEmpty(stoppedJobs) && !aborting) {
2559 /*
2560 * The job table is obviously not full if it has no jobs in
2561 * it...Try and restart the stopped jobs.
2562 */
2563 jobFull = FALSE;
2564 JobRestartJobs();
2565 return(FALSE);
2566 } else {
2567 return(TRUE);
2568 }
2569 } else {
2570 return(FALSE);
2571 }
2572 }
2573
2574 /*-
2575 *-----------------------------------------------------------------------
2576 * JobMatchShell --
2577 * Find a matching shell in 'shells' given its final component.
2578 *
2579 * Results:
2580 * A pointer to the Shell structure.
2581 *
2582 * Side Effects:
2583 * None.
2584 *
2585 *-----------------------------------------------------------------------
2586 */
2587 static Shell *
2588 JobMatchShell(name)
2589 char *name; /* Final component of shell path */
2590 {
2591 register Shell *sh; /* Pointer into shells table */
2592 Shell *match; /* Longest-matching shell */
2593 register char *cp1,
2594 *cp2;
2595 char *eoname;
2596
2597 eoname = name + strlen(name);
2598
2599 match = NULL;
2600
2601 for (sh = shells; sh->name != NULL; sh++) {
2602 for (cp1 = eoname - strlen(sh->name), cp2 = sh->name;
2603 *cp1 != '\0' && *cp1 == *cp2;
2604 cp1++, cp2++) {
2605 continue;
2606 }
2607 if (*cp1 != *cp2) {
2608 continue;
2609 } else if (match == NULL || strlen(match->name) < strlen(sh->name)) {
2610 match = sh;
2611 }
2612 }
2613 return(match == NULL ? sh : match);
2614 }
2615
2616 /*-
2617 *-----------------------------------------------------------------------
2618 * Job_ParseShell --
2619 * Parse a shell specification and set up commandShell, shellPath
2620 * and shellName appropriately.
2621 *
2622 * Results:
2623 * FAILURE if the specification was incorrect.
2624 *
2625 * Side Effects:
2626 * commandShell points to a Shell structure (either predefined or
2627 * created from the shell spec), shellPath is the full path of the
2628 * shell described by commandShell, while shellName is just the
2629 * final component of shellPath.
2630 *
2631 * Notes:
2632 * A shell specification consists of a .SHELL target, with dependency
2633 * operator, followed by a series of blank-separated words. Double
2634 * quotes can be used to use blanks in words. A backslash escapes
2635 * anything (most notably a double-quote and a space) and
2636 * provides the functionality it does in C. Each word consists of
2637 * keyword and value separated by an equal sign. There should be no
2638 * unnecessary spaces in the word. The keywords are as follows:
2639 * name Name of shell.
2640 * path Location of shell. Overrides "name" if given
2641 * quiet Command to turn off echoing.
2642 * echo Command to turn echoing on
2643 * filter Result of turning off echoing that shouldn't be
2644 * printed.
2645 * echoFlag Flag to turn echoing on at the start
2646 * errFlag Flag to turn error checking on at the start
2647 * hasErrCtl True if shell has error checking control
2648 * check Command to turn on error checking if hasErrCtl
2649 * is TRUE or template of command to echo a command
2650 * for which error checking is off if hasErrCtl is
2651 * FALSE.
2652 * ignore Command to turn off error checking if hasErrCtl
2653 * is TRUE or template of command to execute a
2654 * command so as to ignore any errors it returns if
2655 * hasErrCtl is FALSE.
2656 *
2657 *-----------------------------------------------------------------------
2658 */
2659 ReturnStatus
2660 Job_ParseShell(line)
2661 char *line; /* The shell spec */
2662 {
2663 char **words;
2664 int wordCount;
2665 register char **argv;
2666 register int argc;
2667 char *path;
2668 Shell newShell;
2669 Boolean fullSpec = FALSE;
2670
2671 while (isspace((unsigned char)*line)) {
2672 line++;
2673 }
2674
2675 if (shellArgv)
2676 free(shellArgv);
2677
2678 words = brk_string(line, &wordCount, TRUE, &shellArgv);
2679
2680 memset((Address)&newShell, 0, sizeof(newShell));
2681
2682 /*
2683 * Parse the specification by keyword
2684 */
2685 for (path = NULL, argc = wordCount - 1, argv = words;
2686 argc != 0;
2687 argc--, argv++) {
2688 if (strncmp(*argv, "path=", 5) == 0) {
2689 path = &argv[0][5];
2690 } else if (strncmp(*argv, "name=", 5) == 0) {
2691 newShell.name = &argv[0][5];
2692 } else {
2693 if (strncmp(*argv, "quiet=", 6) == 0) {
2694 newShell.echoOff = &argv[0][6];
2695 } else if (strncmp(*argv, "echo=", 5) == 0) {
2696 newShell.echoOn = &argv[0][5];
2697 } else if (strncmp(*argv, "filter=", 7) == 0) {
2698 newShell.noPrint = &argv[0][7];
2699 newShell.noPLen = strlen(newShell.noPrint);
2700 } else if (strncmp(*argv, "echoFlag=", 9) == 0) {
2701 newShell.echo = &argv[0][9];
2702 } else if (strncmp(*argv, "errFlag=", 8) == 0) {
2703 newShell.exit = &argv[0][8];
2704 } else if (strncmp(*argv, "hasErrCtl=", 10) == 0) {
2705 char c = argv[0][10];
2706 newShell.hasErrCtl = !((c != 'Y') && (c != 'y') &&
2707 (c != 'T') && (c != 't'));
2708 } else if (strncmp(*argv, "check=", 6) == 0) {
2709 newShell.errCheck = &argv[0][6];
2710 } else if (strncmp(*argv, "ignore=", 7) == 0) {
2711 newShell.ignErr = &argv[0][7];
2712 } else {
2713 Parse_Error(PARSE_FATAL, "Unknown keyword \"%s\"",
2714 *argv);
2715 free(words);
2716 return(FAILURE);
2717 }
2718 fullSpec = TRUE;
2719 }
2720 }
2721
2722 if (path == NULL) {
2723 /*
2724 * If no path was given, the user wants one of the pre-defined shells,
2725 * yes? So we find the one s/he wants with the help of JobMatchShell
2726 * and set things up the right way. shellPath will be set up by
2727 * Job_Init.
2728 */
2729 if (newShell.name == NULL) {
2730 Parse_Error(PARSE_FATAL, "Neither path nor name specified");
2731 return(FAILURE);
2732 } else {
2733 commandShell = JobMatchShell(newShell.name);
2734 shellName = newShell.name;
2735 }
2736 } else {
2737 /*
2738 * The user provided a path. If s/he gave nothing else (fullSpec is
2739 * FALSE), try and find a matching shell in the ones we know of.
2740 * Else we just take the specification at its word and copy it
2741 * to a new location. In either case, we need to record the
2742 * path the user gave for the shell.
2743 */
2744 shellPath = path;
2745 path = strrchr(path, '/');
2746 if (path == NULL) {
2747 path = shellPath;
2748 } else {
2749 path += 1;
2750 }
2751 if (newShell.name != NULL) {
2752 shellName = newShell.name;
2753 } else {
2754 shellName = path;
2755 }
2756 if (!fullSpec) {
2757 commandShell = JobMatchShell(shellName);
2758 } else {
2759 commandShell = (Shell *) emalloc(sizeof(Shell));
2760 *commandShell = newShell;
2761 }
2762 }
2763
2764 if (commandShell->echoOn && commandShell->echoOff) {
2765 commandShell->hasEchoCtl = TRUE;
2766 }
2767
2768 if (!commandShell->hasErrCtl) {
2769 if (commandShell->errCheck == NULL) {
2770 commandShell->errCheck = "";
2771 }
2772 if (commandShell->ignErr == NULL) {
2773 commandShell->ignErr = "%s\n";
2774 }
2775 }
2776
2777 /*
2778 * Do not free up the words themselves, since they might be in use by the
2779 * shell specification.
2780 */
2781 free(words);
2782 return SUCCESS;
2783 }
2784
2785 /*-
2786 *-----------------------------------------------------------------------
2787 * JobInterrupt --
2788 * Handle the receipt of an interrupt.
2789 *
2790 * Results:
2791 * None
2792 *
2793 * Side Effects:
2794 * All children are killed. Another job will be started if the
2795 * .INTERRUPT target was given.
2796 *-----------------------------------------------------------------------
2797 */
2798 static void
2799 JobInterrupt(runINTERRUPT, signo)
2800 int runINTERRUPT; /* Non-zero if commands for the .INTERRUPT
2801 * target should be executed */
2802 int signo; /* signal received */
2803 {
2804 LstNode ln; /* element in job table */
2805 Job *job; /* job descriptor in that element */
2806 GNode *interrupt; /* the node describing the .INTERRUPT target */
2807
2808 aborting = ABORT_INTERRUPT;
2809
2810 (void) Lst_Open(jobs);
2811 while ((ln = Lst_Next(jobs)) != NILLNODE) {
2812 job = (Job *) Lst_Datum(ln);
2813
2814 if (!Targ_Precious(job->node)) {
2815 char *file = (job->node->path == NULL ?
2816 job->node->name :
2817 job->node->path);
2818 if (!noExecute && eunlink(file) != -1) {
2819 Error("*** %s removed", file);
2820 }
2821 }
2822 #ifdef RMT_WANTS_SIGNALS
2823 if (job->flags & JOB_REMOTE) {
2824 /*
2825 * If job is remote, let the Rmt module do the killing.
2826 */
2827 if (!Rmt_Signal(job, signo)) {
2828 /*
2829 * If couldn't kill the thing, finish it out now with an
2830 * error code, since no exit report will come in likely.
2831 */
2832 int status;
2833
2834 status.w_status = 0;
2835 status.w_retcode = 1;
2836 JobFinish(job, &status);
2837 }
2838 } else if (job->pid) {
2839 KILL(job->pid, signo);
2840 }
2841 #else
2842 if (job->pid) {
2843 if (DEBUG(JOB)) {
2844 (void) fprintf(stdout,
2845 "JobInterrupt passing signal to child %d.\n",
2846 job->pid);
2847 (void) fflush(stdout);
2848 }
2849 KILL(job->pid, signo);
2850 }
2851 #endif /* RMT_WANTS_SIGNALS */
2852 }
2853
2854 #ifdef REMOTE
2855 (void)Lst_Open(stoppedJobs);
2856 while ((ln = Lst_Next(stoppedJobs)) != NILLNODE) {
2857 job = (Job *) Lst_Datum(ln);
2858
2859 if (job->flags & JOB_RESTART) {
2860 if (DEBUG(JOB)) {
2861 (void) fprintf(stdout, "%s%s",
2862 "JobInterrupt skipping job on stopped queue",
2863 "-- it was waiting to be restarted.\n");
2864 (void) fflush(stdout);
2865 }
2866 continue;
2867 }
2868 if (!Targ_Precious(job->node)) {
2869 char *file = (job->node->path == NULL ?
2870 job->node->name :
2871 job->node->path);
2872 if (eunlink(file) == 0) {
2873 Error("*** %s removed", file);
2874 }
2875 }
2876 /*
2877 * Resume the thing so it will take the signal.
2878 */
2879 if (DEBUG(JOB)) {
2880 (void) fprintf(stdout,
2881 "JobInterrupt passing CONT to stopped child %d.\n",
2882 job->pid);
2883 (void) fflush(stdout);
2884 }
2885 KILL(job->pid, SIGCONT);
2886 #ifdef RMT_WANTS_SIGNALS
2887 if (job->flags & JOB_REMOTE) {
2888 /*
2889 * If job is remote, let the Rmt module do the killing.
2890 */
2891 if (!Rmt_Signal(job, SIGINT)) {
2892 /*
2893 * If couldn't kill the thing, finish it out now with an
2894 * error code, since no exit report will come in likely.
2895 */
2896 int status;
2897 status.w_status = 0;
2898 status.w_retcode = 1;
2899 JobFinish(job, &status);
2900 }
2901 } else if (job->pid) {
2902 if (DEBUG(JOB)) {
2903 (void) fprintf(stdout,
2904 "JobInterrupt passing interrupt to stopped child %d.\n",
2905 job->pid);
2906 (void) fflush(stdout);
2907 }
2908 KILL(job->pid, SIGINT);
2909 }
2910 #endif /* RMT_WANTS_SIGNALS */
2911 }
2912 #endif
2913 Lst_Close(stoppedJobs);
2914
2915 if (runINTERRUPT && !touchFlag) {
2916 interrupt = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
2917 if (interrupt != NILGNODE) {
2918 ignoreErrors = FALSE;
2919
2920 JobStart(interrupt, JOB_IGNDOTS, (Job *)0);
2921 while (nJobs) {
2922 Job_CatchOutput();
2923 #ifndef RMT_WILL_WATCH
2924 Job_CatchChildren(!usePipes);
2925 #endif /* RMT_WILL_WATCH */
2926 }
2927 }
2928 }
2929 exit(signo);
2930 }
2931
2932 /*
2933 *-----------------------------------------------------------------------
2934 * Job_Finish --
2935 * Do final processing such as the running of the commands
2936 * attached to the .END target.
2937 *
2938 * Results:
2939 * Number of errors reported.
2940 *
2941 * Side Effects:
2942 * The process' temporary file (tfile) is removed if it still
2943 * existed.
2944 *-----------------------------------------------------------------------
2945 */
2946 int
2947 Job_Finish()
2948 {
2949 if (postCommands != NILGNODE && !Lst_IsEmpty(postCommands->commands)) {
2950 if (errors) {
2951 Error("Errors reported so .END ignored");
2952 } else {
2953 JobStart(postCommands, JOB_SPECIAL | JOB_IGNDOTS, NULL);
2954
2955 while (nJobs) {
2956 Job_CatchOutput();
2957 #ifndef RMT_WILL_WATCH
2958 Job_CatchChildren(!usePipes);
2959 #endif /* RMT_WILL_WATCH */
2960 }
2961 }
2962 }
2963 return(errors);
2964 }
2965
2966 /*-
2967 *-----------------------------------------------------------------------
2968 * Job_End --
2969 * Cleanup any memory used by the jobs module
2970 *
2971 * Results:
2972 * None.
2973 *
2974 * Side Effects:
2975 * Memory is freed
2976 *-----------------------------------------------------------------------
2977 */
2978 void
2979 Job_End()
2980 {
2981 #ifdef CLEANUP
2982 if (shellArgv)
2983 free(shellArgv);
2984 #endif
2985 }
2986
2987 /*-
2988 *-----------------------------------------------------------------------
2989 * Job_Wait --
2990 * Waits for all running jobs to finish and returns. Sets 'aborting'
2991 * to ABORT_WAIT to prevent other jobs from starting.
2992 *
2993 * Results:
2994 * None.
2995 *
2996 * Side Effects:
2997 * Currently running jobs finish.
2998 *
2999 *-----------------------------------------------------------------------
3000 */
3001 void
3002 Job_Wait()
3003 {
3004 aborting = ABORT_WAIT;
3005 while (nJobs != 0) {
3006 Job_CatchOutput();
3007 #ifndef RMT_WILL_WATCH
3008 Job_CatchChildren(!usePipes);
3009 #endif /* RMT_WILL_WATCH */
3010 }
3011 aborting = 0;
3012 }
3013
3014 /*-
3015 *-----------------------------------------------------------------------
3016 * Job_AbortAll --
3017 * Abort all currently running jobs without handling output or anything.
3018 * This function is to be called only in the event of a major
3019 * error. Most definitely NOT to be called from JobInterrupt.
3020 *
3021 * Results:
3022 * None
3023 *
3024 * Side Effects:
3025 * All children are killed, not just the firstborn
3026 *-----------------------------------------------------------------------
3027 */
3028 void
3029 Job_AbortAll()
3030 {
3031 LstNode ln; /* element in job table */
3032 Job *job; /* the job descriptor in that element */
3033 int foo;
3034
3035 aborting = ABORT_ERROR;
3036
3037 if (nJobs) {
3038
3039 (void) Lst_Open(jobs);
3040 while ((ln = Lst_Next(jobs)) != NILLNODE) {
3041 job = (Job *) Lst_Datum(ln);
3042
3043 /*
3044 * kill the child process with increasingly drastic signals to make
3045 * darn sure it's dead.
3046 */
3047 #ifdef RMT_WANTS_SIGNALS
3048 if (job->flags & JOB_REMOTE) {
3049 Rmt_Signal(job, SIGINT);
3050 Rmt_Signal(job, SIGKILL);
3051 } else {
3052 KILL(job->pid, SIGINT);
3053 KILL(job->pid, SIGKILL);
3054 }
3055 #else
3056 KILL(job->pid, SIGINT);
3057 KILL(job->pid, SIGKILL);
3058 #endif /* RMT_WANTS_SIGNALS */
3059 }
3060 }
3061
3062 /*
3063 * Catch as many children as want to report in at first, then give up
3064 */
3065 while (waitpid((pid_t) -1, &foo, WNOHANG) > 0)
3066 continue;
3067 }
3068
3069 #ifdef REMOTE
3070 /*-
3071 *-----------------------------------------------------------------------
3072 * JobFlagForMigration --
3073 * Handle the eviction of a child. Called from RmtStatusChange.
3074 * Flags the child as remigratable and then suspends it.
3075 *
3076 * Results:
3077 * none.
3078 *
3079 * Side Effects:
3080 * The job descriptor is flagged for remigration.
3081 *
3082 *-----------------------------------------------------------------------
3083 */
3084 void
3085 JobFlagForMigration(hostID)
3086 int hostID; /* ID of host we used, for matching children. */
3087 {
3088 register Job *job; /* job descriptor for dead child */
3089 LstNode jnode; /* list element for finding job */
3090
3091 if (DEBUG(JOB)) {
3092 (void) fprintf(stdout, "JobFlagForMigration(%d) called.\n", hostID);
3093 (void) fflush(stdout);
3094 }
3095 jnode = Lst_Find(jobs, (ClientData)hostID, JobCmpRmtID);
3096
3097 if (jnode == NILLNODE) {
3098 jnode = Lst_Find(stoppedJobs, (ClientData)hostID, JobCmpRmtID);
3099 if (jnode == NILLNODE) {
3100 if (DEBUG(JOB)) {
3101 Error("Evicting host(%d) not in table", hostID);
3102 }
3103 return;
3104 }
3105 }
3106 job = (Job *) Lst_Datum(jnode);
3107
3108 if (DEBUG(JOB)) {
3109 (void) fprintf(stdout,
3110 "JobFlagForMigration(%d) found job '%s'.\n", hostID,
3111 job->node->name);
3112 (void) fflush(stdout);
3113 }
3114
3115 KILL(job->pid, SIGSTOP);
3116
3117 job->flags |= JOB_REMIGRATE;
3118 }
3119
3120 #endif
3121
3122 /*-
3124 *-----------------------------------------------------------------------
3125 * JobRestartJobs --
3126 * Tries to restart stopped jobs if there are slots available.
3127 * Note that this tries to restart them regardless of pending errors.
3128 * It's not good to leave stopped jobs lying around!
3129 *
3130 * Results:
3131 * None.
3132 *
3133 * Side Effects:
3134 * Resumes(and possibly migrates) jobs.
3135 *
3136 *-----------------------------------------------------------------------
3137 */
3138 static void
3139 JobRestartJobs()
3140 {
3141 while (!jobFull && !Lst_IsEmpty(stoppedJobs)) {
3142 if (DEBUG(JOB)) {
3143 (void) fprintf(stdout,
3144 "Job queue is not full. Restarting a stopped job.\n");
3145 (void) fflush(stdout);
3146 }
3147 JobRestart((Job *)Lst_DeQueue(stoppedJobs));
3148 }
3149 }
3150