From a64edc1c8d5b917180419b7b28a75f50217cbc2f Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Thu, 27 Jun 2019 14:25:53 +0200 Subject: [PATCH 01/10] cgroups: use __do_free Signed-off-by: Christian Brauner --- src/lxc/cgroups/cgfsng.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/lxc/cgroups/cgfsng.c b/src/lxc/cgroups/cgfsng.c index 4cf67950a..bb0c90981 100644 --- a/src/lxc/cgroups/cgfsng.c +++ b/src/lxc/cgroups/cgfsng.c @@ -2754,7 +2754,7 @@ __cgfsng_ops static bool cgfsng_data_init(struct cgroup_ops *ops) struct cgroup_ops *cgfsng_ops_init(struct lxc_conf *conf) { - struct cgroup_ops *cgfsng_ops; + __do_free struct cgroup_ops *cgfsng_ops = NULL; cgfsng_ops = malloc(sizeof(struct cgroup_ops)); if (!cgfsng_ops) @@ -2763,10 +2763,8 @@ struct cgroup_ops *cgfsng_ops_init(struct lxc_conf *conf) memset(cgfsng_ops, 0, sizeof(struct cgroup_ops)); cgfsng_ops->cgroup_layout = CGROUP_LAYOUT_UNKNOWN; - if (!cg_init(cgfsng_ops, conf)) { - free(cgfsng_ops); + if (!cg_init(cgfsng_ops, conf)) return NULL; - } cgfsng_ops->data_init = cgfsng_data_init; cgfsng_ops->payload_destroy = cgfsng_payload_destroy; @@ -2790,5 +2788,5 @@ struct cgroup_ops *cgfsng_ops_init(struct lxc_conf *conf) cgfsng_ops->mount = cgfsng_mount; cgfsng_ops->nrtasks = cgfsng_nrtasks; - return cgfsng_ops; + return move_ptr(cgfsng_ops); } From 431e2c547f282dbcfac49f81a92fd718698b1806 Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Thu, 27 Jun 2019 14:26:14 +0200 Subject: [PATCH 02/10] cgroups: move variables into tighter scope Signed-off-by: Christian Brauner --- src/lxc/cgroups/cgfsng.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/lxc/cgroups/cgfsng.c b/src/lxc/cgroups/cgfsng.c index bb0c90981..ae2f5dcf9 100644 --- a/src/lxc/cgroups/cgfsng.c +++ b/src/lxc/cgroups/cgfsng.c @@ -2411,15 +2411,13 @@ __cgfsng_ops static bool cgfsng_setup_limits(struct cgroup_ops *ops, static bool cgroup_use_wants_controllers(const struct cgroup_ops *ops, char **controllers) { - char **cur_ctrl, **cur_use; - if (!ops->cgroup_use) return true; - for (cur_ctrl = controllers; cur_ctrl && *cur_ctrl; cur_ctrl++) { + for (char **cur_ctrl = controllers; cur_ctrl && *cur_ctrl; cur_ctrl++) { bool found = false; - for (cur_use = ops->cgroup_use; cur_use && *cur_use; cur_use++) { + for (char **cur_use = ops->cgroup_use; cur_use && *cur_use; cur_use++) { if (strcmp(*cur_use, *cur_ctrl) != 0) continue; From 6280d4c97dd3b72aa2f72cf44b9af888a03052df Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Thu, 27 Jun 2019 14:26:38 +0200 Subject: [PATCH 03/10] cgroups: simplify cgfsng_setup_limits() Signed-off-by: Christian Brauner --- src/lxc/cgroups/cgfsng.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/lxc/cgroups/cgfsng.c b/src/lxc/cgroups/cgfsng.c index ae2f5dcf9..daa2a75d3 100644 --- a/src/lxc/cgroups/cgfsng.c +++ b/src/lxc/cgroups/cgfsng.c @@ -2396,13 +2396,10 @@ static bool __cg_unified_setup_limits(struct cgroup_ops *ops, } __cgfsng_ops static bool cgfsng_setup_limits(struct cgroup_ops *ops, - struct lxc_conf *conf, - bool do_devices) + struct lxc_conf *conf, + bool do_devices) { - bool bret; - - bret = __cg_legacy_setup_limits(ops, &conf->cgroup, do_devices); - if (!bret) + if (!__cg_legacy_setup_limits(ops, &conf->cgroup, do_devices)) return false; return __cg_unified_setup_limits(ops, &conf->cgroup2); From c05b17bd66142da80ab9031cb19ee8e4441c59e9 Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Thu, 27 Jun 2019 14:27:39 +0200 Subject: [PATCH 04/10] cgroups: use __do_free in cgfsng_attach() Signed-off-by: Christian Brauner --- src/lxc/cgroups/cgfsng.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lxc/cgroups/cgfsng.c b/src/lxc/cgroups/cgfsng.c index daa2a75d3..b0f90f222 100644 --- a/src/lxc/cgroups/cgfsng.c +++ b/src/lxc/cgroups/cgfsng.c @@ -2097,8 +2097,7 @@ __cgfsng_ops static bool cgfsng_attach(struct cgroup_ops *ops, const char *name, return false; for (i = 0; ops->hierarchies[i]; i++) { - __do_free char *path = NULL; - char *fullpath = NULL; + __do_free char *fullpath = NULL, *path = NULL; struct hierarchy *h = ops->hierarchies[i]; if (h->version == CGROUP2_SUPER_MAGIC) { From 81b5d48a34ef5d20a8795aa322002045cc20b5df Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Thu, 27 Jun 2019 14:27:58 +0200 Subject: [PATCH 05/10] cgroups: move variable into tighter scope Signed-off-by: Christian Brauner --- src/lxc/cgroups/cgfsng.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lxc/cgroups/cgfsng.c b/src/lxc/cgroups/cgfsng.c index b0f90f222..f29ccd1bc 100644 --- a/src/lxc/cgroups/cgfsng.c +++ b/src/lxc/cgroups/cgfsng.c @@ -2086,7 +2086,7 @@ on_error: __cgfsng_ops static bool cgfsng_attach(struct cgroup_ops *ops, const char *name, const char *lxcpath, pid_t pid) { - int i, len, ret; + int len, ret; char pidstr[INTTYPE_TO_STRLEN(pid_t)]; if (!ops->hierarchies) @@ -2096,7 +2096,7 @@ __cgfsng_ops static bool cgfsng_attach(struct cgroup_ops *ops, const char *name, if (len < 0 || (size_t)len >= sizeof(pidstr)) return false; - for (i = 0; ops->hierarchies[i]; i++) { + for (int i = 0; ops->hierarchies[i]; i++) { __do_free char *fullpath = NULL, *path = NULL; struct hierarchy *h = ops->hierarchies[i]; From 779b3d82e647c79a99e7cee42809b91e001e1024 Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Thu, 27 Jun 2019 14:43:09 +0200 Subject: [PATCH 06/10] cgroups: move variable into tighter scope Signed-off-by: Christian Brauner --- src/lxc/cgroups/cgfsng.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/lxc/cgroups/cgfsng.c b/src/lxc/cgroups/cgfsng.c index f29ccd1bc..7971cea60 100644 --- a/src/lxc/cgroups/cgfsng.c +++ b/src/lxc/cgroups/cgfsng.c @@ -1912,12 +1912,10 @@ __cgfsng_ops static int cgfsng_nrtasks(struct cgroup_ops *ops) __cgfsng_ops static bool cgfsng_escape(const struct cgroup_ops *ops, struct lxc_conf *conf) { - int i; - if (conf->cgroup_meta.relative || geteuid() || !ops->hierarchies) return true; - for (i = 0; ops->hierarchies[i]; i++) { + for (int i = 0; ops->hierarchies[i]; i++) { int ret; __do_free char *fullpath = NULL; From 3312a94ff4152154a59dd77316e29c24e3a779ec Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Thu, 27 Jun 2019 14:43:36 +0200 Subject: [PATCH 07/10] cgroups: simplify cgfsng_nrtasks() Signed-off-by: Christian Brauner --- src/lxc/cgroups/cgfsng.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/lxc/cgroups/cgfsng.c b/src/lxc/cgroups/cgfsng.c index 7971cea60..4205298b5 100644 --- a/src/lxc/cgroups/cgfsng.c +++ b/src/lxc/cgroups/cgfsng.c @@ -1898,14 +1898,12 @@ static int recursive_count_nrtasks(char *dirname) __cgfsng_ops static int cgfsng_nrtasks(struct cgroup_ops *ops) { __do_free char *path = NULL; - int count; if (!ops->container_cgroup || !ops->hierarchies) return -1; path = must_make_path(ops->hierarchies[0]->container_full_path, NULL); - count = recursive_count_nrtasks(path); - return count; + return recursive_count_nrtasks(path); } /* Only root needs to escape to the cgroup of its init. */ From dfa835ac211f3828b68fab7ef89ffd1ca7aeccb6 Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Thu, 27 Jun 2019 14:45:36 +0200 Subject: [PATCH 08/10] cgroups: move variable into tighter scope Signed-off-by: Christian Brauner --- src/lxc/cgroups/cgfsng.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lxc/cgroups/cgfsng.c b/src/lxc/cgroups/cgfsng.c index 4205298b5..78ab8e7e6 100644 --- a/src/lxc/cgroups/cgfsng.c +++ b/src/lxc/cgroups/cgfsng.c @@ -1765,7 +1765,7 @@ __cgfsng_ops static bool cgfsng_mount(struct cgroup_ops *ops, const char *root, int type) { __do_free char *tmpfspath = NULL; - int i, ret; + int ret; bool has_cgns = false, retval = false, wants_force_mount = false; if (!ops->hierarchies) @@ -1803,7 +1803,7 @@ __cgfsng_ops static bool cgfsng_mount(struct cgroup_ops *ops, if (ret < 0) goto on_error; - for (i = 0; ops->hierarchies[i]; i++) { + for (int i = 0; ops->hierarchies[i]; i++) { __do_free char *controllerpath = NULL, *path2 = NULL; struct hierarchy *h = ops->hierarchies[i]; char *controller = strrchr(h->mountpoint, '/'); From f6b54668ef6172191c2a83dac050b3e1dd0cbdb6 Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Thu, 27 Jun 2019 14:46:47 +0200 Subject: [PATCH 09/10] cgroups: correctly order variables Signed-off-by: Christian Brauner --- src/lxc/cgroups/cgfsng.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lxc/cgroups/cgfsng.c b/src/lxc/cgroups/cgfsng.c index 78ab8e7e6..7e16f7a0c 100644 --- a/src/lxc/cgroups/cgfsng.c +++ b/src/lxc/cgroups/cgfsng.c @@ -1715,10 +1715,10 @@ static int cg_legacy_mount_controllers(int type, struct hierarchy *h, static int __cg_mount_direct(int type, struct hierarchy *h, const char *controllerpath) { - int ret; __do_free char *controllers = NULL; char *fstype = "cgroup2"; unsigned long flags = 0; + int ret; flags |= MS_NOSUID; flags |= MS_NOEXEC; From 6a720d7480123425ba83652816c946c40244cbe6 Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Thu, 27 Jun 2019 14:48:34 +0200 Subject: [PATCH 10/10] cgroups: move variable into tighter scope Signed-off-by: Christian Brauner --- src/lxc/cgroups/cgfsng.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lxc/cgroups/cgfsng.c b/src/lxc/cgroups/cgfsng.c index 7e16f7a0c..619727267 100644 --- a/src/lxc/cgroups/cgfsng.c +++ b/src/lxc/cgroups/cgfsng.c @@ -1546,7 +1546,7 @@ static int chowmod(char *path, uid_t chown_uid, gid_t chown_gid, */ static int chown_cgroup_wrapper(void *data) { - int i, ret; + int ret; uid_t destuid; struct generic_userns_exec_data *arg = data; uid_t nsuid = (arg->conf->root_nsuid_map != NULL) ? 0 : arg->conf->init_uid; @@ -1576,7 +1576,7 @@ static int chown_cgroup_wrapper(void *data) if (destuid == LXC_INVALID_UID) destuid = 0; - for (i = 0; arg->hierarchies[i]; i++) { + for (int i = 0; arg->hierarchies[i]; i++) { __do_free char *fullpath = NULL; char *path = arg->hierarchies[i]->container_full_path;