+ end
+ end);
+
+-- Don't even think about it!
+module:add_event_hook("server-starting", function ()
+ local suid = module:get_option("setuid");
+ if not suid or suid == 0 or suid == "root" then
+ if pposix.getuid() == 0 and not module:get_option("run_as_root") then
+ module:log("error", "Danger, Will Robinson! Prosody doesn't need to be run as root, so don't do it!");
+ module:log("error", "For more information on running Prosody as root, see http://prosody.im/doc/root");
+ prosody.shutdown("Refusing to run as root");
+ end
+ end
+ end);
+
+local pidfile_written;
+
+local function remove_pidfile()
+ if pidfile_written then
+ os.remove(pidfile_written);
+ pidfile_written = nil;
+ end
+end
+
+local function write_pidfile()
+ if pidfile_written then
+ remove_pidfile();
+ end
+ local pidfile = module:get_option("pidfile");
+ if pidfile then
+ local pf, err = io.open(pidfile, "w+");
+ if not pf then
+ module:log("error", "Couldn't write pidfile; %s", err);