summaryrefslogtreecommitdiffstats
path: root/tests/walk_test.c
blob: 2e16ecd37f8333e81bc365c7b6556379627a7b4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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;
}