Mercurial > prosody-hg
annotate util/startup.lua @ 14229:ce31fdde0ad1 default tip
net.unbound: Simplify conditional
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Sat, 13 Jun 2026 11:35:18 +0200 |
| parents | 2b13c24ba2d6 |
| children |
| rev | line source |
|---|---|
|
8638
f8f45bbbd8ba
util.startup: Ignore various globals being read and written as part of startup [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8637
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:
8637
diff
changeset
|
2 -- luacheck: ignore 113/CFG_CONFIGDIR 113/CFG_SOURCEDIR 113/CFG_DATADIR 113/CFG_PLUGINDIR |
|
8635
47e3b8b6f17a
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 |
|
12975
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
5 local prosody = { events = require "prosody.util.events".new() }; |
|
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
6 local logger = require "prosody.util.logger"; |
|
8720
dba17a70fd22
util.startup: Initialize prosody.log / _G.log here instead of in loggingmanager to reduce dependencies
Kim Alvefur <zash@zash.se>
parents:
8719
diff
changeset
|
7 local log = logger.init("startup"); |
|
12975
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
8 local parse_args = require "prosody.util.argparse".parse; |
|
8635
47e3b8b6f17a
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 |
|
12975
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
10 local config = require "prosody.core.configmanager"; |
|
9877
ded5303e1fde
util.startup: Log configuration warnings at startup
Matthew Wild <mwild1@gmail.com>
parents:
9873
diff
changeset
|
11 local config_warnings; |
|
8635
47e3b8b6f17a
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 |
|
12975
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
13 local dependencies = require "prosody.util.dependencies"; |
|
8635
47e3b8b6f17a
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 |
|
8666
57780ba1938f
util.startup: Move original_logging_config to a local variable
Matthew Wild <mwild1@gmail.com>
parents:
8665
diff
changeset
|
15 local original_logging_config; |
|
57780ba1938f
util.startup: Move original_logging_config to a local variable
Matthew Wild <mwild1@gmail.com>
parents:
8665
diff
changeset
|
16 |
|
11539
3413fea9e6db
util.startup: Set more aggressive defaults for GC
Matthew Wild <mwild1@gmail.com>
parents:
11073
diff
changeset
|
17 local default_gc_params = { |
|
3413fea9e6db
util.startup: Set more aggressive defaults for GC
Matthew Wild <mwild1@gmail.com>
parents:
11073
diff
changeset
|
18 mode = "incremental"; |
|
3413fea9e6db
util.startup: Set more aggressive defaults for GC
Matthew Wild <mwild1@gmail.com>
parents:
11073
diff
changeset
|
19 -- Incremental mode defaults |
|
3413fea9e6db
util.startup: Set more aggressive defaults for GC
Matthew Wild <mwild1@gmail.com>
parents:
11073
diff
changeset
|
20 threshold = 105, speed = 500; |
|
3413fea9e6db
util.startup: Set more aggressive defaults for GC
Matthew Wild <mwild1@gmail.com>
parents:
11073
diff
changeset
|
21 -- Generational mode defaults |
|
3413fea9e6db
util.startup: Set more aggressive defaults for GC
Matthew Wild <mwild1@gmail.com>
parents:
11073
diff
changeset
|
22 minor_threshold = 20, major_threshold = 50; |
|
13929
203a0b5ade55
util.startup: Add Lua 5.5 GC defaults
Kim Alvefur <zash@zash.se>
parents:
13743
diff
changeset
|
23 -- Lua 5.5 |
|
203a0b5ade55
util.startup: Add Lua 5.5 GC defaults
Kim Alvefur <zash@zash.se>
parents:
13743
diff
changeset
|
24 minor_multiplier = 20; |
|
203a0b5ade55
util.startup: Add Lua 5.5 GC defaults
Kim Alvefur <zash@zash.se>
parents:
13743
diff
changeset
|
25 major_minor_threshold = 50; |
|
203a0b5ade55
util.startup: Add Lua 5.5 GC defaults
Kim Alvefur <zash@zash.se>
parents:
13743
diff
changeset
|
26 minor_major_threshold = 68; |
|
11539
3413fea9e6db
util.startup: Set more aggressive defaults for GC
Matthew Wild <mwild1@gmail.com>
parents:
11073
diff
changeset
|
27 }; |
|
11073
5691b9773c5b
util.startup: Configure the GC on startup, using the config or built-in defaults
Matthew Wild <mwild1@gmail.com>
parents:
10947
diff
changeset
|
28 |
|
11829
4fad0ca42f66
util.startup: Allow separate command line argument settings for prosody and prosodyctl
Kim Alvefur <zash@zash.se>
parents:
11828
diff
changeset
|
29 local arg_settigs = { |
|
4fad0ca42f66
util.startup: Allow separate command line argument settings for prosody and prosodyctl
Kim Alvefur <zash@zash.se>
parents:
11828
diff
changeset
|
30 prosody = { |
|
11869
d52a73425eba
util.startup: Show brief usage on `prosody -h|-?|--help`
Kim Alvefur <zash@zash.se>
parents:
11866
diff
changeset
|
31 short_params = { D = "daemonize"; F = "no-daemonize", h = "help", ["?"] = "help" }; |
|
11829
4fad0ca42f66
util.startup: Allow separate command line argument settings for prosody and prosodyctl
Kim Alvefur <zash@zash.se>
parents:
11828
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:
11828
diff
changeset
|
33 }; |
|
4fad0ca42f66
util.startup: Allow separate command line argument settings for prosody and prosodyctl
Kim Alvefur <zash@zash.se>
parents:
11828
diff
changeset
|
34 prosodyctl = { |
|
11870
1d1ed2be3491
util.startup: Understand -h, -? as --help in prosodyctl but ignore
Kim Alvefur <zash@zash.se>
parents:
11869
diff
changeset
|
35 short_params = { v = "verbose", h = "help", ["?"] = "help" }; |
|
11829
4fad0ca42f66
util.startup: Allow separate command line argument settings for prosody and prosodyctl
Kim Alvefur <zash@zash.se>
parents:
11828
diff
changeset
|
36 value_params = { config = true }; |
|
4fad0ca42f66
util.startup: Allow separate command line argument settings for prosody and prosodyctl
Kim Alvefur <zash@zash.se>
parents:
11828
diff
changeset
|
37 }; |
|
4fad0ca42f66
util.startup: Allow separate command line argument settings for prosody and prosodyctl
Kim Alvefur <zash@zash.se>
parents:
11828
diff
changeset
|
38 } |
|
10596
cb107ea49b35
util.startup: Add startup step for parsing command-line options
Matthew Wild <mwild1@gmail.com>
parents:
10390
diff
changeset
|
39 |
|
12160
ac654fb19203
util.startup: Allow supplying an argument parsing settings
Kim Alvefur <zash@zash.se>
parents:
11963
diff
changeset
|
40 function startup.parse_args(profile) |
|
ac654fb19203
util.startup: Allow supplying an argument parsing settings
Kim Alvefur <zash@zash.se>
parents:
11963
diff
changeset
|
41 local opts, err, where = parse_args(arg, arg_settigs[profile or prosody.process_type] or profile); |
|
10936
d770435f0f84
util.argparse: Move exiting and error to util.startup
Kim Alvefur <zash@zash.se>
parents:
10934
diff
changeset
|
42 if not opts then |
|
d770435f0f84
util.argparse: Move exiting and error to util.startup
Kim Alvefur <zash@zash.se>
parents:
10934
diff
changeset
|
43 if err == "param-not-found" then |
|
d770435f0f84
util.argparse: Move exiting and error to util.startup
Kim Alvefur <zash@zash.se>
parents:
10934
diff
changeset
|
44 print("Unknown command-line option: "..tostring(where)); |
|
11847
2b3ce80ffece
util.startup: Only ask if 'prosodyctl' was meant instead of 'prosody' (fix #1692)
Kim Alvefur <zash@zash.se>
parents:
11846
diff
changeset
|
45 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:
11846
diff
changeset
|
46 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:
11846
diff
changeset
|
47 end |
|
10936
d770435f0f84
util.argparse: Move exiting and error to util.startup
Kim Alvefur <zash@zash.se>
parents:
10934
diff
changeset
|
48 elseif err == "missing-value" then |
|
d770435f0f84
util.argparse: Move exiting and error to util.startup
Kim Alvefur <zash@zash.se>
parents:
10934
diff
changeset
|
49 print("Expected a value to follow command-line option: "..where); |
|
10600
08f2fe5ac30f
util.startup: Fix logic to make --config work again
Matthew Wild <mwild1@gmail.com>
parents:
10597
diff
changeset
|
50 end |
|
10936
d770435f0f84
util.argparse: Move exiting and error to util.startup
Kim Alvefur <zash@zash.se>
parents:
10934
diff
changeset
|
51 os.exit(1); |
|
10596
cb107ea49b35
util.startup: Add startup step for parsing command-line options
Matthew Wild <mwild1@gmail.com>
parents:
10390
diff
changeset
|
52 end |
|
12423
3ae9299d61d7
util.startup: Show error for unrecognized arguments passed to 'prosody' (fixes #1722)
Matthew Wild <mwild1@gmail.com>
parents:
12294
diff
changeset
|
53 if prosody.process_type == "prosody" then |
|
3ae9299d61d7
util.startup: Show error for unrecognized arguments passed to 'prosody' (fixes #1722)
Matthew Wild <mwild1@gmail.com>
parents:
12294
diff
changeset
|
54 if #arg > 0 then |
|
3ae9299d61d7
util.startup: Show error for unrecognized arguments passed to 'prosody' (fixes #1722)
Matthew Wild <mwild1@gmail.com>
parents:
12294
diff
changeset
|
55 print("Unrecognized option: "..arg[1]); |
|
3ae9299d61d7
util.startup: Show error for unrecognized arguments passed to 'prosody' (fixes #1722)
Matthew Wild <mwild1@gmail.com>
parents:
12294
diff
changeset
|
56 print("(Did you mean 'prosodyctl "..arg[1].."'?)"); |
|
3ae9299d61d7
util.startup: Show error for unrecognized arguments passed to 'prosody' (fixes #1722)
Matthew Wild <mwild1@gmail.com>
parents:
12294
diff
changeset
|
57 print(""); |
|
3ae9299d61d7
util.startup: Show error for unrecognized arguments passed to 'prosody' (fixes #1722)
Matthew Wild <mwild1@gmail.com>
parents:
12294
diff
changeset
|
58 end |
|
3ae9299d61d7
util.startup: Show error for unrecognized arguments passed to 'prosody' (fixes #1722)
Matthew Wild <mwild1@gmail.com>
parents:
12294
diff
changeset
|
59 if opts.help or #arg > 0 then |
|
3ae9299d61d7
util.startup: Show error for unrecognized arguments passed to 'prosody' (fixes #1722)
Matthew Wild <mwild1@gmail.com>
parents:
12294
diff
changeset
|
60 print("prosody [ -D | -F ] [ --config /path/to/prosody.cfg.lua ]"); |
|
3ae9299d61d7
util.startup: Show error for unrecognized arguments passed to 'prosody' (fixes #1722)
Matthew Wild <mwild1@gmail.com>
parents:
12294
diff
changeset
|
61 print(" -D, --daemonize Run in the background") |
|
3ae9299d61d7
util.startup: Show error for unrecognized arguments passed to 'prosody' (fixes #1722)
Matthew Wild <mwild1@gmail.com>
parents:
12294
diff
changeset
|
62 print(" -F, --no-daemonize Run in the foreground") |
|
3ae9299d61d7
util.startup: Show error for unrecognized arguments passed to 'prosody' (fixes #1722)
Matthew Wild <mwild1@gmail.com>
parents:
12294
diff
changeset
|
63 print(" --config FILE Specify config file") |
|
3ae9299d61d7
util.startup: Show error for unrecognized arguments passed to 'prosody' (fixes #1722)
Matthew Wild <mwild1@gmail.com>
parents:
12294
diff
changeset
|
64 os.exit(0); |
|
3ae9299d61d7
util.startup: Show error for unrecognized arguments passed to 'prosody' (fixes #1722)
Matthew Wild <mwild1@gmail.com>
parents:
12294
diff
changeset
|
65 end |
|
11869
d52a73425eba
util.startup: Show brief usage on `prosody -h|-?|--help`
Kim Alvefur <zash@zash.se>
parents:
11866
diff
changeset
|
66 end |
|
10936
d770435f0f84
util.argparse: Move exiting and error to util.startup
Kim Alvefur <zash@zash.se>
parents:
10934
diff
changeset
|
67 prosody.opts = opts; |
|
10596
cb107ea49b35
util.startup: Add startup step for parsing command-line options
Matthew Wild <mwild1@gmail.com>
parents:
10390
diff
changeset
|
68 end |
|
cb107ea49b35
util.startup: Add startup step for parsing command-line options
Matthew Wild <mwild1@gmail.com>
parents:
10390
diff
changeset
|
69 |
|
8635
47e3b8b6f17a
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 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
|
71 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
|
72 |
|
47e3b8b6f17a
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 local filename; |
|
10597
25a3c8134b0a
prosody/util.startup: Switch to parse_args() for --root and --config
Matthew Wild <mwild1@gmail.com>
parents:
10596
diff
changeset
|
74 if prosody.opts.config then |
|
25a3c8134b0a
prosody/util.startup: Switch to parse_args() for --root and --config
Matthew Wild <mwild1@gmail.com>
parents:
10596
diff
changeset
|
75 table.insert(filenames, prosody.opts.config); |
|
8635
47e3b8b6f17a
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 CFG_CONFIGDIR then |
|
10597
25a3c8134b0a
prosody/util.startup: Switch to parse_args() for --root and --config
Matthew Wild <mwild1@gmail.com>
parents:
10596
diff
changeset
|
77 table.insert(filenames, CFG_CONFIGDIR.."/"..prosody.opts.config); |
|
8635
47e3b8b6f17a
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 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
|
79 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
|
80 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
|
81 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
|
82 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
|
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 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
|
85 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
|
86 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
|
87 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
|
88 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
|
89 prosody.config_file = filename; |
|
10389
dbb8dae58265
util.startup: Update config path (fixes #1430)
Kim Alvefur <zash@zash.se>
parents:
9762
diff
changeset
|
90 prosody.paths.config = filename:match("^(.*)[\\/][^\\/]*$"); |
|
dbb8dae58265
util.startup: Update config path (fixes #1430)
Kim Alvefur <zash@zash.se>
parents:
9762
diff
changeset
|
91 CFG_CONFIGDIR = prosody.paths.config; -- luacheck: ignore 111 |
|
8635
47e3b8b6f17a
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 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
|
93 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
|
94 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
|
95 prosody.config_file = filename |
|
13743
0c7e11c11968
core.configmanager: Remove dependency on 'prosody' global for Credential
Kim Alvefur <zash@zash.se>
parents:
13659
diff
changeset
|
96 local credentials_directory = os.getenv("CREDENTIALS_DIRECTORY"); |
|
0c7e11c11968
core.configmanager: Remove dependency on 'prosody' global for Credential
Kim Alvefur <zash@zash.se>
parents:
13659
diff
changeset
|
97 if credentials_directory then |
|
0c7e11c11968
core.configmanager: Remove dependency on 'prosody' global for Credential
Kim Alvefur <zash@zash.se>
parents:
13659
diff
changeset
|
98 config.set_credentials_directory(credentials_directory); |
|
0c7e11c11968
core.configmanager: Remove dependency on 'prosody' global for Credential
Kim Alvefur <zash@zash.se>
parents:
13659
diff
changeset
|
99 elseif prosody.process_type == "prosody" then |
|
0c7e11c11968
core.configmanager: Remove dependency on 'prosody' global for Credential
Kim Alvefur <zash@zash.se>
parents:
13659
diff
changeset
|
100 config.set_credential_fallback_mode("error"); |
|
0c7e11c11968
core.configmanager: Remove dependency on 'prosody' global for Credential
Kim Alvefur <zash@zash.se>
parents:
13659
diff
changeset
|
101 else |
|
0c7e11c11968
core.configmanager: Remove dependency on 'prosody' global for Credential
Kim Alvefur <zash@zash.se>
parents:
13659
diff
changeset
|
102 config.set_credential_fallback_mode("warn"); |
|
0c7e11c11968
core.configmanager: Remove dependency on 'prosody' global for Credential
Kim Alvefur <zash@zash.se>
parents:
13659
diff
changeset
|
103 end |
|
14130
a957cd3ea4eb
util.startup: Improve error message when failing to load config file
Matthew Wild <mwild1@gmail.com>
parents:
13979
diff
changeset
|
104 local ok, level, err, errno = config.load(filename); |
|
8635
47e3b8b6f17a
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 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
|
106 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
|
107 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
|
108 if level == "parser" then |
|
8728
41c959c5c84b
Fix spelling throughout the codebase [codespell]
Kim Alvefur <zash@zash.se>
parents:
8721
diff
changeset
|
109 print("A problem occurred while reading the config file "..filename); |
|
8635
47e3b8b6f17a
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 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
|
111 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
|
112 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
|
113 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
|
114 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
|
115 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
|
116 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
|
117 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
|
118 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
|
119 elseif level == "file" then |
|
14130
a957cd3ea4eb
util.startup: Improve error message when failing to load config file
Matthew Wild <mwild1@gmail.com>
parents:
13979
diff
changeset
|
120 local err_kind; |
|
a957cd3ea4eb
util.startup: Improve error message when failing to load config file
Matthew Wild <mwild1@gmail.com>
parents:
13979
diff
changeset
|
121 if errno == 2 then |
|
a957cd3ea4eb
util.startup: Improve error message when failing to load config file
Matthew Wild <mwild1@gmail.com>
parents:
13979
diff
changeset
|
122 err_kind = "find"; |
|
a957cd3ea4eb
util.startup: Improve error message when failing to load config file
Matthew Wild <mwild1@gmail.com>
parents:
13979
diff
changeset
|
123 elseif errno == 13 then |
|
a957cd3ea4eb
util.startup: Improve error message when failing to load config file
Matthew Wild <mwild1@gmail.com>
parents:
13979
diff
changeset
|
124 err_kind = "access"; |
|
a957cd3ea4eb
util.startup: Improve error message when failing to load config file
Matthew Wild <mwild1@gmail.com>
parents:
13979
diff
changeset
|
125 else err_kind = "open"; |
|
a957cd3ea4eb
util.startup: Improve error message when failing to load config file
Matthew Wild <mwild1@gmail.com>
parents:
13979
diff
changeset
|
126 end |
|
a957cd3ea4eb
util.startup: Improve error message when failing to load config file
Matthew Wild <mwild1@gmail.com>
parents:
13979
diff
changeset
|
127 print(("Prosody was unable to %s the configuration file:"):format(err_kind)); |
|
a957cd3ea4eb
util.startup: Improve error message when failing to load config file
Matthew Wild <mwild1@gmail.com>
parents:
13979
diff
changeset
|
128 print(filename); |
|
a957cd3ea4eb
util.startup: Improve error message when failing to load config file
Matthew Wild <mwild1@gmail.com>
parents:
13979
diff
changeset
|
129 print(""); |
|
a957cd3ea4eb
util.startup: Improve error message when failing to load config file
Matthew Wild <mwild1@gmail.com>
parents:
13979
diff
changeset
|
130 if err_kind == "access" then |
|
a957cd3ea4eb
util.startup: Improve error message when failing to load config file
Matthew Wild <mwild1@gmail.com>
parents:
13979
diff
changeset
|
131 print(prosody.process_type.." does not have permission to open the file."); |
|
a957cd3ea4eb
util.startup: Improve error message when failing to load config file
Matthew Wild <mwild1@gmail.com>
parents:
13979
diff
changeset
|
132 print("Try running the command again as a user with permission to read the config file."); |
|
a957cd3ea4eb
util.startup: Improve error message when failing to load config file
Matthew Wild <mwild1@gmail.com>
parents:
13979
diff
changeset
|
133 elseif err_kind == "find" then |
|
a957cd3ea4eb
util.startup: Improve error message when failing to load config file
Matthew Wild <mwild1@gmail.com>
parents:
13979
diff
changeset
|
134 print("A sample config file is included in the Prosody download called prosody.cfg.lua.dist"); |
|
a957cd3ea4eb
util.startup: Improve error message when failing to load config file
Matthew Wild <mwild1@gmail.com>
parents:
13979
diff
changeset
|
135 print("Copy or rename it to "..filename.." and edit as necessary."); |
|
a957cd3ea4eb
util.startup: Improve error message when failing to load config file
Matthew Wild <mwild1@gmail.com>
parents:
13979
diff
changeset
|
136 else |
|
a957cd3ea4eb
util.startup: Improve error message when failing to load config file
Matthew Wild <mwild1@gmail.com>
parents:
13979
diff
changeset
|
137 print(err); |
|
a957cd3ea4eb
util.startup: Improve error message when failing to load config file
Matthew Wild <mwild1@gmail.com>
parents:
13979
diff
changeset
|
138 end |
|
a957cd3ea4eb
util.startup: Improve error message when failing to load config file
Matthew Wild <mwild1@gmail.com>
parents:
13979
diff
changeset
|
139 print(""); |
|
8635
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 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
|
141 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
|
142 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
|
143 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
|
144 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
|
145 os.exit(1); |
|
9877
ded5303e1fde
util.startup: Log configuration warnings at startup
Matthew Wild <mwild1@gmail.com>
parents:
9873
diff
changeset
|
146 elseif err and #err > 0 then |
|
ded5303e1fde
util.startup: Log configuration warnings at startup
Matthew Wild <mwild1@gmail.com>
parents:
9873
diff
changeset
|
147 config_warnings = err; |
|
8635
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 end |
|
9214
8b2b8f1a911f
util.startup: Set flag when config fully loaded
Matthew Wild <mwild1@gmail.com>
parents:
8958
diff
changeset
|
149 prosody.config_loaded = true; |
|
8635
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 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
|
151 |
|
47e3b8b6f17a
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 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
|
153 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
|
154 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
|
155 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
|
156 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
|
157 |
|
47e3b8b6f17a
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 -- 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
|
159 |
|
47e3b8b6f17a
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 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
|
161 -- 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
|
162 -- luacheck: ignore 111/server 111/socket |
|
12975
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
163 require "prosody.util.import" |
|
8635
47e3b8b6f17a
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 socket = require "socket"; |
|
12975
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
165 server = require "prosody.net.server" |
|
8635
47e3b8b6f17a
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 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
|
167 |
|
47e3b8b6f17a
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 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
|
169 -- Initialize logging |
|
12975
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
170 local loggingmanager = require "prosody.core.loggingmanager" |
|
8721
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:
8720
diff
changeset
|
171 loggingmanager.reload_logging(); |
|
9762
34988a408b74
util.startup: Always reload logging after config (fixes #1284)
Kim Alvefur <zash@zash.se>
parents:
9214
diff
changeset
|
172 prosody.events.add_handler("config-reloaded", function () |
|
34988a408b74
util.startup: Always reload logging after config (fixes #1284)
Kim Alvefur <zash@zash.se>
parents:
9214
diff
changeset
|
173 prosody.events.fire_event("reopen-log-files"); |
|
34988a408b74
util.startup: Always reload logging after config (fixes #1284)
Kim Alvefur <zash@zash.se>
parents:
9214
diff
changeset
|
174 end); |
|
8721
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:
8720
diff
changeset
|
175 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:
8720
diff
changeset
|
176 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:
8720
diff
changeset
|
177 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:
8720
diff
changeset
|
178 end); |
|
8635
47e3b8b6f17a
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 |
|
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
180 |
|
9873
dfaeea570f7e
util.startup: Give function a more generic name so it can apply to all warnings
Matthew Wild <mwild1@gmail.com>
parents:
9762
diff
changeset
|
181 function startup.log_startup_warnings() |
|
8635
47e3b8b6f17a
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 dependencies.log_warnings(); |
|
9878
dd61201fc5af
util.startup: Don't die if there are no config warnings to log (thanks buildbot)
Matthew Wild <mwild1@gmail.com>
parents:
9877
diff
changeset
|
183 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:
9877
diff
changeset
|
184 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:
9877
diff
changeset
|
185 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:
9877
diff
changeset
|
186 end |
|
9877
ded5303e1fde
util.startup: Log configuration warnings at startup
Matthew Wild <mwild1@gmail.com>
parents:
9873
diff
changeset
|
187 end |
|
8635
47e3b8b6f17a
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 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
|
189 |
|
47e3b8b6f17a
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 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
|
191 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
|
192 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
|
193 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
|
194 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
|
195 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
|
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 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
|
199 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
|
200 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
|
201 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
|
202 |
|
47e3b8b6f17a
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 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
|
204 -- 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
|
205 -- 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
|
206 -- 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
|
207 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
|
208 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
|
209 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
|
210 -- 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
|
211 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
|
212 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
|
213 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
|
214 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
|
215 end |
|
8638
f8f45bbbd8ba
util.startup: Ignore various globals being read and written as part of startup [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8637
diff
changeset
|
216 function require(...) -- luacheck: ignore 121 |
|
8635
47e3b8b6f17a
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 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
|
218 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
|
219 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
|
220 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
|
221 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
|
222 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
|
223 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
|
224 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
|
225 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
|
226 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
|
227 _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
|
228 _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
|
229 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
|
230 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
|
231 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
|
232 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
|
233 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
|
234 |
|
47e3b8b6f17a
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 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
|
236 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
|
237 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
|
238 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
|
239 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
|
240 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
|
241 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
|
242 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
|
243 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
|
244 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
|
245 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
|
246 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
|
247 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
|
248 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
|
249 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
|
250 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
|
251 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
|
252 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
|
253 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
|
254 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
|
255 function mt.__tostring(f) |
|
11150
0cfa36fa707e
util.startup: Include arguments in function string representation
Kim Alvefur <zash@zash.se>
parents:
11149
diff
changeset
|
256 local info = debug.getinfo(f, "Su"); |
|
11152
89162d27e1b1
util.startup: Handle missing nparams field from debug info (not present in 5.1)
Matthew Wild <mwild1@gmail.com>
parents:
11150
diff
changeset
|
257 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:
11150
diff
changeset
|
258 for i = 1, n_params do |
|
11150
0cfa36fa707e
util.startup: Include arguments in function string representation
Kim Alvefur <zash@zash.se>
parents:
11149
diff
changeset
|
259 info[i] = debug.getlocal(f, i); |
|
0cfa36fa707e
util.startup: Include arguments in function string representation
Kim Alvefur <zash@zash.se>
parents:
11149
diff
changeset
|
260 end |
|
0cfa36fa707e
util.startup: Include arguments in function string representation
Kim Alvefur <zash@zash.se>
parents:
11149
diff
changeset
|
261 if info.isvararg then |
|
11152
89162d27e1b1
util.startup: Handle missing nparams field from debug info (not present in 5.1)
Matthew Wild <mwild1@gmail.com>
parents:
11150
diff
changeset
|
262 info[n_params+1] = "..."; |
|
11150
0cfa36fa707e
util.startup: Include arguments in function string representation
Kim Alvefur <zash@zash.se>
parents:
11149
diff
changeset
|
263 end |
|
12982
fc0109c59807
util.startup: Tweak function string representation
Kim Alvefur <zash@zash.se>
parents:
12975
diff
changeset
|
264 return ("function @%s:%d(%s)"):format(info.short_src:match("[^\\/]*$"), info.linedefined, table.concat(info, ", ")); |
|
8635
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
265 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
|
266 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
|
267 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
|
268 |
|
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
269 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
|
270 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
|
271 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
|
272 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
|
273 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
|
274 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
|
275 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
|
276 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
|
277 |
|
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
278 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
|
279 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
|
280 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
|
281 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
|
282 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
|
283 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
|
284 |
|
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
285 function startup.init_global_state() |
|
8638
f8f45bbbd8ba
util.startup: Ignore various globals being read and written as part of startup [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8637
diff
changeset
|
286 -- luacheck: ignore 121 |
|
8635
47e3b8b6f17a
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 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
|
288 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
|
289 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
|
290 |
|
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
291 -- 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
|
292 -- 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
|
293 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
|
294 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
|
295 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
|
296 |
|
13627
2db7b3b65363
core.configmanager: Add function for getting secrets from separate files
Kim Alvefur <zash@zash.se>
parents:
13564
diff
changeset
|
297 prosody.paths = { |
|
2db7b3b65363
core.configmanager: Add function for getting secrets from separate files
Kim Alvefur <zash@zash.se>
parents:
13564
diff
changeset
|
298 source = CFG_SOURCEDIR; |
|
2db7b3b65363
core.configmanager: Add function for getting secrets from separate files
Kim Alvefur <zash@zash.se>
parents:
13564
diff
changeset
|
299 config = CFG_CONFIGDIR or "."; |
|
2db7b3b65363
core.configmanager: Add function for getting secrets from separate files
Kim Alvefur <zash@zash.se>
parents:
13564
diff
changeset
|
300 plugins = CFG_PLUGINDIR or "plugins"; |
|
2db7b3b65363
core.configmanager: Add function for getting secrets from separate files
Kim Alvefur <zash@zash.se>
parents:
13564
diff
changeset
|
301 data = "data"; |
|
2db7b3b65363
core.configmanager: Add function for getting secrets from separate files
Kim Alvefur <zash@zash.se>
parents:
13564
diff
changeset
|
302 }; |
|
8698
0499f3da0ec4
util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents:
8692
diff
changeset
|
303 |
|
0499f3da0ec4
util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents:
8692
diff
changeset
|
304 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:
8692
diff
changeset
|
305 |
|
8720
dba17a70fd22
util.startup: Initialize prosody.log / _G.log here instead of in loggingmanager to reduce dependencies
Kim Alvefur <zash@zash.se>
parents:
8719
diff
changeset
|
306 _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:
8719
diff
changeset
|
307 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:
8719
diff
changeset
|
308 |
|
8698
0499f3da0ec4
util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents:
8692
diff
changeset
|
309 startup.detect_platform(); |
|
0499f3da0ec4
util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents:
8692
diff
changeset
|
310 startup.detect_installed(); |
|
0499f3da0ec4
util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents:
8692
diff
changeset
|
311 _G.prosody = prosody; |
|
12781
22066b02887f
util.startup: Provide a common Lua 5.3+ math.type() for Lua 5.2
Kim Alvefur <zash@zash.se>
parents:
12779
diff
changeset
|
312 |
|
22066b02887f
util.startup: Provide a common Lua 5.3+ math.type() for Lua 5.2
Kim Alvefur <zash@zash.se>
parents:
12779
diff
changeset
|
313 -- COMPAT Lua < 5.3 |
|
22066b02887f
util.startup: Provide a common Lua 5.3+ math.type() for Lua 5.2
Kim Alvefur <zash@zash.se>
parents:
12779
diff
changeset
|
314 if not math.type then |
|
12975
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
315 require "prosody.util.mathcompat" |
|
12781
22066b02887f
util.startup: Provide a common Lua 5.3+ math.type() for Lua 5.2
Kim Alvefur <zash@zash.se>
parents:
12779
diff
changeset
|
316 end |
|
8698
0499f3da0ec4
util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents:
8692
diff
changeset
|
317 end |
|
0499f3da0ec4
util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents:
8692
diff
changeset
|
318 |
|
0499f3da0ec4
util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents:
8692
diff
changeset
|
319 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:
8692
diff
changeset
|
320 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:
8692
diff
changeset
|
321 end |
|
0499f3da0ec4
util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents:
8692
diff
changeset
|
322 |
|
0499f3da0ec4
util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents:
8692
diff
changeset
|
323 function startup.setup_plugindir() |
|
8635
47e3b8b6f17a
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 local custom_plugin_paths = config.get("*", "plugin_paths"); |
|
10173
0513dd2830b7
util.startup: The .setup_plugindir function now correctly sets a default/specified path for custom plugins
João Duarte <jvsDuarte08@gmail.com>
parents:
10171
diff
changeset
|
325 local path_sep = package.config:sub(3,3); |
|
8635
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
326 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
|
327 -- path1;path2;path3;defaultpath... |
|
8638
f8f45bbbd8ba
util.startup: Ignore various globals being read and written as part of startup [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8637
diff
changeset
|
328 -- luacheck: ignore 111 |
|
8635
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
329 CFG_PLUGINDIR = table.concat(custom_plugin_paths, path_sep)..path_sep..(CFG_PLUGINDIR or "plugins"); |
|
8733
6a234e77c99f
util.startup: Fix traceback due to both plugin path becoming nil if plugin_paths is unset
Kim Alvefur <zash@zash.se>
parents:
8728
diff
changeset
|
330 prosody.paths.plugins = CFG_PLUGINDIR; |
|
8635
47e3b8b6f17a
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 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
|
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 |
|
10404
29c10930a7b2
util.startup: Split plugin installer path setup into a separate function
Kim Alvefur <zash@zash.se>
parents:
10391
diff
changeset
|
334 function startup.setup_plugin_install_path() |
|
11305
cd8516a77255
util.startup: Make installer_plugin_path relative to data directory
Kim Alvefur <zash@zash.se>
parents:
11304
diff
changeset
|
335 local installer_plugin_path = config.get("*", "installer_plugin_path") or "custom_plugins"; |
|
10404
29c10930a7b2
util.startup: Split plugin installer path setup into a separate function
Kim Alvefur <zash@zash.se>
parents:
10391
diff
changeset
|
336 local path_sep = package.config:sub(3,3); |
|
11305
cd8516a77255
util.startup: Make installer_plugin_path relative to data directory
Kim Alvefur <zash@zash.se>
parents:
11304
diff
changeset
|
337 installer_plugin_path = config.resolve_relative_path(CFG_DATADIR or "data", installer_plugin_path); |
|
12975
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
338 require"prosody.util.paths".complement_lua_path(installer_plugin_path); |
|
10404
29c10930a7b2
util.startup: Split plugin installer path setup into a separate function
Kim Alvefur <zash@zash.se>
parents:
10391
diff
changeset
|
339 -- luacheck: ignore 111 |
|
10173
0513dd2830b7
util.startup: The .setup_plugindir function now correctly sets a default/specified path for custom plugins
João Duarte <jvsDuarte08@gmail.com>
parents:
10171
diff
changeset
|
340 CFG_PLUGINDIR = installer_plugin_path..path_sep..(CFG_PLUGINDIR or "plugins"); |
|
11144
2b9f7c537acb
util.startup: Save the path used by the installer to prosody.paths
Kim Alvefur <zash@zash.se>
parents:
11139
diff
changeset
|
341 prosody.paths.installer = installer_plugin_path; |
|
10171
628e238feb04
util.startup: Removed unnecessary if clause at startup.set_plugindir
João Duarte <jvsDuarte08@gmail.com>
parents:
10163
diff
changeset
|
342 prosody.paths.plugins = CFG_PLUGINDIR; |
|
8635
47e3b8b6f17a
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 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
|
344 |
|
8664
d49acc9a8da2
util.startup: Fix chdir() to use correct path variable
Matthew Wild <mwild1@gmail.com>
parents:
8653
diff
changeset
|
345 function startup.chdir() |
|
d49acc9a8da2
util.startup: Fix chdir() to use correct path variable
Matthew Wild <mwild1@gmail.com>
parents:
8653
diff
changeset
|
346 if prosody.installed then |
|
10390
82705ec87253
util.startup: Ensure prosody.paths are absolute (see #1430)
Kim Alvefur <zash@zash.se>
parents:
10389
diff
changeset
|
347 local lfs = require "lfs"; |
|
82705ec87253
util.startup: Ensure prosody.paths are absolute (see #1430)
Kim Alvefur <zash@zash.se>
parents:
10389
diff
changeset
|
348 -- 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:
10389
diff
changeset
|
349 local cwd = lfs.currentdir(); |
|
82705ec87253
util.startup: Ensure prosody.paths are absolute (see #1430)
Kim Alvefur <zash@zash.se>
parents:
10389
diff
changeset
|
350 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:
10389
diff
changeset
|
351 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:
10389
diff
changeset
|
352 prosody.paths.data = config.resolve_relative_path(cwd, prosody.paths.data); |
|
8664
d49acc9a8da2
util.startup: Fix chdir() to use correct path variable
Matthew Wild <mwild1@gmail.com>
parents:
8653
diff
changeset
|
353 -- Change working directory to data path. |
|
10390
82705ec87253
util.startup: Ensure prosody.paths are absolute (see #1430)
Kim Alvefur <zash@zash.se>
parents:
10389
diff
changeset
|
354 lfs.chdir(prosody.paths.data); |
|
8664
d49acc9a8da2
util.startup: Fix chdir() to use correct path variable
Matthew Wild <mwild1@gmail.com>
parents:
8653
diff
changeset
|
355 end |
|
d49acc9a8da2
util.startup: Fix chdir() to use correct path variable
Matthew Wild <mwild1@gmail.com>
parents:
8653
diff
changeset
|
356 end |
|
d49acc9a8da2
util.startup: Fix chdir() to use correct path variable
Matthew Wild <mwild1@gmail.com>
parents:
8653
diff
changeset
|
357 |
|
8635
47e3b8b6f17a
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 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
|
359 -- 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
|
360 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
|
361 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
|
362 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
|
363 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
|
364 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
|
365 if level == "parser" then |
|
10108
659ffa03f1e7
util.startup: Remove tostring call from logging
Kim Alvefur <zash@zash.se>
parents:
9878
diff
changeset
|
366 log("error", "There was an error parsing the configuration file: %s", err); |
|
8635
47e3b8b6f17a
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 elseif level == "file" then |
|
10108
659ffa03f1e7
util.startup: Remove tostring call from logging
Kim Alvefur <zash@zash.se>
parents:
9878
diff
changeset
|
368 log("error", "Couldn't read the config file when trying to reload: %s", err); |
|
8635
47e3b8b6f17a
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 |
|
13979
2041c347c178
util.startup: Inform process manager about failure to reload config
Kim Alvefur <zash@zash.se>
parents:
13743
diff
changeset
|
370 prosody.events.fire_event("config-reload-failed", { |
|
2041c347c178
util.startup: Inform process manager about failure to reload config
Kim Alvefur <zash@zash.se>
parents:
13743
diff
changeset
|
371 filename = prosody.config_file; |
|
2041c347c178
util.startup: Inform process manager about failure to reload config
Kim Alvefur <zash@zash.se>
parents:
13743
diff
changeset
|
372 level = level; error = err; |
|
2041c347c178
util.startup: Inform process manager about failure to reload config
Kim Alvefur <zash@zash.se>
parents:
13743
diff
changeset
|
373 }); |
|
8692
a55574754e5f
configmanager: Move firing of the 'config-reloaded' event into util.startup (fixes #1117)
Kim Alvefur <zash@zash.se>
parents:
8688
diff
changeset
|
374 else |
|
a55574754e5f
configmanager: Move firing of the 'config-reloaded' event into util.startup (fixes #1117)
Kim Alvefur <zash@zash.se>
parents:
8688
diff
changeset
|
375 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:
8688
diff
changeset
|
376 filename = prosody.config_file, |
|
a55574754e5f
configmanager: Move firing of the 'config-reloaded' event into util.startup (fixes #1117)
Kim Alvefur <zash@zash.se>
parents:
8688
diff
changeset
|
377 config = config.getconfig(), |
|
a55574754e5f
configmanager: Move firing of the 'config-reloaded' event into util.startup (fixes #1117)
Kim Alvefur <zash@zash.se>
parents:
8688
diff
changeset
|
378 }); |
|
8635
47e3b8b6f17a
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 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
|
380 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
|
381 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
|
382 |
|
47e3b8b6f17a
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 -- 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
|
384 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
|
385 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
|
386 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
|
387 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
|
388 |
|
47e3b8b6f17a
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 -- 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
|
390 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
|
391 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
|
392 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
|
393 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
|
394 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
|
395 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
|
396 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
|
397 }); |
|
12553
cc0ec0277813
util.startup: Fix async waiting for last shutdown steps
Kim Alvefur <zash@zash.se>
parents:
12423
diff
changeset
|
398 prosody.main_thread:run(startup.shutdown); |
|
8635
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
399 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
|
400 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
|
401 |
|
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
402 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
|
403 --- Load and initialise core modules |
|
12975
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
404 require "prosody.util.xmppstream" |
|
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
405 require "prosody.core.stanza_router" |
|
13120
be4058bb5a7e
util.startup: Record current version in a metric
Kim Alvefur <zash@zash.se>
parents:
13119
diff
changeset
|
406 require "prosody.core.statsmanager".metric("gauge", "prosody_info", "", "Prosody version", { "version" }):with_labels(prosody.version):set(1); |
|
12975
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
407 require "prosody.core.hostmanager" |
|
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
408 require "prosody.core.portmanager" |
|
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
409 require "prosody.core.modulemanager" |
|
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
410 require "prosody.core.usermanager" |
|
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
411 require "prosody.core.rostermanager" |
|
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
412 require "prosody.core.sessionmanager" |
|
8635
47e3b8b6f17a
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 |
|
12975
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
414 require "prosody.util.array" |
|
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
415 require "prosody.util.datetime" |
|
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
416 require "prosody.util.iterators" |
|
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
417 require "prosody.util.timer" |
|
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
418 require "prosody.util.helpers" |
|
8635
47e3b8b6f17a
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 |
|
12975
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
420 pcall(require, "prosody.util.signal") -- Not on Windows |
|
8635
47e3b8b6f17a
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 |
|
47e3b8b6f17a
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 -- 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
|
423 -- 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
|
424 --[[ |
|
47e3b8b6f17a
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 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
|
426 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
|
427 ]] |
|
47e3b8b6f17a
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 |
|
12975
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
429 require "prosody.util.stanza" |
|
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
430 require "prosody.util.jid" |
|
13424
0efb53a0852e
util.startup: Expose core.features.available as prosody.features
Matthew Wild <mwild1@gmail.com>
parents:
13342
diff
changeset
|
431 |
|
0efb53a0852e
util.startup: Expose core.features.available as prosody.features
Matthew Wild <mwild1@gmail.com>
parents:
13342
diff
changeset
|
432 prosody.features = require "prosody.core.features".available; |
|
8635
47e3b8b6f17a
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 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
|
434 |
|
47e3b8b6f17a
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 function startup.init_http_client() |
|
12975
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
436 local http = require "prosody.net.http" |
|
8635
47e3b8b6f17a
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 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
|
438 local https_client = config.get("*", "client_https_ssl") |
|
12975
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
439 http.default.options.sslctx = require "prosody.core.certmanager".create_context("client_https port 0", "client", |
|
8635
47e3b8b6f17a
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 { capath = config_ssl.capath, cafile = config_ssl.cafile, verify = "peer", }, https_client); |
|
12274
10447f940fec
util.startup: Enable DANE in http client library with use_dane
Kim Alvefur <zash@zash.se>
parents:
12244
diff
changeset
|
441 http.default.options.use_dane = config.get("*", "use_dane") |
|
8635
47e3b8b6f17a
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 |
|
11948
dca75cc6fc5a
util.startup: Integrate util.promise with net.server main loop
Kim Alvefur <zash@zash.se>
parents:
11870
diff
changeset
|
444 function startup.init_promise() |
|
12975
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
445 local promise = require "prosody.util.promise"; |
|
11948
dca75cc6fc5a
util.startup: Integrate util.promise with net.server main loop
Kim Alvefur <zash@zash.se>
parents:
11870
diff
changeset
|
446 |
|
12975
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
447 local timer = require "prosody.util.timer"; |
|
11948
dca75cc6fc5a
util.startup: Integrate util.promise with net.server main loop
Kim Alvefur <zash@zash.se>
parents:
11870
diff
changeset
|
448 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:
11870
diff
changeset
|
449 end |
|
dca75cc6fc5a
util.startup: Integrate util.promise with net.server main loop
Kim Alvefur <zash@zash.se>
parents:
11870
diff
changeset
|
450 |
|
11963
f5c6be4a3ecc
util.startup: Initialize util.async at startup
Matthew Wild <mwild1@gmail.com>
parents:
11948
diff
changeset
|
451 function startup.init_async() |
|
12975
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
452 local async = require "prosody.util.async"; |
|
11963
f5c6be4a3ecc
util.startup: Initialize util.async at startup
Matthew Wild <mwild1@gmail.com>
parents:
11948
diff
changeset
|
453 |
|
12975
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
454 local timer = require "prosody.util.timer"; |
|
11963
f5c6be4a3ecc
util.startup: Initialize util.async at startup
Matthew Wild <mwild1@gmail.com>
parents:
11948
diff
changeset
|
455 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:
11948
diff
changeset
|
456 async.set_schedule_function(timer.add_task); |
|
f5c6be4a3ecc
util.startup: Initialize util.async at startup
Matthew Wild <mwild1@gmail.com>
parents:
11948
diff
changeset
|
457 end |
|
f5c6be4a3ecc
util.startup: Initialize util.async at startup
Matthew Wild <mwild1@gmail.com>
parents:
11948
diff
changeset
|
458 |
|
13659
5abdcad8c2e0
net.adns: Collect DNS lookup timing metrics
Kim Alvefur <zash@zash.se>
parents:
13639
diff
changeset
|
459 function startup.instrument() |
|
5abdcad8c2e0
net.adns: Collect DNS lookup timing metrics
Kim Alvefur <zash@zash.se>
parents:
13639
diff
changeset
|
460 local statsmanager = require "prosody.core.statsmanager"; |
|
5abdcad8c2e0
net.adns: Collect DNS lookup timing metrics
Kim Alvefur <zash@zash.se>
parents:
13639
diff
changeset
|
461 local timed = require"prosody.util.openmetrics".timed; |
|
5abdcad8c2e0
net.adns: Collect DNS lookup timing metrics
Kim Alvefur <zash@zash.se>
parents:
13639
diff
changeset
|
462 |
|
5abdcad8c2e0
net.adns: Collect DNS lookup timing metrics
Kim Alvefur <zash@zash.se>
parents:
13639
diff
changeset
|
463 local adns = require "prosody.net.adns"; |
|
5abdcad8c2e0
net.adns: Collect DNS lookup timing metrics
Kim Alvefur <zash@zash.se>
parents:
13639
diff
changeset
|
464 if adns.instrument then |
|
5abdcad8c2e0
net.adns: Collect DNS lookup timing metrics
Kim Alvefur <zash@zash.se>
parents:
13639
diff
changeset
|
465 local m = statsmanager.metric("histogram", "prosody_dns", "seconds", "DNS lookups", { "qclass"; "qtype" }, { |
|
13989
a8aa17293d22
util.startup: Add another bucket for DNS lookup timings
Kim Alvefur <zash@zash.se>
parents:
13988
diff
changeset
|
466 buckets = { 1 / 1024; 1 / 256; 1 / 64; 1 / 16; 1 / 4; 1; 4; 16 }; |
|
13659
5abdcad8c2e0
net.adns: Collect DNS lookup timing metrics
Kim Alvefur <zash@zash.se>
parents:
13639
diff
changeset
|
467 }); |
|
13981
350e16e5f9aa
net.unbound: Count number of DNS response records
Kim Alvefur <zash@zash.se>
parents:
13980
diff
changeset
|
468 local c = statsmanager.metric("histogram", "prosody_dns_response", "records", "DNS response records", { "qclass"; "qtype" }, { |
|
13988
260a6feebe5f
util.startup: Add metric bucket for zero DNS records
Kim Alvefur <zash@zash.se>
parents:
13982
diff
changeset
|
469 buckets = { 0; 1; 2; 4; 8; 16 } }) |
|
13981
350e16e5f9aa
net.unbound: Count number of DNS response records
Kim Alvefur <zash@zash.se>
parents:
13980
diff
changeset
|
470 local function m_times(qclass, qtype) return timed(m:with_labels(qclass, qtype)); end |
|
13982
ccbc4a46f42d
util.startup: Fix error in DNS instrumentation
Kim Alvefur <zash@zash.se>
parents:
13981
diff
changeset
|
471 local function m_counts(qclass, qtype, num) return c:with_labels(qclass, qtype):sample(num); end |
|
13981
350e16e5f9aa
net.unbound: Count number of DNS response records
Kim Alvefur <zash@zash.se>
parents:
13980
diff
changeset
|
472 adns.instrument(m_times, m_counts); |
|
13659
5abdcad8c2e0
net.adns: Collect DNS lookup timing metrics
Kim Alvefur <zash@zash.se>
parents:
13639
diff
changeset
|
473 end |
|
5abdcad8c2e0
net.adns: Collect DNS lookup timing metrics
Kim Alvefur <zash@zash.se>
parents:
13639
diff
changeset
|
474 end |
|
5abdcad8c2e0
net.adns: Collect DNS lookup timing metrics
Kim Alvefur <zash@zash.se>
parents:
13639
diff
changeset
|
475 |
|
8635
47e3b8b6f17a
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 function startup.init_data_store() |
|
12975
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
477 require "prosody.core.storagemanager"; |
|
8635
47e3b8b6f17a
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 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
|
479 |
|
13342
d0a9b631a937
util.startup: Use prosody. module namespace
Kim Alvefur <zash@zash.se>
parents:
13311
diff
changeset
|
480 local running_state = require "prosody.util.fsm".new({ |
|
13311
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
481 default_state = "uninitialized"; |
|
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
482 transitions = { |
|
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
483 { name = "begin_startup", from = "uninitialized", to = "starting" }; |
|
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
484 { name = "finish_startup", from = "starting", to = "running" }; |
|
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
485 { name = "begin_shutdown", from = { "running", "starting" }, to = "stopping" }; |
|
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
486 { name = "finish_shutdown", from = "stopping", to = "stopped" }; |
|
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
487 }; |
|
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
488 handlers = { |
|
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
489 transitioned = function (transition) |
|
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
490 prosody.state = transition.to; |
|
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
491 end; |
|
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
492 }; |
|
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
493 state_handlers = { |
|
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
494 starting = function () |
|
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
495 prosody.log("debug", "Firing server-starting event"); |
|
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
496 prosody.events.fire_event("server-starting"); |
|
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
497 prosody.start_time = os.time(); |
|
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
498 end; |
|
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
499 running = function () |
|
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
500 prosody.log("debug", "Startup complete, firing server-started"); |
|
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
501 prosody.events.fire_event("server-started"); |
|
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
502 end; |
|
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
503 }; |
|
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
504 }):init(); |
|
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
505 |
|
8635
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
506 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
|
507 log("info", "Prosody is using the %s backend for connection handling", server.get_backend()); |
|
13311
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
508 |
|
8635
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
509 -- Signal to modules that we are ready to start |
|
13165
9c13c11b199d
renamening: Fix newly added imports to use the new namespace
Kim Alvefur <zash@zash.se>
parents:
13120
diff
changeset
|
510 prosody.started = require "prosody.util.promise".new(function (resolve) |
|
13311
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
511 if prosody.state == "running" then |
|
13014
06453c564141
util.startup: Add prosody.started promise to easily execute code after startup
Matthew Wild <mwild1@gmail.com>
parents:
12982
diff
changeset
|
512 resolve(); |
|
13311
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
513 else |
|
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
514 prosody.events.add_handler("server-started", function () |
|
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
515 resolve(); |
|
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
516 end); |
|
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
517 end |
|
13014
06453c564141
util.startup: Add prosody.started promise to easily execute code after startup
Matthew Wild <mwild1@gmail.com>
parents:
12982
diff
changeset
|
518 end):catch(function (err) |
|
06453c564141
util.startup: Add prosody.started promise to easily execute code after startup
Matthew Wild <mwild1@gmail.com>
parents:
12982
diff
changeset
|
519 prosody.log("error", "Prosody startup error: %s", err); |
|
06453c564141
util.startup: Add prosody.started promise to easily execute code after startup
Matthew Wild <mwild1@gmail.com>
parents:
12982
diff
changeset
|
520 end); |
|
13311
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
521 |
|
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
522 running_state:begin_startup(); |
|
8635
47e3b8b6f17a
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 |
|
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
525 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
|
526 -- 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
|
527 -- 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
|
528 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
|
529 __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
|
530 __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
|
531 }; |
|
47e3b8b6f17a
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 |
|
47e3b8b6f17a
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 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
|
534 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
|
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 |
|
47e3b8b6f17a
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 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
|
538 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
|
539 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
|
540 |
|
47e3b8b6f17a
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 -- 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
|
542 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
|
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 |
|
47e3b8b6f17a
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 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
|
546 -- 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
|
547 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
|
548 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
|
549 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
|
550 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
|
551 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
|
552 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
|
553 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
|
554 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
|
555 else |
|
12975
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
556 local hg = require"prosody.util.mercurial"; |
|
8635
47e3b8b6f17a
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 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
|
558 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
|
559 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
|
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 |
|
47e3b8b6f17a
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 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
|
563 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
|
564 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
|
565 |
|
47e3b8b6f17a
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 function startup.notify_started() |
|
13311
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
567 running_state:finish_startup(); |
|
8635
47e3b8b6f17a
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 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
|
569 |
|
47e3b8b6f17a
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 -- 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
|
571 function startup.force_console_logging() |
|
8666
57780ba1938f
util.startup: Move original_logging_config to a local variable
Matthew Wild <mwild1@gmail.com>
parents:
8665
diff
changeset
|
572 original_logging_config = config.get("*", "log"); |
|
11828
024ac556e907
prosodyctl: Add support for -v/--verbose to enable debug logging
Kim Alvefur <zash@zash.se>
parents:
11560
diff
changeset
|
573 local log_level = os.getenv("PROSODYCTL_LOG_LEVEL"); |
|
12243
73ecfe811526
util.startup: Teach prosodyctl to be --quiet as complement to --verbose
Kim Alvefur <zash@zash.se>
parents:
12160
diff
changeset
|
574 if not log_level then |
|
73ecfe811526
util.startup: Teach prosodyctl to be --quiet as complement to --verbose
Kim Alvefur <zash@zash.se>
parents:
12160
diff
changeset
|
575 if prosody.opts.verbose then |
|
73ecfe811526
util.startup: Teach prosodyctl to be --quiet as complement to --verbose
Kim Alvefur <zash@zash.se>
parents:
12160
diff
changeset
|
576 log_level = "debug"; |
|
73ecfe811526
util.startup: Teach prosodyctl to be --quiet as complement to --verbose
Kim Alvefur <zash@zash.se>
parents:
12160
diff
changeset
|
577 elseif prosody.opts.quiet then |
|
73ecfe811526
util.startup: Teach prosodyctl to be --quiet as complement to --verbose
Kim Alvefur <zash@zash.se>
parents:
12160
diff
changeset
|
578 log_level = "error"; |
|
12244
858d40d675ee
util.startup: Teach prosodyctl to be completely --silent
Kim Alvefur <zash@zash.se>
parents:
12243
diff
changeset
|
579 elseif prosody.opts.silent then |
|
858d40d675ee
util.startup: Teach prosodyctl to be completely --silent
Kim Alvefur <zash@zash.se>
parents:
12243
diff
changeset
|
580 config.set("*", "log", {}); -- ssssshush! |
|
858d40d675ee
util.startup: Teach prosodyctl to be completely --silent
Kim Alvefur <zash@zash.se>
parents:
12243
diff
changeset
|
581 return |
|
12243
73ecfe811526
util.startup: Teach prosodyctl to be --quiet as complement to --verbose
Kim Alvefur <zash@zash.se>
parents:
12160
diff
changeset
|
582 end |
|
73ecfe811526
util.startup: Teach prosodyctl to be --quiet as complement to --verbose
Kim Alvefur <zash@zash.se>
parents:
12160
diff
changeset
|
583 end |
|
11828
024ac556e907
prosodyctl: Add support for -v/--verbose to enable debug logging
Kim Alvefur <zash@zash.se>
parents:
11560
diff
changeset
|
584 config.set("*", "log", { { levels = { min = log_level or "info" }, to = "console" } }); |
|
8635
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
585 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
|
586 |
|
13461
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
587 local function check_posix() |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
588 if prosody.platform ~= "posix" then return end |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
589 |
|
13564
a350f6332bd8
util.startup: Bump expected util.pposix version
Kim Alvefur <zash@zash.se>
parents:
13477
diff
changeset
|
590 local want_pposix_version = "0.4.1"; |
|
13461
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
591 local have_pposix, pposix = pcall(require, "prosody.util.pposix"); |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
592 |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
593 if pposix._VERSION ~= want_pposix_version then |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
594 print(string.format("Unknown version (%s) of binary pposix module, expected %s", |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
595 tostring(pposix._VERSION), want_pposix_version)); |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
596 os.exit(1); |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
597 end |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
598 if have_pposix and pposix then |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
599 return pposix; |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
600 end |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
601 end |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
602 |
|
8635
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
603 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
|
604 -- 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
|
605 -- 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
|
606 -- 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
|
607 -- 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
|
608 |
|
13461
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
609 local pposix = check_posix() |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
610 if pposix then |
|
8672
86b12ae8d427
util.startup: Expose user switching information via prosody global object
Matthew Wild <mwild1@gmail.com>
parents:
8667
diff
changeset
|
611 prosody.current_uid = pposix.getuid(); |
|
10597
25a3c8134b0a
prosody/util.startup: Switch to parse_args() for --root and --config
Matthew Wild <mwild1@gmail.com>
parents:
10596
diff
changeset
|
612 local arg_root = prosody.opts.root; |
|
8672
86b12ae8d427
util.startup: Expose user switching information via prosody global object
Matthew Wild <mwild1@gmail.com>
parents:
8667
diff
changeset
|
613 if prosody.current_uid == 0 and config.get("*", "run_as_root") ~= true and not arg_root then |
|
8635
47e3b8b6f17a
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 -- 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
|
615 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
|
616 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
|
617 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
|
618 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
|
619 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
|
620 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
|
621 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
|
622 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
|
623 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
|
624 -- Yay! |
|
8672
86b12ae8d427
util.startup: Expose user switching information via prosody global object
Matthew Wild <mwild1@gmail.com>
parents:
8667
diff
changeset
|
625 prosody.switched_user = true; |
|
8635
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
626 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
|
627 end |
|
8672
86b12ae8d427
util.startup: Expose user switching information via prosody global object
Matthew Wild <mwild1@gmail.com>
parents:
8667
diff
changeset
|
628 if not prosody.switched_user then |
|
8635
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
629 -- 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
|
630 print("Warning: Couldn't switch to Prosody user/group '"..tostring(desired_user).."'/'"..tostring(desired_group).."': "..tostring(err)); |
|
11866
515a89dee6ae
util.startup: Skip config readability check in migrator (thanks eTaurus)
Kim Alvefur <zash@zash.se>
parents:
11847
diff
changeset
|
631 elseif prosody.config_file then |
|
8635
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
632 -- Make sure the Prosody user can read the config |
|
10532
19ec384eb782
util.startup: Ignore unused errno variable [luacheck]
Kim Alvefur <zash@zash.se>
parents:
10405
diff
changeset
|
633 local conf, err, errno = io.open(prosody.config_file); --luacheck: ignore 211/errno |
|
8635
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
634 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
|
635 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
|
636 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
|
637 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
|
638 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
|
639 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
|
640 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
|
641 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
|
642 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
|
643 end |
|
8637
c8368c7c81a1
util.startup: Trim trailing whitespace [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8636
diff
changeset
|
644 |
|
8667
a05d36075c6a
util.startup: Fix variable usage [luacheck]
Matthew Wild <mwild1@gmail.com>
parents:
8666
diff
changeset
|
645 pposix.setenv("HOME", prosody.paths.data); |
|
a05d36075c6a
util.startup: Fix variable usage [luacheck]
Matthew Wild <mwild1@gmail.com>
parents:
8666
diff
changeset
|
646 pposix.setenv("PROSODY_CONFIG", prosody.config_file); |
|
8635
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
647 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
|
648 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
|
649 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
|
650 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
|
651 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
|
652 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
|
653 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
|
654 |
|
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
655 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
|
656 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
|
657 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
|
658 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
|
659 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
|
660 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
|
661 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
|
662 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
|
663 end |
|
8637
c8368c7c81a1
util.startup: Trim trailing whitespace [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8636
diff
changeset
|
664 |
|
8635
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
665 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
|
666 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
|
667 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
|
668 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
|
669 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
|
670 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
|
671 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
|
672 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
|
673 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
|
674 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
|
675 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
|
676 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
|
677 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
|
678 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
|
679 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
|
680 end |
|
8637
c8368c7c81a1
util.startup: Trim trailing whitespace [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8636
diff
changeset
|
681 |
|
8635
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
682 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
|
683 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
|
684 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
|
685 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
|
686 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
|
687 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
|
688 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
|
689 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
|
690 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
|
691 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
|
692 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
|
693 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
|
694 |
|
11073
5691b9773c5b
util.startup: Configure the GC on startup, using the config or built-in defaults
Matthew Wild <mwild1@gmail.com>
parents:
10947
diff
changeset
|
695 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:
10947
diff
changeset
|
696 -- Apply garbage collector settings from the config file |
|
12975
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
697 local gc = require "prosody.util.gc"; |
|
11073
5691b9773c5b
util.startup: Configure the GC on startup, using the config or built-in defaults
Matthew Wild <mwild1@gmail.com>
parents:
10947
diff
changeset
|
698 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:
10947
diff
changeset
|
699 |
|
5691b9773c5b
util.startup: Configure the GC on startup, using the config or built-in defaults
Matthew Wild <mwild1@gmail.com>
parents:
10947
diff
changeset
|
700 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:
10947
diff
changeset
|
701 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:
10947
diff
changeset
|
702 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:
10947
diff
changeset
|
703 return nil, err; |
|
5691b9773c5b
util.startup: Configure the GC on startup, using the config or built-in defaults
Matthew Wild <mwild1@gmail.com>
parents:
10947
diff
changeset
|
704 end |
|
5691b9773c5b
util.startup: Configure the GC on startup, using the config or built-in defaults
Matthew Wild <mwild1@gmail.com>
parents:
10947
diff
changeset
|
705 return true; |
|
5691b9773c5b
util.startup: Configure the GC on startup, using the config or built-in defaults
Matthew Wild <mwild1@gmail.com>
parents:
10947
diff
changeset
|
706 end |
|
5691b9773c5b
util.startup: Configure the GC on startup, using the config or built-in defaults
Matthew Wild <mwild1@gmail.com>
parents:
10947
diff
changeset
|
707 |
|
11050
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10948
diff
changeset
|
708 function startup.init_errors() |
|
12975
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
709 require "prosody.util.error".configure(config.get("*", "error_library") or {}); |
|
11050
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10948
diff
changeset
|
710 end |
|
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10948
diff
changeset
|
711 |
|
8673
6aeed79d9283
util.startup: Expose make_host() function
Matthew Wild <mwild1@gmail.com>
parents:
8672
diff
changeset
|
712 function startup.make_host(hostname) |
|
6aeed79d9283
util.startup: Expose make_host() function
Matthew Wild <mwild1@gmail.com>
parents:
8672
diff
changeset
|
713 return { |
|
6aeed79d9283
util.startup: Expose make_host() function
Matthew Wild <mwild1@gmail.com>
parents:
8672
diff
changeset
|
714 type = "local", |
|
6aeed79d9283
util.startup: Expose make_host() function
Matthew Wild <mwild1@gmail.com>
parents:
8672
diff
changeset
|
715 events = prosody.events, |
|
6aeed79d9283
util.startup: Expose make_host() function
Matthew Wild <mwild1@gmail.com>
parents:
8672
diff
changeset
|
716 modules = {}, |
|
6aeed79d9283
util.startup: Expose make_host() function
Matthew Wild <mwild1@gmail.com>
parents:
8672
diff
changeset
|
717 sessions = {}, |
|
12975
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12864
diff
changeset
|
718 users = require "prosody.core.usermanager".new_null_provider(hostname) |
|
8673
6aeed79d9283
util.startup: Expose make_host() function
Matthew Wild <mwild1@gmail.com>
parents:
8672
diff
changeset
|
719 }; |
|
6aeed79d9283
util.startup: Expose make_host() function
Matthew Wild <mwild1@gmail.com>
parents:
8672
diff
changeset
|
720 end |
|
6aeed79d9283
util.startup: Expose make_host() function
Matthew Wild <mwild1@gmail.com>
parents:
8672
diff
changeset
|
721 |
|
8635
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
722 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
|
723 -- 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
|
724 -- 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
|
725 -- with just enough things for most code to work correctly |
|
8638
f8f45bbbd8ba
util.startup: Ignore various globals being read and written as part of startup [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8637
diff
changeset
|
726 -- luacheck: ignore 122/hosts |
|
8635
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
727 prosody.core_post_stanza = function () end; -- TODO: mod_router! |
|
8637
c8368c7c81a1
util.startup: Trim trailing whitespace [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8636
diff
changeset
|
728 |
|
8639
070a77c15f63
util.startup: Remove unused loop variable [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8638
diff
changeset
|
729 for hostname in pairs(config.getconfig()) do |
|
8715
25d8d6091ec3
util.startup: Access the hosts table via the prosody global for consistency
Kim Alvefur <zash@zash.se>
parents:
8713
diff
changeset
|
730 prosody.hosts[hostname] = startup.make_host(hostname); |
|
8635
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
731 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
|
732 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
|
733 |
|
14205
a874c3f4570a
util.startup: Always apply umask (thanks Max Hearnden)
Matthew Wild <mwild1@gmail.com>
parents:
14130
diff
changeset
|
734 function startup.set_umask() |
|
13461
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
735 if prosody.platform ~= "posix" then return end |
|
14205
a874c3f4570a
util.startup: Always apply umask (thanks Max Hearnden)
Matthew Wild <mwild1@gmail.com>
parents:
14130
diff
changeset
|
736 local pposix = check_posix(); |
|
13461
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
737 local umask = config.get("*", "umask") or "027"; |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
738 pposix.umask(umask); |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
739 end |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
740 |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
741 function startup.check_user() |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
742 local pposix = check_posix(); |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
743 if not pposix then return end |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
744 -- Don't even think about it! |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
745 if pposix.getuid() == 0 and not config.get("*", "run_as_root") then |
|
13464
2dbc169aae6a
util.startup: Abort before initialization of logging when started as root
Kim Alvefur <zash@zash.se>
parents:
13463
diff
changeset
|
746 print("Danger, Will Robinson! Prosody doesn't need to be run as root, so don't do it!"); |
|
2dbc169aae6a
util.startup: Abort before initialization of logging when started as root
Kim Alvefur <zash@zash.se>
parents:
13463
diff
changeset
|
747 print("For more information on running Prosody as root, see https://prosody.im/doc/root"); |
|
13463
3ce550ce44ce
util.startup: Don't use not yet existent shutdown procedure when started as root (thanks SigmaTel71)
Kim Alvefur <zash@zash.se>
parents:
13462
diff
changeset
|
748 os.exit(1); -- Refusing to run as root |
|
13461
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
749 end |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
750 end |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
751 |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
752 local function remove_pidfile() |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
753 local pidfile = prosody.pidfile; |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
754 if prosody.pidfile_handle then |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
755 prosody.pidfile_handle:close(); |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
756 os.remove(pidfile); |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
757 prosody.pidfile, prosody.pidfile_handle = nil, nil; |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
758 end |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
759 end |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
760 |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
761 function startup.write_pidfile() |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
762 local pposix = check_posix(); |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
763 if not pposix then return end |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
764 local lfs = require "lfs"; |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
765 local stat = lfs.attributes; |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
766 local pidfile = config.get("*", "pidfile") or nil; |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
767 if not pidfile then return end |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
768 pidfile = config.resolve_relative_path(prosody.paths.data, pidfile); |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
769 local mode = stat(pidfile) and "r+" or "w+"; |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
770 local pidfile_handle, err = io.open(pidfile, mode); |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
771 if not pidfile_handle then |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
772 log("error", "Couldn't write pidfile at %s; %s", pidfile, err); |
|
13467
c2a476f4712a
util.startup: Fix exiting on pidfile trouble
Kim Alvefur <zash@zash.se>
parents:
13464
diff
changeset
|
773 os.exit(1); |
|
13461
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
774 else |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
775 prosody.pidfile = pidfile; |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
776 if not lfs.lock(pidfile_handle, "w") then -- Exclusive lock |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
777 local other_pid = pidfile_handle:read("*a"); |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
778 log("error", "Another Prosody instance seems to be running with PID %s, quitting", other_pid); |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
779 prosody.pidfile_handle = nil; |
|
13467
c2a476f4712a
util.startup: Fix exiting on pidfile trouble
Kim Alvefur <zash@zash.se>
parents:
13464
diff
changeset
|
780 os.exit(1); |
|
13461
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
781 else |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
782 pidfile_handle:close(); |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
783 pidfile_handle, err = io.open(pidfile, "w+"); |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
784 if not pidfile_handle then |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
785 log("error", "Couldn't write pidfile at %s; %s", pidfile, err); |
|
13467
c2a476f4712a
util.startup: Fix exiting on pidfile trouble
Kim Alvefur <zash@zash.se>
parents:
13464
diff
changeset
|
786 os.exit(1); |
|
13461
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
787 else |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
788 if lfs.lock(pidfile_handle, "w") then |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
789 pidfile_handle:write(tostring(pposix.getpid())); |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
790 pidfile_handle:flush(); |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
791 prosody.pidfile_handle = pidfile_handle; |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
792 end |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
793 end |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
794 end |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
795 end |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
796 prosody.events.add_handler("server-stopped", remove_pidfile); |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
797 end |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
798 |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
799 local function remove_log_sinks() |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
800 local lm = require "prosody.core.loggingmanager"; |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
801 lm.register_sink_type("console", nil); |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
802 lm.register_sink_type("stdout", nil); |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
803 lm.reload_logging(); |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
804 end |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
805 |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
806 function startup.posix_daemonize() |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
807 if not prosody.opts.daemonize then return end |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
808 local pposix = check_posix(); |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
809 log("info", "Prosody is about to detach from the console, disabling further console output"); |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
810 remove_log_sinks(); |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
811 local ok, ret = pposix.daemonize(); |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
812 if not ok then |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
813 log("error", "Failed to daemonize: %s", ret); |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
814 elseif ret and ret > 0 then |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
815 os.exit(0); |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
816 else |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
817 log("info", "Successfully daemonized to PID %d", pposix.getpid()); |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
818 end |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
819 end |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
820 |
|
13452
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
821 function startup.hook_posix_signals() |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
822 if prosody.platform ~= "posix" then return end |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
823 local have_signal, signal = pcall(require, "prosody.util.signal"); |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
824 if not have_signal then |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
825 log("warn", "Couldn't load signal library, won't respond to SIGTERM"); |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
826 return |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
827 end |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
828 signal.signal("SIGTERM", function() |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
829 log("warn", "Received SIGTERM"); |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
830 prosody.main_thread:run(function() |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
831 prosody.unlock_globals(); |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
832 prosody.shutdown("Received SIGTERM"); |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
833 prosody.lock_globals(); |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
834 end); |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
835 end); |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
836 |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
837 signal.signal("SIGHUP", function() |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
838 log("info", "Received SIGHUP"); |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
839 prosody.main_thread:run(function() prosody.reload_config(); end); |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
840 -- this also reloads logging |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
841 end); |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
842 |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
843 signal.signal("SIGINT", function() |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
844 log("info", "Received SIGINT"); |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
845 prosody.main_thread:run(function() |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
846 prosody.unlock_globals(); |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
847 prosody.shutdown("Received SIGINT"); |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
848 prosody.lock_globals(); |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
849 end); |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
850 end); |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
851 |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
852 signal.signal("SIGUSR1", function() |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
853 log("info", "Received SIGUSR1"); |
|
13454
b0c27628f588
util.startup: Fix firing of USR1/2 events
Kim Alvefur <zash@zash.se>
parents:
13452
diff
changeset
|
854 prosody.events.fire_event("signal/SIGUSR1"); |
|
13452
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
855 end); |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
856 |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
857 signal.signal("SIGUSR2", function() |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
858 log("info", "Received SIGUSR2"); |
|
13454
b0c27628f588
util.startup: Fix firing of USR1/2 events
Kim Alvefur <zash@zash.se>
parents:
13452
diff
changeset
|
859 prosody.events.fire_event("signal/SIGUSR2"); |
|
13452
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
860 end); |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
861 end |
|
69faf3552d52
mod_posix: Move POSIX signal handling into util.startup to avoid race
Kim Alvefur <zash@zash.se>
parents:
13424
diff
changeset
|
862 |
|
13639
94f77a1994dc
util.startup: Drop mention of systemd from notification socket handling
Kim Alvefur <zash@zash.se>
parents:
13630
diff
changeset
|
863 function startup.notification_socket() |
|
13471
afad3b2725bf
util.startup: Support systemd Type=notify service type
Kim Alvefur <zash@zash.se>
parents:
13467
diff
changeset
|
864 local notify_socket_name = os.getenv("NOTIFY_SOCKET"); |
|
afad3b2725bf
util.startup: Support systemd Type=notify service type
Kim Alvefur <zash@zash.se>
parents:
13467
diff
changeset
|
865 if not notify_socket_name then return end |
|
afad3b2725bf
util.startup: Support systemd Type=notify service type
Kim Alvefur <zash@zash.se>
parents:
13467
diff
changeset
|
866 local have_unix, unix = pcall(require, "socket.unix"); |
|
afad3b2725bf
util.startup: Support systemd Type=notify service type
Kim Alvefur <zash@zash.se>
parents:
13467
diff
changeset
|
867 if not have_unix or type(unix) ~= "table" then |
|
13639
94f77a1994dc
util.startup: Drop mention of systemd from notification socket handling
Kim Alvefur <zash@zash.se>
parents:
13630
diff
changeset
|
868 log("error", "LuaSocket without UNIX socket support, can't notify process manager.") |
|
13471
afad3b2725bf
util.startup: Support systemd Type=notify service type
Kim Alvefur <zash@zash.se>
parents:
13467
diff
changeset
|
869 return os.exit(1); |
|
afad3b2725bf
util.startup: Support systemd Type=notify service type
Kim Alvefur <zash@zash.se>
parents:
13467
diff
changeset
|
870 end |
|
afad3b2725bf
util.startup: Support systemd Type=notify service type
Kim Alvefur <zash@zash.se>
parents:
13467
diff
changeset
|
871 log("debug", "Will notify on socket %q", notify_socket_name); |
|
afad3b2725bf
util.startup: Support systemd Type=notify service type
Kim Alvefur <zash@zash.se>
parents:
13467
diff
changeset
|
872 notify_socket_name = notify_socket_name:gsub("^@", "\0"); |
|
afad3b2725bf
util.startup: Support systemd Type=notify service type
Kim Alvefur <zash@zash.se>
parents:
13467
diff
changeset
|
873 local notify_socket = unix.dgram(); |
|
afad3b2725bf
util.startup: Support systemd Type=notify service type
Kim Alvefur <zash@zash.se>
parents:
13467
diff
changeset
|
874 local ok, err = notify_socket:setpeername(notify_socket_name); |
|
afad3b2725bf
util.startup: Support systemd Type=notify service type
Kim Alvefur <zash@zash.se>
parents:
13467
diff
changeset
|
875 if not ok then |
|
13639
94f77a1994dc
util.startup: Drop mention of systemd from notification socket handling
Kim Alvefur <zash@zash.se>
parents:
13630
diff
changeset
|
876 log("error", "Could not connect to notification socket %q: %q", notify_socket_name, err); |
|
13471
afad3b2725bf
util.startup: Support systemd Type=notify service type
Kim Alvefur <zash@zash.se>
parents:
13467
diff
changeset
|
877 return os.exit(1); |
|
afad3b2725bf
util.startup: Support systemd Type=notify service type
Kim Alvefur <zash@zash.se>
parents:
13467
diff
changeset
|
878 end |
|
afad3b2725bf
util.startup: Support systemd Type=notify service type
Kim Alvefur <zash@zash.se>
parents:
13467
diff
changeset
|
879 local time = require "prosody.util.time"; |
|
afad3b2725bf
util.startup: Support systemd Type=notify service type
Kim Alvefur <zash@zash.se>
parents:
13467
diff
changeset
|
880 |
|
afad3b2725bf
util.startup: Support systemd Type=notify service type
Kim Alvefur <zash@zash.se>
parents:
13467
diff
changeset
|
881 prosody.notify_socket = notify_socket; |
|
afad3b2725bf
util.startup: Support systemd Type=notify service type
Kim Alvefur <zash@zash.se>
parents:
13467
diff
changeset
|
882 prosody.events.add_handler("server-started", function() |
|
afad3b2725bf
util.startup: Support systemd Type=notify service type
Kim Alvefur <zash@zash.se>
parents:
13467
diff
changeset
|
883 notify_socket:send("READY=1"); |
|
afad3b2725bf
util.startup: Support systemd Type=notify service type
Kim Alvefur <zash@zash.se>
parents:
13467
diff
changeset
|
884 end); |
|
13477
e8ac3ce3238e
util.startup: Fix notifying config-reload to systemd
Kim Alvefur <zash@zash.se>
parents:
13471
diff
changeset
|
885 prosody.events.add_handler("reloading-config", function() |
|
13471
afad3b2725bf
util.startup: Support systemd Type=notify service type
Kim Alvefur <zash@zash.se>
parents:
13467
diff
changeset
|
886 notify_socket:send(string.format("RELOADING=1\nMONOTONIC_USEC=%d", math.floor(time.monotonic() * 1000000))); |
|
afad3b2725bf
util.startup: Support systemd Type=notify service type
Kim Alvefur <zash@zash.se>
parents:
13467
diff
changeset
|
887 end); |
|
13979
2041c347c178
util.startup: Inform process manager about failure to reload config
Kim Alvefur <zash@zash.se>
parents:
13743
diff
changeset
|
888 prosody.events.add_handler("config-reload-failed", function(event) |
|
2041c347c178
util.startup: Inform process manager about failure to reload config
Kim Alvefur <zash@zash.se>
parents:
13743
diff
changeset
|
889 if event and event.level == "parser" then |
|
2041c347c178
util.startup: Inform process manager about failure to reload config
Kim Alvefur <zash@zash.se>
parents:
13743
diff
changeset
|
890 notify_socket:send(string.format("READY=1\nSTATUS=Error parsing configuration file: %s", tostring(event.error))); |
|
2041c347c178
util.startup: Inform process manager about failure to reload config
Kim Alvefur <zash@zash.se>
parents:
13743
diff
changeset
|
891 elseif event and event.level == "file" then |
|
2041c347c178
util.startup: Inform process manager about failure to reload config
Kim Alvefur <zash@zash.se>
parents:
13743
diff
changeset
|
892 notify_socket:send(string.format("READY=1\nSTATUS=Could not read configuration file: %s", tostring(event.error))); |
|
2041c347c178
util.startup: Inform process manager about failure to reload config
Kim Alvefur <zash@zash.se>
parents:
13743
diff
changeset
|
893 else |
|
2041c347c178
util.startup: Inform process manager about failure to reload config
Kim Alvefur <zash@zash.se>
parents:
13743
diff
changeset
|
894 notify_socket:send("READY=1\nSTATUS=Could not read configuration file"); |
|
2041c347c178
util.startup: Inform process manager about failure to reload config
Kim Alvefur <zash@zash.se>
parents:
13743
diff
changeset
|
895 end |
|
2041c347c178
util.startup: Inform process manager about failure to reload config
Kim Alvefur <zash@zash.se>
parents:
13743
diff
changeset
|
896 end); |
|
13471
afad3b2725bf
util.startup: Support systemd Type=notify service type
Kim Alvefur <zash@zash.se>
parents:
13467
diff
changeset
|
897 prosody.events.add_handler("config-reloaded", function() |
|
afad3b2725bf
util.startup: Support systemd Type=notify service type
Kim Alvefur <zash@zash.se>
parents:
13467
diff
changeset
|
898 notify_socket:send("READY=1"); |
|
afad3b2725bf
util.startup: Support systemd Type=notify service type
Kim Alvefur <zash@zash.se>
parents:
13467
diff
changeset
|
899 end); |
|
afad3b2725bf
util.startup: Support systemd Type=notify service type
Kim Alvefur <zash@zash.se>
parents:
13467
diff
changeset
|
900 prosody.events.add_handler("server-stopping", function() |
|
afad3b2725bf
util.startup: Support systemd Type=notify service type
Kim Alvefur <zash@zash.se>
parents:
13467
diff
changeset
|
901 notify_socket:send("STOPPING=1"); |
|
afad3b2725bf
util.startup: Support systemd Type=notify service type
Kim Alvefur <zash@zash.se>
parents:
13467
diff
changeset
|
902 end); |
|
afad3b2725bf
util.startup: Support systemd Type=notify service type
Kim Alvefur <zash@zash.se>
parents:
13467
diff
changeset
|
903 end |
|
afad3b2725bf
util.startup: Support systemd Type=notify service type
Kim Alvefur <zash@zash.se>
parents:
13467
diff
changeset
|
904 |
|
12294
81f147ddc4ab
prosody: Move last cleanup and shutdown code into util.startup
Kim Alvefur <zash@zash.se>
parents:
12274
diff
changeset
|
905 function startup.cleanup() |
|
81f147ddc4ab
prosody: Move last cleanup and shutdown code into util.startup
Kim Alvefur <zash@zash.se>
parents:
12274
diff
changeset
|
906 prosody.log("info", "Shutdown status: Cleaning up"); |
|
81f147ddc4ab
prosody: Move last cleanup and shutdown code into util.startup
Kim Alvefur <zash@zash.se>
parents:
12274
diff
changeset
|
907 prosody.events.fire_event("server-cleanup"); |
|
81f147ddc4ab
prosody: Move last cleanup and shutdown code into util.startup
Kim Alvefur <zash@zash.se>
parents:
12274
diff
changeset
|
908 end |
|
81f147ddc4ab
prosody: Move last cleanup and shutdown code into util.startup
Kim Alvefur <zash@zash.se>
parents:
12274
diff
changeset
|
909 |
|
81f147ddc4ab
prosody: Move last cleanup and shutdown code into util.startup
Kim Alvefur <zash@zash.se>
parents:
12274
diff
changeset
|
910 function startup.shutdown() |
|
13311
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
911 running_state:begin_shutdown(); |
|
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
912 |
|
12294
81f147ddc4ab
prosody: Move last cleanup and shutdown code into util.startup
Kim Alvefur <zash@zash.se>
parents:
12274
diff
changeset
|
913 prosody.log("info", "Shutting down..."); |
|
81f147ddc4ab
prosody: Move last cleanup and shutdown code into util.startup
Kim Alvefur <zash@zash.se>
parents:
12274
diff
changeset
|
914 startup.cleanup(); |
|
81f147ddc4ab
prosody: Move last cleanup and shutdown code into util.startup
Kim Alvefur <zash@zash.se>
parents:
12274
diff
changeset
|
915 prosody.events.fire_event("server-stopped"); |
|
13311
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
916 |
|
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
917 running_state:finish_shutdown(); |
|
ab36edc7c217
util.startup: Attempt to bring some order to startup/shutdown with util.fsm
Matthew Wild <mwild1@gmail.com>
parents:
13165
diff
changeset
|
918 |
|
12294
81f147ddc4ab
prosody: Move last cleanup and shutdown code into util.startup
Kim Alvefur <zash@zash.se>
parents:
12274
diff
changeset
|
919 prosody.log("info", "Shutdown complete"); |
|
81f147ddc4ab
prosody: Move last cleanup and shutdown code into util.startup
Kim Alvefur <zash@zash.se>
parents:
12274
diff
changeset
|
920 prosody.log("debug", "Shutdown reason was: %s", prosody.shutdown_reason or "not specified"); |
|
81f147ddc4ab
prosody: Move last cleanup and shutdown code into util.startup
Kim Alvefur <zash@zash.se>
parents:
12274
diff
changeset
|
921 prosody.log("debug", "Exiting with status code: %d", prosody.shutdown_code or 0); |
|
12553
cc0ec0277813
util.startup: Fix async waiting for last shutdown steps
Kim Alvefur <zash@zash.se>
parents:
12423
diff
changeset
|
922 server.setquitting(true); |
|
cc0ec0277813
util.startup: Fix async waiting for last shutdown steps
Kim Alvefur <zash@zash.se>
parents:
12423
diff
changeset
|
923 end |
|
cc0ec0277813
util.startup: Fix async waiting for last shutdown steps
Kim Alvefur <zash@zash.se>
parents:
12423
diff
changeset
|
924 |
|
cc0ec0277813
util.startup: Fix async waiting for last shutdown steps
Kim Alvefur <zash@zash.se>
parents:
12423
diff
changeset
|
925 function startup.exit() |
|
12863
891edd1ebde6
util.startup: Close state on exit to ensure GC finalizers are called
Kim Alvefur <zash@zash.se>
parents:
12779
diff
changeset
|
926 os.exit(prosody.shutdown_code, true); |
|
12294
81f147ddc4ab
prosody: Move last cleanup and shutdown code into util.startup
Kim Alvefur <zash@zash.se>
parents:
12274
diff
changeset
|
927 end |
|
81f147ddc4ab
prosody: Move last cleanup and shutdown code into util.startup
Kim Alvefur <zash@zash.se>
parents:
12274
diff
changeset
|
928 |
|
8635
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
929 -- 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
|
930 function startup.prosodyctl() |
|
10636
a9c975a0f113
util.startup: expose current process type (prosody/prosodyctl) in the global prosody object
Matthew Wild <mwild1@gmail.com>
parents:
10604
diff
changeset
|
931 prosody.process_type = "prosodyctl"; |
|
10597
25a3c8134b0a
prosody/util.startup: Switch to parse_args() for --root and --config
Matthew Wild <mwild1@gmail.com>
parents:
10596
diff
changeset
|
932 startup.parse_args(); |
|
8699
580c13ed0ca1
util.startup: Initialize the 'prosody' global earlier (various things needs the global util.events instance)
Kim Alvefur <zash@zash.se>
parents:
8698
diff
changeset
|
933 startup.init_global_state(); |
|
8635
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
934 startup.read_config(); |
|
8755
857d8f38a010
util.startup: Force console logging before initializing logging (see 2fdeb979cc7c)
Kim Alvefur <zash@zash.se>
parents:
8748
diff
changeset
|
935 startup.force_console_logging(); |
|
8748
2fdeb979cc7c
util.startup: Initialize logging immediately after configuration is read (which is how it used to work)
Matthew Wild <mwild1@gmail.com>
parents:
8734
diff
changeset
|
936 startup.init_logging(); |
|
11073
5691b9773c5b
util.startup: Configure the GC on startup, using the config or built-in defaults
Matthew Wild <mwild1@gmail.com>
parents:
10947
diff
changeset
|
937 startup.init_gc(); |
|
11050
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10948
diff
changeset
|
938 startup.init_errors(); |
|
8698
0499f3da0ec4
util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents:
8692
diff
changeset
|
939 startup.setup_plugindir(); |
|
11138
2a19d61f4ae4
util.startup: Re-enable installer path setup
Kim Alvefur <zash@zash.se>
parents:
11137
diff
changeset
|
940 startup.setup_plugin_install_path(); |
|
8698
0499f3da0ec4
util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents:
8692
diff
changeset
|
941 startup.setup_datadir(); |
|
8635
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
942 startup.chdir(); |
|
8688
019b4b3dd5ad
util.startup: Read version for prosodyctl (restores version in 'about' command)
Kim Alvefur <zash@zash.se>
parents:
8682
diff
changeset
|
943 startup.read_version(); |
|
8665
4b260a3f8b94
util.startup: Restore user switching
Matthew Wild <mwild1@gmail.com>
parents:
8664
diff
changeset
|
944 startup.switch_user(); |
|
14205
a874c3f4570a
util.startup: Always apply umask (thanks Max Hearnden)
Matthew Wild <mwild1@gmail.com>
parents:
14130
diff
changeset
|
945 startup.set_umask(); |
|
8635
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
946 startup.check_dependencies(); |
|
9873
dfaeea570f7e
util.startup: Give function a more generic name so it can apply to all warnings
Matthew Wild <mwild1@gmail.com>
parents:
9762
diff
changeset
|
947 startup.log_startup_warnings(); |
|
8635
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
948 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
|
949 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
|
950 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
|
951 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
|
952 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
|
953 |
|
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
954 function startup.prosody() |
|
8682
151ecd18d624
prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents:
8673
diff
changeset
|
955 -- 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:
8673
diff
changeset
|
956 -- previous steps to have already been performed |
|
10636
a9c975a0f113
util.startup: expose current process type (prosody/prosodyctl) in the global prosody object
Matthew Wild <mwild1@gmail.com>
parents:
10604
diff
changeset
|
957 prosody.process_type = "prosody"; |
|
10597
25a3c8134b0a
prosody/util.startup: Switch to parse_args() for --root and --config
Matthew Wild <mwild1@gmail.com>
parents:
10596
diff
changeset
|
958 startup.parse_args(); |
|
8699
580c13ed0ca1
util.startup: Initialize the 'prosody' global earlier (various things needs the global util.events instance)
Kim Alvefur <zash@zash.se>
parents:
8698
diff
changeset
|
959 startup.init_global_state(); |
|
8682
151ecd18d624
prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents:
8673
diff
changeset
|
960 startup.read_config(); |
|
13464
2dbc169aae6a
util.startup: Abort before initialization of logging when started as root
Kim Alvefur <zash@zash.se>
parents:
13463
diff
changeset
|
961 startup.check_user(); |
|
8748
2fdeb979cc7c
util.startup: Initialize logging immediately after configuration is read (which is how it used to work)
Matthew Wild <mwild1@gmail.com>
parents:
8734
diff
changeset
|
962 startup.init_logging(); |
|
11073
5691b9773c5b
util.startup: Configure the GC on startup, using the config or built-in defaults
Matthew Wild <mwild1@gmail.com>
parents:
10947
diff
changeset
|
963 startup.init_gc(); |
|
11050
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10948
diff
changeset
|
964 startup.init_errors(); |
|
8682
151ecd18d624
prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents:
8673
diff
changeset
|
965 startup.sanity_check(); |
|
151ecd18d624
prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents:
8673
diff
changeset
|
966 startup.sandbox_require(); |
|
151ecd18d624
prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents:
8673
diff
changeset
|
967 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:
8673
diff
changeset
|
968 startup.check_dependencies(); |
|
151ecd18d624
prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents:
8673
diff
changeset
|
969 startup.load_libraries(); |
|
8698
0499f3da0ec4
util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents:
8692
diff
changeset
|
970 startup.setup_plugindir(); |
|
11138
2a19d61f4ae4
util.startup: Re-enable installer path setup
Kim Alvefur <zash@zash.se>
parents:
11137
diff
changeset
|
971 startup.setup_plugin_install_path(); |
|
8698
0499f3da0ec4
util.startup: Factor out processing of plugin and data paths into a separate functions
Kim Alvefur <zash@zash.se>
parents:
8692
diff
changeset
|
972 startup.setup_datadir(); |
|
8682
151ecd18d624
prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents:
8673
diff
changeset
|
973 startup.chdir(); |
|
14205
a874c3f4570a
util.startup: Always apply umask (thanks Max Hearnden)
Matthew Wild <mwild1@gmail.com>
parents:
14130
diff
changeset
|
974 startup.set_umask(); |
|
8682
151ecd18d624
prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents:
8673
diff
changeset
|
975 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:
8673
diff
changeset
|
976 startup.read_version(); |
|
151ecd18d624
prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents:
8673
diff
changeset
|
977 startup.log_greeting(); |
|
9873
dfaeea570f7e
util.startup: Give function a more generic name so it can apply to all warnings
Matthew Wild <mwild1@gmail.com>
parents:
9762
diff
changeset
|
978 startup.log_startup_warnings(); |
|
8682
151ecd18d624
prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents:
8673
diff
changeset
|
979 startup.load_secondary_libraries(); |
|
11948
dca75cc6fc5a
util.startup: Integrate util.promise with net.server main loop
Kim Alvefur <zash@zash.se>
parents:
11870
diff
changeset
|
980 startup.init_promise(); |
|
11963
f5c6be4a3ecc
util.startup: Initialize util.async at startup
Matthew Wild <mwild1@gmail.com>
parents:
11948
diff
changeset
|
981 startup.init_async(); |
|
13659
5abdcad8c2e0
net.adns: Collect DNS lookup timing metrics
Kim Alvefur <zash@zash.se>
parents:
13639
diff
changeset
|
982 startup.instrument(); |
|
8682
151ecd18d624
prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents:
8673
diff
changeset
|
983 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:
8673
diff
changeset
|
984 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:
8673
diff
changeset
|
985 startup.init_global_protection(); |
|
13461
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
986 startup.posix_daemonize(); |
|
c673ff1075bd
mod_posix: Move everything to util.startup
Kim Alvefur <zash@zash.se>
parents:
13459
diff
changeset
|
987 startup.write_pidfile(); |
|
13459
790f60c0843b
util.startup: Back out 598df17b8ebb
Kim Alvefur <zash@zash.se>
parents:
13458
diff
changeset
|
988 startup.hook_posix_signals(); |
|
13639
94f77a1994dc
util.startup: Drop mention of systemd from notification socket handling
Kim Alvefur <zash@zash.se>
parents:
13630
diff
changeset
|
989 startup.notification_socket(); |
|
8682
151ecd18d624
prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents:
8673
diff
changeset
|
990 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:
8673
diff
changeset
|
991 startup.notify_started(); |
|
8635
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
992 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
|
993 |
|
47e3b8b6f17a
prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
994 return startup; |
