]> granicus.if.org Git - icinga2/commitdiff
Bail early if ApiListener cannot be started
authorMichael Friedrich <michael.friedrich@netways.de>
Mon, 4 Aug 2014 14:34:17 +0000 (16:34 +0200)
committerMichael Friedrich <michael.friedrich@netways.de>
Mon, 4 Aug 2014 15:23:41 +0000 (17:23 +0200)
refs #6682

lib/remote/apilistener.cpp
lib/remote/apilistener.hpp
lib/remote/authority.cpp

index bdf3877930ce55373f216bb753c4d3dfa2377665..b404e1ef76565e3057644257e9360c024e20c9c7 100644 (file)
@@ -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<Timer>();
        m_Timer->OnTimerExpired.connect(boost::bind(&ApiListener::ApiTimerHandler, this));
@@ -129,6 +132,10 @@ shared_ptr<SSL_CTX> ApiListener::GetSSLContext(void) const
 Endpoint::Ptr ApiListener::GetMaster(void) const
 {
        Zone::Ptr zone = Zone::GetLocalZone();
+
+       if (!zone)
+               return Endpoint::Ptr();
+
        std::vector<String> 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<SSL_CTX> 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<String> names;
        BOOST_FOREACH(const Endpoint::Ptr& endpoint, DynamicType::GetObjects<Endpoint>())
index 8d95337d940f11d08e9072b9ddeb67fb1471e038..498f8af571acfeb9bd9276184bb974cc852e4f18 100644 (file)
@@ -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);
index a9aa49c5ea85f47b157280e194ac224ecf594ade..2c7159020ed7cc76f9ef0b1ccbe9b4c759000cbe 100644 (file)
@@ -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<Endpoint::Ptr> endpoints;