Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/make Avoid strdup in mkTempFile



details:   https://anonhg.NetBSD.org/src/rev/b770ba25c69d
branches:  trunk
changeset: 951536:b770ba25c69d
user:      sjg <sjg%NetBSD.org@localhost>
date:      Fri Feb 05 19:19:17 2021 +0000

description:
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:

 usr.bin/make/job.c  |  29 +++++++++++++++++++----------
 usr.bin/make/job.h  |   3 ++-
 usr.bin/make/main.c |  20 +++++++++++---------
 usr.bin/make/make.h |   4 ++--
 usr.bin/make/meta.c |   7 +++++--
 5 files changed, 39 insertions(+), 24 deletions(-)

diffs (182 lines):

diff -r 7403d599f18a -r b770ba25c69d usr.bin/make/job.c
--- a/usr.bin/make/job.c        Fri Feb 05 19:18:23 2021 +0000
+++ b/usr.bin/make/job.c        Fri Feb 05 19:19:17 2021 +0000
@@ -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 @@
         * 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 @@
                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 @@
 #endif
 
        *out_run = JobPrintCommands(job);
-
-       free(tfile);
 }
 
 /*
@@ -2764,6 +2759,20 @@
                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 -r 7403d599f18a -r b770ba25c69d usr.bin/make/job.h
--- a/usr.bin/make/job.h        Fri Feb 05 19:18:23 2021 +0000
+++ b/usr.bin/make/job.h        Fri Feb 05 19:19:17 2021 +0000
@@ -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_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 -r 7403d599f18a -r b770ba25c69d usr.bin/make/main.c
--- a/usr.bin/make/main.c       Fri Feb 05 19:18:23 2021 +0000
+++ b/usr.bin/make/main.c       Fri Feb 05 19:19:17 2021 +0000
@@ -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 @@
  * 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 -r 7403d599f18a -r b770ba25c69d usr.bin/make/make.h
--- a/usr.bin/make/make.h       Fri Feb 05 19:18:23 2021 +0000
+++ b/usr.bin/make/make.h       Fri Feb 05 19:19:17 2021 +0000
@@ -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 @@
 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 -r 7403d599f18a -r b770ba25c69d usr.bin/make/meta.c
--- a/usr.bin/make/meta.c       Fri Feb 05 19:18:23 2021 +0000
+++ b/usr.bin/make/meta.c       Fri Feb 05 19:19:17 2021 +0000
@@ -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 @@
      * 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!");
     }



Home | Main Index | Thread Index | Old Index