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 sdb_log(SDB_LOG_INFO, "core: Unloading module %s", plugin_name);
243 obj = sdb_llist_remove_by_name(all_plugins, plugin_name);
244 sdb_object_deref(obj);
245 } /* plugin_unregister_by_name */
247 /*
248 * private types
249 */
251 static int
252 ctx_init(sdb_object_t *obj, va_list __attribute__((unused)) ap)
253 {
254 ctx_t *ctx = CTX(obj);
256 assert(ctx);
258 ctx->public = plugin_default_ctx;
259 ctx->info = plugin_default_info;
260 ctx->handle = NULL;
261 ctx->use_cnt = 1;
262 return 0;
263 } /* ctx_init */
265 static void
266 ctx_destroy(sdb_object_t *obj)
267 {
268 ctx_t *ctx = CTX(obj);
270 if (ctx->handle) {
271 const char *err;
272 lt_dlerror();
273 lt_dlclose(ctx->handle);
274 if ((err = lt_dlerror()))
275 sdb_log(SDB_LOG_WARNING, "core: Failed to unload plugin %s: %s",
276 ctx->info.plugin_name, err);
277 }
279 plugin_info_clear(&ctx->info);
280 } /* ctx_destroy */
282 static sdb_type_t ctx_type = {
283 sizeof(ctx_t),
285 ctx_init,
286 ctx_destroy
287 };
289 static ctx_t *
290 ctx_create(const char *name)
291 {
292 ctx_t *ctx;
294 ctx = CTX(sdb_object_create(name, ctx_type));
295 if (! ctx)
296 return NULL;
298 if (! plugin_ctx_key_initialized)
299 ctx_key_init();
300 pthread_setspecific(plugin_ctx_key, ctx);
301 return ctx;
302 } /* ctx_create */
304 static ctx_t *
305 ctx_get(void)
306 {
307 if (! plugin_ctx_key_initialized)
308 ctx_key_init();
309 return pthread_getspecific(plugin_ctx_key);
310 } /* ctx_get */
312 static ctx_t *
313 ctx_set(ctx_t *new)
314 {
315 ctx_t *old;
317 if (! plugin_ctx_key_initialized)
318 ctx_key_init();
320 old = pthread_getspecific(plugin_ctx_key);
321 pthread_setspecific(plugin_ctx_key, new);
322 return old;
323 } /* ctx_set */
325 static int
326 plugin_cb_init(sdb_object_t *obj, va_list ap)
327 {
328 sdb_llist_t **list = va_arg(ap, sdb_llist_t **);
329 const char *type = va_arg(ap, const char *);
330 void *callback = va_arg(ap, void *);
331 sdb_object_t *ud = va_arg(ap, sdb_object_t *);
333 assert(list);
334 assert(type);
335 assert(obj);
337 if (sdb_llist_search_by_name(*list, obj->name)) {
338 sdb_log(SDB_LOG_WARNING, "core: %s callback '%s' "
339 "has already been registered. Ignoring newly "
340 "registered version.", type, obj->name);
341 return -1;
342 }
344 SDB_PLUGIN_CB(obj)->cb_callback = callback;
345 SDB_PLUGIN_CB(obj)->cb_ctx = ctx_get();
346 sdb_object_ref(SDB_OBJ(SDB_PLUGIN_CB(obj)->cb_ctx));
348 sdb_object_ref(ud);
349 SDB_PLUGIN_CB(obj)->cb_user_data = ud;
350 return 0;
351 } /* plugin_cb_init */
353 static void
354 plugin_cb_destroy(sdb_object_t *obj)
355 {
356 assert(obj);
357 sdb_object_deref(SDB_PLUGIN_CB(obj)->cb_user_data);
358 sdb_object_deref(SDB_OBJ(SDB_PLUGIN_CB(obj)->cb_ctx));
359 } /* plugin_cb_destroy */
361 static sdb_type_t sdb_plugin_cb_type = {
362 sizeof(sdb_plugin_cb_t),
364 plugin_cb_init,
365 plugin_cb_destroy
366 };
368 static sdb_type_t sdb_plugin_collector_cb_type = {
369 sizeof(sdb_plugin_collector_cb_t),
371 plugin_cb_init,
372 plugin_cb_destroy
373 };
375 static int
376 plugin_add_callback(sdb_llist_t **list, const char *type,
377 const char *name, void *callback, sdb_object_t *user_data)
378 {
379 sdb_object_t *obj;
381 if ((! name) || (! callback))
382 return -1;
384 assert(list);
386 if (! *list)
387 *list = sdb_llist_create();
388 if (! *list)
389 return -1;
391 obj = sdb_object_create(name, sdb_plugin_cb_type,
392 list, type, callback, user_data);
393 if (! obj)
394 return -1;
396 if (sdb_llist_append(*list, obj)) {
397 sdb_object_deref(obj);
398 return -1;
399 }
401 /* pass control to the list */
402 sdb_object_deref(obj);
404 sdb_log(SDB_LOG_INFO, "core: Registered %s callback '%s'.",
405 type, name);
406 return 0;
407 } /* plugin_add_callback */
409 /*
410 * public API
411 */
413 int
414 sdb_plugin_load(const char *name, const sdb_plugin_ctx_t *plugin_ctx)
415 {
416 char base_name[name ? strlen(name) + 1 : 1];
417 const char *name_ptr;
418 char *tmp;
420 char filename[1024];
421 ctx_t *ctx;
423 lt_dlhandle lh;
425 int (*mod_init)(sdb_plugin_info_t *);
426 int status;
428 if ((! name) || (! *name))
429 return -1;
431 base_name[0] = '\0';
432 name_ptr = name;
434 while ((tmp = strstr(name_ptr, "::"))) {
435 strncat(base_name, name_ptr, (size_t)(tmp - name_ptr));
436 strcat(base_name, "/");
437 name_ptr = tmp + strlen("::");
438 }
439 strcat(base_name, name_ptr);
441 ctx = CTX(sdb_llist_search_by_name(all_plugins, base_name));
442 if (ctx) {
443 /* plugin already loaded */
444 ++ctx->use_cnt;
445 return 0;
446 }
448 snprintf(filename, sizeof(filename), "%s/%s.so",
449 PKGLIBDIR, base_name);
450 filename[sizeof(filename) - 1] = '\0';
452 if (access(filename, R_OK)) {
453 char errbuf[1024];
454 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s' (%s): %s",
455 name, filename, sdb_strerror(errno, errbuf, sizeof(errbuf)));
456 return -1;
457 }
459 lt_dlinit();
460 lt_dlerror();
462 lh = lt_dlopen(filename);
463 if (! lh) {
464 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': %s"
465 "The most common cause for this problem are missing "
466 "dependencies.\n", name, lt_dlerror());
467 return -1;
468 }
470 if (ctx_get())
471 sdb_log(SDB_LOG_WARNING, "core: Discarding old plugin context");
473 ctx = ctx_create(name);
474 if (! ctx) {
475 sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin context");
476 return -1;
477 }
479 ctx->info.plugin_name = strdup(name);
480 ctx->info.filename = strdup(filename);
481 ctx->handle = lh;
483 if (plugin_ctx)
484 ctx->public = *plugin_ctx;
486 mod_init = (int (*)(sdb_plugin_info_t *))lt_dlsym(lh, "sdb_module_init");
487 if (! mod_init) {
488 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': "
489 "could not find symbol 'sdb_module_init'", name);
490 sdb_object_deref(SDB_OBJ(ctx));
491 return -1;
492 }
494 status = mod_init(&ctx->info);
495 if (status) {
496 sdb_log(SDB_LOG_ERR, "core: Failed to initialize "
497 "module '%s'", name);
498 plugin_unregister_by_name(ctx->info.plugin_name);
499 sdb_object_deref(SDB_OBJ(ctx));
500 return -1;
501 }
503 /* compare minor version */
504 if ((ctx->info.version < 0)
505 || ((int)(ctx->info.version / 100) != (int)(SDB_VERSION / 100)))
506 sdb_log(SDB_LOG_WARNING, "core: WARNING: version of "
507 "plugin '%s' (%i.%i.%i) does not match our version "
508 "(%i.%i.%i); this might cause problems",
509 name, SDB_VERSION_DECODE(ctx->info.version),
510 SDB_VERSION_DECODE(SDB_VERSION));
512 if (! all_plugins) {
513 if (! (all_plugins = sdb_llist_create())) {
514 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': "
515 "internal error while creating linked list", name);
516 plugin_unregister_by_name(ctx->info.plugin_name);
517 sdb_object_deref(SDB_OBJ(ctx));
518 return -1;
519 }
520 }
522 sdb_llist_append(all_plugins, SDB_OBJ(ctx));
524 sdb_log(SDB_LOG_INFO, "core: Successfully loaded "
525 "plugin '%s' v%i (%s)\n\t%s\n\tLicense: %s",
526 INFO_GET(&ctx->info, name), ctx->info.plugin_version,
527 INFO_GET(&ctx->info, description),
528 INFO_GET(&ctx->info, copyright),
529 INFO_GET(&ctx->info, license));
531 /* any registered callbacks took ownership of the context */
532 sdb_object_deref(SDB_OBJ(ctx));
534 /* reset */
535 ctx_set(NULL);
536 return 0;
537 } /* sdb_plugin_load */
539 int
540 sdb_plugin_set_info(sdb_plugin_info_t *info, int type, ...)
541 {
542 va_list ap;
544 if (! info)
545 return -1;
547 va_start(ap, type);
549 switch (type) {
550 case SDB_PLUGIN_INFO_NAME:
551 {
552 char *name = va_arg(ap, char *);
553 if (name) {
554 if (info->name)
555 free(info->name);
556 info->name = strdup(name);
557 }
558 }
559 break;
560 case SDB_PLUGIN_INFO_DESC:
561 {
562 char *desc = va_arg(ap, char *);
563 if (desc) {
564 if (info->description)
565 free(info->description);
566 info->description = strdup(desc);
567 }
568 }
569 break;
570 case SDB_PLUGIN_INFO_COPYRIGHT:
571 {
572 char *copyright = va_arg(ap, char *);
573 if (copyright)
574 info->copyright = strdup(copyright);
575 }
576 break;
577 case SDB_PLUGIN_INFO_LICENSE:
578 {
579 char *license = va_arg(ap, char *);
580 if (license) {
581 if (info->license)
582 free(info->license);
583 info->license = strdup(license);
584 }
585 }
586 break;
587 case SDB_PLUGIN_INFO_VERSION:
588 {
589 int version = va_arg(ap, int);
590 info->version = version;
591 }
592 break;
593 case SDB_PLUGIN_INFO_PLUGIN_VERSION:
594 {
595 int version = va_arg(ap, int);
596 info->plugin_version = version;
597 }
598 break;
599 default:
600 va_end(ap);
601 return -1;
602 }
604 va_end(ap);
605 return 0;
606 } /* sdb_plugin_set_info */
608 int
609 sdb_plugin_register_config(const char *name, sdb_plugin_config_cb callback)
610 {
611 return plugin_add_callback(&config_list, "init", name,
612 (void *)callback, NULL);
613 } /* sdb_plugin_register_config */
615 int
616 sdb_plugin_register_init(const char *name, sdb_plugin_init_cb callback,
617 sdb_object_t *user_data)
618 {
619 return plugin_add_callback(&init_list, "init", name,
620 (void *)callback, user_data);
621 } /* sdb_plugin_register_init */
623 int
624 sdb_plugin_register_shutdown(const char *name, sdb_plugin_shutdown_cb callback,
625 sdb_object_t *user_data)
626 {
627 return plugin_add_callback(&shutdown_list, "shutdown", name,
628 (void *)callback, user_data);
629 } /* sdb_plugin_register_shutdown */
631 int
632 sdb_plugin_register_log(const char *name, sdb_plugin_log_cb callback,
633 sdb_object_t *user_data)
634 {
635 return plugin_add_callback(&log_list, "log", name, (void *)callback,
636 user_data);
637 } /* sdb_plugin_register_log */
639 int
640 sdb_plugin_register_cname(const char *name, sdb_plugin_cname_cb callback,
641 sdb_object_t *user_data)
642 {
643 return plugin_add_callback(&cname_list, "cname", name, (void *)callback,
644 user_data);
645 } /* sdb_plugin_register_cname */
647 int
648 sdb_plugin_register_collector(const char *name, sdb_plugin_collector_cb callback,
649 const sdb_time_t *interval, sdb_object_t *user_data)
650 {
651 sdb_object_t *obj;
653 if ((! name) || (! callback))
654 return -1;
656 if (! collector_list)
657 collector_list = sdb_llist_create();
658 if (! collector_list)
659 return -1;
661 obj = sdb_object_create(name, sdb_plugin_collector_cb_type,
662 &collector_list, "collector", callback, user_data);
663 if (! obj)
664 return -1;
666 if (interval)
667 SDB_PLUGIN_CCB(obj)->ccb_interval = *interval;
668 else {
669 sdb_time_t tmp = sdb_plugin_get_ctx().interval;
671 if (tmp > 0)
672 SDB_PLUGIN_CCB(obj)->ccb_interval = tmp;
673 else
674 SDB_PLUGIN_CCB(obj)->ccb_interval = 0;
675 }
677 if (! (SDB_PLUGIN_CCB(obj)->ccb_next_update = sdb_gettime())) {
678 char errbuf[1024];
679 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
680 "time: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
681 sdb_object_deref(obj);
682 return -1;
683 }
685 if (sdb_llist_insert_sorted(collector_list, obj,
686 plugin_cmp_next_update)) {
687 sdb_object_deref(obj);
688 return -1;
689 }
691 /* pass control to the list */
692 sdb_object_deref(obj);
694 sdb_log(SDB_LOG_INFO, "core: Registered collector callback '%s' "
695 "(interval = %.3fs).", name,
696 SDB_TIME_TO_DOUBLE(SDB_PLUGIN_CCB(obj)->ccb_interval));
697 return 0;
698 } /* sdb_plugin_register_collector */
700 sdb_plugin_ctx_t
701 sdb_plugin_get_ctx(void)
702 {
703 ctx_t *c;
705 c = ctx_get();
706 if (! c) {
707 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid read access to plugin "
708 "context outside a plugin");
709 return plugin_default_ctx;
710 }
711 return c->public;
712 } /* sdb_plugin_get_ctx */
714 int
715 sdb_plugin_set_ctx(sdb_plugin_ctx_t ctx, sdb_plugin_ctx_t *old)
716 {
717 ctx_t *c;
719 c = ctx_get();
720 if (! c) {
721 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid write access to plugin "
722 "context outside a plugin");
723 return -1;
724 }
726 if (old)
727 *old = c->public;
728 c->public = ctx;
729 return 0;
730 } /* sdb_plugin_set_ctx */
732 int
733 sdb_plugin_configure(const char *name, oconfig_item_t *ci)
734 {
735 sdb_plugin_cb_t *plugin;
736 sdb_plugin_config_cb callback;
738 ctx_t *old_ctx;
740 int status;
742 if ((! name) || (! ci))
743 return -1;
745 plugin = SDB_PLUGIN_CB(sdb_llist_search_by_name(config_list, name));
746 if (! plugin) {
747 ctx_t *ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
748 if (! ctx)
749 sdb_log(SDB_LOG_ERR, "core: Plugin '%s' not loaded.", name);
750 else
751 sdb_log(SDB_LOG_ERR, "core: Plugin '%s' did not register "
752 "a config callback.", name);
753 errno = ENOENT;
754 return -1;
755 }
757 old_ctx = ctx_set(plugin->cb_ctx);
758 callback = (sdb_plugin_config_cb)plugin->cb_callback;
759 status = callback(ci);
760 ctx_set(old_ctx);
761 return status;
762 } /* sdb_plugin_configure */
764 int
765 sdb_plugin_init_all(void)
766 {
767 sdb_llist_iter_t *iter;
768 int ret = 0;
770 iter = sdb_llist_get_iter(init_list);
771 while (sdb_llist_iter_has_next(iter)) {
772 sdb_plugin_cb_t *cb;
773 sdb_plugin_init_cb callback;
774 ctx_t *old_ctx;
776 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
777 assert(obj);
778 cb = SDB_PLUGIN_CB(obj);
780 callback = (sdb_plugin_init_cb)cb->cb_callback;
782 old_ctx = ctx_set(cb->cb_ctx);
783 if (callback(cb->cb_user_data)) {
784 sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin "
785 "'%s'. Unregistering all callbacks.", obj->name);
786 plugin_unregister_by_name(cb->cb_ctx->info.plugin_name);
787 ++ret;
788 }
789 ctx_set(old_ctx);
790 }
791 sdb_llist_iter_destroy(iter);
792 return ret;
793 } /* sdb_plugin_init_all */
795 int
796 sdb_plugin_collector_loop(sdb_plugin_loop_t *loop)
797 {
798 if (! collector_list) {
799 sdb_log(SDB_LOG_WARNING, "core: No collectors registered. "
800 "Quiting main loop.");
801 return -1;
802 }
804 if (! loop)
805 return -1;
807 while (loop->do_loop) {
808 sdb_plugin_collector_cb callback;
809 ctx_t *old_ctx;
811 sdb_time_t interval, now;
813 sdb_object_t *obj = sdb_llist_shift(collector_list);
814 if (! obj)
815 return -1;
817 callback = (sdb_plugin_collector_cb)SDB_PLUGIN_CCB(obj)->ccb_callback;
819 if (! (now = sdb_gettime())) {
820 char errbuf[1024];
821 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
822 "time in collector main loop: %s",
823 sdb_strerror(errno, errbuf, sizeof(errbuf)));
824 now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
825 }
827 if (now < SDB_PLUGIN_CCB(obj)->ccb_next_update) {
828 interval = SDB_PLUGIN_CCB(obj)->ccb_next_update - now;
830 errno = 0;
831 while (loop->do_loop && sdb_sleep(interval, &interval)) {
832 if (errno != EINTR) {
833 char errbuf[1024];
834 sdb_log(SDB_LOG_ERR, "core: Failed to sleep "
835 "in collector main loop: %s",
836 sdb_strerror(errno, errbuf, sizeof(errbuf)));
837 return -1;
838 }
839 errno = 0;
840 }
842 if (! loop->do_loop)
843 return 0;
844 }
846 old_ctx = ctx_set(SDB_PLUGIN_CCB(obj)->ccb_ctx);
847 if (callback(SDB_PLUGIN_CCB(obj)->ccb_user_data)) {
848 /* XXX */
849 }
850 ctx_set(old_ctx);
852 interval = SDB_PLUGIN_CCB(obj)->ccb_interval;
853 if (! interval)
854 interval = loop->default_interval;
855 if (! interval) {
856 sdb_log(SDB_LOG_WARNING, "core: No interval configured "
857 "for plugin '%s'; skipping any further "
858 "iterations.", obj->name);
859 sdb_object_deref(obj);
860 continue;
861 }
863 SDB_PLUGIN_CCB(obj)->ccb_next_update += interval;
865 if (! (now = sdb_gettime())) {
866 char errbuf[1024];
867 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
868 "time in collector main loop: %s",
869 sdb_strerror(errno, errbuf, sizeof(errbuf)));
870 now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
871 }
873 if (now > SDB_PLUGIN_CCB(obj)->ccb_next_update) {
874 sdb_log(SDB_LOG_WARNING, "core: Plugin '%s' took too "
875 "long; skipping iterations to keep up.",
876 obj->name);
877 SDB_PLUGIN_CCB(obj)->ccb_next_update = now;
878 }
880 if (sdb_llist_insert_sorted(collector_list, obj,
881 plugin_cmp_next_update)) {
882 sdb_log(SDB_LOG_ERR, "core: Failed to re-insert "
883 "plugin '%s' into collector list. Unable to further "
884 "use the plugin.",
885 obj->name);
886 sdb_object_deref(obj);
887 return -1;
888 }
890 /* pass control back to the list */
891 sdb_object_deref(obj);
892 }
893 return 0;
894 } /* sdb_plugin_read_loop */
896 char *
897 sdb_plugin_cname(char *hostname)
898 {
899 sdb_llist_iter_t *iter;
901 if (! hostname)
902 return NULL;
904 if (! cname_list)
905 return hostname;
907 iter = sdb_llist_get_iter(cname_list);
908 while (sdb_llist_iter_has_next(iter)) {
909 sdb_plugin_cname_cb callback;
910 char *cname;
912 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
913 assert(obj);
915 callback = (sdb_plugin_cname_cb)SDB_PLUGIN_CB(obj)->cb_callback;
916 cname = callback(hostname, SDB_PLUGIN_CB(obj)->cb_user_data);
917 if (cname) {
918 free(hostname);
919 hostname = cname;
920 }
921 /* else: don't change hostname */
922 }
923 sdb_llist_iter_destroy(iter);
924 return hostname;
925 } /* sdb_plugin_cname */
927 int
928 sdb_plugin_log(int prio, const char *msg)
929 {
930 sdb_llist_iter_t *iter;
931 int ret = -1;
933 if (! msg)
934 return 0;
936 if (! log_list)
937 return fprintf(stderr, "[%s] %s\n", SDB_LOG_PRIO_TO_STRING(prio), msg);
939 iter = sdb_llist_get_iter(log_list);
940 while (sdb_llist_iter_has_next(iter)) {
941 sdb_plugin_log_cb callback;
942 int tmp;
944 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
945 assert(obj);
947 callback = (sdb_plugin_log_cb)SDB_PLUGIN_CB(obj)->cb_callback;
948 tmp = callback(prio, msg, SDB_PLUGIN_CB(obj)->cb_user_data);
949 if (tmp > ret)
950 ret = tmp;
951 }
952 sdb_llist_iter_destroy(iter);
953 return ret;
954 } /* sdb_plugin_log */
956 int
957 sdb_plugin_vlogf(int prio, const char *fmt, va_list ap)
958 {
959 sdb_strbuf_t *buf;
960 int ret;
962 if (! fmt)
963 return 0;
965 buf = sdb_strbuf_create(64);
966 if (! buf) {
967 ret = fprintf(stderr, "[%s] ", SDB_LOG_PRIO_TO_STRING(prio));
968 ret += vfprintf(stderr, fmt, ap);
969 return ret;
970 }
972 if (sdb_strbuf_vsprintf(buf, fmt, ap) < 0) {
973 sdb_strbuf_destroy(buf);
974 return -1;
975 }
977 ret = sdb_plugin_log(prio, sdb_strbuf_string(buf));
978 sdb_strbuf_destroy(buf);
979 return ret;
980 } /* sdb_plugin_vlogf */
982 int
983 sdb_plugin_logf(int prio, const char *fmt, ...)
984 {
985 va_list ap;
986 int ret;
988 if (! fmt)
989 return 0;
991 va_start(ap, fmt);
992 ret = sdb_plugin_vlogf(prio, fmt, ap);
993 va_end(ap);
994 return ret;
995 } /* sdb_plugin_logf */
997 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */