Avoid strdup in mkTempFile

Require caller to pass a buffer and size if they
want the tempfile not unlinked.

Add Job_TempFile to handle blocking signals around
call to mkTempFile, so that meta_open_filemon can use it
in jobs mode.
This commit is contained in:
sjg 2021-02-05 19:19:17 +00:00
parent 53b16f4da5
commit 2b67d42e71
5 changed files with 39 additions and 24 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: job.c,v 1.418 2021/02/05 05:53:40 rillig Exp $ */
/* $NetBSD: job.c,v 1.419 2021/02/05 19:19:17 sjg Exp $ */
/*
* Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@ -142,7 +142,7 @@
#include "trace.h"
/* "@(#)job.c 8.2 (Berkeley) 3/19/94" */
MAKE_RCSID("$NetBSD: job.c,v 1.418 2021/02/05 05:53:40 rillig Exp $");
MAKE_RCSID("$NetBSD: job.c,v 1.419 2021/02/05 19:19:17 sjg Exp $");
/*
* A shell defines how the commands are run. All commands for a target are
@ -1569,8 +1569,7 @@ JobWriteShellCommands(Job *job, GNode *gn, Boolean cmdsOK, Boolean *out_run)
* are put. It is removed before the child shell is executed,
* unless DEBUG(SCRIPT) is set.
*/
char *tfile;
sigset_t mask;
char tfile[MAXPATHLEN];
int tfd; /* File descriptor to the temp file */
/*
@ -1582,11 +1581,9 @@ JobWriteShellCommands(Job *job, GNode *gn, Boolean cmdsOK, Boolean *out_run)
DieHorribly();
}
JobSigLock(&mask);
tfd = mkTempFile(TMPPAT, &tfile);
tfd = Job_TempFile(TMPPAT, tfile, sizeof tfile);
if (!DEBUG(SCRIPT))
(void)eunlink(tfile);
JobSigUnlock(&mask);
eunlink(tfile);
job->cmdFILE = fdopen(tfd, "w+");
if (job->cmdFILE == NULL)
@ -1603,8 +1600,6 @@ JobWriteShellCommands(Job *job, GNode *gn, Boolean cmdsOK, Boolean *out_run)
#endif
*out_run = JobPrintCommands(job);
free(tfile);
}
/*
@ -2764,6 +2759,20 @@ JobTokenAdd(void)
continue;
}
/* Get a temp file */
int
Job_TempFile(const char *pattern, char *tfile, size_t tfile_sz)
{
int fd;
sigset_t mask;
JobSigLock(&mask);
fd = mkTempFile(pattern, tfile, tfile_sz);
JobSigUnlock(&mask);
return fd;
}
/* Prep the job token pipe in the root make process. */
void
Job_ServerStart(int max_tokens, int jp_0, int jp_1)

View File

@ -1,4 +1,4 @@
/* $NetBSD: job.h,v 1.71 2020/12/30 10:03:16 rillig Exp $ */
/* $NetBSD: job.h,v 1.72 2021/02/05 19:19:17 sjg Exp $ */
/*
* Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@ -205,5 +205,6 @@ void Job_ServerStart(int, int, int);
void Job_SetPrefix(void);
Boolean Job_RunTarget(const char *, const char *);
void Job_FlagsToString(const Job *, char *, size_t);
int Job_TempFile(const char *, char *, size_t);
#endif /* MAKE_JOB_H */

View File

@ -1,4 +1,4 @@
/* $NetBSD: main.c,v 1.532 2021/02/05 05:15:12 rillig Exp $ */
/* $NetBSD: main.c,v 1.533 2021/02/05 19:19:17 sjg Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@ -111,7 +111,7 @@
#include "trace.h"
/* "@(#)main.c 8.3 (Berkeley) 3/19/94" */
MAKE_RCSID("$NetBSD: main.c,v 1.532 2021/02/05 05:15:12 rillig Exp $");
MAKE_RCSID("$NetBSD: main.c,v 1.533 2021/02/05 19:19:17 sjg Exp $");
#if defined(MAKE_NATIVE) && !defined(lint)
__COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993 "
"The Regents of the University of California. "
@ -2215,27 +2215,29 @@ getTmpdir(void)
* Otherwise unlink the file once open.
*/
int
mkTempFile(const char *pattern, char **out_fname)
mkTempFile(const char *pattern, char *tfile, size_t tfile_sz)
{
static char *tmpdir = NULL;
char tfile[MAXPATHLEN];
char tbuf[MAXPATHLEN];
int fd;
if (pattern == NULL)
pattern = TMPPAT;
if (tmpdir == NULL)
tmpdir = getTmpdir();
if (tfile == NULL) {
tfile = tbuf;
tfile_sz = sizeof tbuf;
}
if (pattern[0] == '/') {
snprintf(tfile, sizeof tfile, "%s", pattern);
snprintf(tfile, tfile_sz, "%s", pattern);
} else {
snprintf(tfile, sizeof tfile, "%s%s", tmpdir, pattern);
snprintf(tfile, tfile_sz, "%s%s", tmpdir, pattern);
}
if ((fd = mkstemp(tfile)) < 0)
Punt("Could not create temporary file %s: %s", tfile,
strerror(errno));
if (out_fname != NULL) {
*out_fname = bmake_strdup(tfile);
} else {
if (tfile == tbuf) {
unlink(tfile); /* we just want the descriptor */
}
return fd;

View File

@ -1,4 +1,4 @@
/* $NetBSD: make.h,v 1.255 2021/02/05 05:42:39 rillig Exp $ */
/* $NetBSD: make.h,v 1.256 2021/02/05 19:19:17 sjg Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@ -708,7 +708,7 @@ Boolean shouldDieQuietly(GNode *, int);
void PrintOnError(GNode *, const char *);
void Main_ExportMAKEFLAGS(Boolean);
Boolean Main_SetObjdir(Boolean, const char *, ...) MAKE_ATTR_PRINTFLIKE(2, 3);
int mkTempFile(const char *, char **);
int mkTempFile(const char *, char *, size_t);
int str2Lst_Append(StringList *, char *);
void GNode_FprintDetails(FILE *, const char *, const GNode *, const char *);
Boolean GNode_ShouldExecute(GNode *gn);

View File

@ -1,4 +1,4 @@
/* $NetBSD: meta.c,v 1.176 2021/02/05 05:15:12 rillig Exp $ */
/* $NetBSD: meta.c,v 1.177 2021/02/05 19:19:17 sjg Exp $ */
/*
* Implement 'meta' mode.
@ -140,7 +140,10 @@ meta_open_filemon(BuildMon *pbm)
* cwd causing getcwd to do a lot more work.
* We only care about the descriptor.
*/
pbm->mon_fd = mkTempFile("filemon.XXXXXX", NULL);
if (!opts.compatMake)
pbm->mon_fd = Job_TempFile("filemon.XXXXXX", NULL, 0);
else
pbm->mon_fd = mkTempFile("filemon.XXXXXX", NULL, 0);
if ((dupfd = dup(pbm->mon_fd)) == -1) {
err(1, "Could not dup filemon output!");
}