Code

plugin: Increment ref-count for a context stored in the TSD.
[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_remove_by_name(all_plugins, plugin_name);
243         if (obj->ref_cnt <= 1)
244                 sdb_log(SDB_LOG_INFO, "core: Unloading module %s", 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;
274                 lt_dlerror();
275                 lt_dlclose(ctx->handle);
276                 if ((err = lt_dlerror()))
277                         sdb_log(SDB_LOG_WARNING, "core: Failed to unload plugin %s: %s",
278                                         ctx->info.plugin_name, err);
279         }
281         plugin_info_clear(&ctx->info);
282 } /* ctx_destroy */
284 static sdb_type_t ctx_type = {
285         sizeof(ctx_t),
287         ctx_init,
288         ctx_destroy
289 };
291 static ctx_t *
292 ctx_create(const char *name)
294         ctx_t *ctx;
296         ctx = CTX(sdb_object_create(name, ctx_type));
297         if (! ctx)
298                 return NULL;
300         if (! plugin_ctx_key_initialized)
301                 ctx_key_init();
302         sdb_object_ref(SDB_OBJ(ctx));
303         pthread_setspecific(plugin_ctx_key, ctx);
304         return ctx;
305 } /* ctx_create */
307 static ctx_t *
308 ctx_get(void)
310         if (! plugin_ctx_key_initialized)
311                 ctx_key_init();
312         return pthread_getspecific(plugin_ctx_key);
313 } /* ctx_get */
315 static ctx_t *
316 ctx_set(ctx_t *new)
318         ctx_t *old;
320         if (! plugin_ctx_key_initialized)
321                 ctx_key_init();
323         old = pthread_getspecific(plugin_ctx_key);
324         if (old)
325                 sdb_object_deref(SDB_OBJ(old));
326         if (new)
327                 sdb_object_ref(SDB_OBJ(new));
328         pthread_setspecific(plugin_ctx_key, new);
329         return old;
330 } /* ctx_set */
332 static int
333 plugin_cb_init(sdb_object_t *obj, va_list ap)
335         sdb_llist_t **list = va_arg(ap, sdb_llist_t **);
336         const char   *type = va_arg(ap, const char *);
337         void     *callback = va_arg(ap, void *);
338         sdb_object_t   *ud = va_arg(ap, sdb_object_t *);
340         assert(list);
341         assert(type);
342         assert(obj);
344         if (sdb_llist_search_by_name(*list, obj->name)) {
345                 sdb_log(SDB_LOG_WARNING, "core: %s callback '%s' "
346                                 "has already been registered. Ignoring newly "
347                                 "registered version.", type, obj->name);
348                 return -1;
349         }
351         SDB_PLUGIN_CB(obj)->cb_callback = callback;
352         SDB_PLUGIN_CB(obj)->cb_ctx      = ctx_get();
353         sdb_object_ref(SDB_OBJ(SDB_PLUGIN_CB(obj)->cb_ctx));
355         sdb_object_ref(ud);
356         SDB_PLUGIN_CB(obj)->cb_user_data = ud;
357         return 0;
358 } /* plugin_cb_init */
360 static void
361 plugin_cb_destroy(sdb_object_t *obj)
363         assert(obj);
364         sdb_object_deref(SDB_PLUGIN_CB(obj)->cb_user_data);
365         sdb_object_deref(SDB_OBJ(SDB_PLUGIN_CB(obj)->cb_ctx));
366 } /* plugin_cb_destroy */
368 static sdb_type_t sdb_plugin_cb_type = {
369         sizeof(sdb_plugin_cb_t),
371         plugin_cb_init,
372         plugin_cb_destroy
373 };
375 static sdb_type_t sdb_plugin_collector_cb_type = {
376         sizeof(sdb_plugin_collector_cb_t),
378         plugin_cb_init,
379         plugin_cb_destroy
380 };
382 static int
383 plugin_add_callback(sdb_llist_t **list, const char *type,
384                 const char *name, void *callback, sdb_object_t *user_data)
386         sdb_object_t *obj;
388         if ((! name) || (! callback))
389                 return -1;
391         assert(list);
393         if (! *list)
394                 *list = sdb_llist_create();
395         if (! *list)
396                 return -1;
398         obj = sdb_object_create(name, sdb_plugin_cb_type,
399                         list, type, callback, user_data);
400         if (! obj)
401                 return -1;
403         if (sdb_llist_append(*list, obj)) {
404                 sdb_object_deref(obj);
405                 return -1;
406         }
408         /* pass control to the list */
409         sdb_object_deref(obj);
411         sdb_log(SDB_LOG_INFO, "core: Registered %s callback '%s'.",
412                         type, name);
413         return 0;
414 } /* plugin_add_callback */
416 /*
417  * public API
418  */
420 int
421 sdb_plugin_load(const char *name, const sdb_plugin_ctx_t *plugin_ctx)
423         char  base_name[name ? strlen(name) + 1 : 1];
424         const char *name_ptr;
425         char *tmp;
427         char filename[1024];
428         ctx_t *ctx;
430         lt_dlhandle lh;
432         int (*mod_init)(sdb_plugin_info_t *);
433         int status;
435         if ((! name) || (! *name))
436                 return -1;
438         base_name[0] = '\0';
439         name_ptr = name;
441         while ((tmp = strstr(name_ptr, "::"))) {
442                 strncat(base_name, name_ptr, (size_t)(tmp - name_ptr));
443                 strcat(base_name, "/");
444                 name_ptr = tmp + strlen("::");
445         }
446         strcat(base_name, name_ptr);
448         ctx = CTX(sdb_llist_search_by_name(all_plugins, base_name));
449         if (ctx) {
450                 /* plugin already loaded */
451                 ++ctx->use_cnt;
452                 return 0;
453         }
455         snprintf(filename, sizeof(filename), "%s/%s.so",
456                         PKGLIBDIR, base_name);
457         filename[sizeof(filename) - 1] = '\0';
459         if (access(filename, R_OK)) {
460                 char errbuf[1024];
461                 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s' (%s): %s",
462                                 name, filename, sdb_strerror(errno, errbuf, sizeof(errbuf)));
463                 return -1;
464         }
466         lt_dlinit();
467         lt_dlerror();
469         lh = lt_dlopen(filename);
470         if (! lh) {
471                 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': %s"
472                                 "The most common cause for this problem are missing "
473                                 "dependencies.\n", name, lt_dlerror());
474                 return -1;
475         }
477         if (ctx_get())
478                 sdb_log(SDB_LOG_WARNING, "core: Discarding old plugin context");
480         ctx = ctx_create(name);
481         if (! ctx) {
482                 sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin context");
483                 return -1;
484         }
486         ctx->info.plugin_name = strdup(name);
487         ctx->info.filename = strdup(filename);
488         ctx->handle = lh;
490         if (plugin_ctx)
491                 ctx->public = *plugin_ctx;
493         mod_init = (int (*)(sdb_plugin_info_t *))lt_dlsym(lh, "sdb_module_init");
494         if (! mod_init) {
495                 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': "
496                                 "could not find symbol 'sdb_module_init'", name);
497                 sdb_object_deref(SDB_OBJ(ctx));
498                 return -1;
499         }
501         status = mod_init(&ctx->info);
502         if (status) {
503                 sdb_log(SDB_LOG_ERR, "core: Failed to initialize "
504                                 "module '%s'", name);
505                 plugin_unregister_by_name(ctx->info.plugin_name);
506                 sdb_object_deref(SDB_OBJ(ctx));
507                 return -1;
508         }
510         /* compare minor version */
511         if ((ctx->info.version < 0)
512                         || ((int)(ctx->info.version / 100) != (int)(SDB_VERSION / 100)))
513                 sdb_log(SDB_LOG_WARNING, "core: WARNING: version of "
514                                 "plugin '%s' (%i.%i.%i) does not match our version "
515                                 "(%i.%i.%i); this might cause problems",
516                                 name, SDB_VERSION_DECODE(ctx->info.version),
517                                 SDB_VERSION_DECODE(SDB_VERSION));
519         if (! all_plugins) {
520                 if (! (all_plugins = sdb_llist_create())) {
521                         sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': "
522                                         "internal error while creating linked list", name);
523                         plugin_unregister_by_name(ctx->info.plugin_name);
524                         sdb_object_deref(SDB_OBJ(ctx));
525                         return -1;
526                 }
527         }
529         sdb_llist_append(all_plugins, SDB_OBJ(ctx));
531         sdb_log(SDB_LOG_INFO, "core: Successfully loaded "
532                         "plugin '%s' v%i (%s)\n\t%s\n\tLicense: %s",
533                         INFO_GET(&ctx->info, name), ctx->info.plugin_version,
534                         INFO_GET(&ctx->info, description),
535                         INFO_GET(&ctx->info, copyright),
536                         INFO_GET(&ctx->info, license));
538         /* any registered callbacks took ownership of the context */
539         sdb_object_deref(SDB_OBJ(ctx));
541         /* reset */
542         ctx_set(NULL);
543         return 0;
544 } /* sdb_plugin_load */
546 int
547 sdb_plugin_set_info(sdb_plugin_info_t *info, int type, ...)
549         va_list ap;
551         if (! info)
552                 return -1;
554         va_start(ap, type);
556         switch (type) {
557                 case SDB_PLUGIN_INFO_NAME:
558                         {
559                                 char *name = va_arg(ap, char *);
560                                 if (name) {
561                                         if (info->name)
562                                                 free(info->name);
563                                         info->name = strdup(name);
564                                 }
565                         }
566                         break;
567                 case SDB_PLUGIN_INFO_DESC:
568                         {
569                                 char *desc = va_arg(ap, char *);
570                                 if (desc) {
571                                         if (info->description)
572                                                 free(info->description);
573                                         info->description = strdup(desc);
574                                 }
575                         }
576                         break;
577                 case SDB_PLUGIN_INFO_COPYRIGHT:
578                         {
579                                 char *copyright = va_arg(ap, char *);
580                                 if (copyright)
581                                         info->copyright = strdup(copyright);
582                         }
583                         break;
584                 case SDB_PLUGIN_INFO_LICENSE:
585                         {
586                                 char *license = va_arg(ap, char *);
587                                 if (license) {
588                                         if (info->license)
589                                                 free(info->license);
590                                         info->license = strdup(license);
591                                 }
592                         }
593                         break;
594                 case SDB_PLUGIN_INFO_VERSION:
595                         {
596                                 int version = va_arg(ap, int);
597                                 info->version = version;
598                         }
599                         break;
600                 case SDB_PLUGIN_INFO_PLUGIN_VERSION:
601                         {
602                                 int version = va_arg(ap, int);
603                                 info->plugin_version = version;
604                         }
605                         break;
606                 default:
607                         va_end(ap);
608                         return -1;
609         }
611         va_end(ap);
612         return 0;
613 } /* sdb_plugin_set_info */
615 int
616 sdb_plugin_register_config(const char *name, sdb_plugin_config_cb callback)
618         return plugin_add_callback(&config_list, "init", name,
619                         (void *)callback, NULL);
620 } /* sdb_plugin_register_config */
622 int
623 sdb_plugin_register_init(const char *name, sdb_plugin_init_cb callback,
624                 sdb_object_t *user_data)
626         return plugin_add_callback(&init_list, "init", name,
627                         (void *)callback, user_data);
628 } /* sdb_plugin_register_init */
630 int
631 sdb_plugin_register_shutdown(const char *name, sdb_plugin_shutdown_cb callback,
632                 sdb_object_t *user_data)
634         return plugin_add_callback(&shutdown_list, "shutdown", name,
635                         (void *)callback, user_data);
636 } /* sdb_plugin_register_shutdown */
638 int
639 sdb_plugin_register_log(const char *name, sdb_plugin_log_cb callback,
640                 sdb_object_t *user_data)
642         return plugin_add_callback(&log_list, "log", name, (void *)callback,
643                         user_data);
644 } /* sdb_plugin_register_log */
646 int
647 sdb_plugin_register_cname(const char *name, sdb_plugin_cname_cb callback,
648                 sdb_object_t *user_data)
650         return plugin_add_callback(&cname_list, "cname", name, (void *)callback,
651                         user_data);
652 } /* sdb_plugin_register_cname */
654 int
655 sdb_plugin_register_collector(const char *name, sdb_plugin_collector_cb callback,
656                 const sdb_time_t *interval, sdb_object_t *user_data)
658         sdb_object_t *obj;
660         if ((! name) || (! callback))
661                 return -1;
663         if (! collector_list)
664                 collector_list = sdb_llist_create();
665         if (! collector_list)
666                 return -1;
668         obj = sdb_object_create(name, sdb_plugin_collector_cb_type,
669                         &collector_list, "collector", callback, user_data);
670         if (! obj)
671                 return -1;
673         if (interval)
674                 SDB_PLUGIN_CCB(obj)->ccb_interval = *interval;
675         else {
676                 sdb_time_t tmp = sdb_plugin_get_ctx().interval;
678                 if (tmp > 0)
679                         SDB_PLUGIN_CCB(obj)->ccb_interval = tmp;
680                 else
681                         SDB_PLUGIN_CCB(obj)->ccb_interval = 0;
682         }
684         if (! (SDB_PLUGIN_CCB(obj)->ccb_next_update = sdb_gettime())) {
685                 char errbuf[1024];
686                 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
687                                 "time: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
688                 sdb_object_deref(obj);
689                 return -1;
690         }
692         if (sdb_llist_insert_sorted(collector_list, obj,
693                                 plugin_cmp_next_update)) {
694                 sdb_object_deref(obj);
695                 return -1;
696         }
698         /* pass control to the list */
699         sdb_object_deref(obj);
701         sdb_log(SDB_LOG_INFO, "core: Registered collector callback '%s' "
702                         "(interval = %.3fs).", name,
703                         SDB_TIME_TO_DOUBLE(SDB_PLUGIN_CCB(obj)->ccb_interval));
704         return 0;
705 } /* sdb_plugin_register_collector */
707 sdb_plugin_ctx_t
708 sdb_plugin_get_ctx(void)
710         ctx_t *c;
712         c = ctx_get();
713         if (! c) {
714                 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid read access to plugin "
715                                 "context outside a plugin");
716                 return plugin_default_ctx;
717         }
718         return c->public;
719 } /* sdb_plugin_get_ctx */
721 int
722 sdb_plugin_set_ctx(sdb_plugin_ctx_t ctx, sdb_plugin_ctx_t *old)
724         ctx_t *c;
726         c = ctx_get();
727         if (! c) {
728                 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid write access to plugin "
729                                 "context outside a plugin");
730                 return -1;
731         }
733         if (old)
734                 *old = c->public;
735         c->public = ctx;
736         return 0;
737 } /* sdb_plugin_set_ctx */
739 int
740 sdb_plugin_configure(const char *name, oconfig_item_t *ci)
742         sdb_plugin_cb_t *plugin;
743         sdb_plugin_config_cb callback;
745         ctx_t *old_ctx;
747         int status;
749         if ((! name) || (! ci))
750                 return -1;
752         plugin = SDB_PLUGIN_CB(sdb_llist_search_by_name(config_list, name));
753         if (! plugin) {
754                 ctx_t *ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
755                 if (! ctx)
756                         sdb_log(SDB_LOG_ERR, "core: Plugin '%s' not loaded.", name);
757                 else
758                         sdb_log(SDB_LOG_ERR, "core: Plugin '%s' did not register "
759                                         "a config callback.", name);
760                 errno = ENOENT;
761                 return -1;
762         }
764         old_ctx = ctx_set(plugin->cb_ctx);
765         callback = (sdb_plugin_config_cb)plugin->cb_callback;
766         status = callback(ci);
767         ctx_set(old_ctx);
768         return status;
769 } /* sdb_plugin_configure */
771 int
772 sdb_plugin_init_all(void)
774         sdb_llist_iter_t *iter;
775         int ret = 0;
777         iter = sdb_llist_get_iter(init_list);
778         while (sdb_llist_iter_has_next(iter)) {
779                 sdb_plugin_cb_t *cb;
780                 sdb_plugin_init_cb callback;
781                 ctx_t *old_ctx;
783                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
784                 assert(obj);
785                 cb = SDB_PLUGIN_CB(obj);
787                 callback = (sdb_plugin_init_cb)cb->cb_callback;
789                 old_ctx = ctx_set(cb->cb_ctx);
790                 if (callback(cb->cb_user_data)) {
791                         sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin "
792                                         "'%s'. Unregistering all callbacks.", obj->name);
793                         plugin_unregister_by_name(cb->cb_ctx->info.plugin_name);
794                         ++ret;
795                 }
796                 ctx_set(old_ctx);
797         }
798         sdb_llist_iter_destroy(iter);
799         return ret;
800 } /* sdb_plugin_init_all */
802 int
803 sdb_plugin_collector_loop(sdb_plugin_loop_t *loop)
805         if (! collector_list) {
806                 sdb_log(SDB_LOG_WARNING, "core: No collectors registered. "
807                                 "Quiting main loop.");
808                 return -1;
809         }
811         if (! loop)
812                 return -1;
814         while (loop->do_loop) {
815                 sdb_plugin_collector_cb callback;
816                 ctx_t *old_ctx;
818                 sdb_time_t interval, now;
820                 sdb_object_t *obj = sdb_llist_shift(collector_list);
821                 if (! obj)
822                         return -1;
824                 callback = (sdb_plugin_collector_cb)SDB_PLUGIN_CCB(obj)->ccb_callback;
826                 if (! (now = sdb_gettime())) {
827                         char errbuf[1024];
828                         sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
829                                         "time in collector main loop: %s",
830                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
831                         now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
832                 }
834                 if (now < SDB_PLUGIN_CCB(obj)->ccb_next_update) {
835                         interval = SDB_PLUGIN_CCB(obj)->ccb_next_update - now;
837                         errno = 0;
838                         while (loop->do_loop && sdb_sleep(interval, &interval)) {
839                                 if (errno != EINTR) {
840                                         char errbuf[1024];
841                                         sdb_log(SDB_LOG_ERR, "core: Failed to sleep "
842                                                         "in collector main loop: %s",
843                                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
844                                         return -1;
845                                 }
846                                 errno = 0;
847                         }
849                         if (! loop->do_loop)
850                                 return 0;
851                 }
853                 old_ctx = ctx_set(SDB_PLUGIN_CCB(obj)->ccb_ctx);
854                 if (callback(SDB_PLUGIN_CCB(obj)->ccb_user_data)) {
855                         /* XXX */
856                 }
857                 ctx_set(old_ctx);
859                 interval = SDB_PLUGIN_CCB(obj)->ccb_interval;
860                 if (! interval)
861                         interval = loop->default_interval;
862                 if (! interval) {
863                         sdb_log(SDB_LOG_WARNING, "core: No interval configured "
864                                         "for plugin '%s'; skipping any further "
865                                         "iterations.", obj->name);
866                         sdb_object_deref(obj);
867                         continue;
868                 }
870                 SDB_PLUGIN_CCB(obj)->ccb_next_update += interval;
872                 if (! (now = sdb_gettime())) {
873                         char errbuf[1024];
874                         sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
875                                         "time in collector main loop: %s",
876                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
877                         now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
878                 }
880                 if (now > SDB_PLUGIN_CCB(obj)->ccb_next_update) {
881                         sdb_log(SDB_LOG_WARNING, "core: Plugin '%s' took too "
882                                         "long; skipping iterations to keep up.",
883                                         obj->name);
884                         SDB_PLUGIN_CCB(obj)->ccb_next_update = now;
885                 }
887                 if (sdb_llist_insert_sorted(collector_list, obj,
888                                         plugin_cmp_next_update)) {
889                         sdb_log(SDB_LOG_ERR, "core: Failed to re-insert "
890                                         "plugin '%s' into collector list. Unable to further "
891                                         "use the plugin.",
892                                         obj->name);
893                         sdb_object_deref(obj);
894                         return -1;
895                 }
897                 /* pass control back to the list */
898                 sdb_object_deref(obj);
899         }
900         return 0;
901 } /* sdb_plugin_read_loop */
903 char *
904 sdb_plugin_cname(char *hostname)
906         sdb_llist_iter_t *iter;
908         if (! hostname)
909                 return NULL;
911         if (! cname_list)
912                 return hostname;
914         iter = sdb_llist_get_iter(cname_list);
915         while (sdb_llist_iter_has_next(iter)) {
916                 sdb_plugin_cname_cb callback;
917                 char *cname;
919                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
920                 assert(obj);
922                 callback = (sdb_plugin_cname_cb)SDB_PLUGIN_CB(obj)->cb_callback;
923                 cname = callback(hostname, SDB_PLUGIN_CB(obj)->cb_user_data);
924                 if (cname) {
925                         free(hostname);
926                         hostname = cname;
927                 }
928                 /* else: don't change hostname */
929         }
930         sdb_llist_iter_destroy(iter);
931         return hostname;
932 } /* sdb_plugin_cname */
934 int
935 sdb_plugin_log(int prio, const char *msg)
937         sdb_llist_iter_t *iter;
938         int ret = -1;
940         if (! msg)
941                 return 0;
943         if (! log_list)
944                 return fprintf(stderr, "[%s] %s\n", SDB_LOG_PRIO_TO_STRING(prio), msg);
946         iter = sdb_llist_get_iter(log_list);
947         while (sdb_llist_iter_has_next(iter)) {
948                 sdb_plugin_log_cb callback;
949                 int tmp;
951                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
952                 assert(obj);
954                 callback = (sdb_plugin_log_cb)SDB_PLUGIN_CB(obj)->cb_callback;
955                 tmp = callback(prio, msg, SDB_PLUGIN_CB(obj)->cb_user_data);
956                 if (tmp > ret)
957                         ret = tmp;
958         }
959         sdb_llist_iter_destroy(iter);
960         return ret;
961 } /* sdb_plugin_log */
963 int
964 sdb_plugin_vlogf(int prio, const char *fmt, va_list ap)
966         sdb_strbuf_t *buf;
967         int ret;
969         if (! fmt)
970                 return 0;
972         buf = sdb_strbuf_create(64);
973         if (! buf) {
974                 ret = fprintf(stderr, "[%s] ", SDB_LOG_PRIO_TO_STRING(prio));
975                 ret += vfprintf(stderr, fmt, ap);
976                 return ret;
977         }
979         if (sdb_strbuf_vsprintf(buf, fmt, ap) < 0) {
980                 sdb_strbuf_destroy(buf);
981                 return -1;
982         }
984         ret = sdb_plugin_log(prio, sdb_strbuf_string(buf));
985         sdb_strbuf_destroy(buf);
986         return ret;
987 } /* sdb_plugin_vlogf */
989 int
990 sdb_plugin_logf(int prio, const char *fmt, ...)
992         va_list ap;
993         int ret;
995         if (! fmt)
996                 return 0;
998         va_start(ap, fmt);
999         ret = sdb_plugin_vlogf(prio, fmt, ap);
1000         va_end(ap);
1001         return ret;
1002 } /* sdb_plugin_logf */
1004 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */