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