[icinga-checkins] icinga.org: icinga2/next: Error Messages: Catch and log all Socket class exceptions.

git at icinga.org git at icinga.org
Thu Jun 5 18:29:54 CEST 2014


Module: icinga2
Branch: next
Commit: 0c021d94cb2a10dda1dfa4d3f61ce23f93a33ad0
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=0c021d94cb2a10dda1dfa4d3f61ce23f93a33ad0

Author: Michael Friedrich <michael.friedrich at netways.de>
Date:   Thu Jun  5 16:17:53 2014 +0200

Error Messages: Catch and log all Socket class exceptions.

Refs #6070

---

 components/livestatus/livestatuslistener.cpp |   12 ++++++----
 components/livestatus/livestatusquery.cpp    |   14 ++++-------
 components/perfdata/graphitewriter.cpp       |   22 ++++++++++--------
 lib/base/socket.cpp                          |   18 +++++++++++++--
 lib/base/tlsstream.cpp                       |   32 +++++++++++++++++++-------
 lib/remote/apilistener.cpp                   |    9 +++++---
 6 files changed, 69 insertions(+), 38 deletions(-)

diff --git a/components/livestatus/livestatuslistener.cpp b/components/livestatus/livestatuslistener.cpp
index 655f5af..57bbdff 100644
--- a/components/livestatus/livestatuslistener.cpp
+++ b/components/livestatus/livestatuslistener.cpp
@@ -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.");
+		}
 	}
 }
 
diff --git a/components/livestatus/livestatusquery.cpp b/components/livestatus/livestatusquery.cpp
index 00e5c41..134b418 100644
--- a/components/livestatus/livestatusquery.cpp
+++ b/components/livestatus/livestatusquery.cpp
@@ -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.");
 	}
 }
 
diff --git a/components/perfdata/graphitewriter.cpp b/components/perfdata/graphitewriter.cpp
index 5472ad9..6a4763e 100644
--- a/components/perfdata/graphitewriter.cpp
+++ b/components/perfdata/graphitewriter.cpp
@@ -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();
 	}
diff --git a/lib/base/socket.cpp b/lib/base/socket.cpp
index ca1eab8..7057cc5 100644
--- a/lib/base/socket.cpp
+++ b/lib/base/socket.cpp
@@ -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;
 }
 
 /**
diff --git a/lib/base/tlsstream.cpp b/lib/base/tlsstream.cpp
index caa9ace..4d8883a 100644
--- a/lib/base/tlsstream.cpp
+++ b/lib/base/tlsstream.cpp
@@ -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;
diff --git a/lib/remote/apilistener.cpp b/lib/remote/apilistener.cpp
index 1e6b364..e74841c 100644
--- a/lib/remote/apilistener.cpp
+++ b/lib/remote/apilistener.cpp
@@ -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.");
+		}
 	}
 }
 



More information about the icinga-checkins mailing list