diff options
Diffstat (limited to 'util-src')
-rw-r--r-- | util-src/Makefile | 21 | ||||
-rw-r--r-- | util-src/encodings.c | 174 | ||||
-rw-r--r-- | util-src/hashes.c | 171 | ||||
-rw-r--r-- | util-src/net.c | 117 | ||||
-rw-r--r-- | util-src/pposix.c | 251 | ||||
-rw-r--r-- | util-src/windows.c | 26 |
6 files changed, 595 insertions, 165 deletions
diff --git a/util-src/Makefile b/util-src/Makefile index 1ca934ad..3a1ba3f2 100644 --- a/util-src/Makefile +++ b/util-src/Makefile @@ -9,9 +9,21 @@ OPENSSL_LIB?=crypto CC?=gcc CXX?=g++ LD?=gcc +CFLAGS+=-ggdb +.PHONY: all install clean .SUFFIXES: .c .o .so +all: encodings.so hashes.so net.so pposix.so signal.so + +install: encodings.so hashes.so net.so pposix.so signal.so + install *.so ../util/ + +clean: + rm -f *.o + rm -f *.so + rm -f ../util/*.so + encodings.so: encodings.o MACOSX_DEPLOYMENT_TARGET="10.3"; export MACOSX_DEPLOYMENT_TARGET; $(CC) -o $@ $< $(LDFLAGS) $(IDNA_LIBS) @@ -27,12 +39,3 @@ hashes.so: hashes.o MACOSX_DEPLOYMENT_TARGET="10.3"; export MACOSX_DEPLOYMENT_TARGET; $(LD) -o $@ $< $(LDFLAGS) -all: encodings.so hashes.so pposix.so signal.so - -install: encodings.so hashes.so pposix.so signal.so - install *.so ../util/ - -clean: - rm -f *.o - rm -f *.so - rm -f ../util/*.so diff --git a/util-src/encodings.c b/util-src/encodings.c index 2a4653fb..b9b6160a 100644 --- a/util-src/encodings.c +++ b/util-src/encodings.c @@ -117,55 +117,8 @@ static const luaL_Reg Reg_base64[] = }; /***************** STRINGPREP *****************/ -#ifndef USE_STRINGPREP_ICU -/****************** libidn ********************/ - -#include <stringprep.h> - -static int stringprep_prep(lua_State *L, const Stringprep_profile *profile) -{ - size_t len; - const char *s; - char string[1024]; - int ret; - if(!lua_isstring(L, 1)) { - lua_pushnil(L); - return 1; - } - s = lua_tolstring(L, 1, &len); - if (len >= 1024) { - lua_pushnil(L); - return 1; /* TODO return error message */ - } - strcpy(string, s); - ret = stringprep(string, 1024, (Stringprep_profile_flags)0, profile); - if (ret == STRINGPREP_OK) { - lua_pushstring(L, string); - return 1; - } else { - lua_pushnil(L); - return 1; /* TODO return error message */ - } -} - -#define MAKE_PREP_FUNC(myFunc, prep) \ -static int myFunc(lua_State *L) { return stringprep_prep(L, prep); } - -MAKE_PREP_FUNC(Lstringprep_nameprep, stringprep_nameprep) /** stringprep.nameprep(s) */ -MAKE_PREP_FUNC(Lstringprep_nodeprep, stringprep_xmpp_nodeprep) /** stringprep.nodeprep(s) */ -MAKE_PREP_FUNC(Lstringprep_resourceprep, stringprep_xmpp_resourceprep) /** stringprep.resourceprep(s) */ -MAKE_PREP_FUNC(Lstringprep_saslprep, stringprep_saslprep) /** stringprep.saslprep(s) */ - -static const luaL_Reg Reg_stringprep[] = -{ - { "nameprep", Lstringprep_nameprep }, - { "nodeprep", Lstringprep_nodeprep }, - { "resourceprep", Lstringprep_resourceprep }, - { "saslprep", Lstringprep_saslprep }, - { NULL, NULL } -}; +#ifdef USE_STRINGPREP_ICU -#else #include <unicode/usprep.h> #include <unicode/ustring.h> #include <unicode/utrace.h> @@ -192,13 +145,17 @@ static int icu_stringprep_prep(lua_State *L, const UStringPrepProfile *profile) return 1; } u_strFromUTF8(unprepped, 1024, &unprepped_len, input, input_len, &err); + if (U_FAILURE(err)) { + lua_pushnil(L); + return 1; + } prepped_len = usprep_prepare(profile, unprepped, unprepped_len, prepped, 1024, 0, NULL, &err); if (U_FAILURE(err)) { lua_pushnil(L); return 1; } else { u_strToUTF8(output, 1024, &output_len, prepped, prepped_len, &err); - if(output_len < 1024) + if (U_SUCCESS(err) && output_len < 1024) lua_pushlstring(L, output, output_len); else lua_pushnil(L); @@ -239,49 +196,58 @@ static const luaL_Reg Reg_stringprep[] = { "saslprep", Lstringprep_saslprep }, { NULL, NULL } }; -#endif +#else /* USE_STRINGPREP_ICU */ -/***************** IDNA *****************/ -#ifndef USE_STRINGPREP_ICU /****************** libidn ********************/ -#include <idna.h> -#include <idn-free.h> +#include <stringprep.h> -static int Lidna_to_ascii(lua_State *L) /** idna.to_ascii(s) */ +static int stringprep_prep(lua_State *L, const Stringprep_profile *profile) { size_t len; - const char *s = luaL_checklstring(L, 1, &len); - char* output = NULL; - int ret = idna_to_ascii_8z(s, &output, IDNA_USE_STD3_ASCII_RULES); - if (ret == IDNA_SUCCESS) { - lua_pushstring(L, output); - idn_free(output); + const char *s; + char string[1024]; + int ret; + if(!lua_isstring(L, 1)) { + lua_pushnil(L); return 1; - } else { + } + s = lua_tolstring(L, 1, &len); + if (len >= 1024) { lua_pushnil(L); - idn_free(output); return 1; /* TODO return error message */ } -} - -static int Lidna_to_unicode(lua_State *L) /** idna.to_unicode(s) */ -{ - size_t len; - const char *s = luaL_checklstring(L, 1, &len); - char* output = NULL; - int ret = idna_to_unicode_8z8z(s, &output, 0); - if (ret == IDNA_SUCCESS) { - lua_pushstring(L, output); - idn_free(output); + strcpy(string, s); + ret = stringprep(string, 1024, (Stringprep_profile_flags)0, profile); + if (ret == STRINGPREP_OK) { + lua_pushstring(L, string); return 1; } else { lua_pushnil(L); - idn_free(output); return 1; /* TODO return error message */ } } -#else + +#define MAKE_PREP_FUNC(myFunc, prep) \ +static int myFunc(lua_State *L) { return stringprep_prep(L, prep); } + +MAKE_PREP_FUNC(Lstringprep_nameprep, stringprep_nameprep) /** stringprep.nameprep(s) */ +MAKE_PREP_FUNC(Lstringprep_nodeprep, stringprep_xmpp_nodeprep) /** stringprep.nodeprep(s) */ +MAKE_PREP_FUNC(Lstringprep_resourceprep, stringprep_xmpp_resourceprep) /** stringprep.resourceprep(s) */ +MAKE_PREP_FUNC(Lstringprep_saslprep, stringprep_saslprep) /** stringprep.saslprep(s) */ + +static const luaL_Reg Reg_stringprep[] = +{ + { "nameprep", Lstringprep_nameprep }, + { "nodeprep", Lstringprep_nodeprep }, + { "resourceprep", Lstringprep_resourceprep }, + { "saslprep", Lstringprep_saslprep }, + { NULL, NULL } +}; +#endif + +/***************** IDNA *****************/ +#ifdef USE_STRINGPREP_ICU #include <unicode/ustdio.h> #include <unicode/uidna.h> /* IDNA2003 or IDNA2008 ? ? ? */ @@ -296,13 +262,18 @@ static int Lidna_to_ascii(lua_State *L) /** idna.to_ascii(s) */ char output[1024]; u_strFromUTF8(ustr, 1024, &ulen, s, len, &err); + if (U_FAILURE(err)) { + lua_pushnil(L); + return 1; + } + dest_len = uidna_IDNToASCII(ustr, ulen, dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err); if (U_FAILURE(err)) { lua_pushnil(L); return 1; } else { u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err); - if(output_len < 1024) + if (U_SUCCESS(err) && output_len < 1024) lua_pushlstring(L, output, output_len); else lua_pushnil(L); @@ -315,25 +286,70 @@ static int Lidna_to_unicode(lua_State *L) /** idna.to_unicode(s) */ size_t len; int32_t ulen, dest_len, output_len; const char *s = luaL_checklstring(L, 1, &len); - UChar* ustr; + UChar ustr[1024]; UErrorCode err = U_ZERO_ERROR; UChar dest[1024]; char output[1024]; u_strFromUTF8(ustr, 1024, &ulen, s, len, &err); + if (U_FAILURE(err)) { + lua_pushnil(L); + return 1; + } + dest_len = uidna_IDNToUnicode(ustr, ulen, dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err); if (U_FAILURE(err)) { lua_pushnil(L); return 1; } else { u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err); - if(output_len < 1024) + if (U_SUCCESS(err) && output_len < 1024) lua_pushlstring(L, output, output_len); else lua_pushnil(L); return 1; } } + +#else /* USE_STRINGPREP_ICU */ +/****************** libidn ********************/ + +#include <idna.h> +#include <idn-free.h> + +static int Lidna_to_ascii(lua_State *L) /** idna.to_ascii(s) */ +{ + size_t len; + const char *s = luaL_checklstring(L, 1, &len); + char* output = NULL; + int ret = idna_to_ascii_8z(s, &output, IDNA_USE_STD3_ASCII_RULES); + if (ret == IDNA_SUCCESS) { + lua_pushstring(L, output); + idn_free(output); + return 1; + } else { + lua_pushnil(L); + idn_free(output); + return 1; /* TODO return error message */ + } +} + +static int Lidna_to_unicode(lua_State *L) /** idna.to_unicode(s) */ +{ + size_t len; + const char *s = luaL_checklstring(L, 1, &len); + char* output = NULL; + int ret = idna_to_unicode_8z8z(s, &output, 0); + if (ret == IDNA_SUCCESS) { + lua_pushstring(L, output); + idn_free(output); + return 1; + } else { + lua_pushnil(L); + idn_free(output); + return 1; /* TODO return error message */ + } +} #endif static const luaL_Reg Reg_idna[] = diff --git a/util-src/hashes.c b/util-src/hashes.c index 33a9be89..33041e83 100644 --- a/util-src/hashes.c +++ b/util-src/hashes.c @@ -14,14 +14,24 @@ */ #include <string.h> +#include <stdlib.h> + +#ifdef _MSC_VER +typedef unsigned __int32 uint32_t; +#else +#include <inttypes.h> +#endif #include "lua.h" #include "lauxlib.h" #include <openssl/sha.h> #include <openssl/md5.h> -const char* hex_tab = "0123456789abcdef"; -void toHex(const char* in, int length, char* out) { +#define HMAC_IPAD 0x36363636 +#define HMAC_OPAD 0x5c5c5c5c + +const char *hex_tab = "0123456789abcdef"; +void toHex(const unsigned char *in, int length, unsigned char *out) { int i; for (i = 0; i < length; i++) { out[i*2] = hex_tab[(in[i] >> 4) & 0xF]; @@ -34,28 +44,161 @@ static int myFunc(lua_State *L) { \ size_t len; \ const char *s = luaL_checklstring(L, 1, &len); \ int hex_out = lua_toboolean(L, 2); \ - char hash[size]; \ - char result[size*2]; \ - func((const unsigned char*)s, len, (unsigned char*)hash); \ + unsigned char hash[size], result[size*2]; \ + func((const unsigned char*)s, len, hash); \ + if (hex_out) { \ + toHex(hash, size, result); \ + lua_pushlstring(L, (char*)result, size*2); \ + } else { \ + lua_pushlstring(L, (char*)hash, size);\ + } \ + return 1; \ +} + +MAKE_HASH_FUNCTION(Lsha1, SHA1, SHA_DIGEST_LENGTH) +MAKE_HASH_FUNCTION(Lsha224, SHA224, SHA224_DIGEST_LENGTH) +MAKE_HASH_FUNCTION(Lsha256, SHA256, SHA256_DIGEST_LENGTH) +MAKE_HASH_FUNCTION(Lsha384, SHA384, SHA384_DIGEST_LENGTH) +MAKE_HASH_FUNCTION(Lsha512, SHA512, SHA512_DIGEST_LENGTH) +MAKE_HASH_FUNCTION(Lmd5, MD5, MD5_DIGEST_LENGTH) + +struct hash_desc { + int (*Init)(void*); + int (*Update)(void*, const void *, size_t); + int (*Final)(unsigned char*, void*); + size_t digestLength; + void *ctx, *ctxo; +}; + +static void hmac(struct hash_desc *desc, const char *key, size_t key_len, + const char *msg, size_t msg_len, unsigned char *result) +{ + union xory { + unsigned char bytes[64]; + uint32_t quadbytes[16]; + }; + + int i; + unsigned char hashedKey[64]; /* Maximum used digest length */ + union xory k_ipad, k_opad; + + if (key_len > 64) { + desc->Init(desc->ctx); + desc->Update(desc->ctx, key, key_len); + desc->Final(hashedKey, desc->ctx); + key = (const char*)hashedKey; + key_len = desc->digestLength; + } + + memcpy(k_ipad.bytes, key, key_len); + memset(k_ipad.bytes + key_len, 0, 64 - key_len); + memcpy(k_opad.bytes, k_ipad.bytes, 64); + + for (i = 0; i < 16; i++) { + k_ipad.quadbytes[i] ^= HMAC_IPAD; + k_opad.quadbytes[i] ^= HMAC_OPAD; + } + + desc->Init(desc->ctx); + desc->Update(desc->ctx, k_ipad.bytes, 64); + desc->Init(desc->ctxo); + desc->Update(desc->ctxo, k_opad.bytes, 64); + desc->Update(desc->ctx, msg, msg_len); + desc->Final(result, desc->ctx); + desc->Update(desc->ctxo, result, desc->digestLength); + desc->Final(result, desc->ctxo); +} + +#define MAKE_HMAC_FUNCTION(myFunc, func, size, type) \ +static int myFunc(lua_State *L) { \ + type ctx, ctxo; \ + unsigned char hash[size], result[2*size]; \ + size_t key_len, msg_len; \ + const char *key = luaL_checklstring(L, 1, &key_len); \ + const char *msg = luaL_checklstring(L, 2, &msg_len); \ + const int hex_out = lua_toboolean(L, 3); \ + struct hash_desc desc; \ + desc.Init = (int (*)(void*))func##_Init; \ + desc.Update = (int (*)(void*, const void *, size_t))func##_Update; \ + desc.Final = (int (*)(unsigned char*, void*))func##_Final; \ + desc.digestLength = size; \ + desc.ctx = &ctx; \ + desc.ctxo = &ctxo; \ + hmac(&desc, key, key_len, msg, msg_len, hash); \ if (hex_out) { \ toHex(hash, size, result); \ - lua_pushlstring(L, result, size*2); \ + lua_pushlstring(L, (char*)result, size*2); \ } else { \ - lua_pushlstring(L, hash, size);\ + lua_pushlstring(L, (char*)hash, size); \ } \ return 1; \ } -MAKE_HASH_FUNCTION(Lsha1, SHA1, 20) -MAKE_HASH_FUNCTION(Lsha256, SHA256, 32) -MAKE_HASH_FUNCTION(Lmd5, MD5, 16) +MAKE_HMAC_FUNCTION(Lhmac_sha1, SHA1, SHA_DIGEST_LENGTH, SHA_CTX) +MAKE_HMAC_FUNCTION(Lhmac_sha256, SHA256, SHA256_DIGEST_LENGTH, SHA256_CTX) +MAKE_HMAC_FUNCTION(Lhmac_sha512, SHA512, SHA512_DIGEST_LENGTH, SHA512_CTX) +MAKE_HMAC_FUNCTION(Lhmac_md5, MD5, MD5_DIGEST_LENGTH, MD5_CTX) + +static int LscramHi(lua_State *L) { + union xory { + unsigned char bytes[SHA_DIGEST_LENGTH]; + uint32_t quadbytes[SHA_DIGEST_LENGTH/4]; + }; + int i; + SHA_CTX ctx, ctxo; + unsigned char Ust[SHA_DIGEST_LENGTH]; + union xory Und; + union xory res; + size_t str_len, salt_len; + struct hash_desc desc; + const char *str = luaL_checklstring(L, 1, &str_len); + const char *salt = luaL_checklstring(L, 2, &salt_len); + char *salt2; + const int iter = luaL_checkinteger(L, 3); + + desc.Init = (int (*)(void*))SHA1_Init; + desc.Update = (int (*)(void*, const void *, size_t))SHA1_Update; + desc.Final = (int (*)(unsigned char*, void*))SHA1_Final; + desc.digestLength = SHA_DIGEST_LENGTH; + desc.ctx = &ctx; + desc.ctxo = &ctxo; + + salt2 = malloc(salt_len + 4); + if (salt2 == NULL) + luaL_error(L, "Out of memory in scramHi"); + memcpy(salt2, salt, salt_len); + memcpy(salt2 + salt_len, "\0\0\0\1", 4); + hmac(&desc, str, str_len, salt2, salt_len + 4, Ust); + free(salt2); + + memcpy(res.bytes, Ust, sizeof(res)); + for (i = 1; i < iter; i++) { + int j; + hmac(&desc, str, str_len, (char*)Ust, sizeof(Ust), Und.bytes); + for (j = 0; j < SHA_DIGEST_LENGTH/4; j++) + res.quadbytes[j] ^= Und.quadbytes[j]; + memcpy(Ust, Und.bytes, sizeof(Ust)); + } + + lua_pushlstring(L, (char*)res.bytes, SHA_DIGEST_LENGTH); + + return 1; +} static const luaL_Reg Reg[] = { - { "sha1", Lsha1 }, - { "sha256", Lsha256 }, - { "md5", Lmd5 }, - { NULL, NULL } + { "sha1", Lsha1 }, + { "sha224", Lsha224 }, + { "sha256", Lsha256 }, + { "sha384", Lsha384 }, + { "sha512", Lsha512 }, + { "md5", Lmd5 }, + { "hmac_sha1", Lhmac_sha1 }, + { "hmac_sha256", Lhmac_sha256 }, + { "hmac_sha512", Lhmac_sha512 }, + { "hmac_md5", Lhmac_md5 }, + { "scram_Hi_sha1", LscramHi }, + { NULL, NULL } }; LUALIB_API int luaopen_util_hashes(lua_State *L) diff --git a/util-src/net.c b/util-src/net.c new file mode 100644 index 00000000..e307c628 --- /dev/null +++ b/util-src/net.c @@ -0,0 +1,117 @@ +/* Prosody IM +-- +-- This project is MIT/X11 licensed. Please see the +-- COPYING file in the source package for more information. +-- +-- Copyright (C) 2012 Paul Aurich +-- Copyright (C) 2013 Matthew Wild +-- Copyright (C) 2013 Florian Zeitz +-- +*/ + +#include <stddef.h> +#include <string.h> +#include <errno.h> + +#ifndef _WIN32 + #include <sys/ioctl.h> + #include <sys/types.h> + #include <sys/socket.h> + #include <net/if.h> + #include <ifaddrs.h> + #include <arpa/inet.h> + #include <netinet/in.h> +#endif + +#include <lua.h> +#include <lauxlib.h> + +/* Enumerate all locally configured IP addresses */ + +const char * const type_strings[] = { + "both", + "ipv4", + "ipv6", + NULL +}; + +static int lc_local_addresses(lua_State *L) +{ +#ifndef _WIN32 + /* Link-local IPv4 addresses; see RFC 3927 and RFC 5735 */ + const long ip4_linklocal = htonl(0xa9fe0000); /* 169.254.0.0 */ + const long ip4_mask = htonl(0xffff0000); + struct ifaddrs *addr = NULL, *a; +#endif + int n = 1; + int type = luaL_checkoption(L, 1, "both", type_strings); + const char link_local = lua_toboolean(L, 2); /* defaults to 0 (false) */ + const char ipv4 = (type == 0 || type == 1); + const char ipv6 = (type == 0 || type == 2); + +#ifndef _WIN32 + if (getifaddrs(&addr) < 0) { + lua_pushnil(L); + lua_pushfstring(L, "getifaddrs failed (%d): %s", errno, + strerror(errno)); + return 2; + } +#endif + lua_newtable(L); + +#ifndef _WIN32 + for (a = addr; a; a = a->ifa_next) { + int family; + char ipaddr[INET6_ADDRSTRLEN]; + const char *tmp = NULL; + + if (a->ifa_addr == NULL || a->ifa_flags & IFF_LOOPBACK) + continue; + + family = a->ifa_addr->sa_family; + + if (ipv4 && family == AF_INET) { + struct sockaddr_in *sa = (struct sockaddr_in *)a->ifa_addr; + if (!link_local &&((sa->sin_addr.s_addr & ip4_mask) == ip4_linklocal)) + continue; + tmp = inet_ntop(family, &sa->sin_addr, ipaddr, sizeof(ipaddr)); + } else if (ipv6 && family == AF_INET6) { + struct sockaddr_in6 *sa = (struct sockaddr_in6 *)a->ifa_addr; + if (!link_local && IN6_IS_ADDR_LINKLOCAL(&sa->sin6_addr)) + continue; + if (IN6_IS_ADDR_V4MAPPED(&sa->sin6_addr) || IN6_IS_ADDR_V4COMPAT(&sa->sin6_addr)) + continue; + tmp = inet_ntop(family, &sa->sin6_addr, ipaddr, sizeof(ipaddr)); + } + + if (tmp != NULL) { + lua_pushstring(L, tmp); + lua_rawseti(L, -2, n++); + } + /* TODO: Error reporting? */ + } + + freeifaddrs(addr); +#else + if (ipv4) { + lua_pushstring(L, "0.0.0.0"); + lua_rawseti(L, -2, n++); + } + if (ipv6) { + lua_pushstring(L, "::"); + lua_rawseti(L, -2, n++); + } +#endif + return 1; +} + +int luaopen_util_net(lua_State* L) +{ + luaL_Reg exports[] = { + { "local_addresses", lc_local_addresses }, + { NULL, NULL } + }; + + luaL_register(L, "net", exports); + return 1; +} diff --git a/util-src/pposix.c b/util-src/pposix.c index ffd21288..8ad167b7 100644 --- a/util-src/pposix.c +++ b/util-src/pposix.c @@ -13,7 +13,7 @@ * POSIX support functions for Lua */ -#define MODULE_VERSION "0.3.5" +#define MODULE_VERSION "0.3.6" #include <stdlib.h> #include <math.h> @@ -32,8 +32,19 @@ #include <string.h> #include <errno.h> #include "lua.h" +#include "lualib.h" #include "lauxlib.h" +#include <fcntl.h> +#if defined(__linux__) && defined(_GNU_SOURCE) +#include <linux/falloc.h> +#endif + +#if (defined(_SVID_SOURCE) && !defined(WITHOUT_MALLINFO)) + #include <malloc.h> + #define WITH_MALLINFO +#endif + /* Daemonization support */ static int lc_daemonize(lua_State *L) @@ -78,6 +89,10 @@ static int lc_daemonize(lua_State *L) close(0); close(1); close(2); + /* Make sure accidental use of FDs 0, 1, 2 don't cause weirdness */ + open("/dev/null", O_RDONLY); + open("/dev/null", O_WRONLY); + open("/dev/null", O_WRONLY); /* Final fork, use it wisely */ if(fork()) @@ -189,12 +204,13 @@ int level_constants[] = { }; int lc_syslog_log(lua_State* L) { - int level = luaL_checkoption(L, 1, "notice", level_strings); - level = level_constants[level]; + int level = level_constants[luaL_checkoption(L, 1, "notice", level_strings)]; - luaL_checkstring(L, 2); + if(lua_gettop(L) == 3) + syslog(level, "%s: %s", luaL_checkstring(L, 2), luaL_checkstring(L, 3)); + else + syslog(level, "%s", lua_tostring(L, 2)); - syslog(level, "%s", lua_tostring(L, 2)); return 0; } @@ -395,23 +411,27 @@ int lc_initgroups(lua_State* L) return 2; } ret = initgroups(lua_tostring(L, 1), gid); - switch(errno) + if(ret) + { + switch(errno) + { + case ENOMEM: + lua_pushnil(L); + lua_pushstring(L, "no-memory"); + break; + case EPERM: + lua_pushnil(L); + lua_pushstring(L, "permission-denied"); + break; + default: + lua_pushnil(L); + lua_pushstring(L, "unknown-error"); + } + } + else { - case 0: lua_pushboolean(L, 1); lua_pushnil(L); - break; - case ENOMEM: - lua_pushnil(L); - lua_pushstring(L, "no-memory"); - break; - case EPERM: - lua_pushnil(L); - lua_pushstring(L, "permission-denied"); - break; - default: - lua_pushnil(L); - lua_pushstring(L, "unknown-error"); } return 2; } @@ -465,51 +485,57 @@ int string2resource(const char *s) { if (!strcmp(s, "NPROC")) return RLIMIT_NPROC; if (!strcmp(s, "RSS")) return RLIMIT_RSS; #endif +#ifdef RLIMIT_NICE + if (!strcmp(s, "NICE")) return RLIMIT_NICE; +#endif return -1; } +unsigned long int arg_to_rlimit(lua_State* L, int idx, rlim_t current) { + switch(lua_type(L, idx)) { + case LUA_TSTRING: + if(strcmp(lua_tostring(L, idx), "unlimited") == 0) + return RLIM_INFINITY; + case LUA_TNUMBER: + return lua_tointeger(L, idx); + case LUA_TNONE: + case LUA_TNIL: + return current; + default: + return luaL_argerror(L, idx, "unexpected type"); + } +} + int lc_setrlimit(lua_State *L) { + struct rlimit lim; int arguments = lua_gettop(L); - int softlimit = -1; - int hardlimit = -1; - const char *resource = NULL; int rid = -1; if(arguments < 1 || arguments > 3) { lua_pushboolean(L, 0); lua_pushstring(L, "incorrect-arguments"); + return 2; } - resource = luaL_checkstring(L, 1); - softlimit = luaL_checkinteger(L, 2); - hardlimit = luaL_checkinteger(L, 3); + rid = string2resource(luaL_checkstring(L, 1)); + if (rid == -1) { + lua_pushboolean(L, 0); + lua_pushstring(L, "invalid-resource"); + return 2; + } - rid = string2resource(resource); - if (rid != -1) { - struct rlimit lim; - struct rlimit lim_current; - - if (softlimit < 0 || hardlimit < 0) { - if (getrlimit(rid, &lim_current)) { - lua_pushboolean(L, 0); - lua_pushstring(L, "getrlimit-failed"); - return 2; - } - } + /* Fetch current values to use as defaults */ + if (getrlimit(rid, &lim)) { + lua_pushboolean(L, 0); + lua_pushstring(L, "getrlimit-failed"); + return 2; + } - if (softlimit < 0) lim.rlim_cur = lim_current.rlim_cur; - else lim.rlim_cur = softlimit; - if (hardlimit < 0) lim.rlim_max = lim_current.rlim_max; - else lim.rlim_max = hardlimit; + lim.rlim_cur = arg_to_rlimit(L, 2, lim.rlim_cur); + lim.rlim_max = arg_to_rlimit(L, 3, lim.rlim_max); - if (setrlimit(rid, &lim)) { - lua_pushboolean(L, 0); - lua_pushstring(L, "setrlimit-failed"); - return 2; - } - } else { - /* Unsupported resoucrce. Sorry I'm pretty limited by POSIX standard. */ + if (setrlimit(rid, &lim)) { lua_pushboolean(L, 0); - lua_pushstring(L, "invalid-resource"); + lua_pushstring(L, "setrlimit-failed"); return 2; } lua_pushboolean(L, 1); @@ -528,6 +554,8 @@ int lc_getrlimit(lua_State *L) { return 2; } + + resource = luaL_checkstring(L, 1); rid = string2resource(resource); if (rid != -1) { @@ -543,8 +571,14 @@ int lc_getrlimit(lua_State *L) { return 2; } lua_pushboolean(L, 1); - lua_pushnumber(L, lim.rlim_cur); - lua_pushnumber(L, lim.rlim_max); + if(lim.rlim_cur == RLIM_INFINITY) + lua_pushstring(L, "unlimited"); + else + lua_pushnumber(L, lim.rlim_cur); + if(lim.rlim_max == RLIM_INFINITY) + lua_pushstring(L, "unlimited"); + else + lua_pushnumber(L, lim.rlim_max); return 3; } @@ -577,6 +611,111 @@ 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 + +/* File handle extraction blatantly stolen from + * https://github.com/rrthomas/luaposix/blob/master/lposix.c#L631 + * */ + +#if _XOPEN_SOURCE >= 600 || _POSIX_C_SOURCE >= 200112L || defined(_GNU_SOURCE) +int lc_fallocate(lua_State* L) +{ + off_t offset, len; + FILE *f = *(FILE**) luaL_checkudata(L, 1, LUA_FILEHANDLE); + + offset = luaL_checkinteger(L, 2); + len = luaL_checkinteger(L, 3); + +#if defined(__linux__) && defined(_GNU_SOURCE) + if(fallocate(fileno(f), FALLOC_FL_KEEP_SIZE, offset, len) == 0) + { + lua_pushboolean(L, 1); + return 1; + } + + if(errno != ENOSYS && errno != EOPNOTSUPP) + { + lua_pushnil(L); + lua_pushstring(L, strerror(errno)); + return 2; + } +#else +#warning Only using posix_fallocate() fallback. +#warning Linux fallocate() is strongly recommended if available: recompile with -D_GNU_SOURCE +#warning Note that posix_fallocate() will still be used on filesystems that dont support fallocate() +#endif + + if(posix_fallocate(fileno(f), offset, len) == 0) + { + lua_pushboolean(L, 1); + return 1; + } + else + { + lua_pushnil(L); + lua_pushstring(L, strerror(errno)); + /* 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); + return 2; + } +} +#endif + /* Register functions */ int luaopen_util_pposix(lua_State *L) @@ -608,6 +747,16 @@ int luaopen_util_pposix(lua_State *L) { "uname", lc_uname }, + { "setenv", lc_setenv }, + +#ifdef WITH_MALLINFO + { "meminfo", lc_meminfo }, +#endif + +#if _XOPEN_SOURCE >= 600 || _POSIX_C_SOURCE >= 200112L || defined(_GNU_SOURCE) + { "fallocate", lc_fallocate }, +#endif + { NULL, NULL } }; diff --git a/util-src/windows.c b/util-src/windows.c index e1e07608..121cc471 100644 --- a/util-src/windows.c +++ b/util-src/windows.c @@ -38,15 +38,16 @@ static int Lget_nameservers(lua_State *L) { } return 1; } else { - luaL_error(L, "DnsQueryConfig returned %d", status); - return 0; // unreachable, but prevents a compiler warning + lua_pushnil(L); + lua_pushfstring(L, "DnsQueryConfig returned %d", status); + return 2; } } -static void lassert(lua_State *L, BOOL test, char* string) { - if (!test) { - luaL_error(L, "%s: %d", string, GetLastError()); - } +static int lerror(lua_State *L, char* string) { + lua_pushnil(L); + lua_pushfstring(L, "%s: %d", string, GetLastError()); + return 2; } static int Lget_consolecolor(lua_State *L) { @@ -55,9 +56,9 @@ static int Lget_consolecolor(lua_State *L) { CONSOLE_SCREEN_BUFFER_INFO info; - lassert(L, console != INVALID_HANDLE_VALUE, "GetStdHandle"); - lassert(L, GetConsoleScreenBufferInfo(console, &info), "GetConsoleScreenBufferInfo"); - lassert(L, ReadConsoleOutputAttribute(console, &color, sizeof(WORD), info.dwCursorPosition, &read_len), "ReadConsoleOutputAttribute"); + if (console == INVALID_HANDLE_VALUE) return lerror(L, "GetStdHandle"); + if (!GetConsoleScreenBufferInfo(console, &info)) return lerror(L, "GetConsoleScreenBufferInfo"); + if (!ReadConsoleOutputAttribute(console, &color, sizeof(WORD), info.dwCursorPosition, &read_len)) return lerror(L, "ReadConsoleOutputAttribute"); lua_pushnumber(L, color); return 1; @@ -65,9 +66,10 @@ static int Lget_consolecolor(lua_State *L) { static int Lset_consolecolor(lua_State *L) { int color = luaL_checkint(L, 1); HWND console = GetStdHandle(STD_OUTPUT_HANDLE); - lassert(L, console != INVALID_HANDLE_VALUE, "GetStdHandle"); - lassert(L, SetConsoleTextAttribute(console, color), "SetConsoleTextAttribute"); - return 0; + if (console == INVALID_HANDLE_VALUE) return lerror(L, "GetStdHandle"); + if (!SetConsoleTextAttribute(console, color)) return lerror(L, "SetConsoleTextAttribute"); + lua_pushboolean(L, 1); + return 1; } static const luaL_Reg Reg[] = |