Code

f10a904169f5d3993dc89894535e6a7b4ca01513
[sysdb.git] / src / core / plugin.c
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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, ...)
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)
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)
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)
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)
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)
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)
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)
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)
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 int
818 sdb_plugin_configure(const char *name, oconfig_item_t *ci)
820         sdb_plugin_cb_t *plugin;
821         sdb_plugin_config_cb callback;
823         ctx_t *old_ctx;
825         int status;
827         if ((! name) || (! ci))
828                 return -1;
830         plugin = SDB_PLUGIN_CB(sdb_llist_search_by_name(config_list, name));
831         if (! plugin) {
832                 ctx_t *ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
833                 if (! ctx)
834                         sdb_log(SDB_LOG_ERR, "core: Cannot configure unknown "
835                                         "plugin '%s'. Missing 'LoadPlugin \"%s\"'?",
836                                         name, name);
837                 else
838                         sdb_log(SDB_LOG_ERR, "core: Plugin '%s' did not register "
839                                         "a config callback.", name);
840                 errno = ENOENT;
841                 return -1;
842         }
844         old_ctx = ctx_set(plugin->cb_ctx);
845         callback = (sdb_plugin_config_cb)plugin->cb_callback;
846         status = callback(ci);
847         ctx_set(old_ctx);
848         return status;
849 } /* sdb_plugin_configure */
851 int
852 sdb_plugin_reconfigure_init(void)
854         sdb_llist_iter_t *iter;
856         iter = sdb_llist_get_iter(config_list);
857         if (config_list && (! iter))
858                 return -1;
860         /* deconfigure all plugins */
861         while (sdb_llist_iter_has_next(iter)) {
862                 sdb_plugin_cb_t *plugin;
863                 sdb_plugin_config_cb callback;
864                 ctx_t *old_ctx;
866                 plugin = SDB_PLUGIN_CB(sdb_llist_iter_get_next(iter));
867                 old_ctx = ctx_set(plugin->cb_ctx);
868                 callback = (sdb_plugin_config_cb)plugin->cb_callback;
869                 callback(NULL);
870                 ctx_set(old_ctx);
871         }
872         sdb_llist_iter_destroy(iter);
874         iter = sdb_llist_get_iter(all_plugins);
875         if (all_plugins && (! iter))
876                 return -1;
878         /* record all plugins as being unused */
879         while (sdb_llist_iter_has_next(iter))
880                 CTX(sdb_llist_iter_get_next(iter))->use_cnt = 0;
881         sdb_llist_iter_destroy(iter);
883         plugin_unregister_all();
884         return 0;
885 } /* sdb_plugin_reconfigure_init */
887 int
888 sdb_plugin_reconfigure_finish(void)
890         sdb_llist_iter_t *iter;
892         iter = sdb_llist_get_iter(all_plugins);
893         if (all_plugins && (! iter))
894                 return -1;
896         while (sdb_llist_iter_has_next(iter)) {
897                 ctx_t *ctx = CTX(sdb_llist_iter_get_next(iter));
898                 if (ctx->use_cnt)
899                         continue;
901                 sdb_log(SDB_LOG_INFO, "core: Module %s no longer in use",
902                                 ctx->info.plugin_name);
903                 sdb_llist_iter_remove_current(iter);
904                 plugin_unregister_by_name(ctx->info.plugin_name);
905                 sdb_object_deref(SDB_OBJ(ctx));
906         }
907         sdb_llist_iter_destroy(iter);
908         return 0;
909 } /* sdb_plugin_reconfigure_finish */
911 int
912 sdb_plugin_init_all(void)
914         sdb_llist_iter_t *iter;
915         int ret = 0;
917         iter = sdb_llist_get_iter(init_list);
918         while (sdb_llist_iter_has_next(iter)) {
919                 sdb_plugin_cb_t *cb;
920                 sdb_plugin_init_cb callback;
921                 ctx_t *old_ctx;
923                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
924                 assert(obj);
925                 cb = SDB_PLUGIN_CB(obj);
927                 callback = (sdb_plugin_init_cb)cb->cb_callback;
929                 old_ctx = ctx_set(cb->cb_ctx);
930                 if (callback(cb->cb_user_data)) {
931                         sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin "
932                                         "'%s'. Unregistering all callbacks.", obj->name);
933                         ctx_set(old_ctx);
934                         plugin_unregister_by_name(cb->cb_ctx->info.plugin_name);
935                         ++ret;
936                 }
937                 else
938                         ctx_set(old_ctx);
939         }
940         sdb_llist_iter_destroy(iter);
941         return ret;
942 } /* sdb_plugin_init_all */
944 int
945 sdb_plugin_shutdown_all(void)
947         sdb_llist_iter_t *iter;
948         int ret = 0;
950         iter = sdb_llist_get_iter(shutdown_list);
951         while (sdb_llist_iter_has_next(iter)) {
952                 sdb_plugin_cb_t *cb;
953                 sdb_plugin_shutdown_cb callback;
954                 ctx_t *old_ctx;
956                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
957                 assert(obj);
958                 cb = SDB_PLUGIN_CB(obj);
960                 callback = (sdb_plugin_shutdown_cb)cb->cb_callback;
962                 old_ctx = ctx_set(cb->cb_ctx);
963                 if (callback(cb->cb_user_data)) {
964                         sdb_log(SDB_LOG_ERR, "core: Failed to shutdown plugin '%s'.",
965                                         obj->name);
966                         ++ret;
967                 }
968                 ctx_set(old_ctx);
969         }
970         sdb_llist_iter_destroy(iter);
971         return ret;
972 } /* sdb_plugin_shutdown_all */
974 int
975 sdb_plugin_collector_loop(sdb_plugin_loop_t *loop)
977         if (! collector_list) {
978                 sdb_log(SDB_LOG_WARNING, "core: No collectors registered. "
979                                 "Quiting main loop.");
980                 return -1;
981         }
983         if (! loop)
984                 return -1;
986         while (loop->do_loop) {
987                 sdb_plugin_collector_cb callback;
988                 ctx_t *old_ctx;
990                 sdb_time_t interval, now;
992                 sdb_object_t *obj = sdb_llist_shift(collector_list);
993                 if (! obj)
994                         return -1;
996                 callback = (sdb_plugin_collector_cb)SDB_PLUGIN_CCB(obj)->ccb_callback;
998                 if (! (now = sdb_gettime())) {
999                         char errbuf[1024];
1000                         sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
1001                                         "time in collector main loop: %s",
1002                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
1003                         now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
1004                 }
1006                 if (now < SDB_PLUGIN_CCB(obj)->ccb_next_update) {
1007                         interval = SDB_PLUGIN_CCB(obj)->ccb_next_update - now;
1009                         errno = 0;
1010                         while (loop->do_loop && sdb_sleep(interval, &interval)) {
1011                                 if (errno != EINTR) {
1012                                         char errbuf[1024];
1013                                         sdb_log(SDB_LOG_ERR, "core: Failed to sleep "
1014                                                         "in collector main loop: %s",
1015                                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
1016                                         sdb_llist_insert_sorted(collector_list, obj,
1017                                                         plugin_cmp_next_update);
1018                                         sdb_object_deref(obj);
1019                                         return -1;
1020                                 }
1021                                 errno = 0;
1022                         }
1024                         if (! loop->do_loop) {
1025                                 /* put back; don't worry about errors */
1026                                 sdb_llist_insert_sorted(collector_list, obj,
1027                                                 plugin_cmp_next_update);
1028                                 sdb_object_deref(obj);
1029                                 return 0;
1030                         }
1031                 }
1033                 old_ctx = ctx_set(SDB_PLUGIN_CCB(obj)->ccb_ctx);
1034                 if (callback(SDB_PLUGIN_CCB(obj)->ccb_user_data)) {
1035                         /* XXX */
1036                 }
1037                 ctx_set(old_ctx);
1039                 interval = SDB_PLUGIN_CCB(obj)->ccb_interval;
1040                 if (! interval)
1041                         interval = loop->default_interval;
1042                 if (! interval) {
1043                         sdb_log(SDB_LOG_WARNING, "core: No interval configured "
1044                                         "for plugin '%s'; skipping any further "
1045                                         "iterations.", obj->name);
1046                         sdb_object_deref(obj);
1047                         continue;
1048                 }
1050                 SDB_PLUGIN_CCB(obj)->ccb_next_update += interval;
1052                 if (! (now = sdb_gettime())) {
1053                         char errbuf[1024];
1054                         sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
1055                                         "time in collector main loop: %s",
1056                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
1057                         now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
1058                 }
1060                 if (now > SDB_PLUGIN_CCB(obj)->ccb_next_update) {
1061                         sdb_log(SDB_LOG_WARNING, "core: Plugin '%s' took too "
1062                                         "long; skipping iterations to keep up.",
1063                                         obj->name);
1064                         SDB_PLUGIN_CCB(obj)->ccb_next_update = now;
1065                 }
1067                 if (sdb_llist_insert_sorted(collector_list, obj,
1068                                         plugin_cmp_next_update)) {
1069                         sdb_log(SDB_LOG_ERR, "core: Failed to re-insert "
1070                                         "plugin '%s' into collector list. Unable to further "
1071                                         "use the plugin.",
1072                                         obj->name);
1073                         sdb_object_deref(obj);
1074                         return -1;
1075                 }
1077                 /* pass control back to the list */
1078                 sdb_object_deref(obj);
1079         }
1080         return 0;
1081 } /* sdb_plugin_read_loop */
1083 char *
1084 sdb_plugin_cname(char *hostname)
1086         sdb_llist_iter_t *iter;
1088         if (! hostname)
1089                 return NULL;
1091         if (! cname_list)
1092                 return hostname;
1094         iter = sdb_llist_get_iter(cname_list);
1095         while (sdb_llist_iter_has_next(iter)) {
1096                 sdb_plugin_cname_cb callback;
1097                 char *cname;
1099                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
1100                 assert(obj);
1102                 callback = (sdb_plugin_cname_cb)SDB_PLUGIN_CB(obj)->cb_callback;
1103                 cname = callback(hostname, SDB_PLUGIN_CB(obj)->cb_user_data);
1104                 if (cname) {
1105                         free(hostname);
1106                         hostname = cname;
1107                 }
1108                 /* else: don't change hostname */
1109         }
1110         sdb_llist_iter_destroy(iter);
1111         return hostname;
1112 } /* sdb_plugin_cname */
1114 int
1115 sdb_plugin_log(int prio, const char *msg)
1117         sdb_llist_iter_t *iter;
1118         int ret = -1;
1120         _Bool logged = 0;
1122         if (! msg)
1123                 return 0;
1125         iter = sdb_llist_get_iter(log_list);
1126         while (sdb_llist_iter_has_next(iter)) {
1127                 sdb_plugin_log_cb callback;
1128                 int tmp;
1130                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
1131                 assert(obj);
1133                 callback = (sdb_plugin_log_cb)SDB_PLUGIN_CB(obj)->cb_callback;
1134                 tmp = callback(prio, msg, SDB_PLUGIN_CB(obj)->cb_user_data);
1135                 if (tmp > ret)
1136                         ret = tmp;
1138                 if (SDB_PLUGIN_CB(obj)->cb_ctx)
1139                         logged = 1;
1140                 /* else: this is an internally registered callback */
1141         }
1142         sdb_llist_iter_destroy(iter);
1144         if (! logged)
1145                 return fprintf(stderr, "[%s] %s\n", SDB_LOG_PRIO_TO_STRING(prio), msg);
1146         return ret;
1147 } /* sdb_plugin_log */
1149 int
1150 sdb_plugin_vlogf(int prio, const char *fmt, va_list ap)
1152         sdb_strbuf_t *buf;
1153         int ret;
1155         if (! fmt)
1156                 return 0;
1158         buf = sdb_strbuf_create(64);
1159         if (! buf) {
1160                 ret = fprintf(stderr, "[%s] ", SDB_LOG_PRIO_TO_STRING(prio));
1161                 ret += vfprintf(stderr, fmt, ap);
1162                 return ret;
1163         }
1165         if (sdb_strbuf_vsprintf(buf, fmt, ap) < 0) {
1166                 sdb_strbuf_destroy(buf);
1167                 return -1;
1168         }
1170         ret = sdb_plugin_log(prio, sdb_strbuf_string(buf));
1171         sdb_strbuf_destroy(buf);
1172         return ret;
1173 } /* sdb_plugin_vlogf */
1175 int
1176 sdb_plugin_logf(int prio, const char *fmt, ...)
1178         va_list ap;
1179         int ret;
1181         if (! fmt)
1182                 return 0;
1184         va_start(ap, fmt);
1185         ret = sdb_plugin_vlogf(prio, fmt, ap);
1186         va_end(ap);
1187         return ret;
1188 } /* sdb_plugin_logf */
1190 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */