[lxc-devel] [PATCH] lua: implement usleep in C module

Natanael Copa ncopa at alpinelinux.org
Thu Sep 5 12:33:28 UTC 2013


So we avoid running os.execute

Signed-off-by: Natanael Copa <ncopa at alpinelinux.org>
---
 src/lua-lxc/core.c |  9 +++++++++
 src/lxc/lxc-top    | 12 ++----------
 2 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/src/lua-lxc/core.c b/src/lua-lxc/core.c
index d404707..504e147 100644
--- a/src/lua-lxc/core.c
+++ b/src/lua-lxc/core.c
@@ -27,11 +27,13 @@
 #include <lauxlib.h>
 #include <assert.h>
 #include <string.h>
+#include <unistd.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))
+#define luaL_checkunsigned(L,n) luaL_checknumber(L,n)
 #endif
 
 #ifdef NO_CHECK_UDATA
@@ -379,10 +381,17 @@ static int lxc_default_config_path_get(lua_State *L) {
     return 1;
 }
 
+/* utility functions */
+static int lxc_util_usleep(lua_State *L) {
+    usleep((useconds_t)luaL_checkunsigned(L, 1));
+    return 0;
+}
+
 static luaL_Reg lxc_lib_methods[] = {
     {"version_get",		lxc_version_get},
     {"default_config_path_get",	lxc_default_config_path_get},
     {"container_new",		container_new},
+    {"usleep",			lxc_util_usleep},
     {NULL, NULL}
 };
 
diff --git a/src/lxc/lxc-top b/src/lxc/lxc-top
index 31aaecf..8e8bafb 100755
--- a/src/lxc/lxc-top
+++ b/src/lxc/lxc-top
@@ -22,6 +22,7 @@
 --
 
 local lxc    = require("lxc")
+local core   = require("lxc.core")
 local getopt = require("alt_getopt")
 local lfs    = require("lfs")
 
@@ -95,15 +96,6 @@ function strsisize(size, width)
     return string.format("%3d.00   ", size)
 end
 
-function usleep(n)
-    if (n ~= 0) then
-	ret = os.execute("usleep " .. tonumber(n))
-	if (ret ~= 0) then
-	    os.exit(0)
-	end
-    end
-end
-
 function tty_lines()
     local rows = 25
     local f = assert(io.popen("stty -a | head -n 1"))
@@ -238,5 +230,5 @@ do
     end
     stats_print(string.format("TOTAL (%-2d)", #containers), stats_total)
     io.flush()
-    usleep(optarg["d"] * 1000000)
+    core.usleep(optarg["d"] * 1000000)
 end
-- 
1.8.4





More information about the lxc-devel mailing list