render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
Pablo Greco 40546a
From 0dcb8bdc63970be07228108f602a7425612bd1fe Mon Sep 17 00:00:00 2001
Pablo Greco 40546a
Message-Id: <0dcb8bdc63970be07228108f602a7425612bd1fe@dist-git>
Pablo Greco 40546a
From: Pavel Hrdina <phrdina@redhat.com>
Pablo Greco 40546a
Date: Mon, 1 Jul 2019 17:06:23 +0200
Pablo Greco 40546a
Subject: [PATCH] vircgroup: extract virCgroupV1Available
Pablo Greco 40546a
MIME-Version: 1.0
Pablo Greco 40546a
Content-Type: text/plain; charset=UTF-8
Pablo Greco 40546a
Content-Transfer-Encoding: 8bit
Pablo Greco 40546a
Pablo Greco 40546a
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Pablo Greco 40546a
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Pablo Greco 40546a
(cherry picked from commit 57d35b72c9049348b36382ce88bee32553e0fae5)
Pablo Greco 40546a
Pablo Greco 40546a
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1689297
Pablo Greco 40546a
Pablo Greco 40546a
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Pablo Greco 40546a
Message-Id: <c54c63779affc0f0a30224fd8001aa53fff9df52.1561993100.git.phrdina@redhat.com>
Pablo Greco 40546a
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Pablo Greco 40546a
---
Pablo Greco 40546a
 src/util/vircgroup.c        | 28 ++++++++--------------------
Pablo Greco 40546a
 src/util/vircgroupbackend.h |  7 +++++++
Pablo Greco 40546a
 src/util/vircgroupv1.c      | 35 +++++++++++++++++++++++++++++++++++
Pablo Greco 40546a
 3 files changed, 50 insertions(+), 20 deletions(-)
Pablo Greco 40546a
Pablo Greco 40546a
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
Pablo Greco 40546a
index d55f411daa..47a3b9ed58 100644
Pablo Greco 40546a
--- a/src/util/vircgroup.c
Pablo Greco 40546a
+++ b/src/util/vircgroup.c
Pablo Greco 40546a
@@ -51,6 +51,7 @@
Pablo Greco 40546a
 
Pablo Greco 40546a
 #include "virutil.h"
Pablo Greco 40546a
 #include "viralloc.h"
Pablo Greco 40546a
+#include "vircgroupbackend.h"
Pablo Greco 40546a
 #include "virerror.h"
Pablo Greco 40546a
 #include "virlog.h"
Pablo Greco 40546a
 #include "virfile.h"
Pablo Greco 40546a
@@ -64,8 +65,6 @@
Pablo Greco 40546a
 
Pablo Greco 40546a
 VIR_LOG_INIT("util.cgroup");
Pablo Greco 40546a
 
Pablo Greco 40546a
-#define CGROUP_MAX_VAL 512
Pablo Greco 40546a
-
Pablo Greco 40546a
 #define VIR_FROM_THIS VIR_FROM_CGROUP
Pablo Greco 40546a
 
Pablo Greco 40546a
 #define CGROUP_NB_TOTAL_CPU_STAT_PARAM 3
Pablo Greco 40546a
@@ -132,29 +131,18 @@ virCgroupGetDevicePermsString(int perms)
Pablo Greco 40546a
 bool
Pablo Greco 40546a
 virCgroupAvailable(void)
Pablo Greco 40546a
 {
Pablo Greco 40546a
-    bool ret = false;
Pablo Greco 40546a
-    FILE *mounts = NULL;
Pablo Greco 40546a
-    struct mntent entry;
Pablo Greco 40546a
-    char buf[CGROUP_MAX_VAL];
Pablo Greco 40546a
+    size_t i;
Pablo Greco 40546a
+    virCgroupBackendPtr *backends = virCgroupBackendGetAll();
Pablo Greco 40546a
 
Pablo Greco 40546a
-    if (!virFileExists("/proc/cgroups"))
Pablo Greco 40546a
+    if (!backends)
Pablo Greco 40546a
         return false;
Pablo Greco 40546a
 
Pablo Greco 40546a
-    if (!(mounts = fopen("/proc/mounts", "r")))
Pablo Greco 40546a
-        return false;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    while (getmntent_r(mounts, &entry, buf, sizeof(buf)) != NULL) {
Pablo Greco 40546a
-        /* We're looking for at least one 'cgroup' fs mount,
Pablo Greco 40546a
-         * which is *not* a named mount. */
Pablo Greco 40546a
-        if (STREQ(entry.mnt_type, "cgroup") &&
Pablo Greco 40546a
-            !strstr(entry.mnt_opts, "name=")) {
Pablo Greco 40546a
-            ret = true;
Pablo Greco 40546a
-            break;
Pablo Greco 40546a
-        }
Pablo Greco 40546a
+    for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
Pablo Greco 40546a
+        if (backends[i] && backends[i]->available())
Pablo Greco 40546a
+            return true;
Pablo Greco 40546a
     }
Pablo Greco 40546a
 
Pablo Greco 40546a
-    VIR_FORCE_FCLOSE(mounts);
Pablo Greco 40546a
-    return ret;
Pablo Greco 40546a
+    return false;
Pablo Greco 40546a
 }
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
diff --git a/src/util/vircgroupbackend.h b/src/util/vircgroupbackend.h
Pablo Greco 40546a
index db052485a8..88f51416b0 100644
Pablo Greco 40546a
--- a/src/util/vircgroupbackend.h
Pablo Greco 40546a
+++ b/src/util/vircgroupbackend.h
Pablo Greco 40546a
@@ -25,14 +25,21 @@
Pablo Greco 40546a
 
