plugins/mod_posix.lua

Thu, 15 Jan 2009 20:59:36 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 15 Jan 2009 20:59:36 +0000
changeset 723
c1e7d280c174
parent 722
63456c9d0522
child 728
fa45dfb27ee5
permissions
-rw-r--r--

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.

587
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local pposix = assert(require "util.pposix");
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 config_get = require "core.configmanager".get;
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local logger_set = require "util.logger".setwriter;
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 module.host = "*"; -- we're a global module
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 if not config_get("*", "core", "no_daemonize") then
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local function daemonize_server()
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local logwriter;
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 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
14 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
15 pposix.syslog_open("prosody");
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 594
diff changeset
16 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
17 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
18 if ... then
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 594
diff changeset
19 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
20 else
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 594
diff changeset
21 syslog(level, message);
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 594
diff changeset
22 end
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 594
diff changeset
23 end;
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 594
diff changeset
24 elseif logfilename then
587
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 local logfile = io.open(logfilename, "a+");
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 if logfile then
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local write, format, flush = logfile.write, string.format, logfile.flush;
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 logwriter = function (name, level, message, ...)
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 if ... then
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 write(logfile, name, "\t", level, "\t", format(message, ...), "\n");
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 else
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 write(logfile, name, "\t" , level, "\t", message, "\n");
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 end
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 flush(logfile);
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 end;
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 end
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 else
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 log("debug", "No logging specified, will continue with default");
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
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 local ok, ret = pposix.daemonize();
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 if not ok then
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 log("error", "Failed to daemonize: %s", ret);
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 elseif ret and ret > 0 then
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 os.exit(0);
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 else
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 if logwriter then
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 local ok, ret = logger_set(logwriter);
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 if not ok then
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 log("error", "Couldn't set new log output: %s", ret);
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 end
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 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
53 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
54
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
55 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
56 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
57 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
58 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
59 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
60 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
61 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
62 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
63 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
64 end
587
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 end
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 end
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 module:add_event_hook("server-starting", daemonize_server);
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 end

mercurial