]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
confile_utils: add new file
authorChristian Brauner <christian.brauner@ubuntu.com>
Thu, 1 Jun 2017 21:43:16 +0000 (23:43 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Fri, 2 Jun 2017 22:41:54 +0000 (00:41 +0200)
This adds confile_utils.{c,h} which will contain a helpers to parse lxc
configuration files.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/Makefile.am
src/lxc/confile.c
src/lxc/confile_utils.c [new file with mode: 0644]
src/lxc/confile_utils.h [new file with mode: 0644]

index a6a69decf193a740aac2329129149abe463e1c27..9f0c7a743c6a484f9437701e3e48eeb0652745c6 100644 (file)
@@ -20,6 +20,8 @@ noinst_HEADERS = \
        cgroups/cgroup.h \
        caps.h \
        conf.h \
+       confile.h \
+       confile_utils.h \
        console.h \
        error.h \
        initutils.h \
@@ -101,6 +103,7 @@ liblxc_la_SOURCES = \
        namespace.h namespace.c \
        conf.c conf.h \
        confile.c confile.h \
+       confile_utils.c confile_utils.h \
        list.h \
        state.c state.h \
        log.c log.h \
index 4186d634714390c82da87a519521177c5e1208ed..be6c3b1d549019cb87c3e3b8fedad0850fa041f7 100644 (file)
@@ -45,6 +45,7 @@
 #include "parse.h"
 #include "config.h"
 #include "confile.h"
+#include "confile_utils.h"
 #include "utils.h"
 #include "log.h"
 #include "conf.h"
@@ -1984,8 +1985,7 @@ static int set_config_idmaps(const char *key, const char *value,
 {
        unsigned long hostid, nsid, range;
        char type;
-       char *window, *slide;
-       char *dup = NULL;
+       int ret;
        struct lxc_list *idmaplist = NULL;
        struct id_map *idmap = NULL;
 
@@ -2001,109 +2001,10 @@ static int set_config_idmaps(const char *key, const char *value,
                goto on_error;
        memset(idmap, 0, sizeof(*idmap));
 
-       /* Duplicate string. */
-       dup = strdup(value);
-       if (!dup)
+       ret = parse_idmaps(value, &type, &nsid, &hostid, &range);
+       if (ret < 0)
                goto on_error;
 
-       /* A prototypical idmap entry would be: "u 1000 1000000 65536" */
-
-       /* align */
-        slide = window = dup;
-        /* skip whitespace */
-        slide += strspn(slide, " \t\r");
-        if (slide != window && *slide == '\0')
-                goto on_error;
-
-       /* Validate type. */
-        if (*slide != 'u' && *slide != 'g')
-                goto on_error;
-        /* Assign type. */
-        type = *slide;
-
-       /* move beyond type */
-        slide++;
-       /* align */
-        window = slide;
-        /* Validate that only whitespace follows. */
-        slide += strspn(slide, " \t\r");
-       /* There must be whitespace. */
-        if (slide == window)
-                goto on_error;
-
-        /* Mark beginning of nsuid. */
-        window = slide;
-       /* Validate that non-whitespace follows. */
-        slide += strcspn(slide, " \t\r");
-       /* There must be non-whitespace. */
-        if (slide == window || *slide == '\0')
-                goto on_error;
-        /* Mark end of nsuid. */
-        *slide = '\0';
-
-        /* Parse nsuid. */
-        if (lxc_safe_ulong(window, &nsid) < 0)
-                goto on_error;
-
-       /* Move beyond \0. */
-        slide++;
-       /* align */
-        window = slide;
-        /* Validate that only whitespace follows. */
-        slide += strspn(slide, " \t\r");
-       /* If there was only one whitespace then we whiped it with our \0 above.
-        * So only ensure that we're not at the end of the string.
-        */
-       if (*slide == '\0')
-                goto on_error;
-
-        /* Mark beginning of hostid. */
-        window = slide;
-       /* Validate that non-whitespace follows. */
-        slide += strcspn(slide, " \t\r");
-       /* There must be non-whitespace. */
-        if (slide == window || *slide == '\0')
-                goto on_error;
-        /* Mark end of nsuid. */
-        *slide = '\0';
-
-        /* Parse hostid. */
-        if (lxc_safe_ulong(window, &hostid) < 0)
-                goto on_error;
-
-       /* Move beyond \0. */
-        slide++;
-       /* align */
-        window = slide;
-        /* Validate that only whitespace follows. */
-        slide += strspn(slide, " \t\r");
-       /* If there was only one whitespace then we whiped it with our \0 above.
-        * So only ensure that we're not at the end of the string.
-        */
-        if (*slide == '\0')
-                goto on_error;
-
-        /* Mark beginning of range. */
-        window = slide;
-       /* Validate that non-whitespace follows. */
-        slide += strcspn(slide, " \t\r");
-       /* There must be non-whitespace. */
-        if (slide == window)
-                goto on_error;
-
-       /* The range is the last valid entry we expect. So make sure that there
-        * is not trailing garbage and if there is, error out.
-        */
-       if (*(slide + strspn(slide, " \t\r\n")) != '\0')
-                goto on_error;
-        /* Mark end of range. */
-        *slide = '\0';
-
-        /* Parse range. */
-        if (lxc_safe_ulong(window, &range) < 0)
-                goto on_error;
-
-       /* Yay, we survived. */
        INFO("read uid map: type %c nsid %lu hostid %lu range %lu", type, nsid, hostid, range);
        if (type == 'u')
                idmap->idtype = ID_TYPE_UID;
@@ -2124,7 +2025,6 @@ static int set_config_idmaps(const char *key, const char *value,
 on_error:
        free(idmaplist);
        free(idmap);
-       free(dup);
 
        return -1;
 }
diff --git a/src/lxc/confile_utils.c b/src/lxc/confile_utils.c
new file mode 100644 (file)
index 0000000..2018f8e
--- /dev/null
@@ -0,0 +1,148 @@
+/* liblxcapi
+ *
+ * Copyright © 2017 Christian Brauner <christian.brauner@ubuntu.com>.
+ * Copyright © 2017 Canonical Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include "utils.h"
+
+int parse_idmaps(const char *idmap, char *type, unsigned long *nsid,
+                unsigned long *hostid, unsigned long *range)
+{
+       int ret = -1;
+       unsigned long tmp_hostid, tmp_nsid, tmp_range;
+       char tmp_type;
+       char *window, *slide;
+       char *dup = NULL;
+
+       /* Duplicate string. */
+       dup = strdup(idmap);
+       if (!dup)
+               goto on_error;
+
+       /* A prototypical idmap entry would be: "u 1000 1000000 65536" */
+
+       /* align */
+       slide = window = dup;
+       /* skip whitespace */
+       slide += strspn(slide, " \t\r");
+       if (slide != window && *slide == '\0')
+               goto on_error;
+
+       /* Validate type. */
+       if (*slide != 'u' && *slide != 'g')
+               goto on_error;
+       /* Assign type. */
+       tmp_type = *slide;
+
+       /* move beyond type */
+       slide++;
+       /* align */
+       window = slide;
+       /* Validate that only whitespace follows. */
+       slide += strspn(slide, " \t\r");
+       /* There must be whitespace. */
+       if (slide == window)
+               goto on_error;
+
+       /* Mark beginning of nsuid. */
+       window = slide;
+       /* Validate that non-whitespace follows. */
+       slide += strcspn(slide, " \t\r");
+       /* There must be non-whitespace. */
+       if (slide == window || *slide == '\0')
+               goto on_error;
+       /* Mark end of nsuid. */
+       *slide = '\0';
+
+       /* Parse nsuid. */
+       if (lxc_safe_ulong(window, &tmp_nsid) < 0)
+               goto on_error;
+
+       /* Move beyond \0. */
+       slide++;
+       /* align */
+       window = slide;
+       /* Validate that only whitespace follows. */
+       slide += strspn(slide, " \t\r");
+       /* If there was only one whitespace then we whiped it with our \0 above.
+        * So only ensure that we're not at the end of the string.
+        */
+       if (*slide == '\0')
+               goto on_error;
+
+       /* Mark beginning of hostid. */
+       window = slide;
+       /* Validate that non-whitespace follows. */
+       slide += strcspn(slide, " \t\r");
+       /* There must be non-whitespace. */
+       if (slide == window || *slide == '\0')
+               goto on_error;
+       /* Mark end of nsuid. */
+       *slide = '\0';
+
+       /* Parse hostid. */
+       if (lxc_safe_ulong(window, &tmp_hostid) < 0)
+               goto on_error;
+
+       /* Move beyond \0. */
+       slide++;
+       /* align */
+       window = slide;
+       /* Validate that only whitespace follows. */
+       slide += strspn(slide, " \t\r");
+       /* If there was only one whitespace then we whiped it with our \0 above.
+        * So only ensure that we're not at the end of the string.
+        */
+       if (*slide == '\0')
+               goto on_error;
+
+       /* Mark beginning of range. */
+       window = slide;
+       /* Validate that non-whitespace follows. */
+       slide += strcspn(slide, " \t\r");
+       /* There must be non-whitespace. */
+       if (slide == window)
+               goto on_error;
+
+       /* The range is the last valid entry we expect. So make sure that there
+        * is not trailing garbage and if there is, error out.
+        */
+       if (*(slide + strspn(slide, " \t\r\n")) != '\0')
+               goto on_error;
+       /* Mark end of range. */
+       *slide = '\0';
+
+       /* Parse range. */
+       if (lxc_safe_ulong(window, &tmp_range) < 0)
+               goto on_error;
+
+       *type = tmp_type;
+       *nsid = tmp_nsid;
+       *hostid = tmp_hostid;
+       *range = tmp_range;
+
+       /* Yay, we survived. */
+       ret = 0;
+
+on_error:
+       free(dup);
+
+       return ret;
+}
diff --git a/src/lxc/confile_utils.h b/src/lxc/confile_utils.h
new file mode 100644 (file)
index 0000000..758d96a
--- /dev/null
@@ -0,0 +1,26 @@
+/* liblxcapi
+ *
+ * Copyright © 2017 Christian Brauner <christian.brauner@ubuntu.com>.
+ * Copyright © 2017 Canonical Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef __LXC_CONFILE_UTILS_H
+#define __LXC_CONFILE_UTILS_H
+
+extern int parse_idmaps(const char *idmap, char *type, unsigned long *nsid,
+                       unsigned long *hostid, unsigned long *range);
+
+#endif /* __LXC_CONFILE_UTILS_H */