prosody
author Kim Alvefur <zash@zash.se>
Sun, 24 Mar 2024 21:32:00 +0100
changeset 13468 2dbc169aae6a
parent 12975 7214baed9e9d
permissions -rwxr-xr-x
util.startup: Abort before initialization of logging when started as root Prevents creation of log files owned by the root user which could be inaccessible once started correctly.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
455
2eeae9c314b0 main.lua -> prosody
Matthew Wild <mwild1@gmail.com>
parents: 452
diff changeset
     1
#!/usr/bin/env lua
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 1493
diff changeset
     2
-- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2877
diff changeset
     3
-- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2877
diff changeset
     4
-- Copyright (C) 2008-2010 Waqas Hussain
7879
c028555866b3 prosody: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7804
diff changeset
     5
--
761
67ec69001fd7 Update main prosody file, since it doesn't match *.lua pattern, and sed -i treats symlinks badly
Matthew Wild <mwild1@gmail.com>
parents: 755
diff changeset
     6
-- This project is MIT/X11 licensed. Please see the
67ec69001fd7 Update main prosody file, since it doesn't match *.lua pattern, and sed -i treats symlinks badly
Matthew Wild <mwild1@gmail.com>
parents: 755
diff changeset
     7
-- COPYING file in the source package for more information.
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 502
diff changeset
     8
--
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 502
diff changeset
     9
3740
69f95537e9e4 prosody: Added a comment, to match prosodyctl.
Waqas Hussain <waqas20@gmail.com>
parents: 3713
diff changeset
    10
-- prosody - main executable for Prosody XMPP server
69f95537e9e4 prosody: Added a comment, to match prosodyctl.
Waqas Hussain <waqas20@gmail.com>
parents: 3713
diff changeset
    11
843
1d2dab41b0db prosody: Protect main loop. Dare I say crashing finally becomes impossible.
Matthew Wild <mwild1@gmail.com>
parents: 793
diff changeset
    12
-- Will be modified by configure script if run --
455
2eeae9c314b0 main.lua -> prosody
Matthew Wild <mwild1@gmail.com>
parents: 452
diff changeset
    13
7300
b34a42a10c9f prosody, prosodyctl: Allow setting CFG_* variables via Lua interpreter before loading Prosody. Fixes #308.
Matthew Wild <mwild1@gmail.com>
parents: 7084
diff changeset
    14
CFG_SOURCEDIR=CFG_SOURCEDIR or os.getenv("PROSODY_SRCDIR");
b34a42a10c9f prosody, prosodyctl: Allow setting CFG_* variables via Lua interpreter before loading Prosody. Fixes #308.
Matthew Wild <mwild1@gmail.com>
parents: 7084
diff changeset
    15
CFG_CONFIGDIR=CFG_CONFIGDIR or os.getenv("PROSODY_CFGDIR");
b34a42a10c9f prosody, prosodyctl: Allow setting CFG_* variables via Lua interpreter before loading Prosody. Fixes #308.
Matthew Wild <mwild1@gmail.com>
parents: 7084
diff changeset
    16
CFG_PLUGINDIR=CFG_PLUGINDIR or os.getenv("PROSODY_PLUGINDIR");
b34a42a10c9f prosody, prosodyctl: Allow setting CFG_* variables via Lua interpreter before loading Prosody. Fixes #308.
Matthew Wild <mwild1@gmail.com>
parents: 7084
diff changeset
    17
CFG_DATADIR=CFG_DATADIR or os.getenv("PROSODY_DATADIR");
455
2eeae9c314b0 main.lua -> prosody
Matthew Wild <mwild1@gmail.com>
parents: 452
diff changeset
    18
2147
119323e35c32 Mainfile: Fixed some comments.
Waqas Hussain <waqas20@gmail.com>
parents: 2087
diff changeset
    19
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
455
2eeae9c314b0 main.lua -> prosody
Matthew Wild <mwild1@gmail.com>
parents: 452
diff changeset
    20
3999
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
    21
local function is_relative(path)
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
    22
	local path_sep = package.config:sub(1,1);
8267
23aee8ccfe9b prosody: Tiny whitespace fix
Kim Alvefur <zash@zash.se>
parents: 8205
diff changeset
    23
	return ((path_sep == "/" and path:sub(1,1) ~= "/")
23aee8ccfe9b prosody: Tiny whitespace fix
Kim Alvefur <zash@zash.se>
parents: 8205
diff changeset
    24
		or (path_sep == "\\" and (path:sub(1,1) ~= "/" and path:sub(2,3) ~= ":\\")))
3999
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
    25
end
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
    26
2337
9eb20b3f3bbb prosody: Clarify and add some comments to describe what we're doing when and why
Matthew Wild <mwild1@gmail.com>
parents: 2330
diff changeset
    27
