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