c313de
From e950a43b63a79cf68e835d800688d759ba408178 Mon Sep 17 00:00:00 2001
c313de
Message-Id: <e950a43b63a79cf68e835d800688d759ba408178@dist-git>
c313de
From: Pavel Hrdina <phrdina@redhat.com>
c313de
Date: Mon, 1 Jul 2019 17:06:25 +0200
c313de
Subject: [PATCH] vircgroup: extract virCgroupV1ValidateMachineGroup
c313de
MIME-Version: 1.0
c313de
Content-Type: text/plain; charset=UTF-8
c313de
Content-Transfer-Encoding: 8bit
c313de
c313de
Reviewed-by: Fabiano Fidêncio <fidencio@redhat.com>
c313de
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c313de
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c313de
(cherry picked from commit 61629d5be3a4809fe05feb15adbc8f2c27916702)
c313de
c313de
Conflicts:
c313de
    src/util/vircgroupv1.c: missing commit c0abcca417
c313de
c313de
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1689297
c313de
c313de
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c313de
Message-Id: <a34a0141721e9e27c0b3337108c13d36caa6bb79.1561993100.git.phrdina@redhat.com>
c313de
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c313de
---
c313de
 src/util/vircgroup.c        | 85 +------------------------------------
c313de
 src/util/vircgroupbackend.h |  7 +++
c313de
 src/util/vircgrouppriv.h    |  2 +
c313de
 src/util/vircgroupv1.c      | 85 +++++++++++++++++++++++++++++++++++++
c313de
 4 files changed, 96 insertions(+), 83 deletions(-)
c313de
c313de
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
c313de
index 15d0cb65ac..6825623478 100644
c313de
--- a/src/util/vircgroup.c
c313de
+++ b/src/util/vircgroup.c
c313de
@@ -216,7 +216,7 @@ virCgroupPartitionNeedsEscaping(const char *path)
c313de
 }
c313de
 
c313de
 
c313de
-static int
c313de
+int
c313de
 virCgroupPartitionEscape(char **path)
c313de
 {
c313de
     int rc;
c313de
@@ -235,87 +235,6 @@ virCgroupPartitionEscape(char **path)
c313de
 }
c313de
 
c313de
 
c313de
-static bool
c313de
-virCgroupValidateMachineGroup(virCgroupPtr group,
c313de
-                              const char *name,
c313de
-                              const char *drivername,
c313de
-                              const char *machinename)
c313de
-{
c313de
-    size_t i;
c313de
-    VIR_AUTOFREE(char *) partname = NULL;
c313de
-    VIR_AUTOFREE(char *) scopename_old = NULL;
c313de
-    VIR_AUTOFREE(char *) scopename_new = NULL;
c313de
-    VIR_AUTOFREE(char *) partmachinename = NULL;
c313de
-
c313de
-    if (virAsprintf(&partname, "%s.libvirt-%s",
c313de
-                    name, drivername) < 0)
c313de
-        return false;
c313de
-
c313de
-    if (virCgroupPartitionEscape(&partname) < 0)
c313de
-        return false;
c313de
-
c313de
-    if (virAsprintf(&partmachinename, "%s.libvirt-%s",
c313de
-                    machinename, drivername) < 0 ||
c313de
-        virCgroupPartitionEscape(&partmachinename) < 0)
c313de
-        return false;
c313de
-
c313de
-    if (!(scopename_old = virSystemdMakeScopeName(name, drivername, true)))
c313de
-        return false;
c313de
-
c313de
-    if (!(scopename_new = virSystemdMakeScopeName(machinename,
c313de
-                                                  drivername, false)))
c313de
-        return false;
c313de
-
c313de
-    if (virCgroupPartitionEscape(&scopename_old) < 0)
c313de
-        return false;
c313de
-
c313de
-    if (virCgroupPartitionEscape(&scopename_new) < 0)
c313de
-        return false;
c313de
-
c313de
-    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
c313de
-        char *tmp;
c313de
-
c313de
-        if (i == VIR_CGROUP_CONTROLLER_SYSTEMD)
c313de
-            continue;
c313de
-
c313de
-        if (!group->controllers[i].placement)
c313de
-            continue;
c313de
-
c313de
-        tmp = strrchr(group->controllers[i].placement, '/');
c313de
-        if (!tmp)
c313de
-            return false;
c313de
-
c313de
-        if (i == VIR_CGROUP_CONTROLLER_CPU ||
c313de
-            i == VIR_CGROUP_CONTROLLER_CPUACCT ||
c313de
-            i == VIR_CGROUP_CONTROLLER_CPUSET) {
c313de
-            if (STREQ(tmp, "/emulator"))
c313de
-                *tmp = '\0';
c313de
-            tmp = strrchr(group->controllers[i].placement, '/');
c313de
-            if (!tmp)
c313de
-                return false;
c313de
-        }
c313de
-
c313de
-        tmp++;
c313de
-
c313de
-        if (STRNEQ(tmp, name) &&
c313de
-            STRNEQ(tmp, machinename) &&
c313de
-            STRNEQ(tmp, partname) &&
c313de
-            STRNEQ(tmp, partmachinename) &&
c313de
-            STRNEQ(tmp, scopename_old) &&
c313de
-            STRNEQ(tmp, scopename_new)) {
c313de
-            VIR_DEBUG("Name '%s' for controller '%s' does not match "
c313de
-                      "'%s', '%s', '%s', '%s' or '%s'",
c313de
-                      tmp, virCgroupControllerTypeToString(i),
c313de
-                      name, machinename, partname,
c313de
-                      scopename_old, scopename_new);
c313de
-            return false;
c313de
-        }
c313de
-    }
c313de
-
c313de
-    return true;
c313de
-}
c313de
-
c313de
-
c313de
 static int
