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