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