plugins/mod_posix.lua

changeset 1032
409f22d0430f
parent 991
cd0d75de8345
child 1033
4a9f0d482028
equal deleted inserted replaced
1031:ec013f93de81 1032:409f22d0430f
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
15 module.host = "*"; -- we're a global module 15 module.host = "*"; -- we're a global module
16 16
17 local pidfile_written;
18
19 local function remove_pidfile()
20 if pidfile_written then
21 os.remove(pidfile);
22 pidfile_written = nil;
23 end
24 end
25
17 local function write_pidfile() 26 local function write_pidfile()
27 if pidfile_written then
28 remove_pidfile();
29 end
18 local pidfile = config.get("*", "core", "pidfile"); 30 local pidfile = config.get("*", "core", "pidfile");
19 if pidfile then 31 if pidfile then
20 local pf, err = io.open(pidfile, "w+"); 32 local pf, err = io.open(pidfile, "w+");
21 if not pf then 33 if not pf then
22 log("error", "Couldn't write pidfile; %s", err); 34 log("error", "Couldn't write pidfile; %s", err);
23 else 35 else
24 pf:write(tostring(pposix.getpid())); 36 pf:write(tostring(pposix.getpid()));
25 pf:close(); 37 pf:close();
38 pidfile_written = pidfile;
26 end 39 end
27 end 40 end
28 end 41 end
29 42
30 local logfilename = config_get("*", "core", "log"); 43 local logfilename = config_get("*", "core", "log");
78 write_pidfile(); 91 write_pidfile();
79 end 92 end
80 end 93 end
81 module:add_event_hook("server-starting", daemonize_server); 94 module:add_event_hook("server-starting", daemonize_server);
82 else 95 else
96 -- Not going to daemonize, so write the pid of this process
83 write_pidfile(); 97 write_pidfile();
84 -- Not going to daemonize, but let's write the pidfile anyway
85 end 98 end
99
100 module:add_event_hook("server-stopped", remove_pidfile);
86 101
87 -- Set signal handler 102 -- Set signal handler
88 if signal.signal then 103 if signal.signal then
89 signal.signal("SIGTERM", function () 104 signal.signal("SIGTERM", function ()
90 log("warn", "Received SIGTERM..."); 105 log("warn", "Received SIGTERM...");

mercurial