Mercurial > prosody-hg
comparison net/server_epoll.lua @ 13440:b27de3d2bad6
net.server_epoll: Support hooking signals via signalfd
Handling signal events the same way as all other events makes sense and
seems safer than the signal handling just jumping around in C and
messing with Lua states.
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Sat, 24 Feb 2024 00:20:35 +0100 |
| parents | 3c80124452ed |
| children | 6d96b6eeee5a |
comparison
equal
deleted
inserted
replaced
| 13439:1e229d710a3c | 13440:b27de3d2bad6 |
|---|---|
| 27 local _SOCKETINVALID = socket._SOCKETINVALID or -1; | 27 local _SOCKETINVALID = socket._SOCKETINVALID or -1; |
| 28 local new_id = require "prosody.util.id".short; | 28 local new_id = require "prosody.util.id".short; |
| 29 local xpcall = require "prosody.util.xpcall".xpcall; | 29 local xpcall = require "prosody.util.xpcall".xpcall; |
| 30 local sslconfig = require "prosody.util.sslconfig"; | 30 local sslconfig = require "prosody.util.sslconfig"; |
| 31 local tls_impl = require "prosody.net.tls_luasec"; | 31 local tls_impl = require "prosody.net.tls_luasec"; |
| 32 local have_signal, signal = pcall(require, "prosody.util.signal"); | |
| 32 | 33 |
| 33 local poller = require "prosody.util.poll" | 34 local poller = require "prosody.util.poll" |
| 34 local EEXIST = poller.EEXIST; | 35 local EEXIST = poller.EEXIST; |
| 35 local ENOENT = poller.ENOENT; | 36 local ENOENT = poller.ENOENT; |
| 36 | 37 |
| 1139 elseif r ~= "signal" then | 1140 elseif r ~= "signal" then |
| 1140 log("debug", "epoll_wait error: %s[%d]", r, w); | 1141 log("debug", "epoll_wait error: %s[%d]", r, w); |
| 1141 end | 1142 end |
| 1142 end | 1143 end |
| 1143 return quitting; | 1144 return quitting; |
| 1145 end | |
| 1146 | |
| 1147 local hook_signal; | |
| 1148 if have_signal and signal.signalfd then | |
| 1149 local function dispatch(self) | |
| 1150 return self:on("signal", signal.signalfd_read(self:getfd())); | |
| 1151 end | |
| 1152 | |
| 1153 function hook_signal(signum, cb) | |
| 1154 local watch = watchfd(signal.signalfd(signum), dispatch); | |
| 1155 watch.listeners = { onsignal = cb }; | |
| 1156 return watch; | |
| 1157 end | |
| 1144 end | 1158 end |
| 1145 | 1159 |
| 1146 return { | 1160 return { |
| 1147 get_backend = function () return "epoll"; end; | 1161 get_backend = function () return "epoll"; end; |
| 1148 addserver = addserver; | 1162 addserver = addserver; |
| 1166 watchfd = watchfd; | 1180 watchfd = watchfd; |
| 1167 link = link; | 1181 link = link; |
| 1168 set_config = function (newconfig) | 1182 set_config = function (newconfig) |
| 1169 cfg = setmetatable(newconfig, default_config); | 1183 cfg = setmetatable(newconfig, default_config); |
| 1170 end; | 1184 end; |
| 1185 hook_signal = hook_signal; | |
| 1171 | 1186 |
| 1172 tls_builder = function(basedir) | 1187 tls_builder = function(basedir) |
| 1173 return sslconfig._new(tls_impl.new_context, basedir) | 1188 return sslconfig._new(tls_impl.new_context, basedir) |
| 1174 end, | 1189 end, |
| 1175 | 1190 |
