free(cgroup);
}
+static int pakfire_cgroup_open_root(struct pakfire_cgroup* cgroup) {
+ int fd = open(ROOT, O_DIRECTORY|O_PATH|O_CLOEXEC);
+ if (fd < 0) {
+ ERROR(cgroup->pakfire, "Could not open %s: %m\n", ROOT);
+ return -1;
+ }
+
+ return fd;
+}
+
static int __pakfire_cgroup_create(struct pakfire_cgroup* cgroup) {
char path[PATH_MAX];
int r;
This function returns a negative value on error.
*/
static int __pakfire_cgroup_open(struct pakfire_cgroup* cgroup) {
- int rootfd = -1;
int fd = -1;
int r;
// Open file descriptor of the cgroup root
- rootfd = open(ROOT, O_DIRECTORY|O_PATH|O_CLOEXEC);
- if (rootfd < 0) {
- ERROR(cgroup->pakfire, "Could not open %s: %m\n", ROOT);
+ int rootfd = pakfire_cgroup_open_root(cgroup);
+ if (rootfd < 0)
return -1;
- }
// Return the rootfd for the root group
if (pakfire_cgroup_is_root(cgroup))
int pakfire_cgroup_destroy(struct pakfire_cgroup* cgroup) {
int r;
+ // Cannot call this for the root group
+ if (pakfire_cgroup_is_root(cgroup)) {
+ errno = EPERM;
+ return 1;
+ }
+
+ DEBUG(cgroup->pakfire, "Destroying cgroup %s\n", pakfire_cgroup_name(cgroup));
+
// Kill everything in this group
r = pakfire_cgroup_killall(cgroup);
if (r)
return r;
- // Delete the directory
- r = rmdir(cgroup->path);
- if (r) {
- ERROR(cgroup->pakfire, "Could not destroy cgroup: %m\n");
- return r;
- }
-
// Close the file descriptor
if (cgroup->fd) {
close(cgroup->fd);
cgroup->fd = 0;
}
- return 0;
+ // Open the root directory
+ int fd = pakfire_cgroup_open_root(cgroup);
+ if (fd < 0)
+ return 1;
+
+ // Delete the directory
+ r = unlinkat(fd, cgroup->path, AT_REMOVEDIR);
+ if (r)
+ ERROR(cgroup->pakfire, "Could not destroy cgroup: %m\n");
+
+ // Close fd
+ close(fd);
+
+ return r;
}
int pakfire_cgroup_fd(struct pakfire_cgroup* cgroup) {