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