7548c0
From f835b834d7922bed1ccda35885e42ab7c3f4a70f Mon Sep 17 00:00:00 2001
7548c0
Message-Id: <f835b834d7922bed1ccda35885e42ab7c3f4a70f@dist-git>
7548c0
From: Pavel Hrdina <phrdina@redhat.com>
7548c0
Date: Fri, 19 Feb 2021 13:33:58 +0100
7548c0
Subject: [PATCH] vircgroup: introduce virCgroupV1Exists and virCgroupV2Exists
7548c0
MIME-Version: 1.0
7548c0
Content-Type: text/plain; charset=UTF-8
7548c0
Content-Transfer-Encoding: 8bit
7548c0
7548c0
This will check if the cgroup actually exists on the system.
7548c0
7548c0
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
7548c0
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
7548c0
(cherry picked from commit badc2bcc7398d8c0a739998a80411ddebf129512)
7548c0
7548c0
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1798463
7548c0
7548c0
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
7548c0
Message-Id: <14297ed923f0f23cc52506e61e637c8f45e331ee.1613737828.git.phrdina@redhat.com>
7548c0
Reviewed-by: Ján Tomko <jtomko@redhat.com>
7548c0
---
7548c0
 src/util/vircgroupbackend.h |  4 ++++
7548c0
 src/util/vircgroupv1.c      | 27 +++++++++++++++++++++++++++
7548c0
 src/util/vircgroupv2.c      | 15 +++++++++++++++
7548c0
 3 files changed, 46 insertions(+)
7548c0
7548c0
diff --git a/src/util/vircgroupbackend.h b/src/util/vircgroupbackend.h
7548c0
index ac7b3ae517..dabc7bd4b4 100644
7548c0
--- a/src/util/vircgroupbackend.h
7548c0
+++ b/src/util/vircgroupbackend.h
7548c0
@@ -115,6 +115,9 @@ typedef int
7548c0
                                const char *key,
7548c0
                                char **path);
7548c0
 
7548c0
+typedef bool
7548c0
+(*virCgroupExistsCB)(virCgroupPtr group);
7548c0
+
7548c0
 typedef int
7548c0
 (*virCgroupMakeGroupCB)(virCgroupPtr parent,
7548c0
                         virCgroupPtr group,
7548c0
@@ -378,6 +381,7 @@ struct _virCgroupBackend {
7548c0
     virCgroupGetAnyControllerCB getAnyController;
7548c0
     virCgroupPathOfControllerCB pathOfController;
7548c0
     virCgroupMakeGroupCB makeGroup;
7548c0
+    virCgroupExistsCB exists;
7548c0
     virCgroupRemoveCB remove;
7548c0
     virCgroupAddTaskCB addTask;
7548c0
     virCgroupHasEmptyTasksCB hasEmptyTasks;
7548c0
diff --git a/src/util/vircgroupv1.c b/src/util/vircgroupv1.c
7548c0
index eb2b611cee..57d617cb69 100644
7548c0
--- a/src/util/vircgroupv1.c
7548c0
+++ b/src/util/vircgroupv1.c
7548c0
@@ -670,6 +670,32 @@ virCgroupV1MakeGroup(virCgroupPtr parent,
7548c0
 }
7548c0
 
7548c0
 
7548c0
+static bool
7548c0
+virCgroupV1Exists(virCgroupPtr group)
7548c0
+{
7548c0
+    size_t i;
7548c0
+
7548c0
+    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
7548c0
+        g_autofree char *path = NULL;
7548c0
+
7548c0
+        if (i == VIR_CGROUP_CONTROLLER_SYSTEMD)
7548c0
+            continue;
7548c0
+
7548c0
+        if (!group->legacy[i].mountPoint)
7548c0
+            continue;
7548c0
+
7548c0
+        if (virCgroupV1PathOfController(group, i, "", &path) < 0)
7548c0
+            return false;
7548c0
+
7548c0
+        if (!virFileExists(path)) {
7548c0
+            return false;
7548c0
+        }
7548c0
+    }
7548c0
+
7548c0
+    return true;
7548c0
+}
7548c0
+
7548c0
+
7548c0
 static int
7548c0
 virCgroupV1Remove(virCgroupPtr group)
7548c0
 {
7548c0
@@ -2136,6 +2162,7 @@ virCgroupBackend virCgroupV1Backend = {
7548c0
     .getAnyController = virCgroupV1GetAnyController,
7548c0
     .pathOfController = virCgroupV1PathOfController,
7548c0
     .makeGroup = virCgroupV1MakeGroup,
7548c0
+    .exists = virCgroupV1Exists,
7548c0
     .remove = virCgroupV1Remove,
7548c0
     .addTask = virCgroupV1AddTask,
7548c0
     .hasEmptyTasks = virCgroupV1HasEmptyTasks,
7548c0
diff --git a/src/util/vircgroupv2.c b/src/util/vircgroupv2.c
7548c0
index 5e19ed8332..d15e2354cf 100644
7548c0
--- a/src/util/vircgroupv2.c
7548c0
+++ b/src/util/vircgroupv2.c
7548c0
@@ -493,6 +493,20 @@ virCgroupV2MakeGroup(virCgroupPtr parent,
7548c0
 }
7548c0
 
7548c0
 
7548c0
+static bool
7548c0
+virCgroupV2Exists(virCgroupPtr group)
7548c0
+{
7548c0
+    g_autofree char *path = NULL;
7548c0
+    int controller;
7548c0
+
7548c0
+    controller = virCgroupV2GetAnyController(group);
7548c0
+    if (virCgroupV2PathOfController(group, controller, "", &path) < 0)
7548c0
+        return false;
7548c0
+
7548c0
+    return virFileExists(path);
7548c0
+}
7548c0
+
7548c0
+
7548c0
 static int
7548c0
 virCgroupV2Remove(virCgroupPtr group)
7548c0
 {
7548c0
@@ -1886,6 +1900,7 @@ virCgroupBackend virCgroupV2Backend = {
7548c0
     .getAnyController = virCgroupV2GetAnyController,
7548c0
     .pathOfController = virCgroupV2PathOfController,
7548c0
     .makeGroup = virCgroupV2MakeGroup,
7548c0
+    .exists = virCgroupV2Exists,
7548c0
     .remove = virCgroupV2Remove,
7548c0
     .addTask = virCgroupV2AddTask,
7548c0
     .hasEmptyTasks = virCgroupV2HasEmptyTasks,
7548c0
-- 
7548c0
2.30.0
7548c0