render / rpms / libvirt

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