#include "virutil.h"
#include "viralloc.h"
+#include "vircgroupbackend.h"
#include "virerror.h"
#include "virlog.h"
#include "virfile.h"
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
bool
virCgroupAvailable(void)
{
- bool ret = false;
- FILE *mounts = NULL;
- struct mntent entry;
- char buf[CGROUP_MAX_VAL];
-
- if (!virFileExists("/proc/cgroups"))
- return false;
+ size_t i;
+ virCgroupBackendPtr *backends = virCgroupBackendGetAll();
- if (!(mounts = fopen("/proc/mounts", "r")))
+ if (!backends)
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;
}
# 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;
*/
#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__
#include "vircgroup.h"
#include "vircgroupbackend.h"
#include "vircgroupv1.h"
+#include "virfile.h"
#include "virlog.h"
VIR_LOG_INIT("util.cgroup");
"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,
};