[lxc-devel] [lxcfs/master] Don't tie entries in 'hierarchies' to their subsystem id

hallyn on Github lxc-bot at linuxcontainers.org
Wed Feb 3 01:36:00 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 581 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160203/08ea6bb4/attachment.bin>
-------------- next part --------------
From e86e51b0c21326059d64f62799693a63f095a3d7 Mon Sep 17 00:00:00 2001
From: Serge Hallyn <serge.hallyn at ubuntu.com>
Date: Tue, 2 Feb 2016 17:23:50 -0800
Subject: [PATCH] Don't tie entries in 'hierarchies' to their subsystem id

There's no place where we rely on it, and it gets out of whack
if cgroups have been unmounted+remounted.

This is an alternative to https://github.com/lxc/lxcfs/pull/70.
Thanks to smemsh for the report and proposed fix.

Signed-off-by: Serge Hallyn <serge.hallyn at ubuntu.com>
---
 bindings.c | 38 +++++++++++---------------------------
 1 file changed, 11 insertions(+), 27 deletions(-)

diff --git a/bindings.c b/bindings.c
index b51a5f3..ef96513 100644
--- a/bindings.c
+++ b/bindings.c
@@ -366,38 +366,22 @@ struct cgfs_files {
 	uint32_t mode;
 };
 
+#define ALLOC_NUM 20
 static bool store_hierarchy(char *stridx, char *h)
 {
-	int idx = atoi(stridx);
-	size_t needed_len = (idx + 1) * sizeof(char *);
-
-	if (idx < 0 || idx > 30) {
-		fprintf(stderr, "Error: corrupt /proc/self/cgroup\n");
-		return false;
-	}
-
-	if (!hierarchies) {
-		hierarchies = malloc(needed_len);
-		memset(hierarchies, 0, needed_len);
-		num_hierarchies = idx + 1;
-	} else if (idx >= num_hierarchies) {
-		char **tmp;
-		size_t old_len = (num_hierarchies + 1) * sizeof(char *);
-		do {
-			tmp = malloc(needed_len);
-		} while (!tmp);
-		memset(tmp, 0, needed_len);
-		memcpy(tmp, hierarchies, old_len);
-		free(hierarchies);
+	if (num_hierarchies % ALLOC_NUM == 0) {
+		size_t n = (num_hierarchies / ALLOC_NUM) + 1;
+		n *= ALLOC_NUM;
+		char **tmp = realloc(hierarchies, n * sizeof(char *));
+		printf("allocated %d\n", n);
+		if (!tmp) {
+			fprintf(stderr, "Out of memory\n");
+			exit(1);
+		}
 		hierarchies = tmp;
-		num_hierarchies = idx + 1;
 	}
 	
-	if (hierarchies[idx]) {
-		fprintf(stderr, "Error: corrupt /proc/self/cgroup\n");
-		return false;
-	}
-	hierarchies[idx] = must_copy_string(h);
+	hierarchies[num_hierarchies++] = must_copy_string(h);
 	return true;
 }
 


More information about the lxc-devel mailing list