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