summaryrefslogtreecommitdiffstats
path: root/tests/test_parent.c
blob: 3b16439169b4bf5850939669f2e263e8d017070e (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
#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;
}