Blob Blame History Raw
From f835b834d7922bed1ccda35885e42ab7c3f4a70f Mon Sep 17 00:00:00 2001
Message-Id: <f835b834d7922bed1ccda35885e42ab7c3f4a70f@dist-git>
From: Pavel Hrdina <phrdina@redhat.com>
Date: Fri, 19 Feb 2021 13:33:58 +0100
Subject: [PATCH] vircgroup: introduce virCgroupV1Exists and virCgroupV2Exists
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This will check if the cgroup actually exists on the system.

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
(cherry picked from commit badc2bcc7398d8c0a739998a80411ddebf129512)

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1798463

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Message-Id: <14297ed923f0f23cc52506e61e637c8f45e331ee.1613737828.git.phrdina@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
---
 src/util/vircgroupbackend.h |  4 ++++
 src/util/vircgroupv1.c      | 27 +++++++++++++++++++++++++++
 src/util/vircgroupv2.c      | 15 +++++++++++++++
 3 files changed, 46 insertions(+)

diff --git a/src/util/vircgroupbackend.h b/src/util/vircgroupbackend.h
index ac7b3ae517..dabc7bd4b4 100644
--- a/src/util/vircgroupbackend.h
+++ b/src/util/vircgroupbackend.h
@@ -115,6 +115,9 @@ typedef int
                                const char *key,
                                char **path);
 
+typedef bool
+(*virCgroupExistsCB)(virCgroupPtr group);
+
 typedef int
 (*virCgroupMakeGroupCB)(virCgroupPtr parent,
                         virCgroupPtr group,
@@ -378,6 +381,7 @@ struct _virCgroupBackend {
     virCgroupGetAnyControllerCB getAnyController;
     virCgroupPathOfControllerCB pathOfController;
     virCgroupMakeGroupCB makeGroup;
+    virCgroupExistsCB exists;
     virCgroupRemoveCB remove;
     virCgroupAddTaskCB addTask;
     virCgroupHasEmptyTasksCB hasEmptyTasks;
diff --git a/src/util/vircgroupv1.c b/src/util/vircgroupv1.c
index eb2b611cee..57d617cb69 100644
--- a/src/util/vircgroupv1.c
+++ b/src/util/vircgroupv1.c
@@ -670,6 +670,32 @@ virCgroupV1MakeGroup(virCgroupPtr parent,
 }
 
 
+static bool
+virCgroupV1Exists(virCgroupPtr group)
+{
+    size_t i;
+
+    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
+        g_autofree char *path = NULL;
+
+        if (i == VIR_CGROUP_CONTROLLER_SYSTEMD)
+            continue;
+
+        if (!group->legacy[i].mountPoint)
+            continue;
+
+        if (virCgroupV1PathOfController(group, i, "", &path) < 0)
+            return false;
+
+        if (!virFileExists(path)) {
+            return false;
+        }
+    }
+
+    return true;
+}
+
+
 static int
 virCgroupV1Remove(virCgroupPtr group)
 {
@@ -2136,6 +2162,7 @@ virCgroupBackend virCgroupV1Backend = {
     .getAnyController = virCgroupV1GetAnyController,
     .pathOfController = virCgroupV1PathOfController,
     .makeGroup = virCgroupV1MakeGroup,
+    .exists = virCgroupV1Exists,
     .remove = virCgroupV1Remove,
     .addTask = virCgroupV1AddTask,
     .hasEmptyTasks = virCgroupV1HasEmptyTasks,
diff --git a/src/util/vircgroupv2.c b/src/util/vircgroupv2.c
index 5e19ed8332..d15e2354cf 100644
--- a/src/util/vircgroupv2.c
+++ b/src/util/vircgroupv2.c
@@ -493,6 +493,20 @@ virCgroupV2MakeGroup(virCgroupPtr parent,
 }
 
 
+static bool
+virCgroupV2Exists(virCgroupPtr group)
+{
+    g_autofree char *path = NULL;
+    int controller;
+
+    controller = virCgroupV2GetAnyController(group);
+    if (virCgroupV2PathOfController(group, controller, "", &path) < 0)
+        return false;
+
+    return virFileExists(path);
+}
+
+
 static int
 virCgroupV2Remove(virCgroupPtr group)
 {
@@ -1886,6 +1900,7 @@ virCgroupBackend virCgroupV2Backend = {
     .getAnyController = virCgroupV2GetAnyController,
     .pathOfController = virCgroupV2PathOfController,
     .makeGroup = virCgroupV2MakeGroup,
+    .exists = virCgroupV2Exists,
     .remove = virCgroupV2Remove,
     .addTask = virCgroupV2AddTask,
     .hasEmptyTasks = virCgroupV2HasEmptyTasks,
-- 
2.30.0