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