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