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