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