Blob Blame History Raw
From a756729403915485dbd3cbdf4f252806318c227c Mon Sep 17 00:00:00 2001
Message-Id: <a756729403915485dbd3cbdf4f252806318c227c@dist-git>
From: Pavel Hrdina <phrdina@redhat.com>
Date: Mon, 1 Jul 2019 17:06:37 +0200
Subject: [PATCH] vircgroup: extract virCgroupV1AddTask
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Reviewed-by: Fabiano Fidêncio <fidencio@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
(cherry picked from commit 064024e70afd6ad08993444b3839eb3ba67426f2)

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1689297

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Message-Id: <558f1a90ce9b0fc5a9688c5a44bf85dd523c37e5.1561993100.git.phrdina@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
---
 src/util/vircgroup.c        | 56 ++++---------------------------------
 src/util/vircgroupbackend.h | 20 +++++++++++++
 src/util/vircgrouppriv.h    |  5 ++++
 src/util/vircgroupv1.c      | 31 ++++++++++++++++++++
 4 files changed, 62 insertions(+), 50 deletions(-)

diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
index 33d34d6eda..a5076cb554 100644
--- a/src/util/vircgroup.c
+++ b/src/util/vircgroup.c
@@ -544,7 +544,7 @@ virCgroupSetValueU64(virCgroupPtr group,
 }
 
 
-static int
+int
 virCgroupSetValueI64(virCgroupPtr group,
                      int controller,
                      const char *key,
@@ -672,50 +672,6 @@ virCgroupNew(pid_t pid,
 }
 
 
-typedef enum {
-    /* Adds a whole process with all threads to specific cgroup except
-     * to systemd named controller. */
-    VIR_CGROUP_TASK_PROCESS = 1 << 0,
-
-    /* Same as VIR_CGROUP_TASK_PROCESS but it also adds the task to systemd
-     * named controller. */
-    VIR_CGROUP_TASK_SYSTEMD = 1 << 1,
-
-    /* Moves only specific thread into cgroup except to systemd
-     * named controller. */
-    VIR_CGROUP_TASK_THREAD = 1 << 2,
-} virCgroupTaskFlags;
-
-
-static int
-virCgroupAddTaskInternal(virCgroupPtr group,
-                         pid_t pid,
-                         unsigned int flags)
-{
-    int ret = -1;
-    size_t i;
-
-    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
-        /* Skip over controllers not mounted */
-        if (!group->controllers[i].mountPoint)
-            continue;
-
-        /* We must never add tasks in systemd's hierarchy
-         * unless we're intentionally trying to move a
-         * task into a systemd machine scope */
-        if (i == VIR_CGROUP_CONTROLLER_SYSTEMD &&
-            !(flags & VIR_CGROUP_TASK_SYSTEMD))
-            continue;
-
-        if (virCgroupSetValueI64(group, i, "tasks", pid) < 0)
-            goto cleanup;
-    }
-
-    ret = 0;
- cleanup:
-    return ret;
-}
-
 /**
  * virCgroupAddProcess:
  *
@@ -730,7 +686,7 @@ virCgroupAddTaskInternal(virCgroupPtr group,
 int
 virCgroupAddProcess(virCgroupPtr group, pid_t pid)
 {
-    return virCgroupAddTaskInternal(group, pid, VIR_CGROUP_TASK_PROCESS);
+    return group->backend->addTask(group, pid, VIR_CGROUP_TASK_PROCESS);
 }
 
 /**
@@ -747,9 +703,9 @@ virCgroupAddProcess(virCgroupPtr group, pid_t pid)
 int
 virCgroupAddMachineProcess(virCgroupPtr group, pid_t pid)
 {
-    return virCgroupAddTaskInternal(group, pid,
-                                    VIR_CGROUP_TASK_PROCESS |
-                                    VIR_CGROUP_TASK_SYSTEMD);
+    return group->backend->addTask(group, pid,
+                                   VIR_CGROUP_TASK_PROCESS |
+                                   VIR_CGROUP_TASK_SYSTEMD);
 }
 
 /**
@@ -767,7 +723,7 @@ int
 virCgroupAddThread(virCgroupPtr group,
                    pid_t pid)
 {
-    return virCgroupAddTaskInternal(group, pid, VIR_CGROUP_TASK_THREAD);
+    return group->backend->addTask(group, pid, VIR_CGROUP_TASK_THREAD);
 }
 
 
diff --git a/src/util/vircgroupbackend.h b/src/util/vircgroupbackend.h
index 1f28c51c49..bff1f8938e 100644
--- a/src/util/vircgroupbackend.h
+++ b/src/util/vircgroupbackend.h
@@ -35,6 +35,20 @@ typedef enum {
                                        */
 } virCgroupBackendFlags;
 
+typedef enum {
+    /* Adds a whole process with all threads to specific cgroup except
+     * to systemd named controller. */
+    VIR_CGROUP_TASK_PROCESS = 1 << 0,
+
+    /* Same as VIR_CGROUP_TASK_PROCESS but it also adds the task to systemd
+     * named controller. */
+    VIR_CGROUP_TASK_SYSTEMD = 1 << 1,
+
+    /* Moves only specific thread into cgroup except to systemd
+     * named controller. */
+    VIR_CGROUP_TASK_THREAD = 1 << 2,
+} virCgroupBackendTaskFlags;
+
 typedef enum {
     VIR_CGROUP_BACKEND_TYPE_V1 = 0,
     VIR_CGROUP_BACKEND_TYPE_LAST,
@@ -103,6 +117,11 @@ typedef int
 typedef int
 (*virCgroupRemoveCB)(virCgroupPtr group);
 
+typedef int
+(*virCgroupAddTaskCB)(virCgroupPtr group,
+                      pid_t pid,
+                      unsigned int flags);
+
 struct _virCgroupBackend {
     virCgroupBackendType type;
 
@@ -121,6 +140,7 @@ struct _virCgroupBackend {
     virCgroupPathOfControllerCB pathOfController;
     virCgroupMakeGroupCB makeGroup;
     virCgroupRemoveCB remove;
+    virCgroupAddTaskCB addTask;
 };
 typedef struct _virCgroupBackend virCgroupBackend;
 typedef virCgroupBackend *virCgroupBackendPtr;
diff --git a/src/util/vircgrouppriv.h b/src/util/vircgrouppriv.h
index a760b9bcfd..01714370be 100644
--- a/src/util/vircgrouppriv.h
+++ b/src/util/vircgrouppriv.h
@@ -73,6 +73,11 @@ int virCgroupGetValueU64(virCgroupPtr group,
                          const char *key,
                          unsigned long long int *value);
 
+int virCgroupSetValueI64(virCgroupPtr group,
+                         int controller,
+                         const char *key,
+                         long long int value);
+
 int virCgroupPartitionEscape(char **path);
 
 int virCgroupNewPartition(const char *path,
diff --git a/src/util/vircgroupv1.c b/src/util/vircgroupv1.c
index 4d4b51094a..fda2c43eef 100644
--- a/src/util/vircgroupv1.c
+++ b/src/util/vircgroupv1.c
@@ -706,6 +706,36 @@ virCgroupV1Remove(virCgroupPtr group)
 }
 
 
+static int
+virCgroupV1AddTask(virCgroupPtr group,
+                   pid_t pid,
+                   unsigned int flags)
+{
+    int ret = -1;
+    size_t i;
+
+    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
+        /* Skip over controllers not mounted */
+        if (!group->controllers[i].mountPoint)
+            continue;
+
+        /* We must never add tasks in systemd's hierarchy
+         * unless we're intentionally trying to move a
+         * task into a systemd machine scope */
+        if (i == VIR_CGROUP_CONTROLLER_SYSTEMD &&
+            !(flags & VIR_CGROUP_TASK_SYSTEMD))
+            continue;
+
+        if (virCgroupSetValueI64(group, i, "tasks", pid) < 0)
+            goto cleanup;
+    }
+
+    ret = 0;
+ cleanup:
+    return ret;
+}
+
+
 virCgroupBackend virCgroupV1Backend = {
     .type = VIR_CGROUP_BACKEND_TYPE_V1,
 
@@ -723,6 +753,7 @@ virCgroupBackend virCgroupV1Backend = {
     .pathOfController = virCgroupV1PathOfController,
     .makeGroup = virCgroupV1MakeGroup,
     .remove = virCgroupV1Remove,
+    .addTask = virCgroupV1AddTask,
 };
 
 
-- 
2.22.0