# HG changeset patch # User Kim Alvefur # Date 1708729529 -3600 # Node ID 1e229d710a3c872926b740511f87c8864bf28944 # Parent 0a0dd2505baa92d0f7dedce43390e62b0c4cdde0 util.signal: Add support for signalfd(2) on Linux signalfd allows handling signal events using the same method as sockets, via file descriptors. Thus all signal dispatch can go through the same main event loop as everything else, removing need for thread-scary signal handling where execution would just jump to the signal handler regardless of the state of Lua, and needing to keep track of Lua states/threads. diff -r 0a0dd2505baa -r 1e229d710a3c util-src/signal.c --- a/util-src/signal.c Fri Feb 23 22:45:42 2024 +0000 +++ b/util-src/signal.c Sat Feb 24 00:05:29 2024 +0100 @@ -32,6 +32,10 @@ #include #include +#ifdef __linux__ +#include +#include +#endif #include "lua.h" #include "lauxlib.h" @@ -368,12 +372,42 @@ #endif +#ifdef __linux__ +static int l_signalfd(lua_State *L) { + sigset_t mask; + + sigemptyset(&mask); + sigaddset(&mask, luaL_checkinteger(L, 1)); + + sigprocmask(SIG_BLOCK, &mask, NULL); /* TODO check err */ + + lua_pushinteger(L, signalfd(-1, &mask, SFD_NONBLOCK)); + return 1; +} + +static int l_signalfd_read(lua_State *L) { + const int sigfd = luaL_checkinteger(L, 1); + struct signalfd_siginfo siginfo; + + if(read(sigfd, &siginfo, sizeof(siginfo)) < 0) { + return 0; + } + + lua_pushinteger(L, siginfo.ssi_signo); + return 1; +} +#endif + static const struct luaL_Reg lsignal_lib[] = { {"signal", l_signal}, {"raise", l_raise}, #if defined(__unix__) || defined(__APPLE__) {"kill", l_kill}, #endif +#ifdef __linux__ + {"signalfd", l_signalfd}, + {"signalfd_read", l_signalfd_read}, +#endif {NULL, NULL} };