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