From afa243cd2f20907f85d9f259cff14f4448d7684a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mattias=20Walstr=C3=B6m?= Date: Wed, 21 Feb 2024 09:20:38 +0100 Subject: [PATCH] confd: core: keep one instance of /run/system.json open Add entry in struct confd to keep it accessable for all callbacks. --- src/confd/src/core.c | 28 +++++++++++++++++----------- src/confd/src/core.h | 4 +++- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/confd/src/core.c b/src/confd/src/core.c index 68c05d5b..7120206e 100644 --- a/src/confd/src/core.c +++ b/src/confd/src/core.c @@ -15,7 +15,7 @@ uint32_t core_hook_prio(void) } int core_startup_save(sr_session_ctx_t *session, uint32_t sub_id, const char *module, - const char *xpath, sr_event_t event, unsigned request_id, void *priv) + const char *xpath, sr_event_t event, unsigned request_id, void *priv) { if (systemf("sysrepocfg -X/cfg/startup-config.cfg -d startup -f json")) return SR_ERR_SYS; @@ -24,7 +24,7 @@ int core_startup_save(sr_session_ctx_t *session, uint32_t sub_id, const char *mo } int core_pre_hook(sr_session_ctx_t *session, uint32_t sub_id, const char *module, - const char *xpath, sr_event_t event, unsigned request_id, void *priv) + const char *xpath, sr_event_t event, unsigned request_id, void *priv) { return 0; } @@ -35,7 +35,7 @@ int core_pre_hook(sr_session_ctx_t *session, uint32_t sub_id, const char *module * For details, see https://github.com/sysrepo/sysrepo/issues/2188 */ int core_post_hook(sr_session_ctx_t *session, uint32_t sub_id, const char *module, - const char *xpath, sr_event_t event, unsigned request_id, void *priv) + const char *xpath, sr_event_t event, unsigned request_id, void *priv) { static int num_changes = 0; @@ -83,9 +83,9 @@ int sr_plugin_init_cb(sr_session_ctx_t *session, void **priv) /* Save context with default running config datastore for all our models */ *priv = (void *)&confd; confd.session = session; - confd.conn = sr_session_get_connection(session); - confd.sub = NULL; - confd.fsub = NULL; + confd.conn = sr_session_get_connection(session); + confd.sub = NULL; + confd.fsub = NULL; if (!confd.conn) goto err; @@ -99,11 +99,13 @@ int sr_plugin_init_cb(sr_session_ctx_t *session, void **priv) rc = sr_session_start(confd.conn, SR_DS_CANDIDATE, &confd.cand); if (rc) goto err; - confd.aug = aug_init(NULL, "", 0); if (!confd.aug) goto err; + confd.root = json_load_file("/run/system.json", 0, NULL); + if (!confd.root) + goto err; rc = ietf_interfaces_init(&confd); if (rc) goto err; @@ -117,10 +119,10 @@ int sr_plugin_init_cb(sr_session_ctx_t *session, void **priv) if (rc) goto err; rc = ietf_factory_default_init(&confd); - if(rc) + if (rc) goto err; rc = ietf_routing_init(&confd); - if(rc) + if (rc) goto err; rc = infix_system_sw_init(&confd); if (rc) @@ -135,8 +137,11 @@ int sr_plugin_init_cb(sr_session_ctx_t *session, void **priv) /* YOUR_INIT GOES HERE */ return SR_ERR_OK; + err: ERROR("init failed: %s", sr_strerror(rc)); + if (confd.root) + json_decref(confd.root); sr_unsubscribe(confd.sub); sr_unsubscribe(confd.fsub); @@ -147,7 +152,8 @@ void sr_plugin_cleanup_cb(sr_session_ctx_t *session, void *priv) { struct confd *confd = (struct confd *)priv; - sr_unsubscribe(confd->sub); - sr_unsubscribe(confd->fsub); + sr_unsubscribe(confd->sub); + sr_unsubscribe(confd->fsub); + json_decref(confd->root); closelog(); } diff --git a/src/confd/src/core.h b/src/confd/src/core.h index f6d5a44e..51f6e034 100644 --- a/src/confd/src/core.h +++ b/src/confd/src/core.h @@ -21,6 +21,8 @@ #include #include +#include + #include #include #include @@ -59,7 +61,7 @@ struct confd { sr_conn_ctx_t *conn; sr_subscription_ctx_t *sub; sr_subscription_ctx_t *fsub; /* factory-default sub */ - + json_t *root; augeas *aug; struct dagger netdag; };