Skip to content

Commit

Permalink
confile: cleanup set_config_prlimit()
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
  • Loading branch information
Christian Brauner committed Dec 15, 2020
1 parent 50957b4 commit 88d3a23
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
9 changes: 9 additions & 0 deletions src/lxc/conf.h
Expand Up @@ -97,6 +97,15 @@ struct lxc_limit {
struct rlimit limit;
};

static void free_lxc_limit(struct lxc_limit *ptr)
{
if (ptr) {
free(ptr->resource);
free_disarm(ptr);
}
}
define_cleanup_function(struct lxc_limit *, free_lxc_limit);

enum idtype {
ID_TYPE_UID,
ID_TYPE_GID
Expand Down
34 changes: 12 additions & 22 deletions src/lxc/confile.c
Expand Up @@ -1733,23 +1733,23 @@ 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 *limlist = NULL;
call_cleaner(free_lxc_limit) struct lxc_limit *limelem = NULL;
struct lxc_list *iter;
struct rlimit limit;
rlim_t limit_value;
struct lxc_list *limlist = NULL;
struct lxc_limit *limelem = NULL;

if (lxc_config_value_empty(value))
return lxc_clear_limits(lxc_conf, key);

if (strncmp(key, "lxc.prlimit.", STRLITERALLEN("lxc.prlimit.")) != 0)
return -1;
return ret_errno(EINVAL);

key += STRLITERALLEN("lxc.prlimit.");

/* soft limit comes first in the value */
if (!parse_limit_value(&value, &limit_value))
return -1;
return ret_errno(EINVAL);

limit.rlim_cur = limit_value;

Expand All @@ -1760,15 +1760,15 @@ static int set_config_prlimit(const char *key, const char *value,
if (*value == ':')
++value;
else if (*value) /* any other character is an error here */
return -1;
return ret_errno(EINVAL);

while (isspace(*value))
++value;

/* optional hard limit */
if (*value) {
if (!parse_limit_value(&value, &limit_value))
return -1;
return ret_errno(EINVAL);

limit.rlim_max = limit_value;

Expand All @@ -1777,7 +1777,7 @@ static int set_config_prlimit(const char *key, const char *value,
++value;

if (*value)
return -1;
return ret_errno(EINVAL);
} else {
/* a single value sets both hard and soft limit */
limit.rlim_max = limit.rlim_cur;
Expand All @@ -1795,32 +1795,22 @@ static int set_config_prlimit(const char *key, const char *value,
/* allocate list element */
limlist = malloc(sizeof(*limlist));
if (!limlist)
goto on_error;
return ret_errno(ENOMEM);

limelem = malloc(sizeof(*limelem));
if (!limelem)
goto on_error;
return ret_errno(ENOMEM);
memset(limelem, 0, sizeof(*limelem));

limelem->resource = strdup(key);
if (!limelem->resource)
goto on_error;
return ret_errno(ENOMEM);

limelem->limit = limit;
lxc_list_add_elem(limlist, limelem);;
lxc_list_add_tail(&lxc_conf->limits, limlist);
lxc_list_add_elem(limlist, move_ptr(limelem));;
lxc_list_add_tail(&lxc_conf->limits, move_ptr(limlist));

return 0;

on_error:
free(limlist);

if (limelem) {
free(limelem->resource);
free(limelem);
}

return -1;
}

static int set_config_sysctl(const char *key, const char *value,
Expand Down

0 comments on commit 88d3a23

Please sign in to comment.