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 /* helper to access info attributes */
54 #define INFO_GET(i, attr) \
55 ((i)->attr ? (i)->attr : #attr" not set")
57 /*
58 * private data types
59 */
61 typedef struct {
62 sdb_object_t super;
63 sdb_plugin_ctx_t public;
65 sdb_plugin_info_t info;
66 lt_dlhandle handle;
68 /* The usage count differs from the object's ref count
69 * in that it provides higher level information about how
70 * the plugin is in use. */
71 size_t use_cnt;
72 } ctx_t;
73 #define CTX_INIT { SDB_OBJECT_INIT, \
74 SDB_PLUGIN_CTX_INIT, SDB_PLUGIN_INFO_INIT, NULL, 0 }
76 #define CTX(obj) ((ctx_t *)(obj))
78 typedef struct {
79 sdb_object_t super;
80 void *cb_callback;
81 sdb_object_t *cb_user_data;
82 ctx_t *cb_ctx;
83 } sdb_plugin_cb_t;
84 #define SDB_PLUGIN_CB_INIT { SDB_OBJECT_INIT, \
85 /* callback = */ NULL, /* user_data = */ NULL, \
86 SDB_PLUGIN_CTX_INIT }
88 typedef struct {
89 sdb_plugin_cb_t super;
90 #define ccb_callback super.cb_callback
91 #define ccb_user_data super.cb_user_data
92 #define ccb_ctx super.cb_ctx
93 sdb_time_t ccb_interval;
94 sdb_time_t ccb_next_update;
95 } sdb_plugin_collector_cb_t;
97 #define SDB_PLUGIN_CB(obj) ((sdb_plugin_cb_t *)(obj))
98 #define SDB_CONST_PLUGIN_CB(obj) ((const sdb_plugin_cb_t *)(obj))
99 #define SDB_PLUGIN_CCB(obj) ((sdb_plugin_collector_cb_t *)(obj))
100 #define SDB_CONST_PLUGIN_CCB(obj) ((const sdb_plugin_collector_cb_t *)(obj))
102 /*
103 * private variables
104 */
106 static sdb_plugin_ctx_t plugin_default_ctx = SDB_PLUGIN_CTX_INIT;
107 static sdb_plugin_info_t plugin_default_info = SDB_PLUGIN_INFO_INIT;
109 static pthread_key_t plugin_ctx_key;
110 static _Bool plugin_ctx_key_initialized = 0;
112 /* a list of the plugin contexts of all registered plugins */
113 static sdb_llist_t *all_plugins = NULL;
115 static sdb_llist_t *config_list = NULL;
116 static sdb_llist_t *init_list = NULL;
117 static sdb_llist_t *collector_list = NULL;
118 static sdb_llist_t *cname_list = NULL;
119 static sdb_llist_t *shutdown_list = NULL;
120 static sdb_llist_t *log_list = NULL;
122 static struct {
123 const char *type;
124 sdb_llist_t **list;
125 } all_lists[] = {
126 { "config", &config_list },
127 { "init", &init_list },
128 { "collector", &collector_list },
129 { "cname", &cname_list },
130 { "shutdown", &shutdown_list },
131 { "log", &log_list },
132 };
134 /*
135 * private helper functions
136 */
138 static void
139 plugin_info_clear(sdb_plugin_info_t *info)
140 {
141 sdb_plugin_info_t empty_info = SDB_PLUGIN_INFO_INIT;
142 if (! info)
143 return;
145 if (info->plugin_name)
146 free(info->plugin_name);
147 if (info->filename)
148 free(info->filename);
150 if (info->description)
151 free(info->description);
152 if (info->copyright)
153 free(info->copyright);
154 if (info->license)
155 free(info->license);
157 *info = empty_info;
158 } /* plugin_info_clear */
160 static void
161 ctx_key_init(void)
162 {
163 if (plugin_ctx_key_initialized)
164 return;
166 pthread_key_create(&plugin_ctx_key, /* destructor */ NULL);
167 plugin_ctx_key_initialized = 1;
168 } /* ctx_key_init */
170 static int
171 plugin_cmp_next_update(const sdb_object_t *a, const sdb_object_t *b)
172 {
173 const sdb_plugin_collector_cb_t *ccb1
174 = (const sdb_plugin_collector_cb_t *)a;
175 const sdb_plugin_collector_cb_t *ccb2
176 = (const sdb_plugin_collector_cb_t *)b;
178 assert(ccb1 && ccb2);
180 return (ccb1->ccb_next_update > ccb2->ccb_next_update)
181 ? 1 : (ccb1->ccb_next_update < ccb2->ccb_next_update)
182 ? -1 : 0;
183 } /* plugin_cmp_next_update */
185 static int
186 plugin_lookup_by_name(const sdb_object_t *obj, const void *id)
187 {
188 const sdb_plugin_cb_t *cb = SDB_CONST_PLUGIN_CB(obj);
189 const char *name = id;
191 assert(cb && id);
193 /* when a plugin was registered from outside a plugin (e.g. the core),
194 * we don't have a plugin context */
195 if (! cb->cb_ctx)
196 return 1;
198 if (!strcasecmp(cb->cb_ctx->info.plugin_name, name))
199 return 0;
200 return 1;
201 } /* plugin_lookup_by_name */
203 /* since this function is called from sdb_plugin_reconfigure_finish()
204 * when iterating through all_plugins, we may not do any additional
205 * modifications to all_plugins except for the optional removal */
206 static void
207 plugin_unregister_by_name(const char *plugin_name)
208 {
209 sdb_object_t *obj;
210 size_t i;
212 for (i = 0; i < SDB_STATIC_ARRAY_LEN(all_lists); ++i) {
213 const char *type = all_lists[i].type;
214 sdb_llist_t *list = *all_lists[i].list;
216 while (1) {
217 sdb_plugin_cb_t *cb;
219 cb = SDB_PLUGIN_CB(sdb_llist_remove(list,
220 plugin_lookup_by_name, plugin_name));
221 if (! cb)
222 break;
224 assert(cb->cb_ctx);
226 sdb_log(SDB_LOG_INFO, "core: Unregistering "
227 "%s callback '%s' (module %s)", type, cb->super.name,
228 cb->cb_ctx->info.plugin_name);
229 sdb_object_deref(SDB_OBJ(cb));
230 }
231 }
233 obj = sdb_llist_search_by_name(all_plugins, plugin_name);
234 /* when called from sdb_plugin_reconfigure_finish, the object has already
235 * been removed from the list */
236 if (obj && (obj->ref_cnt <= 1)) {
237 sdb_llist_remove_by_name(all_plugins, plugin_name);
238 sdb_object_deref(obj);
239 }
240 /* else: other callbacks still reference it */
241 } /* plugin_unregister_by_name */
243 static void
244 plugin_unregister_all(void)
245 {
246 size_t i;
248 for (i = 0; i < SDB_STATIC_ARRAY_LEN(all_lists); ++i) {
249 const char *type = all_lists[i].type;
250 sdb_llist_t *list = *all_lists[i].list;
252 size_t len = sdb_llist_len(list);
254 if (! len)
255 continue;
257 sdb_llist_clear(list);
258 sdb_log(SDB_LOG_INFO, "core: Unregistered %zu %s callback%s",
259 len, type, len == 1 ? "" : "s");
260 }
261 } /* plugin_unregister_all */
263 /*
264 * private types
265 */
267 static int
268 ctx_init(sdb_object_t *obj, va_list __attribute__((unused)) ap)
269 {
270 ctx_t *ctx = CTX(obj);
272 assert(ctx);
274 ctx->public = plugin_default_ctx;
275 ctx->info = plugin_default_info;
276 ctx->handle = NULL;
277 ctx->use_cnt = 1;
278 return 0;
279 } /* ctx_init */
281 static void
282 ctx_destroy(sdb_object_t *obj)
283 {
284 ctx_t *ctx = CTX(obj);
286 if (ctx->handle) {
287 const char *err;
289 sdb_log(SDB_LOG_INFO, "core: Unloading module %s",
290 ctx->info.plugin_name);
292 lt_dlerror();
293 lt_dlclose(ctx->handle);
294 if ((err = lt_dlerror()))
295 sdb_log(SDB_LOG_WARNING, "core: Failed to unload module %s: %s",
296 ctx->info.plugin_name, err);
297 }
299 plugin_info_clear(&ctx->info);
300 } /* ctx_destroy */
302 static sdb_type_t ctx_type = {
303 sizeof(ctx_t),
305 ctx_init,
306 ctx_destroy
307 };
309 static ctx_t *
310 ctx_get(void)
311 {
312 if (! plugin_ctx_key_initialized)
313 ctx_key_init();
314 return pthread_getspecific(plugin_ctx_key);
315 } /* ctx_get */
317 static ctx_t *
318 ctx_set(ctx_t *new)
319 {
320 ctx_t *old;
322 if (! plugin_ctx_key_initialized)
323 ctx_key_init();
325 old = pthread_getspecific(plugin_ctx_key);
326 if (old)
327 sdb_object_deref(SDB_OBJ(old));
328 if (new)
329 sdb_object_ref(SDB_OBJ(new));
330 pthread_setspecific(plugin_ctx_key, new);
331 return old;
332 } /* ctx_set */
334 static ctx_t *
335 ctx_create(const char *name)
336 {
337 ctx_t *ctx;
339 ctx = CTX(sdb_object_create(name, ctx_type));
340 if (! ctx)
341 return NULL;
343 if (! plugin_ctx_key_initialized)
344 ctx_key_init();
345 ctx_set(ctx);
346 return ctx;
347 } /* ctx_create */
349 static int
350 plugin_cb_init(sdb_object_t *obj, va_list ap)
351 {
352 sdb_llist_t **list = va_arg(ap, sdb_llist_t **);
353 const char *type = va_arg(ap, const char *);
354 void *callback = va_arg(ap, void *);
355 sdb_object_t *ud = va_arg(ap, sdb_object_t *);
357 assert(list);
358 assert(type);
359 assert(obj);
361 if (sdb_llist_search_by_name(*list, obj->name)) {
362 sdb_log(SDB_LOG_WARNING, "core: %s callback '%s' "
363 "has already been registered. Ignoring newly "
364 "registered version.", type, obj->name);
365 return -1;
366 }
368 /* cb_ctx may be NULL if the plugin was not registered by a plugin */
370 SDB_PLUGIN_CB(obj)->cb_callback = callback;
371 SDB_PLUGIN_CB(obj)->cb_ctx = ctx_get();
372 sdb_object_ref(SDB_OBJ(SDB_PLUGIN_CB(obj)->cb_ctx));
374 sdb_object_ref(ud);
375 SDB_PLUGIN_CB(obj)->cb_user_data = ud;
376 return 0;
377 } /* plugin_cb_init */
379 static void
380 plugin_cb_destroy(sdb_object_t *obj)
381 {
382 assert(obj);
383 sdb_object_deref(SDB_PLUGIN_CB(obj)->cb_user_data);
384 sdb_object_deref(SDB_OBJ(SDB_PLUGIN_CB(obj)->cb_ctx));
385 } /* plugin_cb_destroy */
387 static sdb_type_t sdb_plugin_cb_type = {
388 sizeof(sdb_plugin_cb_t),
390 plugin_cb_init,
391 plugin_cb_destroy
392 };
394 static sdb_type_t sdb_plugin_collector_cb_type = {
395 sizeof(sdb_plugin_collector_cb_t),
397 plugin_cb_init,
398 plugin_cb_destroy
399 };
401 static int
402 module_init(const char *name, lt_dlhandle lh, sdb_plugin_info_t *info)
403 {
404 int (*mod_init)(sdb_plugin_info_t *);
405 int status;
407 mod_init = (int (*)(sdb_plugin_info_t *))lt_dlsym(lh, "sdb_module_init");
408 if (! mod_init) {
409 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': "
410 "could not find symbol 'sdb_module_init'", name);
411 return -1;
412 }
414 status = mod_init(info);
415 if (status) {
416 sdb_log(SDB_LOG_ERR, "core: Failed to initialize "
417 "module '%s'", name);
418 plugin_unregister_by_name(name);
419 return -1;
420 }
421 return 0;
422 } /* module_init */
424 static int
425 module_load(const char *basedir, const char *name,
426 const sdb_plugin_ctx_t *plugin_ctx)
427 {
428 char base_name[name ? strlen(name) + 1 : 1];
429 const char *name_ptr;
430 char *tmp;
432 char filename[1024];
433 lt_dlhandle lh;
435 ctx_t *ctx;
437 int status;
439 assert(name);
441 base_name[0] = '\0';
442 name_ptr = name;
444 while ((tmp = strstr(name_ptr, "::"))) {
445 strncat(base_name, name_ptr, (size_t)(tmp - name_ptr));
446 strcat(base_name, "/");
447 name_ptr = tmp + strlen("::");
448 }
449 strcat(base_name, name_ptr);
451 if (! basedir)
452 basedir = PKGLIBDIR;
454 snprintf(filename, sizeof(filename), "%s/%s.so", basedir, base_name);
455 filename[sizeof(filename) - 1] = '\0';
457 if (access(filename, R_OK)) {
458 char errbuf[1024];
459 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s' (%s): %s",
460 name, filename, sdb_strerror(errno, errbuf, sizeof(errbuf)));
461 return -1;
462 }
464 lt_dlinit();
465 lt_dlerror();
467 lh = lt_dlopen(filename);
468 if (! lh) {
469 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': %s"
470 "The most common cause for this problem are missing "
471 "dependencies.\n", name, lt_dlerror());
472 return -1;
473 }
475 if (ctx_get())
476 sdb_log(SDB_LOG_WARNING, "core: Discarding old plugin context");
478 ctx = ctx_create(name);
479 if (! ctx) {
480 sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin context");
481 return -1;
482 }
484 ctx->info.plugin_name = strdup(name);
485 ctx->info.filename = strdup(filename);
486 ctx->handle = lh;
488 if (plugin_ctx)
489 ctx->public = *plugin_ctx;
491 if ((status = module_init(name, lh, &ctx->info))) {
492 sdb_object_deref(SDB_OBJ(ctx));
493 return status;
494 }
496 /* compare minor version */
497 if ((ctx->info.version < 0)
498 || ((int)(ctx->info.version / 100) != (int)(SDB_VERSION / 100)))
499 sdb_log(SDB_LOG_WARNING, "core: WARNING: version of "
500 "plugin '%s' (%i.%i.%i) does not match our version "
501 "(%i.%i.%i); this might cause problems",
502 name, SDB_VERSION_DECODE(ctx->info.version),
503 SDB_VERSION_DECODE(SDB_VERSION));
505 sdb_llist_append(all_plugins, SDB_OBJ(ctx));
507 sdb_log(SDB_LOG_INFO, "core: Successfully loaded "
508 "plugin %s v%i (%s)", ctx->info.plugin_name,
509 ctx->info.plugin_version,
510 INFO_GET(&ctx->info, description));
511 sdb_log(SDB_LOG_INFO, "core: Plugin %s: %s, License: %s",
512 ctx->info.plugin_name,
513 INFO_GET(&ctx->info, copyright),
514 INFO_GET(&ctx->info, license));
516 /* any registered callbacks took ownership of the context */
517 sdb_object_deref(SDB_OBJ(ctx));
519 /* reset */
520 ctx_set(NULL);
521 return 0;
522 } /* module_load */
524 static char *
525 plugin_get_name(const char *name, char *buf, size_t bufsize)
526 {
527 ctx_t *ctx = ctx_get();
529 if (ctx)
530 snprintf(buf, bufsize, "%s::%s", ctx->info.plugin_name, name);
531 else
532 snprintf(buf, bufsize, "core::%s", name);
533 return buf;
534 } /* plugin_get_name */
536 static int
537 plugin_add_callback(sdb_llist_t **list, const char *type,
538 const char *name, void *callback, sdb_object_t *user_data)
539 {
540 sdb_object_t *obj;
542 if ((! name) || (! callback))
543 return -1;
545 assert(list);
547 if (! *list)
548 *list = sdb_llist_create();
549 if (! *list)
550 return -1;
552 obj = sdb_object_create(name, sdb_plugin_cb_type,
553 list, type, callback, user_data);
554 if (! obj)
555 return -1;
557 if (sdb_llist_append(*list, obj)) {
558 sdb_object_deref(obj);
559 return -1;
560 }
562 /* pass control to the list */
563 sdb_object_deref(obj);
565 sdb_log(SDB_LOG_INFO, "core: Registered %s callback '%s'.",
566 type, name);
567 return 0;
568 } /* plugin_add_callback */
570 /*
571 * public API
572 */
574 int
575 sdb_plugin_load(const char *basedir, const char *name,
576 const sdb_plugin_ctx_t *plugin_ctx)
577 {
578 ctx_t *ctx;
580 int status;
582 if ((! name) || (! *name))
583 return -1;
585 if (! all_plugins) {
586 if (! (all_plugins = sdb_llist_create())) {
587 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': "
588 "internal error while creating linked list", name);
589 return -1;
590 }
591 }
593 ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
594 if (ctx) {
595 /* plugin already loaded */
596 if (! ctx->use_cnt) {
597 /* reloading plugin */
598 ctx_t *old_ctx = ctx_set(ctx);
600 status = module_init(ctx->info.plugin_name, ctx->handle, NULL);
601 if (status)
602 return status;
604 sdb_log(SDB_LOG_INFO, "core: Successfully reloaded plugin "
605 "'%s' (%s)", ctx->info.plugin_name,
606 INFO_GET(&ctx->info, description));
607 ctx_set(old_ctx);
608 }
609 ++ctx->use_cnt;
610 return 0;
611 }
613 return module_load(basedir, name, plugin_ctx);
614 } /* sdb_plugin_load */
616 int
617 sdb_plugin_set_info(sdb_plugin_info_t *info, int type, ...)
618 {
619 va_list ap;
621 if (! info)
622 return -1;
624 va_start(ap, type);
626 switch (type) {
627 case SDB_PLUGIN_INFO_DESC:
628 {
629 char *desc = va_arg(ap, char *);
630 if (desc) {
631 if (info->description)
632 free(info->description);
633 info->description = strdup(desc);
634 }
635 }
636 break;
637 case SDB_PLUGIN_INFO_COPYRIGHT:
638 {
639 char *copyright = va_arg(ap, char *);
640 if (copyright)
641 info->copyright = strdup(copyright);
642 }
643 break;
644 case SDB_PLUGIN_INFO_LICENSE:
645 {
646 char *license = va_arg(ap, char *);
647 if (license) {
648 if (info->license)
649 free(info->license);
650 info->license = strdup(license);
651 }
652 }
653 break;
654 case SDB_PLUGIN_INFO_VERSION:
655 {
656 int version = va_arg(ap, int);
657 info->version = version;
658 }
659 break;
660 case SDB_PLUGIN_INFO_PLUGIN_VERSION:
661 {
662 int version = va_arg(ap, int);
663 info->plugin_version = version;
664 }
665 break;
666 default:
667 va_end(ap);
668 return -1;
669 }
671 va_end(ap);
672 return 0;
673 } /* sdb_plugin_set_info */
675 int
676 sdb_plugin_register_config(sdb_plugin_config_cb callback)
677 {
678 ctx_t *ctx = ctx_get();
680 if (! ctx) {
681 sdb_log(SDB_LOG_ERR, "core: Invalid attempt to register a "
682 "config callback from outside a plugin");
683 return -1;
684 }
685 return plugin_add_callback(&config_list, "init", ctx->info.plugin_name,
686 (void *)callback, NULL);
687 } /* sdb_plugin_register_config */
689 int
690 sdb_plugin_register_init(const char *name, sdb_plugin_init_cb callback,
691 sdb_object_t *user_data)
692 {
693 char cb_name[1024];
694 return plugin_add_callback(&init_list, "init",
695 plugin_get_name(name, cb_name, sizeof(cb_name)),
696 (void *)callback, user_data);
697 } /* sdb_plugin_register_init */
699 int
700 sdb_plugin_register_shutdown(const char *name, sdb_plugin_shutdown_cb callback,
701 sdb_object_t *user_data)
702 {
703 char cb_name[1024];
704 return plugin_add_callback(&shutdown_list, "shutdown",
705 plugin_get_name(name, cb_name, sizeof(cb_name)),
706 (void *)callback, user_data);
707 } /* sdb_plugin_register_shutdown */
709 int
710 sdb_plugin_register_log(const char *name, sdb_plugin_log_cb callback,
711 sdb_object_t *user_data)
712 {
713 char cb_name[1024];
714 return plugin_add_callback(&log_list, "log",
715 plugin_get_name(name, cb_name, sizeof(cb_name)),
716 callback, user_data);
717 } /* sdb_plugin_register_log */
719 int
720 sdb_plugin_register_cname(const char *name, sdb_plugin_cname_cb callback,
721 sdb_object_t *user_data)
722 {
723 char cb_name[1024];
724 return plugin_add_callback(&cname_list, "cname",
725 plugin_get_name(name, cb_name, sizeof(cb_name)),
726 callback, user_data);
727 } /* sdb_plugin_register_cname */
729 int
730 sdb_plugin_register_collector(const char *name, sdb_plugin_collector_cb callback,
731 const sdb_time_t *interval, sdb_object_t *user_data)
732 {
733 char cb_name[1024];
734 sdb_object_t *obj;
736 if ((! name) || (! callback))
737 return -1;
739 if (! collector_list)
740 collector_list = sdb_llist_create();
741 if (! collector_list)
742 return -1;
744 plugin_get_name(name, cb_name, sizeof(cb_name));
746 obj = sdb_object_create(cb_name, sdb_plugin_collector_cb_type,
747 &collector_list, "collector", callback, user_data);
748 if (! obj)
749 return -1;
751 if (interval)
752 SDB_PLUGIN_CCB(obj)->ccb_interval = *interval;
753 else {
754 sdb_time_t tmp = sdb_plugin_get_ctx().interval;
756 if (tmp > 0)
757 SDB_PLUGIN_CCB(obj)->ccb_interval = tmp;
758 else
759 SDB_PLUGIN_CCB(obj)->ccb_interval = 0;
760 }
762 if (! (SDB_PLUGIN_CCB(obj)->ccb_next_update = sdb_gettime())) {
763 char errbuf[1024];
764 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
765 "time: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
766 sdb_object_deref(obj);
767 return -1;
768 }
770 if (sdb_llist_insert_sorted(collector_list, obj,
771 plugin_cmp_next_update)) {
772 sdb_object_deref(obj);
773 return -1;
774 }
776 /* pass control to the list */
777 sdb_object_deref(obj);
779 sdb_log(SDB_LOG_INFO, "core: Registered collector callback '%s' "
780 "(interval = %.3fs).", cb_name,
781 SDB_TIME_TO_DOUBLE(SDB_PLUGIN_CCB(obj)->ccb_interval));
782 return 0;
783 } /* sdb_plugin_register_collector */
785 sdb_plugin_ctx_t
786 sdb_plugin_get_ctx(void)
787 {
788 ctx_t *c;
790 c = ctx_get();
791 if (! c) {
792 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid read access to plugin "
793 "context outside a plugin");
794 return plugin_default_ctx;
795 }
796 return c->public;
797 } /* sdb_plugin_get_ctx */
799 int
800 sdb_plugin_set_ctx(sdb_plugin_ctx_t ctx, sdb_plugin_ctx_t *old)
801 {
802 ctx_t *c;
804 c = ctx_get();
805 if (! c) {
806 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid write access to plugin "
807 "context outside a plugin");
808 return -1;
809 }
811 if (old)
812 *old = c->public;
813 c->public = ctx;
814 return 0;
815 } /* sdb_plugin_set_ctx */
817 const sdb_plugin_info_t *
818 sdb_plugin_current(void)
819 {
820 ctx_t *ctx = ctx_get();
822 if (! ctx)
823 return NULL;
824 return &ctx->info;
825 } /* sdb_plugin_current */
827 int
828 sdb_plugin_configure(const char *name, oconfig_item_t *ci)
829 {
830 sdb_plugin_cb_t *plugin;
831 sdb_plugin_config_cb callback;
833 ctx_t *old_ctx;
835 int status;
837 if ((! name) || (! ci))
838 return -1;
840 plugin = SDB_PLUGIN_CB(sdb_llist_search_by_name(config_list, name));
841 if (! plugin) {
842 ctx_t *ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
843 if (! ctx)
844 sdb_log(SDB_LOG_ERR, "core: Cannot configure unknown "
845 "plugin '%s'. Missing 'LoadPlugin \"%s\"'?",
846 name, name);
847 else
848 sdb_log(SDB_LOG_ERR, "core: Plugin '%s' did not register "
849 "a config callback.", name);
850 errno = ENOENT;
851 return -1;
852 }
854 old_ctx = ctx_set(plugin->cb_ctx);
855 callback = (sdb_plugin_config_cb)plugin->cb_callback;
856 status = callback(ci);
857 ctx_set(old_ctx);
858 return status;
859 } /* sdb_plugin_configure */
861 int
862 sdb_plugin_reconfigure_init(void)
863 {
864 sdb_llist_iter_t *iter;
866 iter = sdb_llist_get_iter(config_list);
867 if (config_list && (! iter))
868 return -1;
870 /* deconfigure all plugins */
871 while (sdb_llist_iter_has_next(iter)) {
872 sdb_plugin_cb_t *plugin;
873 sdb_plugin_config_cb callback;
874 ctx_t *old_ctx;
876 plugin = SDB_PLUGIN_CB(sdb_llist_iter_get_next(iter));
877 old_ctx = ctx_set(plugin->cb_ctx);
878 callback = (sdb_plugin_config_cb)plugin->cb_callback;
879 callback(NULL);
880 ctx_set(old_ctx);
881 }
882 sdb_llist_iter_destroy(iter);
884 iter = sdb_llist_get_iter(all_plugins);
885 if (all_plugins && (! iter))
886 return -1;
888 /* record all plugins as being unused */
889 while (sdb_llist_iter_has_next(iter))
890 CTX(sdb_llist_iter_get_next(iter))->use_cnt = 0;
891 sdb_llist_iter_destroy(iter);
893 plugin_unregister_all();
894 return 0;
895 } /* sdb_plugin_reconfigure_init */
897 int
898 sdb_plugin_reconfigure_finish(void)
899 {
900 sdb_llist_iter_t *iter;
902 iter = sdb_llist_get_iter(all_plugins);
903 if (all_plugins && (! iter))
904 return -1;
906 while (sdb_llist_iter_has_next(iter)) {
907 ctx_t *ctx = CTX(sdb_llist_iter_get_next(iter));
908 if (ctx->use_cnt)
909 continue;
911 sdb_log(SDB_LOG_INFO, "core: Module %s no longer in use",
912 ctx->info.plugin_name);
913 sdb_llist_iter_remove_current(iter);
914 plugin_unregister_by_name(ctx->info.plugin_name);
915 sdb_object_deref(SDB_OBJ(ctx));
916 }
917 sdb_llist_iter_destroy(iter);
918 return 0;
919 } /* sdb_plugin_reconfigure_finish */
921 int
922 sdb_plugin_init_all(void)
923 {
924 sdb_llist_iter_t *iter;
925 int ret = 0;
927 iter = sdb_llist_get_iter(init_list);
928 while (sdb_llist_iter_has_next(iter)) {
929 sdb_plugin_cb_t *cb;
930 sdb_plugin_init_cb callback;
931 ctx_t *old_ctx;
933 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
934 assert(obj);
935 cb = SDB_PLUGIN_CB(obj);
937 callback = (sdb_plugin_init_cb)cb->cb_callback;
939 old_ctx = ctx_set(cb->cb_ctx);
940 if (callback(cb->cb_user_data)) {
941 sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin "
942 "'%s'. Unregistering all callbacks.", obj->name);
943 ctx_set(old_ctx);
944 plugin_unregister_by_name(cb->cb_ctx->info.plugin_name);
945 ++ret;
946 }
947 else
948 ctx_set(old_ctx);
949 }
950 sdb_llist_iter_destroy(iter);
951 return ret;
952 } /* sdb_plugin_init_all */
954 int
955 sdb_plugin_shutdown_all(void)
956 {
957 sdb_llist_iter_t *iter;
958 int ret = 0;
960 iter = sdb_llist_get_iter(shutdown_list);
961 while (sdb_llist_iter_has_next(iter)) {
962 sdb_plugin_cb_t *cb;
963 sdb_plugin_shutdown_cb callback;
964 ctx_t *old_ctx;
966 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
967 assert(obj);
968 cb = SDB_PLUGIN_CB(obj);
970 callback = (sdb_plugin_shutdown_cb)cb->cb_callback;
972 old_ctx = ctx_set(cb->cb_ctx);
973 if (callback(cb->cb_user_data)) {
974 sdb_log(SDB_LOG_ERR, "core: Failed to shutdown plugin '%s'.",
975 obj->name);
976 ++ret;
977 }
978 ctx_set(old_ctx);
979 }
980 sdb_llist_iter_destroy(iter);
981 return ret;
982 } /* sdb_plugin_shutdown_all */
984 int
985 sdb_plugin_collector_loop(sdb_plugin_loop_t *loop)
986 {
987 if (! collector_list) {
988 sdb_log(SDB_LOG_WARNING, "core: No collectors registered. "
989 "Quiting main loop.");
990 return -1;
991 }
993 if (! loop)
994 return -1;
996 while (loop->do_loop) {
997 sdb_plugin_collector_cb callback;
998 ctx_t *old_ctx;
1000 sdb_time_t interval, now;
1002 sdb_object_t *obj = sdb_llist_shift(collector_list);
1003 if (! obj)
1004 return -1;
1006 callback = (sdb_plugin_collector_cb)SDB_PLUGIN_CCB(obj)->ccb_callback;
1008 if (! (now = sdb_gettime())) {
1009 char errbuf[1024];
1010 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
1011 "time in collector main loop: %s",
1012 sdb_strerror(errno, errbuf, sizeof(errbuf)));
1013 now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
1014 }
1016 if (now < SDB_PLUGIN_CCB(obj)->ccb_next_update) {
1017 interval = SDB_PLUGIN_CCB(obj)->ccb_next_update - now;
1019 errno = 0;
1020 while (loop->do_loop && sdb_sleep(interval, &interval)) {
1021 if (errno != EINTR) {
1022 char errbuf[1024];
1023 sdb_log(SDB_LOG_ERR, "core: Failed to sleep "
1024 "in collector main loop: %s",
1025 sdb_strerror(errno, errbuf, sizeof(errbuf)));
1026 sdb_llist_insert_sorted(collector_list, obj,
1027 plugin_cmp_next_update);
1028 sdb_object_deref(obj);
1029 return -1;
1030 }
1031 errno = 0;
1032 }
1034 if (! loop->do_loop) {
1035 /* put back; don't worry about errors */
1036 sdb_llist_insert_sorted(collector_list, obj,
1037 plugin_cmp_next_update);
1038 sdb_object_deref(obj);
1039 return 0;
1040 }
1041 }
1043 old_ctx = ctx_set(SDB_PLUGIN_CCB(obj)->ccb_ctx);
1044 if (callback(SDB_PLUGIN_CCB(obj)->ccb_user_data)) {
1045 /* XXX */
1046 }
1047 ctx_set(old_ctx);
1049 interval = SDB_PLUGIN_CCB(obj)->ccb_interval;
1050 if (! interval)
1051 interval = loop->default_interval;
1052 if (! interval) {
1053 sdb_log(SDB_LOG_WARNING, "core: No interval configured "
1054 "for plugin '%s'; skipping any further "
1055 "iterations.", obj->name);
1056 sdb_object_deref(obj);
1057 continue;
1058 }
1060 SDB_PLUGIN_CCB(obj)->ccb_next_update += interval;
1062 if (! (now = sdb_gettime())) {
1063 char errbuf[1024];
1064 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
1065 "time in collector main loop: %s",
1066 sdb_strerror(errno, errbuf, sizeof(errbuf)));
1067 now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
1068 }
1070 if (now > SDB_PLUGIN_CCB(obj)->ccb_next_update) {
1071 sdb_log(SDB_LOG_WARNING, "core: Plugin '%s' took too "
1072 "long; skipping iterations to keep up.",
1073 obj->name);
1074 SDB_PLUGIN_CCB(obj)->ccb_next_update = now;
1075 }
1077 if (sdb_llist_insert_sorted(collector_list, obj,
1078 plugin_cmp_next_update)) {
1079 sdb_log(SDB_LOG_ERR, "core: Failed to re-insert "
1080 "plugin '%s' into collector list. Unable to further "
1081 "use the plugin.",
1082 obj->name);
1083 sdb_object_deref(obj);
1084 return -1;
1085 }
1087 /* pass control back to the list */
1088 sdb_object_deref(obj);
1089 }
1090 return 0;
1091 } /* sdb_plugin_read_loop */
1093 char *
1094 sdb_plugin_cname(char *hostname)
1095 {
1096 sdb_llist_iter_t *iter;
1098 if (! hostname)
1099 return NULL;
1101 if (! cname_list)
1102 return hostname;
1104 iter = sdb_llist_get_iter(cname_list);
1105 while (sdb_llist_iter_has_next(iter)) {
1106 sdb_plugin_cname_cb callback;
1107 char *cname;
1109 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
1110 assert(obj);
1112 callback = (sdb_plugin_cname_cb)SDB_PLUGIN_CB(obj)->cb_callback;
1113 cname = callback(hostname, SDB_PLUGIN_CB(obj)->cb_user_data);
1114 if (cname) {
1115 free(hostname);
1116 hostname = cname;
1117 }
1118 /* else: don't change hostname */
1119 }
1120 sdb_llist_iter_destroy(iter);
1121 return hostname;
1122 } /* sdb_plugin_cname */
1124 int
1125 sdb_plugin_log(int prio, const char *msg)
1126 {
1127 sdb_llist_iter_t *iter;
1128 int ret = -1;
1130 _Bool logged = 0;
1132 if (! msg)
1133 return 0;
1135 iter = sdb_llist_get_iter(log_list);
1136 while (sdb_llist_iter_has_next(iter)) {
1137 sdb_plugin_log_cb callback;
1138 int tmp;
1140 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
1141 assert(obj);
1143 callback = (sdb_plugin_log_cb)SDB_PLUGIN_CB(obj)->cb_callback;
1144 tmp = callback(prio, msg, SDB_PLUGIN_CB(obj)->cb_user_data);
1145 if (tmp > ret)
1146 ret = tmp;
1148 if (SDB_PLUGIN_CB(obj)->cb_ctx)
1149 logged = 1;
1150 /* else: this is an internally registered callback */
1151 }
1152 sdb_llist_iter_destroy(iter);
1154 if (! logged)
1155 return fprintf(stderr, "[%s] %s\n", SDB_LOG_PRIO_TO_STRING(prio), msg);
1156 return ret;
1157 } /* sdb_plugin_log */
1159 int
1160 sdb_plugin_vlogf(int prio, const char *fmt, va_list ap)
1161 {
1162 sdb_strbuf_t *buf;
1163 int ret;
1165 if (! fmt)
1166 return 0;
1168 buf = sdb_strbuf_create(64);
1169 if (! buf) {
1170 ret = fprintf(stderr, "[%s] ", SDB_LOG_PRIO_TO_STRING(prio));
1171 ret += vfprintf(stderr, fmt, ap);
1172 return ret;
1173 }
1175 if (sdb_strbuf_vsprintf(buf, fmt, ap) < 0) {
1176 sdb_strbuf_destroy(buf);
1177 return -1;
1178 }
1180 ret = sdb_plugin_log(prio, sdb_strbuf_string(buf));
1181 sdb_strbuf_destroy(buf);
1182 return ret;
1183 } /* sdb_plugin_vlogf */
1185 int
1186 sdb_plugin_logf(int prio, const char *fmt, ...)
1187 {
1188 va_list ap;
1189 int ret;
1191 if (! fmt)
1192 return 0;
1194 va_start(ap, fmt);
1195 ret = sdb_plugin_vlogf(prio, fmt, ap);
1196 va_end(ap);
1197 return ret;
1198 } /* sdb_plugin_logf */
1200 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */