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