]>
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 |
38683db4 | 22 | #include <dirent.h> |
9be53773 | 23 | #include <errno.h> |
93dc5327 | 24 | #include <fcntl.h> |
38683db4 | 25 | #include <grp.h> |
f2363e38 | 26 | #include <libgen.h> |
38683db4 CB |
27 | #include <pthread.h> |
28 | #include <sched.h> | |
29 | #include <stdarg.h> | |
d659597e | 30 | #include <stdint.h> |
9fc7f8c0 | 31 | #include <stdio.h> |
38683db4 CB |
32 | #include <unistd.h> |
33 | #include <arpa/inet.h> | |
66c1f8c2 | 34 | #include <sys/sysmacros.h> |
38683db4 CB |
35 | #include <sys/mman.h> |
36 | #include <sys/mount.h> | |
5f7eba0b | 37 | #include <sys/syscall.h> |
38683db4 CB |
38 | #include <sys/types.h> |
39 | #include <sys/wait.h> | |
f2363e38 | 40 | |
38683db4 | 41 | #include "attach.h" |
d8e48992 CB |
42 | #include "bdev.h" |
43 | #include "lxcoverlay.h" | |
44 | #include "lxcbtrfs.h" | |
38683db4 | 45 | #include "cgroup.h" |
72d0e1cb | 46 | #include "conf.h" |
38683db4 CB |
47 | #include "config.h" |
48 | #include "commands.h" | |
72d0e1cb | 49 | #include "confile.h" |
b5159817 | 50 | #include "console.h" |
e29fe1dd | 51 | #include "criu.h" |
72d0e1cb | 52 | #include "log.h" |
38683db4 CB |
53 | #include "lxc.h" |
54 | #include "lxccontainer.h" | |
55 | #include "lxclock.h" | |
f2363e38 ÇO |
56 | #include "monitor.h" |
57 | #include "namespace.h" | |
fed29fad | 58 | #include "network.h" |
735f2c6e | 59 | #include "sync.h" |
38683db4 CB |
60 | #include "state.h" |
61 | #include "utils.h" | |
62 | #include "version.h" | |
4ba0d9af | 63 | |
af6824fc ST |
64 | /* major()/minor() */ |
65 | #ifdef MAJOR_IN_MKDEV | |
66 | # include <sys/mkdev.h> | |
67 | #endif | |
af6824fc | 68 | |
4ba0d9af | 69 | #if HAVE_IFADDRS_H |
9c83a661 | 70 | #include <ifaddrs.h> |
4ba0d9af SG |
71 | #else |
72 | #include <../include/ifaddrs.h> | |
73 | #endif | |
72d0e1cb | 74 | |
684f79a5 SG |
75 | #if IS_BIONIC |
76 | #include <../include/lxcmntent.h> | |
77 | #else | |
78 | #include <mntent.h> | |
79 | #endif | |
80 | ||
a9a0ed90 ÇO |
81 | #define MAX_BUFFER 4096 |
82 | ||
c868b261 ÇO |
83 | #define NOT_SUPPORTED_ERROR "the requested function %s is not currently supported with unprivileged containers" |
84 | ||
5f7eba0b SG |
85 | /* Define faccessat() if missing from the C library */ |
86 | #ifndef HAVE_FACCESSAT | |
87 | static int faccessat(int __fd, const char *__file, int __type, int __flag) | |
88 | { | |
89 | #ifdef __NR_faccessat | |
90 | return syscall(__NR_faccessat, __fd, __file, __type, __flag); | |
91 | #else | |
92 | errno = ENOSYS; | |
93 | return -1; | |
94 | #endif | |
95 | } | |
96 | #endif | |
97 | ||
72d0e1cb SG |
98 | lxc_log_define(lxc_container, lxc); |
99 | ||
858377e4 SH |
100 | static bool do_lxcapi_destroy(struct lxc_container *c); |
101 | static const char *lxcapi_get_config_path(struct lxc_container *c); | |
102 | #define do_lxcapi_get_config_path(c) lxcapi_get_config_path(c) | |
103 | static bool do_lxcapi_set_config_item(struct lxc_container *c, const char *key, const char *v); | |
104 | static bool container_destroy(struct lxc_container *c); | |
105 | static bool get_snappath_dir(struct lxc_container *c, char *snappath); | |
106 | static bool lxcapi_snapshot_destroy_all(struct lxc_container *c); | |
107 | static bool do_lxcapi_save_config(struct lxc_container *c, const char *alt_file); | |
108 | ||
a41f104b SH |
109 | static bool config_file_exists(const char *lxcpath, const char *cname) |
110 | { | |
111 | /* $lxcpath + '/' + $cname + '/config' + \0 */ | |
112 | int ret, len = strlen(lxcpath) + strlen(cname) + 9; | |
113 | char *fname = alloca(len); | |
114 | ||
115 | ret = snprintf(fname, len, "%s/%s/config", lxcpath, cname); | |
116 | if (ret < 0 || ret >= len) | |
117 | return false; | |
118 | ||
119 | return file_exists(fname); | |
120 | } | |
121 | ||
3e625e2d SH |
122 | /* |
123 | * A few functions to help detect when a container creation failed. | |
124 | * If a container creation was killed partway through, then trying | |
125 | * to actually start that container could harm the host. We detect | |
126 | * this by creating a 'partial' file under the container directory, | |
127 | * and keeping an advisory lock. When container creation completes, | |
128 | * we remove that file. When we load or try to start a container, if | |
129 | * we find that file, without a flock, we remove the container. | |
130 | */ | |
74a3920a | 131 | static int ongoing_create(struct lxc_container *c) |
3e625e2d SH |
132 | { |
133 | int len = strlen(c->config_path) + strlen(c->name) + 10; | |
134 | char *path = alloca(len); | |
135 | int fd, ret; | |
93dc5327 SH |
136 | struct flock lk; |
137 | ||
3e625e2d SH |
138 | ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name); |
139 | if (ret < 0 || ret >= len) { | |
140 | ERROR("Error writing partial pathname"); | |
141 | return -1; | |
142 | } | |
143 | ||
144 | if (!file_exists(path)) | |
145 | return 0; | |
025ed0f3 | 146 | fd = open(path, O_RDWR); |
025ed0f3 | 147 | if (fd < 0) { |
3e625e2d SH |
148 | // give benefit of the doubt |
149 | SYSERROR("Error opening partial file"); | |
3e625e2d SH |
150 | return 0; |
151 | } | |
93dc5327 SH |
152 | lk.l_type = F_WRLCK; |
153 | lk.l_whence = SEEK_SET; | |
154 | lk.l_start = 0; | |
155 | lk.l_len = 0; | |
156 | lk.l_pid = -1; | |
157 | if (fcntl(fd, F_GETLK, &lk) == 0 && lk.l_pid != -1) { | |
3e625e2d SH |
158 | // create is still ongoing |
159 | close(fd); | |
3e625e2d SH |
160 | return 1; |
161 | } | |
162 | // create completed but partial is still there. | |
163 | close(fd); | |
3e625e2d SH |
164 | return 2; |
165 | } | |
166 | ||
74a3920a | 167 | static int create_partial(struct lxc_container *c) |
3e625e2d SH |
168 | { |
169 | // $lxcpath + '/' + $name + '/partial' + \0 | |
170 | int len = strlen(c->config_path) + strlen(c->name) + 10; | |
171 | char *path = alloca(len); | |
172 | int fd, ret; | |
93dc5327 SH |
173 | struct flock lk; |
174 | ||
3e625e2d SH |
175 | ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name); |
176 | if (ret < 0 || ret >= len) { | |
177 | ERROR("Error writing partial pathname"); | |
178 | return -1; | |
179 | } | |
93dc5327 | 180 | if ((fd=open(path, O_RDWR | O_CREAT | O_EXCL, 0755)) < 0) { |
740fe426 | 181 | SYSERROR("Error creating partial file"); |
3e625e2d SH |
182 | return -1; |
183 | } | |
93dc5327 SH |
184 | lk.l_type = F_WRLCK; |
185 | lk.l_whence = SEEK_SET; | |
186 | lk.l_start = 0; | |
187 | lk.l_len = 0; | |
188 | if (fcntl(fd, F_SETLKW, &lk) < 0) { | |
3e625e2d SH |
189 | SYSERROR("Error locking partial file %s", path); |
190 | close(fd); | |
3e625e2d SH |
191 | return -1; |
192 | } | |
3e625e2d SH |
193 | |
194 | return fd; | |
195 | } | |
196 | ||
74a3920a | 197 | static void remove_partial(struct lxc_container *c, int fd) |
3e625e2d SH |
198 | { |
199 | // $lxcpath + '/' + $name + '/partial' + \0 | |
200 | int len = strlen(c->config_path) + strlen(c->name) + 10; | |
201 | char *path = alloca(len); | |
202 | int ret; | |
203 | ||
204 | close(fd); | |
205 | ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name); | |
206 | if (ret < 0 || ret >= len) { | |
207 | ERROR("Error writing partial pathname"); | |
208 | return; | |
209 | } | |
3e625e2d SH |
210 | if (unlink(path) < 0) |
211 | SYSERROR("Error unlink partial file %s", path); | |
3e625e2d SH |
212 | } |
213 | ||
72d0e1cb | 214 | /* LOCKING |
3bc449ed SH |
215 | * 1. container_mem_lock(c) protects the struct lxc_container from multiple threads. |
216 | * 2. container_disk_lock(c) protects the on-disk container data - in particular the | |
217 | * container configuration file. | |
218 | * The container_disk_lock also takes the container_mem_lock. | |
219 | * 3. thread_mutex protects process data (ex: fd table) from multiple threads. | |
72d0e1cb SG |
220 | * NOTHING mutexes two independent programs with their own struct |
221 | * lxc_container for the same c->name, between API calls. For instance, | |
222 | * c->config_read(); c->start(); Between those calls, data on disk | |
223 | * could change (which shouldn't bother the caller unless for instance | |
224 | * the rootfs get moved). c->config_read(); update; c->config_write(); | |
225 | * Two such updaters could race. The callers should therefore check their | |
226 | * results. Trying to prevent that would necessarily expose us to deadlocks | |
227 | * due to hung callers. So I prefer to keep the locks only within our own | |
228 | * functions, not across functions. | |
229 | * | |
3bc449ed | 230 | * If you're going to clone while holding a lxccontainer, increment |
72d0e1cb SG |
231 | * c->numthreads (under privlock) before forking. When deleting, |
232 | * decrement numthreads under privlock, then if it hits 0 you can delete. | |
233 | * Do not ever use a lxccontainer whose numthreads you did not bump. | |
234 | */ | |
235 | ||
236 | static void lxc_container_free(struct lxc_container *c) | |
237 | { | |
238 | if (!c) | |
239 | return; | |
240 | ||
f10fad2f ME |
241 | free(c->configfile); |
242 | c->configfile = NULL; | |
243 | free(c->error_string); | |
244 | c->error_string = NULL; | |
d95db067 | 245 | if (c->slock) { |
df271a59 | 246 | lxc_putlock(c->slock); |
d95db067 DE |
247 | c->slock = NULL; |
248 | } | |
72d0e1cb | 249 | if (c->privlock) { |
df271a59 | 250 | lxc_putlock(c->privlock); |
72d0e1cb SG |
251 | c->privlock = NULL; |
252 | } | |
f10fad2f ME |
253 | free(c->name); |
254 | c->name = NULL; | |
d95db067 DE |
255 | if (c->lxc_conf) { |
256 | lxc_conf_free(c->lxc_conf); | |
257 | c->lxc_conf = NULL; | |
258 | } | |
f10fad2f ME |
259 | free(c->config_path); |
260 | c->config_path = NULL; | |
72cf75fa | 261 | |
72d0e1cb SG |
262 | free(c); |
263 | } | |
264 | ||
43d1aa34 SH |
265 | /* |
266 | * Consider the following case: | |
267 | freer | racing get()er | |
268 | ================================================================== | |
269 | lxc_container_put() | lxc_container_get() | |
270 | \ lxclock(c->privlock) | c->numthreads < 1? (no) | |
271 | \ c->numthreads = 0 | \ lxclock(c->privlock) -> waits | |
272 | \ lxcunlock() | \ | |
273 | \ lxc_container_free() | \ lxclock() returns | |
274 | | \ c->numthreads < 1 -> return 0 | |
275 | \ \ (free stuff) | | |
276 | \ \ sem_destroy(privlock) | | |
277 | ||
278 | * When the get()er checks numthreads the first time, one of the following | |
279 | * is true: | |
280 | * 1. freer has set numthreads = 0. get() returns 0 | |
281 | * 2. freer is between lxclock and setting numthreads to 0. get()er will | |
282 | * sem_wait on privlock, get lxclock after freer() drops it, then see | |
283 | * numthreads is 0 and exit without touching lxclock again.. | |
284 | * 3. freer has not yet locked privlock. If get()er runs first, then put()er | |
285 | * will see --numthreads = 1 and not call lxc_container_free(). | |
286 | */ | |
287 | ||
72d0e1cb SG |
288 | int lxc_container_get(struct lxc_container *c) |
289 | { | |
290 | if (!c) | |
291 | return 0; | |
292 | ||
43d1aa34 SH |
293 | // if someone else has already started freeing the container, don't |
294 | // try to take the lock, which may be invalid | |
295 | if (c->numthreads < 1) | |
296 | return 0; | |
297 | ||
5cee8c50 | 298 | if (container_mem_lock(c)) |
72d0e1cb SG |
299 | return 0; |
300 | if (c->numthreads < 1) { | |
301 | // bail without trying to unlock, bc the privlock is now probably | |
302 | // in freed memory | |
303 | return 0; | |
304 | } | |
305 | c->numthreads++; | |
5cee8c50 | 306 | container_mem_unlock(c); |
72d0e1cb SG |
307 | return 1; |
308 | } | |
309 | ||
310 | int lxc_container_put(struct lxc_container *c) | |
311 | { | |
312 | if (!c) | |
313 | return -1; | |
5cee8c50 | 314 | if (container_mem_lock(c)) |
72d0e1cb SG |
315 | return -1; |
316 | if (--c->numthreads < 1) { | |
5cee8c50 | 317 | container_mem_unlock(c); |
72d0e1cb SG |
318 | lxc_container_free(c); |
319 | return 1; | |
320 | } | |
5cee8c50 | 321 | container_mem_unlock(c); |
72d0e1cb SG |
322 | return 0; |
323 | } | |
324 | ||
858377e4 | 325 | static bool do_lxcapi_is_defined(struct lxc_container *c) |
72d0e1cb SG |
326 | { |
327 | struct stat statbuf; | |
328 | bool ret = false; | |
329 | int statret; | |
330 | ||
331 | if (!c) | |
332 | return false; | |
333 | ||
5cee8c50 | 334 | if (container_mem_lock(c)) |
72d0e1cb SG |
335 | return false; |
336 | if (!c->configfile) | |
337 | goto out; | |
338 | statret = stat(c->configfile, &statbuf); | |
339 | if (statret != 0) | |
340 | goto out; | |
341 | ret = true; | |
342 | ||
343 | out: | |
5cee8c50 | 344 | container_mem_unlock(c); |
72d0e1cb SG |
345 | return ret; |
346 | } | |
347 | ||
858377e4 SH |
348 | #define WRAP_API(rettype, fnname) \ |
349 | static rettype fnname(struct lxc_container *c) \ | |
350 | { \ | |
351 | rettype ret; \ | |
8164f0e2 TA |
352 | bool reset_config = false; \ |
353 | \ | |
354 | if (!current_config && c && c->lxc_conf) { \ | |
355 | current_config = c->lxc_conf; \ | |
356 | reset_config = true; \ | |
357 | } \ | |
358 | \ | |
858377e4 | 359 | ret = do_##fnname(c); \ |
8164f0e2 TA |
360 | if (reset_config) \ |
361 | current_config = NULL; \ | |
362 | \ | |
858377e4 SH |
363 | return ret; \ |
364 | } | |
365 | ||
366 | #define WRAP_API_1(rettype, fnname, t1) \ | |
367 | static rettype fnname(struct lxc_container *c, t1 a1) \ | |
368 | { \ | |
369 | rettype ret; \ | |
8164f0e2 TA |
370 | bool reset_config = false; \ |
371 | \ | |
372 | if (!current_config && c && c->lxc_conf) { \ | |
373 | current_config = c->lxc_conf; \ | |
374 | reset_config = true; \ | |
375 | } \ | |
376 | \ | |
858377e4 | 377 | ret = do_##fnname(c, a1); \ |
8164f0e2 TA |
378 | if (reset_config) \ |
379 | current_config = NULL; \ | |
380 | \ | |
858377e4 SH |
381 | return ret; \ |
382 | } | |
383 | ||
384 | #define WRAP_API_2(rettype, fnname, t1, t2) \ | |
385 | static rettype fnname(struct lxc_container *c, t1 a1, t2 a2) \ | |
386 | { \ | |
387 | rettype ret; \ | |
8164f0e2 TA |
388 | bool reset_config = false; \ |
389 | \ | |
390 | if (!current_config && c && c->lxc_conf) { \ | |
391 | current_config = c->lxc_conf; \ | |
392 | reset_config = true; \ | |
393 | } \ | |
394 | \ | |
858377e4 | 395 | ret = do_##fnname(c, a1, a2); \ |
8164f0e2 TA |
396 | if (reset_config) \ |
397 | current_config = NULL; \ | |
398 | \ | |
858377e4 SH |
399 | return ret; \ |
400 | } | |
401 | ||
402 | #define WRAP_API_3(rettype, fnname, t1, t2, t3) \ | |
403 | static rettype fnname(struct lxc_container *c, t1 a1, t2 a2, t3 a3) \ | |
404 | { \ | |
405 | rettype ret; \ | |
8164f0e2 TA |
406 | bool reset_config = false; \ |
407 | \ | |
408 | if (!current_config && c && c->lxc_conf) { \ | |
409 | current_config = c->lxc_conf; \ | |
410 | reset_config = true; \ | |
411 | } \ | |
412 | \ | |
858377e4 | 413 | ret = do_##fnname(c, a1, a2, a3); \ |
8164f0e2 TA |
414 | if (reset_config) \ |
415 | current_config = NULL; \ | |
416 | \ | |
858377e4 SH |
417 | return ret; \ |
418 | } | |
419 | ||
420 | WRAP_API(bool, lxcapi_is_defined) | |
421 | ||
422 | static const char *do_lxcapi_state(struct lxc_container *c) | |
72d0e1cb | 423 | { |
72d0e1cb SG |
424 | lxc_state_t s; |
425 | ||
426 | if (!c) | |
427 | return NULL; | |
13f5be62 | 428 | s = lxc_getstate(c->name, c->config_path); |
39dc698c | 429 | return lxc_state2str(s); |
72d0e1cb SG |
430 | } |
431 | ||
858377e4 SH |
432 | WRAP_API(const char *, lxcapi_state) |
433 | ||
39dc698c | 434 | static bool is_stopped(struct lxc_container *c) |
794dd120 SH |
435 | { |
436 | lxc_state_t s; | |
13f5be62 | 437 | s = lxc_getstate(c->name, c->config_path); |
794dd120 SH |
438 | return (s == STOPPED); |
439 | } | |
440 | ||
858377e4 | 441 | static bool do_lxcapi_is_running(struct lxc_container *c) |
72d0e1cb SG |
442 | { |
443 | const char *s; | |
444 | ||
445 | if (!c) | |
446 | return false; | |
858377e4 | 447 | s = do_lxcapi_state(c); |
72d0e1cb SG |
448 | if (!s || strcmp(s, "STOPPED") == 0) |
449 | return false; | |
450 | return true; | |
451 | } | |
452 | ||
858377e4 SH |
453 | WRAP_API(bool, lxcapi_is_running) |
454 | ||
455 | static bool do_lxcapi_freeze(struct lxc_container *c) | |
72d0e1cb SG |
456 | { |
457 | int ret; | |
458 | if (!c) | |
459 | return false; | |
460 | ||
9123e471 | 461 | ret = lxc_freeze(c->name, c->config_path); |
72d0e1cb SG |
462 | if (ret) |
463 | return false; | |
464 | return true; | |
465 | } | |
466 | ||
858377e4 SH |
467 | WRAP_API(bool, lxcapi_freeze) |
468 | ||
469 | static bool do_lxcapi_unfreeze(struct lxc_container *c) | |
72d0e1cb SG |
470 | { |
471 | int ret; | |
472 | if (!c) | |
473 | return false; | |
474 | ||
9123e471 | 475 | ret = lxc_unfreeze(c->name, c->config_path); |
72d0e1cb SG |
476 | if (ret) |
477 | return false; | |
478 | return true; | |
479 | } | |
480 | ||
858377e4 SH |
481 | WRAP_API(bool, lxcapi_unfreeze) |
482 | ||
483 | static int do_lxcapi_console_getfd(struct lxc_container *c, int *ttynum, int *masterfd) | |
0115f8fd DE |
484 | { |
485 | int ttyfd; | |
486 | if (!c) | |
487 | return -1; | |
488 | ||
b5159817 | 489 | ttyfd = lxc_console_getfd(c, ttynum, masterfd); |
0115f8fd DE |
490 | return ttyfd; |
491 | } | |
492 | ||
858377e4 SH |
493 | WRAP_API_2(int, lxcapi_console_getfd, int *, int *) |
494 | ||
b5159817 DE |
495 | static int lxcapi_console(struct lxc_container *c, int ttynum, int stdinfd, |
496 | int stdoutfd, int stderrfd, int escape) | |
497 | { | |
858377e4 SH |
498 | int ret; |
499 | ||
500 | if (!c) | |
501 | return -1; | |
502 | ||
503 | current_config = c->lxc_conf; | |
504 | ret = lxc_console(c, ttynum, stdinfd, stdoutfd, stderrfd, escape); | |
505 | current_config = NULL; | |
506 | return ret; | |
b5159817 DE |
507 | } |
508 | ||
858377e4 | 509 | static pid_t do_lxcapi_init_pid(struct lxc_container *c) |
72d0e1cb | 510 | { |
72d0e1cb SG |
511 | if (!c) |
512 | return -1; | |
513 | ||
5cee8c50 | 514 | return lxc_cmd_get_init_pid(c->name, c->config_path); |
72d0e1cb SG |
515 | } |
516 | ||
858377e4 SH |
517 | WRAP_API(pid_t, lxcapi_init_pid) |
518 | ||
12a50cc6 | 519 | static bool load_config_locked(struct lxc_container *c, const char *fname) |
8eb5694b SH |
520 | { |
521 | if (!c->lxc_conf) | |
522 | c->lxc_conf = lxc_conf_init(); | |
6b0d5538 SH |
523 | if (!c->lxc_conf) |
524 | return false; | |
d08779d4 SH |
525 | if (lxc_config_read(fname, c->lxc_conf, false) != 0) |
526 | return false; | |
d08779d4 | 527 | return true; |
8eb5694b SH |
528 | } |
529 | ||
858377e4 | 530 | static bool do_lxcapi_load_config(struct lxc_container *c, const char *alt_file) |
72d0e1cb | 531 | { |
39dc698c SH |
532 | bool ret = false, need_disklock = false; |
533 | int lret; | |
12a50cc6 | 534 | const char *fname; |
72d0e1cb SG |
535 | if (!c) |
536 | return false; | |
537 | ||
538 | fname = c->configfile; | |
539 | if (alt_file) | |
540 | fname = alt_file; | |
541 | if (!fname) | |
542 | return false; | |
39dc698c SH |
543 | /* |
544 | * If we're reading something other than the container's config, | |
545 | * we only need to lock the in-memory container. If loading the | |
546 | * container's config file, take the disk lock. | |
547 | */ | |
548 | if (strcmp(fname, c->configfile) == 0) | |
549 | need_disklock = true; | |
550 | ||
551 | if (need_disklock) | |
552 | lret = container_disk_lock(c); | |
553 | else | |
554 | lret = container_mem_lock(c); | |
555 | if (lret) | |
72d0e1cb | 556 | return false; |
39dc698c | 557 | |
8eb5694b | 558 | ret = load_config_locked(c, fname); |
39dc698c SH |
559 | |
560 | if (need_disklock) | |
561 | container_disk_unlock(c); | |
562 | else | |
563 | container_mem_unlock(c); | |
72d0e1cb SG |
564 | return ret; |
565 | } | |
566 | ||
858377e4 SH |
567 | WRAP_API_1(bool, lxcapi_load_config, const char *) |
568 | ||
569 | static bool do_lxcapi_want_daemonize(struct lxc_container *c, bool state) | |
72d0e1cb | 570 | { |
497a2995 | 571 | if (!c || !c->lxc_conf) |
540f932a | 572 | return false; |
f02abefe | 573 | if (container_mem_lock(c)) { |
3bc449ed | 574 | ERROR("Error getting mem lock"); |
540f932a | 575 | return false; |
3bc449ed | 576 | } |
a2739df5 | 577 | c->daemonize = state; |
3bc449ed | 578 | container_mem_unlock(c); |
540f932a | 579 | return true; |
72d0e1cb SG |
580 | } |
581 | ||
858377e4 SH |
582 | WRAP_API_1(bool, lxcapi_want_daemonize, bool) |
583 | ||
584 | static bool do_lxcapi_want_close_all_fds(struct lxc_container *c, bool state) | |
130a1888 ÇO |
585 | { |
586 | if (!c || !c->lxc_conf) | |
49badbbe | 587 | return false; |
130a1888 ÇO |
588 | if (container_mem_lock(c)) { |
589 | ERROR("Error getting mem lock"); | |
49badbbe | 590 | return false; |
130a1888 | 591 | } |
540f932a | 592 | c->lxc_conf->close_all_fds = state; |
130a1888 | 593 | container_mem_unlock(c); |
49badbbe | 594 | return true; |
130a1888 ÇO |
595 | } |
596 | ||
858377e4 SH |
597 | WRAP_API_1(bool, lxcapi_want_close_all_fds, bool) |
598 | ||
599 | static bool do_lxcapi_wait(struct lxc_container *c, const char *state, int timeout) | |
7a44c8b4 SG |
600 | { |
601 | int ret; | |
602 | ||
603 | if (!c) | |
604 | return false; | |
605 | ||
67e571de | 606 | ret = lxc_wait(c->name, state, timeout, c->config_path); |
7a44c8b4 SG |
607 | return ret == 0; |
608 | } | |
609 | ||
858377e4 | 610 | WRAP_API_2(bool, lxcapi_wait, const char *, int) |
7a44c8b4 | 611 | |
858377e4 | 612 | static bool do_wait_on_daemonized_start(struct lxc_container *c, int pid) |
7a44c8b4 SG |
613 | { |
614 | /* we'll probably want to make this timeout configurable? */ | |
697fa639 | 615 | int timeout = 5, ret, status; |
7a44c8b4 | 616 | |
697fa639 SH |
617 | /* |
618 | * our child is going to fork again, then exit. reap the | |
619 | * child | |
620 | */ | |
03f064ff | 621 | ret = waitpid(pid, &status, 0); |
697fa639 SH |
622 | if (ret == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) |
623 | DEBUG("failed waiting for first dual-fork child"); | |
858377e4 | 624 | return do_lxcapi_wait(c, "RUNNING", timeout); |
7a44c8b4 SG |
625 | } |
626 | ||
858377e4 SH |
627 | WRAP_API_1(bool, wait_on_daemonized_start, int) |
628 | ||
2d834aa8 SH |
629 | static bool am_single_threaded(void) |
630 | { | |
74f96976 | 631 | struct dirent *direntp; |
2d834aa8 SH |
632 | DIR *dir; |
633 | int count=0; | |
634 | ||
2d834aa8 | 635 | dir = opendir("/proc/self/task"); |
2d834aa8 SH |
636 | if (!dir) { |
637 | INFO("failed to open /proc/self/task"); | |
638 | return false; | |
639 | } | |
640 | ||
74f96976 | 641 | while ((direntp = readdir(dir))) { |
2d834aa8 SH |
642 | if (!direntp) |
643 | break; | |
644 | ||
645 | if (!strcmp(direntp->d_name, ".")) | |
646 | continue; | |
647 | ||
648 | if (!strcmp(direntp->d_name, "..")) | |
649 | continue; | |
650 | if (++count > 1) | |
651 | break; | |
652 | } | |
2d834aa8 | 653 | closedir(dir); |
2d834aa8 SH |
654 | return count == 1; |
655 | } | |
656 | ||
fd51a89b SH |
657 | static void push_arg(char ***argp, char *arg, int *nargs) |
658 | { | |
659 | char **argv; | |
660 | char *copy; | |
661 | ||
662 | do { | |
663 | copy = strdup(arg); | |
664 | } while (!copy); | |
665 | do { | |
666 | argv = realloc(*argp, (*nargs + 2) * sizeof(char *)); | |
667 | } while (!argv); | |
668 | *argp = argv; | |
669 | argv[*nargs] = copy; | |
670 | (*nargs)++; | |
671 | argv[*nargs] = NULL; | |
672 | } | |
673 | ||
674 | static char **split_init_cmd(const char *incmd) | |
675 | { | |
676 | size_t len; | |
677 | int nargs = 0; | |
5590a671 | 678 | char *copy, *p, *saveptr = NULL; |
fd51a89b SH |
679 | char **argv; |
680 | ||
681 | if (!incmd) | |
682 | return NULL; | |
683 | ||
684 | len = strlen(incmd) + 1; | |
685 | copy = alloca(len); | |
686 | strncpy(copy, incmd, len); | |
687 | copy[len-1] = '\0'; | |
688 | ||
689 | do { | |
690 | argv = malloc(sizeof(char *)); | |
691 | } while (!argv); | |
692 | argv[0] = NULL; | |
693 | for (p = strtok_r(copy, " ", &saveptr); p != NULL; | |
694 | p = strtok_r(NULL, " ", &saveptr)) | |
695 | push_arg(&argv, p, &nargs); | |
696 | ||
697 | if (nargs == 0) { | |
698 | free(argv); | |
699 | return NULL; | |
700 | } | |
701 | return argv; | |
702 | } | |
703 | ||
704 | static void free_init_cmd(char **argv) | |
705 | { | |
706 | int i = 0; | |
707 | ||
708 | if (!argv) | |
709 | return; | |
710 | while (argv[i]) | |
711 | free(argv[i++]); | |
712 | free(argv); | |
713 | } | |
714 | ||
858377e4 | 715 | static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const argv[]) |
72d0e1cb SG |
716 | { |
717 | int ret; | |
718 | struct lxc_conf *conf; | |
540f932a | 719 | bool daemonize = false; |
6eaac303 | 720 | FILE *pid_fp = NULL; |
72d0e1cb SG |
721 | char *default_args[] = { |
722 | "/sbin/init", | |
13aad0ae | 723 | NULL, |
72d0e1cb | 724 | }; |
fd51a89b | 725 | char **init_cmd = NULL; |
72d0e1cb SG |
726 | |
727 | /* container exists */ | |
728 | if (!c) | |
729 | return false; | |
120146b9 SG |
730 | |
731 | /* If anything fails before we set error_num, we want an error in there */ | |
732 | c->error_num = 1; | |
733 | ||
72d0e1cb SG |
734 | /* container has been setup */ |
735 | if (!c->lxc_conf) | |
736 | return false; | |
737 | ||
3e625e2d SH |
738 | if ((ret = ongoing_create(c)) < 0) { |
739 | ERROR("Error checking for incomplete creation"); | |
740 | return false; | |
741 | } | |
742 | if (ret == 2) { | |
743 | ERROR("Error: %s creation was not completed", c->name); | |
858377e4 | 744 | do_lxcapi_destroy(c); |
3e625e2d SH |
745 | return false; |
746 | } else if (ret == 1) { | |
747 | ERROR("Error: creation of %s is ongoing", c->name); | |
748 | return false; | |
749 | } | |
750 | ||
72d0e1cb SG |
751 | /* is this app meant to be run through lxcinit, as in lxc-execute? */ |
752 | if (useinit && !argv) | |
753 | return false; | |
754 | ||
5cee8c50 | 755 | if (container_mem_lock(c)) |
72d0e1cb SG |
756 | return false; |
757 | conf = c->lxc_conf; | |
758 | daemonize = c->daemonize; | |
5cee8c50 | 759 | container_mem_unlock(c); |
72d0e1cb SG |
760 | |
761 | if (useinit) { | |
507cee36 | 762 | ret = lxc_execute(c->name, argv, 1, conf, c->config_path, daemonize); |
72d0e1cb SG |
763 | return ret == 0 ? true : false; |
764 | } | |
765 | ||
fd51a89b SH |
766 | /* if no argv was passed in, use lxc.init_cmd if provided in |
767 | * configuration */ | |
768 | if (!argv) | |
769 | argv = init_cmd = split_init_cmd(conf->init_cmd); | |
770 | ||
771 | /* ... and otherwise use default_args */ | |
772 | if (!argv) | |
773 | argv = default_args; | |
72d0e1cb SG |
774 | |
775 | /* | |
776 | * say, I'm not sure - what locks do we want here? Any? | |
777 | * Is liblxc's locking enough here to protect the on disk | |
778 | * container? We don't want to exclude things like lxc_info | |
779 | * while container is running... | |
780 | */ | |
781 | if (daemonize) { | |
0a4be28d | 782 | char title[2048]; |
e51d4895 | 783 | lxc_monitord_spawn(c->config_path); |
71454076 | 784 | |
72d0e1cb | 785 | pid_t pid = fork(); |
844f7a38 | 786 | if (pid < 0) |
72d0e1cb | 787 | return false; |
6eaac303 QH |
788 | |
789 | if (pid != 0) { | |
790 | /* Set to NULL because we don't want father unlink | |
791 | * the PID file, child will do the free and unlink. | |
792 | */ | |
793 | c->pidfile = NULL; | |
03f064ff | 794 | return wait_on_daemonized_start(c, pid); |
6eaac303 | 795 | } |
025ed0f3 | 796 | |
0a4be28d TA |
797 | /* We don't really care if this doesn't print all the |
798 | * characters; all that it means is that the proctitle will be | |
799 | * ugly. Similarly, we also don't care if setproctitle() | |
800 | * fails. */ | |
801 | snprintf(title, sizeof(title), "[lxc monitor] %s %s", c->config_path, c->name); | |
802 | INFO("Attempting to set proc title to %s", title); | |
803 | setproctitle(title); | |
804 | ||
697fa639 SH |
805 | /* second fork to be reparented by init */ |
806 | pid = fork(); | |
807 | if (pid < 0) { | |
808 | SYSERROR("Error doing dual-fork"); | |
13353dc4 | 809 | exit(1); |
697fa639 SH |
810 | } |
811 | if (pid != 0) | |
812 | exit(0); | |
72d0e1cb | 813 | /* like daemon(), chdir to / and redirect 0,1,2 to /dev/null */ |
c278cef2 SH |
814 | if (chdir("/")) { |
815 | SYSERROR("Error chdir()ing to /."); | |
13353dc4 | 816 | exit(1); |
c278cef2 | 817 | } |
d2cf4c37 | 818 | lxc_check_inherited(conf, true, -1); |
69aeabac TA |
819 | if (null_stdfds() < 0) { |
820 | ERROR("failed to close fds"); | |
13353dc4 | 821 | exit(1); |
69aeabac | 822 | } |
72d0e1cb | 823 | setsid(); |
2d834aa8 SH |
824 | } else { |
825 | if (!am_single_threaded()) { | |
826 | ERROR("Cannot start non-daemonized container when threaded"); | |
827 | return false; | |
828 | } | |
72d0e1cb SG |
829 | } |
830 | ||
6eaac303 QH |
831 | /* We need to write PID file after daeminize, so we always |
832 | * write the right PID. | |
833 | */ | |
834 | if (c->pidfile) { | |
835 | pid_fp = fopen(c->pidfile, "w"); | |
836 | if (pid_fp == NULL) { | |
837 | SYSERROR("Failed to create pidfile '%s' for '%s'", | |
838 | c->pidfile, c->name); | |
13353dc4 SH |
839 | if (daemonize) |
840 | exit(1); | |
6eaac303 QH |
841 | return false; |
842 | } | |
843 | ||
844 | if (fprintf(pid_fp, "%d\n", getpid()) < 0) { | |
845 | SYSERROR("Failed to write '%s'", c->pidfile); | |
846 | fclose(pid_fp); | |
847 | pid_fp = NULL; | |
13353dc4 SH |
848 | if (daemonize) |
849 | exit(1); | |
6eaac303 QH |
850 | return false; |
851 | } | |
852 | ||
853 | fclose(pid_fp); | |
854 | pid_fp = NULL; | |
855 | } | |
856 | ||
72d0e1cb | 857 | conf->reboot = 0; |
d2cf4c37 | 858 | |
a8dfe4e0 WB |
859 | /* Unshare the mount namespace if requested */ |
860 | if (conf->monitor_unshare) { | |
861 | if (unshare(CLONE_NEWNS)) { | |
862 | SYSERROR("failed to unshare mount namespace"); | |
863 | return false; | |
864 | } | |
865 | if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL)) { | |
866 | SYSERROR("Failed to make / rslave at startup"); | |
867 | return false; | |
868 | } | |
869 | } | |
870 | ||
8bee8851 | 871 | reboot: |
d2cf4c37 SH |
872 | if (lxc_check_inherited(conf, daemonize, -1)) { |
873 | ERROR("Inherited fds found"); | |
874 | ret = 1; | |
875 | goto out; | |
876 | } | |
877 | ||
507cee36 | 878 | ret = lxc_start(c->name, argv, conf, c->config_path, daemonize); |
d4ef230c | 879 | c->error_num = ret; |
72d0e1cb | 880 | |
8bee8851 | 881 | if (conf->reboot == 1) { |
72d0e1cb | 882 | INFO("container requested reboot"); |
8bee8851 | 883 | conf->reboot = 2; |
72d0e1cb SG |
884 | goto reboot; |
885 | } | |
886 | ||
d2cf4c37 | 887 | out: |
487d8008 QH |
888 | if (c->pidfile) { |
889 | unlink(c->pidfile); | |
890 | free(c->pidfile); | |
891 | c->pidfile = NULL; | |
892 | } | |
893 | ||
fd51a89b SH |
894 | free_init_cmd(init_cmd); |
895 | ||
844f7a38 | 896 | if (daemonize) |
05e5d7dc | 897 | exit (ret == 0 ? true : false); |
844f7a38 | 898 | else |
05e5d7dc | 899 | return (ret == 0 ? true : false); |
72d0e1cb SG |
900 | } |
901 | ||
858377e4 SH |
902 | static bool lxcapi_start(struct lxc_container *c, int useinit, char * const argv[]) |
903 | { | |
904 | bool ret; | |
905 | current_config = c ? c->lxc_conf : NULL; | |
906 | ret = do_lxcapi_start(c, useinit, argv); | |
907 | current_config = NULL; | |
908 | return ret; | |
909 | } | |
910 | ||
72d0e1cb SG |
911 | /* |
912 | * note there MUST be an ending NULL | |
913 | */ | |
914 | static bool lxcapi_startl(struct lxc_container *c, int useinit, ...) | |
915 | { | |
916 | va_list ap; | |
a0e93eeb | 917 | char **inargs = NULL; |
72d0e1cb SG |
918 | bool bret = false; |
919 | ||
920 | /* container exists */ | |
921 | if (!c) | |
922 | return false; | |
923 | ||
858377e4 SH |
924 | current_config = c->lxc_conf; |
925 | ||
72d0e1cb | 926 | va_start(ap, useinit); |
a0e93eeb | 927 | inargs = lxc_va_arg_list_to_argv(ap, 0, 1); |
72d0e1cb SG |
928 | va_end(ap); |
929 | ||
a0e93eeb CS |
930 | if (!inargs) { |
931 | ERROR("Memory allocation error."); | |
932 | goto out; | |
72d0e1cb SG |
933 | } |
934 | ||
a0e93eeb | 935 | /* pass NULL if no arguments were supplied */ |
858377e4 | 936 | bret = do_lxcapi_start(c, useinit, *inargs ? inargs : NULL); |
72d0e1cb SG |
937 | |
938 | out: | |
939 | if (inargs) { | |
4e03ae57 DE |
940 | char **arg; |
941 | for (arg = inargs; *arg; arg++) | |
942 | free(*arg); | |
72d0e1cb SG |
943 | free(inargs); |
944 | } | |
945 | ||
858377e4 | 946 | current_config = NULL; |
72d0e1cb SG |
947 | return bret; |
948 | } | |
949 | ||
858377e4 | 950 | static bool do_lxcapi_stop(struct lxc_container *c) |
72d0e1cb SG |
951 | { |
952 | int ret; | |
953 | ||
954 | if (!c) | |
955 | return false; | |
956 | ||
ef6e34ee | 957 | ret = lxc_cmd_stop(c->name, c->config_path); |
72d0e1cb | 958 | |
d775f21b | 959 | return ret == 0; |
72d0e1cb SG |
960 | } |
961 | ||
858377e4 SH |
962 | WRAP_API(bool, lxcapi_stop) |
963 | ||
d5752559 SH |
964 | static int do_create_container_dir(const char *path, struct lxc_conf *conf) |
965 | { | |
966 | int ret = -1, lasterr; | |
967 | char *p = alloca(strlen(path)+1); | |
968 | mode_t mask = umask(0002); | |
969 | ret = mkdir(path, 0770); | |
970 | lasterr = errno; | |
971 | umask(mask); | |
972 | errno = lasterr; | |
973 | if (ret) { | |
974 | if (errno == EEXIST) | |
975 | ret = 0; | |
976 | else { | |
977 | SYSERROR("failed to create container path %s", path); | |
978 | return -1; | |
979 | } | |
980 | } | |
981 | strcpy(p, path); | |
982 | if (!lxc_list_empty(&conf->id_map) && chown_mapped_root(p, conf) != 0) { | |
983 | ERROR("Failed to chown container dir"); | |
984 | ret = -1; | |
985 | } | |
986 | return ret; | |
987 | } | |
988 | ||
72d0e1cb SG |
989 | /* |
990 | * create the standard expected container dir | |
991 | */ | |
992 | static bool create_container_dir(struct lxc_container *c) | |
993 | { | |
994 | char *s; | |
995 | int len, ret; | |
996 | ||
2a59a681 | 997 | len = strlen(c->config_path) + strlen(c->name) + 2; |
72d0e1cb SG |
998 | s = malloc(len); |
999 | if (!s) | |
1000 | return false; | |
2a59a681 | 1001 | ret = snprintf(s, len, "%s/%s", c->config_path, c->name); |
72d0e1cb SG |
1002 | if (ret < 0 || ret >= len) { |
1003 | free(s); | |
1004 | return false; | |
1005 | } | |
d5752559 | 1006 | ret = do_create_container_dir(s, c->lxc_conf); |
72d0e1cb SG |
1007 | free(s); |
1008 | return ret == 0; | |
1009 | } | |
1010 | ||
1011 | /* | |
1897e3bc SH |
1012 | * do_bdev_create: thin wrapper around bdev_create(). Like bdev_create(), |
1013 | * it returns a mounted bdev on success, NULL on error. | |
72d0e1cb | 1014 | */ |
1897e3bc SH |
1015 | static struct bdev *do_bdev_create(struct lxc_container *c, const char *type, |
1016 | struct bdev_specs *specs) | |
1017 | { | |
1018 | char *dest; | |
1897e3bc SH |
1019 | size_t len; |
1020 | struct bdev *bdev; | |
1021 | int ret; | |
1022 | ||
cd219ae6 SY |
1023 | /* rootfs.path or lxcpath/lxcname/rootfs */ |
1024 | if (c->lxc_conf->rootfs.path && access(c->lxc_conf->rootfs.path, F_OK) == 0) { | |
cf465fe4 SH |
1025 | const char *rpath = c->lxc_conf->rootfs.path; |
1026 | len = strlen(rpath) + 1; | |
cd219ae6 | 1027 | dest = alloca(len); |
cf465fe4 | 1028 | ret = snprintf(dest, len, "%s", rpath); |
cd219ae6 | 1029 | } else { |
858377e4 | 1030 | const char *lxcpath = do_lxcapi_get_config_path(c); |
cd219ae6 SY |
1031 | len = strlen(c->name) + strlen(lxcpath) + 9; |
1032 | dest = alloca(len); | |
1033 | ret = snprintf(dest, len, "%s/%s/rootfs", lxcpath, c->name); | |
1034 | } | |
1897e3bc SH |
1035 | if (ret < 0 || ret >= len) |
1036 | return NULL; | |
1037 | ||
1038 | bdev = bdev_create(dest, type, c->name, specs); | |
d44e88c2 | 1039 | if (!bdev) { |
959aee9c | 1040 | ERROR("Failed to create backing store type %s", type); |
1897e3bc | 1041 | return NULL; |
d44e88c2 SH |
1042 | } |
1043 | ||
858377e4 | 1044 | do_lxcapi_set_config_item(c, "lxc.rootfs", bdev->src); |
39460be8 | 1045 | do_lxcapi_set_config_item(c, "lxc.rootfs.backend", bdev->type); |
cf3ef16d SH |
1046 | |
1047 | /* if we are not root, chown the rootfs dir to root in the | |
1048 | * target uidmap */ | |
1049 | ||
0e6e3a41 | 1050 | if (geteuid() != 0 || (c->lxc_conf && !lxc_list_empty(&c->lxc_conf->id_map))) { |
c4d10a05 | 1051 | if (chown_mapped_root(bdev->dest, c->lxc_conf) < 0) { |
959aee9c | 1052 | ERROR("Error chowning %s to container root", bdev->dest); |
97e9cfa0 | 1053 | suggest_default_idmap(); |
cf3ef16d SH |
1054 | bdev_put(bdev); |
1055 | return NULL; | |
1056 | } | |
1057 | } | |
1058 | ||
1897e3bc SH |
1059 | return bdev; |
1060 | } | |
1061 | ||
96b3cb40 | 1062 | static char *lxcbasename(char *path) |
72d0e1cb | 1063 | { |
96b3cb40 SH |
1064 | char *p = path + strlen(path) - 1; |
1065 | while (*p != '/' && p > path) | |
1066 | p--; | |
1067 | return p; | |
1068 | } | |
72d0e1cb | 1069 | |
69aeabac | 1070 | static bool create_run_template(struct lxc_container *c, char *tpath, bool need_null_stdfds, |
96b3cb40 SH |
1071 | char *const argv[]) |
1072 | { | |
1073 | pid_t pid; | |
72d0e1cb | 1074 | |
72d0e1cb | 1075 | if (!tpath) |
96b3cb40 | 1076 | return true; |
72d0e1cb SG |
1077 | |
1078 | pid = fork(); | |
1079 | if (pid < 0) { | |
959aee9c | 1080 | SYSERROR("failed to fork task for container creation template"); |
96b3cb40 | 1081 | return false; |
72d0e1cb SG |
1082 | } |
1083 | ||
1084 | if (pid == 0) { // child | |
39460be8 | 1085 | char *patharg, *namearg, *rootfsarg; |
1897e3bc | 1086 | struct bdev *bdev = NULL; |
72d0e1cb | 1087 | int i; |
96b3cb40 SH |
1088 | int ret, len, nargs = 0; |
1089 | char **newargv; | |
cf3ef16d | 1090 | struct lxc_conf *conf = c->lxc_conf; |
72d0e1cb | 1091 | |
69aeabac TA |
1092 | if (need_null_stdfds && null_stdfds() < 0) { |
1093 | exit(1); | |
dc23c1c8 | 1094 | } |
1897e3bc | 1095 | |
39460be8 | 1096 | bdev = bdev_init(c->lxc_conf, c->lxc_conf->rootfs.path, c->lxc_conf->rootfs.mount, NULL); |
1897e3bc SH |
1097 | if (!bdev) { |
1098 | ERROR("Error opening rootfs"); | |
1099 | exit(1); | |
1100 | } | |
1101 | ||
4de2791f | 1102 | if (geteuid() == 0) { |
cf3ef16d SH |
1103 | if (unshare(CLONE_NEWNS) < 0) { |
1104 | ERROR("error unsharing mounts"); | |
1105 | exit(1); | |
1106 | } | |
4de2791f | 1107 | if (detect_shared_rootfs()) { |
c597baa8 | 1108 | if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL)) { |
4de2791f SH |
1109 | SYSERROR("Failed to make / rslave to run template"); |
1110 | ERROR("Continuing..."); | |
1111 | } | |
1112 | } | |
1113 | } | |
2659c7cb | 1114 | if (strcmp(bdev->type, "dir") && strcmp(bdev->type, "btrfs")) { |
4de2791f | 1115 | if (geteuid() != 0) { |
2659c7cb | 1116 | ERROR("non-root users can only create btrfs and directory-backed containers"); |
4de2791f SH |
1117 | exit(1); |
1118 | } | |
cf3ef16d SH |
1119 | if (bdev->ops->mount(bdev) < 0) { |
1120 | ERROR("Error mounting rootfs"); | |
1121 | exit(1); | |
1122 | } | |
1123 | } else { // TODO come up with a better way here! | |
f10fad2f | 1124 | free(bdev->dest); |
cf3ef16d | 1125 | bdev->dest = strdup(bdev->src); |
1897e3bc SH |
1126 | } |
1127 | ||
72d0e1cb SG |
1128 | /* |
1129 | * create our new array, pre-pend the template name and | |
1130 | * base args | |
1131 | */ | |
1132 | if (argv) | |
1897e3bc | 1133 | for (nargs = 0; argv[nargs]; nargs++) ; |
6849cb5b | 1134 | nargs += 4; // template, path, rootfs and name args |
cf3ef16d | 1135 | |
72d0e1cb SG |
1136 | newargv = malloc(nargs * sizeof(*newargv)); |
1137 | if (!newargv) | |
1138 | exit(1); | |
96b3cb40 | 1139 | newargv[0] = lxcbasename(tpath); |
72d0e1cb | 1140 | |
2a59a681 | 1141 | len = strlen(c->config_path) + strlen(c->name) + strlen("--path=") + 2; |
72d0e1cb SG |
1142 | patharg = malloc(len); |
1143 | if (!patharg) | |
1144 | exit(1); | |
2a59a681 | 1145 | ret = snprintf(patharg, len, "--path=%s/%s", c->config_path, c->name); |
72d0e1cb SG |
1146 | if (ret < 0 || ret >= len) |
1147 | exit(1); | |
1148 | newargv[1] = patharg; | |
1149 | len = strlen("--name=") + strlen(c->name) + 1; | |
1150 | namearg = malloc(len); | |
1151 | if (!namearg) | |
1152 | exit(1); | |
1153 | ret = snprintf(namearg, len, "--name=%s", c->name); | |
1154 | if (ret < 0 || ret >= len) | |
1155 | exit(1); | |
1156 | newargv[2] = namearg; | |
1157 | ||
1897e3bc SH |
1158 | len = strlen("--rootfs=") + 1 + strlen(bdev->dest); |
1159 | rootfsarg = malloc(len); | |
1160 | if (!rootfsarg) | |
1161 | exit(1); | |
1162 | ret = snprintf(rootfsarg, len, "--rootfs=%s", bdev->dest); | |
1163 | if (ret < 0 || ret >= len) | |
1164 | exit(1); | |
1165 | newargv[3] = rootfsarg; | |
1166 | ||
72d0e1cb SG |
1167 | /* add passed-in args */ |
1168 | if (argv) | |
1897e3bc SH |
1169 | for (i = 4; i < nargs; i++) |
1170 | newargv[i] = argv[i-4]; | |
72d0e1cb SG |
1171 | |
1172 | /* add trailing NULL */ | |
1173 | nargs++; | |
1174 | newargv = realloc(newargv, nargs * sizeof(*newargv)); | |
1175 | if (!newargv) | |
1176 | exit(1); | |
1177 | newargv[nargs - 1] = NULL; | |
1178 | ||
cf3ef16d SH |
1179 | /* |
1180 | * If we're running the template in a mapped userns, then | |
1181 | * we prepend the template command with: | |
1182 | * lxc-usernsexec <-m map1> ... <-m mapn> -- | |
57d116ab SH |
1183 | * and we append "--mapped-uid x", where x is the mapped uid |
1184 | * for our geteuid() | |
cf3ef16d | 1185 | */ |
0e6e3a41 | 1186 | if (!lxc_list_empty(&conf->id_map)) { |
cf3ef16d | 1187 | int n2args = 1; |
57d116ab | 1188 | char txtuid[20]; |
2133f58c | 1189 | char txtgid[20]; |
cf3ef16d SH |
1190 | char **n2 = malloc(n2args * sizeof(*n2)); |
1191 | struct lxc_list *it; | |
1192 | struct id_map *map; | |
1193 | ||
57d116ab SH |
1194 | if (!n2) { |
1195 | SYSERROR("out of memory"); | |
1196 | exit(1); | |
1197 | } | |
cf3ef16d SH |
1198 | newargv[0] = tpath; |
1199 | tpath = "lxc-usernsexec"; | |
1200 | n2[0] = "lxc-usernsexec"; | |
1201 | lxc_list_for_each(it, &conf->id_map) { | |
1202 | map = it->elem; | |
1203 | n2args += 2; | |
57d116ab | 1204 | n2 = realloc(n2, n2args * sizeof(char *)); |
cf3ef16d SH |
1205 | if (!n2) |
1206 | exit(1); | |
1207 | n2[n2args-2] = "-m"; | |
1208 | n2[n2args-1] = malloc(200); | |
1209 | if (!n2[n2args-1]) | |
1210 | exit(1); | |
1211 | ret = snprintf(n2[n2args-1], 200, "%c:%lu:%lu:%lu", | |
1212 | map->idtype == ID_TYPE_UID ? 'u' : 'g', | |
1213 | map->nsid, map->hostid, map->range); | |
1214 | if (ret < 0 || ret >= 200) | |
1215 | exit(1); | |
1216 | } | |
2133f58c | 1217 | int hostid_mapped = mapped_hostid(geteuid(), conf, ID_TYPE_UID); |
6849cb5b | 1218 | int extraargs = hostid_mapped >= 0 ? 1 : 3; |
57d116ab | 1219 | n2 = realloc(n2, (nargs + n2args + extraargs) * sizeof(char *)); |
cf3ef16d SH |
1220 | if (!n2) |
1221 | exit(1); | |
57d116ab | 1222 | if (hostid_mapped < 0) { |
339efad9 | 1223 | hostid_mapped = find_unmapped_nsid(conf, ID_TYPE_UID); |
cf3ef16d | 1224 | n2[n2args++] = "-m"; |
57d116ab | 1225 | if (hostid_mapped < 0) { |
cf3ef16d SH |
1226 | ERROR("Could not find free uid to map"); |
1227 | exit(1); | |
1228 | } | |
1229 | n2[n2args++] = malloc(200); | |
1230 | if (!n2[n2args-1]) { | |
1231 | SYSERROR("out of memory"); | |
1232 | exit(1); | |
1233 | } | |
1234 | ret = snprintf(n2[n2args-1], 200, "u:%d:%d:1", | |
57d116ab | 1235 | hostid_mapped, geteuid()); |
cf3ef16d SH |
1236 | if (ret < 0 || ret >= 200) { |
1237 | ERROR("string too long"); | |
1238 | exit(1); | |
1239 | } | |
1240 | } | |
2133f58c SH |
1241 | int hostgid_mapped = mapped_hostid(getegid(), conf, ID_TYPE_GID); |
1242 | extraargs = hostgid_mapped >= 0 ? 1 : 3; | |
1243 | n2 = realloc(n2, (nargs + n2args + extraargs) * sizeof(char *)); | |
1244 | if (!n2) | |
1245 | exit(1); | |
1246 | if (hostgid_mapped < 0) { | |
339efad9 | 1247 | hostgid_mapped = find_unmapped_nsid(conf, ID_TYPE_GID); |
2133f58c SH |
1248 | n2[n2args++] = "-m"; |
1249 | if (hostgid_mapped < 0) { | |
1250 | ERROR("Could not find free uid to map"); | |
1251 | exit(1); | |
1252 | } | |
1253 | n2[n2args++] = malloc(200); | |
1254 | if (!n2[n2args-1]) { | |
1255 | SYSERROR("out of memory"); | |
1256 | exit(1); | |
1257 | } | |
1258 | ret = snprintf(n2[n2args-1], 200, "g:%d:%d:1", | |
1259 | hostgid_mapped, getegid()); | |
1260 | if (ret < 0 || ret >= 200) { | |
1261 | ERROR("string too long"); | |
1262 | exit(1); | |
1263 | } | |
1264 | } | |
cf3ef16d SH |
1265 | n2[n2args++] = "--"; |
1266 | for (i = 0; i < nargs; i++) | |
1267 | n2[i + n2args] = newargv[i]; | |
57d116ab SH |
1268 | n2args += nargs; |
1269 | // Finally add "--mapped-uid $uid" to tell template what to chown | |
1270 | // cached images to | |
2133f58c | 1271 | n2args += 4; |
57d116ab SH |
1272 | n2 = realloc(n2, n2args * sizeof(char *)); |
1273 | if (!n2) { | |
1274 | SYSERROR("out of memory"); | |
1275 | exit(1); | |
1276 | } | |
1277 | // note n2[n2args-1] is NULL | |
2133f58c | 1278 | n2[n2args-5] = "--mapped-uid"; |
57d116ab | 1279 | snprintf(txtuid, 20, "%d", hostid_mapped); |
2133f58c SH |
1280 | n2[n2args-4] = txtuid; |
1281 | n2[n2args-3] = "--mapped-gid"; | |
1282 | snprintf(txtgid, 20, "%d", hostgid_mapped); | |
1283 | n2[n2args-2] = txtgid; | |
57d116ab | 1284 | n2[n2args-1] = NULL; |
cf3ef16d SH |
1285 | free(newargv); |
1286 | newargv = n2; | |
1287 | } | |
72d0e1cb | 1288 | /* execute */ |
cf3ef16d | 1289 | execvp(tpath, newargv); |
72d0e1cb SG |
1290 | SYSERROR("failed to execute template %s", tpath); |
1291 | exit(1); | |
1292 | } | |
1293 | ||
9be53773 | 1294 | if (wait_for_pid(pid) != 0) { |
959aee9c | 1295 | ERROR("container creation template for %s failed", c->name); |
96b3cb40 SH |
1296 | return false; |
1297 | } | |
1298 | ||
1299 | return true; | |
1300 | } | |
1301 | ||
74a3920a | 1302 | static bool prepend_lxc_header(char *path, const char *t, char *const argv[]) |
3ce74686 | 1303 | { |
1fd9bd50 | 1304 | long flen; |
b4569e93 | 1305 | char *contents; |
3ce74686 | 1306 | FILE *f; |
025ed0f3 | 1307 | int ret = -1; |
52026772 | 1308 | #if HAVE_LIBGNUTLS |
025ed0f3 | 1309 | int i; |
3ce74686 | 1310 | unsigned char md_value[SHA_DIGEST_LENGTH]; |
b4569e93 | 1311 | char *tpath; |
52026772 | 1312 | #endif |
3ce74686 | 1313 | |
025ed0f3 | 1314 | f = fopen(path, "r"); |
025ed0f3 | 1315 | if (f == NULL) |
3ce74686 | 1316 | return false; |
025ed0f3 SH |
1317 | |
1318 | if (fseek(f, 0, SEEK_END) < 0) | |
1319 | goto out_error; | |
1320 | if ((flen = ftell(f)) < 0) | |
1321 | goto out_error; | |
1322 | if (fseek(f, 0, SEEK_SET) < 0) | |
1323 | goto out_error; | |
1324 | if ((contents = malloc(flen + 1)) == NULL) | |
1325 | goto out_error; | |
1326 | if (fread(contents, 1, flen, f) != flen) | |
1327 | goto out_free_contents; | |
1328 | ||
3ce74686 | 1329 | contents[flen] = '\0'; |
025ed0f3 | 1330 | ret = fclose(f); |
025ed0f3 SH |
1331 | f = NULL; |
1332 | if (ret < 0) | |
1333 | goto out_free_contents; | |
3ce74686 | 1334 | |
b4569e93 | 1335 | #if HAVE_LIBGNUTLS |
01efd4d3 | 1336 | tpath = get_template_path(t); |
85db5535 | 1337 | if (!tpath) { |
959aee9c | 1338 | ERROR("bad template: %s", t); |
025ed0f3 | 1339 | goto out_free_contents; |
3ce74686 SH |
1340 | } |
1341 | ||
85db5535 DE |
1342 | ret = sha1sum_file(tpath, md_value); |
1343 | if (ret < 0) { | |
1344 | ERROR("Error getting sha1sum of %s", tpath); | |
3ce74686 | 1345 | free(tpath); |
85db5535 | 1346 | goto out_free_contents; |
3ce74686 | 1347 | } |
85db5535 | 1348 | free(tpath); |
3ce74686 SH |
1349 | #endif |
1350 | ||
025ed0f3 | 1351 | f = fopen(path, "w"); |
025ed0f3 | 1352 | if (f == NULL) { |
3ce74686 SH |
1353 | SYSERROR("reopening config for writing"); |
1354 | free(contents); | |
1355 | return false; | |
1356 | } | |
1357 | fprintf(f, "# Template used to create this container: %s\n", t); | |
1358 | if (argv) { | |
1359 | fprintf(f, "# Parameters passed to the template:"); | |
1360 | while (*argv) { | |
1361 | fprintf(f, " %s", *argv); | |
1362 | argv++; | |
1363 | } | |
1364 | fprintf(f, "\n"); | |
1365 | } | |
1366 | #if HAVE_LIBGNUTLS | |
56698177 SH |
1367 | fprintf(f, "# Template script checksum (SHA-1): "); |
1368 | for (i=0; i<SHA_DIGEST_LENGTH; i++) | |
1369 | fprintf(f, "%02x", md_value[i]); | |
1370 | fprintf(f, "\n"); | |
3ce74686 | 1371 | #endif |
0520c252 | 1372 | fprintf(f, "# For additional config options, please look at lxc.container.conf(5)\n"); |
49a2ed80 SH |
1373 | fprintf(f, "\n# Uncomment the following line to support nesting containers:\n"); |
1374 | fprintf(f, "#lxc.include = " LXCTEMPLATECONFIG "/nesting.conf\n"); | |
1375 | fprintf(f, "# (Be aware this has security implications)\n\n"); | |
3ce74686 SH |
1376 | if (fwrite(contents, 1, flen, f) != flen) { |
1377 | SYSERROR("Writing original contents"); | |
1378 | free(contents); | |
1379 | fclose(f); | |
1380 | return false; | |
1381 | } | |
025ed0f3 SH |
1382 | ret = 0; |
1383 | out_free_contents: | |
3ce74686 | 1384 | free(contents); |
025ed0f3 SH |
1385 | out_error: |
1386 | if (f) { | |
1387 | int newret; | |
025ed0f3 | 1388 | newret = fclose(f); |
025ed0f3 SH |
1389 | if (ret == 0) |
1390 | ret = newret; | |
1391 | } | |
1392 | if (ret < 0) { | |
1393 | SYSERROR("Error prepending header"); | |
3ce74686 SH |
1394 | return false; |
1395 | } | |
1396 | return true; | |
1397 | } | |
1398 | ||
4df7f012 SH |
1399 | static void lxcapi_clear_config(struct lxc_container *c) |
1400 | { | |
f979ac15 SH |
1401 | if (c) { |
1402 | if (c->lxc_conf) { | |
1403 | lxc_conf_free(c->lxc_conf); | |
1404 | c->lxc_conf = NULL; | |
1405 | } | |
4df7f012 SH |
1406 | } |
1407 | } | |
1408 | ||
858377e4 SH |
1409 | #define do_lxcapi_clear_config(c) lxcapi_clear_config(c) |
1410 | ||
96b3cb40 SH |
1411 | /* |
1412 | * lxcapi_create: | |
1413 | * create a container with the given parameters. | |
1414 | * @c: container to be created. It has the lxcpath, name, and a starting | |
1415 | * configuration already set | |
1416 | * @t: the template to execute to instantiate the root filesystem and | |
1417 | * adjust the configuration. | |
1418 | * @bdevtype: backing store type to use. If NULL, dir will be used. | |
1419 | * @specs: additional parameters for the backing store, i.e. LVM vg to | |
1420 | * use. | |
1421 | * | |
1422 | * @argv: the arguments to pass to the template, terminated by NULL. If no | |
1423 | * arguments, you can just pass NULL. | |
1424 | */ | |
858377e4 | 1425 | static bool do_lxcapi_create(struct lxc_container *c, const char *t, |
dc23c1c8 | 1426 | const char *bdevtype, struct bdev_specs *specs, int flags, |
96b3cb40 SH |
1427 | char *const argv[]) |
1428 | { | |
a69aad27 | 1429 | bool ret = false; |
96b3cb40 | 1430 | pid_t pid; |
85db5535 | 1431 | char *tpath = NULL; |
cbee8106 | 1432 | int partial_fd; |
96b3cb40 SH |
1433 | |
1434 | if (!c) | |
1435 | return false; | |
1436 | ||
85db5535 DE |
1437 | if (t) { |
1438 | tpath = get_template_path(t); | |
1439 | if (!tpath) { | |
959aee9c | 1440 | ERROR("bad template: %s", t); |
85db5535 DE |
1441 | goto out; |
1442 | } | |
96b3cb40 SH |
1443 | } |
1444 | ||
cf465fe4 SH |
1445 | /* |
1446 | * If a template is passed in, and the rootfs already is defined in | |
1447 | * the container config and exists, then * caller is trying to create | |
1448 | * an existing container. Return an error, but do NOT delete the | |
1449 | * container. | |
1450 | */ | |
858377e4 | 1451 | if (do_lxcapi_is_defined(c) && c->lxc_conf && c->lxc_conf->rootfs.path && |
cf465fe4 SH |
1452 | access(c->lxc_conf->rootfs.path, F_OK) == 0 && tpath) { |
1453 | ERROR("Container %s:%s already exists", c->config_path, c->name); | |
6c6892b5 | 1454 | goto free_tpath; |
cf465fe4 SH |
1455 | } |
1456 | ||
6c6892b5 | 1457 | if (!c->lxc_conf) { |
858377e4 | 1458 | if (!do_lxcapi_load_config(c, lxc_global_config_value("lxc.default_config"))) { |
959aee9c | 1459 | ERROR("Error loading default configuration file %s", lxc_global_config_value("lxc.default_config")); |
6c6892b5 DE |
1460 | goto free_tpath; |
1461 | } | |
96b3cb40 SH |
1462 | } |
1463 | ||
6c6892b5 DE |
1464 | if (!create_container_dir(c)) |
1465 | goto free_tpath; | |
1466 | ||
0590e82c | 1467 | /* |
0590e82c SH |
1468 | * if both template and rootfs.path are set, template is setup as rootfs.path. |
1469 | * container is already created if we have a config and rootfs.path is accessible | |
1470 | */ | |
00370edd DW |
1471 | if (!c->lxc_conf->rootfs.path && !tpath) { |
1472 | /* no template passed in and rootfs does not exist */ | |
1473 | if (!c->save_config(c, NULL)) { | |
1474 | ERROR("failed to save starting configuration for %s\n", c->name); | |
1475 | goto out; | |
1476 | } | |
1477 | ret = true; | |
0590e82c | 1478 | goto out; |
00370edd | 1479 | } |
0590e82c SH |
1480 | if (c->lxc_conf->rootfs.path && access(c->lxc_conf->rootfs.path, F_OK) != 0) |
1481 | /* rootfs passed into configuration, but does not exist: error */ | |
1482 | goto out; | |
858377e4 | 1483 | if (do_lxcapi_is_defined(c) && c->lxc_conf->rootfs.path && !tpath) { |
0590e82c SH |
1484 | /* Rootfs already existed, user just wanted to save the |
1485 | * loaded configuration */ | |
0f4cdd77 DW |
1486 | if (!c->save_config(c, NULL)) |
1487 | ERROR("failed to save starting configuration for %s\n", c->name); | |
0590e82c SH |
1488 | ret = true; |
1489 | goto out; | |
a69aad27 | 1490 | } |
96b3cb40 SH |
1491 | |
1492 | /* Mark that this container is being created */ | |
1493 | if ((partial_fd = create_partial(c)) < 0) | |
1494 | goto out; | |
1495 | ||
1496 | /* no need to get disk lock bc we have the partial locked */ | |
1497 | ||
1498 | /* | |
1499 | * Create the backing store | |
1500 | * Note we can't do this in the same task as we use to execute the | |
1501 | * template because of the way zfs works. | |
1502 | * After you 'zfs create', zfs mounts the fs only in the initial | |
1503 | * namespace. | |
1504 | */ | |
1505 | pid = fork(); | |
1506 | if (pid < 0) { | |
959aee9c | 1507 | SYSERROR("failed to fork task for container creation template"); |
8eb5694b SH |
1508 | goto out_unlock; |
1509 | } | |
1510 | ||
96b3cb40 SH |
1511 | if (pid == 0) { // child |
1512 | struct bdev *bdev = NULL; | |
1513 | ||
1514 | if (!(bdev = do_bdev_create(c, bdevtype, specs))) { | |
1515 | ERROR("Error creating backing store type %s for %s", | |
1516 | bdevtype ? bdevtype : "(none)", c->name); | |
1517 | exit(1); | |
1518 | } | |
1519 | ||
1520 | /* save config file again to store the new rootfs location */ | |
858377e4 | 1521 | if (!do_lxcapi_save_config(c, NULL)) { |
959aee9c | 1522 | ERROR("failed to save starting configuration for %s", c->name); |
96b3cb40 SH |
1523 | // parent task won't see bdev in config so we delete it |
1524 | bdev->ops->umount(bdev); | |
1525 | bdev->ops->destroy(bdev); | |
1526 | exit(1); | |
1527 | } | |
1528 | exit(0); | |
1529 | } | |
1530 | if (wait_for_pid(pid) != 0) | |
a09295f8 | 1531 | goto out_unlock; |
96b3cb40 SH |
1532 | |
1533 | /* reload config to get the rootfs */ | |
a3b47c09 | 1534 | lxc_conf_free(c->lxc_conf); |
96b3cb40 SH |
1535 | c->lxc_conf = NULL; |
1536 | if (!load_config_locked(c, c->configfile)) | |
a09295f8 | 1537 | goto out_unlock; |
96b3cb40 | 1538 | |
dc23c1c8 | 1539 | if (!create_run_template(c, tpath, !!(flags & LXC_CREATE_QUIET), argv)) |
96b3cb40 SH |
1540 | goto out_unlock; |
1541 | ||
8eb5694b SH |
1542 | // now clear out the lxc_conf we have, reload from the created |
1543 | // container | |
858377e4 | 1544 | do_lxcapi_clear_config(c); |
3ce74686 | 1545 | |
9d65a487 KY |
1546 | if (t) { |
1547 | if (!prepend_lxc_header(c->configfile, tpath, argv)) { | |
1548 | ERROR("Error prepending header to configuration file"); | |
1549 | goto out_unlock; | |
1550 | } | |
3ce74686 | 1551 | } |
a69aad27 | 1552 | ret = load_config_locked(c, c->configfile); |
72d0e1cb SG |
1553 | |
1554 | out_unlock: | |
3e625e2d SH |
1555 | if (partial_fd >= 0) |
1556 | remove_partial(c, partial_fd); | |
72d0e1cb | 1557 | out: |
c55d4505 | 1558 | if (!ret) |
18aa217b | 1559 | container_destroy(c); |
6c6892b5 | 1560 | free_tpath: |
f10fad2f | 1561 | free(tpath); |
a69aad27 | 1562 | return ret; |
72d0e1cb SG |
1563 | } |
1564 | ||
858377e4 SH |
1565 | static bool lxcapi_create(struct lxc_container *c, const char *t, |
1566 | const char *bdevtype, struct bdev_specs *specs, int flags, | |
1567 | char *const argv[]) | |
1568 | { | |
1569 | bool ret; | |
1570 | current_config = c ? c->lxc_conf : NULL; | |
1571 | ret = do_lxcapi_create(c, t, bdevtype, specs, flags, argv); | |
1572 | current_config = NULL; | |
1573 | return ret; | |
1574 | } | |
1575 | ||
1576 | static bool do_lxcapi_reboot(struct lxc_container *c) | |
3e625e2d SH |
1577 | { |
1578 | pid_t pid; | |
dd267776 | 1579 | int rebootsignal = SIGINT; |
3e625e2d SH |
1580 | |
1581 | if (!c) | |
1582 | return false; | |
858377e4 | 1583 | if (!do_lxcapi_is_running(c)) |
3e625e2d | 1584 | return false; |
858377e4 | 1585 | pid = do_lxcapi_init_pid(c); |
3e625e2d SH |
1586 | if (pid <= 0) |
1587 | return false; | |
dd267776 BP |
1588 | if (c->lxc_conf && c->lxc_conf->rebootsignal) |
1589 | rebootsignal = c->lxc_conf->rebootsignal; | |
591614a7 CB |
1590 | if (kill(pid, rebootsignal) < 0) { |
1591 | WARN("Could not send signal %d to pid %d.", rebootsignal, pid); | |
3e625e2d | 1592 | return false; |
591614a7 | 1593 | } |
3e625e2d SH |
1594 | return true; |
1595 | ||
1596 | } | |
1597 | ||
858377e4 SH |
1598 | WRAP_API(bool, lxcapi_reboot) |
1599 | ||
1600 | static bool do_lxcapi_shutdown(struct lxc_container *c, int timeout) | |
72d0e1cb SG |
1601 | { |
1602 | bool retv; | |
1603 | pid_t pid; | |
f0f1d8c0 | 1604 | int haltsignal = SIGPWR; |
72d0e1cb SG |
1605 | |
1606 | if (!c) | |
1607 | return false; | |
1608 | ||
858377e4 | 1609 | if (!do_lxcapi_is_running(c)) |
72d0e1cb | 1610 | return true; |
858377e4 | 1611 | pid = do_lxcapi_init_pid(c); |
72d0e1cb SG |
1612 | if (pid <= 0) |
1613 | return true; | |
330ae3d3 CB |
1614 | |
1615 | /* Detect whether we should send SIGRTMIN + 3 (e.g. systemd). */ | |
1616 | if (task_blocking_signal(pid, (SIGRTMIN + 3))) | |
1617 | haltsignal = (SIGRTMIN + 3); | |
1618 | ||
b0227444 | 1619 | if (c->lxc_conf && c->lxc_conf->haltsignal) |
f0f1d8c0 | 1620 | haltsignal = c->lxc_conf->haltsignal; |
330ae3d3 CB |
1621 | |
1622 | INFO("Using signal number '%d' as halt signal.", haltsignal); | |
1623 | ||
591614a7 CB |
1624 | if (kill(pid, haltsignal) < 0) |
1625 | WARN("Could not send signal %d to pid %d.", haltsignal, pid); | |
1626 | ||
858377e4 | 1627 | retv = do_lxcapi_wait(c, "STOPPED", timeout); |
72d0e1cb SG |
1628 | return retv; |
1629 | } | |
1630 | ||
858377e4 SH |
1631 | WRAP_API_1(bool, lxcapi_shutdown, int) |
1632 | ||
1897e3bc | 1633 | static bool lxcapi_createl(struct lxc_container *c, const char *t, |
dc23c1c8 | 1634 | const char *bdevtype, struct bdev_specs *specs, int flags, ...) |
72d0e1cb SG |
1635 | { |
1636 | bool bret = false; | |
a0e93eeb | 1637 | char **args = NULL; |
72d0e1cb | 1638 | va_list ap; |
72d0e1cb SG |
1639 | |
1640 | if (!c) | |
1641 | return false; | |
1642 | ||
858377e4 SH |
1643 | current_config = c->lxc_conf; |
1644 | ||
72d0e1cb SG |
1645 | /* |
1646 | * since we're going to wait for create to finish, I don't think we | |
1647 | * need to get a copy of the arguments. | |
1648 | */ | |
dc23c1c8 | 1649 | va_start(ap, flags); |
a0e93eeb | 1650 | args = lxc_va_arg_list_to_argv(ap, 0, 0); |
72d0e1cb | 1651 | va_end(ap); |
a0e93eeb CS |
1652 | if (!args) { |
1653 | ERROR("Memory allocation error."); | |
1654 | goto out; | |
1655 | } | |
72d0e1cb | 1656 | |
858377e4 | 1657 | bret = do_lxcapi_create(c, t, bdevtype, specs, flags, args); |
72d0e1cb SG |
1658 | |
1659 | out: | |
a0e93eeb | 1660 | free(args); |
858377e4 | 1661 | current_config = NULL; |
72d0e1cb SG |
1662 | return bret; |
1663 | } | |
1664 | ||
6b0d5538 SH |
1665 | static void do_clear_unexp_config_line(struct lxc_conf *conf, const char *key) |
1666 | { | |
1667 | if (strcmp(key, "lxc.cgroup") == 0) | |
1668 | clear_unexp_config_line(conf, key, true); | |
1669 | else if (strcmp(key, "lxc.network") == 0) | |
1670 | clear_unexp_config_line(conf, key, true); | |
1671 | else if (strcmp(key, "lxc.hook") == 0) | |
1672 | clear_unexp_config_line(conf, key, true); | |
1673 | else | |
1674 | clear_unexp_config_line(conf, key, false); | |
1675 | if (!do_append_unexp_config_line(conf, key, "")) | |
1676 | WARN("Error clearing configuration for %s", key); | |
1677 | } | |
1678 | ||
6afd673f CB |
1679 | static bool do_lxcapi_clear_config_item(struct lxc_container *c, |
1680 | const char *key) | |
72d0e1cb | 1681 | { |
6afd673f CB |
1682 | int ret = 1; |
1683 | struct lxc_config_t *config; | |
72d0e1cb SG |
1684 | |
1685 | if (!c || !c->lxc_conf) | |
1686 | return false; | |
6afd673f | 1687 | |
5cee8c50 | 1688 | if (container_mem_lock(c)) |
72d0e1cb | 1689 | return false; |
6afd673f CB |
1690 | |
1691 | config = lxc_getconfig(key); | |
1692 | /* Verify that the config key exists and that it has a callback | |
1693 | * implemented. | |
1694 | */ | |
1695 | if (config && config->clr) | |
1696 | ret = config->clr(key, c->lxc_conf); | |
6b0d5538 SH |
1697 | if (!ret) |
1698 | do_clear_unexp_config_line(c->lxc_conf, key); | |
6afd673f | 1699 | |
5cee8c50 | 1700 | container_mem_unlock(c); |
72d0e1cb SG |
1701 | return ret == 0; |
1702 | } | |
1703 | ||
858377e4 SH |
1704 | WRAP_API_1(bool, lxcapi_clear_config_item, const char *) |
1705 | ||
e0f59189 | 1706 | static inline bool enter_net_ns(struct lxc_container *c) |
51d0854c | 1707 | { |
858377e4 | 1708 | pid_t pid = do_lxcapi_init_pid(c); |
ae22a220 | 1709 | |
0e6e3a41 | 1710 | if ((geteuid() != 0 || (c->lxc_conf && !lxc_list_empty(&c->lxc_conf->id_map))) && access("/proc/self/ns/user", F_OK) == 0) { |
51d0854c DY |
1711 | if (!switch_to_ns(pid, "user")) |
1712 | return false; | |
9c83a661 | 1713 | } |
51d0854c | 1714 | return switch_to_ns(pid, "net"); |
799f29ab ÇO |
1715 | } |
1716 | ||
9c88ff1f ÇO |
1717 | // used by qsort and bsearch functions for comparing names |
1718 | static inline int string_cmp(char **first, char **second) | |
1719 | { | |
1720 | return strcmp(*first, *second); | |
1721 | } | |
1722 | ||
1723 | // used by qsort and bsearch functions for comparing container names | |
1724 | static inline int container_cmp(struct lxc_container **first, struct lxc_container **second) | |
1725 | { | |
1726 | return strcmp((*first)->name, (*second)->name); | |
1727 | } | |
1728 | ||
1729 | static bool add_to_array(char ***names, char *cname, int pos) | |
1730 | { | |
1731 | char **newnames = realloc(*names, (pos+1) * sizeof(char *)); | |
1732 | if (!newnames) { | |
1733 | ERROR("Out of memory"); | |
1734 | return false; | |
1735 | } | |
1736 | ||
1737 | *names = newnames; | |
1738 | newnames[pos] = strdup(cname); | |
1739 | if (!newnames[pos]) | |
1740 | return false; | |
1741 | ||
1742 | // sort the arrray as we will use binary search on it | |
1743 | qsort(newnames, pos + 1, sizeof(char *), (int (*)(const void *,const void *))string_cmp); | |
1744 | ||
1745 | return true; | |
1746 | } | |
1747 | ||
2871830a | 1748 | static bool add_to_clist(struct lxc_container ***list, struct lxc_container *c, int pos, bool sort) |
9c88ff1f ÇO |
1749 | { |
1750 | struct lxc_container **newlist = realloc(*list, (pos+1) * sizeof(struct lxc_container *)); | |
1751 | if (!newlist) { | |
1752 | ERROR("Out of memory"); | |
1753 | return false; | |
1754 | } | |
1755 | ||
1756 | *list = newlist; | |
1757 | newlist[pos] = c; | |
1758 | ||
1759 | // sort the arrray as we will use binary search on it | |
2871830a DE |
1760 | if (sort) |
1761 | qsort(newlist, pos + 1, sizeof(struct lxc_container *), (int (*)(const void *,const void *))container_cmp); | |
9c88ff1f ÇO |
1762 | |
1763 | return true; | |
1764 | } | |
1765 | ||
1766 | static char** get_from_array(char ***names, char *cname, int size) | |
1767 | { | |
1768 | return (char **)bsearch(&cname, *names, size, sizeof(char *), (int (*)(const void *, const void *))string_cmp); | |
1769 | } | |
1770 | ||
1771 | ||
1772 | static bool array_contains(char ***names, char *cname, int size) { | |
1773 | if(get_from_array(names, cname, size) != NULL) | |
1774 | return true; | |
1775 | return false; | |
1776 | } | |
1777 | ||
1778 | static bool remove_from_array(char ***names, char *cname, int size) | |
1779 | { | |
1780 | char **result = get_from_array(names, cname, size); | |
1781 | if (result != NULL) { | |
1782 | free(result); | |
1783 | return true; | |
1784 | } | |
1785 | return false; | |
1786 | } | |
1787 | ||
858377e4 | 1788 | static char ** do_lxcapi_get_interfaces(struct lxc_container *c) |
799f29ab | 1789 | { |
ae22a220 ÇO |
1790 | pid_t pid; |
1791 | int i, count = 0, pipefd[2]; | |
9c88ff1f | 1792 | char **interfaces = NULL; |
ae22a220 | 1793 | char interface[IFNAMSIZ]; |
799f29ab | 1794 | |
ae22a220 ÇO |
1795 | if(pipe(pipefd) < 0) { |
1796 | SYSERROR("pipe failed"); | |
1797 | return NULL; | |
c868b261 ÇO |
1798 | } |
1799 | ||
ae22a220 ÇO |
1800 | pid = fork(); |
1801 | if (pid < 0) { | |
959aee9c | 1802 | SYSERROR("failed to fork task to get interfaces information"); |
ae22a220 ÇO |
1803 | close(pipefd[0]); |
1804 | close(pipefd[1]); | |
1805 | return NULL; | |
1806 | } | |
799f29ab | 1807 | |
ae22a220 ÇO |
1808 | if (pid == 0) { // child |
1809 | int ret = 1, nbytes; | |
1810 | struct ifaddrs *interfaceArray = NULL, *tempIfAddr = NULL; | |
1811 | ||
1812 | /* close the read-end of the pipe */ | |
1813 | close(pipefd[0]); | |
1814 | ||
e0f59189 | 1815 | if (!enter_net_ns(c)) { |
ae22a220 ÇO |
1816 | SYSERROR("failed to enter namespace"); |
1817 | goto out; | |
1818 | } | |
1819 | ||
1820 | /* Grab the list of interfaces */ | |
1821 | if (getifaddrs(&interfaceArray)) { | |
1822 | SYSERROR("failed to get interfaces list"); | |
1823 | goto out; | |
1824 | } | |
1825 | ||
1826 | /* Iterate through the interfaces */ | |
1827 | for (tempIfAddr = interfaceArray; tempIfAddr != NULL; tempIfAddr = tempIfAddr->ifa_next) { | |
1828 | nbytes = write(pipefd[1], tempIfAddr->ifa_name, IFNAMSIZ); | |
1829 | if (nbytes < 0) { | |
1830 | ERROR("write failed"); | |
1831 | goto out; | |
1832 | } | |
1833 | count++; | |
1834 | } | |
1835 | ret = 0; | |
1836 | ||
1837 | out: | |
1838 | if (interfaceArray) | |
1839 | freeifaddrs(interfaceArray); | |
1840 | ||
1841 | /* close the write-end of the pipe, thus sending EOF to the reader */ | |
1842 | close(pipefd[1]); | |
1843 | exit(ret); | |
799f29ab ÇO |
1844 | } |
1845 | ||
ae22a220 ÇO |
1846 | /* close the write-end of the pipe */ |
1847 | close(pipefd[1]); | |
1848 | ||
358afd84 | 1849 | while (read(pipefd[0], &interface, IFNAMSIZ) == IFNAMSIZ) { |
ae22a220 ÇO |
1850 | if (array_contains(&interfaces, interface, count)) |
1851 | continue; | |
799f29ab | 1852 | |
ae22a220 ÇO |
1853 | if(!add_to_array(&interfaces, interface, count)) |
1854 | ERROR("PARENT: add_to_array failed"); | |
9c88ff1f ÇO |
1855 | count++; |
1856 | } | |
799f29ab | 1857 | |
ae22a220 ÇO |
1858 | if (wait_for_pid(pid) != 0) { |
1859 | for(i=0;i<count;i++) | |
1860 | free(interfaces[i]); | |
1861 | free(interfaces); | |
1862 | interfaces = NULL; | |
1863 | } | |
9c88ff1f | 1864 | |
ae22a220 ÇO |
1865 | /* close the read-end of the pipe */ |
1866 | close(pipefd[0]); | |
799f29ab | 1867 | |
9c88ff1f ÇO |
1868 | /* Append NULL to the array */ |
1869 | if(interfaces) | |
1870 | interfaces = (char **)lxc_append_null_to_array((void **)interfaces, count); | |
799f29ab | 1871 | |
9c88ff1f | 1872 | return interfaces; |
799f29ab ÇO |
1873 | } |
1874 | ||
858377e4 SH |
1875 | WRAP_API(char **, lxcapi_get_interfaces) |
1876 | ||
1877 | static char** do_lxcapi_get_ips(struct lxc_container *c, const char* interface, const char* family, int scope) | |
799f29ab | 1878 | { |
ae22a220 ÇO |
1879 | pid_t pid; |
1880 | int i, count = 0, pipefd[2]; | |
9c88ff1f | 1881 | char **addresses = NULL; |
ae22a220 | 1882 | char address[INET6_ADDRSTRLEN]; |
799f29ab | 1883 | |
ae22a220 ÇO |
1884 | if(pipe(pipefd) < 0) { |
1885 | SYSERROR("pipe failed"); | |
1886 | return NULL; | |
c868b261 ÇO |
1887 | } |
1888 | ||
ae22a220 ÇO |
1889 | pid = fork(); |
1890 | if (pid < 0) { | |
959aee9c | 1891 | SYSERROR("failed to fork task to get container ips"); |
ae22a220 ÇO |
1892 | close(pipefd[0]); |
1893 | close(pipefd[1]); | |
1894 | return NULL; | |
9c83a661 SG |
1895 | } |
1896 | ||
ae22a220 ÇO |
1897 | if (pid == 0) { // child |
1898 | int ret = 1, nbytes; | |
1899 | struct ifaddrs *interfaceArray = NULL, *tempIfAddr = NULL; | |
1900 | char addressOutputBuffer[INET6_ADDRSTRLEN]; | |
1901 | void *tempAddrPtr = NULL; | |
1902 | char *address = NULL; | |
fe218ca3 | 1903 | |
ae22a220 ÇO |
1904 | /* close the read-end of the pipe */ |
1905 | close(pipefd[0]); | |
1906 | ||
e0f59189 | 1907 | if (!enter_net_ns(c)) { |
ae22a220 ÇO |
1908 | SYSERROR("failed to enter namespace"); |
1909 | goto out; | |
9c83a661 | 1910 | } |
ae22a220 ÇO |
1911 | |
1912 | /* Grab the list of interfaces */ | |
1913 | if (getifaddrs(&interfaceArray)) { | |
1914 | SYSERROR("failed to get interfaces list"); | |
1915 | goto out; | |
1916 | } | |
1917 | ||
1918 | /* Iterate through the interfaces */ | |
1919 | for (tempIfAddr = interfaceArray; tempIfAddr != NULL; tempIfAddr = tempIfAddr->ifa_next) { | |
1920 | if (tempIfAddr->ifa_addr == NULL) | |
9c83a661 SG |
1921 | continue; |
1922 | ||
ae22a220 ÇO |
1923 | if(tempIfAddr->ifa_addr->sa_family == AF_INET) { |
1924 | if (family && strcmp(family, "inet")) | |
1925 | continue; | |
1926 | tempAddrPtr = &((struct sockaddr_in *)tempIfAddr->ifa_addr)->sin_addr; | |
1927 | } | |
1928 | else { | |
1929 | if (family && strcmp(family, "inet6")) | |
1930 | continue; | |
1931 | ||
1932 | if (((struct sockaddr_in6 *)tempIfAddr->ifa_addr)->sin6_scope_id != scope) | |
1933 | continue; | |
1934 | ||
1935 | tempAddrPtr = &((struct sockaddr_in6 *)tempIfAddr->ifa_addr)->sin6_addr; | |
1936 | } | |
1937 | ||
1938 | if (interface && strcmp(interface, tempIfAddr->ifa_name)) | |
1939 | continue; | |
1940 | else if (!interface && strcmp("lo", tempIfAddr->ifa_name) == 0) | |
9c83a661 SG |
1941 | continue; |
1942 | ||
ae22a220 ÇO |
1943 | address = (char *)inet_ntop(tempIfAddr->ifa_addr->sa_family, |
1944 | tempAddrPtr, | |
1945 | addressOutputBuffer, | |
1946 | sizeof(addressOutputBuffer)); | |
1947 | if (!address) | |
1948 | continue; | |
1949 | ||
1950 | nbytes = write(pipefd[1], address, INET6_ADDRSTRLEN); | |
1951 | if (nbytes < 0) { | |
1952 | ERROR("write failed"); | |
1953 | goto out; | |
1954 | } | |
1955 | count++; | |
9c83a661 | 1956 | } |
ae22a220 | 1957 | ret = 0; |
9c83a661 | 1958 | |
ae22a220 ÇO |
1959 | out: |
1960 | if(interfaceArray) | |
1961 | freeifaddrs(interfaceArray); | |
9c83a661 | 1962 | |
ae22a220 ÇO |
1963 | /* close the write-end of the pipe, thus sending EOF to the reader */ |
1964 | close(pipefd[1]); | |
1965 | exit(ret); | |
6849cb5b | 1966 | } |
9c83a661 | 1967 | |
ae22a220 ÇO |
1968 | /* close the write-end of the pipe */ |
1969 | close(pipefd[1]); | |
1970 | ||
358afd84 | 1971 | while (read(pipefd[0], &address, INET6_ADDRSTRLEN) == INET6_ADDRSTRLEN) { |
9c88ff1f | 1972 | if(!add_to_array(&addresses, address, count)) |
ae22a220 | 1973 | ERROR("PARENT: add_to_array failed"); |
9c88ff1f | 1974 | count++; |
9c83a661 SG |
1975 | } |
1976 | ||
ae22a220 ÇO |
1977 | if (wait_for_pid(pid) != 0) { |
1978 | for(i=0;i<count;i++) | |
1979 | free(addresses[i]); | |
1980 | free(addresses); | |
1981 | addresses = NULL; | |
1982 | } | |
9c83a661 | 1983 | |
ae22a220 ÇO |
1984 | /* close the read-end of the pipe */ |
1985 | close(pipefd[0]); | |
9c83a661 SG |
1986 | |
1987 | /* Append NULL to the array */ | |
9c88ff1f ÇO |
1988 | if(addresses) |
1989 | addresses = (char **)lxc_append_null_to_array((void **)addresses, count); | |
9c83a661 SG |
1990 | |
1991 | return addresses; | |
1992 | } | |
1993 | ||
858377e4 SH |
1994 | WRAP_API_3(char **, lxcapi_get_ips, const char *, const char *, int) |
1995 | ||
1996 | static int do_lxcapi_get_config_item(struct lxc_container *c, const char *key, char *retv, int inlen) | |
72d0e1cb | 1997 | { |
fce687aa CB |
1998 | int ret = -1; |
1999 | struct lxc_config_t *config; | |
72d0e1cb SG |
2000 | |
2001 | if (!c || !c->lxc_conf) | |
2002 | return -1; | |
fce687aa | 2003 | |
5cee8c50 | 2004 | if (container_mem_lock(c)) |
72d0e1cb | 2005 | return -1; |
fce687aa CB |
2006 | |
2007 | config = lxc_getconfig(key); | |
2008 | /* Verify that the config key exists and that it has a callback | |
2009 | * implemented. | |
2010 | */ | |
2011 | if (config && config->get) | |
6bede808 | 2012 | ret = config->get(key, retv, inlen, c->lxc_conf); |
fce687aa | 2013 | |
5cee8c50 | 2014 | container_mem_unlock(c); |
72d0e1cb SG |
2015 | return ret; |
2016 | } | |
2017 | ||
858377e4 SH |
2018 | WRAP_API_3(int, lxcapi_get_config_item, const char *, char *, int) |
2019 | ||
2020 | static char* do_lxcapi_get_running_config_item(struct lxc_container *c, const char *key) | |
8ac18377 ÇO |
2021 | { |
2022 | char *ret; | |
2023 | ||
2024 | if (!c || !c->lxc_conf) | |
2025 | return NULL; | |
2026 | if (container_mem_lock(c)) | |
2027 | return NULL; | |
858377e4 | 2028 | ret = lxc_cmd_get_config_item(c->name, key, do_lxcapi_get_config_path(c)); |
8ac18377 ÇO |
2029 | container_mem_unlock(c); |
2030 | return ret; | |
2031 | } | |
2032 | ||
858377e4 SH |
2033 | WRAP_API_1(char *, lxcapi_get_running_config_item, const char *) |
2034 | ||
2035 | static int do_lxcapi_get_keys(struct lxc_container *c, const char *key, char *retv, int inlen) | |
72d0e1cb SG |
2036 | { |
2037 | if (!key) | |
2038 | return lxc_listconfigs(retv, inlen); | |
2039 | /* | |
2040 | * Support 'lxc.network.<idx>', i.e. 'lxc.network.0' | |
2041 | * This is an intelligent result to show which keys are valid given | |
2042 | * the type of nic it is | |
2043 | */ | |
2044 | if (!c || !c->lxc_conf) | |
2045 | return -1; | |
5cee8c50 | 2046 | if (container_mem_lock(c)) |
72d0e1cb SG |
2047 | return -1; |
2048 | int ret = -1; | |
2049 | if (strncmp(key, "lxc.network.", 12) == 0) | |
6849cb5b | 2050 | ret = lxc_list_nicconfigs(c->lxc_conf, key, retv, inlen); |
5cee8c50 | 2051 | container_mem_unlock(c); |
72d0e1cb SG |
2052 | return ret; |
2053 | } | |
2054 | ||
858377e4 SH |
2055 | WRAP_API_3(int, lxcapi_get_keys, const char *, char *, int) |
2056 | ||
2057 | static bool do_lxcapi_save_config(struct lxc_container *c, const char *alt_file) | |
72d0e1cb | 2058 | { |
39dc698c SH |
2059 | FILE *fout; |
2060 | bool ret = false, need_disklock = false; | |
2061 | int lret; | |
2062 | ||
72d0e1cb SG |
2063 | if (!alt_file) |
2064 | alt_file = c->configfile; | |
2065 | if (!alt_file) | |
6849cb5b | 2066 | return false; // should we write to stdout if no file is specified? |
39dc698c SH |
2067 | |
2068 | // If we haven't yet loaded a config, load the stock config | |
2069 | if (!c->lxc_conf) { | |
858377e4 | 2070 | if (!do_lxcapi_load_config(c, lxc_global_config_value("lxc.default_config"))) { |
959aee9c | 2071 | ERROR("Error loading default configuration file %s while saving %s", lxc_global_config_value("lxc.default_config"), c->name); |
72d0e1cb SG |
2072 | return false; |
2073 | } | |
39dc698c | 2074 | } |
72d0e1cb | 2075 | |
5a3d2e1e SG |
2076 | if (!create_container_dir(c)) |
2077 | return false; | |
2078 | ||
39dc698c SH |
2079 | /* |
2080 | * If we're writing to the container's config file, take the | |
2081 | * disk lock. Otherwise just take the memlock to protect the | |
2082 | * struct lxc_container while we're traversing it. | |
2083 | */ | |
2084 | if (strcmp(c->configfile, alt_file) == 0) | |
2085 | need_disklock = true; | |
2086 | ||
2087 | if (need_disklock) | |
2088 | lret = container_disk_lock(c); | |
2089 | else | |
2090 | lret = container_mem_lock(c); | |
2091 | ||
2092 | if (lret) | |
72d0e1cb | 2093 | return false; |
39dc698c SH |
2094 | |
2095 | fout = fopen(alt_file, "w"); | |
2096 | if (!fout) | |
2097 | goto out; | |
6b0d5538 | 2098 | write_config(fout, c->lxc_conf); |
72d0e1cb | 2099 | fclose(fout); |
39dc698c SH |
2100 | ret = true; |
2101 | ||
2102 | out: | |
2103 | if (need_disklock) | |
2104 | container_disk_unlock(c); | |
2105 | else | |
2106 | container_mem_unlock(c); | |
2107 | return ret; | |
72d0e1cb SG |
2108 | } |
2109 | ||
858377e4 SH |
2110 | WRAP_API_1(bool, lxcapi_save_config, const char *) |
2111 | ||
0ea055b3 CB |
2112 | |
2113 | static bool mod_rdep(struct lxc_container *c0, struct lxc_container *c, bool inc) | |
dfb31b25 | 2114 | { |
0ea055b3 CB |
2115 | FILE *f1; |
2116 | struct stat fbuf; | |
42342bed CB |
2117 | void *buf = NULL; |
2118 | char *del = NULL; | |
dfb31b25 | 2119 | char path[MAXPATHLEN]; |
0ea055b3 CB |
2120 | char newpath[MAXPATHLEN]; |
2121 | int fd, ret, n = 0, v = 0; | |
dfb31b25 | 2122 | bool bret = false; |
42342bed | 2123 | size_t len = 0, bytes = 0; |
dfb31b25 | 2124 | |
0ea055b3 | 2125 | if (container_disk_lock(c0)) |
dfb31b25 | 2126 | return false; |
0ea055b3 CB |
2127 | |
2128 | ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_snapshots", c0->config_path, c0->name); | |
dfb31b25 SH |
2129 | if (ret < 0 || ret > MAXPATHLEN) |
2130 | goto out; | |
0ea055b3 CB |
2131 | ret = snprintf(newpath, MAXPATHLEN, "%s\n%s\n", c->config_path, c->name); |
2132 | if (ret < 0 || ret > MAXPATHLEN) | |
dfb31b25 | 2133 | goto out; |
0ea055b3 CB |
2134 | |
2135 | /* If we find an lxc-snapshot file using the old format only listing the | |
2136 | * number of snapshots we will keep using it. */ | |
2137 | f1 = fopen(path, "r"); | |
2138 | if (f1) { | |
2139 | n = fscanf(f1, "%d", &v); | |
2140 | fclose(f1); | |
2141 | if (n == 1 && v == 0) { | |
2142 | remove(path); | |
2143 | n = 0; | |
2144 | } | |
dfb31b25 | 2145 | } |
0ea055b3 CB |
2146 | if (n == 1) { |
2147 | v += inc ? 1 : -1; | |
2148 | f1 = fopen(path, "w"); | |
2149 | if (!f1) | |
2150 | goto out; | |
2151 | if (fprintf(f1, "%d\n", v) < 0) { | |
2152 | ERROR("Error writing new snapshots value"); | |
2153 | fclose(f1); | |
2154 | goto out; | |
2155 | } | |
2156 | ret = fclose(f1); | |
2157 | if (ret != 0) { | |
2158 | SYSERROR("Error writing to or closing snapshots file"); | |
2159 | goto out; | |
2160 | } | |
2161 | } else { | |
2162 | /* Here we know that we have or can use an lxc-snapshot file | |
2163 | * using the new format. */ | |
2164 | if (inc) { | |
2165 | f1 = fopen(path, "a"); | |
2166 | if (!f1) | |
2167 | goto out; | |
2168 | ||
2169 | if (fprintf(f1, "%s", newpath) < 0) { | |
2170 | ERROR("Error writing new snapshots entry"); | |
2171 | ret = fclose(f1); | |
2172 | if (ret != 0) | |
2173 | SYSERROR("Error writing to or closing snapshots file"); | |
2174 | goto out; | |
2175 | } | |
2176 | ||
2177 | ret = fclose(f1); | |
2178 | if (ret != 0) { | |
2179 | SYSERROR("Error writing to or closing snapshots file"); | |
2180 | goto out; | |
2181 | } | |
2182 | } else if (!inc) { | |
d028235d SG |
2183 | if ((fd = open(path, O_RDWR | O_CLOEXEC)) < 0) |
2184 | goto out; | |
2185 | ||
2186 | if (fstat(fd, &fbuf) < 0) { | |
2187 | close(fd); | |
2188 | goto out; | |
2189 | } | |
2190 | ||
2191 | if (fbuf.st_size != 0) { | |
d028235d | 2192 | |
25086a5f | 2193 | buf = lxc_strmmap(NULL, fbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
d028235d SG |
2194 | if (buf == MAP_FAILED) { |
2195 | SYSERROR("Failed to create mapping %s", path); | |
2196 | close(fd); | |
2197 | goto out; | |
2198 | } | |
2199 | ||
2200 | len = strlen(newpath); | |
2201 | while ((del = strstr((char *)buf, newpath))) { | |
2202 | memmove(del, del + len, strlen(del) - len + 1); | |
2203 | bytes += len; | |
2204 | } | |
2205 | ||
25086a5f | 2206 | lxc_strmunmap(buf, fbuf.st_size); |
d028235d SG |
2207 | if (ftruncate(fd, fbuf.st_size - bytes) < 0) { |
2208 | SYSERROR("Failed to truncate file %s", path); | |
2209 | close(fd); | |
2210 | goto out; | |
2211 | } | |
2212 | } | |
2213 | close(fd); | |
2214 | } | |
0ea055b3 CB |
2215 | |
2216 | /* If the lxc-snapshot file is empty, remove it. */ | |
2217 | if (stat(path, &fbuf) < 0) | |
2218 | goto out; | |
d028235d SG |
2219 | if (!fbuf.st_size) { |
2220 | remove(path); | |
0ea055b3 | 2221 | } |
dfb31b25 SH |
2222 | } |
2223 | ||
2224 | bret = true; | |
2225 | ||
2226 | out: | |
0ea055b3 | 2227 | container_disk_unlock(c0); |
dfb31b25 SH |
2228 | return bret; |
2229 | } | |
2230 | ||
2231 | static void strip_newline(char *p) | |
2232 | { | |
2233 | size_t len = strlen(p); | |
2234 | if (len < 1) | |
2235 | return; | |
2236 | if (p[len-1] == '\n') | |
2237 | p[len-1] = '\0'; | |
2238 | } | |
2239 | ||
d825fff3 | 2240 | void mod_all_rdeps(struct lxc_container *c, bool inc) |
dfb31b25 SH |
2241 | { |
2242 | struct lxc_container *p; | |
2243 | char *lxcpath = NULL, *lxcname = NULL, path[MAXPATHLEN]; | |
2244 | size_t pathlen = 0, namelen = 0; | |
2245 | FILE *f; | |
2246 | int ret; | |
2247 | ||
2248 | ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_rdepends", | |
2249 | c->config_path, c->name); | |
2250 | if (ret < 0 || ret >= MAXPATHLEN) { | |
2251 | ERROR("Path name too long"); | |
2252 | return; | |
2253 | } | |
025ed0f3 | 2254 | f = fopen(path, "r"); |
025ed0f3 | 2255 | if (f == NULL) |
dfb31b25 SH |
2256 | return; |
2257 | while (getline(&lxcpath, &pathlen, f) != -1) { | |
2258 | if (getline(&lxcname, &namelen, f) == -1) { | |
959aee9c | 2259 | ERROR("badly formatted file %s", path); |
dfb31b25 SH |
2260 | goto out; |
2261 | } | |
2262 | strip_newline(lxcpath); | |
2263 | strip_newline(lxcname); | |
2264 | if ((p = lxc_container_new(lxcname, lxcpath)) == NULL) { | |
2265 | ERROR("Unable to find dependent container %s:%s", | |
2266 | lxcpath, lxcname); | |
2267 | continue; | |
2268 | } | |
0ea055b3 CB |
2269 | if (!mod_rdep(p, c, inc)) |
2270 | ERROR("Failed to update snapshots file for %s:%s", | |
dfb31b25 SH |
2271 | lxcpath, lxcname); |
2272 | lxc_container_put(p); | |
2273 | } | |
2274 | out: | |
f10fad2f ME |
2275 | free(lxcpath); |
2276 | free(lxcname); | |
dfb31b25 SH |
2277 | fclose(f); |
2278 | } | |
2279 | ||
18aa217b | 2280 | static bool has_fs_snapshots(struct lxc_container *c) |
dfb31b25 | 2281 | { |
0ea055b3 | 2282 | FILE *f; |
dfb31b25 SH |
2283 | char path[MAXPATHLEN]; |
2284 | int ret, v; | |
0ea055b3 | 2285 | struct stat fbuf; |
dfb31b25 SH |
2286 | bool bret = false; |
2287 | ||
2288 | ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_snapshots", c->config_path, | |
2289 | c->name); | |
2290 | if (ret < 0 || ret > MAXPATHLEN) | |
2291 | goto out; | |
0ea055b3 CB |
2292 | /* If the file doesn't exist there are no snapshots. */ |
2293 | if (stat(path, &fbuf) < 0) | |
dfb31b25 | 2294 | goto out; |
0ea055b3 CB |
2295 | v = fbuf.st_size; |
2296 | if (v != 0) { | |
2297 | f = fopen(path, "r"); | |
2298 | if (!f) | |
2299 | goto out; | |
2300 | ret = fscanf(f, "%d", &v); | |
2301 | fclose(f); | |
2302 | // TODO: Figure out what to do with the return value of fscanf. | |
2303 | if (ret != 1) | |
2304 | INFO("Container uses new lxc-snapshots format %s", path); | |
2305 | } | |
dfb31b25 SH |
2306 | bret = v != 0; |
2307 | ||
2308 | out: | |
2309 | return bret; | |
2310 | } | |
2311 | ||
18aa217b SH |
2312 | static bool has_snapshots(struct lxc_container *c) |
2313 | { | |
2314 | char path[MAXPATHLEN]; | |
74f96976 | 2315 | struct dirent *direntp; |
18aa217b SH |
2316 | int count=0; |
2317 | DIR *dir; | |
2318 | ||
2319 | if (!get_snappath_dir(c, path)) | |
2320 | return false; | |
2321 | dir = opendir(path); | |
2322 | if (!dir) | |
2323 | return false; | |
74f96976 | 2324 | while ((direntp = readdir(dir))) { |
18aa217b SH |
2325 | if (!direntp) |
2326 | break; | |
2327 | ||
2328 | if (!strcmp(direntp->d_name, ".")) | |
2329 | continue; | |
2330 | ||
2331 | if (!strcmp(direntp->d_name, "..")) | |
2332 | continue; | |
2333 | count++; | |
2334 | break; | |
2335 | } | |
2336 | closedir(dir); | |
2337 | return count > 0; | |
2338 | } | |
2339 | ||
297c2d58 | 2340 | static bool do_destroy_container(struct lxc_conf *conf) { |
d028235d | 2341 | if (am_unpriv()) { |
c9b7c33e CB |
2342 | if (userns_exec_1(conf, bdev_destroy_wrapper, conf, |
2343 | "bdev_destroy_wrapper") < 0) | |
d028235d SG |
2344 | return false; |
2345 | return true; | |
2346 | } | |
2347 | return bdev_destroy(conf); | |
297c2d58 CB |
2348 | } |
2349 | ||
4355ab5f SH |
2350 | static int lxc_rmdir_onedev_wrapper(void *data) |
2351 | { | |
2352 | char *arg = (char *) data; | |
18aa217b | 2353 | return lxc_rmdir_onedev(arg, "snaps"); |
4355ab5f SH |
2354 | } |
2355 | ||
18aa217b | 2356 | static bool container_destroy(struct lxc_container *c) |
72d0e1cb | 2357 | { |
c868b261 | 2358 | bool bret = false; |
297c2d58 | 2359 | int ret = 0; |
a70a69e8 | 2360 | struct lxc_conf *conf; |
72d0e1cb | 2361 | |
858377e4 | 2362 | if (!c || !do_lxcapi_is_defined(c)) |
5a3d2e1e SG |
2363 | return false; |
2364 | ||
a70a69e8 | 2365 | conf = c->lxc_conf; |
3bc449ed | 2366 | if (container_disk_lock(c)) |
72d0e1cb | 2367 | return false; |
72d0e1cb | 2368 | |
39dc698c | 2369 | if (!is_stopped(c)) { |
60bf62d4 SH |
2370 | // we should queue some sort of error - in c->error_string? |
2371 | ERROR("container %s is not stopped", c->name); | |
2372 | goto out; | |
72d0e1cb SG |
2373 | } |
2374 | ||
a70a69e8 | 2375 | if (conf && !lxc_list_empty(&conf->hooks[LXCHOOK_DESTROY])) { |
37cf711b | 2376 | /* Start of environment variable setup for hooks */ |
ab7efcf5 | 2377 | if (c->name && setenv("LXC_NAME", c->name, 1)) { |
37cf711b SY |
2378 | SYSERROR("failed to set environment variable for container name"); |
2379 | } | |
ab7efcf5 | 2380 | if (conf->rcfile && setenv("LXC_CONFIG_FILE", conf->rcfile, 1)) { |
37cf711b SY |
2381 | SYSERROR("failed to set environment variable for config path"); |
2382 | } | |
ab7efcf5 | 2383 | if (conf->rootfs.mount && setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1)) { |
37cf711b SY |
2384 | SYSERROR("failed to set environment variable for rootfs mount"); |
2385 | } | |
ab7efcf5 | 2386 | if (conf->rootfs.path && setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1)) { |
37cf711b SY |
2387 | SYSERROR("failed to set environment variable for rootfs mount"); |
2388 | } | |
2389 | if (conf->console.path && setenv("LXC_CONSOLE", conf->console.path, 1)) { | |
2390 | SYSERROR("failed to set environment variable for console path"); | |
2391 | } | |
2392 | if (conf->console.log_path && setenv("LXC_CONSOLE_LOGPATH", conf->console.log_path, 1)) { | |
2393 | SYSERROR("failed to set environment variable for console log"); | |
2394 | } | |
2395 | /* End of environment variable setup for hooks */ | |
2396 | ||
2397 | if (run_lxc_hooks(c->name, "destroy", conf, c->get_config_path(c), NULL)) { | |
2398 | ERROR("Error executing clone hook for %s", c->name); | |
2399 | goto out; | |
2400 | } | |
2401 | } | |
2402 | ||
2403 | if (current_config && conf == current_config) { | |
858377e4 | 2404 | current_config = NULL; |
37cf711b SY |
2405 | if (conf->logfd != -1) { |
2406 | close(conf->logfd); | |
2407 | conf->logfd = -1; | |
858377e4 SH |
2408 | } |
2409 | } | |
2410 | ||
d028235d SG |
2411 | if (conf && conf->rootfs.path && conf->rootfs.mount) { |
2412 | if (!do_destroy_container(conf)) { | |
2413 | ERROR("Error destroying rootfs for %s", c->name); | |
2414 | goto out; | |
2415 | } | |
2416 | INFO("Destroyed rootfs for %s", c->name); | |
2417 | } | |
60bf62d4 | 2418 | |
dfb31b25 SH |
2419 | mod_all_rdeps(c, false); |
2420 | ||
858377e4 | 2421 | const char *p1 = do_lxcapi_get_config_path(c); |
60bf62d4 SH |
2422 | char *path = alloca(strlen(p1) + strlen(c->name) + 2); |
2423 | sprintf(path, "%s/%s", p1, c->name); | |
c868b261 | 2424 | if (am_unpriv()) |
c9b7c33e CB |
2425 | ret = userns_exec_1(conf, lxc_rmdir_onedev_wrapper, path, |
2426 | "lxc_rmdir_onedev_wrapper"); | |
4355ab5f | 2427 | else |
18aa217b | 2428 | ret = lxc_rmdir_onedev(path, "snaps"); |
4355ab5f | 2429 | if (ret < 0) { |
60bf62d4 SH |
2430 | ERROR("Error destroying container directory for %s", c->name); |
2431 | goto out; | |
2432 | } | |
d028235d | 2433 | INFO("Destroyed directory for %s", c->name); |
297c2d58 | 2434 | |
fef48dc9 | 2435 | bret = true; |
60bf62d4 SH |
2436 | |
2437 | out: | |
3bc449ed | 2438 | container_disk_unlock(c); |
fef48dc9 | 2439 | return bret; |
72d0e1cb SG |
2440 | } |
2441 | ||
858377e4 | 2442 | static bool do_lxcapi_destroy(struct lxc_container *c) |
18aa217b SH |
2443 | { |
2444 | if (!c || !lxcapi_is_defined(c)) | |
2445 | return false; | |
2446 | if (has_snapshots(c)) { | |
2447 | ERROR("Container %s has snapshots; not removing", c->name); | |
2448 | return false; | |
2449 | } | |
2450 | ||
2451 | if (has_fs_snapshots(c)) { | |
2452 | ERROR("container %s has snapshots on its rootfs", c->name); | |
2453 | return false; | |
2454 | } | |
2455 | ||
2456 | return container_destroy(c); | |
2457 | } | |
2458 | ||
858377e4 | 2459 | WRAP_API(bool, lxcapi_destroy) |
18aa217b | 2460 | |
858377e4 | 2461 | static bool do_lxcapi_destroy_with_snapshots(struct lxc_container *c) |
18aa217b SH |
2462 | { |
2463 | if (!c || !lxcapi_is_defined(c)) | |
2464 | return false; | |
2465 | if (!lxcapi_snapshot_destroy_all(c)) { | |
2466 | ERROR("Error deleting all snapshots"); | |
2467 | return false; | |
2468 | } | |
2469 | return lxcapi_destroy(c); | |
2470 | } | |
2471 | ||
858377e4 SH |
2472 | WRAP_API(bool, lxcapi_destroy_with_snapshots) |
2473 | ||
96532523 SH |
2474 | static bool set_config_item_locked(struct lxc_container *c, const char *key, const char *v) |
2475 | { | |
2476 | struct lxc_config_t *config; | |
2477 | ||
2478 | if (!c->lxc_conf) | |
2479 | c->lxc_conf = lxc_conf_init(); | |
6b0d5538 | 2480 | if (!c->lxc_conf) |
96532523 SH |
2481 | return false; |
2482 | config = lxc_getconfig(key); | |
2483 | if (!config) | |
2484 | return false; | |
d37f7cd7 | 2485 | if (config->set(key, v, c->lxc_conf) != 0) |
f979ac15 | 2486 | return false; |
6b0d5538 | 2487 | return do_append_unexp_config_line(c->lxc_conf, key, v); |
96532523 SH |
2488 | } |
2489 | ||
858377e4 | 2490 | static bool do_lxcapi_set_config_item(struct lxc_container *c, const char *key, const char *v) |
72d0e1cb | 2491 | { |
72d0e1cb | 2492 | bool b = false; |
72d0e1cb SG |
2493 | |
2494 | if (!c) | |
2495 | return false; | |
2496 | ||
5cee8c50 | 2497 | if (container_mem_lock(c)) |
72d0e1cb SG |
2498 | return false; |
2499 | ||
96532523 | 2500 | b = set_config_item_locked(c, key, v); |
72d0e1cb | 2501 | |
5cee8c50 | 2502 | container_mem_unlock(c); |
72d0e1cb SG |
2503 | return b; |
2504 | } | |
2505 | ||
858377e4 SH |
2506 | WRAP_API_2(bool, lxcapi_set_config_item, const char *, const char *) |
2507 | ||
72d0e1cb SG |
2508 | static char *lxcapi_config_file_name(struct lxc_container *c) |
2509 | { | |
2510 | if (!c || !c->configfile) | |
2511 | return NULL; | |
2512 | return strdup(c->configfile); | |
2513 | } | |
2514 | ||
2a59a681 SH |
2515 | static const char *lxcapi_get_config_path(struct lxc_container *c) |
2516 | { | |
2517 | if (!c || !c->config_path) | |
2518 | return NULL; | |
2519 | return (const char *)(c->config_path); | |
2520 | } | |
2521 | ||
afeecbba SH |
2522 | /* |
2523 | * not for export | |
2524 | * Just recalculate the c->configfile based on the | |
2525 | * c->config_path, which must be set. | |
2526 | * The lxc_container must be locked or not yet public. | |
2527 | */ | |
2528 | static bool set_config_filename(struct lxc_container *c) | |
2529 | { | |
2530 | char *newpath; | |
2531 | int len, ret; | |
2532 | ||
2533 | if (!c->config_path) | |
2534 | return false; | |
2535 | ||
2536 | /* $lxc_path + "/" + c->name + "/" + "config" + '\0' */ | |
2537 | len = strlen(c->config_path) + strlen(c->name) + strlen("config") + 3; | |
2538 | newpath = malloc(len); | |
2539 | if (!newpath) | |
2540 | return false; | |
2541 | ||
2542 | ret = snprintf(newpath, len, "%s/%s/config", c->config_path, c->name); | |
2543 | if (ret < 0 || ret >= len) { | |
2544 | fprintf(stderr, "Error printing out config file name\n"); | |
2545 | free(newpath); | |
2546 | return false; | |
2547 | } | |
2548 | ||
f10fad2f | 2549 | free(c->configfile); |
afeecbba SH |
2550 | c->configfile = newpath; |
2551 | ||
2552 | return true; | |
2553 | } | |
2554 | ||
858377e4 | 2555 | static bool do_lxcapi_set_config_path(struct lxc_container *c, const char *path) |
2a59a681 SH |
2556 | { |
2557 | char *p; | |
2558 | bool b = false; | |
afeecbba | 2559 | char *oldpath = NULL; |
2a59a681 SH |
2560 | |
2561 | if (!c) | |
2562 | return b; | |
2563 | ||
5cee8c50 | 2564 | if (container_mem_lock(c)) |
2a59a681 SH |
2565 | return b; |
2566 | ||
2567 | p = strdup(path); | |
afeecbba SH |
2568 | if (!p) { |
2569 | ERROR("Out of memory setting new lxc path"); | |
2a59a681 | 2570 | goto err; |
afeecbba SH |
2571 | } |
2572 | ||
2a59a681 SH |
2573 | b = true; |
2574 | if (c->config_path) | |
afeecbba | 2575 | oldpath = c->config_path; |
2a59a681 | 2576 | c->config_path = p; |
afeecbba SH |
2577 | |
2578 | /* Since we've changed the config path, we have to change the | |
2579 | * config file name too */ | |
2580 | if (!set_config_filename(c)) { | |
2581 | ERROR("Out of memory setting new config filename"); | |
2582 | b = false; | |
2583 | free(c->config_path); | |
2584 | c->config_path = oldpath; | |
2585 | oldpath = NULL; | |
2586 | } | |
2a59a681 | 2587 | err: |
f10fad2f | 2588 | free(oldpath); |
5cee8c50 | 2589 | container_mem_unlock(c); |
2a59a681 SH |
2590 | return b; |
2591 | } | |
2592 | ||
858377e4 | 2593 | WRAP_API_1(bool, lxcapi_set_config_path, const char *) |
2a59a681 | 2594 | |
858377e4 | 2595 | static bool do_lxcapi_set_cgroup_item(struct lxc_container *c, const char *subsys, const char *value) |
794dd120 SH |
2596 | { |
2597 | int ret; | |
794dd120 SH |
2598 | |
2599 | if (!c) | |
2600 | return false; | |
2601 | ||
3bc449ed | 2602 | if (is_stopped(c)) |
794dd120 SH |
2603 | return false; |
2604 | ||
3bc449ed SH |
2605 | if (container_disk_lock(c)) |
2606 | return false; | |
794dd120 | 2607 | |
33ad9f1a | 2608 | ret = lxc_cgroup_set(subsys, value, c->name, c->config_path); |
3bc449ed SH |
2609 | |
2610 | container_disk_unlock(c); | |
2611 | return ret == 0; | |
794dd120 SH |
2612 | } |
2613 | ||
858377e4 SH |
2614 | WRAP_API_2(bool, lxcapi_set_cgroup_item, const char *, const char *) |
2615 | ||
2616 | static int do_lxcapi_get_cgroup_item(struct lxc_container *c, const char *subsys, char *retv, int inlen) | |
794dd120 | 2617 | { |
3bc449ed | 2618 | int ret; |
794dd120 | 2619 | |
6502006a | 2620 | if (!c) |
794dd120 SH |
2621 | return -1; |
2622 | ||
3bc449ed | 2623 | if (is_stopped(c)) |
794dd120 SH |
2624 | return -1; |
2625 | ||
3bc449ed SH |
2626 | if (container_disk_lock(c)) |
2627 | return -1; | |
794dd120 | 2628 | |
33ad9f1a | 2629 | ret = lxc_cgroup_get(subsys, retv, inlen, c->name, c->config_path); |
794dd120 | 2630 | |
3bc449ed | 2631 | container_disk_unlock(c); |
794dd120 SH |
2632 | return ret; |
2633 | } | |
2634 | ||
858377e4 SH |
2635 | WRAP_API_3(int, lxcapi_get_cgroup_item, const char *, char *, int) |
2636 | ||
593e8478 | 2637 | const char *lxc_get_global_config_item(const char *key) |
83c98d82 | 2638 | { |
593e8478 | 2639 | return lxc_global_config_value(key); |
a8428dfa SH |
2640 | } |
2641 | ||
b6b918a1 SG |
2642 | const char *lxc_get_version(void) |
2643 | { | |
95ee490b | 2644 | return LXC_VERSION; |
b6b918a1 SG |
2645 | } |
2646 | ||
f0ca2726 | 2647 | static int copy_file(const char *old, const char *new) |
9be53773 SH |
2648 | { |
2649 | int in, out; | |
2650 | ssize_t len, ret; | |
2651 | char buf[8096]; | |
2652 | struct stat sbuf; | |
2653 | ||
2654 | if (file_exists(new)) { | |
2655 | ERROR("copy destination %s exists", new); | |
2656 | return -1; | |
2657 | } | |
2658 | ret = stat(old, &sbuf); | |
2659 | if (ret < 0) { | |
dfb31b25 | 2660 | INFO("Error stat'ing %s", old); |
9be53773 SH |
2661 | return -1; |
2662 | } | |
2663 | ||
2664 | in = open(old, O_RDONLY); | |
2665 | if (in < 0) { | |
dfb31b25 | 2666 | SYSERROR("Error opening original file %s", old); |
9be53773 SH |
2667 | return -1; |
2668 | } | |
2669 | out = open(new, O_CREAT | O_EXCL | O_WRONLY, 0644); | |
2670 | if (out < 0) { | |
dfb31b25 | 2671 | SYSERROR("Error opening new file %s", new); |
9be53773 SH |
2672 | close(in); |
2673 | return -1; | |
2674 | } | |
2675 | ||
2676 | while (1) { | |
2677 | len = read(in, buf, 8096); | |
2678 | if (len < 0) { | |
dfb31b25 | 2679 | SYSERROR("Error reading old file %s", old); |
9be53773 SH |
2680 | goto err; |
2681 | } | |
2682 | if (len == 0) | |
2683 | break; | |
2684 | ret = write(out, buf, len); | |
6849cb5b | 2685 | if (ret < len) { // should we retry? |
dfb31b25 | 2686 | SYSERROR("Error: write to new file %s was interrupted", new); |
9be53773 SH |
2687 | goto err; |
2688 | } | |
2689 | } | |
2690 | close(in); | |
2691 | close(out); | |
2692 | ||
2693 | // we set mode, but not owner/group | |
2694 | ret = chmod(new, sbuf.st_mode); | |
2695 | if (ret) { | |
dfb31b25 | 2696 | SYSERROR("Error setting mode on %s", new); |
9be53773 SH |
2697 | return -1; |
2698 | } | |
2699 | ||
2700 | return 0; | |
2701 | ||
2702 | err: | |
2703 | close(in); | |
2704 | close(out); | |
2705 | return -1; | |
2706 | } | |
2707 | ||
9be53773 SH |
2708 | static int copyhooks(struct lxc_container *oldc, struct lxc_container *c) |
2709 | { | |
619256b5 | 2710 | int i, len, ret; |
9be53773 | 2711 | struct lxc_list *it; |
619256b5 ÇO |
2712 | char *cpath; |
2713 | ||
2714 | len = strlen(oldc->config_path) + strlen(oldc->name) + 3; | |
2715 | cpath = alloca(len); | |
2716 | ret = snprintf(cpath, len, "%s/%s/", oldc->config_path, oldc->name); | |
2717 | if (ret < 0 || ret >= len) | |
2718 | return -1; | |
9be53773 SH |
2719 | |
2720 | for (i=0; i<NUM_LXC_HOOKS; i++) { | |
2721 | lxc_list_for_each(it, &c->lxc_conf->hooks[i]) { | |
2722 | char *hookname = it->elem; | |
c32981c3 | 2723 | char *fname = strrchr(hookname, '/'); |
9be53773 SH |
2724 | char tmppath[MAXPATHLEN]; |
2725 | if (!fname) // relative path - we don't support, but maybe we should | |
2726 | return 0; | |
619256b5 ÇO |
2727 | if (strncmp(hookname, cpath, len - 1) != 0) { |
2728 | // this hook is public - ignore | |
2729 | continue; | |
2730 | } | |
9be53773 SH |
2731 | // copy the script, and change the entry in confile |
2732 | ret = snprintf(tmppath, MAXPATHLEN, "%s/%s/%s", | |
2733 | c->config_path, c->name, fname+1); | |
2734 | if (ret < 0 || ret >= MAXPATHLEN) | |
2735 | return -1; | |
2736 | ret = copy_file(it->elem, tmppath); | |
2737 | if (ret < 0) | |
2738 | return -1; | |
2739 | free(it->elem); | |
2740 | it->elem = strdup(tmppath); | |
2741 | if (!it->elem) { | |
2742 | ERROR("out of memory copying hook path"); | |
2743 | return -1; | |
2744 | } | |
9be53773 SH |
2745 | } |
2746 | } | |
2747 | ||
67702c21 SH |
2748 | if (!clone_update_unexp_hooks(c->lxc_conf, oldc->config_path, |
2749 | c->config_path, oldc->name, c->name)) { | |
6b0d5538 SH |
2750 | ERROR("Error saving new hooks in clone"); |
2751 | return -1; | |
2752 | } | |
858377e4 | 2753 | do_lxcapi_save_config(c, NULL); |
9be53773 SH |
2754 | return 0; |
2755 | } | |
2756 | ||
9be53773 SH |
2757 | |
2758 | static int copy_fstab(struct lxc_container *oldc, struct lxc_container *c) | |
2759 | { | |
2760 | char newpath[MAXPATHLEN]; | |
2761 | char *oldpath = oldc->lxc_conf->fstab; | |
2762 | int ret; | |
2763 | ||
2764 | if (!oldpath) | |
2765 | return 0; | |
2766 | ||
6b0d5538 SH |
2767 | clear_unexp_config_line(c->lxc_conf, "lxc.mount", false); |
2768 | ||
c32981c3 | 2769 | char *p = strrchr(oldpath, '/'); |
9be53773 SH |
2770 | if (!p) |
2771 | return -1; | |
2772 | ret = snprintf(newpath, MAXPATHLEN, "%s/%s%s", | |
2773 | c->config_path, c->name, p); | |
2774 | if (ret < 0 || ret >= MAXPATHLEN) { | |
2775 | ERROR("error printing new path for %s", oldpath); | |
2776 | return -1; | |
2777 | } | |
2778 | if (file_exists(newpath)) { | |
2779 | ERROR("error: fstab file %s exists", newpath); | |
2780 | return -1; | |
2781 | } | |
2782 | ||
2783 | if (copy_file(oldpath, newpath) < 0) { | |
2784 | ERROR("error: copying %s to %s", oldpath, newpath); | |
2785 | return -1; | |
2786 | } | |
2787 | free(c->lxc_conf->fstab); | |
2788 | c->lxc_conf->fstab = strdup(newpath); | |
2789 | if (!c->lxc_conf->fstab) { | |
2790 | ERROR("error: allocating pathname"); | |
2791 | return -1; | |
2792 | } | |
6b0d5538 SH |
2793 | if (!do_append_unexp_config_line(c->lxc_conf, "lxc.mount", newpath)) { |
2794 | ERROR("error saving new lxctab"); | |
2795 | return -1; | |
2796 | } | |
9be53773 SH |
2797 | |
2798 | return 0; | |
2799 | } | |
2800 | ||
dfb31b25 SH |
2801 | static void copy_rdepends(struct lxc_container *c, struct lxc_container *c0) |
2802 | { | |
2803 | char path0[MAXPATHLEN], path1[MAXPATHLEN]; | |
2804 | int ret; | |
2805 | ||
2806 | ret = snprintf(path0, MAXPATHLEN, "%s/%s/lxc_rdepends", c0->config_path, | |
2807 | c0->name); | |
2808 | if (ret < 0 || ret >= MAXPATHLEN) { | |
2809 | WARN("Error copying reverse dependencies"); | |
2810 | return; | |
2811 | } | |
2812 | ret = snprintf(path1, MAXPATHLEN, "%s/%s/lxc_rdepends", c->config_path, | |
2813 | c->name); | |
2814 | if (ret < 0 || ret >= MAXPATHLEN) { | |
2815 | WARN("Error copying reverse dependencies"); | |
2816 | return; | |
2817 | } | |
2818 | if (copy_file(path0, path1) < 0) { | |
2819 | INFO("Error copying reverse dependencies"); | |
2820 | return; | |
2821 | } | |
2822 | } | |
2823 | ||
2824 | static bool add_rdepends(struct lxc_container *c, struct lxc_container *c0) | |
2825 | { | |
2826 | int ret; | |
2827 | char path[MAXPATHLEN]; | |
2828 | FILE *f; | |
2829 | bool bret; | |
2830 | ||
2831 | ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_rdepends", c->config_path, | |
2832 | c->name); | |
2833 | if (ret < 0 || ret >= MAXPATHLEN) | |
2834 | return false; | |
2835 | f = fopen(path, "a"); | |
2836 | if (!f) | |
2837 | return false; | |
2838 | bret = true; | |
2839 | // if anything goes wrong, just return an error | |
2840 | if (fprintf(f, "%s\n%s\n", c0->config_path, c0->name) < 0) | |
2841 | bret = false; | |
2842 | if (fclose(f) != 0) | |
2843 | bret = false; | |
2844 | return bret; | |
2845 | } | |
2846 | ||
15a90a10 SH |
2847 | /* |
2848 | * If the fs natively supports snapshot clones with no penalty, | |
2849 | * then default to those even if not requested. | |
2850 | * Currently we only do this for btrfs. | |
2851 | */ | |
2852 | bool should_default_to_snapshot(struct lxc_container *c0, | |
2853 | struct lxc_container *c1) | |
2854 | { | |
2855 | size_t l0 = strlen(c0->config_path) + strlen(c0->name) + 2; | |
2856 | size_t l1 = strlen(c1->config_path) + strlen(c1->name) + 2; | |
2857 | char *p0 = alloca(l0 + 1); | |
2858 | char *p1 = alloca(l1 + 1); | |
6e0fa6a0 | 2859 | char *rootfs = c0->lxc_conf->rootfs.path; |
15a90a10 SH |
2860 | |
2861 | snprintf(p0, l0, "%s/%s", c0->config_path, c0->name); | |
2862 | snprintf(p1, l1, "%s/%s", c1->config_path, c1->name); | |
9a09badc CB |
2863 | |
2864 | if (!is_btrfs_fs(p0) || !is_btrfs_fs(p1)) | |
2865 | return false; | |
2866 | ||
6e0fa6a0 CB |
2867 | if (is_btrfs_subvol(rootfs) <= 0) |
2868 | return false; | |
2869 | ||
15a90a10 SH |
2870 | return btrfs_same_fs(p0, p1) == 0; |
2871 | } | |
2872 | ||
9be53773 | 2873 | static int copy_storage(struct lxc_container *c0, struct lxc_container *c, |
763429b6 CB |
2874 | const char *newtype, int flags, const char *bdevdata, |
2875 | uint64_t newsize) | |
9be53773 SH |
2876 | { |
2877 | struct bdev *bdev; | |
dfb31b25 | 2878 | int need_rdep; |
9be53773 | 2879 | |
15a90a10 SH |
2880 | if (should_default_to_snapshot(c0, c)) |
2881 | flags |= LXC_CLONE_SNAPSHOT; | |
2882 | ||
763429b6 CB |
2883 | bdev = bdev_copy(c0, c->name, c->config_path, newtype, flags, bdevdata, |
2884 | newsize, &need_rdep); | |
9be53773 | 2885 | if (!bdev) { |
763429b6 | 2886 | ERROR("Error copying storage."); |
9be53773 SH |
2887 | return -1; |
2888 | } | |
763429b6 CB |
2889 | |
2890 | /* Set new rootfs. */ | |
9be53773 SH |
2891 | free(c->lxc_conf->rootfs.path); |
2892 | c->lxc_conf->rootfs.path = strdup(bdev->src); | |
763429b6 CB |
2893 | |
2894 | /* Set new bdev type. */ | |
8869eb5f CB |
2895 | free(c->lxc_conf->rootfs.bdev_type); |
2896 | c->lxc_conf->rootfs.bdev_type = strdup(bdev->type); | |
9be53773 | 2897 | bdev_put(bdev); |
763429b6 | 2898 | |
dfb31b25 | 2899 | if (!c->lxc_conf->rootfs.path) { |
763429b6 CB |
2900 | ERROR("Out of memory while setting storage path."); |
2901 | return -1; | |
2902 | } | |
2903 | if (!c->lxc_conf->rootfs.bdev_type) { | |
2904 | ERROR("Out of memory while setting rootfs backend."); | |
9be53773 | 2905 | return -1; |
dfb31b25 | 2906 | } |
763429b6 CB |
2907 | |
2908 | /* Append a new lxc.rootfs entry to the unexpanded config. */ | |
6b0d5538 | 2909 | clear_unexp_config_line(c->lxc_conf, "lxc.rootfs", false); |
763429b6 CB |
2910 | if (!do_append_unexp_config_line(c->lxc_conf, "lxc.rootfs", |
2911 | c->lxc_conf->rootfs.path)) { | |
2912 | ERROR("Error saving new rootfs to cloned config."); | |
d0218321 SH |
2913 | return -1; |
2914 | } | |
763429b6 CB |
2915 | |
2916 | /* Append a new lxc.rootfs.backend entry to the unexpanded config. */ | |
8869eb5f | 2917 | clear_unexp_config_line(c->lxc_conf, "lxc.rootfs.backend", false); |
763429b6 CB |
2918 | if (!do_append_unexp_config_line(c->lxc_conf, "lxc.rootfs.backend", |
2919 | c->lxc_conf->rootfs.bdev_type)) { | |
2920 | ERROR("Error saving new rootfs backend to cloned config."); | |
8869eb5f CB |
2921 | return -1; |
2922 | } | |
763429b6 | 2923 | |
eee59f94 SH |
2924 | if (flags & LXC_CLONE_SNAPSHOT) |
2925 | copy_rdepends(c, c0); | |
dfb31b25 SH |
2926 | if (need_rdep) { |
2927 | if (!add_rdepends(c, c0)) | |
2928 | WARN("Error adding reverse dependency from %s to %s", | |
763429b6 | 2929 | c->name, c0->name); |
dfb31b25 SH |
2930 | } |
2931 | ||
2932 | mod_all_rdeps(c, true); | |
2933 | ||
9be53773 SH |
2934 | return 0; |
2935 | } | |
2936 | ||
1354955b SH |
2937 | struct clone_update_data { |
2938 | struct lxc_container *c0; | |
2939 | struct lxc_container *c1; | |
2940 | int flags; | |
2941 | char **hookargs; | |
2942 | }; | |
2943 | ||
2944 | static int clone_update_rootfs(struct clone_update_data *data) | |
9be53773 | 2945 | { |
1354955b SH |
2946 | struct lxc_container *c0 = data->c0; |
2947 | struct lxc_container *c = data->c1; | |
2948 | int flags = data->flags; | |
2949 | char **hookargs = data->hookargs; | |
9be53773 SH |
2950 | int ret = -1; |
2951 | char path[MAXPATHLEN]; | |
2952 | struct bdev *bdev; | |
2953 | FILE *fout; | |
148e91f5 | 2954 | struct lxc_conf *conf = c->lxc_conf; |
9be53773 SH |
2955 | |
2956 | /* update hostname in rootfs */ | |
2957 | /* we're going to mount, so run in a clean namespace to simplify cleanup */ | |
2958 | ||
1354955b SH |
2959 | if (setgid(0) < 0) { |
2960 | ERROR("Failed to setgid to 0"); | |
2961 | return -1; | |
2962 | } | |
2963 | if (setuid(0) < 0) { | |
2964 | ERROR("Failed to setuid to 0"); | |
9be53773 | 2965 | return -1; |
1354955b | 2966 | } |
c476bdce SH |
2967 | if (setgroups(0, NULL) < 0) |
2968 | WARN("Failed to clear groups"); | |
9be53773 | 2969 | |
1354955b SH |
2970 | if (unshare(CLONE_NEWNS) < 0) |
2971 | return -1; | |
76a26f55 | 2972 | bdev = bdev_init(c->lxc_conf, c->lxc_conf->rootfs.path, c->lxc_conf->rootfs.mount, NULL); |
9be53773 | 2973 | if (!bdev) |
1354955b | 2974 | return -1; |
cf3ef16d SH |
2975 | if (strcmp(bdev->type, "dir") != 0) { |
2976 | if (unshare(CLONE_NEWNS) < 0) { | |
2977 | ERROR("error unsharing mounts"); | |
e7de366c | 2978 | bdev_put(bdev); |
1354955b | 2979 | return -1; |
cf3ef16d | 2980 | } |
2c6f3fc9 SH |
2981 | if (detect_shared_rootfs()) { |
2982 | if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL)) { | |
2983 | SYSERROR("Failed to make / rslave"); | |
2984 | ERROR("Continuing..."); | |
2985 | } | |
2986 | } | |
e7de366c SG |
2987 | if (bdev->ops->mount(bdev) < 0) { |
2988 | bdev_put(bdev); | |
1354955b | 2989 | return -1; |
e7de366c | 2990 | } |
cf3ef16d | 2991 | } else { // TODO come up with a better way |
f10fad2f | 2992 | free(bdev->dest); |
cf3ef16d SH |
2993 | bdev->dest = strdup(bdev->src); |
2994 | } | |
148e91f5 SH |
2995 | |
2996 | if (!lxc_list_empty(&conf->hooks[LXCHOOK_CLONE])) { | |
2997 | /* Start of environment variable setup for hooks */ | |
ab7efcf5 | 2998 | if (c0->name && setenv("LXC_SRC_NAME", c0->name, 1)) { |
1143ed39 DE |
2999 | SYSERROR("failed to set environment variable for source container name"); |
3000 | } | |
ab7efcf5 | 3001 | if (c->name && setenv("LXC_NAME", c->name, 1)) { |
148e91f5 SH |
3002 | SYSERROR("failed to set environment variable for container name"); |
3003 | } | |
ab7efcf5 | 3004 | if (conf->rcfile && setenv("LXC_CONFIG_FILE", conf->rcfile, 1)) { |
148e91f5 SH |
3005 | SYSERROR("failed to set environment variable for config path"); |
3006 | } | |
ab7efcf5 | 3007 | if (bdev->dest && setenv("LXC_ROOTFS_MOUNT", bdev->dest, 1)) { |
148e91f5 SH |
3008 | SYSERROR("failed to set environment variable for rootfs mount"); |
3009 | } | |
ab7efcf5 | 3010 | if (conf->rootfs.path && setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1)) { |
148e91f5 SH |
3011 | SYSERROR("failed to set environment variable for rootfs mount"); |
3012 | } | |
3013 | ||
283678ed | 3014 | if (run_lxc_hooks(c->name, "clone", conf, c->get_config_path(c), hookargs)) { |
148e91f5 | 3015 | ERROR("Error executing clone hook for %s", c->name); |
e7de366c | 3016 | bdev_put(bdev); |
1354955b | 3017 | return -1; |
148e91f5 SH |
3018 | } |
3019 | } | |
3020 | ||
3021 | if (!(flags & LXC_CLONE_KEEPNAME)) { | |
3022 | ret = snprintf(path, MAXPATHLEN, "%s/etc/hostname", bdev->dest); | |
e7de366c SG |
3023 | bdev_put(bdev); |
3024 | ||
148e91f5 | 3025 | if (ret < 0 || ret >= MAXPATHLEN) |
1354955b | 3026 | return -1; |
8058be39 | 3027 | if (!file_exists(path)) |
1354955b | 3028 | return 0; |
148e91f5 | 3029 | if (!(fout = fopen(path, "w"))) { |
959aee9c | 3030 | SYSERROR("unable to open %s: ignoring", path); |
1354955b | 3031 | return 0; |
148e91f5 | 3032 | } |
a684f0b7 ÇO |
3033 | if (fprintf(fout, "%s", c->name) < 0) { |
3034 | fclose(fout); | |
1354955b | 3035 | return -1; |
6849cb5b | 3036 | } |
148e91f5 | 3037 | if (fclose(fout) < 0) |
1354955b | 3038 | return -1; |
9be53773 | 3039 | } |
e7de366c SG |
3040 | else |
3041 | bdev_put(bdev); | |
3042 | ||
1354955b SH |
3043 | return 0; |
3044 | } | |
3045 | ||
3046 | static int clone_update_rootfs_wrapper(void *data) | |
3047 | { | |
3048 | struct clone_update_data *arg = (struct clone_update_data *) data; | |
3049 | return clone_update_rootfs(arg); | |
9be53773 SH |
3050 | } |
3051 | ||
3052 | /* | |
3053 | * We want to support: | |
3054 | sudo lxc-clone -o o1 -n n1 -s -L|-fssize fssize -v|--vgname vgname \ | |
3055 | -p|--lvprefix lvprefix -t|--fstype fstype -B backingstore | |
3056 | ||
3057 | -s [ implies overlayfs] | |
3058 | -s -B overlayfs | |
3059 | -s -B aufs | |
3060 | ||
3061 | only rootfs gets converted (copied/snapshotted) on clone. | |
3062 | */ | |
3063 | ||
d5752559 | 3064 | static int create_file_dirname(char *path, struct lxc_conf *conf) |
9be53773 | 3065 | { |
c32981c3 | 3066 | char *p = strrchr(path, '/'); |
d5752559 | 3067 | int ret = -1; |
9be53773 SH |
3068 | |
3069 | if (!p) | |
3070 | return -1; | |
3071 | *p = '\0'; | |
d028235d | 3072 | ret = do_create_container_dir(path, conf); |
9be53773 SH |
3073 | *p = '/'; |
3074 | return ret; | |
3075 | } | |
3076 | ||
858377e4 | 3077 | static struct lxc_container *do_lxcapi_clone(struct lxc_container *c, const char *newname, |
9be53773 | 3078 | const char *lxcpath, int flags, |
d659597e | 3079 | const char *bdevtype, const char *bdevdata, uint64_t newsize, |
148e91f5 | 3080 | char **hookargs) |
9be53773 SH |
3081 | { |
3082 | struct lxc_container *c2 = NULL; | |
3083 | char newpath[MAXPATHLEN]; | |
176d9acb | 3084 | int ret, storage_copied = 0; |
5eea90e8 | 3085 | char *origroot = NULL, *saved_unexp_conf = NULL; |
1354955b | 3086 | struct clone_update_data data; |
3b392519 | 3087 | size_t saved_unexp_len; |
9be53773 | 3088 | FILE *fout; |
1354955b | 3089 | pid_t pid; |
9be53773 | 3090 | |
858377e4 | 3091 | if (!c || !do_lxcapi_is_defined(c)) |
9be53773 SH |
3092 | return NULL; |
3093 | ||
5cee8c50 | 3094 | if (container_mem_lock(c)) |
9be53773 SH |
3095 | return NULL; |
3096 | ||
39dc698c | 3097 | if (!is_stopped(c)) { |
9be53773 SH |
3098 | ERROR("error: Original container (%s) is running", c->name); |
3099 | goto out; | |
3100 | } | |
3101 | ||
3102 | // Make sure the container doesn't yet exist. | |
05d53f4c SH |
3103 | if (!newname) |
3104 | newname = c->name; | |
3105 | if (!lxcpath) | |
858377e4 | 3106 | lxcpath = do_lxcapi_get_config_path(c); |
05d53f4c | 3107 | ret = snprintf(newpath, MAXPATHLEN, "%s/%s/config", lxcpath, newname); |
6849cb5b | 3108 | if (ret < 0 || ret >= MAXPATHLEN) { |
9be53773 SH |
3109 | SYSERROR("clone: failed making config pathname"); |
3110 | goto out; | |
3111 | } | |
3112 | if (file_exists(newpath)) { | |
3113 | ERROR("error: clone: %s exists", newpath); | |
3114 | goto out; | |
3115 | } | |
3116 | ||
d5752559 | 3117 | ret = create_file_dirname(newpath, c->lxc_conf); |
96532523 | 3118 | if (ret < 0 && errno != EEXIST) { |
9be53773 SH |
3119 | ERROR("Error creating container dir for %s", newpath); |
3120 | goto out; | |
3121 | } | |
3122 | ||
3123 | // copy the configuration, tweak it as needed, | |
8d2efe40 SH |
3124 | if (c->lxc_conf->rootfs.path) { |
3125 | origroot = c->lxc_conf->rootfs.path; | |
3126 | c->lxc_conf->rootfs.path = NULL; | |
3127 | } | |
9be53773 SH |
3128 | fout = fopen(newpath, "w"); |
3129 | if (!fout) { | |
3130 | SYSERROR("open %s", newpath); | |
3131 | goto out; | |
3132 | } | |
5eea90e8 SH |
3133 | |
3134 | saved_unexp_conf = c->lxc_conf->unexpanded_config; | |
3b392519 | 3135 | saved_unexp_len = c->lxc_conf->unexpanded_len; |
5eea90e8 SH |
3136 | c->lxc_conf->unexpanded_config = strdup(saved_unexp_conf); |
3137 | if (!c->lxc_conf->unexpanded_config) { | |
3138 | ERROR("Out of memory"); | |
7d72b959 | 3139 | fclose(fout); |
5eea90e8 SH |
3140 | goto out; |
3141 | } | |
3142 | clear_unexp_config_line(c->lxc_conf, "lxc.rootfs", false); | |
6b0d5538 | 3143 | write_config(fout, c->lxc_conf); |
9be53773 | 3144 | fclose(fout); |
8d2efe40 | 3145 | c->lxc_conf->rootfs.path = origroot; |
5eea90e8 SH |
3146 | free(c->lxc_conf->unexpanded_config); |
3147 | c->lxc_conf->unexpanded_config = saved_unexp_conf; | |
3148 | saved_unexp_conf = NULL; | |
3b392519 | 3149 | c->lxc_conf->unexpanded_len = saved_unexp_len; |
9be53773 | 3150 | |
05d53f4c | 3151 | sprintf(newpath, "%s/%s/rootfs", lxcpath, newname); |
9be53773 SH |
3152 | if (mkdir(newpath, 0755) < 0) { |
3153 | SYSERROR("error creating %s", newpath); | |
3154 | goto out; | |
3155 | } | |
3156 | ||
1354955b SH |
3157 | if (am_unpriv()) { |
3158 | if (chown_mapped_root(newpath, c->lxc_conf) < 0) { | |
959aee9c | 3159 | ERROR("Error chowning %s to container root", newpath); |
1354955b SH |
3160 | goto out; |
3161 | } | |
3162 | } | |
3163 | ||
05d53f4c | 3164 | c2 = lxc_container_new(newname, lxcpath); |
375c2258 | 3165 | if (!c2) { |
05d53f4c SH |
3166 | ERROR("clone: failed to create new container (%s %s)", newname, |
3167 | lxcpath); | |
9be53773 SH |
3168 | goto out; |
3169 | } | |
8d2efe40 SH |
3170 | |
3171 | // copy/snapshot rootfs's | |
3172 | ret = copy_storage(c, c2, bdevtype, flags, bdevdata, newsize); | |
3173 | if (ret < 0) | |
3174 | goto out; | |
9be53773 | 3175 | |
6b0d5538 | 3176 | |
96532523 | 3177 | // update utsname |
3d7ad474 CB |
3178 | if (!(flags & LXC_CLONE_KEEPNAME)) { |
3179 | clear_unexp_config_line(c2->lxc_conf, "lxc.utsname", false); | |
3180 | ||
3181 | if (!set_config_item_locked(c2, "lxc.utsname", newname)) { | |
3182 | ERROR("Error setting new hostname"); | |
3183 | goto out; | |
3184 | } | |
96532523 SH |
3185 | } |
3186 | ||
619256b5 ÇO |
3187 | // copy hooks |
3188 | ret = copyhooks(c, c2); | |
3189 | if (ret < 0) { | |
3190 | ERROR("error copying hooks"); | |
3191 | goto out; | |
9be53773 SH |
3192 | } |
3193 | ||
3194 | if (copy_fstab(c, c2) < 0) { | |
3195 | ERROR("error copying fstab"); | |
9be53773 SH |
3196 | goto out; |
3197 | } | |
3198 | ||
3199 | // update macaddrs | |
6b0d5538 | 3200 | if (!(flags & LXC_CLONE_KEEPMACADDR)) { |
67702c21 SH |
3201 | if (!network_new_hwaddrs(c2->lxc_conf)) { |
3202 | ERROR("Error updating mac addresses"); | |
6b0d5538 SH |
3203 | goto out; |
3204 | } | |
3205 | } | |
9be53773 | 3206 | |
030ce9a9 | 3207 | // update absolute paths for overlay mount directories |
83e79752 | 3208 | if (ovl_update_abs_paths(c2->lxc_conf, c->config_path, c->name, lxcpath, newname) < 0) |
030ce9a9 CB |
3209 | goto out; |
3210 | ||
176d9acb SH |
3211 | // We've now successfully created c2's storage, so clear it out if we |
3212 | // fail after this | |
3213 | storage_copied = 1; | |
3214 | ||
375c2258 | 3215 | if (!c2->save_config(c2, NULL)) |
9be53773 | 3216 | goto out; |
9be53773 | 3217 | |
1354955b SH |
3218 | if ((pid = fork()) < 0) { |
3219 | SYSERROR("fork"); | |
9be53773 | 3220 | goto out; |
1354955b SH |
3221 | } |
3222 | if (pid > 0) { | |
3223 | ret = wait_for_pid(pid); | |
3224 | if (ret) | |
3225 | goto out; | |
3226 | container_mem_unlock(c); | |
3227 | return c2; | |
3228 | } | |
3229 | data.c0 = c; | |
3230 | data.c1 = c2; | |
3231 | data.flags = flags; | |
3232 | data.hookargs = hookargs; | |
3233 | if (am_unpriv()) | |
3234 | ret = userns_exec_1(c->lxc_conf, clone_update_rootfs_wrapper, | |
c9b7c33e | 3235 | &data, "clone_update_rootfs_wrapper"); |
1354955b SH |
3236 | else |
3237 | ret = clone_update_rootfs(&data); | |
3238 | if (ret < 0) | |
3239 | exit(1); | |
9be53773 | 3240 | |
5cee8c50 | 3241 | container_mem_unlock(c); |
1354955b | 3242 | exit(0); |
9be53773 SH |
3243 | |
3244 | out: | |
5cee8c50 | 3245 | container_mem_unlock(c); |
375c2258 | 3246 | if (c2) { |
176d9acb SH |
3247 | if (!storage_copied) |
3248 | c2->lxc_conf->rootfs.path = NULL; | |
375c2258 | 3249 | c2->destroy(c2); |
9be53773 | 3250 | lxc_container_put(c2); |
375c2258 | 3251 | } |
9be53773 SH |
3252 | |
3253 | return NULL; | |
3254 | } | |
3255 | ||
858377e4 SH |
3256 | static struct lxc_container *lxcapi_clone(struct lxc_container *c, const char *newname, |
3257 | const char *lxcpath, int flags, | |
3258 | const char *bdevtype, const char *bdevdata, uint64_t newsize, | |
3259 | char **hookargs) | |
3260 | { | |
3261 | struct lxc_container * ret; | |
3262 | current_config = c ? c->lxc_conf : NULL; | |
3263 | ret = do_lxcapi_clone(c, newname, lxcpath, flags, bdevtype, bdevdata, newsize, hookargs); | |
3264 | current_config = NULL; | |
3265 | return ret; | |
3266 | } | |
3267 | ||
3268 | static bool do_lxcapi_rename(struct lxc_container *c, const char *newname) | |
06e5650e ÇO |
3269 | { |
3270 | struct bdev *bdev; | |
3271 | struct lxc_container *newc; | |
06e5650e | 3272 | |
d693cf93 | 3273 | if (!c || !c->name || !c->config_path || !c->lxc_conf) |
06e5650e ÇO |
3274 | return false; |
3275 | ||
18aa217b SH |
3276 | if (has_fs_snapshots(c) || has_snapshots(c)) { |
3277 | ERROR("Renaming a container with snapshots is not supported"); | |
3278 | return false; | |
3279 | } | |
76a26f55 | 3280 | bdev = bdev_init(c->lxc_conf, c->lxc_conf->rootfs.path, c->lxc_conf->rootfs.mount, NULL); |
06e5650e ÇO |
3281 | if (!bdev) { |
3282 | ERROR("Failed to find original backing store type"); | |
3283 | return false; | |
3284 | } | |
3285 | ||
619256b5 | 3286 | newc = lxcapi_clone(c, newname, c->config_path, LXC_CLONE_KEEPMACADDR, NULL, bdev->type, 0, NULL); |
06e5650e ÇO |
3287 | bdev_put(bdev); |
3288 | if (!newc) { | |
3289 | lxc_container_put(newc); | |
3290 | return false; | |
3291 | } | |
3292 | ||
3293 | if (newc && lxcapi_is_defined(newc)) | |
3294 | lxc_container_put(newc); | |
3295 | ||
18aa217b | 3296 | if (!container_destroy(c)) { |
06e5650e ÇO |
3297 | ERROR("Could not destroy existing container %s", c->name); |
3298 | return false; | |
3299 | } | |
3300 | return true; | |
3301 | } | |
3302 | ||
858377e4 SH |
3303 | WRAP_API_1(bool, lxcapi_rename, const char *) |
3304 | ||
a0e93eeb CS |
3305 | 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) |
3306 | { | |
858377e4 SH |
3307 | int ret; |
3308 | ||
a0e93eeb CS |
3309 | if (!c) |
3310 | return -1; | |
3311 | ||
858377e4 SH |
3312 | current_config = c->lxc_conf; |
3313 | ||
3314 | ret = lxc_attach(c->name, c->config_path, exec_function, exec_payload, options, attached_process); | |
3315 | current_config = NULL; | |
3316 | return ret; | |
a0e93eeb CS |
3317 | } |
3318 | ||
858377e4 | 3319 | static int do_lxcapi_attach_run_wait(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char * const argv[]) |
a0e93eeb CS |
3320 | { |
3321 | lxc_attach_command_t command; | |
3322 | pid_t pid; | |
3323 | int r; | |
3324 | ||
3325 | if (!c) | |
3326 | return -1; | |
3327 | ||
3328 | command.program = (char*)program; | |
3329 | command.argv = (char**)argv; | |
3330 | r = lxc_attach(c->name, c->config_path, lxc_attach_run_command, &command, options, &pid); | |
3331 | if (r < 0) { | |
3332 | ERROR("ups"); | |
3333 | return r; | |
3334 | } | |
3335 | return lxc_wait_for_pid_status(pid); | |
3336 | } | |
3337 | ||
858377e4 SH |
3338 | static int lxcapi_attach_run_wait(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char * const argv[]) |
3339 | { | |
3340 | int ret; | |
3341 | current_config = c ? c->lxc_conf : NULL; | |
3342 | ret = do_lxcapi_attach_run_wait(c, options, program, argv); | |
3343 | current_config = NULL; | |
3344 | return ret; | |
3345 | } | |
3346 | ||
74a3920a | 3347 | static int get_next_index(const char *lxcpath, char *cname) |
f5dd1d53 SH |
3348 | { |
3349 | char *fname; | |
3350 | struct stat sb; | |
3351 | int i = 0, ret; | |
3352 | ||
3353 | fname = alloca(strlen(lxcpath) + 20); | |
3354 | while (1) { | |
3355 | sprintf(fname, "%s/snap%d", lxcpath, i); | |
3356 | ret = stat(fname, &sb); | |
3357 | if (ret != 0) | |
3358 | return i; | |
3359 | i++; | |
3360 | } | |
3361 | } | |
3362 | ||
18aa217b SH |
3363 | static bool get_snappath_dir(struct lxc_container *c, char *snappath) |
3364 | { | |
3365 | int ret; | |
3366 | /* | |
3367 | * If the old style snapshot path exists, use it | |
3368 | * /var/lib/lxc -> /var/lib/lxcsnaps | |
3369 | */ | |
3370 | ret = snprintf(snappath, MAXPATHLEN, "%ssnaps", c->config_path); | |
3371 | if (ret < 0 || ret >= MAXPATHLEN) | |
3372 | return false; | |
3373 | if (dir_exists(snappath)) { | |
3374 | ret = snprintf(snappath, MAXPATHLEN, "%ssnaps/%s", c->config_path, c->name); | |
3375 | if (ret < 0 || ret >= MAXPATHLEN) | |
3376 | return false; | |
3377 | return true; | |
3378 | } | |
3379 | ||
3380 | /* | |
3381 | * Use the new style path | |
3382 | * /var/lib/lxc -> /var/lib/lxc + c->name + /snaps + \0 | |
3383 | */ | |
3384 | ret = snprintf(snappath, MAXPATHLEN, "%s/%s/snaps", c->config_path, c->name); | |
3385 | if (ret < 0 || ret >= MAXPATHLEN) | |
3386 | return false; | |
3387 | return true; | |
3388 | } | |
3389 | ||
858377e4 | 3390 | static int do_lxcapi_snapshot(struct lxc_container *c, const char *commentfile) |
f5dd1d53 SH |
3391 | { |
3392 | int i, flags, ret; | |
3393 | struct lxc_container *c2; | |
3394 | char snappath[MAXPATHLEN], newname[20]; | |
3395 | ||
840f05df SH |
3396 | if (!c || !lxcapi_is_defined(c)) |
3397 | return -1; | |
3398 | ||
cdd01be2 SH |
3399 | if (!bdev_can_backup(c->lxc_conf)) { |
3400 | ERROR("%s's backing store cannot be backed up.", c->name); | |
3401 | ERROR("Your container must use another backing store type."); | |
3402 | return -1; | |
3403 | } | |
3404 | ||
18aa217b | 3405 | if (!get_snappath_dir(c, snappath)) |
f5dd1d53 | 3406 | return -1; |
18aa217b | 3407 | |
f5dd1d53 SH |
3408 | i = get_next_index(snappath, c->name); |
3409 | ||
3410 | if (mkdir_p(snappath, 0755) < 0) { | |
3411 | ERROR("Failed to create snapshot directory %s", snappath); | |
3412 | return -1; | |
3413 | } | |
3414 | ||
3415 | ret = snprintf(newname, 20, "snap%d", i); | |
3416 | if (ret < 0 || ret >= 20) | |
3417 | return -1; | |
3418 | ||
0a83cbbb SH |
3419 | /* |
3420 | * We pass LXC_CLONE_SNAPSHOT to make sure that a rdepends file entry is | |
3421 | * created in the original container | |
3422 | */ | |
3423 | flags = LXC_CLONE_SNAPSHOT | LXC_CLONE_KEEPMACADDR | LXC_CLONE_KEEPNAME | | |
3424 | LXC_CLONE_KEEPBDEVTYPE | LXC_CLONE_MAYBE_SNAPSHOT; | |
76a26f55 | 3425 | if (bdev_is_dir(c->lxc_conf, c->lxc_conf->rootfs.path)) { |
8c39f7a4 SH |
3426 | ERROR("Snapshot of directory-backed container requested."); |
3427 | ERROR("Making a copy-clone. If you do want snapshots, then"); | |
1f92162d | 3428 | ERROR("please create an aufs or overlayfs clone first, snapshot that"); |
8c39f7a4 SH |
3429 | ERROR("and keep the original container pristine."); |
3430 | flags &= ~LXC_CLONE_SNAPSHOT | LXC_CLONE_MAYBE_SNAPSHOT; | |
3431 | } | |
858377e4 | 3432 | c2 = do_lxcapi_clone(c, newname, snappath, flags, NULL, NULL, 0, NULL); |
f5dd1d53 | 3433 | if (!c2) { |
959aee9c | 3434 | ERROR("clone of %s:%s failed", c->config_path, c->name); |
f5dd1d53 SH |
3435 | return -1; |
3436 | } | |
3437 | ||
3438 | lxc_container_put(c2); | |
3439 | ||
3440 | // Now write down the creation time | |
3441 | time_t timer; | |
3442 | char buffer[25]; | |
3443 | struct tm* tm_info; | |
025ed0f3 | 3444 | FILE *f; |
f5dd1d53 SH |
3445 | |
3446 | time(&timer); | |
3447 | tm_info = localtime(&timer); | |
3448 | ||
3449 | strftime(buffer, 25, "%Y:%m:%d %H:%M:%S", tm_info); | |
3450 | ||
3451 | char *dfnam = alloca(strlen(snappath) + strlen(newname) + 5); | |
3452 | sprintf(dfnam, "%s/%s/ts", snappath, newname); | |
025ed0f3 | 3453 | f = fopen(dfnam, "w"); |
f5dd1d53 | 3454 | if (!f) { |
959aee9c | 3455 | ERROR("Failed to open %s", dfnam); |
f5dd1d53 SH |
3456 | return -1; |
3457 | } | |
3458 | if (fprintf(f, "%s", buffer) < 0) { | |
3459 | SYSERROR("Writing timestamp"); | |
3460 | fclose(f); | |
3461 | return -1; | |
3462 | } | |
025ed0f3 | 3463 | ret = fclose(f); |
025ed0f3 | 3464 | if (ret != 0) { |
f5dd1d53 SH |
3465 | SYSERROR("Writing timestamp"); |
3466 | return -1; | |
3467 | } | |
3468 | ||
3469 | if (commentfile) { | |
3470 | // $p / $name / comment \0 | |
3471 | int len = strlen(snappath) + strlen(newname) + 10; | |
3472 | char *path = alloca(len); | |
3473 | sprintf(path, "%s/%s/comment", snappath, newname); | |
3474 | return copy_file(commentfile, path) < 0 ? -1 : i; | |
3475 | } | |
3476 | ||
3477 | return i; | |
3478 | } | |
3479 | ||
858377e4 SH |
3480 | WRAP_API_1(int, lxcapi_snapshot, const char *) |
3481 | ||
f5dd1d53 SH |
3482 | static void lxcsnap_free(struct lxc_snapshot *s) |
3483 | { | |
f10fad2f ME |
3484 | free(s->name); |
3485 | free(s->comment_pathname); | |
3486 | free(s->timestamp); | |
3487 | free(s->lxcpath); | |
f5dd1d53 SH |
3488 | } |
3489 | ||
3490 | static char *get_snapcomment_path(char* snappath, char *name) | |
3491 | { | |
3492 | // $snappath/$name/comment | |
3493 | int ret, len = strlen(snappath) + strlen(name) + 10; | |
3494 | char *s = malloc(len); | |
3495 | ||
3496 | if (s) { | |
3497 | ret = snprintf(s, len, "%s/%s/comment", snappath, name); | |
3498 | if (ret < 0 || ret >= len) { | |
3499 | free(s); | |
3500 | s = NULL; | |
3501 | } | |
3502 | } | |
3503 | return s; | |
3504 | } | |
3505 | ||
3506 | static char *get_timestamp(char* snappath, char *name) | |
3507 | { | |
3508 | char path[MAXPATHLEN], *s = NULL; | |
3509 | int ret, len; | |
3510 | FILE *fin; | |
3511 | ||
3512 | ret = snprintf(path, MAXPATHLEN, "%s/%s/ts", snappath, name); | |
3513 | if (ret < 0 || ret >= MAXPATHLEN) | |
3514 | return NULL; | |
025ed0f3 | 3515 | fin = fopen(path, "r"); |
025ed0f3 | 3516 | if (!fin) |
f5dd1d53 SH |
3517 | return NULL; |
3518 | (void) fseek(fin, 0, SEEK_END); | |
3519 | len = ftell(fin); | |
3520 | (void) fseek(fin, 0, SEEK_SET); | |
3521 | if (len > 0) { | |
3522 | s = malloc(len+1); | |
3523 | if (s) { | |
3524 | s[len] = '\0'; | |
3525 | if (fread(s, 1, len, fin) != len) { | |
3526 | SYSERROR("reading timestamp"); | |
3527 | free(s); | |
3528 | s = NULL; | |
3529 | } | |
3530 | } | |
3531 | } | |
3532 | fclose(fin); | |
3533 | return s; | |
3534 | } | |
3535 | ||
858377e4 | 3536 | static int do_lxcapi_snapshot_list(struct lxc_container *c, struct lxc_snapshot **ret_snaps) |
f5dd1d53 SH |
3537 | { |
3538 | char snappath[MAXPATHLEN], path2[MAXPATHLEN]; | |
18aa217b | 3539 | int count = 0, ret; |
74f96976 | 3540 | struct dirent *direntp; |
f5dd1d53 SH |
3541 | struct lxc_snapshot *snaps =NULL, *nsnaps; |
3542 | DIR *dir; | |
3543 | ||
3544 | if (!c || !lxcapi_is_defined(c)) | |
3545 | return -1; | |
c868b261 | 3546 | |
18aa217b | 3547 | if (!get_snappath_dir(c, snappath)) { |
f5dd1d53 SH |
3548 | ERROR("path name too long"); |
3549 | return -1; | |
3550 | } | |
025ed0f3 | 3551 | dir = opendir(snappath); |
025ed0f3 | 3552 | if (!dir) { |
f5dd1d53 SH |
3553 | INFO("failed to open %s - assuming no snapshots", snappath); |
3554 | return 0; | |
3555 | } | |
3556 | ||
74f96976 | 3557 | while ((direntp = readdir(dir))) { |
f5dd1d53 SH |
3558 | if (!direntp) |
3559 | break; | |
3560 | ||
3561 | if (!strcmp(direntp->d_name, ".")) | |
3562 | continue; | |
3563 | ||
3564 | if (!strcmp(direntp->d_name, "..")) | |
3565 | continue; | |
3566 | ||
3567 | ret = snprintf(path2, MAXPATHLEN, "%s/%s/config", snappath, direntp->d_name); | |
3568 | if (ret < 0 || ret >= MAXPATHLEN) { | |
3569 | ERROR("pathname too long"); | |
3570 | goto out_free; | |
3571 | } | |
3572 | if (!file_exists(path2)) | |
3573 | continue; | |
3574 | nsnaps = realloc(snaps, (count + 1)*sizeof(*snaps)); | |
3575 | if (!nsnaps) { | |
3576 | SYSERROR("Out of memory"); | |
3577 | goto out_free; | |
3578 | } | |
3579 | snaps = nsnaps; | |
3580 | snaps[count].free = lxcsnap_free; | |
3581 | snaps[count].name = strdup(direntp->d_name); | |
3582 | if (!snaps[count].name) | |
3583 | goto out_free; | |
3584 | snaps[count].lxcpath = strdup(snappath); | |
3585 | if (!snaps[count].lxcpath) { | |
3586 | free(snaps[count].name); | |
3587 | goto out_free; | |
3588 | } | |
3589 | snaps[count].comment_pathname = get_snapcomment_path(snappath, direntp->d_name); | |
3590 | snaps[count].timestamp = get_timestamp(snappath, direntp->d_name); | |
3591 | count++; | |
3592 | } | |
3593 | ||
3594 | if (closedir(dir)) | |
3595 | WARN("failed to close directory"); | |
3596 | ||
3597 | *ret_snaps = snaps; | |
3598 | return count; | |
3599 | ||
3600 | out_free: | |
3601 | if (snaps) { | |
3602 | int i; | |
3603 | for (i=0; i<count; i++) | |
3604 | lxcsnap_free(&snaps[i]); | |
3605 | free(snaps); | |
3606 | } | |
9baa57bd SH |
3607 | if (closedir(dir)) |
3608 | WARN("failed to close directory"); | |
f5dd1d53 SH |
3609 | return -1; |
3610 | } | |
3611 | ||
858377e4 SH |
3612 | WRAP_API_1(int, lxcapi_snapshot_list, struct lxc_snapshot **) |
3613 | ||
3614 | static bool do_lxcapi_snapshot_restore(struct lxc_container *c, const char *snapname, const char *newname) | |
f5dd1d53 SH |
3615 | { |
3616 | char clonelxcpath[MAXPATHLEN]; | |
18aa217b | 3617 | int flags = 0; |
f5dd1d53 SH |
3618 | struct lxc_container *snap, *rest; |
3619 | struct bdev *bdev; | |
3620 | bool b = false; | |
3621 | ||
3622 | if (!c || !c->name || !c->config_path) | |
3623 | return false; | |
3624 | ||
18aa217b SH |
3625 | if (has_fs_snapshots(c)) { |
3626 | ERROR("container rootfs has dependent snapshots"); | |
3627 | return false; | |
3628 | } | |
3629 | ||
76a26f55 | 3630 | bdev = bdev_init(c->lxc_conf, c->lxc_conf->rootfs.path, c->lxc_conf->rootfs.mount, NULL); |
f5dd1d53 SH |
3631 | if (!bdev) { |
3632 | ERROR("Failed to find original backing store type"); | |
3633 | return false; | |
3634 | } | |
3635 | ||
3636 | if (!newname) | |
3637 | newname = c->name; | |
7e36f87e | 3638 | |
18aa217b | 3639 | if (!get_snappath_dir(c, clonelxcpath)) { |
f5dd1d53 SH |
3640 | bdev_put(bdev); |
3641 | return false; | |
3642 | } | |
3643 | // how should we lock this? | |
3644 | ||
3645 | snap = lxc_container_new(snapname, clonelxcpath); | |
3646 | if (!snap || !lxcapi_is_defined(snap)) { | |
3647 | ERROR("Could not open snapshot %s", snapname); | |
3648 | if (snap) lxc_container_put(snap); | |
3649 | bdev_put(bdev); | |
3650 | return false; | |
3651 | } | |
3652 | ||
7e36f87e | 3653 | if (strcmp(c->name, newname) == 0) { |
18aa217b | 3654 | if (!container_destroy(c)) { |
7e36f87e ÇO |
3655 | ERROR("Could not destroy existing container %s", newname); |
3656 | lxc_container_put(snap); | |
3657 | bdev_put(bdev); | |
3658 | return false; | |
3659 | } | |
3660 | } | |
3661 | ||
de269ee8 SH |
3662 | if (strcmp(bdev->type, "dir") != 0 && strcmp(bdev->type, "loop") != 0) |
3663 | flags = LXC_CLONE_SNAPSHOT | LXC_CLONE_MAYBE_SNAPSHOT; | |
3664 | rest = lxcapi_clone(snap, newname, c->config_path, flags, | |
3665 | bdev->type, NULL, 0, NULL); | |
f5dd1d53 SH |
3666 | bdev_put(bdev); |
3667 | if (rest && lxcapi_is_defined(rest)) | |
3668 | b = true; | |
3669 | if (rest) | |
3670 | lxc_container_put(rest); | |
3671 | lxc_container_put(snap); | |
3672 | return b; | |
3673 | } | |
3674 | ||
858377e4 SH |
3675 | WRAP_API_2(bool, lxcapi_snapshot_restore, const char *, const char *) |
3676 | ||
18aa217b | 3677 | static bool do_snapshot_destroy(const char *snapname, const char *clonelxcpath) |
771d96b3 | 3678 | { |
771d96b3 | 3679 | struct lxc_container *snap = NULL; |
18aa217b | 3680 | bool bret = false; |
771d96b3 ÇO |
3681 | |
3682 | snap = lxc_container_new(snapname, clonelxcpath); | |
18aa217b | 3683 | if (!snap) { |
771d96b3 ÇO |
3684 | ERROR("Could not find snapshot %s", snapname); |
3685 | goto err; | |
3686 | } | |
3687 | ||
858377e4 | 3688 | if (!do_lxcapi_destroy(snap)) { |
771d96b3 ÇO |
3689 | ERROR("Could not destroy snapshot %s", snapname); |
3690 | goto err; | |
3691 | } | |
18aa217b | 3692 | bret = true; |
771d96b3 | 3693 | |
771d96b3 ÇO |
3694 | err: |
3695 | if (snap) | |
3696 | lxc_container_put(snap); | |
18aa217b SH |
3697 | return bret; |
3698 | } | |
3699 | ||
3700 | static bool remove_all_snapshots(const char *path) | |
3701 | { | |
3702 | DIR *dir; | |
74f96976 | 3703 | struct dirent *direntp; |
18aa217b SH |
3704 | bool bret = true; |
3705 | ||
3706 | dir = opendir(path); | |
3707 | if (!dir) { | |
3708 | SYSERROR("opendir on snapshot path %s", path); | |
3709 | return false; | |
3710 | } | |
74f96976 | 3711 | while ((direntp = readdir(dir))) { |
18aa217b SH |
3712 | if (!direntp) |
3713 | break; | |
3714 | if (!strcmp(direntp->d_name, ".")) | |
3715 | continue; | |
3716 | if (!strcmp(direntp->d_name, "..")) | |
3717 | continue; | |
3718 | if (!do_snapshot_destroy(direntp->d_name, path)) { | |
3719 | bret = false; | |
3720 | continue; | |
3721 | } | |
3722 | } | |
3723 | ||
3724 | closedir(dir); | |
3725 | ||
3726 | if (rmdir(path)) | |
3727 | SYSERROR("Error removing directory %s", path); | |
3728 | ||
3729 | return bret; | |
3730 | } | |
3731 | ||
858377e4 | 3732 | static bool do_lxcapi_snapshot_destroy(struct lxc_container *c, const char *snapname) |
18aa217b SH |
3733 | { |
3734 | char clonelxcpath[MAXPATHLEN]; | |
3735 | ||
3736 | if (!c || !c->name || !c->config_path || !snapname) | |
3737 | return false; | |
3738 | ||
3739 | if (!get_snappath_dir(c, clonelxcpath)) | |
3740 | return false; | |
3741 | ||
3742 | return do_snapshot_destroy(snapname, clonelxcpath); | |
3743 | } | |
3744 | ||
858377e4 SH |
3745 | WRAP_API_1(bool, lxcapi_snapshot_destroy, const char *) |
3746 | ||
3747 | static bool do_lxcapi_snapshot_destroy_all(struct lxc_container *c) | |
18aa217b SH |
3748 | { |
3749 | char clonelxcpath[MAXPATHLEN]; | |
3750 | ||
3751 | if (!c || !c->name || !c->config_path) | |
3752 | return false; | |
3753 | ||
3754 | if (!get_snappath_dir(c, clonelxcpath)) | |
3755 | return false; | |
3756 | ||
3757 | return remove_all_snapshots(clonelxcpath); | |
771d96b3 ÇO |
3758 | } |
3759 | ||
858377e4 SH |
3760 | WRAP_API(bool, lxcapi_snapshot_destroy_all) |
3761 | ||
3762 | static bool do_lxcapi_may_control(struct lxc_container *c) | |
b494d2dd SH |
3763 | { |
3764 | return lxc_try_cmd(c->name, c->config_path) == 0; | |
3765 | } | |
3766 | ||
858377e4 SH |
3767 | WRAP_API(bool, lxcapi_may_control) |
3768 | ||
d5aa23e6 SH |
3769 | static bool do_add_remove_node(pid_t init_pid, const char *path, bool add, |
3770 | struct stat *st) | |
3771 | { | |
3772 | char chrootpath[MAXPATHLEN]; | |
3773 | char *directory_path = NULL; | |
3774 | pid_t pid; | |
3775 | int ret; | |
3776 | ||
3777 | if ((pid = fork()) < 0) { | |
3778 | SYSERROR("failed to fork a child helper"); | |
3779 | return false; | |
3780 | } | |
3781 | if (pid) { | |
3782 | if (wait_for_pid(pid) != 0) { | |
3783 | ERROR("Failed to create note in guest"); | |
3784 | return false; | |
3785 | } | |
3786 | return true; | |
3787 | } | |
3788 | ||
3789 | /* prepare the path */ | |
3790 | ret = snprintf(chrootpath, MAXPATHLEN, "/proc/%d/root", init_pid); | |
3791 | if (ret < 0 || ret >= MAXPATHLEN) | |
3792 | return false; | |
3793 | ||
6b9324bd | 3794 | if (chroot(chrootpath) < 0) |
d5aa23e6 | 3795 | exit(1); |
6b9324bd | 3796 | if (chdir("/") < 0) |
d5aa23e6 SH |
3797 | exit(1); |
3798 | /* remove path if it exists */ | |
3799 | if(faccessat(AT_FDCWD, path, F_OK, AT_SYMLINK_NOFOLLOW) == 0) { | |
3800 | if (unlink(path) < 0) { | |
3801 | ERROR("unlink failed"); | |
3802 | exit(1); | |
3803 | } | |
3804 | } | |
3805 | if (!add) | |
3806 | exit(0); | |
3807 | ||
3808 | /* create any missing directories */ | |
3809 | directory_path = dirname(strdup(path)); | |
3810 | if (mkdir_p(directory_path, 0755) < 0 && errno != EEXIST) { | |
3811 | ERROR("failed to create directory"); | |
3812 | exit(1); | |
3813 | } | |
3814 | ||
3815 | /* create the device node */ | |
3816 | if (mknod(path, st->st_mode, st->st_rdev) < 0) { | |
3817 | ERROR("mknod failed"); | |
3818 | exit(1); | |
3819 | } | |
3820 | ||
3821 | exit(0); | |
3822 | } | |
3823 | ||
f0ca2726 | 3824 | static bool add_remove_device_node(struct lxc_container *c, const char *src_path, const char *dest_path, bool add) |
a9a0ed90 ÇO |
3825 | { |
3826 | int ret; | |
3827 | struct stat st; | |
a9a0ed90 | 3828 | char value[MAX_BUFFER]; |
f0ca2726 | 3829 | const char *p; |
a9a0ed90 ÇO |
3830 | |
3831 | /* make sure container is running */ | |
858377e4 | 3832 | if (!do_lxcapi_is_running(c)) { |
a9a0ed90 | 3833 | ERROR("container is not running"); |
d5aa23e6 | 3834 | return false; |
a9a0ed90 ÇO |
3835 | } |
3836 | ||
3837 | /* use src_path if dest_path is NULL otherwise use dest_path */ | |
3838 | p = dest_path ? dest_path : src_path; | |
3839 | ||
a9a0ed90 ÇO |
3840 | /* make sure we can access p */ |
3841 | if(access(p, F_OK) < 0 || stat(p, &st) < 0) | |
d5aa23e6 | 3842 | return false; |
a9a0ed90 ÇO |
3843 | |
3844 | /* continue if path is character device or block device */ | |
c6a9b0d7 | 3845 | if (S_ISCHR(st.st_mode)) |
a9a0ed90 | 3846 | ret = snprintf(value, MAX_BUFFER, "c %d:%d rwm", major(st.st_rdev), minor(st.st_rdev)); |
c6a9b0d7 | 3847 | else if (S_ISBLK(st.st_mode)) |
a9a0ed90 ÇO |
3848 | ret = snprintf(value, MAX_BUFFER, "b %d:%d rwm", major(st.st_rdev), minor(st.st_rdev)); |
3849 | else | |
d5aa23e6 | 3850 | return false; |
a9a0ed90 ÇO |
3851 | |
3852 | /* check snprintf return code */ | |
3853 | if (ret < 0 || ret >= MAX_BUFFER) | |
d5aa23e6 | 3854 | return false; |
a9a0ed90 | 3855 | |
858377e4 | 3856 | if (!do_add_remove_node(do_lxcapi_init_pid(c), p, add, &st)) |
d5aa23e6 | 3857 | return false; |
a9a0ed90 | 3858 | |
d5aa23e6 | 3859 | /* add or remove device to/from cgroup access list */ |
a9a0ed90 | 3860 | if (add) { |
858377e4 | 3861 | if (!do_lxcapi_set_cgroup_item(c, "devices.allow", value)) { |
a9a0ed90 | 3862 | ERROR("set_cgroup_item failed while adding the device node"); |
d5aa23e6 | 3863 | return false; |
a9a0ed90 ÇO |
3864 | } |
3865 | } else { | |
858377e4 | 3866 | if (!do_lxcapi_set_cgroup_item(c, "devices.deny", value)) { |
a9a0ed90 | 3867 | ERROR("set_cgroup_item failed while removing the device node"); |
d5aa23e6 | 3868 | return false; |
a9a0ed90 ÇO |
3869 | } |
3870 | } | |
d5aa23e6 | 3871 | |
a9a0ed90 | 3872 | return true; |
a9a0ed90 ÇO |
3873 | } |
3874 | ||
858377e4 | 3875 | static bool do_lxcapi_add_device_node(struct lxc_container *c, const char *src_path, const char *dest_path) |
a9a0ed90 | 3876 | { |
c868b261 ÇO |
3877 | if (am_unpriv()) { |
3878 | ERROR(NOT_SUPPORTED_ERROR, __FUNCTION__); | |
3879 | return false; | |
3880 | } | |
a9a0ed90 ÇO |
3881 | return add_remove_device_node(c, src_path, dest_path, true); |
3882 | } | |
3883 | ||
858377e4 SH |
3884 | WRAP_API_2(bool, lxcapi_add_device_node, const char *, const char *) |
3885 | ||
3886 | static bool do_lxcapi_remove_device_node(struct lxc_container *c, const char *src_path, const char *dest_path) | |
a9a0ed90 | 3887 | { |
c868b261 ÇO |
3888 | if (am_unpriv()) { |
3889 | ERROR(NOT_SUPPORTED_ERROR, __FUNCTION__); | |
3890 | return false; | |
3891 | } | |
a9a0ed90 ÇO |
3892 | return add_remove_device_node(c, src_path, dest_path, false); |
3893 | } | |
3894 | ||
858377e4 SH |
3895 | WRAP_API_2(bool, lxcapi_remove_device_node, const char *, const char *) |
3896 | ||
3897 | static bool do_lxcapi_attach_interface(struct lxc_container *c, const char *ifname, | |
e58fae8f DY |
3898 | const char *dst_ifname) |
3899 | { | |
3900 | int ret = 0; | |
3901 | if (am_unpriv()) { | |
3902 | ERROR(NOT_SUPPORTED_ERROR, __FUNCTION__); | |
3903 | return false; | |
3904 | } | |
3905 | ||
3906 | if (!ifname) { | |
3907 | ERROR("No source interface name given"); | |
3908 | return false; | |
3909 | } | |
3910 | ||
3911 | ret = lxc_netdev_isup(ifname); | |
e58fae8f | 3912 | |
e5848d39 SH |
3913 | if (ret > 0) { |
3914 | /* netdev of ifname is up. */ | |
e58fae8f DY |
3915 | ret = lxc_netdev_down(ifname); |
3916 | if (ret) | |
3917 | goto err; | |
3918 | } | |
3919 | ||
858377e4 | 3920 | ret = lxc_netdev_move_by_name(ifname, do_lxcapi_init_pid(c), dst_ifname); |
e58fae8f DY |
3921 | if (ret) |
3922 | goto err; | |
3923 | ||
3924 | return true; | |
e5848d39 | 3925 | |
e58fae8f | 3926 | err: |
e58fae8f DY |
3927 | return false; |
3928 | } | |
3929 | ||
858377e4 SH |
3930 | WRAP_API_2(bool, lxcapi_attach_interface, const char *, const char *) |
3931 | ||
3932 | static bool do_lxcapi_detach_interface(struct lxc_container *c, const char *ifname, | |
e58fae8f DY |
3933 | const char *dst_ifname) |
3934 | { | |
3935 | pid_t pid, pid_outside; | |
3936 | ||
3937 | if (am_unpriv()) { | |
3938 | ERROR(NOT_SUPPORTED_ERROR, __FUNCTION__); | |
3939 | return false; | |
3940 | } | |
3941 | ||
3942 | if (!ifname) { | |
3943 | ERROR("No source interface name given"); | |
3944 | return false; | |
3945 | } | |
3946 | ||
3947 | pid_outside = getpid(); | |
3948 | pid = fork(); | |
3949 | if (pid < 0) { | |
3950 | ERROR("failed to fork task to get interfaces information"); | |
3951 | return false; | |
3952 | } | |
3953 | ||
3954 | if (pid == 0) { // child | |
3955 | int ret = 0; | |
e0f59189 | 3956 | if (!enter_net_ns(c)) { |
e58fae8f DY |
3957 | ERROR("failed to enter namespace"); |
3958 | exit(-1); | |
3959 | } | |
3960 | ||
3961 | ret = lxc_netdev_isup(ifname); | |
3962 | if (ret < 0) | |
3963 | exit(ret); | |
3964 | ||
3965 | /* netdev of ifname is up. */ | |
3966 | if (ret) { | |
3967 | ret = lxc_netdev_down(ifname); | |
3968 | if (ret) | |
3969 | exit(ret); | |
3970 | } | |
3971 | ||
3972 | ret = lxc_netdev_move_by_name(ifname, pid_outside, dst_ifname); | |
3973 | ||
3974 | /* -EINVAL means there is no netdev named as ifanme. */ | |
3975 | if (ret == -EINVAL) { | |
3976 | ERROR("No network device named as %s.", ifname); | |
3977 | } | |
3978 | exit(ret); | |
3979 | } | |
3980 | ||
3981 | if (wait_for_pid(pid) != 0) | |
3982 | return false; | |
3983 | ||
3984 | return true; | |
3985 | } | |
3986 | ||
858377e4 SH |
3987 | WRAP_API_2(bool, lxcapi_detach_interface, const char *, const char *) |
3988 | ||
aef3d51e TA |
3989 | static int do_lxcapi_migrate(struct lxc_container *c, unsigned int cmd, |
3990 | struct migrate_opts *opts, unsigned int size) | |
bbd4e13e | 3991 | { |
aef3d51e | 3992 | int ret; |
2cb80427 | 3993 | struct migrate_opts *valid_opts = opts; |
85c50991 | 3994 | |
aef3d51e TA |
3995 | /* If the caller has a bigger (newer) struct migrate_opts, let's make |
3996 | * sure that the stuff on the end is zero, i.e. that they didn't ask us | |
3997 | * to do anything special. | |
3998 | */ | |
3999 | if (size > sizeof(*opts)) { | |
4000 | unsigned char *addr; | |
4001 | unsigned char *end; | |
4002 | ||
4003 | addr = (void *)opts + sizeof(*opts); | |
4004 | end = (void *)opts + size; | |
4005 | for (; addr < end; addr++) { | |
4006 | if (*addr) { | |
4007 | return -E2BIG; | |
4008 | } | |
4009 | } | |
85c50991 TA |
4010 | } |
4011 | ||
2cb80427 TA |
4012 | /* If the caller has a smaller struct, let's zero out the end for them |
4013 | * so we don't accidentally use bits of it that they didn't know about | |
4014 | * to initialize. | |
4015 | */ | |
4016 | if (size < sizeof(*opts)) { | |
4017 | valid_opts = malloc(sizeof(*opts)); | |
4018 | if (!valid_opts) | |
4019 | return -ENOMEM; | |
4020 | ||
4021 | memset(valid_opts, 0, sizeof(*opts)); | |
4022 | memcpy(valid_opts, opts, size); | |
4023 | } | |
4024 | ||
aef3d51e TA |
4025 | switch (cmd) { |
4026 | case MIGRATE_PRE_DUMP: | |
7ad13c91 TA |
4027 | if (!do_lxcapi_is_running(c)) { |
4028 | ERROR("container is not running"); | |
4029 | return false; | |
4030 | } | |
2cb80427 | 4031 | ret = !__criu_pre_dump(c, valid_opts); |
aef3d51e TA |
4032 | break; |
4033 | case MIGRATE_DUMP: | |
7ad13c91 TA |
4034 | if (!do_lxcapi_is_running(c)) { |
4035 | ERROR("container is not running"); | |
4036 | return false; | |
4037 | } | |
2cb80427 | 4038 | ret = !__criu_dump(c, valid_opts); |
aef3d51e TA |
4039 | break; |
4040 | case MIGRATE_RESTORE: | |
7ad13c91 TA |
4041 | if (do_lxcapi_is_running(c)) { |
4042 | ERROR("container is already running"); | |
4043 | return false; | |
4044 | } | |
2cb80427 | 4045 | ret = !__criu_restore(c, valid_opts); |
aef3d51e TA |
4046 | break; |
4047 | default: | |
4048 | ERROR("invalid migrate command %u", cmd); | |
4049 | ret = -EINVAL; | |
4050 | } | |
735f2c6e | 4051 | |
f686506d TA |
4052 | if (size < sizeof(*opts)) |
4053 | free(valid_opts); | |
4054 | ||
aef3d51e TA |
4055 | return ret; |
4056 | } | |
735f2c6e | 4057 | |
aef3d51e | 4058 | WRAP_API_3(int, lxcapi_migrate, unsigned int, struct migrate_opts *, unsigned int) |
735f2c6e | 4059 | |
aef3d51e TA |
4060 | static bool do_lxcapi_checkpoint(struct lxc_container *c, char *directory, bool stop, bool verbose) |
4061 | { | |
ebb088e1 AR |
4062 | struct migrate_opts opts; |
4063 | ||
4064 | memset(&opts, 0, sizeof(opts)); | |
4065 | ||
4066 | opts.directory = directory; | |
4067 | opts.stop = stop; | |
4068 | opts.verbose = verbose; | |
735f2c6e | 4069 | |
aef3d51e | 4070 | return !do_lxcapi_migrate(c, MIGRATE_DUMP, &opts, sizeof(opts)); |
735f2c6e TA |
4071 | } |
4072 | ||
858377e4 SH |
4073 | WRAP_API_3(bool, lxcapi_checkpoint, char *, bool, bool) |
4074 | ||
4075 | static bool do_lxcapi_restore(struct lxc_container *c, char *directory, bool verbose) | |
c9d8f2ee | 4076 | { |
ebb088e1 AR |
4077 | struct migrate_opts opts; |
4078 | ||
4079 | memset(&opts, 0, sizeof(opts)); | |
4080 | ||
4081 | opts.directory = directory; | |
4082 | opts.verbose = verbose; | |
c9d8f2ee | 4083 | |
aef3d51e | 4084 | return !do_lxcapi_migrate(c, MIGRATE_RESTORE, &opts, sizeof(opts)); |
735f2c6e TA |
4085 | } |
4086 | ||
858377e4 SH |
4087 | WRAP_API_2(bool, lxcapi_restore, char *, bool) |
4088 | ||
a0e93eeb CS |
4089 | static int lxcapi_attach_run_waitl(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char *arg, ...) |
4090 | { | |
4091 | va_list ap; | |
4092 | const char **argv; | |
4093 | int ret; | |
4094 | ||
4095 | if (!c) | |
4096 | return -1; | |
4097 | ||
858377e4 SH |
4098 | current_config = c->lxc_conf; |
4099 | ||
a0e93eeb CS |
4100 | va_start(ap, arg); |
4101 | argv = lxc_va_arg_list_to_argv_const(ap, 1); | |
4102 | va_end(ap); | |
4103 | ||
4104 | if (!argv) { | |
4105 | ERROR("Memory allocation error."); | |
858377e4 SH |
4106 | ret = -1; |
4107 | goto out; | |
a0e93eeb CS |
4108 | } |
4109 | argv[0] = arg; | |
4110 | ||
858377e4 | 4111 | ret = do_lxcapi_attach_run_wait(c, options, program, (const char * const *)argv); |
a0e93eeb | 4112 | free((void*)argv); |
858377e4 SH |
4113 | out: |
4114 | current_config = NULL; | |
a0e93eeb CS |
4115 | return ret; |
4116 | } | |
4117 | ||
afeecbba | 4118 | struct lxc_container *lxc_container_new(const char *name, const char *configpath) |
72d0e1cb SG |
4119 | { |
4120 | struct lxc_container *c; | |
72d0e1cb | 4121 | |
18aa217b SH |
4122 | if (!name) |
4123 | return NULL; | |
4124 | ||
72d0e1cb SG |
4125 | c = malloc(sizeof(*c)); |
4126 | if (!c) { | |
4127 | fprintf(stderr, "failed to malloc lxc_container\n"); | |
4128 | return NULL; | |
4129 | } | |
4130 | memset(c, 0, sizeof(*c)); | |
4131 | ||
afeecbba SH |
4132 | if (configpath) |
4133 | c->config_path = strdup(configpath); | |
4134 | else | |
593e8478 | 4135 | c->config_path = strdup(lxc_global_config_value("lxc.lxcpath")); |
afeecbba | 4136 | |
2a59a681 | 4137 | if (!c->config_path) { |
03fadd16 | 4138 | fprintf(stderr, "Out of memory\n"); |
2a59a681 SH |
4139 | goto err; |
4140 | } | |
4141 | ||
f5dd1d53 | 4142 | remove_trailing_slashes(c->config_path); |
72d0e1cb SG |
4143 | c->name = malloc(strlen(name)+1); |
4144 | if (!c->name) { | |
4145 | fprintf(stderr, "Error allocating lxc_container name\n"); | |
4146 | goto err; | |
4147 | } | |
4148 | strcpy(c->name, name); | |
4149 | ||
4150 | c->numthreads = 1; | |
df271a59 | 4151 | if (!(c->slock = lxc_newlock(c->config_path, name))) { |
72d0e1cb SG |
4152 | fprintf(stderr, "failed to create lock\n"); |
4153 | goto err; | |
4154 | } | |
4155 | ||
df271a59 | 4156 | if (!(c->privlock = lxc_newlock(NULL, NULL))) { |
72d0e1cb SG |
4157 | fprintf(stderr, "failed to alloc privlock\n"); |
4158 | goto err; | |
4159 | } | |
4160 | ||
afeecbba | 4161 | if (!set_config_filename(c)) { |
72d0e1cb SG |
4162 | fprintf(stderr, "Error allocating config file pathname\n"); |
4163 | goto err; | |
4164 | } | |
72d0e1cb | 4165 | |
bac806d1 SH |
4166 | if (file_exists(c->configfile) && !lxcapi_load_config(c, NULL)) |
4167 | goto err; | |
72d0e1cb | 4168 | |
3e625e2d SH |
4169 | if (ongoing_create(c) == 2) { |
4170 | ERROR("Error: %s creation was not completed", c->name); | |
18aa217b | 4171 | container_destroy(c); |
4df7f012 | 4172 | lxcapi_clear_config(c); |
3e625e2d | 4173 | } |
a2739df5 | 4174 | c->daemonize = true; |
72cf75fa | 4175 | c->pidfile = NULL; |
3e625e2d | 4176 | |
72d0e1cb SG |
4177 | // assign the member functions |
4178 | c->is_defined = lxcapi_is_defined; | |
4179 | c->state = lxcapi_state; | |
4180 | c->is_running = lxcapi_is_running; | |
4181 | c->freeze = lxcapi_freeze; | |
4182 | c->unfreeze = lxcapi_unfreeze; | |
0115f8fd | 4183 | c->console = lxcapi_console; |
b5159817 | 4184 | c->console_getfd = lxcapi_console_getfd; |
72d0e1cb SG |
4185 | c->init_pid = lxcapi_init_pid; |
4186 | c->load_config = lxcapi_load_config; | |
4187 | c->want_daemonize = lxcapi_want_daemonize; | |
130a1888 | 4188 | c->want_close_all_fds = lxcapi_want_close_all_fds; |
72d0e1cb SG |
4189 | c->start = lxcapi_start; |
4190 | c->startl = lxcapi_startl; | |
4191 | c->stop = lxcapi_stop; | |
4192 | c->config_file_name = lxcapi_config_file_name; | |
4193 | c->wait = lxcapi_wait; | |
4194 | c->set_config_item = lxcapi_set_config_item; | |
4195 | c->destroy = lxcapi_destroy; | |
18aa217b | 4196 | c->destroy_with_snapshots = lxcapi_destroy_with_snapshots; |
06e5650e | 4197 | c->rename = lxcapi_rename; |
72d0e1cb SG |
4198 | c->save_config = lxcapi_save_config; |
4199 | c->get_keys = lxcapi_get_keys; | |
4200 | c->create = lxcapi_create; | |
4201 | c->createl = lxcapi_createl; | |
4202 | c->shutdown = lxcapi_shutdown; | |
3e625e2d | 4203 | c->reboot = lxcapi_reboot; |
4df7f012 | 4204 | c->clear_config = lxcapi_clear_config; |
72d0e1cb SG |
4205 | c->clear_config_item = lxcapi_clear_config_item; |
4206 | c->get_config_item = lxcapi_get_config_item; | |
8ac18377 | 4207 | c->get_running_config_item = lxcapi_get_running_config_item; |
794dd120 SH |
4208 | c->get_cgroup_item = lxcapi_get_cgroup_item; |
4209 | c->set_cgroup_item = lxcapi_set_cgroup_item; | |
2a59a681 SH |
4210 | c->get_config_path = lxcapi_get_config_path; |
4211 | c->set_config_path = lxcapi_set_config_path; | |
9be53773 | 4212 | c->clone = lxcapi_clone; |
799f29ab | 4213 | c->get_interfaces = lxcapi_get_interfaces; |
9c83a661 | 4214 | c->get_ips = lxcapi_get_ips; |
a0e93eeb CS |
4215 | c->attach = lxcapi_attach; |
4216 | c->attach_run_wait = lxcapi_attach_run_wait; | |
4217 | c->attach_run_waitl = lxcapi_attach_run_waitl; | |
f5dd1d53 SH |
4218 | c->snapshot = lxcapi_snapshot; |
4219 | c->snapshot_list = lxcapi_snapshot_list; | |
4220 | c->snapshot_restore = lxcapi_snapshot_restore; | |
771d96b3 | 4221 | c->snapshot_destroy = lxcapi_snapshot_destroy; |
18aa217b | 4222 | c->snapshot_destroy_all = lxcapi_snapshot_destroy_all; |
b494d2dd | 4223 | c->may_control = lxcapi_may_control; |
a9a0ed90 ÇO |
4224 | c->add_device_node = lxcapi_add_device_node; |
4225 | c->remove_device_node = lxcapi_remove_device_node; | |
e58fae8f DY |
4226 | c->attach_interface = lxcapi_attach_interface; |
4227 | c->detach_interface = lxcapi_detach_interface; | |
735f2c6e TA |
4228 | c->checkpoint = lxcapi_checkpoint; |
4229 | c->restore = lxcapi_restore; | |
aef3d51e | 4230 | c->migrate = lxcapi_migrate; |
72d0e1cb | 4231 | |
72d0e1cb SG |
4232 | return c; |
4233 | ||
4234 | err: | |
4235 | lxc_container_free(c); | |
4236 | return NULL; | |
4237 | } | |
4238 | ||
4a7c7daa | 4239 | int lxc_get_wait_states(const char **states) |
72d0e1cb SG |
4240 | { |
4241 | int i; | |
4242 | ||
4243 | if (states) | |
4244 | for (i=0; i<MAX_STATE; i++) | |
4245 | states[i] = lxc_state2str(i); | |
4246 | return MAX_STATE; | |
4247 | } | |
a41f104b | 4248 | |
a41f104b SH |
4249 | /* |
4250 | * These next two could probably be done smarter with reusing a common function | |
4251 | * with different iterators and tests... | |
4252 | */ | |
4253 | int list_defined_containers(const char *lxcpath, char ***names, struct lxc_container ***cret) | |
4254 | { | |
4255 | DIR *dir; | |
4256 | int i, cfound = 0, nfound = 0; | |
74f96976 | 4257 | struct dirent *direntp; |
a41f104b SH |
4258 | struct lxc_container *c; |
4259 | ||
4260 | if (!lxcpath) | |
593e8478 | 4261 | lxcpath = lxc_global_config_value("lxc.lxcpath"); |
a41f104b | 4262 | |
a41f104b | 4263 | dir = opendir(lxcpath); |
a41f104b SH |
4264 | if (!dir) { |
4265 | SYSERROR("opendir on lxcpath"); | |
4266 | return -1; | |
4267 | } | |
4268 | ||
4269 | if (cret) | |
4270 | *cret = NULL; | |
4271 | if (names) | |
4272 | *names = NULL; | |
4273 | ||
74f96976 | 4274 | while ((direntp = readdir(dir))) { |
a41f104b SH |
4275 | if (!direntp) |
4276 | break; | |
e4ebeab1 CALP |
4277 | |
4278 | // Ignore '.', '..' and any hidden directory | |
4279 | if (!strncmp(direntp->d_name, ".", 1)) | |
a41f104b SH |
4280 | continue; |
4281 | ||
4282 | if (!config_file_exists(lxcpath, direntp->d_name)) | |
4283 | continue; | |
4284 | ||
4285 | if (names) { | |
9c88ff1f | 4286 | if (!add_to_array(names, direntp->d_name, cfound)) |
a41f104b SH |
4287 | goto free_bad; |
4288 | } | |
4289 | cfound++; | |
4290 | ||
4291 | if (!cret) { | |
4292 | nfound++; | |
4293 | continue; | |
4294 | } | |
4295 | ||
4296 | c = lxc_container_new(direntp->d_name, lxcpath); | |
4297 | if (!c) { | |
4298 | INFO("Container %s:%s has a config but could not be loaded", | |
4299 | lxcpath, direntp->d_name); | |
4300 | if (names) | |
9c88ff1f ÇO |
4301 | if(!remove_from_array(names, direntp->d_name, cfound--)) |
4302 | goto free_bad; | |
a41f104b SH |
4303 | continue; |
4304 | } | |
858377e4 | 4305 | if (!do_lxcapi_is_defined(c)) { |
a41f104b SH |
4306 | INFO("Container %s:%s has a config but is not defined", |
4307 | lxcpath, direntp->d_name); | |
4308 | if (names) | |
9c88ff1f ÇO |
4309 | if(!remove_from_array(names, direntp->d_name, cfound--)) |
4310 | goto free_bad; | |
a41f104b SH |
4311 | lxc_container_put(c); |
4312 | continue; | |
4313 | } | |
4314 | ||
2871830a | 4315 | if (!add_to_clist(cret, c, nfound, true)) { |
a41f104b SH |
4316 | lxc_container_put(c); |
4317 | goto free_bad; | |
4318 | } | |
4319 | nfound++; | |
4320 | } | |
4321 | ||
a41f104b | 4322 | closedir(dir); |
a41f104b SH |
4323 | return nfound; |
4324 | ||
4325 | free_bad: | |
4326 | if (names && *names) { | |
4327 | for (i=0; i<cfound; i++) | |
4328 | free((*names)[i]); | |
4329 | free(*names); | |
4330 | } | |
4331 | if (cret && *cret) { | |
4332 | for (i=0; i<nfound; i++) | |
4333 | lxc_container_put((*cret)[i]); | |
4334 | free(*cret); | |
4335 | } | |
a41f104b | 4336 | closedir(dir); |
a41f104b SH |
4337 | return -1; |
4338 | } | |
4339 | ||
148a9d27 DE |
4340 | int list_active_containers(const char *lxcpath, char ***nret, |
4341 | struct lxc_container ***cret) | |
a41f104b | 4342 | { |
148a9d27 | 4343 | int i, ret = -1, cret_cnt = 0, ct_name_cnt = 0; |
a41f104b SH |
4344 | int lxcpath_len; |
4345 | char *line = NULL; | |
148a9d27 | 4346 | char **ct_name = NULL; |
a41f104b | 4347 | size_t len = 0; |
97bc2422 | 4348 | struct lxc_container *c = NULL; |
88556fd7 | 4349 | bool is_hashed; |
a41f104b SH |
4350 | |
4351 | if (!lxcpath) | |
593e8478 | 4352 | lxcpath = lxc_global_config_value("lxc.lxcpath"); |
a41f104b SH |
4353 | lxcpath_len = strlen(lxcpath); |
4354 | ||
4355 | if (cret) | |
4356 | *cret = NULL; | |
148a9d27 DE |
4357 | if (nret) |
4358 | *nret = NULL; | |
a41f104b | 4359 | |
a41f104b | 4360 | FILE *f = fopen("/proc/net/unix", "r"); |
a41f104b SH |
4361 | if (!f) |
4362 | return -1; | |
4363 | ||
4364 | while (getline(&line, &len, f) != -1) { | |
88556fd7 | 4365 | |
0f8f9c8a | 4366 | char *p = strrchr(line, ' '), *p2; |
a41f104b SH |
4367 | if (!p) |
4368 | continue; | |
4369 | p++; | |
4370 | if (*p != 0x40) | |
4371 | continue; | |
4372 | p++; | |
88556fd7 ÇO |
4373 | |
4374 | is_hashed = false; | |
4375 | if (strncmp(p, lxcpath, lxcpath_len) == 0) { | |
4376 | p += lxcpath_len; | |
4377 | } else if (strncmp(p, "lxc/", 4) == 0) { | |
4378 | p += 4; | |
4379 | is_hashed = true; | |
4380 | } else { | |
a41f104b | 4381 | continue; |
88556fd7 ÇO |
4382 | } |
4383 | ||
a41f104b SH |
4384 | while (*p == '/') |
4385 | p++; | |
4386 | ||
4387 | // Now p is the start of lxc_name | |
46cd2845 | 4388 | p2 = strchr(p, '/'); |
a41f104b SH |
4389 | if (!p2 || strncmp(p2, "/command", 8) != 0) |
4390 | continue; | |
4391 | *p2 = '\0'; | |
4392 | ||
88556fd7 | 4393 | if (is_hashed) { |
899a9f55 CB |
4394 | char *recvpath = lxc_cmd_get_lxcpath(p); |
4395 | if (!recvpath) | |
4396 | continue; | |
4397 | if (strncmp(lxcpath, recvpath, lxcpath_len) != 0) | |
88556fd7 ÇO |
4398 | continue; |
4399 | p = lxc_cmd_get_name(p); | |
4400 | } | |
4401 | ||
148a9d27 | 4402 | if (array_contains(&ct_name, p, ct_name_cnt)) |
9c88ff1f ÇO |
4403 | continue; |
4404 | ||
148a9d27 DE |
4405 | if (!add_to_array(&ct_name, p, ct_name_cnt)) |
4406 | goto free_cret_list; | |
9c88ff1f | 4407 | |
148a9d27 | 4408 | ct_name_cnt++; |
a41f104b | 4409 | |
148a9d27 | 4410 | if (!cret) |
a41f104b | 4411 | continue; |
a41f104b SH |
4412 | |
4413 | c = lxc_container_new(p, lxcpath); | |
4414 | if (!c) { | |
4415 | INFO("Container %s:%s is running but could not be loaded", | |
4416 | lxcpath, p); | |
148a9d27 | 4417 | remove_from_array(&ct_name, p, ct_name_cnt--); |
a41f104b SH |
4418 | continue; |
4419 | } | |
4420 | ||
4421 | /* | |
4422 | * If this is an anonymous container, then is_defined *can* | |
4423 | * return false. So we don't do that check. Count on the | |
4424 | * fact that the command socket exists. | |
4425 | */ | |
4426 | ||
148a9d27 | 4427 | if (!add_to_clist(cret, c, cret_cnt, true)) { |
a41f104b | 4428 | lxc_container_put(c); |
148a9d27 | 4429 | goto free_cret_list; |
a41f104b | 4430 | } |
148a9d27 | 4431 | cret_cnt++; |
a41f104b SH |
4432 | } |
4433 | ||
97bc2422 CB |
4434 | if (nret && cret && cret_cnt != ct_name_cnt) { |
4435 | if (c) | |
4436 | lxc_container_put(c); | |
4437 | goto free_cret_list; | |
4438 | } | |
4439 | ||
148a9d27 DE |
4440 | ret = ct_name_cnt; |
4441 | if (nret) | |
4442 | *nret = ct_name; | |
4443 | else | |
4444 | goto free_ct_name; | |
4445 | goto out; | |
a41f104b | 4446 | |
148a9d27 | 4447 | free_cret_list: |
a41f104b | 4448 | if (cret && *cret) { |
148a9d27 | 4449 | for (i = 0; i < cret_cnt; i++) |
a41f104b SH |
4450 | lxc_container_put((*cret)[i]); |
4451 | free(*cret); | |
4452 | } | |
148a9d27 DE |
4453 | |
4454 | free_ct_name: | |
4455 | if (ct_name) { | |
4456 | for (i = 0; i < ct_name_cnt; i++) | |
4457 | free(ct_name[i]); | |
4458 | free(ct_name); | |
4459 | } | |
4460 | ||
4461 | out: | |
f10fad2f | 4462 | free(line); |
e853a32d | 4463 | |
a41f104b | 4464 | fclose(f); |
148a9d27 | 4465 | return ret; |
a41f104b | 4466 | } |
2871830a DE |
4467 | |
4468 | int list_all_containers(const char *lxcpath, char ***nret, | |
4469 | struct lxc_container ***cret) | |
4470 | { | |
4471 | int i, ret, active_cnt, ct_cnt, ct_list_cnt; | |
4472 | char **active_name; | |
4473 | char **ct_name; | |
4474 | struct lxc_container **ct_list = NULL; | |
4475 | ||
4476 | ct_cnt = list_defined_containers(lxcpath, &ct_name, NULL); | |
4477 | if (ct_cnt < 0) | |
4478 | return ct_cnt; | |
4479 | ||
4480 | active_cnt = list_active_containers(lxcpath, &active_name, NULL); | |
4481 | if (active_cnt < 0) { | |
4482 | ret = active_cnt; | |
4483 | goto free_ct_name; | |
4484 | } | |
4485 | ||
4486 | for (i = 0; i < active_cnt; i++) { | |
4487 | if (!array_contains(&ct_name, active_name[i], ct_cnt)) { | |
4488 | if (!add_to_array(&ct_name, active_name[i], ct_cnt)) { | |
4489 | ret = -1; | |
4490 | goto free_active_name; | |
4491 | } | |
4492 | ct_cnt++; | |
4493 | } | |
4494 | free(active_name[i]); | |
4495 | active_name[i] = NULL; | |
4496 | } | |
4497 | free(active_name); | |
4498 | active_name = NULL; | |
4499 | active_cnt = 0; | |
4500 | ||
4501 | for (i = 0, ct_list_cnt = 0; i < ct_cnt && cret; i++) { | |
4502 | struct lxc_container *c; | |
4503 | ||
4504 | c = lxc_container_new(ct_name[i], lxcpath); | |
4505 | if (!c) { | |
4506 | WARN("Container %s:%s could not be loaded", lxcpath, ct_name[i]); | |
4507 | remove_from_array(&ct_name, ct_name[i], ct_cnt--); | |
4508 | continue; | |
4509 | } | |
4510 | ||
4511 | if (!add_to_clist(&ct_list, c, ct_list_cnt, false)) { | |
4512 | lxc_container_put(c); | |
4513 | ret = -1; | |
4514 | goto free_ct_list; | |
4515 | } | |
4516 | ct_list_cnt++; | |
4517 | } | |
4518 | ||
4519 | if (cret) | |
4520 | *cret = ct_list; | |
4521 | ||
4522 | if (nret) | |
4523 | *nret = ct_name; | |
4524 | else { | |
4525 | ret = ct_cnt; | |
4526 | goto free_ct_name; | |
4527 | } | |
4528 | return ct_cnt; | |
4529 | ||
4530 | free_ct_list: | |
4531 | for (i = 0; i < ct_list_cnt; i++) { | |
4532 | lxc_container_put(ct_list[i]); | |
4533 | } | |
f10fad2f | 4534 | free(ct_list); |
2871830a DE |
4535 | |
4536 | free_active_name: | |
4537 | for (i = 0; i < active_cnt; i++) { | |
f10fad2f | 4538 | free(active_name[i]); |
2871830a | 4539 | } |
f10fad2f | 4540 | free(active_name); |
2871830a DE |
4541 | |
4542 | free_ct_name: | |
4543 | for (i = 0; i < ct_cnt; i++) { | |
4544 | free(ct_name[i]); | |
4545 | } | |
4546 | free(ct_name); | |
4547 | return ret; | |
4548 | } | |
12461428 CB |
4549 | |
4550 | bool lxc_config_item_is_supported(const char *key) | |
4551 | { | |
4552 | return !!lxc_getconfig(key); | |
4553 | } |