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