aboutsummaryrefslogtreecommitdiffstats
path: root/util-src
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2012-07-28 01:14:31 +0100
committerMatthew Wild <mwild1@gmail.com>2012-07-28 01:14:31 +0100
commitfbe75768718891167313bc0cbcffcd3a46042d31 (patch)
tree2732225257e7b8ade28695dad7c0df3f7cea00b1 /util-src
parent8395775fc1bd1e72a8c83927003d2af1ed5f304a (diff)
parentf926338983c2f1dd0276d41e3c3d8e28cbef3d02 (diff)
downloadprosody-fbe75768718891167313bc0cbcffcd3a46042d31.tar.gz
prosody-fbe75768718891167313bc0cbcffcd3a46042d31.zip
Merge with Florob
Diffstat (limited to 'util-src')
-rw-r--r--util-src/pposix.c67
1 files changed, 67 insertions, 0 deletions
diff --git a/util-src/pposix.c b/util-src/pposix.c
index dae48390..65f8b4ab 100644
--- a/util-src/pposix.c
+++ b/util-src/pposix.c
@@ -34,6 +34,11 @@
#include "lua.h"
#include "lauxlib.h"
+#if (defined(_SVID_SOURCE) && !defined(WITHOUT_MALLINFO))
+ #include <malloc.h>
+ #define WITH_MALLINFO
+#endif
+
/* Daemonization support */
static int lc_daemonize(lua_State *L)
@@ -581,6 +586,62 @@ int lc_uname(lua_State* L)
return 1;
}
+int lc_setenv(lua_State* L)
+{
+ const char *var = luaL_checkstring(L, 1);
+ const char *value;
+
+ /* If the second argument is nil or nothing, unset the var */
+ if(lua_isnoneornil(L, 2))
+ {
+ if(unsetenv(var) != 0)
+ {
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(errno));
+ return 2;
+ }
+ lua_pushboolean(L, 1);
+ return 1;
+ }
+
+ value = luaL_checkstring(L, 2);
+
+ if(setenv(var, value, 1) != 0)
+ {
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(errno));
+ return 2;
+ }
+
+ lua_pushboolean(L, 1);
+ return 1;
+}
+
+#ifdef WITH_MALLINFO
+int lc_meminfo(lua_State* L)
+{
+ struct mallinfo info = mallinfo();
+ lua_newtable(L);
+ /* This is the total size of memory allocated with sbrk by malloc, in bytes. */
+ lua_pushinteger(L, info.arena);
+ lua_setfield(L, -2, "allocated");
+ /* This is the total size of memory allocated with mmap, in bytes. */
+ lua_pushinteger(L, info.hblkhd);
+ lua_setfield(L, -2, "allocated_mmap");
+ /* This is the total size of memory occupied by chunks handed out by malloc. */
+ lua_pushinteger(L, info.uordblks);
+ lua_setfield(L, -2, "used");
+ /* This is the total size of memory occupied by free (not in use) chunks. */
+ lua_pushinteger(L, info.fordblks);
+ lua_setfield(L, -2, "unused");
+ /* This is the size of the top-most releasable chunk that normally borders the
+ end of the heap (i.e., the high end of the virtual address space's data segment). */
+ lua_pushinteger(L, info.keepcost);
+ lua_setfield(L, -2, "returnable");
+ return 1;
+}
+#endif
+
/* Register functions */
int luaopen_util_pposix(lua_State *L)
@@ -612,6 +673,12 @@ int luaopen_util_pposix(lua_State *L)
{ "uname", lc_uname },
+ { "setenv", lc_setenv },
+
+#ifdef WITH_MALLINFO
+ { "meminfo", lc_meminfo },
+#endif
+
{ NULL, NULL }
};