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