]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
conf: adapt idmap helpers
authorChristian Brauner <christian.brauner@ubuntu.com>
Tue, 2 Jan 2018 21:15:17 +0000 (22:15 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Fri, 19 Jan 2018 13:17:31 +0000 (14:17 +0100)
- mapped_hostid_entry()
- idmap_add()

Closes #2033.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/conf.c

index 3aeed5c2d093e4a93564c8de8a2066cff7bb8b85..126faee12cad947421fda50ee32f89f0806a3b87 100644 (file)
@@ -3474,8 +3474,8 @@ static int run_userns_fn(void *data)
        return d->fn(d->arg);
 }
 
-static struct id_map *mapped_hostid_entry(struct lxc_conf *conf, unsigned id,
-                                         enum idtype idtype)
+static struct id_map *find_mapped_hostid_entry(struct lxc_conf *conf,
+                                              unsigned id, enum idtype idtype)
 {
        struct lxc_list *it;
        struct id_map *map;
@@ -3492,14 +3492,6 @@ static struct id_map *mapped_hostid_entry(struct lxc_conf *conf, unsigned id,
                }
        }
 
-       if (!retmap)
-               return NULL;
-
-       retmap = malloc(sizeof(*retmap));
-       if (!retmap)
-               return NULL;
-
-       memcpy(retmap, map, sizeof(*retmap));
        return retmap;
 }
 
@@ -3507,27 +3499,28 @@ static struct id_map *mapped_hostid_entry(struct lxc_conf *conf, unsigned id,
  * Allocate a new {g,u}id mapping for the given {g,u}id. Re-use an already
  * existing one or establish a new one.
  */
-static struct id_map *idmap_add(struct lxc_conf *conf, uid_t id, enum idtype type)
+static struct id_map *mapped_hostid_add(struct lxc_conf *conf, uid_t id, enum idtype type)
 {
        int hostid_mapped;
-       struct id_map *entry = NULL;
+       struct id_map *entry = NULL, *tmp = NULL;
+
+       entry = malloc(sizeof(*entry));
+       if (!entry)
+               return NULL;
 
        /* Reuse existing mapping. */
-       entry = mapped_hostid_entry(conf, id, type);
-       if (entry)
-               return entry;
+       tmp = find_mapped_hostid_entry(conf, id, type);
+       if (tmp)
+               return memcpy(entry, tmp, sizeof(*entry));
 
        /* Find new mapping. */
        hostid_mapped = find_unmapped_nsid(conf, type);
        if (hostid_mapped < 0) {
-               DEBUG("failed to find free mapping for id %d", id);
+               DEBUG("Failed to find free mapping for id %d", id);
+               free(entry);
                return NULL;
        }
 
-       entry = malloc(sizeof(*entry));
-       if (!entry)
-               return NULL;
-
        entry->idtype = type;
        entry->nsid = hostid_mapped;
        entry->hostid = (unsigned long)id;
@@ -3636,10 +3629,10 @@ int userns_exec_1(struct lxc_conf *conf, int (*fn)(void *), void *data,
 
        /* Check whether the {g,u}id of the user has a mapping. */
        if (!host_uid_map)
-               host_uid_map = idmap_add(conf, euid, ID_TYPE_UID);
+               host_uid_map = mapped_hostid_add(conf, euid, ID_TYPE_UID);
 
        if (!host_gid_map)
-               host_gid_map = idmap_add(conf, egid, ID_TYPE_GID);
+               host_gid_map = mapped_hostid_add(conf, egid, ID_TYPE_GID);
 
        if (!host_uid_map) {
                DEBUG("failed to find mapping for uid %d", euid);
@@ -3847,12 +3840,12 @@ int userns_exec_full(struct lxc_conf *conf, int (*fn)(void *), void *data,
 
        /* Check whether the {g,u}id of the user has a mapping. */
        if (!host_uid_map)
-               host_uid_map = idmap_add(conf, euid, ID_TYPE_UID);
+               host_uid_map = mapped_hostid_add(conf, euid, ID_TYPE_UID);
        else
                host_uid_map = container_root_uid;
 
        if (!host_gid_map)
-               host_gid_map = idmap_add(conf, egid, ID_TYPE_GID);
+               host_gid_map = mapped_hostid_add(conf, egid, ID_TYPE_GID);
        else
                host_gid_map = container_root_gid;