Revision: 6964
          http://playerstage.svn.sourceforge.net/playerstage/?rev=6964&view=rev
Author:   natepak
Date:     2008-08-20 21:28:24 +0000 (Wed, 20 Aug 2008)

Log Message:
-----------
Added patch 2003324 from John Hsu

Modified Paths:
--------------
    code/gazebo/trunk/libgazebo/Server.cc

Modified: code/gazebo/trunk/libgazebo/Server.cc
===================================================================
--- code/gazebo/trunk/libgazebo/Server.cc       2008-08-20 21:22:19 UTC (rev 
6963)
+++ code/gazebo/trunk/libgazebo/Server.cc       2008-08-20 21:28:24 UTC (rev 
6964)
@@ -38,6 +38,7 @@
 #include <sys/sem.h>
 #include <sstream>
 #include <iostream>
+#include <signal.h>
 
 #include "gazebo.h"
 
@@ -92,6 +93,42 @@
 
   std::cout << "creating " << this->filename << "\n";
 
+  // check to see if there is already a directory created.
+  struct stat astat;
+  if (stat(this->filename.c_str(), &astat) == 0) {
+    // directory already exists, check gazebo.pid to see if 
+    // another gazebo is already running.
+
+    std::string pidfn = this->filename + "/gazebo.pid";
+    
+    FILE *fp = fopen(pidfn.c_str(), "r");
+    if(fp) {
+      int pid;
+      fscanf(fp, "%d", &pid);
+      fclose(fp);
+      std::cout << "found a pid file: pid=" << pid << "\n";
+
+      if(kill(pid, 0) == 0) {
+       // a gazebo process is still alive.
+       errStream << "directory [" <<  this->filename
+                 <<  "] already exists (previous crash?)\n"
+                 << "gazebo (pid=" << pid << ") is still running.";
+       throw(errStream.str());
+      } else {
+       // the gazebo process is not alive.
+       // remove directory.
+       std::cout << "The gazebo process is not alive.\n";
+
+       // remove the existing directory.
+       std::string cmd = "rm -rf '" + this->filename + "'";
+       if(system(cmd.c_str()) != 0) {
+         errStream << "couldn't remove directory [" <<  this->filename << "]";
+         throw(errStream.str());
+       }
+      }
+    }
+  }
+
   // Create the directory
   if (mkdir(this->filename.c_str(), S_IRUSR | S_IWUSR | S_IXUSR) != 0)
   {
@@ -108,7 +145,17 @@
       <<  strerror(errno) << "]";
       throw(errStream.str());
     }
+
   }
+
+  // write the PID to a file
+  std::string pidfn = this->filename + "/gazebo.pid";
+    
+  FILE *fp = fopen(pidfn.c_str(), "w");
+  if(fp) {
+    fprintf(fp, "%d\n", getpid());
+    fclose(fp);
+  }
 }
 
 
@@ -120,6 +167,15 @@
 
   std::cout << "deleting " << this->filename << "\n";
 
+  // unlink the pid file
+  std::string pidfn = this->filename + "/gazebo.pid";
+  if (unlink(pidfn.c_str()) < 0)
+  {
+    std::ostringstream stream;
+    stream << "error deleting pid file: " << strerror(errno);
+    throw(stream.str());
+  }
+
   // Delete the server dir
   if (rmdir(this->filename.c_str()) != 0)
   {


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Playerstage-commit mailing list
Playerstage-commit@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/playerstage-commit

Reply via email to