summaryrefslogtreecommitdiff
path: root/cpp/src/qpid/DataDir.h
diff options
context:
space:
mode:
Diffstat (limited to 'cpp/src/qpid/DataDir.h')
-rw-r--r--cpp/src/qpid/DataDir.h29
1 files changed, 29 insertions, 0 deletions
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: