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