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