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