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