lua: prepare for Lua 5.2

Adjust code for Lua 5.2 and keep compatibility with Lua 5.1.

Signed-off-by: Natanael Copa <ncopa@alpinelinux.org>
Acked-by: Dwight Engen <dwight.engen@oracle.com>
Acked-by: Stéphane Graber <stgraber@ubuntu.com>
This commit is contained in:
Natanael Copa 2013-09-05 08:45:33 +02:00 committed by Stéphane Graber
parent 2698b46924
commit 44a80d675f
2 changed files with 29 additions and 16 deletions

View File

@ -25,9 +25,15 @@
#define _GNU_SOURCE #define _GNU_SOURCE
#include <lua.h> #include <lua.h>
#include <lauxlib.h> #include <lauxlib.h>
#include <assert.h>
#include <string.h> #include <string.h>
#include <lxc/lxccontainer.h> #include <lxc/lxccontainer.h>
#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 #ifdef NO_CHECK_UDATA
#define checkudata(L,i,tname) lua_touserdata(L, i) #define checkudata(L,i,tname) lua_touserdata(L, i)
#else #else
@ -389,7 +395,7 @@ static int lxc_lib_uninit(lua_State *L) {
LUALIB_API int luaopen_lxc_core(lua_State *L) { LUALIB_API int luaopen_lxc_core(lua_State *L) {
/* this is where we would initialize liblxc.so if we needed to */ /* 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_newuserdata(L, 0);
lua_newtable(L); /* metatable */ lua_newtable(L); /* metatable */
@ -401,12 +407,12 @@ LUALIB_API int luaopen_lxc_core(lua_State *L) {
lua_rawset(L, -3); lua_rawset(L, -3);
luaL_newmetatable(L, CONTAINER_TYPENAME); luaL_newmetatable(L, CONTAINER_TYPENAME);
luaL_setfuncs(L, lxc_container_methods, 0);
lua_pushvalue(L, -1); /* push metatable */ lua_pushvalue(L, -1); /* push metatable */
lua_pushstring(L, "__gc"); lua_pushstring(L, "__gc");
lua_pushcfunction(L, container_gc); lua_pushcfunction(L, container_gc);
lua_settable(L, -3); lua_settable(L, -3);
lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */ lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
luaL_register(L, NULL, lxc_container_methods);
lua_pop(L, 1); lua_pop(L, 1);
return 1; return 1;
} }

View File

@ -32,6 +32,11 @@ local lxc_path
local cgroup_path local cgroup_path
local log_level = 3 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 -- the following two functions can be useful for debugging
function printf(...) function printf(...)
local function wrapper(...) io.write(string.format(...)) end local function wrapper(...) io.write(string.format(...)) end
@ -286,7 +291,7 @@ function container:stat_get_ints(controller, item, coords)
table.insert(result, val) table.insert(result, val)
end end
end end
return unpack(result) return table.unpack(result)
end end
-- read an integer from a cgroup file -- read an integer from a cgroup file
@ -324,17 +329,6 @@ function container:stat_match_get_int(controller, item, match, column)
return val return val
end 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) function container:stats_get(total)
local stat = {} local stat = {}
stat.mem_used = self:stat_get_int("memory", "memory.usage_in_bytes") stat.mem_used = self:stat_get_int("memory", "memory.usage_in_bytes")
@ -359,10 +353,21 @@ function container:stats_get(total)
return stat return stat
end 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 -- return configured containers found in LXC_PATH directory
function containers_configured(names_only) function M.containers_configured(names_only)
local containers = {} local containers = {}
for dir in lfs.dir(lxc_path) do for dir in lfs.dir(lxc_path) do
@ -390,7 +395,7 @@ function containers_configured(names_only)
end end
-- return running containers found in cgroup fs -- return running containers found in cgroup fs
function containers_running(names_only) function M.containers_running(names_only)
local containers = {} local containers = {}
local attr local attr
@ -426,3 +431,5 @@ end
lxc_path = core.default_config_path_get() lxc_path = core.default_config_path_get()
cgroup_path = cgroup_path_get() cgroup_path = cgroup_path_get()
return M