MUC: No need to call is_admin twice now, global admins are admins on hosts
[prosody.git] / plugins / mod_posix.lua
index 5e9d25442aebb4012ea9b20a180546382185ed0f..77b2f2a4ee25ae16d23d7198e834be09f7b13e10 100644 (file)
@@ -82,6 +82,7 @@ local function write_pidfile()
        end
        pidfile = module:get_option("pidfile");
        if pidfile then
+               local err;
                local mode = stat(pidfile) and "r+" or "w+";
                pidfile_handle, err = io.open(pidfile, mode);
                if not pidfile_handle then
@@ -94,8 +95,17 @@ local function write_pidfile()
                                pidfile_handle = nil;
                                prosody.shutdown("Prosody already running");
                        else
-                               pidfile_handle:write(tostring(pposix.getpid()));
-                               pidfile_handle:flush();
+                               pidfile_handle:close();
+                               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");
+                               else
+                                       if lfs.lock(pidfile_handle, "w") then
+                                               pidfile_handle:write(tostring(pposix.getpid()));
+                                               pidfile_handle:flush();
+                                       end
+                               end
                        end
                end
        end