Remove unnecessary cynara async API usage 24/315424/13
authorTomasz Swierczek <t.swierczek@samsung.com>
Mon, 2 Dec 2024 02:35:44 +0000 (03:35 +0100)
committerTomasz Swierczek <t.swierczek@samsung.com>
Mon, 7 Apr 2025 05:19:13 +0000 (07:19 +0200)
Change-Id: I2f0558b8bddf21c43e5edcf19bcb706be5a38160

packaging/security-manager.spec
src/client/CMakeLists.txt
src/common/CMakeLists.txt
src/common/cynara.cpp
src/common/include/cynara.h
test/CMakeLists.txt

index 10d19b31d4d95a2e281e24de4ffd90c39b3db0c7..04f5f64f9efa38b03f927b6d858220228d3a8eb6 100644 (file)
@@ -39,7 +39,7 @@ BuildRequires: pkgconfig(libtzplatform-config)
 BuildRequires: tizen-platform-config-tools
 BuildRequires: pkgconfig(sqlite3)
 BuildRequires: pkgconfig(cynara-admin) >= 0.19.0
-BuildRequires: pkgconfig(cynara-client-async)
+BuildRequires: pkgconfig(cynara-client)
 BuildRequires: pkgconfig(security-privilege-manager)
 BuildRequires: pkgconfig(openssl3)
 BuildRequires: pkgconfig(mount)
index bea9571783c8caa6e0c4da16c384a18fdaf28a06..253be26b1d0249c2e3a694d09de845535b27cc0b 100644 (file)
@@ -22,7 +22,7 @@
 PKG_CHECK_MODULES(CLIENT_DEP
     REQUIRED
     capi-system-resource
-    cynara-client-async
+    cynara-client
     libsmack
     libcap
     mount
index d257e35d5fc7a9778dd9203f89ff3bf3a477f6ad..84658c239dd3be61c501fc577987c4369fa67d72 100644 (file)
@@ -30,7 +30,7 @@ PKG_CHECK_MODULES(COMMON_DEP
     libsmack
     sqlite3
     cynara-admin
-    cynara-client-async
+    cynara-client
     libtzplatform-config
     security-privilege-manager
     mount
index 758a9fcd0e6eadb893eb9147753d2662cb294a21..1d5da17dc1c02230d0b3ada370f1365af76de432 100644 (file)
@@ -925,189 +925,35 @@ std::vector<CynaraAdmin::BatchCheckResult> CynaraAdmin::checkBatch(const std::ve
     }
 }
 
-Cynara::Cynara() : m_eventFd(eventfd(0, 0)), m_cynaraFd(m_eventFd), m_cynaraFdEvents(0), m_terminate(false)
+Cynara::Cynara()
 {
-    if (m_eventFd == -1) {
-        LogErrno("creating eventfd");
-        ThrowMsg(CynaraException::UnknownError, "Error while creating eventfd");
-    }
-
-    cynara_async_configuration *p_conf = nullptr;
-    checkCynaraError(cynara_async_configuration_create(&p_conf),
-                     "Cannot create cynara async configuration");
-    auto confPtr = makeUnique(p_conf, cynara_async_configuration_destroy);
-
-    checkCynaraError(cynara_async_configuration_set_cache_size(p_conf, CACHE_SIZE),
-            "Cannot set cynara async configuration cache size");
-    checkCynaraError(
-        cynara_async_initialize(&m_cynara, p_conf, &Cynara::statusCallback, this),
-        "Cannot connect to Cynara policy interface.");
-
-    m_thread = std::thread(&Cynara::run, this);
+    cynara_configuration *p_conf = nullptr;
+    checkCynaraError(cynara_configuration_create(&p_conf),
+                     "Cannot create cynara configuration");
+    auto confPtr = makeUnique(p_conf, cynara_configuration_destroy);
+    checkCynaraError(cynara_configuration_set_cache_size(p_conf, CACHE_SIZE),
+                     "Cannot set cynara cache size");
+    checkCynaraError(cynara_initialize(&m_cynara, p_conf),
+                     "Cannot connect to Cynara policy interface.");
 }
 
 Cynara::~Cynara()
 {
-    LogDebug("Sending terminate event to Cynara thread");
-    m_terminate = true;
-    threadNotifyPut();
-    m_thread.join();
-
-    // Critical section
-    std::lock_guard<std::mutex> guard(m_mutex);
-    cynara_async_finish(m_cynara);
-}
-
-void Cynara::threadNotifyPut()
-{
-    if (eventfd_write(m_eventFd, 1))
-        LogErrno("writing to eventfd");
+    cynara_finish(m_cynara);
 }
 
-void Cynara::threadNotifyGet()
-{
-    eventfd_t value;
-    if (eventfd_read(m_eventFd, &value))
-        LogErrno("reading from eventfd");
-}
-
-void Cynara::statusCallback(int oldFd, int newFd, cynara_async_status status)
-{
-    LogDebug("Cynara status callback. " <<
-        "Status = " << status << ", oldFd = " << oldFd << ", newFd = " << newFd);
-
-    if (newFd == -1) {
-        m_cynaraFdEvents = 0;
-    } else {
-        m_cynaraFd = newFd;
-        switch (status) {
-        case CYNARA_STATUS_FOR_READ:
-            m_cynaraFdEvents = POLLIN;
-            break;
-        case CYNARA_STATUS_FOR_RW:
-            m_cynaraFdEvents = POLLIN | POLLOUT;
-            break;
-        }
-    }
-
-    std::atomic_thread_fence(std::memory_order_release);
-    threadNotifyPut();
-}
-
-void Cynara::statusCallback(int oldFd, int newFd, cynara_async_status status,
-    void *ptr)
-{
-    static_cast<Cynara *>(ptr)->statusCallback(oldFd, newFd, status);
-}
-
-void Cynara::responseCallback(cynara_check_id checkId,
-    cynara_async_call_cause cause, int response, void *ptr)
-{
-    LogDebug("Response for received for Cynara check id: " << checkId);
-
-    auto promise = static_cast<std::promise<bool>*>(ptr);
-
-    switch (cause) {
-    case CYNARA_CALL_CAUSE_ANSWER:
-        LogDebug("Cynara cause: ANSWER: " << response);
-        promise->set_value(response == CYNARA_API_ACCESS_ALLOWED);
-        break;
-
-    case CYNARA_CALL_CAUSE_CANCEL:
-        LogDebug("Cynara cause: CANCEL");
-        promise->set_value(CYNARA_API_ACCESS_DENIED);
-        break;
-
-    case CYNARA_CALL_CAUSE_FINISH:
-        LogDebug("Cynara cause: FINISH");
-        promise->set_value(CYNARA_API_ACCESS_DENIED);
-        break;
-
-    case CYNARA_CALL_CAUSE_SERVICE_NOT_AVAILABLE:
-        LogError("Cynara cause: SERVICE_NOT_AVAILABLE");
-
-        try {
-            ThrowMsg(CynaraException::ServiceNotAvailable,
-                "Cynara service not available");
-        } catch (...) {
-            promise->set_exception(std::current_exception());
-        }
-        break;
-    }
-}
-
-void Cynara::run()
-{
-    LogInfo("Cynara thread started");
-    while (true) {
-        std::atomic_thread_fence(std::memory_order_acquire);
-        struct pollfd pollFds[2] = {{m_eventFd, POLLIN, 0}, {m_cynaraFd, m_cynaraFdEvents, 0}};
-        int ret = poll(pollFds, 2, -1);
-
-        if (ret == -1) {
-            Assert(errno == EINTR);
-            continue;
-        }
-
-        // Check eventfd for termination signal
-        if (pollFds[0].revents) {
-            threadNotifyGet();
-            if (m_terminate) {
-                LogInfo("Cynara thread terminated");
-                return;
-            }
-        }
-
-        // Check if Cynara fd is ready for processing
-        if (pollFds[1].revents) {
-            try {
-                // Critical section
-                std::lock_guard<std::mutex> guard(m_mutex);
-
-                checkCynaraError(cynara_async_process(m_cynara),
-                    "Unexpected error returned by cynara_async_process");
-            } catch (const CynaraException::Base &e) {
-                LogError("Error while processing Cynara events: " << e.DumpToString());
-            }
-        }
-    }
-}
 
 bool Cynara::check(const std::string &label, const std::string &privilege,
         const std::string &user, const std::string &session)
 {
     std::lock_guard<std::mutex> lock(m_api_mutex);
-    // TODO remove the async API here in refactoring later
-    // introduced in 2015 with multithreaded security-manager in mind
     LogDebug("check: client = " << label << ", user = " << user <<
         ", privilege = " << privilege << ", session = " << session);
 
-    std::promise<bool> promise;
-    auto future = promise.get_future();
-
-    // Critical section
-    {
-        std::lock_guard<std::mutex> guard(m_mutex);
-
-        int ret = cynara_async_check_cache(m_cynara,
+    int ret = cynara_check(m_cynara,
             label.c_str(), session.c_str(), user.c_str(), privilege.c_str());
 
-        if (ret != CYNARA_API_CACHE_MISS)
-            return checkCynaraError(ret, "Error while checking Cynara cache");
-
-        LogDebug("Cynara cache miss");
-
-        cynara_check_id check_id;
-        checkCynaraError(
-            cynara_async_create_request(m_cynara,
-                label.c_str(), session.c_str(), user.c_str(), privilege.c_str(),
-                &check_id, &Cynara::responseCallback, &promise),
-            "Cannot check permission with Cynara.");
-
-        LogDebug("Waiting for response to Cynara query id " << check_id);
-    }
-
-    return future.get();
+    return ret == CYNARA_API_ACCESS_ALLOWED;
 }
 
 } // namespace SecurityManager
index cb4e1961a8ba73e8942872557a4cd3ffc6bf8956..61abaa744dbb56407c747cba49ce623b165e75fa 100644 (file)
 
 #pragma once
 
-#include <cynara-client-async.h>
+#include <cynara-client.h>
 #include <cynara-admin.h>
 #include <dpl/exception.h>
 #include <string>
 #include <vector>
 #include <map>
 #include <mutex>
-#include <thread>
-#include <future>
-
-#include <poll.h>
-#include <sys/eventfd.h>
 
 #include "security-manager.h"
 #include "privilege_db.h"
@@ -416,28 +411,8 @@ public:
 private:
     static const int CACHE_SIZE = 100;
 
-    void statusCallback(int oldFd, int newFd, cynara_async_status status);
-
-    static void statusCallback(int oldFd, int newFd,
-        cynara_async_status status, void *ptr);
-
-    static void responseCallback(cynara_check_id checkId,
-        cynara_async_call_cause cause, int response, void *ptr);
-
-    void run();
-
-    void threadNotifyPut();
-    void threadNotifyGet();
-
-    cynara_async *m_cynara;
-    std::mutex m_mutex;
+    cynara *m_cynara;
     std::mutex m_api_mutex;
-    std::thread m_thread;
-
-    const int m_eventFd;
-    std::atomic<int> m_cynaraFd;
-    std::atomic<short> m_cynaraFdEvents;
-    std::atomic<bool> m_terminate;
 };
 
 } // namespace SecurityManager
index af3c1566570a8edcc7738214a887eefc8e86f722..322278cb9b4b39b15229fff2d870ff1bf009c78c 100644 (file)
@@ -27,7 +27,7 @@ PKG_CHECK_MODULES(COMMON_DEP REQUIRED
     libsmack
     sqlite3
     cynara-admin
-    cynara-client-async
+    cynara-client
     libtzplatform-config
     security-privilege-manager
     mount