c480ed
From a756729403915485dbd3cbdf4f252806318c227c Mon Sep 17 00:00:00 2001
c480ed
Message-Id: <a756729403915485dbd3cbdf4f252806318c227c@dist-git>
c480ed
From: Pavel Hrdina <phrdina@redhat.com>
c480ed
Date: Mon, 1 Jul 2019 17:06:37 +0200
c480ed
Subject: [PATCH] vircgroup: extract virCgroupV1AddTask
c480ed
MIME-Version: 1.0
c480ed
Content-Type: text/plain; charset=UTF-8
c480ed
Content-Transfer-Encoding: 8bit
c480ed
c480ed
Reviewed-by: Fabiano Fidêncio <fidencio@redhat.com>
c480ed
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c480ed
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c480ed
(cherry picked from commit 064024e70afd6ad08993444b3839eb3ba67426f2)
c480ed
c480ed
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1689297
c480ed
c480ed
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c480ed
Message-Id: <558f1a90ce9b0fc5a9688c5a44bf85dd523c37e5.1561993100.git.phrdina@redhat.com>
c480ed
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c480ed
---
c480ed
 src/util/vircgroup.c        | 56 ++++---------------------------------
c480ed
 src/util/vircgroupbackend.h | 20 +++++++++++++
c480ed
 src/util/vircgrouppriv.h    |  5 ++++
c480ed
 src/util/vircgroupv1.c      | 31 ++++++++++++++++++++
c480ed
 4 files changed, 62 insertions(+), 50 deletions(-)
c480ed
c480ed
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
c480ed
index 33d34d6eda..a5076cb554 100644
c480ed
--- a/src/util/vircgroup.c
c480ed
+++ b/src/util/vircgroup.c
c480ed
@@ -544,7 +544,7 @@ virCgroupSetValueU64(virCgroupPtr group,
c480ed
 }
c480ed
 
c480ed
 
