Blob Blame History Raw
From e950a43b63a79cf68e835d800688d759ba408178 Mon Sep 17 00:00:00 2001
Message-Id: <e950a43b63a79cf68e835d800688d759ba408178@dist-git>
From: Pavel Hrdina <phrdina@redhat.com>
Date: Mon, 1 Jul 2019 17:06:25 +0200
Subject: [PATCH] vircgroup: extract virCgroupV1ValidateMachineGroup
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 61629d5be3a4809fe05feb15adbc8f2c27916702)

Conflicts:
    src/util/vircgroupv1.c: missing commit c0abcca417

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

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Message-Id: <a34a0141721e9e27c0b3337108c13d36caa6bb79.1561993100.git.phrdina@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
---
 src/util/vircgroup.c        | 85 +------------------------------------
 src/util/vircgroupbackend.h |  7 +++
 src/util/vircgrouppriv.h    |  2 +
 src/util/vircgroupv1.c      | 85 +++++++++++++++++++++++++++++++++++++
 4 files changed, 96 insertions(+), 83 deletions(-)

diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
index 15d0cb65ac..6825623478 100644
--- a/src/util/vircgroup.c
+++ b/src/util/vircgroup.c
@@ -216,7 +216,7 @@ virCgroupPartitionNeedsEscaping(const char *path)
 }
 
 
-static int
+int
 virCgroupPartitionEscape(char **path)
 {
     int rc;
@@ -235,87 +235,6 @@ virCgroupPartitionEscape(char **path)
 }
 
 
-static bool
-virCgroupValidateMachineGroup(virCgroupPtr group,
-                              const char *name,
-                              const char *drivername,
-                              const char *machinename)
-{
-    size_t i;
-    VIR_AUTOFREE(char *) partname = NULL;
-    VIR_AUTOFREE(char *) scopename_old = NULL;
-    VIR_AUTOFREE(char *) scopename_new = NULL;
-    VIR_AUTOFREE(char *) partmachinename = NULL;
-
-    if (virAsprintf(&partname, "%s.libvirt-%s",
-                    name, drivername) < 0)
-        return false;
-
-    if (virCgroupPartitionEscape(&partname) < 0)
-        return false;
-
-    if (virAsprintf(&partmachinename, "%s.libvirt-%s",
-                    machinename, drivername) < 0 ||
-        virCgroupPartitionEscape(&partmachinename) < 0)
-        return false;
-
-    if (!(scopename_old = virSystemdMakeScopeName(name, drivername, true)))
-        return false;
-
-    if (!(scopename_new = virSystemdMakeScopeName(machinename,
-                                                  drivername, false)))
-        return false;
-
-    if (virCgroupPartitionEscape(&scopename_old) < 0)
-        return false;
-
-    if (virCgroupPartitionEscape(&scopename_new) < 0)
-        return false;
-
-    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
-        char *tmp;
-
-        if (i == VIR_CGROUP_CONTROLLER_SYSTEMD)
-            continue;
-
-        if (!group->controllers[i].placement)
-            continue;
-
-        tmp = strrchr(group->controllers[i].placement, '/');
-        if (!tmp)
-            return false;
-
-        if (i == VIR_CGROUP_CONTROLLER_CPU ||
-            i == VIR_CGROUP_CONTROLLER_CPUACCT ||
-            i == VIR_CGROUP_CONTROLLER_CPUSET) {
-            if (STREQ(tmp, "/emulator"))
-                *tmp = '\0';
-            tmp = strrchr(group->controllers[i].placement, '/');
-            if (!tmp)
-                return false;
-        }
-
-        tmp++;
-
-        if (STRNEQ(tmp, name) &&
-            STRNEQ(tmp, machinename) &&
-            STRNEQ(tmp, partname) &&
-            STRNEQ(tmp, partmachinename) &&
-            STRNEQ(tmp, scopename_old) &&
-            STRNEQ(tmp, scopename_new)) {
-            VIR_DEBUG("Name '%s' for controller '%s' does not match "
-                      "'%s', '%s', '%s', '%s' or '%s'",
-                      tmp, virCgroupControllerTypeToString(i),
-                      name, machinename, partname,
-                      scopename_old, scopename_new);
-            return false;
-        }
-    }
-
-    return true;
-}
-
-
 static int
 virCgroupCopyMounts(virCgroupPtr group,
                     virCgroupPtr parent)
@@ -1508,7 +1427,7 @@ virCgroupNewDetectMachine(const char *name,
         return -1;
     }
 
