717 pidfile = config.resolve_relative_path(prosody.paths.data, pidfile); |
717 pidfile = config.resolve_relative_path(prosody.paths.data, pidfile); |
718 local mode = stat(pidfile) and "r+" or "w+"; |
718 local mode = stat(pidfile) and "r+" or "w+"; |
719 local pidfile_handle, err = io.open(pidfile, mode); |
719 local pidfile_handle, err = io.open(pidfile, mode); |
720 if not pidfile_handle then |
720 if not pidfile_handle then |
721 log("error", "Couldn't write pidfile at %s; %s", pidfile, err); |
721 log("error", "Couldn't write pidfile at %s; %s", pidfile, err); |
722 prosody.shutdown("Couldn't write pidfile", 1); |
722 os.exit(1); |
723 else |
723 else |
724 prosody.pidfile = pidfile; |
724 prosody.pidfile = pidfile; |
725 if not lfs.lock(pidfile_handle, "w") then -- Exclusive lock |
725 if not lfs.lock(pidfile_handle, "w") then -- Exclusive lock |
726 local other_pid = pidfile_handle:read("*a"); |
726 local other_pid = pidfile_handle:read("*a"); |
727 log("error", "Another Prosody instance seems to be running with PID %s, quitting", other_pid); |
727 log("error", "Another Prosody instance seems to be running with PID %s, quitting", other_pid); |
728 prosody.pidfile_handle = nil; |
728 prosody.pidfile_handle = nil; |
729 prosody.shutdown("Prosody already running", 1); |
729 os.exit(1); |
730 else |
730 else |
731 pidfile_handle:close(); |
731 pidfile_handle:close(); |
732 pidfile_handle, err = io.open(pidfile, "w+"); |
732 pidfile_handle, err = io.open(pidfile, "w+"); |
733 if not pidfile_handle then |
733 if not pidfile_handle then |
734 log("error", "Couldn't write pidfile at %s; %s", pidfile, err); |
734 log("error", "Couldn't write pidfile at %s; %s", pidfile, err); |
735 prosody.shutdown("Couldn't write pidfile", 1); |
735 os.exit(1); |
736 else |
736 else |
737 if lfs.lock(pidfile_handle, "w") then |
737 if lfs.lock(pidfile_handle, "w") then |
738 pidfile_handle:write(tostring(pposix.getpid())); |
738 pidfile_handle:write(tostring(pposix.getpid())); |
739 pidfile_handle:flush(); |
739 pidfile_handle:flush(); |
740 prosody.pidfile_handle = pidfile_handle; |
740 prosody.pidfile_handle = pidfile_handle; |