]> git.ipfire.org Git - pakfire.git/commitdiff
cgroup: Initialize file descriptors
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 12 Oct 2024 16:36:57 +0000 (16:36 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 12 Oct 2024 16:36:57 +0000 (16:36 +0000)
It was possible that those closed the standard input.

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

index 60284f72e40f1e3cebab83db23d7d9e8986a05ec..2f8c6f015821b89a021fb985e4ac055e009ef347 100644 (file)
@@ -283,13 +283,13 @@ static int __pakfire_cgroup_create(struct pakfire_cgroup* cgroup) {
        This function returns a negative value on error.
 */
 static int __pakfire_cgroup_open(struct pakfire_cgroup* cgroup) {
-       int fd = -1;
+       int fd = -EBADF;
        int r;
 
        // Open file descriptor of the cgroup root
        int rootfd = pakfire_cgroup_open_root(cgroup);
        if (rootfd < 0)
-               return -1;
+               return rootfd;
 
        // Return the rootfd for the root group
        if (pakfire_cgroup_is_root(cgroup))
@@ -357,7 +357,7 @@ static ssize_t pakfire_cgroup_read(struct pakfire_cgroup* cgroup, const char* pa
        ssize_t bytes_read = -1;
 
        // Check if this cgroup has been destroyed already
-       if (!cgroup->fd) {
+       if (cgroup->fd < 0) {
                CTX_ERROR(cgroup->ctx, "Trying to read from destroyed cgroup\n");
                return -1;
        }
@@ -398,7 +398,7 @@ static int pakfire_cgroup_write(struct pakfire_cgroup* cgroup,
        int r = 0;
 
        // Check if this cgroup has been destroyed already
-       if (!cgroup->fd) {
+       if (cgroup->fd < 0) {
                CTX_ERROR(cgroup->ctx, "Trying to write to destroyed cgroup\n");
                errno = EPERM;
                return 1;
@@ -583,6 +583,9 @@ int pakfire_cgroup_open(struct pakfire_cgroup** cgroup,
        // Initialize reference counter
        c->nrefs = 1;
 
+       // Initialize file descriptors
+       c->fd = c->devicesfd = -EBADF;
+
        // Find the root
        r = pakfire_cgroup_set_root(c);
        if (r)
@@ -747,9 +750,9 @@ int pakfire_cgroup_destroy(struct pakfire_cgroup* cgroup) {
                return r;
 
        // Close the file descriptor
-       if (cgroup->fd > 0) {
+       if (cgroup->fd >= 0) {
                close(cgroup->fd);
-               cgroup->fd = 0;
+               cgroup->fd = -EBADF;
        }
 
        // Open the root directory