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