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