xref: /netbsd-src/usr.bin/make/meta.c (revision 0c336a8c052a37cdf502a55be2c3f58963a4ecf6)
1*0c336a8cSrillig /*      $NetBSD: meta.c,v 1.210 2024/06/02 15:31:26 rillig Exp $ */
2ff543813Ssjg 
34db43f7eSsjg /*
44db43f7eSsjg  * Implement 'meta' mode.
54db43f7eSsjg  * Adapted from John Birrell's patches to FreeBSD make.
64db43f7eSsjg  * --sjg
74db43f7eSsjg  */
84db43f7eSsjg /*
9f7ebaac7Ssjg  * Copyright (c) 2009-2016, Juniper Networks, Inc.
104db43f7eSsjg  * Portions Copyright (c) 2009, John Birrell.
114db43f7eSsjg  *
124db43f7eSsjg  * Redistribution and use in source and binary forms, with or without
134db43f7eSsjg  * modification, are permitted provided that the following conditions
144db43f7eSsjg  * are met:
154db43f7eSsjg  * 1. Redistributions of source code must retain the above copyright
164db43f7eSsjg  *    notice, this list of conditions and the following disclaimer.
174db43f7eSsjg  * 2. Redistributions in binary form must reproduce the above copyright
184db43f7eSsjg  *    notice, this list of conditions and the following disclaimer in the
194db43f7eSsjg  *    documentation and/or other materials provided with the distribution.
204db43f7eSsjg  *
214db43f7eSsjg  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
224db43f7eSsjg  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
234db43f7eSsjg  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
244db43f7eSsjg  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
254db43f7eSsjg  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
264db43f7eSsjg  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
274db43f7eSsjg  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
284db43f7eSsjg  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
294db43f7eSsjg  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
304db43f7eSsjg  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
314db43f7eSsjg  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
324db43f7eSsjg  */
33e7543dc4Ssjg #if defined(USE_META)
344db43f7eSsjg 
354db43f7eSsjg #ifdef HAVE_CONFIG_H
364db43f7eSsjg # include "config.h"
374db43f7eSsjg #endif
384db43f7eSsjg #include <sys/stat.h>
394db43f7eSsjg #include <libgen.h>
404db43f7eSsjg #include <errno.h>
41796c4b02Ssjg #if !defined(HAVE_CONFIG_H) || defined(HAVE_ERR_H)
424db43f7eSsjg #include <err.h>
43796c4b02Ssjg #endif
444db43f7eSsjg 
454db43f7eSsjg #include "make.h"
460c93b3daSrillig #include "dir.h"
474db43f7eSsjg #include "job.h"
484db43f7eSsjg 
491378959eSriastradh #ifdef USE_FILEMON
50bea0f8c1Sriastradh #include "filemon/filemon.h"
512a5d072bSmaxv #endif
522a5d072bSmaxv 
534db43f7eSsjg static BuildMon Mybm;			/* for compat */
54733418ceSrillig static StringList metaBailiwick = LST_INIT; /* our scope of control */
55e46a288cSchristos static char *metaBailiwickStr;		/* string storage for the list */
56733418ceSrillig static StringList metaIgnorePaths = LST_INIT; /* paths we deliberately ignore */
57e46a288cSchristos static char *metaIgnorePathsStr;	/* string storage for the list */
58adfd644bSsjg 
59adfd644bSsjg #ifndef MAKE_META_IGNORE_PATHS
60adfd644bSsjg #define MAKE_META_IGNORE_PATHS ".MAKE.META.IGNORE_PATHS"
61adfd644bSsjg #endif
621ea1c693Ssjg #ifndef MAKE_META_IGNORE_PATTERNS
631ea1c693Ssjg #define MAKE_META_IGNORE_PATTERNS ".MAKE.META.IGNORE_PATTERNS"
641ea1c693Ssjg #endif
6569fc18aaSsjg #ifndef MAKE_META_IGNORE_FILTER
6669fc18aaSsjg #define MAKE_META_IGNORE_FILTER ".MAKE.META.IGNORE_FILTER"
6769fc18aaSsjg #endif
683e72ec51Ssjg #ifndef MAKE_META_CMP_FILTER
693e72ec51Ssjg #define MAKE_META_CMP_FILTER ".MAKE.META.CMP_FILTER"
703e72ec51Ssjg #endif
714db43f7eSsjg 
72244fd9f4Srillig bool useMeta = false;
73244fd9f4Srillig static bool useFilemon = false;
74244fd9f4Srillig static bool writeMeta = false;
75244fd9f4Srillig static bool metaMissing = false;	/* oodate if missing */
76244fd9f4Srillig static bool filemonMissing = false;	/* oodate if missing */
77244fd9f4Srillig static bool metaEnv = false;		/* don't save env unless asked */
78244fd9f4Srillig static bool metaVerbose = false;
79244fd9f4Srillig static bool metaIgnoreCMDs = false;	/* ignore CMDs in .meta files */
80244fd9f4Srillig static bool metaIgnorePatterns = false; /* do we need to do pattern matches */
81244fd9f4Srillig static bool metaIgnoreFilter = false;	/* do we have more complex filtering? */
823e72ec51Ssjg static bool metaCmpFilter = false;	/* do we have CMP_FILTER ? */
83244fd9f4Srillig static bool metaCurdirOk = false;	/* write .meta in .CURDIR Ok? */
84244fd9f4Srillig static bool metaSilent = false;		/* if we have a .meta be SILENT */
854db43f7eSsjg 
864db43f7eSsjg 
874db43f7eSsjg #define MAKE_META_PREFIX	".MAKE.META.PREFIX"
884db43f7eSsjg 
894db43f7eSsjg #ifndef N2U
904db43f7eSsjg # define N2U(n, u)   (((n) + ((u) - 1)) / (u))
914db43f7eSsjg #endif
924db43f7eSsjg #ifndef ROUNDUP
934db43f7eSsjg # define ROUNDUP(n, u)   (N2U((n), (u)) * (u))
944db43f7eSsjg #endif
954db43f7eSsjg 
96796c4b02Ssjg #if !defined(HAVE_STRSEP)
97a0c92525Srillig # define strsep(s, d) stresep((s), (d), '\0')
98796c4b02Ssjg #endif
99796c4b02Ssjg 
1004db43f7eSsjg /*
1012a5d072bSmaxv  * Filemon is a kernel module which snoops certain syscalls.
1022a5d072bSmaxv  *
1032a5d072bSmaxv  * C chdir
1042a5d072bSmaxv  * E exec
1052a5d072bSmaxv  * F [v]fork
1062a5d072bSmaxv  * L [sym]link
1072a5d072bSmaxv  * M rename
1082a5d072bSmaxv  * R read
1092a5d072bSmaxv  * W write
1102a5d072bSmaxv  * S stat
1112a5d072bSmaxv  *
1122a5d072bSmaxv  * See meta_oodate below - we mainly care about 'E' and 'R'.
1132a5d072bSmaxv  *
1142a5d072bSmaxv  * We can still use meta mode without filemon, but
1152a5d072bSmaxv  * the benefits are more limited.
1162a5d072bSmaxv  */
1172a5d072bSmaxv #ifdef USE_FILEMON
1182a5d072bSmaxv 
1192a5d072bSmaxv /*
1202a5d072bSmaxv  * Open the filemon device.
1212a5d072bSmaxv  */
1222a5d072bSmaxv static void
meta_open_filemon(BuildMon * pbm)1231378959eSriastradh meta_open_filemon(BuildMon *pbm)
1242a5d072bSmaxv {
1251378959eSriastradh     int dupfd;
1262a5d072bSmaxv 
1271378959eSriastradh     pbm->mon_fd = -1;
1281378959eSriastradh     pbm->filemon = NULL;
12931940a95Srillig     if (!useFilemon || pbm->mfp == NULL)
1302a5d072bSmaxv 	return;
1312a5d072bSmaxv 
1321378959eSriastradh     pbm->filemon = filemon_open();
1331378959eSriastradh     if (pbm->filemon == NULL) {
134244fd9f4Srillig 	useFilemon = false;
135bea0f8c1Sriastradh 	warn("Could not open filemon %s", filemon_path());
1362a5d072bSmaxv 	return;
1372a5d072bSmaxv     }
1382a5d072bSmaxv 
1392a5d072bSmaxv     /*
1402a5d072bSmaxv      * We use a file outside of '.'
1412a5d072bSmaxv      * to avoid a FreeBSD kernel bug where unlink invalidates
1422a5d072bSmaxv      * cwd causing getcwd to do a lot more work.
1432a5d072bSmaxv      * We only care about the descriptor.
1442a5d072bSmaxv      */
1452b67d42eSsjg     if (!opts.compatMake)
1462b67d42eSsjg 	pbm->mon_fd = Job_TempFile("filemon.XXXXXX", NULL, 0);
1472b67d42eSsjg     else
1482b67d42eSsjg 	pbm->mon_fd = mkTempFile("filemon.XXXXXX", NULL, 0);
1491378959eSriastradh     if ((dupfd = dup(pbm->mon_fd)) == -1) {
1507cd58762Ssjg 	Punt("Could not dup filemon output: %s", strerror(errno));
1511378959eSriastradh     }
1521378959eSriastradh     (void)fcntl(dupfd, F_SETFD, FD_CLOEXEC);
1531378959eSriastradh     if (filemon_setfd(pbm->filemon, dupfd) == -1) {
1547cd58762Ssjg 	Punt("Could not set filemon file descriptor: %s", strerror(errno));
1552a5d072bSmaxv     }
1562a5d072bSmaxv     /* we don't need these once we exec */
1572a5d072bSmaxv     (void)fcntl(pbm->mon_fd, F_SETFD, FD_CLOEXEC);
1582a5d072bSmaxv }
1592a5d072bSmaxv 
1602a5d072bSmaxv /*
1612a5d072bSmaxv  * Read the build monitor output file and write records to the target's
1622a5d072bSmaxv  * metadata file.
1632a5d072bSmaxv  */
1642a5d072bSmaxv static int
filemon_read(FILE * mfp,int fd)1652a5d072bSmaxv filemon_read(FILE *mfp, int fd)
1662a5d072bSmaxv {
1672a5d072bSmaxv     char buf[BUFSIZ];
1682a5d072bSmaxv     int error;
1692a5d072bSmaxv 
1702a5d072bSmaxv     /* Check if we're not writing to a meta data file.*/
1712a5d072bSmaxv     if (mfp == NULL) {
1722a5d072bSmaxv 	if (fd >= 0)
1732a5d072bSmaxv 	    close(fd);			/* not interested */
1742a5d072bSmaxv 	return 0;
1752a5d072bSmaxv     }
1762a5d072bSmaxv     /* rewind */
177cb589a02Ssjg     if (lseek(fd, (off_t)0, SEEK_SET) < 0) {
178cb589a02Ssjg 	error = errno;
179cb589a02Ssjg 	warn("Could not rewind filemon");
180cb589a02Ssjg 	fprintf(mfp, "\n");
181cb589a02Ssjg     } else {
1825658125dSrillig 	ssize_t n;
1835658125dSrillig 
1842a5d072bSmaxv 	error = 0;
1852a5d072bSmaxv 	fprintf(mfp, "\n-- filemon acquired metadata --\n");
1862a5d072bSmaxv 
187143a3267Srillig 	while ((n = read(fd, buf, sizeof buf)) > 0) {
1885658125dSrillig 	    if ((ssize_t)fwrite(buf, 1, (size_t)n, mfp) < n)
1892a5d072bSmaxv 		error = EIO;
1902a5d072bSmaxv 	}
191cb589a02Ssjg     }
1927cd58762Ssjg     if (fflush(mfp) != 0)
1937cd58762Ssjg 	Punt("Cannot write filemon data to meta file: %s",
1947cd58762Ssjg 	    strerror(errno));
1952a5d072bSmaxv     if (close(fd) < 0)
1962a5d072bSmaxv 	error = errno;
1972a5d072bSmaxv     return error;
1982a5d072bSmaxv }
1992a5d072bSmaxv #endif
2002a5d072bSmaxv 
2012a5d072bSmaxv /*
2028bb1fe5dSsjg  * when realpath() fails,
2038bb1fe5dSsjg  * we use this, to clean up ./ and ../
2048bb1fe5dSsjg  */
2058bb1fe5dSsjg static void
eat_dots(char * buf)2069c9a1981Srillig eat_dots(char *buf)
2078bb1fe5dSsjg {
2089c9a1981Srillig     char *p;
2098bb1fe5dSsjg 
2109c9a1981Srillig     while ((p = strstr(buf, "/./")) != NULL)
2119c9a1981Srillig 	memmove(p, p + 2, strlen(p + 2) + 1);
2129c9a1981Srillig 
2139c9a1981Srillig     while ((p = strstr(buf, "/../")) != NULL) {
2149c9a1981Srillig 	char *p2 = p + 3;
2159c9a1981Srillig 	if (p > buf) {
2168bb1fe5dSsjg 	    do {
2179c9a1981Srillig 		p--;
2189c9a1981Srillig 	    } while (p > buf && *p != '/');
2198bb1fe5dSsjg 	}
2209c9a1981Srillig 	if (*p == '/')
2219c9a1981Srillig 	    memmove(p, p2, strlen(p2) + 1);
2229c9a1981Srillig 	else
2238bb1fe5dSsjg 	    return;		/* can't happen? */
2248bb1fe5dSsjg     }
2258bb1fe5dSsjg }
2268bb1fe5dSsjg 
2274db43f7eSsjg static char *
meta_name(char * mname,size_t mnamelen,const char * dname,const char * tname,const char * cwd)22837bfc650Srillig meta_name(char *mname, size_t mnamelen,
2294db43f7eSsjg 	  const char *dname,
2302d086608Ssjg 	  const char *tname,
2312d086608Ssjg 	  const char *cwd)
2324db43f7eSsjg {
2334db43f7eSsjg     char buf[MAXPATHLEN];
234b0baaa75Srillig     char *rp, *cp;
235b0baaa75Srillig     const char *tname_base;
2364db43f7eSsjg     char *tp;
237f2001301Ssjg     char *dtp;
238f2001301Ssjg     size_t ldname;
2398bb1fe5dSsjg 
2404db43f7eSsjg     /*
2414db43f7eSsjg      * Weed out relative paths from the target file name.
2424db43f7eSsjg      * We have to be careful though since if target is a
2434db43f7eSsjg      * symlink, the result will be unstable.
2444db43f7eSsjg      * So we use realpath() just to get the dirname, and leave the
2454db43f7eSsjg      * basename as given to us.
2464db43f7eSsjg      */
247b0baaa75Srillig     if ((tname_base = strrchr(tname, '/')) != NULL) {
24871c58e78Srillig 	if (cached_realpath(tname, buf) != NULL) {
24971c58e78Srillig 	    if ((rp = strrchr(buf, '/')) != NULL) {
2504db43f7eSsjg 		rp++;
251b0baaa75Srillig 		tname_base++;
252b0baaa75Srillig 		if (strcmp(tname_base, rp) != 0)
253b0baaa75Srillig 		    strlcpy(rp, tname_base, sizeof buf - (size_t)(rp - buf));
2544db43f7eSsjg 	    }
2554db43f7eSsjg 	    tname = buf;
2568bb1fe5dSsjg 	} else {
2578bb1fe5dSsjg 	    /*
2588bb1fe5dSsjg 	     * We likely have a directory which is about to be made.
2598bb1fe5dSsjg 	     * We pretend realpath() succeeded, to have a chance
2608bb1fe5dSsjg 	     * of generating the same meta file name that we will
2618bb1fe5dSsjg 	     * next time through.
2628bb1fe5dSsjg 	     */
2638bb1fe5dSsjg 	    if (tname[0] == '/') {
264143a3267Srillig 		strlcpy(buf, tname, sizeof buf);
2658bb1fe5dSsjg 	    } else {
266143a3267Srillig 		snprintf(buf, sizeof buf, "%s/%s", cwd, tname);
2678bb1fe5dSsjg 	    }
2689c9a1981Srillig 	    eat_dots(buf);
2698bb1fe5dSsjg 	    tname = buf;
2704db43f7eSsjg 	}
2714db43f7eSsjg     }
2724db43f7eSsjg     /* on some systems dirname may modify its arg */
2734db43f7eSsjg     tp = bmake_strdup(tname);
274f2001301Ssjg     dtp = dirname(tp);
2759cebc24eSsjg     if (strcmp(dname, dtp) == 0) {
2769cebc24eSsjg 	if (snprintf(mname, mnamelen, "%s.meta", tname) >= (int)mnamelen)
2779cebc24eSsjg 	    mname[mnamelen - 1] = '\0';
2789cebc24eSsjg     } else {
2799cebc24eSsjg 	int x;
2809cebc24eSsjg 
281f2001301Ssjg 	ldname = strlen(dname);
282f2001301Ssjg 	if (strncmp(dname, dtp, ldname) == 0 && dtp[ldname] == '/')
2839cebc24eSsjg 	    x = snprintf(mname, mnamelen, "%s/%s.meta", dname, &tname[ldname+1]);
284f2001301Ssjg 	else
2859cebc24eSsjg 	    x = snprintf(mname, mnamelen, "%s/%s.meta", dname, tname);
2869cebc24eSsjg 	if (x >= (int)mnamelen)
2879cebc24eSsjg 	    mname[mnamelen - 1] = '\0';
2884db43f7eSsjg 	/*
2894db43f7eSsjg 	 * Replace path separators in the file name after the
2904db43f7eSsjg 	 * current object directory path.
2914db43f7eSsjg 	 */
2924db43f7eSsjg 	cp = mname + strlen(dname) + 1;
2934db43f7eSsjg 
2944db43f7eSsjg 	while (*cp != '\0') {
2954db43f7eSsjg 	    if (*cp == '/')
2964db43f7eSsjg 		*cp = '_';
2974db43f7eSsjg 	    cp++;
2984db43f7eSsjg 	}
2994db43f7eSsjg     }
3004db43f7eSsjg     free(tp);
30144d841d2Srillig     return mname;
3024db43f7eSsjg }
3034db43f7eSsjg 
3044db43f7eSsjg /*
3054db43f7eSsjg  * Return true if running ${.MAKE}
3064db43f7eSsjg  * Bypassed if target is flagged .MAKE
3074db43f7eSsjg  */
308244fd9f4Srillig static bool
is_submake(const char * cmd,GNode * gn)309a41e5257Srillig is_submake(const char *cmd, GNode *gn)
3104db43f7eSsjg {
311ab2bfed6Srillig     static const char *p_make = NULL;
3125658125dSrillig     static size_t p_len;
3134db43f7eSsjg     char *mp = NULL;
314bbf0adc9Srillig     const char *cp2;
315244fd9f4Srillig     bool rc = false;
3164db43f7eSsjg 
3172861b54eSrillig     if (p_make == NULL) {
318835b4db4Srillig 	p_make = Var_Value(gn, ".MAKE").str;
3194db43f7eSsjg 	p_len = strlen(p_make);
3204db43f7eSsjg     }
321bbf0adc9Srillig     if (strchr(cmd, '$') != NULL) {
322*0c336a8cSrillig 	mp = Var_Subst(cmd, gn, VARE_EVAL);
323e366c998Srillig 	/* TODO: handle errors */
3244db43f7eSsjg 	cmd = mp;
3254db43f7eSsjg     }
3264db43f7eSsjg     cp2 = strstr(cmd, p_make);
327a0c92525Srillig     if (cp2 != NULL) {
3284db43f7eSsjg 	switch (cp2[p_len]) {
3294db43f7eSsjg 	case '\0':
3304db43f7eSsjg 	case ' ':
3314db43f7eSsjg 	case '\t':
3324db43f7eSsjg 	case '\n':
333244fd9f4Srillig 	    rc = true;
3344db43f7eSsjg 	    break;
3354db43f7eSsjg 	}
336a41e5257Srillig 	if (cp2 > cmd && rc) {
3374db43f7eSsjg 	    switch (cp2[-1]) {
3384db43f7eSsjg 	    case ' ':
3394db43f7eSsjg 	    case '\t':
3404db43f7eSsjg 	    case '\n':
3414db43f7eSsjg 		break;
3424db43f7eSsjg 	    default:
343244fd9f4Srillig 		rc = false;		/* no match */
3444db43f7eSsjg 		break;
3454db43f7eSsjg 	    }
3464db43f7eSsjg 	}
3474db43f7eSsjg     }
3484db43f7eSsjg     free(mp);
34944d841d2Srillig     return rc;
3504db43f7eSsjg }
3514db43f7eSsjg 
352244fd9f4Srillig static bool
any_is_submake(GNode * gn)353a41e5257Srillig any_is_submake(GNode *gn)
354a41e5257Srillig {
355a41e5257Srillig     StringListNode *ln;
356a41e5257Srillig 
357e93d02d9Srillig     for (ln = gn->commands.first; ln != NULL; ln = ln->next)
358a41e5257Srillig 	if (is_submake(ln->datum, gn))
359244fd9f4Srillig 	    return true;
360244fd9f4Srillig     return false;
361a41e5257Srillig }
362a41e5257Srillig 
3636d249710Srillig static void
printCMD(const char * ucmd,FILE * fp,GNode * gn)36449c1680dSrillig printCMD(const char *ucmd, FILE *fp, GNode *gn)
3654db43f7eSsjg {
36649c1680dSrillig     FStr xcmd = FStr_InitRefer(ucmd);
3674db43f7eSsjg 
368*0c336a8cSrillig     Var_Expand(&xcmd, gn, VARE_EVAL);
36949c1680dSrillig     fprintf(fp, "CMD %s\n", xcmd.str);
37049c1680dSrillig     FStr_Done(&xcmd);
3714db43f7eSsjg }
3724db43f7eSsjg 
373c679cfbcSrillig static void
printCMDs(GNode * gn,FILE * fp)374357a62a6Srillig printCMDs(GNode *gn, FILE *fp)
375c679cfbcSrillig {
3764b4d5670Srillig     StringListNode *ln;
377c679cfbcSrillig 
378e93d02d9Srillig     for (ln = gn->commands.first; ln != NULL; ln = ln->next)
379357a62a6Srillig 	printCMD(ln->datum, fp, gn);
380c679cfbcSrillig }
381c679cfbcSrillig 
3824db43f7eSsjg /*
3834db43f7eSsjg  * Certain node types never get a .meta file
3844db43f7eSsjg  */
385588ed71aSrillig #define SKIP_META_TYPE(flag, str) do { \
386588ed71aSrillig     if ((gn->type & (flag))) { \
387588ed71aSrillig 	if (verbose) \
388588ed71aSrillig 	    debug_printf("Skipping meta for %s: .%s\n", gn->name, str); \
389244fd9f4Srillig 	return false; \
3904db43f7eSsjg     } \
3919349f183Srillig } while (false)
3924db43f7eSsjg 
3932d086608Ssjg 
3942d086608Ssjg /*
3952d086608Ssjg  * Do we need/want a .meta file ?
3962d086608Ssjg  */
397244fd9f4Srillig static bool
meta_needed(GNode * gn,const char * dname,char * objdir_realpath,bool verbose)39856cb5728Srillig meta_needed(GNode *gn, const char *dname,
399244fd9f4Srillig 	    char *objdir_realpath, bool verbose)
4002d086608Ssjg {
40133ac710bSrillig     struct cached_stat cst;
4022d086608Ssjg 
4032d086608Ssjg     if (verbose)
40482632c2fSrillig 	verbose = DEBUG(META);
4052d086608Ssjg 
4062d086608Ssjg     /* This may be a phony node which we don't want meta data for... */
4072d086608Ssjg     /* Skip .meta for .BEGIN, .END, .ERROR etc as well. */
4082d086608Ssjg     /* Or it may be explicitly flagged as .NOMETA */
409588ed71aSrillig     SKIP_META_TYPE(OP_NOMETA, "NOMETA");
4102d086608Ssjg     /* Unless it is explicitly flagged as .META */
4112d086608Ssjg     if (!(gn->type & OP_META)) {
412588ed71aSrillig 	SKIP_META_TYPE(OP_PHONY, "PHONY");
413588ed71aSrillig 	SKIP_META_TYPE(OP_SPECIAL, "SPECIAL");
414588ed71aSrillig 	SKIP_META_TYPE(OP_MAKE, "MAKE");
4152d086608Ssjg     }
4162d086608Ssjg 
4172d086608Ssjg     /* Check if there are no commands to execute. */
418e93d02d9Srillig     if (Lst_IsEmpty(&gn->commands)) {
4192d086608Ssjg 	if (verbose)
42037111947Srillig 	    debug_printf("Skipping meta for %s: no commands\n", gn->name);
421244fd9f4Srillig 	return false;
4222d086608Ssjg     }
4232d086608Ssjg     if ((gn->type & (OP_META|OP_SUBMAKE)) == OP_SUBMAKE) {
4242d086608Ssjg 	/* OP_SUBMAKE is a bit too aggressive */
425a41e5257Srillig 	if (any_is_submake(gn)) {
426ae6e2b2cSrillig 	    DEBUG1(META, "Skipping meta for %s: .SUBMAKE\n", gn->name);
427244fd9f4Srillig 	    return false;
4282d086608Ssjg 	}
4292d086608Ssjg     }
4302d086608Ssjg 
4312d086608Ssjg     /* The object directory may not exist. Check it.. */
43233ac710bSrillig     if (cached_stat(dname, &cst) != 0) {
4332d086608Ssjg 	if (verbose)
43437111947Srillig 	    debug_printf("Skipping meta for %s: no .OBJDIR\n", gn->name);
435244fd9f4Srillig 	return false;
4362d086608Ssjg     }
4372d086608Ssjg 
4382d086608Ssjg     /* make sure these are canonical */
43971c58e78Srillig     if (cached_realpath(dname, objdir_realpath) != NULL)
4407fdaeb8cSrillig 	dname = objdir_realpath;
4412d086608Ssjg 
4422d086608Ssjg     /* If we aren't in the object directory, don't create a meta file. */
4432d086608Ssjg     if (!metaCurdirOk && strcmp(curdir, dname) == 0) {
4442d086608Ssjg 	if (verbose)
44537111947Srillig 	    debug_printf("Skipping meta for %s: .OBJDIR == .CURDIR\n",
4462d086608Ssjg 			 gn->name);
447244fd9f4Srillig 	return false;
4482d086608Ssjg     }
449244fd9f4Srillig     return true;
4502d086608Ssjg }
4512d086608Ssjg 
4522d086608Ssjg 
4534db43f7eSsjg static FILE *
meta_create(BuildMon * pbm,GNode * gn)4544db43f7eSsjg meta_create(BuildMon *pbm, GNode *gn)
4554db43f7eSsjg {
456357a62a6Srillig     FILE *fp;
4574db43f7eSsjg     char buf[MAXPATHLEN];
4587fdaeb8cSrillig     char objdir_realpath[MAXPATHLEN];
4594db43f7eSsjg     char **ptr;
46003074130Srillig     FStr dname;
4614db43f7eSsjg     const char *tname;
4624db43f7eSsjg     char *fname;
4634db43f7eSsjg     const char *cp;
4644db43f7eSsjg 
465357a62a6Srillig     fp = NULL;
4664db43f7eSsjg 
467835b4db4Srillig     dname = Var_Value(gn, ".OBJDIR");
468f015e31cSrillig     tname = GNode_VarTarget(gn);
4694db43f7eSsjg 
4707fdaeb8cSrillig     /* if this succeeds objdir_realpath is realpath of dname */
471244fd9f4Srillig     if (!meta_needed(gn, dname.str, objdir_realpath, true))
4724db43f7eSsjg 	goto out;
47303074130Srillig     dname.str = objdir_realpath;
4744db43f7eSsjg 
4754db43f7eSsjg     if (metaVerbose) {
4764db43f7eSsjg 	/* Describe the target we are building */
477*0c336a8cSrillig 	char *mp = Var_Subst("${" MAKE_META_PREFIX "}", gn, VARE_EVAL);
478e366c998Srillig 	/* TODO: handle errors */
47969f9166fSrillig 	if (mp[0] != '\0')
4804db43f7eSsjg 	    fprintf(stdout, "%s\n", mp);
4814db43f7eSsjg 	free(mp);
4824db43f7eSsjg     }
4834db43f7eSsjg     /* Get the basename of the target */
4847f604d47Srillig     cp = str_basename(tname);
4854db43f7eSsjg 
4864db43f7eSsjg     fflush(stdout);
4874db43f7eSsjg 
4884db43f7eSsjg     if (!writeMeta)
4894db43f7eSsjg 	/* Don't create meta data. */
4904db43f7eSsjg 	goto out;
4914db43f7eSsjg 
492143a3267Srillig     fname = meta_name(pbm->meta_fname, sizeof pbm->meta_fname,
49303074130Srillig 		      dname.str, tname, objdir_realpath);
4944db43f7eSsjg 
4958bb1fe5dSsjg #ifdef DEBUG_META_MODE
496ae6e2b2cSrillig     DEBUG1(META, "meta_create: %s\n", fname);
4978bb1fe5dSsjg #endif
4988bb1fe5dSsjg 
499357a62a6Srillig     if ((fp = fopen(fname, "w")) == NULL)
5007cd58762Ssjg 	Punt("Could not open meta file '%s': %s", fname, strerror(errno));
5014db43f7eSsjg 
502357a62a6Srillig     fprintf(fp, "# Meta data file %s\n", fname);
5034db43f7eSsjg 
504357a62a6Srillig     printCMDs(gn, fp);
5054db43f7eSsjg 
506357a62a6Srillig     fprintf(fp, "CWD %s\n", getcwd(buf, sizeof buf));
507357a62a6Srillig     fprintf(fp, "TARGET %s\n", tname);
508f015e31cSrillig     cp = GNode_VarOodate(gn);
50931940a95Srillig     if (cp != NULL && *cp != '\0') {
510357a62a6Srillig 	fprintf(fp, "OODATE %s\n", cp);
511438d096bSsjg     }
5124db43f7eSsjg     if (metaEnv) {
5134db43f7eSsjg 	for (ptr = environ; *ptr != NULL; ptr++)
514357a62a6Srillig 	    fprintf(fp, "ENV %s\n", *ptr);
5154db43f7eSsjg     }
5164db43f7eSsjg 
517357a62a6Srillig     fprintf(fp, "-- command output --\n");
5187cd58762Ssjg     if (fflush(fp) != 0)
5197cd58762Ssjg 	Punt("Cannot write expanded command to meta file: %s",
5207cd58762Ssjg 	    strerror(errno));
5214db43f7eSsjg 
522a1bc5ad8Srillig     Global_Append(".MAKE.META.FILES", fname);
523a1bc5ad8Srillig     Global_Append(".MAKE.META.CREATED", fname);
5244db43f7eSsjg 
52509fee58eSsjg     gn->type |= OP_META;		/* in case anyone wants to know */
52609fee58eSsjg     if (metaSilent) {
52709fee58eSsjg 	    gn->type |= OP_SILENT;
52809fee58eSsjg     }
5294db43f7eSsjg  out:
53003074130Srillig     FStr_Done(&dname);
5314db43f7eSsjg 
532357a62a6Srillig     return fp;
5334db43f7eSsjg }
5344db43f7eSsjg 
535244fd9f4Srillig static bool
boolValue(const char * s)536b0baaa75Srillig boolValue(const char *s)
537dd0a10eeSsjg {
538dd0a10eeSsjg     switch (*s) {
539dd0a10eeSsjg     case '0':
540dd0a10eeSsjg     case 'N':
541dd0a10eeSsjg     case 'n':
542dd0a10eeSsjg     case 'F':
543dd0a10eeSsjg     case 'f':
544244fd9f4Srillig 	return false;
545dd0a10eeSsjg     }
546244fd9f4Srillig     return true;
547dd0a10eeSsjg }
5484db43f7eSsjg 
54903ce90ebSsjg /*
55003ce90ebSsjg  * Initialization we need before reading makefiles.
55103ce90ebSsjg  */
5524db43f7eSsjg void
meta_init(void)5535410a37eSsjg meta_init(void)
55403ce90ebSsjg {
5552a5d072bSmaxv #ifdef USE_FILEMON
5562a5d072bSmaxv 	/* this allows makefiles to test if we have filemon support */
55787686000Srillig 	Global_Set(".MAKE.PATH_FILEMON", filemon_path());
5582a5d072bSmaxv #endif
55903ce90ebSsjg }
56003ce90ebSsjg 
56103ce90ebSsjg 
5622d086608Ssjg #define get_mode_bf(bf, token) \
56371c58e78Srillig     if ((cp = strstr(make_mode, token)) != NULL) \
564143a3267Srillig 	bf = boolValue(cp + sizeof (token) - 1)
5652d086608Ssjg 
56603ce90ebSsjg /*
56703ce90ebSsjg  * Initialization we need after reading makefiles.
56803ce90ebSsjg  */
56903ce90ebSsjg void
meta_mode_init(const char * make_mode)57003ce90ebSsjg meta_mode_init(const char *make_mode)
5714db43f7eSsjg {
572244fd9f4Srillig     static bool once = false;
573b0baaa75Srillig     const char *cp;
5744db43f7eSsjg 
575244fd9f4Srillig     useMeta = true;
576244fd9f4Srillig     useFilemon = true;
577244fd9f4Srillig     writeMeta = true;
5784db43f7eSsjg 
5794adbce36Srillig     if (make_mode != NULL) {
58071c58e78Srillig 	if (strstr(make_mode, "env") != NULL)
581244fd9f4Srillig 	    metaEnv = true;
58271c58e78Srillig 	if (strstr(make_mode, "verb") != NULL)
583244fd9f4Srillig 	    metaVerbose = true;
58471c58e78Srillig 	if (strstr(make_mode, "read") != NULL)
585244fd9f4Srillig 	    writeMeta = false;
58671c58e78Srillig 	if (strstr(make_mode, "nofilemon") != NULL)
587244fd9f4Srillig 	    useFilemon = false;
58871c58e78Srillig 	if (strstr(make_mode, "ignore-cmd") != NULL)
589244fd9f4Srillig 	    metaIgnoreCMDs = true;
5902d086608Ssjg 	if (useFilemon)
5912d086608Ssjg 	    get_mode_bf(filemonMissing, "missing-filemon=");
5922d086608Ssjg 	get_mode_bf(metaCurdirOk, "curdirok=");
5932d086608Ssjg 	get_mode_bf(metaMissing, "missing-meta=");
5942d086608Ssjg 	get_mode_bf(metaSilent, "silent=");
5954db43f7eSsjg     }
596835b4db4Srillig     if (metaVerbose && !Var_Exists(SCOPE_GLOBAL, MAKE_META_PREFIX)) {
5974db43f7eSsjg 	/*
5984db43f7eSsjg 	 * The default value for MAKE_META_PREFIX
5994db43f7eSsjg 	 * prints the absolute path of the target.
6004db43f7eSsjg 	 * This works be cause :H will generate '.' if there is no /
6014db43f7eSsjg 	 * and :tA will resolve that to cwd.
6024db43f7eSsjg 	 */
60387686000Srillig 	Global_Set(MAKE_META_PREFIX,
6043ec10f8dSrillig 	    "Building ${.TARGET:H:tA}/${.TARGET:T}");
6054db43f7eSsjg     }
6064db43f7eSsjg     if (once)
6074db43f7eSsjg 	return;
608244fd9f4Srillig     once = true;
609143a3267Srillig     memset(&Mybm, 0, sizeof Mybm);
61048587910Ssjg     /*
61148587910Ssjg      * We consider ourselves master of all within ${.MAKE.META.BAILIWICK}
61248587910Ssjg      */
613a4cbe1d7Srillig     metaBailiwickStr = Var_Subst("${.MAKE.META.BAILIWICK:O:u:tA}",
614*0c336a8cSrillig 				 SCOPE_GLOBAL, VARE_EVAL);
615e366c998Srillig     /* TODO: handle errors */
6169bb158b5Srillig     AppendWords(&metaBailiwick, metaBailiwickStr);
617adfd644bSsjg     /*
618adfd644bSsjg      * We ignore any paths that start with ${.MAKE.META.IGNORE_PATHS}
619adfd644bSsjg      */
620a1bc5ad8Srillig     Global_Append(MAKE_META_IGNORE_PATHS,
6213ec10f8dSrillig 	       "/dev /etc /proc /tmp /var/run /var/tmp ${TMPDIR}");
622a4cbe1d7Srillig     metaIgnorePathsStr = Var_Subst("${" MAKE_META_IGNORE_PATHS ":O:u:tA}",
623*0c336a8cSrillig 				   SCOPE_GLOBAL, VARE_EVAL);
624e366c998Srillig     /* TODO: handle errors */
6259bb158b5Srillig     AppendWords(&metaIgnorePaths, metaIgnorePathsStr);
6261ea1c693Ssjg 
6271ea1c693Ssjg     /*
6281ea1c693Ssjg      * We ignore any paths that match ${.MAKE.META.IGNORE_PATTERNS}
6291ea1c693Ssjg      */
6309c1d64c2Srillig     metaIgnorePatterns = Var_Exists(SCOPE_GLOBAL, MAKE_META_IGNORE_PATTERNS);
6319c1d64c2Srillig     metaIgnoreFilter = Var_Exists(SCOPE_GLOBAL, MAKE_META_IGNORE_FILTER);
6329c1d64c2Srillig     metaCmpFilter = Var_Exists(SCOPE_GLOBAL, MAKE_META_CMP_FILTER);
6334db43f7eSsjg }
6344db43f7eSsjg 
63518e5949aSsjg MAKE_INLINE BuildMon *
BM(Job * job)63618e5949aSsjg BM(Job *job)
63718e5949aSsjg {
63818e5949aSsjg 
63918e5949aSsjg 	return ((job != NULL) ? &job->bm : &Mybm);
64018e5949aSsjg }
64118e5949aSsjg 
6424db43f7eSsjg /*
6434db43f7eSsjg  * In each case below we allow for job==NULL
6444db43f7eSsjg  */
6454db43f7eSsjg void
meta_job_start(Job * job,GNode * gn)6464db43f7eSsjg meta_job_start(Job *job, GNode *gn)
6474db43f7eSsjg {
6484db43f7eSsjg     BuildMon *pbm;
6494db43f7eSsjg 
6506f652502Ssjg     pbm = BM(job);
6514db43f7eSsjg     pbm->mfp = meta_create(pbm, gn);
6522a5d072bSmaxv #ifdef USE_FILEMON_ONCE
6532a5d072bSmaxv     /* compat mode we open the filemon dev once per command */
6542a5d072bSmaxv     if (job == NULL)
6552a5d072bSmaxv 	return;
6562a5d072bSmaxv #endif
6572a5d072bSmaxv #ifdef USE_FILEMON
6582a5d072bSmaxv     if (pbm->mfp != NULL && useFilemon) {
6591378959eSriastradh 	meta_open_filemon(pbm);
6602a5d072bSmaxv     } else {
6611378959eSriastradh 	pbm->mon_fd = -1;
6621378959eSriastradh 	pbm->filemon = NULL;
6632a5d072bSmaxv     }
6642a5d072bSmaxv #endif
6654db43f7eSsjg }
6664db43f7eSsjg 
6674db43f7eSsjg /*
6684db43f7eSsjg  * The child calls this before doing anything.
6694db43f7eSsjg  * It does not disturb our state.
6704db43f7eSsjg  */
6714db43f7eSsjg void
meta_job_child(Job * job MAKE_ATTR_UNUSED)672e56a2ae1Ssjg meta_job_child(Job *job MAKE_ATTR_UNUSED)
6734db43f7eSsjg {
6742a5d072bSmaxv #ifdef USE_FILEMON
6752a5d072bSmaxv     BuildMon *pbm;
6762a5d072bSmaxv 
6776f652502Ssjg     pbm = BM(job);
6782a5d072bSmaxv     if (pbm->mfp != NULL) {
6792a5d072bSmaxv 	close(fileno(pbm->mfp));
68031940a95Srillig 	if (useFilemon && pbm->filemon != NULL) {
6812a5d072bSmaxv 	    pid_t pid;
6822a5d072bSmaxv 
6832a5d072bSmaxv 	    pid = getpid();
6841378959eSriastradh 	    if (filemon_setpid_child(pbm->filemon, pid) == -1) {
6857cd58762Ssjg 		Punt("Could not set filemon pid: %s", strerror(errno));
6862a5d072bSmaxv 	    }
6872a5d072bSmaxv 	}
6882a5d072bSmaxv     }
6892a5d072bSmaxv #endif
6904db43f7eSsjg }
6914db43f7eSsjg 
6924db43f7eSsjg void
meta_job_parent(Job * job MAKE_ATTR_UNUSED,pid_t pid MAKE_ATTR_UNUSED)693e56a2ae1Ssjg meta_job_parent(Job *job MAKE_ATTR_UNUSED, pid_t pid MAKE_ATTR_UNUSED)
6941378959eSriastradh {
695ee757927Ssjg #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
6961378959eSriastradh     BuildMon *pbm;
6971378959eSriastradh 
6986f652502Ssjg     pbm = BM(job);
69931940a95Srillig     if (useFilemon && pbm->filemon != NULL) {
7001378959eSriastradh 	filemon_setpid_parent(pbm->filemon, pid);
7011378959eSriastradh     }
7021378959eSriastradh #endif
7031378959eSriastradh }
7041378959eSriastradh 
7051378959eSriastradh int
meta_job_fd(Job * job MAKE_ATTR_UNUSED)706e56a2ae1Ssjg meta_job_fd(Job *job MAKE_ATTR_UNUSED)
7071378959eSriastradh {
708ee757927Ssjg #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
7091378959eSriastradh     BuildMon *pbm;
7101378959eSriastradh 
7116f652502Ssjg     pbm = BM(job);
71231940a95Srillig     if (useFilemon && pbm->filemon != NULL) {
7131378959eSriastradh 	return filemon_readfd(pbm->filemon);
7141378959eSriastradh     }
7151378959eSriastradh #endif
7161378959eSriastradh     return -1;
7171378959eSriastradh }
7181378959eSriastradh 
7191378959eSriastradh int
meta_job_event(Job * job MAKE_ATTR_UNUSED)720e56a2ae1Ssjg meta_job_event(Job *job MAKE_ATTR_UNUSED)
7211378959eSriastradh {
722ee757927Ssjg #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
7231378959eSriastradh     BuildMon *pbm;
7241378959eSriastradh 
7256f652502Ssjg     pbm = BM(job);
72631940a95Srillig     if (useFilemon && pbm->filemon != NULL) {
7271378959eSriastradh 	return filemon_process(pbm->filemon);
7281378959eSriastradh     }
7291378959eSriastradh #endif
7301378959eSriastradh     return 0;
7311378959eSriastradh }
7321378959eSriastradh 
7331378959eSriastradh void
meta_job_error(Job * job,GNode * gn,bool ignerr,int status)734244fd9f4Srillig meta_job_error(Job *job, GNode *gn, bool ignerr, int status)
7354db43f7eSsjg {
7364db43f7eSsjg     char cwd[MAXPATHLEN];
7374db43f7eSsjg     BuildMon *pbm;
7384db43f7eSsjg 
7396f652502Ssjg     pbm = BM(job);
7406f652502Ssjg     if (job != NULL && gn == NULL)
7414db43f7eSsjg 	    gn = job->node;
7424db43f7eSsjg     if (pbm->mfp != NULL) {
7433dd087f3Ssjg 	fprintf(pbm->mfp, "\n*** Error code %d%s\n",
744c2147f48Srillig 		status, ignerr ? "(ignored)" : "");
7454db43f7eSsjg     }
7463ec10f8dSrillig     if (gn != NULL)
74787686000Srillig 	Global_Set(".ERROR_TARGET", GNode_Path(gn));
7489cebc24eSsjg     if (getcwd(cwd, sizeof cwd) == NULL)
7499cebc24eSsjg 	Punt("Cannot get cwd: %s", strerror(errno));
7509cebc24eSsjg 
75187686000Srillig     Global_Set(".ERROR_CWD", cwd);
752a0c92525Srillig     if (pbm->meta_fname[0] != '\0') {
75387686000Srillig 	Global_Set(".ERROR_META_FILE", pbm->meta_fname);
7544db43f7eSsjg     }
755c76b9c11Ssjg     meta_job_finish(job);
7564db43f7eSsjg }
7574db43f7eSsjg 
7584db43f7eSsjg void
meta_job_output(Job * job,char * cp,const char * nl)7594db43f7eSsjg meta_job_output(Job *job, char *cp, const char *nl)
7604db43f7eSsjg {
7614db43f7eSsjg     BuildMon *pbm;
7624db43f7eSsjg 
7636f652502Ssjg     pbm = BM(job);
7644db43f7eSsjg     if (pbm->mfp != NULL) {
7654db43f7eSsjg 	if (metaVerbose) {
7664db43f7eSsjg 	    static char *meta_prefix = NULL;
7675658125dSrillig 	    static size_t meta_prefix_len;
7684db43f7eSsjg 
7692861b54eSrillig 	    if (meta_prefix == NULL) {
7704db43f7eSsjg 		char *cp2;
7714db43f7eSsjg 
772a4cbe1d7Srillig 		meta_prefix = Var_Subst("${" MAKE_META_PREFIX "}",
773*0c336a8cSrillig 					SCOPE_GLOBAL, VARE_EVAL);
774e366c998Srillig 		/* TODO: handle errors */
77571c58e78Srillig 		if ((cp2 = strchr(meta_prefix, '$')) != NULL)
7765658125dSrillig 		    meta_prefix_len = (size_t)(cp2 - meta_prefix);
7774db43f7eSsjg 		else
7784db43f7eSsjg 		    meta_prefix_len = strlen(meta_prefix);
7794db43f7eSsjg 	    }
7804db43f7eSsjg 	    if (strncmp(cp, meta_prefix, meta_prefix_len) == 0) {
7814db43f7eSsjg 		cp = strchr(cp + 1, '\n');
78294f06ed7Srillig 		if (cp == NULL)
7834db43f7eSsjg 		    return;
78494f06ed7Srillig 		cp++;
7854db43f7eSsjg 	    }
7864db43f7eSsjg 	}
7874db43f7eSsjg 	fprintf(pbm->mfp, "%s%s", cp, nl);
7884db43f7eSsjg     }
7894db43f7eSsjg }
7904db43f7eSsjg 
791ccfdc947Ssjg int
meta_cmd_finish(void * pbmp)7924db43f7eSsjg meta_cmd_finish(void *pbmp)
7934db43f7eSsjg {
794ccfdc947Ssjg     int error = 0;
7954db43f7eSsjg     BuildMon *pbm = pbmp;
7962a5d072bSmaxv #ifdef USE_FILEMON
7972a5d072bSmaxv     int x;
7982a5d072bSmaxv #endif
7994db43f7eSsjg 
800a0c92525Srillig     if (pbm == NULL)
8014db43f7eSsjg 	pbm = &Mybm;
8024db43f7eSsjg 
8032a5d072bSmaxv #ifdef USE_FILEMON
80471c58e78Srillig     if (pbm->filemon != NULL) {
8051378959eSriastradh 	while (filemon_process(pbm->filemon) > 0)
8061378959eSriastradh 	    continue;
8077cd58762Ssjg 	if (filemon_close(pbm->filemon) == -1) {
8082a5d072bSmaxv 	    error = errno;
8097cd58762Ssjg 	    Punt("filemon failed: %s", strerror(errno));
8107cd58762Ssjg 	}
8112a5d072bSmaxv 	x = filemon_read(pbm->mfp, pbm->mon_fd);
8122a5d072bSmaxv 	if (error == 0 && x != 0)
8132a5d072bSmaxv 	    error = x;
8141378959eSriastradh 	pbm->mon_fd = -1;
8151378959eSriastradh 	pbm->filemon = NULL;
816f52baef8Srillig 	return error;
817f52baef8Srillig     }
8182a5d072bSmaxv #endif
819f52baef8Srillig 
8203dd087f3Ssjg     fprintf(pbm->mfp, "\n");	/* ensure end with newline */
821ccfdc947Ssjg     return error;
8224db43f7eSsjg }
8234db43f7eSsjg 
824ccfdc947Ssjg int
meta_job_finish(Job * job)8254db43f7eSsjg meta_job_finish(Job *job)
8264db43f7eSsjg {
8274db43f7eSsjg     BuildMon *pbm;
828ccfdc947Ssjg     int error = 0;
829ccfdc947Ssjg     int x;
8304db43f7eSsjg 
8316f652502Ssjg     pbm = BM(job);
8324db43f7eSsjg     if (pbm->mfp != NULL) {
833ccfdc947Ssjg 	error = meta_cmd_finish(pbm);
834ccfdc947Ssjg 	x = fclose(pbm->mfp);
835ccfdc947Ssjg 	if (error == 0 && x != 0)
836ccfdc947Ssjg 	    error = errno;
8374db43f7eSsjg 	pbm->mfp = NULL;
838b3f04f73Ssjg 	pbm->meta_fname[0] = '\0';
8394db43f7eSsjg     }
840ccfdc947Ssjg     return error;
8414db43f7eSsjg }
8424db43f7eSsjg 
843e46a288cSchristos void
meta_finish(void)844e46a288cSchristos meta_finish(void)
845e46a288cSchristos {
846733418ceSrillig     Lst_Done(&metaBailiwick);
847e46a288cSchristos     free(metaBailiwickStr);
848733418ceSrillig     Lst_Done(&metaIgnorePaths);
849e46a288cSchristos     free(metaIgnorePathsStr);
850e46a288cSchristos }
851e46a288cSchristos 
8524db43f7eSsjg /*
8534db43f7eSsjg  * Fetch a full line from fp - growing bufp if needed
8544db43f7eSsjg  * Return length in bufp.
8554db43f7eSsjg  */
8564db43f7eSsjg static int
fgetLine(char ** bufp,size_t * szp,int o,FILE * fp)8574db43f7eSsjg fgetLine(char **bufp, size_t *szp, int o, FILE *fp)
8584db43f7eSsjg {
8594db43f7eSsjg     char *buf = *bufp;
8604db43f7eSsjg     size_t bufsz = *szp;
8614db43f7eSsjg     struct stat fs;
8624db43f7eSsjg     int x;
8634db43f7eSsjg 
8645658125dSrillig     if (fgets(&buf[o], (int)bufsz - o, fp) != NULL) {
8654db43f7eSsjg     check_newline:
8665658125dSrillig 	x = o + (int)strlen(&buf[o]);
8674db43f7eSsjg 	if (buf[x - 1] == '\n')
8684db43f7eSsjg 	    return x;
8694db43f7eSsjg 	/*
8704db43f7eSsjg 	 * We need to grow the buffer.
8714db43f7eSsjg 	 * The meta file can give us a clue.
8724db43f7eSsjg 	 */
8734db43f7eSsjg 	if (fstat(fileno(fp), &fs) == 0) {
8744db43f7eSsjg 	    size_t newsz;
8754db43f7eSsjg 	    char *p;
8764db43f7eSsjg 
8775658125dSrillig 	    newsz = ROUNDUP(((size_t)fs.st_size / 2), BUFSIZ);
8784db43f7eSsjg 	    if (newsz <= bufsz)
8795658125dSrillig 		newsz = ROUNDUP((size_t)fs.st_size, BUFSIZ);
8803dd087f3Ssjg 	    if (newsz <= bufsz)
8813dd087f3Ssjg 		return x;		/* truncated */
882ed716649Srillig 	    DEBUG2(META, "growing buffer %u -> %u\n",
883ed716649Srillig 		   (unsigned)bufsz, (unsigned)newsz);
8844db43f7eSsjg 	    p = bmake_realloc(buf, newsz);
8854db43f7eSsjg 	    *bufp = buf = p;
8864db43f7eSsjg 	    *szp = bufsz = newsz;
8874db43f7eSsjg 	    /* fetch the rest */
8882861b54eSrillig 	    if (fgets(&buf[x], (int)bufsz - x, fp) == NULL)
8894db43f7eSsjg 		return x;		/* truncated! */
8904db43f7eSsjg 	    goto check_newline;
8914db43f7eSsjg 	}
8924db43f7eSsjg     }
8934db43f7eSsjg     return 0;
8944db43f7eSsjg }
8954db43f7eSsjg 
896244fd9f4Srillig static bool
prefix_match(const char * prefix,const char * path)897a41e5257Srillig prefix_match(const char *prefix, const char *path)
89848587910Ssjg {
89948587910Ssjg     size_t n = strlen(prefix);
90048587910Ssjg 
90144d841d2Srillig     return strncmp(path, prefix, n) == 0;
90248587910Ssjg }
90348587910Ssjg 
904244fd9f4Srillig static bool
has_any_prefix(const char * path,StringList * prefixes)905a41e5257Srillig has_any_prefix(const char *path, StringList *prefixes)
906a41e5257Srillig {
907a41e5257Srillig     StringListNode *ln;
908a41e5257Srillig 
909a41e5257Srillig     for (ln = prefixes->first; ln != NULL; ln = ln->next)
910a41e5257Srillig 	if (prefix_match(ln->datum, path))
911244fd9f4Srillig 	    return true;
912244fd9f4Srillig     return false;
913a41e5257Srillig }
914a41e5257Srillig 
915db2e5ed4Srillig /* See if the path equals prefix or starts with "prefix/". */
916244fd9f4Srillig static bool
path_starts_with(const char * path,const char * prefix)9179c2db9feSrillig path_starts_with(const char *path, const char *prefix)
918c2d73114Ssjg {
919c2d73114Ssjg     size_t n = strlen(prefix);
920c2d73114Ssjg 
921db2e5ed4Srillig     if (strncmp(path, prefix, n) != 0)
922244fd9f4Srillig 	return false;
923db2e5ed4Srillig     return path[n] == '\0' || path[n] == '/';
924c2d73114Ssjg }
925c2d73114Ssjg 
926244fd9f4Srillig static bool
meta_ignore(GNode * gn,const char * p)9272e603a11Ssjg meta_ignore(GNode *gn, const char *p)
9282e603a11Ssjg {
9292e603a11Ssjg     char fname[MAXPATHLEN];
9302e603a11Ssjg 
9312e603a11Ssjg     if (p == NULL)
932244fd9f4Srillig 	return true;
9332e603a11Ssjg 
9342e603a11Ssjg     if (*p == '/') {
9355452dd36Ssjg 	/* first try the raw path "as is" */
9365452dd36Ssjg 	if (has_any_prefix(p, &metaIgnorePaths)) {
9375452dd36Ssjg #ifdef DEBUG_META_MODE
9385452dd36Ssjg 	    DEBUG1(META, "meta_oodate: ignoring path: %s\n", p);
9395452dd36Ssjg #endif
9405452dd36Ssjg 	    return true;
9415452dd36Ssjg 	}
9422e603a11Ssjg 	cached_realpath(p, fname); /* clean it up */
943733418ceSrillig 	if (has_any_prefix(fname, &metaIgnorePaths)) {
9442e603a11Ssjg #ifdef DEBUG_META_MODE
945ae6e2b2cSrillig 	    DEBUG1(META, "meta_oodate: ignoring path: %s\n", p);
9462e603a11Ssjg #endif
947244fd9f4Srillig 	    return true;
9482e603a11Ssjg 	}
9492e603a11Ssjg     }
9502e603a11Ssjg 
9512e603a11Ssjg     if (metaIgnorePatterns) {
95286abf69eSrillig 	const char *expr;
95386abf69eSrillig 	char *pm;
95486abf69eSrillig 
955835b4db4Srillig 	/*
956835b4db4Srillig 	 * XXX: This variable is set on a target GNode but is not one of
957835b4db4Srillig 	 * the usual local variables.  It should be deleted afterwards.
958835b4db4Srillig 	 * Ideally it would not be created in the first place, just like
959835b4db4Srillig 	 * in a .for loop.
960835b4db4Srillig 	 */
961835b4db4Srillig 	Var_Set(gn, ".p.", p);
96286abf69eSrillig 	expr = "${" MAKE_META_IGNORE_PATTERNS ":@m@${.p.:M$m}@}";
963*0c336a8cSrillig 	pm = Var_Subst(expr, gn, VARE_EVAL);
964e366c998Srillig 	/* TODO: handle errors */
96569f9166fSrillig 	if (pm[0] != '\0') {
9662e603a11Ssjg #ifdef DEBUG_META_MODE
967ae6e2b2cSrillig 	    DEBUG1(META, "meta_oodate: ignoring pattern: %s\n", p);
9682e603a11Ssjg #endif
9692e603a11Ssjg 	    free(pm);
970244fd9f4Srillig 	    return true;
9712e603a11Ssjg 	}
9722e603a11Ssjg 	free(pm);
9732e603a11Ssjg     }
9742e603a11Ssjg 
9752e603a11Ssjg     if (metaIgnoreFilter) {
9762e603a11Ssjg 	char *fm;
9772e603a11Ssjg 
9782e603a11Ssjg 	/* skip if filter result is empty */
979143a3267Srillig 	snprintf(fname, sizeof fname,
9802e603a11Ssjg 		 "${%s:L:${%s:ts:}}",
9812e603a11Ssjg 		 p, MAKE_META_IGNORE_FILTER);
982*0c336a8cSrillig 	fm = Var_Subst(fname, gn, VARE_EVAL);
983e366c998Srillig 	/* TODO: handle errors */
9842e603a11Ssjg 	if (*fm == '\0') {
9852e603a11Ssjg #ifdef DEBUG_META_MODE
986ae6e2b2cSrillig 	    DEBUG1(META, "meta_oodate: ignoring filtered: %s\n", p);
9872e603a11Ssjg #endif
9882e603a11Ssjg 	    free(fm);
989244fd9f4Srillig 	    return true;
9902e603a11Ssjg 	}
9912e603a11Ssjg 	free(fm);
9922e603a11Ssjg     }
993244fd9f4Srillig     return false;
9942e603a11Ssjg }
9952e603a11Ssjg 
9964db43f7eSsjg /*
9974db43f7eSsjg  * When running with 'meta' functionality, a target can be out-of-date
998f0a7346dSsnj  * if any of the references in its meta data file is more recent.
9992550dc93Ssjg  * We have to track the latestdir on a per-process basis.
10004db43f7eSsjg  */
1001545bddb1Ssjg #define LCWD_VNAME_FMT ".meta.%d.lcwd"
10022550dc93Ssjg #define LDIR_VNAME_FMT ".meta.%d.ldir"
10032550dc93Ssjg 
1004ebe0193fSsjg /*
1005ebe0193fSsjg  * It is possible that a .meta file is corrupted,
1006ebe0193fSsjg  * if we detect this we want to reproduce it.
1007244fd9f4Srillig  * Setting oodate true will have that effect.
1008ebe0193fSsjg  */
100931940a95Srillig #define CHECK_VALID_META(p) if (!(p != NULL && *p != '\0')) { \
101054add74bSrillig     warnx("%s: %u: malformed", fname, lineno); \
1011244fd9f4Srillig     oodate = true; \
1012ebe0193fSsjg     continue; \
1013ebe0193fSsjg     }
1014ebe0193fSsjg 
10155042f283Ssjg #define DEQUOTE(p) if (*p == '\'') {	\
10165042f283Ssjg     char *ep; \
10175042f283Ssjg     p++; \
101871c58e78Srillig     if ((ep = strchr(p, '\'')) != NULL) \
10195042f283Ssjg 	*ep = '\0'; \
10205042f283Ssjg     }
10215042f283Ssjg 
1022a455fbeeSrillig static void
append_if_new(StringList * list,const char * str)1023a455fbeeSrillig append_if_new(StringList *list, const char *str)
1024a455fbeeSrillig {
1025a455fbeeSrillig     StringListNode *ln;
1026a455fbeeSrillig 
1027a455fbeeSrillig     for (ln = list->first; ln != NULL; ln = ln->next)
1028a455fbeeSrillig 	if (strcmp(ln->datum, str) == 0)
1029a455fbeeSrillig 	    return;
1030a455fbeeSrillig     Lst_Append(list, bmake_strdup(str));
1031a455fbeeSrillig }
1032a455fbeeSrillig 
1033f153d1edSsjg /* A "reserved" variable to store the command to be filtered */
1034f153d1edSsjg #define META_CMD_FILTER_VAR ".MAKE.cmd_filtered"
1035f153d1edSsjg 
10363e72ec51Ssjg static char *
meta_filter_cmd(GNode * gn,char * s)1037f153d1edSsjg meta_filter_cmd(GNode *gn, char *s)
10383e72ec51Ssjg {
1039f153d1edSsjg     Var_Set(gn, META_CMD_FILTER_VAR, s);
1040a4cbe1d7Srillig     s = Var_Subst(
1041a4cbe1d7Srillig 	"${" META_CMD_FILTER_VAR ":${" MAKE_META_CMP_FILTER ":ts:}}",
1042*0c336a8cSrillig 	gn, VARE_EVAL);
10433e72ec51Ssjg     return s;
10443e72ec51Ssjg }
10453e72ec51Ssjg 
10463e72ec51Ssjg static int
meta_cmd_cmp(GNode * gn,char * a,char * b,bool filter)104730df8a48Ssjg meta_cmd_cmp(GNode *gn, char *a, char *b, bool filter)
10483e72ec51Ssjg {
10493e72ec51Ssjg     int rc;
10503e72ec51Ssjg 
10513e72ec51Ssjg     rc = strcmp(a, b);
105230df8a48Ssjg     if (rc == 0 || !filter)
10533e72ec51Ssjg 	return rc;
1054f153d1edSsjg     a = meta_filter_cmd(gn, a);
1055f153d1edSsjg     b = meta_filter_cmd(gn, b);
10563e72ec51Ssjg     rc = strcmp(a, b);
10573e72ec51Ssjg     free(a);
10583e72ec51Ssjg     free(b);
1059f153d1edSsjg     Var_Delete(gn, META_CMD_FILTER_VAR);
10603e72ec51Ssjg     return rc;
10613e72ec51Ssjg }
10623e72ec51Ssjg 
1063244fd9f4Srillig bool
meta_oodate(GNode * gn,bool oodate)1064244fd9f4Srillig meta_oodate(GNode *gn, bool oodate)
10654db43f7eSsjg {
10662550dc93Ssjg     static char *tmpdir = NULL;
1067c37859c2Ssjg     static char cwd[MAXPATHLEN];
1068545bddb1Ssjg     char lcwd_vname[64];
10692550dc93Ssjg     char ldir_vname[64];
1070545bddb1Ssjg     char lcwd[MAXPATHLEN];
10714db43f7eSsjg     char latestdir[MAXPATHLEN];
10724db43f7eSsjg     char fname[MAXPATHLEN];
10734db43f7eSsjg     char fname1[MAXPATHLEN];
10742550dc93Ssjg     char fname2[MAXPATHLEN];
1075545bddb1Ssjg     char fname3[MAXPATHLEN];
107603074130Srillig     FStr dname;
10772d086608Ssjg     const char *tname;
10784db43f7eSsjg     char *p;
10795042f283Ssjg     char *link_src;
10805042f283Ssjg     char *move_target;
1081c37859c2Ssjg     static size_t cwdlen = 0;
10828d31de31Ssjg     static size_t tmplen = 0;
10834db43f7eSsjg     FILE *fp;
1084244fd9f4Srillig     bool needOODATE = false;
108568212223Srillig     StringList missingFiles;
1086244fd9f4Srillig     bool have_filemon = false;
108730df8a48Ssjg     bool cmp_filter;
10884db43f7eSsjg 
10891b59e5d0Ssjg     if (oodate)
10901b59e5d0Ssjg 	return oodate;		/* we're done */
10911b59e5d0Ssjg 
1092835b4db4Srillig     dname = Var_Value(gn, ".OBJDIR");
1093f015e31cSrillig     tname = GNode_VarTarget(gn);
10942d086608Ssjg 
10952d086608Ssjg     /* if this succeeds fname3 is realpath of dname */
1096244fd9f4Srillig     if (!meta_needed(gn, dname.str, fname3, false))
10972d086608Ssjg 	goto oodate_out;
109803074130Srillig     dname.str = fname3;
10992d086608Ssjg 
110068212223Srillig     Lst_Init(&missingFiles);
110148587910Ssjg 
11024db43f7eSsjg     /*
11034db43f7eSsjg      * We need to check if the target is out-of-date. This includes
11044db43f7eSsjg      * checking if the expanded command has changed. This in turn
11054db43f7eSsjg      * requires that all variables are set in the same way that they
11064db43f7eSsjg      * would be if the target needs to be re-built.
11074db43f7eSsjg      */
1108730db1a0Srillig     GNode_SetLocalVars(gn);
11094db43f7eSsjg 
111003074130Srillig     meta_name(fname, sizeof fname, dname.str, tname, dname.str);
11114db43f7eSsjg 
11128bb1fe5dSsjg #ifdef DEBUG_META_MODE
1113ae6e2b2cSrillig     DEBUG1(META, "meta_oodate: %s\n", fname);
11148bb1fe5dSsjg #endif
11158bb1fe5dSsjg 
11164db43f7eSsjg     if ((fp = fopen(fname, "r")) != NULL) {
11174db43f7eSsjg 	static char *buf = NULL;
11184db43f7eSsjg 	static size_t bufsz;
111954add74bSrillig 	unsigned lineno = 0;
11202550dc93Ssjg 	int lastpid = 0;
11212550dc93Ssjg 	int pid;
11224db43f7eSsjg 	int x;
1123d5631b52Srillig 	StringListNode *cmdNode;
112433ac710bSrillig 	struct cached_stat cst;
11254db43f7eSsjg 
11262861b54eSrillig 	if (buf == NULL) {
11274db43f7eSsjg 	    bufsz = 8 * BUFSIZ;
11284db43f7eSsjg 	    buf = bmake_malloc(bufsz);
11294db43f7eSsjg 	}
11304db43f7eSsjg 
11312861b54eSrillig 	if (cwdlen == 0) {
1132143a3267Srillig 	    if (getcwd(cwd, sizeof cwd) == NULL)
1133c37859c2Ssjg 		err(1, "Could not get current working directory");
1134c37859c2Ssjg 	    cwdlen = strlen(cwd);
1135c37859c2Ssjg 	}
1136143a3267Srillig 	strlcpy(lcwd, cwd, sizeof lcwd);
1137143a3267Srillig 	strlcpy(latestdir, cwd, sizeof latestdir);
1138c37859c2Ssjg 
11392861b54eSrillig 	if (tmpdir == NULL) {
11402550dc93Ssjg 	    tmpdir = getTmpdir();
11412550dc93Ssjg 	    tmplen = strlen(tmpdir);
11422550dc93Ssjg 	}
11432550dc93Ssjg 
11444db43f7eSsjg 	/* we want to track all the .meta we read */
1145a1bc5ad8Srillig 	Global_Append(".MAKE.META.FILES", fname);
11464db43f7eSsjg 
1147b64904b4Srillig 	cmp_filter = metaCmpFilter || Var_Exists(gn, MAKE_META_CMP_FILTER);
114830df8a48Ssjg 
1149e93d02d9Srillig 	cmdNode = gn->commands.first;
11504db43f7eSsjg 	while (!oodate && (x = fgetLine(&buf, &bufsz, 0, fp)) > 0) {
11514db43f7eSsjg 	    lineno++;
11524db43f7eSsjg 	    if (buf[x - 1] == '\n')
11534db43f7eSsjg 		buf[x - 1] = '\0';
1154ebe0193fSsjg 	    else {
115554add74bSrillig 		warnx("%s: %u: line truncated at %u", fname, lineno, x);
1156244fd9f4Srillig 		oodate = true;
1157ebe0193fSsjg 		break;
1158ebe0193fSsjg 	    }
11595042f283Ssjg 	    link_src = NULL;
11605042f283Ssjg 	    move_target = NULL;
11614db43f7eSsjg 	    /* Find the start of the build monitor section. */
11622d086608Ssjg 	    if (!have_filemon) {
11634db43f7eSsjg 		if (strncmp(buf, "-- filemon", 10) == 0) {
1164244fd9f4Srillig 		    have_filemon = true;
11654db43f7eSsjg 		    continue;
11664db43f7eSsjg 		}
11674db43f7eSsjg 		if (strncmp(buf, "# buildmon", 10) == 0) {
1168244fd9f4Srillig 		    have_filemon = true;
11694db43f7eSsjg 		    continue;
11704db43f7eSsjg 		}
11714db43f7eSsjg 	    }
11724db43f7eSsjg 
11734db43f7eSsjg 	    /* Delimit the record type. */
11744db43f7eSsjg 	    p = buf;
11758d31de31Ssjg #ifdef DEBUG_META_MODE
117654add74bSrillig 	    DEBUG3(META, "%s: %u: %s\n", fname, lineno, buf);
11778d31de31Ssjg #endif
11784db43f7eSsjg 	    strsep(&p, " ");
11792d086608Ssjg 	    if (have_filemon) {
11802550dc93Ssjg 		/*
11812550dc93Ssjg 		 * We are in the 'filemon' output section.
11822550dc93Ssjg 		 * Each record from filemon follows the general form:
11832550dc93Ssjg 		 *
11842550dc93Ssjg 		 * <key> <pid> <data>
11852550dc93Ssjg 		 *
11862550dc93Ssjg 		 * Where:
11872550dc93Ssjg 		 * <key> is a single letter, denoting the syscall.
11882550dc93Ssjg 		 * <pid> is the process that made the syscall.
11892550dc93Ssjg 		 * <data> is the arguments (of interest).
11902550dc93Ssjg 		 */
11912550dc93Ssjg 		switch(buf[0]) {
11922550dc93Ssjg 		case '#':		/* comment */
11932550dc93Ssjg 		case 'V':		/* version */
11942550dc93Ssjg 		    break;
11952550dc93Ssjg 		default:
11962550dc93Ssjg 		    /*
11972550dc93Ssjg 		     * We need to track pathnames per-process.
11982550dc93Ssjg 		     *
11991baad74eSrillig 		     * Each process run by make starts off in the 'CWD'
12002550dc93Ssjg 		     * recorded in the .meta file, if it chdirs ('C')
12012550dc93Ssjg 		     * elsewhere we need to track that - but only for
12022550dc93Ssjg 		     * that process.  If it forks ('F'), we initialize
12032550dc93Ssjg 		     * the child to have the same cwd as its parent.
12042550dc93Ssjg 		     *
12052550dc93Ssjg 		     * We also need to track the 'latestdir' of
12062550dc93Ssjg 		     * interest.  This is usually the same as cwd, but
12072550dc93Ssjg 		     * not if a process is reading directories.
12082550dc93Ssjg 		     *
12092550dc93Ssjg 		     * Each time we spot a different process ('pid')
12102550dc93Ssjg 		     * we save the current value of 'latestdir' in a
12112550dc93Ssjg 		     * variable qualified by 'lastpid', and
12122550dc93Ssjg 		     * re-initialize 'latestdir' to any pre-saved
12132550dc93Ssjg 		     * value for the current 'pid' and 'CWD' if none.
12142550dc93Ssjg 		     */
1215ebe0193fSsjg 		    CHECK_VALID_META(p);
12162550dc93Ssjg 		    pid = atoi(p);
12172550dc93Ssjg 		    if (pid > 0 && pid != lastpid) {
121803074130Srillig 			FStr ldir;
12192550dc93Ssjg 
12202550dc93Ssjg 			if (lastpid > 0) {
1221545bddb1Ssjg 			    /* We need to remember these. */
12222ae09f1bSrillig 			    Global_Set(lcwd_vname, lcwd);
12232ae09f1bSrillig 			    Global_Set(ldir_vname, latestdir);
12242550dc93Ssjg 			}
1225143a3267Srillig 			snprintf(lcwd_vname, sizeof lcwd_vname, LCWD_VNAME_FMT, pid);
1226143a3267Srillig 			snprintf(ldir_vname, sizeof ldir_vname, LDIR_VNAME_FMT, pid);
12272550dc93Ssjg 			lastpid = pid;
1228835b4db4Srillig 			ldir = Var_Value(SCOPE_GLOBAL, ldir_vname);
122903074130Srillig 			if (ldir.str != NULL) {
123003074130Srillig 			    strlcpy(latestdir, ldir.str, sizeof latestdir);
123103074130Srillig 			    FStr_Done(&ldir);
1232545bddb1Ssjg 			}
1233835b4db4Srillig 			ldir = Var_Value(SCOPE_GLOBAL, lcwd_vname);
123403074130Srillig 			if (ldir.str != NULL) {
123503074130Srillig 			    strlcpy(lcwd, ldir.str, sizeof lcwd);
123603074130Srillig 			    FStr_Done(&ldir);
1237545bddb1Ssjg 			}
12382550dc93Ssjg 		    }
12392550dc93Ssjg 		    /* Skip past the pid. */
12402550dc93Ssjg 		    if (strsep(&p, " ") == NULL)
12412550dc93Ssjg 			continue;
12428d31de31Ssjg #ifdef DEBUG_META_MODE
12438d31de31Ssjg 		    if (DEBUG(META))
124454add74bSrillig 			debug_printf("%s: %u: %d: %c: cwd=%s lcwd=%s ldir=%s\n",
1245545bddb1Ssjg 				     fname, lineno,
1246545bddb1Ssjg 				     pid, buf[0], cwd, lcwd, latestdir);
12478d31de31Ssjg #endif
12482550dc93Ssjg 		    break;
12492550dc93Ssjg 		}
12502550dc93Ssjg 
1251ebe0193fSsjg 		CHECK_VALID_META(p);
1252ebe0193fSsjg 
12534db43f7eSsjg 		/* Process according to record type. */
12544db43f7eSsjg 		switch (buf[0]) {
12552550dc93Ssjg 		case 'X':		/* eXit */
12562ae09f1bSrillig 		    Var_Delete(SCOPE_GLOBAL, lcwd_vname);
12572ae09f1bSrillig 		    Var_Delete(SCOPE_GLOBAL, ldir_vname);
12582550dc93Ssjg 		    lastpid = 0;	/* no need to save ldir_vname */
12594db43f7eSsjg 		    break;
12604db43f7eSsjg 
12612550dc93Ssjg 		case 'F':		/* [v]Fork */
12622550dc93Ssjg 		    {
12632550dc93Ssjg 			char cldir[64];
12642550dc93Ssjg 			int child;
12652550dc93Ssjg 
12662550dc93Ssjg 			child = atoi(p);
12672550dc93Ssjg 			if (child > 0) {
1268143a3267Srillig 			    snprintf(cldir, sizeof cldir, LCWD_VNAME_FMT, child);
12692ae09f1bSrillig 			    Global_Set(cldir, lcwd);
1270143a3267Srillig 			    snprintf(cldir, sizeof cldir, LDIR_VNAME_FMT, child);
12712ae09f1bSrillig 			    Global_Set(cldir, latestdir);
1272545bddb1Ssjg #ifdef DEBUG_META_MODE
1273545bddb1Ssjg 			    if (DEBUG(META))
127437111947Srillig 				debug_printf(
127554add74bSrillig 					"%s: %u: %d: cwd=%s lcwd=%s ldir=%s\n",
1276545bddb1Ssjg 					fname, lineno,
1277545bddb1Ssjg 					child, cwd, lcwd, latestdir);
1278545bddb1Ssjg #endif
12792550dc93Ssjg 			}
12802550dc93Ssjg 		    }
12812550dc93Ssjg 		    break;
12822550dc93Ssjg 
12832550dc93Ssjg 		case 'C':		/* Chdir */
1284545bddb1Ssjg 		    /* Update lcwd and latest directory. */
1285143a3267Srillig 		    strlcpy(latestdir, p, sizeof latestdir);
1286143a3267Srillig 		    strlcpy(lcwd, p, sizeof lcwd);
12872ae09f1bSrillig 		    Global_Set(lcwd_vname, lcwd);
12882ae09f1bSrillig 		    Global_Set(ldir_vname, lcwd);
1289545bddb1Ssjg #ifdef DEBUG_META_MODE
129054add74bSrillig 		    DEBUG4(META, "%s: %u: cwd=%s ldir=%s\n",
1291ae6e2b2cSrillig 			   fname, lineno, cwd, lcwd);
1292545bddb1Ssjg #endif
12934db43f7eSsjg 		    break;
12944db43f7eSsjg 
129548587910Ssjg 		case 'M':		/* renaMe */
12965042f283Ssjg 		    /*
12975042f283Ssjg 		     * For 'M'oves we want to check
12985042f283Ssjg 		     * the src as for 'R'ead
12995042f283Ssjg 		     * and the target as for 'W'rite.
13005042f283Ssjg 		     */
130106ccd6ceSrillig 		    {
130206ccd6ceSrillig 			char *cp = p;	/* save this for a second */
13035042f283Ssjg 			/* now get target */
13045042f283Ssjg 			if (strsep(&p, " ") == NULL)
13055042f283Ssjg 			    continue;
13065042f283Ssjg 			CHECK_VALID_META(p);
13075042f283Ssjg 			move_target = p;
13085042f283Ssjg 			p = cp;
130906ccd6ceSrillig 		    }
131048587910Ssjg 		    /* 'L' and 'M' put single quotes around the args */
13115042f283Ssjg 		    DEQUOTE(p);
13125042f283Ssjg 		    DEQUOTE(move_target);
131348587910Ssjg 		    /* FALLTHROUGH */
131448587910Ssjg 		case 'D':		/* unlink */
13159c2db9feSrillig 		    if (*p == '/') {
1316c2d73114Ssjg 			/* remove any missingFiles entries that match p */
131768212223Srillig 			StringListNode *ln = missingFiles.first;
13189c2db9feSrillig 			while (ln != NULL) {
13199c2db9feSrillig 			    StringListNode *next = ln->next;
13209c2db9feSrillig 			    if (path_starts_with(ln->datum, p)) {
13219c2db9feSrillig 				free(ln->datum);
132268212223Srillig 				Lst_Remove(&missingFiles, ln);
13239c2db9feSrillig 			    }
13249c2db9feSrillig 			    ln = next;
132548587910Ssjg 			}
132648587910Ssjg 		    }
13275042f283Ssjg 		    if (buf[0] == 'M') {
13285042f283Ssjg 			/* the target of the mv is a file 'W'ritten */
13295042f283Ssjg #ifdef DEBUG_META_MODE
1330ae6e2b2cSrillig 			DEBUG2(META, "meta_oodate: M %s -> %s\n",
13315042f283Ssjg 			       p, move_target);
13325042f283Ssjg #endif
13335042f283Ssjg 			p = move_target;
13345042f283Ssjg 			goto check_write;
13355042f283Ssjg 		    }
133648587910Ssjg 		    break;
133748587910Ssjg 		case 'L':		/* Link */
13385042f283Ssjg 		    /*
13395042f283Ssjg 		     * For 'L'inks check
13405042f283Ssjg 		     * the src as for 'R'ead
13415042f283Ssjg 		     * and the target as for 'W'rite.
13425042f283Ssjg 		     */
13435042f283Ssjg 		    link_src = p;
13445042f283Ssjg 		    /* now get target */
134548587910Ssjg 		    if (strsep(&p, " ") == NULL)
134648587910Ssjg 			continue;
1347ebe0193fSsjg 		    CHECK_VALID_META(p);
134848587910Ssjg 		    /* 'L' and 'M' put single quotes around the args */
13495042f283Ssjg 		    DEQUOTE(p);
13505042f283Ssjg 		    DEQUOTE(link_src);
13515042f283Ssjg #ifdef DEBUG_META_MODE
1352ae6e2b2cSrillig 		    DEBUG2(META, "meta_oodate: L %s -> %s\n", link_src, p);
13535042f283Ssjg #endif
135448587910Ssjg 		    /* FALLTHROUGH */
135548587910Ssjg 		case 'W':		/* Write */
13565042f283Ssjg 		check_write:
135748587910Ssjg 		    /*
135848587910Ssjg 		     * If a file we generated within our bailiwick
135948587910Ssjg 		     * but outside of .OBJDIR is missing,
136048587910Ssjg 		     * we need to do it again.
136148587910Ssjg 		     */
136248587910Ssjg 		    /* ignore non-absolute paths */
136348587910Ssjg 		    if (*p != '/')
136448587910Ssjg 			break;
136548587910Ssjg 
1366733418ceSrillig 		    if (Lst_IsEmpty(&metaBailiwick))
136748587910Ssjg 			break;
136848587910Ssjg 
136948587910Ssjg 		    /* ignore cwd - normal dependencies handle those */
137048587910Ssjg 		    if (strncmp(p, cwd, cwdlen) == 0)
137148587910Ssjg 			break;
137248587910Ssjg 
1373733418ceSrillig 		    if (!has_any_prefix(p, &metaBailiwick))
137448587910Ssjg 			break;
137548587910Ssjg 
137648587910Ssjg 		    /* tmpdir might be within */
137748587910Ssjg 		    if (tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0)
137848587910Ssjg 			break;
137948587910Ssjg 
138048587910Ssjg 		    /* ignore anything containing the string "tmp" */
13811c1f1456Srillig 		    /* XXX: The arguments to strstr must be swapped. */
138271c58e78Srillig 		    if (strstr("tmp", p) != NULL)
138348587910Ssjg 			break;
138448587910Ssjg 
138533ac710bSrillig 		    if ((link_src != NULL && cached_lstat(p, &cst) < 0) ||
138633ac710bSrillig 			(link_src == NULL && cached_stat(p, &cst) < 0)) {
1387a455fbeeSrillig 			if (!meta_ignore(gn, p))
138868212223Srillig 			    append_if_new(&missingFiles, p);
13892e603a11Ssjg 		    }
139048587910Ssjg 		    break;
13915042f283Ssjg 		check_link_src:
13925042f283Ssjg 		    p = link_src;
13935042f283Ssjg 		    link_src = NULL;
13945042f283Ssjg #ifdef DEBUG_META_MODE
1395ae6e2b2cSrillig 		    DEBUG1(META, "meta_oodate: L src %s\n", p);
13965042f283Ssjg #endif
13975042f283Ssjg 		    /* FALLTHROUGH */
13982550dc93Ssjg 		case 'R':		/* Read */
13992550dc93Ssjg 		case 'E':		/* Exec */
14004db43f7eSsjg 		    /*
14014db43f7eSsjg 		     * Check for runtime files that can't
14024db43f7eSsjg 		     * be part of the dependencies because
14034db43f7eSsjg 		     * they are _expected_ to change.
14044db43f7eSsjg 		     */
14052e603a11Ssjg 		    if (meta_ignore(gn, p))
14062550dc93Ssjg 			break;
140769fc18aaSsjg 
14084db43f7eSsjg 		    /*
14092550dc93Ssjg 		     * The rest of the record is the file name.
14102550dc93Ssjg 		     * Check if it's not an absolute path.
14114db43f7eSsjg 		     */
14122550dc93Ssjg 		    {
14132550dc93Ssjg 			char *sdirs[4];
14142550dc93Ssjg 			char **sdp;
14152550dc93Ssjg 			int sdx = 0;
1416244fd9f4Srillig 			bool found = false;
14174db43f7eSsjg 
14182550dc93Ssjg 			if (*p == '/') {
14192550dc93Ssjg 			    sdirs[sdx++] = p; /* done */
14202550dc93Ssjg 			} else {
14212550dc93Ssjg 			    if (strcmp(".", p) == 0)
14222550dc93Ssjg 				continue; /* no point */
14232550dc93Ssjg 
14242550dc93Ssjg 			    /* Check vs latestdir */
14259cebc24eSsjg 			    if (snprintf(fname1, sizeof fname1, "%s/%s", latestdir, p) < (int)(sizeof fname1))
14262550dc93Ssjg 				sdirs[sdx++] = fname1;
14272550dc93Ssjg 
1428545bddb1Ssjg 			    if (strcmp(latestdir, lcwd) != 0) {
1429545bddb1Ssjg 				/* Check vs lcwd */
14309cebc24eSsjg 				if (snprintf(fname2, sizeof fname2, "%s/%s", lcwd, p) < (int)(sizeof fname2))
14312550dc93Ssjg 				    sdirs[sdx++] = fname2;
14322550dc93Ssjg 			    }
1433545bddb1Ssjg 			    if (strcmp(lcwd, cwd) != 0) {
1434545bddb1Ssjg 				/* Check vs cwd */
14359cebc24eSsjg 				if (snprintf(fname3, sizeof fname3, "%s/%s", cwd, p) < (int)(sizeof fname3))
1436545bddb1Ssjg 				    sdirs[sdx++] = fname3;
1437545bddb1Ssjg 			    }
14382550dc93Ssjg 			}
14392550dc93Ssjg 			sdirs[sdx++] = NULL;
14402550dc93Ssjg 
144131940a95Srillig 			for (sdp = sdirs; *sdp != NULL && !found; sdp++) {
14428d31de31Ssjg #ifdef DEBUG_META_MODE
144354add74bSrillig 			    DEBUG3(META, "%s: %u: looking for: %s\n",
1444ae6e2b2cSrillig 				   fname, lineno, *sdp);
14458d31de31Ssjg #endif
144633ac710bSrillig 			    if (cached_stat(*sdp, &cst) == 0) {
1447244fd9f4Srillig 				found = true;
14482550dc93Ssjg 				p = *sdp;
14492550dc93Ssjg 			    }
14502550dc93Ssjg 			}
14512550dc93Ssjg 			if (found) {
14528d31de31Ssjg #ifdef DEBUG_META_MODE
145354add74bSrillig 			    DEBUG3(META, "%s: %u: found: %s\n",
1454ae6e2b2cSrillig 				   fname, lineno, p);
14558d31de31Ssjg #endif
145633ac710bSrillig 			    if (!S_ISDIR(cst.cst_mode) &&
145733ac710bSrillig 				cst.cst_mtime > gn->mtime) {
145854add74bSrillig 				DEBUG3(META, "%s: %u: file '%s' is newer than the target...\n",
1459ae6e2b2cSrillig 				       fname, lineno, p);
1460244fd9f4Srillig 				oodate = true;
146133ac710bSrillig 			    } else if (S_ISDIR(cst.cst_mode)) {
14622550dc93Ssjg 				/* Update the latest directory. */
1463cb28c699Ssjg 				cached_realpath(p, latestdir);
14644db43f7eSsjg 			    }
14652550dc93Ssjg 			} else if (errno == ENOENT && *p == '/' &&
14662550dc93Ssjg 				   strncmp(p, cwd, cwdlen) != 0) {
14672550dc93Ssjg 			    /*
14682550dc93Ssjg 			     * A referenced file outside of CWD is missing.
14692550dc93Ssjg 			     * We cannot catch every eventuality here...
14702550dc93Ssjg 			     */
147168212223Srillig 			    append_if_new(&missingFiles, p);
14721b59e5d0Ssjg 			}
14732550dc93Ssjg 		    }
1474545bddb1Ssjg 		    if (buf[0] == 'E') {
1475545bddb1Ssjg 			/* previous latestdir is no longer relevant */
1476143a3267Srillig 			strlcpy(latestdir, lcwd, sizeof latestdir);
1477545bddb1Ssjg 		    }
14784db43f7eSsjg 		    break;
14794db43f7eSsjg 		default:
14804db43f7eSsjg 		    break;
14814db43f7eSsjg 		}
14825042f283Ssjg 		if (!oodate && buf[0] == 'L' && link_src != NULL)
14835042f283Ssjg 		    goto check_link_src;
14842550dc93Ssjg 	    } else if (strcmp(buf, "CMD") == 0) {
14854db43f7eSsjg 		/*
14864db43f7eSsjg 		 * Compare the current command with the one in the
14874db43f7eSsjg 		 * meta data file.
14884db43f7eSsjg 		 */
1489d5631b52Srillig 		if (cmdNode == NULL) {
149054add74bSrillig 		    DEBUG2(META, "%s: %u: there were more build commands in the meta data file than there are now...\n",
1491ae6e2b2cSrillig 			   fname, lineno);
1492244fd9f4Srillig 		    oodate = true;
14934db43f7eSsjg 		} else {
149406ccd6ceSrillig 		    const char *cp;
1495859199baSrillig 		    char *cmd = cmdNode->datum;
1496244fd9f4Srillig 		    bool hasOODATE = false;
14974db43f7eSsjg 
149871c58e78Srillig 		    if (strstr(cmd, "$?") != NULL)
1499244fd9f4Srillig 			hasOODATE = true;
150071c58e78Srillig 		    else if ((cp = strstr(cmd, ".OODATE")) != NULL) {
1501e570fc40Ssjg 			/* check for $[{(].OODATE[:)}] */
15024db43f7eSsjg 			if (cp > cmd + 2 && cp[-2] == '$')
1503244fd9f4Srillig 			    hasOODATE = true;
15044db43f7eSsjg 		    }
1505e570fc40Ssjg 		    if (hasOODATE) {
1506244fd9f4Srillig 			needOODATE = true;
150754add74bSrillig 			DEBUG2(META, "%s: %u: cannot compare command using .OODATE\n",
1508ae6e2b2cSrillig 			       fname, lineno);
15094db43f7eSsjg 		    }
1510*0c336a8cSrillig 		    cmd = Var_Subst(cmd, gn, VARE_EVAL_DEFINED);
1511e366c998Srillig 		    /* TODO: handle errors */
15124db43f7eSsjg 
151371c58e78Srillig 		    if ((cp = strchr(cmd, '\n')) != NULL) {
15144db43f7eSsjg 			int n;
15154db43f7eSsjg 
15164db43f7eSsjg 			/*
15174db43f7eSsjg 			 * This command contains newlines, we need to
15184db43f7eSsjg 			 * fetch more from the .meta file before we
15194db43f7eSsjg 			 * attempt a comparison.
15204db43f7eSsjg 			 */
15214db43f7eSsjg 			/* first put the newline back at buf[x - 1] */
15224db43f7eSsjg 			buf[x - 1] = '\n';
15234db43f7eSsjg 			do {
15244db43f7eSsjg 			    /* now fetch the next line */
15254db43f7eSsjg 			    if ((n = fgetLine(&buf, &bufsz, x, fp)) <= 0)
15264db43f7eSsjg 				break;
15274db43f7eSsjg 			    x = n;
15284db43f7eSsjg 			    lineno++;
15294db43f7eSsjg 			    if (buf[x - 1] != '\n') {
153054add74bSrillig 				warnx("%s: %u: line truncated at %u", fname, lineno, x);
15314db43f7eSsjg 				break;
15324db43f7eSsjg 			    }
153394f06ed7Srillig 			    cp = strchr(cp + 1, '\n');
15344adbce36Srillig 			} while (cp != NULL);
15354db43f7eSsjg 			if (buf[x - 1] == '\n')
15364db43f7eSsjg 			    buf[x - 1] = '\0';
15374db43f7eSsjg 		    }
1538a0c92525Srillig 		    if (p != NULL &&
15398953c857Ssjg 			!hasOODATE &&
15404db43f7eSsjg 			!(gn->type & OP_NOMETA_CMP) &&
154130df8a48Ssjg 			(meta_cmd_cmp(gn, p, cmd, cmp_filter) != 0)) {
154254add74bSrillig 			DEBUG4(META, "%s: %u: a build command has changed\n%s\nvs\n%s\n",
1543ae6e2b2cSrillig 			       fname, lineno, p, cmd);
15444db43f7eSsjg 			if (!metaIgnoreCMDs)
1545244fd9f4Srillig 			    oodate = true;
15464db43f7eSsjg 		    }
15474db43f7eSsjg 		    free(cmd);
1548073f669aSrillig 		    cmdNode = cmdNode->next;
15494db43f7eSsjg 		}
15504db43f7eSsjg 	    } else if (strcmp(buf, "CWD") == 0) {
15514db43f7eSsjg 		/*
15524db43f7eSsjg 		 * Check if there are extra commands now
15534db43f7eSsjg 		 * that weren't in the meta data file.
15544db43f7eSsjg 		 */
1555d5631b52Srillig 		if (!oodate && cmdNode != NULL) {
155654add74bSrillig 		    DEBUG2(META, "%s: %u: there are extra build commands now that weren't in the meta data file\n",
1557ae6e2b2cSrillig 			   fname, lineno);
1558244fd9f4Srillig 		    oodate = true;
15594db43f7eSsjg 		}
15609b28b4b7Ssjg 		CHECK_VALID_META(p);
1561be03bd03Ssjg 		if (strcmp(p, cwd) != 0) {
156254add74bSrillig 		    DEBUG4(META, "%s: %u: the current working directory has changed from '%s' to '%s'\n",
1563ae6e2b2cSrillig 			   fname, lineno, p, curdir);
1564244fd9f4Srillig 		    oodate = true;
1565be03bd03Ssjg 		}
1566be03bd03Ssjg 	    }
1567be03bd03Ssjg 	}
15684db43f7eSsjg 
15694db43f7eSsjg 	fclose(fp);
157068212223Srillig 	if (!Lst_IsEmpty(&missingFiles)) {
1571ae6e2b2cSrillig 	    DEBUG2(META, "%s: missing files: %s...\n",
157268212223Srillig 		   fname, (char *)missingFiles.first->datum);
1573244fd9f4Srillig 	    oodate = true;
157448587910Ssjg 	}
1575ec729b23Ssjg 	if (!oodate && !have_filemon && filemonMissing) {
1576ae6e2b2cSrillig 	    DEBUG1(META, "%s: missing filemon data\n", fname);
1577244fd9f4Srillig 	    oodate = true;
1578ec729b23Ssjg 	}
1579bbf6dc99Ssjg     } else {
158093d38918Ssjg 	if (writeMeta && (metaMissing || (gn->type & OP_META))) {
158106ccd6ceSrillig 	    const char *cp = NULL;
1582ec729b23Ssjg 
1583ec729b23Ssjg 	    /* if target is in .CURDIR we do not need a meta file */
158431940a95Srillig 	    if (gn->path != NULL && (cp = strrchr(gn->path, '/')) != NULL &&
158531940a95Srillig 		(cp > gn->path)) {
15865658125dSrillig 		if (strncmp(curdir, gn->path, (size_t)(cp - gn->path)) != 0) {
1587ec729b23Ssjg 		    cp = NULL;		/* not in .CURDIR */
1588ec729b23Ssjg 		}
1589ec729b23Ssjg 	    }
15902861b54eSrillig 	    if (cp == NULL) {
1591ae6e2b2cSrillig 		DEBUG1(META, "%s: required but missing\n", fname);
1592244fd9f4Srillig 		oodate = true;
1593244fd9f4Srillig 		needOODATE = true;	/* assume the worst */
1594bbf6dc99Ssjg 	    }
15954db43f7eSsjg 	}
15962d086608Ssjg     }
15972d086608Ssjg 
15981bf92009Srillig     Lst_DoneFree(&missingFiles);
1599b6546efdSchristos 
16002a4896f8Ssjg     if (oodate && needOODATE) {
16011b59e5d0Ssjg 	/*
16022a4896f8Ssjg 	 * Target uses .OODATE which is empty; or we wouldn't be here.
16032a4896f8Ssjg 	 * We have decided it is oodate, so .OODATE needs to be set.
16042a4896f8Ssjg 	 * All we can sanely do is set it to .ALLSRC.
16051b59e5d0Ssjg 	 */
1606835b4db4Srillig 	Var_Delete(gn, OODATE);
1607835b4db4Srillig 	Var_Set(gn, OODATE, GNode_VarAllsrc(gn));
16081b59e5d0Ssjg     }
16092d086608Ssjg 
16102d086608Ssjg  oodate_out:
161103074130Srillig     FStr_Done(&dname);
16124db43f7eSsjg     return oodate;
16134db43f7eSsjg }
16144db43f7eSsjg 
16154db43f7eSsjg /* support for compat mode */
16164db43f7eSsjg 
16174db43f7eSsjg static int childPipe[2];
16184db43f7eSsjg 
16194db43f7eSsjg void
meta_compat_start(void)16204db43f7eSsjg meta_compat_start(void)
16214db43f7eSsjg {
16222a5d072bSmaxv #ifdef USE_FILEMON_ONCE
16232a5d072bSmaxv     /*
16242a5d072bSmaxv      * We need to re-open filemon for each cmd.
16252a5d072bSmaxv      */
16262a5d072bSmaxv     BuildMon *pbm = &Mybm;
16272a5d072bSmaxv 
16282a5d072bSmaxv     if (pbm->mfp != NULL && useFilemon) {
16291378959eSriastradh 	meta_open_filemon(pbm);
16302a5d072bSmaxv     } else {
16311378959eSriastradh 	pbm->mon_fd = -1;
16321378959eSriastradh 	pbm->filemon = NULL;
16332a5d072bSmaxv     }
16342a5d072bSmaxv #endif
16354db43f7eSsjg     if (pipe(childPipe) < 0)
16364db43f7eSsjg 	Punt("Cannot create pipe: %s", strerror(errno));
16374db43f7eSsjg     /* Set close-on-exec flag for both */
163843e24988Schristos     (void)fcntl(childPipe[0], F_SETFD, FD_CLOEXEC);
163943e24988Schristos     (void)fcntl(childPipe[1], F_SETFD, FD_CLOEXEC);
16404db43f7eSsjg }
16414db43f7eSsjg 
16424db43f7eSsjg void
meta_compat_child(void)16434db43f7eSsjg meta_compat_child(void)
16444db43f7eSsjg {
16454db43f7eSsjg     meta_job_child(NULL);
1646f80730c1Srillig     if (dup2(childPipe[1], STDOUT_FILENO) < 0
1647f80730c1Srillig 	    || dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
16482309709fSrillig 	execDie("dup2", "pipe");
16494db43f7eSsjg }
16504db43f7eSsjg 
16514db43f7eSsjg void
meta_compat_parent(pid_t child)16521378959eSriastradh meta_compat_parent(pid_t child)
16534db43f7eSsjg {
16541378959eSriastradh     int outfd, metafd, maxfd, nfds;
1655ee757927Ssjg     char buf[BUFSIZ+1];
16561378959eSriastradh     fd_set readfds;
16574db43f7eSsjg 
16581378959eSriastradh     meta_job_parent(NULL, child);
16594db43f7eSsjg     close(childPipe[1]);			/* child side */
16601378959eSriastradh     outfd = childPipe[0];
1661b8cb226cSsjg #ifdef USE_FILEMON
166231940a95Srillig     metafd = Mybm.filemon != NULL ? filemon_readfd(Mybm.filemon) : -1;
1663b8cb226cSsjg #else
1664b8cb226cSsjg     metafd = -1;
1665b8cb226cSsjg #endif
16661378959eSriastradh     maxfd = -1;
16671378959eSriastradh     if (outfd > maxfd)
16681378959eSriastradh 	    maxfd = outfd;
16691378959eSriastradh     if (metafd > maxfd)
16701378959eSriastradh 	    maxfd = metafd;
16711378959eSriastradh 
16721378959eSriastradh     while (outfd != -1 || metafd != -1) {
16731378959eSriastradh 	FD_ZERO(&readfds);
16741378959eSriastradh 	if (outfd != -1) {
16751378959eSriastradh 	    FD_SET(outfd, &readfds);
16764db43f7eSsjg 	}
16771378959eSriastradh 	if (metafd != -1) {
16781378959eSriastradh 	    FD_SET(metafd, &readfds);
16791378959eSriastradh 	}
16801378959eSriastradh 	nfds = select(maxfd + 1, &readfds, NULL, NULL, NULL);
16811378959eSriastradh 	if (nfds == -1) {
16821378959eSriastradh 	    if (errno == EINTR)
16831378959eSriastradh 		continue;
16841378959eSriastradh 	    err(1, "select");
16851378959eSriastradh 	}
16861378959eSriastradh 
168731940a95Srillig 	if (outfd != -1 && FD_ISSET(outfd, &readfds) != 0) do {
16881378959eSriastradh 	    /* XXX this is not line-buffered */
1689143a3267Srillig 	    ssize_t nread = read(outfd, buf, sizeof buf - 1);
16901378959eSriastradh 	    if (nread == -1)
16911378959eSriastradh 		err(1, "read");
16921378959eSriastradh 	    if (nread == 0) {
16931378959eSriastradh 		close(outfd);
16941378959eSriastradh 		outfd = -1;
16951378959eSriastradh 		break;
16961378959eSriastradh 	    }
16971378959eSriastradh 	    fwrite(buf, 1, (size_t)nread, stdout);
16981378959eSriastradh 	    fflush(stdout);
1699ee757927Ssjg 	    buf[nread] = '\0';
1700ee757927Ssjg 	    meta_job_output(NULL, buf, "");
17019349f183Srillig 	} while (false);
170231940a95Srillig 	if (metafd != -1 && FD_ISSET(metafd, &readfds) != 0) {
17031378959eSriastradh 	    if (meta_job_event(NULL) <= 0)
17041378959eSriastradh 		metafd = -1;
17051378959eSriastradh 	}
17061378959eSriastradh     }
17074db43f7eSsjg }
1708e7543dc4Ssjg 
1709e7543dc4Ssjg #endif /* USE_META */
1710