Module Name:    src
Committed By:   sjg
Date:           Fri Feb  5 19:19:17 UTC 2021

Modified Files:
        src/usr.bin/make: job.c job.h main.c make.h meta.c

Log Message:
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.


To generate a diff of this commit:
cvs rdiff -u -r1.418 -r1.419 src/usr.bin/make/job.c
cvs rdiff -u -r1.71 -r1.72 src/usr.bin/make/job.h
cvs rdiff -u -r1.532 -r1.533 src/usr.bin/make/main.c
cvs rdiff -u -r1.255 -r1.256 src/usr.bin/make/make.h
cvs rdiff -u -r1.176 -r1.177 src/usr.bin/make/meta.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/make/job.c
diff -u src/usr.bin/make/job.c:1.418 src/usr.bin/make/job.c:1.419
--- src/usr.bin/make/job.c:1.418	Fri Feb  5 05:53:40 2021
+++ src/usr.bin/make/job.c	Fri Feb  5 19:19:17 2021
@@ -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 *g
 	 * 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 *g
 		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 *g
 #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)

Index: src/usr.bin/make/job.h
diff -u src/usr.bin/make/job.h:1.71 src/usr.bin/make/job.h:1.72
--- src/usr.bin/make/job.h:1.71	Wed Dec 30 10:03:16 2020
+++ src/usr.bin/make/job.h	Fri Feb  5 19:19:17 2021
@@ -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 */

Index: src/usr.bin/make/main.c
diff -u src/usr.bin/make/main.c:1.532 src/usr.bin/make/main.c:1.533
--- src/usr.bin/make/main.c:1.532	Fri Feb  5 05:15:12 2021
+++ src/usr.bin/make/main.c	Fri Feb  5 19:19:17 2021
@@ -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;

Index: src/usr.bin/make/make.h
diff -u src/usr.bin/make/make.h:1.255 src/usr.bin/make/make.h:1.256
--- src/usr.bin/make/make.h:1.255	Fri Feb  5 05:42:39 2021
+++ src/usr.bin/make/make.h	Fri Feb  5 19:19:17 2021
@@ -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);

Index: src/usr.bin/make/meta.c
diff -u src/usr.bin/make/meta.c:1.176 src/usr.bin/make/meta.c:1.177
--- src/usr.bin/make/meta.c:1.176	Fri Feb  5 05:15:12 2021
+++ src/usr.bin/make/meta.c	Fri Feb  5 19:19:17 2021
@@ -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!");
     }

Reply via email to