Code

Merged branch 'master' of git://git.tokkee.org/sysdb.
[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         assert(name);
460         base_name[0] = '\0';
461         name_ptr = name;
463         while ((tmp = strstr(name_ptr, "::"))) {
464                 strncat(base_name, name_ptr, (size_t)(tmp - name_ptr));
465                 strcat(base_name, "/");
466                 name_ptr = tmp + strlen("::");
467         }
468         strcat(base_name, name_ptr);
470         snprintf(filename, sizeof(filename), "%s/%s.so",
471                         PKGLIBDIR, base_name);
472         filename[sizeof(filename) - 1] = '\0';
474         if (access(filename, R_OK)) {
475                 char errbuf[1024];
476                 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s' (%s): %s",
477                                 name, filename, sdb_strerror(errno, errbuf, sizeof(errbuf)));
478                 return -1;
479         }
481         lt_dlinit();
482         lt_dlerror();
484         lh = lt_dlopen(filename);
485         if (! lh) {
486                 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': %s"
487                                 "The most common cause for this problem are missing "
488                                 "dependencies.\n", name, lt_dlerror());
489                 return -1;
490         }
492         if (ctx_get())
493                 sdb_log(SDB_LOG_WARNING, "core: Discarding old plugin context");
495         ctx = ctx_create(name);
496         if (! ctx) {
497                 sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin context");
498                 return -1;
499         }
501         ctx->info.plugin_name = strdup(name);
502         ctx->info.filename = strdup(filename);
503         ctx->handle = lh;
505         if (plugin_ctx)
506                 ctx->public = *plugin_ctx;
508         if ((status = module_init(name, lh, &ctx->info))) {
509                 sdb_object_deref(SDB_OBJ(ctx));
510                 return status;
511         }
513         /* compare minor version */
514         if ((ctx->info.version < 0)
515                         || ((int)(ctx->info.version / 100) != (int)(SDB_VERSION / 100)))
516                 sdb_log(SDB_LOG_WARNING, "core: WARNING: version of "
517                                 "plugin '%s' (%i.%i.%i) does not match our version "
518                                 "(%i.%i.%i); this might cause problems",
519                                 name, SDB_VERSION_DECODE(ctx->info.version),
520                                 SDB_VERSION_DECODE(SDB_VERSION));
522         sdb_llist_append(all_plugins, SDB_OBJ(ctx));
524         sdb_log(SDB_LOG_INFO, "core: Successfully loaded "
525                         "plugin '%s' v%i (%s)\n\t%s\n\tLicense: %s",
526                         INFO_GET(&ctx->info, name), ctx->info.plugin_version,
527                         INFO_GET(&ctx->info, description),
528                         INFO_GET(&ctx->info, copyright),
529                         INFO_GET(&ctx->info, license));
531         /* any registered callbacks took ownership of the context */
532         sdb_object_deref(SDB_OBJ(ctx));
534         /* reset */
535         ctx_set(NULL);
536         return 0;
537 } /* module_load */
539 static int
540 plugin_add_callback(sdb_llist_t **list, const char *type,
541                 const char *name, void *callback, sdb_object_t *user_data)
543         sdb_object_t *obj;
545         if ((! name) || (! callback))
546                 return -1;
548         assert(list);
550         if (! *list)
551                 *list = sdb_llist_create();
552         if (! *list)
553                 return -1;
555         obj = sdb_object_create(name, sdb_plugin_cb_type,
556                         list, type, callback, user_data);
557         if (! obj)
558                 return -1;
560         if (sdb_llist_append(*list, obj)) {
561                 sdb_object_deref(obj);
562                 return -1;
563         }
565         /* pass control to the list */
566         sdb_object_deref(obj);
568         sdb_log(SDB_LOG_INFO, "core: Registered %s callback '%s'.",
569                         type, name);
570         return 0;
571 } /* plugin_add_callback */
573 /*
574  * public API
575  */
577 int
578 sdb_plugin_load(const char *name, const sdb_plugin_ctx_t *plugin_ctx)
580         ctx_t *ctx;
582         int status;
584         if ((! name) || (! *name))
585                 return -1;
587         if (! all_plugins) {
588                 if (! (all_plugins = sdb_llist_create())) {
589                         sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': "
590                                         "internal error while creating linked list", name);
591                         return -1;
592                 }
593         }
595         ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
596         if (ctx) {
597                 /* plugin already loaded */
598                 if (! ctx->use_cnt) {
599                         /* reloading plugin */
600                         ctx_t *old_ctx = ctx_set(ctx);
602                         status = module_init(ctx->info.plugin_name, ctx->handle, NULL);
603                         if (status)
604                                 return status;
606                         sdb_log(SDB_LOG_INFO, "core: Successfully reloaded plugin "
607                                         "'%s' (%s)", INFO_GET(&ctx->info, name),
608                                         INFO_GET(&ctx->info, description));
609                         ctx_set(old_ctx);
610                 }
611                 ++ctx->use_cnt;
612                 return 0;
613         }
615         return module_load(name, plugin_ctx);
616 } /* sdb_plugin_load */
618 int
619 sdb_plugin_set_info(sdb_plugin_info_t *info, int type, ...)
621         va_list ap;
623         if (! info)
624                 return -1;
626         va_start(ap, type);
628         switch (type) {
629                 case SDB_PLUGIN_INFO_NAME:
630                         {
631                                 char *name = va_arg(ap, char *);
632                                 if (name) {
633                                         if (info->name)
634                                                 free(info->name);
635                                         info->name = strdup(name);
636                                 }
637                         }
638                         break;
639                 case SDB_PLUGIN_INFO_DESC:
640                         {
641                                 char *desc = va_arg(ap, char *);
642                                 if (desc) {
643                                         if (info->description)
644                                                 free(info->description);
645                                         info->description = strdup(desc);
646                                 }
647                         }
648                         break;
649                 case SDB_PLUGIN_INFO_COPYRIGHT:
650                         {
651                                 char *copyright = va_arg(ap, char *);
652                                 if (copyright)
653                                         info->copyright = strdup(copyright);
654                         }
655                         break;
656                 case SDB_PLUGIN_INFO_LICENSE:
657                         {
658                                 char *license = va_arg(ap, char *);
659                                 if (license) {
660                                         if (info->license)
661                                                 free(info->license);
662                                         info->license = strdup(license);
663                                 }
664                         }
665                         break;
666                 case SDB_PLUGIN_INFO_VERSION:
667                         {
668                                 int version = va_arg(ap, int);
669                                 info->version = version;
670                         }
671                         break;
672                 case SDB_PLUGIN_INFO_PLUGIN_VERSION:
673                         {
674                                 int version = va_arg(ap, int);
675                                 info->plugin_version = version;
676                         }
677                         break;
678                 default:
679                         va_end(ap);
680                         return -1;
681         }
683         va_end(ap);
684         return 0;
685 } /* sdb_plugin_set_info */
687 int
688 sdb_plugin_register_config(const char *name, sdb_plugin_config_cb callback)
690         return plugin_add_callback(&config_list, "init", name,
691                         (void *)callback, NULL);
692 } /* sdb_plugin_register_config */
694 int
695 sdb_plugin_register_init(const char *name, sdb_plugin_init_cb callback,
696                 sdb_object_t *user_data)
698         return plugin_add_callback(&init_list, "init", name,
699                         (void *)callback, user_data);
700 } /* sdb_plugin_register_init */
702 int
703 sdb_plugin_register_shutdown(const char *name, sdb_plugin_shutdown_cb callback,
704                 sdb_object_t *user_data)
706         return plugin_add_callback(&shutdown_list, "shutdown", name,
707                         (void *)callback, user_data);
708 } /* sdb_plugin_register_shutdown */
710 int
711 sdb_plugin_register_log(const char *name, sdb_plugin_log_cb callback,
712                 sdb_object_t *user_data)
714         return plugin_add_callback(&log_list, "log", name, (void *)callback,
715                         user_data);
716 } /* sdb_plugin_register_log */
718 int
719 sdb_plugin_register_cname(const char *name, sdb_plugin_cname_cb callback,
720                 sdb_object_t *user_data)
722         return plugin_add_callback(&cname_list, "cname", name, (void *)callback,
723                         user_data);
724 } /* sdb_plugin_register_cname */
726 int
727 sdb_plugin_register_collector(const char *name, sdb_plugin_collector_cb callback,
728                 const sdb_time_t *interval, sdb_object_t *user_data)
730         sdb_object_t *obj;
732         if ((! name) || (! callback))
733                 return -1;
735         if (! collector_list)
736                 collector_list = sdb_llist_create();
737         if (! collector_list)
738                 return -1;
740         obj = sdb_object_create(name, sdb_plugin_collector_cb_type,
741                         &collector_list, "collector", callback, user_data);
742         if (! obj)
743                 return -1;
745         if (interval)
746                 SDB_PLUGIN_CCB(obj)->ccb_interval = *interval;
747         else {
748                 sdb_time_t tmp = sdb_plugin_get_ctx().interval;
750                 if (tmp > 0)
751                         SDB_PLUGIN_CCB(obj)->ccb_interval = tmp;
752                 else
753                         SDB_PLUGIN_CCB(obj)->ccb_interval = 0;
754         }
756         if (! (SDB_PLUGIN_CCB(obj)->ccb_next_update = sdb_gettime())) {
757                 char errbuf[1024];
758                 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
759                                 "time: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
760                 sdb_object_deref(obj);
761                 return -1;
762         }
764         if (sdb_llist_insert_sorted(collector_list, obj,
765                                 plugin_cmp_next_update)) {
766                 sdb_object_deref(obj);
767                 return -1;
768         }
770         /* pass control to the list */
771         sdb_object_deref(obj);
773         sdb_log(SDB_LOG_INFO, "core: Registered collector callback '%s' "
774                         "(interval = %.3fs).", name,
775                         SDB_TIME_TO_DOUBLE(SDB_PLUGIN_CCB(obj)->ccb_interval));
776         return 0;
777 } /* sdb_plugin_register_collector */
779 sdb_plugin_ctx_t
780 sdb_plugin_get_ctx(void)
782         ctx_t *c;
784         c = ctx_get();
785         if (! c) {
786                 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid read access to plugin "
787                                 "context outside a plugin");
788                 return plugin_default_ctx;
789         }
790         return c->public;
791 } /* sdb_plugin_get_ctx */
793 int
794 sdb_plugin_set_ctx(sdb_plugin_ctx_t ctx, sdb_plugin_ctx_t *old)
796         ctx_t *c;
798         c = ctx_get();
799         if (! c) {
800                 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid write access to plugin "
801                                 "context outside a plugin");
802                 return -1;
803         }
805         if (old)
806                 *old = c->public;
807         c->public = ctx;
808         return 0;
809 } /* sdb_plugin_set_ctx */
811 int
812 sdb_plugin_configure(const char *name, oconfig_item_t *ci)
814         sdb_plugin_cb_t *plugin;
815         sdb_plugin_config_cb callback;
817         ctx_t *old_ctx;
819         int status;
821         if ((! name) || (! ci))
822                 return -1;
824         plugin = SDB_PLUGIN_CB(sdb_llist_search_by_name(config_list, name));
825         if (! plugin) {
826                 ctx_t *ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
827                 if (! ctx)
828                         sdb_log(SDB_LOG_ERR, "core: Plugin '%s' not loaded.", name);
829                 else
830                         sdb_log(SDB_LOG_ERR, "core: Plugin '%s' did not register "
831                                         "a config callback.", name);
832                 errno = ENOENT;
833                 return -1;
834         }
836         old_ctx = ctx_set(plugin->cb_ctx);
837         callback = (sdb_plugin_config_cb)plugin->cb_callback;
838         status = callback(ci);
839         ctx_set(old_ctx);
840         return status;
841 } /* sdb_plugin_configure */
843 int
844 sdb_plugin_reconfigure_init(void)
846         sdb_llist_iter_t *iter;
848         iter = sdb_llist_get_iter(config_list);
849         if (config_list && (! iter))
850                 return -1;
852         /* deconfigure all plugins */
853         while (sdb_llist_iter_has_next(iter)) {
854                 sdb_plugin_cb_t *plugin;
855                 sdb_plugin_config_cb callback;
856                 ctx_t *old_ctx;
858                 plugin = SDB_PLUGIN_CB(sdb_llist_iter_get_next(iter));
859                 old_ctx = ctx_set(plugin->cb_ctx);
860                 callback = (sdb_plugin_config_cb)plugin->cb_callback;
861                 callback(NULL);
862                 ctx_set(old_ctx);
863         }
864         sdb_llist_iter_destroy(iter);
866         iter = sdb_llist_get_iter(all_plugins);
867         if (all_plugins && (! iter))
868                 return -1;
870         /* record all plugins as being unused */
871         while (sdb_llist_iter_has_next(iter))
872                 CTX(sdb_llist_iter_get_next(iter))->use_cnt = 0;
873         sdb_llist_iter_destroy(iter);
875         plugin_unregister_all();
876         return 0;
877 } /* sdb_plugin_reconfigure_init */
879 int
880 sdb_plugin_reconfigure_finish(void)
882         sdb_llist_iter_t *iter;
884         iter = sdb_llist_get_iter(all_plugins);
885         if (all_plugins && (! iter))
886                 return -1;
888         while (sdb_llist_iter_has_next(iter)) {
889                 ctx_t *ctx = CTX(sdb_llist_iter_get_next(iter));
890                 if (ctx->use_cnt)
891                         continue;
893                 sdb_log(SDB_LOG_INFO, "core: Module %s no longer in use",
894                                 ctx->info.plugin_name);
895                 sdb_llist_iter_remove_current(iter);
896                 plugin_unregister_by_name(ctx->info.plugin_name);
897                 sdb_object_deref(SDB_OBJ(ctx));
898         }
899         sdb_llist_iter_destroy(iter);
900         return 0;
901 } /* sdb_plugin_reconfigure_finish */
903 int
904 sdb_plugin_init_all(void)
906         sdb_llist_iter_t *iter;
907         int ret = 0;
909         iter = sdb_llist_get_iter(init_list);
910         while (sdb_llist_iter_has_next(iter)) {
911                 sdb_plugin_cb_t *cb;
912                 sdb_plugin_init_cb callback;
913                 ctx_t *old_ctx;
915                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
916                 assert(obj);
917                 cb = SDB_PLUGIN_CB(obj);
919                 callback = (sdb_plugin_init_cb)cb->cb_callback;
921                 old_ctx = ctx_set(cb->cb_ctx);
922                 if (callback(cb->cb_user_data)) {
923                         sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin "
924                                         "'%s'. Unregistering all callbacks.", obj->name);
925                         ctx_set(old_ctx);
926                         plugin_unregister_by_name(cb->cb_ctx->info.plugin_name);
927                         ++ret;
928                 }
929                 else
930                         ctx_set(old_ctx);
931         }
932         sdb_llist_iter_destroy(iter);
933         return ret;
934 } /* sdb_plugin_init_all */
936 int
937 sdb_plugin_shutdown_all(void)
939         sdb_llist_iter_t *iter;
940         int ret = 0;
942         iter = sdb_llist_get_iter(shutdown_list);
943         while (sdb_llist_iter_has_next(iter)) {
944                 sdb_plugin_cb_t *cb;
945                 sdb_plugin_shutdown_cb callback;
946                 ctx_t *old_ctx;
948                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
949                 assert(obj);
950                 cb = SDB_PLUGIN_CB(obj);
952                 callback = (sdb_plugin_shutdown_cb)cb->cb_callback;
954                 old_ctx = ctx_set(cb->cb_ctx);
955                 if (callback(cb->cb_user_data)) {
956                         sdb_log(SDB_LOG_ERR, "core: Failed to shutdown plugin '%s'.",
957                                         obj->name);
958                         ++ret;
959                 }
960                 ctx_set(old_ctx);
961         }
962         sdb_llist_iter_destroy(iter);
963         return ret;
964 } /* sdb_plugin_shutdown_all */
966 int
967 sdb_plugin_collector_loop(sdb_plugin_loop_t *loop)
969         if (! collector_list) {
970                 sdb_log(SDB_LOG_WARNING, "core: No collectors registered. "
971                                 "Quiting main loop.");
972                 return -1;
973         }
975         if (! loop)
976                 return -1;
978         while (loop->do_loop) {
979                 sdb_plugin_collector_cb callback;
980                 ctx_t *old_ctx;
982                 sdb_time_t interval, now;
984                 sdb_object_t *obj = sdb_llist_shift(collector_list);
985                 if (! obj)
986                         return -1;
988                 callback = (sdb_plugin_collector_cb)SDB_PLUGIN_CCB(obj)->ccb_callback;
990                 if (! (now = sdb_gettime())) {
991                         char errbuf[1024];
992                         sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
993                                         "time in collector main loop: %s",
994                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
995                         now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
996                 }
998                 if (now < SDB_PLUGIN_CCB(obj)->ccb_next_update) {
999                         interval = SDB_PLUGIN_CCB(obj)->ccb_next_update - now;
1001                         errno = 0;
1002                         while (loop->do_loop && sdb_sleep(interval, &interval)) {
1003                                 if (errno != EINTR) {
1004                                         char errbuf[1024];
1005                                         sdb_log(SDB_LOG_ERR, "core: Failed to sleep "
1006                                                         "in collector main loop: %s",
1007                                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
1008                                         sdb_llist_insert_sorted(collector_list, obj,
1009                                                         plugin_cmp_next_update);
1010                                         sdb_object_deref(obj);
1011                                         return -1;
1012                                 }
1013                                 errno = 0;
1014                         }
1016                         if (! loop->do_loop) {
1017                                 /* put back; don't worry about errors */
1018                                 sdb_llist_insert_sorted(collector_list, obj,
1019                                                 plugin_cmp_next_update);
1020                                 sdb_object_deref(obj);
1021                                 return 0;
1022                         }
1023                 }
1025                 old_ctx = ctx_set(SDB_PLUGIN_CCB(obj)->ccb_ctx);
1026                 if (callback(SDB_PLUGIN_CCB(obj)->ccb_user_data)) {
1027                         /* XXX */
1028                 }
1029                 ctx_set(old_ctx);
1031                 interval = SDB_PLUGIN_CCB(obj)->ccb_interval;
1032                 if (! interval)
1033                         interval = loop->default_interval;
1034                 if (! interval) {
1035                         sdb_log(SDB_LOG_WARNING, "core: No interval configured "
1036                                         "for plugin '%s'; skipping any further "
1037                                         "iterations.", obj->name);
1038                         sdb_object_deref(obj);
1039                         continue;
1040                 }
1042                 SDB_PLUGIN_CCB(obj)->ccb_next_update += interval;
1044                 if (! (now = sdb_gettime())) {
1045                         char errbuf[1024];
1046                         sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
1047                                         "time in collector main loop: %s",
1048                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
1049                         now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
1050                 }
1052                 if (now > SDB_PLUGIN_CCB(obj)->ccb_next_update) {
1053                         sdb_log(SDB_LOG_WARNING, "core: Plugin '%s' took too "
1054                                         "long; skipping iterations to keep up.",
1055                                         obj->name);
1056                         SDB_PLUGIN_CCB(obj)->ccb_next_update = now;
1057                 }
1059                 if (sdb_llist_insert_sorted(collector_list, obj,
1060                                         plugin_cmp_next_update)) {
1061                         sdb_log(SDB_LOG_ERR, "core: Failed to re-insert "
1062                                         "plugin '%s' into collector list. Unable to further "
1063                                         "use the plugin.",
1064                                         obj->name);
1065                         sdb_object_deref(obj);
1066                         return -1;
1067                 }
1069                 /* pass control back to the list */
1070                 sdb_object_deref(obj);
1071         }
1072         return 0;
1073 } /* sdb_plugin_read_loop */
1075 char *
1076 sdb_plugin_cname(char *hostname)
1078         sdb_llist_iter_t *iter;
1080         if (! hostname)
1081                 return NULL;
1083         if (! cname_list)
1084                 return hostname;
1086         iter = sdb_llist_get_iter(cname_list);
1087         while (sdb_llist_iter_has_next(iter)) {
1088                 sdb_plugin_cname_cb callback;
1089                 char *cname;
1091                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
1092                 assert(obj);
1094                 callback = (sdb_plugin_cname_cb)SDB_PLUGIN_CB(obj)->cb_callback;
1095                 cname = callback(hostname, SDB_PLUGIN_CB(obj)->cb_user_data);
1096                 if (cname) {
1097                         free(hostname);
1098                         hostname = cname;
1099                 }
1100                 /* else: don't change hostname */
1101         }
1102         sdb_llist_iter_destroy(iter);
1103         return hostname;
1104 } /* sdb_plugin_cname */
1106 int
1107 sdb_plugin_log(int prio, const char *msg)
1109         sdb_llist_iter_t *iter;
1110         int ret = -1;
1112         if (! msg)
1113                 return 0;
1115         if (! sdb_llist_len(log_list))
1116                 return fprintf(stderr, "[%s] %s\n", SDB_LOG_PRIO_TO_STRING(prio), msg);
1118         iter = sdb_llist_get_iter(log_list);
1119         while (sdb_llist_iter_has_next(iter)) {
1120                 sdb_plugin_log_cb callback;
1121                 int tmp;
1123                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
1124                 assert(obj);
1126                 callback = (sdb_plugin_log_cb)SDB_PLUGIN_CB(obj)->cb_callback;
1127                 tmp = callback(prio, msg, SDB_PLUGIN_CB(obj)->cb_user_data);
1128                 if (tmp > ret)
1129                         ret = tmp;
1130         }
1131         sdb_llist_iter_destroy(iter);
1132         return ret;
1133 } /* sdb_plugin_log */
1135 int
1136 sdb_plugin_vlogf(int prio, const char *fmt, va_list ap)
1138         sdb_strbuf_t *buf;
1139         int ret;
1141         if (! fmt)
1142                 return 0;
1144         buf = sdb_strbuf_create(64);
1145         if (! buf) {
1146                 ret = fprintf(stderr, "[%s] ", SDB_LOG_PRIO_TO_STRING(prio));
1147                 ret += vfprintf(stderr, fmt, ap);
1148                 return ret;
1149         }
1151         if (sdb_strbuf_vsprintf(buf, fmt, ap) < 0) {
1152                 sdb_strbuf_destroy(buf);
1153                 return -1;
1154         }
1156         ret = sdb_plugin_log(prio, sdb_strbuf_string(buf));
1157         sdb_strbuf_destroy(buf);
1158         return ret;
1159 } /* sdb_plugin_vlogf */
1161 int
1162 sdb_plugin_logf(int prio, const char *fmt, ...)
1164         va_list ap;
1165         int ret;
1167         if (! fmt)
1168                 return 0;
1170         va_start(ap, fmt);
1171         ret = sdb_plugin_vlogf(prio, fmt, ap);
1172         va_end(ap);
1173         return ret;
1174 } /* sdb_plugin_logf */
1176 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */