From: Michael Tremer Date: Sat, 12 Oct 2024 16:36:57 +0000 (+0000) Subject: cgroup: Initialize file descriptors X-Git-Tag: 0.9.30~1072 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ddb7baa57efe09c85feca678e34b2bc03f6457f9;p=pakfire.git cgroup: Initialize file descriptors It was possible that those closed the standard input. Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/cgroup.c b/src/libpakfire/cgroup.c index 60284f72e..2f8c6f015 100644 --- a/src/libpakfire/cgroup.c +++ b/src/libpakfire/cgroup.c @@ -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