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