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