~ubuntu-branches/ubuntu/maverick/libcgroup/maverick-proposed

« back to all changes in this revision

Viewing changes to tests/walk_test.c

  • Committer: Bazaar Package Importer
  • Author(s): Dustin Kirkland
  • Date: 2009-08-26 11:29:17 UTC
  • Revision ID: james.westby@ubuntu.com-20090826112917-402ews2uj6v350d2
Tags: upstream-0.34
ImportĀ upstreamĀ versionĀ 0.34

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <stdlib.h>
 
3
#include <sys/types.h>
 
4
#include <unistd.h>
 
5
#include <string.h>
 
6
#include <libcgroup.h>
 
7
 
 
8
void visit_node(struct cgroup_file_info *info, char *root)
 
9
{
 
10
        if (info->type == CGROUP_FILE_TYPE_DIR) {
 
11
                printf("path %s, parent %s, relative %s, full %s\n",
 
12
                        info->path, info->parent, info->full_path +
 
13
                                + strlen(root) - 1,
 
14
                                info->full_path);
 
15
        }
 
16
}
 
17
 
 
18
int main(int argc, char *argv[])
 
19
{
 
20
        int ret;
 
21
        char *controller;
 
22
        void *handle;
 
23
        struct cgroup_file_info info;
 
24
        char root[FILENAME_MAX];
 
25
        int lvl, i;
 
26
 
 
27
        if (argc < 2) {
 
28
                fprintf(stderr, "Usage %s: <controller name>\n",
 
29
                        argv[0]);
 
30
                exit(EXIT_FAILURE);
 
31
        }
 
32
 
 
33
        controller = argv[1];
 
34
 
 
35
        cgroup_init();
 
36
 
 
37
        ret = cgroup_walk_tree_begin(controller, "/", 0, &handle, &info, &lvl);
 
38
 
 
39
        if (ret != 0) {
 
40
                fprintf(stderr, "Walk failed\n");
 
41
                exit(EXIT_FAILURE);
 
42
        }
 
43
        strcpy(root, info.full_path);
 
44
        printf("Begin pre-order walk\n");
 
45
        printf("root is %s\n", root);
 
46
        visit_node(&info, root);
 
47
        while ((ret = cgroup_walk_tree_next(0, &handle, &info, lvl)) !=
 
48
                        ECGEOF) {
 
49
                visit_node(&info, root);
 
50
        }
 
51
        cgroup_walk_tree_end(&handle);
 
52
 
 
53
        printf("pre-order walk finished\n");
 
54
        ret = cgroup_walk_tree_begin(controller, "/", 0, &handle, &info, &lvl);
 
55
 
 
56
        if (ret != 0) {
 
57
                fprintf(stderr, "Walk failed\n");
 
58
                exit(EXIT_FAILURE);
 
59
        }
 
60
 
 
61
        ret = cgroup_walk_tree_set_flags(&handle, CGROUP_WALK_TYPE_POST_DIR);
 
62
 
 
63
        if (ret) {
 
64
                fprintf(stderr, "Walk failed with %s\n", cgroup_strerror(ret));
 
65
                exit(EXIT_FAILURE);
 
66
        }
 
67
 
 
68
        strcpy(root, info.full_path);
 
69
        printf("Begin post-order walk\n");
 
70
        printf("root is %s\n", root);
 
71
        visit_node(&info, root);
 
72
        while ((ret = cgroup_walk_tree_next(0, &handle, &info, lvl)) !=
 
73
                        ECGEOF) {
 
74
                visit_node(&info, root);
 
75
        }
 
76
        cgroup_walk_tree_end(&handle);
 
77
        printf("post order walk finished\n");
 
78
 
 
79
        ret = cgroup_walk_tree_begin(controller, "/a", 2, &handle, &info, &lvl);
 
80
 
 
81
        if (ret != 0) {
 
82
                fprintf(stderr, "Walk failed\n");
 
83
                exit(EXIT_FAILURE);
 
84
        }
 
85
        strcpy(root, info.full_path);
 
86
        printf("root is %s\n", root);
 
87
        visit_node(&info, root);
 
88
        while ((ret = cgroup_walk_tree_next(2, &handle, &info, lvl)) !=
 
89
                        ECGEOF) {
 
90
                visit_node(&info, root);
 
91
        }
 
92
        cgroup_walk_tree_end(&handle);
 
93
 
 
94
        /*
 
95
         * Walk only the first five nodes
 
96
         */
 
97
        i = 0;
 
98
        printf("Walking the first 5 nodes\n");
 
99
        ret = cgroup_walk_tree_begin(controller, "/", 0, &handle, &info, &lvl);
 
100
 
 
101
        if (ret != 0) {
 
102
                fprintf(stderr, "Walk failed\n");
 
103
                exit(EXIT_FAILURE);
 
104
        }
 
105
        strcpy(root, info.full_path);
 
106
        printf("root is %s\n", root);
 
107
        visit_node(&info, root);
 
108
        i++;
 
109
        while ((ret = cgroup_walk_tree_next(0, &handle, &info, lvl)) !=
 
110
                        ECGEOF) {
 
111
                visit_node(&info, root);
 
112
                if (++i >= 5)
 
113
                        break;
 
114
        }
 
115
        cgroup_walk_tree_end(&handle);
 
116
        return EXIT_SUCCESS;
 
117
}