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