lxccontainer.c revision eddaaafd1a9b02ba39e5b6b13d40b4a5d37a04e1
/* liblxcapi
*
* Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
* Copyright © 2012 Canonical Ltd.
*
* it under the terms of the GNU General Public License version 2, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#define _GNU_SOURCE
#include <pthread.h>
#include <unistd.h>
#include <errno.h>
#include <sched.h>
#include "config.h"
#include "lxc.h"
#include "state.h"
#include "lxccontainer.h"
#include "conf.h"
#include "confile.h"
#include "cgroup.h"
#include "commands.h"
#include "version.h"
#include "log.h"
#include "bdev.h"
#include "utils.h"
#include <sched.h>
#include <fcntl.h>
#include <ifaddrs.h>
static bool file_exists(char *f)
{
}
/*
* A few functions to help detect when a container creation failed.
* If a container creation was killed partway through, then trying
* to actually start that container could harm the host. We detect
* this by creating a 'partial' file under the container directory,
* and keeping an advisory lock. When container creation completes,
* we remove that file. When we load or try to start a container, if
* we find that file, without a flock, we remove the container.
*/
int ongoing_create(struct lxc_container *c)
{
ERROR("Error writing partial pathname");
return -1;
}
if (!file_exists(path))
return 0;
if (process_lock())
return -1;
// give benefit of the doubt
SYSERROR("Error opening partial file");
return 0;
}
errno == EWOULDBLOCK) {
// create is still ongoing
return 1;
}
// create completed but partial is still there.
return 2;
}
int create_partial(struct lxc_container *c)
{
// $lxcpath + '/' + $name + '/partial' + \0
ERROR("Error writing partial pathname");
return -1;
}
if (process_lock())
return -1;
SYSERROR("Erorr creating partial file");
return -1;
}
return -1;
}
return fd;
}
{
// $lxcpath + '/' + $name + '/partial' + \0
int ret;
ERROR("Error writing partial pathname");
return;
}
if (process_lock())
return;
}
/* LOCKING
* 1. container_mem_lock(c) protects the struct lxc_container from multiple threads.
* 2. container_disk_lock(c) protects the on-disk container data - in particular the
* container configuration file.
* The container_disk_lock also takes the container_mem_lock.
* 3. thread_mutex protects process data (ex: fd table) from multiple threads.
* NOTHING mutexes two independent programs with their own struct
* lxc_container for the same c->name, between API calls. For instance,
* c->config_read(); c->start(); Between those calls, data on disk
* could change (which shouldn't bother the caller unless for instance
* the rootfs get moved). c->config_read(); update; c->config_write();
* Two such updaters could race. The callers should therefore check their
* results. Trying to prevent that would necessarily expose us to deadlocks
* due to hung callers. So I prefer to keep the locks only within our own
* functions, not across functions.
*
* If you're going to clone while holding a lxccontainer, increment
* c->numthreads (under privlock) before forking. When deleting,
* decrement numthreads under privlock, then if it hits 0 you can delete.
* Do not ever use a lxccontainer whose numthreads you did not bump.
*/
static void lxc_container_free(struct lxc_container *c)
{
if (!c)
return;
if (c->configfile) {
free(c->configfile);
c->configfile = NULL;
}
if (c->error_string) {
free(c->error_string);
c->error_string = NULL;
}
if (c->slock) {
lxc_putlock(c->slock);
}
if (c->privlock) {
lxc_putlock(c->privlock);
}
if (c->name) {
}
if (c->lxc_conf) {
lxc_conf_free(c->lxc_conf);
}
if (c->config_path) {
free(c->config_path);
c->config_path = NULL;
}
free(c);
}
/*
* Consider the following case:
freer | racing get()er
==================================================================
lxc_container_put() | lxc_container_get()
\ lxclock(c->privlock) | c->numthreads < 1? (no)
\ c->numthreads = 0 | \ lxclock(c->privlock) -> waits
\ lxcunlock() | \
\ lxc_container_free() | \ lxclock() returns
| \ c->numthreads < 1 -> return 0
\ \ (free stuff) |
\ \ sem_destroy(privlock) |
* When the get()er checks numthreads the first time, one of the following
* is true:
* 1. freer has set numthreads = 0. get() returns 0
* 2. freer is between lxclock and setting numthreads to 0. get()er will
* sem_wait on privlock, get lxclock after freer() drops it, then see
* numthreads is 0 and exit without touching lxclock again..
* 3. freer has not yet locked privlock. If get()er runs first, then put()er
* will see --numthreads = 1 and not call lxc_container_free().
*/
int lxc_container_get(struct lxc_container *c)
{
if (!c)
return 0;
// if someone else has already started freeing the container, don't
// try to take the lock, which may be invalid
if (c->numthreads < 1)
return 0;
if (container_mem_lock(c))
return 0;
if (c->numthreads < 1) {
// bail without trying to unlock, bc the privlock is now probably
// in freed memory
return 0;
}
c->numthreads++;
return 1;
}
int lxc_container_put(struct lxc_container *c)
{
if (!c)
return -1;
if (container_mem_lock(c))
return -1;
if (--c->numthreads < 1) {
return 1;
}
return 0;
}
static bool lxcapi_is_defined(struct lxc_container *c)
{
bool ret = false;
int statret;
if (!c)
return false;
if (container_mem_lock(c))
return false;
if (!c->configfile)
goto out;
if (statret != 0)
goto out;
ret = true;
out:
return ret;
}
static const char *lxcapi_state(struct lxc_container *c)
{
lxc_state_t s;
if (!c)
return NULL;
return lxc_state2str(s);
}
static bool is_stopped(struct lxc_container *c)
{
lxc_state_t s;
return (s == STOPPED);
}
static bool lxcapi_is_running(struct lxc_container *c)
{
const char *s;
if (!c)
return false;
s = lxcapi_state(c);
if (!s || strcmp(s, "STOPPED") == 0)
return false;
return true;
}
static bool lxcapi_freeze(struct lxc_container *c)
{
int ret;
if (!c)
return false;
if (ret)
return false;
return true;
}
static bool lxcapi_unfreeze(struct lxc_container *c)
{
int ret;
if (!c)
return false;
if (ret)
return false;
return true;
}
{
int ttyfd;
if (!c)
return -1;
return ttyfd;
}
{
if (!c)
return -1;
}
{
if (!c->lxc_conf)
c->lxc_conf = lxc_conf_init();
return true;
return false;
}
{
bool ret = false, need_disklock = false;
int lret;
const char *fname;
if (!c)
return false;
fname = c->configfile;
if (alt_file)
if (!fname)
return false;
/*
* If we're reading something other than the container's config,
* we only need to lock the in-memory container. If loading the
* container's config file, take the disk lock.
*/
need_disklock = true;
if (need_disklock)
lret = container_disk_lock(c);
else
lret = container_mem_lock(c);
if (lret)
return false;
if (need_disklock)
else
return ret;
}
static void lxcapi_want_daemonize(struct lxc_container *c)
{
if (!c)
return;
if (!container_mem_lock(c)) {
ERROR("Error getting mem lock");
return;
}
c->daemonize = 1;
}
{
int ret;
if (!c)
return false;
return ret == 0;
}
static bool wait_on_daemonized_start(struct lxc_container *c)
{
/* we'll probably want to make this timeout configurable? */
/*
* our child is going to fork again, then exit. reap the
* child
*/
DEBUG("failed waiting for first dual-fork child");
}
/*
* I can't decide if it'd be more convenient for callers if we accept '...',
* or a null-terminated array (i.e. execl vs execv)
*/
{
int ret;
int daemonize = 0;
char *default_args[] = {
'\0',
};
/* container exists */
if (!c)
return false;
/* container has been setup */
if (!c->lxc_conf)
return false;
if ((ret = ongoing_create(c)) < 0) {
ERROR("Error checking for incomplete creation");
return false;
}
if (ret == 2) {
c->destroy(c);
return false;
} else if (ret == 1) {
return false;
}
/* is this app meant to be run through lxcinit, as in lxc-execute? */
return false;
if (container_mem_lock(c))
return false;
if (useinit) {
return ret == 0 ? true : false;
}
if (!argv)
argv = default_args;
/*
* say, I'm not sure - what locks do we want here? Any?
* Is liblxc's locking enough here to protect the on disk
* container? We don't want to exclude things like lxc_info
* while container is running...
*/
if (daemonize) {
if (!lxc_container_get(c))
return false;
if (process_lock())
return false;
if (pid < 0) {
return false;
}
if (pid != 0) {
ret = wait_on_daemonized_start(c);
return ret;
}
/* second fork to be reparented by init */
if (pid < 0) {
SYSERROR("Error doing dual-fork");
return false;
}
if (pid != 0)
exit(0);
if (chdir("/")) {
SYSERROR("Error chdir()ing to /.");
return false;
}
close(0);
close(1);
close(2);
setsid();
}
INFO("container requested reboot");
goto reboot;
}
if (daemonize) {
} else {
return (ret == 0 ? true : false);
}
}
/*
* note there MUST be an ending NULL
*/
{
int n_inargs = 0;
bool bret = false;
/* container exists */
if (!c)
return false;
/* build array of arguments if any */
while (1) {
char *arg;
if (!arg)
break;
n_inargs++;
if (!temp) {
goto out;
}
}
/* add trailing NULL */
if (n_inargs) {
n_inargs++;
if (!temp)
goto out;
}
out:
if (inargs) {
int i;
for (i = 0; i < n_inargs; i++) {
if (inargs[i])
}
}
return bret;
}
static bool lxcapi_stop(struct lxc_container *c)
{
int ret;
if (!c)
return false;
return ret == 0;
}
static bool valid_template(char *t)
{
int statret;
if (statret == 0)
return true;
return false;
}
/*
* create the standard expected container dir
*/
static bool create_container_dir(struct lxc_container *c)
{
char *s;
if (!s)
return false;
free(s);
return false;
}
if (ret) {
ret = 0;
else
}
free(s);
return ret == 0;
}
static const char *lxcapi_get_config_path(struct lxc_container *c);
/*
* do_bdev_create: thin wrapper around bdev_create(). Like bdev_create(),
* it returns a mounted bdev on success, NULL on error.
*/
struct bdev_specs *specs)
{
char *dest;
const char *lxcpath = lxcapi_get_config_path(c);
int ret;
return NULL;
if (!bdev)
return NULL;
return bdev;
}
static bool lxcapi_destroy(struct lxc_container *c);
/*
* lxcapi_create:
* create a container with the given parameters.
* @c: container to be created. It has the lxcpath, name, and a starting
* configuration already set
* @t: the template to execute to instantiate the root filesystem and
* adjust the configuration.
* @bdevtype: backing store type to use. If NULL, dir will be used.
* @specs: additional parameters for the backing store, i.e. LVM vg to
* use.
*
* @argv: the arguments to pass to the template, terminated by NULL. If no
* arguments, you can just pass NULL.
*/
static bool lxcapi_create(struct lxc_container *c, const char *t,
char *const argv[])
{
bool bret = false;
if (!c)
return false;
if (!tpath)
return false;
goto out;
if (!valid_template(tpath)) {
ERROR("bad template: %s\n", t);
goto out;
}
if (!c->save_config(c, NULL)) {
goto out;
}
/* container is already created if we have a config and rootfs.path is accessible */
if (lxcapi_is_defined(c) && c->lxc_conf && c->lxc_conf->rootfs.path && access(c->lxc_conf->rootfs.path, F_OK) == 0)
goto out;
/* Mark that this container is being created */
if ((partial_fd = create_partial(c)) < 0)
goto out;
/* no need to get disk lock bc we have the partial locked */
/*
* Create the backing store
* Note we can't do this in the same task as we use to execute the
* template because of the way zfs works.
* After you 'zfs create', zfs mounts the fs only in the initial
* namespace.
*/
if (pid < 0) {
SYSERROR("failed to fork task for container creation template\n");
goto out_unlock;
}
if (pid == 0) { // child
ERROR("Error creating backing store type %s for %s",
exit(1);
}
/* save config file again to store the new rootfs location */
if (!c->save_config(c, NULL)) {
// parent task won't see bdev in config so we delete it
exit(1);
}
exit(0);
}
if (wait_for_pid(pid) != 0)
goto out;
/* reload config to get the rootfs */
if (c->lxc_conf)
lxc_conf_free(c->lxc_conf);
if (!load_config_locked(c, c->configfile))
goto out;
/*
* now execute the template
*/
if (pid < 0) {
SYSERROR("failed to fork task for container creation template\n");
goto out_unlock;
}
if (pid == 0) { // child
int i;
if (unshare(CLONE_NEWNS) < 0) {
ERROR("error unsharing mounts");
exit(1);
}
/*
* for an overlayfs create, what the user wants is the template to fill
* in what will become the readonly lower layer. So don't mount for
* the template
*/
}
if (!bdev) {
ERROR("Error opening rootfs");
exit(1);
}
ERROR("Error mounting rootfs");
exit(1);
}
/*
* create our new array, pre-pend the template name and
* base args
*/
if (argv)
if (!newargv)
exit(1);
newargv[0] = (char *)t;
if (!patharg)
exit(1);
exit(1);
if (!namearg)
exit(1);
exit(1);
if (!rootfsarg)
exit(1);
exit(1);
/* add passed-in args */
if (argv)
for (i = 4; i < nargs; i++)
/* add trailing NULL */
nargs++;
if (!newargv)
exit(1);
/* execute */
exit(1);
}
if (wait_for_pid(pid) != 0) {
goto out_unlock;
}
// now clear out the lxc_conf we have, reload from the created
// container
if (c->lxc_conf)
lxc_conf_free(c->lxc_conf);
if (partial_fd >= 0)
remove_partial(c, partial_fd);
out:
if (tpath)
if (!bret && c)
lxcapi_destroy(c);
return bret;
}
static bool lxcapi_reboot(struct lxc_container *c)
{
if (!c)
return false;
if (!c->is_running(c))
return false;
if (pid <= 0)
return false;
return false;
return true;
}
{
bool retv;
if (!c)
return false;
if (!timeout)
timeout = -1;
if (!c->is_running(c))
return true;
if (pid <= 0)
return true;
c->stop(c);
}
return retv;
}
static bool lxcapi_createl(struct lxc_container *c, const char *t,
{
bool bret = false;
int nargs = 0;
if (!c)
return false;
/*
* since we're going to wait for create to finish, I don't think we
* need to get a copy of the arguments.
*/
while (1) {
char *arg;
if (!arg)
break;
nargs++;
if (!temp) {
goto out;
}
}
if (args)
out:
if (args)
return bret;
}
{
int ret;
if (!c || !c->lxc_conf)
return false;
if (container_mem_lock(c))
return false;
return ret == 0;
}
{
int count = 0;
void *tempAddrPtr = NULL;
char new_netns_path[MAXPATHLEN];
if (!c->is_running(c))
goto out;
/* Save reference to old netns */
if (old_netns < 0) {
goto out;
}
/* Switch to new netns */
goto out;
if (new_netns < 0) {
goto out;
}
SYSERROR("failed to setns");
goto out;
}
/* Grab the list of interfaces */
if (getifaddrs(&interfaceArray)) {
SYSERROR("failed to get interfaces list");
goto out;
}
/* Iterate through the interfaces */
continue;
}
else {
continue;
continue;
}
continue;
continue;
sizeof(addressOutputBuffer));
if (!address)
continue;
count += 1;
if (!temp) {
count--;
goto out;
}
}
out:
if(interfaceArray)
/* Switch back to original netns */
SYSERROR("failed to setns");
if (new_netns >= 0)
if (old_netns >= 0)
/* Append NULL to the array */
if (count) {
count++;
if (!temp) {
int i;
for (i = 0; i < count-1; i++)
return NULL;
}
}
return addresses;
}
{
int ret;
if (!c || !c->lxc_conf)
return -1;
if (container_mem_lock(c))
return -1;
return ret;
}
{
if (!key)
/*
* Support 'lxc.network.<idx>', i.e. 'lxc.network.0'
* This is an intelligent result to show which keys are valid given
* the type of nic it is
*/
if (!c || !c->lxc_conf)
return -1;
if (container_mem_lock(c))
return -1;
int ret = -1;
return ret;
}
{
bool ret = false, need_disklock = false;
int lret;
if (!alt_file)
alt_file = c->configfile;
if (!alt_file)
return false; // should we write to stdout if no file is specified?
// If we haven't yet loaded a config, load the stock config
if (!c->lxc_conf) {
if (!c->load_config(c, LXC_DEFAULT_CONFIG)) {
ERROR("Error loading default configuration file %s while saving %s\n", LXC_DEFAULT_CONFIG, c->name);
return false;
}
}
if (!create_container_dir(c))
return false;
/*
* If we're writing to the container's config file, take the
* disk lock. Otherwise just take the memlock to protect the
* struct lxc_container while we're traversing it.
*/
need_disklock = true;
if (need_disklock)
lret = container_disk_lock(c);
else
lret = container_mem_lock(c);
if (lret)
return false;
if (!fout)
goto out;
ret = true;
out:
if (need_disklock)
else
return ret;
}
// do we want the api to support --force, or leave that to the caller?
static bool lxcapi_destroy(struct lxc_container *c)
{
bool ret = false;
if (!c || !lxcapi_is_defined(c))
return false;
if (container_disk_lock(c))
return false;
if (!is_stopped(c)) {
// we should queue some sort of error - in c->error_string?
goto out;
}
if (r) {
goto out;
}
}
const char *p1 = lxcapi_get_config_path(c);
if (lxc_rmdir_onedev(path) < 0) {
goto out;
}
ret = true;
out:
return ret;
}
{
int ret;
bool b = false;
struct lxc_config_t *config;
if (!c)
return false;
if (container_mem_lock(c))
return false;
if (!c->lxc_conf)
c->lxc_conf = lxc_conf_init();
if (!c->lxc_conf)
goto err;
if (!config)
goto err;
if (!ret)
b = true;
err:
return b;
}
static char *lxcapi_config_file_name(struct lxc_container *c)
{
if (!c || !c->configfile)
return NULL;
return strdup(c->configfile);
}
static const char *lxcapi_get_config_path(struct lxc_container *c)
{
if (!c || !c->config_path)
return NULL;
return (const char *)(c->config_path);
}
/*
* not for export
* Just recalculate the c->configfile based on the
* c->config_path, which must be set.
* The lxc_container must be locked or not yet public.
*/
static bool set_config_filename(struct lxc_container *c)
{
char *newpath;
if (!c->config_path)
return false;
/* $lxc_path + "/" + c->name + "/" + "config" + '\0' */
if (!newpath)
return false;
return false;
}
if (c->configfile)
free(c->configfile);
c->configfile = newpath;
return true;
}
{
char *p;
bool b = false;
if (!c)
return b;
if (container_mem_lock(c))
return b;
if (!p) {
ERROR("Out of memory setting new lxc path");
goto err;
}
b = true;
if (c->config_path)
oldpath = c->config_path;
c->config_path = p;
/* Since we've changed the config path, we have to change the
* config file name too */
if (!set_config_filename(c)) {
ERROR("Out of memory setting new config filename");
b = false;
free(c->config_path);
c->config_path = oldpath;
}
err:
if (oldpath)
return b;
}
{
int ret;
if (!c)
return false;
if (is_stopped(c))
return false;
if (container_disk_lock(c))
return false;
return ret == 0;
}
static int lxcapi_get_cgroup_item(struct lxc_container *c, const char *subsys, char *retv, int inlen)
{
int ret;
if (!c || !c->lxc_conf)
return -1;
if (is_stopped(c))
return -1;
if (container_disk_lock(c))
return -1;
return ret;
}
const char *lxc_get_default_config_path(void)
{
return default_lxc_path();
}
const char *lxc_get_default_lvm_vg(void)
{
return default_lvm_vg();
}
const char *lxc_get_default_zfs_root(void)
{
return default_zfs_root();
}
const char *lxc_get_version(void)
{
return lxc_version();
}
{
char buf[8096];
if (file_exists(new)) {
return -1;
}
if (ret < 0) {
return -1;
}
if (in < 0) {
return -1;
}
if (out < 0) {
return -1;
}
while (1) {
if (len < 0) {
goto err;
}
if (len == 0)
break;
goto err;
}
}
if (ret) {
return -1;
}
return 0;
err:
return -1;
}
/*
* we're being passed result of two strstrs(x, y). We want to write
* all data up to the first found string, or to end of the string if
* neither string was found.
* This function will return the earliest found string if any, or else
* NULL
*/
{
if (!p1)
return p2;
if (!p2)
return p1;
}
static int is_word_sep(char c)
{
switch(c) {
case '\0':
case '\n':
case '\r':
case '\t':
case ' ':
case '=':
case '/':
return 1;
default: return 0;
}
}
static const char *find_first_wholeword(const char *p, const char *word)
{
if (!p)
return NULL;
return p;
p++;
}
return NULL;
}
{
FILE *f;
long flen;
char *contents;
if (!f) {
SYSERROR("opening old config");
return -1;
}
SYSERROR("seeking to end of old config");
fclose(f);
return -1;
}
if (flen < 0) {
fclose(f);
SYSERROR("telling size of old config");
return -1;
}
fclose(f);
SYSERROR("rewinding old config");
return -1;
}
if (!contents) {
SYSERROR("out of memory");
fclose(f);
return -1;
}
fclose(f);
SYSERROR("reading old config");
return -1;
}
if (fclose(f) < 0) {
SYSERROR("closing old config");
return -1;
}
if (!f) {
SYSERROR("reopening config");
return -1;
}
while (1) {
// write the rest and be done
SYSERROR("writing new config");
fclose(f);
return -1;
}
fclose(f);
// success
return 0;
} else {
SYSERROR("writing new config");
fclose(f);
return -1;
}
p0 = p;
// now write the newpath or newname
SYSERROR("writing new name or path in new config");
fclose(f);
return -1;
}
}
}
}
{
int i;
int ret;
for (i=0; i<NUM_LXC_HOOKS; i++) {
char tmppath[MAXPATHLEN];
if (!fname) // relative path - we don't support, but maybe we should
return 0;
// copy the script, and change the entry in confile
return -1;
if (ret < 0)
return -1;
ERROR("out of memory copying hook path");
return -1;
}
}
}
c->save_config(c, NULL);
return 0;
}
static void new_hwaddr(char *hwaddr)
{
if (f) {
unsigned int seed;
if (ret != 1)
fclose(f);
} else
}
static void network_new_hwaddrs(struct lxc_container *c)
{
if (n->hwaddr)
new_hwaddr(n->hwaddr);
}
}
{
char newpath[MAXPATHLEN];
int ret;
if (!oldpath)
return 0;
if (!p)
return -1;
c->config_path, c->name, p);
return -1;
}
if (file_exists(newpath)) {
return -1;
}
return -1;
}
ERROR("error: allocating pathname");
return -1;
}
return 0;
}
{
if (!bdev) {
ERROR("error copying storage");
return -1;
}
return -1;
// here we could also update all lxc.mount.entries or even
// items in the lxc.mount fstab list. As discussed on m-l,
// we could do either any source paths starting with the
// fs or a bind mount.
return 0;
}
{
int ret = -1;
char path[MAXPATHLEN];
/* update hostname in rootfs */
/* we're going to mount, so run in a clean namespace to simplify cleanup */
if (pid < 0)
return -1;
if (pid > 0)
return wait_for_pid(pid);
if (unshare(CLONE_NEWNS) < 0) {
ERROR("error unsharing mounts");
exit(1);
}
if (!bdev)
exit(1);
exit(1);
/* Start of environment variable setup for hooks */
SYSERROR("failed to set environment variable for container name");
}
SYSERROR("failed to set environment variable for config path");
}
SYSERROR("failed to set environment variable for rootfs mount");
}
SYSERROR("failed to set environment variable for rootfs mount");
}
exit(1);
}
}
if (!(flags & LXC_CLONE_KEEPNAME)) {
exit(1);
exit(0);
}
exit(1);
exit(1);
}
exit(0);
}
/*
* We want to support:
sudo lxc-clone -o o1 -n n1 -s -L|-fssize fssize -v|--vgname vgname \
-p|--lvprefix lvprefix -t|--fstype fstype -B backingstore
-s [ implies overlayfs]
-s -B overlayfs
-s -B aufs
only rootfs gets converted (copied/snapshotted) on clone.
*/
static int create_file_dirname(char *path)
{
int ret;
if (!p)
return -1;
*p = '\0';
*p = '/';
return ret;
}
char **hookargs)
{
char newpath[MAXPATHLEN];
int ret;
const char *n, *l;
if (!c || !c->is_defined(c))
return NULL;
if (container_mem_lock(c))
return NULL;
if (!is_stopped(c)) {
goto out;
}
// Make sure the container doesn't yet exist.
SYSERROR("clone: failed making config pathname");
goto out;
}
if (file_exists(newpath)) {
goto out;
}
if (create_file_dirname(newpath) < 0) {
goto out;
}
// copy the configuration, tweak it as needed,
if (!fout) {
goto out;
}
if (update_name_and_paths(newpath, c, n, l) < 0) {
ERROR("Error updating name in cloned config");
goto out;
}
goto out;
}
c2 = lxc_container_new(n, l);
if (!c2) {
ERROR("clone: failed to create new container (%s %s)", n, l);
goto out;
}
// copy hooks if requested
if (flags & LXC_CLONE_COPYHOOKS) {
if (ret < 0) {
ERROR("error copying hooks");
goto out;
}
}
if (copy_fstab(c, c2) < 0) {
ERROR("error copying fstab");
goto out;
}
// update macaddrs
if (!(flags & LXC_CLONE_KEEPMACADDR))
if (ret < 0)
goto out;
goto out;
goto out;
// TODO: update c's lxc.snapshot = count
return c2;
out:
if (c2) {
}
return NULL;
}
{
struct lxc_container *c;
c = malloc(sizeof(*c));
if (!c) {
return NULL;
}
memset(c, 0, sizeof(*c));
if (configpath)
else
if (!c->config_path) {
goto err;
}
if (!c->name) {
goto err;
}
c->numthreads = 1;
goto err;
}
goto err;
}
if (!set_config_filename(c)) {
goto err;
}
if (file_exists(c->configfile))
lxcapi_load_config(c, NULL);
if (ongoing_create(c) == 2) {
lxcapi_destroy(c);
lxc_conf_free(c->lxc_conf);
}
// assign the member functions
c->is_defined = lxcapi_is_defined;
c->state = lxcapi_state;
c->is_running = lxcapi_is_running;
c->freeze = lxcapi_freeze;
c->unfreeze = lxcapi_unfreeze;
c->console = lxcapi_console;
c->init_pid = lxcapi_init_pid;
c->start = lxcapi_start;
c->startl = lxcapi_startl;
c->stop = lxcapi_stop;
c->wait = lxcapi_wait;
c->destroy = lxcapi_destroy;
c->get_keys = lxcapi_get_keys;
c->create = lxcapi_create;
c->createl = lxcapi_createl;
c->shutdown = lxcapi_shutdown;
c->reboot = lxcapi_reboot;
c->clone = lxcapi_clone;
c->get_ips = lxcapi_get_ips;
/* we'll allow the caller to update these later */
goto err;
}
return c;
err:
return NULL;
}
int lxc_get_wait_states(const char **states)
{
int i;
if (states)
for (i=0; i<MAX_STATE; i++)
states[i] = lxc_state2str(i);
return MAX_STATE;
}