From 3ac4722a92d2c88a667d8a8e90a68362e41f4dda Mon Sep 17 00:00:00 2001 From: Susant Sahani Date: Wed, 22 May 2024 19:00:11 +0530 Subject: [PATCH] TCP: Reconnect to remote server after a reset/closed connection by remote server closes https://github.com/systemd/systemd-netlogd/issues/104 --- src/netlog/netlog-manager.h | 1 + src/netlog/netlog-network.c | 38 ++++++++++++++++++++++++++----------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/netlog/netlog-manager.h b/src/netlog/netlog-manager.h index a922aff..9cb1058 100644 --- a/src/netlog/netlog-manager.h +++ b/src/netlog/netlog-manager.h @@ -78,6 +78,7 @@ struct Manager { bool keep_alive; bool no_delay; + bool connected; unsigned keep_alive_cnt; diff --git a/src/netlog/netlog-network.c b/src/netlog/netlog-network.c index db4e0c4..c0cacb1 100644 --- a/src/netlog/netlog-network.c +++ b/src/netlog/netlog-network.c @@ -86,6 +86,13 @@ static int protocol_send(Manager *m, struct iovec *iovec, unsigned n_iovec) { return r; } break; + case SYSLOG_TRANSMISSION_PROTOCOL_TCP: + r = network_send(m, iovec, n_iovec); + if (r < 0 && r != -EAGAIN) { + manager_close_network_socket(m); + return r; + } + break; default: return network_send(m, iovec, n_iovec); } @@ -297,9 +304,6 @@ int manager_push_to_network(Manager *m, assert(m); - if (!message) - return 0; - switch (m->protocol) { case SYSLOG_TRANSMISSION_PROTOCOL_DTLS: if (!m->dtls->connected) { @@ -316,6 +320,13 @@ int manager_push_to_network(Manager *m, return r; } break; + case SYSLOG_TRANSMISSION_PROTOCOL_TCP: + if (!m->connected) { + r = manager_open_network_socket(m); + if (r < 0) + return r; + } + break; default: break; } @@ -340,6 +351,7 @@ void manager_close_network_socket(Manager *m) { log_error_errno(errno, "Failed to shutdown netlog socket: %m"); } + m->connected = false; m->socket = safe_close(m->socket); } @@ -377,11 +389,16 @@ int manager_network_connect_socket(Manager *m) { if (r < 0) return r; + log_debug("Connecting to remote server: '%s'", pretty); + r = connect(m->socket, &m->address.sockaddr.sa, salen); if (r < 0 && errno != EINPROGRESS) - return log_error_errno(errno, "Failed to connect to remote server='%s'", pretty); + return log_error_errno(errno, "Failed to connect to remote server='%s': %m", pretty); - log_debug("Connected to remote server: '%s'", pretty); + if (errno != EINPROGRESS) + log_debug("Connected to remote server: '%s'", pretty); + else + log_debug("Connection in progress to remote server: '%s'", pretty); return 0; } @@ -407,6 +424,8 @@ int manager_open_network_socket(Manager *m) { if (m->socket < 0) return log_error_errno(errno, "Failed to create socket: %m");; + log_debug("Successfully created socket with fd='%d'", m->socket); + switch (m->protocol) { case SYSLOG_TRANSMISSION_PROTOCOL_UDP: { r = setsockopt_int(m->socket, IPPROTO_IP, IP_MULTICAST_LOOP, true); @@ -462,17 +481,14 @@ int manager_open_network_socket(Manager *m) { } r = fd_nonblock(m->socket, true); - if (r < 0) { - log_error_errno(errno, "Failed to set socket nonblock: %m"); - goto fail; - } + if (r < 0) + log_debug_errno(errno, "Failed to set socket='%d' nonblock: %m", m->socket); r = manager_network_connect_socket(m); if (r < 0) goto fail; - log_debug("Successfully created socket with fd='%d'", m->socket); - + m->connected = true; return m->socket; fail: