mod_posix: Truncate the pidfile before writing to ensure that we never overwrite with a PID shorter than the previous, and end with an invalid PID in the file.

Fri, 09 Jul 2010 01:16:09 +0100

author
Brian Cully <bjc@junctionnetworks.com>
date
Fri, 09 Jul 2010 01:16:09 +0100
changeset 3340
0769cc5f34b6
parent 3339
7893055e54d1
child 3341
a8a3e662fea7

mod_posix: Truncate the pidfile before writing to ensure that we never overwrite with a PID shorter than the previous, and end with an invalid PID in the file.

plugins/mod_posix.lua file | annotate | diff | comparison | revisions
--- a/plugins/mod_posix.lua	Fri Jul 09 01:09:57 2010 +0100
+++ b/plugins/mod_posix.lua	Fri Jul 09 01:16:09 2010 +0100
@@ -95,6 +95,11 @@
 				pidfile_handle = nil;
 				prosody.shutdown("Prosody already running");
 			else
+				pidfile_handle, err = io.open(pidfile, "w+");
+				if not pidfile_handle then
+					module:log("error", "Couldn't write pidfile at %s; %s", pidfile, err);
+					prosody.shutdown("Couldn't write pidfile");
+				end
 				pidfile_handle:write(tostring(pposix.getpid()));
 				pidfile_handle:flush();
 			end

mercurial