# HG changeset patch # User Kim Alvefur # Date 1478560196 -3600 # Node ID 3296a09b4e57e89aca084ea355151e7e6ffb5883 # Parent f8ecb4b248b08b862abc77b7d01546ffc439e719 misc/systemd: Experimental files for enabling socket activation diff -r f8ecb4b248b0 -r 3296a09b4e57 misc/systemd/prosody-c2s.socket --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/misc/systemd/prosody-c2s.socket Tue Nov 08 00:09:56 2016 +0100 @@ -0,0 +1,9 @@ +[Install] +WantedBy=sockets.target + +[Socket] +ListenStream=0.0.0.0:5222 +ListenStream=5222 +BindIPv6Only=ipv6-only +Accept=false +Service=prosody.service diff -r f8ecb4b248b0 -r 3296a09b4e57 misc/systemd/prosody-s2s.socket --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/misc/systemd/prosody-s2s.socket Tue Nov 08 00:09:56 2016 +0100 @@ -0,0 +1,9 @@ +[Install] +WantedBy=sockets.target + +[Socket] +ListenStream=0.0.0.0:5269 +ListenStream=5269 +BindIPv6Only=ipv6-only +Accept=false +Service=prosody.service diff -r f8ecb4b248b0 -r 3296a09b4e57 misc/systemd/socket-activation.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/misc/systemd/socket-activation.lua Tue Nov 08 00:09:56 2016 +0100 @@ -0,0 +1,42 @@ +-- Monkeypatch to support socket activation +-- +-- Requires LuaSocket after "agnostic" changes merged +-- +-- To enable: +-- RunScript "socket-activation.lua" + +local socket = require"socket"; +local tcp_serv_mt = debug.getregistry()["tcp{server}"]; +local socket_bind = socket.bind; + +local SD_LISTEN_FDS_START = 3; + +local fds = tonumber(os.getenv"LISTEN_FDS") or 0; + +if fds < SD_LISTEN_FDS_START then return; end + +local servs = {}; + +for i = 1, fds do + local serv = socket.tcp(); + if serv:getfd() >= 0 then + return; -- This won't work, we will leak the old FD + end + debug.setmetatable(serv, tcp_serv_mt); + serv:setfd(SD_LISTEN_FDS_START + i - 1); + local ip, port = serv:getsockname(); + servs [ ip .. ":" .. port ] = serv; +end + +function socket.bind( ip, port, backlog ) + local sock = servs [ ip .. ":" .. port ]; + if sock then + servs [ ip .. ":" .. port ] = nil; + return sock; + end + if next(servs) == nil then + -- my work here is done + socket.bind = socket_bind; + end + return socket_bind( ip, port, backlog ); +end