From: Christian Brauner Date: Wed, 25 Aug 2021 16:55:10 +0000 (+0200) Subject: conf: port procs to new list type X-Git-Tag: lxc-5.0.0~102^2~12 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=91d04bf9db0218f3efd801c538158f488bea6d81;p=thirdparty%2Flxc.git conf: port procs to new list type Signed-off-by: Christian Brauner --- diff --git a/src/lxc/attach.c b/src/lxc/attach.c index b1289e0eb..396001963 100644 --- a/src/lxc/attach.c +++ b/src/lxc/attach.c @@ -1659,13 +1659,9 @@ int lxc_attach(struct lxc_container *container, lxc_attach_exec_t exec_function, goto on_error; /* Setup /proc limits */ - if (!lxc_list_empty(&conf->procs)) { - ret = setup_proc_filesystem(&conf->procs, pid); - if (ret < 0) - goto on_error; - - TRACE("Setup /proc/%d settings", pid); - } + ret = setup_proc_filesystem(conf, pid); + if (ret < 0) + goto on_error; /* Setup resource limits */ ret = setup_resource_limits(conf, pid); diff --git a/src/lxc/conf.c b/src/lxc/conf.c index f4bb53c9f..d048874ef 100644 --- a/src/lxc/conf.c +++ b/src/lxc/conf.c @@ -3305,30 +3305,33 @@ int setup_sysctl_parameters(struct lxc_conf *conf) return 0; } -int setup_proc_filesystem(struct lxc_list *procs, pid_t pid) +int setup_proc_filesystem(struct lxc_conf *conf, pid_t pid) { __do_free char *tmp = NULL; - struct lxc_list *it; - struct lxc_proc *elem; int ret = 0; char filename[PATH_MAX] = {0}; + struct lxc_proc *proc; + + if (!list_empty(&conf->procs)) + return 0; - lxc_list_for_each (it, procs) { - elem = it->elem; - tmp = lxc_string_replace(".", "/", elem->filename); + list_for_each_entry(proc, &conf->procs, head) { + tmp = lxc_string_replace(".", "/", proc->filename); if (!tmp) - return log_error(-1, "Failed to replace key %s", elem->filename); + return log_error(-1, "Failed to replace key %s", proc->filename); ret = strnprintf(filename, sizeof(filename), "/proc/%d/%s", pid, tmp); if (ret < 0) return log_error(-1, "Error setting up proc filesystem path"); - ret = lxc_write_to_file(filename, elem->value, - strlen(elem->value), false, 0666); + ret = lxc_write_to_file(filename, proc->value, + strlen(proc->value), false, 0666); if (ret < 0) - return log_error_errno(-1, errno, "Failed to setup proc filesystem %s to %s", elem->filename, elem->value); + return log_error_errno(-1, errno, "Failed to setup proc filesystem %s to %s", + proc->filename, proc->value); } + TRACE("Setup /proc/%d settings", pid); return 0; } @@ -3392,7 +3395,7 @@ struct lxc_conf *lxc_conf_init(void) lxc_list_init(&new->environment); INIT_LIST_HEAD(&new->limits); INIT_LIST_HEAD(&new->sysctls); - lxc_list_init(&new->procs); + INIT_LIST_HEAD(&new->procs); new->hooks_version = 0; for (i = 0; i < NUM_LXC_HOOKS; i++) lxc_list_init(&new->hooks[i]); @@ -4647,9 +4650,9 @@ int lxc_clear_sysctls(struct lxc_conf *c, const char *key) int lxc_clear_procs(struct lxc_conf *c, const char *key) { - struct lxc_list *it, *next; const char *k = NULL; bool all = false; + struct lxc_proc *proc, *nproc; if (strequal(key, "lxc.proc")) all = true; @@ -4658,21 +4661,18 @@ int lxc_clear_procs(struct lxc_conf *c, const char *key) else return -1; - lxc_list_for_each_safe(it, &c->procs, next) { - struct lxc_proc *proc = it->elem; - + list_for_each_entry_safe(proc, nproc, &c->procs, head) { if (!all && !strequal(proc->filename, k)) continue; - lxc_list_del(it); + list_del(&proc->head); free(proc->filename); free(proc->value); free(proc); - free(it); } if (all) - lxc_list_init(&c->procs); + INIT_LIST_HEAD(&c->procs); return 0; } diff --git a/src/lxc/conf.h b/src/lxc/conf.h index 59cbce602..00b4a5849 100644 --- a/src/lxc/conf.h +++ b/src/lxc/conf.h @@ -153,6 +153,7 @@ define_cleanup_function(struct lxc_sysctl *, free_lxc_sysctl); struct lxc_proc { char *filename; char *value; + struct list_head head; }; static void free_lxc_proc(struct lxc_proc *ptr) @@ -496,7 +497,7 @@ struct lxc_conf { struct list_head sysctls; /* procs */ - struct lxc_list procs; + struct list_head procs; struct shmount { /* Absolute path to the shared mount point on the host */ @@ -575,7 +576,7 @@ static inline bool lxc_wants_cap(int cap, struct lxc_conf *conf) __hidden extern int setup_sysctl_parameters(struct lxc_conf *conf); __hidden extern int lxc_clear_sysctls(struct lxc_conf *c, const char *key); -__hidden extern int setup_proc_filesystem(struct lxc_list *procs, pid_t pid); +__hidden extern int setup_proc_filesystem(struct lxc_conf *conf, pid_t pid); __hidden extern int lxc_clear_procs(struct lxc_conf *c, const char *key); __hidden extern int lxc_clear_apparmor_raw(struct lxc_conf *c); __hidden extern int lxc_clear_namespace(struct lxc_conf *c); diff --git a/src/lxc/confile.c b/src/lxc/confile.c index 4d3437d6d..97b822b1a 100644 --- a/src/lxc/confile.c +++ b/src/lxc/confile.c @@ -2183,8 +2183,7 @@ static int set_config_sysctl(const char *key, const char *value, static int set_config_proc(const char *key, const char *value, struct lxc_conf *lxc_conf, void *data) { - __do_free struct lxc_list *proclist = NULL; - call_cleaner(free_lxc_proc) struct lxc_proc *procelem = NULL; + call_cleaner(free_lxc_proc) struct lxc_proc *new_proc = NULL; const char *subkey; if (lxc_config_value_empty(value)) @@ -2197,24 +2196,20 @@ static int set_config_proc(const char *key, const char *value, if (*subkey == '\0') return ret_errno(EINVAL); - proclist = lxc_list_new(); - if (!proclist) + new_proc = zalloc(sizeof(*new_proc)); + if (!new_proc) return ret_errno(ENOMEM); - procelem = zalloc(sizeof(*procelem)); - if (!procelem) + new_proc->filename = strdup(subkey); + if (!new_proc->filename) return ret_errno(ENOMEM); - procelem->filename = strdup(subkey); - if (!procelem->filename) + new_proc->value = strdup(value); + if (!new_proc->value) return ret_errno(ENOMEM); - procelem->value = strdup(value); - if (!procelem->value) - return ret_errno(ENOMEM); - - proclist->elem = move_ptr(procelem); - lxc_list_add_tail(&lxc_conf->procs, move_ptr(proclist)); + list_add_tail(&new_proc->head, &lxc_conf->procs); + move_ptr(new_proc); return 0; } @@ -4632,10 +4627,10 @@ static int get_config_sysctl(const char *key, char *retv, int inlen, static int get_config_proc(const char *key, char *retv, int inlen, struct lxc_conf *c, void *data) { - struct lxc_list *it; - int len; int fulllen = 0; bool get_all = false; + int len; + struct lxc_proc *proc; if (!retv) inlen = 0; @@ -4649,9 +4644,7 @@ static int get_config_proc(const char *key, char *retv, int inlen, else return ret_errno(EINVAL); - lxc_list_for_each(it, &c->procs) { - struct lxc_proc *proc = it->elem; - + list_for_each_entry(proc, &c->procs, head) { if (get_all) { strprint(retv, inlen, "lxc.proc.%s = %s\n", proc->filename, proc->value); diff --git a/src/lxc/start.c b/src/lxc/start.c index 1d4d22908..4f09e4e5c 100644 --- a/src/lxc/start.c +++ b/src/lxc/start.c @@ -1816,10 +1816,10 @@ static int lxc_spawn(struct lxc_handler *handler) } } - if (!lxc_list_empty(&conf->procs)) { - ret = setup_proc_filesystem(&conf->procs, handler->pid); - if (ret < 0) - goto out_delete_net; + ret = setup_proc_filesystem(conf, handler->pid); + if (ret < 0) { + ERROR("Failed to setup procfs limits"); + goto out_delete_net; } ret = setup_resource_limits(conf, handler->pid);