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_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)
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;
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)
293 {
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 pthread_setspecific(plugin_ctx_key, ctx);
303 return ctx;
304 } /* ctx_create */
306 static ctx_t *
307 ctx_get(void)
308 {
309 if (! plugin_ctx_key_initialized)
310 ctx_key_init();
311 return pthread_getspecific(plugin_ctx_key);
312 } /* ctx_get */
314 static ctx_t *
315 ctx_set(ctx_t *new)
316 {
317 ctx_t *old;
319 if (! plugin_ctx_key_initialized)
320 ctx_key_init();
322 old = pthread_getspecific(plugin_ctx_key);
323 pthread_setspecific(plugin_ctx_key, new);
324 return old;
325 } /* ctx_set */
327 static int
328 plugin_cb_init(sdb_object_t *obj, va_list ap)
329 {
330 sdb_llist_t **list = va_arg(ap, sdb_llist_t **);
331 const char *type = va_arg(ap, const char *);
332 void *callback = va_arg(ap, void *);
333 sdb_object_t *ud = va_arg(ap, sdb_object_t *);
335 assert(list);
336 assert(type);
337 assert(obj);
339 if (sdb_llist_search_by_name(*list, obj->name)) {
340 sdb_log(SDB_LOG_WARNING, "core: %s callback '%s' "
341 "has already been registered. Ignoring newly "
342 "registered version.", type, obj->name);
343 return -1;
344 }
346 SDB_PLUGIN_CB(obj)->cb_callback = callback;
347 SDB_PLUGIN_CB(obj)->cb_ctx = ctx_get();
348 sdb_object_ref(SDB_OBJ(SDB_PLUGIN_CB(obj)->cb_ctx));
350 sdb_object_ref(ud);
351 SDB_PLUGIN_CB(obj)->cb_user_data = ud;
352 return 0;
353 } /* plugin_cb_init */
355 static void
356 plugin_cb_destroy(sdb_object_t *obj)
357 {
358 assert(obj);
359 sdb_object_deref(SDB_PLUGIN_CB(obj)->cb_user_data);
360 sdb_object_deref(SDB_OBJ(SDB_PLUGIN_CB(obj)->cb_ctx));
361 } /* plugin_cb_destroy */
363 static sdb_type_t sdb_plugin_cb_type = {
364 sizeof(sdb_plugin_cb_t),
366 plugin_cb_init,
367 plugin_cb_destroy
368 };
370 static sdb_type_t sdb_plugin_collector_cb_type = {
371 sizeof(sdb_plugin_collector_cb_t),
373 plugin_cb_init,
374 plugin_cb_destroy
375 };
377 static int
378 plugin_add_callback(sdb_llist_t **list, const char *type,
379 const char *name, void *callback, sdb_object_t *user_data)
380 {
381 sdb_object_t *obj;
383 if ((! name) || (! callback))
384 return -1;
386 assert(list);
388 if (! *list)
389 *list = sdb_llist_create();
390 if (! *list)
391 return -1;
393 obj = sdb_object_create(name, sdb_plugin_cb_type,
394 list, type, callback, user_data);
395 if (! obj)
396 return -1;
398 if (sdb_llist_append(*list, obj)) {
399 sdb_object_deref(obj);
400 return -1;
401 }
403 /* pass control to the list */
404 sdb_object_deref(obj);
406 sdb_log(SDB_LOG_INFO, "core: Registered %s callback '%s'.",
407 type, name);
408 return 0;
409 } /* plugin_add_callback */
411 /*
412 * public API
413 */
415 int
416 sdb_plugin_load(const char *name, const sdb_plugin_ctx_t *plugin_ctx)
417 {
418 char base_name[name ? strlen(name) + 1 : 1];
419 const char *name_ptr;
420 char *tmp;
422 char filename[1024];
423 ctx_t *ctx;
425 lt_dlhandle lh;
427 int (*mod_init)(sdb_plugin_info_t *);
428 int status;
430 if ((! name) || (! *name))
431 return -1;
433 base_name[0] = '\0';
434 name_ptr = name;
436 while ((tmp = strstr(name_ptr, "::"))) {
437 strncat(base_name, name_ptr, (size_t)(tmp - name_ptr));
438 strcat(base_name, "/");
439 name_ptr = tmp + strlen("::");
440 }
441 strcat(base_name, name_ptr);
443 ctx = CTX(sdb_llist_search_by_name(all_plugins, base_name));
444 if (ctx) {
445 /* plugin already loaded */
446 ++ctx->use_cnt;
447 return 0;
448 }
450 snprintf(filename, sizeof(filename), "%s/%s.so",
451 PKGLIBDIR, base_name);
452 filename[sizeof(filename) - 1] = '\0';
454 if (access(filename, R_OK)) {
455 char errbuf[1024];
456 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s' (%s): %s",
457 name, filename, sdb_strerror(errno, errbuf, sizeof(errbuf)));
458 return -1;
459 }
461 lt_dlinit();
462 lt_dlerror();
464 lh = lt_dlopen(filename);
465 if (! lh) {
466 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': %s"
467 "The most common cause for this problem are missing "
468 "dependencies.\n", name, lt_dlerror());
469 return -1;
470 }
472 if (ctx_get())
473 sdb_log(SDB_LOG_WARNING, "core: Discarding old plugin context");
475 ctx = ctx_create(name);
476 if (! ctx) {
477 sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin context");
478 return -1;
479 }
481 ctx->info.plugin_name = strdup(name);
482 ctx->info.filename = strdup(filename);
483 ctx->handle = lh;
485 if (plugin_ctx)
486 ctx->public = *plugin_ctx;
488 mod_init = (int (*)(sdb_plugin_info_t *))lt_dlsym(lh, "sdb_module_init");
489 if (! mod_init) {
490 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': "
491 "could not find symbol 'sdb_module_init'", name);
492 sdb_object_deref(SDB_OBJ(ctx));
493 return -1;
494 }
496 status = mod_init(&ctx->info);
497 if (status) {
498 sdb_log(SDB_LOG_ERR, "core: Failed to initialize "
499 "module '%s'", name);
500 plugin_unregister_by_name(ctx->info.plugin_name);
501 sdb_object_deref(SDB_OBJ(ctx));
502 return -1;
503 }
505 /* compare minor version */
506 if ((ctx->info.version < 0)
507 || ((int)(ctx->info.version / 100) != (int)(SDB_VERSION / 100)))
508 sdb_log(SDB_LOG_WARNING, "core: WARNING: version of "
509 "plugin '%s' (%i.%i.%i) does not match our version "
510 "(%i.%i.%i); this might cause problems",
511 name, SDB_VERSION_DECODE(ctx->info.version),
512 SDB_VERSION_DECODE(SDB_VERSION));
514 if (! all_plugins) {
515 if (! (all_plugins = sdb_llist_create())) {
516 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': "
517 "internal error while creating linked list", name);
518 plugin_unregister_by_name(ctx->info.plugin_name);
519 sdb_object_deref(SDB_OBJ(ctx));
520 return -1;
521 }
522 }
524 sdb_llist_append(all_plugins, SDB_OBJ(ctx));
526 sdb_log(SDB_LOG_INFO, "core: Successfully loaded "
527 "plugin '%s' v%i (%s)\n\t%s\n\tLicense: %s",
528 INFO_GET(&ctx->info, name), ctx->info.plugin_version,
529 INFO_GET(&ctx->info, description),
530 INFO_GET(&ctx->info, copyright),
531 INFO_GET(&ctx->info, license));
533 /* any registered callbacks took ownership of the context */
534 sdb_object_deref(SDB_OBJ(ctx));
536 /* reset */
537 ctx_set(NULL);
538 return 0;
539 } /* sdb_plugin_load */
541 int
542 sdb_plugin_set_info(sdb_plugin_info_t *info, int type, ...)
543 {
544 va_list ap;
546 if (! info)
547 return -1;
549 va_start(ap, type);
551 switch (type) {
552 case SDB_PLUGIN_INFO_NAME:
553 {
554 char *name = va_arg(ap, char *);
555 if (name) {
556 if (info->name)
557 free(info->name);
558 info->name = strdup(name);
559 }
560 }
561 break;
562 case SDB_PLUGIN_INFO_DESC:
563 {
564 char *desc = va_arg(ap, char *);
565 if (desc) {
566 if (info->description)
567 free(info->description);
568 info->description = strdup(desc);
569 }
570 }
571 break;
572 case SDB_PLUGIN_INFO_COPYRIGHT:
573 {
574 char *copyright = va_arg(ap, char *);
575 if (copyright)
576 info->copyright = strdup(copyright);
577 }
578 break;
579 case SDB_PLUGIN_INFO_LICENSE:
580 {
581 char *license = va_arg(ap, char *);
582 if (license) {
583 if (info->license)
584 free(info->license);
585 info->license = strdup(license);
586 }
587 }
588 break;
589 case SDB_PLUGIN_INFO_VERSION:
590 {
591 int version = va_arg(ap, int);
592 info->version = version;
593 }
594 break;
595 case SDB_PLUGIN_INFO_PLUGIN_VERSION:
596 {
597 int version = va_arg(ap, int);
598 info->plugin_version = version;
599 }
600 break;
601 default:
602 va_end(ap);
603 return -1;
604 }
606 va_end(ap);
607 return 0;
608 } /* sdb_plugin_set_info */
610 int
611 sdb_plugin_register_config(const char *name, sdb_plugin_config_cb callback)
612 {
613 return plugin_add_callback(&config_list, "init", name,
614 (void *)callback, NULL);
615 } /* sdb_plugin_register_config */
617 int
618 sdb_plugin_register_init(const char *name, sdb_plugin_init_cb callback,
619 sdb_object_t *user_data)
620 {
621 return plugin_add_callback(&init_list, "init", name,
622 (void *)callback, user_data);
623 } /* sdb_plugin_register_init */
625 int
626 sdb_plugin_register_shutdown(const char *name, sdb_plugin_shutdown_cb callback,
627 sdb_object_t *user_data)
628 {
629 return plugin_add_callback(&shutdown_list, "shutdown", name,
630 (void *)callback, user_data);
631 } /* sdb_plugin_register_shutdown */
633 int
634 sdb_plugin_register_log(const char *name, sdb_plugin_log_cb callback,
635 sdb_object_t *user_data)
636 {
637 return plugin_add_callback(&log_list, "log", name, (void *)callback,
638 user_data);
639 } /* sdb_plugin_register_log */
641 int
642 sdb_plugin_register_cname(const char *name, sdb_plugin_cname_cb callback,
643 sdb_object_t *user_data)
644 {
645 return plugin_add_callback(&cname_list, "cname", name, (void *)callback,
646 user_data);
647 } /* sdb_plugin_register_cname */
649 int
650 sdb_plugin_register_collector(const char *name, sdb_plugin_collector_cb callback,
651 const sdb_time_t *interval, sdb_object_t *user_data)
652 {
653 sdb_object_t *obj;
655 if ((! name) || (! callback))
656 return -1;
658 if (! collector_list)
659 collector_list = sdb_llist_create();
660 if (! collector_list)
661 return -1;
663 obj = sdb_object_create(name, sdb_plugin_collector_cb_type,
664 &collector_list, "collector", callback, user_data);
665 if (! obj)
666 return -1;
668 if (interval)
669 SDB_PLUGIN_CCB(obj)->ccb_interval = *interval;
670 else {
671 sdb_time_t tmp = sdb_plugin_get_ctx().interval;
673 if (tmp > 0)
674 SDB_PLUGIN_CCB(obj)->ccb_interval = tmp;
675 else
676 SDB_PLUGIN_CCB(obj)->ccb_interval = 0;
677 }
679 if (! (SDB_PLUGIN_CCB(obj)->ccb_next_update = sdb_gettime())) {
680 char errbuf[1024];
681 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
682 "time: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
683 sdb_object_deref(obj);
684 return -1;
685 }
687 if (sdb_llist_insert_sorted(collector_list, obj,
688 plugin_cmp_next_update)) {
689 sdb_object_deref(obj);
690 return -1;
691 }
693 /* pass control to the list */
694 sdb_object_deref(obj);
696 sdb_log(SDB_LOG_INFO, "core: Registered collector callback '%s' "
697 "(interval = %.3fs).", name,
698 SDB_TIME_TO_DOUBLE(SDB_PLUGIN_CCB(obj)->ccb_interval));
699 return 0;
700 } /* sdb_plugin_register_collector */
702 sdb_plugin_ctx_t
703 sdb_plugin_get_ctx(void)
704 {
705 ctx_t *c;
707 c = ctx_get();
708 if (! c) {
709 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid read access to plugin "
710 "context outside a plugin");
711 return plugin_default_ctx;
712 }
713 return c->public;
714 } /* sdb_plugin_get_ctx */
716 int
717 sdb_plugin_set_ctx(sdb_plugin_ctx_t ctx, sdb_plugin_ctx_t *old)
718 {
719 ctx_t *c;
721 c = ctx_get();
722 if (! c) {
723 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid write access to plugin "
724 "context outside a plugin");
725 return -1;
726 }
728 if (old)
729 *old = c->public;
730 c->public = ctx;
731 return 0;
732 } /* sdb_plugin_set_ctx */
734 int
735 sdb_plugin_configure(const char *name, oconfig_item_t *ci)
736 {
737 sdb_plugin_cb_t *plugin;
738 sdb_plugin_config_cb callback;
740 ctx_t *old_ctx;
742 int status;
744 if ((! name) || (! ci))
745 return -1;
747 plugin = SDB_PLUGIN_CB(sdb_llist_search_by_name(config_list, name));
748 if (! plugin) {
749 ctx_t *ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
750 if (! ctx)
751 sdb_log(SDB_LOG_ERR, "core: Plugin '%s' not loaded.", name);
752 else
753 sdb_log(SDB_LOG_ERR, "core: Plugin '%s' did not register "
754 "a config callback.", name);
755 errno = ENOENT;
756 return -1;
757 }
759 old_ctx = ctx_set(plugin->cb_ctx);
760 callback = (sdb_plugin_config_cb)plugin->cb_callback;
761 status = callback(ci);
762 ctx_set(old_ctx);
763 return status;
764 } /* sdb_plugin_configure */
766 int
767 sdb_plugin_init_all(void)
768 {
769 sdb_llist_iter_t *iter;
770 int ret = 0;
772 iter = sdb_llist_get_iter(init_list);
773 while (sdb_llist_iter_has_next(iter)) {
774 sdb_plugin_cb_t *cb;
775 sdb_plugin_init_cb callback;
776 ctx_t *old_ctx;
778 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
779 assert(obj);
780 cb = SDB_PLUGIN_CB(obj);
782 callback = (sdb_plugin_init_cb)cb->cb_callback;
784 old_ctx = ctx_set(cb->cb_ctx);
785 if (callback(cb->cb_user_data)) {
786 sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin "
787 "'%s'. Unregistering all callbacks.", obj->name);
788 plugin_unregister_by_name(cb->cb_ctx->info.plugin_name);
789 ++ret;
790 }
791 ctx_set(old_ctx);
792 }
793 sdb_llist_iter_destroy(iter);
794 return ret;
795 } /* sdb_plugin_init_all */
797 int
798 sdb_plugin_collector_loop(sdb_plugin_loop_t *loop)
799 {
800 if (! collector_list) {
801 sdb_log(SDB_LOG_WARNING, "core: No collectors registered. "
802 "Quiting main loop.");
803 return -1;
804 }
806 if (! loop)
807 return -1;
809 while (loop->do_loop) {
810 sdb_plugin_collector_cb callback;
811 ctx_t *old_ctx;
813 sdb_time_t interval, now;
815 sdb_object_t *obj = sdb_llist_shift(collector_list);
816 if (! obj)
817 return -1;
819 callback = (sdb_plugin_collector_cb)SDB_PLUGIN_CCB(obj)->ccb_callback;
821 if (! (now = sdb_gettime())) {
822 char errbuf[1024];
823 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
824 "time in collector main loop: %s",
825 sdb_strerror(errno, errbuf, sizeof(errbuf)));
826 now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
827 }
829 if (now < SDB_PLUGIN_CCB(obj)->ccb_next_update) {
830 interval = SDB_PLUGIN_CCB(obj)->ccb_next_update - now;
832 errno = 0;
833 while (loop->do_loop && sdb_sleep(interval, &interval)) {
834 if (errno != EINTR) {
835 char errbuf[1024];
836 sdb_log(SDB_LOG_ERR, "core: Failed to sleep "
837 "in collector main loop: %s",
838 sdb_strerror(errno, errbuf, sizeof(errbuf)));
839 return -1;
840 }
841 errno = 0;
842 }
844 if (! loop->do_loop)
845 return 0;
846 }
848 old_ctx = ctx_set(SDB_PLUGIN_CCB(obj)->ccb_ctx);
849 if (callback(SDB_PLUGIN_CCB(obj)->ccb_user_data)) {
850 /* XXX */
851 }
852 ctx_set(old_ctx);
854 interval = SDB_PLUGIN_CCB(obj)->ccb_interval;
855 if (! interval)
856 interval = loop->default_interval;
857 if (! interval) {
858 sdb_log(SDB_LOG_WARNING, "core: No interval configured "
859 "for plugin '%s'; skipping any further "
860 "iterations.", obj->name);
861 sdb_object_deref(obj);
862 continue;
863 }
865 SDB_PLUGIN_CCB(obj)->ccb_next_update += interval;
867 if (! (now = sdb_gettime())) {
868 char errbuf[1024];
869 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
870 "time in collector main loop: %s",
871 sdb_strerror(errno, errbuf, sizeof(errbuf)));
872 now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
873 }
875 if (now > SDB_PLUGIN_CCB(obj)->ccb_next_update) {
876 sdb_log(SDB_LOG_WARNING, "core: Plugin '%s' took too "
877 "long; skipping iterations to keep up.",
878 obj->name);
879 SDB_PLUGIN_CCB(obj)->ccb_next_update = now;
880 }
882 if (sdb_llist_insert_sorted(collector_list, obj,
883 plugin_cmp_next_update)) {
884 sdb_log(SDB_LOG_ERR, "core: Failed to re-insert "
885 "plugin '%s' into collector list. Unable to further "
886 "use the plugin.",
887 obj->name);
888 sdb_object_deref(obj);
889 return -1;
890 }
892 /* pass control back to the list */
893 sdb_object_deref(obj);
894 }
895 return 0;
896 } /* sdb_plugin_read_loop */
898 char *
899 sdb_plugin_cname(char *hostname)
900 {
901 sdb_llist_iter_t *iter;
903 if (! hostname)
904 return NULL;
906 if (! cname_list)
907 return hostname;
909 iter = sdb_llist_get_iter(cname_list);
910 while (sdb_llist_iter_has_next(iter)) {
911 sdb_plugin_cname_cb callback;
912 char *cname;
914 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
915 assert(obj);
917 callback = (sdb_plugin_cname_cb)SDB_PLUGIN_CB(obj)->cb_callback;
918 cname = callback(hostname, SDB_PLUGIN_CB(obj)->cb_user_data);
919 if (cname) {
920 free(hostname);
921 hostname = cname;
922 }
923 /* else: don't change hostname */
924 }
925 sdb_llist_iter_destroy(iter);
926 return hostname;
927 } /* sdb_plugin_cname */
929 int
930 sdb_plugin_log(int prio, const char *msg)
931 {
932 sdb_llist_iter_t *iter;
933 int ret = -1;
935 if (! msg)
936 return 0;
938 if (! log_list)
939 return fprintf(stderr, "[%s] %s\n", SDB_LOG_PRIO_TO_STRING(prio), msg);
941 iter = sdb_llist_get_iter(log_list);
942 while (sdb_llist_iter_has_next(iter)) {
943 sdb_plugin_log_cb callback;
944 int tmp;
946 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
947 assert(obj);
949 callback = (sdb_plugin_log_cb)SDB_PLUGIN_CB(obj)->cb_callback;
950 tmp = callback(prio, msg, SDB_PLUGIN_CB(obj)->cb_user_data);
951 if (tmp > ret)
952 ret = tmp;
953 }
954 sdb_llist_iter_destroy(iter);
955 return ret;
956 } /* sdb_plugin_log */
958 int
959 sdb_plugin_vlogf(int prio, const char *fmt, va_list ap)
960 {
961 sdb_strbuf_t *buf;
962 int ret;
964 if (! fmt)
965 return 0;
967 buf = sdb_strbuf_create(64);
968 if (! buf) {
969 ret = fprintf(stderr, "[%s] ", SDB_LOG_PRIO_TO_STRING(prio));
970 ret += vfprintf(stderr, fmt, ap);
971 return ret;
972 }
974 if (sdb_strbuf_vsprintf(buf, fmt, ap) < 0) {
975 sdb_strbuf_destroy(buf);
976 return -1;
977 }
979 ret = sdb_plugin_log(prio, sdb_strbuf_string(buf));
980 sdb_strbuf_destroy(buf);
981 return ret;
982 } /* sdb_plugin_vlogf */
984 int
985 sdb_plugin_logf(int prio, const char *fmt, ...)
986 {
987 va_list ap;
988 int ret;
990 if (! fmt)
991 return 0;
993 va_start(ap, fmt);
994 ret = sdb_plugin_vlogf(prio, fmt, ap);
995 va_end(ap);
996 return ret;
997 } /* sdb_plugin_logf */
999 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */