render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
Pablo Greco 40546a
From 43695c7877a99d12bab96ec17c7d7de250ffc39a Mon Sep 17 00:00:00 2001
Pablo Greco 40546a
Message-Id: <43695c7877a99d12bab96ec17c7d7de250ffc39a@dist-git>
Pablo Greco 40546a
From: Pavel Hrdina <phrdina@redhat.com>
Pablo Greco 40546a
Date: Mon, 1 Jul 2019 17:06:54 +0200
Pablo Greco 40546a
Subject: [PATCH] vircgroup: extract virCgroupV1(Allow|Deny)Device
Pablo Greco 40546a
MIME-Version: 1.0
Pablo Greco 40546a
Content-Type: text/plain; charset=UTF-8
Pablo Greco 40546a
Content-Transfer-Encoding: 8bit
Pablo Greco 40546a
Pablo Greco 40546a
Reviewed-by: Fabiano Fidêncio <fidencio@redhat.com>
Pablo Greco 40546a
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Pablo Greco 40546a
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Pablo Greco 40546a
(cherry picked from commit 8cbb0c76ba24878229830c8d53b365cf4dc1b54d)
Pablo Greco 40546a
Pablo Greco 40546a
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1689297
Pablo Greco 40546a
Pablo Greco 40546a
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Pablo Greco 40546a
Message-Id: <9a4073085dbeb674b24544aa253960bb2b1b53dc.1561993100.git.phrdina@redhat.com>
Pablo Greco 40546a
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Pablo Greco 40546a
---
Pablo Greco 40546a
 src/util/vircgroup.c        | 68 +++++++-----------------------------
Pablo Greco 40546a
 src/util/vircgroupbackend.h | 17 +++++++++
Pablo Greco 40546a
 src/util/vircgroupv1.c      | 69 +++++++++++++++++++++++++++++++++++++
Pablo Greco 40546a
 3 files changed, 98 insertions(+), 56 deletions(-)
Pablo Greco 40546a
Pablo Greco 40546a
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
Pablo Greco 40546a
index 088e97cb3f..a30fc6241d 100644
Pablo Greco 40546a
--- a/src/util/vircgroup.c
Pablo Greco 40546a
+++ b/src/util/vircgroup.c
Pablo Greco 40546a
@@ -1875,29 +1875,7 @@ int
Pablo Greco 40546a
 virCgroupAllowDevice(virCgroupPtr group, char type, int major, int minor,
Pablo Greco 40546a
                      int perms)
