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