]> xenbits.xensource.com Git - libvirt.git/commitdiff
vircgroup: refactor virCgroupEnableMissingControllers
authorPavel Hrdina <phrdina@redhat.com>
Mon, 2 Nov 2020 15:05:07 +0000 (16:05 +0100)
committerPavel Hrdina <phrdina@redhat.com>
Tue, 3 Nov 2020 20:26:32 +0000 (21:26 +0100)
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@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/util/vircgroup.c

index 1e18b84b54224689a5feadfe4ae8a4f989dd4922..f4c162356785bd41955837c2c76f54fdf4c2fc71 100644 (file)
@@ -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;
 }