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