Mercurial > prosody-modules
annotate misc/systemd/socket-activation.lua @ 4989:b74d592df9e2
mod_http_muc_log: Remove dead code
This might be something left over since a different variant where the
loop went like `for n = i-1, i-100, -1 do ... end` i.e. it went trough a
fixed number of items instead of all the page until the current message.
Then it would have needed something to stop going over the end, but
since the checks are simple it shouldn't be much of a problem looping
over even a very busy day.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 16 Aug 2022 01:27:59 +0200 |
parents | 3296a09b4e57 |
children |
rev | line source |
---|---|
2352
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 -- Monkeypatch to support socket activation |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 -- |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 -- Requires LuaSocket after "agnostic" changes merged |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 -- |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 -- To enable: |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 -- RunScript "socket-activation.lua" |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 local socket = require"socket"; |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 local tcp_serv_mt = debug.getregistry()["tcp{server}"]; |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 local socket_bind = socket.bind; |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 local SD_LISTEN_FDS_START = 3; |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 local fds = tonumber(os.getenv"LISTEN_FDS") or 0; |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 if fds < SD_LISTEN_FDS_START then return; end |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 local servs = {}; |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 for i = 1, fds do |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 local serv = socket.tcp(); |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 if serv:getfd() >= 0 then |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 return; -- This won't work, we will leak the old FD |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 end |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 debug.setmetatable(serv, tcp_serv_mt); |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 serv:setfd(SD_LISTEN_FDS_START + i - 1); |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 local ip, port = serv:getsockname(); |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 servs [ ip .. ":" .. port ] = serv; |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 end |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 function socket.bind( ip, port, backlog ) |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 local sock = servs [ ip .. ":" .. port ]; |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 if sock then |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 servs [ ip .. ":" .. port ] = nil; |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 return sock; |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 end |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 if next(servs) == nil then |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 -- my work here is done |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 socket.bind = socket_bind; |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 end |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 return socket_bind( ip, port, backlog ); |
3296a09b4e57
misc/systemd: Experimental files for enabling socket activation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 end |