Software /
code /
prosody
Comparison
util-src/signal.c @ 13439:1e229d710a3c
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.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 24 Feb 2024 00:05:29 +0100 |
parent | 12976:a187600ec7d6 |
child | 13441:6d96b6eeee5a |
comparison
equal
deleted
inserted
replaced
13438:0a0dd2505baa | 13439:1e229d710a3c |
---|---|
30 #define _GNU_SOURCE | 30 #define _GNU_SOURCE |
31 #endif | 31 #endif |
32 | 32 |
33 #include <signal.h> | 33 #include <signal.h> |
34 #include <stdlib.h> | 34 #include <stdlib.h> |
35 #ifdef __linux__ | |
36 #include <unistd.h> | |
37 #include <sys/signalfd.h> | |
38 #endif | |
35 | 39 |
36 #include "lua.h" | 40 #include "lua.h" |
37 #include "lauxlib.h" | 41 #include "lauxlib.h" |
38 | 42 |
39 #if (LUA_VERSION_NUM < 503) | 43 #if (LUA_VERSION_NUM < 503) |
366 return 1; | 370 return 1; |
367 } | 371 } |
368 | 372 |
369 #endif | 373 #endif |
370 | 374 |
375 #ifdef __linux__ | |
376 static int l_signalfd(lua_State *L) { | |
377 sigset_t mask; | |
378 | |
379 sigemptyset(&mask); | |
380 sigaddset(&mask, luaL_checkinteger(L, 1)); | |
381 | |
382 sigprocmask(SIG_BLOCK, &mask, NULL); /* TODO check err */ | |
383 | |
384 lua_pushinteger(L, signalfd(-1, &mask, SFD_NONBLOCK)); | |
385 return 1; | |
386 } | |
387 | |
388 static int l_signalfd_read(lua_State *L) { | |
389 const int sigfd = luaL_checkinteger(L, 1); | |
390 struct signalfd_siginfo siginfo; | |
391 | |
392 if(read(sigfd, &siginfo, sizeof(siginfo)) < 0) { | |
393 return 0; | |
394 } | |
395 | |
396 lua_pushinteger(L, siginfo.ssi_signo); | |
397 return 1; | |
398 } | |
399 #endif | |
400 | |
371 static const struct luaL_Reg lsignal_lib[] = { | 401 static const struct luaL_Reg lsignal_lib[] = { |
372 {"signal", l_signal}, | 402 {"signal", l_signal}, |
373 {"raise", l_raise}, | 403 {"raise", l_raise}, |
374 #if defined(__unix__) || defined(__APPLE__) | 404 #if defined(__unix__) || defined(__APPLE__) |
375 {"kill", l_kill}, | 405 {"kill", l_kill}, |
406 #endif | |
407 #ifdef __linux__ | |
408 {"signalfd", l_signalfd}, | |
409 {"signalfd_read", l_signalfd_read}, | |
376 #endif | 410 #endif |
377 {NULL, NULL} | 411 {NULL, NULL} |
378 }; | 412 }; |
379 | 413 |
380 int luaopen_prosody_util_signal(lua_State *L) { | 414 int luaopen_prosody_util_signal(lua_State *L) { |