c313de
 virCgroupCopyMounts(virCgroupPtr group,
c313de
                     virCgroupPtr parent)
c313de
@@ -1508,7 +1427,7 @@ virCgroupNewDetectMachine(const char *name,
c313de
         return -1;
c313de
     }
c313de
 
c313de
-    if (!virCgroupValidateMachineGroup(*group, name, drivername, machinename)) {
c313de
+    if (!(*group)->backend->validateMachineGroup(*group, name, drivername, machinename)) {
c313de
         VIR_DEBUG("Failed to validate machine name for '%s' driver '%s'",
c313de
                   name, drivername);
c313de
         virCgroupFree(group);
c313de
diff --git a/src/util/vircgroupbackend.h b/src/util/vircgroupbackend.h
c313de
index 88f51416b0..daf47bac09 100644
c313de
--- a/src/util/vircgroupbackend.h
c313de
+++ b/src/util/vircgroupbackend.h
c313de
@@ -35,11 +35,18 @@ typedef enum {
c313de
 typedef bool
c313de
 (*virCgroupAvailableCB)(void);
c313de
 
c313de
+typedef bool
c313de
+(*virCgroupValidateMachineGroupCB)(virCgroupPtr group,
c313de
+                                   const char *name,
c313de
+                                   const char *drivername,
c313de
+                                   const char *machinename);
c313de
+
c313de
 struct _virCgroupBackend {
c313de
     virCgroupBackendType type;
c313de
 
c313de
     /* Mandatory callbacks that need to be implemented for every backend. */
c313de
     virCgroupAvailableCB available;
c313de
+    virCgroupValidateMachineGroupCB validateMachineGroup;
c313de
 };
c313de
 typedef struct _virCgroupBackend virCgroupBackend;
c313de
 typedef virCgroupBackend *virCgroupBackendPtr;
c313de
diff --git a/src/util/vircgrouppriv.h b/src/util/vircgrouppriv.h
c313de
index 2caa966fee..e7f4a1f0fc 100644
c313de
--- a/src/util/vircgrouppriv.h
c313de
+++ b/src/util/vircgrouppriv.h
c313de
@@ -53,6 +53,8 @@ struct _virCgroup {
c313de
     virCgroupController controllers[VIR_CGROUP_CONTROLLER_LAST];
c313de
 };
c313de
 
c313de
+int virCgroupPartitionEscape(char **path);
c313de
+
c313de
 int virCgroupNewPartition(const char *path,
c313de
                           bool create,
c313de
                           int controllers,
c313de
diff --git a/src/util/vircgroupv1.c b/src/util/vircgroupv1.c
c313de
index 73045b1109..b78cdcab53 100644
c313de
--- a/src/util/vircgroupv1.c
c313de
+++ b/src/util/vircgroupv1.c
c313de
@@ -30,11 +30,14 @@
c313de
 #include "vircgrouppriv.h"
c313de
 #undef __VIR_CGROUP_ALLOW_INCLUDE_PRIV_H__
c313de
 
c313de
+#include "viralloc.h"
c313de
 #include "vircgroup.h"
c313de
 #include "vircgroupbackend.h"
c313de
 #include "vircgroupv1.h"
c313de
 #include "virfile.h"
c313de
 #include "virlog.h"
c313de
+#include "virstring.h"
c313de
+#include "virsystemd.h"
c313de
 
c313de
 VIR_LOG_INIT("util.cgroup");
c313de
 
c313de
@@ -76,10 +79,92 @@ virCgroupV1Available(void)
c313de
 }
c313de
 
c313de
 
c313de
+static bool
c313de
+virCgroupV1ValidateMachineGroup(virCgroupPtr group,
c313de
+                                const char *name,
c313de
+                                const char *drivername,
c313de
+                                const char *machinename)
c313de
+{
c313de
+    size_t i;
c313de
+    VIR_AUTOFREE(char *) partname = NULL;
c313de
+    VIR_AUTOFREE(char *) scopename_old = NULL;
c313de
+    VIR_AUTOFREE(char *) scopename_new = NULL;
c313de
+    VIR_AUTOFREE(char *) partmachinename = NULL;
c313de
+
c313de
+    if (virAsprintf(&partname, "%s.libvirt-%s",
c313de
+                    name, drivername) < 0)
c313de
+        return false;
c313de
+
c313de
+    if (virCgroupPartitionEscape(&partname) < 0)
c313de
+        return false;
c313de
+
c313de
+    if (virAsprintf(&partmachinename, "%s.libvirt-%s",
c313de
+                    machinename, drivername) < 0 ||
c313de
+        virCgroupPartitionEscape(&partmachinename) < 0)
c313de
+        return false;
c313de
+
c313de
+    if (!(scopename_old = virSystemdMakeScopeName(name, drivername, true)))
c313de
+        return false;
c313de
+
c313de
+    if (!(scopename_new = virSystemdMakeScopeName(machinename,
c313de
+                                                  drivername, false)))
c313de
+        return false;
c313de
+
c313de
+    if (virCgroupPartitionEscape(&scopename_old) < 0)
c313de
+        return false;
c313de
+
c313de
+    if (virCgroupPartitionEscape(&scopename_new) < 0)
c313de
+        return false;
c313de
+
c313de
+    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
c313de
+        char *tmp;
c313de
+
c313de
+        if (i == VIR_CGROUP_CONTROLLER_SYSTEMD)
c313de
+            continue;
c313de
+
c313de
+        if (!group->controllers[i].placement)
c313de
+            continue;
c313de
+
c313de
+        tmp = strrchr(group->controllers[i].placement, '/');
c313de
+        if (!tmp)
c313de
+            return false;
c313de
+
c313de
+        if (i == VIR_CGROUP_CONTROLLER_CPU ||
c313de
+            i == VIR_CGROUP_CONTROLLER_CPUACCT ||
c313de
+            i == VIR_CGROUP_CONTROLLER_CPUSET) {
c313de
+            if (STREQ(tmp, "/emulator"))
c313de
+                *tmp = '\0';
c313de
+            tmp = strrchr(group->controllers[i].placement, '/');
c313de
+            if (!tmp)
c313de
+                return false;
c313de
+        }
c313de
+
c313de
+        tmp++;
c313de
+
c313de
+        if (STRNEQ(tmp, name) &&
c313de
+            STRNEQ(tmp, machinename) &&
c313de
+            STRNEQ(tmp, partname) &&
c313de
+            STRNEQ(tmp, partmachinename) &&
c313de
+            STRNEQ(tmp, scopename_old) &&
c313de
+            STRNEQ(tmp, scopename_new)) {
c313de
+            VIR_DEBUG("Name '%s' for controller '%s' does not match "
c313de
+                      "'%s', '%s', '%s', '%s' or '%s'",
c313de
+                      tmp, virCgroupV1ControllerTypeToString(i),
c313de
+                      name, machinename, partname,
c313de
+                      scopename_old, scopename_new);
c313de
+            return false;
c313de
+        }
c313de
+    }
c313de
+
c313de
+    return true;
c313de
+}
c313de
+
c313de
+
c313de
 virCgroupBackend virCgroupV1Backend = {
c313de
     .type = VIR_CGROUP_BACKEND_TYPE_V1,
c313de
 
c313de
     .available = virCgroupV1Available,
c313de
+    .validateMachineGroup = virCgroupV1ValidateMachineGroup,
c313de
 };
c313de
 
c313de
 
c313de
-- 
c313de
2.22.0
c313de