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