Code

plugin: Fixed detection of plugins loaded twice.
[sysdb.git] / src / core / plugin.c
1 /*
2  * SysDB - src/core/plugin.c
3  * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #if HAVE_CONFIG_H
29 #       include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "sysdb.h"
33 #include "core/plugin.h"
34 #include "core/time.h"
35 #include "utils/error.h"
36 #include "utils/llist.h"
37 #include "utils/strbuf.h"
39 #include <assert.h>
41 #include <errno.h>
43 #include <stdarg.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <strings.h>
47 #include <unistd.h>
49 #include <ltdl.h>
51 #include <pthread.h>
53 /*
54  * private data types
55  */
57 struct sdb_plugin_info {
58         char *plugin_name;
59         char *filename;
61         /* public attributes */
62         char *name;
64         char *description;
65         char *copyright;
66         char *license;
68         int   version;
69         int   plugin_version;
70 };
71 #define SDB_PLUGIN_INFO_INIT { \
72         /* plugin_name */ NULL, /* filename */ NULL, \
73         /* name */ NULL, /* desc */ NULL, \
74         /* copyright */ NULL, /* license */ NULL, \
75         /* version */ -1, /* plugin_version */ -1 }
76 #define INFO_GET(i, attr) \
77         ((i)->attr ? (i)->attr : #attr" not set")
79 typedef struct {
80         sdb_object_t super;
81         sdb_plugin_ctx_t public;
83         sdb_plugin_info_t info;
84         lt_dlhandle handle;
86         /* The usage count differs from the object's ref count
87          * in that it provides higher level information about how
88          * the plugin is in use. */
89         size_t use_cnt;
90 } ctx_t;
91 #define CTX_INIT { SDB_OBJECT_INIT, \
92         SDB_PLUGIN_CTX_INIT, SDB_PLUGIN_INFO_INIT, NULL, 0 }
94 #define CTX(obj) ((ctx_t *)(obj))
96 typedef struct {
97         sdb_object_t super;
98         void *cb_callback;
99         sdb_object_t *cb_user_data;
100         ctx_t *cb_ctx;
101 } sdb_plugin_cb_t;
102 #define SDB_PLUGIN_CB_INIT { SDB_OBJECT_INIT, \
103         /* callback = */ NULL, /* user_data = */ NULL, \
104         SDB_PLUGIN_CTX_INIT }
106 typedef struct {
107         sdb_plugin_cb_t super;
108 #define ccb_callback super.cb_callback
109 #define ccb_user_data super.cb_user_data
110 #define ccb_ctx super.cb_ctx
111         sdb_time_t ccb_interval;
112         sdb_time_t ccb_next_update;
113 } sdb_plugin_collector_cb_t;
115 #define SDB_PLUGIN_CB(obj) ((sdb_plugin_cb_t *)(obj))
116 #define SDB_CONST_PLUGIN_CB(obj) ((const sdb_plugin_cb_t *)(obj))
117 #define SDB_PLUGIN_CCB(obj) ((sdb_plugin_collector_cb_t *)(obj))
118 #define SDB_CONST_PLUGIN_CCB(obj) ((const sdb_plugin_collector_cb_t *)(obj))
120 /*
121  * private variables
122  */
124 static sdb_plugin_ctx_t  plugin_default_ctx  = SDB_PLUGIN_CTX_INIT;
125 static sdb_plugin_info_t plugin_default_info = SDB_PLUGIN_INFO_INIT;
127 static pthread_key_t     plugin_ctx_key;
128 static _Bool             plugin_ctx_key_initialized = 0;
130 /* a list of the plugin contexts of all registered plugins */
131 static sdb_llist_t      *all_plugins = NULL;
133 static sdb_llist_t      *config_list = NULL;
134 static sdb_llist_t      *init_list = NULL;
135 static sdb_llist_t      *collector_list = NULL;
136 static sdb_llist_t      *cname_list = NULL;
137 static sdb_llist_t      *shutdown_list = NULL;
138 static sdb_llist_t      *log_list = NULL;
140 /*
141  * private helper functions
142  */
144 static void
145 plugin_info_clear(sdb_plugin_info_t *info)
147         sdb_plugin_info_t empty_info = SDB_PLUGIN_INFO_INIT;
148         if (! info)
149                 return;
151         if (info->plugin_name)
152                 free(info->plugin_name);
153         if (info->filename)
154                 free(info->filename);
156         if (info->name)
157                 free(info->name);
158         if (info->description)
159                 free(info->description);
160         if (info->copyright)
161                 free(info->copyright);
162         if (info->license)
163                 free(info->license);
165         *info = empty_info;
166 } /* plugin_info_clear */
168 static void
169 ctx_key_init(void)
171         if (plugin_ctx_key_initialized)
172                 return;
174         pthread_key_create(&plugin_ctx_key, /* destructor */ NULL);
175         plugin_ctx_key_initialized = 1;
176 } /* ctx_key_init */
178 static int
179 plugin_cmp_next_update(const sdb_object_t *a, const sdb_object_t *b)
181         const sdb_plugin_collector_cb_t *ccb1
182                 = (const sdb_plugin_collector_cb_t *)a;
183         const sdb_plugin_collector_cb_t *ccb2
184                 = (const sdb_plugin_collector_cb_t *)b;
186         assert(ccb1 && ccb2);
188         return (ccb1->ccb_next_update > ccb2->ccb_next_update)
189                 ? 1 : (ccb1->ccb_next_update < ccb2->ccb_next_update)
190                 ? -1 : 0;
191 } /* plugin_cmp_next_update */
193 static int
194 plugin_lookup_by_name(const sdb_object_t *obj, const void *id)
196         const sdb_plugin_cb_t *cb = SDB_CONST_PLUGIN_CB(obj);
197         const char *name = id;
199         assert(cb && id && cb->cb_ctx);
200         if (!strcasecmp(cb->cb_ctx->info.plugin_name, name))
201                 return 0;
202         return 1;
203 } /* plugin_lookup_by_name */
205 static void
206 plugin_unregister_by_name(const char *plugin_name)
208         sdb_object_t *obj;
209         size_t i;
211         struct {
212                 const char  *type;
213                 sdb_llist_t *list;
214         } all_lists[] = {
215                 { "config",    config_list },
216                 { "init",      init_list },
217                 { "collector", collector_list },
218                 { "cname",     cname_list },
219                 { "shutdown",  shutdown_list },
220                 { "log",       log_list },
221         };
223         for (i = 0; i < SDB_STATIC_ARRAY_LEN(all_lists); ++i) {
224                 const char  *type = all_lists[i].type;
225                 sdb_llist_t *list = all_lists[i].list;
227                 while (1) {
228                         sdb_plugin_cb_t *cb;
230                         cb = SDB_PLUGIN_CB(sdb_llist_remove(list,
231                                                 plugin_lookup_by_name, plugin_name));
232                         if (! cb)
233                                 break;
235                         sdb_log(SDB_LOG_INFO, "core: Unregistering "
236                                         "%s callback '%s' (module %s)", type, cb->super.name,
237                                         cb->cb_ctx->info.plugin_name);
238                         sdb_object_deref(SDB_OBJ(cb));
239                 }
240         }
242         obj = sdb_llist_search_by_name(all_plugins, plugin_name);
243         if (obj->ref_cnt <= 1)
244                 sdb_llist_remove_by_name(all_plugins, plugin_name);
245         /* else: other callbacks still reference it */
246         sdb_object_deref(obj);
247 } /* plugin_unregister_by_name */
249 /*
250  * private types
251  */
253 static int
254 ctx_init(sdb_object_t *obj, va_list __attribute__((unused)) ap)
256         ctx_t *ctx = CTX(obj);
258         assert(ctx);
260         ctx->public = plugin_default_ctx;
261         ctx->info = plugin_default_info;
262         ctx->handle = NULL;
263         ctx->use_cnt = 1;
264         return 0;
265 } /* ctx_init */
267 static void
268 ctx_destroy(sdb_object_t *obj)
270         ctx_t *ctx = CTX(obj);
272         if (ctx->handle) {
273                 const char *err;
275                 sdb_log(SDB_LOG_INFO, "core: Unloading module %s",
276                                 ctx->info.plugin_name);
278                 lt_dlerror();
279                 lt_dlclose(ctx->handle);
280                 if ((err = lt_dlerror()))
281                         sdb_log(SDB_LOG_WARNING, "core: Failed to unload module %s: %s",
282                                         ctx->info.plugin_name, err);
283         }
285         plugin_info_clear(&ctx->info);
286 } /* ctx_destroy */
288 static sdb_type_t ctx_type = {
289         sizeof(ctx_t),
291         ctx_init,
292         ctx_destroy
293 };
295 static ctx_t *
296 ctx_create(const char *name)
298         ctx_t *ctx;
300         ctx = CTX(sdb_object_create(name, ctx_type));
301         if (! ctx)
302                 return NULL;
304         if (! plugin_ctx_key_initialized)
305                 ctx_key_init();
306         sdb_object_ref(SDB_OBJ(ctx));
307         pthread_setspecific(plugin_ctx_key, ctx);
308         return ctx;
309 } /* ctx_create */
311 static ctx_t *
312 ctx_get(void)
314         if (! plugin_ctx_key_initialized)
315                 ctx_key_init();
316         return pthread_getspecific(plugin_ctx_key);
317 } /* ctx_get */
319 static ctx_t *
320 ctx_set(ctx_t *new)
322         ctx_t *old;
324         if (! plugin_ctx_key_initialized)
325                 ctx_key_init();
327         old = pthread_getspecific(plugin_ctx_key);
328         if (old)
329                 sdb_object_deref(SDB_OBJ(old));
330         if (new)
331                 sdb_object_ref(SDB_OBJ(new));
332         pthread_setspecific(plugin_ctx_key, new);
333         return old;
334 } /* ctx_set */
336 static int
337 plugin_cb_init(sdb_object_t *obj, va_list ap)
339         sdb_llist_t **list = va_arg(ap, sdb_llist_t **);
340         const char   *type = va_arg(ap, const char *);
341         void     *callback = va_arg(ap, void *);
342         sdb_object_t   *ud = va_arg(ap, sdb_object_t *);
344         assert(list);
345         assert(type);
346         assert(obj);
348         if (sdb_llist_search_by_name(*list, obj->name)) {
349                 sdb_log(SDB_LOG_WARNING, "core: %s callback '%s' "
350                                 "has already been registered. Ignoring newly "
351                                 "registered version.", type, obj->name);
352                 return -1;
353         }
355         SDB_PLUGIN_CB(obj)->cb_callback = callback;
356         SDB_PLUGIN_CB(obj)->cb_ctx      = ctx_get();
357         sdb_object_ref(SDB_OBJ(SDB_PLUGIN_CB(obj)->cb_ctx));
359         sdb_object_ref(ud);
360         SDB_PLUGIN_CB(obj)->cb_user_data = ud;
361         return 0;
362 } /* plugin_cb_init */
364 static void
365 plugin_cb_destroy(sdb_object_t *obj)
367         assert(obj);
368         sdb_object_deref(SDB_PLUGIN_CB(obj)->cb_user_data);
369         sdb_object_deref(SDB_OBJ(SDB_PLUGIN_CB(obj)->cb_ctx));
370 } /* plugin_cb_destroy */
372 static sdb_type_t sdb_plugin_cb_type = {
373         sizeof(sdb_plugin_cb_t),
375         plugin_cb_init,
376         plugin_cb_destroy
377 };
379 static sdb_type_t sdb_plugin_collector_cb_type = {
380         sizeof(sdb_plugin_collector_cb_t),
382         plugin_cb_init,
383         plugin_cb_destroy
384 };
386 static int
387 plugin_add_callback(sdb_llist_t **list, const char *type,
388                 const char *name, void *callback, sdb_object_t *user_data)
390         sdb_object_t *obj;
392         if ((! name) || (! callback))
393                 return -1;
395         assert(list);
397         if (! *list)
398                 *list = sdb_llist_create();
399         if (! *list)
400                 return -1;
402         obj = sdb_object_create(name, sdb_plugin_cb_type,
403                         list, type, callback, user_data);
404         if (! obj)
405                 return -1;
407         if (sdb_llist_append(*list, obj)) {
408                 sdb_object_deref(obj);
409                 return -1;
410         }
412         /* pass control to the list */
413         sdb_object_deref(obj);
415         sdb_log(SDB_LOG_INFO, "core: Registered %s callback '%s'.",
416                         type, name);
417         return 0;
418 } /* plugin_add_callback */
420 /*
421  * public API
422  */
424 int
425 sdb_plugin_load(const char *name, const sdb_plugin_ctx_t *plugin_ctx)
427         char  base_name[name ? strlen(name) + 1 : 1];
428         const char *name_ptr;
429         char *tmp;
431         char filename[1024];
432         ctx_t *ctx;
434         lt_dlhandle lh;
436         int (*mod_init)(sdb_plugin_info_t *);
437         int status;
439         if ((! name) || (! *name))
440                 return -1;
442         base_name[0] = '\0';
443         name_ptr = name;
445         while ((tmp = strstr(name_ptr, "::"))) {
446                 strncat(base_name, name_ptr, (size_t)(tmp - name_ptr));
447                 strcat(base_name, "/");
448                 name_ptr = tmp + strlen("::");
449         }
450         strcat(base_name, name_ptr);
452         ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
453         if (ctx) {
454                 /* plugin already loaded */
455                 ++ctx->use_cnt;
456                 return 0;
457         }
459         snprintf(filename, sizeof(filename), "%s/%s.so",
460                         PKGLIBDIR, base_name);
461         filename[sizeof(filename) - 1] = '\0';
463         if (access(filename, R_OK)) {
464                 char errbuf[1024];
465                 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s' (%s): %s",
466                                 name, filename, sdb_strerror(errno, errbuf, sizeof(errbuf)));
467                 return -1;
468         }
470         lt_dlinit();
471         lt_dlerror();
473         lh = lt_dlopen(filename);
474         if (! lh) {
475                 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': %s"
476                                 "The most common cause for this problem are missing "
477                                 "dependencies.\n", name, lt_dlerror());
478                 return -1;
479         }
481         if (ctx_get())
482                 sdb_log(SDB_LOG_WARNING, "core: Discarding old plugin context");
484         ctx = ctx_create(name);
485         if (! ctx) {
486                 sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin context");
487                 return -1;
488         }
490         ctx->info.plugin_name = strdup(name);
491         ctx->info.filename = strdup(filename);
492         ctx->handle = lh;
494         if (plugin_ctx)
495                 ctx->public = *plugin_ctx;
497         mod_init = (int (*)(sdb_plugin_info_t *))lt_dlsym(lh, "sdb_module_init");
498         if (! mod_init) {
499                 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': "
500                                 "could not find symbol 'sdb_module_init'", name);
501                 sdb_object_deref(SDB_OBJ(ctx));
502                 return -1;
503         }
505         status = mod_init(&ctx->info);
506         if (status) {
507                 sdb_log(SDB_LOG_ERR, "core: Failed to initialize "
508                                 "module '%s'", name);
509                 plugin_unregister_by_name(ctx->info.plugin_name);
510                 sdb_object_deref(SDB_OBJ(ctx));
511                 return -1;
512         }
514         /* compare minor version */
515         if ((ctx->info.version < 0)
516                         || ((int)(ctx->info.version / 100) != (int)(SDB_VERSION / 100)))
517                 sdb_log(SDB_LOG_WARNING, "core: WARNING: version of "
518                                 "plugin '%s' (%i.%i.%i) does not match our version "
519                                 "(%i.%i.%i); this might cause problems",
520                                 name, SDB_VERSION_DECODE(ctx->info.version),
521                                 SDB_VERSION_DECODE(SDB_VERSION));
523         if (! all_plugins) {
524                 if (! (all_plugins = sdb_llist_create())) {
525                         sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': "
526                                         "internal error while creating linked list", name);
527                         plugin_unregister_by_name(ctx->info.plugin_name);
528                         sdb_object_deref(SDB_OBJ(ctx));
529                         return -1;
530                 }
531         }
533         sdb_llist_append(all_plugins, SDB_OBJ(ctx));
535         sdb_log(SDB_LOG_INFO, "core: Successfully loaded "
536                         "plugin '%s' v%i (%s)\n\t%s\n\tLicense: %s",
537                         INFO_GET(&ctx->info, name), ctx->info.plugin_version,
538                         INFO_GET(&ctx->info, description),
539                         INFO_GET(&ctx->info, copyright),
540                         INFO_GET(&ctx->info, license));
542         /* any registered callbacks took ownership of the context */
543         sdb_object_deref(SDB_OBJ(ctx));
545         /* reset */
546         ctx_set(NULL);
547         return 0;
548 } /* sdb_plugin_load */
550 int
551 sdb_plugin_set_info(sdb_plugin_info_t *info, int type, ...)
553         va_list ap;
555         if (! info)
556                 return -1;
558         va_start(ap, type);
560         switch (type) {
561                 case SDB_PLUGIN_INFO_NAME:
562                         {
563                                 char *name = va_arg(ap, char *);
564                                 if (name) {
565                                         if (info->name)
566                                                 free(info->name);
567                                         info->name = strdup(name);
568                                 }
569                         }
570                         break;
571                 case SDB_PLUGIN_INFO_DESC:
572                         {
573                                 char *desc = va_arg(ap, char *);
574                                 if (desc) {
575                                         if (info->description)
576                                                 free(info->description);
577                                         info->description = strdup(desc);
578                                 }
579                         }
580                         break;
581                 case SDB_PLUGIN_INFO_COPYRIGHT:
582                         {
583                                 char *copyright = va_arg(ap, char *);
584                                 if (copyright)
585                                         info->copyright = strdup(copyright);
586                         }
587                         break;
588                 case SDB_PLUGIN_INFO_LICENSE:
589                         {
590                                 char *license = va_arg(ap, char *);
591                                 if (license) {
592                                         if (info->license)
593                                                 free(info->license);
594                                         info->license = strdup(license);
595                                 }
596                         }
597                         break;
598                 case SDB_PLUGIN_INFO_VERSION:
599                         {
600                                 int version = va_arg(ap, int);
601                                 info->version = version;
602                         }
603                         break;
604                 case SDB_PLUGIN_INFO_PLUGIN_VERSION:
605                         {
606                                 int version = va_arg(ap, int);
607                                 info->plugin_version = version;
608                         }
609                         break;
610                 default:
611                         va_end(ap);
612                         return -1;
613         }
615         va_end(ap);
616         return 0;
617 } /* sdb_plugin_set_info */
619 int
620 sdb_plugin_register_config(const char *name, sdb_plugin_config_cb callback)
622         return plugin_add_callback(&config_list, "init", name,
623                         (void *)callback, NULL);
624 } /* sdb_plugin_register_config */
626 int
627 sdb_plugin_register_init(const char *name, sdb_plugin_init_cb callback,
628                 sdb_object_t *user_data)
630         return plugin_add_callback(&init_list, "init", name,
631                         (void *)callback, user_data);
632 } /* sdb_plugin_register_init */
634 int
635 sdb_plugin_register_shutdown(const char *name, sdb_plugin_shutdown_cb callback,
636                 sdb_object_t *user_data)
638         return plugin_add_callback(&shutdown_list, "shutdown", name,
639                         (void *)callback, user_data);
640 } /* sdb_plugin_register_shutdown */
642 int
643 sdb_plugin_register_log(const char *name, sdb_plugin_log_cb callback,
644                 sdb_object_t *user_data)
646         return plugin_add_callback(&log_list, "log", name, (void *)callback,
647                         user_data);
648 } /* sdb_plugin_register_log */
650 int
651 sdb_plugin_register_cname(const char *name, sdb_plugin_cname_cb callback,
652                 sdb_object_t *user_data)
654         return plugin_add_callback(&cname_list, "cname", name, (void *)callback,
655                         user_data);
656 } /* sdb_plugin_register_cname */
658 int
659 sdb_plugin_register_collector(const char *name, sdb_plugin_collector_cb callback,
660                 const sdb_time_t *interval, sdb_object_t *user_data)
662         sdb_object_t *obj;
664         if ((! name) || (! callback))
665                 return -1;
667         if (! collector_list)
668                 collector_list = sdb_llist_create();
669         if (! collector_list)
670                 return -1;
672         obj = sdb_object_create(name, sdb_plugin_collector_cb_type,
673                         &collector_list, "collector", callback, user_data);
674         if (! obj)
675                 return -1;
677         if (interval)
678                 SDB_PLUGIN_CCB(obj)->ccb_interval = *interval;
679         else {
680                 sdb_time_t tmp = sdb_plugin_get_ctx().interval;
682                 if (tmp > 0)
683                         SDB_PLUGIN_CCB(obj)->ccb_interval = tmp;
684                 else
685                         SDB_PLUGIN_CCB(obj)->ccb_interval = 0;
686         }
688         if (! (SDB_PLUGIN_CCB(obj)->ccb_next_update = sdb_gettime())) {
689                 char errbuf[1024];
690                 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
691                                 "time: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
692                 sdb_object_deref(obj);
693                 return -1;
694         }
696         if (sdb_llist_insert_sorted(collector_list, obj,
697                                 plugin_cmp_next_update)) {
698                 sdb_object_deref(obj);
699                 return -1;
700         }
702         /* pass control to the list */
703         sdb_object_deref(obj);
705         sdb_log(SDB_LOG_INFO, "core: Registered collector callback '%s' "
706                         "(interval = %.3fs).", name,
707                         SDB_TIME_TO_DOUBLE(SDB_PLUGIN_CCB(obj)->ccb_interval));
708         return 0;
709 } /* sdb_plugin_register_collector */
711 sdb_plugin_ctx_t
712 sdb_plugin_get_ctx(void)
714         ctx_t *c;
716         c = ctx_get();
717         if (! c) {
718                 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid read access to plugin "
719                                 "context outside a plugin");
720                 return plugin_default_ctx;
721         }
722         return c->public;
723 } /* sdb_plugin_get_ctx */
725 int
726 sdb_plugin_set_ctx(sdb_plugin_ctx_t ctx, sdb_plugin_ctx_t *old)
728         ctx_t *c;
730         c = ctx_get();
731         if (! c) {
732                 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid write access to plugin "
733                                 "context outside a plugin");
734                 return -1;
735         }
737         if (old)
738                 *old = c->public;
739         c->public = ctx;
740         return 0;
741 } /* sdb_plugin_set_ctx */
743 int
744 sdb_plugin_configure(const char *name, oconfig_item_t *ci)
746         sdb_plugin_cb_t *plugin;
747         sdb_plugin_config_cb callback;
749         ctx_t *old_ctx;
751         int status;
753         if ((! name) || (! ci))
754                 return -1;
756         plugin = SDB_PLUGIN_CB(sdb_llist_search_by_name(config_list, name));
757         if (! plugin) {
758                 ctx_t *ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
759                 if (! ctx)
760                         sdb_log(SDB_LOG_ERR, "core: Plugin '%s' not loaded.", name);
761                 else
762                         sdb_log(SDB_LOG_ERR, "core: Plugin '%s' did not register "
763                                         "a config callback.", name);
764                 errno = ENOENT;
765                 return -1;
766         }
768         old_ctx = ctx_set(plugin->cb_ctx);
769         callback = (sdb_plugin_config_cb)plugin->cb_callback;
770         status = callback(ci);
771         ctx_set(old_ctx);
772         return status;
773 } /* sdb_plugin_configure */
775 int
776 sdb_plugin_init_all(void)
778         sdb_llist_iter_t *iter;
779         int ret = 0;
781         iter = sdb_llist_get_iter(init_list);
782         while (sdb_llist_iter_has_next(iter)) {
783                 sdb_plugin_cb_t *cb;
784                 sdb_plugin_init_cb callback;
785                 ctx_t *old_ctx;
787                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
788                 assert(obj);
789                 cb = SDB_PLUGIN_CB(obj);
791                 callback = (sdb_plugin_init_cb)cb->cb_callback;
793                 old_ctx = ctx_set(cb->cb_ctx);
794                 if (callback(cb->cb_user_data)) {
795                         sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin "
796                                         "'%s'. Unregistering all callbacks.", obj->name);
797                         plugin_unregister_by_name(cb->cb_ctx->info.plugin_name);
798                         ++ret;
799                 }
800                 ctx_set(old_ctx);
801         }
802         sdb_llist_iter_destroy(iter);
803         return ret;
804 } /* sdb_plugin_init_all */
806 int
807 sdb_plugin_collector_loop(sdb_plugin_loop_t *loop)
809         if (! collector_list) {
810                 sdb_log(SDB_LOG_WARNING, "core: No collectors registered. "
811                                 "Quiting main loop.");
812                 return -1;
813         }
815         if (! loop)
816                 return -1;
818         while (loop->do_loop) {
819                 sdb_plugin_collector_cb callback;
820                 ctx_t *old_ctx;
822                 sdb_time_t interval, now;
824                 sdb_object_t *obj = sdb_llist_shift(collector_list);
825                 if (! obj)
826                         return -1;
828                 callback = (sdb_plugin_collector_cb)SDB_PLUGIN_CCB(obj)->ccb_callback;
830                 if (! (now = sdb_gettime())) {
831                         char errbuf[1024];
832                         sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
833                                         "time in collector main loop: %s",
834                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
835                         now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
836                 }
838                 if (now < SDB_PLUGIN_CCB(obj)->ccb_next_update) {
839                         interval = SDB_PLUGIN_CCB(obj)->ccb_next_update - now;
841                         errno = 0;
842                         while (loop->do_loop && sdb_sleep(interval, &interval)) {
843                                 if (errno != EINTR) {
844                                         char errbuf[1024];
845                                         sdb_log(SDB_LOG_ERR, "core: Failed to sleep "
846                                                         "in collector main loop: %s",
847                                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
848                                         return -1;
849                                 }
850                                 errno = 0;
851                         }
853                         if (! loop->do_loop)
854                                 return 0;
855                 }
857                 old_ctx = ctx_set(SDB_PLUGIN_CCB(obj)->ccb_ctx);
858                 if (callback(SDB_PLUGIN_CCB(obj)->ccb_user_data)) {
859                         /* XXX */
860                 }
861                 ctx_set(old_ctx);
863                 interval = SDB_PLUGIN_CCB(obj)->ccb_interval;
864                 if (! interval)
865                         interval = loop->default_interval;
866                 if (! interval) {
867                         sdb_log(SDB_LOG_WARNING, "core: No interval configured "
868                                         "for plugin '%s'; skipping any further "
869                                         "iterations.", obj->name);
870                         sdb_object_deref(obj);
871                         continue;
872                 }
874                 SDB_PLUGIN_CCB(obj)->ccb_next_update += interval;
876                 if (! (now = sdb_gettime())) {
877                         char errbuf[1024];
878                         sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
879                                         "time in collector main loop: %s",
880                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
881                         now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
882                 }
884                 if (now > SDB_PLUGIN_CCB(obj)->ccb_next_update) {
885                         sdb_log(SDB_LOG_WARNING, "core: Plugin '%s' took too "
886                                         "long; skipping iterations to keep up.",
887                                         obj->name);
888                         SDB_PLUGIN_CCB(obj)->ccb_next_update = now;
889                 }
891                 if (sdb_llist_insert_sorted(collector_list, obj,
892                                         plugin_cmp_next_update)) {
893                         sdb_log(SDB_LOG_ERR, "core: Failed to re-insert "
894                                         "plugin '%s' into collector list. Unable to further "
895                                         "use the plugin.",
896                                         obj->name);
897                         sdb_object_deref(obj);
898                         return -1;
899                 }
901                 /* pass control back to the list */
902                 sdb_object_deref(obj);
903         }
904         return 0;
905 } /* sdb_plugin_read_loop */
907 char *
908 sdb_plugin_cname(char *hostname)
910         sdb_llist_iter_t *iter;
912         if (! hostname)
913                 return NULL;
915         if (! cname_list)
916                 return hostname;
918         iter = sdb_llist_get_iter(cname_list);
919         while (sdb_llist_iter_has_next(iter)) {
920                 sdb_plugin_cname_cb callback;
921                 char *cname;
923                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
924                 assert(obj);
926                 callback = (sdb_plugin_cname_cb)SDB_PLUGIN_CB(obj)->cb_callback;
927                 cname = callback(hostname, SDB_PLUGIN_CB(obj)->cb_user_data);
928                 if (cname) {
929                         free(hostname);
930                         hostname = cname;
931                 }
932                 /* else: don't change hostname */
933         }
934         sdb_llist_iter_destroy(iter);
935         return hostname;
936 } /* sdb_plugin_cname */
938 int
939 sdb_plugin_log(int prio, const char *msg)
941         sdb_llist_iter_t *iter;
942         int ret = -1;
944         if (! msg)
945                 return 0;
947         if (! log_list)
948                 return fprintf(stderr, "[%s] %s\n", SDB_LOG_PRIO_TO_STRING(prio), msg);
950         iter = sdb_llist_get_iter(log_list);
951         while (sdb_llist_iter_has_next(iter)) {
952                 sdb_plugin_log_cb callback;
953                 int tmp;
955                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
956                 assert(obj);
958                 callback = (sdb_plugin_log_cb)SDB_PLUGIN_CB(obj)->cb_callback;
959                 tmp = callback(prio, msg, SDB_PLUGIN_CB(obj)->cb_user_data);
960                 if (tmp > ret)
961                         ret = tmp;
962         }
963         sdb_llist_iter_destroy(iter);
964         return ret;
965 } /* sdb_plugin_log */
967 int
968 sdb_plugin_vlogf(int prio, const char *fmt, va_list ap)
970         sdb_strbuf_t *buf;
971         int ret;
973         if (! fmt)
974                 return 0;
976         buf = sdb_strbuf_create(64);
977         if (! buf) {
978                 ret = fprintf(stderr, "[%s] ", SDB_LOG_PRIO_TO_STRING(prio));
979                 ret += vfprintf(stderr, fmt, ap);
980                 return ret;
981         }
983         if (sdb_strbuf_vsprintf(buf, fmt, ap) < 0) {
984                 sdb_strbuf_destroy(buf);
985                 return -1;
986         }
988         ret = sdb_plugin_log(prio, sdb_strbuf_string(buf));
989         sdb_strbuf_destroy(buf);
990         return ret;
991 } /* sdb_plugin_vlogf */
993 int
994 sdb_plugin_logf(int prio, const char *fmt, ...)
996         va_list ap;
997         int ret;
999         if (! fmt)
1000                 return 0;
1002         va_start(ap, fmt);
1003         ret = sdb_plugin_vlogf(prio, fmt, ap);
1004         va_end(ap);
1005         return ret;
1006 } /* sdb_plugin_logf */
1008 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */