[lxc-devel] [lxcfs/master] tree-wide: fixes

brauner on Github lxc-bot at linuxcontainers.org
Tue Mar 17 09:09:33 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 365 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200317/ca594c7b/attachment.bin>
-------------- next part --------------
From 2a5d697d25393c01a86550095a272b98a6d024fa Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Tue, 17 Mar 2020 10:05:53 +0100
Subject: [PATCH 1/2] tree-wide: remove is_relative() and use
 must_make_path_relative()

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/cgroups/cgroup_utils.h |  5 -----
 src/proc_loadavg.c         | 20 ++++++--------------
 2 files changed, 6 insertions(+), 19 deletions(-)

diff --git a/src/cgroups/cgroup_utils.h b/src/cgroups/cgroup_utils.h
index e626e3f..3b9d971 100644
--- a/src/cgroups/cgroup_utils.h
+++ b/src/cgroups/cgroup_utils.h
@@ -102,11 +102,6 @@ static inline const char *dot_or_empty(const char *s)
 	return (*s == '/') ? dot : empty;
 }
 
-static inline bool is_relative(const char *s)
-{
-	return s && *s != '/';
-}
-
 #define must_make_path_relative(__first__, ...)                                \
 	({                                                                     \
 		char *__ptr__;                                                 \
diff --git a/src/proc_loadavg.c b/src/proc_loadavg.c
index cb8a738..a8c01d0 100644
--- a/src/proc_loadavg.c
+++ b/src/proc_loadavg.c
@@ -499,9 +499,8 @@ static struct load_node *del_node(struct load_node *n, int locate)
 static void *load_begin(void *arg)
 {
 
-	int sum, length, ret;
+	int first_node, sum;
 	struct load_node *f;
-	int first_node;
 	clock_t time1, time2;
 
 	for (;;) {
@@ -521,22 +520,14 @@ static void *load_begin(void *arg)
 			while (f) {
 				__do_free char *path = NULL;
 
-				length = strlen(f->cg) + 2;
-					/* strlen(f->cg) + '.' or '' + \0 */
-				path = malloc(length);
-				if  (!path)
-					goto out;
-
-				ret = snprintf(path, length, "%s%s", !is_relative(f->cg) ? "." : "", f->cg);
-				/* Ignore the node if snprintf fails.*/
-				if (ret < 0 || ret > length - 1)
-					log_error(goto out, "Refresh node %s failed for snprintf()", f->cg);
+				path = must_make_path_relative(f->cg, NULL);
 
 				sum = refresh_load(f, path);
 				if (sum == 0)
 					f = del_node(f, i);
 				else
-out:					f = f->next;
+					f = f->next;
+
 				/* load_hash[i].lock locks only on the first node.*/
 				if (first_node == 1) {
 					first_node = 0;
@@ -549,7 +540,8 @@ out:					f = f->next;
 			return NULL;
 
 		time2 = clock();
-		usleep(FLUSH_TIME * 1000000 - (int)((time2 - time1) * 1000000 / CLOCKS_PER_SEC));
+		usleep(FLUSH_TIME * 1000000 -
+		       (int)((time2 - time1) * 1000000 / CLOCKS_PER_SEC));
 	}
 }
 

From 3c5e82301c70f1fb2feaa26058a34cbbe7299dab Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Tue, 17 Mar 2020 10:08:21 +0100
Subject: [PATCH 2/2] cgroup_fuse: rework is_child_cgroup()

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/cgroup_fuse.c | 21 ++++++---------------
 1 file changed, 6 insertions(+), 15 deletions(-)

diff --git a/src/cgroup_fuse.c b/src/cgroup_fuse.c
index 2e4aa2c..a93614b 100644
--- a/src/cgroup_fuse.c
+++ b/src/cgroup_fuse.c
@@ -162,28 +162,19 @@ static void get_cgdir_and_path(const char *cg, char **dir, char **last)
 	*p = '\0';
 }
 
-static bool is_child_cgroup(const char *controller, const char *cgroup, const char *f)
+static bool is_child_cgroup(const char *controller, const char *cgroup,
+			    const char *file)
 {
-	int cfd;
-	size_t len;
-	char *fnam;
-	int ret;
+	__do_free char *path = NULL;
+	int cfd, ret;
 	struct stat sb;
 
 	cfd = get_cgroup_fd_handle_named(controller);
 	if (cfd < 0)
 		return false;
 
-	/* Make sure we pass a relative path to *at() family of functions.
-	 * . + /cgroup + / + f + \0
-	 */
-	len = strlen(cgroup) + strlen(f) + 3;
-	fnam = alloca(len);
-	ret = snprintf(fnam, len, "%s%s/%s", dot_or_empty(cgroup), cgroup, f);
-	if (ret < 0 || (size_t)ret >= len)
-		return false;
-
-	ret = fstatat(cfd, fnam, &sb, 0);
+	path = must_make_path_relative(cgroup, file, NULL);
+	ret = fstatat(cfd, path, &sb, 0);
 	if (ret < 0 || !S_ISDIR(sb.st_mode))
 		return false;
 


More information about the lxc-devel mailing list