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