]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
conf: rework lxc specific mount option parsing
authorChristian Brauner <christian.brauner@ubuntu.com>
Wed, 17 Mar 2021 09:01:19 +0000 (10:01 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Wed, 21 Apr 2021 08:05:58 +0000 (10:05 +0200)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/conf.c
src/lxc/conf.h

index 6a0d54b8384870a5c820d9807d806b1db0d767d9..40e238ebd311c3f76a4b8fceb7327f17246d876c 100644 (file)
@@ -2090,33 +2090,48 @@ skipremount:
        return 0;
 }
 
+const char *lxc_mount_options_info[LXC_MOUNT_MAX] = {
+       "create=dir",
+       "create=file",
+       "optional",
+       "relative",
+};
+
 /* Remove "optional", "create=dir", and "create=file" from mntopt */
-static void cull_mntent_opt(struct mntent *mntent)
+void parse_lxc_mntopts(struct lxc_mount_options *opts, char *mnt_opts)
 {
-       int i;
-       char *list[] = {
-               "create=dir",
-               "create=file",
-               "optional",
-               "relative",
-               NULL
-       };
 
-       for (i = 0; list[i]; i++) {
+       for (size_t i = LXC_MOUNT_CREATE_DIR; i < LXC_MOUNT_MAX; i++) {
+               const char *opt_name = lxc_mount_options_info[i];
                char *p, *p2;
 
-               p = strstr(mntent->mnt_opts, list[i]);
+               p = strstr(mnt_opts, opt_name);
                if (!p)
                        continue;
 
-               p2 = strchr(p, ',');
-               if (!p2) {
-                       /* no more mntopts, so just chop it here */
-                       *p = '\0';
+               switch (i) {
+               case LXC_MOUNT_CREATE_DIR:
+                       opts->create_dir = 1;
+                       break;
+               case LXC_MOUNT_CREATE_FILE:
+                       opts->create_file = 1;
+                       break;
+               case LXC_MOUNT_OPTIONAL:
+                       opts->optional = 1;
+                       break;
+               case LXC_MOUNT_RELATIVE:
+                       opts->relative = 1;
+                       break;
+               default:
+                       WARN("Unknown LXC specific mount option");
                        continue;
                }
 
-               memmove(p, p2 + 1, strlen(p2 + 1) + 1);
+               p2 = strchr(p, ',');
+               if (!p2)
+                       *p = '\0'; /* no more mntopts, so just chop it here */
+               else
+                       memmove(p, p2 + 1, strlen(p2 + 1) + 1);
        }
 }
 
@@ -2178,6 +2193,7 @@ static inline int mount_entry_on_generic(struct mntent *mntent,
        char *rootfs_path = NULL;
        int ret;
        bool dev, optional, relative;
+       struct lxc_mount_options opts = {};
 
        optional = hasmntopt(mntent, "optional") != NULL;
        dev = hasmntopt(mntent, "dev") != NULL;
@@ -2194,7 +2210,7 @@ static inline int mount_entry_on_generic(struct mntent *mntent,
 
                return -1;
        }
-       cull_mntent_opt(mntent);
+       parse_lxc_mntopts(&opts, mntent->mnt_opts);
 
        ret = parse_propagationopts(mntent->mnt_opts, &pflags);
        if (ret < 0)
index 2824c8719ba52a59441a0f27f241248db5b996a0..a141f9409595091de5341c34e6c48b8848846dd6 100644 (file)
@@ -181,6 +181,23 @@ struct lxc_tty_info {
        struct lxc_terminal_info *tty;
 };
 
+typedef enum lxc_mount_options_t {
+       LXC_MOUNT_CREATE_DIR    = 0,
+       LXC_MOUNT_CREATE_FILE   = 1,
+       LXC_MOUNT_OPTIONAL      = 2,
+       LXC_MOUNT_RELATIVE      = 3,
+       LXC_MOUNT_MAX           = 4,
+} lxc_mount_options_t;
+
+__hidden extern const char *lxc_mount_options_info[LXC_MOUNT_MAX];
+
+struct lxc_mount_options {
+       int create_dir : 1;
+       int create_file : 1;
+       int optional : 1;
+       int relative : 1;
+};
+
 /* Defines a structure to store the rootfs location, the
  * optionals pivot_root, rootfs mount paths
  * @path         : the rootfs source (directory or device)
@@ -211,6 +228,7 @@ struct lxc_rootfs {
        unsigned long mountflags;
        char *data;
        bool managed;
+       struct lxc_mount_options mnt_opts;
 };
 
 /*
@@ -509,6 +527,7 @@ __hidden extern int userns_exec_full(struct lxc_conf *conf, int (*fn)(void *), v
                                     const char *fn_name);
 __hidden extern int parse_mntopts(const char *mntopts, unsigned long *mntflags, char **mntdata);
 __hidden extern int parse_propagationopts(const char *mntopts, unsigned long *pflags);
+__hidden extern void parse_lxc_mntopts(struct lxc_mount_options *opts, char *mnt_opts);
 __hidden extern void tmp_proc_unmount(struct lxc_conf *lxc_conf);
 __hidden extern void suggest_default_idmap(void);
 __hidden extern FILE *make_anonymous_mount_file(struct lxc_list *mount, bool include_nesting_helpers);