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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
// SPDX-License-Identifier: LGPL-2.1-only
#include <libcgroup.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.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[])
{
struct cgroup_file_info info;
char root[FILENAME_MAX];
char *controller;
void *handle;
int lvl, i;
int ret;
if (argc < 2) {
fprintf(stderr, "Usage %s: <controller name>\n",
argv[0]);
exit(EXIT_FAILURE);
}
controller = argv[1];
ret = cgroup_init();
if (ret != 0) {
fprintf(stderr, "Init failed\n");
exit(EXIT_FAILURE);
}
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("Begin pre-order walk\n");
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);
printf("pre-order walk finished\n");
ret = cgroup_walk_tree_begin(controller, "/", 0, &handle, &info, &lvl);
if (ret != 0) {
fprintf(stderr, "Walk failed\n");
exit(EXIT_FAILURE);
}
ret = cgroup_walk_tree_set_flags(&handle, CGROUP_WALK_TYPE_POST_DIR);
if (ret) {
fprintf(stderr, "Walk failed with %s\n", cgroup_strerror(ret));
exit(EXIT_FAILURE);
}
strcpy(root, info.full_path);
printf("Begin post-order walk\n");
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);
printf("post order walk finished\n");
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;
}
|