From e30c443b76228fb0e591dc97fd91d9c6bb5cb733 Mon Sep 17 00:00:00 2001 From: Bartlomiej Grzelewski Date: Mon, 4 Jan 2016 15:50:12 +0100 Subject: [PATCH] Integrate glib loop with key-manager. Change-Id: I218d3794e4405ea668c513b3ba40a6e3f98e044c --- src/CMakeLists.txt | 4 ++ src/manager/main/key-manager-main.cpp | 2 + src/manager/main/service-messages.h | 11 +++- src/manager/main/socket-manager.cpp | 17 ++--- src/manager/main/socket-manager.h | 1 + src/manager/service/ckm-service.cpp | 5 ++ src/manager/service/ckm-service.h | 3 +- src/manager/service/glib-logic.cpp | 117 ++++++++++++++++++++++++++++++++++ src/manager/service/glib-logic.h | 67 +++++++++++++++++++ src/manager/service/glib-service.cpp | 81 +++++++++++++++++++++++ src/manager/service/glib-service.h | 67 +++++++++++++++++++ 11 files changed, 362 insertions(+), 13 deletions(-) create mode 100644 src/manager/service/glib-logic.cpp create mode 100644 src/manager/service/glib-logic.h create mode 100644 src/manager/service/glib-service.cpp create mode 100644 src/manager/service/glib-service.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4745577..6de324d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 diff --git a/src/manager/main/key-manager-main.cpp b/src/manager/main/key-manager-main.cpp index c3c256f..e9c87a4 100644 --- a/src/manager/main/key-manager-main.cpp +++ b/src/manager/main/key-manager-main.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -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(); } diff --git a/src/manager/main/service-messages.h b/src/manager/main/service-messages.h index 61ebbb4..f7021a4 100644 --- a/src/manager/main/service-messages.h +++ b/src/manager/main/service-messages.h @@ -22,6 +22,7 @@ #pragma once #include +#include #include #include @@ -74,6 +75,14 @@ struct MsgKeyResponse : public MsgBase { int error; }; -typedef CommunicationManager CommMgr; +struct MsgRemoveAppData { + explicit MsgRemoveAppData(std::string pkgIdT) + : pkgId(std::move(pkgIdT)) + {} + + std::string pkgId; +}; + +typedef CommunicationManager CommMgr; } /* namespace CKM */ diff --git a/src/manager/main/socket-manager.cpp b/src/manager/main/socket-manager.cpp index e892f7f..a8e884f 100644 --- a/src/manager/main/socket-manager.cpp +++ b/src/manager/main/socket-manager.cpp @@ -19,9 +19,6 @@ * @version 1.0 * @brief Implementation of SocketManager. */ - -#include - #include #include #include @@ -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 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); diff --git a/src/manager/main/socket-manager.h b/src/manager/main/socket-manager.h index 933d9b1..cc951d7 100644 --- a/src/manager/main/socket-manager.h +++ b/src/manager/main/socket-manager.h @@ -160,6 +160,7 @@ protected: std::priority_queue m_timeoutQueue; CommMgr m_commMgr; std::unique_ptr m_cynara; + std::vector m_serviceVector; }; } // namespace CKM diff --git a/src/manager/service/ckm-service.cpp b/src/manager/service/ckm-service.cpp index 6773c47..2951063 100644 --- a/src/manager/service/ckm-service.cpp +++ b/src/manager/service/ckm-service.cpp @@ -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"); diff --git a/src/manager/service/ckm-service.h b/src/manager/service/ckm-service.h index b927f40..962e614 100644 --- a/src/manager/service/ckm-service.h +++ b/src/manager/service/ckm-service.h @@ -30,7 +30,7 @@ namespace CKM { class CKMLogic; -class CKMService : public ThreadMessageService { +class CKMService : public ThreadMessageService { 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 index 0000000..52ec71e --- /dev/null +++ b/src/manager/service/glib-logic.cpp @@ -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 +#include + +#include +#include + +#include +#include + +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(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 index 0000000..19eadaf --- /dev/null +++ b/src/manager/service/glib-logic.h @@ -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 + +#include +#include +#include + +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 index 0000000..e5560e8 --- /dev/null +++ b/src/manager/service/glib-service.cpp @@ -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 + +#include + +#include +#include + +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 index 0000000..726dcd5 --- /dev/null +++ b/src/manager/service/glib-service.h @@ -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 + +#include +#include + +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 + -- 2.7.4