Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/make Just because $TMPDIR is set does not mean it is...



details:   https://anonhg.NetBSD.org/src/rev/7de7b036d4b9
branches:  trunk
changeset: 754174:7de7b036d4b9
user:      sjg <sjg%NetBSD.org@localhost>
date:      Thu Apr 22 19:11:17 2010 +0000

description:
Just because $TMPDIR is set does not mean it is valid.
Add a central function for creating temp files so we have one place to
audit.

Reviewed by: dh

diffstat:

 usr.bin/make/job.c  |  28 ++++------------------------
 usr.bin/make/main.c |  51 ++++++++++++++++++++++++++++++++++++++++++++++++---
 usr.bin/make/make.h |   3 ++-
 3 files changed, 54 insertions(+), 28 deletions(-)

diffs (183 lines):

diff -r 2d11d992a5ac -r 7de7b036d4b9 usr.bin/make/job.c
--- a/usr.bin/make/job.c        Thu Apr 22 18:53:23 2010 +0000
+++ b/usr.bin/make/job.c        Thu Apr 22 19:11:17 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: job.c,v 1.147 2010/04/07 00:11:27 sjg Exp $    */
+/*     $NetBSD: job.c,v 1.148 2010/04/22 19:11:17 sjg Exp $    */
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -70,14 +70,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: job.c,v 1.147 2010/04/07 00:11:27 sjg Exp $";
+static char rcsid[] = "$NetBSD: job.c,v 1.148 2010/04/22 19:11:17 sjg Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)job.c      8.2 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: job.c,v 1.147 2010/04/07 00:11:27 sjg Exp $");
+__RCSID("$NetBSD: job.c,v 1.148 2010/04/22 19:11:17 sjg Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -342,8 +342,6 @@
 #define KILLPG(pid, sig)       killpg((pid), (sig))
 #endif
 
-static char *tmpdir;           /* directory name, always ending with "/" */
-
 static void JobChildSig(int);
 static void JobContinueSig(int);
 static Job *JobFindPid(int, int);
@@ -1555,11 +1553,7 @@
        }
 
        JobSigLock(&mask);
-       tfile = bmake_malloc(strlen(tmpdir) + sizeof(TMPPAT));
-       strcpy(tfile, tmpdir);
-       strcat(tfile, TMPPAT);
-       if ((tfd = mkstemp(tfile)) == -1)
-           Punt("Could not create temporary file %s", strerror(errno));
+       tfd = mkTempFile(TMPPAT, &tfile);
        if (!DEBUG(SCRIPT))
                (void)eunlink(tfile);
        JobSigUnlock(&mask);
@@ -2122,8 +2116,6 @@
 Job_Init(void)
 {
     GNode         *begin;     /* node for commands to do at the very start */
-    const char    *p;
-    size_t        len;
 
     /* Allocate space for all the job info */
     job_table = bmake_malloc(maxJobs * sizeof *job_table);
@@ -2136,18 +2128,6 @@
 
     lastNode =   NULL;
 
-    /* set tmpdir, and ensure that it ends with "/" */
-    p = getenv("TMPDIR");
-    if (p == NULL || *p == '\0') {
-       p = _PATH_TMP;
-    }
-    len = strlen(p);
-    tmpdir = bmake_malloc(len + 2);
-    strcpy(tmpdir, p);
-    if (tmpdir[len - 1] != '/') {
-       strcat(tmpdir, "/");
-    }
-
     if (maxJobs == 1) {
        /*
         * If only one job can run at a time, there's no need for a banner,
diff -r 2d11d992a5ac -r 7de7b036d4b9 usr.bin/make/main.c
--- a/usr.bin/make/main.c       Thu Apr 22 18:53:23 2010 +0000
+++ b/usr.bin/make/main.c       Thu Apr 22 19:11:17 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: main.c,v 1.179 2010/04/20 17:18:08 sjg Exp $   */
+/*     $NetBSD: main.c,v 1.180 2010/04/22 19:11:17 sjg Exp $   */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,7 +69,7 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: main.c,v 1.179 2010/04/20 17:18:08 sjg Exp $";
+static char rcsid[] = "$NetBSD: main.c,v 1.180 2010/04/22 19:11:17 sjg Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
@@ -81,7 +81,7 @@
 #if 0
 static char sccsid[] = "@(#)main.c     8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: main.c,v 1.179 2010/04/20 17:18:08 sjg Exp $");
+__RCSID("$NetBSD: main.c,v 1.180 2010/04/22 19:11:17 sjg Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -385,6 +385,7 @@
                case 'B':
                        compatMake = TRUE;
                        Var_Append(MAKEFLAGS, "-B", VAR_GLOBAL);
+                       Var_Set(MAKE_MODE, "compat", VAR_GLOBAL, 0);
                        break;
                case 'C':
                        if (chdir(argvalue) == -1) {
@@ -500,6 +501,7 @@
                        }
                        Var_Append(MAKEFLAGS, "-j", VAR_GLOBAL);
                        Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
+                       Var_Set(".MAKE.JOBS", argvalue, VAR_GLOBAL, 0);
                        maxJobTokens = maxJobs;
                        break;
                case 'k':
@@ -1942,3 +1944,46 @@
 #endif
     }
 }
+
+/*
+ * Create and open a temp file using "pattern".
+ * If "fnamep" is provided set it to a copy of the filename created.
+ * Otherwise unlink the file once open.
+ */
+int
+mkTempFile(const char *pattern, char **fnamep)
+{
+    static char *tmpdir = NULL;
+    char tfile[MAXPATHLEN];
+    int fd;
+    
+    if (!pattern)
+       pattern = TMPPAT;
+
+    if (!tmpdir) {
+       struct stat st;
+
+       /*
+        * Honor $TMPDIR but only if it is valid.
+        * Ensure it ends with /.
+        */
+       tmpdir = Var_Subst(NULL, "${TMPDIR:tA:U/tmp}/", VAR_GLOBAL, 0);
+       if (stat(tmpdir, &st) < 0 || !S_ISDIR(st.st_mode)) {
+           free(tmpdir);
+           tmpdir = bmake_strdup(_PATH_TMP);
+       }
+    }
+    if (pattern[0] == '/') {
+       strlcpy(tfile, pattern, sizeof(tfile));
+    } else {
+       snprintf(tfile, sizeof(tfile), "%s%s", tmpdir, pattern);
+    }
+    if ((fd = mkstemp(tfile)) < 0)
+       Punt("Could not create temporary file %s: %s", tfile, strerror(errno));
+    if (fnamep) {
+       *fnamep = bmake_strdup(tfile);
+    } else {
+       unlink(tfile);                  /* we just want the descriptor */
+    }
+    return fd;
+}
diff -r 2d11d992a5ac -r 7de7b036d4b9 usr.bin/make/make.h
--- a/usr.bin/make/make.h       Thu Apr 22 18:53:23 2010 +0000
+++ b/usr.bin/make/make.h       Thu Apr 22 19:11:17 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: make.h,v 1.80 2010/04/07 00:11:27 sjg Exp $    */
+/*     $NetBSD: make.h,v 1.81 2010/04/22 19:11:17 sjg Exp $    */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -450,6 +450,7 @@
 void PrintOnError(GNode *, const char *);
 void Main_ExportMAKEFLAGS(Boolean);
 Boolean Main_SetObjdir(const char *);
+int mkTempFile(const char *, char **);
 
 #ifdef __GNUC__
 #define UNCONST(ptr)   ({              \



Home | Main Index | Thread Index | Old Index