]>
Commit | Line | Data |
---|---|---|
72d0e1cb SG |
1 | /* liblxcapi |
2 | * | |
3 | * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>. | |
4 | * Copyright © 2012 Canonical Ltd. | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2, as | |
8 | * published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along | |
16 | * with this program; if not, write to the Free Software Foundation, Inc., | |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 | */ | |
e49c56d6 CB |
19 | |
20 | #include "config.h" | |
21 | ||
948955a2 | 22 | #include <lxc/lxccontainer.h> |
72d0e1cb SG |
23 | |
24 | #include <unistd.h> | |
25 | #include <signal.h> | |
26 | #include <stdio.h> | |
27 | #include <sys/types.h> | |
28 | #include <sys/wait.h> | |
29 | #include <stdlib.h> | |
30 | #include <errno.h> | |
95ee490b | 31 | #include <string.h> |
83319023 | 32 | |
2cf04d66 | 33 | #include "state.h" |
83319023 | 34 | #include "lxctest.h" |
6f6c71cd CB |
35 | #include "utils.h" |
36 | ||
34498dea | 37 | #if !HAVE_STRLCPY |
58db1a61 | 38 | #include "strlcpy.h" |
6f6c71cd | 39 | #endif |
72d0e1cb SG |
40 | |
41 | #define MYNAME "lxctest1" | |
42 | ||
43 | int main(int argc, char *argv[]) | |
44 | { | |
6f6c71cd CB |
45 | int fd_log, ret; |
46 | struct lxc_container *c = NULL; | |
7edd0540 | 47 | int fret = EXIT_FAILURE; |
72d0e1cb | 48 | char v1[2], v2[256], v3[2048]; |
6f6c71cd CB |
49 | struct lxc_log log = {}; |
50 | char template[sizeof(P_tmpdir"/attach_XXXXXX")]; | |
51 | ||
52 | (void)strlcpy(template, P_tmpdir"/attach_XXXXXX", sizeof(template)); | |
53 | ||
54 | fd_log = lxc_make_tmpfile(template, false); | |
55 | if (fd_log < 0) { | |
56 | lxc_error("Failed to create temporary log file for container %s\n", MYNAME); | |
57 | exit(EXIT_FAILURE); | |
58 | } | |
59 | log.name = MYNAME; | |
60 | log.file = template; | |
61 | log.level = "TRACE"; | |
62 | log.prefix = "get_item"; | |
63 | log.quiet = false; | |
64 | log.lxcpath = NULL; | |
65 | if (lxc_log_init(&log)) | |
66 | goto out; | |
72d0e1cb | 67 | |
afeecbba | 68 | if ((c = lxc_container_new("testxyz", NULL)) == NULL) { |
72d0e1cb | 69 | fprintf(stderr, "%d: error opening lxc_container %s\n", __LINE__, MYNAME); |
d479e1f7 | 70 | exit(EXIT_FAILURE); |
72d0e1cb SG |
71 | } |
72 | ||
6fba98b5 CB |
73 | /* EXPECT SUCCESS: lxc.log.syslog with valid value. */ |
74 | if (!c->set_config_item(c, "lxc.log.syslog", "local0")) { | |
75 | lxc_error("%s\n", "Failed to set lxc.log.syslog.\n"); | |
83319023 CB |
76 | goto out; |
77 | } | |
b947e657 | 78 | |
6fba98b5 | 79 | ret = c->get_config_item(c, "lxc.log.syslog", v2, 255); |
83319023 | 80 | if (ret < 0) { |
6fba98b5 | 81 | lxc_error("Failed to retrieve lxc.log.syslog: %d.\n", ret); |
83319023 CB |
82 | goto out; |
83 | } | |
b947e657 | 84 | |
83319023 CB |
85 | if (strcmp(v2, "local0") != 0) { |
86 | lxc_error("Expected: local0 == %s.\n", v2); | |
87 | goto out; | |
88 | } | |
6fba98b5 | 89 | lxc_debug("Retrieving value for lxc.log.syslog correctly returned: %s.\n", v2); |
83319023 | 90 | |
6fba98b5 CB |
91 | /* EXPECT FAILURE: lxc.log.syslog with invalid value. */ |
92 | if (c->set_config_item(c, "lxc.log.syslog", "NONSENSE")) { | |
93 | lxc_error("%s\n", "Succeeded int setting lxc.log.syslog to invalid value \"NONSENSE\".\n"); | |
83319023 CB |
94 | goto out; |
95 | } | |
6fba98b5 | 96 | lxc_debug("%s\n", "Successfully failed to set lxc.log.syslog to invalid value.\n"); |
83319023 | 97 | |
72d0e1cb SG |
98 | if (!c->set_config_item(c, "lxc.hook.pre-start", "hi there")) { |
99 | fprintf(stderr, "%d: failed to set hook.pre-start\n", __LINE__); | |
72d0e1cb SG |
100 | goto out; |
101 | } | |
b947e657 | 102 | |
72d0e1cb SG |
103 | ret = c->get_config_item(c, "lxc.hook.pre-start", v2, 255); |
104 | if (ret < 0) { | |
105 | fprintf(stderr, "%d: get_config_item(lxc.hook.pre-start) returned %d\n", __LINE__, ret); | |
72d0e1cb SG |
106 | goto out; |
107 | } | |
108 | fprintf(stderr, "lxc.hook.pre-start returned %d %s\n", ret, v2); | |
109 | ||
6fba98b5 | 110 | ret = c->get_config_item(c, "lxc.net", v2, 255); |
72d0e1cb SG |
111 | if (ret < 0) { |
112 | fprintf(stderr, "%d: get_config_item returned %d\n", __LINE__, ret); | |
72d0e1cb SG |
113 | goto out; |
114 | } | |
6fba98b5 | 115 | fprintf(stderr, "%d: get_config_item(lxc.net) returned %d %s\n", __LINE__, ret, v2); |
fe1c5887 | 116 | |
6fba98b5 | 117 | if (!c->set_config_item(c, "lxc.tty.max", "4")) { |
72d0e1cb | 118 | fprintf(stderr, "%d: failed to set tty\n", __LINE__); |
72d0e1cb SG |
119 | goto out; |
120 | } | |
b947e657 | 121 | |
6fba98b5 | 122 | ret = c->get_config_item(c, "lxc.tty.max", v2, 255); |
72d0e1cb SG |
123 | if (ret < 0) { |
124 | fprintf(stderr, "%d: get_config_item(lxc.tty) returned %d\n", __LINE__, ret); | |
72d0e1cb SG |
125 | goto out; |
126 | } | |
127 | fprintf(stderr, "lxc.tty returned %d %s\n", ret, v2); | |
128 | ||
129 | if (!c->set_config_item(c, "lxc.arch", "x86")) { | |
130 | fprintf(stderr, "%d: failed to set arch\n", __LINE__); | |
72d0e1cb SG |
131 | goto out; |
132 | } | |
b947e657 | 133 | |
72d0e1cb SG |
134 | ret = c->get_config_item(c, "lxc.arch", v2, 255); |
135 | if (ret < 0) { | |
136 | fprintf(stderr, "%d: get_config_item(lxc.arch) returned %d\n", __LINE__, ret); | |
72d0e1cb SG |
137 | goto out; |
138 | } | |
139 | printf("lxc.arch returned %d %s\n", ret, v2); | |
140 | ||
6fba98b5 | 141 | if (!c->set_config_item(c, "lxc.init.uid", "100")) { |
68d18db8 | 142 | fprintf(stderr, "%d: failed to set init_uid\n", __LINE__); |
68d18db8 PT |
143 | goto out; |
144 | } | |
b947e657 | 145 | |
6fba98b5 | 146 | ret = c->get_config_item(c, "lxc.init.uid", v2, 255); |
68d18db8 PT |
147 | if (ret < 0) { |
148 | fprintf(stderr, "%d: get_config_item(lxc.init_uid) returned %d\n", __LINE__, ret); | |
68d18db8 PT |
149 | goto out; |
150 | } | |
151 | printf("lxc.init_uid returned %d %s\n", ret, v2); | |
152 | ||
6fba98b5 | 153 | if (!c->set_config_item(c, "lxc.init.gid", "100")) { |
68d18db8 | 154 | fprintf(stderr, "%d: failed to set init_gid\n", __LINE__); |
68d18db8 PT |
155 | goto out; |
156 | } | |
b947e657 | 157 | |
6fba98b5 | 158 | ret = c->get_config_item(c, "lxc.init.gid", v2, 255); |
68d18db8 PT |
159 | if (ret < 0) { |
160 | fprintf(stderr, "%d: get_config_item(lxc.init_gid) returned %d\n", __LINE__, ret); | |
68d18db8 PT |
161 | goto out; |
162 | } | |
163 | printf("lxc.init_gid returned %d %s\n", ret, v2); | |
164 | ||
6f6c71cd CB |
165 | if (c->set_config_item(c, "lxc.init.groups", "10,20,foo,40")) { |
166 | fprintf(stderr, "%d: managed to set lxc.init.groups to '10,20,foo,40'\n", __LINE__); | |
bf31b337 RJ |
167 | goto out; |
168 | } | |
169 | ||
6f6c71cd CB |
170 | if (!c->set_config_item(c, "lxc.init.groups", "10,20,30,40")) { |
171 | fprintf(stderr, "%d: managed to set lxc.init.groups to '10,20,30,40'\n", __LINE__); | |
bf31b337 RJ |
172 | goto out; |
173 | } | |
174 | ||
6f6c71cd CB |
175 | ret = c->get_config_item(c, "lxc.init.groups", v2, 255); |
176 | if (ret < 0) { | |
177 | fprintf(stderr, "%d: failed to get lxc.init.groups\n", __LINE__); | |
178 | goto out; | |
179 | } | |
180 | ret = strcmp("10,20,30,40", v2); | |
181 | printf("%d: lxc.init.groups returned %d %s\n", __LINE__, ret, v2); | |
182 | if (ret != 0) { | |
183 | goto out; | |
184 | } | |
185 | ||
186 | if (!c->set_config_item(c, "lxc.init.groups", "50,60,70,80")) { | |
187 | fprintf(stderr, "%d: failed to set lxc.init.groups to '50,60,70,80'\n", __LINE__); | |
bf31b337 RJ |
188 | goto out; |
189 | } | |
190 | ||
191 | ret = c->get_config_item(c, "lxc.init.groups", v2, 255); | |
192 | if (ret < 0) { | |
6f6c71cd | 193 | fprintf(stderr, "%d: failed to get lxc.init.groups\n", __LINE__); |
bf31b337 RJ |
194 | goto out; |
195 | } | |
6f6c71cd CB |
196 | ret = strcmp("10,20,30,40,50,60,70,80", v2); |
197 | printf("%d: lxc.init.groups returned %d %s\n", __LINE__, ret, v2); | |
198 | if (ret != 0) { | |
199 | goto out; | |
200 | } | |
201 | ||
202 | if (!c->set_config_item(c, "lxc.init.groups", "")) { | |
203 | fprintf(stderr, "%d: failed to set lxc.init.groups to ''\n", __LINE__); | |
204 | goto out; | |
205 | } | |
206 | ||
207 | ret = c->get_config_item(c, "lxc.init.groups", v2, 255); | |
208 | if (ret < 0) { | |
209 | fprintf(stderr, "%d: failed to get lxc.init.groups\n", __LINE__); | |
210 | goto out; | |
211 | } | |
212 | ret = strcmp("", v2); | |
213 | printf("%d: lxc.init.groups returned %d %s\n", __LINE__, ret, v2); | |
bf31b337 RJ |
214 | if (ret != 0) { |
215 | goto out; | |
216 | } | |
217 | ||
72d0e1cb SG |
218 | #define HNAME "hostname1" |
219 | // demonstrate proper usage: | |
220 | char *alloced; | |
b947e657 | 221 | int len; |
222 | ||
6fba98b5 | 223 | if (!c->set_config_item(c, "lxc.uts.name", HNAME)) { |
72d0e1cb | 224 | fprintf(stderr, "%d: failed to set utsname\n", __LINE__); |
72d0e1cb SG |
225 | goto out; |
226 | } | |
227 | ||
6fba98b5 | 228 | len = c->get_config_item(c, "lxc.uts.name", NULL, 0); // query the size of the string |
72d0e1cb SG |
229 | if (len < 0) { |
230 | fprintf(stderr, "%d: get_config_item(lxc.utsname) returned %d\n", __LINE__, len); | |
72d0e1cb SG |
231 | goto out; |
232 | } | |
233 | printf("lxc.utsname returned %d\n", len); | |
234 | ||
235 | // allocate the length of string + 1 for trailing \0 | |
236 | alloced = malloc(len+1); | |
237 | if (!alloced) { | |
238 | fprintf(stderr, "%d: failed to allocate %d bytes for utsname\n", __LINE__, len); | |
72d0e1cb SG |
239 | goto out; |
240 | } | |
b947e657 | 241 | |
72d0e1cb SG |
242 | // now pass in the malloc'd array, and pass in length of string + 1: again |
243 | // because we need room for the trailing \0 | |
6fba98b5 | 244 | ret = c->get_config_item(c, "lxc.uts.name", alloced, len+1); |
72d0e1cb SG |
245 | if (ret < 0) { |
246 | fprintf(stderr, "%d: get_config_item(lxc.utsname) returned %d\n", __LINE__, ret); | |
72d0e1cb SG |
247 | goto out; |
248 | } | |
b947e657 | 249 | |
72d0e1cb SG |
250 | if (strcmp(alloced, HNAME) != 0 || ret != len) { |
251 | fprintf(stderr, "lxc.utsname returned wrong value: %d %s not %d %s\n", ret, alloced, len, HNAME); | |
72d0e1cb SG |
252 | goto out; |
253 | } | |
254 | printf("lxc.utsname returned %d %s\n", len, alloced); | |
255 | free(alloced); | |
256 | ||
257 | if (!c->set_config_item(c, "lxc.mount.entry", "hi there")) { | |
258 | fprintf(stderr, "%d: failed to set mount.entry\n", __LINE__); | |
72d0e1cb SG |
259 | goto out; |
260 | } | |
b947e657 | 261 | |
72d0e1cb SG |
262 | ret = c->get_config_item(c, "lxc.mount.entry", v2, 255); |
263 | if (ret < 0) { | |
264 | fprintf(stderr, "%d: get_config_item(lxc.mount.entry) returned %d\n", __LINE__, ret); | |
72d0e1cb SG |
265 | goto out; |
266 | } | |
267 | printf("lxc.mount.entry returned %d %s\n", ret, v2); | |
268 | ||
6fba98b5 | 269 | ret = c->get_config_item(c, "lxc.prlimit", v3, 2047); |
fe17b651 WB |
270 | if (ret != 0) { |
271 | fprintf(stderr, "%d: get_config_item(limit) returned %d\n", __LINE__, ret); | |
272 | goto out; | |
273 | } | |
274 | ||
6fba98b5 | 275 | if (!c->set_config_item(c, "lxc.prlimit.nofile", "1234:unlimited")) { |
fe17b651 WB |
276 | fprintf(stderr, "%d: failed to set limit.nofile\n", __LINE__); |
277 | goto out; | |
278 | } | |
b947e657 | 279 | |
6fba98b5 | 280 | ret = c->get_config_item(c, "lxc.prlimit.nofile", v2, 255); |
fe17b651 | 281 | if (ret < 0) { |
6fba98b5 | 282 | fprintf(stderr, "%d: get_config_item(lxc.prlimit.nofile) returned %d\n", __LINE__, ret); |
fe17b651 WB |
283 | goto out; |
284 | } | |
b947e657 | 285 | |
fe17b651 | 286 | if (strcmp(v2, "1234:unlimited")) { |
6fba98b5 | 287 | fprintf(stderr, "%d: lxc.prlimit.nofile returned wrong value: %d %s not 14 1234:unlimited\n", __LINE__, ret, v2); |
fe17b651 WB |
288 | goto out; |
289 | } | |
6fba98b5 | 290 | printf("lxc.prlimit.nofile returned %d %s\n", ret, v2); |
fe17b651 | 291 | |
6fba98b5 | 292 | if (!c->set_config_item(c, "lxc.prlimit.stack", "unlimited")) { |
fe17b651 WB |
293 | fprintf(stderr, "%d: failed to set limit.stack\n", __LINE__); |
294 | goto out; | |
295 | } | |
b947e657 | 296 | |
6fba98b5 | 297 | ret = c->get_config_item(c, "lxc.prlimit.stack", v2, 255); |
fe17b651 | 298 | if (ret < 0) { |
6fba98b5 | 299 | fprintf(stderr, "%d: get_config_item(lxc.prlimit.stack) returned %d\n", __LINE__, ret); |
fe17b651 WB |
300 | goto out; |
301 | } | |
b947e657 | 302 | |
fe17b651 | 303 | if (strcmp(v2, "unlimited")) { |
6fba98b5 | 304 | fprintf(stderr, "%d: lxc.prlimit.stack returned wrong value: %d %s not 9 unlimited\n", __LINE__, ret, v2); |
fe17b651 WB |
305 | goto out; |
306 | } | |
6fba98b5 | 307 | printf("lxc.prlimit.stack returned %d %s\n", ret, v2); |
fe17b651 | 308 | |
6fba98b5 CB |
309 | #define LIMIT_STACK "lxc.prlimit.stack = unlimited\n" |
310 | #define ALL_LIMITS "lxc.prlimit.nofile = 1234:unlimited\n" LIMIT_STACK | |
311 | ret = c->get_config_item(c, "lxc.prlimit", v3, 2047); | |
fe17b651 WB |
312 | if (ret != sizeof(ALL_LIMITS)-1) { |
313 | fprintf(stderr, "%d: get_config_item(limit) returned %d\n", __LINE__, ret); | |
314 | goto out; | |
315 | } | |
b947e657 | 316 | |
fe17b651 | 317 | if (strcmp(v3, ALL_LIMITS)) { |
6fba98b5 | 318 | fprintf(stderr, "%d: lxc.prlimit returned wrong value: %d %s not %d %s\n", __LINE__, ret, v3, (int)sizeof(ALL_LIMITS)-1, ALL_LIMITS); |
fe17b651 WB |
319 | goto out; |
320 | } | |
6fba98b5 | 321 | printf("lxc.prlimit returned %d %s\n", ret, v3); |
fe17b651 | 322 | |
6fba98b5 | 323 | if (!c->clear_config_item(c, "lxc.prlimit.nofile")) { |
fe17b651 WB |
324 | fprintf(stderr, "%d: failed clearing limit.nofile\n", __LINE__); |
325 | goto out; | |
326 | } | |
b947e657 | 327 | |
6fba98b5 | 328 | ret = c->get_config_item(c, "lxc.prlimit", v3, 2047); |
fe17b651 WB |
329 | if (ret != sizeof(LIMIT_STACK)-1) { |
330 | fprintf(stderr, "%d: get_config_item(limit) returned %d\n", __LINE__, ret); | |
331 | goto out; | |
332 | } | |
b947e657 | 333 | |
fe17b651 | 334 | if (strcmp(v3, LIMIT_STACK)) { |
6fba98b5 | 335 | fprintf(stderr, "%d: lxc.prlimit returned wrong value: %d %s not %d %s\n", __LINE__, ret, v3, (int)sizeof(LIMIT_STACK)-1, LIMIT_STACK); |
fe17b651 WB |
336 | goto out; |
337 | } | |
6fba98b5 | 338 | printf("lxc.prlimit returned %d %s\n", ret, v3); |
fe17b651 | 339 | |
7edd0540 L |
340 | #define SYSCTL_SOMAXCONN "lxc.sysctl.net.core.somaxconn = 256\n" |
341 | #define ALL_SYSCTLS "lxc.sysctl.net.ipv4.ip_forward = 1\n" SYSCTL_SOMAXCONN | |
342 | ||
343 | ret = c->get_config_item(c, "lxc.sysctl", v3, 2047); | |
344 | if (ret != 0) { | |
345 | fprintf(stderr, "%d: get_config_item(sysctl) returned %d\n", __LINE__, ret); | |
346 | goto out; | |
347 | } | |
348 | ||
349 | if (!c->set_config_item(c, "lxc.sysctl.net.ipv4.ip_forward", "1")) { | |
350 | fprintf(stderr, "%d: failed to set lxc.sysctl.net.ipv4.ip_forward\n", __LINE__); | |
351 | goto out; | |
352 | } | |
b947e657 | 353 | |
7edd0540 L |
354 | ret = c->get_config_item(c, "lxc.sysctl.net.ipv4.ip_forward", v2, 255); |
355 | if (ret < 0) { | |
356 | fprintf(stderr, "%d: get_config_item(lxc.sysctl.net.ipv4.ip_forward) returned %d\n", __LINE__, ret); | |
357 | goto out; | |
358 | } | |
b947e657 | 359 | |
7edd0540 L |
360 | if (strcmp(v2, "1")) { |
361 | fprintf(stderr, "%d: lxc.sysctl.net.ipv4.ip_forward returned wrong value: %d %s not 1\n", __LINE__, ret, v2); | |
362 | goto out; | |
363 | } | |
364 | printf("lxc.sysctl.net.ipv4.ip_forward returned %d %s\n", ret, v2); | |
365 | ||
366 | if (!c->set_config_item(c, "lxc.sysctl.net.core.somaxconn", "256")) { | |
367 | fprintf(stderr, "%d: failed to set lxc.sysctl.net.core.somaxconn\n", __LINE__); | |
368 | goto out; | |
369 | } | |
b947e657 | 370 | |
7edd0540 L |
371 | ret = c->get_config_item(c, "lxc.sysctl.net.core.somaxconn", v2, 255); |
372 | if (ret < 0) { | |
373 | fprintf(stderr, "%d: get_config_item(lxc.sysctl.net.core.somaxconn) returned %d\n", __LINE__, ret); | |
374 | goto out; | |
375 | } | |
b947e657 | 376 | |
7edd0540 L |
377 | if (strcmp(v2, "256")) { |
378 | fprintf(stderr, "%d: lxc.sysctl.net.core.somaxconn returned wrong value: %d %s not 256\n", __LINE__, ret, v2); | |
379 | goto out; | |
380 | } | |
381 | printf("lxc.sysctl.net.core.somaxconn returned %d %s\n", ret, v2); | |
382 | ||
383 | ret = c->get_config_item(c, "lxc.sysctl", v3, 2047); | |
384 | if (ret != sizeof(ALL_SYSCTLS)-1) { | |
385 | fprintf(stderr, "%d: get_config_item(sysctl) returned %d\n", __LINE__, ret); | |
386 | goto out; | |
387 | } | |
b947e657 | 388 | |
7edd0540 L |
389 | if (strcmp(v3, ALL_SYSCTLS)) { |
390 | fprintf(stderr, "%d: lxc.sysctl returned wrong value: %d %s not %d %s\n", __LINE__, ret, v3, (int)sizeof(ALL_SYSCTLS) - 1, ALL_SYSCTLS); | |
391 | goto out; | |
392 | } | |
393 | printf("lxc.sysctl returned %d %s\n", ret, v3); | |
394 | ||
395 | if (!c->clear_config_item(c, "lxc.sysctl.net.ipv4.ip_forward")) { | |
396 | fprintf(stderr, "%d: failed clearing lxc.sysctl.net.ipv4.ip_forward\n", __LINE__); | |
397 | goto out; | |
398 | } | |
b947e657 | 399 | |
7edd0540 L |
400 | ret = c->get_config_item(c, "lxc.sysctl", v3, 2047); |
401 | if (ret != sizeof(SYSCTL_SOMAXCONN) - 1) { | |
402 | fprintf(stderr, "%d: get_config_item(sysctl) returned %d\n", __LINE__, ret); | |
403 | goto out; | |
404 | } | |
b947e657 | 405 | |
7edd0540 L |
406 | if (strcmp(v3, SYSCTL_SOMAXCONN)) { |
407 | fprintf(stderr, "%d: lxc.sysctl returned wrong value: %d %s not %d %s\n", __LINE__, ret, v3, (int)sizeof(SYSCTL_SOMAXCONN) - 1, SYSCTL_SOMAXCONN); | |
408 | goto out; | |
409 | } | |
410 | printf("lxc.sysctl returned %d %s\n", ret, v3); | |
411 | ||
61d7a733 YT |
412 | #define PROC_OOM_SCORE_ADJ "lxc.proc.oom_score_adj = 10\n" |
413 | #define ALL_PROCS "lxc.proc.setgroups = allow\n" PROC_OOM_SCORE_ADJ | |
414 | ||
415 | ret = c->get_config_item(c, "lxc.proc", v3, 2047); | |
416 | if (ret != 0) { | |
417 | fprintf(stderr, "%d: get_config_item(proc) returned %d\n", __LINE__, ret); | |
418 | goto out; | |
419 | } | |
420 | ||
421 | if (!c->set_config_item(c, "lxc.proc.setgroups", "allow")) { | |
422 | fprintf(stderr, "%d: failed to set lxc.proc.setgroups\n", __LINE__); | |
423 | goto out; | |
424 | } | |
425 | ||
426 | ret = c->get_config_item(c, "lxc.proc.setgroups", v2, 255); | |
427 | if (ret < 0) { | |
428 | fprintf(stderr, "%d: get_config_item(lxc.proc.setgroups) returned %d\n", __LINE__, ret); | |
429 | goto out; | |
430 | } | |
b947e657 | 431 | |
61d7a733 YT |
432 | if (strcmp(v2, "allow")) { |
433 | fprintf(stderr, "%d: lxc.proc.setgroups returned wrong value: %d %s not 10\n", __LINE__, ret, v2); | |
434 | goto out; | |
435 | } | |
436 | printf("lxc.proc.setgroups returned %d %s\n", ret, v2); | |
437 | ||
438 | if (!c->set_config_item(c, "lxc.proc.oom_score_adj", "10")) { | |
439 | fprintf(stderr, "%d: failed to set lxc.proc.oom_score_adj\n", __LINE__); | |
440 | goto out; | |
441 | } | |
442 | ||
443 | ret = c->get_config_item(c, "lxc.proc.oom_score_adj", v2, 255); | |
444 | if (ret < 0) { | |
445 | fprintf(stderr, "%d: get_config_item(lxc.proc.oom_score_adj) returned %d\n", __LINE__, ret); | |
446 | goto out; | |
447 | } | |
b947e657 | 448 | |
61d7a733 YT |
449 | if (strcmp(v2, "10")) { |
450 | fprintf(stderr, "%d: lxc.proc.oom_score_adj returned wrong value: %d %s not 10\n", __LINE__, ret, v2); | |
451 | goto out; | |
452 | } | |
453 | printf("lxc.proc.oom_score_adj returned %d %s\n", ret, v2); | |
454 | ||
455 | ret = c->get_config_item(c, "lxc.proc", v3, 2047); | |
456 | if (ret != sizeof(ALL_PROCS)-1) { | |
457 | fprintf(stderr, "%d: get_config_item(proc) returned %d\n", __LINE__, ret); | |
458 | goto out; | |
459 | } | |
b947e657 | 460 | |
61d7a733 YT |
461 | if (strcmp(v3, ALL_PROCS)) { |
462 | fprintf(stderr, "%d: lxc.proc returned wrong value: %d %s not %d %s\n", __LINE__, ret, v3, (int)sizeof(ALL_PROCS) - 1, ALL_PROCS); | |
463 | goto out; | |
464 | } | |
465 | printf("lxc.proc returned %d %s\n", ret, v3); | |
466 | ||
467 | if (!c->clear_config_item(c, "lxc.proc.setgroups")) { | |
468 | fprintf(stderr, "%d: failed clearing lxc.proc.setgroups\n", __LINE__); | |
469 | goto out; | |
470 | } | |
b947e657 | 471 | |
61d7a733 YT |
472 | ret = c->get_config_item(c, "lxc.proc", v3, 2047); |
473 | if (ret < 0) { | |
474 | fprintf(stderr, "%d: get_config_item(proc) returned %d\n", __LINE__, ret); | |
475 | goto out; | |
476 | } | |
b947e657 | 477 | |
61d7a733 YT |
478 | if (strcmp(v3, PROC_OOM_SCORE_ADJ)) { |
479 | fprintf(stderr, "%d: lxc.proc returned wrong value: %d %s not %d %s\n", __LINE__, ret, v3, (int)sizeof(PROC_OOM_SCORE_ADJ) - 1, PROC_OOM_SCORE_ADJ); | |
480 | goto out; | |
481 | } | |
482 | printf("lxc.proc returned %d %s\n", ret, v3); | |
483 | ||
6fba98b5 | 484 | if (!c->set_config_item(c, "lxc.apparmor.profile", "unconfined")) { |
72d0e1cb | 485 | fprintf(stderr, "%d: failed to set aa_profile\n", __LINE__); |
72d0e1cb SG |
486 | goto out; |
487 | } | |
b947e657 | 488 | |
6fba98b5 | 489 | ret = c->get_config_item(c, "lxc.apparmor.profile", v2, 255); |
72d0e1cb SG |
490 | if (ret < 0) { |
491 | fprintf(stderr, "%d: get_config_item(lxc.aa_profile) returned %d\n", __LINE__, ret); | |
72d0e1cb SG |
492 | goto out; |
493 | } | |
494 | printf("lxc.aa_profile returned %d %s\n", ret, v2); | |
495 | ||
496 | lxc_container_put(c); | |
497 | ||
498 | // new test with real container | |
afeecbba | 499 | if ((c = lxc_container_new(MYNAME, NULL)) == NULL) { |
72d0e1cb | 500 | fprintf(stderr, "%d: error opening lxc_container %s\n", __LINE__, MYNAME); |
72d0e1cb SG |
501 | goto out; |
502 | } | |
503 | c->destroy(c); | |
504 | lxc_container_put(c); | |
505 | ||
afeecbba | 506 | if ((c = lxc_container_new(MYNAME, NULL)) == NULL) { |
72d0e1cb | 507 | fprintf(stderr, "%d: error opening lxc_container %s\n", __LINE__, MYNAME); |
72d0e1cb SG |
508 | goto out; |
509 | } | |
b947e657 | 510 | |
787c3ebe | 511 | if (!c->createl(c, "busybox", NULL, NULL, 0, NULL)) { |
e403a064 | 512 | fprintf(stderr, "%d: failed to create a trusty container\n", __LINE__); |
72d0e1cb SG |
513 | goto out; |
514 | } | |
72d0e1cb SG |
515 | lxc_container_put(c); |
516 | ||
517 | /* XXX TODO load_config needs to clear out any old config first */ | |
afeecbba | 518 | if ((c = lxc_container_new(MYNAME, NULL)) == NULL) { |
72d0e1cb | 519 | fprintf(stderr, "%d: error opening lxc_container %s\n", __LINE__, MYNAME); |
72d0e1cb SG |
520 | goto out; |
521 | } | |
522 | ||
523 | ret = c->get_config_item(c, "lxc.cap.drop", NULL, 300); | |
524 | if (ret < 5 || ret > 255) { | |
525 | fprintf(stderr, "%d: get_config_item(lxc.cap.drop) with NULL returned %d\n", __LINE__, ret); | |
72d0e1cb SG |
526 | goto out; |
527 | } | |
b947e657 | 528 | |
72d0e1cb SG |
529 | ret = c->get_config_item(c, "lxc.cap.drop", v1, 1); |
530 | if (ret < 5 || ret > 255) { | |
531 | fprintf(stderr, "%d: get_config_item(lxc.cap.drop) returned %d\n", __LINE__, ret); | |
72d0e1cb SG |
532 | goto out; |
533 | } | |
b947e657 | 534 | |
72d0e1cb SG |
535 | ret = c->get_config_item(c, "lxc.cap.drop", v2, 255); |
536 | if (ret < 0) { | |
537 | fprintf(stderr, "%d: get_config_item(lxc.cap.drop) returned %d %s\n", __LINE__, ret, v2); | |
72d0e1cb SG |
538 | goto out; |
539 | } | |
540 | printf("%d: get_config_item(lxc.cap.drop) returned %d %s\n", __LINE__, ret, v2); | |
b947e657 | 541 | |
6fba98b5 | 542 | ret = c->get_config_item(c, "lxc.net", v2, 255); |
72d0e1cb | 543 | if (ret < 0) { |
6fba98b5 | 544 | fprintf(stderr, "%d: get_config_item(lxc.net) returned %d\n", __LINE__, ret); |
72d0e1cb SG |
545 | goto out; |
546 | } | |
6fba98b5 | 547 | printf("%d: get_config_item(lxc.net) returned %d %s\n", __LINE__, ret, v2); |
72d0e1cb | 548 | |
6fba98b5 CB |
549 | if (!c->set_config_item(c, "lxc.net.0.type", "veth")) { |
550 | fprintf(stderr, "%d: failed to set lxc.net.0.type\n", __LINE__); | |
935cfd3e | 551 | goto out; |
552 | } | |
b947e657 | 553 | |
6fba98b5 | 554 | if (!c->set_config_item(c, "lxc.net.0.link", "lxcbr0")) { |
935cfd3e | 555 | fprintf(stderr, "%d: failed to set network.link\n", __LINE__); |
556 | goto out; | |
557 | } | |
b947e657 | 558 | |
6fba98b5 | 559 | if (!c->set_config_item(c, "lxc.net.0.flags", "up")) { |
935cfd3e | 560 | fprintf(stderr, "%d: failed to set network.flags\n", __LINE__); |
561 | goto out; | |
562 | } | |
b947e657 | 563 | |
6fba98b5 | 564 | if (!c->set_config_item(c, "lxc.net.0.hwaddr", "00:16:3e:xx:xx:xx")) { |
935cfd3e | 565 | fprintf(stderr, "%d: failed to set network.hwaddr\n", __LINE__); |
566 | goto out; | |
567 | } | |
b947e657 | 568 | |
6fba98b5 | 569 | if (!c->set_config_item(c, "lxc.net.0.ipv4.address", "10.2.3.4")) { |
72d0e1cb | 570 | fprintf(stderr, "%d: failed to set ipv4\n", __LINE__); |
72d0e1cb SG |
571 | goto out; |
572 | } | |
573 | ||
6fba98b5 | 574 | ret = c->get_config_item(c, "lxc.net.0.ipv4.address", v2, 255); |
72d0e1cb | 575 | if (ret <= 0) { |
6fba98b5 | 576 | fprintf(stderr, "%d: lxc.net.0.ipv4 returned %d\n", __LINE__, ret); |
72d0e1cb SG |
577 | goto out; |
578 | } | |
b947e657 | 579 | |
6fba98b5 | 580 | if (!c->clear_config_item(c, "lxc.net.0.ipv4.address")) { |
72d0e1cb | 581 | fprintf(stderr, "%d: failed clearing all ipv4 entries\n", __LINE__); |
72d0e1cb SG |
582 | goto out; |
583 | } | |
b947e657 | 584 | |
6fba98b5 | 585 | ret = c->get_config_item(c, "lxc.net.0.ipv4.address", v2, 255); |
72d0e1cb SG |
586 | if (ret != 0) { |
587 | fprintf(stderr, "%d: after clearing ipv4 entries get_item(lxc.network.0.ipv4 returned %d\n", __LINE__, ret); | |
72d0e1cb SG |
588 | goto out; |
589 | } | |
590 | ||
6fba98b5 | 591 | if (!c->set_config_item(c, "lxc.net.0.ipv4.gateway", "10.2.3.254")) { |
9eaf8a59 | 592 | fprintf(stderr, "%d: failed to set ipv4.gateway\n", __LINE__); |
9eaf8a59 SJ |
593 | goto out; |
594 | } | |
595 | ||
6fba98b5 | 596 | ret = c->get_config_item(c, "lxc.net.0.ipv4.gateway", v2, 255); |
9eaf8a59 | 597 | if (ret <= 0) { |
6fba98b5 | 598 | fprintf(stderr, "%d: lxc.net.0.ipv4.gateway returned %d\n", __LINE__, ret); |
9eaf8a59 SJ |
599 | goto out; |
600 | } | |
b947e657 | 601 | |
6fba98b5 | 602 | if (!c->set_config_item(c, "lxc.net.0.ipv4.gateway", "")) { |
9eaf8a59 | 603 | fprintf(stderr, "%d: failed clearing ipv4.gateway\n", __LINE__); |
9eaf8a59 SJ |
604 | goto out; |
605 | } | |
b947e657 | 606 | |
6fba98b5 | 607 | ret = c->get_config_item(c, "lxc.net.0.ipv4.gateway", v2, 255); |
9eaf8a59 SJ |
608 | if (ret != 0) { |
609 | fprintf(stderr, "%d: after clearing ipv4.gateway get_item(lxc.network.0.ipv4.gateway returned %d\n", __LINE__, ret); | |
9eaf8a59 SJ |
610 | goto out; |
611 | } | |
612 | ||
6fba98b5 | 613 | ret = c->get_config_item(c, "lxc.net.0.link", v2, 255); |
72d0e1cb SG |
614 | if (ret < 0) { |
615 | fprintf(stderr, "%d: get_config_item returned %d\n", __LINE__, ret); | |
72d0e1cb SG |
616 | goto out; |
617 | } | |
618 | printf("%d: get_config_item (link) returned %d %s\n", __LINE__, ret, v2); | |
b947e657 | 619 | |
6fba98b5 | 620 | ret = c->get_config_item(c, "lxc.net.0.name", v2, 255); |
72d0e1cb SG |
621 | if (ret < 0) { |
622 | fprintf(stderr, "%d: get_config_item returned %d\n", __LINE__, ret); | |
72d0e1cb SG |
623 | goto out; |
624 | } | |
625 | printf("%d: get_config_item (name) returned %d %s\n", __LINE__, ret, v2); | |
626 | ||
6fba98b5 | 627 | if (!c->clear_config_item(c, "lxc.net")) { |
72d0e1cb | 628 | fprintf(stderr, "%d: clear_config_item failed\n", __LINE__); |
72d0e1cb SG |
629 | goto out; |
630 | } | |
b947e657 | 631 | |
6fba98b5 | 632 | ret = c->get_config_item(c, "lxc.net", v2, 255); |
72d0e1cb SG |
633 | if (ret != 0) { |
634 | fprintf(stderr, "%d: network was not actually cleared (get_network returned %d)\n", __LINE__, ret); | |
72d0e1cb SG |
635 | goto out; |
636 | } | |
637 | ||
638 | ret = c->get_config_item(c, "lxc.cgroup", v3, 2047); | |
639 | if (ret < 0) { | |
640 | fprintf(stderr, "%d: get_config_item(cgroup.devices) returned %d\n", __LINE__, ret); | |
72d0e1cb SG |
641 | goto out; |
642 | } | |
643 | printf("%d: get_config_item (cgroup.devices) returned %d %s\n", __LINE__, ret, v3); | |
644 | ||
645 | ret = c->get_config_item(c, "lxc.cgroup.devices.allow", v3, 2047); | |
646 | if (ret < 0) { | |
647 | fprintf(stderr, "%d: get_config_item(cgroup.devices.devices.allow) returned %d\n", __LINE__, ret); | |
72d0e1cb SG |
648 | goto out; |
649 | } | |
650 | printf("%d: get_config_item (cgroup.devices.devices.allow) returned %d %s\n", __LINE__, ret, v3); | |
651 | ||
652 | if (!c->clear_config_item(c, "lxc.cgroup")) { | |
03fadd16 | 653 | fprintf(stderr, "%d: failed clearing lxc.cgroup\n", __LINE__); |
72d0e1cb SG |
654 | goto out; |
655 | } | |
b947e657 | 656 | |
72d0e1cb | 657 | if (!c->clear_config_item(c, "lxc.cap.drop")) { |
03fadd16 | 658 | fprintf(stderr, "%d: failed clearing lxc.cap.drop\n", __LINE__); |
72d0e1cb SG |
659 | goto out; |
660 | } | |
b947e657 | 661 | |
6a564066 SG |
662 | if (!c->clear_config_item(c, "lxc.mount.entry")) { |
663 | fprintf(stderr, "%d: failed clearing lxc.mount.entry\n", __LINE__); | |
72d0e1cb SG |
664 | goto out; |
665 | } | |
b947e657 | 666 | |
72d0e1cb | 667 | if (!c->clear_config_item(c, "lxc.hook")) { |
03fadd16 | 668 | fprintf(stderr, "%d: failed clearing lxc.hook\n", __LINE__); |
72d0e1cb SG |
669 | goto out; |
670 | } | |
add40e62 CB |
671 | |
672 | if (!lxc_config_item_is_supported("lxc.arch")) { | |
673 | fprintf(stderr, "%d: failed to report \"lxc.arch\" as supported configuration item\n", __LINE__); | |
674 | goto out; | |
675 | } | |
676 | ||
677 | if (lxc_config_item_is_supported("lxc.nonsense")) { | |
678 | fprintf(stderr, "%d: failed to detect \"lxc.nonsense\" as unsupported configuration item\n", __LINE__); | |
679 | goto out; | |
680 | } | |
681 | ||
6eb516a7 RJ |
682 | if (lxc_config_item_is_supported("lxc.arch.nonsense")) { |
683 | fprintf(stderr, "%d: failed to detect \"lxc.arch.nonsense\" as unsupported configuration item\n", __LINE__); | |
684 | goto out; | |
685 | } | |
686 | ||
646e6c8b CB |
687 | if (c->set_config_item(c, "lxc.notaconfigkey", "invalid")) { |
688 | fprintf(stderr, "%d: Managed to set \"lxc.notaconfigkey\"\n", __LINE__); | |
689 | goto out; | |
690 | } | |
691 | ||
692 | ||
72d0e1cb | 693 | printf("All get_item tests passed\n"); |
7edd0540 | 694 | fret = EXIT_SUCCESS; |
b947e657 | 695 | |
72d0e1cb | 696 | out: |
5090cb06 DJ |
697 | if (c) { |
698 | c->destroy(c); | |
699 | lxc_container_put(c); | |
700 | } | |
6f6c71cd CB |
701 | if (fret != EXIT_SUCCESS) { |
702 | char buf[4096]; | |
703 | ssize_t buflen; | |
704 | while ((buflen = read(fd_log, buf, 1024)) > 0) { | |
705 | buflen = write(STDERR_FILENO, buf, buflen); | |
706 | if (buflen <= 0) | |
707 | break; | |
708 | } | |
709 | close(fd_log); | |
710 | } | |
711 | (void)unlink(template); | |
5090cb06 | 712 | |
7edd0540 | 713 | exit(fret); |
d479e1f7 | 714 | } |