You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by tr...@apache.org on 2008/08/08 19:10:10 UTC

svn commit: r684017 - in /incubator/qpid/branches/qpid.0-10/cpp/src/qpid: DataDir.cpp DataDir.h

Author: tross
Date: Fri Aug  8 10:10:10 2008
New Revision: 684017

URL: http://svn.apache.org/viewvc?rev=684017&view=rev
Log:
Ported from trunk: Usage of lockf for locking the data directory.  This ensures that locks aren't left by crashed processes

Modified:
    incubator/qpid/branches/qpid.0-10/cpp/src/qpid/DataDir.cpp
    incubator/qpid/branches/qpid.0-10/cpp/src/qpid/DataDir.h

Modified: incubator/qpid/branches/qpid.0-10/cpp/src/qpid/DataDir.cpp
URL: http://svn.apache.org/viewvc/incubator/qpid/branches/qpid.0-10/cpp/src/qpid/DataDir.cpp?rev=684017&r1=684016&r2=684017&view=diff
==============================================================================
--- incubator/qpid/branches/qpid.0-10/cpp/src/qpid/DataDir.cpp (original)
+++ incubator/qpid/branches/qpid.0-10/cpp/src/qpid/DataDir.cpp Fri Aug  8 10:10:10 2008
@@ -28,6 +28,7 @@
 
 namespace qpid {
 
+
 DataDir::DataDir (std::string path) :
     enabled (!path.empty ()),
     dirPath (path)
@@ -50,34 +51,12 @@
             throw Exception ("Data directory not found: " + path);
     }
 
-    std::string lockFile (path);
-    lockFile = lockFile + "/lock";
-    int fd = ::open (lockFile.c_str (), O_CREAT | O_EXCL,
-                     S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
-    if (fd == -1)
-    {
-        if (errno == EEXIST)
-            throw Exception ("Data directory is locked by another process: " + path);
-        if (errno == EACCES)
-            throw Exception ("Insufficient privileges for data directory: " + path);
-        throw Exception(
-            QPID_MSG("Error locking " << lockFile << ": " << strError(errno)));
-    }
-
-    QPID_LOG (info, "Locked data directory: " << dirPath);
+    std::string lockFileName(path);
+    lockFileName += "/lock";
+    lockFile = std::auto_ptr<LockFile>(new LockFile(lockFileName, true));
 }
 
-DataDir::~DataDir ()
-{
-    if (dirPath.empty ())
-        return;
-
-    std::string lockFile (dirPath);
-    lockFile = lockFile + "/lock";
-
-    ::unlink (lockFile.c_str ());
-    QPID_LOG (info, "Unlocked data directory: " << dirPath);
-}
+DataDir::~DataDir () {}
 
 } // namespace qpid
 

Modified: incubator/qpid/branches/qpid.0-10/cpp/src/qpid/DataDir.h
URL: http://svn.apache.org/viewvc/incubator/qpid/branches/qpid.0-10/cpp/src/qpid/DataDir.h?rev=684017&r1=684016&r2=684017&view=diff
==============================================================================
--- incubator/qpid/branches/qpid.0-10/cpp/src/qpid/DataDir.h (original)
+++ incubator/qpid/branches/qpid.0-10/cpp/src/qpid/DataDir.h Fri Aug  8 10:10:10 2008
@@ -22,9 +22,37 @@
  */
 
 #include <string>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/file.h>
+#include <fcntl.h>
+#include <cerrno>
+#include <unistd.h>
 
 namespace qpid {
 
+class LockFile {
+public:
+    LockFile(const std::string& path_, bool create):
+        path(path_), fd(-1), created(create) {
+        int flags=create ? O_WRONLY|O_CREAT|O_NOFOLLOW : O_RDWR;
+        fd = ::open(path.c_str(), flags, 0644);
+        if (fd < 0) throw Exception("Cannot open " + path + ": " + strError(errno));
+        if (::lockf(fd, F_TLOCK, 0) < 0) throw Exception("Cannot lock " + path + ": " + strError(errno));
+    }
+
+    ~LockFile() {
+        if (fd >= 0) {
+            (void) ::lockf(fd, F_ULOCK, 0); // Suppress warnings about ignoring return value.
+            ::close(fd);
+        }
+    }
+
+    std::string path;
+    int fd;
+    bool created;
+};
+
 /**
  * DataDir class.
  */
@@ -32,6 +60,7 @@
 {
     const bool        enabled;
     const std::string dirPath;
+    std::auto_ptr<LockFile> lockFile;
 
   public: