Integrate glib loop with key-manager. 31/56131/7
authorBartlomiej Grzelewski <b.grzelewski@samsung.com>
Mon, 4 Jan 2016 14:50:12 +0000 (15:50 +0100)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Tue, 12 Jan 2016 14:15:10 +0000 (15:15 +0100)
Change-Id: I218d3794e4405ea668c513b3ba40a6e3f98e044c

src/CMakeLists.txt
src/manager/main/key-manager-main.cpp
src/manager/main/service-messages.h
src/manager/main/socket-manager.cpp
src/manager/main/socket-manager.h
src/manager/service/ckm-service.cpp
src/manager/service/ckm-service.h
src/manager/service/glib-logic.cpp [new file with mode: 0644]
src/manager/service/glib-logic.h [new file with mode: 0644]
src/manager/service/glib-service.cpp [new file with mode: 0644]
src/manager/service/glib-service.h [new file with mode: 0644]

index 4745577..6de324d 100644 (file)
@@ -1,6 +1,7 @@
 PKG_CHECK_MODULES(KEY_MANAGER_DEP
     REQUIRED
     dlog
+    glib-2.0
     openssl
     libsmack
     libcrypto
@@ -11,6 +12,7 @@ PKG_CHECK_MODULES(KEY_MANAGER_DEP
     security-manager
     cynara-client-async
     cynara-creds-socket
+    capi-appfw-package-manager
     )
 FIND_PACKAGE(Threads REQUIRED)
 
@@ -42,6 +44,8 @@ SET(KEY_MANAGER_SOURCES
     ${KEY_MANAGER_PATH}/service/access-control.cpp
     ${KEY_MANAGER_PATH}/service/ckm-service.cpp
     ${KEY_MANAGER_PATH}/service/ckm-logic.cpp
+    ${KEY_MANAGER_PATH}/service/glib-service.cpp
+    ${KEY_MANAGER_PATH}/service/glib-logic.cpp
     ${KEY_MANAGER_PATH}/service/key-provider.cpp
     ${KEY_MANAGER_PATH}/service/ocsp.cpp
     ${KEY_MANAGER_PATH}/service/crypto-logic.cpp
index c3c256f..e9c87a4 100644 (file)
@@ -30,6 +30,7 @@
 #include <ckm-service.h>
 #include <ocsp-service.h>
 #include <encryption-service.h>
+#include <glib-service.h>
 #include <crypto-init.h>
 
 #include <key-provider.h>
@@ -96,6 +97,7 @@ int main(void)
             REGISTER_SOCKET_SERVICE(manager, CKM::CKMService);
             REGISTER_SOCKET_SERVICE(manager, CKM::OCSPService);
             REGISTER_SOCKET_SERVICE(manager, CKM::EncryptionService);
+            REGISTER_SOCKET_SERVICE(manager, CKM::GLIBService);
 
             manager.MainLoop();
         }
index 61ebbb4..f7021a4 100644 (file)
@@ -22,6 +22,7 @@
 #pragma once
 
 #include <memory>
+#include <string>
 
 #include <credentials.h>
 #include <ckm/ckm-type.h>
@@ -74,6 +75,14 @@ struct MsgKeyResponse : public MsgBase {
     int error;
 };
 
-typedef CommunicationManager<MsgKeyRequest, MsgKeyResponse> CommMgr;
+struct MsgRemoveAppData {
+    explicit MsgRemoveAppData(std::string pkgIdT)
+      : pkgId(std::move(pkgIdT))
+    {}
+
+    std::string pkgId;
+};
+
+typedef CommunicationManager<MsgKeyRequest, MsgKeyResponse, MsgRemoveAppData> CommMgr;
 
 } /* namespace CKM */
index e892f7f..a8e884f 100644 (file)
@@ -19,9 +19,6 @@
  * @version     1.0
  * @brief       Implementation of SocketManager.
  */
-
-#include <set>
-
 #include <signal.h>
 #include <sys/select.h>
 #include <sys/signalfd.h>
@@ -185,6 +182,7 @@ SocketManager::SocketManager() :
 
     auto &desc = CreateDefaultReadSocketDescription(m_notifyMe[0], false);
     desc.service = new DummyService;
+    m_serviceVector.push_back(desc.service);
 
     // std::thread bases on pthread so this should work fine
     sigset_t set;
@@ -206,6 +204,9 @@ SocketManager::SocketManager() :
         LogInfo("SignalService mounted on " << filefd << " descriptor");
     }
 
+    if (signalService)
+        m_serviceVector.push_back(signalService);
+
     // We cannot create Cynara earlier because descriptors are not initialized!
     m_cynara.reset(new Cynara(this));
 }
@@ -213,16 +214,9 @@ SocketManager::SocketManager() :
 SocketManager::~SocketManager()
 {
     m_cynara.reset(nullptr);
-    std::set<GenericSocketService*> serviceMap;
-
-    // Find all services. Set is used to remove duplicates.
-    // In this implementation, services are not able to react in any way.
-    for (size_t i=0; i < m_socketDescriptionVector.size(); ++i)
-        if (m_socketDescriptionVector[i].isOpen())
-            serviceMap.insert(m_socketDescriptionVector[i].service);
 
     // Time to destroy all services.
-    for (auto service : serviceMap) {
+    for (auto service : m_serviceVector) {
         LogDebug("delete " << (void*)(service));
         if (service)
             service->Stop();
@@ -616,6 +610,7 @@ void SocketManager::RegisterSocketService(GenericSocketService *service)
     service->SetSocketManager(this);
     service->SetCommManager(&m_commMgr);
     auto serviceVector = service->GetServiceDescription();
+    m_serviceVector.push_back(service);
     Try {
         for (auto iter = serviceVector.begin(); iter != serviceVector.end(); ++iter)
             CreateDomainSocket(service, *iter);
index 933d9b1..cc951d7 100644 (file)
@@ -160,6 +160,7 @@ protected:
     std::priority_queue<Timeout> m_timeoutQueue;
     CommMgr m_commMgr;
     std::unique_ptr<Cynara> m_cynara;
+    std::vector<GenericSocketService*> m_serviceVector;
 };
 
 } // namespace CKM
index 6773c47..2951063 100644 (file)
@@ -418,6 +418,11 @@ void CKMService::ProcessMessage(MsgKeyRequest msg)
     }
 }
 
