diff options
author | Dhaval Giani <dhaval@linux.vnet.ibm.com> | 2009-04-22 15:30:19 +0530 |
---|---|---|
committer | Dhaval Giani <dhaval@linux.vnet.ibm.com> | 2009-04-22 15:46:54 +0530 |
commit | 9414f3be0c3f191fdea212ecc3157af5e70a45de (patch) | |
tree | efff88098ef8f5753cc6573d014d18cce1ac00fc /src/api.c | |
parent | 8d8a747b161508ebfd48a6dac52f1438e2714a2b (diff) | |
download | libcg-9414f3be0c3f191fdea212ecc3157af5e70a45de.tar.gz libcg-9414f3be0c3f191fdea212ecc3157af5e70a45de.tar.xz libcg-9414f3be0c3f191fdea212ecc3157af5e70a45de.zip |
libcgroup: Add new API to walk tasks
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>
Diffstat (limited to 'src/api.c')
-rw-r--r-- | src/api.c | 68 |
1 files changed, 68 insertions, 0 deletions
@@ -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; +} |