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