-- Tell Lua where to find our libraries
455
2eeae9c314b0 main.lua -> prosody
Matthew Wild <mwild1@gmail.com>
parents: 452
diff changeset
    28
if CFG_SOURCEDIR then
3999
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
    29
	local function filter_relative_paths(path)
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
    30
		if is_relative(path) then return ""; end
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
    31
	end
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
    32
	local function sanitise_paths(paths)
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
    33
		return (paths:gsub("[^;]+;?", filter_relative_paths):gsub(";;+", ";"));
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
    34
	end
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
    35
	package.path = sanitise_paths(CFG_SOURCEDIR.."/?.lua;"..package.path);
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
    36
	package.cpath = sanitise_paths(CFG_SOURCEDIR.."/?.so;"..package.cpath);
455
2eeae9c314b0 main.lua -> prosody
Matthew Wild <mwild1@gmail.com>
parents: 452
diff changeset
    37
end
2eeae9c314b0 main.lua -> prosody
Matthew Wild <mwild1@gmail.com>
parents: 452
diff changeset
    38
2337
9eb20b3f3bbb prosody: Clarify and add some comments to describe what we're doing when and why
Matthew Wild <mwild1@gmail.com>
parents: 2330
diff changeset
    39
-- Substitute ~ with path to home directory in data path
502
21dc299387a6 Installation improvements (auto-creation of data directories)
Matthew Wild <mwild1@gmail.com>
parents: 500
diff changeset
    40
if CFG_DATADIR then
467
66f145f5c932 Update Makefile to now pass config paths to prosody. Update prosody, modulemanager and connectionlisteners to obey these paths.
Matthew Wild <mwild1@gmail.com>
parents: 455
diff changeset
    41
	if os.getenv("HOME") then
502
21dc299387a6 Installation improvements (auto-creation of data directories)
Matthew Wild <mwild1@gmail.com>
parents: 500
diff changeset
    42
		CFG_DATADIR = CFG_DATADIR:gsub("^~", os.getenv("HOME"));
467
66f145f5c932 Update Makefile to now pass config paths to prosody. Update prosody, modulemanager and connectionlisteners to obey these paths.
Matthew Wild <mwild1@gmail.com>
parents: 455
diff changeset
    43
	end
502
21dc299387a6 Installation improvements (auto-creation of data directories)
Matthew Wild <mwild1@gmail.com>
parents: 500
diff changeset
    44
end
467
66f145f5c932 Update Makefile to now pass config paths to prosody. Update prosody, modulemanager and connectionlisteners to obey these paths.
Matthew Wild <mwild1@gmail.com>
parents: 455
diff changeset
    45
10601
25a3c8134b0a prosody/util.startup: Switch to parse_args() for --root and --config
Matthew Wild <mwild1@gmail.com>
parents: 8729
diff changeset
    46
12582
10bb58ad5583 executables: Reject Lua 5.1 early
Kim Alvefur <zash@zash.se>
parents: 12557
diff changeset
    47
-- Check before first require, to preempt the probable failure
10bb58ad5583 executables: Reject Lua 5.1 early
Kim Alvefur <zash@zash.se>
parents: 12557
diff changeset
    48
if _VERSION < "Lua 5.2" then
10bb58ad5583 executables: Reject Lua 5.1 early
Kim Alvefur <zash@zash.se>
parents: 12557
diff changeset
    49
	io.stderr:write("Prosody is no longer compatible with Lua 5.1\n")
10bb58ad5583 executables: Reject Lua 5.1 early
Kim Alvefur <zash@zash.se>
parents: 12557
diff changeset
    50
	io.stderr:write("See https://prosody.im/doc/depends#lua for more information\n")
10bb58ad5583 executables: Reject Lua 5.1 early
Kim Alvefur <zash@zash.se>
parents: 12557
diff changeset
    51
	return os.exit(1);
10bb58ad5583 executables: Reject Lua 5.1 early
Kim Alvefur <zash@zash.se>
parents: 12557
diff changeset
    52
end
6988
a2e1f5ebdb53 prosody: Don't silently ignore unknown command-line options
Matthew Wild <mwild1@gmail.com>
parents: 6924
diff changeset
    53
12958
4f2accd99373 executables: Invoke loader to allow mixing of old and new import style
Kim Alvefur <zash@zash.se>
parents: 12582
diff changeset
    54
if not pcall(require, "prosody.loader") then
4f2accd99373 executables: Invoke loader to allow mixing of old and new import style
Kim Alvefur <zash@zash.se>
parents: 12582
diff changeset
    55
	pcall(require, "loader");
4f2accd99373 executables: Invoke loader to allow mixing of old and new import style
Kim Alvefur <zash@zash.se>
parents: 12582
diff changeset
    56
