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();
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.");
}
}
}

View 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.");
}
}

View 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();
}

View 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;
}
/**

View 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;

View 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.");
}
}
}