diff options
author | David Lamparter <equinox@diac24.net> | 2019-02-04 22:56:50 +0100 |
---|---|---|
committer | David Lamparter <equinox@diac24.net> | 2019-02-19 21:41:39 +0100 |
commit | 591f57cff3e035439dff7c39a9bd688892fa5363 (patch) | |
tree | e52873cf2ad95b7cf029809244991dac48ad8e82 /lib | |
parent | watchfrr: build in defaults for -r/-s/-k (diff) | |
download | frr-591f57cff3e035439dff7c39a9bd688892fa5363.tar.xz frr-591f57cff3e035439dff7c39a9bd688892fa5363.zip |
lib: yang: use common yang_ctx_new_setup()
After creating a libyang context, we need to hook up our callback to use
embedded built-in modules. I hadn't added this to the yang translator
code.
Also, ly_ctx_new fails if the search directory doesn't exist. Since
that's not a hard error for us, work around that and ignore inaccessible
YANG_MODELS_DIR. (This is needed for snap packages.)
Signed-off-by: David Lamparter <equinox@diac24.net>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/yang.c | 27 | ||||
-rw-r--r-- | lib/yang.h | 5 | ||||
-rw-r--r-- | lib/yang_translator.c | 6 |
3 files changed, 31 insertions, 7 deletions
diff --git a/lib/yang.c b/lib/yang.c index f62a8163f..1d8e82eb2 100644 --- a/lib/yang.c +++ b/lib/yang.c @@ -649,6 +649,29 @@ CPP_NOTICE("lib/yang: time to remove non-LIBYANG_EXT_BUILTIN support") extern struct lytype_plugin_list frr_user_types[]; #endif +struct ly_ctx *yang_ctx_new_setup(void) +{ + struct ly_ctx *ctx; + const char *yang_models_path = YANG_MODELS_PATH; + + if (access(yang_models_path, R_OK | X_OK)) { + yang_models_path = NULL; + if (errno == ENOENT) + zlog_info("yang model directory \"%s\" does not exist", + YANG_MODELS_PATH); + else + flog_err_sys(EC_LIB_LIBYANG, + "cannot access yang model directory \"%s\"", + YANG_MODELS_PATH); + } + + ctx = ly_ctx_new(yang_models_path, LY_CTX_DISABLE_SEARCHDIR_CWD); + if (!ctx) + return NULL; + ly_ctx_set_module_imp_clb(ctx, yang_module_imp_clb, NULL); + return ctx; +} + void yang_init(void) { #ifndef LIBYANG_EXT_BUILTIN @@ -677,13 +700,11 @@ CPP_NOTICE("lib/yang: deprecated libyang <0.16.74 extension loading in use!") #endif /* Initialize libyang container for native models. */ - ly_native_ctx = - ly_ctx_new(YANG_MODELS_PATH, LY_CTX_DISABLE_SEARCHDIR_CWD); + ly_native_ctx = yang_ctx_new_setup(); if (!ly_native_ctx) { flog_err(EC_LIB_LIBYANG, "%s: ly_ctx_new() failed", __func__); exit(1); } - ly_ctx_set_module_imp_clb(ly_native_ctx, yang_module_imp_clb, NULL); ly_ctx_set_priv_dup_clb(ly_native_ctx, ly_dup_cb); #ifndef LIBYANG_EXT_BUILTIN diff --git a/lib/yang.h b/lib/yang.h index 4680db08d..885218272 100644 --- a/lib/yang.h +++ b/lib/yang.h @@ -514,6 +514,11 @@ extern void yang_data_free(struct yang_data *data); extern struct list *yang_data_list_new(void); /* + * Create and set up a libyang context (for use by the translator) + */ +extern struct ly_ctx *yang_ctx_new_setup(void); + +/* * Initialize the YANG subsystem. Should be called only once during the * daemon initialization process. */ diff --git a/lib/yang_translator.c b/lib/yang_translator.c index c3092e56e..6d6f92836 100644 --- a/lib/yang_translator.c +++ b/lib/yang_translator.c @@ -162,8 +162,7 @@ struct yang_translator *yang_translator_load(const char *path) RB_INSERT(yang_translators, &yang_translators, translator); /* Initialize the translator libyang context. */ - translator->ly_ctx = - ly_ctx_new(YANG_MODELS_PATH, LY_CTX_DISABLE_SEARCHDIR_CWD); + translator->ly_ctx = yang_ctx_new_setup(); if (!translator->ly_ctx) { flog_warn(EC_LIB_LIBYANG, "%s: ly_ctx_new() failed", __func__); goto error; @@ -525,8 +524,7 @@ static void str_replace(char *o_string, const char *s_string, void yang_translator_init(void) { - ly_translator_ctx = - ly_ctx_new(YANG_MODELS_PATH, LY_CTX_DISABLE_SEARCHDIR_CWD); + ly_translator_ctx = yang_ctx_new_setup(); if (!ly_translator_ctx) { flog_err(EC_LIB_LIBYANG, "%s: ly_ctx_new() failed", __func__); exit(1); |