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