Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cgroup04.c « static « zdtm « test - github.com/checkpoint-restore/criu.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8c40ffd6bd5284c441988c66758657712bb9ed4f (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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167

#include <unistd.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <limits.h>
#include "zdtmtst.h"

#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))

const char *test_doc = "Check that some cgroups properties in kernel controllers are preserved";
const char *test_author = "Tycho Andersen <tycho.andersen@canonical.com>";

char *dirname;
TEST_OPTION(dirname, string, "cgroup directory name", 1);
static const char *cgname = "zdtmtst";

int mount_and_add(const char *controller, const char *path, const char *prop, const char *value)
{
	char aux[1024], paux[1024], subdir[1024];

	if (mkdir(dirname, 0700) < 0 && errno != EEXIST) {
		pr_perror("Can't make dir");
		return -1;
	}

	sprintf(subdir, "%s/%s", dirname, controller);
	if (mkdir(subdir, 0700) < 0) {
		pr_perror("Can't make dir");
		return -1;
	}

	if (mount("none", subdir, "cgroup", 0, controller)) {
		pr_perror("Can't mount cgroups");
		goto err_rd;
	}

	ssprintf(paux, "%s/%s", subdir, path);
	mkdir(paux, 0600);

	ssprintf(paux, "%s/%s/%s", subdir, path, prop);
	if (write_value(paux, value) < 0)
		goto err_rs;

	sprintf(aux, "%d", getpid());
	ssprintf(paux, "%s/%s/tasks", subdir, path);
	if (write_value(paux, aux) < 0)
		goto err_rs;

	ssprintf(paux, "%s/%s/special_prop_check", subdir, path);
	mkdir(paux, 0600);

	return 0;
err_rs:
	umount(dirname);
err_rd:
	rmdir(dirname);
	return -1;
}

bool checkval(char *path, char *val)
{
	char buf[1024];
	int fd, n;

	fd = open(path, O_RDONLY);
	if (fd < 0) {
		pr_perror("open %s", path);
		return false;
	}

	n = read(fd, buf, sizeof(buf) - 1);
	close(fd);
	if (n < 0) {
		pr_perror("read");
		return false;
	}
	buf[n] = 0;

	if (strcmp(val, buf)) {
		pr_err("got %s expected %s\n", buf, val);
		return false;
	}

	return true;
}

int main(int argc, char **argv)
{
	int ret = -1, i;
	char buf[1024], path[PATH_MAX];
	struct stat sb;

	char *dev_allow[] = {
		"c *:* m",   "b *:* m",	  "c 1:3 rwm", "c 1:5 rwm",   "c 1:7 rwm",    "c 5:0 rwm",
		"c 5:2 rwm", "c 1:8 rwm", "c 1:9 rwm", "c 136:* rwm", "c 10:229 rwm",
	};

	test_init(argc, argv);

	if (mount_and_add("devices", cgname, "devices.deny", "a") < 0)
		goto out;

	/* need to allow /dev/null for restore */
	sprintf(path, "%s/devices/%s/devices.allow", dirname, cgname);
	for (i = 0; i < ARRAY_SIZE(dev_allow); i++) {
		if (write_value(path, dev_allow[i]) < 0)
			goto out;
	}

	if (mount_and_add("memory", cgname, "memory.limit_in_bytes", "268435456") < 0)
		goto out;

	test_daemon();
	test_waitsig();

	buf[0] = 0;
	for (i = 0; i < ARRAY_SIZE(dev_allow); i++) {
		strcat(buf, dev_allow[i]);
		strcat(buf, "\n");
	}

	sprintf(path, "%s/devices/%s/devices.list", dirname, cgname);
	if (!checkval(path, buf)) {
		fail();
		goto out;
	}

	sprintf(path, "%s/memory/%s/memory.limit_in_bytes", dirname, cgname);
	if (!checkval(path, "268435456\n")) {
		fail();
		goto out;
	}

	sprintf(path, "%s/devices/%s/special_prop_check", dirname, cgname);
	if (stat(path, &sb) < 0) {
		fail("special_prop_check doesn't exist?");
		goto out;
	}

	if (!S_ISDIR(sb.st_mode)) {
		fail("special_prop_check not a directory?");
		goto out;
	}

	pass();
	ret = 0;
out:
	sprintf(path, "%s/devices/%s/special_prop_check", dirname, cgname);
	rmdir(path);

	sprintf(path, "%s/devices/%s", dirname, cgname);
	rmdir(path);
	sprintf(path, "%s/devices", dirname);
	umount(path);

	sprintf(path, "%s/memory/%s", dirname, cgname);
	rmdir(path);
	sprintf(path, "%s/memory", dirname);
	umount(path);

	return ret;
}