about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2023-12-25 02:10:04 +0100
committerLeah Neukirchen <leah@vuxu.org>2023-12-25 02:10:04 +0100
commit787b7b7631c8fec4743fff93ce93c2b986235e61 (patch)
tree677ddc3ddd169de2688fb2e8da866b3b5c3824a8
parent6c09d05650a552d7b56332921f6a060e70e52128 (diff)
downloadnitro-787b7b7631c8fec4743fff93ce93c2b986235e61.tar.gz
nitro-787b7b7631c8fec4743fff93ce93c2b986235e61.tar.xz
nitro-787b7b7631c8fec4743fff93ce93c2b986235e61.zip
ensure PATH is set without using realloc
-rw-r--r--nitro.c22
1 files changed, 18 insertions, 4 deletions
diff --git a/nitro.c b/nitro.c
index 4f9a266..7e4860a 100644
--- a/nitro.c
+++ b/nitro.c
@@ -13,6 +13,7 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <limits.h>
+#include <paths.h>
 #include <poll.h>
 #include <signal.h>
 #include <stdarg.h>
@@ -23,6 +24,10 @@
 #include <time.h>
 #include <unistd.h>
 
+extern char **environ;
+char **child_environ;
+char *envbuf[64];
+
 typedef int64_t deadline;		/* milliseconds since boot */
 
 deadline time_now()
@@ -180,7 +185,7 @@ proc_launch(int i)
 				dup2(globallog[1], 1);
 		}
 
-		execl("run", "run", (char *)0);
+		execle("run", "run", (char *)0, child_environ);
 
 		status = (errno == ENOENT ? 127 : 126);
 		write(alivepipefd[1], &status, 1);
@@ -246,7 +251,7 @@ proc_setup(int i)
 		else if (globallog[1] != -1)
 			dup2(globallog[1], 1);
 
-		execl("setup", "setup", (char *)0);
+		execle("setup", "setup", (char *)0, child_environ);
 		_exit(127);
 	} else if (child < 0) {
 		abort();	/* XXX handle retry */
@@ -301,7 +306,7 @@ proc_finish(int i)
 
 		setsid();
 
-		execl("finish", "finish", run_status, run_signal, (char *)0);
+		execle("finish", "finish", run_status, run_signal, (char *)0, child_environ);
 		_exit(127);
 	} else if (child < 0) {
 		abort();	/* XXX handle retry */
@@ -1161,7 +1166,6 @@ main(int argc, char *argv[])
 	pid1 = real_pid1 = (getpid() == 1);
 	if (pid1) {
 		umask(0022);
-		setenv("PATH", "/usr/bin:/usr/sbin", 0);
 		init_mount();
 		own_console();
 #ifdef __linux__
@@ -1170,6 +1174,16 @@ main(int argc, char *argv[])
 #endif
 	}
 
+	// can't use putenv, which pulls in realloc
+	if (!getenv("PATH")) {
+		envbuf[0] = (char*) "PATH=" _PATH_DEFPATH;
+		for (char **e = environ, **c = envbuf+1; *e; e++, c++)
+			*c = *e;
+		child_environ = envbuf;
+	} else {
+		child_environ = environ;
+	}
+
 	const char *dir = "/etc/nitro";
 	if (argc == 2)
 		dir = argv[1];