Blame SOURCES/libcgroup-0.41-change-cgroup-of-every-thread.patch

0a7b38
From c9d651cfd532da6494dab03190c0a207cdf7289c Mon Sep 17 00:00:00 2001
0a7b38
From: =?UTF-8?q?Nikola=20Forr=C3=B3?= <nforro@redhat.com>
0a7b38
Date: Mon, 23 Jul 2018 17:38:26 +0200
0a7b38
Subject: [PATCH] api.c: always move all tasks of a process to a cgroup
0a7b38
0a7b38
---
0a7b38
 src/api.c | 40 ++++++++++++++++++++++++++++++++++++++--
0a7b38
 1 file changed, 38 insertions(+), 2 deletions(-)
0a7b38
0a7b38
diff --git a/src/api.c b/src/api.c
0a7b38
index 803ff35..a2cf981 100644
0a7b38
--- a/src/api.c
0a7b38
+++ b/src/api.c
0a7b38
@@ -3180,7 +3180,12 @@ int cgroup_change_cgroup_path(const char *dest, pid_t pid,
0a7b38
 				const char *const controllers[])
0a7b38
 {
0a7b38
 	int ret;
0a7b38
+	int nr;
0a7b38
 	struct cgroup cgroup;
0a7b38
+	DIR *dir;
0a7b38
+	struct dirent *task_dir = NULL;
0a7b38
+	char path[FILENAME_MAX];
0a7b38
+	pid_t tid;
0a7b38
 
0a7b38
 	if (!cgroup_initialized) {
0a7b38
 		cgroup_warn("Warning: libcgroup is not initialized\n");
0a7b38
@@ -3191,11 +3196,42 @@ int cgroup_change_cgroup_path(const char *dest, pid_t pid,
0a7b38
 	ret = cg_prepare_cgroup(&cgroup, pid, dest, controllers);
0a7b38
 	if (ret)
0a7b38
 		return ret;
0a7b38
-	/* Add task to cgroup */
0a7b38
+	/* Add process to cgroup */
0a7b38
 	ret = cgroup_attach_task_pid(&cgroup, pid);
0a7b38
-	if (ret)
0a7b38
+	if (ret) {
0a7b38
 		cgroup_warn("Warning: cgroup_attach_task_pid failed: %d\n",
0a7b38
 				ret);
0a7b38
+		goto finished;
0a7b38
+	}
0a7b38
+
0a7b38
+	/* Add all threads to cgroup */
0a7b38
+	snprintf(path, FILENAME_MAX, "/proc/%d/task/", pid);
0a7b38
+	dir = opendir(path);
0a7b38
+	if (!dir) {
0a7b38
+		last_errno = errno;
0a7b38
+		ret = ECGOTHER;
0a7b38
+		goto finished;
0a7b38
+	}
0a7b38
+
0a7b38
+	while ((task_dir = readdir(dir)) != NULL) {
0a7b38
+		nr = sscanf(task_dir->d_name, "%i", &tid;;
0a7b38
+		if (nr < 1)
0a7b38
+			continue;
0a7b38
+
0a7b38
+		if (tid == pid)
0a7b38
+			continue;
0a7b38
+
0a7b38
+		ret = cgroup_attach_task_pid(&cgroup, tid);
0a7b38
+		if (ret) {
0a7b38
+			cgroup_warn("Warning: cgroup_attach_task_pid failed: %d\n",
0a7b38
+					ret);
0a7b38
+			break;
0a7b38
+		}
0a7b38
+	}
0a7b38
+
0a7b38
+	closedir(dir);
0a7b38
+
0a7b38
+finished:
0a7b38
 	cgroup_free_controllers(&cgroup);
0a7b38
 	return ret;
0a7b38
 }
0a7b38
-- 
0a7b38
2.17.1
0a7b38