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