]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
lxc_id_mapping: don't try to write mappings if there are none
authorSerge Hallyn <serge.hallyn@ubuntu.com>
Wed, 13 Mar 2013 15:33:00 +0000 (10:33 -0500)
committerStéphane Graber <stgraber@ubuntu.com>
Wed, 13 Mar 2013 16:30:21 +0000 (12:30 -0400)
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@gmail.com>
Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
Acked-by: Stéphane Graber <stgraber@ubuntu.com>
src/lxc/conf.c

index 85e1c61e4206d38b6e88e4ff2a7e17ed7c6181b8..af7569012b131cab203ab3b57069ad6d3fef4726 100644 (file)
@@ -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;
 }