meta.c revision 1.16 1 /* $NetBSD: meta.c,v 1.16 2011/03/31 06:50:43 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 meta_job_finish(job);
660 }
661
662 void
663 meta_job_output(Job *job, char *cp, const char *nl)
664 {
665 BuildMon *pbm;
666
667 if (job != NULL) {
668 pbm = &job->bm;
669 } else {
670 pbm = &Mybm;
671 }
672 if (pbm->mfp != NULL) {
673 if (metaVerbose) {
674 static char *meta_prefix = NULL;
675 static int meta_prefix_len;
676
677 if (!meta_prefix) {
678 char *cp2;
679
680 meta_prefix = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", VAR_GLOBAL, 0);
681 if ((cp2 = strchr(meta_prefix, '$')))
682 meta_prefix_len = cp2 - meta_prefix;
683 else
684 meta_prefix_len = strlen(meta_prefix);
685 }
686 if (strncmp(cp, meta_prefix, meta_prefix_len) == 0) {
687 cp = strchr(cp+1, '\n');
688 if (!cp++)
689 return;
690 }
691 }
692 fprintf(pbm->mfp, "%s%s", cp, nl);
693 }
694 }
695
696 void
697 meta_cmd_finish(void *pbmp)
698 {
699 #ifdef USE_FILEMON
700 BuildMon *pbm = pbmp;
701
702 if (!pbm)
703 pbm = &Mybm;
704
705 if (pbm->filemon_fd >= 0) {
706 close(pbm->filemon_fd);
707 filemon_read(pbm->mfp, pbm->mon_fd);
708 pbm->filemon_fd = pbm->mon_fd = -1;
709 }
710 #endif
711 }
712
713 void
714 meta_job_finish(Job *job)
715 {
716 BuildMon *pbm;
717
718 if (job != NULL) {
719 pbm = &job->bm;
720 } else {
721 pbm = &Mybm;
722 }
723 if (pbm->mfp != NULL) {
724 meta_cmd_finish(pbm);
725 fclose(pbm->mfp);
726 pbm->mfp = NULL;
727 pbm->meta_fname[0] = '\0';
728 }
729 }
730
731 /*
732 * Fetch a full line from fp - growing bufp if needed
733 * Return length in bufp.
734 */
735 static int
736 fgetLine(char **bufp, size_t *szp, int o, FILE *fp)
737 {
738 char *buf = *bufp;
739 size_t bufsz = *szp;
740 struct stat fs;
741 int x;
742
743 if (fgets(&buf[o], bufsz - o, fp) != NULL) {
744 check_newline:
745 x = o + strlen(&buf[o]);
746 if (buf[x - 1] == '\n')
747 return x;
748 /*
749 * We need to grow the buffer.
750 * The meta file can give us a clue.
751 */
752 if (fstat(fileno(fp), &fs) == 0) {
753 size_t newsz;
754 char *p;
755
756 newsz = ROUNDUP((fs.st_size / 2), BUFSIZ);
757 if (newsz <= bufsz)
758 newsz = ROUNDUP(fs.st_size, BUFSIZ);
759 if (DEBUG(META))
760 fprintf(debug_file, "growing buffer %u -> %u\n",
761 bufsz, newsz);
762 p = bmake_realloc(buf, newsz);
763 if (p) {
764 *bufp = buf = p;
765 *szp = bufsz = newsz;
766 /* fetch the rest */
767 if (!fgets(&buf[x], bufsz - x, fp))
768 return x; /* truncated! */
769 goto check_newline;
770 }
771 }
772 }
773 return 0;
774 }
775
776 /*
777 * When running with 'meta' functionality, a target can be out-of-date
778 * if any of the references in it's meta data file is more recent.
779 * We have to track the latestdir on a per-process basis.
780 */
781 #define LDIR_VNAME_FMT ".meta.%d.ldir"
782
783 Boolean
784 meta_oodate(GNode *gn, Boolean oodate)
785 {
786 static char *tmpdir = NULL;
787 static char cwd[MAXPATHLEN];
788 char ldir_vname[64];
789 char latestdir[MAXPATHLEN];
790 char fname[MAXPATHLEN];
791 char fname1[MAXPATHLEN];
792 char fname2[MAXPATHLEN];
793 char *p;
794 char *cp;
795 static size_t cwdlen = 0;
796 static size_t tmplen = 0;
797 FILE *fp;
798 Boolean ignoreOODATE = FALSE;
799
800 if (oodate)
801 return oodate; /* we're done */
802
803 /*
804 * We need to check if the target is out-of-date. This includes
805 * checking if the expanded command has changed. This in turn
806 * requires that all variables are set in the same way that they
807 * would be if the target needs to be re-built.
808 */
809 Make_DoAllVar(gn);
810
811 meta_name(gn, fname, sizeof(fname), NULL, NULL);
812
813 #ifdef DEBUG_META_MODE
814 if (DEBUG(META))
815 fprintf(debug_file, "meta_oodate: %s\n", fname);
816 #endif
817
818 if ((fp = fopen(fname, "r")) != NULL) {
819 static char *buf = NULL;
820 static size_t bufsz;
821 int lineno = 0;
822 int lastpid = 0;
823 int pid;
824 int f = 0;
825 int x;
826 LstNode ln;
827 struct stat fs;
828
829 if (!buf) {
830 bufsz = 8 * BUFSIZ;
831 buf = bmake_malloc(bufsz);
832 }
833
834 if (!cwdlen) {
835 if (getcwd(cwd, sizeof(cwd)) == NULL)
836 err(1, "Could not get current working directory");
837 cwdlen = strlen(cwd);
838 }
839
840 if (!tmpdir) {
841 tmpdir = getTmpdir();
842 tmplen = strlen(tmpdir);
843 }
844
845 /* we want to track all the .meta we read */
846 Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
847
848 ln = Lst_First(gn->commands);
849 while (!oodate && (x = fgetLine(&buf, &bufsz, 0, fp)) > 0) {
850 lineno++;
851 if (buf[x - 1] == '\n')
852 buf[x - 1] = '\0';
853 else
854 warnx("%s: %d: line truncated at %u", fname, lineno, x);
855
856 /* Find the start of the build monitor section. */
857 if (!f) {
858 if (strncmp(buf, "-- filemon", 10) == 0) {
859 f = 1;
860 continue;
861 }
862 if (strncmp(buf, "# buildmon", 10) == 0) {
863 f = 1;
864 continue;
865 }
866 }
867
868 /* Delimit the record type. */
869 p = buf;
870 #ifdef DEBUG_META_MODE
871 if (DEBUG(META))
872 fprintf(debug_file, "%s: %d: %s\n", fname, lineno, buf);
873 #endif
874 strsep(&p, " ");
875 if (f) {
876 /*
877 * We are in the 'filemon' output section.
878 * Each record from filemon follows the general form:
879 *
880 * <key> <pid> <data>
881 *
882 * Where:
883 * <key> is a single letter, denoting the syscall.
884 * <pid> is the process that made the syscall.
885 * <data> is the arguments (of interest).
886 */
887 switch(buf[0]) {
888 case '#': /* comment */
889 case 'V': /* version */
890 break;
891 default:
892 /*
893 * We need to track pathnames per-process.
894 *
895 * Each process run by make, starts off in the 'CWD'
896 * recorded in the .meta file, if it chdirs ('C')
897 * elsewhere we need to track that - but only for
898 * that process. If it forks ('F'), we initialize
899 * the child to have the same cwd as its parent.
900 *
901 * We also need to track the 'latestdir' of
902 * interest. This is usually the same as cwd, but
903 * not if a process is reading directories.
904 *
905 * Each time we spot a different process ('pid')
906 * we save the current value of 'latestdir' in a
907 * variable qualified by 'lastpid', and
908 * re-initialize 'latestdir' to any pre-saved
909 * value for the current 'pid' and 'CWD' if none.
910 */
911 pid = atoi(p);
912 if (pid > 0 && pid != lastpid) {
913 char *ldir;
914 char *tp;
915
916 if (lastpid > 0) {
917 /* We need to remember this. */
918 Var_Set(ldir_vname, latestdir, VAR_GLOBAL, 0);
919 }
920 snprintf(ldir_vname, sizeof(ldir_vname), LDIR_VNAME_FMT, pid);
921 lastpid = pid;
922 ldir = Var_Value(ldir_vname, VAR_GLOBAL, &tp);
923 if (ldir) {
924 strlcpy(latestdir, ldir, sizeof(latestdir));
925 if (tp)
926 free(tp);
927 } else
928 strlcpy(latestdir, cwd, sizeof(latestdir));
929 }
930 /* Skip past the pid. */
931 if (strsep(&p, " ") == NULL)
932 continue;
933 #ifdef DEBUG_META_MODE
934 if (DEBUG(META))
935 fprintf(debug_file, "%s: %d: cwd=%s ldir=%s\n", fname, lineno, cwd, latestdir);
936 #endif
937 break;
938 }
939
940 /* Process according to record type. */
941 switch (buf[0]) {
942 case 'X': /* eXit */
943 Var_Delete(ldir_vname, VAR_GLOBAL);
944 lastpid = 0; /* no need to save ldir_vname */
945 break;
946
947 case 'F': /* [v]Fork */
948 {
949 char cldir[64];
950 int child;
951
952 child = atoi(p);
953 if (child > 0) {
954 snprintf(cldir, sizeof(cldir), LDIR_VNAME_FMT, child);
955 Var_Set(cldir, latestdir, VAR_GLOBAL, 0);
956 }
957 }
958 break;
959
960 case 'C': /* Chdir */
961 /* Update the latest directory. */
962 strlcpy(latestdir, p, sizeof(latestdir));
963 break;
964
965 case 'R': /* Read */
966 case 'E': /* Exec */
967 /*
968 * Check for runtime files that can't
969 * be part of the dependencies because
970 * they are _expected_ to change.
971 */
972 if (strncmp(p, "/tmp/", 5) == 0 ||
973 (tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0))
974 break;
975
976 if (strncmp(p, "/var/", 5) == 0)
977 break;
978
979 /* Ignore device files. */
980 if (strncmp(p, "/dev/", 5) == 0)
981 break;
982
983 /* Ignore /etc/ files. */
984 if (strncmp(p, "/etc/", 5) == 0)
985 break;
986
987 /*
988 * The rest of the record is the file name.
989 * Check if it's not an absolute path.
990 */
991 {
992 char *sdirs[4];
993 char **sdp;
994 int sdx = 0;
995 int found = 0;
996
997 if (*p == '/') {
998 sdirs[sdx++] = p; /* done */
999 } else {
1000 if (strcmp(".", p) == 0)
1001 continue; /* no point */
1002
1003 /* Check vs latestdir */
1004 snprintf(fname1, sizeof(fname1), "%s/%s", latestdir, p);
1005 sdirs[sdx++] = fname1;
1006
1007 if (strcmp(latestdir, cwd) != 0) {
1008 /* Check vs cwd */
1009 snprintf(fname2, sizeof(fname2), "%s/%s", cwd, p);
1010 sdirs[sdx++] = fname2;
1011 }
1012 }
1013 sdirs[sdx++] = NULL;
1014
1015 for (sdp = sdirs; *sdp && !found; sdp++) {
1016 #ifdef DEBUG_META_MODE
1017 if (DEBUG(META))
1018 fprintf(debug_file, "%s: %d: looking for: %s\n", fname, lineno, *sdp);
1019 #endif
1020 if (stat(*sdp, &fs) == 0) {
1021 found = 1;
1022 p = *sdp;
1023 }
1024 }
1025 if (found) {
1026 #ifdef DEBUG_META_MODE
1027 if (DEBUG(META))
1028 fprintf(debug_file, "%s: %d: found: %s\n", fname, lineno, p);
1029 #endif
1030 if (!S_ISDIR(fs.st_mode) &&
1031 fs.st_mtime > gn->mtime) {
1032 if (DEBUG(META))
1033 fprintf(debug_file, "%s: %d: file '%s' is newer than the target...\n", fname, lineno, p);
1034 oodate = TRUE;
1035 } else if (S_ISDIR(fs.st_mode)) {
1036 /* Update the latest directory. */
1037 realpath(p, latestdir);
1038 }
1039 } else if (errno == ENOENT && *p == '/' &&
1040 strncmp(p, cwd, cwdlen) != 0) {
1041 /*
1042 * A referenced file outside of CWD is missing.
1043 * We cannot catch every eventuality here...
1044 */
1045 if (DEBUG(META))
1046 fprintf(debug_file, "%s: %d: file '%s' may have moved?...\n", fname, lineno, p);
1047 oodate = TRUE;
1048 }
1049 }
1050 break;
1051 default:
1052 break;
1053 }
1054 } else if (strcmp(buf, "CMD") == 0) {
1055 /*
1056 * Compare the current command with the one in the
1057 * meta data file.
1058 */
1059 if (ln == NULL) {
1060 if (DEBUG(META))
1061 fprintf(debug_file, "%s: %d: there were more build commands in the meta data file than there are now...\n", fname, lineno);
1062 oodate = TRUE;
1063 } else {
1064 char *cmd = (char *)Lst_Datum(ln);
1065
1066 if (!ignoreOODATE) {
1067 if (strstr(cmd, "$?"))
1068 ignoreOODATE = TRUE;
1069 else if ((cp = strstr(cmd, ".OODATE"))) {
1070 /* check for $[{(].OODATE[)}] */
1071 if (cp > cmd + 2 && cp[-2] == '$')
1072 ignoreOODATE = TRUE;
1073 }
1074 if (ignoreOODATE && DEBUG(META))
1075 fprintf(debug_file, "%s: %d: cannot compare commands using .OODATE\n", fname, lineno);
1076 }
1077 cmd = Var_Subst(NULL, cmd, gn, TRUE);
1078
1079 if ((cp = strchr(cmd, '\n'))) {
1080 int n;
1081
1082 /*
1083 * This command contains newlines, we need to
1084 * fetch more from the .meta file before we
1085 * attempt a comparison.
1086 */
1087 /* first put the newline back at buf[x - 1] */
1088 buf[x - 1] = '\n';
1089 do {
1090 /* now fetch the next line */
1091 if ((n = fgetLine(&buf, &bufsz, x, fp)) <= 0)
1092 break;
1093 x = n;
1094 lineno++;
1095 if (buf[x - 1] != '\n') {
1096 warnx("%s: %d: line truncated at %u", fname, lineno, x);
1097 break;
1098 }
1099 cp = strchr(++cp, '\n');
1100 } while (cp);
1101 if (buf[x - 1] == '\n')
1102 buf[x - 1] = '\0';
1103 }
1104 if (!ignoreOODATE &&
1105 !(gn->type & OP_NOMETA_CMP) &&
1106 strcmp(p, cmd) != 0) {
1107 if (DEBUG(META))
1108 fprintf(debug_file, "%s: %d: a build command has changed\n%s\nvs\n%s\n", fname, lineno, p, cmd);
1109 if (!metaIgnoreCMDs)
1110 oodate = TRUE;
1111 }
1112 free(cmd);
1113 ln = Lst_Succ(ln);
1114 }
1115 } else if (strcmp(buf, "CWD") == 0) {
1116 /*
1117 * Check if there are extra commands now
1118 * that weren't in the meta data file.
1119 */
1120 if (!oodate && ln != NULL) {
1121 if (DEBUG(META))
1122 fprintf(debug_file, "%s: %d: there are extra build commands now that weren't in the meta data file\n", fname, lineno);
1123 oodate = TRUE;
1124 }
1125 if (strcmp(p, cwd) != 0) {
1126 if (DEBUG(META))
1127 fprintf(debug_file, "%s: %d: the current working directory has changed from '%s' to '%s'\n", fname, lineno, p, curdir);
1128 oodate = TRUE;
1129 }
1130 }
1131 }
1132
1133 fclose(fp);
1134 }
1135 if (oodate && ignoreOODATE) {
1136 /*
1137 * Target uses .OODATE, so we need to re-compute it.
1138 * We need to clean up what Make_DoAllVar() did.
1139 */
1140 Var_Delete(ALLSRC, gn);
1141 Var_Delete(OODATE, gn);
1142 gn->flags &= ~DONE_ALLSRC;
1143 }
1144 return oodate;
1145 }
1146
1147 /* support for compat mode */
1148
1149 static int childPipe[2];
1150
1151 void
1152 meta_compat_start(void)
1153 {
1154 #ifdef USE_FILEMON_ONCE
1155 /*
1156 * We need to re-open filemon for each cmd.
1157 */
1158 BuildMon *pbm = &Mybm;
1159
1160 if (pbm->mfp != NULL && useFilemon) {
1161 filemon_open(pbm);
1162 } else {
1163 pbm->mon_fd = pbm->filemon_fd = -1;
1164 }
1165 #endif
1166 if (pipe(childPipe) < 0)
1167 Punt("Cannot create pipe: %s", strerror(errno));
1168 /* Set close-on-exec flag for both */
1169 (void)fcntl(childPipe[0], F_SETFD, 1);
1170 (void)fcntl(childPipe[1], F_SETFD, 1);
1171 }
1172
1173 void
1174 meta_compat_child(void)
1175 {
1176 meta_job_child(NULL);
1177 if (dup2(childPipe[1], 1) < 0 ||
1178 dup2(1, 2) < 0) {
1179 execError("dup2", "pipe");
1180 _exit(1);
1181 }
1182 }
1183
1184 void
1185 meta_compat_parent(void)
1186 {
1187 FILE *fp;
1188 char buf[BUFSIZ];
1189
1190 close(childPipe[1]); /* child side */
1191 fp = fdopen(childPipe[0], "r");
1192 while (fgets(buf, sizeof(buf), fp)) {
1193 meta_job_output(NULL, buf, "");
1194 printf("%s", buf);
1195 }
1196 fclose(fp);
1197 }
1198
1199 #endif /* USE_META */
1200