lxccontainer.c revision afeecbba0359d2b4404cdf896e6b6d0b5a8443b0
/* 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.
*/
#include "lxc.h"
#include "state.h"
#include "lxccontainer.h"
#include "conf.h"
#include "config.h"
#include "confile.h"
#include "cgroup.h"
#include "commands.h"
#include "log.h"
#include <unistd.h>
#include <errno.h>
/* LOCKING
* c->privlock protects the struct lxc_container from multiple threads.
* c->slock protects the on-disk container data
* 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 fork 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) {
}
if (c->privlock) {
sem_destroy(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);
}
int lxc_container_get(struct lxc_container *c)
{
if (!c)
return 0;
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;
return -1;
if (--c->numthreads < 1) {
return 1;
}
return 0;
}
static bool file_exists(char *f)
{
}
static bool lxcapi_is_defined(struct lxc_container *c)
{
bool ret = false;
int statret;
if (!c)
return false;
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)
{
const char *ret;
lxc_state_t s;
if (!c)
return NULL;
return NULL;
s = lxc_getstate(c->name);
ret = lxc_state2str(s);
return ret;
}
static bool is_stopped_nolock(struct lxc_container *c)
{
lxc_state_t s;
s = lxc_getstate(c->name);
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;
return false;
if (ret)
return false;
return true;
}
static bool lxcapi_unfreeze(struct lxc_container *c)
{
int ret;
if (!c)
return false;
return false;
if (ret)
return false;
return true;
}
{
if (!c)
return -1;
return -1;
return ret;
}
{
if (!c->lxc_conf)
c->lxc_conf = lxc_conf_init();
return true;
return false;
}
{
bool ret = false;
const char *fname;
if (!c)
return false;
fname = c->configfile;
if (alt_file)
if (!fname)
return false;
return false;
return ret;
}
static void lxcapi_want_daemonize(struct lxc_container *c)
{
if (!c)
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;
/* is this app meant to be run through lxcinit, as in lxc-execute? */
return false;
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);
/* 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();
}
if (clearenv()) {
SYSERROR("failed to clear environment");
/* don't error out though */
}
if (putenv("container=lxc")) {
if (daemonize) {
exit(1);
} else {
return false;
}
}
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;
}
/*
* backing stores not (yet) supported
* for ->create, argv contains the arguments to pass to the template,
* terminated by NULL. If no arguments, you can just pass NULL.
*/
{
bool bret = false;
char **newargv;
if (!c)
return false;
if (!tpath)
return false;
goto out;
if (!valid_template(tpath)) {
ERROR("bad template: %s\n", t);
goto out;
}
if (!create_container_dir(c))
goto out;
if (!c->save_config(c, NULL)) {
goto out;
}
/* we're going to fork. but since we'll wait for our child, we
don't need to lxc_container_get */
goto out;
}
if (pid < 0) {
SYSERROR("failed to fork task for container creation template\n");
goto out_unlock;
}
if (pid == 0) { // child
int i;
close(0);
close(1);
close(2);
/*
* create our new array, pre-pend the template name and
* base args
*/
if (argv)
if (!newargv)
exit(1);
newargv[0] = t;
if (!patharg)
exit(1);
exit(1);
if (!namearg)
exit(1);
exit(1);
/* add passed-in args */
if (argv)
for (i = 3; i < nargs; i++)
/* add trailing NULL */
nargs++;
if (!newargv)
exit(1);
/* execute */
exit(1);
}
if (ret == -1) {
goto again;
SYSERROR("waitpid failed");
goto out_unlock;
}
goto again;
// we could set an error code and string inside the
// container_struct here if we like
ERROR("container creation template exited abnormally\n");
goto out_unlock;
}
if (WEXITSTATUS(status) != 0) {
ERROR("container creation template for %s exited with %d\n",
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);
out:
if (tpath)
return bret;
}
{
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, 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;
}
out:
if (args)
return bret;
}
{
int ret;
if (!c || !c->lxc_conf)
return false;
return false;
}
return ret == 0;
}
{
int ret;
if (!c || !c->lxc_conf)
return -1;
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;
return -1;
int ret = -1;
return ret;
}
/* default config file - should probably come through autoconf */
#define LXC_DEFAULT_CONFIG "/etc/lxc/default.conf"
{
if (!alt_file)
alt_file = c->configfile;
if (!alt_file)
return false; // should we write to stdout if no file is specified?
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 (!fout)
return false;
return false;
}
return true;
}
static bool lxcapi_destroy(struct lxc_container *c)
{
if (!c)
return false;
if (pid < 0)
return false;
if (pid == 0) { // child
perror("execl");
exit(1);
}
if (ret == -1) {
goto again;
perror("waitpid");
return false;
}
goto again;
// we could set an error code and string inside the
// container_struct here if we like
return false;
}
return WEXITSTATUS(status) == 0;
}
{
int ret;
bool b = false;
struct lxc_config_t *config;
if (!c)
return false;
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;
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;
bool b = false;
if (!c)
return false;
return false;
if (is_stopped_nolock(c))
goto err;
if (!ret)
b = true;
err:
return b;
}
static int lxcapi_get_cgroup_item(struct lxc_container *c, const char *subsys, char *retv, int inlen)
{
int ret = -1;
if (!c || !c->lxc_conf)
return -1;
return -1;
if (is_stopped_nolock(c))
goto out;
out:
return ret;
}
{
struct lxc_container *c;
c = malloc(sizeof(*c));
if (!c) {
return NULL;
}
memset(c, 0, sizeof(*c));
if (configpath)
else
c->config_path = default_lxc_path();
if (!c->config_path) {
goto err;
}
if (!c->name) {
goto err;
}
c->numthreads = 1;
if (!c->slock) {
goto err;
}
if (!c->privlock) {
goto err;
}
if (!set_config_filename(c)) {
goto err;
}
if (file_exists(c->configfile))
lxcapi_load_config(c, NULL);
// 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->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;
/* 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;
}