Software /
code /
prosody
Annotate
plugins/mod_posix.lua @ 10441:f2c9abc71f08
Merge 0.11->trunk
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 24 Nov 2019 04:46:36 +0100 |
parent | 9928:d92ab8c8006a |
child | 10599:4f655918fef1 |
rev | line source |
---|---|
1522
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1238
diff
changeset
|
1 -- Prosody IM |
2923
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
2795
diff
changeset
|
2 -- Copyright (C) 2008-2010 Matthew Wild |
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
2795
diff
changeset
|
3 -- Copyright (C) 2008-2010 Waqas Hussain |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5452
diff
changeset
|
4 -- |
1522
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1238
diff
changeset
|
5 -- This project is MIT/X11 licensed. Please see the |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1238
diff
changeset
|
6 -- COPYING file in the source package for more information. |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1238
diff
changeset
|
7 -- |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1238
diff
changeset
|
8 |
728
fa45dfb27ee5
mod_posix: Check version of pposix
Matthew Wild <mwild1@gmail.com>
parents:
723
diff
changeset
|
9 |
8012 | 10 local want_pposix_version = "0.4.0"; |
587 | 11 |
12 local pposix = assert(require "util.pposix"); | |
5452
edf3db386a19
mod_posix: Improve error message for a pposix version mismatch
Matthew Wild <mwild1@gmail.com>
parents:
5451
diff
changeset
|
13 if pposix._VERSION ~= want_pposix_version then |
8166
bbedf564b9f9
mod_posix: Split long line [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8116
diff
changeset
|
14 module:log("warn", "Unknown version (%s) of binary pposix module, expected %s." |
bbedf564b9f9
mod_posix: Split long line [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8116
diff
changeset
|
15 .. "Perhaps you need to recompile?", tostring(pposix._VERSION), want_pposix_version); |
5452
edf3db386a19
mod_posix: Improve error message for a pposix version mismatch
Matthew Wild <mwild1@gmail.com>
parents:
5451
diff
changeset
|
16 end |
587 | 17 |
6875
12d68f7b1be0
mod_posix: Detect failure to load util.signal by first pcall return value not by type of the second
Kim Alvefur <zash@zash.se>
parents:
6874
diff
changeset
|
18 local have_signal, signal = pcall(require, "util.signal"); |
12d68f7b1be0
mod_posix: Detect failure to load util.signal by first pcall return value not by type of the second
Kim Alvefur <zash@zash.se>
parents:
6874
diff
changeset
|
19 if not have_signal then |
1062
f9a1ac50782b
mod_posix: Fix calls to log() (replace with module:log) and make some global accesses explicit
Matthew Wild <mwild1@gmail.com>
parents:
1061
diff
changeset
|
20 module:log("warn", "Couldn't load signal library, won't respond to SIGTERM"); |
991
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
21 end |
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
22 |
2793
08892e3f24bd
mod_posix: Lock pidfile when in use, shut down if we can't write or lock the pidfile
Matthew Wild <mwild1@gmail.com>
parents:
2074
diff
changeset
|
23 local lfs = require "lfs"; |
2795
d6fcd13c07e7
mod_posix: Adjust file open mode depending on whether file exists (take that fopen designers!!!)
Matthew Wild <mwild1@gmail.com>
parents:
2793
diff
changeset
|
24 local stat = lfs.attributes; |
2793
08892e3f24bd
mod_posix: Lock pidfile when in use, shut down if we can't write or lock the pidfile
Matthew Wild <mwild1@gmail.com>
parents:
2074
diff
changeset
|
25 |
1238
f4c08caca3e7
mod_posix: Use global prosody object
Matthew Wild <mwild1@gmail.com>
parents:
1119
diff
changeset
|
26 local prosody = _G.prosody; |
f4c08caca3e7
mod_posix: Use global prosody object
Matthew Wild <mwild1@gmail.com>
parents:
1119
diff
changeset
|
27 |
4623
403b56b78018
mod_posix, mod_bosh, mod_admin_telnet: Use module:set_global()
Kim Alvefur <zash@zash.se>
parents:
3555
diff
changeset
|
28 module:set_global(); -- we're a global module |
587 | 29 |
7731
a0ee83c4a82c
mod_posix: Use type-specific config API
Kim Alvefur <zash@zash.se>
parents:
6875
diff
changeset
|
30 local umask = module:get_option_string("umask", "027"); |
2440
11e3d16a128f
mod_posix: Set umask to 'umask' from the config, or 027
Matthew Wild <mwild1@gmail.com>
parents:
2438
diff
changeset
|
31 pposix.umask(umask); |
11e3d16a128f
mod_posix: Set umask to 'umask' from the config, or 027
Matthew Wild <mwild1@gmail.com>
parents:
2438
diff
changeset
|
32 |
1680
f3d241915429
Add setuid and setgid support.
Tobias Markmann <tm@ayena.de>
parents:
1579
diff
changeset
|
33 -- Allow switching away from root, some people like strange ports. |
3537
7bbb19804d82
mod_posix: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
3481
diff
changeset
|
34 module:hook("server-started", function () |
6874
e011f289ec77
mod_posix: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6367
diff
changeset
|
35 local uid = module:get_option("setuid"); |
e011f289ec77
mod_posix: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6367
diff
changeset
|
36 local gid = module:get_option("setgid"); |
e011f289ec77
mod_posix: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6367
diff
changeset
|
37 if gid then |
e011f289ec77
mod_posix: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6367
diff
changeset
|
38 local success, msg = pposix.setgid(gid); |
e011f289ec77
mod_posix: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6367
diff
changeset
|
39 if success then |
e011f289ec77
mod_posix: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6367
diff
changeset
|
40 module:log("debug", "Changed group to %s successfully.", gid); |
e011f289ec77
mod_posix: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6367
diff
changeset
|
41 else |
e011f289ec77
mod_posix: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6367
diff
changeset
|
42 module:log("error", "Failed to change group to %s. Error: %s", gid, msg); |
e011f289ec77
mod_posix: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6367
diff
changeset
|
43 prosody.shutdown("Failed to change group to %s", gid); |
1680
f3d241915429
Add setuid and setgid support.
Tobias Markmann <tm@ayena.de>
parents:
1579
diff
changeset
|
44 end |
6874
e011f289ec77
mod_posix: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6367
diff
changeset
|
45 end |
e011f289ec77
mod_posix: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6367
diff
changeset
|
46 if uid then |
e011f289ec77
mod_posix: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6367
diff
changeset
|
47 local success, msg = pposix.setuid(uid); |
e011f289ec77
mod_posix: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6367
diff
changeset
|
48 if success then |
e011f289ec77
mod_posix: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6367
diff
changeset
|
49 module:log("debug", "Changed user to %s successfully.", uid); |
e011f289ec77
mod_posix: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6367
diff
changeset
|
50 else |
e011f289ec77
mod_posix: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6367
diff
changeset
|
51 module:log("error", "Failed to change user to %s. Error: %s", uid, msg); |
e011f289ec77
mod_posix: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6367
diff
changeset
|
52 prosody.shutdown("Failed to change user to %s", uid); |
1680
f3d241915429
Add setuid and setgid support.
Tobias Markmann <tm@ayena.de>
parents:
1579
diff
changeset
|
53 end |
6874
e011f289ec77
mod_posix: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6367
diff
changeset
|
54 end |
e011f289ec77
mod_posix: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6367
diff
changeset
|
55 end); |
1680
f3d241915429
Add setuid and setgid support.
Tobias Markmann <tm@ayena.de>
parents:
1579
diff
changeset
|
56 |
1092
b547967d87fc
mod_posix: Don't let the server run as root without the magic run_as_root in config
Matthew Wild <mwild1@gmail.com>
parents:
1062
diff
changeset
|
57 -- Don't even think about it! |
3022
948d511f479c
mod_posix: Don't add a server-starting event handler while the server-starting event is being fired.
Waqas Hussain <waqas20@gmail.com>
parents:
2925
diff
changeset
|
58 if not prosody.start_time then -- server-starting |
948d511f479c
mod_posix: Don't add a server-starting event handler while the server-starting event is being fired.
Waqas Hussain <waqas20@gmail.com>
parents:
2925
diff
changeset
|
59 local suid = module:get_option("setuid"); |
948d511f479c
mod_posix: Don't add a server-starting event handler while the server-starting event is being fired.
Waqas Hussain <waqas20@gmail.com>
parents:
2925
diff
changeset
|
60 if not suid or suid == 0 or suid == "root" then |
8116
76ac8b617402
mod_posix: Use typed config API
Kim Alvefur <zash@zash.se>
parents:
8012
diff
changeset
|
61 if pposix.getuid() == 0 and not module:get_option_boolean("run_as_root") then |
3022
948d511f479c
mod_posix: Don't add a server-starting event handler while the server-starting event is being fired.
Waqas Hussain <waqas20@gmail.com>
parents:
2925
diff
changeset
|
62 module:log("error", "Danger, Will Robinson! Prosody doesn't need to be run as root, so don't do it!"); |
7359
a5a080c12c96
Update every link to the documentation to use HTTPS
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6875
diff
changeset
|
63 module:log("error", "For more information on running Prosody as root, see https://prosody.im/doc/root"); |
3022
948d511f479c
mod_posix: Don't add a server-starting event handler while the server-starting event is being fired.
Waqas Hussain <waqas20@gmail.com>
parents:
2925
diff
changeset
|
64 prosody.shutdown("Refusing to run as root"); |
1092
b547967d87fc
mod_posix: Don't let the server run as root without the magic run_as_root in config
Matthew Wild <mwild1@gmail.com>
parents:
1062
diff
changeset
|
65 end |
3022
948d511f479c
mod_posix: Don't add a server-starting event handler while the server-starting event is being fired.
Waqas Hussain <waqas20@gmail.com>
parents:
2925
diff
changeset
|
66 end |
948d511f479c
mod_posix: Don't add a server-starting event handler while the server-starting event is being fired.
Waqas Hussain <waqas20@gmail.com>
parents:
2925
diff
changeset
|
67 end |
1092
b547967d87fc
mod_posix: Don't let the server run as root without the magic run_as_root in config
Matthew Wild <mwild1@gmail.com>
parents:
1062
diff
changeset
|
68 |
2793
08892e3f24bd
mod_posix: Lock pidfile when in use, shut down if we can't write or lock the pidfile
Matthew Wild <mwild1@gmail.com>
parents:
2074
diff
changeset
|
69 local pidfile; |
08892e3f24bd
mod_posix: Lock pidfile when in use, shut down if we can't write or lock the pidfile
Matthew Wild <mwild1@gmail.com>
parents:
2074
diff
changeset
|
70 local pidfile_handle; |
1032
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
71 |
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
72 local function remove_pidfile() |
2793
08892e3f24bd
mod_posix: Lock pidfile when in use, shut down if we can't write or lock the pidfile
Matthew Wild <mwild1@gmail.com>
parents:
2074
diff
changeset
|
73 if pidfile_handle then |
08892e3f24bd
mod_posix: Lock pidfile when in use, shut down if we can't write or lock the pidfile
Matthew Wild <mwild1@gmail.com>
parents:
2074
diff
changeset
|
74 pidfile_handle:close(); |
08892e3f24bd
mod_posix: Lock pidfile when in use, shut down if we can't write or lock the pidfile
Matthew Wild <mwild1@gmail.com>
parents:
2074
diff
changeset
|
75 os.remove(pidfile); |
08892e3f24bd
mod_posix: Lock pidfile when in use, shut down if we can't write or lock the pidfile
Matthew Wild <mwild1@gmail.com>
parents:
2074
diff
changeset
|
76 pidfile, pidfile_handle = nil, nil; |
1032
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
77 end |
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
78 end |
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
79 |
991
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
80 local function write_pidfile() |
2793
08892e3f24bd
mod_posix: Lock pidfile when in use, shut down if we can't write or lock the pidfile
Matthew Wild <mwild1@gmail.com>
parents:
2074
diff
changeset
|
81 if pidfile_handle then |
1032
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
82 remove_pidfile(); |
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
83 end |
7992
51396e0836cf
mod_posix: Use path variant of config API for pidfile option
Kim Alvefur <zash@zash.se>
parents:
7731
diff
changeset
|
84 pidfile = module:get_option_path("pidfile", nil, "data"); |
991
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
85 if pidfile then |
3026
dec4527a7499
mod_posix: Fixed a global access.
Waqas Hussain <waqas20@gmail.com>
parents:
2923
diff
changeset
|
86 local err; |
2795
d6fcd13c07e7
mod_posix: Adjust file open mode depending on whether file exists (take that fopen designers!!!)
Matthew Wild <mwild1@gmail.com>
parents:
2793
diff
changeset
|
87 local mode = stat(pidfile) and "r+" or "w+"; |
d6fcd13c07e7
mod_posix: Adjust file open mode depending on whether file exists (take that fopen designers!!!)
Matthew Wild <mwild1@gmail.com>
parents:
2793
diff
changeset
|
88 pidfile_handle, err = io.open(pidfile, mode); |
2793
08892e3f24bd
mod_posix: Lock pidfile when in use, shut down if we can't write or lock the pidfile
Matthew Wild <mwild1@gmail.com>
parents:
2074
diff
changeset
|
89 if not pidfile_handle then |
08892e3f24bd
mod_posix: Lock pidfile when in use, shut down if we can't write or lock the pidfile
Matthew Wild <mwild1@gmail.com>
parents:
2074
diff
changeset
|
90 module:log("error", "Couldn't write pidfile at %s; %s", pidfile, err); |
08892e3f24bd
mod_posix: Lock pidfile when in use, shut down if we can't write or lock the pidfile
Matthew Wild <mwild1@gmail.com>
parents:
2074
diff
changeset
|
91 prosody.shutdown("Couldn't write pidfile"); |
991
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
92 else |
2793
08892e3f24bd
mod_posix: Lock pidfile when in use, shut down if we can't write or lock the pidfile
Matthew Wild <mwild1@gmail.com>
parents:
2074
diff
changeset
|
93 if not lfs.lock(pidfile_handle, "w") then -- Exclusive lock |
08892e3f24bd
mod_posix: Lock pidfile when in use, shut down if we can't write or lock the pidfile
Matthew Wild <mwild1@gmail.com>
parents:
2074
diff
changeset
|
94 local other_pid = pidfile_handle:read("*a"); |
08892e3f24bd
mod_posix: Lock pidfile when in use, shut down if we can't write or lock the pidfile
Matthew Wild <mwild1@gmail.com>
parents:
2074
diff
changeset
|
95 module:log("error", "Another Prosody instance seems to be running with PID %s, quitting", other_pid); |
08892e3f24bd
mod_posix: Lock pidfile when in use, shut down if we can't write or lock the pidfile
Matthew Wild <mwild1@gmail.com>
parents:
2074
diff
changeset
|
96 pidfile_handle = nil; |
08892e3f24bd
mod_posix: Lock pidfile when in use, shut down if we can't write or lock the pidfile
Matthew Wild <mwild1@gmail.com>
parents:
2074
diff
changeset
|
97 prosody.shutdown("Prosody already running"); |
08892e3f24bd
mod_posix: Lock pidfile when in use, shut down if we can't write or lock the pidfile
Matthew Wild <mwild1@gmail.com>
parents:
2074
diff
changeset
|
98 else |
3341
a8a3e662fea7
mod_posix: Some (perhaps temporary) changes to re-lock the pidfile after truncating, to avoid breaking prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
3340
diff
changeset
|
99 pidfile_handle:close(); |
3340
0769cc5f34b6
mod_posix: Truncate the pidfile before writing to ensure that we never overwrite with a PID shorter than the previous, and end with an invalid PID in the file.
Brian Cully <bjc@junctionnetworks.com>
parents:
3029
diff
changeset
|
100 pidfile_handle, err = io.open(pidfile, "w+"); |
0769cc5f34b6
mod_posix: Truncate the pidfile before writing to ensure that we never overwrite with a PID shorter than the previous, and end with an invalid PID in the file.
Brian Cully <bjc@junctionnetworks.com>
parents:
3029
diff
changeset
|
101 if not pidfile_handle then |
0769cc5f34b6
mod_posix: Truncate the pidfile before writing to ensure that we never overwrite with a PID shorter than the previous, and end with an invalid PID in the file.
Brian Cully <bjc@junctionnetworks.com>
parents:
3029
diff
changeset
|
102 module:log("error", "Couldn't write pidfile at %s; %s", pidfile, err); |
0769cc5f34b6
mod_posix: Truncate the pidfile before writing to ensure that we never overwrite with a PID shorter than the previous, and end with an invalid PID in the file.
Brian Cully <bjc@junctionnetworks.com>
parents:
3029
diff
changeset
|
103 prosody.shutdown("Couldn't write pidfile"); |
3341
a8a3e662fea7
mod_posix: Some (perhaps temporary) changes to re-lock the pidfile after truncating, to avoid breaking prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
3340
diff
changeset
|
104 else |
a8a3e662fea7
mod_posix: Some (perhaps temporary) changes to re-lock the pidfile after truncating, to avoid breaking prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
3340
diff
changeset
|
105 if lfs.lock(pidfile_handle, "w") then |
a8a3e662fea7
mod_posix: Some (perhaps temporary) changes to re-lock the pidfile after truncating, to avoid breaking prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
3340
diff
changeset
|
106 pidfile_handle:write(tostring(pposix.getpid())); |
a8a3e662fea7
mod_posix: Some (perhaps temporary) changes to re-lock the pidfile after truncating, to avoid breaking prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
3340
diff
changeset
|
107 pidfile_handle:flush(); |
a8a3e662fea7
mod_posix: Some (perhaps temporary) changes to re-lock the pidfile after truncating, to avoid breaking prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
3340
diff
changeset
|
108 end |
3340
0769cc5f34b6
mod_posix: Truncate the pidfile before writing to ensure that we never overwrite with a PID shorter than the previous, and end with an invalid PID in the file.
Brian Cully <bjc@junctionnetworks.com>
parents:
3029
diff
changeset
|
109 end |
2793
08892e3f24bd
mod_posix: Lock pidfile when in use, shut down if we can't write or lock the pidfile
Matthew Wild <mwild1@gmail.com>
parents:
2074
diff
changeset
|
110 end |
991
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
111 end |
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
112 end |
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
113 end |
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
114 |
6002
3547f315a1f5
mod_posix: Daemonize by default only when installed
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
115 local daemonize = module:get_option("daemonize", prosody.installed); |
2073
72784ce0c0e0
mod_posix: Switch config option to 'daemonize', fall back to 'no_daemonize' if not set, default behaviour remains the same... daemonize if mod_posix is loaded
Matthew Wild <mwild1@gmail.com>
parents:
1712
diff
changeset
|
116 |
5175
fabaed7418a6
mod_posix: Remove console and stdout logging sinks before daemonizing
Kim Alvefur <zash@zash.se>
parents:
4993
diff
changeset
|
117 local function remove_log_sinks() |
fabaed7418a6
mod_posix: Remove console and stdout logging sinks before daemonizing
Kim Alvefur <zash@zash.se>
parents:
4993
diff
changeset
|
118 local lm = require "core.loggingmanager"; |
fabaed7418a6
mod_posix: Remove console and stdout logging sinks before daemonizing
Kim Alvefur <zash@zash.se>
parents:
4993
diff
changeset
|
119 lm.register_sink_type("console", nil); |
fabaed7418a6
mod_posix: Remove console and stdout logging sinks before daemonizing
Kim Alvefur <zash@zash.se>
parents:
4993
diff
changeset
|
120 lm.register_sink_type("stdout", nil); |
fabaed7418a6
mod_posix: Remove console and stdout logging sinks before daemonizing
Kim Alvefur <zash@zash.se>
parents:
4993
diff
changeset
|
121 lm.reload_logging(); |
fabaed7418a6
mod_posix: Remove console and stdout logging sinks before daemonizing
Kim Alvefur <zash@zash.se>
parents:
4993
diff
changeset
|
122 end |
fabaed7418a6
mod_posix: Remove console and stdout logging sinks before daemonizing
Kim Alvefur <zash@zash.se>
parents:
4993
diff
changeset
|
123 |
2073
72784ce0c0e0
mod_posix: Switch config option to 'daemonize', fall back to 'no_daemonize' if not set, default behaviour remains the same... daemonize if mod_posix is loaded
Matthew Wild <mwild1@gmail.com>
parents:
1712
diff
changeset
|
124 if daemonize then |
587 | 125 local function daemonize_server() |
5177
add9ad38208e
mod_posix: Log a message explaining that we are detaching from the console
Kim Alvefur <zash@zash.se>
parents:
5175
diff
changeset
|
126 module:log("info", "Prosody is about to detach from the console, disabling further console output"); |
5175
fabaed7418a6
mod_posix: Remove console and stdout logging sinks before daemonizing
Kim Alvefur <zash@zash.se>
parents:
4993
diff
changeset
|
127 remove_log_sinks(); |
587 | 128 local ok, ret = pposix.daemonize(); |
129 if not ok then | |
1062
f9a1ac50782b
mod_posix: Fix calls to log() (replace with module:log) and make some global accesses explicit
Matthew Wild <mwild1@gmail.com>
parents:
1061
diff
changeset
|
130 module:log("error", "Failed to daemonize: %s", ret); |
587 | 131 elseif ret and ret > 0 then |
132 os.exit(0); | |
133 else | |
1062
f9a1ac50782b
mod_posix: Fix calls to log() (replace with module:log) and make some global accesses explicit
Matthew Wild <mwild1@gmail.com>
parents:
1061
diff
changeset
|
134 module:log("info", "Successfully daemonized to PID %d", pposix.getpid()); |
991
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
135 write_pidfile(); |
587 | 136 end |
137 end | |
3022
948d511f479c
mod_posix: Don't add a server-starting event handler while the server-starting event is being fired.
Waqas Hussain <waqas20@gmail.com>
parents:
2925
diff
changeset
|
138 if not prosody.start_time then -- server-starting |
948d511f479c
mod_posix: Don't add a server-starting event handler while the server-starting event is being fired.
Waqas Hussain <waqas20@gmail.com>
parents:
2925
diff
changeset
|
139 daemonize_server(); |
948d511f479c
mod_posix: Don't add a server-starting event handler while the server-starting event is being fired.
Waqas Hussain <waqas20@gmail.com>
parents:
2925
diff
changeset
|
140 end |
991
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
141 else |
1032
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
142 -- Not going to daemonize, so write the pid of this process |
991
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
143 write_pidfile(); |
587 | 144 end |
991
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
145 |
3537
7bbb19804d82
mod_posix: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
3481
diff
changeset
|
146 module:hook("server-stopped", remove_pidfile); |
1032
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
147 |
1118
239d4362a040
mod_posix: Reload the config and reopen log files on SIGHUP
Matthew Wild <mwild1@gmail.com>
parents:
1092
diff
changeset
|
148 -- Set signal handlers |
6875
12d68f7b1be0
mod_posix: Detect failure to load util.signal by first pcall return value not by type of the second
Kim Alvefur <zash@zash.se>
parents:
6874
diff
changeset
|
149 if have_signal then |
8663
a7a9d9511dc1
mod_posix: Delay setting signal handlers until in the main thread
Matthew Wild <mwild1@gmail.com>
parents:
8235
diff
changeset
|
150 module:add_timer(0, function () |
a7a9d9511dc1
mod_posix: Delay setting signal handlers until in the main thread
Matthew Wild <mwild1@gmail.com>
parents:
8235
diff
changeset
|
151 signal.signal("SIGTERM", function () |
a7a9d9511dc1
mod_posix: Delay setting signal handlers until in the main thread
Matthew Wild <mwild1@gmail.com>
parents:
8235
diff
changeset
|
152 module:log("warn", "Received SIGTERM"); |
a7a9d9511dc1
mod_posix: Delay setting signal handlers until in the main thread
Matthew Wild <mwild1@gmail.com>
parents:
8235
diff
changeset
|
153 prosody.unlock_globals(); |
a7a9d9511dc1
mod_posix: Delay setting signal handlers until in the main thread
Matthew Wild <mwild1@gmail.com>
parents:
8235
diff
changeset
|
154 prosody.shutdown("Received SIGTERM"); |
a7a9d9511dc1
mod_posix: Delay setting signal handlers until in the main thread
Matthew Wild <mwild1@gmail.com>
parents:
8235
diff
changeset
|
155 prosody.lock_globals(); |
a7a9d9511dc1
mod_posix: Delay setting signal handlers until in the main thread
Matthew Wild <mwild1@gmail.com>
parents:
8235
diff
changeset
|
156 end); |
1118
239d4362a040
mod_posix: Reload the config and reopen log files on SIGHUP
Matthew Wild <mwild1@gmail.com>
parents:
1092
diff
changeset
|
157 |
8663
a7a9d9511dc1
mod_posix: Delay setting signal handlers until in the main thread
Matthew Wild <mwild1@gmail.com>
parents:
8235
diff
changeset
|
158 signal.signal("SIGHUP", function () |
a7a9d9511dc1
mod_posix: Delay setting signal handlers until in the main thread
Matthew Wild <mwild1@gmail.com>
parents:
8235
diff
changeset
|
159 module:log("info", "Received SIGHUP"); |
a7a9d9511dc1
mod_posix: Delay setting signal handlers until in the main thread
Matthew Wild <mwild1@gmail.com>
parents:
8235
diff
changeset
|
160 prosody.reload_config(); |
9763
982529dd0bed
mod_posix: Don't reload logging twice
Kim Alvefur <zash@zash.se>
parents:
8663
diff
changeset
|
161 -- this also reloads logging |
8663
a7a9d9511dc1
mod_posix: Delay setting signal handlers until in the main thread
Matthew Wild <mwild1@gmail.com>
parents:
8235
diff
changeset
|
162 end); |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5452
diff
changeset
|
163 |
8663
a7a9d9511dc1
mod_posix: Delay setting signal handlers until in the main thread
Matthew Wild <mwild1@gmail.com>
parents:
8235
diff
changeset
|
164 signal.signal("SIGINT", function () |
a7a9d9511dc1
mod_posix: Delay setting signal handlers until in the main thread
Matthew Wild <mwild1@gmail.com>
parents:
8235
diff
changeset
|
165 module:log("info", "Received SIGINT"); |
a7a9d9511dc1
mod_posix: Delay setting signal handlers until in the main thread
Matthew Wild <mwild1@gmail.com>
parents:
8235
diff
changeset
|
166 prosody.unlock_globals(); |
a7a9d9511dc1
mod_posix: Delay setting signal handlers until in the main thread
Matthew Wild <mwild1@gmail.com>
parents:
8235
diff
changeset
|
167 prosody.shutdown("Received SIGINT"); |
a7a9d9511dc1
mod_posix: Delay setting signal handlers until in the main thread
Matthew Wild <mwild1@gmail.com>
parents:
8235
diff
changeset
|
168 prosody.lock_globals(); |
a7a9d9511dc1
mod_posix: Delay setting signal handlers until in the main thread
Matthew Wild <mwild1@gmail.com>
parents:
8235
diff
changeset
|
169 end); |
2332 | 170 end); |
991
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
171 end |