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