]>
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 | */ | |
948955a2 | 19 | #include <lxc/lxccontainer.h> |
72d0e1cb SG |
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> | |
95ee490b | 28 | #include <string.h> |
83319023 | 29 | |
f2363e38 | 30 | #include "lxc/state.h" |
83319023 | 31 | #include "lxctest.h" |
6f6c71cd CB |
32 | #include "utils.h" |
33 | ||
34 | #ifndef HAVE_STRLCPY | |
35 | #include "include/strlcpy.h" | |
36 | #endif | |
72d0e1cb SG |
37 | |
38 | #define MYNAME "lxctest1" | |
39 | ||
40 | int main(int argc, char *argv[]) | |
41 | { | |
6f6c71cd CB |
42 | int fd_log, ret; |
43 | struct lxc_container *c = NULL; | |
7edd0540 | 44 | int fret = EXIT_FAILURE; |
72d0e1cb | 45 | char v1[2], v2[256], v3[2048]; |
6f6c71cd CB |
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; | |
72d0e1cb | 64 | |
afeecbba | 65 | if ((c = lxc_container_new("testxyz", NULL)) == NULL) { |
72d0e1cb | 66 | fprintf(stderr, "%d: error opening lxc_container %s\n", __LINE__, MYNAME); |
d479e1f7 | 67 | exit(EXIT_FAILURE); |
72d0e1cb SG |
68 | } |
69 | ||
6fba98b5 CB |
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"); | |
83319023 CB |
73 | goto out; |
74 | } | |
b947e657 | 75 | |
6fba98b5 | 76 | ret = c->get_config_item(c, "lxc.log.syslog", v2, 255); |
83319023 | 77 | if (ret < 0) { |
6fba98b5 | 78 | lxc_error("Failed to retrieve lxc.log.syslog: %d.\n", ret); |
83319023 CB |
79 | goto out; |
80 | } | |
b947e657 | 81 | |
83319023 CB |
82 | if (strcmp(v2, "local0") != 0) { |
83 | lxc_error("Expected: local0 == %s.\n", v2); | |
84 | goto out; | |
85 | } | |
6fba98b5 | 86 | lxc_debug("Retrieving value for lxc.log.syslog correctly returned: %s.\n", v2); |
83319023 | 87 | |
6fba98b5 CB |
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"); | |
83319023 CB |
91 | goto out; |
92 | } | |
6fba98b5 | 93 | lxc_debug("%s\n", "Successfully failed to set lxc.log.syslog to invalid value.\n"); |
83319023 | 94 | |
72d0e1cb SG |
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__); | |
72d0e1cb SG |
97 | goto out; |
98 | } | |
b947e657 | 99 | |
72d0e1cb SG |
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); | |
72d0e1cb SG |
103 | goto out; |
104 | } | |
105 | fprintf(stderr, "lxc.hook.pre-start returned %d %s\n", ret, v2); | |
106 | ||
6fba98b5 | 107 | ret = c->get_config_item(c, "lxc.net", v2, 255); |
72d0e1cb SG |
108 | if (ret < 0) { |
109 | fprintf(stderr, "%d: get_config_item returned %d\n", __LINE__, ret); | |
72d0e1cb SG |
110 | goto out; |
111 | } | |
6fba98b5 | 112 | fprintf(stderr, "%d: get_config_item(lxc.net) returned %d %s\n", __LINE__, ret, v2); |
fe1c5887 | 113 | |
6fba98b5 | 114 | if (!c->set_config_item(c, "lxc.tty.max", "4")) { |
72d0e1cb | 115 | fprintf(stderr, "%d: failed to set tty\n", __LINE__); |
72d0e1cb SG |
116 | goto out; |
117 | } | |
b947e657 | 118 | |
6fba98b5 | 119 | ret = c->get_config_item(c, "lxc.tty.max", v2, 255); |
72d0e1cb SG |
120 | if (ret < 0) { |
121 | fprintf(stderr, "%d: get_config_item(lxc.tty) returned %d\n", __LINE__, ret); | |
72d0e1cb SG |
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__); | |
72d0e1cb SG |
128 | goto out; |
129 | } | |
b947e657 | 130 | |
72d0e1cb SG |
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); | |
72d0e1cb SG |
134 | goto out; |
135 | } | |
136 | printf("lxc.arch returned %d %s\n", ret, v2); | |
137 | ||
6fba98b5 | 138 | if (!c->set_config_item(c, "lxc.init.uid", "100")) { |
68d18db8 | 139 | fprintf(stderr, "%d: failed to set init_uid\n", __LINE__); |
68d18db8 PT |
140 | goto out; |
141 | } | |
b947e657 | 142 | |
6fba98b5 | 143 | ret = c->get_config_item(c, "lxc.init.uid", v2, 255); |
68d18db8 PT |
144 | if (ret < 0) { |
145 | fprintf(stderr, "%d: get_config_item(lxc.init_uid) returned %d\n", __LINE__, ret); | |
68d18db8 PT |
146 | goto out; |
147 | } | |
148 | printf("lxc.init_uid returned %d %s\n", ret, v2); | |
149 | ||
6fba98b5 | 150 | if (!c->set_config_item(c, "lxc.init.gid", "100")) { |
68d18db8 | 151 | fprintf(stderr, "%d: failed to set init_gid\n", __LINE__); |
68d18db8 PT |
152 | goto out; |
153 | } | |
b947e657 | 154 | |
6fba98b5 | 155 | ret = c->get_config_item(c, "lxc.init.gid", v2, 255); |
68d18db8 PT |
156 | if (ret < 0) { |
157 | fprintf(stderr, "%d: get_config_item(lxc.init_gid) returned %d\n", __LINE__, ret); | |
68d18db8 PT |
158 | goto out; |
159 | } | |
160 | printf("lxc.init_gid returned %d %s\n", ret, v2); | |
161 | ||
6f6c71cd CB |
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__); | |
bf31b337 RJ |
164 | goto out; |
165 | } | |
166 | ||
6f6c71cd CB |
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__); | |
bf31b337 RJ |
169 | goto out; |
170 | } | |
171 | ||
6f6c71cd CB |
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__); | |
bf31b337 RJ |
185 | goto out; |
186 | } | |
187 | ||
188 | ret = c->get_config_item(c, "lxc.init.groups", v2, 255); | |
189 | if (ret < 0) { | |
6f6c71cd | 190 | fprintf(stderr, "%d: failed to get lxc.init.groups\n", __LINE__); |
bf31b337 RJ |
191 | goto out; |
192 | } | |
6f6c71cd CB |
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); | |
bf31b337 RJ |
211 | if (ret != 0) { |
212 | goto out; | |
213 | } | |
214 | ||
72d0e1cb SG |
215 | #define HNAME "hostname1" |
216 | // demonstrate proper usage: | |
217 | char *alloced; | |
b947e657 | 218 | int len; |
219 | ||
6fba98b5 | 220 | if (!c->set_config_item(c, "lxc.uts.name", HNAME)) { |
72d0e1cb | 221 | fprintf(stderr, "%d: failed to set utsname\n", __LINE__); |
72d0e1cb SG |
222 | goto out; |
223 | } | |
224 | ||
6fba98b5 | 225 | len = c->get_config_item(c, "lxc.uts.name", NULL, 0); // query the size of the string |
72d0e1cb SG |
226 | if (len < 0) { |
227 | fprintf(stderr, "%d: get_config_item(lxc.utsname) returned %d\n", __LINE__, len); | |
72d0e1cb SG |
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); | |
72d0e1cb SG |
236 | goto out; |
237 | } | |
b947e657 | 238 | |
72d0e1cb SG |
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 | |
6fba98b5 | 241 | ret = c->get_config_item(c, "lxc.uts.name", alloced, len+1); |
72d0e1cb SG |
242 | if (ret < 0) { |
243 | fprintf(stderr, "%d: get_config_item(lxc.utsname) returned %d\n", __LINE__, ret); | |
72d0e1cb SG |
244 | goto out; |
245 | } | |
b947e657 | 246 | |
72d0e1cb SG |
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); | |
72d0e1cb SG |
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__); | |
72d0e1cb SG |
256 | goto out; |
257 | } | |
b947e657 | 258 | |
72d0e1cb SG |
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); | |
72d0e1cb SG |
262 | goto out; |
263 | } | |
264 | printf("lxc.mount.entry returned %d %s\n", ret, v2); | |
265 | ||
6fba98b5 | 266 | ret = c->get_config_item(c, "lxc.prlimit", v3, 2047); |
fe17b651 WB |
267 | if (ret != 0) { |
268 | fprintf(stderr, "%d: get_config_item(limit) returned %d\n", __LINE__, ret); | |
269 | goto out; | |
270 | } | |
271 | ||
6fba98b5 | 272 | if (!c->set_config_item(c, "lxc.prlimit.nofile", "1234:unlimited")) { |
fe17b651 WB |
273 | fprintf(stderr, "%d: failed to set limit.nofile\n", __LINE__); |
274 | goto out; | |
275 | } | |
b947e657 | 276 | |
6fba98b5 | 277 | ret = c->get_config_item(c, "lxc.prlimit.nofile", v2, 255); |
fe17b651 | 278 | if (ret < 0) { |
6fba98b5 | 279 | fprintf(stderr, "%d: get_config_item(lxc.prlimit.nofile) returned %d\n", __LINE__, ret); |
fe17b651 WB |
280 | goto out; |
281 | } | |
b947e657 | 282 | |
fe17b651 | 283 | if (strcmp(v2, "1234:unlimited")) { |
6fba98b5 | 284 | fprintf(stderr, "%d: lxc.prlimit.nofile returned wrong value: %d %s not 14 1234:unlimited\n", __LINE__, ret, v2); |
fe17b651 WB |
285 | goto out; |
286 | } | |
6fba98b5 | 287 | printf("lxc.prlimit.nofile returned %d %s\n", ret, v2); |
fe17b651 | 288 | |
6fba98b5 | 289 | if (!c->set_config_item(c, "lxc.prlimit.stack", "unlimited")) { |
fe17b651 WB |
290 | fprintf(stderr, "%d: failed to set limit.stack\n", __LINE__); |
291 | goto out; | |
292 | } | |
b947e657 | 293 | |
6fba98b5 | 294 | ret = c->get_config_item(c, "lxc.prlimit.stack", v2, 255); |
fe17b651 | 295 | if (ret < 0) { |
6fba98b5 | 296 | fprintf(stderr, "%d: get_config_item(lxc.prlimit.stack) returned %d\n", __LINE__, ret); |
fe17b651 WB |
297 | goto out; |
298 | } | |
b947e657 | 299 | |
fe17b651 | 300 | if (strcmp(v2, "unlimited")) { |
6fba98b5 | 301 | fprintf(stderr, "%d: lxc.prlimit.stack returned wrong value: %d %s not 9 unlimited\n", __LINE__, ret, v2); |
fe17b651 WB |
302 | goto out; |
303 | } | |
6fba98b5 | 304 | printf("lxc.prlimit.stack returned %d %s\n", ret, v2); |
fe17b651 | 305 | |
6fba98b5 CB |
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); | |
fe17b651 WB |
309 | if (ret != sizeof(ALL_LIMITS)-1) { |
310 | fprintf(stderr, "%d: get_config_item(limit) returned %d\n", __LINE__, ret); | |
311 | goto out; | |
312 | } | |
b947e657 | 313 | |
fe17b651 | 314 | if (strcmp(v3, ALL_LIMITS)) { |
6fba98b5 | 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); |
fe17b651 WB |
316 | goto out; |
317 | } | |
6fba98b5 | 318 | printf("lxc.prlimit returned %d %s\n", ret, v3); |
fe17b651 | 319 | |
6fba98b5 | 320 | if (!c->clear_config_item(c, "lxc.prlimit.nofile")) { |
fe17b651 WB |
321 | fprintf(stderr, "%d: failed clearing limit.nofile\n", __LINE__); |
322 | goto out; | |
323 | } | |
b947e657 | 324 | |
6fba98b5 | 325 | ret = c->get_config_item(c, "lxc.prlimit", v3, 2047); |
fe17b651 WB |
326 | if (ret != sizeof(LIMIT_STACK)-1) { |
327 | fprintf(stderr, "%d: get_config_item(limit) returned %d\n", __LINE__, ret); | |
328 | goto out; | |
329 | } | |
b947e657 | 330 | |
fe17b651 | 331 | if (strcmp(v3, LIMIT_STACK)) { |
6fba98b5 | 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); |
fe17b651 WB |
333 | goto out; |
334 | } | |
6fba98b5 | 335 | printf("lxc.prlimit returned %d %s\n", ret, v3); |
fe17b651 | 336 | |
7edd0540 L |
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 | } | |
b947e657 | 350 | |
7edd0540 L |
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 | } | |
b947e657 | 356 | |
7edd0540 L |
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 | } | |
b947e657 | 367 | |
7edd0540 L |
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 | } | |
b947e657 | 373 | |
7edd0540 L |
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 | } | |
b947e657 | 385 | |
7edd0540 L |
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 | } | |
b947e657 | 396 | |
7edd0540 L |
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 | } | |
b947e657 | 402 | |
7edd0540 L |
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 | ||
61d7a733 YT |
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 | } | |
b947e657 | 428 | |
61d7a733 YT |
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 | } | |
b947e657 | 445 | |
61d7a733 YT |
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 | } | |
b947e657 | 457 | |
61d7a733 YT |
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 | } | |
b947e657 | 468 | |
61d7a733 YT |
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 | } | |
b947e657 | 474 | |
61d7a733 YT |
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 | ||
6fba98b5 | 481 | if (!c->set_config_item(c, "lxc.apparmor.profile", "unconfined")) { |
72d0e1cb | 482 | fprintf(stderr, "%d: failed to set aa_profile\n", __LINE__); |
72d0e1cb SG |
483 | goto out; |
484 | } | |
b947e657 | 485 | |
6fba98b5 | 486 | ret = c->get_config_item(c, "lxc.apparmor.profile", v2, 255); |
72d0e1cb SG |
487 | if (ret < 0) { |
488 | fprintf(stderr, "%d: get_config_item(lxc.aa_profile) returned %d\n", __LINE__, ret); | |
72d0e1cb SG |
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 | |
afeecbba | 496 | if ((c = lxc_container_new(MYNAME, NULL)) == NULL) { |
72d0e1cb | 497 | fprintf(stderr, "%d: error opening lxc_container %s\n", __LINE__, MYNAME); |
72d0e1cb SG |
498 | goto out; |
499 | } | |
500 | c->destroy(c); | |
501 | lxc_container_put(c); | |
502 | ||
afeecbba | 503 | if ((c = lxc_container_new(MYNAME, NULL)) == NULL) { |
72d0e1cb | 504 | fprintf(stderr, "%d: error opening lxc_container %s\n", __LINE__, MYNAME); |
72d0e1cb SG |
505 | goto out; |
506 | } | |
b947e657 | 507 | |
787c3ebe | 508 | if (!c->createl(c, "busybox", NULL, NULL, 0, NULL)) { |
e403a064 | 509 | fprintf(stderr, "%d: failed to create a trusty container\n", __LINE__); |
72d0e1cb SG |
510 | goto out; |
511 | } | |
72d0e1cb SG |
512 | lxc_container_put(c); |
513 | ||
514 | /* XXX TODO load_config needs to clear out any old config first */ | |
afeecbba | 515 | if ((c = lxc_container_new(MYNAME, NULL)) == NULL) { |
72d0e1cb | 516 | fprintf(stderr, "%d: error opening lxc_container %s\n", __LINE__, MYNAME); |
72d0e1cb SG |
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); | |
72d0e1cb SG |
523 | goto out; |
524 | } | |
b947e657 | 525 | |
72d0e1cb SG |
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); | |
72d0e1cb SG |
529 | goto out; |
530 | } | |
b947e657 | 531 | |
72d0e1cb SG |
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); | |
72d0e1cb SG |
535 | goto out; |
536 | } | |
537 | printf("%d: get_config_item(lxc.cap.drop) returned %d %s\n", __LINE__, ret, v2); | |
b947e657 | 538 | |
6fba98b5 | 539 | ret = c->get_config_item(c, "lxc.net", v2, 255); |
72d0e1cb | 540 | if (ret < 0) { |
6fba98b5 | 541 | fprintf(stderr, "%d: get_config_item(lxc.net) returned %d\n", __LINE__, ret); |
72d0e1cb SG |
542 | goto out; |
543 | } | |
6fba98b5 | 544 | printf("%d: get_config_item(lxc.net) returned %d %s\n", __LINE__, ret, v2); |
72d0e1cb | 545 | |
6fba98b5 CB |
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__); | |
935cfd3e | 548 | goto out; |
549 | } | |
b947e657 | 550 | |
6fba98b5 | 551 | if (!c->set_config_item(c, "lxc.net.0.link", "lxcbr0")) { |
935cfd3e | 552 | fprintf(stderr, "%d: failed to set network.link\n", __LINE__); |
553 | goto out; | |
554 | } | |
b947e657 | 555 | |
6fba98b5 | 556 | if (!c->set_config_item(c, "lxc.net.0.flags", "up")) { |
935cfd3e | 557 | fprintf(stderr, "%d: failed to set network.flags\n", __LINE__); |
558 | goto out; | |
559 | } | |
b947e657 | 560 | |
6fba98b5 | 561 | if (!c->set_config_item(c, "lxc.net.0.hwaddr", "00:16:3e:xx:xx:xx")) { |
935cfd3e | 562 | fprintf(stderr, "%d: failed to set network.hwaddr\n", __LINE__); |
563 | goto out; | |
564 | } | |
b947e657 | 565 | |
6fba98b5 | 566 | if (!c->set_config_item(c, "lxc.net.0.ipv4.address", "10.2.3.4")) { |
72d0e1cb | 567 | fprintf(stderr, "%d: failed to set ipv4\n", __LINE__); |
72d0e1cb SG |
568 | goto out; |
569 | } | |
570 | ||
6fba98b5 | 571 | ret = c->get_config_item(c, "lxc.net.0.ipv4.address", v2, 255); |
72d0e1cb | 572 | if (ret <= 0) { |
6fba98b5 | 573 | fprintf(stderr, "%d: lxc.net.0.ipv4 returned %d\n", __LINE__, ret); |
72d0e1cb SG |
574 | goto out; |
575 | } | |
b947e657 | 576 | |
6fba98b5 | 577 | if (!c->clear_config_item(c, "lxc.net.0.ipv4.address")) { |
72d0e1cb | 578 | fprintf(stderr, "%d: failed clearing all ipv4 entries\n", __LINE__); |
72d0e1cb SG |
579 | goto out; |
580 | } | |
b947e657 | 581 | |
6fba98b5 | 582 | ret = c->get_config_item(c, "lxc.net.0.ipv4.address", v2, 255); |
72d0e1cb SG |
583 | if (ret != 0) { |
584 | fprintf(stderr, "%d: after clearing ipv4 entries get_item(lxc.network.0.ipv4 returned %d\n", __LINE__, ret); | |
72d0e1cb SG |
585 | goto out; |
586 | } | |
587 | ||
6fba98b5 | 588 | if (!c->set_config_item(c, "lxc.net.0.ipv4.gateway", "10.2.3.254")) { |
9eaf8a59 | 589 | fprintf(stderr, "%d: failed to set ipv4.gateway\n", __LINE__); |
9eaf8a59 SJ |
590 | goto out; |
591 | } | |
592 | ||
6fba98b5 | 593 | ret = c->get_config_item(c, "lxc.net.0.ipv4.gateway", v2, 255); |
9eaf8a59 | 594 | if (ret <= 0) { |
6fba98b5 | 595 | fprintf(stderr, "%d: lxc.net.0.ipv4.gateway returned %d\n", __LINE__, ret); |
9eaf8a59 SJ |
596 | goto out; |
597 | } | |
b947e657 | 598 | |
6fba98b5 | 599 | if (!c->set_config_item(c, "lxc.net.0.ipv4.gateway", "")) { |
9eaf8a59 | 600 | fprintf(stderr, "%d: failed clearing ipv4.gateway\n", __LINE__); |
9eaf8a59 SJ |
601 | goto out; |
602 | } | |
b947e657 | 603 | |
6fba98b5 | 604 | ret = c->get_config_item(c, "lxc.net.0.ipv4.gateway", v2, 255); |
9eaf8a59 SJ |
605 | if (ret != 0) { |
606 | fprintf(stderr, "%d: after clearing ipv4.gateway get_item(lxc.network.0.ipv4.gateway returned %d\n", __LINE__, ret); | |
9eaf8a59 SJ |
607 | goto out; |
608 | } | |
609 | ||
6fba98b5 | 610 | ret = c->get_config_item(c, "lxc.net.0.link", v2, 255); |
72d0e1cb SG |
611 | if (ret < 0) { |
612 | fprintf(stderr, "%d: get_config_item returned %d\n", __LINE__, ret); | |
72d0e1cb SG |
613 | goto out; |
614 | } | |
615 | printf("%d: get_config_item (link) returned %d %s\n", __LINE__, ret, v2); | |
b947e657 | 616 | |
6fba98b5 | 617 | ret = c->get_config_item(c, "lxc.net.0.name", v2, 255); |
72d0e1cb SG |
618 | if (ret < 0) { |
619 | fprintf(stderr, "%d: get_config_item returned %d\n", __LINE__, ret); | |
72d0e1cb SG |
620 | goto out; |
621 | } | |
622 | printf("%d: get_config_item (name) returned %d %s\n", __LINE__, ret, v2); | |
623 | ||
6fba98b5 | 624 | if (!c->clear_config_item(c, "lxc.net")) { |
72d0e1cb | 625 | fprintf(stderr, "%d: clear_config_item failed\n", __LINE__); |
72d0e1cb SG |
626 | goto out; |
627 | } | |
b947e657 | 628 | |
6fba98b5 | 629 | ret = c->get_config_item(c, "lxc.net", v2, 255); |
72d0e1cb SG |
630 | if (ret != 0) { |
631 | fprintf(stderr, "%d: network was not actually cleared (get_network returned %d)\n", __LINE__, ret); | |
72d0e1cb SG |
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); | |
72d0e1cb SG |
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); | |
72d0e1cb SG |
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")) { | |
03fadd16 | 650 | fprintf(stderr, "%d: failed clearing lxc.cgroup\n", __LINE__); |
72d0e1cb SG |
651 | goto out; |
652 | } | |
b947e657 | 653 | |
72d0e1cb | 654 | if (!c->clear_config_item(c, "lxc.cap.drop")) { |
03fadd16 | 655 | fprintf(stderr, "%d: failed clearing lxc.cap.drop\n", __LINE__); |
72d0e1cb SG |
656 | goto out; |
657 | } | |
b947e657 | 658 | |
6a564066 SG |
659 | if (!c->clear_config_item(c, "lxc.mount.entry")) { |
660 | fprintf(stderr, "%d: failed clearing lxc.mount.entry\n", __LINE__); | |
72d0e1cb SG |
661 | goto out; |
662 | } | |
b947e657 | 663 | |
72d0e1cb | 664 | if (!c->clear_config_item(c, "lxc.hook")) { |
03fadd16 | 665 | fprintf(stderr, "%d: failed clearing lxc.hook\n", __LINE__); |
72d0e1cb SG |
666 | goto out; |
667 | } | |
add40e62 CB |
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 | ||
6eb516a7 RJ |
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 | ||
646e6c8b CB |
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 | ||
72d0e1cb | 690 | printf("All get_item tests passed\n"); |
7edd0540 | 691 | fret = EXIT_SUCCESS; |
b947e657 | 692 | |
72d0e1cb | 693 | out: |
5090cb06 DJ |
694 | if (c) { |
695 | c->destroy(c); | |
696 | lxc_container_put(c); | |
697 | } | |
6f6c71cd CB |
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); | |
5090cb06 | 709 | |
7edd0540 | 710 | exit(fret); |
d479e1f7 | 711 | } |