aboutsummaryrefslogtreecommitdiffstats
path: root/util-src
diff options
context:
space:
mode:
Diffstat (limited to 'util-src')
-rw-r--r--util-src/GNUmakefile (renamed from util-src/Makefile)4
-rw-r--r--util-src/crand.c64
-rw-r--r--util-src/encodings.c2
-rw-r--r--util-src/makefile44
-rw-r--r--util-src/net.c66
-rw-r--r--util-src/poll.c474
-rw-r--r--util-src/pposix.c23
-rw-r--r--util-src/ringbuffer.c42
-rw-r--r--util-src/signal.c3
-rw-r--r--util-src/time.c35
10 files changed, 727 insertions, 30 deletions
diff --git a/util-src/Makefile b/util-src/GNUmakefile
index f18d5a80..f2f9b1bc 100644
--- a/util-src/Makefile
+++ b/util-src/GNUmakefile
@@ -6,7 +6,7 @@ CFLAGS+=-I$(LUA_INCDIR)
INSTALL_DATA=install -m644
TARGET?=../util/
-ALL=encodings.so hashes.so net.so pposix.so signal.so table.so ringbuffer.so
+ALL=encodings.so hashes.so net.so pposix.so signal.so table.so ringbuffer.so time.so poll.so
ifdef RANDOM
ALL+=crand.so
@@ -18,7 +18,7 @@ endif
all: $(ALL)
install: $(ALL)
- $(INSTALL_DATA) $^ $(TARGET)
+ $(INSTALL_DATA) $? $(TARGET)
clean:
rm -f $(ALL) $(patsubst %.so,%.o,$(ALL))
diff --git a/util-src/crand.c b/util-src/crand.c
index 7eea1f2b..160ac1f6 100644
--- a/util-src/crand.c
+++ b/util-src/crand.c
@@ -21,19 +21,22 @@
#define _DEFAULT_SOURCE
-#include "lualib.h"
-#include "lauxlib.h"
-
+#include <stdlib.h>
#include <string.h>
#include <errno.h>
+#include "lualib.h"
+#include "lauxlib.h"
+
#if defined(WITH_GETRANDOM)
#ifndef __GLIBC_PREREQ
+/* Not compiled with glibc at all */
#define __GLIBC_PREREQ(a,b) 0
#endif
#if ! __GLIBC_PREREQ(2,25)
+/* Not compiled with a glibc that provides getrandom() */
#include <unistd.h>
#include <sys/syscall.h>
@@ -49,45 +52,66 @@ int getrandom(void *buf, size_t buflen, unsigned int flags) {
#include <sys/random.h>
#endif
-#elif defined(WITH_ARC4RANDOM)
-#include <stdlib.h>
#elif defined(WITH_OPENSSL)
#include <openssl/rand.h>
+#elif defined(WITH_ARC4RANDOM)
+#ifdef __linux__
+#include <bsd/stdlib.h>
+#endif
#else
#error util.crand compiled without a random source
#endif
+#ifndef SMALLBUFSIZ
+#define SMALLBUFSIZ 32
+#endif
+
int Lrandom(lua_State *L) {
- int ret = 0;
- size_t len = (size_t)luaL_checkinteger(L, 1);
- void *buf = lua_newuserdata(L, len);
+ char smallbuf[SMALLBUFSIZ];
+ char *buf = &smallbuf[0];
+ const lua_Integer l = luaL_checkinteger(L, 1);
+ const size_t len = l;
+ luaL_argcheck(L, l >= 0, 1, "must be > 0");
+
+ if(len == 0) {
+ lua_pushliteral(L, "");
+ return 1;
+ }
+
+ if(len > SMALLBUFSIZ) {
+ buf = lua_newuserdata(L, len);
+ }
#if defined(WITH_GETRANDOM)
/*
* This acts like a read from /dev/urandom with the exception that it
* *does* block if the entropy pool is not yet initialized.
*/
- ret = getrandom(buf, len, 0);
+ int left = len;
+ char *p = buf;
- if(ret < 0) {
- lua_pushstring(L, strerror(errno));
- return lua_error(L);
- }
+ do {
+ int ret = getrandom(p, left, 0);
+
+ if(ret < 0) {
+ lua_pushstring(L, strerror(errno));
+ return lua_error(L);
+ }
+
+ p += ret;
+ left -= ret;
+ } while(left > 0);
#elif defined(WITH_ARC4RANDOM)
arc4random_buf(buf, len);
- ret = len;
#elif defined(WITH_OPENSSL)
+
if(!RAND_status()) {
lua_pushliteral(L, "OpenSSL PRNG not seeded");
return lua_error(L);
}
- ret = RAND_bytes(buf, len);
-
- if(ret == 1) {
- ret = len;
- } else {
+ if(RAND_bytes((unsigned char *)buf, len) != 1) {
/* TODO ERR_get_error() */
lua_pushstring(L, "RAND_bytes() failed");
return lua_error(L);
@@ -95,7 +119,7 @@ int Lrandom(lua_State *L) {
#endif
- lua_pushlstring(L, buf, ret);
+ lua_pushlstring(L, buf, len);
return 1;
}
diff --git a/util-src/encodings.c b/util-src/encodings.c
index d85c7cf6..f7e8131f 100644
--- a/util-src/encodings.c
+++ b/util-src/encodings.c
@@ -79,9 +79,11 @@ static void base64_decode(luaL_Buffer *b, int c1, int c2, int c3, int c4, int n)
switch(--n) {
case 3:
s[2] = (char) tuple;
+ /* Falls through. */
case 2:
s[1] = (char)(tuple >> 8);
+ /* Falls through. */
case 1:
s[0] = (char)(tuple >> 16);
diff --git a/util-src/makefile b/util-src/makefile
new file mode 100644
index 00000000..4db95abe
--- /dev/null
+++ b/util-src/makefile
@@ -0,0 +1,44 @@
+include ../config.unix
+
+CFLAGS+=-I$(LUA_INCDIR)
+
+INSTALL_DATA=install -m644
+TARGET?=../util/
+
+ALL=encodings.so hashes.so net.so pposix.so signal.so table.so ringbuffer.so time.so
+
+.ifdef $(RANDOM)
+ALL+=crand.so
+.endif
+
+.PHONY: all install clean
+.SUFFIXES: .c .o .so
+
+all: $(ALL)
+
+install: $(ALL)
+ $(INSTALL_DATA) $(ALL) $(TARGET)
+
+clean:
+ rm -f $(ALL) $(patsubst %.so,%.o,$(ALL))
+
+encodings.so: encodings.o
+ $(LD) $(LDFLAGS) -o $@ $< $(LDLIBS) $(IDNA_LIBS)
+
+hashes.so: hashes.o
+ $(LD) $(LDFLAGS) -o $@ $< $(LDLIBS) $(OPENSSL_LIBS)
+
+crand.o: crand.c
+ $(CC) $(CFLAGS) -DWITH_$(RANDOM) -c -o $@ $<
+
+crand.so: crand.o
+ $(LD) $(LDFLAGS) -o $@ $< $(LDLIBS) $(RANDOM_LIBS)
+
+%.so: %.o
+ $(LD) $(LDFLAGS) -o $@ $< $(LDLIBS)
+
+.c.o:
+ $(CC) $(CFLAGS) -c -o $@ $<
+
+.o.so:
+ $(LD) $(LDFLAGS) -o $@ $< $(LDLIBS)
diff --git a/util-src/net.c b/util-src/net.c
index bb159d57..5f706d81 100644
--- a/util-src/net.c
+++ b/util-src/net.c
@@ -9,7 +9,10 @@
--
*/
+#ifndef _GNU_SOURCE
#define _GNU_SOURCE
+#endif
+
#include <stddef.h>
#include <string.h>
#include <errno.h>
@@ -125,12 +128,75 @@ static int lc_local_addresses(lua_State *L) {
return 1;
}
+static int lc_pton(lua_State *L) {
+ char buf[16];
+ const char *ipaddr = luaL_checkstring(L, 1);
+ int errno_ = 0;
+ int family = strchr(ipaddr, ':') ? AF_INET6 : AF_INET;
+
+ switch(inet_pton(family, ipaddr, &buf)) {
+ case 1:
+ lua_pushlstring(L, buf, family == AF_INET6 ? 16 : 4);
+ return 1;
+
+ case -1:
+ errno_ = errno;
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(errno_));
+ lua_pushinteger(L, errno_);
+ return 3;
+
+ default:
+ case 0:
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(EINVAL));
+ lua_pushinteger(L, EINVAL);
+ return 3;
+ }
+
+}
+
+static int lc_ntop(lua_State *L) {
+ char buf[INET6_ADDRSTRLEN];
+ int family;
+ int errno_;
+ size_t l;
+ const char *ipaddr = luaL_checklstring(L, 1, &l);
+
+ if(l == 16) {
+ family = AF_INET6;
+ }
+ else if(l == 4) {
+ family = AF_INET;
+ }
+ else {
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(EAFNOSUPPORT));
+ lua_pushinteger(L, EAFNOSUPPORT);
+ return 3;
+ }
+
+ if(!inet_ntop(family, ipaddr, buf, INET6_ADDRSTRLEN))
+ {
+ errno_ = errno;
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(errno_));
+ lua_pushinteger(L, errno_);
+ return 3;
+ }
+
+ lua_pushstring(L, (const char *)(&buf));
+ return 1;
+}
+
int luaopen_util_net(lua_State *L) {
#if (LUA_VERSION_NUM > 501)
luaL_checkversion(L);
#endif
luaL_Reg exports[] = {
{ "local_addresses", lc_local_addresses },
+ { "pton", lc_pton },
+ { "ntop", lc_ntop },
{ NULL, NULL }
};
diff --git a/util-src/poll.c b/util-src/poll.c
new file mode 100644
index 00000000..0ca0cf28
--- /dev/null
+++ b/util-src/poll.c
@@ -0,0 +1,474 @@
+
+/*
+ * Lua polling library
+ * Copyright (C) 2017-2018 Kim Alvefur
+ *
+ * This project is MIT licensed. Please see the
+ * COPYING file in the source package for more information.
+ *
+ */
+
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+
+#ifdef __linux__
+#define USE_EPOLL
+#endif
+
+#ifdef USE_EPOLL
+#include <sys/epoll.h>
+#ifndef MAX_EVENTS
+#define MAX_EVENTS 64
+#endif
+#else
+#include <sys/select.h>
+#endif
+
+#include <lualib.h>
+#include <lauxlib.h>
+
+#ifdef USE_EPOLL
+#define STATE_MT "util.poll<epoll>"
+#else
+#define STATE_MT "util.poll<select>"
+#endif
+
+#if (LUA_VERSION_NUM == 501)
+#define luaL_setmetatable(L, tname) luaL_getmetatable(L, tname); lua_setmetatable(L, -2)
+#endif
+
+/*
+ * Structure to keep state for each type of API
+ */
+typedef struct Lpoll_state {
+ int processed;
+#ifdef USE_EPOLL
+ int epoll_fd;
+ struct epoll_event events[MAX_EVENTS];
+#else
+ fd_set wantread;
+ fd_set wantwrite;
+ fd_set readable;
+ fd_set writable;
+ fd_set all;
+ fd_set err;
+#endif
+} Lpoll_state;
+
+/*
+ * Add an FD to be watched
+ */
+int Ladd(lua_State *L) {
+ struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
+ int fd = luaL_checkinteger(L, 2);
+
+ int wantread = lua_toboolean(L, 3);
+ int wantwrite = lua_toboolean(L, 4);
+
+ if(fd < 0) {
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(EBADF));
+ lua_pushinteger(L, EBADF);
+ return 3;
+ }
+
+#ifdef USE_EPOLL
+ struct epoll_event event;
+ event.data.fd = fd;
+ event.events = (wantread ? EPOLLIN : 0) | (wantwrite ? EPOLLOUT : 0);
+
+ event.events |= EPOLLERR | EPOLLHUP | EPOLLRDHUP;
+
+ int ret = epoll_ctl(state->epoll_fd, EPOLL_CTL_ADD, fd, &event);
+
+ if(ret < 0) {
+ ret = errno;
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(ret));
+ lua_pushinteger(L, ret);
+ return 3;
+ }
+
+ lua_pushboolean(L, 1);
+ return 1;
+
+#else
+
+ if(fd > FD_SETSIZE) {
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(EBADF));
+ lua_pushinteger(L, EBADF);
+ return 3;
+ }
+
+ if(FD_ISSET(fd, &state->all)) {
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(EEXIST));
+ lua_pushinteger(L, EEXIST);
+ return 3;
+ }
+
+ FD_CLR(fd, &state->readable);
+ FD_CLR(fd, &state->writable);
+ FD_CLR(fd, &state->err);
+
+ FD_SET(fd, &state->all);
+
+ if(wantread) {
+ FD_SET(fd, &state->wantread);
+ }
+ else {
+ FD_CLR(fd, &state->wantread);
+ }
+
+ if(wantwrite) {
+ FD_SET(fd, &state->wantwrite);
+ }
+ else {
+ FD_CLR(fd, &state->wantwrite);
+ }
+
+ lua_pushboolean(L, 1);
+ return 1;
+#endif
+}
+
+/*
+ * Set events to watch for, readable and/or writable
+ */
+int Lset(lua_State *L) {
+ struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
+ int fd = luaL_checkinteger(L, 2);
+
+#ifdef USE_EPOLL
+
+ int wantread = lua_toboolean(L, 3);
+ int wantwrite = lua_toboolean(L, 4);
+
+ struct epoll_event event;
+ event.data.fd = fd;
+ event.events = (wantread ? EPOLLIN : 0) | (wantwrite ? EPOLLOUT : 0);
+
+ event.events |= EPOLLERR | EPOLLHUP | EPOLLRDHUP;
+
+ int ret = epoll_ctl(state->epoll_fd, EPOLL_CTL_MOD, fd, &event);
+
+ if(ret == 0) {
+ lua_pushboolean(L, 1);
+ return 1;
+ }
+ else {
+ ret = errno;
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(ret));
+ lua_pushinteger(L, ret);
+ return 3;
+ }
+
+#else
+
+ if(!FD_ISSET(fd, &state->all)) {
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(ENOENT));
+ lua_pushinteger(L, ENOENT);
+ }
+
+ if(!lua_isnoneornil(L, 3)) {
+ if(lua_toboolean(L, 3)) {
+ FD_SET(fd, &state->wantread);
+ }
+ else {
+ FD_CLR(fd, &state->wantread);
+ }
+ }
+
+ if(!lua_isnoneornil(L, 4)) {
+ if(lua_toboolean(L, 4)) {
+ FD_SET(fd, &state->wantwrite);
+ }
+ else {
+ FD_CLR(fd, &state->wantwrite);
+ }
+ }
+
+ lua_pushboolean(L, 1);
+ return 1;
+#endif
+}
+
+/*
+ * Remove FDs
+ */
+int Ldel(lua_State *L) {
+ struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
+ int fd = luaL_checkinteger(L, 2);
+
+#ifdef USE_EPOLL
+
+ struct epoll_event event;
+ event.data.fd = fd;
+
+ int ret = epoll_ctl(state->epoll_fd, EPOLL_CTL_DEL, fd, &event);
+
+ if(ret == 0) {
+ lua_pushboolean(L, 1);
+ return 1;
+ }
+ else {
+ ret = errno;
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(ret));
+ lua_pushinteger(L, ret);
+ return 3;
+ }
+
+#else
+
+ if(!FD_ISSET(fd, &state->all)) {
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(ENOENT));
+ lua_pushinteger(L, ENOENT);
+ }
+
+ FD_CLR(fd, &state->wantread);
+ FD_CLR(fd, &state->wantwrite);
+ FD_CLR(fd, &state->readable);
+ FD_CLR(fd, &state->writable);
+ FD_CLR(fd, &state->all);
+ FD_CLR(fd, &state->err);
+
+ lua_pushboolean(L, 1);
+ return 1;
+#endif
+}
+
+
+/*
+ * Check previously manipulated event state for FDs ready for reading or writing
+ */
+int Lpushevent(lua_State *L, struct Lpoll_state *state) {
+#ifdef USE_EPOLL
+
+ if(state->processed > 0) {
+ state->processed--;
+ struct epoll_event event = state->events[state->processed];
+ lua_pushinteger(L, event.data.fd);
+ lua_pushboolean(L, event.events & (EPOLLIN | EPOLLHUP | EPOLLRDHUP | EPOLLERR));
+ lua_pushboolean(L, event.events & EPOLLOUT);
+ return 3;
+ }
+
+#else
+
+ for(int fd = state->processed + 1; fd < FD_SETSIZE; fd++) {
+ if(FD_ISSET(fd, &state->readable) || FD_ISSET(fd, &state->writable) || FD_ISSET(fd, &state->err)) {
+ lua_pushinteger(L, fd);
+ lua_pushboolean(L, FD_ISSET(fd, &state->readable) | FD_ISSET(fd, &state->err));
+ lua_pushboolean(L, FD_ISSET(fd, &state->writable));
+ FD_CLR(fd, &state->readable);
+ FD_CLR(fd, &state->writable);
+ FD_CLR(fd, &state->err);
+ state->processed = fd;
+ return 3;
+ }
+ }
+
+#endif
+ return 0;
+}
+
+/*
+ * Wait for event
+ */
+int Lwait(lua_State *L) {
+ struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
+
+ int ret = Lpushevent(L, state);
+
+ if(ret != 0) {
+ return ret;
+ }
+
+ lua_Number timeout = luaL_checknumber(L, 2);
+ luaL_argcheck(L, timeout >= 0, 1, "positive number expected");
+
+#ifdef USE_EPOLL
+ ret = epoll_wait(state->epoll_fd, state->events, MAX_EVENTS, timeout * 1000);
+#else
+ /*
+ * select(2) mutates the fd_sets passed to it so in order to not
+ * have to recreate it manually every time a copy is made.
+ */
+ memcpy(&state->readable, &state->wantread, sizeof(fd_set));
+ memcpy(&state->writable, &state->wantwrite, sizeof(fd_set));
+ memcpy(&state->err, &state->all, sizeof(fd_set));
+
+ struct timeval tv;
+ tv.tv_sec = (time_t)timeout;
+ tv.tv_usec = ((suseconds_t)(timeout * 1000000)) % 1000000;
+
+ ret = select(FD_SETSIZE, &state->readable, &state->writable, &state->err, &tv);
+#endif
+
+ if(ret == 0) {
+ lua_pushnil(L);
+ lua_pushstring(L, "timeout");
+ return 2;
+ }
+ else if(ret < 0 && errno == EINTR) {
+ lua_pushnil(L);
+ lua_pushstring(L, "signal");
+ return 2;
+ }
+ else if(ret < 0) {
+ ret = errno;
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(ret));
+ lua_pushinteger(L, ret);
+ return 3;
+ }
+
+ /*
+ * Search for the first ready FD and return it
+ */
+#ifdef USE_EPOLL
+ state->processed = ret;
+#else
+ state->processed = -1;
+#endif
+ return Lpushevent(L, state);
+}
+
+#ifdef USE_EPOLL
+/*
+ * Return Epoll FD
+ */
+int Lgetfd(lua_State *L) {
+ struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
+ lua_pushinteger(L, state->epoll_fd);
+ return 1;
+}
+
+/*
+ * Close epoll FD
+ */
+int Lgc(lua_State *L) {
+ struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
+
+ if(state->epoll_fd == -1) {
+ return 0;
+ }
+
+ if(close(state->epoll_fd) == 0) {
+ state->epoll_fd = -1;
+ }
+ else {
+ lua_pushstring(L, strerror(errno));
+ lua_error(L);
+ }
+
+ return 0;
+}
+#endif
+
+/*
+ * String representation
+ */
+int Ltos(lua_State *L) {
+ struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
+ lua_pushfstring(L, "%s: %p", STATE_MT, state);
+ return 1;
+}
+
+/*
+ * Create a new context
+ */
+int Lnew(lua_State *L) {
+ /* Allocate state */
+ Lpoll_state *state = lua_newuserdata(L, sizeof(Lpoll_state));
+ luaL_setmetatable(L, STATE_MT);
+
+ /* Initialize state */
+#ifdef USE_EPOLL
+ state->epoll_fd = -1;
+ state->processed = 0;
+
+ int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
+
+ if(epoll_fd <= 0) {
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(errno));
+ lua_pushinteger(L, errno);
+ return 3;
+ }
+
+ state->epoll_fd = epoll_fd;
+#else
+ FD_ZERO(&state->wantread);
+ FD_ZERO(&state->wantwrite);
+ FD_ZERO(&state->readable);
+ FD_ZERO(&state->writable);
+ FD_ZERO(&state->all);
+ FD_ZERO(&state->err);
+ state->processed = FD_SETSIZE;
+#endif
+
+ return 1;
+}
+
+/*
+ * Open library
+ */
+int luaopen_util_poll(lua_State *L) {
+#if (LUA_VERSION_NUM > 501)
+ luaL_checkversion(L);
+#endif
+
+ luaL_newmetatable(L, STATE_MT);
+ {
+
+ lua_pushliteral(L, STATE_MT);
+ lua_setfield(L, -2, "__name");
+
+ lua_pushcfunction(L, Ltos);
+ lua_setfield(L, -2, "__tostring");
+
+ lua_createtable(L, 0, 2);
+ {
+ lua_pushcfunction(L, Ladd);
+ lua_setfield(L, -2, "add");
+ lua_pushcfunction(L, Lset);
+ lua_setfield(L, -2, "set");
+ lua_pushcfunction(L, Ldel);
+ lua_setfield(L, -2, "del");
+ lua_pushcfunction(L, Lwait);
+ lua_setfield(L, -2, "wait");
+#ifdef USE_EPOLL
+ lua_pushcfunction(L, Lgetfd);
+ lua_setfield(L, -2, "getfd");
+#endif
+ }
+ lua_setfield(L, -2, "__index");
+
+#ifdef USE_EPOLL
+ lua_pushcfunction(L, Lgc);
+ lua_setfield(L, -2, "__gc");
+#endif
+ }
+
+ lua_createtable(L, 0, 3);
+ {
+ lua_pushcfunction(L, Lnew);
+ lua_setfield(L, -2, "new");
+
+#define push_errno(named_error) lua_pushinteger(L, named_error);\
+ lua_setfield(L, -2, #named_error);
+
+ push_errno(EEXIST);
+ push_errno(ENOENT);
+
+ }
+ return 1;
+}
+
diff --git a/util-src/pposix.c b/util-src/pposix.c
index d3b1e5e6..5c926603 100644
--- a/util-src/pposix.c
+++ b/util-src/pposix.c
@@ -17,14 +17,22 @@
#if defined(__linux__)
+#ifndef _GNU_SOURCE
#define _GNU_SOURCE
+#endif
#else
+#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE
#endif
+#endif
#if defined(__APPLE__)
+#ifndef _DARWIN_C_SOURCE
#define _DARWIN_C_SOURCE
#endif
+#endif
+#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200809L
+#endif
#include <stdlib.h>
#include <math.h>
@@ -55,7 +63,7 @@
#include <linux/falloc.h>
#endif
-#if !defined(WITHOUT_MALLINFO) && defined(__linux__)
+#if !defined(WITHOUT_MALLINFO) && defined(__linux__) && defined(__GLIBC__)
#include <malloc.h>
#define WITH_MALLINFO
#endif
@@ -107,14 +115,10 @@ static int lc_daemonize(lua_State *L) {
return 2;
}
- /* Close stdin, stdout, stderr */
- 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);
+ freopen("/dev/null", "r", stdin);
+ freopen("/dev/null", "w", stdout);
+ freopen("/dev/null", "w", stderr);
/* Final fork, use it wisely */
if(fork()) {
@@ -238,6 +242,7 @@ int lc_syslog_log(lua_State *L) {
}
int lc_syslog_close(lua_State *L) {
+ (void)L;
closelog();
if(syslog_ident) {
@@ -552,6 +557,7 @@ rlim_t arg_to_rlimit(lua_State *L, int idx, rlim_t current) {
if(strcmp(lua_tostring(L, idx), "unlimited") == 0) {
return RLIM_INFINITY;
}
+ return luaL_argerror(L, idx, "unexpected type");
case LUA_TNUMBER:
return lua_tointeger(L, idx);
@@ -650,6 +656,7 @@ int lc_getrlimit(lua_State *L) {
}
int lc_abort(lua_State *L) {
+ (void)L;
abort();
return 0;
}
diff --git a/util-src/ringbuffer.c b/util-src/ringbuffer.c
index 8d9e49e6..8f9013f7 100644
--- a/util-src/ringbuffer.c
+++ b/util-src/ringbuffer.c
@@ -39,10 +39,12 @@ int find(ringbuffer *b, const char *s, size_t l) {
return 0;
}
+ /* look for a matching first byte */
for(i = 0; i <= b->blen - l; i++) {
if(b->buffer[(b->rpos + i) % b->alen] == *s) {
m = 1;
+ /* check if the following byte also match */
for(j = 1; j < l; j++)
if(b->buffer[(b->rpos + i + j) % b->alen] != s[j]) {
m = 0;
@@ -58,6 +60,10 @@ int find(ringbuffer *b, const char *s, size_t l) {
return 0;
}
+/*
+ * Find first position of a substring in buffer
+ * (buffer, string) -> number
+ */
int rb_find(lua_State *L) {
size_t l, m;
ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
@@ -72,6 +78,31 @@ int rb_find(lua_State *L) {
return 0;
}
+/*
+ * Move read position forward without returning the data
+ * (buffer, number) -> boolean
+ */
+int rb_discard(lua_State *L) {
+ ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
+ size_t r = luaL_checkinteger(L, 2);
+
+ if(r > b->blen) {
+ lua_pushboolean(L, 0);
+ return 1;
+ }
+
+ b->blen -= r;
+ b->rpos += r;
+ modpos(b);
+
+ lua_pushboolean(L, 1);
+ return 1;
+}
+
+/*
+ * Read bytes from buffer
+ * (buffer, number, boolean?) -> string
+ */
int rb_read(lua_State *L) {
ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
size_t r = luaL_checkinteger(L, 2);
@@ -83,6 +114,7 @@ int rb_read(lua_State *L) {
}
if((b->rpos + r) > b->alen) {
+ /* Substring wraps around to the beginning of the buffer */
lua_pushlstring(L, &b->buffer[b->rpos], b->alen - b->rpos);
lua_pushlstring(L, b->buffer, r - (b->alen - b->rpos));
lua_concat(L, 2);
@@ -99,6 +131,10 @@ int rb_read(lua_State *L) {
return 1;
}
+/*
+ * Read buffer until first occurrence of a substring
+ * (buffer, string) -> string
+ */
int rb_readuntil(lua_State *L) {
size_t l, m;
ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
@@ -114,6 +150,10 @@ int rb_readuntil(lua_State *L) {
return 0;
}
+/*
+ * Write bytes into the buffer
+ * (buffer, string) -> integer
+ */
int rb_write(lua_State *L) {
size_t l, w = 0;
ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
@@ -191,6 +231,8 @@ int luaopen_util_ringbuffer(lua_State *L) {
{
lua_pushcfunction(L, rb_find);
lua_setfield(L, -2, "find");
+ lua_pushcfunction(L, rb_discard);
+ lua_setfield(L, -2, "discard");
lua_pushcfunction(L, rb_read);
lua_setfield(L, -2, "read");
lua_pushcfunction(L, rb_readuntil);
diff --git a/util-src/signal.c b/util-src/signal.c
index 9e6f6f63..c696a3a2 100644
--- a/util-src/signal.c
+++ b/util-src/signal.c
@@ -26,7 +26,9 @@
* OTHER DEALINGS IN THE SOFTWARE.
*/
+#ifndef _GNU_SOURCE
#define _GNU_SOURCE
+#endif
#include <signal.h>
#include <stdlib.h>
@@ -166,6 +168,7 @@ int signals[MAX_PENDING_SIGNALS];
int nsig = 0;
static void sighook(lua_State *L, lua_Debug *ar) {
+ (void)ar;
/* restore the old hook */
lua_sethook(L, Hsig, Hmask, Hcount);
diff --git a/util-src/time.c b/util-src/time.c
new file mode 100644
index 00000000..bfad52ee
--- /dev/null
+++ b/util-src/time.c
@@ -0,0 +1,35 @@
+#ifndef _POSIX_C_SOURCE
+#define _POSIX_C_SOURCE 199309L
+#endif
+
+#include <time.h>
+#include <lua.h>
+
+lua_Number tv2number(struct timespec *tv) {
+ return tv->tv_sec + tv->tv_nsec * 1e-9;
+}
+
+int lc_time_realtime(lua_State *L) {
+ struct timespec t;
+ clock_gettime(CLOCK_REALTIME, &t);
+ lua_pushnumber(L, tv2number(&t));
+ return 1;
+}
+
+int lc_time_monotonic(lua_State *L) {
+ struct timespec t;
+ clock_gettime(CLOCK_MONOTONIC, &t);
+ lua_pushnumber(L, tv2number(&t));
+ return 1;
+}
+
+int luaopen_util_time(lua_State *L) {
+ lua_createtable(L, 0, 2);
+ {
+ lua_pushcfunction(L, lc_time_realtime);
+ lua_setfield(L, -2, "now");
+ lua_pushcfunction(L, lc_time_monotonic);
+ lua_setfield(L, -2, "monotonic");
+ }
+ return 1;
+}