From: Christian Brauner Date: Fri, 27 Aug 2021 12:57:51 +0000 (+0200) Subject: conf: port hooks to new list type X-Git-Tag: lxc-5.0.0~97^2~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5090de3e62238efa119947ef6fc0b307459c2a56;p=thirdparty%2Flxc.git conf: port hooks to new list type Signed-off-by: Christian Brauner --- diff --git a/src/lxc/conf.c b/src/lxc/conf.c index dc14708cd..789540b0a 100644 --- a/src/lxc/conf.c +++ b/src/lxc/conf.c @@ -3374,7 +3374,7 @@ struct lxc_conf *lxc_conf_init(void) INIT_LIST_HEAD(&new->procs); new->hooks_version = 0; for (i = 0; i < NUM_LXC_HOOKS; i++) - lxc_list_init(&new->hooks[i]); + INIT_LIST_HEAD(&new->hooks[i]); lxc_list_init(&new->groups); INIT_LIST_HEAD(&new->state_clients); new->lsm_aa_profile = NULL; @@ -3922,11 +3922,11 @@ int lxc_setup_rootfs_prepare_root(struct lxc_conf *conf, const char *name, static bool verify_start_hooks(struct lxc_conf *conf) { char path[PATH_MAX]; - struct lxc_list *it; + struct string_entry *hook; - lxc_list_for_each (it, &conf->hooks[LXCHOOK_START]) { + list_for_each_entry(hook, &conf->hooks[LXCHOOK_START], head) { int ret; - char *hookname = it->elem; + char *hookname = hook->val; ret = strnprintf(path, sizeof(path), "%s%s", conf->rootfs.path ? conf->rootfs.mount : "", @@ -4422,8 +4422,8 @@ int lxc_setup(struct lxc_handler *handler) int run_lxc_hooks(const char *name, char *hookname, struct lxc_conf *conf, char *argv[]) { - struct lxc_list *it; int which; + struct string_entry *entry; for (which = 0; which < NUM_LXC_HOOKS; which ++) { if (strequal(hookname, lxchook_names[which])) @@ -4433,9 +4433,9 @@ int run_lxc_hooks(const char *name, char *hookname, struct lxc_conf *conf, if (which >= NUM_LXC_HOOKS) return -1; - lxc_list_for_each (it, &conf->hooks[which]) { + list_for_each_entry(entry, &conf->hooks[which], head) { int ret; - char *hook = it->elem; + char *hook = entry->val; ret = run_script_argv(name, conf->hooks_version, "lxc", hook, hookname, argv); @@ -4684,9 +4684,9 @@ int lxc_clear_automounts(struct lxc_conf *c) int lxc_clear_hooks(struct lxc_conf *c, const char *key) { - struct lxc_list *it, *next; const char *k = NULL; bool all = false, done = false; + struct string_entry *entry, *nentry; if (strequal(key, "lxc.hook")) all = true; @@ -4697,13 +4697,12 @@ int lxc_clear_hooks(struct lxc_conf *c, const char *key) for (int i = 0; i < NUM_LXC_HOOKS; i++) { if (all || strequal(k, lxchook_names[i])) { - lxc_list_for_each_safe (it, &c->hooks[i], next) { - lxc_list_del(it); - free(it->elem); - free(it); + list_for_each_entry_safe(entry, nentry, &c->hooks[i], head) { + list_del(&entry->head); + free(entry->val); + free(entry); } - lxc_list_init(&c->hooks[i]); - + INIT_LIST_HEAD(&c->hooks[i]); done = true; } } diff --git a/src/lxc/conf.h b/src/lxc/conf.h index af5de4dc2..5fcae6364 100644 --- a/src/lxc/conf.h +++ b/src/lxc/conf.h @@ -416,7 +416,7 @@ struct lxc_conf { struct { unsigned int hooks_version; - struct lxc_list hooks[NUM_LXC_HOOKS]; + struct list_head hooks[NUM_LXC_HOOKS]; }; char *lsm_aa_profile; diff --git a/src/lxc/confile.c b/src/lxc/confile.c index c2f9a0bc2..096dff349 100644 --- a/src/lxc/confile.c +++ b/src/lxc/confile.c @@ -1113,14 +1113,15 @@ static int set_config_net_script_down(const char *key, const char *value, static int add_hook(struct lxc_conf *lxc_conf, int which, __owns char *hook) { __do_free char *val = hook; - struct lxc_list *hooklist; + __do_free struct string_entry *entry; - hooklist = lxc_list_new(); - if (!hooklist) + entry = zalloc(sizeof(struct string_entry)); + if (!entry) return ret_errno(ENOMEM); - hooklist->elem = move_ptr(val); - lxc_list_add_tail(&lxc_conf->hooks[which], hooklist); + entry->val = move_ptr(val); + list_add_tail(&entry->head, &lxc_conf->hooks[which]); + move_ptr(entry); return 0; } @@ -4189,7 +4190,7 @@ static int get_config_hooks(const char *key, char *retv, int inlen, { char *subkey; int len, fulllen = 0, found = -1; - struct lxc_list *it; + struct string_entry *entry; int i; subkey = strchr(key, '.'); @@ -4218,8 +4219,8 @@ static int get_config_hooks(const char *key, char *retv, int inlen, else memset(retv, 0, inlen); - lxc_list_for_each(it, &c->hooks[found]) { - strprint(retv, inlen, "%s\n", (char *)it->elem); + list_for_each_entry(entry, &c->hooks[found], head) { + strprint(retv, inlen, "%s\n", entry->val); } return fulllen; diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c index 8b38f0511..a4168f777 100644 --- a/src/lxc/lxccontainer.c +++ b/src/lxc/lxccontainer.c @@ -2961,7 +2961,7 @@ static bool container_destroy(struct lxc_container *c, goto out; } - if (conf && !lxc_list_empty(&conf->hooks[LXCHOOK_DESTROY])) { + if (conf && !list_empty(&conf->hooks[LXCHOOK_DESTROY])) { /* Start of environment variable setup for hooks */ if (setenv("LXC_NAME", c->name, 1)) SYSERROR("Failed to set environment variable for container name"); @@ -3400,26 +3400,30 @@ static int copyhooks(struct lxc_container *oldc, struct lxc_container *c) { __do_free char *cpath = NULL; int i, len, ret; - struct lxc_list *it; + struct string_entry *entry; len = strlen(oldc->config_path) + strlen(oldc->name) + 3; - cpath = must_realloc(NULL, len); + cpath = malloc(len); + if (!cpath) + return ret_errno(ENOMEM); + ret = strnprintf(cpath, len, "%s/%s/", oldc->config_path, oldc->name); if (ret < 0) return -1; - for (i=0; ilxc_conf->hooks[i]) { - char *hookname = it->elem; - char *fname = strrchr(hookname, '/'); + for (i = 0; i < NUM_LXC_HOOKS; i++) { + list_for_each_entry(entry, &c->lxc_conf->hooks[i], head) { + __do_free char *hookname = NULL; + char *fname, *new_hook; char tmppath[PATH_MAX]; - if (!fname) /* relative path - we don't support, but maybe we should */ + + fname = strrchr(hookname, '/'); + if (!fname) return 0; - if (!strnequal(hookname, cpath, len - 1)) { - /* this hook is public - ignore */ + /* If this hook is public - ignore. */ + if (!strnequal(hookname, cpath, len - 1)) continue; - } /* copy the script, and change the entry in confile */ ret = strnprintf(tmppath, sizeof(tmppath), "%s/%s/%s", @@ -3427,31 +3431,28 @@ static int copyhooks(struct lxc_container *oldc, struct lxc_container *c) if (ret < 0) return -1; - ret = copy_file(it->elem, tmppath); + ret = copy_file(entry->val, tmppath); if (ret < 0) return -1; - free(it->elem); + new_hook = strdup(tmppath); + if (!new_hook) + return syserror("out of memory copying hook path"); - it->elem = strdup(tmppath); - if (!it->elem) { - ERROR("out of memory copying hook path"); - return -1; - } + hookname = move_ptr(entry->val); + entry->val = move_ptr(new_hook); } } if (!clone_update_unexp_hooks(c->lxc_conf, oldc->config_path, - c->config_path, oldc->name, c->name)) { - ERROR("Error saving new hooks in clone"); - return -1; + c->config_path, oldc->name, c->name)) { + return syserror_ret(-1, "Error saving new hooks in clone"); } do_lxcapi_save_config(c, NULL); return 0; } - static int copy_fstab(struct lxc_container *oldc, struct lxc_container *c) { char newpath[PATH_MAX]; @@ -3686,7 +3687,7 @@ static int clone_update_rootfs(struct clone_update_data *data) bdev->dest = strdup(lxc_storage_get_path(bdev->src, bdev->type)); } - if (!lxc_list_empty(&conf->hooks[LXCHOOK_CLONE])) { + if (!list_empty(&conf->hooks[LXCHOOK_CLONE])) { /* Start of environment variable setup for hooks */ if (c0->name && setenv("LXC_SRC_NAME", c0->name, 1)) SYSERROR("failed to set environment variable for source container name");