meta.c revision 250837
155714Skris/*      $NetBSD: meta.c,v 1.30 2013/05/16 21:56:56 sjg Exp $ */
255714Skris
3238405Sjkim/*
455714Skris * Implement 'meta' mode.
555714Skris * Adapted from John Birrell's patches to FreeBSD make.
655714Skris * --sjg
755714Skris */
855714Skris/*
955714Skris * Copyright (c) 2009-2010, Juniper Networks, Inc.
10280304Sjkim * Portions Copyright (c) 2009, John Birrell.
1155714Skris *
1255714Skris * Redistribution and use in source and binary forms, with or without
1355714Skris * modification, are permitted provided that the following conditions
1455714Skris * are met:
1555714Skris * 1. Redistributions of source code must retain the above copyright
1655714Skris *    notice, this list of conditions and the following disclaimer.
1755714Skris * 2. Redistributions in binary form must reproduce the above copyright
1855714Skris *    notice, this list of conditions and the following disclaimer in the
1955714Skris *    documentation and/or other materials provided with the distribution.
2055714Skris *
2155714Skris * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2255714Skris * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2355714Skris * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2455714Skris * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2555714Skris * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2655714Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2755714Skris * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2855714Skris * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2955714Skris * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3055714Skris * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3155714Skris * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3255714Skris */
3355714Skris#if defined(USE_META)
3455714Skris
3555714Skris#ifdef HAVE_CONFIG_H
3655714Skris# include "config.h"
3755714Skris#endif
3855714Skris#include <sys/stat.h>
3955714Skris#include <sys/ioctl.h>
4055714Skris#include <fcntl.h>
4155714Skris#include <libgen.h>
4255714Skris#include <errno.h>
4355714Skris#if !defined(HAVE_CONFIG_H) || defined(HAVE_ERR_H)
4455714Skris#include <err.h>
4555714Skris#endif
4655714Skris
4755714Skris#include "make.h"
4855714Skris#include "job.h"
4955714Skris
5055714Skris#ifdef HAVE_FILEMON_H
5155714Skris# include <filemon.h>
5255714Skris#endif
5355714Skris#if !defined(USE_FILEMON) && defined(FILEMON_SET_FD)
5455714Skris# define USE_FILEMON
5555714Skris#endif
56280304Sjkim
57280304Sjkimstatic BuildMon Mybm;			/* for compat */
5859191Skrisstatic Lst metaBailiwick;			/* our scope of control */
5959191Skris
6055714SkrisBoolean useMeta = FALSE;
6155714Skrisstatic Boolean useFilemon = FALSE;
6255714Skrisstatic Boolean writeMeta = FALSE;
6355714Skrisstatic Boolean metaEnv = FALSE;		/* don't save env unless asked */
6455714Skrisstatic Boolean metaVerbose = FALSE;
6555714Skrisstatic Boolean metaIgnoreCMDs = FALSE;	/* ignore CMDs in .meta files */
6655714Skrisstatic Boolean metaCurdirOk = FALSE;	/* write .meta in .CURDIR Ok? */
67109998Smarkmstatic Boolean metaSilent = FALSE;	/* if we have a .meta be SILENT */
68160814Ssimon
69280304Sjkimextern Boolean forceJobs;
70280304Sjkimextern Boolean comatMake;
71160814Ssimonextern char    **environ;
72280304Sjkim
73280304Sjkim#define	MAKE_META_PREFIX	".MAKE.META.PREFIX"
74280304Sjkim
75280304Sjkim#ifndef N2U
76280304Sjkim# define N2U(n, u)   (((n) + ((u) - 1)) / (u))
77280304Sjkim#endif
78280304Sjkim#ifndef ROUNDUP
79280304Sjkim# define ROUNDUP(n, u)   (N2U((n), (u)) * (u))
80280304Sjkim#endif
81280304Sjkim
82280304Sjkim#if !defined(HAVE_STRSEP)
83280304Sjkim# define strsep(s, d) stresep((s), (d), 0)
84280304Sjkim#endif
85280304Sjkim
86280304Sjkim/*
87280304Sjkim * Filemon is a kernel module which snoops certain syscalls.
88280304Sjkim *
89280304Sjkim * C chdir
90280304Sjkim * E exec
91280304Sjkim * F [v]fork
92280304Sjkim * L [sym]link
93280304Sjkim * M rename
94280304Sjkim * R read
95280304Sjkim * W write
96280304Sjkim * S stat
97280304Sjkim *
98280304Sjkim * See meta_oodate below - we mainly care about 'E' and 'R'.
99280304Sjkim *
100280304Sjkim * We can still use meta mode without filemon, but
10155714Skris * the benefits are more limited.
102280304Sjkim */
103280304Sjkim#ifdef USE_FILEMON
104280304Sjkim# ifndef _PATH_FILEMON
105280304Sjkim#   define _PATH_FILEMON "/dev/filemon"
106280304Sjkim# endif
107280304Sjkim
108280304Sjkim/*
109280304Sjkim * Open the filemon device.
110280304Sjkim */
111280304Sjkimstatic void
112280304Sjkimfilemon_open(BuildMon *pbm)
113280304Sjkim{
114280304Sjkim    int retry;
115280304Sjkim
116280304Sjkim    pbm->mon_fd = pbm->filemon_fd = -1;
117280304Sjkim    if (!useFilemon)
11855714Skris	return;
11955714Skris
12055714Skris    for (retry = 5; retry >= 0; retry--) {
12155714Skris	if ((pbm->filemon_fd = open(_PATH_FILEMON, O_RDWR)) >= 0)
122280304Sjkim	    break;
123167612Ssimon    }
12455714Skris
125280304Sjkim    if (pbm->filemon_fd < 0) {
126280304Sjkim	useFilemon = FALSE;
127280304Sjkim	warn("Could not open %s", _PATH_FILEMON);
128280304Sjkim	return;
12955714Skris    }
130280304Sjkim
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 */
150static void
151filemon_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 */
182static void
183eat_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
221static char *
222meta_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 */
307static int
308is_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
355typedef struct meta_file_s {
356    FILE *fp;
357    GNode *gn;
358} meta_file_t;
359
360static int
361printCMD(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
389static FILE *
390meta_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
528static Boolean
529boolValue(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/*
543 * Initialization we need before reading makefiles.
544 */
545void
546meta_init(void)
547{
548#ifdef USE_FILEMON
549	/* this allows makefiles to test if we have filemon support */
550	Var_Set(".MAKE.PATH_FILEMON", _PATH_FILEMON, VAR_GLOBAL, 0);
551#endif
552}
553
554
555/*
556 * Initialization we need after reading makefiles.
557 */
558void
559meta_mode_init(const char *make_mode)
560{
561    static int once = 0;
562    char *cp;
563
564    useMeta = TRUE;
565    useFilemon = TRUE;
566    writeMeta = TRUE;
567
568    if (make_mode) {
569	if (strstr(make_mode, "env"))
570	    metaEnv = TRUE;
571	if (strstr(make_mode, "verb"))
572	    metaVerbose = TRUE;
573	if (strstr(make_mode, "read"))
574	    writeMeta = FALSE;
575	if (strstr(make_mode, "nofilemon"))
576	    useFilemon = FALSE;
577	if ((cp = strstr(make_mode, "curdirok="))) {
578	    metaCurdirOk = boolValue(&cp[9]);
579	}
580	if ((cp = strstr(make_mode, "silent="))) {
581	    metaSilent = boolValue(&cp[7]);
582	}
583	if (strstr(make_mode, "ignore-cmd"))
584	    metaIgnoreCMDs = TRUE;
585	/* for backwards compatability */
586	Var_Set(".MAKE.META_CREATED", "${.MAKE.META.CREATED}", VAR_GLOBAL, 0);
587	Var_Set(".MAKE.META_FILES", "${.MAKE.META.FILES}", VAR_GLOBAL, 0);
588    }
589    if (metaVerbose && !Var_Exists(MAKE_META_PREFIX, VAR_GLOBAL)) {
590	/*
591	 * The default value for MAKE_META_PREFIX
592	 * prints the absolute path of the target.
593	 * This works be cause :H will generate '.' if there is no /
594	 * and :tA will resolve that to cwd.
595	 */
596	Var_Set(MAKE_META_PREFIX, "Building ${.TARGET:H:tA}/${.TARGET:T}", VAR_GLOBAL, 0);
597    }
598    if (once)
599	return;
600    once = 1;
601    memset(&Mybm, 0, sizeof(Mybm));
602    /*
603     * We consider ourselves master of all within ${.MAKE.META.BAILIWICK}
604     */
605    metaBailiwick = Lst_Init(FALSE);
606    cp = Var_Subst(NULL, "${.MAKE.META.BAILIWICK:O:u:tA}", VAR_GLOBAL, 0);
607    if (cp) {
608	str2Lst_Append(metaBailiwick, cp, NULL);
609    }
610}
611
612/*
613 * In each case below we allow for job==NULL
614 */
615void
616meta_job_start(Job *job, GNode *gn)
617{
618    BuildMon *pbm;
619
620    if (job != NULL) {
621	pbm = &job->bm;
622    } else {
623	pbm = &Mybm;
624    }
625    pbm->mfp = meta_create(pbm, gn);
626#ifdef USE_FILEMON_ONCE
627    /* compat mode we open the filemon dev once per command */
628    if (job == NULL)
629	return;
630#endif
631#ifdef USE_FILEMON
632    if (pbm->mfp != NULL && useFilemon) {
633	filemon_open(pbm);
634    } else {
635	pbm->mon_fd = pbm->filemon_fd = -1;
636    }
637#endif
638}
639
640/*
641 * The child calls this before doing anything.
642 * It does not disturb our state.
643 */
644void
645meta_job_child(Job *job)
646{
647#ifdef USE_FILEMON
648    BuildMon *pbm;
649    pid_t pid;
650
651    if (job != NULL) {
652	pbm = &job->bm;
653    } else {
654	pbm = &Mybm;
655    }
656    pid = getpid();
657    if (pbm->mfp != NULL && useFilemon) {
658	if (ioctl(pbm->filemon_fd, FILEMON_SET_PID, &pid) < 0) {
659	    err(1, "Could not set filemon pid!");
660	}
661    }
662#endif
663}
664
665void
666meta_job_error(Job *job, GNode *gn, int flags, int status)
667{
668    char cwd[MAXPATHLEN];
669    BuildMon *pbm;
670
671    if (job != NULL) {
672	pbm = &job->bm;
673    } else {
674	if (!gn)
675	    gn = job->node;
676	pbm = &Mybm;
677    }
678    if (pbm->mfp != NULL) {
679	fprintf(pbm->mfp, "*** Error code %d%s\n",
680		status,
681		(flags & JOB_IGNERR) ?
682		"(ignored)" : "");
683    }
684    if (gn) {
685	Var_Set(".ERROR_TARGET", gn->path ? gn->path : gn->name, VAR_GLOBAL, 0);
686    }
687    getcwd(cwd, sizeof(cwd));
688    Var_Set(".ERROR_CWD", cwd, VAR_GLOBAL, 0);
689    if (pbm && pbm->meta_fname[0]) {
690	Var_Set(".ERROR_META_FILE", pbm->meta_fname, VAR_GLOBAL, 0);
691    }
692    meta_job_finish(job);
693}
694
695void
696meta_job_output(Job *job, char *cp, const char *nl)
697{
698    BuildMon *pbm;
699
700    if (job != NULL) {
701	pbm = &job->bm;
702    } else {
703	pbm = &Mybm;
704    }
705    if (pbm->mfp != NULL) {
706	if (metaVerbose) {
707	    static char *meta_prefix = NULL;
708	    static int meta_prefix_len;
709
710	    if (!meta_prefix) {
711		char *cp2;
712
713		meta_prefix = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", VAR_GLOBAL, 0);
714		if ((cp2 = strchr(meta_prefix, '$')))
715		    meta_prefix_len = cp2 - meta_prefix;
716		else
717		    meta_prefix_len = strlen(meta_prefix);
718	    }
719	    if (strncmp(cp, meta_prefix, meta_prefix_len) == 0) {
720		cp = strchr(cp+1, '\n');
721		if (!cp++)
722		    return;
723	    }
724	}
725	fprintf(pbm->mfp, "%s%s", cp, nl);
726    }
727}
728
729void
730meta_cmd_finish(void *pbmp)
731{
732#ifdef USE_FILEMON
733    BuildMon *pbm = pbmp;
734
735    if (!pbm)
736	pbm = &Mybm;
737
738    if (pbm->filemon_fd >= 0) {
739	close(pbm->filemon_fd);
740	filemon_read(pbm->mfp, pbm->mon_fd);
741	pbm->filemon_fd = pbm->mon_fd = -1;
742    }
743#endif
744}
745
746void
747meta_job_finish(Job *job)
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	meta_cmd_finish(pbm);
758	fclose(pbm->mfp);
759	pbm->mfp = NULL;
760	pbm->meta_fname[0] = '\0';
761    }
762}
763
764/*
765 * Fetch a full line from fp - growing bufp if needed
766 * Return length in bufp.
767 */
768static int
769fgetLine(char **bufp, size_t *szp, int o, FILE *fp)
770{
771    char *buf = *bufp;
772    size_t bufsz = *szp;
773    struct stat fs;
774    int x;
775
776    if (fgets(&buf[o], bufsz - o, fp) != NULL) {
777    check_newline:
778	x = o + strlen(&buf[o]);
779	if (buf[x - 1] == '\n')
780	    return x;
781	/*
782	 * We need to grow the buffer.
783	 * The meta file can give us a clue.
784	 */
785	if (fstat(fileno(fp), &fs) == 0) {
786	    size_t newsz;
787	    char *p;
788
789	    newsz = ROUNDUP((fs.st_size / 2), BUFSIZ);
790	    if (newsz <= bufsz)
791		newsz = ROUNDUP(fs.st_size, BUFSIZ);
792	    if (DEBUG(META))
793		fprintf(debug_file, "growing buffer %u -> %u\n",
794			(unsigned)bufsz, (unsigned)newsz);
795	    p = bmake_realloc(buf, newsz);
796	    if (p) {
797		*bufp = buf = p;
798		*szp = bufsz = newsz;
799		/* fetch the rest */
800		if (!fgets(&buf[x], bufsz - x, fp))
801		    return x;		/* truncated! */
802		goto check_newline;
803	    }
804	}
805    }
806    return 0;
807}
808
809static int
810prefix_match(void *p, void *q)
811{
812    const char *prefix = p;
813    const char *path = q;
814    size_t n = strlen(prefix);
815
816    return (0 == strncmp(path, prefix, n));
817}
818
819static int
820string_match(const void *p, const void *q)
821{
822    const char *p1 = p;
823    const char *p2 = q;
824
825    return strcmp(p1, p2);
826}
827
828
829/*
830 * When running with 'meta' functionality, a target can be out-of-date
831 * if any of the references in it's meta data file is more recent.
832 * We have to track the latestdir on a per-process basis.
833 */
834#define LDIR_VNAME_FMT ".meta.%d.ldir"
835
836/*
837 * It is possible that a .meta file is corrupted,
838 * if we detect this we want to reproduce it.
839 * Setting oodate TRUE will have that effect.
840 */
841#define CHECK_VALID_META(p) if (!(p && *p)) { \
842    warnx("%s: %d: malformed", fname, lineno); \
843    oodate = TRUE; \
844    continue; \
845    }
846
847Boolean
848meta_oodate(GNode *gn, Boolean oodate)
849{
850    static char *tmpdir = NULL;
851    static char cwd[MAXPATHLEN];
852    char ldir_vname[64];
853    char latestdir[MAXPATHLEN];
854    char fname[MAXPATHLEN];
855    char fname1[MAXPATHLEN];
856    char fname2[MAXPATHLEN];
857    char *p;
858    char *cp;
859    static size_t cwdlen = 0;
860    static size_t tmplen = 0;
861    FILE *fp;
862    Boolean needOODATE = FALSE;
863    Lst missingFiles;
864
865    if (oodate)
866	return oodate;		/* we're done */
867
868    missingFiles = Lst_Init(FALSE);
869
870    /*
871     * We need to check if the target is out-of-date. This includes
872     * checking if the expanded command has changed. This in turn
873     * requires that all variables are set in the same way that they
874     * would be if the target needs to be re-built.
875     */
876    Make_DoAllVar(gn);
877
878    meta_name(gn, fname, sizeof(fname), NULL, NULL);
879
880#ifdef DEBUG_META_MODE
881    if (DEBUG(META))
882	fprintf(debug_file, "meta_oodate: %s\n", fname);
883#endif
884
885    if ((fp = fopen(fname, "r")) != NULL) {
886	static char *buf = NULL;
887	static size_t bufsz;
888	int lineno = 0;
889	int lastpid = 0;
890	int pid;
891	int f = 0;
892	int x;
893	LstNode ln;
894	struct stat fs;
895
896	if (!buf) {
897	    bufsz = 8 * BUFSIZ;
898	    buf = bmake_malloc(bufsz);
899	}
900
901	if (!cwdlen) {
902	    if (getcwd(cwd, sizeof(cwd)) == NULL)
903		err(1, "Could not get current working directory");
904	    cwdlen = strlen(cwd);
905	}
906
907	if (!tmpdir) {
908	    tmpdir = getTmpdir();
909	    tmplen = strlen(tmpdir);
910	}
911
912	/* we want to track all the .meta we read */
913	Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
914
915	ln = Lst_First(gn->commands);
916	while (!oodate && (x = fgetLine(&buf, &bufsz, 0, fp)) > 0) {
917	    lineno++;
918	    if (buf[x - 1] == '\n')
919		buf[x - 1] = '\0';
920	    else {
921		warnx("%s: %d: line truncated at %u", fname, lineno, x);
922		oodate = TRUE;
923		break;
924	    }
925	    /* Find the start of the build monitor section. */
926	    if (!f) {
927		if (strncmp(buf, "-- filemon", 10) == 0) {
928		    f = 1;
929		    continue;
930		}
931		if (strncmp(buf, "# buildmon", 10) == 0) {
932		    f = 1;
933		    continue;
934		}
935	    }
936
937	    /* Delimit the record type. */
938	    p = buf;
939#ifdef DEBUG_META_MODE
940	    if (DEBUG(META))
941		fprintf(debug_file, "%s: %d: %s\n", fname, lineno, buf);
942#endif
943	    strsep(&p, " ");
944	    if (f) {
945		/*
946		 * We are in the 'filemon' output section.
947		 * Each record from filemon follows the general form:
948		 *
949		 * <key> <pid> <data>
950		 *
951		 * Where:
952		 * <key> is a single letter, denoting the syscall.
953		 * <pid> is the process that made the syscall.
954		 * <data> is the arguments (of interest).
955		 */
956		switch(buf[0]) {
957		case '#':		/* comment */
958		case 'V':		/* version */
959		    break;
960		default:
961		    /*
962		     * We need to track pathnames per-process.
963		     *
964		     * Each process run by make, starts off in the 'CWD'
965		     * recorded in the .meta file, if it chdirs ('C')
966		     * elsewhere we need to track that - but only for
967		     * that process.  If it forks ('F'), we initialize
968		     * the child to have the same cwd as its parent.
969		     *
970		     * We also need to track the 'latestdir' of
971		     * interest.  This is usually the same as cwd, but
972		     * not if a process is reading directories.
973		     *
974		     * Each time we spot a different process ('pid')
975		     * we save the current value of 'latestdir' in a
976		     * variable qualified by 'lastpid', and
977		     * re-initialize 'latestdir' to any pre-saved
978		     * value for the current 'pid' and 'CWD' if none.
979		     */
980		    CHECK_VALID_META(p);
981		    pid = atoi(p);
982		    if (pid > 0 && pid != lastpid) {
983			char *ldir;
984			char *tp;
985
986			if (lastpid > 0) {
987			    /* We need to remember this. */
988			    Var_Set(ldir_vname, latestdir, VAR_GLOBAL, 0);
989			}
990			snprintf(ldir_vname, sizeof(ldir_vname), LDIR_VNAME_FMT, pid);
991			lastpid = pid;
992			ldir = Var_Value(ldir_vname, VAR_GLOBAL, &tp);
993			if (ldir) {
994			    strlcpy(latestdir, ldir, sizeof(latestdir));
995			    if (tp)
996				free(tp);
997			} else
998			    strlcpy(latestdir, cwd, sizeof(latestdir));
999		    }
1000		    /* Skip past the pid. */
1001		    if (strsep(&p, " ") == NULL)
1002			continue;
1003#ifdef DEBUG_META_MODE
1004		    if (DEBUG(META))
1005			fprintf(debug_file, "%s: %d: cwd=%s ldir=%s\n", fname, lineno, cwd, latestdir);
1006#endif
1007		    break;
1008		}
1009
1010		CHECK_VALID_META(p);
1011
1012		/* Process according to record type. */
1013		switch (buf[0]) {
1014		case 'X':		/* eXit */
1015		    Var_Delete(ldir_vname, VAR_GLOBAL);
1016		    lastpid = 0;	/* no need to save ldir_vname */
1017		    break;
1018
1019		case 'F':		/* [v]Fork */
1020		    {
1021			char cldir[64];
1022			int child;
1023
1024			child = atoi(p);
1025			if (child > 0) {
1026			    snprintf(cldir, sizeof(cldir), LDIR_VNAME_FMT, child);
1027			    Var_Set(cldir, latestdir, VAR_GLOBAL, 0);
1028			}
1029		    }
1030		    break;
1031
1032		case 'C':		/* Chdir */
1033		    /* Update the latest directory. */
1034		    strlcpy(latestdir, p, sizeof(latestdir));
1035		    break;
1036
1037		case 'M':		/* renaMe */
1038		    if (Lst_IsEmpty(missingFiles))
1039			break;
1040		    /* 'L' and 'M' put single quotes around the args */
1041		    if (*p == '\'') {
1042			char *ep;
1043
1044			p++;
1045			if ((ep = strchr(p, '\'')))
1046			    *ep = '\0';
1047		    }
1048		    /* FALLTHROUGH */
1049		case 'D':		/* unlink */
1050		    if (*p == '/' && !Lst_IsEmpty(missingFiles)) {
1051			/* remove p from the missingFiles list if present */
1052			if ((ln = Lst_Find(missingFiles, p, string_match)) != NULL) {
1053			    char *tp = Lst_Datum(ln);
1054			    Lst_Remove(missingFiles, ln);
1055			    free(tp);
1056			    ln = NULL;	/* we're done with it */
1057			}
1058		    }
1059		    break;
1060		case 'L':		/* Link */
1061		    /* we want the target */
1062		    if (strsep(&p, " ") == NULL)
1063			continue;
1064		    CHECK_VALID_META(p);
1065		    /* 'L' and 'M' put single quotes around the args */
1066		    if (*p == '\'') {
1067			char *ep;
1068
1069			p++;
1070			if ((ep = strchr(p, '\'')))
1071			    *ep = '\0';
1072		    }
1073		    /* FALLTHROUGH */
1074		case 'W':		/* Write */
1075		    /*
1076		     * If a file we generated within our bailiwick
1077		     * but outside of .OBJDIR is missing,
1078		     * we need to do it again.
1079		     */
1080		    /* ignore non-absolute paths */
1081		    if (*p != '/')
1082			break;
1083
1084		    if (Lst_IsEmpty(metaBailiwick))
1085			break;
1086
1087		    /* ignore cwd - normal dependencies handle those */
1088		    if (strncmp(p, cwd, cwdlen) == 0)
1089			break;
1090
1091		    if (!Lst_ForEach(metaBailiwick, prefix_match, p))
1092			break;
1093
1094		    /* tmpdir might be within */
1095		    if (tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0)
1096			break;
1097
1098		    /* ignore anything containing the string "tmp" */
1099		    if ((strstr("tmp", p)))
1100			break;
1101
1102		    if (stat(p, &fs) < 0) {
1103			Lst_AtEnd(missingFiles, bmake_strdup(p));
1104		    }
1105		    break;
1106		case 'R':		/* Read */
1107		case 'E':		/* Exec */
1108		    /*
1109		     * Check for runtime files that can't
1110		     * be part of the dependencies because
1111		     * they are _expected_ to change.
1112		     */
1113		    if (strncmp(p, "/tmp/", 5) == 0 ||
1114			(tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0))
1115			break;
1116
1117		    if (strncmp(p, "/var/", 5) == 0)
1118			break;
1119
1120		    /* Ignore device files. */
1121		    if (strncmp(p, "/dev/", 5) == 0)
1122			break;
1123
1124		    /* Ignore /etc/ files. */
1125		    if (strncmp(p, "/etc/", 5) == 0)
1126			break;
1127
1128		    if ((cp = strrchr(p, '/'))) {
1129			cp++;
1130			/*
1131			 * We don't normally expect to see this,
1132			 * but we do expect it to change.
1133			 */
1134			if (strcmp(cp, makeDependfile) == 0)
1135			    break;
1136		    }
1137
1138		    /*
1139		     * The rest of the record is the file name.
1140		     * Check if it's not an absolute path.
1141		     */
1142		    {
1143			char *sdirs[4];
1144			char **sdp;
1145			int sdx = 0;
1146			int found = 0;
1147
1148			if (*p == '/') {
1149			    sdirs[sdx++] = p; /* done */
1150			} else {
1151			    if (strcmp(".", p) == 0)
1152				continue;  /* no point */
1153
1154			    /* Check vs latestdir */
1155			    snprintf(fname1, sizeof(fname1), "%s/%s", latestdir, p);
1156			    sdirs[sdx++] = fname1;
1157
1158			    if (strcmp(latestdir, cwd) != 0) {
1159				/* Check vs cwd */
1160				snprintf(fname2, sizeof(fname2), "%s/%s", cwd, p);
1161				sdirs[sdx++] = fname2;
1162			    }
1163			}
1164			sdirs[sdx++] = NULL;
1165
1166			for (sdp = sdirs; *sdp && !found; sdp++) {
1167#ifdef DEBUG_META_MODE
1168			    if (DEBUG(META))
1169				fprintf(debug_file, "%s: %d: looking for: %s\n", fname, lineno, *sdp);
1170#endif
1171			    if (stat(*sdp, &fs) == 0) {
1172				found = 1;
1173				p = *sdp;
1174			    }
1175			}
1176			if (found) {
1177#ifdef DEBUG_META_MODE
1178			    if (DEBUG(META))
1179				fprintf(debug_file, "%s: %d: found: %s\n", fname, lineno, p);
1180#endif
1181			    if (!S_ISDIR(fs.st_mode) &&
1182				fs.st_mtime > gn->mtime) {
1183				if (DEBUG(META))
1184				    fprintf(debug_file, "%s: %d: file '%s' is newer than the target...\n", fname, lineno, p);
1185				oodate = TRUE;
1186			    } else if (S_ISDIR(fs.st_mode)) {
1187				/* Update the latest directory. */
1188				realpath(p, latestdir);
1189			    }
1190			} else if (errno == ENOENT && *p == '/' &&
1191				   strncmp(p, cwd, cwdlen) != 0) {
1192			    /*
1193			     * A referenced file outside of CWD is missing.
1194			     * We cannot catch every eventuality here...
1195			     */
1196			    if (DEBUG(META))
1197				fprintf(debug_file, "%s: %d: file '%s' may have moved?...\n", fname, lineno, p);
1198			    oodate = TRUE;
1199			}
1200		    }
1201		    break;
1202		default:
1203		    break;
1204		}
1205	    } else if (strcmp(buf, "CMD") == 0) {
1206		/*
1207		 * Compare the current command with the one in the
1208		 * meta data file.
1209		 */
1210		if (ln == NULL) {
1211		    if (DEBUG(META))
1212			fprintf(debug_file, "%s: %d: there were more build commands in the meta data file than there are now...\n", fname, lineno);
1213		    oodate = TRUE;
1214		} else {
1215		    char *cmd = (char *)Lst_Datum(ln);
1216		    Boolean hasOODATE = FALSE;
1217
1218		    if (strstr(cmd, "$?"))
1219			hasOODATE = TRUE;
1220		    else if ((cp = strstr(cmd, ".OODATE"))) {
1221			/* check for $[{(].OODATE[:)}] */
1222			if (cp > cmd + 2 && cp[-2] == '$')
1223			    hasOODATE = TRUE;
1224		    }
1225		    if (hasOODATE) {
1226			needOODATE = TRUE;
1227			if (DEBUG(META))
1228			    fprintf(debug_file, "%s: %d: cannot compare command using .OODATE\n", fname, lineno);
1229		    }
1230		    cmd = Var_Subst(NULL, cmd, gn, TRUE);
1231
1232		    if ((cp = strchr(cmd, '\n'))) {
1233			int n;
1234
1235			/*
1236			 * This command contains newlines, we need to
1237			 * fetch more from the .meta file before we
1238			 * attempt a comparison.
1239			 */
1240			/* first put the newline back at buf[x - 1] */
1241			buf[x - 1] = '\n';
1242			do {
1243			    /* now fetch the next line */
1244			    if ((n = fgetLine(&buf, &bufsz, x, fp)) <= 0)
1245				break;
1246			    x = n;
1247			    lineno++;
1248			    if (buf[x - 1] != '\n') {
1249				warnx("%s: %d: line truncated at %u", fname, lineno, x);
1250				break;
1251			    }
1252			    cp = strchr(++cp, '\n');
1253			} while (cp);
1254			if (buf[x - 1] == '\n')
1255			    buf[x - 1] = '\0';
1256		    }
1257		    if (!hasOODATE &&
1258			!(gn->type & OP_NOMETA_CMP) &&
1259			strcmp(p, cmd) != 0) {
1260			if (DEBUG(META))
1261			    fprintf(debug_file, "%s: %d: a build command has changed\n%s\nvs\n%s\n", fname, lineno, p, cmd);
1262			if (!metaIgnoreCMDs)
1263			    oodate = TRUE;
1264		    }
1265		    free(cmd);
1266		    ln = Lst_Succ(ln);
1267		}
1268	    } else if (strcmp(buf, "CWD") == 0) {
1269		/*
1270		 * Check if there are extra commands now
1271		 * that weren't in the meta data file.
1272		 */
1273		if (!oodate && ln != NULL) {
1274		    if (DEBUG(META))
1275			fprintf(debug_file, "%s: %d: there are extra build commands now that weren't in the meta data file\n", fname, lineno);
1276		    oodate = TRUE;
1277		}
1278		if (strcmp(p, cwd) != 0) {
1279		    if (DEBUG(META))
1280			fprintf(debug_file, "%s: %d: the current working directory has changed from '%s' to '%s'\n", fname, lineno, p, curdir);
1281		    oodate = TRUE;
1282		}
1283	    }
1284	}
1285
1286	fclose(fp);
1287	if (!Lst_IsEmpty(missingFiles)) {
1288	    if (DEBUG(META))
1289		fprintf(debug_file, "%s: missing files: %s...\n",
1290			fname, (char *)Lst_Datum(Lst_First(missingFiles)));
1291	    oodate = TRUE;
1292	    Lst_Destroy(missingFiles, (FreeProc *)free);
1293	}
1294    } else {
1295	if ((gn->type & OP_META)) {
1296	    if (DEBUG(META))
1297		fprintf(debug_file, "%s: required but missing\n", fname);
1298	    oodate = TRUE;
1299	}
1300    }
1301    if (oodate && needOODATE) {
1302	/*
1303	 * Target uses .OODATE which is empty; or we wouldn't be here.
1304	 * We have decided it is oodate, so .OODATE needs to be set.
1305	 * All we can sanely do is set it to .ALLSRC.
1306	 */
1307	Var_Delete(OODATE, gn);
1308	Var_Set(OODATE, Var_Value(ALLSRC, gn, &cp), gn, 0);
1309	if (cp)
1310	    free(cp);
1311    }
1312    return oodate;
1313}
1314
1315/* support for compat mode */
1316
1317static int childPipe[2];
1318
1319void
1320meta_compat_start(void)
1321{
1322#ifdef USE_FILEMON_ONCE
1323    /*
1324     * We need to re-open filemon for each cmd.
1325     */
1326    BuildMon *pbm = &Mybm;
1327
1328    if (pbm->mfp != NULL && useFilemon) {
1329	filemon_open(pbm);
1330    } else {
1331	pbm->mon_fd = pbm->filemon_fd = -1;
1332    }
1333#endif
1334    if (pipe(childPipe) < 0)
1335	Punt("Cannot create pipe: %s", strerror(errno));
1336    /* Set close-on-exec flag for both */
1337    (void)fcntl(childPipe[0], F_SETFD, 1);
1338    (void)fcntl(childPipe[1], F_SETFD, 1);
1339}
1340
1341void
1342meta_compat_child(void)
1343{
1344    meta_job_child(NULL);
1345    if (dup2(childPipe[1], 1) < 0 ||
1346	dup2(1, 2) < 0) {
1347	execError("dup2", "pipe");
1348	_exit(1);
1349    }
1350}
1351
1352void
1353meta_compat_parent(void)
1354{
1355    FILE *fp;
1356    char buf[BUFSIZ];
1357
1358    close(childPipe[1]);			/* child side */
1359    fp = fdopen(childPipe[0], "r");
1360    while (fgets(buf, sizeof(buf), fp)) {
1361	meta_job_output(NULL, buf, "");
1362	printf("%s", buf);
1363    }
1364    fclose(fp);
1365}
1366
1367#endif	/* USE_META */
1368