[lxc-devel] [lxc/master] cgroup: remove __attribute__((constructor))

brauner on Github lxc-bot at linuxcontainers.org
Fri May 18 18:18:07 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 364 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180518/fac89e19/attachment.bin>
-------------- next part --------------
From a687702d3d098d306d9dca3a6e928fe09dcb361c Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Fri, 18 May 2018 20:16:22 +0200
Subject: [PATCH] cgroup: remove __attribute__((constructor))

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/attach.c         |  7 +++++--
 src/lxc/cgroups/cgfsng.c | 22 +++++++++++-----------
 src/lxc/cgroups/cgroup.c | 48 ++++++++++++++++++++++++++++++++----------------
 src/lxc/start.c          |  4 +---
 4 files changed, 49 insertions(+), 32 deletions(-)

diff --git a/src/lxc/attach.c b/src/lxc/attach.c
index e1699b137..73bfc1b9a 100644
--- a/src/lxc/attach.c
+++ b/src/lxc/attach.c
@@ -1272,10 +1272,13 @@ int lxc_attach(const char *name, const char *lxcpath,
 
 		/* Attach to cgroup, if requested. */
 		if (options->attach_flags & LXC_ATTACH_MOVE_TO_CGROUP) {
+			if (!cgroup_init(NULL))
+				goto on_error;
+
 			if (!cgroup_attach(name, lxcpath, pid))
 				goto on_error;
-			TRACE("Moved intermediate process %d into container's "
-			      "cgroups", pid);
+
+			TRACE("Moved intermediate process %d into container's cgroups", pid);
 		}
 
 		/* Setup /proc limits */
diff --git a/src/lxc/cgroups/cgfsng.c b/src/lxc/cgroups/cgfsng.c
index 2f9c15c56..e12c839a2 100644
--- a/src/lxc/cgroups/cgfsng.c
+++ b/src/lxc/cgroups/cgfsng.c
@@ -1463,6 +1463,17 @@ static bool cg_init(void)
 	return cg_hybrid_init();
 }
 
+struct cgroup_ops *cgfsng_ops_init(void)
+{
+	if (getenv("LXC_DEBUG_CGFSNG"))
+		lxc_cgfsng_debug = true;
+
+	if (!cg_init())
+		return NULL;
+
+	return &cgfsng_ops;
+}
+
 static void *cgfsng_init(struct lxc_handler *handler)
 {
 	const char *cgroup_pattern;
@@ -1655,17 +1666,6 @@ static void cgfsng_destroy(void *hdata, struct lxc_conf *conf)
 	free_handler_data(d);
 }
 
-struct cgroup_ops *cgfsng_ops_init(void)
-{
-	if (getenv("LXC_DEBUG_CGFSNG"))
-		lxc_cgfsng_debug = true;
-
-	if (!cg_init())
-		return NULL;
-
-	return &cgfsng_ops;
-}
-
 static bool cg_unified_create_cgroup(struct hierarchy *h, char *cgname)
 {
 	size_t i, parts_len;
diff --git a/src/lxc/cgroups/cgroup.c b/src/lxc/cgroups/cgroup.c
index 9e7b26e00..351c82be7 100644
--- a/src/lxc/cgroups/cgroup.c
+++ b/src/lxc/cgroups/cgroup.c
@@ -36,32 +36,48 @@ static struct cgroup_ops *ops = NULL;
 
 extern struct cgroup_ops *cgfsng_ops_init(void);
 
-__attribute__((constructor)) void cgroup_ops_init(void)
+static bool cgroup_ops_init(void)
 {
-	if (ops) {
-		INFO("Running with %s in version %s", ops->driver, ops->version);
-		return;
-	}
+	if (ops)
+		return true;
 
-	DEBUG("cgroup_init");
 	ops = cgfsng_ops_init();
-	if (ops)
-		INFO("Initialized cgroup driver %s", ops->driver);
+	if (!ops) {
+		ERROR("Failed to initialize cgroup driver %s", ops->driver);
+		return false;
+	}
+	TRACE("Initialized cgroup driver %s", ops->driver);
+
+	return true;
 }
 
-bool cgroup_init(struct lxc_handler *handler)
+static bool cgroup_data_init(struct lxc_handler *handler)
 {
-	if (handler->cgroup_data) {
-		ERROR("cgroup_init called on already initialized handler");
+	if (handler->cgroup_data)
 		return true;
-	}
 
-	if (ops) {
-		INFO("cgroup driver %s initing for %s", ops->driver, handler->name);
-		handler->cgroup_data = ops->init(handler);
+	handler->cgroup_data = ops->init(handler);
+	if (!handler->cgroup_data) {
+		ERROR("Failed to initialize cgroup data for driver %s", ops->driver);
+		return false;
 	}
+	TRACE("Initialized cgroup data for driver %s", ops->driver);
+
+	return true;
+}
 
-	return handler->cgroup_data != NULL;
+bool cgroup_init(struct lxc_handler *handler)
+{
+	if (!cgroup_ops_init())
+		return false;
+
+	if (!handler)
+		return true;
+
+	if (!cgroup_data_init(handler))
+		return false;
+
+	return true;
 }
 
 void cgroup_destroy(struct lxc_handler *handler)
diff --git a/src/lxc/start.c b/src/lxc/start.c
index b8404f99e..166ade0a7 100644
--- a/src/lxc/start.c
+++ b/src/lxc/start.c
@@ -1567,10 +1567,8 @@ static int lxc_spawn(struct lxc_handler *handler)
 		}
 	}
 
-	if (!cgroup_init(handler)) {
-		ERROR("Failed initializing cgroup support");
+	if (!cgroup_init(handler))
 		goto out_delete_net;
-	}
 
 	cgroups_connected = true;
 


More information about the lxc-devel mailing list