#include <pakfire/string.h>
#include <pakfire/util.h>
-#define ROOT "/sys/fs/cgroup"
#define BUFFER_SIZE 64 * 1024
enum pakfire_cgroup_controllers {
struct pakfire* pakfire;
int nrefs;
+ // Store the root path
+ char root[PATH_MAX];
+
// Flags
int flags;
return cgroup->flags & flag;
}
+static int pakfire_cgroup_set_root(struct pakfire_cgroup* cgroup) {
+ int r;
+
+ // Find the current UID
+ const uid_t uid = getuid();
+
+ switch (uid) {
+ // root
+ case 0:
+ r = pakfire_string_set(cgroup->root, "/sys/fs/cgroup");
+
+ // unprivileged users
+ default:
+ r = pakfire_string_format(cgroup->root,
+ "/sys/fs/cgroup/user.slice/user-%d.slice/user@%d.service", uid, uid);
+ }
+
+ if (r)
+ ERROR(cgroup->pakfire, "Could not determine cgroup root: %m\n");
+
+ return r;
+}
+
static const char* pakfire_cgroup_name(struct pakfire_cgroup* cgroup) {
if (pakfire_cgroup_is_root(cgroup))
return "(root)";
}
static int pakfire_cgroup_open_root(struct pakfire_cgroup* cgroup) {
- int fd = open(ROOT, O_DIRECTORY|O_PATH|O_CLOEXEC);
+ int fd = open(cgroup->root, O_DIRECTORY|O_PATH|O_CLOEXEC);
if (fd < 0) {
- ERROR(cgroup->pakfire, "Could not open %s: %m\n", ROOT);
+ ERROR(cgroup->pakfire, "Could not open %s: %m\n", cgroup->root);
return -1;
}
DEBUG(cgroup->pakfire, "Trying to create cgroup %s\n", pakfire_cgroup_name(cgroup));
// Compose the absolute path
- r = pakfire_path_join(path, ROOT, cgroup->path);
+ r = pakfire_path_join(path, cgroup->root, cgroup->path);
if (r)
return 1;
// Initialize reference counter
c->nrefs = 1;
+ // Find the root
+ r = pakfire_cgroup_set_root(c);
+ if (r)
+ goto ERROR;
+
// Copy path
pakfire_string_set(c->path, path);