c480ed
From 81bf1dade08ee40a9ecc32f5d384fdf35e3edbf3 Mon Sep 17 00:00:00 2001
c480ed
Message-Id: <81bf1dade08ee40a9ecc32f5d384fdf35e3edbf3@dist-git>
c480ed
From: Pavel Hrdina <phrdina@redhat.com>
c480ed
Date: Mon, 1 Jul 2019 17:06:24 +0200
c480ed
Subject: [PATCH] vircgroup: detect available backend for cgroup
c480ed
MIME-Version: 1.0
c480ed
Content-Type: text/plain; charset=UTF-8
c480ed
Content-Transfer-Encoding: 8bit
c480ed
c480ed
We need to update one test-case because now new cgroup object will be
c480ed
created only if there is any cgroup backend available.
c480ed
c480ed
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c480ed
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c480ed
(cherry picked from commit f60af21594801088fff217aebf33b58825bd8a80)
c480ed
c480ed
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1689297
c480ed
c480ed
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c480ed
Message-Id: <2815d54d5cffdef81bf9a41136b2d6a7e1d75b55.1561993100.git.phrdina@redhat.com>
c480ed
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c480ed
---
c480ed
 src/util/vircgroup.c     | 18 ++++++++++++++++++
c480ed
 src/util/vircgrouppriv.h |  3 +++
c480ed
 tests/vircgrouptest.c    | 38 +++-----------------------------------
c480ed
 3 files changed, 24 insertions(+), 35 deletions(-)
c480ed
c480ed
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
c480ed
index 47a3b9ed58..15d0cb65ac 100644
c480ed
--- a/src/util/vircgroup.c
c480ed
+++ b/src/util/vircgroup.c
c480ed
@@ -711,10 +711,28 @@ virCgroupDetect(virCgroupPtr group,
c480ed
                 virCgroupPtr parent)