Pablo Greco 40546a
 {
Pablo Greco 40546a
-    VIR_AUTOFREE(char *) devstr = NULL;
Pablo Greco 40546a
-    VIR_AUTOFREE(char *) majorstr = NULL;
Pablo Greco 40546a
-    VIR_AUTOFREE(char *) minorstr = NULL;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    if ((major < 0 && VIR_STRDUP(majorstr, "*") < 0) ||
Pablo Greco 40546a
-        (major >= 0 && virAsprintf(&majorstr, "%i", major) < 0))
Pablo Greco 40546a
-        return -1;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    if ((minor < 0 && VIR_STRDUP(minorstr, "*") < 0) ||
Pablo Greco 40546a
-        (minor >= 0 && virAsprintf(&minorstr, "%i", minor) < 0))
Pablo Greco 40546a
-        return -1;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    if (virAsprintf(&devstr, "%c %s:%s %s", type, majorstr, minorstr,
Pablo Greco 40546a
-                    virCgroupGetDevicePermsString(perms)) < 0)
Pablo Greco 40546a
-        return -1;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    if (virCgroupSetValueStr(group,
Pablo Greco 40546a
-                             VIR_CGROUP_CONTROLLER_DEVICES,
Pablo Greco 40546a
-                             "devices.allow",
Pablo Greco 40546a
-                             devstr) < 0)
Pablo Greco 40546a
-        return -1;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    return 0;
Pablo Greco 40546a
+    VIR_CGROUP_BACKEND_CALL(group, allowDevice, -1, type, major, minor, perms);
Pablo Greco 40546a
 }
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
@@ -1936,11 +1914,11 @@ virCgroupAllowDevicePath(virCgroupPtr group,
Pablo Greco 40546a
     if (!S_ISCHR(sb.st_mode) && !S_ISBLK(sb.st_mode))
Pablo Greco 40546a
         return 1;
Pablo Greco 40546a
 
Pablo Greco 40546a
-    return virCgroupAllowDevice(group,
Pablo Greco 40546a
-                                S_ISCHR(sb.st_mode) ? 'c' : 'b',
Pablo Greco 40546a
-                                major(sb.st_rdev),
Pablo Greco 40546a
-                                minor(sb.st_rdev),
Pablo Greco 40546a
-                                perms);
Pablo Greco 40546a
+    VIR_CGROUP_BACKEND_CALL(group, allowDevice, -1,
Pablo Greco 40546a
+                            S_ISCHR(sb.st_mode) ? 'c' : 'b',
Pablo Greco 40546a
+                            major(sb.st_rdev),
Pablo Greco 40546a
+                            minor(sb.st_rdev),
Pablo Greco 40546a
+                            perms);
Pablo Greco 40546a
 }
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
@@ -1959,29 +1937,7 @@ int
Pablo Greco 40546a
 virCgroupDenyDevice(virCgroupPtr group, char type, int major, int minor,
Pablo Greco 40546a
                     int perms)
Pablo Greco 40546a
 {
Pablo Greco 40546a
-    VIR_AUTOFREE(char *) devstr = NULL;
Pablo Greco 40546a
-    VIR_AUTOFREE(char *) majorstr = NULL;
Pablo Greco 40546a
-    VIR_AUTOFREE(char *) minorstr = NULL;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    if ((major < 0 && VIR_STRDUP(majorstr, "*") < 0) ||
Pablo Greco 40546a
-        (major >= 0 && virAsprintf(&majorstr, "%i", major) < 0))
Pablo Greco 40546a
-        return -1;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    if ((minor < 0 && VIR_STRDUP(minorstr, "*") < 0) ||
Pablo Greco 40546a
-        (minor >= 0 && virAsprintf(&minorstr, "%i", minor) < 0))
Pablo Greco 40546a
-        return -1;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    if (virAsprintf(&devstr, "%c %s:%s %s", type, majorstr, minorstr,
Pablo Greco 40546a
-                    virCgroupGetDevicePermsString(perms)) < 0)
Pablo Greco 40546a
-        return -1;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    if (virCgroupSetValueStr(group,
Pablo Greco 40546a
-                             VIR_CGROUP_CONTROLLER_DEVICES,
Pablo Greco 40546a
-                             "devices.deny",
Pablo Greco 40546a
-                             devstr) < 0)
Pablo Greco 40546a
-        return -1;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    return 0;
Pablo Greco 40546a
+    VIR_CGROUP_BACKEND_CALL(group, denyDevice, -1, type, major, minor, perms);
Pablo Greco 40546a
 }
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
@@ -2020,11 +1976,11 @@ virCgroupDenyDevicePath(virCgroupPtr group,
Pablo Greco 40546a
     if (!S_ISCHR(sb.st_mode) && !S_ISBLK(sb.st_mode))
Pablo Greco 40546a
         return 1;
Pablo Greco 40546a
 
Pablo Greco 40546a
-    return virCgroupDenyDevice(group,
Pablo Greco 40546a
-                               S_ISCHR(sb.st_mode) ? 'c' : 'b',
Pablo Greco 40546a
-                               major(sb.st_rdev),
Pablo Greco 40546a
-                               minor(sb.st_rdev),
Pablo Greco 40546a
-                               perms);
Pablo Greco 40546a
+    VIR_CGROUP_BACKEND_CALL(group, denyDevice, -1,
Pablo Greco 40546a
+                            S_ISCHR(sb.st_mode) ? 'c' : 'b',
Pablo Greco 40546a
+                            major(sb.st_rdev),
Pablo Greco 40546a
+                            minor(sb.st_rdev),
Pablo Greco 40546a
+                            perms);
Pablo Greco 40546a
 }
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
diff --git a/src/util/vircgroupbackend.h b/src/util/vircgroupbackend.h
Pablo Greco 40546a
index 27e6b18ea2..04897b5895 100644
Pablo Greco 40546a
--- a/src/util/vircgroupbackend.h
Pablo Greco 40546a
+++ b/src/util/vircgroupbackend.h
Pablo Greco 40546a
@@ -255,6 +255,20 @@ typedef int
Pablo Greco 40546a
 (*virCgroupGetMemSwapUsageCB)(virCgroupPtr group,
Pablo Greco 40546a
                               unsigned long long *kb);
