From: Kamalesh Babulal Date: Thu, 7 Mar 2024 01:58:01 +0000 (+0530) Subject: api: add helper to write into cgroup.procs (v1) X-Git-Tag: v3.2.0~128 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=97a944800b984a08e71f5da794bff94ffefee83e;p=thirdparty%2Flibcgroup.git api: add helper to write into cgroup.procs (v1) 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 Signed-off-by: Tom Hromatka --- diff --git a/src/api.c b/src/api.c index 383bbe93..161cdb97 100644 --- 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); } /**