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