Module Name: src Committed By: sjg Date: Thu Apr 22 19:11:17 UTC 2010
Modified Files: src/usr.bin/make: job.c main.c make.h Log Message: 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 To generate a diff of this commit: cvs rdiff -u -r1.147 -r1.148 src/usr.bin/make/job.c cvs rdiff -u -r1.179 -r1.180 src/usr.bin/make/main.c cvs rdiff -u -r1.80 -r1.81 src/usr.bin/make/make.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/usr.bin/make/job.c diff -u src/usr.bin/make/job.c:1.147 src/usr.bin/make/job.c:1.148 --- src/usr.bin/make/job.c:1.147 Wed Apr 7 00:11:27 2010 +++ src/usr.bin/make/job.c Thu Apr 22 19:11:17 2010 @@ -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, Index: src/usr.bin/make/main.c diff -u src/usr.bin/make/main.c:1.179 src/usr.bin/make/main.c:1.180 --- src/usr.bin/make/main.c:1.179 Tue Apr 20 17:18:08 2010 +++ src/usr.bin/make/main.c Thu Apr 22 19:11:17 2010 @@ -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; +} Index: src/usr.bin/make/make.h diff -u src/usr.bin/make/make.h:1.80 src/usr.bin/make/make.h:1.81 --- src/usr.bin/make/make.h:1.80 Wed Apr 7 00:11:27 2010 +++ src/usr.bin/make/make.h Thu Apr 22 19:11:17 2010 @@ -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) ({ \