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