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