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