Code

873e64a19436903c379bff983565c825008f2a43
[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 /*
54  * private data types
55  */
57 struct sdb_plugin_info {
58         char *plugin_name;
59         char *filename;
61         /* public attributes */
62         char *name;
64         char *description;
65         char *copyright;
66         char *license;
68         int   version;
69         int   plugin_version;
70 };
71 #define SDB_PLUGIN_INFO_INIT { \
72         /* plugin_name */ NULL, /* filename */ NULL, \
73         /* name */ NULL, /* desc */ NULL, \
74         /* copyright */ NULL, /* license */ NULL, \
75         /* version */ -1, /* plugin_version */ -1 }
76 #define INFO_GET(i, attr) \
77         ((i)->attr ? (i)->attr : #attr" not set")
79 typedef struct {
80         sdb_object_t super;
81         sdb_plugin_ctx_t public;
83         sdb_plugin_info_t info;
84         lt_dlhandle handle;
86         /* The usage count differs from the object's ref count
87          * in that it provides higher level information about how
88          * the plugin is in use. */
89         size_t use_cnt;
90 } ctx_t;
91 #define CTX_INIT { SDB_OBJECT_INIT, \
92         SDB_PLUGIN_CTX_INIT, SDB_PLUGIN_INFO_INIT, NULL, 0 }
94 #define CTX(obj) ((ctx_t *)(obj))
96 typedef struct {
97         sdb_object_t super;
98         void *cb_callback;
99         sdb_object_t *cb_user_data;
100         ctx_t *cb_ctx;
101 } sdb_plugin_cb_t;
102 #define SDB_PLUGIN_CB_INIT { SDB_OBJECT_INIT, \
103         /* callback = */ NULL, /* user_data = */ NULL, \
104         SDB_PLUGIN_CTX_INIT }
106 typedef struct {
107         sdb_plugin_cb_t super;
108 #define ccb_callback super.cb_callback
109 #define ccb_user_data super.cb_user_data
110 #define ccb_ctx super.cb_ctx
111         sdb_time_t ccb_interval;
112         sdb_time_t ccb_next_update;
113 } sdb_plugin_collector_cb_t;
115 #define SDB_PLUGIN_CB(obj) ((sdb_plugin_cb_t *)(obj))
116 #define SDB_CONST_PLUGIN_CB(obj) ((const sdb_plugin_cb_t *)(obj))
117 #define SDB_PLUGIN_CCB(obj) ((sdb_plugin_collector_cb_t *)(obj))
118 #define SDB_CONST_PLUGIN_CCB(obj) ((const sdb_plugin_collector_cb_t *)(obj))
120 /*
121  * private variables
122  */
124 static sdb_plugin_ctx_t  plugin_default_ctx  = SDB_PLUGIN_CTX_INIT;
125 static sdb_plugin_info_t plugin_default_info = SDB_PLUGIN_INFO_INIT;
127 static pthread_key_t     plugin_ctx_key;
128 static _Bool             plugin_ctx_key_initialized = 0;
130 /* a list of the plugin contexts of all registered plugins */
131 static sdb_llist_t      *all_plugins = NULL;
133 static sdb_llist_t      *config_list = NULL;
134 static sdb_llist_t      *init_list = NULL;
135 static sdb_llist_t      *collector_list = NULL;
136 static sdb_llist_t      *cname_list = NULL;
137 static sdb_llist_t      *shutdown_list = NULL;
138 static sdb_llist_t      *log_list = NULL;
140 static struct {
141         const char   *type;
142         sdb_llist_t **list;
143 } all_lists[] = {
144         { "config",    &config_list },
145         { "init",      &init_list },
146         { "collector", &collector_list },
147         { "cname",     &cname_list },
148         { "shutdown",  &shutdown_list },
149         { "log",       &log_list },
150 };
152 /*
153  * private helper functions
154  */
156 static void
157 plugin_info_clear(sdb_plugin_info_t *info)
159         sdb_plugin_info_t empty_info = SDB_PLUGIN_INFO_INIT;
160         if (! info)
161                 return;
163         if (info->plugin_name)
164                 free(info->plugin_name);
165         if (info->filename)
166                 free(info->filename);
168         if (info->name)
169                 free(info->name);
170         if (info->description)
171                 free(info->description);
172         if (info->copyright)
173                 free(info->copyright);
174         if (info->license)
175                 free(info->license);
177         *info = empty_info;
178 } /* plugin_info_clear */
180 static void
181 ctx_key_init(void)
183         if (plugin_ctx_key_initialized)
184                 return;
186         pthread_key_create(&plugin_ctx_key, /* destructor */ NULL);
187         plugin_ctx_key_initialized = 1;
188 } /* ctx_key_init */
190 static int
191 plugin_cmp_next_update(const sdb_object_t *a, const sdb_object_t *b)
193         const sdb_plugin_collector_cb_t *ccb1
194                 = (const sdb_plugin_collector_cb_t *)a;
195         const sdb_plugin_collector_cb_t *ccb2
196                 = (const sdb_plugin_collector_cb_t *)b;
198         assert(ccb1 && ccb2);
200         return (ccb1->ccb_next_update > ccb2->ccb_next_update)
201                 ? 1 : (ccb1->ccb_next_update < ccb2->ccb_next_update)
202                 ? -1 : 0;
203 } /* plugin_cmp_next_update */
205 static int
206 plugin_lookup_by_name(const sdb_object_t *obj, const void *id)
208         const sdb_plugin_cb_t *cb = SDB_CONST_PLUGIN_CB(obj);
209         const char *name = id;
211         assert(cb && id);
213         /* when a plugin was registered from outside a plugin (e.g. the core),
214          * we don't have a plugin context */
215         if (! cb->cb_ctx)
216                 return 1;
218         if (!strcasecmp(cb->cb_ctx->info.plugin_name, name))
219                 return 0;
220         return 1;
221 } /* plugin_lookup_by_name */
223 /* since this function is called from sdb_plugin_reconfigure_finish()
224  * when iterating through all_plugins, we may not do any additional
225  * modifications to all_plugins except for the optional removal */
226 static void
227 plugin_unregister_by_name(const char *plugin_name)
229         sdb_object_t *obj;
230         size_t i;
232         for (i = 0; i < SDB_STATIC_ARRAY_LEN(all_lists); ++i) {
233                 const char  *type =  all_lists[i].type;
234                 sdb_llist_t *list = *all_lists[i].list;
236                 while (1) {
237                         sdb_plugin_cb_t *cb;
239                         cb = SDB_PLUGIN_CB(sdb_llist_remove(list,
240                                                 plugin_lookup_by_name, plugin_name));
241                         if (! cb)
242                                 break;
244                         assert(cb->cb_ctx);
246                         sdb_log(SDB_LOG_INFO, "core: Unregistering "
247                                         "%s callback '%s' (module %s)", type, cb->super.name,
248                                         cb->cb_ctx->info.plugin_name);
249                         sdb_object_deref(SDB_OBJ(cb));
250                 }
251         }
253         obj = sdb_llist_search_by_name(all_plugins, plugin_name);
254         /* when called from sdb_plugin_reconfigure_finish, the object has already
255          * been removed from the list */
256         if (obj && (obj->ref_cnt <= 1)) {
257                 sdb_llist_remove_by_name(all_plugins, plugin_name);
258                 sdb_object_deref(obj);
259         }
260         /* else: other callbacks still reference it */
261 } /* plugin_unregister_by_name */
263 static void
264 plugin_unregister_all(void)
266         size_t i;
268         for (i = 0; i < SDB_STATIC_ARRAY_LEN(all_lists); ++i) {
269                 const char  *type =  all_lists[i].type;
270                 sdb_llist_t *list = *all_lists[i].list;
272                 size_t len = sdb_llist_len(list);
274                 if (! len)
275                         continue;
277                 sdb_llist_clear(list);
278                 sdb_log(SDB_LOG_INFO, "core: Unregistered %zu %s callback%s",
279                                 len, type, len == 1 ? "" : "s");
280         }
281 } /* plugin_unregister_all */
283 /*
284  * private types
285  */
287 static int
288 ctx_init(sdb_object_t *obj, va_list __attribute__((unused)) ap)
290         ctx_t *ctx = CTX(obj);
292         assert(ctx);
294         ctx->public = plugin_default_ctx;
295         ctx->info = plugin_default_info;
296         ctx->handle = NULL;
297         ctx->use_cnt = 1;
298         return 0;
299 } /* ctx_init */
301 static void
302 ctx_destroy(sdb_object_t *obj)
304         ctx_t *ctx = CTX(obj);
306         if (ctx->handle) {
307                 const char *err;
309                 sdb_log(SDB_LOG_INFO, "core: Unloading module %s",
310                                 ctx->info.plugin_name);
312                 lt_dlerror();
313                 lt_dlclose(ctx->handle);
314                 if ((err = lt_dlerror()))
315                         sdb_log(SDB_LOG_WARNING, "core: Failed to unload module %s: %s",
316                                         ctx->info.plugin_name, err);
317         }
319         plugin_info_clear(&ctx->info);
320 } /* ctx_destroy */
322 static sdb_type_t ctx_type = {
323         sizeof(ctx_t),
325         ctx_init,
326         ctx_destroy
327 };
329 static ctx_t *
330 ctx_get(void)
332         if (! plugin_ctx_key_initialized)
333                 ctx_key_init();
334         return pthread_getspecific(plugin_ctx_key);
335 } /* ctx_get */
337 static ctx_t *
338 ctx_set(ctx_t *new)
340         ctx_t *old;
342         if (! plugin_ctx_key_initialized)
343                 ctx_key_init();
345         old = pthread_getspecific(plugin_ctx_key);
346         if (old)
347                 sdb_object_deref(SDB_OBJ(old));
348         if (new)
349                 sdb_object_ref(SDB_OBJ(new));
350         pthread_setspecific(plugin_ctx_key, new);
351         return old;
352 } /* ctx_set */
354 static ctx_t *
355 ctx_create(const char *name)
357         ctx_t *ctx;
359         ctx = CTX(sdb_object_create(name, ctx_type));
360         if (! ctx)
361                 return NULL;
363         if (! plugin_ctx_key_initialized)
364                 ctx_key_init();
365         ctx_set(ctx);
366         return ctx;
367 } /* ctx_create */
369 static int
370 plugin_cb_init(sdb_object_t *obj, va_list ap)
372         sdb_llist_t **list = va_arg(ap, sdb_llist_t **);
373         const char   *type = va_arg(ap, const char *);
374         void     *callback = va_arg(ap, void *);
375         sdb_object_t   *ud = va_arg(ap, sdb_object_t *);
377         assert(list);
378         assert(type);
379         assert(obj);
381         if (sdb_llist_search_by_name(*list, obj->name)) {
382                 sdb_log(SDB_LOG_WARNING, "core: %s callback '%s' "
383                                 "has already been registered. Ignoring newly "
384                                 "registered version.", type, obj->name);
385                 return -1;
386         }
388         /* cb_ctx may be NULL if the plugin was not registered by a plugin */
390         SDB_PLUGIN_CB(obj)->cb_callback = callback;
391         SDB_PLUGIN_CB(obj)->cb_ctx      = ctx_get();
392         sdb_object_ref(SDB_OBJ(SDB_PLUGIN_CB(obj)->cb_ctx));
394         sdb_object_ref(ud);
395         SDB_PLUGIN_CB(obj)->cb_user_data = ud;
396         return 0;
397 } /* plugin_cb_init */
399 static void
400 plugin_cb_destroy(sdb_object_t *obj)
402         assert(obj);
403         sdb_object_deref(SDB_PLUGIN_CB(obj)->cb_user_data);
404         sdb_object_deref(SDB_OBJ(SDB_PLUGIN_CB(obj)->cb_ctx));
405 } /* plugin_cb_destroy */
407 static sdb_type_t sdb_plugin_cb_type = {
408         sizeof(sdb_plugin_cb_t),
410         plugin_cb_init,
411         plugin_cb_destroy
412 };
414 static sdb_type_t sdb_plugin_collector_cb_type = {
415         sizeof(sdb_plugin_collector_cb_t),
417         plugin_cb_init,
418         plugin_cb_destroy
419 };
421 static int
422 module_init(const char *name, lt_dlhandle lh, sdb_plugin_info_t *info)
424         int (*mod_init)(sdb_plugin_info_t *);
425         int status;
427         mod_init = (int (*)(sdb_plugin_info_t *))lt_dlsym(lh, "sdb_module_init");
428         if (! mod_init) {
429                 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': "
430                                 "could not find symbol 'sdb_module_init'", name);
431                 return -1;
432         }
434         status = mod_init(info);
435         if (status) {
436                 sdb_log(SDB_LOG_ERR, "core: Failed to initialize "
437                                 "module '%s'", name);
438                 plugin_unregister_by_name(name);
439                 return -1;
440         }
441         return 0;
442 } /* module_init */
444 static int
445 module_load(const char *name, const sdb_plugin_ctx_t *plugin_ctx)
447         char  base_name[name ? strlen(name) + 1 : 1];
448         const char *name_ptr;
449         char *tmp;
451         char filename[1024];
452         lt_dlhandle lh;
454         ctx_t *ctx;
456         int status;
458         base_name[0] = '\0';
459         name_ptr = name;
461         while ((tmp = strstr(name_ptr, "::"))) {
462                 strncat(base_name, name_ptr, (size_t)(tmp - name_ptr));
463                 strcat(base_name, "/");
464                 name_ptr = tmp + strlen("::");
465         }
466         strcat(base_name, name_ptr);
468         snprintf(filename, sizeof(filename), "%s/%s.so",
469                         PKGLIBDIR, base_name);
470         filename[sizeof(filename) - 1] = '\0';
472         if (access(filename, R_OK)) {
473                 char errbuf[1024];
474                 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s' (%s): %s",
475                                 name, filename, sdb_strerror(errno, errbuf, sizeof(errbuf)));
476                 return -1;
477         }
479         lt_dlinit();
480         lt_dlerror();
482         lh = lt_dlopen(filename);
483         if (! lh) {
484                 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': %s"
485                                 "The most common cause for this problem are missing "
486                                 "dependencies.\n", name, lt_dlerror());
487                 return -1;
488         }
490         if (ctx_get())
491                 sdb_log(SDB_LOG_WARNING, "core: Discarding old plugin context");
493         ctx = ctx_create(name);
494         if (! ctx) {
495                 sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin context");
496                 return -1;
497         }
499         ctx->info.plugin_name = strdup(name);
500         ctx->info.filename = strdup(filename);
501         ctx->handle = lh;
503         if (plugin_ctx)
504                 ctx->public = *plugin_ctx;
506         if ((status = module_init(name, lh, &ctx->info))) {
507                 sdb_object_deref(SDB_OBJ(ctx));
508                 return status;
509         }
511         /* compare minor version */
512         if ((ctx->info.version < 0)
513                         || ((int)(ctx->info.version / 100) != (int)(SDB_VERSION / 100)))
514                 sdb_log(SDB_LOG_WARNING, "core: WARNING: version of "
515                                 "plugin '%s' (%i.%i.%i) does not match our version "
516                                 "(%i.%i.%i); this might cause problems",
517                                 name, SDB_VERSION_DECODE(ctx->info.version),
518                                 SDB_VERSION_DECODE(SDB_VERSION));
520         sdb_llist_append(all_plugins, SDB_OBJ(ctx));
522         sdb_log(SDB_LOG_INFO, "core: Successfully loaded "
523                         "plugin '%s' v%i (%s)\n\t%s\n\tLicense: %s",
524                         INFO_GET(&ctx->info, name), ctx->info.plugin_version,
525                         INFO_GET(&ctx->info, description),
526                         INFO_GET(&ctx->info, copyright),
527                         INFO_GET(&ctx->info, license));
529         /* any registered callbacks took ownership of the context */
530         sdb_object_deref(SDB_OBJ(ctx));
532         /* reset */
533         ctx_set(NULL);
534         return 0;
535 } /* module_load */
537 static int
538 plugin_add_callback(sdb_llist_t **list, const char *type,
539                 const char *name, void *callback, sdb_object_t *user_data)
541         sdb_object_t *obj;
543         if ((! name) || (! callback))
544                 return -1;
546         assert(list);
548         if (! *list)
549                 *list = sdb_llist_create();
550         if (! *list)
551                 return -1;
553         obj = sdb_object_create(name, sdb_plugin_cb_type,
554                         list, type, callback, user_data);
555         if (! obj)
556                 return -1;
558         if (sdb_llist_append(*list, obj)) {
559                 sdb_object_deref(obj);
560                 return -1;
561         }
563         /* pass control to the list */
564         sdb_object_deref(obj);
566         sdb_log(SDB_LOG_INFO, "core: Registered %s callback '%s'.",
567                         type, name);
568         return 0;
569 } /* plugin_add_callback */
571 /*
572  * public API
573  */
575 int
576 sdb_plugin_load(const char *name, 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)", INFO_GET(&ctx->info, 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(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_NAME:
628                         {
629                                 char *name = va_arg(ap, char *);
630                                 if (name) {
631                                         if (info->name)
632                                                 free(info->name);
633                                         info->name = strdup(name);
634                                 }
635                         }
636                         break;
637                 case SDB_PLUGIN_INFO_DESC:
638                         {
639                                 char *desc = va_arg(ap, char *);
640                                 if (desc) {
641                                         if (info->description)
642                                                 free(info->description);
643                                         info->description = strdup(desc);
644                                 }
645                         }
646                         break;
647                 case SDB_PLUGIN_INFO_COPYRIGHT:
648                         {
649                                 char *copyright = va_arg(ap, char *);
650                                 if (copyright)
651                                         info->copyright = strdup(copyright);
652                         }
653                         break;
654                 case SDB_PLUGIN_INFO_LICENSE:
655                         {
656                                 char *license = va_arg(ap, char *);
657                                 if (license) {
658                                         if (info->license)
659                                                 free(info->license);
660                                         info->license = strdup(license);
661                                 }
662                         }
663                         break;
664                 case SDB_PLUGIN_INFO_VERSION:
665                         {
666                                 int version = va_arg(ap, int);
667                                 info->version = version;
668                         }
669                         break;
670                 case SDB_PLUGIN_INFO_PLUGIN_VERSION:
671                         {
672                                 int version = va_arg(ap, int);
673                                 info->plugin_version = version;
674                         }
675                         break;
676                 default:
677                         va_end(ap);
678                         return -1;
679         }
681         va_end(ap);
682         return 0;
683 } /* sdb_plugin_set_info */
685 int
686 sdb_plugin_register_config(const char *name, sdb_plugin_config_cb callback)
688         return plugin_add_callback(&config_list, "init", name,
689                         (void *)callback, NULL);
690 } /* sdb_plugin_register_config */
692 int
693 sdb_plugin_register_init(const char *name, sdb_plugin_init_cb callback,
694                 sdb_object_t *user_data)
696         return plugin_add_callback(&init_list, "init", name,
697                         (void *)callback, user_data);
698 } /* sdb_plugin_register_init */
700 int
701 sdb_plugin_register_shutdown(const char *name, sdb_plugin_shutdown_cb callback,
702                 sdb_object_t *user_data)
704         return plugin_add_callback(&shutdown_list, "shutdown", name,
705                         (void *)callback, user_data);
706 } /* sdb_plugin_register_shutdown */
708 int
709 sdb_plugin_register_log(const char *name, sdb_plugin_log_cb callback,
710                 sdb_object_t *user_data)
712         return plugin_add_callback(&log_list, "log", name, (void *)callback,
713                         user_data);
714 } /* sdb_plugin_register_log */
716 int
717 sdb_plugin_register_cname(const char *name, sdb_plugin_cname_cb callback,
718                 sdb_object_t *user_data)
720         return plugin_add_callback(&cname_list, "cname", name, (void *)callback,
721                         user_data);
722 } /* sdb_plugin_register_cname */
724 int
725 sdb_plugin_register_collector(const char *name, sdb_plugin_collector_cb callback,
726                 const sdb_time_t *interval, sdb_object_t *user_data)
728         sdb_object_t *obj;
730         if ((! name) || (! callback))
731                 return -1;
733         if (! collector_list)
734                 collector_list = sdb_llist_create();
735         if (! collector_list)
736                 return -1;
738         obj = sdb_object_create(name, sdb_plugin_collector_cb_type,
739                         &collector_list, "collector", callback, user_data);
740         if (! obj)
741                 return -1;
743         if (interval)
744                 SDB_PLUGIN_CCB(obj)->ccb_interval = *interval;
745         else {
746                 sdb_time_t tmp = sdb_plugin_get_ctx().interval;
748                 if (tmp > 0)
749                         SDB_PLUGIN_CCB(obj)->ccb_interval = tmp;
750                 else
751                         SDB_PLUGIN_CCB(obj)->ccb_interval = 0;
752         }
754         if (! (SDB_PLUGIN_CCB(obj)->ccb_next_update = sdb_gettime())) {
755                 char errbuf[1024];
756                 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
757                                 "time: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
758                 sdb_object_deref(obj);
759                 return -1;
760         }
762         if (sdb_llist_insert_sorted(collector_list, obj,
763                                 plugin_cmp_next_update)) {
764                 sdb_object_deref(obj);
765                 return -1;
766         }
768         /* pass control to the list */
769         sdb_object_deref(obj);
771         sdb_log(SDB_LOG_INFO, "core: Registered collector callback '%s' "
772                         "(interval = %.3fs).", name,
773                         SDB_TIME_TO_DOUBLE(SDB_PLUGIN_CCB(obj)->ccb_interval));
774         return 0;
775 } /* sdb_plugin_register_collector */
777 sdb_plugin_ctx_t
778 sdb_plugin_get_ctx(void)
780         ctx_t *c;
782         c = ctx_get();
783         if (! c) {
784                 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid read access to plugin "
785                                 "context outside a plugin");
786                 return plugin_default_ctx;
787         }
788         return c->public;
789 } /* sdb_plugin_get_ctx */
791 int
792 sdb_plugin_set_ctx(sdb_plugin_ctx_t ctx, sdb_plugin_ctx_t *old)
794         ctx_t *c;
796         c = ctx_get();
797         if (! c) {
798                 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid write access to plugin "
799                                 "context outside a plugin");
800                 return -1;
801         }
803         if (old)
804                 *old = c->public;
805         c->public = ctx;
806         return 0;
807 } /* sdb_plugin_set_ctx */
809 int
810 sdb_plugin_configure(const char *name, oconfig_item_t *ci)
812         sdb_plugin_cb_t *plugin;
813         sdb_plugin_config_cb callback;
815         ctx_t *old_ctx;
817         int status;
819         if ((! name) || (! ci))
820                 return -1;
822         plugin = SDB_PLUGIN_CB(sdb_llist_search_by_name(config_list, name));
823         if (! plugin) {
824                 ctx_t *ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
825                 if (! ctx)
826                         sdb_log(SDB_LOG_ERR, "core: Plugin '%s' not loaded.", name);
827                 else
828                         sdb_log(SDB_LOG_ERR, "core: Plugin '%s' did not register "
829                                         "a config callback.", name);
830                 errno = ENOENT;
831                 return -1;
832         }
834         old_ctx = ctx_set(plugin->cb_ctx);
835         callback = (sdb_plugin_config_cb)plugin->cb_callback;
836         status = callback(ci);
837         ctx_set(old_ctx);
838         return status;
839 } /* sdb_plugin_configure */
841 int
842 sdb_plugin_reconfigure_init(void)
844         sdb_llist_iter_t *iter;
846         iter = sdb_llist_get_iter(config_list);
847         if (config_list && (! iter))
848                 return -1;
850         /* deconfigure all plugins */
851         while (sdb_llist_iter_has_next(iter)) {
852                 sdb_plugin_cb_t *plugin;
853                 sdb_plugin_config_cb callback;
854                 ctx_t *old_ctx;
856                 plugin = SDB_PLUGIN_CB(sdb_llist_iter_get_next(iter));
857                 old_ctx = ctx_set(plugin->cb_ctx);
858                 callback = (sdb_plugin_config_cb)plugin->cb_callback;
859                 callback(NULL);
860                 ctx_set(old_ctx);
861         }
862         sdb_llist_iter_destroy(iter);
864         iter = sdb_llist_get_iter(all_plugins);
865         if (all_plugins && (! iter))
866                 return -1;
868         /* record all plugins as being unused */
869         while (sdb_llist_iter_has_next(iter))
870                 CTX(sdb_llist_iter_get_next(iter))->use_cnt = 0;
871         sdb_llist_iter_destroy(iter);
873         plugin_unregister_all();
874         return 0;
875 } /* sdb_plugin_reconfigure_init */
877 int
878 sdb_plugin_reconfigure_finish(void)
880         sdb_llist_iter_t *iter;
882         iter = sdb_llist_get_iter(all_plugins);
883         if (all_plugins && (! iter))
884                 return -1;
886         while (sdb_llist_iter_has_next(iter)) {
887                 ctx_t *ctx = CTX(sdb_llist_iter_get_next(iter));
888                 if (ctx->use_cnt)
889                         continue;
891                 sdb_log(SDB_LOG_INFO, "core: Module %s no longer in use",
892                                 ctx->info.plugin_name);
893                 sdb_llist_iter_remove_current(iter);
894                 plugin_unregister_by_name(ctx->info.plugin_name);
895                 sdb_object_deref(SDB_OBJ(ctx));
896         }
897         sdb_llist_iter_destroy(iter);
898         return 0;
899 } /* sdb_plugin_reconfigure_finish */
901 int
902 sdb_plugin_init_all(void)
904         sdb_llist_iter_t *iter;
905         int ret = 0;
907         iter = sdb_llist_get_iter(init_list);
908         while (sdb_llist_iter_has_next(iter)) {
909                 sdb_plugin_cb_t *cb;
910                 sdb_plugin_init_cb callback;
911                 ctx_t *old_ctx;
913                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
914                 assert(obj);
915                 cb = SDB_PLUGIN_CB(obj);
917                 callback = (sdb_plugin_init_cb)cb->cb_callback;
919                 old_ctx = ctx_set(cb->cb_ctx);
920                 if (callback(cb->cb_user_data)) {
921                         sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin "
922                                         "'%s'. Unregistering all callbacks.", obj->name);
923                         ctx_set(old_ctx);
924                         plugin_unregister_by_name(cb->cb_ctx->info.plugin_name);
925                         ++ret;
926                 }
927                 else
928                         ctx_set(old_ctx);
929         }
930         sdb_llist_iter_destroy(iter);
931         return ret;
932 } /* sdb_plugin_init_all */
934 int
935 sdb_plugin_collector_loop(sdb_plugin_loop_t *loop)
937         if (! collector_list) {
938                 sdb_log(SDB_LOG_WARNING, "core: No collectors registered. "
939                                 "Quiting main loop.");
940                 return -1;
941         }
943         if (! loop)
944                 return -1;
946         while (loop->do_loop) {
947                 sdb_plugin_collector_cb callback;
948                 ctx_t *old_ctx;
950                 sdb_time_t interval, now;
952                 sdb_object_t *obj = sdb_llist_shift(collector_list);
953                 if (! obj)
954                         return -1;
956                 callback = (sdb_plugin_collector_cb)SDB_PLUGIN_CCB(obj)->ccb_callback;
958                 if (! (now = sdb_gettime())) {
959                         char errbuf[1024];
960                         sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
961                                         "time in collector main loop: %s",
962                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
963                         now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
964                 }
966                 if (now < SDB_PLUGIN_CCB(obj)->ccb_next_update) {
967                         interval = SDB_PLUGIN_CCB(obj)->ccb_next_update - now;
969                         errno = 0;
970                         while (loop->do_loop && sdb_sleep(interval, &interval)) {
971                                 if (errno != EINTR) {
972                                         char errbuf[1024];
973                                         sdb_log(SDB_LOG_ERR, "core: Failed to sleep "
974                                                         "in collector main loop: %s",
975                                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
976                                         return -1;
977                                 }
978                                 errno = 0;
979                         }
981                         if (! loop->do_loop)
982                                 return 0;
983                 }
985                 old_ctx = ctx_set(SDB_PLUGIN_CCB(obj)->ccb_ctx);
986                 if (callback(SDB_PLUGIN_CCB(obj)->ccb_user_data)) {
987                         /* XXX */
988                 }
989                 ctx_set(old_ctx);
991                 interval = SDB_PLUGIN_CCB(obj)->ccb_interval;
992                 if (! interval)
993                         interval = loop->default_interval;
994                 if (! interval) {
995                         sdb_log(SDB_LOG_WARNING, "core: No interval configured "
996                                         "for plugin '%s'; skipping any further "
997                                         "iterations.", obj->name);
998                         sdb_object_deref(obj);
999                         continue;
1000                 }
1002                 SDB_PLUGIN_CCB(obj)->ccb_next_update += interval;
1004                 if (! (now = sdb_gettime())) {
1005                         char errbuf[1024];
1006                         sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
1007                                         "time in collector main loop: %s",
1008                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
1009                         now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
1010                 }
1012                 if (now > SDB_PLUGIN_CCB(obj)->ccb_next_update) {
1013                         sdb_log(SDB_LOG_WARNING, "core: Plugin '%s' took too "
1014                                         "long; skipping iterations to keep up.",
1015                                         obj->name);
1016                         SDB_PLUGIN_CCB(obj)->ccb_next_update = now;
1017                 }
1019                 if (sdb_llist_insert_sorted(collector_list, obj,
1020                                         plugin_cmp_next_update)) {
1021                         sdb_log(SDB_LOG_ERR, "core: Failed to re-insert "
1022                                         "plugin '%s' into collector list. Unable to further "
1023                                         "use the plugin.",
1024                                         obj->name);
1025                         sdb_object_deref(obj);
1026                         return -1;
1027                 }
1029                 /* pass control back to the list */
1030                 sdb_object_deref(obj);
1031         }
1032         return 0;
1033 } /* sdb_plugin_read_loop */
1035 char *
1036 sdb_plugin_cname(char *hostname)
1038         sdb_llist_iter_t *iter;
1040         if (! hostname)
1041                 return NULL;
1043         if (! cname_list)
1044                 return hostname;
1046         iter = sdb_llist_get_iter(cname_list);
1047         while (sdb_llist_iter_has_next(iter)) {
1048                 sdb_plugin_cname_cb callback;
1049                 char *cname;
1051                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
1052                 assert(obj);
1054                 callback = (sdb_plugin_cname_cb)SDB_PLUGIN_CB(obj)->cb_callback;
1055                 cname = callback(hostname, SDB_PLUGIN_CB(obj)->cb_user_data);
1056                 if (cname) {
1057                         free(hostname);
1058                         hostname = cname;
1059                 }
1060                 /* else: don't change hostname */
1061         }
1062         sdb_llist_iter_destroy(iter);
1063         return hostname;
1064 } /* sdb_plugin_cname */
1066 int
1067 sdb_plugin_log(int prio, const char *msg)
1069         sdb_llist_iter_t *iter;
1070         int ret = -1;
1072         if (! msg)
1073                 return 0;
1075         if (! sdb_llist_len(log_list))
1076                 return fprintf(stderr, "[%s] %s\n", SDB_LOG_PRIO_TO_STRING(prio), msg);
1078         iter = sdb_llist_get_iter(log_list);
1079         while (sdb_llist_iter_has_next(iter)) {
1080                 sdb_plugin_log_cb callback;
1081                 int tmp;
1083                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
1084                 assert(obj);
1086                 callback = (sdb_plugin_log_cb)SDB_PLUGIN_CB(obj)->cb_callback;
1087                 tmp = callback(prio, msg, SDB_PLUGIN_CB(obj)->cb_user_data);
1088                 if (tmp > ret)
1089                         ret = tmp;
1090         }
1091         sdb_llist_iter_destroy(iter);
1092         return ret;
1093 } /* sdb_plugin_log */
1095 int
1096 sdb_plugin_vlogf(int prio, const char *fmt, va_list ap)
1098         sdb_strbuf_t *buf;
1099         int ret;
1101         if (! fmt)
1102                 return 0;
1104         buf = sdb_strbuf_create(64);
1105         if (! buf) {
1106                 ret = fprintf(stderr, "[%s] ", SDB_LOG_PRIO_TO_STRING(prio));
1107                 ret += vfprintf(stderr, fmt, ap);
1108                 return ret;
1109         }
1111         if (sdb_strbuf_vsprintf(buf, fmt, ap) < 0) {
1112                 sdb_strbuf_destroy(buf);
1113                 return -1;
1114         }
1116         ret = sdb_plugin_log(prio, sdb_strbuf_string(buf));
1117         sdb_strbuf_destroy(buf);
1118         return ret;
1119 } /* sdb_plugin_vlogf */
1121 int
1122 sdb_plugin_logf(int prio, const char *fmt, ...)
1124         va_list ap;
1125         int ret;
1127         if (! fmt)
1128                 return 0;
1130         va_start(ap, fmt);
1131         ret = sdb_plugin_vlogf(prio, fmt, ap);
1132         va_end(ap);
1133         return ret;
1134 } /* sdb_plugin_logf */
1136 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */