plugins/mod_posix.lua

Tue, 31 Mar 2009 20:15:33 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Tue, 31 Mar 2009 20:15:33 +0100
changeset 948
4aff205cc4cd
parent 735
d247d061409a
child 991
cd0d75de8345
permissions
-rw-r--r--

Tagging VERSION

728
fa45dfb27ee5 mod_posix: Check version of pposix
Matthew Wild <mwild1@gmail.com>
parents: 723
diff changeset
1
734
cfb4ec5cba5e Fix for pposix version detection
Matthew Wild <mwild1@gmail.com>
parents: 728
diff changeset
2 local want_pposix_version = "0.3.0";
587
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local pposix = assert(require "util.pposix");
735
d247d061409a mod_posix: logging fix
Matthew Wild <mwild1@gmail.com>
parents: 734
diff changeset
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
587
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local config_get = require "core.configmanager".get;
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local logger_set = require "util.logger".setwriter;
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 module.host = "*"; -- we're a global module
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 if not config_get("*", "core", "no_daemonize") then
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local function daemonize_server()
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local logwriter;
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local logfilename = config_get("*", "core", "log");
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 594
diff changeset
17 if logfilename == "syslog" then
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 594
diff changeset
18 pposix.syslog_open("prosody");
728
fa45dfb27ee5 mod_posix: Check version of pposix
Matthew Wild <mwild1@gmail.com>
parents: 723
diff changeset
19 pposix.syslog_setminlevel(config.get("*", "core", "minimum_log_level") or "info");
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 594
diff changeset
20 local syslog, format = pposix.syslog_log, string.format;
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 594
diff changeset
21 logwriter = function (name, level, message, ...)
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 594
diff changeset
22 if ... then
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 594
diff changeset
23 syslog(level, format(message, ...));
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 594
diff changeset
24 else
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 594
diff changeset
25 syslog(level, message);
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 594
diff changeset
26 end
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 594
diff changeset
27 end;
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 594
diff changeset
28 elseif logfilename then
587
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 local logfile = io.open(logfilename, "a+");
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 if logfile then
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 local write, format, flush = logfile.write, string.format, logfile.flush;
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 logwriter = function (name, level, message, ...)
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 if ... then
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 write(logfile, name, "\t", level, "\t", format(message, ...), "\n");
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 else
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 write(logfile, name, "\t" , level, "\t", message, "\n");
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 end
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 flush(logfile);
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 end;
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 end
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 else
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 log("debug", "No logging specified, will continue with default");
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 local ok, ret = pposix.daemonize();
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 if not ok then
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 log("error", "Failed to daemonize: %s", ret);
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 elseif ret and ret > 0 then
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 os.exit(0);
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 else
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 if logwriter then
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 local ok, ret = logger_set(logwriter);
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 if not ok then
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 log("error", "Couldn't set new log output: %s", ret);
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 end
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 end
723
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
57 log("info", "Successfully daemonized to PID %d", pposix.getpid());
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
58
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
59 local pidfile = config.get("*", "core", "pidfile");
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
60 if pidfile then
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
61 local pf, err = io.open(pidfile, "w+");
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
62 if not pf then
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
63 log("error", "Couldn't write pidfile; %s", err);
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
64 else
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
65 pf:write(tostring(pposix.getpid()));
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
66 pf:close();
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
67 end
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
68 end
587
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 end
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 end
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 module:add_event_hook("server-starting", daemonize_server);
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 end

mercurial