3 -- Copyright (C) 2008-2010 Matthew Wild
4 -- Copyright (C) 2008-2010 Waqas Hussain
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
10 -- prosody - main executable for Prosody XMPP server
12 -- Will be modified by configure script if run --
14 CFG_SOURCEDIR=CFG_SOURCEDIR or os.getenv("PROSODY_SRCDIR");
15 CFG_CONFIGDIR=CFG_CONFIGDIR or os.getenv("PROSODY_CFGDIR");
16 CFG_PLUGINDIR=CFG_PLUGINDIR or os.getenv("PROSODY_PLUGINDIR");
17 CFG_DATADIR=CFG_DATADIR or os.getenv("PROSODY_DATADIR");
19 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
21 local function is_relative(path)
22 local path_sep = package.config:sub(1,1);
23 return ((path_sep == "/" and path:sub(1,1) ~= "/")
24 or (path_sep == "\\" and (path:sub(1,1) ~= "/" and path:sub(2,3) ~= ":\\")))
27 -- Tell Lua where to find our libraries
29 local function filter_relative_paths(path)
30 if is_relative(path) then return ""; end
32 local function sanitise_paths(paths)
33 return (paths:gsub("[^;]+;?", filter_relative_paths):gsub(";;+", ";"));
35 package.path = sanitise_paths(CFG_SOURCEDIR.."/?.lua;"..package.path);
36 package.cpath = sanitise_paths(CFG_SOURCEDIR.."/?.so;"..package.cpath);
39 -- Substitute ~ with path to home directory in data path
41 if os.getenv("HOME") then
42 CFG_DATADIR = CFG_DATADIR:gsub("^~", os.getenv("HOME"));
46 if #arg > 0 and arg[1] ~= "--config" then
47 print("Unknown command-line option: "..tostring(arg[1]));
48 print("Perhaps you meant to use prosodyctl instead?");
52 local startup = require "util.startup";
53 local async = require "util.async";
55 -- Note: it's important that this thread is not GC'd, as some C libraries
56 -- that are initialized here store a pointer to it ( :/ ).
57 local thread = async.runner();
59 thread:run(startup.prosody);
62 -- Error handler for errors that make it this far
63 local function catch_uncaught_error(err)
64 if type(err) == "string" and err:match("interrupted!$") then
68 prosody.log("error", "Top-level error, please report:\n%s", tostring(err));
69 local traceback = debug.traceback("", 2);
71 prosody.log("error", "%s", traceback);
74 prosody.events.fire_event("very-bad-error", {error = err, traceback = traceback});
77 local sleep = require"socket".sleep;
78 local server = require "net.server";
80 while select(2, xpcall(server.loop, catch_uncaught_error)) ~= "quitting" do
85 local function cleanup()
86 prosody.log("info", "Shutdown status: Cleaning up");
87 prosody.events.fire_event("server-cleanup");
92 prosody.log("info", "Shutting down...");
94 prosody.events.fire_event("server-stopped");
95 prosody.log("info", "Shutdown complete");
97 prosody.log("debug", "Shutdown reason was: %s", prosody.shutdown_reason or "not specified");
98 prosody.log("debug", "Exiting with status code: %d", prosody.shutdown_code or 0);
99 os.exit(prosody.shutdown_code);