]>
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); | |
cbb9c7c7 DJ |
1195 | (void)strlcpy(p, path, len + 1); |
1196 | ||
78d44e5a CB |
1197 | if (!lxc_list_empty(&conf->id_map)) { |
1198 | ret = chown_mapped_root(p, conf); | |
1199 | if (ret < 0) | |
1200 | ret = -1; | |
d5752559 | 1201 | } |
78d44e5a | 1202 | |
d5752559 SH |
1203 | return ret; |
1204 | } | |
1205 | ||
dfa7eaeb | 1206 | /* Create the standard expected container dir. */ |
72d0e1cb SG |
1207 | static bool create_container_dir(struct lxc_container *c) |
1208 | { | |
dfa7eaeb CB |
1209 | int ret; |
1210 | size_t len; | |
72d0e1cb | 1211 | char *s; |
72d0e1cb | 1212 | |
2a59a681 | 1213 | len = strlen(c->config_path) + strlen(c->name) + 2; |
72d0e1cb SG |
1214 | s = malloc(len); |
1215 | if (!s) | |
1216 | return false; | |
dfa7eaeb | 1217 | |
2a59a681 | 1218 | ret = snprintf(s, len, "%s/%s", c->config_path, c->name); |
dfa7eaeb | 1219 | if (ret < 0 || (size_t)ret >= len) { |
72d0e1cb SG |
1220 | free(s); |
1221 | return false; | |
1222 | } | |
dfa7eaeb | 1223 | |
d5752559 | 1224 | ret = do_create_container_dir(s, c->lxc_conf); |
72d0e1cb | 1225 | free(s); |
dfa7eaeb | 1226 | |
72d0e1cb SG |
1227 | return ret == 0; |
1228 | } | |
1229 | ||
10bc1861 CB |
1230 | /* do_storage_create: thin wrapper around storage_create(). Like |
1231 | * storage_create(), it returns a mounted bdev on success, NULL on error. | |
72d0e1cb | 1232 | */ |
10bc1861 CB |
1233 | static struct lxc_storage *do_storage_create(struct lxc_container *c, |
1234 | const char *type, | |
1235 | struct bdev_specs *specs) | |
1897e3bc | 1236 | { |
e9e29a33 | 1237 | int ret; |
1897e3bc | 1238 | size_t len; |
e9e29a33 | 1239 | char *dest; |
10bc1861 | 1240 | struct lxc_storage *bdev; |
1897e3bc | 1241 | |
cd219ae6 | 1242 | /* rootfs.path or lxcpath/lxcname/rootfs */ |
e9e29a33 CB |
1243 | if (c->lxc_conf->rootfs.path && |
1244 | (access(c->lxc_conf->rootfs.path, F_OK) == 0)) { | |
cf465fe4 SH |
1245 | const char *rpath = c->lxc_conf->rootfs.path; |
1246 | len = strlen(rpath) + 1; | |
cd219ae6 | 1247 | dest = alloca(len); |
cf465fe4 | 1248 | ret = snprintf(dest, len, "%s", rpath); |
cd219ae6 | 1249 | } else { |
858377e4 | 1250 | const char *lxcpath = do_lxcapi_get_config_path(c); |
cd219ae6 SY |
1251 | len = strlen(c->name) + strlen(lxcpath) + 9; |
1252 | dest = alloca(len); | |
1253 | ret = snprintf(dest, len, "%s/%s/rootfs", lxcpath, c->name); | |
1254 | } | |
e9e29a33 | 1255 | if (ret < 0 || (size_t)ret >= len) |
1897e3bc SH |
1256 | return NULL; |
1257 | ||
10bc1861 | 1258 | bdev = storage_create(dest, type, c->name, specs); |
d44e88c2 | 1259 | if (!bdev) { |
e9e29a33 | 1260 | ERROR("Failed to create \"%s\" storage", type); |
1897e3bc | 1261 | return NULL; |
d44e88c2 SH |
1262 | } |
1263 | ||
7a96a068 | 1264 | if (!c->set_config_item(c, "lxc.rootfs.path", bdev->src)) { |
e9e29a33 | 1265 | ERROR("Failed to set \"lxc.rootfs.path = %s\"", bdev->src); |
f7ac4459 CB |
1266 | return NULL; |
1267 | } | |
cf3ef16d | 1268 | |
e9e29a33 CB |
1269 | /* If we are not root, chown the rootfs dir to root in the target user |
1270 | * namespace. | |
f7ac4459 | 1271 | */ |
e9e29a33 CB |
1272 | ret = geteuid(); |
1273 | if (ret != 0 || (c->lxc_conf && !lxc_list_empty(&c->lxc_conf->id_map))) { | |
1274 | ret = chown_mapped_root(bdev->dest, c->lxc_conf); | |
1275 | if (ret < 0) { | |
1276 | ERROR("Error chowning \"%s\" to container root", bdev->dest); | |
97e9cfa0 | 1277 | suggest_default_idmap(); |
10bc1861 | 1278 | storage_put(bdev); |
cf3ef16d SH |
1279 | return NULL; |
1280 | } | |
1281 | } | |
1282 | ||
1897e3bc SH |
1283 | return bdev; |
1284 | } | |
1285 | ||
96b3cb40 | 1286 | static char *lxcbasename(char *path) |
72d0e1cb | 1287 | { |
47e55887 CB |
1288 | char *p; |
1289 | ||
1290 | p = path + strlen(path) - 1; | |
96b3cb40 SH |
1291 | while (*p != '/' && p > path) |
1292 | p--; | |
47e55887 | 1293 | |
96b3cb40 SH |
1294 | return p; |
1295 | } | |
72d0e1cb | 1296 | |
47e55887 CB |
1297 | static bool create_run_template(struct lxc_container *c, char *tpath, |
1298 | bool need_null_stdfds, char *const argv[]) | |
96b3cb40 | 1299 | { |
47e55887 | 1300 | int ret; |
96b3cb40 | 1301 | pid_t pid; |
72d0e1cb | 1302 | |
72d0e1cb | 1303 | if (!tpath) |
96b3cb40 | 1304 | return true; |
72d0e1cb SG |
1305 | |
1306 | pid = fork(); | |
1307 | if (pid < 0) { | |
7e34710e | 1308 | SYSERROR("Failed to fork task for container creation template"); |
96b3cb40 | 1309 | return false; |
72d0e1cb SG |
1310 | } |
1311 | ||
1a0e70ac | 1312 | if (pid == 0) { /* child */ |
47e55887 CB |
1313 | int i, len; |
1314 | char *namearg, *patharg, *rootfsarg; | |
96b3cb40 | 1315 | char **newargv; |
47e55887 CB |
1316 | int nargs = 0; |
1317 | struct lxc_storage *bdev = NULL; | |
cf3ef16d | 1318 | struct lxc_conf *conf = c->lxc_conf; |
47e55887 | 1319 | uid_t euid; |
72d0e1cb | 1320 | |
47e55887 CB |
1321 | if (need_null_stdfds) { |
1322 | ret = null_stdfds(); | |
1323 | if (ret < 0) | |
1324 | _exit(EXIT_FAILURE); | |
dc23c1c8 | 1325 | } |
1897e3bc | 1326 | |
8a388ed4 | 1327 | bdev = storage_init(c->lxc_conf); |
1897e3bc | 1328 | if (!bdev) { |
47e55887 | 1329 | ERROR("Failed to initialize storage"); |
7e34710e | 1330 | _exit(EXIT_FAILURE); |
1897e3bc SH |
1331 | } |
1332 | ||
47e55887 CB |
1333 | euid = geteuid(); |
1334 | if (euid == 0) { | |
1335 | ret = unshare(CLONE_NEWNS); | |
1336 | if (ret < 0) { | |
1337 | ERROR("Failed to unshare CLONE_NEWNS"); | |
7e34710e | 1338 | _exit(EXIT_FAILURE); |
cf3ef16d | 1339 | } |
47e55887 CB |
1340 | |
1341 | ret = detect_shared_rootfs(); | |
1342 | if (ret == 1) { | |
1343 | ret = mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL); | |
1344 | if (ret < 0) { | |
1345 | SYSERROR("Failed to make \"/\" rslave"); | |
4de2791f SH |
1346 | ERROR("Continuing..."); |
1347 | } | |
1348 | } | |
1349 | } | |
47e55887 CB |
1350 | |
1351 | if (strcmp(bdev->type, "dir") != 0 && strcmp(bdev->type, "btrfs") != 0) { | |
1352 | if (euid != 0) { | |
1353 | ERROR("Unprivileged users can only create " | |
1354 | "btrfs and directory-backed containers"); | |
eb70aaf0 | 1355 | _exit(EXIT_FAILURE); |
4de2791f | 1356 | } |
241978fa | 1357 | |
47e55887 CB |
1358 | if (strcmp(bdev->type, "overlay") == 0 || |
1359 | strcmp(bdev->type, "overlayfs") == 0) { | |
241978fa CB |
1360 | /* If we create an overlay container we need to |
1361 | * rsync the contents into | |
1362 | * <container-path>/<container-name>/rootfs. | |
1363 | * However, the overlay mount function will | |
1364 | * mount will mount | |
1365 | * <container-path>/<container-name>/delta0 | |
1366 | * over | |
1367 | * <container-path>/<container-name>/rootfs | |
1368 | * which means we would rsync the rootfs into | |
1369 | * the delta directory. That doesn't make sense | |
1370 | * since the delta directory only exists to | |
1371 | * record the differences to | |
1372 | * <container-path>/<container-name>/rootfs. So | |
1373 | * let's simply bind-mount here and then rsync | |
1374 | * directly into | |
1375 | * <container-path>/<container-name>/rootfs. | |
1376 | */ | |
1377 | char *src; | |
1378 | ||
1379 | src = ovl_get_rootfs(bdev->src, &(size_t){0}); | |
1380 | if (!src) { | |
1381 | ERROR("Failed to get rootfs"); | |
eb70aaf0 | 1382 | _exit(EXIT_FAILURE); |
241978fa CB |
1383 | } |
1384 | ||
1385 | ret = mount(src, bdev->dest, "bind", MS_BIND | MS_REC, NULL); | |
1386 | if (ret < 0) { | |
1387 | ERROR("Failed to mount rootfs"); | |
8f55c742 | 1388 | _exit(EXIT_FAILURE); |
241978fa CB |
1389 | } |
1390 | } else { | |
47e55887 CB |
1391 | ret = bdev->ops->mount(bdev); |
1392 | if (ret < 0) { | |
241978fa | 1393 | ERROR("Failed to mount rootfs"); |
eb70aaf0 | 1394 | _exit(EXIT_FAILURE); |
241978fa | 1395 | } |
cf3ef16d | 1396 | } |
1a0e70ac | 1397 | } else { /* TODO come up with a better way here! */ |
41dc7155 | 1398 | const char *src; |
f10fad2f | 1399 | free(bdev->dest); |
e9705550 CB |
1400 | src = lxc_storage_get_path(bdev->src, bdev->type); |
1401 | bdev->dest = strdup(src); | |
1897e3bc SH |
1402 | } |
1403 | ||
47e55887 CB |
1404 | /* Create our new array, pre-pend the template name and base |
1405 | * args. | |
72d0e1cb SG |
1406 | */ |
1407 | if (argv) | |
47e55887 CB |
1408 | for (nargs = 0; argv[nargs]; nargs++) { |
1409 | ; | |
1410 | } | |
1411 | /* template, path, rootfs and name args */ | |
1412 | nargs += 4; | |
cf3ef16d | 1413 | |
72d0e1cb SG |
1414 | newargv = malloc(nargs * sizeof(*newargv)); |
1415 | if (!newargv) | |
7e34710e | 1416 | _exit(EXIT_FAILURE); |
96b3cb40 | 1417 | newargv[0] = lxcbasename(tpath); |
72d0e1cb | 1418 | |
47e55887 | 1419 | /* --path */ |
2a59a681 | 1420 | len = strlen(c->config_path) + strlen(c->name) + strlen("--path=") + 2; |
72d0e1cb SG |
1421 | patharg = malloc(len); |
1422 | if (!patharg) | |
7e34710e | 1423 | _exit(EXIT_FAILURE); |
2a59a681 | 1424 | ret = snprintf(patharg, len, "--path=%s/%s", c->config_path, c->name); |
72d0e1cb | 1425 | if (ret < 0 || ret >= len) |
7e34710e | 1426 | _exit(EXIT_FAILURE); |
72d0e1cb | 1427 | newargv[1] = patharg; |
47e55887 CB |
1428 | |
1429 | /* --name */ | |
72d0e1cb SG |
1430 | len = strlen("--name=") + strlen(c->name) + 1; |
1431 | namearg = malloc(len); | |
1432 | if (!namearg) | |
7e34710e | 1433 | _exit(EXIT_FAILURE); |
72d0e1cb SG |
1434 | ret = snprintf(namearg, len, "--name=%s", c->name); |
1435 | if (ret < 0 || ret >= len) | |
7e34710e | 1436 | _exit(EXIT_FAILURE); |
72d0e1cb SG |
1437 | newargv[2] = namearg; |
1438 | ||
47e55887 | 1439 | /* --rootfs */ |
1897e3bc SH |
1440 | len = strlen("--rootfs=") + 1 + strlen(bdev->dest); |
1441 | rootfsarg = malloc(len); | |
1442 | if (!rootfsarg) | |
7e34710e | 1443 | _exit(EXIT_FAILURE); |
1897e3bc SH |
1444 | ret = snprintf(rootfsarg, len, "--rootfs=%s", bdev->dest); |
1445 | if (ret < 0 || ret >= len) | |
7e34710e | 1446 | _exit(EXIT_FAILURE); |
1897e3bc SH |
1447 | newargv[3] = rootfsarg; |
1448 | ||
72d0e1cb SG |
1449 | /* add passed-in args */ |
1450 | if (argv) | |
1897e3bc | 1451 | for (i = 4; i < nargs; i++) |
47e55887 | 1452 | newargv[i] = argv[i - 4]; |
72d0e1cb SG |
1453 | |
1454 | /* add trailing NULL */ | |
1455 | nargs++; | |
1456 | newargv = realloc(newargv, nargs * sizeof(*newargv)); | |
1457 | if (!newargv) | |
7e34710e | 1458 | _exit(EXIT_FAILURE); |
72d0e1cb SG |
1459 | newargv[nargs - 1] = NULL; |
1460 | ||
47e55887 CB |
1461 | /* If we're running the template in a mapped userns, then we |
1462 | * prepend the template command with: lxc-usernsexec <-m map1> | |
1463 | * ... <-m mapn> -- and we append "--mapped-uid x", where x is | |
1464 | * the mapped uid for our geteuid() | |
cf3ef16d | 1465 | */ |
0e6e3a41 | 1466 | if (!lxc_list_empty(&conf->id_map)) { |
47e55887 CB |
1467 | int extraargs, hostuid_mapped, hostgid_mapped; |
1468 | char **n2; | |
1469 | char txtuid[20], txtgid[20]; | |
cf3ef16d SH |
1470 | struct lxc_list *it; |
1471 | struct id_map *map; | |
47e55887 | 1472 | int n2args = 1; |
cf3ef16d | 1473 | |
47e55887 CB |
1474 | n2 = malloc(n2args * sizeof(*n2)); |
1475 | if (!n2) | |
7e34710e | 1476 | _exit(EXIT_FAILURE); |
47e55887 | 1477 | |
cf3ef16d SH |
1478 | newargv[0] = tpath; |
1479 | tpath = "lxc-usernsexec"; | |
1480 | n2[0] = "lxc-usernsexec"; | |
1481 | lxc_list_for_each(it, &conf->id_map) { | |
1482 | map = it->elem; | |
1483 | n2args += 2; | |
57d116ab | 1484 | n2 = realloc(n2, n2args * sizeof(char *)); |
cf3ef16d | 1485 | if (!n2) |
7e34710e | 1486 | _exit(EXIT_FAILURE); |
47e55887 CB |
1487 | |
1488 | n2[n2args - 2] = "-m"; | |
1489 | n2[n2args - 1] = malloc(200); | |
1490 | if (!n2[n2args - 1]) | |
7e34710e | 1491 | _exit(EXIT_FAILURE); |
47e55887 CB |
1492 | |
1493 | ret = snprintf(n2[n2args - 1], 200, "%c:%lu:%lu:%lu", | |
1494 | map->idtype == ID_TYPE_UID ? 'u' : 'g', | |
1495 | map->nsid, map->hostid, map->range); | |
cf3ef16d | 1496 | if (ret < 0 || ret >= 200) |
7e34710e | 1497 | _exit(EXIT_FAILURE); |
cf3ef16d | 1498 | } |
47e55887 CB |
1499 | |
1500 | hostuid_mapped = mapped_hostid(geteuid(), conf, ID_TYPE_UID); | |
1501 | extraargs = hostuid_mapped >= 0 ? 1 : 3; | |
57d116ab | 1502 | n2 = realloc(n2, (nargs + n2args + extraargs) * sizeof(char *)); |
cf3ef16d | 1503 | if (!n2) |
7e34710e | 1504 | _exit(EXIT_FAILURE); |
47e55887 CB |
1505 | |
1506 | if (hostuid_mapped < 0) { | |
1507 | hostuid_mapped = find_unmapped_nsid(conf, ID_TYPE_UID); | |
cf3ef16d | 1508 | n2[n2args++] = "-m"; |
47e55887 CB |
1509 | if (hostuid_mapped < 0) { |
1510 | ERROR("Failed to find free uid to map"); | |
7e34710e | 1511 | _exit(EXIT_FAILURE); |
cf3ef16d | 1512 | } |
47e55887 | 1513 | |
cf3ef16d | 1514 | n2[n2args++] = malloc(200); |
47e55887 | 1515 | if (!n2[n2args - 1]) { |
cf3ef16d | 1516 | SYSERROR("out of memory"); |
7e34710e | 1517 | _exit(EXIT_FAILURE); |
cf3ef16d | 1518 | } |
47e55887 CB |
1519 | ret = snprintf(n2[n2args - 1], 200, "u:%d:%d:1", |
1520 | hostuid_mapped, geteuid()); | |
1521 | if (ret < 0 || ret >= 200) | |
7e34710e | 1522 | _exit(EXIT_FAILURE); |
cf3ef16d | 1523 | } |
47e55887 CB |
1524 | |
1525 | hostgid_mapped = mapped_hostid(getegid(), conf, ID_TYPE_GID); | |
2133f58c SH |
1526 | extraargs = hostgid_mapped >= 0 ? 1 : 3; |
1527 | n2 = realloc(n2, (nargs + n2args + extraargs) * sizeof(char *)); | |
1528 | if (!n2) | |
7e34710e | 1529 | _exit(EXIT_FAILURE); |
47e55887 | 1530 | |
2133f58c | 1531 | if (hostgid_mapped < 0) { |
339efad9 | 1532 | hostgid_mapped = find_unmapped_nsid(conf, ID_TYPE_GID); |
2133f58c SH |
1533 | n2[n2args++] = "-m"; |
1534 | if (hostgid_mapped < 0) { | |
47e55887 | 1535 | ERROR("Failed to find free gid to map"); |
7e34710e | 1536 | _exit(EXIT_FAILURE); |
2133f58c | 1537 | } |
47e55887 | 1538 | |
2133f58c | 1539 | n2[n2args++] = malloc(200); |
47e55887 | 1540 | if (!n2[n2args - 1]) { |
2133f58c | 1541 | SYSERROR("out of memory"); |
7e34710e | 1542 | _exit(EXIT_FAILURE); |
2133f58c | 1543 | } |
47e55887 CB |
1544 | |
1545 | ret = snprintf(n2[n2args - 1], 200, "g:%d:%d:1", | |
1546 | hostgid_mapped, getegid()); | |
1547 | if (ret < 0 || ret >= 200) | |
7e34710e | 1548 | _exit(EXIT_FAILURE); |
2133f58c | 1549 | } |
cf3ef16d SH |
1550 | n2[n2args++] = "--"; |
1551 | for (i = 0; i < nargs; i++) | |
1552 | n2[i + n2args] = newargv[i]; | |
57d116ab | 1553 | n2args += nargs; |
47e55887 CB |
1554 | |
1555 | /* Finally add "--mapped-uid $uid" to tell template what | |
1556 | * to chown cached images to. | |
1a0e70ac | 1557 | */ |
2133f58c | 1558 | n2args += 4; |
57d116ab | 1559 | n2 = realloc(n2, n2args * sizeof(char *)); |
47e55887 | 1560 | if (!n2) |
7e34710e | 1561 | _exit(EXIT_FAILURE); |
47e55887 | 1562 | |
1a0e70ac | 1563 | /* note n2[n2args-1] is NULL */ |
47e55887 | 1564 | n2[n2args - 5] = "--mapped-uid"; |
4250ef64 CB |
1565 | |
1566 | ret = snprintf(txtuid, 20, "%d", hostuid_mapped); | |
1567 | if (ret < 0 || ret >= 20) { | |
1568 | free(newargv); | |
1569 | free(n2); | |
1570 | _exit(EXIT_FAILURE); | |
1571 | } | |
1572 | ||
47e55887 CB |
1573 | n2[n2args - 4] = txtuid; |
1574 | n2[n2args - 3] = "--mapped-gid"; | |
4250ef64 | 1575 | |
1f080b1d CB |
1576 | ret = snprintf(txtgid, 20, "%d", hostgid_mapped); |
1577 | if (ret < 0 || ret >= 20) { | |
1578 | free(newargv); | |
1579 | free(n2); | |
1580 | _exit(EXIT_FAILURE); | |
1581 | } | |
4250ef64 | 1582 | |
47e55887 CB |
1583 | n2[n2args - 2] = txtgid; |
1584 | n2[n2args - 1] = NULL; | |
cf3ef16d SH |
1585 | free(newargv); |
1586 | newargv = n2; | |
1587 | } | |
47e55887 | 1588 | |
cf3ef16d | 1589 | execvp(tpath, newargv); |
e9705550 | 1590 | SYSERROR("Failed to execute template %s", tpath); |
7e34710e | 1591 | _exit(EXIT_FAILURE); |
72d0e1cb SG |
1592 | } |
1593 | ||
47e55887 CB |
1594 | ret = wait_for_pid(pid); |
1595 | if (ret != 0) { | |
1596 | ERROR("Failed to create container from template"); | |
96b3cb40 SH |
1597 | return false; |
1598 | } | |
1599 | ||
1600 | return true; | |
1601 | } | |
1602 | ||
74a3920a | 1603 | static bool prepend_lxc_header(char *path, const char *t, char *const argv[]) |
3ce74686 | 1604 | { |
1fd9bd50 | 1605 | long flen; |
630ac7c6 | 1606 | size_t len; |
b4569e93 | 1607 | char *contents; |
3ce74686 | 1608 | FILE *f; |
025ed0f3 | 1609 | int ret = -1; |
52026772 | 1610 | #if HAVE_LIBGNUTLS |
025ed0f3 | 1611 | int i; |
3ce74686 | 1612 | unsigned char md_value[SHA_DIGEST_LENGTH]; |
b4569e93 | 1613 | char *tpath; |
52026772 | 1614 | #endif |
3ce74686 | 1615 | |
025ed0f3 | 1616 | f = fopen(path, "r"); |
025ed0f3 | 1617 | if (f == NULL) |
3ce74686 | 1618 | return false; |
025ed0f3 | 1619 | |
630ac7c6 CB |
1620 | ret = fseek(f, 0, SEEK_END); |
1621 | if (ret < 0) | |
025ed0f3 | 1622 | goto out_error; |
630ac7c6 CB |
1623 | |
1624 | ret = -1; | |
1625 | flen = ftell(f); | |
1626 | if (flen < 0) | |
025ed0f3 | 1627 | goto out_error; |
630ac7c6 CB |
1628 | |
1629 | ret = fseek(f, 0, SEEK_SET); | |
1630 | if (ret < 0) | |
025ed0f3 | 1631 | goto out_error; |
630ac7c6 CB |
1632 | |
1633 | ret = fseek(f, 0, SEEK_SET); | |
1634 | if (ret < 0) | |
1635 | goto out_error; | |
1636 | ||
1637 | ret = -1; | |
1638 | contents = malloc(flen + 1); | |
1639 | if (!contents) | |
025ed0f3 | 1640 | goto out_error; |
630ac7c6 CB |
1641 | |
1642 | len = fread(contents, 1, flen, f); | |
1643 | if (len != flen) | |
025ed0f3 SH |
1644 | goto out_free_contents; |
1645 | ||
3ce74686 | 1646 | contents[flen] = '\0'; |
025ed0f3 | 1647 | ret = fclose(f); |
025ed0f3 SH |
1648 | f = NULL; |
1649 | if (ret < 0) | |
1650 | goto out_free_contents; | |
3ce74686 | 1651 | |
b4569e93 | 1652 | #if HAVE_LIBGNUTLS |
01efd4d3 | 1653 | tpath = get_template_path(t); |
85db5535 | 1654 | if (!tpath) { |
630ac7c6 | 1655 | ERROR("Invalid template \"%s\" specified", t); |
025ed0f3 | 1656 | goto out_free_contents; |
3ce74686 SH |
1657 | } |
1658 | ||
85db5535 DE |
1659 | ret = sha1sum_file(tpath, md_value); |
1660 | if (ret < 0) { | |
630ac7c6 | 1661 | ERROR("Failed to get sha1sum of %s", tpath); |
cef701ed | 1662 | free(tpath); |
85db5535 | 1663 | goto out_free_contents; |
3ce74686 | 1664 | } |
cef701ed | 1665 | free(tpath); |
3ce74686 SH |
1666 | #endif |
1667 | ||
025ed0f3 | 1668 | f = fopen(path, "w"); |
025ed0f3 | 1669 | if (f == NULL) { |
630ac7c6 | 1670 | SYSERROR("Reopening config for writing"); |
3ce74686 SH |
1671 | free(contents); |
1672 | return false; | |
1673 | } | |
630ac7c6 | 1674 | |
3ce74686 SH |
1675 | fprintf(f, "# Template used to create this container: %s\n", t); |
1676 | if (argv) { | |
1677 | fprintf(f, "# Parameters passed to the template:"); | |
1678 | while (*argv) { | |
1679 | fprintf(f, " %s", *argv); | |
1680 | argv++; | |
1681 | } | |
1682 | fprintf(f, "\n"); | |
1683 | } | |
1684 | #if HAVE_LIBGNUTLS | |
56698177 SH |
1685 | fprintf(f, "# Template script checksum (SHA-1): "); |
1686 | for (i=0; i<SHA_DIGEST_LENGTH; i++) | |
1687 | fprintf(f, "%02x", md_value[i]); | |
1688 | fprintf(f, "\n"); | |
3ce74686 | 1689 | #endif |
0520c252 | 1690 | fprintf(f, "# For additional config options, please look at lxc.container.conf(5)\n"); |
49a2ed80 SH |
1691 | fprintf(f, "\n# Uncomment the following line to support nesting containers:\n"); |
1692 | fprintf(f, "#lxc.include = " LXCTEMPLATECONFIG "/nesting.conf\n"); | |
1693 | fprintf(f, "# (Be aware this has security implications)\n\n"); | |
3ce74686 SH |
1694 | if (fwrite(contents, 1, flen, f) != flen) { |
1695 | SYSERROR("Writing original contents"); | |
1696 | free(contents); | |
1697 | fclose(f); | |
1698 | return false; | |
1699 | } | |
630ac7c6 | 1700 | |
025ed0f3 | 1701 | ret = 0; |
630ac7c6 | 1702 | |
025ed0f3 | 1703 | out_free_contents: |
3ce74686 | 1704 | free(contents); |
630ac7c6 | 1705 | |
025ed0f3 SH |
1706 | out_error: |
1707 | if (f) { | |
1708 | int newret; | |
025ed0f3 | 1709 | newret = fclose(f); |
025ed0f3 SH |
1710 | if (ret == 0) |
1711 | ret = newret; | |
1712 | } | |
630ac7c6 | 1713 | |
025ed0f3 SH |
1714 | if (ret < 0) { |
1715 | SYSERROR("Error prepending header"); | |
3ce74686 SH |
1716 | return false; |
1717 | } | |
630ac7c6 | 1718 | |
3ce74686 SH |
1719 | return true; |
1720 | } | |
1721 | ||
4df7f012 SH |
1722 | static void lxcapi_clear_config(struct lxc_container *c) |
1723 | { | |
e62fd16f CB |
1724 | if (!c || !c->lxc_conf) |
1725 | return; | |
1726 | ||
1727 | lxc_conf_free(c->lxc_conf); | |
1728 | c->lxc_conf = NULL; | |
4df7f012 SH |
1729 | } |
1730 | ||
858377e4 SH |
1731 | #define do_lxcapi_clear_config(c) lxcapi_clear_config(c) |
1732 | ||
96b3cb40 SH |
1733 | /* |
1734 | * lxcapi_create: | |
1735 | * create a container with the given parameters. | |
1736 | * @c: container to be created. It has the lxcpath, name, and a starting | |
1737 | * configuration already set | |
1738 | * @t: the template to execute to instantiate the root filesystem and | |
1739 | * adjust the configuration. | |
1740 | * @bdevtype: backing store type to use. If NULL, dir will be used. | |
1741 | * @specs: additional parameters for the backing store, i.e. LVM vg to | |
1742 | * use. | |
1743 | * | |
1744 | * @argv: the arguments to pass to the template, terminated by NULL. If no | |
1745 | * arguments, you can just pass NULL. | |
1746 | */ | |
858377e4 | 1747 | static bool do_lxcapi_create(struct lxc_container *c, const char *t, |
85aec4ac CB |
1748 | const char *bdevtype, struct bdev_specs *specs, |
1749 | int flags, char *const argv[]) | |
96b3cb40 | 1750 | { |
e9e29a33 | 1751 | int partial_fd; |
51f0f73b | 1752 | mode_t mask; |
96b3cb40 | 1753 | pid_t pid; |
e9e29a33 | 1754 | bool ret = false; |
85db5535 | 1755 | char *tpath = NULL; |
96b3cb40 SH |
1756 | |
1757 | if (!c) | |
1758 | return false; | |
1759 | ||
85db5535 DE |
1760 | if (t) { |
1761 | tpath = get_template_path(t); | |
1762 | if (!tpath) { | |
e9e29a33 | 1763 | ERROR("Unknown template \"%s\"", t); |
85db5535 DE |
1764 | goto out; |
1765 | } | |
96b3cb40 SH |
1766 | } |
1767 | ||
e9e29a33 CB |
1768 | /* If a template is passed in, and the rootfs already is defined in the |
1769 | * container config and exists, then the caller is trying to create an | |
1770 | * existing container. Return an error, but do NOT delete the container. | |
cf465fe4 | 1771 | */ |
858377e4 | 1772 | if (do_lxcapi_is_defined(c) && c->lxc_conf && c->lxc_conf->rootfs.path && |
cf465fe4 | 1773 | access(c->lxc_conf->rootfs.path, F_OK) == 0 && tpath) { |
e9e29a33 CB |
1774 | ERROR("Container \"%s\" already exists in \"%s\"", c->name, |
1775 | c->config_path); | |
6c6892b5 | 1776 | goto free_tpath; |
cf465fe4 SH |
1777 | } |
1778 | ||
6c6892b5 | 1779 | if (!c->lxc_conf) { |
858377e4 | 1780 | if (!do_lxcapi_load_config(c, lxc_global_config_value("lxc.default_config"))) { |
e9e29a33 CB |
1781 | ERROR("Error loading default configuration file %s", |
1782 | lxc_global_config_value("lxc.default_config")); | |
6c6892b5 DE |
1783 | goto free_tpath; |
1784 | } | |
96b3cb40 SH |
1785 | } |
1786 | ||
6c6892b5 DE |
1787 | if (!create_container_dir(c)) |
1788 | goto free_tpath; | |
1789 | ||
e9e29a33 CB |
1790 | /* If both template and rootfs.path are set, template is setup as |
1791 | * rootfs.path. The container is already created if we have a config and | |
1792 | * rootfs.path is accessible | |
0590e82c | 1793 | */ |
00370edd | 1794 | if (!c->lxc_conf->rootfs.path && !tpath) { |
e9e29a33 | 1795 | /* No template passed in and rootfs does not exist. */ |
00370edd | 1796 | if (!c->save_config(c, NULL)) { |
e9e29a33 | 1797 | ERROR("Failed to save initial config for \"%s\"", c->name); |
00370edd DW |
1798 | goto out; |
1799 | } | |
1800 | ret = true; | |
0590e82c | 1801 | goto out; |
00370edd | 1802 | } |
e9e29a33 CB |
1803 | |
1804 | /* Rootfs passed into configuration, but does not exist. */ | |
0590e82c | 1805 | if (c->lxc_conf->rootfs.path && access(c->lxc_conf->rootfs.path, F_OK) != 0) |
0590e82c | 1806 | goto out; |
e9e29a33 | 1807 | |
858377e4 | 1808 | if (do_lxcapi_is_defined(c) && c->lxc_conf->rootfs.path && !tpath) { |
e9e29a33 CB |
1809 | /* Rootfs already existed, user just wanted to save the loaded |
1810 | * configuration. | |
1811 | */ | |
0f4cdd77 | 1812 | if (!c->save_config(c, NULL)) |
e9e29a33 | 1813 | ERROR("Failed to save initial config for \"%s\"", c->name); |
0590e82c SH |
1814 | ret = true; |
1815 | goto out; | |
a69aad27 | 1816 | } |
96b3cb40 SH |
1817 | |
1818 | /* Mark that this container is being created */ | |
e9e29a33 CB |
1819 | partial_fd = create_partial(c); |
1820 | if (partial_fd < 0) | |
96b3cb40 SH |
1821 | goto out; |
1822 | ||
e9e29a33 | 1823 | /* No need to get disk lock bc we have the partial lock. */ |
96b3cb40 | 1824 | |
51f0f73b KR |
1825 | mask = umask(0022); |
1826 | ||
e9e29a33 | 1827 | /* Create the storage. |
96b3cb40 SH |
1828 | * Note we can't do this in the same task as we use to execute the |
1829 | * template because of the way zfs works. | |
1830 | * After you 'zfs create', zfs mounts the fs only in the initial | |
1831 | * namespace. | |
1832 | */ | |
1833 | pid = fork(); | |
1834 | if (pid < 0) { | |
e9e29a33 | 1835 | SYSERROR("Failed to fork task for container creation template"); |
8eb5694b SH |
1836 | goto out_unlock; |
1837 | } | |
1838 | ||
1a0e70ac | 1839 | if (pid == 0) { /* child */ |
10bc1861 | 1840 | struct lxc_storage *bdev = NULL; |
96b3cb40 | 1841 | |
10bc1861 CB |
1842 | bdev = do_storage_create(c, bdevtype, specs); |
1843 | if (!bdev) { | |
e9e29a33 CB |
1844 | ERROR("Failed to create %s storage for %s", |
1845 | bdevtype ? bdevtype : "(none)", c->name); | |
85aec4ac | 1846 | _exit(EXIT_FAILURE); |
96b3cb40 SH |
1847 | } |
1848 | ||
e9e29a33 | 1849 | /* Save config file again to store the new rootfs location. */ |
858377e4 | 1850 | if (!do_lxcapi_save_config(c, NULL)) { |
e9e29a33 CB |
1851 | ERROR("Failed to save initial config for %s", c->name); |
1852 | /* Parent task won't see the storage driver in the | |
1853 | * config so we delete it. | |
1854 | */ | |
96b3cb40 SH |
1855 | bdev->ops->umount(bdev); |
1856 | bdev->ops->destroy(bdev); | |
85aec4ac | 1857 | _exit(EXIT_FAILURE); |
96b3cb40 | 1858 | } |
85aec4ac | 1859 | _exit(EXIT_SUCCESS); |
96b3cb40 SH |
1860 | } |
1861 | if (wait_for_pid(pid) != 0) | |
a09295f8 | 1862 | goto out_unlock; |
96b3cb40 | 1863 | |
e9e29a33 | 1864 | /* Reload config to get the rootfs. */ |
a3b47c09 | 1865 | lxc_conf_free(c->lxc_conf); |
96b3cb40 SH |
1866 | c->lxc_conf = NULL; |
1867 | if (!load_config_locked(c, c->configfile)) | |
a09295f8 | 1868 | goto out_unlock; |
96b3cb40 | 1869 | |
dc23c1c8 | 1870 | if (!create_run_template(c, tpath, !!(flags & LXC_CREATE_QUIET), argv)) |
96b3cb40 SH |
1871 | goto out_unlock; |
1872 | ||
1a0e70ac CB |
1873 | /* Now clear out the lxc_conf we have, reload from the created |
1874 | * container. | |
1875 | */ | |
858377e4 | 1876 | do_lxcapi_clear_config(c); |
3ce74686 | 1877 | |
9d65a487 KY |
1878 | if (t) { |
1879 | if (!prepend_lxc_header(c->configfile, tpath, argv)) { | |
e9e29a33 | 1880 | ERROR("Failed to prepend header to config file"); |
9d65a487 KY |
1881 | goto out_unlock; |
1882 | } | |
3ce74686 | 1883 | } |
a69aad27 | 1884 | ret = load_config_locked(c, c->configfile); |
72d0e1cb SG |
1885 | |
1886 | out_unlock: | |
51f0f73b | 1887 | umask(mask); |
3e625e2d SH |
1888 | if (partial_fd >= 0) |
1889 | remove_partial(c, partial_fd); | |
72d0e1cb | 1890 | out: |
c55d4505 | 1891 | if (!ret) |
17a367d8 | 1892 | container_destroy(c, NULL); |
6c6892b5 | 1893 | free_tpath: |
f10fad2f | 1894 | free(tpath); |
a69aad27 | 1895 | return ret; |
72d0e1cb SG |
1896 | } |
1897 | ||
858377e4 | 1898 | static bool lxcapi_create(struct lxc_container *c, const char *t, |
e9e29a33 CB |
1899 | const char *bdevtype, struct bdev_specs *specs, |
1900 | int flags, char *const argv[]) | |
858377e4 SH |
1901 | { |
1902 | bool ret; | |
1903 | current_config = c ? c->lxc_conf : NULL; | |
1904 | ret = do_lxcapi_create(c, t, bdevtype, specs, flags, argv); | |
1905 | current_config = NULL; | |
1906 | return ret; | |
1907 | } | |
1908 | ||
1909 | static bool do_lxcapi_reboot(struct lxc_container *c) | |
3e625e2d | 1910 | { |
9dd54153 | 1911 | int ret; |
3e625e2d | 1912 | pid_t pid; |
dd267776 | 1913 | int rebootsignal = SIGINT; |
3e625e2d SH |
1914 | |
1915 | if (!c) | |
1916 | return false; | |
9dd54153 | 1917 | |
858377e4 | 1918 | if (!do_lxcapi_is_running(c)) |
3e625e2d | 1919 | return false; |
9dd54153 | 1920 | |
858377e4 | 1921 | pid = do_lxcapi_init_pid(c); |
3e625e2d SH |
1922 | if (pid <= 0) |
1923 | return false; | |
9dd54153 | 1924 | |
dd267776 BP |
1925 | if (c->lxc_conf && c->lxc_conf->rebootsignal) |
1926 | rebootsignal = c->lxc_conf->rebootsignal; | |
9dd54153 CB |
1927 | |
1928 | ret = kill(pid, rebootsignal); | |
1929 | if (ret < 0) { | |
1930 | WARN("Failed to send signal %d to pid %d", rebootsignal, pid); | |
3e625e2d | 1931 | return false; |
591614a7 | 1932 | } |
3e625e2d | 1933 | |
9dd54153 | 1934 | return true; |
3e625e2d SH |
1935 | } |
1936 | ||
858377e4 SH |
1937 | WRAP_API(bool, lxcapi_reboot) |
1938 | ||
d39b10eb CB |
1939 | static bool do_lxcapi_reboot2(struct lxc_container *c, int timeout) |
1940 | { | |
1941 | int killret, ret; | |
1942 | pid_t pid; | |
1943 | int rebootsignal = SIGINT, state_client_fd = -1; | |
1944 | lxc_state_t states[MAX_STATE] = {0}; | |
1945 | ||
1946 | if (!c) | |
1947 | return false; | |
1948 | ||
1949 | if (!do_lxcapi_is_running(c)) | |
1950 | return true; | |
1951 | ||
1952 | pid = do_lxcapi_init_pid(c); | |
1953 | if (pid <= 0) | |
1954 | return true; | |
1955 | ||
1956 | if (c->lxc_conf && c->lxc_conf->rebootsignal) | |
1957 | rebootsignal = c->lxc_conf->rebootsignal; | |
1958 | ||
1959 | /* Add a new state client before sending the shutdown signal so that we | |
1960 | * don't miss a state. | |
1961 | */ | |
1962 | if (timeout != 0) { | |
1963 | states[RUNNING] = 2; | |
1964 | ret = lxc_cmd_add_state_client(c->name, c->config_path, states, | |
1965 | &state_client_fd); | |
1966 | if (ret < 0) | |
1967 | return false; | |
1968 | ||
1969 | if (state_client_fd < 0) | |
1970 | return false; | |
1971 | ||
1972 | if (ret == RUNNING) | |
1973 | return true; | |
1974 | ||
1975 | if (ret < MAX_STATE) | |
1976 | return false; | |
1977 | } | |
1978 | ||
1979 | /* Send reboot signal to container. */ | |
1980 | killret = kill(pid, rebootsignal); | |
1981 | if (killret < 0) { | |
d39b10eb CB |
1982 | if (state_client_fd >= 0) |
1983 | close(state_client_fd); | |
9dd54153 | 1984 | WARN("Failed to send signal %d to pid %d", rebootsignal, pid); |
d39b10eb CB |
1985 | return false; |
1986 | } | |
1987 | TRACE("Sent signal %d to pid %d", rebootsignal, pid); | |
1988 | ||
a579fa51 | 1989 | if (timeout == 0) |
d39b10eb CB |
1990 | return true; |
1991 | ||
1992 | ret = lxc_cmd_sock_rcv_state(state_client_fd, timeout); | |
1993 | close(state_client_fd); | |
1994 | if (ret < 0) | |
1995 | return false; | |
1996 | ||
1997 | TRACE("Received state \"%s\"", lxc_state2str(ret)); | |
1998 | if (ret != RUNNING) | |
1999 | return false; | |
2000 | ||
2001 | return true; | |
2002 | } | |
2003 | ||
2004 | WRAP_API_1(bool, lxcapi_reboot2, int) | |
2005 | ||
858377e4 | 2006 | static bool do_lxcapi_shutdown(struct lxc_container *c, int timeout) |
72d0e1cb | 2007 | { |
f8bdb6dc | 2008 | int killret, ret; |
72d0e1cb | 2009 | pid_t pid; |
9dd54153 | 2010 | int haltsignal = SIGPWR, state_client_fd = -EBADF; |
92e35018 | 2011 | lxc_state_t states[MAX_STATE] = {0}; |
72d0e1cb SG |
2012 | |
2013 | if (!c) | |
2014 | return false; | |
2015 | ||
858377e4 | 2016 | if (!do_lxcapi_is_running(c)) |
72d0e1cb | 2017 | return true; |
f8bdb6dc | 2018 | |
858377e4 | 2019 | pid = do_lxcapi_init_pid(c); |
72d0e1cb SG |
2020 | if (pid <= 0) |
2021 | return true; | |
330ae3d3 CB |
2022 | |
2023 | /* Detect whether we should send SIGRTMIN + 3 (e.g. systemd). */ | |
b0227444 | 2024 | if (c->lxc_conf && c->lxc_conf->haltsignal) |
f0f1d8c0 | 2025 | haltsignal = c->lxc_conf->haltsignal; |
573ad77f | 2026 | else if (task_blocks_signal(pid, (SIGRTMIN + 3))) |
eabf1ea9 | 2027 | haltsignal = (SIGRTMIN + 3); |
330ae3d3 | 2028 | |
92e35018 CB |
2029 | /* Add a new state client before sending the shutdown signal so that we |
2030 | * don't miss a state. | |
2031 | */ | |
f8bdb6dc CB |
2032 | if (timeout != 0) { |
2033 | states[STOPPED] = 1; | |
2034 | ret = lxc_cmd_add_state_client(c->name, c->config_path, states, | |
2035 | &state_client_fd); | |
2036 | if (ret < 0) | |
2037 | return false; | |
92e35018 | 2038 | |
f8bdb6dc CB |
2039 | if (state_client_fd < 0) |
2040 | return false; | |
2041 | ||
2042 | if (ret == STOPPED) | |
2043 | return true; | |
591614a7 | 2044 | |
f8bdb6dc | 2045 | if (ret < MAX_STATE) |
92e35018 | 2046 | return false; |
92e35018 CB |
2047 | } |
2048 | ||
f8bdb6dc CB |
2049 | /* Send shutdown signal to container. */ |
2050 | killret = kill(pid, haltsignal); | |
2051 | if (killret < 0) { | |
f8bdb6dc CB |
2052 | if (state_client_fd >= 0) |
2053 | close(state_client_fd); | |
9dd54153 | 2054 | WARN("Failed to send signal %d to pid %d", haltsignal, pid); |
f8bdb6dc CB |
2055 | return false; |
2056 | } | |
2057 | TRACE("Sent signal %d to pid %d", haltsignal, pid); | |
2058 | ||
923929f6 | 2059 | if (timeout == 0) |
f8bdb6dc CB |
2060 | return true; |
2061 | ||
2062 | ret = lxc_cmd_sock_rcv_state(state_client_fd, timeout); | |
2063 | close(state_client_fd); | |
2064 | if (ret < 0) | |
2065 | return false; | |
2066 | ||
2067 | TRACE("Received state \"%s\"", lxc_state2str(ret)); | |
2068 | if (ret != STOPPED) | |
2069 | return false; | |
2070 | ||
2071 | return true; | |
72d0e1cb SG |
2072 | } |
2073 | ||
858377e4 SH |
2074 | WRAP_API_1(bool, lxcapi_shutdown, int) |
2075 | ||
1897e3bc | 2076 | static bool lxcapi_createl(struct lxc_container *c, const char *t, |
dc23c1c8 | 2077 | const char *bdevtype, struct bdev_specs *specs, int flags, ...) |
72d0e1cb SG |
2078 | { |
2079 | bool bret = false; | |
a0e93eeb | 2080 | char **args = NULL; |
72d0e1cb | 2081 | va_list ap; |
72d0e1cb SG |
2082 | |
2083 | if (!c) | |
2084 | return false; | |
2085 | ||
858377e4 SH |
2086 | current_config = c->lxc_conf; |
2087 | ||
72d0e1cb SG |
2088 | /* |
2089 | * since we're going to wait for create to finish, I don't think we | |
2090 | * need to get a copy of the arguments. | |
2091 | */ | |
dc23c1c8 | 2092 | va_start(ap, flags); |
a0e93eeb | 2093 | args = lxc_va_arg_list_to_argv(ap, 0, 0); |
72d0e1cb | 2094 | va_end(ap); |
a0e93eeb | 2095 | if (!args) { |
e9e29a33 | 2096 | ERROR("Failed to allocate memory"); |
a0e93eeb CS |
2097 | goto out; |
2098 | } | |
72d0e1cb | 2099 | |
858377e4 | 2100 | bret = do_lxcapi_create(c, t, bdevtype, specs, flags, args); |
72d0e1cb SG |
2101 | |
2102 | out: | |
a0e93eeb | 2103 | free(args); |
858377e4 | 2104 | current_config = NULL; |
72d0e1cb SG |
2105 | return bret; |
2106 | } | |
2107 | ||
6b0d5538 SH |
2108 | static void do_clear_unexp_config_line(struct lxc_conf *conf, const char *key) |
2109 | { | |
4222a9f4 CB |
2110 | if (!strcmp(key, "lxc.cgroup")) |
2111 | return clear_unexp_config_line(conf, key, true); | |
2112 | ||
2113 | if (!strcmp(key, "lxc.network")) | |
2114 | return clear_unexp_config_line(conf, key, true); | |
2115 | ||
2116 | if (!strcmp(key, "lxc.net")) | |
2117 | return clear_unexp_config_line(conf, key, true); | |
2118 | ||
2119 | /* Clear a network with a specific index. */ | |
2120 | if (!strncmp(key, "lxc.net.", 8)) { | |
2121 | int ret; | |
2122 | const char *idx; | |
2123 | ||
2124 | idx = key + 8; | |
2125 | ret = lxc_safe_uint(idx, &(unsigned int){0}); | |
2126 | if (!ret) | |
2127 | return clear_unexp_config_line(conf, key, true); | |
2128 | } | |
2129 | ||
2130 | if (!strcmp(key, "lxc.hook")) | |
2131 | return clear_unexp_config_line(conf, key, true); | |
2132 | ||
2133 | return clear_unexp_config_line(conf, key, false); | |
6b0d5538 SH |
2134 | } |
2135 | ||
6afd673f CB |
2136 | static bool do_lxcapi_clear_config_item(struct lxc_container *c, |
2137 | const char *key) | |
72d0e1cb | 2138 | { |
6afd673f CB |
2139 | int ret = 1; |
2140 | struct lxc_config_t *config; | |
72d0e1cb SG |
2141 | |
2142 | if (!c || !c->lxc_conf) | |
2143 | return false; | |
6afd673f | 2144 | |
5cee8c50 | 2145 | if (container_mem_lock(c)) |
72d0e1cb | 2146 | return false; |
6afd673f | 2147 | |
300df83e | 2148 | config = lxc_get_config(key); |
6afd673f CB |
2149 | /* Verify that the config key exists and that it has a callback |
2150 | * implemented. | |
2151 | */ | |
2152 | if (config && config->clr) | |
26471403 | 2153 | ret = config->clr(key, c->lxc_conf, NULL); |
6b0d5538 SH |
2154 | if (!ret) |
2155 | do_clear_unexp_config_line(c->lxc_conf, key); | |
6afd673f | 2156 | |
5cee8c50 | 2157 | container_mem_unlock(c); |
72d0e1cb SG |
2158 | return ret == 0; |
2159 | } | |
2160 | ||
858377e4 SH |
2161 | WRAP_API_1(bool, lxcapi_clear_config_item, const char *) |
2162 | ||
e0f59189 | 2163 | static inline bool enter_net_ns(struct lxc_container *c) |
51d0854c | 2164 | { |
858377e4 | 2165 | pid_t pid = do_lxcapi_init_pid(c); |
ae22a220 | 2166 | |
0e6e3a41 | 2167 | if ((geteuid() != 0 || (c->lxc_conf && !lxc_list_empty(&c->lxc_conf->id_map))) && access("/proc/self/ns/user", F_OK) == 0) { |
51d0854c DY |
2168 | if (!switch_to_ns(pid, "user")) |
2169 | return false; | |
9c83a661 | 2170 | } |
51d0854c | 2171 | return switch_to_ns(pid, "net"); |
799f29ab ÇO |
2172 | } |
2173 | ||
1a0e70ac | 2174 | /* Used by qsort and bsearch functions for comparing names. */ |
9c88ff1f ÇO |
2175 | static inline int string_cmp(char **first, char **second) |
2176 | { | |
2177 | return strcmp(*first, *second); | |
2178 | } | |
2179 | ||
1a0e70ac CB |
2180 | /* Used by qsort and bsearch functions for comparing container names. */ |
2181 | static inline int container_cmp(struct lxc_container **first, | |
2182 | struct lxc_container **second) | |
9c88ff1f ÇO |
2183 | { |
2184 | return strcmp((*first)->name, (*second)->name); | |
2185 | } | |
2186 | ||
2187 | static bool add_to_array(char ***names, char *cname, int pos) | |
2188 | { | |
2189 | char **newnames = realloc(*names, (pos+1) * sizeof(char *)); | |
2190 | if (!newnames) { | |
2191 | ERROR("Out of memory"); | |
2192 | return false; | |
2193 | } | |
2194 | ||
2195 | *names = newnames; | |
2196 | newnames[pos] = strdup(cname); | |
2197 | if (!newnames[pos]) | |
2198 | return false; | |
2199 | ||
1a0e70ac CB |
2200 | /* Sort the arrray as we will use binary search on it. */ |
2201 | qsort(newnames, pos + 1, sizeof(char *), | |
2202 | (int (*)(const void *, const void *))string_cmp); | |
9c88ff1f ÇO |
2203 | |
2204 | return true; | |
2205 | } | |
2206 | ||
1a0e70ac CB |
2207 | static bool add_to_clist(struct lxc_container ***list, struct lxc_container *c, |
2208 | int pos, bool sort) | |
9c88ff1f | 2209 | { |
1a0e70ac | 2210 | struct lxc_container **newlist = realloc(*list, (pos + 1) * sizeof(struct lxc_container *)); |
9c88ff1f ÇO |
2211 | if (!newlist) { |
2212 | ERROR("Out of memory"); | |
2213 | return false; | |
2214 | } | |
2215 | ||
2216 | *list = newlist; | |
2217 | newlist[pos] = c; | |
2218 | ||
1a0e70ac | 2219 | /* Sort the arrray as we will use binary search on it. */ |
2871830a | 2220 | if (sort) |
1a0e70ac CB |
2221 | qsort(newlist, pos + 1, sizeof(struct lxc_container *), |
2222 | (int (*)(const void *, const void *))container_cmp); | |
9c88ff1f ÇO |
2223 | |
2224 | return true; | |
2225 | } | |
2226 | ||
2227 | static char** get_from_array(char ***names, char *cname, int size) | |
2228 | { | |
2229 | return (char **)bsearch(&cname, *names, size, sizeof(char *), (int (*)(const void *, const void *))string_cmp); | |
2230 | } | |
2231 | ||
2232 | ||
2233 | static bool array_contains(char ***names, char *cname, int size) { | |
2234 | if(get_from_array(names, cname, size) != NULL) | |
2235 | return true; | |
2236 | return false; | |
2237 | } | |
2238 | ||
2239 | static bool remove_from_array(char ***names, char *cname, int size) | |
2240 | { | |
2241 | char **result = get_from_array(names, cname, size); | |
2242 | if (result != NULL) { | |
2243 | free(result); | |
2244 | return true; | |
2245 | } | |
2246 | return false; | |
2247 | } | |
2248 | ||
858377e4 | 2249 | static char ** do_lxcapi_get_interfaces(struct lxc_container *c) |
799f29ab | 2250 | { |
ae22a220 ÇO |
2251 | pid_t pid; |
2252 | int i, count = 0, pipefd[2]; | |
9c88ff1f | 2253 | char **interfaces = NULL; |
ae22a220 | 2254 | char interface[IFNAMSIZ]; |
799f29ab | 2255 | |
ae22a220 ÇO |
2256 | if(pipe(pipefd) < 0) { |
2257 | SYSERROR("pipe failed"); | |
2258 | return NULL; | |
c868b261 ÇO |
2259 | } |
2260 | ||
ae22a220 ÇO |
2261 | pid = fork(); |
2262 | if (pid < 0) { | |
959aee9c | 2263 | SYSERROR("failed to fork task to get interfaces information"); |
ae22a220 ÇO |
2264 | close(pipefd[0]); |
2265 | close(pipefd[1]); | |
2266 | return NULL; | |
2267 | } | |
799f29ab | 2268 | |
1a0e70ac | 2269 | if (pid == 0) { /* child */ |
ae22a220 ÇO |
2270 | int ret = 1, nbytes; |
2271 | struct ifaddrs *interfaceArray = NULL, *tempIfAddr = NULL; | |
2272 | ||
2273 | /* close the read-end of the pipe */ | |
2274 | close(pipefd[0]); | |
2275 | ||
e0f59189 | 2276 | if (!enter_net_ns(c)) { |
ae22a220 ÇO |
2277 | SYSERROR("failed to enter namespace"); |
2278 | goto out; | |
2279 | } | |
2280 | ||
2281 | /* Grab the list of interfaces */ | |
2282 | if (getifaddrs(&interfaceArray)) { | |
2283 | SYSERROR("failed to get interfaces list"); | |
2284 | goto out; | |
2285 | } | |
2286 | ||
2287 | /* Iterate through the interfaces */ | |
2288 | for (tempIfAddr = interfaceArray; tempIfAddr != NULL; tempIfAddr = tempIfAddr->ifa_next) { | |
2289 | nbytes = write(pipefd[1], tempIfAddr->ifa_name, IFNAMSIZ); | |
2290 | if (nbytes < 0) { | |
2291 | ERROR("write failed"); | |
2292 | goto out; | |
2293 | } | |
2294 | count++; | |
2295 | } | |
2296 | ret = 0; | |
2297 | ||
2298 | out: | |
2299 | if (interfaceArray) | |
2300 | freeifaddrs(interfaceArray); | |
2301 | ||
2302 | /* close the write-end of the pipe, thus sending EOF to the reader */ | |
2303 | close(pipefd[1]); | |
02c611b0 | 2304 | _exit(ret); |
799f29ab ÇO |
2305 | } |
2306 | ||
ae22a220 ÇO |
2307 | /* close the write-end of the pipe */ |
2308 | close(pipefd[1]); | |
2309 | ||
358afd84 | 2310 | while (read(pipefd[0], &interface, IFNAMSIZ) == IFNAMSIZ) { |
3151d4e2 CB |
2311 | interface[IFNAMSIZ - 1] = '\0'; |
2312 | ||
ae22a220 ÇO |
2313 | if (array_contains(&interfaces, interface, count)) |
2314 | continue; | |
799f29ab | 2315 | |
ae22a220 | 2316 | if(!add_to_array(&interfaces, interface, count)) |
3151d4e2 CB |
2317 | ERROR("Failed to add \"%s\" to array", interface); |
2318 | ||
9c88ff1f ÇO |
2319 | count++; |
2320 | } | |
799f29ab | 2321 | |
ae22a220 ÇO |
2322 | if (wait_for_pid(pid) != 0) { |
2323 | for(i=0;i<count;i++) | |
2324 | free(interfaces[i]); | |
2325 | free(interfaces); | |
2326 | interfaces = NULL; | |
2327 | } | |
9c88ff1f | 2328 | |
ae22a220 ÇO |
2329 | /* close the read-end of the pipe */ |
2330 | close(pipefd[0]); | |
799f29ab | 2331 | |
9c88ff1f ÇO |
2332 | /* Append NULL to the array */ |
2333 | if(interfaces) | |
2334 | interfaces = (char **)lxc_append_null_to_array((void **)interfaces, count); | |
799f29ab | 2335 | |
9c88ff1f | 2336 | return interfaces; |
799f29ab ÇO |
2337 | } |
2338 | ||
858377e4 SH |
2339 | WRAP_API(char **, lxcapi_get_interfaces) |
2340 | ||
02a0e184 CB |
2341 | static char **do_lxcapi_get_ips(struct lxc_container *c, const char *interface, |
2342 | const char *family, int scope) | |
799f29ab | 2343 | { |
02a0e184 | 2344 | int i, ret; |
ae22a220 | 2345 | pid_t pid; |
02a0e184 | 2346 | int pipefd[2]; |
ae22a220 | 2347 | char address[INET6_ADDRSTRLEN]; |
02a0e184 CB |
2348 | int count = 0; |
2349 | char **addresses = NULL; | |
799f29ab | 2350 | |
02a0e184 CB |
2351 | ret = pipe(pipefd); |
2352 | if (ret < 0) { | |
2353 | SYSERROR("Failed to create pipe"); | |
ae22a220 | 2354 | return NULL; |
c868b261 ÇO |
2355 | } |
2356 | ||
ae22a220 ÇO |
2357 | pid = fork(); |
2358 | if (pid < 0) { | |
02a0e184 | 2359 | SYSERROR("Failed to create new process"); |
ae22a220 ÇO |
2360 | close(pipefd[0]); |
2361 | close(pipefd[1]); | |
2362 | return NULL; | |
9c83a661 SG |
2363 | } |
2364 | ||
02a0e184 CB |
2365 | if (pid == 0) { |
2366 | ssize_t nbytes; | |
ae22a220 | 2367 | char addressOutputBuffer[INET6_ADDRSTRLEN]; |
02a0e184 | 2368 | int ret = 1; |
ae22a220 | 2369 | char *address = NULL; |
02a0e184 CB |
2370 | void *tempAddrPtr = NULL; |
2371 | struct ifaddrs *interfaceArray = NULL, *tempIfAddr = NULL; | |
fe218ca3 | 2372 | |
ae22a220 ÇO |
2373 | /* close the read-end of the pipe */ |
2374 | close(pipefd[0]); | |
2375 | ||
e0f59189 | 2376 | if (!enter_net_ns(c)) { |
02a0e184 | 2377 | SYSERROR("Failed to attach to network namespace"); |
ae22a220 | 2378 | goto out; |
9c83a661 | 2379 | } |
ae22a220 ÇO |
2380 | |
2381 | /* Grab the list of interfaces */ | |
2382 | if (getifaddrs(&interfaceArray)) { | |
02a0e184 | 2383 | SYSERROR("Failed to get interfaces list"); |
ae22a220 ÇO |
2384 | goto out; |
2385 | } | |
2386 | ||
2387 | /* Iterate through the interfaces */ | |
02a0e184 CB |
2388 | for (tempIfAddr = interfaceArray; tempIfAddr; |
2389 | tempIfAddr = tempIfAddr->ifa_next) { | |
ae22a220 | 2390 | if (tempIfAddr->ifa_addr == NULL) |
9c83a661 SG |
2391 | continue; |
2392 | ||
02a0e184 | 2393 | if (tempIfAddr->ifa_addr->sa_family == AF_INET) { |
ae22a220 ÇO |
2394 | if (family && strcmp(family, "inet")) |
2395 | continue; | |
02a0e184 | 2396 | |
ae22a220 | 2397 | tempAddrPtr = &((struct sockaddr_in *)tempIfAddr->ifa_addr)->sin_addr; |
02a0e184 | 2398 | } else { |
ae22a220 ÇO |
2399 | if (family && strcmp(family, "inet6")) |
2400 | continue; | |
2401 | ||
2402 | if (((struct sockaddr_in6 *)tempIfAddr->ifa_addr)->sin6_scope_id != scope) | |
2403 | continue; | |
2404 | ||
2405 | tempAddrPtr = &((struct sockaddr_in6 *)tempIfAddr->ifa_addr)->sin6_addr; | |
2406 | } | |
2407 | ||
2408 | if (interface && strcmp(interface, tempIfAddr->ifa_name)) | |
2409 | continue; | |
2410 | else if (!interface && strcmp("lo", tempIfAddr->ifa_name) == 0) | |
9c83a661 SG |
2411 | continue; |
2412 | ||
ae22a220 | 2413 | address = (char *)inet_ntop(tempIfAddr->ifa_addr->sa_family, |
02a0e184 CB |
2414 | tempAddrPtr, addressOutputBuffer, |
2415 | sizeof(addressOutputBuffer)); | |
ae22a220 | 2416 | if (!address) |
02a0e184 | 2417 | continue; |
ae22a220 | 2418 | |
02a0e184 CB |
2419 | nbytes = lxc_write_nointr(pipefd[1], address, INET6_ADDRSTRLEN); |
2420 | if (nbytes != INET6_ADDRSTRLEN) { | |
2421 | SYSERROR("Failed to send ipv6 address \"%s\"", | |
2422 | address); | |
ae22a220 ÇO |
2423 | goto out; |
2424 | } | |
2425 | count++; | |
9c83a661 | 2426 | } |
ae22a220 | 2427 | ret = 0; |
9c83a661 | 2428 | |
ae22a220 | 2429 | out: |
02a0e184 | 2430 | if (interfaceArray) |
ae22a220 | 2431 | freeifaddrs(interfaceArray); |
9c83a661 | 2432 | |
ae22a220 ÇO |
2433 | /* close the write-end of the pipe, thus sending EOF to the reader */ |
2434 | close(pipefd[1]); | |
fe1ce58c | 2435 | _exit(ret); |
6849cb5b | 2436 | } |
9c83a661 | 2437 | |
ae22a220 ÇO |
2438 | /* close the write-end of the pipe */ |
2439 | close(pipefd[1]); | |
2440 | ||
02a0e184 CB |
2441 | while (lxc_read_nointr(pipefd[0], &address, INET6_ADDRSTRLEN) == INET6_ADDRSTRLEN) { |
2442 | address[INET6_ADDRSTRLEN - 1] = '\0'; | |
2443 | ||
2444 | if (!add_to_array(&addresses, address, count)) | |
ae22a220 | 2445 | ERROR("PARENT: add_to_array failed"); |
02a0e184 | 2446 | |
9c88ff1f | 2447 | count++; |
9c83a661 SG |
2448 | } |
2449 | ||
ae22a220 | 2450 | if (wait_for_pid(pid) != 0) { |
02a0e184 | 2451 | for (i = 0; i < count; i++) |
ae22a220 | 2452 | free(addresses[i]); |
02a0e184 | 2453 | |
ae22a220 ÇO |
2454 | free(addresses); |
2455 | addresses = NULL; | |
2456 | } | |
9c83a661 | 2457 | |
ae22a220 ÇO |
2458 | /* close the read-end of the pipe */ |
2459 | close(pipefd[0]); | |
9c83a661 SG |
2460 | |
2461 | /* Append NULL to the array */ | |
02a0e184 | 2462 | if (addresses) |
9c88ff1f | 2463 | addresses = (char **)lxc_append_null_to_array((void **)addresses, count); |
9c83a661 SG |
2464 | |
2465 | return addresses; | |
2466 | } | |
2467 | ||
858377e4 SH |
2468 | WRAP_API_3(char **, lxcapi_get_ips, const char *, const char *, int) |
2469 | ||
2470 | static int do_lxcapi_get_config_item(struct lxc_container *c, const char *key, char *retv, int inlen) | |
72d0e1cb | 2471 | { |
fce687aa CB |
2472 | int ret = -1; |
2473 | struct lxc_config_t *config; | |
72d0e1cb SG |
2474 | |
2475 | if (!c || !c->lxc_conf) | |
2476 | return -1; | |
fce687aa | 2477 | |
5cee8c50 | 2478 | if (container_mem_lock(c)) |
72d0e1cb | 2479 | return -1; |
fce687aa | 2480 | |
300df83e | 2481 | config = lxc_get_config(key); |
fce687aa CB |
2482 | /* Verify that the config key exists and that it has a callback |
2483 | * implemented. | |
2484 | */ | |
2485 | if (config && config->get) | |
cccd2219 | 2486 | ret = config->get(key, retv, inlen, c->lxc_conf, NULL); |
fce687aa | 2487 | |
5cee8c50 | 2488 | container_mem_unlock(c); |
72d0e1cb SG |
2489 | return ret; |
2490 | } | |
2491 | ||
858377e4 SH |
2492 | WRAP_API_3(int, lxcapi_get_config_item, const char *, char *, int) |
2493 | ||
2494 | static char* do_lxcapi_get_running_config_item(struct lxc_container *c, const char *key) | |
8ac18377 ÇO |
2495 | { |
2496 | char *ret; | |
2497 | ||
2498 | if (!c || !c->lxc_conf) | |
2499 | return NULL; | |
2500 | if (container_mem_lock(c)) | |
2501 | return NULL; | |
858377e4 | 2502 | ret = lxc_cmd_get_config_item(c->name, key, do_lxcapi_get_config_path(c)); |
8ac18377 ÇO |
2503 | container_mem_unlock(c); |
2504 | return ret; | |
2505 | } | |
2506 | ||
858377e4 SH |
2507 | WRAP_API_1(char *, lxcapi_get_running_config_item, const char *) |
2508 | ||
2509 | static int do_lxcapi_get_keys(struct lxc_container *c, const char *key, char *retv, int inlen) | |
72d0e1cb | 2510 | { |
300df83e CB |
2511 | int ret = -1; |
2512 | ||
2513 | /* List all config items. */ | |
72d0e1cb | 2514 | if (!key) |
cfc67626 | 2515 | return lxc_list_config_items(retv, inlen); |
300df83e | 2516 | |
72d0e1cb SG |
2517 | if (!c || !c->lxc_conf) |
2518 | return -1; | |
300df83e | 2519 | |
5cee8c50 | 2520 | if (container_mem_lock(c)) |
72d0e1cb | 2521 | return -1; |
300df83e CB |
2522 | |
2523 | /* Support 'lxc.net.<idx>', i.e. 'lxc.net.0' | |
2524 | * This is an intelligent result to show which keys are valid given the | |
2525 | * type of nic it is. | |
2526 | */ | |
6fba98b5 | 2527 | if (strncmp(key, "lxc.net.", 8) == 0) |
01f55c40 | 2528 | ret = lxc_list_net(c->lxc_conf, key, retv, inlen); |
fe9b7349 CB |
2529 | else |
2530 | ret = lxc_list_subkeys(c->lxc_conf, key, retv, inlen); | |
300df83e | 2531 | |
5cee8c50 | 2532 | container_mem_unlock(c); |
72d0e1cb SG |
2533 | return ret; |
2534 | } | |
2535 | ||
858377e4 SH |
2536 | WRAP_API_3(int, lxcapi_get_keys, const char *, char *, int) |
2537 | ||
2538 | static bool do_lxcapi_save_config(struct lxc_container *c, const char *alt_file) | |
72d0e1cb | 2539 | { |
0e1a60b0 | 2540 | int fd, lret; |
39dc698c | 2541 | bool ret = false, need_disklock = false; |
39dc698c | 2542 | |
72d0e1cb SG |
2543 | if (!alt_file) |
2544 | alt_file = c->configfile; | |
2545 | if (!alt_file) | |
1a0e70ac | 2546 | return false; |
39dc698c | 2547 | |
1a0e70ac | 2548 | /* If we haven't yet loaded a config, load the stock config. */ |
39dc698c | 2549 | if (!c->lxc_conf) { |
858377e4 | 2550 | if (!do_lxcapi_load_config(c, lxc_global_config_value("lxc.default_config"))) { |
0e1a60b0 CB |
2551 | ERROR("Error loading default configuration file %s " |
2552 | "while saving %s", | |
2553 | lxc_global_config_value("lxc.default_config"), | |
2554 | c->name); | |
72d0e1cb SG |
2555 | return false; |
2556 | } | |
39dc698c | 2557 | } |
72d0e1cb | 2558 | |
5a3d2e1e SG |
2559 | if (!create_container_dir(c)) |
2560 | return false; | |
2561 | ||
1a0e70ac CB |
2562 | /* If we're writing to the container's config file, take the disk lock. |
2563 | * Otherwise just take the memlock to protect the struct lxc_container | |
2564 | * while we're traversing it. | |
39dc698c SH |
2565 | */ |
2566 | if (strcmp(c->configfile, alt_file) == 0) | |
2567 | need_disklock = true; | |
2568 | ||
2569 | if (need_disklock) | |
2570 | lret = container_disk_lock(c); | |
2571 | else | |
2572 | lret = container_mem_lock(c); | |
39dc698c | 2573 | if (lret) |
72d0e1cb | 2574 | return false; |
39dc698c | 2575 | |
10034af5 | 2576 | fd = open(alt_file, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, |
0e1a60b0 CB |
2577 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
2578 | if (fd < 0) | |
2579 | goto on_error; | |
2580 | ||
2581 | lret = write_config(fd, c->lxc_conf); | |
2582 | close(fd); | |
2583 | if (lret < 0) | |
2584 | goto on_error; | |
2585 | ||
39dc698c SH |
2586 | ret = true; |
2587 | ||
0e1a60b0 | 2588 | on_error: |
39dc698c SH |
2589 | if (need_disklock) |
2590 | container_disk_unlock(c); | |
2591 | else | |
2592 | container_mem_unlock(c); | |
0e1a60b0 | 2593 | |
39dc698c | 2594 | return ret; |
72d0e1cb SG |
2595 | } |
2596 | ||
858377e4 SH |
2597 | WRAP_API_1(bool, lxcapi_save_config, const char *) |
2598 | ||
0ea055b3 CB |
2599 | |
2600 | static bool mod_rdep(struct lxc_container *c0, struct lxc_container *c, bool inc) | |
dfb31b25 | 2601 | { |
0ea055b3 CB |
2602 | FILE *f1; |
2603 | struct stat fbuf; | |
42342bed CB |
2604 | void *buf = NULL; |
2605 | char *del = NULL; | |
dfb31b25 | 2606 | char path[MAXPATHLEN]; |
0ea055b3 CB |
2607 | char newpath[MAXPATHLEN]; |
2608 | int fd, ret, n = 0, v = 0; | |
dfb31b25 | 2609 | bool bret = false; |
42342bed | 2610 | size_t len = 0, bytes = 0; |
dfb31b25 | 2611 | |
0ea055b3 | 2612 | if (container_disk_lock(c0)) |
dfb31b25 | 2613 | return false; |
0ea055b3 CB |
2614 | |
2615 | ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_snapshots", c0->config_path, c0->name); | |
dfb31b25 SH |
2616 | if (ret < 0 || ret > MAXPATHLEN) |
2617 | goto out; | |
0ea055b3 CB |
2618 | ret = snprintf(newpath, MAXPATHLEN, "%s\n%s\n", c->config_path, c->name); |
2619 | if (ret < 0 || ret > MAXPATHLEN) | |
dfb31b25 | 2620 | goto out; |
0ea055b3 CB |
2621 | |
2622 | /* If we find an lxc-snapshot file using the old format only listing the | |
2623 | * number of snapshots we will keep using it. */ | |
2624 | f1 = fopen(path, "r"); | |
2625 | if (f1) { | |
2626 | n = fscanf(f1, "%d", &v); | |
2627 | fclose(f1); | |
2628 | if (n == 1 && v == 0) { | |
dc509bf2 CB |
2629 | ret = remove(path); |
2630 | if (ret < 0) | |
6d1400b5 | 2631 | SYSERROR("Failed to remove \"%s\"", path); |
2632 | ||
0ea055b3 CB |
2633 | n = 0; |
2634 | } | |
dfb31b25 | 2635 | } |
6d1400b5 | 2636 | |
0ea055b3 CB |
2637 | if (n == 1) { |
2638 | v += inc ? 1 : -1; | |
2639 | f1 = fopen(path, "w"); | |
2640 | if (!f1) | |
2641 | goto out; | |
6d1400b5 | 2642 | |
0ea055b3 CB |
2643 | if (fprintf(f1, "%d\n", v) < 0) { |
2644 | ERROR("Error writing new snapshots value"); | |
2645 | fclose(f1); | |
2646 | goto out; | |
2647 | } | |
6d1400b5 | 2648 | |
0ea055b3 CB |
2649 | ret = fclose(f1); |
2650 | if (ret != 0) { | |
2651 | SYSERROR("Error writing to or closing snapshots file"); | |
2652 | goto out; | |
2653 | } | |
2654 | } else { | |
2655 | /* Here we know that we have or can use an lxc-snapshot file | |
2656 | * using the new format. */ | |
2657 | if (inc) { | |
2658 | f1 = fopen(path, "a"); | |
2659 | if (!f1) | |
2660 | goto out; | |
2661 | ||
2662 | if (fprintf(f1, "%s", newpath) < 0) { | |
2663 | ERROR("Error writing new snapshots entry"); | |
2664 | ret = fclose(f1); | |
2665 | if (ret != 0) | |
2666 | SYSERROR("Error writing to or closing snapshots file"); | |
2667 | goto out; | |
2668 | } | |
2669 | ||
2670 | ret = fclose(f1); | |
2671 | if (ret != 0) { | |
2672 | SYSERROR("Error writing to or closing snapshots file"); | |
2673 | goto out; | |
2674 | } | |
2675 | } else if (!inc) { | |
d028235d SG |
2676 | if ((fd = open(path, O_RDWR | O_CLOEXEC)) < 0) |
2677 | goto out; | |
2678 | ||
2679 | if (fstat(fd, &fbuf) < 0) { | |
2680 | close(fd); | |
2681 | goto out; | |
2682 | } | |
2683 | ||
2684 | if (fbuf.st_size != 0) { | |
25086a5f | 2685 | buf = lxc_strmmap(NULL, fbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
d028235d SG |
2686 | if (buf == MAP_FAILED) { |
2687 | SYSERROR("Failed to create mapping %s", path); | |
2688 | close(fd); | |
2689 | goto out; | |
2690 | } | |
2691 | ||
2692 | len = strlen(newpath); | |
2693 | while ((del = strstr((char *)buf, newpath))) { | |
2694 | memmove(del, del + len, strlen(del) - len + 1); | |
2695 | bytes += len; | |
2696 | } | |
2697 | ||
25086a5f | 2698 | lxc_strmunmap(buf, fbuf.st_size); |
d028235d SG |
2699 | if (ftruncate(fd, fbuf.st_size - bytes) < 0) { |
2700 | SYSERROR("Failed to truncate file %s", path); | |
2701 | close(fd); | |
2702 | goto out; | |
2703 | } | |
2704 | } | |
2705 | close(fd); | |
2706 | } | |
0ea055b3 CB |
2707 | |
2708 | /* If the lxc-snapshot file is empty, remove it. */ | |
2709 | if (stat(path, &fbuf) < 0) | |
2710 | goto out; | |
6d1400b5 | 2711 | |
d028235d | 2712 | if (!fbuf.st_size) { |
71261a5c CB |
2713 | ret = remove(path); |
2714 | if (ret < 0) | |
2715 | SYSERROR("Failed to remove \"%s\"", path); | |
0ea055b3 | 2716 | } |
dfb31b25 SH |
2717 | } |
2718 | ||
2719 | bret = true; | |
2720 | ||
2721 | out: | |
0ea055b3 | 2722 | container_disk_unlock(c0); |
dfb31b25 SH |
2723 | return bret; |
2724 | } | |
2725 | ||
d825fff3 | 2726 | void mod_all_rdeps(struct lxc_container *c, bool inc) |
dfb31b25 SH |
2727 | { |
2728 | struct lxc_container *p; | |
2729 | char *lxcpath = NULL, *lxcname = NULL, path[MAXPATHLEN]; | |
2730 | size_t pathlen = 0, namelen = 0; | |
2731 | FILE *f; | |
2732 | int ret; | |
2733 | ||
2734 | ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_rdepends", | |
2735 | c->config_path, c->name); | |
2736 | if (ret < 0 || ret >= MAXPATHLEN) { | |
2737 | ERROR("Path name too long"); | |
2738 | return; | |
2739 | } | |
025ed0f3 | 2740 | f = fopen(path, "r"); |
025ed0f3 | 2741 | if (f == NULL) |
dfb31b25 SH |
2742 | return; |
2743 | while (getline(&lxcpath, &pathlen, f) != -1) { | |
2744 | if (getline(&lxcname, &namelen, f) == -1) { | |
959aee9c | 2745 | ERROR("badly formatted file %s", path); |
dfb31b25 SH |
2746 | goto out; |
2747 | } | |
7ad37670 CB |
2748 | |
2749 | remove_trailing_newlines(lxcpath); | |
2750 | remove_trailing_newlines(lxcname); | |
2751 | ||
dfb31b25 SH |
2752 | if ((p = lxc_container_new(lxcname, lxcpath)) == NULL) { |
2753 | ERROR("Unable to find dependent container %s:%s", | |
2754 | lxcpath, lxcname); | |
2755 | continue; | |
2756 | } | |
0ea055b3 CB |
2757 | if (!mod_rdep(p, c, inc)) |
2758 | ERROR("Failed to update snapshots file for %s:%s", | |
dfb31b25 SH |
2759 | lxcpath, lxcname); |
2760 | lxc_container_put(p); | |
2761 | } | |
2762 | out: | |
f10fad2f ME |
2763 | free(lxcpath); |
2764 | free(lxcname); | |
dfb31b25 SH |
2765 | fclose(f); |
2766 | } | |
2767 | ||
18aa217b | 2768 | static bool has_fs_snapshots(struct lxc_container *c) |
dfb31b25 | 2769 | { |
0ea055b3 | 2770 | FILE *f; |
dfb31b25 SH |
2771 | char path[MAXPATHLEN]; |
2772 | int ret, v; | |
0ea055b3 | 2773 | struct stat fbuf; |
dfb31b25 SH |
2774 | bool bret = false; |
2775 | ||
2776 | ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_snapshots", c->config_path, | |
2777 | c->name); | |
2778 | if (ret < 0 || ret > MAXPATHLEN) | |
2779 | goto out; | |
0ea055b3 CB |
2780 | /* If the file doesn't exist there are no snapshots. */ |
2781 | if (stat(path, &fbuf) < 0) | |
dfb31b25 | 2782 | goto out; |
0ea055b3 CB |
2783 | v = fbuf.st_size; |
2784 | if (v != 0) { | |
2785 | f = fopen(path, "r"); | |
2786 | if (!f) | |
2787 | goto out; | |
2788 | ret = fscanf(f, "%d", &v); | |
2789 | fclose(f); | |
1a0e70ac | 2790 | /* TODO: Figure out what to do with the return value of fscanf. */ |
0ea055b3 CB |
2791 | if (ret != 1) |
2792 | INFO("Container uses new lxc-snapshots format %s", path); | |
2793 | } | |
dfb31b25 SH |
2794 | bret = v != 0; |
2795 | ||
2796 | out: | |
2797 | return bret; | |
2798 | } | |
2799 | ||
18aa217b SH |
2800 | static bool has_snapshots(struct lxc_container *c) |
2801 | { | |
2802 | char path[MAXPATHLEN]; | |
74f96976 | 2803 | struct dirent *direntp; |
18aa217b SH |
2804 | int count=0; |
2805 | DIR *dir; | |
2806 | ||
2807 | if (!get_snappath_dir(c, path)) | |
2808 | return false; | |
2809 | dir = opendir(path); | |
2810 | if (!dir) | |
2811 | return false; | |
74f96976 | 2812 | while ((direntp = readdir(dir))) { |
18aa217b SH |
2813 | if (!strcmp(direntp->d_name, ".")) |
2814 | continue; | |
2815 | ||
2816 | if (!strcmp(direntp->d_name, "..")) | |
2817 | continue; | |
2818 | count++; | |
2819 | break; | |
2820 | } | |
2821 | closedir(dir); | |
2822 | return count > 0; | |
2823 | } | |
2824 | ||
297c2d58 | 2825 | static bool do_destroy_container(struct lxc_conf *conf) { |
ee484f7f CB |
2826 | int ret; |
2827 | ||
e0010464 | 2828 | if (am_guest_unpriv()) { |
ee484f7f CB |
2829 | ret = userns_exec_full(conf, storage_destroy_wrapper, conf, |
2830 | "storage_destroy_wrapper"); | |
2831 | if (ret < 0) | |
d028235d | 2832 | return false; |
ee484f7f | 2833 | |
d028235d SG |
2834 | return true; |
2835 | } | |
ee484f7f | 2836 | |
10bc1861 | 2837 | return storage_destroy(conf); |
297c2d58 CB |
2838 | } |
2839 | ||
4355ab5f SH |
2840 | static int lxc_rmdir_onedev_wrapper(void *data) |
2841 | { | |
2842 | char *arg = (char *) data; | |
18aa217b | 2843 | return lxc_rmdir_onedev(arg, "snaps"); |
4355ab5f SH |
2844 | } |
2845 | ||
17a367d8 | 2846 | static int lxc_unlink_exec_wrapper(void *data) |
72d0e1cb | 2847 | { |
17a367d8 CB |
2848 | char *arg = data; |
2849 | return unlink(arg); | |
2850 | } | |
2851 | ||
2852 | static bool container_destroy(struct lxc_container *c, | |
2853 | struct lxc_storage *storage) | |
2854 | { | |
2855 | const char *p1; | |
2856 | size_t len; | |
2857 | struct lxc_conf *conf; | |
2858 | char *path = NULL; | |
c868b261 | 2859 | bool bret = false; |
297c2d58 | 2860 | int ret = 0; |
72d0e1cb | 2861 | |
858377e4 | 2862 | if (!c || !do_lxcapi_is_defined(c)) |
5a3d2e1e SG |
2863 | return false; |
2864 | ||
a70a69e8 | 2865 | conf = c->lxc_conf; |
3bc449ed | 2866 | if (container_disk_lock(c)) |
72d0e1cb | 2867 | return false; |
72d0e1cb | 2868 | |
39dc698c | 2869 | if (!is_stopped(c)) { |
1a0e70ac | 2870 | /* We should queue some sort of error - in c->error_string? */ |
60bf62d4 SH |
2871 | ERROR("container %s is not stopped", c->name); |
2872 | goto out; | |
72d0e1cb SG |
2873 | } |
2874 | ||
a70a69e8 | 2875 | if (conf && !lxc_list_empty(&conf->hooks[LXCHOOK_DESTROY])) { |
37cf711b | 2876 | /* Start of environment variable setup for hooks */ |
cb8ff4d0 | 2877 | if (setenv("LXC_NAME", c->name, 1)) |
17a367d8 CB |
2878 | SYSERROR("Failed to set environment variable for container name"); |
2879 | ||
2880 | if (conf->rcfile && setenv("LXC_CONFIG_FILE", conf->rcfile, 1)) | |
2881 | SYSERROR("Failed to set environment variable for config path"); | |
2882 | ||
2883 | if (conf->rootfs.mount && setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1)) | |
2884 | SYSERROR("Failed to set environment variable for rootfs mount"); | |
2885 | ||
2886 | if (conf->rootfs.path && setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1)) | |
2887 | SYSERROR("Failed to set environment variable for rootfs mount"); | |
2888 | ||
2889 | if (conf->console.path && setenv("LXC_CONSOLE", conf->console.path, 1)) | |
2890 | SYSERROR("Failed to set environment variable for console path"); | |
2891 | ||
2892 | if (conf->console.log_path && setenv("LXC_CONSOLE_LOGPATH", conf->console.log_path, 1)) | |
2893 | SYSERROR("Failed to set environment variable for console log"); | |
37cf711b SY |
2894 | /* End of environment variable setup for hooks */ |
2895 | ||
14a7b0f9 | 2896 | if (run_lxc_hooks(c->name, "destroy", conf, NULL)) { |
17a367d8 | 2897 | ERROR("Failed to execute clone hook for \"%s\"", c->name); |
37cf711b SY |
2898 | goto out; |
2899 | } | |
2900 | } | |
2901 | ||
2902 | if (current_config && conf == current_config) { | |
858377e4 | 2903 | current_config = NULL; |
37cf711b SY |
2904 | if (conf->logfd != -1) { |
2905 | close(conf->logfd); | |
2906 | conf->logfd = -1; | |
858377e4 SH |
2907 | } |
2908 | } | |
2909 | ||
d028235d SG |
2910 | if (conf && conf->rootfs.path && conf->rootfs.mount) { |
2911 | if (!do_destroy_container(conf)) { | |
2912 | ERROR("Error destroying rootfs for %s", c->name); | |
2913 | goto out; | |
2914 | } | |
2915 | INFO("Destroyed rootfs for %s", c->name); | |
2916 | } | |
60bf62d4 | 2917 | |
dfb31b25 SH |
2918 | mod_all_rdeps(c, false); |
2919 | ||
17a367d8 CB |
2920 | p1 = do_lxcapi_get_config_path(c); |
2921 | /* strlen(p1) | |
2922 | * + | |
2923 | * / | |
2924 | * + | |
2925 | * strlen(c->name) | |
2926 | * + | |
2927 | * / | |
2928 | * + | |
2929 | * strlen("config") = 6 | |
2930 | * + | |
2931 | * \0 | |
2932 | */ | |
2933 | len = strlen(p1) + 1 + strlen(c->name) + 1 + 6 + 1; | |
2934 | path = malloc(len); | |
2935 | if (!path) { | |
2936 | ERROR("Failed to allocate memory"); | |
2937 | goto out; | |
2938 | } | |
2939 | ||
2940 | /* For an overlay container the rootfs is considered immutable and | |
2941 | * cannot be removed when restoring from a snapshot. | |
2942 | */ | |
2943 | if (storage && (!strcmp(storage->type, "overlay") || | |
2944 | !strcmp(storage->type, "overlayfs")) && | |
2945 | (storage->flags & LXC_STORAGE_INTERNAL_OVERLAY_RESTORE)) { | |
2946 | ret = snprintf(path, len, "%s/%s/config", p1, c->name); | |
2947 | if (ret < 0 || (size_t)ret >= len) | |
2948 | goto out; | |
2949 | ||
e0010464 | 2950 | if (am_guest_unpriv()) |
17a367d8 CB |
2951 | ret = userns_exec_1(conf, lxc_unlink_exec_wrapper, path, |
2952 | "lxc_unlink_exec_wrapper"); | |
2953 | else | |
2954 | ret = unlink(path); | |
2955 | if (ret < 0) { | |
2956 | SYSERROR("Failed to destroy config file \"%s\" for \"%s\"", | |
2957 | path, c->name); | |
2958 | goto out; | |
2959 | } | |
2960 | INFO("Destroyed config file \"%s\" for \"%s\"", path, c->name); | |
2961 | ||
2962 | bret = true; | |
2963 | goto out; | |
2964 | } | |
2965 | ||
2966 | ret = snprintf(path, len, "%s/%s", p1, c->name); | |
2967 | if (ret < 0 || (size_t)ret >= len) | |
2968 | goto out; | |
e0010464 | 2969 | if (am_guest_unpriv()) |
ee484f7f CB |
2970 | ret = userns_exec_full(conf, lxc_rmdir_onedev_wrapper, path, |
2971 | "lxc_rmdir_onedev_wrapper"); | |
4355ab5f | 2972 | else |
18aa217b | 2973 | ret = lxc_rmdir_onedev(path, "snaps"); |
4355ab5f | 2974 | if (ret < 0) { |
17a367d8 CB |
2975 | ERROR("Failed to destroy directory \"%s\" for \"%s\"", path, |
2976 | c->name); | |
60bf62d4 SH |
2977 | goto out; |
2978 | } | |
17a367d8 | 2979 | INFO("Destroyed directory \"%s\" for \"%s\"", path, c->name); |
297c2d58 | 2980 | |
fef48dc9 | 2981 | bret = true; |
60bf62d4 SH |
2982 | |
2983 | out: | |
17a367d8 CB |
2984 | if (path) |
2985 | free(path); | |
3bc449ed | 2986 | container_disk_unlock(c); |
fef48dc9 | 2987 | return bret; |
72d0e1cb SG |
2988 | } |
2989 | ||
858377e4 | 2990 | static bool do_lxcapi_destroy(struct lxc_container *c) |
18aa217b SH |
2991 | { |
2992 | if (!c || !lxcapi_is_defined(c)) | |
2993 | return false; | |
2b2655a8 | 2994 | |
18aa217b SH |
2995 | if (has_snapshots(c)) { |
2996 | ERROR("Container %s has snapshots; not removing", c->name); | |
2997 | return false; | |
2998 | } | |
2999 | ||
3000 | if (has_fs_snapshots(c)) { | |
3001 | ERROR("container %s has snapshots on its rootfs", c->name); | |
3002 | return false; | |
3003 | } | |
3004 | ||
17a367d8 | 3005 | return container_destroy(c, NULL); |
18aa217b SH |
3006 | } |
3007 | ||
858377e4 | 3008 | WRAP_API(bool, lxcapi_destroy) |
18aa217b | 3009 | |
858377e4 | 3010 | static bool do_lxcapi_destroy_with_snapshots(struct lxc_container *c) |
18aa217b SH |
3011 | { |
3012 | if (!c || !lxcapi_is_defined(c)) | |
3013 | return false; | |
3014 | if (!lxcapi_snapshot_destroy_all(c)) { | |
3015 | ERROR("Error deleting all snapshots"); | |
3016 | return false; | |
3017 | } | |
3018 | return lxcapi_destroy(c); | |
3019 | } | |
3020 | ||
858377e4 SH |
3021 | WRAP_API(bool, lxcapi_destroy_with_snapshots) |
3022 | ||
0d9cd9c3 CB |
3023 | int lxc_set_config_item_locked(struct lxc_conf *conf, const char *key, |
3024 | const char *v) | |
96532523 | 3025 | { |
0d9cd9c3 | 3026 | int ret; |
96532523 | 3027 | struct lxc_config_t *config; |
0d9cd9c3 CB |
3028 | bool bret = true; |
3029 | ||
3030 | config = lxc_get_config(key); | |
3031 | if (!config) | |
3032 | return -EINVAL; | |
3033 | ||
3034 | ret = config->set(key, v, conf, NULL); | |
3035 | if (ret < 0) | |
3036 | return -EINVAL; | |
3037 | ||
3038 | if (lxc_config_value_empty(v)) | |
3039 | do_clear_unexp_config_line(conf, key); | |
3040 | else | |
3041 | bret = do_append_unexp_config_line(conf, key, v); | |
3042 | if (!bret) | |
3043 | return -ENOMEM; | |
3044 | ||
3045 | return 0; | |
3046 | } | |
3047 | ||
3048 | static bool do_set_config_item_locked(struct lxc_container *c, const char *key, | |
3049 | const char *v) | |
3050 | { | |
3051 | int ret; | |
96532523 SH |
3052 | |
3053 | if (!c->lxc_conf) | |
3054 | c->lxc_conf = lxc_conf_init(); | |
4222a9f4 | 3055 | |
6b0d5538 | 3056 | if (!c->lxc_conf) |
96532523 | 3057 | return false; |
4222a9f4 | 3058 | |
0d9cd9c3 CB |
3059 | ret = lxc_set_config_item_locked(c->lxc_conf, key, v); |
3060 | if (ret < 0) | |
f979ac15 | 3061 | return false; |
4222a9f4 | 3062 | |
0d9cd9c3 | 3063 | return true; |
96532523 SH |
3064 | } |
3065 | ||
858377e4 | 3066 | static bool do_lxcapi_set_config_item(struct lxc_container *c, const char *key, const char *v) |
72d0e1cb | 3067 | { |
72d0e1cb | 3068 | bool b = false; |
72d0e1cb SG |
3069 | |
3070 | if (!c) | |
3071 | return false; | |
3072 | ||
5cee8c50 | 3073 | if (container_mem_lock(c)) |
72d0e1cb SG |
3074 | return false; |
3075 | ||
0d9cd9c3 | 3076 | b = do_set_config_item_locked(c, key, v); |
72d0e1cb | 3077 | |
5cee8c50 | 3078 | container_mem_unlock(c); |
72d0e1cb SG |
3079 | return b; |
3080 | } | |
3081 | ||
858377e4 SH |
3082 | WRAP_API_2(bool, lxcapi_set_config_item, const char *, const char *) |
3083 | ||
72d0e1cb SG |
3084 | static char *lxcapi_config_file_name(struct lxc_container *c) |
3085 | { | |
3086 | if (!c || !c->configfile) | |
3087 | return NULL; | |
3088 | return strdup(c->configfile); | |
3089 | } | |
3090 | ||
2a59a681 SH |
3091 | static const char *lxcapi_get_config_path(struct lxc_container *c) |
3092 | { | |
3093 | if (!c || !c->config_path) | |
3094 | return NULL; | |
3095 | return (const char *)(c->config_path); | |
3096 | } | |
3097 | ||
afeecbba SH |
3098 | /* |
3099 | * not for export | |
3100 | * Just recalculate the c->configfile based on the | |
3101 | * c->config_path, which must be set. | |
3102 | * The lxc_container must be locked or not yet public. | |
3103 | */ | |
3104 | static bool set_config_filename(struct lxc_container *c) | |
3105 | { | |
3106 | char *newpath; | |
3107 | int len, ret; | |
3108 | ||
3109 | if (!c->config_path) | |
3110 | return false; | |
3111 | ||
3112 | /* $lxc_path + "/" + c->name + "/" + "config" + '\0' */ | |
3113 | len = strlen(c->config_path) + strlen(c->name) + strlen("config") + 3; | |
3114 | newpath = malloc(len); | |
3115 | if (!newpath) | |
3116 | return false; | |
3117 | ||
3118 | ret = snprintf(newpath, len, "%s/%s/config", c->config_path, c->name); | |
3119 | if (ret < 0 || ret >= len) { | |
3120 | fprintf(stderr, "Error printing out config file name\n"); | |
3121 | free(newpath); | |
3122 | return false; | |
3123 | } | |
3124 | ||
f10fad2f | 3125 | free(c->configfile); |
afeecbba SH |
3126 | c->configfile = newpath; |
3127 | ||
3128 | return true; | |
3129 | } | |
3130 | ||
858377e4 | 3131 | static bool do_lxcapi_set_config_path(struct lxc_container *c, const char *path) |
2a59a681 SH |
3132 | { |
3133 | char *p; | |
3134 | bool b = false; | |
afeecbba | 3135 | char *oldpath = NULL; |
2a59a681 SH |
3136 | |
3137 | if (!c) | |
3138 | return b; | |
3139 | ||
5cee8c50 | 3140 | if (container_mem_lock(c)) |
2a59a681 SH |
3141 | return b; |
3142 | ||
3143 | p = strdup(path); | |
afeecbba SH |
3144 | if (!p) { |
3145 | ERROR("Out of memory setting new lxc path"); | |
2a59a681 | 3146 | goto err; |
afeecbba SH |
3147 | } |
3148 | ||
2a59a681 SH |
3149 | b = true; |
3150 | if (c->config_path) | |
afeecbba | 3151 | oldpath = c->config_path; |
2a59a681 | 3152 | c->config_path = p; |
afeecbba SH |
3153 | |
3154 | /* Since we've changed the config path, we have to change the | |
3155 | * config file name too */ | |
3156 | if (!set_config_filename(c)) { | |
3157 | ERROR("Out of memory setting new config filename"); | |
3158 | b = false; | |
3159 | free(c->config_path); | |
3160 | c->config_path = oldpath; | |
3161 | oldpath = NULL; | |
3162 | } | |
2a59a681 | 3163 | err: |
f10fad2f | 3164 | free(oldpath); |
5cee8c50 | 3165 | container_mem_unlock(c); |
2a59a681 SH |
3166 | return b; |
3167 | } | |
3168 | ||
858377e4 | 3169 | WRAP_API_1(bool, lxcapi_set_config_path, const char *) |
2a59a681 | 3170 | |
858377e4 | 3171 | static bool do_lxcapi_set_cgroup_item(struct lxc_container *c, const char *subsys, const char *value) |
794dd120 SH |
3172 | { |
3173 | int ret; | |
2202afc9 | 3174 | struct cgroup_ops *cgroup_ops; |
794dd120 SH |
3175 | |
3176 | if (!c) | |
3177 | return false; | |
3178 | ||
3bc449ed | 3179 | if (is_stopped(c)) |
794dd120 SH |
3180 | return false; |
3181 | ||
2202afc9 CB |
3182 | cgroup_ops = cgroup_init(NULL); |
3183 | if (!cgroup_ops) | |
3184 | return false; | |
3185 | ||
3bc449ed SH |
3186 | if (container_disk_lock(c)) |
3187 | return false; | |
794dd120 | 3188 | |
2202afc9 | 3189 | ret = cgroup_ops->set(cgroup_ops, subsys, value, c->name, c->config_path); |
3bc449ed SH |
3190 | |
3191 | container_disk_unlock(c); | |
2202afc9 CB |
3192 | |
3193 | cgroup_exit(cgroup_ops); | |
3194 | ||
3bc449ed | 3195 | return ret == 0; |
794dd120 SH |
3196 | } |
3197 | ||
858377e4 SH |
3198 | WRAP_API_2(bool, lxcapi_set_cgroup_item, const char *, const char *) |
3199 | ||
3200 | static int do_lxcapi_get_cgroup_item(struct lxc_container *c, const char *subsys, char *retv, int inlen) | |
794dd120 | 3201 | { |
3bc449ed | 3202 | int ret; |
2202afc9 | 3203 | struct cgroup_ops *cgroup_ops; |
794dd120 | 3204 | |
6502006a | 3205 | if (!c) |
794dd120 SH |
3206 | return -1; |
3207 | ||
3bc449ed | 3208 | if (is_stopped(c)) |
794dd120 SH |
3209 | return -1; |
3210 | ||
2202afc9 CB |
3211 | cgroup_ops = cgroup_init(NULL); |
3212 | if (!cgroup_ops) | |
3213 | return -1; | |
3214 | ||
3bc449ed SH |
3215 | if (container_disk_lock(c)) |
3216 | return -1; | |
794dd120 | 3217 | |
2202afc9 CB |
3218 | ret = cgroup_ops->get(cgroup_ops, subsys, retv, inlen, c->name, |
3219 | c->config_path); | |
794dd120 | 3220 | |
3bc449ed | 3221 | container_disk_unlock(c); |
2202afc9 CB |
3222 | |
3223 | cgroup_exit(cgroup_ops); | |
3224 | ||
794dd120 SH |
3225 | return ret; |
3226 | } | |
3227 | ||
858377e4 SH |
3228 | WRAP_API_3(int, lxcapi_get_cgroup_item, const char *, char *, int) |
3229 | ||
593e8478 | 3230 | const char *lxc_get_global_config_item(const char *key) |
83c98d82 | 3231 | { |
593e8478 | 3232 | return lxc_global_config_value(key); |
a8428dfa SH |
3233 | } |
3234 | ||
b6b918a1 SG |
3235 | const char *lxc_get_version(void) |
3236 | { | |
95ee490b | 3237 | return LXC_VERSION; |
b6b918a1 SG |
3238 | } |
3239 | ||
f0ca2726 | 3240 | static int copy_file(const char *old, const char *new) |
9be53773 SH |
3241 | { |
3242 | int in, out; | |
3243 | ssize_t len, ret; | |
3244 | char buf[8096]; | |
3245 | struct stat sbuf; | |
3246 | ||
3247 | if (file_exists(new)) { | |
3248 | ERROR("copy destination %s exists", new); | |
3249 | return -1; | |
3250 | } | |
3251 | ret = stat(old, &sbuf); | |
3252 | if (ret < 0) { | |
dfb31b25 | 3253 | INFO("Error stat'ing %s", old); |
9be53773 SH |
3254 | return -1; |
3255 | } | |
3256 | ||
3257 | in = open(old, O_RDONLY); | |
3258 | if (in < 0) { | |
dfb31b25 | 3259 | SYSERROR("Error opening original file %s", old); |
9be53773 SH |
3260 | return -1; |
3261 | } | |
3262 | out = open(new, O_CREAT | O_EXCL | O_WRONLY, 0644); | |
3263 | if (out < 0) { | |
dfb31b25 | 3264 | SYSERROR("Error opening new file %s", new); |
9be53773 SH |
3265 | close(in); |
3266 | return -1; | |
3267 | } | |
3268 | ||
3269 | while (1) { | |
3270 | len = read(in, buf, 8096); | |
3271 | if (len < 0) { | |
dfb31b25 | 3272 | SYSERROR("Error reading old file %s", old); |
9be53773 SH |
3273 | goto err; |
3274 | } | |
3275 | if (len == 0) | |
3276 | break; | |
3277 | ret = write(out, buf, len); | |
1a0e70ac | 3278 | if (ret < len) { /* should we retry? */ |
dfb31b25 | 3279 | SYSERROR("Error: write to new file %s was interrupted", new); |
9be53773 SH |
3280 | goto err; |
3281 | } | |
3282 | } | |
3283 | close(in); | |
3284 | close(out); | |
3285 | ||
1a0e70ac | 3286 | /* We set mode, but not owner/group. */ |
9be53773 SH |
3287 | ret = chmod(new, sbuf.st_mode); |
3288 | if (ret) { | |
dfb31b25 | 3289 | SYSERROR("Error setting mode on %s", new); |
9be53773 SH |
3290 | return -1; |
3291 | } | |
3292 | ||
3293 | return 0; | |
3294 | ||
3295 | err: | |
3296 | close(in); | |
3297 | close(out); | |
3298 | return -1; | |
3299 | } | |
3300 | ||
9be53773 SH |
3301 | static int copyhooks(struct lxc_container *oldc, struct lxc_container *c) |
3302 | { | |
619256b5 | 3303 | int i, len, ret; |
9be53773 | 3304 | struct lxc_list *it; |
619256b5 ÇO |
3305 | char *cpath; |
3306 | ||
3307 | len = strlen(oldc->config_path) + strlen(oldc->name) + 3; | |
3308 | cpath = alloca(len); | |
3309 | ret = snprintf(cpath, len, "%s/%s/", oldc->config_path, oldc->name); | |
3310 | if (ret < 0 || ret >= len) | |
3311 | return -1; | |
9be53773 SH |
3312 | |
3313 | for (i=0; i<NUM_LXC_HOOKS; i++) { | |
3314 | lxc_list_for_each(it, &c->lxc_conf->hooks[i]) { | |
3315 | char *hookname = it->elem; | |
c32981c3 | 3316 | char *fname = strrchr(hookname, '/'); |
9be53773 | 3317 | char tmppath[MAXPATHLEN]; |
1a0e70ac | 3318 | if (!fname) /* relative path - we don't support, but maybe we should */ |
9be53773 | 3319 | return 0; |
619256b5 | 3320 | if (strncmp(hookname, cpath, len - 1) != 0) { |
1a0e70ac | 3321 | /* this hook is public - ignore */ |
619256b5 ÇO |
3322 | continue; |
3323 | } | |
1a0e70ac | 3324 | /* copy the script, and change the entry in confile */ |
9be53773 SH |
3325 | ret = snprintf(tmppath, MAXPATHLEN, "%s/%s/%s", |
3326 | c->config_path, c->name, fname+1); | |
3327 | if (ret < 0 || ret >= MAXPATHLEN) | |
3328 | return -1; | |
3329 | ret = copy_file(it->elem, tmppath); | |
3330 | if (ret < 0) | |
3331 | return -1; | |
3332 | free(it->elem); | |
3333 | it->elem = strdup(tmppath); | |
3334 | if (!it->elem) { | |
3335 | ERROR("out of memory copying hook path"); | |
3336 | return -1; | |
3337 | } | |
9be53773 SH |
3338 | } |
3339 | } | |
3340 | ||
67702c21 SH |
3341 | if (!clone_update_unexp_hooks(c->lxc_conf, oldc->config_path, |
3342 | c->config_path, oldc->name, c->name)) { | |
6b0d5538 SH |
3343 | ERROR("Error saving new hooks in clone"); |
3344 | return -1; | |
3345 | } | |
858377e4 | 3346 | do_lxcapi_save_config(c, NULL); |
9be53773 SH |
3347 | return 0; |
3348 | } | |
3349 | ||
9be53773 SH |
3350 | |
3351 | static int copy_fstab(struct lxc_container *oldc, struct lxc_container *c) | |
3352 | { | |
3353 | char newpath[MAXPATHLEN]; | |
3354 | char *oldpath = oldc->lxc_conf->fstab; | |
3355 | int ret; | |
3356 | ||
3357 | if (!oldpath) | |
3358 | return 0; | |
3359 | ||
47148e96 CB |
3360 | clear_unexp_config_line(c->lxc_conf, "lxc.mount.fstab", false); |
3361 | ||
c32981c3 | 3362 | char *p = strrchr(oldpath, '/'); |
9be53773 SH |
3363 | if (!p) |
3364 | return -1; | |
3365 | ret = snprintf(newpath, MAXPATHLEN, "%s/%s%s", | |
3366 | c->config_path, c->name, p); | |
3367 | if (ret < 0 || ret >= MAXPATHLEN) { | |
3368 | ERROR("error printing new path for %s", oldpath); | |
3369 | return -1; | |
3370 | } | |
3371 | if (file_exists(newpath)) { | |
3372 | ERROR("error: fstab file %s exists", newpath); | |
3373 | return -1; | |
3374 | } | |
3375 | ||
3376 | if (copy_file(oldpath, newpath) < 0) { | |
3377 | ERROR("error: copying %s to %s", oldpath, newpath); | |
3378 | return -1; | |
3379 | } | |
3380 | free(c->lxc_conf->fstab); | |
3381 | c->lxc_conf->fstab = strdup(newpath); | |
3382 | if (!c->lxc_conf->fstab) { | |
3383 | ERROR("error: allocating pathname"); | |
3384 | return -1; | |
3385 | } | |
47148e96 | 3386 | if (!do_append_unexp_config_line(c->lxc_conf, "lxc.mount.fstab", newpath)) { |
6b0d5538 SH |
3387 | ERROR("error saving new lxctab"); |
3388 | return -1; | |
3389 | } | |
9be53773 SH |
3390 | |
3391 | return 0; | |
3392 | } | |
3393 | ||
dfb31b25 SH |
3394 | static void copy_rdepends(struct lxc_container *c, struct lxc_container *c0) |
3395 | { | |
3396 | char path0[MAXPATHLEN], path1[MAXPATHLEN]; | |
3397 | int ret; | |
3398 | ||
3399 | ret = snprintf(path0, MAXPATHLEN, "%s/%s/lxc_rdepends", c0->config_path, | |
3400 | c0->name); | |
3401 | if (ret < 0 || ret >= MAXPATHLEN) { | |
3402 | WARN("Error copying reverse dependencies"); | |
3403 | return; | |
3404 | } | |
3405 | ret = snprintf(path1, MAXPATHLEN, "%s/%s/lxc_rdepends", c->config_path, | |
3406 | c->name); | |
3407 | if (ret < 0 || ret >= MAXPATHLEN) { | |
3408 | WARN("Error copying reverse dependencies"); | |
3409 | return; | |
3410 | } | |
3411 | if (copy_file(path0, path1) < 0) { | |
3412 | INFO("Error copying reverse dependencies"); | |
3413 | return; | |
3414 | } | |
3415 | } | |
3416 | ||
3417 | static bool add_rdepends(struct lxc_container *c, struct lxc_container *c0) | |
3418 | { | |
3419 | int ret; | |
3420 | char path[MAXPATHLEN]; | |
3421 | FILE *f; | |
3422 | bool bret; | |
3423 | ||
3424 | ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_rdepends", c->config_path, | |
3425 | c->name); | |
3426 | if (ret < 0 || ret >= MAXPATHLEN) | |
3427 | return false; | |
3428 | f = fopen(path, "a"); | |
3429 | if (!f) | |
3430 | return false; | |
3431 | bret = true; | |
1a0e70ac | 3432 | /* If anything goes wrong, just return an error. */ |
dfb31b25 SH |
3433 | if (fprintf(f, "%s\n%s\n", c0->config_path, c0->name) < 0) |
3434 | bret = false; | |
3435 | if (fclose(f) != 0) | |
3436 | bret = false; | |
3437 | return bret; | |
3438 | } | |
3439 | ||
15a90a10 SH |
3440 | /* |
3441 | * If the fs natively supports snapshot clones with no penalty, | |
3442 | * then default to those even if not requested. | |
3443 | * Currently we only do this for btrfs. | |
3444 | */ | |
3445 | bool should_default_to_snapshot(struct lxc_container *c0, | |
3446 | struct lxc_container *c1) | |
3447 | { | |
2afdc31f | 3448 | int ret; |
15a90a10 SH |
3449 | size_t l0 = strlen(c0->config_path) + strlen(c0->name) + 2; |
3450 | size_t l1 = strlen(c1->config_path) + strlen(c1->name) + 2; | |
3451 | char *p0 = alloca(l0 + 1); | |
3452 | char *p1 = alloca(l1 + 1); | |
6e0fa6a0 | 3453 | char *rootfs = c0->lxc_conf->rootfs.path; |
15a90a10 | 3454 | |
2afdc31f CB |
3455 | ret = snprintf(p0, l0, "%s/%s", c0->config_path, c0->name); |
3456 | if (ret < 0 || ret >= l0) | |
3457 | return false; | |
3458 | ||
3459 | ret = snprintf(p1, l1, "%s/%s", c1->config_path, c1->name); | |
3460 | if (ret < 0 || ret >= l1) | |
3461 | return false; | |
9a09badc CB |
3462 | |
3463 | if (!is_btrfs_fs(p0) || !is_btrfs_fs(p1)) | |
3464 | return false; | |
3465 | ||
6e0fa6a0 CB |
3466 | if (is_btrfs_subvol(rootfs) <= 0) |
3467 | return false; | |
3468 | ||
15a90a10 SH |
3469 | return btrfs_same_fs(p0, p1) == 0; |
3470 | } | |
3471 | ||
9be53773 | 3472 | static int copy_storage(struct lxc_container *c0, struct lxc_container *c, |
763429b6 CB |
3473 | const char *newtype, int flags, const char *bdevdata, |
3474 | uint64_t newsize) | |
9be53773 | 3475 | { |
10bc1861 | 3476 | struct lxc_storage *bdev; |
07db51a2 | 3477 | bool need_rdep; |
9be53773 | 3478 | |
15a90a10 SH |
3479 | if (should_default_to_snapshot(c0, c)) |
3480 | flags |= LXC_CLONE_SNAPSHOT; | |
3481 | ||
10bc1861 CB |
3482 | bdev = storage_copy(c0, c->name, c->config_path, newtype, flags, |
3483 | bdevdata, newsize, &need_rdep); | |
9be53773 | 3484 | if (!bdev) { |
763429b6 | 3485 | ERROR("Error copying storage."); |
9be53773 SH |
3486 | return -1; |
3487 | } | |
763429b6 CB |
3488 | |
3489 | /* Set new rootfs. */ | |
9be53773 SH |
3490 | free(c->lxc_conf->rootfs.path); |
3491 | c->lxc_conf->rootfs.path = strdup(bdev->src); | |
10bc1861 | 3492 | storage_put(bdev); |
763429b6 | 3493 | |
dfb31b25 | 3494 | if (!c->lxc_conf->rootfs.path) { |
763429b6 CB |
3495 | ERROR("Out of memory while setting storage path."); |
3496 | return -1; | |
3497 | } | |
763429b6 | 3498 | |
7a96a068 CB |
3499 | /* Append a new lxc.rootfs.path entry to the unexpanded config. */ |
3500 | clear_unexp_config_line(c->lxc_conf, "lxc.rootfs.path", false); | |
3501 | if (!do_append_unexp_config_line(c->lxc_conf, "lxc.rootfs.path", | |
763429b6 CB |
3502 | c->lxc_conf->rootfs.path)) { |
3503 | ERROR("Error saving new rootfs to cloned config."); | |
d0218321 SH |
3504 | return -1; |
3505 | } | |
763429b6 | 3506 | |
eee59f94 SH |
3507 | if (flags & LXC_CLONE_SNAPSHOT) |
3508 | copy_rdepends(c, c0); | |
dfb31b25 SH |
3509 | if (need_rdep) { |
3510 | if (!add_rdepends(c, c0)) | |
3511 | WARN("Error adding reverse dependency from %s to %s", | |
763429b6 | 3512 | c->name, c0->name); |
dfb31b25 SH |
3513 | } |
3514 | ||
3515 | mod_all_rdeps(c, true); | |
3516 | ||
9be53773 SH |
3517 | return 0; |
3518 | } | |
3519 | ||
1354955b SH |
3520 | struct clone_update_data { |
3521 | struct lxc_container *c0; | |
3522 | struct lxc_container *c1; | |
3523 | int flags; | |
3524 | char **hookargs; | |
3525 | }; | |
3526 | ||
3527 | static int clone_update_rootfs(struct clone_update_data *data) | |
9be53773 | 3528 | { |
1354955b SH |
3529 | struct lxc_container *c0 = data->c0; |
3530 | struct lxc_container *c = data->c1; | |
3531 | int flags = data->flags; | |
3532 | char **hookargs = data->hookargs; | |
9be53773 SH |
3533 | int ret = -1; |
3534 | char path[MAXPATHLEN]; | |
10bc1861 | 3535 | struct lxc_storage *bdev; |
9be53773 | 3536 | FILE *fout; |
148e91f5 | 3537 | struct lxc_conf *conf = c->lxc_conf; |
9be53773 SH |
3538 | |
3539 | /* update hostname in rootfs */ | |
3540 | /* we're going to mount, so run in a clean namespace to simplify cleanup */ | |
3541 | ||
1354955b SH |
3542 | if (setgid(0) < 0) { |
3543 | ERROR("Failed to setgid to 0"); | |
3544 | return -1; | |
3545 | } | |
3546 | if (setuid(0) < 0) { | |
3547 | ERROR("Failed to setuid to 0"); | |
9be53773 | 3548 | return -1; |
1354955b | 3549 | } |
c476bdce SH |
3550 | if (setgroups(0, NULL) < 0) |
3551 | WARN("Failed to clear groups"); | |
9be53773 | 3552 | |
1354955b SH |
3553 | if (unshare(CLONE_NEWNS) < 0) |
3554 | return -1; | |
8a388ed4 | 3555 | bdev = storage_init(c->lxc_conf); |
9be53773 | 3556 | if (!bdev) |
1354955b | 3557 | return -1; |
cf3ef16d SH |
3558 | if (strcmp(bdev->type, "dir") != 0) { |
3559 | if (unshare(CLONE_NEWNS) < 0) { | |
3560 | ERROR("error unsharing mounts"); | |
10bc1861 | 3561 | storage_put(bdev); |
1354955b | 3562 | return -1; |
cf3ef16d | 3563 | } |
2c6f3fc9 SH |
3564 | if (detect_shared_rootfs()) { |
3565 | if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL)) { | |
3566 | SYSERROR("Failed to make / rslave"); | |
3567 | ERROR("Continuing..."); | |
3568 | } | |
3569 | } | |
e7de366c | 3570 | if (bdev->ops->mount(bdev) < 0) { |
10bc1861 | 3571 | storage_put(bdev); |
1354955b | 3572 | return -1; |
e7de366c | 3573 | } |
1a0e70ac | 3574 | } else { /* TODO come up with a better way */ |
f10fad2f | 3575 | free(bdev->dest); |
cf3ef16d SH |
3576 | bdev->dest = strdup(bdev->src); |
3577 | } | |
148e91f5 SH |
3578 | |
3579 | if (!lxc_list_empty(&conf->hooks[LXCHOOK_CLONE])) { | |
3580 | /* Start of environment variable setup for hooks */ | |
ab7efcf5 | 3581 | if (c0->name && setenv("LXC_SRC_NAME", c0->name, 1)) { |
1143ed39 DE |
3582 | SYSERROR("failed to set environment variable for source container name"); |
3583 | } | |
cb8ff4d0 | 3584 | if (setenv("LXC_NAME", c->name, 1)) { |
148e91f5 SH |
3585 | SYSERROR("failed to set environment variable for container name"); |
3586 | } | |
ab7efcf5 | 3587 | if (conf->rcfile && setenv("LXC_CONFIG_FILE", conf->rcfile, 1)) { |
148e91f5 SH |
3588 | SYSERROR("failed to set environment variable for config path"); |
3589 | } | |
ab7efcf5 | 3590 | if (bdev->dest && setenv("LXC_ROOTFS_MOUNT", bdev->dest, 1)) { |
148e91f5 SH |
3591 | SYSERROR("failed to set environment variable for rootfs mount"); |
3592 | } | |
ab7efcf5 | 3593 | if (conf->rootfs.path && setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1)) { |
148e91f5 SH |
3594 | SYSERROR("failed to set environment variable for rootfs mount"); |
3595 | } | |
3596 | ||
14a7b0f9 | 3597 | if (run_lxc_hooks(c->name, "clone", conf, hookargs)) { |
148e91f5 | 3598 | ERROR("Error executing clone hook for %s", c->name); |
10bc1861 | 3599 | storage_put(bdev); |
1354955b | 3600 | return -1; |
148e91f5 SH |
3601 | } |
3602 | } | |
3603 | ||
3604 | if (!(flags & LXC_CLONE_KEEPNAME)) { | |
3605 | ret = snprintf(path, MAXPATHLEN, "%s/etc/hostname", bdev->dest); | |
10bc1861 | 3606 | storage_put(bdev); |
e7de366c | 3607 | |
148e91f5 | 3608 | if (ret < 0 || ret >= MAXPATHLEN) |
1354955b | 3609 | return -1; |
8058be39 | 3610 | if (!file_exists(path)) |
1354955b | 3611 | return 0; |
148e91f5 | 3612 | if (!(fout = fopen(path, "w"))) { |
959aee9c | 3613 | SYSERROR("unable to open %s: ignoring", path); |
1354955b | 3614 | return 0; |
148e91f5 | 3615 | } |
a684f0b7 ÇO |
3616 | if (fprintf(fout, "%s", c->name) < 0) { |
3617 | fclose(fout); | |
1354955b | 3618 | return -1; |
6849cb5b | 3619 | } |
148e91f5 | 3620 | if (fclose(fout) < 0) |
1354955b | 3621 | return -1; |
10bc1861 CB |
3622 | } else { |
3623 | storage_put(bdev); | |
9be53773 | 3624 | } |
e7de366c | 3625 | |
1354955b SH |
3626 | return 0; |
3627 | } | |
3628 | ||
3629 | static int clone_update_rootfs_wrapper(void *data) | |
3630 | { | |
3631 | struct clone_update_data *arg = (struct clone_update_data *) data; | |
3632 | return clone_update_rootfs(arg); | |
9be53773 SH |
3633 | } |
3634 | ||
3635 | /* | |
3636 | * We want to support: | |
3637 | sudo lxc-clone -o o1 -n n1 -s -L|-fssize fssize -v|--vgname vgname \ | |
3638 | -p|--lvprefix lvprefix -t|--fstype fstype -B backingstore | |
3639 | ||
ba115175 CB |
3640 | -s [ implies overlay] |
3641 | -s -B overlay | |
9be53773 SH |
3642 | |
3643 | only rootfs gets converted (copied/snapshotted) on clone. | |
3644 | */ | |
3645 | ||
d5752559 | 3646 | static int create_file_dirname(char *path, struct lxc_conf *conf) |
9be53773 | 3647 | { |
c32981c3 | 3648 | char *p = strrchr(path, '/'); |
d5752559 | 3649 | int ret = -1; |
9be53773 SH |
3650 | |
3651 | if (!p) | |
3652 | return -1; | |
3653 | *p = '\0'; | |
d028235d | 3654 | ret = do_create_container_dir(path, conf); |
9be53773 SH |
3655 | *p = '/'; |
3656 | return ret; | |
3657 | } | |
3658 | ||
858377e4 | 3659 | static struct lxc_container *do_lxcapi_clone(struct lxc_container *c, const char *newname, |
9be53773 | 3660 | const char *lxcpath, int flags, |
d659597e | 3661 | const char *bdevtype, const char *bdevdata, uint64_t newsize, |
148e91f5 | 3662 | char **hookargs) |
9be53773 | 3663 | { |
9be53773 | 3664 | char newpath[MAXPATHLEN]; |
0e1a60b0 | 3665 | int fd, ret; |
1354955b | 3666 | struct clone_update_data data; |
3b392519 | 3667 | size_t saved_unexp_len; |
1354955b | 3668 | pid_t pid; |
17a367d8 CB |
3669 | int storage_copied = 0; |
3670 | char *origroot = NULL, *saved_unexp_conf = NULL; | |
3671 | struct lxc_container *c2 = NULL; | |
9be53773 | 3672 | |
858377e4 | 3673 | if (!c || !do_lxcapi_is_defined(c)) |
9be53773 SH |
3674 | return NULL; |
3675 | ||
5cee8c50 | 3676 | if (container_mem_lock(c)) |
9be53773 SH |
3677 | return NULL; |
3678 | ||
39dc698c | 3679 | if (!is_stopped(c)) { |
9be53773 SH |
3680 | ERROR("error: Original container (%s) is running", c->name); |
3681 | goto out; | |
3682 | } | |
3683 | ||
1a0e70ac | 3684 | /* Make sure the container doesn't yet exist. */ |
05d53f4c SH |
3685 | if (!newname) |
3686 | newname = c->name; | |
3687 | if (!lxcpath) | |
858377e4 | 3688 | lxcpath = do_lxcapi_get_config_path(c); |
05d53f4c | 3689 | ret = snprintf(newpath, MAXPATHLEN, "%s/%s/config", lxcpath, newname); |
6849cb5b | 3690 | if (ret < 0 || ret >= MAXPATHLEN) { |
9be53773 SH |
3691 | SYSERROR("clone: failed making config pathname"); |
3692 | goto out; | |
3693 | } | |
17a367d8 | 3694 | |
9be53773 SH |
3695 | if (file_exists(newpath)) { |
3696 | ERROR("error: clone: %s exists", newpath); | |
3697 | goto out; | |
3698 | } | |
3699 | ||
d5752559 | 3700 | ret = create_file_dirname(newpath, c->lxc_conf); |
96532523 | 3701 | if (ret < 0 && errno != EEXIST) { |
9be53773 SH |
3702 | ERROR("Error creating container dir for %s", newpath); |
3703 | goto out; | |
3704 | } | |
3705 | ||
1a0e70ac | 3706 | /* Copy the configuration. Tweak it as needed. */ |
8d2efe40 SH |
3707 | if (c->lxc_conf->rootfs.path) { |
3708 | origroot = c->lxc_conf->rootfs.path; | |
3709 | c->lxc_conf->rootfs.path = NULL; | |
3710 | } | |
0e1a60b0 CB |
3711 | |
3712 | fd = open(newpath, O_WRONLY | O_CREAT | O_CLOEXEC, | |
3713 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); | |
3714 | if (fd < 0) { | |
3715 | SYSERROR("Failed to open \"%s\"", newpath); | |
9be53773 SH |
3716 | goto out; |
3717 | } | |
5eea90e8 SH |
3718 | |
3719 | saved_unexp_conf = c->lxc_conf->unexpanded_config; | |
3b392519 | 3720 | saved_unexp_len = c->lxc_conf->unexpanded_len; |
5eea90e8 SH |
3721 | c->lxc_conf->unexpanded_config = strdup(saved_unexp_conf); |
3722 | if (!c->lxc_conf->unexpanded_config) { | |
0e1a60b0 | 3723 | close(fd); |
5eea90e8 SH |
3724 | goto out; |
3725 | } | |
7a96a068 | 3726 | |
7a96a068 | 3727 | clear_unexp_config_line(c->lxc_conf, "lxc.rootfs.path", false); |
0e1a60b0 CB |
3728 | write_config(fd, c->lxc_conf); |
3729 | close(fd); | |
8d2efe40 | 3730 | c->lxc_conf->rootfs.path = origroot; |
5eea90e8 SH |
3731 | free(c->lxc_conf->unexpanded_config); |
3732 | c->lxc_conf->unexpanded_config = saved_unexp_conf; | |
3733 | saved_unexp_conf = NULL; | |
3b392519 | 3734 | c->lxc_conf->unexpanded_len = saved_unexp_len; |
9be53773 | 3735 | |
90b366fc CB |
3736 | ret = snprintf(newpath, MAXPATHLEN, "%s/%s/rootfs", lxcpath, newname); |
3737 | if (ret < 0 || ret >= MAXPATHLEN) { | |
3738 | SYSERROR("clone: failed making rootfs pathname"); | |
3739 | goto out; | |
3740 | } | |
17a367d8 CB |
3741 | |
3742 | ret = mkdir(newpath, 0755); | |
3743 | if (ret < 0) { | |
3744 | /* For an overlay container the rootfs is considered immutable | |
3745 | * and will not have been removed when restoring from a | |
3746 | * snapshot. | |
3747 | */ | |
3748 | if (errno != ENOENT && | |
3749 | !(flags & LXC_STORAGE_INTERNAL_OVERLAY_RESTORE)) { | |
3750 | SYSERROR("Failed to create directory \"%s\"", newpath); | |
3751 | goto out; | |
3752 | } | |
9be53773 SH |
3753 | } |
3754 | ||
e0010464 | 3755 | if (am_guest_unpriv()) { |
1354955b | 3756 | if (chown_mapped_root(newpath, c->lxc_conf) < 0) { |
959aee9c | 3757 | ERROR("Error chowning %s to container root", newpath); |
1354955b SH |
3758 | goto out; |
3759 | } | |
3760 | } | |
3761 | ||
05d53f4c | 3762 | c2 = lxc_container_new(newname, lxcpath); |
375c2258 | 3763 | if (!c2) { |
05d53f4c SH |
3764 | ERROR("clone: failed to create new container (%s %s)", newname, |
3765 | lxcpath); | |
9be53773 SH |
3766 | goto out; |
3767 | } | |
8d2efe40 | 3768 | |
1a0e70ac | 3769 | /* copy/snapshot rootfs's */ |
8d2efe40 SH |
3770 | ret = copy_storage(c, c2, bdevtype, flags, bdevdata, newsize); |
3771 | if (ret < 0) | |
3772 | goto out; | |
9be53773 | 3773 | |
6b0d5538 | 3774 | |
1a0e70ac | 3775 | /* update utsname */ |
3d7ad474 CB |
3776 | if (!(flags & LXC_CLONE_KEEPNAME)) { |
3777 | clear_unexp_config_line(c2->lxc_conf, "lxc.utsname", false); | |
b67771bc | 3778 | clear_unexp_config_line(c2->lxc_conf, "lxc.uts.name", false); |
3d7ad474 | 3779 | |
0d9cd9c3 | 3780 | if (!do_set_config_item_locked(c2, "lxc.uts.name", newname)) { |
3d7ad474 CB |
3781 | ERROR("Error setting new hostname"); |
3782 | goto out; | |
3783 | } | |
96532523 SH |
3784 | } |
3785 | ||
1a0e70ac | 3786 | /* copy hooks */ |
619256b5 ÇO |
3787 | ret = copyhooks(c, c2); |
3788 | if (ret < 0) { | |
3789 | ERROR("error copying hooks"); | |
3790 | goto out; | |
9be53773 SH |
3791 | } |
3792 | ||
3793 | if (copy_fstab(c, c2) < 0) { | |
3794 | ERROR("error copying fstab"); | |
9be53773 SH |
3795 | goto out; |
3796 | } | |
3797 | ||
1a0e70ac | 3798 | /* update macaddrs */ |
6b0d5538 | 3799 | if (!(flags & LXC_CLONE_KEEPMACADDR)) { |
67702c21 SH |
3800 | if (!network_new_hwaddrs(c2->lxc_conf)) { |
3801 | ERROR("Error updating mac addresses"); | |
6b0d5538 SH |
3802 | goto out; |
3803 | } | |
3804 | } | |
9be53773 | 3805 | |
1a0e70ac | 3806 | /* Update absolute paths for overlay mount directories. */ |
83e79752 | 3807 | if (ovl_update_abs_paths(c2->lxc_conf, c->config_path, c->name, lxcpath, newname) < 0) |
030ce9a9 CB |
3808 | goto out; |
3809 | ||
1a0e70ac CB |
3810 | /* We've now successfully created c2's storage, so clear it out if we |
3811 | * fail after this. | |
3812 | */ | |
176d9acb SH |
3813 | storage_copied = 1; |
3814 | ||
375c2258 | 3815 | if (!c2->save_config(c2, NULL)) |
9be53773 | 3816 | goto out; |
9be53773 | 3817 | |
1354955b SH |
3818 | if ((pid = fork()) < 0) { |
3819 | SYSERROR("fork"); | |
9be53773 | 3820 | goto out; |
1354955b SH |
3821 | } |
3822 | if (pid > 0) { | |
3823 | ret = wait_for_pid(pid); | |
3824 | if (ret) | |
3825 | goto out; | |
3826 | container_mem_unlock(c); | |
3827 | return c2; | |
3828 | } | |
3829 | data.c0 = c; | |
3830 | data.c1 = c2; | |
3831 | data.flags = flags; | |
3832 | data.hookargs = hookargs; | |
e0010464 | 3833 | if (am_guest_unpriv()) |
ee484f7f CB |
3834 | ret = userns_exec_full(c->lxc_conf, clone_update_rootfs_wrapper, |
3835 | &data, "clone_update_rootfs_wrapper"); | |
1354955b SH |
3836 | else |
3837 | ret = clone_update_rootfs(&data); | |
3838 | if (ret < 0) | |
d8480a31 | 3839 | _exit(EXIT_FAILURE); |
9be53773 | 3840 | |
5cee8c50 | 3841 | container_mem_unlock(c); |
d8480a31 | 3842 | _exit(EXIT_SUCCESS); |
9be53773 SH |
3843 | |
3844 | out: | |
5cee8c50 | 3845 | container_mem_unlock(c); |
375c2258 | 3846 | if (c2) { |
176d9acb SH |
3847 | if (!storage_copied) |
3848 | c2->lxc_conf->rootfs.path = NULL; | |
375c2258 | 3849 | c2->destroy(c2); |
9be53773 | 3850 | lxc_container_put(c2); |
375c2258 | 3851 | } |
9be53773 SH |
3852 | |
3853 | return NULL; | |
3854 | } | |
3855 | ||
858377e4 SH |
3856 | static struct lxc_container *lxcapi_clone(struct lxc_container *c, const char *newname, |
3857 | const char *lxcpath, int flags, | |
3858 | const char *bdevtype, const char *bdevdata, uint64_t newsize, | |
3859 | char **hookargs) | |
3860 | { | |
3861 | struct lxc_container * ret; | |
3862 | current_config = c ? c->lxc_conf : NULL; | |
3863 | ret = do_lxcapi_clone(c, newname, lxcpath, flags, bdevtype, bdevdata, newsize, hookargs); | |
3864 | current_config = NULL; | |
3865 | return ret; | |
3866 | } | |
3867 | ||
3868 | static bool do_lxcapi_rename(struct lxc_container *c, const char *newname) | |
06e5650e | 3869 | { |
10bc1861 | 3870 | struct lxc_storage *bdev; |
06e5650e | 3871 | struct lxc_container *newc; |
06e5650e | 3872 | |
d693cf93 | 3873 | if (!c || !c->name || !c->config_path || !c->lxc_conf) |
06e5650e ÇO |
3874 | return false; |
3875 | ||
18aa217b SH |
3876 | if (has_fs_snapshots(c) || has_snapshots(c)) { |
3877 | ERROR("Renaming a container with snapshots is not supported"); | |
3878 | return false; | |
3879 | } | |
8a388ed4 | 3880 | bdev = storage_init(c->lxc_conf); |
06e5650e ÇO |
3881 | if (!bdev) { |
3882 | ERROR("Failed to find original backing store type"); | |
3883 | return false; | |
3884 | } | |
3885 | ||
619256b5 | 3886 | newc = lxcapi_clone(c, newname, c->config_path, LXC_CLONE_KEEPMACADDR, NULL, bdev->type, 0, NULL); |
10bc1861 | 3887 | storage_put(bdev); |
06e5650e ÇO |
3888 | if (!newc) { |
3889 | lxc_container_put(newc); | |
3890 | return false; | |
3891 | } | |
3892 | ||
3893 | if (newc && lxcapi_is_defined(newc)) | |
3894 | lxc_container_put(newc); | |
3895 | ||
17a367d8 | 3896 | if (!container_destroy(c, NULL)) { |
06e5650e ÇO |
3897 | ERROR("Could not destroy existing container %s", c->name); |
3898 | return false; | |
3899 | } | |
3900 | return true; | |
3901 | } | |
3902 | ||
858377e4 SH |
3903 | WRAP_API_1(bool, lxcapi_rename, const char *) |
3904 | ||
a0e93eeb CS |
3905 | 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) |
3906 | { | |
858377e4 SH |
3907 | int ret; |
3908 | ||
a0e93eeb CS |
3909 | if (!c) |
3910 | return -1; | |
3911 | ||
858377e4 SH |
3912 | current_config = c->lxc_conf; |
3913 | ||
3914 | ret = lxc_attach(c->name, c->config_path, exec_function, exec_payload, options, attached_process); | |
3915 | current_config = NULL; | |
3916 | return ret; | |
a0e93eeb CS |
3917 | } |
3918 | ||
858377e4 | 3919 | 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 |
3920 | { |
3921 | lxc_attach_command_t command; | |
3922 | pid_t pid; | |
3923 | int r; | |
3924 | ||
3925 | if (!c) | |
3926 | return -1; | |
3927 | ||
3928 | command.program = (char*)program; | |
3929 | command.argv = (char**)argv; | |
3930 | r = lxc_attach(c->name, c->config_path, lxc_attach_run_command, &command, options, &pid); | |
3931 | if (r < 0) { | |
3932 | ERROR("ups"); | |
3933 | return r; | |
3934 | } | |
3935 | return lxc_wait_for_pid_status(pid); | |
3936 | } | |
3937 | ||
858377e4 SH |
3938 | static int lxcapi_attach_run_wait(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char * const argv[]) |
3939 | { | |
3940 | int ret; | |
3941 | current_config = c ? c->lxc_conf : NULL; | |
3942 | ret = do_lxcapi_attach_run_wait(c, options, program, argv); | |
3943 | current_config = NULL; | |
3944 | return ret; | |
3945 | } | |
3946 | ||
74a3920a | 3947 | static int get_next_index(const char *lxcpath, char *cname) |
f5dd1d53 SH |
3948 | { |
3949 | char *fname; | |
3950 | struct stat sb; | |
3951 | int i = 0, ret; | |
3952 | ||
3953 | fname = alloca(strlen(lxcpath) + 20); | |
3954 | while (1) { | |
3955 | sprintf(fname, "%s/snap%d", lxcpath, i); | |
3956 | ret = stat(fname, &sb); | |
3957 | if (ret != 0) | |
3958 | return i; | |
3959 | i++; | |
3960 | } | |
3961 | } | |
3962 | ||
18aa217b SH |
3963 | static bool get_snappath_dir(struct lxc_container *c, char *snappath) |
3964 | { | |
3965 | int ret; | |
3966 | /* | |
3967 | * If the old style snapshot path exists, use it | |
3968 | * /var/lib/lxc -> /var/lib/lxcsnaps | |
3969 | */ | |
3970 | ret = snprintf(snappath, MAXPATHLEN, "%ssnaps", c->config_path); | |
3971 | if (ret < 0 || ret >= MAXPATHLEN) | |
3972 | return false; | |
3973 | if (dir_exists(snappath)) { | |
3974 | ret = snprintf(snappath, MAXPATHLEN, "%ssnaps/%s", c->config_path, c->name); | |
3975 | if (ret < 0 || ret >= MAXPATHLEN) | |
3976 | return false; | |
3977 | return true; | |
3978 | } | |
3979 | ||
3980 | /* | |
3981 | * Use the new style path | |
3982 | * /var/lib/lxc -> /var/lib/lxc + c->name + /snaps + \0 | |
3983 | */ | |
3984 | ret = snprintf(snappath, MAXPATHLEN, "%s/%s/snaps", c->config_path, c->name); | |
3985 | if (ret < 0 || ret >= MAXPATHLEN) | |
3986 | return false; | |
3987 | return true; | |
3988 | } | |
3989 | ||
858377e4 | 3990 | static int do_lxcapi_snapshot(struct lxc_container *c, const char *commentfile) |
f5dd1d53 SH |
3991 | { |
3992 | int i, flags, ret; | |
3993 | struct lxc_container *c2; | |
3994 | char snappath[MAXPATHLEN], newname[20]; | |
3995 | ||
840f05df SH |
3996 | if (!c || !lxcapi_is_defined(c)) |
3997 | return -1; | |
3998 | ||
10bc1861 | 3999 | if (!storage_can_backup(c->lxc_conf)) { |
cdd01be2 SH |
4000 | ERROR("%s's backing store cannot be backed up.", c->name); |
4001 | ERROR("Your container must use another backing store type."); | |
4002 | return -1; | |
4003 | } | |
4004 | ||
18aa217b | 4005 | if (!get_snappath_dir(c, snappath)) |
f5dd1d53 | 4006 | return -1; |
18aa217b | 4007 | |
f5dd1d53 SH |
4008 | i = get_next_index(snappath, c->name); |
4009 | ||
4010 | if (mkdir_p(snappath, 0755) < 0) { | |
4011 | ERROR("Failed to create snapshot directory %s", snappath); | |
4012 | return -1; | |
4013 | } | |
4014 | ||
4015 | ret = snprintf(newname, 20, "snap%d", i); | |
4016 | if (ret < 0 || ret >= 20) | |
4017 | return -1; | |
4018 | ||
0a83cbbb SH |
4019 | /* |
4020 | * We pass LXC_CLONE_SNAPSHOT to make sure that a rdepends file entry is | |
4021 | * created in the original container | |
4022 | */ | |
4023 | flags = LXC_CLONE_SNAPSHOT | LXC_CLONE_KEEPMACADDR | LXC_CLONE_KEEPNAME | | |
4024 | LXC_CLONE_KEEPBDEVTYPE | LXC_CLONE_MAYBE_SNAPSHOT; | |
068aa488 | 4025 | if (storage_is_dir(c->lxc_conf)) { |
8c39f7a4 SH |
4026 | ERROR("Snapshot of directory-backed container requested."); |
4027 | ERROR("Making a copy-clone. If you do want snapshots, then"); | |
12e6ab5d | 4028 | ERROR("please create overlay clone first, snapshot that"); |
8c39f7a4 SH |
4029 | ERROR("and keep the original container pristine."); |
4030 | flags &= ~LXC_CLONE_SNAPSHOT | LXC_CLONE_MAYBE_SNAPSHOT; | |
4031 | } | |
858377e4 | 4032 | c2 = do_lxcapi_clone(c, newname, snappath, flags, NULL, NULL, 0, NULL); |
f5dd1d53 | 4033 | if (!c2) { |
959aee9c | 4034 | ERROR("clone of %s:%s failed", c->config_path, c->name); |
f5dd1d53 SH |
4035 | return -1; |
4036 | } | |
4037 | ||
4038 | lxc_container_put(c2); | |
4039 | ||
1a0e70ac | 4040 | /* Now write down the creation time. */ |
f5dd1d53 SH |
4041 | time_t timer; |
4042 | char buffer[25]; | |
4043 | struct tm* tm_info; | |
025ed0f3 | 4044 | FILE *f; |
f5dd1d53 SH |
4045 | |
4046 | time(&timer); | |
4047 | tm_info = localtime(&timer); | |
4048 | ||
4049 | strftime(buffer, 25, "%Y:%m:%d %H:%M:%S", tm_info); | |
4050 | ||
4051 | char *dfnam = alloca(strlen(snappath) + strlen(newname) + 5); | |
4052 | sprintf(dfnam, "%s/%s/ts", snappath, newname); | |
025ed0f3 | 4053 | f = fopen(dfnam, "w"); |
f5dd1d53 | 4054 | if (!f) { |
959aee9c | 4055 | ERROR("Failed to open %s", dfnam); |
f5dd1d53 SH |
4056 | return -1; |
4057 | } | |
4058 | if (fprintf(f, "%s", buffer) < 0) { | |
4059 | SYSERROR("Writing timestamp"); | |
4060 | fclose(f); | |
4061 | return -1; | |
4062 | } | |
025ed0f3 | 4063 | ret = fclose(f); |
025ed0f3 | 4064 | if (ret != 0) { |
f5dd1d53 SH |
4065 | SYSERROR("Writing timestamp"); |
4066 | return -1; | |
4067 | } | |
4068 | ||
4069 | if (commentfile) { | |
1a0e70ac | 4070 | /* $p / $name / comment \0 */ |
f5dd1d53 SH |
4071 | int len = strlen(snappath) + strlen(newname) + 10; |
4072 | char *path = alloca(len); | |
4073 | sprintf(path, "%s/%s/comment", snappath, newname); | |
4074 | return copy_file(commentfile, path) < 0 ? -1 : i; | |
4075 | } | |
4076 | ||
4077 | return i; | |
4078 | } | |
4079 | ||
858377e4 SH |
4080 | WRAP_API_1(int, lxcapi_snapshot, const char *) |
4081 | ||
f5dd1d53 SH |
4082 | static void lxcsnap_free(struct lxc_snapshot *s) |
4083 | { | |
f10fad2f ME |
4084 | free(s->name); |
4085 | free(s->comment_pathname); | |
4086 | free(s->timestamp); | |
4087 | free(s->lxcpath); | |
f5dd1d53 SH |
4088 | } |
4089 | ||
4090 | static char *get_snapcomment_path(char* snappath, char *name) | |
4091 | { | |
1a0e70ac | 4092 | /* $snappath/$name/comment */ |
f5dd1d53 SH |
4093 | int ret, len = strlen(snappath) + strlen(name) + 10; |
4094 | char *s = malloc(len); | |
4095 | ||
4096 | if (s) { | |
4097 | ret = snprintf(s, len, "%s/%s/comment", snappath, name); | |
4098 | if (ret < 0 || ret >= len) { | |
4099 | free(s); | |
4100 | s = NULL; | |
4101 | } | |
4102 | } | |
4103 | return s; | |
4104 | } | |
4105 | ||
4106 | static char *get_timestamp(char* snappath, char *name) | |
4107 | { | |
4108 | char path[MAXPATHLEN], *s = NULL; | |
4109 | int ret, len; | |
4110 | FILE *fin; | |
4111 | ||
4112 | ret = snprintf(path, MAXPATHLEN, "%s/%s/ts", snappath, name); | |
4113 | if (ret < 0 || ret >= MAXPATHLEN) | |
4114 | return NULL; | |
025ed0f3 | 4115 | fin = fopen(path, "r"); |
025ed0f3 | 4116 | if (!fin) |
f5dd1d53 SH |
4117 | return NULL; |
4118 | (void) fseek(fin, 0, SEEK_END); | |
4119 | len = ftell(fin); | |
4120 | (void) fseek(fin, 0, SEEK_SET); | |
4121 | if (len > 0) { | |
4122 | s = malloc(len+1); | |
4123 | if (s) { | |
4124 | s[len] = '\0'; | |
4125 | if (fread(s, 1, len, fin) != len) { | |
4126 | SYSERROR("reading timestamp"); | |
4127 | free(s); | |
4128 | s = NULL; | |
4129 | } | |
4130 | } | |
4131 | } | |
4132 | fclose(fin); | |
4133 | return s; | |
4134 | } | |
4135 | ||
858377e4 | 4136 | static int do_lxcapi_snapshot_list(struct lxc_container *c, struct lxc_snapshot **ret_snaps) |
f5dd1d53 SH |
4137 | { |
4138 | char snappath[MAXPATHLEN], path2[MAXPATHLEN]; | |
18aa217b | 4139 | int count = 0, ret; |
74f96976 | 4140 | struct dirent *direntp; |
f5dd1d53 SH |
4141 | struct lxc_snapshot *snaps =NULL, *nsnaps; |
4142 | DIR *dir; | |
4143 | ||
4144 | if (!c || !lxcapi_is_defined(c)) | |
4145 | return -1; | |
c868b261 | 4146 | |
18aa217b | 4147 | if (!get_snappath_dir(c, snappath)) { |
f5dd1d53 SH |
4148 | ERROR("path name too long"); |
4149 | return -1; | |
4150 | } | |
025ed0f3 | 4151 | dir = opendir(snappath); |
025ed0f3 | 4152 | if (!dir) { |
f5dd1d53 SH |
4153 | INFO("failed to open %s - assuming no snapshots", snappath); |
4154 | return 0; | |
4155 | } | |
4156 | ||
74f96976 | 4157 | while ((direntp = readdir(dir))) { |
f5dd1d53 SH |
4158 | if (!strcmp(direntp->d_name, ".")) |
4159 | continue; | |
4160 | ||
4161 | if (!strcmp(direntp->d_name, "..")) | |
4162 | continue; | |
4163 | ||
4164 | ret = snprintf(path2, MAXPATHLEN, "%s/%s/config", snappath, direntp->d_name); | |
4165 | if (ret < 0 || ret >= MAXPATHLEN) { | |
4166 | ERROR("pathname too long"); | |
4167 | goto out_free; | |
4168 | } | |
4169 | if (!file_exists(path2)) | |
4170 | continue; | |
4171 | nsnaps = realloc(snaps, (count + 1)*sizeof(*snaps)); | |
4172 | if (!nsnaps) { | |
4173 | SYSERROR("Out of memory"); | |
4174 | goto out_free; | |
4175 | } | |
4176 | snaps = nsnaps; | |
4177 | snaps[count].free = lxcsnap_free; | |
4178 | snaps[count].name = strdup(direntp->d_name); | |
4179 | if (!snaps[count].name) | |
4180 | goto out_free; | |
4181 | snaps[count].lxcpath = strdup(snappath); | |
4182 | if (!snaps[count].lxcpath) { | |
4183 | free(snaps[count].name); | |
4184 | goto out_free; | |
4185 | } | |
4186 | snaps[count].comment_pathname = get_snapcomment_path(snappath, direntp->d_name); | |
4187 | snaps[count].timestamp = get_timestamp(snappath, direntp->d_name); | |
4188 | count++; | |
4189 | } | |
4190 | ||
4191 | if (closedir(dir)) | |
4192 | WARN("failed to close directory"); | |
4193 | ||
4194 | *ret_snaps = snaps; | |
4195 | return count; | |
4196 | ||
4197 | out_free: | |
4198 | if (snaps) { | |
4199 | int i; | |
4200 | for (i=0; i<count; i++) | |
4201 | lxcsnap_free(&snaps[i]); | |
4202 | free(snaps); | |
4203 | } | |
9baa57bd SH |
4204 | if (closedir(dir)) |
4205 | WARN("failed to close directory"); | |
f5dd1d53 SH |
4206 | return -1; |
4207 | } | |
4208 | ||
858377e4 SH |
4209 | WRAP_API_1(int, lxcapi_snapshot_list, struct lxc_snapshot **) |
4210 | ||
4211 | static bool do_lxcapi_snapshot_restore(struct lxc_container *c, const char *snapname, const char *newname) | |
f5dd1d53 SH |
4212 | { |
4213 | char clonelxcpath[MAXPATHLEN]; | |
18aa217b | 4214 | int flags = 0; |
f5dd1d53 | 4215 | struct lxc_container *snap, *rest; |
10bc1861 | 4216 | struct lxc_storage *bdev; |
f5dd1d53 SH |
4217 | bool b = false; |
4218 | ||
4219 | if (!c || !c->name || !c->config_path) | |
4220 | return false; | |
4221 | ||
18aa217b SH |
4222 | if (has_fs_snapshots(c)) { |
4223 | ERROR("container rootfs has dependent snapshots"); | |
4224 | return false; | |
4225 | } | |
4226 | ||
8a388ed4 | 4227 | bdev = storage_init(c->lxc_conf); |
f5dd1d53 SH |
4228 | if (!bdev) { |
4229 | ERROR("Failed to find original backing store type"); | |
4230 | return false; | |
4231 | } | |
4232 | ||
17a367d8 CB |
4233 | /* For an overlay container the rootfs is considered immutable |
4234 | * and cannot be removed when restoring from a snapshot. We pass this | |
4235 | * internal flag along to communicate this to various parts of the | |
4236 | * codebase. | |
4237 | */ | |
4238 | if (!strcmp(bdev->type, "overlay") || !strcmp(bdev->type, "overlayfs")) | |
4239 | bdev->flags |= LXC_STORAGE_INTERNAL_OVERLAY_RESTORE; | |
4240 | ||
f5dd1d53 SH |
4241 | if (!newname) |
4242 | newname = c->name; | |
7e36f87e | 4243 | |
18aa217b | 4244 | if (!get_snappath_dir(c, clonelxcpath)) { |
10bc1861 | 4245 | storage_put(bdev); |
f5dd1d53 SH |
4246 | return false; |
4247 | } | |
1a0e70ac | 4248 | /* how should we lock this? */ |
f5dd1d53 SH |
4249 | |
4250 | snap = lxc_container_new(snapname, clonelxcpath); | |
4251 | if (!snap || !lxcapi_is_defined(snap)) { | |
4252 | ERROR("Could not open snapshot %s", snapname); | |
17a367d8 CB |
4253 | if (snap) |
4254 | lxc_container_put(snap); | |
10bc1861 | 4255 | storage_put(bdev); |
f5dd1d53 SH |
4256 | return false; |
4257 | } | |
4258 | ||
17a367d8 CB |
4259 | if (!strcmp(c->name, newname)) { |
4260 | if (!container_destroy(c, bdev)) { | |
7e36f87e ÇO |
4261 | ERROR("Could not destroy existing container %s", newname); |
4262 | lxc_container_put(snap); | |
10bc1861 | 4263 | storage_put(bdev); |
7e36f87e ÇO |
4264 | return false; |
4265 | } | |
4266 | } | |
4267 | ||
de269ee8 SH |
4268 | if (strcmp(bdev->type, "dir") != 0 && strcmp(bdev->type, "loop") != 0) |
4269 | flags = LXC_CLONE_SNAPSHOT | LXC_CLONE_MAYBE_SNAPSHOT; | |
17a367d8 CB |
4270 | |
4271 | if (!strcmp(bdev->type, "overlay") || !strcmp(bdev->type, "overlayfs")) | |
4272 | flags |= LXC_STORAGE_INTERNAL_OVERLAY_RESTORE; | |
09f6f8c4 CB |
4273 | rest = lxcapi_clone(snap, newname, c->config_path, flags, bdev->type, |
4274 | NULL, 0, NULL); | |
10bc1861 | 4275 | storage_put(bdev); |
f5dd1d53 SH |
4276 | if (rest && lxcapi_is_defined(rest)) |
4277 | b = true; | |
4278 | if (rest) | |
4279 | lxc_container_put(rest); | |
17a367d8 | 4280 | |
f5dd1d53 SH |
4281 | lxc_container_put(snap); |
4282 | return b; | |
4283 | } | |
4284 | ||
858377e4 SH |
4285 | WRAP_API_2(bool, lxcapi_snapshot_restore, const char *, const char *) |
4286 | ||
18aa217b | 4287 | static bool do_snapshot_destroy(const char *snapname, const char *clonelxcpath) |
771d96b3 | 4288 | { |
771d96b3 | 4289 | struct lxc_container *snap = NULL; |
18aa217b | 4290 | bool bret = false; |
771d96b3 ÇO |
4291 | |
4292 | snap = lxc_container_new(snapname, clonelxcpath); | |
18aa217b | 4293 | if (!snap) { |
771d96b3 ÇO |
4294 | ERROR("Could not find snapshot %s", snapname); |
4295 | goto err; | |
4296 | } | |
4297 | ||
858377e4 | 4298 | if (!do_lxcapi_destroy(snap)) { |
771d96b3 ÇO |
4299 | ERROR("Could not destroy snapshot %s", snapname); |
4300 | goto err; | |
4301 | } | |
18aa217b | 4302 | bret = true; |
771d96b3 | 4303 | |
771d96b3 ÇO |
4304 | err: |
4305 | if (snap) | |
4306 | lxc_container_put(snap); | |
18aa217b SH |
4307 | return bret; |
4308 | } | |
4309 | ||
4310 | static bool remove_all_snapshots(const char *path) | |
4311 | { | |
4312 | DIR *dir; | |
74f96976 | 4313 | struct dirent *direntp; |
18aa217b SH |
4314 | bool bret = true; |
4315 | ||
4316 | dir = opendir(path); | |
4317 | if (!dir) { | |
4318 | SYSERROR("opendir on snapshot path %s", path); | |
4319 | return false; | |
4320 | } | |
74f96976 | 4321 | while ((direntp = readdir(dir))) { |
18aa217b SH |
4322 | if (!strcmp(direntp->d_name, ".")) |
4323 | continue; | |
4324 | if (!strcmp(direntp->d_name, "..")) | |
4325 | continue; | |
4326 | if (!do_snapshot_destroy(direntp->d_name, path)) { | |
4327 | bret = false; | |
4328 | continue; | |
4329 | } | |
4330 | } | |
4331 | ||
4332 | closedir(dir); | |
4333 | ||
4334 | if (rmdir(path)) | |
4335 | SYSERROR("Error removing directory %s", path); | |
4336 | ||
4337 | return bret; | |
4338 | } | |
4339 | ||
858377e4 | 4340 | static bool do_lxcapi_snapshot_destroy(struct lxc_container *c, const char *snapname) |
18aa217b SH |
4341 | { |
4342 | char clonelxcpath[MAXPATHLEN]; | |
4343 | ||
4344 | if (!c || !c->name || !c->config_path || !snapname) | |
4345 | return false; | |
4346 | ||
4347 | if (!get_snappath_dir(c, clonelxcpath)) | |
4348 | return false; | |
4349 | ||
4350 | return do_snapshot_destroy(snapname, clonelxcpath); | |
4351 | } | |
4352 | ||
858377e4 SH |
4353 | WRAP_API_1(bool, lxcapi_snapshot_destroy, const char *) |
4354 | ||
4355 | static bool do_lxcapi_snapshot_destroy_all(struct lxc_container *c) | |
18aa217b SH |
4356 | { |
4357 | char clonelxcpath[MAXPATHLEN]; | |
4358 | ||
4359 | if (!c || !c->name || !c->config_path) | |
4360 | return false; | |
4361 | ||
4362 | if (!get_snappath_dir(c, clonelxcpath)) | |
4363 | return false; | |
4364 | ||
4365 | return remove_all_snapshots(clonelxcpath); | |
771d96b3 ÇO |
4366 | } |
4367 | ||
858377e4 SH |
4368 | WRAP_API(bool, lxcapi_snapshot_destroy_all) |
4369 | ||
4370 | static bool do_lxcapi_may_control(struct lxc_container *c) | |
b494d2dd SH |
4371 | { |
4372 | return lxc_try_cmd(c->name, c->config_path) == 0; | |
4373 | } | |
4374 | ||
858377e4 SH |
4375 | WRAP_API(bool, lxcapi_may_control) |
4376 | ||
d5aa23e6 | 4377 | static bool do_add_remove_node(pid_t init_pid, const char *path, bool add, |
b69dfc9f | 4378 | struct stat *st) |
d5aa23e6 | 4379 | { |
b69dfc9f CB |
4380 | int ret; |
4381 | char *tmp; | |
4382 | pid_t pid; | |
d5aa23e6 SH |
4383 | char chrootpath[MAXPATHLEN]; |
4384 | char *directory_path = NULL; | |
d5aa23e6 | 4385 | |
b69dfc9f CB |
4386 | pid = fork(); |
4387 | if (pid < 0) { | |
4388 | SYSERROR("Failed to fork()"); | |
d5aa23e6 SH |
4389 | return false; |
4390 | } | |
b69dfc9f | 4391 | |
d5aa23e6 | 4392 | if (pid) { |
b69dfc9f CB |
4393 | ret = wait_for_pid(pid); |
4394 | if (ret != 0) { | |
4395 | ERROR("Failed to create device node"); | |
d5aa23e6 SH |
4396 | return false; |
4397 | } | |
b69dfc9f | 4398 | |
d5aa23e6 SH |
4399 | return true; |
4400 | } | |
4401 | ||
4402 | /* prepare the path */ | |
4403 | ret = snprintf(chrootpath, MAXPATHLEN, "/proc/%d/root", init_pid); | |
4404 | if (ret < 0 || ret >= MAXPATHLEN) | |
4405 | return false; | |
4406 | ||
b69dfc9f CB |
4407 | ret = chroot(chrootpath); |
4408 | if (ret < 0) | |
a7764ce7 | 4409 | _exit(EXIT_FAILURE); |
b69dfc9f CB |
4410 | |
4411 | ret = chdir("/"); | |
4412 | if (ret < 0) | |
a7764ce7 | 4413 | _exit(EXIT_FAILURE); |
b69dfc9f | 4414 | |
d5aa23e6 | 4415 | /* remove path if it exists */ |
b69dfc9f CB |
4416 | ret = faccessat(AT_FDCWD, path, F_OK, AT_SYMLINK_NOFOLLOW); |
4417 | if(ret == 0) { | |
4418 | ret = unlink(path); | |
c7d76c09 | 4419 | if (ret < 0) { |
6d1400b5 | 4420 | SYSERROR("Failed to remove \"%s\"", path); |
a7764ce7 | 4421 | _exit(EXIT_FAILURE); |
c7d76c09 | 4422 | } |
d5aa23e6 | 4423 | } |
b69dfc9f | 4424 | |
d5aa23e6 | 4425 | if (!add) |
a7764ce7 | 4426 | _exit(EXIT_SUCCESS); |
d5aa23e6 SH |
4427 | |
4428 | /* create any missing directories */ | |
b69dfc9f CB |
4429 | tmp = strdup(path); |
4430 | if (!tmp) | |
a7764ce7 | 4431 | _exit(EXIT_FAILURE); |
b69dfc9f CB |
4432 | |
4433 | directory_path = dirname(tmp); | |
4434 | ret = mkdir_p(directory_path, 0755); | |
4435 | if (ret < 0 && errno != EEXIST) { | |
6d1400b5 | 4436 | SYSERROR("Failed to create path \"%s\"", directory_path); |
b69dfc9f | 4437 | free(tmp); |
a7764ce7 | 4438 | _exit(EXIT_FAILURE); |
d5aa23e6 SH |
4439 | } |
4440 | ||
4441 | /* create the device node */ | |
b69dfc9f CB |
4442 | ret = mknod(path, st->st_mode, st->st_rdev); |
4443 | free(tmp); | |
c7d76c09 | 4444 | if (ret < 0) { |
6d1400b5 | 4445 | SYSERROR("Failed to create device node at \"%s\"", path); |
a7764ce7 | 4446 | _exit(EXIT_FAILURE); |
c7d76c09 | 4447 | } |
d5aa23e6 | 4448 | |
a7764ce7 | 4449 | _exit(EXIT_SUCCESS); |
d5aa23e6 SH |
4450 | } |
4451 | ||
f0ca2726 | 4452 | static bool add_remove_device_node(struct lxc_container *c, const char *src_path, const char *dest_path, bool add) |
a9a0ed90 ÇO |
4453 | { |
4454 | int ret; | |
4455 | struct stat st; | |
238b3e5e | 4456 | char value[LXC_MAX_BUFFER]; |
f0ca2726 | 4457 | const char *p; |
a9a0ed90 ÇO |
4458 | |
4459 | /* make sure container is running */ | |
858377e4 | 4460 | if (!do_lxcapi_is_running(c)) { |
a9a0ed90 | 4461 | ERROR("container is not running"); |
d5aa23e6 | 4462 | return false; |
a9a0ed90 ÇO |
4463 | } |
4464 | ||
4465 | /* use src_path if dest_path is NULL otherwise use dest_path */ | |
4466 | p = dest_path ? dest_path : src_path; | |
4467 | ||
a9a0ed90 ÇO |
4468 | /* make sure we can access p */ |
4469 | if(access(p, F_OK) < 0 || stat(p, &st) < 0) | |
d5aa23e6 | 4470 | return false; |
a9a0ed90 ÇO |
4471 | |
4472 | /* continue if path is character device or block device */ | |
c6a9b0d7 | 4473 | if (S_ISCHR(st.st_mode)) |
238b3e5e | 4474 | ret = snprintf(value, LXC_MAX_BUFFER, "c %d:%d rwm", major(st.st_rdev), minor(st.st_rdev)); |
c6a9b0d7 | 4475 | else if (S_ISBLK(st.st_mode)) |
238b3e5e | 4476 | ret = snprintf(value, LXC_MAX_BUFFER, "b %d:%d rwm", major(st.st_rdev), minor(st.st_rdev)); |
a9a0ed90 | 4477 | else |
d5aa23e6 | 4478 | return false; |
a9a0ed90 ÇO |
4479 | |
4480 | /* check snprintf return code */ | |
238b3e5e | 4481 | if (ret < 0 || ret >= LXC_MAX_BUFFER) |
d5aa23e6 | 4482 | return false; |
a9a0ed90 | 4483 | |
858377e4 | 4484 | if (!do_add_remove_node(do_lxcapi_init_pid(c), p, add, &st)) |
d5aa23e6 | 4485 | return false; |
a9a0ed90 | 4486 | |
d5aa23e6 | 4487 | /* add or remove device to/from cgroup access list */ |
a9a0ed90 | 4488 | if (add) { |
858377e4 | 4489 | if (!do_lxcapi_set_cgroup_item(c, "devices.allow", value)) { |
a9a0ed90 | 4490 | ERROR("set_cgroup_item failed while adding the device node"); |
d5aa23e6 | 4491 | return false; |
a9a0ed90 ÇO |
4492 | } |
4493 | } else { | |
858377e4 | 4494 | if (!do_lxcapi_set_cgroup_item(c, "devices.deny", value)) { |
a9a0ed90 | 4495 | ERROR("set_cgroup_item failed while removing the device node"); |
d5aa23e6 | 4496 | return false; |
a9a0ed90 ÇO |
4497 | } |
4498 | } | |
d5aa23e6 | 4499 | |
a9a0ed90 | 4500 | return true; |
a9a0ed90 ÇO |
4501 | } |
4502 | ||
858377e4 | 4503 | static bool do_lxcapi_add_device_node(struct lxc_container *c, const char *src_path, const char *dest_path) |
a9a0ed90 | 4504 | { |
e0010464 | 4505 | // cannot mknod if we're not privileged wrt init_user_ns |
5384e99d | 4506 | if (am_host_unpriv()) { |
238b3e5e | 4507 | ERROR(LXC_UNPRIV_EOPNOTSUPP, __FUNCTION__); |
c868b261 ÇO |
4508 | return false; |
4509 | } | |
a9a0ed90 ÇO |
4510 | return add_remove_device_node(c, src_path, dest_path, true); |
4511 | } | |
4512 | ||
858377e4 SH |
4513 | WRAP_API_2(bool, lxcapi_add_device_node, const char *, const char *) |
4514 | ||
4515 | static bool do_lxcapi_remove_device_node(struct lxc_container *c, const char *src_path, const char *dest_path) | |
a9a0ed90 | 4516 | { |
e0010464 | 4517 | if (am_guest_unpriv()) { |
238b3e5e | 4518 | ERROR(LXC_UNPRIV_EOPNOTSUPP, __FUNCTION__); |
c868b261 ÇO |
4519 | return false; |
4520 | } | |
a9a0ed90 ÇO |
4521 | return add_remove_device_node(c, src_path, dest_path, false); |
4522 | } | |
4523 | ||
858377e4 SH |
4524 | WRAP_API_2(bool, lxcapi_remove_device_node, const char *, const char *) |
4525 | ||
c7d76c09 CB |
4526 | static bool do_lxcapi_attach_interface(struct lxc_container *c, |
4527 | const char *ifname, | |
4528 | const char *dst_ifname) | |
e58fae8f | 4529 | { |
c7d76c09 | 4530 | pid_t init_pid; |
e58fae8f | 4531 | int ret = 0; |
c7d76c09 | 4532 | |
e0010464 | 4533 | if (am_guest_unpriv()) { |
238b3e5e | 4534 | ERROR(LXC_UNPRIV_EOPNOTSUPP, __FUNCTION__); |
e58fae8f DY |
4535 | return false; |
4536 | } | |
4537 | ||
4538 | if (!ifname) { | |
4539 | ERROR("No source interface name given"); | |
4540 | return false; | |
4541 | } | |
4542 | ||
4543 | ret = lxc_netdev_isup(ifname); | |
e5848d39 SH |
4544 | if (ret > 0) { |
4545 | /* netdev of ifname is up. */ | |
e58fae8f DY |
4546 | ret = lxc_netdev_down(ifname); |
4547 | if (ret) | |
4548 | goto err; | |
4549 | } | |
4550 | ||
c7d76c09 CB |
4551 | init_pid = do_lxcapi_init_pid(c); |
4552 | ret = lxc_netdev_move_by_name(ifname, init_pid, dst_ifname); | |
e58fae8f DY |
4553 | if (ret) |
4554 | goto err; | |
4555 | ||
c7d76c09 | 4556 | INFO("Moved network device \"%s\" to network namespace of %d", ifname, init_pid); |
e58fae8f | 4557 | return true; |
e5848d39 | 4558 | |
e58fae8f | 4559 | err: |
e58fae8f DY |
4560 | return false; |
4561 | } | |
4562 | ||
858377e4 SH |
4563 | WRAP_API_2(bool, lxcapi_attach_interface, const char *, const char *) |
4564 | ||
c7d76c09 CB |
4565 | static bool do_lxcapi_detach_interface(struct lxc_container *c, |
4566 | const char *ifname, | |
4567 | const char *dst_ifname) | |
e58fae8f | 4568 | { |
c7d76c09 | 4569 | int ret; |
e58fae8f DY |
4570 | pid_t pid, pid_outside; |
4571 | ||
e0010464 SH |
4572 | /* |
4573 | * TODO - if this is a physical device, then we need am_host_unpriv. | |
4574 | * But for other types guest privilege suffices. | |
4575 | */ | |
4576 | if (am_guest_unpriv()) { | |
238b3e5e | 4577 | ERROR(LXC_UNPRIV_EOPNOTSUPP, __FUNCTION__); |
e58fae8f DY |
4578 | return false; |
4579 | } | |
4580 | ||
4581 | if (!ifname) { | |
4582 | ERROR("No source interface name given"); | |
4583 | return false; | |
4584 | } | |
4585 | ||
0059379f | 4586 | pid_outside = lxc_raw_getpid(); |
e58fae8f DY |
4587 | pid = fork(); |
4588 | if (pid < 0) { | |
c7d76c09 | 4589 | ERROR("Failed to fork"); |
e58fae8f DY |
4590 | return false; |
4591 | } | |
4592 | ||
1a0e70ac | 4593 | if (pid == 0) { /* child */ |
acbfeda8 CB |
4594 | pid_t init_pid; |
4595 | ||
4596 | init_pid = do_lxcapi_init_pid(c); | |
4597 | if (!switch_to_ns(init_pid, "net")) { | |
4598 | ERROR("Failed to enter network namespace"); | |
8d7b6c25 | 4599 | _exit(EXIT_FAILURE); |
e58fae8f DY |
4600 | } |
4601 | ||
4602 | ret = lxc_netdev_isup(ifname); | |
c7d76c09 CB |
4603 | if (ret < 0) { |
4604 | ERROR("Failed to determine whether network device \"%s\" is up", ifname); | |
8d7b6c25 | 4605 | _exit(EXIT_FAILURE); |
c7d76c09 | 4606 | } |
e58fae8f DY |
4607 | |
4608 | /* netdev of ifname is up. */ | |
4609 | if (ret) { | |
4610 | ret = lxc_netdev_down(ifname); | |
c7d76c09 CB |
4611 | if (ret) { |
4612 | ERROR("Failed to set network device \"%s\" down", ifname); | |
8d7b6c25 | 4613 | _exit(EXIT_FAILURE); |
c7d76c09 | 4614 | } |
e58fae8f DY |
4615 | } |
4616 | ||
4617 | ret = lxc_netdev_move_by_name(ifname, pid_outside, dst_ifname); | |
c7d76c09 CB |
4618 | /* -EINVAL means there is no netdev named as ifname. */ |
4619 | if (ret < 0) { | |
4620 | if (ret == -EINVAL) | |
4621 | ERROR("Network device \"%s\" not found", ifname); | |
4622 | else | |
4623 | ERROR("Failed to remove network device \"%s\"", ifname); | |
8d7b6c25 | 4624 | _exit(EXIT_FAILURE); |
e58fae8f | 4625 | } |
c7d76c09 | 4626 | |
8d7b6c25 | 4627 | _exit(EXIT_SUCCESS); |
e58fae8f DY |
4628 | } |
4629 | ||
c7d76c09 CB |
4630 | ret = wait_for_pid(pid); |
4631 | if (ret != 0) | |
e58fae8f DY |
4632 | return false; |
4633 | ||
c7d76c09 | 4634 | INFO("Moved network device \"%s\" to network namespace of %d", ifname, pid_outside); |
e58fae8f DY |
4635 | return true; |
4636 | } | |
4637 | ||
858377e4 SH |
4638 | WRAP_API_2(bool, lxcapi_detach_interface, const char *, const char *) |
4639 | ||
aef3d51e TA |
4640 | static int do_lxcapi_migrate(struct lxc_container *c, unsigned int cmd, |
4641 | struct migrate_opts *opts, unsigned int size) | |
bbd4e13e | 4642 | { |
6b9be523 | 4643 | int ret = -1; |
2cb80427 | 4644 | struct migrate_opts *valid_opts = opts; |
b5b12b9e | 4645 | uint64_t features_to_check = 0; |
85c50991 | 4646 | |
aef3d51e TA |
4647 | /* If the caller has a bigger (newer) struct migrate_opts, let's make |
4648 | * sure that the stuff on the end is zero, i.e. that they didn't ask us | |
4649 | * to do anything special. | |
4650 | */ | |
4651 | if (size > sizeof(*opts)) { | |
4652 | unsigned char *addr; | |
4653 | unsigned char *end; | |
4654 | ||
4655 | addr = (void *)opts + sizeof(*opts); | |
4656 | end = (void *)opts + size; | |
4657 | for (; addr < end; addr++) { | |
4658 | if (*addr) { | |
4659 | return -E2BIG; | |
4660 | } | |
4661 | } | |
85c50991 TA |
4662 | } |
4663 | ||
2cb80427 TA |
4664 | /* If the caller has a smaller struct, let's zero out the end for them |
4665 | * so we don't accidentally use bits of it that they didn't know about | |
4666 | * to initialize. | |
4667 | */ | |
4668 | if (size < sizeof(*opts)) { | |
4669 | valid_opts = malloc(sizeof(*opts)); | |
4670 | if (!valid_opts) | |
4671 | return -ENOMEM; | |
4672 | ||
4673 | memset(valid_opts, 0, sizeof(*opts)); | |
4674 | memcpy(valid_opts, opts, size); | |
4675 | } | |
4676 | ||
aef3d51e TA |
4677 | switch (cmd) { |
4678 | case MIGRATE_PRE_DUMP: | |
7ad13c91 TA |
4679 | if (!do_lxcapi_is_running(c)) { |
4680 | ERROR("container is not running"); | |
6b9be523 | 4681 | goto on_error; |
7ad13c91 | 4682 | } |
2cb80427 | 4683 | ret = !__criu_pre_dump(c, valid_opts); |
aef3d51e TA |
4684 | break; |
4685 | case MIGRATE_DUMP: | |
7ad13c91 TA |
4686 | if (!do_lxcapi_is_running(c)) { |
4687 | ERROR("container is not running"); | |
6b9be523 | 4688 | goto on_error; |
7ad13c91 | 4689 | } |
2cb80427 | 4690 | ret = !__criu_dump(c, valid_opts); |
aef3d51e TA |
4691 | break; |
4692 | case MIGRATE_RESTORE: | |
7ad13c91 TA |
4693 | if (do_lxcapi_is_running(c)) { |
4694 | ERROR("container is already running"); | |
6b9be523 | 4695 | goto on_error; |
7ad13c91 | 4696 | } |
2cb80427 | 4697 | ret = !__criu_restore(c, valid_opts); |
aef3d51e | 4698 | break; |
b5b12b9e AR |
4699 | case MIGRATE_FEATURE_CHECK: |
4700 | features_to_check = valid_opts->features_to_check; | |
4701 | ret = !__criu_check_feature(&features_to_check); | |
4702 | if (ret) { | |
4703 | /* Something went wrong. Let's let the caller | |
4704 | * know which feature checks failed. */ | |
4705 | valid_opts->features_to_check = features_to_check; | |
4706 | } | |
4707 | break; | |
aef3d51e TA |
4708 | default: |
4709 | ERROR("invalid migrate command %u", cmd); | |
4710 | ret = -EINVAL; | |
4711 | } | |
735f2c6e | 4712 | |
6b9be523 | 4713 | on_error: |
f686506d TA |
4714 | if (size < sizeof(*opts)) |
4715 | free(valid_opts); | |
4716 | ||
aef3d51e TA |
4717 | return ret; |
4718 | } | |
735f2c6e | 4719 | |
aef3d51e | 4720 | WRAP_API_3(int, lxcapi_migrate, unsigned int, struct migrate_opts *, unsigned int) |
735f2c6e | 4721 | |
aef3d51e TA |
4722 | static bool do_lxcapi_checkpoint(struct lxc_container *c, char *directory, bool stop, bool verbose) |
4723 | { | |
ebb088e1 AR |
4724 | struct migrate_opts opts; |
4725 | ||
4726 | memset(&opts, 0, sizeof(opts)); | |
4727 | ||
4728 | opts.directory = directory; | |
4729 | opts.stop = stop; | |
4730 | opts.verbose = verbose; | |
735f2c6e | 4731 | |
aef3d51e | 4732 | return !do_lxcapi_migrate(c, MIGRATE_DUMP, &opts, sizeof(opts)); |
735f2c6e TA |
4733 | } |
4734 | ||
858377e4 SH |
4735 | WRAP_API_3(bool, lxcapi_checkpoint, char *, bool, bool) |
4736 | ||
4737 | static bool do_lxcapi_restore(struct lxc_container *c, char *directory, bool verbose) | |
c9d8f2ee | 4738 | { |
ebb088e1 AR |
4739 | struct migrate_opts opts; |
4740 | ||
4741 | memset(&opts, 0, sizeof(opts)); | |
4742 | ||
4743 | opts.directory = directory; | |
4744 | opts.verbose = verbose; | |
c9d8f2ee | 4745 | |
aef3d51e | 4746 | return !do_lxcapi_migrate(c, MIGRATE_RESTORE, &opts, sizeof(opts)); |
735f2c6e TA |
4747 | } |
4748 | ||
858377e4 SH |
4749 | WRAP_API_2(bool, lxcapi_restore, char *, bool) |
4750 | ||
a0e93eeb CS |
4751 | static int lxcapi_attach_run_waitl(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char *arg, ...) |
4752 | { | |
4753 | va_list ap; | |
4754 | const char **argv; | |
4755 | int ret; | |
4756 | ||
4757 | if (!c) | |
4758 | return -1; | |
4759 | ||
858377e4 SH |
4760 | current_config = c->lxc_conf; |
4761 | ||
a0e93eeb CS |
4762 | va_start(ap, arg); |
4763 | argv = lxc_va_arg_list_to_argv_const(ap, 1); | |
4764 | va_end(ap); | |
4765 | ||
4766 | if (!argv) { | |
4767 | ERROR("Memory allocation error."); | |
858377e4 SH |
4768 | ret = -1; |
4769 | goto out; | |
a0e93eeb CS |
4770 | } |
4771 | argv[0] = arg; | |
4772 | ||
858377e4 | 4773 | ret = do_lxcapi_attach_run_wait(c, options, program, (const char * const *)argv); |
a0e93eeb | 4774 | free((void*)argv); |
858377e4 SH |
4775 | out: |
4776 | current_config = NULL; | |
a0e93eeb CS |
4777 | return ret; |
4778 | } | |
4779 | ||
afeecbba | 4780 | struct lxc_container *lxc_container_new(const char *name, const char *configpath) |
72d0e1cb SG |
4781 | { |
4782 | struct lxc_container *c; | |
cbb9c7c7 | 4783 | size_t len; |
72d0e1cb | 4784 | |
18aa217b SH |
4785 | if (!name) |
4786 | return NULL; | |
4787 | ||
72d0e1cb SG |
4788 | c = malloc(sizeof(*c)); |
4789 | if (!c) { | |
e9e29a33 | 4790 | fprintf(stderr, "Failed to allocate memory for %s\n", name); |
72d0e1cb SG |
4791 | return NULL; |
4792 | } | |
4793 | memset(c, 0, sizeof(*c)); | |
4794 | ||
afeecbba SH |
4795 | if (configpath) |
4796 | c->config_path = strdup(configpath); | |
4797 | else | |
593e8478 | 4798 | c->config_path = strdup(lxc_global_config_value("lxc.lxcpath")); |
afeecbba | 4799 | |
2a59a681 | 4800 | if (!c->config_path) { |
e9e29a33 | 4801 | fprintf(stderr, "Failed to allocate memory for %s\n", name); |
2a59a681 SH |
4802 | goto err; |
4803 | } | |
4804 | ||
f5dd1d53 | 4805 | remove_trailing_slashes(c->config_path); |
cbb9c7c7 DJ |
4806 | |
4807 | len = strlen(name); | |
4808 | c->name = malloc(len + 1); | |
72d0e1cb | 4809 | if (!c->name) { |
e9e29a33 | 4810 | fprintf(stderr, "Failed to allocate memory for %s\n", name); |
72d0e1cb SG |
4811 | goto err; |
4812 | } | |
cbb9c7c7 | 4813 | (void)strlcpy(c->name, name, len + 1); |
72d0e1cb SG |
4814 | |
4815 | c->numthreads = 1; | |
e9e29a33 CB |
4816 | c->slock = lxc_newlock(c->config_path, name); |
4817 | if (!c->slock) { | |
4818 | fprintf(stderr, "Failed to create lock for %s\n", name); | |
72d0e1cb SG |
4819 | goto err; |
4820 | } | |
4821 | ||
e9e29a33 CB |
4822 | c->privlock = lxc_newlock(NULL, NULL); |
4823 | if (!c->privlock) { | |
4824 | fprintf(stderr, "Failed to create private lock for %s\n", name); | |
72d0e1cb SG |
4825 | goto err; |
4826 | } | |
4827 | ||
afeecbba | 4828 | if (!set_config_filename(c)) { |
e9e29a33 | 4829 | fprintf(stderr, "Failed to create config file name for %s\n", name); |
72d0e1cb SG |
4830 | goto err; |
4831 | } | |
72d0e1cb | 4832 | |
e9e29a33 CB |
4833 | if (file_exists(c->configfile) && !lxcapi_load_config(c, NULL)) { |
4834 | fprintf(stderr, "Failed to load config for %s\n", name); | |
bac806d1 | 4835 | goto err; |
e9e29a33 | 4836 | } |
72d0e1cb | 4837 | |
3e625e2d | 4838 | if (ongoing_create(c) == 2) { |
e9e29a33 | 4839 | ERROR("Failed to complete container creation for %s", c->name); |
17a367d8 | 4840 | container_destroy(c, NULL); |
4df7f012 | 4841 | lxcapi_clear_config(c); |
3e625e2d | 4842 | } |
a2739df5 | 4843 | c->daemonize = true; |
72cf75fa | 4844 | c->pidfile = NULL; |
3e625e2d | 4845 | |
1a0e70ac | 4846 | /* Assign the member functions. */ |
72d0e1cb SG |
4847 | c->is_defined = lxcapi_is_defined; |
4848 | c->state = lxcapi_state; | |
4849 | c->is_running = lxcapi_is_running; | |
4850 | c->freeze = lxcapi_freeze; | |
4851 | c->unfreeze = lxcapi_unfreeze; | |
0115f8fd | 4852 | c->console = lxcapi_console; |
b5159817 | 4853 | c->console_getfd = lxcapi_console_getfd; |
72d0e1cb SG |
4854 | c->init_pid = lxcapi_init_pid; |
4855 | c->load_config = lxcapi_load_config; | |
4856 | c->want_daemonize = lxcapi_want_daemonize; | |
130a1888 | 4857 | c->want_close_all_fds = lxcapi_want_close_all_fds; |
72d0e1cb SG |
4858 | c->start = lxcapi_start; |
4859 | c->startl = lxcapi_startl; | |
4860 | c->stop = lxcapi_stop; | |
4861 | c->config_file_name = lxcapi_config_file_name; | |
4862 | c->wait = lxcapi_wait; | |
4863 | c->set_config_item = lxcapi_set_config_item; | |
4864 | c->destroy = lxcapi_destroy; | |
18aa217b | 4865 | c->destroy_with_snapshots = lxcapi_destroy_with_snapshots; |
06e5650e | 4866 | c->rename = lxcapi_rename; |
72d0e1cb SG |
4867 | c->save_config = lxcapi_save_config; |
4868 | c->get_keys = lxcapi_get_keys; | |
4869 | c->create = lxcapi_create; | |
4870 | c->createl = lxcapi_createl; | |
4871 | c->shutdown = lxcapi_shutdown; | |
3e625e2d | 4872 | c->reboot = lxcapi_reboot; |
d39b10eb | 4873 | c->reboot2 = lxcapi_reboot2; |
4df7f012 | 4874 | c->clear_config = lxcapi_clear_config; |
72d0e1cb SG |
4875 | c->clear_config_item = lxcapi_clear_config_item; |
4876 | c->get_config_item = lxcapi_get_config_item; | |
8ac18377 | 4877 | c->get_running_config_item = lxcapi_get_running_config_item; |
794dd120 SH |
4878 | c->get_cgroup_item = lxcapi_get_cgroup_item; |
4879 | c->set_cgroup_item = lxcapi_set_cgroup_item; | |
2a59a681 SH |
4880 | c->get_config_path = lxcapi_get_config_path; |
4881 | c->set_config_path = lxcapi_set_config_path; | |
9be53773 | 4882 | c->clone = lxcapi_clone; |
799f29ab | 4883 | c->get_interfaces = lxcapi_get_interfaces; |
9c83a661 | 4884 | c->get_ips = lxcapi_get_ips; |
a0e93eeb CS |
4885 | c->attach = lxcapi_attach; |
4886 | c->attach_run_wait = lxcapi_attach_run_wait; | |
4887 | c->attach_run_waitl = lxcapi_attach_run_waitl; | |
f5dd1d53 SH |
4888 | c->snapshot = lxcapi_snapshot; |
4889 | c->snapshot_list = lxcapi_snapshot_list; | |
4890 | c->snapshot_restore = lxcapi_snapshot_restore; | |
771d96b3 | 4891 | c->snapshot_destroy = lxcapi_snapshot_destroy; |
18aa217b | 4892 | c->snapshot_destroy_all = lxcapi_snapshot_destroy_all; |
b494d2dd | 4893 | c->may_control = lxcapi_may_control; |
a9a0ed90 ÇO |
4894 | c->add_device_node = lxcapi_add_device_node; |
4895 | c->remove_device_node = lxcapi_remove_device_node; | |
e58fae8f DY |
4896 | c->attach_interface = lxcapi_attach_interface; |
4897 | c->detach_interface = lxcapi_detach_interface; | |
735f2c6e TA |
4898 | c->checkpoint = lxcapi_checkpoint; |
4899 | c->restore = lxcapi_restore; | |
aef3d51e | 4900 | c->migrate = lxcapi_migrate; |
191d43cc | 4901 | c->console_log = lxcapi_console_log; |
72d0e1cb | 4902 | |
72d0e1cb SG |
4903 | return c; |
4904 | ||
4905 | err: | |
4906 | lxc_container_free(c); | |
4907 | return NULL; | |
4908 | } | |
4909 | ||
4a7c7daa | 4910 | int lxc_get_wait_states(const char **states) |
72d0e1cb SG |
4911 | { |
4912 | int i; | |
4913 | ||
4914 | if (states) | |
4915 | for (i=0; i<MAX_STATE; i++) | |
4916 | states[i] = lxc_state2str(i); | |
4917 | return MAX_STATE; | |
4918 | } | |
a41f104b | 4919 | |
a41f104b SH |
4920 | /* |
4921 | * These next two could probably be done smarter with reusing a common function | |
4922 | * with different iterators and tests... | |
4923 | */ | |
4924 | int list_defined_containers(const char *lxcpath, char ***names, struct lxc_container ***cret) | |
4925 | { | |
4926 | DIR *dir; | |
4927 | int i, cfound = 0, nfound = 0; | |
74f96976 | 4928 | struct dirent *direntp; |
a41f104b SH |
4929 | struct lxc_container *c; |
4930 | ||
4931 | if (!lxcpath) | |
593e8478 | 4932 | lxcpath = lxc_global_config_value("lxc.lxcpath"); |
a41f104b | 4933 | |
a41f104b | 4934 | dir = opendir(lxcpath); |
a41f104b SH |
4935 | if (!dir) { |
4936 | SYSERROR("opendir on lxcpath"); | |
4937 | return -1; | |
4938 | } | |
4939 | ||
4940 | if (cret) | |
4941 | *cret = NULL; | |
4942 | if (names) | |
4943 | *names = NULL; | |
4944 | ||
74f96976 | 4945 | while ((direntp = readdir(dir))) { |
1a0e70ac | 4946 | /* Ignore '.', '..' and any hidden directory. */ |
e4ebeab1 | 4947 | if (!strncmp(direntp->d_name, ".", 1)) |
a41f104b SH |
4948 | continue; |
4949 | ||
4950 | if (!config_file_exists(lxcpath, direntp->d_name)) | |
4951 | continue; | |
4952 | ||
4953 | if (names) { | |
9c88ff1f | 4954 | if (!add_to_array(names, direntp->d_name, cfound)) |
a41f104b SH |
4955 | goto free_bad; |
4956 | } | |
4957 | cfound++; | |
4958 | ||
4959 | if (!cret) { | |
4960 | nfound++; | |
4961 | continue; | |
4962 | } | |
4963 | ||
4964 | c = lxc_container_new(direntp->d_name, lxcpath); | |
4965 | if (!c) { | |
4966 | INFO("Container %s:%s has a config but could not be loaded", | |
4967 | lxcpath, direntp->d_name); | |
4968 | if (names) | |
9c88ff1f ÇO |
4969 | if(!remove_from_array(names, direntp->d_name, cfound--)) |
4970 | goto free_bad; | |
a41f104b SH |
4971 | continue; |
4972 | } | |
858377e4 | 4973 | if (!do_lxcapi_is_defined(c)) { |
a41f104b SH |
4974 | INFO("Container %s:%s has a config but is not defined", |
4975 | lxcpath, direntp->d_name); | |
4976 | if (names) | |
9c88ff1f ÇO |
4977 | if(!remove_from_array(names, direntp->d_name, cfound--)) |
4978 | goto free_bad; | |
a41f104b SH |
4979 | lxc_container_put(c); |
4980 | continue; | |
4981 | } | |
4982 | ||
2871830a | 4983 | if (!add_to_clist(cret, c, nfound, true)) { |
a41f104b SH |
4984 | lxc_container_put(c); |
4985 | goto free_bad; | |
4986 | } | |
4987 | nfound++; | |
4988 | } | |
4989 | ||
a41f104b | 4990 | closedir(dir); |
a41f104b SH |
4991 | return nfound; |
4992 | ||
4993 | free_bad: | |
4994 | if (names && *names) { | |
4995 | for (i=0; i<cfound; i++) | |
4996 | free((*names)[i]); | |
4997 | free(*names); | |
4998 | } | |
4999 | if (cret && *cret) { | |
5000 | for (i=0; i<nfound; i++) | |
5001 | lxc_container_put((*cret)[i]); | |
5002 | free(*cret); | |
5003 | } | |
a41f104b | 5004 | closedir(dir); |
a41f104b SH |
5005 | return -1; |
5006 | } | |
5007 | ||
148a9d27 DE |
5008 | int list_active_containers(const char *lxcpath, char ***nret, |
5009 | struct lxc_container ***cret) | |
a41f104b | 5010 | { |
148a9d27 | 5011 | int i, ret = -1, cret_cnt = 0, ct_name_cnt = 0; |
a41f104b SH |
5012 | int lxcpath_len; |
5013 | char *line = NULL; | |
148a9d27 | 5014 | char **ct_name = NULL; |
a41f104b | 5015 | size_t len = 0; |
97bc2422 | 5016 | struct lxc_container *c = NULL; |
88556fd7 | 5017 | bool is_hashed; |
a41f104b SH |
5018 | |
5019 | if (!lxcpath) | |
593e8478 | 5020 | lxcpath = lxc_global_config_value("lxc.lxcpath"); |
a41f104b SH |
5021 | lxcpath_len = strlen(lxcpath); |
5022 | ||
5023 | if (cret) | |
5024 | *cret = NULL; | |
148a9d27 DE |
5025 | if (nret) |
5026 | *nret = NULL; | |
a41f104b | 5027 | |
a41f104b | 5028 | FILE *f = fopen("/proc/net/unix", "r"); |
a41f104b SH |
5029 | if (!f) |
5030 | return -1; | |
5031 | ||
5032 | while (getline(&line, &len, f) != -1) { | |
88556fd7 | 5033 | |
0f8f9c8a | 5034 | char *p = strrchr(line, ' '), *p2; |
a41f104b SH |
5035 | if (!p) |
5036 | continue; | |
5037 | p++; | |
5038 | if (*p != 0x40) | |
5039 | continue; | |
5040 | p++; | |
88556fd7 ÇO |
5041 | |
5042 | is_hashed = false; | |
5043 | if (strncmp(p, lxcpath, lxcpath_len) == 0) { | |
5044 | p += lxcpath_len; | |
5045 | } else if (strncmp(p, "lxc/", 4) == 0) { | |
5046 | p += 4; | |
5047 | is_hashed = true; | |
5048 | } else { | |
a41f104b | 5049 | continue; |
88556fd7 ÇO |
5050 | } |
5051 | ||
a41f104b SH |
5052 | while (*p == '/') |
5053 | p++; | |
5054 | ||
1a0e70ac | 5055 | /* Now p is the start of lxc_name. */ |
46cd2845 | 5056 | p2 = strchr(p, '/'); |
a41f104b SH |
5057 | if (!p2 || strncmp(p2, "/command", 8) != 0) |
5058 | continue; | |
5059 | *p2 = '\0'; | |
5060 | ||
88556fd7 | 5061 | if (is_hashed) { |
899a9f55 CB |
5062 | char *recvpath = lxc_cmd_get_lxcpath(p); |
5063 | if (!recvpath) | |
5064 | continue; | |
e07eafa8 L |
5065 | if (strncmp(lxcpath, recvpath, lxcpath_len) != 0) { |
5066 | free(recvpath); | |
88556fd7 | 5067 | continue; |
e07eafa8 L |
5068 | } |
5069 | free(recvpath); | |
88556fd7 | 5070 | p = lxc_cmd_get_name(p); |
ee2d7093 L |
5071 | if (!p) |
5072 | continue; | |
88556fd7 ÇO |
5073 | } |
5074 | ||
e07eafa8 L |
5075 | if (array_contains(&ct_name, p, ct_name_cnt)) { |
5076 | if (is_hashed) | |
5077 | free(p); | |
9c88ff1f | 5078 | continue; |
e07eafa8 | 5079 | } |
9c88ff1f | 5080 | |
e07eafa8 L |
5081 | if (!add_to_array(&ct_name, p, ct_name_cnt)) { |
5082 | if (is_hashed) | |
5083 | free(p); | |
148a9d27 | 5084 | goto free_cret_list; |
e07eafa8 | 5085 | } |
9c88ff1f | 5086 | |
148a9d27 | 5087 | ct_name_cnt++; |
a41f104b | 5088 | |
e07eafa8 L |
5089 | if (!cret) { |
5090 | if (is_hashed) | |
5091 | free(p); | |
a41f104b | 5092 | continue; |
e07eafa8 | 5093 | } |
a41f104b SH |
5094 | |
5095 | c = lxc_container_new(p, lxcpath); | |
5096 | if (!c) { | |
5097 | INFO("Container %s:%s is running but could not be loaded", | |
5098 | lxcpath, p); | |
148a9d27 | 5099 | remove_from_array(&ct_name, p, ct_name_cnt--); |
e07eafa8 L |
5100 | if (is_hashed) |
5101 | free(p); | |
a41f104b SH |
5102 | continue; |
5103 | } | |
5104 | ||
e07eafa8 L |
5105 | if (is_hashed) |
5106 | free(p); | |
5107 | ||
a41f104b SH |
5108 | /* |
5109 | * If this is an anonymous container, then is_defined *can* | |
5110 | * return false. So we don't do that check. Count on the | |
5111 | * fact that the command socket exists. | |
5112 | */ | |
5113 | ||
148a9d27 | 5114 | if (!add_to_clist(cret, c, cret_cnt, true)) { |
a41f104b | 5115 | lxc_container_put(c); |
148a9d27 | 5116 | goto free_cret_list; |
a41f104b | 5117 | } |
148a9d27 | 5118 | cret_cnt++; |
a41f104b SH |
5119 | } |
5120 | ||
97bc2422 CB |
5121 | if (nret && cret && cret_cnt != ct_name_cnt) { |
5122 | if (c) | |
5123 | lxc_container_put(c); | |
5124 | goto free_cret_list; | |
5125 | } | |
5126 | ||
148a9d27 DE |
5127 | ret = ct_name_cnt; |
5128 | if (nret) | |
5129 | *nret = ct_name; | |
5130 | else | |
5131 | goto free_ct_name; | |
5132 | goto out; | |
a41f104b | 5133 | |
148a9d27 | 5134 | free_cret_list: |
a41f104b | 5135 | if (cret && *cret) { |
148a9d27 | 5136 | for (i = 0; i < cret_cnt; i++) |
a41f104b SH |
5137 | lxc_container_put((*cret)[i]); |
5138 | free(*cret); | |
5139 | } | |
148a9d27 DE |
5140 | |
5141 | free_ct_name: | |
5142 | if (ct_name) { | |
5143 | for (i = 0; i < ct_name_cnt; i++) | |
5144 | free(ct_name[i]); | |
5145 | free(ct_name); | |
5146 | } | |
5147 | ||
5148 | out: | |
f10fad2f | 5149 | free(line); |
e853a32d | 5150 | |
a41f104b | 5151 | fclose(f); |
148a9d27 | 5152 | return ret; |
a41f104b | 5153 | } |
2871830a DE |
5154 | |
5155 | int list_all_containers(const char *lxcpath, char ***nret, | |
5156 | struct lxc_container ***cret) | |
5157 | { | |
5158 | int i, ret, active_cnt, ct_cnt, ct_list_cnt; | |
5159 | char **active_name; | |
5160 | char **ct_name; | |
5161 | struct lxc_container **ct_list = NULL; | |
5162 | ||
5163 | ct_cnt = list_defined_containers(lxcpath, &ct_name, NULL); | |
5164 | if (ct_cnt < 0) | |
5165 | return ct_cnt; | |
5166 | ||
5167 | active_cnt = list_active_containers(lxcpath, &active_name, NULL); | |
5168 | if (active_cnt < 0) { | |
5169 | ret = active_cnt; | |
5170 | goto free_ct_name; | |
5171 | } | |
5172 | ||
5173 | for (i = 0; i < active_cnt; i++) { | |
5174 | if (!array_contains(&ct_name, active_name[i], ct_cnt)) { | |
5175 | if (!add_to_array(&ct_name, active_name[i], ct_cnt)) { | |
5176 | ret = -1; | |
5177 | goto free_active_name; | |
5178 | } | |
5179 | ct_cnt++; | |
5180 | } | |
5181 | free(active_name[i]); | |
5182 | active_name[i] = NULL; | |
5183 | } | |
5184 | free(active_name); | |
5185 | active_name = NULL; | |
5186 | active_cnt = 0; | |
5187 | ||
5188 | for (i = 0, ct_list_cnt = 0; i < ct_cnt && cret; i++) { | |
5189 | struct lxc_container *c; | |
5190 | ||
5191 | c = lxc_container_new(ct_name[i], lxcpath); | |
5192 | if (!c) { | |
5193 | WARN("Container %s:%s could not be loaded", lxcpath, ct_name[i]); | |
5194 | remove_from_array(&ct_name, ct_name[i], ct_cnt--); | |
5195 | continue; | |
5196 | } | |
5197 | ||
5198 | if (!add_to_clist(&ct_list, c, ct_list_cnt, false)) { | |
5199 | lxc_container_put(c); | |
5200 | ret = -1; | |
5201 | goto free_ct_list; | |
5202 | } | |
5203 | ct_list_cnt++; | |
5204 | } | |
5205 | ||
5206 | if (cret) | |
5207 | *cret = ct_list; | |
5208 | ||
5209 | if (nret) | |
5210 | *nret = ct_name; | |
5211 | else { | |
5212 | ret = ct_cnt; | |
5213 | goto free_ct_name; | |
5214 | } | |
5215 | return ct_cnt; | |
5216 | ||
5217 | free_ct_list: | |
5218 | for (i = 0; i < ct_list_cnt; i++) { | |
5219 | lxc_container_put(ct_list[i]); | |
5220 | } | |
f10fad2f | 5221 | free(ct_list); |
2871830a DE |
5222 | |
5223 | free_active_name: | |
5224 | for (i = 0; i < active_cnt; i++) { | |
f10fad2f | 5225 | free(active_name[i]); |
2871830a | 5226 | } |
f10fad2f | 5227 | free(active_name); |
2871830a DE |
5228 | |
5229 | free_ct_name: | |
5230 | for (i = 0; i < ct_cnt; i++) { | |
5231 | free(ct_name[i]); | |
5232 | } | |
5233 | free(ct_name); | |
5234 | return ret; | |
5235 | } | |
12461428 CB |
5236 | |
5237 | bool lxc_config_item_is_supported(const char *key) | |
5238 | { | |
300df83e | 5239 | return !!lxc_get_config(key); |
12461428 | 5240 | } |