diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile | 6 | ||||
-rw-r--r-- | tests/walk_test.c | 90 |
2 files changed, 95 insertions, 1 deletions
diff --git a/tests/Makefile b/tests/Makefile index 00bfe3d..9ebadac 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -8,7 +8,8 @@ TARGET= libtest_functions.a \ libcgrouptest01 \ libcg_ba \ setuid \ - pathtest + pathtest \ + walk_test all: $(TARGET) @@ -30,5 +31,8 @@ setuid: setuid.c pathtest: pathtest.c $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) $(LIBS) +walk_test: walk_test.c + $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) $(LIBS) + clean: \rm -f $(TARGET) test_functions.o diff --git a/tests/walk_test.c b/tests/walk_test.c new file mode 100644 index 0000000..2e16ecd --- /dev/null +++ b/tests/walk_test.c @@ -0,0 +1,90 @@ +#include <stdio.h> +#include <stdlib.h> +#include <sys/types.h> +#include <unistd.h> +#include <string.h> +#include <libcgroup.h> + +void visit_node(struct cgroup_file_info *info, char *root) +{ + if (info->type == CGROUP_FILE_TYPE_DIR) { + printf("path %s, parent %s, relative %s, full %s\n", + info->path, info->parent, info->full_path + + + strlen(root) - 1, + info->full_path); + } +} + +int main(int argc, char *argv[]) +{ + int ret; + char *controller; + void *handle; + struct cgroup_file_info info; + char root[FILENAME_MAX]; + int lvl, i; + + if (argc < 2) { + fprintf(stderr, "Usage %s: <controller name>\n", + argv[0]); + exit(EXIT_FAILURE); + } + + controller = argv[1]; + + cgroup_init(); + + ret = cgroup_walk_tree_begin(controller, "/", 0, &handle, &info, &lvl); + + if (ret != 0) { + fprintf(stderr, "Walk failed\n"); + exit(EXIT_FAILURE); + } + strcpy(root, info.full_path); + printf("root is %s\n", root); + visit_node(&info, root); + while ((ret = cgroup_walk_tree_next(0, &handle, &info, lvl)) != + ECGEOF) { + visit_node(&info, root); + } + cgroup_walk_tree_end(&handle); + + ret = cgroup_walk_tree_begin(controller, "/a", 2, &handle, &info, &lvl); + + if (ret != 0) { + fprintf(stderr, "Walk failed\n"); + exit(EXIT_FAILURE); + } + strcpy(root, info.full_path); + printf("root is %s\n", root); + visit_node(&info, root); + while ((ret = cgroup_walk_tree_next(2, &handle, &info, lvl)) != + ECGEOF) { + visit_node(&info, root); + } + cgroup_walk_tree_end(&handle); + + /* + * Walk only the first five nodes + */ + i = 0; + printf("Walking the first 5 nodes\n"); + ret = cgroup_walk_tree_begin(controller, "/", 0, &handle, &info, &lvl); + + if (ret != 0) { + fprintf(stderr, "Walk failed\n"); + exit(EXIT_FAILURE); + } + strcpy(root, info.full_path); + printf("root is %s\n", root); + visit_node(&info, root); + i++; + while ((ret = cgroup_walk_tree_next(0, &handle, &info, lvl)) != + ECGEOF) { + visit_node(&info, root); + if (++i >= 5) + break; + } + cgroup_walk_tree_end(&handle); + return EXIT_SUCCESS; +} |