]>
Commit | Line | Data |
---|---|---|
9c61a6d6 CB |
1 | /* liblxcapi |
2 | * | |
3 | * Copyright © 2017 Christian Brauner <christian.brauner@ubuntu.com>. | |
4 | * Copyright © 2017 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 <stdlib.h> | |
25 | #include <sys/types.h> | |
26 | #include <sys/wait.h> | |
27 | #include <stdlib.h> | |
28 | #include <errno.h> | |
29 | #include <string.h> | |
30 | ||
31 | #include "lxc/state.h" | |
32 | #include "lxctest.h" | |
33 | ||
34 | #define MYNAME "lxctest1" | |
35 | ||
36 | static int set_clear_save_and_load(struct lxc_container *c, const char *key, | |
37 | const char *value, const char *config_file) | |
38 | { | |
39 | char retval[4096] = {0}; | |
40 | int ret; | |
41 | ||
42 | if (!c->set_config_item(c, key, value)) { | |
43 | lxc_error("failed to set config item \"%s\" to \"%s\"\n", key, | |
44 | value); | |
45 | return -1; | |
46 | } | |
47 | ||
48 | ret = c->get_config_item(c, key, retval, sizeof(retval)); | |
49 | if (ret < 0) { | |
50 | lxc_error("failed to get config item \"%s\"\n", key); | |
51 | return -1; | |
52 | } | |
53 | ||
54 | if (config_file) { | |
55 | if (!c->save_config(c, config_file)) { | |
56 | lxc_error("%s\n", "failed to save config file"); | |
57 | return -1; | |
58 | } | |
59 | ||
60 | c->clear_config(c); | |
61 | c->lxc_conf = NULL; | |
62 | ||
63 | if (!c->load_config(c, config_file)) { | |
64 | lxc_error("%s\n", "failed to load config file"); | |
65 | return -1; | |
66 | } | |
67 | } | |
68 | ||
69 | if (!c->clear_config_item(c, key)) { | |
70 | lxc_error("failed to clear config item \"%s\"\n", key); | |
71 | return -1; | |
72 | } | |
73 | ||
74 | if (config_file) { | |
75 | if (!c->save_config(c, config_file)) { | |
76 | lxc_error("%s\n", "failed to save config file"); | |
77 | return -1; | |
78 | } | |
79 | ||
80 | c->clear_config(c); | |
81 | c->lxc_conf = NULL; | |
82 | ||
83 | if (!c->load_config(c, config_file)) { | |
84 | lxc_error("%s\n", "failed to load config file"); | |
85 | return -1; | |
86 | } | |
87 | } | |
88 | ||
89 | c->clear_config(c); | |
90 | c->lxc_conf = NULL; | |
91 | ||
92 | return 0; | |
93 | } | |
94 | ||
95 | int main(int argc, char *argv[]) | |
96 | { | |
97 | struct lxc_container *c; | |
98 | int fd = -1; | |
99 | int ret = EXIT_FAILURE; | |
100 | char tmpf[] = "lxc-parse-config-file-XXXXXX"; | |
101 | char retval[4096] = {0}; | |
102 | ||
103 | c = lxc_container_new("lxc-parse-config-file-testxyz", NULL); | |
104 | if (!c) { | |
105 | lxc_error("%s\n", "failed to create new container"); | |
106 | exit(EXIT_FAILURE); | |
107 | } | |
108 | ||
109 | fd = mkstemp(tmpf); | |
110 | if (fd < 0) { | |
111 | lxc_error("%s\n", "Could not create temporary file"); | |
112 | goto non_test_error; | |
113 | } | |
114 | close(fd); | |
115 | ||
116 | /* lxc.arch */ | |
117 | if (set_clear_save_and_load(c, "lxc.arch", "x86_64", tmpf) < 0) { | |
118 | lxc_error("%s\n", "lxc.arch"); | |
119 | goto non_test_error; | |
120 | } | |
121 | ||
122 | /* lxc.pts */ | |
123 | if (set_clear_save_and_load(c, "lxc.pts", "1000", tmpf) < 0) { | |
124 | lxc_error("%s\n", "lxc.pts"); | |
125 | goto non_test_error; | |
126 | } | |
127 | ||
128 | /* lxc.tty */ | |
129 | if (set_clear_save_and_load(c, "lxc.tty", "4", tmpf) < 0) { | |
130 | lxc_error("%s\n", "lxc.tty"); | |
131 | goto non_test_error; | |
132 | } | |
133 | ||
134 | /* lxc.devttydir */ | |
135 | if (set_clear_save_and_load(c, "lxc.devttydir", "not-dev", tmpf) < 0) { | |
136 | lxc_error("%s\n", "lxc.devttydir"); | |
137 | goto non_test_error; | |
138 | } | |
139 | ||
140 | /* lxc.kmsg */ | |
141 | if (set_clear_save_and_load(c, "lxc.kmsg", "1", tmpf) < 0) { | |
142 | lxc_error("%s\n", "lxc.kmsg"); | |
143 | goto non_test_error; | |
144 | } | |
145 | ||
146 | /* lxc.aa_profile */ | |
147 | if (set_clear_save_and_load(c, "lxc.aa_profile", "unconfined", tmpf) < | |
148 | 0) { | |
149 | lxc_error("%s\n", "lxc.aa_profile"); | |
150 | goto non_test_error; | |
151 | } | |
152 | ||
153 | /* lxc.aa_allow_incomplete */ | |
154 | if (set_clear_save_and_load(c, "lxc.aa_allow_incomplete", "1", tmpf) < 0) { | |
155 | lxc_error("%s\n", "lxc.aa_allow_incomplete"); | |
156 | goto non_test_error; | |
157 | } | |
158 | ||
159 | /* lxc.cgroup.cpuset.cpus */ | |
160 | if (set_clear_save_and_load(c, "lxc.cgroup.cpuset.cpus", "1-100", | |
161 | tmpf) < 0) { | |
162 | lxc_error("%s\n", "lxc.cgroup.cpuset.cpus"); | |
163 | goto non_test_error; | |
164 | } | |
165 | ||
166 | /* lxc.cgroup */ | |
167 | if (!c->set_config_item(c, "lxc.cgroup.cpuset.cpus", "1-100")) { | |
168 | lxc_error("%s\n", "failed to set config item " | |
169 | "\"lxc.cgroup.cpuset.cpus\" to \"1-100\""); | |
170 | return -1; | |
171 | } | |
172 | ||
173 | if (!c->set_config_item(c, "lxc.cgroup.memory.limit_in_bytes", | |
174 | "123456789")) { | |
175 | lxc_error( | |
176 | "%s\n", | |
177 | "failed to set config item " | |
178 | "\"lxc.cgroup.memory.limit_in_bytes\" to \"123456789\""); | |
179 | return -1; | |
180 | } | |
181 | ||
182 | if (!c->get_config_item(c, "lxc.cgroup", retval, sizeof(retval))) { | |
183 | lxc_error("%s\n", "failed to get config item \"lxc.cgroup\""); | |
184 | return -1; | |
185 | } | |
186 | ||
187 | c->clear_config(c); | |
188 | c->lxc_conf = NULL; | |
189 | ||
190 | /* lxc.id_map | |
191 | * We can't really save the config here since save_config() wants to | |
192 | * chown the container's directory but we haven't created an on-disk | |
193 | * container. So let's test set-get-clear. | |
194 | */ | |
195 | if (set_clear_save_and_load(c, "lxc.id_map", "u 0 100000 1000000000", | |
196 | NULL) < 0) { | |
197 | lxc_error("%s\n", "lxc.id_map"); | |
198 | goto non_test_error; | |
199 | } | |
200 | ||
201 | if (!c->set_config_item(c, "lxc.id_map", "u 1 100000 10000000")) { | |
202 | lxc_error("%s\n", "failed to set config item " | |
203 | "\"lxc.id_map\" to \"u 1 100000 10000000\""); | |
204 | return -1; | |
205 | } | |
206 | ||
207 | if (!c->set_config_item(c, "lxc.id_map", "g 1 100000 10000000")) { | |
208 | lxc_error("%s\n", "failed to set config item " | |
209 | "\"lxc.id_map\" to \"g 1 100000 10000000\""); | |
210 | return -1; | |
211 | } | |
212 | ||
213 | if (!c->get_config_item(c, "lxc.id_map", retval, sizeof(retval))) { | |
214 | lxc_error("%s\n", "failed to get config item \"lxc.cgroup\""); | |
215 | return -1; | |
216 | } | |
217 | ||
218 | c->clear_config(c); | |
219 | c->lxc_conf = NULL; | |
220 | ||
221 | /* lxc.loglevel */ | |
222 | if (set_clear_save_and_load(c, "lxc.loglevel", "debug", tmpf) < 0) { | |
223 | lxc_error("%s\n", "lxc.loglevel"); | |
224 | goto non_test_error; | |
225 | } | |
226 | ||
227 | /* lxc.logfile */ | |
228 | if (set_clear_save_and_load(c, "lxc.logfile", "/some/path", tmpf) < 0) { | |
229 | lxc_error("%s\n", "lxc.logfile"); | |
230 | goto non_test_error; | |
231 | } | |
232 | ||
233 | /* lxc.mount */ | |
234 | if (set_clear_save_and_load(c, "lxc.mount", "/some/path", NULL) < 0) { | |
235 | lxc_error("%s\n", "lxc.mount"); | |
236 | goto non_test_error; | |
237 | } | |
238 | ||
239 | /* lxc.mount.auto */ | |
240 | if (set_clear_save_and_load(c, "lxc.mount.auto", "proc:rw sys:rw cgroup-full:rw", tmpf) < 0) { | |
241 | lxc_error("%s\n", "lxc.mount.auto"); | |
242 | goto non_test_error; | |
243 | } | |
244 | ||
245 | /* lxc.mount.entry */ | |
246 | if (set_clear_save_and_load( | |
247 | c, "lxc.mount.entry", | |
248 | "/dev/dri dev/dri none bind,optional,create=dir", tmpf) < 0) { | |
249 | lxc_error("%s\n", "lxc.mount.entry"); | |
250 | goto non_test_error; | |
251 | } | |
252 | ||
253 | /* lxc.rootfs */ | |
254 | if (set_clear_save_and_load(c, "lxc.rootfs", "/some/path", tmpf) < 0) { | |
255 | lxc_error("%s\n", "lxc.rootfs"); | |
256 | goto non_test_error; | |
257 | } | |
258 | ||
259 | /* lxc.rootfs.mount */ | |
260 | if (set_clear_save_and_load(c, "lxc.rootfs.mount", "/some/path", tmpf) < 0) { | |
261 | lxc_error("%s\n", "lxc.rootfs.mount"); | |
262 | goto non_test_error; | |
263 | } | |
264 | ||
265 | /* lxc.rootfs.options */ | |
266 | if (set_clear_save_and_load(c, "lxc.rootfs.options", "ext4,discard", tmpf) < 0) { | |
267 | lxc_error("%s\n", "lxc.rootfs.options"); | |
268 | goto non_test_error; | |
269 | } | |
270 | ||
271 | /* lxc.rootfs.backend */ | |
272 | if (set_clear_save_and_load(c, "lxc.rootfs.backend", "btrfs", tmpf) < 0) { | |
273 | lxc_error("%s\n", "lxc.rootfs.backend"); | |
274 | goto non_test_error; | |
275 | } | |
276 | ||
277 | /* lxc.utsname */ | |
278 | if (set_clear_save_and_load(c, "lxc.utsname", "the-shire", tmpf) < 0) { | |
279 | lxc_error("%s\n", "lxc.utsname"); | |
280 | goto non_test_error; | |
281 | } | |
282 | ||
283 | /* lxc.hook.pre-start */ | |
284 | if (set_clear_save_and_load(c, "lxc.hook.pre-start", "/some/pre-start", tmpf) < 0) { | |
285 | lxc_error("%s\n", "lxc.hook.pre-start"); | |
286 | goto non_test_error; | |
287 | } | |
288 | ||
289 | /* lxc.hook.pre-mount */ | |
290 | if (set_clear_save_and_load(c, "lxc.hook.pre-mount", "/some/pre-mount", tmpf) < 0) { | |
291 | lxc_error("%s\n", "lxc.hook.pre-mount"); | |
292 | goto non_test_error; | |
293 | } | |
294 | ||
295 | /* lxc.hook.mount */ | |
296 | if (set_clear_save_and_load(c, "lxc.hook.mount", "/some/mount", tmpf) < 0) { | |
297 | lxc_error("%s\n", "lxc.hook.mount"); | |
298 | goto non_test_error; | |
299 | } | |
300 | ||
301 | /* lxc.hook.autodev */ | |
302 | if (set_clear_save_and_load(c, "lxc.hook.autodev", "/some/autodev", tmpf) < 0) { | |
303 | lxc_error("%s\n", "lxc.hook.autodev"); | |
304 | goto non_test_error; | |
305 | } | |
306 | ||
307 | /* lxc.hook.start */ | |
308 | if (set_clear_save_and_load(c, "lxc.hook.start", "/some/start", tmpf) < 0) { | |
309 | lxc_error("%s\n", "lxc.hook.start"); | |
310 | goto non_test_error; | |
311 | } | |
312 | ||
313 | /* lxc.hook.stop */ | |
314 | if (set_clear_save_and_load(c, "lxc.hook.stop", "/some/stop", tmpf) < 0) { | |
315 | lxc_error("%s\n", "lxc.hook.stop"); | |
316 | goto non_test_error; | |
317 | } | |
318 | ||
319 | /* lxc.hook.post-stop */ | |
320 | if (set_clear_save_and_load(c, "lxc.hook.post-stop", "/some/post-stop", tmpf) < 0) { | |
321 | lxc_error("%s\n", "lxc.hook.post-stop"); | |
322 | goto non_test_error; | |
323 | } | |
324 | ||
325 | /* lxc.hook.clone */ | |
326 | if (set_clear_save_and_load(c, "lxc.hook.clone", "/some/clone", tmpf) < 0) { | |
327 | lxc_error("%s\n", "lxc.hook.clone"); | |
328 | goto non_test_error; | |
329 | } | |
330 | ||
331 | /* lxc.hook.destroy */ | |
332 | if (set_clear_save_and_load(c, "lxc.hook.destroy", "/some/destroy", tmpf) < 0) { | |
333 | lxc_error("%s\n", "lxc.hook.destroy"); | |
334 | goto non_test_error; | |
335 | } | |
336 | ||
337 | /* lxc.cap.drop */ | |
338 | if (set_clear_save_and_load(c, "lxc.cap.drop", "sys_module mknod setuid net_raw", tmpf) < 0) { | |
339 | lxc_error("%s\n", "lxc.cap.drop"); | |
340 | goto non_test_error; | |
341 | } | |
342 | ||
343 | /* lxc.cap.keep */ | |
344 | if (set_clear_save_and_load(c, "lxc.cap.keep", "sys_module mknod setuid net_raw", tmpf) < 0) { | |
345 | lxc_error("%s\n", "lxc.cap.keep"); | |
346 | goto non_test_error; | |
347 | } | |
348 | ||
349 | /* lxc.console */ | |
350 | if (set_clear_save_and_load(c, "lxc.console", "none", tmpf) < 0) { | |
351 | lxc_error("%s\n", "lxc.console"); | |
352 | goto non_test_error; | |
353 | } | |
354 | ||
355 | /* lxc.console.logfile */ | |
356 | if (set_clear_save_and_load(c, "lxc.console.logfile", "/some/logfile", tmpf) < 0) { | |
357 | lxc_error("%s\n", "lxc.console.logfile"); | |
358 | goto non_test_error; | |
359 | } | |
360 | ||
361 | /* lxc.seccomp */ | |
362 | if (set_clear_save_and_load(c, "lxc.seccomp", "/some/seccomp/file", tmpf) < 0) { | |
363 | lxc_error("%s\n", "lxc.seccomp"); | |
364 | goto non_test_error; | |
365 | } | |
366 | ||
367 | /* lxc.autodev */ | |
368 | if (set_clear_save_and_load(c, "lxc.autodev", "1", tmpf) < 0) { | |
369 | lxc_error("%s\n", "lxc.autodev"); | |
370 | goto non_test_error; | |
371 | } | |
372 | ||
373 | /* lxc.haltsignal */ | |
374 | if (set_clear_save_and_load(c, "lxc.haltsignal", "1", tmpf) < 0) { | |
375 | lxc_error("%s\n", "lxc.haltsignal"); | |
376 | goto non_test_error; | |
377 | } | |
378 | ||
379 | /* lxc.rebootsignal */ | |
380 | if (set_clear_save_and_load(c, "lxc.rebootsignal", "1", tmpf) < 0) { | |
381 | lxc_error("%s\n", "lxc.rebootsignal"); | |
382 | goto non_test_error; | |
383 | } | |
384 | ||
385 | /* lxc.stopsignal */ | |
386 | if (set_clear_save_and_load(c, "lxc.stopsignal", "1", tmpf) < 0) { | |
387 | lxc_error("%s\n", "lxc.stopsignal"); | |
388 | goto non_test_error; | |
389 | } | |
390 | ||
391 | /* lxc.start.auto */ | |
392 | if (set_clear_save_and_load(c, "lxc.start.auto", "1", tmpf) < 0) { | |
393 | lxc_error("%s\n", "lxc.start.auto"); | |
394 | goto non_test_error; | |
395 | } | |
396 | ||
397 | /* lxc.start.delay */ | |
398 | if (set_clear_save_and_load(c, "lxc.start.delay", "5", tmpf) < 0) { | |
399 | lxc_error("%s\n", "lxc.start.delay"); | |
400 | goto non_test_error; | |
401 | } | |
402 | ||
403 | /* lxc.start.order */ | |
404 | if (set_clear_save_and_load(c, "lxc.start.order", "1", tmpf) < 0) { | |
405 | lxc_error("%s\n", "lxc.start.order"); | |
406 | goto non_test_error; | |
407 | } | |
408 | ||
409 | /* lxc.syslog */ | |
410 | if (set_clear_save_and_load(c, "lxc.syslog", "local0", tmpf) < 0) { | |
411 | lxc_error("%s\n", "lxc.syslog"); | |
412 | goto non_test_error; | |
413 | } | |
414 | ||
415 | /* lxc.utsname */ | |
416 | if (set_clear_save_and_load(c, "lxc.utsname", "get-schwifty", tmpf) < | |
417 | 0) { | |
418 | lxc_error("%s\n", "lxc.utsname"); | |
419 | goto non_test_error; | |
420 | } | |
421 | ||
422 | /* lxc.monitor.unshare */ | |
423 | if (set_clear_save_and_load(c, "lxc.monitor.unshare", "1", tmpf) < 0) { | |
424 | lxc_error("%s\n", "lxc.monitor.unshare"); | |
425 | goto non_test_error; | |
426 | } | |
427 | ||
428 | /* lxc.group */ | |
429 | if (set_clear_save_and_load(c, "lxc.group", "some,container,groups", tmpf) < 0) { | |
430 | lxc_error("%s\n", "lxc.group"); | |
431 | goto non_test_error; | |
432 | } | |
433 | ||
434 | /* lxc.environment */ | |
435 | if (set_clear_save_and_load(c, "lxc.environment", "FOO=BAR", tmpf) < 0) { | |
436 | lxc_error("%s\n", "lxc.environment"); | |
437 | goto non_test_error; | |
438 | } | |
439 | ||
440 | /* lxc.init_cmd */ | |
441 | if (set_clear_save_and_load(c, "lxc.init_cmd", "/bin/bash", tmpf) < 0) { | |
442 | lxc_error("%s\n", "lxc.init_cmd"); | |
443 | goto non_test_error; | |
444 | } | |
445 | ||
446 | /* lxc.init_uid */ | |
447 | if (set_clear_save_and_load(c, "lxc.init_uid", "1000", tmpf) < 0) { | |
448 | lxc_error("%s\n", "lxc.init_uid"); | |
449 | goto non_test_error; | |
450 | } | |
451 | ||
452 | /* lxc.init_gid */ | |
453 | if (set_clear_save_and_load(c, "lxc.init_gid", "1000", tmpf) < 0) { | |
454 | lxc_error("%s\n", "lxc.init_gid"); | |
455 | goto non_test_error; | |
456 | } | |
457 | ||
458 | /* lxc.ephemeral */ | |
459 | if (set_clear_save_and_load(c, "lxc.ephemeral", "1", tmpf) < 0) { | |
460 | lxc_error("%s\n", "lxc.ephemeral"); | |
461 | goto non_test_error; | |
462 | } | |
463 | ||
464 | /* lxc.no_new_privs */ | |
465 | if (set_clear_save_and_load(c, "lxc.no_new_privs", "1", tmpf) < 0) { | |
466 | lxc_error("%s\n", "lxc.no_new_privs"); | |
467 | goto non_test_error; | |
468 | } | |
469 | ||
470 | /* lxc.limit.nofile */ | |
471 | if (set_clear_save_and_load(c, "lxc.limit.nofile", "65536", tmpf) < 0) { | |
472 | lxc_error("%s\n", "lxc.limit.nofile"); | |
473 | goto non_test_error; | |
474 | } | |
475 | ||
476 | ret = EXIT_SUCCESS; | |
477 | non_test_error: | |
478 | c->destroy(c); | |
479 | lxc_container_put(c); | |
480 | exit(ret); | |
481 | } |