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