3 * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
4 * Copyright © 2012 Canonical Ltd.
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.
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.
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
33 #include <arpa/inet.h>
34 #include <sys/sysmacros.h>
36 #include <sys/mount.h>
37 #include <sys/syscall.h>
38 #include <sys/types.h>
47 #include "commands_utils.h"
49 #include "confile_utils.h"
52 #include "initutils.h"
55 #include "lxccontainer.h"
58 #include "namespace.h"
63 #include "storage_utils.h"
64 #include "storage/btrfs.h"
65 #include "storage/overlay.h"
72 # include <sys/mkdev.h>
78 #include <../include/ifaddrs.h>
82 #include <../include/lxcmntent.h>
87 #define MAX_BUFFER 4096
89 #define NOT_SUPPORTED_ERROR "the requested function %s is not currently supported with unprivileged containers"
91 /* Define faccessat() if missing from the C library */
92 #ifndef HAVE_FACCESSAT
93 static int faccessat(int __fd
, const char *__file
, int __type
, int __flag
)
96 return syscall(__NR_faccessat
, __fd
, __file
, __type
, __flag
);
104 lxc_log_define(lxc_container
, lxc
);
106 static bool do_lxcapi_destroy(struct lxc_container
*c
);
107 static const char *lxcapi_get_config_path(struct lxc_container
*c
);
108 #define do_lxcapi_get_config_path(c) lxcapi_get_config_path(c)
109 static bool do_lxcapi_set_config_item(struct lxc_container
*c
, const char *key
, const char *v
);
110 static bool container_destroy(struct lxc_container
*c
,
111 struct lxc_storage
*storage
);
112 static bool get_snappath_dir(struct lxc_container
*c
, char *snappath
);
113 static bool lxcapi_snapshot_destroy_all(struct lxc_container
*c
);
114 static bool do_lxcapi_save_config(struct lxc_container
*c
, const char *alt_file
);
116 static bool config_file_exists(const char *lxcpath
, const char *cname
)
118 /* $lxcpath + '/' + $cname + '/config' + \0 */
119 int ret
, len
= strlen(lxcpath
) + strlen(cname
) + 9;
120 char *fname
= alloca(len
);
122 ret
= snprintf(fname
, len
, "%s/%s/config", lxcpath
, cname
);
123 if (ret
< 0 || ret
>= len
)
126 return file_exists(fname
);
130 * A few functions to help detect when a container creation failed.
131 * If a container creation was killed partway through, then trying
132 * to actually start that container could harm the host. We detect
133 * this by creating a 'partial' file under the container directory,
134 * and keeping an advisory lock. When container creation completes,
135 * we remove that file. When we load or try to start a container, if
136 * we find that file, without a flock, we remove the container.
138 static int ongoing_create(struct lxc_container
*c
)
140 int len
= strlen(c
->config_path
) + strlen(c
->name
) + 10;
141 char *path
= alloca(len
);
145 ret
= snprintf(path
, len
, "%s/%s/partial", c
->config_path
, c
->name
);
146 if (ret
< 0 || ret
>= len
) {
147 ERROR("Error writing partial pathname");
151 if (!file_exists(path
))
153 fd
= open(path
, O_RDWR
);
155 /* give benefit of the doubt */
156 SYSERROR("Error opening partial file");
160 lk
.l_whence
= SEEK_SET
;
164 if (fcntl(fd
, F_GETLK
, &lk
) == 0 && lk
.l_pid
!= -1) {
165 /* create is still ongoing */
169 /* create completed but partial is still there. */
174 static int create_partial(struct lxc_container
*c
)
176 /* $lxcpath + '/' + $name + '/partial' + \0 */
177 int len
= strlen(c
->config_path
) + strlen(c
->name
) + 10;
178 char *path
= alloca(len
);
182 ret
= snprintf(path
, len
, "%s/%s/partial", c
->config_path
, c
->name
);
183 if (ret
< 0 || ret
>= len
) {
184 ERROR("Error writing partial pathname");
187 if ((fd
=open(path
, O_RDWR
| O_CREAT
| O_EXCL
, 0755)) < 0) {
188 SYSERROR("Error creating partial file");
192 lk
.l_whence
= SEEK_SET
;
195 if (fcntl(fd
, F_SETLKW
, &lk
) < 0) {
196 SYSERROR("Error locking partial file %s", path
);
204 static void remove_partial(struct lxc_container
*c
, int fd
)
206 /* $lxcpath + '/' + $name + '/partial' + \0 */
207 int len
= strlen(c
->config_path
) + strlen(c
->name
) + 10;
208 char *path
= alloca(len
);
212 ret
= snprintf(path
, len
, "%s/%s/partial", c
->config_path
, c
->name
);
213 if (ret
< 0 || ret
>= len
) {
214 ERROR("Error writing partial pathname");
217 if (unlink(path
) < 0)
218 SYSERROR("Error unlink partial file %s", path
);
222 * 1. container_mem_lock(c) protects the struct lxc_container from multiple threads.
223 * 2. container_disk_lock(c) protects the on-disk container data - in particular the
224 * container configuration file.
225 * The container_disk_lock also takes the container_mem_lock.
226 * 3. thread_mutex protects process data (ex: fd table) from multiple threads.
227 * NOTHING mutexes two independent programs with their own struct
228 * lxc_container for the same c->name, between API calls. For instance,
229 * c->config_read(); c->start(); Between those calls, data on disk
230 * could change (which shouldn't bother the caller unless for instance
231 * the rootfs get moved). c->config_read(); update; c->config_write();
232 * Two such updaters could race. The callers should therefore check their
233 * results. Trying to prevent that would necessarily expose us to deadlocks
234 * due to hung callers. So I prefer to keep the locks only within our own
235 * functions, not across functions.
237 * If you're going to clone while holding a lxccontainer, increment
238 * c->numthreads (under privlock) before forking. When deleting,
239 * decrement numthreads under privlock, then if it hits 0 you can delete.
240 * Do not ever use a lxccontainer whose numthreads you did not bump.
243 static void lxc_container_free(struct lxc_container
*c
)
249 c
->configfile
= NULL
;
250 free(c
->error_string
);
251 c
->error_string
= NULL
;
253 lxc_putlock(c
->slock
);
257 lxc_putlock(c
->privlock
);
263 lxc_conf_free(c
->lxc_conf
);
266 free(c
->config_path
);
267 c
->config_path
= NULL
;
273 * Consider the following case:
274 freer | racing get()er
275 ==================================================================
276 lxc_container_put() | lxc_container_get()
277 \ lxclock(c->privlock) | c->numthreads < 1? (no)
278 \ c->numthreads = 0 | \ lxclock(c->privlock) -> waits
280 \ lxc_container_free() | \ lxclock() returns
281 | \ c->numthreads < 1 -> return 0
283 \ \ sem_destroy(privlock) |
285 * When the get()er checks numthreads the first time, one of the following
287 * 1. freer has set numthreads = 0. get() returns 0
288 * 2. freer is between lxclock and setting numthreads to 0. get()er will
289 * sem_wait on privlock, get lxclock after freer() drops it, then see
290 * numthreads is 0 and exit without touching lxclock again..
291 * 3. freer has not yet locked privlock. If get()er runs first, then put()er
292 * will see --numthreads = 1 and not call lxc_container_free().
295 int lxc_container_get(struct lxc_container
*c
)
300 /* If someone else has already started freeing the container, don't try
301 * to take the lock, which may be invalid.
303 if (c
->numthreads
< 1)
306 if (container_mem_lock(c
))
309 /* Bail without trying to unlock, bc the privlock is now probably in
312 if (c
->numthreads
< 1)
316 container_mem_unlock(c
);
320 int lxc_container_put(struct lxc_container
*c
)
324 if (container_mem_lock(c
))
326 if (--c
->numthreads
< 1) {
327 container_mem_unlock(c
);
328 lxc_container_free(c
);
331 container_mem_unlock(c
);
335 static bool do_lxcapi_is_defined(struct lxc_container
*c
)
344 if (container_mem_lock(c
))
348 statret
= stat(c
->configfile
, &statbuf
);
354 container_mem_unlock(c
);
358 #define WRAP_API(rettype, fnname) \
359 static rettype fnname(struct lxc_container *c) \
362 bool reset_config = false; \
364 if (!current_config && c && c->lxc_conf) { \
365 current_config = c->lxc_conf; \
366 reset_config = true; \
369 ret = do_##fnname(c); \
371 current_config = NULL; \
376 #define WRAP_API_1(rettype, fnname, t1) \
377 static rettype fnname(struct lxc_container *c, t1 a1) \
380 bool reset_config = false; \
382 if (!current_config && c && c->lxc_conf) { \
383 current_config = c->lxc_conf; \
384 reset_config = true; \
387 ret = do_##fnname(c, a1); \
389 current_config = NULL; \
394 #define WRAP_API_2(rettype, fnname, t1, t2) \
395 static rettype fnname(struct lxc_container *c, t1 a1, t2 a2) \
398 bool reset_config = false; \
400 if (!current_config && c && c->lxc_conf) { \
401 current_config = c->lxc_conf; \
402 reset_config = true; \
405 ret = do_##fnname(c, a1, a2); \
407 current_config = NULL; \
412 #define WRAP_API_3(rettype, fnname, t1, t2, t3) \
413 static rettype fnname(struct lxc_container *c, t1 a1, t2 a2, t3 a3) \
416 bool reset_config = false; \
418 if (!current_config && c && c->lxc_conf) { \
419 current_config = c->lxc_conf; \
420 reset_config = true; \
423 ret = do_##fnname(c, a1, a2, a3); \
425 current_config = NULL; \
430 WRAP_API(bool, lxcapi_is_defined
)
432 static const char *do_lxcapi_state(struct lxc_container
*c
)
438 s
= lxc_getstate(c
->name
, c
->config_path
);
439 return lxc_state2str(s
);
442 WRAP_API(const char *, lxcapi_state
)
444 static bool is_stopped(struct lxc_container
*c
)
447 s
= lxc_getstate(c
->name
, c
->config_path
);
448 return (s
== STOPPED
);
451 static bool do_lxcapi_is_running(struct lxc_container
*c
)
457 s
= do_lxcapi_state(c
);
458 if (!s
|| strcmp(s
, "STOPPED") == 0)
463 WRAP_API(bool, lxcapi_is_running
)
465 static bool do_lxcapi_freeze(struct lxc_container
*c
)
471 ret
= lxc_freeze(c
->name
, c
->config_path
);
477 WRAP_API(bool, lxcapi_freeze
)
479 static bool do_lxcapi_unfreeze(struct lxc_container
*c
)
485 ret
= lxc_unfreeze(c
->name
, c
->config_path
);
491 WRAP_API(bool, lxcapi_unfreeze
)
493 static int do_lxcapi_console_getfd(struct lxc_container
*c
, int *ttynum
, int *masterfd
)
499 ttyfd
= lxc_console_getfd(c
, ttynum
, masterfd
);
503 WRAP_API_2(int, lxcapi_console_getfd
, int *, int *)
505 static int lxcapi_console(struct lxc_container
*c
, int ttynum
, int stdinfd
,
506 int stdoutfd
, int stderrfd
, int escape
)
513 current_config
= c
->lxc_conf
;
514 ret
= lxc_console(c
, ttynum
, stdinfd
, stdoutfd
, stderrfd
, escape
);
515 current_config
= NULL
;
519 static int do_lxcapi_console_log(struct lxc_container
*c
, struct lxc_console_log
*log
)
523 ret
= lxc_cmd_console_log(c
->name
, do_lxcapi_get_config_path(c
), log
);
526 NOTICE("The console log is empty");
527 else if (ret
== -EFAULT
)
528 NOTICE("The container does not keep a console log");
529 else if (ret
== -ENOENT
)
530 NOTICE("The container does not keep a console log file");
531 else if (ret
== -EIO
)
532 NOTICE("Failed to write console log to log file");
534 ERROR("Failed to retrieve console log");
540 WRAP_API_1(int, lxcapi_console_log
, struct lxc_console_log
*)
542 static pid_t
do_lxcapi_init_pid(struct lxc_container
*c
)
547 return lxc_cmd_get_init_pid(c
->name
, c
->config_path
);
550 WRAP_API(pid_t
, lxcapi_init_pid
)
552 static bool load_config_locked(struct lxc_container
*c
, const char *fname
)
555 c
->lxc_conf
= lxc_conf_init();
558 if (lxc_config_read(fname
, c
->lxc_conf
, false) != 0)
563 static bool do_lxcapi_load_config(struct lxc_container
*c
, const char *alt_file
)
565 bool ret
= false, need_disklock
= false;
571 fname
= c
->configfile
;
577 * If we're reading something other than the container's config,
578 * we only need to lock the in-memory container. If loading the
579 * container's config file, take the disk lock.
581 if (strcmp(fname
, c
->configfile
) == 0)
582 need_disklock
= true;
585 lret
= container_disk_lock(c
);
587 lret
= container_mem_lock(c
);
591 ret
= load_config_locked(c
, fname
);
594 container_disk_unlock(c
);
596 container_mem_unlock(c
);
600 WRAP_API_1(bool, lxcapi_load_config
, const char *)
602 static bool do_lxcapi_want_daemonize(struct lxc_container
*c
, bool state
)
604 if (!c
|| !c
->lxc_conf
)
606 if (container_mem_lock(c
)) {
607 ERROR("Error getting mem lock");
610 c
->daemonize
= state
;
611 container_mem_unlock(c
);
615 WRAP_API_1(bool, lxcapi_want_daemonize
, bool)
617 static bool do_lxcapi_want_close_all_fds(struct lxc_container
*c
, bool state
)
619 if (!c
|| !c
->lxc_conf
)
621 if (container_mem_lock(c
)) {
622 ERROR("Error getting mem lock");
625 c
->lxc_conf
->close_all_fds
= state
;
626 container_mem_unlock(c
);
630 WRAP_API_1(bool, lxcapi_want_close_all_fds
, bool)
632 static bool do_lxcapi_wait(struct lxc_container
*c
, const char *state
, int timeout
)
639 ret
= lxc_wait(c
->name
, state
, timeout
, c
->config_path
);
643 WRAP_API_2(bool, lxcapi_wait
, const char *, int)
645 static bool am_single_threaded(void)
647 struct dirent
*direntp
;
651 dir
= opendir("/proc/self/task");
653 INFO("failed to open /proc/self/task");
657 while ((direntp
= readdir(dir
))) {
658 if (!strcmp(direntp
->d_name
, "."))
661 if (!strcmp(direntp
->d_name
, ".."))
670 static void push_arg(char ***argp
, char *arg
, int *nargs
)
679 argv
= realloc(*argp
, (*nargs
+ 2) * sizeof(char *));
687 static char **split_init_cmd(const char *incmd
)
691 char *copy
, *p
, *saveptr
= NULL
;
697 len
= strlen(incmd
) + 1;
699 strncpy(copy
, incmd
, len
);
703 argv
= malloc(sizeof(char *));
706 for (p
= strtok_r(copy
, " ", &saveptr
); p
!= NULL
;
707 p
= strtok_r(NULL
, " ", &saveptr
))
708 push_arg(&argv
, p
, &nargs
);
717 static void free_init_cmd(char **argv
)
728 static int lxc_rcv_status(int state_socket
)
734 /* Receive container state. */
735 ret
= lxc_abstract_unix_rcv_credential(state_socket
, &state
,
740 TRACE("Caught EINTR; retrying");
747 static bool wait_on_daemonized_start(struct lxc_handler
*handler
, int pid
)
751 /* Close write end of the socket pair. */
752 close(handler
->state_socket_pair
[1]);
753 handler
->state_socket_pair
[1] = -1;
755 state
= lxc_rcv_status(handler
->state_socket_pair
[0]);
757 /* Close read end of the socket pair. */
758 close(handler
->state_socket_pair
[0]);
759 handler
->state_socket_pair
[0] = -1;
761 /* The first child is going to fork() again and then exits. So we reap
762 * the first child here.
764 if (waitpid(pid
, &status
, 0) < 0)
765 DEBUG("Failed waiting on first child");
766 else if (!WIFEXITED(status
))
767 DEBUG("Failed to retrieve exit status of first child");
768 else if (WEXITSTATUS(status
) != 0)
769 DEBUG("First child exited with: %d", WEXITSTATUS(status
));
772 SYSERROR("Failed to receive the container state");
776 /* If we receive anything else then running we know that the container
779 if (state
!= RUNNING
) {
780 ERROR("Received container state \"%s\" instead of \"RUNNING\"",
781 lxc_state2str(state
));
785 TRACE("Container is in \"RUNNING\" state");
789 static bool do_lxcapi_start(struct lxc_container
*c
, int useinit
, char * const argv
[])
792 struct lxc_handler
*handler
;
793 struct lxc_conf
*conf
;
794 bool daemonize
= false;
796 char *default_args
[] = {
800 char **init_cmd
= NULL
;
801 int keepfds
[3] = {-1, -1, -1};
803 /* container does exist */
807 /* If anything fails before we set error_num, we want an error in there.
811 /* Container has not been setup. */
815 ret
= ongoing_create(c
);
817 ERROR("Failed checking for incomplete container creation");
819 } else if (ret
== 1) {
820 ERROR("Ongoing container creation detected");
822 } else if (ret
== 2) {
823 ERROR("Failed to create container");
824 do_lxcapi_destroy(c
);
828 if (container_mem_lock(c
))
831 daemonize
= c
->daemonize
;
833 /* initialize handler */
834 handler
= lxc_init_handler(c
->name
, conf
, c
->config_path
, daemonize
);
835 container_mem_unlock(c
);
840 if (useinit
&& conf
->execute_cmd
)
841 argv
= init_cmd
= split_init_cmd(conf
->execute_cmd
);
843 argv
= init_cmd
= split_init_cmd(conf
->init_cmd
);
846 /* ... otherwise use default_args. */
849 ERROR("No valid init detected");
850 lxc_free_handler(handler
);
856 /* I'm not sure what locks we want here.Any? Is liblxc's locking enough
857 * here to protect the on disk container? We don't want to exclude
858 * things like lxc_info while the container is running.
867 free_init_cmd(init_cmd
);
868 lxc_free_handler(handler
);
874 /* Set to NULL because we don't want father unlink
875 * the PID file, child will do the free and unlink.
879 /* Wait for container to tell us whether it started
882 started
= wait_on_daemonized_start(handler
, pid
);
884 free_init_cmd(init_cmd
);
885 lxc_free_handler(handler
);
891 /* We don't really care if this doesn't print all the
892 * characters. All that it means is that the proctitle will be
893 * ugly. Similarly, we also don't care if setproctitle() fails.
895 snprintf(title
, sizeof(title
), "[lxc monitor] %s %s", c
->config_path
, c
->name
);
896 INFO("Attempting to set proc title to %s", title
);
899 /* We fork() a second time to be reparented to init. Like
900 * POSIX's daemon() function we change to "/" and redirect
901 * std{in,out,err} to /dev/null.
905 SYSERROR("Failed to fork first child process");
911 free_init_cmd(init_cmd
);
912 lxc_free_handler(handler
);
918 /* change to / directory */
921 SYSERROR("Failed to change to \"/\" directory");
925 keepfds
[0] = handler
->conf
->maincmd_fd
;
926 keepfds
[1] = handler
->state_socket_pair
[0];
927 keepfds
[2] = handler
->state_socket_pair
[1];
928 ret
= lxc_check_inherited(conf
, true, keepfds
,
929 sizeof(keepfds
) / sizeof(keepfds
[0]));
933 /* redirect std{in,out,err} to /dev/null */
936 ERROR("Failed to redirect std{in,out,err} to /dev/null");
940 /* become session leader */
943 TRACE("Process %d is already process group leader", lxc_raw_getpid());
945 if (!am_single_threaded()) {
946 ERROR("Cannot start non-daemonized container when threaded");
947 free_init_cmd(init_cmd
);
948 lxc_free_handler(handler
);
953 /* We need to write PID file after daemonize, so we always
954 * write the right PID.
957 pid_fp
= fopen(c
->pidfile
, "w");
958 if (pid_fp
== NULL
) {
959 SYSERROR("Failed to create pidfile '%s' for '%s'",
960 c
->pidfile
, c
->name
);
961 free_init_cmd(init_cmd
);
962 lxc_free_handler(handler
);
968 if (fprintf(pid_fp
, "%d\n", lxc_raw_getpid()) < 0) {
969 SYSERROR("Failed to write '%s'", c
->pidfile
);
972 free_init_cmd(init_cmd
);
973 lxc_free_handler(handler
);
985 /* Unshare the mount namespace if requested */
986 if (conf
->monitor_unshare
) {
987 ret
= unshare(CLONE_NEWNS
);
989 SYSERROR("failed to unshare mount namespace");
990 lxc_free_handler(handler
);
995 ret
= mount(NULL
, "/", NULL
, MS_SLAVE
|MS_REC
, NULL
);
997 SYSERROR("Failed to make / rslave at startup");
998 lxc_free_handler(handler
);
1005 if (conf
->reboot
== 2) {
1006 /* initialize handler */
1007 handler
= lxc_init_handler(c
->name
, conf
, c
->config_path
, daemonize
);
1014 keepfds
[0] = handler
->conf
->maincmd_fd
;
1015 keepfds
[1] = handler
->state_socket_pair
[0];
1016 keepfds
[2] = handler
->state_socket_pair
[1];
1017 ret
= lxc_check_inherited(conf
, daemonize
, keepfds
,
1018 sizeof(keepfds
) / sizeof(keepfds
[0]));
1020 lxc_free_handler(handler
);
1026 ret
= lxc_execute(c
->name
, argv
, 1, handler
, c
->config_path
, daemonize
);
1028 ret
= lxc_start(c
->name
, argv
, handler
, c
->config_path
, daemonize
);
1029 c
->error_num
= handler
->exit_status
;
1031 if (conf
->reboot
== 1) {
1032 INFO("Container requested reboot");
1043 free_init_cmd(init_cmd
);
1045 if (daemonize
&& ret
!= 0)
1056 static bool lxcapi_start(struct lxc_container
*c
, int useinit
, char * const argv
[])
1059 current_config
= c
? c
->lxc_conf
: NULL
;
1060 ret
= do_lxcapi_start(c
, useinit
, argv
);
1061 current_config
= NULL
;
1066 * note there MUST be an ending NULL
1068 static bool lxcapi_startl(struct lxc_container
*c
, int useinit
, ...)
1071 char **inargs
= NULL
;
1074 /* container exists */
1078 current_config
= c
->lxc_conf
;
1080 va_start(ap
, useinit
);
1081 inargs
= lxc_va_arg_list_to_argv(ap
, 0, 1);
1085 ERROR("Memory allocation error.");
1089 /* pass NULL if no arguments were supplied */
1090 bret
= do_lxcapi_start(c
, useinit
, *inargs
? inargs
: NULL
);
1095 for (arg
= inargs
; *arg
; arg
++)
1100 current_config
= NULL
;
1104 static bool do_lxcapi_stop(struct lxc_container
*c
)
1111 ret
= lxc_cmd_stop(c
->name
, c
->config_path
);
1116 WRAP_API(bool, lxcapi_stop
)
1118 static int do_create_container_dir(const char *path
, struct lxc_conf
*conf
)
1120 int ret
= -1, lasterr
;
1121 char *p
= alloca(strlen(path
)+1);
1122 mode_t mask
= umask(0002);
1123 ret
= mkdir(path
, 0770);
1128 if (errno
== EEXIST
)
1131 SYSERROR("failed to create container path %s", path
);
1136 if (!lxc_list_empty(&conf
->id_map
) && chown_mapped_root(p
, conf
) != 0) {
1137 ERROR("Failed to chown container dir");
1144 * create the standard expected container dir
1146 static bool create_container_dir(struct lxc_container
*c
)
1151 len
= strlen(c
->config_path
) + strlen(c
->name
) + 2;
1155 ret
= snprintf(s
, len
, "%s/%s", c
->config_path
, c
->name
);
1156 if (ret
< 0 || ret
>= len
) {
1160 ret
= do_create_container_dir(s
, c
->lxc_conf
);
1165 /* do_storage_create: thin wrapper around storage_create(). Like
1166 * storage_create(), it returns a mounted bdev on success, NULL on error.
1168 static struct lxc_storage
*do_storage_create(struct lxc_container
*c
,
1170 struct bdev_specs
*specs
)
1175 struct lxc_storage
*bdev
;
1177 /* rootfs.path or lxcpath/lxcname/rootfs */
1178 if (c
->lxc_conf
->rootfs
.path
&&
1179 (access(c
->lxc_conf
->rootfs
.path
, F_OK
) == 0)) {
1180 const char *rpath
= c
->lxc_conf
->rootfs
.path
;
1181 len
= strlen(rpath
) + 1;
1183 ret
= snprintf(dest
, len
, "%s", rpath
);
1185 const char *lxcpath
= do_lxcapi_get_config_path(c
);
1186 len
= strlen(c
->name
) + strlen(lxcpath
) + 9;
1188 ret
= snprintf(dest
, len
, "%s/%s/rootfs", lxcpath
, c
->name
);
1190 if (ret
< 0 || (size_t)ret
>= len
)
1193 bdev
= storage_create(dest
, type
, c
->name
, specs
);
1195 ERROR("Failed to create \"%s\" storage", type
);
1199 if (!c
->set_config_item(c
, "lxc.rootfs.path", bdev
->src
)) {
1200 ERROR("Failed to set \"lxc.rootfs.path = %s\"", bdev
->src
);
1204 /* If we are not root, chown the rootfs dir to root in the target user
1208 if (ret
!= 0 || (c
->lxc_conf
&& !lxc_list_empty(&c
->lxc_conf
->id_map
))) {
1209 ret
= chown_mapped_root(bdev
->dest
, c
->lxc_conf
);
1211 ERROR("Error chowning \"%s\" to container root", bdev
->dest
);
1212 suggest_default_idmap();
1221 static char *lxcbasename(char *path
)
1223 char *p
= path
+ strlen(path
) - 1;
1224 while (*p
!= '/' && p
> path
)
1229 static bool create_run_template(struct lxc_container
*c
, char *tpath
, bool need_null_stdfds
,
1239 SYSERROR("failed to fork task for container creation template");
1243 if (pid
== 0) { /* child */
1244 char *patharg
, *namearg
, *rootfsarg
;
1245 struct lxc_storage
*bdev
= NULL
;
1247 int ret
, len
, nargs
= 0;
1249 struct lxc_conf
*conf
= c
->lxc_conf
;
1251 if (need_null_stdfds
&& null_stdfds() < 0) {
1255 bdev
= storage_init(c
->lxc_conf
);
1257 ERROR("Error opening rootfs");
1261 if (geteuid() == 0) {
1262 if (unshare(CLONE_NEWNS
) < 0) {
1263 ERROR("error unsharing mounts");
1266 if (detect_shared_rootfs()) {
1267 if (mount(NULL
, "/", NULL
, MS_SLAVE
|MS_REC
, NULL
)) {
1268 SYSERROR("Failed to make / rslave to run template");
1269 ERROR("Continuing...");
1273 if (strcmp(bdev
->type
, "dir") && strcmp(bdev
->type
, "btrfs")) {
1274 if (geteuid() != 0) {
1275 ERROR("non-root users can only create btrfs and directory-backed containers");
1279 if (!strcmp(bdev
->type
, "overlay") || !strcmp(bdev
->type
, "overlayfs")) {
1280 /* If we create an overlay container we need to
1281 * rsync the contents into
1282 * <container-path>/<container-name>/rootfs.
1283 * However, the overlay mount function will
1285 * <container-path>/<container-name>/delta0
1287 * <container-path>/<container-name>/rootfs
1288 * which means we would rsync the rootfs into
1289 * the delta directory. That doesn't make sense
1290 * since the delta directory only exists to
1291 * record the differences to
1292 * <container-path>/<container-name>/rootfs. So
1293 * let's simply bind-mount here and then rsync
1295 * <container-path>/<container-name>/rootfs.
1299 src
= ovl_get_rootfs(bdev
->src
, &(size_t){0});
1301 ERROR("Failed to get rootfs");
1305 ret
= mount(src
, bdev
->dest
, "bind", MS_BIND
| MS_REC
, NULL
);
1307 ERROR("Failed to mount rootfs");
1311 if (bdev
->ops
->mount(bdev
) < 0) {
1312 ERROR("Failed to mount rootfs");
1316 } else { /* TODO come up with a better way here! */
1319 src
= lxc_storage_get_path(bdev
->src
, bdev
->type
);
1320 bdev
->dest
= strdup(src
);
1324 * create our new array, pre-pend the template name and
1328 for (nargs
= 0; argv
[nargs
]; nargs
++) ;
1329 nargs
+= 4; /* template, path, rootfs and name args */
1331 newargv
= malloc(nargs
* sizeof(*newargv
));
1334 newargv
[0] = lxcbasename(tpath
);
1336 len
= strlen(c
->config_path
) + strlen(c
->name
) + strlen("--path=") + 2;
1337 patharg
= malloc(len
);
1340 ret
= snprintf(patharg
, len
, "--path=%s/%s", c
->config_path
, c
->name
);
1341 if (ret
< 0 || ret
>= len
)
1343 newargv
[1] = patharg
;
1344 len
= strlen("--name=") + strlen(c
->name
) + 1;
1345 namearg
= malloc(len
);
1348 ret
= snprintf(namearg
, len
, "--name=%s", c
->name
);
1349 if (ret
< 0 || ret
>= len
)
1351 newargv
[2] = namearg
;
1353 len
= strlen("--rootfs=") + 1 + strlen(bdev
->dest
);
1354 rootfsarg
= malloc(len
);
1357 ret
= snprintf(rootfsarg
, len
, "--rootfs=%s", bdev
->dest
);
1358 if (ret
< 0 || ret
>= len
)
1360 newargv
[3] = rootfsarg
;
1362 /* add passed-in args */
1364 for (i
= 4; i
< nargs
; i
++)
1365 newargv
[i
] = argv
[i
-4];
1367 /* add trailing NULL */
1369 newargv
= realloc(newargv
, nargs
* sizeof(*newargv
));
1372 newargv
[nargs
- 1] = NULL
;
1375 * If we're running the template in a mapped userns, then
1376 * we prepend the template command with:
1377 * lxc-usernsexec <-m map1> ... <-m mapn> --
1378 * and we append "--mapped-uid x", where x is the mapped uid
1381 if (!lxc_list_empty(&conf
->id_map
)) {
1385 char **n2
= malloc(n2args
* sizeof(*n2
));
1386 struct lxc_list
*it
;
1390 SYSERROR("out of memory");
1394 tpath
= "lxc-usernsexec";
1395 n2
[0] = "lxc-usernsexec";
1396 lxc_list_for_each(it
, &conf
->id_map
) {
1399 n2
= realloc(n2
, n2args
* sizeof(char *));
1402 n2
[n2args
-2] = "-m";
1403 n2
[n2args
-1] = malloc(200);
1406 ret
= snprintf(n2
[n2args
-1], 200, "%c:%lu:%lu:%lu",
1407 map
->idtype
== ID_TYPE_UID
? 'u' : 'g',
1408 map
->nsid
, map
->hostid
, map
->range
);
1409 if (ret
< 0 || ret
>= 200)
1412 int hostid_mapped
= mapped_hostid(geteuid(), conf
, ID_TYPE_UID
);
1413 int extraargs
= hostid_mapped
>= 0 ? 1 : 3;
1414 n2
= realloc(n2
, (nargs
+ n2args
+ extraargs
) * sizeof(char *));
1417 if (hostid_mapped
< 0) {
1418 hostid_mapped
= find_unmapped_nsid(conf
, ID_TYPE_UID
);
1419 n2
[n2args
++] = "-m";
1420 if (hostid_mapped
< 0) {
1421 ERROR("Could not find free uid to map");
1424 n2
[n2args
++] = malloc(200);
1425 if (!n2
[n2args
-1]) {
1426 SYSERROR("out of memory");
1429 ret
= snprintf(n2
[n2args
-1], 200, "u:%d:%d:1",
1430 hostid_mapped
, geteuid());
1431 if (ret
< 0 || ret
>= 200) {
1432 ERROR("string too long");
1436 int hostgid_mapped
= mapped_hostid(getegid(), conf
, ID_TYPE_GID
);
1437 extraargs
= hostgid_mapped
>= 0 ? 1 : 3;
1438 n2
= realloc(n2
, (nargs
+ n2args
+ extraargs
) * sizeof(char *));
1441 if (hostgid_mapped
< 0) {
1442 hostgid_mapped
= find_unmapped_nsid(conf
, ID_TYPE_GID
);
1443 n2
[n2args
++] = "-m";
1444 if (hostgid_mapped
< 0) {
1445 ERROR("Could not find free uid to map");
1448 n2
[n2args
++] = malloc(200);
1449 if (!n2
[n2args
-1]) {
1450 SYSERROR("out of memory");
1453 ret
= snprintf(n2
[n2args
-1], 200, "g:%d:%d:1",
1454 hostgid_mapped
, getegid());
1455 if (ret
< 0 || ret
>= 200) {
1456 ERROR("string too long");
1460 n2
[n2args
++] = "--";
1461 for (i
= 0; i
< nargs
; i
++)
1462 n2
[i
+ n2args
] = newargv
[i
];
1464 /* Finally add "--mapped-uid $uid" to tell template
1465 * what to chown cached images to.
1468 n2
= realloc(n2
, n2args
* sizeof(char *));
1470 SYSERROR("out of memory");
1473 /* note n2[n2args-1] is NULL */
1474 n2
[n2args
-5] = "--mapped-uid";
1475 snprintf(txtuid
, 20, "%d", hostid_mapped
);
1476 n2
[n2args
-4] = txtuid
;
1477 n2
[n2args
-3] = "--mapped-gid";
1478 snprintf(txtgid
, 20, "%d", hostgid_mapped
);
1479 n2
[n2args
-2] = txtgid
;
1480 n2
[n2args
-1] = NULL
;
1485 execvp(tpath
, newargv
);
1486 SYSERROR("Failed to execute template %s", tpath
);
1490 if (wait_for_pid(pid
) != 0) {
1491 ERROR("container creation template for %s failed", c
->name
);
1498 static bool prepend_lxc_header(char *path
, const char *t
, char *const argv
[])
1506 unsigned char md_value
[SHA_DIGEST_LENGTH
];
1510 f
= fopen(path
, "r");
1514 if (fseek(f
, 0, SEEK_END
) < 0)
1516 if ((flen
= ftell(f
)) < 0)
1518 if (fseek(f
, 0, SEEK_SET
) < 0)
1520 if ((contents
= malloc(flen
+ 1)) == NULL
)
1522 if (fread(contents
, 1, flen
, f
) != flen
)
1523 goto out_free_contents
;
1525 contents
[flen
] = '\0';
1529 goto out_free_contents
;
1532 tpath
= get_template_path(t
);
1534 ERROR("bad template: %s", t
);
1535 goto out_free_contents
;
1538 ret
= sha1sum_file(tpath
, md_value
);
1540 ERROR("Error getting sha1sum of %s", tpath
);
1542 goto out_free_contents
;
1547 f
= fopen(path
, "w");
1549 SYSERROR("reopening config for writing");
1553 fprintf(f
, "# Template used to create this container: %s\n", t
);
1555 fprintf(f
, "# Parameters passed to the template:");
1557 fprintf(f
, " %s", *argv
);
1563 fprintf(f
, "# Template script checksum (SHA-1): ");
1564 for (i
=0; i
<SHA_DIGEST_LENGTH
; i
++)
1565 fprintf(f
, "%02x", md_value
[i
]);
1568 fprintf(f
, "# For additional config options, please look at lxc.container.conf(5)\n");
1569 fprintf(f
, "\n# Uncomment the following line to support nesting containers:\n");
1570 fprintf(f
, "#lxc.include = " LXCTEMPLATECONFIG
"/nesting.conf\n");
1571 fprintf(f
, "# (Be aware this has security implications)\n\n");
1572 if (fwrite(contents
, 1, flen
, f
) != flen
) {
1573 SYSERROR("Writing original contents");
1589 SYSERROR("Error prepending header");
1595 static void lxcapi_clear_config(struct lxc_container
*c
)
1599 lxc_conf_free(c
->lxc_conf
);
1605 #define do_lxcapi_clear_config(c) lxcapi_clear_config(c)
1609 * create a container with the given parameters.
1610 * @c: container to be created. It has the lxcpath, name, and a starting
1611 * configuration already set
1612 * @t: the template to execute to instantiate the root filesystem and
1613 * adjust the configuration.
1614 * @bdevtype: backing store type to use. If NULL, dir will be used.
1615 * @specs: additional parameters for the backing store, i.e. LVM vg to
1618 * @argv: the arguments to pass to the template, terminated by NULL. If no
1619 * arguments, you can just pass NULL.
1621 static bool do_lxcapi_create(struct lxc_container
*c
, const char *t
,
1622 const char *bdevtype
, struct bdev_specs
*specs
, int flags
,
1634 tpath
= get_template_path(t
);
1636 ERROR("Unknown template \"%s\"", t
);
1641 /* If a template is passed in, and the rootfs already is defined in the
1642 * container config and exists, then the caller is trying to create an
1643 * existing container. Return an error, but do NOT delete the container.
1645 if (do_lxcapi_is_defined(c
) && c
->lxc_conf
&& c
->lxc_conf
->rootfs
.path
&&
1646 access(c
->lxc_conf
->rootfs
.path
, F_OK
) == 0 && tpath
) {
1647 ERROR("Container \"%s\" already exists in \"%s\"", c
->name
,
1653 if (!do_lxcapi_load_config(c
, lxc_global_config_value("lxc.default_config"))) {
1654 ERROR("Error loading default configuration file %s",
1655 lxc_global_config_value("lxc.default_config"));
1660 if (!create_container_dir(c
))
1663 /* If both template and rootfs.path are set, template is setup as
1664 * rootfs.path. The container is already created if we have a config and
1665 * rootfs.path is accessible
1667 if (!c
->lxc_conf
->rootfs
.path
&& !tpath
) {
1668 /* No template passed in and rootfs does not exist. */
1669 if (!c
->save_config(c
, NULL
)) {
1670 ERROR("Failed to save initial config for \"%s\"", c
->name
);
1677 /* Rootfs passed into configuration, but does not exist. */
1678 if (c
->lxc_conf
->rootfs
.path
&& access(c
->lxc_conf
->rootfs
.path
, F_OK
) != 0)
1681 if (do_lxcapi_is_defined(c
) && c
->lxc_conf
->rootfs
.path
&& !tpath
) {
1682 /* Rootfs already existed, user just wanted to save the loaded
1685 if (!c
->save_config(c
, NULL
))
1686 ERROR("Failed to save initial config for \"%s\"", c
->name
);
1691 /* Mark that this container is being created */
1692 partial_fd
= create_partial(c
);
1696 /* No need to get disk lock bc we have the partial lock. */
1698 /* Create the storage.
1699 * Note we can't do this in the same task as we use to execute the
1700 * template because of the way zfs works.
1701 * After you 'zfs create', zfs mounts the fs only in the initial
1706 SYSERROR("Failed to fork task for container creation template");
1710 if (pid
== 0) { /* child */
1711 struct lxc_storage
*bdev
= NULL
;
1713 bdev
= do_storage_create(c
, bdevtype
, specs
);
1715 ERROR("Failed to create %s storage for %s",
1716 bdevtype
? bdevtype
: "(none)", c
->name
);
1720 /* Save config file again to store the new rootfs location. */
1721 if (!do_lxcapi_save_config(c
, NULL
)) {
1722 ERROR("Failed to save initial config for %s", c
->name
);
1723 /* Parent task won't see the storage driver in the
1724 * config so we delete it.
1726 bdev
->ops
->umount(bdev
);
1727 bdev
->ops
->destroy(bdev
);
1732 if (wait_for_pid(pid
) != 0)
1735 /* Reload config to get the rootfs. */
1736 lxc_conf_free(c
->lxc_conf
);
1738 if (!load_config_locked(c
, c
->configfile
))
1741 if (!create_run_template(c
, tpath
, !!(flags
& LXC_CREATE_QUIET
), argv
))
1744 /* Now clear out the lxc_conf we have, reload from the created
1747 do_lxcapi_clear_config(c
);
1750 if (!prepend_lxc_header(c
->configfile
, tpath
, argv
)) {
1751 ERROR("Failed to prepend header to config file");
1755 ret
= load_config_locked(c
, c
->configfile
);
1758 if (partial_fd
>= 0)
1759 remove_partial(c
, partial_fd
);
1762 container_destroy(c
, NULL
);
1768 static bool lxcapi_create(struct lxc_container
*c
, const char *t
,
1769 const char *bdevtype
, struct bdev_specs
*specs
,
1770 int flags
, char *const argv
[])
1773 current_config
= c
? c
->lxc_conf
: NULL
;
1774 ret
= do_lxcapi_create(c
, t
, bdevtype
, specs
, flags
, argv
);
1775 current_config
= NULL
;
1779 static bool do_lxcapi_reboot(struct lxc_container
*c
)
1782 int rebootsignal
= SIGINT
;
1786 if (!do_lxcapi_is_running(c
))
1788 pid
= do_lxcapi_init_pid(c
);
1791 if (c
->lxc_conf
&& c
->lxc_conf
->rebootsignal
)
1792 rebootsignal
= c
->lxc_conf
->rebootsignal
;
1793 if (kill(pid
, rebootsignal
) < 0) {
1794 WARN("Could not send signal %d to pid %d.", rebootsignal
, pid
);
1801 WRAP_API(bool, lxcapi_reboot
)
1803 static bool do_lxcapi_reboot2(struct lxc_container
*c
, int timeout
)
1807 int rebootsignal
= SIGINT
, state_client_fd
= -1;
1808 lxc_state_t states
[MAX_STATE
] = {0};
1813 if (!do_lxcapi_is_running(c
))
1816 pid
= do_lxcapi_init_pid(c
);
1820 if (c
->lxc_conf
&& c
->lxc_conf
->rebootsignal
)
1821 rebootsignal
= c
->lxc_conf
->rebootsignal
;
1823 /* Add a new state client before sending the shutdown signal so that we
1824 * don't miss a state.
1827 states
[RUNNING
] = 2;
1828 ret
= lxc_cmd_add_state_client(c
->name
, c
->config_path
, states
,
1833 if (state_client_fd
< 0)
1839 if (ret
< MAX_STATE
)
1843 /* Send reboot signal to container. */
1844 killret
= kill(pid
, rebootsignal
);
1846 WARN("Could not send signal %d to pid %d", rebootsignal
, pid
);
1847 if (state_client_fd
>= 0)
1848 close(state_client_fd
);
1851 TRACE("Sent signal %d to pid %d", rebootsignal
, pid
);
1856 ret
= lxc_cmd_sock_rcv_state(state_client_fd
, timeout
);
1857 close(state_client_fd
);
1861 TRACE("Received state \"%s\"", lxc_state2str(ret
));
1868 WRAP_API_1(bool, lxcapi_reboot2
, int)
1870 static bool do_lxcapi_shutdown(struct lxc_container
*c
, int timeout
)
1874 int haltsignal
= SIGPWR
, state_client_fd
= -1;
1875 lxc_state_t states
[MAX_STATE
] = {0};
1880 if (!do_lxcapi_is_running(c
))
1883 pid
= do_lxcapi_init_pid(c
);
1887 /* Detect whether we should send SIGRTMIN + 3 (e.g. systemd). */
1888 if (task_blocking_signal(pid
, (SIGRTMIN
+ 3)))
1889 haltsignal
= (SIGRTMIN
+ 3);
1891 if (c
->lxc_conf
&& c
->lxc_conf
->haltsignal
)
1892 haltsignal
= c
->lxc_conf
->haltsignal
;
1894 /* Add a new state client before sending the shutdown signal so that we
1895 * don't miss a state.
1898 states
[STOPPED
] = 1;
1899 ret
= lxc_cmd_add_state_client(c
->name
, c
->config_path
, states
,
1904 if (state_client_fd
< 0)
1910 if (ret
< MAX_STATE
)
1914 /* Send shutdown signal to container. */
1915 killret
= kill(pid
, haltsignal
);
1917 WARN("Could not send signal %d to pid %d", haltsignal
, pid
);
1918 if (state_client_fd
>= 0)
1919 close(state_client_fd
);
1922 TRACE("Sent signal %d to pid %d", haltsignal
, pid
);
1927 ret
= lxc_cmd_sock_rcv_state(state_client_fd
, timeout
);
1928 close(state_client_fd
);
1932 TRACE("Received state \"%s\"", lxc_state2str(ret
));
1939 WRAP_API_1(bool, lxcapi_shutdown
, int)
1941 static bool lxcapi_createl(struct lxc_container
*c
, const char *t
,
1942 const char *bdevtype
, struct bdev_specs
*specs
, int flags
, ...)
1951 current_config
= c
->lxc_conf
;
1954 * since we're going to wait for create to finish, I don't think we
1955 * need to get a copy of the arguments.
1957 va_start(ap
, flags
);
1958 args
= lxc_va_arg_list_to_argv(ap
, 0, 0);
1961 ERROR("Failed to allocate memory");
1965 bret
= do_lxcapi_create(c
, t
, bdevtype
, specs
, flags
, args
);
1969 current_config
= NULL
;
1973 static void do_clear_unexp_config_line(struct lxc_conf
*conf
, const char *key
)
1975 if (!strcmp(key
, "lxc.cgroup"))
1976 return clear_unexp_config_line(conf
, key
, true);
1978 if (!strcmp(key
, "lxc.network"))
1979 return clear_unexp_config_line(conf
, key
, true);
1981 if (!strcmp(key
, "lxc.net"))
1982 return clear_unexp_config_line(conf
, key
, true);
1984 /* Clear a network with a specific index. */
1985 if (!strncmp(key
, "lxc.net.", 8)) {
1990 ret
= lxc_safe_uint(idx
, &(unsigned int){0});
1992 return clear_unexp_config_line(conf
, key
, true);
1995 if (!strcmp(key
, "lxc.hook"))
1996 return clear_unexp_config_line(conf
, key
, true);
1998 return clear_unexp_config_line(conf
, key
, false);
2001 static bool do_lxcapi_clear_config_item(struct lxc_container
*c
,
2005 struct lxc_config_t
*config
;
2007 if (!c
|| !c
->lxc_conf
)
2010 if (container_mem_lock(c
))
2013 config
= lxc_get_config(key
);
2014 /* Verify that the config key exists and that it has a callback
2017 if (config
&& config
->clr
)
2018 ret
= config
->clr(key
, c
->lxc_conf
, NULL
);
2020 do_clear_unexp_config_line(c
->lxc_conf
, key
);
2022 container_mem_unlock(c
);
2026 WRAP_API_1(bool, lxcapi_clear_config_item
, const char *)
2028 static inline bool enter_net_ns(struct lxc_container
*c
)
2030 pid_t pid
= do_lxcapi_init_pid(c
);
2032 if ((geteuid() != 0 || (c
->lxc_conf
&& !lxc_list_empty(&c
->lxc_conf
->id_map
))) && access("/proc/self/ns/user", F_OK
) == 0) {
2033 if (!switch_to_ns(pid
, "user"))
2036 return switch_to_ns(pid
, "net");
2039 /* Used by qsort and bsearch functions for comparing names. */
2040 static inline int string_cmp(char **first
, char **second
)
2042 return strcmp(*first
, *second
);
2045 /* Used by qsort and bsearch functions for comparing container names. */
2046 static inline int container_cmp(struct lxc_container
**first
,
2047 struct lxc_container
**second
)
2049 return strcmp((*first
)->name
, (*second
)->name
);
2052 static bool add_to_array(char ***names
, char *cname
, int pos
)
2054 char **newnames
= realloc(*names
, (pos
+1) * sizeof(char *));
2056 ERROR("Out of memory");
2061 newnames
[pos
] = strdup(cname
);
2065 /* Sort the arrray as we will use binary search on it. */
2066 qsort(newnames
, pos
+ 1, sizeof(char *),
2067 (int (*)(const void *, const void *))string_cmp
);
2072 static bool add_to_clist(struct lxc_container
***list
, struct lxc_container
*c
,
2075 struct lxc_container
**newlist
= realloc(*list
, (pos
+ 1) * sizeof(struct lxc_container
*));
2077 ERROR("Out of memory");
2084 /* Sort the arrray as we will use binary search on it. */
2086 qsort(newlist
, pos
+ 1, sizeof(struct lxc_container
*),
2087 (int (*)(const void *, const void *))container_cmp
);
2092 static char** get_from_array(char ***names
, char *cname
, int size
)
2094 return (char **)bsearch(&cname
, *names
, size
, sizeof(char *), (int (*)(const void *, const void *))string_cmp
);
2098 static bool array_contains(char ***names
, char *cname
, int size
) {
2099 if(get_from_array(names
, cname
, size
) != NULL
)
2104 static bool remove_from_array(char ***names
, char *cname
, int size
)
2106 char **result
= get_from_array(names
, cname
, size
);
2107 if (result
!= NULL
) {
2114 static char ** do_lxcapi_get_interfaces(struct lxc_container
*c
)
2117 int i
, count
= 0, pipefd
[2];
2118 char **interfaces
= NULL
;
2119 char interface
[IFNAMSIZ
];
2121 if(pipe(pipefd
) < 0) {
2122 SYSERROR("pipe failed");
2128 SYSERROR("failed to fork task to get interfaces information");
2134 if (pid
== 0) { /* child */
2135 int ret
= 1, nbytes
;
2136 struct ifaddrs
*interfaceArray
= NULL
, *tempIfAddr
= NULL
;
2138 /* close the read-end of the pipe */
2141 if (!enter_net_ns(c
)) {
2142 SYSERROR("failed to enter namespace");
2146 /* Grab the list of interfaces */
2147 if (getifaddrs(&interfaceArray
)) {
2148 SYSERROR("failed to get interfaces list");
2152 /* Iterate through the interfaces */
2153 for (tempIfAddr
= interfaceArray
; tempIfAddr
!= NULL
; tempIfAddr
= tempIfAddr
->ifa_next
) {
2154 nbytes
= write(pipefd
[1], tempIfAddr
->ifa_name
, IFNAMSIZ
);
2156 ERROR("write failed");
2165 freeifaddrs(interfaceArray
);
2167 /* close the write-end of the pipe, thus sending EOF to the reader */
2172 /* close the write-end of the pipe */
2175 while (read(pipefd
[0], &interface
, IFNAMSIZ
) == IFNAMSIZ
) {
2176 interface
[IFNAMSIZ
- 1] = '\0';
2178 if (array_contains(&interfaces
, interface
, count
))
2181 if(!add_to_array(&interfaces
, interface
, count
))
2182 ERROR("Failed to add \"%s\" to array", interface
);
2187 if (wait_for_pid(pid
) != 0) {
2188 for(i
=0;i
<count
;i
++)
2189 free(interfaces
[i
]);
2194 /* close the read-end of the pipe */
2197 /* Append NULL to the array */
2199 interfaces
= (char **)lxc_append_null_to_array((void **)interfaces
, count
);
2204 WRAP_API(char **, lxcapi_get_interfaces
)
2206 static char** do_lxcapi_get_ips(struct lxc_container
*c
, const char* interface
, const char* family
, int scope
)
2209 int i
, count
= 0, pipefd
[2];
2210 char **addresses
= NULL
;
2211 char address
[INET6_ADDRSTRLEN
];
2213 if(pipe(pipefd
) < 0) {
2214 SYSERROR("pipe failed");
2220 SYSERROR("failed to fork task to get container ips");
2226 if (pid
== 0) { /* child */
2227 int ret
= 1, nbytes
;
2228 struct ifaddrs
*interfaceArray
= NULL
, *tempIfAddr
= NULL
;
2229 char addressOutputBuffer
[INET6_ADDRSTRLEN
];
2230 void *tempAddrPtr
= NULL
;
2231 char *address
= NULL
;
2233 /* close the read-end of the pipe */
2236 if (!enter_net_ns(c
)) {
2237 SYSERROR("failed to enter namespace");
2241 /* Grab the list of interfaces */
2242 if (getifaddrs(&interfaceArray
)) {
2243 SYSERROR("failed to get interfaces list");
2247 /* Iterate through the interfaces */
2248 for (tempIfAddr
= interfaceArray
; tempIfAddr
!= NULL
; tempIfAddr
= tempIfAddr
->ifa_next
) {
2249 if (tempIfAddr
->ifa_addr
== NULL
)
2252 if(tempIfAddr
->ifa_addr
->sa_family
== AF_INET
) {
2253 if (family
&& strcmp(family
, "inet"))
2255 tempAddrPtr
= &((struct sockaddr_in
*)tempIfAddr
->ifa_addr
)->sin_addr
;
2258 if (family
&& strcmp(family
, "inet6"))
2261 if (((struct sockaddr_in6
*)tempIfAddr
->ifa_addr
)->sin6_scope_id
!= scope
)
2264 tempAddrPtr
= &((struct sockaddr_in6
*)tempIfAddr
->ifa_addr
)->sin6_addr
;
2267 if (interface
&& strcmp(interface
, tempIfAddr
->ifa_name
))
2269 else if (!interface
&& strcmp("lo", tempIfAddr
->ifa_name
) == 0)
2272 address
= (char *)inet_ntop(tempIfAddr
->ifa_addr
->sa_family
,
2274 addressOutputBuffer
,
2275 sizeof(addressOutputBuffer
));
2279 nbytes
= write(pipefd
[1], address
, INET6_ADDRSTRLEN
);
2281 ERROR("write failed");
2290 freeifaddrs(interfaceArray
);
2292 /* close the write-end of the pipe, thus sending EOF to the reader */
2297 /* close the write-end of the pipe */
2300 while (read(pipefd
[0], &address
, INET6_ADDRSTRLEN
) == INET6_ADDRSTRLEN
) {
2301 if(!add_to_array(&addresses
, address
, count
))
2302 ERROR("PARENT: add_to_array failed");
2306 if (wait_for_pid(pid
) != 0) {
2307 for(i
=0;i
<count
;i
++)
2313 /* close the read-end of the pipe */
2316 /* Append NULL to the array */
2318 addresses
= (char **)lxc_append_null_to_array((void **)addresses
, count
);
2323 WRAP_API_3(char **, lxcapi_get_ips
, const char *, const char *, int)
2325 static int do_lxcapi_get_config_item(struct lxc_container
*c
, const char *key
, char *retv
, int inlen
)
2328 struct lxc_config_t
*config
;
2330 if (!c
|| !c
->lxc_conf
)
2333 if (container_mem_lock(c
))
2336 config
= lxc_get_config(key
);
2337 /* Verify that the config key exists and that it has a callback
2340 if (config
&& config
->get
)
2341 ret
= config
->get(key
, retv
, inlen
, c
->lxc_conf
, NULL
);
2343 container_mem_unlock(c
);
2347 WRAP_API_3(int, lxcapi_get_config_item
, const char *, char *, int)
2349 static char* do_lxcapi_get_running_config_item(struct lxc_container
*c
, const char *key
)
2353 if (!c
|| !c
->lxc_conf
)
2355 if (container_mem_lock(c
))
2357 ret
= lxc_cmd_get_config_item(c
->name
, key
, do_lxcapi_get_config_path(c
));
2358 container_mem_unlock(c
);
2362 WRAP_API_1(char *, lxcapi_get_running_config_item
, const char *)
2364 static int do_lxcapi_get_keys(struct lxc_container
*c
, const char *key
, char *retv
, int inlen
)
2368 /* List all config items. */
2370 return lxc_list_config_items(retv
, inlen
);
2372 if (!c
|| !c
->lxc_conf
)
2375 if (container_mem_lock(c
))
2378 /* Support 'lxc.net.<idx>', i.e. 'lxc.net.0'
2379 * This is an intelligent result to show which keys are valid given the
2380 * type of nic it is.
2382 if (strncmp(key
, "lxc.net.", 8) == 0)
2383 ret
= lxc_list_net(c
->lxc_conf
, key
, retv
, inlen
);
2385 ret
= lxc_list_subkeys(c
->lxc_conf
, key
, retv
, inlen
);
2387 container_mem_unlock(c
);
2391 WRAP_API_3(int, lxcapi_get_keys
, const char *, char *, int)
2393 static bool do_lxcapi_save_config(struct lxc_container
*c
, const char *alt_file
)
2396 bool ret
= false, need_disklock
= false;
2400 alt_file
= c
->configfile
;
2404 /* If we haven't yet loaded a config, load the stock config. */
2406 if (!do_lxcapi_load_config(c
, lxc_global_config_value("lxc.default_config"))) {
2407 ERROR("Error loading default configuration file %s while saving %s", lxc_global_config_value("lxc.default_config"), c
->name
);
2412 if (!create_container_dir(c
))
2415 /* If we're writing to the container's config file, take the disk lock.
2416 * Otherwise just take the memlock to protect the struct lxc_container
2417 * while we're traversing it.
2419 if (strcmp(c
->configfile
, alt_file
) == 0)
2420 need_disklock
= true;
2423 lret
= container_disk_lock(c
);
2425 lret
= container_mem_lock(c
);
2430 fout
= fopen(alt_file
, "w");
2433 write_config(fout
, c
->lxc_conf
);
2439 container_disk_unlock(c
);
2441 container_mem_unlock(c
);
2445 WRAP_API_1(bool, lxcapi_save_config
, const char *)
2448 static bool mod_rdep(struct lxc_container
*c0
, struct lxc_container
*c
, bool inc
)
2454 char path
[MAXPATHLEN
];
2455 char newpath
[MAXPATHLEN
];
2456 int fd
, ret
, n
= 0, v
= 0;
2458 size_t len
= 0, bytes
= 0;
2460 if (container_disk_lock(c0
))
2463 ret
= snprintf(path
, MAXPATHLEN
, "%s/%s/lxc_snapshots", c0
->config_path
, c0
->name
);
2464 if (ret
< 0 || ret
> MAXPATHLEN
)
2466 ret
= snprintf(newpath
, MAXPATHLEN
, "%s\n%s\n", c
->config_path
, c
->name
);
2467 if (ret
< 0 || ret
> MAXPATHLEN
)
2470 /* If we find an lxc-snapshot file using the old format only listing the
2471 * number of snapshots we will keep using it. */
2472 f1
= fopen(path
, "r");
2474 n
= fscanf(f1
, "%d", &v
);
2476 if (n
== 1 && v
== 0) {
2483 f1
= fopen(path
, "w");
2486 if (fprintf(f1
, "%d\n", v
) < 0) {
2487 ERROR("Error writing new snapshots value");
2493 SYSERROR("Error writing to or closing snapshots file");
2497 /* Here we know that we have or can use an lxc-snapshot file
2498 * using the new format. */
2500 f1
= fopen(path
, "a");
2504 if (fprintf(f1
, "%s", newpath
) < 0) {
2505 ERROR("Error writing new snapshots entry");
2508 SYSERROR("Error writing to or closing snapshots file");
2514 SYSERROR("Error writing to or closing snapshots file");
2518 if ((fd
= open(path
, O_RDWR
| O_CLOEXEC
)) < 0)
2521 if (fstat(fd
, &fbuf
) < 0) {
2526 if (fbuf
.st_size
!= 0) {
2528 buf
= lxc_strmmap(NULL
, fbuf
.st_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
2529 if (buf
== MAP_FAILED
) {
2530 SYSERROR("Failed to create mapping %s", path
);
2535 len
= strlen(newpath
);
2536 while ((del
= strstr((char *)buf
, newpath
))) {
2537 memmove(del
, del
+ len
, strlen(del
) - len
+ 1);
2541 lxc_strmunmap(buf
, fbuf
.st_size
);
2542 if (ftruncate(fd
, fbuf
.st_size
- bytes
) < 0) {
2543 SYSERROR("Failed to truncate file %s", path
);
2551 /* If the lxc-snapshot file is empty, remove it. */
2552 if (stat(path
, &fbuf
) < 0)
2554 if (!fbuf
.st_size
) {
2562 container_disk_unlock(c0
);
2566 static void strip_newline(char *p
)
2568 size_t len
= strlen(p
);
2571 if (p
[len
-1] == '\n')
2575 void mod_all_rdeps(struct lxc_container
*c
, bool inc
)
2577 struct lxc_container
*p
;
2578 char *lxcpath
= NULL
, *lxcname
= NULL
, path
[MAXPATHLEN
];
2579 size_t pathlen
= 0, namelen
= 0;
2583 ret
= snprintf(path
, MAXPATHLEN
, "%s/%s/lxc_rdepends",
2584 c
->config_path
, c
->name
);
2585 if (ret
< 0 || ret
>= MAXPATHLEN
) {
2586 ERROR("Path name too long");
2589 f
= fopen(path
, "r");
2592 while (getline(&lxcpath
, &pathlen
, f
) != -1) {
2593 if (getline(&lxcname
, &namelen
, f
) == -1) {
2594 ERROR("badly formatted file %s", path
);
2597 strip_newline(lxcpath
);
2598 strip_newline(lxcname
);
2599 if ((p
= lxc_container_new(lxcname
, lxcpath
)) == NULL
) {
2600 ERROR("Unable to find dependent container %s:%s",
2604 if (!mod_rdep(p
, c
, inc
))
2605 ERROR("Failed to update snapshots file for %s:%s",
2607 lxc_container_put(p
);
2615 static bool has_fs_snapshots(struct lxc_container
*c
)
2618 char path
[MAXPATHLEN
];
2623 ret
= snprintf(path
, MAXPATHLEN
, "%s/%s/lxc_snapshots", c
->config_path
,
2625 if (ret
< 0 || ret
> MAXPATHLEN
)
2627 /* If the file doesn't exist there are no snapshots. */
2628 if (stat(path
, &fbuf
) < 0)
2632 f
= fopen(path
, "r");
2635 ret
= fscanf(f
, "%d", &v
);
2637 /* TODO: Figure out what to do with the return value of fscanf. */
2639 INFO("Container uses new lxc-snapshots format %s", path
);
2647 static bool has_snapshots(struct lxc_container
*c
)
2649 char path
[MAXPATHLEN
];
2650 struct dirent
*direntp
;
2654 if (!get_snappath_dir(c
, path
))
2656 dir
= opendir(path
);
2659 while ((direntp
= readdir(dir
))) {
2663 if (!strcmp(direntp
->d_name
, "."))
2666 if (!strcmp(direntp
->d_name
, ".."))
2675 static bool do_destroy_container(struct lxc_conf
*conf
) {
2678 if (am_guest_unpriv()) {
2679 ret
= userns_exec_full(conf
, storage_destroy_wrapper
, conf
,
2680 "storage_destroy_wrapper");
2687 return storage_destroy(conf
);
2690 static int lxc_rmdir_onedev_wrapper(void *data
)
2692 char *arg
= (char *) data
;
2693 return lxc_rmdir_onedev(arg
, "snaps");
2696 static int lxc_unlink_exec_wrapper(void *data
)
2702 static bool container_destroy(struct lxc_container
*c
,
2703 struct lxc_storage
*storage
)
2707 struct lxc_conf
*conf
;
2712 if (!c
|| !do_lxcapi_is_defined(c
))
2716 if (container_disk_lock(c
))
2719 if (!is_stopped(c
)) {
2720 /* We should queue some sort of error - in c->error_string? */
2721 ERROR("container %s is not stopped", c
->name
);
2725 if (conf
&& !lxc_list_empty(&conf
->hooks
[LXCHOOK_DESTROY
])) {
2726 /* Start of environment variable setup for hooks */
2727 if (setenv("LXC_NAME", c
->name
, 1))
2728 SYSERROR("Failed to set environment variable for container name");
2730 if (conf
->rcfile
&& setenv("LXC_CONFIG_FILE", conf
->rcfile
, 1))
2731 SYSERROR("Failed to set environment variable for config path");
2733 if (conf
->rootfs
.mount
&& setenv("LXC_ROOTFS_MOUNT", conf
->rootfs
.mount
, 1))
2734 SYSERROR("Failed to set environment variable for rootfs mount");
2736 if (conf
->rootfs
.path
&& setenv("LXC_ROOTFS_PATH", conf
->rootfs
.path
, 1))
2737 SYSERROR("Failed to set environment variable for rootfs mount");
2739 if (conf
->console
.path
&& setenv("LXC_CONSOLE", conf
->console
.path
, 1))
2740 SYSERROR("Failed to set environment variable for console path");
2742 if (conf
->console
.log_path
&& setenv("LXC_CONSOLE_LOGPATH", conf
->console
.log_path
, 1))
2743 SYSERROR("Failed to set environment variable for console log");
2744 /* End of environment variable setup for hooks */
2746 if (run_lxc_hooks(c
->name
, "destroy", conf
, NULL
)) {
2747 ERROR("Failed to execute clone hook for \"%s\"", c
->name
);
2752 if (current_config
&& conf
== current_config
) {
2753 current_config
= NULL
;
2754 if (conf
->logfd
!= -1) {
2760 if (conf
&& conf
->rootfs
.path
&& conf
->rootfs
.mount
) {
2761 if (!do_destroy_container(conf
)) {
2762 ERROR("Error destroying rootfs for %s", c
->name
);
2765 INFO("Destroyed rootfs for %s", c
->name
);
2768 mod_all_rdeps(c
, false);
2770 p1
= do_lxcapi_get_config_path(c
);
2779 * strlen("config") = 6
2783 len
= strlen(p1
) + 1 + strlen(c
->name
) + 1 + 6 + 1;
2786 ERROR("Failed to allocate memory");
2790 /* For an overlay container the rootfs is considered immutable and
2791 * cannot be removed when restoring from a snapshot.
2793 if (storage
&& (!strcmp(storage
->type
, "overlay") ||
2794 !strcmp(storage
->type
, "overlayfs")) &&
2795 (storage
->flags
& LXC_STORAGE_INTERNAL_OVERLAY_RESTORE
)) {
2796 ret
= snprintf(path
, len
, "%s/%s/config", p1
, c
->name
);
2797 if (ret
< 0 || (size_t)ret
>= len
)
2800 if (am_guest_unpriv())
2801 ret
= userns_exec_1(conf
, lxc_unlink_exec_wrapper
, path
,
2802 "lxc_unlink_exec_wrapper");
2806 SYSERROR("Failed to destroy config file \"%s\" for \"%s\"",
2810 INFO("Destroyed config file \"%s\" for \"%s\"", path
, c
->name
);
2816 ret
= snprintf(path
, len
, "%s/%s", p1
, c
->name
);
2817 if (ret
< 0 || (size_t)ret
>= len
)
2819 if (am_guest_unpriv())
2820 ret
= userns_exec_full(conf
, lxc_rmdir_onedev_wrapper
, path
,
2821 "lxc_rmdir_onedev_wrapper");
2823 ret
= lxc_rmdir_onedev(path
, "snaps");
2825 ERROR("Failed to destroy directory \"%s\" for \"%s\"", path
,
2829 INFO("Destroyed directory \"%s\" for \"%s\"", path
, c
->name
);
2836 container_disk_unlock(c
);
2840 static bool do_lxcapi_destroy(struct lxc_container
*c
)
2842 if (!c
|| !lxcapi_is_defined(c
))
2844 if (has_snapshots(c
)) {
2845 ERROR("Container %s has snapshots; not removing", c
->name
);
2849 if (has_fs_snapshots(c
)) {
2850 ERROR("container %s has snapshots on its rootfs", c
->name
);
2854 return container_destroy(c
, NULL
);
2857 WRAP_API(bool, lxcapi_destroy
)
2859 static bool do_lxcapi_destroy_with_snapshots(struct lxc_container
*c
)
2861 if (!c
|| !lxcapi_is_defined(c
))
2863 if (!lxcapi_snapshot_destroy_all(c
)) {
2864 ERROR("Error deleting all snapshots");
2867 return lxcapi_destroy(c
);
2870 WRAP_API(bool, lxcapi_destroy_with_snapshots
)
2872 int lxc_set_config_item_locked(struct lxc_conf
*conf
, const char *key
,
2876 struct lxc_config_t
*config
;
2879 config
= lxc_get_config(key
);
2883 ret
= config
->set(key
, v
, conf
, NULL
);
2887 if (lxc_config_value_empty(v
))
2888 do_clear_unexp_config_line(conf
, key
);
2890 bret
= do_append_unexp_config_line(conf
, key
, v
);
2897 static bool do_set_config_item_locked(struct lxc_container
*c
, const char *key
,
2903 c
->lxc_conf
= lxc_conf_init();
2908 ret
= lxc_set_config_item_locked(c
->lxc_conf
, key
, v
);
2915 static bool do_lxcapi_set_config_item(struct lxc_container
*c
, const char *key
, const char *v
)
2922 if (container_mem_lock(c
))
2925 b
= do_set_config_item_locked(c
, key
, v
);
2927 container_mem_unlock(c
);
2931 WRAP_API_2(bool, lxcapi_set_config_item
, const char *, const char *)
2933 static char *lxcapi_config_file_name(struct lxc_container
*c
)
2935 if (!c
|| !c
->configfile
)
2937 return strdup(c
->configfile
);
2940 static const char *lxcapi_get_config_path(struct lxc_container
*c
)
2942 if (!c
|| !c
->config_path
)
2944 return (const char *)(c
->config_path
);
2949 * Just recalculate the c->configfile based on the
2950 * c->config_path, which must be set.
2951 * The lxc_container must be locked or not yet public.
2953 static bool set_config_filename(struct lxc_container
*c
)
2958 if (!c
->config_path
)
2961 /* $lxc_path + "/" + c->name + "/" + "config" + '\0' */
2962 len
= strlen(c
->config_path
) + strlen(c
->name
) + strlen("config") + 3;
2963 newpath
= malloc(len
);
2967 ret
= snprintf(newpath
, len
, "%s/%s/config", c
->config_path
, c
->name
);
2968 if (ret
< 0 || ret
>= len
) {
2969 fprintf(stderr
, "Error printing out config file name\n");
2974 free(c
->configfile
);
2975 c
->configfile
= newpath
;
2980 static bool do_lxcapi_set_config_path(struct lxc_container
*c
, const char *path
)
2984 char *oldpath
= NULL
;
2989 if (container_mem_lock(c
))
2994 ERROR("Out of memory setting new lxc path");
3000 oldpath
= c
->config_path
;
3003 /* Since we've changed the config path, we have to change the
3004 * config file name too */
3005 if (!set_config_filename(c
)) {
3006 ERROR("Out of memory setting new config filename");
3008 free(c
->config_path
);
3009 c
->config_path
= oldpath
;
3014 container_mem_unlock(c
);
3018 WRAP_API_1(bool, lxcapi_set_config_path
, const char *)
3020 static bool do_lxcapi_set_cgroup_item(struct lxc_container
*c
, const char *subsys
, const char *value
)
3030 if (container_disk_lock(c
))
3033 ret
= lxc_cgroup_set(subsys
, value
, c
->name
, c
->config_path
);
3035 container_disk_unlock(c
);
3039 WRAP_API_2(bool, lxcapi_set_cgroup_item
, const char *, const char *)
3041 static int do_lxcapi_get_cgroup_item(struct lxc_container
*c
, const char *subsys
, char *retv
, int inlen
)
3051 if (container_disk_lock(c
))
3054 ret
= lxc_cgroup_get(subsys
, retv
, inlen
, c
->name
, c
->config_path
);
3056 container_disk_unlock(c
);
3060 WRAP_API_3(int, lxcapi_get_cgroup_item
, const char *, char *, int)
3062 const char *lxc_get_global_config_item(const char *key
)
3064 return lxc_global_config_value(key
);
3067 const char *lxc_get_version(void)
3072 static int copy_file(const char *old
, const char *new)
3079 if (file_exists(new)) {
3080 ERROR("copy destination %s exists", new);
3083 ret
= stat(old
, &sbuf
);
3085 INFO("Error stat'ing %s", old
);
3089 in
= open(old
, O_RDONLY
);
3091 SYSERROR("Error opening original file %s", old
);
3094 out
= open(new, O_CREAT
| O_EXCL
| O_WRONLY
, 0644);
3096 SYSERROR("Error opening new file %s", new);
3102 len
= read(in
, buf
, 8096);
3104 SYSERROR("Error reading old file %s", old
);
3109 ret
= write(out
, buf
, len
);
3110 if (ret
< len
) { /* should we retry? */
3111 SYSERROR("Error: write to new file %s was interrupted", new);
3118 /* We set mode, but not owner/group. */
3119 ret
= chmod(new, sbuf
.st_mode
);
3121 SYSERROR("Error setting mode on %s", new);
3133 static int copyhooks(struct lxc_container
*oldc
, struct lxc_container
*c
)
3136 struct lxc_list
*it
;
3139 len
= strlen(oldc
->config_path
) + strlen(oldc
->name
) + 3;
3140 cpath
= alloca(len
);
3141 ret
= snprintf(cpath
, len
, "%s/%s/", oldc
->config_path
, oldc
->name
);
3142 if (ret
< 0 || ret
>= len
)
3145 for (i
=0; i
<NUM_LXC_HOOKS
; i
++) {
3146 lxc_list_for_each(it
, &c
->lxc_conf
->hooks
[i
]) {
3147 char *hookname
= it
->elem
;
3148 char *fname
= strrchr(hookname
, '/');
3149 char tmppath
[MAXPATHLEN
];
3150 if (!fname
) /* relative path - we don't support, but maybe we should */
3152 if (strncmp(hookname
, cpath
, len
- 1) != 0) {
3153 /* this hook is public - ignore */
3156 /* copy the script, and change the entry in confile */
3157 ret
= snprintf(tmppath
, MAXPATHLEN
, "%s/%s/%s",
3158 c
->config_path
, c
->name
, fname
+1);
3159 if (ret
< 0 || ret
>= MAXPATHLEN
)
3161 ret
= copy_file(it
->elem
, tmppath
);
3165 it
->elem
= strdup(tmppath
);
3167 ERROR("out of memory copying hook path");
3173 if (!clone_update_unexp_hooks(c
->lxc_conf
, oldc
->config_path
,
3174 c
->config_path
, oldc
->name
, c
->name
)) {
3175 ERROR("Error saving new hooks in clone");
3178 do_lxcapi_save_config(c
, NULL
);
3183 static int copy_fstab(struct lxc_container
*oldc
, struct lxc_container
*c
)
3185 char newpath
[MAXPATHLEN
];
3186 char *oldpath
= oldc
->lxc_conf
->fstab
;
3192 clear_unexp_config_line(c
->lxc_conf
, "lxc.mount.fstab", false);
3194 char *p
= strrchr(oldpath
, '/');
3197 ret
= snprintf(newpath
, MAXPATHLEN
, "%s/%s%s",
3198 c
->config_path
, c
->name
, p
);
3199 if (ret
< 0 || ret
>= MAXPATHLEN
) {
3200 ERROR("error printing new path for %s", oldpath
);
3203 if (file_exists(newpath
)) {
3204 ERROR("error: fstab file %s exists", newpath
);
3208 if (copy_file(oldpath
, newpath
) < 0) {
3209 ERROR("error: copying %s to %s", oldpath
, newpath
);
3212 free(c
->lxc_conf
->fstab
);
3213 c
->lxc_conf
->fstab
= strdup(newpath
);
3214 if (!c
->lxc_conf
->fstab
) {
3215 ERROR("error: allocating pathname");
3218 if (!do_append_unexp_config_line(c
->lxc_conf
, "lxc.mount.fstab", newpath
)) {
3219 ERROR("error saving new lxctab");
3226 static void copy_rdepends(struct lxc_container
*c
, struct lxc_container
*c0
)
3228 char path0
[MAXPATHLEN
], path1
[MAXPATHLEN
];
3231 ret
= snprintf(path0
, MAXPATHLEN
, "%s/%s/lxc_rdepends", c0
->config_path
,
3233 if (ret
< 0 || ret
>= MAXPATHLEN
) {
3234 WARN("Error copying reverse dependencies");
3237 ret
= snprintf(path1
, MAXPATHLEN
, "%s/%s/lxc_rdepends", c
->config_path
,
3239 if (ret
< 0 || ret
>= MAXPATHLEN
) {
3240 WARN("Error copying reverse dependencies");
3243 if (copy_file(path0
, path1
) < 0) {
3244 INFO("Error copying reverse dependencies");
3249 static bool add_rdepends(struct lxc_container
*c
, struct lxc_container
*c0
)
3252 char path
[MAXPATHLEN
];
3256 ret
= snprintf(path
, MAXPATHLEN
, "%s/%s/lxc_rdepends", c
->config_path
,
3258 if (ret
< 0 || ret
>= MAXPATHLEN
)
3260 f
= fopen(path
, "a");
3264 /* If anything goes wrong, just return an error. */
3265 if (fprintf(f
, "%s\n%s\n", c0
->config_path
, c0
->name
) < 0)
3273 * If the fs natively supports snapshot clones with no penalty,
3274 * then default to those even if not requested.
3275 * Currently we only do this for btrfs.
3277 bool should_default_to_snapshot(struct lxc_container
*c0
,
3278 struct lxc_container
*c1
)
3280 size_t l0
= strlen(c0
->config_path
) + strlen(c0
->name
) + 2;
3281 size_t l1
= strlen(c1
->config_path
) + strlen(c1
->name
) + 2;
3282 char *p0
= alloca(l0
+ 1);
3283 char *p1
= alloca(l1
+ 1);
3284 char *rootfs
= c0
->lxc_conf
->rootfs
.path
;
3286 snprintf(p0
, l0
, "%s/%s", c0
->config_path
, c0
->name
);
3287 snprintf(p1
, l1
, "%s/%s", c1
->config_path
, c1
->name
);
3289 if (!is_btrfs_fs(p0
) || !is_btrfs_fs(p1
))
3292 if (is_btrfs_subvol(rootfs
) <= 0)
3295 return btrfs_same_fs(p0
, p1
) == 0;
3298 static int copy_storage(struct lxc_container
*c0
, struct lxc_container
*c
,
3299 const char *newtype
, int flags
, const char *bdevdata
,
3302 struct lxc_storage
*bdev
;
3305 if (should_default_to_snapshot(c0
, c
))
3306 flags
|= LXC_CLONE_SNAPSHOT
;
3308 bdev
= storage_copy(c0
, c
->name
, c
->config_path
, newtype
, flags
,
3309 bdevdata
, newsize
, &need_rdep
);
3311 ERROR("Error copying storage.");
3315 /* Set new rootfs. */
3316 free(c
->lxc_conf
->rootfs
.path
);
3317 c
->lxc_conf
->rootfs
.path
= strdup(bdev
->src
);
3320 if (!c
->lxc_conf
->rootfs
.path
) {
3321 ERROR("Out of memory while setting storage path.");
3325 /* Append a new lxc.rootfs.path entry to the unexpanded config. */
3326 clear_unexp_config_line(c
->lxc_conf
, "lxc.rootfs.path", false);
3327 if (!do_append_unexp_config_line(c
->lxc_conf
, "lxc.rootfs.path",
3328 c
->lxc_conf
->rootfs
.path
)) {
3329 ERROR("Error saving new rootfs to cloned config.");
3333 if (flags
& LXC_CLONE_SNAPSHOT
)
3334 copy_rdepends(c
, c0
);
3336 if (!add_rdepends(c
, c0
))
3337 WARN("Error adding reverse dependency from %s to %s",
3341 mod_all_rdeps(c
, true);
3346 struct clone_update_data
{
3347 struct lxc_container
*c0
;
3348 struct lxc_container
*c1
;
3353 static int clone_update_rootfs(struct clone_update_data
*data
)
3355 struct lxc_container
*c0
= data
->c0
;
3356 struct lxc_container
*c
= data
->c1
;
3357 int flags
= data
->flags
;
3358 char **hookargs
= data
->hookargs
;
3360 char path
[MAXPATHLEN
];
3361 struct lxc_storage
*bdev
;
3363 struct lxc_conf
*conf
= c
->lxc_conf
;
3365 /* update hostname in rootfs */
3366 /* we're going to mount, so run in a clean namespace to simplify cleanup */
3368 if (setgid(0) < 0) {
3369 ERROR("Failed to setgid to 0");
3372 if (setuid(0) < 0) {
3373 ERROR("Failed to setuid to 0");
3376 if (setgroups(0, NULL
) < 0)
3377 WARN("Failed to clear groups");
3379 if (unshare(CLONE_NEWNS
) < 0)
3381 bdev
= storage_init(c
->lxc_conf
);
3384 if (strcmp(bdev
->type
, "dir") != 0) {
3385 if (unshare(CLONE_NEWNS
) < 0) {
3386 ERROR("error unsharing mounts");
3390 if (detect_shared_rootfs()) {
3391 if (mount(NULL
, "/", NULL
, MS_SLAVE
|MS_REC
, NULL
)) {
3392 SYSERROR("Failed to make / rslave");
3393 ERROR("Continuing...");
3396 if (bdev
->ops
->mount(bdev
) < 0) {
3400 } else { /* TODO come up with a better way */
3402 bdev
->dest
= strdup(bdev
->src
);
3405 if (!lxc_list_empty(&conf
->hooks
[LXCHOOK_CLONE
])) {
3406 /* Start of environment variable setup for hooks */
3407 if (c0
->name
&& setenv("LXC_SRC_NAME", c0
->name
, 1)) {
3408 SYSERROR("failed to set environment variable for source container name");
3410 if (setenv("LXC_NAME", c
->name
, 1)) {
3411 SYSERROR("failed to set environment variable for container name");
3413 if (conf
->rcfile
&& setenv("LXC_CONFIG_FILE", conf
->rcfile
, 1)) {
3414 SYSERROR("failed to set environment variable for config path");
3416 if (bdev
->dest
&& setenv("LXC_ROOTFS_MOUNT", bdev
->dest
, 1)) {
3417 SYSERROR("failed to set environment variable for rootfs mount");
3419 if (conf
->rootfs
.path
&& setenv("LXC_ROOTFS_PATH", conf
->rootfs
.path
, 1)) {
3420 SYSERROR("failed to set environment variable for rootfs mount");
3423 if (run_lxc_hooks(c
->name
, "clone", conf
, hookargs
)) {
3424 ERROR("Error executing clone hook for %s", c
->name
);
3430 if (!(flags
& LXC_CLONE_KEEPNAME
)) {
3431 ret
= snprintf(path
, MAXPATHLEN
, "%s/etc/hostname", bdev
->dest
);
3434 if (ret
< 0 || ret
>= MAXPATHLEN
)
3436 if (!file_exists(path
))
3438 if (!(fout
= fopen(path
, "w"))) {
3439 SYSERROR("unable to open %s: ignoring", path
);
3442 if (fprintf(fout
, "%s", c
->name
) < 0) {
3446 if (fclose(fout
) < 0)
3455 static int clone_update_rootfs_wrapper(void *data
)
3457 struct clone_update_data
*arg
= (struct clone_update_data
*) data
;
3458 return clone_update_rootfs(arg
);
3462 * We want to support:
3463 sudo lxc-clone -o o1 -n n1 -s -L|-fssize fssize -v|--vgname vgname \
3464 -p|--lvprefix lvprefix -t|--fstype fstype -B backingstore
3466 -s [ implies overlay]
3470 only rootfs gets converted (copied/snapshotted) on clone.
3473 static int create_file_dirname(char *path
, struct lxc_conf
*conf
)
3475 char *p
= strrchr(path
, '/');
3481 ret
= do_create_container_dir(path
, conf
);
3486 static struct lxc_container
*do_lxcapi_clone(struct lxc_container
*c
, const char *newname
,
3487 const char *lxcpath
, int flags
,
3488 const char *bdevtype
, const char *bdevdata
, uint64_t newsize
,
3491 char newpath
[MAXPATHLEN
];
3493 struct clone_update_data data
;
3494 size_t saved_unexp_len
;
3497 int storage_copied
= 0;
3498 char *origroot
= NULL
, *saved_unexp_conf
= NULL
;
3499 struct lxc_container
*c2
= NULL
;
3501 if (!c
|| !do_lxcapi_is_defined(c
))
3504 if (container_mem_lock(c
))
3507 if (!is_stopped(c
)) {
3508 ERROR("error: Original container (%s) is running", c
->name
);
3512 /* Make sure the container doesn't yet exist. */
3516 lxcpath
= do_lxcapi_get_config_path(c
);
3517 ret
= snprintf(newpath
, MAXPATHLEN
, "%s/%s/config", lxcpath
, newname
);
3518 if (ret
< 0 || ret
>= MAXPATHLEN
) {
3519 SYSERROR("clone: failed making config pathname");
3523 if (file_exists(newpath
)) {
3524 ERROR("error: clone: %s exists", newpath
);
3528 ret
= create_file_dirname(newpath
, c
->lxc_conf
);
3529 if (ret
< 0 && errno
!= EEXIST
) {
3530 ERROR("Error creating container dir for %s", newpath
);
3534 /* Copy the configuration. Tweak it as needed. */
3535 if (c
->lxc_conf
->rootfs
.path
) {
3536 origroot
= c
->lxc_conf
->rootfs
.path
;
3537 c
->lxc_conf
->rootfs
.path
= NULL
;
3539 fout
= fopen(newpath
, "w");
3541 SYSERROR("open %s", newpath
);
3545 saved_unexp_conf
= c
->lxc_conf
->unexpanded_config
;
3546 saved_unexp_len
= c
->lxc_conf
->unexpanded_len
;
3547 c
->lxc_conf
->unexpanded_config
= strdup(saved_unexp_conf
);
3548 if (!c
->lxc_conf
->unexpanded_config
) {
3549 ERROR("Out of memory");
3554 clear_unexp_config_line(c
->lxc_conf
, "lxc.rootfs.path", false);
3555 write_config(fout
, c
->lxc_conf
);
3557 c
->lxc_conf
->rootfs
.path
= origroot
;
3558 free(c
->lxc_conf
->unexpanded_config
);
3559 c
->lxc_conf
->unexpanded_config
= saved_unexp_conf
;
3560 saved_unexp_conf
= NULL
;
3561 c
->lxc_conf
->unexpanded_len
= saved_unexp_len
;
3563 ret
= snprintf(newpath
, MAXPATHLEN
, "%s/%s/rootfs", lxcpath
, newname
);
3564 if (ret
< 0 || ret
>= MAXPATHLEN
) {
3565 SYSERROR("clone: failed making rootfs pathname");
3569 ret
= mkdir(newpath
, 0755);
3571 /* For an overlay container the rootfs is considered immutable
3572 * and will not have been removed when restoring from a
3575 if (errno
!= ENOENT
&&
3576 !(flags
& LXC_STORAGE_INTERNAL_OVERLAY_RESTORE
)) {
3577 SYSERROR("Failed to create directory \"%s\"", newpath
);
3582 if (am_guest_unpriv()) {
3583 if (chown_mapped_root(newpath
, c
->lxc_conf
) < 0) {
3584 ERROR("Error chowning %s to container root", newpath
);
3589 c2
= lxc_container_new(newname
, lxcpath
);
3591 ERROR("clone: failed to create new container (%s %s)", newname
,
3596 /* copy/snapshot rootfs's */
3597 ret
= copy_storage(c
, c2
, bdevtype
, flags
, bdevdata
, newsize
);
3602 /* update utsname */
3603 if (!(flags
& LXC_CLONE_KEEPNAME
)) {
3604 clear_unexp_config_line(c2
->lxc_conf
, "lxc.utsname", false);
3605 clear_unexp_config_line(c2
->lxc_conf
, "lxc.uts.name", false);
3607 if (!do_set_config_item_locked(c2
, "lxc.uts.name", newname
)) {
3608 ERROR("Error setting new hostname");
3614 ret
= copyhooks(c
, c2
);
3616 ERROR("error copying hooks");
3620 if (copy_fstab(c
, c2
) < 0) {
3621 ERROR("error copying fstab");
3625 /* update macaddrs */
3626 if (!(flags
& LXC_CLONE_KEEPMACADDR
)) {
3627 if (!network_new_hwaddrs(c2
->lxc_conf
)) {
3628 ERROR("Error updating mac addresses");
3633 /* Update absolute paths for overlay mount directories. */
3634 if (ovl_update_abs_paths(c2
->lxc_conf
, c
->config_path
, c
->name
, lxcpath
, newname
) < 0)
3637 /* We've now successfully created c2's storage, so clear it out if we
3642 if (!c2
->save_config(c2
, NULL
))
3645 if ((pid
= fork()) < 0) {
3650 ret
= wait_for_pid(pid
);
3653 container_mem_unlock(c
);
3659 data
.hookargs
= hookargs
;
3660 if (am_guest_unpriv())
3661 ret
= userns_exec_full(c
->lxc_conf
, clone_update_rootfs_wrapper
,
3662 &data
, "clone_update_rootfs_wrapper");
3664 ret
= clone_update_rootfs(&data
);
3668 container_mem_unlock(c
);
3672 container_mem_unlock(c
);
3674 if (!storage_copied
)
3675 c2
->lxc_conf
->rootfs
.path
= NULL
;
3677 lxc_container_put(c2
);
3683 static struct lxc_container
*lxcapi_clone(struct lxc_container
*c
, const char *newname
,
3684 const char *lxcpath
, int flags
,
3685 const char *bdevtype
, const char *bdevdata
, uint64_t newsize
,
3688 struct lxc_container
* ret
;
3689 current_config
= c
? c
->lxc_conf
: NULL
;
3690 ret
= do_lxcapi_clone(c
, newname
, lxcpath
, flags
, bdevtype
, bdevdata
, newsize
, hookargs
);
3691 current_config
= NULL
;
3695 static bool do_lxcapi_rename(struct lxc_container
*c
, const char *newname
)
3697 struct lxc_storage
*bdev
;
3698 struct lxc_container
*newc
;
3700 if (!c
|| !c
->name
|| !c
->config_path
|| !c
->lxc_conf
)
3703 if (has_fs_snapshots(c
) || has_snapshots(c
)) {
3704 ERROR("Renaming a container with snapshots is not supported");
3707 bdev
= storage_init(c
->lxc_conf
);
3709 ERROR("Failed to find original backing store type");
3713 newc
= lxcapi_clone(c
, newname
, c
->config_path
, LXC_CLONE_KEEPMACADDR
, NULL
, bdev
->type
, 0, NULL
);
3716 lxc_container_put(newc
);
3720 if (newc
&& lxcapi_is_defined(newc
))
3721 lxc_container_put(newc
);
3723 if (!container_destroy(c
, NULL
)) {
3724 ERROR("Could not destroy existing container %s", c
->name
);
3730 WRAP_API_1(bool, lxcapi_rename
, const char *)
3732 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
)
3739 current_config
= c
->lxc_conf
;
3741 ret
= lxc_attach(c
->name
, c
->config_path
, exec_function
, exec_payload
, options
, attached_process
);
3742 current_config
= NULL
;
3746 static int do_lxcapi_attach_run_wait(struct lxc_container
*c
, lxc_attach_options_t
*options
, const char *program
, const char * const argv
[])
3748 lxc_attach_command_t command
;
3755 command
.program
= (char*)program
;
3756 command
.argv
= (char**)argv
;
3757 r
= lxc_attach(c
->name
, c
->config_path
, lxc_attach_run_command
, &command
, options
, &pid
);
3762 return lxc_wait_for_pid_status(pid
);
3765 static int lxcapi_attach_run_wait(struct lxc_container
*c
, lxc_attach_options_t
*options
, const char *program
, const char * const argv
[])
3768 current_config
= c
? c
->lxc_conf
: NULL
;
3769 ret
= do_lxcapi_attach_run_wait(c
, options
, program
, argv
);
3770 current_config
= NULL
;
3774 static int get_next_index(const char *lxcpath
, char *cname
)
3780 fname
= alloca(strlen(lxcpath
) + 20);
3782 sprintf(fname
, "%s/snap%d", lxcpath
, i
);
3783 ret
= stat(fname
, &sb
);
3790 static bool get_snappath_dir(struct lxc_container
*c
, char *snappath
)
3794 * If the old style snapshot path exists, use it
3795 * /var/lib/lxc -> /var/lib/lxcsnaps
3797 ret
= snprintf(snappath
, MAXPATHLEN
, "%ssnaps", c
->config_path
);
3798 if (ret
< 0 || ret
>= MAXPATHLEN
)
3800 if (dir_exists(snappath
)) {
3801 ret
= snprintf(snappath
, MAXPATHLEN
, "%ssnaps/%s", c
->config_path
, c
->name
);
3802 if (ret
< 0 || ret
>= MAXPATHLEN
)
3808 * Use the new style path
3809 * /var/lib/lxc -> /var/lib/lxc + c->name + /snaps + \0
3811 ret
= snprintf(snappath
, MAXPATHLEN
, "%s/%s/snaps", c
->config_path
, c
->name
);
3812 if (ret
< 0 || ret
>= MAXPATHLEN
)
3817 static int do_lxcapi_snapshot(struct lxc_container
*c
, const char *commentfile
)
3820 struct lxc_container
*c2
;
3821 char snappath
[MAXPATHLEN
], newname
[20];
3823 if (!c
|| !lxcapi_is_defined(c
))
3826 if (!storage_can_backup(c
->lxc_conf
)) {
3827 ERROR("%s's backing store cannot be backed up.", c
->name
);
3828 ERROR("Your container must use another backing store type.");
3832 if (!get_snappath_dir(c
, snappath
))
3835 i
= get_next_index(snappath
, c
->name
);
3837 if (mkdir_p(snappath
, 0755) < 0) {
3838 ERROR("Failed to create snapshot directory %s", snappath
);
3842 ret
= snprintf(newname
, 20, "snap%d", i
);
3843 if (ret
< 0 || ret
>= 20)
3847 * We pass LXC_CLONE_SNAPSHOT to make sure that a rdepends file entry is
3848 * created in the original container
3850 flags
= LXC_CLONE_SNAPSHOT
| LXC_CLONE_KEEPMACADDR
| LXC_CLONE_KEEPNAME
|
3851 LXC_CLONE_KEEPBDEVTYPE
| LXC_CLONE_MAYBE_SNAPSHOT
;
3852 if (storage_is_dir(c
->lxc_conf
)) {
3853 ERROR("Snapshot of directory-backed container requested.");
3854 ERROR("Making a copy-clone. If you do want snapshots, then");
3855 ERROR("please create an aufs or overlay clone first, snapshot that");
3856 ERROR("and keep the original container pristine.");
3857 flags
&= ~LXC_CLONE_SNAPSHOT
| LXC_CLONE_MAYBE_SNAPSHOT
;
3859 c2
= do_lxcapi_clone(c
, newname
, snappath
, flags
, NULL
, NULL
, 0, NULL
);
3861 ERROR("clone of %s:%s failed", c
->config_path
, c
->name
);
3865 lxc_container_put(c2
);
3867 /* Now write down the creation time. */
3874 tm_info
= localtime(&timer
);
3876 strftime(buffer
, 25, "%Y:%m:%d %H:%M:%S", tm_info
);
3878 char *dfnam
= alloca(strlen(snappath
) + strlen(newname
) + 5);
3879 sprintf(dfnam
, "%s/%s/ts", snappath
, newname
);
3880 f
= fopen(dfnam
, "w");
3882 ERROR("Failed to open %s", dfnam
);
3885 if (fprintf(f
, "%s", buffer
) < 0) {
3886 SYSERROR("Writing timestamp");
3892 SYSERROR("Writing timestamp");
3897 /* $p / $name / comment \0 */
3898 int len
= strlen(snappath
) + strlen(newname
) + 10;
3899 char *path
= alloca(len
);
3900 sprintf(path
, "%s/%s/comment", snappath
, newname
);
3901 return copy_file(commentfile
, path
) < 0 ? -1 : i
;
3907 WRAP_API_1(int, lxcapi_snapshot
, const char *)
3909 static void lxcsnap_free(struct lxc_snapshot
*s
)
3912 free(s
->comment_pathname
);
3917 static char *get_snapcomment_path(char* snappath
, char *name
)
3919 /* $snappath/$name/comment */
3920 int ret
, len
= strlen(snappath
) + strlen(name
) + 10;
3921 char *s
= malloc(len
);
3924 ret
= snprintf(s
, len
, "%s/%s/comment", snappath
, name
);
3925 if (ret
< 0 || ret
>= len
) {
3933 static char *get_timestamp(char* snappath
, char *name
)
3935 char path
[MAXPATHLEN
], *s
= NULL
;
3939 ret
= snprintf(path
, MAXPATHLEN
, "%s/%s/ts", snappath
, name
);
3940 if (ret
< 0 || ret
>= MAXPATHLEN
)
3942 fin
= fopen(path
, "r");
3945 (void) fseek(fin
, 0, SEEK_END
);
3947 (void) fseek(fin
, 0, SEEK_SET
);
3952 if (fread(s
, 1, len
, fin
) != len
) {
3953 SYSERROR("reading timestamp");
3963 static int do_lxcapi_snapshot_list(struct lxc_container
*c
, struct lxc_snapshot
**ret_snaps
)
3965 char snappath
[MAXPATHLEN
], path2
[MAXPATHLEN
];
3967 struct dirent
*direntp
;
3968 struct lxc_snapshot
*snaps
=NULL
, *nsnaps
;
3971 if (!c
|| !lxcapi_is_defined(c
))
3974 if (!get_snappath_dir(c
, snappath
)) {
3975 ERROR("path name too long");
3978 dir
= opendir(snappath
);
3980 INFO("failed to open %s - assuming no snapshots", snappath
);
3984 while ((direntp
= readdir(dir
))) {
3988 if (!strcmp(direntp
->d_name
, "."))
3991 if (!strcmp(direntp
->d_name
, ".."))
3994 ret
= snprintf(path2
, MAXPATHLEN
, "%s/%s/config", snappath
, direntp
->d_name
);
3995 if (ret
< 0 || ret
>= MAXPATHLEN
) {
3996 ERROR("pathname too long");
3999 if (!file_exists(path2
))
4001 nsnaps
= realloc(snaps
, (count
+ 1)*sizeof(*snaps
));
4003 SYSERROR("Out of memory");
4007 snaps
[count
].free
= lxcsnap_free
;
4008 snaps
[count
].name
= strdup(direntp
->d_name
);
4009 if (!snaps
[count
].name
)
4011 snaps
[count
].lxcpath
= strdup(snappath
);
4012 if (!snaps
[count
].lxcpath
) {
4013 free(snaps
[count
].name
);
4016 snaps
[count
].comment_pathname
= get_snapcomment_path(snappath
, direntp
->d_name
);
4017 snaps
[count
].timestamp
= get_timestamp(snappath
, direntp
->d_name
);
4022 WARN("failed to close directory");
4030 for (i
=0; i
<count
; i
++)
4031 lxcsnap_free(&snaps
[i
]);
4035 WARN("failed to close directory");
4039 WRAP_API_1(int, lxcapi_snapshot_list
, struct lxc_snapshot
**)
4041 static bool do_lxcapi_snapshot_restore(struct lxc_container
*c
, const char *snapname
, const char *newname
)
4043 char clonelxcpath
[MAXPATHLEN
];
4045 struct lxc_container
*snap
, *rest
;
4046 struct lxc_storage
*bdev
;
4049 if (!c
|| !c
->name
|| !c
->config_path
)
4052 if (has_fs_snapshots(c
)) {
4053 ERROR("container rootfs has dependent snapshots");
4057 bdev
= storage_init(c
->lxc_conf
);
4059 ERROR("Failed to find original backing store type");
4063 /* For an overlay container the rootfs is considered immutable
4064 * and cannot be removed when restoring from a snapshot. We pass this
4065 * internal flag along to communicate this to various parts of the
4068 if (!strcmp(bdev
->type
, "overlay") || !strcmp(bdev
->type
, "overlayfs"))
4069 bdev
->flags
|= LXC_STORAGE_INTERNAL_OVERLAY_RESTORE
;
4074 if (!get_snappath_dir(c
, clonelxcpath
)) {
4078 /* how should we lock this? */
4080 snap
= lxc_container_new(snapname
, clonelxcpath
);
4081 if (!snap
|| !lxcapi_is_defined(snap
)) {
4082 ERROR("Could not open snapshot %s", snapname
);
4084 lxc_container_put(snap
);
4089 if (!strcmp(c
->name
, newname
)) {
4090 if (!container_destroy(c
, bdev
)) {
4091 ERROR("Could not destroy existing container %s", newname
);
4092 lxc_container_put(snap
);
4098 if (strcmp(bdev
->type
, "dir") != 0 && strcmp(bdev
->type
, "loop") != 0)
4099 flags
= LXC_CLONE_SNAPSHOT
| LXC_CLONE_MAYBE_SNAPSHOT
;
4101 if (!strcmp(bdev
->type
, "overlay") || !strcmp(bdev
->type
, "overlayfs"))
4102 flags
|= LXC_STORAGE_INTERNAL_OVERLAY_RESTORE
;
4103 rest
= lxcapi_clone(snap
, newname
, c
->config_path
, flags
, bdev
->type
,
4106 if (rest
&& lxcapi_is_defined(rest
))
4109 lxc_container_put(rest
);
4111 lxc_container_put(snap
);
4115 WRAP_API_2(bool, lxcapi_snapshot_restore
, const char *, const char *)
4117 static bool do_snapshot_destroy(const char *snapname
, const char *clonelxcpath
)
4119 struct lxc_container
*snap
= NULL
;
4122 snap
= lxc_container_new(snapname
, clonelxcpath
);
4124 ERROR("Could not find snapshot %s", snapname
);
4128 if (!do_lxcapi_destroy(snap
)) {
4129 ERROR("Could not destroy snapshot %s", snapname
);
4136 lxc_container_put(snap
);
4140 static bool remove_all_snapshots(const char *path
)
4143 struct dirent
*direntp
;
4146 dir
= opendir(path
);
4148 SYSERROR("opendir on snapshot path %s", path
);
4151 while ((direntp
= readdir(dir
))) {
4152 if (!strcmp(direntp
->d_name
, "."))
4154 if (!strcmp(direntp
->d_name
, ".."))
4156 if (!do_snapshot_destroy(direntp
->d_name
, path
)) {
4165 SYSERROR("Error removing directory %s", path
);
4170 static bool do_lxcapi_snapshot_destroy(struct lxc_container
*c
, const char *snapname
)
4172 char clonelxcpath
[MAXPATHLEN
];
4174 if (!c
|| !c
->name
|| !c
->config_path
|| !snapname
)
4177 if (!get_snappath_dir(c
, clonelxcpath
))
4180 return do_snapshot_destroy(snapname
, clonelxcpath
);
4183 WRAP_API_1(bool, lxcapi_snapshot_destroy
, const char *)
4185 static bool do_lxcapi_snapshot_destroy_all(struct lxc_container
*c
)
4187 char clonelxcpath
[MAXPATHLEN
];
4189 if (!c
|| !c
->name
|| !c
->config_path
)
4192 if (!get_snappath_dir(c
, clonelxcpath
))
4195 return remove_all_snapshots(clonelxcpath
);
4198 WRAP_API(bool, lxcapi_snapshot_destroy_all
)
4200 static bool do_lxcapi_may_control(struct lxc_container
*c
)
4202 return lxc_try_cmd(c
->name
, c
->config_path
) == 0;
4205 WRAP_API(bool, lxcapi_may_control
)
4207 static bool do_add_remove_node(pid_t init_pid
, const char *path
, bool add
,
4213 char chrootpath
[MAXPATHLEN
];
4214 char *directory_path
= NULL
;
4218 SYSERROR("Failed to fork()");
4223 ret
= wait_for_pid(pid
);
4225 ERROR("Failed to create device node");
4232 /* prepare the path */
4233 ret
= snprintf(chrootpath
, MAXPATHLEN
, "/proc/%d/root", init_pid
);
4234 if (ret
< 0 || ret
>= MAXPATHLEN
)
4237 ret
= chroot(chrootpath
);
4245 /* remove path if it exists */
4246 ret
= faccessat(AT_FDCWD
, path
, F_OK
, AT_SYMLINK_NOFOLLOW
);
4250 ERROR("%s - Failed to remove \"%s\"", strerror(errno
), path
);
4258 /* create any missing directories */
4263 directory_path
= dirname(tmp
);
4264 ret
= mkdir_p(directory_path
, 0755);
4265 if (ret
< 0 && errno
!= EEXIST
) {
4266 ERROR("%s - Failed to create path \"%s\"", strerror(errno
), directory_path
);
4271 /* create the device node */
4272 ret
= mknod(path
, st
->st_mode
, st
->st_rdev
);
4275 ERROR("%s - Failed to create device node at \"%s\"", strerror(errno
), path
);
4282 static bool add_remove_device_node(struct lxc_container
*c
, const char *src_path
, const char *dest_path
, bool add
)
4286 char value
[MAX_BUFFER
];
4289 /* make sure container is running */
4290 if (!do_lxcapi_is_running(c
)) {
4291 ERROR("container is not running");
4295 /* use src_path if dest_path is NULL otherwise use dest_path */
4296 p
= dest_path
? dest_path
: src_path
;
4298 /* make sure we can access p */
4299 if(access(p
, F_OK
) < 0 || stat(p
, &st
) < 0)
4302 /* continue if path is character device or block device */
4303 if (S_ISCHR(st
.st_mode
))
4304 ret
= snprintf(value
, MAX_BUFFER
, "c %d:%d rwm", major(st
.st_rdev
), minor(st
.st_rdev
));
4305 else if (S_ISBLK(st
.st_mode
))
4306 ret
= snprintf(value
, MAX_BUFFER
, "b %d:%d rwm", major(st
.st_rdev
), minor(st
.st_rdev
));
4310 /* check snprintf return code */
4311 if (ret
< 0 || ret
>= MAX_BUFFER
)
4314 if (!do_add_remove_node(do_lxcapi_init_pid(c
), p
, add
, &st
))
4317 /* add or remove device to/from cgroup access list */
4319 if (!do_lxcapi_set_cgroup_item(c
, "devices.allow", value
)) {
4320 ERROR("set_cgroup_item failed while adding the device node");
4324 if (!do_lxcapi_set_cgroup_item(c
, "devices.deny", value
)) {
4325 ERROR("set_cgroup_item failed while removing the device node");
4333 static bool do_lxcapi_add_device_node(struct lxc_container
*c
, const char *src_path
, const char *dest_path
)
4335 // cannot mknod if we're not privileged wrt init_user_ns
4336 if (am_host_unpriv()) {
4337 ERROR(NOT_SUPPORTED_ERROR
, __FUNCTION__
);
4340 return add_remove_device_node(c
, src_path
, dest_path
, true);
4343 WRAP_API_2(bool, lxcapi_add_device_node
, const char *, const char *)
4345 static bool do_lxcapi_remove_device_node(struct lxc_container
*c
, const char *src_path
, const char *dest_path
)
4347 if (am_guest_unpriv()) {
4348 ERROR(NOT_SUPPORTED_ERROR
, __FUNCTION__
);
4351 return add_remove_device_node(c
, src_path
, dest_path
, false);
4354 WRAP_API_2(bool, lxcapi_remove_device_node
, const char *, const char *)
4356 static bool do_lxcapi_attach_interface(struct lxc_container
*c
,
4358 const char *dst_ifname
)
4363 if (am_guest_unpriv()) {
4364 ERROR(NOT_SUPPORTED_ERROR
, __FUNCTION__
);
4369 ERROR("No source interface name given");
4373 ret
= lxc_netdev_isup(ifname
);
4375 /* netdev of ifname is up. */
4376 ret
= lxc_netdev_down(ifname
);
4381 init_pid
= do_lxcapi_init_pid(c
);
4382 ret
= lxc_netdev_move_by_name(ifname
, init_pid
, dst_ifname
);
4386 INFO("Moved network device \"%s\" to network namespace of %d", ifname
, init_pid
);
4393 WRAP_API_2(bool, lxcapi_attach_interface
, const char *, const char *)
4395 static bool do_lxcapi_detach_interface(struct lxc_container
*c
,
4397 const char *dst_ifname
)
4400 pid_t pid
, pid_outside
;
4403 * TODO - if this is a physical device, then we need am_host_unpriv.
4404 * But for other types guest privilege suffices.
4406 if (am_guest_unpriv()) {
4407 ERROR(NOT_SUPPORTED_ERROR
, __FUNCTION__
);
4412 ERROR("No source interface name given");
4416 pid_outside
= lxc_raw_getpid();
4419 ERROR("Failed to fork");
4423 if (pid
== 0) { /* child */
4426 init_pid
= do_lxcapi_init_pid(c
);
4427 if (!switch_to_ns(init_pid
, "net")) {
4428 ERROR("Failed to enter network namespace");
4432 ret
= lxc_netdev_isup(ifname
);
4434 ERROR("Failed to determine whether network device \"%s\" is up", ifname
);
4438 /* netdev of ifname is up. */
4440 ret
= lxc_netdev_down(ifname
);
4442 ERROR("Failed to set network device \"%s\" down", ifname
);
4447 ret
= lxc_netdev_move_by_name(ifname
, pid_outside
, dst_ifname
);
4448 /* -EINVAL means there is no netdev named as ifname. */
4451 ERROR("Network device \"%s\" not found", ifname
);
4453 ERROR("Failed to remove network device \"%s\"", ifname
);
4460 ret
= wait_for_pid(pid
);
4464 INFO("Moved network device \"%s\" to network namespace of %d", ifname
, pid_outside
);
4468 WRAP_API_2(bool, lxcapi_detach_interface
, const char *, const char *)
4470 static int do_lxcapi_migrate(struct lxc_container
*c
, unsigned int cmd
,
4471 struct migrate_opts
*opts
, unsigned int size
)
4474 struct migrate_opts
*valid_opts
= opts
;
4475 uint64_t features_to_check
= 0;
4477 /* If the caller has a bigger (newer) struct migrate_opts, let's make
4478 * sure that the stuff on the end is zero, i.e. that they didn't ask us
4479 * to do anything special.
4481 if (size
> sizeof(*opts
)) {
4482 unsigned char *addr
;
4485 addr
= (void *)opts
+ sizeof(*opts
);
4486 end
= (void *)opts
+ size
;
4487 for (; addr
< end
; addr
++) {
4494 /* If the caller has a smaller struct, let's zero out the end for them
4495 * so we don't accidentally use bits of it that they didn't know about
4498 if (size
< sizeof(*opts
)) {
4499 valid_opts
= malloc(sizeof(*opts
));
4503 memset(valid_opts
, 0, sizeof(*opts
));
4504 memcpy(valid_opts
, opts
, size
);
4508 case MIGRATE_PRE_DUMP
:
4509 if (!do_lxcapi_is_running(c
)) {
4510 ERROR("container is not running");
4513 ret
= !__criu_pre_dump(c
, valid_opts
);
4516 if (!do_lxcapi_is_running(c
)) {
4517 ERROR("container is not running");
4520 ret
= !__criu_dump(c
, valid_opts
);
4522 case MIGRATE_RESTORE
:
4523 if (do_lxcapi_is_running(c
)) {
4524 ERROR("container is already running");
4527 ret
= !__criu_restore(c
, valid_opts
);
4529 case MIGRATE_FEATURE_CHECK
:
4530 features_to_check
= valid_opts
->features_to_check
;
4531 ret
= !__criu_check_feature(&features_to_check
);
4533 /* Something went wrong. Let's let the caller
4534 * know which feature checks failed. */
4535 valid_opts
->features_to_check
= features_to_check
;
4539 ERROR("invalid migrate command %u", cmd
);
4544 if (size
< sizeof(*opts
))
4550 WRAP_API_3(int, lxcapi_migrate
, unsigned int, struct migrate_opts
*, unsigned int)
4552 static bool do_lxcapi_checkpoint(struct lxc_container
*c
, char *directory
, bool stop
, bool verbose
)
4554 struct migrate_opts opts
;
4556 memset(&opts
, 0, sizeof(opts
));
4558 opts
.directory
= directory
;
4560 opts
.verbose
= verbose
;
4562 return !do_lxcapi_migrate(c
, MIGRATE_DUMP
, &opts
, sizeof(opts
));
4565 WRAP_API_3(bool, lxcapi_checkpoint
, char *, bool, bool)
4567 static bool do_lxcapi_restore(struct lxc_container
*c
, char *directory
, bool verbose
)
4569 struct migrate_opts opts
;
4571 memset(&opts
, 0, sizeof(opts
));
4573 opts
.directory
= directory
;
4574 opts
.verbose
= verbose
;
4576 return !do_lxcapi_migrate(c
, MIGRATE_RESTORE
, &opts
, sizeof(opts
));
4579 WRAP_API_2(bool, lxcapi_restore
, char *, bool)
4581 static int lxcapi_attach_run_waitl(struct lxc_container
*c
, lxc_attach_options_t
*options
, const char *program
, const char *arg
, ...)
4590 current_config
= c
->lxc_conf
;
4593 argv
= lxc_va_arg_list_to_argv_const(ap
, 1);
4597 ERROR("Memory allocation error.");
4603 ret
= do_lxcapi_attach_run_wait(c
, options
, program
, (const char * const *)argv
);
4606 current_config
= NULL
;
4610 struct lxc_container
*lxc_container_new(const char *name
, const char *configpath
)
4612 struct lxc_container
*c
;
4617 c
= malloc(sizeof(*c
));
4619 fprintf(stderr
, "Failed to allocate memory for %s\n", name
);
4622 memset(c
, 0, sizeof(*c
));
4625 c
->config_path
= strdup(configpath
);
4627 c
->config_path
= strdup(lxc_global_config_value("lxc.lxcpath"));
4629 if (!c
->config_path
) {
4630 fprintf(stderr
, "Failed to allocate memory for %s\n", name
);
4634 remove_trailing_slashes(c
->config_path
);
4635 c
->name
= malloc(strlen(name
)+1);
4637 fprintf(stderr
, "Failed to allocate memory for %s\n", name
);
4640 strcpy(c
->name
, name
);
4643 c
->slock
= lxc_newlock(c
->config_path
, name
);
4645 fprintf(stderr
, "Failed to create lock for %s\n", name
);
4649 c
->privlock
= lxc_newlock(NULL
, NULL
);
4651 fprintf(stderr
, "Failed to create private lock for %s\n", name
);
4655 if (!set_config_filename(c
)) {
4656 fprintf(stderr
, "Failed to create config file name for %s\n", name
);
4660 if (file_exists(c
->configfile
) && !lxcapi_load_config(c
, NULL
)) {
4661 fprintf(stderr
, "Failed to load config for %s\n", name
);
4665 if (ongoing_create(c
) == 2) {
4666 ERROR("Failed to complete container creation for %s", c
->name
);
4667 container_destroy(c
, NULL
);
4668 lxcapi_clear_config(c
);
4670 c
->daemonize
= true;
4673 /* Assign the member functions. */
4674 c
->is_defined
= lxcapi_is_defined
;
4675 c
->state
= lxcapi_state
;
4676 c
->is_running
= lxcapi_is_running
;
4677 c
->freeze
= lxcapi_freeze
;
4678 c
->unfreeze
= lxcapi_unfreeze
;
4679 c
->console
= lxcapi_console
;
4680 c
->console_getfd
= lxcapi_console_getfd
;
4681 c
->init_pid
= lxcapi_init_pid
;
4682 c
->load_config
= lxcapi_load_config
;
4683 c
->want_daemonize
= lxcapi_want_daemonize
;
4684 c
->want_close_all_fds
= lxcapi_want_close_all_fds
;
4685 c
->start
= lxcapi_start
;
4686 c
->startl
= lxcapi_startl
;
4687 c
->stop
= lxcapi_stop
;
4688 c
->config_file_name
= lxcapi_config_file_name
;
4689 c
->wait
= lxcapi_wait
;
4690 c
->set_config_item
= lxcapi_set_config_item
;
4691 c
->destroy
= lxcapi_destroy
;
4692 c
->destroy_with_snapshots
= lxcapi_destroy_with_snapshots
;
4693 c
->rename
= lxcapi_rename
;
4694 c
->save_config
= lxcapi_save_config
;
4695 c
->get_keys
= lxcapi_get_keys
;
4696 c
->create
= lxcapi_create
;
4697 c
->createl
= lxcapi_createl
;
4698 c
->shutdown
= lxcapi_shutdown
;
4699 c
->reboot
= lxcapi_reboot
;
4700 c
->reboot2
= lxcapi_reboot2
;
4701 c
->clear_config
= lxcapi_clear_config
;
4702 c
->clear_config_item
= lxcapi_clear_config_item
;
4703 c
->get_config_item
= lxcapi_get_config_item
;
4704 c
->get_running_config_item
= lxcapi_get_running_config_item
;
4705 c
->get_cgroup_item
= lxcapi_get_cgroup_item
;
4706 c
->set_cgroup_item
= lxcapi_set_cgroup_item
;
4707 c
->get_config_path
= lxcapi_get_config_path
;
4708 c
->set_config_path
= lxcapi_set_config_path
;
4709 c
->clone
= lxcapi_clone
;
4710 c
->get_interfaces
= lxcapi_get_interfaces
;
4711 c
->get_ips
= lxcapi_get_ips
;
4712 c
->attach
= lxcapi_attach
;
4713 c
->attach_run_wait
= lxcapi_attach_run_wait
;
4714 c
->attach_run_waitl
= lxcapi_attach_run_waitl
;
4715 c
->snapshot
= lxcapi_snapshot
;
4716 c
->snapshot_list
= lxcapi_snapshot_list
;
4717 c
->snapshot_restore
= lxcapi_snapshot_restore
;
4718 c
->snapshot_destroy
= lxcapi_snapshot_destroy
;
4719 c
->snapshot_destroy_all
= lxcapi_snapshot_destroy_all
;
4720 c
->may_control
= lxcapi_may_control
;
4721 c
->add_device_node
= lxcapi_add_device_node
;
4722 c
->remove_device_node
= lxcapi_remove_device_node
;
4723 c
->attach_interface
= lxcapi_attach_interface
;
4724 c
->detach_interface
= lxcapi_detach_interface
;
4725 c
->checkpoint
= lxcapi_checkpoint
;
4726 c
->restore
= lxcapi_restore
;
4727 c
->migrate
= lxcapi_migrate
;
4728 c
->console_log
= lxcapi_console_log
;
4733 lxc_container_free(c
);
4737 int lxc_get_wait_states(const char **states
)
4742 for (i
=0; i
<MAX_STATE
; i
++)
4743 states
[i
] = lxc_state2str(i
);
4748 * These next two could probably be done smarter with reusing a common function
4749 * with different iterators and tests...
4751 int list_defined_containers(const char *lxcpath
, char ***names
, struct lxc_container
***cret
)
4754 int i
, cfound
= 0, nfound
= 0;
4755 struct dirent
*direntp
;
4756 struct lxc_container
*c
;
4759 lxcpath
= lxc_global_config_value("lxc.lxcpath");
4761 dir
= opendir(lxcpath
);
4763 SYSERROR("opendir on lxcpath");
4772 while ((direntp
= readdir(dir
))) {
4776 /* Ignore '.', '..' and any hidden directory. */
4777 if (!strncmp(direntp
->d_name
, ".", 1))
4780 if (!config_file_exists(lxcpath
, direntp
->d_name
))
4784 if (!add_to_array(names
, direntp
->d_name
, cfound
))
4794 c
= lxc_container_new(direntp
->d_name
, lxcpath
);
4796 INFO("Container %s:%s has a config but could not be loaded",
4797 lxcpath
, direntp
->d_name
);
4799 if(!remove_from_array(names
, direntp
->d_name
, cfound
--))
4803 if (!do_lxcapi_is_defined(c
)) {
4804 INFO("Container %s:%s has a config but is not defined",
4805 lxcpath
, direntp
->d_name
);
4807 if(!remove_from_array(names
, direntp
->d_name
, cfound
--))
4809 lxc_container_put(c
);
4813 if (!add_to_clist(cret
, c
, nfound
, true)) {
4814 lxc_container_put(c
);
4824 if (names
&& *names
) {
4825 for (i
=0; i
<cfound
; i
++)
4829 if (cret
&& *cret
) {
4830 for (i
=0; i
<nfound
; i
++)
4831 lxc_container_put((*cret
)[i
]);
4838 int list_active_containers(const char *lxcpath
, char ***nret
,
4839 struct lxc_container
***cret
)
4841 int i
, ret
= -1, cret_cnt
= 0, ct_name_cnt
= 0;
4844 char **ct_name
= NULL
;
4846 struct lxc_container
*c
= NULL
;
4850 lxcpath
= lxc_global_config_value("lxc.lxcpath");
4851 lxcpath_len
= strlen(lxcpath
);
4858 FILE *f
= fopen("/proc/net/unix", "r");
4862 while (getline(&line
, &len
, f
) != -1) {
4864 char *p
= strrchr(line
, ' '), *p2
;
4873 if (strncmp(p
, lxcpath
, lxcpath_len
) == 0) {
4875 } else if (strncmp(p
, "lxc/", 4) == 0) {
4885 /* Now p is the start of lxc_name. */
4886 p2
= strchr(p
, '/');
4887 if (!p2
|| strncmp(p2
, "/command", 8) != 0)
4892 char *recvpath
= lxc_cmd_get_lxcpath(p
);
4895 if (strncmp(lxcpath
, recvpath
, lxcpath_len
) != 0)
4897 p
= lxc_cmd_get_name(p
);
4902 if (array_contains(&ct_name
, p
, ct_name_cnt
))
4905 if (!add_to_array(&ct_name
, p
, ct_name_cnt
))
4906 goto free_cret_list
;
4913 c
= lxc_container_new(p
, lxcpath
);
4915 INFO("Container %s:%s is running but could not be loaded",
4917 remove_from_array(&ct_name
, p
, ct_name_cnt
--);
4922 * If this is an anonymous container, then is_defined *can*
4923 * return false. So we don't do that check. Count on the
4924 * fact that the command socket exists.
4927 if (!add_to_clist(cret
, c
, cret_cnt
, true)) {
4928 lxc_container_put(c
);
4929 goto free_cret_list
;
4934 if (nret
&& cret
&& cret_cnt
!= ct_name_cnt
) {
4936 lxc_container_put(c
);
4937 goto free_cret_list
;
4948 if (cret
&& *cret
) {
4949 for (i
= 0; i
< cret_cnt
; i
++)
4950 lxc_container_put((*cret
)[i
]);
4956 for (i
= 0; i
< ct_name_cnt
; i
++)
4968 int list_all_containers(const char *lxcpath
, char ***nret
,
4969 struct lxc_container
***cret
)
4971 int i
, ret
, active_cnt
, ct_cnt
, ct_list_cnt
;
4974 struct lxc_container
**ct_list
= NULL
;
4976 ct_cnt
= list_defined_containers(lxcpath
, &ct_name
, NULL
);
4980 active_cnt
= list_active_containers(lxcpath
, &active_name
, NULL
);
4981 if (active_cnt
< 0) {
4986 for (i
= 0; i
< active_cnt
; i
++) {
4987 if (!array_contains(&ct_name
, active_name
[i
], ct_cnt
)) {
4988 if (!add_to_array(&ct_name
, active_name
[i
], ct_cnt
)) {
4990 goto free_active_name
;
4994 free(active_name
[i
]);
4995 active_name
[i
] = NULL
;
5001 for (i
= 0, ct_list_cnt
= 0; i
< ct_cnt
&& cret
; i
++) {
5002 struct lxc_container
*c
;
5004 c
= lxc_container_new(ct_name
[i
], lxcpath
);
5006 WARN("Container %s:%s could not be loaded", lxcpath
, ct_name
[i
]);
5007 remove_from_array(&ct_name
, ct_name
[i
], ct_cnt
--);
5011 if (!add_to_clist(&ct_list
, c
, ct_list_cnt
, false)) {
5012 lxc_container_put(c
);
5031 for (i
= 0; i
< ct_list_cnt
; i
++) {
5032 lxc_container_put(ct_list
[i
]);
5037 for (i
= 0; i
< active_cnt
; i
++) {
5038 free(active_name
[i
]);
5043 for (i
= 0; i
< ct_cnt
; i
++) {
5050 bool lxc_config_item_is_supported(const char *key
)
5052 return !!lxc_get_config(key
);