Commit 9bfdc223 by Andrey Jr. Melnikov Committed by Stéphane Graber

Add LUA api get_ips(), get_interfaces(), rename() functions

parent 6a564066
......@@ -58,6 +58,9 @@
#define CONTAINER_TYPENAME "lxc.container"
/* Max Lua arguments for function */
#define MAXVARS 200
static int container_new(lua_State *L)
{
struct lxc_container *c;
......@@ -194,6 +197,20 @@ static int container_wait(lua_State *L)
return 1;
}
static int container_rename(lua_State *L)
{
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
const char *new_name;
int argc = lua_gettop(L);
if (argc > 1) {
new_name = luaL_checkstring(L, 2);
lua_pushboolean(L, !!c->rename(c, new_name));
} else
lua_pushnil(L);
return 1;
}
static int container_freeze(lua_State *L)
{
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
......@@ -407,6 +424,73 @@ static int container_attach(lua_State *L)
return 1;
}
static int container_get_interfaces(lua_State *L)
{
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
char **ifaces;
int i;
ifaces = c->get_interfaces(c);
if (!ifaces){
lua_pushnil(L);
return 1;
}
for (i = 0; ifaces[i]; i++);
/* protect LUA stack form overflow */
if (i > MAXVARS || !lua_checkstack(L, i)){
for (i = 0; ifaces[i]; i++)
free(ifaces[i]);
lua_pushnil(L);
return 1;
}
for (i = 0; ifaces[i]; i++){
lua_pushstring(L, ifaces[i]);
free(ifaces[i]);
}
return i;
}
static int container_get_ips(lua_State *L)
{
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
int argc = lua_gettop(L);
char **addresses;
char *iface = NULL, *family = NULL;
int i, scope = 0;
if (argc > 1)
iface = (char *)luaL_checkstring(L, 2);
if (argc > 2)
family = (char *)luaL_checkstring(L, 3);
if (argc > 3)
scope = luaL_checkinteger(L, 4);
addresses = c->get_ips(c, iface, family, scope);
if (!addresses){
lua_pushnil(L);
return 1;
}
for (i = 0; addresses[i]; i++);
/* protect LUA stack form overflow */
if (i > MAXVARS || !lua_checkstack(L, i)){
for (i = 0; addresses[i]; i++)
free(addresses[i]);
lua_pushnil(L);
return 1;
}
for (i = 0; addresses[i]; i++){
lua_pushstring(L, addresses[i]);
free(addresses[i]);
}
return i;
}
static luaL_Reg lxc_container_methods[] =
{
{"attach", container_attach},
......@@ -423,6 +507,7 @@ static luaL_Reg lxc_container_methods[] =
{"stop", container_stop},
{"shutdown", container_shutdown},
{"wait", container_wait},
{"rename", container_rename},
{"config_file_name", container_config_file_name},
{"load_config", container_load_config},
......@@ -435,6 +520,8 @@ static luaL_Reg lxc_container_methods[] =
{"set_config_item", container_set_config_item},
{"clear_config_item", container_clear_config_item},
{"get_keys", container_get_keys},
{"get_interfaces", container_get_interfaces},
{"get_ips", container_get_ips},
{NULL, NULL}
};
......
......@@ -151,6 +151,11 @@ function container:destroy()
return self.core:destroy()
end
-- return nil if name missing
function container:rename(name)
return self.core:rename(name)
end
function container:get_config_path()
return self.core:get_config_path()
end
......@@ -221,6 +226,16 @@ function container:get_keys(base)
return ktab
end
-- return nil or more args
function container:get_interfaces()
return self.core:get_interfaces()
end
-- return nil or more args
function container:get_ips(...)
return self.core:get_ips(...)
end
function container:load_config(alt_path)
if (alt_path) then
return self.core:load_config(alt_path)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment