diff options
author | Dhaval Giani <dhaval@linux.vnet.ibm.com> | 2009-02-28 09:27:31 +0000 |
---|---|---|
committer | Dhaval Giani <dhaval@linux.vnet.ibm.com> | 2009-02-28 09:27:31 +0000 |
commit | bcf5c9ac11e899311ba0f3ba1b551ec62b8602cc (patch) | |
tree | 85862610bfa8b91d6c0639973a61fd46945a3ab7 /api.c | |
parent | 1b17475ed4300d050cc87334898908f444881af5 (diff) | |
download | libcg-bcf5c9ac11e899311ba0f3ba1b551ec62b8602cc.tar.gz libcg-bcf5c9ac11e899311ba0f3ba1b551ec62b8602cc.tar.xz libcg-bcf5c9ac11e899311ba0f3ba1b551ec62b8602cc.zip |
Feature: Provide new libcgroup walk tree API
From: Balbir Singh <balbir@linux.vnet.ibm.com>
Changelog v3..v4
1. Add _end() method for the iterator
Changelog v3..v2
1. Move to iterator based design
Changelog v2..v1
1. Add base path and depth semantics for walking
This patch adds the capability to walk cgroups by providing a new API
called cgroup_walk_tree. The API accepts the controller to walk and the
order in which the directories and files must be visited. The code is
implemented as an iterator, the begin function starts the walk and
we have depth control. The next function gets the following node
and returns ECGEOF when done.
libcgroup.map has been updated to reflect the same change and the prototype
is exported in libcgroup.h.
I've also added test cases (tests/walk_test.c). Sample output is show
root is /cgroup/cpu///
path , parent , relative /, full /cgroup/cpu///
path l3, parent , relative /l3, full /cgroup/cpu///l3
path ll1, parent l3, relative /l3/ll1, full /cgroup/cpu///l3/ll1
path lll1, parent ll1, relative /l3/ll1/lll1, full /cgroup/cpu///l3/ll1/lll1
path l2, parent , relative /l2, full /cgroup/cpu///l2
path ll1, parent l2, relative /l2/ll1, full /cgroup/cpu///l2/ll1
path lll1, parent ll1, relative /l2/ll1/lll1, full /cgroup/cpu///l2/ll1/lll1
path l1, parent , relative /l1, full /cgroup/cpu///l1
path ll1, parent l1, relative /l1/ll1, full /cgroup/cpu///l1/ll1
path lll1, parent ll1, relative /l1/ll1/lll1, full /cgroup/cpu///l1/ll1/lll1
path a, parent , relative /a, full /cgroup/cpu///a
path e, parent a, relative /a/e, full /cgroup/cpu///a/e
path f, parent e, relative /a/e/f, full /cgroup/cpu///a/e/f
path f, parent a, relative /a/f, full /cgroup/cpu///a/f
path x, parent a, relative /a/x, full /cgroup/cpu///a/x
path b, parent a, relative /a/b, full /cgroup/cpu///a/b
path c, parent b, relative /a/b/c, full /cgroup/cpu///a/b/c
path d, parent c, relative /a/b/c/d, full /cgroup/cpu///a/b/c/d
path default, parent , relative /default, full /cgroup/cpu///default
root is /cgroup/cpu//a/
path , parent , relative /, full /cgroup/cpu//a/
path e, parent , relative /e, full /cgroup/cpu//a/e
path f, parent e, relative /e/f, full /cgroup/cpu//a/e/f
path f, parent , relative /f, full /cgroup/cpu//a/f
path x, parent , relative /x, full /cgroup/cpu//a/x
path b, parent , relative /b, full /cgroup/cpu//a/b
path c, parent b, relative /b/c, full /cgroup/cpu//a/b/c
Walking the first 5 nodes
root is /cgroup/cpu///
path , parent , relative /, full /cgroup/cpu///
path l3, parent , relative /l3, full /cgroup/cpu///l3
path ll1, parent l3, relative /l3/ll1, full /cgroup/cpu///l3/ll1
path lll1, parent ll1, relative /l3/ll1/lll1, full /cgroup/cpu///l3/ll1/lll1
NOTE: Parent directory is represented by an empty (not NULL) string "".
The length of the string is 0.
Signed-off-by: Balbir Singh <balbir@linux.vnet.ibm.com>
Signed-off-by: Dhaval Giani <dhaval@linux.vnet.ibm.com>
git-svn-id: https://libcg.svn.sourceforge.net/svnroot/libcg/trunk@356 4f4bb910-9a46-0410-90c8-c897d4f1cd53
Diffstat (limited to 'api.c')
-rw-r--r-- | api.c | 106 |
1 files changed, 106 insertions, 0 deletions
@@ -105,6 +105,7 @@ char *cgroup_strerror_codes[] = { "Cgroup, rules file does not exist", "Cgroup mounting failed", "The config file can not be opend", + "End of File or iterator", }; static int cg_chown_file(FTS *fts, FTSENT *ent, uid_t owner, gid_t group) @@ -2242,3 +2243,108 @@ int cgroup_get_last_errno() { return last_errno; } + + +static int cg_walk_node(FTS *fts, FTSENT *ent, const int depth, + struct cgroup_file_info *info) +{ + int ret = 0; + int base_level; + + cgroup_dbg("seeing file %s\n", ent->fts_path); + + info->path = ent->fts_name; + info->parent = ent->fts_parent->fts_name; + info->full_path = ent->fts_path; + info->depth = ent->fts_level; + info->type = CGROUP_FILE_TYPE_OTHER; + + if (depth && (info->depth > depth)) + return 0; + + switch (ent->fts_info) { + case FTS_DNR: + case FTS_ERR: + errno = ent->fts_errno; + break; + case FTS_D: + info->type = CGROUP_FILE_TYPE_DIR; + break; + case FTS_DC: + case FTS_NSOK: + case FTS_NS: + case FTS_DP: + break; + case FTS_F: + info->type = CGROUP_FILE_TYPE_FILE; + break; + case FTS_DEFAULT: + break; + } + return ret; +} + +int cgroup_walk_tree_next(const int depth, void **handle, + struct cgroup_file_info *info, int base_level) +{ + int ret = 0; + FTS *fts = *(FTS **)handle; + FTSENT *ent; + + if (!handle) + return ECGINVAL; + ent = fts_read(fts); + if (!ent) + return ECGEOF; + if (!base_level && depth) + base_level = ent->fts_level + depth; + ret = cg_walk_node(fts, ent, base_level, info); + *handle = fts; + return ret; +} + +int cgroup_walk_tree_end(void **handle) +{ + int ret = 0; + FTS *fts = *(FTS **)handle; + + if (!handle) + return ECGINVAL; + fts_close(fts); + return 0; +} + +/* + * TODO: Need to decide a better place to put this function. + */ +int cgroup_walk_tree_begin(char *controller, char *base_path, const int depth, + void **handle, struct cgroup_file_info *info, + int *base_level) +{ + int ret = 0; + cgroup_dbg("path is %s\n", base_path); + char *cg_path[2]; + char full_path[FILENAME_MAX]; + FTSENT *ent; + FTS *fts; + + if (!cg_build_path(base_path, full_path, controller)) + return ECGOTHER; + + *base_level = 0; + cg_path[0] = full_path; + cg_path[1] = NULL; + + fts = fts_open(cg_path, FTS_LOGICAL | FTS_NOCHDIR | + FTS_NOSTAT, NULL); + ent = fts_read(fts); + if (!ent) { + cgroup_dbg("fts_read failed\n"); + return ECGINVAL; + } + if (!*base_level && depth) + *base_level = ent->fts_level + depth; + ret = cg_walk_node(fts, ent, *base_level, info); + *handle = fts; + return ret; +} |