Software / code / prosody
Comparison
plugins/mod_admin_socket.lua @ 10855:70ac7d23673d
mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Mon, 01 Jun 2020 15:42:19 +0100 |
| child | 10862:1cfae9e85021 |
comparison
equal
deleted
inserted
replaced
| 10854:472fe13a05f9 | 10855:70ac7d23673d |
|---|---|
| 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_string("admin_socket", prosody.paths.data.."/prosody.sock"); | |
| 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 conn = server.watchfd(sock:getfd(), accept_connection); | |
| 59 end | |
| 60 | |
| 61 function module.unload() | |
| 62 if conn then | |
| 63 conn:close(); | |
| 64 end | |
| 65 if sock then | |
| 66 sock:close(); | |
| 67 end | |
| 68 os.remove(socket_path); | |
| 69 end |