aboutsummaryrefslogtreecommitdiffstats
path: root/util-src
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2023-06-14 13:39:39 +0200
committerKim Alvefur <zash@zash.se>2023-06-14 13:39:39 +0200
commitb7b65435f781ce9170e446e121be901213455c6c (patch)
treeecf36f217576b8e211598e0a8525e7d398c8a420 /util-src
parent23273ed228afad60d1ba4f69af9bcd21d1b8165d (diff)
downloadprosody-b7b65435f781ce9170e446e121be901213455c6c.tar.gz
prosody-b7b65435f781ce9170e446e121be901213455c6c.zip
util.pposix: Use Lua enum API for resource limit name argument
Because diffstat.
Diffstat (limited to 'util-src')
-rw-r--r--util-src/pposix.c80
1 files changed, 33 insertions, 47 deletions
diff --git a/util-src/pposix.c b/util-src/pposix.c
index d5a6712e..2d5b6797 100644
--- a/util-src/pposix.c
+++ b/util-src/pposix.c
@@ -507,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)) {
@@ -589,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);
@@ -619,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;
@@ -629,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)) {