render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
Blob Blame History Raw
From 51e0205bf57346f381143b498cfe0a77852a8da1 Mon Sep 17 00:00:00 2001
Message-Id: <51e0205bf57346f381143b498cfe0a77852a8da1@dist-git>
From: Pavel Hrdina <phrdina@redhat.com>
Date: Mon, 1 Jul 2019 17:06:07 +0200
Subject: [PATCH] vircgroup: Split virCgroupPathOfController into two functions
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The case where we need path of any controller is only for internal use
so move it out to a different function.

Reviewed-by: Ján Tomko <jtomko@redhat.com>
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
(cherry picked from commit 998658bd1ebdc5fd9ec43131c381cece1a3893cf)

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

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Message-Id: <7578336183563ecf49d686f6705724db25037191.1561993099.git.phrdina@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
---
 src/util/vircgroup.c | 54 ++++++++++++++++++++++++++------------------
 src/util/vircgroup.h |  2 +-
 2 files changed, 33 insertions(+), 23 deletions(-)

diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
index 2bc4febf23..8646a4a479 100644
--- a/src/util/vircgroup.c
+++ b/src/util/vircgroup.c
@@ -1772,28 +1772,13 @@ virCgroupHasController(virCgroupPtr cgroup, int controller)
 
 int
 virCgroupPathOfController(virCgroupPtr group,
-                          int controller,
+                          unsigned int controller,
                           const char *key,
                           char **path)
 {
-    if (controller == -1) {
-        size_t i;
-        for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
-            /* Reject any controller with a placement
-             * of '/' to avoid doing bad stuff to the root
-             * cgroup
-             */
-            if (group->controllers[i].mountPoint &&
-                group->controllers[i].placement &&
-                STRNEQ(group->controllers[i].placement, "/")) {
-                controller = i;
-                break;
-            }
-        }
-    }
-    if (controller == -1) {
-        virReportSystemError(ENOSYS, "%s",
-                             _("No controllers are mounted"));
+    if (controller >= VIR_CGROUP_CONTROLLER_LAST) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Invalid controller id '%d'"), controller);
         return -1;
     }
 
@@ -3505,6 +3490,31 @@ virCgroupRemove(virCgroupPtr group)
 }
 
 
+static int
+virCgroupPathOfAnyController(virCgroupPtr group,
+                             const char *name,
+                             char **keypath)
+{
+    size_t i;
+
+    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
+        /* Reject any controller with a placement
+         * of '/' to avoid doing bad stuff to the root
+         * cgroup
+         */
+        if (group->controllers[i].mountPoint &&
+            group->controllers[i].placement &&
+            STRNEQ(group->controllers[i].placement, "/")) {
+            return virCgroupPathOfController(group, i, name, keypath);
+        }
+    }
+
+    virReportSystemError(ENOSYS, "%s",
+                         _("No controllers are mounted"));
+    return -1;
+}
+
+
 /*
  * Returns 1 if some PIDs are killed, 0 if none are killed, or -1 on error
  */
@@ -3519,7 +3529,7 @@ virCgroupKillInternal(virCgroupPtr group, int signum, virHashTablePtr pids)
     VIR_DEBUG("group=%p path=%s signum=%d pids=%p",
               group, group->path, signum, pids);
 
-    if (virCgroupPathOfController(group, -1, "tasks", &keypath) < 0)
+    if (virCgroupPathOfAnyController(group, "tasks", &keypath) < 0)
         return -1;
 
     /* PIDs may be forking as we kill them, so loop
@@ -3622,7 +3632,7 @@ virCgroupKillRecursiveInternal(virCgroupPtr group,
     VIR_DEBUG("group=%p path=%s signum=%d pids=%p",
               group, group->path, signum, pids);
 
-    if (virCgroupPathOfController(group, -1, "", &keypath) < 0)
+    if (virCgroupPathOfAnyController(group, "", &keypath) < 0)
         return -1;
 
     if ((rc = virCgroupKillInternal(group, signum, pids)) < 0)
@@ -4180,7 +4190,7 @@ virCgroupHasController(virCgroupPtr cgroup ATTRIBUTE_UNUSED,
 
 int
 virCgroupPathOfController(virCgroupPtr group ATTRIBUTE_UNUSED,
-                          int controller ATTRIBUTE_UNUSED,
+                          unsigned int controller ATTRIBUTE_UNUSED,
                           const char *key ATTRIBUTE_UNUSED,
                           char **path ATTRIBUTE_UNUSED)
 {
diff --git a/src/util/vircgroup.h b/src/util/vircgroup.h
index c7fdaaede4..ee3b7c7222 100644
--- a/src/util/vircgroup.h
+++ b/src/util/vircgroup.h
@@ -114,7 +114,7 @@ void virCgroupFree(virCgroupPtr *group);
 
 bool virCgroupHasController(virCgroupPtr cgroup, int controller);
 int virCgroupPathOfController(virCgroupPtr group,
-                              int controller,
+                              unsigned int controller,
                               const char *key,
                               char **path);
 
-- 
2.22.0