diff --git a/src/include/pgagroal.h b/src/include/pgagroal.h index da99a15b..d78ff518 100644 --- a/src/include/pgagroal.h +++ b/src/include/pgagroal.h @@ -527,6 +527,7 @@ struct main_configuration int management; /**< The management port */ bool gracefully; /**< Is pgagroal in gracefully mode */ + bool all_disabled; /**< Are all databases disabled */ char disabled[NUMBER_OF_DISABLED][MAX_DATABASE_LENGTH]; /**< Which databases are disabled */ int pipeline; /**< The pipeline type */ diff --git a/src/libpgagroal/configuration.c b/src/libpgagroal/configuration.c index 6095a9ac..82adc84d 100644 --- a/src/libpgagroal/configuration.c +++ b/src/libpgagroal/configuration.c @@ -144,6 +144,8 @@ pgagroal_init_configuration(void* shm) config->disconnect_client = 0; config->disconnect_client_force = false; + config->all_disabled = false; + config->keep_alive = true; config->nodelay = true; config->non_blocking = false; diff --git a/src/libpgagroal/security.c b/src/libpgagroal/security.c index 04a3641c..40dcff1b 100644 --- a/src/libpgagroal/security.c +++ b/src/libpgagroal/security.c @@ -3096,10 +3096,14 @@ is_disabled(char* database) config = (struct main_configuration*)shmem; + if (config->all_disabled) + { + return true; + } + for (int i = 0; i < NUMBER_OF_DISABLED; i++) { - if (!strcmp(config->disabled[i], "*") || - !strcmp(config->disabled[i], database)) + if (!strcmp(config->disabled[i], database)) { return true; } diff --git a/src/libpgagroal/status.c b/src/libpgagroal/status.c index a82690be..81284aec 100644 --- a/src/libpgagroal/status.c +++ b/src/libpgagroal/status.c @@ -210,7 +210,9 @@ status_details(bool details, struct json* response) if (details) { + int number_of_disabled = 0; struct json* limits = NULL; + struct json* databases = NULL; struct json* connections = NULL; pgagroal_json_create(&limits); @@ -236,23 +238,38 @@ status_details(bool details, struct json* response) pgagroal_json_put(response, MANAGEMENT_ARGUMENT_LIMITS, (uintptr_t)limits, ValueJSON); - // TODO Databases + Disabled -/* for (int i = 0; i < NUMBER_OF_DISABLED; i++) */ -/* { */ -/* if (strcmp(disabled[i], "")) */ -/* { */ -/* if (!strcmp(disabled[i], "*")) */ -/* { */ -/* cJSON_AddItemToArray(databases_array, cJSON_CreateString("ALL")); */ -/* counter = -1; */ -/* } */ -/* else */ -/* { */ -/* cJSON_AddItemToArray(databases_array, cJSON_CreateString(disabled[i])); */ -/* counter++; */ -/* } */ -/* } */ -/* } */ + pgagroal_json_create(&databases); + + for (int i = 0; i < NUMBER_OF_DISABLED; i++) + { + struct json* js = NULL; + + if (strlen(config->disabled[i]) > 0) + { + pgagroal_json_create(&js); + + pgagroal_json_put(js, MANAGEMENT_ARGUMENT_DATABASE, (uintptr_t)config->disabled[i], ValueString); + pgagroal_json_put(js, MANAGEMENT_ARGUMENT_ENABLED, (uintptr_t)false, ValueBool); + + pgagroal_json_append(databases, (uintptr_t)js, ValueJSON); + + number_of_disabled++; + } + } + + if (number_of_disabled == 0) + { + struct json* js = NULL; + + pgagroal_json_create(&js); + + pgagroal_json_put(js, MANAGEMENT_ARGUMENT_DATABASE, (uintptr_t)"*", ValueString); + pgagroal_json_put(js, MANAGEMENT_ARGUMENT_ENABLED, (uintptr_t)!config->all_disabled, ValueBool); + + pgagroal_json_append(databases, (uintptr_t)js, ValueJSON); + } + + pgagroal_json_put(response, MANAGEMENT_ARGUMENT_DATABASES, (uintptr_t)databases, ValueJSON); for (int i = 0; i < config->max_connections; i++) { @@ -260,11 +277,11 @@ status_details(bool details, struct json* response) pgagroal_json_create(&js); - pgagroal_json_put(js, MANAGEMENT_ARGUMENT_START_TIME, (uintptr_t)config->connections[i].start_time, ValueUInt64); - pgagroal_json_put(js, MANAGEMENT_ARGUMENT_TIMESTAMP, (uintptr_t)config->connections[i].timestamp, ValueUInt64); + pgagroal_json_put(js, MANAGEMENT_ARGUMENT_START_TIME, (uintptr_t)config->connections[i].start_time, ValueInt64); + pgagroal_json_put(js, MANAGEMENT_ARGUMENT_TIMESTAMP, (uintptr_t)config->connections[i].timestamp, ValueInt64); - pgagroal_json_put(js, MANAGEMENT_ARGUMENT_PID, (uintptr_t)config->connections[i].pid, ValueUInt32); - pgagroal_json_put(js, MANAGEMENT_ARGUMENT_FD, (uintptr_t)config->connections[i].fd, ValueUInt32); + pgagroal_json_put(js, MANAGEMENT_ARGUMENT_PID, (uintptr_t)config->connections[i].pid, ValueInt32); + pgagroal_json_put(js, MANAGEMENT_ARGUMENT_FD, (uintptr_t)config->connections[i].fd, ValueInt32); pgagroal_json_put(js, MANAGEMENT_ARGUMENT_DATABASE, (uintptr_t)config->connections[i].database, ValueString); pgagroal_json_put(js, MANAGEMENT_ARGUMENT_USERNAME, (uintptr_t)config->connections[i].username, ValueString); diff --git a/src/main.c b/src/main.c index 24dd4cdf..053aafd8 100644 --- a/src/main.c +++ b/src/main.c @@ -1484,30 +1484,40 @@ accept_mgt_cb(struct ev_loop* loop, struct ev_io* watcher, int revents) pgagroal_management_create_response(payload, -1, &res); pgagroal_json_create(&databases); - for (int i = 0; i < NUMBER_OF_DISABLED; i++) + if (!strcmp("*", database)) { - bool found = false; struct json* js = NULL; - if (!strcmp("*", database)) - { - memset(&config->disabled[i], 0, MAX_DATABASE_LENGTH); - found = true; - } - else if (!strcmp(config->disabled[i], database)) - { - memset(&config->disabled[i], 0, MAX_DATABASE_LENGTH); - found = true; - } + config->all_disabled = false; + memset(&config->disabled, 0, sizeof(config->disabled)); - if (found) + pgagroal_json_create(&js); + + pgagroal_json_put(js, MANAGEMENT_ARGUMENT_DATABASE, (uintptr_t)database, ValueString); + pgagroal_json_put(js, MANAGEMENT_ARGUMENT_ENABLED, (uintptr_t)true, ValueBool); + + pgagroal_json_append(databases, (uintptr_t)js, ValueJSON); + } + else + { + bool found = false; + for (int i = 0; !found && i < NUMBER_OF_DISABLED; i++) { - pgagroal_json_create(&js); + struct json* js = NULL; + + if (!strcmp(config->disabled[i], database)) + { + memset(&config->disabled[i], 0, MAX_DATABASE_LENGTH); - pgagroal_json_put(js, MANAGEMENT_ARGUMENT_DATABASE, (uintptr_t)database, ValueString); - pgagroal_json_put(js, MANAGEMENT_ARGUMENT_ENABLED, (uintptr_t)true, ValueBool); + pgagroal_json_create(&js); - pgagroal_json_append(databases, (uintptr_t)js, ValueJSON); + pgagroal_json_put(js, MANAGEMENT_ARGUMENT_DATABASE, (uintptr_t)database, ValueString); + pgagroal_json_put(js, MANAGEMENT_ARGUMENT_ENABLED, (uintptr_t)true, ValueBool); + + pgagroal_json_append(databases, (uintptr_t)js, ValueJSON); + + found = true; + } } } @@ -1535,29 +1545,42 @@ accept_mgt_cb(struct ev_loop* loop, struct ev_io* watcher, int revents) pgagroal_management_create_response(payload, -1, &res); pgagroal_json_create(&databases); - for (int i = 0; i < NUMBER_OF_DISABLED; i++) + config->all_disabled = false; + + if (!strcmp("*", database)) { - bool found = false; struct json* js = NULL; - if (!strcmp("*", database)) - { - memcpy(&config->disabled[i], database, strlen(database)); - } - else if (!strcmp(config->disabled[i], "")) - { - memcpy(&config->disabled[i], database, strlen(database)); - break; - } + config->all_disabled = true; + memset(&config->disabled, 0, sizeof(config->disabled)); + + pgagroal_json_create(&js); + + pgagroal_json_put(js, MANAGEMENT_ARGUMENT_DATABASE, (uintptr_t)database, ValueString); + pgagroal_json_put(js, MANAGEMENT_ARGUMENT_ENABLED, (uintptr_t)false, ValueBool); - if (found) + pgagroal_json_append(databases, (uintptr_t)js, ValueJSON); + } + else + { + bool inserted = false; + for (int i = 0; !inserted && i < NUMBER_OF_DISABLED; i++) { - pgagroal_json_create(&js); + struct json* js = NULL; + + if (strlen(config->disabled[i]) == 0) + { + memcpy(&config->disabled[i], database, strlen(database) > MAX_DATABASE_LENGTH ? MAX_DATABASE_LENGTH : strlen(database)); + + pgagroal_json_create(&js); - pgagroal_json_put(js, MANAGEMENT_ARGUMENT_DATABASE, (uintptr_t)database, ValueString); - pgagroal_json_put(js, MANAGEMENT_ARGUMENT_ENABLED, (uintptr_t)false, ValueBool); + pgagroal_json_put(js, MANAGEMENT_ARGUMENT_DATABASE, (uintptr_t)database, ValueString); + pgagroal_json_put(js, MANAGEMENT_ARGUMENT_ENABLED, (uintptr_t)false, ValueBool); - pgagroal_json_append(databases, (uintptr_t)js, ValueJSON); + pgagroal_json_append(databases, (uintptr_t)js, ValueJSON); + + inserted = true; + } } }