From: Michael Tremer Date: Mon, 9 Dec 2024 15:58:53 +0000 (+0000) Subject: cgroups: Minor code cleanups X-Git-Tag: 0.9.30~742 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2fd0e776c892a34d737f63832621baf25da992f2;p=pakfire.git cgroups: Minor code cleanups Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/cgroup.c b/src/libpakfire/cgroup.c index 13f9858e4..39914696a 100644 --- a/src/libpakfire/cgroup.c +++ b/src/libpakfire/cgroup.c @@ -199,20 +199,6 @@ static struct pakfire_cgroup* pakfire_cgroup_parent(struct pakfire_cgroup* cgrou return parent; } -static void pakfire_cgroup_free(struct pakfire_cgroup* cgroup) { - DEBUG(cgroup->ctx, "Releasing cgroup %s at %p\n", - pakfire_cgroup_name(cgroup), cgroup); - - // Close the file descriptors - if (cgroup->fd >= 0) - close(cgroup->fd); - if (cgroup->devicesfd >= 0) - close(cgroup->devicesfd); - if (cgroup->ctx) - pakfire_ctx_unref(cgroup->ctx); - free(cgroup); -} - static int pakfire_cgroup_setup_devices(struct pakfire_cgroup* cgroup) { static char bpf_log_buffer[BPF_LOG_BUF_SIZE]; @@ -568,14 +554,13 @@ static int pakfire_cgroup_enable_accounting(struct pakfire_cgroup* cgroup) { */ int pakfire_cgroup_open(struct pakfire_cgroup** cgroup, struct pakfire_ctx* ctx, const char* path, int flags) { - int r = 1; + struct pakfire_cgroup* c = NULL; + int r; // Allocate the cgroup struct - struct pakfire_cgroup* c = calloc(1, sizeof(*c)); + c = calloc(1, sizeof(*c)); if (!c) - return 1; - - DEBUG(ctx, "Allocated cgroup %s at %p\n", path, c); + return -errno; // Store a reference to the context c->ctx = pakfire_ctx_ref(ctx); @@ -588,11 +573,13 @@ int pakfire_cgroup_open(struct pakfire_cgroup** cgroup, // Find the root r = pakfire_cgroup_set_root(c); - if (r) + if (r < 0) goto ERROR; // Copy path - pakfire_string_set(c->path, path); + r = pakfire_string_set(c->path, path); + if (r < 0) + goto ERROR; // Copy flags c->flags = flags; @@ -616,14 +603,26 @@ int pakfire_cgroup_open(struct pakfire_cgroup** cgroup, if (r) goto ERROR; - *cgroup = c; - return 0; + // Return the pointer + *cgroup = pakfire_cgroup_ref(c); ERROR: - pakfire_cgroup_free(c); + if (c) + pakfire_cgroup_unref(c); + return r; } +static void pakfire_cgroup_free(struct pakfire_cgroup* cgroup) { + if (cgroup->fd >= 0) + close(cgroup->fd); + if (cgroup->devicesfd >= 0) + close(cgroup->devicesfd); + if (cgroup->ctx) + pakfire_ctx_unref(cgroup->ctx); + free(cgroup); +} + struct pakfire_cgroup* pakfire_cgroup_ref(struct pakfire_cgroup* cgroup) { ++cgroup->nrefs; @@ -640,23 +639,21 @@ struct pakfire_cgroup* pakfire_cgroup_unref(struct pakfire_cgroup* cgroup) { // Open a child cgroup int pakfire_cgroup_child(struct pakfire_cgroup** child, - struct pakfire_cgroup* cgroup, const char* name, int flags) { - char path[PATH_MAX]; + struct pakfire_cgroup* cgroup, const char* path, int flags) { + char p[PATH_MAX]; int r; // Check input - if (!name) { - errno = EINVAL; - return 1; - } + if (!path) + return -EINVAL; // Join paths - r = pakfire_path_append(path, cgroup->path, name); - if (r) - return 1; + r = pakfire_path_append(p, cgroup->path, path); + if (r < 0) + return r; // Open the child group - return pakfire_cgroup_open(child, cgroup->ctx, path, flags); + return pakfire_cgroup_open(child, cgroup->ctx, p, flags); } static int pakfire_cgroup_procs_callback(struct pakfire_cgroup* cgroup, diff --git a/src/libpakfire/include/pakfire/cgroup.h b/src/libpakfire/include/pakfire/cgroup.h index 9f7f1f9e9..53621e88a 100644 --- a/src/libpakfire/include/pakfire/cgroup.h +++ b/src/libpakfire/include/pakfire/cgroup.h @@ -206,7 +206,7 @@ struct pakfire_cgroup* pakfire_cgroup_ref(struct pakfire_cgroup* cgroup); struct pakfire_cgroup* pakfire_cgroup_unref(struct pakfire_cgroup* cgroup); int pakfire_cgroup_child(struct pakfire_cgroup** child, - struct pakfire_cgroup* cgroup, const char* name, int flags); + struct pakfire_cgroup* cgroup, const char* path, int flags); int pakfire_cgroup_destroy(struct pakfire_cgroup* cgroup);