c480ed
 {
c480ed
     int rc;
c480ed
+    size_t i;
c480ed
+    virCgroupBackendPtr *backends = virCgroupBackendGetAll();
c480ed
 
c480ed
     VIR_DEBUG("group=%p controllers=%d path=%s parent=%p",
c480ed
               group, controllers, path, parent);
c480ed
 
c480ed
+    if (!backends)
c480ed
+        return -1;
c480ed
+
c480ed
+    for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
c480ed
+        if (backends[i] && backends[i]->available()) {
c480ed
+            group->backend = backends[i];
c480ed
+            break;
c480ed
+        }
c480ed
+    }
c480ed
+
c480ed
+    if (!group->backend) {
c480ed
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
c480ed
+                       _("no cgroup backend available"));
c480ed
+        return -1;
c480ed
+    }
c480ed
+
c480ed
     if (parent) {
c480ed
         if (virCgroupCopyMounts(group, parent) < 0)
c480ed
             return -1;
c480ed
diff --git a/src/util/vircgrouppriv.h b/src/util/vircgrouppriv.h
c480ed
index 046c96c52c..2caa966fee 100644
c480ed
--- a/src/util/vircgrouppriv.h
c480ed
+++ b/src/util/vircgrouppriv.h
c480ed
@@ -30,6 +30,7 @@
c480ed
 # define __VIR_CGROUP_PRIV_H__
c480ed
 
c480ed
 # include "vircgroup.h"
c480ed
+# include "vircgroupbackend.h"
c480ed
 
c480ed
 struct _virCgroupController {
c480ed
     int type;
c480ed
@@ -47,6 +48,8 @@ typedef virCgroupController *virCgroupControllerPtr;
c480ed
 struct _virCgroup {
c480ed
     char *path;
c480ed
 
c480ed
+    virCgroupBackendPtr backend;
c480ed
+
c480ed
     virCgroupController controllers[VIR_CGROUP_CONTROLLER_LAST];
c480ed
 };
c480ed
 
c480ed
diff --git a/tests/vircgrouptest.c b/tests/vircgrouptest.c
c480ed
index 5031a2973d..588a6e824d 100644
c480ed
--- a/tests/vircgrouptest.c
c480ed
+++ b/tests/vircgrouptest.c
c480ed
@@ -114,16 +114,6 @@ const char *mountsAllInOne[VIR_CGROUP_CONTROLLER_LAST] = {
c480ed
     [VIR_CGROUP_CONTROLLER_BLKIO] = "/not/really/sys/fs/cgroup",
c480ed
     [VIR_CGROUP_CONTROLLER_SYSTEMD] = NULL,
c480ed
 };
c480ed
-const char *mountsLogind[VIR_CGROUP_CONTROLLER_LAST] = {
c480ed
-    [VIR_CGROUP_CONTROLLER_CPU] = NULL,
c480ed
-    [VIR_CGROUP_CONTROLLER_CPUACCT] = NULL,
c480ed
-    [VIR_CGROUP_CONTROLLER_CPUSET] = NULL,
c480ed
-    [VIR_CGROUP_CONTROLLER_MEMORY] = NULL,
c480ed
-    [VIR_CGROUP_CONTROLLER_DEVICES] = NULL,
c480ed
-    [VIR_CGROUP_CONTROLLER_FREEZER] = NULL,
c480ed
-    [VIR_CGROUP_CONTROLLER_BLKIO] = NULL,
c480ed
-    [VIR_CGROUP_CONTROLLER_SYSTEMD] = "/not/really/sys/fs/cgroup/systemd",
c480ed
-};
c480ed
 
c480ed
 const char *links[VIR_CGROUP_CONTROLLER_LAST] = {
c480ed
     [VIR_CGROUP_CONTROLLER_CPU] = "/not/really/sys/fs/cgroup/cpu",
c480ed
@@ -147,17 +137,6 @@ const char *linksAllInOne[VIR_CGROUP_CONTROLLER_LAST] = {
c480ed
     [VIR_CGROUP_CONTROLLER_SYSTEMD] = NULL,
c480ed
 };
c480ed
 
c480ed
-const char *linksLogind[VIR_CGROUP_CONTROLLER_LAST] = {
c480ed
-    [VIR_CGROUP_CONTROLLER_CPU] = NULL,
c480ed
-    [VIR_CGROUP_CONTROLLER_CPUACCT] = NULL,
c480ed
-    [VIR_CGROUP_CONTROLLER_CPUSET] = NULL,
c480ed
-    [VIR_CGROUP_CONTROLLER_MEMORY] = NULL,
c480ed
-    [VIR_CGROUP_CONTROLLER_DEVICES] = NULL,
c480ed
-    [VIR_CGROUP_CONTROLLER_FREEZER] = NULL,
c480ed
-    [VIR_CGROUP_CONTROLLER_BLKIO] = NULL,
c480ed
-    [VIR_CGROUP_CONTROLLER_SYSTEMD] = NULL,
c480ed
-};
c480ed
-
c480ed
 
c480ed
 struct _detectMountsData {
c480ed
     const char *file;
c480ed
@@ -555,24 +534,13 @@ static int testCgroupNewForSelfLogind(const void *args ATTRIBUTE_UNUSED)
c480ed
 {
c480ed
     virCgroupPtr cgroup = NULL;
c480ed
     int ret = -1;
c480ed
-    const char *placement[VIR_CGROUP_CONTROLLER_LAST] = {
c480ed
-        [VIR_CGROUP_CONTROLLER_CPU] = NULL,
c480ed
-        [VIR_CGROUP_CONTROLLER_CPUACCT] = NULL,
c480ed
-        [VIR_CGROUP_CONTROLLER_CPUSET] = NULL,
c480ed
-        [VIR_CGROUP_CONTROLLER_MEMORY] = NULL,
c480ed
-        [VIR_CGROUP_CONTROLLER_DEVICES] = NULL,
c480ed
-        [VIR_CGROUP_CONTROLLER_FREEZER] = NULL,
c480ed
-        [VIR_CGROUP_CONTROLLER_BLKIO] = NULL,
c480ed
-        [VIR_CGROUP_CONTROLLER_SYSTEMD] = "/",
c480ed
-    };
c480ed
 
c480ed
-    if (virCgroupNewSelf(&cgroup) < 0) {
c480ed
-        fprintf(stderr, "Cannot create cgroup for self\n");
c480ed
+    if (virCgroupNewSelf(&cgroup) == 0) {
c480ed
+        fprintf(stderr, "Expected cgroup creation to fail.\n");
c480ed
         goto cleanup;
c480ed
     }
c480ed
 
c480ed
-    ret = validateCgroup(cgroup, "", mountsLogind, linksLogind, placement);
c480ed
-
c480ed
+    ret = 0;
c480ed
  cleanup:
c480ed
     virCgroupFree(&cgroup);
c480ed
     return ret;
c480ed
-- 
c480ed
2.22.0
c480ed