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