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