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 } callback_t;
84 #define CB_INIT { SDB_OBJECT_INIT, \
85 /* callback = */ NULL, /* user_data = */ NULL, \
86 SDB_PLUGIN_CTX_INIT }
87 #define CB(obj) ((callback_t *)(obj))
88 #define CONST_CB(obj) ((const callback_t *)(obj))
90 typedef struct {
91 callback_t super;
92 #define ccb_callback super.cb_callback
93 #define ccb_user_data super.cb_user_data
94 #define ccb_ctx super.cb_ctx
95 sdb_time_t ccb_interval;
96 sdb_time_t ccb_next_update;
97 } collector_t;
98 #define CCB(obj) ((collector_t *)(obj))
99 #define CONST_CCB(obj) ((const collector_t *)(obj))
101 typedef struct {
102 callback_t super; /* cb_callback will always be NULL */
103 #define w_user_data super.cb_user_data
104 #define w_ctx super.cb_ctx
105 sdb_store_writer_t impl;
106 } writer_t;
107 #define WRITER(obj) ((writer_t *)(obj))
109 /*
110 * private variables
111 */
113 static sdb_plugin_ctx_t plugin_default_ctx = SDB_PLUGIN_CTX_INIT;
114 static sdb_plugin_info_t plugin_default_info = SDB_PLUGIN_INFO_INIT;
116 static pthread_key_t plugin_ctx_key;
117 static bool plugin_ctx_key_initialized = 0;
119 /* a list of the plugin contexts of all registered plugins */
120 static sdb_llist_t *all_plugins = NULL;
122 static sdb_llist_t *config_list = NULL;
123 static sdb_llist_t *init_list = NULL;
124 static sdb_llist_t *collector_list = NULL;
125 static sdb_llist_t *cname_list = NULL;
126 static sdb_llist_t *shutdown_list = NULL;
127 static sdb_llist_t *log_list = NULL;
128 static sdb_llist_t *ts_fetcher_list = NULL;
129 static sdb_llist_t *writer_list = NULL;
131 static struct {
132 const char *type;
133 sdb_llist_t **list;
134 } all_lists[] = {
135 { "config", &config_list },
136 { "init", &init_list },
137 { "collector", &collector_list },
138 { "cname", &cname_list },
139 { "shutdown", &shutdown_list },
140 { "log", &log_list },
141 { "timeseries fetcher", &ts_fetcher_list },
142 { "store writer", &writer_list },
143 };
145 /*
146 * private helper functions
147 */
149 static void
150 plugin_info_clear(sdb_plugin_info_t *info)
151 {
152 sdb_plugin_info_t empty_info = SDB_PLUGIN_INFO_INIT;
153 if (! info)
154 return;
156 if (info->plugin_name)
157 free(info->plugin_name);
158 if (info->filename)
159 free(info->filename);
161 if (info->description)
162 free(info->description);
163 if (info->copyright)
164 free(info->copyright);
165 if (info->license)
166 free(info->license);
168 *info = empty_info;
169 } /* plugin_info_clear */
171 static void
172 ctx_key_init(void)
173 {
174 if (plugin_ctx_key_initialized)
175 return;
177 pthread_key_create(&plugin_ctx_key, /* destructor */ NULL);
178 plugin_ctx_key_initialized = 1;
179 } /* ctx_key_init */
181 static int
182 plugin_cmp_next_update(const sdb_object_t *a, const sdb_object_t *b)
183 {
184 const collector_t *ccb1 = (const collector_t *)a;
185 const collector_t *ccb2 = (const collector_t *)b;
187 assert(ccb1 && ccb2);
189 return (ccb1->ccb_next_update > ccb2->ccb_next_update)
190 ? 1 : (ccb1->ccb_next_update < ccb2->ccb_next_update)
191 ? -1 : 0;
192 } /* plugin_cmp_next_update */
194 static int
195 plugin_lookup_by_name(const sdb_object_t *obj, const void *id)
196 {
197 const callback_t *cb = CONST_CB(obj);
198 const char *name = id;
200 assert(cb && id);
202 /* when a plugin was registered from outside a plugin (e.g. the core),
203 * we don't have a plugin context */
204 if (! cb->cb_ctx)
205 return 1;
207 if (!strcasecmp(cb->cb_ctx->info.plugin_name, name))
208 return 0;
209 return 1;
210 } /* plugin_lookup_by_name */
212 /* since this function is called from sdb_plugin_reconfigure_finish()
213 * when iterating through all_plugins, we may not do any additional
214 * modifications to all_plugins except for the optional removal */
215 static void
216 plugin_unregister_by_name(const char *plugin_name)
217 {
218 sdb_object_t *obj;
219 size_t i;
221 for (i = 0; i < SDB_STATIC_ARRAY_LEN(all_lists); ++i) {
222 const char *type = all_lists[i].type;
223 sdb_llist_t *list = *all_lists[i].list;
225 while (1) {
226 callback_t *cb;
228 cb = CB(sdb_llist_remove(list,
229 plugin_lookup_by_name, plugin_name));
230 if (! cb)
231 break;
233 assert(cb->cb_ctx);
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 /* when called from sdb_plugin_reconfigure_finish, the object has already
244 * been removed from the list */
245 if (obj && (obj->ref_cnt <= 1)) {
246 sdb_llist_remove_by_name(all_plugins, plugin_name);
247 sdb_object_deref(obj);
248 }
249 /* else: other callbacks still reference it */
250 } /* plugin_unregister_by_name */
252 /*
253 * private types
254 */
256 static int
257 ctx_init(sdb_object_t *obj, va_list __attribute__((unused)) ap)
258 {
259 ctx_t *ctx = CTX(obj);
261 assert(ctx);
263 ctx->public = plugin_default_ctx;
264 ctx->info = plugin_default_info;
265 ctx->handle = NULL;
266 ctx->use_cnt = 1;
267 return 0;
268 } /* ctx_init */
270 static void
271 ctx_destroy(sdb_object_t *obj)
272 {
273 ctx_t *ctx = CTX(obj);
275 if (ctx->handle) {
276 const char *err;
278 sdb_log(SDB_LOG_INFO, "core: Unloading module %s",
279 ctx->info.plugin_name);
281 lt_dlerror();
282 lt_dlclose(ctx->handle);
283 if ((err = lt_dlerror()))
284 sdb_log(SDB_LOG_WARNING, "core: Failed to unload module %s: %s",
285 ctx->info.plugin_name, err);
286 }
288 plugin_info_clear(&ctx->info);
289 } /* ctx_destroy */
291 static sdb_type_t ctx_type = {
292 sizeof(ctx_t),
294 ctx_init,
295 ctx_destroy
296 };
298 static ctx_t *
299 ctx_get(void)
300 {
301 if (! plugin_ctx_key_initialized)
302 ctx_key_init();
303 return pthread_getspecific(plugin_ctx_key);
304 } /* ctx_get */
306 static ctx_t *
307 ctx_set(ctx_t *new)
308 {
309 ctx_t *old;
311 if (! plugin_ctx_key_initialized)
312 ctx_key_init();
314 old = pthread_getspecific(plugin_ctx_key);
315 if (old)
316 sdb_object_deref(SDB_OBJ(old));
317 if (new)
318 sdb_object_ref(SDB_OBJ(new));
319 pthread_setspecific(plugin_ctx_key, new);
320 return old;
321 } /* ctx_set */
323 static ctx_t *
324 ctx_create(const char *name)
325 {
326 ctx_t *ctx;
328 ctx = CTX(sdb_object_create(name, ctx_type));
329 if (! ctx)
330 return NULL;
332 if (! plugin_ctx_key_initialized)
333 ctx_key_init();
334 ctx_set(ctx);
335 return ctx;
336 } /* ctx_create */
338 static int
339 plugin_cb_init(sdb_object_t *obj, va_list ap)
340 {
341 sdb_llist_t **list = va_arg(ap, sdb_llist_t **);
342 const char *type = va_arg(ap, const char *);
343 void *callback = va_arg(ap, void *);
344 sdb_object_t *ud = va_arg(ap, sdb_object_t *);
346 assert(list);
347 assert(type);
348 assert(obj);
350 if (sdb_llist_search_by_name(*list, obj->name)) {
351 sdb_log(SDB_LOG_WARNING, "core: %s callback '%s' "
352 "has already been registered. Ignoring newly "
353 "registered version.", type, obj->name);
354 return -1;
355 }
357 /* cb_ctx may be NULL if the plugin was not registered by a plugin */
359 CB(obj)->cb_callback = callback;
360 CB(obj)->cb_ctx = ctx_get();
361 sdb_object_ref(SDB_OBJ(CB(obj)->cb_ctx));
363 sdb_object_ref(ud);
364 CB(obj)->cb_user_data = ud;
365 return 0;
366 } /* plugin_cb_init */
368 static void
369 plugin_cb_destroy(sdb_object_t *obj)
370 {
371 assert(obj);
372 sdb_object_deref(CB(obj)->cb_user_data);
373 sdb_object_deref(SDB_OBJ(CB(obj)->cb_ctx));
374 } /* plugin_cb_destroy */
376 static sdb_type_t callback_type = {
377 sizeof(callback_t),
379 plugin_cb_init,
380 plugin_cb_destroy
381 };
383 static sdb_type_t collector_type = {
384 sizeof(collector_t),
386 plugin_cb_init,
387 plugin_cb_destroy
388 };
390 static int
391 plugin_writer_init(sdb_object_t *obj, va_list ap)
392 {
393 sdb_store_writer_t *impl = va_arg(ap, sdb_store_writer_t *);
394 sdb_object_t *ud = va_arg(ap, sdb_object_t *);
396 assert(impl);
398 if ((! impl->store_host) || (! impl->store_service)
399 || (! impl->store_metric) || (! impl->store_attribute)
400 || (! impl->store_service_attr) || (! impl->store_metric_attr)) {
401 sdb_log(SDB_LOG_ERR, "core: store writer callback '%s' "
402 "does not fully implement the writer interface.",
403 obj->name);
404 return -1;
405 }
406 if (sdb_llist_search_by_name(writer_list, obj->name)) {
407 sdb_log(SDB_LOG_WARNING, "core: store writer callback '%s' "
408 "has already been registered. Ignoring newly "
409 "registered version.", obj->name);
410 return -1;
411 }
413 /* ctx may be NULL if the callback was not registered by a plugin */
415 WRITER(obj)->impl = *impl;
416 WRITER(obj)->w_ctx = ctx_get();
417 sdb_object_ref(SDB_OBJ(WRITER(obj)->w_ctx));
419 sdb_object_ref(ud);
420 WRITER(obj)->w_user_data = ud;
421 return 0;
422 } /* plugin_writer_init */
424 static void
425 plugin_writer_destroy(sdb_object_t *obj)
426 {
427 assert(obj);
428 sdb_object_deref(WRITER(obj)->w_user_data);
429 sdb_object_deref(SDB_OBJ(WRITER(obj)->w_ctx));
430 } /* plugin_writer_destroy */
432 static sdb_type_t writer_type = {
433 sizeof(writer_t),
435 plugin_writer_init,
436 plugin_writer_destroy
437 };
439 static int
440 module_init(const char *name, lt_dlhandle lh, sdb_plugin_info_t *info)
441 {
442 int (*mod_init)(sdb_plugin_info_t *);
443 int status;
445 mod_init = (int (*)(sdb_plugin_info_t *))lt_dlsym(lh, "sdb_module_init");
446 if (! mod_init) {
447 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': "
448 "could not find symbol 'sdb_module_init'", name);
449 return -1;
450 }
452 status = mod_init(info);
453 if (status) {
454 sdb_log(SDB_LOG_ERR, "core: Failed to initialize "
455 "module '%s'", name);
456 plugin_unregister_by_name(name);
457 return -1;
458 }
459 return 0;
460 } /* module_init */
462 static int
463 module_load(const char *basedir, const char *name,
464 const sdb_plugin_ctx_t *plugin_ctx)
465 {
466 char base_name[name ? strlen(name) + 1 : 1];
467 const char *name_ptr;
468 char *tmp;
470 char filename[1024];
471 lt_dlhandle lh;
473 ctx_t *ctx;
475 int status;
477 assert(name);
479 base_name[0] = '\0';
480 name_ptr = name;
482 while ((tmp = strstr(name_ptr, "::"))) {
483 strncat(base_name, name_ptr, (size_t)(tmp - name_ptr));
484 strcat(base_name, "/");
485 name_ptr = tmp + strlen("::");
486 }
487 strcat(base_name, name_ptr);
489 if (! basedir)
490 basedir = PKGLIBDIR;
492 snprintf(filename, sizeof(filename), "%s/%s.so", basedir, base_name);
493 filename[sizeof(filename) - 1] = '\0';
495 if (access(filename, R_OK)) {
496 char errbuf[1024];
497 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s' (%s): %s",
498 name, filename, sdb_strerror(errno, errbuf, sizeof(errbuf)));
499 return -1;
500 }
502 lt_dlinit();
503 lt_dlerror();
505 lh = lt_dlopen(filename);
506 if (! lh) {
507 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': %s"
508 "The most common cause for this problem are missing "
509 "dependencies.\n", name, lt_dlerror());
510 return -1;
511 }
513 if (ctx_get())
514 sdb_log(SDB_LOG_WARNING, "core: Discarding old plugin context");
516 ctx = ctx_create(name);
517 if (! ctx) {
518 sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin context");
519 return -1;
520 }
522 ctx->info.plugin_name = strdup(name);
523 ctx->info.filename = strdup(filename);
524 ctx->handle = lh;
526 if (plugin_ctx)
527 ctx->public = *plugin_ctx;
529 if ((status = module_init(name, lh, &ctx->info))) {
530 sdb_object_deref(SDB_OBJ(ctx));
531 return status;
532 }
534 /* compare minor version */
535 if ((ctx->info.version < 0)
536 || ((int)(ctx->info.version / 100) != (int)(SDB_VERSION / 100)))
537 sdb_log(SDB_LOG_WARNING, "core: WARNING: version of "
538 "plugin '%s' (%i.%i.%i) does not match our version "
539 "(%i.%i.%i); this might cause problems",
540 name, SDB_VERSION_DECODE(ctx->info.version),
541 SDB_VERSION_DECODE(SDB_VERSION));
543 sdb_llist_append(all_plugins, SDB_OBJ(ctx));
545 sdb_log(SDB_LOG_INFO, "core: Successfully loaded "
546 "plugin %s v%i (%s)", ctx->info.plugin_name,
547 ctx->info.plugin_version,
548 INFO_GET(&ctx->info, description));
549 sdb_log(SDB_LOG_INFO, "core: Plugin %s: %s, License: %s",
550 ctx->info.plugin_name,
551 INFO_GET(&ctx->info, copyright),
552 INFO_GET(&ctx->info, license));
554 /* any registered callbacks took ownership of the context */
555 sdb_object_deref(SDB_OBJ(ctx));
557 /* reset */
558 ctx_set(NULL);
559 return 0;
560 } /* module_load */
562 static char *
563 plugin_get_name(const char *name, char *buf, size_t bufsize)
564 {
565 ctx_t *ctx = ctx_get();
567 if (ctx)
568 snprintf(buf, bufsize, "%s::%s", ctx->info.plugin_name, name);
569 else
570 snprintf(buf, bufsize, "core::%s", name);
571 return buf;
572 } /* plugin_get_name */
574 static int
575 plugin_add_callback(sdb_llist_t **list, const char *type,
576 const char *name, void *callback, sdb_object_t *user_data)
577 {
578 sdb_object_t *obj;
580 if ((! name) || (! callback))
581 return -1;
583 assert(list);
585 if (! *list)
586 *list = sdb_llist_create();
587 if (! *list)
588 return -1;
590 obj = sdb_object_create(name, callback_type,
591 list, type, callback, user_data);
592 if (! obj)
593 return -1;
595 if (sdb_llist_append(*list, obj)) {
596 sdb_object_deref(obj);
597 return -1;
598 }
600 /* pass control to the list */
601 sdb_object_deref(obj);
603 sdb_log(SDB_LOG_INFO, "core: Registered %s callback '%s'.",
604 type, name);
605 return 0;
606 } /* plugin_add_callback */
608 /*
609 * public API
610 */
612 int
613 sdb_plugin_load(const char *basedir, const char *name,
614 const sdb_plugin_ctx_t *plugin_ctx)
615 {
616 ctx_t *ctx;
618 int status;
620 if ((! name) || (! *name))
621 return -1;
623 if (! all_plugins) {
624 if (! (all_plugins = sdb_llist_create())) {
625 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': "
626 "internal error while creating linked list", name);
627 return -1;
628 }
629 }
631 ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
632 if (ctx) {
633 /* plugin already loaded */
634 if (! ctx->use_cnt) {
635 /* reloading plugin */
636 ctx_t *old_ctx = ctx_set(ctx);
638 status = module_init(ctx->info.plugin_name, ctx->handle, NULL);
639 if (status)
640 return status;
642 sdb_log(SDB_LOG_INFO, "core: Successfully reloaded plugin "
643 "'%s' (%s)", ctx->info.plugin_name,
644 INFO_GET(&ctx->info, description));
645 ctx_set(old_ctx);
646 }
647 ++ctx->use_cnt;
648 return 0;
649 }
651 return module_load(basedir, name, plugin_ctx);
652 } /* sdb_plugin_load */
654 int
655 sdb_plugin_set_info(sdb_plugin_info_t *info, int type, ...)
656 {
657 va_list ap;
659 if (! info)
660 return -1;
662 va_start(ap, type);
664 switch (type) {
665 case SDB_PLUGIN_INFO_DESC:
666 {
667 char *desc = va_arg(ap, char *);
668 if (desc) {
669 if (info->description)
670 free(info->description);
671 info->description = strdup(desc);
672 }
673 }
674 break;
675 case SDB_PLUGIN_INFO_COPYRIGHT:
676 {
677 char *copyright = va_arg(ap, char *);
678 if (copyright)
679 info->copyright = strdup(copyright);
680 }
681 break;
682 case SDB_PLUGIN_INFO_LICENSE:
683 {
684 char *license = va_arg(ap, char *);
685 if (license) {
686 if (info->license)
687 free(info->license);
688 info->license = strdup(license);
689 }
690 }
691 break;
692 case SDB_PLUGIN_INFO_VERSION:
693 {
694 int version = va_arg(ap, int);
695 info->version = version;
696 }
697 break;
698 case SDB_PLUGIN_INFO_PLUGIN_VERSION:
699 {
700 int version = va_arg(ap, int);
701 info->plugin_version = version;
702 }
703 break;
704 default:
705 va_end(ap);
706 return -1;
707 }
709 va_end(ap);
710 return 0;
711 } /* sdb_plugin_set_info */
713 int
714 sdb_plugin_register_config(sdb_plugin_config_cb callback)
715 {
716 ctx_t *ctx = ctx_get();
718 if (! ctx) {
719 sdb_log(SDB_LOG_ERR, "core: Invalid attempt to register a "
720 "config callback from outside a plugin");
721 return -1;
722 }
723 return plugin_add_callback(&config_list, "config", ctx->info.plugin_name,
724 (void *)callback, NULL);
725 } /* sdb_plugin_register_config */
727 int
728 sdb_plugin_register_init(const char *name, sdb_plugin_init_cb callback,
729 sdb_object_t *user_data)
730 {
731 char cb_name[1024];
732 return plugin_add_callback(&init_list, "init",
733 plugin_get_name(name, cb_name, sizeof(cb_name)),
734 (void *)callback, user_data);
735 } /* sdb_plugin_register_init */
737 int
738 sdb_plugin_register_shutdown(const char *name, sdb_plugin_shutdown_cb callback,
739 sdb_object_t *user_data)
740 {
741 char cb_name[1024];
742 return plugin_add_callback(&shutdown_list, "shutdown",
743 plugin_get_name(name, cb_name, sizeof(cb_name)),
744 (void *)callback, user_data);
745 } /* sdb_plugin_register_shutdown */
747 int
748 sdb_plugin_register_log(const char *name, sdb_plugin_log_cb callback,
749 sdb_object_t *user_data)
750 {
751 char cb_name[1024];
752 return plugin_add_callback(&log_list, "log",
753 plugin_get_name(name, cb_name, sizeof(cb_name)),
754 callback, user_data);
755 } /* sdb_plugin_register_log */
757 int
758 sdb_plugin_register_cname(const char *name, sdb_plugin_cname_cb callback,
759 sdb_object_t *user_data)
760 {
761 char cb_name[1024];
762 return plugin_add_callback(&cname_list, "cname",
763 plugin_get_name(name, cb_name, sizeof(cb_name)),
764 callback, user_data);
765 } /* sdb_plugin_register_cname */
767 int
768 sdb_plugin_register_collector(const char *name, sdb_plugin_collector_cb callback,
769 const sdb_time_t *interval, sdb_object_t *user_data)
770 {
771 char cb_name[1024];
772 sdb_object_t *obj;
774 if ((! name) || (! callback))
775 return -1;
777 if (! collector_list)
778 collector_list = sdb_llist_create();
779 if (! collector_list)
780 return -1;
782 plugin_get_name(name, cb_name, sizeof(cb_name));
784 obj = sdb_object_create(cb_name, collector_type,
785 &collector_list, "collector", callback, user_data);
786 if (! obj)
787 return -1;
789 if (interval)
790 CCB(obj)->ccb_interval = *interval;
791 else {
792 ctx_t *ctx = ctx_get();
794 if (! ctx) {
795 sdb_log(SDB_LOG_ERR, "core: Cannot determine interval "
796 "for collector %s; none specified and no plugin "
797 "context found", cb_name);
798 return -1;
799 }
801 CCB(obj)->ccb_interval = ctx->public.interval;
802 }
804 if (! (CCB(obj)->ccb_next_update = sdb_gettime())) {
805 char errbuf[1024];
806 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
807 "time: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
808 sdb_object_deref(obj);
809 return -1;
810 }
812 if (sdb_llist_insert_sorted(collector_list, obj,
813 plugin_cmp_next_update)) {
814 sdb_object_deref(obj);
815 return -1;
816 }
818 /* pass control to the list */
819 sdb_object_deref(obj);
821 sdb_log(SDB_LOG_INFO, "core: Registered collector callback '%s' "
822 "(interval = %.3fs).", cb_name,
823 SDB_TIME_TO_DOUBLE(CCB(obj)->ccb_interval));
824 return 0;
825 } /* sdb_plugin_register_collector */
827 int
828 sdb_plugin_register_ts_fetcher(const char *name,
829 sdb_plugin_fetch_ts_cb callback, sdb_object_t *user_data)
830 {
831 return plugin_add_callback(&ts_fetcher_list, "time-series fetcher",
832 name, callback, user_data);
833 } /* sdb_plugin_register_ts_fetcher */
835 int
836 sdb_plugin_register_writer(const char *name,
837 sdb_store_writer_t *writer, sdb_object_t *user_data)
838 {
839 char cb_name[1024];
840 sdb_object_t *obj;
842 if ((! name) || (! writer))
843 return -1;
845 if (! writer_list)
846 writer_list = sdb_llist_create();
847 if (! writer_list)
848 return -1;
850 plugin_get_name(name, cb_name, sizeof(cb_name));
852 obj = sdb_object_create(cb_name, writer_type,
853 writer, user_data);
854 if (! obj)
855 return -1;
857 if (sdb_llist_append(writer_list, obj)) {
858 sdb_object_deref(obj);
859 return -1;
860 }
862 /* pass control to the list */
863 sdb_object_deref(obj);
865 sdb_log(SDB_LOG_INFO, "core: Registered store writer callback '%s'.",
866 cb_name);
867 return 0;
868 } /* sdb_store_register_writer */
870 void
871 sdb_plugin_unregister_all(void)
872 {
873 size_t i;
875 for (i = 0; i < SDB_STATIC_ARRAY_LEN(all_lists); ++i) {
876 const char *type = all_lists[i].type;
877 sdb_llist_t *list = *all_lists[i].list;
879 size_t len = sdb_llist_len(list);
881 if (! len)
882 continue;
884 sdb_llist_clear(list);
885 sdb_log(SDB_LOG_INFO, "core: Unregistered %zu %s callback%s",
886 len, type, len == 1 ? "" : "s");
887 }
888 } /* sdb_plugin_unregister_all */
890 sdb_plugin_ctx_t
891 sdb_plugin_get_ctx(void)
892 {
893 ctx_t *c;
895 c = ctx_get();
896 if (! c) {
897 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid read access to plugin "
898 "context outside a plugin");
899 return plugin_default_ctx;
900 }
901 return c->public;
902 } /* sdb_plugin_get_ctx */
904 int
905 sdb_plugin_set_ctx(sdb_plugin_ctx_t ctx, sdb_plugin_ctx_t *old)
906 {
907 ctx_t *c;
909 c = ctx_get();
910 if (! c) {
911 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid write access to plugin "
912 "context outside a plugin");
913 return -1;
914 }
916 if (old)
917 *old = c->public;
918 c->public = ctx;
919 return 0;
920 } /* sdb_plugin_set_ctx */
922 const sdb_plugin_info_t *
923 sdb_plugin_current(void)
924 {
925 ctx_t *ctx = ctx_get();
927 if (! ctx)
928 return NULL;
929 return &ctx->info;
930 } /* sdb_plugin_current */
932 int
933 sdb_plugin_configure(const char *name, oconfig_item_t *ci)
934 {
935 callback_t *plugin;
936 sdb_plugin_config_cb callback;
938 ctx_t *old_ctx;
940 int status;
942 if ((! name) || (! ci))
943 return -1;
945 plugin = CB(sdb_llist_search_by_name(config_list, name));
946 if (! plugin) {
947 ctx_t *ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
948 if (! ctx)
949 sdb_log(SDB_LOG_ERR, "core: Cannot configure unknown "
950 "plugin '%s'. Missing 'LoadPlugin \"%s\"'?",
951 name, name);
952 else
953 sdb_log(SDB_LOG_ERR, "core: Plugin '%s' did not register "
954 "a config callback.", name);
955 errno = ENOENT;
956 return -1;
957 }
959 old_ctx = ctx_set(plugin->cb_ctx);
960 callback = (sdb_plugin_config_cb)plugin->cb_callback;
961 status = callback(ci);
962 ctx_set(old_ctx);
963 return status;
964 } /* sdb_plugin_configure */
966 int
967 sdb_plugin_reconfigure_init(void)
968 {
969 sdb_llist_iter_t *iter;
971 iter = sdb_llist_get_iter(config_list);
972 if (config_list && (! iter))
973 return -1;
975 /* deconfigure all plugins */
976 while (sdb_llist_iter_has_next(iter)) {
977 callback_t *plugin;
978 sdb_plugin_config_cb callback;
979 ctx_t *old_ctx;
981 plugin = CB(sdb_llist_iter_get_next(iter));
982 old_ctx = ctx_set(plugin->cb_ctx);
983 callback = (sdb_plugin_config_cb)plugin->cb_callback;
984 callback(NULL);
985 ctx_set(old_ctx);
986 }
987 sdb_llist_iter_destroy(iter);
989 iter = sdb_llist_get_iter(all_plugins);
990 if (all_plugins && (! iter))
991 return -1;
993 /* record all plugins as being unused */
994 while (sdb_llist_iter_has_next(iter))
995 CTX(sdb_llist_iter_get_next(iter))->use_cnt = 0;
996 sdb_llist_iter_destroy(iter);
998 sdb_plugin_unregister_all();
999 return 0;
1000 } /* sdb_plugin_reconfigure_init */
1002 int
1003 sdb_plugin_reconfigure_finish(void)
1004 {
1005 sdb_llist_iter_t *iter;
1007 iter = sdb_llist_get_iter(all_plugins);
1008 if (all_plugins && (! iter))
1009 return -1;
1011 while (sdb_llist_iter_has_next(iter)) {
1012 ctx_t *ctx = CTX(sdb_llist_iter_get_next(iter));
1013 if (ctx->use_cnt)
1014 continue;
1016 sdb_log(SDB_LOG_INFO, "core: Module %s no longer in use",
1017 ctx->info.plugin_name);
1018 sdb_llist_iter_remove_current(iter);
1019 plugin_unregister_by_name(ctx->info.plugin_name);
1020 sdb_object_deref(SDB_OBJ(ctx));
1021 }
1022 sdb_llist_iter_destroy(iter);
1023 return 0;
1024 } /* sdb_plugin_reconfigure_finish */
1026 int
1027 sdb_plugin_init_all(void)
1028 {
1029 sdb_llist_iter_t *iter;
1030 int ret = 0;
1032 iter = sdb_llist_get_iter(init_list);
1033 while (sdb_llist_iter_has_next(iter)) {
1034 callback_t *cb;
1035 sdb_plugin_init_cb callback;
1036 ctx_t *old_ctx;
1038 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
1039 assert(obj);
1040 cb = CB(obj);
1042 callback = (sdb_plugin_init_cb)cb->cb_callback;
1044 old_ctx = ctx_set(cb->cb_ctx);
1045 if (callback(cb->cb_user_data)) {
1046 sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin "
1047 "'%s'. Unregistering all callbacks.", obj->name);
1048 ctx_set(old_ctx);
1049 plugin_unregister_by_name(cb->cb_ctx->info.plugin_name);
1050 ++ret;
1051 }
1052 else
1053 ctx_set(old_ctx);
1054 }
1055 sdb_llist_iter_destroy(iter);
1056 return ret;
1057 } /* sdb_plugin_init_all */
1059 int
1060 sdb_plugin_shutdown_all(void)
1061 {
1062 sdb_llist_iter_t *iter;
1063 int ret = 0;
1065 iter = sdb_llist_get_iter(shutdown_list);
1066 while (sdb_llist_iter_has_next(iter)) {
1067 callback_t *cb;
1068 sdb_plugin_shutdown_cb callback;
1069 ctx_t *old_ctx;
1071 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
1072 assert(obj);
1073 cb = CB(obj);
1075 callback = (sdb_plugin_shutdown_cb)cb->cb_callback;
1077 old_ctx = ctx_set(cb->cb_ctx);
1078 if (callback(cb->cb_user_data)) {
1079 sdb_log(SDB_LOG_ERR, "core: Failed to shutdown plugin '%s'.",
1080 obj->name);
1081 ++ret;
1082 }
1083 ctx_set(old_ctx);
1084 }
1085 sdb_llist_iter_destroy(iter);
1086 return ret;
1087 } /* sdb_plugin_shutdown_all */
1089 int
1090 sdb_plugin_collector_loop(sdb_plugin_loop_t *loop)
1091 {
1092 if (! collector_list) {
1093 sdb_log(SDB_LOG_WARNING, "core: No collectors registered. "
1094 "Quiting main loop.");
1095 return -1;
1096 }
1098 if (! loop)
1099 return -1;
1101 while (loop->do_loop) {
1102 sdb_plugin_collector_cb callback;
1103 ctx_t *old_ctx;
1105 sdb_time_t interval, now;
1107 sdb_object_t *obj = sdb_llist_shift(collector_list);
1108 if (! obj)
1109 return -1;
1111 callback = (sdb_plugin_collector_cb)CCB(obj)->ccb_callback;
1113 if (! (now = sdb_gettime())) {
1114 char errbuf[1024];
1115 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
1116 "time in collector main loop: %s",
1117 sdb_strerror(errno, errbuf, sizeof(errbuf)));
1118 now = CCB(obj)->ccb_next_update;
1119 }
1121 if (now < CCB(obj)->ccb_next_update) {
1122 interval = CCB(obj)->ccb_next_update - now;
1124 errno = 0;
1125 while (loop->do_loop && sdb_sleep(interval, &interval)) {
1126 if (errno != EINTR) {
1127 char errbuf[1024];
1128 sdb_log(SDB_LOG_ERR, "core: Failed to sleep "
1129 "in collector main loop: %s",
1130 sdb_strerror(errno, errbuf, sizeof(errbuf)));
1131 sdb_llist_insert_sorted(collector_list, obj,
1132 plugin_cmp_next_update);
1133 sdb_object_deref(obj);
1134 return -1;
1135 }
1136 errno = 0;
1137 }
1139 if (! loop->do_loop) {
1140 /* put back; don't worry about errors */
1141 sdb_llist_insert_sorted(collector_list, obj,
1142 plugin_cmp_next_update);
1143 sdb_object_deref(obj);
1144 return 0;
1145 }
1146 }
1148 old_ctx = ctx_set(CCB(obj)->ccb_ctx);
1149 if (callback(CCB(obj)->ccb_user_data)) {
1150 /* XXX */
1151 }
1152 ctx_set(old_ctx);
1154 interval = CCB(obj)->ccb_interval;
1155 if (! interval)
1156 interval = loop->default_interval;
1157 if (! interval) {
1158 sdb_log(SDB_LOG_WARNING, "core: No interval configured "
1159 "for plugin '%s'; skipping any further "
1160 "iterations.", obj->name);
1161 sdb_object_deref(obj);
1162 continue;
1163 }
1165 CCB(obj)->ccb_next_update += interval;
1167 if (! (now = sdb_gettime())) {
1168 char errbuf[1024];
1169 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
1170 "time in collector main loop: %s",
1171 sdb_strerror(errno, errbuf, sizeof(errbuf)));
1172 now = CCB(obj)->ccb_next_update;
1173 }
1175 if (now > CCB(obj)->ccb_next_update) {
1176 sdb_log(SDB_LOG_WARNING, "core: Plugin '%s' took too "
1177 "long; skipping iterations to keep up.",
1178 obj->name);
1179 CCB(obj)->ccb_next_update = now;
1180 }
1182 if (sdb_llist_insert_sorted(collector_list, obj,
1183 plugin_cmp_next_update)) {
1184 sdb_log(SDB_LOG_ERR, "core: Failed to re-insert "
1185 "plugin '%s' into collector list. Unable to further "
1186 "use the plugin.",
1187 obj->name);
1188 sdb_object_deref(obj);
1189 return -1;
1190 }
1192 /* pass control back to the list */
1193 sdb_object_deref(obj);
1194 }
1195 return 0;
1196 } /* sdb_plugin_read_loop */
1198 char *
1199 sdb_plugin_cname(char *hostname)
1200 {
1201 sdb_llist_iter_t *iter;
1203 if (! hostname)
1204 return NULL;
1206 if (! cname_list)
1207 return hostname;
1209 iter = sdb_llist_get_iter(cname_list);
1210 while (sdb_llist_iter_has_next(iter)) {
1211 sdb_plugin_cname_cb callback;
1212 char *cname;
1214 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
1215 assert(obj);
1217 callback = (sdb_plugin_cname_cb)CB(obj)->cb_callback;
1218 cname = callback(hostname, CB(obj)->cb_user_data);
1219 if (cname) {
1220 free(hostname);
1221 hostname = cname;
1222 }
1223 /* else: don't change hostname */
1224 }
1225 sdb_llist_iter_destroy(iter);
1226 return hostname;
1227 } /* sdb_plugin_cname */
1229 int
1230 sdb_plugin_log(int prio, const char *msg)
1231 {
1232 sdb_llist_iter_t *iter;
1233 int ret = -1;
1235 bool logged = 0;
1237 if (! msg)
1238 return 0;
1240 iter = sdb_llist_get_iter(log_list);
1241 while (sdb_llist_iter_has_next(iter)) {
1242 sdb_plugin_log_cb callback;
1243 int tmp;
1245 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
1246 assert(obj);
1248 callback = (sdb_plugin_log_cb)CB(obj)->cb_callback;
1249 tmp = callback(prio, msg, CB(obj)->cb_user_data);
1250 if (tmp > ret)
1251 ret = tmp;
1253 if (CB(obj)->cb_ctx)
1254 logged = 1;
1255 /* else: this is an internally registered callback */
1256 }
1257 sdb_llist_iter_destroy(iter);
1259 if (! logged)
1260 return fprintf(stderr, "[%s] %s\n", SDB_LOG_PRIO_TO_STRING(prio), msg);
1261 return ret;
1262 } /* sdb_plugin_log */
1264 int
1265 sdb_plugin_vlogf(int prio, const char *fmt, va_list ap)
1266 {
1267 sdb_strbuf_t *buf;
1268 int ret;
1270 if (! fmt)
1271 return 0;
1273 buf = sdb_strbuf_create(64);
1274 if (! buf) {
1275 ret = fprintf(stderr, "[%s] ", SDB_LOG_PRIO_TO_STRING(prio));
1276 ret += vfprintf(stderr, fmt, ap);
1277 return ret;
1278 }
1280 if (sdb_strbuf_vsprintf(buf, fmt, ap) < 0) {
1281 sdb_strbuf_destroy(buf);
1282 return -1;
1283 }
1285 ret = sdb_plugin_log(prio, sdb_strbuf_string(buf));
1286 sdb_strbuf_destroy(buf);
1287 return ret;
1288 } /* sdb_plugin_vlogf */
1290 int
1291 sdb_plugin_logf(int prio, const char *fmt, ...)
1292 {
1293 va_list ap;
1294 int ret;
1296 if (! fmt)
1297 return 0;
1299 va_start(ap, fmt);
1300 ret = sdb_plugin_vlogf(prio, fmt, ap);
1301 va_end(ap);
1302 return ret;
1303 } /* sdb_plugin_logf */
1305 sdb_timeseries_t *
1306 sdb_plugin_fetch_timeseries(const char *type, const char *id,
1307 sdb_timeseries_opts_t *opts)
1308 {
1309 callback_t *plugin;
1310 sdb_plugin_fetch_ts_cb callback;
1311 sdb_timeseries_t *ts;
1313 ctx_t *old_ctx;
1315 if ((! type) || (! id) || (! opts))
1316 return NULL;
1318 plugin = CB(sdb_llist_search_by_name(ts_fetcher_list, type));
1319 if (! plugin) {
1320 sdb_log(SDB_LOG_ERR, "core: Cannot fetch time-series of type %s: "
1321 "no such plugin loaded", type);
1322 errno = ENOENT;
1323 return NULL;
1324 }
1326 old_ctx = ctx_set(plugin->cb_ctx);
1327 callback = (sdb_plugin_fetch_ts_cb)plugin->cb_callback;
1328 ts = callback(id, opts, plugin->cb_user_data);
1329 ctx_set(old_ctx);
1330 return ts;
1331 } /* sdb_plugin_fetch_timeseries */
1333 int
1334 sdb_plugin_store_host(const char *name, sdb_time_t last_update)
1335 {
1336 sdb_llist_iter_t *iter;
1337 int status = 0;
1339 if (! name)
1340 return -1;
1342 iter = sdb_llist_get_iter(writer_list);
1343 while (sdb_llist_iter_has_next(iter)) {
1344 writer_t *writer = WRITER(sdb_llist_iter_get_next(iter));
1345 int s;
1346 assert(writer);
1347 s = writer->impl.store_host(name, last_update, writer->w_user_data);
1348 if (((s > 0) && (status >= 0)) || (s < 0))
1349 status = s;
1350 }
1351 sdb_llist_iter_destroy(iter);
1352 return status;
1353 } /* sdb_plugin_store_host */
1355 int
1356 sdb_plugin_store_service(const char *hostname, const char *name,
1357 sdb_time_t last_update)
1358 {
1359 sdb_llist_iter_t *iter;
1360 int status = 0;
1362 if ((! hostname) || (! name))
1363 return -1;
1365 iter = sdb_llist_get_iter(writer_list);
1366 while (sdb_llist_iter_has_next(iter)) {
1367 writer_t *writer = WRITER(sdb_llist_iter_get_next(iter));
1368 int s;
1369 assert(writer);
1370 s = writer->impl.store_service(hostname, name, last_update,
1371 writer->w_user_data);
1372 if (((s > 0) && (status >= 0)) || (s < 0))
1373 status = s;
1374 }
1375 sdb_llist_iter_destroy(iter);
1376 return status;
1377 } /* sdb_plugin_store_service */
1379 int
1380 sdb_plugin_store_metric(const char *hostname, const char *name,
1381 sdb_metric_store_t *store, sdb_time_t last_update)
1382 {
1383 sdb_llist_iter_t *iter;
1384 int status = 0;
1386 if ((! hostname) || (! name))
1387 return -1;
1389 if (store && ((! store->type) || (! store->id)))
1390 store = NULL;
1392 iter = sdb_llist_get_iter(writer_list);
1393 while (sdb_llist_iter_has_next(iter)) {
1394 writer_t *writer = WRITER(sdb_llist_iter_get_next(iter));
1395 int s;
1396 assert(writer);
1397 s = writer->impl.store_metric(hostname, name, store, last_update,
1398 writer->w_user_data);
1399 if (((s > 0) && (status >= 0)) || (s < 0))
1400 status = s;
1401 }
1402 sdb_llist_iter_destroy(iter);
1403 return status;
1404 } /* sdb_plugin_store_metric */
1406 int
1407 sdb_plugin_store_attribute(const char *hostname, const char *key,
1408 const sdb_data_t *value, sdb_time_t last_update)
1409 {
1410 sdb_llist_iter_t *iter;
1411 int status = 0;
1413 if ((! hostname) || (! key) || (! value))
1414 return -1;
1416 iter = sdb_llist_get_iter(writer_list);
1417 while (sdb_llist_iter_has_next(iter)) {
1418 writer_t *writer = WRITER(sdb_llist_iter_get_next(iter));
1419 int s;
1420 assert(writer);
1421 s = writer->impl.store_attribute(hostname, key, value, last_update,
1422 writer->w_user_data);
1423 if (((s > 0) && (status >= 0)) || (s < 0))
1424 status = s;
1425 }
1426 sdb_llist_iter_destroy(iter);
1427 return status;
1428 } /* sdb_plugin_store_attribute */
1430 int
1431 sdb_plugin_store_service_attribute(const char *hostname, const char *service,
1432 const char *key, const sdb_data_t *value, sdb_time_t last_update)
1433 {
1434 sdb_llist_iter_t *iter;
1435 int status = 0;
1437 if ((! hostname) || (! service) || (! key) || (! value))
1438 return -1;
1440 iter = sdb_llist_get_iter(writer_list);
1441 while (sdb_llist_iter_has_next(iter)) {
1442 writer_t *writer = WRITER(sdb_llist_iter_get_next(iter));
1443 int s;
1444 assert(writer);
1445 s = writer->impl.store_service_attr(hostname, service,
1446 key, value, last_update, writer->w_user_data);
1447 if (((s > 0) && (status >= 0)) || (s < 0))
1448 status = s;
1449 }
1450 sdb_llist_iter_destroy(iter);
1451 return status;
1452 } /* sdb_plugin_store_service_attribute */
1454 int
1455 sdb_plugin_store_metric_attribute(const char *hostname, const char *metric,
1456 const char *key, const sdb_data_t *value, sdb_time_t last_update)
1457 {
1458 sdb_llist_iter_t *iter;
1459 int status = 0;
1461 if ((! hostname) || (! metric) || (! key) || (! value))
1462 return -1;
1464 iter = sdb_llist_get_iter(writer_list);
1465 while (sdb_llist_iter_has_next(iter)) {
1466 writer_t *writer = WRITER(sdb_llist_iter_get_next(iter));
1467 int s;
1468 assert(writer);
1469 s = writer->impl.store_metric_attr(hostname, metric,
1470 key, value, last_update, writer->w_user_data);
1471 if (((s > 0) && (status >= 0)) || (s < 0))
1472 status = s;
1473 }
1474 sdb_llist_iter_destroy(iter);
1475 return status;
1476 } /* sdb_plugin_store_metric_attribute */
1478 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */