mod_posix: Remove pidfile on exit

Wed, 22 Apr 2009 19:59:58 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Wed, 22 Apr 2009 19:59:58 +0100
changeset 1032
409f22d0430f
parent 1031
ec013f93de81
child 1033
4a9f0d482028

mod_posix: Remove pidfile on exit

plugins/mod_posix.lua file | annotate | diff | comparison | revisions
--- a/plugins/mod_posix.lua	Wed Apr 22 19:49:58 2009 +0100
+++ b/plugins/mod_posix.lua	Wed Apr 22 19:59:58 2009 +0100
@@ -14,7 +14,19 @@
 
 module.host = "*"; -- we're a global module
 
+local pidfile_written;
+
+local function remove_pidfile()
+	if pidfile_written then
+		os.remove(pidfile);
+		pidfile_written = nil;
+	end
+end
+
 local function write_pidfile()
+	if pidfile_written then
+		remove_pidfile();
+	end
 	local pidfile = config.get("*", "core", "pidfile");
 	if pidfile then
 		local pf, err = io.open(pidfile, "w+");
@@ -23,6 +35,7 @@
 		else
 			pf:write(tostring(pposix.getpid()));
 			pf:close();
+			pidfile_written = pidfile;
 		end
 	end
 end
@@ -80,10 +93,12 @@
 	end
 	module:add_event_hook("server-starting", daemonize_server);
 else
+	-- Not going to daemonize, so write the pid of this process
 	write_pidfile();
-	-- Not going to daemonize, but let's write the pidfile anyway
 end
 
+module:add_event_hook("server-stopped", remove_pidfile);
+
 -- Set signal handler
 if signal.signal then
 	signal.signal("SIGTERM", function ()

mercurial