diff options
author | Zdenek Prikryl <zprikryl@redhat.com> | 2009-02-12 19:10:27 +0100 |
---|---|---|
committer | Zdenek Prikryl <zprikryl@redhat.com> | 2009-02-12 19:10:27 +0100 |
commit | 0f4bdae3d730bb6106b421f1e4f88dbf5bc7e98f (patch) | |
tree | b8e3c3267b6e637b27ee20fcc44081fa022cc754 /lib/Utils | |
parent | 95912e10fb5ee698b99f338a6933a994b5c6d5a8 (diff) | |
download | abrt-0f4bdae3d730bb6106b421f1e4f88dbf5bc7e98f.tar.gz abrt-0f4bdae3d730bb6106b421f1e4f88dbf5bc7e98f.tar.xz abrt-0f4bdae3d730bb6106b421f1e4f88dbf5bc7e98f.zip |
fixed bug in locking
Diffstat (limited to 'lib/Utils')
-rw-r--r-- | lib/Utils/DebugDump.cpp | 43 | ||||
-rw-r--r-- | lib/Utils/DebugDump.h | 2 |
2 files changed, 29 insertions, 16 deletions
diff --git a/lib/Utils/DebugDump.cpp b/lib/Utils/DebugDump.cpp index 19d3a2be..2559b9cf 100644 --- a/lib/Utils/DebugDump.cpp +++ b/lib/Utils/DebugDump.cpp @@ -38,7 +38,7 @@ CDebugDump::CDebugDump() : m_sDebugDumpDir(""), m_nFD(0), - m_bLockCreated(false) + m_bUnlock(true) {} void CDebugDump::Open(const std::string& pDir) @@ -77,35 +77,48 @@ void CDebugDump::Lock() std::string lockPath = m_sDebugDumpDir + ".lock"; if (ExistFileDir(lockPath)) { - if ((m_nFD = open(lockPath.c_str(), O_RDWR | O_CREAT, 0640)) < 0) + int res; + if ((m_nFD = open(lockPath.c_str(), O_RDWR)) < 0) { - throw std::string("CDebugDump::Create(): can not create lock file"); + throw std::string("CDebugDump::Lock(): can not create lock file"); } - m_bLockCreated = false; - } - else - { - if ((m_nFD = open(lockPath.c_str(), O_RDWR | O_CREAT, 0640)) < 0) + res = lockf(m_nFD, F_TEST, 0); + if (res < 0) + { + throw std::string("CDebugDump::Lock(): cannot lock DebugDump"); + } + else if (res == 0) { - throw std::string("CDebugDump::Create(): can not create lock file"); + close(m_nFD); + m_bUnlock = false; + return; } - m_bLockCreated = true; + while (ExistFileDir(lockPath)) + { + std::cerr << "CDebugDump::Lock(): waiting..." << std::endl; + usleep(10); + } + } + + if ((m_nFD = open(lockPath.c_str(), O_RDWR | O_CREAT, 0640)) < 0) + { + throw std::string("CDebugDump::Lock(): can not create lock file"); } if (lockf(m_nFD,F_LOCK, 0) < 0) { - throw std::string("CDebugDump::Create(): cannot lock DebugDump"); + throw std::string("CDebugDump::Lock(): cannot lock DebugDump"); } } void CDebugDump::UnLock() { std::string lockPath = m_sDebugDumpDir + ".lock"; - lockf(m_nFD,F_ULOCK, 0); - close(m_nFD); - if (m_bLockCreated) + if (m_bUnlock) { + lockf(m_nFD,F_ULOCK, 0); + close(m_nFD); remove(lockPath.c_str()); - m_bLockCreated = false; + m_bUnlock = true; } } diff --git a/lib/Utils/DebugDump.h b/lib/Utils/DebugDump.h index a6bb0511..b32dfeba 100644 --- a/lib/Utils/DebugDump.h +++ b/lib/Utils/DebugDump.h @@ -44,7 +44,7 @@ class CDebugDump private: std::string m_sDebugDumpDir; int m_nFD; - bool m_bLockCreated; + bool m_bUnlock; void SaveEnvironment(); void SaveTime(); |