[lxc-devel] [lxc/master] parse: tweak config parsing

brauner on Github lxc-bot at linuxcontainers.org
Sat Oct 6 22:53:02 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 364 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20181006/1a752586/attachment.bin>
-------------- next part --------------
From 46ac8c5b35089048e35b87f4ebdffb23649aa4cb Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Sun, 7 Oct 2018 00:42:44 +0200
Subject: [PATCH 1/2] parse: remove access() check

We can just fail on open() and not waste an additional syscall.

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/confile.c | 4 ----
 1 file changed, 4 deletions(-)

diff --git a/src/lxc/confile.c b/src/lxc/confile.c
index 8e13b123d..d49afae11 100644
--- a/src/lxc/confile.c
+++ b/src/lxc/confile.c
@@ -2499,10 +2499,6 @@ int lxc_config_read(const char *file, struct lxc_conf *conf, bool from_include)
 	c.conf = conf;
 	c.from_include = from_include;
 
-	ret = access(file, R_OK);
-	if (ret < 0)
-		return -1;
-
 	/* Catch only the top level config file name in the structure. */
 	if (!conf->rcfile)
 		conf->rcfile = strdup(file);

From 872c1f046a09b678b9c3c39c42ba5bb865b9dabb Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Sun, 7 Oct 2018 00:50:52 +0200
Subject: [PATCH 2/2] parse: report errors when failing config parsing

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/parse.c | 38 ++++++++++++++++++++++++--------------
 1 file changed, 24 insertions(+), 14 deletions(-)

diff --git a/src/lxc/parse.c b/src/lxc/parse.c
index 1c0cc9f49..bab842a21 100644
--- a/src/lxc/parse.c
+++ b/src/lxc/parse.c
@@ -65,34 +65,35 @@ int lxc_strmunmap(void *addr, size_t length)
 	return munmap(addr, length + 1);
 }
 
-int lxc_file_for_each_line_mmap(const char *file, lxc_file_cb callback,
-				void *data)
+int lxc_file_for_each_line_mmap(const char *file, lxc_file_cb callback, void *data)
 {
-	int fd;
+	int fd, saved_errno;
 	char *buf, *line;
 	struct stat st;
 	int ret = 0;
 
 	fd = open(file, O_RDONLY | O_CLOEXEC);
-	if (fd < 0)
+	if (fd < 0) {
+		SYSERROR("Failed to open config file \"%s\"", file);
 		return -1;
+	}
 
 	ret = fstat(fd, &st);
 	if (ret < 0) {
-		close(fd);
-		return -1;
+		SYSERROR("Failed to stat config file \"%s\"", file);
+		goto on_error;
 	}
 
-	if (st.st_size == 0) {
-		close(fd);
-		return 0;
-	}
+	ret = 0;
+	if (st.st_size == 0)
+		goto on_error;
 
+	ret = -1;
 	buf = lxc_strmmap(NULL, st.st_size, PROT_READ | PROT_WRITE,
 			  MAP_PRIVATE | MAP_POPULATE, fd, 0);
 	if (buf == MAP_FAILED) {
-		close(fd);
-		return -1;
+		SYSERROR("Failed to map config file \"%s\"", file);
+		goto on_error;
 	}
 
 	lxc_iterate_parts(line, buf, "\n\0") {
@@ -102,13 +103,22 @@ int lxc_file_for_each_line_mmap(const char *file, lxc_file_cb callback,
 			 * error.
 			 */
 			if (ret < 0)
-				ERROR("Failed to parse config: %s", line);
+				ERROR("Failed to parse config file \"%s\" at "
+				      "line \"%s\"",
+				      file, line);
 			break;
 		}
 	}
 
-	lxc_strmunmap(buf, st.st_size);
+on_error:
+	ret = lxc_strmunmap(buf, st.st_size);
+	if (ret < 0)
+		SYSERROR("Failed to unmap config file \"%s\"", file);
+
+	saved_errno = errno;
 	close(fd);
+	errno = saved_errno;
+
 	return ret;
 }
 


More information about the lxc-devel mailing list