Complete moving to VIR_CGROUP_SUPPORTED --- src/util/vircgroup.c | 497 ++++++++++++++++++++++++++++----------------------- 1 file changed, 272 insertions(+), 225 deletions(-) diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c index 795897b..e5625f8 100644 --- a/src/util/vircgroup.c +++ b/src/util/vircgroup.c @@ -74,7 +74,134 @@ typedef enum { */ } virCgroupFlags; -#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R + +#ifdef VIR_CGROUP_SUPPORTED +bool virCgroupAvailable(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")) { + ret = true; + break; + } + } + + VIR_FORCE_FCLOSE(mounts); + return ret; +} + + +static int virCgroupPartitionEscape(char **path); + +static bool +virCgroupValidateMachineGroup(virCgroupPtr group, + const char *name, + const char *drivername, + const char *partition, + bool stripEmulatorSuffix) +{ + size_t i; + bool valid = false; + char *partname; + char *scopename; + + if (virAsprintf(&partname, "%s.libvirt-%s", + name, drivername) < 0) + goto cleanup; + + if (virCgroupPartitionEscape(&partname) < 0) + goto cleanup; + + if (!partition) + partition = "/machine"; + + if (!(scopename = virSystemdMakeScopeName(name, drivername, partition))) + goto cleanup; + + if (virCgroupPartitionEscape(&scopename) < 0) + goto cleanup; + + for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) { + char *tmp; + + if (i == VIR_CGROUP_CONTROLLER_SYSTEMD) + continue; + + if (!group->controllers[i].placement) + continue; + + tmp = strrchr(group->controllers[i].placement, '/'); + if (!tmp) + goto cleanup; + + if (stripEmulatorSuffix && + (i == VIR_CGROUP_CONTROLLER_CPU || + i == VIR_CGROUP_CONTROLLER_CPUACCT || + i == VIR_CGROUP_CONTROLLER_CPUSET)) { + if (STREQ(tmp, "/emulator")) + *tmp = '\0'; + tmp = strrchr(group->controllers[i].placement, '/'); + if (!tmp) + goto cleanup; + } + + tmp++; + + if (STRNEQ(tmp, name) && + STRNEQ(tmp, partname) && + STRNEQ(tmp, scopename)) { + VIR_DEBUG("Name '%s' for controller '%s' does not match '%s', '%s' or '%s'", + tmp, virCgroupControllerTypeToString(i), name, partname, scopename); + goto cleanup; + } + } + + valid = true; + + cleanup: + VIR_FREE(partname); + VIR_FREE(scopename); + return valid; +} + + +/* + * Returns 0 on success (but @group may be NULL), -1 on fatal error + */ +int virCgroupNewDetectMachine(const char *name, + const char *drivername, + pid_t pid, + const char *partition, + int controllers, + virCgroupPtr *group) +{ + if (virCgroupNewDetect(pid, controllers, group) < 0) { + if (virCgroupNewIgnoreError()) + return 0; + return -1; + } + + if (!virCgroupValidateMachineGroup(*group, name, drivername, partition, true)) { + VIR_DEBUG("Failed to validate machine name for '%s' driver '%s'", + name, drivername); + virCgroupFree(group); + return 0; + } + + return 0; +} + + static int virCgroupCopyMounts(virCgroupPtr group, virCgroupPtr parent) { @@ -346,6 +473,7 @@ cleanup: return ret; } + static int virCgroupDetect(virCgroupPtr group, pid_t pid, int controllers, @@ -453,7 +581,6 @@ static int virCgroupDetect(virCgroupPtr group, return 0; } -#endif int virCgroupPathOfController(virCgroupPtr group, @@ -531,6 +658,7 @@ cleanup: return ret; } + static int virCgroupGetValueStr(virCgroupPtr group, int controller, const char *key, @@ -563,6 +691,7 @@ cleanup: return ret; } + static int virCgroupSetValueU64(virCgroupPtr group, int controller, const char *key, @@ -582,7 +711,6 @@ static int virCgroupSetValueU64(virCgroupPtr group, } - static int virCgroupSetValueI64(virCgroupPtr group, int controller, const char *key, @@ -601,6 +729,7 @@ static int virCgroupSetValueI64(virCgroupPtr group, return ret; } + static int virCgroupGetValueI64(virCgroupPtr group, int controller, const char *key, @@ -626,6 +755,7 @@ cleanup: return ret; } + static int virCgroupGetValueU64(virCgroupPtr group, int controller, const char *key, @@ -652,7 +782,6 @@ cleanup: } -#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R static int virCgroupCpuSetInherit(virCgroupPtr parent, virCgroupPtr group) { size_t i; @@ -686,6 +815,7 @@ static int virCgroupCpuSetInherit(virCgroupPtr parent, virCgroupPtr group) return 0; } + static int virCgroupSetMemoryUseHierarchy(virCgroupPtr group) { unsigned long long value; @@ -709,6 +839,7 @@ static int virCgroupSetMemoryUseHierarchy(virCgroupPtr group) return 0; } + static int virCgroupMakeGroup(virCgroupPtr parent, virCgroupPtr group, bool create, @@ -847,7 +978,6 @@ error: return -1; } -#endif /** @@ -930,6 +1060,7 @@ cleanup: return ret; } + /** * virCgroupAddTaskController: * @@ -1002,6 +1133,7 @@ cleanup: return rc; } + /** * virCgroupMoveTask: * @@ -1050,7 +1182,6 @@ cleanup: } -#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R static int virCgroupPartitionNeedsEscaping(const char *path) { FILE *fp = NULL; @@ -1120,6 +1251,7 @@ cleanup: return ret; } + static int virCgroupPartitionEscape(char **path) { size_t len = strlen(*path) + 1; @@ -1135,6 +1267,7 @@ static int virCgroupPartitionEscape(char **path) return 0; } + static int virCgroupSetPartitionSuffix(const char *path, char **res) { char **tokens; @@ -1179,6 +1312,7 @@ cleanup: return ret; } + /** * virCgroupNewPartition: * @path: path for the partition @@ -1242,17 +1376,6 @@ cleanup: VIR_FREE(newpath); return ret; } -#else -int virCgroupNewPartition(const char *path ATTRIBUTE_UNUSED, - bool create ATTRIBUTE_UNUSED, - int controllers ATTRIBUTE_UNUSED, - virCgroupPtr *group ATTRIBUTE_UNUSED) -{ - virReportSystemError(ENXIO, "%s", - _("Control groups not supported on this platform")); - return -1; -} -#endif /** @@ -1281,7 +1404,6 @@ int virCgroupNewSelf(virCgroupPtr *group) * * Returns 0 on success, or -1 on error */ -#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R int virCgroupNewDomainPartition(virCgroupPtr partition, const char *driver, const char *name, @@ -1323,18 +1445,7 @@ cleanup: VIR_FREE(grpname); return ret; } -#else -int virCgroupNewDomainPartition(virCgroupPtr partition ATTRIBUTE_UNUSED, - const char *driver ATTRIBUTE_UNUSED, - const char *name ATTRIBUTE_UNUSED, - bool create ATTRIBUTE_UNUSED, - virCgroupPtr *group ATTRIBUTE_UNUSED) -{ - virReportSystemError(ENXIO, "%s", - _("Control groups not supported on this platform")); - return -1; -} -#endif + /** * virCgroupNewVcpu: @@ -1346,7 +1457,6 @@ int virCgroupNewDomainPartition(virCgroupPtr partition ATTRIBUTE_UNUSED, * * Returns 0 on success, or -1 on error */ -#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R int virCgroupNewVcpu(virCgroupPtr domain, int vcpuid, bool create, @@ -1377,17 +1487,7 @@ cleanup: VIR_FREE(name); return ret; } -#else -int virCgroupNewVcpu(virCgroupPtr domain ATTRIBUTE_UNUSED, - int vcpuid ATTRIBUTE_UNUSED, - bool create ATTRIBUTE_UNUSED, - virCgroupPtr *group ATTRIBUTE_UNUSED) -{ - virReportSystemError(ENXIO, "%s", - _("Control groups not supported on this platform")); - return -1; -} -#endif + /** * virCgroupNewEmulator: @@ -1398,7 +1498,6 @@ int virCgroupNewVcpu(virCgroupPtr domain ATTRIBUTE_UNUSED, * * Returns: 0 on success or -1 on error */ -#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R int virCgroupNewEmulator(virCgroupPtr domain, bool create, virCgroupPtr *group) @@ -1423,36 +1522,14 @@ int virCgroupNewEmulator(virCgroupPtr domain, cleanup: return ret; } -#else -int virCgroupNewEmulator(virCgroupPtr domain ATTRIBUTE_UNUSED, - bool create ATTRIBUTE_UNUSED, - virCgroupPtr *group ATTRIBUTE_UNUSED) -{ - virReportSystemError(ENXIO, "%s", - _("Control groups not supported on this platform")); - return -1; -} -#endif - -#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R int virCgroupNewDetect(pid_t pid, int controllers, virCgroupPtr *group) { return virCgroupNew(pid, "", NULL, controllers, group); } -#else -int virCgroupNewDetect(pid_t pid ATTRIBUTE_UNUSED, - int controllers ATTRIBUTE_UNUSED, - virCgroupPtr *group ATTRIBUTE_UNUSED) -{ - virReportSystemError(ENXIO, "%s", - _("Control groups not supported on this platform")); - return -1; -} -#endif /* @@ -1561,6 +1638,7 @@ virCgroupNewMachineSystemd(const char *name, return ret; } + static int virCgroupNewMachineManual(const char *name, const char *drivername, @@ -1608,6 +1686,7 @@ cleanup: return ret; } + int virCgroupNewMachine(const char *name, const char *drivername, bool privileged, @@ -1646,6 +1725,7 @@ int virCgroupNewMachine(const char *name, group); } + bool virCgroupNewIgnoreError(void) { if (virLastErrorIsSystemErrno(ENXIO) || @@ -1658,6 +1738,7 @@ bool virCgroupNewIgnoreError(void) return false; } + /** * virCgroupSetBlkioWeight: * @@ -1681,6 +1762,7 @@ int virCgroupSetBlkioWeight(virCgroupPtr group, unsigned int weight) weight); } + /** * virCgroupGetBlkioWeight: * @@ -1701,6 +1783,7 @@ int virCgroupGetBlkioWeight(virCgroupPtr group, unsigned int *weight) return ret; } + /** * virCgroupSetBlkioDeviceWeight: * @@ -1713,7 +1796,6 @@ int virCgroupGetBlkioWeight(virCgroupPtr group, unsigned int *weight) * * Returns: 0 on success, -1 on error */ -#if defined(major) && defined(minor) int virCgroupSetBlkioDeviceWeight(virCgroupPtr group, const char *path, unsigned int weight) @@ -1754,17 +1836,7 @@ int virCgroupSetBlkioDeviceWeight(virCgroupPtr group, VIR_FREE(str); return ret; } -#else -int -virCgroupSetBlkioDeviceWeight(virCgroupPtr group ATTRIBUTE_UNUSED, - const char *path ATTRIBUTE_UNUSED, - unsigned int weight ATTRIBUTE_UNUSED) -{ - virReportSystemError(ENOSYS, "%s", - _("Control groups not supported on this platform")); - return -1; -} -#endif + /** * virCgroupSetMemory: @@ -1797,6 +1869,7 @@ int virCgroupSetMemory(virCgroupPtr group, unsigned long long kb) kb << 10); } + /** * virCgroupGetMemoryUsage: * @@ -1817,6 +1890,7 @@ int virCgroupGetMemoryUsage(virCgroupPtr group, unsigned long *kb) return ret; } + /** * virCgroupSetMemoryHardLimit: * @@ -1830,6 +1904,7 @@ int virCgroupSetMemoryHardLimit(virCgroupPtr group, unsigned long long kb) return virCgroupSetMemory(group, kb); } + /** * virCgroupGetMemoryHardLimit: * @@ -1850,6 +1925,7 @@ int virCgroupGetMemoryHardLimit(virCgroupPtr group, unsigned long long *kb) return ret; } + /** * virCgroupSetMemorySoftLimit: * @@ -1902,6 +1978,7 @@ int virCgroupGetMemorySoftLimit(virCgroupPtr group, unsigned long long *kb) return ret; } + /** * virCgroupSetMemSwapHardLimit: * @@ -1933,6 +2010,7 @@ int virCgroupSetMemSwapHardLimit(virCgroupPtr group, unsigned long long kb) kb << 10); } + /** * virCgroupGetMemSwapHardLimit: * @@ -1953,6 +2031,7 @@ int virCgroupGetMemSwapHardLimit(virCgroupPtr group, unsigned long long *kb) return ret; } + /** * virCgroupGetMemSwapUsage: * @@ -1973,6 +2052,7 @@ int virCgroupGetMemSwapUsage(virCgroupPtr group, unsigned long long *kb) return ret; } + /** * virCgroupSetCpusetMems: * @@ -1989,6 +2069,7 @@ int virCgroupSetCpusetMems(virCgroupPtr group, const char *mems) mems); } + /** * virCgroupGetCpusetMems: * @@ -2005,6 +2086,7 @@ int virCgroupGetCpusetMems(virCgroupPtr group, char **mems) mems); } + /** * virCgroupSetCpusetCpus: * @@ -2021,6 +2103,7 @@ int virCgroupSetCpusetCpus(virCgroupPtr group, const char *cpus) cpus); } + /** * virCgroupGetCpusetCpus: * @@ -2037,6 +2120,7 @@ int virCgroupGetCpusetCpus(virCgroupPtr group, char **cpus) cpus); } + /** * virCgroupDenyAllDevices: * @@ -2052,6 +2136,7 @@ int virCgroupDenyAllDevices(virCgroupPtr group) "a"); } + /** * virCgroupAllowDevice: * @@ -2088,6 +2173,7 @@ cleanup: return ret; } + /** * virCgroupAllowDeviceMajor: * @@ -2123,6 +2209,7 @@ cleanup: return ret; } + /** * virCgroupAllowDevicePath: * @@ -2136,7 +2223,6 @@ cleanup: * Returns: 0 on success, 1 if path exists but is not a device, or * -1 on error */ -#if defined(major) && defined(minor) int virCgroupAllowDevicePath(virCgroupPtr group, const char *path, int perms) { struct stat sb; @@ -2157,16 +2243,6 @@ int virCgroupAllowDevicePath(virCgroupPtr group, const char *path, int perms) minor(sb.st_rdev), perms); } -#else -int virCgroupAllowDevicePath(virCgroupPtr group ATTRIBUTE_UNUSED, - const char *path ATTRIBUTE_UNUSED, - int perms ATTRIBUTE_UNUSED) -{ - virReportSystemError(ENOSYS, "%s", - _("Control groups not supported on this platform")); - return -1; -} -#endif /** @@ -2205,6 +2281,7 @@ cleanup: return ret; } + /** * virCgroupDenyDeviceMajor: * @@ -2240,7 +2317,7 @@ cleanup: return ret; } -#if defined(major) && defined(minor) + int virCgroupDenyDevicePath(virCgroupPtr group, const char *path, int perms) { struct stat sb; @@ -2261,16 +2338,7 @@ int virCgroupDenyDevicePath(virCgroupPtr group, const char *path, int perms) minor(sb.st_rdev), perms); } -#else -int virCgroupDenyDevicePath(virCgroupPtr group ATTRIBUTE_UNUSED, - const char *path ATTRIBUTE_UNUSED, - int perms ATTRIBUTE_UNUSED) -{ - virReportSystemError(ENOSYS, "%s", - _("Control groups not supported on this platform")); - return -1; -} -#endif + int virCgroupSetCpuShares(virCgroupPtr group, unsigned long long shares) { @@ -2279,6 +2347,7 @@ int virCgroupSetCpuShares(virCgroupPtr group, unsigned long long shares) "cpu.shares", shares); } + int virCgroupGetCpuShares(virCgroupPtr group, unsigned long long *shares) { return virCgroupGetValueU64(group, @@ -2286,6 +2355,7 @@ int virCgroupGetCpuShares(virCgroupPtr group, unsigned long long *shares) "cpu.shares", shares); } + /** * virCgroupSetCpuCfsPeriod: * @@ -2353,132 +2423,6 @@ int virCgroupSetCpuCfsQuota(virCgroupPtr group, long long cfs_quota) } -#ifdef VIR_CGROUP_SUPPORTED -bool virCgroupAvailable(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")) { - ret = true; - break; - } - } - - VIR_FORCE_FCLOSE(mounts); - return ret; -} - - -static int virCgroupPartitionEscape(char **path); - -static bool -virCgroupValidateMachineGroup(virCgroupPtr group, - const char *name, - const char *drivername, - const char *partition, - bool stripEmulatorSuffix) -{ - size_t i; - bool valid = false; - char *partname; - char *scopename; - - if (virAsprintf(&partname, "%s.libvirt-%s", - name, drivername) < 0) - goto cleanup; - - if (virCgroupPartitionEscape(&partname) < 0) - goto cleanup; - - if (!partition) - partition = "/machine"; - - if (!(scopename = virSystemdMakeScopeName(name, drivername, partition))) - goto cleanup; - - if (virCgroupPartitionEscape(&scopename) < 0) - goto cleanup; - - for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) { - char *tmp; - - if (i == VIR_CGROUP_CONTROLLER_SYSTEMD) - continue; - - if (!group->controllers[i].placement) - continue; - - tmp = strrchr(group->controllers[i].placement, '/'); - if (!tmp) - goto cleanup; - - if (stripEmulatorSuffix && - (i == VIR_CGROUP_CONTROLLER_CPU || - i == VIR_CGROUP_CONTROLLER_CPUACCT || - i == VIR_CGROUP_CONTROLLER_CPUSET)) { - if (STREQ(tmp, "/emulator")) - *tmp = '\0'; - tmp = strrchr(group->controllers[i].placement, '/'); - if (!tmp) - goto cleanup; - } - - tmp++; - - if (STRNEQ(tmp, name) && - STRNEQ(tmp, partname) && - STRNEQ(tmp, scopename)) { - VIR_DEBUG("Name '%s' for controller '%s' does not match '%s', '%s' or '%s'", - tmp, virCgroupControllerTypeToString(i), name, partname, scopename); - goto cleanup; - } - } - - valid = true; - - cleanup: - VIR_FREE(partname); - VIR_FREE(scopename); - return valid; -} - - -/* - * Returns 0 on success (but @group may be NULL), -1 on fatal error - */ -int virCgroupNewDetectMachine(const char *name, - const char *drivername, - pid_t pid, - const char *partition, - int controllers, - virCgroupPtr *group) -{ - if (virCgroupNewDetect(pid, controllers, group) < 0) { - if (virCgroupNewIgnoreError()) - return 0; - return -1; - } - - if (!virCgroupValidateMachineGroup(*group, name, drivername, partition, true)) { - VIR_DEBUG("Failed to validate machine name for '%s' driver '%s'", - name, drivername); - virCgroupFree(group); - return 0; - } - - return 0; -} - /** * virCgroupFree: @@ -3026,6 +2970,109 @@ int virCgroupNewDetectMachine(const char *name ATTRIBUTE_UNUSED, } +int virCgroupPathOfController(virCgroupPtr group ATTRIBUTE_UNUSED, + int controller ATTRIBUTE_UNUSED, + const char *key ATTRIBUTE_UNUSED, + char **path ATTRIBUTE_UNUSED) +{ + virReportSystemError(ENXIO, "%s", + _("Control groups not supported on this platform")); + return -1; +} + + +int virCgroupNewPartition(const char *path ATTRIBUTE_UNUSED, + bool create ATTRIBUTE_UNUSED, + int controllers ATTRIBUTE_UNUSED, + virCgroupPtr *group ATTRIBUTE_UNUSED) +{ + virReportSystemError(ENXIO, "%s", + _("Control groups not supported on this platform")); + return -1; +} + + +int virCgroupNewSelf(virCgroupPtr *group ATTRIBUTE_UNUSED) +{ + virReportSystemError(ENXIO, "%s", + _("Control groups not supported on this platform")); + return -1; +} + + +int virCgroupNewDomainPartition(virCgroupPtr partition ATTRIBUTE_UNUSED, + const char *driver ATTRIBUTE_UNUSED, + const char *name ATTRIBUTE_UNUSED, + bool create ATTRIBUTE_UNUSED, + virCgroupPtr *group ATTRIBUTE_UNUSED) +{ + virReportSystemError(ENXIO, "%s", + _("Control groups not supported on this platform")); + return -1; +} + + +int virCgroupNewVcpu(virCgroupPtr domain ATTRIBUTE_UNUSED, + int vcpuid ATTRIBUTE_UNUSED, + bool create ATTRIBUTE_UNUSED, + virCgroupPtr *group ATTRIBUTE_UNUSED) +{ + virReportSystemError(ENXIO, "%s", + _("Control groups not supported on this platform")); + return -1; +} + + +int virCgroupNewEmulator(virCgroupPtr domain ATTRIBUTE_UNUSED, + bool create ATTRIBUTE_UNUSED, + virCgroupPtr *group ATTRIBUTE_UNUSED) +{ + virReportSystemError(ENXIO, "%s", + _("Control groups not supported on this platform")); + return -1; +} + +int virCgroupNewDetect(pid_t pid ATTRIBUTE_UNUSED, + int controllers ATTRIBUTE_UNUSED, + virCgroupPtr *group ATTRIBUTE_UNUSED) +{ + virReportSystemError(ENXIO, "%s", + _("Control groups not supported on this platform")); + return -1; +} + + +int +virCgroupSetBlkioDeviceWeight(virCgroupPtr group ATTRIBUTE_UNUSED, + const char *path ATTRIBUTE_UNUSED, + unsigned int weight ATTRIBUTE_UNUSED) +{ + virReportSystemError(ENOSYS, "%s", + _("Control groups not supported on this platform")); + return -1; +} + + +int virCgroupAllowDevicePath(virCgroupPtr group ATTRIBUTE_UNUSED, + const char *path ATTRIBUTE_UNUSED, + int perms ATTRIBUTE_UNUSED) +{ + virReportSystemError(ENOSYS, "%s", + _("Control groups not supported on this platform")); + return -1; +} + + +int virCgroupDenyDevicePath(virCgroupPtr group ATTRIBUTE_UNUSED, + const char *path ATTRIBUTE_UNUSED, + int perms ATTRIBUTE_UNUSED) +{ + virReportSystemError(ENOSYS, "%s", + _("Control groups not supported on this platform")); + return -1; +} + + void virCgroupFree(virCgroupPtr *group ATTRIBUTE_UNUSED) { virReportSystemError(ENXIO, "%s", -- 1.8.2.3 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list