Comparison

plugins/mod_posix.lua @ 991:cd0d75de8345

mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
author Matthew Wild <mwild1@gmail.com>
date Sun, 12 Apr 2009 02:57:52 +0100
parent 735:d247d061409a
child 1032:409f22d0430f
comparison
equal deleted inserted replaced
990:235abebc896e 991:cd0d75de8345
2 local want_pposix_version = "0.3.0"; 2 local want_pposix_version = "0.3.0";
3 3
4 local pposix = assert(require "util.pposix"); 4 local pposix = assert(require "util.pposix");
5 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 5 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
6 6
7 local signal = select(2, pcall(require, "util.signal"));
8 if type(signal) == "string" then
9 log("warn", "Couldn't load signal library, won't respond to SIGTERM");
10 end
11
7 local config_get = require "core.configmanager".get; 12 local config_get = require "core.configmanager".get;
8 local logger_set = require "util.logger".setwriter; 13 local logger_set = require "util.logger".setwriter;
9 14
10 module.host = "*"; -- we're a global module 15 module.host = "*"; -- we're a global module
11 16
17 local function write_pidfile()
18 local pidfile = config.get("*", "core", "pidfile");
19 if pidfile then
20 local pf, err = io.open(pidfile, "w+");
21 if not pf then
22 log("error", "Couldn't write pidfile; %s", err);
23 else
24 pf:write(tostring(pposix.getpid()));
25 pf:close();
26 end
27 end
28 end
29
30 local logfilename = config_get("*", "core", "log");
31 if logfilename == "syslog" then
32 pposix.syslog_open("prosody");
33 pposix.syslog_setminlevel(config.get("*", "core", "minimum_log_level") or "info");
34 local syslog, format = pposix.syslog_log, string.format;
35 logwriter = function (name, level, message, ...)
36 if ... then
37 syslog(level, format(message, ...));
38 else
39 syslog(level, message);
40 end
41 end;
42 elseif logfilename then
43 local logfile = io.open(logfilename, "a+");
44 if logfile then
45 local write, format, flush = logfile.write, string.format, logfile.flush;
46 logwriter = function (name, level, message, ...)
47 if ... then
48 write(logfile, name, "\t", level, "\t", format(message, ...), "\n");
49 else
50 write(logfile, name, "\t" , level, "\t", message, "\n");
51 end
52 flush(logfile);
53 end;
54 end
55 else
56 log("debug", "No logging specified, will continue with default");
57 end
58
59 if logwriter then
60 local ok, ret = logger_set(logwriter);
61 if not ok then
62 log("error", "Couldn't set new log output: %s", ret);
63 end
64 end
65
12 if not config_get("*", "core", "no_daemonize") then 66 if not config_get("*", "core", "no_daemonize") then
13 local function daemonize_server() 67 local function daemonize_server()
14 local logwriter; 68 local logwriter;
15 69
16 local logfilename = config_get("*", "core", "log");
17 if logfilename == "syslog" then
18 pposix.syslog_open("prosody");
19 pposix.syslog_setminlevel(config.get("*", "core", "minimum_log_level") or "info");
20 local syslog, format = pposix.syslog_log, string.format;
21 logwriter = function (name, level, message, ...)
22 if ... then
23 syslog(level, format(message, ...));
24 else
25 syslog(level, message);
26 end
27 end;
28 elseif logfilename then
29 local logfile = io.open(logfilename, "a+");
30 if logfile then
31 local write, format, flush = logfile.write, string.format, logfile.flush;
32 logwriter = function (name, level, message, ...)
33 if ... then
34 write(logfile, name, "\t", level, "\t", format(message, ...), "\n");
35 else
36 write(logfile, name, "\t" , level, "\t", message, "\n");
37 end
38 flush(logfile);
39 end;
40 end
41 else
42 log("debug", "No logging specified, will continue with default");
43 end
44 70
45 local ok, ret = pposix.daemonize(); 71 local ok, ret = pposix.daemonize();
46 if not ok then 72 if not ok then
47 log("error", "Failed to daemonize: %s", ret); 73 log("error", "Failed to daemonize: %s", ret);
48 elseif ret and ret > 0 then 74 elseif ret and ret > 0 then
49 os.exit(0); 75 os.exit(0);
50 else 76 else
51 if logwriter then
52 local ok, ret = logger_set(logwriter);
53 if not ok then
54 log("error", "Couldn't set new log output: %s", ret);
55 end
56 end
57 log("info", "Successfully daemonized to PID %d", pposix.getpid()); 77 log("info", "Successfully daemonized to PID %d", pposix.getpid());
58 78 write_pidfile();
59 local pidfile = config.get("*", "core", "pidfile");
60 if pidfile then
61 local pf, err = io.open(pidfile, "w+");
62 if not pf then
63 log("error", "Couldn't write pidfile; %s", err);
64 else
65 pf:write(tostring(pposix.getpid()));
66 pf:close();
67 end
68 end
69 end 79 end
70 end 80 end
71 module:add_event_hook("server-starting", daemonize_server); 81 module:add_event_hook("server-starting", daemonize_server);
82 else
83 write_pidfile();
84 -- Not going to daemonize, but let's write the pidfile anyway
72 end 85 end
86
87 -- Set signal handler
88 if signal.signal then
89 signal.signal("SIGTERM", function ()
90 log("warn", "Received SIGTERM...");
91 unlock_globals();
92 if prosody_shutdown then
93 prosody_shutdown("Received SIGTERM");
94 else
95 log("warn", "...no prosody_shutdown(), ignoring.");
96 end
97 lock_globals();
98 end);
99 end