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