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];
*/
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);
// 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;
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;
// 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,