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
This commit is contained in:
sjg 2010-04-22 19:11:17 +00:00
parent 6772f0e062
commit a71a4119cf
3 changed files with 54 additions and 28 deletions

View File

@ -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 @@ static sigset_t caught_signals; /* Set of signals we handle */
#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 @@ JobStart(GNode *gn, int flags)
}
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 @@ void
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 @@ Job_Init(void)
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,

View File

@ -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 @@ __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993\
#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 @@ rearg:
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 @@ rearg:
}
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 @@ Main_ExportMAKEFLAGS(Boolean first)
#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;
}

View File

@ -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 Check_Cwd(const char **);
void PrintOnError(GNode *, const char *);
void Main_ExportMAKEFLAGS(Boolean);
Boolean Main_SetObjdir(const char *);
int mkTempFile(const char *, char **);
#ifdef __GNUC__
#define UNCONST(ptr) ({ \