]> git.ipfire.org Git - pakfire.git/commitdiff
cgroups: Minor code cleanups
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 9 Dec 2024 15:58:53 +0000 (15:58 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 9 Dec 2024 15:58:53 +0000 (15:58 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/cgroup.c
src/libpakfire/include/pakfire/cgroup.h

index 13f9858e4016cb4e660afcfaf50370d60ca47634..39914696ac89f9438fa59109d806cbd57ffaa098 100644 (file)
@@ -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,
index 9f7f1f9e96ae04115c251081c670d11ef67dbc36..53621e88aa398606005ae0a9111ec40be1a99a26 100644 (file)
@@ -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);