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