From 8cfab97eaec2b3a8066db7ad69a132912c13b4e4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois-Xavier=20Bourlet?= <fx@dotcloud.com>
Date: Sun, 20 Mar 2011 05:12:59 +0100
Subject: [PATCH] lxc_cgroup_path_get, cache the right value

---
 src/lxc/cgroup.c |   29 +++++++++++++----------------
 1 files changed, 13 insertions(+), 16 deletions(-)

diff --git a/src/lxc/cgroup.c b/src/lxc/cgroup.c
index 69761b4..a068a01 100644
--- a/src/lxc/cgroup.c
+++ b/src/lxc/cgroup.c
@@ -47,8 +47,6 @@ lxc_log_define(lxc_cgroup, lxc);
 
 #define MTAB "/proc/mounts"
 
-static char nsgroup_path[MAXPATHLEN];
-
 enum {
 	CGROUP_NS_CGROUP = 1,
 	CGROUP_CLONE_CHILDREN,
@@ -291,22 +289,21 @@ int lxc_cgroup_destroy(const char *name)
 
 int lxc_cgroup_path_get(char **path, const char *name)
 {
-	char cgroup[MAXPATHLEN];
-
-	*path = &nsgroup_path[0];
-
-	/*
-	 * report nsgroup_path string if already set
-	 */
-	if (**path != 0)
-		return 0;
-
-	if (get_cgroup_mount(MTAB, cgroup)) {
-		ERROR("cgroup is not mounted");
-		return -1;
+	static char        cgroup[MAXPATHLEN];
+	static const char* cgroup_cached = 0;
+	static char        buf[MAXPATHLEN];
+
+	if (!cgroup_cached) {
+		if (get_cgroup_mount(MTAB, cgroup)) {
+			ERROR("cgroup is not mounted");
+			return -1;
+		} else {
+			cgroup_cached = cgroup;
+		}
 	}
 
-	snprintf(nsgroup_path, MAXPATHLEN, "%s/%s", cgroup, name);
+	snprintf(buf, MAXPATHLEN, "%s/%s", cgroup_cached, name);
+	*path = buf;
 	return 0;
 }
 
-- 
1.7.1