Pablo Greco 40546a
 
Pablo Greco 40546a
+typedef int
Pablo Greco 40546a
+(*virCgroupAllowDeviceCB)(virCgroupPtr group,
Pablo Greco 40546a
+                          char type,
Pablo Greco 40546a
+                          int major,
Pablo Greco 40546a
+                          int minor,
Pablo Greco 40546a
+                          int perms);
Pablo Greco 40546a
+
Pablo Greco 40546a
+typedef int
Pablo Greco 40546a
+(*virCgroupDenyDeviceCB)(virCgroupPtr group,
Pablo Greco 40546a
+                         char type,
Pablo Greco 40546a
+                         int major,
Pablo Greco 40546a
+                         int minor,
Pablo Greco 40546a
+                         int perms);
Pablo Greco 40546a
+
Pablo Greco 40546a
 struct _virCgroupBackend {
Pablo Greco 40546a
     virCgroupBackendType type;
Pablo Greco 40546a
 
Pablo Greco 40546a
@@ -304,6 +318,9 @@ struct _virCgroupBackend {
Pablo Greco 40546a
     virCgroupSetMemSwapHardLimitCB setMemSwapHardLimit;
Pablo Greco 40546a
     virCgroupGetMemSwapHardLimitCB getMemSwapHardLimit;
Pablo Greco 40546a
     virCgroupGetMemSwapUsageCB getMemSwapUsage;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    virCgroupAllowDeviceCB allowDevice;
Pablo Greco 40546a
+    virCgroupDenyDeviceCB denyDevice;
Pablo Greco 40546a
 };
Pablo Greco 40546a
 typedef struct _virCgroupBackend virCgroupBackend;
Pablo Greco 40546a
 typedef virCgroupBackend *virCgroupBackendPtr;
Pablo Greco 40546a
diff --git a/src/util/vircgroupv1.c b/src/util/vircgroupv1.c
Pablo Greco 40546a
index 55b1d3ebd0..02cd7ab956 100644
Pablo Greco 40546a
--- a/src/util/vircgroupv1.c
Pablo Greco 40546a
+++ b/src/util/vircgroupv1.c
Pablo Greco 40546a
@@ -1671,6 +1671,72 @@ virCgroupV1GetMemSwapUsage(virCgroupPtr group,
Pablo Greco 40546a
 }
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
+static int
Pablo Greco 40546a
+virCgroupV1AllowDevice(virCgroupPtr group,
Pablo Greco 40546a
+                       char type,
Pablo Greco 40546a
+                       int major,
Pablo Greco 40546a
+                       int minor,
Pablo Greco 40546a
+                       int perms)
Pablo Greco 40546a
+{
Pablo Greco 40546a
+    VIR_AUTOFREE(char *) devstr = NULL;
Pablo Greco 40546a
+    VIR_AUTOFREE(char *) majorstr = NULL;
Pablo Greco 40546a
+    VIR_AUTOFREE(char *) minorstr = NULL;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if ((major < 0 && VIR_STRDUP(majorstr, "*") < 0) ||
Pablo Greco 40546a
+        (major >= 0 && virAsprintf(&majorstr, "%i", major) < 0))
Pablo Greco 40546a
+        return -1;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if ((minor < 0 && VIR_STRDUP(minorstr, "*") < 0) ||
Pablo Greco 40546a
+        (minor >= 0 && virAsprintf(&minorstr, "%i", minor) < 0))
Pablo Greco 40546a
+        return -1;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if (virAsprintf(&devstr, "%c %s:%s %s", type, majorstr, minorstr,
Pablo Greco 40546a
+                    virCgroupGetDevicePermsString(perms)) < 0)
Pablo Greco 40546a
+        return -1;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if (virCgroupSetValueStr(group,
Pablo Greco 40546a
+                             VIR_CGROUP_CONTROLLER_DEVICES,
Pablo Greco 40546a
+                             "devices.allow",
Pablo Greco 40546a
+                             devstr) < 0)
Pablo Greco 40546a
+        return -1;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    return 0;
Pablo Greco 40546a
+}
Pablo Greco 40546a
+
Pablo Greco 40546a
+
Pablo Greco 40546a
+static int
Pablo Greco 40546a
+virCgroupV1DenyDevice(virCgroupPtr group,
Pablo Greco 40546a
+                      char type,
Pablo Greco 40546a
+                      int major,
Pablo Greco 40546a
+                      int minor,
Pablo Greco 40546a
+                      int perms)
Pablo Greco 40546a
+{
Pablo Greco 40546a
+    VIR_AUTOFREE(char *) devstr = NULL;
Pablo Greco 40546a
+    VIR_AUTOFREE(char *) majorstr = NULL;
Pablo Greco 40546a
+    VIR_AUTOFREE(char *) minorstr = NULL;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if ((major < 0 && VIR_STRDUP(majorstr, "*") < 0) ||
Pablo Greco 40546a
+        (major >= 0 && virAsprintf(&majorstr, "%i", major) < 0))
Pablo Greco 40546a
+        return -1;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if ((minor < 0 && VIR_STRDUP(minorstr, "*") < 0) ||
Pablo Greco 40546a
+        (minor >= 0 && virAsprintf(&minorstr, "%i", minor) < 0))
Pablo Greco 40546a
+        return -1;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if (virAsprintf(&devstr, "%c %s:%s %s", type, majorstr, minorstr,
Pablo Greco 40546a
+                    virCgroupGetDevicePermsString(perms)) < 0)
Pablo Greco 40546a
+        return -1;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if (virCgroupSetValueStr(group,
Pablo Greco 40546a
+                             VIR_CGROUP_CONTROLLER_DEVICES,
Pablo Greco 40546a
+                             "devices.deny",
Pablo Greco 40546a
+                             devstr) < 0)
Pablo Greco 40546a
+        return -1;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    return 0;
Pablo Greco 40546a
+}
Pablo Greco 40546a
+
Pablo Greco 40546a
+
Pablo Greco 40546a
 virCgroupBackend virCgroupV1Backend = {
Pablo Greco 40546a
     .type = VIR_CGROUP_BACKEND_TYPE_V1,
Pablo Greco 40546a
 
Pablo Greco 40546a
@@ -1718,6 +1784,9 @@ virCgroupBackend virCgroupV1Backend = {
Pablo Greco 40546a
     .setMemSwapHardLimit = virCgroupV1SetMemSwapHardLimit,
Pablo Greco 40546a
     .getMemSwapHardLimit = virCgroupV1GetMemSwapHardLimit,
Pablo Greco 40546a
     .getMemSwapUsage = virCgroupV1GetMemSwapUsage,
Pablo Greco 40546a
+
Pablo Greco 40546a
+    .allowDevice = virCgroupV1AllowDevice,
Pablo Greco 40546a
+    .denyDevice = virCgroupV1DenyDevice,
Pablo Greco 40546a
 };
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
-- 
Pablo Greco 40546a
2.22.0
Pablo Greco 40546a