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