]> git.ipfire.org Git - pakfire.git/commitdiff
cgroups: Refactor the code that creates a cgroup
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 14 Dec 2024 13:54:20 +0000 (13:54 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 14 Dec 2024 13:54:20 +0000 (13:54 +0000)
The previous solution seemed to be very complicated, but this one will
likely have an extra syscall in some cases.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/cgroup.c

index 2581426bc6122e5735f9c272805857324ec0c637..786f0f898d74296a106120124ab84460b23140cf 100644 (file)
@@ -161,26 +161,26 @@ static int __pakfire_cgroup_open(struct pakfire_cgroup* cgroup) {
        if (!cgroup->parent)
                return pakfire_cgroup_open_root(cgroup);
 
-       // Try to open the cgroup
-       fd = openat(cgroup->parent->fd, cgroup->name, O_DIRECTORY|O_PATH|O_CLOEXEC);
-       if (fd < 0) {
+       // Try creating the cgroup
+       r = mkdirat(cgroup->parent->fd, cgroup->name, 0755);
+       if (r < 0) {
                switch (errno) {
-                       case ENOENT:
-                               r = mkdirat(cgroup->parent->fd, cgroup->name, 0755);
-                               if (r < 0) {
-                                       ERROR(cgroup->ctx, "Could not create cgroup '%s': %m\n", cgroup->name);
-                                       return -errno;
-                               }
-
-                               // Try opening it again
-                               return __pakfire_cgroup_open(cgroup);
+                       case EEXIST:
+                               break;
 
                        default:
-                               ERROR(cgroup->ctx, "Could not open cgroup '%s': %m\n", cgroup->name);
+                               ERROR(cgroup->ctx, "Could not create cgroup '%s': %m\n", cgroup->name);
                                return -errno;
                }
        }
 
+       // Try opening the cgroup
+       fd = openat(cgroup->parent->fd, cgroup->name, O_DIRECTORY|O_PATH|O_CLOEXEC);
+       if (fd < 0) {
+               ERROR(cgroup->ctx, "Could not open cgroup %s: %m\n", cgroup->path);
+               return -errno;
+       }
+
        return fd;
 }