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