Mercurial > prosody-modules
comparison misc/systemd/socket-activation.lua @ 2352:3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 08 Nov 2016 00:09:56 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
2351:f8ecb4b248b0 | 2352:3296a09b4e57 |
---|---|
1 -- Monkeypatch to support socket activation | |
2 -- | |
3 -- Requires LuaSocket after "agnostic" changes merged | |
4 -- | |
5 -- To enable: | |
6 -- RunScript "socket-activation.lua" | |
7 | |
8 local socket = require"socket"; | |
9 local tcp_serv_mt = debug.getregistry()["tcp{server}"]; | |
10 local socket_bind = socket.bind; | |
11 | |
12 local SD_LISTEN_FDS_START = 3; | |
13 | |
14 local fds = tonumber(os.getenv"LISTEN_FDS") or 0; | |
15 | |
16 if fds < SD_LISTEN_FDS_START then return; end | |
17 | |
18 local servs = {}; | |
19 | |
20 for i = 1, fds do | |
21 local serv = socket.tcp(); | |
22 if serv:getfd() >= 0 then | |
23 return; -- This won't work, we will leak the old FD | |
24 end | |
25 debug.setmetatable(serv, tcp_serv_mt); | |
26 serv:setfd(SD_LISTEN_FDS_START + i - 1); | |
27 local ip, port = serv:getsockname(); | |
28 servs [ ip .. ":" .. port ] = serv; | |
29 end | |
30 | |
31 function socket.bind( ip, port, backlog ) | |
32 local sock = servs [ ip .. ":" .. port ]; | |
33 if sock then | |
34 servs [ ip .. ":" .. port ] = nil; | |
35 return sock; | |
36 end | |
37 if next(servs) == nil then | |
38 -- my work here is done | |
39 socket.bind = socket_bind; | |
40 end | |
41 return socket_bind( ip, port, backlog ); | |
42 end |