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