diff options
Diffstat (limited to 'src/appMain/life_cycle.cc')
-rw-r--r-- | src/appMain/life_cycle.cc | 396 |
1 files changed, 183 insertions, 213 deletions
diff --git a/src/appMain/life_cycle.cc b/src/appMain/life_cycle.cc index ca629e2311..71ca588e65 100644 --- a/src/appMain/life_cycle.cc +++ b/src/appMain/life_cycle.cc @@ -1,5 +1,5 @@ /* -* Copyright (c) 2014, Ford Motor Company +* Copyright (c) 2016, Ford Motor Company * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -30,25 +30,28 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include "./life_cycle.h" +#include "life_cycle.h" #include "utils/signals.h" +#include "utils/make_shared.h" #include "config_profile/profile.h" #include "resumption/last_state.h" #ifdef ENABLE_SECURITY #include "security_manager/security_manager_impl.h" #include "security_manager/crypto_manager_impl.h" +#include "security_manager/crypto_manager_settings_impl.h" +#include "application_manager/policies/policy_handler.h" #endif // ENABLE_SECURITY #ifdef ENABLE_LOG #include "utils/log_message_loop_thread.h" -#endif +#endif // ENABLE_LOG using threads::Thread; namespace main_namespace { -CREATE_LOGGERPTR_GLOBAL(logger_, "appMain") +CREATE_LOGGERPTR_GLOBAL(logger_, "SDLMain") namespace { void NameMessageBrokerThread(const System::Thread& thread, @@ -58,117 +61,86 @@ void NameMessageBrokerThread(const System::Thread& thread, } // namespace LifeCycle::LifeCycle() - : transport_manager_(NULL) - , protocol_handler_(NULL) - , connection_handler_(NULL) - , app_manager_(NULL) + : transport_manager_(NULL) + , protocol_handler_(NULL) + , connection_handler_(NULL) + , app_manager_(NULL) #ifdef ENABLE_SECURITY - , crypto_manager_(NULL) - , security_manager_(NULL) + , crypto_manager_(NULL) + , security_manager_(NULL) #endif // ENABLE_SECURITY - , hmi_handler_(NULL) - , hmi_message_adapter_(NULL) - , media_manager_(NULL) -#ifdef TIME_TESTER - , time_tester_(NULL) -#endif // TIME_TESTER + , hmi_handler_(NULL) + , hmi_message_adapter_(NULL) + , media_manager_(NULL) +#ifdef TELEMETRY_MONITOR + , telemetry_monitor_(NULL) +#endif // TELEMETRY_MONITOR #ifdef DBUS_HMIADAPTER - , dbus_adapter_(NULL) - , dbus_adapter_thread_(NULL) + , dbus_adapter_(NULL) + , dbus_adapter_thread_(NULL) #endif // DBUS_HMIADAPTER #ifdef MESSAGEBROKER_HMIADAPTER - , mb_adapter_(NULL) - , message_broker_(NULL) - , message_broker_server_(NULL) - , mb_thread_(NULL) - , mb_server_thread_(NULL) - , mb_adapter_thread_(NULL) + , mb_adapter_(NULL) + , message_broker_(NULL) + , message_broker_server_(NULL) + , mb_thread_(NULL) + , mb_server_thread_(NULL) + , mb_adapter_thread_(NULL) #endif // MESSAGEBROKER_HMIADAPTER - , components_started_(false) -{ } +{ +} bool LifeCycle::StartComponents() { - LOG4CXX_INFO(logger_, "LifeCycle::StartComponents()"); - transport_manager_ = - transport_manager::TransportManagerDefault::instance(); - DCHECK(transport_manager_ != NULL); + LOG4CXX_AUTO_TRACE(logger_); + + DCHECK(!transport_manager_); + transport_manager_ = transport_manager::TransportManagerDefault::instance(); + DCHECK(transport_manager_); + DCHECK(!connection_handler_); + connection_handler_ = new connection_handler::ConnectionHandlerImpl( + *profile::Profile::instance(), + *transport_manager_); + DCHECK(connection_handler_); + + DCHECK(!protocol_handler_); protocol_handler_ = - new protocol_handler::ProtocolHandlerImpl(transport_manager_, - profile::Profile::instance()->message_frequency_time(), - profile::Profile::instance()->message_frequency_count(), - profile::Profile::instance()->malformed_message_filtering(), - profile::Profile::instance()->malformed_frequency_time(), - profile::Profile::instance()->malformed_frequency_count()); - DCHECK(protocol_handler_ != NULL); - - connection_handler_ = - connection_handler::ConnectionHandlerImpl::instance(); - DCHECK(connection_handler_ != NULL); - - app_manager_ = - application_manager::ApplicationManagerImpl::instance(); - DCHECK(app_manager_ != NULL); + new protocol_handler::ProtocolHandlerImpl(*(profile::Profile::instance()), + *connection_handler_, + *connection_handler_, + *transport_manager_); + DCHECK(protocol_handler_); + + DCHECK(!app_manager_); + app_manager_ = application_manager::ApplicationManagerImpl::instance(); + DCHECK(app_manager_); + if (!app_manager_->Init()) { LOG4CXX_ERROR(logger_, "Application manager init failed."); return false; } - hmi_handler_ = - hmi_message_handler::HMIMessageHandlerImpl::instance(); - DCHECK(hmi_handler_ != NULL) + DCHECK(!hmi_handler_) + hmi_handler_ = new hmi_message_handler::HMIMessageHandlerImpl( + *(profile::Profile::instance())); + DCHECK(hmi_handler_) #ifdef ENABLE_SECURITY security_manager_ = new security_manager::SecurityManagerImpl(); + crypto_manager_ = new security_manager::CryptoManagerImpl( + utils::MakeShared<security_manager::CryptoManagerSettingsImpl>( + *(profile::Profile::instance()), + policy::PolicyHandler::instance()->RetrieveCertificate())); + protocol_handler_->AddProtocolObserver(security_manager_); + protocol_handler_->set_security_manager(security_manager_); - // FIXME(EZamakhov): move to Config or in Sm initialization method - std::string cert_filename; - profile::Profile::instance()->ReadStringValue( - &cert_filename, "", - security_manager::SecurityManagerImpl::ConfigSection(), "CertificatePath"); - - std::string ssl_mode; - profile::Profile::instance()->ReadStringValue( - &ssl_mode, "CLIENT", security_manager::SecurityManagerImpl::ConfigSection(), "SSLMode"); - crypto_manager_ = new security_manager::CryptoManagerImpl(); - - std::string key_filename; - profile::Profile::instance()->ReadStringValue( - &key_filename, "", security_manager::SecurityManagerImpl::ConfigSection(), "KeyPath"); - - std::string ciphers_list; - profile::Profile::instance()->ReadStringValue( - &ciphers_list, SSL_TXT_ALL, security_manager::SecurityManagerImpl::ConfigSection(), "CipherList"); - - bool verify_peer; - profile::Profile::instance()->ReadBoolValue( - &verify_peer, false, security_manager::SecurityManagerImpl::ConfigSection(), "VerifyPeer"); - - std::string protocol_name; - profile::Profile::instance()->ReadStringValue( - &protocol_name, "TLSv1.2", security_manager::SecurityManagerImpl::ConfigSection(), "Protocol"); - - security_manager::Protocol protocol; - if (protocol_name == "TLSv1.0") { - protocol = security_manager::TLSv1; - } else if (protocol_name == "TLSv1.1") { - protocol = security_manager::TLSv1_1; - } else if (protocol_name == "TLSv1.2") { - protocol = security_manager::TLSv1_2; - } else if (protocol_name == "SSLv3") { - protocol = security_manager::SSLv3; - } else { - LOG4CXX_ERROR(logger_, "Unknown protocol: " << protocol_name); - return false; - } + security_manager_->set_session_observer(connection_handler_); + security_manager_->set_protocol_handler(protocol_handler_); + security_manager_->set_crypto_manager(crypto_manager_); + security_manager_->AddListener(app_manager_); - if (!crypto_manager_->Init( - ssl_mode == "SERVER" ? security_manager::SERVER : security_manager::CLIENT, - protocol, - cert_filename, - key_filename, - ciphers_list, - verify_peer)) { + app_manager_->AddPolicyObserver(crypto_manager_); + if (!crypto_manager_->Init()) { LOG4CXX_ERROR(logger_, "CryptoManager initialization fail."); return false; } @@ -181,30 +153,21 @@ bool LifeCycle::StartComponents() { media_manager_ = media_manager::MediaManagerImpl::instance(); - protocol_handler_->set_session_observer(connection_handler_); protocol_handler_->AddProtocolObserver(media_manager_); protocol_handler_->AddProtocolObserver(app_manager_); -#ifdef ENABLE_SECURITY - protocol_handler_->AddProtocolObserver(security_manager_); - protocol_handler_->set_security_manager(security_manager_); -#endif // ENABLE_SECURITY + media_manager_->SetProtocolHandler(protocol_handler_); - connection_handler_->set_transport_manager(transport_manager_); connection_handler_->set_protocol_handler(protocol_handler_); connection_handler_->set_connection_handler_observer(app_manager_); -#ifdef ENABLE_SECURITY - security_manager_->set_session_observer(connection_handler_); - security_manager_->set_protocol_handler(protocol_handler_); - security_manager_->set_crypto_manager(crypto_manager_); -#endif // ENABLE_SECURITY - - // it is important to initialise TimeTester before TM to listen TM Adapters -#ifdef TIME_TESTER - time_tester_ = new time_tester::TimeManager(); - time_tester_->Init(protocol_handler_); -#endif // TIME_TESTER +// it is important to initialise TelemetryMonitor before TM to listen TM Adapters +#ifdef TELEMETRY_MONITOR + telemetry_monitor_ = new telemetry_monitor::TelemetryMonitor(profile::Profile::instance()->server_address(), + profile::Profile::instance()->time_testing_port()); + telemetry_monitor_->Start(); + telemetry_monitor_->Init(protocol_handler_, app_manager_, transport_manager_); +#endif // TELEMETRY_MONITOR // It's important to initialise TM after setting up listener chain // [TM -> CH -> AM], otherwise some events from TM could arrive at nowhere app_manager_->set_protocol_handler(protocol_handler_); @@ -215,24 +178,22 @@ bool LifeCycle::StartComponents() { // start transport manager transport_manager_->Visibility(true); - components_started_ = true; return true; } #ifdef MESSAGEBROKER_HMIADAPTER bool LifeCycle::InitMessageSystem() { - message_broker_ = - NsMessageBroker::CMessageBroker::getInstance(); + DCHECK(!message_broker_) + message_broker_ = NsMessageBroker::CMessageBroker::getInstance(); if (!message_broker_) { LOG4CXX_FATAL(logger_, " Wrong pMessageBroker pointer!"); return false; } - message_broker_server_ = - new NsMessageBroker::TcpServer( - profile::Profile::instance()->server_address(), - profile::Profile::instance()->server_port(), - message_broker_); + message_broker_server_ = new NsMessageBroker::TcpServer( + profile::Profile::instance()->server_address(), + profile::Profile::instance()->server_port(), + message_broker_); if (!message_broker_server_) { LOG4CXX_FATAL(logger_, " Wrong pJSONRPC20Server pointer!"); return false; @@ -257,43 +218,44 @@ bool LifeCycle::InitMessageSystem() { LOG4CXX_INFO(logger_, " Message broker server listen successful!"); } - mb_adapter_ = - new hmi_message_handler::MessageBrokerAdapter( - hmi_message_handler::HMIMessageHandlerImpl::instance(), - profile::Profile::instance()->server_address(), - profile::Profile::instance()->server_port()); - - hmi_message_handler::HMIMessageHandlerImpl::instance()->AddHMIMessageAdapter( - mb_adapter_); - if (!mb_adapter_->Connect()) { - LOG4CXX_FATAL(logger_, "Cannot connect to remote peer!"); - return false; - } + mb_adapter_ = new hmi_message_handler::MessageBrokerAdapter( + hmi_handler_, + profile::Profile::instance()->server_address(), + profile::Profile::instance()->server_port()); + + hmi_handler_->AddHMIMessageAdapter(mb_adapter_); + if (!mb_adapter_->Connect()) { + LOG4CXX_FATAL(logger_, "Cannot connect to remote peer!"); + return false; + } LOG4CXX_INFO(logger_, "Start CMessageBroker thread!"); mb_thread_ = new System::Thread( - new System::ThreadArgImpl<NsMessageBroker::CMessageBroker>( - *message_broker_, &NsMessageBroker::CMessageBroker::MethodForThread, - NULL)); + new System::ThreadArgImpl<NsMessageBroker::CMessageBroker>( + *message_broker_, + &NsMessageBroker::CMessageBroker::MethodForThread, + NULL)); mb_thread_->Start(false); // Thread can be named only when started because before that point // thread doesn't have valid Id to associate name with NameMessageBrokerThread(*mb_thread_, "MessageBroker"); LOG4CXX_INFO(logger_, "Start MessageBroker TCP server thread!"); - mb_server_thread_ = new System::Thread( - new System::ThreadArgImpl<NsMessageBroker::TcpServer>( - *message_broker_server_, &NsMessageBroker::TcpServer::MethodForThread, - NULL)); + mb_server_thread_ = + new System::Thread(new System::ThreadArgImpl<NsMessageBroker::TcpServer>( + *message_broker_server_, + &NsMessageBroker::TcpServer::MethodForThread, + NULL)); mb_server_thread_->Start(false); NameMessageBrokerThread(*mb_server_thread_, "MB TCPServer"); LOG4CXX_INFO(logger_, "StartAppMgr JSONRPC 2.0 controller receiver thread!"); - mb_adapter_thread_ = new System::Thread( - new System::ThreadArgImpl<hmi_message_handler::MessageBrokerAdapter>( - *mb_adapter_, - &hmi_message_handler::MessageBrokerAdapter::SubscribeAndBeginReceiverThread, - NULL)); + mb_adapter_thread_ = new System::Thread( + new System::ThreadArgImpl<hmi_message_handler::MessageBrokerAdapter>( + *mb_adapter_, + &hmi_message_handler::MessageBrokerAdapter:: + SubscribeAndBeginReceiverThread, + NULL)); mb_adapter_thread_->Start(false); NameMessageBrokerThread(*mb_adapter_thread_, "MB Adapter"); @@ -308,10 +270,10 @@ bool LifeCycle::InitMessageSystem() { */ bool LifeCycle::InitMessageSystem() { dbus_adapter_ = new hmi_message_handler::DBusMessageAdapter( - hmi_message_handler::HMIMessageHandlerImpl::instance()); + hmi_message_handler::HMIMessageHandlerImpl::instance()); hmi_message_handler::HMIMessageHandlerImpl::instance()->AddHMIMessageAdapter( - dbus_adapter_); + dbus_adapter_); if (!dbus_adapter_->Init()) { LOG4CXX_FATAL(logger_, "Cannot init DBus service!"); return false; @@ -321,10 +283,10 @@ bool LifeCycle::InitMessageSystem() { LOG4CXX_INFO(logger_, "Start DBusMessageAdapter thread!"); dbus_adapter_thread_ = new System::Thread( - new System::ThreadArgImpl<hmi_message_handler::DBusMessageAdapter>( - *dbus_adapter_, - &hmi_message_handler::DBusMessageAdapter::MethodForReceiverThread, - NULL)); + new System::ThreadArgImpl<hmi_message_handler::DBusMessageAdapter>( + *dbus_adapter_, + &hmi_message_handler::DBusMessageAdapter::MethodForReceiverThread, + NULL)); dbus_adapter_thread_->Start(false); return true; @@ -334,88 +296,97 @@ bool LifeCycle::InitMessageSystem() { #ifdef MQUEUE_HMIADAPTER bool LifeCycle::InitMessageSystem() { hmi_message_adapter_ = new hmi_message_handler::MqueueAdapter( - hmi_message_handler::HMIMessageHandlerImpl::instance()); + hmi_message_handler::HMIMessageHandlerImpl::instance()); hmi_message_handler::HMIMessageHandlerImpl::instance()->AddHMIMessageAdapter( - hmi_message_adapter_); + hmi_message_adapter_); return true; } #endif // MQUEUE_HMIADAPTER namespace { - void sig_handler(int sig) { - // Do nothing - } - - void agony(int sig) { -// these actions are not signal safe -// (in case logger is on) -// but they cannot be moved to a separate thread -// because the application most probably will crash as soon as this handler returns -// -// the application is anyway about to crash - LOG4CXX_FATAL(logger_, "Stopping application due to segmentation fault"); -#ifdef ENABLE_LOG - logger::LogMessageLoopThread::destroy(); -#endif + switch(sig) { + case SIGINT: + LOG4CXX_DEBUG(logger_, "SIGINT signal has been caught"); + break; + case SIGTERM: + LOG4CXX_DEBUG(logger_, "SIGTERM signal has been caught"); + break; + case SIGSEGV: + LOG4CXX_DEBUG(logger_, "SIGSEGV signal has been caught"); + FLUSH_LOGGER(); + // exit need to prevent endless sending SIGSEGV + // http://stackoverflow.com/questions/2663456/how-to-write-a-signal-handler-to-catch-sigsegv + abort(); + default: + LOG4CXX_DEBUG(logger_, "Unexpected signal has been caught"); + exit(EXIT_FAILURE); + } } - } // namespace void LifeCycle::Run() { - // First, register signal handlers - ::utils::SubscribeToTerminateSignal(&sig_handler); - ::utils::SubscribeToFaultSignal(&agony); - // Now wait for any signal - pause(); + LOG4CXX_AUTO_TRACE(logger_); + // Register signal handlers and wait sys signals + // from OS + if (!utils::WaitTerminationSignals(&sig_handler)) { + LOG4CXX_FATAL(logger_, "Fail to catch system signal!"); + } } - void LifeCycle::StopComponents() { - if (!components_started_) { - LOG4CXX_TRACE(logger_, "exit"); - LOG4CXX_ERROR(logger_, "Components wasn't started"); - return; - } + LOG4CXX_AUTO_TRACE(logger_); + + DCHECK_OR_RETURN_VOID(hmi_handler_); hmi_handler_->set_message_observer(NULL); + + DCHECK_OR_RETURN_VOID(connection_handler_); connection_handler_->set_connection_handler_observer(NULL); + + DCHECK_OR_RETURN_VOID(protocol_handler_); protocol_handler_->RemoveProtocolObserver(app_manager_); + + DCHECK_OR_RETURN_VOID(app_manager_); app_manager_->Stop(); LOG4CXX_INFO(logger_, "Stopping Protocol Handler"); + DCHECK_OR_RETURN_VOID(protocol_handler_); protocol_handler_->RemoveProtocolObserver(media_manager_); + #ifdef ENABLE_SECURITY protocol_handler_->RemoveProtocolObserver(security_manager_); + if (security_manager_) { + security_manager_->RemoveListener(app_manager_); + LOG4CXX_INFO(logger_, "Destroying Crypto Manager"); + delete crypto_manager_; + LOG4CXX_INFO(logger_, "Destroying Security Manager"); + delete security_manager_; + } #endif // ENABLE_SECURITY protocol_handler_->Stop(); LOG4CXX_INFO(logger_, "Destroying Media Manager"); + DCHECK_OR_RETURN_VOID(media_manager_); media_manager_->SetProtocolHandler(NULL); media_manager::MediaManagerImpl::destroy(); LOG4CXX_INFO(logger_, "Destroying Transport Manager."); + DCHECK_OR_RETURN_VOID(transport_manager_); transport_manager_->Visibility(false); transport_manager_->Stop(); transport_manager::TransportManagerDefault::destroy(); LOG4CXX_INFO(logger_, "Stopping Connection Handler."); - connection_handler::ConnectionHandlerImpl::instance()->Stop(); + DCHECK_OR_RETURN_VOID(connection_handler_); + connection_handler_->Stop(); LOG4CXX_INFO(logger_, "Destroying Protocol Handler"); + DCHECK_OR_RETURN_VOID(protocol_handler_); delete protocol_handler_; LOG4CXX_INFO(logger_, "Destroying Connection Handler."); - connection_handler::ConnectionHandlerImpl::destroy(); - -#ifdef ENABLE_SECURITY - LOG4CXX_INFO(logger_, "Destroying Crypto Manager"); - crypto_manager_->Finish(); - delete crypto_manager_; - - LOG4CXX_INFO(logger_, "Destroying Security Manager"); - delete security_manager_; -#endif // ENABLE_SECURITY + delete connection_handler_; LOG4CXX_INFO(logger_, "Destroying Last State"); resumption::LastState::destroy(); @@ -424,6 +395,7 @@ void LifeCycle::StopComponents() { application_manager::ApplicationManagerImpl::destroy(); LOG4CXX_INFO(logger_, "Destroying HMI Message Handler and MB adapter."); + #ifdef DBUS_HMIADAPTER if (dbus_adapter_) { if (hmi_handler_) { @@ -438,27 +410,22 @@ void LifeCycle::StopComponents() { delete dbus_adapter_; } #endif // DBUS_HMIADAPTER + #ifdef MESSAGEBROKER_HMIADAPTER + DCHECK_OR_RETURN_VOID(mb_adapter_); hmi_handler_->RemoveHMIMessageAdapter(mb_adapter_); - if (mb_adapter_) { - mb_adapter_->unregisterController(); - mb_adapter_->Close(); - mb_adapter_->exitReceivingThread(); - if (mb_adapter_thread_) { - mb_adapter_thread_->Join(); - } - delete mb_adapter_; - } - hmi_message_handler::HMIMessageHandlerImpl::destroy(); + mb_adapter_->unregisterController(); + mb_adapter_->exitReceivingThread(); if (mb_adapter_thread_) { mb_adapter_thread_->Stop(); + mb_adapter_thread_->Join(); delete mb_adapter_thread_; } + delete mb_adapter_; -#endif // MESSAGEBROKER_HMIADAPTER - + DCHECK_OR_RETURN_VOID(hmi_handler_); + delete hmi_handler_; -#ifdef MESSAGEBROKER_HMIADAPTER LOG4CXX_INFO(logger_, "Destroying Message Broker"); if (mb_server_thread_) { mb_server_thread_->Stop(); @@ -470,9 +437,14 @@ void LifeCycle::StopComponents() { mb_thread_->Join(); delete mb_thread_; } - message_broker_server_->Close(); + if (message_broker_server_) { + message_broker_server_->Close(); + } delete message_broker_server_; - message_broker_->stopMessageBroker(); + + if (message_broker_) { + message_broker_->stopMessageBroker(); + } networking::cleanup(); #endif // MESSAGEBROKER_HMIADAPTER @@ -480,16 +452,14 @@ void LifeCycle::StopComponents() { delete hmi_message_adapter_; hmi_message_adapter_ = NULL; -#ifdef TIME_TESTER +#ifdef TELEMETRY_MONITOR // It's important to delete tester Obcervers after TM adapters destruction - if (time_tester_) { - time_tester_->Stop(); - delete time_tester_; - time_tester_ = NULL; + if (telemetry_monitor_) { + telemetry_monitor_->Stop(); + delete telemetry_monitor_; + telemetry_monitor_ = NULL; } -#endif // TIME_TESTER - components_started_ = false; - LOG4CXX_TRACE(logger_, "exit"); +#endif // TELEMETRY_MONITOR } } // namespace main_namespace |