lxccontainer.c revision b494d2ddf769220da1ef75fd24275ce68cdf297c
/* liblxcapi
*
* Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
* Copyright © 2012 Canonical Ltd.
*
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library 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
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _GNU_SOURCE
#include <stdarg.h>
#include <pthread.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <dirent.h>
#include "config.h"
#include "lxc.h"
#include "state.h"
#include <lxc/lxccontainer.h>
#include "conf.h"
#include "confile.h"
#include "console.h"
#include "cgroup.h"
#include "commands.h"
#include "version.h"
#include "log.h"
#include "bdev.h"
#include "utils.h"
#include "attach.h"
#include <lxc/namespace.h>
#include <sched.h>
#if HAVE_IFADDRS_H
#include <ifaddrs.h>
#else
#include <../include/ifaddrs.h>
#endif
#ifndef HAVE_GETLINE
#ifdef HAVE_FGETLN
#include <../include/getline.h>
#endif
#endif
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;
process_lock();
if (fd < 0) {
// give benefit of the doubt
SYSERROR("Error opening partial file");
return 0;
}
// create is still ongoing
process_lock();
return 1;
}
// create completed but partial is still there.
process_lock();
return 2;
}
int create_partial(struct lxc_container *c)
{
// $lxcpath + '/' + $name + '/partial' + \0
ERROR("Error writing partial pathname");
return -1;
}
process_lock();
SYSERROR("Erorr creating partial file");
return -1;
}
return -1;
}
return fd;
}
{
// $lxcpath + '/' + $name + '/partial' + \0
int ret;
process_lock();
ERROR("Error writing partial pathname");
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;
}
static bool lxcapi_want_close_all_fds(struct lxc_container *c)
{
if (!c || !c->lxc_conf)
return false;
if (container_mem_lock(c)) {
ERROR("Error getting mem lock");
return false;
}
return true;
}
{
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 (pid < 0) {
return false;
}
if (pid != 0)
return wait_on_daemonized_start(c);
process_unlock(); // we're no longer sharing
/* 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
*/
{
bool bret = false;
/* container exists */
if (!c)
return false;
if (!inargs) {
ERROR("Memory allocation error.");
goto out;
}
/* pass NULL if no arguments were supplied */
out:
if (inargs) {
char *arg;
}
return bret;
}
static bool lxcapi_stop(struct lxc_container *c)
{
int ret;
if (!c)
return false;
return ret == 0;
}
/*
* 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;
}
/*
* Given the '-t' template option to lxc-create, figure out what to
* do. If the template is a full executable path, use that. If it
* is something like 'sshd', then return $templatepath/lxc-sshd.
* On success return the template, on error return NULL.
*/
static char *get_template_path(const char *t)
{
char *tpath;
return tpath;
}
if (!tpath)
return NULL;
return NULL;
}
SYSERROR("bad template: %s\n", t);
return NULL;
}
return tpath;
}
static char *lxcbasename(char *path)
{
while (*p != '/' && p > path)
p--;
return p;
}
char *const argv[])
{
if (!tpath)
return true;
if (pid < 0) {
SYSERROR("failed to fork task for container creation template\n");
return false;
}
if (pid == 0) { // child
int i;
char **newargv;
process_unlock(); // we're no longer sharing
if (quiet) {
close(0);
close(1);
close(2);
}
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);
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) {
return false;
}
return true;
}
{
long flen;
char *contents;
FILE *f;
int ret = -1;
#if HAVE_LIBGNUTLS
int i;
unsigned char md_value[SHA_DIGEST_LENGTH];
char *tpath;
#endif
process_lock();
if (f == NULL)
return false;
goto out_error;
goto out_error;
goto out_error;
goto out_error;
goto out_free_contents;
process_lock();
f = NULL;
if (ret < 0)
goto out_free_contents;
#if HAVE_LIBGNUTLS
tpath = get_template_path(t);
if (!tpath) {
ERROR("bad template: %s\n", t);
goto out_free_contents;
}
if (ret < 0) {
goto out_free_contents;
}
#endif
process_lock();
if (f == NULL) {
SYSERROR("reopening config for writing");
return false;
}
fprintf(f, "# Template used to create this container: %s\n", t);
if (argv) {
fprintf(f, "# Parameters passed to the template:");
while (*argv) {
argv++;
}
fprintf(f, "\n");
}
#if HAVE_LIBGNUTLS
fprintf(f, "# Template script checksum (SHA-1): ");
for (i=0; i<SHA_DIGEST_LENGTH; i++)
fprintf(f, "\n");
#endif
SYSERROR("Writing original contents");
process_lock();
fclose(f);
return false;
}
ret = 0;
if (f) {
int newret;
process_lock();
if (ret == 0)
}
if (ret < 0) {
SYSERROR("Error prepending header");
return false;
}
return true;
}
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;
int partial_fd;
if (!c)
return false;
if (t) {
tpath = get_template_path(t);
if (!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
process_unlock(); // we're no longer sharing
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_unlock;
/* 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_unlock;
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);
ERROR("Error prepending header to configuration file");
goto out_unlock;
}
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;
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.
*/
if (!args) {
ERROR("Memory allocation error.");
goto out;
}
out:
return bret;
}
{
int ret;
if (!c || !c->lxc_conf)
return false;
if (container_mem_lock(c))
return false;
return ret == 0;
}
/* Switch back to original netns */
SYSERROR("failed to setns");
process_lock();
if (*new_netns >= 0)
if (*old_netns >= 0)
}
int ret = 0;
char new_netns_path[MAXPATHLEN];
if (!c->is_running(c))
goto out;
/* Save reference to old netns */
process_lock();
if (*old_netns < 0) {
goto out;
}
/* Switch to new netns */
goto out;
process_lock();
if (*new_netns < 0) {
goto out;
}
SYSERROR("failed to setns");
goto out;
}
return true;
out:
return false;
}
static char** lxcapi_get_interfaces(struct lxc_container *c)
{
int count = 0, i;
bool found = false;
goto out;
/* Grab the list of interfaces */
if (getifaddrs(&interfaceArray)) {
SYSERROR("failed to get interfaces list");
goto out;
}
/* Iterate through the interfaces */
/*
* WARNING: Following "for" loop does a linear search over the interfaces array
* For the containers with lots of interfaces this may be problematic.
* I'm not expecting this to be the common usage but if it turns out to be
* than using binary search or a hash table could be more elegant solution.
*/
for (i = 0; i < count; i++) {
found = true;
break;
}
}
if (!found) {
count += 1;
if (!temp) {
count -= 1;
goto out;
}
interfaces = temp;
}
found = false;
}
out:
if (interfaceArray)
/* Append NULL to the array */
return interfaces;
}
{
int count = 0;
void *tempAddrPtr = NULL;
goto out;
/* Grab the list of interfaces */
if (getifaddrs(&interfaceArray)) {
SYSERROR("failed to get interfaces list");
goto out;
}
/* Iterate through the interfaces */
continue;
continue;
}
else {
continue;
continue;
}
continue;
continue;
sizeof(addressOutputBuffer));
if (!address)
continue;
count += 1;
if (!temp) {
count--;
goto out;
}
}
out:
if(interfaceArray)
/* Append NULL to the array */
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;
}
{
char path[MAXPATHLEN];
int ret, v = 0;
FILE *f;
bool bret = false;
if (container_disk_lock(c))
return false;
c->name);
goto out;
process_lock();
if (f) {
process_lock();
fclose(f);
if (ret != 1) {
goto out;
}
}
process_lock();
if (!f)
goto out;
if (fprintf(f, "%d\n", v) < 0) {
ERROR("Error writing new snapshots value");
process_lock();
fclose(f);
goto out;
}
process_lock();
if (ret != 0) {
SYSERROR("Error writing to or closing snapshots file");
goto out;
}
bret = true;
out:
return bret;
}
static void strip_newline(char *p)
{
if (len < 1)
return;
}
{
struct lxc_container *p;
FILE *f;
int ret;
c->config_path, c->name);
ERROR("Path name too long");
return;
}
process_lock();
if (f == NULL)
return;
goto out;
}
ERROR("Unable to find dependent container %s:%s",
continue;
}
ERROR("Failed to increase numsnapshots for %s:%s",
}
out:
process_lock();
fclose(f);
}
static bool has_snapshots(struct lxc_container *c)
{
char path[MAXPATHLEN];
int ret, v;
FILE *f;
bool bret = false;
c->name);
goto out;
process_lock();
if (!f)
goto out;
process_lock();
fclose(f);
if (ret != 1)
goto out;
bret = v != 0;
out:
return bret;
}
// 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 (c->lxc_conf && has_snapshots(c)) {
goto out;
}
if (r) {
bdev_put(r);
goto out;
}
bdev_put(r);
}
mod_all_rdeps(c, false);
const char *p1 = lxcapi_get_config_path(c);
if (lxc_rmdir_onedev(path) < 0) {
goto out;
}
ret = true;
out:
return ret;
}
{
struct lxc_config_t *config;
if (!c->lxc_conf)
c->lxc_conf = lxc_conf_init();
if (!c->lxc_conf)
return false;
if (!config)
return false;
}
{
bool b = false;
if (!c)
return false;
if (container_mem_lock(c))
return false;
b = set_config_item_locked(c, key, v);
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;
}
process_lock();
if (in < 0) {
return -1;
}
process_lock();
if (out < 0) {
process_lock();
return -1;
}
while (1) {
if (len < 0) {
goto err;
}
if (len == 0)
break;
goto err;
}
}
process_lock();
if (ret) {
return -1;
}
return 0;
err:
process_lock();
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)
{
FILE *f;
process_lock();
if (f) {
unsigned int seed;
if (ret != 1)
process_lock();
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;
}
{
int ret;
WARN("Error copying reverse dependencies");
return;
}
c->name);
WARN("Error copying reverse dependencies");
return;
}
INFO("Error copying reverse dependencies");
return;
}
}
{
int ret;
char path[MAXPATHLEN];
FILE *f;
bool bret;
c->name);
return false;
process_lock();
if (!f)
return false;
bret = true;
// if anything goes wrong, just return an error
bret = false;
process_lock();
if (fclose(f) != 0)
bret = false;
return bret;
}
{
int need_rdep;
if (!bdev) {
ERROR("Error copying storage");
return -1;
}
ERROR("Out of memory while setting storage path");
return -1;
}
if (flags & LXC_CLONE_SNAPSHOT)
copy_rdepends(c, c0);
if (need_rdep) {
if (!add_rdepends(c, c0))
WARN("Error adding reverse dependency from %s to %s",
}
mod_all_rdeps(c, true);
return 0;
}
struct lxc_container *c, int flags,
char **hookargs)
{
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);
process_unlock(); // we're no longer sharing
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 source container name");
}
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);
if (!file_exists(path))
exit(0);
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, storage_copied = 0;
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;
}
goto out;
}
// copy the configuration, tweak it as needed,
process_lock();
if (!fout) {
goto out;
}
process_lock();
goto out;
}
c2 = lxc_container_new(n, l);
if (!c2) {
ERROR("clone: failed to create new container (%s %s)", n, l);
goto out;
}
// update utsname
ERROR("Error setting new hostname");
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;
// We've now successfully created c2's storage, so clear it out if we
// fail after this
storage_copied = 1;
goto out;
goto out;
// TODO: update c's lxc.snapshot = count
return c2;
out:
if (c2) {
if (!storage_copied)
}
return NULL;
}
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)
{
if (!c)
return -1;
}
static int lxcapi_attach_run_wait(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char * const argv[])
{
int r;
if (!c)
return -1;
if (r < 0) {
ERROR("ups");
return r;
}
return lxc_wait_for_pid_status(pid);
}
{
char *fname;
int i = 0, ret;
while (1) {
if (ret != 0)
return i;
i++;
}
}
{
struct lxc_container *c2;
return -1;
return -1;
}
return -1;
if (!c2) {
return -1;
}
// Now write down the creation time
char buffer[25];
FILE *f;
process_lock();
if (!f) {
return -1;
}
SYSERROR("Writing timestamp");
fclose(f);
return -1;
}
process_lock();
if (ret != 0) {
SYSERROR("Writing timestamp");
return -1;
}
if (commentfile) {
// $p / $name / comment \0
}
return i;
}
static void lxcsnap_free(struct lxc_snapshot *s)
{
if (s->name)
if (s->comment_pathname)
free(s->comment_pathname);
if (s->timestamp)
if (s->lxcpath)
}
{
if (s) {
free(s);
s = NULL;
}
}
return s;
}
{
return NULL;
process_lock();
if (!fin)
return NULL;
if (len > 0) {
if (s) {
s[len] = '\0';
SYSERROR("reading timestamp");
free(s);
s = NULL;
}
}
}
process_lock();
return s;
}
{
if (!c || !lxcapi_is_defined(c))
return -1;
// snappath is ${lxcpath}snaps/${lxcname}/
ERROR("path name too long");
return -1;
}
process_lock();
if (!dir) {
return 0;
}
if (!direntp)
break;
continue;
continue;
ERROR("pathname too long");
goto out_free;
}
if (!file_exists(path2))
continue;
if (!nsnaps) {
SYSERROR("Out of memory");
goto out_free;
}
goto out_free;
goto out_free;
}
count++;
}
process_lock();
WARN("failed to close directory");
return count;
if (snaps) {
int i;
for (i=0; i<count; i++)
lxcsnap_free(&snaps[i]);
}
return -1;
}
{
char clonelxcpath[MAXPATHLEN];
int ret;
bool b = false;
if (!c || !c->name || !c->config_path)
return false;
if (!bdev) {
ERROR("Failed to find original backing store type");
return false;
}
if (!newname)
if (!lxcapi_destroy(c)) {
return false;
}
}
return false;
}
// how should we lock this?
return false;
}
b = true;
if (rest)
return b;
}
static bool lxcapi_may_control(struct lxc_container *c)
{
}
static int lxcapi_attach_run_waitl(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char *arg, ...)
{
const char **argv;
int ret;
if (!c)
return -1;
if (!argv) {
ERROR("Memory allocation error.");
return -1;
}
return ret;
}
{
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;
c->attach = lxcapi_attach;
c->snapshot = lxcapi_snapshot;
/* 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;
}