]> granicus.if.org Git - icinga2/commitdiff
Error Messages: Catch and log all Socket class exceptions.
authorMichael Friedrich <michael.friedrich@netways.de>
Thu, 5 Jun 2014 14:17:53 +0000 (16:17 +0200)
committerMichael Friedrich <michael.friedrich@netways.de>
Thu, 5 Jun 2014 14:17:53 +0000 (16:17 +0200)
Refs #6070

components/livestatus/livestatuslistener.cpp
components/livestatus/livestatusquery.cpp
components/perfdata/graphitewriter.cpp
lib/base/socket.cpp
lib/base/tlsstream.cpp
lib/remote/apilistener.cpp

index 655f5af5c88e96e8d09f249f326ebcd2a7991beb..57bbdffcda0dbddec689ee62493a15e85bd1ddef 100644 (file)
@@ -131,11 +131,13 @@ void LivestatusListener::ServerThreadProc(const Socket::Ptr& server)
        server->Listen();
 
        for (;;) {
-               Socket::Ptr client = server->Accept();
-
-               Log(LogNotice, "LivestatusListener", "Client connected");
-
-               Utility::QueueAsyncCallback(boost::bind(&LivestatusListener::ClientHandler, this, client));
+               try {
+                       Socket::Ptr client = server->Accept();
+                       Log(LogNotice, "LivestatusListener", "Client connected");
+                       Utility::QueueAsyncCallback(boost::bind(&LivestatusListener::ClientHandler, this, client));
+               } catch (std::exception&) {
+                       Log(LogCritical, "ListenerListener", "Cannot accept new connection.");
+               }
        }
 }
 
index 00e5c4176a578a6d73fe3bfef601d71d088034e3..134b418e22cf0d13ea88215d3ea463738fc51c5e 100644 (file)
@@ -517,11 +517,8 @@ void LivestatusQuery::SendResponse(const Stream::Ptr& stream, int code, const St
        if (m_ResponseHeader == "fixed16" || code == LivestatusErrorOK) {
                try {
                        stream->Write(data.CStr(), data.GetLength());
-               } catch (const std::exception& ex) {
-                       std::ostringstream info;
-                       info << "Exception thrown while writing to the livestatus socket: " << std::endl
-                            << DiagnosticInformation(ex);
-                       Log(LogCritical, "LivestatusQuery", info.str());
+               } catch (const std::exception&) {
+                       Log(LogCritical, "LivestatusQuery", "Cannot write to tcp socket.");
                }
        }
 }
@@ -537,11 +534,8 @@ void LivestatusQuery::PrintFixed16(const Stream::Ptr& stream, int code, const St
 
        try {
                stream->Write(header.CStr(), header.GetLength());
-       } catch (const std::exception& ex) {
-               std::ostringstream info;
-               info << "Exception thrown while writing to the livestatus socket: " << std::endl
-                    << DiagnosticInformation(ex);
-               Log(LogCritical, "LivestatusQuery", info.str());
+       } catch (const std::exception&) {
+               Log(LogCritical, "LivestatusQuery", "Cannot write to tcp socket.");
        }
 }
 
index 5472ad982a92b8b20ac1f43786e5e93eb21907db..6a4763e6190893db70770436cdbd181333d4c3bf 100644 (file)
@@ -77,17 +77,23 @@ void GraphiteWriter::ReconnectTimerHandler(void)
        try {
                if (m_Stream) {
                        m_Stream->Write("\n", 1);
-                       Log(LogNotice, "GraphiteWriter", "GraphiteWriter already connected on socket on host '" + GetHost() + "' port '" + GetPort() + "'.");
+                       Log(LogNotice, "GraphiteWriter", "Already connected on socket on host '" + GetHost() + "' port '" + GetPort() + "'.");
                        return;
                }
-       } catch (const std::exception& ex) {
-               Log(LogWarning, "GraphiteWriter", "GraphiteWriter socket on host '" + GetHost() + "' port '" + GetPort() + "' gone. Attempting to reconnect.");
+       } catch (const std::exception&) {
+               Log(LogWarning, "GraphiteWriter", "Socket on host '" + GetHost() + "' port '" + GetPort() + "' gone. Attempting to reconnect.");
        }
 
        TcpSocket::Ptr socket = make_shared<TcpSocket>();
 
-       Log(LogNotice, "GraphiteWriter", "GraphiteWriter: Reconnect to tcp socket on host '" + GetHost() + "' port '" + GetPort() + "'.");
-       socket->Connect(GetHost(), GetPort());
+       Log(LogNotice, "GraphiteWriter", "Reconnect to tcp socket on host '" + GetHost() + "' port '" + GetPort() + "'.");
+
+       try {
+               socket->Connect(GetHost(), GetPort());
+       } catch (std::exception&) {
+               Log(LogCritical, "GraphiteWriter", "Can't connect to tcp socket on host '" + GetHost() + "' port '" + GetPort() + "'.");
+               return;
+       }
 
        m_Stream = make_shared<NetworkStream>(socket);
 }
