From: Arran Cudbard-Bell Date: Tue, 5 Nov 2019 01:36:25 +0000 (-0600) Subject: Allow dictionary root dir to be manipulated at runtime X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7bce2879f40bceec124b8b7f7fcc8a08a776b2fb;p=thirdparty%2Ffreeradius-server.git Allow dictionary root dir to be manipulated at runtime This is to allow us to load test dictionaries --- diff --git a/src/lib/util/dict.h b/src/lib/util/dict.h index 41bd8d2469e..c240c86e975 100644 --- a/src/lib/util/dict.h +++ b/src/lib/util/dict.h @@ -414,13 +414,21 @@ void fr_dl_dict_autofree(dl_t const *module, void *symbol, void *user_ctx); int fr_dl_dict_attr_autoload(dl_t const *module, void *symbol, void *user_ctx); /** @} */ +/** @name Allocating and freeing + * + * @{ + */ void fr_dict_free(fr_dict_t **dict); +/** @} */ + /** @name Initialisation * * @{ */ int fr_dict_global_init(TALLOC_CTX *ctx, char const *dict_dir); + + int fr_dict_dir_set(char const *dict_dir); /** @} */ /** @name Dictionary testing and validation diff --git a/src/lib/util/dict_tokenize.c b/src/lib/util/dict_tokenize.c index 3aabb7ffc8e..017587f94ae 100644 --- a/src/lib/util/dict_tokenize.c +++ b/src/lib/util/dict_tokenize.c @@ -2406,7 +2406,7 @@ int fr_dict_read(fr_dict_t *dict, char const *dir, char const *filename) INTERNAL_IF_NULL(dict, -1); if (!dict->attributes_by_name) { - fr_strerror_printf("%s: Must call fr_dict_internal_afrom_file() before fr_dict_read()", __FUNCTION__); + fr_strerror_printf("%s: Must initialise dictionary before calling fr_dict_read()", __FUNCTION__); return -1; } diff --git a/src/lib/util/dict_util.c b/src/lib/util/dict_util.c index ee92e16963b..a7fe0854fa6 100644 --- a/src/lib/util/dict_util.c +++ b/src/lib/util/dict_util.c @@ -2472,7 +2472,7 @@ static int dict_onload_func(dl_t const *dl, void *symbol, UNUSED void *user_ctx) * * @note Must be called before any other dictionary functions. * - * @param[in] ctx to allocate protocol hashes in. + * @param[in] ctx to allocate global resources in. * @param[in] dict_dir the default location for the dictionaries. * @return * - 0 on success. @@ -2480,8 +2480,11 @@ static int dict_onload_func(dl_t const *dl, void *symbol, UNUSED void *user_ctx) */ int fr_dict_global_init(TALLOC_CTX *ctx, char const *dict_dir) { + TALLOC_FREE(dict_ctx); + dict_ctx = ctx; + if (!protocol_by_name) { - protocol_by_name = fr_hash_table_create(ctx, dict_protocol_name_hash, dict_protocol_name_cmp, NULL); + protocol_by_name = fr_hash_table_create(dict_ctx, dict_protocol_name_hash, dict_protocol_name_cmp, NULL); if (!protocol_by_name) { fr_strerror_printf("Failed initializing protocol_by_name hash"); return -1; @@ -2489,7 +2492,7 @@ int fr_dict_global_init(TALLOC_CTX *ctx, char const *dict_dir) } if (!protocol_by_num) { - protocol_by_num = fr_hash_table_create(ctx, dict_protocol_num_hash, dict_protocol_num_cmp, NULL); + protocol_by_num = fr_hash_table_create(dict_ctx, dict_protocol_num_hash, dict_protocol_num_cmp, NULL); if (!protocol_by_num) { fr_strerror_printf("Failed initializing protocol_by_num hash"); return -1; @@ -2497,7 +2500,7 @@ int fr_dict_global_init(TALLOC_CTX *ctx, char const *dict_dir) } talloc_free(dict_dir_default); /* Free previous value */ - dict_dir_default = talloc_strdup(ctx, dict_dir); + dict_dir_default = talloc_strdup(dict_ctx, dict_dir); dict_loader = dl_loader_init(ctx, NULL, NULL, false, false); if (!dict_loader) return -1; @@ -2511,6 +2514,22 @@ int fr_dict_global_init(TALLOC_CTX *ctx, char const *dict_dir) return 0; } +/** Allow the default dict dir to be changed after initialisation + * + * @param[in] dict_dir New default dict dir to use. + * @return + * - 0 on success. + * - -1 on failure. + */ +int fr_dict_dir_set(char const *dict_dir) +{ + talloc_free(dict_dir_default); /* Free previous value */ + dict_dir_default = talloc_strdup(dict_ctx, dict_dir); + if (!dict_dir_default) return -1; + + return 0; +} + /* * [a-zA-Z0-9_-:.]+ */