[lxc-devel] [PATCH 1/1] lxc_id_mapping: don't try to write mappings if there are none

Serge Hallyn serge.hallyn at ubuntu.com
Wed Mar 13 15:33:00 UTC 2013


Otherwise containers fail to start even if they aren't trying to map
ids.

Also don't allocate buf unless we need to.

Reported-by: Alexander Vladimirov <alexander.idkfa.vladimirov at gmail.com>
Signed-off-by: Serge Hallyn <serge.hallyn at ubuntu.com>
---
 src/lxc/conf.c | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)

diff --git a/src/lxc/conf.c b/src/lxc/conf.c
index 85e1c61..af75690 100644
--- a/src/lxc/conf.c
+++ b/src/lxc/conf.c
@@ -2479,17 +2479,20 @@ int lxc_map_ids(struct lxc_list *idmap, pid_t pid)
 	struct lxc_list *iterator;
 	struct id_map *map;
 	int ret = 0;
-	char *buf,*pos;
 	enum idtype type;
-
-	/* The kernel only takes <= 4k for writes to /proc/<nr>/[ug]id_map */
-	buf = pos = malloc(4096);
-	if (!buf)
-		return -ENOMEM;
+	char *buf = NULL, *pos;
 
 	for(type = ID_TYPE_UID; type <= ID_TYPE_GID; type++) {
-		int left,fill;
+		int left, fill;
+
+		pos = buf;
 		lxc_list_for_each(iterator, idmap) {
+			/* The kernel only takes <= 4k for writes to /proc/<nr>/[ug]id_map */
+			if (!buf)
+				buf = pos = malloc(4096);
+			if (!buf)
+				return -ENOMEM;
+
 			map = iterator->elem;
 			if (map->idtype == type) {
 				left = 4096 - (pos - buf);
@@ -2500,13 +2503,15 @@ int lxc_map_ids(struct lxc_list *idmap, pid_t pid)
 				pos += fill;
 			}
 		}
+		if (pos == buf) // no mappings were found
+			continue;
 		ret = write_id_mapping(type, pid, buf, pos-buf);
 		if (ret)
 			break;
-		pos = buf;
 	}
 
-	free(buf);
+	if (buf)
+		free(buf);
 	return ret;
 }
 
-- 
1.8.1.2





More information about the lxc-devel mailing list