plugins/mod_posix.lua

Wed, 05 Jan 2011 03:05:13 +0000

author
daurnimator <quae@daurnimator.com>
date
Wed, 05 Jan 2011 03:05:13 +0000
changeset 3998
13a5a8df7c34
parent 3555
6938b6153890
permissions
-rw-r--r--

stanza_router: Replace xmlns == nil checks with 'not xmlns'

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
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1238
diff changeset
4 --
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
3481
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
10 local want_pposix_version = "0.3.5";
587
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local pposix = assert(require "util.pposix");
735
d247d061409a mod_posix: logging fix
Matthew Wild <mwild1@gmail.com>
parents: 734
diff changeset
13 if pposix._VERSION ~= want_pposix_version then module:log("warn", "Unknown version (%s) of binary pposix module, expected %s", tostring(pposix._VERSION), want_pposix_version); end
587
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
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
15 local signal = select(2, pcall(require, "util.signal"));
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
16 if type(signal) == "string" 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
17 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
18 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
19
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
20 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
21 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
22
1238
f4c08caca3e7 mod_posix: Use global prosody object
Matthew Wild <mwild1@gmail.com>
parents: 1119
diff changeset
23 local prosody = _G.prosody;
f4c08caca3e7 mod_posix: Use global prosody object
Matthew Wild <mwild1@gmail.com>
parents: 1119
diff changeset
24
587
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 module.host = "*"; -- we're a global module
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
2440
11e3d16a128f mod_posix: Set umask to 'umask' from the config, or 027
Matthew Wild <mwild1@gmail.com>
parents: 2438
diff changeset
27 local umask = module:get_option("umask") or "027";
11e3d16a128f mod_posix: Set umask to 'umask' from the config, or 027
Matthew Wild <mwild1@gmail.com>
parents: 2438
diff changeset
28 pposix.umask(umask);
11e3d16a128f mod_posix: Set umask to 'umask' from the config, or 027
Matthew Wild <mwild1@gmail.com>
parents: 2438
diff changeset
29
1680
f3d241915429 Add setuid and setgid support.
Tobias Markmann <tm@ayena.de>
parents: 1579
diff changeset
30 -- 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
31 module:hook("server-started", function ()
1712
45a81d6d8777 Merge waqas with Tobias. Eww.
Matthew Wild <mwild1@gmail.com>
parents: 1682 1691
diff changeset
32 local uid = module:get_option("setuid");
45a81d6d8777 Merge waqas with Tobias. Eww.
Matthew Wild <mwild1@gmail.com>
parents: 1682 1691
diff changeset
33 local gid = module:get_option("setgid");
1680
f3d241915429 Add setuid and setgid support.
Tobias Markmann <tm@ayena.de>
parents: 1579
diff changeset
34 if gid then
1682
883cf1f516a0 Shutdown prosody if changing user or group fails.
Tobias Markmann <tm@ayena.de>
parents: 1681
diff changeset
35 local success, msg = pposix.setgid(gid);
883cf1f516a0 Shutdown prosody if changing user or group fails.
Tobias Markmann <tm@ayena.de>
parents: 1681
diff changeset
36 if success then
883cf1f516a0 Shutdown prosody if changing user or group fails.
Tobias Markmann <tm@ayena.de>
parents: 1681
diff changeset
37 module:log("debug", "Changed group to "..gid.." successfully.");
883cf1f516a0 Shutdown prosody if changing user or group fails.
Tobias Markmann <tm@ayena.de>
parents: 1681
diff changeset
38 else
883cf1f516a0 Shutdown prosody if changing user or group fails.
Tobias Markmann <tm@ayena.de>
parents: 1681
diff changeset
39 module:log("error", "Failed to change group to "..gid..". Error: "..msg);
883cf1f516a0 Shutdown prosody if changing user or group fails.
Tobias Markmann <tm@ayena.de>
parents: 1681
diff changeset
40 prosody.shutdown("Failed to change group to "..gid);
883cf1f516a0 Shutdown prosody if changing user or group fails.
Tobias Markmann <tm@ayena.de>
parents: 1681
diff changeset
41 end
1680
f3d241915429 Add setuid and setgid support.
Tobias Markmann <tm@ayena.de>
parents: 1579
diff changeset
42 end
f3d241915429 Add setuid and setgid support.
Tobias Markmann <tm@ayena.de>
parents: 1579
diff changeset
43 if uid then
1682
883cf1f516a0 Shutdown prosody if changing user or group fails.
Tobias Markmann <tm@ayena.de>
parents: 1681
diff changeset
44 local success, msg = pposix.setuid(uid);
883cf1f516a0 Shutdown prosody if changing user or group fails.
Tobias Markmann <tm@ayena.de>
parents: 1681
diff changeset
45 if success then
883cf1f516a0 Shutdown prosody if changing user or group fails.
Tobias Markmann <tm@ayena.de>
parents: 1681
diff changeset
46 module:log("debug", "Changed user to "..uid.." successfully.");
883cf1f516a0 Shutdown prosody if changing user or group fails.
Tobias Markmann <tm@ayena.de>
parents: 1681
diff changeset
47 else
883cf1f516a0 Shutdown prosody if changing user or group fails.
Tobias Markmann <tm@ayena.de>
parents: 1681
diff changeset
48 module:log("error", "Failed to change user to "..uid..". Error: "..msg);
883cf1f516a0 Shutdown prosody if changing user or group fails.
Tobias Markmann <tm@ayena.de>
parents: 1681
diff changeset
49 prosody.shutdown("Failed to change user to "..uid);
883cf1f516a0 Shutdown prosody if changing user or group fails.
Tobias Markmann <tm@ayena.de>
parents: 1681
diff changeset
50 end
1680
f3d241915429 Add setuid and setgid support.
Tobias Markmann <tm@ayena.de>
parents: 1579
diff changeset
51 end
f3d241915429 Add setuid and setgid support.
Tobias Markmann <tm@ayena.de>
parents: 1579
diff changeset
52 end);
f3d241915429 Add setuid and setgid support.
Tobias Markmann <tm@ayena.de>
parents: 1579
diff changeset
53
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
54 -- 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
55 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
56 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
57 if not suid or suid == 0 or suid == "root" then
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 pposix.getuid() == 0 and not module:get_option("run_as_root") then
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 module:log("error", "Danger, Will Robinson! Prosody doesn't need to be run as root, so don't do it!");
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 module:log("error", "For more information on running Prosody as root, see http://prosody.im/doc/root");
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
61 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
62 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
63 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
64 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
65
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
66 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
67 local pidfile_handle;
1032
409f22d0430f mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents: 991
diff changeset
68
409f22d0430f mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents: 991
diff changeset
69 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
70 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
71 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
72 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
73 pidfile, pidfile_handle = nil, nil;
1032
409f22d0430f mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents: 991
diff changeset
74 end
409f22d0430f mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents: 991
diff changeset
75 end
409f22d0430f mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents: 991
diff changeset
76
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
77 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
78 if pidfile_handle then
1032
409f22d0430f mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents: 991
diff changeset
79 remove_pidfile();
409f22d0430f mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents: 991
diff changeset
80 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
81 pidfile = module:get_option("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
82 if pidfile then
3026
dec4527a7499 mod_posix: Fixed a global access.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
83 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
84 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
85 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
86 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
87 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
88 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
89 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
90 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
91 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
92 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
93 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
94 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
95 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
96 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
97 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
98 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
99 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
100 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
101 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
102 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
103 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
104 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
105 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
106 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
107 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
108 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
109 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
110 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
111
3540
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3537
diff changeset
112 local syslog_opened;
1033
4a9f0d482028 mod_posix: Integrate with loggingmanager, register syslog sink, remove redundant logging code
Matthew Wild <mwild1@gmail.com>
parents: 1032
diff changeset
113 function syslog_sink_maker(config)
4a9f0d482028 mod_posix: Integrate with loggingmanager, register syslog sink, remove redundant logging code
Matthew Wild <mwild1@gmail.com>
parents: 1032
diff changeset
114 if not syslog_opened then
4a9f0d482028 mod_posix: Integrate with loggingmanager, register syslog sink, remove redundant logging code
Matthew Wild <mwild1@gmail.com>
parents: 1032
diff changeset
115 pposix.syslog_open("prosody");
4a9f0d482028 mod_posix: Integrate with loggingmanager, register syslog sink, remove redundant logging code
Matthew Wild <mwild1@gmail.com>
parents: 1032
diff changeset
116 syslog_opened = true;
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
117 end
1033
4a9f0d482028 mod_posix: Integrate with loggingmanager, register syslog sink, remove redundant logging code
Matthew Wild <mwild1@gmail.com>
parents: 1032
diff changeset
118 local syslog, format = pposix.syslog_log, string.format;
4a9f0d482028 mod_posix: Integrate with loggingmanager, register syslog sink, remove redundant logging code
Matthew Wild <mwild1@gmail.com>
parents: 1032
diff changeset
119 return function (name, level, message, ...)
3540
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3537
diff changeset
120 if ... then
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3537
diff changeset
121 syslog(level, format(message, ...));
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3537
diff changeset
122 else
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3537
diff changeset
123 syslog(level, message);
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3537
diff changeset
124 end
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3537
diff changeset
125 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
126 end
1033
4a9f0d482028 mod_posix: Integrate with loggingmanager, register syslog sink, remove redundant logging code
Matthew Wild <mwild1@gmail.com>
parents: 1032
diff changeset
127 require "core.loggingmanager".register_sink_type("syslog", syslog_sink_maker);
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
128
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
129 local daemonize = module:get_option("daemonize");
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
130 if daemonize == nil then
2074
c59c8f3ec645 mod_posix: Log warning when no_daemonize is used, and instruct on how to update config
Matthew Wild <mwild1@gmail.com>
parents: 2073
diff changeset
131 local no_daemonize = module:get_option("no_daemonize"); --COMPAT w/ 0.5
c59c8f3ec645 mod_posix: Log warning when no_daemonize is used, and instruct on how to update config
Matthew Wild <mwild1@gmail.com>
parents: 2073
diff changeset
132 daemonize = not no_daemonize;
c59c8f3ec645 mod_posix: Log warning when no_daemonize is used, and instruct on how to update config
Matthew Wild <mwild1@gmail.com>
parents: 2073
diff changeset
133 if no_daemonize ~= nil then
c59c8f3ec645 mod_posix: Log warning when no_daemonize is used, and instruct on how to update config
Matthew Wild <mwild1@gmail.com>
parents: 2073
diff changeset
134 module:log("warn", "The 'no_daemonize' option is now replaced by 'daemonize'");
c59c8f3ec645 mod_posix: Log warning when no_daemonize is used, and instruct on how to update config
Matthew Wild <mwild1@gmail.com>
parents: 2073
diff changeset
135 module:log("warn", "Update your config from 'no_daemonize = %s' to 'daemonize = %s'", tostring(no_daemonize), tostring(daemonize));
c59c8f3ec645 mod_posix: Log warning when no_daemonize is used, and instruct on how to update config
Matthew Wild <mwild1@gmail.com>
parents: 2073
diff changeset
136 end
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
137 end
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
138
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
139 if daemonize then
587
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 local function daemonize_server()
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 local ok, ret = pposix.daemonize();
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 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
143 module:log("error", "Failed to daemonize: %s", ret);
587
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 elseif ret and ret > 0 then
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 os.exit(0);
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 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
147 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
148 write_pidfile();
587
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 end
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 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
151 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
152 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
153 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
154 else
1032
409f22d0430f mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents: 991
diff changeset
155 -- 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
156 write_pidfile();
587
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 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
158
3537
7bbb19804d82 mod_posix: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3481
diff changeset
159 module:hook("server-stopped", remove_pidfile);
1032
409f22d0430f mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents: 991
diff changeset
160
1118
239d4362a040 mod_posix: Reload the config and reopen log files on SIGHUP
Matthew Wild <mwild1@gmail.com>
parents: 1092
diff changeset
161 -- Set signal handlers
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
162 if signal.signal then
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
163 signal.signal("SIGTERM", function ()
1118
239d4362a040 mod_posix: Reload the config and reopen log files on SIGHUP
Matthew Wild <mwild1@gmail.com>
parents: 1092
diff changeset
164 module:log("warn", "Received SIGTERM");
1238
f4c08caca3e7 mod_posix: Use global prosody object
Matthew Wild <mwild1@gmail.com>
parents: 1119
diff changeset
165 prosody.unlock_globals();
f4c08caca3e7 mod_posix: Use global prosody object
Matthew Wild <mwild1@gmail.com>
parents: 1119
diff changeset
166 prosody.shutdown("Received SIGTERM");
f4c08caca3e7 mod_posix: Use global prosody object
Matthew Wild <mwild1@gmail.com>
parents: 1119
diff changeset
167 prosody.lock_globals();
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
168 end);
1118
239d4362a040 mod_posix: Reload the config and reopen log files on SIGHUP
Matthew Wild <mwild1@gmail.com>
parents: 1092
diff changeset
169
239d4362a040 mod_posix: Reload the config and reopen log files on SIGHUP
Matthew Wild <mwild1@gmail.com>
parents: 1092
diff changeset
170 signal.signal("SIGHUP", function ()
239d4362a040 mod_posix: Reload the config and reopen log files on SIGHUP
Matthew Wild <mwild1@gmail.com>
parents: 1092
diff changeset
171 module:log("info", "Received SIGHUP");
1238
f4c08caca3e7 mod_posix: Use global prosody object
Matthew Wild <mwild1@gmail.com>
parents: 1119
diff changeset
172 prosody.reload_config();
f4c08caca3e7 mod_posix: Use global prosody object
Matthew Wild <mwild1@gmail.com>
parents: 1119
diff changeset
173 prosody.reopen_logfiles();
1118
239d4362a040 mod_posix: Reload the config and reopen log files on SIGHUP
Matthew Wild <mwild1@gmail.com>
parents: 1092
diff changeset
174 end);
2332
7772dde4010b mod_posix: Catch SIGINT
Matthew Wild <mwild1@gmail.com>
parents: 2074
diff changeset
175
7772dde4010b mod_posix: Catch SIGINT
Matthew Wild <mwild1@gmail.com>
parents: 2074
diff changeset
176 signal.signal("SIGINT", function ()
7772dde4010b mod_posix: Catch SIGINT
Matthew Wild <mwild1@gmail.com>
parents: 2074
diff changeset
177 module:log("info", "Received SIGINT");
7772dde4010b mod_posix: Catch SIGINT
Matthew Wild <mwild1@gmail.com>
parents: 2074
diff changeset
178 prosody.unlock_globals();
7772dde4010b mod_posix: Catch SIGINT
Matthew Wild <mwild1@gmail.com>
parents: 2074
diff changeset
179 prosody.shutdown("Received SIGINT");
7772dde4010b mod_posix: Catch SIGINT
Matthew Wild <mwild1@gmail.com>
parents: 2074
diff changeset
180 prosody.lock_globals();
7772dde4010b mod_posix: Catch SIGINT
Matthew Wild <mwild1@gmail.com>
parents: 2074
diff changeset
181 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
182 end

mercurial