From 9ea0cb4353753d1b27b2a4b6d7a8319bc8a4cc57 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Tue, 12 Mar 2013 14:06:59 +0100 Subject: [PATCH] Truncate the PID file before writing the new PID. --- lib/base/application.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/base/application.cpp b/lib/base/application.cpp index 2ee689ec6..cfbc226ab 100644 --- a/lib/base/application.cpp +++ b/lib/base/application.cpp @@ -462,9 +462,15 @@ void Application::UpdatePidFile(const String& filename) if (m_PidFile != NULL) fclose(m_PidFile); +#ifndef _WIN32 + char *mode = "r+"; +#else /* _WIN32 */ + char *mode = "w"; +#endif /* _WIN32 */ + /* There's just no sane way of getting a file descriptor for a * C++ ofstream which is why we're using FILEs here. */ - m_PidFile = fopen(filename.CStr(), "r+"); + m_PidFile = fopen(filename.CStr(), mode); if (m_PidFile == NULL) BOOST_THROW_EXCEPTION(runtime_error("Could not open PID file '" + filename + "'")); @@ -479,6 +485,8 @@ void Application::UpdatePidFile(const String& filename) _exit(EXIT_FAILURE); } + + (void) ftruncate(fd, 0); #endif /* _WIN32 */ fprintf(m_PidFile, "%d", Utility::GetPid());