From 13d9b04fb09d39a7204ba1e9cc9c8403fa22efa8 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Mon, 16 Oct 2023 16:35:28 +0200 Subject: uloop: add support for user defined signal handlers Reuse and extend the existing signal waker pipe mechanism to add user defined signal handling functionality to uloop. This commit introduces two new api functions `uloop_signal_add()` and `uloop_signal_remove()` along with a new structure type `uloop_signal` to allow adding and removing arbitrary signal handlers. Registered signal handlers are maintained in a linked list and matched by their signo member value which allows registering multiple handlers for the same signal numbers. Upon registering a new signal handler, the existing handler is saved in the `uloop_signal` structure. When removing the user defined signal handler, the original behavior is restored. The Lua binding has been updated as well to support the new signal handler mechanism. Signed-off-by: Jo-Philipp Wich --- examples/uloop-example.lua | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'examples') diff --git a/examples/uloop-example.lua b/examples/uloop-example.lua index f3aef60..1b73aed 100755 --- a/examples/uloop-example.lua +++ b/examples/uloop-example.lua @@ -63,6 +63,22 @@ uloop.timer( end, 2000 ) +-- SIGINT handler +uloop.signal(function(signo) + print(string.format("Terminating on SIGINT (#%d)!", signo)) + + -- end uloop to terminate program + uloop.cancel() +end, uloop.SIGINT) + +local sig +sig = uloop.signal(function(signo) + print(string.format("Got SIGUSR2 (#%d)!", signo)) + + -- remove signal handler, next SIGUSR2 will terminate program + sig:delete() +end, uloop.SIGUSR2) + -- Keep udp_ev reference, events will be gc'd, even if the callback is still referenced -- .delete will manually untrack. udp_ev = uloop.fd_add(udp, function(ufd, events) -- cgit v1.2.3