xref: /netbsd-src/usr.bin/make/meta.c (revision e6c7e151de239c49d2e38720a061ed9d1fa99309)
1 /*      $NetBSD: meta.c,v 1.81 2020/04/03 03:32:28 sjg Exp $ */
2 
3 /*
4  * Implement 'meta' mode.
5  * Adapted from John Birrell's patches to FreeBSD make.
6  * --sjg
7  */
8 /*
9  * Copyright (c) 2009-2016, Juniper Networks, Inc.
10  * Portions Copyright (c) 2009, John Birrell.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 #if defined(USE_META)
34 
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38 #include <sys/stat.h>
39 #include <libgen.h>
40 #include <errno.h>
41 #if !defined(HAVE_CONFIG_H) || defined(HAVE_ERR_H)
42 #include <err.h>
43 #endif
44 
45 #include "make.h"
46 #include "job.h"
47 
48 #ifdef USE_FILEMON
49 #include "filemon/filemon.h"
50 #endif
51 
52 static BuildMon Mybm;			/* for compat */
53 static Lst metaBailiwick;		/* our scope of control */
54 static char *metaBailiwickStr;		/* string storage for the list */
55 static Lst metaIgnorePaths;		/* paths we deliberately ignore */
56 static char *metaIgnorePathsStr;	/* string storage for the list */
57 
58 #ifndef MAKE_META_IGNORE_PATHS
59 #define MAKE_META_IGNORE_PATHS ".MAKE.META.IGNORE_PATHS"
60 #endif
61 #ifndef MAKE_META_IGNORE_PATTERNS
62 #define MAKE_META_IGNORE_PATTERNS ".MAKE.META.IGNORE_PATTERNS"
63 #endif
64 #ifndef MAKE_META_IGNORE_FILTER
65 #define MAKE_META_IGNORE_FILTER ".MAKE.META.IGNORE_FILTER"
66 #endif
67 
68 Boolean useMeta = FALSE;
69 static Boolean useFilemon = FALSE;
70 static Boolean writeMeta = FALSE;
71 static Boolean metaMissing = FALSE;	/* oodate if missing */
72 static Boolean filemonMissing = FALSE;	/* oodate if missing */
73 static Boolean metaEnv = FALSE;		/* don't save env unless asked */
74 static Boolean metaVerbose = FALSE;
75 static Boolean metaIgnoreCMDs = FALSE;	/* ignore CMDs in .meta files */
76 static Boolean metaIgnorePatterns = FALSE; /* do we need to do pattern matches */
77 static Boolean metaIgnoreFilter = FALSE;   /* do we have more complex filtering? */
78 static Boolean metaCurdirOk = FALSE;	/* write .meta in .CURDIR Ok? */
79 static Boolean metaSilent = FALSE;	/* if we have a .meta be SILENT */
80 
81 extern Boolean forceJobs;
82 extern Boolean comatMake;
83 extern char    **environ;
84 
85 #define	MAKE_META_PREFIX	".MAKE.META.PREFIX"
86 
87 #ifndef N2U
88 # define N2U(n, u)   (((n) + ((u) - 1)) / (u))
89 #endif
90 #ifndef ROUNDUP
91 # define ROUNDUP(n, u)   (N2U((n), (u)) * (u))
92 #endif
93 
94 #if !defined(HAVE_STRSEP)
95 # define strsep(s, d) stresep((s), (d), 0)
96 #endif
97 
98 /*
99  * Filemon is a kernel module which snoops certain syscalls.
100  *
101  * C chdir
102  * E exec
103  * F [v]fork
104  * L [sym]link
105  * M rename
106  * R read
107  * W write
108  * S stat
109  *
110  * See meta_oodate below - we mainly care about 'E' and 'R'.
111  *
112  * We can still use meta mode without filemon, but
113  * the benefits are more limited.
114  */
115 #ifdef USE_FILEMON
116 
117 /*
118  * Open the filemon device.
119  */
120 static void
121 meta_open_filemon(BuildMon *pbm)
122 {
123     int dupfd;
124 
125     pbm->mon_fd = -1;
126     pbm->filemon = NULL;
127     if (!useFilemon || !pbm->mfp)
128 	return;
129 
130     pbm->filemon = filemon_open();
131     if (pbm->filemon == NULL) {
132 	useFilemon = FALSE;
133 	warn("Could not open filemon %s", filemon_path());
134 	return;
135     }
136 
137     /*
138      * We use a file outside of '.'
139      * to avoid a FreeBSD kernel bug where unlink invalidates
140      * cwd causing getcwd to do a lot more work.
141      * We only care about the descriptor.
142      */
143     pbm->mon_fd = mkTempFile("filemon.XXXXXX", NULL);
144     if ((dupfd = dup(pbm->mon_fd)) == -1) {
145 	err(1, "Could not dup filemon output!");
146     }
147     (void)fcntl(dupfd, F_SETFD, FD_CLOEXEC);
148     if (filemon_setfd(pbm->filemon, dupfd) == -1) {
149 	err(1, "Could not set filemon file descriptor!");
150     }
151     /* we don't need these once we exec */
152     (void)fcntl(pbm->mon_fd, F_SETFD, FD_CLOEXEC);
153 }
154 
155 /*
156  * Read the build monitor output file and write records to the target's
157  * metadata file.
158  */
159 static int
160 filemon_read(FILE *mfp, int fd)
161 {
162     char buf[BUFSIZ];
163     int n;
164     int error;
165 
166     /* Check if we're not writing to a meta data file.*/
167     if (mfp == NULL) {
168 	if (fd >= 0)
169 	    close(fd);			/* not interested */
170 	return 0;
171     }
172     /* rewind */
173     (void)lseek(fd, (off_t)0, SEEK_SET);
174 
175     error = 0;
176     fprintf(mfp, "\n-- filemon acquired metadata --\n");
177 
178     while ((n = read(fd, buf, sizeof(buf))) > 0) {
179 	if ((int)fwrite(buf, 1, n, mfp) < n)
180 	    error = EIO;
181     }
182     fflush(mfp);
183     if (close(fd) < 0)
184 	error = errno;
185     return error;
186 }
187 #endif
188 
189 /*
190  * when realpath() fails,
191  * we use this, to clean up ./ and ../
192  */
193 static void
194 eat_dots(char *buf, size_t bufsz, int dots)
195 {
196     char *cp;
197     char *cp2;
198     const char *eat;
199     size_t eatlen;
200 
201     switch (dots) {
202     case 1:
203 	eat = "/./";
204 	eatlen = 2;
205 	break;
206     case 2:
207 	eat = "/../";
208 	eatlen = 3;
209 	break;
210     default:
211 	return;
212     }
213 
214     do {
215 	cp = strstr(buf, eat);
216 	if (cp) {
217 	    cp2 = cp + eatlen;
218 	    if (dots == 2 && cp > buf) {
219 		do {
220 		    cp--;
221 		} while (cp > buf && *cp != '/');
222 	    }
223 	    if (*cp == '/') {
224 		strlcpy(cp, cp2, bufsz - (cp - buf));
225 	    } else {
226 		return;			/* can't happen? */
227 	    }
228 	}
229     } while (cp);
230 }
231 
232 static char *
233 meta_name(struct GNode *gn, char *mname, size_t mnamelen,
234 	  const char *dname,
235 	  const char *tname,
236 	  const char *cwd)
237 {
238     char buf[MAXPATHLEN];
239     char *rp;
240     char *cp;
241     char *tp;
242     char *dtp;
243     size_t ldname;
244 
245     /*
246      * Weed out relative paths from the target file name.
247      * We have to be careful though since if target is a
248      * symlink, the result will be unstable.
249      * So we use realpath() just to get the dirname, and leave the
250      * basename as given to us.
251      */
252     if ((cp = strrchr(tname, '/'))) {
253 	if (cached_realpath(tname, buf)) {
254 	    if ((rp = strrchr(buf, '/'))) {
255 		rp++;
256 		cp++;
257 		if (strcmp(cp, rp) != 0)
258 		    strlcpy(rp, cp, sizeof(buf) - (rp - buf));
259 	    }
260 	    tname = buf;
261 	} else {
262 	    /*
263 	     * We likely have a directory which is about to be made.
264 	     * We pretend realpath() succeeded, to have a chance
265 	     * of generating the same meta file name that we will
266 	     * next time through.
267 	     */
268 	    if (tname[0] == '/') {
269 		strlcpy(buf, tname, sizeof(buf));
270 	    } else {
271 		snprintf(buf, sizeof(buf), "%s/%s", cwd, tname);
272 	    }
273 	    eat_dots(buf, sizeof(buf), 1);	/* ./ */
274 	    eat_dots(buf, sizeof(buf), 2);	/* ../ */
275 	    tname = buf;
276 	}
277     }
278     /* on some systems dirname may modify its arg */
279     tp = bmake_strdup(tname);
280     dtp = dirname(tp);
281     if (strcmp(dname, dtp) == 0)
282 	snprintf(mname, mnamelen, "%s.meta", tname);
283     else {
284 	ldname = strlen(dname);
285 	if (strncmp(dname, dtp, ldname) == 0 && dtp[ldname] == '/')
286 	    snprintf(mname, mnamelen, "%s/%s.meta", dname, &tname[ldname+1]);
287 	else
288 	    snprintf(mname, mnamelen, "%s/%s.meta", dname, tname);
289 
290 	/*
291 	 * Replace path separators in the file name after the
292 	 * current object directory path.
293 	 */
294 	cp = mname + strlen(dname) + 1;
295 
296 	while (*cp != '\0') {
297 	    if (*cp == '/')
298 		*cp = '_';
299 	    cp++;
300 	}
301     }
302     free(tp);
303     return (mname);
304 }
305 
306 /*
307  * Return true if running ${.MAKE}
308  * Bypassed if target is flagged .MAKE
309  */
310 static int
311 is_submake(void *cmdp, void *gnp)
312 {
313     static char *p_make = NULL;
314     static int p_len;
315     char  *cmd = cmdp;
316     GNode *gn = gnp;
317     char *mp = NULL;
318     char *cp;
319     char *cp2;
320     int rc = 0;				/* keep looking */
321 
322     if (!p_make) {
323 	p_make = Var_Value(".MAKE", gn, &cp);
324 	p_len = strlen(p_make);
325     }
326     cp = strchr(cmd, '$');
327     if ((cp)) {
328 	mp = Var_Subst(NULL, cmd, gn, VARF_WANTRES);
329 	cmd = mp;
330     }
331     cp2 = strstr(cmd, p_make);
332     if ((cp2)) {
333 	switch (cp2[p_len]) {
334 	case '\0':
335 	case ' ':
336 	case '\t':
337 	case '\n':
338 	    rc = 1;
339 	    break;
340 	}
341 	if (cp2 > cmd && rc > 0) {
342 	    switch (cp2[-1]) {
343 	    case ' ':
344 	    case '\t':
345 	    case '\n':
346 		break;
347 	    default:
348 		rc = 0;			/* no match */
349 		break;
350 	    }
351 	}
352     }
353     free(mp);
354     return (rc);
355 }
356 
357 typedef struct meta_file_s {
358     FILE *fp;
359     GNode *gn;
360 } meta_file_t;
361 
362 static int
363 printCMD(void *cmdp, void *mfpp)
364 {
365     meta_file_t *mfp = mfpp;
366     char *cmd = cmdp;
367     char *cp = NULL;
368 
369     if (strchr(cmd, '$')) {
370 	cmd = cp = Var_Subst(NULL, cmd, mfp->gn, VARF_WANTRES);
371     }
372     fprintf(mfp->fp, "CMD %s\n", cmd);
373     free(cp);
374     return 0;
375 }
376 
377 /*
378  * Certain node types never get a .meta file
379  */
380 #define SKIP_META_TYPE(_type) do { \
381     if ((gn->type & __CONCAT(OP_, _type))) {	\
382 	if (verbose) { \
383 	    fprintf(debug_file, "Skipping meta for %s: .%s\n", \
384 		    gn->name, __STRING(_type));		       \
385 	} \
386 	return FALSE; \
387     } \
388 } while (0)
389 
390 
391 /*
392  * Do we need/want a .meta file ?
393  */
394 static Boolean
395 meta_needed(GNode *gn, const char *dname, const char *tname,
396 	     char *objdir, int verbose)
397 {
398     struct stat fs;
399 
400     if (verbose)
401 	verbose = DEBUG(META);
402 
403     /* This may be a phony node which we don't want meta data for... */
404     /* Skip .meta for .BEGIN, .END, .ERROR etc as well. */
405     /* Or it may be explicitly flagged as .NOMETA */
406     SKIP_META_TYPE(NOMETA);
407     /* Unless it is explicitly flagged as .META */
408     if (!(gn->type & OP_META)) {
409 	SKIP_META_TYPE(PHONY);
410 	SKIP_META_TYPE(SPECIAL);
411 	SKIP_META_TYPE(MAKE);
412     }
413 
414     /* Check if there are no commands to execute. */
415     if (Lst_IsEmpty(gn->commands)) {
416 	if (verbose)
417 	    fprintf(debug_file, "Skipping meta for %s: no commands\n",
418 		    gn->name);
419 	return FALSE;
420     }
421     if ((gn->type & (OP_META|OP_SUBMAKE)) == OP_SUBMAKE) {
422 	/* OP_SUBMAKE is a bit too aggressive */
423 	if (Lst_ForEach(gn->commands, is_submake, gn)) {
424 	    if (DEBUG(META))
425 		fprintf(debug_file, "Skipping meta for %s: .SUBMAKE\n",
426 			gn->name);
427 	    return FALSE;
428 	}
429     }
430 
431     /* The object directory may not exist. Check it.. */
432     if (cached_stat(dname, &fs) != 0) {
433 	if (verbose)
434 	    fprintf(debug_file, "Skipping meta for %s: no .OBJDIR\n",
435 		    gn->name);
436 	return FALSE;
437     }
438 
439     /* make sure these are canonical */
440     if (cached_realpath(dname, objdir))
441 	dname = objdir;
442 
443     /* If we aren't in the object directory, don't create a meta file. */
444     if (!metaCurdirOk && strcmp(curdir, dname) == 0) {
445 	if (verbose)
446 	    fprintf(debug_file, "Skipping meta for %s: .OBJDIR == .CURDIR\n",
447 		    gn->name);
448 	return FALSE;
449     }
450     return TRUE;
451 }
452 
453 
454 static FILE *
455 meta_create(BuildMon *pbm, GNode *gn)
456 {
457     meta_file_t mf;
458     char buf[MAXPATHLEN];
459     char objdir[MAXPATHLEN];
460     char **ptr;
461     const char *dname;
462     const char *tname;
463     char *fname;
464     const char *cp;
465     char *p[5];				/* >= possible uses */
466     int i;
467 
468     mf.fp = NULL;
469     i = 0;
470 
471     dname = Var_Value(".OBJDIR", gn, &p[i++]);
472     tname = Var_Value(TARGET, gn, &p[i++]);
473 
474     /* if this succeeds objdir is realpath of dname */
475     if (!meta_needed(gn, dname, tname, objdir, TRUE))
476 	goto out;
477     dname = objdir;
478 
479     if (metaVerbose) {
480 	char *mp;
481 
482 	/* Describe the target we are building */
483 	mp = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", gn, VARF_WANTRES);
484 	if (*mp)
485 	    fprintf(stdout, "%s\n", mp);
486 	free(mp);
487     }
488     /* Get the basename of the target */
489     if ((cp = strrchr(tname, '/')) == NULL) {
490 	cp = tname;
491     } else {
492 	cp++;
493     }
494 
495     fflush(stdout);
496 
497     if (!writeMeta)
498 	/* Don't create meta data. */
499 	goto out;
500 
501     fname = meta_name(gn, pbm->meta_fname, sizeof(pbm->meta_fname),
502 		      dname, tname, objdir);
503 
504 #ifdef DEBUG_META_MODE
505     if (DEBUG(META))
506 	fprintf(debug_file, "meta_create: %s\n", fname);
507 #endif
508 
509     if ((mf.fp = fopen(fname, "w")) == NULL)
510 	err(1, "Could not open meta file '%s'", fname);
511 
512     fprintf(mf.fp, "# Meta data file %s\n", fname);
513 
514     mf.gn = gn;
515 
516     Lst_ForEach(gn->commands, printCMD, &mf);
517 
518     fprintf(mf.fp, "CWD %s\n", getcwd(buf, sizeof(buf)));
519     fprintf(mf.fp, "TARGET %s\n", tname);
520     cp = Var_Value(".OODATE", gn, &p[i++]);
521     if (cp && *cp) {
522 	    fprintf(mf.fp, "OODATE %s\n", cp);
523     }
524     if (metaEnv) {
525 	for (ptr = environ; *ptr != NULL; ptr++)
526 	    fprintf(mf.fp, "ENV %s\n", *ptr);
527     }
528 
529     fprintf(mf.fp, "-- command output --\n");
530     fflush(mf.fp);
531 
532     Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
533     Var_Append(".MAKE.META.CREATED", fname, VAR_GLOBAL);
534 
535     gn->type |= OP_META;		/* in case anyone wants to know */
536     if (metaSilent) {
537 	    gn->type |= OP_SILENT;
538     }
539  out:
540     for (i--; i >= 0; i--) {
541 	free(p[i]);
542     }
543 
544     return (mf.fp);
545 }
546 
547 static Boolean
548 boolValue(char *s)
549 {
550     switch(*s) {
551     case '0':
552     case 'N':
553     case 'n':
554     case 'F':
555     case 'f':
556 	return FALSE;
557     }
558     return TRUE;
559 }
560 
561 /*
562  * Initialization we need before reading makefiles.
563  */
564 void
565 meta_init(void)
566 {
567 #ifdef USE_FILEMON
568 	/* this allows makefiles to test if we have filemon support */
569 	Var_Set(".MAKE.PATH_FILEMON", filemon_path(), VAR_GLOBAL, 0);
570 #endif
571 }
572 
573 
574 #define get_mode_bf(bf, token) \
575     if ((cp = strstr(make_mode, token))) \
576 	bf = boolValue(&cp[sizeof(token) - 1])
577 
578 /*
579  * Initialization we need after reading makefiles.
580  */
581 void
582 meta_mode_init(const char *make_mode)
583 {
584     static int once = 0;
585     char *cp;
586 
587     useMeta = TRUE;
588     useFilemon = TRUE;
589     writeMeta = TRUE;
590 
591     if (make_mode) {
592 	if (strstr(make_mode, "env"))
593 	    metaEnv = TRUE;
594 	if (strstr(make_mode, "verb"))
595 	    metaVerbose = TRUE;
596 	if (strstr(make_mode, "read"))
597 	    writeMeta = FALSE;
598 	if (strstr(make_mode, "nofilemon"))
599 	    useFilemon = FALSE;
600 	if (strstr(make_mode, "ignore-cmd"))
601 	    metaIgnoreCMDs = TRUE;
602 	if (useFilemon)
603 	    get_mode_bf(filemonMissing, "missing-filemon=");
604 	get_mode_bf(metaCurdirOk, "curdirok=");
605 	get_mode_bf(metaMissing, "missing-meta=");
606 	get_mode_bf(metaSilent, "silent=");
607     }
608     if (metaVerbose && !Var_Exists(MAKE_META_PREFIX, VAR_GLOBAL)) {
609 	/*
610 	 * The default value for MAKE_META_PREFIX
611 	 * prints the absolute path of the target.
612 	 * This works be cause :H will generate '.' if there is no /
613 	 * and :tA will resolve that to cwd.
614 	 */
615 	Var_Set(MAKE_META_PREFIX, "Building ${.TARGET:H:tA}/${.TARGET:T}", VAR_GLOBAL, 0);
616     }
617     if (once)
618 	return;
619     once = 1;
620     memset(&Mybm, 0, sizeof(Mybm));
621     /*
622      * We consider ourselves master of all within ${.MAKE.META.BAILIWICK}
623      */
624     metaBailiwick = Lst_Init(FALSE);
625     metaBailiwickStr = Var_Subst(NULL, "${.MAKE.META.BAILIWICK:O:u:tA}",
626 	VAR_GLOBAL, VARF_WANTRES);
627     if (metaBailiwickStr) {
628 	str2Lst_Append(metaBailiwick, metaBailiwickStr, NULL);
629     }
630     /*
631      * We ignore any paths that start with ${.MAKE.META.IGNORE_PATHS}
632      */
633     metaIgnorePaths = Lst_Init(FALSE);
634     Var_Append(MAKE_META_IGNORE_PATHS,
635 	       "/dev /etc /proc /tmp /var/run /var/tmp ${TMPDIR}", VAR_GLOBAL);
636     metaIgnorePathsStr = Var_Subst(NULL,
637 		   "${" MAKE_META_IGNORE_PATHS ":O:u:tA}", VAR_GLOBAL,
638 		   VARF_WANTRES);
639     if (metaIgnorePathsStr) {
640 	str2Lst_Append(metaIgnorePaths, metaIgnorePathsStr, NULL);
641     }
642 
643     /*
644      * We ignore any paths that match ${.MAKE.META.IGNORE_PATTERNS}
645      */
646     cp = NULL;
647     if (Var_Value(MAKE_META_IGNORE_PATTERNS, VAR_GLOBAL, &cp)) {
648 	metaIgnorePatterns = TRUE;
649 	free(cp);
650     }
651     cp = NULL;
652     if (Var_Value(MAKE_META_IGNORE_FILTER, VAR_GLOBAL, &cp)) {
653 	metaIgnoreFilter = TRUE;
654 	free(cp);
655     }
656 }
657 
658 /*
659  * In each case below we allow for job==NULL
660  */
661 void
662 meta_job_start(Job *job, GNode *gn)
663 {
664     BuildMon *pbm;
665 
666     if (job != NULL) {
667 	pbm = &job->bm;
668     } else {
669 	pbm = &Mybm;
670     }
671     pbm->mfp = meta_create(pbm, gn);
672 #ifdef USE_FILEMON_ONCE
673     /* compat mode we open the filemon dev once per command */
674     if (job == NULL)
675 	return;
676 #endif
677 #ifdef USE_FILEMON
678     if (pbm->mfp != NULL && useFilemon) {
679 	meta_open_filemon(pbm);
680     } else {
681 	pbm->mon_fd = -1;
682 	pbm->filemon = NULL;
683     }
684 #endif
685 }
686 
687 /*
688  * The child calls this before doing anything.
689  * It does not disturb our state.
690  */
691 void
692 meta_job_child(Job *job)
693 {
694 #ifdef USE_FILEMON
695     BuildMon *pbm;
696 
697     if (job != NULL) {
698 	pbm = &job->bm;
699     } else {
700 	pbm = &Mybm;
701     }
702     if (pbm->mfp != NULL) {
703 	close(fileno(pbm->mfp));
704 	if (useFilemon && pbm->filemon) {
705 	    pid_t pid;
706 
707 	    pid = getpid();
708 	    if (filemon_setpid_child(pbm->filemon, pid) == -1) {
709 		err(1, "Could not set filemon pid!");
710 	    }
711 	}
712     }
713 #endif
714 }
715 
716 void
717 meta_job_parent(Job *job, pid_t pid)
718 {
719 #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
720     BuildMon *pbm;
721 
722     if (job != NULL) {
723 	pbm = &job->bm;
724     } else {
725 	pbm = &Mybm;
726     }
727     if (useFilemon && pbm->filemon) {
728 	filemon_setpid_parent(pbm->filemon, pid);
729     }
730 #endif
731 }
732 
733 int
734 meta_job_fd(Job *job)
735 {
736 #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
737     BuildMon *pbm;
738 
739     if (job != NULL) {
740 	pbm = &job->bm;
741     } else {
742 	pbm = &Mybm;
743     }
744     if (useFilemon && pbm->filemon) {
745 	return filemon_readfd(pbm->filemon);
746     }
747 #endif
748     return -1;
749 }
750 
751 int
752 meta_job_event(Job *job)
753 {
754 #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
755     BuildMon *pbm;
756 
757     if (job != NULL) {
758 	pbm = &job->bm;
759     } else {
760 	pbm = &Mybm;
761     }
762     if (useFilemon && pbm->filemon) {
763 	return filemon_process(pbm->filemon);
764     }
765 #endif
766     return 0;
767 }
768 
769 void
770 meta_job_error(Job *job, GNode *gn, int flags, int status)
771 {
772     char cwd[MAXPATHLEN];
773     BuildMon *pbm;
774 
775     if (job != NULL) {
776 	pbm = &job->bm;
777 	if (!gn)
778 	    gn = job->node;
779     } else {
780 	pbm = &Mybm;
781     }
782     if (pbm->mfp != NULL) {
783 	fprintf(pbm->mfp, "\n*** Error code %d%s\n",
784 		status,
785 		(flags & JOB_IGNERR) ?
786 		"(ignored)" : "");
787     }
788     if (gn) {
789 	Var_Set(".ERROR_TARGET", gn->path ? gn->path : gn->name, VAR_GLOBAL, 0);
790     }
791     getcwd(cwd, sizeof(cwd));
792     Var_Set(".ERROR_CWD", cwd, VAR_GLOBAL, 0);
793     if (pbm->meta_fname[0]) {
794 	Var_Set(".ERROR_META_FILE", pbm->meta_fname, VAR_GLOBAL, 0);
795     }
796     meta_job_finish(job);
797 }
798 
799 void
800 meta_job_output(Job *job, char *cp, const char *nl)
801 {
802     BuildMon *pbm;
803 
804     if (job != NULL) {
805 	pbm = &job->bm;
806     } else {
807 	pbm = &Mybm;
808     }
809     if (pbm->mfp != NULL) {
810 	if (metaVerbose) {
811 	    static char *meta_prefix = NULL;
812 	    static int meta_prefix_len;
813 
814 	    if (!meta_prefix) {
815 		char *cp2;
816 
817 		meta_prefix = Var_Subst(NULL, "${" MAKE_META_PREFIX "}",
818 					VAR_GLOBAL, VARF_WANTRES);
819 		if ((cp2 = strchr(meta_prefix, '$')))
820 		    meta_prefix_len = cp2 - meta_prefix;
821 		else
822 		    meta_prefix_len = strlen(meta_prefix);
823 	    }
824 	    if (strncmp(cp, meta_prefix, meta_prefix_len) == 0) {
825 		cp = strchr(cp+1, '\n');
826 		if (!cp++)
827 		    return;
828 	    }
829 	}
830 	fprintf(pbm->mfp, "%s%s", cp, nl);
831     }
832 }
833 
834 int
835 meta_cmd_finish(void *pbmp)
836 {
837     int error = 0;
838     BuildMon *pbm = pbmp;
839 #ifdef USE_FILEMON
840     int x;
841 #endif
842 
843     if (!pbm)
844 	pbm = &Mybm;
845 
846 #ifdef USE_FILEMON
847     if (pbm->filemon) {
848 	while (filemon_process(pbm->filemon) > 0)
849 	    continue;
850 	if (filemon_close(pbm->filemon) == -1)
851 	    error = errno;
852 	x = filemon_read(pbm->mfp, pbm->mon_fd);
853 	if (error == 0 && x != 0)
854 	    error = x;
855 	pbm->mon_fd = -1;
856 	pbm->filemon = NULL;
857     } else
858 #endif
859 	fprintf(pbm->mfp, "\n");	/* ensure end with newline */
860     return error;
861 }
862 
863 int
864 meta_job_finish(Job *job)
865 {
866     BuildMon *pbm;
867     int error = 0;
868     int x;
869 
870     if (job != NULL) {
871 	pbm = &job->bm;
872     } else {
873 	pbm = &Mybm;
874     }
875     if (pbm->mfp != NULL) {
876 	error = meta_cmd_finish(pbm);
877 	x = fclose(pbm->mfp);
878 	if (error == 0 && x != 0)
879 	    error = errno;
880 	pbm->mfp = NULL;
881 	pbm->meta_fname[0] = '\0';
882     }
883     return error;
884 }
885 
886 void
887 meta_finish(void)
888 {
889     Lst_Destroy(metaBailiwick, NULL);
890     free(metaBailiwickStr);
891     Lst_Destroy(metaIgnorePaths, NULL);
892     free(metaIgnorePathsStr);
893 }
894 
895 /*
896  * Fetch a full line from fp - growing bufp if needed
897  * Return length in bufp.
898  */
899 static int
900 fgetLine(char **bufp, size_t *szp, int o, FILE *fp)
901 {
902     char *buf = *bufp;
903     size_t bufsz = *szp;
904     struct stat fs;
905     int x;
906 
907     if (fgets(&buf[o], bufsz - o, fp) != NULL) {
908     check_newline:
909 	x = o + strlen(&buf[o]);
910 	if (buf[x - 1] == '\n')
911 	    return x;
912 	/*
913 	 * We need to grow the buffer.
914 	 * The meta file can give us a clue.
915 	 */
916 	if (fstat(fileno(fp), &fs) == 0) {
917 	    size_t newsz;
918 	    char *p;
919 
920 	    newsz = ROUNDUP((fs.st_size / 2), BUFSIZ);
921 	    if (newsz <= bufsz)
922 		newsz = ROUNDUP(fs.st_size, BUFSIZ);
923 	    if (newsz <= bufsz)
924 		return x;		/* truncated */
925 	    if (DEBUG(META))
926 		fprintf(debug_file, "growing buffer %zu -> %zu\n",
927 			bufsz, newsz);
928 	    p = bmake_realloc(buf, newsz);
929 	    if (p) {
930 		*bufp = buf = p;
931 		*szp = bufsz = newsz;
932 		/* fetch the rest */
933 		if (!fgets(&buf[x], bufsz - x, fp))
934 		    return x;		/* truncated! */
935 		goto check_newline;
936 	    }
937 	}
938     }
939     return 0;
940 }
941 
942 /* Lst_ForEach wants 1 to stop search */
943 static int
944 prefix_match(void *p, void *q)
945 {
946     const char *prefix = p;
947     const char *path = q;
948     size_t n = strlen(prefix);
949 
950     return (0 == strncmp(path, prefix, n));
951 }
952 
953 /*
954  * looking for exact or prefix/ match to
955  * Lst_Find wants 0 to stop search
956  */
957 static int
958 path_match(const void *p, const void *q)
959 {
960     const char *prefix = q;
961     const char *path = p;
962     size_t n = strlen(prefix);
963     int rc;
964 
965     if ((rc = strncmp(path, prefix, n)) == 0) {
966 	switch (path[n]) {
967 	case '\0':
968 	case '/':
969 	    break;
970 	default:
971 	    rc = 1;
972 	    break;
973 	}
974     }
975     return rc;
976 }
977 
978 /* Lst_Find wants 0 to stop search */
979 static int
980 string_match(const void *p, const void *q)
981 {
982     const char *p1 = p;
983     const char *p2 = q;
984 
985     return strcmp(p1, p2);
986 }
987 
988 
989 static int
990 meta_ignore(GNode *gn, const char *p)
991 {
992     char fname[MAXPATHLEN];
993 
994     if (p == NULL)
995 	return TRUE;
996 
997     if (*p == '/') {
998 	cached_realpath(p, fname); /* clean it up */
999 	if (Lst_ForEach(metaIgnorePaths, prefix_match, fname)) {
1000 #ifdef DEBUG_META_MODE
1001 	    if (DEBUG(META))
1002 		fprintf(debug_file, "meta_oodate: ignoring path: %s\n",
1003 			p);
1004 #endif
1005 	    return TRUE;
1006 	}
1007     }
1008 
1009     if (metaIgnorePatterns) {
1010 	char *pm;
1011 
1012 	Var_Set(".p.", p, gn, 0);
1013 	pm = Var_Subst(NULL,
1014 		       "${" MAKE_META_IGNORE_PATTERNS ":@m@${.p.:M$m}@}",
1015 		       gn, VARF_WANTRES);
1016 	if (*pm) {
1017 #ifdef DEBUG_META_MODE
1018 	    if (DEBUG(META))
1019 		fprintf(debug_file, "meta_oodate: ignoring pattern: %s\n",
1020 			p);
1021 #endif
1022 	    free(pm);
1023 	    return TRUE;
1024 	}
1025 	free(pm);
1026     }
1027 
1028     if (metaIgnoreFilter) {
1029 	char *fm;
1030 
1031 	/* skip if filter result is empty */
1032 	snprintf(fname, sizeof(fname),
1033 		 "${%s:L:${%s:ts:}}",
1034 		 p, MAKE_META_IGNORE_FILTER);
1035 	fm = Var_Subst(NULL, fname, gn, VARF_WANTRES);
1036 	if (*fm == '\0') {
1037 #ifdef DEBUG_META_MODE
1038 	    if (DEBUG(META))
1039 		fprintf(debug_file, "meta_oodate: ignoring filtered: %s\n",
1040 			p);
1041 #endif
1042 	    free(fm);
1043 	    return TRUE;
1044 	}
1045 	free(fm);
1046     }
1047     return FALSE;
1048 }
1049 
1050 /*
1051  * When running with 'meta' functionality, a target can be out-of-date
1052  * if any of the references in its meta data file is more recent.
1053  * We have to track the latestdir on a per-process basis.
1054  */
1055 #define LCWD_VNAME_FMT ".meta.%d.lcwd"
1056 #define LDIR_VNAME_FMT ".meta.%d.ldir"
1057 
1058 /*
1059  * It is possible that a .meta file is corrupted,
1060  * if we detect this we want to reproduce it.
1061  * Setting oodate TRUE will have that effect.
1062  */
1063 #define CHECK_VALID_META(p) if (!(p && *p)) { \
1064     warnx("%s: %d: malformed", fname, lineno); \
1065     oodate = TRUE; \
1066     continue; \
1067     }
1068 
1069 #define DEQUOTE(p) if (*p == '\'') {	\
1070     char *ep; \
1071     p++; \
1072     if ((ep = strchr(p, '\''))) \
1073 	*ep = '\0'; \
1074     }
1075 
1076 Boolean
1077 meta_oodate(GNode *gn, Boolean oodate)
1078 {
1079     static char *tmpdir = NULL;
1080     static char cwd[MAXPATHLEN];
1081     char lcwd_vname[64];
1082     char ldir_vname[64];
1083     char lcwd[MAXPATHLEN];
1084     char latestdir[MAXPATHLEN];
1085     char fname[MAXPATHLEN];
1086     char fname1[MAXPATHLEN];
1087     char fname2[MAXPATHLEN];
1088     char fname3[MAXPATHLEN];
1089     const char *dname;
1090     const char *tname;
1091     char *p;
1092     char *cp;
1093     char *link_src;
1094     char *move_target;
1095     static size_t cwdlen = 0;
1096     static size_t tmplen = 0;
1097     FILE *fp;
1098     Boolean needOODATE = FALSE;
1099     Lst missingFiles;
1100     char *pa[4];			/* >= possible uses */
1101     int i;
1102     int have_filemon = FALSE;
1103 
1104     if (oodate)
1105 	return oodate;		/* we're done */
1106 
1107     i = 0;
1108 
1109     dname = Var_Value(".OBJDIR", gn, &pa[i++]);
1110     tname = Var_Value(TARGET, gn, &pa[i++]);
1111 
1112     /* if this succeeds fname3 is realpath of dname */
1113     if (!meta_needed(gn, dname, tname, fname3, FALSE))
1114 	goto oodate_out;
1115     dname = fname3;
1116 
1117     missingFiles = Lst_Init(FALSE);
1118 
1119     /*
1120      * We need to check if the target is out-of-date. This includes
1121      * checking if the expanded command has changed. This in turn
1122      * requires that all variables are set in the same way that they
1123      * would be if the target needs to be re-built.
1124      */
1125     Make_DoAllVar(gn);
1126 
1127     meta_name(gn, fname, sizeof(fname), dname, tname, dname);
1128 
1129 #ifdef DEBUG_META_MODE
1130     if (DEBUG(META))
1131 	fprintf(debug_file, "meta_oodate: %s\n", fname);
1132 #endif
1133 
1134     if ((fp = fopen(fname, "r")) != NULL) {
1135 	static char *buf = NULL;
1136 	static size_t bufsz;
1137 	int lineno = 0;
1138 	int lastpid = 0;
1139 	int pid;
1140 	int x;
1141 	LstNode ln;
1142 	struct stat fs;
1143 
1144 	if (!buf) {
1145 	    bufsz = 8 * BUFSIZ;
1146 	    buf = bmake_malloc(bufsz);
1147 	}
1148 
1149 	if (!cwdlen) {
1150 	    if (getcwd(cwd, sizeof(cwd)) == NULL)
1151 		err(1, "Could not get current working directory");
1152 	    cwdlen = strlen(cwd);
1153 	}
1154 	strlcpy(lcwd, cwd, sizeof(lcwd));
1155 	strlcpy(latestdir, cwd, sizeof(latestdir));
1156 
1157 	if (!tmpdir) {
1158 	    tmpdir = getTmpdir();
1159 	    tmplen = strlen(tmpdir);
1160 	}
1161 
1162 	/* we want to track all the .meta we read */
1163 	Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
1164 
1165 	ln = Lst_First(gn->commands);
1166 	while (!oodate && (x = fgetLine(&buf, &bufsz, 0, fp)) > 0) {
1167 	    lineno++;
1168 	    if (buf[x - 1] == '\n')
1169 		buf[x - 1] = '\0';
1170 	    else {
1171 		warnx("%s: %d: line truncated at %u", fname, lineno, x);
1172 		oodate = TRUE;
1173 		break;
1174 	    }
1175 	    link_src = NULL;
1176 	    move_target = NULL;
1177 	    /* Find the start of the build monitor section. */
1178 	    if (!have_filemon) {
1179 		if (strncmp(buf, "-- filemon", 10) == 0) {
1180 		    have_filemon = TRUE;
1181 		    continue;
1182 		}
1183 		if (strncmp(buf, "# buildmon", 10) == 0) {
1184 		    have_filemon = TRUE;
1185 		    continue;
1186 		}
1187 	    }
1188 
1189 	    /* Delimit the record type. */
1190 	    p = buf;
1191 #ifdef DEBUG_META_MODE
1192 	    if (DEBUG(META))
1193 		fprintf(debug_file, "%s: %d: %s\n", fname, lineno, buf);
1194 #endif
1195 	    strsep(&p, " ");
1196 	    if (have_filemon) {
1197 		/*
1198 		 * We are in the 'filemon' output section.
1199 		 * Each record from filemon follows the general form:
1200 		 *
1201 		 * <key> <pid> <data>
1202 		 *
1203 		 * Where:
1204 		 * <key> is a single letter, denoting the syscall.
1205 		 * <pid> is the process that made the syscall.
1206 		 * <data> is the arguments (of interest).
1207 		 */
1208 		switch(buf[0]) {
1209 		case '#':		/* comment */
1210 		case 'V':		/* version */
1211 		    break;
1212 		default:
1213 		    /*
1214 		     * We need to track pathnames per-process.
1215 		     *
1216 		     * Each process run by make, starts off in the 'CWD'
1217 		     * recorded in the .meta file, if it chdirs ('C')
1218 		     * elsewhere we need to track that - but only for
1219 		     * that process.  If it forks ('F'), we initialize
1220 		     * the child to have the same cwd as its parent.
1221 		     *
1222 		     * We also need to track the 'latestdir' of
1223 		     * interest.  This is usually the same as cwd, but
1224 		     * not if a process is reading directories.
1225 		     *
1226 		     * Each time we spot a different process ('pid')
1227 		     * we save the current value of 'latestdir' in a
1228 		     * variable qualified by 'lastpid', and
1229 		     * re-initialize 'latestdir' to any pre-saved
1230 		     * value for the current 'pid' and 'CWD' if none.
1231 		     */
1232 		    CHECK_VALID_META(p);
1233 		    pid = atoi(p);
1234 		    if (pid > 0 && pid != lastpid) {
1235 			char *ldir;
1236 			char *tp;
1237 
1238 			if (lastpid > 0) {
1239 			    /* We need to remember these. */
1240 			    Var_Set(lcwd_vname, lcwd, VAR_GLOBAL, 0);
1241 			    Var_Set(ldir_vname, latestdir, VAR_GLOBAL, 0);
1242 			}
1243 			snprintf(lcwd_vname, sizeof(lcwd_vname), LCWD_VNAME_FMT, pid);
1244 			snprintf(ldir_vname, sizeof(ldir_vname), LDIR_VNAME_FMT, pid);
1245 			lastpid = pid;
1246 			ldir = Var_Value(ldir_vname, VAR_GLOBAL, &tp);
1247 			if (ldir) {
1248 			    strlcpy(latestdir, ldir, sizeof(latestdir));
1249 			    free(tp);
1250 			}
1251 			ldir = Var_Value(lcwd_vname, VAR_GLOBAL, &tp);
1252 			if (ldir) {
1253 			    strlcpy(lcwd, ldir, sizeof(lcwd));
1254 			    free(tp);
1255 			}
1256 		    }
1257 		    /* Skip past the pid. */
1258 		    if (strsep(&p, " ") == NULL)
1259 			continue;
1260 #ifdef DEBUG_META_MODE
1261 		    if (DEBUG(META))
1262 			    fprintf(debug_file, "%s: %d: %d: %c: cwd=%s lcwd=%s ldir=%s\n",
1263 				    fname, lineno,
1264 				    pid, buf[0], cwd, lcwd, latestdir);
1265 #endif
1266 		    break;
1267 		}
1268 
1269 		CHECK_VALID_META(p);
1270 
1271 		/* Process according to record type. */
1272 		switch (buf[0]) {
1273 		case 'X':		/* eXit */
1274 		    Var_Delete(lcwd_vname, VAR_GLOBAL);
1275 		    Var_Delete(ldir_vname, VAR_GLOBAL);
1276 		    lastpid = 0;	/* no need to save ldir_vname */
1277 		    break;
1278 
1279 		case 'F':		/* [v]Fork */
1280 		    {
1281 			char cldir[64];
1282 			int child;
1283 
1284 			child = atoi(p);
1285 			if (child > 0) {
1286 			    snprintf(cldir, sizeof(cldir), LCWD_VNAME_FMT, child);
1287 			    Var_Set(cldir, lcwd, VAR_GLOBAL, 0);
1288 			    snprintf(cldir, sizeof(cldir), LDIR_VNAME_FMT, child);
1289 			    Var_Set(cldir, latestdir, VAR_GLOBAL, 0);
1290 #ifdef DEBUG_META_MODE
1291 			    if (DEBUG(META))
1292 				    fprintf(debug_file, "%s: %d: %d: cwd=%s lcwd=%s ldir=%s\n",
1293 					    fname, lineno,
1294 					    child, cwd, lcwd, latestdir);
1295 #endif
1296 			}
1297 		    }
1298 		    break;
1299 
1300 		case 'C':		/* Chdir */
1301 		    /* Update lcwd and latest directory. */
1302 		    strlcpy(latestdir, p, sizeof(latestdir));
1303 		    strlcpy(lcwd, p, sizeof(lcwd));
1304 		    Var_Set(lcwd_vname, lcwd, VAR_GLOBAL, 0);
1305 		    Var_Set(ldir_vname, lcwd, VAR_GLOBAL, 0);
1306 #ifdef DEBUG_META_MODE
1307 		    if (DEBUG(META))
1308 			fprintf(debug_file, "%s: %d: cwd=%s ldir=%s\n", fname, lineno, cwd, lcwd);
1309 #endif
1310 		    break;
1311 
1312 		case 'M':		/* renaMe */
1313 		    /*
1314 		     * For 'M'oves we want to check
1315 		     * the src as for 'R'ead
1316 		     * and the target as for 'W'rite.
1317 		     */
1318 		    cp = p;		/* save this for a second */
1319 		    /* now get target */
1320 		    if (strsep(&p, " ") == NULL)
1321 			continue;
1322 		    CHECK_VALID_META(p);
1323 		    move_target = p;
1324 		    p = cp;
1325 		    /* 'L' and 'M' put single quotes around the args */
1326 		    DEQUOTE(p);
1327 		    DEQUOTE(move_target);
1328 		    /* FALLTHROUGH */
1329 		case 'D':		/* unlink */
1330 		    if (*p == '/' && !Lst_IsEmpty(missingFiles)) {
1331 			/* remove any missingFiles entries that match p */
1332 			if ((ln = Lst_Find(missingFiles, p,
1333 					   path_match)) != NULL) {
1334 			    LstNode nln;
1335 			    char *tp;
1336 
1337 			    do {
1338 				nln = Lst_FindFrom(missingFiles, Lst_Succ(ln),
1339 						   p, path_match);
1340 				tp = Lst_Datum(ln);
1341 				Lst_Remove(missingFiles, ln);
1342 				free(tp);
1343 			    } while ((ln = nln) != NULL);
1344 			}
1345 		    }
1346 		    if (buf[0] == 'M') {
1347 			/* the target of the mv is a file 'W'ritten */
1348 #ifdef DEBUG_META_MODE
1349 			if (DEBUG(META))
1350 			    fprintf(debug_file, "meta_oodate: M %s -> %s\n",
1351 				    p, move_target);
1352 #endif
1353 			p = move_target;
1354 			goto check_write;
1355 		    }
1356 		    break;
1357 		case 'L':		/* Link */
1358 		    /*
1359 		     * For 'L'inks check
1360 		     * the src as for 'R'ead
1361 		     * and the target as for 'W'rite.
1362 		     */
1363 		    link_src = p;
1364 		    /* now get target */
1365 		    if (strsep(&p, " ") == NULL)
1366 			continue;
1367 		    CHECK_VALID_META(p);
1368 		    /* 'L' and 'M' put single quotes around the args */
1369 		    DEQUOTE(p);
1370 		    DEQUOTE(link_src);
1371 #ifdef DEBUG_META_MODE
1372 		    if (DEBUG(META))
1373 			fprintf(debug_file, "meta_oodate: L %s -> %s\n",
1374 				link_src, p);
1375 #endif
1376 		    /* FALLTHROUGH */
1377 		case 'W':		/* Write */
1378 		check_write:
1379 		    /*
1380 		     * If a file we generated within our bailiwick
1381 		     * but outside of .OBJDIR is missing,
1382 		     * we need to do it again.
1383 		     */
1384 		    /* ignore non-absolute paths */
1385 		    if (*p != '/')
1386 			break;
1387 
1388 		    if (Lst_IsEmpty(metaBailiwick))
1389 			break;
1390 
1391 		    /* ignore cwd - normal dependencies handle those */
1392 		    if (strncmp(p, cwd, cwdlen) == 0)
1393 			break;
1394 
1395 		    if (!Lst_ForEach(metaBailiwick, prefix_match, p))
1396 			break;
1397 
1398 		    /* tmpdir might be within */
1399 		    if (tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0)
1400 			break;
1401 
1402 		    /* ignore anything containing the string "tmp" */
1403 		    if ((strstr("tmp", p)))
1404 			break;
1405 
1406 		    if ((link_src != NULL && cached_lstat(p, &fs) < 0) ||
1407 			(link_src == NULL && cached_stat(p, &fs) < 0)) {
1408 			if (!meta_ignore(gn, p)) {
1409 			    if (Lst_Find(missingFiles, p, string_match) == NULL)
1410 				Lst_AtEnd(missingFiles, bmake_strdup(p));
1411 			}
1412 		    }
1413 		    break;
1414 		check_link_src:
1415 		    p = link_src;
1416 		    link_src = NULL;
1417 #ifdef DEBUG_META_MODE
1418 		    if (DEBUG(META))
1419 			fprintf(debug_file, "meta_oodate: L src %s\n", p);
1420 #endif
1421 		    /* FALLTHROUGH */
1422 		case 'R':		/* Read */
1423 		case 'E':		/* Exec */
1424 		    /*
1425 		     * Check for runtime files that can't
1426 		     * be part of the dependencies because
1427 		     * they are _expected_ to change.
1428 		     */
1429 		    if (meta_ignore(gn, p))
1430 			break;
1431 
1432 		    /*
1433 		     * The rest of the record is the file name.
1434 		     * Check if it's not an absolute path.
1435 		     */
1436 		    {
1437 			char *sdirs[4];
1438 			char **sdp;
1439 			int sdx = 0;
1440 			int found = 0;
1441 
1442 			if (*p == '/') {
1443 			    sdirs[sdx++] = p; /* done */
1444 			} else {
1445 			    if (strcmp(".", p) == 0)
1446 				continue;  /* no point */
1447 
1448 			    /* Check vs latestdir */
1449 			    snprintf(fname1, sizeof(fname1), "%s/%s", latestdir, p);
1450 			    sdirs[sdx++] = fname1;
1451 
1452 			    if (strcmp(latestdir, lcwd) != 0) {
1453 				/* Check vs lcwd */
1454 				snprintf(fname2, sizeof(fname2), "%s/%s", lcwd, p);
1455 				sdirs[sdx++] = fname2;
1456 			    }
1457 			    if (strcmp(lcwd, cwd) != 0) {
1458 				/* Check vs cwd */
1459 				snprintf(fname3, sizeof(fname3), "%s/%s", cwd, p);
1460 				sdirs[sdx++] = fname3;
1461 			    }
1462 			}
1463 			sdirs[sdx++] = NULL;
1464 
1465 			for (sdp = sdirs; *sdp && !found; sdp++) {
1466 #ifdef DEBUG_META_MODE
1467 			    if (DEBUG(META))
1468 				fprintf(debug_file, "%s: %d: looking for: %s\n", fname, lineno, *sdp);
1469 #endif
1470 			    if (cached_stat(*sdp, &fs) == 0) {
1471 				found = 1;
1472 				p = *sdp;
1473 			    }
1474 			}
1475 			if (found) {
1476 #ifdef DEBUG_META_MODE
1477 			    if (DEBUG(META))
1478 				fprintf(debug_file, "%s: %d: found: %s\n", fname, lineno, p);
1479 #endif
1480 			    if (!S_ISDIR(fs.st_mode) &&
1481 				fs.st_mtime > gn->mtime) {
1482 				if (DEBUG(META))
1483 				    fprintf(debug_file, "%s: %d: file '%s' is newer than the target...\n", fname, lineno, p);
1484 				oodate = TRUE;
1485 			    } else if (S_ISDIR(fs.st_mode)) {
1486 				/* Update the latest directory. */
1487 				cached_realpath(p, latestdir);
1488 			    }
1489 			} else if (errno == ENOENT && *p == '/' &&
1490 				   strncmp(p, cwd, cwdlen) != 0) {
1491 			    /*
1492 			     * A referenced file outside of CWD is missing.
1493 			     * We cannot catch every eventuality here...
1494 			     */
1495 			    if (Lst_Find(missingFiles, p, string_match) == NULL)
1496 				    Lst_AtEnd(missingFiles, bmake_strdup(p));
1497 			}
1498 		    }
1499 		    if (buf[0] == 'E') {
1500 			/* previous latestdir is no longer relevant */
1501 			strlcpy(latestdir, lcwd, sizeof(latestdir));
1502 		    }
1503 		    break;
1504 		default:
1505 		    break;
1506 		}
1507 		if (!oodate && buf[0] == 'L' && link_src != NULL)
1508 		    goto check_link_src;
1509 	    } else if (strcmp(buf, "CMD") == 0) {
1510 		/*
1511 		 * Compare the current command with the one in the
1512 		 * meta data file.
1513 		 */
1514 		if (ln == NULL) {
1515 		    if (DEBUG(META))
1516 			fprintf(debug_file, "%s: %d: there were more build commands in the meta data file than there are now...\n", fname, lineno);
1517 		    oodate = TRUE;
1518 		} else {
1519 		    char *cmd = (char *)Lst_Datum(ln);
1520 		    Boolean hasOODATE = FALSE;
1521 
1522 		    if (strstr(cmd, "$?"))
1523 			hasOODATE = TRUE;
1524 		    else if ((cp = strstr(cmd, ".OODATE"))) {
1525 			/* check for $[{(].OODATE[:)}] */
1526 			if (cp > cmd + 2 && cp[-2] == '$')
1527 			    hasOODATE = TRUE;
1528 		    }
1529 		    if (hasOODATE) {
1530 			needOODATE = TRUE;
1531 			if (DEBUG(META))
1532 			    fprintf(debug_file, "%s: %d: cannot compare command using .OODATE\n", fname, lineno);
1533 		    }
1534 		    cmd = Var_Subst(NULL, cmd, gn, VARF_WANTRES|VARF_UNDEFERR);
1535 
1536 		    if ((cp = strchr(cmd, '\n'))) {
1537 			int n;
1538 
1539 			/*
1540 			 * This command contains newlines, we need to
1541 			 * fetch more from the .meta file before we
1542 			 * attempt a comparison.
1543 			 */
1544 			/* first put the newline back at buf[x - 1] */
1545 			buf[x - 1] = '\n';
1546 			do {
1547 			    /* now fetch the next line */
1548 			    if ((n = fgetLine(&buf, &bufsz, x, fp)) <= 0)
1549 				break;
1550 			    x = n;
1551 			    lineno++;
1552 			    if (buf[x - 1] != '\n') {
1553 				warnx("%s: %d: line truncated at %u", fname, lineno, x);
1554 				break;
1555 			    }
1556 			    cp = strchr(++cp, '\n');
1557 			} while (cp);
1558 			if (buf[x - 1] == '\n')
1559 			    buf[x - 1] = '\0';
1560 		    }
1561 		    if (p &&
1562 			!hasOODATE &&
1563 			!(gn->type & OP_NOMETA_CMP) &&
1564 			strcmp(p, cmd) != 0) {
1565 			if (DEBUG(META))
1566 			    fprintf(debug_file, "%s: %d: a build command has changed\n%s\nvs\n%s\n", fname, lineno, p, cmd);
1567 			if (!metaIgnoreCMDs)
1568 			    oodate = TRUE;
1569 		    }
1570 		    free(cmd);
1571 		    ln = Lst_Succ(ln);
1572 		}
1573 	    } else if (strcmp(buf, "CWD") == 0) {
1574 		/*
1575 		 * Check if there are extra commands now
1576 		 * that weren't in the meta data file.
1577 		 */
1578 		if (!oodate && ln != NULL) {
1579 		    if (DEBUG(META))
1580 			fprintf(debug_file, "%s: %d: there are extra build commands now that weren't in the meta data file\n", fname, lineno);
1581 		    oodate = TRUE;
1582 		}
1583 		CHECK_VALID_META(p);
1584 		if (strcmp(p, cwd) != 0) {
1585 		    if (DEBUG(META))
1586 			fprintf(debug_file, "%s: %d: the current working directory has changed from '%s' to '%s'\n", fname, lineno, p, curdir);
1587 		    oodate = TRUE;
1588 		}
1589 	    }
1590 	}
1591 
1592 	fclose(fp);
1593 	if (!Lst_IsEmpty(missingFiles)) {
1594 	    if (DEBUG(META))
1595 		fprintf(debug_file, "%s: missing files: %s...\n",
1596 			fname, (char *)Lst_Datum(Lst_First(missingFiles)));
1597 	    oodate = TRUE;
1598 	}
1599 	if (!oodate && !have_filemon && filemonMissing) {
1600 	    if (DEBUG(META))
1601 		fprintf(debug_file, "%s: missing filemon data\n", fname);
1602 	    oodate = TRUE;
1603 	}
1604     } else {
1605 	if (writeMeta && metaMissing) {
1606 	    cp = NULL;
1607 
1608 	    /* if target is in .CURDIR we do not need a meta file */
1609 	    if (gn->path && (cp = strrchr(gn->path, '/')) && cp > gn->path) {
1610 		if (strncmp(curdir, gn->path, (cp - gn->path)) != 0) {
1611 		    cp = NULL;		/* not in .CURDIR */
1612 		}
1613 	    }
1614 	    if (!cp) {
1615 		if (DEBUG(META))
1616 		    fprintf(debug_file, "%s: required but missing\n", fname);
1617 		oodate = TRUE;
1618 		needOODATE = TRUE;	/* assume the worst */
1619 	    }
1620 	}
1621     }
1622 
1623     Lst_Destroy(missingFiles, (FreeProc *)free);
1624 
1625     if (oodate && needOODATE) {
1626 	/*
1627 	 * Target uses .OODATE which is empty; or we wouldn't be here.
1628 	 * We have decided it is oodate, so .OODATE needs to be set.
1629 	 * All we can sanely do is set it to .ALLSRC.
1630 	 */
1631 	Var_Delete(OODATE, gn);
1632 	Var_Set(OODATE, Var_Value(ALLSRC, gn, &cp), gn, 0);
1633 	free(cp);
1634     }
1635 
1636  oodate_out:
1637     for (i--; i >= 0; i--) {
1638 	free(pa[i]);
1639     }
1640     return oodate;
1641 }
1642 
1643 /* support for compat mode */
1644 
1645 static int childPipe[2];
1646 
1647 void
1648 meta_compat_start(void)
1649 {
1650 #ifdef USE_FILEMON_ONCE
1651     /*
1652      * We need to re-open filemon for each cmd.
1653      */
1654     BuildMon *pbm = &Mybm;
1655 
1656     if (pbm->mfp != NULL && useFilemon) {
1657 	meta_open_filemon(pbm);
1658     } else {
1659 	pbm->mon_fd = -1;
1660 	pbm->filemon = NULL;
1661     }
1662 #endif
1663     if (pipe(childPipe) < 0)
1664 	Punt("Cannot create pipe: %s", strerror(errno));
1665     /* Set close-on-exec flag for both */
1666     (void)fcntl(childPipe[0], F_SETFD, FD_CLOEXEC);
1667     (void)fcntl(childPipe[1], F_SETFD, FD_CLOEXEC);
1668 }
1669 
1670 void
1671 meta_compat_child(void)
1672 {
1673     meta_job_child(NULL);
1674     if (dup2(childPipe[1], 1) < 0 ||
1675 	dup2(1, 2) < 0) {
1676 	execError("dup2", "pipe");
1677 	_exit(1);
1678     }
1679 }
1680 
1681 void
1682 meta_compat_parent(pid_t child)
1683 {
1684     int outfd, metafd, maxfd, nfds;
1685     char buf[BUFSIZ+1];
1686     fd_set readfds;
1687 
1688     meta_job_parent(NULL, child);
1689     close(childPipe[1]);			/* child side */
1690     outfd = childPipe[0];
1691 #ifdef USE_FILEMON
1692     metafd = Mybm.filemon ? filemon_readfd(Mybm.filemon) : -1;
1693 #else
1694     metafd = -1;
1695 #endif
1696     maxfd = -1;
1697     if (outfd > maxfd)
1698 	    maxfd = outfd;
1699     if (metafd > maxfd)
1700 	    maxfd = metafd;
1701 
1702     while (outfd != -1 || metafd != -1) {
1703 	FD_ZERO(&readfds);
1704 	if (outfd != -1) {
1705 	    FD_SET(outfd, &readfds);
1706 	}
1707 	if (metafd != -1) {
1708 	    FD_SET(metafd, &readfds);
1709 	}
1710 	nfds = select(maxfd + 1, &readfds, NULL, NULL, NULL);
1711 	if (nfds == -1) {
1712 	    if (errno == EINTR)
1713 		continue;
1714 	    err(1, "select");
1715 	}
1716 
1717 	if (outfd != -1 && FD_ISSET(outfd, &readfds)) do {
1718 	    /* XXX this is not line-buffered */
1719 	    ssize_t nread = read(outfd, buf, sizeof(buf) - 1);
1720 	    if (nread == -1)
1721 		err(1, "read");
1722 	    if (nread == 0) {
1723 		close(outfd);
1724 		outfd = -1;
1725 		break;
1726 	    }
1727 	    fwrite(buf, 1, (size_t)nread, stdout);
1728 	    fflush(stdout);
1729 	    buf[nread] = '\0';
1730 	    meta_job_output(NULL, buf, "");
1731 	} while (0);
1732 	if (metafd != -1 && FD_ISSET(metafd, &readfds)) {
1733 	    if (meta_job_event(NULL) <= 0)
1734 		metafd = -1;
1735 	}
1736     }
1737 }
1738 
1739 #endif	/* USE_META */
1740