Home | History | Annotate | Line # | Download | only in make
meta.c revision 1.21
      1 /*      $NetBSD: meta.c,v 1.21 2011/08/18 00:00:21 sjg Exp $ */
      2 
      3 /*
      4  * Implement 'meta' mode.
      5  * Adapted from John Birrell's patches to FreeBSD make.
      6  * --sjg
      7  */
      8 /*
      9  * Copyright (c) 2009-2010, 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 <sys/ioctl.h>
     40 #include <fcntl.h>
     41 #include <libgen.h>
     42 #include <errno.h>
     43 #if !defined(HAVE_CONFIG_H) || defined(HAVE_ERR_H)
     44 #include <err.h>
     45 #endif
     46 
     47 #include "make.h"
     48 #include "job.h"
     49 
     50 #ifdef HAVE_FILEMON_H
     51 # include <filemon.h>
     52 #endif
     53 #if !defined(USE_FILEMON) && defined(FILEMON_SET_FD)
     54 # define USE_FILEMON
     55 #endif
     56 
     57 static BuildMon Mybm;			/* for compat */
     58 static Lst metaBailiwick;			/* our scope of control */
     59 
     60 Boolean useMeta = FALSE;
     61 static Boolean useFilemon = FALSE;
     62 static Boolean writeMeta = FALSE;
     63 static Boolean metaEnv = FALSE;		/* don't save env unless asked */
     64 static Boolean metaVerbose = FALSE;
     65 static Boolean metaIgnoreCMDs = FALSE;	/* ignore CMDs in .meta files */
     66 static Boolean metaCurdirOk = FALSE;	/* write .meta in .CURDIR Ok? */
     67 
     68 extern Boolean forceJobs;
     69 extern Boolean comatMake;
     70 
     71 #define	MAKE_META_PREFIX	".MAKE.META.PREFIX"
     72 
     73 #ifndef N2U
     74 # define N2U(n, u)   (((n) + ((u) - 1)) / (u))
     75 #endif
     76 #ifndef ROUNDUP
     77 # define ROUNDUP(n, u)   (N2U((n), (u)) * (u))
     78 #endif
     79 
     80 #if !defined(HAVE_STRSEP)
     81 # define strsep(s, d) stresep((s), (d), 0)
     82 #endif
     83 
     84 /*
     85  * Filemon is a kernel module which snoops certain syscalls.
     86  *
     87  * C chdir
     88  * E exec
     89  * F [v]fork
     90  * L [sym]link
     91  * M rename
     92  * R read
     93  * W write
     94  * S stat
     95  *
     96  * See meta_oodate below - we mainly care about 'E' and 'R'.
     97  *
     98  * We can still use meta mode without filemon, but
     99  * the benefits are more limited.
    100  */
    101 #ifdef USE_FILEMON
    102 # ifndef _PATH_FILEMON
    103 #   define _PATH_FILEMON "/dev/filemon"
    104 # endif
    105 
    106 /*
    107  * Open the filemon device.
    108  */
    109 static void
    110 filemon_open(BuildMon *pbm)
    111 {
    112     int retry;
    113 
    114     pbm->mon_fd = pbm->filemon_fd = -1;
    115     if (!useFilemon)
    116 	return;
    117 
    118     for (retry = 5; retry >= 0; retry--) {
    119 	if ((pbm->filemon_fd = open(_PATH_FILEMON, O_RDWR)) >= 0)
    120 	    break;
    121     }
    122 
    123     if (pbm->filemon_fd < 0) {
    124 	useFilemon = FALSE;
    125 	warn("Could not open %s", _PATH_FILEMON);
    126 	return;
    127     }
    128 
    129     /*
    130      * We use a file outside of '.'
    131      * to avoid a FreeBSD kernel bug where unlink invalidates
    132      * cwd causing getcwd to do a lot more work.
    133      * We only care about the descriptor.
    134      */
    135     pbm->mon_fd = mkTempFile("filemon.XXXXXX", NULL);
    136     if (ioctl(pbm->filemon_fd, FILEMON_SET_FD, &pbm->mon_fd) < 0) {
    137 	err(1, "Could not set filemon file descriptor!");
    138     }
    139     /* we don't need these once we exec */
    140     (void)fcntl(pbm->mon_fd, F_SETFD, 1);
    141     (void)fcntl(pbm->filemon_fd, F_SETFD, 1);
    142 }
    143 
    144 /*
    145  * Read the build monitor output file and write records to the target's
    146  * metadata file.
    147  */
    148 static void
    149 filemon_read(FILE *mfp, int fd)
    150 {
    151     FILE *fp;
    152     char buf[BUFSIZ];
    153 
    154     /* Check if we're not writing to a meta data file.*/
    155     if (mfp == NULL) {
    156 	if (fd >= 0)
    157 	    close(fd);			/* not interested */
    158 	return;
    159     }
    160     /* rewind */
    161     lseek(fd, SEEK_SET, 0);
    162     if ((fp = fdopen(fd, "r")) == NULL)
    163 	err(1, "Could not read build monitor file '%d'", fd);
    164 
    165     fprintf(mfp, "-- filemon acquired metadata --\n");
    166 
    167     while (fgets(buf, sizeof(buf), fp)) {
    168 	fprintf(mfp, "%s", buf);
    169     }
    170     fflush(mfp);
    171     clearerr(fp);
    172     fclose(fp);
    173 }
    174 #endif
    175 
    176 /*
    177  * when realpath() fails,
    178  * we use this, to clean up ./ and ../
    179  */
    180 static void
    181 eat_dots(char *buf, size_t bufsz, int dots)
    182 {
    183     char *cp;
    184     char *cp2;
    185     const char *eat;
    186     size_t eatlen;
    187 
    188     switch (dots) {
    189     case 1:
    190 	eat = "/./";
    191 	eatlen = 2;
    192 	break;
    193     case 2:
    194 	eat = "/../";
    195 	eatlen = 3;
    196 	break;
    197     default:
    198 	return;
    199     }
    200 
    201     do {
    202 	cp = strstr(buf, eat);
    203 	if (cp) {
    204 	    cp2 = cp + eatlen;
    205 	    if (dots == 2 && cp > buf) {
    206 		do {
    207 		    cp--;
    208 		} while (cp > buf && *cp != '/');
    209 	    }
    210 	    if (*cp == '/') {
    211 		strlcpy(cp, cp2, bufsz - (cp - buf));
    212 	    } else {
    213 		return;			/* can't happen? */
    214 	    }
    215 	}
    216     } while (cp);
    217 }
    218 
    219 static char *
    220 meta_name(struct GNode *gn, char *mname, size_t mnamelen,
    221 	  const char *dname,
    222 	  const char *tname)
    223 {
    224     char buf[MAXPATHLEN];
    225     char cwd[MAXPATHLEN];
    226     char *rp;
    227     char *cp;
    228     char *tp;
    229     char *p[4];				/* >= number of possible uses */
    230     int i;
    231 
    232     i = 0;
    233     if (!dname)
    234 	dname = Var_Value(".OBJDIR", gn, &p[i++]);
    235     if (!tname)
    236 	tname = Var_Value(TARGET, gn, &p[i++]);
    237 
    238     if (realpath(dname, cwd))
    239 	dname = cwd;
    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 ((cp = strrchr(tname, '/'))) {
    249 	if (realpath(tname, buf)) {
    250 	    if ((rp = strrchr(buf, '/'))) {
    251 		rp++;
    252 		cp++;
    253 		if (strcmp(cp, rp) != 0)
    254 		    strlcpy(rp, cp, sizeof(buf) - (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, sizeof(buf), 1);	/* ./ */
    270 	    eat_dots(buf, sizeof(buf), 2);	/* ../ */
    271 	    tname = buf;
    272 	}
    273     }
    274     /* on some systems dirname may modify its arg */
    275     tp = bmake_strdup(tname);
    276     if (strcmp(dname, dirname(tp)) == 0)
    277 	snprintf(mname, mnamelen, "%s.meta", tname);
    278     else {
    279 	snprintf(mname, mnamelen, "%s/%s.meta", dname, tname);
    280 
    281 	/*
    282 	 * Replace path separators in the file name after the
    283 	 * current object directory path.
    284 	 */
    285 	cp = mname + strlen(dname) + 1;
    286 
    287 	while (*cp != '\0') {
    288 	    if (*cp == '/')
    289 		*cp = '_';
    290 	    cp++;
    291 	}
    292     }
    293     free(tp);
    294     for (i--; i >= 0; i--) {
    295 	if (p[i])
    296 	    free(p[i]);
    297     }
    298     return (mname);
    299 }
    300 
    301 /*
    302  * Return true if running ${.MAKE}
    303  * Bypassed if target is flagged .MAKE
    304  */
    305 static int
    306 is_submake(void *cmdp, void *gnp)
    307 {
    308     static char *p_make = NULL;
    309     static int p_len;
    310     char  *cmd = cmdp;
    311     GNode *gn = gnp;
    312     char *mp = NULL;
    313     char *cp;
    314     char *cp2;
    315     int rc = 0;				/* keep looking */
    316 
    317     if (!p_make) {
    318 	p_make = Var_Value(".MAKE", gn, &cp);
    319 	p_len = strlen(p_make);
    320     }
    321     cp = strchr(cmd, '$');
    322     if ((cp)) {
    323 	mp = Var_Subst(NULL, cmd, gn, FALSE);
    324 	cmd = mp;
    325     }
    326     cp2 = strstr(cmd, p_make);
    327     if ((cp2)) {
    328 	switch (cp2[p_len]) {
    329 	case '\0':
    330 	case ' ':
    331 	case '\t':
    332 	case '\n':
    333 	    rc = 1;
    334 	    break;
    335 	}
    336 	if (cp2 > cmd && rc > 0) {
    337 	    switch (cp2[-1]) {
    338 	    case ' ':
    339 	    case '\t':
    340 	    case '\n':
    341 		break;
    342 	    default:
    343 		rc = 0;			/* no match */
    344 		break;
    345 	    }
    346 	}
    347     }
    348     if (mp)
    349 	free(mp);
    350     return (rc);
    351 }
    352 
    353 typedef struct meta_file_s {
    354     FILE *fp;
    355     GNode *gn;
    356 } meta_file_t;
    357 
    358 static int
    359 printCMD(void *cmdp, void *mfpp)
    360 {
    361     meta_file_t *mfp = mfpp;
    362     char *cmd = cmdp;
    363     char *cp = NULL;
    364 
    365     if (strchr(cmd, '$')) {
    366 	cmd = cp = Var_Subst(NULL, cmd, mfp->gn, FALSE);
    367     }
    368     fprintf(mfp->fp, "CMD %s\n", cmd);
    369     if (cp)
    370 	free(cp);
    371     return 0;
    372 }
    373 
    374 /*
    375  * Certain node types never get a .meta file
    376  */
    377 #define SKIP_META_TYPE(_type) do { \
    378     if ((gn->type & __CONCAT(OP_, _type))) {	\
    379 	if (DEBUG(META)) { \
    380 	    fprintf(debug_file, "Skipping meta for %s: .%s\n", \
    381 		    gn->name, __STRING(_type));		       \
    382 	} \
    383 	return (NULL); \
    384     } \
    385 } while (0)
    386 
    387 static FILE *
    388 meta_create(BuildMon *pbm, GNode *gn)
    389 {
    390     extern char **environ;
    391     meta_file_t mf;
    392     char buf[MAXPATHLEN];
    393     char objdir[MAXPATHLEN];
    394     char **ptr;
    395     const char *dname;
    396     const char *tname;
    397     char *fname;
    398     const char *cp;
    399     char *p[4];				/* >= possible uses */
    400     int i;
    401     struct stat fs;
    402 
    403 
    404     /* This may be a phony node which we don't want meta data for... */
    405     /* Skip .meta for .BEGIN, .END, .ERROR etc as well. */
    406     /* Or it may be explicitly flagged as .NOMETA */
    407     SKIP_META_TYPE(NOMETA);
    408     /* Unless it is explicitly flagged as .META */
    409     if (!(gn->type & OP_META)) {
    410 	SKIP_META_TYPE(PHONY);
    411 	SKIP_META_TYPE(SPECIAL);
    412 	SKIP_META_TYPE(MAKE);
    413     }
    414 
    415     mf.fp = NULL;
    416 
    417     i = 0;
    418 
    419     dname = Var_Value(".OBJDIR", gn, &p[i++]);
    420     tname = Var_Value(TARGET, gn, &p[i++]);
    421 
    422     /* The object directory may not exist. Check it.. */
    423     if (stat(dname, &fs) != 0) {
    424 	if (DEBUG(META))
    425 	    fprintf(debug_file, "Skipping meta for %s: no .OBJDIR\n",
    426 		    gn->name);
    427 	goto out;
    428     }
    429     /* Check if there are no commands to execute. */
    430     if (Lst_IsEmpty(gn->commands)) {
    431 	if (DEBUG(META))
    432 	    fprintf(debug_file, "Skipping meta for %s: no commands\n",
    433 		    gn->name);
    434 	goto out;
    435     }
    436 
    437     /* make sure these are canonical */
    438     if (realpath(dname, objdir))
    439 	dname = objdir;
    440 
    441     /* If we aren't in the object directory, don't create a meta file. */
    442     if (!metaCurdirOk && strcmp(curdir, dname) == 0) {
    443 	if (DEBUG(META))
    444 	    fprintf(debug_file, "Skipping meta for %s: .OBJDIR == .CURDIR\n",
    445 		    gn->name);
    446 	goto out;
    447     }
    448     if (!(gn->type & OP_META)) {
    449 	/* We do not generate .meta files for sub-makes */
    450 	if (Lst_ForEach(gn->commands, is_submake, gn)) {
    451 	    if (DEBUG(META))
    452 		fprintf(debug_file, "Skipping meta for %s: .MAKE\n",
    453 			gn->name);
    454 	    goto out;
    455 	}
    456     }
    457 
    458     if (metaVerbose) {
    459 	char *mp;
    460 
    461 	/* Describe the target we are building */
    462 	mp = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", gn, 0);
    463 	if (*mp)
    464 	    fprintf(stdout, "%s\n", mp);
    465 	free(mp);
    466     }
    467     /* Get the basename of the target */
    468     if ((cp = strrchr(tname, '/')) == NULL) {
    469 	cp = tname;
    470     } else {
    471 	cp++;
    472     }
    473 
    474     fflush(stdout);
    475 
    476     if (strcmp(cp, makeDependfile) == 0)
    477 	goto out;
    478 
    479     if (!writeMeta)
    480 	/* Don't create meta data. */
    481 	goto out;
    482 
    483     fname = meta_name(gn, pbm->meta_fname, sizeof(pbm->meta_fname),
    484 		      dname, tname);
    485 
    486 #ifdef DEBUG_META_MODE
    487     if (DEBUG(META))
    488 	fprintf(debug_file, "meta_create: %s\n", fname);
    489 #endif
    490 
    491     if ((mf.fp = fopen(fname, "w")) == NULL)
    492 	err(1, "Could not open meta file '%s'", fname);
    493 
    494     fprintf(mf.fp, "# Meta data file %s\n", fname);
    495 
    496     mf.gn = gn;
    497 
    498     Lst_ForEach(gn->commands, printCMD, &mf);
    499 
    500     fprintf(mf.fp, "CWD %s\n", getcwd(buf, sizeof(buf)));
    501     fprintf(mf.fp, "TARGET %s\n", tname);
    502 
    503     if (metaEnv) {
    504 	for (ptr = environ; *ptr != NULL; ptr++)
    505 	    fprintf(mf.fp, "ENV %s\n", *ptr);
    506     }
    507 
    508     fprintf(mf.fp, "-- command output --\n");
    509     fflush(mf.fp);
    510 
    511     Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
    512     Var_Append(".MAKE.META.CREATED", fname, VAR_GLOBAL);
    513 
    514  out:
    515     for (i--; i >= 0; i--) {
    516 	if (p[i])
    517 	    free(p[i]);
    518     }
    519 
    520     return (mf.fp);
    521 }
    522 
    523 static Boolean
    524 boolValue(char *s)
    525 {
    526     switch(*s) {
    527     case '0':
    528     case 'N':
    529     case 'n':
    530     case 'F':
    531     case 'f':
    532 	return FALSE;
    533     }
    534     return TRUE;
    535 }
    536 
    537 void
    538 meta_init(const char *make_mode)
    539 {
    540     static int once = 0;
    541     char *cp;
    542 
    543     useMeta = TRUE;
    544     useFilemon = TRUE;
    545     writeMeta = TRUE;
    546 
    547     if (make_mode) {
    548 	if (strstr(make_mode, "env"))
    549 	    metaEnv = TRUE;
    550 	if (strstr(make_mode, "verb"))
    551 	    metaVerbose = TRUE;
    552 	if (strstr(make_mode, "read"))
    553 	    writeMeta = FALSE;
    554 	if (strstr(make_mode, "nofilemon"))
    555 	    useFilemon = FALSE;
    556 	if ((cp = strstr(make_mode, "curdirok="))) {
    557 	    metaCurdirOk = boolValue(&cp[9]);
    558 	}
    559 	if (strstr(make_mode, "ignore-cmd"))
    560 	    metaIgnoreCMDs = TRUE;
    561 	/* for backwards compatability */
    562 	Var_Set(".MAKE.META_CREATED", "${.MAKE.META.CREATED}", VAR_GLOBAL, 0);
    563 	Var_Set(".MAKE.META_FILES", "${.MAKE.META.FILES}", VAR_GLOBAL, 0);
    564     }
    565     if (metaVerbose && !Var_Exists(MAKE_META_PREFIX, VAR_GLOBAL)) {
    566 	/*
    567 	 * The default value for MAKE_META_PREFIX
    568 	 * prints the absolute path of the target.
    569 	 * This works be cause :H will generate '.' if there is no /
    570 	 * and :tA will resolve that to cwd.
    571 	 */
    572 	Var_Set(MAKE_META_PREFIX, "Building ${.TARGET:H:tA}/${.TARGET:T}", VAR_GLOBAL, 0);
    573     }
    574     if (once)
    575 	return;
    576     once = 1;
    577     memset(&Mybm, 0, sizeof(Mybm));
    578     /*
    579      * We consider ourselves master of all within ${.MAKE.META.BAILIWICK}
    580      */
    581     metaBailiwick = Lst_Init(FALSE);
    582     cp = Var_Subst(NULL, "${.MAKE.META.BAILIWICK:O:u:tA}", VAR_GLOBAL, 0);
    583     if (cp) {
    584 	str2Lst_Append(metaBailiwick, cp, NULL);
    585     }
    586 }
    587 
    588 /*
    589  * In each case below we allow for job==NULL
    590  */
    591 void
    592 meta_job_start(Job *job, GNode *gn)
    593 {
    594     BuildMon *pbm;
    595 
    596     if (job != NULL) {
    597 	pbm = &job->bm;
    598     } else {
    599 	pbm = &Mybm;
    600     }
    601     pbm->mfp = meta_create(pbm, gn);
    602 #ifdef USE_FILEMON_ONCE
    603     /* compat mode we open the filemon dev once per command */
    604     if (job == NULL)
    605 	return;
    606 #endif
    607 #ifdef USE_FILEMON
    608     if (pbm->mfp != NULL && useFilemon) {
    609 	filemon_open(pbm);
    610     } else {
    611 	pbm->mon_fd = pbm->filemon_fd = -1;
    612     }
    613 #endif
    614 }
    615 
    616 /*
    617  * The child calls this before doing anything.
    618  * It does not disturb our state.
    619  */
    620 void
    621 meta_job_child(Job *job)
    622 {
    623 #ifdef USE_FILEMON
    624     BuildMon *pbm;
    625     pid_t pid;
    626 
    627     if (job != NULL) {
    628 	pbm = &job->bm;
    629     } else {
    630 	pbm = &Mybm;
    631     }
    632     pid = getpid();
    633     if (pbm->mfp != NULL && useFilemon) {
    634 	if (ioctl(pbm->filemon_fd, FILEMON_SET_PID, &pid) < 0) {
    635 	    err(1, "Could not set filemon pid!");
    636 	}
    637     }
    638 #endif
    639 }
    640 
    641 void
    642 meta_job_error(Job *job, GNode *gn, int flags, int status)
    643 {
    644     char cwd[MAXPATHLEN];
    645     BuildMon *pbm;
    646 
    647     if (job != NULL) {
    648 	pbm = &job->bm;
    649     } else {
    650 	if (!gn)
    651 	    gn = job->node;
    652 	pbm = &Mybm;
    653     }
    654     if (pbm->mfp != NULL) {
    655 	fprintf(pbm->mfp, "*** Error code %d%s\n",
    656 		status,
    657 		(flags & JOB_IGNERR) ?
    658 		"(ignored)" : "");
    659     }
    660     if (gn) {
    661 	Var_Set(".ERROR_TARGET", gn->path ? gn->path : gn->name, VAR_GLOBAL, 0);
    662     }
    663     getcwd(cwd, sizeof(cwd));
    664     Var_Set(".ERROR_CWD", cwd, VAR_GLOBAL, 0);
    665     if (pbm && pbm->meta_fname[0]) {
    666 	Var_Set(".ERROR_META_FILE", pbm->meta_fname, VAR_GLOBAL, 0);
    667     }
    668     meta_job_finish(job);
    669 }
    670 
    671 void
    672 meta_job_output(Job *job, char *cp, const char *nl)
    673 {
    674     BuildMon *pbm;
    675 
    676     if (job != NULL) {
    677 	pbm = &job->bm;
    678     } else {
    679 	pbm = &Mybm;
    680     }
    681     if (pbm->mfp != NULL) {
    682 	if (metaVerbose) {
    683 	    static char *meta_prefix = NULL;
    684 	    static int meta_prefix_len;
    685 
    686 	    if (!meta_prefix) {
    687 		char *cp2;
    688 
    689 		meta_prefix = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", VAR_GLOBAL, 0);
    690 		if ((cp2 = strchr(meta_prefix, '$')))
    691 		    meta_prefix_len = cp2 - meta_prefix;
    692 		else
    693 		    meta_prefix_len = strlen(meta_prefix);
    694 	    }
    695 	    if (strncmp(cp, meta_prefix, meta_prefix_len) == 0) {
    696 		cp = strchr(cp+1, '\n');
    697 		if (!cp++)
    698 		    return;
    699 	    }
    700 	}
    701 	fprintf(pbm->mfp, "%s%s", cp, nl);
    702     }
    703 }
    704 
    705 void
    706 meta_cmd_finish(void *pbmp)
    707 {
    708 #ifdef USE_FILEMON
    709     BuildMon *pbm = pbmp;
    710 
    711     if (!pbm)
    712 	pbm = &Mybm;
    713 
    714     if (pbm->filemon_fd >= 0) {
    715 	close(pbm->filemon_fd);
    716 	filemon_read(pbm->mfp, pbm->mon_fd);
    717 	pbm->filemon_fd = pbm->mon_fd = -1;
    718     }
    719 #endif
    720 }
    721 
    722 void
    723 meta_job_finish(Job *job)
    724 {
    725     BuildMon *pbm;
    726 
    727     if (job != NULL) {
    728 	pbm = &job->bm;
    729     } else {
    730 	pbm = &Mybm;
    731     }
    732     if (pbm->mfp != NULL) {
    733 	meta_cmd_finish(pbm);
    734 	fclose(pbm->mfp);
    735 	pbm->mfp = NULL;
    736 	pbm->meta_fname[0] = '\0';
    737     }
    738 }
    739 
    740 /*
    741  * Fetch a full line from fp - growing bufp if needed
    742  * Return length in bufp.
    743  */
    744 static int
    745 fgetLine(char **bufp, size_t *szp, int o, FILE *fp)
    746 {
    747     char *buf = *bufp;
    748     size_t bufsz = *szp;
    749     struct stat fs;
    750     int x;
    751 
    752     if (fgets(&buf[o], bufsz - o, fp) != NULL) {
    753     check_newline:
    754 	x = o + strlen(&buf[o]);
    755 	if (buf[x - 1] == '\n')
    756 	    return x;
    757 	/*
    758 	 * We need to grow the buffer.
    759 	 * The meta file can give us a clue.
    760 	 */
    761 	if (fstat(fileno(fp), &fs) == 0) {
    762 	    size_t newsz;
    763 	    char *p;
    764 
    765 	    newsz = ROUNDUP((fs.st_size / 2), BUFSIZ);
    766 	    if (newsz <= bufsz)
    767 		newsz = ROUNDUP(fs.st_size, BUFSIZ);
    768 	    if (DEBUG(META))
    769 		fprintf(debug_file, "growing buffer %zu -> %zu\n",
    770 			bufsz, newsz);
    771 	    p = bmake_realloc(buf, newsz);
    772 	    if (p) {
    773 		*bufp = buf = p;
    774 		*szp = bufsz = newsz;
    775 		/* fetch the rest */
    776 		if (!fgets(&buf[x], bufsz - x, fp))
    777 		    return x;		/* truncated! */
    778 		goto check_newline;
    779 	    }
    780 	}
    781     }
    782     return 0;
    783 }
    784 
    785 static int
    786 prefix_match(void *p, void *q)
    787 {
    788     const char *prefix = p;
    789     const char *path = q;
    790     size_t n = strlen(prefix);
    791 
    792     return (0 == strncmp(path, prefix, n));
    793 }
    794 
    795 static int
    796 string_match(const void *p, const void *q)
    797 {
    798     const char *p1 = p;
    799     const char *p2 = q;
    800 
    801     return strcmp(p1, p2);
    802 }
    803 
    804 
    805 /*
    806  * When running with 'meta' functionality, a target can be out-of-date
    807  * if any of the references in it's meta data file is more recent.
    808  * We have to track the latestdir on a per-process basis.
    809  */
    810 #define LDIR_VNAME_FMT ".meta.%d.ldir"
    811 
    812 /*
    813  * It is possible that a .meta file is corrupted,
    814  * if we detect this we want to reproduce it.
    815  * Setting oodate TRUE will have that effect.
    816  */
    817 #define CHECK_VALID_META(p) if (!(p && *p)) { \
    818     warnx("%s: %d: malformed", fname, lineno); \
    819     oodate = TRUE; \
    820     continue; \
    821     }
    822 
    823 Boolean
    824 meta_oodate(GNode *gn, Boolean oodate)
    825 {
    826     static char *tmpdir = NULL;
    827     static char cwd[MAXPATHLEN];
    828     char ldir_vname[64];
    829     char latestdir[MAXPATHLEN];
    830     char fname[MAXPATHLEN];
    831     char fname1[MAXPATHLEN];
    832     char fname2[MAXPATHLEN];
    833     char *p;
    834     char *cp;
    835     static size_t cwdlen = 0;
    836     static size_t tmplen = 0;
    837     FILE *fp;
    838     Boolean ignoreOODATE = FALSE;
    839     Lst missingFiles;
    840 
    841     if (oodate)
    842 	return oodate;		/* we're done */
    843 
    844     missingFiles = Lst_Init(FALSE);
    845 
    846     /*
    847      * We need to check if the target is out-of-date. This includes
    848      * checking if the expanded command has changed. This in turn
    849      * requires that all variables are set in the same way that they
    850      * would be if the target needs to be re-built.
    851      */
    852     Make_DoAllVar(gn);
    853 
    854     meta_name(gn, fname, sizeof(fname), NULL, NULL);
    855 
    856 #ifdef DEBUG_META_MODE
    857     if (DEBUG(META))
    858 	fprintf(debug_file, "meta_oodate: %s\n", fname);
    859 #endif
    860 
    861     if ((fp = fopen(fname, "r")) != NULL) {
    862 	static char *buf = NULL;
    863 	static size_t bufsz;
    864 	int lineno = 0;
    865 	int lastpid = 0;
    866 	int pid;
    867 	int f = 0;
    868 	int x;
    869 	LstNode ln;
    870 	struct stat fs;
    871 
    872 	if (!buf) {
    873 	    bufsz = 8 * BUFSIZ;
    874 	    buf = bmake_malloc(bufsz);
    875 	}
    876 
    877 	if (!cwdlen) {
    878 	    if (getcwd(cwd, sizeof(cwd)) == NULL)
    879 		err(1, "Could not get current working directory");
    880 	    cwdlen = strlen(cwd);
    881 	}
    882 
    883 	if (!tmpdir) {
    884 	    tmpdir = getTmpdir();
    885 	    tmplen = strlen(tmpdir);
    886 	}
    887 
    888 	/* we want to track all the .meta we read */
    889 	Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
    890 
    891 	ln = Lst_First(gn->commands);
    892 	while (!oodate && (x = fgetLine(&buf, &bufsz, 0, fp)) > 0) {
    893 	    lineno++;
    894 	    if (buf[x - 1] == '\n')
    895 		buf[x - 1] = '\0';
    896 	    else {
    897 		warnx("%s: %d: line truncated at %u", fname, lineno, x);
    898 		oodate = TRUE;
    899 		break;
    900 	    }
    901 	    /* Find the start of the build monitor section. */
    902 	    if (!f) {
    903 		if (strncmp(buf, "-- filemon", 10) == 0) {
    904 		    f = 1;
    905 		    continue;
    906 		}
    907 		if (strncmp(buf, "# buildmon", 10) == 0) {
    908 		    f = 1;
    909 		    continue;
    910 		}
    911 	    }
    912 
    913 	    /* Delimit the record type. */
    914 	    p = buf;
    915 #ifdef DEBUG_META_MODE
    916 	    if (DEBUG(META))
    917 		fprintf(debug_file, "%s: %d: %s\n", fname, lineno, buf);
    918 #endif
    919 	    strsep(&p, " ");
    920 	    if (f) {
    921 		/*
    922 		 * We are in the 'filemon' output section.
    923 		 * Each record from filemon follows the general form:
    924 		 *
    925 		 * <key> <pid> <data>
    926 		 *
    927 		 * Where:
    928 		 * <key> is a single letter, denoting the syscall.
    929 		 * <pid> is the process that made the syscall.
    930 		 * <data> is the arguments (of interest).
    931 		 */
    932 		switch(buf[0]) {
    933 		case '#':		/* comment */
    934 		case 'V':		/* version */
    935 		    break;
    936 		default:
    937 		    /*
    938 		     * We need to track pathnames per-process.
    939 		     *
    940 		     * Each process run by make, starts off in the 'CWD'
    941 		     * recorded in the .meta file, if it chdirs ('C')
    942 		     * elsewhere we need to track that - but only for
    943 		     * that process.  If it forks ('F'), we initialize
    944 		     * the child to have the same cwd as its parent.
    945 		     *
    946 		     * We also need to track the 'latestdir' of
    947 		     * interest.  This is usually the same as cwd, but
    948 		     * not if a process is reading directories.
    949 		     *
    950 		     * Each time we spot a different process ('pid')
    951 		     * we save the current value of 'latestdir' in a
    952 		     * variable qualified by 'lastpid', and
    953 		     * re-initialize 'latestdir' to any pre-saved
    954 		     * value for the current 'pid' and 'CWD' if none.
    955 		     */
    956 		    CHECK_VALID_META(p);
    957 		    pid = atoi(p);
    958 		    if (pid > 0 && pid != lastpid) {
    959 			char *ldir;
    960 			char *tp;
    961 
    962 			if (lastpid > 0) {
    963 			    /* We need to remember this. */
    964 			    Var_Set(ldir_vname, latestdir, VAR_GLOBAL, 0);
    965 			}
    966 			snprintf(ldir_vname, sizeof(ldir_vname), LDIR_VNAME_FMT, pid);
    967 			lastpid = pid;
    968 			ldir = Var_Value(ldir_vname, VAR_GLOBAL, &tp);
    969 			if (ldir) {
    970 			    strlcpy(latestdir, ldir, sizeof(latestdir));
    971 			    if (tp)
    972 				free(tp);
    973 			} else
    974 			    strlcpy(latestdir, cwd, sizeof(latestdir));
    975 		    }
    976 		    /* Skip past the pid. */
    977 		    if (strsep(&p, " ") == NULL)
    978 			continue;
    979 #ifdef DEBUG_META_MODE
    980 		    if (DEBUG(META))
    981 			fprintf(debug_file, "%s: %d: cwd=%s ldir=%s\n", fname, lineno, cwd, latestdir);
    982 #endif
    983 		    break;
    984 		}
    985 
    986 		CHECK_VALID_META(p);
    987 
    988 		/* Process according to record type. */
    989 		switch (buf[0]) {
    990 		case 'X':		/* eXit */
    991 		    Var_Delete(ldir_vname, VAR_GLOBAL);
    992 		    lastpid = 0;	/* no need to save ldir_vname */
    993 		    break;
    994 
    995 		case 'F':		/* [v]Fork */
    996 		    {
    997 			char cldir[64];
    998 			int child;
    999 
   1000 			child = atoi(p);
   1001 			if (child > 0) {
   1002 			    snprintf(cldir, sizeof(cldir), LDIR_VNAME_FMT, child);
   1003 			    Var_Set(cldir, latestdir, VAR_GLOBAL, 0);
   1004 			}
   1005 		    }
   1006 		    break;
   1007 
   1008 		case 'C':		/* Chdir */
   1009 		    /* Update the latest directory. */
   1010 		    strlcpy(latestdir, p, sizeof(latestdir));
   1011 		    break;
   1012 
   1013 		case 'M':		/* renaMe */
   1014 		    if (Lst_IsEmpty(missingFiles))
   1015 			break;
   1016 		    /* 'L' and 'M' put single quotes around the args */
   1017 		    if (*p == '\'') {
   1018 			char *ep;
   1019 
   1020 			p++;
   1021 			if ((ep = strchr(p, '\'')))
   1022 			    *ep = '\0';
   1023 		    }
   1024 		    /* FALLTHROUGH */
   1025 		case 'D':		/* unlink */
   1026 		    if (*p == '/' && !Lst_IsEmpty(missingFiles)) {
   1027 			/* remove p from the missingFiles list if present */
   1028 			if ((ln = Lst_Find(missingFiles, p, string_match)) != NULL) {
   1029 			    char *tp = Lst_Datum(ln);
   1030 			    Lst_Remove(missingFiles, ln);
   1031 			    free(tp);
   1032 			}
   1033 		    }
   1034 		    break;
   1035 		case 'L':		/* Link */
   1036 		    /* we want the target */
   1037 		    if (strsep(&p, " ") == NULL)
   1038 			continue;
   1039 		    CHECK_VALID_META(p);
   1040 		    /* 'L' and 'M' put single quotes around the args */
   1041 		    if (*p == '\'') {
   1042 			char *ep;
   1043 
   1044 			p++;
   1045 			if ((ep = strchr(p, '\'')))
   1046 			    *ep = '\0';
   1047 		    }
   1048 		    /* FALLTHROUGH */
   1049 		case 'W':		/* Write */
   1050 		    /*
   1051 		     * If a file we generated within our bailiwick
   1052 		     * but outside of .OBJDIR is missing,
   1053 		     * we need to do it again.
   1054 		     */
   1055 		    /* ignore non-absolute paths */
   1056 		    if (*p != '/')
   1057 			break;
   1058 
   1059 		    if (Lst_IsEmpty(metaBailiwick))
   1060 			break;
   1061 
   1062 		    /* ignore cwd - normal dependencies handle those */
   1063 		    if (strncmp(p, cwd, cwdlen) == 0)
   1064 			break;
   1065 
   1066 		    if (!Lst_ForEach(metaBailiwick, prefix_match, p))
   1067 			break;
   1068 
   1069 		    /* tmpdir might be within */
   1070 		    if (tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0)
   1071 			break;
   1072 
   1073 		    /* ignore anything containing the string "tmp" */
   1074 		    if ((strstr("tmp", p)))
   1075 			break;
   1076 
   1077 		    if (stat(p, &fs) < 0) {
   1078 			Lst_AtEnd(missingFiles, bmake_strdup(p));
   1079 		    }
   1080 		    break;
   1081 		case 'R':		/* Read */
   1082 		case 'E':		/* Exec */
   1083 		    /*
   1084 		     * Check for runtime files that can't
   1085 		     * be part of the dependencies because
   1086 		     * they are _expected_ to change.
   1087 		     */
   1088 		    if (strncmp(p, "/tmp/", 5) == 0 ||
   1089 			(tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0))
   1090 			break;
   1091 
   1092 		    if (strncmp(p, "/var/", 5) == 0)
   1093 			break;
   1094 
   1095 		    /* Ignore device files. */
   1096 		    if (strncmp(p, "/dev/", 5) == 0)
   1097 			break;
   1098 
   1099 		    /* Ignore /etc/ files. */
   1100 		    if (strncmp(p, "/etc/", 5) == 0)
   1101 			break;
   1102 
   1103 		    /*
   1104 		     * The rest of the record is the file name.
   1105 		     * Check if it's not an absolute path.
   1106 		     */
   1107 		    {
   1108 			char *sdirs[4];
   1109 			char **sdp;
   1110 			int sdx = 0;
   1111 			int found = 0;
   1112 
   1113 			if (*p == '/') {
   1114 			    sdirs[sdx++] = p; /* done */
   1115 			} else {
   1116 			    if (strcmp(".", p) == 0)
   1117 				continue;  /* no point */
   1118 
   1119 			    /* Check vs latestdir */
   1120 			    snprintf(fname1, sizeof(fname1), "%s/%s", latestdir, p);
   1121 			    sdirs[sdx++] = fname1;
   1122 
   1123 			    if (strcmp(latestdir, cwd) != 0) {
   1124 				/* Check vs cwd */
   1125 				snprintf(fname2, sizeof(fname2), "%s/%s", cwd, p);
   1126 				sdirs[sdx++] = fname2;
   1127 			    }
   1128 			}
   1129 			sdirs[sdx++] = NULL;
   1130 
   1131 			for (sdp = sdirs; *sdp && !found; sdp++) {
   1132 #ifdef DEBUG_META_MODE
   1133 			    if (DEBUG(META))
   1134 				fprintf(debug_file, "%s: %d: looking for: %s\n", fname, lineno, *sdp);
   1135 #endif
   1136 			    if (stat(*sdp, &fs) == 0) {
   1137 				found = 1;
   1138 				p = *sdp;
   1139 			    }
   1140 			}
   1141 			if (found) {
   1142 #ifdef DEBUG_META_MODE
   1143 			    if (DEBUG(META))
   1144 				fprintf(debug_file, "%s: %d: found: %s\n", fname, lineno, p);
   1145 #endif
   1146 			    if (!S_ISDIR(fs.st_mode) &&
   1147 				fs.st_mtime > gn->mtime) {
   1148 				if (DEBUG(META))
   1149 				    fprintf(debug_file, "%s: %d: file '%s' is newer than the target...\n", fname, lineno, p);
   1150 				oodate = TRUE;
   1151 			    } else if (S_ISDIR(fs.st_mode)) {
   1152 				/* Update the latest directory. */
   1153 				realpath(p, latestdir);
   1154 			    }
   1155 			} else if (errno == ENOENT && *p == '/' &&
   1156 				   strncmp(p, cwd, cwdlen) != 0) {
   1157 			    /*
   1158 			     * A referenced file outside of CWD is missing.
   1159 			     * We cannot catch every eventuality here...
   1160 			     */
   1161 			    if (DEBUG(META))
   1162 				fprintf(debug_file, "%s: %d: file '%s' may have moved?...\n", fname, lineno, p);
   1163 			    oodate = TRUE;
   1164 			}
   1165 		    }
   1166 		    break;
   1167 		default:
   1168 		    break;
   1169 		}
   1170 	    } else if (strcmp(buf, "CMD") == 0) {
   1171 		/*
   1172 		 * Compare the current command with the one in the
   1173 		 * meta data file.
   1174 		 */
   1175 		if (ln == NULL) {
   1176 		    if (DEBUG(META))
   1177 			fprintf(debug_file, "%s: %d: there were more build commands in the meta data file than there are now...\n", fname, lineno);
   1178 		    oodate = TRUE;
   1179 		} else {
   1180 		    char *cmd = (char *)Lst_Datum(ln);
   1181 
   1182 		    if (!ignoreOODATE) {
   1183 			if (strstr(cmd, "$?"))
   1184 			    ignoreOODATE = TRUE;
   1185 			else if ((cp = strstr(cmd, ".OODATE"))) {
   1186 			    /* check for $[{(].OODATE[)}] */
   1187 			    if (cp > cmd + 2 && cp[-2] == '$')
   1188 				ignoreOODATE = TRUE;
   1189 			}
   1190 			if (ignoreOODATE && DEBUG(META))
   1191 			    fprintf(debug_file, "%s: %d: cannot compare commands using .OODATE\n", fname, lineno);
   1192 		    }
   1193 		    cmd = Var_Subst(NULL, cmd, gn, TRUE);
   1194 
   1195 		    if ((cp = strchr(cmd, '\n'))) {
   1196 			int n;
   1197 
   1198 			/*
   1199 			 * This command contains newlines, we need to
   1200 			 * fetch more from the .meta file before we
   1201 			 * attempt a comparison.
   1202 			 */
   1203 			/* first put the newline back at buf[x - 1] */
   1204 			buf[x - 1] = '\n';
   1205 			do {
   1206 			    /* now fetch the next line */
   1207 			    if ((n = fgetLine(&buf, &bufsz, x, fp)) <= 0)
   1208 				break;
   1209 			    x = n;
   1210 			    lineno++;
   1211 			    if (buf[x - 1] != '\n') {
   1212 				warnx("%s: %d: line truncated at %u", fname, lineno, x);
   1213 				break;
   1214 			    }
   1215 			    cp = strchr(++cp, '\n');
   1216 			} while (cp);
   1217 			if (buf[x - 1] == '\n')
   1218 			    buf[x - 1] = '\0';
   1219 		    }
   1220 		    if (!ignoreOODATE &&
   1221 			!(gn->type & OP_NOMETA_CMP) &&
   1222 			strcmp(p, cmd) != 0) {
   1223 			if (DEBUG(META))
   1224 			    fprintf(debug_file, "%s: %d: a build command has changed\n%s\nvs\n%s\n", fname, lineno, p, cmd);
   1225 			if (!metaIgnoreCMDs)
   1226 			    oodate = TRUE;
   1227 		    }
   1228 		    free(cmd);
   1229 		    ln = Lst_Succ(ln);
   1230 		}
   1231 	    } else if (strcmp(buf, "CWD") == 0) {
   1232 		/*
   1233 		 * Check if there are extra commands now
   1234 		 * that weren't in the meta data file.
   1235 		 */
   1236 		if (!oodate && ln != NULL) {
   1237 		    if (DEBUG(META))
   1238 			fprintf(debug_file, "%s: %d: there are extra build commands now that weren't in the meta data file\n", fname, lineno);
   1239 		    oodate = TRUE;
   1240 		}
   1241 		if (strcmp(p, cwd) != 0) {
   1242 		    if (DEBUG(META))
   1243 			fprintf(debug_file, "%s: %d: the current working directory has changed from '%s' to '%s'\n", fname, lineno, p, curdir);
   1244 		    oodate = TRUE;
   1245 		}
   1246 	    }
   1247 	}
   1248 
   1249 	fclose(fp);
   1250 	if (!Lst_IsEmpty(missingFiles)) {
   1251 	    if (DEBUG(META))
   1252 		fprintf(debug_file, "%s: missing files: %s...\n",
   1253 			fname, (char *)Lst_Datum(Lst_First(missingFiles)));
   1254 	    oodate = TRUE;
   1255 	    Lst_Destroy(missingFiles, (FreeProc *)free);
   1256 	}
   1257     } else {
   1258 	if ((gn->type & OP_META)) {
   1259 	    if (DEBUG(META))
   1260 		fprintf(debug_file, "%s: required but missing\n", fname);
   1261 	    oodate = TRUE;
   1262 	}
   1263     }
   1264     if (oodate && ignoreOODATE) {
   1265 	/*
   1266 	 * Target uses .OODATE, so we need to re-compute it.
   1267 	 * We need to clean up what Make_DoAllVar() did.
   1268 	 */
   1269 	Var_Delete(ALLSRC, gn);
   1270 	Var_Delete(OODATE, gn);
   1271 	gn->flags &= ~DONE_ALLSRC;
   1272     }
   1273     return oodate;
   1274 }
   1275 
   1276 /* support for compat mode */
   1277 
   1278 static int childPipe[2];
   1279 
   1280 void
   1281 meta_compat_start(void)
   1282 {
   1283 #ifdef USE_FILEMON_ONCE
   1284     /*
   1285      * We need to re-open filemon for each cmd.
   1286      */
   1287     BuildMon *pbm = &Mybm;
   1288 
   1289     if (pbm->mfp != NULL && useFilemon) {
   1290 	filemon_open(pbm);
   1291     } else {
   1292 	pbm->mon_fd = pbm->filemon_fd = -1;
   1293     }
   1294 #endif
   1295     if (pipe(childPipe) < 0)
   1296 	Punt("Cannot create pipe: %s", strerror(errno));
   1297     /* Set close-on-exec flag for both */
   1298     (void)fcntl(childPipe[0], F_SETFD, 1);
   1299     (void)fcntl(childPipe[1], F_SETFD, 1);
   1300 }
   1301 
   1302 void
   1303 meta_compat_child(void)
   1304 {
   1305     meta_job_child(NULL);
   1306     if (dup2(childPipe[1], 1) < 0 ||
   1307 	dup2(1, 2) < 0) {
   1308 	execError("dup2", "pipe");
   1309 	_exit(1);
   1310     }
   1311 }
   1312 
   1313 void
   1314 meta_compat_parent(void)
   1315 {
   1316     FILE *fp;
   1317     char buf[BUFSIZ];
   1318 
   1319     close(childPipe[1]);			/* child side */
   1320     fp = fdopen(childPipe[0], "r");
   1321     while (fgets(buf, sizeof(buf), fp)) {
   1322 	meta_job_output(NULL, buf, "");
   1323 	printf("%s", buf);
   1324     }
   1325     fclose(fp);
   1326 }
   1327 
   1328 #endif	/* USE_META */
   1329