[lxc-devel] [lxd/master] Revert "lxd: remove /proc/self/cmdline parsing"

brauner on Github lxc-bot at linuxcontainers.org
Thu Feb 14 15:57:08 UTC 2019


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 810 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20190214/79e695ec/attachment.bin>
-------------- next part --------------
From 4f4b5c57c3d4bd75bde3035e3c8f3ece7b71b7d1 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Thu, 14 Feb 2019 14:35:10 +0100
Subject: [PATCH] Revert "lxd: remove /proc/self/cmdline parsing"

The ELF binary spec doesn specify:

SHT_INIT_ARRAY
This section contains an array of pointers to initialization functions,
as described in ``Initialization and Termination Functions'' in Chapter
5. Each pointer in the array is taken as a parameterless procedure with
a void return.

which means libcs other than glibc might not pass down argc and argv to
constructors.

This reverts commit 2149bdaf895a1145d49e7627b50e0097678de189.

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 lxd/main_forkfile.go |  4 ----
 lxd/main_nsexec.go   | 54 ++++++++++++++++++++++++++++++++------------
 2 files changed, 39 insertions(+), 19 deletions(-)

diff --git a/lxd/main_forkfile.go b/lxd/main_forkfile.go
index 24576da18b..38030e2ae0 100644
--- a/lxd/main_forkfile.go
+++ b/lxd/main_forkfile.go
@@ -409,10 +409,6 @@ void forkfile() {
 
 	// Get the subcommand
 	command = advance_arg(false);
-	if (command)
-		fprintf(stderr, "BBBB: %s\n", command);
-	else
-		fprintf(stderr, "CCCC\n");
 	if (command == NULL || (strcmp(command, "--help") == 0 || strcmp(command, "--version") == 0 || strcmp(command, "-h") == 0)) {
 		return;
 	}
diff --git a/lxd/main_nsexec.go b/lxd/main_nsexec.go
index 789ad7b914..c03a5d24f0 100644
--- a/lxd/main_nsexec.go
+++ b/lxd/main_nsexec.go
@@ -42,12 +42,17 @@ extern void forkproxy();
 extern void forkuevent();
 
 // Command line parsing and tracking
-#define CMDLINE_INDEX_SKIP_SELF 1
-size_t cmdline_index = CMDLINE_INDEX_SKIP_SELF;
-char **cmdline;
+#define CMDLINE_SIZE (8 * PATH_MAX)
+char cmdline_buf[CMDLINE_SIZE];
+char *cmdline_cur = NULL;
+ssize_t cmdline_size = -1;
 
 char* advance_arg(bool required) {
-	if (!cmdline[cmdline_index]) {
+	while (*cmdline_cur != 0)
+		cmdline_cur++;
+
+	cmdline_cur++;
+	if (cmdline_size <= cmdline_cur - cmdline_buf) {
 		if (!required)
 			return NULL;
 
@@ -55,7 +60,7 @@ char* advance_arg(bool required) {
 		_exit(1);
 	}
 
-	return cmdline[cmdline_index++];
+	return cmdline_cur;
 }
 
 void error(char *msg)
@@ -205,27 +210,46 @@ void attach_userns(int pid) {
 	}
 }
 
-__attribute__((constructor)) void init(int argc, char *argv[]) {
-	cmdline = argv;
+__attribute__((constructor)) void init(void) {
+	int cmdline;
+
+	// Extract arguments
+	cmdline = open("/proc/self/cmdline", O_RDONLY);
+	if (cmdline < 0) {
+		error("error: open");
+		_exit(232);
+	}
 
-	char *cmd = advance_arg(false);
-	if (!cmd) {
+	memset(cmdline_buf, 0, sizeof(cmdline_buf));
+	if ((cmdline_size = read(cmdline, cmdline_buf, sizeof(cmdline_buf)-1)) < 0) {
+		close(cmdline);
+		error("error: read");
+		_exit(232);
+	}
+	close(cmdline);
+
+	// Skip the first argument (but don't fail on missing second argument)
+	cmdline_cur = cmdline_buf;
+	while (*cmdline_cur != 0)
+		cmdline_cur++;
+	cmdline_cur++;
+	if (cmdline_size <= cmdline_cur - cmdline_buf) {
 		checkfeature();
 		return;
 	}
 
 	// Intercepts some subcommands
-	if (strcmp(cmd, "forkfile") == 0)
+	if (strcmp(cmdline_cur, "forkfile") == 0)
 		forkfile();
-	else if (strcmp(cmd, "forkmount") == 0)
+	else if (strcmp(cmdline_cur, "forkmount") == 0)
 		forkmount();
-	else if (strcmp(cmd, "forknet") == 0)
+	else if (strcmp(cmdline_cur, "forknet") == 0)
 		forknet();
-	else if (strcmp(cmd, "forkproxy") == 0)
+	else if (strcmp(cmdline_cur, "forkproxy") == 0)
 		forkproxy();
-	else if (strcmp(cmd, "forkuevent") == 0)
+	else if (strcmp(cmdline_cur, "forkuevent") == 0)
 		forkuevent();
-	else if (strncmp(cmd, "-", 1) == 0 || strcmp(cmdline[1], "daemon") == 0)
+	else if (strncmp(cmdline_cur, "-", 1) == 0 || strcmp(cmdline_cur, "daemon") == 0)
 		checkfeature();
 }
 */


More information about the lxc-devel mailing list