From cc37956a35bacb368ee0ece5bd2648bcd1b55d7e Mon Sep 17 00:00:00 2001 From: EuGig Date: Mon, 18 Mar 2024 15:08:35 +0100 Subject: [PATCH] [#392] Add application name and version in management protocol In order to improve debugging, [#392] suggested to write the sender application name and its version into the header over which pgagroal-cli and pgagroal communicate. Introduces the 'write_header_info' function in management.c. --- src/include/pgagroal.h | 42 ++++++++++++------------ src/libpgagroal/management.c | 63 +++++++++++++++++++++++++++++++++--- 2 files changed, 80 insertions(+), 25 deletions(-) diff --git a/src/include/pgagroal.h b/src/include/pgagroal.h index b76c72cf..77378565 100644 --- a/src/include/pgagroal.h +++ b/src/include/pgagroal.h @@ -174,14 +174,14 @@ extern "C" { #define unlikely(x) __builtin_expect (!!(x), 0) #define MAX(a, b) \ - ({ __typeof__ (a) _a = (a); \ - __typeof__ (b) _b = (b); \ - _a > _b ? _a : _b; }) + ({ __typeof__ (a) _a = (a); \ + __typeof__ (b) _b = (b); \ + _a > _b ? _a : _b; }) #define MIN(a, b) \ - ({ __typeof__ (a) _a = (a); \ - __typeof__ (b) _b = (b); \ - _a < _b ? _a : _b; }) + ({ __typeof__ (a) _a = (a); \ + __typeof__ (b) _b = (b); \ + _a < _b ? _a : _b; }) /* * Common piece of code to perform a sleeping. @@ -194,13 +194,13 @@ extern "C" { * */ #define SLEEP(zzz) \ - do \ - { \ - struct timespec ts_private; \ - ts_private.tv_sec = 0; \ - ts_private.tv_nsec = zzz; \ - nanosleep(&ts_private, NULL); \ - } while (0); + do \ + { \ + struct timespec ts_private; \ + ts_private.tv_sec = 0; \ + ts_private.tv_nsec = zzz; \ + nanosleep(&ts_private, NULL); \ + } while (0); /* * Commonly used block of code to sleep @@ -217,14 +217,14 @@ extern "C" { SLEEP_AND_GOTO(100000L, retry) */ #define SLEEP_AND_GOTO(zzz, goto_to) \ - do \ - { \ - struct timespec ts_private; \ - ts_private.tv_sec = 0; \ - ts_private.tv_nsec = zzz; \ - nanosleep(&ts_private, NULL); \ - goto goto_to; \ - } while (0); + do \ + { \ + struct timespec ts_private; \ + ts_private.tv_sec = 0; \ + ts_private.tv_nsec = zzz; \ + nanosleep(&ts_private, NULL); \ + goto goto_to; \ + } while (0); /** * The shared memory segment diff --git a/src/libpgagroal/management.c b/src/libpgagroal/management.c index 30af7d54..7e4b0228 100644 --- a/src/libpgagroal/management.c +++ b/src/libpgagroal/management.c @@ -52,13 +52,14 @@ #include #include -#define MANAGEMENT_HEADER_SIZE 5 +#define MANAGEMENT_HEADER_SIZE 23 static int read_complete(SSL* ssl, int socket, void* buf, size_t size); static int write_complete(SSL* ssl, int socket, void* buf, size_t size); static int write_socket(int socket, void* buf, size_t size); static int write_ssl(SSL* ssl, void* buf, size_t size); static int write_header(SSL* ssl, int fd, signed char type, int slot); +static int write_header_info(char* header, char* command, int offset); static int pgagroal_management_write_conf_ls_detail(int socket, char* what); static int pgagroal_management_read_conf_ls_detail(SSL* ssl, int socket, char* buffer); @@ -837,6 +838,8 @@ int pgagroal_management_write_status(int socket, bool graceful) { char buf[16]; + char buf_info[MANAGEMENT_HEADER_SIZE]; + int active; int total; struct configuration* config; @@ -896,6 +899,15 @@ pgagroal_management_write_status(int socket, bool graceful) goto error; } + write_header_info(buf_info, "pgagroal", 0); + + if (write_complete(NULL, socket, &buf_info, sizeof(buf_info))) + { + pgagroal_log_warn("pgagroal_management_write_status: write: %d %s", socket, strerror(errno)); + errno = 0; + goto error; + } + return 0; error: @@ -951,7 +963,7 @@ pgagroal_management_read_details(SSL* ssl, int socket, char output_format) int pgagroal_management_write_details(int socket) { - char header[12 + MAX_NUMBER_OF_CONNECTIONS]; + char header[12 + MAX_NUMBER_OF_CONNECTIONS + MANAGEMENT_HEADER_SIZE]; struct configuration* config; config = (struct configuration*)shmem; @@ -968,6 +980,8 @@ pgagroal_management_write_details(int socket) header[12 + i] = (char)state; } + write_header_info(header, "pgagroal", 12 + MAX_NUMBER_OF_CONNECTIONS); + if (write_complete(NULL, socket, header, sizeof(header))) { pgagroal_log_warn("pgagroal_management_write_details: write: %d %s", socket, strerror(errno)); @@ -1113,7 +1127,7 @@ pgagroal_management_read_isalive(SSL* ssl, int socket, int* status, char output_ int pgagroal_management_write_isalive(int socket, bool gracefully) { - char buf[4]; + char buf[MANAGEMENT_HEADER_SIZE]; memset(&buf, 0, sizeof(buf)); @@ -1126,6 +1140,8 @@ pgagroal_management_write_isalive(int socket, bool gracefully) pgagroal_write_int32(buf, PING_STATUS_SHUTDOWN_GRACEFULLY); } + write_header_info(buf, "pgagroal", 4); + if (write_complete(NULL, socket, &buf, sizeof(buf))) { pgagroal_log_warn("pgagroal_management_write_isalive: write: %d %s", socket, strerror(errno)); @@ -1610,14 +1626,40 @@ write_ssl(SSL* ssl, void* buf, size_t size) return 1; } +/** + * Function to write sender application name and its version + * into header socket. + * + * @param header: the header to write sender application name and its version into + * @param command: the server application name + * @param offset: the offset at which info has to be written + * @return 0 on success + */ +static int +write_header_info(char* header, char* command, int offset) +{ + char header_info[MANAGEMENT_HEADER_SIZE] = "S:"; + + strcat(header_info, command); + strcat(header_info, ";V:"); + strcat(header_info, PGAGROAL_VERSION); + + pgagroal_write_string(header + offset, header_info); + pgagroal_log_debug("Header Info: %s\n", header + offset); + + return 0; +} + static int write_header(SSL* ssl, int fd, signed char type, int slot) { - char header[MANAGEMENT_HEADER_SIZE]; + char header[5 + MANAGEMENT_HEADER_SIZE]; pgagroal_write_byte(&(header), type); pgagroal_write_int32(&(header[1]), slot); + write_header_info(header, "pgagroal-cli", 5); + return write_complete(ssl, fd, &(header), MANAGEMENT_HEADER_SIZE); } @@ -1682,6 +1724,8 @@ pgagroal_management_write_config_get(int socket, char* config_key) { char data[MISC_LENGTH]; char buf[4]; + char buf_info[MANAGEMENT_HEADER_SIZE]; + size_t size; if (!config_key || !strlen(config_key)) @@ -1726,6 +1770,8 @@ pgagroal_management_write_config_get(int socket, char* config_key) goto error; } + write_header_info(buf_info, "pgagroal", 0); + return 0; error: @@ -2017,6 +2063,8 @@ pgagroal_management_write_conf_ls(int socket) config = (struct configuration*)shmem; + char buf_info[MANAGEMENT_HEADER_SIZE + 21]; + if (pgagroal_management_write_conf_ls_detail(socket, config->configuration_path)) { goto error; @@ -2053,6 +2101,13 @@ pgagroal_management_write_conf_ls(int socket) goto error; } + write_header_info(buf_info, "pgagroal", 0); + + if (write_complete(NULL, socket, &buf_info, sizeof(buf_info))) + { + goto error; + } + return 0; error: