From: Andrey Jr. Melnikov Date: Thu, 10 Dec 2015 18:08:11 +0000 (-0500) Subject: Add LUA api get_ips(), get_interfaces(), rename() functions X-Git-Tag: lxc-2.0.0.beta1~17 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9bfdc2235cd71004b68a010c3575d564efe48378;p=thirdparty%2Flxc.git Add LUA api get_ips(), get_interfaces(), rename() functions Signed-off-by: Andrey Jr. Melnikov Acked-by: Stéphane Graber --- diff --git a/src/lua-lxc/core.c b/src/lua-lxc/core.c index 34180a775..3fb915ba4 100644 --- a/src/lua-lxc/core.c +++ b/src/lua-lxc/core.c @@ -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} }; diff --git a/src/lua-lxc/lxc.lua b/src/lua-lxc/lxc.lua index 122a4823b..3d1de1014 100755 --- a/src/lua-lxc/lxc.lua +++ b/src/lua-lxc/lxc.lua @@ -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)