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