diff --git a/src/knot/conf/base.c b/src/knot/conf/base.c index ad195aa4a536ab63222c2b2f42df426e6416b48f..11045a44cbf41acd2ba5fe364a7064078a3be73d 100644 --- a/src/knot/conf/base.c +++ b/src/knot/conf/base.c @@ -137,12 +137,20 @@ int conf_refresh( return conf->api->txn_begin(conf->db, &conf->read_txn, KNOT_DB_RDONLY); } -static void init_values( +void conf_refresh_hostname( conf_t *conf) { + if (conf == NULL) { + return; + } + free(conf->hostname); conf->hostname = sockaddr_hostname(); +} +static void init_cache( + conf_t *conf) +{ conf->cache.srv_nsid = conf_get(conf, C_SRV, C_NSID); conf->cache.srv_max_udp_payload = conf_get(conf, C_SRV, C_MAX_UDP_PAYLOAD); conf->cache.srv_max_tcp_clients = conf_get(conf, C_SRV, C_MAX_TCP_CLIENTS); @@ -237,8 +245,13 @@ int conf_new( // Initialize query modules list. init_list(&out->query_modules); + // Cache the current hostname. + if (!(flags & CONF_FNOHOSTNAME)) { + out->hostname = sockaddr_hostname(); + } + // Initialize cached values. - init_values(out); + init_cache(out); *conf = out; @@ -293,8 +306,13 @@ int conf_clone( // Initialize query modules list. init_list(&out->query_modules); + // Reuse the hostname. + if (s_conf->hostname != NULL) { + out->hostname = strdup(s_conf->hostname); + } + // Initialize cached values. - init_values(out); + init_cache(out); out->is_clone = true; @@ -748,7 +766,7 @@ int conf_import( } // Update cached values. - init_values(conf); + init_cache(conf); // Reset the filename. free(conf->filename); diff --git a/src/knot/conf/base.h b/src/knot/conf/base.h index 3aef2afd8b89e93f5fdc99429fd2c32a10df519e..c3df34ddfe5673cb375fba1a2ddc47d6250cfadc 100644 --- a/src/knot/conf/base.h +++ b/src/knot/conf/base.h @@ -109,9 +109,10 @@ typedef struct { * Configuration access flags. */ typedef enum { - CONF_FNONE = 0, /*!< Empty flag. */ - CONF_FREADONLY = 1 << 0, /*!< Read only access. */ - CONF_FNOCHECK = 1 << 1 /*!< Disabled confdb check. */ + CONF_FNONE = 0, /*!< Empty flag. */ + CONF_FREADONLY = 1 << 0, /*!< Read only access. */ + CONF_FNOCHECK = 1 << 1, /*!< Disabled confdb check. */ + CONF_FNOHOSTNAME = 1 << 2, /*!< Don't set the hostname. */ } conf_flag_t; /*! @@ -130,6 +131,15 @@ int conf_refresh( conf_t *conf ); +/*! + * Refreshes cached hostname. + * + * \param[in] conf Configuration. + */ +void conf_refresh_hostname( + conf_t *conf +); + /*! * Creates new or opens old configuration database. * diff --git a/src/knot/ctl/commands.c b/src/knot/ctl/commands.c index 62c1d87ca0914e7ec0327386ce5549719868dcb1..93fc184824303557e9f20a2e81977fc1de592c0e 100644 --- a/src/knot/ctl/commands.c +++ b/src/knot/ctl/commands.c @@ -269,7 +269,7 @@ static int ctl_reload(server_t *s, remote_cmdargs_t *a) UNUSED(s); UNUSED(a); - return server_reload(s, conf()->filename); + return server_reload(s, conf()->filename, true); } static int ctl_zone_status(server_t *s, remote_cmdargs_t *a) @@ -375,7 +375,7 @@ static int ctl_conf_commit(server_t *s, remote_cmdargs_t *a) return ret; } - return server_reload(s, NULL); + return server_reload(s, NULL, false); } static int ctl_conf_abort(server_t *s, remote_cmdargs_t *a) diff --git a/src/knot/server/server.c b/src/knot/server/server.c index 70597c808af5d2a3308391f43644645d9138edce..40efe280dee787a5b1982d9f70c16ea602bdcda5 100644 --- a/src/knot/server/server.c +++ b/src/knot/server/server.c @@ -506,7 +506,7 @@ void server_wait(server_t *server) } } -int server_reload(server_t *server, const char *cf) +int server_reload(server_t *server, const char *cf, bool refresh_hostname) { if (server == NULL) { return KNOT_EINVAL; @@ -545,6 +545,11 @@ int server_reload(server_t *server, const char *cf) conf_activate_modules(new_conf, NULL, &new_conf->query_modules, &new_conf->query_plan); + /* Refresh hostname. */ + if (refresh_hostname) { + conf_refresh_hostname(new_conf); + } + /* Update to the new config. */ conf_update(new_conf); diff --git a/src/knot/server/server.h b/src/knot/server/server.h index 3026725456ffaffc65f251cb448959269e8bd725..3998672577be3d915c14ee1c1a1770b193ebf994 100644 --- a/src/knot/server/server.h +++ b/src/knot/server/server.h @@ -155,11 +155,12 @@ void server_wait(server_t *server); /*! * \brief Reload server configuration. * - * \param server Server instance. - * \param cf Config file path. + * \param server Server instance. + * \param cf Config file path. + * \param refresh_hostname Refresh hostname indicator. * \return */ -int server_reload(server_t *server, const char *cf); +int server_reload(server_t *server, const char *cf, bool refresh_hostname); /*! * \brief Requests server to stop. diff --git a/src/utils/knotc/main.c b/src/utils/knotc/main.c index 2cd1e3916bb537df4dddddbc82ee7c50fff406a2..12d0850fde0e4bd836c63374a18520db0b27fc02 100644 --- a/src/utils/knotc/main.c +++ b/src/utils/knotc/main.c @@ -94,7 +94,7 @@ static int set_config(const cmd_desc_t *desc, const char *confdb, (src != NULL) ? src : "empty"); /* Prepare config flags. */ - conf_flag_t conf_flags = CONF_FNONE; + conf_flag_t conf_flags = CONF_FNOHOSTNAME; if (confdb != NULL && !(desc->flags & CMD_CONF_FWRITE)) { conf_flags |= CONF_FREADONLY; } diff --git a/src/utils/knotd/main.c b/src/utils/knotd/main.c index 5e2f1d2bf6f0cb82046ecee863b771dca1c60458..91dd8e7fe78d613eae25b395a4ff11371159d734 100644 --- a/src/utils/knotd/main.c +++ b/src/utils/knotd/main.c @@ -245,7 +245,7 @@ static void event_loop(server_t *server, char *socket) } if (sig_req_reload) { sig_req_reload = false; - server_reload(server, conf()->filename); + server_reload(server, conf()->filename, true); } /* Control interface. */