From: Tomasz Swierczek Date: Mon, 26 Oct 2020 10:07:14 +0000 (+0100) Subject: Change FileLocker implemenation from POSIX to libc flocks X-Git-Tag: submit/tizen/20201026.160418~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=1a43f64e606936d60da16f790db9e2c67ceab147;p=platform%2Fcore%2Fsecurity%2Fsecurity-manager.git Change FileLocker implemenation from POSIX to libc flocks Thanks to this change, same locking could be used in sh/bash scripts and in security-manager daemon (which previously used the POSIX-based boost locks). Change-Id: Ia4f2a5251d3556a40a68234fc2dc1ea51ac48188 --- diff --git a/src/common/file-lock.cpp b/src/common/file-lock.cpp index 1331d98a..021a9988 100644 --- a/src/common/file-lock.cpp +++ b/src/common/file-lock.cpp @@ -28,7 +28,10 @@ /* vim: set ts=4 et sw=4 tw=78 : */ #include +#include +#include +#include "dpl/errno_string.h" #include "dpl/log/log.h" #include "file-lock.h" @@ -42,7 +45,12 @@ FileLocker::FileLocker(const std::string &lockFile, bool blocking) ThrowMsg(FileLocker::Exception::LockFailed, "File name can not be empty."); } - + m_lockFileFd = open(lockFile.c_str(), O_RDWR | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); + if (m_lockFileFd == -1) { + LogError("Lock file cannot be opened " << lockFile); + ThrowMsg(FileLocker::Exception::LockFailed, + "Lock file cannot be opened " << lockFile); + } m_locked = false; m_blocking = blocking; m_lockFile = lockFile; @@ -56,6 +64,7 @@ FileLocker::~FileLocker() } catch (...) { LogError("~FileLocker() threw an exception"); } + (void)close(m_lockFileFd); } bool FileLocker::Locked() @@ -68,20 +77,14 @@ void FileLocker::Lock() if (m_locked) return; - try { - std::ofstream tmpf(m_lockFile); - tmpf.close(); - - m_flock = boost::interprocess::file_lock(m_lockFile.c_str()); - if (m_blocking) { - m_flock.lock(); - m_locked = true; - } else - m_locked = m_flock.try_lock(); - } catch (const std::exception &e) { - LogError("Error while locking a file: " << e.what()); + int ret; + while((-1 == (ret = flock(m_lockFileFd, m_blocking ? LOCK_EX : LOCK_NB | LOCK_EX))) && (errno == EINTR)); + m_locked = ret == 0; + int err = errno; + if (-1 == ret && err != EWOULDBLOCK) { + LogError("Error while locking a file " << m_lockFile); ThrowMsg(FileLocker::Exception::LockFailed, - "Error while locking a file: " << e.what()); + "Error while locking a file " << m_lockFile << ", error: " << GetErrnoString(err)); } if (m_locked) @@ -93,12 +96,13 @@ void FileLocker::Lock() void FileLocker::Unlock() { if (m_locked) { - try { - m_flock.unlock(); - } catch (const std::exception &e) { - LogError("Error while unlocking a file: " << e.what()); + int ret; + while((-1 == (ret = flock(m_lockFileFd, LOCK_UN))) && (errno == EINTR)); + if (-1 == ret) { + int err = errno; + LogError("Error while unlocking a file " << m_lockFile); ThrowMsg(FileLocker::Exception::UnlockFailed, - "Error while unlocking a file: " << e.what()); + "Error while unlocking a file " << m_lockFile << ", error: " << GetErrnoString(err)); } m_locked = false; LogDebug("Lock released."); diff --git a/src/common/include/file-lock.h b/src/common/include/file-lock.h index 913ebe02..ecb69806 100644 --- a/src/common/include/file-lock.h +++ b/src/common/include/file-lock.h @@ -29,8 +29,6 @@ #pragma once -#include - #include #include @@ -57,7 +55,7 @@ public: private: std::string m_lockFile; - boost::interprocess::file_lock m_flock; + int m_lockFileFd; bool m_blocking; bool m_locked; };