From nobody Mon Feb 9 12:29:07 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1537285621266515.4945945332119; Tue, 18 Sep 2018 08:47:01 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 249B2308FF16; Tue, 18 Sep 2018 15:46:59 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id DB3CF68C99; Tue, 18 Sep 2018 15:46:58 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 789774A470; Tue, 18 Sep 2018 15:46:58 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w8IFkW45012402 for ; Tue, 18 Sep 2018 11:46:32 -0400 Received: by smtp.corp.redhat.com (Postfix) id C45115D9C7; Tue, 18 Sep 2018 15:46:32 +0000 (UTC) Received: from antique-work.brq.redhat.com (unknown [10.43.2.181]) by smtp.corp.redhat.com (Postfix) with ESMTP id 221DD82AD2 for ; Tue, 18 Sep 2018 15:46:31 +0000 (UTC) From: Pavel Hrdina To: libvir-list@redhat.com Date: Tue, 18 Sep 2018 17:45:41 +0200 Message-Id: <3aa9a9ec8c137f8119f27ac50dbd57e07a927932.1537285203.git.phrdina@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 20/47] vircgroup: extract virCgroupV1SetOwner X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.49]); Tue, 18 Sep 2018 15:47:00 +0000 (UTC) X-ZohoMail: RDMRC_0 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Pavel Hrdina Reviewed-by: J=EF=BF=BDn Tomko --- src/util/vircgroup.c | 54 +----------------------------- src/util/vircgroupbackend.h | 7 ++++ src/util/vircgroupv1.c | 65 +++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 53 deletions(-) diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c index 29faf5fb2c..479a2bf664 100644 --- a/src/util/vircgroup.c +++ b/src/util/vircgroup.c @@ -3284,59 +3284,7 @@ int virCgroupSetOwner(virCgroupPtr cgroup, gid_t gid, int controllers) { - int ret =3D -1; - size_t i; - DIR *dh =3D NULL; - int direrr; - - for (i =3D 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) { - VIR_AUTOFREE(char *) base =3D 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 =3D virDirRead(dh, &de, base)) > 0) { - VIR_AUTOFREE(char *) entry =3D 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 =3D 0; - - cleanup: - VIR_DIR_CLOSE(dh); - return ret; + return cgroup->backend->setOwner(cgroup, uid, gid, controllers); } =20 =20 diff --git a/src/util/vircgroupbackend.h b/src/util/vircgroupbackend.h index 70deb47461..4538654068 100644 --- a/src/util/vircgroupbackend.h +++ b/src/util/vircgroupbackend.h @@ -123,6 +123,12 @@ typedef int const char *oldroot, const char *mountopts); =20 +typedef int +(*virCgroupSetOwnerCB)(virCgroupPtr cgroup, + uid_t uid, + gid_t gid, + int controllers); + struct _virCgroupBackend { virCgroupBackendType type; =20 @@ -144,6 +150,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 0514ba392d..d74fc8ff96 100644 --- a/src/util/vircgroupv1.c +++ b/src/util/vircgroupv1.c @@ -866,6 +866,70 @@ virCgroupV1BindMount(virCgroupPtr group, } =20 =20 +static int +virCgroupV1SetOwner(virCgroupPtr cgroup, + uid_t uid, + gid_t gid, + int controllers) +{ + int ret =3D -1; + size_t i; + VIR_AUTOFREE(char *) base =3D NULL; + VIR_AUTOFREE(char *) entry =3D NULL; + DIR *dh =3D NULL; + int direrr; + + for (i =3D 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) { + 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 =3D virDirRead(dh, &de, base)) > 0) { + 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; + } + + VIR_FREE(entry); + } + 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_FREE(base); + VIR_DIR_CLOSE(dh); + } + + ret =3D 0; + + cleanup: + VIR_DIR_CLOSE(dh); + return ret; +} + + virCgroupBackend virCgroupV1Backend =3D { .type =3D VIR_CGROUP_BACKEND_TYPE_V1, =20 @@ -886,6 +950,7 @@ virCgroupBackend virCgroupV1Backend =3D { .addTask =3D virCgroupV1AddTask, .hasEmptyTasks =3D virCgroupV1HasEmptyTasks, .bindMount =3D virCgroupV1BindMount, + .setOwner =3D virCgroupV1SetOwner, }; =20 =20 --=20 2.17.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list