]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
libcgroup: Add new API to walk tasks
authorDhaval Giani <dhaval@linux.vnet.ibm.com>
Wed, 22 Apr 2009 10:00:19 +0000 (15:30 +0530)
committerDhaval Giani <dhaval@linux.vnet.ibm.com>
Wed, 22 Apr 2009 10:16:54 +0000 (15:46 +0530)
Add a new API to iterate through the tasks file to get
the list of all the tasks in a cgroup.

Signed-off-by: Dhaval Giani <dhaval@linux.vnet.ibm.com>
Acked-by: Balbir Singh <balbir@linux.vnet.ibm.com>
Acked-by: Bharata B Rao <bharata@linux.vnet.ibm.com>
include/libcgroup.h
src/api.c
src/libcgroup.map

index efa852b99e6ec685c8bcd4e2c1e56c6b930e47ee..085c17ac9b464a9d584c1d4d00a5757dfde3c89d 100644 (file)
@@ -274,6 +274,25 @@ int cgroup_read_stats_next(void **handle, struct cgroup_stat *stat);
 
 int cgroup_read_stats_end(void **handle);
 
+/**
+ * Read the tasks file to get the list of tasks in a cgroup
+ * @cgroup: Name of the cgroup
+ * @controller: Name of the cgroup subsystem
+ * @handle: Handle to be used in the iteration
+ * @pid: The pid read from the tasks file. Will be filled in by the API
+ */
+int cgroup_get_task_begin(char *cgroup, char *controller, void **handle,
+                                                               pid_t *pid);
+
+/**
+ * Read the next task value
+ * @handle: The handle used for iterating
+ * @pid: The variable where the value will be stored
+ *
+ * return ECGEOF when the iterator finishes getting the list of tasks.
+ */
+int cgroup_get_task_next(void *handle, pid_t *pid);
+int cgroup_get_task_end(void **handle);
 /* The wrappers for filling libcg structures */
 
 struct cgroup *cgroup_new_cgroup(const char *name);
index 4d5b5246f8a23a089127a62e8f6f96aeaaf481d2..baeb856a465b14844ed68a267807387e5622c68c 100644 (file)
--- a/src/api.c
+++ b/src/api.c
@@ -2407,3 +2407,71 @@ int cgroup_read_stats_begin(char *controller, char *path, void **handle,
        *handle = fp;
        return ret;
 }
+
+int cgroup_get_task_end(void **handle)
+{
+       if (!cgroup_initialized)
+               return ECGROUPNOTINITIALIZED;
+
+       if (!*handle)
+               return ECGINVAL;
+
+       fclose((FILE *) *handle);
+       *handle = NULL;
+
+       return 0;
+}
+
+int cgroup_get_task_next(void *handle, pid_t *pid)
+{
+       int ret;
+
+       if (!cgroup_initialized)
+               return ECGROUPNOTINITIALIZED;
+
+       if (!handle)
+               return ECGINVAL;
+
+       ret = fscanf((FILE *) handle, "%u", pid);
+
+       if (ret != 1) {
+               if (ret == EOF)
+                       return ECGEOF;
+               last_errno = errno;
+               return ECGOTHER;
+       }
+
+       return 0;
+}
+
+int cgroup_get_task_begin(char *cgroup, char *controller, void **handle,
+                                                               pid_t *pid)
+{
+       int ret = 0;
+       char path[FILENAME_MAX];
+       char *fullpath = NULL;
+
+       if (!cgroup_initialized)
+               return ECGROUPNOTINITIALIZED;
+
+       if (!cg_build_path(cgroup, path, controller))
+               return ECGOTHER;
+
+       ret = asprintf(&fullpath, "%s/tasks", path);
+
+       if (ret < 0) {
+               last_errno = errno;
+               return ECGOTHER;
+       }
+
+       *handle = (void *) fopen(fullpath, "r");
+       free(fullpath);
+
+       if (!*handle) {
+               last_errno = errno;
+               return ECGOTHER;
+       }
+       ret = cgroup_get_task_next(*handle, pid);
+
+       return ret;
+}
index dd44fd7cdca3097e11870ee908cd284f3007d31b..2e2348e0a1aba2189ad2c6bb727312e3cc19012b 100644 (file)
@@ -57,3 +57,9 @@ global:
        cgroup_read_stats_end;
 } CGROUP_0.32.1;
 
+CGROUP_0.34 {
+global:
+       cgroup_get_task_begin;
+       cgroup_get_task_end;
+       cgroup_get_task_next;
+} CGROUP_0.33;