]>
Commit | Line | Data |
---|---|---|
6c03089c LP |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2013 Zbigniew Jędrzejewski-Szmek | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
96cde13a ZJS |
22 | #include <assert.h> |
23 | ||
24 | #include "util.h" | |
25 | #include "cgroup-util.h" | |
143bfdaf | 26 | #include "test-helper.h" |
96cde13a | 27 | |
7027ff61 | 28 | static void check_p_d_u(const char *path, int code, const char *result) { |
6c03089c | 29 | _cleanup_free_ char *unit = NULL; |
96cde13a | 30 | |
7027ff61 | 31 | assert_se(cg_path_decode_unit(path, &unit) == code); |
6c03089c LP |
32 | assert_se(streq_ptr(unit, result)); |
33 | } | |
96cde13a | 34 | |
7027ff61 | 35 | static void test_path_decode_unit(void) { |
d7bd3de0 LP |
36 | check_p_d_u("getty@tty2.service", 0, "getty@tty2.service"); |
37 | check_p_d_u("getty@tty2.service/", 0, "getty@tty2.service"); | |
38 | check_p_d_u("getty@tty2.service/xxx", 0, "getty@tty2.service"); | |
7027ff61 LP |
39 | check_p_d_u("getty@.service/", -EINVAL, NULL); |
40 | check_p_d_u("getty@.service", -EINVAL, NULL); | |
41 | check_p_d_u("getty.service", 0, "getty.service"); | |
42 | check_p_d_u("getty", -EINVAL, NULL); | |
d7bd3de0 LP |
43 | check_p_d_u("getty/waldo", -EINVAL, NULL); |
44 | check_p_d_u("_cpu.service", 0, "cpu.service"); | |
6c03089c LP |
45 | } |
46 | ||
47 | static void check_p_g_u(const char *path, int code, const char *result) { | |
48 | _cleanup_free_ char *unit = NULL; | |
49 | ||
50 | assert_se(cg_path_get_unit(path, &unit) == code); | |
51 | assert_se(streq_ptr(unit, result)); | |
52 | } | |
53 | ||
9444b1f2 LP |
54 | static void test_path_get_unit(void) { |
55 | check_p_g_u("/system.slice/foobar.service/sdfdsaf", 0, "foobar.service"); | |
d7bd3de0 LP |
56 | check_p_g_u("/system.slice/getty@tty5.service", 0, "getty@tty5.service"); |
57 | check_p_g_u("/system.slice/getty@tty5.service/aaa/bbb", 0, "getty@tty5.service"); | |
58 | check_p_g_u("/system.slice/getty@tty5.service/", 0, "getty@tty5.service"); | |
9444b1f2 LP |
59 | check_p_g_u("/system.slice/getty@tty6.service/tty5", 0, "getty@tty6.service"); |
60 | check_p_g_u("sadfdsafsda", -EINVAL, NULL); | |
d7bd3de0 | 61 | check_p_g_u("/system.slice/getty####@tty6.service/xxx", -EINVAL, NULL); |
9444b1f2 | 62 | check_p_g_u("/system.slice/system-waldo.slice/foobar.service/sdfdsaf", 0, "foobar.service"); |
d7bd3de0 | 63 | check_p_g_u("/system.slice/system-waldo.slice/_cpu.service/sdfdsaf", 0, "cpu.service"); |
9444b1f2 LP |
64 | } |
65 | ||
6c03089c LP |
66 | static void check_p_g_u_u(const char *path, int code, const char *result) { |
67 | _cleanup_free_ char *unit = NULL; | |
68 | ||
69 | assert_se(cg_path_get_user_unit(path, &unit) == code); | |
70 | assert_se(streq_ptr(unit, result)); | |
71 | } | |
72 | ||
9444b1f2 | 73 | static void test_path_get_user_unit(void) { |
374ec6ab LP |
74 | check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, "foobar.service"); |
75 | check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo.slice/foobar.service", 0, "foobar.service"); | |
76 | check_p_g_u_u("/user.slice/user-1002.slice/session-2.scope/foobar.service/waldo", 0, "foobar.service"); | |
77 | check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar.service/waldo/uuuux", 0, "foobar.service"); | |
78 | check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo/waldo/uuuux", -EINVAL, NULL); | |
d7bd3de0 LP |
79 | check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service"); |
80 | check_p_g_u_u("/session-2.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service"); | |
81 | check_p_g_u_u("/xyz.slice/xyz-waldo.slice/session-77.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service"); | |
9444b1f2 | 82 | check_p_g_u_u("/meh.service", -ENOENT, NULL); |
d7bd3de0 | 83 | check_p_g_u_u("/session-3.scope/_cpu.service", 0, "cpu.service"); |
7027ff61 | 84 | } |
6c03089c | 85 | |
9444b1f2 LP |
86 | static void check_p_g_s(const char *path, int code, const char *result) { |
87 | _cleanup_free_ char *s = NULL; | |
88 | ||
89 | assert_se(cg_path_get_session(path, &s) == code); | |
90 | assert_se(streq_ptr(s, result)); | |
96cde13a ZJS |
91 | } |
92 | ||
9444b1f2 | 93 | static void test_path_get_session(void) { |
374ec6ab LP |
94 | check_p_g_s("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, "2"); |
95 | check_p_g_s("/session-3.scope", 0, "3"); | |
9444b1f2 LP |
96 | check_p_g_s("", -ENOENT, 0); |
97 | } | |
7027ff61 | 98 | |
9444b1f2 LP |
99 | static void check_p_g_o_u(const char *path, int code, uid_t result) { |
100 | uid_t uid = 0; | |
7027ff61 | 101 | |
9444b1f2 LP |
102 | assert_se(cg_path_get_owner_uid(path, &uid) == code); |
103 | assert_se(uid == result); | |
104 | } | |
105 | ||
106 | static void test_path_get_owner_uid(void) { | |
374ec6ab LP |
107 | check_p_g_o_u("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, 1000); |
108 | check_p_g_o_u("/user.slice/user-1006.slice", 0, 1006); | |
9444b1f2 LP |
109 | check_p_g_o_u("", -ENOENT, 0); |
110 | } | |
7027ff61 | 111 | |
9444b1f2 LP |
112 | static void check_p_g_m_n(const char *path, int code, const char *result) { |
113 | _cleanup_free_ char *m = NULL; | |
7027ff61 | 114 | |
9444b1f2 LP |
115 | assert_se(cg_path_get_machine_name(path, &m) == code); |
116 | assert_se(streq_ptr(m, result)); | |
117 | } | |
118 | ||
119 | static void test_path_get_machine_name(void) { | |
374ec6ab LP |
120 | check_p_g_m_n("/user.slice/machine-foobar.scope", 0, "foobar"); |
121 | check_p_g_m_n("/machine-foobar.scope", 0, "foobar"); | |
122 | check_p_g_m_n("/user.slice/user-kuux.slice/machine-foobar.scope", 0, "foobar"); | |
123 | check_p_g_m_n("/user.slice/user-kuux.slice/machine-foobar.scope/asjhdkj", 0, "foobar"); | |
9444b1f2 LP |
124 | check_p_g_m_n("", -ENOENT, NULL); |
125 | } | |
126 | ||
127 | static void test_get_paths(void) { | |
128 | _cleanup_free_ char *a = NULL; | |
129 | ||
130 | assert_se(cg_get_root_path(&a) >= 0); | |
131 | log_info("Root = %s", a); | |
7027ff61 LP |
132 | } |
133 | ||
aff38e74 LP |
134 | static void test_proc(void) { |
135 | _cleanup_closedir_ DIR *d = NULL; | |
136 | struct dirent *de; | |
137 | int r; | |
138 | ||
139 | d = opendir("/proc"); | |
140 | assert_se(d); | |
141 | ||
142 | FOREACH_DIRENT(de, d, break) { | |
1021b21b | 143 | _cleanup_free_ char *path = NULL, *path_shifted = NULL, *session = NULL, *unit = NULL, *user_unit = NULL, *machine = NULL, *prefix = NULL, *slice = NULL; |
aff38e74 | 144 | pid_t pid; |
ae018d9b | 145 | uid_t uid = (uid_t) -1; |
aff38e74 LP |
146 | |
147 | if (de->d_type != DT_DIR && | |
148 | de->d_type != DT_UNKNOWN) | |
149 | continue; | |
150 | ||
151 | r = parse_pid(de->d_name, &pid); | |
152 | if (r < 0) | |
153 | continue; | |
154 | ||
ae018d9b LP |
155 | if (is_kernel_thread(pid)) |
156 | continue; | |
157 | ||
aff38e74 LP |
158 | cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &path); |
159 | cg_pid_get_path_shifted(pid, &prefix, &path_shifted); | |
ae018d9b | 160 | cg_pid_get_owner_uid(pid, &uid); |
aff38e74 LP |
161 | cg_pid_get_session(pid, &session); |
162 | cg_pid_get_unit(pid, &unit); | |
163 | cg_pid_get_user_unit(pid, &user_unit); | |
164 | cg_pid_get_machine_name(pid, &machine); | |
1021b21b | 165 | cg_pid_get_slice(pid, &slice); |
aff38e74 | 166 | |
1021b21b | 167 | printf("%lu\t%s\t%s\t%s\t%lu\t%s\t%s\t%s\t%s\t%s\n", |
aff38e74 LP |
168 | (unsigned long) pid, |
169 | path, | |
170 | prefix, | |
171 | path_shifted, | |
ae018d9b | 172 | (unsigned long) uid, |
aff38e74 LP |
173 | session, |
174 | unit, | |
175 | user_unit, | |
1021b21b LP |
176 | machine, |
177 | slice); | |
aff38e74 LP |
178 | } |
179 | } | |
180 | ||
ae018d9b LP |
181 | static void test_escape_one(const char *s, const char *r) { |
182 | _cleanup_free_ char *b; | |
183 | ||
184 | b = cg_escape(s); | |
185 | assert_se(b); | |
186 | assert_se(streq(b, r)); | |
187 | ||
188 | assert_se(streq(cg_unescape(b), s)); | |
189 | } | |
190 | ||
191 | static void test_escape(void) { | |
192 | test_escape_one("foobar", "foobar"); | |
a0ab5665 | 193 | test_escape_one(".foobar", "_.foobar"); |
ae018d9b LP |
194 | test_escape_one("foobar.service", "foobar.service"); |
195 | test_escape_one("cgroup.service", "_cgroup.service"); | |
ae018d9b | 196 | test_escape_one("tasks", "_tasks"); |
e13bb5d2 KS |
197 | if (access("/sys/fs/cgroup/cpu", F_OK) == 0) |
198 | test_escape_one("cpu.service", "_cpu.service"); | |
ae018d9b | 199 | test_escape_one("_foobar", "__foobar"); |
a0ab5665 LP |
200 | test_escape_one("", "_"); |
201 | test_escape_one("_", "__"); | |
202 | test_escape_one(".", "_."); | |
ae018d9b LP |
203 | } |
204 | ||
78edb35a LP |
205 | static void test_controller_is_valid(void) { |
206 | assert_se(cg_controller_is_valid("foobar", false)); | |
207 | assert_se(cg_controller_is_valid("foo_bar", false)); | |
208 | assert_se(cg_controller_is_valid("name=foo", true)); | |
209 | assert_se(!cg_controller_is_valid("", false)); | |
210 | assert_se(!cg_controller_is_valid("name=", true)); | |
211 | assert_se(!cg_controller_is_valid("=", false)); | |
212 | assert_se(!cg_controller_is_valid("cpu,cpuacct", false)); | |
213 | assert_se(!cg_controller_is_valid("_", false)); | |
214 | assert_se(!cg_controller_is_valid("_foobar", false)); | |
215 | assert_se(!cg_controller_is_valid("tatü", false)); | |
216 | } | |
217 | ||
a016b922 LP |
218 | static void test_slice_to_path_one(const char *unit, const char *path, int error) { |
219 | _cleanup_free_ char *ret = NULL; | |
220 | ||
221 | assert_se(cg_slice_to_path(unit, &ret) == error); | |
222 | assert_se(streq_ptr(ret, path)); | |
223 | } | |
224 | ||
225 | static void test_slice_to_path(void) { | |
226 | ||
227 | test_slice_to_path_one("foobar.slice", "foobar.slice", 0); | |
228 | test_slice_to_path_one("foobar-waldo.slice", "foobar.slice/foobar-waldo.slice", 0); | |
229 | test_slice_to_path_one("foobar-waldo.service", NULL, -EINVAL); | |
230 | test_slice_to_path_one("-.slice", NULL, -EINVAL); | |
231 | test_slice_to_path_one("-foo-.slice", NULL, -EINVAL); | |
232 | test_slice_to_path_one("-foo.slice", NULL, -EINVAL); | |
233 | test_slice_to_path_one("a-b.slice", "a.slice/a-b.slice", 0); | |
234 | test_slice_to_path_one("a-b-c-d-e.slice", "a.slice/a-b.slice/a-b-c.slice/a-b-c-d.slice/a-b-c-d-e.slice", 0); | |
235 | } | |
236 | ||
96cde13a | 237 | int main(void) { |
7027ff61 | 238 | test_path_decode_unit(); |
6c03089c | 239 | test_path_get_unit(); |
7027ff61 | 240 | test_path_get_user_unit(); |
9444b1f2 LP |
241 | test_path_get_session(); |
242 | test_path_get_owner_uid(); | |
243 | test_path_get_machine_name(); | |
143bfdaf | 244 | TEST_REQ_RUNNING_SYSTEMD(test_get_paths()); |
aff38e74 | 245 | test_proc(); |
143bfdaf | 246 | TEST_REQ_RUNNING_SYSTEMD(test_escape()); |
78edb35a | 247 | test_controller_is_valid(); |
a016b922 | 248 | test_slice_to_path(); |
96cde13a ZJS |
249 | |
250 | return 0; | |
251 | } |