diff options
author | Ted Ross <tross@apache.org> | 2008-08-08 17:10:10 +0000 |
---|---|---|
committer | Ted Ross <tross@apache.org> | 2008-08-08 17:10:10 +0000 |
commit | 9960a6b2d103145c3fe554dc0720545dec242816 (patch) | |
tree | 40cbbca79bcfdacd7f5ea233d19ec5d66b0fa7e9 | |
parent | bfa30386fb3d881b3849a99fdb69ece18a19d837 (diff) | |
download | qpid-python-9960a6b2d103145c3fe554dc0720545dec242816.tar.gz |
Ported from trunk: Usage of lockf for locking the data directory. This ensures that locks aren't left by crashed processes
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/branches/qpid.0-10@684017 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | cpp/src/qpid/DataDir.cpp | 31 | ||||
-rw-r--r-- | cpp/src/qpid/DataDir.h | 29 |
2 files changed, 34 insertions, 26 deletions
diff --git a/cpp/src/qpid/DataDir.cpp b/cpp/src/qpid/DataDir.cpp index e9c6aaad53..0b0bd7e707 100644 --- a/cpp/src/qpid/DataDir.cpp +++ b/cpp/src/qpid/DataDir.cpp @@ -28,6 +28,7 @@ namespace qpid { + DataDir::DataDir (std::string path) : enabled (!path.empty ()), dirPath (path) @@ -50,34 +51,12 @@ DataDir::DataDir (std::string path) : 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 diff --git a/cpp/src/qpid/DataDir.h b/cpp/src/qpid/DataDir.h index 56aa4f26d7..3873a53485 100644 --- a/cpp/src/qpid/DataDir.h +++ b/cpp/src/qpid/DataDir.h @@ -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 @@ class DataDir { const bool enabled; const std::string dirPath; + std::auto_ptr<LockFile> lockFile; public: |