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