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