render / rpms / libvirt

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