From 3137c74ec0090d352443940918deaf5540310c7a Mon Sep 17 00:00:00 2001 From: kirill reshke Date: Tue, 30 Nov 2021 17:45:09 +0500 Subject: [PATCH] Fix show storages to actually show storages (#370) --- sources/config.c | 14 ++++---- sources/console.c | 92 ++++++++++++++++++++++++++++++----------------- sources/rules.c | 61 ++++++++++++++++++++++++++++++- sources/rules.h | 5 +++ sources/system.c | 8 +++++ 5 files changed, 138 insertions(+), 42 deletions(-) diff --git a/sources/config.c b/sources/config.c index 14868a16..3eb15fad 100644 --- a/sources/config.c +++ b/sources/config.c @@ -225,9 +225,8 @@ int od_config_validate(od_config_t *config, od_logger_t *logger) "online restart or/and enable bindwith_reuseport"); return NOT_OK_RESPONSE; } - return OK_RESPONSE; - return 0; + return OK_RESPONSE; } static inline char *od_config_yes_no(int value) @@ -334,23 +333,22 @@ void od_config_print(od_config_t *config, od_logger_t *logger) listen->backlog); if (listen->tls_opts->tls) od_log(logger, "config", NULL, NULL, - " tls_opts->tls %s", - listen->tls_opts->tls); + " tls %s", listen->tls_opts->tls); if (listen->tls_opts->tls_ca_file) od_log(logger, "config", NULL, NULL, - " tls_opts->tls_ca_file %s", + " tls_ca_file %s", listen->tls_opts->tls_ca_file); if (listen->tls_opts->tls_key_file) od_log(logger, "config", NULL, NULL, - " tls_opts->tls_key_file %s", + " tls_key_file %s", listen->tls_opts->tls_key_file); if (listen->tls_opts->tls_cert_file) od_log(logger, "config", NULL, NULL, - " tls_opts->tls_cert_file %s", + " tls_cert_file %s", listen->tls_opts->tls_cert_file); if (listen->tls_opts->tls_protocols) od_log(logger, "config", NULL, NULL, - " tls_opts->tls_protocols %s", + " tls_protocols %s", listen->tls_opts->tls_protocols); od_log(logger, "config", NULL, NULL, ""); } diff --git a/sources/console.c b/sources/console.c index bd21e1b8..0a7da3bc 100644 --- a/sources/console.c +++ b/sources/console.c @@ -698,6 +698,7 @@ static inline int od_console_show_databases_add_cb(od_route_t *route, od_rule_t *rule = route->rule; od_rule_storage_t *storage = rule->storage; + /* host */ char *host = storage->host; if (!host) { host = ""; @@ -1343,11 +1344,11 @@ static inline int od_console_write_nullable_str(machine_msg_t *stream, return kiwi_be_write_data_row_add(stream, offset, str, strlen(str)); } -static inline int od_console_show_tls_options(od_config_listen_t *listen_config, +static inline int od_console_show_tls_options(od_tls_opts_t *tls_opts, int offset, od_client_t *client, machine_msg_t *stream) { - char *tls = od_config_tls_to_str(listen_config->tls_opts->tls_mode); + char *tls = od_config_tls_to_str(tls_opts->tls_mode); od_retcode_t rc; @@ -1358,29 +1359,29 @@ static inline int od_console_show_tls_options(od_config_listen_t *listen_config, } /* tls_cert_file */ - rc = od_console_write_nullable_str( - stream, offset, listen_config->tls_opts->tls_cert_file); + rc = od_console_write_nullable_str(stream, offset, + tls_opts->tls_cert_file); if (rc != OK_RESPONSE) { return rc; } /* tls_key_file */ - rc = od_console_write_nullable_str( - stream, offset, listen_config->tls_opts->tls_key_file); + rc = od_console_write_nullable_str(stream, offset, + tls_opts->tls_key_file); if (rc != OK_RESPONSE) { return rc; } /* tls_ca_file */ - rc = od_console_write_nullable_str( - stream, offset, listen_config->tls_opts->tls_ca_file); + rc = od_console_write_nullable_str(stream, offset, + tls_opts->tls_ca_file); if (rc != OK_RESPONSE) { return rc; } /* tls_protocols */ - rc = od_console_write_nullable_str( - stream, offset, listen_config->tls_opts->tls_protocols); + rc = od_console_write_nullable_str(stream, offset, + tls_opts->tls_protocols); if (rc != OK_RESPONSE) { return rc; } @@ -1439,8 +1440,8 @@ static inline int od_console_show_listen(od_client_t *client, return rc; } - rc = od_console_show_tls_options(listen_config, offset, client, - stream); + rc = od_console_show_tls_options(listen_config->tls_opts, + offset, client, stream); if (rc != OK_RESPONSE) { return rc; } @@ -1456,8 +1457,8 @@ static inline int od_console_show_storages(od_client_t *client, od_router_t *router = client->global->router; machine_msg_t *msg; - msg = kiwi_be_write_row_descriptionf(stream, "sdsssss", "host", "port", - "tls", "tls_cert_file", + msg = kiwi_be_write_row_descriptionf(stream, "ssdsssss", "type", "host", + "port", "tls", "tls_cert_file", "tls_key_file", "tls_ca_file", "tls_protocols"); @@ -1466,49 +1467,74 @@ static inline int od_console_show_storages(od_client_t *client, } od_instance_t *instance = router->global->instance; - od_config_t *config = &instance->config; + od_rules_t *rules = &router->rules; char data[64]; int data_len; int rc; int offset; + pthread_mutex_lock(&rules->mu); + od_list_t *i; - od_list_foreach(&config->listen, i) + od_list_foreach(&rules->storages, i) { - od_config_listen_t *listen_config; - listen_config = od_container_of(i, od_config_listen_t, link); + od_rule_storage_t *storage; + storage = od_container_of(i, od_rule_storage_t, link); msg = kiwi_be_write_data_row(stream, &offset); if (msg == NULL) { - return NOT_OK_RESPONSE; + rc = NOT_OK_RESPONSE; + goto error; } - /* host */ - rc = od_console_write_nullable_str(stream, offset, - listen_config->host); - - if (rc != OK_RESPONSE) { - return rc; + if (storage->storage_type == OD_RULE_STORAGE_REMOTE) { + rc = kiwi_be_write_data_row_add(stream, offset, + "remote", 6 + 1); + if (rc == NOT_OK_RESPONSE) { + goto error; + } + } else { + rc = kiwi_be_write_data_row_add(stream, offset, "local", + 5 + 1); + if (rc == NOT_OK_RESPONSE) { + goto error; + } } + char *host = storage->host; + if (!host) { + host = ""; + } + + rc = kiwi_be_write_data_row_add(stream, offset, host, + strlen(host)); + if (rc == NOT_OK_RESPONSE) { + goto error; + } + + char data[64]; + int data_len; + /* port */ - data_len = od_snprintf(data, sizeof(data), "%d", - listen_config->port); + data_len = od_snprintf(data, sizeof(data), "%d", storage->port); rc = kiwi_be_write_data_row_add(stream, offset, data, data_len); - - if (rc != OK_RESPONSE) { - return rc; + if (rc == NOT_OK_RESPONSE) { + goto error; } - rc = od_console_show_tls_options(listen_config, offset, client, - stream); + rc = od_console_show_tls_options(storage->tls_opts, offset, + client, stream); if (rc != OK_RESPONSE) { - return rc; + goto error; } } + pthread_mutex_unlock(&rules->mu); return kiwi_be_write_complete(stream, "SHOW", 5); +error: + pthread_mutex_unlock(&rules->mu); + return rc; } static inline int od_console_show(od_client_t *client, machine_msg_t *stream, diff --git a/sources/rules.c b/sources/rules.c index ae3d7c62..b716943f 100644 --- a/sources/rules.c +++ b/sources/rules.c @@ -14,6 +14,7 @@ void od_rules_init(od_rules_t *rules) { + pthread_mutex_init(&rules->mu, NULL); od_list_init(&rules->storages); #ifdef LDAP_FOUND od_list_init(&rules->ldap_endpoints); @@ -25,6 +26,7 @@ void od_rules_rule_free(od_rule_t *); void od_rules_free(od_rules_t *rules) { + pthread_mutex_destroy(&rules->mu); od_list_t *i, *n; od_list_foreach_safe(&rules->rules, i, n) { @@ -938,8 +940,13 @@ int od_rules_validate(od_rules_t *rules, od_config_t *config, } } + return 0; +} + +int od_rules_cleanup(od_rules_t *rules) +{ /* cleanup declarative storages rules data */ - od_list_t *n; + od_list_t *n, *i; od_list_foreach_safe(&rules->storages, i, n) { od_rule_storage_t *storage; @@ -948,8 +955,19 @@ int od_rules_validate(od_rules_t *rules, od_config_t *config, } od_list_init(&rules->storages); #ifdef LDAP_FOUND + + /* TODO: cleanup ldap + od_list_foreach_safe(&rules->storages, i, n) + { + od_ldap_endpoint_t *endp; + storage = od_container_of(i, od_ldap_endpoint_t, link); + od_ldap_endpoint_free(endp); + } + */ + od_list_init(&rules->ldap_endpoints); #endif + return 0; } @@ -961,6 +979,47 @@ static inline char *od_rules_yes_no(int value) void od_rules_print(od_rules_t *rules, od_logger_t *logger) { od_list_t *i; + od_log(logger, "config", NULL, NULL, "storages"); + + od_list_foreach(&rules->storages, i) + { + od_rule_storage_t *storage; + storage = od_container_of(i, od_rule_storage_t, link); + + od_log(logger, "storage", NULL, NULL, + " storage types %s", + storage->storage_type == OD_RULE_STORAGE_REMOTE ? + "remote" : + "local"); + + od_log(logger, "storage", NULL, NULL, " host %s", + storage->host ? storage->host : ""); + + od_log(logger, "storage", NULL, NULL, " port %d", + storage->port); + + if (storage->tls_opts->tls) + od_log(logger, "storage", NULL, NULL, + " tls %s", storage->tls_opts->tls); + if (storage->tls_opts->tls_ca_file) + od_log(logger, "storage", NULL, NULL, + " tls_ca_file %s", + storage->tls_opts->tls_ca_file); + if (storage->tls_opts->tls_key_file) + od_log(logger, "storage", NULL, NULL, + " tls_key_file %s", + storage->tls_opts->tls_key_file); + if (storage->tls_opts->tls_cert_file) + od_log(logger, "storage", NULL, NULL, + " tls_cert_file %s", + storage->tls_opts->tls_cert_file); + if (storage->tls_opts->tls_protocols) + od_log(logger, "storage", NULL, NULL, + " tls_protocols %s", + storage->tls_opts->tls_protocols); + od_log(logger, "storage", NULL, NULL, ""); + } + od_list_foreach(&rules->rules, i) { od_rule_t *rule; diff --git a/sources/rules.h b/sources/rules.h index 6e1dc0eb..a28e2f3b 100644 --- a/sources/rules.h +++ b/sources/rules.h @@ -148,6 +148,7 @@ struct od_rule { }; struct od_rules { + pthread_mutex_t mu; od_list_t storages; #ifdef LDAP_FOUND od_list_t ldap_endpoints; @@ -155,6 +156,8 @@ struct od_rules { od_list_t rules; }; +/* rules */ + void od_rules_init(od_rules_t *); void od_rules_free(od_rules_t *); int od_rules_validate(od_rules_t *, od_config_t *, od_logger_t *); @@ -162,6 +165,8 @@ int od_rules_merge(od_rules_t *, od_rules_t *, od_list_t *added, od_list_t *deleted); void od_rules_print(od_rules_t *, od_logger_t *); +int od_rules_cleanup(od_rules_t *rules); + /* rule */ od_rule_t *od_rules_add(od_rules_t *); void od_rules_ref(od_rule_t *); diff --git a/sources/system.c b/sources/system.c index bb1ce83e..b72faa67 100644 --- a/sources/system.c +++ b/sources/system.c @@ -341,6 +341,10 @@ void od_system_config_reload(od_system_t *system) od_log(&instance->logger, "config", NULL, NULL, "importing changes from '%s'", instance->config_file); + pthread_mutex_lock(&router->rules.mu); + + od_rules_cleanup(&router->rules); + od_error_t error; od_error_init(&error); @@ -356,6 +360,7 @@ void od_system_config_reload(od_system_t *system) if (rc == -1) { od_error(&instance->logger, "config", NULL, NULL, "%s", error.error); + pthread_mutex_unlock(&router->rules.mu); od_config_free(&config); od_rules_free(&rules); return; @@ -363,6 +368,7 @@ void od_system_config_reload(od_system_t *system) rc = od_config_validate(&config, &instance->logger); if (rc == -1) { + pthread_mutex_unlock(&router->rules.mu); od_config_free(&config); od_rules_free(&rules); return; @@ -371,6 +377,8 @@ void od_system_config_reload(od_system_t *system) rc = od_rules_validate(&rules, &config, &instance->logger); od_config_reload(&instance->config, &config); + pthread_mutex_unlock(&router->rules.mu); + /* Reload TLS certificates */ od_list_t *i; od_list_foreach(&router->servers, i)