From af9429a6718c2238406ef7e33b3d517babde0932 Mon Sep 17 00:00:00 2001 From: apb Date: Sat, 11 Apr 2009 09:41:18 +0000 Subject: [PATCH] Honour the TMPDIR environment variable instead of always using /tmp as a place to store temporary files. --- usr.bin/make/job.c | 35 +++++++++++++++++++++++++++-------- usr.bin/make/job.h | 4 ++-- usr.bin/make/make.1 | 25 +++++++++++++++++-------- usr.bin/make/pathnames.h | 5 ++++- 4 files changed, 50 insertions(+), 19 deletions(-) diff --git a/usr.bin/make/job.c b/usr.bin/make/job.c index 629c42c3638a..e9af8f5bd052 100644 --- a/usr.bin/make/job.c +++ b/usr.bin/make/job.c @@ -1,4 +1,4 @@ -/* $NetBSD: job.c,v 1.144 2009/01/23 21:26:30 dsl Exp $ */ +/* $NetBSD: job.c,v 1.145 2009/04/11 09:41:18 apb 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.144 2009/01/23 21:26:30 dsl Exp $"; +static char rcsid[] = "$NetBSD: job.c,v 1.145 2009/04/11 09:41:18 apb Exp $"; #else #include #ifndef lint #if 0 static char sccsid[] = "@(#)job.c 8.2 (Berkeley) 3/19/94"; #else -__RCSID("$NetBSD: job.c,v 1.144 2009/01/23 21:26:30 dsl Exp $"); +__RCSID("$NetBSD: job.c,v 1.145 2009/04/11 09:41:18 apb Exp $"); #endif #endif /* not lint */ #endif @@ -342,6 +342,8 @@ 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); @@ -1538,11 +1540,10 @@ JobStart(GNode *gn, int flags) (!noExecute && !touchFlag)) { /* * tfile is the name of a file into which all shell commands are - * put. It is used over by removing it before the child shell is - * executed. The XXXXXX in the string are replaced by the pid of - * the make process in a 6-character field with leading zeroes. + * put. It is removed before the child shell is executed, unless + * DEBUG(SCRIPT) is set. */ - char tfile[sizeof(TMPPAT)]; + char *tfile; sigset_t mask; /* * We're serious here, but if the commands were bogus, we're @@ -1553,7 +1554,9 @@ JobStart(GNode *gn, int flags) } JobSigLock(&mask); - (void)strcpy(tfile, TMPPAT); + 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)); if (!DEBUG(SCRIPT)) @@ -1584,6 +1587,8 @@ JobStart(GNode *gn, int flags) if (numCommands == 0) { noExec = TRUE; } + + free(tfile); } else if (NoExecute(gn)) { /* * Not executing anything -- just print all the commands to stdout @@ -2116,6 +2121,8 @@ 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); @@ -2128,6 +2135,18 @@ 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, diff --git a/usr.bin/make/job.h b/usr.bin/make/job.h index c5134d47839d..326aa3e5c7bc 100644 --- a/usr.bin/make/job.h +++ b/usr.bin/make/job.h @@ -1,4 +1,4 @@ -/* $NetBSD: job.h,v 1.38 2008/12/13 15:19:29 dsl Exp $ */ +/* $NetBSD: job.h,v 1.39 2009/04/11 09:41:18 apb Exp $ */ /* * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. @@ -80,7 +80,7 @@ #ifndef _JOB_H_ #define _JOB_H_ -#define TMPPAT "/tmp/makeXXXXXX" +#define TMPPAT "makeXXXXXX" /* relative to tmpdir */ #ifdef USE_SELECT /* diff --git a/usr.bin/make/make.1 b/usr.bin/make/make.1 index 0c1557902a13..7be0f82201cb 100644 --- a/usr.bin/make/make.1 +++ b/usr.bin/make/make.1 @@ -1,4 +1,4 @@ -.\" $NetBSD: make.1,v 1.153 2009/01/24 13:02:33 wiz Exp $ +.\" $NetBSD: make.1,v 1.154 2009/04/11 09:41:18 apb Exp $ .\" .\" Copyright (c) 1990, 1993 .\" The Regents of the University of California. All rights reserved. @@ -185,16 +185,24 @@ Also known as "loud" behavior. Print debugging information about making targets, including modification dates. .It Ar n -Don't delete the temporary command scripts created in +Don't delete the temporary command scripts created when running commands. +These temporary scripts are created in the directory +referred to by the +.Ev TMPDIR +environment variable, or in .Pa /tmp -when running commands. -These are created via -.Xr mkstemp 3 +if +.Ev TMPDIR +is unset or set to the empty string. +The temporary scripts are created by +.Xr mkstemp 3 , and have names of the form -.Pa /tmp/makeXXXXX . +.Pa makeXXXXXX . .Em NOTE : This can create many file in -.Pa /tmp +.Ev TMPDIR +or +.Pa /tmp , so use with care. .It Ar p Print debugging information about makefile parsing. @@ -1769,8 +1777,9 @@ uses the following environment variables, if they exist: .Ev MAKEOBJDIR , .Ev MAKEOBJDIRPREFIX , .Ev MAKESYSPATH , +.Ev PWD , and -.Ev PWD . +.Ev TMPDIR . .Pp .Ev MAKEOBJDIRPREFIX and diff --git a/usr.bin/make/pathnames.h b/usr.bin/make/pathnames.h index aa8c37ddb21f..12c4f3d7b0ef 100644 --- a/usr.bin/make/pathnames.h +++ b/usr.bin/make/pathnames.h @@ -1,4 +1,4 @@ -/* $NetBSD: pathnames.h,v 1.16 2005/06/24 04:33:25 lukem Exp $ */ +/* $NetBSD: pathnames.h,v 1.17 2009/04/11 09:41:18 apb Exp $ */ /* * Copyright (c) 1990, 1993 @@ -48,3 +48,6 @@ #ifndef _PATH_DEFSYSPATH #define _PATH_DEFSYSPATH "/usr/share/mk" #endif +#ifndef _PATH_TMP +#define _PATH_TMP "/tmp/" /* with trailing slash */ +#endif