diff options
author | Jan Safranek <jsafrane@redhat.com> | 2009-07-28 16:08:39 +0200 |
---|---|---|
committer | Jan Safranek <jsafrane@redhat.com> | 2009-07-28 16:08:39 +0200 |
commit | 9b269475dbc9d9a7527c7ff512bf2a5a0aa493a4 (patch) | |
tree | eee5965cedac513540260b651c1d2d177f2bb6ed | |
parent | 4b198940a8d38da33581ea2eef7c8b05ee103257 (diff) | |
download | libcg-9b269475dbc9d9a7527c7ff512bf2a5a0aa493a4.tar.gz libcg-9b269475dbc9d9a7527c7ff512bf2a5a0aa493a4.tar.xz libcg-9b269475dbc9d9a7527c7ff512bf2a5a0aa493a4.zip |
Export cgroup_find_parent and add testing code for it
This patch does not necessarily need to be accepted, it just provides testing
facility for new cgroup_find_parent.
Especially note the ugly dependence on ../src/tools/tools-common.c :)
Signed-off-by: Jan Safranek <jsafrane@redhat.com>
-rw-r--r-- | include/libcgroup.h | 12 | ||||
-rw-r--r-- | src/libcgroup.map | 1 | ||||
-rw-r--r-- | tests/Makefile.am | 3 | ||||
-rw-r--r-- | tests/test_parent.c | 81 |
4 files changed, 96 insertions, 1 deletions
diff --git a/include/libcgroup.h b/include/libcgroup.h index 3d5660c..0942161 100644 --- a/include/libcgroup.h +++ b/include/libcgroup.h @@ -364,6 +364,18 @@ int cgroup_get_controller_end(void **handle); */ int cgroup_get_subsys_mount_point(char *controller, char **mount_point); +/** + * Find the parent of the specified directory. It returns the parent (the + * parent is usually name/.. unless name is a mount point. + * + * @param cgroup The cgroup. + * @param parent Output, name of parent's group (if the group has parent) or + * NULL, if the provided cgroup is the root group and has no parent. + * Caller is responsible for freeing the returned string! + * @return 0 on success, >0 on error. + */ +int cgroup_find_parent(struct cgroup *cgroup, char **parent); + /* The wrappers for filling libcg structures */ struct cgroup *cgroup_new_cgroup(const char *name); diff --git a/src/libcgroup.map b/src/libcgroup.map index aa9d0d2..e66c332 100644 --- a/src/libcgroup.map +++ b/src/libcgroup.map @@ -73,4 +73,5 @@ global: cgroup_get_procname_from_procfs; cgroup_register_unchanged_process; cgroup_change_cgroup_flags; + cgroup_find_parent; } CGROUP_0.33; diff --git a/tests/Makefile.am b/tests/Makefile.am index 14bcdf3..0157a54 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -2,7 +2,7 @@ INCLUDES = -I$(top_srcdir)/include LDADD = $(top_srcdir)/src/.libs/libcgroup.la # compile the tests, but do not install them -noinst_PROGRAMS = libcgrouptest01 libcg_ba setuid pathtest walk_test read_stats walk_task get_controller get_mount_point proctest +noinst_PROGRAMS = libcgrouptest01 libcg_ba setuid pathtest walk_test read_stats walk_task get_controller get_mount_point proctest test_parent libcgrouptest01_SOURCES=libcgrouptest01.c test_functions.c libcgrouptest.h libcg_ba_SOURCES=libcg_ba.cpp @@ -14,6 +14,7 @@ walk_task_SOURCES=walk_task.c get_controller_SOURCES=get_controller.c get_mount_point_SOURCES=get_mount_point.c proctest_SOURCES=proctest.c +test_parent_SOURCES=test_parent.c ../src/tools/tools-common.c ../src/tools/tools-common.h EXTRA_DIST = pathtest.sh runlibcgrouptest.sh diff --git a/tests/test_parent.c b/tests/test_parent.c new file mode 100644 index 0000000..3b16439 --- /dev/null +++ b/tests/test_parent.c @@ -0,0 +1,81 @@ +#include <stdio.h> +#include <libcgroup.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> + +/* Ugly, I know... */ +#include "../src/tools/tools-common.h" + +struct cgroup_group_spec *cgroup_list[CG_HIER_MAX]; + +int main(int argc, char **argv) +{ + int ret; + int i, j; + char *parent; + char controllers[PATH_MAX]; + struct cgroup *cgroup; + struct cgroup_controller *cgc; + + ret = cgroup_init(); + if (ret) { + fprintf(stderr, "cgroup_init failed with %s\n", + cgroup_strerror(ret)); + exit(1); + } + + if (argc < 2) { + fprintf(stderr, "at least one argument is expected\n"); + printf("Usage: %s <controllers>:<group> ... \n", + argv[0]); + exit(1); + } + + for (i = 1; i < argc; i++) { + ret = parse_cgroup_spec(cgroup_list, argv[i]); + if (ret) { + fprintf(stderr, "cannot parse %s\n", argv[i]); + exit(1); + } + } + + for (i = 0; i < CG_HIER_MAX; i++) { + if (!cgroup_list[i]) + break; + + cgroup = cgroup_new_cgroup(cgroup_list[i]->path); + if (cgroup == NULL) { + fprintf(stderr, "cannot create new cgroup for %s\n", + argv[i]); + exit(1); + } + + memset(controllers, 0, sizeof(controllers)); + for (j = 0; j < CG_CONTROLLER_MAX; j++) { + if (cgroup_list[i]->controllers[j] == NULL) + break; + strncat(controllers, cgroup_list[i]->controllers[j], + sizeof(controllers) - + strlen(controllers)); + strncat(controllers, ",", sizeof(controllers) - + strlen(controllers)); + cgc = cgroup_add_controller(cgroup, + cgroup_list[i]->controllers[j]); + if (cgc == NULL) { + fprintf(stderr, "cannot add %s to %s\n", + cgroup_list[i]->path, + cgroup_list[i]->controllers[j]); + exit(1); + } + } + ret = cgroup_find_parent(cgroup, &parent); + printf("Parent of %s:%s is: %s (%d)\n", controllers, + cgroup_list[i]->path, parent, ret); + + free(parent); + cgroup_free_group_spec(cgroup_list[i]); + cgroup_free(&cgroup); + } + return 0; +} |