Move DB, back-log and ring-buffer config code into separate functions. No functional changes. Signed-off-by: Jeremy Sowden <jeremy@xxxxxxxxxx> --- util/db.c | 118 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 83 insertions(+), 35 deletions(-) diff --git a/util/db.c b/util/db.c index ce1273638ae0..a633257b5929 100644 --- a/util/db.c +++ b/util/db.c @@ -40,6 +40,7 @@ /* generic db layer */ +static int _configure_db(struct ulogd_pluginstance *upi); static int _interp_db_init(struct ulogd_pluginstance *upi); static int _interp_db_main(struct ulogd_pluginstance *upi); static int _interp_db_disabled(struct ulogd_pluginstance *upi); @@ -55,10 +56,12 @@ static unsigned int _calc_sql_stmt_size(const char *procedure, static void _bind_sql_stmt(struct ulogd_pluginstance *upi, char *stmt); +static int _configure_backlog(struct ulogd_pluginstance *upi); static int _add_to_backlog(struct ulogd_pluginstance *upi, const char *stmt, unsigned int len); static int _process_backlog(struct ulogd_pluginstance *upi); +static int _configure_ring(struct ulogd_pluginstance *upi); static int _add_to_ring(struct ulogd_pluginstance *upi, struct db_instance *di); static void *_process_ring(void *arg); @@ -66,53 +69,23 @@ int ulogd_db_configure(struct ulogd_pluginstance *upi, struct ulogd_pluginstance_stack *stack) { - struct db_instance *di = (struct db_instance *) upi->private; int ret; ulogd_log(ULOGD_NOTICE, "(re)configuring\n"); - /* First: Parse configuration file section for this instance */ - ret = config_parse_file(upi->id, upi->config_kset); + ret = _configure_db(upi); if (ret < 0) { - ulogd_log(ULOGD_ERROR, "error parsing config file\n"); return ret; } - /* Second: Open Database */ - ret = di->driver->open_db(upi); + ret = _configure_backlog(upi); if (ret < 0) { - ulogd_log(ULOGD_ERROR, "error in open_db\n"); return ret; } - /* Third: Determine required input keys for given table */ - ret = di->driver->get_columns(upi); - if (ret < 0) - ulogd_log(ULOGD_ERROR, "error in get_columns\n"); - - /* Close database, since ulogd core could just call configure - * but abort during input key resolving routines. configure - * doesn't have a destructor... */ - di->driver->close_db(upi); - - INIT_LLIST_HEAD(&di->backlog); - di->backlog_memusage = 0; - - di->ring.size = ringsize_ce(upi->config_kset).u.value; - di->backlog_memcap = backlog_memcap_ce(upi->config_kset).u.value; - - if (di->ring.size && di->backlog_memcap) { - ulogd_log(ULOGD_ERROR, "Ring buffer has precedence over backlog\n"); - di->backlog_memcap = 0; - } else if (di->backlog_memcap > 0) { - di->backlog_oneshot = backlog_oneshot_ce(upi->config_kset).u.value; - if (di->backlog_oneshot <= 2) { - ulogd_log(ULOGD_ERROR, - "backlog_oneshot_requests must be > 2 to hope" - " cleaning. Setting it to 3.\n"); - di->backlog_oneshot = 3; - } - di->backlog_full = 0; + ret = _configure_ring(upi); + if (ret < 0) { + return ret; } return 0; @@ -274,6 +247,40 @@ ulogd_db_stop(struct ulogd_pluginstance *upi) /******************************************************************************/ +static int +_configure_db(struct ulogd_pluginstance *upi) +{ + struct db_instance *di = (struct db_instance *) upi->private; + int ret; + + /* First: Parse configuration file section for this instance */ + ret = config_parse_file(upi->id, upi->config_kset); + if (ret < 0) { + ulogd_log(ULOGD_ERROR, "error parsing config file\n"); + return ret; + } + + /* Second: Open Database */ + ret = di->driver->open_db(upi); + if (ret < 0) { + ulogd_log(ULOGD_ERROR, "error in open_db\n"); + return ret; + } + + /* Third: Determine required input keys for given table */ + ret = di->driver->get_columns(upi); + if (ret < 0) + ulogd_log(ULOGD_ERROR, "error in get_columns\n"); + + /* Close database, since ulogd core could just call configure + * but abort during input key resolving routines. configure + * doesn't have a destructor... */ + di->driver->close_db(upi); + + return ret; + +} + static int _interp_db_init(struct ulogd_pluginstance *upi) { @@ -640,6 +647,37 @@ _bind_sql_stmt(struct ulogd_pluginstance *upi, char *start) /******************************************************************************/ +static int +_configure_backlog(struct ulogd_pluginstance *upi) +{ + struct db_instance *di = (struct db_instance *) &upi->private; + + INIT_LLIST_HEAD(&di->backlog); + + di->backlog_memusage = 0; + di->backlog_memcap = backlog_memcap_ce(upi->config_kset).u.value; + di->backlog_full = 0; + + if (di->backlog_memcap == 0) + return 0; + + if (ringsize_ce(upi->config_kset).u.value) { + ulogd_log(ULOGD_ERROR, + "Ring buffer has precedence over backlog\n"); + di->backlog_memcap = 0; + return 0; + } + + di->backlog_oneshot = backlog_oneshot_ce(upi->config_kset).u.value; + if (di->backlog_oneshot <= 2) { + ulogd_log(ULOGD_ERROR, + "backlog_oneshot_requests must be > 2 to be effective. Setting it to 3.\n"); + di->backlog_oneshot = 3; + } + + return 0; +} + static int _add_to_backlog(struct ulogd_pluginstance *upi, const char *stmt, unsigned int len) @@ -714,6 +752,16 @@ _process_backlog(struct ulogd_pluginstance *upi) /******************************************************************************/ +static int +_configure_ring(struct ulogd_pluginstance *upi) +{ + struct db_instance *di = (struct db_instance *) &upi->private; + + di->ring.size = ringsize_ce(upi->config_kset).u.value; + + return 0; +} + static int _add_to_ring(struct ulogd_pluginstance *upi, struct db_instance *di) { -- 2.35.1