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