From: Kamalesh Babulal Date: Fri, 8 Mar 2024 01:59:54 +0000 (+0530) Subject: api: add new API cgroup_attach_thread_tid() (v1) X-Git-Tag: v3.2.0~127 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=11d233dd59313c42d1414a28449345b0bf18477e;p=thirdparty%2Flibcgroup.git api: add new API cgroup_attach_thread_tid() (v1) Add a new API cgroup_attach_thread_tid(), that will allow the users to move all threads sharing the same tid (thread group leader). Example: --------- #include #include #include #define CGRP_NAME "cgrp_foo" int main(int argc, char **argv) { struct cgroup_controller *cgc = NULL; struct cgroup *cgroup = NULL; int ret = 0; ret = cgroup_init(); if (ret) { fprintf(stderr, "cgroup_init failed\n"); exit(1); } cgroup = cgroup_new_cgroup(CGRP_NAME); if (!cgroup) { fprintf(stderr, "Failed to allocate cgroup %s\n", CGRP_NAME); exit(1); } cgc = cgroup_add_controller(cgroup, "cpu"); if (!cgc) { fprintf(stderr, "Failed to add controller cpu cgroup %s\n", CGRP_NAME); exit(1); } ret = cgroup_create_cgroup(cgroup, 0); if (ret) fprintf(stderr, "Failed to create cgroup %s\n", CGRP_NAME); ret = cgroup_attach_thread_tid(cgroup, atoi(argv[1])); if (ret) fprintf(stderr, "failed to write threads tid %d\n", atoi(argv[1])); cgroup_free(&cgroup); exit(0); } Signed-off-by: Kamalesh Babulal Signed-off-by: Tom Hromatka --- diff --git a/include/libcgroup/tasks.h b/include/libcgroup/tasks.h index 77a85265..b504bf52 100644 --- a/include/libcgroup/tasks.h +++ b/include/libcgroup/tasks.h @@ -203,6 +203,13 @@ int cgroup_change_cgroup_uid_gid(uid_t uid, gid_t gid, pid_t pid); */ int cgroup_register_unchanged_process(pid_t pid, int flags); +/** + * Move given threads (=thread) to given control group. + * @param cgroup Destination control group. + * @param tid The task to move. + */ +int cgroup_attach_thread_tid(struct cgroup *cgroup, pid_t tid); + /** * @} * @} diff --git a/src/api.c b/src/api.c index 161cdb97..645e9a62 100644 --- a/src/api.c +++ b/src/api.c @@ -2139,6 +2139,20 @@ int cgroup_attach_task(struct cgroup *cgroup) return cgroup_attach_task_tid(cgroup, tid, 0); } +/** + * cgroup_attach_thread_tid is used to assign threads to a cgroup. + * struct cgroup *cgroup: The cgroup to assign the thread to. + * pid_t tid: The thread to be assigned to the cgroup. + * + * returns 0 on success. + * returns ECGROUPNOTOWNER if the caller does not have access to the cgroup. + * returns ECGROUPNOTALLOWED for other causes of failure. + */ +int cgroup_attach_thread_tid(struct cgroup *cgroup, pid_t tid) +{ + return cgroup_attach_task_tid(cgroup, tid, 1); +} + /** * cg_mkdir_p, emulate the mkdir -p command (recursively creating paths) * @path: path to create diff --git a/src/libcgroup.map b/src/libcgroup.map index 2e37d76e..31780656 100644 --- a/src/libcgroup.map +++ b/src/libcgroup.map @@ -162,4 +162,5 @@ CGROUP_3.0 { is_cgroup_mode_hybrid; is_cgroup_mode_unified; cgroup_is_systemd_enabled; + cgroup_attach_thread_tid; } CGROUP_2.0;