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