Change FileLocker implemenation from POSIX to libc flocks 81/246181/2
authorTomasz Swierczek <t.swierczek@samsung.com>
Mon, 26 Oct 2020 10:07:14 +0000 (11:07 +0100)
committerDariusz Michaluk <d.michaluk@samsung.com>
Mon, 26 Oct 2020 15:46:08 +0000 (16:46 +0100)
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

src/common/file-lock.cpp
src/common/include/file-lock.h

index 1331d98a835daa695156186ae6d5c7b3cc036361..021a9988e07924515e2b20fbed4b7b37afc3f4ee 100644 (file)
 /* vim: set ts=4 et sw=4 tw=78 : */
 
 #include <fstream>
+#include <sys/file.h>
+#include <unistd.h>
 
+#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.");
index 913ebe022adb3dacd2503ba982143105d5ea867b..ecb69806d784f7d5221c504d66b1151d131f6748 100644 (file)
@@ -29,8 +29,6 @@
 
 #pragma once
 
-#include <boost/interprocess/sync/file_lock.hpp>
-
 #include <dpl/exception.h>
 #include <dpl/noncopyable.h>
 
@@ -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;
 };