]>
Commit | Line | Data |
---|---|---|
72d0e1cb SG |
1 | /* liblxcapi |
2 | * | |
3 | * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>. | |
4 | * Copyright © 2012 Canonical Ltd. | |
5 | * | |
d75462e4 SH |
6 | * This library is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU Lesser General Public | |
8 | * License as published by the Free Software Foundation; either | |
9 | * version 2.1 of the License, or (at your option) any later version. | |
10 | ||
11 | * This library is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * Lesser General Public License for more details. | |
15 | ||
16 | * You should have received a copy of the GNU Lesser General Public | |
17 | * License along with this library; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
72d0e1cb SG |
19 | */ |
20 | ||
9be53773 | 21 | #define _GNU_SOURCE |
38683db4 | 22 | #include <dirent.h> |
9be53773 | 23 | #include <errno.h> |
93dc5327 | 24 | #include <fcntl.h> |
38683db4 | 25 | #include <grp.h> |
f2363e38 | 26 | #include <libgen.h> |
38683db4 CB |
27 | #include <pthread.h> |
28 | #include <sched.h> | |
29 | #include <stdarg.h> | |
d659597e | 30 | #include <stdint.h> |
9fc7f8c0 | 31 | #include <stdio.h> |
38683db4 CB |
32 | #include <unistd.h> |
33 | #include <arpa/inet.h> | |
66c1f8c2 | 34 | #include <sys/sysmacros.h> |
38683db4 CB |
35 | #include <sys/mman.h> |
36 | #include <sys/mount.h> | |
5f7eba0b | 37 | #include <sys/syscall.h> |
38683db4 CB |
38 | #include <sys/types.h> |
39 | #include <sys/wait.h> | |
f2363e38 | 40 | |
5e5576a4 | 41 | #include "af_unix.h" |
38683db4 | 42 | #include "attach.h" |
38683db4 | 43 | #include "cgroup.h" |
72d0e1cb | 44 | #include "conf.h" |
38683db4 CB |
45 | #include "config.h" |
46 | #include "commands.h" | |
92e35018 | 47 | #include "commands_utils.h" |
72d0e1cb | 48 | #include "confile.h" |
4222a9f4 | 49 | #include "confile_utils.h" |
b5159817 | 50 | #include "console.h" |
e29fe1dd | 51 | #include "criu.h" |
238b3e5e | 52 | #include "error.h" |
a6f151a7 | 53 | #include "initutils.h" |
72d0e1cb | 54 | #include "log.h" |
38683db4 CB |
55 | #include "lxc.h" |
56 | #include "lxccontainer.h" | |
57 | #include "lxclock.h" | |
f2363e38 ÇO |
58 | #include "monitor.h" |
59 | #include "namespace.h" | |
fed29fad | 60 | #include "network.h" |
9994d140 | 61 | #include "parse.h" |
aa460476 | 62 | #include "start.h" |
38683db4 | 63 | #include "state.h" |
28d832c4 | 64 | #include "storage.h" |
ec5d663c | 65 | #include "storage_utils.h" |
28d832c4 CB |
66 | #include "storage/btrfs.h" |
67 | #include "storage/overlay.h" | |
68 | #include "sync.h" | |
38683db4 CB |
69 | #include "utils.h" |
70 | #include "version.h" | |
4ba0d9af | 71 | |
af6824fc ST |
72 | /* major()/minor() */ |
73 | #ifdef MAJOR_IN_MKDEV | |
238b3e5e | 74 | #include <sys/mkdev.h> |
af6824fc | 75 | #endif |
af6824fc | 76 | |
4ba0d9af | 77 | #if HAVE_IFADDRS_H |
9c83a661 | 78 | #include <ifaddrs.h> |
4ba0d9af SG |
79 | #else |
80 | #include <../include/ifaddrs.h> | |
81 | #endif | |
72d0e1cb | 82 | |
684f79a5 SG |
83 | #if IS_BIONIC |
84 | #include <../include/lxcmntent.h> | |
85 | #else | |
86 | #include <mntent.h> | |
87 | #endif | |
88 | ||
5f7eba0b SG |
89 | /* Define faccessat() if missing from the C library */ |
90 | #ifndef HAVE_FACCESSAT | |
91 | static int faccessat(int __fd, const char *__file, int __type, int __flag) | |
92 | { | |
93 | #ifdef __NR_faccessat | |
238b3e5e | 94 | return syscall(__NR_faccessat, __fd, __file, __type, __flag); |
5f7eba0b | 95 | #else |
238b3e5e CB |
96 | errno = ENOSYS; |
97 | return -1; | |
5f7eba0b SG |
98 | #endif |
99 | } | |
100 | #endif | |
101 | ||
72d0e1cb SG |
102 | lxc_log_define(lxc_container, lxc); |
103 | ||
858377e4 SH |
104 | static bool do_lxcapi_destroy(struct lxc_container *c); |
105 | static const char *lxcapi_get_config_path(struct lxc_container *c); | |
106 | #define do_lxcapi_get_config_path(c) lxcapi_get_config_path(c) | |
107 | static bool do_lxcapi_set_config_item(struct lxc_container *c, const char *key, const char *v); | |
17a367d8 CB |
108 | static bool container_destroy(struct lxc_container *c, |
109 | struct lxc_storage *storage); | |
858377e4 SH |
110 | static bool get_snappath_dir(struct lxc_container *c, char *snappath); |
111 | static bool lxcapi_snapshot_destroy_all(struct lxc_container *c); | |
112 | static bool do_lxcapi_save_config(struct lxc_container *c, const char *alt_file); | |
113 | ||
a41f104b SH |
114 | static bool config_file_exists(const char *lxcpath, const char *cname) |
115 | { | |
ef1ab8f1 CB |
116 | int ret; |
117 | size_t len; | |
118 | char *fname; | |
a41f104b | 119 | |
ef1ab8f1 CB |
120 | /* $lxcpath + '/' + $cname + '/config' + \0 */ |
121 | len = strlen(lxcpath) + strlen(cname) + 9; | |
122 | fname = alloca(len); | |
123 | ret = snprintf(fname, len, "%s/%s/config", lxcpath, cname); | |
124 | if (ret < 0 || (size_t)ret >= len) | |
a41f104b SH |
125 | return false; |
126 | ||
127 | return file_exists(fname); | |
128 | } | |
129 | ||
444249ea CB |
130 | /* A few functions to help detect when a container creation failed. If a |
131 | * container creation was killed partway through, then trying to actually start | |
132 | * that container could harm the host. We detect this by creating a 'partial' | |
133 | * file under the container directory, and keeping an advisory lock. When | |
134 | * container creation completes, we remove that file. When we load or try to | |
135 | * start a container, if we find that file, without a flock, we remove the | |
136 | * container. | |
3e625e2d | 137 | */ |
74a3920a | 138 | static int ongoing_create(struct lxc_container *c) |
3e625e2d | 139 | { |
3e625e2d | 140 | int fd, ret; |
444249ea CB |
141 | size_t len; |
142 | char *path; | |
93dc5327 SH |
143 | struct flock lk; |
144 | ||
444249ea CB |
145 | len = strlen(c->config_path) + strlen(c->name) + 10; |
146 | path = alloca(len); | |
3e625e2d | 147 | ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name); |
444249ea | 148 | if (ret < 0 || (size_t)ret >= len) |
3e625e2d | 149 | return -1; |
3e625e2d SH |
150 | |
151 | if (!file_exists(path)) | |
152 | return 0; | |
444249ea | 153 | |
025ed0f3 | 154 | fd = open(path, O_RDWR); |
444249ea | 155 | if (fd < 0) |
3e625e2d | 156 | return 0; |
444249ea | 157 | |
93dc5327 SH |
158 | lk.l_type = F_WRLCK; |
159 | lk.l_whence = SEEK_SET; | |
160 | lk.l_start = 0; | |
161 | lk.l_len = 0; | |
162 | lk.l_pid = -1; | |
444249ea CB |
163 | |
164 | ret = fcntl(fd, F_GETLK, &lk); | |
165 | close(fd); | |
166 | if (ret == 0 && lk.l_pid != -1) { | |
1a0e70ac | 167 | /* create is still ongoing */ |
3e625e2d SH |
168 | return 1; |
169 | } | |
444249ea CB |
170 | |
171 | /* Create completed but partial is still there. */ | |
3e625e2d SH |
172 | return 2; |
173 | } | |
174 | ||
74a3920a | 175 | static int create_partial(struct lxc_container *c) |
3e625e2d | 176 | { |
3e625e2d | 177 | int fd, ret; |
f5cd0252 CB |
178 | size_t len; |
179 | char *path; | |
93dc5327 SH |
180 | struct flock lk; |
181 | ||
f5cd0252 CB |
182 | /* $lxcpath + '/' + $name + '/partial' + \0 */ |
183 | len = strlen(c->config_path) + strlen(c->name) + 10; | |
184 | path = alloca(len); | |
3e625e2d | 185 | ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name); |
f5cd0252 | 186 | if (ret < 0 || (size_t)ret >= len) |
3e625e2d | 187 | return -1; |
f5cd0252 CB |
188 | |
189 | fd = open(path, O_RDWR | O_CREAT | O_EXCL, 0755); | |
190 | if (fd < 0) | |
3e625e2d | 191 | return -1; |
f5cd0252 | 192 | |
93dc5327 SH |
193 | lk.l_type = F_WRLCK; |
194 | lk.l_whence = SEEK_SET; | |
195 | lk.l_start = 0; | |
196 | lk.l_len = 0; | |
f5cd0252 CB |
197 | |
198 | ret = fcntl(fd, F_SETLKW, &lk); | |
199 | if (ret < 0) { | |
200 | SYSERROR("Failed to lock partial file %s", path); | |
3e625e2d | 201 | close(fd); |
3e625e2d SH |
202 | return -1; |
203 | } | |
3e625e2d SH |
204 | |
205 | return fd; | |
206 | } | |
207 | ||
74a3920a | 208 | static void remove_partial(struct lxc_container *c, int fd) |
3e625e2d | 209 | { |
3e625e2d | 210 | int ret; |
a3740e80 CB |
211 | size_t len; |
212 | char *path; | |
3e625e2d SH |
213 | |
214 | close(fd); | |
a3740e80 CB |
215 | /* $lxcpath + '/' + $name + '/partial' + \0 */ |
216 | len = strlen(c->config_path) + strlen(c->name) + 10; | |
217 | path = alloca(len); | |
3e625e2d | 218 | ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name); |
a3740e80 | 219 | if (ret < 0 || (size_t)ret >= len) |
3e625e2d | 220 | return; |
a3740e80 CB |
221 | |
222 | ret = unlink(path); | |
223 | if (ret < 0) | |
224 | SYSERROR("Failed to remove partial file %s", path); | |
3e625e2d SH |
225 | } |
226 | ||
72d0e1cb | 227 | /* LOCKING |
3bc449ed SH |
228 | * 1. container_mem_lock(c) protects the struct lxc_container from multiple threads. |
229 | * 2. container_disk_lock(c) protects the on-disk container data - in particular the | |
230 | * container configuration file. | |
231 | * The container_disk_lock also takes the container_mem_lock. | |
232 | * 3. thread_mutex protects process data (ex: fd table) from multiple threads. | |
72d0e1cb SG |
233 | * NOTHING mutexes two independent programs with their own struct |
234 | * lxc_container for the same c->name, between API calls. For instance, | |
235 | * c->config_read(); c->start(); Between those calls, data on disk | |
236 | * could change (which shouldn't bother the caller unless for instance | |
237 | * the rootfs get moved). c->config_read(); update; c->config_write(); | |
238 | * Two such updaters could race. The callers should therefore check their | |
239 | * results. Trying to prevent that would necessarily expose us to deadlocks | |
240 | * due to hung callers. So I prefer to keep the locks only within our own | |
241 | * functions, not across functions. | |
242 | * | |
3bc449ed | 243 | * If you're going to clone while holding a lxccontainer, increment |
72d0e1cb SG |
244 | * c->numthreads (under privlock) before forking. When deleting, |
245 | * decrement numthreads under privlock, then if it hits 0 you can delete. | |
246 | * Do not ever use a lxccontainer whose numthreads you did not bump. | |
247 | */ | |
248 | ||
249 | static void lxc_container_free(struct lxc_container *c) | |
250 | { | |
251 | if (!c) | |
252 | return; | |
253 | ||
f10fad2f ME |
254 | free(c->configfile); |
255 | c->configfile = NULL; | |
70849dc2 | 256 | |
f10fad2f ME |
257 | free(c->error_string); |
258 | c->error_string = NULL; | |
70849dc2 | 259 | |
d95db067 | 260 | if (c->slock) { |
df271a59 | 261 | lxc_putlock(c->slock); |
d95db067 DE |
262 | c->slock = NULL; |
263 | } | |
70849dc2 | 264 | |
72d0e1cb | 265 | if (c->privlock) { |
df271a59 | 266 | lxc_putlock(c->privlock); |
72d0e1cb SG |
267 | c->privlock = NULL; |
268 | } | |
70849dc2 | 269 | |
f10fad2f ME |
270 | free(c->name); |
271 | c->name = NULL; | |
70849dc2 | 272 | |
d95db067 DE |
273 | if (c->lxc_conf) { |
274 | lxc_conf_free(c->lxc_conf); | |
275 | c->lxc_conf = NULL; | |
276 | } | |
70849dc2 | 277 | |
f10fad2f ME |
278 | free(c->config_path); |
279 | c->config_path = NULL; | |
72cf75fa | 280 | |
72d0e1cb SG |
281 | free(c); |
282 | } | |
283 | ||
43d1aa34 SH |
284 | /* |
285 | * Consider the following case: | |
286 | freer | racing get()er | |
287 | ================================================================== | |
288 | lxc_container_put() | lxc_container_get() | |
289 | \ lxclock(c->privlock) | c->numthreads < 1? (no) | |
290 | \ c->numthreads = 0 | \ lxclock(c->privlock) -> waits | |
291 | \ lxcunlock() | \ | |
292 | \ lxc_container_free() | \ lxclock() returns | |
293 | | \ c->numthreads < 1 -> return 0 | |
294 | \ \ (free stuff) | | |
295 | \ \ sem_destroy(privlock) | | |
296 | ||
297 | * When the get()er checks numthreads the first time, one of the following | |
298 | * is true: | |
299 | * 1. freer has set numthreads = 0. get() returns 0 | |
300 | * 2. freer is between lxclock and setting numthreads to 0. get()er will | |
301 | * sem_wait on privlock, get lxclock after freer() drops it, then see | |
302 | * numthreads is 0 and exit without touching lxclock again.. | |
303 | * 3. freer has not yet locked privlock. If get()er runs first, then put()er | |
304 | * will see --numthreads = 1 and not call lxc_container_free(). | |
305 | */ | |
306 | ||
72d0e1cb SG |
307 | int lxc_container_get(struct lxc_container *c) |
308 | { | |
309 | if (!c) | |
310 | return 0; | |
311 | ||
1a0e70ac CB |
312 | /* If someone else has already started freeing the container, don't try |
313 | * to take the lock, which may be invalid. | |
314 | */ | |
43d1aa34 SH |
315 | if (c->numthreads < 1) |
316 | return 0; | |
317 | ||
5cee8c50 | 318 | if (container_mem_lock(c)) |
72d0e1cb | 319 | return 0; |
1a0e70ac CB |
320 | |
321 | /* Bail without trying to unlock, bc the privlock is now probably in | |
322 | * freed memory. | |
323 | */ | |
324 | if (c->numthreads < 1) | |
72d0e1cb | 325 | return 0; |
1a0e70ac | 326 | |
72d0e1cb | 327 | c->numthreads++; |
5cee8c50 | 328 | container_mem_unlock(c); |
72d0e1cb SG |
329 | return 1; |
330 | } | |
331 | ||
332 | int lxc_container_put(struct lxc_container *c) | |
333 | { | |
334 | if (!c) | |
335 | return -1; | |
5cee8c50 | 336 | if (container_mem_lock(c)) |
72d0e1cb SG |
337 | return -1; |
338 | if (--c->numthreads < 1) { | |
5cee8c50 | 339 | container_mem_unlock(c); |
72d0e1cb SG |
340 | lxc_container_free(c); |
341 | return 1; | |
342 | } | |
5cee8c50 | 343 | container_mem_unlock(c); |
72d0e1cb SG |
344 | return 0; |
345 | } | |
346 | ||
858377e4 | 347 | static bool do_lxcapi_is_defined(struct lxc_container *c) |
72d0e1cb SG |
348 | { |
349 | struct stat statbuf; | |
350 | bool ret = false; | |
351 | int statret; | |
352 | ||
353 | if (!c) | |
354 | return false; | |
355 | ||
5cee8c50 | 356 | if (container_mem_lock(c)) |
72d0e1cb SG |
357 | return false; |
358 | if (!c->configfile) | |
359 | goto out; | |
360 | statret = stat(c->configfile, &statbuf); | |
361 | if (statret != 0) | |
362 | goto out; | |
363 | ret = true; | |
364 | ||
365 | out: | |
5cee8c50 | 366 | container_mem_unlock(c); |
72d0e1cb SG |
367 | return ret; |
368 | } | |
369 | ||
858377e4 SH |
370 | #define WRAP_API(rettype, fnname) \ |
371 | static rettype fnname(struct lxc_container *c) \ | |
372 | { \ | |
373 | rettype ret; \ | |
8164f0e2 TA |
374 | bool reset_config = false; \ |
375 | \ | |
376 | if (!current_config && c && c->lxc_conf) { \ | |
377 | current_config = c->lxc_conf; \ | |
378 | reset_config = true; \ | |
379 | } \ | |
380 | \ | |
858377e4 | 381 | ret = do_##fnname(c); \ |
8164f0e2 TA |
382 | if (reset_config) \ |
383 | current_config = NULL; \ | |
384 | \ | |
858377e4 SH |
385 | return ret; \ |
386 | } | |
387 | ||
388 | #define WRAP_API_1(rettype, fnname, t1) \ | |
389 | static rettype fnname(struct lxc_container *c, t1 a1) \ | |
390 | { \ | |
391 | rettype ret; \ | |
8164f0e2 TA |
392 | bool reset_config = false; \ |
393 | \ | |
394 | if (!current_config && c && c->lxc_conf) { \ | |
395 | current_config = c->lxc_conf; \ | |
396 | reset_config = true; \ | |
397 | } \ | |
398 | \ | |
858377e4 | 399 | ret = do_##fnname(c, a1); \ |
8164f0e2 TA |
400 | if (reset_config) \ |
401 | current_config = NULL; \ | |
402 | \ | |
858377e4 SH |
403 | return ret; \ |
404 | } | |
405 | ||
406 | #define WRAP_API_2(rettype, fnname, t1, t2) \ | |
407 | static rettype fnname(struct lxc_container *c, t1 a1, t2 a2) \ | |
408 | { \ | |
409 | rettype ret; \ | |
8164f0e2 TA |
410 | bool reset_config = false; \ |
411 | \ | |
412 | if (!current_config && c && c->lxc_conf) { \ | |
413 | current_config = c->lxc_conf; \ | |
414 | reset_config = true; \ | |
415 | } \ | |
416 | \ | |
858377e4 | 417 | ret = do_##fnname(c, a1, a2); \ |
8164f0e2 TA |
418 | if (reset_config) \ |
419 | current_config = NULL; \ | |
420 | \ | |
858377e4 SH |
421 | return ret; \ |
422 | } | |
423 | ||
424 | #define WRAP_API_3(rettype, fnname, t1, t2, t3) \ | |
425 | static rettype fnname(struct lxc_container *c, t1 a1, t2 a2, t3 a3) \ | |
426 | { \ | |
427 | rettype ret; \ | |
8164f0e2 TA |
428 | bool reset_config = false; \ |
429 | \ | |
430 | if (!current_config && c && c->lxc_conf) { \ | |
431 | current_config = c->lxc_conf; \ | |
432 | reset_config = true; \ | |
433 | } \ | |
434 | \ | |
858377e4 | 435 | ret = do_##fnname(c, a1, a2, a3); \ |
8164f0e2 TA |
436 | if (reset_config) \ |
437 | current_config = NULL; \ | |
438 | \ | |
858377e4 SH |
439 | return ret; \ |
440 | } | |
441 | ||
442 | WRAP_API(bool, lxcapi_is_defined) | |
443 | ||
444 | static const char *do_lxcapi_state(struct lxc_container *c) | |
72d0e1cb | 445 | { |
72d0e1cb SG |
446 | lxc_state_t s; |
447 | ||
448 | if (!c) | |
449 | return NULL; | |
13f5be62 | 450 | s = lxc_getstate(c->name, c->config_path); |
39dc698c | 451 | return lxc_state2str(s); |
72d0e1cb SG |
452 | } |
453 | ||
858377e4 SH |
454 | WRAP_API(const char *, lxcapi_state) |
455 | ||
39dc698c | 456 | static bool is_stopped(struct lxc_container *c) |
794dd120 SH |
457 | { |
458 | lxc_state_t s; | |
13f5be62 | 459 | s = lxc_getstate(c->name, c->config_path); |
794dd120 SH |
460 | return (s == STOPPED); |
461 | } | |
462 | ||
858377e4 | 463 | static bool do_lxcapi_is_running(struct lxc_container *c) |
72d0e1cb SG |
464 | { |
465 | const char *s; | |
466 | ||
467 | if (!c) | |
468 | return false; | |
858377e4 | 469 | s = do_lxcapi_state(c); |
72d0e1cb SG |
470 | if (!s || strcmp(s, "STOPPED") == 0) |
471 | return false; | |
472 | return true; | |
473 | } | |
474 | ||
858377e4 SH |
475 | WRAP_API(bool, lxcapi_is_running) |
476 | ||
477 | static bool do_lxcapi_freeze(struct lxc_container *c) | |
72d0e1cb SG |
478 | { |
479 | int ret; | |
480 | if (!c) | |
481 | return false; | |
482 | ||
9123e471 | 483 | ret = lxc_freeze(c->name, c->config_path); |
72d0e1cb SG |
484 | if (ret) |
485 | return false; | |
486 | return true; | |
487 | } | |
488 | ||
858377e4 SH |
489 | WRAP_API(bool, lxcapi_freeze) |
490 | ||
491 | static bool do_lxcapi_unfreeze(struct lxc_container *c) | |
72d0e1cb SG |
492 | { |
493 | int ret; | |
494 | if (!c) | |
495 | return false; | |
496 | ||
9123e471 | 497 | ret = lxc_unfreeze(c->name, c->config_path); |
72d0e1cb SG |
498 | if (ret) |
499 | return false; | |
500 | return true; | |
501 | } | |
502 | ||
858377e4 SH |
503 | WRAP_API(bool, lxcapi_unfreeze) |
504 | ||
505 | static int do_lxcapi_console_getfd(struct lxc_container *c, int *ttynum, int *masterfd) | |
0115f8fd DE |
506 | { |
507 | int ttyfd; | |
508 | if (!c) | |
509 | return -1; | |
510 | ||
b5159817 | 511 | ttyfd = lxc_console_getfd(c, ttynum, masterfd); |
0115f8fd DE |
512 | return ttyfd; |
513 | } | |
514 | ||
858377e4 SH |
515 | WRAP_API_2(int, lxcapi_console_getfd, int *, int *) |
516 | ||
b5159817 DE |
517 | static int lxcapi_console(struct lxc_container *c, int ttynum, int stdinfd, |
518 | int stdoutfd, int stderrfd, int escape) | |
519 | { | |
858377e4 SH |
520 | int ret; |
521 | ||
522 | if (!c) | |
523 | return -1; | |
524 | ||
525 | current_config = c->lxc_conf; | |
526 | ret = lxc_console(c, ttynum, stdinfd, stdoutfd, stderrfd, escape); | |
527 | current_config = NULL; | |
528 | return ret; | |
b5159817 DE |
529 | } |
530 | ||
191d43cc CB |
531 | static int do_lxcapi_console_log(struct lxc_container *c, struct lxc_console_log *log) |
532 | { | |
533 | int ret; | |
534 | ||
535 | ret = lxc_cmd_console_log(c->name, do_lxcapi_get_config_path(c), log); | |
536 | if (ret < 0) { | |
537 | if (ret == -ENODATA) | |
63b74cda | 538 | NOTICE("The console log is empty"); |
191d43cc | 539 | else if (ret == -EFAULT) |
63b74cda CB |
540 | NOTICE("The container does not keep a console log"); |
541 | else if (ret == -ENOENT) | |
542 | NOTICE("The container does not keep a console log file"); | |
543 | else if (ret == -EIO) | |
544 | NOTICE("Failed to write console log to log file"); | |
191d43cc | 545 | else |
63b74cda | 546 | ERROR("Failed to retrieve console log"); |
191d43cc CB |
547 | } |
548 | ||
549 | return ret; | |
550 | } | |
551 | ||
552 | WRAP_API_1(int, lxcapi_console_log, struct lxc_console_log *) | |
553 | ||
858377e4 | 554 | static pid_t do_lxcapi_init_pid(struct lxc_container *c) |
72d0e1cb | 555 | { |
72d0e1cb SG |
556 | if (!c) |
557 | return -1; | |
558 | ||
5cee8c50 | 559 | return lxc_cmd_get_init_pid(c->name, c->config_path); |
72d0e1cb SG |
560 | } |
561 | ||
858377e4 SH |
562 | WRAP_API(pid_t, lxcapi_init_pid) |
563 | ||
12a50cc6 | 564 | static bool load_config_locked(struct lxc_container *c, const char *fname) |
8eb5694b SH |
565 | { |
566 | if (!c->lxc_conf) | |
567 | c->lxc_conf = lxc_conf_init(); | |
6b0d5538 SH |
568 | if (!c->lxc_conf) |
569 | return false; | |
d08779d4 SH |
570 | if (lxc_config_read(fname, c->lxc_conf, false) != 0) |
571 | return false; | |
d08779d4 | 572 | return true; |
8eb5694b SH |
573 | } |
574 | ||
858377e4 | 575 | static bool do_lxcapi_load_config(struct lxc_container *c, const char *alt_file) |
72d0e1cb | 576 | { |
39dc698c SH |
577 | bool ret = false, need_disklock = false; |
578 | int lret; | |
12a50cc6 | 579 | const char *fname; |
72d0e1cb SG |
580 | if (!c) |
581 | return false; | |
582 | ||
583 | fname = c->configfile; | |
584 | if (alt_file) | |
585 | fname = alt_file; | |
586 | if (!fname) | |
587 | return false; | |
39dc698c SH |
588 | /* |
589 | * If we're reading something other than the container's config, | |
590 | * we only need to lock the in-memory container. If loading the | |
591 | * container's config file, take the disk lock. | |
592 | */ | |
593 | if (strcmp(fname, c->configfile) == 0) | |
594 | need_disklock = true; | |
595 | ||
596 | if (need_disklock) | |
597 | lret = container_disk_lock(c); | |
598 | else | |
599 | lret = container_mem_lock(c); | |
600 | if (lret) | |
72d0e1cb | 601 | return false; |
39dc698c | 602 | |
8eb5694b | 603 | ret = load_config_locked(c, fname); |
39dc698c SH |
604 | |
605 | if (need_disklock) | |
606 | container_disk_unlock(c); | |
607 | else | |
608 | container_mem_unlock(c); | |
72d0e1cb SG |
609 | return ret; |
610 | } | |
611 | ||
858377e4 SH |
612 | WRAP_API_1(bool, lxcapi_load_config, const char *) |
613 | ||
614 | static bool do_lxcapi_want_daemonize(struct lxc_container *c, bool state) | |
72d0e1cb | 615 | { |
497a2995 | 616 | if (!c || !c->lxc_conf) |
540f932a | 617 | return false; |
f02abefe | 618 | if (container_mem_lock(c)) { |
3bc449ed | 619 | ERROR("Error getting mem lock"); |
540f932a | 620 | return false; |
3bc449ed | 621 | } |
a2739df5 | 622 | c->daemonize = state; |
3bc449ed | 623 | container_mem_unlock(c); |
540f932a | 624 | return true; |
72d0e1cb SG |
625 | } |
626 | ||
858377e4 SH |
627 | WRAP_API_1(bool, lxcapi_want_daemonize, bool) |
628 | ||
629 | static bool do_lxcapi_want_close_all_fds(struct lxc_container *c, bool state) | |
130a1888 ÇO |
630 | { |
631 | if (!c || !c->lxc_conf) | |
49badbbe | 632 | return false; |
130a1888 ÇO |
633 | if (container_mem_lock(c)) { |
634 | ERROR("Error getting mem lock"); | |
49badbbe | 635 | return false; |
130a1888 | 636 | } |
540f932a | 637 | c->lxc_conf->close_all_fds = state; |
130a1888 | 638 | container_mem_unlock(c); |
49badbbe | 639 | return true; |
130a1888 ÇO |
640 | } |
641 | ||
858377e4 SH |
642 | WRAP_API_1(bool, lxcapi_want_close_all_fds, bool) |
643 | ||
644 | static bool do_lxcapi_wait(struct lxc_container *c, const char *state, int timeout) | |
7a44c8b4 SG |
645 | { |
646 | int ret; | |
647 | ||
648 | if (!c) | |
649 | return false; | |
650 | ||
67e571de | 651 | ret = lxc_wait(c->name, state, timeout, c->config_path); |
7a44c8b4 SG |
652 | return ret == 0; |
653 | } | |
654 | ||
858377e4 | 655 | WRAP_API_2(bool, lxcapi_wait, const char *, int) |
7a44c8b4 | 656 | |
2d834aa8 SH |
657 | static bool am_single_threaded(void) |
658 | { | |
74f96976 | 659 | struct dirent *direntp; |
2d834aa8 SH |
660 | DIR *dir; |
661 | int count=0; | |
662 | ||
2d834aa8 | 663 | dir = opendir("/proc/self/task"); |
2d834aa8 SH |
664 | if (!dir) { |
665 | INFO("failed to open /proc/self/task"); | |
666 | return false; | |
667 | } | |
668 | ||
74f96976 | 669 | while ((direntp = readdir(dir))) { |
2d834aa8 SH |
670 | if (!strcmp(direntp->d_name, ".")) |
671 | continue; | |
672 | ||
673 | if (!strcmp(direntp->d_name, "..")) | |
674 | continue; | |
675 | if (++count > 1) | |
676 | break; | |
677 | } | |
2d834aa8 | 678 | closedir(dir); |
2d834aa8 SH |
679 | return count == 1; |
680 | } | |
681 | ||
fd51a89b SH |
682 | static void push_arg(char ***argp, char *arg, int *nargs) |
683 | { | |
684 | char **argv; | |
685 | char *copy; | |
686 | ||
687 | do { | |
688 | copy = strdup(arg); | |
689 | } while (!copy); | |
690 | do { | |
691 | argv = realloc(*argp, (*nargs + 2) * sizeof(char *)); | |
692 | } while (!argv); | |
693 | *argp = argv; | |
694 | argv[*nargs] = copy; | |
695 | (*nargs)++; | |
696 | argv[*nargs] = NULL; | |
697 | } | |
698 | ||
699 | static char **split_init_cmd(const char *incmd) | |
700 | { | |
701 | size_t len; | |
702 | int nargs = 0; | |
5590a671 | 703 | char *copy, *p, *saveptr = NULL; |
fd51a89b SH |
704 | char **argv; |
705 | ||
706 | if (!incmd) | |
707 | return NULL; | |
708 | ||
709 | len = strlen(incmd) + 1; | |
710 | copy = alloca(len); | |
711 | strncpy(copy, incmd, len); | |
712 | copy[len-1] = '\0'; | |
713 | ||
714 | do { | |
715 | argv = malloc(sizeof(char *)); | |
716 | } while (!argv); | |
717 | argv[0] = NULL; | |
718 | for (p = strtok_r(copy, " ", &saveptr); p != NULL; | |
719 | p = strtok_r(NULL, " ", &saveptr)) | |
720 | push_arg(&argv, p, &nargs); | |
721 | ||
722 | if (nargs == 0) { | |
723 | free(argv); | |
724 | return NULL; | |
725 | } | |
726 | return argv; | |
727 | } | |
728 | ||
729 | static void free_init_cmd(char **argv) | |
730 | { | |
731 | int i = 0; | |
732 | ||
733 | if (!argv) | |
734 | return; | |
735 | while (argv[i]) | |
736 | free(argv[i++]); | |
737 | free(argv); | |
738 | } | |
739 | ||
5e5576a4 CB |
740 | static int lxc_rcv_status(int state_socket) |
741 | { | |
742 | int ret; | |
ee8377bd | 743 | int state = -1; |
5e5576a4 | 744 | |
ee8377bd | 745 | again: |
5e5576a4 CB |
746 | /* Receive container state. */ |
747 | ret = lxc_abstract_unix_rcv_credential(state_socket, &state, | |
ee8377bd CB |
748 | sizeof(int)); |
749 | if (ret <= 0) { | |
750 | if (errno != EINTR) | |
751 | return -1; | |
752 | TRACE("Caught EINTR; retrying"); | |
753 | goto again; | |
754 | } | |
5e5576a4 CB |
755 | |
756 | return state; | |
757 | } | |
758 | ||
759 | static bool wait_on_daemonized_start(struct lxc_handler *handler, int pid) | |
760 | { | |
01c1b11e | 761 | int ret, state; |
5e5576a4 CB |
762 | |
763 | /* Close write end of the socket pair. */ | |
764 | close(handler->state_socket_pair[1]); | |
765 | handler->state_socket_pair[1] = -1; | |
766 | ||
767 | state = lxc_rcv_status(handler->state_socket_pair[0]); | |
768 | ||
769 | /* Close read end of the socket pair. */ | |
770 | close(handler->state_socket_pair[0]); | |
771 | handler->state_socket_pair[0] = -1; | |
772 | ||
773 | /* The first child is going to fork() again and then exits. So we reap | |
774 | * the first child here. | |
775 | */ | |
01c1b11e CB |
776 | ret = wait_for_pid(pid); |
777 | if (ret < 0) | |
778 | DEBUG("Failed waiting on first child %d", pid); | |
779 | else | |
780 | DEBUG("First child %d exited", pid); | |
5e5576a4 CB |
781 | |
782 | if (state < 0) { | |
783 | SYSERROR("Failed to receive the container state"); | |
784 | return false; | |
785 | } | |
786 | ||
787 | /* If we receive anything else then running we know that the container | |
788 | * failed to start. | |
789 | */ | |
790 | if (state != RUNNING) { | |
791 | ERROR("Received container state \"%s\" instead of \"RUNNING\"", | |
792 | lxc_state2str(state)); | |
793 | return false; | |
794 | } | |
795 | ||
796 | TRACE("Container is in \"RUNNING\" state"); | |
797 | return true; | |
798 | } | |
799 | ||
858377e4 | 800 | static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const argv[]) |
72d0e1cb SG |
801 | { |
802 | int ret; | |
aa460476 | 803 | struct lxc_handler *handler; |
72d0e1cb | 804 | struct lxc_conf *conf; |
540f932a | 805 | bool daemonize = false; |
6eaac303 | 806 | FILE *pid_fp = NULL; |
72d0e1cb SG |
807 | char *default_args[] = { |
808 | "/sbin/init", | |
13aad0ae | 809 | NULL, |
72d0e1cb | 810 | }; |
fd51a89b | 811 | char **init_cmd = NULL; |
c1a3e547 | 812 | int keepfds[3] = {-1, -1, -1}; |
72d0e1cb | 813 | |
7cb19d44 | 814 | /* container does exist */ |
72d0e1cb SG |
815 | if (!c) |
816 | return false; | |
120146b9 | 817 | |
7cb19d44 CB |
818 | /* If anything fails before we set error_num, we want an error in there. |
819 | */ | |
120146b9 SG |
820 | c->error_num = 1; |
821 | ||
7cb19d44 | 822 | /* Container has not been setup. */ |
72d0e1cb SG |
823 | if (!c->lxc_conf) |
824 | return false; | |
825 | ||
c47eafec CB |
826 | ret = ongoing_create(c); |
827 | if (ret < 0) { | |
7cb19d44 | 828 | ERROR("Failed checking for incomplete container creation"); |
3e625e2d | 829 | return false; |
3e625e2d | 830 | } else if (ret == 1) { |
7cb19d44 | 831 | ERROR("Ongoing container creation detected"); |
3e625e2d | 832 | return false; |
c47eafec | 833 | } else if (ret == 2) { |
7cb19d44 | 834 | ERROR("Failed to create container"); |
c47eafec CB |
835 | do_lxcapi_destroy(c); |
836 | return false; | |
3e625e2d SH |
837 | } |
838 | ||
5cee8c50 | 839 | if (container_mem_lock(c)) |
72d0e1cb SG |
840 | return false; |
841 | conf = c->lxc_conf; | |
842 | daemonize = c->daemonize; | |
72d0e1cb | 843 | |
aa460476 | 844 | /* initialize handler */ |
5e5576a4 | 845 | handler = lxc_init_handler(c->name, conf, c->config_path, daemonize); |
dbc9832d | 846 | container_mem_unlock(c); |
aa460476 CB |
847 | if (!handler) |
848 | return false; | |
849 | ||
fa30091b CB |
850 | if (!argv) { |
851 | if (useinit && conf->execute_cmd) | |
852 | argv = init_cmd = split_init_cmd(conf->execute_cmd); | |
853 | else | |
854 | argv = init_cmd = split_init_cmd(conf->init_cmd); | |
855 | } | |
fd51a89b | 856 | |
7cb19d44 | 857 | /* ... otherwise use default_args. */ |
fa30091b | 858 | if (!argv) { |
e1e76423 CB |
859 | if (useinit) { |
860 | ERROR("No valid init detected"); | |
861 | lxc_free_handler(handler); | |
fa30091b | 862 | return false; |
e1e76423 CB |
863 | } |
864 | argv = default_args; | |
fa30091b | 865 | } |
72d0e1cb | 866 | |
7cb19d44 CB |
867 | /* I'm not sure what locks we want here.Any? Is liblxc's locking enough |
868 | * here to protect the on disk container? We don't want to exclude | |
869 | * things like lxc_info while the container is running. | |
870 | */ | |
72d0e1cb | 871 | if (daemonize) { |
a6b6ad7b | 872 | bool started; |
0a4be28d | 873 | char title[2048]; |
c47eafec | 874 | pid_t pid; |
71454076 | 875 | |
c47eafec | 876 | pid = fork(); |
f2e07cb6 | 877 | if (pid < 0) { |
c47eafec | 878 | free_init_cmd(init_cmd); |
f2e07cb6 | 879 | lxc_free_handler(handler); |
72d0e1cb | 880 | return false; |
f2e07cb6 | 881 | } |
6eaac303 | 882 | |
7cb19d44 | 883 | /* first parent */ |
6eaac303 QH |
884 | if (pid != 0) { |
885 | /* Set to NULL because we don't want father unlink | |
886 | * the PID file, child will do the free and unlink. | |
887 | */ | |
888 | c->pidfile = NULL; | |
5e5576a4 | 889 | |
7cb19d44 CB |
890 | /* Wait for container to tell us whether it started |
891 | * successfully. | |
892 | */ | |
a6b6ad7b CB |
893 | started = wait_on_daemonized_start(handler, pid); |
894 | ||
895 | free_init_cmd(init_cmd); | |
896 | lxc_free_handler(handler); | |
897 | return started; | |
6eaac303 | 898 | } |
025ed0f3 | 899 | |
7cb19d44 CB |
900 | /* first child */ |
901 | ||
0a4be28d | 902 | /* We don't really care if this doesn't print all the |
7cb19d44 CB |
903 | * characters. All that it means is that the proctitle will be |
904 | * ugly. Similarly, we also don't care if setproctitle() fails. | |
905 | * */ | |
0a4be28d TA |
906 | snprintf(title, sizeof(title), "[lxc monitor] %s %s", c->config_path, c->name); |
907 | INFO("Attempting to set proc title to %s", title); | |
908 | setproctitle(title); | |
909 | ||
7cb19d44 CB |
910 | /* We fork() a second time to be reparented to init. Like |
911 | * POSIX's daemon() function we change to "/" and redirect | |
912 | * std{in,out,err} to /dev/null. | |
913 | */ | |
697fa639 SH |
914 | pid = fork(); |
915 | if (pid < 0) { | |
7cb19d44 | 916 | SYSERROR("Failed to fork first child process"); |
d608fbda | 917 | _exit(EXIT_FAILURE); |
697fa639 | 918 | } |
a6b6ad7b | 919 | |
7cb19d44 | 920 | /* second parent */ |
a6b6ad7b CB |
921 | if (pid != 0) { |
922 | free_init_cmd(init_cmd); | |
923 | lxc_free_handler(handler); | |
d608fbda | 924 | _exit(EXIT_SUCCESS); |
a6b6ad7b CB |
925 | } |
926 | ||
7cb19d44 CB |
927 | /* second child */ |
928 | ||
929 | /* change to / directory */ | |
930 | ret = chdir("/"); | |
931 | if (ret < 0) { | |
932 | SYSERROR("Failed to change to \"/\" directory"); | |
d608fbda | 933 | _exit(EXIT_FAILURE); |
c278cef2 | 934 | } |
7cb19d44 | 935 | |
c1a3e547 CB |
936 | keepfds[0] = handler->conf->maincmd_fd; |
937 | keepfds[1] = handler->state_socket_pair[0]; | |
938 | keepfds[2] = handler->state_socket_pair[1]; | |
939 | ret = lxc_check_inherited(conf, true, keepfds, | |
940 | sizeof(keepfds) / sizeof(keepfds[0])); | |
7cb19d44 | 941 | if (ret < 0) |
d608fbda | 942 | _exit(EXIT_FAILURE); |
7cb19d44 CB |
943 | |
944 | /* redirect std{in,out,err} to /dev/null */ | |
945 | ret = null_stdfds(); | |
946 | if (ret < 0) { | |
947 | ERROR("Failed to redirect std{in,out,err} to /dev/null"); | |
d608fbda | 948 | _exit(EXIT_FAILURE); |
69aeabac | 949 | } |
7cb19d44 CB |
950 | |
951 | /* become session leader */ | |
952 | ret = setsid(); | |
953 | if (ret < 0) | |
0059379f | 954 | TRACE("Process %d is already process group leader", lxc_raw_getpid()); |
2d834aa8 SH |
955 | } else { |
956 | if (!am_single_threaded()) { | |
957 | ERROR("Cannot start non-daemonized container when threaded"); | |
a6b6ad7b | 958 | free_init_cmd(init_cmd); |
f2e07cb6 | 959 | lxc_free_handler(handler); |
2d834aa8 SH |
960 | return false; |
961 | } | |
72d0e1cb SG |
962 | } |
963 | ||
aa460476 | 964 | /* We need to write PID file after daemonize, so we always |
6eaac303 QH |
965 | * write the right PID. |
966 | */ | |
967 | if (c->pidfile) { | |
968 | pid_fp = fopen(c->pidfile, "w"); | |
969 | if (pid_fp == NULL) { | |
970 | SYSERROR("Failed to create pidfile '%s' for '%s'", | |
971 | c->pidfile, c->name); | |
c47eafec | 972 | free_init_cmd(init_cmd); |
f2e07cb6 | 973 | lxc_free_handler(handler); |
13353dc4 | 974 | if (daemonize) |
d608fbda | 975 | _exit(EXIT_FAILURE); |
6eaac303 QH |
976 | return false; |
977 | } | |
978 | ||
0059379f | 979 | if (fprintf(pid_fp, "%d\n", lxc_raw_getpid()) < 0) { |
6eaac303 QH |
980 | SYSERROR("Failed to write '%s'", c->pidfile); |
981 | fclose(pid_fp); | |
982 | pid_fp = NULL; | |
c47eafec | 983 | free_init_cmd(init_cmd); |
f2e07cb6 | 984 | lxc_free_handler(handler); |
13353dc4 | 985 | if (daemonize) |
d608fbda | 986 | _exit(EXIT_FAILURE); |
6eaac303 QH |
987 | return false; |
988 | } | |
989 | ||
990 | fclose(pid_fp); | |
991 | pid_fp = NULL; | |
992 | } | |
993 | ||
72d0e1cb | 994 | conf->reboot = 0; |
d2cf4c37 | 995 | |
a8dfe4e0 WB |
996 | /* Unshare the mount namespace if requested */ |
997 | if (conf->monitor_unshare) { | |
7cb19d44 CB |
998 | ret = unshare(CLONE_NEWNS); |
999 | if (ret < 0) { | |
a8dfe4e0 | 1000 | SYSERROR("failed to unshare mount namespace"); |
f2e07cb6 | 1001 | lxc_free_handler(handler); |
a6b6ad7b | 1002 | ret = 1; |
7cb19d44 | 1003 | goto on_error; |
a8dfe4e0 | 1004 | } |
7cb19d44 CB |
1005 | |
1006 | ret = mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL); | |
1007 | if (ret < 0) { | |
a8dfe4e0 | 1008 | SYSERROR("Failed to make / rslave at startup"); |
f2e07cb6 | 1009 | lxc_free_handler(handler); |
a6b6ad7b | 1010 | ret = 1; |
7cb19d44 | 1011 | goto on_error; |
a8dfe4e0 WB |
1012 | } |
1013 | } | |
1014 | ||
8bee8851 | 1015 | reboot: |
aa460476 CB |
1016 | if (conf->reboot == 2) { |
1017 | /* initialize handler */ | |
5e5576a4 | 1018 | handler = lxc_init_handler(c->name, conf, c->config_path, daemonize); |
a6b6ad7b CB |
1019 | if (!handler) { |
1020 | ret = 1; | |
7cb19d44 | 1021 | goto on_error; |
a6b6ad7b | 1022 | } |
aa460476 CB |
1023 | } |
1024 | ||
c1a3e547 CB |
1025 | keepfds[0] = handler->conf->maincmd_fd; |
1026 | keepfds[1] = handler->state_socket_pair[0]; | |
1027 | keepfds[2] = handler->state_socket_pair[1]; | |
1028 | ret = lxc_check_inherited(conf, daemonize, keepfds, | |
1029 | sizeof(keepfds) / sizeof(keepfds[0])); | |
7cb19d44 | 1030 | if (ret < 0) { |
f2e07cb6 | 1031 | lxc_free_handler(handler); |
d2cf4c37 | 1032 | ret = 1; |
7cb19d44 | 1033 | goto on_error; |
d2cf4c37 SH |
1034 | } |
1035 | ||
9605460f CB |
1036 | if (useinit) |
1037 | ret = lxc_execute(c->name, argv, 1, handler, c->config_path, daemonize); | |
1038 | else | |
1039 | ret = lxc_start(c->name, argv, handler, c->config_path, daemonize); | |
cd5177e9 | 1040 | c->error_num = handler->exit_status; |
72d0e1cb | 1041 | |
8bee8851 | 1042 | if (conf->reboot == 1) { |
7cb19d44 | 1043 | INFO("Container requested reboot"); |
8bee8851 | 1044 | conf->reboot = 2; |
72d0e1cb SG |
1045 | goto reboot; |
1046 | } | |
1047 | ||
7cb19d44 | 1048 | on_error: |
487d8008 QH |
1049 | if (c->pidfile) { |
1050 | unlink(c->pidfile); | |
1051 | free(c->pidfile); | |
1052 | c->pidfile = NULL; | |
1053 | } | |
fd51a89b SH |
1054 | free_init_cmd(init_cmd); |
1055 | ||
7cb19d44 | 1056 | if (daemonize && ret != 0) |
d608fbda | 1057 | _exit(EXIT_FAILURE); |
7cb19d44 | 1058 | else if (daemonize) |
d608fbda | 1059 | _exit(EXIT_SUCCESS); |
7cb19d44 CB |
1060 | |
1061 | if (ret != 0) | |
1062 | return false; | |
1063 | ||
1064 | return true; | |
72d0e1cb SG |
1065 | } |
1066 | ||
858377e4 SH |
1067 | static bool lxcapi_start(struct lxc_container *c, int useinit, char * const argv[]) |
1068 | { | |
1069 | bool ret; | |
1070 | current_config = c ? c->lxc_conf : NULL; | |
1071 | ret = do_lxcapi_start(c, useinit, argv); | |
1072 | current_config = NULL; | |
1073 | return ret; | |
1074 | } | |
1075 | ||
72d0e1cb SG |
1076 | /* |
1077 | * note there MUST be an ending NULL | |
1078 | */ | |
1079 | static bool lxcapi_startl(struct lxc_container *c, int useinit, ...) | |
1080 | { | |
1081 | va_list ap; | |
a0e93eeb | 1082 | char **inargs = NULL; |
72d0e1cb SG |
1083 | bool bret = false; |
1084 | ||
1085 | /* container exists */ | |
1086 | if (!c) | |
1087 | return false; | |
1088 | ||
858377e4 SH |
1089 | current_config = c->lxc_conf; |
1090 | ||
72d0e1cb | 1091 | va_start(ap, useinit); |
a0e93eeb | 1092 | inargs = lxc_va_arg_list_to_argv(ap, 0, 1); |
72d0e1cb SG |
1093 | va_end(ap); |
1094 | ||
a0e93eeb CS |
1095 | if (!inargs) { |
1096 | ERROR("Memory allocation error."); | |
1097 | goto out; | |
72d0e1cb SG |
1098 | } |
1099 | ||
a0e93eeb | 1100 | /* pass NULL if no arguments were supplied */ |
858377e4 | 1101 | bret = do_lxcapi_start(c, useinit, *inargs ? inargs : NULL); |
72d0e1cb SG |
1102 | |
1103 | out: | |
1104 | if (inargs) { | |
4e03ae57 DE |
1105 | char **arg; |
1106 | for (arg = inargs; *arg; arg++) | |
1107 | free(*arg); | |
72d0e1cb SG |
1108 | free(inargs); |
1109 | } | |
1110 | ||
858377e4 | 1111 | current_config = NULL; |
72d0e1cb SG |
1112 | return bret; |
1113 | } | |
1114 | ||
858377e4 | 1115 | static bool do_lxcapi_stop(struct lxc_container *c) |
72d0e1cb SG |
1116 | { |
1117 | int ret; | |
1118 | ||
1119 | if (!c) | |
1120 | return false; | |
1121 | ||
ef6e34ee | 1122 | ret = lxc_cmd_stop(c->name, c->config_path); |
72d0e1cb | 1123 | |
d775f21b | 1124 | return ret == 0; |
72d0e1cb SG |
1125 | } |
1126 | ||
858377e4 SH |
1127 | WRAP_API(bool, lxcapi_stop) |
1128 | ||
d5752559 SH |
1129 | static int do_create_container_dir(const char *path, struct lxc_conf *conf) |
1130 | { | |
1131 | int ret = -1, lasterr; | |
1132 | char *p = alloca(strlen(path)+1); | |
1133 | mode_t mask = umask(0002); | |
1134 | ret = mkdir(path, 0770); | |
1135 | lasterr = errno; | |
1136 | umask(mask); | |
1137 | errno = lasterr; | |
1138 | if (ret) { | |
1139 | if (errno == EEXIST) | |
1140 | ret = 0; | |
1141 | else { | |
1142 | SYSERROR("failed to create container path %s", path); | |
1143 | return -1; | |
1144 | } | |
1145 | } | |
1146 | strcpy(p, path); | |
1147 | if (!lxc_list_empty(&conf->id_map) && chown_mapped_root(p, conf) != 0) { | |
1148 | ERROR("Failed to chown container dir"); | |
1149 | ret = -1; | |
1150 | } | |
1151 | return ret; | |
1152 | } | |
1153 | ||
72d0e1cb SG |
1154 | /* |
1155 | * create the standard expected container dir | |
1156 | */ | |
1157 | static bool create_container_dir(struct lxc_container *c) | |
1158 | { | |
1159 | char *s; | |
1160 | int len, ret; | |
1161 | ||
2a59a681 | 1162 | len = strlen(c->config_path) + strlen(c->name) + 2; |
72d0e1cb SG |
1163 | s = malloc(len); |
1164 | if (!s) | |
1165 | return false; | |
2a59a681 | 1166 | ret = snprintf(s, len, "%s/%s", c->config_path, c->name); |
72d0e1cb SG |
1167 | if (ret < 0 || ret >= len) { |
1168 | free(s); | |
1169 | return false; | |
1170 | } | |
d5752559 | 1171 | ret = do_create_container_dir(s, c->lxc_conf); |
72d0e1cb SG |
1172 | free(s); |
1173 | return ret == 0; | |
1174 | } | |
1175 | ||
10bc1861 CB |
1176 | /* do_storage_create: thin wrapper around storage_create(). Like |
1177 | * storage_create(), it returns a mounted bdev on success, NULL on error. | |
72d0e1cb | 1178 | */ |
10bc1861 CB |
1179 | static struct lxc_storage *do_storage_create(struct lxc_container *c, |
1180 | const char *type, | |
1181 | struct bdev_specs *specs) | |
1897e3bc | 1182 | { |
e9e29a33 | 1183 | int ret; |
1897e3bc | 1184 | size_t len; |
e9e29a33 | 1185 | char *dest; |
10bc1861 | 1186 | struct lxc_storage *bdev; |
1897e3bc | 1187 | |
cd219ae6 | 1188 | /* rootfs.path or lxcpath/lxcname/rootfs */ |
e9e29a33 CB |
1189 | if (c->lxc_conf->rootfs.path && |
1190 | (access(c->lxc_conf->rootfs.path, F_OK) == 0)) { | |
cf465fe4 SH |
1191 | const char *rpath = c->lxc_conf->rootfs.path; |
1192 | len = strlen(rpath) + 1; | |
cd219ae6 | 1193 | dest = alloca(len); |
cf465fe4 | 1194 | ret = snprintf(dest, len, "%s", rpath); |
cd219ae6 | 1195 | } else { |
858377e4 | 1196 | const char *lxcpath = do_lxcapi_get_config_path(c); |
cd219ae6 SY |
1197 | len = strlen(c->name) + strlen(lxcpath) + 9; |
1198 | dest = alloca(len); | |
1199 | ret = snprintf(dest, len, "%s/%s/rootfs", lxcpath, c->name); | |
1200 | } | |
e9e29a33 | 1201 | if (ret < 0 || (size_t)ret >= len) |
1897e3bc SH |
1202 | return NULL; |
1203 | ||
10bc1861 | 1204 | bdev = storage_create(dest, type, c->name, specs); |
d44e88c2 | 1205 | if (!bdev) { |
e9e29a33 | 1206 | ERROR("Failed to create \"%s\" storage", type); |
1897e3bc | 1207 | return NULL; |
d44e88c2 SH |
1208 | } |
1209 | ||
7a96a068 | 1210 | if (!c->set_config_item(c, "lxc.rootfs.path", bdev->src)) { |
e9e29a33 | 1211 | ERROR("Failed to set \"lxc.rootfs.path = %s\"", bdev->src); |
f7ac4459 CB |
1212 | return NULL; |
1213 | } | |
cf3ef16d | 1214 | |
e9e29a33 CB |
1215 | /* If we are not root, chown the rootfs dir to root in the target user |
1216 | * namespace. | |
f7ac4459 | 1217 | */ |
e9e29a33 CB |
1218 | ret = geteuid(); |
1219 | if (ret != 0 || (c->lxc_conf && !lxc_list_empty(&c->lxc_conf->id_map))) { | |
1220 | ret = chown_mapped_root(bdev->dest, c->lxc_conf); | |
1221 | if (ret < 0) { | |
1222 | ERROR("Error chowning \"%s\" to container root", bdev->dest); | |
97e9cfa0 | 1223 | suggest_default_idmap(); |
10bc1861 | 1224 | storage_put(bdev); |
cf3ef16d SH |
1225 | return NULL; |
1226 | } | |
1227 | } | |
1228 | ||
1897e3bc SH |
1229 | return bdev; |
1230 | } | |
1231 | ||
96b3cb40 | 1232 | static char *lxcbasename(char *path) |
72d0e1cb | 1233 | { |
47e55887 CB |
1234 | char *p; |
1235 | ||
1236 | p = path + strlen(path) - 1; | |
96b3cb40 SH |
1237 | while (*p != '/' && p > path) |
1238 | p--; | |
47e55887 | 1239 | |
96b3cb40 SH |
1240 | return p; |
1241 | } | |
72d0e1cb | 1242 | |
47e55887 CB |
1243 | static bool create_run_template(struct lxc_container *c, char *tpath, |
1244 | bool need_null_stdfds, char *const argv[]) | |
96b3cb40 | 1245 | { |
47e55887 | 1246 | int ret; |
96b3cb40 | 1247 | pid_t pid; |
72d0e1cb | 1248 | |
72d0e1cb | 1249 | if (!tpath) |
96b3cb40 | 1250 | return true; |
72d0e1cb SG |
1251 | |
1252 | pid = fork(); | |
1253 | if (pid < 0) { | |
7e34710e | 1254 | SYSERROR("Failed to fork task for container creation template"); |
96b3cb40 | 1255 | return false; |
72d0e1cb SG |
1256 | } |
1257 | ||
1a0e70ac | 1258 | if (pid == 0) { /* child */ |
47e55887 CB |
1259 | int i, len; |
1260 | char *namearg, *patharg, *rootfsarg; | |
96b3cb40 | 1261 | char **newargv; |
47e55887 CB |
1262 | int nargs = 0; |
1263 | struct lxc_storage *bdev = NULL; | |
cf3ef16d | 1264 | struct lxc_conf *conf = c->lxc_conf; |
47e55887 | 1265 | uid_t euid; |
72d0e1cb | 1266 | |
47e55887 CB |
1267 | if (need_null_stdfds) { |
1268 | ret = null_stdfds(); | |
1269 | if (ret < 0) | |
1270 | _exit(EXIT_FAILURE); | |
dc23c1c8 | 1271 | } |
1897e3bc | 1272 | |
8a388ed4 | 1273 | bdev = storage_init(c->lxc_conf); |
1897e3bc | 1274 | if (!bdev) { |
47e55887 | 1275 | ERROR("Failed to initialize storage"); |
7e34710e | 1276 | _exit(EXIT_FAILURE); |
1897e3bc SH |
1277 | } |
1278 | ||
47e55887 CB |
1279 | euid = geteuid(); |
1280 | if (euid == 0) { | |
1281 | ret = unshare(CLONE_NEWNS); | |
1282 | if (ret < 0) { | |
1283 | ERROR("Failed to unshare CLONE_NEWNS"); | |
7e34710e | 1284 | _exit(EXIT_FAILURE); |
cf3ef16d | 1285 | } |
47e55887 CB |
1286 | |
1287 | ret = detect_shared_rootfs(); | |
1288 | if (ret == 1) { | |
1289 | ret = mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL); | |
1290 | if (ret < 0) { | |
1291 | SYSERROR("Failed to make \"/\" rslave"); | |
4de2791f SH |
1292 | ERROR("Continuing..."); |
1293 | } | |
1294 | } | |
1295 | } | |
47e55887 CB |
1296 | |
1297 | if (strcmp(bdev->type, "dir") != 0 && strcmp(bdev->type, "btrfs") != 0) { | |
1298 | if (euid != 0) { | |
1299 | ERROR("Unprivileged users can only create " | |
1300 | "btrfs and directory-backed containers"); | |
eb70aaf0 | 1301 | _exit(EXIT_FAILURE); |
4de2791f | 1302 | } |
241978fa | 1303 | |
47e55887 CB |
1304 | if (strcmp(bdev->type, "overlay") == 0 || |
1305 | strcmp(bdev->type, "overlayfs") == 0) { | |
241978fa CB |
1306 | /* If we create an overlay container we need to |
1307 | * rsync the contents into | |
1308 | * <container-path>/<container-name>/rootfs. | |
1309 | * However, the overlay mount function will | |
1310 | * mount will mount | |
1311 | * <container-path>/<container-name>/delta0 | |
1312 | * over | |
1313 | * <container-path>/<container-name>/rootfs | |
1314 | * which means we would rsync the rootfs into | |
1315 | * the delta directory. That doesn't make sense | |
1316 | * since the delta directory only exists to | |
1317 | * record the differences to | |
1318 | * <container-path>/<container-name>/rootfs. So | |
1319 | * let's simply bind-mount here and then rsync | |
1320 | * directly into | |
1321 | * <container-path>/<container-name>/rootfs. | |
1322 | */ | |
1323 | char *src; | |
1324 | ||
1325 | src = ovl_get_rootfs(bdev->src, &(size_t){0}); | |
1326 | if (!src) { | |
1327 | ERROR("Failed to get rootfs"); | |
eb70aaf0 | 1328 | _exit(EXIT_FAILURE); |
241978fa CB |
1329 | } |
1330 | ||
1331 | ret = mount(src, bdev->dest, "bind", MS_BIND | MS_REC, NULL); | |
1332 | if (ret < 0) { | |
1333 | ERROR("Failed to mount rootfs"); | |
1334 | return -1; | |
1335 | } | |
1336 | } else { | |
47e55887 CB |
1337 | ret = bdev->ops->mount(bdev); |
1338 | if (ret < 0) { | |
241978fa | 1339 | ERROR("Failed to mount rootfs"); |
eb70aaf0 | 1340 | _exit(EXIT_FAILURE); |
241978fa | 1341 | } |
cf3ef16d | 1342 | } |
1a0e70ac | 1343 | } else { /* TODO come up with a better way here! */ |
41dc7155 | 1344 | const char *src; |
f10fad2f | 1345 | free(bdev->dest); |
e9705550 CB |
1346 | src = lxc_storage_get_path(bdev->src, bdev->type); |
1347 | bdev->dest = strdup(src); | |
1897e3bc SH |
1348 | } |
1349 | ||
47e55887 CB |
1350 | /* Create our new array, pre-pend the template name and base |
1351 | * args. | |
72d0e1cb SG |
1352 | */ |
1353 | if (argv) | |
47e55887 CB |
1354 | for (nargs = 0; argv[nargs]; nargs++) { |
1355 | ; | |
1356 | } | |
1357 | /* template, path, rootfs and name args */ | |
1358 | nargs += 4; | |
cf3ef16d | 1359 | |
72d0e1cb SG |
1360 | newargv = malloc(nargs * sizeof(*newargv)); |
1361 | if (!newargv) | |
7e34710e | 1362 | _exit(EXIT_FAILURE); |
96b3cb40 | 1363 | newargv[0] = lxcbasename(tpath); |
72d0e1cb | 1364 | |
47e55887 | 1365 | /* --path */ |
2a59a681 | 1366 | len = strlen(c->config_path) + strlen(c->name) + strlen("--path=") + 2; |
72d0e1cb SG |
1367 | patharg = malloc(len); |
1368 | if (!patharg) | |
7e34710e | 1369 | _exit(EXIT_FAILURE); |
2a59a681 | 1370 | ret = snprintf(patharg, len, "--path=%s/%s", c->config_path, c->name); |
72d0e1cb | 1371 | if (ret < 0 || ret >= len) |
7e34710e | 1372 | _exit(EXIT_FAILURE); |
72d0e1cb | 1373 | newargv[1] = patharg; |
47e55887 CB |
1374 | |
1375 | /* --name */ | |
72d0e1cb SG |
1376 | len = strlen("--name=") + strlen(c->name) + 1; |
1377 | namearg = malloc(len); | |
1378 | if (!namearg) | |
7e34710e | 1379 | _exit(EXIT_FAILURE); |
72d0e1cb SG |
1380 | ret = snprintf(namearg, len, "--name=%s", c->name); |
1381 | if (ret < 0 || ret >= len) | |
7e34710e | 1382 | _exit(EXIT_FAILURE); |
72d0e1cb SG |
1383 | newargv[2] = namearg; |
1384 | ||
47e55887 | 1385 | /* --rootfs */ |
1897e3bc SH |
1386 | len = strlen("--rootfs=") + 1 + strlen(bdev->dest); |
1387 | rootfsarg = malloc(len); | |
1388 | if (!rootfsarg) | |
7e34710e | 1389 | _exit(EXIT_FAILURE); |
1897e3bc SH |
1390 | ret = snprintf(rootfsarg, len, "--rootfs=%s", bdev->dest); |
1391 | if (ret < 0 || ret >= len) | |
7e34710e | 1392 | _exit(EXIT_FAILURE); |
1897e3bc SH |
1393 | newargv[3] = rootfsarg; |
1394 | ||
72d0e1cb SG |
1395 | /* add passed-in args */ |
1396 | if (argv) | |
1897e3bc | 1397 | for (i = 4; i < nargs; i++) |
47e55887 | 1398 | newargv[i] = argv[i - 4]; |
72d0e1cb SG |
1399 | |
1400 | /* add trailing NULL */ | |
1401 | nargs++; | |
1402 | newargv = realloc(newargv, nargs * sizeof(*newargv)); | |
1403 | if (!newargv) | |
7e34710e | 1404 | _exit(EXIT_FAILURE); |
72d0e1cb SG |
1405 | newargv[nargs - 1] = NULL; |
1406 | ||
47e55887 CB |
1407 | /* If we're running the template in a mapped userns, then we |
1408 | * prepend the template command with: lxc-usernsexec <-m map1> | |
1409 | * ... <-m mapn> -- and we append "--mapped-uid x", where x is | |
1410 | * the mapped uid for our geteuid() | |
cf3ef16d | 1411 | */ |
0e6e3a41 | 1412 | if (!lxc_list_empty(&conf->id_map)) { |
47e55887 CB |
1413 | int extraargs, hostuid_mapped, hostgid_mapped; |
1414 | char **n2; | |
1415 | char txtuid[20], txtgid[20]; | |
cf3ef16d SH |
1416 | struct lxc_list *it; |
1417 | struct id_map *map; | |
47e55887 | 1418 | int n2args = 1; |
cf3ef16d | 1419 | |
47e55887 CB |
1420 | n2 = malloc(n2args * sizeof(*n2)); |
1421 | if (!n2) | |
7e34710e | 1422 | _exit(EXIT_FAILURE); |
47e55887 | 1423 | |
cf3ef16d SH |
1424 | newargv[0] = tpath; |
1425 | tpath = "lxc-usernsexec"; | |
1426 | n2[0] = "lxc-usernsexec"; | |
1427 | lxc_list_for_each(it, &conf->id_map) { | |
1428 | map = it->elem; | |
1429 | n2args += 2; | |
57d116ab | 1430 | n2 = realloc(n2, n2args * sizeof(char *)); |
cf3ef16d | 1431 | if (!n2) |
7e34710e | 1432 | _exit(EXIT_FAILURE); |
47e55887 CB |
1433 | |
1434 | n2[n2args - 2] = "-m"; | |
1435 | n2[n2args - 1] = malloc(200); | |
1436 | if (!n2[n2args - 1]) | |
7e34710e | 1437 | _exit(EXIT_FAILURE); |
47e55887 CB |
1438 | |
1439 | ret = snprintf(n2[n2args - 1], 200, "%c:%lu:%lu:%lu", | |
1440 | map->idtype == ID_TYPE_UID ? 'u' : 'g', | |
1441 | map->nsid, map->hostid, map->range); | |
cf3ef16d | 1442 | if (ret < 0 || ret >= 200) |
7e34710e | 1443 | _exit(EXIT_FAILURE); |
cf3ef16d | 1444 | } |
47e55887 CB |
1445 | |
1446 | hostuid_mapped = mapped_hostid(geteuid(), conf, ID_TYPE_UID); | |
1447 | extraargs = hostuid_mapped >= 0 ? 1 : 3; | |
57d116ab | 1448 | n2 = realloc(n2, (nargs + n2args + extraargs) * sizeof(char *)); |
cf3ef16d | 1449 | if (!n2) |
7e34710e | 1450 | _exit(EXIT_FAILURE); |
47e55887 CB |
1451 | |
1452 | if (hostuid_mapped < 0) { | |
1453 | hostuid_mapped = find_unmapped_nsid(conf, ID_TYPE_UID); | |
cf3ef16d | 1454 | n2[n2args++] = "-m"; |
47e55887 CB |
1455 | if (hostuid_mapped < 0) { |
1456 | ERROR("Failed to find free uid to map"); | |
7e34710e | 1457 | _exit(EXIT_FAILURE); |
cf3ef16d | 1458 | } |
47e55887 | 1459 | |
cf3ef16d | 1460 | n2[n2args++] = malloc(200); |
47e55887 | 1461 | if (!n2[n2args - 1]) { |
cf3ef16d | 1462 | SYSERROR("out of memory"); |
7e34710e | 1463 | _exit(EXIT_FAILURE); |
cf3ef16d | 1464 | } |
47e55887 CB |
1465 | ret = snprintf(n2[n2args - 1], 200, "u:%d:%d:1", |
1466 | hostuid_mapped, geteuid()); | |
1467 | if (ret < 0 || ret >= 200) | |
7e34710e | 1468 | _exit(EXIT_FAILURE); |
cf3ef16d | 1469 | } |
47e55887 CB |
1470 | |
1471 | hostgid_mapped = mapped_hostid(getegid(), conf, ID_TYPE_GID); | |
2133f58c SH |
1472 | extraargs = hostgid_mapped >= 0 ? 1 : 3; |
1473 | n2 = realloc(n2, (nargs + n2args + extraargs) * sizeof(char *)); | |
1474 | if (!n2) | |
7e34710e | 1475 | _exit(EXIT_FAILURE); |
47e55887 | 1476 | |
2133f58c | 1477 | if (hostgid_mapped < 0) { |
339efad9 | 1478 | hostgid_mapped = find_unmapped_nsid(conf, ID_TYPE_GID); |
2133f58c SH |
1479 | n2[n2args++] = "-m"; |
1480 | if (hostgid_mapped < 0) { | |
47e55887 | 1481 | ERROR("Failed to find free gid to map"); |
7e34710e | 1482 | _exit(EXIT_FAILURE); |
2133f58c | 1483 | } |
47e55887 | 1484 | |
2133f58c | 1485 | n2[n2args++] = malloc(200); |
47e55887 | 1486 | if (!n2[n2args - 1]) { |
2133f58c | 1487 | SYSERROR("out of memory"); |
7e34710e | 1488 | _exit(EXIT_FAILURE); |
2133f58c | 1489 | } |
47e55887 CB |
1490 | |
1491 | ret = snprintf(n2[n2args - 1], 200, "g:%d:%d:1", | |
1492 | hostgid_mapped, getegid()); | |
1493 | if (ret < 0 || ret >= 200) | |
7e34710e | 1494 | _exit(EXIT_FAILURE); |
2133f58c | 1495 | } |
cf3ef16d SH |
1496 | n2[n2args++] = "--"; |
1497 | for (i = 0; i < nargs; i++) | |
1498 | n2[i + n2args] = newargv[i]; | |
57d116ab | 1499 | n2args += nargs; |
47e55887 CB |
1500 | |
1501 | /* Finally add "--mapped-uid $uid" to tell template what | |
1502 | * to chown cached images to. | |
1a0e70ac | 1503 | */ |
2133f58c | 1504 | n2args += 4; |
57d116ab | 1505 | n2 = realloc(n2, n2args * sizeof(char *)); |
47e55887 | 1506 | if (!n2) |
7e34710e | 1507 | _exit(EXIT_FAILURE); |
47e55887 | 1508 | |
1a0e70ac | 1509 | /* note n2[n2args-1] is NULL */ |
47e55887 CB |
1510 | n2[n2args - 5] = "--mapped-uid"; |
1511 | snprintf(txtuid, 20, "%d", hostuid_mapped); | |
1512 | n2[n2args - 4] = txtuid; | |
1513 | n2[n2args - 3] = "--mapped-gid"; | |
2133f58c | 1514 | snprintf(txtgid, 20, "%d", hostgid_mapped); |
47e55887 CB |
1515 | n2[n2args - 2] = txtgid; |
1516 | n2[n2args - 1] = NULL; | |
cf3ef16d SH |
1517 | free(newargv); |
1518 | newargv = n2; | |
1519 | } | |
47e55887 | 1520 | |
cf3ef16d | 1521 | execvp(tpath, newargv); |
e9705550 | 1522 | SYSERROR("Failed to execute template %s", tpath); |
7e34710e | 1523 | _exit(EXIT_FAILURE); |
72d0e1cb SG |
1524 | } |
1525 | ||
47e55887 CB |
1526 | ret = wait_for_pid(pid); |
1527 | if (ret != 0) { | |
1528 | ERROR("Failed to create container from template"); | |
96b3cb40 SH |
1529 | return false; |
1530 | } | |
1531 | ||
1532 | return true; | |
1533 | } | |
1534 | ||
74a3920a | 1535 | static bool prepend_lxc_header(char *path, const char *t, char *const argv[]) |
3ce74686 | 1536 | { |
1fd9bd50 | 1537 | long flen; |
b4569e93 | 1538 | char *contents; |
3ce74686 | 1539 | FILE *f; |
025ed0f3 | 1540 | int ret = -1; |
52026772 | 1541 | #if HAVE_LIBGNUTLS |
025ed0f3 | 1542 | int i; |
3ce74686 | 1543 | unsigned char md_value[SHA_DIGEST_LENGTH]; |
b4569e93 | 1544 | char *tpath; |
52026772 | 1545 | #endif |
3ce74686 | 1546 | |
025ed0f3 | 1547 | f = fopen(path, "r"); |
025ed0f3 | 1548 | if (f == NULL) |
3ce74686 | 1549 | return false; |
025ed0f3 SH |
1550 | |
1551 | if (fseek(f, 0, SEEK_END) < 0) | |
1552 | goto out_error; | |
1553 | if ((flen = ftell(f)) < 0) | |
1554 | goto out_error; | |
1555 | if (fseek(f, 0, SEEK_SET) < 0) | |
1556 | goto out_error; | |
1557 | if ((contents = malloc(flen + 1)) == NULL) | |
1558 | goto out_error; | |
1559 | if (fread(contents, 1, flen, f) != flen) | |
1560 | goto out_free_contents; | |
1561 | ||
3ce74686 | 1562 | contents[flen] = '\0'; |
025ed0f3 | 1563 | ret = fclose(f); |
025ed0f3 SH |
1564 | f = NULL; |
1565 | if (ret < 0) | |
1566 | goto out_free_contents; | |
3ce74686 | 1567 | |
b4569e93 | 1568 | #if HAVE_LIBGNUTLS |
01efd4d3 | 1569 | tpath = get_template_path(t); |
85db5535 | 1570 | if (!tpath) { |
959aee9c | 1571 | ERROR("bad template: %s", t); |
025ed0f3 | 1572 | goto out_free_contents; |
3ce74686 SH |
1573 | } |
1574 | ||
85db5535 DE |
1575 | ret = sha1sum_file(tpath, md_value); |
1576 | if (ret < 0) { | |
1577 | ERROR("Error getting sha1sum of %s", tpath); | |
3ce74686 | 1578 | free(tpath); |
85db5535 | 1579 | goto out_free_contents; |
3ce74686 | 1580 | } |
85db5535 | 1581 | free(tpath); |
3ce74686 SH |
1582 | #endif |
1583 | ||
025ed0f3 | 1584 | f = fopen(path, "w"); |
025ed0f3 | 1585 | if (f == NULL) { |
3ce74686 SH |
1586 | SYSERROR("reopening config for writing"); |
1587 | free(contents); | |
1588 | return false; | |
1589 | } | |
1590 | fprintf(f, "# Template used to create this container: %s\n", t); | |
1591 | if (argv) { | |
1592 | fprintf(f, "# Parameters passed to the template:"); | |
1593 | while (*argv) { | |
1594 | fprintf(f, " %s", *argv); | |
1595 | argv++; | |
1596 | } | |
1597 | fprintf(f, "\n"); | |
1598 | } | |
1599 | #if HAVE_LIBGNUTLS | |
56698177 SH |
1600 | fprintf(f, "# Template script checksum (SHA-1): "); |
1601 | for (i=0; i<SHA_DIGEST_LENGTH; i++) | |
1602 | fprintf(f, "%02x", md_value[i]); | |
1603 | fprintf(f, "\n"); | |
3ce74686 | 1604 | #endif |
0520c252 | 1605 | fprintf(f, "# For additional config options, please look at lxc.container.conf(5)\n"); |
49a2ed80 SH |
1606 | fprintf(f, "\n# Uncomment the following line to support nesting containers:\n"); |
1607 | fprintf(f, "#lxc.include = " LXCTEMPLATECONFIG "/nesting.conf\n"); | |
1608 | fprintf(f, "# (Be aware this has security implications)\n\n"); | |
3ce74686 SH |
1609 | if (fwrite(contents, 1, flen, f) != flen) { |
1610 | SYSERROR("Writing original contents"); | |
1611 | free(contents); | |
1612 | fclose(f); | |
1613 | return false; | |
1614 | } | |
025ed0f3 SH |
1615 | ret = 0; |
1616 | out_free_contents: | |
3ce74686 | 1617 | free(contents); |
025ed0f3 SH |
1618 | out_error: |
1619 | if (f) { | |
1620 | int newret; | |
025ed0f3 | 1621 | newret = fclose(f); |
025ed0f3 SH |
1622 | if (ret == 0) |
1623 | ret = newret; | |
1624 | } | |
1625 | if (ret < 0) { | |
1626 | SYSERROR("Error prepending header"); | |
3ce74686 SH |
1627 | return false; |
1628 | } | |
1629 | return true; | |
1630 | } | |
1631 | ||
4df7f012 SH |
1632 | static void lxcapi_clear_config(struct lxc_container *c) |
1633 | { | |
f979ac15 SH |
1634 | if (c) { |
1635 | if (c->lxc_conf) { | |
1636 | lxc_conf_free(c->lxc_conf); | |
1637 | c->lxc_conf = NULL; | |
1638 | } | |
4df7f012 SH |
1639 | } |
1640 | } | |
1641 | ||
858377e4 SH |
1642 | #define do_lxcapi_clear_config(c) lxcapi_clear_config(c) |
1643 | ||
96b3cb40 SH |
1644 | /* |
1645 | * lxcapi_create: | |
1646 | * create a container with the given parameters. | |
1647 | * @c: container to be created. It has the lxcpath, name, and a starting | |
1648 | * configuration already set | |
1649 | * @t: the template to execute to instantiate the root filesystem and | |
1650 | * adjust the configuration. | |
1651 | * @bdevtype: backing store type to use. If NULL, dir will be used. | |
1652 | * @specs: additional parameters for the backing store, i.e. LVM vg to | |
1653 | * use. | |
1654 | * | |
1655 | * @argv: the arguments to pass to the template, terminated by NULL. If no | |
1656 | * arguments, you can just pass NULL. | |
1657 | */ | |
858377e4 | 1658 | static bool do_lxcapi_create(struct lxc_container *c, const char *t, |
85aec4ac CB |
1659 | const char *bdevtype, struct bdev_specs *specs, |
1660 | int flags, char *const argv[]) | |
96b3cb40 | 1661 | { |
e9e29a33 | 1662 | int partial_fd; |
96b3cb40 | 1663 | pid_t pid; |
e9e29a33 | 1664 | bool ret = false; |
85db5535 | 1665 | char *tpath = NULL; |
96b3cb40 SH |
1666 | |
1667 | if (!c) | |
1668 | return false; | |
1669 | ||
85db5535 DE |
1670 | if (t) { |
1671 | tpath = get_template_path(t); | |
1672 | if (!tpath) { | |
e9e29a33 | 1673 | ERROR("Unknown template \"%s\"", t); |
85db5535 DE |
1674 | goto out; |
1675 | } | |
96b3cb40 SH |
1676 | } |
1677 | ||
e9e29a33 CB |
1678 | /* If a template is passed in, and the rootfs already is defined in the |
1679 | * container config and exists, then the caller is trying to create an | |
1680 | * existing container. Return an error, but do NOT delete the container. | |
cf465fe4 | 1681 | */ |
858377e4 | 1682 | if (do_lxcapi_is_defined(c) && c->lxc_conf && c->lxc_conf->rootfs.path && |
cf465fe4 | 1683 | access(c->lxc_conf->rootfs.path, F_OK) == 0 && tpath) { |
e9e29a33 CB |
1684 | ERROR("Container \"%s\" already exists in \"%s\"", c->name, |
1685 | c->config_path); | |
6c6892b5 | 1686 | goto free_tpath; |
cf465fe4 SH |
1687 | } |
1688 | ||
6c6892b5 | 1689 | if (!c->lxc_conf) { |
858377e4 | 1690 | if (!do_lxcapi_load_config(c, lxc_global_config_value("lxc.default_config"))) { |
e9e29a33 CB |
1691 | ERROR("Error loading default configuration file %s", |
1692 | lxc_global_config_value("lxc.default_config")); | |
6c6892b5 DE |
1693 | goto free_tpath; |
1694 | } | |
96b3cb40 SH |
1695 | } |
1696 | ||
6c6892b5 DE |
1697 | if (!create_container_dir(c)) |
1698 | goto free_tpath; | |
1699 | ||
e9e29a33 CB |
1700 | /* If both template and rootfs.path are set, template is setup as |
1701 | * rootfs.path. The container is already created if we have a config and | |
1702 | * rootfs.path is accessible | |
0590e82c | 1703 | */ |
00370edd | 1704 | if (!c->lxc_conf->rootfs.path && !tpath) { |
e9e29a33 | 1705 | /* No template passed in and rootfs does not exist. */ |
00370edd | 1706 | if (!c->save_config(c, NULL)) { |
e9e29a33 | 1707 | ERROR("Failed to save initial config for \"%s\"", c->name); |
00370edd DW |
1708 | goto out; |
1709 | } | |
1710 | ret = true; | |
0590e82c | 1711 | goto out; |
00370edd | 1712 | } |
e9e29a33 CB |
1713 | |
1714 | /* Rootfs passed into configuration, but does not exist. */ | |
0590e82c | 1715 | if (c->lxc_conf->rootfs.path && access(c->lxc_conf->rootfs.path, F_OK) != 0) |
0590e82c | 1716 | goto out; |
e9e29a33 | 1717 | |
858377e4 | 1718 | if (do_lxcapi_is_defined(c) && c->lxc_conf->rootfs.path && !tpath) { |
e9e29a33 CB |
1719 | /* Rootfs already existed, user just wanted to save the loaded |
1720 | * configuration. | |
1721 | */ | |
0f4cdd77 | 1722 | if (!c->save_config(c, NULL)) |
e9e29a33 | 1723 | ERROR("Failed to save initial config for \"%s\"", c->name); |
0590e82c SH |
1724 | ret = true; |
1725 | goto out; | |
a69aad27 | 1726 | } |
96b3cb40 SH |
1727 | |
1728 | /* Mark that this container is being created */ | |
e9e29a33 CB |
1729 | partial_fd = create_partial(c); |
1730 | if (partial_fd < 0) | |
96b3cb40 SH |
1731 | goto out; |
1732 | ||
e9e29a33 | 1733 | /* No need to get disk lock bc we have the partial lock. */ |
96b3cb40 | 1734 | |
e9e29a33 | 1735 | /* Create the storage. |
96b3cb40 SH |
1736 | * Note we can't do this in the same task as we use to execute the |
1737 | * template because of the way zfs works. | |
1738 | * After you 'zfs create', zfs mounts the fs only in the initial | |
1739 | * namespace. | |
1740 | */ | |
1741 | pid = fork(); | |
1742 | if (pid < 0) { | |
e9e29a33 | 1743 | SYSERROR("Failed to fork task for container creation template"); |
8eb5694b SH |
1744 | goto out_unlock; |
1745 | } | |
1746 | ||
1a0e70ac | 1747 | if (pid == 0) { /* child */ |
10bc1861 | 1748 | struct lxc_storage *bdev = NULL; |
96b3cb40 | 1749 | |
10bc1861 CB |
1750 | bdev = do_storage_create(c, bdevtype, specs); |
1751 | if (!bdev) { | |
e9e29a33 CB |
1752 | ERROR("Failed to create %s storage for %s", |
1753 | bdevtype ? bdevtype : "(none)", c->name); | |
85aec4ac | 1754 | _exit(EXIT_FAILURE); |
96b3cb40 SH |
1755 | } |
1756 | ||
e9e29a33 | 1757 | /* Save config file again to store the new rootfs location. */ |
858377e4 | 1758 | if (!do_lxcapi_save_config(c, NULL)) { |
e9e29a33 CB |
1759 | ERROR("Failed to save initial config for %s", c->name); |
1760 | /* Parent task won't see the storage driver in the | |
1761 | * config so we delete it. | |
1762 | */ | |
96b3cb40 SH |
1763 | bdev->ops->umount(bdev); |
1764 | bdev->ops->destroy(bdev); | |
85aec4ac | 1765 | _exit(EXIT_FAILURE); |
96b3cb40 | 1766 | } |
85aec4ac | 1767 | _exit(EXIT_SUCCESS); |
96b3cb40 SH |
1768 | } |
1769 | if (wait_for_pid(pid) != 0) | |
a09295f8 | 1770 | goto out_unlock; |
96b3cb40 | 1771 | |
e9e29a33 | 1772 | /* Reload config to get the rootfs. */ |
a3b47c09 | 1773 | lxc_conf_free(c->lxc_conf); |
96b3cb40 SH |
1774 | c->lxc_conf = NULL; |
1775 | if (!load_config_locked(c, c->configfile)) | |
a09295f8 | 1776 | goto out_unlock; |
96b3cb40 | 1777 | |
dc23c1c8 | 1778 | if (!create_run_template(c, tpath, !!(flags & LXC_CREATE_QUIET), argv)) |
96b3cb40 SH |
1779 | goto out_unlock; |
1780 | ||
1a0e70ac CB |
1781 | /* Now clear out the lxc_conf we have, reload from the created |
1782 | * container. | |
1783 | */ | |
858377e4 | 1784 | do_lxcapi_clear_config(c); |
3ce74686 | 1785 | |
9d65a487 KY |
1786 | if (t) { |
1787 | if (!prepend_lxc_header(c->configfile, tpath, argv)) { | |
e9e29a33 | 1788 | ERROR("Failed to prepend header to config file"); |
9d65a487 KY |
1789 | goto out_unlock; |
1790 | } | |
3ce74686 | 1791 | } |
a69aad27 | 1792 | ret = load_config_locked(c, c->configfile); |
72d0e1cb SG |
1793 | |
1794 | out_unlock: | |
3e625e2d SH |
1795 | if (partial_fd >= 0) |
1796 | remove_partial(c, partial_fd); | |
72d0e1cb | 1797 | out: |
c55d4505 | 1798 | if (!ret) |
17a367d8 | 1799 | container_destroy(c, NULL); |
6c6892b5 | 1800 | free_tpath: |
f10fad2f | 1801 | free(tpath); |
a69aad27 | 1802 | return ret; |
72d0e1cb SG |
1803 | } |
1804 | ||
858377e4 | 1805 | static bool lxcapi_create(struct lxc_container *c, const char *t, |
e9e29a33 CB |
1806 | const char *bdevtype, struct bdev_specs *specs, |
1807 | int flags, char *const argv[]) | |
858377e4 SH |
1808 | { |
1809 | bool ret; | |
1810 | current_config = c ? c->lxc_conf : NULL; | |
1811 | ret = do_lxcapi_create(c, t, bdevtype, specs, flags, argv); | |
1812 | current_config = NULL; | |
1813 | return ret; | |
1814 | } | |
1815 | ||
1816 | static bool do_lxcapi_reboot(struct lxc_container *c) | |
3e625e2d SH |
1817 | { |
1818 | pid_t pid; | |
dd267776 | 1819 | int rebootsignal = SIGINT; |
3e625e2d SH |
1820 | |
1821 | if (!c) | |
1822 | return false; | |
858377e4 | 1823 | if (!do_lxcapi_is_running(c)) |
3e625e2d | 1824 | return false; |
858377e4 | 1825 | pid = do_lxcapi_init_pid(c); |
3e625e2d SH |
1826 | if (pid <= 0) |
1827 | return false; | |
dd267776 BP |
1828 | if (c->lxc_conf && c->lxc_conf->rebootsignal) |
1829 | rebootsignal = c->lxc_conf->rebootsignal; | |
591614a7 CB |
1830 | if (kill(pid, rebootsignal) < 0) { |
1831 | WARN("Could not send signal %d to pid %d.", rebootsignal, pid); | |
3e625e2d | 1832 | return false; |
591614a7 | 1833 | } |
3e625e2d SH |
1834 | return true; |
1835 | ||
1836 | } | |
1837 | ||
858377e4 SH |
1838 | WRAP_API(bool, lxcapi_reboot) |
1839 | ||
d39b10eb CB |
1840 | static bool do_lxcapi_reboot2(struct lxc_container *c, int timeout) |
1841 | { | |
1842 | int killret, ret; | |
1843 | pid_t pid; | |
1844 | int rebootsignal = SIGINT, state_client_fd = -1; | |
1845 | lxc_state_t states[MAX_STATE] = {0}; | |
1846 | ||
1847 | if (!c) | |
1848 | return false; | |
1849 | ||
1850 | if (!do_lxcapi_is_running(c)) | |
1851 | return true; | |
1852 | ||
1853 | pid = do_lxcapi_init_pid(c); | |
1854 | if (pid <= 0) | |
1855 | return true; | |
1856 | ||
1857 | if (c->lxc_conf && c->lxc_conf->rebootsignal) | |
1858 | rebootsignal = c->lxc_conf->rebootsignal; | |
1859 | ||
1860 | /* Add a new state client before sending the shutdown signal so that we | |
1861 | * don't miss a state. | |
1862 | */ | |
1863 | if (timeout != 0) { | |
1864 | states[RUNNING] = 2; | |
1865 | ret = lxc_cmd_add_state_client(c->name, c->config_path, states, | |
1866 | &state_client_fd); | |
1867 | if (ret < 0) | |
1868 | return false; | |
1869 | ||
1870 | if (state_client_fd < 0) | |
1871 | return false; | |
1872 | ||
1873 | if (ret == RUNNING) | |
1874 | return true; | |
1875 | ||
1876 | if (ret < MAX_STATE) | |
1877 | return false; | |
1878 | } | |
1879 | ||
1880 | /* Send reboot signal to container. */ | |
1881 | killret = kill(pid, rebootsignal); | |
1882 | if (killret < 0) { | |
1883 | WARN("Could not send signal %d to pid %d", rebootsignal, pid); | |
1884 | if (state_client_fd >= 0) | |
1885 | close(state_client_fd); | |
1886 | return false; | |
1887 | } | |
1888 | TRACE("Sent signal %d to pid %d", rebootsignal, pid); | |
1889 | ||
1890 | if (timeout == 0) | |
1891 | return true; | |
1892 | ||
1893 | ret = lxc_cmd_sock_rcv_state(state_client_fd, timeout); | |
1894 | close(state_client_fd); | |
1895 | if (ret < 0) | |
1896 | return false; | |
1897 | ||
1898 | TRACE("Received state \"%s\"", lxc_state2str(ret)); | |
1899 | if (ret != RUNNING) | |
1900 | return false; | |
1901 | ||
1902 | return true; | |
1903 | } | |
1904 | ||
1905 | WRAP_API_1(bool, lxcapi_reboot2, int) | |
1906 | ||
858377e4 | 1907 | static bool do_lxcapi_shutdown(struct lxc_container *c, int timeout) |
72d0e1cb | 1908 | { |
f8bdb6dc | 1909 | int killret, ret; |
72d0e1cb | 1910 | pid_t pid; |
f8bdb6dc | 1911 | int haltsignal = SIGPWR, state_client_fd = -1; |
92e35018 | 1912 | lxc_state_t states[MAX_STATE] = {0}; |
72d0e1cb SG |
1913 | |
1914 | if (!c) | |
1915 | return false; | |
1916 | ||
858377e4 | 1917 | if (!do_lxcapi_is_running(c)) |
72d0e1cb | 1918 | return true; |
f8bdb6dc | 1919 | |
858377e4 | 1920 | pid = do_lxcapi_init_pid(c); |
72d0e1cb SG |
1921 | if (pid <= 0) |
1922 | return true; | |
330ae3d3 CB |
1923 | |
1924 | /* Detect whether we should send SIGRTMIN + 3 (e.g. systemd). */ | |
1925 | if (task_blocking_signal(pid, (SIGRTMIN + 3))) | |
1926 | haltsignal = (SIGRTMIN + 3); | |
1927 | ||
b0227444 | 1928 | if (c->lxc_conf && c->lxc_conf->haltsignal) |
f0f1d8c0 | 1929 | haltsignal = c->lxc_conf->haltsignal; |
330ae3d3 | 1930 | |
92e35018 CB |
1931 | /* Add a new state client before sending the shutdown signal so that we |
1932 | * don't miss a state. | |
1933 | */ | |
f8bdb6dc CB |
1934 | if (timeout != 0) { |
1935 | states[STOPPED] = 1; | |
1936 | ret = lxc_cmd_add_state_client(c->name, c->config_path, states, | |
1937 | &state_client_fd); | |
1938 | if (ret < 0) | |
1939 | return false; | |
92e35018 | 1940 | |
f8bdb6dc CB |
1941 | if (state_client_fd < 0) |
1942 | return false; | |
1943 | ||
1944 | if (ret == STOPPED) | |
1945 | return true; | |
591614a7 | 1946 | |
f8bdb6dc | 1947 | if (ret < MAX_STATE) |
92e35018 | 1948 | return false; |
92e35018 CB |
1949 | } |
1950 | ||
f8bdb6dc CB |
1951 | /* Send shutdown signal to container. */ |
1952 | killret = kill(pid, haltsignal); | |
1953 | if (killret < 0) { | |
1954 | WARN("Could not send signal %d to pid %d", haltsignal, pid); | |
1955 | if (state_client_fd >= 0) | |
1956 | close(state_client_fd); | |
1957 | return false; | |
1958 | } | |
1959 | TRACE("Sent signal %d to pid %d", haltsignal, pid); | |
1960 | ||
1961 | if (timeout == 0) | |
1962 | return true; | |
1963 | ||
1964 | ret = lxc_cmd_sock_rcv_state(state_client_fd, timeout); | |
1965 | close(state_client_fd); | |
1966 | if (ret < 0) | |
1967 | return false; | |
1968 | ||
1969 | TRACE("Received state \"%s\"", lxc_state2str(ret)); | |
1970 | if (ret != STOPPED) | |
1971 | return false; | |
1972 | ||
1973 | return true; | |
72d0e1cb SG |
1974 | } |
1975 | ||
858377e4 SH |
1976 | WRAP_API_1(bool, lxcapi_shutdown, int) |
1977 | ||
1897e3bc | 1978 | static bool lxcapi_createl(struct lxc_container *c, const char *t, |
dc23c1c8 | 1979 | const char *bdevtype, struct bdev_specs *specs, int flags, ...) |
72d0e1cb SG |
1980 | { |
1981 | bool bret = false; | |
a0e93eeb | 1982 | char **args = NULL; |
72d0e1cb | 1983 | va_list ap; |
72d0e1cb SG |
1984 | |
1985 | if (!c) | |
1986 | return false; | |
1987 | ||
858377e4 SH |
1988 | current_config = c->lxc_conf; |
1989 | ||
72d0e1cb SG |
1990 | /* |
1991 | * since we're going to wait for create to finish, I don't think we | |
1992 | * need to get a copy of the arguments. | |
1993 | */ | |
dc23c1c8 | 1994 | va_start(ap, flags); |
a0e93eeb | 1995 | args = lxc_va_arg_list_to_argv(ap, 0, 0); |
72d0e1cb | 1996 | va_end(ap); |
a0e93eeb | 1997 | if (!args) { |
e9e29a33 | 1998 | ERROR("Failed to allocate memory"); |
a0e93eeb CS |
1999 | goto out; |
2000 | } | |
72d0e1cb | 2001 | |
858377e4 | 2002 | bret = do_lxcapi_create(c, t, bdevtype, specs, flags, args); |
72d0e1cb SG |
2003 | |
2004 | out: | |
a0e93eeb | 2005 | free(args); |
858377e4 | 2006 | current_config = NULL; |
72d0e1cb SG |
2007 | return bret; |
2008 | } | |
2009 | ||
6b0d5538 SH |
2010 | static void do_clear_unexp_config_line(struct lxc_conf *conf, const char *key) |
2011 | { | |
4222a9f4 CB |
2012 | if (!strcmp(key, "lxc.cgroup")) |
2013 | return clear_unexp_config_line(conf, key, true); | |
2014 | ||
2015 | if (!strcmp(key, "lxc.network")) | |
2016 | return clear_unexp_config_line(conf, key, true); | |
2017 | ||
2018 | if (!strcmp(key, "lxc.net")) | |
2019 | return clear_unexp_config_line(conf, key, true); | |
2020 | ||
2021 | /* Clear a network with a specific index. */ | |
2022 | if (!strncmp(key, "lxc.net.", 8)) { | |
2023 | int ret; | |
2024 | const char *idx; | |
2025 | ||
2026 | idx = key + 8; | |
2027 | ret = lxc_safe_uint(idx, &(unsigned int){0}); | |
2028 | if (!ret) | |
2029 | return clear_unexp_config_line(conf, key, true); | |
2030 | } | |
2031 | ||
2032 | if (!strcmp(key, "lxc.hook")) | |
2033 | return clear_unexp_config_line(conf, key, true); | |
2034 | ||
2035 | return clear_unexp_config_line(conf, key, false); | |
6b0d5538 SH |
2036 | } |
2037 | ||
6afd673f CB |
2038 | static bool do_lxcapi_clear_config_item(struct lxc_container *c, |
2039 | const char *key) | |
72d0e1cb | 2040 | { |
6afd673f CB |
2041 | int ret = 1; |
2042 | struct lxc_config_t *config; | |
72d0e1cb SG |
2043 | |
2044 | if (!c || !c->lxc_conf) | |
2045 | return false; | |
6afd673f | 2046 | |
5cee8c50 | 2047 | if (container_mem_lock(c)) |
72d0e1cb | 2048 | return false; |
6afd673f | 2049 | |
300df83e | 2050 | config = lxc_get_config(key); |
6afd673f CB |
2051 | /* Verify that the config key exists and that it has a callback |
2052 | * implemented. | |
2053 | */ | |
2054 | if (config && config->clr) | |
26471403 | 2055 | ret = config->clr(key, c->lxc_conf, NULL); |
6b0d5538 SH |
2056 | if (!ret) |
2057 | do_clear_unexp_config_line(c->lxc_conf, key); | |
6afd673f | 2058 | |
5cee8c50 | 2059 | container_mem_unlock(c); |
72d0e1cb SG |
2060 | return ret == 0; |
2061 | } | |
2062 | ||
858377e4 SH |
2063 | WRAP_API_1(bool, lxcapi_clear_config_item, const char *) |
2064 | ||
e0f59189 | 2065 | static inline bool enter_net_ns(struct lxc_container *c) |
51d0854c | 2066 | { |
858377e4 | 2067 | pid_t pid = do_lxcapi_init_pid(c); |
ae22a220 | 2068 | |
0e6e3a41 | 2069 | if ((geteuid() != 0 || (c->lxc_conf && !lxc_list_empty(&c->lxc_conf->id_map))) && access("/proc/self/ns/user", F_OK) == 0) { |
51d0854c DY |
2070 | if (!switch_to_ns(pid, "user")) |
2071 | return false; | |
9c83a661 | 2072 | } |
51d0854c | 2073 | return switch_to_ns(pid, "net"); |
799f29ab ÇO |
2074 | } |
2075 | ||
1a0e70ac | 2076 | /* Used by qsort and bsearch functions for comparing names. */ |
9c88ff1f ÇO |
2077 | static inline int string_cmp(char **first, char **second) |
2078 | { | |
2079 | return strcmp(*first, *second); | |
2080 | } | |
2081 | ||
1a0e70ac CB |
2082 | /* Used by qsort and bsearch functions for comparing container names. */ |
2083 | static inline int container_cmp(struct lxc_container **first, | |
2084 | struct lxc_container **second) | |
9c88ff1f ÇO |
2085 | { |
2086 | return strcmp((*first)->name, (*second)->name); | |
2087 | } | |
2088 | ||
2089 | static bool add_to_array(char ***names, char *cname, int pos) | |
2090 | { | |
2091 | char **newnames = realloc(*names, (pos+1) * sizeof(char *)); | |
2092 | if (!newnames) { | |
2093 | ERROR("Out of memory"); | |
2094 | return false; | |
2095 | } | |
2096 | ||
2097 | *names = newnames; | |
2098 | newnames[pos] = strdup(cname); | |
2099 | if (!newnames[pos]) | |
2100 | return false; | |
2101 | ||
1a0e70ac CB |
2102 | /* Sort the arrray as we will use binary search on it. */ |
2103 | qsort(newnames, pos + 1, sizeof(char *), | |
2104 | (int (*)(const void *, const void *))string_cmp); | |
9c88ff1f ÇO |
2105 | |
2106 | return true; | |
2107 | } | |
2108 | ||
1a0e70ac CB |
2109 | static bool add_to_clist(struct lxc_container ***list, struct lxc_container *c, |
2110 | int pos, bool sort) | |
9c88ff1f | 2111 | { |
1a0e70ac | 2112 | struct lxc_container **newlist = realloc(*list, (pos + 1) * sizeof(struct lxc_container *)); |
9c88ff1f ÇO |
2113 | if (!newlist) { |
2114 | ERROR("Out of memory"); | |
2115 | return false; | |
2116 | } | |
2117 | ||
2118 | *list = newlist; | |
2119 | newlist[pos] = c; | |
2120 | ||
1a0e70ac | 2121 | /* Sort the arrray as we will use binary search on it. */ |
2871830a | 2122 | if (sort) |
1a0e70ac CB |
2123 | qsort(newlist, pos + 1, sizeof(struct lxc_container *), |
2124 | (int (*)(const void *, const void *))container_cmp); | |
9c88ff1f ÇO |
2125 | |
2126 | return true; | |
2127 | } | |
2128 | ||
2129 | static char** get_from_array(char ***names, char *cname, int size) | |
2130 | { | |
2131 | return (char **)bsearch(&cname, *names, size, sizeof(char *), (int (*)(const void *, const void *))string_cmp); | |
2132 | } | |
2133 | ||
2134 | ||
2135 | static bool array_contains(char ***names, char *cname, int size) { | |
2136 | if(get_from_array(names, cname, size) != NULL) | |
2137 | return true; | |
2138 | return false; | |
2139 | } | |
2140 | ||
2141 | static bool remove_from_array(char ***names, char *cname, int size) | |
2142 | { | |
2143 | char **result = get_from_array(names, cname, size); | |
2144 | if (result != NULL) { | |
2145 | free(result); | |
2146 | return true; | |
2147 | } | |
2148 | return false; | |
2149 | } | |
2150 | ||
858377e4 | 2151 | static char ** do_lxcapi_get_interfaces(struct lxc_container *c) |
799f29ab | 2152 | { |
ae22a220 ÇO |
2153 | pid_t pid; |
2154 | int i, count = 0, pipefd[2]; | |
9c88ff1f | 2155 | char **interfaces = NULL; |
ae22a220 | 2156 | char interface[IFNAMSIZ]; |
799f29ab | 2157 | |
ae22a220 ÇO |
2158 | if(pipe(pipefd) < 0) { |
2159 | SYSERROR("pipe failed"); | |
2160 | return NULL; | |
c868b261 ÇO |
2161 | } |
2162 | ||
ae22a220 ÇO |
2163 | pid = fork(); |
2164 | if (pid < 0) { | |
959aee9c | 2165 | SYSERROR("failed to fork task to get interfaces information"); |
ae22a220 ÇO |
2166 | close(pipefd[0]); |
2167 | close(pipefd[1]); | |
2168 | return NULL; | |
2169 | } | |
799f29ab | 2170 | |
1a0e70ac | 2171 | if (pid == 0) { /* child */ |
ae22a220 ÇO |
2172 | int ret = 1, nbytes; |
2173 | struct ifaddrs *interfaceArray = NULL, *tempIfAddr = NULL; | |
2174 | ||
2175 | /* close the read-end of the pipe */ | |
2176 | close(pipefd[0]); | |
2177 | ||
e0f59189 | 2178 | if (!enter_net_ns(c)) { |
ae22a220 ÇO |
2179 | SYSERROR("failed to enter namespace"); |
2180 | goto out; | |
2181 | } | |
2182 | ||
2183 | /* Grab the list of interfaces */ | |
2184 | if (getifaddrs(&interfaceArray)) { | |
2185 | SYSERROR("failed to get interfaces list"); | |
2186 | goto out; | |
2187 | } | |
2188 | ||
2189 | /* Iterate through the interfaces */ | |
2190 | for (tempIfAddr = interfaceArray; tempIfAddr != NULL; tempIfAddr = tempIfAddr->ifa_next) { | |
2191 | nbytes = write(pipefd[1], tempIfAddr->ifa_name, IFNAMSIZ); | |
2192 | if (nbytes < 0) { | |
2193 | ERROR("write failed"); | |
2194 | goto out; | |
2195 | } | |
2196 | count++; | |
2197 | } | |
2198 | ret = 0; | |
2199 | ||
2200 | out: | |
2201 | if (interfaceArray) | |
2202 | freeifaddrs(interfaceArray); | |
2203 | ||
2204 | /* close the write-end of the pipe, thus sending EOF to the reader */ | |
2205 | close(pipefd[1]); | |
02c611b0 | 2206 | _exit(ret); |
799f29ab ÇO |
2207 | } |
2208 | ||
ae22a220 ÇO |
2209 | /* close the write-end of the pipe */ |
2210 | close(pipefd[1]); | |
2211 | ||
358afd84 | 2212 | while (read(pipefd[0], &interface, IFNAMSIZ) == IFNAMSIZ) { |
3151d4e2 CB |
2213 | interface[IFNAMSIZ - 1] = '\0'; |
2214 | ||
ae22a220 ÇO |
2215 | if (array_contains(&interfaces, interface, count)) |
2216 | continue; | |
799f29ab | 2217 | |
ae22a220 | 2218 | if(!add_to_array(&interfaces, interface, count)) |
3151d4e2 CB |
2219 | ERROR("Failed to add \"%s\" to array", interface); |
2220 | ||
9c88ff1f ÇO |
2221 | count++; |
2222 | } | |
799f29ab | 2223 | |
ae22a220 ÇO |
2224 | if (wait_for_pid(pid) != 0) { |
2225 | for(i=0;i<count;i++) | |
2226 | free(interfaces[i]); | |
2227 | free(interfaces); | |
2228 | interfaces = NULL; | |
2229 | } | |
9c88ff1f | 2230 | |
ae22a220 ÇO |
2231 | /* close the read-end of the pipe */ |
2232 | close(pipefd[0]); | |
799f29ab | 2233 | |
9c88ff1f ÇO |
2234 | /* Append NULL to the array */ |
2235 | if(interfaces) | |
2236 | interfaces = (char **)lxc_append_null_to_array((void **)interfaces, count); | |
799f29ab | 2237 | |
9c88ff1f | 2238 | return interfaces; |
799f29ab ÇO |
2239 | } |
2240 | ||
858377e4 SH |
2241 | WRAP_API(char **, lxcapi_get_interfaces) |
2242 | ||
2243 | static char** do_lxcapi_get_ips(struct lxc_container *c, const char* interface, const char* family, int scope) | |
799f29ab | 2244 | { |
ae22a220 ÇO |
2245 | pid_t pid; |
2246 | int i, count = 0, pipefd[2]; | |
9c88ff1f | 2247 | char **addresses = NULL; |
ae22a220 | 2248 | char address[INET6_ADDRSTRLEN]; |
799f29ab | 2249 | |
ae22a220 ÇO |
2250 | if(pipe(pipefd) < 0) { |
2251 | SYSERROR("pipe failed"); | |
2252 | return NULL; | |
c868b261 ÇO |
2253 | } |
2254 | ||
ae22a220 ÇO |
2255 | pid = fork(); |
2256 | if (pid < 0) { | |
959aee9c | 2257 | SYSERROR("failed to fork task to get container ips"); |
ae22a220 ÇO |
2258 | close(pipefd[0]); |
2259 | close(pipefd[1]); | |
2260 | return NULL; | |
9c83a661 SG |
2261 | } |
2262 | ||
1a0e70ac | 2263 | if (pid == 0) { /* child */ |
ae22a220 ÇO |
2264 | int ret = 1, nbytes; |
2265 | struct ifaddrs *interfaceArray = NULL, *tempIfAddr = NULL; | |
2266 | char addressOutputBuffer[INET6_ADDRSTRLEN]; | |
2267 | void *tempAddrPtr = NULL; | |
2268 | char *address = NULL; | |
fe218ca3 | 2269 | |
ae22a220 ÇO |
2270 | /* close the read-end of the pipe */ |
2271 | close(pipefd[0]); | |
2272 | ||
e0f59189 | 2273 | if (!enter_net_ns(c)) { |
ae22a220 ÇO |
2274 | SYSERROR("failed to enter namespace"); |
2275 | goto out; | |
9c83a661 | 2276 | } |
ae22a220 ÇO |
2277 | |
2278 | /* Grab the list of interfaces */ | |
2279 | if (getifaddrs(&interfaceArray)) { | |
2280 | SYSERROR("failed to get interfaces list"); | |
2281 | goto out; | |
2282 | } | |
2283 | ||
2284 | /* Iterate through the interfaces */ | |
2285 | for (tempIfAddr = interfaceArray; tempIfAddr != NULL; tempIfAddr = tempIfAddr->ifa_next) { | |
2286 | if (tempIfAddr->ifa_addr == NULL) | |
9c83a661 SG |
2287 | continue; |
2288 | ||
ae22a220 ÇO |
2289 | if(tempIfAddr->ifa_addr->sa_family == AF_INET) { |
2290 | if (family && strcmp(family, "inet")) | |
2291 | continue; | |
2292 | tempAddrPtr = &((struct sockaddr_in *)tempIfAddr->ifa_addr)->sin_addr; | |
2293 | } | |
2294 | else { | |
2295 | if (family && strcmp(family, "inet6")) | |
2296 | continue; | |
2297 | ||
2298 | if (((struct sockaddr_in6 *)tempIfAddr->ifa_addr)->sin6_scope_id != scope) | |
2299 | continue; | |
2300 | ||
2301 | tempAddrPtr = &((struct sockaddr_in6 *)tempIfAddr->ifa_addr)->sin6_addr; | |
2302 | } | |
2303 | ||
2304 | if (interface && strcmp(interface, tempIfAddr->ifa_name)) | |
2305 | continue; | |
2306 | else if (!interface && strcmp("lo", tempIfAddr->ifa_name) == 0) | |
9c83a661 SG |
2307 | continue; |
2308 | ||
ae22a220 ÇO |
2309 | address = (char *)inet_ntop(tempIfAddr->ifa_addr->sa_family, |
2310 | tempAddrPtr, | |
2311 | addressOutputBuffer, | |
2312 | sizeof(addressOutputBuffer)); | |
2313 | if (!address) | |
2314 | continue; | |
2315 | ||
2316 | nbytes = write(pipefd[1], address, INET6_ADDRSTRLEN); | |
2317 | if (nbytes < 0) { | |
2318 | ERROR("write failed"); | |
2319 | goto out; | |
2320 | } | |
2321 | count++; | |
9c83a661 | 2322 | } |
ae22a220 | 2323 | ret = 0; |
9c83a661 | 2324 | |
ae22a220 ÇO |
2325 | out: |
2326 | if(interfaceArray) | |
2327 | freeifaddrs(interfaceArray); | |
9c83a661 | 2328 | |
ae22a220 ÇO |
2329 | /* close the write-end of the pipe, thus sending EOF to the reader */ |
2330 | close(pipefd[1]); | |
fe1ce58c | 2331 | _exit(ret); |
6849cb5b | 2332 | } |
9c83a661 | 2333 | |
ae22a220 ÇO |
2334 | /* close the write-end of the pipe */ |
2335 | close(pipefd[1]); | |
2336 | ||
358afd84 | 2337 | while (read(pipefd[0], &address, INET6_ADDRSTRLEN) == INET6_ADDRSTRLEN) { |
9c88ff1f | 2338 | if(!add_to_array(&addresses, address, count)) |
ae22a220 | 2339 | ERROR("PARENT: add_to_array failed"); |
9c88ff1f | 2340 | count++; |
9c83a661 SG |
2341 | } |
2342 | ||
ae22a220 ÇO |
2343 | if (wait_for_pid(pid) != 0) { |
2344 | for(i=0;i<count;i++) | |
2345 | free(addresses[i]); | |
2346 | free(addresses); | |
2347 | addresses = NULL; | |
2348 | } | |
9c83a661 | 2349 | |
ae22a220 ÇO |
2350 | /* close the read-end of the pipe */ |
2351 | close(pipefd[0]); | |
9c83a661 SG |
2352 | |
2353 | /* Append NULL to the array */ | |
9c88ff1f ÇO |
2354 | if(addresses) |
2355 | addresses = (char **)lxc_append_null_to_array((void **)addresses, count); | |
9c83a661 SG |
2356 | |
2357 | return addresses; | |
2358 | } | |
2359 | ||
858377e4 SH |
2360 | WRAP_API_3(char **, lxcapi_get_ips, const char *, const char *, int) |
2361 | ||
2362 | static int do_lxcapi_get_config_item(struct lxc_container *c, const char *key, char *retv, int inlen) | |
72d0e1cb | 2363 | { |
fce687aa CB |
2364 | int ret = -1; |
2365 | struct lxc_config_t *config; | |
72d0e1cb SG |
2366 | |
2367 | if (!c || !c->lxc_conf) | |
2368 | return -1; | |
fce687aa | 2369 | |
5cee8c50 | 2370 | if (container_mem_lock(c)) |
72d0e1cb | 2371 | return -1; |
fce687aa | 2372 | |
300df83e | 2373 | config = lxc_get_config(key); |
fce687aa CB |
2374 | /* Verify that the config key exists and that it has a callback |
2375 | * implemented. | |
2376 | */ | |
2377 | if (config && config->get) | |
cccd2219 | 2378 | ret = config->get(key, retv, inlen, c->lxc_conf, NULL); |
fce687aa | 2379 | |
5cee8c50 | 2380 | container_mem_unlock(c); |
72d0e1cb SG |
2381 | return ret; |
2382 | } | |
2383 | ||
858377e4 SH |
2384 | WRAP_API_3(int, lxcapi_get_config_item, const char *, char *, int) |
2385 | ||
2386 | static char* do_lxcapi_get_running_config_item(struct lxc_container *c, const char *key) | |
8ac18377 ÇO |
2387 | { |
2388 | char *ret; | |
2389 | ||
2390 | if (!c || !c->lxc_conf) | |
2391 | return NULL; | |
2392 | if (container_mem_lock(c)) | |
2393 | return NULL; | |
858377e4 | 2394 | ret = lxc_cmd_get_config_item(c->name, key, do_lxcapi_get_config_path(c)); |
8ac18377 ÇO |
2395 | container_mem_unlock(c); |
2396 | return ret; | |
2397 | } | |
2398 | ||
858377e4 SH |
2399 | WRAP_API_1(char *, lxcapi_get_running_config_item, const char *) |
2400 | ||
2401 | static int do_lxcapi_get_keys(struct lxc_container *c, const char *key, char *retv, int inlen) | |
72d0e1cb | 2402 | { |
300df83e CB |
2403 | int ret = -1; |
2404 | ||
2405 | /* List all config items. */ | |
72d0e1cb | 2406 | if (!key) |
cfc67626 | 2407 | return lxc_list_config_items(retv, inlen); |
300df83e | 2408 | |
72d0e1cb SG |
2409 | if (!c || !c->lxc_conf) |
2410 | return -1; | |
300df83e | 2411 | |
5cee8c50 | 2412 | if (container_mem_lock(c)) |
72d0e1cb | 2413 | return -1; |
300df83e CB |
2414 | |
2415 | /* Support 'lxc.net.<idx>', i.e. 'lxc.net.0' | |
2416 | * This is an intelligent result to show which keys are valid given the | |
2417 | * type of nic it is. | |
2418 | */ | |
6fba98b5 | 2419 | if (strncmp(key, "lxc.net.", 8) == 0) |
01f55c40 | 2420 | ret = lxc_list_net(c->lxc_conf, key, retv, inlen); |
fe9b7349 CB |
2421 | else |
2422 | ret = lxc_list_subkeys(c->lxc_conf, key, retv, inlen); | |
300df83e | 2423 | |
5cee8c50 | 2424 | container_mem_unlock(c); |
72d0e1cb SG |
2425 | return ret; |
2426 | } | |
2427 | ||
858377e4 SH |
2428 | WRAP_API_3(int, lxcapi_get_keys, const char *, char *, int) |
2429 | ||
2430 | static bool do_lxcapi_save_config(struct lxc_container *c, const char *alt_file) | |
72d0e1cb | 2431 | { |
0e1a60b0 | 2432 | int fd, lret; |
39dc698c | 2433 | bool ret = false, need_disklock = false; |
39dc698c | 2434 | |
72d0e1cb SG |
2435 | if (!alt_file) |
2436 | alt_file = c->configfile; | |
2437 | if (!alt_file) | |
1a0e70ac | 2438 | return false; |
39dc698c | 2439 | |
1a0e70ac | 2440 | /* If we haven't yet loaded a config, load the stock config. */ |
39dc698c | 2441 | if (!c->lxc_conf) { |
858377e4 | 2442 | if (!do_lxcapi_load_config(c, lxc_global_config_value("lxc.default_config"))) { |
0e1a60b0 CB |
2443 | ERROR("Error loading default configuration file %s " |
2444 | "while saving %s", | |
2445 | lxc_global_config_value("lxc.default_config"), | |
2446 | c->name); | |
72d0e1cb SG |
2447 | return false; |
2448 | } | |
39dc698c | 2449 | } |
72d0e1cb | 2450 | |
5a3d2e1e SG |
2451 | if (!create_container_dir(c)) |
2452 | return false; | |
2453 | ||
1a0e70ac CB |
2454 | /* If we're writing to the container's config file, take the disk lock. |
2455 | * Otherwise just take the memlock to protect the struct lxc_container | |
2456 | * while we're traversing it. | |
39dc698c SH |
2457 | */ |
2458 | if (strcmp(c->configfile, alt_file) == 0) | |
2459 | need_disklock = true; | |
2460 | ||
2461 | if (need_disklock) | |
2462 | lret = container_disk_lock(c); | |
2463 | else | |
2464 | lret = container_mem_lock(c); | |
2465 | ||
2466 | if (lret) | |
72d0e1cb | 2467 | return false; |
39dc698c | 2468 | |
0e1a60b0 CB |
2469 | fd = open(alt_file, O_WRONLY | O_CREAT | O_CLOEXEC, |
2470 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); | |
2471 | if (fd < 0) | |
2472 | goto on_error; | |
2473 | ||
2474 | lret = write_config(fd, c->lxc_conf); | |
2475 | close(fd); | |
2476 | if (lret < 0) | |
2477 | goto on_error; | |
2478 | ||
39dc698c SH |
2479 | ret = true; |
2480 | ||
0e1a60b0 | 2481 | on_error: |
39dc698c SH |
2482 | if (need_disklock) |
2483 | container_disk_unlock(c); | |
2484 | else | |
2485 | container_mem_unlock(c); | |
0e1a60b0 | 2486 | |
39dc698c | 2487 | return ret; |
72d0e1cb SG |
2488 | } |
2489 | ||
858377e4 SH |
2490 | WRAP_API_1(bool, lxcapi_save_config, const char *) |
2491 | ||
0ea055b3 CB |
2492 | |
2493 | static bool mod_rdep(struct lxc_container *c0, struct lxc_container *c, bool inc) | |
dfb31b25 | 2494 | { |
0ea055b3 CB |
2495 | FILE *f1; |
2496 | struct stat fbuf; | |
42342bed CB |
2497 | void *buf = NULL; |
2498 | char *del = NULL; | |
dfb31b25 | 2499 | char path[MAXPATHLEN]; |
0ea055b3 CB |
2500 | char newpath[MAXPATHLEN]; |
2501 | int fd, ret, n = 0, v = 0; | |
dfb31b25 | 2502 | bool bret = false; |
42342bed | 2503 | size_t len = 0, bytes = 0; |
dfb31b25 | 2504 | |
0ea055b3 | 2505 | if (container_disk_lock(c0)) |
dfb31b25 | 2506 | return false; |
0ea055b3 CB |
2507 | |
2508 | ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_snapshots", c0->config_path, c0->name); | |
dfb31b25 SH |
2509 | if (ret < 0 || ret > MAXPATHLEN) |
2510 | goto out; | |
0ea055b3 CB |
2511 | ret = snprintf(newpath, MAXPATHLEN, "%s\n%s\n", c->config_path, c->name); |
2512 | if (ret < 0 || ret > MAXPATHLEN) | |
dfb31b25 | 2513 | goto out; |
0ea055b3 CB |
2514 | |
2515 | /* If we find an lxc-snapshot file using the old format only listing the | |
2516 | * number of snapshots we will keep using it. */ | |
2517 | f1 = fopen(path, "r"); | |
2518 | if (f1) { | |
2519 | n = fscanf(f1, "%d", &v); | |
2520 | fclose(f1); | |
2521 | if (n == 1 && v == 0) { | |
2522 | remove(path); | |
2523 | n = 0; | |
2524 | } | |
dfb31b25 | 2525 | } |
0ea055b3 CB |
2526 | if (n == 1) { |
2527 | v += inc ? 1 : -1; | |
2528 | f1 = fopen(path, "w"); | |
2529 | if (!f1) | |
2530 | goto out; | |
2531 | if (fprintf(f1, "%d\n", v) < 0) { | |
2532 | ERROR("Error writing new snapshots value"); | |
2533 | fclose(f1); | |
2534 | goto out; | |
2535 | } | |
2536 | ret = fclose(f1); | |
2537 | if (ret != 0) { | |
2538 | SYSERROR("Error writing to or closing snapshots file"); | |
2539 | goto out; | |
2540 | } | |
2541 | } else { | |
2542 | /* Here we know that we have or can use an lxc-snapshot file | |
2543 | * using the new format. */ | |
2544 | if (inc) { | |
2545 | f1 = fopen(path, "a"); | |
2546 | if (!f1) | |
2547 | goto out; | |
2548 | ||
2549 | if (fprintf(f1, "%s", newpath) < 0) { | |
2550 | ERROR("Error writing new snapshots entry"); | |
2551 | ret = fclose(f1); | |
2552 | if (ret != 0) | |
2553 | SYSERROR("Error writing to or closing snapshots file"); | |
2554 | goto out; | |
2555 | } | |
2556 | ||
2557 | ret = fclose(f1); | |
2558 | if (ret != 0) { | |
2559 | SYSERROR("Error writing to or closing snapshots file"); | |
2560 | goto out; | |
2561 | } | |
2562 | } else if (!inc) { | |
d028235d SG |
2563 | if ((fd = open(path, O_RDWR | O_CLOEXEC)) < 0) |
2564 | goto out; | |
2565 | ||
2566 | if (fstat(fd, &fbuf) < 0) { | |
2567 | close(fd); | |
2568 | goto out; | |
2569 | } | |
2570 | ||
2571 | if (fbuf.st_size != 0) { | |
d028235d | 2572 | |
25086a5f | 2573 | buf = lxc_strmmap(NULL, fbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
d028235d SG |
2574 | if (buf == MAP_FAILED) { |
2575 | SYSERROR("Failed to create mapping %s", path); | |
2576 | close(fd); | |
2577 | goto out; | |
2578 | } | |
2579 | ||
2580 | len = strlen(newpath); | |
2581 | while ((del = strstr((char *)buf, newpath))) { | |
2582 | memmove(del, del + len, strlen(del) - len + 1); | |
2583 | bytes += len; | |
2584 | } | |
2585 | ||
25086a5f | 2586 | lxc_strmunmap(buf, fbuf.st_size); |
d028235d SG |
2587 | if (ftruncate(fd, fbuf.st_size - bytes) < 0) { |
2588 | SYSERROR("Failed to truncate file %s", path); | |
2589 | close(fd); | |
2590 | goto out; | |
2591 | } | |
2592 | } | |
2593 | close(fd); | |
2594 | } | |
0ea055b3 CB |
2595 | |
2596 | /* If the lxc-snapshot file is empty, remove it. */ | |
2597 | if (stat(path, &fbuf) < 0) | |
2598 | goto out; | |
d028235d SG |
2599 | if (!fbuf.st_size) { |
2600 | remove(path); | |
0ea055b3 | 2601 | } |
dfb31b25 SH |
2602 | } |
2603 | ||
2604 | bret = true; | |
2605 | ||
2606 | out: | |
0ea055b3 | 2607 | container_disk_unlock(c0); |
dfb31b25 SH |
2608 | return bret; |
2609 | } | |
2610 | ||
2611 | static void strip_newline(char *p) | |
2612 | { | |
2613 | size_t len = strlen(p); | |
2614 | if (len < 1) | |
2615 | return; | |
2616 | if (p[len-1] == '\n') | |
2617 | p[len-1] = '\0'; | |
2618 | } | |
2619 | ||
d825fff3 | 2620 | void mod_all_rdeps(struct lxc_container *c, bool inc) |
dfb31b25 SH |
2621 | { |
2622 | struct lxc_container *p; | |
2623 | char *lxcpath = NULL, *lxcname = NULL, path[MAXPATHLEN]; | |
2624 | size_t pathlen = 0, namelen = 0; | |
2625 | FILE *f; | |
2626 | int ret; | |
2627 | ||
2628 | ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_rdepends", | |
2629 | c->config_path, c->name); | |
2630 | if (ret < 0 || ret >= MAXPATHLEN) { | |
2631 | ERROR("Path name too long"); | |
2632 | return; | |
2633 | } | |
025ed0f3 | 2634 | f = fopen(path, "r"); |
025ed0f3 | 2635 | if (f == NULL) |
dfb31b25 SH |
2636 | return; |
2637 | while (getline(&lxcpath, &pathlen, f) != -1) { | |
2638 | if (getline(&lxcname, &namelen, f) == -1) { | |
959aee9c | 2639 | ERROR("badly formatted file %s", path); |
dfb31b25 SH |
2640 | goto out; |
2641 | } | |
2642 | strip_newline(lxcpath); | |
2643 | strip_newline(lxcname); | |
2644 | if ((p = lxc_container_new(lxcname, lxcpath)) == NULL) { | |
2645 | ERROR("Unable to find dependent container %s:%s", | |
2646 | lxcpath, lxcname); | |
2647 | continue; | |
2648 | } | |
0ea055b3 CB |
2649 | if (!mod_rdep(p, c, inc)) |
2650 | ERROR("Failed to update snapshots file for %s:%s", | |
dfb31b25 SH |
2651 | lxcpath, lxcname); |
2652 | lxc_container_put(p); | |
2653 | } | |
2654 | out: | |
f10fad2f ME |
2655 | free(lxcpath); |
2656 | free(lxcname); | |
dfb31b25 SH |
2657 | fclose(f); |
2658 | } | |
2659 | ||
18aa217b | 2660 | static bool has_fs_snapshots(struct lxc_container *c) |
dfb31b25 | 2661 | { |
0ea055b3 | 2662 | FILE *f; |
dfb31b25 SH |
2663 | char path[MAXPATHLEN]; |
2664 | int ret, v; | |
0ea055b3 | 2665 | struct stat fbuf; |
dfb31b25 SH |
2666 | bool bret = false; |
2667 | ||
2668 | ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_snapshots", c->config_path, | |
2669 | c->name); | |
2670 | if (ret < 0 || ret > MAXPATHLEN) | |
2671 | goto out; | |
0ea055b3 CB |
2672 | /* If the file doesn't exist there are no snapshots. */ |
2673 | if (stat(path, &fbuf) < 0) | |
dfb31b25 | 2674 | goto out; |
0ea055b3 CB |
2675 | v = fbuf.st_size; |
2676 | if (v != 0) { | |
2677 | f = fopen(path, "r"); | |
2678 | if (!f) | |
2679 | goto out; | |
2680 | ret = fscanf(f, "%d", &v); | |
2681 | fclose(f); | |
1a0e70ac | 2682 | /* TODO: Figure out what to do with the return value of fscanf. */ |
0ea055b3 CB |
2683 | if (ret != 1) |
2684 | INFO("Container uses new lxc-snapshots format %s", path); | |
2685 | } | |
dfb31b25 SH |
2686 | bret = v != 0; |
2687 | ||
2688 | out: | |
2689 | return bret; | |
2690 | } | |
2691 | ||
18aa217b SH |
2692 | static bool has_snapshots(struct lxc_container *c) |
2693 | { | |
2694 | char path[MAXPATHLEN]; | |
74f96976 | 2695 | struct dirent *direntp; |
18aa217b SH |
2696 | int count=0; |
2697 | DIR *dir; | |
2698 | ||
2699 | if (!get_snappath_dir(c, path)) | |
2700 | return false; | |
2701 | dir = opendir(path); | |
2702 | if (!dir) | |
2703 | return false; | |
74f96976 | 2704 | while ((direntp = readdir(dir))) { |
18aa217b SH |
2705 | if (!direntp) |
2706 | break; | |
2707 | ||
2708 | if (!strcmp(direntp->d_name, ".")) | |
2709 | continue; | |
2710 | ||
2711 | if (!strcmp(direntp->d_name, "..")) | |
2712 | continue; | |
2713 | count++; | |
2714 | break; | |
2715 | } | |
2716 | closedir(dir); | |
2717 | return count > 0; | |
2718 | } | |
2719 | ||
297c2d58 | 2720 | static bool do_destroy_container(struct lxc_conf *conf) { |
ee484f7f CB |
2721 | int ret; |
2722 | ||
e0010464 | 2723 | if (am_guest_unpriv()) { |
ee484f7f CB |
2724 | ret = userns_exec_full(conf, storage_destroy_wrapper, conf, |
2725 | "storage_destroy_wrapper"); | |
2726 | if (ret < 0) | |
d028235d | 2727 | return false; |
ee484f7f | 2728 | |
d028235d SG |
2729 | return true; |
2730 | } | |
ee484f7f | 2731 | |
10bc1861 | 2732 | return storage_destroy(conf); |
297c2d58 CB |
2733 | } |
2734 | ||
4355ab5f SH |
2735 | static int lxc_rmdir_onedev_wrapper(void *data) |
2736 | { | |
2737 | char *arg = (char *) data; | |
18aa217b | 2738 | return lxc_rmdir_onedev(arg, "snaps"); |
4355ab5f SH |
2739 | } |
2740 | ||
17a367d8 | 2741 | static int lxc_unlink_exec_wrapper(void *data) |
72d0e1cb | 2742 | { |
17a367d8 CB |
2743 | char *arg = data; |
2744 | return unlink(arg); | |
2745 | } | |
2746 | ||
2747 | static bool container_destroy(struct lxc_container *c, | |
2748 | struct lxc_storage *storage) | |
2749 | { | |
2750 | const char *p1; | |
2751 | size_t len; | |
2752 | struct lxc_conf *conf; | |
2753 | char *path = NULL; | |
c868b261 | 2754 | bool bret = false; |
297c2d58 | 2755 | int ret = 0; |
72d0e1cb | 2756 | |
858377e4 | 2757 | if (!c || !do_lxcapi_is_defined(c)) |
5a3d2e1e SG |
2758 | return false; |
2759 | ||
a70a69e8 | 2760 | conf = c->lxc_conf; |
3bc449ed | 2761 | if (container_disk_lock(c)) |
72d0e1cb | 2762 | return false; |
72d0e1cb | 2763 | |
39dc698c | 2764 | if (!is_stopped(c)) { |
1a0e70ac | 2765 | /* We should queue some sort of error - in c->error_string? */ |
60bf62d4 SH |
2766 | ERROR("container %s is not stopped", c->name); |
2767 | goto out; | |
72d0e1cb SG |
2768 | } |
2769 | ||
a70a69e8 | 2770 | if (conf && !lxc_list_empty(&conf->hooks[LXCHOOK_DESTROY])) { |
37cf711b | 2771 | /* Start of environment variable setup for hooks */ |
cb8ff4d0 | 2772 | if (setenv("LXC_NAME", c->name, 1)) |
17a367d8 CB |
2773 | SYSERROR("Failed to set environment variable for container name"); |
2774 | ||
2775 | if (conf->rcfile && setenv("LXC_CONFIG_FILE", conf->rcfile, 1)) | |
2776 | SYSERROR("Failed to set environment variable for config path"); | |
2777 | ||
2778 | if (conf->rootfs.mount && setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1)) | |
2779 | SYSERROR("Failed to set environment variable for rootfs mount"); | |
2780 | ||
2781 | if (conf->rootfs.path && setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1)) | |
2782 | SYSERROR("Failed to set environment variable for rootfs mount"); | |
2783 | ||
2784 | if (conf->console.path && setenv("LXC_CONSOLE", conf->console.path, 1)) | |
2785 | SYSERROR("Failed to set environment variable for console path"); | |
2786 | ||
2787 | if (conf->console.log_path && setenv("LXC_CONSOLE_LOGPATH", conf->console.log_path, 1)) | |
2788 | SYSERROR("Failed to set environment variable for console log"); | |
37cf711b SY |
2789 | /* End of environment variable setup for hooks */ |
2790 | ||
14a7b0f9 | 2791 | if (run_lxc_hooks(c->name, "destroy", conf, NULL)) { |
17a367d8 | 2792 | ERROR("Failed to execute clone hook for \"%s\"", c->name); |
37cf711b SY |
2793 | goto out; |
2794 | } | |
2795 | } | |
2796 | ||
2797 | if (current_config && conf == current_config) { | |
858377e4 | 2798 | current_config = NULL; |
37cf711b SY |
2799 | if (conf->logfd != -1) { |
2800 | close(conf->logfd); | |
2801 | conf->logfd = -1; | |
858377e4 SH |
2802 | } |
2803 | } | |
2804 | ||
d028235d SG |
2805 | if (conf && conf->rootfs.path && conf->rootfs.mount) { |
2806 | if (!do_destroy_container(conf)) { | |
2807 | ERROR("Error destroying rootfs for %s", c->name); | |
2808 | goto out; | |
2809 | } | |
2810 | INFO("Destroyed rootfs for %s", c->name); | |
2811 | } | |
60bf62d4 | 2812 | |
dfb31b25 SH |
2813 | mod_all_rdeps(c, false); |
2814 | ||
17a367d8 CB |
2815 | p1 = do_lxcapi_get_config_path(c); |
2816 | /* strlen(p1) | |
2817 | * + | |
2818 | * / | |
2819 | * + | |
2820 | * strlen(c->name) | |
2821 | * + | |
2822 | * / | |
2823 | * + | |
2824 | * strlen("config") = 6 | |
2825 | * + | |
2826 | * \0 | |
2827 | */ | |
2828 | len = strlen(p1) + 1 + strlen(c->name) + 1 + 6 + 1; | |
2829 | path = malloc(len); | |
2830 | if (!path) { | |
2831 | ERROR("Failed to allocate memory"); | |
2832 | goto out; | |
2833 | } | |
2834 | ||
2835 | /* For an overlay container the rootfs is considered immutable and | |
2836 | * cannot be removed when restoring from a snapshot. | |
2837 | */ | |
2838 | if (storage && (!strcmp(storage->type, "overlay") || | |
2839 | !strcmp(storage->type, "overlayfs")) && | |
2840 | (storage->flags & LXC_STORAGE_INTERNAL_OVERLAY_RESTORE)) { | |
2841 | ret = snprintf(path, len, "%s/%s/config", p1, c->name); | |
2842 | if (ret < 0 || (size_t)ret >= len) | |
2843 | goto out; | |
2844 | ||
e0010464 | 2845 | if (am_guest_unpriv()) |
17a367d8 CB |
2846 | ret = userns_exec_1(conf, lxc_unlink_exec_wrapper, path, |
2847 | "lxc_unlink_exec_wrapper"); | |
2848 | else | |
2849 | ret = unlink(path); | |
2850 | if (ret < 0) { | |
2851 | SYSERROR("Failed to destroy config file \"%s\" for \"%s\"", | |
2852 | path, c->name); | |
2853 | goto out; | |
2854 | } | |
2855 | INFO("Destroyed config file \"%s\" for \"%s\"", path, c->name); | |
2856 | ||
2857 | bret = true; | |
2858 | goto out; | |
2859 | } | |
2860 | ||
2861 | ret = snprintf(path, len, "%s/%s", p1, c->name); | |
2862 | if (ret < 0 || (size_t)ret >= len) | |
2863 | goto out; | |
e0010464 | 2864 | if (am_guest_unpriv()) |
ee484f7f CB |
2865 | ret = userns_exec_full(conf, lxc_rmdir_onedev_wrapper, path, |
2866 | "lxc_rmdir_onedev_wrapper"); | |
4355ab5f | 2867 | else |
18aa217b | 2868 | ret = lxc_rmdir_onedev(path, "snaps"); |
4355ab5f | 2869 | if (ret < 0) { |
17a367d8 CB |
2870 | ERROR("Failed to destroy directory \"%s\" for \"%s\"", path, |
2871 | c->name); | |
60bf62d4 SH |
2872 | goto out; |
2873 | } | |
17a367d8 | 2874 | INFO("Destroyed directory \"%s\" for \"%s\"", path, c->name); |
297c2d58 | 2875 | |
fef48dc9 | 2876 | bret = true; |
60bf62d4 SH |
2877 | |
2878 | out: | |
17a367d8 CB |
2879 | if (path) |
2880 | free(path); | |
3bc449ed | 2881 | container_disk_unlock(c); |
fef48dc9 | 2882 | return bret; |
72d0e1cb SG |
2883 | } |
2884 | ||
858377e4 | 2885 | static bool do_lxcapi_destroy(struct lxc_container *c) |
18aa217b SH |
2886 | { |
2887 | if (!c || !lxcapi_is_defined(c)) | |
2888 | return false; | |
2889 | if (has_snapshots(c)) { | |
2890 | ERROR("Container %s has snapshots; not removing", c->name); | |
2891 | return false; | |
2892 | } | |
2893 | ||
2894 | if (has_fs_snapshots(c)) { | |
2895 | ERROR("container %s has snapshots on its rootfs", c->name); | |
2896 | return false; | |
2897 | } | |
2898 | ||
17a367d8 | 2899 | return container_destroy(c, NULL); |
18aa217b SH |
2900 | } |
2901 | ||
858377e4 | 2902 | WRAP_API(bool, lxcapi_destroy) |
18aa217b | 2903 | |
858377e4 | 2904 | static bool do_lxcapi_destroy_with_snapshots(struct lxc_container *c) |
18aa217b SH |
2905 | { |
2906 | if (!c || !lxcapi_is_defined(c)) | |
2907 | return false; | |
2908 | if (!lxcapi_snapshot_destroy_all(c)) { | |
2909 | ERROR("Error deleting all snapshots"); | |
2910 | return false; | |
2911 | } | |
2912 | return lxcapi_destroy(c); | |
2913 | } | |
2914 | ||
858377e4 SH |
2915 | WRAP_API(bool, lxcapi_destroy_with_snapshots) |
2916 | ||
0d9cd9c3 CB |
2917 | int lxc_set_config_item_locked(struct lxc_conf *conf, const char *key, |
2918 | const char *v) | |
96532523 | 2919 | { |
0d9cd9c3 | 2920 | int ret; |
96532523 | 2921 | struct lxc_config_t *config; |
0d9cd9c3 CB |
2922 | bool bret = true; |
2923 | ||
2924 | config = lxc_get_config(key); | |
2925 | if (!config) | |
2926 | return -EINVAL; | |
2927 | ||
2928 | ret = config->set(key, v, conf, NULL); | |
2929 | if (ret < 0) | |
2930 | return -EINVAL; | |
2931 | ||
2932 | if (lxc_config_value_empty(v)) | |
2933 | do_clear_unexp_config_line(conf, key); | |
2934 | else | |
2935 | bret = do_append_unexp_config_line(conf, key, v); | |
2936 | if (!bret) | |
2937 | return -ENOMEM; | |
2938 | ||
2939 | return 0; | |
2940 | } | |
2941 | ||
2942 | static bool do_set_config_item_locked(struct lxc_container *c, const char *key, | |
2943 | const char *v) | |
2944 | { | |
2945 | int ret; | |
96532523 SH |
2946 | |
2947 | if (!c->lxc_conf) | |
2948 | c->lxc_conf = lxc_conf_init(); | |
4222a9f4 | 2949 | |
6b0d5538 | 2950 | if (!c->lxc_conf) |
96532523 | 2951 | return false; |
4222a9f4 | 2952 | |
0d9cd9c3 CB |
2953 | ret = lxc_set_config_item_locked(c->lxc_conf, key, v); |
2954 | if (ret < 0) | |
f979ac15 | 2955 | return false; |
4222a9f4 | 2956 | |
0d9cd9c3 | 2957 | return true; |
96532523 SH |
2958 | } |
2959 | ||
858377e4 | 2960 | static bool do_lxcapi_set_config_item(struct lxc_container *c, const char *key, const char *v) |
72d0e1cb | 2961 | { |
72d0e1cb | 2962 | bool b = false; |
72d0e1cb SG |
2963 | |
2964 | if (!c) | |
2965 | return false; | |
2966 | ||
5cee8c50 | 2967 | if (container_mem_lock(c)) |
72d0e1cb SG |
2968 | return false; |
2969 | ||
0d9cd9c3 | 2970 | b = do_set_config_item_locked(c, key, v); |
72d0e1cb | 2971 | |
5cee8c50 | 2972 | container_mem_unlock(c); |
72d0e1cb SG |
2973 | return b; |
2974 | } | |
2975 | ||
858377e4 SH |
2976 | WRAP_API_2(bool, lxcapi_set_config_item, const char *, const char *) |
2977 | ||
72d0e1cb SG |
2978 | static char *lxcapi_config_file_name(struct lxc_container *c) |
2979 | { | |
2980 | if (!c || !c->configfile) | |
2981 | return NULL; | |
2982 | return strdup(c->configfile); | |
2983 | } | |
2984 | ||
2a59a681 SH |
2985 | static const char *lxcapi_get_config_path(struct lxc_container *c) |
2986 | { | |
2987 | if (!c || !c->config_path) | |
2988 | return NULL; | |
2989 | return (const char *)(c->config_path); | |
2990 | } | |
2991 | ||
afeecbba SH |
2992 | /* |
2993 | * not for export | |
2994 | * Just recalculate the c->configfile based on the | |
2995 | * c->config_path, which must be set. | |
2996 | * The lxc_container must be locked or not yet public. | |
2997 | */ | |
2998 | static bool set_config_filename(struct lxc_container *c) | |
2999 | { | |
3000 | char *newpath; | |
3001 | int len, ret; | |
3002 | ||
3003 | if (!c->config_path) | |
3004 | return false; | |
3005 | ||
3006 | /* $lxc_path + "/" + c->name + "/" + "config" + '\0' */ | |
3007 | len = strlen(c->config_path) + strlen(c->name) + strlen("config") + 3; | |
3008 | newpath = malloc(len); | |
3009 | if (!newpath) | |
3010 | return false; | |
3011 | ||
3012 | ret = snprintf(newpath, len, "%s/%s/config", c->config_path, c->name); | |
3013 | if (ret < 0 || ret >= len) { | |
3014 | fprintf(stderr, "Error printing out config file name\n"); | |
3015 | free(newpath); | |
3016 | return false; | |
3017 | } | |
3018 | ||
f10fad2f | 3019 | free(c->configfile); |
afeecbba SH |
3020 | c->configfile = newpath; |
3021 | ||
3022 | return true; | |
3023 | } | |
3024 | ||
858377e4 | 3025 | static bool do_lxcapi_set_config_path(struct lxc_container *c, const char *path) |
2a59a681 SH |
3026 | { |
3027 | char *p; | |
3028 | bool b = false; | |
afeecbba | 3029 | char *oldpath = NULL; |
2a59a681 SH |
3030 | |
3031 | if (!c) | |
3032 | return b; | |
3033 | ||
5cee8c50 | 3034 | if (container_mem_lock(c)) |
2a59a681 SH |
3035 | return b; |
3036 | ||
3037 | p = strdup(path); | |
afeecbba SH |
3038 | if (!p) { |
3039 | ERROR("Out of memory setting new lxc path"); | |
2a59a681 | 3040 | goto err; |
afeecbba SH |
3041 | } |
3042 | ||
2a59a681 SH |
3043 | b = true; |
3044 | if (c->config_path) | |
afeecbba | 3045 | oldpath = c->config_path; |
2a59a681 | 3046 | c->config_path = p; |
afeecbba SH |
3047 | |
3048 | /* Since we've changed the config path, we have to change the | |
3049 | * config file name too */ | |
3050 | if (!set_config_filename(c)) { | |
3051 | ERROR("Out of memory setting new config filename"); | |
3052 | b = false; | |
3053 | free(c->config_path); | |
3054 | c->config_path = oldpath; | |
3055 | oldpath = NULL; | |
3056 | } | |
2a59a681 | 3057 | err: |
f10fad2f | 3058 | free(oldpath); |
5cee8c50 | 3059 | container_mem_unlock(c); |
2a59a681 SH |
3060 | return b; |
3061 | } | |
3062 | ||
858377e4 | 3063 | WRAP_API_1(bool, lxcapi_set_config_path, const char *) |
2a59a681 | 3064 | |
858377e4 | 3065 | static bool do_lxcapi_set_cgroup_item(struct lxc_container *c, const char *subsys, const char *value) |
794dd120 SH |
3066 | { |
3067 | int ret; | |
794dd120 SH |
3068 | |
3069 | if (!c) | |
3070 | return false; | |
3071 | ||
3bc449ed | 3072 | if (is_stopped(c)) |
794dd120 SH |
3073 | return false; |
3074 | ||
3bc449ed SH |
3075 | if (container_disk_lock(c)) |
3076 | return false; | |
794dd120 | 3077 | |
33ad9f1a | 3078 | ret = lxc_cgroup_set(subsys, value, c->name, c->config_path); |
3bc449ed SH |
3079 | |
3080 | container_disk_unlock(c); | |
3081 | return ret == 0; | |
794dd120 SH |
3082 | } |
3083 | ||
858377e4 SH |
3084 | WRAP_API_2(bool, lxcapi_set_cgroup_item, const char *, const char *) |
3085 | ||
3086 | static int do_lxcapi_get_cgroup_item(struct lxc_container *c, const char *subsys, char *retv, int inlen) | |
794dd120 | 3087 | { |
3bc449ed | 3088 | int ret; |
794dd120 | 3089 | |
6502006a | 3090 | if (!c) |
794dd120 SH |
3091 | return -1; |
3092 | ||
3bc449ed | 3093 | if (is_stopped(c)) |
794dd120 SH |
3094 | return -1; |
3095 | ||
3bc449ed SH |
3096 | if (container_disk_lock(c)) |
3097 | return -1; | |
794dd120 | 3098 | |
33ad9f1a | 3099 | ret = lxc_cgroup_get(subsys, retv, inlen, c->name, c->config_path); |
794dd120 | 3100 | |
3bc449ed | 3101 | container_disk_unlock(c); |
794dd120 SH |
3102 | return ret; |
3103 | } | |
3104 | ||
858377e4 SH |
3105 | WRAP_API_3(int, lxcapi_get_cgroup_item, const char *, char *, int) |
3106 | ||
593e8478 | 3107 | const char *lxc_get_global_config_item(const char *key) |
83c98d82 | 3108 | { |
593e8478 | 3109 | return lxc_global_config_value(key); |
a8428dfa SH |
3110 | } |
3111 | ||
b6b918a1 SG |
3112 | const char *lxc_get_version(void) |
3113 | { | |
95ee490b | 3114 | return LXC_VERSION; |
b6b918a1 SG |
3115 | } |
3116 | ||
f0ca2726 | 3117 | static int copy_file(const char *old, const char *new) |
9be53773 SH |
3118 | { |
3119 | int in, out; | |
3120 | ssize_t len, ret; | |
3121 | char buf[8096]; | |
3122 | struct stat sbuf; | |
3123 | ||
3124 | if (file_exists(new)) { | |
3125 | ERROR("copy destination %s exists", new); | |
3126 | return -1; | |
3127 | } | |
3128 | ret = stat(old, &sbuf); | |
3129 | if (ret < 0) { | |
dfb31b25 | 3130 | INFO("Error stat'ing %s", old); |
9be53773 SH |
3131 | return -1; |
3132 | } | |
3133 | ||
3134 | in = open(old, O_RDONLY); | |
3135 | if (in < 0) { | |
dfb31b25 | 3136 | SYSERROR("Error opening original file %s", old); |
9be53773 SH |
3137 | return -1; |
3138 | } | |
3139 | out = open(new, O_CREAT | O_EXCL | O_WRONLY, 0644); | |
3140 | if (out < 0) { | |
dfb31b25 | 3141 | SYSERROR("Error opening new file %s", new); |
9be53773 SH |
3142 | close(in); |
3143 | return -1; | |
3144 | } | |
3145 | ||
3146 | while (1) { | |
3147 | len = read(in, buf, 8096); | |
3148 | if (len < 0) { | |
dfb31b25 | 3149 | SYSERROR("Error reading old file %s", old); |
9be53773 SH |
3150 | goto err; |
3151 | } | |
3152 | if (len == 0) | |
3153 | break; | |
3154 | ret = write(out, buf, len); | |
1a0e70ac | 3155 | if (ret < len) { /* should we retry? */ |
dfb31b25 | 3156 | SYSERROR("Error: write to new file %s was interrupted", new); |
9be53773 SH |
3157 | goto err; |
3158 | } | |
3159 | } | |
3160 | close(in); | |
3161 | close(out); | |
3162 | ||
1a0e70ac | 3163 | /* We set mode, but not owner/group. */ |
9be53773 SH |
3164 | ret = chmod(new, sbuf.st_mode); |
3165 | if (ret) { | |
dfb31b25 | 3166 | SYSERROR("Error setting mode on %s", new); |
9be53773 SH |
3167 | return -1; |
3168 | } | |
3169 | ||
3170 | return 0; | |
3171 | ||
3172 | err: | |
3173 | close(in); | |
3174 | close(out); | |
3175 | return -1; | |
3176 | } | |
3177 | ||
9be53773 SH |
3178 | static int copyhooks(struct lxc_container *oldc, struct lxc_container *c) |
3179 | { | |
619256b5 | 3180 | int i, len, ret; |
9be53773 | 3181 | struct lxc_list *it; |
619256b5 ÇO |
3182 | char *cpath; |
3183 | ||
3184 | len = strlen(oldc->config_path) + strlen(oldc->name) + 3; | |
3185 | cpath = alloca(len); | |
3186 | ret = snprintf(cpath, len, "%s/%s/", oldc->config_path, oldc->name); | |
3187 | if (ret < 0 || ret >= len) | |
3188 | return -1; | |
9be53773 SH |
3189 | |
3190 | for (i=0; i<NUM_LXC_HOOKS; i++) { | |
3191 | lxc_list_for_each(it, &c->lxc_conf->hooks[i]) { | |
3192 | char *hookname = it->elem; | |
c32981c3 | 3193 | char *fname = strrchr(hookname, '/'); |
9be53773 | 3194 | char tmppath[MAXPATHLEN]; |
1a0e70ac | 3195 | if (!fname) /* relative path - we don't support, but maybe we should */ |
9be53773 | 3196 | return 0; |
619256b5 | 3197 | if (strncmp(hookname, cpath, len - 1) != 0) { |
1a0e70ac | 3198 | /* this hook is public - ignore */ |
619256b5 ÇO |
3199 | continue; |
3200 | } | |
1a0e70ac | 3201 | /* copy the script, and change the entry in confile */ |
9be53773 SH |
3202 | ret = snprintf(tmppath, MAXPATHLEN, "%s/%s/%s", |
3203 | c->config_path, c->name, fname+1); | |
3204 | if (ret < 0 || ret >= MAXPATHLEN) | |
3205 | return -1; | |
3206 | ret = copy_file(it->elem, tmppath); | |
3207 | if (ret < 0) | |
3208 | return -1; | |
3209 | free(it->elem); | |
3210 | it->elem = strdup(tmppath); | |
3211 | if (!it->elem) { | |
3212 | ERROR("out of memory copying hook path"); | |
3213 | return -1; | |
3214 | } | |
9be53773 SH |
3215 | } |
3216 | } | |
3217 | ||
67702c21 SH |
3218 | if (!clone_update_unexp_hooks(c->lxc_conf, oldc->config_path, |
3219 | c->config_path, oldc->name, c->name)) { | |
6b0d5538 SH |
3220 | ERROR("Error saving new hooks in clone"); |
3221 | return -1; | |
3222 | } | |
858377e4 | 3223 | do_lxcapi_save_config(c, NULL); |
9be53773 SH |
3224 | return 0; |
3225 | } | |
3226 | ||
9be53773 SH |
3227 | |
3228 | static int copy_fstab(struct lxc_container *oldc, struct lxc_container *c) | |
3229 | { | |
3230 | char newpath[MAXPATHLEN]; | |
3231 | char *oldpath = oldc->lxc_conf->fstab; | |
3232 | int ret; | |
3233 | ||
3234 | if (!oldpath) | |
3235 | return 0; | |
3236 | ||
47148e96 CB |
3237 | clear_unexp_config_line(c->lxc_conf, "lxc.mount.fstab", false); |
3238 | ||
c32981c3 | 3239 | char *p = strrchr(oldpath, '/'); |
9be53773 SH |
3240 | if (!p) |
3241 | return -1; | |
3242 | ret = snprintf(newpath, MAXPATHLEN, "%s/%s%s", | |
3243 | c->config_path, c->name, p); | |
3244 | if (ret < 0 || ret >= MAXPATHLEN) { | |
3245 | ERROR("error printing new path for %s", oldpath); | |
3246 | return -1; | |
3247 | } | |
3248 | if (file_exists(newpath)) { | |
3249 | ERROR("error: fstab file %s exists", newpath); | |
3250 | return -1; | |
3251 | } | |
3252 | ||
3253 | if (copy_file(oldpath, newpath) < 0) { | |
3254 | ERROR("error: copying %s to %s", oldpath, newpath); | |
3255 | return -1; | |
3256 | } | |
3257 | free(c->lxc_conf->fstab); | |
3258 | c->lxc_conf->fstab = strdup(newpath); | |
3259 | if (!c->lxc_conf->fstab) { | |
3260 | ERROR("error: allocating pathname"); | |
3261 | return -1; | |
3262 | } | |
47148e96 | 3263 | if (!do_append_unexp_config_line(c->lxc_conf, "lxc.mount.fstab", newpath)) { |
6b0d5538 SH |
3264 | ERROR("error saving new lxctab"); |
3265 | return -1; | |
3266 | } | |
9be53773 SH |
3267 | |
3268 | return 0; | |
3269 | } | |
3270 | ||
dfb31b25 SH |
3271 | static void copy_rdepends(struct lxc_container *c, struct lxc_container *c0) |
3272 | { | |
3273 | char path0[MAXPATHLEN], path1[MAXPATHLEN]; | |
3274 | int ret; | |
3275 | ||
3276 | ret = snprintf(path0, MAXPATHLEN, "%s/%s/lxc_rdepends", c0->config_path, | |
3277 | c0->name); | |
3278 | if (ret < 0 || ret >= MAXPATHLEN) { | |
3279 | WARN("Error copying reverse dependencies"); | |
3280 | return; | |
3281 | } | |
3282 | ret = snprintf(path1, MAXPATHLEN, "%s/%s/lxc_rdepends", c->config_path, | |
3283 | c->name); | |
3284 | if (ret < 0 || ret >= MAXPATHLEN) { | |
3285 | WARN("Error copying reverse dependencies"); | |
3286 | return; | |
3287 | } | |
3288 | if (copy_file(path0, path1) < 0) { | |
3289 | INFO("Error copying reverse dependencies"); | |
3290 | return; | |
3291 | } | |
3292 | } | |
3293 | ||
3294 | static bool add_rdepends(struct lxc_container *c, struct lxc_container *c0) | |
3295 | { | |
3296 | int ret; | |
3297 | char path[MAXPATHLEN]; | |
3298 | FILE *f; | |
3299 | bool bret; | |
3300 | ||
3301 | ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_rdepends", c->config_path, | |
3302 | c->name); | |
3303 | if (ret < 0 || ret >= MAXPATHLEN) | |
3304 | return false; | |
3305 | f = fopen(path, "a"); | |
3306 | if (!f) | |
3307 | return false; | |
3308 | bret = true; | |
1a0e70ac | 3309 | /* If anything goes wrong, just return an error. */ |
dfb31b25 SH |
3310 | if (fprintf(f, "%s\n%s\n", c0->config_path, c0->name) < 0) |
3311 | bret = false; | |
3312 | if (fclose(f) != 0) | |
3313 | bret = false; | |
3314 | return bret; | |
3315 | } | |
3316 | ||
15a90a10 SH |
3317 | /* |
3318 | * If the fs natively supports snapshot clones with no penalty, | |
3319 | * then default to those even if not requested. | |
3320 | * Currently we only do this for btrfs. | |
3321 | */ | |
3322 | bool should_default_to_snapshot(struct lxc_container *c0, | |
3323 | struct lxc_container *c1) | |
3324 | { | |
3325 | size_t l0 = strlen(c0->config_path) + strlen(c0->name) + 2; | |
3326 | size_t l1 = strlen(c1->config_path) + strlen(c1->name) + 2; | |
3327 | char *p0 = alloca(l0 + 1); | |
3328 | char *p1 = alloca(l1 + 1); | |
6e0fa6a0 | 3329 | char *rootfs = c0->lxc_conf->rootfs.path; |
15a90a10 SH |
3330 | |
3331 | snprintf(p0, l0, "%s/%s", c0->config_path, c0->name); | |
3332 | snprintf(p1, l1, "%s/%s", c1->config_path, c1->name); | |
9a09badc CB |
3333 | |
3334 | if (!is_btrfs_fs(p0) || !is_btrfs_fs(p1)) | |
3335 | return false; | |
3336 | ||
6e0fa6a0 CB |
3337 | if (is_btrfs_subvol(rootfs) <= 0) |
3338 | return false; | |
3339 | ||
15a90a10 SH |
3340 | return btrfs_same_fs(p0, p1) == 0; |
3341 | } | |
3342 | ||
9be53773 | 3343 | static int copy_storage(struct lxc_container *c0, struct lxc_container *c, |
763429b6 CB |
3344 | const char *newtype, int flags, const char *bdevdata, |
3345 | uint64_t newsize) | |
9be53773 | 3346 | { |
10bc1861 | 3347 | struct lxc_storage *bdev; |
07db51a2 | 3348 | bool need_rdep; |
9be53773 | 3349 | |
15a90a10 SH |
3350 | if (should_default_to_snapshot(c0, c)) |
3351 | flags |= LXC_CLONE_SNAPSHOT; | |
3352 | ||
10bc1861 CB |
3353 | bdev = storage_copy(c0, c->name, c->config_path, newtype, flags, |
3354 | bdevdata, newsize, &need_rdep); | |
9be53773 | 3355 | if (!bdev) { |
763429b6 | 3356 | ERROR("Error copying storage."); |
9be53773 SH |
3357 | return -1; |
3358 | } | |
763429b6 CB |
3359 | |
3360 | /* Set new rootfs. */ | |
9be53773 SH |
3361 | free(c->lxc_conf->rootfs.path); |
3362 | c->lxc_conf->rootfs.path = strdup(bdev->src); | |
10bc1861 | 3363 | storage_put(bdev); |
763429b6 | 3364 | |
dfb31b25 | 3365 | if (!c->lxc_conf->rootfs.path) { |
763429b6 CB |
3366 | ERROR("Out of memory while setting storage path."); |
3367 | return -1; | |
3368 | } | |
763429b6 | 3369 | |
7a96a068 CB |
3370 | /* Append a new lxc.rootfs.path entry to the unexpanded config. */ |
3371 | clear_unexp_config_line(c->lxc_conf, "lxc.rootfs.path", false); | |
3372 | if (!do_append_unexp_config_line(c->lxc_conf, "lxc.rootfs.path", | |
763429b6 CB |
3373 | c->lxc_conf->rootfs.path)) { |
3374 | ERROR("Error saving new rootfs to cloned config."); | |
d0218321 SH |
3375 | return -1; |
3376 | } | |
763429b6 | 3377 | |
eee59f94 SH |
3378 | if (flags & LXC_CLONE_SNAPSHOT) |
3379 | copy_rdepends(c, c0); | |
dfb31b25 SH |
3380 | if (need_rdep) { |
3381 | if (!add_rdepends(c, c0)) | |
3382 | WARN("Error adding reverse dependency from %s to %s", | |
763429b6 | 3383 | c->name, c0->name); |
dfb31b25 SH |
3384 | } |
3385 | ||
3386 | mod_all_rdeps(c, true); | |
3387 | ||
9be53773 SH |
3388 | return 0; |
3389 | } | |
3390 | ||
1354955b SH |
3391 | struct clone_update_data { |
3392 | struct lxc_container *c0; | |
3393 | struct lxc_container *c1; | |
3394 | int flags; | |
3395 | char **hookargs; | |
3396 | }; | |
3397 | ||
3398 | static int clone_update_rootfs(struct clone_update_data *data) | |
9be53773 | 3399 | { |
1354955b SH |
3400 | struct lxc_container *c0 = data->c0; |
3401 | struct lxc_container *c = data->c1; | |
3402 | int flags = data->flags; | |
3403 | char **hookargs = data->hookargs; | |
9be53773 SH |
3404 | int ret = -1; |
3405 | char path[MAXPATHLEN]; | |
10bc1861 | 3406 | struct lxc_storage *bdev; |
9be53773 | 3407 | FILE *fout; |
148e91f5 | 3408 | struct lxc_conf *conf = c->lxc_conf; |
9be53773 SH |
3409 | |
3410 | /* update hostname in rootfs */ | |
3411 | /* we're going to mount, so run in a clean namespace to simplify cleanup */ | |
3412 | ||
1354955b SH |
3413 | if (setgid(0) < 0) { |
3414 | ERROR("Failed to setgid to 0"); | |
3415 | return -1; | |
3416 | } | |
3417 | if (setuid(0) < 0) { | |
3418 | ERROR("Failed to setuid to 0"); | |
9be53773 | 3419 | return -1; |
1354955b | 3420 | } |
c476bdce SH |
3421 | if (setgroups(0, NULL) < 0) |
3422 | WARN("Failed to clear groups"); | |
9be53773 | 3423 | |
1354955b SH |
3424 | if (unshare(CLONE_NEWNS) < 0) |
3425 | return -1; | |
8a388ed4 | 3426 | bdev = storage_init(c->lxc_conf); |
9be53773 | 3427 | if (!bdev) |
1354955b | 3428 | return -1; |
cf3ef16d SH |
3429 | if (strcmp(bdev->type, "dir") != 0) { |
3430 | if (unshare(CLONE_NEWNS) < 0) { | |
3431 | ERROR("error unsharing mounts"); | |
10bc1861 | 3432 | storage_put(bdev); |
1354955b | 3433 | return -1; |
cf3ef16d | 3434 | } |
2c6f3fc9 SH |
3435 | if (detect_shared_rootfs()) { |
3436 | if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL)) { | |
3437 | SYSERROR("Failed to make / rslave"); | |
3438 | ERROR("Continuing..."); | |
3439 | } | |
3440 | } | |
e7de366c | 3441 | if (bdev->ops->mount(bdev) < 0) { |
10bc1861 | 3442 | storage_put(bdev); |
1354955b | 3443 | return -1; |
e7de366c | 3444 | } |
1a0e70ac | 3445 | } else { /* TODO come up with a better way */ |
f10fad2f | 3446 | free(bdev->dest); |
cf3ef16d SH |
3447 | bdev->dest = strdup(bdev->src); |
3448 | } | |
148e91f5 SH |
3449 | |
3450 | if (!lxc_list_empty(&conf->hooks[LXCHOOK_CLONE])) { | |
3451 | /* Start of environment variable setup for hooks */ | |
ab7efcf5 | 3452 | if (c0->name && setenv("LXC_SRC_NAME", c0->name, 1)) { |
1143ed39 DE |
3453 | SYSERROR("failed to set environment variable for source container name"); |
3454 | } | |
cb8ff4d0 | 3455 | if (setenv("LXC_NAME", c->name, 1)) { |
148e91f5 SH |
3456 | SYSERROR("failed to set environment variable for container name"); |
3457 | } | |
ab7efcf5 | 3458 | if (conf->rcfile && setenv("LXC_CONFIG_FILE", conf->rcfile, 1)) { |
148e91f5 SH |
3459 | SYSERROR("failed to set environment variable for config path"); |
3460 | } | |
ab7efcf5 | 3461 | if (bdev->dest && setenv("LXC_ROOTFS_MOUNT", bdev->dest, 1)) { |
148e91f5 SH |
3462 | SYSERROR("failed to set environment variable for rootfs mount"); |
3463 | } | |
ab7efcf5 | 3464 | if (conf->rootfs.path && setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1)) { |
148e91f5 SH |
3465 | SYSERROR("failed to set environment variable for rootfs mount"); |
3466 | } | |
3467 | ||
14a7b0f9 | 3468 | if (run_lxc_hooks(c->name, "clone", conf, hookargs)) { |
148e91f5 | 3469 | ERROR("Error executing clone hook for %s", c->name); |
10bc1861 | 3470 | storage_put(bdev); |
1354955b | 3471 | return -1; |
148e91f5 SH |
3472 | } |
3473 | } | |
3474 | ||
3475 | if (!(flags & LXC_CLONE_KEEPNAME)) { | |
3476 | ret = snprintf(path, MAXPATHLEN, "%s/etc/hostname", bdev->dest); | |
10bc1861 | 3477 | storage_put(bdev); |
e7de366c | 3478 | |
148e91f5 | 3479 | if (ret < 0 || ret >= MAXPATHLEN) |
1354955b | 3480 | return -1; |
8058be39 | 3481 | if (!file_exists(path)) |
1354955b | 3482 | return 0; |
148e91f5 | 3483 | if (!(fout = fopen(path, "w"))) { |
959aee9c | 3484 | SYSERROR("unable to open %s: ignoring", path); |
1354955b | 3485 | return 0; |
148e91f5 | 3486 | } |
a684f0b7 ÇO |
3487 | if (fprintf(fout, "%s", c->name) < 0) { |
3488 | fclose(fout); | |
1354955b | 3489 | return -1; |
6849cb5b | 3490 | } |
148e91f5 | 3491 | if (fclose(fout) < 0) |
1354955b | 3492 | return -1; |
10bc1861 CB |
3493 | } else { |
3494 | storage_put(bdev); | |
9be53773 | 3495 | } |
e7de366c | 3496 | |
1354955b SH |
3497 | return 0; |
3498 | } | |
3499 | ||
3500 | static int clone_update_rootfs_wrapper(void *data) | |
3501 | { | |
3502 | struct clone_update_data *arg = (struct clone_update_data *) data; | |
3503 | return clone_update_rootfs(arg); | |
9be53773 SH |
3504 | } |
3505 | ||
3506 | /* | |
3507 | * We want to support: | |
3508 | sudo lxc-clone -o o1 -n n1 -s -L|-fssize fssize -v|--vgname vgname \ | |
3509 | -p|--lvprefix lvprefix -t|--fstype fstype -B backingstore | |
3510 | ||
ba115175 CB |
3511 | -s [ implies overlay] |
3512 | -s -B overlay | |
9be53773 SH |
3513 | -s -B aufs |
3514 | ||
3515 | only rootfs gets converted (copied/snapshotted) on clone. | |
3516 | */ | |
3517 | ||
d5752559 | 3518 | static int create_file_dirname(char *path, struct lxc_conf *conf) |
9be53773 | 3519 | { |
c32981c3 | 3520 | char *p = strrchr(path, '/'); |
d5752559 | 3521 | int ret = -1; |
9be53773 SH |
3522 | |
3523 | if (!p) | |
3524 | return -1; | |
3525 | *p = '\0'; | |
d028235d | 3526 | ret = do_create_container_dir(path, conf); |
9be53773 SH |
3527 | *p = '/'; |
3528 | return ret; | |
3529 | } | |
3530 | ||
858377e4 | 3531 | static struct lxc_container *do_lxcapi_clone(struct lxc_container *c, const char *newname, |
9be53773 | 3532 | const char *lxcpath, int flags, |
d659597e | 3533 | const char *bdevtype, const char *bdevdata, uint64_t newsize, |
148e91f5 | 3534 | char **hookargs) |
9be53773 | 3535 | { |
9be53773 | 3536 | char newpath[MAXPATHLEN]; |
0e1a60b0 | 3537 | int fd, ret; |
1354955b | 3538 | struct clone_update_data data; |
3b392519 | 3539 | size_t saved_unexp_len; |
1354955b | 3540 | pid_t pid; |
17a367d8 CB |
3541 | int storage_copied = 0; |
3542 | char *origroot = NULL, *saved_unexp_conf = NULL; | |
3543 | struct lxc_container *c2 = NULL; | |
9be53773 | 3544 | |
858377e4 | 3545 | if (!c || !do_lxcapi_is_defined(c)) |
9be53773 SH |
3546 | return NULL; |
3547 | ||
5cee8c50 | 3548 | if (container_mem_lock(c)) |
9be53773 SH |
3549 | return NULL; |
3550 | ||
39dc698c | 3551 | if (!is_stopped(c)) { |
9be53773 SH |
3552 | ERROR("error: Original container (%s) is running", c->name); |
3553 | goto out; | |
3554 | } | |
3555 | ||
1a0e70ac | 3556 | /* Make sure the container doesn't yet exist. */ |
05d53f4c SH |
3557 | if (!newname) |
3558 | newname = c->name; | |
3559 | if (!lxcpath) | |
858377e4 | 3560 | lxcpath = do_lxcapi_get_config_path(c); |
05d53f4c | 3561 | ret = snprintf(newpath, MAXPATHLEN, "%s/%s/config", lxcpath, newname); |
6849cb5b | 3562 | if (ret < 0 || ret >= MAXPATHLEN) { |
9be53773 SH |
3563 | SYSERROR("clone: failed making config pathname"); |
3564 | goto out; | |
3565 | } | |
17a367d8 | 3566 | |
9be53773 SH |
3567 | if (file_exists(newpath)) { |
3568 | ERROR("error: clone: %s exists", newpath); | |
3569 | goto out; | |
3570 | } | |
3571 | ||
d5752559 | 3572 | ret = create_file_dirname(newpath, c->lxc_conf); |
96532523 | 3573 | if (ret < 0 && errno != EEXIST) { |
9be53773 SH |
3574 | ERROR("Error creating container dir for %s", newpath); |
3575 | goto out; | |
3576 | } | |
3577 | ||
1a0e70ac | 3578 | /* Copy the configuration. Tweak it as needed. */ |
8d2efe40 SH |
3579 | if (c->lxc_conf->rootfs.path) { |
3580 | origroot = c->lxc_conf->rootfs.path; | |
3581 | c->lxc_conf->rootfs.path = NULL; | |
3582 | } | |
0e1a60b0 CB |
3583 | |
3584 | fd = open(newpath, O_WRONLY | O_CREAT | O_CLOEXEC, | |
3585 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); | |
3586 | if (fd < 0) { | |
3587 | SYSERROR("Failed to open \"%s\"", newpath); | |
9be53773 SH |
3588 | goto out; |
3589 | } | |
5eea90e8 SH |
3590 | |
3591 | saved_unexp_conf = c->lxc_conf->unexpanded_config; | |
3b392519 | 3592 | saved_unexp_len = c->lxc_conf->unexpanded_len; |
5eea90e8 SH |
3593 | c->lxc_conf->unexpanded_config = strdup(saved_unexp_conf); |
3594 | if (!c->lxc_conf->unexpanded_config) { | |
0e1a60b0 | 3595 | close(fd); |
5eea90e8 SH |
3596 | goto out; |
3597 | } | |
7a96a068 | 3598 | |
7a96a068 | 3599 | clear_unexp_config_line(c->lxc_conf, "lxc.rootfs.path", false); |
0e1a60b0 CB |
3600 | write_config(fd, c->lxc_conf); |
3601 | close(fd); | |
8d2efe40 | 3602 | c->lxc_conf->rootfs.path = origroot; |
5eea90e8 SH |
3603 | free(c->lxc_conf->unexpanded_config); |
3604 | c->lxc_conf->unexpanded_config = saved_unexp_conf; | |
3605 | saved_unexp_conf = NULL; | |
3b392519 | 3606 | c->lxc_conf->unexpanded_len = saved_unexp_len; |
9be53773 | 3607 | |
90b366fc CB |
3608 | ret = snprintf(newpath, MAXPATHLEN, "%s/%s/rootfs", lxcpath, newname); |
3609 | if (ret < 0 || ret >= MAXPATHLEN) { | |
3610 | SYSERROR("clone: failed making rootfs pathname"); | |
3611 | goto out; | |
3612 | } | |
17a367d8 CB |
3613 | |
3614 | ret = mkdir(newpath, 0755); | |
3615 | if (ret < 0) { | |
3616 | /* For an overlay container the rootfs is considered immutable | |
3617 | * and will not have been removed when restoring from a | |
3618 | * snapshot. | |
3619 | */ | |
3620 | if (errno != ENOENT && | |
3621 | !(flags & LXC_STORAGE_INTERNAL_OVERLAY_RESTORE)) { | |
3622 | SYSERROR("Failed to create directory \"%s\"", newpath); | |
3623 | goto out; | |
3624 | } | |
9be53773 SH |
3625 | } |
3626 | ||
e0010464 | 3627 | if (am_guest_unpriv()) { |
1354955b | 3628 | if (chown_mapped_root(newpath, c->lxc_conf) < 0) { |
959aee9c | 3629 | ERROR("Error chowning %s to container root", newpath); |
1354955b SH |
3630 | goto out; |
3631 | } | |
3632 | } | |
3633 | ||
05d53f4c | 3634 | c2 = lxc_container_new(newname, lxcpath); |
375c2258 | 3635 | if (!c2) { |
05d53f4c SH |
3636 | ERROR("clone: failed to create new container (%s %s)", newname, |
3637 | lxcpath); | |
9be53773 SH |
3638 | goto out; |
3639 | } | |
8d2efe40 | 3640 | |
1a0e70ac | 3641 | /* copy/snapshot rootfs's */ |
8d2efe40 SH |
3642 | ret = copy_storage(c, c2, bdevtype, flags, bdevdata, newsize); |
3643 | if (ret < 0) | |
3644 | goto out; | |
9be53773 | 3645 | |
6b0d5538 | 3646 | |
1a0e70ac | 3647 | /* update utsname */ |
3d7ad474 CB |
3648 | if (!(flags & LXC_CLONE_KEEPNAME)) { |
3649 | clear_unexp_config_line(c2->lxc_conf, "lxc.utsname", false); | |
b67771bc | 3650 | clear_unexp_config_line(c2->lxc_conf, "lxc.uts.name", false); |
3d7ad474 | 3651 | |
0d9cd9c3 | 3652 | if (!do_set_config_item_locked(c2, "lxc.uts.name", newname)) { |
3d7ad474 CB |
3653 | ERROR("Error setting new hostname"); |
3654 | goto out; | |
3655 | } | |
96532523 SH |
3656 | } |
3657 | ||
1a0e70ac | 3658 | /* copy hooks */ |
619256b5 ÇO |
3659 | ret = copyhooks(c, c2); |
3660 | if (ret < 0) { | |
3661 | ERROR("error copying hooks"); | |
3662 | goto out; | |
9be53773 SH |
3663 | } |
3664 | ||
3665 | if (copy_fstab(c, c2) < 0) { | |
3666 | ERROR("error copying fstab"); | |
9be53773 SH |
3667 | goto out; |
3668 | } | |
3669 | ||
1a0e70ac | 3670 | /* update macaddrs */ |
6b0d5538 | 3671 | if (!(flags & LXC_CLONE_KEEPMACADDR)) { |
67702c21 SH |
3672 | if (!network_new_hwaddrs(c2->lxc_conf)) { |
3673 | ERROR("Error updating mac addresses"); | |
6b0d5538 SH |
3674 | goto out; |
3675 | } | |
3676 | } | |
9be53773 | 3677 | |
1a0e70ac | 3678 | /* Update absolute paths for overlay mount directories. */ |
83e79752 | 3679 | if (ovl_update_abs_paths(c2->lxc_conf, c->config_path, c->name, lxcpath, newname) < 0) |
030ce9a9 CB |
3680 | goto out; |
3681 | ||
1a0e70ac CB |
3682 | /* We've now successfully created c2's storage, so clear it out if we |
3683 | * fail after this. | |
3684 | */ | |
176d9acb SH |
3685 | storage_copied = 1; |
3686 | ||
375c2258 | 3687 | if (!c2->save_config(c2, NULL)) |
9be53773 | 3688 | goto out; |
9be53773 | 3689 | |
1354955b SH |
3690 | if ((pid = fork()) < 0) { |
3691 | SYSERROR("fork"); | |
9be53773 | 3692 | goto out; |
1354955b SH |
3693 | } |
3694 | if (pid > 0) { | |
3695 | ret = wait_for_pid(pid); | |
3696 | if (ret) | |
3697 | goto out; | |
3698 | container_mem_unlock(c); | |
3699 | return c2; | |
3700 | } | |
3701 | data.c0 = c; | |
3702 | data.c1 = c2; | |
3703 | data.flags = flags; | |
3704 | data.hookargs = hookargs; | |
e0010464 | 3705 | if (am_guest_unpriv()) |
ee484f7f CB |
3706 | ret = userns_exec_full(c->lxc_conf, clone_update_rootfs_wrapper, |
3707 | &data, "clone_update_rootfs_wrapper"); | |
1354955b SH |
3708 | else |
3709 | ret = clone_update_rootfs(&data); | |
3710 | if (ret < 0) | |
d8480a31 | 3711 | _exit(EXIT_FAILURE); |
9be53773 | 3712 | |
5cee8c50 | 3713 | container_mem_unlock(c); |
d8480a31 | 3714 | _exit(EXIT_SUCCESS); |
9be53773 SH |
3715 | |
3716 | out: | |
5cee8c50 | 3717 | container_mem_unlock(c); |
375c2258 | 3718 | if (c2) { |
176d9acb SH |
3719 | if (!storage_copied) |
3720 | c2->lxc_conf->rootfs.path = NULL; | |
375c2258 | 3721 | c2->destroy(c2); |
9be53773 | 3722 | lxc_container_put(c2); |
375c2258 | 3723 | } |
9be53773 SH |
3724 | |
3725 | return NULL; | |
3726 | } | |
3727 | ||
858377e4 SH |
3728 | static struct lxc_container *lxcapi_clone(struct lxc_container *c, const char *newname, |
3729 | const char *lxcpath, int flags, | |
3730 | const char *bdevtype, const char *bdevdata, uint64_t newsize, | |
3731 | char **hookargs) | |
3732 | { | |
3733 | struct lxc_container * ret; | |
3734 | current_config = c ? c->lxc_conf : NULL; | |
3735 | ret = do_lxcapi_clone(c, newname, lxcpath, flags, bdevtype, bdevdata, newsize, hookargs); | |
3736 | current_config = NULL; | |
3737 | return ret; | |
3738 | } | |
3739 | ||
3740 | static bool do_lxcapi_rename(struct lxc_container *c, const char *newname) | |
06e5650e | 3741 | { |
10bc1861 | 3742 | struct lxc_storage *bdev; |
06e5650e | 3743 | struct lxc_container *newc; |
06e5650e | 3744 | |
d693cf93 | 3745 | if (!c || !c->name || !c->config_path || !c->lxc_conf) |
06e5650e ÇO |
3746 | return false; |
3747 | ||
18aa217b SH |
3748 | if (has_fs_snapshots(c) || has_snapshots(c)) { |
3749 | ERROR("Renaming a container with snapshots is not supported"); | |
3750 | return false; | |
3751 | } | |
8a388ed4 | 3752 | bdev = storage_init(c->lxc_conf); |
06e5650e ÇO |
3753 | if (!bdev) { |
3754 | ERROR("Failed to find original backing store type"); | |
3755 | return false; | |
3756 | } | |
3757 | ||
619256b5 | 3758 | newc = lxcapi_clone(c, newname, c->config_path, LXC_CLONE_KEEPMACADDR, NULL, bdev->type, 0, NULL); |
10bc1861 | 3759 | storage_put(bdev); |
06e5650e ÇO |
3760 | if (!newc) { |
3761 | lxc_container_put(newc); | |
3762 | return false; | |
3763 | } | |
3764 | ||
3765 | if (newc && lxcapi_is_defined(newc)) | |
3766 | lxc_container_put(newc); | |
3767 | ||
17a367d8 | 3768 | if (!container_destroy(c, NULL)) { |
06e5650e ÇO |
3769 | ERROR("Could not destroy existing container %s", c->name); |
3770 | return false; | |
3771 | } | |
3772 | return true; | |
3773 | } | |
3774 | ||
858377e4 SH |
3775 | WRAP_API_1(bool, lxcapi_rename, const char *) |
3776 | ||
a0e93eeb CS |
3777 | static int lxcapi_attach(struct lxc_container *c, lxc_attach_exec_t exec_function, void *exec_payload, lxc_attach_options_t *options, pid_t *attached_process) |
3778 | { | |
858377e4 SH |
3779 | int ret; |
3780 | ||
a0e93eeb CS |
3781 | if (!c) |
3782 | return -1; | |
3783 | ||
858377e4 SH |
3784 | current_config = c->lxc_conf; |
3785 | ||
3786 | ret = lxc_attach(c->name, c->config_path, exec_function, exec_payload, options, attached_process); | |
3787 | current_config = NULL; | |
3788 | return ret; | |
a0e93eeb CS |
3789 | } |
3790 | ||
858377e4 | 3791 | static int do_lxcapi_attach_run_wait(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char * const argv[]) |
a0e93eeb CS |
3792 | { |
3793 | lxc_attach_command_t command; | |
3794 | pid_t pid; | |
3795 | int r; | |
3796 | ||
3797 | if (!c) | |
3798 | return -1; | |
3799 | ||
3800 | command.program = (char*)program; | |
3801 | command.argv = (char**)argv; | |
3802 | r = lxc_attach(c->name, c->config_path, lxc_attach_run_command, &command, options, &pid); | |
3803 | if (r < 0) { | |
3804 | ERROR("ups"); | |
3805 | return r; | |
3806 | } | |
3807 | return lxc_wait_for_pid_status(pid); | |
3808 | } | |
3809 | ||
858377e4 SH |
3810 | static int lxcapi_attach_run_wait(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char * const argv[]) |
3811 | { | |
3812 | int ret; | |
3813 | current_config = c ? c->lxc_conf : NULL; | |
3814 | ret = do_lxcapi_attach_run_wait(c, options, program, argv); | |
3815 | current_config = NULL; | |
3816 | return ret; | |
3817 | } | |
3818 | ||
74a3920a | 3819 | static int get_next_index(const char *lxcpath, char *cname) |
f5dd1d53 SH |
3820 | { |
3821 | char *fname; | |
3822 | struct stat sb; | |
3823 | int i = 0, ret; | |
3824 | ||
3825 | fname = alloca(strlen(lxcpath) + 20); | |
3826 | while (1) { | |
3827 | sprintf(fname, "%s/snap%d", lxcpath, i); | |
3828 | ret = stat(fname, &sb); | |
3829 | if (ret != 0) | |
3830 | return i; | |
3831 | i++; | |
3832 | } | |
3833 | } | |
3834 | ||
18aa217b SH |
3835 | static bool get_snappath_dir(struct lxc_container *c, char *snappath) |
3836 | { | |
3837 | int ret; | |
3838 | /* | |
3839 | * If the old style snapshot path exists, use it | |
3840 | * /var/lib/lxc -> /var/lib/lxcsnaps | |
3841 | */ | |
3842 | ret = snprintf(snappath, MAXPATHLEN, "%ssnaps", c->config_path); | |
3843 | if (ret < 0 || ret >= MAXPATHLEN) | |
3844 | return false; | |
3845 | if (dir_exists(snappath)) { | |
3846 | ret = snprintf(snappath, MAXPATHLEN, "%ssnaps/%s", c->config_path, c->name); | |
3847 | if (ret < 0 || ret >= MAXPATHLEN) | |
3848 | return false; | |
3849 | return true; | |
3850 | } | |
3851 | ||
3852 | /* | |
3853 | * Use the new style path | |
3854 | * /var/lib/lxc -> /var/lib/lxc + c->name + /snaps + \0 | |
3855 | */ | |
3856 | ret = snprintf(snappath, MAXPATHLEN, "%s/%s/snaps", c->config_path, c->name); | |
3857 | if (ret < 0 || ret >= MAXPATHLEN) | |
3858 | return false; | |
3859 | return true; | |
3860 | } | |
3861 | ||
858377e4 | 3862 | static int do_lxcapi_snapshot(struct lxc_container *c, const char *commentfile) |
f5dd1d53 SH |
3863 | { |
3864 | int i, flags, ret; | |
3865 | struct lxc_container *c2; | |
3866 | char snappath[MAXPATHLEN], newname[20]; | |
3867 | ||
840f05df SH |
3868 | if (!c || !lxcapi_is_defined(c)) |
3869 | return -1; | |
3870 | ||
10bc1861 | 3871 | if (!storage_can_backup(c->lxc_conf)) { |
cdd01be2 SH |
3872 | ERROR("%s's backing store cannot be backed up.", c->name); |
3873 | ERROR("Your container must use another backing store type."); | |
3874 | return -1; | |
3875 | } | |
3876 | ||
18aa217b | 3877 | if (!get_snappath_dir(c, snappath)) |
f5dd1d53 | 3878 | return -1; |
18aa217b | 3879 | |
f5dd1d53 SH |
3880 | i = get_next_index(snappath, c->name); |
3881 | ||
3882 | if (mkdir_p(snappath, 0755) < 0) { | |
3883 | ERROR("Failed to create snapshot directory %s", snappath); | |
3884 | return -1; | |
3885 | } | |
3886 | ||
3887 | ret = snprintf(newname, 20, "snap%d", i); | |
3888 | if (ret < 0 || ret >= 20) | |
3889 | return -1; | |
3890 | ||
0a83cbbb SH |
3891 | /* |
3892 | * We pass LXC_CLONE_SNAPSHOT to make sure that a rdepends file entry is | |
3893 | * created in the original container | |
3894 | */ | |
3895 | flags = LXC_CLONE_SNAPSHOT | LXC_CLONE_KEEPMACADDR | LXC_CLONE_KEEPNAME | | |
3896 | LXC_CLONE_KEEPBDEVTYPE | LXC_CLONE_MAYBE_SNAPSHOT; | |
068aa488 | 3897 | if (storage_is_dir(c->lxc_conf)) { |
8c39f7a4 SH |
3898 | ERROR("Snapshot of directory-backed container requested."); |
3899 | ERROR("Making a copy-clone. If you do want snapshots, then"); | |
ba115175 | 3900 | ERROR("please create an aufs or overlay clone first, snapshot that"); |
8c39f7a4 SH |
3901 | ERROR("and keep the original container pristine."); |
3902 | flags &= ~LXC_CLONE_SNAPSHOT | LXC_CLONE_MAYBE_SNAPSHOT; | |
3903 | } | |
858377e4 | 3904 | c2 = do_lxcapi_clone(c, newname, snappath, flags, NULL, NULL, 0, NULL); |
f5dd1d53 | 3905 | if (!c2) { |
959aee9c | 3906 | ERROR("clone of %s:%s failed", c->config_path, c->name); |
f5dd1d53 SH |
3907 | return -1; |
3908 | } | |
3909 | ||
3910 | lxc_container_put(c2); | |
3911 | ||
1a0e70ac | 3912 | /* Now write down the creation time. */ |
f5dd1d53 SH |
3913 | time_t timer; |
3914 | char buffer[25]; | |
3915 | struct tm* tm_info; | |
025ed0f3 | 3916 | FILE *f; |
f5dd1d53 SH |
3917 | |
3918 | time(&timer); | |
3919 | tm_info = localtime(&timer); | |
3920 | ||
3921 | strftime(buffer, 25, "%Y:%m:%d %H:%M:%S", tm_info); | |
3922 | ||
3923 | char *dfnam = alloca(strlen(snappath) + strlen(newname) + 5); | |
3924 | sprintf(dfnam, "%s/%s/ts", snappath, newname); | |
025ed0f3 | 3925 | f = fopen(dfnam, "w"); |
f5dd1d53 | 3926 | if (!f) { |
959aee9c | 3927 | ERROR("Failed to open %s", dfnam); |
f5dd1d53 SH |
3928 | return -1; |
3929 | } | |
3930 | if (fprintf(f, "%s", buffer) < 0) { | |
3931 | SYSERROR("Writing timestamp"); | |
3932 | fclose(f); | |
3933 | return -1; | |
3934 | } | |
025ed0f3 | 3935 | ret = fclose(f); |
025ed0f3 | 3936 | if (ret != 0) { |
f5dd1d53 SH |
3937 | SYSERROR("Writing timestamp"); |
3938 | return -1; | |
3939 | } | |
3940 | ||
3941 | if (commentfile) { | |
1a0e70ac | 3942 | /* $p / $name / comment \0 */ |
f5dd1d53 SH |
3943 | int len = strlen(snappath) + strlen(newname) + 10; |
3944 | char *path = alloca(len); | |
3945 | sprintf(path, "%s/%s/comment", snappath, newname); | |
3946 | return copy_file(commentfile, path) < 0 ? -1 : i; | |
3947 | } | |
3948 | ||
3949 | return i; | |
3950 | } | |
3951 | ||
858377e4 SH |
3952 | WRAP_API_1(int, lxcapi_snapshot, const char *) |
3953 | ||
f5dd1d53 SH |
3954 | static void lxcsnap_free(struct lxc_snapshot *s) |
3955 | { | |
f10fad2f ME |
3956 | free(s->name); |
3957 | free(s->comment_pathname); | |
3958 | free(s->timestamp); | |
3959 | free(s->lxcpath); | |
f5dd1d53 SH |
3960 | } |
3961 | ||
3962 | static char *get_snapcomment_path(char* snappath, char *name) | |
3963 | { | |
1a0e70ac | 3964 | /* $snappath/$name/comment */ |
f5dd1d53 SH |
3965 | int ret, len = strlen(snappath) + strlen(name) + 10; |
3966 | char *s = malloc(len); | |
3967 | ||
3968 | if (s) { | |
3969 | ret = snprintf(s, len, "%s/%s/comment", snappath, name); | |
3970 | if (ret < 0 || ret >= len) { | |
3971 | free(s); | |
3972 | s = NULL; | |
3973 | } | |
3974 | } | |
3975 | return s; | |
3976 | } | |
3977 | ||
3978 | static char *get_timestamp(char* snappath, char *name) | |
3979 | { | |
3980 | char path[MAXPATHLEN], *s = NULL; | |
3981 | int ret, len; | |
3982 | FILE *fin; | |
3983 | ||
3984 | ret = snprintf(path, MAXPATHLEN, "%s/%s/ts", snappath, name); | |
3985 | if (ret < 0 || ret >= MAXPATHLEN) | |
3986 | return NULL; | |
025ed0f3 | 3987 | fin = fopen(path, "r"); |
025ed0f3 | 3988 | if (!fin) |
f5dd1d53 SH |
3989 | return NULL; |
3990 | (void) fseek(fin, 0, SEEK_END); | |
3991 | len = ftell(fin); | |
3992 | (void) fseek(fin, 0, SEEK_SET); | |
3993 | if (len > 0) { | |
3994 | s = malloc(len+1); | |
3995 | if (s) { | |
3996 | s[len] = '\0'; | |
3997 | if (fread(s, 1, len, fin) != len) { | |
3998 | SYSERROR("reading timestamp"); | |
3999 | free(s); | |
4000 | s = NULL; | |
4001 | } | |
4002 | } | |
4003 | } | |
4004 | fclose(fin); | |
4005 | return s; | |
4006 | } | |
4007 | ||
858377e4 | 4008 | static int do_lxcapi_snapshot_list(struct lxc_container *c, struct lxc_snapshot **ret_snaps) |
f5dd1d53 SH |
4009 | { |
4010 | char snappath[MAXPATHLEN], path2[MAXPATHLEN]; | |
18aa217b | 4011 | int count = 0, ret; |
74f96976 | 4012 | struct dirent *direntp; |
f5dd1d53 SH |
4013 | struct lxc_snapshot *snaps =NULL, *nsnaps; |
4014 | DIR *dir; | |
4015 | ||
4016 | if (!c || !lxcapi_is_defined(c)) | |
4017 | return -1; | |
c868b261 | 4018 | |
18aa217b | 4019 | if (!get_snappath_dir(c, snappath)) { |
f5dd1d53 SH |
4020 | ERROR("path name too long"); |
4021 | return -1; | |
4022 | } | |
025ed0f3 | 4023 | dir = opendir(snappath); |
025ed0f3 | 4024 | if (!dir) { |
f5dd1d53 SH |
4025 | INFO("failed to open %s - assuming no snapshots", snappath); |
4026 | return 0; | |
4027 | } | |
4028 | ||
74f96976 | 4029 | while ((direntp = readdir(dir))) { |
f5dd1d53 SH |
4030 | if (!direntp) |
4031 | break; | |
4032 | ||
4033 | if (!strcmp(direntp->d_name, ".")) | |
4034 | continue; | |
4035 | ||
4036 | if (!strcmp(direntp->d_name, "..")) | |
4037 | continue; | |
4038 | ||
4039 | ret = snprintf(path2, MAXPATHLEN, "%s/%s/config", snappath, direntp->d_name); | |
4040 | if (ret < 0 || ret >= MAXPATHLEN) { | |
4041 | ERROR("pathname too long"); | |
4042 | goto out_free; | |
4043 | } | |
4044 | if (!file_exists(path2)) | |
4045 | continue; | |
4046 | nsnaps = realloc(snaps, (count + 1)*sizeof(*snaps)); | |
4047 | if (!nsnaps) { | |
4048 | SYSERROR("Out of memory"); | |
4049 | goto out_free; | |
4050 | } | |
4051 | snaps = nsnaps; | |
4052 | snaps[count].free = lxcsnap_free; | |
4053 | snaps[count].name = strdup(direntp->d_name); | |
4054 | if (!snaps[count].name) | |
4055 | goto out_free; | |
4056 | snaps[count].lxcpath = strdup(snappath); | |
4057 | if (!snaps[count].lxcpath) { | |
4058 | free(snaps[count].name); | |
4059 | goto out_free; | |
4060 | } | |
4061 | snaps[count].comment_pathname = get_snapcomment_path(snappath, direntp->d_name); | |
4062 | snaps[count].timestamp = get_timestamp(snappath, direntp->d_name); | |
4063 | count++; | |
4064 | } | |
4065 | ||
4066 | if (closedir(dir)) | |
4067 | WARN("failed to close directory"); | |
4068 | ||
4069 | *ret_snaps = snaps; | |
4070 | return count; | |
4071 | ||
4072 | out_free: | |
4073 | if (snaps) { | |
4074 | int i; | |
4075 | for (i=0; i<count; i++) | |
4076 | lxcsnap_free(&snaps[i]); | |
4077 | free(snaps); | |
4078 | } | |
9baa57bd SH |
4079 | if (closedir(dir)) |
4080 | WARN("failed to close directory"); | |
f5dd1d53 SH |
4081 | return -1; |
4082 | } | |
4083 | ||
858377e4 SH |
4084 | WRAP_API_1(int, lxcapi_snapshot_list, struct lxc_snapshot **) |
4085 | ||
4086 | static bool do_lxcapi_snapshot_restore(struct lxc_container *c, const char *snapname, const char *newname) | |
f5dd1d53 SH |
4087 | { |
4088 | char clonelxcpath[MAXPATHLEN]; | |
18aa217b | 4089 | int flags = 0; |
f5dd1d53 | 4090 | struct lxc_container *snap, *rest; |
10bc1861 | 4091 | struct lxc_storage *bdev; |
f5dd1d53 SH |
4092 | bool b = false; |
4093 | ||
4094 | if (!c || !c->name || !c->config_path) | |
4095 | return false; | |
4096 | ||
18aa217b SH |
4097 | if (has_fs_snapshots(c)) { |
4098 | ERROR("container rootfs has dependent snapshots"); | |
4099 | return false; | |
4100 | } | |
4101 | ||
8a388ed4 | 4102 | bdev = storage_init(c->lxc_conf); |
f5dd1d53 SH |
4103 | if (!bdev) { |
4104 | ERROR("Failed to find original backing store type"); | |
4105 | return false; | |
4106 | } | |
4107 | ||
17a367d8 CB |
4108 | /* For an overlay container the rootfs is considered immutable |
4109 | * and cannot be removed when restoring from a snapshot. We pass this | |
4110 | * internal flag along to communicate this to various parts of the | |
4111 | * codebase. | |
4112 | */ | |
4113 | if (!strcmp(bdev->type, "overlay") || !strcmp(bdev->type, "overlayfs")) | |
4114 | bdev->flags |= LXC_STORAGE_INTERNAL_OVERLAY_RESTORE; | |
4115 | ||
f5dd1d53 SH |
4116 | if (!newname) |
4117 | newname = c->name; | |
7e36f87e | 4118 | |
18aa217b | 4119 | if (!get_snappath_dir(c, clonelxcpath)) { |
10bc1861 | 4120 | storage_put(bdev); |
f5dd1d53 SH |
4121 | return false; |
4122 | } | |
1a0e70ac | 4123 | /* how should we lock this? */ |
f5dd1d53 SH |
4124 | |
4125 | snap = lxc_container_new(snapname, clonelxcpath); | |
4126 | if (!snap || !lxcapi_is_defined(snap)) { | |
4127 | ERROR("Could not open snapshot %s", snapname); | |
17a367d8 CB |
4128 | if (snap) |
4129 | lxc_container_put(snap); | |
10bc1861 | 4130 | storage_put(bdev); |
f5dd1d53 SH |
4131 | return false; |
4132 | } | |
4133 | ||
17a367d8 CB |
4134 | if (!strcmp(c->name, newname)) { |
4135 | if (!container_destroy(c, bdev)) { | |
7e36f87e ÇO |
4136 | ERROR("Could not destroy existing container %s", newname); |
4137 | lxc_container_put(snap); | |
10bc1861 | 4138 | storage_put(bdev); |
7e36f87e ÇO |
4139 | return false; |
4140 | } | |
4141 | } | |
4142 | ||
de269ee8 SH |
4143 | if (strcmp(bdev->type, "dir") != 0 && strcmp(bdev->type, "loop") != 0) |
4144 | flags = LXC_CLONE_SNAPSHOT | LXC_CLONE_MAYBE_SNAPSHOT; | |
17a367d8 CB |
4145 | |
4146 | if (!strcmp(bdev->type, "overlay") || !strcmp(bdev->type, "overlayfs")) | |
4147 | flags |= LXC_STORAGE_INTERNAL_OVERLAY_RESTORE; | |
09f6f8c4 CB |
4148 | rest = lxcapi_clone(snap, newname, c->config_path, flags, bdev->type, |
4149 | NULL, 0, NULL); | |
10bc1861 | 4150 | storage_put(bdev); |
f5dd1d53 SH |
4151 | if (rest && lxcapi_is_defined(rest)) |
4152 | b = true; | |
4153 | if (rest) | |
4154 | lxc_container_put(rest); | |
17a367d8 | 4155 | |
f5dd1d53 SH |
4156 | lxc_container_put(snap); |
4157 | return b; | |
4158 | } | |
4159 | ||
858377e4 SH |
4160 | WRAP_API_2(bool, lxcapi_snapshot_restore, const char *, const char *) |
4161 | ||
18aa217b | 4162 | static bool do_snapshot_destroy(const char *snapname, const char *clonelxcpath) |
771d96b3 | 4163 | { |
771d96b3 | 4164 | struct lxc_container *snap = NULL; |
18aa217b | 4165 | bool bret = false; |
771d96b3 ÇO |
4166 | |
4167 | snap = lxc_container_new(snapname, clonelxcpath); | |
18aa217b | 4168 | if (!snap) { |
771d96b3 ÇO |
4169 | ERROR("Could not find snapshot %s", snapname); |
4170 | goto err; | |
4171 | } | |
4172 | ||
858377e4 | 4173 | if (!do_lxcapi_destroy(snap)) { |
771d96b3 ÇO |
4174 | ERROR("Could not destroy snapshot %s", snapname); |
4175 | goto err; | |
4176 | } | |
18aa217b | 4177 | bret = true; |
771d96b3 | 4178 | |
771d96b3 ÇO |
4179 | err: |
4180 | if (snap) | |
4181 | lxc_container_put(snap); | |
18aa217b SH |
4182 | return bret; |
4183 | } | |
4184 | ||
4185 | static bool remove_all_snapshots(const char *path) | |
4186 | { | |
4187 | DIR *dir; | |
74f96976 | 4188 | struct dirent *direntp; |
18aa217b SH |
4189 | bool bret = true; |
4190 | ||
4191 | dir = opendir(path); | |
4192 | if (!dir) { | |
4193 | SYSERROR("opendir on snapshot path %s", path); | |
4194 | return false; | |
4195 | } | |
74f96976 | 4196 | while ((direntp = readdir(dir))) { |
18aa217b SH |
4197 | if (!strcmp(direntp->d_name, ".")) |
4198 | continue; | |
4199 | if (!strcmp(direntp->d_name, "..")) | |
4200 | continue; | |
4201 | if (!do_snapshot_destroy(direntp->d_name, path)) { | |
4202 | bret = false; | |
4203 | continue; | |
4204 | } | |
4205 | } | |
4206 | ||
4207 | closedir(dir); | |
4208 | ||
4209 | if (rmdir(path)) | |
4210 | SYSERROR("Error removing directory %s", path); | |
4211 | ||
4212 | return bret; | |
4213 | } | |
4214 | ||
858377e4 | 4215 | static bool do_lxcapi_snapshot_destroy(struct lxc_container *c, const char *snapname) |
18aa217b SH |
4216 | { |
4217 | char clonelxcpath[MAXPATHLEN]; | |
4218 | ||
4219 | if (!c || !c->name || !c->config_path || !snapname) | |
4220 | return false; | |
4221 | ||
4222 | if (!get_snappath_dir(c, clonelxcpath)) | |
4223 | return false; | |
4224 | ||
4225 | return do_snapshot_destroy(snapname, clonelxcpath); | |
4226 | } | |
4227 | ||
858377e4 SH |
4228 | WRAP_API_1(bool, lxcapi_snapshot_destroy, const char *) |
4229 | ||
4230 | static bool do_lxcapi_snapshot_destroy_all(struct lxc_container *c) | |
18aa217b SH |
4231 | { |
4232 | char clonelxcpath[MAXPATHLEN]; | |
4233 | ||
4234 | if (!c || !c->name || !c->config_path) | |
4235 | return false; | |
4236 | ||
4237 | if (!get_snappath_dir(c, clonelxcpath)) | |
4238 | return false; | |
4239 | ||
4240 | return remove_all_snapshots(clonelxcpath); | |
771d96b3 ÇO |
4241 | } |
4242 | ||
858377e4 SH |
4243 | WRAP_API(bool, lxcapi_snapshot_destroy_all) |
4244 | ||
4245 | static bool do_lxcapi_may_control(struct lxc_container *c) | |
b494d2dd SH |
4246 | { |
4247 | return lxc_try_cmd(c->name, c->config_path) == 0; | |
4248 | } | |
4249 | ||
858377e4 SH |
4250 | WRAP_API(bool, lxcapi_may_control) |
4251 | ||
d5aa23e6 | 4252 | static bool do_add_remove_node(pid_t init_pid, const char *path, bool add, |
b69dfc9f | 4253 | struct stat *st) |
d5aa23e6 | 4254 | { |
b69dfc9f CB |
4255 | int ret; |
4256 | char *tmp; | |
4257 | pid_t pid; | |
d5aa23e6 SH |
4258 | char chrootpath[MAXPATHLEN]; |
4259 | char *directory_path = NULL; | |
d5aa23e6 | 4260 | |
b69dfc9f CB |
4261 | pid = fork(); |
4262 | if (pid < 0) { | |
4263 | SYSERROR("Failed to fork()"); | |
d5aa23e6 SH |
4264 | return false; |
4265 | } | |
b69dfc9f | 4266 | |
d5aa23e6 | 4267 | if (pid) { |
b69dfc9f CB |
4268 | ret = wait_for_pid(pid); |
4269 | if (ret != 0) { | |
4270 | ERROR("Failed to create device node"); | |
d5aa23e6 SH |
4271 | return false; |
4272 | } | |
b69dfc9f | 4273 | |
d5aa23e6 SH |
4274 | return true; |
4275 | } | |
4276 | ||
4277 | /* prepare the path */ | |
4278 | ret = snprintf(chrootpath, MAXPATHLEN, "/proc/%d/root", init_pid); | |
4279 | if (ret < 0 || ret >= MAXPATHLEN) | |
4280 | return false; | |
4281 | ||
b69dfc9f CB |
4282 | ret = chroot(chrootpath); |
4283 | if (ret < 0) | |
a7764ce7 | 4284 | _exit(EXIT_FAILURE); |
b69dfc9f CB |
4285 | |
4286 | ret = chdir("/"); | |
4287 | if (ret < 0) | |
a7764ce7 | 4288 | _exit(EXIT_FAILURE); |
b69dfc9f | 4289 | |
d5aa23e6 | 4290 | /* remove path if it exists */ |
b69dfc9f CB |
4291 | ret = faccessat(AT_FDCWD, path, F_OK, AT_SYMLINK_NOFOLLOW); |
4292 | if(ret == 0) { | |
4293 | ret = unlink(path); | |
c7d76c09 CB |
4294 | if (ret < 0) { |
4295 | ERROR("%s - Failed to remove \"%s\"", strerror(errno), path); | |
a7764ce7 | 4296 | _exit(EXIT_FAILURE); |
c7d76c09 | 4297 | } |
d5aa23e6 | 4298 | } |
b69dfc9f | 4299 | |
d5aa23e6 | 4300 | if (!add) |
a7764ce7 | 4301 | _exit(EXIT_SUCCESS); |
d5aa23e6 SH |
4302 | |
4303 | /* create any missing directories */ | |
b69dfc9f CB |
4304 | tmp = strdup(path); |
4305 | if (!tmp) | |
a7764ce7 | 4306 | _exit(EXIT_FAILURE); |
b69dfc9f CB |
4307 | |
4308 | directory_path = dirname(tmp); | |
4309 | ret = mkdir_p(directory_path, 0755); | |
4310 | if (ret < 0 && errno != EEXIST) { | |
c7d76c09 | 4311 | ERROR("%s - Failed to create path \"%s\"", strerror(errno), directory_path); |
b69dfc9f | 4312 | free(tmp); |
a7764ce7 | 4313 | _exit(EXIT_FAILURE); |
d5aa23e6 SH |
4314 | } |
4315 | ||
4316 | /* create the device node */ | |
b69dfc9f CB |
4317 | ret = mknod(path, st->st_mode, st->st_rdev); |
4318 | free(tmp); | |
c7d76c09 CB |
4319 | if (ret < 0) { |
4320 | ERROR("%s - Failed to create device node at \"%s\"", strerror(errno), path); | |
a7764ce7 | 4321 | _exit(EXIT_FAILURE); |
c7d76c09 | 4322 | } |
d5aa23e6 | 4323 | |
a7764ce7 | 4324 | _exit(EXIT_SUCCESS); |
d5aa23e6 SH |
4325 | } |
4326 | ||
f0ca2726 | 4327 | static bool add_remove_device_node(struct lxc_container *c, const char *src_path, const char *dest_path, bool add) |
a9a0ed90 ÇO |
4328 | { |
4329 | int ret; | |
4330 | struct stat st; | |
238b3e5e | 4331 | char value[LXC_MAX_BUFFER]; |
f0ca2726 | 4332 | const char *p; |
a9a0ed90 ÇO |
4333 | |
4334 | /* make sure container is running */ | |
858377e4 | 4335 | if (!do_lxcapi_is_running(c)) { |
a9a0ed90 | 4336 | ERROR("container is not running"); |
d5aa23e6 | 4337 | return false; |
a9a0ed90 ÇO |
4338 | } |
4339 | ||
4340 | /* use src_path if dest_path is NULL otherwise use dest_path */ | |
4341 | p = dest_path ? dest_path : src_path; | |
4342 | ||
a9a0ed90 ÇO |
4343 | /* make sure we can access p */ |
4344 | if(access(p, F_OK) < 0 || stat(p, &st) < 0) | |
d5aa23e6 | 4345 | return false; |
a9a0ed90 ÇO |
4346 | |
4347 | /* continue if path is character device or block device */ | |
c6a9b0d7 | 4348 | if (S_ISCHR(st.st_mode)) |
238b3e5e | 4349 | ret = snprintf(value, LXC_MAX_BUFFER, "c %d:%d rwm", major(st.st_rdev), minor(st.st_rdev)); |
c6a9b0d7 | 4350 | else if (S_ISBLK(st.st_mode)) |
238b3e5e | 4351 | ret = snprintf(value, LXC_MAX_BUFFER, "b %d:%d rwm", major(st.st_rdev), minor(st.st_rdev)); |
a9a0ed90 | 4352 | else |
d5aa23e6 | 4353 | return false; |
a9a0ed90 ÇO |
4354 | |
4355 | /* check snprintf return code */ | |
238b3e5e | 4356 | if (ret < 0 || ret >= LXC_MAX_BUFFER) |
d5aa23e6 | 4357 | return false; |
a9a0ed90 | 4358 | |
858377e4 | 4359 | if (!do_add_remove_node(do_lxcapi_init_pid(c), p, add, &st)) |
d5aa23e6 | 4360 | return false; |
a9a0ed90 | 4361 | |
d5aa23e6 | 4362 | /* add or remove device to/from cgroup access list */ |
a9a0ed90 | 4363 | if (add) { |
858377e4 | 4364 | if (!do_lxcapi_set_cgroup_item(c, "devices.allow", value)) { |
a9a0ed90 | 4365 | ERROR("set_cgroup_item failed while adding the device node"); |
d5aa23e6 | 4366 | return false; |
a9a0ed90 ÇO |
4367 | } |
4368 | } else { | |
858377e4 | 4369 | if (!do_lxcapi_set_cgroup_item(c, "devices.deny", value)) { |
a9a0ed90 | 4370 | ERROR("set_cgroup_item failed while removing the device node"); |
d5aa23e6 | 4371 | return false; |
a9a0ed90 ÇO |
4372 | } |
4373 | } | |
d5aa23e6 | 4374 | |
a9a0ed90 | 4375 | return true; |
a9a0ed90 ÇO |
4376 | } |
4377 | ||
858377e4 | 4378 | static bool do_lxcapi_add_device_node(struct lxc_container *c, const char *src_path, const char *dest_path) |
a9a0ed90 | 4379 | { |
e0010464 | 4380 | // cannot mknod if we're not privileged wrt init_user_ns |
5384e99d | 4381 | if (am_host_unpriv()) { |
238b3e5e | 4382 | ERROR(LXC_UNPRIV_EOPNOTSUPP, __FUNCTION__); |
c868b261 ÇO |
4383 | return false; |
4384 | } | |
a9a0ed90 ÇO |
4385 | return add_remove_device_node(c, src_path, dest_path, true); |
4386 | } | |
4387 | ||
858377e4 SH |
4388 | WRAP_API_2(bool, lxcapi_add_device_node, const char *, const char *) |
4389 | ||
4390 | static bool do_lxcapi_remove_device_node(struct lxc_container *c, const char *src_path, const char *dest_path) | |
a9a0ed90 | 4391 | { |
e0010464 | 4392 | if (am_guest_unpriv()) { |
238b3e5e | 4393 | ERROR(LXC_UNPRIV_EOPNOTSUPP, __FUNCTION__); |
c868b261 ÇO |
4394 | return false; |
4395 | } | |
a9a0ed90 ÇO |
4396 | return add_remove_device_node(c, src_path, dest_path, false); |
4397 | } | |
4398 | ||
858377e4 SH |
4399 | WRAP_API_2(bool, lxcapi_remove_device_node, const char *, const char *) |
4400 | ||
c7d76c09 CB |
4401 | static bool do_lxcapi_attach_interface(struct lxc_container *c, |
4402 | const char *ifname, | |
4403 | const char *dst_ifname) | |
e58fae8f | 4404 | { |
c7d76c09 | 4405 | pid_t init_pid; |
e58fae8f | 4406 | int ret = 0; |
c7d76c09 | 4407 | |
e0010464 | 4408 | if (am_guest_unpriv()) { |
238b3e5e | 4409 | ERROR(LXC_UNPRIV_EOPNOTSUPP, __FUNCTION__); |
e58fae8f DY |
4410 | return false; |
4411 | } | |
4412 | ||
4413 | if (!ifname) { | |
4414 | ERROR("No source interface name given"); | |
4415 | return false; | |
4416 | } | |
4417 | ||
4418 | ret = lxc_netdev_isup(ifname); | |
e5848d39 SH |
4419 | if (ret > 0) { |
4420 | /* netdev of ifname is up. */ | |
e58fae8f DY |
4421 | ret = lxc_netdev_down(ifname); |
4422 | if (ret) | |
4423 | goto err; | |
4424 | } | |
4425 | ||
c7d76c09 CB |
4426 | init_pid = do_lxcapi_init_pid(c); |
4427 | ret = lxc_netdev_move_by_name(ifname, init_pid, dst_ifname); | |
e58fae8f DY |
4428 | if (ret) |
4429 | goto err; | |
4430 | ||
c7d76c09 | 4431 | INFO("Moved network device \"%s\" to network namespace of %d", ifname, init_pid); |
e58fae8f | 4432 | return true; |
e5848d39 | 4433 | |
e58fae8f | 4434 | err: |
e58fae8f DY |
4435 | return false; |
4436 | } | |
4437 | ||
858377e4 SH |
4438 | WRAP_API_2(bool, lxcapi_attach_interface, const char *, const char *) |
4439 | ||
c7d76c09 CB |
4440 | static bool do_lxcapi_detach_interface(struct lxc_container *c, |
4441 | const char *ifname, | |
4442 | const char *dst_ifname) | |
e58fae8f | 4443 | { |
c7d76c09 | 4444 | int ret; |
e58fae8f DY |
4445 | pid_t pid, pid_outside; |
4446 | ||
e0010464 SH |
4447 | /* |
4448 | * TODO - if this is a physical device, then we need am_host_unpriv. | |
4449 | * But for other types guest privilege suffices. | |
4450 | */ | |
4451 | if (am_guest_unpriv()) { | |
238b3e5e | 4452 | ERROR(LXC_UNPRIV_EOPNOTSUPP, __FUNCTION__); |
e58fae8f DY |
4453 | return false; |
4454 | } | |
4455 | ||
4456 | if (!ifname) { | |
4457 | ERROR("No source interface name given"); | |
4458 | return false; | |
4459 | } | |
4460 | ||
0059379f | 4461 | pid_outside = lxc_raw_getpid(); |
e58fae8f DY |
4462 | pid = fork(); |
4463 | if (pid < 0) { | |
c7d76c09 | 4464 | ERROR("Failed to fork"); |
e58fae8f DY |
4465 | return false; |
4466 | } | |
4467 | ||
1a0e70ac | 4468 | if (pid == 0) { /* child */ |
acbfeda8 CB |
4469 | pid_t init_pid; |
4470 | ||
4471 | init_pid = do_lxcapi_init_pid(c); | |
4472 | if (!switch_to_ns(init_pid, "net")) { | |
4473 | ERROR("Failed to enter network namespace"); | |
8d7b6c25 | 4474 | _exit(EXIT_FAILURE); |
e58fae8f DY |
4475 | } |
4476 | ||
4477 | ret = lxc_netdev_isup(ifname); | |
c7d76c09 CB |
4478 | if (ret < 0) { |
4479 | ERROR("Failed to determine whether network device \"%s\" is up", ifname); | |
8d7b6c25 | 4480 | _exit(EXIT_FAILURE); |
c7d76c09 | 4481 | } |
e58fae8f DY |
4482 | |
4483 | /* netdev of ifname is up. */ | |
4484 | if (ret) { | |
4485 | ret = lxc_netdev_down(ifname); | |
c7d76c09 CB |
4486 | if (ret) { |
4487 | ERROR("Failed to set network device \"%s\" down", ifname); | |
8d7b6c25 | 4488 | _exit(EXIT_FAILURE); |
c7d76c09 | 4489 | } |
e58fae8f DY |
4490 | } |
4491 | ||
4492 | ret = lxc_netdev_move_by_name(ifname, pid_outside, dst_ifname); | |
c7d76c09 CB |
4493 | /* -EINVAL means there is no netdev named as ifname. */ |
4494 | if (ret < 0) { | |
4495 | if (ret == -EINVAL) | |
4496 | ERROR("Network device \"%s\" not found", ifname); | |
4497 | else | |
4498 | ERROR("Failed to remove network device \"%s\"", ifname); | |
8d7b6c25 | 4499 | _exit(EXIT_FAILURE); |
e58fae8f | 4500 | } |
c7d76c09 | 4501 | |
8d7b6c25 | 4502 | _exit(EXIT_SUCCESS); |
e58fae8f DY |
4503 | } |
4504 | ||
c7d76c09 CB |
4505 | ret = wait_for_pid(pid); |
4506 | if (ret != 0) | |
e58fae8f DY |
4507 | return false; |
4508 | ||
c7d76c09 | 4509 | INFO("Moved network device \"%s\" to network namespace of %d", ifname, pid_outside); |
e58fae8f DY |
4510 | return true; |
4511 | } | |
4512 | ||
858377e4 SH |
4513 | WRAP_API_2(bool, lxcapi_detach_interface, const char *, const char *) |
4514 | ||
aef3d51e TA |
4515 | static int do_lxcapi_migrate(struct lxc_container *c, unsigned int cmd, |
4516 | struct migrate_opts *opts, unsigned int size) | |
bbd4e13e | 4517 | { |
6b9be523 | 4518 | int ret = -1; |
2cb80427 | 4519 | struct migrate_opts *valid_opts = opts; |
b5b12b9e | 4520 | uint64_t features_to_check = 0; |
85c50991 | 4521 | |
aef3d51e TA |
4522 | /* If the caller has a bigger (newer) struct migrate_opts, let's make |
4523 | * sure that the stuff on the end is zero, i.e. that they didn't ask us | |
4524 | * to do anything special. | |
4525 | */ | |
4526 | if (size > sizeof(*opts)) { | |
4527 | unsigned char *addr; | |
4528 | unsigned char *end; | |
4529 | ||
4530 | addr = (void *)opts + sizeof(*opts); | |
4531 | end = (void *)opts + size; | |
4532 | for (; addr < end; addr++) { | |
4533 | if (*addr) { | |
4534 | return -E2BIG; | |
4535 | } | |
4536 | } | |
85c50991 TA |
4537 | } |
4538 | ||
2cb80427 TA |
4539 | /* If the caller has a smaller struct, let's zero out the end for them |
4540 | * so we don't accidentally use bits of it that they didn't know about | |
4541 | * to initialize. | |
4542 | */ | |
4543 | if (size < sizeof(*opts)) { | |
4544 | valid_opts = malloc(sizeof(*opts)); | |
4545 | if (!valid_opts) | |
4546 | return -ENOMEM; | |
4547 | ||
4548 | memset(valid_opts, 0, sizeof(*opts)); | |
4549 | memcpy(valid_opts, opts, size); | |
4550 | } | |
4551 | ||
aef3d51e TA |
4552 | switch (cmd) { |
4553 | case MIGRATE_PRE_DUMP: | |
7ad13c91 TA |
4554 | if (!do_lxcapi_is_running(c)) { |
4555 | ERROR("container is not running"); | |
6b9be523 | 4556 | goto on_error; |
7ad13c91 | 4557 | } |
2cb80427 | 4558 | ret = !__criu_pre_dump(c, valid_opts); |
aef3d51e TA |
4559 | break; |
4560 | case MIGRATE_DUMP: | |
7ad13c91 TA |
4561 | if (!do_lxcapi_is_running(c)) { |
4562 | ERROR("container is not running"); | |
6b9be523 | 4563 | goto on_error; |
7ad13c91 | 4564 | } |
2cb80427 | 4565 | ret = !__criu_dump(c, valid_opts); |
aef3d51e TA |
4566 | break; |
4567 | case MIGRATE_RESTORE: | |
7ad13c91 TA |
4568 | if (do_lxcapi_is_running(c)) { |
4569 | ERROR("container is already running"); | |
6b9be523 | 4570 | goto on_error; |
7ad13c91 | 4571 | } |
2cb80427 | 4572 | ret = !__criu_restore(c, valid_opts); |
aef3d51e | 4573 | break; |
b5b12b9e AR |
4574 | case MIGRATE_FEATURE_CHECK: |
4575 | features_to_check = valid_opts->features_to_check; | |
4576 | ret = !__criu_check_feature(&features_to_check); | |
4577 | if (ret) { | |
4578 | /* Something went wrong. Let's let the caller | |
4579 | * know which feature checks failed. */ | |
4580 | valid_opts->features_to_check = features_to_check; | |
4581 | } | |
4582 | break; | |
aef3d51e TA |
4583 | default: |
4584 | ERROR("invalid migrate command %u", cmd); | |
4585 | ret = -EINVAL; | |
4586 | } | |
735f2c6e | 4587 | |
6b9be523 | 4588 | on_error: |
f686506d TA |
4589 | if (size < sizeof(*opts)) |
4590 | free(valid_opts); | |
4591 | ||
aef3d51e TA |
4592 | return ret; |
4593 | } | |
735f2c6e | 4594 | |
aef3d51e | 4595 | WRAP_API_3(int, lxcapi_migrate, unsigned int, struct migrate_opts *, unsigned int) |
735f2c6e | 4596 | |
aef3d51e TA |
4597 | static bool do_lxcapi_checkpoint(struct lxc_container *c, char *directory, bool stop, bool verbose) |
4598 | { | |
ebb088e1 AR |
4599 | struct migrate_opts opts; |
4600 | ||
4601 | memset(&opts, 0, sizeof(opts)); | |
4602 | ||
4603 | opts.directory = directory; | |
4604 | opts.stop = stop; | |
4605 | opts.verbose = verbose; | |
735f2c6e | 4606 | |
aef3d51e | 4607 | return !do_lxcapi_migrate(c, MIGRATE_DUMP, &opts, sizeof(opts)); |
735f2c6e TA |
4608 | } |
4609 | ||
858377e4 SH |
4610 | WRAP_API_3(bool, lxcapi_checkpoint, char *, bool, bool) |
4611 | ||
4612 | static bool do_lxcapi_restore(struct lxc_container *c, char *directory, bool verbose) | |
c9d8f2ee | 4613 | { |
ebb088e1 AR |
4614 | struct migrate_opts opts; |
4615 | ||
4616 | memset(&opts, 0, sizeof(opts)); | |
4617 | ||
4618 | opts.directory = directory; | |
4619 | opts.verbose = verbose; | |
c9d8f2ee | 4620 | |
aef3d51e | 4621 | return !do_lxcapi_migrate(c, MIGRATE_RESTORE, &opts, sizeof(opts)); |
735f2c6e TA |
4622 | } |
4623 | ||
858377e4 SH |
4624 | WRAP_API_2(bool, lxcapi_restore, char *, bool) |
4625 | ||
a0e93eeb CS |
4626 | static int lxcapi_attach_run_waitl(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char *arg, ...) |
4627 | { | |
4628 | va_list ap; | |
4629 | const char **argv; | |
4630 | int ret; | |
4631 | ||
4632 | if (!c) | |
4633 | return -1; | |
4634 | ||
858377e4 SH |
4635 | current_config = c->lxc_conf; |
4636 | ||
a0e93eeb CS |
4637 | va_start(ap, arg); |
4638 | argv = lxc_va_arg_list_to_argv_const(ap, 1); | |
4639 | va_end(ap); | |
4640 | ||
4641 | if (!argv) { | |
4642 | ERROR("Memory allocation error."); | |
858377e4 SH |
4643 | ret = -1; |
4644 | goto out; | |
a0e93eeb CS |
4645 | } |
4646 | argv[0] = arg; | |
4647 | ||
858377e4 | 4648 | ret = do_lxcapi_attach_run_wait(c, options, program, (const char * const *)argv); |
a0e93eeb | 4649 | free((void*)argv); |
858377e4 SH |
4650 | out: |
4651 | current_config = NULL; | |
a0e93eeb CS |
4652 | return ret; |
4653 | } | |
4654 | ||
afeecbba | 4655 | struct lxc_container *lxc_container_new(const char *name, const char *configpath) |
72d0e1cb SG |
4656 | { |
4657 | struct lxc_container *c; | |
72d0e1cb | 4658 | |
18aa217b SH |
4659 | if (!name) |
4660 | return NULL; | |
4661 | ||
72d0e1cb SG |
4662 | c = malloc(sizeof(*c)); |
4663 | if (!c) { | |
e9e29a33 | 4664 | fprintf(stderr, "Failed to allocate memory for %s\n", name); |
72d0e1cb SG |
4665 | return NULL; |
4666 | } | |
4667 | memset(c, 0, sizeof(*c)); | |
4668 | ||
afeecbba SH |
4669 | if (configpath) |
4670 | c->config_path = strdup(configpath); | |
4671 | else | |
593e8478 | 4672 | c->config_path = strdup(lxc_global_config_value("lxc.lxcpath")); |
afeecbba | 4673 | |
2a59a681 | 4674 | if (!c->config_path) { |
e9e29a33 | 4675 | fprintf(stderr, "Failed to allocate memory for %s\n", name); |
2a59a681 SH |
4676 | goto err; |
4677 | } | |
4678 | ||
f5dd1d53 | 4679 | remove_trailing_slashes(c->config_path); |
72d0e1cb SG |
4680 | c->name = malloc(strlen(name)+1); |
4681 | if (!c->name) { | |
e9e29a33 | 4682 | fprintf(stderr, "Failed to allocate memory for %s\n", name); |
72d0e1cb SG |
4683 | goto err; |
4684 | } | |
4685 | strcpy(c->name, name); | |
4686 | ||
4687 | c->numthreads = 1; | |
e9e29a33 CB |
4688 | c->slock = lxc_newlock(c->config_path, name); |
4689 | if (!c->slock) { | |
4690 | fprintf(stderr, "Failed to create lock for %s\n", name); | |
72d0e1cb SG |
4691 | goto err; |
4692 | } | |
4693 | ||
e9e29a33 CB |
4694 | c->privlock = lxc_newlock(NULL, NULL); |
4695 | if (!c->privlock) { | |
4696 | fprintf(stderr, "Failed to create private lock for %s\n", name); | |
72d0e1cb SG |
4697 | goto err; |
4698 | } | |
4699 | ||
afeecbba | 4700 | if (!set_config_filename(c)) { |
e9e29a33 | 4701 | fprintf(stderr, "Failed to create config file name for %s\n", name); |
72d0e1cb SG |
4702 | goto err; |
4703 | } | |
72d0e1cb | 4704 | |
e9e29a33 CB |
4705 | if (file_exists(c->configfile) && !lxcapi_load_config(c, NULL)) { |
4706 | fprintf(stderr, "Failed to load config for %s\n", name); | |
bac806d1 | 4707 | goto err; |
e9e29a33 | 4708 | } |
72d0e1cb | 4709 | |
3e625e2d | 4710 | if (ongoing_create(c) == 2) { |
e9e29a33 | 4711 | ERROR("Failed to complete container creation for %s", c->name); |
17a367d8 | 4712 | container_destroy(c, NULL); |
4df7f012 | 4713 | lxcapi_clear_config(c); |
3e625e2d | 4714 | } |
a2739df5 | 4715 | c->daemonize = true; |
72cf75fa | 4716 | c->pidfile = NULL; |
3e625e2d | 4717 | |
1a0e70ac | 4718 | /* Assign the member functions. */ |
72d0e1cb SG |
4719 | c->is_defined = lxcapi_is_defined; |
4720 | c->state = lxcapi_state; | |
4721 | c->is_running = lxcapi_is_running; | |
4722 | c->freeze = lxcapi_freeze; | |
4723 | c->unfreeze = lxcapi_unfreeze; | |
0115f8fd | 4724 | c->console = lxcapi_console; |
b5159817 | 4725 | c->console_getfd = lxcapi_console_getfd; |
72d0e1cb SG |
4726 | c->init_pid = lxcapi_init_pid; |
4727 | c->load_config = lxcapi_load_config; | |
4728 | c->want_daemonize = lxcapi_want_daemonize; | |
130a1888 | 4729 | c->want_close_all_fds = lxcapi_want_close_all_fds; |
72d0e1cb SG |
4730 | c->start = lxcapi_start; |
4731 | c->startl = lxcapi_startl; | |
4732 | c->stop = lxcapi_stop; | |
4733 | c->config_file_name = lxcapi_config_file_name; | |
4734 | c->wait = lxcapi_wait; | |
4735 | c->set_config_item = lxcapi_set_config_item; | |
4736 | c->destroy = lxcapi_destroy; | |
18aa217b | 4737 | c->destroy_with_snapshots = lxcapi_destroy_with_snapshots; |
06e5650e | 4738 | c->rename = lxcapi_rename; |
72d0e1cb SG |
4739 | c->save_config = lxcapi_save_config; |
4740 | c->get_keys = lxcapi_get_keys; | |
4741 | c->create = lxcapi_create; | |
4742 | c->createl = lxcapi_createl; | |
4743 | c->shutdown = lxcapi_shutdown; | |
3e625e2d | 4744 | c->reboot = lxcapi_reboot; |
d39b10eb | 4745 | c->reboot2 = lxcapi_reboot2; |
4df7f012 | 4746 | c->clear_config = lxcapi_clear_config; |
72d0e1cb SG |
4747 | c->clear_config_item = lxcapi_clear_config_item; |
4748 | c->get_config_item = lxcapi_get_config_item; | |
8ac18377 | 4749 | c->get_running_config_item = lxcapi_get_running_config_item; |
794dd120 SH |
4750 | c->get_cgroup_item = lxcapi_get_cgroup_item; |
4751 | c->set_cgroup_item = lxcapi_set_cgroup_item; | |
2a59a681 SH |
4752 | c->get_config_path = lxcapi_get_config_path; |
4753 | c->set_config_path = lxcapi_set_config_path; | |
9be53773 | 4754 | c->clone = lxcapi_clone; |
799f29ab | 4755 | c->get_interfaces = lxcapi_get_interfaces; |
9c83a661 | 4756 | c->get_ips = lxcapi_get_ips; |
a0e93eeb CS |
4757 | c->attach = lxcapi_attach; |
4758 | c->attach_run_wait = lxcapi_attach_run_wait; | |
4759 | c->attach_run_waitl = lxcapi_attach_run_waitl; | |
f5dd1d53 SH |
4760 | c->snapshot = lxcapi_snapshot; |
4761 | c->snapshot_list = lxcapi_snapshot_list; | |
4762 | c->snapshot_restore = lxcapi_snapshot_restore; | |
771d96b3 | 4763 | c->snapshot_destroy = lxcapi_snapshot_destroy; |
18aa217b | 4764 | c->snapshot_destroy_all = lxcapi_snapshot_destroy_all; |
b494d2dd | 4765 | c->may_control = lxcapi_may_control; |
a9a0ed90 ÇO |
4766 | c->add_device_node = lxcapi_add_device_node; |
4767 | c->remove_device_node = lxcapi_remove_device_node; | |
e58fae8f DY |
4768 | c->attach_interface = lxcapi_attach_interface; |
4769 | c->detach_interface = lxcapi_detach_interface; | |
735f2c6e TA |
4770 | c->checkpoint = lxcapi_checkpoint; |
4771 | c->restore = lxcapi_restore; | |
aef3d51e | 4772 | c->migrate = lxcapi_migrate; |
191d43cc | 4773 | c->console_log = lxcapi_console_log; |
72d0e1cb | 4774 | |
72d0e1cb SG |
4775 | return c; |
4776 | ||
4777 | err: | |
4778 | lxc_container_free(c); | |
4779 | return NULL; | |
4780 | } | |
4781 | ||
4a7c7daa | 4782 | int lxc_get_wait_states(const char **states) |
72d0e1cb SG |
4783 | { |
4784 | int i; | |
4785 | ||
4786 | if (states) | |
4787 | for (i=0; i<MAX_STATE; i++) | |
4788 | states[i] = lxc_state2str(i); | |
4789 | return MAX_STATE; | |
4790 | } | |
a41f104b | 4791 | |
a41f104b SH |
4792 | /* |
4793 | * These next two could probably be done smarter with reusing a common function | |
4794 | * with different iterators and tests... | |
4795 | */ | |
4796 | int list_defined_containers(const char *lxcpath, char ***names, struct lxc_container ***cret) | |
4797 | { | |
4798 | DIR *dir; | |
4799 | int i, cfound = 0, nfound = 0; | |
74f96976 | 4800 | struct dirent *direntp; |
a41f104b SH |
4801 | struct lxc_container *c; |
4802 | ||
4803 | if (!lxcpath) | |
593e8478 | 4804 | lxcpath = lxc_global_config_value("lxc.lxcpath"); |
a41f104b | 4805 | |
a41f104b | 4806 | dir = opendir(lxcpath); |
a41f104b SH |
4807 | if (!dir) { |
4808 | SYSERROR("opendir on lxcpath"); | |
4809 | return -1; | |
4810 | } | |
4811 | ||
4812 | if (cret) | |
4813 | *cret = NULL; | |
4814 | if (names) | |
4815 | *names = NULL; | |
4816 | ||
74f96976 | 4817 | while ((direntp = readdir(dir))) { |
a41f104b SH |
4818 | if (!direntp) |
4819 | break; | |
e4ebeab1 | 4820 | |
1a0e70ac | 4821 | /* Ignore '.', '..' and any hidden directory. */ |
e4ebeab1 | 4822 | if (!strncmp(direntp->d_name, ".", 1)) |
a41f104b SH |
4823 | continue; |
4824 | ||
4825 | if (!config_file_exists(lxcpath, direntp->d_name)) | |
4826 | continue; | |
4827 | ||
4828 | if (names) { | |
9c88ff1f | 4829 | if (!add_to_array(names, direntp->d_name, cfound)) |
a41f104b SH |
4830 | goto free_bad; |
4831 | } | |
4832 | cfound++; | |
4833 | ||
4834 | if (!cret) { | |
4835 | nfound++; | |
4836 | continue; | |
4837 | } | |
4838 | ||
4839 | c = lxc_container_new(direntp->d_name, lxcpath); | |
4840 | if (!c) { | |
4841 | INFO("Container %s:%s has a config but could not be loaded", | |
4842 | lxcpath, direntp->d_name); | |
4843 | if (names) | |
9c88ff1f ÇO |
4844 | if(!remove_from_array(names, direntp->d_name, cfound--)) |
4845 | goto free_bad; | |
a41f104b SH |
4846 | continue; |
4847 | } | |
858377e4 | 4848 | if (!do_lxcapi_is_defined(c)) { |
a41f104b SH |
4849 | INFO("Container %s:%s has a config but is not defined", |
4850 | lxcpath, direntp->d_name); | |
4851 | if (names) | |
9c88ff1f ÇO |
4852 | if(!remove_from_array(names, direntp->d_name, cfound--)) |
4853 | goto free_bad; | |
a41f104b SH |
4854 | lxc_container_put(c); |
4855 | continue; | |
4856 | } | |
4857 | ||
2871830a | 4858 | if (!add_to_clist(cret, c, nfound, true)) { |
a41f104b SH |
4859 | lxc_container_put(c); |
4860 | goto free_bad; | |
4861 | } | |
4862 | nfound++; | |
4863 | } | |
4864 | ||
a41f104b | 4865 | closedir(dir); |
a41f104b SH |
4866 | return nfound; |
4867 | ||
4868 | free_bad: | |
4869 | if (names && *names) { | |
4870 | for (i=0; i<cfound; i++) | |
4871 | free((*names)[i]); | |
4872 | free(*names); | |
4873 | } | |
4874 | if (cret && *cret) { | |
4875 | for (i=0; i<nfound; i++) | |
4876 | lxc_container_put((*cret)[i]); | |
4877 | free(*cret); | |
4878 | } | |
a41f104b | 4879 | closedir(dir); |
a41f104b SH |
4880 | return -1; |
4881 | } | |
4882 | ||
148a9d27 DE |
4883 | int list_active_containers(const char *lxcpath, char ***nret, |
4884 | struct lxc_container ***cret) | |
a41f104b | 4885 | { |
148a9d27 | 4886 | int i, ret = -1, cret_cnt = 0, ct_name_cnt = 0; |
a41f104b SH |
4887 | int lxcpath_len; |
4888 | char *line = NULL; | |
148a9d27 | 4889 | char **ct_name = NULL; |
a41f104b | 4890 | size_t len = 0; |
97bc2422 | 4891 | struct lxc_container *c = NULL; |
88556fd7 | 4892 | bool is_hashed; |
a41f104b SH |
4893 | |
4894 | if (!lxcpath) | |
593e8478 | 4895 | lxcpath = lxc_global_config_value("lxc.lxcpath"); |
a41f104b SH |
4896 | lxcpath_len = strlen(lxcpath); |
4897 | ||
4898 | if (cret) | |
4899 | *cret = NULL; | |
148a9d27 DE |
4900 | if (nret) |
4901 | *nret = NULL; | |
a41f104b | 4902 | |
a41f104b | 4903 | FILE *f = fopen("/proc/net/unix", "r"); |
a41f104b SH |
4904 | if (!f) |
4905 | return -1; | |
4906 | ||
4907 | while (getline(&line, &len, f) != -1) { | |
88556fd7 | 4908 | |
0f8f9c8a | 4909 | char *p = strrchr(line, ' '), *p2; |
a41f104b SH |
4910 | if (!p) |
4911 | continue; | |
4912 | p++; | |
4913 | if (*p != 0x40) | |
4914 | continue; | |
4915 | p++; | |
88556fd7 ÇO |
4916 | |
4917 | is_hashed = false; | |
4918 | if (strncmp(p, lxcpath, lxcpath_len) == 0) { | |
4919 | p += lxcpath_len; | |
4920 | } else if (strncmp(p, "lxc/", 4) == 0) { | |
4921 | p += 4; | |
4922 | is_hashed = true; | |
4923 | } else { | |
a41f104b | 4924 | continue; |
88556fd7 ÇO |
4925 | } |
4926 | ||
a41f104b SH |
4927 | while (*p == '/') |
4928 | p++; | |
4929 | ||
1a0e70ac | 4930 | /* Now p is the start of lxc_name. */ |
46cd2845 | 4931 | p2 = strchr(p, '/'); |
a41f104b SH |
4932 | if (!p2 || strncmp(p2, "/command", 8) != 0) |
4933 | continue; | |
4934 | *p2 = '\0'; | |
4935 | ||
88556fd7 | 4936 | if (is_hashed) { |
899a9f55 CB |
4937 | char *recvpath = lxc_cmd_get_lxcpath(p); |
4938 | if (!recvpath) | |
4939 | continue; | |
4940 | if (strncmp(lxcpath, recvpath, lxcpath_len) != 0) | |
88556fd7 ÇO |
4941 | continue; |
4942 | p = lxc_cmd_get_name(p); | |
ee2d7093 L |
4943 | if (!p) |
4944 | continue; | |
88556fd7 ÇO |
4945 | } |
4946 | ||
148a9d27 | 4947 | if (array_contains(&ct_name, p, ct_name_cnt)) |
9c88ff1f ÇO |
4948 | continue; |
4949 | ||
148a9d27 DE |
4950 | if (!add_to_array(&ct_name, p, ct_name_cnt)) |
4951 | goto free_cret_list; | |
9c88ff1f | 4952 | |
148a9d27 | 4953 | ct_name_cnt++; |
a41f104b | 4954 | |
148a9d27 | 4955 | if (!cret) |
a41f104b | 4956 | continue; |
a41f104b SH |
4957 | |
4958 | c = lxc_container_new(p, lxcpath); | |
4959 | if (!c) { | |
4960 | INFO("Container %s:%s is running but could not be loaded", | |
4961 | lxcpath, p); | |
148a9d27 | 4962 | remove_from_array(&ct_name, p, ct_name_cnt--); |
a41f104b SH |
4963 | continue; |
4964 | } | |
4965 | ||
4966 | /* | |
4967 | * If this is an anonymous container, then is_defined *can* | |
4968 | * return false. So we don't do that check. Count on the | |
4969 | * fact that the command socket exists. | |
4970 | */ | |
4971 | ||
148a9d27 | 4972 | if (!add_to_clist(cret, c, cret_cnt, true)) { |
a41f104b | 4973 | lxc_container_put(c); |
148a9d27 | 4974 | goto free_cret_list; |
a41f104b | 4975 | } |
148a9d27 | 4976 | cret_cnt++; |
a41f104b SH |
4977 | } |
4978 | ||
97bc2422 CB |
4979 | if (nret && cret && cret_cnt != ct_name_cnt) { |
4980 | if (c) | |
4981 | lxc_container_put(c); | |
4982 | goto free_cret_list; | |
4983 | } | |
4984 | ||
148a9d27 DE |
4985 | ret = ct_name_cnt; |
4986 | if (nret) | |
4987 | *nret = ct_name; | |
4988 | else | |
4989 | goto free_ct_name; | |
4990 | goto out; | |
a41f104b | 4991 | |
148a9d27 | 4992 | free_cret_list: |
a41f104b | 4993 | if (cret && *cret) { |
148a9d27 | 4994 | for (i = 0; i < cret_cnt; i++) |
a41f104b SH |
4995 | lxc_container_put((*cret)[i]); |
4996 | free(*cret); | |
4997 | } | |
148a9d27 DE |
4998 | |
4999 | free_ct_name: | |
5000 | if (ct_name) { | |
5001 | for (i = 0; i < ct_name_cnt; i++) | |
5002 | free(ct_name[i]); | |
5003 | free(ct_name); | |
5004 | } | |
5005 | ||
5006 | out: | |
f10fad2f | 5007 | free(line); |
e853a32d | 5008 | |
a41f104b | 5009 | fclose(f); |
148a9d27 | 5010 | return ret; |
a41f104b | 5011 | } |
2871830a DE |
5012 | |
5013 | int list_all_containers(const char *lxcpath, char ***nret, | |
5014 | struct lxc_container ***cret) | |
5015 | { | |
5016 | int i, ret, active_cnt, ct_cnt, ct_list_cnt; | |
5017 | char **active_name; | |
5018 | char **ct_name; | |
5019 | struct lxc_container **ct_list = NULL; | |
5020 | ||
5021 | ct_cnt = list_defined_containers(lxcpath, &ct_name, NULL); | |
5022 | if (ct_cnt < 0) | |
5023 | return ct_cnt; | |
5024 | ||
5025 | active_cnt = list_active_containers(lxcpath, &active_name, NULL); | |
5026 | if (active_cnt < 0) { | |
5027 | ret = active_cnt; | |
5028 | goto free_ct_name; | |
5029 | } | |
5030 | ||
5031 | for (i = 0; i < active_cnt; i++) { | |
5032 | if (!array_contains(&ct_name, active_name[i], ct_cnt)) { | |
5033 | if (!add_to_array(&ct_name, active_name[i], ct_cnt)) { | |
5034 | ret = -1; | |
5035 | goto free_active_name; | |
5036 | } | |
5037 | ct_cnt++; | |
5038 | } | |
5039 | free(active_name[i]); | |
5040 | active_name[i] = NULL; | |
5041 | } | |
5042 | free(active_name); | |
5043 | active_name = NULL; | |
5044 | active_cnt = 0; | |
5045 | ||
5046 | for (i = 0, ct_list_cnt = 0; i < ct_cnt && cret; i++) { | |
5047 | struct lxc_container *c; | |
5048 | ||
5049 | c = lxc_container_new(ct_name[i], lxcpath); | |
5050 | if (!c) { | |
5051 | WARN("Container %s:%s could not be loaded", lxcpath, ct_name[i]); | |
5052 | remove_from_array(&ct_name, ct_name[i], ct_cnt--); | |
5053 | continue; | |
5054 | } | |
5055 | ||
5056 | if (!add_to_clist(&ct_list, c, ct_list_cnt, false)) { | |
5057 | lxc_container_put(c); | |
5058 | ret = -1; | |
5059 | goto free_ct_list; | |
5060 | } | |
5061 | ct_list_cnt++; | |
5062 | } | |
5063 | ||
5064 | if (cret) | |
5065 | *cret = ct_list; | |
5066 | ||
5067 | if (nret) | |
5068 | *nret = ct_name; | |
5069 | else { | |
5070 | ret = ct_cnt; | |
5071 | goto free_ct_name; | |
5072 | } | |
5073 | return ct_cnt; | |
5074 | ||
5075 | free_ct_list: | |
5076 | for (i = 0; i < ct_list_cnt; i++) { | |
5077 | lxc_container_put(ct_list[i]); | |
5078 | } | |
f10fad2f | 5079 | free(ct_list); |
2871830a DE |
5080 | |
5081 | free_active_name: | |
5082 | for (i = 0; i < active_cnt; i++) { | |
f10fad2f | 5083 | free(active_name[i]); |
2871830a | 5084 | } |
f10fad2f | 5085 | free(active_name); |
2871830a DE |
5086 | |
5087 | free_ct_name: | |
5088 | for (i = 0; i < ct_cnt; i++) { | |
5089 | free(ct_name[i]); | |
5090 | } | |
5091 | free(ct_name); | |
5092 | return ret; | |
5093 | } | |
12461428 CB |
5094 | |
5095 | bool lxc_config_item_is_supported(const char *key) | |
5096 | { | |
300df83e | 5097 | return !!lxc_get_config(key); |
12461428 | 5098 | } |