1 /* $NetBSD: meta.c,v 1.61 2016/06/07 00:40:00 sjg Exp $ */
4 * Implement 'meta' mode.
5 * Adapted from John Birrell's patches to FreeBSD make.
9 * Copyright (c) 2009-2016, Juniper Networks, Inc.
10 * Portions Copyright (c) 2009, John Birrell.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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.
39 #include <sys/ioctl.h>
42 #elif !defined(HAVE_DIRNAME)
43 char * dirname(char *);
46 #if !defined(HAVE_CONFIG_H) || defined(HAVE_ERR_H)
56 #if !defined(USE_FILEMON) && defined(FILEMON_SET_FD)
60 static BuildMon Mybm
; /* for compat */
61 static Lst metaBailiwick
; /* our scope of control */
62 static char *metaBailiwickStr
; /* string storage for the list */
63 static Lst metaIgnorePaths
; /* paths we deliberately ignore */
64 static char *metaIgnorePathsStr
; /* string storage for the list */
66 #ifndef MAKE_META_IGNORE_PATHS
67 #define MAKE_META_IGNORE_PATHS ".MAKE.META.IGNORE_PATHS"
69 #ifndef MAKE_META_IGNORE_PATTERNS
70 #define MAKE_META_IGNORE_PATTERNS ".MAKE.META.IGNORE_PATTERNS"
73 Boolean useMeta
= FALSE
;
74 static Boolean useFilemon
= FALSE
;
75 static Boolean writeMeta
= FALSE
;
76 static Boolean metaMissing
= FALSE
; /* oodate if missing */
77 static Boolean filemonMissing
= FALSE
; /* oodate if missing */
78 static Boolean metaEnv
= FALSE
; /* don't save env unless asked */
79 static Boolean metaVerbose
= FALSE
;
80 static Boolean metaIgnoreCMDs
= FALSE
; /* ignore CMDs in .meta files */
81 static Boolean metaIgnorePatterns
= FALSE
; /* do we need to do pattern matches */
82 static Boolean metaCurdirOk
= FALSE
; /* write .meta in .CURDIR Ok? */
83 static Boolean metaSilent
= FALSE
; /* if we have a .meta be SILENT */
85 extern Boolean forceJobs
;
86 extern Boolean comatMake
;
87 extern char **environ
;
89 #define MAKE_META_PREFIX ".MAKE.META.PREFIX"
92 # define N2U(n, u) (((n) + ((u) - 1)) / (u))
95 # define ROUNDUP(n, u) (N2U((n), (u)) * (u))
98 #if !defined(HAVE_STRSEP)
99 # define strsep(s, d) stresep((s), (d), 0)
103 * Filemon is a kernel module which snoops certain syscalls.
114 * See meta_oodate below - we mainly care about 'E' and 'R'.
116 * We can still use meta mode without filemon, but
117 * the benefits are more limited.
120 # ifndef _PATH_FILEMON
121 # define _PATH_FILEMON "/dev/filemon"
125 * Open the filemon device.
128 filemon_open(BuildMon
*pbm
)
132 pbm
->mon_fd
= pbm
->filemon_fd
= -1;
136 for (retry
= 5; retry
>= 0; retry
--) {
137 if ((pbm
->filemon_fd
= open(_PATH_FILEMON
, O_RDWR
)) >= 0)
141 if (pbm
->filemon_fd
< 0) {
143 warn("Could not open %s", _PATH_FILEMON
);
148 * We use a file outside of '.'
149 * to avoid a FreeBSD kernel bug where unlink invalidates
150 * cwd causing getcwd to do a lot more work.
151 * We only care about the descriptor.
153 pbm
->mon_fd
= mkTempFile("filemon.XXXXXX", NULL
);
154 if (ioctl(pbm
->filemon_fd
, FILEMON_SET_FD
, &pbm
->mon_fd
) < 0) {
155 err(1, "Could not set filemon file descriptor!");
157 /* we don't need these once we exec */
158 (void)fcntl(pbm
->mon_fd
, F_SETFD
, FD_CLOEXEC
);
159 (void)fcntl(pbm
->filemon_fd
, F_SETFD
, FD_CLOEXEC
);
163 * Read the build monitor output file and write records to the target's
167 filemon_read(FILE *mfp
, int fd
)
173 /* Check if we're not writing to a meta data file.*/
176 close(fd
); /* not interested */
180 (void)lseek(fd
, (off_t
)0, SEEK_SET
);
183 fprintf(mfp
, "\n-- filemon acquired metadata --\n");
185 while ((n
= read(fd
, buf
, sizeof(buf
))) > 0) {
186 if ((int)fwrite(buf
, 1, n
, mfp
) < n
)
197 * when realpath() fails,
198 * we use this, to clean up ./ and ../
201 eat_dots(char *buf
, size_t bufsz
, int dots
)
222 cp
= strstr(buf
, eat
);
225 if (dots
== 2 && cp
> buf
) {
228 } while (cp
> buf
&& *cp
!= '/');
231 strlcpy(cp
, cp2
, bufsz
- (cp
- buf
));
233 return; /* can't happen? */
240 meta_name(struct GNode
*gn
, char *mname
, size_t mnamelen
,
245 char buf
[MAXPATHLEN
];
251 * Weed out relative paths from the target file name.
252 * We have to be careful though since if target is a
253 * symlink, the result will be unstable.
254 * So we use realpath() just to get the dirname, and leave the
255 * basename as given to us.
257 if ((cp
= strrchr(tname
, '/'))) {
258 if (cached_realpath(tname
, buf
)) {
259 if ((rp
= strrchr(buf
, '/'))) {
262 if (strcmp(cp
, rp
) != 0)
263 strlcpy(rp
, cp
, sizeof(buf
) - (rp
- buf
));
268 * We likely have a directory which is about to be made.
269 * We pretend realpath() succeeded, to have a chance
270 * of generating the same meta file name that we will
273 if (tname
[0] == '/') {
274 strlcpy(buf
, tname
, sizeof(buf
));
276 snprintf(buf
, sizeof(buf
), "%s/%s", cwd
, tname
);
278 eat_dots(buf
, sizeof(buf
), 1); /* ./ */
279 eat_dots(buf
, sizeof(buf
), 2); /* ../ */
283 /* on some systems dirname may modify its arg */
284 tp
= bmake_strdup(tname
);
285 if (strcmp(dname
, dirname(tp
)) == 0)
286 snprintf(mname
, mnamelen
, "%s.meta", tname
);
288 snprintf(mname
, mnamelen
, "%s/%s.meta", dname
, tname
);
291 * Replace path separators in the file name after the
292 * current object directory path.
294 cp
= mname
+ strlen(dname
) + 1;
296 while (*cp
!= '\0') {
307 * Return true if running ${.MAKE}
308 * Bypassed if target is flagged .MAKE
311 is_submake(void *cmdp
, void *gnp
)
313 static char *p_make
= NULL
;
320 int rc
= 0; /* keep looking */
323 p_make
= Var_Value(".MAKE", gn
, &cp
);
324 p_len
= strlen(p_make
);
326 cp
= strchr(cmd
, '$');
328 mp
= Var_Subst(NULL
, cmd
, gn
, VARF_WANTRES
);
331 cp2
= strstr(cmd
, p_make
);
333 switch (cp2
[p_len
]) {
341 if (cp2
> cmd
&& rc
> 0) {
348 rc
= 0; /* no match */
357 typedef struct meta_file_s
{
363 printCMD(void *cmdp
, void *mfpp
)
365 meta_file_t
*mfp
= mfpp
;
369 if (strchr(cmd
, '$')) {
370 cmd
= cp
= Var_Subst(NULL
, cmd
, mfp
->gn
, VARF_WANTRES
);
372 fprintf(mfp
->fp
, "CMD %s\n", cmd
);
378 * Certain node types never get a .meta file
380 #define SKIP_META_TYPE(_type) do { \
381 if ((gn->type & __CONCAT(OP_, _type))) { \
383 fprintf(debug_file, "Skipping meta for %s: .%s\n", \
384 gn->name, __STRING(_type)); \
392 * Do we need/want a .meta file ?
395 meta_needed(GNode
*gn
, const char *dname
, const char *tname
,
396 char *objdir
, int verbose
)
401 verbose
= DEBUG(META
);
403 /* This may be a phony node which we don't want meta data for... */
404 /* Skip .meta for .BEGIN, .END, .ERROR etc as well. */
405 /* Or it may be explicitly flagged as .NOMETA */
406 SKIP_META_TYPE(NOMETA
);
407 /* Unless it is explicitly flagged as .META */
408 if (!(gn
->type
& OP_META
)) {
409 SKIP_META_TYPE(PHONY
);
410 SKIP_META_TYPE(SPECIAL
);
411 SKIP_META_TYPE(MAKE
);
414 /* Check if there are no commands to execute. */
415 if (Lst_IsEmpty(gn
->commands
)) {
417 fprintf(debug_file
, "Skipping meta for %s: no commands\n",
421 if ((gn
->type
& (OP_META
|OP_SUBMAKE
)) == OP_SUBMAKE
) {
422 /* OP_SUBMAKE is a bit too aggressive */
423 if (Lst_ForEach(gn
->commands
, is_submake
, gn
)) {
425 fprintf(debug_file
, "Skipping meta for %s: .SUBMAKE\n",
431 /* The object directory may not exist. Check it.. */
432 if (cached_stat(dname
, &fs
) != 0) {
434 fprintf(debug_file
, "Skipping meta for %s: no .OBJDIR\n",
439 /* make sure these are canonical */
440 if (cached_realpath(dname
, objdir
))
443 /* If we aren't in the object directory, don't create a meta file. */
444 if (!metaCurdirOk
&& strcmp(curdir
, dname
) == 0) {
446 fprintf(debug_file
, "Skipping meta for %s: .OBJDIR == .CURDIR\n",
455 meta_create(BuildMon
*pbm
, GNode
*gn
)
458 char buf
[MAXPATHLEN
];
459 char objdir
[MAXPATHLEN
];
465 char *p
[4]; /* >= possible uses */
471 dname
= Var_Value(".OBJDIR", gn
, &p
[i
++]);
472 tname
= Var_Value(TARGET
, gn
, &p
[i
++]);
474 /* if this succeeds objdir is realpath of dname */
475 if (!meta_needed(gn
, dname
, tname
, objdir
, TRUE
))
482 /* Describe the target we are building */
483 mp
= Var_Subst(NULL
, "${" MAKE_META_PREFIX
"}", gn
, VARF_WANTRES
);
485 fprintf(stdout
, "%s\n", mp
);
488 /* Get the basename of the target */
489 if ((cp
= strrchr(tname
, '/')) == NULL
) {
498 /* Don't create meta data. */
501 fname
= meta_name(gn
, pbm
->meta_fname
, sizeof(pbm
->meta_fname
),
502 dname
, tname
, objdir
);
504 #ifdef DEBUG_META_MODE
506 fprintf(debug_file
, "meta_create: %s\n", fname
);
509 if ((mf
.fp
= fopen(fname
, "w")) == NULL
)
510 err(1, "Could not open meta file '%s'", fname
);
512 fprintf(mf
.fp
, "# Meta data file %s\n", fname
);
516 Lst_ForEach(gn
->commands
, printCMD
, &mf
);
518 fprintf(mf
.fp
, "CWD %s\n", getcwd(buf
, sizeof(buf
)));
519 fprintf(mf
.fp
, "TARGET %s\n", tname
);
522 for (ptr
= environ
; *ptr
!= NULL
; ptr
++)
523 fprintf(mf
.fp
, "ENV %s\n", *ptr
);
526 fprintf(mf
.fp
, "-- command output --\n");
529 Var_Append(".MAKE.META.FILES", fname
, VAR_GLOBAL
);
530 Var_Append(".MAKE.META.CREATED", fname
, VAR_GLOBAL
);
532 gn
->type
|= OP_META
; /* in case anyone wants to know */
534 gn
->type
|= OP_SILENT
;
537 for (i
--; i
>= 0; i
--) {
559 * Initialization we need before reading makefiles.
565 /* this allows makefiles to test if we have filemon support */
566 Var_Set(".MAKE.PATH_FILEMON", _PATH_FILEMON
, VAR_GLOBAL
, 0);
571 #define get_mode_bf(bf, token) \
572 if ((cp = strstr(make_mode, token))) \
573 bf = boolValue(&cp[sizeof(token) - 1])
576 * Initialization we need after reading makefiles.
579 meta_mode_init(const char *make_mode
)
589 if (strstr(make_mode
, "env"))
591 if (strstr(make_mode
, "verb"))
593 if (strstr(make_mode
, "read"))
595 if (strstr(make_mode
, "nofilemon"))
597 if (strstr(make_mode
, "ignore-cmd"))
598 metaIgnoreCMDs
= TRUE
;
600 get_mode_bf(filemonMissing
, "missing-filemon=");
601 get_mode_bf(metaCurdirOk
, "curdirok=");
602 get_mode_bf(metaMissing
, "missing-meta=");
603 get_mode_bf(metaSilent
, "silent=");
605 if (metaVerbose
&& !Var_Exists(MAKE_META_PREFIX
, VAR_GLOBAL
)) {
607 * The default value for MAKE_META_PREFIX
608 * prints the absolute path of the target.
609 * This works be cause :H will generate '.' if there is no /
610 * and :tA will resolve that to cwd.
612 Var_Set(MAKE_META_PREFIX
, "Building ${.TARGET:H:tA}/${.TARGET:T}", VAR_GLOBAL
, 0);
617 memset(&Mybm
, 0, sizeof(Mybm
));
619 * We consider ourselves master of all within ${.MAKE.META.BAILIWICK}
621 metaBailiwick
= Lst_Init(FALSE
);
622 metaBailiwickStr
= Var_Subst(NULL
, "${.MAKE.META.BAILIWICK:O:u:tA}",
623 VAR_GLOBAL
, VARF_WANTRES
);
624 if (metaBailiwickStr
) {
625 str2Lst_Append(metaBailiwick
, metaBailiwickStr
, NULL
);
628 * We ignore any paths that start with ${.MAKE.META.IGNORE_PATHS}
630 metaIgnorePaths
= Lst_Init(FALSE
);
631 Var_Append(MAKE_META_IGNORE_PATHS
,
632 "/dev /etc /proc /tmp /var/run /var/tmp ${TMPDIR}", VAR_GLOBAL
);
633 metaIgnorePathsStr
= Var_Subst(NULL
,
634 "${" MAKE_META_IGNORE_PATHS
":O:u:tA}", VAR_GLOBAL
,
636 if (metaIgnorePathsStr
) {
637 str2Lst_Append(metaIgnorePaths
, metaIgnorePathsStr
, NULL
);
641 * We ignore any paths that match ${.MAKE.META.IGNORE_PATTERNS}
644 if (Var_Value(MAKE_META_IGNORE_PATTERNS
, VAR_GLOBAL
, &cp
)) {
645 metaIgnorePatterns
= TRUE
;
651 * In each case below we allow for job==NULL
654 meta_job_start(Job
*job
, GNode
*gn
)
663 pbm
->mfp
= meta_create(pbm
, gn
);
664 #ifdef USE_FILEMON_ONCE
665 /* compat mode we open the filemon dev once per command */
670 if (pbm
->mfp
!= NULL
&& useFilemon
) {
673 pbm
->mon_fd
= pbm
->filemon_fd
= -1;
679 * The child calls this before doing anything.
680 * It does not disturb our state.
683 meta_job_child(Job
*job
)
693 if (pbm
->mfp
!= NULL
) {
694 close(fileno(pbm
->mfp
));
699 if (ioctl(pbm
->filemon_fd
, FILEMON_SET_PID
, &pid
) < 0) {
700 err(1, "Could not set filemon pid!");
708 meta_job_error(Job
*job
, GNode
*gn
, int flags
, int status
)
710 char cwd
[MAXPATHLEN
];
720 if (pbm
->mfp
!= NULL
) {
721 fprintf(pbm
->mfp
, "*** Error code %d%s\n",
723 (flags
& JOB_IGNERR
) ?
727 Var_Set(".ERROR_TARGET", gn
->path
? gn
->path
: gn
->name
, VAR_GLOBAL
, 0);
729 getcwd(cwd
, sizeof(cwd
));
730 Var_Set(".ERROR_CWD", cwd
, VAR_GLOBAL
, 0);
731 if (pbm
->meta_fname
[0]) {
732 Var_Set(".ERROR_META_FILE", pbm
->meta_fname
, VAR_GLOBAL
, 0);
734 meta_job_finish(job
);
738 meta_job_output(Job
*job
, char *cp
, const char *nl
)
747 if (pbm
->mfp
!= NULL
) {
749 static char *meta_prefix
= NULL
;
750 static int meta_prefix_len
;
755 meta_prefix
= Var_Subst(NULL
, "${" MAKE_META_PREFIX
"}",
756 VAR_GLOBAL
, VARF_WANTRES
);
757 if ((cp2
= strchr(meta_prefix
, '$')))
758 meta_prefix_len
= cp2
- meta_prefix
;
760 meta_prefix_len
= strlen(meta_prefix
);
762 if (strncmp(cp
, meta_prefix
, meta_prefix_len
) == 0) {
763 cp
= strchr(cp
+1, '\n');
768 fprintf(pbm
->mfp
, "%s%s", cp
, nl
);
773 meta_cmd_finish(void *pbmp
)
777 BuildMon
*pbm
= pbmp
;
783 if (pbm
->filemon_fd
>= 0) {
784 if (close(pbm
->filemon_fd
) < 0)
786 x
= filemon_read(pbm
->mfp
, pbm
->mon_fd
);
787 if (error
== 0 && x
!= 0)
789 pbm
->filemon_fd
= pbm
->mon_fd
= -1;
796 meta_job_finish(Job
*job
)
807 if (pbm
->mfp
!= NULL
) {
808 error
= meta_cmd_finish(pbm
);
809 x
= fclose(pbm
->mfp
);
810 if (error
== 0 && x
!= 0)
813 pbm
->meta_fname
[0] = '\0';
821 Lst_Destroy(metaBailiwick
, NULL
);
822 free(metaBailiwickStr
);
823 Lst_Destroy(metaIgnorePaths
, NULL
);
824 free(metaIgnorePathsStr
);
828 * Fetch a full line from fp - growing bufp if needed
829 * Return length in bufp.
832 fgetLine(char **bufp
, size_t *szp
, int o
, FILE *fp
)
839 if (fgets(&buf
[o
], bufsz
- o
, fp
) != NULL
) {
841 x
= o
+ strlen(&buf
[o
]);
842 if (buf
[x
- 1] == '\n')
845 * We need to grow the buffer.
846 * The meta file can give us a clue.
848 if (fstat(fileno(fp
), &fs
) == 0) {
852 newsz
= ROUNDUP((fs
.st_size
/ 2), BUFSIZ
);
854 newsz
= ROUNDUP(fs
.st_size
, BUFSIZ
);
856 fprintf(debug_file
, "growing buffer %u -> %u\n",
857 (unsigned)bufsz
, (unsigned)newsz
);
858 p
= bmake_realloc(buf
, newsz
);
861 *szp
= bufsz
= newsz
;
863 if (!fgets(&buf
[x
], bufsz
- x
, fp
))
864 return x
; /* truncated! */
873 prefix_match(void *p
, void *q
)
875 const char *prefix
= p
;
876 const char *path
= q
;
877 size_t n
= strlen(prefix
);
879 return (0 == strncmp(path
, prefix
, n
));
883 string_match(const void *p
, const void *q
)
888 return strcmp(p1
, p2
);
893 * When running with 'meta' functionality, a target can be out-of-date
894 * if any of the references in its meta data file is more recent.
895 * We have to track the latestdir on a per-process basis.
897 #define LCWD_VNAME_FMT ".meta.%d.lcwd"
898 #define LDIR_VNAME_FMT ".meta.%d.ldir"
901 * It is possible that a .meta file is corrupted,
902 * if we detect this we want to reproduce it.
903 * Setting oodate TRUE will have that effect.
905 #define CHECK_VALID_META(p) if (!(p && *p)) { \
906 warnx("%s: %d: malformed", fname, lineno); \
911 #define DEQUOTE(p) if (*p == '\'') { \
914 if ((ep = strchr(p, '\''))) \
919 meta_oodate(GNode
*gn
, Boolean oodate
)
921 static char *tmpdir
= NULL
;
922 static char cwd
[MAXPATHLEN
];
925 char lcwd
[MAXPATHLEN
];
926 char latestdir
[MAXPATHLEN
];
927 char fname
[MAXPATHLEN
];
928 char fname1
[MAXPATHLEN
];
929 char fname2
[MAXPATHLEN
];
930 char fname3
[MAXPATHLEN
];
937 static size_t cwdlen
= 0;
938 static size_t tmplen
= 0;
940 Boolean needOODATE
= FALSE
;
942 char *pa
[4]; /* >= possible uses */
944 int have_filemon
= FALSE
;
947 return oodate
; /* we're done */
951 dname
= Var_Value(".OBJDIR", gn
, &pa
[i
++]);
952 tname
= Var_Value(TARGET
, gn
, &pa
[i
++]);
954 /* if this succeeds fname3 is realpath of dname */
955 if (!meta_needed(gn
, dname
, tname
, fname3
, FALSE
))
959 missingFiles
= Lst_Init(FALSE
);
962 * We need to check if the target is out-of-date. This includes
963 * checking if the expanded command has changed. This in turn
964 * requires that all variables are set in the same way that they
965 * would be if the target needs to be re-built.
969 meta_name(gn
, fname
, sizeof(fname
), dname
, tname
, dname
);
971 #ifdef DEBUG_META_MODE
973 fprintf(debug_file
, "meta_oodate: %s\n", fname
);
976 if ((fp
= fopen(fname
, "r")) != NULL
) {
977 static char *buf
= NULL
;
988 buf
= bmake_malloc(bufsz
);
992 if (getcwd(cwd
, sizeof(cwd
)) == NULL
)
993 err(1, "Could not get current working directory");
994 cwdlen
= strlen(cwd
);
996 strlcpy(lcwd
, cwd
, sizeof(lcwd
));
997 strlcpy(latestdir
, cwd
, sizeof(latestdir
));
1000 tmpdir
= getTmpdir();
1001 tmplen
= strlen(tmpdir
);
1004 /* we want to track all the .meta we read */
1005 Var_Append(".MAKE.META.FILES", fname
, VAR_GLOBAL
);
1007 ln
= Lst_First(gn
->commands
);
1008 while (!oodate
&& (x
= fgetLine(&buf
, &bufsz
, 0, fp
)) > 0) {
1010 if (buf
[x
- 1] == '\n')
1013 warnx("%s: %d: line truncated at %u", fname
, lineno
, x
);
1019 /* Find the start of the build monitor section. */
1020 if (!have_filemon
) {
1021 if (strncmp(buf
, "-- filemon", 10) == 0) {
1022 have_filemon
= TRUE
;
1025 if (strncmp(buf
, "# buildmon", 10) == 0) {
1026 have_filemon
= TRUE
;
1031 /* Delimit the record type. */
1033 #ifdef DEBUG_META_MODE
1035 fprintf(debug_file
, "%s: %d: %s\n", fname
, lineno
, buf
);
1040 * We are in the 'filemon' output section.
1041 * Each record from filemon follows the general form:
1043 * <key> <pid> <data>
1046 * <key> is a single letter, denoting the syscall.
1047 * <pid> is the process that made the syscall.
1048 * <data> is the arguments (of interest).
1051 case '#': /* comment */
1052 case 'V': /* version */
1056 * We need to track pathnames per-process.
1058 * Each process run by make, starts off in the 'CWD'
1059 * recorded in the .meta file, if it chdirs ('C')
1060 * elsewhere we need to track that - but only for
1061 * that process. If it forks ('F'), we initialize
1062 * the child to have the same cwd as its parent.
1064 * We also need to track the 'latestdir' of
1065 * interest. This is usually the same as cwd, but
1066 * not if a process is reading directories.
1068 * Each time we spot a different process ('pid')
1069 * we save the current value of 'latestdir' in a
1070 * variable qualified by 'lastpid', and
1071 * re-initialize 'latestdir' to any pre-saved
1072 * value for the current 'pid' and 'CWD' if none.
1074 CHECK_VALID_META(p
);
1076 if (pid
> 0 && pid
!= lastpid
) {
1081 /* We need to remember these. */
1082 Var_Set(lcwd_vname
, lcwd
, VAR_GLOBAL
, 0);
1083 Var_Set(ldir_vname
, latestdir
, VAR_GLOBAL
, 0);
1085 snprintf(lcwd_vname
, sizeof(lcwd_vname
), LCWD_VNAME_FMT
, pid
);
1086 snprintf(ldir_vname
, sizeof(ldir_vname
), LDIR_VNAME_FMT
, pid
);
1088 ldir
= Var_Value(ldir_vname
, VAR_GLOBAL
, &tp
);
1090 strlcpy(latestdir
, ldir
, sizeof(latestdir
));
1093 ldir
= Var_Value(lcwd_vname
, VAR_GLOBAL
, &tp
);
1095 strlcpy(lcwd
, ldir
, sizeof(lcwd
));
1099 /* Skip past the pid. */
1100 if (strsep(&p
, " ") == NULL
)
1102 #ifdef DEBUG_META_MODE
1104 fprintf(debug_file
, "%s: %d: %d: %c: cwd=%s lcwd=%s ldir=%s\n",
1106 pid
, buf
[0], cwd
, lcwd
, latestdir
);
1111 CHECK_VALID_META(p
);
1113 /* Process according to record type. */
1115 case 'X': /* eXit */
1116 Var_Delete(lcwd_vname
, VAR_GLOBAL
);
1117 Var_Delete(ldir_vname
, VAR_GLOBAL
);
1118 lastpid
= 0; /* no need to save ldir_vname */
1121 case 'F': /* [v]Fork */
1128 snprintf(cldir
, sizeof(cldir
), LCWD_VNAME_FMT
, child
);
1129 Var_Set(cldir
, lcwd
, VAR_GLOBAL
, 0);
1130 snprintf(cldir
, sizeof(cldir
), LDIR_VNAME_FMT
, child
);
1131 Var_Set(cldir
, latestdir
, VAR_GLOBAL
, 0);
1132 #ifdef DEBUG_META_MODE
1134 fprintf(debug_file
, "%s: %d: %d: cwd=%s lcwd=%s ldir=%s\n",
1136 child
, cwd
, lcwd
, latestdir
);
1142 case 'C': /* Chdir */
1143 /* Update lcwd and latest directory. */
1144 strlcpy(latestdir
, p
, sizeof(latestdir
));
1145 strlcpy(lcwd
, p
, sizeof(lcwd
));
1146 Var_Set(lcwd_vname
, lcwd
, VAR_GLOBAL
, 0);
1147 Var_Set(ldir_vname
, lcwd
, VAR_GLOBAL
, 0);
1148 #ifdef DEBUG_META_MODE
1150 fprintf(debug_file
, "%s: %d: cwd=%s ldir=%s\n", fname
, lineno
, cwd
, lcwd
);
1154 case 'M': /* renaMe */
1156 * For 'M'oves we want to check
1157 * the src as for 'R'ead
1158 * and the target as for 'W'rite.
1160 cp
= p
; /* save this for a second */
1161 /* now get target */
1162 if (strsep(&p
, " ") == NULL
)
1164 CHECK_VALID_META(p
);
1167 /* 'L' and 'M' put single quotes around the args */
1169 DEQUOTE(move_target
);
1171 case 'D': /* unlink */
1172 if (*p
== '/' && !Lst_IsEmpty(missingFiles
)) {
1173 /* remove p from the missingFiles list if present */
1174 if ((ln
= Lst_Find(missingFiles
, p
, string_match
)) != NULL
) {
1175 char *tp
= Lst_Datum(ln
);
1176 Lst_Remove(missingFiles
, ln
);
1178 ln
= NULL
; /* we're done with it */
1181 if (buf
[0] == 'M') {
1182 /* the target of the mv is a file 'W'ritten */
1183 #ifdef DEBUG_META_MODE
1185 fprintf(debug_file
, "meta_oodate: M %s -> %s\n",
1192 case 'L': /* Link */
1195 * the src as for 'R'ead
1196 * and the target as for 'W'rite.
1199 /* now get target */
1200 if (strsep(&p
, " ") == NULL
)
1202 CHECK_VALID_META(p
);
1203 /* 'L' and 'M' put single quotes around the args */
1206 #ifdef DEBUG_META_MODE
1208 fprintf(debug_file
, "meta_oodate: L %s -> %s\n",
1212 case 'W': /* Write */
1215 * If a file we generated within our bailiwick
1216 * but outside of .OBJDIR is missing,
1217 * we need to do it again.
1219 /* ignore non-absolute paths */
1223 if (Lst_IsEmpty(metaBailiwick
))
1226 /* ignore cwd - normal dependencies handle those */
1227 if (strncmp(p
, cwd
, cwdlen
) == 0)
1230 if (!Lst_ForEach(metaBailiwick
, prefix_match
, p
))
1233 /* tmpdir might be within */
1234 if (tmplen
> 0 && strncmp(p
, tmpdir
, tmplen
) == 0)
1237 /* ignore anything containing the string "tmp" */
1238 if ((strstr("tmp", p
)))
1241 if ((link_src
!= NULL
&& cached_lstat(p
, &fs
) < 0) ||
1242 (link_src
== NULL
&& cached_stat(p
, &fs
) < 0)) {
1243 if (Lst_Find(missingFiles
, p
, string_match
) == NULL
)
1244 Lst_AtEnd(missingFiles
, bmake_strdup(p
));
1250 #ifdef DEBUG_META_MODE
1252 fprintf(debug_file
, "meta_oodate: L src %s\n", p
);
1255 case 'R': /* Read */
1256 case 'E': /* Exec */
1258 * Check for runtime files that can't
1259 * be part of the dependencies because
1260 * they are _expected_ to change.
1263 cached_realpath(p
, fname1
); /* clean it up */
1264 if (Lst_ForEach(metaIgnorePaths
, prefix_match
, fname1
)) {
1265 #ifdef DEBUG_META_MODE
1267 fprintf(debug_file
, "meta_oodate: ignoring path: %s\n",
1274 if (metaIgnorePatterns
) {
1277 snprintf(fname1
, sizeof(fname1
),
1278 "${%s:@m@${%s:L:M$m}@}",
1279 MAKE_META_IGNORE_PATTERNS
, p
);
1280 pm
= Var_Subst(NULL
, fname1
, gn
, VARF_WANTRES
);
1282 #ifdef DEBUG_META_MODE
1284 fprintf(debug_file
, "meta_oodate: ignoring pattern: %s\n",
1294 * The rest of the record is the file name.
1295 * Check if it's not an absolute path.
1304 sdirs
[sdx
++] = p
; /* done */
1306 if (strcmp(".", p
) == 0)
1307 continue; /* no point */
1309 /* Check vs latestdir */
1310 snprintf(fname1
, sizeof(fname1
), "%s/%s", latestdir
, p
);
1311 sdirs
[sdx
++] = fname1
;
1313 if (strcmp(latestdir
, lcwd
) != 0) {
1315 snprintf(fname2
, sizeof(fname2
), "%s/%s", lcwd
, p
);
1316 sdirs
[sdx
++] = fname2
;
1318 if (strcmp(lcwd
, cwd
) != 0) {
1320 snprintf(fname3
, sizeof(fname3
), "%s/%s", cwd
, p
);
1321 sdirs
[sdx
++] = fname3
;
1324 sdirs
[sdx
++] = NULL
;
1326 for (sdp
= sdirs
; *sdp
&& !found
; sdp
++) {
1327 #ifdef DEBUG_META_MODE
1329 fprintf(debug_file
, "%s: %d: looking for: %s\n", fname
, lineno
, *sdp
);
1331 if (cached_stat(*sdp
, &fs
) == 0) {
1337 #ifdef DEBUG_META_MODE
1339 fprintf(debug_file
, "%s: %d: found: %s\n", fname
, lineno
, p
);
1341 if (!S_ISDIR(fs
.st_mode
) &&
1342 fs
.st_mtime
> gn
->mtime
) {
1344 fprintf(debug_file
, "%s: %d: file '%s' is newer than the target...\n", fname
, lineno
, p
);
1346 } else if (S_ISDIR(fs
.st_mode
)) {
1347 /* Update the latest directory. */
1348 cached_realpath(p
, latestdir
);
1350 } else if (errno
== ENOENT
&& *p
== '/' &&
1351 strncmp(p
, cwd
, cwdlen
) != 0) {
1353 * A referenced file outside of CWD is missing.
1354 * We cannot catch every eventuality here...
1356 if (Lst_Find(missingFiles
, p
, string_match
) == NULL
)
1357 Lst_AtEnd(missingFiles
, bmake_strdup(p
));
1360 if (buf
[0] == 'E') {
1361 /* previous latestdir is no longer relevant */
1362 strlcpy(latestdir
, lcwd
, sizeof(latestdir
));
1368 if (!oodate
&& buf
[0] == 'L' && link_src
!= NULL
)
1369 goto check_link_src
;
1370 } else if (strcmp(buf
, "CMD") == 0) {
1372 * Compare the current command with the one in the
1377 fprintf(debug_file
, "%s: %d: there were more build commands in the meta data file than there are now...\n", fname
, lineno
);
1380 char *cmd
= (char *)Lst_Datum(ln
);
1381 Boolean hasOODATE
= FALSE
;
1383 if (strstr(cmd
, "$?"))
1385 else if ((cp
= strstr(cmd
, ".OODATE"))) {
1386 /* check for $[{(].OODATE[:)}] */
1387 if (cp
> cmd
+ 2 && cp
[-2] == '$')
1393 fprintf(debug_file
, "%s: %d: cannot compare command using .OODATE\n", fname
, lineno
);
1395 cmd
= Var_Subst(NULL
, cmd
, gn
, VARF_WANTRES
|VARF_UNDEFERR
);
1397 if ((cp
= strchr(cmd
, '\n'))) {
1401 * This command contains newlines, we need to
1402 * fetch more from the .meta file before we
1403 * attempt a comparison.
1405 /* first put the newline back at buf[x - 1] */
1408 /* now fetch the next line */
1409 if ((n
= fgetLine(&buf
, &bufsz
, x
, fp
)) <= 0)
1413 if (buf
[x
- 1] != '\n') {
1414 warnx("%s: %d: line truncated at %u", fname
, lineno
, x
);
1417 cp
= strchr(++cp
, '\n');
1419 if (buf
[x
- 1] == '\n')
1423 !(gn
->type
& OP_NOMETA_CMP
) &&
1424 strcmp(p
, cmd
) != 0) {
1426 fprintf(debug_file
, "%s: %d: a build command has changed\n%s\nvs\n%s\n", fname
, lineno
, p
, cmd
);
1427 if (!metaIgnoreCMDs
)
1433 } else if (strcmp(buf
, "CWD") == 0) {
1435 * Check if there are extra commands now
1436 * that weren't in the meta data file.
1438 if (!oodate
&& ln
!= NULL
) {
1440 fprintf(debug_file
, "%s: %d: there are extra build commands now that weren't in the meta data file\n", fname
, lineno
);
1443 if (strcmp(p
, cwd
) != 0) {
1445 fprintf(debug_file
, "%s: %d: the current working directory has changed from '%s' to '%s'\n", fname
, lineno
, p
, curdir
);
1452 if (!Lst_IsEmpty(missingFiles
)) {
1454 fprintf(debug_file
, "%s: missing files: %s...\n",
1455 fname
, (char *)Lst_Datum(Lst_First(missingFiles
)));
1458 if (!oodate
&& !have_filemon
&& filemonMissing
) {
1460 fprintf(debug_file
, "%s: missing filemon data\n", fname
);
1464 if (writeMeta
&& metaMissing
) {
1467 /* if target is in .CURDIR we do not need a meta file */
1468 if (gn
->path
&& (cp
= strrchr(gn
->path
, '/')) && cp
> gn
->path
) {
1469 if (strncmp(curdir
, gn
->path
, (cp
- gn
->path
)) != 0) {
1470 cp
= NULL
; /* not in .CURDIR */
1475 fprintf(debug_file
, "%s: required but missing\n", fname
);
1481 Lst_Destroy(missingFiles
, (FreeProc
*)free
);
1483 if (oodate
&& needOODATE
) {
1485 * Target uses .OODATE which is empty; or we wouldn't be here.
1486 * We have decided it is oodate, so .OODATE needs to be set.
1487 * All we can sanely do is set it to .ALLSRC.
1489 Var_Delete(OODATE
, gn
);
1490 Var_Set(OODATE
, Var_Value(ALLSRC
, gn
, &cp
), gn
, 0);
1495 for (i
--; i
>= 0; i
--) {
1501 /* support for compat mode */
1503 static int childPipe
[2];
1506 meta_compat_start(void)
1508 #ifdef USE_FILEMON_ONCE
1510 * We need to re-open filemon for each cmd.
1512 BuildMon
*pbm
= &Mybm
;
1514 if (pbm
->mfp
!= NULL
&& useFilemon
) {
1517 pbm
->mon_fd
= pbm
->filemon_fd
= -1;
1520 if (pipe(childPipe
) < 0)
1521 Punt("Cannot create pipe: %s", strerror(errno
));
1522 /* Set close-on-exec flag for both */
1523 (void)fcntl(childPipe
[0], F_SETFD
, FD_CLOEXEC
);
1524 (void)fcntl(childPipe
[1], F_SETFD
, FD_CLOEXEC
);
1528 meta_compat_child(void)
1530 meta_job_child(NULL
);
1531 if (dup2(childPipe
[1], 1) < 0 ||
1533 execError("dup2", "pipe");
1539 meta_compat_parent(void)
1544 close(childPipe
[1]); /* child side */
1545 fp
= fdopen(childPipe
[0], "r");
1546 while (fgets(buf
, sizeof(buf
), fp
)) {
1547 meta_job_output(NULL
, buf
, "");
1553 #endif /* USE_META */