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