From 52a585a047d58039fd240a6220893567beeedaec Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Wed, 29 Jun 2022 04:23:36 -0400 Subject: [PATCH] Create a more descriptive process title for connections. This commit introduces the function `pgagroal_set_connection_proc_title()` that builds a process title with the form: user@host:port/database using in turn the `pgagroal_set_proc_title` function. The `pgagroal_set_connection_proc_title` function is used only by the `worker` process stuff and required to identify the primary server (to get the hostname and port). Related to #215. --- src/include/pgagroal.h | 1 - src/include/utils.h | 14 ++++++++++++++ src/libpgagroal/utils.c | 28 ++++++++++++++++++++++++---- src/libpgagroal/worker.c | 2 +- 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/include/pgagroal.h b/src/include/pgagroal.h index b04c3d6d..345951fa 100644 --- a/src/include/pgagroal.h +++ b/src/include/pgagroal.h @@ -62,7 +62,6 @@ extern "C" { #define PGAGROAL_DEFAULT_ADMINS_FILE PGAGROAL_DEFAULT_CONFIGURATION_PATH "pgagroal_admins.conf" #define PGAGROAL_DEFAULT_SUPERUSER_FILE PGAGROAL_DEFAULT_CONFIGURATION_PATH "pgagroal_superuser.conf" - #define MAX_BUFFER_SIZE 65535 #define DEFAULT_BUFFER_SIZE 65535 #define SECURITY_BUFFER_SIZE 512 diff --git a/src/include/utils.h b/src/include/utils.h index b48c451e..ad0b0660 100644 --- a/src/include/utils.h +++ b/src/include/utils.h @@ -254,6 +254,20 @@ pgagroal_base64_decode(char* encoded, size_t encoded_length, char** raw, int* ra void pgagroal_set_proc_title(int argc, char** argv, char* s1, char* s2); +/** + * Sets the process title for a given connection. + * + * Uses `pgagroal_set_proc_title` to build an information string + * with the form + * user@host:port/database + * + * @param argc the number of arguments + * @param argv command line arguments + * @param connection the struct connection pointer for the established connection. + */ +void +pgagroal_set_connection_proc_title(int argc, char** argv, struct connection* connection); + #ifdef DEBUG /** diff --git a/src/libpgagroal/utils.c b/src/libpgagroal/utils.c index 66b6be8c..d369c53d 100644 --- a/src/libpgagroal/utils.c +++ b/src/libpgagroal/utils.c @@ -30,6 +30,7 @@ #include #include #include +#include /* system */ #include @@ -758,10 +759,10 @@ pgagroal_set_proc_title(int argc, char** argv, char* s1, char* s2) // when the application was started if (max_process_title_size == 0) { - for (int i = 0; i < argc; i++) - { - max_process_title_size += strlen(argv[i]) + 1; - } + for (int i = 0; i < argc; i++) + { + max_process_title_size += strlen(argv[i]) + 1; + } } // compose the new title @@ -791,6 +792,25 @@ pgagroal_set_proc_title(int argc, char** argv, char* s1, char* s2) #endif } +void +pgagroal_set_connection_proc_title(int argc, char** argv, struct connection* connection) +{ + struct configuration* config; + int primary; + char info[MISC_LENGTH]; + + config = (struct configuration*)shmem; + + pgagroal_get_primary(&primary); + + snprintf(info, MISC_LENGTH, "%s@%s:%d", + connection->username, + config->servers[primary].host, + config->servers[primary].port); + + pgagroal_set_proc_title(argc, argv, info, connection->database); +} + #ifdef DEBUG int diff --git a/src/libpgagroal/worker.c b/src/libpgagroal/worker.c index ab2b2e60..614d9d66 100644 --- a/src/libpgagroal/worker.c +++ b/src/libpgagroal/worker.c @@ -106,7 +106,7 @@ pgagroal_worker(int client_fd, char* address, char** argv) pgagroal_prometheus_client_active_add(); pgagroal_pool_status(); - pgagroal_set_proc_title(1, argv, config->connections[slot].username, config->connections[slot].database); + pgagroal_set_connection_proc_title(1, argv, &config->connections[slot]); if (config->pipeline == PIPELINE_PERFORMANCE) {