From: Michael Tremer Date: Sat, 14 Dec 2024 13:54:20 +0000 (+0000) Subject: cgroups: Refactor the code that creates a cgroup X-Git-Tag: 0.9.30~720 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ebb69a77e52208416afdf80843b72b1b04ac3ff5;p=pakfire.git cgroups: Refactor the code that creates a cgroup 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 --- diff --git a/src/libpakfire/cgroup.c b/src/libpakfire/cgroup.c index 2581426bc..786f0f898 100644 --- a/src/libpakfire/cgroup.c +++ b/src/libpakfire/cgroup.c @@ -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; }