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