Skip to content

Commit

Permalink
[#388] Add conf ls to get the configuration file locations
Browse files Browse the repository at this point in the history
Introducing the command `conf ls` to `pgagroal-cli` that prints out
where the configuration files are located.
A new management key, action and three new management methods have
been added to implement the logic.
The idea is that, once the action is required, the management prints
on the socket every single `struct configuration*` path variables, and
then the management read method reads back in the same order to the
same list of values to be printed on the output screen.

In order to achieve this, two new static functions have been added to
the managemenet in order to write and read from the socket a single
path. A path is written with a size and the following path, assuming
it will never be larger than MAX_PATH or an error will be thrown.
In the case a path is NULL or empty, an empty string will be written
on the socket and so the final result is that the entry will be
printed as empty in the command output.

Documentation updated.

Shell completions updated.

Fixes also a few log_trace in the cli application.

Close #388
  • Loading branch information
fluca1978 committed Nov 7, 2023
1 parent e2dcb10 commit 5988613
Show file tree
Hide file tree
Showing 7 changed files with 363 additions and 9 deletions.
2 changes: 1 addition & 1 deletion contrib/shell_comp/pgagroal_comp.bash
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pgagroal_cli_completions()
COMPREPLY+=($(compgen -W "server prometheus" "${COMP_WORDS[2]}"))
;;
conf)
COMPREPLY+=($(compgen -W "reload get set" "${COMP_WORDS[2]}"))
COMPREPLY+=($(compgen -W "reload get set ls" "${COMP_WORDS[2]}"))
;;
status)
COMPREPLY+=($(compgen -W "details" "${COMP_WORDS[2]}"))
Expand Down
2 changes: 1 addition & 1 deletion contrib/shell_comp/pgagroal_comp.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function _pgagroal_cli_conf()
{
local line
_arguments -C \
"1: :(reload get set)" \
"1: :(reload get set ls)" \
"*::arg:->args"
}

Expand Down
20 changes: 17 additions & 3 deletions doc/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ Manages the configuration of the running instance.
This command requires one subcommand, that can be:
- `reload` issue a reload of the configuration, applying at runtime any changes from the configuration files;
- `get` provides a configuration parameter value;
- `set` modifies a configuration parameter at runtime.
- `set` modifies a configuration parameter at runtime;
- `ls` prints where the configuration files are located.

Command

Expand All @@ -205,7 +206,7 @@ pgagroal-cli conf set max_connections 25

The details about how to get and set values at run-time are explained in the following.

### conf get
#### conf get
Given a configuration setting name, provides the current value for such setting.

The configuration setting name must be the same as the one used in the configuration files.
Expand Down Expand Up @@ -256,7 +257,7 @@ If the parameter name specified is not found or invalid, the program `pgagroal-c



### conf set
#### conf set
Allows the setting of a configuration parameter at run-time, if possible.

Examples
Expand Down Expand Up @@ -303,7 +304,20 @@ WARN 1 settings cannot be applied
DEBUG pgagroal_management_write_config_set: unable to apply changes to <max_connections> -> <100>
```

#### conf ls

The command `conf ls` provides information about the location of the configuration files.
As an example:

```
Main Configuration file: /etc/pgagroal/pgagroal.conf
HBA file: /etc/pgagroal/pgagroal_hba.conf
Limit file: /etc/pgagroal/pgagroal_databases.conf
Frontend users file: /etc/pgagroal/pgagroal_frontend_users.conf
Admins file: /etc/pgagroal/pgagroal_admins.conf
Superuser file:
Users file: /etc/pgagroal/pgagroal_users.conf
```

### clear
Resets different parts of the pooler. It accepts an operational mode:
Expand Down
44 changes: 40 additions & 4 deletions src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
#define ACTION_RELOAD 13
#define ACTION_CONFIG_GET 14
#define ACTION_CONFIG_SET 15
#define ACTION_CONFIG_LS 16

static int flush(SSL* ssl, int socket, int32_t mode, char* database);
static int enabledb(SSL* ssl, int socket, char* database);
Expand All @@ -82,6 +83,7 @@ static int switch_to(SSL* ssl, int socket, char* server);
static int reload(SSL* ssl, int socket);
static int config_get(SSL* ssl, int socket, char* config_key, bool verbose);
static int config_set(SSL* ssl, int socket, char* config_key, char* config_value, bool verbose);
static int config_ls(SSL* ssl, int socket);

static void
version(void)
Expand Down Expand Up @@ -134,7 +136,8 @@ usage(void)
printf(" - 'get' to obtain information about a runtime configuration value;\n");
printf(" conf get <parameter_name>\n");
printf(" - 'set' to modify a configuration value;\n");
printf(" conf set <parameter_name> <parameter_value>\n");
printf(" conf set <parameter_name> <parameter_value>;\n");
printf(" - 'ls' lists the configuration files used.\n");
printf(" clear <what> Resets either the Prometheus statistics or the specified server.\n");
printf(" <what> can be\n");
printf(" - 'server' (default) followed by a server name\n");
Expand Down Expand Up @@ -431,20 +434,25 @@ main(int argc, char** argv)
{
action = ACTION_RELOAD;
}
pgagroal_log_debug("Command: <reload>");
pgagroal_log_trace("Command: <reload>");
}
else if (parse_command(argc, argv, optind, "conf", "get", &config_key, NULL, NULL, NULL)
|| parse_deprecated_command(argc, argv, optind, "config-get", NULL, "conf get", 1, 6))
{
action = config_key != NULL && strlen(config_key) > 0 ? ACTION_CONFIG_GET : ACTION_UNKNOWN;
pgagroal_log_debug("Command: <conf get> [%s]", config_key);
pgagroal_log_trace("Command: <conf get> [%s]", config_key);
}
else if (parse_command(argc, argv, optind, "conf", "set", &config_key, NULL, &config_value, NULL)
|| parse_deprecated_command(argc, argv, optind, "config-set", NULL, "conf set", 1, 6))
{
// if there is no configuration key set the action to unknown, so the help screen will be printed
action = config_key != NULL && strlen(config_key) > 0 ? ACTION_CONFIG_SET : ACTION_UNKNOWN;
pgagroal_log_debug("Command: <conf set> [%s] = [%s]", config_key, config_value);
pgagroal_log_trace("Command: <conf set> [%s] = [%s]", config_key, config_value);
}
else if (parse_command_simple(argc, argv, optind, "conf", "ls"))
{
pgagroal_log_debug("Command: <conf ls>");
action = ACTION_CONFIG_LS;
}

if (action != ACTION_UNKNOWN)
Expand Down Expand Up @@ -606,6 +614,10 @@ main(int argc, char** argv)
{
exit_code = config_set(s_ssl, socket, config_key, config_value, verbose);
}
else if (action == ACTION_CONFIG_LS)
{
exit_code = config_ls(s_ssl, socket);
}

done:

Expand Down Expand Up @@ -970,3 +982,27 @@ config_set(SSL* ssl, int socket, char* config_key, char* config_value, bool verb
error:
return EXIT_STATUS_CONNECTION_ERROR;
}

/**
* Asks the daemon about the configuration file location.
*
* @returns 0 on success
*/
static int
config_ls(SSL* ssl, int socket)
{

if (pgagroal_management_conf_ls(ssl, socket))
{
goto error;
}

if (pgagroal_management_read_conf_ls(ssl, socket))
{
goto error;
}

return EXIT_STATUS_OK;
error:
return EXIT_STATUS_CONNECTION_ERROR;
}
55 changes: 55 additions & 0 deletions src/include/management.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ extern "C" {
#define MANAGEMENT_REMOVE_FD 19
#define MANAGEMENT_CONFIG_GET 20
#define MANAGEMENT_CONFIG_SET 21
#define MANAGEMENT_CONFIG_LS 22

/**
* Read the management header
Expand Down Expand Up @@ -386,6 +387,60 @@ pgagroal_management_config_set(SSL* ssl, int socket, char* config_key, char* con
int
pgagroal_management_write_config_set(int socket, char* config_key, char* config_value);

/**
* Entry point for managing the `conf ls` command that
* will list all the configuration files used by the running
* daemon.
*
* @param ssl the SSL handler
* @param fd the socket file descriptor
* @returns 0 on success
*/
int
pgagroal_management_conf_ls(SSL* ssl, int fd);

/**
* Reads out of the socket the list of configuration
* files and prints them out to the standard output.
*
* The order of the read paths is:
* - configuration path
* - HBA path
* - limit path
* - frontend users path
* - admins path
* - Superusers path
* - users path
*
* @param socket the file descriptor of the open socket
* @param ssl the SSL handler
* @returns 0 on success
*/
int
pgagroal_management_read_conf_ls(SSL* ssl, int socket);

/**
* The management function responsible for sending
* the configuration paths into the socket.
*
* The function sends every path following the path length,
* that must be limited to MAX_PATH size.
*
* The order of the sent paths is:
* - configuration path
* - HBA path
* - limit path
* - frontend users path
* - admins path
* - Superusers path
* - users path
*
* @params socket the file descriptor of the open socket
* @returns 0 on success
*/
int
pgagroal_management_write_conf_ls(int socket);

#ifdef __cplusplus
}
#endif
Expand Down
Loading

0 comments on commit 5988613

Please sign in to comment.