< Previous by Date Date Index Next by Date >
  Thread Index  

[reSIProcate-users] Patch for ServerProcess.cxx


To support Windows, tested on Windows 7 32bit and 64bit.

Best regards,
@@ -25,11 +25,21 @@ using namespace resip;
 using namespace std;
 
 ServerProcess::ServerProcess() : mPidFile("")
+#ifdef _WIN32
+                                                               
,mProcessMutex(NULL)
+#endif // _WIN32
+
 {
 }
 
 ServerProcess::~ServerProcess()
 {
+#ifdef _WIN32
+       if (mProcessMutex)
+       {
+               CloseHandle(mProcessMutex);
+       }
+#endif
 }
 
 void
@@ -132,58 +142,75 @@ ServerProcess::dropPrivileges(const Data& runAsUser, 
const Data& runAsGroup)
 }
 
 bool
-ServerProcess::isAlreadyRunning()
+ServerProcess::isAlreadyRunning(const Data & appName)
 {
-#ifndef __linux__
-   WarningLog(<<"can't check if process already running on this platform (not 
implemented yet)");
-   return false;
-#else
-   if(mPidFile.size() == 0)
-   {
-      // if no PID file specified, we do not make any check
-      return false;
-   }
+#ifdef __linux__
 
-   pid_t running_pid;
-   std::ifstream _pid(mPidFile.c_str(), std::ios_base::in);
-   if(!_pid.good())
-   {
-      // if the file doesn't exist or can't be opened, just ignore
-      return false;
-   }
-   _pid >> running_pid;
-   _pid.close();
+       if(mPidFile.size() == 0)
+       {
+               // if no PID file specified, we do not make any check
+               return false;
+       }
 
-   StackLog(<< mPidFile << " contains PID " << running_pid);
+       pid_t running_pid;
+       std::ifstream _pid(mPidFile.c_str(), std::ios_base::in);
+       if(!_pid.good())
+       {
+               // if the file doesn't exist or can't be opened, just ignore
+               return false;
+       }
+       _pid >> running_pid;
+       _pid.close();
 
-   Data ourProc = Data("/proc/self/exe");
-   Data otherProc = Data("/proc/") + Data(running_pid) + Data("/exe");
-   char our_exe[513], other_exe[513];
-   int buf_size;
+       StackLog(<< mPidFile << " contains PID " << running_pid);
 
-   buf_size = readlink(ourProc.c_str(), our_exe, 512);
-   if(buf_size < 0 || buf_size == 512)
-   {
-      // if readlink fails, just ignore
-      return false;
-   }
-   our_exe[buf_size] = 0;
+       Data ourProc = Data("/proc/self/exe");
+       Data otherProc = Data("/proc/") + Data(running_pid) + Data("/exe");
+       char our_exe[513], other_exe[513];
+       int buf_size;
 
-   buf_size = readlink(otherProc.c_str(), other_exe, 512);
-   if(buf_size < 0 || buf_size == 512)
-   {
-      // if readlink fails, just ignore
-      return false;
-   }
-   other_exe[buf_size] = 0;
+       buf_size = readlink(ourProc.c_str(), our_exe, 512);
+       if(buf_size < 0 || buf_size == 512)
+       {
+               // if readlink fails, just ignore
+               return false;
+       }
+       our_exe[buf_size] = 0;
+
+       buf_size = readlink(otherProc.c_str(), other_exe, 512);
+       if(buf_size < 0 || buf_size == 512)
+       {
+               // if readlink fails, just ignore
+               return false;
+       }
+       other_exe[buf_size] = 0;
+
+       if(strcmp(our_exe, other_exe) == 0)
+       {
+               ErrLog(<<"already running PID: " << running_pid);
+               return true;
+       }
+       return false;
+
+#elif _WIN32
+
+       if (appName.size() == 0)
+       {
+               return false;
+       }
+
+       mProcessMutex = CreateMutex(NULL, FALSE, appName.c_str());
+       if (!mProcessMutex)
+       {
+               if (GetLastError() == ERROR_ALREADY_EXISTS)
+               {
+                       return true;
+               }
+       }
 
-   if(strcmp(our_exe, other_exe) == 0)
-   {
-      ErrLog(<<"already running PID: " << running_pid);
-      return true;
-   }
-   return false;
 #endif
+
+       return false;
 }
 
 void
@@ -5,6 +5,10 @@
 
 #include "rutil/Data.hxx"
 
+#ifdef _WIN32
+#include <winbase.h>
+#endif
+
 namespace resip
 {
 
@@ -21,7 +25,7 @@ protected:
 
    /* If the PID file is specified, checks if we are already running
       an instance of this binary */
-   bool isAlreadyRunning();
+   bool isAlreadyRunning(const Data & appName);
 
    /* The main subclass can call daemonize() if and
       when it wants to become a daemon */
@@ -32,6 +36,12 @@ protected:
 
 private:
    Data mPidFile;
+
+#ifdef _WIN32
+   HANDLE mProcessMutex;
+#endif // _WIN32
+
+
 };
 
 }