util/startup.lua
author Matthew Wild <mwild1@gmail.com>
Mon, 29 Nov 2021 14:16:20 +0000
changeset 11967 f5c6be4a3ecc
parent 11952 dca75cc6fc5a
child 12164 ac654fb19203
permissions -rw-r--r--
util.startup: Initialize util.async at startup
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8641
f8f45bbbd8ba util.startup: Ignore various globals being read and written as part of startup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8640
diff changeset
     1
-- Ignore the CFG_* variables
f8f45bbbd8ba util.startup: Ignore various globals being read and written as part of startup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8640
diff changeset
     2
-- luacheck: ignore 113/CFG_CONFIGDIR 113/CFG_SOURCEDIR 113/CFG_DATADIR 113/CFG_PLUGINDIR
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     3
local startup = {};
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     4
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     5
local prosody = { events = require "util.events".new() };
8723
dba17a70fd22 util.startup: Initialize prosody.log / _G.log here instead of in loggingmanager to reduce dependencies
Kim Alvefur <zash@zash.se>
parents: 8722
diff changeset
     6
local logger = require "util.logger";
dba17a70fd22 util.startup: Initialize prosody.log / _G.log here instead of in loggingmanager to reduce dependencies
Kim Alvefur <zash@zash.se>
parents: 8722
diff changeset
     7
local log = logger.init("startup");
10655
1196f1e8d178 util.startup: Break out command line argument parsing into util.argparse
Kim Alvefur <zash@zash.se>
parents: 10640
diff changeset
     8
local parse_args = require "util.argparse".parse;
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
local config = require "core.configmanager";
9881
ded5303e1fde util.startup: Log configuration warnings at startup
Matthew Wild <mwild1@gmail.com>
parents: 9877
diff changeset
    11
local config_warnings;
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
local dependencies = require "util.dependencies";
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
8669
57780ba1938f util.startup: Move original_logging_config to a local variable
Matthew Wild <mwild1@gmail.com>
parents: 8668
diff changeset
    15
local original_logging_config;
57780ba1938f util.startup: Move original_logging_config to a local variable
Matthew Wild <mwild1@gmail.com>
parents: 8668
diff changeset
    16
11543
3413fea9e6db util.startup: Set more aggressive defaults for GC
Matthew Wild <mwild1@gmail.com>
parents: 11077
diff changeset
    17
local default_gc_params = {
3413fea9e6db util.startup: Set more aggressive defaults for GC
Matthew Wild <mwild1@gmail.com>
parents: 11077
diff changeset
    18
	mode = "incremental";
3413fea9e6db util.startup: Set more aggressive defaults for GC
Matthew Wild <mwild1@gmail.com>
parents: 11077
diff changeset
    19
	-- Incremental mode defaults
3413fea9e6db util.startup: Set more aggressive defaults for GC
Matthew Wild <mwild1@gmail.com>
parents: 11077
diff changeset
    20
	threshold = 105, speed = 500;
3413fea9e6db util.startup: Set more aggressive defaults for GC
Matthew Wild <mwild1@gmail.com>
parents: 11077
diff changeset
    21
	-- Generational mode defaults
3413fea9e6db util.startup: Set more aggressive defaults for GC
Matthew Wild <mwild1@gmail.com>
parents: 11077
diff changeset
    22
	minor_threshold = 20, major_threshold = 50;
3413fea9e6db util.startup: Set more aggressive defaults for GC
Matthew Wild <mwild1@gmail.com>
parents: 11077
diff changeset
    23
};
11077
5691b9773c5b util.startup: Configure the GC on startup, using the config or built-in defaults
Matthew Wild <mwild1@gmail.com>
parents: 10951
diff changeset
    24
11833
4fad0ca42f66 util.startup: Allow separate command line argument settings for prosody and prosodyctl
Kim Alvefur <zash@zash.se>
parents: 11832
diff changeset
    25
local arg_settigs = {
4fad0ca42f66 util.startup: Allow separate command line argument settings for prosody and prosodyctl
Kim Alvefur <zash@zash.se>
parents: 11832
diff changeset
    26
	prosody = {
11873
d52a73425eba util.startup: Show brief usage on `prosody -h|-?|--help`
Kim Alvefur <zash@zash.se>
parents: 11870
diff changeset
    27
		short_params = { D = "daemonize"; F = "no-daemonize", h = "help", ["?"] = "help" };
11833
4fad0ca42f66 util.startup: Allow separate command line argument settings for prosody and prosodyctl
Kim Alvefur <zash@zash.se>
parents: 11832
diff changeset
    28
		value_params = { config = true };
4fad0ca42f66 util.startup: Allow separate command line argument settings for prosody and prosodyctl
Kim Alvefur <zash@zash.se>
parents: 11832
diff changeset
    29
	};
4fad0ca42f66 util.startup: Allow separate command line argument settings for prosody and prosodyctl
Kim Alvefur <zash@zash.se>
parents: 11832
diff changeset
    30
	prosodyctl = {
11874
1d1ed2be3491 util.startup: Understand -h, -? as --help in prosodyctl but ignore
Kim Alvefur <zash@zash.se>
parents: 11873
diff changeset
    31
		short_params = { v = "verbose", h = "help", ["?"] = "help" };
11833
4fad0ca42f66 util.startup: Allow separate command line argument settings for prosody and prosodyctl
Kim Alvefur <zash@zash.se>
parents: 11832
diff changeset
    32
		value_params = { config = true };
4fad0ca42f66 util.startup: Allow separate command line argument settings for prosody and prosodyctl
Kim Alvefur <zash@zash.se>
parents: 11832
diff changeset
    33
	};
4fad0ca42f66 util.startup: Allow separate command line argument settings for prosody and prosodyctl
Kim Alvefur <zash@zash.se>
parents: 11832
diff changeset
    34
}
10600
cb107ea49b35 util.startup: Add startup step for parsing command-line options
Matthew Wild <mwild1@gmail.com>
parents: 10394
diff changeset
    35
cb107ea49b35 util.startup: Add startup step for parsing command-line options
Matthew Wild <mwild1@gmail.com>
parents: 10394
diff changeset
    36
function startup.parse_args()
11833
4fad0ca42f66 util.startup: Allow separate command line argument settings for prosody and prosodyctl
Kim Alvefur <zash@zash.se>
parents: 11832
diff changeset
    37
	local opts, err, where = parse_args(arg, arg_settigs[prosody.process_type]);
10940
d770435f0f84 util.argparse: Move exiting and error to util.startup
Kim Alvefur <zash@zash.se>
parents: 10938
diff changeset
    38
	if not opts then
d770435f0f84 util.argparse: Move exiting and error to util.startup
Kim Alvefur <zash@zash.se>
parents: 10938
diff changeset
    39
		if err == "param-not-found" then
d770435f0f84 util.argparse: Move exiting and error to util.startup
Kim Alvefur <zash@zash.se>
parents: 10938
diff changeset
    40
			print("Unknown command-line option: "..tostring(where));
11851
2b3ce80ffece util.startup: Only ask if 'prosodyctl' was meant instead of 'prosody' (fix #1692)
Kim Alvefur <zash@zash.se>
parents: 11850
diff changeset
    41
			if prosody.process_type == "prosody" then
2b3ce80ffece util.startup: Only ask if 'prosodyctl' was meant instead of 'prosody' (fix #1692)
Kim Alvefur <zash@zash.se>
parents: 11850
diff changeset
    42
				print("Perhaps you meant to use prosodyctl instead?");
2b3ce80ffece util.startup: Only ask if 'prosodyctl' was meant instead of 'prosody' (fix #1692)
Kim Alvefur <zash@zash.se>
parents: 11850
diff changeset
    43
			end
10940
d770435f0f84 util.argparse: Move exiting and error to util.startup
Kim Alvefur <zash@zash.se>
parents: 10938
diff changeset
    44
		elseif err == "missing-value" then
d770435f0f84 util.argparse: Move exiting and error to util.startup
Kim Alvefur <zash@zash.se>
parents: 10938
diff changeset
    45
			print("Expected a value to follow command-line option: "..where);
10604
08f2fe5ac30f util.startup: Fix logic to make --config work again
Matthew Wild <mwild1@gmail.com>
parents: 10601
diff changeset
    46
		end
10940
d770435f0f84 util.argparse: Move exiting and error to util.startup
Kim Alvefur <zash@zash.se>
parents: 10938
diff changeset
    47
		os.exit(1);
10600
cb107ea49b35 util.startup: Add startup step for parsing command-line options
Matthew Wild <mwild1@gmail.com>
parents: 10394
diff changeset
    48
	end
11873
d52a73425eba util.startup: Show brief usage on `prosody -h|-?|--help`
Kim Alvefur <zash@zash.se>
parents: 11870
diff changeset
    49
	if opts.help and prosody.process_type == "prosody" then
d52a73425eba util.startup: Show brief usage on `prosody -h|-?|--help`
Kim Alvefur <zash@zash.se>
parents: 11870
diff changeset
    50
		print("prosody [ -D | -F ] [ --config /path/to/prosody.cfg.lua ]");
d52a73425eba util.startup: Show brief usage on `prosody -h|-?|--help`
Kim Alvefur <zash@zash.se>
parents: 11870
diff changeset
    51
		print("  -D, --daemonize       Run in the background")
d52a73425eba util.startup: Show brief usage on `prosody -h|-?|--help`
Kim Alvefur <zash@zash.se>
parents: 11870
diff changeset
    52
		print("  -F, --no-daemonize    Run in the foreground")
d52a73425eba util.startup: Show brief usage on `prosody -h|-?|--help`
Kim Alvefur <zash@zash.se>
parents: 11870
diff changeset
    53
		print("  --config FILE         Specify config file")
d52a73425eba util.startup: Show brief usage on `prosody -h|-?|--help`
Kim Alvefur <zash@zash.se>
parents: 11870
diff changeset
    54
		os.exit(0);
d52a73425eba util.startup: Show brief usage on `prosody -h|-?|--help`
Kim Alvefur <zash@zash.se>
parents: 11870
diff changeset
    55
	end
10940
d770435f0f84 util.argparse: Move exiting and error to util.startup
Kim Alvefur <zash@zash.se>
parents: 10938
diff changeset
    56
	prosody.opts = opts;
10600
cb107ea49b35 util.startup: Add startup step for parsing command-line options
Matthew Wild <mwild1@gmail.com>
parents: 10394
diff changeset
    57
end
cb107ea49b35 util.startup: Add startup step for parsing command-line options
Matthew Wild <mwild1@gmail.com>
parents: 10394
diff changeset
    58
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    59
function startup.read_config()
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    60
	local filenames = {};
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    61
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    62
	local filename;
10601
25a3c8134b0a prosody/util.startup: Switch to parse_args() for --root and --config
Matthew Wild <mwild1@gmail.com>
parents: 10600
diff changeset
    63
	if prosody.opts.config then
25a3c8134b0a prosody/util.startup: Switch to parse_args() for --root and --config
Matthew Wild <mwild1@gmail.com>
parents: 10600
diff changeset
    64
		table.insert(filenames, prosody.opts.config);
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    65
		if CFG_CONFIGDIR then
10601
25a3c8134b0a prosody/util.startup: Switch to parse_args() for --root and --config
Matthew Wild <mwild1@gmail.com>
parents: 10600
diff changeset
    66
			table.insert(filenames, CFG_CONFIGDIR.."/"..prosody.opts.config);
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    67
		end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    68
	elseif os.getenv("PROSODY_CONFIG") then -- Passed by prosodyctl
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    69
			table.insert(filenames, os.getenv("PROSODY_CONFIG"));
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    70
	else
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    71
		table.insert(filenames, (CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    72
	end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    73
	for _,_filename in ipairs(filenames) do
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    74
		filename = _filename;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    75
		local file = io.open(filename);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    76
		if file then
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    77
			file:close();
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    78
			prosody.config_file = filename;
10393
dbb8dae58265 util.startup: Update config path (fixes #1430)
Kim Alvefur <zash@zash.se>
parents: 9766
diff changeset
    79
			prosody.paths.config = filename:match("^(.*)[\\/][^\\/]*$");
dbb8dae58265 util.startup: Update config path (fixes #1430)
Kim Alvefur <zash@zash.se>
parents: 9766
diff changeset
    80
			CFG_CONFIGDIR = prosody.paths.config; -- luacheck: ignore 111
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    81
			break;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    82
		end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    83
	end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    84
	prosody.config_file = filename
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    85
	local ok, level, err = config.load(filename);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    86
	if not ok then
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    87
		print("\n");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    88
		print("**************************");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    89
		if level == "parser" then
8731
41c959c5c84b Fix spelling throughout the codebase [codespell]
Kim Alvefur <zash@zash.se>
parents: 8724
diff changeset
    90
			print("A problem occurred while reading the config file "..filename);
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    91
			print("");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    92
			local err_line, err_message = tostring(err):match("%[string .-%]:(%d*): (.*)");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    93
			if err:match("chunk has too many syntax levels$") then
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    94
				print("An Include statement in a config file is including an already-included");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    95
				print("file and causing an infinite loop. An Include statement in a config file is...");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    96
			else
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    97
				print("Error"..(err_line and (" on line "..err_line) or "")..": "..(err_message or tostring(err)));
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    98
			end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    99
			print("");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   100
		elseif level == "file" then
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   101
			print("Prosody was unable to find the configuration file.");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   102
			print("We looked for: "..filename);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   103
			print("A sample config file is included in the Prosody download called prosody.cfg.lua.dist");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   104
			print("Copy or rename it to prosody.cfg.lua and edit as necessary.");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   105
		end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   106
		print("More help on configuring Prosody can be found at https://prosody.im/doc/configure");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   107
		print("Good luck!");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   108
		print("**************************");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   109
		print("");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   110
		os.exit(1);
9881
ded5303e1fde util.startup: Log configuration warnings at startup
Matthew Wild <mwild1@gmail.com>
parents: 9877
diff changeset
   111
	elseif err and #err > 0 then
ded5303e1fde util.startup: Log configuration warnings at startup
Matthew Wild <mwild1@gmail.com>
parents: 9877
diff changeset
   112
		config_warnings = err;
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   113
	end
9217
8b2b8f1a911f util.startup: Set flag when config fully loaded
Matthew Wild <mwild1@gmail.com>
parents: 8961
diff changeset
   114
	prosody.config_loaded = true;
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   115
end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   116
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   117
function startup.check_dependencies()
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   118
	if not dependencies.check_dependencies() then
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   119
		os.exit(1);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   120
	end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   121
end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   122
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   123
-- luacheck: globals socket server
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   124
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   125
function startup.load_libraries()
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   126
	-- Load socket framework
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   127
	-- luacheck: ignore 111/server 111/socket
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   128
	socket = require "socket";
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   129
	server = require "net.server"
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   130
end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   131
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   132
function startup.init_logging()
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   133
	-- Initialize logging
8724
b773b15fee71 util.startup: Set up event hooks for reloading logging here instead of in loggingmanager to simplify startup dependencies
Kim Alvefur <zash@zash.se>
parents: 8723
diff changeset
   134
	local loggingmanager = require "core.loggingmanager"
b773b15fee71 util.startup: Set up event hooks for reloading logging here instead of in loggingmanager to simplify startup dependencies
Kim Alvefur <zash@zash.se>
parents: 8723
diff changeset
   135
	loggingmanager.reload_logging();
9766
34988a408b74 util.startup: Always reload logging after config (fixes #1284)
Kim Alvefur <zash@zash.se>
parents: 9217
diff changeset
   136
	prosody.events.add_handler("config-reloaded", function ()
34988a408b74 util.startup: Always reload logging after config (fixes #1284)
Kim Alvefur <zash@zash.se>
parents: 9217
diff changeset
   137
		prosody.events.fire_event("reopen-log-files");
34988a408b74 util.startup: Always reload logging after config (fixes #1284)
Kim Alvefur <zash@zash.se>
parents: 9217
diff changeset
   138
	end);
8724
b773b15fee71 util.startup: Set up event hooks for reloading logging here instead of in loggingmanager to simplify startup dependencies
Kim Alvefur <zash@zash.se>
parents: 8723
diff changeset
   139
	prosody.events.add_handler("reopen-log-files", function ()
b773b15fee71 util.startup: Set up event hooks for reloading logging here instead of in loggingmanager to simplify startup dependencies
Kim Alvefur <zash@zash.se>
parents: 8723
diff changeset
   140
		loggingmanager.reload_logging();
b773b15fee71 util.startup: Set up event hooks for reloading logging here instead of in loggingmanager to simplify startup dependencies
Kim Alvefur <zash@zash.se>
parents: 8723
diff changeset
   141
		prosody.events.fire_event("logging-reloaded");
b773b15fee71 util.startup: Set up event hooks for reloading logging here instead of in loggingmanager to simplify startup dependencies
Kim Alvefur <zash@zash.se>
parents: 8723
diff changeset
   142
	end);
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   143
end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   144
9877
dfaeea570f7e util.startup: Give function a more generic name so it can apply to all warnings
Matthew Wild <mwild1@gmail.com>
parents: 9766
diff changeset
   145
function startup.log_startup_warnings()
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   146
	dependencies.log_warnings();
9882
dd61201fc5af util.startup: Don't die if there are no config warnings to log (thanks buildbot)
Matthew Wild <mwild1@gmail.com>
parents: 9881
diff changeset
   147
	if config_warnings then
dd61201fc5af util.startup: Don't die if there are no config warnings to log (thanks buildbot)
Matthew Wild <mwild1@gmail.com>
parents: 9881
diff changeset
   148
		for _, warning in ipairs(config_warnings) do
dd61201fc5af util.startup: Don't die if there are no config warnings to log (thanks buildbot)
Matthew Wild <mwild1@gmail.com>
parents: 9881
diff changeset
   149
			log("warn", "Configuration warning: %s", warning);
dd61201fc5af util.startup: Don't die if there are no config warnings to log (thanks buildbot)
Matthew Wild <mwild1@gmail.com>
parents: 9881
diff changeset
   150
		end
9881
ded5303e1fde util.startup: Log configuration warnings at startup
Matthew Wild <mwild1@gmail.com>
parents: 9877
diff changeset
   151
	end
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   152
end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   153
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   154
function startup.sanity_check()
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   155
	for host, host_config in pairs(config.getconfig()) do
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   156
		if host ~= "*"
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   157
		and host_config.enabled ~= false
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   158
		and not host_config.component_module then
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   159
			return;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   160
		end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   161
	end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   162
	log("error", "No enabled VirtualHost entries found in the config file.");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   163
	log("error", "At least one active host is required for Prosody to function. Exiting...");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   164
	os.exit(1);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   165
end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   166
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   167
function startup.sandbox_require()
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   168
	-- Replace require() with one that doesn't pollute _G, required
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   169
	-- for neat sandboxing of modules
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   170
	-- luacheck: ignore 113/getfenv 111/require
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   171
	local _realG = _G;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   172
	local _real_require = require;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   173
	local getfenv = getfenv or function (f)
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   174
		-- FIXME: This is a hack to replace getfenv() in Lua 5.2
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   175
		local name, env = debug.getupvalue(debug.getinfo(f or 1).func, 1);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   176
		if name == "_ENV" then
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   177
			return env;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   178
		end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   179
	end
8641
f8f45bbbd8ba util.startup: Ignore various globals being read and written as part of startup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8640
diff changeset
   180
	function require(...) -- luacheck: ignore 121
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   181
		local curr_env = getfenv(2);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   182
		local curr_env_mt = getmetatable(curr_env);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   183
		local _realG_mt = getmetatable(_realG);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   184
		if curr_env_mt and curr_env_mt.__index and not curr_env_mt.__newindex and _realG_mt then
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   185
			local old_newindex, old_index;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   186
			old_newindex, _realG_mt.__newindex = _realG_mt.__newindex, curr_env;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   187
			old_index, _realG_mt.__index = _realG_mt.__index, function (_G, k) -- luacheck: ignore 212/_G
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   188
				return rawget(curr_env, k);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   189
			end;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   190
			local ret = _real_require(...);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   191
			_realG_mt.__newindex = old_newindex;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   192
			_realG_mt.__index = old_index;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   193
			return ret;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   194
		end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   195
		return _real_require(...);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   196
	end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   197
end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   198
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   199
function startup.set_function_metatable()
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   200
	local mt = {};
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   201
	function mt.__index(f, upvalue)
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   202
		local i, name, value = 0;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   203
		repeat
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   204
			i = i + 1;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   205
			name, value = debug.getupvalue(f, i);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   206
		until name == upvalue or name == nil;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   207
		return value;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   208
	end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   209
	function mt.__newindex(f, upvalue, value)
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   210
		local i, name = 0;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   211
		repeat
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   212
			i = i + 1;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   213
			name = debug.getupvalue(f, i);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   214
		until name == upvalue or name == nil;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   215
		if name then
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   216
			debug.setupvalue(f, i, value);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   217
		end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   218
	end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   219
	function mt.__tostring(f)
11154
0cfa36fa707e util.startup: Include arguments in function string representation
Kim Alvefur <zash@zash.se>
parents: 11153
diff changeset
   220
		local info = debug.getinfo(f, "Su");
11156
89162d27e1b1 util.startup: Handle missing nparams field from debug info (not present in 5.1)
Matthew Wild <mwild1@gmail.com>
parents: 11154
diff changeset
   221
		local n_params = info.nparams or 0;
89162d27e1b1 util.startup: Handle missing nparams field from debug info (not present in 5.1)
Matthew Wild <mwild1@gmail.com>
parents: 11154
diff changeset
   222
		for i = 1, n_params do
11154
0cfa36fa707e util.startup: Include arguments in function string representation
Kim Alvefur <zash@zash.se>
parents: 11153
diff changeset
   223
			info[i] = debug.getlocal(f, i);
0cfa36fa707e util.startup: Include arguments in function string representation
Kim Alvefur <zash@zash.se>
parents: 11153
diff changeset
   224
		end
0cfa36fa707e util.startup: Include arguments in function string representation
Kim Alvefur <zash@zash.se>
parents: 11153
diff changeset
   225
		if info.isvararg then
11156
89162d27e1b1 util.startup: Handle missing nparams field from debug info (not present in 5.1)
Matthew Wild <mwild1@gmail.com>
parents: 11154
diff changeset
   226
			info[n_params+1] = "...";
11154
0cfa36fa707e util.startup: Include arguments in function string representation
Kim Alvefur <zash@zash.se>
parents: 11153
diff changeset
   227
		end
0cfa36fa707e util.startup: Include arguments in function string representation
Kim Alvefur <zash@zash.se>
parents: 11153
diff changeset
   228
		return ("function<%s:%d>(%s)"):format(info.short_src:match("[^\\/]*$"), info.linedefined, table.concat(info, ", "));
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   229
	end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   230
	debug.setmetatable(function() end, mt);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   231
end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   232
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   233
function startup.detect_platform()
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   234
	prosody.platform = "unknown";
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   235
	if os.getenv("WINDIR") then
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   236
		prosody.platform = "windows";
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   237
	elseif package.config:sub(1,1) == "/" then
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   238
		prosody.platform = "posix";
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   239
	end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   240
end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   241
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   242
function startup.detect_installed()
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   243
	prosody.installed = nil;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   244
	if CFG_SOURCEDIR and (prosody.platform == "windows" or CFG_SOURCEDIR:match("^/")) then
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   245
		prosody.installed = true;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   246
	end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   247
end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   248
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   249
function startup.init_global_state()
8641
f8f45bbbd8ba util.startup: Ignore various globals being read and written as part of startup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8640
diff changeset
   250
	-- luacheck: ignore 121
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   251
	prosody.bare_sessions = {};
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   252
	prosody.full_sessions = {};
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   253
	prosody.hosts = {};
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   254
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   255
	-- COMPAT: These globals are deprecated
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   256
	-- luacheck: ignore 111/bare_sessions 111/full_sessions 111/hosts
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   257
	bare_sessions = prosody.bare_sessions;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   258
	full_sessions = prosody.full_sessions;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   259
	hosts = prosody.hosts;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   260
8701
0499f3da0ec4 util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents: 8695
diff changeset
   261
	prosody.paths = { source = CFG_SOURCEDIR, config = CFG_CONFIGDIR or ".",
0499f3da0ec4 util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents: 8695
diff changeset
   262
	                  plugins = CFG_PLUGINDIR or "plugins", data = "data" };
0499f3da0ec4 util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents: 8695
diff changeset
   263
0499f3da0ec4 util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents: 8695
diff changeset
   264
	prosody.arg = _G.arg;
0499f3da0ec4 util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents: 8695
diff changeset
   265
8723
dba17a70fd22 util.startup: Initialize prosody.log / _G.log here instead of in loggingmanager to reduce dependencies
Kim Alvefur <zash@zash.se>
parents: 8722
diff changeset
   266
	_G.log = logger.init("general");
dba17a70fd22 util.startup: Initialize prosody.log / _G.log here instead of in loggingmanager to reduce dependencies
Kim Alvefur <zash@zash.se>
parents: 8722
diff changeset
   267
	prosody.log = logger.init("general");
dba17a70fd22 util.startup: Initialize prosody.log / _G.log here instead of in loggingmanager to reduce dependencies
Kim Alvefur <zash@zash.se>
parents: 8722
diff changeset
   268
8701
0499f3da0ec4 util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents: 8695
diff changeset
   269
	startup.detect_platform();
0499f3da0ec4 util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents: 8695
diff changeset
   270
	startup.detect_installed();
0499f3da0ec4 util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents: 8695
diff changeset
   271
	_G.prosody = prosody;
0499f3da0ec4 util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents: 8695
diff changeset
   272
end
0499f3da0ec4 util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents: 8695
diff changeset
   273
0499f3da0ec4 util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents: 8695
diff changeset
   274
function startup.setup_datadir()
0499f3da0ec4 util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents: 8695
diff changeset
   275
	prosody.paths.data = config.get("*", "data_path") or CFG_DATADIR or "data";
0499f3da0ec4 util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents: 8695
diff changeset
   276
end
0499f3da0ec4 util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents: 8695
diff changeset
   277
0499f3da0ec4 util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents: 8695
diff changeset
   278
function startup.setup_plugindir()
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   279
	local custom_plugin_paths = config.get("*", "plugin_paths");
10177
0513dd2830b7 util.startup: The .setup_plugindir function now correctly sets a default/specified path for custom plugins
João Duarte <jvsDuarte08@gmail.com>
parents: 10175
diff changeset
   280
	local path_sep = package.config:sub(3,3);
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   281
	if custom_plugin_paths then
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   282
		-- path1;path2;path3;defaultpath...
8641
f8f45bbbd8ba util.startup: Ignore various globals being read and written as part of startup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8640
diff changeset
   283
		-- luacheck: ignore 111
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   284
		CFG_PLUGINDIR = table.concat(custom_plugin_paths, path_sep)..path_sep..(CFG_PLUGINDIR or "plugins");
8736
6a234e77c99f util.startup: Fix traceback due to both plugin path becoming nil if plugin_paths is unset
Kim Alvefur <zash@zash.se>
parents: 8731
diff changeset
   285
		prosody.paths.plugins = CFG_PLUGINDIR;
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   286
	end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   287
end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   288
10408
29c10930a7b2 util.startup: Split plugin installer path setup into a separate function
Kim Alvefur <zash@zash.se>
parents: 10395
diff changeset
   289
function startup.setup_plugin_install_path()
11309
cd8516a77255 util.startup: Make installer_plugin_path relative to data directory
Kim Alvefur <zash@zash.se>
parents: 11308
diff changeset
   290
	local installer_plugin_path = config.get("*", "installer_plugin_path") or "custom_plugins";
10408
29c10930a7b2 util.startup: Split plugin installer path setup into a separate function
Kim Alvefur <zash@zash.se>
parents: 10395
diff changeset
   291
	local path_sep = package.config:sub(3,3);
11309
cd8516a77255 util.startup: Make installer_plugin_path relative to data directory
Kim Alvefur <zash@zash.se>
parents: 11308
diff changeset
   292
	installer_plugin_path = config.resolve_relative_path(CFG_DATADIR or "data", installer_plugin_path);
10408
29c10930a7b2 util.startup: Split plugin installer path setup into a separate function
Kim Alvefur <zash@zash.se>
parents: 10395
diff changeset
   293
	require"util.paths".complement_lua_path(installer_plugin_path);
29c10930a7b2 util.startup: Split plugin installer path setup into a separate function
Kim Alvefur <zash@zash.se>
parents: 10395
diff changeset
   294
	-- luacheck: ignore 111
10177
0513dd2830b7 util.startup: The .setup_plugindir function now correctly sets a default/specified path for custom plugins
João Duarte <jvsDuarte08@gmail.com>
parents: 10175
diff changeset
   295
	CFG_PLUGINDIR = installer_plugin_path..path_sep..(CFG_PLUGINDIR or "plugins");
11148
2b9f7c537acb util.startup: Save the path used by the installer to prosody.paths
Kim Alvefur <zash@zash.se>
parents: 11143
diff changeset
   296
	prosody.paths.installer = installer_plugin_path;
10175
628e238feb04 util.startup: Removed unnecessary if clause at startup.set_plugindir
João Duarte <jvsDuarte08@gmail.com>
parents: 10167
diff changeset
   297
	prosody.paths.plugins = CFG_PLUGINDIR;
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   298
end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   299
8667
d49acc9a8da2 util.startup: Fix chdir() to use correct path variable
Matthew Wild <mwild1@gmail.com>
parents: 8656
diff changeset
   300
function startup.chdir()
d49acc9a8da2 util.startup: Fix chdir() to use correct path variable
Matthew Wild <mwild1@gmail.com>
parents: 8656
diff changeset
   301
	if prosody.installed then
10394
82705ec87253 util.startup: Ensure prosody.paths are absolute (see #1430)
Kim Alvefur <zash@zash.se>
parents: 10393
diff changeset
   302
		local lfs = require "lfs";
82705ec87253 util.startup: Ensure prosody.paths are absolute (see #1430)
Kim Alvefur <zash@zash.se>
parents: 10393
diff changeset
   303
		-- Ensure paths are absolute, not relative to the working directory which we're about to change
82705ec87253 util.startup: Ensure prosody.paths are absolute (see #1430)
Kim Alvefur <zash@zash.se>
parents: 10393
diff changeset
   304
		local cwd = lfs.currentdir();
82705ec87253 util.startup: Ensure prosody.paths are absolute (see #1430)
Kim Alvefur <zash@zash.se>
parents: 10393
diff changeset
   305
		prosody.paths.source = config.resolve_relative_path(cwd, prosody.paths.source);
82705ec87253 util.startup: Ensure prosody.paths are absolute (see #1430)
Kim Alvefur <zash@zash.se>
parents: 10393
diff changeset
   306
		prosody.paths.config = config.resolve_relative_path(cwd, prosody.paths.config);
82705ec87253 util.startup: Ensure prosody.paths are absolute (see #1430)
Kim Alvefur <zash@zash.se>
parents: 10393
diff changeset
   307
		prosody.paths.data = config.resolve_relative_path(cwd, prosody.paths.data);
8667
d49acc9a8da2 util.startup: Fix chdir() to use correct path variable
Matthew Wild <mwild1@gmail.com>
parents: 8656
diff changeset
   308
		-- Change working directory to data path.
10394
82705ec87253 util.startup: Ensure prosody.paths are absolute (see #1430)
Kim Alvefur <zash@zash.se>
parents: 10393
diff changeset
   309
		lfs.chdir(prosody.paths.data);
8667
d49acc9a8da2 util.startup: Fix chdir() to use correct path variable
Matthew Wild <mwild1@gmail.com>
parents: 8656
diff changeset
   310
	end
d49acc9a8da2 util.startup: Fix chdir() to use correct path variable
Matthew Wild <mwild1@gmail.com>
parents: 8656
diff changeset
   311
end
d49acc9a8da2 util.startup: Fix chdir() to use correct path variable
Matthew Wild <mwild1@gmail.com>
parents: 8656
diff changeset
   312
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   313
function startup.add_global_prosody_functions()
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   314
	-- Function to reload the config file
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   315
	function prosody.reload_config()
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   316
		log("info", "Reloading configuration file");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   317
		prosody.events.fire_event("reloading-config");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   318
		local ok, level, err = config.load(prosody.config_file);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   319
		if not ok then
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   320
			if level == "parser" then
10112
659ffa03f1e7 util.startup: Remove tostring call from logging
Kim Alvefur <zash@zash.se>
parents: 9882
diff changeset
   321
				log("error", "There was an error parsing the configuration file: %s", err);
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   322
			elseif level == "file" then
10112
659ffa03f1e7 util.startup: Remove tostring call from logging
Kim Alvefur <zash@zash.se>
parents: 9882
diff changeset
   323
				log("error", "Couldn't read the config file when trying to reload: %s", err);
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   324
			end
8695
a55574754e5f configmanager: Move firing of the 'config-reloaded' event into util.startup (fixes #1117)
Kim Alvefur <zash@zash.se>
parents: 8691
diff changeset
   325
		else
a55574754e5f configmanager: Move firing of the 'config-reloaded' event into util.startup (fixes #1117)
Kim Alvefur <zash@zash.se>
parents: 8691
diff changeset
   326
			prosody.events.fire_event("config-reloaded", {
a55574754e5f configmanager: Move firing of the 'config-reloaded' event into util.startup (fixes #1117)
Kim Alvefur <zash@zash.se>
parents: 8691
diff changeset
   327
				filename = prosody.config_file,
a55574754e5f configmanager: Move firing of the 'config-reloaded' event into util.startup (fixes #1117)
Kim Alvefur <zash@zash.se>
parents: 8691
diff changeset
   328
				config = config.getconfig(),
a55574754e5f configmanager: Move firing of the 'config-reloaded' event into util.startup (fixes #1117)
Kim Alvefur <zash@zash.se>
parents: 8691
diff changeset
   329
			});
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   330
		end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   331
		return ok, (err and tostring(level)..": "..tostring(err)) or nil;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   332
	end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   333
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   334
	-- Function to reopen logfiles
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   335
	function prosody.reopen_logfiles()
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   336
		log("info", "Re-opening log files");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   337
		prosody.events.fire_event("reopen-log-files");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   338
	end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   339
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   340
	-- Function to initiate prosody shutdown
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   341
	function prosody.shutdown(reason, code)
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   342
		log("info", "Shutting down: %s", reason or "unknown reason");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   343
		prosody.shutdown_reason = reason;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   344
		prosody.shutdown_code = code;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   345
		prosody.events.fire_event("server-stopping", {
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   346
			reason = reason;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   347
			code = code;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   348
		});
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   349
		server.setquitting(true);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   350
	end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   351
end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   352
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   353
function startup.load_secondary_libraries()
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   354
	--- Load and initialise core modules
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   355
	require "util.import"
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   356
	require "util.xmppstream"
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   357
	require "core.stanza_router"
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   358
	require "core.statsmanager"
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   359
	require "core.hostmanager"
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   360
	require "core.portmanager"
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   361
	require "core.modulemanager"
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   362
	require "core.usermanager"
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   363
	require "core.rostermanager"
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   364
	require "core.sessionmanager"
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   365
	package.loaded['core.componentmanager'] = setmetatable({},{__index=function()
8961
b10a37f6f75f util.startup: Add a comment marking some compat code
Kim Alvefur <zash@zash.se>
parents: 8885
diff changeset
   366
		-- COMPAT which version?
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   367
		log("warn", "componentmanager is deprecated: %s", debug.traceback():match("\n[^\n]*\n[ \t]*([^\n]*)"));
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   368
		return function() end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   369
	end});
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   370
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   371
	require "util.array"
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   372
	require "util.datetime"
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   373
	require "util.iterators"
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   374
	require "util.timer"
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   375
	require "util.helpers"
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   376
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   377
	pcall(require, "util.signal") -- Not on Windows
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   378
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   379
	-- Commented to protect us from
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   380
	-- the second kind of people
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   381
	--[[
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   382
	pcall(require, "remdebug.engine");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   383
	if remdebug then remdebug.engine.start() end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   384
	]]
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   385
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   386
	require "util.stanza"
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   387
	require "util.jid"
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   388
end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   389
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   390
function startup.init_http_client()
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   391
	local http = require "net.http"
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   392
	local config_ssl = config.get("*", "ssl") or {}
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   393
	local https_client = config.get("*", "client_https_ssl")
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   394
	http.default.options.sslctx = require "core.certmanager".create_context("client_https port 0", "client",
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   395
		{ capath = config_ssl.capath, cafile = config_ssl.cafile, verify = "peer", }, https_client);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   396
end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   397
11952
dca75cc6fc5a util.startup: Integrate util.promise with net.server main loop
Kim Alvefur <zash@zash.se>
parents: 11874
diff changeset
   398
function startup.init_promise()
dca75cc6fc5a util.startup: Integrate util.promise with net.server main loop
Kim Alvefur <zash@zash.se>
parents: 11874
diff changeset
   399
	local promise = require "util.promise";
dca75cc6fc5a util.startup: Integrate util.promise with net.server main loop
Kim Alvefur <zash@zash.se>
parents: 11874
diff changeset
   400
dca75cc6fc5a util.startup: Integrate util.promise with net.server main loop
Kim Alvefur <zash@zash.se>
parents: 11874
diff changeset
   401
	local timer = require "util.timer";
dca75cc6fc5a util.startup: Integrate util.promise with net.server main loop
Kim Alvefur <zash@zash.se>
parents: 11874
diff changeset
   402
	promise.set_nexttick(function(f) return timer.add_task(0, f); end);
dca75cc6fc5a util.startup: Integrate util.promise with net.server main loop
Kim Alvefur <zash@zash.se>
parents: 11874
diff changeset
   403
end
dca75cc6fc5a util.startup: Integrate util.promise with net.server main loop
Kim Alvefur <zash@zash.se>
parents: 11874
diff changeset
   404
11967
f5c6be4a3ecc util.startup: Initialize util.async at startup
Matthew Wild <mwild1@gmail.com>
parents: 11952
diff changeset
   405
function startup.init_async()
f5c6be4a3ecc util.startup: Initialize util.async at startup
Matthew Wild <mwild1@gmail.com>
parents: 11952
diff changeset
   406
	local async = require "util.async";
f5c6be4a3ecc util.startup: Initialize util.async at startup
Matthew Wild <mwild1@gmail.com>
parents: 11952
diff changeset
   407
f5c6be4a3ecc util.startup: Initialize util.async at startup
Matthew Wild <mwild1@gmail.com>
parents: 11952
diff changeset
   408
	local timer = require "util.timer";
f5c6be4a3ecc util.startup: Initialize util.async at startup
Matthew Wild <mwild1@gmail.com>
parents: 11952
diff changeset
   409
	async.set_nexttick(function(f) return timer.add_task(0, f); end);
f5c6be4a3ecc util.startup: Initialize util.async at startup
Matthew Wild <mwild1@gmail.com>
parents: 11952
diff changeset
   410
	async.set_schedule_function(timer.add_task);
f5c6be4a3ecc util.startup: Initialize util.async at startup
Matthew Wild <mwild1@gmail.com>
parents: 11952
diff changeset
   411
end
f5c6be4a3ecc util.startup: Initialize util.async at startup
Matthew Wild <mwild1@gmail.com>
parents: 11952
diff changeset
   412
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   413
function startup.init_data_store()
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   414
	require "core.storagemanager";
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   415
end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   416
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   417
function startup.prepare_to_start()
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   418
	log("info", "Prosody is using the %s backend for connection handling", server.get_backend());
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   419
	-- Signal to modules that we are ready to start
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   420
	prosody.events.fire_event("server-starting");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   421
	prosody.start_time = os.time();
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   422
end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   423
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   424
function startup.init_global_protection()
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   425
	-- Catch global accesses
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   426
	-- luacheck: ignore 212/t
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   427
	local locked_globals_mt = {
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   428
		__index = function (t, k) log("warn", "%s", debug.traceback("Attempt to read a non-existent global '"..tostring(k).."'", 2)); end;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   429
		__newindex = function (t, k, v) error("Attempt to set a global: "..tostring(k).." = "..tostring(v), 2); end;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   430
	};
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   431
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   432
	function prosody.unlock_globals()
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   433
		setmetatable(_G, nil);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   434
	end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   435
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   436
	function prosody.lock_globals()
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   437
		setmetatable(_G, locked_globals_mt);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   438
	end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   439
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   440
	-- And lock now...
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   441
	prosody.lock_globals();
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   442
end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   443
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   444
function startup.read_version()
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   445
	-- Try to determine version
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   446
	local version_file = io.open((CFG_SOURCEDIR or ".").."/prosody.version");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   447
	prosody.version = "unknown";
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   448
	if version_file then
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   449
		prosody.version = version_file:read("*a"):gsub("%s*$", "");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   450
		version_file:close();
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   451
		if #prosody.version == 12 and prosody.version:match("^[a-f0-9]+$") then
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   452
			prosody.version = "hg:"..prosody.version;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   453
		end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   454
	else
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   455
		local hg = require"util.mercurial";
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   456
		local hgid = hg.check_id(CFG_SOURCEDIR or ".");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   457
		if hgid then prosody.version = "hg:" .. hgid; end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   458
	end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   459
end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   460
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   461
function startup.log_greeting()
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   462
	log("info", "Hello and welcome to Prosody version %s", prosody.version);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   463
end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   464
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   465
function startup.notify_started()
8640
c8368c7c81a1 util.startup: Trim trailing whitespace [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8639
diff changeset
   466
	prosody.events.fire_event("server-started");
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   467
end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   468
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   469
-- Override logging config (used by prosodyctl)
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   470
function startup.force_console_logging()
8669
57780ba1938f util.startup: Move original_logging_config to a local variable
Matthew Wild <mwild1@gmail.com>
parents: 8668
diff changeset
   471
	original_logging_config = config.get("*", "log");
11832
024ac556e907 prosodyctl: Add support for -v/--verbose to enable debug logging
Kim Alvefur <zash@zash.se>
parents: 11564
diff changeset
   472
	local log_level = os.getenv("PROSODYCTL_LOG_LEVEL");
024ac556e907 prosodyctl: Add support for -v/--verbose to enable debug logging
Kim Alvefur <zash@zash.se>
parents: 11564
diff changeset
   473
	if not log_level and prosody.opts.verbose then log_level = "debug"; end
024ac556e907 prosodyctl: Add support for -v/--verbose to enable debug logging
Kim Alvefur <zash@zash.se>
parents: 11564
diff changeset
   474
	config.set("*", "log", { { levels = { min = log_level or "info" }, to = "console" } });
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   475
end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   476
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   477
function startup.switch_user()
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   478
	-- Switch away from root and into the prosody user --
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   479
	-- NOTE: This function is only used by prosodyctl.
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   480
	-- The prosody process is built with the assumption that
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   481
	-- it is already started as the appropriate user.
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   482
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   483
	local want_pposix_version = "0.4.0";
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   484
	local have_pposix, pposix = pcall(require, "util.pposix");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   485
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   486
	if have_pposix and pposix then
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   487
		if pposix._VERSION ~= want_pposix_version then
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   488
			print(string.format("Unknown version (%s) of binary pposix module, expected %s",
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   489
				tostring(pposix._VERSION), want_pposix_version));
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   490
			os.exit(1);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   491
		end
8675
86b12ae8d427 util.startup: Expose user switching information via prosody global object
Matthew Wild <mwild1@gmail.com>
parents: 8670
diff changeset
   492
		prosody.current_uid = pposix.getuid();
10601
25a3c8134b0a prosody/util.startup: Switch to parse_args() for --root and --config
Matthew Wild <mwild1@gmail.com>
parents: 10600
diff changeset
   493
		local arg_root = prosody.opts.root;
8675
86b12ae8d427 util.startup: Expose user switching information via prosody global object
Matthew Wild <mwild1@gmail.com>
parents: 8670
diff changeset
   494
		if prosody.current_uid == 0 and config.get("*", "run_as_root") ~= true and not arg_root then
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   495
			-- We haz root!
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   496
			local desired_user = config.get("*", "prosody_user") or "prosody";
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   497
			local desired_group = config.get("*", "prosody_group") or desired_user;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   498
			local ok, err = pposix.setgid(desired_group);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   499
			if ok then
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   500
				ok, err = pposix.initgroups(desired_user);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   501
			end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   502
			if ok then
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   503
				ok, err = pposix.setuid(desired_user);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   504
				if ok then
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   505
					-- Yay!
8675
86b12ae8d427 util.startup: Expose user switching information via prosody global object
Matthew Wild <mwild1@gmail.com>
parents: 8670
diff changeset
   506
					prosody.switched_user = true;
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   507
				end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   508
			end
8675
86b12ae8d427 util.startup: Expose user switching information via prosody global object
Matthew Wild <mwild1@gmail.com>
parents: 8670
diff changeset
   509
			if not prosody.switched_user then
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   510
				-- Boo!
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   511
				print("Warning: Couldn't switch to Prosody user/group '"..tostring(desired_user).."'/'"..tostring(desired_group).."': "..tostring(err));
11870
515a89dee6ae util.startup: Skip config readability check in migrator (thanks eTaurus)
Kim Alvefur <zash@zash.se>
parents: 11851
diff changeset
   512
			elseif prosody.config_file then
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   513
				-- Make sure the Prosody user can read the config
10536
19ec384eb782 util.startup: Ignore unused errno variable [luacheck]
Kim Alvefur <zash@zash.se>
parents: 10409
diff changeset
   514
				local conf, err, errno = io.open(prosody.config_file); --luacheck: ignore 211/errno
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   515
				if conf then
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   516
					conf:close();
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   517
				else
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   518
					print("The config file is not readable by the '"..desired_user.."' user.");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   519
					print("Prosody will not be able to read it.");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   520
					print("Error was "..err);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   521
					os.exit(1);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   522
				end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   523
			end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   524
		end
8640
c8368c7c81a1 util.startup: Trim trailing whitespace [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8639
diff changeset
   525
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   526
		-- Set our umask to protect data files
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   527
		pposix.umask(config.get("*", "umask") or "027");
8670
a05d36075c6a util.startup: Fix variable usage [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 8669
diff changeset
   528
		pposix.setenv("HOME", prosody.paths.data);
a05d36075c6a util.startup: Fix variable usage [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 8669
diff changeset
   529
		pposix.setenv("PROSODY_CONFIG", prosody.config_file);
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   530
	else
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   531
		print("Error: Unable to load pposix module. Check that Prosody is installed correctly.")
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   532
		print("For more help send the below error to us through https://prosody.im/discuss");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   533
		print(tostring(pposix))
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   534
		os.exit(1);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   535
	end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   536
end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   537
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   538
function startup.check_unwriteable()
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   539
	local function test_writeable(filename)
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   540
		local f, err = io.open(filename, "a");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   541
		if not f then
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   542
			return false, err;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   543
		end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   544
		f:close();
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   545
		return true;
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   546
	end
8640
c8368c7c81a1 util.startup: Trim trailing whitespace [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8639
diff changeset
   547
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   548
	local unwriteable_files = {};
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   549
	if type(original_logging_config) == "string" and original_logging_config:sub(1,1) ~= "*" then
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   550
		local ok, err = test_writeable(original_logging_config);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   551
		if not ok then
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   552
			table.insert(unwriteable_files, err);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   553
		end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   554
	elseif type(original_logging_config) == "table" then
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   555
		for _, rule in ipairs(original_logging_config) do
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   556
			if rule.filename then
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   557
				local ok, err = test_writeable(rule.filename);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   558
				if not ok then
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   559
					table.insert(unwriteable_files, err);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   560
				end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   561
			end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   562
		end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   563
	end
8640
c8368c7c81a1 util.startup: Trim trailing whitespace [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8639
diff changeset
   564
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   565
	if #unwriteable_files > 0 then
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   566
		print("One of more of the Prosody log files are not");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   567
		print("writeable, please correct the errors and try");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   568
		print("starting prosodyctl again.");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   569
		print("");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   570
		for _, err in ipairs(unwriteable_files) do
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   571
			print(err);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   572
		end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   573
		print("");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   574
		os.exit(1);
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   575
	end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   576
end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   577
11077
5691b9773c5b util.startup: Configure the GC on startup, using the config or built-in defaults
Matthew Wild <mwild1@gmail.com>
parents: 10951
diff changeset
   578
function startup.init_gc()
5691b9773c5b util.startup: Configure the GC on startup, using the config or built-in defaults
Matthew Wild <mwild1@gmail.com>
parents: 10951
diff changeset
   579
	-- Apply garbage collector settings from the config file
5691b9773c5b util.startup: Configure the GC on startup, using the config or built-in defaults
Matthew Wild <mwild1@gmail.com>
parents: 10951
diff changeset
   580
	local gc = require "util.gc";
5691b9773c5b util.startup: Configure the GC on startup, using the config or built-in defaults
Matthew Wild <mwild1@gmail.com>
parents: 10951
diff changeset
   581
	local gc_settings = config.get("*", "gc") or { mode = default_gc_params.mode };
5691b9773c5b util.startup: Configure the GC on startup, using the config or built-in defaults
Matthew Wild <mwild1@gmail.com>
parents: 10951
diff changeset
   582
5691b9773c5b util.startup: Configure the GC on startup, using the config or built-in defaults
Matthew Wild <mwild1@gmail.com>
parents: 10951
diff changeset
   583
	local ok, err = gc.configure(gc_settings, default_gc_params);
5691b9773c5b util.startup: Configure the GC on startup, using the config or built-in defaults
Matthew Wild <mwild1@gmail.com>
parents: 10951
diff changeset
   584
	if not ok then
5691b9773c5b util.startup: Configure the GC on startup, using the config or built-in defaults
Matthew Wild <mwild1@gmail.com>
parents: 10951
diff changeset
   585
		log("error", "Failed to apply GC configuration: %s", err);
5691b9773c5b util.startup: Configure the GC on startup, using the config or built-in defaults
Matthew Wild <mwild1@gmail.com>
parents: 10951
diff changeset
   586
		return nil, err;
5691b9773c5b util.startup: Configure the GC on startup, using the config or built-in defaults
Matthew Wild <mwild1@gmail.com>
parents: 10951
diff changeset
   587
	end
5691b9773c5b util.startup: Configure the GC on startup, using the config or built-in defaults
Matthew Wild <mwild1@gmail.com>
parents: 10951
diff changeset
   588
	return true;
5691b9773c5b util.startup: Configure the GC on startup, using the config or built-in defaults
Matthew Wild <mwild1@gmail.com>
parents: 10951
diff changeset
   589
end
5691b9773c5b util.startup: Configure the GC on startup, using the config or built-in defaults
Matthew Wild <mwild1@gmail.com>
parents: 10951
diff changeset
   590
11054
51be24b16e8a util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents: 10952
diff changeset
   591
function startup.init_errors()
11056
1f42b08b134f util.startup: Init util.error with defaults if none given
Matthew Wild <mwild1@gmail.com>
parents: 11054
diff changeset
   592
	require "util.error".configure(config.get("*", "error_library") or {});
11054
51be24b16e8a util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents: 10952
diff changeset
   593
end
51be24b16e8a util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents: 10952
diff changeset
   594
8676
6aeed79d9283 util.startup: Expose make_host() function
Matthew Wild <mwild1@gmail.com>
parents: 8675
diff changeset
   595
function startup.make_host(hostname)
6aeed79d9283 util.startup: Expose make_host() function
Matthew Wild <mwild1@gmail.com>
parents: 8675
diff changeset
   596
	return {
6aeed79d9283 util.startup: Expose make_host() function
Matthew Wild <mwild1@gmail.com>
parents: 8675
diff changeset
   597
		type = "local",
6aeed79d9283 util.startup: Expose make_host() function
Matthew Wild <mwild1@gmail.com>
parents: 8675
diff changeset
   598
		events = prosody.events,
6aeed79d9283 util.startup: Expose make_host() function
Matthew Wild <mwild1@gmail.com>
parents: 8675
diff changeset
   599
		modules = {},
6aeed79d9283 util.startup: Expose make_host() function
Matthew Wild <mwild1@gmail.com>
parents: 8675
diff changeset
   600
		sessions = {},
6aeed79d9283 util.startup: Expose make_host() function
Matthew Wild <mwild1@gmail.com>
parents: 8675
diff changeset
   601
		users = require "core.usermanager".new_null_provider(hostname)
6aeed79d9283 util.startup: Expose make_host() function
Matthew Wild <mwild1@gmail.com>
parents: 8675
diff changeset
   602
	};
6aeed79d9283 util.startup: Expose make_host() function
Matthew Wild <mwild1@gmail.com>
parents: 8675
diff changeset
   603
end
6aeed79d9283 util.startup: Expose make_host() function
Matthew Wild <mwild1@gmail.com>
parents: 8675
diff changeset
   604
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   605
function startup.make_dummy_hosts()
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   606
	-- When running under prosodyctl, we don't want to
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   607
	-- fully initialize the server, so we populate prosody.hosts
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   608
	-- with just enough things for most code to work correctly
8641
f8f45bbbd8ba util.startup: Ignore various globals being read and written as part of startup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8640
diff changeset
   609
	-- luacheck: ignore 122/hosts
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   610
	prosody.core_post_stanza = function () end; -- TODO: mod_router!
8640
c8368c7c81a1 util.startup: Trim trailing whitespace [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8639
diff changeset
   611
8642
070a77c15f63 util.startup: Remove unused loop variable [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
   612
	for hostname in pairs(config.getconfig()) do
8718
25d8d6091ec3 util.startup: Access the hosts table via the prosody global for consistency
Kim Alvefur <zash@zash.se>
parents: 8716
diff changeset
   613
		prosody.hosts[hostname] = startup.make_host(hostname);
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   614
	end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   615
end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   616
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   617
-- prosodyctl only
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   618
function startup.prosodyctl()
10640
a9c975a0f113 util.startup: expose current process type (prosody/prosodyctl) in the global prosody object
Matthew Wild <mwild1@gmail.com>
parents: 10608
diff changeset
   619
	prosody.process_type = "prosodyctl";
10601
25a3c8134b0a prosody/util.startup: Switch to parse_args() for --root and --config
Matthew Wild <mwild1@gmail.com>
parents: 10600
diff changeset
   620
	startup.parse_args();
8702
580c13ed0ca1 util.startup: Initialize the 'prosody' global earlier (various things needs the global util.events instance)
Kim Alvefur <zash@zash.se>
parents: 8701
diff changeset
   621
	startup.init_global_state();
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   622
	startup.read_config();
8758
857d8f38a010 util.startup: Force console logging before initializing logging (see 2fdeb979cc7c)
Kim Alvefur <zash@zash.se>
parents: 8751
diff changeset
   623
	startup.force_console_logging();
8751
2fdeb979cc7c util.startup: Initialize logging immediately after configuration is read (which is how it used to work)
Matthew Wild <mwild1@gmail.com>
parents: 8737
diff changeset
   624
	startup.init_logging();
11077
5691b9773c5b util.startup: Configure the GC on startup, using the config or built-in defaults
Matthew Wild <mwild1@gmail.com>
parents: 10951
diff changeset
   625
	startup.init_gc();
11054
51be24b16e8a util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents: 10952
diff changeset
   626
	startup.init_errors();
8701
0499f3da0ec4 util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents: 8695
diff changeset
   627
	startup.setup_plugindir();
11142
2a19d61f4ae4 util.startup: Re-enable installer path setup
Kim Alvefur <zash@zash.se>
parents: 11141
diff changeset
   628
	startup.setup_plugin_install_path();
8701
0499f3da0ec4 util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents: 8695
diff changeset
   629
	startup.setup_datadir();
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   630
	startup.chdir();
8691
019b4b3dd5ad util.startup: Read version for prosodyctl (restores version in 'about' command)
Kim Alvefur <zash@zash.se>
parents: 8685
diff changeset
   631
	startup.read_version();
8668
4b260a3f8b94 util.startup: Restore user switching
Matthew Wild <mwild1@gmail.com>
parents: 8667
diff changeset
   632
	startup.switch_user();
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   633
	startup.check_dependencies();
9877
dfaeea570f7e util.startup: Give function a more generic name so it can apply to all warnings
Matthew Wild <mwild1@gmail.com>
parents: 9766
diff changeset
   634
	startup.log_startup_warnings();
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   635
	startup.check_unwriteable();
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   636
	startup.load_libraries();
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   637
	startup.init_http_client();
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   638
	startup.make_dummy_hosts();
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   639
end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   640
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   641
function startup.prosody()
8685
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8676
diff changeset
   642
	-- These actions are in a strict order, as many depend on
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8676
diff changeset
   643
	-- previous steps to have already been performed
10640
a9c975a0f113 util.startup: expose current process type (prosody/prosodyctl) in the global prosody object
Matthew Wild <mwild1@gmail.com>
parents: 10608
diff changeset
   644
	prosody.process_type = "prosody";
10601
25a3c8134b0a prosody/util.startup: Switch to parse_args() for --root and --config
Matthew Wild <mwild1@gmail.com>
parents: 10600
diff changeset
   645
	startup.parse_args();
8702
580c13ed0ca1 util.startup: Initialize the 'prosody' global earlier (various things needs the global util.events instance)
Kim Alvefur <zash@zash.se>
parents: 8701
diff changeset
   646
	startup.init_global_state();
8685
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8676
diff changeset
   647
	startup.read_config();
8751
2fdeb979cc7c util.startup: Initialize logging immediately after configuration is read (which is how it used to work)
Matthew Wild <mwild1@gmail.com>
parents: 8737
diff changeset
   648
	startup.init_logging();
11077
5691b9773c5b util.startup: Configure the GC on startup, using the config or built-in defaults
Matthew Wild <mwild1@gmail.com>
parents: 10951
diff changeset
   649
	startup.init_gc();
11054
51be24b16e8a util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents: 10952
diff changeset
   650
	startup.init_errors();
8685
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8676
diff changeset
   651
	startup.sanity_check();
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8676
diff changeset
   652
	startup.sandbox_require();
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8676
diff changeset
   653
	startup.set_function_metatable();
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8676
diff changeset
   654
	startup.check_dependencies();
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8676
diff changeset
   655
	startup.load_libraries();
8701
0499f3da0ec4 util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents: 8695
diff changeset
   656
	startup.setup_plugindir();
11142
2a19d61f4ae4 util.startup: Re-enable installer path setup
Kim Alvefur <zash@zash.se>
parents: 11141
diff changeset
   657
	startup.setup_plugin_install_path();
8701
0499f3da0ec4 util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents: 8695
diff changeset
   658
	startup.setup_datadir();
8685
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8676
diff changeset
   659
	startup.chdir();
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8676
diff changeset
   660
	startup.add_global_prosody_functions();
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8676
diff changeset
   661
	startup.read_version();
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8676
diff changeset
   662
	startup.log_greeting();
9877
dfaeea570f7e util.startup: Give function a more generic name so it can apply to all warnings
Matthew Wild <mwild1@gmail.com>
parents: 9766
diff changeset
   663
	startup.log_startup_warnings();
8685
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8676
diff changeset
   664
	startup.load_secondary_libraries();
11952
dca75cc6fc5a util.startup: Integrate util.promise with net.server main loop
Kim Alvefur <zash@zash.se>
parents: 11874
diff changeset
   665
	startup.init_promise();
11967
f5c6be4a3ecc util.startup: Initialize util.async at startup
Matthew Wild <mwild1@gmail.com>
parents: 11952
diff changeset
   666
	startup.init_async();
8685
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8676
diff changeset
   667
	startup.init_http_client();
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8676
diff changeset
   668
	startup.init_data_store();
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8676
diff changeset
   669
	startup.init_global_protection();
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8676
diff changeset
   670
	startup.prepare_to_start();
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8676
diff changeset
   671
	startup.notify_started();
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   672
end
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   673
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   674
return startup;