c480ed
-static int
c480ed
+int
c480ed
 virCgroupSetValueI64(virCgroupPtr group,
c480ed
                      int controller,
c480ed
                      const char *key,
c480ed
@@ -672,50 +672,6 @@ virCgroupNew(pid_t pid,
c480ed
 }
c480ed
 
c480ed
 
c480ed
-typedef enum {
c480ed
-    /* Adds a whole process with all threads to specific cgroup except
c480ed
-     * to systemd named controller. */
c480ed
-    VIR_CGROUP_TASK_PROCESS = 1 << 0,
c480ed
-
c480ed
-    /* Same as VIR_CGROUP_TASK_PROCESS but it also adds the task to systemd
c480ed
-     * named controller. */
c480ed
-    VIR_CGROUP_TASK_SYSTEMD = 1 << 1,
c480ed
-
c480ed
-    /* Moves only specific thread into cgroup except to systemd
c480ed
-     * named controller. */
c480ed
-    VIR_CGROUP_TASK_THREAD = 1 << 2,
c480ed
-} virCgroupTaskFlags;
c480ed
-
c480ed
-
c480ed
-static int
c480ed
-virCgroupAddTaskInternal(virCgroupPtr group,
c480ed
-                         pid_t pid,
c480ed
-                         unsigned int flags)
c480ed
-{
c480ed
-    int ret = -1;
c480ed
-    size_t i;
c480ed
-
c480ed
-    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
c480ed
-        /* Skip over controllers not mounted */
c480ed
-        if (!group->controllers[i].mountPoint)
c480ed
-            continue;
c480ed
-
c480ed
-        /* We must never add tasks in systemd's hierarchy
c480ed
-         * unless we're intentionally trying to move a
c480ed
-         * task into a systemd machine scope */
c480ed
-        if (i == VIR_CGROUP_CONTROLLER_SYSTEMD &&
c480ed
-            !(flags & VIR_CGROUP_TASK_SYSTEMD))
c480ed
-            continue;
c480ed
-
c480ed
-        if (virCgroupSetValueI64(group, i, "tasks", pid) < 0)
c480ed
-            goto cleanup;
c480ed
-    }
c480ed
-
c480ed
-    ret = 0;
c480ed
- cleanup:
c480ed
-    return ret;
c480ed
-}
c480ed
-
c480ed
 /**
c480ed
  * virCgroupAddProcess:
c480ed
  *
c480ed
@@ -730,7 +686,7 @@ virCgroupAddTaskInternal(virCgroupPtr group,
c480ed
 int
c480ed
 virCgroupAddProcess(virCgroupPtr group, pid_t pid)
c480ed
 {
c480ed
-    return virCgroupAddTaskInternal(group, pid, VIR_CGROUP_TASK_PROCESS);
c480ed
+    return group->backend->addTask(group, pid, VIR_CGROUP_TASK_PROCESS);
c480ed
 }
c480ed
 
c480ed
 /**
c480ed
@@ -747,9 +703,9 @@ virCgroupAddProcess(virCgroupPtr group, pid_t pid)
c480ed
 int
c480ed
 virCgroupAddMachineProcess(virCgroupPtr group, pid_t pid)
c480ed
 {
c480ed
-    return virCgroupAddTaskInternal(group, pid,
c480ed
-                                    VIR_CGROUP_TASK_PROCESS |
c480ed
-                                    VIR_CGROUP_TASK_SYSTEMD);
c480ed
+    return group->backend->addTask(group, pid,
c480ed
+                                   VIR_CGROUP_TASK_PROCESS |
c480ed
+                                   VIR_CGROUP_TASK_SYSTEMD);
c480ed
 }
c480ed
 
c480ed
 /**
c480ed
@@ -767,7 +723,7 @@ int
c480ed
 virCgroupAddThread(virCgroupPtr group,
c480ed
                    pid_t pid)
c480ed
 {
c480ed
-    return virCgroupAddTaskInternal(group, pid, VIR_CGROUP_TASK_THREAD);
c480ed
+    return group->backend->addTask(group, pid, VIR_CGROUP_TASK_THREAD);
c480ed
 }
c480ed
 
c480ed
 
c480ed
diff --git a/src/util/vircgroupbackend.h b/src/util/vircgroupbackend.h
c480ed
index 1f28c51c49..bff1f8938e 100644
c480ed
--- a/src/util/vircgroupbackend.h
c480ed
+++ b/src/util/vircgroupbackend.h
c480ed
@@ -35,6 +35,20 @@ typedef enum {
c480ed
                                        */
c480ed
 } virCgroupBackendFlags;
c480ed
 
