Module Name:    src
Committed By:   rillig
Date:           Sat May 25 15:37:17 UTC 2024

Modified Files:
        src/usr.bin/make: compat.c
        src/usr.bin/make/unit-tests: shell-csh.mk

Log Message:
make: minimize local variables around a vfork call

Passing all relevant values as arguments allows to remove the 'volatile'
qualifiers.


To generate a diff of this commit:
cvs rdiff -u -r1.255 -r1.256 src/usr.bin/make/compat.c
cvs rdiff -u -r1.8 -r1.9 src/usr.bin/make/unit-tests/shell-csh.mk

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/compat.c
diff -u src/usr.bin/make/compat.c:1.255 src/usr.bin/make/compat.c:1.256
--- src/usr.bin/make/compat.c:1.255	Sat Apr 20 10:18:55 2024
+++ src/usr.bin/make/compat.c	Sat May 25 15:37:17 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: compat.c,v 1.255 2024/04/20 10:18:55 rillig Exp $	*/
+/*	$NetBSD: compat.c,v 1.256 2024/05/25 15:37:17 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -91,7 +91,7 @@
 #include "pathnames.h"
 
 /*	"@(#)compat.c	8.2 (Berkeley) 3/19/94"	*/
-MAKE_RCSID("$NetBSD: compat.c,v 1.255 2024/04/20 10:18:55 rillig Exp $");
+MAKE_RCSID("$NetBSD: compat.c,v 1.256 2024/05/25 15:37:17 rillig Exp $");
 
 static GNode *curTarg = NULL;
 static pid_t compatChild;
@@ -200,6 +200,24 @@ UseShell(const char *cmd MAKE_ATTR_UNUSE
 #endif
 }
 
+static int
+Compat_Spawn(const char **av)
+{
+	int pid = vfork();
+	if (pid < 0)
+		Fatal("Could not fork");
+
+	if (pid == 0) {
+#ifdef USE_META
+		if (useMeta)
+			meta_compat_child();
+#endif
+		(void)execvp(av[0], (char *const *)UNCONST(av));
+		execDie("exec", av[0]);
+	}
+	return pid;
+}
+
 /*
  * Execute the next command for a target. If the command returns an error,
  * the node's made field is set to ERROR and creation stops.
@@ -222,13 +240,12 @@ Compat_RunCommand(const char *cmdp, GNod
 	volatile bool errCheck;	/* Check errors */
 	int reason;		/* Reason for child's death */
 	int status;		/* Description of child's death */
-	pid_t cpid;		/* Child actually found */
 	pid_t retstat;		/* Result of wait */
-	const char **volatile av; /* Argument vector for thing to exec */
+	const char **av;	/* Arguments for the child process */
 	char **volatile mav;	/* Copy of the argument vector for freeing */
 	bool useShell;		/* True if command should be executed using a
 				 * shell */
-	const char *volatile cmd = cmdp;
+	const char *cmd = cmdp;
 
 	silent = (gn->type & OP_SILENT) != OP_NONE;
 	errCheck = !(gn->type & OP_IGNORE);
@@ -330,19 +347,7 @@ Compat_RunCommand(const char *cmdp, GNod
 
 	Var_ReexportVars(gn);
 
-	compatChild = cpid = vfork();
-	if (cpid < 0)
-		Fatal("Could not fork");
-
-	if (cpid == 0) {
-#ifdef USE_META
-		if (useMeta)
-			meta_compat_child();
-#endif
-		(void)execvp(av[0], (char *const *)UNCONST(av));
-		execDie("exec", av[0]);
-	}
-
+	compatChild = Compat_Spawn(av);
 	free(mav);
 	free(bp);
 
@@ -352,11 +357,11 @@ Compat_RunCommand(const char *cmdp, GNod
 
 #ifdef USE_META
 	if (useMeta)
-		meta_compat_parent(cpid);
+		meta_compat_parent(compatChild);
 #endif
 
 	/* The child is off and running. Now all we can do is wait... */
-	while ((retstat = wait(&reason)) != cpid) {
+	while ((retstat = wait(&reason)) != compatChild) {
 		if (retstat > 0)
 			JobReapChild(retstat, reason, false); /* not ours? */
 		if (retstat == -1 && errno != EINTR)

Index: src/usr.bin/make/unit-tests/shell-csh.mk
diff -u src/usr.bin/make/unit-tests/shell-csh.mk:1.8 src/usr.bin/make/unit-tests/shell-csh.mk:1.9
--- src/usr.bin/make/unit-tests/shell-csh.mk:1.8	Sun Apr  4 09:58:51 2021
+++ src/usr.bin/make/unit-tests/shell-csh.mk	Sat May 25 15:37:17 2024
@@ -1,4 +1,4 @@
-# $NetBSD: shell-csh.mk,v 1.8 2021/04/04 09:58:51 rillig Exp $
+# $NetBSD: shell-csh.mk,v 1.9 2024/05/25 15:37:17 rillig Exp $
 #
 # Tests for using a C shell for running the commands.
 
@@ -6,7 +6,7 @@ CSH!=	which csh 2> /dev/null || true
 
 # The shell path must be an absolute path.
 # This is only obvious in parallel mode since in compat mode,
-# simple commands are executed via execve directly.
+# simple commands are executed via execvp directly.
 .if ${CSH} != ""
 .SHELL: name="csh" path="${CSH}"
 .endif

Reply via email to