]>
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 |
148a9d27 | 22 | #include <assert.h> |
a0e93eeb | 23 | #include <stdarg.h> |
71454076 | 24 | #include <pthread.h> |
9be53773 SH |
25 | #include <unistd.h> |
26 | #include <sys/types.h> | |
27 | #include <sys/wait.h> | |
4de2791f | 28 | #include <sys/mount.h> |
9be53773 | 29 | #include <errno.h> |
93dc5327 | 30 | #include <fcntl.h> |
9be53773 | 31 | #include <sched.h> |
f5dd1d53 | 32 | #include <dirent.h> |
f2363e38 ÇO |
33 | #include <sched.h> |
34 | #include <arpa/inet.h> | |
35 | #include <libgen.h> | |
d659597e | 36 | #include <stdint.h> |
f2363e38 ÇO |
37 | |
38 | #include <lxc/lxccontainer.h> | |
39 | #include <lxc/version.h> | |
40 | ||
9be53773 | 41 | #include "config.h" |
72d0e1cb SG |
42 | #include "lxc.h" |
43 | #include "state.h" | |
72d0e1cb | 44 | #include "conf.h" |
72d0e1cb | 45 | #include "confile.h" |
b5159817 | 46 | #include "console.h" |
72d0e1cb SG |
47 | #include "cgroup.h" |
48 | #include "commands.h" | |
49 | #include "log.h" | |
9be53773 | 50 | #include "bdev.h" |
6a44839f | 51 | #include "utils.h" |
a0e93eeb | 52 | #include "attach.h" |
f2363e38 ÇO |
53 | #include "monitor.h" |
54 | #include "namespace.h" | |
95ee490b | 55 | #include "lxclock.h" |
4ba0d9af SG |
56 | |
57 | #if HAVE_IFADDRS_H | |
9c83a661 | 58 | #include <ifaddrs.h> |
4ba0d9af SG |
59 | #else |
60 | #include <../include/ifaddrs.h> | |
61 | #endif | |
72d0e1cb | 62 | |
a9a0ed90 ÇO |
63 | #define MAX_BUFFER 4096 |
64 | ||
72d0e1cb SG |
65 | lxc_log_define(lxc_container, lxc); |
66 | ||
f0ca2726 | 67 | static bool file_exists(const char *f) |
3e625e2d SH |
68 | { |
69 | struct stat statbuf; | |
70 | ||
71 | return stat(f, &statbuf) == 0; | |
72 | } | |
73 | ||
a41f104b SH |
74 | static bool config_file_exists(const char *lxcpath, const char *cname) |
75 | { | |
76 | /* $lxcpath + '/' + $cname + '/config' + \0 */ | |
77 | int ret, len = strlen(lxcpath) + strlen(cname) + 9; | |
78 | char *fname = alloca(len); | |
79 | ||
80 | ret = snprintf(fname, len, "%s/%s/config", lxcpath, cname); | |
81 | if (ret < 0 || ret >= len) | |
82 | return false; | |
83 | ||
84 | return file_exists(fname); | |
85 | } | |
86 | ||
3e625e2d SH |
87 | /* |
88 | * A few functions to help detect when a container creation failed. | |
89 | * If a container creation was killed partway through, then trying | |
90 | * to actually start that container could harm the host. We detect | |
91 | * this by creating a 'partial' file under the container directory, | |
92 | * and keeping an advisory lock. When container creation completes, | |
93 | * we remove that file. When we load or try to start a container, if | |
94 | * we find that file, without a flock, we remove the container. | |
95 | */ | |
74a3920a | 96 | static int ongoing_create(struct lxc_container *c) |
3e625e2d SH |
97 | { |
98 | int len = strlen(c->config_path) + strlen(c->name) + 10; | |
99 | char *path = alloca(len); | |
100 | int fd, ret; | |
93dc5327 SH |
101 | struct flock lk; |
102 | ||
3e625e2d SH |
103 | ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name); |
104 | if (ret < 0 || ret >= len) { | |
105 | ERROR("Error writing partial pathname"); | |
106 | return -1; | |
107 | } | |
108 | ||
109 | if (!file_exists(path)) | |
110 | return 0; | |
025ed0f3 | 111 | fd = open(path, O_RDWR); |
025ed0f3 | 112 | if (fd < 0) { |
3e625e2d SH |
113 | // give benefit of the doubt |
114 | SYSERROR("Error opening partial file"); | |
3e625e2d SH |
115 | return 0; |
116 | } | |
93dc5327 SH |
117 | lk.l_type = F_WRLCK; |
118 | lk.l_whence = SEEK_SET; | |
119 | lk.l_start = 0; | |
120 | lk.l_len = 0; | |
121 | lk.l_pid = -1; | |
122 | if (fcntl(fd, F_GETLK, &lk) == 0 && lk.l_pid != -1) { | |
3e625e2d SH |
123 | // create is still ongoing |
124 | close(fd); | |
3e625e2d SH |
125 | return 1; |
126 | } | |
127 | // create completed but partial is still there. | |
128 | close(fd); | |
3e625e2d SH |
129 | return 2; |
130 | } | |
131 | ||
74a3920a | 132 | static int create_partial(struct lxc_container *c) |
3e625e2d SH |
133 | { |
134 | // $lxcpath + '/' + $name + '/partial' + \0 | |
135 | int len = strlen(c->config_path) + strlen(c->name) + 10; | |
136 | char *path = alloca(len); | |
137 | int fd, ret; | |
93dc5327 SH |
138 | struct flock lk; |
139 | ||
3e625e2d SH |
140 | ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name); |
141 | if (ret < 0 || ret >= len) { | |
142 | ERROR("Error writing partial pathname"); | |
143 | return -1; | |
144 | } | |
93dc5327 | 145 | if ((fd=open(path, O_RDWR | O_CREAT | O_EXCL, 0755)) < 0) { |
3e625e2d | 146 | SYSERROR("Erorr creating partial file"); |
3e625e2d SH |
147 | return -1; |
148 | } | |
93dc5327 SH |
149 | lk.l_type = F_WRLCK; |
150 | lk.l_whence = SEEK_SET; | |
151 | lk.l_start = 0; | |
152 | lk.l_len = 0; | |
153 | if (fcntl(fd, F_SETLKW, &lk) < 0) { | |
3e625e2d SH |
154 | SYSERROR("Error locking partial file %s", path); |
155 | close(fd); | |
3e625e2d SH |
156 | return -1; |
157 | } | |
3e625e2d SH |
158 | |
159 | return fd; | |
160 | } | |
161 | ||
74a3920a | 162 | static void remove_partial(struct lxc_container *c, int fd) |
3e625e2d SH |
163 | { |
164 | // $lxcpath + '/' + $name + '/partial' + \0 | |
165 | int len = strlen(c->config_path) + strlen(c->name) + 10; | |
166 | char *path = alloca(len); | |
167 | int ret; | |
168 | ||
169 | close(fd); | |
170 | ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name); | |
171 | if (ret < 0 || ret >= len) { | |
172 | ERROR("Error writing partial pathname"); | |
173 | return; | |
174 | } | |
3e625e2d SH |
175 | if (unlink(path) < 0) |
176 | SYSERROR("Error unlink partial file %s", path); | |
3e625e2d SH |
177 | } |
178 | ||
72d0e1cb | 179 | /* LOCKING |
3bc449ed SH |
180 | * 1. container_mem_lock(c) protects the struct lxc_container from multiple threads. |
181 | * 2. container_disk_lock(c) protects the on-disk container data - in particular the | |
182 | * container configuration file. | |
183 | * The container_disk_lock also takes the container_mem_lock. | |
184 | * 3. thread_mutex protects process data (ex: fd table) from multiple threads. | |
72d0e1cb SG |
185 | * NOTHING mutexes two independent programs with their own struct |
186 | * lxc_container for the same c->name, between API calls. For instance, | |
187 | * c->config_read(); c->start(); Between those calls, data on disk | |
188 | * could change (which shouldn't bother the caller unless for instance | |
189 | * the rootfs get moved). c->config_read(); update; c->config_write(); | |
190 | * Two such updaters could race. The callers should therefore check their | |
191 | * results. Trying to prevent that would necessarily expose us to deadlocks | |
192 | * due to hung callers. So I prefer to keep the locks only within our own | |
193 | * functions, not across functions. | |
194 | * | |
3bc449ed | 195 | * If you're going to clone while holding a lxccontainer, increment |
72d0e1cb SG |
196 | * c->numthreads (under privlock) before forking. When deleting, |
197 | * decrement numthreads under privlock, then if it hits 0 you can delete. | |
198 | * Do not ever use a lxccontainer whose numthreads you did not bump. | |
199 | */ | |
200 | ||
201 | static void lxc_container_free(struct lxc_container *c) | |
202 | { | |
203 | if (!c) | |
204 | return; | |
205 | ||
206 | if (c->configfile) { | |
207 | free(c->configfile); | |
208 | c->configfile = NULL; | |
209 | } | |
210 | if (c->error_string) { | |
211 | free(c->error_string); | |
212 | c->error_string = NULL; | |
213 | } | |
d95db067 | 214 | if (c->slock) { |
df271a59 | 215 | lxc_putlock(c->slock); |
d95db067 DE |
216 | c->slock = NULL; |
217 | } | |
72d0e1cb | 218 | if (c->privlock) { |
df271a59 | 219 | lxc_putlock(c->privlock); |
72d0e1cb SG |
220 | c->privlock = NULL; |
221 | } | |
222 | if (c->name) { | |
223 | free(c->name); | |
224 | c->name = NULL; | |
225 | } | |
d95db067 DE |
226 | if (c->lxc_conf) { |
227 | lxc_conf_free(c->lxc_conf); | |
228 | c->lxc_conf = NULL; | |
229 | } | |
2a59a681 SH |
230 | if (c->config_path) { |
231 | free(c->config_path); | |
232 | c->config_path = NULL; | |
233 | } | |
72d0e1cb SG |
234 | free(c); |
235 | } | |
236 | ||
43d1aa34 SH |
237 | /* |
238 | * Consider the following case: | |
239 | freer | racing get()er | |
240 | ================================================================== | |
241 | lxc_container_put() | lxc_container_get() | |
242 | \ lxclock(c->privlock) | c->numthreads < 1? (no) | |
243 | \ c->numthreads = 0 | \ lxclock(c->privlock) -> waits | |
244 | \ lxcunlock() | \ | |
245 | \ lxc_container_free() | \ lxclock() returns | |
246 | | \ c->numthreads < 1 -> return 0 | |
247 | \ \ (free stuff) | | |
248 | \ \ sem_destroy(privlock) | | |
249 | ||
250 | * When the get()er checks numthreads the first time, one of the following | |
251 | * is true: | |
252 | * 1. freer has set numthreads = 0. get() returns 0 | |
253 | * 2. freer is between lxclock and setting numthreads to 0. get()er will | |
254 | * sem_wait on privlock, get lxclock after freer() drops it, then see | |
255 | * numthreads is 0 and exit without touching lxclock again.. | |
256 | * 3. freer has not yet locked privlock. If get()er runs first, then put()er | |
257 | * will see --numthreads = 1 and not call lxc_container_free(). | |
258 | */ | |
259 | ||
72d0e1cb SG |
260 | int lxc_container_get(struct lxc_container *c) |
261 | { | |
262 | if (!c) | |
263 | return 0; | |
264 | ||
43d1aa34 SH |
265 | // if someone else has already started freeing the container, don't |
266 | // try to take the lock, which may be invalid | |
267 | if (c->numthreads < 1) | |
268 | return 0; | |
269 | ||
5cee8c50 | 270 | if (container_mem_lock(c)) |
72d0e1cb SG |
271 | return 0; |
272 | if (c->numthreads < 1) { | |
273 | // bail without trying to unlock, bc the privlock is now probably | |
274 | // in freed memory | |
275 | return 0; | |
276 | } | |
277 | c->numthreads++; | |
5cee8c50 | 278 | container_mem_unlock(c); |
72d0e1cb SG |
279 | return 1; |
280 | } | |
281 | ||
282 | int lxc_container_put(struct lxc_container *c) | |
283 | { | |
284 | if (!c) | |
285 | return -1; | |
5cee8c50 | 286 | if (container_mem_lock(c)) |
72d0e1cb SG |
287 | return -1; |
288 | if (--c->numthreads < 1) { | |
5cee8c50 | 289 | container_mem_unlock(c); |
72d0e1cb SG |
290 | lxc_container_free(c); |
291 | return 1; | |
292 | } | |
5cee8c50 | 293 | container_mem_unlock(c); |
72d0e1cb SG |
294 | return 0; |
295 | } | |
296 | ||
72d0e1cb SG |
297 | static bool lxcapi_is_defined(struct lxc_container *c) |
298 | { | |
299 | struct stat statbuf; | |
300 | bool ret = false; | |
301 | int statret; | |
302 | ||
303 | if (!c) | |
304 | return false; | |
305 | ||
5cee8c50 | 306 | if (container_mem_lock(c)) |
72d0e1cb SG |
307 | return false; |
308 | if (!c->configfile) | |
309 | goto out; | |
310 | statret = stat(c->configfile, &statbuf); | |
311 | if (statret != 0) | |
312 | goto out; | |
313 | ret = true; | |
314 | ||
315 | out: | |
5cee8c50 | 316 | container_mem_unlock(c); |
72d0e1cb SG |
317 | return ret; |
318 | } | |
319 | ||
320 | static const char *lxcapi_state(struct lxc_container *c) | |
321 | { | |
72d0e1cb SG |
322 | lxc_state_t s; |
323 | ||
324 | if (!c) | |
325 | return NULL; | |
13f5be62 | 326 | s = lxc_getstate(c->name, c->config_path); |
39dc698c | 327 | return lxc_state2str(s); |
72d0e1cb SG |
328 | } |
329 | ||
39dc698c | 330 | static bool is_stopped(struct lxc_container *c) |
794dd120 SH |
331 | { |
332 | lxc_state_t s; | |
13f5be62 | 333 | s = lxc_getstate(c->name, c->config_path); |
794dd120 SH |
334 | return (s == STOPPED); |
335 | } | |
336 | ||
72d0e1cb SG |
337 | static bool lxcapi_is_running(struct lxc_container *c) |
338 | { | |
339 | const char *s; | |
340 | ||
341 | if (!c) | |
342 | return false; | |
343 | s = lxcapi_state(c); | |
344 | if (!s || strcmp(s, "STOPPED") == 0) | |
345 | return false; | |
346 | return true; | |
347 | } | |
348 | ||
349 | static bool lxcapi_freeze(struct lxc_container *c) | |
350 | { | |
351 | int ret; | |
352 | if (!c) | |
353 | return false; | |
354 | ||
9123e471 | 355 | ret = lxc_freeze(c->name, c->config_path); |
72d0e1cb SG |
356 | if (ret) |
357 | return false; | |
358 | return true; | |
359 | } | |
360 | ||
361 | static bool lxcapi_unfreeze(struct lxc_container *c) | |
362 | { | |
363 | int ret; | |
364 | if (!c) | |
365 | return false; | |
366 | ||
9123e471 | 367 | ret = lxc_unfreeze(c->name, c->config_path); |
72d0e1cb SG |
368 | if (ret) |
369 | return false; | |
370 | return true; | |
371 | } | |
372 | ||
b5159817 | 373 | static int lxcapi_console_getfd(struct lxc_container *c, int *ttynum, int *masterfd) |
0115f8fd DE |
374 | { |
375 | int ttyfd; | |
376 | if (!c) | |
377 | return -1; | |
378 | ||
b5159817 | 379 | ttyfd = lxc_console_getfd(c, ttynum, masterfd); |
0115f8fd DE |
380 | return ttyfd; |
381 | } | |
382 | ||
b5159817 DE |
383 | static int lxcapi_console(struct lxc_container *c, int ttynum, int stdinfd, |
384 | int stdoutfd, int stderrfd, int escape) | |
385 | { | |
386 | return lxc_console(c, ttynum, stdinfd, stdoutfd, stderrfd, escape); | |
387 | } | |
388 | ||
72d0e1cb SG |
389 | static pid_t lxcapi_init_pid(struct lxc_container *c) |
390 | { | |
72d0e1cb SG |
391 | if (!c) |
392 | return -1; | |
393 | ||
5cee8c50 | 394 | return lxc_cmd_get_init_pid(c->name, c->config_path); |
72d0e1cb SG |
395 | } |
396 | ||
12a50cc6 | 397 | static bool load_config_locked(struct lxc_container *c, const char *fname) |
8eb5694b SH |
398 | { |
399 | if (!c->lxc_conf) | |
400 | c->lxc_conf = lxc_conf_init(); | |
401 | if (c->lxc_conf && !lxc_config_read(fname, c->lxc_conf)) | |
402 | return true; | |
403 | return false; | |
404 | } | |
405 | ||
12a50cc6 | 406 | static bool lxcapi_load_config(struct lxc_container *c, const char *alt_file) |
72d0e1cb | 407 | { |
39dc698c SH |
408 | bool ret = false, need_disklock = false; |
409 | int lret; | |
12a50cc6 | 410 | const char *fname; |
72d0e1cb SG |
411 | if (!c) |
412 | return false; | |
413 | ||
414 | fname = c->configfile; | |
415 | if (alt_file) | |
416 | fname = alt_file; | |
417 | if (!fname) | |
418 | return false; | |
39dc698c SH |
419 | /* |
420 | * If we're reading something other than the container's config, | |
421 | * we only need to lock the in-memory container. If loading the | |
422 | * container's config file, take the disk lock. | |
423 | */ | |
424 | if (strcmp(fname, c->configfile) == 0) | |
425 | need_disklock = true; | |
426 | ||
427 | if (need_disklock) | |
428 | lret = container_disk_lock(c); | |
429 | else | |
430 | lret = container_mem_lock(c); | |
431 | if (lret) | |
72d0e1cb | 432 | return false; |
39dc698c | 433 | |
8eb5694b | 434 | ret = load_config_locked(c, fname); |
39dc698c SH |
435 | |
436 | if (need_disklock) | |
437 | container_disk_unlock(c); | |
438 | else | |
439 | container_mem_unlock(c); | |
72d0e1cb SG |
440 | return ret; |
441 | } | |
442 | ||
540f932a | 443 | static bool lxcapi_want_daemonize(struct lxc_container *c, bool state) |
72d0e1cb | 444 | { |
497a2995 | 445 | if (!c || !c->lxc_conf) |
540f932a | 446 | return false; |
f02abefe | 447 | if (container_mem_lock(c)) { |
3bc449ed | 448 | ERROR("Error getting mem lock"); |
540f932a | 449 | return false; |
3bc449ed | 450 | } |
540f932a | 451 | c->daemonize = state; |
83758ed0 | 452 | /* daemonize implies close_all_fds so set it */ |
540f932a SG |
453 | if (state == 1) |
454 | c->lxc_conf->close_all_fds = 1; | |
3bc449ed | 455 | container_mem_unlock(c); |
540f932a | 456 | return true; |
72d0e1cb SG |
457 | } |
458 | ||
540f932a | 459 | static bool lxcapi_want_close_all_fds(struct lxc_container *c, bool state) |
130a1888 ÇO |
460 | { |
461 | if (!c || !c->lxc_conf) | |
49badbbe | 462 | return false; |
130a1888 ÇO |
463 | if (container_mem_lock(c)) { |
464 | ERROR("Error getting mem lock"); | |
49badbbe | 465 | return false; |
130a1888 | 466 | } |
540f932a | 467 | c->lxc_conf->close_all_fds = state; |
130a1888 | 468 | container_mem_unlock(c); |
49badbbe | 469 | return true; |
130a1888 ÇO |
470 | } |
471 | ||
12a50cc6 | 472 | static bool lxcapi_wait(struct lxc_container *c, const char *state, int timeout) |
7a44c8b4 SG |
473 | { |
474 | int ret; | |
475 | ||
476 | if (!c) | |
477 | return false; | |
478 | ||
67e571de | 479 | ret = lxc_wait(c->name, state, timeout, c->config_path); |
7a44c8b4 SG |
480 | return ret == 0; |
481 | } | |
482 | ||
483 | ||
03f064ff | 484 | static bool wait_on_daemonized_start(struct lxc_container *c, int pid) |
7a44c8b4 SG |
485 | { |
486 | /* we'll probably want to make this timeout configurable? */ | |
697fa639 | 487 | int timeout = 5, ret, status; |
7a44c8b4 | 488 | |
697fa639 SH |
489 | /* |
490 | * our child is going to fork again, then exit. reap the | |
491 | * child | |
492 | */ | |
03f064ff | 493 | ret = waitpid(pid, &status, 0); |
697fa639 SH |
494 | if (ret == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) |
495 | DEBUG("failed waiting for first dual-fork child"); | |
7a44c8b4 SG |
496 | return lxcapi_wait(c, "RUNNING", timeout); |
497 | } | |
498 | ||
2d834aa8 SH |
499 | static bool am_single_threaded(void) |
500 | { | |
501 | struct dirent dirent, *direntp; | |
502 | DIR *dir; | |
503 | int count=0; | |
504 | ||
2d834aa8 | 505 | dir = opendir("/proc/self/task"); |
2d834aa8 SH |
506 | if (!dir) { |
507 | INFO("failed to open /proc/self/task"); | |
508 | return false; | |
509 | } | |
510 | ||
511 | while (!readdir_r(dir, &dirent, &direntp)) { | |
512 | if (!direntp) | |
513 | break; | |
514 | ||
515 | if (!strcmp(direntp->d_name, ".")) | |
516 | continue; | |
517 | ||
518 | if (!strcmp(direntp->d_name, "..")) | |
519 | continue; | |
520 | if (++count > 1) | |
521 | break; | |
522 | } | |
2d834aa8 | 523 | closedir(dir); |
2d834aa8 SH |
524 | return count == 1; |
525 | } | |
526 | ||
72d0e1cb SG |
527 | /* |
528 | * I can't decide if it'd be more convenient for callers if we accept '...', | |
529 | * or a null-terminated array (i.e. execl vs execv) | |
530 | */ | |
12a50cc6 | 531 | static bool lxcapi_start(struct lxc_container *c, int useinit, char * const argv[]) |
72d0e1cb SG |
532 | { |
533 | int ret; | |
534 | struct lxc_conf *conf; | |
540f932a | 535 | bool daemonize = false; |
72d0e1cb SG |
536 | char *default_args[] = { |
537 | "/sbin/init", | |
538 | '\0', | |
539 | }; | |
540 | ||
541 | /* container exists */ | |
542 | if (!c) | |
543 | return false; | |
544 | /* container has been setup */ | |
545 | if (!c->lxc_conf) | |
546 | return false; | |
547 | ||
3e625e2d SH |
548 | if ((ret = ongoing_create(c)) < 0) { |
549 | ERROR("Error checking for incomplete creation"); | |
550 | return false; | |
551 | } | |
552 | if (ret == 2) { | |
553 | ERROR("Error: %s creation was not completed", c->name); | |
554 | c->destroy(c); | |
555 | return false; | |
556 | } else if (ret == 1) { | |
557 | ERROR("Error: creation of %s is ongoing", c->name); | |
558 | return false; | |
559 | } | |
560 | ||
72d0e1cb SG |
561 | /* is this app meant to be run through lxcinit, as in lxc-execute? */ |
562 | if (useinit && !argv) | |
563 | return false; | |
564 | ||
5cee8c50 | 565 | if (container_mem_lock(c)) |
72d0e1cb SG |
566 | return false; |
567 | conf = c->lxc_conf; | |
568 | daemonize = c->daemonize; | |
5cee8c50 | 569 | container_mem_unlock(c); |
72d0e1cb SG |
570 | |
571 | if (useinit) { | |
13f5be62 | 572 | ret = lxc_execute(c->name, argv, 1, conf, c->config_path); |
72d0e1cb SG |
573 | return ret == 0 ? true : false; |
574 | } | |
575 | ||
576 | if (!argv) | |
577 | argv = default_args; | |
578 | ||
579 | /* | |
580 | * say, I'm not sure - what locks do we want here? Any? | |
581 | * Is liblxc's locking enough here to protect the on disk | |
582 | * container? We don't want to exclude things like lxc_info | |
583 | * while container is running... | |
584 | */ | |
585 | if (daemonize) { | |
586 | if (!lxc_container_get(c)) | |
587 | return false; | |
e51d4895 | 588 | lxc_monitord_spawn(c->config_path); |
71454076 | 589 | |
72d0e1cb SG |
590 | pid_t pid = fork(); |
591 | if (pid < 0) { | |
592 | lxc_container_put(c); | |
593 | return false; | |
594 | } | |
025ed0f3 | 595 | if (pid != 0) |
03f064ff | 596 | return wait_on_daemonized_start(c, pid); |
025ed0f3 | 597 | |
697fa639 SH |
598 | /* second fork to be reparented by init */ |
599 | pid = fork(); | |
600 | if (pid < 0) { | |
601 | SYSERROR("Error doing dual-fork"); | |
602 | return false; | |
603 | } | |
604 | if (pid != 0) | |
605 | exit(0); | |
72d0e1cb | 606 | /* like daemon(), chdir to / and redirect 0,1,2 to /dev/null */ |
c278cef2 SH |
607 | if (chdir("/")) { |
608 | SYSERROR("Error chdir()ing to /."); | |
609 | return false; | |
610 | } | |
72d0e1cb SG |
611 | close(0); |
612 | close(1); | |
613 | close(2); | |
eddaaafd | 614 | open("/dev/zero", O_RDONLY); |
72d0e1cb SG |
615 | open("/dev/null", O_RDWR); |
616 | open("/dev/null", O_RDWR); | |
617 | setsid(); | |
2d834aa8 SH |
618 | } else { |
619 | if (!am_single_threaded()) { | |
620 | ERROR("Cannot start non-daemonized container when threaded"); | |
621 | return false; | |
622 | } | |
72d0e1cb SG |
623 | } |
624 | ||
72d0e1cb SG |
625 | reboot: |
626 | conf->reboot = 0; | |
13f5be62 | 627 | ret = lxc_start(c->name, argv, conf, c->config_path); |
72d0e1cb SG |
628 | |
629 | if (conf->reboot) { | |
630 | INFO("container requested reboot"); | |
631 | conf->reboot = 0; | |
72d0e1cb SG |
632 | goto reboot; |
633 | } | |
634 | ||
635 | if (daemonize) { | |
636 | lxc_container_put(c); | |
637 | exit (ret == 0 ? true : false); | |
638 | } else { | |
639 | return (ret == 0 ? true : false); | |
640 | } | |
641 | } | |
642 | ||
643 | /* | |
644 | * note there MUST be an ending NULL | |
645 | */ | |
646 | static bool lxcapi_startl(struct lxc_container *c, int useinit, ...) | |
647 | { | |
648 | va_list ap; | |
a0e93eeb | 649 | char **inargs = NULL; |
72d0e1cb SG |
650 | bool bret = false; |
651 | ||
652 | /* container exists */ | |
653 | if (!c) | |
654 | return false; | |
655 | ||
72d0e1cb | 656 | va_start(ap, useinit); |
a0e93eeb | 657 | inargs = lxc_va_arg_list_to_argv(ap, 0, 1); |
72d0e1cb SG |
658 | va_end(ap); |
659 | ||
a0e93eeb CS |
660 | if (!inargs) { |
661 | ERROR("Memory allocation error."); | |
662 | goto out; | |
72d0e1cb SG |
663 | } |
664 | ||
a0e93eeb CS |
665 | /* pass NULL if no arguments were supplied */ |
666 | bret = lxcapi_start(c, useinit, *inargs ? inargs : NULL); | |
72d0e1cb SG |
667 | |
668 | out: | |
669 | if (inargs) { | |
4e03ae57 DE |
670 | char **arg; |
671 | for (arg = inargs; *arg; arg++) | |
672 | free(*arg); | |
72d0e1cb SG |
673 | free(inargs); |
674 | } | |
675 | ||
676 | return bret; | |
677 | } | |
678 | ||
679 | static bool lxcapi_stop(struct lxc_container *c) | |
680 | { | |
681 | int ret; | |
682 | ||
683 | if (!c) | |
684 | return false; | |
685 | ||
ef6e34ee | 686 | ret = lxc_cmd_stop(c->name, c->config_path); |
72d0e1cb SG |
687 | |
688 | return ret == 0; | |
72d0e1cb SG |
689 | } |
690 | ||
72d0e1cb SG |
691 | /* |
692 | * create the standard expected container dir | |
693 | */ | |
694 | static bool create_container_dir(struct lxc_container *c) | |
695 | { | |
696 | char *s; | |
697 | int len, ret; | |
698 | ||
2a59a681 | 699 | len = strlen(c->config_path) + strlen(c->name) + 2; |
72d0e1cb SG |
700 | s = malloc(len); |
701 | if (!s) | |
702 | return false; | |
2a59a681 | 703 | ret = snprintf(s, len, "%s/%s", c->config_path, c->name); |
72d0e1cb SG |
704 | if (ret < 0 || ret >= len) { |
705 | free(s); | |
706 | return false; | |
707 | } | |
708 | ret = mkdir(s, 0755); | |
709 | if (ret) { | |
710 | if (errno == EEXIST) | |
711 | ret = 0; | |
712 | else | |
713 | SYSERROR("failed to create container path for %s\n", c->name); | |
714 | } | |
715 | free(s); | |
716 | return ret == 0; | |
717 | } | |
718 | ||
1897e3bc SH |
719 | static const char *lxcapi_get_config_path(struct lxc_container *c); |
720 | static bool lxcapi_set_config_item(struct lxc_container *c, const char *key, const char *v); | |
721 | ||
72d0e1cb | 722 | /* |
1897e3bc SH |
723 | * do_bdev_create: thin wrapper around bdev_create(). Like bdev_create(), |
724 | * it returns a mounted bdev on success, NULL on error. | |
72d0e1cb | 725 | */ |
1897e3bc SH |
726 | static struct bdev *do_bdev_create(struct lxc_container *c, const char *type, |
727 | struct bdev_specs *specs) | |
728 | { | |
729 | char *dest; | |
1897e3bc SH |
730 | size_t len; |
731 | struct bdev *bdev; | |
732 | int ret; | |
733 | ||
cd219ae6 SY |
734 | /* rootfs.path or lxcpath/lxcname/rootfs */ |
735 | if (c->lxc_conf->rootfs.path && access(c->lxc_conf->rootfs.path, F_OK) == 0) { | |
cf465fe4 SH |
736 | const char *rpath = c->lxc_conf->rootfs.path; |
737 | len = strlen(rpath) + 1; | |
cd219ae6 | 738 | dest = alloca(len); |
cf465fe4 | 739 | ret = snprintf(dest, len, "%s", rpath); |
cd219ae6 | 740 | } else { |
cf465fe4 | 741 | const char *lxcpath = lxcapi_get_config_path(c); |
cd219ae6 SY |
742 | len = strlen(c->name) + strlen(lxcpath) + 9; |
743 | dest = alloca(len); | |
744 | ret = snprintf(dest, len, "%s/%s/rootfs", lxcpath, c->name); | |
745 | } | |
1897e3bc SH |
746 | if (ret < 0 || ret >= len) |
747 | return NULL; | |
748 | ||
749 | bdev = bdev_create(dest, type, c->name, specs); | |
d44e88c2 SH |
750 | if (!bdev) { |
751 | ERROR("Failed to create backing store type %s\n", type); | |
1897e3bc | 752 | return NULL; |
d44e88c2 SH |
753 | } |
754 | ||
1897e3bc | 755 | lxcapi_set_config_item(c, "lxc.rootfs", bdev->src); |
cf3ef16d SH |
756 | |
757 | /* if we are not root, chown the rootfs dir to root in the | |
758 | * target uidmap */ | |
759 | ||
760 | if (geteuid() != 0) { | |
c4d10a05 SH |
761 | if (chown_mapped_root(bdev->dest, c->lxc_conf) < 0) { |
762 | ERROR("Error chowning %s to container root\n", bdev->dest); | |
cf3ef16d SH |
763 | bdev_put(bdev); |
764 | return NULL; | |
765 | } | |
766 | } | |
767 | ||
1897e3bc SH |
768 | return bdev; |
769 | } | |
770 | ||
cbee8106 SH |
771 | /* |
772 | * Given the '-t' template option to lxc-create, figure out what to | |
773 | * do. If the template is a full executable path, use that. If it | |
85db5535 DE |
774 | * is something like 'sshd', then return $templatepath/lxc-sshd. |
775 | * On success return the template, on error return NULL. | |
cbee8106 | 776 | */ |
85db5535 | 777 | static char *get_template_path(const char *t) |
cbee8106 SH |
778 | { |
779 | int ret, len; | |
780 | char *tpath; | |
781 | ||
cbee8106 SH |
782 | if (t[0] == '/' && access(t, X_OK) == 0) { |
783 | tpath = strdup(t); | |
cbee8106 SH |
784 | return tpath; |
785 | } | |
786 | ||
787 | len = strlen(LXCTEMPLATEDIR) + strlen(t) + strlen("/lxc-") + 1; | |
788 | tpath = malloc(len); | |
789 | if (!tpath) | |
85db5535 | 790 | return NULL; |
cbee8106 SH |
791 | ret = snprintf(tpath, len, "%s/lxc-%s", LXCTEMPLATEDIR, t); |
792 | if (ret < 0 || ret >= len) { | |
793 | free(tpath); | |
85db5535 | 794 | return NULL; |
cbee8106 SH |
795 | } |
796 | if (access(tpath, X_OK) < 0) { | |
797 | SYSERROR("bad template: %s\n", t); | |
798 | free(tpath); | |
85db5535 | 799 | return NULL; |
cbee8106 SH |
800 | } |
801 | ||
802 | return tpath; | |
803 | } | |
804 | ||
96b3cb40 | 805 | static char *lxcbasename(char *path) |
72d0e1cb | 806 | { |
96b3cb40 SH |
807 | char *p = path + strlen(path) - 1; |
808 | while (*p != '/' && p > path) | |
809 | p--; | |
810 | return p; | |
811 | } | |
72d0e1cb | 812 | |
dc23c1c8 | 813 | static bool create_run_template(struct lxc_container *c, char *tpath, bool quiet, |
96b3cb40 SH |
814 | char *const argv[]) |
815 | { | |
816 | pid_t pid; | |
72d0e1cb | 817 | |
72d0e1cb | 818 | if (!tpath) |
96b3cb40 | 819 | return true; |
72d0e1cb SG |
820 | |
821 | pid = fork(); | |
822 | if (pid < 0) { | |
823 | SYSERROR("failed to fork task for container creation template\n"); | |
96b3cb40 | 824 | return false; |
72d0e1cb SG |
825 | } |
826 | ||
827 | if (pid == 0) { // child | |
1897e3bc SH |
828 | char *patharg, *namearg, *rootfsarg, *src; |
829 | struct bdev *bdev = NULL; | |
72d0e1cb | 830 | int i; |
96b3cb40 SH |
831 | int ret, len, nargs = 0; |
832 | char **newargv; | |
cf3ef16d | 833 | struct lxc_conf *conf = c->lxc_conf; |
72d0e1cb | 834 | |
dc23c1c8 SH |
835 | if (quiet) { |
836 | close(0); | |
837 | close(1); | |
838 | close(2); | |
839 | open("/dev/zero", O_RDONLY); | |
840 | open("/dev/null", O_RDWR); | |
841 | open("/dev/null", O_RDWR); | |
842 | } | |
1897e3bc SH |
843 | |
844 | src = c->lxc_conf->rootfs.path; | |
845 | /* | |
846 | * for an overlayfs create, what the user wants is the template to fill | |
847 | * in what will become the readonly lower layer. So don't mount for | |
848 | * the template | |
849 | */ | |
850 | if (strncmp(src, "overlayfs:", 10) == 0) { | |
851 | src = overlayfs_getlower(src+10); | |
852 | } | |
853 | bdev = bdev_init(src, c->lxc_conf->rootfs.mount, NULL); | |
854 | if (!bdev) { | |
855 | ERROR("Error opening rootfs"); | |
856 | exit(1); | |
857 | } | |
858 | ||
4de2791f | 859 | if (geteuid() == 0) { |
cf3ef16d SH |
860 | if (unshare(CLONE_NEWNS) < 0) { |
861 | ERROR("error unsharing mounts"); | |
862 | exit(1); | |
863 | } | |
4de2791f SH |
864 | if (detect_shared_rootfs()) { |
865 | if (mount("", "", NULL, MS_SLAVE|MS_REC, 0)) { | |
866 | SYSERROR("Failed to make / rslave to run template"); | |
867 | ERROR("Continuing..."); | |
868 | } | |
869 | } | |
870 | } | |
871 | if (strcmp(bdev->type, "dir") != 0) { | |
872 | if (geteuid() != 0) { | |
873 | ERROR("non-root users can only create directory-backed containers"); | |
874 | exit(1); | |
875 | } | |
cf3ef16d SH |
876 | if (bdev->ops->mount(bdev) < 0) { |
877 | ERROR("Error mounting rootfs"); | |
878 | exit(1); | |
879 | } | |
880 | } else { // TODO come up with a better way here! | |
881 | if (bdev->dest) | |
882 | free(bdev->dest); | |
883 | bdev->dest = strdup(bdev->src); | |
1897e3bc SH |
884 | } |
885 | ||
72d0e1cb SG |
886 | /* |
887 | * create our new array, pre-pend the template name and | |
888 | * base args | |
889 | */ | |
890 | if (argv) | |
1897e3bc SH |
891 | for (nargs = 0; argv[nargs]; nargs++) ; |
892 | nargs += 4; // template, path, rootfs and name args | |
cf3ef16d | 893 | |
72d0e1cb SG |
894 | newargv = malloc(nargs * sizeof(*newargv)); |
895 | if (!newargv) | |
896 | exit(1); | |
96b3cb40 | 897 | newargv[0] = lxcbasename(tpath); |
72d0e1cb | 898 | |
2a59a681 | 899 | len = strlen(c->config_path) + strlen(c->name) + strlen("--path=") + 2; |
72d0e1cb SG |
900 | patharg = malloc(len); |
901 | if (!patharg) | |
902 | exit(1); | |
2a59a681 | 903 | ret = snprintf(patharg, len, "--path=%s/%s", c->config_path, c->name); |
72d0e1cb SG |
904 | if (ret < 0 || ret >= len) |
905 | exit(1); | |
906 | newargv[1] = patharg; | |
907 | len = strlen("--name=") + strlen(c->name) + 1; | |
908 | namearg = malloc(len); | |
909 | if (!namearg) | |
910 | exit(1); | |
911 | ret = snprintf(namearg, len, "--name=%s", c->name); | |
912 | if (ret < 0 || ret >= len) | |
913 | exit(1); | |
914 | newargv[2] = namearg; | |
915 | ||
1897e3bc SH |
916 | len = strlen("--rootfs=") + 1 + strlen(bdev->dest); |
917 | rootfsarg = malloc(len); | |
918 | if (!rootfsarg) | |
919 | exit(1); | |
920 | ret = snprintf(rootfsarg, len, "--rootfs=%s", bdev->dest); | |
921 | if (ret < 0 || ret >= len) | |
922 | exit(1); | |
923 | newargv[3] = rootfsarg; | |
924 | ||
72d0e1cb SG |
925 | /* add passed-in args */ |
926 | if (argv) | |
1897e3bc SH |
927 | for (i = 4; i < nargs; i++) |
928 | newargv[i] = argv[i-4]; | |
72d0e1cb SG |
929 | |
930 | /* add trailing NULL */ | |
931 | nargs++; | |
932 | newargv = realloc(newargv, nargs * sizeof(*newargv)); | |
933 | if (!newargv) | |
934 | exit(1); | |
935 | newargv[nargs - 1] = NULL; | |
936 | ||
cf3ef16d SH |
937 | /* |
938 | * If we're running the template in a mapped userns, then | |
939 | * we prepend the template command with: | |
940 | * lxc-usernsexec <-m map1> ... <-m mapn> -- | |
57d116ab SH |
941 | * and we append "--mapped-uid x", where x is the mapped uid |
942 | * for our geteuid() | |
cf3ef16d SH |
943 | */ |
944 | if (geteuid() != 0 && !lxc_list_empty(&conf->id_map)) { | |
945 | int n2args = 1; | |
57d116ab | 946 | char txtuid[20]; |
cf3ef16d SH |
947 | char **n2 = malloc(n2args * sizeof(*n2)); |
948 | struct lxc_list *it; | |
949 | struct id_map *map; | |
950 | ||
57d116ab SH |
951 | if (!n2) { |
952 | SYSERROR("out of memory"); | |
953 | exit(1); | |
954 | } | |
cf3ef16d SH |
955 | newargv[0] = tpath; |
956 | tpath = "lxc-usernsexec"; | |
957 | n2[0] = "lxc-usernsexec"; | |
958 | lxc_list_for_each(it, &conf->id_map) { | |
959 | map = it->elem; | |
960 | n2args += 2; | |
57d116ab | 961 | n2 = realloc(n2, n2args * sizeof(char *)); |
cf3ef16d SH |
962 | if (!n2) |
963 | exit(1); | |
964 | n2[n2args-2] = "-m"; | |
965 | n2[n2args-1] = malloc(200); | |
966 | if (!n2[n2args-1]) | |
967 | exit(1); | |
968 | ret = snprintf(n2[n2args-1], 200, "%c:%lu:%lu:%lu", | |
969 | map->idtype == ID_TYPE_UID ? 'u' : 'g', | |
970 | map->nsid, map->hostid, map->range); | |
971 | if (ret < 0 || ret >= 200) | |
972 | exit(1); | |
973 | } | |
57d116ab SH |
974 | int hostid_mapped = mapped_hostid(geteuid(), conf); |
975 | int extraargs = hostid_mapped >= 0 ? 1 : 3; | |
976 | n2 = realloc(n2, (nargs + n2args + extraargs) * sizeof(char *)); | |
cf3ef16d SH |
977 | if (!n2) |
978 | exit(1); | |
57d116ab SH |
979 | if (hostid_mapped < 0) { |
980 | hostid_mapped = find_unmapped_nsuid(conf); | |
cf3ef16d | 981 | n2[n2args++] = "-m"; |
57d116ab | 982 | if (hostid_mapped < 0) { |
cf3ef16d SH |
983 | ERROR("Could not find free uid to map"); |
984 | exit(1); | |
985 | } | |
986 | n2[n2args++] = malloc(200); | |
987 | if (!n2[n2args-1]) { | |
988 | SYSERROR("out of memory"); | |
989 | exit(1); | |
990 | } | |
991 | ret = snprintf(n2[n2args-1], 200, "u:%d:%d:1", | |
57d116ab | 992 | hostid_mapped, geteuid()); |
cf3ef16d SH |
993 | if (ret < 0 || ret >= 200) { |
994 | ERROR("string too long"); | |
995 | exit(1); | |
996 | } | |
997 | } | |
998 | n2[n2args++] = "--"; | |
999 | for (i = 0; i < nargs; i++) | |
1000 | n2[i + n2args] = newargv[i]; | |
57d116ab SH |
1001 | n2args += nargs; |
1002 | // Finally add "--mapped-uid $uid" to tell template what to chown | |
1003 | // cached images to | |
1004 | n2args += 2; | |
1005 | n2 = realloc(n2, n2args * sizeof(char *)); | |
1006 | if (!n2) { | |
1007 | SYSERROR("out of memory"); | |
1008 | exit(1); | |
1009 | } | |
1010 | // note n2[n2args-1] is NULL | |
1011 | n2[n2args-3] = "--mapped-uid"; | |
1012 | snprintf(txtuid, 20, "%d", hostid_mapped); | |
1013 | n2[n2args-2] = txtuid; | |
1014 | n2[n2args-1] = NULL; | |
cf3ef16d SH |
1015 | free(newargv); |
1016 | newargv = n2; | |
1017 | } | |
72d0e1cb | 1018 | /* execute */ |
cf3ef16d | 1019 | execvp(tpath, newargv); |
72d0e1cb SG |
1020 | SYSERROR("failed to execute template %s", tpath); |
1021 | exit(1); | |
1022 | } | |
1023 | ||
9be53773 SH |
1024 | if (wait_for_pid(pid) != 0) { |
1025 | ERROR("container creation template for %s failed\n", c->name); | |
96b3cb40 SH |
1026 | return false; |
1027 | } | |
1028 | ||
1029 | return true; | |
1030 | } | |
1031 | ||
74a3920a | 1032 | static bool prepend_lxc_header(char *path, const char *t, char *const argv[]) |
3ce74686 | 1033 | { |
1fd9bd50 | 1034 | long flen; |
b4569e93 | 1035 | char *contents; |
3ce74686 | 1036 | FILE *f; |
025ed0f3 | 1037 | int ret = -1; |
52026772 | 1038 | #if HAVE_LIBGNUTLS |
025ed0f3 | 1039 | int i; |
3ce74686 | 1040 | unsigned char md_value[SHA_DIGEST_LENGTH]; |
b4569e93 | 1041 | char *tpath; |
52026772 | 1042 | #endif |
3ce74686 | 1043 | |
025ed0f3 | 1044 | f = fopen(path, "r"); |
025ed0f3 | 1045 | if (f == NULL) |
3ce74686 | 1046 | return false; |
025ed0f3 SH |
1047 | |
1048 | if (fseek(f, 0, SEEK_END) < 0) | |
1049 | goto out_error; | |
1050 | if ((flen = ftell(f)) < 0) | |
1051 | goto out_error; | |
1052 | if (fseek(f, 0, SEEK_SET) < 0) | |
1053 | goto out_error; | |
1054 | if ((contents = malloc(flen + 1)) == NULL) | |
1055 | goto out_error; | |
1056 | if (fread(contents, 1, flen, f) != flen) | |
1057 | goto out_free_contents; | |
1058 | ||
3ce74686 | 1059 | contents[flen] = '\0'; |
025ed0f3 | 1060 | ret = fclose(f); |
025ed0f3 SH |
1061 | f = NULL; |
1062 | if (ret < 0) | |
1063 | goto out_free_contents; | |
3ce74686 | 1064 | |
b4569e93 | 1065 | #if HAVE_LIBGNUTLS |
01efd4d3 | 1066 | tpath = get_template_path(t); |
85db5535 | 1067 | if (!tpath) { |
3ce74686 | 1068 | ERROR("bad template: %s\n", t); |
025ed0f3 | 1069 | goto out_free_contents; |
3ce74686 SH |
1070 | } |
1071 | ||
85db5535 DE |
1072 | ret = sha1sum_file(tpath, md_value); |
1073 | if (ret < 0) { | |
1074 | ERROR("Error getting sha1sum of %s", tpath); | |
3ce74686 | 1075 | free(tpath); |
85db5535 | 1076 | goto out_free_contents; |
3ce74686 | 1077 | } |
85db5535 | 1078 | free(tpath); |
3ce74686 SH |
1079 | #endif |
1080 | ||
025ed0f3 | 1081 | f = fopen(path, "w"); |
025ed0f3 | 1082 | if (f == NULL) { |
3ce74686 SH |
1083 | SYSERROR("reopening config for writing"); |
1084 | free(contents); | |
1085 | return false; | |
1086 | } | |
1087 | fprintf(f, "# Template used to create this container: %s\n", t); | |
1088 | if (argv) { | |
1089 | fprintf(f, "# Parameters passed to the template:"); | |
1090 | while (*argv) { | |
1091 | fprintf(f, " %s", *argv); | |
1092 | argv++; | |
1093 | } | |
1094 | fprintf(f, "\n"); | |
1095 | } | |
1096 | #if HAVE_LIBGNUTLS | |
56698177 SH |
1097 | fprintf(f, "# Template script checksum (SHA-1): "); |
1098 | for (i=0; i<SHA_DIGEST_LENGTH; i++) | |
1099 | fprintf(f, "%02x", md_value[i]); | |
1100 | fprintf(f, "\n"); | |
3ce74686 | 1101 | #endif |
f2a95ee1 | 1102 | fprintf(f, "# For additional config options, please look at lxc.conf(5)\n"); |
3ce74686 SH |
1103 | if (fwrite(contents, 1, flen, f) != flen) { |
1104 | SYSERROR("Writing original contents"); | |
1105 | free(contents); | |
1106 | fclose(f); | |
1107 | return false; | |
1108 | } | |
025ed0f3 SH |
1109 | ret = 0; |
1110 | out_free_contents: | |
3ce74686 | 1111 | free(contents); |
025ed0f3 SH |
1112 | out_error: |
1113 | if (f) { | |
1114 | int newret; | |
025ed0f3 | 1115 | newret = fclose(f); |
025ed0f3 SH |
1116 | if (ret == 0) |
1117 | ret = newret; | |
1118 | } | |
1119 | if (ret < 0) { | |
1120 | SYSERROR("Error prepending header"); | |
3ce74686 SH |
1121 | return false; |
1122 | } | |
1123 | return true; | |
1124 | } | |
1125 | ||
4df7f012 SH |
1126 | static void lxcapi_clear_config(struct lxc_container *c) |
1127 | { | |
1128 | if (c && c->lxc_conf) { | |
1129 | lxc_conf_free(c->lxc_conf); | |
1130 | c->lxc_conf = NULL; | |
1131 | } | |
1132 | } | |
1133 | ||
96b3cb40 SH |
1134 | static bool lxcapi_destroy(struct lxc_container *c); |
1135 | /* | |
1136 | * lxcapi_create: | |
1137 | * create a container with the given parameters. | |
1138 | * @c: container to be created. It has the lxcpath, name, and a starting | |
1139 | * configuration already set | |
1140 | * @t: the template to execute to instantiate the root filesystem and | |
1141 | * adjust the configuration. | |
1142 | * @bdevtype: backing store type to use. If NULL, dir will be used. | |
1143 | * @specs: additional parameters for the backing store, i.e. LVM vg to | |
1144 | * use. | |
1145 | * | |
1146 | * @argv: the arguments to pass to the template, terminated by NULL. If no | |
1147 | * arguments, you can just pass NULL. | |
1148 | */ | |
1149 | static bool lxcapi_create(struct lxc_container *c, const char *t, | |
dc23c1c8 | 1150 | const char *bdevtype, struct bdev_specs *specs, int flags, |
96b3cb40 SH |
1151 | char *const argv[]) |
1152 | { | |
a69aad27 | 1153 | bool ret = false; |
96b3cb40 | 1154 | pid_t pid; |
85db5535 | 1155 | char *tpath = NULL; |
cbee8106 | 1156 | int partial_fd; |
96b3cb40 SH |
1157 | |
1158 | if (!c) | |
1159 | return false; | |
1160 | ||
85db5535 DE |
1161 | if (t) { |
1162 | tpath = get_template_path(t); | |
1163 | if (!tpath) { | |
1164 | ERROR("bad template: %s\n", t); | |
1165 | goto out; | |
1166 | } | |
96b3cb40 SH |
1167 | } |
1168 | ||
cf465fe4 SH |
1169 | /* |
1170 | * If a template is passed in, and the rootfs already is defined in | |
1171 | * the container config and exists, then * caller is trying to create | |
1172 | * an existing container. Return an error, but do NOT delete the | |
1173 | * container. | |
1174 | */ | |
1175 | if (lxcapi_is_defined(c) && c->lxc_conf && c->lxc_conf->rootfs.path && | |
1176 | access(c->lxc_conf->rootfs.path, F_OK) == 0 && tpath) { | |
1177 | ERROR("Container %s:%s already exists", c->config_path, c->name); | |
6c6892b5 | 1178 | goto free_tpath; |
cf465fe4 SH |
1179 | } |
1180 | ||
6c6892b5 | 1181 | if (!c->lxc_conf) { |
dad87e3b SG |
1182 | if (!c->load_config(c, lxc_global_config_value("lxc.default_config"))) { |
1183 | ERROR("Error loading default configuration file %s\n", lxc_global_config_value("lxc.default_config")); | |
6c6892b5 DE |
1184 | goto free_tpath; |
1185 | } | |
96b3cb40 SH |
1186 | } |
1187 | ||
6c6892b5 DE |
1188 | if (!create_container_dir(c)) |
1189 | goto free_tpath; | |
1190 | ||
0590e82c SH |
1191 | /* |
1192 | * either template or rootfs.path should be set. | |
1193 | * if both template and rootfs.path are set, template is setup as rootfs.path. | |
1194 | * container is already created if we have a config and rootfs.path is accessible | |
1195 | */ | |
1196 | if (!c->lxc_conf->rootfs.path && !tpath) | |
1197 | /* no template passed in and rootfs does not exist: error */ | |
1198 | goto out; | |
1199 | if (c->lxc_conf->rootfs.path && access(c->lxc_conf->rootfs.path, F_OK) != 0) | |
1200 | /* rootfs passed into configuration, but does not exist: error */ | |
1201 | goto out; | |
1202 | if (lxcapi_is_defined(c) && c->lxc_conf->rootfs.path && !tpath) { | |
1203 | /* Rootfs already existed, user just wanted to save the | |
1204 | * loaded configuration */ | |
1205 | ret = true; | |
1206 | goto out; | |
a69aad27 | 1207 | } |
96b3cb40 SH |
1208 | |
1209 | /* Mark that this container is being created */ | |
1210 | if ((partial_fd = create_partial(c)) < 0) | |
1211 | goto out; | |
1212 | ||
1213 | /* no need to get disk lock bc we have the partial locked */ | |
1214 | ||
1215 | /* | |
1216 | * Create the backing store | |
1217 | * Note we can't do this in the same task as we use to execute the | |
1218 | * template because of the way zfs works. | |
1219 | * After you 'zfs create', zfs mounts the fs only in the initial | |
1220 | * namespace. | |
1221 | */ | |
1222 | pid = fork(); | |
1223 | if (pid < 0) { | |
1224 | SYSERROR("failed to fork task for container creation template\n"); | |
8eb5694b SH |
1225 | goto out_unlock; |
1226 | } | |
1227 | ||
96b3cb40 SH |
1228 | if (pid == 0) { // child |
1229 | struct bdev *bdev = NULL; | |
1230 | ||
1231 | if (!(bdev = do_bdev_create(c, bdevtype, specs))) { | |
1232 | ERROR("Error creating backing store type %s for %s", | |
1233 | bdevtype ? bdevtype : "(none)", c->name); | |
1234 | exit(1); | |
1235 | } | |
1236 | ||
1237 | /* save config file again to store the new rootfs location */ | |
1238 | if (!c->save_config(c, NULL)) { | |
1239 | ERROR("failed to save starting configuration for %s\n", c->name); | |
1240 | // parent task won't see bdev in config so we delete it | |
1241 | bdev->ops->umount(bdev); | |
1242 | bdev->ops->destroy(bdev); | |
1243 | exit(1); | |
1244 | } | |
1245 | exit(0); | |
1246 | } | |
1247 | if (wait_for_pid(pid) != 0) | |
a09295f8 | 1248 | goto out_unlock; |
96b3cb40 SH |
1249 | |
1250 | /* reload config to get the rootfs */ | |
a3b47c09 | 1251 | lxc_conf_free(c->lxc_conf); |
96b3cb40 SH |
1252 | c->lxc_conf = NULL; |
1253 | if (!load_config_locked(c, c->configfile)) | |
a09295f8 | 1254 | goto out_unlock; |
96b3cb40 | 1255 | |
dc23c1c8 | 1256 | if (!create_run_template(c, tpath, !!(flags & LXC_CREATE_QUIET), argv)) |
96b3cb40 SH |
1257 | goto out_unlock; |
1258 | ||
8eb5694b SH |
1259 | // now clear out the lxc_conf we have, reload from the created |
1260 | // container | |
4df7f012 | 1261 | lxcapi_clear_config(c); |
3ce74686 | 1262 | |
9d65a487 KY |
1263 | if (t) { |
1264 | if (!prepend_lxc_header(c->configfile, tpath, argv)) { | |
1265 | ERROR("Error prepending header to configuration file"); | |
1266 | goto out_unlock; | |
1267 | } | |
3ce74686 | 1268 | } |
a69aad27 | 1269 | ret = load_config_locked(c, c->configfile); |
72d0e1cb SG |
1270 | |
1271 | out_unlock: | |
3e625e2d SH |
1272 | if (partial_fd >= 0) |
1273 | remove_partial(c, partial_fd); | |
72d0e1cb | 1274 | out: |
a69aad27 | 1275 | if (!ret && c) |
1897e3bc | 1276 | lxcapi_destroy(c); |
6c6892b5 DE |
1277 | free_tpath: |
1278 | if (tpath) | |
1279 | free(tpath); | |
a69aad27 | 1280 | return ret; |
72d0e1cb SG |
1281 | } |
1282 | ||
3e625e2d SH |
1283 | static bool lxcapi_reboot(struct lxc_container *c) |
1284 | { | |
1285 | pid_t pid; | |
1286 | ||
1287 | if (!c) | |
1288 | return false; | |
1289 | if (!c->is_running(c)) | |
1290 | return false; | |
1291 | pid = c->init_pid(c); | |
1292 | if (pid <= 0) | |
1293 | return false; | |
1294 | if (kill(pid, SIGINT) < 0) | |
1295 | return false; | |
1296 | return true; | |
1297 | ||
1298 | } | |
1299 | ||
72d0e1cb SG |
1300 | static bool lxcapi_shutdown(struct lxc_container *c, int timeout) |
1301 | { | |
1302 | bool retv; | |
1303 | pid_t pid; | |
f0f1d8c0 | 1304 | int haltsignal = SIGPWR; |
72d0e1cb SG |
1305 | |
1306 | if (!c) | |
1307 | return false; | |
1308 | ||
1309 | if (!timeout) | |
1310 | timeout = -1; | |
1311 | if (!c->is_running(c)) | |
1312 | return true; | |
1313 | pid = c->init_pid(c); | |
1314 | if (pid <= 0) | |
1315 | return true; | |
f0f1d8c0 DE |
1316 | if (c->lxc_conf->haltsignal) |
1317 | haltsignal = c->lxc_conf->haltsignal; | |
1318 | kill(pid, haltsignal); | |
72d0e1cb | 1319 | retv = c->wait(c, "STOPPED", timeout); |
f6144ed4 | 1320 | if (!retv && timeout > 0) { |
72d0e1cb SG |
1321 | c->stop(c); |
1322 | retv = c->wait(c, "STOPPED", 0); // 0 means don't wait | |
1323 | } | |
1324 | return retv; | |
1325 | } | |
1326 | ||
1897e3bc | 1327 | static bool lxcapi_createl(struct lxc_container *c, const char *t, |
dc23c1c8 | 1328 | const char *bdevtype, struct bdev_specs *specs, int flags, ...) |
72d0e1cb SG |
1329 | { |
1330 | bool bret = false; | |
a0e93eeb | 1331 | char **args = NULL; |
72d0e1cb | 1332 | va_list ap; |
72d0e1cb SG |
1333 | |
1334 | if (!c) | |
1335 | return false; | |
1336 | ||
1337 | /* | |
1338 | * since we're going to wait for create to finish, I don't think we | |
1339 | * need to get a copy of the arguments. | |
1340 | */ | |
dc23c1c8 | 1341 | va_start(ap, flags); |
a0e93eeb | 1342 | args = lxc_va_arg_list_to_argv(ap, 0, 0); |
72d0e1cb | 1343 | va_end(ap); |
a0e93eeb CS |
1344 | if (!args) { |
1345 | ERROR("Memory allocation error."); | |
1346 | goto out; | |
1347 | } | |
72d0e1cb | 1348 | |
dc23c1c8 | 1349 | bret = c->create(c, t, bdevtype, specs, flags, args); |
72d0e1cb SG |
1350 | |
1351 | out: | |
a0e93eeb | 1352 | free(args); |
72d0e1cb SG |
1353 | return bret; |
1354 | } | |
1355 | ||
12a50cc6 | 1356 | static bool lxcapi_clear_config_item(struct lxc_container *c, const char *key) |
72d0e1cb SG |
1357 | { |
1358 | int ret; | |
1359 | ||
1360 | if (!c || !c->lxc_conf) | |
1361 | return false; | |
5cee8c50 | 1362 | if (container_mem_lock(c)) |
72d0e1cb | 1363 | return false; |
72d0e1cb | 1364 | ret = lxc_clear_config_item(c->lxc_conf, key); |
5cee8c50 | 1365 | container_mem_unlock(c); |
72d0e1cb SG |
1366 | return ret == 0; |
1367 | } | |
1368 | ||
799f29ab ÇO |
1369 | static inline void exit_from_ns(struct lxc_container *c, int *old_netns, int *new_netns) { |
1370 | /* Switch back to original netns */ | |
1371 | if (*old_netns >= 0 && setns(*old_netns, CLONE_NEWNET)) | |
1372 | SYSERROR("failed to setns"); | |
799f29ab ÇO |
1373 | if (*new_netns >= 0) |
1374 | close(*new_netns); | |
1375 | if (*old_netns >= 0) | |
1376 | close(*old_netns); | |
799f29ab ÇO |
1377 | } |
1378 | ||
1379 | static inline bool enter_to_ns(struct lxc_container *c, int *old_netns, int *new_netns) { | |
1380 | int ret = 0; | |
9c83a661 | 1381 | char new_netns_path[MAXPATHLEN]; |
9c83a661 SG |
1382 | |
1383 | if (!c->is_running(c)) | |
1384 | goto out; | |
1385 | ||
1386 | /* Save reference to old netns */ | |
799f29ab | 1387 | *old_netns = open("/proc/self/ns/net", O_RDONLY); |
799f29ab | 1388 | if (*old_netns < 0) { |
9c83a661 SG |
1389 | SYSERROR("failed to open /proc/self/ns/net"); |
1390 | goto out; | |
1391 | } | |
1392 | ||
1393 | /* Switch to new netns */ | |
1394 | ret = snprintf(new_netns_path, MAXPATHLEN, "/proc/%d/ns/net", c->init_pid(c)); | |
1395 | if (ret < 0 || ret >= MAXPATHLEN) | |
1396 | goto out; | |
1397 | ||
799f29ab | 1398 | *new_netns = open(new_netns_path, O_RDONLY); |
799f29ab | 1399 | if (*new_netns < 0) { |
9c83a661 SG |
1400 | SYSERROR("failed to open %s", new_netns_path); |
1401 | goto out; | |
1402 | } | |
1403 | ||
799f29ab | 1404 | if (setns(*new_netns, CLONE_NEWNET)) { |
9c83a661 SG |
1405 | SYSERROR("failed to setns"); |
1406 | goto out; | |
1407 | } | |
799f29ab ÇO |
1408 | return true; |
1409 | out: | |
1410 | exit_from_ns(c, old_netns, new_netns); | |
1411 | return false; | |
1412 | } | |
1413 | ||
9c88ff1f ÇO |
1414 | // used by qsort and bsearch functions for comparing names |
1415 | static inline int string_cmp(char **first, char **second) | |
1416 | { | |
1417 | return strcmp(*first, *second); | |
1418 | } | |
1419 | ||
1420 | // used by qsort and bsearch functions for comparing container names | |
1421 | static inline int container_cmp(struct lxc_container **first, struct lxc_container **second) | |
1422 | { | |
1423 | return strcmp((*first)->name, (*second)->name); | |
1424 | } | |
1425 | ||
1426 | static bool add_to_array(char ***names, char *cname, int pos) | |
1427 | { | |
1428 | char **newnames = realloc(*names, (pos+1) * sizeof(char *)); | |
1429 | if (!newnames) { | |
1430 | ERROR("Out of memory"); | |
1431 | return false; | |
1432 | } | |
1433 | ||
1434 | *names = newnames; | |
1435 | newnames[pos] = strdup(cname); | |
1436 | if (!newnames[pos]) | |
1437 | return false; | |
1438 | ||
1439 | // sort the arrray as we will use binary search on it | |
1440 | qsort(newnames, pos + 1, sizeof(char *), (int (*)(const void *,const void *))string_cmp); | |
1441 | ||
1442 | return true; | |
1443 | } | |
1444 | ||
2871830a | 1445 | static bool add_to_clist(struct lxc_container ***list, struct lxc_container *c, int pos, bool sort) |
9c88ff1f ÇO |
1446 | { |
1447 | struct lxc_container **newlist = realloc(*list, (pos+1) * sizeof(struct lxc_container *)); | |
1448 | if (!newlist) { | |
1449 | ERROR("Out of memory"); | |
1450 | return false; | |
1451 | } | |
1452 | ||
1453 | *list = newlist; | |
1454 | newlist[pos] = c; | |
1455 | ||
1456 | // sort the arrray as we will use binary search on it | |
2871830a DE |
1457 | if (sort) |
1458 | qsort(newlist, pos + 1, sizeof(struct lxc_container *), (int (*)(const void *,const void *))container_cmp); | |
9c88ff1f ÇO |
1459 | |
1460 | return true; | |
1461 | } | |
1462 | ||
1463 | static char** get_from_array(char ***names, char *cname, int size) | |
1464 | { | |
1465 | return (char **)bsearch(&cname, *names, size, sizeof(char *), (int (*)(const void *, const void *))string_cmp); | |
1466 | } | |
1467 | ||
1468 | ||
1469 | static bool array_contains(char ***names, char *cname, int size) { | |
1470 | if(get_from_array(names, cname, size) != NULL) | |
1471 | return true; | |
1472 | return false; | |
1473 | } | |
1474 | ||
1475 | static bool remove_from_array(char ***names, char *cname, int size) | |
1476 | { | |
1477 | char **result = get_from_array(names, cname, size); | |
1478 | if (result != NULL) { | |
1479 | free(result); | |
1480 | return true; | |
1481 | } | |
1482 | return false; | |
1483 | } | |
1484 | ||
799f29ab ÇO |
1485 | static char** lxcapi_get_interfaces(struct lxc_container *c) |
1486 | { | |
9c88ff1f | 1487 | int i, count = 0; |
799f29ab | 1488 | struct ifaddrs *interfaceArray = NULL, *tempIfAddr = NULL; |
9c88ff1f | 1489 | char **interfaces = NULL; |
799f29ab ÇO |
1490 | int old_netns = -1, new_netns = -1; |
1491 | ||
1492 | if (!enter_to_ns(c, &old_netns, &new_netns)) | |
1493 | goto out; | |
1494 | ||
1495 | /* Grab the list of interfaces */ | |
1496 | if (getifaddrs(&interfaceArray)) { | |
1497 | SYSERROR("failed to get interfaces list"); | |
1498 | goto out; | |
1499 | } | |
1500 | ||
1501 | /* Iterate through the interfaces */ | |
1502 | for (tempIfAddr = interfaceArray; tempIfAddr != NULL; tempIfAddr = tempIfAddr->ifa_next) { | |
9c88ff1f ÇO |
1503 | if (array_contains(&interfaces, tempIfAddr->ifa_name, count)) |
1504 | continue; | |
799f29ab | 1505 | |
9c88ff1f ÇO |
1506 | if(!add_to_array(&interfaces, tempIfAddr->ifa_name, count)) |
1507 | goto err; | |
1508 | count++; | |
1509 | } | |
799f29ab ÇO |
1510 | |
1511 | out: | |
9c88ff1f ÇO |
1512 | if (interfaceArray) |
1513 | freeifaddrs(interfaceArray); | |
1514 | ||
1515 | exit_from_ns(c, &old_netns, &new_netns); | |
799f29ab | 1516 | |
9c88ff1f ÇO |
1517 | /* Append NULL to the array */ |
1518 | if(interfaces) | |
1519 | interfaces = (char **)lxc_append_null_to_array((void **)interfaces, count); | |
799f29ab | 1520 | |
9c88ff1f | 1521 | return interfaces; |
799f29ab | 1522 | |
9c88ff1f ÇO |
1523 | err: |
1524 | for(i=0;i<count;i++) | |
1525 | free(interfaces[i]); | |
1526 | free(interfaces); | |
1527 | interfaces = NULL; | |
1528 | goto out; | |
799f29ab ÇO |
1529 | } |
1530 | ||
f0ca2726 | 1531 | static char** lxcapi_get_ips(struct lxc_container *c, const char* interface, const char* family, int scope) |
799f29ab | 1532 | { |
9c88ff1f | 1533 | int i, count = 0; |
799f29ab ÇO |
1534 | struct ifaddrs *interfaceArray = NULL, *tempIfAddr = NULL; |
1535 | char addressOutputBuffer[INET6_ADDRSTRLEN]; | |
1536 | void *tempAddrPtr = NULL; | |
9c88ff1f | 1537 | char **addresses = NULL; |
799f29ab ÇO |
1538 | char *address = NULL; |
1539 | int old_netns = -1, new_netns = -1; | |
1540 | ||
1541 | if (!enter_to_ns(c, &old_netns, &new_netns)) | |
1542 | goto out; | |
9c83a661 SG |
1543 | |
1544 | /* Grab the list of interfaces */ | |
1545 | if (getifaddrs(&interfaceArray)) { | |
1546 | SYSERROR("failed to get interfaces list"); | |
1547 | goto out; | |
1548 | } | |
1549 | ||
1550 | /* Iterate through the interfaces */ | |
1551 | for (tempIfAddr = interfaceArray; tempIfAddr != NULL; tempIfAddr = tempIfAddr->ifa_next) { | |
fe218ca3 SG |
1552 | if (tempIfAddr->ifa_addr == NULL) |
1553 | continue; | |
1554 | ||
9c83a661 SG |
1555 | if(tempIfAddr->ifa_addr->sa_family == AF_INET) { |
1556 | if (family && strcmp(family, "inet")) | |
1557 | continue; | |
1558 | tempAddrPtr = &((struct sockaddr_in *)tempIfAddr->ifa_addr)->sin_addr; | |
1559 | } | |
1560 | else { | |
1561 | if (family && strcmp(family, "inet6")) | |
1562 | continue; | |
1563 | ||
1564 | if (((struct sockaddr_in6 *)tempIfAddr->ifa_addr)->sin6_scope_id != scope) | |
1565 | continue; | |
1566 | ||
1567 | tempAddrPtr = &((struct sockaddr_in6 *)tempIfAddr->ifa_addr)->sin6_addr; | |
1568 | } | |
1569 | ||
1570 | if (interface && strcmp(interface, tempIfAddr->ifa_name)) | |
1571 | continue; | |
1572 | else if (!interface && strcmp("lo", tempIfAddr->ifa_name) == 0) | |
1573 | continue; | |
1574 | ||
1575 | address = (char *)inet_ntop(tempIfAddr->ifa_addr->sa_family, | |
1576 | tempAddrPtr, | |
1577 | addressOutputBuffer, | |
1578 | sizeof(addressOutputBuffer)); | |
1579 | if (!address) | |
1580 | continue; | |
1581 | ||
9c88ff1f ÇO |
1582 | if(!add_to_array(&addresses, address, count)) |
1583 | goto err; | |
1584 | count++; | |
9c83a661 SG |
1585 | } |
1586 | ||
1587 | out: | |
1588 | if(interfaceArray) | |
1589 | freeifaddrs(interfaceArray); | |
1590 | ||
799f29ab | 1591 | exit_from_ns(c, &old_netns, &new_netns); |
9c83a661 SG |
1592 | |
1593 | /* Append NULL to the array */ | |
9c88ff1f ÇO |
1594 | if(addresses) |
1595 | addresses = (char **)lxc_append_null_to_array((void **)addresses, count); | |
9c83a661 SG |
1596 | |
1597 | return addresses; | |
9c88ff1f ÇO |
1598 | |
1599 | err: | |
1600 | for(i=0;i<count;i++) | |
1601 | free(addresses[i]); | |
1602 | free(addresses); | |
1603 | addresses = NULL; | |
1604 | ||
1605 | goto out; | |
9c83a661 SG |
1606 | } |
1607 | ||
12a50cc6 | 1608 | static int lxcapi_get_config_item(struct lxc_container *c, const char *key, char *retv, int inlen) |
72d0e1cb SG |
1609 | { |
1610 | int ret; | |
1611 | ||
1612 | if (!c || !c->lxc_conf) | |
1613 | return -1; | |
5cee8c50 | 1614 | if (container_mem_lock(c)) |
72d0e1cb | 1615 | return -1; |
72d0e1cb | 1616 | ret = lxc_get_config_item(c->lxc_conf, key, retv, inlen); |
5cee8c50 | 1617 | container_mem_unlock(c); |
72d0e1cb SG |
1618 | return ret; |
1619 | } | |
1620 | ||
12a50cc6 | 1621 | static int lxcapi_get_keys(struct lxc_container *c, const char *key, char *retv, int inlen) |
72d0e1cb SG |
1622 | { |
1623 | if (!key) | |
1624 | return lxc_listconfigs(retv, inlen); | |
1625 | /* | |
1626 | * Support 'lxc.network.<idx>', i.e. 'lxc.network.0' | |
1627 | * This is an intelligent result to show which keys are valid given | |
1628 | * the type of nic it is | |
1629 | */ | |
1630 | if (!c || !c->lxc_conf) | |
1631 | return -1; | |
5cee8c50 | 1632 | if (container_mem_lock(c)) |
72d0e1cb SG |
1633 | return -1; |
1634 | int ret = -1; | |
1635 | if (strncmp(key, "lxc.network.", 12) == 0) | |
1636 | ret = lxc_list_nicconfigs(c->lxc_conf, key, retv, inlen); | |
5cee8c50 | 1637 | container_mem_unlock(c); |
72d0e1cb SG |
1638 | return ret; |
1639 | } | |
1640 | ||
12a50cc6 | 1641 | static bool lxcapi_save_config(struct lxc_container *c, const char *alt_file) |
72d0e1cb | 1642 | { |
39dc698c SH |
1643 | FILE *fout; |
1644 | bool ret = false, need_disklock = false; | |
1645 | int lret; | |
1646 | ||
72d0e1cb SG |
1647 | if (!alt_file) |
1648 | alt_file = c->configfile; | |
1649 | if (!alt_file) | |
1650 | return false; // should we write to stdout if no file is specified? | |
39dc698c SH |
1651 | |
1652 | // If we haven't yet loaded a config, load the stock config | |
1653 | if (!c->lxc_conf) { | |
dad87e3b SG |
1654 | if (!c->load_config(c, lxc_global_config_value("lxc.default_config"))) { |
1655 | ERROR("Error loading default configuration file %s while saving %s\n", lxc_global_config_value("lxc.default_config"), c->name); | |
72d0e1cb SG |
1656 | return false; |
1657 | } | |
39dc698c | 1658 | } |
72d0e1cb | 1659 | |
5a3d2e1e SG |
1660 | if (!create_container_dir(c)) |
1661 | return false; | |
1662 | ||
39dc698c SH |
1663 | /* |
1664 | * If we're writing to the container's config file, take the | |
1665 | * disk lock. Otherwise just take the memlock to protect the | |
1666 | * struct lxc_container while we're traversing it. | |
1667 | */ | |
1668 | if (strcmp(c->configfile, alt_file) == 0) | |
1669 | need_disklock = true; | |
1670 | ||
1671 | if (need_disklock) | |
1672 | lret = container_disk_lock(c); | |
1673 | else | |
1674 | lret = container_mem_lock(c); | |
1675 | ||
1676 | if (lret) | |
72d0e1cb | 1677 | return false; |
39dc698c SH |
1678 | |
1679 | fout = fopen(alt_file, "w"); | |
1680 | if (!fout) | |
1681 | goto out; | |
72d0e1cb SG |
1682 | write_config(fout, c->lxc_conf); |
1683 | fclose(fout); | |
39dc698c SH |
1684 | ret = true; |
1685 | ||
1686 | out: | |
1687 | if (need_disklock) | |
1688 | container_disk_unlock(c); | |
1689 | else | |
1690 | container_mem_unlock(c); | |
1691 | return ret; | |
72d0e1cb SG |
1692 | } |
1693 | ||
dfb31b25 SH |
1694 | static bool mod_rdep(struct lxc_container *c, bool inc) |
1695 | { | |
1696 | char path[MAXPATHLEN]; | |
1697 | int ret, v = 0; | |
1698 | FILE *f; | |
1699 | bool bret = false; | |
1700 | ||
1701 | if (container_disk_lock(c)) | |
1702 | return false; | |
1703 | ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_snapshots", c->config_path, | |
1704 | c->name); | |
1705 | if (ret < 0 || ret > MAXPATHLEN) | |
1706 | goto out; | |
1707 | f = fopen(path, "r"); | |
1708 | if (f) { | |
1709 | ret = fscanf(f, "%d", &v); | |
1710 | fclose(f); | |
1711 | if (ret != 1) { | |
1712 | ERROR("Corrupted file %s", path); | |
1713 | goto out; | |
1714 | } | |
1715 | } | |
1716 | v += inc ? 1 : -1; | |
1717 | f = fopen(path, "w"); | |
1718 | if (!f) | |
1719 | goto out; | |
1720 | if (fprintf(f, "%d\n", v) < 0) { | |
1721 | ERROR("Error writing new snapshots value"); | |
1722 | fclose(f); | |
1723 | goto out; | |
1724 | } | |
025ed0f3 | 1725 | ret = fclose(f); |
025ed0f3 | 1726 | if (ret != 0) { |
dfb31b25 SH |
1727 | SYSERROR("Error writing to or closing snapshots file"); |
1728 | goto out; | |
1729 | } | |
1730 | ||
1731 | bret = true; | |
1732 | ||
1733 | out: | |
1734 | container_disk_unlock(c); | |
1735 | return bret; | |
1736 | } | |
1737 | ||
1738 | static void strip_newline(char *p) | |
1739 | { | |
1740 | size_t len = strlen(p); | |
1741 | if (len < 1) | |
1742 | return; | |
1743 | if (p[len-1] == '\n') | |
1744 | p[len-1] = '\0'; | |
1745 | } | |
1746 | ||
1747 | static void mod_all_rdeps(struct lxc_container *c, bool inc) | |
1748 | { | |
1749 | struct lxc_container *p; | |
1750 | char *lxcpath = NULL, *lxcname = NULL, path[MAXPATHLEN]; | |
1751 | size_t pathlen = 0, namelen = 0; | |
1752 | FILE *f; | |
1753 | int ret; | |
1754 | ||
1755 | ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_rdepends", | |
1756 | c->config_path, c->name); | |
1757 | if (ret < 0 || ret >= MAXPATHLEN) { | |
1758 | ERROR("Path name too long"); | |
1759 | return; | |
1760 | } | |
025ed0f3 | 1761 | f = fopen(path, "r"); |
025ed0f3 | 1762 | if (f == NULL) |
dfb31b25 SH |
1763 | return; |
1764 | while (getline(&lxcpath, &pathlen, f) != -1) { | |
1765 | if (getline(&lxcname, &namelen, f) == -1) { | |
1766 | ERROR("badly formatted file %s\n", path); | |
1767 | goto out; | |
1768 | } | |
1769 | strip_newline(lxcpath); | |
1770 | strip_newline(lxcname); | |
1771 | if ((p = lxc_container_new(lxcname, lxcpath)) == NULL) { | |
1772 | ERROR("Unable to find dependent container %s:%s", | |
1773 | lxcpath, lxcname); | |
1774 | continue; | |
1775 | } | |
1776 | if (!mod_rdep(p, inc)) | |
1777 | ERROR("Failed to increase numsnapshots for %s:%s", | |
1778 | lxcpath, lxcname); | |
1779 | lxc_container_put(p); | |
1780 | } | |
1781 | out: | |
1782 | if (lxcpath) free(lxcpath); | |
1783 | if (lxcname) free(lxcname); | |
1784 | fclose(f); | |
1785 | } | |
1786 | ||
1787 | static bool has_snapshots(struct lxc_container *c) | |
1788 | { | |
1789 | char path[MAXPATHLEN]; | |
1790 | int ret, v; | |
1791 | FILE *f; | |
1792 | bool bret = false; | |
1793 | ||
1794 | ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_snapshots", c->config_path, | |
1795 | c->name); | |
1796 | if (ret < 0 || ret > MAXPATHLEN) | |
1797 | goto out; | |
1798 | f = fopen(path, "r"); | |
1799 | if (!f) | |
1800 | goto out; | |
1801 | ret = fscanf(f, "%d", &v); | |
1802 | fclose(f); | |
1803 | if (ret != 1) | |
1804 | goto out; | |
1805 | bret = v != 0; | |
1806 | ||
1807 | out: | |
1808 | return bret; | |
1809 | } | |
1810 | ||
4355ab5f SH |
1811 | static int lxc_rmdir_onedev_wrapper(void *data) |
1812 | { | |
1813 | char *arg = (char *) data; | |
1814 | return lxc_rmdir_onedev(arg); | |
1815 | } | |
1816 | ||
60bf62d4 | 1817 | // do we want the api to support --force, or leave that to the caller? |
72d0e1cb SG |
1818 | static bool lxcapi_destroy(struct lxc_container *c) |
1819 | { | |
1897e3bc | 1820 | struct bdev *r = NULL; |
fef48dc9 SH |
1821 | bool bret = false, am_unpriv; |
1822 | int ret; | |
72d0e1cb | 1823 | |
1897e3bc | 1824 | if (!c || !lxcapi_is_defined(c)) |
5a3d2e1e SG |
1825 | return false; |
1826 | ||
3bc449ed | 1827 | if (container_disk_lock(c)) |
72d0e1cb | 1828 | return false; |
72d0e1cb | 1829 | |
39dc698c | 1830 | if (!is_stopped(c)) { |
60bf62d4 SH |
1831 | // we should queue some sort of error - in c->error_string? |
1832 | ERROR("container %s is not stopped", c->name); | |
1833 | goto out; | |
72d0e1cb SG |
1834 | } |
1835 | ||
4355ab5f SH |
1836 | am_unpriv = c->lxc_conf && geteuid() != 0 && !lxc_list_empty(&c->lxc_conf->id_map); |
1837 | ||
dfb31b25 | 1838 | if (c->lxc_conf && has_snapshots(c)) { |
2a2d36a4 | 1839 | ERROR("container %s has dependent snapshots", c->name); |
dfb31b25 SH |
1840 | goto out; |
1841 | } | |
1842 | ||
4355ab5f | 1843 | if (!am_unpriv && c->lxc_conf->rootfs.path && c->lxc_conf->rootfs.mount) { |
1897e3bc | 1844 | r = bdev_init(c->lxc_conf->rootfs.path, c->lxc_conf->rootfs.mount, NULL); |
4355ab5f SH |
1845 | if (r) { |
1846 | if (r->ops->destroy(r) < 0) { | |
1847 | bdev_put(r); | |
1848 | ERROR("Error destroying rootfs for %s", c->name); | |
1849 | goto out; | |
1850 | } | |
59d66af2 | 1851 | bdev_put(r); |
60bf62d4 SH |
1852 | } |
1853 | } | |
1854 | ||
dfb31b25 SH |
1855 | mod_all_rdeps(c, false); |
1856 | ||
60bf62d4 SH |
1857 | const char *p1 = lxcapi_get_config_path(c); |
1858 | char *path = alloca(strlen(p1) + strlen(c->name) + 2); | |
1859 | sprintf(path, "%s/%s", p1, c->name); | |
4355ab5f SH |
1860 | if (am_unpriv) |
1861 | ret = userns_exec_1(c->lxc_conf, lxc_rmdir_onedev_wrapper, path); | |
1862 | else | |
1863 | ret = lxc_rmdir_onedev(path); | |
1864 | if (ret < 0) { | |
60bf62d4 SH |
1865 | ERROR("Error destroying container directory for %s", c->name); |
1866 | goto out; | |
1867 | } | |
fef48dc9 | 1868 | bret = true; |
60bf62d4 SH |
1869 | |
1870 | out: | |
3bc449ed | 1871 | container_disk_unlock(c); |
fef48dc9 | 1872 | return bret; |
72d0e1cb SG |
1873 | } |
1874 | ||
96532523 SH |
1875 | static bool set_config_item_locked(struct lxc_container *c, const char *key, const char *v) |
1876 | { | |
1877 | struct lxc_config_t *config; | |
1878 | ||
1879 | if (!c->lxc_conf) | |
1880 | c->lxc_conf = lxc_conf_init(); | |
1881 | if (!c->lxc_conf) | |
1882 | return false; | |
1883 | config = lxc_getconfig(key); | |
1884 | if (!config) | |
1885 | return false; | |
1886 | return (0 == config->cb(key, v, c->lxc_conf)); | |
1887 | } | |
1888 | ||
12a50cc6 | 1889 | static bool lxcapi_set_config_item(struct lxc_container *c, const char *key, const char *v) |
72d0e1cb | 1890 | { |
72d0e1cb | 1891 | bool b = false; |
72d0e1cb SG |
1892 | |
1893 | if (!c) | |
1894 | return false; | |
1895 | ||
5cee8c50 | 1896 | if (container_mem_lock(c)) |
72d0e1cb SG |
1897 | return false; |
1898 | ||
96532523 | 1899 | b = set_config_item_locked(c, key, v); |
72d0e1cb | 1900 | |
5cee8c50 | 1901 | container_mem_unlock(c); |
72d0e1cb SG |
1902 | return b; |
1903 | } | |
1904 | ||
1905 | static char *lxcapi_config_file_name(struct lxc_container *c) | |
1906 | { | |
1907 | if (!c || !c->configfile) | |
1908 | return NULL; | |
1909 | return strdup(c->configfile); | |
1910 | } | |
1911 | ||
2a59a681 SH |
1912 | static const char *lxcapi_get_config_path(struct lxc_container *c) |
1913 | { | |
1914 | if (!c || !c->config_path) | |
1915 | return NULL; | |
1916 | return (const char *)(c->config_path); | |
1917 | } | |
1918 | ||
afeecbba SH |
1919 | /* |
1920 | * not for export | |
1921 | * Just recalculate the c->configfile based on the | |
1922 | * c->config_path, which must be set. | |
1923 | * The lxc_container must be locked or not yet public. | |
1924 | */ | |
1925 | static bool set_config_filename(struct lxc_container *c) | |
1926 | { | |
1927 | char *newpath; | |
1928 | int len, ret; | |
1929 | ||
1930 | if (!c->config_path) | |
1931 | return false; | |
1932 | ||
1933 | /* $lxc_path + "/" + c->name + "/" + "config" + '\0' */ | |
1934 | len = strlen(c->config_path) + strlen(c->name) + strlen("config") + 3; | |
1935 | newpath = malloc(len); | |
1936 | if (!newpath) | |
1937 | return false; | |
1938 | ||
1939 | ret = snprintf(newpath, len, "%s/%s/config", c->config_path, c->name); | |
1940 | if (ret < 0 || ret >= len) { | |
1941 | fprintf(stderr, "Error printing out config file name\n"); | |
1942 | free(newpath); | |
1943 | return false; | |
1944 | } | |
1945 | ||
1946 | if (c->configfile) | |
1947 | free(c->configfile); | |
1948 | c->configfile = newpath; | |
1949 | ||
1950 | return true; | |
1951 | } | |
1952 | ||
2a59a681 SH |
1953 | static bool lxcapi_set_config_path(struct lxc_container *c, const char *path) |
1954 | { | |
1955 | char *p; | |
1956 | bool b = false; | |
afeecbba | 1957 | char *oldpath = NULL; |
2a59a681 SH |
1958 | |
1959 | if (!c) | |
1960 | return b; | |
1961 | ||
5cee8c50 | 1962 | if (container_mem_lock(c)) |
2a59a681 SH |
1963 | return b; |
1964 | ||
1965 | p = strdup(path); | |
afeecbba SH |
1966 | if (!p) { |
1967 | ERROR("Out of memory setting new lxc path"); | |
2a59a681 | 1968 | goto err; |
afeecbba SH |
1969 | } |
1970 | ||
2a59a681 SH |
1971 | b = true; |
1972 | if (c->config_path) | |
afeecbba | 1973 | oldpath = c->config_path; |
2a59a681 | 1974 | c->config_path = p; |
afeecbba SH |
1975 | |
1976 | /* Since we've changed the config path, we have to change the | |
1977 | * config file name too */ | |
1978 | if (!set_config_filename(c)) { | |
1979 | ERROR("Out of memory setting new config filename"); | |
1980 | b = false; | |
1981 | free(c->config_path); | |
1982 | c->config_path = oldpath; | |
1983 | oldpath = NULL; | |
1984 | } | |
2a59a681 | 1985 | err: |
afeecbba SH |
1986 | if (oldpath) |
1987 | free(oldpath); | |
5cee8c50 | 1988 | container_mem_unlock(c); |
2a59a681 SH |
1989 | return b; |
1990 | } | |
1991 | ||
1992 | ||
794dd120 SH |
1993 | static bool lxcapi_set_cgroup_item(struct lxc_container *c, const char *subsys, const char *value) |
1994 | { | |
1995 | int ret; | |
794dd120 SH |
1996 | |
1997 | if (!c) | |
1998 | return false; | |
1999 | ||
3bc449ed | 2000 | if (is_stopped(c)) |
794dd120 SH |
2001 | return false; |
2002 | ||
3bc449ed SH |
2003 | if (container_disk_lock(c)) |
2004 | return false; | |
794dd120 | 2005 | |
33ad9f1a | 2006 | ret = lxc_cgroup_set(subsys, value, c->name, c->config_path); |
3bc449ed SH |
2007 | |
2008 | container_disk_unlock(c); | |
2009 | return ret == 0; | |
794dd120 SH |
2010 | } |
2011 | ||
2012 | static int lxcapi_get_cgroup_item(struct lxc_container *c, const char *subsys, char *retv, int inlen) | |
2013 | { | |
3bc449ed | 2014 | int ret; |
794dd120 | 2015 | |
6502006a | 2016 | if (!c) |
794dd120 SH |
2017 | return -1; |
2018 | ||
3bc449ed | 2019 | if (is_stopped(c)) |
794dd120 SH |
2020 | return -1; |
2021 | ||
3bc449ed SH |
2022 | if (container_disk_lock(c)) |
2023 | return -1; | |
794dd120 | 2024 | |
33ad9f1a | 2025 | ret = lxc_cgroup_get(subsys, retv, inlen, c->name, c->config_path); |
794dd120 | 2026 | |
3bc449ed | 2027 | container_disk_unlock(c); |
794dd120 SH |
2028 | return ret; |
2029 | } | |
2030 | ||
593e8478 | 2031 | const char *lxc_get_global_config_item(const char *key) |
83c98d82 | 2032 | { |
593e8478 | 2033 | return lxc_global_config_value(key); |
a8428dfa SH |
2034 | } |
2035 | ||
b6b918a1 SG |
2036 | const char *lxc_get_version(void) |
2037 | { | |
95ee490b | 2038 | return LXC_VERSION; |
b6b918a1 SG |
2039 | } |
2040 | ||
f0ca2726 | 2041 | static int copy_file(const char *old, const char *new) |
9be53773 SH |
2042 | { |
2043 | int in, out; | |
2044 | ssize_t len, ret; | |
2045 | char buf[8096]; | |
2046 | struct stat sbuf; | |
2047 | ||
2048 | if (file_exists(new)) { | |
2049 | ERROR("copy destination %s exists", new); | |
2050 | return -1; | |
2051 | } | |
2052 | ret = stat(old, &sbuf); | |
2053 | if (ret < 0) { | |
dfb31b25 | 2054 | INFO("Error stat'ing %s", old); |
9be53773 SH |
2055 | return -1; |
2056 | } | |
2057 | ||
2058 | in = open(old, O_RDONLY); | |
2059 | if (in < 0) { | |
dfb31b25 | 2060 | SYSERROR("Error opening original file %s", old); |
9be53773 SH |
2061 | return -1; |
2062 | } | |
2063 | out = open(new, O_CREAT | O_EXCL | O_WRONLY, 0644); | |
2064 | if (out < 0) { | |
dfb31b25 | 2065 | SYSERROR("Error opening new file %s", new); |
9be53773 SH |
2066 | close(in); |
2067 | return -1; | |
2068 | } | |
2069 | ||
2070 | while (1) { | |
2071 | len = read(in, buf, 8096); | |
2072 | if (len < 0) { | |
dfb31b25 | 2073 | SYSERROR("Error reading old file %s", old); |
9be53773 SH |
2074 | goto err; |
2075 | } | |
2076 | if (len == 0) | |
2077 | break; | |
2078 | ret = write(out, buf, len); | |
2079 | if (ret < len) { // should we retry? | |
dfb31b25 | 2080 | SYSERROR("Error: write to new file %s was interrupted", new); |
9be53773 SH |
2081 | goto err; |
2082 | } | |
2083 | } | |
2084 | close(in); | |
2085 | close(out); | |
2086 | ||
2087 | // we set mode, but not owner/group | |
2088 | ret = chmod(new, sbuf.st_mode); | |
2089 | if (ret) { | |
dfb31b25 | 2090 | SYSERROR("Error setting mode on %s", new); |
9be53773 SH |
2091 | return -1; |
2092 | } | |
2093 | ||
2094 | return 0; | |
2095 | ||
2096 | err: | |
2097 | close(in); | |
2098 | close(out); | |
2099 | return -1; | |
2100 | } | |
2101 | ||
9be53773 SH |
2102 | static int copyhooks(struct lxc_container *oldc, struct lxc_container *c) |
2103 | { | |
619256b5 | 2104 | int i, len, ret; |
9be53773 | 2105 | struct lxc_list *it; |
619256b5 ÇO |
2106 | char *cpath; |
2107 | ||
2108 | len = strlen(oldc->config_path) + strlen(oldc->name) + 3; | |
2109 | cpath = alloca(len); | |
2110 | ret = snprintf(cpath, len, "%s/%s/", oldc->config_path, oldc->name); | |
2111 | if (ret < 0 || ret >= len) | |
2112 | return -1; | |
9be53773 SH |
2113 | |
2114 | for (i=0; i<NUM_LXC_HOOKS; i++) { | |
2115 | lxc_list_for_each(it, &c->lxc_conf->hooks[i]) { | |
2116 | char *hookname = it->elem; | |
c32981c3 | 2117 | char *fname = strrchr(hookname, '/'); |
9be53773 SH |
2118 | char tmppath[MAXPATHLEN]; |
2119 | if (!fname) // relative path - we don't support, but maybe we should | |
2120 | return 0; | |
619256b5 ÇO |
2121 | if (strncmp(hookname, cpath, len - 1) != 0) { |
2122 | // this hook is public - ignore | |
2123 | continue; | |
2124 | } | |
9be53773 SH |
2125 | // copy the script, and change the entry in confile |
2126 | ret = snprintf(tmppath, MAXPATHLEN, "%s/%s/%s", | |
2127 | c->config_path, c->name, fname+1); | |
2128 | if (ret < 0 || ret >= MAXPATHLEN) | |
2129 | return -1; | |
2130 | ret = copy_file(it->elem, tmppath); | |
2131 | if (ret < 0) | |
2132 | return -1; | |
2133 | free(it->elem); | |
2134 | it->elem = strdup(tmppath); | |
2135 | if (!it->elem) { | |
2136 | ERROR("out of memory copying hook path"); | |
2137 | return -1; | |
2138 | } | |
9be53773 SH |
2139 | } |
2140 | } | |
2141 | ||
2142 | c->save_config(c, NULL); | |
2143 | return 0; | |
2144 | } | |
2145 | ||
2146 | static void new_hwaddr(char *hwaddr) | |
2147 | { | |
025ed0f3 | 2148 | FILE *f; |
025ed0f3 | 2149 | f = fopen("/dev/urandom", "r"); |
9be53773 SH |
2150 | if (f) { |
2151 | unsigned int seed; | |
2152 | int ret = fread(&seed, sizeof(seed), 1, f); | |
2153 | if (ret != 1) | |
2154 | seed = time(NULL); | |
2155 | fclose(f); | |
2156 | srand(seed); | |
2157 | } else | |
2158 | srand(time(NULL)); | |
2159 | snprintf(hwaddr, 18, "00:16:3e:%02x:%02x:%02x", | |
2160 | rand() % 255, rand() % 255, rand() % 255); | |
2161 | } | |
2162 | ||
2163 | static void network_new_hwaddrs(struct lxc_container *c) | |
2164 | { | |
2165 | struct lxc_list *it; | |
2166 | ||
2167 | lxc_list_for_each(it, &c->lxc_conf->network) { | |
2168 | struct lxc_netdev *n = it->elem; | |
2169 | if (n->hwaddr) | |
2170 | new_hwaddr(n->hwaddr); | |
2171 | } | |
2172 | } | |
2173 | ||
2174 | static int copy_fstab(struct lxc_container *oldc, struct lxc_container *c) | |
2175 | { | |
2176 | char newpath[MAXPATHLEN]; | |
2177 | char *oldpath = oldc->lxc_conf->fstab; | |
2178 | int ret; | |
2179 | ||
2180 | if (!oldpath) | |
2181 | return 0; | |
2182 | ||
c32981c3 | 2183 | char *p = strrchr(oldpath, '/'); |
9be53773 SH |
2184 | if (!p) |
2185 | return -1; | |
2186 | ret = snprintf(newpath, MAXPATHLEN, "%s/%s%s", | |
2187 | c->config_path, c->name, p); | |
2188 | if (ret < 0 || ret >= MAXPATHLEN) { | |
2189 | ERROR("error printing new path for %s", oldpath); | |
2190 | return -1; | |
2191 | } | |
2192 | if (file_exists(newpath)) { | |
2193 | ERROR("error: fstab file %s exists", newpath); | |
2194 | return -1; | |
2195 | } | |
2196 | ||
2197 | if (copy_file(oldpath, newpath) < 0) { | |
2198 | ERROR("error: copying %s to %s", oldpath, newpath); | |
2199 | return -1; | |
2200 | } | |
2201 | free(c->lxc_conf->fstab); | |
2202 | c->lxc_conf->fstab = strdup(newpath); | |
2203 | if (!c->lxc_conf->fstab) { | |
2204 | ERROR("error: allocating pathname"); | |
2205 | return -1; | |
2206 | } | |
2207 | ||
2208 | return 0; | |
2209 | } | |
2210 | ||
dfb31b25 SH |
2211 | static void copy_rdepends(struct lxc_container *c, struct lxc_container *c0) |
2212 | { | |
2213 | char path0[MAXPATHLEN], path1[MAXPATHLEN]; | |
2214 | int ret; | |
2215 | ||
2216 | ret = snprintf(path0, MAXPATHLEN, "%s/%s/lxc_rdepends", c0->config_path, | |
2217 | c0->name); | |
2218 | if (ret < 0 || ret >= MAXPATHLEN) { | |
2219 | WARN("Error copying reverse dependencies"); | |
2220 | return; | |
2221 | } | |
2222 | ret = snprintf(path1, MAXPATHLEN, "%s/%s/lxc_rdepends", c->config_path, | |
2223 | c->name); | |
2224 | if (ret < 0 || ret >= MAXPATHLEN) { | |
2225 | WARN("Error copying reverse dependencies"); | |
2226 | return; | |
2227 | } | |
2228 | if (copy_file(path0, path1) < 0) { | |
2229 | INFO("Error copying reverse dependencies"); | |
2230 | return; | |
2231 | } | |
2232 | } | |
2233 | ||
2234 | static bool add_rdepends(struct lxc_container *c, struct lxc_container *c0) | |
2235 | { | |
2236 | int ret; | |
2237 | char path[MAXPATHLEN]; | |
2238 | FILE *f; | |
2239 | bool bret; | |
2240 | ||
2241 | ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_rdepends", c->config_path, | |
2242 | c->name); | |
2243 | if (ret < 0 || ret >= MAXPATHLEN) | |
2244 | return false; | |
2245 | f = fopen(path, "a"); | |
2246 | if (!f) | |
2247 | return false; | |
2248 | bret = true; | |
2249 | // if anything goes wrong, just return an error | |
2250 | if (fprintf(f, "%s\n%s\n", c0->config_path, c0->name) < 0) | |
2251 | bret = false; | |
2252 | if (fclose(f) != 0) | |
2253 | bret = false; | |
2254 | return bret; | |
2255 | } | |
2256 | ||
9be53773 | 2257 | static int copy_storage(struct lxc_container *c0, struct lxc_container *c, |
d659597e | 2258 | const char *newtype, int flags, const char *bdevdata, uint64_t newsize) |
9be53773 SH |
2259 | { |
2260 | struct bdev *bdev; | |
dfb31b25 | 2261 | int need_rdep; |
9be53773 SH |
2262 | |
2263 | bdev = bdev_copy(c0->lxc_conf->rootfs.path, c0->name, c->name, | |
0a83cbbb | 2264 | c0->config_path, c->config_path, newtype, flags, |
dfb31b25 | 2265 | bdevdata, newsize, &need_rdep); |
9be53773 | 2266 | if (!bdev) { |
dfb31b25 | 2267 | ERROR("Error copying storage"); |
9be53773 SH |
2268 | return -1; |
2269 | } | |
2270 | free(c->lxc_conf->rootfs.path); | |
2271 | c->lxc_conf->rootfs.path = strdup(bdev->src); | |
2272 | bdev_put(bdev); | |
dfb31b25 SH |
2273 | if (!c->lxc_conf->rootfs.path) { |
2274 | ERROR("Out of memory while setting storage path"); | |
9be53773 | 2275 | return -1; |
dfb31b25 | 2276 | } |
eee59f94 SH |
2277 | if (flags & LXC_CLONE_SNAPSHOT) |
2278 | copy_rdepends(c, c0); | |
dfb31b25 SH |
2279 | if (need_rdep) { |
2280 | if (!add_rdepends(c, c0)) | |
2281 | WARN("Error adding reverse dependency from %s to %s", | |
2282 | c->name, c0->name); | |
2283 | } | |
2284 | ||
2285 | mod_all_rdeps(c, true); | |
2286 | ||
9be53773 SH |
2287 | return 0; |
2288 | } | |
2289 | ||
1143ed39 DE |
2290 | static int clone_update_rootfs(struct lxc_container *c0, |
2291 | struct lxc_container *c, int flags, | |
2292 | char **hookargs) | |
9be53773 SH |
2293 | { |
2294 | int ret = -1; | |
2295 | char path[MAXPATHLEN]; | |
2296 | struct bdev *bdev; | |
2297 | FILE *fout; | |
2298 | pid_t pid; | |
148e91f5 | 2299 | struct lxc_conf *conf = c->lxc_conf; |
9be53773 SH |
2300 | |
2301 | /* update hostname in rootfs */ | |
2302 | /* we're going to mount, so run in a clean namespace to simplify cleanup */ | |
2303 | ||
2304 | pid = fork(); | |
2305 | if (pid < 0) | |
2306 | return -1; | |
2307 | if (pid > 0) | |
2308 | return wait_for_pid(pid); | |
2309 | ||
9be53773 SH |
2310 | bdev = bdev_init(c->lxc_conf->rootfs.path, c->lxc_conf->rootfs.mount, NULL); |
2311 | if (!bdev) | |
2312 | exit(1); | |
cf3ef16d SH |
2313 | if (strcmp(bdev->type, "dir") != 0) { |
2314 | if (unshare(CLONE_NEWNS) < 0) { | |
2315 | ERROR("error unsharing mounts"); | |
2316 | exit(1); | |
2317 | } | |
2318 | if (bdev->ops->mount(bdev) < 0) | |
2319 | exit(1); | |
2320 | } else { // TODO come up with a better way | |
2321 | if (bdev->dest) | |
2322 | free(bdev->dest); | |
2323 | bdev->dest = strdup(bdev->src); | |
2324 | } | |
148e91f5 SH |
2325 | |
2326 | if (!lxc_list_empty(&conf->hooks[LXCHOOK_CLONE])) { | |
2327 | /* Start of environment variable setup for hooks */ | |
1143ed39 DE |
2328 | if (setenv("LXC_SRC_NAME", c0->name, 1)) { |
2329 | SYSERROR("failed to set environment variable for source container name"); | |
2330 | } | |
148e91f5 SH |
2331 | if (setenv("LXC_NAME", c->name, 1)) { |
2332 | SYSERROR("failed to set environment variable for container name"); | |
2333 | } | |
2334 | if (setenv("LXC_CONFIG_FILE", conf->rcfile, 1)) { | |
2335 | SYSERROR("failed to set environment variable for config path"); | |
2336 | } | |
24ef39f4 | 2337 | if (setenv("LXC_ROOTFS_MOUNT", bdev->dest, 1)) { |
148e91f5 SH |
2338 | SYSERROR("failed to set environment variable for rootfs mount"); |
2339 | } | |
2340 | if (setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1)) { | |
2341 | SYSERROR("failed to set environment variable for rootfs mount"); | |
2342 | } | |
2343 | ||
283678ed | 2344 | if (run_lxc_hooks(c->name, "clone", conf, c->get_config_path(c), hookargs)) { |
148e91f5 SH |
2345 | ERROR("Error executing clone hook for %s", c->name); |
2346 | exit(1); | |
2347 | } | |
2348 | } | |
2349 | ||
2350 | if (!(flags & LXC_CLONE_KEEPNAME)) { | |
2351 | ret = snprintf(path, MAXPATHLEN, "%s/etc/hostname", bdev->dest); | |
2352 | if (ret < 0 || ret >= MAXPATHLEN) | |
2353 | exit(1); | |
8058be39 DE |
2354 | if (!file_exists(path)) |
2355 | exit(0); | |
148e91f5 SH |
2356 | if (!(fout = fopen(path, "w"))) { |
2357 | SYSERROR("unable to open %s: ignoring\n", path); | |
2358 | exit(0); | |
2359 | } | |
2360 | if (fprintf(fout, "%s", c->name) < 0) | |
2361 | exit(1); | |
2362 | if (fclose(fout) < 0) | |
2363 | exit(1); | |
9be53773 | 2364 | } |
9be53773 SH |
2365 | exit(0); |
2366 | } | |
2367 | ||
2368 | /* | |
2369 | * We want to support: | |
2370 | sudo lxc-clone -o o1 -n n1 -s -L|-fssize fssize -v|--vgname vgname \ | |
2371 | -p|--lvprefix lvprefix -t|--fstype fstype -B backingstore | |
2372 | ||
2373 | -s [ implies overlayfs] | |
2374 | -s -B overlayfs | |
2375 | -s -B aufs | |
2376 | ||
2377 | only rootfs gets converted (copied/snapshotted) on clone. | |
2378 | */ | |
2379 | ||
2380 | static int create_file_dirname(char *path) | |
2381 | { | |
c32981c3 | 2382 | char *p = strrchr(path, '/'); |
9be53773 SH |
2383 | int ret; |
2384 | ||
2385 | if (!p) | |
2386 | return -1; | |
2387 | *p = '\0'; | |
2388 | ret = mkdir(path, 0755); | |
2389 | if (ret && errno != EEXIST) | |
2390 | SYSERROR("creating container path %s\n", path); | |
2391 | *p = '/'; | |
2392 | return ret; | |
2393 | } | |
2394 | ||
74a3920a | 2395 | static struct lxc_container *lxcapi_clone(struct lxc_container *c, const char *newname, |
9be53773 | 2396 | const char *lxcpath, int flags, |
d659597e | 2397 | const char *bdevtype, const char *bdevdata, uint64_t newsize, |
148e91f5 | 2398 | char **hookargs) |
9be53773 SH |
2399 | { |
2400 | struct lxc_container *c2 = NULL; | |
2401 | char newpath[MAXPATHLEN]; | |
176d9acb | 2402 | int ret, storage_copied = 0; |
9be53773 SH |
2403 | const char *n, *l; |
2404 | FILE *fout; | |
2405 | ||
2406 | if (!c || !c->is_defined(c)) | |
2407 | return NULL; | |
2408 | ||
5cee8c50 | 2409 | if (container_mem_lock(c)) |
9be53773 SH |
2410 | return NULL; |
2411 | ||
39dc698c | 2412 | if (!is_stopped(c)) { |
9be53773 SH |
2413 | ERROR("error: Original container (%s) is running", c->name); |
2414 | goto out; | |
2415 | } | |
2416 | ||
2417 | // Make sure the container doesn't yet exist. | |
2418 | n = newname ? newname : c->name; | |
2419 | l = lxcpath ? lxcpath : c->get_config_path(c); | |
2420 | ret = snprintf(newpath, MAXPATHLEN, "%s/%s/config", l, n); | |
2421 | if (ret < 0 || ret >= MAXPATHLEN) { | |
2422 | SYSERROR("clone: failed making config pathname"); | |
2423 | goto out; | |
2424 | } | |
2425 | if (file_exists(newpath)) { | |
2426 | ERROR("error: clone: %s exists", newpath); | |
2427 | goto out; | |
2428 | } | |
2429 | ||
96532523 SH |
2430 | ret = create_file_dirname(newpath); |
2431 | if (ret < 0 && errno != EEXIST) { | |
9be53773 SH |
2432 | ERROR("Error creating container dir for %s", newpath); |
2433 | goto out; | |
2434 | } | |
2435 | ||
2436 | // copy the configuration, tweak it as needed, | |
2437 | fout = fopen(newpath, "w"); | |
2438 | if (!fout) { | |
2439 | SYSERROR("open %s", newpath); | |
2440 | goto out; | |
2441 | } | |
2442 | write_config(fout, c->lxc_conf); | |
2443 | fclose(fout); | |
2444 | ||
9be53773 SH |
2445 | sprintf(newpath, "%s/%s/rootfs", l, n); |
2446 | if (mkdir(newpath, 0755) < 0) { | |
2447 | SYSERROR("error creating %s", newpath); | |
2448 | goto out; | |
2449 | } | |
2450 | ||
2451 | c2 = lxc_container_new(n, l); | |
375c2258 | 2452 | if (!c2) { |
9be53773 SH |
2453 | ERROR("clone: failed to create new container (%s %s)", n, l); |
2454 | goto out; | |
2455 | } | |
2456 | ||
96532523 SH |
2457 | // update utsname |
2458 | if (!set_config_item_locked(c2, "lxc.utsname", newname)) { | |
2459 | ERROR("Error setting new hostname"); | |
2460 | goto out; | |
2461 | } | |
2462 | ||
619256b5 ÇO |
2463 | // copy hooks |
2464 | ret = copyhooks(c, c2); | |
2465 | if (ret < 0) { | |
2466 | ERROR("error copying hooks"); | |
2467 | goto out; | |
9be53773 SH |
2468 | } |
2469 | ||
2470 | if (copy_fstab(c, c2) < 0) { | |
2471 | ERROR("error copying fstab"); | |
9be53773 SH |
2472 | goto out; |
2473 | } | |
2474 | ||
2475 | // update macaddrs | |
2476 | if (!(flags & LXC_CLONE_KEEPMACADDR)) | |
2477 | network_new_hwaddrs(c2); | |
2478 | ||
2479 | // copy/snapshot rootfs's | |
2480 | ret = copy_storage(c, c2, bdevtype, flags, bdevdata, newsize); | |
375c2258 | 2481 | if (ret < 0) |
9be53773 | 2482 | goto out; |
9be53773 | 2483 | |
176d9acb SH |
2484 | // We've now successfully created c2's storage, so clear it out if we |
2485 | // fail after this | |
2486 | storage_copied = 1; | |
2487 | ||
375c2258 | 2488 | if (!c2->save_config(c2, NULL)) |
9be53773 | 2489 | goto out; |
9be53773 | 2490 | |
1143ed39 | 2491 | if (clone_update_rootfs(c, c2, flags, hookargs) < 0) |
9be53773 | 2492 | goto out; |
9be53773 SH |
2493 | |
2494 | // TODO: update c's lxc.snapshot = count | |
5cee8c50 | 2495 | container_mem_unlock(c); |
9be53773 SH |
2496 | return c2; |
2497 | ||
2498 | out: | |
5cee8c50 | 2499 | container_mem_unlock(c); |
375c2258 | 2500 | if (c2) { |
176d9acb SH |
2501 | if (!storage_copied) |
2502 | c2->lxc_conf->rootfs.path = NULL; | |
375c2258 | 2503 | c2->destroy(c2); |
9be53773 | 2504 | lxc_container_put(c2); |
375c2258 | 2505 | } |
9be53773 SH |
2506 | |
2507 | return NULL; | |
2508 | } | |
2509 | ||
06e5650e ÇO |
2510 | static bool lxcapi_rename(struct lxc_container *c, const char *newname) |
2511 | { | |
2512 | struct bdev *bdev; | |
2513 | struct lxc_container *newc; | |
06e5650e ÇO |
2514 | |
2515 | if (!c || !c->name || !c->config_path) | |
2516 | return false; | |
2517 | ||
2518 | bdev = bdev_init(c->lxc_conf->rootfs.path, c->lxc_conf->rootfs.mount, NULL); | |
2519 | if (!bdev) { | |
2520 | ERROR("Failed to find original backing store type"); | |
2521 | return false; | |
2522 | } | |
2523 | ||
619256b5 | 2524 | newc = lxcapi_clone(c, newname, c->config_path, LXC_CLONE_KEEPMACADDR, NULL, bdev->type, 0, NULL); |
06e5650e ÇO |
2525 | bdev_put(bdev); |
2526 | if (!newc) { | |
2527 | lxc_container_put(newc); | |
2528 | return false; | |
2529 | } | |
2530 | ||
2531 | if (newc && lxcapi_is_defined(newc)) | |
2532 | lxc_container_put(newc); | |
2533 | ||
2534 | if (!lxcapi_destroy(c)) { | |
2535 | ERROR("Could not destroy existing container %s", c->name); | |
2536 | return false; | |
2537 | } | |
2538 | return true; | |
2539 | } | |
2540 | ||
a0e93eeb CS |
2541 | 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) |
2542 | { | |
2543 | if (!c) | |
2544 | return -1; | |
2545 | ||
2546 | return lxc_attach(c->name, c->config_path, exec_function, exec_payload, options, attached_process); | |
2547 | } | |
2548 | ||
2549 | static int lxcapi_attach_run_wait(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char * const argv[]) | |
2550 | { | |
2551 | lxc_attach_command_t command; | |
2552 | pid_t pid; | |
2553 | int r; | |
2554 | ||
2555 | if (!c) | |
2556 | return -1; | |
2557 | ||
2558 | command.program = (char*)program; | |
2559 | command.argv = (char**)argv; | |
2560 | r = lxc_attach(c->name, c->config_path, lxc_attach_run_command, &command, options, &pid); | |
2561 | if (r < 0) { | |
2562 | ERROR("ups"); | |
2563 | return r; | |
2564 | } | |
2565 | return lxc_wait_for_pid_status(pid); | |
2566 | } | |
2567 | ||
74a3920a | 2568 | static int get_next_index(const char *lxcpath, char *cname) |
f5dd1d53 SH |
2569 | { |
2570 | char *fname; | |
2571 | struct stat sb; | |
2572 | int i = 0, ret; | |
2573 | ||
2574 | fname = alloca(strlen(lxcpath) + 20); | |
2575 | while (1) { | |
2576 | sprintf(fname, "%s/snap%d", lxcpath, i); | |
2577 | ret = stat(fname, &sb); | |
2578 | if (ret != 0) | |
2579 | return i; | |
2580 | i++; | |
2581 | } | |
2582 | } | |
2583 | ||
f0ca2726 | 2584 | static int lxcapi_snapshot(struct lxc_container *c, const char *commentfile) |
f5dd1d53 SH |
2585 | { |
2586 | int i, flags, ret; | |
2587 | struct lxc_container *c2; | |
2588 | char snappath[MAXPATHLEN], newname[20]; | |
2589 | ||
2590 | // /var/lib/lxc -> /var/lib/lxcsnaps \0 | |
2591 | ret = snprintf(snappath, MAXPATHLEN, "%ssnaps/%s", c->config_path, c->name); | |
2592 | if (ret < 0 || ret >= MAXPATHLEN) | |
2593 | return -1; | |
2594 | i = get_next_index(snappath, c->name); | |
2595 | ||
2596 | if (mkdir_p(snappath, 0755) < 0) { | |
2597 | ERROR("Failed to create snapshot directory %s", snappath); | |
2598 | return -1; | |
2599 | } | |
2600 | ||
2601 | ret = snprintf(newname, 20, "snap%d", i); | |
2602 | if (ret < 0 || ret >= 20) | |
2603 | return -1; | |
2604 | ||
0a83cbbb SH |
2605 | /* |
2606 | * We pass LXC_CLONE_SNAPSHOT to make sure that a rdepends file entry is | |
2607 | * created in the original container | |
2608 | */ | |
2609 | flags = LXC_CLONE_SNAPSHOT | LXC_CLONE_KEEPMACADDR | LXC_CLONE_KEEPNAME | | |
2610 | LXC_CLONE_KEEPBDEVTYPE | LXC_CLONE_MAYBE_SNAPSHOT; | |
f5dd1d53 SH |
2611 | c2 = c->clone(c, newname, snappath, flags, NULL, NULL, 0, NULL); |
2612 | if (!c2) { | |
2613 | ERROR("clone of %s:%s failed\n", c->config_path, c->name); | |
2614 | return -1; | |
2615 | } | |
2616 | ||
2617 | lxc_container_put(c2); | |
2618 | ||
2619 | // Now write down the creation time | |
2620 | time_t timer; | |
2621 | char buffer[25]; | |
2622 | struct tm* tm_info; | |
025ed0f3 | 2623 | FILE *f; |
f5dd1d53 SH |
2624 | |
2625 | time(&timer); | |
2626 | tm_info = localtime(&timer); | |
2627 | ||
2628 | strftime(buffer, 25, "%Y:%m:%d %H:%M:%S", tm_info); | |
2629 | ||
2630 | char *dfnam = alloca(strlen(snappath) + strlen(newname) + 5); | |
2631 | sprintf(dfnam, "%s/%s/ts", snappath, newname); | |
025ed0f3 | 2632 | f = fopen(dfnam, "w"); |
f5dd1d53 SH |
2633 | if (!f) { |
2634 | ERROR("Failed to open %s\n", dfnam); | |
2635 | return -1; | |
2636 | } | |
2637 | if (fprintf(f, "%s", buffer) < 0) { | |
2638 | SYSERROR("Writing timestamp"); | |
2639 | fclose(f); | |
2640 | return -1; | |
2641 | } | |
025ed0f3 | 2642 | ret = fclose(f); |
025ed0f3 | 2643 | if (ret != 0) { |
f5dd1d53 SH |
2644 | SYSERROR("Writing timestamp"); |
2645 | return -1; | |
2646 | } | |
2647 | ||
2648 | if (commentfile) { | |
2649 | // $p / $name / comment \0 | |
2650 | int len = strlen(snappath) + strlen(newname) + 10; | |
2651 | char *path = alloca(len); | |
2652 | sprintf(path, "%s/%s/comment", snappath, newname); | |
2653 | return copy_file(commentfile, path) < 0 ? -1 : i; | |
2654 | } | |
2655 | ||
2656 | return i; | |
2657 | } | |
2658 | ||
2659 | static void lxcsnap_free(struct lxc_snapshot *s) | |
2660 | { | |
2661 | if (s->name) | |
2662 | free(s->name); | |
2663 | if (s->comment_pathname) | |
2664 | free(s->comment_pathname); | |
2665 | if (s->timestamp) | |
2666 | free(s->timestamp); | |
2667 | if (s->lxcpath) | |
2668 | free(s->lxcpath); | |
2669 | } | |
2670 | ||
2671 | static char *get_snapcomment_path(char* snappath, char *name) | |
2672 | { | |
2673 | // $snappath/$name/comment | |
2674 | int ret, len = strlen(snappath) + strlen(name) + 10; | |
2675 | char *s = malloc(len); | |
2676 | ||
2677 | if (s) { | |
2678 | ret = snprintf(s, len, "%s/%s/comment", snappath, name); | |
2679 | if (ret < 0 || ret >= len) { | |
2680 | free(s); | |
2681 | s = NULL; | |
2682 | } | |
2683 | } | |
2684 | return s; | |
2685 | } | |
2686 | ||
2687 | static char *get_timestamp(char* snappath, char *name) | |
2688 | { | |
2689 | char path[MAXPATHLEN], *s = NULL; | |
2690 | int ret, len; | |
2691 | FILE *fin; | |
2692 | ||
2693 | ret = snprintf(path, MAXPATHLEN, "%s/%s/ts", snappath, name); | |
2694 | if (ret < 0 || ret >= MAXPATHLEN) | |
2695 | return NULL; | |
025ed0f3 | 2696 | fin = fopen(path, "r"); |
025ed0f3 | 2697 | if (!fin) |
f5dd1d53 SH |
2698 | return NULL; |
2699 | (void) fseek(fin, 0, SEEK_END); | |
2700 | len = ftell(fin); | |
2701 | (void) fseek(fin, 0, SEEK_SET); | |
2702 | if (len > 0) { | |
2703 | s = malloc(len+1); | |
2704 | if (s) { | |
2705 | s[len] = '\0'; | |
2706 | if (fread(s, 1, len, fin) != len) { | |
2707 | SYSERROR("reading timestamp"); | |
2708 | free(s); | |
2709 | s = NULL; | |
2710 | } | |
2711 | } | |
2712 | } | |
2713 | fclose(fin); | |
2714 | return s; | |
2715 | } | |
2716 | ||
2717 | static int lxcapi_snapshot_list(struct lxc_container *c, struct lxc_snapshot **ret_snaps) | |
2718 | { | |
2719 | char snappath[MAXPATHLEN], path2[MAXPATHLEN]; | |
2720 | int dirlen, count = 0, ret; | |
2721 | struct dirent dirent, *direntp; | |
2722 | struct lxc_snapshot *snaps =NULL, *nsnaps; | |
2723 | DIR *dir; | |
2724 | ||
2725 | if (!c || !lxcapi_is_defined(c)) | |
2726 | return -1; | |
2727 | // snappath is ${lxcpath}snaps/${lxcname}/ | |
2728 | dirlen = snprintf(snappath, MAXPATHLEN, "%ssnaps/%s", c->config_path, c->name); | |
2729 | if (dirlen < 0 || dirlen >= MAXPATHLEN) { | |
2730 | ERROR("path name too long"); | |
2731 | return -1; | |
2732 | } | |
025ed0f3 | 2733 | dir = opendir(snappath); |
025ed0f3 | 2734 | if (!dir) { |
f5dd1d53 SH |
2735 | INFO("failed to open %s - assuming no snapshots", snappath); |
2736 | return 0; | |
2737 | } | |
2738 | ||
2739 | while (!readdir_r(dir, &dirent, &direntp)) { | |
2740 | if (!direntp) | |
2741 | break; | |
2742 | ||
2743 | if (!strcmp(direntp->d_name, ".")) | |
2744 | continue; | |
2745 | ||
2746 | if (!strcmp(direntp->d_name, "..")) | |
2747 | continue; | |
2748 | ||
2749 | ret = snprintf(path2, MAXPATHLEN, "%s/%s/config", snappath, direntp->d_name); | |
2750 | if (ret < 0 || ret >= MAXPATHLEN) { | |
2751 | ERROR("pathname too long"); | |
2752 | goto out_free; | |
2753 | } | |
2754 | if (!file_exists(path2)) | |
2755 | continue; | |
2756 | nsnaps = realloc(snaps, (count + 1)*sizeof(*snaps)); | |
2757 | if (!nsnaps) { | |
2758 | SYSERROR("Out of memory"); | |
2759 | goto out_free; | |
2760 | } | |
2761 | snaps = nsnaps; | |
2762 | snaps[count].free = lxcsnap_free; | |
2763 | snaps[count].name = strdup(direntp->d_name); | |
2764 | if (!snaps[count].name) | |
2765 | goto out_free; | |
2766 | snaps[count].lxcpath = strdup(snappath); | |
2767 | if (!snaps[count].lxcpath) { | |
2768 | free(snaps[count].name); | |
2769 | goto out_free; | |
2770 | } | |
2771 | snaps[count].comment_pathname = get_snapcomment_path(snappath, direntp->d_name); | |
2772 | snaps[count].timestamp = get_timestamp(snappath, direntp->d_name); | |
2773 | count++; | |
2774 | } | |
2775 | ||
2776 | if (closedir(dir)) | |
2777 | WARN("failed to close directory"); | |
2778 | ||
2779 | *ret_snaps = snaps; | |
2780 | return count; | |
2781 | ||
2782 | out_free: | |
2783 | if (snaps) { | |
2784 | int i; | |
2785 | for (i=0; i<count; i++) | |
2786 | lxcsnap_free(&snaps[i]); | |
2787 | free(snaps); | |
2788 | } | |
9baa57bd SH |
2789 | if (closedir(dir)) |
2790 | WARN("failed to close directory"); | |
f5dd1d53 SH |
2791 | return -1; |
2792 | } | |
2793 | ||
f0ca2726 | 2794 | static bool lxcapi_snapshot_restore(struct lxc_container *c, const char *snapname, const char *newname) |
f5dd1d53 SH |
2795 | { |
2796 | char clonelxcpath[MAXPATHLEN]; | |
2797 | int ret; | |
2798 | struct lxc_container *snap, *rest; | |
2799 | struct bdev *bdev; | |
2800 | bool b = false; | |
2801 | ||
2802 | if (!c || !c->name || !c->config_path) | |
2803 | return false; | |
2804 | ||
2805 | bdev = bdev_init(c->lxc_conf->rootfs.path, c->lxc_conf->rootfs.mount, NULL); | |
2806 | if (!bdev) { | |
2807 | ERROR("Failed to find original backing store type"); | |
2808 | return false; | |
2809 | } | |
2810 | ||
2811 | if (!newname) | |
2812 | newname = c->name; | |
2813 | if (strcmp(c->name, newname) == 0) { | |
2814 | if (!lxcapi_destroy(c)) { | |
2815 | ERROR("Could not destroy existing container %s", newname); | |
2816 | bdev_put(bdev); | |
2817 | return false; | |
2818 | } | |
2819 | } | |
2820 | ret = snprintf(clonelxcpath, MAXPATHLEN, "%ssnaps/%s", c->config_path, c->name); | |
2821 | if (ret < 0 || ret >= MAXPATHLEN) { | |
2822 | bdev_put(bdev); | |
2823 | return false; | |
2824 | } | |
2825 | // how should we lock this? | |
2826 | ||
2827 | snap = lxc_container_new(snapname, clonelxcpath); | |
2828 | if (!snap || !lxcapi_is_defined(snap)) { | |
2829 | ERROR("Could not open snapshot %s", snapname); | |
2830 | if (snap) lxc_container_put(snap); | |
2831 | bdev_put(bdev); | |
2832 | return false; | |
2833 | } | |
2834 | ||
2835 | rest = lxcapi_clone(snap, newname, c->config_path, 0, bdev->type, NULL, 0, NULL); | |
2836 | bdev_put(bdev); | |
2837 | if (rest && lxcapi_is_defined(rest)) | |
2838 | b = true; | |
2839 | if (rest) | |
2840 | lxc_container_put(rest); | |
2841 | lxc_container_put(snap); | |
2842 | return b; | |
2843 | } | |
2844 | ||
f0ca2726 | 2845 | static bool lxcapi_snapshot_destroy(struct lxc_container *c, const char *snapname) |
771d96b3 ÇO |
2846 | { |
2847 | int ret; | |
2848 | char clonelxcpath[MAXPATHLEN]; | |
2849 | struct lxc_container *snap = NULL; | |
2850 | ||
2851 | if (!c || !c->name || !c->config_path) | |
2852 | return false; | |
2853 | ||
2854 | ret = snprintf(clonelxcpath, MAXPATHLEN, "%ssnaps/%s", c->config_path, c->name); | |
2855 | if (ret < 0 || ret >= MAXPATHLEN) | |
2856 | goto err; | |
2857 | ||
2858 | snap = lxc_container_new(snapname, clonelxcpath); | |
2859 | if (!snap || !lxcapi_is_defined(snap)) { | |
2860 | ERROR("Could not find snapshot %s", snapname); | |
2861 | goto err; | |
2862 | } | |
2863 | ||
2864 | if (!lxcapi_destroy(snap)) { | |
2865 | ERROR("Could not destroy snapshot %s", snapname); | |
2866 | goto err; | |
2867 | } | |
2868 | lxc_container_put(snap); | |
2869 | ||
2870 | return true; | |
2871 | err: | |
2872 | if (snap) | |
2873 | lxc_container_put(snap); | |
2874 | return false; | |
2875 | } | |
2876 | ||
b494d2dd SH |
2877 | static bool lxcapi_may_control(struct lxc_container *c) |
2878 | { | |
2879 | return lxc_try_cmd(c->name, c->config_path) == 0; | |
2880 | } | |
2881 | ||
f0ca2726 | 2882 | static bool add_remove_device_node(struct lxc_container *c, const char *src_path, const char *dest_path, bool add) |
a9a0ed90 ÇO |
2883 | { |
2884 | int ret; | |
2885 | struct stat st; | |
2886 | char path[MAXPATHLEN]; | |
2887 | char value[MAX_BUFFER]; | |
f0ca2726 JH |
2888 | char *directory_path = NULL; |
2889 | const char *p; | |
a9a0ed90 ÇO |
2890 | |
2891 | /* make sure container is running */ | |
2892 | if (!c->is_running(c)) { | |
2893 | ERROR("container is not running"); | |
2894 | goto out; | |
2895 | } | |
2896 | ||
2897 | /* use src_path if dest_path is NULL otherwise use dest_path */ | |
2898 | p = dest_path ? dest_path : src_path; | |
2899 | ||
2900 | /* prepare the path */ | |
2901 | ret = snprintf(path, MAXPATHLEN, "/proc/%d/root/%s", c->init_pid(c), p); | |
2902 | if (ret < 0 || ret >= MAXPATHLEN) | |
2903 | goto out; | |
2904 | remove_trailing_slashes(path); | |
2905 | ||
2906 | p = add ? src_path : path; | |
2907 | /* make sure we can access p */ | |
2908 | if(access(p, F_OK) < 0 || stat(p, &st) < 0) | |
2909 | goto out; | |
2910 | ||
2911 | /* continue if path is character device or block device */ | |
c6a9b0d7 | 2912 | if (S_ISCHR(st.st_mode)) |
a9a0ed90 | 2913 | ret = snprintf(value, MAX_BUFFER, "c %d:%d rwm", major(st.st_rdev), minor(st.st_rdev)); |
c6a9b0d7 | 2914 | else if (S_ISBLK(st.st_mode)) |
a9a0ed90 ÇO |
2915 | ret = snprintf(value, MAX_BUFFER, "b %d:%d rwm", major(st.st_rdev), minor(st.st_rdev)); |
2916 | else | |
2917 | goto out; | |
2918 | ||
2919 | /* check snprintf return code */ | |
2920 | if (ret < 0 || ret >= MAX_BUFFER) | |
2921 | goto out; | |
2922 | ||
2923 | directory_path = dirname(strdup(path)); | |
2924 | /* remove path and directory_path (if empty) */ | |
2925 | if(access(path, F_OK) == 0) { | |
2926 | if (unlink(path) < 0) { | |
2927 | ERROR("unlink failed"); | |
2928 | goto out; | |
2929 | } | |
2930 | if (rmdir(directory_path) < 0 && errno != ENOTEMPTY) { | |
2931 | ERROR("rmdir failed"); | |
2932 | goto out; | |
2933 | } | |
2934 | } | |
2935 | ||
2936 | if (add) { | |
2937 | /* create the missing directories */ | |
2938 | if (mkdir_p(directory_path, 0755) < 0) { | |
2939 | ERROR("failed to create directory"); | |
2940 | goto out; | |
2941 | } | |
2942 | ||
2943 | /* create the device node */ | |
2944 | if (mknod(path, st.st_mode, st.st_rdev) < 0) { | |
2945 | ERROR("mknod failed"); | |
2946 | goto out; | |
2947 | } | |
2948 | ||
2949 | /* add device node to device list */ | |
2950 | if (!c->set_cgroup_item(c, "devices.allow", value)) { | |
2951 | ERROR("set_cgroup_item failed while adding the device node"); | |
2952 | goto out; | |
2953 | } | |
2954 | } else { | |
2955 | /* remove device node from device list */ | |
2956 | if (!c->set_cgroup_item(c, "devices.deny", value)) { | |
2957 | ERROR("set_cgroup_item failed while removing the device node"); | |
2958 | goto out; | |
2959 | } | |
2960 | } | |
2961 | return true; | |
2962 | out: | |
2963 | if (directory_path) | |
2964 | free(directory_path); | |
2965 | return false; | |
2966 | } | |
2967 | ||
f0ca2726 | 2968 | static bool lxcapi_add_device_node(struct lxc_container *c, const char *src_path, const char *dest_path) |
a9a0ed90 ÇO |
2969 | { |
2970 | return add_remove_device_node(c, src_path, dest_path, true); | |
2971 | } | |
2972 | ||
f0ca2726 | 2973 | static bool lxcapi_remove_device_node(struct lxc_container *c, const char *src_path, const char *dest_path) |
a9a0ed90 ÇO |
2974 | { |
2975 | return add_remove_device_node(c, src_path, dest_path, false); | |
2976 | } | |
2977 | ||
a0e93eeb CS |
2978 | static int lxcapi_attach_run_waitl(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char *arg, ...) |
2979 | { | |
2980 | va_list ap; | |
2981 | const char **argv; | |
2982 | int ret; | |
2983 | ||
2984 | if (!c) | |
2985 | return -1; | |
2986 | ||
2987 | va_start(ap, arg); | |
2988 | argv = lxc_va_arg_list_to_argv_const(ap, 1); | |
2989 | va_end(ap); | |
2990 | ||
2991 | if (!argv) { | |
2992 | ERROR("Memory allocation error."); | |
2993 | return -1; | |
2994 | } | |
2995 | argv[0] = arg; | |
2996 | ||
2997 | ret = lxcapi_attach_run_wait(c, options, program, (const char * const *)argv); | |
2998 | free((void*)argv); | |
2999 | return ret; | |
3000 | } | |
3001 | ||
afeecbba | 3002 | struct lxc_container *lxc_container_new(const char *name, const char *configpath) |
72d0e1cb SG |
3003 | { |
3004 | struct lxc_container *c; | |
72d0e1cb SG |
3005 | |
3006 | c = malloc(sizeof(*c)); | |
3007 | if (!c) { | |
3008 | fprintf(stderr, "failed to malloc lxc_container\n"); | |
3009 | return NULL; | |
3010 | } | |
3011 | memset(c, 0, sizeof(*c)); | |
3012 | ||
afeecbba SH |
3013 | if (configpath) |
3014 | c->config_path = strdup(configpath); | |
3015 | else | |
593e8478 | 3016 | c->config_path = strdup(lxc_global_config_value("lxc.lxcpath")); |
afeecbba | 3017 | |
2a59a681 SH |
3018 | if (!c->config_path) { |
3019 | fprintf(stderr, "Out of memory"); | |
3020 | goto err; | |
3021 | } | |
3022 | ||
f5dd1d53 | 3023 | remove_trailing_slashes(c->config_path); |
72d0e1cb SG |
3024 | c->name = malloc(strlen(name)+1); |
3025 | if (!c->name) { | |
3026 | fprintf(stderr, "Error allocating lxc_container name\n"); | |
3027 | goto err; | |
3028 | } | |
3029 | strcpy(c->name, name); | |
3030 | ||
3031 | c->numthreads = 1; | |
df271a59 | 3032 | if (!(c->slock = lxc_newlock(c->config_path, name))) { |
72d0e1cb SG |
3033 | fprintf(stderr, "failed to create lock\n"); |
3034 | goto err; | |
3035 | } | |
3036 | ||
df271a59 | 3037 | if (!(c->privlock = lxc_newlock(NULL, NULL))) { |
72d0e1cb SG |
3038 | fprintf(stderr, "failed to alloc privlock\n"); |
3039 | goto err; | |
3040 | } | |
3041 | ||
afeecbba | 3042 | if (!set_config_filename(c)) { |
72d0e1cb SG |
3043 | fprintf(stderr, "Error allocating config file pathname\n"); |
3044 | goto err; | |
3045 | } | |
72d0e1cb SG |
3046 | |
3047 | if (file_exists(c->configfile)) | |
3048 | lxcapi_load_config(c, NULL); | |
3049 | ||
3e625e2d SH |
3050 | if (ongoing_create(c) == 2) { |
3051 | ERROR("Error: %s creation was not completed", c->name); | |
1897e3bc | 3052 | lxcapi_destroy(c); |
4df7f012 | 3053 | lxcapi_clear_config(c); |
3e625e2d | 3054 | } |
c8ad5f46 | 3055 | c->daemonize = true; |
3e625e2d | 3056 | |
72d0e1cb SG |
3057 | // assign the member functions |
3058 | c->is_defined = lxcapi_is_defined; | |
3059 | c->state = lxcapi_state; | |
3060 | c->is_running = lxcapi_is_running; | |
3061 | c->freeze = lxcapi_freeze; | |
3062 | c->unfreeze = lxcapi_unfreeze; | |
0115f8fd | 3063 | c->console = lxcapi_console; |
b5159817 | 3064 | c->console_getfd = lxcapi_console_getfd; |
72d0e1cb SG |
3065 | c->init_pid = lxcapi_init_pid; |
3066 | c->load_config = lxcapi_load_config; | |
3067 | c->want_daemonize = lxcapi_want_daemonize; | |
130a1888 | 3068 | c->want_close_all_fds = lxcapi_want_close_all_fds; |
72d0e1cb SG |
3069 | c->start = lxcapi_start; |
3070 | c->startl = lxcapi_startl; | |
3071 | c->stop = lxcapi_stop; | |
3072 | c->config_file_name = lxcapi_config_file_name; | |
3073 | c->wait = lxcapi_wait; | |
3074 | c->set_config_item = lxcapi_set_config_item; | |
3075 | c->destroy = lxcapi_destroy; | |
06e5650e | 3076 | c->rename = lxcapi_rename; |
72d0e1cb SG |
3077 | c->save_config = lxcapi_save_config; |
3078 | c->get_keys = lxcapi_get_keys; | |
3079 | c->create = lxcapi_create; | |
3080 | c->createl = lxcapi_createl; | |
3081 | c->shutdown = lxcapi_shutdown; | |
3e625e2d | 3082 | c->reboot = lxcapi_reboot; |
4df7f012 | 3083 | c->clear_config = lxcapi_clear_config; |
72d0e1cb SG |
3084 | c->clear_config_item = lxcapi_clear_config_item; |
3085 | c->get_config_item = lxcapi_get_config_item; | |
794dd120 SH |
3086 | c->get_cgroup_item = lxcapi_get_cgroup_item; |
3087 | c->set_cgroup_item = lxcapi_set_cgroup_item; | |
2a59a681 SH |
3088 | c->get_config_path = lxcapi_get_config_path; |
3089 | c->set_config_path = lxcapi_set_config_path; | |
9be53773 | 3090 | c->clone = lxcapi_clone; |
799f29ab | 3091 | c->get_interfaces = lxcapi_get_interfaces; |
9c83a661 | 3092 | c->get_ips = lxcapi_get_ips; |
a0e93eeb CS |
3093 | c->attach = lxcapi_attach; |
3094 | c->attach_run_wait = lxcapi_attach_run_wait; | |
3095 | c->attach_run_waitl = lxcapi_attach_run_waitl; | |
f5dd1d53 SH |
3096 | c->snapshot = lxcapi_snapshot; |
3097 | c->snapshot_list = lxcapi_snapshot_list; | |
3098 | c->snapshot_restore = lxcapi_snapshot_restore; | |
771d96b3 | 3099 | c->snapshot_destroy = lxcapi_snapshot_destroy; |
b494d2dd | 3100 | c->may_control = lxcapi_may_control; |
a9a0ed90 ÇO |
3101 | c->add_device_node = lxcapi_add_device_node; |
3102 | c->remove_device_node = lxcapi_remove_device_node; | |
72d0e1cb SG |
3103 | |
3104 | /* we'll allow the caller to update these later */ | |
ab1bf971 | 3105 | if (lxc_log_init(NULL, "none", NULL, "lxc_container", 0, c->config_path)) { |
72d0e1cb SG |
3106 | fprintf(stderr, "failed to open log\n"); |
3107 | goto err; | |
3108 | } | |
3109 | ||
72d0e1cb SG |
3110 | return c; |
3111 | ||
3112 | err: | |
3113 | lxc_container_free(c); | |
3114 | return NULL; | |
3115 | } | |
3116 | ||
4a7c7daa | 3117 | int lxc_get_wait_states(const char **states) |
72d0e1cb SG |
3118 | { |
3119 | int i; | |
3120 | ||
3121 | if (states) | |
3122 | for (i=0; i<MAX_STATE; i++) | |
3123 | states[i] = lxc_state2str(i); | |
3124 | return MAX_STATE; | |
3125 | } | |
a41f104b | 3126 | |
a41f104b SH |
3127 | /* |
3128 | * These next two could probably be done smarter with reusing a common function | |
3129 | * with different iterators and tests... | |
3130 | */ | |
3131 | int list_defined_containers(const char *lxcpath, char ***names, struct lxc_container ***cret) | |
3132 | { | |
3133 | DIR *dir; | |
3134 | int i, cfound = 0, nfound = 0; | |
3135 | struct dirent dirent, *direntp; | |
3136 | struct lxc_container *c; | |
3137 | ||
3138 | if (!lxcpath) | |
593e8478 | 3139 | lxcpath = lxc_global_config_value("lxc.lxcpath"); |
a41f104b | 3140 | |
a41f104b | 3141 | dir = opendir(lxcpath); |
a41f104b SH |
3142 | if (!dir) { |
3143 | SYSERROR("opendir on lxcpath"); | |
3144 | return -1; | |
3145 | } | |
3146 | ||
3147 | if (cret) | |
3148 | *cret = NULL; | |
3149 | if (names) | |
3150 | *names = NULL; | |
3151 | ||
3152 | while (!readdir_r(dir, &dirent, &direntp)) { | |
3153 | if (!direntp) | |
3154 | break; | |
3155 | if (!strcmp(direntp->d_name, ".")) | |
3156 | continue; | |
3157 | if (!strcmp(direntp->d_name, "..")) | |
3158 | continue; | |
3159 | ||
3160 | if (!config_file_exists(lxcpath, direntp->d_name)) | |
3161 | continue; | |
3162 | ||
3163 | if (names) { | |
9c88ff1f | 3164 | if (!add_to_array(names, direntp->d_name, cfound)) |
a41f104b SH |
3165 | goto free_bad; |
3166 | } | |
3167 | cfound++; | |
3168 | ||
3169 | if (!cret) { | |
3170 | nfound++; | |
3171 | continue; | |
3172 | } | |
3173 | ||
3174 | c = lxc_container_new(direntp->d_name, lxcpath); | |
3175 | if (!c) { | |
3176 | INFO("Container %s:%s has a config but could not be loaded", | |
3177 | lxcpath, direntp->d_name); | |
3178 | if (names) | |
9c88ff1f ÇO |
3179 | if(!remove_from_array(names, direntp->d_name, cfound--)) |
3180 | goto free_bad; | |
a41f104b SH |
3181 | continue; |
3182 | } | |
3183 | if (!lxcapi_is_defined(c)) { | |
3184 | INFO("Container %s:%s has a config but is not defined", | |
3185 | lxcpath, direntp->d_name); | |
3186 | if (names) | |
9c88ff1f ÇO |
3187 | if(!remove_from_array(names, direntp->d_name, cfound--)) |
3188 | goto free_bad; | |
a41f104b SH |
3189 | lxc_container_put(c); |
3190 | continue; | |
3191 | } | |
3192 | ||
2871830a | 3193 | if (!add_to_clist(cret, c, nfound, true)) { |
a41f104b SH |
3194 | lxc_container_put(c); |
3195 | goto free_bad; | |
3196 | } | |
3197 | nfound++; | |
3198 | } | |
3199 | ||
a41f104b | 3200 | closedir(dir); |
a41f104b SH |
3201 | return nfound; |
3202 | ||
3203 | free_bad: | |
3204 | if (names && *names) { | |
3205 | for (i=0; i<cfound; i++) | |
3206 | free((*names)[i]); | |
3207 | free(*names); | |
3208 | } | |
3209 | if (cret && *cret) { | |
3210 | for (i=0; i<nfound; i++) | |
3211 | lxc_container_put((*cret)[i]); | |
3212 | free(*cret); | |
3213 | } | |
a41f104b | 3214 | closedir(dir); |
a41f104b SH |
3215 | return -1; |
3216 | } | |
3217 | ||
148a9d27 DE |
3218 | int list_active_containers(const char *lxcpath, char ***nret, |
3219 | struct lxc_container ***cret) | |
a41f104b | 3220 | { |
148a9d27 | 3221 | int i, ret = -1, cret_cnt = 0, ct_name_cnt = 0; |
a41f104b SH |
3222 | int lxcpath_len; |
3223 | char *line = NULL; | |
148a9d27 | 3224 | char **ct_name = NULL; |
a41f104b SH |
3225 | size_t len = 0; |
3226 | struct lxc_container *c; | |
3227 | ||
3228 | if (!lxcpath) | |
593e8478 | 3229 | lxcpath = lxc_global_config_value("lxc.lxcpath"); |
a41f104b SH |
3230 | lxcpath_len = strlen(lxcpath); |
3231 | ||
3232 | if (cret) | |
3233 | *cret = NULL; | |
148a9d27 DE |
3234 | if (nret) |
3235 | *nret = NULL; | |
a41f104b | 3236 | |
a41f104b | 3237 | FILE *f = fopen("/proc/net/unix", "r"); |
a41f104b SH |
3238 | if (!f) |
3239 | return -1; | |
3240 | ||
3241 | while (getline(&line, &len, f) != -1) { | |
0f8f9c8a | 3242 | char *p = strrchr(line, ' '), *p2; |
a41f104b SH |
3243 | if (!p) |
3244 | continue; | |
3245 | p++; | |
3246 | if (*p != 0x40) | |
3247 | continue; | |
3248 | p++; | |
3249 | if (strncmp(p, lxcpath, lxcpath_len) != 0) | |
3250 | continue; | |
3251 | p += lxcpath_len; | |
3252 | while (*p == '/') | |
3253 | p++; | |
3254 | ||
3255 | // Now p is the start of lxc_name | |
3256 | p2 = index(p, '/'); | |
3257 | if (!p2 || strncmp(p2, "/command", 8) != 0) | |
3258 | continue; | |
3259 | *p2 = '\0'; | |
3260 | ||
148a9d27 | 3261 | if (array_contains(&ct_name, p, ct_name_cnt)) |
9c88ff1f ÇO |
3262 | continue; |
3263 | ||
148a9d27 DE |
3264 | if (!add_to_array(&ct_name, p, ct_name_cnt)) |
3265 | goto free_cret_list; | |
9c88ff1f | 3266 | |
148a9d27 | 3267 | ct_name_cnt++; |
a41f104b | 3268 | |
148a9d27 | 3269 | if (!cret) |
a41f104b | 3270 | continue; |
a41f104b SH |
3271 | |
3272 | c = lxc_container_new(p, lxcpath); | |
3273 | if (!c) { | |
3274 | INFO("Container %s:%s is running but could not be loaded", | |
3275 | lxcpath, p); | |
148a9d27 | 3276 | remove_from_array(&ct_name, p, ct_name_cnt--); |
a41f104b SH |
3277 | continue; |
3278 | } | |
3279 | ||
3280 | /* | |
3281 | * If this is an anonymous container, then is_defined *can* | |
3282 | * return false. So we don't do that check. Count on the | |
3283 | * fact that the command socket exists. | |
3284 | */ | |
3285 | ||
148a9d27 | 3286 | if (!add_to_clist(cret, c, cret_cnt, true)) { |
a41f104b | 3287 | lxc_container_put(c); |
148a9d27 | 3288 | goto free_cret_list; |
a41f104b | 3289 | } |
148a9d27 | 3290 | cret_cnt++; |
a41f104b SH |
3291 | } |
3292 | ||
148a9d27 DE |
3293 | assert(!nret || !cret || cret_cnt == ct_name_cnt); |
3294 | ret = ct_name_cnt; | |
3295 | if (nret) | |
3296 | *nret = ct_name; | |
3297 | else | |
3298 | goto free_ct_name; | |
3299 | goto out; | |
a41f104b | 3300 | |
148a9d27 | 3301 | free_cret_list: |
a41f104b | 3302 | if (cret && *cret) { |
148a9d27 | 3303 | for (i = 0; i < cret_cnt; i++) |
a41f104b SH |
3304 | lxc_container_put((*cret)[i]); |
3305 | free(*cret); | |
3306 | } | |
148a9d27 DE |
3307 | |
3308 | free_ct_name: | |
3309 | if (ct_name) { | |
3310 | for (i = 0; i < ct_name_cnt; i++) | |
3311 | free(ct_name[i]); | |
3312 | free(ct_name); | |
3313 | } | |
3314 | ||
3315 | out: | |
e853a32d ÇO |
3316 | if (line) |
3317 | free(line); | |
3318 | ||
a41f104b | 3319 | fclose(f); |
148a9d27 | 3320 | return ret; |
a41f104b | 3321 | } |
2871830a DE |
3322 | |
3323 | int list_all_containers(const char *lxcpath, char ***nret, | |
3324 | struct lxc_container ***cret) | |
3325 | { | |
3326 | int i, ret, active_cnt, ct_cnt, ct_list_cnt; | |
3327 | char **active_name; | |
3328 | char **ct_name; | |
3329 | struct lxc_container **ct_list = NULL; | |
3330 | ||
3331 | ct_cnt = list_defined_containers(lxcpath, &ct_name, NULL); | |
3332 | if (ct_cnt < 0) | |
3333 | return ct_cnt; | |
3334 | ||
3335 | active_cnt = list_active_containers(lxcpath, &active_name, NULL); | |
3336 | if (active_cnt < 0) { | |
3337 | ret = active_cnt; | |
3338 | goto free_ct_name; | |
3339 | } | |
3340 | ||
3341 | for (i = 0; i < active_cnt; i++) { | |
3342 | if (!array_contains(&ct_name, active_name[i], ct_cnt)) { | |
3343 | if (!add_to_array(&ct_name, active_name[i], ct_cnt)) { | |
3344 | ret = -1; | |
3345 | goto free_active_name; | |
3346 | } | |
3347 | ct_cnt++; | |
3348 | } | |
3349 | free(active_name[i]); | |
3350 | active_name[i] = NULL; | |
3351 | } | |
3352 | free(active_name); | |
3353 | active_name = NULL; | |
3354 | active_cnt = 0; | |
3355 | ||
3356 | for (i = 0, ct_list_cnt = 0; i < ct_cnt && cret; i++) { | |
3357 | struct lxc_container *c; | |
3358 | ||
3359 | c = lxc_container_new(ct_name[i], lxcpath); | |
3360 | if (!c) { | |
3361 | WARN("Container %s:%s could not be loaded", lxcpath, ct_name[i]); | |
3362 | remove_from_array(&ct_name, ct_name[i], ct_cnt--); | |
3363 | continue; | |
3364 | } | |
3365 | ||
3366 | if (!add_to_clist(&ct_list, c, ct_list_cnt, false)) { | |
3367 | lxc_container_put(c); | |
3368 | ret = -1; | |
3369 | goto free_ct_list; | |
3370 | } | |
3371 | ct_list_cnt++; | |
3372 | } | |
3373 | ||
3374 | if (cret) | |
3375 | *cret = ct_list; | |
3376 | ||
3377 | if (nret) | |
3378 | *nret = ct_name; | |
3379 | else { | |
3380 | ret = ct_cnt; | |
3381 | goto free_ct_name; | |
3382 | } | |
3383 | return ct_cnt; | |
3384 | ||
3385 | free_ct_list: | |
3386 | for (i = 0; i < ct_list_cnt; i++) { | |
3387 | lxc_container_put(ct_list[i]); | |
3388 | } | |
3389 | if (ct_list) | |
3390 | free(ct_list); | |
3391 | ||
3392 | free_active_name: | |
3393 | for (i = 0; i < active_cnt; i++) { | |
3394 | if (active_name[i]) | |
3395 | free(active_name[i]); | |
3396 | } | |
3397 | if (active_name) | |
3398 | free(active_name); | |
3399 | ||
3400 | free_ct_name: | |
3401 | for (i = 0; i < ct_cnt; i++) { | |
3402 | free(ct_name[i]); | |
3403 | } | |
3404 | free(ct_name); | |
3405 | return ret; | |
3406 | } |