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