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