]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
api: Support NULL controllers[] in cgroup_change_cgroup_path()
authorTom Hromatka <tom.hromatka@oracle.com>
Wed, 17 May 2023 20:46:58 +0000 (14:46 -0600)
committerTom Hromatka <tom.hromatka@oracle.com>
Tue, 23 May 2023 21:43:05 +0000 (15:43 -0600)
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 <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
src/api.c

index c30ac98b95f453712eee9522dc3b70fc31fe000c..1d835c4d9b1b3fa51721af65bf11977a44e746bb 100644 (file)
--- 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);