diff options
author | Krzysztof Jackiewicz <k.jackiewicz@samsung.com> | 2015-08-27 12:44:16 +0200 |
---|---|---|
committer | Janusz Kozerski <j.kozerski@samsung.com> | 2015-09-08 14:12:36 +0200 |
commit | 8fc0b8fc2a814035dc9cf8c7e211dc3a8f74d11b (patch) | |
tree | 6f86a12ee0423d9b83eb5ca5f55bd20cb8dee129 /src | |
parent | ae4a130374e96d383e09571f2e098ef237e28418 (diff) | |
download | cert-checker-8fc0b8fc2a814035dc9cf8c7e211dc3a8f74d11b.tar.gz cert-checker-8fc0b8fc2a814035dc9cf8c7e211dc3a8f74d11b.tar.bz2 cert-checker-8fc0b8fc2a814035dc9cf8c7e211dc3a8f74d11b.zip |
Fixed synchronisation issues
[Problem] Production code mixed with test code. Poor readability.
Synchronisation issues.
[Solution] Synchronisation reimplemented. Test code separated from production
code.
[Verification] Run all test
Change-Id: Iea5ed2ce9f10a4cdac8994acf91809cd12050d69
Diffstat (limited to 'src')
-rw-r--r-- | src/include/cchecker/logic.h | 11 | ||||
-rw-r--r-- | src/logic.cpp | 130 |
2 files changed, 82 insertions, 59 deletions
diff --git a/src/include/cchecker/logic.h b/src/include/cchecker/logic.h index aa50388..b5f1c22 100644 --- a/src/include/cchecker/logic.h +++ b/src/include/cchecker/logic.h @@ -97,10 +97,15 @@ class Logic { void *logic_ptr) ); + void push_event(event_t event); + void process_all(void); void process_queue(void); - void process_event(const event_t &event); + virtual void process_event(const event_t &event); + + bool process_app(app_t& app); void process_buffer(void); + virtual void app_processed() {}; // for tests bool get_online(void) const; void set_online(bool online); @@ -108,7 +113,7 @@ class Logic { bool get_should_exit(void) const; void set_should_exit(void); - void call_ui(const app_t &app); + bool call_ui(const app_t &app); Queue m_queue; Certs m_certs; @@ -117,6 +122,8 @@ class Logic { bool m_was_setup_called; bool m_is_online; + // TODO: use m_queue for online events + bool m_is_online_enabled; std::condition_variable m_to_process; std::mutex m_mutex_cv; std::thread m_thread; diff --git a/src/logic.cpp b/src/logic.cpp index aef6aec..da1599c 100644 --- a/src/logic.cpp +++ b/src/logic.cpp @@ -46,8 +46,11 @@ Logic::~Logic(void) // wait and join processing thread if (m_thread.joinable()) { LogDebug("Waiting for join processing thread"); - set_should_exit(); - m_to_process.notify_one(); + { + std::lock_guard<std::mutex> lock(m_mutex_cv); + set_should_exit(); + m_to_process.notify_one(); + } m_thread.join(); LogDebug("Processing thread joined"); } @@ -67,6 +70,7 @@ Logic::Logic(void) : m_sqlquery(NULL), m_was_setup_called(false), m_is_online(false), + m_is_online_enabled(false), m_should_exit(false), m_proxy_connman(NULL), m_proxy_pkgmgr_install(NULL), @@ -80,7 +84,14 @@ bool Logic::get_online() const void Logic::set_online(bool online) { + std::lock_guard<std::mutex> lock(m_mutex_cv); + if (m_is_online == online) + return; m_is_online = online; + if (m_is_online) { + m_is_online_enabled = true; + m_to_process.notify_one(); + } } error_t Logic::setup_db() @@ -316,15 +327,13 @@ void Logic::pkgmgr_callback_internal(GVariant *parameters, if (event == EVENT_INSTALL) { LogDebug("Install: uid : " << uid << ", pkgid: " << pkgid << ", state: " << state << ", status: " << status); - m_queue.push_event(event_t(app, event_t::event_type_t::APP_INSTALL)); + push_event(event_t(app, event_t::event_type_t::APP_INSTALL)); } else if (event == EVENT_UNINSTALL) { LogDebug("Uninstall: uid : " << uid << ", pkgid: " << pkgid << ", state: " << state << ", status: " << status); - m_queue.push_event(event_t(app, event_t::event_type_t::APP_UNINSTALL)); + push_event(event_t(app, event_t::event_type_t::APP_UNINSTALL)); } - - m_to_process.notify_one(); } else LogDebug("Wrong state (" << std::string(state) << ") or status (" << std::string(status) << ")"); @@ -381,82 +390,92 @@ void Logic::process_queue(void) } } -void Logic::call_ui(const app_t &app) +bool Logic::call_ui(const app_t &app) { UI::UIBackend ui; if (ui.call_popup(app)) { // If calling popup or app_controll service will fail, // do not remove application, and ask about it once again later - remove_app_from_buffer_and_database(app); LogDebug("Popup shown correctly. Application will be removed from DB and buffer"); + return true; } - else - LogDebug("Popup error. Application will be marked to show popup later."); + LogDebug("Popup error. Application will be marked to show popup later."); + return false; } -void Logic::process_buffer(void) -{ - for (auto iter = m_buffer.begin(); iter != m_buffer.end();) { - - // Check if app hasn't already been verified. - // If yes then just try to display popup once again, and go the next app. +bool Logic::process_app(app_t& app) { + // Check if app hasn't already been verified. + // If yes then just try to display popup once again, and go the next app. #if POPUP - if (iter->verified == app_t::verified_t::NO) { - app_t app_cpy = *iter; - LogDebug(app_cpy.str() << " has been verified before. Popup should be shown."); - call_ui(app_cpy); - iter++; - continue; - } + if (app.verified == app_t::verified_t::NO) { + LogDebug(app.str() << " has been verified before. Popup should be shown."); + return call_ui(app); + } #endif - Certs::ocsp_response_t ret; - ret = m_certs.check_ocsp(*iter); + Certs::ocsp_response_t ret; + ret = m_certs.check_ocsp(app); - // If OCSP returns success or OCSP checking fails we should remove application from buffer and database - if (ret == Certs::ocsp_response_t::OCSP_APP_OK || - ret == Certs::ocsp_response_t::OCSP_CERT_ERROR) { - LogDebug(iter->str() << " OCSP verified (or not available for app's chains)"); - app_t app_cpy = *iter; - iter++; - remove_app_from_buffer_and_database(app_cpy); - } - else if (ret == Certs::ocsp_response_t::OCSP_APP_REVOKED) { - LogDebug(iter->str() << " certificate has been revoked. Popup should be shown"); - iter->verified = app_t::verified_t::NO; - app_t app_cpy = *iter; - iter++; + // If OCSP returns success or OCSP checking fails we should remove application from buffer and database + if (ret == Certs::ocsp_response_t::OCSP_APP_OK || + ret == Certs::ocsp_response_t::OCSP_CERT_ERROR) { + LogDebug(app.str() << " OCSP verified (or not available for app's chains)"); + return true; + } + else if (ret == Certs::ocsp_response_t::OCSP_APP_REVOKED) { + LogDebug(app.str() << " certificate has been revoked. Popup should be shown"); + app.verified = app_t::verified_t::NO; #if POPUP // Do not remove app here - just waits for user answer from popup // Temporary solution because notification framework doesn't work - call_ui(app_cpy); + return call_ui(app); #else - remove_app_from_buffer_and_database(app_cpy); + return true; #endif - } - else { - LogDebug(iter->str() << " should be checked again later"); - // If check_ocsp returns Certs::ocsp_response_t::OCSP_CHECK_AGAIN - // app should be checked again later - iter++; - } } + else { + LogDebug(app.str() << " should be checked again later"); + // If check_ocsp returns Certs::ocsp_response_t::OCSP_CHECK_AGAIN + // app should be checked again later + } + return false; +} + +void Logic::process_buffer(void) +{ + for (auto iter = m_buffer.begin(); iter != m_buffer.end();) { + bool remove = process_app(*iter); + auto prev = *iter; + iter++; + if (remove) + remove_app_from_buffer_and_database(prev); + app_processed(); + } +} + +void Logic::push_event(event_t event) +{ + std::lock_guard<std::mutex> lock(m_mutex_cv); + m_queue.push_event(std::move(event)); + m_to_process.notify_one(); } void Logic::process_all() { - //Check if should't exit - while (!get_should_exit()) { + for(;;) { std::unique_lock<std::mutex> lock(m_mutex_cv); + if (m_should_exit) + break; //lock will be unlocked upon destruction - if (m_queue.empty()) { + // don't sleep if there are online/installation/deinstallation events to process + if(m_queue.empty() && !m_is_online_enabled) { LogDebug("Processing thread: waiting on condition"); - m_to_process.wait(lock); + m_to_process.wait(lock); // spurious wakeups do not concern us + LogDebug("Processing thread: running"); } - else - LogDebug("Processing thread: More events in queue - processing again"); - LogDebug("Processing thread: running"); + m_is_online_enabled = false; + lock.unlock(); process_queue(); if (get_online()) @@ -466,9 +485,6 @@ void Logic::process_all() LogDebug("Processing done"); } - - // should process queue just before exit - process_queue(); } void Logic::process_event(const event_t &event) |