diff --git a/lib/remote/apilistener.cpp b/lib/remote/apilistener.cpp index bdf387793..b404e1ef7 100644 --- a/lib/remote/apilistener.cpp +++ b/lib/remote/apilistener.cpp @@ -102,7 +102,10 @@ void ApiListener::Start(void) } /* create the primary JSON-RPC listener */ - AddListener(GetBindPort()); + if (!AddListener(GetBindPort())) { + Log(LogCritical, "ApiListener", "Cannot add listener for port '" + Convert::ToString(GetBindPort()) + "'."); + Application::Exit(EXIT_FAILURE); + } m_Timer = make_shared(); m_Timer->OnTimerExpired.connect(boost::bind(&ApiListener::ApiTimerHandler, this)); @@ -129,6 +132,10 @@ shared_ptr ApiListener::GetSSLContext(void) const Endpoint::Ptr ApiListener::GetMaster(void) const { Zone::Ptr zone = Zone::GetLocalZone(); + + if (!zone) + return Endpoint::Ptr(); + std::vector names; BOOST_FOREACH(const Endpoint::Ptr& endpoint, zone->GetEndpoints()) @@ -142,7 +149,12 @@ Endpoint::Ptr ApiListener::GetMaster(void) const bool ApiListener::IsMaster(void) const { - return GetMaster()->GetName() == GetIdentity(); + Endpoint::Ptr master = GetMaster(); + + if (!master) + return false; + + return master->GetName() == GetIdentity(); } /** @@ -150,7 +162,7 @@ bool ApiListener::IsMaster(void) const * * @param service The port to listen on. */ -void ApiListener::AddListener(const String& service) +bool ApiListener::AddListener(const String& service) { ObjectLock olock(this); @@ -158,7 +170,7 @@ void ApiListener::AddListener(const String& service) if (!sslContext) { Log(LogCritical, "ApiListener", "SSL context is required for AddListener()"); - return; + return false; } std::ostringstream s; @@ -171,13 +183,15 @@ void ApiListener::AddListener(const String& service) server->Bind(service, AF_UNSPEC); } catch(std::exception&) { Log(LogCritical, "ApiListener", "Cannot bind tcp socket on '" + service + "'."); - return; + return false; } boost::thread thread(boost::bind(&ApiListener::ListenerThreadProc, this, server)); thread.detach(); m_Servers.insert(server); + + return true; } void ApiListener::ListenerThreadProc(const Socket::Ptr& server) @@ -209,7 +223,7 @@ void ApiListener::AddConnection(const Endpoint::Ptr& endpoint) shared_ptr sslContext = m_SSLContext; if (!sslContext) { - Log(LogCritical, "ApiListener", "SSL context is required for AddListener()"); + Log(LogCritical, "ApiListener", "SSL context is required for AddConnection()"); return; } } @@ -398,7 +412,10 @@ void ApiListener::ApiTimerHandler(void) Utility::FormatDateTime("%Y/%m/%d %H:%M:%S", ts)); } - Log(LogNotice, "ApiListener", "Current zone master: " + GetMaster()->GetName()); + Endpoint::Ptr master = GetMaster(); + + if (master) + Log(LogNotice, "ApiListener", "Current zone master: " + master->GetName()); std::vector names; BOOST_FOREACH(const Endpoint::Ptr& endpoint, DynamicType::GetObjects()) diff --git a/lib/remote/apilistener.hpp b/lib/remote/apilistener.hpp index 8d95337d9..498f8af57 100644 --- a/lib/remote/apilistener.hpp +++ b/lib/remote/apilistener.hpp @@ -79,7 +79,7 @@ private: void ApiTimerHandler(void); - void AddListener(const String& service); + bool AddListener(const String& service); void AddConnection(const Endpoint::Ptr& endpoint); void NewClientHandler(const Socket::Ptr& client, ConnectionRole role); diff --git a/lib/remote/authority.cpp b/lib/remote/authority.cpp index a9aa49c5e..2c7159020 100644 --- a/lib/remote/authority.cpp +++ b/lib/remote/authority.cpp @@ -42,6 +42,9 @@ static void AuthorityTimerHandler(void) return; Zone::Ptr my_zone = Zone::GetLocalZone(); + if (!my_zone) + return; + Endpoint::Ptr my_endpoint = Endpoint::GetLocalEndpoint(); std::vector endpoints;