Blame SOURCES/libcgroup-0.41-change-cgroup-of-threads.patch

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