From 0463e70bbf8c85b8870e96a8096d448c4a5ef1b3 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Tue, 27 Jun 2023 19:26:32 +0200 Subject: [PATCH 1/2] Ensure everything gets written after client EOF is read --- src/gmpd.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/gmpd.c b/src/gmpd.c index 77f3bd515..351c0a960 100644 --- a/src/gmpd.c +++ b/src/gmpd.c @@ -77,6 +77,11 @@ buffer_size_t from_client_start = 0; */ buffer_size_t from_client_end = 0; +/** + * @brief Flag for serve_gmp and gmpd_send_to_client. + */ +static int close_connection = 0; + /** * @brief Initialise the GMP library for the GMP daemon. * @@ -373,6 +378,9 @@ gmpd_send_to_client (const char* msg, void* write_to_client_data) switch (write_to_client (write_to_client_data)) { case 0: /* Wrote everything in to_client. */ + // FIX + //if (close_connection) + // goto client_free; break; case -1: /* Error. */ g_debug (" %s full (%i < %zu); client write failed", @@ -455,10 +463,13 @@ int serve_gmp (gvm_connection_t *client_connection, const db_conn_info_t *database, gchar **disable) { - int nfds, rc = 0; + int nfds, rc; g_debug (" Serving GMP"); + rc = 0; + close_connection = 0; + /* Initialise the XML parser and the manage library. */ init_gmp_process (database, (int (*) (const char*, void*)) gmpd_send_to_client, @@ -542,7 +553,8 @@ serve_gmp (gvm_connection_t *client_connection, const db_conn_info_t *database, } /* Read any data from the client. */ - if (client_connection->socket > 0 + if (close_connection == 0 + && client_connection->socket > 0 && FD_ISSET (client_connection->socket, &readfds)) { buffer_size_t initial_start = from_client_end; @@ -561,11 +573,9 @@ serve_gmp (gvm_connection_t *client_connection, const db_conn_info_t *database, g_debug (" EOF reading from client"); if (client_connection->socket > 0 && FD_ISSET (client_connection->socket, &writefds)) - /* Write rest of to_client to client, so that the client gets - * any buffered output and the response to the error. */ - write_to_client (client_connection); - rc = 0; - goto client_free; + /* Stop reading, but process rest of input and output. */ + close_connection = 1; + break; default: /* Programming error. */ assert (0); } @@ -613,6 +623,8 @@ serve_gmp (gvm_connection_t *client_connection, const db_conn_info_t *database, switch (write_to_client (client_connection)) { case 0: /* Wrote everything in to_client. */ + if (close_connection) + goto client_free; break; case -1: /* Error. */ rc = -1; From 75c5e3f453903c9a01520f8871862231b84deefb Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Mon, 3 Jul 2023 21:57:37 +0200 Subject: [PATCH 2/2] Remove note about handling close_connection This case is safe because process_gmp_client_input will always leave some response text in to_client, so the final write_to_client in serve_gmp will catch the EOF. --- src/gmpd.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/gmpd.c b/src/gmpd.c index 351c0a960..772a405c0 100644 --- a/src/gmpd.c +++ b/src/gmpd.c @@ -378,9 +378,6 @@ gmpd_send_to_client (const char* msg, void* write_to_client_data) switch (write_to_client (write_to_client_data)) { case 0: /* Wrote everything in to_client. */ - // FIX - //if (close_connection) - // goto client_free; break; case -1: /* Error. */ g_debug (" %s full (%i < %zu); client write failed",