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