fe8844a505d061e1df09ec9a720d95c2f30fb9c9
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_get(void)
297 {
298 if (! plugin_ctx_key_initialized)
299 ctx_key_init();
300 return pthread_getspecific(plugin_ctx_key);
301 } /* ctx_get */
303 static ctx_t *
304 ctx_set(ctx_t *new)
305 {
306 ctx_t *old;
308 if (! plugin_ctx_key_initialized)
309 ctx_key_init();
311 old = pthread_getspecific(plugin_ctx_key);
312 if (old)
313 sdb_object_deref(SDB_OBJ(old));
314 if (new)
315 sdb_object_ref(SDB_OBJ(new));
316 pthread_setspecific(plugin_ctx_key, new);
317 return old;
318 } /* ctx_set */
320 static ctx_t *
321 ctx_create(const char *name)
322 {
323 ctx_t *ctx;
325 ctx = CTX(sdb_object_create(name, ctx_type));
326 if (! ctx)
327 return NULL;
329 if (! plugin_ctx_key_initialized)
330 ctx_key_init();
331 ctx_set(ctx);
332 return ctx;
333 } /* ctx_create */
335 static int
336 plugin_cb_init(sdb_object_t *obj, va_list ap)
337 {
338 sdb_llist_t **list = va_arg(ap, sdb_llist_t **);
339 const char *type = va_arg(ap, const char *);
340 void *callback = va_arg(ap, void *);
341 sdb_object_t *ud = va_arg(ap, sdb_object_t *);
343 assert(list);
344 assert(type);
345 assert(obj);
347 if (sdb_llist_search_by_name(*list, obj->name)) {
348 sdb_log(SDB_LOG_WARNING, "core: %s callback '%s' "
349 "has already been registered. Ignoring newly "
350 "registered version.", type, obj->name);
351 return -1;
352 }
354 SDB_PLUGIN_CB(obj)->cb_callback = callback;
355 SDB_PLUGIN_CB(obj)->cb_ctx = ctx_get();
356 sdb_object_ref(SDB_OBJ(SDB_PLUGIN_CB(obj)->cb_ctx));
358 sdb_object_ref(ud);
359 SDB_PLUGIN_CB(obj)->cb_user_data = ud;
360 return 0;
361 } /* plugin_cb_init */
363 static void
364 plugin_cb_destroy(sdb_object_t *obj)
365 {
366 assert(obj);
367 sdb_object_deref(SDB_PLUGIN_CB(obj)->cb_user_data);
368 sdb_object_deref(SDB_OBJ(SDB_PLUGIN_CB(obj)->cb_ctx));
369 } /* plugin_cb_destroy */
371 static sdb_type_t sdb_plugin_cb_type = {
372 sizeof(sdb_plugin_cb_t),
374 plugin_cb_init,
375 plugin_cb_destroy
376 };
378 static sdb_type_t sdb_plugin_collector_cb_type = {
379 sizeof(sdb_plugin_collector_cb_t),
381 plugin_cb_init,
382 plugin_cb_destroy
383 };
385 static int
386 module_init(const char *name, lt_dlhandle lh, sdb_plugin_info_t *info)
387 {
388 int (*mod_init)(sdb_plugin_info_t *);
389 int status;
391 mod_init = (int (*)(sdb_plugin_info_t *))lt_dlsym(lh, "sdb_module_init");
392 if (! mod_init) {
393 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': "
394 "could not find symbol 'sdb_module_init'", name);
395 return -1;
396 }
398 status = mod_init(info);
399 if (status) {
400 sdb_log(SDB_LOG_ERR, "core: Failed to initialize "
401 "module '%s'", name);
402 plugin_unregister_by_name(name);
403 return -1;
404 }
405 return 0;
406 } /* module_init */
408 static int
409 module_load(const char *name, const sdb_plugin_ctx_t *plugin_ctx)
410 {
411 char base_name[name ? strlen(name) + 1 : 1];
412 const char *name_ptr;
413 char *tmp;
415 char filename[1024];
416 lt_dlhandle lh;
418 ctx_t *ctx;
420 int status;
422 base_name[0] = '\0';
423 name_ptr = name;
425 while ((tmp = strstr(name_ptr, "::"))) {
426 strncat(base_name, name_ptr, (size_t)(tmp - name_ptr));
427 strcat(base_name, "/");
428 name_ptr = tmp + strlen("::");
429 }
430 strcat(base_name, name_ptr);
432 snprintf(filename, sizeof(filename), "%s/%s.so",
433 PKGLIBDIR, base_name);
434 filename[sizeof(filename) - 1] = '\0';
436 if (access(filename, R_OK)) {
437 char errbuf[1024];
438 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s' (%s): %s",
439 name, filename, sdb_strerror(errno, errbuf, sizeof(errbuf)));
440 return -1;
441 }
443 lt_dlinit();
444 lt_dlerror();
446 lh = lt_dlopen(filename);
447 if (! lh) {
448 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': %s"
449 "The most common cause for this problem are missing "
450 "dependencies.\n", name, lt_dlerror());
451 return -1;
452 }
454 if (ctx_get())
455 sdb_log(SDB_LOG_WARNING, "core: Discarding old plugin context");
457 ctx = ctx_create(name);
458 if (! ctx) {
459 sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin context");
460 return -1;
461 }
463 ctx->info.plugin_name = strdup(name);
464 ctx->info.filename = strdup(filename);
465 ctx->handle = lh;
467 if (plugin_ctx)
468 ctx->public = *plugin_ctx;
470 if ((status = module_init(name, lh, &ctx->info))) {
471 sdb_object_deref(SDB_OBJ(ctx));
472 return status;
473 }
475 /* compare minor version */
476 if ((ctx->info.version < 0)
477 || ((int)(ctx->info.version / 100) != (int)(SDB_VERSION / 100)))
478 sdb_log(SDB_LOG_WARNING, "core: WARNING: version of "
479 "plugin '%s' (%i.%i.%i) does not match our version "
480 "(%i.%i.%i); this might cause problems",
481 name, SDB_VERSION_DECODE(ctx->info.version),
482 SDB_VERSION_DECODE(SDB_VERSION));
484 sdb_llist_append(all_plugins, SDB_OBJ(ctx));
486 sdb_log(SDB_LOG_INFO, "core: Successfully loaded "
487 "plugin '%s' v%i (%s)\n\t%s\n\tLicense: %s",
488 INFO_GET(&ctx->info, name), ctx->info.plugin_version,
489 INFO_GET(&ctx->info, description),
490 INFO_GET(&ctx->info, copyright),
491 INFO_GET(&ctx->info, license));
493 /* any registered callbacks took ownership of the context */
494 sdb_object_deref(SDB_OBJ(ctx));
496 /* reset */
497 ctx_set(NULL);
498 return 0;
499 } /* module_load */
501 static int
502 plugin_add_callback(sdb_llist_t **list, const char *type,
503 const char *name, void *callback, sdb_object_t *user_data)
504 {
505 sdb_object_t *obj;
507 if ((! name) || (! callback))
508 return -1;
510 assert(list);
512 if (! *list)
513 *list = sdb_llist_create();
514 if (! *list)
515 return -1;
517 obj = sdb_object_create(name, sdb_plugin_cb_type,
518 list, type, callback, user_data);
519 if (! obj)
520 return -1;
522 if (sdb_llist_append(*list, obj)) {
523 sdb_object_deref(obj);
524 return -1;
525 }
527 /* pass control to the list */
528 sdb_object_deref(obj);
530 sdb_log(SDB_LOG_INFO, "core: Registered %s callback '%s'.",
531 type, name);
532 return 0;
533 } /* plugin_add_callback */
535 /*
536 * public API
537 */
539 int
540 sdb_plugin_load(const char *name, const sdb_plugin_ctx_t *plugin_ctx)
541 {
542 ctx_t *ctx;
544 if ((! name) || (! *name))
545 return -1;
547 if (! all_plugins) {
548 if (! (all_plugins = sdb_llist_create())) {
549 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': "
550 "internal error while creating linked list", name);
551 return -1;
552 }
553 }
555 ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
556 if (ctx) {
557 /* plugin already loaded */
558 ++ctx->use_cnt;
559 return 0;
560 }
562 return module_load(name, plugin_ctx);
563 } /* sdb_plugin_load */
565 int
566 sdb_plugin_set_info(sdb_plugin_info_t *info, int type, ...)
567 {
568 va_list ap;
570 if (! info)
571 return -1;
573 va_start(ap, type);
575 switch (type) {
576 case SDB_PLUGIN_INFO_NAME:
577 {
578 char *name = va_arg(ap, char *);
579 if (name) {
580 if (info->name)
581 free(info->name);
582 info->name = strdup(name);
583 }
584 }
585 break;
586 case SDB_PLUGIN_INFO_DESC:
587 {
588 char *desc = va_arg(ap, char *);
589 if (desc) {
590 if (info->description)
591 free(info->description);
592 info->description = strdup(desc);
593 }
594 }
595 break;
596 case SDB_PLUGIN_INFO_COPYRIGHT:
597 {
598 char *copyright = va_arg(ap, char *);
599 if (copyright)
600 info->copyright = strdup(copyright);
601 }
602 break;
603 case SDB_PLUGIN_INFO_LICENSE:
604 {
605 char *license = va_arg(ap, char *);
606 if (license) {
607 if (info->license)
608 free(info->license);
609 info->license = strdup(license);
610 }
611 }
612 break;
613 case SDB_PLUGIN_INFO_VERSION:
614 {
615 int version = va_arg(ap, int);
616 info->version = version;
617 }
618 break;
619 case SDB_PLUGIN_INFO_PLUGIN_VERSION:
620 {
621 int version = va_arg(ap, int);
622 info->plugin_version = version;
623 }
624 break;
625 default:
626 va_end(ap);
627 return -1;
628 }
630 va_end(ap);
631 return 0;
632 } /* sdb_plugin_set_info */
634 int
635 sdb_plugin_register_config(const char *name, sdb_plugin_config_cb callback)
636 {
637 return plugin_add_callback(&config_list, "init", name,
638 (void *)callback, NULL);
639 } /* sdb_plugin_register_config */
641 int
642 sdb_plugin_register_init(const char *name, sdb_plugin_init_cb callback,
643 sdb_object_t *user_data)
644 {
645 return plugin_add_callback(&init_list, "init", name,
646 (void *)callback, user_data);
647 } /* sdb_plugin_register_init */
649 int
650 sdb_plugin_register_shutdown(const char *name, sdb_plugin_shutdown_cb callback,
651 sdb_object_t *user_data)
652 {
653 return plugin_add_callback(&shutdown_list, "shutdown", name,
654 (void *)callback, user_data);
655 } /* sdb_plugin_register_shutdown */
657 int
658 sdb_plugin_register_log(const char *name, sdb_plugin_log_cb callback,
659 sdb_object_t *user_data)
660 {
661 return plugin_add_callback(&log_list, "log", name, (void *)callback,
662 user_data);
663 } /* sdb_plugin_register_log */
665 int
666 sdb_plugin_register_cname(const char *name, sdb_plugin_cname_cb callback,
667 sdb_object_t *user_data)
668 {
669 return plugin_add_callback(&cname_list, "cname", name, (void *)callback,
670 user_data);
671 } /* sdb_plugin_register_cname */
673 int
674 sdb_plugin_register_collector(const char *name, sdb_plugin_collector_cb callback,
675 const sdb_time_t *interval, sdb_object_t *user_data)
676 {
677 sdb_object_t *obj;
679 if ((! name) || (! callback))
680 return -1;
682 if (! collector_list)
683 collector_list = sdb_llist_create();
684 if (! collector_list)
685 return -1;
687 obj = sdb_object_create(name, sdb_plugin_collector_cb_type,
688 &collector_list, "collector", callback, user_data);
689 if (! obj)
690 return -1;
692 if (interval)
693 SDB_PLUGIN_CCB(obj)->ccb_interval = *interval;
694 else {
695 sdb_time_t tmp = sdb_plugin_get_ctx().interval;
697 if (tmp > 0)
698 SDB_PLUGIN_CCB(obj)->ccb_interval = tmp;
699 else
700 SDB_PLUGIN_CCB(obj)->ccb_interval = 0;
701 }
703 if (! (SDB_PLUGIN_CCB(obj)->ccb_next_update = sdb_gettime())) {
704 char errbuf[1024];
705 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
706 "time: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
707 sdb_object_deref(obj);
708 return -1;
709 }
711 if (sdb_llist_insert_sorted(collector_list, obj,
712 plugin_cmp_next_update)) {
713 sdb_object_deref(obj);
714 return -1;
715 }
717 /* pass control to the list */
718 sdb_object_deref(obj);
720 sdb_log(SDB_LOG_INFO, "core: Registered collector callback '%s' "
721 "(interval = %.3fs).", name,
722 SDB_TIME_TO_DOUBLE(SDB_PLUGIN_CCB(obj)->ccb_interval));
723 return 0;
724 } /* sdb_plugin_register_collector */
726 sdb_plugin_ctx_t
727 sdb_plugin_get_ctx(void)
728 {
729 ctx_t *c;
731 c = ctx_get();
732 if (! c) {
733 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid read access to plugin "
734 "context outside a plugin");
735 return plugin_default_ctx;
736 }
737 return c->public;
738 } /* sdb_plugin_get_ctx */
740 int
741 sdb_plugin_set_ctx(sdb_plugin_ctx_t ctx, sdb_plugin_ctx_t *old)
742 {
743 ctx_t *c;
745 c = ctx_get();
746 if (! c) {
747 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid write access to plugin "
748 "context outside a plugin");
749 return -1;
750 }
752 if (old)
753 *old = c->public;
754 c->public = ctx;
755 return 0;
756 } /* sdb_plugin_set_ctx */
758 int
759 sdb_plugin_configure(const char *name, oconfig_item_t *ci)
760 {
761 sdb_plugin_cb_t *plugin;
762 sdb_plugin_config_cb callback;
764 ctx_t *old_ctx;
766 int status;
768 if ((! name) || (! ci))
769 return -1;
771 plugin = SDB_PLUGIN_CB(sdb_llist_search_by_name(config_list, name));
772 if (! plugin) {
773 ctx_t *ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
774 if (! ctx)
775 sdb_log(SDB_LOG_ERR, "core: Plugin '%s' not loaded.", name);
776 else
777 sdb_log(SDB_LOG_ERR, "core: Plugin '%s' did not register "
778 "a config callback.", name);
779 errno = ENOENT;
780 return -1;
781 }
783 old_ctx = ctx_set(plugin->cb_ctx);
784 callback = (sdb_plugin_config_cb)plugin->cb_callback;
785 status = callback(ci);
786 ctx_set(old_ctx);
787 return status;
788 } /* sdb_plugin_configure */
790 int
791 sdb_plugin_init_all(void)
792 {
793 sdb_llist_iter_t *iter;
794 int ret = 0;
796 iter = sdb_llist_get_iter(init_list);
797 while (sdb_llist_iter_has_next(iter)) {
798 sdb_plugin_cb_t *cb;
799 sdb_plugin_init_cb callback;
800 ctx_t *old_ctx;
802 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
803 assert(obj);
804 cb = SDB_PLUGIN_CB(obj);
806 callback = (sdb_plugin_init_cb)cb->cb_callback;
808 old_ctx = ctx_set(cb->cb_ctx);
809 if (callback(cb->cb_user_data)) {
810 sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin "
811 "'%s'. Unregistering all callbacks.", obj->name);
812 ctx_set(old_ctx);
813 plugin_unregister_by_name(cb->cb_ctx->info.plugin_name);
814 ++ret;
815 }
816 else
817 ctx_set(old_ctx);
818 }
819 sdb_llist_iter_destroy(iter);
820 return ret;
821 } /* sdb_plugin_init_all */
823 int
824 sdb_plugin_collector_loop(sdb_plugin_loop_t *loop)
825 {
826 if (! collector_list) {
827 sdb_log(SDB_LOG_WARNING, "core: No collectors registered. "
828 "Quiting main loop.");
829 return -1;
830 }
832 if (! loop)
833 return -1;
835 while (loop->do_loop) {
836 sdb_plugin_collector_cb callback;
837 ctx_t *old_ctx;
839 sdb_time_t interval, now;
841 sdb_object_t *obj = sdb_llist_shift(collector_list);
842 if (! obj)
843 return -1;
845 callback = (sdb_plugin_collector_cb)SDB_PLUGIN_CCB(obj)->ccb_callback;
847 if (! (now = sdb_gettime())) {
848 char errbuf[1024];
849 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
850 "time in collector main loop: %s",
851 sdb_strerror(errno, errbuf, sizeof(errbuf)));
852 now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
853 }
855 if (now < SDB_PLUGIN_CCB(obj)->ccb_next_update) {
856 interval = SDB_PLUGIN_CCB(obj)->ccb_next_update - now;
858 errno = 0;
859 while (loop->do_loop && sdb_sleep(interval, &interval)) {
860 if (errno != EINTR) {
861 char errbuf[1024];
862 sdb_log(SDB_LOG_ERR, "core: Failed to sleep "
863 "in collector main loop: %s",
864 sdb_strerror(errno, errbuf, sizeof(errbuf)));
865 return -1;
866 }
867 errno = 0;
868 }
870 if (! loop->do_loop)
871 return 0;
872 }
874 old_ctx = ctx_set(SDB_PLUGIN_CCB(obj)->ccb_ctx);
875 if (callback(SDB_PLUGIN_CCB(obj)->ccb_user_data)) {
876 /* XXX */
877 }
878 ctx_set(old_ctx);
880 interval = SDB_PLUGIN_CCB(obj)->ccb_interval;
881 if (! interval)
882 interval = loop->default_interval;
883 if (! interval) {
884 sdb_log(SDB_LOG_WARNING, "core: No interval configured "
885 "for plugin '%s'; skipping any further "
886 "iterations.", obj->name);
887 sdb_object_deref(obj);
888 continue;
889 }
891 SDB_PLUGIN_CCB(obj)->ccb_next_update += interval;
893 if (! (now = sdb_gettime())) {
894 char errbuf[1024];
895 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
896 "time in collector main loop: %s",
897 sdb_strerror(errno, errbuf, sizeof(errbuf)));
898 now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
899 }
901 if (now > SDB_PLUGIN_CCB(obj)->ccb_next_update) {
902 sdb_log(SDB_LOG_WARNING, "core: Plugin '%s' took too "
903 "long; skipping iterations to keep up.",
904 obj->name);
905 SDB_PLUGIN_CCB(obj)->ccb_next_update = now;
906 }
908 if (sdb_llist_insert_sorted(collector_list, obj,
909 plugin_cmp_next_update)) {
910 sdb_log(SDB_LOG_ERR, "core: Failed to re-insert "
911 "plugin '%s' into collector list. Unable to further "
912 "use the plugin.",
913 obj->name);
914 sdb_object_deref(obj);
915 return -1;
916 }
918 /* pass control back to the list */
919 sdb_object_deref(obj);
920 }
921 return 0;
922 } /* sdb_plugin_read_loop */
924 char *
925 sdb_plugin_cname(char *hostname)
926 {
927 sdb_llist_iter_t *iter;
929 if (! hostname)
930 return NULL;
932 if (! cname_list)
933 return hostname;
935 iter = sdb_llist_get_iter(cname_list);
936 while (sdb_llist_iter_has_next(iter)) {
937 sdb_plugin_cname_cb callback;
938 char *cname;
940 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
941 assert(obj);
943 callback = (sdb_plugin_cname_cb)SDB_PLUGIN_CB(obj)->cb_callback;
944 cname = callback(hostname, SDB_PLUGIN_CB(obj)->cb_user_data);
945 if (cname) {
946 free(hostname);
947 hostname = cname;
948 }
949 /* else: don't change hostname */
950 }
951 sdb_llist_iter_destroy(iter);
952 return hostname;
953 } /* sdb_plugin_cname */
955 int
956 sdb_plugin_log(int prio, const char *msg)
957 {
958 sdb_llist_iter_t *iter;
959 int ret = -1;
961 if (! msg)
962 return 0;
964 if (! sdb_llist_len(log_list))
965 return fprintf(stderr, "[%s] %s\n", SDB_LOG_PRIO_TO_STRING(prio), msg);
967 iter = sdb_llist_get_iter(log_list);
968 while (sdb_llist_iter_has_next(iter)) {
969 sdb_plugin_log_cb callback;
970 int tmp;
972 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
973 assert(obj);
975 callback = (sdb_plugin_log_cb)SDB_PLUGIN_CB(obj)->cb_callback;
976 tmp = callback(prio, msg, SDB_PLUGIN_CB(obj)->cb_user_data);
977 if (tmp > ret)
978 ret = tmp;
979 }
980 sdb_llist_iter_destroy(iter);
981 return ret;
982 } /* sdb_plugin_log */
984 int
985 sdb_plugin_vlogf(int prio, const char *fmt, va_list ap)
986 {
987 sdb_strbuf_t *buf;
988 int ret;
990 if (! fmt)
991 return 0;
993 buf = sdb_strbuf_create(64);
994 if (! buf) {
995 ret = fprintf(stderr, "[%s] ", SDB_LOG_PRIO_TO_STRING(prio));
996 ret += vfprintf(stderr, fmt, ap);
997 return ret;
998 }
1000 if (sdb_strbuf_vsprintf(buf, fmt, ap) < 0) {
1001 sdb_strbuf_destroy(buf);
1002 return -1;
1003 }
1005 ret = sdb_plugin_log(prio, sdb_strbuf_string(buf));
1006 sdb_strbuf_destroy(buf);
1007 return ret;
1008 } /* sdb_plugin_vlogf */
1010 int
1011 sdb_plugin_logf(int prio, const char *fmt, ...)
1012 {
1013 va_list ap;
1014 int ret;
1016 if (! fmt)
1017 return 0;
1019 va_start(ap, fmt);
1020 ret = sdb_plugin_vlogf(prio, fmt, ap);
1021 va_end(ap);
1022 return ret;
1023 } /* sdb_plugin_logf */
1025 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */