]>
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 */ | |
1386 | ret = true; | |
1387 | goto out; | |
a69aad27 | 1388 | } |
96b3cb40 SH |
1389 | |
1390 | /* Mark that this container is being created */ | |
1391 | if ((partial_fd = create_partial(c)) < 0) | |
1392 | goto out; | |
1393 | ||
1394 | /* no need to get disk lock bc we have the partial locked */ | |
1395 | ||
1396 | /* | |
1397 | * Create the backing store | |
1398 | * Note we can't do this in the same task as we use to execute the | |
1399 | * template because of the way zfs works. | |
1400 | * After you 'zfs create', zfs mounts the fs only in the initial | |
1401 | * namespace. | |
1402 | */ | |
1403 | pid = fork(); | |
1404 | if (pid < 0) { | |
959aee9c | 1405 | SYSERROR("failed to fork task for container creation template"); |
8eb5694b SH |
1406 | goto out_unlock; |
1407 | } | |
1408 | ||
96b3cb40 SH |
1409 | if (pid == 0) { // child |
1410 | struct bdev *bdev = NULL; | |
1411 | ||
1412 | if (!(bdev = do_bdev_create(c, bdevtype, specs))) { | |
1413 | ERROR("Error creating backing store type %s for %s", | |
1414 | bdevtype ? bdevtype : "(none)", c->name); | |
1415 | exit(1); | |
1416 | } | |
1417 | ||
1418 | /* save config file again to store the new rootfs location */ | |
858377e4 | 1419 | if (!do_lxcapi_save_config(c, NULL)) { |
959aee9c | 1420 | ERROR("failed to save starting configuration for %s", c->name); |
96b3cb40 SH |
1421 | // parent task won't see bdev in config so we delete it |
1422 | bdev->ops->umount(bdev); | |
1423 | bdev->ops->destroy(bdev); | |
1424 | exit(1); | |
1425 | } | |
1426 | exit(0); | |
1427 | } | |
1428 | if (wait_for_pid(pid) != 0) | |
a09295f8 | 1429 | goto out_unlock; |
96b3cb40 SH |
1430 | |
1431 | /* reload config to get the rootfs */ | |
a3b47c09 | 1432 | lxc_conf_free(c->lxc_conf); |
96b3cb40 SH |
1433 | c->lxc_conf = NULL; |
1434 | if (!load_config_locked(c, c->configfile)) | |
a09295f8 | 1435 | goto out_unlock; |
96b3cb40 | 1436 | |
dc23c1c8 | 1437 | if (!create_run_template(c, tpath, !!(flags & LXC_CREATE_QUIET), argv)) |
96b3cb40 SH |
1438 | goto out_unlock; |
1439 | ||
8eb5694b SH |
1440 | // now clear out the lxc_conf we have, reload from the created |
1441 | // container | |
858377e4 | 1442 | do_lxcapi_clear_config(c); |
3ce74686 | 1443 | |
9d65a487 KY |
1444 | if (t) { |
1445 | if (!prepend_lxc_header(c->configfile, tpath, argv)) { | |
1446 | ERROR("Error prepending header to configuration file"); | |
1447 | goto out_unlock; | |
1448 | } | |
3ce74686 | 1449 | } |
a69aad27 | 1450 | ret = load_config_locked(c, c->configfile); |
72d0e1cb SG |
1451 | |
1452 | out_unlock: | |
3e625e2d SH |
1453 | if (partial_fd >= 0) |
1454 | remove_partial(c, partial_fd); | |
72d0e1cb | 1455 | out: |
c55d4505 | 1456 | if (!ret) |
18aa217b | 1457 | container_destroy(c); |
6c6892b5 | 1458 | free_tpath: |
f10fad2f | 1459 | free(tpath); |
a69aad27 | 1460 | return ret; |
72d0e1cb SG |
1461 | } |
1462 | ||
858377e4 SH |
1463 | static bool lxcapi_create(struct lxc_container *c, const char *t, |
1464 | const char *bdevtype, struct bdev_specs *specs, int flags, | |
1465 | char *const argv[]) | |
1466 | { | |
1467 | bool ret; | |
1468 | current_config = c ? c->lxc_conf : NULL; | |
1469 | ret = do_lxcapi_create(c, t, bdevtype, specs, flags, argv); | |
1470 | current_config = NULL; | |
1471 | return ret; | |
1472 | } | |
1473 | ||
1474 | static bool do_lxcapi_reboot(struct lxc_container *c) | |
3e625e2d SH |
1475 | { |
1476 | pid_t pid; | |
dd267776 | 1477 | int rebootsignal = SIGINT; |
3e625e2d SH |
1478 | |
1479 | if (!c) | |
1480 | return false; | |
858377e4 | 1481 | if (!do_lxcapi_is_running(c)) |
3e625e2d | 1482 | return false; |
858377e4 | 1483 | pid = do_lxcapi_init_pid(c); |
3e625e2d SH |
1484 | if (pid <= 0) |
1485 | return false; | |
dd267776 BP |
1486 | if (c->lxc_conf && c->lxc_conf->rebootsignal) |
1487 | rebootsignal = c->lxc_conf->rebootsignal; | |
1488 | if (kill(pid, rebootsignal) < 0) | |
3e625e2d SH |
1489 | return false; |
1490 | return true; | |
1491 | ||
1492 | } | |
1493 | ||
858377e4 SH |
1494 | WRAP_API(bool, lxcapi_reboot) |
1495 | ||
1496 | static bool do_lxcapi_shutdown(struct lxc_container *c, int timeout) | |
72d0e1cb SG |
1497 | { |
1498 | bool retv; | |
1499 | pid_t pid; | |
f0f1d8c0 | 1500 | int haltsignal = SIGPWR; |
72d0e1cb SG |
1501 | |
1502 | if (!c) | |
1503 | return false; | |
1504 | ||
858377e4 | 1505 | if (!do_lxcapi_is_running(c)) |
72d0e1cb | 1506 | return true; |
858377e4 | 1507 | pid = do_lxcapi_init_pid(c); |
72d0e1cb SG |
1508 | if (pid <= 0) |
1509 | return true; | |
b0227444 | 1510 | if (c->lxc_conf && c->lxc_conf->haltsignal) |
f0f1d8c0 DE |
1511 | haltsignal = c->lxc_conf->haltsignal; |
1512 | kill(pid, haltsignal); | |
858377e4 | 1513 | retv = do_lxcapi_wait(c, "STOPPED", timeout); |
72d0e1cb SG |
1514 | return retv; |
1515 | } | |
1516 | ||
858377e4 SH |
1517 | WRAP_API_1(bool, lxcapi_shutdown, int) |
1518 | ||
1897e3bc | 1519 | static bool lxcapi_createl(struct lxc_container *c, const char *t, |
dc23c1c8 | 1520 | const char *bdevtype, struct bdev_specs *specs, int flags, ...) |
72d0e1cb SG |
1521 | { |
1522 | bool bret = false; | |
a0e93eeb | 1523 | char **args = NULL; |
72d0e1cb | 1524 | va_list ap; |
72d0e1cb SG |
1525 | |
1526 | if (!c) | |
1527 | return false; | |
1528 | ||
858377e4 SH |
1529 | current_config = c->lxc_conf; |
1530 | ||
72d0e1cb SG |
1531 | /* |
1532 | * since we're going to wait for create to finish, I don't think we | |
1533 | * need to get a copy of the arguments. | |
1534 | */ | |
dc23c1c8 | 1535 | va_start(ap, flags); |
a0e93eeb | 1536 | args = lxc_va_arg_list_to_argv(ap, 0, 0); |
72d0e1cb | 1537 | va_end(ap); |
a0e93eeb CS |
1538 | if (!args) { |
1539 | ERROR("Memory allocation error."); | |
1540 | goto out; | |
1541 | } | |
72d0e1cb | 1542 | |
858377e4 | 1543 | bret = do_lxcapi_create(c, t, bdevtype, specs, flags, args); |
72d0e1cb SG |
1544 | |
1545 | out: | |
a0e93eeb | 1546 | free(args); |
858377e4 | 1547 | current_config = NULL; |
72d0e1cb SG |
1548 | return bret; |
1549 | } | |
1550 | ||
6b0d5538 SH |
1551 | static void do_clear_unexp_config_line(struct lxc_conf *conf, const char *key) |
1552 | { | |
1553 | if (strcmp(key, "lxc.cgroup") == 0) | |
1554 | clear_unexp_config_line(conf, key, true); | |
1555 | else if (strcmp(key, "lxc.network") == 0) | |
1556 | clear_unexp_config_line(conf, key, true); | |
1557 | else if (strcmp(key, "lxc.hook") == 0) | |
1558 | clear_unexp_config_line(conf, key, true); | |
1559 | else | |
1560 | clear_unexp_config_line(conf, key, false); | |
1561 | if (!do_append_unexp_config_line(conf, key, "")) | |
1562 | WARN("Error clearing configuration for %s", key); | |
1563 | } | |
1564 | ||
858377e4 | 1565 | static bool do_lxcapi_clear_config_item(struct lxc_container *c, const char *key) |
72d0e1cb SG |
1566 | { |
1567 | int ret; | |
1568 | ||
1569 | if (!c || !c->lxc_conf) | |
1570 | return false; | |
5cee8c50 | 1571 | if (container_mem_lock(c)) |
72d0e1cb | 1572 | return false; |
72d0e1cb | 1573 | ret = lxc_clear_config_item(c->lxc_conf, key); |
6b0d5538 SH |
1574 | if (!ret) |
1575 | do_clear_unexp_config_line(c->lxc_conf, key); | |
5cee8c50 | 1576 | container_mem_unlock(c); |
72d0e1cb SG |
1577 | return ret == 0; |
1578 | } | |
1579 | ||
858377e4 SH |
1580 | WRAP_API_1(bool, lxcapi_clear_config_item, const char *) |
1581 | ||
e0f59189 | 1582 | static inline bool enter_net_ns(struct lxc_container *c) |
51d0854c | 1583 | { |
858377e4 | 1584 | pid_t pid = do_lxcapi_init_pid(c); |
ae22a220 | 1585 | |
0e6e3a41 | 1586 | if ((geteuid() != 0 || (c->lxc_conf && !lxc_list_empty(&c->lxc_conf->id_map))) && access("/proc/self/ns/user", F_OK) == 0) { |
51d0854c DY |
1587 | if (!switch_to_ns(pid, "user")) |
1588 | return false; | |
9c83a661 | 1589 | } |
51d0854c | 1590 | return switch_to_ns(pid, "net"); |
799f29ab ÇO |
1591 | } |
1592 | ||
9c88ff1f ÇO |
1593 | // used by qsort and bsearch functions for comparing names |
1594 | static inline int string_cmp(char **first, char **second) | |
1595 | { | |
1596 | return strcmp(*first, *second); | |
1597 | } | |
1598 | ||
1599 | // used by qsort and bsearch functions for comparing container names | |
1600 | static inline int container_cmp(struct lxc_container **first, struct lxc_container **second) | |
1601 | { | |
1602 | return strcmp((*first)->name, (*second)->name); | |
1603 | } | |
1604 | ||
1605 | static bool add_to_array(char ***names, char *cname, int pos) | |
1606 | { | |
1607 | char **newnames = realloc(*names, (pos+1) * sizeof(char *)); | |
1608 | if (!newnames) { | |
1609 | ERROR("Out of memory"); | |
1610 | return false; | |
1611 | } | |
1612 | ||
1613 | *names = newnames; | |
1614 | newnames[pos] = strdup(cname); | |
1615 | if (!newnames[pos]) | |
1616 | return false; | |
1617 | ||
1618 | // sort the arrray as we will use binary search on it | |
1619 | qsort(newnames, pos + 1, sizeof(char *), (int (*)(const void *,const void *))string_cmp); | |
1620 | ||
1621 | return true; | |
1622 | } | |
1623 | ||
2871830a | 1624 | static bool add_to_clist(struct lxc_container ***list, struct lxc_container *c, int pos, bool sort) |
9c88ff1f ÇO |
1625 | { |
1626 | struct lxc_container **newlist = realloc(*list, (pos+1) * sizeof(struct lxc_container *)); | |
1627 | if (!newlist) { | |
1628 | ERROR("Out of memory"); | |
1629 | return false; | |
1630 | } | |
1631 | ||
1632 | *list = newlist; | |
1633 | newlist[pos] = c; | |
1634 | ||
1635 | // sort the arrray as we will use binary search on it | |
2871830a DE |
1636 | if (sort) |
1637 | qsort(newlist, pos + 1, sizeof(struct lxc_container *), (int (*)(const void *,const void *))container_cmp); | |
9c88ff1f ÇO |
1638 | |
1639 | return true; | |
1640 | } | |
1641 | ||
1642 | static char** get_from_array(char ***names, char *cname, int size) | |
1643 | { | |
1644 | return (char **)bsearch(&cname, *names, size, sizeof(char *), (int (*)(const void *, const void *))string_cmp); | |
1645 | } | |
1646 | ||
1647 | ||
1648 | static bool array_contains(char ***names, char *cname, int size) { | |
1649 | if(get_from_array(names, cname, size) != NULL) | |
1650 | return true; | |
1651 | return false; | |
1652 | } | |
1653 | ||
1654 | static bool remove_from_array(char ***names, char *cname, int size) | |
1655 | { | |
1656 | char **result = get_from_array(names, cname, size); | |
1657 | if (result != NULL) { | |
1658 | free(result); | |
1659 | return true; | |
1660 | } | |
1661 | return false; | |
1662 | } | |
1663 | ||
858377e4 | 1664 | static char ** do_lxcapi_get_interfaces(struct lxc_container *c) |
799f29ab | 1665 | { |
ae22a220 ÇO |
1666 | pid_t pid; |
1667 | int i, count = 0, pipefd[2]; | |
9c88ff1f | 1668 | char **interfaces = NULL; |
ae22a220 | 1669 | char interface[IFNAMSIZ]; |
799f29ab | 1670 | |
ae22a220 ÇO |
1671 | if(pipe(pipefd) < 0) { |
1672 | SYSERROR("pipe failed"); | |
1673 | return NULL; | |
c868b261 ÇO |
1674 | } |
1675 | ||
ae22a220 ÇO |
1676 | pid = fork(); |
1677 | if (pid < 0) { | |
959aee9c | 1678 | SYSERROR("failed to fork task to get interfaces information"); |
ae22a220 ÇO |
1679 | close(pipefd[0]); |
1680 | close(pipefd[1]); | |
1681 | return NULL; | |
1682 | } | |
799f29ab | 1683 | |
ae22a220 ÇO |
1684 | if (pid == 0) { // child |
1685 | int ret = 1, nbytes; | |
1686 | struct ifaddrs *interfaceArray = NULL, *tempIfAddr = NULL; | |
1687 | ||
1688 | /* close the read-end of the pipe */ | |
1689 | close(pipefd[0]); | |
1690 | ||
e0f59189 | 1691 | if (!enter_net_ns(c)) { |
ae22a220 ÇO |
1692 | SYSERROR("failed to enter namespace"); |
1693 | goto out; | |
1694 | } | |
1695 | ||
1696 | /* Grab the list of interfaces */ | |
1697 | if (getifaddrs(&interfaceArray)) { | |
1698 | SYSERROR("failed to get interfaces list"); | |
1699 | goto out; | |
1700 | } | |
1701 | ||
1702 | /* Iterate through the interfaces */ | |
1703 | for (tempIfAddr = interfaceArray; tempIfAddr != NULL; tempIfAddr = tempIfAddr->ifa_next) { | |
1704 | nbytes = write(pipefd[1], tempIfAddr->ifa_name, IFNAMSIZ); | |
1705 | if (nbytes < 0) { | |
1706 | ERROR("write failed"); | |
1707 | goto out; | |
1708 | } | |
1709 | count++; | |
1710 | } | |
1711 | ret = 0; | |
1712 | ||
1713 | out: | |
1714 | if (interfaceArray) | |
1715 | freeifaddrs(interfaceArray); | |
1716 | ||
1717 | /* close the write-end of the pipe, thus sending EOF to the reader */ | |
1718 | close(pipefd[1]); | |
1719 | exit(ret); | |
799f29ab ÇO |
1720 | } |
1721 | ||
ae22a220 ÇO |
1722 | /* close the write-end of the pipe */ |
1723 | close(pipefd[1]); | |
1724 | ||
358afd84 | 1725 | while (read(pipefd[0], &interface, IFNAMSIZ) == IFNAMSIZ) { |
ae22a220 ÇO |
1726 | if (array_contains(&interfaces, interface, count)) |
1727 | continue; | |
799f29ab | 1728 | |
ae22a220 ÇO |
1729 | if(!add_to_array(&interfaces, interface, count)) |
1730 | ERROR("PARENT: add_to_array failed"); | |
9c88ff1f ÇO |
1731 | count++; |
1732 | } | |
799f29ab | 1733 | |
ae22a220 ÇO |
1734 | if (wait_for_pid(pid) != 0) { |
1735 | for(i=0;i<count;i++) | |
1736 | free(interfaces[i]); | |
1737 | free(interfaces); | |
1738 | interfaces = NULL; | |
1739 | } | |
9c88ff1f | 1740 | |
ae22a220 ÇO |
1741 | /* close the read-end of the pipe */ |
1742 | close(pipefd[0]); | |
799f29ab | 1743 | |
9c88ff1f ÇO |
1744 | /* Append NULL to the array */ |
1745 | if(interfaces) | |
1746 | interfaces = (char **)lxc_append_null_to_array((void **)interfaces, count); | |
799f29ab | 1747 | |
9c88ff1f | 1748 | return interfaces; |
799f29ab ÇO |
1749 | } |
1750 | ||
858377e4 SH |
1751 | WRAP_API(char **, lxcapi_get_interfaces) |
1752 | ||
1753 | static char** do_lxcapi_get_ips(struct lxc_container *c, const char* interface, const char* family, int scope) | |
799f29ab | 1754 | { |
ae22a220 ÇO |
1755 | pid_t pid; |
1756 | int i, count = 0, pipefd[2]; | |
9c88ff1f | 1757 | char **addresses = NULL; |
ae22a220 | 1758 | char address[INET6_ADDRSTRLEN]; |
799f29ab | 1759 | |
ae22a220 ÇO |
1760 | if(pipe(pipefd) < 0) { |
1761 | SYSERROR("pipe failed"); | |
1762 | return NULL; | |
c868b261 ÇO |
1763 | } |
1764 | ||
ae22a220 ÇO |
1765 | pid = fork(); |
1766 | if (pid < 0) { | |
959aee9c | 1767 | SYSERROR("failed to fork task to get container ips"); |
ae22a220 ÇO |
1768 | close(pipefd[0]); |
1769 | close(pipefd[1]); | |
1770 | return NULL; | |
9c83a661 SG |
1771 | } |
1772 | ||
ae22a220 ÇO |
1773 | if (pid == 0) { // child |
1774 | int ret = 1, nbytes; | |
1775 | struct ifaddrs *interfaceArray = NULL, *tempIfAddr = NULL; | |
1776 | char addressOutputBuffer[INET6_ADDRSTRLEN]; | |
1777 | void *tempAddrPtr = NULL; | |
1778 | char *address = NULL; | |
fe218ca3 | 1779 | |
ae22a220 ÇO |
1780 | /* close the read-end of the pipe */ |
1781 | close(pipefd[0]); | |
1782 | ||
e0f59189 | 1783 | if (!enter_net_ns(c)) { |
ae22a220 ÇO |
1784 | SYSERROR("failed to enter namespace"); |
1785 | goto out; | |
9c83a661 | 1786 | } |
ae22a220 ÇO |
1787 | |
1788 | /* Grab the list of interfaces */ | |
1789 | if (getifaddrs(&interfaceArray)) { | |
1790 | SYSERROR("failed to get interfaces list"); | |
1791 | goto out; | |
1792 | } | |
1793 | ||
1794 | /* Iterate through the interfaces */ | |
1795 | for (tempIfAddr = interfaceArray; tempIfAddr != NULL; tempIfAddr = tempIfAddr->ifa_next) { | |
1796 | if (tempIfAddr->ifa_addr == NULL) | |
9c83a661 SG |
1797 | continue; |
1798 | ||
ae22a220 ÇO |
1799 | if(tempIfAddr->ifa_addr->sa_family == AF_INET) { |
1800 | if (family && strcmp(family, "inet")) | |
1801 | continue; | |
1802 | tempAddrPtr = &((struct sockaddr_in *)tempIfAddr->ifa_addr)->sin_addr; | |
1803 | } | |
1804 | else { | |
1805 | if (family && strcmp(family, "inet6")) | |
1806 | continue; | |
1807 | ||
1808 | if (((struct sockaddr_in6 *)tempIfAddr->ifa_addr)->sin6_scope_id != scope) | |
1809 | continue; | |
1810 | ||
1811 | tempAddrPtr = &((struct sockaddr_in6 *)tempIfAddr->ifa_addr)->sin6_addr; | |
1812 | } | |
1813 | ||
1814 | if (interface && strcmp(interface, tempIfAddr->ifa_name)) | |
1815 | continue; | |
1816 | else if (!interface && strcmp("lo", tempIfAddr->ifa_name) == 0) | |
9c83a661 SG |
1817 | continue; |
1818 | ||
ae22a220 ÇO |
1819 | address = (char *)inet_ntop(tempIfAddr->ifa_addr->sa_family, |
1820 | tempAddrPtr, | |
1821 | addressOutputBuffer, | |
1822 | sizeof(addressOutputBuffer)); | |
1823 | if (!address) | |
1824 | continue; | |
1825 | ||
1826 | nbytes = write(pipefd[1], address, INET6_ADDRSTRLEN); | |
1827 | if (nbytes < 0) { | |
1828 | ERROR("write failed"); | |
1829 | goto out; | |
1830 | } | |
1831 | count++; | |
9c83a661 | 1832 | } |
ae22a220 | 1833 | ret = 0; |
9c83a661 | 1834 | |
ae22a220 ÇO |
1835 | out: |
1836 | if(interfaceArray) | |
1837 | freeifaddrs(interfaceArray); | |
9c83a661 | 1838 | |
ae22a220 ÇO |
1839 | /* close the write-end of the pipe, thus sending EOF to the reader */ |
1840 | close(pipefd[1]); | |
1841 | exit(ret); | |
6849cb5b | 1842 | } |
9c83a661 | 1843 | |
ae22a220 ÇO |
1844 | /* close the write-end of the pipe */ |
1845 | close(pipefd[1]); | |
1846 | ||
358afd84 | 1847 | while (read(pipefd[0], &address, INET6_ADDRSTRLEN) == INET6_ADDRSTRLEN) { |
9c88ff1f | 1848 | if(!add_to_array(&addresses, address, count)) |
ae22a220 | 1849 | ERROR("PARENT: add_to_array failed"); |
9c88ff1f | 1850 | count++; |
9c83a661 SG |
1851 | } |
1852 | ||
ae22a220 ÇO |
1853 | if (wait_for_pid(pid) != 0) { |
1854 | for(i=0;i<count;i++) | |
1855 | free(addresses[i]); | |
1856 | free(addresses); | |
1857 | addresses = NULL; | |
1858 | } | |
9c83a661 | 1859 | |
ae22a220 ÇO |
1860 | /* close the read-end of the pipe */ |
1861 | close(pipefd[0]); | |
9c83a661 SG |
1862 | |
1863 | /* Append NULL to the array */ | |
9c88ff1f ÇO |
1864 | if(addresses) |
1865 | addresses = (char **)lxc_append_null_to_array((void **)addresses, count); | |
9c83a661 SG |
1866 | |
1867 | return addresses; | |
1868 | } | |
1869 | ||
858377e4 SH |
1870 | WRAP_API_3(char **, lxcapi_get_ips, const char *, const char *, int) |
1871 | ||
1872 | static int do_lxcapi_get_config_item(struct lxc_container *c, const char *key, char *retv, int inlen) | |
72d0e1cb SG |
1873 | { |
1874 | int ret; | |
1875 | ||
1876 | if (!c || !c->lxc_conf) | |
1877 | return -1; | |
5cee8c50 | 1878 | if (container_mem_lock(c)) |
72d0e1cb | 1879 | return -1; |
72d0e1cb | 1880 | ret = lxc_get_config_item(c->lxc_conf, key, retv, inlen); |
5cee8c50 | 1881 | container_mem_unlock(c); |
72d0e1cb SG |
1882 | return ret; |
1883 | } | |
1884 | ||
858377e4 SH |
1885 | WRAP_API_3(int, lxcapi_get_config_item, const char *, char *, int) |
1886 | ||
1887 | static char* do_lxcapi_get_running_config_item(struct lxc_container *c, const char *key) | |
8ac18377 ÇO |
1888 | { |
1889 | char *ret; | |
1890 | ||
1891 | if (!c || !c->lxc_conf) | |
1892 | return NULL; | |
1893 | if (container_mem_lock(c)) | |
1894 | return NULL; | |
858377e4 | 1895 | ret = lxc_cmd_get_config_item(c->name, key, do_lxcapi_get_config_path(c)); |
8ac18377 ÇO |
1896 | container_mem_unlock(c); |
1897 | return ret; | |
1898 | } | |
1899 | ||
858377e4 SH |
1900 | WRAP_API_1(char *, lxcapi_get_running_config_item, const char *) |
1901 | ||
1902 | static int do_lxcapi_get_keys(struct lxc_container *c, const char *key, char *retv, int inlen) | |
72d0e1cb SG |
1903 | { |
1904 | if (!key) | |
1905 | return lxc_listconfigs(retv, inlen); | |
1906 | /* | |
1907 | * Support 'lxc.network.<idx>', i.e. 'lxc.network.0' | |
1908 | * This is an intelligent result to show which keys are valid given | |
1909 | * the type of nic it is | |
1910 | */ | |
1911 | if (!c || !c->lxc_conf) | |
1912 | return -1; | |
5cee8c50 | 1913 | if (container_mem_lock(c)) |
72d0e1cb SG |
1914 | return -1; |
1915 | int ret = -1; | |
1916 | if (strncmp(key, "lxc.network.", 12) == 0) | |
6849cb5b | 1917 | ret = lxc_list_nicconfigs(c->lxc_conf, key, retv, inlen); |
5cee8c50 | 1918 | container_mem_unlock(c); |
72d0e1cb SG |
1919 | return ret; |
1920 | } | |
1921 | ||
858377e4 SH |
1922 | WRAP_API_3(int, lxcapi_get_keys, const char *, char *, int) |
1923 | ||
1924 | static bool do_lxcapi_save_config(struct lxc_container *c, const char *alt_file) | |
72d0e1cb | 1925 | { |
39dc698c SH |
1926 | FILE *fout; |
1927 | bool ret = false, need_disklock = false; | |
1928 | int lret; | |
1929 | ||
72d0e1cb SG |
1930 | if (!alt_file) |
1931 | alt_file = c->configfile; | |
1932 | if (!alt_file) | |
6849cb5b | 1933 | return false; // should we write to stdout if no file is specified? |
39dc698c SH |
1934 | |
1935 | // If we haven't yet loaded a config, load the stock config | |
1936 | if (!c->lxc_conf) { | |
858377e4 | 1937 | if (!do_lxcapi_load_config(c, lxc_global_config_value("lxc.default_config"))) { |
959aee9c | 1938 | ERROR("Error loading default configuration file %s while saving %s", lxc_global_config_value("lxc.default_config"), c->name); |
72d0e1cb SG |
1939 | return false; |
1940 | } | |
39dc698c | 1941 | } |
72d0e1cb | 1942 | |
5a3d2e1e SG |
1943 | if (!create_container_dir(c)) |
1944 | return false; | |
1945 | ||
39dc698c SH |
1946 | /* |
1947 | * If we're writing to the container's config file, take the | |
1948 | * disk lock. Otherwise just take the memlock to protect the | |
1949 | * struct lxc_container while we're traversing it. | |
1950 | */ | |
1951 | if (strcmp(c->configfile, alt_file) == 0) | |
1952 | need_disklock = true; | |
1953 | ||
1954 | if (need_disklock) | |
1955 | lret = container_disk_lock(c); | |
1956 | else | |
1957 | lret = container_mem_lock(c); | |
1958 | ||
1959 | if (lret) | |
72d0e1cb | 1960 | return false; |
39dc698c SH |
1961 | |
1962 | fout = fopen(alt_file, "w"); | |
1963 | if (!fout) | |
1964 | goto out; | |
6b0d5538 | 1965 | write_config(fout, c->lxc_conf); |
72d0e1cb | 1966 | fclose(fout); |
39dc698c SH |
1967 | ret = true; |
1968 | ||
1969 | out: | |
1970 | if (need_disklock) | |
1971 | container_disk_unlock(c); | |
1972 | else | |
1973 | container_mem_unlock(c); | |
1974 | return ret; | |
72d0e1cb SG |
1975 | } |
1976 | ||
858377e4 SH |
1977 | WRAP_API_1(bool, lxcapi_save_config, const char *) |
1978 | ||
0ea055b3 CB |
1979 | |
1980 | static bool mod_rdep(struct lxc_container *c0, struct lxc_container *c, bool inc) | |
dfb31b25 | 1981 | { |
0ea055b3 CB |
1982 | FILE *f1; |
1983 | struct stat fbuf; | |
1984 | char *buf = NULL; | |
1985 | char *del; | |
dfb31b25 | 1986 | char path[MAXPATHLEN]; |
0ea055b3 CB |
1987 | char newpath[MAXPATHLEN]; |
1988 | int fd, ret, n = 0, v = 0; | |
dfb31b25 | 1989 | bool bret = false; |
0ea055b3 | 1990 | size_t len; |
dfb31b25 | 1991 | |
0ea055b3 | 1992 | if (container_disk_lock(c0)) |
dfb31b25 | 1993 | return false; |
0ea055b3 CB |
1994 | |
1995 | ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_snapshots", c0->config_path, c0->name); | |
dfb31b25 SH |
1996 | if (ret < 0 || ret > MAXPATHLEN) |
1997 | goto out; | |
0ea055b3 CB |
1998 | ret = snprintf(newpath, MAXPATHLEN, "%s\n%s\n", c->config_path, c->name); |
1999 | if (ret < 0 || ret > MAXPATHLEN) | |
dfb31b25 | 2000 | goto out; |
0ea055b3 CB |
2001 | |
2002 | /* If we find an lxc-snapshot file using the old format only listing the | |
2003 | * number of snapshots we will keep using it. */ | |
2004 | f1 = fopen(path, "r"); | |
2005 | if (f1) { | |
2006 | n = fscanf(f1, "%d", &v); | |
2007 | fclose(f1); | |
2008 | if (n == 1 && v == 0) { | |
2009 | remove(path); | |
2010 | n = 0; | |
2011 | } | |
dfb31b25 | 2012 | } |
0ea055b3 CB |
2013 | if (n == 1) { |
2014 | v += inc ? 1 : -1; | |
2015 | f1 = fopen(path, "w"); | |
2016 | if (!f1) | |
2017 | goto out; | |
2018 | if (fprintf(f1, "%d\n", v) < 0) { | |
2019 | ERROR("Error writing new snapshots value"); | |
2020 | fclose(f1); | |
2021 | goto out; | |
2022 | } | |
2023 | ret = fclose(f1); | |
2024 | if (ret != 0) { | |
2025 | SYSERROR("Error writing to or closing snapshots file"); | |
2026 | goto out; | |
2027 | } | |
2028 | } else { | |
2029 | /* Here we know that we have or can use an lxc-snapshot file | |
2030 | * using the new format. */ | |
2031 | if (inc) { | |
2032 | f1 = fopen(path, "a"); | |
2033 | if (!f1) | |
2034 | goto out; | |
2035 | ||
2036 | if (fprintf(f1, "%s", newpath) < 0) { | |
2037 | ERROR("Error writing new snapshots entry"); | |
2038 | ret = fclose(f1); | |
2039 | if (ret != 0) | |
2040 | SYSERROR("Error writing to or closing snapshots file"); | |
2041 | goto out; | |
2042 | } | |
2043 | ||
2044 | ret = fclose(f1); | |
2045 | if (ret != 0) { | |
2046 | SYSERROR("Error writing to or closing snapshots file"); | |
2047 | goto out; | |
2048 | } | |
2049 | } else if (!inc) { | |
2050 | fd = open(path, O_RDWR | O_CLOEXEC); | |
2051 | if (fd < 0) | |
2052 | goto out; | |
2053 | ||
2054 | ret = fstat(fd, &fbuf); | |
2055 | if (ret < 0) { | |
2056 | close(fd); | |
2057 | goto out; | |
2058 | } | |
2059 | ||
2060 | if (fbuf.st_size != 0) { | |
2061 | buf = mmap(NULL, fbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); | |
2062 | if (buf == MAP_FAILED) { | |
2063 | SYSERROR("Failed to create mapping %s", path); | |
2064 | close(fd); | |
2065 | goto out; | |
2066 | } | |
2067 | } | |
2068 | ||
2069 | len = strlen(newpath); | |
2070 | ||
2071 | /* mmap()ed memory is only \0-terminated when it is not | |
2072 | * a multiple of a pagesize. Hence, we'll use memmem(). */ | |
2073 | if ((del = memmem(buf, fbuf.st_size, newpath, len))) { | |
2074 | /* remove container entry */ | |
2075 | memmove(del, del + len, strlen(del) - len + 1); | |
2076 | ||
2077 | munmap(buf, fbuf.st_size); | |
2078 | ||
2079 | if (ftruncate(fd, fbuf.st_size - len) < 0) { | |
2080 | SYSERROR("Failed to truncate file %s", path); | |
2081 | close(fd); | |
2082 | goto out; | |
2083 | } | |
2084 | } else { | |
2085 | munmap(buf, fbuf.st_size); | |
2086 | } | |
2087 | ||
2088 | close(fd); | |
2089 | } | |
2090 | ||
2091 | /* If the lxc-snapshot file is empty, remove it. */ | |
2092 | if (stat(path, &fbuf) < 0) | |
2093 | goto out; | |
2094 | if (!fbuf.st_size) { | |
2095 | remove(path); | |
2096 | } | |
dfb31b25 SH |
2097 | } |
2098 | ||
2099 | bret = true; | |
2100 | ||
2101 | out: | |
0ea055b3 | 2102 | container_disk_unlock(c0); |
dfb31b25 SH |
2103 | return bret; |
2104 | } | |
2105 | ||
2106 | static void strip_newline(char *p) | |
2107 | { | |
2108 | size_t len = strlen(p); | |
2109 | if (len < 1) | |
2110 | return; | |
2111 | if (p[len-1] == '\n') | |
2112 | p[len-1] = '\0'; | |
2113 | } | |
2114 | ||
2115 | static void mod_all_rdeps(struct lxc_container *c, bool inc) | |
2116 | { | |
2117 | struct lxc_container *p; | |
2118 | char *lxcpath = NULL, *lxcname = NULL, path[MAXPATHLEN]; | |
2119 | size_t pathlen = 0, namelen = 0; | |
2120 | FILE *f; | |
2121 | int ret; | |
2122 | ||
2123 | ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_rdepends", | |
2124 | c->config_path, c->name); | |
2125 | if (ret < 0 || ret >= MAXPATHLEN) { | |
2126 | ERROR("Path name too long"); | |
2127 | return; | |
2128 | } | |
025ed0f3 | 2129 | f = fopen(path, "r"); |
025ed0f3 | 2130 | if (f == NULL) |
dfb31b25 SH |
2131 | return; |
2132 | while (getline(&lxcpath, &pathlen, f) != -1) { | |
2133 | if (getline(&lxcname, &namelen, f) == -1) { | |
959aee9c | 2134 | ERROR("badly formatted file %s", path); |
dfb31b25 SH |
2135 | goto out; |
2136 | } | |
2137 | strip_newline(lxcpath); | |
2138 | strip_newline(lxcname); | |
2139 | if ((p = lxc_container_new(lxcname, lxcpath)) == NULL) { | |
2140 | ERROR("Unable to find dependent container %s:%s", | |
2141 | lxcpath, lxcname); | |
2142 | continue; | |
2143 | } | |
0ea055b3 CB |
2144 | if (!mod_rdep(p, c, inc)) |
2145 | ERROR("Failed to update snapshots file for %s:%s", | |
dfb31b25 SH |
2146 | lxcpath, lxcname); |
2147 | lxc_container_put(p); | |
2148 | } | |
2149 | out: | |
f10fad2f ME |
2150 | free(lxcpath); |
2151 | free(lxcname); | |
dfb31b25 SH |
2152 | fclose(f); |
2153 | } | |
2154 | ||
18aa217b | 2155 | static bool has_fs_snapshots(struct lxc_container *c) |
dfb31b25 | 2156 | { |
0ea055b3 | 2157 | FILE *f; |
dfb31b25 SH |
2158 | char path[MAXPATHLEN]; |
2159 | int ret, v; | |
0ea055b3 | 2160 | struct stat fbuf; |
dfb31b25 SH |
2161 | bool bret = false; |
2162 | ||
2163 | ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_snapshots", c->config_path, | |
2164 | c->name); | |
2165 | if (ret < 0 || ret > MAXPATHLEN) | |
2166 | goto out; | |
0ea055b3 CB |
2167 | /* If the file doesn't exist there are no snapshots. */ |
2168 | if (stat(path, &fbuf) < 0) | |
dfb31b25 | 2169 | goto out; |
0ea055b3 CB |
2170 | v = fbuf.st_size; |
2171 | if (v != 0) { | |
2172 | f = fopen(path, "r"); | |
2173 | if (!f) | |
2174 | goto out; | |
2175 | ret = fscanf(f, "%d", &v); | |
2176 | fclose(f); | |
2177 | // TODO: Figure out what to do with the return value of fscanf. | |
2178 | if (ret != 1) | |
2179 | INFO("Container uses new lxc-snapshots format %s", path); | |
2180 | } | |
dfb31b25 SH |
2181 | bret = v != 0; |
2182 | ||
2183 | out: | |
2184 | return bret; | |
2185 | } | |
2186 | ||
18aa217b SH |
2187 | static bool has_snapshots(struct lxc_container *c) |
2188 | { | |
2189 | char path[MAXPATHLEN]; | |
2190 | struct dirent dirent, *direntp; | |
2191 | int count=0; | |
2192 | DIR *dir; | |
2193 | ||
2194 | if (!get_snappath_dir(c, path)) | |
2195 | return false; | |
2196 | dir = opendir(path); | |
2197 | if (!dir) | |
2198 | return false; | |
2199 | while (!readdir_r(dir, &dirent, &direntp)) { | |
2200 | if (!direntp) | |
2201 | break; | |
2202 | ||
2203 | if (!strcmp(direntp->d_name, ".")) | |
2204 | continue; | |
2205 | ||
2206 | if (!strcmp(direntp->d_name, "..")) | |
2207 | continue; | |
2208 | count++; | |
2209 | break; | |
2210 | } | |
2211 | closedir(dir); | |
2212 | return count > 0; | |
2213 | } | |
2214 | ||
4355ab5f SH |
2215 | static int lxc_rmdir_onedev_wrapper(void *data) |
2216 | { | |
2217 | char *arg = (char *) data; | |
18aa217b | 2218 | return lxc_rmdir_onedev(arg, "snaps"); |
4355ab5f SH |
2219 | } |
2220 | ||
44a706bd SH |
2221 | static int do_bdev_destroy(struct lxc_conf *conf) |
2222 | { | |
2223 | struct bdev *r; | |
2224 | int ret = 0; | |
2225 | ||
76a26f55 | 2226 | r = bdev_init(conf, conf->rootfs.path, conf->rootfs.mount, NULL); |
44a706bd SH |
2227 | if (!r) |
2228 | return -1; | |
2229 | ||
2230 | if (r->ops->destroy(r) < 0) | |
2231 | ret = -1; | |
2232 | bdev_put(r); | |
2233 | return ret; | |
2234 | } | |
2235 | ||
2236 | static int bdev_destroy_wrapper(void *data) | |
2237 | { | |
2238 | struct lxc_conf *conf = data; | |
2239 | ||
2240 | if (setgid(0) < 0) { | |
2241 | ERROR("Failed to setgid to 0"); | |
2242 | return -1; | |
2243 | } | |
2244 | if (setgroups(0, NULL) < 0) | |
2245 | WARN("Failed to clear groups"); | |
2246 | if (setuid(0) < 0) { | |
2247 | ERROR("Failed to setuid to 0"); | |
2248 | return -1; | |
2249 | } | |
2250 | return do_bdev_destroy(conf); | |
2251 | } | |
2252 | ||
18aa217b | 2253 | static bool container_destroy(struct lxc_container *c) |
72d0e1cb | 2254 | { |
c868b261 | 2255 | bool bret = false; |
fef48dc9 | 2256 | int ret; |
a70a69e8 | 2257 | struct lxc_conf *conf; |
72d0e1cb | 2258 | |
858377e4 | 2259 | if (!c || !do_lxcapi_is_defined(c)) |
5a3d2e1e SG |
2260 | return false; |
2261 | ||
a70a69e8 | 2262 | conf = c->lxc_conf; |
3bc449ed | 2263 | if (container_disk_lock(c)) |
72d0e1cb | 2264 | return false; |
72d0e1cb | 2265 | |
39dc698c | 2266 | if (!is_stopped(c)) { |
60bf62d4 SH |
2267 | // we should queue some sort of error - in c->error_string? |
2268 | ERROR("container %s is not stopped", c->name); | |
2269 | goto out; | |
72d0e1cb SG |
2270 | } |
2271 | ||
a70a69e8 | 2272 | if (conf && !lxc_list_empty(&conf->hooks[LXCHOOK_DESTROY])) { |
37cf711b | 2273 | /* Start of environment variable setup for hooks */ |
ab7efcf5 | 2274 | if (c->name && setenv("LXC_NAME", c->name, 1)) { |
37cf711b SY |
2275 | SYSERROR("failed to set environment variable for container name"); |
2276 | } | |
ab7efcf5 | 2277 | if (conf->rcfile && setenv("LXC_CONFIG_FILE", conf->rcfile, 1)) { |
37cf711b SY |
2278 | SYSERROR("failed to set environment variable for config path"); |
2279 | } | |
ab7efcf5 | 2280 | if (conf->rootfs.mount && setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1)) { |
37cf711b SY |
2281 | SYSERROR("failed to set environment variable for rootfs mount"); |
2282 | } | |
ab7efcf5 | 2283 | if (conf->rootfs.path && setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1)) { |
37cf711b SY |
2284 | SYSERROR("failed to set environment variable for rootfs mount"); |
2285 | } | |
2286 | if (conf->console.path && setenv("LXC_CONSOLE", conf->console.path, 1)) { | |
2287 | SYSERROR("failed to set environment variable for console path"); | |
2288 | } | |
2289 | if (conf->console.log_path && setenv("LXC_CONSOLE_LOGPATH", conf->console.log_path, 1)) { | |
2290 | SYSERROR("failed to set environment variable for console log"); | |
2291 | } | |
2292 | /* End of environment variable setup for hooks */ | |
2293 | ||
2294 | if (run_lxc_hooks(c->name, "destroy", conf, c->get_config_path(c), NULL)) { | |
2295 | ERROR("Error executing clone hook for %s", c->name); | |
2296 | goto out; | |
2297 | } | |
2298 | } | |
2299 | ||
2300 | if (current_config && conf == current_config) { | |
858377e4 | 2301 | current_config = NULL; |
37cf711b SY |
2302 | if (conf->logfd != -1) { |
2303 | close(conf->logfd); | |
2304 | conf->logfd = -1; | |
858377e4 SH |
2305 | } |
2306 | } | |
2307 | ||
37cf711b | 2308 | if (conf && conf->rootfs.path && conf->rootfs.mount) { |
44a706bd | 2309 | if (am_unpriv()) |
37cf711b | 2310 | ret = userns_exec_1(conf, bdev_destroy_wrapper, conf); |
44a706bd | 2311 | else |
37cf711b | 2312 | ret = do_bdev_destroy(conf); |
44a706bd SH |
2313 | if (ret < 0) { |
2314 | ERROR("Error destroying rootfs for %s", c->name); | |
2315 | goto out; | |
60bf62d4 SH |
2316 | } |
2317 | } | |
2318 | ||
dfb31b25 SH |
2319 | mod_all_rdeps(c, false); |
2320 | ||
858377e4 | 2321 | const char *p1 = do_lxcapi_get_config_path(c); |
60bf62d4 SH |
2322 | char *path = alloca(strlen(p1) + strlen(c->name) + 2); |
2323 | sprintf(path, "%s/%s", p1, c->name); | |
c868b261 | 2324 | if (am_unpriv()) |
37cf711b | 2325 | ret = userns_exec_1(conf, lxc_rmdir_onedev_wrapper, path); |
4355ab5f | 2326 | else |
18aa217b | 2327 | ret = lxc_rmdir_onedev(path, "snaps"); |
4355ab5f | 2328 | if (ret < 0) { |
60bf62d4 SH |
2329 | ERROR("Error destroying container directory for %s", c->name); |
2330 | goto out; | |
2331 | } | |
fef48dc9 | 2332 | bret = true; |
60bf62d4 SH |
2333 | |
2334 | out: | |
3bc449ed | 2335 | container_disk_unlock(c); |
fef48dc9 | 2336 | return bret; |
72d0e1cb SG |
2337 | } |
2338 | ||
858377e4 | 2339 | static bool do_lxcapi_destroy(struct lxc_container *c) |
18aa217b SH |
2340 | { |
2341 | if (!c || !lxcapi_is_defined(c)) | |
2342 | return false; | |
2343 | if (has_snapshots(c)) { | |
2344 | ERROR("Container %s has snapshots; not removing", c->name); | |
2345 | return false; | |
2346 | } | |
2347 | ||
2348 | if (has_fs_snapshots(c)) { | |
2349 | ERROR("container %s has snapshots on its rootfs", c->name); | |
2350 | return false; | |
2351 | } | |
2352 | ||
2353 | return container_destroy(c); | |
2354 | } | |
2355 | ||
858377e4 | 2356 | WRAP_API(bool, lxcapi_destroy) |
18aa217b | 2357 | |
858377e4 | 2358 | static bool do_lxcapi_destroy_with_snapshots(struct lxc_container *c) |
18aa217b SH |
2359 | { |
2360 | if (!c || !lxcapi_is_defined(c)) | |
2361 | return false; | |
2362 | if (!lxcapi_snapshot_destroy_all(c)) { | |
2363 | ERROR("Error deleting all snapshots"); | |
2364 | return false; | |
2365 | } | |
2366 | return lxcapi_destroy(c); | |
2367 | } | |
2368 | ||
858377e4 SH |
2369 | WRAP_API(bool, lxcapi_destroy_with_snapshots) |
2370 | ||
96532523 SH |
2371 | static bool set_config_item_locked(struct lxc_container *c, const char *key, const char *v) |
2372 | { | |
2373 | struct lxc_config_t *config; | |
2374 | ||
2375 | if (!c->lxc_conf) | |
2376 | c->lxc_conf = lxc_conf_init(); | |
6b0d5538 | 2377 | if (!c->lxc_conf) |
96532523 SH |
2378 | return false; |
2379 | config = lxc_getconfig(key); | |
2380 | if (!config) | |
2381 | return false; | |
6b0d5538 | 2382 | if (config->cb(key, v, c->lxc_conf) != 0) |
f979ac15 | 2383 | return false; |
6b0d5538 | 2384 | return do_append_unexp_config_line(c->lxc_conf, key, v); |
96532523 SH |
2385 | } |
2386 | ||
858377e4 | 2387 | static bool do_lxcapi_set_config_item(struct lxc_container *c, const char *key, const char *v) |
72d0e1cb | 2388 | { |
72d0e1cb | 2389 | bool b = false; |
72d0e1cb SG |
2390 | |
2391 | if (!c) | |
2392 | return false; | |
2393 | ||
5cee8c50 | 2394 | if (container_mem_lock(c)) |
72d0e1cb SG |
2395 | return false; |
2396 | ||
96532523 | 2397 | b = set_config_item_locked(c, key, v); |
72d0e1cb | 2398 | |
5cee8c50 | 2399 | container_mem_unlock(c); |
72d0e1cb SG |
2400 | return b; |
2401 | } | |
2402 | ||
858377e4 SH |
2403 | WRAP_API_2(bool, lxcapi_set_config_item, const char *, const char *) |
2404 | ||
72d0e1cb SG |
2405 | static char *lxcapi_config_file_name(struct lxc_container *c) |
2406 | { | |
2407 | if (!c || !c->configfile) | |
2408 | return NULL; | |
2409 | return strdup(c->configfile); | |
2410 | } | |
2411 | ||
2a59a681 SH |
2412 | static const char *lxcapi_get_config_path(struct lxc_container *c) |
2413 | { | |
2414 | if (!c || !c->config_path) | |
2415 | return NULL; | |
2416 | return (const char *)(c->config_path); | |
2417 | } | |
2418 | ||
afeecbba SH |
2419 | /* |
2420 | * not for export | |
2421 | * Just recalculate the c->configfile based on the | |
2422 | * c->config_path, which must be set. | |
2423 | * The lxc_container must be locked or not yet public. | |
2424 | */ | |
2425 | static bool set_config_filename(struct lxc_container *c) | |
2426 | { | |
2427 | char *newpath; | |
2428 | int len, ret; | |
2429 | ||
2430 | if (!c->config_path) | |
2431 | return false; | |
2432 | ||
2433 | /* $lxc_path + "/" + c->name + "/" + "config" + '\0' */ | |
2434 | len = strlen(c->config_path) + strlen(c->name) + strlen("config") + 3; | |
2435 | newpath = malloc(len); | |
2436 | if (!newpath) | |
2437 | return false; | |
2438 | ||
2439 | ret = snprintf(newpath, len, "%s/%s/config", c->config_path, c->name); | |
2440 | if (ret < 0 || ret >= len) { | |
2441 | fprintf(stderr, "Error printing out config file name\n"); | |
2442 | free(newpath); | |
2443 | return false; | |
2444 | } | |
2445 | ||
f10fad2f | 2446 | free(c->configfile); |
afeecbba SH |
2447 | c->configfile = newpath; |
2448 | ||
2449 | return true; | |
2450 | } | |
2451 | ||
858377e4 | 2452 | static bool do_lxcapi_set_config_path(struct lxc_container *c, const char *path) |
2a59a681 SH |
2453 | { |
2454 | char *p; | |
2455 | bool b = false; | |
afeecbba | 2456 | char *oldpath = NULL; |
2a59a681 SH |
2457 | |
2458 | if (!c) | |
2459 | return b; | |
2460 | ||
5cee8c50 | 2461 | if (container_mem_lock(c)) |
2a59a681 SH |
2462 | return b; |
2463 | ||
2464 | p = strdup(path); | |
afeecbba SH |
2465 | if (!p) { |
2466 | ERROR("Out of memory setting new lxc path"); | |
2a59a681 | 2467 | goto err; |
afeecbba SH |
2468 | } |
2469 | ||
2a59a681 SH |
2470 | b = true; |
2471 | if (c->config_path) | |
afeecbba | 2472 | oldpath = c->config_path; |
2a59a681 | 2473 | c->config_path = p; |
afeecbba SH |
2474 | |
2475 | /* Since we've changed the config path, we have to change the | |
2476 | * config file name too */ | |
2477 | if (!set_config_filename(c)) { | |
2478 | ERROR("Out of memory setting new config filename"); | |
2479 | b = false; | |
2480 | free(c->config_path); | |
2481 | c->config_path = oldpath; | |
2482 | oldpath = NULL; | |
2483 | } | |
2a59a681 | 2484 | err: |
f10fad2f | 2485 | free(oldpath); |
5cee8c50 | 2486 | container_mem_unlock(c); |
2a59a681 SH |
2487 | return b; |
2488 | } | |
2489 | ||
858377e4 | 2490 | WRAP_API_1(bool, lxcapi_set_config_path, const char *) |
2a59a681 | 2491 | |
858377e4 | 2492 | static bool do_lxcapi_set_cgroup_item(struct lxc_container *c, const char *subsys, const char *value) |
794dd120 SH |
2493 | { |
2494 | int ret; | |
794dd120 SH |
2495 | |
2496 | if (!c) | |
2497 | return false; | |
2498 | ||
3bc449ed | 2499 | if (is_stopped(c)) |
794dd120 SH |
2500 | return false; |
2501 | ||
3bc449ed SH |
2502 | if (container_disk_lock(c)) |
2503 | return false; | |
794dd120 | 2504 | |
33ad9f1a | 2505 | ret = lxc_cgroup_set(subsys, value, c->name, c->config_path); |
3bc449ed SH |
2506 | |
2507 | container_disk_unlock(c); | |
2508 | return ret == 0; | |
794dd120 SH |
2509 | } |
2510 | ||
858377e4 SH |
2511 | WRAP_API_2(bool, lxcapi_set_cgroup_item, const char *, const char *) |
2512 | ||
2513 | static int do_lxcapi_get_cgroup_item(struct lxc_container *c, const char *subsys, char *retv, int inlen) | |
794dd120 | 2514 | { |
3bc449ed | 2515 | int ret; |
794dd120 | 2516 | |
6502006a | 2517 | if (!c) |
794dd120 SH |
2518 | return -1; |
2519 | ||
3bc449ed | 2520 | if (is_stopped(c)) |
794dd120 SH |
2521 | return -1; |
2522 | ||
3bc449ed SH |
2523 | if (container_disk_lock(c)) |
2524 | return -1; | |
794dd120 | 2525 | |
33ad9f1a | 2526 | ret = lxc_cgroup_get(subsys, retv, inlen, c->name, c->config_path); |
794dd120 | 2527 | |
3bc449ed | 2528 | container_disk_unlock(c); |
794dd120 SH |
2529 | return ret; |
2530 | } | |
2531 | ||
858377e4 SH |
2532 | WRAP_API_3(int, lxcapi_get_cgroup_item, const char *, char *, int) |
2533 | ||
593e8478 | 2534 | const char *lxc_get_global_config_item(const char *key) |
83c98d82 | 2535 | { |
593e8478 | 2536 | return lxc_global_config_value(key); |
a8428dfa SH |
2537 | } |
2538 | ||
b6b918a1 SG |
2539 | const char *lxc_get_version(void) |
2540 | { | |
95ee490b | 2541 | return LXC_VERSION; |
b6b918a1 SG |
2542 | } |
2543 | ||
f0ca2726 | 2544 | static int copy_file(const char *old, const char *new) |
9be53773 SH |
2545 | { |
2546 | int in, out; | |
2547 | ssize_t len, ret; | |
2548 | char buf[8096]; | |
2549 | struct stat sbuf; | |
2550 | ||
2551 | if (file_exists(new)) { | |
2552 | ERROR("copy destination %s exists", new); | |
2553 | return -1; | |
2554 | } | |
2555 | ret = stat(old, &sbuf); | |
2556 | if (ret < 0) { | |
dfb31b25 | 2557 | INFO("Error stat'ing %s", old); |
9be53773 SH |
2558 | return -1; |
2559 | } | |
2560 | ||
2561 | in = open(old, O_RDONLY); | |
2562 | if (in < 0) { | |
dfb31b25 | 2563 | SYSERROR("Error opening original file %s", old); |
9be53773 SH |
2564 | return -1; |
2565 | } | |
2566 | out = open(new, O_CREAT | O_EXCL | O_WRONLY, 0644); | |
2567 | if (out < 0) { | |
dfb31b25 | 2568 | SYSERROR("Error opening new file %s", new); |
9be53773 SH |
2569 | close(in); |
2570 | return -1; | |
2571 | } | |
2572 | ||
2573 | while (1) { | |
2574 | len = read(in, buf, 8096); | |
2575 | if (len < 0) { | |
dfb31b25 | 2576 | SYSERROR("Error reading old file %s", old); |
9be53773 SH |
2577 | goto err; |
2578 | } | |
2579 | if (len == 0) | |
2580 | break; | |
2581 | ret = write(out, buf, len); | |
6849cb5b | 2582 | if (ret < len) { // should we retry? |
dfb31b25 | 2583 | SYSERROR("Error: write to new file %s was interrupted", new); |
9be53773 SH |
2584 | goto err; |
2585 | } | |
2586 | } | |
2587 | close(in); | |
2588 | close(out); | |
2589 | ||
2590 | // we set mode, but not owner/group | |
2591 | ret = chmod(new, sbuf.st_mode); | |
2592 | if (ret) { | |
dfb31b25 | 2593 | SYSERROR("Error setting mode on %s", new); |
9be53773 SH |
2594 | return -1; |
2595 | } | |
2596 | ||
2597 | return 0; | |
2598 | ||
2599 | err: | |
2600 | close(in); | |
2601 | close(out); | |
2602 | return -1; | |
2603 | } | |
2604 | ||
9be53773 SH |
2605 | static int copyhooks(struct lxc_container *oldc, struct lxc_container *c) |
2606 | { | |
619256b5 | 2607 | int i, len, ret; |
9be53773 | 2608 | struct lxc_list *it; |
619256b5 ÇO |
2609 | char *cpath; |
2610 | ||
2611 | len = strlen(oldc->config_path) + strlen(oldc->name) + 3; | |
2612 | cpath = alloca(len); | |
2613 | ret = snprintf(cpath, len, "%s/%s/", oldc->config_path, oldc->name); | |
2614 | if (ret < 0 || ret >= len) | |
2615 | return -1; | |
9be53773 SH |
2616 | |
2617 | for (i=0; i<NUM_LXC_HOOKS; i++) { | |
2618 | lxc_list_for_each(it, &c->lxc_conf->hooks[i]) { | |
2619 | char *hookname = it->elem; | |
c32981c3 | 2620 | char *fname = strrchr(hookname, '/'); |
9be53773 SH |
2621 | char tmppath[MAXPATHLEN]; |
2622 | if (!fname) // relative path - we don't support, but maybe we should | |
2623 | return 0; | |
619256b5 ÇO |
2624 | if (strncmp(hookname, cpath, len - 1) != 0) { |
2625 | // this hook is public - ignore | |
2626 | continue; | |
2627 | } | |
9be53773 SH |
2628 | // copy the script, and change the entry in confile |
2629 | ret = snprintf(tmppath, MAXPATHLEN, "%s/%s/%s", | |
2630 | c->config_path, c->name, fname+1); | |
2631 | if (ret < 0 || ret >= MAXPATHLEN) | |
2632 | return -1; | |
2633 | ret = copy_file(it->elem, tmppath); | |
2634 | if (ret < 0) | |
2635 | return -1; | |
2636 | free(it->elem); | |
2637 | it->elem = strdup(tmppath); | |
2638 | if (!it->elem) { | |
2639 | ERROR("out of memory copying hook path"); | |
2640 | return -1; | |
2641 | } | |
9be53773 SH |
2642 | } |
2643 | } | |
2644 | ||
67702c21 SH |
2645 | if (!clone_update_unexp_hooks(c->lxc_conf, oldc->config_path, |
2646 | c->config_path, oldc->name, c->name)) { | |
6b0d5538 SH |
2647 | ERROR("Error saving new hooks in clone"); |
2648 | return -1; | |
2649 | } | |
858377e4 | 2650 | do_lxcapi_save_config(c, NULL); |
9be53773 SH |
2651 | return 0; |
2652 | } | |
2653 | ||
9be53773 SH |
2654 | |
2655 | static int copy_fstab(struct lxc_container *oldc, struct lxc_container *c) | |
2656 | { | |
2657 | char newpath[MAXPATHLEN]; | |
2658 | char *oldpath = oldc->lxc_conf->fstab; | |
2659 | int ret; | |
2660 | ||
2661 | if (!oldpath) | |
2662 | return 0; | |
2663 | ||
6b0d5538 SH |
2664 | clear_unexp_config_line(c->lxc_conf, "lxc.mount", false); |
2665 | ||
c32981c3 | 2666 | char *p = strrchr(oldpath, '/'); |
9be53773 SH |
2667 | if (!p) |
2668 | return -1; | |
2669 | ret = snprintf(newpath, MAXPATHLEN, "%s/%s%s", | |
2670 | c->config_path, c->name, p); | |
2671 | if (ret < 0 || ret >= MAXPATHLEN) { | |
2672 | ERROR("error printing new path for %s", oldpath); | |
2673 | return -1; | |
2674 | } | |
2675 | if (file_exists(newpath)) { | |
2676 | ERROR("error: fstab file %s exists", newpath); | |
2677 | return -1; | |
2678 | } | |
2679 | ||
2680 | if (copy_file(oldpath, newpath) < 0) { | |
2681 | ERROR("error: copying %s to %s", oldpath, newpath); | |
2682 | return -1; | |
2683 | } | |
2684 | free(c->lxc_conf->fstab); | |
2685 | c->lxc_conf->fstab = strdup(newpath); | |
2686 | if (!c->lxc_conf->fstab) { | |
2687 | ERROR("error: allocating pathname"); | |
2688 | return -1; | |
2689 | } | |
6b0d5538 SH |
2690 | if (!do_append_unexp_config_line(c->lxc_conf, "lxc.mount", newpath)) { |
2691 | ERROR("error saving new lxctab"); | |
2692 | return -1; | |
2693 | } | |
9be53773 SH |
2694 | |
2695 | return 0; | |
2696 | } | |
2697 | ||
dfb31b25 SH |
2698 | static void copy_rdepends(struct lxc_container *c, struct lxc_container *c0) |
2699 | { | |
2700 | char path0[MAXPATHLEN], path1[MAXPATHLEN]; | |
2701 | int ret; | |
2702 | ||
2703 | ret = snprintf(path0, MAXPATHLEN, "%s/%s/lxc_rdepends", c0->config_path, | |
2704 | c0->name); | |
2705 | if (ret < 0 || ret >= MAXPATHLEN) { | |
2706 | WARN("Error copying reverse dependencies"); | |
2707 | return; | |
2708 | } | |
2709 | ret = snprintf(path1, MAXPATHLEN, "%s/%s/lxc_rdepends", c->config_path, | |
2710 | c->name); | |
2711 | if (ret < 0 || ret >= MAXPATHLEN) { | |
2712 | WARN("Error copying reverse dependencies"); | |
2713 | return; | |
2714 | } | |
2715 | if (copy_file(path0, path1) < 0) { | |
2716 | INFO("Error copying reverse dependencies"); | |
2717 | return; | |
2718 | } | |
2719 | } | |
2720 | ||
2721 | static bool add_rdepends(struct lxc_container *c, struct lxc_container *c0) | |
2722 | { | |
2723 | int ret; | |
2724 | char path[MAXPATHLEN]; | |
2725 | FILE *f; | |
2726 | bool bret; | |
2727 | ||
2728 | ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_rdepends", c->config_path, | |
2729 | c->name); | |
2730 | if (ret < 0 || ret >= MAXPATHLEN) | |
2731 | return false; | |
2732 | f = fopen(path, "a"); | |
2733 | if (!f) | |
2734 | return false; | |
2735 | bret = true; | |
2736 | // if anything goes wrong, just return an error | |
2737 | if (fprintf(f, "%s\n%s\n", c0->config_path, c0->name) < 0) | |
2738 | bret = false; | |
2739 | if (fclose(f) != 0) | |
2740 | bret = false; | |
2741 | return bret; | |
2742 | } | |
2743 | ||
9be53773 | 2744 | static int copy_storage(struct lxc_container *c0, struct lxc_container *c, |
d659597e | 2745 | const char *newtype, int flags, const char *bdevdata, uint64_t newsize) |
9be53773 SH |
2746 | { |
2747 | struct bdev *bdev; | |
dfb31b25 | 2748 | int need_rdep; |
9be53773 | 2749 | |
1354955b | 2750 | bdev = bdev_copy(c0, c->name, c->config_path, newtype, flags, |
dfb31b25 | 2751 | bdevdata, newsize, &need_rdep); |
9be53773 | 2752 | if (!bdev) { |
dfb31b25 | 2753 | ERROR("Error copying storage"); |
9be53773 SH |
2754 | return -1; |
2755 | } | |
2756 | free(c->lxc_conf->rootfs.path); | |
2757 | c->lxc_conf->rootfs.path = strdup(bdev->src); | |
2758 | bdev_put(bdev); | |
dfb31b25 SH |
2759 | if (!c->lxc_conf->rootfs.path) { |
2760 | ERROR("Out of memory while setting storage path"); | |
9be53773 | 2761 | return -1; |
dfb31b25 | 2762 | } |
6b0d5538 SH |
2763 | // We will simply append a new lxc.rootfs entry to the unexpanded config |
2764 | clear_unexp_config_line(c->lxc_conf, "lxc.rootfs", false); | |
2765 | if (!do_append_unexp_config_line(c->lxc_conf, "lxc.rootfs", c->lxc_conf->rootfs.path)) { | |
222dc581 | 2766 | ERROR("Error saving new rootfs to cloned config"); |
d0218321 SH |
2767 | return -1; |
2768 | } | |
eee59f94 SH |
2769 | if (flags & LXC_CLONE_SNAPSHOT) |
2770 | copy_rdepends(c, c0); | |
dfb31b25 SH |
2771 | if (need_rdep) { |
2772 | if (!add_rdepends(c, c0)) | |
2773 | WARN("Error adding reverse dependency from %s to %s", | |
2774 | c->name, c0->name); | |
2775 | } | |
2776 | ||
2777 | mod_all_rdeps(c, true); | |
2778 | ||
9be53773 SH |
2779 | return 0; |
2780 | } | |
2781 | ||
1354955b SH |
2782 | struct clone_update_data { |
2783 | struct lxc_container *c0; | |
2784 | struct lxc_container *c1; | |
2785 | int flags; | |
2786 | char **hookargs; | |
2787 | }; | |
2788 | ||
2789 | static int clone_update_rootfs(struct clone_update_data *data) | |
9be53773 | 2790 | { |
1354955b SH |
2791 | struct lxc_container *c0 = data->c0; |
2792 | struct lxc_container *c = data->c1; | |
2793 | int flags = data->flags; | |
2794 | char **hookargs = data->hookargs; | |
9be53773 SH |
2795 | int ret = -1; |
2796 | char path[MAXPATHLEN]; | |
2797 | struct bdev *bdev; | |
2798 | FILE *fout; | |
148e91f5 | 2799 | struct lxc_conf *conf = c->lxc_conf; |
9be53773 SH |
2800 | |
2801 | /* update hostname in rootfs */ | |
2802 | /* we're going to mount, so run in a clean namespace to simplify cleanup */ | |
2803 | ||
1354955b SH |
2804 | if (setgid(0) < 0) { |
2805 | ERROR("Failed to setgid to 0"); | |
2806 | return -1; | |
2807 | } | |
2808 | if (setuid(0) < 0) { | |
2809 | ERROR("Failed to setuid to 0"); | |
9be53773 | 2810 | return -1; |
1354955b | 2811 | } |
c476bdce SH |
2812 | if (setgroups(0, NULL) < 0) |
2813 | WARN("Failed to clear groups"); | |
9be53773 | 2814 | |
1354955b SH |
2815 | if (unshare(CLONE_NEWNS) < 0) |
2816 | return -1; | |
76a26f55 | 2817 | bdev = bdev_init(c->lxc_conf, c->lxc_conf->rootfs.path, c->lxc_conf->rootfs.mount, NULL); |
9be53773 | 2818 | if (!bdev) |
1354955b | 2819 | return -1; |
cf3ef16d SH |
2820 | if (strcmp(bdev->type, "dir") != 0) { |
2821 | if (unshare(CLONE_NEWNS) < 0) { | |
2822 | ERROR("error unsharing mounts"); | |
e7de366c | 2823 | bdev_put(bdev); |
1354955b | 2824 | return -1; |
cf3ef16d | 2825 | } |
2c6f3fc9 SH |
2826 | if (detect_shared_rootfs()) { |
2827 | if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL)) { | |
2828 | SYSERROR("Failed to make / rslave"); | |
2829 | ERROR("Continuing..."); | |
2830 | } | |
2831 | } | |
e7de366c SG |
2832 | if (bdev->ops->mount(bdev) < 0) { |
2833 | bdev_put(bdev); | |
1354955b | 2834 | return -1; |
e7de366c | 2835 | } |
cf3ef16d | 2836 | } else { // TODO come up with a better way |
f10fad2f | 2837 | free(bdev->dest); |
cf3ef16d SH |
2838 | bdev->dest = strdup(bdev->src); |
2839 | } | |
148e91f5 SH |
2840 | |
2841 | if (!lxc_list_empty(&conf->hooks[LXCHOOK_CLONE])) { | |
2842 | /* Start of environment variable setup for hooks */ | |
ab7efcf5 | 2843 | if (c0->name && setenv("LXC_SRC_NAME", c0->name, 1)) { |
1143ed39 DE |
2844 | SYSERROR("failed to set environment variable for source container name"); |
2845 | } | |
ab7efcf5 | 2846 | if (c->name && setenv("LXC_NAME", c->name, 1)) { |
148e91f5 SH |
2847 | SYSERROR("failed to set environment variable for container name"); |
2848 | } | |
ab7efcf5 | 2849 | if (conf->rcfile && setenv("LXC_CONFIG_FILE", conf->rcfile, 1)) { |
148e91f5 SH |
2850 | SYSERROR("failed to set environment variable for config path"); |
2851 | } | |
ab7efcf5 | 2852 | if (bdev->dest && setenv("LXC_ROOTFS_MOUNT", bdev->dest, 1)) { |
148e91f5 SH |
2853 | SYSERROR("failed to set environment variable for rootfs mount"); |
2854 | } | |
ab7efcf5 | 2855 | if (conf->rootfs.path && setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1)) { |
148e91f5 SH |
2856 | SYSERROR("failed to set environment variable for rootfs mount"); |
2857 | } | |
2858 | ||
283678ed | 2859 | if (run_lxc_hooks(c->name, "clone", conf, c->get_config_path(c), hookargs)) { |
148e91f5 | 2860 | ERROR("Error executing clone hook for %s", c->name); |
e7de366c | 2861 | bdev_put(bdev); |
1354955b | 2862 | return -1; |
148e91f5 SH |
2863 | } |
2864 | } | |
2865 | ||
2866 | if (!(flags & LXC_CLONE_KEEPNAME)) { | |
2867 | ret = snprintf(path, MAXPATHLEN, "%s/etc/hostname", bdev->dest); | |
e7de366c SG |
2868 | bdev_put(bdev); |
2869 | ||
148e91f5 | 2870 | if (ret < 0 || ret >= MAXPATHLEN) |
1354955b | 2871 | return -1; |
8058be39 | 2872 | if (!file_exists(path)) |
1354955b | 2873 | return 0; |
148e91f5 | 2874 | if (!(fout = fopen(path, "w"))) { |
959aee9c | 2875 | SYSERROR("unable to open %s: ignoring", path); |
1354955b | 2876 | return 0; |
148e91f5 | 2877 | } |
a684f0b7 ÇO |
2878 | if (fprintf(fout, "%s", c->name) < 0) { |
2879 | fclose(fout); | |
1354955b | 2880 | return -1; |
6849cb5b | 2881 | } |
148e91f5 | 2882 | if (fclose(fout) < 0) |
1354955b | 2883 | return -1; |
9be53773 | 2884 | } |
e7de366c SG |
2885 | else |
2886 | bdev_put(bdev); | |
2887 | ||
1354955b SH |
2888 | return 0; |
2889 | } | |
2890 | ||
2891 | static int clone_update_rootfs_wrapper(void *data) | |
2892 | { | |
2893 | struct clone_update_data *arg = (struct clone_update_data *) data; | |
2894 | return clone_update_rootfs(arg); | |
9be53773 SH |
2895 | } |
2896 | ||
2897 | /* | |
2898 | * We want to support: | |
2899 | sudo lxc-clone -o o1 -n n1 -s -L|-fssize fssize -v|--vgname vgname \ | |
2900 | -p|--lvprefix lvprefix -t|--fstype fstype -B backingstore | |
2901 | ||
2902 | -s [ implies overlayfs] | |
2903 | -s -B overlayfs | |
2904 | -s -B aufs | |
2905 | ||
2906 | only rootfs gets converted (copied/snapshotted) on clone. | |
2907 | */ | |
2908 | ||
d5752559 | 2909 | static int create_file_dirname(char *path, struct lxc_conf *conf) |
9be53773 | 2910 | { |
c32981c3 | 2911 | char *p = strrchr(path, '/'); |
d5752559 | 2912 | int ret = -1; |
9be53773 SH |
2913 | |
2914 | if (!p) | |
2915 | return -1; | |
2916 | *p = '\0'; | |
d5752559 | 2917 | ret = do_create_container_dir(path, conf); |
9be53773 SH |
2918 | *p = '/'; |
2919 | return ret; | |
2920 | } | |
2921 | ||
858377e4 | 2922 | static struct lxc_container *do_lxcapi_clone(struct lxc_container *c, const char *newname, |
9be53773 | 2923 | const char *lxcpath, int flags, |
d659597e | 2924 | const char *bdevtype, const char *bdevdata, uint64_t newsize, |
148e91f5 | 2925 | char **hookargs) |
9be53773 SH |
2926 | { |
2927 | struct lxc_container *c2 = NULL; | |
2928 | char newpath[MAXPATHLEN]; | |
176d9acb | 2929 | int ret, storage_copied = 0; |
8d2efe40 | 2930 | char *origroot = NULL; |
1354955b | 2931 | struct clone_update_data data; |
9be53773 | 2932 | FILE *fout; |
1354955b | 2933 | pid_t pid; |
9be53773 | 2934 | |
858377e4 | 2935 | if (!c || !do_lxcapi_is_defined(c)) |
9be53773 SH |
2936 | return NULL; |
2937 | ||
5cee8c50 | 2938 | if (container_mem_lock(c)) |
9be53773 SH |
2939 | return NULL; |
2940 | ||
39dc698c | 2941 | if (!is_stopped(c)) { |
9be53773 SH |
2942 | ERROR("error: Original container (%s) is running", c->name); |
2943 | goto out; | |
2944 | } | |
2945 | ||
2946 | // Make sure the container doesn't yet exist. | |
05d53f4c SH |
2947 | if (!newname) |
2948 | newname = c->name; | |
2949 | if (!lxcpath) | |
858377e4 | 2950 | lxcpath = do_lxcapi_get_config_path(c); |
05d53f4c | 2951 | ret = snprintf(newpath, MAXPATHLEN, "%s/%s/config", lxcpath, newname); |
6849cb5b | 2952 | if (ret < 0 || ret >= MAXPATHLEN) { |
9be53773 SH |
2953 | SYSERROR("clone: failed making config pathname"); |
2954 | goto out; | |
2955 | } | |
2956 | if (file_exists(newpath)) { | |
2957 | ERROR("error: clone: %s exists", newpath); | |
2958 | goto out; | |
2959 | } | |
2960 | ||
d5752559 | 2961 | ret = create_file_dirname(newpath, c->lxc_conf); |
96532523 | 2962 | if (ret < 0 && errno != EEXIST) { |
9be53773 SH |
2963 | ERROR("Error creating container dir for %s", newpath); |
2964 | goto out; | |
2965 | } | |
2966 | ||
2967 | // copy the configuration, tweak it as needed, | |
8d2efe40 SH |
2968 | if (c->lxc_conf->rootfs.path) { |
2969 | origroot = c->lxc_conf->rootfs.path; | |
2970 | c->lxc_conf->rootfs.path = NULL; | |
2971 | } | |
9be53773 SH |
2972 | fout = fopen(newpath, "w"); |
2973 | if (!fout) { | |
2974 | SYSERROR("open %s", newpath); | |
2975 | goto out; | |
2976 | } | |
6b0d5538 | 2977 | write_config(fout, c->lxc_conf); |
9be53773 | 2978 | fclose(fout); |
8d2efe40 | 2979 | c->lxc_conf->rootfs.path = origroot; |
9be53773 | 2980 | |
05d53f4c | 2981 | sprintf(newpath, "%s/%s/rootfs", lxcpath, newname); |
9be53773 SH |
2982 | if (mkdir(newpath, 0755) < 0) { |
2983 | SYSERROR("error creating %s", newpath); | |
2984 | goto out; | |
2985 | } | |
2986 | ||
1354955b SH |
2987 | if (am_unpriv()) { |
2988 | if (chown_mapped_root(newpath, c->lxc_conf) < 0) { | |
959aee9c | 2989 | ERROR("Error chowning %s to container root", newpath); |
1354955b SH |
2990 | goto out; |
2991 | } | |
2992 | } | |
2993 | ||
05d53f4c | 2994 | c2 = lxc_container_new(newname, lxcpath); |
375c2258 | 2995 | if (!c2) { |
05d53f4c SH |
2996 | ERROR("clone: failed to create new container (%s %s)", newname, |
2997 | lxcpath); | |
9be53773 SH |
2998 | goto out; |
2999 | } | |
8d2efe40 SH |
3000 | |
3001 | // copy/snapshot rootfs's | |
3002 | ret = copy_storage(c, c2, bdevtype, flags, bdevdata, newsize); | |
3003 | if (ret < 0) | |
3004 | goto out; | |
9be53773 | 3005 | |
6b0d5538 | 3006 | |
96532523 | 3007 | // update utsname |
3d7ad474 CB |
3008 | if (!(flags & LXC_CLONE_KEEPNAME)) { |
3009 | clear_unexp_config_line(c2->lxc_conf, "lxc.utsname", false); | |
3010 | ||
3011 | if (!set_config_item_locked(c2, "lxc.utsname", newname)) { | |
3012 | ERROR("Error setting new hostname"); | |
3013 | goto out; | |
3014 | } | |
96532523 SH |
3015 | } |
3016 | ||
619256b5 ÇO |
3017 | // copy hooks |
3018 | ret = copyhooks(c, c2); | |
3019 | if (ret < 0) { | |
3020 | ERROR("error copying hooks"); | |
3021 | goto out; | |
9be53773 SH |
3022 | } |
3023 | ||
3024 | if (copy_fstab(c, c2) < 0) { | |
3025 | ERROR("error copying fstab"); | |
9be53773 SH |
3026 | goto out; |
3027 | } | |
3028 | ||
3029 | // update macaddrs | |
6b0d5538 | 3030 | if (!(flags & LXC_CLONE_KEEPMACADDR)) { |
67702c21 SH |
3031 | if (!network_new_hwaddrs(c2->lxc_conf)) { |
3032 | ERROR("Error updating mac addresses"); | |
6b0d5538 SH |
3033 | goto out; |
3034 | } | |
3035 | } | |
9be53773 | 3036 | |
176d9acb SH |
3037 | // We've now successfully created c2's storage, so clear it out if we |
3038 | // fail after this | |
3039 | storage_copied = 1; | |
3040 | ||
375c2258 | 3041 | if (!c2->save_config(c2, NULL)) |
9be53773 | 3042 | goto out; |
9be53773 | 3043 | |
1354955b SH |
3044 | if ((pid = fork()) < 0) { |
3045 | SYSERROR("fork"); | |
9be53773 | 3046 | goto out; |
1354955b SH |
3047 | } |
3048 | if (pid > 0) { | |
3049 | ret = wait_for_pid(pid); | |
3050 | if (ret) | |
3051 | goto out; | |
3052 | container_mem_unlock(c); | |
3053 | return c2; | |
3054 | } | |
3055 | data.c0 = c; | |
3056 | data.c1 = c2; | |
3057 | data.flags = flags; | |
3058 | data.hookargs = hookargs; | |
3059 | if (am_unpriv()) | |
3060 | ret = userns_exec_1(c->lxc_conf, clone_update_rootfs_wrapper, | |
3061 | &data); | |
3062 | else | |
3063 | ret = clone_update_rootfs(&data); | |
3064 | if (ret < 0) | |
3065 | exit(1); | |
9be53773 | 3066 | |
5cee8c50 | 3067 | container_mem_unlock(c); |
1354955b | 3068 | exit(0); |
9be53773 SH |
3069 | |
3070 | out: | |
5cee8c50 | 3071 | container_mem_unlock(c); |
375c2258 | 3072 | if (c2) { |
176d9acb SH |
3073 | if (!storage_copied) |
3074 | c2->lxc_conf->rootfs.path = NULL; | |
375c2258 | 3075 | c2->destroy(c2); |
9be53773 | 3076 | lxc_container_put(c2); |
375c2258 | 3077 | } |
9be53773 SH |
3078 | |
3079 | return NULL; | |
3080 | } | |
3081 | ||
858377e4 SH |
3082 | static struct lxc_container *lxcapi_clone(struct lxc_container *c, const char *newname, |
3083 | const char *lxcpath, int flags, | |
3084 | const char *bdevtype, const char *bdevdata, uint64_t newsize, | |
3085 | char **hookargs) | |
3086 | { | |
3087 | struct lxc_container * ret; | |
3088 | current_config = c ? c->lxc_conf : NULL; | |
3089 | ret = do_lxcapi_clone(c, newname, lxcpath, flags, bdevtype, bdevdata, newsize, hookargs); | |
3090 | current_config = NULL; | |
3091 | return ret; | |
3092 | } | |
3093 | ||
3094 | static bool do_lxcapi_rename(struct lxc_container *c, const char *newname) | |
06e5650e ÇO |
3095 | { |
3096 | struct bdev *bdev; | |
3097 | struct lxc_container *newc; | |
06e5650e | 3098 | |
d693cf93 | 3099 | if (!c || !c->name || !c->config_path || !c->lxc_conf) |
06e5650e ÇO |
3100 | return false; |
3101 | ||
18aa217b SH |
3102 | if (has_fs_snapshots(c) || has_snapshots(c)) { |
3103 | ERROR("Renaming a container with snapshots is not supported"); | |
3104 | return false; | |
3105 | } | |
76a26f55 | 3106 | bdev = bdev_init(c->lxc_conf, c->lxc_conf->rootfs.path, c->lxc_conf->rootfs.mount, NULL); |
06e5650e ÇO |
3107 | if (!bdev) { |
3108 | ERROR("Failed to find original backing store type"); | |
3109 | return false; | |
3110 | } | |
3111 | ||
619256b5 | 3112 | newc = lxcapi_clone(c, newname, c->config_path, LXC_CLONE_KEEPMACADDR, NULL, bdev->type, 0, NULL); |
06e5650e ÇO |
3113 | bdev_put(bdev); |
3114 | if (!newc) { | |
3115 | lxc_container_put(newc); | |
3116 | return false; | |
3117 | } | |
3118 | ||
3119 | if (newc && lxcapi_is_defined(newc)) | |
3120 | lxc_container_put(newc); | |
3121 | ||
18aa217b | 3122 | if (!container_destroy(c)) { |
06e5650e ÇO |
3123 | ERROR("Could not destroy existing container %s", c->name); |
3124 | return false; | |
3125 | } | |
3126 | return true; | |
3127 | } | |
3128 | ||
858377e4 SH |
3129 | WRAP_API_1(bool, lxcapi_rename, const char *) |
3130 | ||
a0e93eeb CS |
3131 | 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) |
3132 | { | |
858377e4 SH |
3133 | int ret; |
3134 | ||
a0e93eeb CS |
3135 | if (!c) |
3136 | return -1; | |
3137 | ||
858377e4 SH |
3138 | current_config = c->lxc_conf; |
3139 | ||
3140 | ret = lxc_attach(c->name, c->config_path, exec_function, exec_payload, options, attached_process); | |
3141 | current_config = NULL; | |
3142 | return ret; | |
a0e93eeb CS |
3143 | } |
3144 | ||
858377e4 | 3145 | 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 |
3146 | { |
3147 | lxc_attach_command_t command; | |
3148 | pid_t pid; | |
3149 | int r; | |
3150 | ||
3151 | if (!c) | |
3152 | return -1; | |
3153 | ||
3154 | command.program = (char*)program; | |
3155 | command.argv = (char**)argv; | |
3156 | r = lxc_attach(c->name, c->config_path, lxc_attach_run_command, &command, options, &pid); | |
3157 | if (r < 0) { | |
3158 | ERROR("ups"); | |
3159 | return r; | |
3160 | } | |
3161 | return lxc_wait_for_pid_status(pid); | |
3162 | } | |
3163 | ||
858377e4 SH |
3164 | static int lxcapi_attach_run_wait(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char * const argv[]) |
3165 | { | |
3166 | int ret; | |
3167 | current_config = c ? c->lxc_conf : NULL; | |
3168 | ret = do_lxcapi_attach_run_wait(c, options, program, argv); | |
3169 | current_config = NULL; | |
3170 | return ret; | |
3171 | } | |
3172 | ||
74a3920a | 3173 | static int get_next_index(const char *lxcpath, char *cname) |
f5dd1d53 SH |
3174 | { |
3175 | char *fname; | |
3176 | struct stat sb; | |
3177 | int i = 0, ret; | |
3178 | ||
3179 | fname = alloca(strlen(lxcpath) + 20); | |
3180 | while (1) { | |
3181 | sprintf(fname, "%s/snap%d", lxcpath, i); | |
3182 | ret = stat(fname, &sb); | |
3183 | if (ret != 0) | |
3184 | return i; | |
3185 | i++; | |
3186 | } | |
3187 | } | |
3188 | ||
18aa217b SH |
3189 | static bool get_snappath_dir(struct lxc_container *c, char *snappath) |
3190 | { | |
3191 | int ret; | |
3192 | /* | |
3193 | * If the old style snapshot path exists, use it | |
3194 | * /var/lib/lxc -> /var/lib/lxcsnaps | |
3195 | */ | |
3196 | ret = snprintf(snappath, MAXPATHLEN, "%ssnaps", c->config_path); | |
3197 | if (ret < 0 || ret >= MAXPATHLEN) | |
3198 | return false; | |
3199 | if (dir_exists(snappath)) { | |
3200 | ret = snprintf(snappath, MAXPATHLEN, "%ssnaps/%s", c->config_path, c->name); | |
3201 | if (ret < 0 || ret >= MAXPATHLEN) | |
3202 | return false; | |
3203 | return true; | |
3204 | } | |
3205 | ||
3206 | /* | |
3207 | * Use the new style path | |
3208 | * /var/lib/lxc -> /var/lib/lxc + c->name + /snaps + \0 | |
3209 | */ | |
3210 | ret = snprintf(snappath, MAXPATHLEN, "%s/%s/snaps", c->config_path, c->name); | |
3211 | if (ret < 0 || ret >= MAXPATHLEN) | |
3212 | return false; | |
3213 | return true; | |
3214 | } | |
3215 | ||
858377e4 | 3216 | static int do_lxcapi_snapshot(struct lxc_container *c, const char *commentfile) |
f5dd1d53 SH |
3217 | { |
3218 | int i, flags, ret; | |
3219 | struct lxc_container *c2; | |
3220 | char snappath[MAXPATHLEN], newname[20]; | |
3221 | ||
840f05df SH |
3222 | if (!c || !lxcapi_is_defined(c)) |
3223 | return -1; | |
3224 | ||
cdd01be2 SH |
3225 | if (!bdev_can_backup(c->lxc_conf)) { |
3226 | ERROR("%s's backing store cannot be backed up.", c->name); | |
3227 | ERROR("Your container must use another backing store type."); | |
3228 | return -1; | |
3229 | } | |
3230 | ||
18aa217b | 3231 | if (!get_snappath_dir(c, snappath)) |
f5dd1d53 | 3232 | return -1; |
18aa217b | 3233 | |
f5dd1d53 SH |
3234 | i = get_next_index(snappath, c->name); |
3235 | ||
3236 | if (mkdir_p(snappath, 0755) < 0) { | |
3237 | ERROR("Failed to create snapshot directory %s", snappath); | |
3238 | return -1; | |
3239 | } | |
3240 | ||
3241 | ret = snprintf(newname, 20, "snap%d", i); | |
3242 | if (ret < 0 || ret >= 20) | |
3243 | return -1; | |
3244 | ||
0a83cbbb SH |
3245 | /* |
3246 | * We pass LXC_CLONE_SNAPSHOT to make sure that a rdepends file entry is | |
3247 | * created in the original container | |
3248 | */ | |
3249 | flags = LXC_CLONE_SNAPSHOT | LXC_CLONE_KEEPMACADDR | LXC_CLONE_KEEPNAME | | |
3250 | LXC_CLONE_KEEPBDEVTYPE | LXC_CLONE_MAYBE_SNAPSHOT; | |
76a26f55 | 3251 | if (bdev_is_dir(c->lxc_conf, c->lxc_conf->rootfs.path)) { |
8c39f7a4 SH |
3252 | ERROR("Snapshot of directory-backed container requested."); |
3253 | ERROR("Making a copy-clone. If you do want snapshots, then"); | |
1f92162d | 3254 | ERROR("please create an aufs or overlayfs clone first, snapshot that"); |
8c39f7a4 SH |
3255 | ERROR("and keep the original container pristine."); |
3256 | flags &= ~LXC_CLONE_SNAPSHOT | LXC_CLONE_MAYBE_SNAPSHOT; | |
3257 | } | |
858377e4 | 3258 | c2 = do_lxcapi_clone(c, newname, snappath, flags, NULL, NULL, 0, NULL); |
f5dd1d53 | 3259 | if (!c2) { |
959aee9c | 3260 | ERROR("clone of %s:%s failed", c->config_path, c->name); |
f5dd1d53 SH |
3261 | return -1; |
3262 | } | |
3263 | ||
3264 | lxc_container_put(c2); | |
3265 | ||
3266 | // Now write down the creation time | |
3267 | time_t timer; | |
3268 | char buffer[25]; | |
3269 | struct tm* tm_info; | |
025ed0f3 | 3270 | FILE *f; |
f5dd1d53 SH |
3271 | |
3272 | time(&timer); | |
3273 | tm_info = localtime(&timer); | |
3274 | ||
3275 | strftime(buffer, 25, "%Y:%m:%d %H:%M:%S", tm_info); | |
3276 | ||
3277 | char *dfnam = alloca(strlen(snappath) + strlen(newname) + 5); | |
3278 | sprintf(dfnam, "%s/%s/ts", snappath, newname); | |
025ed0f3 | 3279 | f = fopen(dfnam, "w"); |
f5dd1d53 | 3280 | if (!f) { |
959aee9c | 3281 | ERROR("Failed to open %s", dfnam); |
f5dd1d53 SH |
3282 | return -1; |
3283 | } | |
3284 | if (fprintf(f, "%s", buffer) < 0) { | |
3285 | SYSERROR("Writing timestamp"); | |
3286 | fclose(f); | |
3287 | return -1; | |
3288 | } | |
025ed0f3 | 3289 | ret = fclose(f); |
025ed0f3 | 3290 | if (ret != 0) { |
f5dd1d53 SH |
3291 | SYSERROR("Writing timestamp"); |
3292 | return -1; | |
3293 | } | |
3294 | ||
3295 | if (commentfile) { | |
3296 | // $p / $name / comment \0 | |
3297 | int len = strlen(snappath) + strlen(newname) + 10; | |
3298 | char *path = alloca(len); | |
3299 | sprintf(path, "%s/%s/comment", snappath, newname); | |
3300 | return copy_file(commentfile, path) < 0 ? -1 : i; | |
3301 | } | |
3302 | ||
3303 | return i; | |
3304 | } | |
3305 | ||
858377e4 SH |
3306 | WRAP_API_1(int, lxcapi_snapshot, const char *) |
3307 | ||
f5dd1d53 SH |
3308 | static void lxcsnap_free(struct lxc_snapshot *s) |
3309 | { | |
f10fad2f ME |
3310 | free(s->name); |
3311 | free(s->comment_pathname); | |
3312 | free(s->timestamp); | |
3313 | free(s->lxcpath); | |
f5dd1d53 SH |
3314 | } |
3315 | ||
3316 | static char *get_snapcomment_path(char* snappath, char *name) | |
3317 | { | |
3318 | // $snappath/$name/comment | |
3319 | int ret, len = strlen(snappath) + strlen(name) + 10; | |
3320 | char *s = malloc(len); | |
3321 | ||
3322 | if (s) { | |
3323 | ret = snprintf(s, len, "%s/%s/comment", snappath, name); | |
3324 | if (ret < 0 || ret >= len) { | |
3325 | free(s); | |
3326 | s = NULL; | |
3327 | } | |
3328 | } | |
3329 | return s; | |
3330 | } | |
3331 | ||
3332 | static char *get_timestamp(char* snappath, char *name) | |
3333 | { | |
3334 | char path[MAXPATHLEN], *s = NULL; | |
3335 | int ret, len; | |
3336 | FILE *fin; | |
3337 | ||
3338 | ret = snprintf(path, MAXPATHLEN, "%s/%s/ts", snappath, name); | |
3339 | if (ret < 0 || ret >= MAXPATHLEN) | |
3340 | return NULL; | |
025ed0f3 | 3341 | fin = fopen(path, "r"); |
025ed0f3 | 3342 | if (!fin) |
f5dd1d53 SH |
3343 | return NULL; |
3344 | (void) fseek(fin, 0, SEEK_END); | |
3345 | len = ftell(fin); | |
3346 | (void) fseek(fin, 0, SEEK_SET); | |
3347 | if (len > 0) { | |
3348 | s = malloc(len+1); | |
3349 | if (s) { | |
3350 | s[len] = '\0'; | |
3351 | if (fread(s, 1, len, fin) != len) { | |
3352 | SYSERROR("reading timestamp"); | |
3353 | free(s); | |
3354 | s = NULL; | |
3355 | } | |
3356 | } | |
3357 | } | |
3358 | fclose(fin); | |
3359 | return s; | |
3360 | } | |
3361 | ||
858377e4 | 3362 | static int do_lxcapi_snapshot_list(struct lxc_container *c, struct lxc_snapshot **ret_snaps) |
f5dd1d53 SH |
3363 | { |
3364 | char snappath[MAXPATHLEN], path2[MAXPATHLEN]; | |
18aa217b | 3365 | int count = 0, ret; |
f5dd1d53 SH |
3366 | struct dirent dirent, *direntp; |
3367 | struct lxc_snapshot *snaps =NULL, *nsnaps; | |
3368 | DIR *dir; | |
3369 | ||
3370 | if (!c || !lxcapi_is_defined(c)) | |
3371 | return -1; | |
c868b261 | 3372 | |
18aa217b | 3373 | if (!get_snappath_dir(c, snappath)) { |
f5dd1d53 SH |
3374 | ERROR("path name too long"); |
3375 | return -1; | |
3376 | } | |
025ed0f3 | 3377 | dir = opendir(snappath); |
025ed0f3 | 3378 | if (!dir) { |
f5dd1d53 SH |
3379 | INFO("failed to open %s - assuming no snapshots", snappath); |
3380 | return 0; | |
3381 | } | |
3382 | ||
3383 | while (!readdir_r(dir, &dirent, &direntp)) { | |
3384 | if (!direntp) | |
3385 | break; | |
3386 | ||
3387 | if (!strcmp(direntp->d_name, ".")) | |
3388 | continue; | |
3389 | ||
3390 | if (!strcmp(direntp->d_name, "..")) | |
3391 | continue; | |
3392 | ||
3393 | ret = snprintf(path2, MAXPATHLEN, "%s/%s/config", snappath, direntp->d_name); | |
3394 | if (ret < 0 || ret >= MAXPATHLEN) { | |
3395 | ERROR("pathname too long"); | |
3396 | goto out_free; | |
3397 | } | |
3398 | if (!file_exists(path2)) | |
3399 | continue; | |
3400 | nsnaps = realloc(snaps, (count + 1)*sizeof(*snaps)); | |
3401 | if (!nsnaps) { | |
3402 | SYSERROR("Out of memory"); | |
3403 | goto out_free; | |
3404 | } | |
3405 | snaps = nsnaps; | |
3406 | snaps[count].free = lxcsnap_free; | |
3407 | snaps[count].name = strdup(direntp->d_name); | |
3408 | if (!snaps[count].name) | |
3409 | goto out_free; | |
3410 | snaps[count].lxcpath = strdup(snappath); | |
3411 | if (!snaps[count].lxcpath) { | |
3412 | free(snaps[count].name); | |
3413 | goto out_free; | |
3414 | } | |
3415 | snaps[count].comment_pathname = get_snapcomment_path(snappath, direntp->d_name); | |
3416 | snaps[count].timestamp = get_timestamp(snappath, direntp->d_name); | |
3417 | count++; | |
3418 | } | |
3419 | ||
3420 | if (closedir(dir)) | |
3421 | WARN("failed to close directory"); | |
3422 | ||
3423 | *ret_snaps = snaps; | |
3424 | return count; | |
3425 | ||
3426 | out_free: | |
3427 | if (snaps) { | |
3428 | int i; | |
3429 | for (i=0; i<count; i++) | |
3430 | lxcsnap_free(&snaps[i]); | |
3431 | free(snaps); | |
3432 | } | |
9baa57bd SH |
3433 | if (closedir(dir)) |
3434 | WARN("failed to close directory"); | |
f5dd1d53 SH |
3435 | return -1; |
3436 | } | |
3437 | ||
858377e4 SH |
3438 | WRAP_API_1(int, lxcapi_snapshot_list, struct lxc_snapshot **) |
3439 | ||
3440 | static bool do_lxcapi_snapshot_restore(struct lxc_container *c, const char *snapname, const char *newname) | |
f5dd1d53 SH |
3441 | { |
3442 | char clonelxcpath[MAXPATHLEN]; | |
18aa217b | 3443 | int flags = 0; |
f5dd1d53 SH |
3444 | struct lxc_container *snap, *rest; |
3445 | struct bdev *bdev; | |
3446 | bool b = false; | |
3447 | ||
3448 | if (!c || !c->name || !c->config_path) | |
3449 | return false; | |
3450 | ||
18aa217b SH |
3451 | if (has_fs_snapshots(c)) { |
3452 | ERROR("container rootfs has dependent snapshots"); | |
3453 | return false; | |
3454 | } | |
3455 | ||
76a26f55 | 3456 | bdev = bdev_init(c->lxc_conf, c->lxc_conf->rootfs.path, c->lxc_conf->rootfs.mount, NULL); |
f5dd1d53 SH |
3457 | if (!bdev) { |
3458 | ERROR("Failed to find original backing store type"); | |
3459 | return false; | |
3460 | } | |
3461 | ||
3462 | if (!newname) | |
3463 | newname = c->name; | |
7e36f87e | 3464 | |
18aa217b | 3465 | if (!get_snappath_dir(c, clonelxcpath)) { |
f5dd1d53 SH |
3466 | bdev_put(bdev); |
3467 | return false; | |
3468 | } | |
3469 | // how should we lock this? | |
3470 | ||
3471 | snap = lxc_container_new(snapname, clonelxcpath); | |
3472 | if (!snap || !lxcapi_is_defined(snap)) { | |
3473 | ERROR("Could not open snapshot %s", snapname); | |
3474 | if (snap) lxc_container_put(snap); | |
3475 | bdev_put(bdev); | |
3476 | return false; | |
3477 | } | |
3478 | ||
7e36f87e | 3479 | if (strcmp(c->name, newname) == 0) { |
18aa217b | 3480 | if (!container_destroy(c)) { |
7e36f87e ÇO |
3481 | ERROR("Could not destroy existing container %s", newname); |
3482 | lxc_container_put(snap); | |
3483 | bdev_put(bdev); | |
3484 | return false; | |
3485 | } | |
3486 | } | |
3487 | ||
de269ee8 SH |
3488 | if (strcmp(bdev->type, "dir") != 0 && strcmp(bdev->type, "loop") != 0) |
3489 | flags = LXC_CLONE_SNAPSHOT | LXC_CLONE_MAYBE_SNAPSHOT; | |
3490 | rest = lxcapi_clone(snap, newname, c->config_path, flags, | |
3491 | bdev->type, NULL, 0, NULL); | |
f5dd1d53 SH |
3492 | bdev_put(bdev); |
3493 | if (rest && lxcapi_is_defined(rest)) | |
3494 | b = true; | |
3495 | if (rest) | |
3496 | lxc_container_put(rest); | |
3497 | lxc_container_put(snap); | |
3498 | return b; | |
3499 | } | |
3500 | ||
858377e4 SH |
3501 | WRAP_API_2(bool, lxcapi_snapshot_restore, const char *, const char *) |
3502 | ||
18aa217b | 3503 | static bool do_snapshot_destroy(const char *snapname, const char *clonelxcpath) |
771d96b3 | 3504 | { |
771d96b3 | 3505 | struct lxc_container *snap = NULL; |
18aa217b | 3506 | bool bret = false; |
771d96b3 ÇO |
3507 | |
3508 | snap = lxc_container_new(snapname, clonelxcpath); | |
18aa217b | 3509 | if (!snap) { |
771d96b3 ÇO |
3510 | ERROR("Could not find snapshot %s", snapname); |
3511 | goto err; | |
3512 | } | |
3513 | ||
858377e4 | 3514 | if (!do_lxcapi_destroy(snap)) { |
771d96b3 ÇO |
3515 | ERROR("Could not destroy snapshot %s", snapname); |
3516 | goto err; | |
3517 | } | |
18aa217b | 3518 | bret = true; |
771d96b3 | 3519 | |
771d96b3 ÇO |
3520 | err: |
3521 | if (snap) | |
3522 | lxc_container_put(snap); | |
18aa217b SH |
3523 | return bret; |
3524 | } | |
3525 | ||
3526 | static bool remove_all_snapshots(const char *path) | |
3527 | { | |
3528 | DIR *dir; | |
3529 | struct dirent dirent, *direntp; | |
3530 | bool bret = true; | |
3531 | ||
3532 | dir = opendir(path); | |
3533 | if (!dir) { | |
3534 | SYSERROR("opendir on snapshot path %s", path); | |
3535 | return false; | |
3536 | } | |
3537 | while (!readdir_r(dir, &dirent, &direntp)) { | |
3538 | if (!direntp) | |
3539 | break; | |
3540 | if (!strcmp(direntp->d_name, ".")) | |
3541 | continue; | |
3542 | if (!strcmp(direntp->d_name, "..")) | |
3543 | continue; | |
3544 | if (!do_snapshot_destroy(direntp->d_name, path)) { | |
3545 | bret = false; | |
3546 | continue; | |
3547 | } | |
3548 | } | |
3549 | ||
3550 | closedir(dir); | |
3551 | ||
3552 | if (rmdir(path)) | |
3553 | SYSERROR("Error removing directory %s", path); | |
3554 | ||
3555 | return bret; | |
3556 | } | |
3557 | ||
858377e4 | 3558 | static bool do_lxcapi_snapshot_destroy(struct lxc_container *c, const char *snapname) |
18aa217b SH |
3559 | { |
3560 | char clonelxcpath[MAXPATHLEN]; | |
3561 | ||
3562 | if (!c || !c->name || !c->config_path || !snapname) | |
3563 | return false; | |
3564 | ||
3565 | if (!get_snappath_dir(c, clonelxcpath)) | |
3566 | return false; | |
3567 | ||
3568 | return do_snapshot_destroy(snapname, clonelxcpath); | |
3569 | } | |
3570 | ||
858377e4 SH |
3571 | WRAP_API_1(bool, lxcapi_snapshot_destroy, const char *) |
3572 | ||
3573 | static bool do_lxcapi_snapshot_destroy_all(struct lxc_container *c) | |
18aa217b SH |
3574 | { |
3575 | char clonelxcpath[MAXPATHLEN]; | |
3576 | ||
3577 | if (!c || !c->name || !c->config_path) | |
3578 | return false; | |
3579 | ||
3580 | if (!get_snappath_dir(c, clonelxcpath)) | |
3581 | return false; | |
3582 | ||
3583 | return remove_all_snapshots(clonelxcpath); | |
771d96b3 ÇO |
3584 | } |
3585 | ||
858377e4 SH |
3586 | WRAP_API(bool, lxcapi_snapshot_destroy_all) |
3587 | ||
3588 | static bool do_lxcapi_may_control(struct lxc_container *c) | |
b494d2dd SH |
3589 | { |
3590 | return lxc_try_cmd(c->name, c->config_path) == 0; | |
3591 | } | |
3592 | ||
858377e4 SH |
3593 | WRAP_API(bool, lxcapi_may_control) |
3594 | ||
d5aa23e6 SH |
3595 | static bool do_add_remove_node(pid_t init_pid, const char *path, bool add, |
3596 | struct stat *st) | |
3597 | { | |
3598 | char chrootpath[MAXPATHLEN]; | |
3599 | char *directory_path = NULL; | |
3600 | pid_t pid; | |
3601 | int ret; | |
3602 | ||
3603 | if ((pid = fork()) < 0) { | |
3604 | SYSERROR("failed to fork a child helper"); | |
3605 | return false; | |
3606 | } | |
3607 | if (pid) { | |
3608 | if (wait_for_pid(pid) != 0) { | |
3609 | ERROR("Failed to create note in guest"); | |
3610 | return false; | |
3611 | } | |
3612 | return true; | |
3613 | } | |
3614 | ||
3615 | /* prepare the path */ | |
3616 | ret = snprintf(chrootpath, MAXPATHLEN, "/proc/%d/root", init_pid); | |
3617 | if (ret < 0 || ret >= MAXPATHLEN) | |
3618 | return false; | |
3619 | ||
6b9324bd | 3620 | if (chroot(chrootpath) < 0) |
d5aa23e6 | 3621 | exit(1); |
6b9324bd | 3622 | if (chdir("/") < 0) |
d5aa23e6 SH |
3623 | exit(1); |
3624 | /* remove path if it exists */ | |
3625 | if(faccessat(AT_FDCWD, path, F_OK, AT_SYMLINK_NOFOLLOW) == 0) { | |
3626 | if (unlink(path) < 0) { | |
3627 | ERROR("unlink failed"); | |
3628 | exit(1); | |
3629 | } | |
3630 | } | |
3631 | if (!add) | |
3632 | exit(0); | |
3633 | ||
3634 | /* create any missing directories */ | |
3635 | directory_path = dirname(strdup(path)); | |
3636 | if (mkdir_p(directory_path, 0755) < 0 && errno != EEXIST) { | |
3637 | ERROR("failed to create directory"); | |
3638 | exit(1); | |
3639 | } | |
3640 | ||
3641 | /* create the device node */ | |
3642 | if (mknod(path, st->st_mode, st->st_rdev) < 0) { | |
3643 | ERROR("mknod failed"); | |
3644 | exit(1); | |
3645 | } | |
3646 | ||
3647 | exit(0); | |
3648 | } | |
3649 | ||
f0ca2726 | 3650 | static bool add_remove_device_node(struct lxc_container *c, const char *src_path, const char *dest_path, bool add) |
a9a0ed90 ÇO |
3651 | { |
3652 | int ret; | |
3653 | struct stat st; | |
a9a0ed90 | 3654 | char value[MAX_BUFFER]; |
f0ca2726 | 3655 | const char *p; |
a9a0ed90 ÇO |
3656 | |
3657 | /* make sure container is running */ | |
858377e4 | 3658 | if (!do_lxcapi_is_running(c)) { |
a9a0ed90 | 3659 | ERROR("container is not running"); |
d5aa23e6 | 3660 | return false; |
a9a0ed90 ÇO |
3661 | } |
3662 | ||
3663 | /* use src_path if dest_path is NULL otherwise use dest_path */ | |
3664 | p = dest_path ? dest_path : src_path; | |
3665 | ||
a9a0ed90 ÇO |
3666 | /* make sure we can access p */ |
3667 | if(access(p, F_OK) < 0 || stat(p, &st) < 0) | |
d5aa23e6 | 3668 | return false; |
a9a0ed90 ÇO |
3669 | |
3670 | /* continue if path is character device or block device */ | |
c6a9b0d7 | 3671 | if (S_ISCHR(st.st_mode)) |
a9a0ed90 | 3672 | ret = snprintf(value, MAX_BUFFER, "c %d:%d rwm", major(st.st_rdev), minor(st.st_rdev)); |
c6a9b0d7 | 3673 | else if (S_ISBLK(st.st_mode)) |
a9a0ed90 ÇO |
3674 | ret = snprintf(value, MAX_BUFFER, "b %d:%d rwm", major(st.st_rdev), minor(st.st_rdev)); |
3675 | else | |
d5aa23e6 | 3676 | return false; |
a9a0ed90 ÇO |
3677 | |
3678 | /* check snprintf return code */ | |
3679 | if (ret < 0 || ret >= MAX_BUFFER) | |
d5aa23e6 | 3680 | return false; |
a9a0ed90 | 3681 | |
858377e4 | 3682 | if (!do_add_remove_node(do_lxcapi_init_pid(c), p, add, &st)) |
d5aa23e6 | 3683 | return false; |
a9a0ed90 | 3684 | |
d5aa23e6 | 3685 | /* add or remove device to/from cgroup access list */ |
a9a0ed90 | 3686 | if (add) { |
858377e4 | 3687 | if (!do_lxcapi_set_cgroup_item(c, "devices.allow", value)) { |
a9a0ed90 | 3688 | ERROR("set_cgroup_item failed while adding the device node"); |
d5aa23e6 | 3689 | return false; |
a9a0ed90 ÇO |
3690 | } |
3691 | } else { | |
858377e4 | 3692 | if (!do_lxcapi_set_cgroup_item(c, "devices.deny", value)) { |
a9a0ed90 | 3693 | ERROR("set_cgroup_item failed while removing the device node"); |
d5aa23e6 | 3694 | return false; |
a9a0ed90 ÇO |
3695 | } |
3696 | } | |
d5aa23e6 | 3697 | |
a9a0ed90 | 3698 | return true; |
a9a0ed90 ÇO |
3699 | } |
3700 | ||
858377e4 | 3701 | static bool do_lxcapi_add_device_node(struct lxc_container *c, const char *src_path, const char *dest_path) |
a9a0ed90 | 3702 | { |
c868b261 ÇO |
3703 | if (am_unpriv()) { |
3704 | ERROR(NOT_SUPPORTED_ERROR, __FUNCTION__); | |
3705 | return false; | |
3706 | } | |
a9a0ed90 ÇO |
3707 | return add_remove_device_node(c, src_path, dest_path, true); |
3708 | } | |
3709 | ||
858377e4 SH |
3710 | WRAP_API_2(bool, lxcapi_add_device_node, const char *, const char *) |
3711 | ||
3712 | static bool do_lxcapi_remove_device_node(struct lxc_container *c, const char *src_path, const char *dest_path) | |
a9a0ed90 | 3713 | { |
c868b261 ÇO |
3714 | if (am_unpriv()) { |
3715 | ERROR(NOT_SUPPORTED_ERROR, __FUNCTION__); | |
3716 | return false; | |
3717 | } | |
a9a0ed90 ÇO |
3718 | return add_remove_device_node(c, src_path, dest_path, false); |
3719 | } | |
3720 | ||
858377e4 SH |
3721 | WRAP_API_2(bool, lxcapi_remove_device_node, const char *, const char *) |
3722 | ||
3723 | static bool do_lxcapi_attach_interface(struct lxc_container *c, const char *ifname, | |
e58fae8f DY |
3724 | const char *dst_ifname) |
3725 | { | |
3726 | int ret = 0; | |
3727 | if (am_unpriv()) { | |
3728 | ERROR(NOT_SUPPORTED_ERROR, __FUNCTION__); | |
3729 | return false; | |
3730 | } | |
3731 | ||
3732 | if (!ifname) { | |
3733 | ERROR("No source interface name given"); | |
3734 | return false; | |
3735 | } | |
3736 | ||
3737 | ret = lxc_netdev_isup(ifname); | |
e58fae8f | 3738 | |
e5848d39 SH |
3739 | if (ret > 0) { |
3740 | /* netdev of ifname is up. */ | |
e58fae8f DY |
3741 | ret = lxc_netdev_down(ifname); |
3742 | if (ret) | |
3743 | goto err; | |
3744 | } | |
3745 | ||
858377e4 | 3746 | ret = lxc_netdev_move_by_name(ifname, do_lxcapi_init_pid(c), dst_ifname); |
e58fae8f DY |
3747 | if (ret) |
3748 | goto err; | |
3749 | ||
3750 | return true; | |
e5848d39 | 3751 | |
e58fae8f | 3752 | err: |
e58fae8f DY |
3753 | return false; |
3754 | } | |
3755 | ||
858377e4 SH |
3756 | WRAP_API_2(bool, lxcapi_attach_interface, const char *, const char *) |
3757 | ||
3758 | static bool do_lxcapi_detach_interface(struct lxc_container *c, const char *ifname, | |
e58fae8f DY |
3759 | const char *dst_ifname) |
3760 | { | |
3761 | pid_t pid, pid_outside; | |
3762 | ||
3763 | if (am_unpriv()) { | |
3764 | ERROR(NOT_SUPPORTED_ERROR, __FUNCTION__); | |
3765 | return false; | |
3766 | } | |
3767 | ||
3768 | if (!ifname) { | |
3769 | ERROR("No source interface name given"); | |
3770 | return false; | |
3771 | } | |
3772 | ||
3773 | pid_outside = getpid(); | |
3774 | pid = fork(); | |
3775 | if (pid < 0) { | |
3776 | ERROR("failed to fork task to get interfaces information"); | |
3777 | return false; | |
3778 | } | |
3779 | ||
3780 | if (pid == 0) { // child | |
3781 | int ret = 0; | |
e0f59189 | 3782 | if (!enter_net_ns(c)) { |
e58fae8f DY |
3783 | ERROR("failed to enter namespace"); |
3784 | exit(-1); | |
3785 | } | |
3786 | ||
3787 | ret = lxc_netdev_isup(ifname); | |
3788 | if (ret < 0) | |
3789 | exit(ret); | |
3790 | ||
3791 | /* netdev of ifname is up. */ | |
3792 | if (ret) { | |
3793 | ret = lxc_netdev_down(ifname); | |
3794 | if (ret) | |
3795 | exit(ret); | |
3796 | } | |
3797 | ||
3798 | ret = lxc_netdev_move_by_name(ifname, pid_outside, dst_ifname); | |
3799 | ||
3800 | /* -EINVAL means there is no netdev named as ifanme. */ | |
3801 | if (ret == -EINVAL) { | |
3802 | ERROR("No network device named as %s.", ifname); | |
3803 | } | |
3804 | exit(ret); | |
3805 | } | |
3806 | ||
3807 | if (wait_for_pid(pid) != 0) | |
3808 | return false; | |
3809 | ||
3810 | return true; | |
3811 | } | |
3812 | ||
858377e4 SH |
3813 | WRAP_API_2(bool, lxcapi_detach_interface, const char *, const char *) |
3814 | ||
3815 | static bool do_lxcapi_checkpoint(struct lxc_container *c, char *directory, bool stop, bool verbose) | |
bbd4e13e TA |
3816 | { |
3817 | pid_t pid; | |
3818 | int status; | |
85c50991 | 3819 | char path[PATH_MAX]; |
bbd4e13e TA |
3820 | |
3821 | if (!criu_ok(c)) | |
3822 | return false; | |
3823 | ||
3824 | if (mkdir(directory, 0700) < 0 && errno != EEXIST) | |
3825 | return false; | |
3826 | ||
85c50991 TA |
3827 | status = snprintf(path, sizeof(path), "%s/inventory.img", directory); |
3828 | if (status < 0 || status >= sizeof(path)) | |
3829 | return false; | |
3830 | ||
3831 | if (access(path, F_OK) == 0) { | |
3832 | ERROR("please use a fresh directory for the dump directory\n"); | |
3833 | return false; | |
3834 | } | |
3835 | ||
735f2c6e TA |
3836 | pid = fork(); |
3837 | if (pid < 0) | |
3838 | return false; | |
3839 | ||
3840 | if (pid == 0) { | |
3841 | struct criu_opts os; | |
3842 | ||
3843 | os.action = "dump"; | |
3844 | os.directory = directory; | |
3845 | os.c = c; | |
3846 | os.stop = stop; | |
3847 | os.verbose = verbose; | |
3848 | ||
3849 | /* exec_criu() returning is an error */ | |
3850 | exec_criu(&os); | |
3851 | exit(1); | |
3852 | } else { | |
3853 | pid_t w = waitpid(pid, &status, 0); | |
3854 | if (w == -1) { | |
3fdf4a73 | 3855 | SYSERROR("waitpid"); |
735f2c6e TA |
3856 | return false; |
3857 | } | |
3858 | ||
3859 | if (WIFEXITED(status)) { | |
3860 | return !WEXITSTATUS(status); | |
3861 | } | |
3862 | ||
3863 | return false; | |
3864 | } | |
3865 | } | |
3866 | ||
858377e4 SH |
3867 | WRAP_API_3(bool, lxcapi_checkpoint, char *, bool, bool) |
3868 | ||
3869 | static bool do_lxcapi_restore(struct lxc_container *c, char *directory, bool verbose) | |
c9d8f2ee TA |
3870 | { |
3871 | pid_t pid; | |
3872 | int status, nread; | |
3873 | int pipefd[2]; | |
3874 | ||
3875 | if (!criu_ok(c)) | |
3876 | return false; | |
3877 | ||
3878 | if (geteuid()) { | |
3879 | ERROR("Must be root to restore\n"); | |
3880 | return false; | |
3881 | } | |
3882 | ||
3883 | if (pipe(pipefd)) { | |
3884 | ERROR("failed to create pipe"); | |
3885 | return false; | |
3886 | } | |
3887 | ||
3888 | pid = fork(); | |
3889 | if (pid < 0) { | |
3890 | close(pipefd[0]); | |
3891 | close(pipefd[1]); | |
3892 | return false; | |
3893 | } | |
3894 | ||
3895 | if (pid == 0) { | |
3896 | close(pipefd[0]); | |
3897 | // this never returns | |
3898 | do_restore(c, pipefd[1], directory, verbose); | |
3899 | } | |
3900 | ||
3901 | close(pipefd[1]); | |
3902 | ||
3903 | nread = read(pipefd[0], &status, sizeof(status)); | |
3904 | close(pipefd[0]); | |
3905 | if (sizeof(status) != nread) { | |
3906 | ERROR("reading status from pipe failed"); | |
3907 | goto err_wait; | |
3908 | } | |
3909 | ||
3910 | // If the criu process was killed or exited nonzero, wait() for the | |
3911 | // handler, since the restore process died. Otherwise, we don't need to | |
3912 | // wait, since the child becomes the monitor process. | |
3913 | if (!WIFEXITED(status) || WEXITSTATUS(status)) | |
3914 | goto err_wait; | |
3915 | return true; | |
3916 | ||
3917 | err_wait: | |
3918 | if (wait_for_pid(pid)) | |
3919 | ERROR("restore process died"); | |
3920 | return false; | |
735f2c6e TA |
3921 | } |
3922 | ||
858377e4 SH |
3923 | WRAP_API_2(bool, lxcapi_restore, char *, bool) |
3924 | ||
a0e93eeb CS |
3925 | static int lxcapi_attach_run_waitl(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char *arg, ...) |
3926 | { | |
3927 | va_list ap; | |
3928 | const char **argv; | |
3929 | int ret; | |
3930 | ||
3931 | if (!c) | |
3932 | return -1; | |
3933 | ||
858377e4 SH |
3934 | current_config = c->lxc_conf; |
3935 | ||
a0e93eeb CS |
3936 | va_start(ap, arg); |
3937 | argv = lxc_va_arg_list_to_argv_const(ap, 1); | |
3938 | va_end(ap); | |
3939 | ||
3940 | if (!argv) { | |
3941 | ERROR("Memory allocation error."); | |
858377e4 SH |
3942 | ret = -1; |
3943 | goto out; | |
a0e93eeb CS |
3944 | } |
3945 | argv[0] = arg; | |
3946 | ||
858377e4 | 3947 | ret = do_lxcapi_attach_run_wait(c, options, program, (const char * const *)argv); |
a0e93eeb | 3948 | free((void*)argv); |
858377e4 SH |
3949 | out: |
3950 | current_config = NULL; | |
a0e93eeb CS |
3951 | return ret; |
3952 | } | |
3953 | ||
afeecbba | 3954 | struct lxc_container *lxc_container_new(const char *name, const char *configpath) |
72d0e1cb SG |
3955 | { |
3956 | struct lxc_container *c; | |
72d0e1cb | 3957 | |
18aa217b SH |
3958 | if (!name) |
3959 | return NULL; | |
3960 | ||
72d0e1cb SG |
3961 | c = malloc(sizeof(*c)); |
3962 | if (!c) { | |
3963 | fprintf(stderr, "failed to malloc lxc_container\n"); | |
3964 | return NULL; | |
3965 | } | |
3966 | memset(c, 0, sizeof(*c)); | |
3967 | ||
afeecbba SH |
3968 | if (configpath) |
3969 | c->config_path = strdup(configpath); | |
3970 | else | |
593e8478 | 3971 | c->config_path = strdup(lxc_global_config_value("lxc.lxcpath")); |
afeecbba | 3972 | |
2a59a681 | 3973 | if (!c->config_path) { |
03fadd16 | 3974 | fprintf(stderr, "Out of memory\n"); |
2a59a681 SH |
3975 | goto err; |
3976 | } | |
3977 | ||
f5dd1d53 | 3978 | remove_trailing_slashes(c->config_path); |
72d0e1cb SG |
3979 | c->name = malloc(strlen(name)+1); |
3980 | if (!c->name) { | |
3981 | fprintf(stderr, "Error allocating lxc_container name\n"); | |
3982 | goto err; | |
3983 | } | |
3984 | strcpy(c->name, name); | |
3985 | ||
3986 | c->numthreads = 1; | |
df271a59 | 3987 | if (!(c->slock = lxc_newlock(c->config_path, name))) { |
72d0e1cb SG |
3988 | fprintf(stderr, "failed to create lock\n"); |
3989 | goto err; | |
3990 | } | |
3991 | ||
df271a59 | 3992 | if (!(c->privlock = lxc_newlock(NULL, NULL))) { |
72d0e1cb SG |
3993 | fprintf(stderr, "failed to alloc privlock\n"); |
3994 | goto err; | |
3995 | } | |
3996 | ||
afeecbba | 3997 | if (!set_config_filename(c)) { |
72d0e1cb SG |
3998 | fprintf(stderr, "Error allocating config file pathname\n"); |
3999 | goto err; | |
4000 | } | |
72d0e1cb | 4001 | |
bac806d1 SH |
4002 | if (file_exists(c->configfile) && !lxcapi_load_config(c, NULL)) |
4003 | goto err; | |
72d0e1cb | 4004 | |
3e625e2d SH |
4005 | if (ongoing_create(c) == 2) { |
4006 | ERROR("Error: %s creation was not completed", c->name); | |
18aa217b | 4007 | container_destroy(c); |
4df7f012 | 4008 | lxcapi_clear_config(c); |
3e625e2d | 4009 | } |
a2739df5 | 4010 | c->daemonize = true; |
72cf75fa | 4011 | c->pidfile = NULL; |
3e625e2d | 4012 | |
72d0e1cb SG |
4013 | // assign the member functions |
4014 | c->is_defined = lxcapi_is_defined; | |
4015 | c->state = lxcapi_state; | |
4016 | c->is_running = lxcapi_is_running; | |
4017 | c->freeze = lxcapi_freeze; | |
4018 | c->unfreeze = lxcapi_unfreeze; | |
0115f8fd | 4019 | c->console = lxcapi_console; |
b5159817 | 4020 | c->console_getfd = lxcapi_console_getfd; |
72d0e1cb SG |
4021 | c->init_pid = lxcapi_init_pid; |
4022 | c->load_config = lxcapi_load_config; | |
4023 | c->want_daemonize = lxcapi_want_daemonize; | |
130a1888 | 4024 | c->want_close_all_fds = lxcapi_want_close_all_fds; |
72d0e1cb SG |
4025 | c->start = lxcapi_start; |
4026 | c->startl = lxcapi_startl; | |
4027 | c->stop = lxcapi_stop; | |
4028 | c->config_file_name = lxcapi_config_file_name; | |
4029 | c->wait = lxcapi_wait; | |
4030 | c->set_config_item = lxcapi_set_config_item; | |
4031 | c->destroy = lxcapi_destroy; | |
18aa217b | 4032 | c->destroy_with_snapshots = lxcapi_destroy_with_snapshots; |
06e5650e | 4033 | c->rename = lxcapi_rename; |
72d0e1cb SG |
4034 | c->save_config = lxcapi_save_config; |
4035 | c->get_keys = lxcapi_get_keys; | |
4036 | c->create = lxcapi_create; | |
4037 | c->createl = lxcapi_createl; | |
4038 | c->shutdown = lxcapi_shutdown; | |
3e625e2d | 4039 | c->reboot = lxcapi_reboot; |
4df7f012 | 4040 | c->clear_config = lxcapi_clear_config; |
72d0e1cb SG |
4041 | c->clear_config_item = lxcapi_clear_config_item; |
4042 | c->get_config_item = lxcapi_get_config_item; | |
8ac18377 | 4043 | c->get_running_config_item = lxcapi_get_running_config_item; |
794dd120 SH |
4044 | c->get_cgroup_item = lxcapi_get_cgroup_item; |
4045 | c->set_cgroup_item = lxcapi_set_cgroup_item; | |
2a59a681 SH |
4046 | c->get_config_path = lxcapi_get_config_path; |
4047 | c->set_config_path = lxcapi_set_config_path; | |
9be53773 | 4048 | c->clone = lxcapi_clone; |
799f29ab | 4049 | c->get_interfaces = lxcapi_get_interfaces; |
9c83a661 | 4050 | c->get_ips = lxcapi_get_ips; |
a0e93eeb CS |
4051 | c->attach = lxcapi_attach; |
4052 | c->attach_run_wait = lxcapi_attach_run_wait; | |
4053 | c->attach_run_waitl = lxcapi_attach_run_waitl; | |
f5dd1d53 SH |
4054 | c->snapshot = lxcapi_snapshot; |
4055 | c->snapshot_list = lxcapi_snapshot_list; | |
4056 | c->snapshot_restore = lxcapi_snapshot_restore; | |
771d96b3 | 4057 | c->snapshot_destroy = lxcapi_snapshot_destroy; |
18aa217b | 4058 | c->snapshot_destroy_all = lxcapi_snapshot_destroy_all; |
b494d2dd | 4059 | c->may_control = lxcapi_may_control; |
a9a0ed90 ÇO |
4060 | c->add_device_node = lxcapi_add_device_node; |
4061 | c->remove_device_node = lxcapi_remove_device_node; | |
e58fae8f DY |
4062 | c->attach_interface = lxcapi_attach_interface; |
4063 | c->detach_interface = lxcapi_detach_interface; | |
735f2c6e TA |
4064 | c->checkpoint = lxcapi_checkpoint; |
4065 | c->restore = lxcapi_restore; | |
72d0e1cb | 4066 | |
72d0e1cb SG |
4067 | return c; |
4068 | ||
4069 | err: | |
4070 | lxc_container_free(c); | |
4071 | return NULL; | |
4072 | } | |
4073 | ||
4a7c7daa | 4074 | int lxc_get_wait_states(const char **states) |
72d0e1cb SG |
4075 | { |
4076 | int i; | |
4077 | ||
4078 | if (states) | |
4079 | for (i=0; i<MAX_STATE; i++) | |
4080 | states[i] = lxc_state2str(i); | |
4081 | return MAX_STATE; | |
4082 | } | |
a41f104b | 4083 | |
a41f104b SH |
4084 | /* |
4085 | * These next two could probably be done smarter with reusing a common function | |
4086 | * with different iterators and tests... | |
4087 | */ | |
4088 | int list_defined_containers(const char *lxcpath, char ***names, struct lxc_container ***cret) | |
4089 | { | |
4090 | DIR *dir; | |
4091 | int i, cfound = 0, nfound = 0; | |
4092 | struct dirent dirent, *direntp; | |
4093 | struct lxc_container *c; | |
4094 | ||
4095 | if (!lxcpath) | |
593e8478 | 4096 | lxcpath = lxc_global_config_value("lxc.lxcpath"); |
a41f104b | 4097 | |
a41f104b | 4098 | dir = opendir(lxcpath); |
a41f104b SH |
4099 | if (!dir) { |
4100 | SYSERROR("opendir on lxcpath"); | |
4101 | return -1; | |
4102 | } | |
4103 | ||
4104 | if (cret) | |
4105 | *cret = NULL; | |
4106 | if (names) | |
4107 | *names = NULL; | |
4108 | ||
4109 | while (!readdir_r(dir, &dirent, &direntp)) { | |
4110 | if (!direntp) | |
4111 | break; | |
4112 | if (!strcmp(direntp->d_name, ".")) | |
4113 | continue; | |
4114 | if (!strcmp(direntp->d_name, "..")) | |
4115 | continue; | |
4116 | ||
4117 | if (!config_file_exists(lxcpath, direntp->d_name)) | |
4118 | continue; | |
4119 | ||
4120 | if (names) { | |
9c88ff1f | 4121 | if (!add_to_array(names, direntp->d_name, cfound)) |
a41f104b SH |
4122 | goto free_bad; |
4123 | } | |
4124 | cfound++; | |
4125 | ||
4126 | if (!cret) { | |
4127 | nfound++; | |
4128 | continue; | |
4129 | } | |
4130 | ||
4131 | c = lxc_container_new(direntp->d_name, lxcpath); | |
4132 | if (!c) { | |
4133 | INFO("Container %s:%s has a config but could not be loaded", | |
4134 | lxcpath, direntp->d_name); | |
4135 | if (names) | |
9c88ff1f ÇO |
4136 | if(!remove_from_array(names, direntp->d_name, cfound--)) |
4137 | goto free_bad; | |
a41f104b SH |
4138 | continue; |
4139 | } | |
858377e4 | 4140 | if (!do_lxcapi_is_defined(c)) { |
a41f104b SH |
4141 | INFO("Container %s:%s has a config but is not defined", |
4142 | lxcpath, direntp->d_name); | |
4143 | if (names) | |
9c88ff1f ÇO |
4144 | if(!remove_from_array(names, direntp->d_name, cfound--)) |
4145 | goto free_bad; | |
a41f104b SH |
4146 | lxc_container_put(c); |
4147 | continue; | |
4148 | } | |
4149 | ||
2871830a | 4150 | if (!add_to_clist(cret, c, nfound, true)) { |
a41f104b SH |
4151 | lxc_container_put(c); |
4152 | goto free_bad; | |
4153 | } | |
4154 | nfound++; | |
4155 | } | |
4156 | ||
a41f104b | 4157 | closedir(dir); |
a41f104b SH |
4158 | return nfound; |
4159 | ||
4160 | free_bad: | |
4161 | if (names && *names) { | |
4162 | for (i=0; i<cfound; i++) | |
4163 | free((*names)[i]); | |
4164 | free(*names); | |
4165 | } | |
4166 | if (cret && *cret) { | |
4167 | for (i=0; i<nfound; i++) | |
4168 | lxc_container_put((*cret)[i]); | |
4169 | free(*cret); | |
4170 | } | |
a41f104b | 4171 | closedir(dir); |
a41f104b SH |
4172 | return -1; |
4173 | } | |
4174 | ||
148a9d27 DE |
4175 | int list_active_containers(const char *lxcpath, char ***nret, |
4176 | struct lxc_container ***cret) | |
a41f104b | 4177 | { |
148a9d27 | 4178 | int i, ret = -1, cret_cnt = 0, ct_name_cnt = 0; |
a41f104b SH |
4179 | int lxcpath_len; |
4180 | char *line = NULL; | |
148a9d27 | 4181 | char **ct_name = NULL; |
a41f104b SH |
4182 | size_t len = 0; |
4183 | struct lxc_container *c; | |
88556fd7 | 4184 | bool is_hashed; |
a41f104b SH |
4185 | |
4186 | if (!lxcpath) | |
593e8478 | 4187 | lxcpath = lxc_global_config_value("lxc.lxcpath"); |
a41f104b SH |
4188 | lxcpath_len = strlen(lxcpath); |
4189 | ||
4190 | if (cret) | |
4191 | *cret = NULL; | |
148a9d27 DE |
4192 | if (nret) |
4193 | *nret = NULL; | |
a41f104b | 4194 | |
a41f104b | 4195 | FILE *f = fopen("/proc/net/unix", "r"); |
a41f104b SH |
4196 | if (!f) |
4197 | return -1; | |
4198 | ||
4199 | while (getline(&line, &len, f) != -1) { | |
88556fd7 | 4200 | |
0f8f9c8a | 4201 | char *p = strrchr(line, ' '), *p2; |
a41f104b SH |
4202 | if (!p) |
4203 | continue; | |
4204 | p++; | |
4205 | if (*p != 0x40) | |
4206 | continue; | |
4207 | p++; | |
88556fd7 ÇO |
4208 | |
4209 | is_hashed = false; | |
4210 | if (strncmp(p, lxcpath, lxcpath_len) == 0) { | |
4211 | p += lxcpath_len; | |
4212 | } else if (strncmp(p, "lxc/", 4) == 0) { | |
4213 | p += 4; | |
4214 | is_hashed = true; | |
4215 | } else { | |
a41f104b | 4216 | continue; |
88556fd7 ÇO |
4217 | } |
4218 | ||
a41f104b SH |
4219 | while (*p == '/') |
4220 | p++; | |
4221 | ||
4222 | // Now p is the start of lxc_name | |
46cd2845 | 4223 | p2 = strchr(p, '/'); |
a41f104b SH |
4224 | if (!p2 || strncmp(p2, "/command", 8) != 0) |
4225 | continue; | |
4226 | *p2 = '\0'; | |
4227 | ||
88556fd7 ÇO |
4228 | if (is_hashed) { |
4229 | if (strncmp(lxcpath, lxc_cmd_get_lxcpath(p), lxcpath_len) != 0) | |
4230 | continue; | |
4231 | p = lxc_cmd_get_name(p); | |
4232 | } | |
4233 | ||
148a9d27 | 4234 | if (array_contains(&ct_name, p, ct_name_cnt)) |
9c88ff1f ÇO |
4235 | continue; |
4236 | ||
148a9d27 DE |
4237 | if (!add_to_array(&ct_name, p, ct_name_cnt)) |
4238 | goto free_cret_list; | |
9c88ff1f | 4239 | |
148a9d27 | 4240 | ct_name_cnt++; |
a41f104b | 4241 | |
148a9d27 | 4242 | if (!cret) |
a41f104b | 4243 | continue; |
a41f104b SH |
4244 | |
4245 | c = lxc_container_new(p, lxcpath); | |
4246 | if (!c) { | |
4247 | INFO("Container %s:%s is running but could not be loaded", | |
4248 | lxcpath, p); | |
148a9d27 | 4249 | remove_from_array(&ct_name, p, ct_name_cnt--); |
a41f104b SH |
4250 | continue; |
4251 | } | |
4252 | ||
4253 | /* | |
4254 | * If this is an anonymous container, then is_defined *can* | |
4255 | * return false. So we don't do that check. Count on the | |
4256 | * fact that the command socket exists. | |
4257 | */ | |
4258 | ||
148a9d27 | 4259 | if (!add_to_clist(cret, c, cret_cnt, true)) { |
a41f104b | 4260 | lxc_container_put(c); |
148a9d27 | 4261 | goto free_cret_list; |
a41f104b | 4262 | } |
148a9d27 | 4263 | cret_cnt++; |
a41f104b SH |
4264 | } |
4265 | ||
148a9d27 DE |
4266 | assert(!nret || !cret || cret_cnt == ct_name_cnt); |
4267 | ret = ct_name_cnt; | |
4268 | if (nret) | |
4269 | *nret = ct_name; | |
4270 | else | |
4271 | goto free_ct_name; | |
4272 | goto out; | |
a41f104b | 4273 | |
148a9d27 | 4274 | free_cret_list: |
a41f104b | 4275 | if (cret && *cret) { |
148a9d27 | 4276 | for (i = 0; i < cret_cnt; i++) |
a41f104b SH |
4277 | lxc_container_put((*cret)[i]); |
4278 | free(*cret); | |
4279 | } | |
148a9d27 DE |
4280 | |
4281 | free_ct_name: | |
4282 | if (ct_name) { | |
4283 | for (i = 0; i < ct_name_cnt; i++) | |
4284 | free(ct_name[i]); | |
4285 | free(ct_name); | |
4286 | } | |
4287 | ||
4288 | out: | |
f10fad2f | 4289 | free(line); |
e853a32d | 4290 | |
a41f104b | 4291 | fclose(f); |
148a9d27 | 4292 | return ret; |
a41f104b | 4293 | } |
2871830a DE |
4294 | |
4295 | int list_all_containers(const char *lxcpath, char ***nret, | |
4296 | struct lxc_container ***cret) | |
4297 | { | |
4298 | int i, ret, active_cnt, ct_cnt, ct_list_cnt; | |
4299 | char **active_name; | |
4300 | char **ct_name; | |
4301 | struct lxc_container **ct_list = NULL; | |
4302 | ||
4303 | ct_cnt = list_defined_containers(lxcpath, &ct_name, NULL); | |
4304 | if (ct_cnt < 0) | |
4305 | return ct_cnt; | |
4306 | ||
4307 | active_cnt = list_active_containers(lxcpath, &active_name, NULL); | |
4308 | if (active_cnt < 0) { | |
4309 | ret = active_cnt; | |
4310 | goto free_ct_name; | |
4311 | } | |
4312 | ||
4313 | for (i = 0; i < active_cnt; i++) { | |
4314 | if (!array_contains(&ct_name, active_name[i], ct_cnt)) { | |
4315 | if (!add_to_array(&ct_name, active_name[i], ct_cnt)) { | |
4316 | ret = -1; | |
4317 | goto free_active_name; | |
4318 | } | |
4319 | ct_cnt++; | |
4320 | } | |
4321 | free(active_name[i]); | |
4322 | active_name[i] = NULL; | |
4323 | } | |
4324 | free(active_name); | |
4325 | active_name = NULL; | |
4326 | active_cnt = 0; | |
4327 | ||
4328 | for (i = 0, ct_list_cnt = 0; i < ct_cnt && cret; i++) { | |
4329 | struct lxc_container *c; | |
4330 | ||
4331 | c = lxc_container_new(ct_name[i], lxcpath); | |
4332 | if (!c) { | |
4333 | WARN("Container %s:%s could not be loaded", lxcpath, ct_name[i]); | |
4334 | remove_from_array(&ct_name, ct_name[i], ct_cnt--); | |
4335 | continue; | |
4336 | } | |
4337 | ||
4338 | if (!add_to_clist(&ct_list, c, ct_list_cnt, false)) { | |
4339 | lxc_container_put(c); | |
4340 | ret = -1; | |
4341 | goto free_ct_list; | |
4342 | } | |
4343 | ct_list_cnt++; | |
4344 | } | |
4345 | ||
4346 | if (cret) | |
4347 | *cret = ct_list; | |
4348 | ||
4349 | if (nret) | |
4350 | *nret = ct_name; | |
4351 | else { | |
4352 | ret = ct_cnt; | |
4353 | goto free_ct_name; | |
4354 | } | |
4355 | return ct_cnt; | |
4356 | ||
4357 | free_ct_list: | |
4358 | for (i = 0; i < ct_list_cnt; i++) { | |
4359 | lxc_container_put(ct_list[i]); | |
4360 | } | |
f10fad2f | 4361 | free(ct_list); |
2871830a DE |
4362 | |
4363 | free_active_name: | |
4364 | for (i = 0; i < active_cnt; i++) { | |
f10fad2f | 4365 | free(active_name[i]); |
2871830a | 4366 | } |
f10fad2f | 4367 | free(active_name); |
2871830a DE |
4368 | |
4369 | free_ct_name: | |
4370 | for (i = 0; i < ct_cnt; i++) { | |
4371 | free(ct_name[i]); | |
4372 | } | |
4373 | free(ct_name); | |
4374 | return ret; | |
4375 | } |