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