c480ed
+typedef enum {
c480ed
+    /* Adds a whole process with all threads to specific cgroup except
c480ed
+     * to systemd named controller. */
c480ed
+    VIR_CGROUP_TASK_PROCESS = 1 << 0,
c480ed
+
c480ed
+    /* Same as VIR_CGROUP_TASK_PROCESS but it also adds the task to systemd
c480ed
+     * named controller. */
c480ed
+    VIR_CGROUP_TASK_SYSTEMD = 1 << 1,
c480ed
+
c480ed
+    /* Moves only specific thread into cgroup except to systemd
c480ed
+     * named controller. */
c480ed
+    VIR_CGROUP_TASK_THREAD = 1 << 2,
c480ed
+} virCgroupBackendTaskFlags;
c480ed
+
c480ed
 typedef enum {
c480ed
     VIR_CGROUP_BACKEND_TYPE_V1 = 0,
c480ed
     VIR_CGROUP_BACKEND_TYPE_LAST,
c480ed
@@ -103,6 +117,11 @@ typedef int
c480ed
 typedef int
c480ed
 (*virCgroupRemoveCB)(virCgroupPtr group);
c480ed
 
c480ed
+typedef int
c480ed
+(*virCgroupAddTaskCB)(virCgroupPtr group,
c480ed
+                      pid_t pid,
c480ed
+                      unsigned int flags);
c480ed
+
c480ed
 struct _virCgroupBackend {
c480ed
     virCgroupBackendType type;
c480ed
 
c480ed
@@ -121,6 +140,7 @@ struct _virCgroupBackend {
c480ed
     virCgroupPathOfControllerCB pathOfController;
c480ed
     virCgroupMakeGroupCB makeGroup;
c480ed
     virCgroupRemoveCB remove;
c480ed
+    virCgroupAddTaskCB addTask;
c480ed
 };
c480ed
 typedef struct _virCgroupBackend virCgroupBackend;
c480ed
 typedef virCgroupBackend *virCgroupBackendPtr;
c480ed
diff --git a/src/util/vircgrouppriv.h b/src/util/vircgrouppriv.h
c480ed
index a760b9bcfd..01714370be 100644
c480ed
--- a/src/util/vircgrouppriv.h
c480ed
+++ b/src/util/vircgrouppriv.h
c480ed
@@ -73,6 +73,11 @@ int virCgroupGetValueU64(virCgroupPtr group,
c480ed
                          const char *key,
c480ed
                          unsigned long long int *value);
c480ed
 
c480ed
+int virCgroupSetValueI64(virCgroupPtr group,
c480ed
+                         int controller,
c480ed
+                         const char *key,
c480ed
+                         long long int value);
c480ed
+
c480ed
 int virCgroupPartitionEscape(char **path);
c480ed
 
c480ed
 int virCgroupNewPartition(const char *path,
c480ed
diff --git a/src/util/vircgroupv1.c b/src/util/vircgroupv1.c
c480ed
index 4d4b51094a..fda2c43eef 100644
c480ed
--- a/src/util/vircgroupv1.c
c480ed
+++ b/src/util/vircgroupv1.c
c480ed
@@ -706,6 +706,36 @@ virCgroupV1Remove(virCgroupPtr group)
c480ed
 }
c480ed
 
c480ed
 
c480ed
+static int
c480ed
+virCgroupV1AddTask(virCgroupPtr group,
c480ed
+                   pid_t pid,
c480ed
+                   unsigned int flags)
c480ed
+{
c480ed
+    int ret = -1;
c480ed
+    size_t i;
c480ed
+
c480ed
+    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
c480ed
+        /* Skip over controllers not mounted */
c480ed
+        if (!group->controllers[i].mountPoint)
c480ed
+            continue;
c480ed
+
c480ed
+        /* We must never add tasks in systemd's hierarchy
c480ed
+         * unless we're intentionally trying to move a
c480ed
+         * task into a systemd machine scope */
c480ed
+        if (i == VIR_CGROUP_CONTROLLER_SYSTEMD &&
c480ed
+            !(flags & VIR_CGROUP_TASK_SYSTEMD))
c480ed
+            continue;
c480ed
+
c480ed
+        if (virCgroupSetValueI64(group, i, "tasks", pid) < 0)
c480ed
+            goto cleanup;
c480ed
+    }
c480ed
+
c480ed
+    ret = 0;
c480ed
+ cleanup:
c480ed
+    return ret;
c480ed
+}
c480ed
+
c480ed
+
c480ed
 virCgroupBackend virCgroupV1Backend = {
c480ed
     .type = VIR_CGROUP_BACKEND_TYPE_V1,
c480ed
 
c480ed
@@ -723,6 +753,7 @@ virCgroupBackend virCgroupV1Backend = {
c480ed
     .pathOfController = virCgroupV1PathOfController,
c480ed
     .makeGroup = virCgroupV1MakeGroup,
c480ed
     .remove = virCgroupV1Remove,
c480ed
+    .addTask = virCgroupV1AddTask,
c480ed
 };
c480ed
 
c480ed
 
c480ed
-- 
c480ed
2.22.0
c480ed