lxccontainer.c revision 67702c2129c462b5e8124020a496fbf6b7ae5540
/* 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 <assert.h>
#include <stdarg.h>
#include <pthread.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <dirent.h>
#include <sched.h>
#include <libgen.h>
#include <stdint.h>
#include <grp.h>
#include <lxc/lxccontainer.h>
#include "config.h"
#include "lxc.h"
#include "state.h"
#include "conf.h"
#include "confile.h"
#include "console.h"
#include "cgroup.h"
#include "commands.h"
#include "log.h"
#include "bdev.h"
#include "utils.h"
#include "attach.h"
#include "monitor.h"
#include "namespace.h"
#include "lxclock.h"
#include "sync.h"
#if HAVE_IFADDRS_H
#include <ifaddrs.h>
#else
#include <../include/ifaddrs.h>
#endif
#define MAX_BUFFER 4096
#define NOT_SUPPORTED_ERROR "the requested function %s is not currently supported with unprivileged containers"
/* Define faccessat() if missing from the C library */
#ifndef HAVE_FACCESSAT
{
#ifdef __NR_faccessat
#else
return -1;
#endif
}
#endif
{
/* $lxcpath + '/' + $cname + '/config' + \0 */
return false;
return file_exists(fname);
}
/*
* 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.
*/
static int ongoing_create(struct lxc_container *c)
{
ERROR("Error writing partial pathname");
return -1;
}
if (!file_exists(path))
return 0;
if (fd < 0) {
// give benefit of the doubt
SYSERROR("Error opening partial file");
return 0;
}
// create is still ongoing
return 1;
}
// create completed but partial is still there.
return 2;
}
static int create_partial(struct lxc_container *c)
{
// $lxcpath + '/' + $name + '/partial' + \0
ERROR("Error writing partial pathname");
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;
}
}
/* 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();
if (!c->lxc_conf)
return false;
return false;
return true;
}
{
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;
}
{
if (!c || !c->lxc_conf)
return false;
if (container_mem_lock(c)) {
ERROR("Error getting mem lock");
return false;
}
/* daemonize implies close_all_fds so set it */
if (state == 1)
return true;
}
{
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;
}
{
/* 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");
}
static bool am_single_threaded(void)
{
int count=0;
if (!dir) {
return false;
}
if (!direntp)
break;
continue;
continue;
if (++count > 1)
break;
}
return count == 1;
}
/*
* 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;
bool daemonize = false;
char *default_args[] = {
NULL,
};
/* 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 (pid < 0)
return false;
if (pid != 0) {
/* Set to NULL because we don't want father unlink
* the PID file, child will do the free and unlink.
*/
return wait_on_daemonized_start(c, pid);
}
/* 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();
} else {
if (!am_single_threaded()) {
ERROR("Cannot start non-daemonized container when threaded");
return false;
}
}
/* We need to write PID file after daeminize, so we always
* write the right PID.
*/
if (c->pidfile) {
SYSERROR("Failed to create pidfile '%s' for '%s'",
return false;
}
return false;
}
}
INFO("container requested reboot");
goto reboot;
}
if (c->pidfile) {
}
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;
}
{
if (ret) {
ret = 0;
else {
return -1;
}
}
ERROR("Failed to chown container dir");
ret = -1;
}
return ret;
}
/*
* 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;
}
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;
int ret;
} else {
const char *lxcpath = lxcapi_get_config_path(c);
}
return NULL;
if (!bdev) {
return NULL;
}
/* if we are not root, chown the rootfs dir to root in the
* target uidmap */
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", 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");
return false;
}
if (pid == 0) { // child
int i;
char **newargv;
if (quiet) {
close(0);
close(1);
close(2);
}
/*
* for an overlay 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);
}
if (geteuid() == 0) {
if (unshare(CLONE_NEWNS) < 0) {
ERROR("error unsharing mounts");
exit(1);
}
if (detect_shared_rootfs()) {
SYSERROR("Failed to make / rslave to run template");
ERROR("Continuing...");
}
}
}
if (geteuid() != 0) {
ERROR("non-root users can only create btrfs and directory-backed containers");
exit(1);
}
ERROR("Error mounting rootfs");
exit(1);
}
} else { // TODO come up with a better way here!
}
/*
* 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);
/*
* If we're running the template in a mapped userns, then
* we prepend the template command with:
* lxc-usernsexec <-m map1> ... <-m mapn> --
* and we append "--mapped-uid x", where x is the mapped uid
* for our geteuid()
*/
int n2args = 1;
char txtuid[20];
char txtgid[20];
if (!n2) {
SYSERROR("out of memory");
exit(1);
}
tpath = "lxc-usernsexec";
n2[0] = "lxc-usernsexec";
n2args += 2;
if (!n2)
exit(1);
exit(1);
exit(1);
}
if (!n2)
exit(1);
if (hostid_mapped < 0) {
if (hostid_mapped < 0) {
ERROR("Could not find free uid to map");
exit(1);
}
SYSERROR("out of memory");
exit(1);
}
hostid_mapped, geteuid());
ERROR("string too long");
exit(1);
}
}
if (!n2)
exit(1);
if (hostgid_mapped < 0) {
if (hostgid_mapped < 0) {
ERROR("Could not find free uid to map");
exit(1);
}
SYSERROR("out of memory");
exit(1);
}
hostgid_mapped, getegid());
ERROR("string too long");
exit(1);
}
}
for (i = 0; i < nargs; i++)
// Finally add "--mapped-uid $uid" to tell template what to chown
// cached images to
n2args += 4;
if (!n2) {
SYSERROR("out of memory");
exit(1);
}
// note n2[n2args-1] is NULL
}
/* 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
if (f == NULL)
return false;
goto out_error;
goto out_error;
goto out_error;
goto out_error;
goto out_free_contents;
f = NULL;
if (ret < 0)
goto out_free_contents;
#if HAVE_LIBGNUTLS
tpath = get_template_path(t);
if (!tpath) {
ERROR("bad template: %s", t);
goto out_free_contents;
}
if (ret < 0) {
goto out_free_contents;
}
#endif
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
fprintf(f, "# For additional config options, please look at lxc.container.conf(5)\n");
SYSERROR("Writing original contents");
fclose(f);
return false;
}
ret = 0;
if (f) {
int newret;
if (ret == 0)
}
if (ret < 0) {
SYSERROR("Error prepending header");
return false;
}
return true;
}
static void lxcapi_clear_config(struct lxc_container *c)
{
if (c) {
if (c->lxc_conf) {
lxc_conf_free(c->lxc_conf);
}
}
}
static bool lxcapi_destroy(struct lxc_container *c);
static bool container_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 ret = false;
int partial_fd;
if (!c)
return false;
if (t) {
tpath = get_template_path(t);
if (!tpath) {
ERROR("bad template: %s", t);
goto out;
}
}
/*
* If a template is passed in, and the rootfs already is defined in
* the container config and exists, then * caller is trying to create
* an existing container. Return an error, but do NOT delete the
* container.
*/
goto free_tpath;
}
if (!c->lxc_conf) {
ERROR("Error loading default configuration file %s", lxc_global_config_value("lxc.default_config"));
goto free_tpath;
}
}
if (!create_container_dir(c))
goto free_tpath;
/*
* either template or rootfs.path should be set.
* if both template and rootfs.path are set, template is setup as rootfs.path.
* container is already created if we have a config and rootfs.path is accessible
*/
/* no template passed in and rootfs does not exist: error */
goto out;
/* rootfs passed into configuration, but does not exist: error */
goto out;
/* Rootfs already existed, user just wanted to save the
* loaded configuration */
ret = true;
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");
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_unlock;
/* reload config to get the rootfs */
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 (t) {
ERROR("Error prepending header to configuration file");
goto out_unlock;
}
}
if (partial_fd >= 0)
remove_partial(c, partial_fd);
out:
if (!ret && c)
if (tpath)
return ret;
}
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;
int haltsignal = SIGPWR;
if (!c)
return false;
if (!c->is_running(c))
return true;
if (pid <= 0)
return true;
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;
}
{
else
}
{
int ret;
if (!c || !c->lxc_conf)
return false;
if (container_mem_lock(c))
return false;
if (!ret)
return ret == 0;
}
static inline bool enter_to_ns(struct lxc_container *c) {
char new_netns_path[MAXPATHLEN];
char new_userns_path[MAXPATHLEN];
if (!c->is_running(c))
goto out;
/* Switch to new userns */
if ((geteuid() != 0 || (c->lxc_conf && !lxc_list_empty(&c->lxc_conf->id_map))) && access("/proc/self/ns/user", F_OK) == 0) {
goto out;
if (userns < 0) {
goto out;
}
SYSERROR("failed to setns for CLONE_NEWUSER");
goto out;
}
}
/* Switch to new netns */
goto out;
if (netns < 0) {
goto out;
}
SYSERROR("failed to setns for CLONE_NEWNET");
goto out;
}
return true;
out:
return false;
}
// used by qsort and bsearch functions for comparing names
{
}
// used by qsort and bsearch functions for comparing container names
{
}
{
if (!newnames) {
ERROR("Out of memory");
return false;
}
return false;
// sort the arrray as we will use binary search on it
return true;
}
{
if (!newlist) {
ERROR("Out of memory");
return false;
}
// sort the arrray as we will use binary search on it
if (sort)
qsort(newlist, pos + 1, sizeof(struct lxc_container *), (int (*)(const void *,const void *))container_cmp);
return true;
}
{
return (char **)bsearch(&cname, *names, size, sizeof(char *), (int (*)(const void *, const void *))string_cmp);
}
return true;
return false;
}
{
return true;
}
return false;
}
static char** lxcapi_get_interfaces(struct lxc_container *c)
{
char **interfaces = NULL;
SYSERROR("pipe failed");
return NULL;
}
if (pid < 0) {
SYSERROR("failed to fork task to get interfaces information");
return NULL;
}
if (pid == 0) { // child
/* close the read-end of the pipe */
if (!enter_to_ns(c)) {
SYSERROR("failed to enter namespace");
goto out;
}
/* Grab the list of interfaces */
if (getifaddrs(&interfaceArray)) {
SYSERROR("failed to get interfaces list");
goto out;
}
/* Iterate through the interfaces */
if (nbytes < 0) {
ERROR("write failed");
goto out;
}
count++;
}
ret = 0;
out:
if (interfaceArray)
/* close the write-end of the pipe, thus sending EOF to the reader */
}
/* close the write-end of the pipe */
continue;
ERROR("PARENT: add_to_array failed");
count++;
}
if (wait_for_pid(pid) != 0) {
for(i=0;i<count;i++)
free(interfaces[i]);
interfaces = NULL;
}
/* close the read-end of the pipe */
/* Append NULL to the array */
if(interfaces)
return interfaces;
}
static char** lxcapi_get_ips(struct lxc_container *c, const char* interface, const char* family, int scope)
{
char address[INET6_ADDRSTRLEN];
SYSERROR("pipe failed");
return NULL;
}
if (pid < 0) {
SYSERROR("failed to fork task to get container ips");
return NULL;
}
if (pid == 0) { // child
void *tempAddrPtr = NULL;
/* close the read-end of the pipe */
if (!enter_to_ns(c)) {
SYSERROR("failed to enter namespace");
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;
if (nbytes < 0) {
ERROR("write failed");
goto out;
}
count++;
}
ret = 0;
out:
if(interfaceArray)
/* close the write-end of the pipe, thus sending EOF to the reader */
}
/* close the write-end of the pipe */
ERROR("PARENT: add_to_array failed");
count++;
}
if (wait_for_pid(pid) != 0) {
for(i=0;i<count;i++)
}
/* close the read-end of the pipe */
/* Append NULL to the array */
if(addresses)
return addresses;
}
{
int ret;
if (!c || !c->lxc_conf)
return -1;
if (container_mem_lock(c))
return -1;
return ret;
}
{
char *ret;
if (!c || !c->lxc_conf)
return NULL;
if (container_mem_lock(c))
return NULL;
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) {
ERROR("Error loading default configuration file %s while saving %s", lxc_global_config_value("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;
if (f) {
fclose(f);
if (ret != 1) {
goto out;
}
}
if (!f)
goto out;
if (fprintf(f, "%d\n", v) < 0) {
ERROR("Error writing new snapshots value");
fclose(f);
goto out;
}
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;
}
if (f == NULL)
return;
goto out;
}
ERROR("Unable to find dependent container %s:%s",
continue;
}
ERROR("Failed to increase numsnapshots for %s:%s",
}
out:
fclose(f);
}
static bool has_fs_snapshots(struct lxc_container *c)
{
char path[MAXPATHLEN];
int ret, v;
FILE *f;
bool bret = false;
c->name);
goto out;
if (!f)
goto out;
fclose(f);
if (ret != 1)
goto out;
bret = v != 0;
out:
return bret;
}
static bool has_snapshots(struct lxc_container *c)
{
char path[MAXPATHLEN];
int count=0;
if (!get_snappath_dir(c, path))
return false;
if (!dir)
return false;
if (!direntp)
break;
continue;
continue;
count++;
break;
}
return count > 0;
}
static int lxc_rmdir_onedev_wrapper(void *data)
{
}
{
struct bdev *r;
int ret = 0;
if (!r)
return -1;
ret = -1;
bdev_put(r);
return ret;
}
static int bdev_destroy_wrapper(void *data)
{
if (setgid(0) < 0) {
ERROR("Failed to setgid to 0");
return -1;
}
WARN("Failed to clear groups");
if (setuid(0) < 0) {
ERROR("Failed to setuid to 0");
return -1;
}
return do_bdev_destroy(conf);
}
static bool container_destroy(struct lxc_container *c)
{
bool bret = false;
int ret;
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 (am_unpriv())
else
if (ret < 0) {
goto out;
}
}
mod_all_rdeps(c, false);
const char *p1 = lxcapi_get_config_path(c);
if (am_unpriv())
else
if (ret < 0) {
goto out;
}
bret = true;
out:
return bret;
}
static bool lxcapi_destroy(struct lxc_container *c)
{
if (!c || !lxcapi_is_defined(c))
return false;
if (has_snapshots(c)) {
return false;
}
if (has_fs_snapshots(c)) {
return false;
}
return container_destroy(c);
}
static bool lxcapi_snapshot_destroy_all(struct lxc_container *c);
static bool lxcapi_destroy_with_snapshots(struct lxc_container *c)
{
if (!c || !lxcapi_is_defined(c))
return false;
if (!lxcapi_snapshot_destroy_all(c)) {
ERROR("Error deleting all snapshots");
return false;
}
return lxcapi_destroy(c);
}
{
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;
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)
return -1;
if (is_stopped(c))
return -1;
if (container_disk_lock(c))
return -1;
return ret;
}
const char *lxc_get_global_config_item(const char *key)
{
return lxc_global_config_value(key);
}
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;
}
{
char *cpath;
return -1;
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;
// this hook is public - ignore
continue;
}
// 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;
}
}
}
ERROR("Error saving new hooks in clone");
return -1;
}
c->save_config(c, NULL);
return 0;
}
{
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;
}
ERROR("error saving new lxctab");
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;
if (!f)
return false;
bret = true;
// if anything goes wrong, just return an error
bret = false;
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;
}
// We will simply append a new lxc.rootfs entry to the unexpanded config
ERROR("Error saving new rootfs to cloend config");
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 clone_update_data {
struct lxc_container *c0;
struct lxc_container *c1;
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 (setgid(0) < 0) {
ERROR("Failed to setgid to 0");
return -1;
}
if (setuid(0) < 0) {
ERROR("Failed to setuid to 0");
return -1;
}
WARN("Failed to clear groups");
if (unshare(CLONE_NEWNS) < 0)
return -1;
if (!bdev)
return -1;
if (unshare(CLONE_NEWNS) < 0) {
ERROR("error unsharing mounts");
return -1;
}
if (detect_shared_rootfs()) {
SYSERROR("Failed to make / rslave");
ERROR("Continuing...");
}
}
return -1;
}
} else { // TODO come up with a better way
}
/* 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");
}
return -1;
}
}
if (!(flags & LXC_CLONE_KEEPNAME)) {
return -1;
if (!file_exists(path))
return 0;
return 0;
}
return -1;
}
return -1;
}
else
return 0;
}
static int clone_update_rootfs_wrapper(void *data)
{
return clone_update_rootfs(arg);
}
/*
* 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.
*/
{
int ret = -1;
if (!p)
return -1;
*p = '\0';
*p = '/';
return ret;
}
char **hookargs)
{
char newpath[MAXPATHLEN];
int ret, storage_copied = 0;
struct clone_update_data data;
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.
if (!newname)
if (!lxcpath)
lxcpath = c->get_config_path(c);
SYSERROR("clone: failed making config pathname");
goto out;
}
if (file_exists(newpath)) {
goto out;
}
goto out;
}
// copy the configuration, tweak it as needed,
}
if (!fout) {
goto out;
}
goto out;
}
if (am_unpriv()) {
goto out;
}
}
if (!c2) {
lxcpath);
goto out;
}
if (ret < 0)
goto out;
// update utsname
ERROR("Error setting new hostname");
goto out;
}
// copy hooks
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)) {
ERROR("Error updating mac addresses");
goto out;
}
}
// We've now successfully created c2's storage, so clear it out if we
// fail after this
storage_copied = 1;
goto out;
SYSERROR("fork");
goto out;
}
if (pid > 0) {
if (ret)
goto out;
return c2;
}
if (am_unpriv())
&data);
else
if (ret < 0)
exit(1);
exit(0);
out:
if (c2) {
if (!storage_copied)
}
return NULL;
}
{
struct lxc_container *newc;
return false;
if (has_fs_snapshots(c) || has_snapshots(c)) {
ERROR("Renaming a container with snapshots is not supported");
return false;
}
if (!bdev) {
ERROR("Failed to find original backing store type");
return false;
}
if (!newc) {
return false;
}
if (!container_destroy(c)) {
return false;
}
return true;
}
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++;
}
}
{
int ret;
/*
* If the old style snapshot path exists, use it
*/
return false;
if (dir_exists(snappath)) {
return false;
return true;
}
/*
* Use the new style path
*/
return false;
return true;
}
{
struct lxc_container *c2;
if (!c || !lxcapi_is_defined(c))
return -1;
if (!bdev_can_backup(c->lxc_conf)) {
ERROR("Your container must use another backing store type.");
return -1;
}
if (!get_snappath_dir(c, snappath))
return -1;
return -1;
}
return -1;
/*
* We pass LXC_CLONE_SNAPSHOT to make sure that a rdepends file entry is
* created in the original container
*/
ERROR("Snapshot of directory-backed container requested.");
ERROR("Making a copy-clone. If you do want snapshots, then");
ERROR("please create an aufs or overlayfs clone first, snapshot that");
ERROR("and keep the original container pristine.");
}
if (!c2) {
return -1;
}
// Now write down the creation time
char buffer[25];
FILE *f;
if (!f) {
return -1;
}
SYSERROR("Writing timestamp");
fclose(f);
return -1;
}
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;
if (!fin)
return NULL;
if (len > 0) {
if (s) {
s[len] = '\0';
SYSERROR("reading timestamp");
free(s);
s = NULL;
}
}
}
return s;
}
{
if (!c || !lxcapi_is_defined(c))
return -1;
if (!get_snappath_dir(c, snappath)) {
ERROR("path name too long");
return -1;
}
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++;
}
WARN("failed to close directory");
return count;
if (snaps) {
int i;
for (i=0; i<count; i++)
lxcsnap_free(&snaps[i]);
}
WARN("failed to close directory");
return -1;
}
static bool lxcapi_snapshot_restore(struct lxc_container *c, const char *snapname, const char *newname)
{
char clonelxcpath[MAXPATHLEN];
int flags = 0;
bool b = false;
if (!c || !c->name || !c->config_path)
return false;
if (has_fs_snapshots(c)) {
ERROR("container rootfs has dependent snapshots");
return false;
}
if (!bdev) {
ERROR("Failed to find original backing store type");
return false;
}
if (!newname)
if (!get_snappath_dir(c, clonelxcpath)) {
return false;
}
// how should we lock this?
return false;
}
if (!container_destroy(c)) {
return false;
}
}
b = true;
if (rest)
return b;
}
{
bool bret = false;
if (!snap) {
goto err;
}
if (!lxcapi_destroy(snap)) {
goto err;
}
bret = true;
err:
if (snap)
return bret;
}
static bool remove_all_snapshots(const char *path)
{
bool bret = true;
if (!dir) {
return false;
}
if (!direntp)
break;
continue;
continue;
bret = false;
continue;
}
}
return bret;
}
{
char clonelxcpath[MAXPATHLEN];
return false;
if (!get_snappath_dir(c, clonelxcpath))
return false;
}
static bool lxcapi_snapshot_destroy_all(struct lxc_container *c)
{
char clonelxcpath[MAXPATHLEN];
if (!c || !c->name || !c->config_path)
return false;
if (!get_snappath_dir(c, clonelxcpath))
return false;
return remove_all_snapshots(clonelxcpath);
}
static bool lxcapi_may_control(struct lxc_container *c)
{
}
{
char chrootpath[MAXPATHLEN];
char *directory_path = NULL;
int ret;
SYSERROR("failed to fork a child helper");
return false;
}
if (pid) {
if (wait_for_pid(pid) != 0) {
ERROR("Failed to create note in guest");
return false;
}
return true;
}
/* prepare the path */
return false;
if (chroot(chrootpath) < 0)
exit(1);
if (chdir("/") < 0)
exit(1);
/* remove path if it exists */
ERROR("unlink failed");
exit(1);
}
}
if (!add)
exit(0);
/* create any missing directories */
ERROR("failed to create directory");
exit(1);
}
/* create the device node */
ERROR("mknod failed");
exit(1);
}
exit(0);
}
static bool add_remove_device_node(struct lxc_container *c, const char *src_path, const char *dest_path, bool add)
{
int ret;
char value[MAX_BUFFER];
const char *p;
/* make sure container is running */
if (!c->is_running(c)) {
ERROR("container is not running");
return false;
}
/* use src_path if dest_path is NULL otherwise use dest_path */
/* make sure we can access p */
return false;
/* continue if path is character device or block device */
else
return false;
/* check snprintf return code */
return false;
return false;
if (add) {
ERROR("set_cgroup_item failed while adding the device node");
return false;
}
} else {
ERROR("set_cgroup_item failed while removing the device node");
return false;
}
}
return true;
}
static bool lxcapi_add_device_node(struct lxc_container *c, const char *src_path, const char *dest_path)
{
if (am_unpriv()) {
return false;
}
}
static bool lxcapi_remove_device_node(struct lxc_container *c, const char *src_path, const char *dest_path)
{
if (am_unpriv()) {
return false;
}
}
struct criu_opts {
/* The type of criu invocation, one of "dump" or "restore" */
char *action;
/* The directory to pass to criu */
char *directory;
/* The container to dump */
struct lxc_container *c;
/* Enable criu verbose mode? */
bool verbose;
/* dump: stop the container or not after dumping? */
bool stop;
/* restore: the file to write the init process' pid into */
char *pidfile;
};
/*
* @out must be 128 bytes long
*/
{
int ret;
FILE *f;
return -1;
}
if (!f)
return -1;
fclose(f);
if (ret <= 0)
return -1;
return 0;
}
{
/* The command line always looks like:
* criu $(action) --tcp-established --file-locks --link-remap --manage-cgroups \
* --action-script foo.sh -D $(directory) -o $(directory)/$(action).log
* +1 for final NULL */
/* -t pid */
static_args += 2;
/* --leave-running */
static_args++;
/* --root $(lxc_mount_point) --restore-detached --pidfile $foo */
static_args += 5;
} else {
return;
}
static_args++;
ERROR("logfile name too long\n");
return;
}
if (!argv)
return;
#define DECLARE_ARG(arg) \
do { \
goto err; \
} while (0)
ERROR("Couldn't find criu binary\n");
goto err;
}
DECLARE_ARG("--tcp-established");
DECLARE_ARG("--file-locks");
DECLARE_ARG("--link-remap");
DECLARE_ARG("--manage-cgroups");
DECLARE_ARG("--action-script");
DECLARE_ARG("-D");
DECLARE_ARG("-o");
DECLARE_ARG("-vvvvvv");
char pid[32];
goto err;
DECLARE_ARG("-t");
DECLARE_ARG("--leave-running");
int netnr = 0;
DECLARE_ARG("--root");
DECLARE_ARG("--restore-detached");
DECLARE_ARG("--pidfile");
void *m;
goto err;
goto err;
goto err;
/* final NULL and --veth-pair eth0:vethASDF */
if (!m)
goto err;
argv = m;
DECLARE_ARG("--veth-pair");
netnr++;
}
}
err:
for (i = 0; argv[i]; i++)
}
/* Check and make sure the container has a configuration that we know CRIU can
* dump. */
static bool criu_ok(struct lxc_container *c)
{
bool found_deny_rule = false;
if (geteuid()) {
ERROR("Must be root to checkpoint\n");
return false;
}
/* We only know how to restore containers with veth networks. */
ERROR("Found network that is not VETH or NONE\n");
return false;
}
}
// These requirements come from http://criu.org/LXC
return false;
}
return false;
}
found_deny_rule = true;
break;
}
}
if (!found_deny_rule) {
ERROR("couldn't find devices.deny = c 5:1 rwm");
return false;
}
return true;
}
{
bool error = false;
if (!criu_ok(c))
return false;
return false;
netnr = 0;
int pret;
error = true;
goto out;
}
if (!veth) {
/* criu_ok() checks that all interfaces are
* LXC_NET{VETH,NONE}, and VETHs should have this
* config */
break;
}
error = true;
goto out;
}
if (!bridge) {
error = true;
goto out;
}
error = true;
goto out;
}
error = true;
goto out;
}
if (n->name) {
error = true;
goto out;
}
} else
error = true;
out:
if (error)
return false;
}
if (pid < 0)
return false;
if (pid == 0) {
os.c = c;
/* exec_criu() returning is an error */
exit(1);
} else {
if (w == -1) {
perror("waitpid");
return false;
}
return !WEXITSTATUS(status);
}
return false;
}
}
{
struct lxc_rootfs *rootfs;
if (!criu_ok(c))
return false;
if (geteuid()) {
ERROR("Must be root to restore\n");
return false;
}
return false;
struct lxc_handler *handler;
if (!handler)
return false;
if (pid < 0)
return false;
if (pid == 0) {
if (unshare(CLONE_NEWNS))
return false;
/* CRIU needs the lxc root bind mounted so that it is the root of some
* mount. */
if (rootfs_is_blockdev(c->lxc_conf)) {
return false;
}
else {
return false;
return false;
}
}
os.c = c;
/* exec_criu() returning is an error */
exit(1);
} else {
int status;
if (w == -1) {
perror("waitpid");
return false;
}
if (WEXITSTATUS(status)) {
return false;
}
else {
bool error = false;
if (!f) {
perror("reading pidfile");
return false;
}
fclose(f);
if (ret != 1) {
ERROR("reading restore pid failed");
return false;
}
if (container_mem_lock(c))
return false;
error = true;
goto out_unlock;
}
error = true;
goto out_unlock;
}
error = true;
goto out_unlock;
}
netnr++;
}
if (error)
return false;
return false;
}
}
return false;
}
}
return true;
}
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;
if (!name)
return NULL;
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;
}
goto err;
if (ongoing_create(c) == 2) {
}
c->daemonize = true;
// 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->rename = lxcapi_rename;
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;
c->checkpoint = lxcapi_checkpoint;
c->restore = lxcapi_restore;
/* 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;
}
/*
* These next two could probably be done smarter with reusing a common function
* with different iterators and tests...
*/
{
struct lxc_container *c;
if (!lxcpath)
if (!dir) {
SYSERROR("opendir on lxcpath");
return -1;
}
if (cret)
if (names)
if (!direntp)
break;
continue;
continue;
continue;
if (names) {
goto free_bad;
}
cfound++;
if (!cret) {
nfound++;
continue;
}
if (!c) {
INFO("Container %s:%s has a config but could not be loaded",
if (names)
goto free_bad;
continue;
}
if (!lxcapi_is_defined(c)) {
INFO("Container %s:%s has a config but is not defined",
if (names)
goto free_bad;
continue;
}
goto free_bad;
}
nfound++;
}
return nfound;
for (i=0; i<cfound; i++)
}
for (i=0; i<nfound; i++)
lxc_container_put((*cret)[i]);
}
return -1;
}
struct lxc_container ***cret)
{
int lxcpath_len;
struct lxc_container *c;
bool is_hashed;
if (!lxcpath)
if (cret)
if (nret)
if (!f)
return -1;
if (!p)
continue;
p++;
if (*p != 0x40)
continue;
p++;
is_hashed = false;
p += lxcpath_len;
p += 4;
is_hashed = true;
} else {
continue;
}
while (*p == '/')
p++;
// Now p is the start of lxc_name
continue;
*p2 = '\0';
if (is_hashed) {
continue;
p = lxc_cmd_get_name(p);
}
continue;
goto free_cret_list;
ct_name_cnt++;
if (!cret)
continue;
c = lxc_container_new(p, lxcpath);
if (!c) {
INFO("Container %s:%s is running but could not be loaded",
lxcpath, p);
continue;
}
/*
* If this is an anonymous container, then is_defined *can*
* return false. So we don't do that check. Count on the
* fact that the command socket exists.
*/
goto free_cret_list;
}
cret_cnt++;
}
ret = ct_name_cnt;
if (nret)
else
goto free_ct_name;
goto out;
for (i = 0; i < cret_cnt; i++)
lxc_container_put((*cret)[i]);
}
if (ct_name) {
for (i = 0; i < ct_name_cnt; i++)
}
out:
if (line)
fclose(f);
return ret;
}
struct lxc_container ***cret)
{
char **active_name;
char **ct_name;
if (ct_cnt < 0)
return ct_cnt;
if (active_cnt < 0) {
ret = active_cnt;
goto free_ct_name;
}
for (i = 0; i < active_cnt; i++) {
ret = -1;
goto free_active_name;
}
ct_cnt++;
}
free(active_name[i]);
active_name[i] = NULL;
}
active_name = NULL;
active_cnt = 0;
struct lxc_container *c;
if (!c) {
continue;
}
ret = -1;
goto free_ct_list;
}
ct_list_cnt++;
}
if (cret)
if (nret)
else {
goto free_ct_name;
}
return ct_cnt;
for (i = 0; i < ct_list_cnt; i++) {
lxc_container_put(ct_list[i]);
}
if (ct_list)
for (i = 0; i < active_cnt; i++) {
if (active_name[i])
free(active_name[i]);
}
if (active_name)
for (i = 0; i < ct_cnt; i++) {
}
return ret;
}