diff options
author | hoek <hoek@FreeBSD.org> | 1999-08-22 05:28:13 +0000 |
---|---|---|
committer | hoek <hoek@FreeBSD.org> | 1999-08-22 05:28:13 +0000 |
commit | 963913abb6a88a3947bb204f37f32c6307430d3b (patch) | |
tree | 7d933325084c86f7a8500bc84cbe5ce6a8a0465d /usr.bin/make | |
parent | 04960451714ed32ed277dc3fa6b97ed1494a7413 (diff) | |
download | FreeBSD-src-963913abb6a88a3947bb204f37f32c6307430d3b.zip FreeBSD-src-963913abb6a88a3947bb204f37f32c6307430d3b.tar.gz |
Fix a temp file race occurring only when -j is used.
Noticed by: ru
Obtained from: OpenBSD
Diffstat (limited to 'usr.bin/make')
-rw-r--r-- | usr.bin/make/job.c | 26 | ||||
-rw-r--r-- | usr.bin/make/job.h | 6 |
2 files changed, 17 insertions, 15 deletions
diff --git a/usr.bin/make/job.c b/usr.bin/make/job.c index d102c53..0dbc3ad 100644 --- a/usr.bin/make/job.c +++ b/usr.bin/make/job.c @@ -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); diff --git a/usr.bin/make/job.h b/usr.bin/make/job.h index 2d3e961..6dac5e4 100644 --- a/usr.bin/make/job.h +++ b/usr.bin/make/job.h @@ -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 |