+void CKMService::ProcessMessage(MsgRemoveAppData msg) {
+    LogDebug("Call removeApplicationData. pkgId: " << msg.pkgId);
+    m_logic->removeApplicationData(msg.pkgId);
+}
+
 void CKMService::CustomHandle(const ReadEvent &event)
 {
     LogDebug("Read event");
index b927f40..962e614 100644 (file)
@@ -30,7 +30,7 @@ namespace CKM {
 
 class CKMLogic;
 
-class CKMService : public ThreadMessageService<MsgKeyRequest> {
+class CKMService : public ThreadMessageService<MsgKeyRequest, MsgRemoveAppData> {
 public:
     CKMService();
     CKMService(const CKMService &) = delete;
@@ -84,6 +84,7 @@ private:
         MessageBuffer &buffer);
 
     virtual void ProcessMessage(MsgKeyRequest msg);
+    virtual void ProcessMessage(MsgRemoveAppData msg);
 
     CKMLogic *m_logic;
 };
diff --git a/src/manager/service/glib-logic.cpp b/src/manager/service/glib-logic.cpp
new file mode 100644 (file)
index 0000000..52ec71e
--- /dev/null
@@ -0,0 +1,117 @@
+/*
+ *  Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ *
+ *
+ * @file        glib-logic.cpp
+ * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
+ * @version     1.0
+ * @brief       Dbus listener implementation as service.
+ */
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <glib.h>
+#include <package_manager.h>
+
+#include <dpl/log/log.h>
+#include <glib-logic.h>
+
+namespace CKM {
+
+GLIBLogic::GLIBLogic()
+  : m_commMgr(nullptr)
+{
+    LogDebug("Allocation g_main_loop");
+    m_gMainLoop = g_main_loop_new(nullptr, FALSE);
+}
+
+void GLIBLogic::LoopStart() {
+    package_manager_h request;
+    package_manager_create(&request);
+    LogDebug("Register uninstalledApp event callback start");
+    if (0 != package_manager_set_global_event_cb(request, packageEventCallbackStatic, this)) {
+        LogError("Error in package_manager_set_event_cb");
+    }
+    LogDebug("Starting g_main_loop");
+    g_main_loop_run(m_gMainLoop);
+    LogDebug("...g_main_loop ended");
+}
+
+void GLIBLogic::LoopStop() {
+    LogDebug("Closing g_main_loop");
+    g_main_loop_quit(m_gMainLoop);
+}
+
+GLIBLogic::~GLIBLogic() {
+    LogDebug("Destroying g_main_loop");
+    g_main_loop_unref(m_gMainLoop);
+}
+
+void GLIBLogic::SetCommManager(CommMgr *manager) {
+    m_commMgr = manager;
+}
+
+void GLIBLogic::packageEventCallbackStatic(
+        uid_t uid,
+        const char *type,
+        const char *package,
+        package_manager_event_type_e eventType,
+        package_manager_event_state_e eventState,
+        int progress,
+        package_manager_error_e error,
+        void *userData)
+{
+    LogDebug("Some event was caught");
+
+    if (!userData)
+        return;
+
+    static_cast<GLIBLogic*>(userData)->packageEventCallback(
+        uid,
+        type,
+        package,
+        eventType,
+        eventState,
+        progress,
+        error);
+}
+
+void GLIBLogic::packageEventCallback(
+        uid_t uid,
+        const char *type,
+        const char *package,
+        package_manager_event_type_e eventType,
+        package_manager_event_state_e eventState,
+        int progress,
+        package_manager_error_e error)
+{
+    (void) uid;
+    (void) type;
+    (void) progress;
+    (void) error;
+
+    if (eventType != PACKAGE_MANAGER_EVENT_TYPE_UNINSTALL
+            || eventState != PACKAGE_MANAGER_EVENT_STATE_COMPLETED
+            || package == NULL)
+    {
+        return;
+    }
+
+    LogDebug("PackageUninstalled Callback. Uninstalation of: " << package);
+    m_commMgr->SendMessage(MsgRemoveAppData(std::string(package)));
+}
+
+} // namespace CKM
+
diff --git a/src/manager/service/glib-logic.h b/src/manager/service/glib-logic.h
new file mode 100644 (file)
index 0000000..19eadaf
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ *  Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ *
+ *
+ * @file        glib-logic.h
+ * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
+ * @version     1.0
+ * @brief       Dbus listener implementation as service.
+ */
+#pragma once
+
+#include <glib.h>
+
+#include <noncopyable.h>
+#include <package_manager.h>
+#include <service-messages.h>
+
+namespace CKM {
+
+class GLIBLogic {
+public:
+    GLIBLogic();
+    
+    NONCOPYABLE(GLIBLogic);
+
+    void LoopStart();
+    void LoopStop();
+    void SetCommManager(CommMgr *manager);
+    virtual ~GLIBLogic();
+protected:
+    static void packageEventCallbackStatic(
+        uid_t uid,
+        const char *type,
+        const char *package,
+        package_manager_event_type_e eventType,
+        package_manager_event_state_e eventState,
+        int progress,
+        package_manager_error_e error,
+        void *userData);
+
+    void packageEventCallback(
+        uid_t uid,
+        const char *type,
+        const char *package,
+        package_manager_event_type_e eventType,
+        package_manager_event_state_e eventState,
+        int progress,
+        package_manager_error_e error);
+
+    CommMgr *m_commMgr;
+    GMainLoop *m_gMainLoop;
+};
+
+} // namespace CKM
+
diff --git a/src/manager/service/glib-service.cpp b/src/manager/service/glib-service.cpp
new file mode 100644 (file)
index 0000000..e5560e8
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ *  Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ *
+ *
+ * @file        glib-service.cpp
+ * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
+ * @version     1.0
+ * @brief       Dbus listener implementation as service.
+ */
+
+#include <thread>
+
+#include <dpl/log/log.h>
+
+#include <glib-service.h>
+#include <glib-logic.h>
+
+namespace CKM {
+
+GLIBService::GLIBService()
+  : m_state(State::NoThread)
+  , m_logic(new GLIBLogic())
+{}
+
+void GLIBService::Event(const AcceptEvent &) {}
+void GLIBService::Event(const WriteEvent &) {}
+void GLIBService::Event(const ReadEvent &) {}
+void GLIBService::Event(const CloseEvent &) {}
+void GLIBService::Event(const SecurityEvent &) {}
+
+void GLIBService::Start(){
+    LogDebug("Starting thread!");
+    assert(m_state == State::NoThread);
+    m_thread = std::thread(ThreadLoopStatic, this);
+    m_state = State::Work;
+}
+
+void GLIBService::Stop(){
+    LogDebug("Stopping thread!");
+    assert(m_state == State::Work);
+    m_logic->LoopStop();
+    m_thread.join();
+    m_state = State::NoThread;
+    LogDebug("Thread for glib joined!");
+}
+
+GLIBService::~GLIBService(){
+    delete m_logic;
+}
+
+GLIBService::ServiceDescriptionVector GLIBService::GetServiceDescription() {
+    return ServiceDescriptionVector();
+}
+
+void GLIBService::ThreadLoopStatic(GLIBService *ptr) {
+    ptr->ThreadLoop();
+}
+
+void GLIBService::ThreadLoop() {
+    m_logic->LoopStart();
+}
+
+void GLIBService::SetCommManager(CommMgr *manager) {
+    m_commMgr = manager;
+    m_logic->SetCommManager(manager);
+}
+
+} // namespace CKM
+
diff --git a/src/manager/service/glib-service.h b/src/manager/service/glib-service.h
new file mode 100644 (file)
index 0000000..726dcd5
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ *  Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ *
+ *
+ * @file        glib-service.h
+ * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
+ * @version     1.0
+ * @brief       Dbus listener implementation as service.
+ */
+#pragma once
+
+#include <thread>
+
+#include <noncopyable.h>
+#include <generic-socket-manager.h>
+
+namespace CKM {
+
+class GLIBLogic;
+
+class GLIBService : public CKM::GenericSocketService {
+public:
+    enum class State {
+        NoThread,
+        Work,
+    };
+
+    GLIBService();
+    NONCOPYABLE(GLIBService);
+
+    // This service does not provide any socket for communication so no events will be supported
+    virtual void Event(const AcceptEvent &);
+    virtual void Event(const WriteEvent &);
+    virtual void Event(const ReadEvent &);
+    virtual void Event(const CloseEvent &);
+    virtual void Event(const SecurityEvent &);
+
+    virtual void Start();
+    virtual void Stop();
+
+    virtual ~GLIBService();
+
+    virtual ServiceDescriptionVector GetServiceDescription();
+    virtual void SetCommManager(CommMgr *manager);
+protected:
+    static void ThreadLoopStatic(GLIBService *ptr);
+    void ThreadLoop();
+
+    State m_state;
+    std::thread m_thread;
+    GLIBLogic *m_logic;
+};
+
+} // namespace CKM
+