[libvirt PATCH 22/25] vircgroup: refactor virCgroupEnableMissingControllers

Pavel Hrdina phrdina at redhat.com
Tue Nov 3 12:41:34 UTC 2020


Use virStringSplit() to get the list of directories needed to be
created. This improves readability of the code and stops passing
absolute path to virCgroupNewFromParent().

Signed-off-by: Pavel Hrdina <phrdina at redhat.com>
---
 src/util/vircgroup.c | 26 ++++++++------------------
 1 file changed, 8 insertions(+), 18 deletions(-)

diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
index 1e18b84b54..f4c1623567 100644
--- a/src/util/vircgroup.c
+++ b/src/util/vircgroup.c
@@ -1131,21 +1131,18 @@ virCgroupEnableMissingControllers(char *path,
                                   virCgroupPtr *group)
 {
     g_autoptr(virCgroup) parent = NULL;
-    char *offset = path;
+    VIR_AUTOSTRINGLIST tokens = virStringSplit(path, "/", 0);
+    size_t i;
 
-    if (virCgroupNew("/",
-                     controllers,
-                     &parent) < 0)
+    if (virCgroupNew("/", controllers, &parent) < 0)
         return -1;
 
-    for (;;) {
+    /* Skip the first token as it is empty string. */
+    for (i = 1; tokens[i]; i++) {
         g_autoptr(virCgroup) tmp = NULL;
-        char *t = strchr(offset + 1, '/');
-        if (t)
-            *t = '\0';
 
         if (virCgroupNewFromParent(parent,
-                                   path,
+                                   tokens[i],
                                    controllers,
                                    &tmp) < 0)
             return -1;
@@ -1153,17 +1150,10 @@ virCgroupEnableMissingControllers(char *path,
         if (virCgroupMakeGroup(parent, tmp, true, VIR_CGROUP_SYSTEMD) < 0)
             return -1;
 
-        if (t) {
-            *t = '/';
-            offset = t;
-            virCgroupFree(parent);
-            parent = g_steal_pointer(&tmp);
-        } else {
-            *group = g_steal_pointer(&tmp);
-            break;
-        }
+        parent = g_steal_pointer(&tmp);
     }
 
+    *group = g_steal_pointer(&parent);
     return 0;
 }
 
-- 
2.26.2




More information about the libvir-list mailing list