Code

919df2b3c982fed934b0495849817936907d9253
[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                 ctx_t *ctx = ctx_get();
756                 if (! ctx) {
757                         sdb_log(SDB_LOG_ERR, "core: Cannot determine interval "
758                                         "for collector %s; none specified and no plugin "
759                                         "context found", cb_name);
760                         return -1;
761                 }
763                 SDB_PLUGIN_CCB(obj)->ccb_interval = ctx->public.interval;
764         }
766         if (! (SDB_PLUGIN_CCB(obj)->ccb_next_update = sdb_gettime())) {
767                 char errbuf[1024];
768                 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
769                                 "time: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
770                 sdb_object_deref(obj);
771                 return -1;
772         }
774         if (sdb_llist_insert_sorted(collector_list, obj,
775                                 plugin_cmp_next_update)) {
776                 sdb_object_deref(obj);
777                 return -1;
778         }
780         /* pass control to the list */
781         sdb_object_deref(obj);
783         sdb_log(SDB_LOG_INFO, "core: Registered collector callback '%s' "
784                         "(interval = %.3fs).", cb_name,
785                         SDB_TIME_TO_DOUBLE(SDB_PLUGIN_CCB(obj)->ccb_interval));
786         return 0;
787 } /* sdb_plugin_register_collector */
789 sdb_plugin_ctx_t
790 sdb_plugin_get_ctx(void)
792         ctx_t *c;
794         c = ctx_get();
795         if (! c) {
796                 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid read access to plugin "
797                                 "context outside a plugin");
798                 return plugin_default_ctx;
799         }
800         return c->public;
801 } /* sdb_plugin_get_ctx */
803 int
804 sdb_plugin_set_ctx(sdb_plugin_ctx_t ctx, sdb_plugin_ctx_t *old)
806         ctx_t *c;
808         c = ctx_get();
809         if (! c) {
810                 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid write access to plugin "
811                                 "context outside a plugin");
812                 return -1;
813         }
815         if (old)
816                 *old = c->public;
817         c->public = ctx;
818         return 0;
819 } /* sdb_plugin_set_ctx */
821 const sdb_plugin_info_t *
822 sdb_plugin_current(void)
824         ctx_t *ctx = ctx_get();
826         if (! ctx)
827                 return NULL;
828         return &ctx->info;
829 } /* sdb_plugin_current */
831 int
832 sdb_plugin_configure(const char *name, oconfig_item_t *ci)
834         sdb_plugin_cb_t *plugin;
835         sdb_plugin_config_cb callback;
837         ctx_t *old_ctx;
839         int status;
841         if ((! name) || (! ci))
842                 return -1;
844         plugin = SDB_PLUGIN_CB(sdb_llist_search_by_name(config_list, name));
845         if (! plugin) {
846                 ctx_t *ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
847                 if (! ctx)
848                         sdb_log(SDB_LOG_ERR, "core: Cannot configure unknown "
849                                         "plugin '%s'. Missing 'LoadPlugin \"%s\"'?",
850                                         name, name);
851                 else
852                         sdb_log(SDB_LOG_ERR, "core: Plugin '%s' did not register "
853                                         "a config callback.", name);
854                 errno = ENOENT;
855                 return -1;
856         }
858         old_ctx = ctx_set(plugin->cb_ctx);
859         callback = (sdb_plugin_config_cb)plugin->cb_callback;
860         status = callback(ci);
861         ctx_set(old_ctx);
862         return status;
863 } /* sdb_plugin_configure */
865 int
866 sdb_plugin_reconfigure_init(void)
868         sdb_llist_iter_t *iter;
870         iter = sdb_llist_get_iter(config_list);
871         if (config_list && (! iter))
872                 return -1;
874         /* deconfigure all plugins */
875         while (sdb_llist_iter_has_next(iter)) {
876                 sdb_plugin_cb_t *plugin;
877                 sdb_plugin_config_cb callback;
878                 ctx_t *old_ctx;
880                 plugin = SDB_PLUGIN_CB(sdb_llist_iter_get_next(iter));
881                 old_ctx = ctx_set(plugin->cb_ctx);
882                 callback = (sdb_plugin_config_cb)plugin->cb_callback;
883                 callback(NULL);
884                 ctx_set(old_ctx);
885         }
886         sdb_llist_iter_destroy(iter);
888         iter = sdb_llist_get_iter(all_plugins);
889         if (all_plugins && (! iter))
890                 return -1;
892         /* record all plugins as being unused */
893         while (sdb_llist_iter_has_next(iter))
894                 CTX(sdb_llist_iter_get_next(iter))->use_cnt = 0;
895         sdb_llist_iter_destroy(iter);
897         plugin_unregister_all();
898         return 0;
899 } /* sdb_plugin_reconfigure_init */
901 int
902 sdb_plugin_reconfigure_finish(void)
904         sdb_llist_iter_t *iter;
906         iter = sdb_llist_get_iter(all_plugins);
907         if (all_plugins && (! iter))
908                 return -1;
910         while (sdb_llist_iter_has_next(iter)) {
911                 ctx_t *ctx = CTX(sdb_llist_iter_get_next(iter));
912                 if (ctx->use_cnt)
913                         continue;
915                 sdb_log(SDB_LOG_INFO, "core: Module %s no longer in use",
916                                 ctx->info.plugin_name);
917                 sdb_llist_iter_remove_current(iter);
918                 plugin_unregister_by_name(ctx->info.plugin_name);
919                 sdb_object_deref(SDB_OBJ(ctx));
920         }
921         sdb_llist_iter_destroy(iter);
922         return 0;
923 } /* sdb_plugin_reconfigure_finish */
925 int
926 sdb_plugin_init_all(void)
928         sdb_llist_iter_t *iter;
929         int ret = 0;
931         iter = sdb_llist_get_iter(init_list);
932         while (sdb_llist_iter_has_next(iter)) {
933                 sdb_plugin_cb_t *cb;
934                 sdb_plugin_init_cb callback;
935                 ctx_t *old_ctx;
937                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
938                 assert(obj);
939                 cb = SDB_PLUGIN_CB(obj);
941                 callback = (sdb_plugin_init_cb)cb->cb_callback;
943                 old_ctx = ctx_set(cb->cb_ctx);
944                 if (callback(cb->cb_user_data)) {
945                         sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin "
946                                         "'%s'. Unregistering all callbacks.", obj->name);
947                         ctx_set(old_ctx);
948                         plugin_unregister_by_name(cb->cb_ctx->info.plugin_name);
949                         ++ret;
950                 }
951                 else
952                         ctx_set(old_ctx);
953         }
954         sdb_llist_iter_destroy(iter);
955         return ret;
956 } /* sdb_plugin_init_all */
958 int
959 sdb_plugin_shutdown_all(void)
961         sdb_llist_iter_t *iter;
962         int ret = 0;
964         iter = sdb_llist_get_iter(shutdown_list);
965         while (sdb_llist_iter_has_next(iter)) {
966                 sdb_plugin_cb_t *cb;
967                 sdb_plugin_shutdown_cb callback;
968                 ctx_t *old_ctx;
970                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
971                 assert(obj);
972                 cb = SDB_PLUGIN_CB(obj);
974                 callback = (sdb_plugin_shutdown_cb)cb->cb_callback;
976                 old_ctx = ctx_set(cb->cb_ctx);
977                 if (callback(cb->cb_user_data)) {
978                         sdb_log(SDB_LOG_ERR, "core: Failed to shutdown plugin '%s'.",
979                                         obj->name);
980                         ++ret;
981                 }
982                 ctx_set(old_ctx);
983         }
984         sdb_llist_iter_destroy(iter);
985         return ret;
986 } /* sdb_plugin_shutdown_all */
988 int
989 sdb_plugin_collector_loop(sdb_plugin_loop_t *loop)
991         if (! collector_list) {
992                 sdb_log(SDB_LOG_WARNING, "core: No collectors registered. "
993                                 "Quiting main loop.");
994                 return -1;
995         }
997         if (! loop)
998                 return -1;
1000         while (loop->do_loop) {
1001                 sdb_plugin_collector_cb callback;
1002                 ctx_t *old_ctx;
1004                 sdb_time_t interval, now;
1006                 sdb_object_t *obj = sdb_llist_shift(collector_list);
1007                 if (! obj)
1008                         return -1;
1010                 callback = (sdb_plugin_collector_cb)SDB_PLUGIN_CCB(obj)->ccb_callback;
1012                 if (! (now = sdb_gettime())) {
1013                         char errbuf[1024];
1014                         sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
1015                                         "time in collector main loop: %s",
1016                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
1017                         now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
1018                 }
1020                 if (now < SDB_PLUGIN_CCB(obj)->ccb_next_update) {
1021                         interval = SDB_PLUGIN_CCB(obj)->ccb_next_update - now;
1023                         errno = 0;
1024                         while (loop->do_loop && sdb_sleep(interval, &interval)) {
1025                                 if (errno != EINTR) {
1026                                         char errbuf[1024];
1027                                         sdb_log(SDB_LOG_ERR, "core: Failed to sleep "
1028                                                         "in collector main loop: %s",
1029                                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
1030                                         sdb_llist_insert_sorted(collector_list, obj,
1031                                                         plugin_cmp_next_update);
1032                                         sdb_object_deref(obj);
1033                                         return -1;
1034                                 }
1035                                 errno = 0;
1036                         }
1038                         if (! loop->do_loop) {
1039                                 /* put back; don't worry about errors */
1040                                 sdb_llist_insert_sorted(collector_list, obj,
1041                                                 plugin_cmp_next_update);
1042                                 sdb_object_deref(obj);
1043                                 return 0;
1044                         }
1045                 }
1047                 old_ctx = ctx_set(SDB_PLUGIN_CCB(obj)->ccb_ctx);
1048                 if (callback(SDB_PLUGIN_CCB(obj)->ccb_user_data)) {
1049                         /* XXX */
1050                 }
1051                 ctx_set(old_ctx);
1053                 interval = SDB_PLUGIN_CCB(obj)->ccb_interval;
1054                 if (! interval)
1055                         interval = loop->default_interval;
1056                 if (! interval) {
1057                         sdb_log(SDB_LOG_WARNING, "core: No interval configured "
1058                                         "for plugin '%s'; skipping any further "
1059                                         "iterations.", obj->name);
1060                         sdb_object_deref(obj);
1061                         continue;
1062                 }
1064                 SDB_PLUGIN_CCB(obj)->ccb_next_update += interval;
1066                 if (! (now = sdb_gettime())) {
1067                         char errbuf[1024];
1068                         sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
1069                                         "time in collector main loop: %s",
1070                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
1071                         now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
1072                 }
1074                 if (now > SDB_PLUGIN_CCB(obj)->ccb_next_update) {
1075                         sdb_log(SDB_LOG_WARNING, "core: Plugin '%s' took too "
1076                                         "long; skipping iterations to keep up.",
1077                                         obj->name);
1078                         SDB_PLUGIN_CCB(obj)->ccb_next_update = now;
1079                 }
1081                 if (sdb_llist_insert_sorted(collector_list, obj,
1082                                         plugin_cmp_next_update)) {
1083                         sdb_log(SDB_LOG_ERR, "core: Failed to re-insert "
1084                                         "plugin '%s' into collector list. Unable to further "
1085                                         "use the plugin.",
1086                                         obj->name);
1087                         sdb_object_deref(obj);
1088                         return -1;
1089                 }
1091                 /* pass control back to the list */
1092                 sdb_object_deref(obj);
1093         }
1094         return 0;
1095 } /* sdb_plugin_read_loop */
1097 char *
1098 sdb_plugin_cname(char *hostname)
1100         sdb_llist_iter_t *iter;
1102         if (! hostname)
1103                 return NULL;
1105         if (! cname_list)
1106                 return hostname;
1108         iter = sdb_llist_get_iter(cname_list);
1109         while (sdb_llist_iter_has_next(iter)) {
1110                 sdb_plugin_cname_cb callback;
1111                 char *cname;
1113                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
1114                 assert(obj);
1116                 callback = (sdb_plugin_cname_cb)SDB_PLUGIN_CB(obj)->cb_callback;
1117                 cname = callback(hostname, SDB_PLUGIN_CB(obj)->cb_user_data);
1118                 if (cname) {
1119                         free(hostname);
1120                         hostname = cname;
1121                 }
1122                 /* else: don't change hostname */
1123         }
1124         sdb_llist_iter_destroy(iter);
1125         return hostname;
1126 } /* sdb_plugin_cname */
1128 int
1129 sdb_plugin_log(int prio, const char *msg)
1131         sdb_llist_iter_t *iter;
1132         int ret = -1;
1134         _Bool logged = 0;
1136         if (! msg)
1137                 return 0;
1139         iter = sdb_llist_get_iter(log_list);
1140         while (sdb_llist_iter_has_next(iter)) {
1141                 sdb_plugin_log_cb callback;
1142                 int tmp;
1144                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
1145                 assert(obj);
1147                 callback = (sdb_plugin_log_cb)SDB_PLUGIN_CB(obj)->cb_callback;
1148                 tmp = callback(prio, msg, SDB_PLUGIN_CB(obj)->cb_user_data);
1149                 if (tmp > ret)
1150                         ret = tmp;
1152                 if (SDB_PLUGIN_CB(obj)->cb_ctx)
1153                         logged = 1;
1154                 /* else: this is an internally registered callback */
1155         }
1156         sdb_llist_iter_destroy(iter);
1158         if (! logged)
1159                 return fprintf(stderr, "[%s] %s\n", SDB_LOG_PRIO_TO_STRING(prio), msg);
1160         return ret;
1161 } /* sdb_plugin_log */
1163 int
1164 sdb_plugin_vlogf(int prio, const char *fmt, va_list ap)
1166         sdb_strbuf_t *buf;
1167         int ret;
1169         if (! fmt)
1170                 return 0;
1172         buf = sdb_strbuf_create(64);
1173         if (! buf) {
1174                 ret = fprintf(stderr, "[%s] ", SDB_LOG_PRIO_TO_STRING(prio));
1175                 ret += vfprintf(stderr, fmt, ap);
1176                 return ret;
1177         }
1179         if (sdb_strbuf_vsprintf(buf, fmt, ap) < 0) {
1180                 sdb_strbuf_destroy(buf);
1181                 return -1;
1182         }
1184         ret = sdb_plugin_log(prio, sdb_strbuf_string(buf));
1185         sdb_strbuf_destroy(buf);
1186         return ret;
1187 } /* sdb_plugin_vlogf */
1189 int
1190 sdb_plugin_logf(int prio, const char *fmt, ...)
1192         va_list ap;
1193         int ret;
1195         if (! fmt)
1196                 return 0;
1198         va_start(ap, fmt);
1199         ret = sdb_plugin_vlogf(prio, fmt, ap);
1200         va_end(ap);
1201         return ret;
1202 } /* sdb_plugin_logf */
1204 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */