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