Error Messages: Catch and log all Socket class exceptions.

Refs #6070
This commit is contained in:
Michael Friedrich 2014-06-05 16:17:53 +02:00
parent 6a080edf80
commit 0c021d94cb
6 changed files with 69 additions and 38 deletions

View File

@ -131,11 +131,13 @@ void LivestatusListener::ServerThreadProc(const Socket::Ptr& server)
server->Listen(); server->Listen();
for (;;) { for (;;) {
Socket::Ptr client = server->Accept(); try {
Socket::Ptr client = server->Accept();
Log(LogNotice, "LivestatusListener", "Client connected"); Log(LogNotice, "LivestatusListener", "Client connected");
Utility::QueueAsyncCallback(boost::bind(&LivestatusListener::ClientHandler, this, client));
Utility::QueueAsyncCallback(boost::bind(&LivestatusListener::ClientHandler, this, client)); } catch (std::exception&) {
Log(LogCritical, "ListenerListener", "Cannot accept new connection.");
}
} }
} }

View File

@ -517,11 +517,8 @@ void LivestatusQuery::SendResponse(const Stream::Ptr& stream, int code, const St
if (m_ResponseHeader == "fixed16" || code == LivestatusErrorOK) { if (m_ResponseHeader == "fixed16" || code == LivestatusErrorOK) {
try { try {
stream->Write(data.CStr(), data.GetLength()); stream->Write(data.CStr(), data.GetLength());
} catch (const std::exception& ex) { } catch (const std::exception&) {
std::ostringstream info; Log(LogCritical, "LivestatusQuery", "Cannot write to tcp socket.");
info << "Exception thrown while writing to the livestatus socket: " << std::endl
<< DiagnosticInformation(ex);
Log(LogCritical, "LivestatusQuery", info.str());
} }
} }
} }
@ -537,11 +534,8 @@ void LivestatusQuery::PrintFixed16(const Stream::Ptr& stream, int code, const St
try { try {
stream->Write(header.CStr(), header.GetLength()); stream->Write(header.CStr(), header.GetLength());
} catch (const std::exception& ex) { } catch (const std::exception&) {
std::ostringstream info; Log(LogCritical, "LivestatusQuery", "Cannot write to tcp socket.");
info << "Exception thrown while writing to the livestatus socket: " << std::endl
<< DiagnosticInformation(ex);
Log(LogCritical, "LivestatusQuery", info.str());
} }
} }

View File

@ -77,17 +77,23 @@ void GraphiteWriter::ReconnectTimerHandler(void)
try { try {
if (m_Stream) { if (m_Stream) {
m_Stream->Write("\n", 1); 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; return;
} }
} catch (const std::exception& ex) { } catch (const std::exception&) {
Log(LogWarning, "GraphiteWriter", "GraphiteWriter socket on host '" + GetHost() + "' port '" + GetPort() + "' gone. Attempting to reconnect."); Log(LogWarning, "GraphiteWriter", "Socket on host '" + GetHost() + "' port '" + GetPort() + "' gone. Attempting to reconnect.");
} }
TcpSocket::Ptr socket = make_shared<TcpSocket>(); TcpSocket::Ptr socket = make_shared<TcpSocket>();
Log(LogNotice, "GraphiteWriter", "GraphiteWriter: Reconnect to tcp socket on host '" + GetHost() + "' port '" + GetPort() + "'."); Log(LogNotice, "GraphiteWriter", "Reconnect to tcp socket on host '" + GetHost() + "' port '" + GetPort() + "'.");
socket->Connect(GetHost(), 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); m_Stream = make_shared<NetworkStream>(socket);
} }
@ -173,11 +179,7 @@ void GraphiteWriter::SendMetric(const String& prefix, const String& name, double
try { try {
m_Stream->Write(metric.CStr(), metric.GetLength()); m_Stream->Write(metric.CStr(), metric.GetLength());
} catch (const std::exception& ex) { } catch (const std::exception& ex) {
std::ostringstream msgbuf; Log(LogCritical, "GraphiteWriter", "Cannot write to tcp socket on host '" + GetHost() + "' port '" + GetPort() + "'.");
msgbuf << "Exception thrown while writing to the Graphite socket: " << std::endl
<< DiagnosticInformation(ex);
Log(LogCritical, "GraphiteWriter", msgbuf.str());
m_Stream.reset(); m_Stream.reset();
} }

View File

@ -186,7 +186,14 @@ String Socket::GetClientAddress(void)
#endif /* _WIN32 */ #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 */ #endif /* _WIN32 */
} }
return GetAddressFromSockaddr((sockaddr *)&sin, len); String address;
try {
address = GetAddressFromSockaddr((sockaddr *)&sin, len);
} catch (std::exception&) {
/* already logged */
}
return address;
} }
/** /**

View File

@ -108,10 +108,14 @@ void TlsStream::Handshake(void)
switch (err) { switch (err) {
case SSL_ERROR_WANT_READ: case SSL_ERROR_WANT_READ:
m_Socket->Poll(true, false); try {
m_Socket->Poll(true, false);
} catch (std::exception&) {}
continue; continue;
case SSL_ERROR_WANT_WRITE: case SSL_ERROR_WANT_WRITE:
m_Socket->Poll(false, true); try {
m_Socket->Poll(false, true);
} catch (std::exception&) {}
continue; continue;
case SSL_ERROR_ZERO_RETURN: case SSL_ERROR_ZERO_RETURN:
Close(); Close();
@ -149,10 +153,14 @@ size_t TlsStream::Read(void *buffer, size_t count)
if (rc <= 0) { if (rc <= 0) {
switch (err) { switch (err) {
case SSL_ERROR_WANT_READ: case SSL_ERROR_WANT_READ:
m_Socket->Poll(true, false); try {
m_Socket->Poll(true, false);
} catch (std::exception&) {}
continue; continue;
case SSL_ERROR_WANT_WRITE: case SSL_ERROR_WANT_WRITE:
m_Socket->Poll(false, true); try {
m_Socket->Poll(false, true);
} catch (std::exception&) {}
continue; continue;
case SSL_ERROR_ZERO_RETURN: case SSL_ERROR_ZERO_RETURN:
Close(); Close();
@ -192,10 +200,14 @@ void TlsStream::Write(const void *buffer, size_t count)
if (rc <= 0) { if (rc <= 0) {
switch (err) { switch (err) {
case SSL_ERROR_WANT_READ: case SSL_ERROR_WANT_READ:
m_Socket->Poll(true, false); try {
m_Socket->Poll(true, false);
} catch (std::exception&) {}
continue; continue;
case SSL_ERROR_WANT_WRITE: case SSL_ERROR_WANT_WRITE:
m_Socket->Poll(false, true); try {
m_Socket->Poll(false, true);
} catch (std::exception&) {}
continue; continue;
case SSL_ERROR_ZERO_RETURN: case SSL_ERROR_ZERO_RETURN:
Close(); Close();
@ -238,10 +250,14 @@ void TlsStream::Close(void)
switch (err) { switch (err) {
case SSL_ERROR_WANT_READ: case SSL_ERROR_WANT_READ:
m_Socket->Poll(true, false); try {
m_Socket->Poll(true, false);
} catch (std::exception&) {}
continue; continue;
case SSL_ERROR_WANT_WRITE: case SSL_ERROR_WANT_WRITE:
m_Socket->Poll(false, true); try {
m_Socket->Poll(false, true);
} catch (std::exception&) {}
continue; continue;
default: default:
goto close_socket; goto close_socket;

View File

@ -187,9 +187,12 @@ void ApiListener::ListenerThreadProc(const Socket::Ptr& server)
server->Listen(); server->Listen();
for (;;) { for (;;) {
Socket::Ptr client = server->Accept(); try {
Socket::Ptr client = server->Accept();
Utility::QueueAsyncCallback(boost::bind(&ApiListener::NewClientHandler, this, client, RoleServer)); Utility::QueueAsyncCallback(boost::bind(&ApiListener::NewClientHandler, this, client, RoleServer));
} catch (std::exception&) {
Log(LogCritical, "ApiListener", "Cannot accept new connection.");
}
} }
} }