[lxc-devel] [PATCH 1/3] add [gs]et_cgroup_item to lua api
Serge Hallyn
serge.hallyn at ubuntu.com
Mon Nov 4 12:35:40 UTC 2013
Quoting Dwight Engen (dwight.engen at gmail.com):
> fix up api test to run and add test for new [gs]et_cgroup_item
>
> Signed-off-by: Dwight Engen <dwight.engen at oracle.com>
For the set,
Acked-by: Serge E. Hallyn <serge.hallyn at ubuntu.com>
> ---
> src/lua-lxc/core.c | 35 +++++++++++++++++++++++++++++++++++
> src/lua-lxc/lxc.lua | 16 ++++++++++++++++
> src/lua-lxc/test/apitest.lua | 13 +++++++++++++
> 3 files changed, 64 insertions(+)
>
> diff --git a/src/lua-lxc/core.c b/src/lua-lxc/core.c
> index 002e8bf..ea19cc3 100644
> --- a/src/lua-lxc/core.c
> +++ b/src/lua-lxc/core.c
> @@ -282,6 +282,29 @@ static int container_clear_config_item(lua_State *L)
> return 1;
> }
>
> +static int container_get_cgroup_item(lua_State *L)
> +{
> + struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
> + const char *key = luaL_checkstring(L, 2);
> + int len;
> + char *value;
> +
> + len = c->get_cgroup_item(c, key, NULL, 0);
> + if (len <= 0)
> + goto not_found;
> +
> + value = alloca(sizeof(char)*len + 1);
> + if (c->get_cgroup_item(c, key, value, len + 1) != len)
> + goto not_found;
> +
> + lua_pushstring(L, value);
> + return 1;
> +
> +not_found:
> + lua_pushnil(L);
> + return 1;
> +}
> +
> static int container_get_config_item(lua_State *L)
> {
> struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
> @@ -305,6 +328,16 @@ not_found:
> return 1;
> }
>
> +static int container_set_cgroup_item(lua_State *L)
> +{
> + struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
> + const char *key = luaL_checkstring(L, 2);
> + const char *value = luaL_checkstring(L, 3);
> +
> + lua_pushboolean(L, !!c->set_cgroup_item(c, key, value));
> + return 1;
> +}
> +
> static int container_set_config_item(lua_State *L)
> {
> struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
> @@ -361,6 +394,8 @@ static luaL_Reg lxc_container_methods[] =
> {"config_file_name", container_config_file_name},
> {"load_config", container_load_config},
> {"save_config", container_save_config},
> + {"get_cgroup_item", container_get_cgroup_item},
> + {"set_cgroup_item", container_set_cgroup_item},
> {"get_config_path", container_get_config_path},
> {"set_config_path", container_set_config_path},
> {"get_config_item", container_get_config_item},
> diff --git a/src/lua-lxc/lxc.lua b/src/lua-lxc/lxc.lua
> index aa80a95..7c9580e 100755
> --- a/src/lua-lxc/lxc.lua
> +++ b/src/lua-lxc/lxc.lua
> @@ -189,6 +189,10 @@ function container:clear_config_item(key)
> return self.core:clear_config_item(key)
> end
>
> +function container:get_cgroup_item(key)
> + return self.core:get_cgroup_item(key)
> +end
> +
> function container:get_config_item(key)
> local value
> local vals = {}
> @@ -209,6 +213,10 @@ function container:get_config_item(key)
> return vals
> end
>
> +function container:set_cgroup_item(key, value)
> + return self.core:set_cgroup_item(key, value)
> +end
> +
> function container:set_config_item(key, value)
> return self.core:set_config_item(key, value)
> end
> @@ -410,6 +418,14 @@ function M.containers_running(names_only)
> return containers
> end
>
> +function M.version_get()
> + return core.version_get()
> +end
> +
> +function M.default_config_path_get()
> + return core.default_config_path_get()
> +end
> +
> lxc_path = core.default_config_path_get()
> cgroup_path = cgroup_path_get()
>
> diff --git a/src/lua-lxc/test/apitest.lua b/src/lua-lxc/test/apitest.lua
> index 1365f91..f957ca4 100755
> --- a/src/lua-lxc/test/apitest.lua
> +++ b/src/lua-lxc/test/apitest.lua
> @@ -206,6 +206,17 @@ function test_container_in_cfglist(should_find)
> end
> end
>
> +function test_container_cgroup()
> + log(0, "Test get/set cgroup items...")
> +
> + max_mem = container:get_cgroup_item("memory.max_usage_in_bytes")
> + saved_limit = container:get_cgroup_item("memory.limit_in_bytes")
> + assert(saved_limit ~= max_mem)
> + assert(container:set_cgroup_item("memory.limit_in_bytes", max_mem))
> + assert(container:get_cgroup_item("memory.limit_in_bytes") ~= saved_limit)
> + assert(container:set_cgroup_item("memory.limit_in_bytes", "-1"))
> +end
> +
> function test_config_items()
> log(0, "Test set/clear configuration items...")
>
> @@ -313,6 +324,8 @@ test_config_network(0)
> test_container_start()
> test_container_started()
>
> +test_container_cgroup()
> +
> test_container_freeze()
> test_container_frozen()
> test_container_unfreeze()
> --
> 1.8.3.1
>
>
> ------------------------------------------------------------------------------
> Android is increasing in popularity, but the open development platform that
> developers love is also attractive to malware creators. Download this white
> paper to learn more about secure code signing practices that can help keep
> Android apps secure.
> http://pubads.g.doubleclick.net/gampad/clk?id=65839951&iu=/4140/ostg.clktrk
> _______________________________________________
> Lxc-devel mailing list
> Lxc-devel at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/lxc-devel
More information about the lxc-devel
mailing list