]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
api: add helper to write into cgroup.procs (v1)
authorKamalesh Babulal <kamalesh.babulal@oracle.com>
Thu, 7 Mar 2024 01:58:01 +0000 (07:28 +0530)
committerTom Hromatka <tom.hromatka@oracle.com>
Wed, 20 Mar 2024 20:40:45 +0000 (14:40 -0600)
Add helper cgroup_v1_build_cgroup_procs_path(), that constructs the path
to write tid into cgroup.procs, that will allow to move all of the tasks
sharing the same pid (thread leader). This helper, will be called by
cgroup_attach_task_tid() based on the move_threads function argument.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
src/api.c

index 383bbe931536c74b955d27fb6e99e98452f67fd7..161cdb97b81e999d8d481ccb7a04b34819f001bc 100644 (file)
--- a/src/api.c
+++ b/src/api.c
@@ -2012,10 +2012,33 @@ err:
        return ret;
 }
 
-static int cgroup_attach_task_tid(struct cgroup *cgroup, pid_t tid)
+static int cgroup_v1_build_procs_path(const char * const ctrl_name, char *path)
+{
+       enum cg_version_t version;
+       size_t len;
+       int ret;
+
+       ret = cgroup_get_controller_version(ctrl_name, &version);
+       if (ret)
+               return ret;
+
+       if (version != CGROUP_V1)
+               return ret;
+
+       /* replace tasks with cgroup.procs file name in the path */
+       len = strlen(path) - 5;
+       path[len] = '\0';
+
+       strncat(path, "cgroup.procs", FILENAME_MAX - (len + 1));
+       path[FILENAME_MAX - 1] = '\0';
+
+       return ret;
+}
+
+static int cgroup_attach_task_tid(struct cgroup *cgroup, pid_t tid, bool move_tids)
 {
        char path[FILENAME_MAX] = {0};
-       char *controller_name;
+       char *controller_name = NULL;
        int empty_cgroup = 0;
        int i, ret = 0;
 
@@ -2033,6 +2056,12 @@ static int cgroup_attach_task_tid(struct cgroup *cgroup, pid_t tid)
                        if (ret)
                                return ret;
 
+                       if (move_tids) {
+                               ret = cgroup_v1_build_procs_path(controller_name, path);
+                               if (ret)
+                                       return ret;
+                       }
+
                        ret = __cgroup_attach_task_pid(path, tid);
                        if (ret) {
                                pthread_rwlock_unlock(&cg_mount_table_lock);
@@ -2069,6 +2098,12 @@ static int cgroup_attach_task_tid(struct cgroup *cgroup, pid_t tid)
                        if (ret)
                                return ret;
 
+                       if (move_tids) {
+                               ret = cgroup_v1_build_procs_path(controller_name, path);
+                               if (ret)
+                                       return ret;
+                       }
+
                        ret = __cgroup_attach_task_pid(path, tid);
                        if (ret)
                                return ret;
@@ -2088,7 +2123,7 @@ static int cgroup_attach_task_tid(struct cgroup *cgroup, pid_t tid)
  */
 int cgroup_attach_task_pid(struct cgroup *cgroup, pid_t tid)
 {
-       return cgroup_attach_task_tid(cgroup, tid);
+       return cgroup_attach_task_tid(cgroup, tid, 0);
 }
 
 /**
@@ -2101,7 +2136,7 @@ int cgroup_attach_task(struct cgroup *cgroup)
 {
        pid_t tid = cg_gettid();
 
-       return cgroup_attach_task_tid(cgroup, tid);
+       return cgroup_attach_task_tid(cgroup, tid, 0);
 }
 
 /**