From: Christian Brauner Date: Fri, 27 Aug 2021 11:08:09 +0000 (+0200) Subject: conf: port mounts to new list type X-Git-Tag: lxc-5.0.0~99^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F3956%2Fhead;p=thirdparty%2Flxc.git conf: port mounts to new list type Signed-off-by: Christian Brauner --- diff --git a/src/lxc/conf.c b/src/lxc/conf.c index 8aaefa16f..5937b0c76 100644 --- a/src/lxc/conf.c +++ b/src/lxc/conf.c @@ -2775,14 +2775,13 @@ static const char nesting_helpers[] = "proc dev/.lxc/proc proc create=dir,optional 0 0\n" "sys dev/.lxc/sys sysfs create=dir,optional 0 0\n"; -FILE *make_anonymous_mount_file(struct lxc_list *mount, +FILE *make_anonymous_mount_file(const struct list_head *mount_entries, bool include_nesting_helpers) { __do_close int fd = -EBADF; FILE *f; int ret; - char *mount_entry; - struct lxc_list *iterator; + struct string_entry *entry; fd = memfd_create(".lxc_mount_file", MFD_CLOEXEC); if (fd < 0) { @@ -2798,13 +2797,12 @@ FILE *make_anonymous_mount_file(struct lxc_list *mount, TRACE("Created temporary mount file"); } - lxc_list_for_each (iterator, mount) { + list_for_each_entry(entry, mount_entries, head) { size_t len; - mount_entry = iterator->elem; - len = strlen(mount_entry); + len = strlen(entry->val); - ret = lxc_write_nointr(fd, mount_entry, len); + ret = lxc_write_nointr(fd, entry->val, len); if (ret != len) return NULL; @@ -2831,12 +2829,12 @@ FILE *make_anonymous_mount_file(struct lxc_list *mount, } static int setup_mount_entries(const struct lxc_conf *conf, - struct lxc_rootfs *rootfs, struct lxc_list *mount, + struct lxc_rootfs *rootfs, const char *lxc_name, const char *lxc_path) { __do_fclose FILE *f = NULL; - f = make_anonymous_mount_file(mount, conf->lsm_aa_allow_nesting); + f = make_anonymous_mount_file(&conf->mount_entries, conf->lsm_aa_allow_nesting); if (!f) return -1; @@ -3057,10 +3055,10 @@ static int lxc_idmapped_mounts_child(struct lxc_handler *handler) int fret = -1; struct lxc_conf *conf = handler->conf; const char *fstab = conf->fstab; - struct lxc_list *mount = &conf->mount_list; int ret; - f_entries = make_anonymous_mount_file(mount, conf->lsm_aa_allow_nesting); + f_entries = make_anonymous_mount_file(&conf->mount_entries, + conf->lsm_aa_allow_nesting); if (!f_entries) { SYSERROR("Failed to create anonymous mount file"); goto out; @@ -3365,7 +3363,7 @@ struct lxc_conf *lxc_conf_init(void) /* Block ("allowlist") all devices by default. */ new->bpf_devices.list_type = LXC_BPF_DEVICE_CGROUP_ALLOWLIST; INIT_LIST_HEAD(&(new->bpf_devices).devices); - lxc_list_init(&new->mount_list); + INIT_LIST_HEAD(&new->mount_entries); INIT_LIST_HEAD(&new->caps.list); INIT_LIST_HEAD(&new->id_map); new->root_nsuid_map = NULL; @@ -4322,9 +4320,8 @@ int lxc_setup(struct lxc_handler *handler) if (ret < 0) return log_error(-1, "Failed to setup mounts"); - if (!lxc_list_empty(&lxc_conf->mount_list)) { - ret = setup_mount_entries(lxc_conf, &lxc_conf->rootfs, - &lxc_conf->mount_list, name, lxcpath); + if (!list_empty(&lxc_conf->mount_entries)) { + ret = setup_mount_entries(lxc_conf, &lxc_conf->rootfs, name, lxcpath); if (ret < 0) return log_error(-1, "Failed to setup mount entries"); } @@ -4667,15 +4664,15 @@ int lxc_clear_environment(struct lxc_conf *c) int lxc_clear_mount_entries(struct lxc_conf *c) { - struct lxc_list *it, *next; + struct string_entry *entry, *nentry; - lxc_list_for_each_safe (it, &c->mount_list, next) { - lxc_list_del(it); - free(it->elem); - free(it); + list_for_each_entry_safe(entry, nentry, &c->mount_entries, head) { + list_del(&entry->head); + free(entry->val); + free(entry); } - lxc_list_init(&c->mount_list); + INIT_LIST_HEAD(&c->mount_entries); return 0; } diff --git a/src/lxc/conf.h b/src/lxc/conf.h index 270b7f8d2..53075b3f8 100644 --- a/src/lxc/conf.h +++ b/src/lxc/conf.h @@ -354,6 +354,11 @@ struct caps { struct list_head list; }; +struct string_entry { + char *val; + struct list_head head; +}; + struct lxc_conf { /* Pointer to the name of the container. Do not free! */ const char *name; @@ -389,7 +394,7 @@ struct lxc_conf { struct { char *fstab; int auto_mounts; - struct lxc_list mount_list; + struct list_head mount_entries; }; struct caps caps; @@ -567,7 +572,8 @@ __hidden extern int parse_lxc_mount_attrs(struct lxc_mount_options *opts, char * __hidden extern int parse_mount_attrs(struct lxc_mount_options *opts, const char *mntopts); __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); +__hidden extern FILE *make_anonymous_mount_file(const struct list_head *mount, + bool include_nesting_helpers); __hidden extern int run_script(const char *name, const char *section, const char *script, ...); __hidden extern int run_script_argv(const char *name, unsigned int hook_version, const char *section, const char *script, const char *hookname, char **argsin); diff --git a/src/lxc/confile.c b/src/lxc/confile.c index 8aad86d16..03bdef077 100644 --- a/src/lxc/confile.c +++ b/src/lxc/confile.c @@ -2348,21 +2348,22 @@ static int set_config_mount(const char *key, const char *value, struct lxc_conf *lxc_conf, void *data) { __do_free char *mntelem = NULL; - __do_free struct lxc_list *mntlist = NULL; + __do_free struct string_entry *entry = NULL; if (lxc_config_value_empty(value)) return lxc_clear_mount_entries(lxc_conf); - mntlist = lxc_list_new(); - if (!mntlist) + entry = zalloc(sizeof(struct string_entry)); + if (!entry) return ret_errno(ENOMEM); mntelem = strdup(value); if (!mntelem) return ret_errno(ENOMEM); - mntlist->elem = move_ptr(mntelem); - lxc_list_add_tail(&lxc_conf->mount_list, move_ptr(mntlist)); + entry->val = move_ptr(mntelem); + list_add_tail(&entry->head, &lxc_conf->mount_entries); + move_ptr(entry); return 0; } @@ -4136,15 +4137,15 @@ static int get_config_mount(const char *key, char *retv, int inlen, struct lxc_conf *c, void *data) { int len, fulllen = 0; - struct lxc_list *it; + struct string_entry *entry; if (!retv) inlen = 0; else memset(retv, 0, inlen); - lxc_list_for_each(it, &c->mount_list) { - strprint(retv, inlen, "%s\n", (char *)it->elem); + list_for_each_entry(entry, &c->mount_entries, head) { + strprint(retv, inlen, "%s\n", entry->val); } return fulllen; diff --git a/src/lxc/criu.c b/src/lxc/criu.c index f936bea14..569940b4d 100644 --- a/src/lxc/criu.c +++ b/src/lxc/criu.c @@ -246,7 +246,7 @@ static int exec_criu(struct cgroup_ops *cgroup_ops, struct lxc_conf *conf, if (opts->user->action_script) static_args += 2; - static_args += 2 * lxc_list_len(&opts->c->lxc_conf->mount_list); + static_args += 2 * list_len(&opts->c->lxc_conf->mount_entries); ret = strnprintf(log, sizeof(log), "%s/%s.log", opts->user->directory, opts->action); if (ret < 0) @@ -348,7 +348,7 @@ static int exec_criu(struct cgroup_ops *cgroup_ops, struct lxc_conf *conf, DECLARE_ARG(opts->user->action_script); } - f_mnt = make_anonymous_mount_file(&opts->c->lxc_conf->mount_list, + f_mnt = make_anonymous_mount_file(&opts->c->lxc_conf->mount_entries, opts->c->lxc_conf->lsm_aa_allow_nesting); if (!f_mnt) return log_error_errno(-ENOENT, ENOENT, "Failed to create anonymous mount file"); diff --git a/src/lxc/storage/overlay.c b/src/lxc/storage/overlay.c index 1479a9ce8..410b4ff5a 100644 --- a/src/lxc/storage/overlay.c +++ b/src/lxc/storage/overlay.c @@ -656,7 +656,7 @@ err: /* To be called from lxcapi_clone() in lxccontainer.c: When we clone a container * with overlay lxc.mount.entry entries we need to update absolute paths for * upper- and workdir. This update is done in two locations: - * lxc_conf->unexpanded_config and lxc_conf->mount_list. Both updates are done + * lxc_conf->unexpanded_config and lxc_conf->mount_entries. Both updates are done * independent of each other since lxc_conf->mountlist may contain more mount * entries (e.g. from other included files) than lxc_conf->unexpanded_config. */ @@ -667,7 +667,7 @@ int ovl_update_abs_paths(struct lxc_conf *lxc_conf, const char *lxc_path, char new_upper[PATH_MAX], new_work[PATH_MAX], old_upper[PATH_MAX], old_work[PATH_MAX]; size_t i; - struct lxc_list *iterator; + struct string_entry *entry; char *cleanpath = NULL; int fret = -1; int ret = 0; @@ -681,7 +681,7 @@ int ovl_update_abs_paths(struct lxc_conf *lxc_conf, const char *lxc_path, /* * We have to update lxc_conf->unexpanded_config separately from - * lxc_conf->mount_list. + * lxc_conf->mount_entries. */ for (i = 0; i < sizeof(ovl_dirs) / sizeof(ovl_dirs[0]); i++) { if (!clone_update_unexp_ovl_paths(lxc_conf, lxc_path, newpath, @@ -700,11 +700,11 @@ int ovl_update_abs_paths(struct lxc_conf *lxc_conf, const char *lxc_path, if (ret < 0 || ret >= PATH_MAX) goto err; - lxc_list_for_each(iterator, &lxc_conf->mount_list) { + list_for_each_entry(entry, &lxc_conf->mount_entries, head) { char *mnt_entry = NULL, *new_mnt_entry = NULL, *tmp = NULL, *tmp_mnt_entry = NULL; - mnt_entry = iterator->elem; + mnt_entry = entry->val; if (strstr(mnt_entry, "overlay")) tmp = "upperdir"; @@ -721,26 +721,26 @@ int ovl_update_abs_paths(struct lxc_conf *lxc_conf, const char *lxc_path, if (ret < 0 || ret >= PATH_MAX) goto err; - if (strstr(mnt_entry, old_upper)) { - tmp_mnt_entry = - lxc_string_replace(old_upper, new_upper, mnt_entry); - } + if (strstr(mnt_entry, old_upper)) + tmp_mnt_entry = lxc_string_replace(old_upper, new_upper, mnt_entry); if (strstr(mnt_entry, old_work)) { if (tmp_mnt_entry) - new_mnt_entry = lxc_string_replace( - old_work, new_work, tmp_mnt_entry); + new_mnt_entry = lxc_string_replace(old_work, + new_work, + tmp_mnt_entry); else - new_mnt_entry = lxc_string_replace( - old_work, new_work, mnt_entry); + new_mnt_entry = lxc_string_replace(old_work, + new_work, + mnt_entry); } if (new_mnt_entry) { - free(iterator->elem); - iterator->elem = strdup(new_mnt_entry); + free(entry->val); + entry->val = strdup(new_mnt_entry); } else if (tmp_mnt_entry) { - free(iterator->elem); - iterator->elem = strdup(tmp_mnt_entry); + free(entry->val); + entry->val = strdup(tmp_mnt_entry); } free(new_mnt_entry); diff --git a/src/lxc/storage/overlay.h b/src/lxc/storage/overlay.h index d4c780ff4..bed876e80 100644 --- a/src/lxc/storage/overlay.h +++ b/src/lxc/storage/overlay.h @@ -35,9 +35,10 @@ __hidden extern int ovl_umount(struct lxc_storage *bdev); /* To be called from lxcapi_clone() in lxccontainer.c: When we clone a container * with overlay lxc.mount.entry entries we need to update absolute paths for * upper- and workdir. This update is done in two locations: - * lxc_conf->unexpanded_config and lxc_conf->mount_list. Both updates are done - * independent of each other since lxc_conf->mountlist may container more mount - * entries (e.g. from other included files) than lxc_conf->unexpanded_config . + * lxc_conf->unexpanded_config and lxc_conf->mount_entries. Both updates are + * done independent of each other since lxc_conf->mountlist may container more + * mount entries (e.g. from other included files) than + * lxc_conf->unexpanded_config . */ __hidden extern int ovl_update_abs_paths(struct lxc_conf *lxc_conf, const char *lxc_path, const char *lxc_name, const char *newpath,