[lxc-devel] [PATCH] update lua binding to support both 5.2 and 5.1

Dwight Engen dwight.engen at oracle.com
Thu Sep 5 13:41:12 UTC 2013


fix a bug in the parsing of /proc/mounts (from Natanael Copa
<ncopa at alpinelinux.org>)

remove lxc subdir in cgroup paths

remove extraneous debug printfs

remove extra call to stats_clear

Tested lxc-top, and apitest.lua on both lua 5.1 and 5.2

Signed-off-by: Dwight Engen <dwight.engen at oracle.com>
---
 configure.ac            | 10 +++++++++-
 src/lua-lxc/Makefile.am |  4 ++--
 src/lua-lxc/lxc.lua     | 50 ++++++++++++++++++++++---------------------------
 src/lxc/lxc-top         | 14 ++++++++++----
 4 files changed, 43 insertions(+), 35 deletions(-)

diff --git a/configure.ac b/configure.ac
index df9a44b..f6f067d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -164,9 +164,17 @@ AC_ARG_ENABLE([lua],
 AM_CONDITIONAL([ENABLE_LUA], [test "x$enable_lua" = "xyes"])
 
 AM_COND_IF([ENABLE_LUA],
-	[PKG_CHECK_MODULES([LUA], [$LUAPKGCONFIG >= 5.1],[],[AC_MSG_ERROR([You must install lua-devel for lua 5.1])])
+	[AC_CHECK_PROG([LUA],[lua],[lua],[no])
+	PKG_CHECK_MODULES([LUADEVEL], [$LUAPKGCONFIG >= 5.1],[],[AC_MSG_ERROR([You must install lua-devel])])
 	AC_DEFINE_UNQUOTED([ENABLE_LUA], 1, [Lua is available])])
 
+AS_IF([test "x$LUA" != "xno"],
+	[AC_MSG_CHECKING([for lua version])
+	LUA_VERSION=`$LUA -e 'print(_VERSION)' | awk '{print $2}'`
+	AC_MSG_RESULT([$LUA_VERSION])
+	AC_SUBST([LUA_VERSION])])
+
+
 # Optional test binaries
 AC_ARG_ENABLE([tests],
 	[AC_HELP_STRING([--enable-tests], [build test/example binaries])],
diff --git a/src/lua-lxc/Makefile.am b/src/lua-lxc/Makefile.am
index 82dbae8..137fcda 100644
--- a/src/lua-lxc/Makefile.am
+++ b/src/lua-lxc/Makefile.am
@@ -1,7 +1,7 @@
 if ENABLE_LUA
 
-luadir=$(datadir)/lua/5.1
-sodir=$(libdir)/lua/5.1/lxc
+luadir=$(datadir)/lua/$(LUA_VERSION)
+sodir=$(libdir)/lua/$(LUA_VERSION)/lxc
 
 lua_SCRIPTS=lxc.lua
 
diff --git a/src/lua-lxc/lxc.lua b/src/lua-lxc/lxc.lua
index b48eb76..c04906e 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
@@ -89,6 +94,9 @@ function cgroup_path_get()
 	while true do
 	    local c
 	    line = f:read()
+	    if line == nil then
+		break
+	    end
 	    c = line:split(" ", 6)
 	    if (c[1] == "cgroup") then
 		cgroup_path = dirname(c[2])
@@ -262,7 +270,7 @@ end
 -- methods for stats collection from various cgroup files
 -- read integers at given coordinates from a cgroup file
 function container:stat_get_ints(controller, item, coords)
-    local f = io.open(cgroup_path.."/"..controller.."/lxc/"..self.ctname.."/"..item, "r")
+    local f = io.open(cgroup_path.."/"..controller.."/"..self.ctname.."/"..item, "r")
     local lines = {}
     local result = {}
 
@@ -283,12 +291,12 @@ 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
 function container:stat_get_int(controller, item)
-    local f = io.open(cgroup_path.."/"..controller.."/lxc/"..self.ctname.."/"..item, "r")
+    local f = io.open(cgroup_path.."/"..controller.."/"..self.ctname.."/"..item, "r")
     if (not f) then
 	return 0
     end
@@ -302,19 +310,17 @@ end
 
 function container:stat_match_get_int(controller, item, match, column)
     local val
-    local f = io.open(cgroup_path.."/"..controller.."/lxc/"..self.ctname.."/"..item, "r")
+    local f = io.open(cgroup_path.."/"..controller.."/"..self.ctname.."/"..item, "r")
     if (not f) then
 	return 0
     end
 
     for line in f:lines() do
-	printf("matching line:%s with match:%s\n", line, match)
 	if (string.find(line, match)) then
 	    local col
 
 	    col = line:split(" ", 80)
 	    val = tonumber(col[column]) or 0
-	    printf("found line!! val:%d\n", val)
 	end
     end
     f:close()
@@ -389,34 +395,22 @@ end
 -- return running containers found in cgroup fs
 function containers_running(names_only)
     local containers = {}
-    local attr
-
-    -- the lxc directory won't exist if no containers has ever been started
-    attr = lfs.attributes(cgroup_path .. "/cpu/lxc")
-    if (not attr) then
-	return containers
-    end
+    local names = containers_configured(true)
 
-    for file in lfs.dir(cgroup_path .. "/cpu/lxc") do
-	if (file ~= "." and file ~= "..")
-	then
-	    local pathfile = cgroup_path .. "/cpu/lxc/" .. file
-	    local attr = lfs.attributes(pathfile)
-
-	    if (attr.mode == "directory") then
+    for _,name in ipairs(names) do
+	local ct = container:new(name)
+	if ct:running() then
+		-- note, this is a "mixed" table, ie both dictionary and list
+		table.insert(containers, name)
 		if (names_only) then
-		    -- note, this is a "mixed" table, ie both dictionary and list
-		    containers[file] = true
-		    table.insert(containers, file)
+		    containers[name] = true
+		    ct = nil
 		else
-		    local ct = container:new(file)
-		    -- note, this is a "mixed" table, ie both dictionary and list
-		    containers[file] = ct
-		    table.insert(containers, file)
+		    containers[name] = ct
 		end
-	    end
 	end
     end
+    
     table.sort(containers, function (a,b) return (a < b) end)
     return containers
 end
diff --git a/src/lxc/lxc-top b/src/lxc/lxc-top
index 31aaecf..969f1f7 100755
--- a/src/lxc/lxc-top
+++ b/src/lxc/lxc-top
@@ -97,9 +97,16 @@ end
 
 function usleep(n)
     if (n ~= 0) then
-	ret = os.execute("usleep " .. tonumber(n))
-	if (ret ~= 0) then
-	    os.exit(0)
+	local ret = os.execute("usleep " .. tonumber(n))
+	if (string.sub(_VERSION, 5) == "5.2") then
+	    if (ret == nil) then
+		os.exit(0)
+	    end
+	end
+	if (string.sub(_VERSION, 5) == "5.1") then
+	    if (ret ~= 0) then
+		os.exit(0)
+	    end
 	end
     end
 end
@@ -138,7 +145,6 @@ end
 function container_list_update()
     local now_running
 
-    lxc.stats_clear(stats_total)
     now_running = lxc.containers_running(true)
 
     -- check for newly started containers
-- 
1.8.1.4







More information about the lxc-devel mailing list