]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
Add LUA api get_ips(), get_interfaces(), rename() functions
authorAndrey Jr. Melnikov <temnota.am@gmail.com>
Thu, 10 Dec 2015 18:08:11 +0000 (13:08 -0500)
committerStéphane Graber <stgraber@ubuntu.com>
Thu, 10 Dec 2015 18:08:11 +0000 (13:08 -0500)
Signed-off-by: Andrey Jr. Melnikov <temnota.am@gmail.com>
Acked-by: Stéphane Graber <stgraber@ubuntu.com>
src/lua-lxc/core.c
src/lua-lxc/lxc.lua

index 34180a77523c95bb8ca4593c84c07920ffba739f..3fb915ba4eb850c7de78d09c5c2c0e31e1a6885f 100644 (file)
@@ -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}
 };
 
index 122a4823b29e1ac32254eff9c4da551ca71874af..3d1de1014325809cd4df40740bc67bd60b1c290b 100755 (executable)
@@ -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)