/** * @brief Check the session and reconnect if it is needed. */ static void sr_pd_session_check(sr_pd_ctx_t *ctx) { int rc = SR_ERR_OK; CHECK_NULL_ARG_VOID(ctx); rc = sr_session_check(ctx->session); if (SR_ERR_OK != rc) { SR_LOG_DBG_MSG("Reconnecting to Sysrepo Engine."); /* disconnect */ sr_session_stop(ctx->session); sr_disconnect(ctx->connection); ctx->session = NULL; ctx->connection = NULL; /* reconnect */ rc = sr_connect("sysrepo-plugind", connect_options, &ctx->connection); if (SR_ERR_OK == rc) { rc = sr_session_start(ctx->connection, SR_DS_STARTUP, SR_SESS_DEFAULT, &ctx->session); } if (SR_ERR_OK != rc) { SR_LOG_ERR("Error by reconnecting to Sysrepo Engine: %s", sr_strerror(rc)); } } }
void sr_log_syslog(sr_log_level_t log_level) { #if SR_LOGGING_ENABLED sr_ll_syslog = log_level; SR_LOG_DBG("Setting log level for syslog logs to %d.", log_level); if ((SR_LL_NONE != log_level) && !sr_syslog_enabled) { /* if syslog connection hasn't been initiated yet, initiate it now */ if (NULL == sr_syslog_identifier) { /* if we don't have any syslog identifier set, sr_logger_init hasn't been called yet */ sr_logger_init(NULL); } /* open the connection to syslog */ openlog((char*)sr_syslog_identifier, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_DAEMON); sr_syslog_enabled = true; SR_LOG_DBG_MSG("Opening the connection to system logger (syslog)."); } #endif }
/** * @brief Import content of the specified datastore for the given module from a file * referenced by the descriptor 'fd_in' */ static int srcfg_import_datastore(struct ly_ctx *ly_ctx, int fd_in, const char *module_name, srcfg_datastore_t datastore, LYD_FORMAT format, bool permanent) { int rc = SR_ERR_INTERNAL; unsigned i = 0; struct lyd_node *new_data_tree = NULL; struct lyd_node *current_data_tree = NULL; struct lyd_difflist *diff = NULL; char *first_xpath = NULL, *second_xpath = NULL; char *input_data = NULL; int ret = 0; struct stat info; CHECK_NULL_ARG2(ly_ctx, module_name); /* parse input data */ ret = fstat(fd_in, &info); CHECK_NOT_MINUS1_LOG_GOTO(ret, rc, SR_ERR_INTERNAL, cleanup, "Unable to obtain input file info: %s.", sr_strerror_safe(errno)); ly_errno = LY_SUCCESS; if (S_ISREG(info.st_mode)) { /* load (using mmap) and parse the input data in one step */ new_data_tree = lyd_parse_fd(ly_ctx, fd_in, format, LYD_OPT_STRICT | LYD_OPT_CONFIG); } else { /* most likely STDIN */ /* load input data into the memory first */ ret = srcfg_read_file_content(fd_in, &input_data); CHECK_RC_MSG_GOTO(ret, cleanup, "Unable to read the input data."); /* parse the input data stored inside memory buffer */ new_data_tree = lyd_parse_mem(ly_ctx, input_data, format, LYD_OPT_STRICT | LYD_OPT_CONFIG); } if (NULL == new_data_tree && LY_SUCCESS != ly_errno) { SR_LOG_ERR("Unable to parse the input data: %s", ly_errmsg()); goto cleanup; } /* validate input data */ if (NULL != new_data_tree) { ret = lyd_validate(&new_data_tree, LYD_OPT_STRICT | LYD_OPT_CONFIG | LYD_WD_IMPL_TAG); CHECK_ZERO_LOG_GOTO(ret, rc, SR_ERR_INTERNAL, cleanup, "Input data are not valid: %s", ly_errmsg()); } /* remove default nodes */ lyd_wd_cleanup(&new_data_tree, 0); /* get data tree of currently stored configuration */ rc = srcfg_get_module_data(ly_ctx, module_name, ¤t_data_tree); if (SR_ERR_OK != rc) { goto cleanup; } /* get the list of changes made by the user */ diff = lyd_diff(current_data_tree, new_data_tree, 0); if (NULL == diff) { SR_LOG_ERR("Unable to get the list of changes: %s", ly_errmsg()); goto cleanup; } /* iterate over the list of differences and for each issue corresponding Sysrepo command(s) */ while (diff->type && LYD_DIFF_END != diff->type[i]) { if (NULL != diff->first[i]) { first_xpath = lyd_path(diff->first[i]); if (NULL == first_xpath) { SR_LOG_ERR("Error returned from lyd_path: %s.", ly_errmsg()); goto cleanup; } } if (NULL != diff->second[i]) { second_xpath = lyd_path(diff->second[i]); if (NULL == second_xpath) { free(first_xpath); first_xpath = NULL; SR_LOG_ERR("Error returned from lyd_path: %s.", ly_errmsg()); goto cleanup; } } switch (diff->type[i]) { case LYD_DIFF_DELETED: SR_LOG_DBG("<LYD_DIFF_DELETED> node: %s", first_xpath); rc = srcfg_convert_lydiff_deleted(first_xpath); break; case LYD_DIFF_CHANGED: SR_LOG_DBG("<LYD_DIFF_CHANGED> orig: %s, new: %s", first_xpath, second_xpath); rc = srcfg_convert_lydiff_changed(first_xpath, diff->second[i]); break; case LYD_DIFF_MOVEDAFTER1: SR_LOG_DBG("<LYD_DIFF_MOVEDAFTER1> moved: %s, after: %s", first_xpath, second_xpath); rc = srcfg_convert_lydiff_movedafter(first_xpath, second_xpath); break; case LYD_DIFF_CREATED: SR_LOG_DBG("<LYD_DIFF_CREATED> parent: %s, new node: %s", first_xpath, second_xpath); rc = srcfg_convert_lydiff_created(diff->second[i]); break; case LYD_DIFF_MOVEDAFTER2: SR_LOG_DBG("<LYD_DIFF_MOVEDAFTER2> after: %s, this new node was inserted: %s", first_xpath, second_xpath); rc = srcfg_convert_lydiff_movedafter(second_xpath, first_xpath); break; default: assert(0 && "not reachable"); } free(first_xpath); free(second_xpath); first_xpath = second_xpath = NULL; if (SR_ERR_OK != rc) { goto cleanup; } ++i; } if (0 == i) { SR_LOG_DBG_MSG("No changes were made."); } else { /* commit the changes */ rc = sr_commit(srcfg_session); if (SR_ERR_OK != rc) { SR_LOG_ERR("Error returned from sr_commit: %s.", sr_strerror(rc)); goto cleanup; } if (SRCFG_STORE_RUNNING == datastore && permanent) { /* copy running datastore data into the startup datastore */ rc = sr_copy_config(srcfg_session, module_name, SR_DS_RUNNING, SR_DS_STARTUP); if (SR_ERR_OK != rc) { SR_LOG_ERR("Error returned from sr_copy_config: %s.", sr_strerror(rc)); goto cleanup; } } } rc = SR_ERR_OK; cleanup: if (NULL != diff) { lyd_free_diff(diff); } if (NULL != current_data_tree) { lyd_free_withsiblings(current_data_tree); } if (NULL != new_data_tree) { lyd_free_withsiblings(new_data_tree); } if (input_data) { free(input_data); } return rc; }
/** * @brief Main routine of the sysrepo daemon. */ int main(int argc, char* argv[]) { sr_pd_ctx_t ctx = { 0, }; pid_t parent_pid = 0; int pidfile_fd = -1; int c = 0; bool debug_mode = false; int log_level = -1; int rc = SR_ERR_OK; while ((c = getopt (argc, argv, "hvdDl:")) != -1) { switch (c) { case 'v': sr_pd_print_version(); return 0; break; case 'd': debug_mode = true; break; case 'D': connect_options |= SR_CONN_DAEMON_START; break; case 'l': log_level = atoi(optarg); break; default: sr_pd_print_help(); return 0; } } /* init logger */ sr_logger_init("sysrepo-plugind"); /* daemonize the process */ parent_pid = sr_daemonize(debug_mode, log_level, SR_PLUGIN_DAEMON_PID_FILE, &pidfile_fd); SR_LOG_DBG_MSG("Sysrepo plugin daemon initialization started."); /* init the event loop */ ctx.event_loop = ev_loop_new(EVFLAG_AUTO); /* init signal watchers */ ev_signal_init(&ctx.signal_watcher[0], sr_pd_signal_cb, SIGTERM); ev_signal_start(ctx.event_loop, &ctx.signal_watcher[0]); ev_signal_init(&ctx.signal_watcher[1], sr_pd_signal_cb, SIGINT); ev_signal_start(ctx.event_loop, &ctx.signal_watcher[1]); /* init timers */ ev_timer_init(&ctx.health_check_timer, sr_pd_health_check_timer_cb, SR_PLUGIN_HEALTH_CHECK_TIMEOUT, SR_PLUGIN_HEALTH_CHECK_TIMEOUT); ctx.health_check_timer.data = &ctx; ev_timer_init(&ctx.init_retry_timer, sr_pd_init_retry_timer_cb, SR_PLUGIN_INIT_RETRY_TIMEOUT, SR_PLUGIN_INIT_RETRY_TIMEOUT); ctx.init_retry_timer.data = &ctx; /* connect to sysrepo */ rc = sr_connect("sysrepo-plugind", connect_options, &ctx.connection); CHECK_RC_LOG_GOTO(rc, cleanup, "Unable to connect to sysrepod: %s", sr_strerror(rc)); /* start the session */ rc = sr_session_start(ctx.connection, SR_DS_STARTUP, SR_SESS_DEFAULT, &ctx.session); CHECK_RC_LOG_GOTO(rc, cleanup, "Unable to connect to sysrepo: %s", sr_strerror(rc)); /* tell the parent process that we are okay */ if (!debug_mode) { sr_daemonize_signal_success(parent_pid); } /* load the plugins */ rc = sr_pd_load_plugins(&ctx); SR_LOG_INF_MSG("Sysrepo plugin daemon initialized successfully."); /* start health check timer */ ev_timer_start(ctx.event_loop, &ctx.health_check_timer); /* run the event loop */ ev_run(ctx.event_loop, 0); ev_loop_destroy(ctx.event_loop); /* check whether the session is still valid & reconnect if needed */ sr_pd_session_check(&ctx); cleanup: sr_pd_cleanup_plugins(&ctx); if (NULL != ctx.session) { sr_session_stop(ctx.session); } if (NULL != ctx.connection) { sr_disconnect(ctx.connection); } SR_LOG_INF_MSG("Sysrepo plugin daemon terminated."); sr_logger_cleanup(); unlink(SR_PLUGIN_DAEMON_PID_FILE); if (-1 != pidfile_fd) { close(pidfile_fd); } exit((SR_ERR_OK == rc) ? EXIT_SUCCESS : EXIT_FAILURE); }