-    if (!virCgroupValidateMachineGroup(*group, name, drivername, machinename)) {
+    if (!(*group)->backend->validateMachineGroup(*group, name, drivername, machinename)) {
         VIR_DEBUG("Failed to validate machine name for '%s' driver '%s'",
                   name, drivername);
         virCgroupFree(group);
diff --git a/src/util/vircgroupbackend.h b/src/util/vircgroupbackend.h
index 88f51416b0..daf47bac09 100644
--- a/src/util/vircgroupbackend.h
+++ b/src/util/vircgroupbackend.h
@@ -35,11 +35,18 @@ typedef enum {
 typedef bool
 (*virCgroupAvailableCB)(void);
 
+typedef bool
+(*virCgroupValidateMachineGroupCB)(virCgroupPtr group,
+                                   const char *name,
+                                   const char *drivername,
+                                   const char *machinename);
+
 struct _virCgroupBackend {
     virCgroupBackendType type;
 
     /* Mandatory callbacks that need to be implemented for every backend. */
     virCgroupAvailableCB available;
+    virCgroupValidateMachineGroupCB validateMachineGroup;
 };
 typedef struct _virCgroupBackend virCgroupBackend;
 typedef virCgroupBackend *virCgroupBackendPtr;
diff --git a/src/util/vircgrouppriv.h b/src/util/vircgrouppriv.h
index 2caa966fee..e7f4a1f0fc 100644
--- a/src/util/vircgrouppriv.h
+++ b/src/util/vircgrouppriv.h
@@ -53,6 +53,8 @@ struct _virCgroup {
     virCgroupController controllers[VIR_CGROUP_CONTROLLER_LAST];
 };
 
+int virCgroupPartitionEscape(char **path);
+
 int virCgroupNewPartition(const char *path,
                           bool create,
                           int controllers,
diff --git a/src/util/vircgroupv1.c b/src/util/vircgroupv1.c
index 73045b1109..b78cdcab53 100644
--- a/src/util/vircgroupv1.c
+++ b/src/util/vircgroupv1.c
@@ -30,11 +30,14 @@
 #include "vircgrouppriv.h"
 #undef __VIR_CGROUP_ALLOW_INCLUDE_PRIV_H__
 
+#include "viralloc.h"
 #include "vircgroup.h"
 #include "vircgroupbackend.h"
 #include "vircgroupv1.h"
 #include "virfile.h"
 #include "virlog.h"
+#include "virstring.h"
+#include "virsystemd.h"
 
 VIR_LOG_INIT("util.cgroup");
 
@@ -76,10 +79,92 @@ virCgroupV1Available(void)
 }
 
 
+static bool
+virCgroupV1ValidateMachineGroup(virCgroupPtr group,
+                                const char *name,
+                                const char *drivername,
+                                const char *machinename)
+{
+    size_t i;
+    VIR_AUTOFREE(char *) partname = NULL;
+    VIR_AUTOFREE(char *) scopename_old = NULL;
+    VIR_AUTOFREE(char *) scopename_new = NULL;
+    VIR_AUTOFREE(char *) partmachinename = NULL;
+
+    if (virAsprintf(&partname, "%s.libvirt-%s",
+                    name, drivername) < 0)
+        return false;
+
+    if (virCgroupPartitionEscape(&partname) < 0)
+        return false;
+
+    if (virAsprintf(&partmachinename, "%s.libvirt-%s",
+                    machinename, drivername) < 0 ||
+        virCgroupPartitionEscape(&partmachinename) < 0)
+        return false;
+
+    if (!(scopename_old = virSystemdMakeScopeName(name, drivername, true)))
+        return false;
+
+    if (!(scopename_new = virSystemdMakeScopeName(machinename,
+                                                  drivername, false)))
+        return false;
+
+    if (virCgroupPartitionEscape(&scopename_old) < 0)
+        return false;
+
+    if (virCgroupPartitionEscape(&scopename_new) < 0)
+        return false;
+
+    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
+        char *tmp;
+
+        if (i == VIR_CGROUP_CONTROLLER_SYSTEMD)
+            continue;
+
+        if (!group->controllers[i].placement)
+            continue;
+
+        tmp = strrchr(group->controllers[i].placement, '/');
+        if (!tmp)
+            return false;
+
+        if (i == VIR_CGROUP_CONTROLLER_CPU ||
+            i == VIR_CGROUP_CONTROLLER_CPUACCT ||
+            i == VIR_CGROUP_CONTROLLER_CPUSET) {
+            if (STREQ(tmp, "/emulator"))
+                *tmp = '\0';
+            tmp = strrchr(group->controllers[i].placement, '/');
+            if (!tmp)
+                return false;
+        }
+
+        tmp++;
+
+        if (STRNEQ(tmp, name) &&
+            STRNEQ(tmp, machinename) &&
+            STRNEQ(tmp, partname) &&
+            STRNEQ(tmp, partmachinename) &&
+            STRNEQ(tmp, scopename_old) &&
+            STRNEQ(tmp, scopename_new)) {
+            VIR_DEBUG("Name '%s' for controller '%s' does not match "
+                      "'%s', '%s', '%s', '%s' or '%s'",
+                      tmp, virCgroupV1ControllerTypeToString(i),
+                      name, machinename, partname,
+                      scopename_old, scopename_new);
+            return false;
+        }
+    }
+
+    return true;
+}
+
+
 virCgroupBackend virCgroupV1Backend = {
     .type = VIR_CGROUP_BACKEND_TYPE_V1,
 
     .available = virCgroupV1Available,
+    .validateMachineGroup = virCgroupV1ValidateMachineGroup,
 };
 
 
-- 
2.22.0