mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-08-25 13:50:09 +03:00
vircgroup: extract virCgroupV1Available
Reviewed-by: Ján Tomko <jtomko@redhat.com> Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
This commit is contained in:
@ -47,6 +47,7 @@
|
|||||||
|
|
||||||
#include "virutil.h"
|
#include "virutil.h"
|
||||||
#include "viralloc.h"
|
#include "viralloc.h"
|
||||||
|
#include "vircgroupbackend.h"
|
||||||
#include "virerror.h"
|
#include "virerror.h"
|
||||||
#include "virlog.h"
|
#include "virlog.h"
|
||||||
#include "virfile.h"
|
#include "virfile.h"
|
||||||
@ -60,8 +61,6 @@
|
|||||||
|
|
||||||
VIR_LOG_INIT("util.cgroup");
|
VIR_LOG_INIT("util.cgroup");
|
||||||
|
|
||||||
#define CGROUP_MAX_VAL 512
|
|
||||||
|
|
||||||
#define VIR_FROM_THIS VIR_FROM_CGROUP
|
#define VIR_FROM_THIS VIR_FROM_CGROUP
|
||||||
|
|
||||||
#define CGROUP_NB_TOTAL_CPU_STAT_PARAM 3
|
#define CGROUP_NB_TOTAL_CPU_STAT_PARAM 3
|
||||||
@ -128,29 +127,18 @@ virCgroupGetDevicePermsString(int perms)
|
|||||||
bool
|
bool
|
||||||
virCgroupAvailable(void)
|
virCgroupAvailable(void)
|
||||||
{
|
{
|
||||||
bool ret = false;
|
size_t i;
|
||||||
FILE *mounts = NULL;
|
virCgroupBackendPtr *backends = virCgroupBackendGetAll();
|
||||||
struct mntent entry;
|
|
||||||
char buf[CGROUP_MAX_VAL];
|
|
||||||
|
|
||||||
if (!virFileExists("/proc/cgroups"))
|
if (!backends)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (!(mounts = fopen("/proc/mounts", "r")))
|
for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
|
||||||
return false;
|
if (backends[i] && backends[i]->available())
|
||||||
|
return true;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
VIR_FORCE_FCLOSE(mounts);
|
return false;
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -25,14 +25,21 @@
|
|||||||
|
|
||||||
# include "vircgroup.h"
|
# include "vircgroup.h"
|
||||||
|
|
||||||
|
# define CGROUP_MAX_VAL 512
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
VIR_CGROUP_BACKEND_TYPE_V1 = 0,
|
VIR_CGROUP_BACKEND_TYPE_V1 = 0,
|
||||||
VIR_CGROUP_BACKEND_TYPE_LAST,
|
VIR_CGROUP_BACKEND_TYPE_LAST,
|
||||||
} virCgroupBackendType;
|
} virCgroupBackendType;
|
||||||
|
|
||||||
|
typedef bool
|
||||||
|
(*virCgroupAvailableCB)(void);
|
||||||
|
|
||||||
struct _virCgroupBackend {
|
struct _virCgroupBackend {
|
||||||
virCgroupBackendType type;
|
virCgroupBackendType type;
|
||||||
|
|
||||||
|
/* Mandatory callbacks that need to be implemented for every backend. */
|
||||||
|
virCgroupAvailableCB available;
|
||||||
};
|
};
|
||||||
typedef struct _virCgroupBackend virCgroupBackend;
|
typedef struct _virCgroupBackend virCgroupBackend;
|
||||||
typedef virCgroupBackend *virCgroupBackendPtr;
|
typedef virCgroupBackend *virCgroupBackendPtr;
|
||||||
|
@ -20,6 +20,10 @@
|
|||||||
*/
|
*/
|
||||||
#include <config.h>
|
#include <config.h>
|
||||||
|
|
||||||
|
#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R
|
||||||
|
# include <mntent.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "internal.h"
|
#include "internal.h"
|
||||||
|
|
||||||
#define __VIR_CGROUP_ALLOW_INCLUDE_PRIV_H__
|
#define __VIR_CGROUP_ALLOW_INCLUDE_PRIV_H__
|
||||||
@ -29,6 +33,7 @@
|
|||||||
#include "vircgroup.h"
|
#include "vircgroup.h"
|
||||||
#include "vircgroupbackend.h"
|
#include "vircgroupbackend.h"
|
||||||
#include "vircgroupv1.h"
|
#include "vircgroupv1.h"
|
||||||
|
#include "virfile.h"
|
||||||
#include "virlog.h"
|
#include "virlog.h"
|
||||||
|
|
||||||
VIR_LOG_INIT("util.cgroup");
|
VIR_LOG_INIT("util.cgroup");
|
||||||
@ -43,8 +48,38 @@ VIR_ENUM_IMPL(virCgroupV1Controller, VIR_CGROUP_CONTROLLER_LAST,
|
|||||||
"name=systemd");
|
"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 = {
|
virCgroupBackend virCgroupV1Backend = {
|
||||||
.type = VIR_CGROUP_BACKEND_TYPE_V1,
|
.type = VIR_CGROUP_BACKEND_TYPE_V1,
|
||||||
|
|
||||||
|
.available = virCgroupV1Available,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user