aboutsummaryrefslogtreecommitdiffstats
path: root/recipes-containers/oci-systemd-hook/oci-systemd-hook/0001-Add-additional-cgroup-mounts-from-root-NS-automatica.patch
blob: 388eda3dec52326a45d60eb394415b487c55c01e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
From 12d92162c449d51f4ffa482f7daaeb42c4135937 Mon Sep 17 00:00:00 2001
From: Jason Wessel <jason.wessel@windriver.com>
Date: Tue, 2 Jul 2019 20:51:08 +0000
Subject: [PATCH] Add additional cgroup mounts from root NS automatically

Upstream-Status: Inappropriate [embedded specific]

Signed-off-by: Jason Wessel <jason.wessel@windriver.com>
---
 src/systemdhook.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/src/systemdhook.c b/src/systemdhook.c
index c2cb2b9..f9ec9f2 100644
--- a/src/systemdhook.c
+++ b/src/systemdhook.c
@@ -274,6 +274,11 @@ static char *get_process_cgroup_subsystem_path(const char *id, int pid, const ch
 static int mount_cgroup(const char *id, const char *rootfs, const char *options, char *systemd_path)
 {
 	_cleanup_free_ char *cgroup_path = NULL;
+	char *spath, *dpath;
+	DIR *dir;
+	struct dirent *d;
+	char link[80];
+	int got;
 
 	if (asprintf(&cgroup_path, "%s/%s", rootfs, CGROUP_ROOT) < 0) {
 		pr_perror("%s: Failed to create path for %s", id, CGROUP_ROOT);
@@ -292,6 +297,46 @@ static int mount_cgroup(const char *id, const char *rootfs, const char *options,
 		pr_perror("%s: Failed to mkdir new dest: %s", id, systemd_path);
 		return -1;
 	}
+	/* Create all additional cgroup mounts which are in the root namespace */
+	dir = opendir(CGROUP_ROOT);
+	if (!dir) {
+		pr_perror("Failed to open %s", CGROUP_ROOT);
+		return -1;
+	}
+	/* Skip "." and ".." */
+	readdir(dir);
+	readdir(dir);
+	while ((d = readdir(dir))) {
+		/* Systemd is already handled above */
+		if (strcmp(d->d_name, "systemd") == 0) {
+			continue;
+		}
+		if (asprintf(&spath, "%s/%s", CGROUP_ROOT, d->d_name) < 0) {
+			pr_perror("Failed to create path for %s", d->d_name);
+			return -1;
+		}
+		if (asprintf(&dpath, "%s%s/%s", rootfs, CGROUP_ROOT, d->d_name) < 0) {
+			pr_perror("Failed to create path for %s", d->d_name);
+			return -1;
+		}
+		got = readlink(spath, link, sizeof(link) - 1);
+		if (got > 0) {
+			link[got] = '\0';
+			symlink(link, dpath);
+		} else {
+			if ((makepath(dpath, 0755) == -1) && (errno != EEXIST)) {
+				pr_perror("Failed to mkdir new dest: %s", dpath);
+				return -1;
+			}
+			if (bind_mount(id, spath, dpath, false)) {
+				pr_perror("Failed to bind mount %s on %s", spath, dpath);
+				return -1;
+			}
+		}
+		free(spath);
+		free(dpath);
+	}
+	closedir(dir);
 	if (remount_readonly(id, cgroup_path, cgroup_path) < 0) {
 		return -1;
 	}
-- 
2.7.4