Pablo Greco 40546a
 # include "vircgroup.h"
Pablo Greco 40546a
 
Pablo Greco 40546a
+# define CGROUP_MAX_VAL 512
Pablo Greco 40546a
 
Pablo Greco 40546a
 typedef enum {
Pablo Greco 40546a
     VIR_CGROUP_BACKEND_TYPE_V1 = 0,
Pablo Greco 40546a
     VIR_CGROUP_BACKEND_TYPE_LAST,
Pablo Greco 40546a
 } virCgroupBackendType;
Pablo Greco 40546a
 
Pablo Greco 40546a
+typedef bool
Pablo Greco 40546a
+(*virCgroupAvailableCB)(void);
Pablo Greco 40546a
+
Pablo Greco 40546a
 struct _virCgroupBackend {
Pablo Greco 40546a
     virCgroupBackendType type;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    /* Mandatory callbacks that need to be implemented for every backend. */
Pablo Greco 40546a
+    virCgroupAvailableCB available;
Pablo Greco 40546a
 };
Pablo Greco 40546a
 typedef struct _virCgroupBackend virCgroupBackend;
Pablo Greco 40546a
 typedef virCgroupBackend *virCgroupBackendPtr;
Pablo Greco 40546a
diff --git a/src/util/vircgroupv1.c b/src/util/vircgroupv1.c
Pablo Greco 40546a
index 4dda7865f1..73045b1109 100644
Pablo Greco 40546a
--- a/src/util/vircgroupv1.c
Pablo Greco 40546a
+++ b/src/util/vircgroupv1.c
Pablo Greco 40546a
@@ -20,6 +20,10 @@
Pablo Greco 40546a
  */
Pablo Greco 40546a
 #include <config.h>
Pablo Greco 40546a
 
Pablo Greco 40546a
+#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R
Pablo Greco 40546a
+# include <mntent.h>
Pablo Greco 40546a
+#endif
Pablo Greco 40546a
+
Pablo Greco 40546a
 #include "internal.h"
Pablo Greco 40546a
 
Pablo Greco 40546a
 #define __VIR_CGROUP_ALLOW_INCLUDE_PRIV_H__
Pablo Greco 40546a
@@ -29,6 +33,7 @@
Pablo Greco 40546a
 #include "vircgroup.h"
Pablo Greco 40546a
 #include "vircgroupbackend.h"
Pablo Greco 40546a
 #include "vircgroupv1.h"
Pablo Greco 40546a
+#include "virfile.h"
Pablo Greco 40546a
 #include "virlog.h"
Pablo Greco 40546a
 
Pablo Greco 40546a
 VIR_LOG_INIT("util.cgroup");
Pablo Greco 40546a
@@ -43,8 +48,38 @@ VIR_ENUM_IMPL(virCgroupV1Controller, VIR_CGROUP_CONTROLLER_LAST,
Pablo Greco 40546a
               "name=systemd");
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
+/* We're looking for at least one 'cgroup' fs mount,
Pablo Greco 40546a
+ * which is *not* a named mount. */
Pablo Greco 40546a
+static bool
Pablo Greco 40546a
+virCgroupV1Available(void)
Pablo Greco 40546a
+{
Pablo Greco 40546a
+    bool ret = false;
Pablo Greco 40546a
+    FILE *mounts = NULL;
Pablo Greco 40546a
+    struct mntent entry;
Pablo Greco 40546a
+    char buf[CGROUP_MAX_VAL];
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if (!virFileExists("/proc/cgroups"))
Pablo Greco 40546a
+        return false;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if (!(mounts = fopen("/proc/mounts", "r")))
Pablo Greco 40546a
+        return false;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    while (getmntent_r(mounts, &entry, buf, sizeof(buf)) != NULL) {
Pablo Greco 40546a
+        if (STREQ(entry.mnt_type, "cgroup") && !strstr(entry.mnt_opts, "name=")) {
Pablo Greco 40546a
+            ret = true;
Pablo Greco 40546a
+            break;
Pablo Greco 40546a
+        }
Pablo Greco 40546a
+    }
Pablo Greco 40546a
+
Pablo Greco 40546a
+    VIR_FORCE_FCLOSE(mounts);
Pablo Greco 40546a
+    return ret;
Pablo Greco 40546a
+}
Pablo Greco 40546a
+
Pablo Greco 40546a
+
Pablo Greco 40546a
 virCgroupBackend virCgroupV1Backend = {
Pablo Greco 40546a
     .type = VIR_CGROUP_BACKEND_TYPE_V1,
Pablo Greco 40546a
+
Pablo Greco 40546a
+    .available = virCgroupV1Available,
Pablo Greco 40546a
 };
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
-- 
Pablo Greco 40546a
2.22.0
Pablo Greco 40546a