diff --git a/mote/v2/openwrt/package/luci/libs/nixio/src/file.c b/mote/v2/openwrt/package/luci/libs/nixio/src/file.c index b86e040..8b73755 100644 --- a/mote/v2/openwrt/package/luci/libs/nixio/src/file.c +++ b/mote/v2/openwrt/package/luci/libs/nixio/src/file.c @@ -223,6 +223,22 @@ static int nixio_file_read(lua_State *L) { } } +static int nixio_file_numexp(lua_State *L) { + int fd = nixio__checkfd(L, 1); + uint64_t numexp; + int readc; + + do { + readc = read(fd, &numexp, sizeof(uint64_t)); + } while (readc == -1 && errno == EINTR); + + if (readc < 0) { + return nixio__perror(L); + } else { + lua_pushnumber(L, (lua_Number)numexp); + return 1; + } +} static int nixio_file_seek(lua_State *L) { int fd = nixio__checkfd(L, 1); @@ -350,6 +366,7 @@ static int nixio_file__tostring(lua_State *L) { static const luaL_reg M[] = { {"write", nixio_file_write}, {"read", nixio_file_read}, + {"numexp", nixio_file_numexp}, {"tell", nixio_file_tell}, {"seek", nixio_file_seek}, {"stat", nixio_file_stat}, diff --git a/mote/v2/openwrt/package/luci/libs/nixio/src/poll.c b/mote/v2/openwrt/package/luci/libs/nixio/src/poll.c index c830db2..6203aa4 100644 --- a/mote/v2/openwrt/package/luci/libs/nixio/src/poll.c +++ b/mote/v2/openwrt/package/luci/libs/nixio/src/poll.c @@ -195,8 +195,57 @@ static int nixio_poll(lua_State *L) { #ifdef __linux__ +#include #include #include +#include + +static int nixio_timerfd(lua_State *L) { + struct itimerspec its; + + its.it_value.tv_sec = (time_t)luaL_optinteger(L, 1, 0); + its.it_value.tv_nsec = (long)luaL_optinteger(L, 2, 0); + + its.it_interval.tv_sec = (time_t)luaL_optinteger(L, 3, 0); + its.it_interval.tv_nsec = (long)luaL_optinteger(L, 4, 0); + + /* Create a timer object and associated fd */ + int fd = timerfd_create(CLOCK_REALTIME, 0); + + if (fd == -1) { + return nixio__perror(L); + } + + /* Workaround for TFD_NONBLOCK 'invalid argument' in uClibc*/ + int flags; + flags = fcntl(fd, F_GETFL); + if (flags == -1) + return nixio__perror(L); + flags |= O_NONBLOCK; + if(fcntl(fd, F_SETFL, flags) == -1) + return nixio__perror(L); + + /* Arm the timer */ + if (timerfd_settime(fd, 0, &its ,NULL) == -1) { + close(fd); + return nixio__perror(L); + } + + /* Create a userdatum for fd */ + int *udata = lua_newuserdata(L, sizeof(int)); + if (!udata) { + close(fd); + return luaL_error(L, "out of memory"); + } + + *udata = fd; + + luaL_getmetatable(L, NIXIO_FILE_META); + lua_setmetatable(L, -2); + + return 1; +} + #include #include @@ -263,6 +312,7 @@ static int nixio_setitimerfd(lua_State *L) { /* module table */ static const luaL_reg R[] = { #ifdef __linux__ + {"timerfd", nixio_timerfd}, {"setitimerfd", nixio_setitimerfd}, #endif {"gettimeofday", nixio_gettimeofday},