Skip to content

Commit

Permalink
[agroal#485] Fix conf command (and subcommands)
Browse files Browse the repository at this point in the history
In the case of the `conf reload` command there were a couple of
explicit `exit` calls that terminated immediatly the `main`. They have
been changed to the boolean return value.

Implemented the `conf get` command.

See agroal#485
  • Loading branch information
fluca1978 committed Nov 13, 2024
1 parent f34e4f9 commit ea07bd4
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 60 deletions.
71 changes: 36 additions & 35 deletions src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
#define COMMAND_STATUS "status"
#define COMMAND_STATUS_DETAILS "status-details"
#define COMMAND_SWITCH_TO "switch-to"
/* #define COMMAND_CONFIG_GET "conf-get" */
#define COMMAND_CONFIG_GET "conf-get"
/* #define COMMAND_CONFIG_LS "conf-ls" */
/* #define COMMAND_CONFIG_SET "conf-set" */

Expand All @@ -91,7 +91,7 @@ static void help_status_details(void);
static void help_switch_to(void);

static int cancel_shutdown(SSL* ssl, int socket, uint8_t compression, uint8_t encryption, int32_t output_format);
/* static int config_get(SSL* ssl, int socket, char* config_key, bool verbose, uint8_t compression, uint8_t encryption, int32_t output_format); */
static int config_get(SSL* ssl, int socket, char* config_key, bool verbose, uint8_t compression, uint8_t encryption, int32_t output_format);
/* static int config_ls(SSL* ssl, int socket, uint8_t compression, uint8_t encryption, int32_t output_format); */
/* static int config_set(SSL* ssl, int socket, char* config_key, char* config_value, bool verbose, uint8_t compression, uint8_t encryption, int32_t output_format); */
static int details(SSL* ssl, int socket, uint8_t compression, uint8_t encryption, int32_t output_format);
Expand Down Expand Up @@ -216,14 +216,14 @@ const struct pgagroal_command command_table[] = {
.deprecated = false,
.log_message = "<conf reload>"
},
/* { */
/* .command = "conf", */
/* .subcommand = "get", */
/* .accepted_argument_count = {1}, */
/* .action = MANAGEMENT_CONFIG_GET, */
/* .deprecated = false, */
/* .log_message = "<conf get> [%s]" */
/* }, */
{
.command = "conf",
.subcommand = "get",
.accepted_argument_count = {1},
.action = MANAGEMENT_CONFIG_GET,
.deprecated = false,
.log_message = "<conf get> [%s]"
},
/* { */
/* .command = "conf", */
/* .subcommand = "set", */
Expand Down Expand Up @@ -797,10 +797,10 @@ main(int argc, char** argv)
exit_code = reload(s_ssl, socket, compression, encryption, output_format);
}
}
/* else if (parsed.cmd->action == MANAGEMENT_CONFIG_GET) */
/* { */
/* exit_code = config_get(s_ssl, socket, parsed.args[0], verbose, compression, encryption, output_format); */
/* } */
else if (parsed.cmd->action == MANAGEMENT_CONFIG_GET)
{
exit_code = config_get(s_ssl, socket, parsed.args[0], verbose, compression, encryption, output_format);
}
/* else if (parsed.cmd->action == MANAGEMENT_CONFIG_SET) */
/* { */
/* exit_code = config_set(s_ssl, socket, parsed.args[0], parsed.args[1], verbose, compression, encryption, output_format); */
Expand Down Expand Up @@ -928,7 +928,7 @@ display_helper(char* command)
else if (//!strcmp(command, COMMAND_CONFIG_GET) ||
//!strcmp(command, COMMAND_CONFIG_LS) ||
//!strcmp(command, COMMAND_CONFIG_SET) ||
!strcmp(command, COMMAND_RELOAD))
!strcmp(command, COMMAND_RELOAD))
{
help_conf();
}
Expand Down Expand Up @@ -1227,34 +1227,35 @@ reload(SSL* ssl, int socket, uint8_t compression, uint8_t encryption, int32_t ou
return 0;

error:

return 1;
}

/* static int */
/* config_get(SSL* ssl, int socket, char* config_key, bool verbose, uint8_t compression, uint8_t encryption, int32_t output_format) */
/* { */
static int
config_get(SSL* ssl, int socket, char* config_key, bool verbose, uint8_t compression, uint8_t encryption, int32_t output_format)
{

/* if (!config_key || strlen(config_key) > MISC_LENGTH) */
/* { */
/* goto error; */
/* } */
if (!config_key || strlen(config_key) > MISC_LENGTH)
{
goto error;
}

/* if (pgagroal_management_config_get(ssl, socket, config_key)) */
/* { */
/* goto error; */
/* } */
if (pgagroal_management_request_config_get(ssl, socket, config_key, compression, encryption, output_format))
{
pgagroal_log_debug("Error trying to get configuration key <%s>", config_key);
goto error;
}

/* if (pgagroal_management_read_config_get(socket, config_key, NULL, verbose, output_format)) */
/* { */
/* goto error; */
/* } */
if (process_result(ssl, socket, output_format))
{
pgagroal_log_debug("Error while processing result for conf_get with key <%s>", config_key);
goto error;
}

/* return 0; */
return 0;

/* error: */
/* return 1; */
/* } */
error:
return 1;
}

