aboutsummaryrefslogtreecommitdiffstats
path: root/util-src/pposix.c
diff options
context:
space:
mode:
Diffstat (limited to 'util-src/pposix.c')
-rw-r--r--util-src/pposix.c126
1 files changed, 73 insertions, 53 deletions
diff --git a/util-src/pposix.c b/util-src/pposix.c
index a8e0720f..b2a8dbac 100644
--- a/util-src/pposix.c
+++ b/util-src/pposix.c
@@ -58,9 +58,6 @@
#include "lualib.h"
#include "lauxlib.h"
-#if (LUA_VERSION_NUM == 501)
-#define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R)
-#endif
#if (LUA_VERSION_NUM < 503)
#define lua_isinteger(L, n) lua_isnumber(L, n)
#endif
@@ -510,55 +507,43 @@ static int lc_mkdir(lua_State *L) {
* Example usage:
* pposix.setrlimit("NOFILE", 1000, 2000)
*/
-static int string2resource(const char *s) {
- if(!strcmp(s, "CORE")) {
- return RLIMIT_CORE;
- }
-
- if(!strcmp(s, "CPU")) {
- return RLIMIT_CPU;
- }
-
- if(!strcmp(s, "DATA")) {
- return RLIMIT_DATA;
- }
-
- if(!strcmp(s, "FSIZE")) {
- return RLIMIT_FSIZE;
- }
-
- if(!strcmp(s, "NOFILE")) {
- return RLIMIT_NOFILE;
- }
-
- if(!strcmp(s, "STACK")) {
- return RLIMIT_STACK;
- }
+static const char *const resource_strings[] = {
+ /* Defined by POSIX */
+ "CORE",
+ "CPU",
+ "DATA",
+ "FSIZE",
+ "NOFILE",
+ "STACK",
#if !(defined(sun) || defined(__sun) || defined(__APPLE__))
-
- if(!strcmp(s, "MEMLOCK")) {
- return RLIMIT_MEMLOCK;
- }
-
- if(!strcmp(s, "NPROC")) {
- return RLIMIT_NPROC;
- }
-
- if(!strcmp(s, "RSS")) {
- return RLIMIT_RSS;
- }
-
+ "MEMLOCK",
+ "NPROC",
+ "RSS",
#endif
#ifdef RLIMIT_NICE
+ "NICE",
+#endif
+ NULL
+};
- if(!strcmp(s, "NICE")) {
- return RLIMIT_NICE;
- }
-
+static int resource_constants[] = {
+ RLIMIT_CORE,
+ RLIMIT_CPU,
+ RLIMIT_DATA,
+ RLIMIT_FSIZE,
+ RLIMIT_NOFILE,
+ RLIMIT_STACK,
+#if !(defined(sun) || defined(__sun) || defined(__APPLE__))
+ RLIMIT_MEMLOCK,
+ RLIMIT_NPROC,
+ RLIMIT_RSS,
#endif
- return -1;
-}
+#ifdef RLIMIT_NICE
+ RLIMIT_NICE,
+#endif
+ -1,
+};
static rlim_t arg_to_rlimit(lua_State *L, int idx, rlim_t current) {
switch(lua_type(L, idx)) {
@@ -592,7 +577,7 @@ static int lc_setrlimit(lua_State *L) {
return 2;
}
- rid = string2resource(luaL_checkstring(L, 1));
+ rid = resource_constants[luaL_checkoption(L, 1,NULL, resource_strings)];
if(rid == -1) {
lua_pushboolean(L, 0);
@@ -622,7 +607,6 @@ static int lc_setrlimit(lua_State *L) {
static int lc_getrlimit(lua_State *L) {
int arguments = lua_gettop(L);
- const char *resource = NULL;
int rid = -1;
struct rlimit lim;
@@ -632,8 +616,7 @@ static int lc_getrlimit(lua_State *L) {
return 2;
}
- resource = luaL_checkstring(L, 1);
- rid = string2resource(resource);
+ rid = resource_constants[luaL_checkoption(L, 1, NULL, resource_strings)];
if(rid != -1) {
if(getrlimit(rid, &lim)) {
@@ -819,6 +802,41 @@ static int lc_atomic_append(lua_State *L) {
return 3;
}
+static int lc_remove_blocks(lua_State *L) {
+#if defined(__linux__)
+ int err;
+
+ FILE *f = *(FILE **) luaL_checkudata(L, 1, LUA_FILEHANDLE);
+ off_t offset = (off_t)luaL_checkinteger(L, 2);
+ off_t length = (off_t)luaL_checkinteger(L, 3);
+
+ errno = 0;
+
+ if((err = fallocate(fileno(f), FALLOC_FL_COLLAPSE_RANGE, offset, length))) {
+ if(errno != 0) {
+ /* Some old versions of Linux apparently use the return value instead of errno */
+ err = errno;
+ }
+
+ switch(err) {
+ default: /* Other issues */
+ luaL_pushfail(L);
+ lua_pushstring(L, strerror(err));
+ lua_pushinteger(L, err);
+ return 3;
+ }
+ }
+
+ lua_pushboolean(L, err == 0);
+ return 1;
+#else
+ luaL_pushfail(L);
+ lua_pushstring(L, strerror(EOPNOTSUPP));
+ lua_pushinteger(L, EOPNOTSUPP);
+ return 3;
+#endif
+}
+
static int lc_isatty(lua_State *L) {
FILE *f = *(FILE **) luaL_checkudata(L, 1, LUA_FILEHANDLE);
const int fd = fileno(f);
@@ -828,10 +846,8 @@ static int lc_isatty(lua_State *L) {
/* Register functions */
-int luaopen_util_pposix(lua_State *L) {
-#if (LUA_VERSION_NUM > 501)
+int luaopen_prosody_util_pposix(lua_State *L) {
luaL_checkversion(L);
-#endif
luaL_Reg exports[] = {
{ "abort", lc_abort },
@@ -866,6 +882,7 @@ int luaopen_util_pposix(lua_State *L) {
#endif
{ "atomic_append", lc_atomic_append },
+ { "remove_blocks", lc_remove_blocks },
{ "isatty", lc_isatty },
@@ -888,3 +905,6 @@ int luaopen_util_pposix(lua_State *L) {
return 1;
}
+int luaopen_util_pposix(lua_State *L) {
+ return luaopen_prosody_util_pposix(L);
+}