render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
Blob Blame History Raw
From 0dcb8bdc63970be07228108f602a7425612bd1fe Mon Sep 17 00:00:00 2001
Message-Id: <0dcb8bdc63970be07228108f602a7425612bd1fe@dist-git>
From: Pavel Hrdina <phrdina@redhat.com>
Date: Mon, 1 Jul 2019 17:06:23 +0200
Subject: [PATCH] vircgroup: extract virCgroupV1Available
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Reviewed-by: Ján Tomko <jtomko@redhat.com>
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
(cherry picked from commit 57d35b72c9049348b36382ce88bee32553e0fae5)

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

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Message-Id: <c54c63779affc0f0a30224fd8001aa53fff9df52.1561993100.git.phrdina@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
---
 src/util/vircgroup.c        | 28 ++++++++--------------------
 src/util/vircgroupbackend.h |  7 +++++++
 src/util/vircgroupv1.c      | 35 +++++++++++++++++++++++++++++++++++
 3 files changed, 50 insertions(+), 20 deletions(-)

diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
index d55f411daa..47a3b9ed58 100644
--- a/src/util/vircgroup.c
+++ b/src/util/vircgroup.c
@@ -51,6 +51,7 @@
 
 #include "virutil.h"
 #include "viralloc.h"
+#include "vircgroupbackend.h"
 #include "virerror.h"
 #include "virlog.h"
 #include "virfile.h"
@@ -64,8 +65,6 @@
 
 VIR_LOG_INIT("util.cgroup");
 
-#define CGROUP_MAX_VAL 512
-
 #define VIR_FROM_THIS VIR_FROM_CGROUP
 
 #define CGROUP_NB_TOTAL_CPU_STAT_PARAM 3
@@ -132,29 +131,18 @@ virCgroupGetDevicePermsString(int perms)
 bool
 virCgroupAvailable(void)
 {
-    bool ret = false;
-    FILE *mounts = NULL;
-    struct mntent entry;
-    char buf[CGROUP_MAX_VAL];
+    size_t i;
+    virCgroupBackendPtr *backends = virCgroupBackendGetAll();
 
-    if (!virFileExists("/proc/cgroups"))
+    if (!backends)
         return false;
 
-    if (!(mounts = fopen("/proc/mounts", "r")))
-        return false;
-
-    while (getmntent_r(mounts, &entry, buf, sizeof(buf)) != NULL) {
-        /* We're looking for at least one 'cgroup' fs mount,
-         * which is *not* a named mount. */
-        if (STREQ(entry.mnt_type, "cgroup") &&
-            !strstr(entry.mnt_opts, "name=")) {
-            ret = true;
-            break;
-        }
+    for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
+        if (backends[i] && backends[i]->available())
+            return true;
     }
 
-    VIR_FORCE_FCLOSE(mounts);
-    return ret;
+    return false;
 }
 
 
diff --git a/src/util/vircgroupbackend.h b/src/util/vircgroupbackend.h
index db052485a8..88f51416b0 100644
--- a/src/util/vircgroupbackend.h
+++ b/src/util/vircgroupbackend.h
@@ -25,14 +25,21 @@
 
 # include "vircgroup.h"
 
+# define CGROUP_MAX_VAL 512
 
 typedef enum {
     VIR_CGROUP_BACKEND_TYPE_V1 = 0,
     VIR_CGROUP_BACKEND_TYPE_LAST,
 } virCgroupBackendType;
 
+typedef bool
+(*virCgroupAvailableCB)(void);
+
 struct _virCgroupBackend {
     virCgroupBackendType type;
+
+    /* Mandatory callbacks that need to be implemented for every backend. */
+    virCgroupAvailableCB available;
 };
 typedef struct _virCgroupBackend virCgroupBackend;
 typedef virCgroupBackend *virCgroupBackendPtr;
diff --git a/src/util/vircgroupv1.c b/src/util/vircgroupv1.c
index 4dda7865f1..73045b1109 100644
--- a/src/util/vircgroupv1.c
+++ b/src/util/vircgroupv1.c
@@ -20,6 +20,10 @@
  */
 #include <config.h>
 
+#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R
+# include <mntent.h>
+#endif
+
 #include "internal.h"
 
 #define __VIR_CGROUP_ALLOW_INCLUDE_PRIV_H__
@@ -29,6 +33,7 @@
 #include "vircgroup.h"
 #include "vircgroupbackend.h"
 #include "vircgroupv1.h"
+#include "virfile.h"
 #include "virlog.h"
 
 VIR_LOG_INIT("util.cgroup");
@@ -43,8 +48,38 @@ VIR_ENUM_IMPL(virCgroupV1Controller, VIR_CGROUP_CONTROLLER_LAST,
               "name=systemd");
 
 
+/* We're looking for at least one 'cgroup' fs mount,
+ * which is *not* a named mount. */
+static bool
+virCgroupV1Available(void)
+{
+    bool ret = false;
+    FILE *mounts = NULL;
+    struct mntent entry;
+    char buf[CGROUP_MAX_VAL];
+
+    if (!virFileExists("/proc/cgroups"))
+        return false;
+
+    if (!(mounts = fopen("/proc/mounts", "r")))
+        return false;
+
+    while (getmntent_r(mounts, &entry, buf, sizeof(buf)) != NULL) {
+        if (STREQ(entry.mnt_type, "cgroup") && !strstr(entry.mnt_opts, "name=")) {
+            ret = true;
+            break;
+        }
+    }
+
+    VIR_FORCE_FCLOSE(mounts);
+    return ret;
+}
+
+
 virCgroupBackend virCgroupV1Backend = {
     .type = VIR_CGROUP_BACKEND_TYPE_V1,
+
+    .available = virCgroupV1Available,
 };
 
 
-- 
2.22.0