Code

object: Removed support for cloning typed objects.
[sysdb.git] / src / core / plugin.c
index f771d70936e5eda9fbbc2f2fbfa7c735b3e0605a..8ca0fd74472a23b7b7f0ff0daa953ff1329ce0cf 100644 (file)
@@ -30,6 +30,7 @@
 #include "core/error.h"
 #include "core/time.h"
 #include "utils/llist.h"
+#include "utils/strbuf.h"
 
 #include <assert.h>
 
@@ -97,6 +98,7 @@ static _Bool            plugin_ctx_key_initialized = 0;
 static sdb_llist_t      *config_list = NULL;
 static sdb_llist_t      *init_list = NULL;
 static sdb_llist_t      *collector_list = NULL;
+static sdb_llist_t      *cname_list = NULL;
 static sdb_llist_t      *shutdown_list = NULL;
 static sdb_llist_t      *log_list = NULL;
 
@@ -154,27 +156,6 @@ sdb_plugin_cmp_next_update(const sdb_object_t *a, const sdb_object_t *b)
                ? -1 : 0;
 } /* sdb_plugin_cmp_next_update */
 
-static sdb_plugin_cb_t *
-sdb_plugin_find_by_name(sdb_llist_t *list, const char *name)
-{
-       sdb_object_t tmp = SDB_OBJECT_INIT;
-
-       sdb_object_t *obj;
-       assert(name);
-
-       if (! list)
-               return NULL;
-
-       tmp.name = strdup(name);
-       if (! tmp.name)
-               return NULL;
-       obj = sdb_llist_search(list, &tmp, sdb_object_cmp_by_name);
-       free(tmp.name);
-       if (! obj)
-               return NULL;
-       return SDB_PLUGIN_CB(obj);
-} /* sdb_plugin_find_by_name */
-
 /*
  * private types
  */
@@ -191,7 +172,7 @@ sdb_plugin_cb_init(sdb_object_t *obj, va_list ap)
        assert(type);
        assert(obj);
 
-       if (sdb_plugin_find_by_name(*list, obj->name)) {
+       if (sdb_llist_search_by_name(*list, obj->name)) {
                sdb_log(SDB_LOG_WARNING, "plugin: %s callback '%s' "
                                "has already been registered. Ignoring newly "
                                "registered version.", type, obj->name);
@@ -217,16 +198,14 @@ static sdb_type_t sdb_plugin_cb_type = {
        sizeof(sdb_plugin_cb_t),
 
        sdb_plugin_cb_init,
-       sdb_plugin_cb_destroy,
-       /* clone = */ NULL
+       sdb_plugin_cb_destroy
 };
 
 static sdb_type_t sdb_plugin_collector_cb_type = {
        sizeof(sdb_plugin_collector_cb_t),
 
        sdb_plugin_cb_init,
-       sdb_plugin_cb_destroy,
-       /* clone = */ NULL
+       sdb_plugin_cb_destroy
 };
 
 static int
@@ -435,6 +414,14 @@ sdb_plugin_register_log(const char *name, sdb_plugin_log_cb callback,
                        user_data);
 } /* sdb_plugin_register_log */
 
+int
+sdb_plugin_register_cname(const char *name, sdb_plugin_cname_cb callback,
+               sdb_object_t *user_data)
+{
+       return sdb_plugin_add_callback(&cname_list, "cname", name, callback,
+                       user_data);
+} /* sdb_plugin_register_cname */
+
 int
 sdb_plugin_register_collector(const char *name, sdb_plugin_collector_cb callback,
                const sdb_time_t *interval, sdb_object_t *user_data)
@@ -537,7 +524,7 @@ sdb_plugin_configure(const char *name, oconfig_item_t *ci)
        if ((! name) || (! ci))
                return -1;
 
-       plugin = sdb_plugin_find_by_name(config_list, name);
+       plugin = SDB_PLUGIN_CB(sdb_llist_search_by_name(config_list, name));
        if (! plugin) {
                /* XXX: check if any such plugin has been loaded */
                sdb_log(SDB_LOG_ERR, "plugin: Plugin '%s' did not register "
@@ -676,12 +663,46 @@ sdb_plugin_collector_loop(sdb_plugin_loop_t *loop)
        return 0;
 } /* sdb_plugin_read_loop */
 
+char *
+sdb_plugin_cname(char *hostname)
+{
+       sdb_llist_iter_t *iter;
+
+       if (! hostname)
+               return NULL;
+
+       if (! cname_list)
+               return hostname;
+
+       iter = sdb_llist_get_iter(cname_list);
+       while (sdb_llist_iter_has_next(iter)) {
+               sdb_plugin_cname_cb callback;
+               char *cname;
+
+               sdb_object_t *obj = sdb_llist_iter_get_next(iter);
+               assert(obj);
+
+               callback = SDB_PLUGIN_CB(obj)->cb_callback;
+               cname = callback(hostname, SDB_PLUGIN_CB(obj)->cb_user_data);
+               if (cname) {
+                       free(hostname);
+                       hostname = cname;
+               }
+               /* else: don't change hostname */
+       }
+       sdb_llist_iter_destroy(iter);
+       return hostname;
+} /* sdb_plugin_cname */
+
 int
 sdb_plugin_log(int prio, const char *msg)
 {
        sdb_llist_iter_t *iter;
        int ret = -1;
 
+       if (! msg)
+               return 0;
+
        if (! log_list)
                return fprintf(stderr, "[%s] %s\n", SDB_LOG_PRIO_TO_STRING(prio), msg);
 
@@ -702,5 +723,46 @@ sdb_plugin_log(int prio, const char *msg)
        return ret;
 } /* sdb_plugin_log */
 
+int
+sdb_plugin_vlogf(int prio, const char *fmt, va_list ap)
+{
+       sdb_strbuf_t *buf;
+       int ret;
+
+       if (! fmt)
+               return 0;
+
+       buf = sdb_strbuf_create(64);
+       if (! buf) {
+               ret = fprintf(stderr, "[%s] ", SDB_LOG_PRIO_TO_STRING(prio));
+               ret += vfprintf(stderr, fmt, ap);
+               return ret;
+       }
+
+       if (sdb_strbuf_vsprintf(buf, fmt, ap) < 0) {
+               sdb_strbuf_destroy(buf);
+               return -1;
+       }
+
+       ret = sdb_plugin_log(prio, sdb_strbuf_string(buf));
+       sdb_strbuf_destroy(buf);
+       return ret;
+} /* sdb_plugin_vlogf */
+
+int
+sdb_plugin_logf(int prio, const char *fmt, ...)
+{
+       va_list ap;
+       int ret;
+
+       if (! fmt)
+               return 0;
+
+       va_start(ap, fmt);
+       ret = sdb_plugin_vlogf(prio, fmt, ap);
+       va_end(ap);
+       return ret;
+} /* sdb_plugin_logf */
+
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */