Skip to content

Commit

Permalink
Ensure everything gets written after client EOF is read
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmundell committed Jun 27, 2023
1 parent e3fbe51 commit 0463e70
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/gmpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 0463e70

Please sign in to comment.