util.pposix: Ask for shared file descriptor table using rfork() on *BSD (fixes #412)
[prosody.git] / util-src / pposix.c
index d797f0329af95907a5153fff02019b2aab7efc69..b48465d3a75461bd6a9ee216fbace132d81e647c 100644 (file)
@@ -35,8 +35,8 @@
 #include "lualib.h"
 #include "lauxlib.h"
 
-#if (LUA_VERSION_NUM == 502)
-#define luaL_register(L, N, R) luaL_setfuncs(L, R, 0)
+#if (LUA_VERSION_NUM == 501)
+#define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R)
 #endif
 
 #include <fcntl.h>
 #define WITH_MALLINFO
 #endif
 
+#if defined(RFPROC) && defined(EV_SET)
+/*
+ * On *BSD, calling fork() is equivalent to rfork(RFPROC | RFFDG).
+ *
+ * RFFDG being set means that the file descriptor table is copied,
+ * otherwise it's shared. We want the later, otherwise libevent gets
+ * messed up.
+ *
+ * See issue #412
+ */
+#define fork() rfork(RFPROC)
+#endif
+
 /* Daemonization support */
 
 static int lc_daemonize(lua_State* L) {
@@ -642,6 +655,10 @@ int lc_uname(lua_State* L) {
        lua_setfield(L, -2, "version");
        lua_pushstring(L, uname_info.machine);
        lua_setfield(L, -2, "machine");
+#ifdef _GNU_SOURCE
+       lua_pushstring(L, uname_info.domainname);
+       lua_setfield(L, -2, "domainname");
+#endif
        return 1;
 }
 
@@ -750,7 +767,10 @@ int lc_fallocate(lua_State* L) {
                lua_pushstring(L, strerror(ret));
                /* posix_fallocate() can leave a bunch of NULs at the end, so we cut that
                 * this assumes that offset == length of the file */
-               ftruncate(fileno(f), offset);
+               if(ftruncate(fileno(f), offset) != 0) {
+                       lua_pushstring(L, strerror(errno));
+                       return 3;
+               }
                return 2;
        }
 }
@@ -800,7 +820,7 @@ int luaopen_util_pposix(lua_State* L) {
        };
 
        lua_newtable(L);
-       luaL_register(L, NULL,  exports);
+       luaL_setfuncs(L, exports, 0);
 
        lua_pushliteral(L, "pposix");
        lua_setfield(L, -2, "_NAME");