]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
api: add new API cgroup_attach_thread_tid() (v1)
authorKamalesh Babulal <kamalesh.babulal@oracle.com>
Fri, 8 Mar 2024 01:59:54 +0000 (07:29 +0530)
committerTom Hromatka <tom.hromatka@oracle.com>
Wed, 20 Mar 2024 20:40:45 +0000 (14:40 -0600)
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 <libcgroup.h>
 #include <stdio.h>
 #include <stdlib.h>

 #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 <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
include/libcgroup/tasks.h
src/api.c
src/libcgroup.map

index 77a85265f4aaa68db26f32b79f4e4c54fd5c1e43..b504bf523d0b06d8d22accaa1939e58894ba4a05 100644 (file)
@@ -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);
+
 /**
  * @}
  * @}
index 161cdb97b81e999d8d481ccb7a04b34819f001bc..645e9a622066655900e12a2964858f2e2840fc24 100644 (file)
--- 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
index 2e37d76e1185f5870beb954155b07620ec58340e..317806568dec86393cca96562ed877c3fb6beb21 100644 (file)
@@ -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;