32966deabcf8042dbf58e120016419833a05dde0
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 static struct {
141 const char *type;
142 sdb_llist_t **list;
143 } all_lists[] = {
144 { "config", &config_list },
145 { "init", &init_list },
146 { "collector", &collector_list },
147 { "cname", &cname_list },
148 { "shutdown", &shutdown_list },
149 { "log", &log_list },
150 };
152 /*
153 * private helper functions
154 */
156 static void
157 plugin_info_clear(sdb_plugin_info_t *info)
158 {
159 sdb_plugin_info_t empty_info = SDB_PLUGIN_INFO_INIT;
160 if (! info)
161 return;
163 if (info->plugin_name)
164 free(info->plugin_name);
165 if (info->filename)
166 free(info->filename);
168 if (info->name)
169 free(info->name);
170 if (info->description)
171 free(info->description);
172 if (info->copyright)
173 free(info->copyright);
174 if (info->license)
175 free(info->license);
177 *info = empty_info;
178 } /* plugin_info_clear */
180 static void
181 ctx_key_init(void)
182 {
183 if (plugin_ctx_key_initialized)
184 return;
186 pthread_key_create(&plugin_ctx_key, /* destructor */ NULL);
187 plugin_ctx_key_initialized = 1;
188 } /* ctx_key_init */
190 static int
191 plugin_cmp_next_update(const sdb_object_t *a, const sdb_object_t *b)
192 {
193 const sdb_plugin_collector_cb_t *ccb1
194 = (const sdb_plugin_collector_cb_t *)a;
195 const sdb_plugin_collector_cb_t *ccb2
196 = (const sdb_plugin_collector_cb_t *)b;
198 assert(ccb1 && ccb2);
200 return (ccb1->ccb_next_update > ccb2->ccb_next_update)
201 ? 1 : (ccb1->ccb_next_update < ccb2->ccb_next_update)
202 ? -1 : 0;
203 } /* plugin_cmp_next_update */
205 static int
206 plugin_lookup_by_name(const sdb_object_t *obj, const void *id)
207 {
208 const sdb_plugin_cb_t *cb = SDB_CONST_PLUGIN_CB(obj);
209 const char *name = id;
211 assert(cb && id);
213 /* when a plugin was registered from outside a plugin (e.g. the core),
214 * we don't have a plugin context */
215 if (! cb->cb_ctx)
216 return 1;
218 if (!strcasecmp(cb->cb_ctx->info.plugin_name, name))
219 return 0;
220 return 1;
221 } /* plugin_lookup_by_name */
223 /* since this function is called from sdb_plugin_reconfigure_finish()
224 * when iterating through all_plugins, we may not do any additional
225 * modifications to all_plugins except for the optional removal */
226 static void
227 plugin_unregister_by_name(const char *plugin_name)
228 {
229 sdb_object_t *obj;
230 size_t i;
232 for (i = 0; i < SDB_STATIC_ARRAY_LEN(all_lists); ++i) {
233 const char *type = all_lists[i].type;
234 sdb_llist_t *list = *all_lists[i].list;
236 while (1) {
237 sdb_plugin_cb_t *cb;
239 cb = SDB_PLUGIN_CB(sdb_llist_remove(list,
240 plugin_lookup_by_name, plugin_name));
241 if (! cb)
242 break;
244 assert(cb->cb_ctx);
246 sdb_log(SDB_LOG_INFO, "core: Unregistering "
247 "%s callback '%s' (module %s)", type, cb->super.name,
248 cb->cb_ctx->info.plugin_name);
249 sdb_object_deref(SDB_OBJ(cb));
250 }
251 }
253 obj = sdb_llist_search_by_name(all_plugins, plugin_name);
254 /* when called from sdb_plugin_reconfigure_finish, the object has already
255 * been removed from the list */
256 if (obj && (obj->ref_cnt <= 1)) {
257 sdb_llist_remove_by_name(all_plugins, plugin_name);
258 sdb_object_deref(obj);
259 }
260 /* else: other callbacks still reference it */
261 } /* plugin_unregister_by_name */
263 static void
264 plugin_unregister_all(void)
265 {
266 size_t i;
268 for (i = 0; i < SDB_STATIC_ARRAY_LEN(all_lists); ++i) {
269 const char *type = all_lists[i].type;
270 sdb_llist_t *list = *all_lists[i].list;
272 size_t len = sdb_llist_len(list);
274 if (! len)
275 continue;
277 sdb_llist_clear(list);
278 sdb_log(SDB_LOG_INFO, "core: Unregistered %zu %s callback%s",
279 len, type, len == 1 ? "" : "s");
280 }
281 } /* plugin_unregister_all */
283 /*
284 * private types
285 */
287 static int
288 ctx_init(sdb_object_t *obj, va_list __attribute__((unused)) ap)
289 {
290 ctx_t *ctx = CTX(obj);
292 assert(ctx);
294 ctx->public = plugin_default_ctx;
295 ctx->info = plugin_default_info;
296 ctx->handle = NULL;
297 ctx->use_cnt = 1;
298 return 0;
299 } /* ctx_init */
301 static void
302 ctx_destroy(sdb_object_t *obj)
303 {
304 ctx_t *ctx = CTX(obj);
306 if (ctx->handle) {
307 const char *err;
309 sdb_log(SDB_LOG_INFO, "core: Unloading module %s",
310 ctx->info.plugin_name);
312 lt_dlerror();
313 lt_dlclose(ctx->handle);
314 if ((err = lt_dlerror()))
315 sdb_log(SDB_LOG_WARNING, "core: Failed to unload module %s: %s",
316 ctx->info.plugin_name, err);
317 }
319 plugin_info_clear(&ctx->info);
320 } /* ctx_destroy */
322 static sdb_type_t ctx_type = {
323 sizeof(ctx_t),
325 ctx_init,
326 ctx_destroy
327 };
329 static ctx_t *
330 ctx_get(void)
331 {
332 if (! plugin_ctx_key_initialized)
333 ctx_key_init();
334 return pthread_getspecific(plugin_ctx_key);
335 } /* ctx_get */
337 static ctx_t *
338 ctx_set(ctx_t *new)
339 {
340 ctx_t *old;
342 if (! plugin_ctx_key_initialized)
343 ctx_key_init();
345 old = pthread_getspecific(plugin_ctx_key);
346 if (old)
347 sdb_object_deref(SDB_OBJ(old));
348 if (new)
349 sdb_object_ref(SDB_OBJ(new));
350 pthread_setspecific(plugin_ctx_key, new);
351 return old;
352 } /* ctx_set */
354 static ctx_t *
355 ctx_create(const char *name)
356 {
357 ctx_t *ctx;
359 ctx = CTX(sdb_object_create(name, ctx_type));
360 if (! ctx)
361 return NULL;
363 if (! plugin_ctx_key_initialized)
364 ctx_key_init();
365 ctx_set(ctx);
366 return ctx;
367 } /* ctx_create */
369 static int
370 plugin_cb_init(sdb_object_t *obj, va_list ap)
371 {
372 sdb_llist_t **list = va_arg(ap, sdb_llist_t **);
373 const char *type = va_arg(ap, const char *);
374 void *callback = va_arg(ap, void *);
375 sdb_object_t *ud = va_arg(ap, sdb_object_t *);
377 assert(list);
378 assert(type);
379 assert(obj);
381 if (sdb_llist_search_by_name(*list, obj->name)) {
382 sdb_log(SDB_LOG_WARNING, "core: %s callback '%s' "
383 "has already been registered. Ignoring newly "
384 "registered version.", type, obj->name);
385 return -1;
386 }
388 /* cb_ctx may be NULL if the plugin was not registered by a plugin */
390 SDB_PLUGIN_CB(obj)->cb_callback = callback;
391 SDB_PLUGIN_CB(obj)->cb_ctx = ctx_get();
392 sdb_object_ref(SDB_OBJ(SDB_PLUGIN_CB(obj)->cb_ctx));
394 sdb_object_ref(ud);
395 SDB_PLUGIN_CB(obj)->cb_user_data = ud;
396 return 0;
397 } /* plugin_cb_init */
399 static void
400 plugin_cb_destroy(sdb_object_t *obj)
401 {
402 assert(obj);
403 sdb_object_deref(SDB_PLUGIN_CB(obj)->cb_user_data);
404 sdb_object_deref(SDB_OBJ(SDB_PLUGIN_CB(obj)->cb_ctx));
405 } /* plugin_cb_destroy */
407 static sdb_type_t sdb_plugin_cb_type = {
408 sizeof(sdb_plugin_cb_t),
410 plugin_cb_init,
411 plugin_cb_destroy
412 };
414 static sdb_type_t sdb_plugin_collector_cb_type = {
415 sizeof(sdb_plugin_collector_cb_t),
417 plugin_cb_init,
418 plugin_cb_destroy
419 };
421 static int
422 module_init(const char *name, lt_dlhandle lh, sdb_plugin_info_t *info)
423 {
424 int (*mod_init)(sdb_plugin_info_t *);
425 int status;
427 mod_init = (int (*)(sdb_plugin_info_t *))lt_dlsym(lh, "sdb_module_init");
428 if (! mod_init) {
429 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': "
430 "could not find symbol 'sdb_module_init'", name);
431 return -1;
432 }
434 status = mod_init(info);
435 if (status) {
436 sdb_log(SDB_LOG_ERR, "core: Failed to initialize "
437 "module '%s'", name);
438 plugin_unregister_by_name(name);
439 return -1;
440 }
441 return 0;
442 } /* module_init */
444 static int
445 module_load(const char *basedir, const char *name,
446 const sdb_plugin_ctx_t *plugin_ctx)
447 {
448 char base_name[name ? strlen(name) + 1 : 1];
449 const char *name_ptr;
450 char *tmp;
452 char filename[1024];
453 lt_dlhandle lh;
455 ctx_t *ctx;
457 int status;
459 assert(name);
461 base_name[0] = '\0';
462 name_ptr = name;
464 while ((tmp = strstr(name_ptr, "::"))) {
465 strncat(base_name, name_ptr, (size_t)(tmp - name_ptr));
466 strcat(base_name, "/");
467 name_ptr = tmp + strlen("::");
468 }
469 strcat(base_name, name_ptr);
471 if (! basedir)
472 basedir = PKGLIBDIR;
474 snprintf(filename, sizeof(filename), "%s/%s.so", basedir, base_name);
475 filename[sizeof(filename) - 1] = '\0';
477 if (access(filename, R_OK)) {
478 char errbuf[1024];
479 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s' (%s): %s",
480 name, filename, sdb_strerror(errno, errbuf, sizeof(errbuf)));
481 return -1;
482 }
484 lt_dlinit();
485 lt_dlerror();
487 lh = lt_dlopen(filename);
488 if (! lh) {
489 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': %s"
490 "The most common cause for this problem are missing "
491 "dependencies.\n", name, lt_dlerror());
492 return -1;
493 }
495 if (ctx_get())
496 sdb_log(SDB_LOG_WARNING, "core: Discarding old plugin context");
498 ctx = ctx_create(name);
499 if (! ctx) {
500 sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin context");
501 return -1;
502 }
504 ctx->info.plugin_name = strdup(name);
505 ctx->info.filename = strdup(filename);
506 ctx->handle = lh;
508 if (plugin_ctx)
509 ctx->public = *plugin_ctx;
511 if ((status = module_init(name, lh, &ctx->info))) {
512 sdb_object_deref(SDB_OBJ(ctx));
513 return status;
514 }
516 /* compare minor version */
517 if ((ctx->info.version < 0)
518 || ((int)(ctx->info.version / 100) != (int)(SDB_VERSION / 100)))
519 sdb_log(SDB_LOG_WARNING, "core: WARNING: version of "
520 "plugin '%s' (%i.%i.%i) does not match our version "
521 "(%i.%i.%i); this might cause problems",
522 name, SDB_VERSION_DECODE(ctx->info.version),
523 SDB_VERSION_DECODE(SDB_VERSION));
525 sdb_llist_append(all_plugins, SDB_OBJ(ctx));
527 sdb_log(SDB_LOG_INFO, "core: Successfully loaded "
528 "plugin '%s' v%i (%s)\n\t%s\n\tLicense: %s",
529 INFO_GET(&ctx->info, name), ctx->info.plugin_version,
530 INFO_GET(&ctx->info, description),
531 INFO_GET(&ctx->info, copyright),
532 INFO_GET(&ctx->info, license));
534 /* any registered callbacks took ownership of the context */
535 sdb_object_deref(SDB_OBJ(ctx));
537 /* reset */
538 ctx_set(NULL);
539 return 0;
540 } /* module_load */
542 static char *
543 plugin_get_name(const char *name, char *buf, size_t bufsize)
544 {
545 ctx_t *ctx = ctx_get();
547 if (ctx)
548 snprintf(buf, bufsize, "%s::%s", ctx->info.plugin_name, name);
549 else
550 snprintf(buf, bufsize, "core::%s", name);
551 return buf;
552 } /* plugin_get_name */
554 static int
555 plugin_add_callback(sdb_llist_t **list, const char *type,
556 const char *name, void *callback, sdb_object_t *user_data)
557 {
558 sdb_object_t *obj;
560 if ((! name) || (! callback))
561 return -1;
563 assert(list);
565 if (! *list)
566 *list = sdb_llist_create();
567 if (! *list)
568 return -1;
570 obj = sdb_object_create(name, sdb_plugin_cb_type,
571 list, type, callback, user_data);
572 if (! obj)
573 return -1;
575 if (sdb_llist_append(*list, obj)) {
576 sdb_object_deref(obj);
577 return -1;
578 }
580 /* pass control to the list */
581 sdb_object_deref(obj);
583 sdb_log(SDB_LOG_INFO, "core: Registered %s callback '%s'.",
584 type, name);
585 return 0;
586 } /* plugin_add_callback */
588 /*
589 * public API
590 */
592 int
593 sdb_plugin_load(const char *basedir, const char *name,
594 const sdb_plugin_ctx_t *plugin_ctx)
595 {
596 ctx_t *ctx;
598 int status;
600 if ((! name) || (! *name))
601 return -1;
603 if (! all_plugins) {
604 if (! (all_plugins = sdb_llist_create())) {
605 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': "
606 "internal error while creating linked list", name);
607 return -1;
608 }
609 }
611 ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
612 if (ctx) {
613 /* plugin already loaded */
614 if (! ctx->use_cnt) {
615 /* reloading plugin */
616 ctx_t *old_ctx = ctx_set(ctx);
618 status = module_init(ctx->info.plugin_name, ctx->handle, NULL);
619 if (status)
620 return status;
622 sdb_log(SDB_LOG_INFO, "core: Successfully reloaded plugin "
623 "'%s' (%s)", INFO_GET(&ctx->info, name),
624 INFO_GET(&ctx->info, description));
625 ctx_set(old_ctx);
626 }
627 ++ctx->use_cnt;
628 return 0;
629 }
631 return module_load(basedir, name, plugin_ctx);
632 } /* sdb_plugin_load */
634 int
635 sdb_plugin_set_info(sdb_plugin_info_t *info, int type, ...)
636 {
637 va_list ap;
639 if (! info)
640 return -1;
642 va_start(ap, type);
644 switch (type) {
645 case SDB_PLUGIN_INFO_NAME:
646 {
647 char *name = va_arg(ap, char *);
648 if (name) {
649 if (info->name)
650 free(info->name);
651 info->name = strdup(name);
652 }
653 }
654 break;
655 case SDB_PLUGIN_INFO_DESC:
656 {
657 char *desc = va_arg(ap, char *);
658 if (desc) {
659 if (info->description)
660 free(info->description);
661 info->description = strdup(desc);
662 }
663 }
664 break;
665 case SDB_PLUGIN_INFO_COPYRIGHT:
666 {
667 char *copyright = va_arg(ap, char *);
668 if (copyright)
669 info->copyright = strdup(copyright);
670 }
671 break;
672 case SDB_PLUGIN_INFO_LICENSE:
673 {
674 char *license = va_arg(ap, char *);
675 if (license) {
676 if (info->license)
677 free(info->license);
678 info->license = strdup(license);
679 }
680 }
681 break;
682 case SDB_PLUGIN_INFO_VERSION:
683 {
684 int version = va_arg(ap, int);
685 info->version = version;
686 }
687 break;
688 case SDB_PLUGIN_INFO_PLUGIN_VERSION:
689 {
690 int version = va_arg(ap, int);
691 info->plugin_version = version;
692 }
693 break;
694 default:
695 va_end(ap);
696 return -1;
697 }
699 va_end(ap);
700 return 0;
701 } /* sdb_plugin_set_info */
703 int
704 sdb_plugin_register_config(sdb_plugin_config_cb callback)
705 {
706 ctx_t *ctx = ctx_get();
708 if (! ctx) {
709 sdb_log(SDB_LOG_ERR, "core: Invalid attempt to register a "
710 "config callback from outside a plugin");
711 return -1;
712 }
713 return plugin_add_callback(&config_list, "init", ctx->info.plugin_name,
714 (void *)callback, NULL);
715 } /* sdb_plugin_register_config */
717 int
718 sdb_plugin_register_init(const char *name, sdb_plugin_init_cb callback,
719 sdb_object_t *user_data)
720 {
721 char cb_name[1024];
722 return plugin_add_callback(&init_list, "init",
723 plugin_get_name(name, cb_name, sizeof(cb_name)),
724 (void *)callback, user_data);
725 } /* sdb_plugin_register_init */
727 int
728 sdb_plugin_register_shutdown(const char *name, sdb_plugin_shutdown_cb callback,
729 sdb_object_t *user_data)
730 {
731 char cb_name[1024];
732 return plugin_add_callback(&shutdown_list, "shutdown",
733 plugin_get_name(name, cb_name, sizeof(cb_name)),
734 (void *)callback, user_data);
735 } /* sdb_plugin_register_shutdown */
737 int
738 sdb_plugin_register_log(const char *name, sdb_plugin_log_cb callback,
739 sdb_object_t *user_data)
740 {
741 char cb_name[1024];
742 return plugin_add_callback(&log_list, "log",
743 plugin_get_name(name, cb_name, sizeof(cb_name)),
744 callback, user_data);
745 } /* sdb_plugin_register_log */
747 int
748 sdb_plugin_register_cname(const char *name, sdb_plugin_cname_cb callback,
749 sdb_object_t *user_data)
750 {
751 char cb_name[1024];
752 return plugin_add_callback(&cname_list, "cname",
753 plugin_get_name(name, cb_name, sizeof(cb_name)),
754 callback, user_data);
755 } /* sdb_plugin_register_cname */
757 int
758 sdb_plugin_register_collector(const char *name, sdb_plugin_collector_cb callback,
759 const sdb_time_t *interval, sdb_object_t *user_data)
760 {
761 char cb_name[1024];
762 sdb_object_t *obj;
764 if ((! name) || (! callback))
765 return -1;
767 if (! collector_list)
768 collector_list = sdb_llist_create();
769 if (! collector_list)
770 return -1;
772 plugin_get_name(name, cb_name, sizeof(cb_name));
774 obj = sdb_object_create(cb_name, sdb_plugin_collector_cb_type,
775 &collector_list, "collector", callback, user_data);
776 if (! obj)
777 return -1;
779 if (interval)
780 SDB_PLUGIN_CCB(obj)->ccb_interval = *interval;
781 else {
782 sdb_time_t tmp = sdb_plugin_get_ctx().interval;
784 if (tmp > 0)
785 SDB_PLUGIN_CCB(obj)->ccb_interval = tmp;
786 else
787 SDB_PLUGIN_CCB(obj)->ccb_interval = 0;
788 }
790 if (! (SDB_PLUGIN_CCB(obj)->ccb_next_update = sdb_gettime())) {
791 char errbuf[1024];
792 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
793 "time: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
794 sdb_object_deref(obj);
795 return -1;
796 }
798 if (sdb_llist_insert_sorted(collector_list, obj,
799 plugin_cmp_next_update)) {
800 sdb_object_deref(obj);
801 return -1;
802 }
804 /* pass control to the list */
805 sdb_object_deref(obj);
807 sdb_log(SDB_LOG_INFO, "core: Registered collector callback '%s' "
808 "(interval = %.3fs).", cb_name,
809 SDB_TIME_TO_DOUBLE(SDB_PLUGIN_CCB(obj)->ccb_interval));
810 return 0;
811 } /* sdb_plugin_register_collector */
813 sdb_plugin_ctx_t
814 sdb_plugin_get_ctx(void)
815 {
816 ctx_t *c;
818 c = ctx_get();
819 if (! c) {
820 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid read access to plugin "
821 "context outside a plugin");
822 return plugin_default_ctx;
823 }
824 return c->public;
825 } /* sdb_plugin_get_ctx */
827 int
828 sdb_plugin_set_ctx(sdb_plugin_ctx_t ctx, sdb_plugin_ctx_t *old)
829 {
830 ctx_t *c;
832 c = ctx_get();
833 if (! c) {
834 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid write access to plugin "
835 "context outside a plugin");
836 return -1;
837 }
839 if (old)
840 *old = c->public;
841 c->public = ctx;
842 return 0;
843 } /* sdb_plugin_set_ctx */
845 int
846 sdb_plugin_configure(const char *name, oconfig_item_t *ci)
847 {
848 sdb_plugin_cb_t *plugin;
849 sdb_plugin_config_cb callback;
851 ctx_t *old_ctx;
853 int status;
855 if ((! name) || (! ci))
856 return -1;
858 plugin = SDB_PLUGIN_CB(sdb_llist_search_by_name(config_list, name));
859 if (! plugin) {
860 ctx_t *ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
861 if (! ctx)
862 sdb_log(SDB_LOG_ERR, "core: Cannot configure unknown "
863 "plugin '%s'. Missing 'LoadPlugin \"%s\"'?",
864 name, name);
865 else
866 sdb_log(SDB_LOG_ERR, "core: Plugin '%s' did not register "
867 "a config callback.", name);
868 errno = ENOENT;
869 return -1;
870 }
872 old_ctx = ctx_set(plugin->cb_ctx);
873 callback = (sdb_plugin_config_cb)plugin->cb_callback;
874 status = callback(ci);
875 ctx_set(old_ctx);
876 return status;
877 } /* sdb_plugin_configure */
879 int
880 sdb_plugin_reconfigure_init(void)
881 {
882 sdb_llist_iter_t *iter;
884 iter = sdb_llist_get_iter(config_list);
885 if (config_list && (! iter))
886 return -1;
888 /* deconfigure all plugins */
889 while (sdb_llist_iter_has_next(iter)) {
890 sdb_plugin_cb_t *plugin;
891 sdb_plugin_config_cb callback;
892 ctx_t *old_ctx;
894 plugin = SDB_PLUGIN_CB(sdb_llist_iter_get_next(iter));
895 old_ctx = ctx_set(plugin->cb_ctx);
896 callback = (sdb_plugin_config_cb)plugin->cb_callback;
897 callback(NULL);
898 ctx_set(old_ctx);
899 }
900 sdb_llist_iter_destroy(iter);
902 iter = sdb_llist_get_iter(all_plugins);
903 if (all_plugins && (! iter))
904 return -1;
906 /* record all plugins as being unused */
907 while (sdb_llist_iter_has_next(iter))
908 CTX(sdb_llist_iter_get_next(iter))->use_cnt = 0;
909 sdb_llist_iter_destroy(iter);
911 plugin_unregister_all();
912 return 0;
913 } /* sdb_plugin_reconfigure_init */
915 int
916 sdb_plugin_reconfigure_finish(void)
917 {
918 sdb_llist_iter_t *iter;
920 iter = sdb_llist_get_iter(all_plugins);
921 if (all_plugins && (! iter))
922 return -1;
924 while (sdb_llist_iter_has_next(iter)) {
925 ctx_t *ctx = CTX(sdb_llist_iter_get_next(iter));
926 if (ctx->use_cnt)
927 continue;
929 sdb_log(SDB_LOG_INFO, "core: Module %s no longer in use",
930 ctx->info.plugin_name);
931 sdb_llist_iter_remove_current(iter);
932 plugin_unregister_by_name(ctx->info.plugin_name);
933 sdb_object_deref(SDB_OBJ(ctx));
934 }
935 sdb_llist_iter_destroy(iter);
936 return 0;
937 } /* sdb_plugin_reconfigure_finish */
939 int
940 sdb_plugin_init_all(void)
941 {
942 sdb_llist_iter_t *iter;
943 int ret = 0;
945 iter = sdb_llist_get_iter(init_list);
946 while (sdb_llist_iter_has_next(iter)) {
947 sdb_plugin_cb_t *cb;
948 sdb_plugin_init_cb callback;
949 ctx_t *old_ctx;
951 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
952 assert(obj);
953 cb = SDB_PLUGIN_CB(obj);
955 callback = (sdb_plugin_init_cb)cb->cb_callback;
957 old_ctx = ctx_set(cb->cb_ctx);
958 if (callback(cb->cb_user_data)) {
959 sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin "
960 "'%s'. Unregistering all callbacks.", obj->name);
961 ctx_set(old_ctx);
962 plugin_unregister_by_name(cb->cb_ctx->info.plugin_name);
963 ++ret;
964 }
965 else
966 ctx_set(old_ctx);
967 }
968 sdb_llist_iter_destroy(iter);
969 return ret;
970 } /* sdb_plugin_init_all */
972 int
973 sdb_plugin_shutdown_all(void)
974 {
975 sdb_llist_iter_t *iter;
976 int ret = 0;
978 iter = sdb_llist_get_iter(shutdown_list);
979 while (sdb_llist_iter_has_next(iter)) {
980 sdb_plugin_cb_t *cb;
981 sdb_plugin_shutdown_cb callback;
982 ctx_t *old_ctx;
984 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
985 assert(obj);
986 cb = SDB_PLUGIN_CB(obj);
988 callback = (sdb_plugin_shutdown_cb)cb->cb_callback;
990 old_ctx = ctx_set(cb->cb_ctx);
991 if (callback(cb->cb_user_data)) {
992 sdb_log(SDB_LOG_ERR, "core: Failed to shutdown plugin '%s'.",
993 obj->name);
994 ++ret;
995 }
996 ctx_set(old_ctx);
997 }
998 sdb_llist_iter_destroy(iter);
999 return ret;
1000 } /* sdb_plugin_shutdown_all */
1002 int
1003 sdb_plugin_collector_loop(sdb_plugin_loop_t *loop)
1004 {
1005 if (! collector_list) {
1006 sdb_log(SDB_LOG_WARNING, "core: No collectors registered. "
1007 "Quiting main loop.");
1008 return -1;
1009 }
1011 if (! loop)
1012 return -1;
1014 while (loop->do_loop) {
1015 sdb_plugin_collector_cb callback;
1016 ctx_t *old_ctx;
1018 sdb_time_t interval, now;
1020 sdb_object_t *obj = sdb_llist_shift(collector_list);
1021 if (! obj)
1022 return -1;
1024 callback = (sdb_plugin_collector_cb)SDB_PLUGIN_CCB(obj)->ccb_callback;
1026 if (! (now = sdb_gettime())) {
1027 char errbuf[1024];
1028 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
1029 "time in collector main loop: %s",
1030 sdb_strerror(errno, errbuf, sizeof(errbuf)));
1031 now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
1032 }
1034 if (now < SDB_PLUGIN_CCB(obj)->ccb_next_update) {
1035 interval = SDB_PLUGIN_CCB(obj)->ccb_next_update - now;
1037 errno = 0;
1038 while (loop->do_loop && sdb_sleep(interval, &interval)) {
1039 if (errno != EINTR) {
1040 char errbuf[1024];
1041 sdb_log(SDB_LOG_ERR, "core: Failed to sleep "
1042 "in collector main loop: %s",
1043 sdb_strerror(errno, errbuf, sizeof(errbuf)));
1044 sdb_llist_insert_sorted(collector_list, obj,
1045 plugin_cmp_next_update);
1046 sdb_object_deref(obj);
1047 return -1;
1048 }
1049 errno = 0;
1050 }
1052 if (! loop->do_loop) {
1053 /* put back; don't worry about errors */
1054 sdb_llist_insert_sorted(collector_list, obj,
1055 plugin_cmp_next_update);
1056 sdb_object_deref(obj);
1057 return 0;
1058 }
1059 }
1061 old_ctx = ctx_set(SDB_PLUGIN_CCB(obj)->ccb_ctx);
1062 if (callback(SDB_PLUGIN_CCB(obj)->ccb_user_data)) {
1063 /* XXX */
1064 }
1065 ctx_set(old_ctx);
1067 interval = SDB_PLUGIN_CCB(obj)->ccb_interval;
1068 if (! interval)
1069 interval = loop->default_interval;
1070 if (! interval) {
1071 sdb_log(SDB_LOG_WARNING, "core: No interval configured "
1072 "for plugin '%s'; skipping any further "
1073 "iterations.", obj->name);
1074 sdb_object_deref(obj);
1075 continue;
1076 }
1078 SDB_PLUGIN_CCB(obj)->ccb_next_update += interval;
1080 if (! (now = sdb_gettime())) {
1081 char errbuf[1024];
1082 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
1083 "time in collector main loop: %s",
1084 sdb_strerror(errno, errbuf, sizeof(errbuf)));
1085 now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
1086 }
1088 if (now > SDB_PLUGIN_CCB(obj)->ccb_next_update) {
1089 sdb_log(SDB_LOG_WARNING, "core: Plugin '%s' took too "
1090 "long; skipping iterations to keep up.",
1091 obj->name);
1092 SDB_PLUGIN_CCB(obj)->ccb_next_update = now;
1093 }
1095 if (sdb_llist_insert_sorted(collector_list, obj,
1096 plugin_cmp_next_update)) {
1097 sdb_log(SDB_LOG_ERR, "core: Failed to re-insert "
1098 "plugin '%s' into collector list. Unable to further "
1099 "use the plugin.",
1100 obj->name);
1101 sdb_object_deref(obj);
1102 return -1;
1103 }
1105 /* pass control back to the list */
1106 sdb_object_deref(obj);
1107 }
1108 return 0;
1109 } /* sdb_plugin_read_loop */
1111 char *
1112 sdb_plugin_cname(char *hostname)
1113 {
1114 sdb_llist_iter_t *iter;
1116 if (! hostname)
1117 return NULL;
1119 if (! cname_list)
1120 return hostname;
1122 iter = sdb_llist_get_iter(cname_list);
1123 while (sdb_llist_iter_has_next(iter)) {
1124 sdb_plugin_cname_cb callback;
1125 char *cname;
1127 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
1128 assert(obj);
1130 callback = (sdb_plugin_cname_cb)SDB_PLUGIN_CB(obj)->cb_callback;
1131 cname = callback(hostname, SDB_PLUGIN_CB(obj)->cb_user_data);
1132 if (cname) {
1133 free(hostname);
1134 hostname = cname;
1135 }
1136 /* else: don't change hostname */
1137 }
1138 sdb_llist_iter_destroy(iter);
1139 return hostname;
1140 } /* sdb_plugin_cname */
1142 int
1143 sdb_plugin_log(int prio, const char *msg)
1144 {
1145 sdb_llist_iter_t *iter;
1146 int ret = -1;
1148 _Bool logged = 0;
1150 if (! msg)
1151 return 0;
1153 iter = sdb_llist_get_iter(log_list);
1154 while (sdb_llist_iter_has_next(iter)) {
1155 sdb_plugin_log_cb callback;
1156 int tmp;
1158 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
1159 assert(obj);
1161 callback = (sdb_plugin_log_cb)SDB_PLUGIN_CB(obj)->cb_callback;
1162 tmp = callback(prio, msg, SDB_PLUGIN_CB(obj)->cb_user_data);
1163 if (tmp > ret)
1164 ret = tmp;
1166 if (SDB_PLUGIN_CB(obj)->cb_ctx)
1167 logged = 1;
1168 /* else: this is an internally registered callback */
1169 }
1170 sdb_llist_iter_destroy(iter);
1172 if (! logged)
1173 return fprintf(stderr, "[%s] %s\n", SDB_LOG_PRIO_TO_STRING(prio), msg);
1174 return ret;
1175 } /* sdb_plugin_log */
1177 int
1178 sdb_plugin_vlogf(int prio, const char *fmt, va_list ap)
1179 {
1180 sdb_strbuf_t *buf;
1181 int ret;
1183 if (! fmt)
1184 return 0;
1186 buf = sdb_strbuf_create(64);
1187 if (! buf) {
1188 ret = fprintf(stderr, "[%s] ", SDB_LOG_PRIO_TO_STRING(prio));
1189 ret += vfprintf(stderr, fmt, ap);
1190 return ret;
1191 }
1193 if (sdb_strbuf_vsprintf(buf, fmt, ap) < 0) {
1194 sdb_strbuf_destroy(buf);
1195 return -1;
1196 }
1198 ret = sdb_plugin_log(prio, sdb_strbuf_string(buf));
1199 sdb_strbuf_destroy(buf);
1200 return ret;
1201 } /* sdb_plugin_vlogf */
1203 int
1204 sdb_plugin_logf(int prio, const char *fmt, ...)
1205 {
1206 va_list ap;
1207 int ret;
1209 if (! fmt)
1210 return 0;
1212 va_start(ap, fmt);
1213 ret = sdb_plugin_vlogf(prio, fmt, ap);
1214 va_end(ap);
1215 return ret;
1216 } /* sdb_plugin_logf */
1218 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */