core.c revision 540f932aeb28274e8e7ea1e8f3a8e5889b88e1d6
/*
* lua-lxc: lua bindings for lxc
*
* Copyright © 2012 Oracle.
*
* Authors:
* Dwight Engen <dwight.engen@oracle.com>
*
* 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 LUA_LIB
#define _GNU_SOURCE
#include <lua.h>
#include <lauxlib.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include <libgen.h>
#include <lxc/lxccontainer.h>
#include <lxc/commands.h>
#if LUA_VERSION_NUM < 502
#define luaL_checkunsigned(L,n) luaL_checknumber(L,n)
#endif
#ifdef NO_CHECK_UDATA
#else
#endif
#define lua_boxpointer(L,u) \
(*(void **) (lua_newuserdata(L, sizeof(void *))) = (u))
#define lua_unboxpointer(L,i,tname) \
(*(void **) (checkudata(L, i, tname)))
#define CONTAINER_TYPENAME "lxc.container"
static int container_new(lua_State *L)
{
struct lxc_container *c;
const char *configpath = NULL;
int argc = lua_gettop(L);
if (argc > 1)
if (c) {
lua_boxpointer(L, c);
lua_setmetatable(L, -2);
} else {
lua_pushnil(L);
}
return 1;
}
static int container_gc(lua_State *L)
{
/* XXX what to do if this fails? */
return 0;
}
static int container_config_file_name(lua_State *L)
{
char *config_file_name;
config_file_name = c->config_file_name(c);
return 1;
}
static int container_defined(lua_State *L)
{
lua_pushboolean(L, !!c->is_defined(c));
return 1;
}
static int container_name(lua_State *L)
{
lua_pushstring(L, c->name);
return 1;
}
static int container_create(lua_State *L)
{
int argc = lua_gettop(L);
char **argv;
int i;
for (i = 0; i < argc-2; i++)
return 1;
}
static int container_destroy(lua_State *L)
{
lua_pushboolean(L, !!c->destroy(c));
return 1;
}
/* container state */
static int container_start(lua_State *L)
{
int argc = lua_gettop(L);
int i,j;
int useinit = 0;
if (argc > 1) {
for (i = 0, j = 0; i < argc-1; i++) {
useinit = 1;
else
}
}
c->want_daemonize(c, true);
return 1;
}
static int container_stop(lua_State *L)
{
lua_pushboolean(L, !!c->stop(c));
return 1;
}
static int container_shutdown(lua_State *L)
{
return 1;
}
static int container_wait(lua_State *L)
{
return 1;
}
static int container_freeze(lua_State *L)
{
lua_pushboolean(L, !!c->freeze(c));
return 1;
}
static int container_unfreeze(lua_State *L)
{
lua_pushboolean(L, !!c->unfreeze(c));
return 1;
}
static int container_running(lua_State *L)
{
lua_pushboolean(L, !!c->is_running(c));
return 1;
}
static int container_state(lua_State *L)
{
lua_pushstring(L, c->state(c));
return 1;
}
static int container_init_pid(lua_State *L)
{
lua_pushinteger(L, c->init_pid(c));
return 1;
}
/* configuration file methods */
static int container_load_config(lua_State *L)
{
int arg_cnt = lua_gettop(L);
if (arg_cnt > 1)
return 1;
}
static int container_save_config(lua_State *L)
{
int arg_cnt = lua_gettop(L);
if (arg_cnt > 1)
return 1;
}
static int container_get_config_path(lua_State *L)
{
const char *config_path;
config_path = c->get_config_path(c);
return 1;
}
static int container_set_config_path(lua_State *L)
{
return 1;
}
static int container_clear_config_item(lua_State *L)
{
return 1;
}
static int container_get_cgroup_item(lua_State *L)
{
int len;
char *value;
if (len <= 0)
goto not_found;
goto not_found;
lua_pushstring(L, value);
return 1;
lua_pushnil(L);
return 1;
}
static int container_get_config_item(lua_State *L)
{
int len;
char *value;
if (len <= 0)
goto not_found;
goto not_found;
lua_pushstring(L, value);
return 1;
lua_pushnil(L);
return 1;
}
static int container_set_cgroup_item(lua_State *L)
{
return 1;
}
static int container_set_config_item(lua_State *L)
{
return 1;
}
static int container_get_keys(lua_State *L)
{
int len;
char *value;
int arg_cnt = lua_gettop(L);
if (arg_cnt > 1)
if (len <= 0)
goto not_found;
goto not_found;
lua_pushstring(L, value);
return 1;
lua_pushnil(L);
return 1;
}
static luaL_Reg lxc_container_methods[] =
{
{"create", container_create},
{"defined", container_defined},
{"destroy", container_destroy},
{"init_pid", container_init_pid},
{"name", container_name},
{"running", container_running},
{"state", container_state},
{"freeze", container_freeze},
{"unfreeze", container_unfreeze},
{"start", container_start},
{"stop", container_stop},
{"shutdown", container_shutdown},
{"wait", container_wait},
{"config_file_name", container_config_file_name},
{"load_config", container_load_config},
{"save_config", container_save_config},
{"get_cgroup_item", container_get_cgroup_item},
{"set_cgroup_item", container_set_cgroup_item},
{"get_config_path", container_get_config_path},
{"set_config_path", container_set_config_path},
{"get_config_item", container_get_config_item},
{"set_config_item", container_set_config_item},
{"clear_config_item", container_clear_config_item},
{"get_keys", container_get_keys},
};
static int lxc_version_get(lua_State *L) {
lua_pushstring(L, VERSION);
return 1;
}
static int lxc_default_config_path_get(lua_State *L) {
const char *lxcpath = lxc_get_default_config_path();
lua_pushstring(L, lxcpath);
return 1;
}
static int cmd_get_config_item(lua_State *L)
{
int arg_cnt = lua_gettop(L);
char *value;
if (arg_cnt > 2)
if (!value)
goto not_found;
lua_pushstring(L, value);
return 1;
lua_pushnil(L);
return 1;
}
/* utility functions */
static int lxc_util_usleep(lua_State *L) {
return 0;
}
static int lxc_util_dirname(lua_State *L) {
return 1;
}
static luaL_Reg lxc_lib_methods[] = {
{"version_get", lxc_version_get},
{"default_config_path_get", lxc_default_config_path_get},
{"cmd_get_config_item", cmd_get_config_item},
{"container_new", container_new},
{"usleep", lxc_util_usleep},
{"dirname", lxc_util_dirname},
};
static int lxc_lib_uninit(lua_State *L) {
(void) L;
/* this is where we would fini liblxc.so if we needed to */
return 0;
}
/* this is where we would initialize liblxc.so if we needed to */
lua_newuserdata(L, 0);
lua_newtable(L); /* metatable */
lua_pushvalue(L, -1);
lua_pushliteral(L, "__gc");
lua_rawset(L, -3);
lua_setmetatable(L, -3);
lua_rawset(L, -3);
luaL_setfuncs(L, lxc_container_methods, 0);
lua_pushstring(L, "__gc");
lua_settable(L, -3);
lua_pop(L, 1);
return 1;
}