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