Comparison

plugins/mod_posix.lua @ 1042:a3d77353c18a

mod_*: Fix a load of global accesses
author Matthew Wild <mwild1@gmail.com>
date Wed, 22 Apr 2009 21:32:23 +0100
parent 1033:4a9f0d482028
child 1050:9552c08241af
comparison
equal deleted inserted replaced
1041:07835534d996 1042:a3d77353c18a
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")); 7 local signal = select(2, pcall(require, "util.signal"));
8 if type(signal) == "string" then 8 if type(signal) == "string" then
9 log("warn", "Couldn't load signal library, won't respond to SIGTERM"); 9 module:log("warn", "Couldn't load signal library, won't respond to SIGTERM");
10 end 10 end
11 11
12 local config_get = require "core.configmanager".get; 12 local config_get = require "core.configmanager".get;
13 local logger_set = require "util.logger".setwriter; 13 local logger_set = require "util.logger".setwriter;
14 14
16 16
17 local pidfile_written; 17 local pidfile_written;
18 18
19 local function remove_pidfile() 19 local function remove_pidfile()
20 if pidfile_written then 20 if pidfile_written then
21 os.remove(pidfile); 21 os.remove(pidfile_written);
22 pidfile_written = nil; 22 pidfile_written = nil;
23 end 23 end
24 end 24 end
25 25
26 local function write_pidfile() 26 local function write_pidfile()
27 if pidfile_written then 27 if pidfile_written then
28 remove_pidfile(); 28 remove_pidfile();
29 end 29 end
30 local pidfile = config.get("*", "core", "pidfile"); 30 local pidfile = config_get("*", "core", "pidfile");
31 if pidfile then 31 if pidfile then
32 local pf, err = io.open(pidfile, "w+"); 32 local pf, err = io.open(pidfile, "w+");
33 if not pf then 33 if not pf then
34 log("error", "Couldn't write pidfile; %s", err); 34 module:log("error", "Couldn't write pidfile; %s", err);
35 else 35 else
36 pf:write(tostring(pposix.getpid())); 36 pf:write(tostring(pposix.getpid()));
37 pf:close(); 37 pf:close();
38 pidfile_written = pidfile; 38 pidfile_written = pidfile;
39 end 39 end
60 60
61 if not config_get("*", "core", "no_daemonize") then 61 if not config_get("*", "core", "no_daemonize") then
62 local function daemonize_server() 62 local function daemonize_server()
63 local ok, ret = pposix.daemonize(); 63 local ok, ret = pposix.daemonize();
64 if not ok then 64 if not ok then
65 log("error", "Failed to daemonize: %s", ret); 65 module:log("error", "Failed to daemonize: %s", ret);
66 elseif ret and ret > 0 then 66 elseif ret and ret > 0 then
67 os.exit(0); 67 os.exit(0);
68 else 68 else
69 log("info", "Successfully daemonized to PID %d", pposix.getpid()); 69 module:log("info", "Successfully daemonized to PID %d", pposix.getpid());
70 write_pidfile(); 70 write_pidfile();
71 end 71 end
72 end 72 end
73 module:add_event_hook("server-starting", daemonize_server); 73 module:add_event_hook("server-starting", daemonize_server);
74 else 74 else
79 module:add_event_hook("server-stopped", remove_pidfile); 79 module:add_event_hook("server-stopped", remove_pidfile);
80 80
81 -- Set signal handler 81 -- Set signal handler
82 if signal.signal then 82 if signal.signal then
83 signal.signal("SIGTERM", function () 83 signal.signal("SIGTERM", function ()
84 log("warn", "Received SIGTERM..."); 84 module:log("warn", "Received SIGTERM...");
85 unlock_globals(); 85 _G.unlock_globals();
86 if prosody_shutdown then 86 if _G.prosody_shutdown then
87 prosody_shutdown("Received SIGTERM"); 87 _G.prosody_shutdown("Received SIGTERM");
88 else 88 else
89 log("warn", "...no prosody_shutdown(), ignoring."); 89 module:log("warn", "...no prosody_shutdown(), ignoring.");
90 end 90 end
91 lock_globals(); 91 _G.lock_globals();
92 end); 92 end);
93 end 93 end