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