From 4f7521b4137d37fed01d3d11d4d2dde92138158a Mon Sep 17 00:00:00 2001 From: Serge Hallyn Date: Wed, 13 Mar 2013 10:33:00 -0500 Subject: [PATCH] lxc_id_mapping: don't try to write mappings if there are none MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Serge Hallyn Acked-by: Stéphane Graber --- 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 85e1c61e4..af7569012 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//[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//[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; } -- 2.47.2