Fix a temp file race occurring only when -j is used.

Noticed by:	ru
Obtained from:	OpenBSD
This commit is contained in:
hoek 1999-08-22 05:28:13 +00:00
parent 0496045171
commit 963913abb6
2 changed files with 17 additions and 15 deletions

View File

@ -35,7 +35,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: job.c,v 1.12 1999/02/14 22:22:42 dt Exp $
* $Id: job.c,v 1.13 1999/08/17 00:39:19 hoek Exp $
*/
#ifndef lint
@ -166,11 +166,10 @@ static int numCommands; /* The number of commands actually printed
/*
* 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 XXXXX in
* the string are replaced by the pid of the make process in a 5-character
* field with leading zeroes.
* used over by removing it before the child shell is executed. The XXXXXXXXXX
* in the string are replaced by mkstemp(3).
*/
static char tfile[] = TMPPAT;
static char tfile[sizeof(TMPPAT)];
/*
@ -1668,7 +1667,6 @@ JobStart(gn, flags, previous)
{
register Job *job; /* new job descriptor */
char *argv[4]; /* Argument vector to shell */
static int jobno = 0; /* job number of catching output in a file */
Boolean cmdsOK; /* true if the nodes commands were all right */
Boolean local; /* Set true if the job was run locally */
Boolean noExec; /* Set true if we decide not to run the job */
@ -1875,8 +1873,7 @@ JobStart(gn, flags, previous)
/*
* If we're using pipes to catch output, create the pipe by which we'll
* get the shell's output. If we're using files, print out that we're
* starting a job and then set up its temporary-file name. This is just
* tfile with two extra digits tacked on -- jobno.
* starting a job and then set up its temporary-file name.
*/
if (!compatMake || (job->flags & JOB_FIRST)) {
if (usePipes) {
@ -1890,9 +1887,9 @@ JobStart(gn, flags, previous)
} else {
(void) fprintf(stdout, "Remaking `%s'\n", gn->name);
(void) fflush(stdout);
sprintf(job->outFile, "%s%02d", tfile, jobno);
jobno = (jobno + 1) % 100;
job->outFd = open(job->outFile,O_WRONLY|O_CREAT|O_APPEND,0600);
(void) strcpy(job->outFile, TMPPAT);
if ((job->outFd = mkstemp(job->outFile)) == -1)
Punt("cannot create temp file: %s", strerror(errno));
(void) fcntl(job->outFd, F_SETFD, 1);
}
}
@ -2408,8 +2405,13 @@ Job_Init(maxproc, maxlocal)
* be running at once. */
{
GNode *begin; /* node for commands to do at the very start */
int tfd;
(void) sprintf(tfile, "/tmp/make%05d", getpid());
(void) strcpy(tfile, TMPPAT);
if ((tfd = mkstemp(tfile)) == -1)
Punt("cannot create temp file: %s", strerror(errno));
else
(void) close(tfd);
jobs = Lst_Init(FALSE);
stoppedJobs = Lst_Init(FALSE);

View File

@ -36,7 +36,7 @@
* SUCH DAMAGE.
*
* from: @(#)job.h 8.1 (Berkeley) 6/6/93
* $Id: job.h,v 1.7 1997/02/22 19:27:12 peter Exp $
* $Id: job.h,v 1.8 1997/04/21 20:32:11 phk Exp $
*/
/*-
@ -47,7 +47,7 @@
#ifndef _JOB_H_
#define _JOB_H_
#define TMPPAT "/tmp/makeXXXXX"
#define TMPPAT "/tmp/makeXXXXXXXXXX"
/*
* The SEL_ constants determine the maximum amount of time spent in select
@ -128,7 +128,7 @@ typedef struct Job {
} o_pipe; /* data used when catching the output via
* a pipe */
struct {
char of_outFile[sizeof(TMPPAT)+2];
char of_outFile[sizeof(TMPPAT)];
/* Name of file to which shell output
* was rerouted */
int of_outFd; /* Stream open to the output