From 44a80d675ffb81ebb1a66a62c162e93a4c5882a0 Mon Sep 17 00:00:00 2001 From: Natanael Copa Date: Thu, 5 Sep 2013 08:45:33 +0200 Subject: [PATCH] lua: prepare for Lua 5.2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adjust code for Lua 5.2 and keep compatibility with Lua 5.1. Signed-off-by: Natanael Copa Acked-by: Dwight Engen Acked-by: Stéphane Graber --- src/lua-lxc/core.c | 10 ++++++++-- src/lua-lxc/lxc.lua | 35 +++++++++++++++++++++-------------- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/lua-lxc/core.c b/src/lua-lxc/core.c index 9ccbab44a..d40470700 100644 --- a/src/lua-lxc/core.c +++ b/src/lua-lxc/core.c @@ -25,9 +25,15 @@ #define _GNU_SOURCE #include #include +#include #include #include +#if LUA_VERSION_NUM < 502 +#define luaL_newlib(L,l) (lua_newtable(L), luaL_register(L,NULL,l)) +#define luaL_setfuncs(L,l,n) (assert(n==0), luaL_register(L,NULL,l)) +#endif + #ifdef NO_CHECK_UDATA #define checkudata(L,i,tname) lua_touserdata(L, i) #else @@ -389,7 +395,7 @@ static int lxc_lib_uninit(lua_State *L) { LUALIB_API int luaopen_lxc_core(lua_State *L) { /* this is where we would initialize liblxc.so if we needed to */ - luaL_register(L, "lxc", lxc_lib_methods); + luaL_newlib(L, lxc_lib_methods); lua_newuserdata(L, 0); lua_newtable(L); /* metatable */ @@ -401,12 +407,12 @@ LUALIB_API int luaopen_lxc_core(lua_State *L) { lua_rawset(L, -3); luaL_newmetatable(L, CONTAINER_TYPENAME); + luaL_setfuncs(L, lxc_container_methods, 0); lua_pushvalue(L, -1); /* push metatable */ lua_pushstring(L, "__gc"); lua_pushcfunction(L, container_gc); lua_settable(L, -3); lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */ - luaL_register(L, NULL, lxc_container_methods); lua_pop(L, 1); return 1; } diff --git a/src/lua-lxc/lxc.lua b/src/lua-lxc/lxc.lua index 05501ecc8..ae47c0dd6 100755 --- a/src/lua-lxc/lxc.lua +++ b/src/lua-lxc/lxc.lua @@ -32,6 +32,11 @@ local lxc_path local cgroup_path local log_level = 3 +-- lua 5.1 compat +if table.unpack == nil then + table.unpack = unpack +end + -- the following two functions can be useful for debugging function printf(...) local function wrapper(...) io.write(string.format(...)) end @@ -286,7 +291,7 @@ function container:stat_get_ints(controller, item, coords) table.insert(result, val) end end - return unpack(result) + return table.unpack(result) end -- read an integer from a cgroup file @@ -324,17 +329,6 @@ function container:stat_match_get_int(controller, item, match, column) return val end -function stats_clear(stat) - stat.mem_used = 0 - stat.mem_limit = 0 - stat.memsw_used = 0 - stat.memsw_limit = 0 - stat.cpu_use_nanos = 0 - stat.cpu_use_user = 0 - stat.cpu_use_sys = 0 - stat.blkio = 0 -end - function container:stats_get(total) local stat = {} stat.mem_used = self:stat_get_int("memory", "memory.usage_in_bytes") @@ -359,10 +353,21 @@ function container:stats_get(total) return stat end +local M = { container = container } +function M.stats_clear(stat) + stat.mem_used = 0 + stat.mem_limit = 0 + stat.memsw_used = 0 + stat.memsw_limit = 0 + stat.cpu_use_nanos = 0 + stat.cpu_use_user = 0 + stat.cpu_use_sys = 0 + stat.blkio = 0 +end -- return configured containers found in LXC_PATH directory -function containers_configured(names_only) +function M.containers_configured(names_only) local containers = {} for dir in lfs.dir(lxc_path) do @@ -390,7 +395,7 @@ function containers_configured(names_only) end -- return running containers found in cgroup fs -function containers_running(names_only) +function M.containers_running(names_only) local containers = {} local attr @@ -426,3 +431,5 @@ end lxc_path = core.default_config_path_get() cgroup_path = cgroup_path_get() + +return M