summaryrefslogtreecommitdiffstats
path: root/lib/Utils
diff options
context:
space:
mode:
authorZdenek Prikryl <zprikryl@redhat.com>2009-02-19 18:14:49 +0100
committerZdenek Prikryl <zprikryl@redhat.com>2009-02-19 18:14:49 +0100
commit478eb9a110e235542a829dc287184a681f1abe46 (patch)
tree3401daec2119e65c00d6333de31224ad841fa5da /lib/Utils
parentf15c98d9f235133f198e4e6786bf5a97e5682907 (diff)
downloadabrt-478eb9a110e235542a829dc287184a681f1abe46.tar.gz
abrt-478eb9a110e235542a829dc287184a681f1abe46.tar.xz
abrt-478eb9a110e235542a829dc287184a681f1abe46.zip
rewrote locking stuff
Diffstat (limited to 'lib/Utils')
-rw-r--r--lib/Utils/DebugDump.cpp75
-rw-r--r--lib/Utils/DebugDump.h3
2 files changed, 43 insertions, 35 deletions
diff --git a/lib/Utils/DebugDump.cpp b/lib/Utils/DebugDump.cpp
index 302d3be3..032bed4e 100644
--- a/lib/Utils/DebugDump.cpp
+++ b/lib/Utils/DebugDump.cpp
@@ -35,12 +35,16 @@
CDebugDump::CDebugDump() :
m_sDebugDumpDir(""),
- m_nFD(0),
+ m_bOpened(false),
m_bUnlock(true)
{}
void CDebugDump::Open(const std::string& pDir)
{
+ if (m_bOpened)
+ {
+ throw "CDebugDump::CDebugDump(): DebugDump is already opened.";
+ }
m_sDebugDumpDir = pDir;
std::string lockPath = m_sDebugDumpDir + "/.lock";
if (!ExistFileDir(pDir))
@@ -48,6 +52,7 @@ void CDebugDump::Open(const std::string& pDir)
throw "CDebugDump::CDebugDump(): "+pDir+" does not exist.";
}
Lock();
+ m_bOpened = true;
}
bool CDebugDump::Exist(const std::string& pPath)
@@ -70,43 +75,43 @@ bool CDebugDump::ExistFileDir(const std::string& pPath)
return false;
}
-void CDebugDump::Lock()
+bool CDebugDump::GetAndSetLock(const std::string& pLockFile, const std::string& pPID)
{
- std::string lockPath = m_sDebugDumpDir + ".lock";
- if (ExistFileDir(lockPath))
+ std::ifstream fIn;
+ std::ofstream fOut;
+
+ fIn.open(pLockFile.c_str());
+ if (!fIn.is_open())
{
- int res;
- if ((m_nFD = open(lockPath.c_str(), O_RDWR)) < 0)
- {
- throw std::string("CDebugDump::Lock(): can not create lock file");
- }
- res = lockf(m_nFD, F_TEST, 0);
- if (res < 0)
- {
- throw std::string("CDebugDump::Lock(): cannot lock DebugDump");
- }
- else if (res == 0)
+ fOut.open(pLockFile.c_str());
+ fOut << pPID;
+ fOut.close();
+ m_bUnlock = true;
+ return true;
+ }
+ else
+ {
+ std::string line;
+ getline(fIn, line);
+ if (line == pPID)
{
- close(m_nFD);
m_bUnlock = false;
- return;
+ return true;
}
+ return false;
}
+}
- while (ExistFileDir(lockPath))
+void CDebugDump::Lock()
+{
+ std::string lockPath = m_sDebugDumpDir + ".lock";
+ pid_t nPID = getpid();
+ std::stringstream ss;
+ ss << nPID;
+ while (!GetAndSetLock(lockPath, ss.str()))
{
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)
- {
- remove(lockPath.c_str());
- throw std::string("CDebugDump::Lock(): cannot lock DebugDump");
+ usleep(100);
}
}
@@ -115,15 +120,17 @@ void CDebugDump::UnLock()
std::string lockPath = m_sDebugDumpDir + ".lock";
if (m_bUnlock)
{
- lockf(m_nFD,F_ULOCK, 0);
- close(m_nFD);
remove(lockPath.c_str());
- m_bUnlock = true;
}
}
void CDebugDump::Create(const std::string& pDir)
{
+ if (m_bOpened)
+ {
+ throw "CDebugDump::CDebugDump(): DebugDump is already opened.";
+ }
+
m_sDebugDumpDir = pDir;
std::string lockPath = pDir + ".lock";
if (ExistFileDir(pDir))
@@ -132,6 +139,7 @@ void CDebugDump::Create(const std::string& pDir)
}
Lock();
+ m_bOpened = true;
if (mkdir(pDir.c_str(), 0755) == -1)
{
@@ -182,14 +190,13 @@ void CDebugDump::Delete()
{
return;
}
- Lock();
DeleteFileDir(m_sDebugDumpDir);
- UnLock();
}
void CDebugDump::Close()
{
UnLock();
+ m_bOpened = false;
}
void CDebugDump::SaveEnvironment()
diff --git a/lib/Utils/DebugDump.h b/lib/Utils/DebugDump.h
index eecc89d4..327367ac 100644
--- a/lib/Utils/DebugDump.h
+++ b/lib/Utils/DebugDump.h
@@ -43,7 +43,7 @@ class CDebugDump
{
private:
std::string m_sDebugDumpDir;
- int m_nFD;
+ bool m_bOpened;
bool m_bUnlock;
void SaveEnvironment();
@@ -57,6 +57,7 @@ class CDebugDump
bool ExistFileDir(const std::string& pPath);
void Lock();
+ bool GetAndSetLock(const std::string& pLockFile, const std::string& pPID);
void UnLock();
void DeleteFileDir(const std::string& pDir);