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