From e638778eb38b4d7fbc3038dffe63c6db14b310f9 Mon Sep 17 00:00:00 2001 From: "Daniel P. Berrange" Date: Tue, 23 Jul 2013 15:26:21 +0100 Subject: [PATCH] Add API for checking if a cgroup is valid for a domain Add virCgroupIsValidMachine API to check whether an auto detected cgroup is valid for a machine. This lets us check if a VM has just been placed into some generic shared cgroup, or worse, the root cgroup Signed-off-by: Daniel P. Berrange --- src/libvirt_private.syms | 1 + src/util/vircgroup.c | 42 ++++++++++++++++++++++++++++++++++++++++ src/util/vircgroup.h | 5 +++++ 3 files changed, 48 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 49b9f9d943..3be604b913 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1182,6 +1182,7 @@ virCgroupGetMemSwapHardLimit; virCgroupGetMemSwapUsage; virCgroupHasController; virCgroupIsolateMount; +virCgroupIsValidMachineGroup; virCgroupKill; virCgroupKillPainfully; virCgroupKillRecursive; diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c index 94d19e00f5..c84caf1ed5 100644 --- a/src/util/vircgroup.c +++ b/src/util/vircgroup.c @@ -67,6 +67,8 @@ typedef enum { */ } virCgroupFlags; +static int virCgroupPartitionEscape(char **path); + bool virCgroupAvailable(void) { FILE *mounts = NULL; @@ -91,6 +93,46 @@ bool virCgroupAvailable(void) return ret; } +bool virCgroupIsValidMachineGroup(virCgroupPtr group, + const char *name, + const char *drivername) +{ + size_t i; + bool valid = false; + char *partname; + + if (virAsprintf(&partname, "%s.libvirt-%s", + name, drivername) < 0) + goto cleanup; + + if (virCgroupPartitionEscape(&partname) < 0) + goto cleanup; + + for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) { + char *tmp; + + if (!group->controllers[i].placement) + continue; + + tmp = strrchr(group->controllers[i].placement, '/'); + if (!tmp) + goto cleanup; + tmp++; + + if (STRNEQ(tmp, name) && + STRNEQ(tmp, partname)) + goto cleanup; + + } + + valid = true; + + cleanup: + VIR_FREE(partname); + return valid; +} + + /** * virCgroupFree: * diff --git a/src/util/vircgroup.h b/src/util/vircgroup.h index 602d4ff7e9..9bf0d7e86a 100644 --- a/src/util/vircgroup.h +++ b/src/util/vircgroup.h @@ -48,6 +48,11 @@ VIR_ENUM_DECL(virCgroupController); bool virCgroupAvailable(void); +bool virCgroupIsValidMachineGroup(virCgroupPtr group, + const char *machinename, + const char *drivername); + + int virCgroupNewPartition(const char *path, bool create, int controllers, -- GitLab