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