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