[libvirt] [PATCH v2 3/5] cgroup macros refactoring

Roman Bogorodskiy bogorodskiy at gmail.com
Sun Aug 11 12:04:27 UTC 2013


Continue converting to VIR_CGROUP_SUPPORTED
---
 src/util/vircgroup.c | 378 +++++++++++++++++++++++++++------------------------
 1 file changed, 202 insertions(+), 176 deletions(-)

diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
index 3640fbd..795897b 100644
--- a/src/util/vircgroup.c
+++ b/src/util/vircgroup.c
@@ -74,157 +74,6 @@ typedef enum {
                                        */
 } virCgroupFlags;
 
-bool virCgroupAvailable(void)
-{
-    bool ret = false;
-#ifdef HAVE_GETMNTENT_R
-    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);
-#endif
-    return ret;
-}
-
-#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R
-
-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;
-}
-#else
-static bool
-virCgroupValidateMachineGroup(virCgroupPtr group ATTRIBUTE_UNUSED,
-                              const char *name ATTRIBUTE_UNUSED,
-                              const char *drivername ATTRIBUTE_UNUSED,
-                              bool stripEmulatorSuffix ATTRIBUTE_UNUSED)
-{
-    return true;
-}
-#endif
-
-/**
- * virCgroupFree:
- *
- * @group: The group structure to free
- */
-void virCgroupFree(virCgroupPtr *group)
-{
-    size_t i;
-
-    if (*group == NULL)
-        return;
-
-    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
-        VIR_FREE((*group)->controllers[i].mountPoint);
-        VIR_FREE((*group)->controllers[i].linkPoint);
-        VIR_FREE((*group)->controllers[i].placement);
-    }
-
-    VIR_FREE((*group)->path);
-    VIR_FREE(*group);
-}
-
-/**
- * virCgroupHasController: query whether a cgroup controller is present
- *
- * @cgroup: The group structure to be queried, or NULL
- * @controller: cgroup subsystem id
- *
- * Returns true if a cgroup controller is mounted and is associated
- * with this cgroup object.
- */
-bool virCgroupHasController(virCgroupPtr cgroup, int controller)
-{
-    if (!cgroup)
-        return false;
-    if (controller < 0 || controller >= VIR_CGROUP_CONTROLLER_LAST)
-        return false;
-    return cgroup->controllers[controller].mountPoint != NULL;
-}
-
 #if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R
 static int virCgroupCopyMounts(virCgroupPtr group,
                                virCgroupPtr parent)
@@ -1605,31 +1454,6 @@ int virCgroupNewDetect(pid_t pid ATTRIBUTE_UNUSED,
 }
 #endif
 
-/*
- * 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;
-}
 
 /*
  * Returns 0 on success, -1 on fatal error, -2 on systemd not available
@@ -2530,6 +2354,173 @@ 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:
+ *
+ * @group: The group structure to free
+ */
+void virCgroupFree(virCgroupPtr *group)
+{
+    size_t i;
+
+    if (*group == NULL)
+        return;
+
+    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
+        VIR_FREE((*group)->controllers[i].mountPoint);
+        VIR_FREE((*group)->controllers[i].linkPoint);
+        VIR_FREE((*group)->controllers[i].placement);
+    }
+
+    VIR_FREE((*group)->path);
+    VIR_FREE(*group);
+}
+
+
+/**
+ * virCgroupHasController: query whether a cgroup controller is present
+ *
+ * @cgroup: The group structure to be queried, or NULL
+ * @controller: cgroup subsystem id
+ *
+ * Returns true if a cgroup controller is mounted and is associated
+ * with this cgroup object.
+ */
+bool virCgroupHasController(virCgroupPtr cgroup, int controller)
+{
+    if (!cgroup)
+        return false;
+    if (controller < 0 || controller >= VIR_CGROUP_CONTROLLER_LAST)
+        return false;
+    return cgroup->controllers[controller].mountPoint != NULL;
+}
+
 int virCgroupRemoveRecursively(char *grppath)
 {
     DIR *grpdir;
@@ -3016,6 +3007,41 @@ cleanup:
     return ret;
 }
 #else /* VIR_CGROUP_SUPPORTED */
+bool virCgroupAvailable(void)
+{
+    return false;
+}
+
+
+int virCgroupNewDetectMachine(const char *name ATTRIBUTE_UNUSED,
+                              const char *drivername ATTRIBUTE_UNUSED,
+                              pid_t pid ATTRIBUTE_UNUSED,
+                              const char *partition ATTRIBUTE_UNUSED,
+                              int controllers ATTRIBUTE_UNUSED,
+                              virCgroupPtr *group ATTRIBUTE_UNUSED)
+{
+    virReportSystemError(ENXIO, "%s",
+                         _("Control groups not supported on this platform"));
+    return -1;
+}
+
+
+void virCgroupFree(virCgroupPtr *group ATTRIBUTE_UNUSED)
+{
+    virReportSystemError(ENXIO, "%s",
+                         _("Control groups not supported on this platform"));
+}
+
+
+bool virCgroupHasController(virCgroupPtr cgroup ATTRIBUTE_UNUSED,
+                            int controller ATTRIBUTE_UNUSED)
+{
+    virReportSystemError(ENXIO, "%s",
+                         _("Control groups not supported on this platform"));
+    return false;
+}
+
+
 int virCgroupRemoveRecursively(char *grppath ATTRIBUTE_UNUSED)
 {
     virReportSystemError(ENXIO, "%s",
-- 
1.8.2.3




More information about the libvir-list mailing list