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