comparison plugins/mod_admin_socket.lua @ 11200:bf8f2da84007

Merge 0.11->trunk
author Kim Alvefur <zash@zash.se>
date Thu, 05 Nov 2020 22:31:25 +0100
parents 5265f7fe11dd
children 5373724e08a5
comparison
equal deleted inserted replaced
11199:6c7c50a4de32 11200:bf8f2da84007
1 module:set_global();
2
3 local have_unix, unix = pcall(require, "socket.unix");
4
5 if not have_unix or type(unix) ~= "table" then
6 module:log_status("error", "LuaSocket unix socket support not available or incompatible, ensure it is up to date");
7 return;
8 end
9
10 local server = require "net.server";
11
12 local adminstream = require "util.adminstream";
13
14 local socket_path = module:get_option_path("admin_socket", "prosody.sock", "data");
15
16 local sessions = module:shared("sessions");
17
18 local function fire_admin_event(session, stanza)
19 local event_data = {
20 origin = session, stanza = stanza;
21 };
22 local event_name;
23 if stanza.attr.xmlns then
24 event_name = "admin/"..stanza.attr.xmlns..":"..stanza.name;
25 else
26 event_name = "admin/"..stanza.name;
27 end
28 module:log("debug", "Firing %s", event_name);
29 return module:fire_event(event_name, event_data);
30 end
31
32 module:hook("server-stopping", function ()
33 for _, session in pairs(sessions) do
34 session:close("system-shutdown");
35 end
36 os.remove(socket_path);
37 end);
38
39 --- Unix domain socket management
40
41 local conn, sock;
42
43 local listeners = adminstream.server(sessions, fire_admin_event).listeners;
44
45 local function accept_connection()
46 module:log("debug", "accepting...");
47 local client = sock:accept();
48 if not client then return; end
49 server.wrapclient(client, "unix", 0, listeners, "*a");
50 end
51
52 function module.load()
53 sock = unix.stream();
54 sock:settimeout(0);
55 os.remove(socket_path);
56 assert(sock:bind(socket_path));
57 assert(sock:listen());
58 if server.wrapserver then
59 conn = server.wrapserver(sock, socket_path, 0, listeners);
60 else
61 conn = server.watchfd(sock:getfd(), accept_connection);
62 end
63 end
64
65 function module.unload()
66 if conn then
67 conn:close();
68 end
69 if sock then
70 sock:close();
71 end
72 os.remove(socket_path);
73 end