diff options
| author | sjg <sjg@NetBSD.org> | 2021-02-05 19:19:17 +0000 |
|---|---|---|
| committer | sjg <sjg@NetBSD.org> | 2021-02-05 19:19:17 +0000 |
| commit | 9f989261bc4c7e772c8dd03ae5d64a185d173b6d (patch) | |
| tree | 42e9031c4f354b3d07881db1ce6d506b8935d633 /usr.bin/make | |
| parent | 78c443ced3d1575ec5145b59a68fd55f713909e9 (diff) | |
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.
Diffstat (limited to 'usr.bin/make')
| -rw-r--r-- | usr.bin/make/job.c | 29 | ||||
| -rw-r--r-- | usr.bin/make/job.h | 3 | ||||
| -rw-r--r-- | usr.bin/make/main.c | 20 | ||||
| -rw-r--r-- | usr.bin/make/make.h | 4 | ||||
| -rw-r--r-- | usr.bin/make/meta.c | 7 |
5 files changed, 39 insertions, 24 deletions
diff --git a/usr.bin/make/job.c b/usr.bin/make/job.c index 22550cc4877..c4035f386a4 100644 --- a/usr.bin/make/job.c +++ b/usr.bin/make/job.c @@ -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) diff --git a/usr.bin/make/job.h b/usr.bin/make/job.h index 9bb5f149766..0d7d7313b9d 100644 --- a/usr.bin/make/job.h +++ b/usr.bin/make/job.h @@ -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 */ diff --git a/usr.bin/make/main.c b/usr.bin/make/main.c index 25707f992f3..cd9321a6132 100644 --- a/usr.bin/make/main.c +++ b/usr.bin/make/main.c @@ -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; diff --git a/usr.bin/make/make.h b/usr.bin/make/make.h index fd7ce407be9..6466737784b 100644 --- a/usr.bin/make/make.h +++ b/usr.bin/make/make.h @@ -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); diff --git a/usr.bin/make/meta.c b/usr.bin/make/meta.c index 104f47d94ed..a12fd049eb3 100644 --- a/usr.bin/make/meta.c +++ b/usr.bin/make/meta.c @@ -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!"); } |
