From 223797c313c0be4ea8e1cca64b3ad1215004c020 Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Wed, 25 Aug 2021 18:38:06 +0200 Subject: [PATCH] conf: port rlimits to new list type Signed-off-by: Christian Brauner --- src/lxc/attach.c | 10 +++------- src/lxc/conf.c | 23 ++++++++++------------- src/lxc/conf.h | 6 ++++-- src/lxc/confile.c | 37 ++++++++++++++----------------------- src/lxc/start.c | 10 ++++------ 5 files changed, 35 insertions(+), 51 deletions(-) diff --git a/src/lxc/attach.c b/src/lxc/attach.c index 4bfdf23cf..b1289e0eb 100644 --- a/src/lxc/attach.c +++ b/src/lxc/attach.c @@ -1668,13 +1668,9 @@ int lxc_attach(struct lxc_container *container, lxc_attach_exec_t exec_function, } /* Setup resource limits */ - if (!lxc_list_empty(&conf->limits)) { - ret = setup_resource_limits(&conf->limits, pid); - if (ret < 0) - goto on_error; - - TRACE("Setup resource limits"); - } + ret = setup_resource_limits(conf, pid); + if (ret < 0) + goto on_error; if (options->attach_flags & LXC_ATTACH_TERMINAL) { ret = lxc_attach_terminal_mainloop_init(&terminal, &descr); diff --git a/src/lxc/conf.c b/src/lxc/conf.c index 34cb24689..41c23967f 100644 --- a/src/lxc/conf.c +++ b/src/lxc/conf.c @@ -3249,15 +3249,15 @@ static int parse_resource(const char *res) return resid; } -int setup_resource_limits(struct lxc_list *limits, pid_t pid) +int setup_resource_limits(struct lxc_conf *conf, pid_t pid) { int resid; - struct lxc_list *it; struct lxc_limit *lim; - lxc_list_for_each (it, limits) { - lim = it->elem; + if (list_empty(&conf->limits)) + return 0; + list_for_each_entry(lim, &conf->limits, head) { resid = parse_resource(lim->resource); if (resid < 0) return log_error(-1, "Unknown resource %s", lim->resource); @@ -3272,6 +3272,7 @@ int setup_resource_limits(struct lxc_list *limits, pid_t pid) #endif } + TRACE("Setup resource limits"); return 0; } @@ -3388,7 +3389,7 @@ struct lxc_conf *lxc_conf_init(void) lxc_list_init(&new->includes); lxc_list_init(&new->aliens); lxc_list_init(&new->environment); - lxc_list_init(&new->limits); + INIT_LIST_HEAD(&new->limits); lxc_list_init(&new->sysctls); lxc_list_init(&new->procs); new->hooks_version = 0; @@ -4588,9 +4589,9 @@ static inline void lxc_clear_cgroups_devices(struct lxc_conf *conf) int lxc_clear_limits(struct lxc_conf *c, const char *key) { - struct lxc_list *it, *next; const char *k = NULL; bool all = false; + struct lxc_limit *lim, *nlim; if (strequal(key, "lxc.limit") || strequal(key, "lxc.prlimit")) all = true; @@ -4601,21 +4602,17 @@ int lxc_clear_limits(struct lxc_conf *c, const char *key) else return ret_errno(EINVAL); - lxc_list_for_each_safe (it, &c->limits, next) { - struct lxc_limit *lim = it->elem; - + list_for_each_entry_safe(lim, nlim, &c->limits, head) { if (!all && !strequal(lim->resource, k)) continue; - lxc_list_del(it); - + list_del(&lim->head); free_disarm(lim->resource); free(lim); - free(it); } if (all) - lxc_list_init(&c->limits); + INIT_LIST_HEAD(&c->limits); return 0; } diff --git a/src/lxc/conf.h b/src/lxc/conf.h index e5b57737c..9de181d00 100644 --- a/src/lxc/conf.h +++ b/src/lxc/conf.h @@ -95,6 +95,7 @@ define_cleanup_function(struct lxc_cgroup *, free_lxc_cgroup); struct rlimit { unsigned long rlim_cur; unsigned long rlim_max; + struct list_head head; }; #endif @@ -106,6 +107,7 @@ struct rlimit { struct lxc_limit { char *resource; struct rlimit limit; + struct list_head head; }; static void free_lxc_limit(struct lxc_limit *ptr) @@ -468,7 +470,7 @@ struct lxc_conf { bool no_new_privs; /* RLIMIT_* limits */ - struct lxc_list limits; + struct list_head limits; /* Contains generic info about the cgroup configuration for this * container. Note that struct lxc_cgroup contains a union. It is only @@ -539,7 +541,7 @@ __hidden extern int lxc_setup_rootfs_prepare_root(struct lxc_conf *conf, const c const char *lxcpath); __hidden extern int lxc_setup(struct lxc_handler *handler); __hidden extern int lxc_setup_parent(struct lxc_handler *handler); -__hidden extern int setup_resource_limits(struct lxc_list *limits, pid_t pid); +__hidden extern int setup_resource_limits(struct lxc_conf *conf, pid_t pid); __hidden extern int find_unmapped_nsid(const struct lxc_conf *conf, enum idtype idtype); __hidden extern int mapped_hostid(unsigned id, const struct lxc_conf *conf, enum idtype idtype); __hidden extern int userns_exec_1(const struct lxc_conf *conf, int (*fn)(void *), void *data, diff --git a/src/lxc/confile.c b/src/lxc/confile.c index d63b6b60a..2bf7dcd8d 100644 --- a/src/lxc/confile.c +++ b/src/lxc/confile.c @@ -2056,11 +2056,10 @@ static bool parse_limit_value(const char **value, rlim_t *res) static int set_config_prlimit(const char *key, const char *value, struct lxc_conf *lxc_conf, void *data) { - __do_free struct lxc_list *list = NULL; - call_cleaner(free_lxc_limit) struct lxc_limit *elem = NULL; - struct lxc_list *iter; + call_cleaner(free_lxc_limit) struct lxc_limit *new_lim = NULL; struct rlimit limit; rlim_t limit_value; + struct lxc_limit *lim; if (lxc_config_value_empty(value)) return lxc_clear_limits(lxc_conf, key); @@ -2107,32 +2106,25 @@ static int set_config_prlimit(const char *key, const char *value, } /* find existing list element */ - lxc_list_for_each(iter, &lxc_conf->limits) { - struct lxc_limit *cur = iter->elem; - - if (!strequal(key, cur->resource)) + list_for_each_entry(lim, &lxc_conf->limits, head) { + if (!strequal(key, lim->resource)) continue; - cur->limit = limit; + lim->limit = limit; return 0; } - /* allocate list element */ - list = lxc_list_new(); - if (!list) - return ret_errno(ENOMEM); - - elem = zalloc(sizeof(*elem)); - if (!elem) + new_lim = zalloc(sizeof(*new_lim)); + if (!new_lim) return ret_errno(ENOMEM); - elem->resource = strdup(key); - if (!elem->resource) + new_lim->resource = strdup(key); + if (!new_lim->resource) return ret_errno(ENOMEM); - elem->limit = limit; - lxc_list_add_elem(list, move_ptr(elem));; - lxc_list_add_tail(&lxc_conf->limits, move_ptr(list)); + new_lim->limit = limit; + list_add_tail(&new_lim->head, &lxc_conf->limits); + move_ptr(new_lim); return 0; } @@ -4561,7 +4553,7 @@ static int get_config_prlimit(const char *key, char *retv, int inlen, { int fulllen = 0, len; bool get_all = false; - struct lxc_list *it; + struct lxc_limit *lim; if (!retv) inlen = 0; @@ -4575,11 +4567,10 @@ static int get_config_prlimit(const char *key, char *retv, int inlen, else return ret_errno(EINVAL); - lxc_list_for_each(it, &c->limits) { + list_for_each_entry(lim, &c->limits, head) { /* 2 colon separated 64 bit integers or the word 'unlimited' */ char buf[INTTYPE_TO_STRLEN(uint64_t) * 2 + 2]; int partlen; - struct lxc_limit *lim = it->elem; if (lim->limit.rlim_cur == RLIM_INFINITY) { memcpy(buf, "unlimited", STRLITERALLEN("unlimited") + 1); diff --git a/src/lxc/start.c b/src/lxc/start.c index 41f820279..1d4d22908 100644 --- a/src/lxc/start.c +++ b/src/lxc/start.c @@ -1822,12 +1822,10 @@ static int lxc_spawn(struct lxc_handler *handler) goto out_delete_net; } - if (!lxc_list_empty(&conf->limits)) { - ret = setup_resource_limits(&conf->limits, handler->pid); - if (ret < 0) { - ERROR("Failed to setup resource limits"); - goto out_delete_net; - } + ret = setup_resource_limits(conf, handler->pid); + if (ret < 0) { + ERROR("Failed to setup resource limits"); + goto out_delete_net; } /* Tell the child to continue its initialization. */ -- 2.47.2