cgroupv2_subtree_control_recursive() writes to subtree_control
files in the cgroup tree, starting at the root cgroup. As the
root cgroup is managed by SystemD, its control files belong to the
root user.
This new check ensures that we only attempt to write to subtree_control
if the desired controller is not already active (e.g., because we're
using SystemD Delegation mechanisms), thus better enabling cgroup
manipulations for non-root users.
Signed-off-by: Adriaan Schmidt <adriaan.schmidt@siemens.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
STATIC int cgroupv2_subtree_control_recursive(char *path, const char *ctrl_name, bool enable)
{
char *path_copy, *tmp_path, *stok_buff = NULL;
- bool found_mount = false;
+ bool found_mount = false, controller_enabled = false;
size_t mount_len;
int i, error = 0;
if (error)
goto out;
+ error = cgroupv2_get_subtree_control(path_copy, ctrl_name, &controller_enabled);
+ if (controller_enabled)
+ continue;
+ if (error != ECGROUPNOTMOUNTED)
+ goto out;
+
error = cgroupv2_subtree_control(path_copy, ctrl_name, enable);
if (error)
goto out;