From: Tom Hromatka Date: Wed, 17 May 2023 20:46:58 +0000 (-0600) Subject: api: Support NULL controllers[] in cgroup_change_cgroup_path() X-Git-Tag: v3.1.0~63 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4c86b1cd6e615b2356b72df1f13bbccb362bc3db;p=thirdparty%2Flibcgroup.git api: Support NULL controllers[] in cgroup_change_cgroup_path() cgroup_change_cgroup_path() allows the user to move a pid to a different cgroup (via the cgroup name and not a cgroup struct). In cgroup v1, it's imperative that the controller(s) are provided, so that the proper path can be built up. In cgroup v2, the list of controllers is optional, since they are not a part of the path. Add support for the controllers[] list to be NULL iff we are running in unified (cgroup v2) mode. Signed-off-by: Tom Hromatka Reviewed-by: Kamalesh Babulal --- diff --git a/src/api.c b/src/api.c index c30ac98b..1d835c4d 100644 --- a/src/api.c +++ b/src/api.c @@ -4619,9 +4619,23 @@ int cgroup_change_cgroup_path(const char *dest, pid_t pid, const char *const con } memset(&cgroup, 0, sizeof(struct cgroup)); - ret = cg_prepare_cgroup(&cgroup, pid, dest, controllers); - if (ret) - return ret; + + if (is_cgroup_mode_unified() && !controllers) { + /* + * Do not require the user to pass in an array of controller strings on + * cgroup v2 systems. The hierarchy will be the same regardless of + * whether controllers are provided or not. + */ + strncpy(cgroup.name, dest, FILENAME_MAX); + cgroup.name[FILENAME_MAX-1] = '\0'; + } else { + if (!controllers) + return ECGINVAL; + + ret = cg_prepare_cgroup(&cgroup, pid, dest, controllers); + if (ret) + return ret; + } /* Add process to cgroup */ ret = cgroup_attach_task_pid(&cgroup, pid);