Skip to content

Commit

Permalink
[agroal#392] Add application name and version in management protocol
Browse files Browse the repository at this point in the history
In order to improve debugging, [agroal#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.
  • Loading branch information
EuGig committed Mar 25, 2024
1 parent 411dd98 commit cc37956
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 25 deletions.
42 changes: 21 additions & 21 deletions src/include/pgagroal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand Down
63 changes: 59 additions & 4 deletions src/libpgagroal/management.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@
#include <openssl/err.h>
#include <openssl/ssl.h>

#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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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;
Expand All @@ -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));
Expand Down Expand Up @@ -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));

Expand All @@ -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));
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit cc37956

Please sign in to comment.