/* static int */
/* config_set(SSL* ssl, int socket, char* config_key, char* config_value, bool verbose, uint8_t compression, uint8_t encryption, int32_t output_format) */
Expand Down
18 changes: 17 additions & 1 deletion src/include/management.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ extern "C" {
* Management commands
*/
#define MANAGEMENT_CANCEL_SHUTDOWN 1
/* #define MANAGEMENT_CONFIG_GET 2 */
#define MANAGEMENT_CONFIG_GET 2
/* #define MANAGEMENT_CONFIG_LS 3 */
/* #define MANAGEMENT_CONFIG_SET 4 */
#define MANAGEMENT_DETAILS 5
Expand Down Expand Up @@ -122,6 +122,7 @@ extern "C" {
#define MANAGEMENT_ARGUMENT_TIMESTAMP "Timestamp"
#define MANAGEMENT_ARGUMENT_TOTAL_CONNECTIONS "TotalConnections"
#define MANAGEMENT_ARGUMENT_USERNAME "Username"
#define MANAGEMENT_ARGUMENT_CONFIGURATION_KEY "ConfigurationKey"

/**
* Management error
Expand Down Expand Up @@ -439,6 +440,21 @@ pgagroal_management_read_json(SSL* ssl, int socket, uint8_t* compression, uint8_
int
pgagroal_management_write_json(SSL* ssl, int socket, uint8_t compression, uint8_t encryption, struct json* json);

/**
* Prepares and sends the request to get the configuration key specified.
*
* @param ssl the SSL connection
* @param socket the socket descriptor
* @param config_key the configuration key to be extracted from the configuration
* @param compression the compression method to use within the protocol
* @param encryption the encryption method to use within the protocol
* @param output_format the requested output format
*
* @returns 0 on success
*/
int
pgagroal_management_request_config_get(SSL* ssl, int socket, char* config_key, uint8_t compression, uint8_t encryption, int32_t output_format);

#ifdef __cplusplus
}
#endif
Expand Down
73 changes: 49 additions & 24 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1472,7 +1472,7 @@ accept_mgt_cb(struct ev_loop* loop, struct ev_io* watcher, int revents)
struct json* res = NULL;
struct json* databases = NULL;
char* database = NULL;

pgagroal_log_debug("pgagroal: Management enabledb: ");
pgagroal_pool_status();

Expand All @@ -1483,7 +1483,7 @@ accept_mgt_cb(struct ev_loop* loop, struct ev_io* watcher, int revents)

pgagroal_management_create_response(payload, -1, &res);
pgagroal_json_create(&databases);

if (!strcmp("*", database))
{
struct json* js = NULL;
Expand Down Expand Up @@ -1533,7 +1533,7 @@ accept_mgt_cb(struct ev_loop* loop, struct ev_io* watcher, int revents)
struct json* res = NULL;
struct json* databases = NULL;
char* database = NULL;

pgagroal_log_debug("pgagroal: Management disabledb: ");
pgagroal_pool_status();

Expand Down Expand Up @@ -1598,7 +1598,7 @@ accept_mgt_cb(struct ev_loop* loop, struct ev_io* watcher, int revents)
start_time = time(NULL);

config->gracefully = true;

end_time = time(NULL);

pgagroal_management_response_ok(NULL, client_fd, start_time, end_time, compression, encryption, payload);
Expand Down Expand Up @@ -1659,7 +1659,6 @@ accept_mgt_cb(struct ev_loop* loop, struct ev_io* watcher, int revents)
pgagroal_log_debug("pgagroal: Management details");
pgagroal_pool_status();


pid = fork();
if (pid == -1)
{
Expand Down Expand Up @@ -1700,7 +1699,7 @@ accept_mgt_cb(struct ev_loop* loop, struct ev_io* watcher, int revents)
start_time = time(NULL);

pgagroal_prometheus_clear();

end_time = time(NULL);

pgagroal_management_response_ok(NULL, client_fd, start_time, end_time, compression, encryption, payload);
Expand All @@ -1711,15 +1710,15 @@ accept_mgt_cb(struct ev_loop* loop, struct ev_io* watcher, int revents)
pgagroal_log_debug("pgagroal: Management clear server");
char* server = NULL;
struct json* req = NULL;

start_time = time(NULL);

req = (struct json*)pgagroal_json_get(payload, MANAGEMENT_CATEGORY_REQUEST);
server = (char*)pgagroal_json_get(req, MANAGEMENT_ARGUMENT_SERVER);

pgagroal_server_clear(server);
pgagroal_prometheus_failed_servers();

end_time = time(NULL);

pgagroal_management_response_ok(NULL, client_fd, start_time, end_time, compression, encryption, payload);
Expand All @@ -1731,7 +1730,7 @@ accept_mgt_cb(struct ev_loop* loop, struct ev_io* watcher, int revents)
signed char server_state;
char* server = NULL;
struct json* req = NULL;

start_time = time(NULL);

req = (struct json*)pgagroal_json_get(payload, MANAGEMENT_CATEGORY_REQUEST);
Expand Down Expand Up @@ -1776,7 +1775,7 @@ accept_mgt_cb(struct ev_loop* loop, struct ev_io* watcher, int revents)
pgagroal_log_debug("pgagroal: Management reload");

start_time = time(NULL);

restart = reload_configuration();

pgagroal_management_create_response(payload, -1, &res);
Expand All @@ -1787,11 +1786,37 @@ accept_mgt_cb(struct ev_loop* loop, struct ev_io* watcher, int revents)

pgagroal_management_response_ok(NULL, client_fd, start_time, end_time, compression, encryption, payload);
}
/* else if (id == MANAGEMENT_CONFIG_GET) */
/* { */
/* pgagroal_log_debug("pgagroal: Management config-get for key <%s>", payload_s); */
/* pgagroal_management_write_config_get(client_fd, payload_s); */
/* } */
else if (id == MANAGEMENT_CONFIG_GET)
{
struct json* response = NULL;
struct json* request = NULL;
char config_value[ MISC_LENGTH ];
char* config_key;
int status;

pgagroal_log_debug("pgagroal: Management conf get");

start_time = time(NULL);

pgagroal_management_create_response(payload, -1, &response);

request = (struct json*)pgagroal_json_get(payload, MANAGEMENT_CATEGORY_REQUEST);
config_key = (char*)pgagroal_json_get(request, MANAGEMENT_ARGUMENT_CONFIGURATION_KEY);
status = pgagroal_write_config_value(config_value, config_key, MISC_LENGTH - 1);

pgagroal_json_put(response, config_key, (uintptr_t) config_value, ValueString);

end_time = time(NULL);

if (status)
{
pgagroal_management_response_error(NULL, client_fd, NULL, status, compression, encryption, payload);
}
else
{
pgagroal_management_response_ok(NULL, client_fd, start_time, end_time, compression, encryption, payload);
}
}
/* else if (id == MANAGEMENT_CONFIG_SET) */
/* { */
/* // this command has a secondary payload to extract, that is the configuration value */
Expand All @@ -1810,9 +1835,9 @@ accept_mgt_cb(struct ev_loop* loop, struct ev_io* watcher, int revents)
char* username = NULL;
struct json* req = NULL;
struct json* res = NULL;

start_time = time(NULL);

req = (struct json*)pgagroal_json_get(payload, MANAGEMENT_CATEGORY_REQUEST);
username = (char*)pgagroal_json_get(req, MANAGEMENT_ARGUMENT_USERNAME);

Expand All @@ -1823,9 +1848,9 @@ accept_mgt_cb(struct ev_loop* loop, struct ev_io* watcher, int revents)
index = i;
}
}

pgagroal_management_create_response(payload, -1, &res);

pgagroal_json_put(res, MANAGEMENT_ARGUMENT_USERNAME, (uintptr_t)username, ValueString);

if (index != -1)
Expand Down Expand Up @@ -1972,7 +1997,7 @@ accept_transfer_cb(struct ev_loop* loop, struct ev_io* watcher, int revents)
}

pgagroal_disconnect(c_fd);

c = c->next;
}
}
Expand Down Expand Up @@ -2025,7 +2050,7 @@ accept_transfer_cb(struct ev_loop* loop, struct ev_io* watcher, int revents)
}

pgagroal_disconnect(c_fd);

c = c->next;
}

Expand Down Expand Up @@ -2055,7 +2080,7 @@ accept_transfer_cb(struct ev_loop* loop, struct ev_io* watcher, int revents)
pgagroal_prometheus_self_sockets_sub();

return;

error:

pgagroal_disconnect(client_fd);
Expand Down Expand Up @@ -2583,11 +2608,11 @@ reload_configuration(void)
remove_pidfile();
}

exit(0);
return true;

error:
remove_pidfile();
exit(1);
return false;
}

/**
Expand Down

0 comments on commit ea07bd4

Please sign in to comment.