end
4f2accd99373 executables: Invoke loader to allow mixing of old and new import style
Kim Alvefur <zash@zash.se>
parents: 12582
diff changeset
    57
12975
7214baed9e9d executables: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12958
diff changeset
    58
local startup = require "prosody.util.startup";
7214baed9e9d executables: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12958
diff changeset
    59
local async = require "prosody.util.async";
2587
c37f971f0fe6 prosody, prosodyctl: Re-jiggle load order again, fixes logging config not being obeyed (thanks darkrain)
Matthew Wild <mwild1@gmail.com>
parents: 2567
diff changeset
    60
8685
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
    61
-- Note: it's important that this thread is not GC'd, as some C libraries
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
    62
-- that are initialized here store a pointer to it ( :/ ).
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
    63
local thread = async.runner();
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
    64
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
    65
thread:run(startup.prosody);
1530
0494f5e3be23 prosody: Call initialisation functions at once
Matthew Wild <mwild1@gmail.com>
parents: 1529
diff changeset
    66
12300
49ebac8a5260 prosody: Expose main thread on the 'prosody' global
Kim Alvefur <zash@zash.se>
parents: 12299
diff changeset
    67
prosody.main_thread = thread;
49ebac8a5260 prosody: Expose main thread on the 'prosody' global
Kim Alvefur <zash@zash.se>
parents: 12299
diff changeset
    68
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents: 8288
diff changeset
    69
local function loop()
1529
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
    70
	-- Error handler for errors that make it this far
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
    71
	local function catch_uncaught_error(err)
2769
826f6fb7036d prosody: Less strict matching for the magic 'interrupted' error
Matthew Wild <mwild1@gmail.com>
parents: 2321
diff changeset
    72
		if type(err) == "string" and err:match("interrupted!$") then
1529
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
    73
			return "quitting";
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
    74
		end
7879
c028555866b3 prosody: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7804
diff changeset
    75
8717
e1c4bdb2cd25 prosody: Use prosody.log instead of _G.log because it looks nicer
Kim Alvefur <zash@zash.se>
parents: 8685
diff changeset
    76
		prosody.log("error", "Top-level error, please report:\n%s", tostring(err));
1529
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
    77
		local traceback = debug.traceback("", 2);
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
    78
		if traceback then
8717
e1c4bdb2cd25 prosody: Use prosody.log instead of _G.log because it looks nicer
Kim Alvefur <zash@zash.se>
parents: 8685
diff changeset
    79
			prosody.log("error", "%s", traceback);
1529
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
    80
		end
7879
c028555866b3 prosody: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7804
diff changeset
    81
1529
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
    82
		prosody.events.fire_event("very-bad-error", {error = err, traceback = traceback});
992
3153eff6dae2 prosody: Add prosody_shutdown() function to initiate a server shutdown, add code to gracefully close connections before stopping
Matthew Wild <mwild1@gmail.com>
parents: 978
diff changeset
    83
	end
7879
c028555866b3 prosody: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7804
diff changeset
    84
6924
45fa2e554c79 prosody: Fix sleep call that relied on the no longer existing socket global
Kim Alvefur <zash@zash.se>
parents: 6811
diff changeset
    85
	local sleep = require"socket".sleep;
12975
7214baed9e9d executables: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12958
diff changeset
    86
	local server = require "prosody.net.server";
6924
45fa2e554c79 prosody: Fix sleep call that relied on the no longer existing socket global
Kim Alvefur <zash@zash.se>
parents: 6811
diff changeset
    87
1529
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
    88
	while select(2, xpcall(server.loop, catch_uncaught_error)) ~= "quitting" do
6924
45fa2e554c79 prosody: Fix sleep call that relied on the no longer existing socket global
Kim Alvefur <zash@zash.se>
parents: 6811
diff changeset
    89
		sleep(0.2);
1529
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
    90
	end
992
3153eff6dae2 prosody: Add prosody_shutdown() function to initiate a server shutdown, add code to gracefully close connections before stopping
Matthew Wild <mwild1@gmail.com>
parents: 978
diff changeset
    91
end
1026
e640df2e4e9b prosody: Fire events during server shutdown process
Matthew Wild <mwild1@gmail.com>
parents: 1017
diff changeset
    92
1530
0494f5e3be23 prosody: Call initialisation functions at once
Matthew Wild <mwild1@gmail.com>
parents: 1529
diff changeset
    93
loop();
853
c0a40522041e prosody: Log top-level errors
Matthew Wild <mwild1@gmail.com>
parents: 843
diff changeset
    94
12557
cc0ec0277813 util.startup: Fix async waiting for last shutdown steps
Kim Alvefur <zash@zash.se>
parents: 12300
diff changeset
    95
startup.exit();