render / rpms / libvirt

Forked from rpms/libvirt 11 months ago
Clone
Pablo Greco 40546a
From 955ac573d3291bfb069664e2df9f4edb0ed09313 Mon Sep 17 00:00:00 2001
Pablo Greco 40546a
Message-Id: <955ac573d3291bfb069664e2df9f4edb0ed09313@dist-git>
Pablo Greco 40546a
From: Pavel Hrdina <phrdina@redhat.com>
Pablo Greco 40546a
Date: Mon, 1 Jul 2019 17:06:40 +0200
Pablo Greco 40546a
Subject: [PATCH] vircgroup: extract virCgroupV1SetOwner
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: Ján Tomko <jtomko@redhat.com>
Pablo Greco 40546a
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Pablo Greco 40546a
(cherry picked from commit dad061101d34a8e4b76ec3c03253ed3e83b50b2a)
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: <644b2434def24cbb7834a7950595c110d5438166.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        | 54 +------------------------------
Pablo Greco 40546a
 src/util/vircgroupbackend.h |  7 +++++
Pablo Greco 40546a
 src/util/vircgroupv1.c      | 63 +++++++++++++++++++++++++++++++++++++
Pablo Greco 40546a
 3 files changed, 71 insertions(+), 53 deletions(-)
Pablo Greco 40546a
Pablo Greco 40546a
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
Pablo Greco 40546a
index 7789966472..e57aecb08a 100644
Pablo Greco 40546a
--- a/src/util/vircgroup.c
Pablo Greco 40546a
+++ b/src/util/vircgroup.c
Pablo Greco 40546a
@@ -3282,59 +3282,7 @@ int virCgroupSetOwner(virCgroupPtr cgroup,
Pablo Greco 40546a
                       gid_t gid,
Pablo Greco 40546a
                       int controllers)
Pablo Greco 40546a
 {
Pablo Greco 40546a
-    int ret = -1;
Pablo Greco 40546a
-    size_t i;
Pablo Greco 40546a
-    DIR *dh = NULL;
Pablo Greco 40546a
-    int direrr;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
Pablo Greco 40546a
-        VIR_AUTOFREE(char *) base = NULL;
Pablo Greco 40546a
-        struct dirent *de;
Pablo Greco 40546a
-
Pablo Greco 40546a
-        if (!((1 << i) & controllers))
Pablo Greco 40546a
-            continue;
Pablo Greco 40546a
-
Pablo Greco 40546a
-        if (!cgroup->controllers[i].mountPoint)
Pablo Greco 40546a
-            continue;
Pablo Greco 40546a
-
Pablo Greco 40546a
-        if (virAsprintf(&base, "%s%s", cgroup->controllers[i].mountPoint,
Pablo Greco 40546a
-                        cgroup->controllers[i].placement) < 0)
Pablo Greco 40546a
-            goto cleanup;
Pablo Greco 40546a
-
Pablo Greco 40546a
-        if (virDirOpen(&dh, base) < 0)
Pablo Greco 40546a
-            goto cleanup;
Pablo Greco 40546a
-
Pablo Greco 40546a
-        while ((direrr = virDirRead(dh, &de, base)) > 0) {
Pablo Greco 40546a
-            VIR_AUTOFREE(char *) entry = NULL;
Pablo Greco 40546a
-
Pablo Greco 40546a
-            if (virAsprintf(&entry, "%s/%s", base, de->d_name) < 0)
Pablo Greco 40546a
-                goto cleanup;
Pablo Greco 40546a
-
Pablo Greco 40546a
-            if (chown(entry, uid, gid) < 0) {
Pablo Greco 40546a
-                virReportSystemError(errno,
Pablo Greco 40546a
-                                     _("cannot chown '%s' to (%u, %u)"),
Pablo Greco 40546a
-                                     entry, uid, gid);
Pablo Greco 40546a
-                goto cleanup;
Pablo Greco 40546a
-            }
Pablo Greco 40546a
-        }
Pablo Greco 40546a
-        if (direrr < 0)
Pablo Greco 40546a
-            goto cleanup;
Pablo Greco 40546a
-
Pablo Greco 40546a
-        if (chown(base, uid, gid) < 0) {
Pablo Greco 40546a
-            virReportSystemError(errno,
Pablo Greco 40546a
-                                 _("cannot chown '%s' to (%u, %u)"),
Pablo Greco 40546a
-                                 base, uid, gid);
Pablo Greco 40546a
-            goto cleanup;
Pablo Greco 40546a
-        }
Pablo Greco 40546a
-
Pablo Greco 40546a
-        VIR_DIR_CLOSE(dh);
Pablo Greco 40546a
-    }
Pablo Greco 40546a
-
Pablo Greco 40546a
-    ret = 0;
Pablo Greco 40546a
-
Pablo Greco 40546a
- cleanup:
Pablo Greco 40546a
-    VIR_DIR_CLOSE(dh);
Pablo Greco 40546a
-    return ret;
Pablo Greco 40546a
+    return cgroup->backend->setOwner(cgroup, uid, gid, controllers);
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 caeec3de60..74af796c2f 100644
Pablo Greco 40546a
--- a/src/util/vircgroupbackend.h
Pablo Greco 40546a
+++ b/src/util/vircgroupbackend.h
Pablo Greco 40546a
@@ -131,6 +131,12 @@ typedef int
Pablo Greco 40546a
                         const char *oldroot,
Pablo Greco 40546a
                         const char *mountopts);
Pablo Greco 40546a
 
Pablo Greco 40546a
+typedef int
Pablo Greco 40546a
+(*virCgroupSetOwnerCB)(virCgroupPtr cgroup,
Pablo Greco 40546a
+                       uid_t uid,
Pablo Greco 40546a
+                       gid_t gid,
Pablo Greco 40546a
+                       int controllers);
Pablo Greco 40546a
+
Pablo Greco 40546a
 struct _virCgroupBackend {
Pablo Greco 40546a
     virCgroupBackendType type;
Pablo Greco 40546a
 
Pablo Greco 40546a
@@ -152,6 +158,7 @@ struct _virCgroupBackend {
Pablo Greco 40546a
     virCgroupAddTaskCB addTask;
Pablo Greco 40546a
     virCgroupHasEmptyTasksCB hasEmptyTasks;
Pablo Greco 40546a
     virCgroupBindMountCB bindMount;
Pablo Greco 40546a
+    virCgroupSetOwnerCB setOwner;
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 d63525dfb0..c1e2583912 100644
Pablo Greco 40546a
--- a/src/util/vircgroupv1.c
Pablo Greco 40546a
+++ b/src/util/vircgroupv1.c
Pablo Greco 40546a
@@ -867,6 +867,68 @@ virCgroupV1BindMount(virCgroupPtr group,
Pablo Greco 40546a
 }
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
+static int
Pablo Greco 40546a
+virCgroupV1SetOwner(virCgroupPtr cgroup,
Pablo Greco 40546a
+                    uid_t uid,
Pablo Greco 40546a
+                    gid_t gid,
Pablo Greco 40546a
+                    int controllers)
Pablo Greco 40546a
+{
Pablo Greco 40546a
+    int ret = -1;
Pablo Greco 40546a
+    size_t i;
Pablo Greco 40546a
+    DIR *dh = NULL;
Pablo Greco 40546a
+    int direrr;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
Pablo Greco 40546a
+        VIR_AUTOFREE(char *) base = NULL;
Pablo Greco 40546a
+        struct dirent *de;
Pablo Greco 40546a
+
Pablo Greco 40546a
+        if (!((1 << i) & controllers))
Pablo Greco 40546a
+            continue;
Pablo Greco 40546a
+
Pablo Greco 40546a
+        if (!cgroup->controllers[i].mountPoint)
Pablo Greco 40546a
+            continue;
Pablo Greco 40546a
+
Pablo Greco 40546a
+        if (virAsprintf(&base, "%s%s", cgroup->controllers[i].mountPoint,
Pablo Greco 40546a
+                        cgroup->controllers[i].placement) < 0)
Pablo Greco 40546a
+            goto cleanup;
Pablo Greco 40546a
+
Pablo Greco 40546a
+        if (virDirOpen(&dh, base) < 0)
Pablo Greco 40546a
+            goto cleanup;
Pablo Greco 40546a
+
Pablo Greco 40546a
+        while ((direrr = virDirRead(dh, &de, base)) > 0) {
Pablo Greco 40546a
+            VIR_AUTOFREE(char *) entry = NULL;
Pablo Greco 40546a
+
Pablo Greco 40546a
+            if (virAsprintf(&entry, "%s/%s", base, de->d_name) < 0)
Pablo Greco 40546a
+                goto cleanup;
Pablo Greco 40546a
+
Pablo Greco 40546a
+            if (chown(entry, uid, gid) < 0) {
Pablo Greco 40546a
+                virReportSystemError(errno,
Pablo Greco 40546a
+                                     _("cannot chown '%s' to (%u, %u)"),
Pablo Greco 40546a
+                                     entry, uid, gid);
Pablo Greco 40546a
+                goto cleanup;
Pablo Greco 40546a
+            }
Pablo Greco 40546a
+        }
Pablo Greco 40546a
+        if (direrr < 0)
Pablo Greco 40546a
+            goto cleanup;
Pablo Greco 40546a
+
Pablo Greco 40546a
+        if (chown(base, uid, gid) < 0) {
Pablo Greco 40546a
+            virReportSystemError(errno,
Pablo Greco 40546a
+                                 _("cannot chown '%s' to (%u, %u)"),
Pablo Greco 40546a
+                                 base, uid, gid);
Pablo Greco 40546a
+            goto cleanup;
Pablo Greco 40546a
+        }
Pablo Greco 40546a
+
Pablo Greco 40546a
+        VIR_DIR_CLOSE(dh);
Pablo Greco 40546a
+    }
Pablo Greco 40546a
+
Pablo Greco 40546a
+    ret = 0;
Pablo Greco 40546a
+
Pablo Greco 40546a
+ cleanup:
Pablo Greco 40546a
+    VIR_DIR_CLOSE(dh);
Pablo Greco 40546a
+    return ret;
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
@@ -887,6 +949,7 @@ virCgroupBackend virCgroupV1Backend = {
Pablo Greco 40546a
     .addTask = virCgroupV1AddTask,
Pablo Greco 40546a
     .hasEmptyTasks = virCgroupV1HasEmptyTasks,
Pablo Greco 40546a
     .bindMount = virCgroupV1BindMount,
Pablo Greco 40546a
+    .setOwner = virCgroupV1SetOwner,
Pablo Greco 40546a
 };
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
-- 
Pablo Greco 40546a
2.22.0
Pablo Greco 40546a