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