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)
146 {
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)
170 {
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)
180 {
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)
195 {
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)
207 {
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)
255 {
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)
269 {
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)
297 {
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)
313 {
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)
321 {
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)
338 {
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)
366 {
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 module_init(const char *name, lt_dlhandle lh, sdb_plugin_info_t *info)
388 {
389 int (*mod_init)(sdb_plugin_info_t *);
390 int status;
392 mod_init = (int (*)(sdb_plugin_info_t *))lt_dlsym(lh, "sdb_module_init");
393 if (! mod_init) {
394 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': "
395 "could not find symbol 'sdb_module_init'", name);
396 return -1;
397 }
399 status = mod_init(info);
400 if (status) {
401 sdb_log(SDB_LOG_ERR, "core: Failed to initialize "
402 "module '%s'", name);
403 plugin_unregister_by_name(name);
404 return -1;
405 }
406 return 0;
407 } /* module_init */
409 static int
410 module_load(const char *name, const sdb_plugin_ctx_t *plugin_ctx)
411 {
412 char base_name[name ? strlen(name) + 1 : 1];
413 const char *name_ptr;
414 char *tmp;
416 char filename[1024];
417 lt_dlhandle lh;
419 ctx_t *ctx;
421 int status;
423 base_name[0] = '\0';
424 name_ptr = name;
426 while ((tmp = strstr(name_ptr, "::"))) {
427 strncat(base_name, name_ptr, (size_t)(tmp - name_ptr));
428 strcat(base_name, "/");
429 name_ptr = tmp + strlen("::");
430 }
431 strcat(base_name, name_ptr);
433 snprintf(filename, sizeof(filename), "%s/%s.so",
434 PKGLIBDIR, base_name);
435 filename[sizeof(filename) - 1] = '\0';
437 if (access(filename, R_OK)) {
438 char errbuf[1024];
439 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s' (%s): %s",
440 name, filename, sdb_strerror(errno, errbuf, sizeof(errbuf)));
441 return -1;
442 }
444 lt_dlinit();
445 lt_dlerror();
447 lh = lt_dlopen(filename);
448 if (! lh) {
449 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': %s"
450 "The most common cause for this problem are missing "
451 "dependencies.\n", name, lt_dlerror());
452 return -1;
453 }
455 if (ctx_get())
456 sdb_log(SDB_LOG_WARNING, "core: Discarding old plugin context");
458 ctx = ctx_create(name);
459 if (! ctx) {
460 sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin context");
461 return -1;
462 }
464 ctx->info.plugin_name = strdup(name);
465 ctx->info.filename = strdup(filename);
466 ctx->handle = lh;
468 if (plugin_ctx)
469 ctx->public = *plugin_ctx;
471 if ((status = module_init(name, lh, &ctx->info))) {
472 sdb_object_deref(SDB_OBJ(ctx));
473 return status;
474 }
476 /* compare minor version */
477 if ((ctx->info.version < 0)
478 || ((int)(ctx->info.version / 100) != (int)(SDB_VERSION / 100)))
479 sdb_log(SDB_LOG_WARNING, "core: WARNING: version of "
480 "plugin '%s' (%i.%i.%i) does not match our version "
481 "(%i.%i.%i); this might cause problems",
482 name, SDB_VERSION_DECODE(ctx->info.version),
483 SDB_VERSION_DECODE(SDB_VERSION));
485 sdb_llist_append(all_plugins, SDB_OBJ(ctx));
487 sdb_log(SDB_LOG_INFO, "core: Successfully loaded "
488 "plugin '%s' v%i (%s)\n\t%s\n\tLicense: %s",
489 INFO_GET(&ctx->info, name), ctx->info.plugin_version,
490 INFO_GET(&ctx->info, description),
491 INFO_GET(&ctx->info, copyright),
492 INFO_GET(&ctx->info, license));
494 /* any registered callbacks took ownership of the context */
495 sdb_object_deref(SDB_OBJ(ctx));
497 /* reset */
498 ctx_set(NULL);
499 return 0;
500 } /* module_load */
502 static int
503 plugin_add_callback(sdb_llist_t **list, const char *type,
504 const char *name, void *callback, sdb_object_t *user_data)
505 {
506 sdb_object_t *obj;
508 if ((! name) || (! callback))
509 return -1;
511 assert(list);
513 if (! *list)
514 *list = sdb_llist_create();
515 if (! *list)
516 return -1;
518 obj = sdb_object_create(name, sdb_plugin_cb_type,
519 list, type, callback, user_data);
520 if (! obj)
521 return -1;
523 if (sdb_llist_append(*list, obj)) {
524 sdb_object_deref(obj);
525 return -1;
526 }
528 /* pass control to the list */
529 sdb_object_deref(obj);
531 sdb_log(SDB_LOG_INFO, "core: Registered %s callback '%s'.",
532 type, name);
533 return 0;
534 } /* plugin_add_callback */
536 /*
537 * public API
538 */
540 int
541 sdb_plugin_load(const char *name, const sdb_plugin_ctx_t *plugin_ctx)
542 {
543 ctx_t *ctx;
545 if ((! name) || (! *name))
546 return -1;
548 if (! all_plugins) {
549 if (! (all_plugins = sdb_llist_create())) {
550 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': "
551 "internal error while creating linked list", name);
552 return -1;
553 }
554 }
556 ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
557 if (ctx) {
558 /* plugin already loaded */
559 ++ctx->use_cnt;
560 return 0;
561 }
563 return module_load(name, plugin_ctx);
564 } /* sdb_plugin_load */
566 int
567 sdb_plugin_set_info(sdb_plugin_info_t *info, int type, ...)
568 {
569 va_list ap;
571 if (! info)
572 return -1;
574 va_start(ap, type);
576 switch (type) {
577 case SDB_PLUGIN_INFO_NAME:
578 {
579 char *name = va_arg(ap, char *);
580 if (name) {
581 if (info->name)
582 free(info->name);
583 info->name = strdup(name);
584 }
585 }
586 break;
587 case SDB_PLUGIN_INFO_DESC:
588 {
589 char *desc = va_arg(ap, char *);
590 if (desc) {
591 if (info->description)
592 free(info->description);
593 info->description = strdup(desc);
594 }
595 }
596 break;
597 case SDB_PLUGIN_INFO_COPYRIGHT:
598 {
599 char *copyright = va_arg(ap, char *);
600 if (copyright)
601 info->copyright = strdup(copyright);
602 }
603 break;
604 case SDB_PLUGIN_INFO_LICENSE:
605 {
606 char *license = va_arg(ap, char *);
607 if (license) {
608 if (info->license)
609 free(info->license);
610 info->license = strdup(license);
611 }
612 }
613 break;
614 case SDB_PLUGIN_INFO_VERSION:
615 {
616 int version = va_arg(ap, int);
617 info->version = version;
618 }
619 break;
620 case SDB_PLUGIN_INFO_PLUGIN_VERSION:
621 {
622 int version = va_arg(ap, int);
623 info->plugin_version = version;
624 }
625 break;
626 default:
627 va_end(ap);
628 return -1;
629 }
631 va_end(ap);
632 return 0;
633 } /* sdb_plugin_set_info */
635 int
636 sdb_plugin_register_config(const char *name, sdb_plugin_config_cb callback)
637 {
638 return plugin_add_callback(&config_list, "init", name,
639 (void *)callback, NULL);
640 } /* sdb_plugin_register_config */
642 int
643 sdb_plugin_register_init(const char *name, sdb_plugin_init_cb callback,
644 sdb_object_t *user_data)
645 {
646 return plugin_add_callback(&init_list, "init", name,
647 (void *)callback, user_data);
648 } /* sdb_plugin_register_init */
650 int
651 sdb_plugin_register_shutdown(const char *name, sdb_plugin_shutdown_cb callback,
652 sdb_object_t *user_data)
653 {
654 return plugin_add_callback(&shutdown_list, "shutdown", name,
655 (void *)callback, user_data);
656 } /* sdb_plugin_register_shutdown */
658 int
659 sdb_plugin_register_log(const char *name, sdb_plugin_log_cb callback,
660 sdb_object_t *user_data)
661 {
662 return plugin_add_callback(&log_list, "log", name, (void *)callback,
663 user_data);
664 } /* sdb_plugin_register_log */
666 int
667 sdb_plugin_register_cname(const char *name, sdb_plugin_cname_cb callback,
668 sdb_object_t *user_data)
669 {
670 return plugin_add_callback(&cname_list, "cname", name, (void *)callback,
671 user_data);
672 } /* sdb_plugin_register_cname */
674 int
675 sdb_plugin_register_collector(const char *name, sdb_plugin_collector_cb callback,
676 const sdb_time_t *interval, sdb_object_t *user_data)
677 {
678 sdb_object_t *obj;
680 if ((! name) || (! callback))
681 return -1;
683 if (! collector_list)
684 collector_list = sdb_llist_create();
685 if (! collector_list)
686 return -1;
688 obj = sdb_object_create(name, sdb_plugin_collector_cb_type,
689 &collector_list, "collector", callback, user_data);
690 if (! obj)
691 return -1;
693 if (interval)
694 SDB_PLUGIN_CCB(obj)->ccb_interval = *interval;
695 else {
696 sdb_time_t tmp = sdb_plugin_get_ctx().interval;
698 if (tmp > 0)
699 SDB_PLUGIN_CCB(obj)->ccb_interval = tmp;
700 else
701 SDB_PLUGIN_CCB(obj)->ccb_interval = 0;
702 }
704 if (! (SDB_PLUGIN_CCB(obj)->ccb_next_update = sdb_gettime())) {
705 char errbuf[1024];
706 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
707 "time: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
708 sdb_object_deref(obj);
709 return -1;
710 }
712 if (sdb_llist_insert_sorted(collector_list, obj,
713 plugin_cmp_next_update)) {
714 sdb_object_deref(obj);
715 return -1;
716 }
718 /* pass control to the list */
719 sdb_object_deref(obj);
721 sdb_log(SDB_LOG_INFO, "core: Registered collector callback '%s' "
722 "(interval = %.3fs).", name,
723 SDB_TIME_TO_DOUBLE(SDB_PLUGIN_CCB(obj)->ccb_interval));
724 return 0;
725 } /* sdb_plugin_register_collector */
727 sdb_plugin_ctx_t
728 sdb_plugin_get_ctx(void)
729 {
730 ctx_t *c;
732 c = ctx_get();
733 if (! c) {
734 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid read access to plugin "
735 "context outside a plugin");
736 return plugin_default_ctx;
737 }
738 return c->public;
739 } /* sdb_plugin_get_ctx */
741 int
742 sdb_plugin_set_ctx(sdb_plugin_ctx_t ctx, sdb_plugin_ctx_t *old)
743 {
744 ctx_t *c;
746 c = ctx_get();
747 if (! c) {
748 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid write access to plugin "
749 "context outside a plugin");
750 return -1;
751 }
753 if (old)
754 *old = c->public;
755 c->public = ctx;
756 return 0;
757 } /* sdb_plugin_set_ctx */
759 int
760 sdb_plugin_configure(const char *name, oconfig_item_t *ci)
761 {
762 sdb_plugin_cb_t *plugin;
763 sdb_plugin_config_cb callback;
765 ctx_t *old_ctx;
767 int status;
769 if ((! name) || (! ci))
770 return -1;
772 plugin = SDB_PLUGIN_CB(sdb_llist_search_by_name(config_list, name));
773 if (! plugin) {
774 ctx_t *ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
775 if (! ctx)
776 sdb_log(SDB_LOG_ERR, "core: Plugin '%s' not loaded.", name);
777 else
778 sdb_log(SDB_LOG_ERR, "core: Plugin '%s' did not register "
779 "a config callback.", name);
780 errno = ENOENT;
781 return -1;
782 }
784 old_ctx = ctx_set(plugin->cb_ctx);
785 callback = (sdb_plugin_config_cb)plugin->cb_callback;
786 status = callback(ci);
787 ctx_set(old_ctx);
788 return status;
789 } /* sdb_plugin_configure */
791 int
792 sdb_plugin_init_all(void)
793 {
794 sdb_llist_iter_t *iter;
795 int ret = 0;
797 iter = sdb_llist_get_iter(init_list);
798 while (sdb_llist_iter_has_next(iter)) {
799 sdb_plugin_cb_t *cb;
800 sdb_plugin_init_cb callback;
801 ctx_t *old_ctx;
803 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
804 assert(obj);
805 cb = SDB_PLUGIN_CB(obj);
807 callback = (sdb_plugin_init_cb)cb->cb_callback;
809 old_ctx = ctx_set(cb->cb_ctx);
810 if (callback(cb->cb_user_data)) {
811 sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin "
812 "'%s'. Unregistering all callbacks.", obj->name);
813 plugin_unregister_by_name(cb->cb_ctx->info.plugin_name);
814 ++ret;
815 }
816 ctx_set(old_ctx);
817 }
818 sdb_llist_iter_destroy(iter);
819 return ret;
820 } /* sdb_plugin_init_all */
822 int
823 sdb_plugin_collector_loop(sdb_plugin_loop_t *loop)
824 {
825 if (! collector_list) {
826 sdb_log(SDB_LOG_WARNING, "core: No collectors registered. "
827 "Quiting main loop.");
828 return -1;
829 }
831 if (! loop)
832 return -1;
834 while (loop->do_loop) {
835 sdb_plugin_collector_cb callback;
836 ctx_t *old_ctx;
838 sdb_time_t interval, now;
840 sdb_object_t *obj = sdb_llist_shift(collector_list);
841 if (! obj)
842 return -1;
844 callback = (sdb_plugin_collector_cb)SDB_PLUGIN_CCB(obj)->ccb_callback;
846 if (! (now = sdb_gettime())) {
847 char errbuf[1024];
848 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
849 "time in collector main loop: %s",
850 sdb_strerror(errno, errbuf, sizeof(errbuf)));
851 now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
852 }
854 if (now < SDB_PLUGIN_CCB(obj)->ccb_next_update) {
855 interval = SDB_PLUGIN_CCB(obj)->ccb_next_update - now;
857 errno = 0;
858 while (loop->do_loop && sdb_sleep(interval, &interval)) {
859 if (errno != EINTR) {
860 char errbuf[1024];
861 sdb_log(SDB_LOG_ERR, "core: Failed to sleep "
862 "in collector main loop: %s",
863 sdb_strerror(errno, errbuf, sizeof(errbuf)));
864 return -1;
865 }
866 errno = 0;
867 }
869 if (! loop->do_loop)
870 return 0;
871 }
873 old_ctx = ctx_set(SDB_PLUGIN_CCB(obj)->ccb_ctx);
874 if (callback(SDB_PLUGIN_CCB(obj)->ccb_user_data)) {
875 /* XXX */
876 }
877 ctx_set(old_ctx);
879 interval = SDB_PLUGIN_CCB(obj)->ccb_interval;
880 if (! interval)
881 interval = loop->default_interval;
882 if (! interval) {
883 sdb_log(SDB_LOG_WARNING, "core: No interval configured "
884 "for plugin '%s'; skipping any further "
885 "iterations.", obj->name);
886 sdb_object_deref(obj);
887 continue;
888 }
890 SDB_PLUGIN_CCB(obj)->ccb_next_update += interval;
892 if (! (now = sdb_gettime())) {
893 char errbuf[1024];
894 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
895 "time in collector main loop: %s",
896 sdb_strerror(errno, errbuf, sizeof(errbuf)));
897 now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
898 }
900 if (now > SDB_PLUGIN_CCB(obj)->ccb_next_update) {
901 sdb_log(SDB_LOG_WARNING, "core: Plugin '%s' took too "
902 "long; skipping iterations to keep up.",
903 obj->name);
904 SDB_PLUGIN_CCB(obj)->ccb_next_update = now;
905 }
907 if (sdb_llist_insert_sorted(collector_list, obj,
908 plugin_cmp_next_update)) {
909 sdb_log(SDB_LOG_ERR, "core: Failed to re-insert "
910 "plugin '%s' into collector list. Unable to further "
911 "use the plugin.",
912 obj->name);
913 sdb_object_deref(obj);
914 return -1;
915 }
917 /* pass control back to the list */
918 sdb_object_deref(obj);
919 }
920 return 0;
921 } /* sdb_plugin_read_loop */
923 char *
924 sdb_plugin_cname(char *hostname)
925 {
926 sdb_llist_iter_t *iter;
928 if (! hostname)
929 return NULL;
931 if (! cname_list)
932 return hostname;
934 iter = sdb_llist_get_iter(cname_list);
935 while (sdb_llist_iter_has_next(iter)) {
936 sdb_plugin_cname_cb callback;
937 char *cname;
939 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
940 assert(obj);
942 callback = (sdb_plugin_cname_cb)SDB_PLUGIN_CB(obj)->cb_callback;
943 cname = callback(hostname, SDB_PLUGIN_CB(obj)->cb_user_data);
944 if (cname) {
945 free(hostname);
946 hostname = cname;
947 }
948 /* else: don't change hostname */
949 }
950 sdb_llist_iter_destroy(iter);
951 return hostname;
952 } /* sdb_plugin_cname */
954 int
955 sdb_plugin_log(int prio, const char *msg)
956 {
957 sdb_llist_iter_t *iter;
958 int ret = -1;
960 if (! msg)
961 return 0;
963 if (! log_list)
964 return fprintf(stderr, "[%s] %s\n", SDB_LOG_PRIO_TO_STRING(prio), msg);
966 iter = sdb_llist_get_iter(log_list);
967 while (sdb_llist_iter_has_next(iter)) {
968 sdb_plugin_log_cb callback;
969 int tmp;
971 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
972 assert(obj);
974 callback = (sdb_plugin_log_cb)SDB_PLUGIN_CB(obj)->cb_callback;
975 tmp = callback(prio, msg, SDB_PLUGIN_CB(obj)->cb_user_data);
976 if (tmp > ret)
977 ret = tmp;
978 }
979 sdb_llist_iter_destroy(iter);
980 return ret;
981 } /* sdb_plugin_log */
983 int
984 sdb_plugin_vlogf(int prio, const char *fmt, va_list ap)
985 {
986 sdb_strbuf_t *buf;
987 int ret;
989 if (! fmt)
990 return 0;
992 buf = sdb_strbuf_create(64);
993 if (! buf) {
994 ret = fprintf(stderr, "[%s] ", SDB_LOG_PRIO_TO_STRING(prio));
995 ret += vfprintf(stderr, fmt, ap);
996 return ret;
997 }
999 if (sdb_strbuf_vsprintf(buf, fmt, ap) < 0) {
1000 sdb_strbuf_destroy(buf);
1001 return -1;
1002 }
1004 ret = sdb_plugin_log(prio, sdb_strbuf_string(buf));
1005 sdb_strbuf_destroy(buf);
1006 return ret;
1007 } /* sdb_plugin_vlogf */
1009 int
1010 sdb_plugin_logf(int prio, const char *fmt, ...)
1011 {
1012 va_list ap;
1013 int ret;
1015 if (! fmt)
1016 return 0;
1018 va_start(ap, fmt);
1019 ret = sdb_plugin_vlogf(prio, fmt, ap);
1020 va_end(ap);
1021 return ret;
1022 } /* sdb_plugin_logf */
1024 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */