]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
tree-wide: port cgroup cleanup to call_cleaner(cgroup_exit)
authorChristian Brauner <christian.brauner@ubuntu.com>
Sun, 15 Mar 2020 00:55:24 +0000 (01:55 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Sun, 15 Mar 2020 00:55:24 +0000 (01:55 +0100)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/attach.c
src/lxc/cgroups/cgroup.h
src/lxc/freezer.c
src/lxc/lxccontainer.c

index 26fd753ecce1683280a617867c0bca4184ecb4b3..d147a59ed178c180945ea0574b5d3cce270890cf 100644 (file)
@@ -1176,7 +1176,7 @@ int lxc_attach(struct lxc_container *container, lxc_attach_exec_t exec_function,
                         */
                        ret = cgroup_attach(name, lxcpath, pid);
                        if (ret) {
-                               __do_cgroup_exit struct cgroup_ops *cgroup_ops = NULL;
+                               call_cleaner(cgroup_exit) struct cgroup_ops *cgroup_ops = NULL;
 
                                cgroup_ops = cgroup_init(conf);
                                if (!cgroup_ops)
index 309f31fae7324c997a2dd040ff110658b15e1b1b..9f44ad51babd17fd0cce45329e155bf9c067c900 100644 (file)
@@ -8,6 +8,7 @@
 #include <sys/types.h>
 
 #include "macro.h"
+#include "memory_utils.h"
 
 #define DEFAULT_CGROUP_MOUNTPOINT "/sys/fs/cgroup"
 #define DEFAULT_PAYLOAD_CGROUP_PREFIX "lxc.payload."
@@ -171,20 +172,14 @@ struct cgroup_ops {
 };
 
 extern struct cgroup_ops *cgroup_init(struct lxc_conf *conf);
+
 extern void cgroup_exit(struct cgroup_ops *ops);
+define_cleanup_function(struct cgroup_ops *, cgroup_exit);
 
 extern void prune_init_scope(char *cg);
 
-static inline void __auto_cgroup_exit__(struct cgroup_ops **ops)
-{
-       if (*ops)
-               cgroup_exit(*ops);
-}
-
 extern int cgroup_attach(const char *name, const char *lxcpath, int64_t pid);
 
-#define __do_cgroup_exit __attribute__((__cleanup__(__auto_cgroup_exit__)))
-
 static inline bool pure_unified_layout(const struct cgroup_ops *ops)
 {
        return ops->cgroup_layout == CGROUP_LAYOUT_UNIFIED;
index 13b77561589e17032a573e4a0e3c32437015dce2..dbb70f61df369d006c6a19e330e9e338ef9dcc38 100644 (file)
@@ -35,7 +35,7 @@ static void notify_state_listeners(const char *name, const char *lxcpath,
 static int do_freeze_thaw(bool freeze, struct lxc_conf *conf, const char *name,
                          const char *lxcpath)
 {
-       __do_cgroup_exit struct cgroup_ops *cgroup_ops = NULL;
+       call_cleaner(cgroup_exit) struct cgroup_ops *cgroup_ops = NULL;
        lxc_state_t new_state = freeze ? FROZEN : THAWED;
        int ret;
        const char *state;
index 0c69670aaefcac6ec571cd2718a21b26e5499c17..42e3b562f70a27e52a5651dcf7b896e848e136be 100644 (file)
@@ -3308,8 +3308,7 @@ WRAP_API_1(bool, lxcapi_set_config_path, const char *)
 
 static bool do_lxcapi_set_cgroup_item(struct lxc_container *c, const char *subsys, const char *value)
 {
-       int ret;
-       struct cgroup_ops *cgroup_ops;
+       call_cleaner(cgroup_exit) struct cgroup_ops *cgroup_ops = NULL;
 
        if (!c)
                return false;
@@ -3321,19 +3320,15 @@ static bool do_lxcapi_set_cgroup_item(struct lxc_container *c, const char *subsy
        if (!cgroup_ops)
                return false;
 
-       ret = cgroup_ops->set(cgroup_ops, subsys, value, c->name, c->config_path);
-
-       cgroup_exit(cgroup_ops);
-
-       return ret == 0;
+       return cgroup_ops->set(cgroup_ops, subsys, value, c->name,
+                              c->config_path) == 0;
 }
 
 WRAP_API_2(bool, lxcapi_set_cgroup_item, const char *, const char *)
 
 static int do_lxcapi_get_cgroup_item(struct lxc_container *c, const char *subsys, char *retv, int inlen)
 {
-       int ret;
-       struct cgroup_ops *cgroup_ops;
+       call_cleaner(cgroup_exit) struct cgroup_ops *cgroup_ops = NULL;
 
        if (!c)
                return -1;
@@ -3345,12 +3340,8 @@ static int do_lxcapi_get_cgroup_item(struct lxc_container *c, const char *subsys
        if (!cgroup_ops)
                return -1;
 
-       ret = cgroup_ops->get(cgroup_ops, subsys, retv, inlen, c->name,
-                             c->config_path);
-
-       cgroup_exit(cgroup_ops);
-
-       return ret;
+       return cgroup_ops->get(cgroup_ops, subsys, retv, inlen, c->name,
+                              c->config_path);
 }
 
 WRAP_API_3(int, lxcapi_get_cgroup_item, const char *, char *, int)