Make Application::ReadPidFile work on Windows.

Refs #5788
This commit is contained in:
Gunnar Beutner 2014-05-01 18:42:36 +02:00
parent 5e094ed185
commit 630a1a28e2
2 changed files with 10 additions and 5 deletions

View File

@ -435,7 +435,7 @@ int Main(void)
if (!g_AppParams.count("validate") && !g_AppParams.count("reload-internal")) { if (!g_AppParams.count("validate") && !g_AppParams.count("reload-internal")) {
pid_t runningpid = Application::ReadPidFile(Application::GetPidPath()); pid_t runningpid = Application::ReadPidFile(Application::GetPidPath());
if (runningpid >= 0) { if (runningpid > 0) {
Log(LogCritical, "icinga-app", "Another instance of Icinga already running with PID " + Convert::ToString(runningpid)); Log(LogCritical, "icinga-app", "Another instance of Icinga already running with PID " + Convert::ToString(runningpid));
return EXIT_FAILURE; return EXIT_FAILURE;
} }

View File

@ -682,14 +682,14 @@ void Application::ClosePidFile(void)
* Checks if another process currently owns the pidfile and read it * Checks if another process currently owns the pidfile and read it
* *
* @param filename The name of the PID file. * @param filename The name of the PID file.
* @returns -1: no process owning the pidfile, pid of the process otherwise * @returns 0: no process owning the pidfile, pid of the process otherwise
*/ */
pid_t Application::ReadPidFile(const String& filename) pid_t Application::ReadPidFile(const String& filename)
{ {
FILE *pidfile = fopen(filename.CStr(), "r"); FILE *pidfile = fopen(filename.CStr(), "r");
if (pidfile == NULL) if (pidfile == NULL)
return -1; return 0;
#ifndef _WIN32 #ifndef _WIN32
int fd = fileno(pidfile); int fd = fileno(pidfile);
@ -722,10 +722,15 @@ pid_t Application::ReadPidFile(const String& filename)
// bogus result? // bogus result?
if (res != 1) if (res != 1)
return -1; return 0;
#ifdef _WIN32 #ifdef _WIN32
// TODO: add check if the read pid is still running or not HANDLE hProcess = OpenProcess(0, FALSE, runningpid);
if (!hProcess)
return 0;
CloseHandle(hProcess);
#endif /* _WIN32 */ #endif /* _WIN32 */
return runningpid; return runningpid;