@@ -173,11 +179,7 @@ void GraphiteWriter::SendMetric(const String& prefix, const String& name, double
        try {
                m_Stream->Write(metric.CStr(), metric.GetLength());
        } catch (const std::exception& ex) {
-               std::ostringstream msgbuf;
-               msgbuf << "Exception thrown while writing to the Graphite socket: " << std::endl
-                      << DiagnosticInformation(ex);
-
-               Log(LogCritical, "GraphiteWriter", msgbuf.str());
+               Log(LogCritical, "GraphiteWriter", "Cannot write to tcp socket on host '" + GetHost() + "' port '" + GetPort() + "'.");
 
                m_Stream.reset();
        }
index ca1eab8a6a32744961249bda4b9ca2728af951f4..7057cc551695b291894f2156a6fe4e348731886c 100644 (file)
@@ -186,7 +186,14 @@ String Socket::GetClientAddress(void)
 #endif /* _WIN32 */
        }
 
-       return GetAddressFromSockaddr((sockaddr *)&sin, len);
+       String address;
+       try {
+               address = GetAddressFromSockaddr((sockaddr *)&sin, len);
+       } catch (std::exception&) {
+               /* already logged */
+       }
+
+       return address;
 }
 
 /**
@@ -221,7 +228,14 @@ String Socket::GetPeerAddress(void)
 #endif /* _WIN32 */
        }
 
-       return GetAddressFromSockaddr((sockaddr *)&sin, len);
+       String address;
+       try {
+               address = GetAddressFromSockaddr((sockaddr *)&sin, len);
+       } catch (std::exception&) {
+               /* already logged */
+       }
+
+       return address;
 }
 
 /**
index caa9ace84010954bc2d515d27a23ef5f3be6acb6..4d8883a623eb07aad5e9671d196766fe5547a1fd 100644 (file)
@@ -108,10 +108,14 @@ void TlsStream::Handshake(void)
 
                switch (err) {
                        case SSL_ERROR_WANT_READ:
-                               m_Socket->Poll(true, false);
+                               try {
+                                       m_Socket->Poll(true, false);
+                               } catch (std::exception&) {}
                                continue;
                        case SSL_ERROR_WANT_WRITE:
-                               m_Socket->Poll(false, true);
+                               try {
+                                       m_Socket->Poll(false, true);
+                               } catch (std::exception&) {}
                                continue;
                        case SSL_ERROR_ZERO_RETURN:
                                Close();
@@ -149,10 +153,14 @@ size_t TlsStream::Read(void *buffer, size_t count)
                if (rc <= 0) {
                        switch (err) {
                                case SSL_ERROR_WANT_READ:
-                                       m_Socket->Poll(true, false);
+                                       try {
+                                               m_Socket->Poll(true, false);
+                                       } catch (std::exception&) {}
                                        continue;
                                case SSL_ERROR_WANT_WRITE:
-                                       m_Socket->Poll(false, true);
+                                       try {
+                                               m_Socket->Poll(false, true);
+                                       } catch (std::exception&) {}
                                        continue;
                                case SSL_ERROR_ZERO_RETURN:
                                        Close();
@@ -192,10 +200,14 @@ void TlsStream::Write(const void *buffer, size_t count)
                if (rc <= 0) {
                        switch (err) {
                                case SSL_ERROR_WANT_READ:
-                                       m_Socket->Poll(true, false);
+                                       try {
+                                               m_Socket->Poll(true, false);
+                                       } catch (std::exception&) {}
                                        continue;
                                case SSL_ERROR_WANT_WRITE:
-                                       m_Socket->Poll(false, true);
+                                       try {
+                                               m_Socket->Poll(false, true);
+                                       } catch (std::exception&) {}
                                        continue;
                                case SSL_ERROR_ZERO_RETURN:
                                        Close();
@@ -238,10 +250,14 @@ void TlsStream::Close(void)
 
                switch (err) {
                        case SSL_ERROR_WANT_READ:
-                               m_Socket->Poll(true, false);
+                               try {
+                                       m_Socket->Poll(true, false);
+                               } catch (std::exception&) {}
                                continue;
                        case SSL_ERROR_WANT_WRITE:
-                               m_Socket->Poll(false, true);
+                               try {
+                                       m_Socket->Poll(false, true);
+                               } catch (std::exception&) {}
                                continue;
                        default:
                                goto close_socket;
index 1e6b364aa97c72689bf4f0cbb4e1b7ac29f53a1e..e74841cb9eca124e79f87d02dd7ffe386a523ff2 100644 (file)
@@ -187,9 +187,12 @@ void ApiListener::ListenerThreadProc(const Socket::Ptr& server)
        server->Listen();
 
        for (;;) {
-               Socket::Ptr client = server->Accept();
-
-               Utility::QueueAsyncCallback(boost::bind(&ApiListener::NewClientHandler, this, client, RoleServer));
+               try {
+                       Socket::Ptr client = server->Accept();
+                       Utility::QueueAsyncCallback(boost::bind(&ApiListener::NewClientHandler, this, client, RoleServer));
+               } catch (std::exception&) {
+                       Log(LogCritical, "ApiListener", "Cannot accept new connection.");
+               }
        }
 }