Code

ReleaseNotes: Added a summary of the utility functions.
[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 *basedir, const char *name,
446                 const sdb_plugin_ctx_t *plugin_ctx)
448         char  base_name[name ? strlen(name) + 1 : 1];
449         const char *name_ptr;
450         char *tmp;
452         char filename[1024];
453         lt_dlhandle lh;
455         ctx_t *ctx;
457         int status;
459         assert(name);
461         base_name[0] = '\0';
462         name_ptr = name;
464         while ((tmp = strstr(name_ptr, "::"))) {
465                 strncat(base_name, name_ptr, (size_t)(tmp - name_ptr));
466                 strcat(base_name, "/");
467                 name_ptr = tmp + strlen("::");
468         }
469         strcat(base_name, name_ptr);
471         if (! basedir)
472                 basedir = PKGLIBDIR;
474         snprintf(filename, sizeof(filename), "%s/%s.so", basedir, base_name);
475         filename[sizeof(filename) - 1] = '\0';
477         if (access(filename, R_OK)) {
478                 char errbuf[1024];
479                 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s' (%s): %s",
480                                 name, filename, sdb_strerror(errno, errbuf, sizeof(errbuf)));
481                 return -1;
482         }
484         lt_dlinit();
485         lt_dlerror();
487         lh = lt_dlopen(filename);
488         if (! lh) {
489                 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': %s"
490                                 "The most common cause for this problem are missing "
491                                 "dependencies.\n", name, lt_dlerror());
492                 return -1;
493         }
495         if (ctx_get())
496                 sdb_log(SDB_LOG_WARNING, "core: Discarding old plugin context");
498         ctx = ctx_create(name);
499         if (! ctx) {
500                 sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin context");
501                 return -1;
502         }
504         ctx->info.plugin_name = strdup(name);
505         ctx->info.filename = strdup(filename);
506         ctx->handle = lh;
508         if (plugin_ctx)
509                 ctx->public = *plugin_ctx;
511         if ((status = module_init(name, lh, &ctx->info))) {
512                 sdb_object_deref(SDB_OBJ(ctx));
513                 return status;
514         }
516         /* compare minor version */
517         if ((ctx->info.version < 0)
518                         || ((int)(ctx->info.version / 100) != (int)(SDB_VERSION / 100)))
519                 sdb_log(SDB_LOG_WARNING, "core: WARNING: version of "
520                                 "plugin '%s' (%i.%i.%i) does not match our version "
521                                 "(%i.%i.%i); this might cause problems",
522                                 name, SDB_VERSION_DECODE(ctx->info.version),
523                                 SDB_VERSION_DECODE(SDB_VERSION));
525         sdb_llist_append(all_plugins, SDB_OBJ(ctx));
527         sdb_log(SDB_LOG_INFO, "core: Successfully loaded "
528                         "plugin '%s' v%i (%s)\n\t%s\n\tLicense: %s",
529                         INFO_GET(&ctx->info, name), ctx->info.plugin_version,
530                         INFO_GET(&ctx->info, description),
531                         INFO_GET(&ctx->info, copyright),
532                         INFO_GET(&ctx->info, license));
534         /* any registered callbacks took ownership of the context */
535         sdb_object_deref(SDB_OBJ(ctx));
537         /* reset */
538         ctx_set(NULL);
539         return 0;
540 } /* module_load */
542 static int
543 plugin_add_callback(sdb_llist_t **list, const char *type,
544                 const char *name, void *callback, sdb_object_t *user_data)
546         sdb_object_t *obj;
548         if ((! name) || (! callback))
549                 return -1;
551         assert(list);
553         if (! *list)
554                 *list = sdb_llist_create();
555         if (! *list)
556                 return -1;
558         obj = sdb_object_create(name, sdb_plugin_cb_type,
559                         list, type, callback, user_data);
560         if (! obj)
561                 return -1;
563         if (sdb_llist_append(*list, obj)) {
564                 sdb_object_deref(obj);
565                 return -1;
566         }
568         /* pass control to the list */
569         sdb_object_deref(obj);
571         sdb_log(SDB_LOG_INFO, "core: Registered %s callback '%s'.",
572                         type, name);
573         return 0;
574 } /* plugin_add_callback */
576 /*
577  * public API
578  */
580 int
581 sdb_plugin_load(const char *basedir, const char *name,
582                 const sdb_plugin_ctx_t *plugin_ctx)
584         ctx_t *ctx;
586         int status;
588         if ((! name) || (! *name))
589                 return -1;
591         if (! all_plugins) {
592                 if (! (all_plugins = sdb_llist_create())) {
593                         sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': "
594                                         "internal error while creating linked list", name);
595                         return -1;
596                 }
597         }
599         ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
600         if (ctx) {
601                 /* plugin already loaded */
602                 if (! ctx->use_cnt) {
603                         /* reloading plugin */
604                         ctx_t *old_ctx = ctx_set(ctx);
606                         status = module_init(ctx->info.plugin_name, ctx->handle, NULL);
607                         if (status)
608                                 return status;
610                         sdb_log(SDB_LOG_INFO, "core: Successfully reloaded plugin "
611                                         "'%s' (%s)", INFO_GET(&ctx->info, name),
612                                         INFO_GET(&ctx->info, description));
613                         ctx_set(old_ctx);
614                 }
615                 ++ctx->use_cnt;
616                 return 0;
617         }
619         return module_load(basedir, name, plugin_ctx);
620 } /* sdb_plugin_load */
622 int
623 sdb_plugin_set_info(sdb_plugin_info_t *info, int type, ...)
625         va_list ap;
627         if (! info)
628                 return -1;
630         va_start(ap, type);
632         switch (type) {
633                 case SDB_PLUGIN_INFO_NAME:
634                         {
635                                 char *name = va_arg(ap, char *);
636                                 if (name) {
637                                         if (info->name)
638                                                 free(info->name);
639                                         info->name = strdup(name);
640                                 }
641                         }
642                         break;
643                 case SDB_PLUGIN_INFO_DESC:
644                         {
645                                 char *desc = va_arg(ap, char *);
646                                 if (desc) {
647                                         if (info->description)
648                                                 free(info->description);
649                                         info->description = strdup(desc);
650                                 }
651                         }
652                         break;
653                 case SDB_PLUGIN_INFO_COPYRIGHT:
654                         {
655                                 char *copyright = va_arg(ap, char *);
656                                 if (copyright)
657                                         info->copyright = strdup(copyright);
658                         }
659                         break;
660                 case SDB_PLUGIN_INFO_LICENSE:
661                         {
662                                 char *license = va_arg(ap, char *);
663                                 if (license) {
664                                         if (info->license)
665                                                 free(info->license);
666                                         info->license = strdup(license);
667                                 }
668                         }
669                         break;
670                 case SDB_PLUGIN_INFO_VERSION:
671                         {
672                                 int version = va_arg(ap, int);
673                                 info->version = version;
674                         }
675                         break;
676                 case SDB_PLUGIN_INFO_PLUGIN_VERSION:
677                         {
678                                 int version = va_arg(ap, int);
679                                 info->plugin_version = version;
680                         }
681                         break;
682                 default:
683                         va_end(ap);
684                         return -1;
685         }
687         va_end(ap);
688         return 0;
689 } /* sdb_plugin_set_info */
691 int
692 sdb_plugin_register_config(const char *name, sdb_plugin_config_cb callback)
694         return plugin_add_callback(&config_list, "init", name,
695                         (void *)callback, NULL);
696 } /* sdb_plugin_register_config */
698 int
699 sdb_plugin_register_init(const char *name, sdb_plugin_init_cb callback,
700                 sdb_object_t *user_data)
702         return plugin_add_callback(&init_list, "init", name,
703                         (void *)callback, user_data);
704 } /* sdb_plugin_register_init */
706 int
707 sdb_plugin_register_shutdown(const char *name, sdb_plugin_shutdown_cb callback,
708                 sdb_object_t *user_data)
710         return plugin_add_callback(&shutdown_list, "shutdown", name,
711                         (void *)callback, user_data);
712 } /* sdb_plugin_register_shutdown */
714 int
715 sdb_plugin_register_log(const char *name, sdb_plugin_log_cb callback,
716                 sdb_object_t *user_data)
718         return plugin_add_callback(&log_list, "log", name, (void *)callback,
719                         user_data);
720 } /* sdb_plugin_register_log */
722 int
723 sdb_plugin_register_cname(const char *name, sdb_plugin_cname_cb callback,
724                 sdb_object_t *user_data)
726         return plugin_add_callback(&cname_list, "cname", name, (void *)callback,
727                         user_data);
728 } /* sdb_plugin_register_cname */
730 int
731 sdb_plugin_register_collector(const char *name, sdb_plugin_collector_cb callback,
732                 const sdb_time_t *interval, sdb_object_t *user_data)
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         obj = sdb_object_create(name, sdb_plugin_collector_cb_type,
745                         &collector_list, "collector", callback, user_data);
746         if (! obj)
747                 return -1;
749         if (interval)
750                 SDB_PLUGIN_CCB(obj)->ccb_interval = *interval;
751         else {
752                 sdb_time_t tmp = sdb_plugin_get_ctx().interval;
754                 if (tmp > 0)
755                         SDB_PLUGIN_CCB(obj)->ccb_interval = tmp;
756                 else
757                         SDB_PLUGIN_CCB(obj)->ccb_interval = 0;
758         }
760         if (! (SDB_PLUGIN_CCB(obj)->ccb_next_update = sdb_gettime())) {
761                 char errbuf[1024];
762                 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
763                                 "time: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
764                 sdb_object_deref(obj);
765                 return -1;
766         }
768         if (sdb_llist_insert_sorted(collector_list, obj,
769                                 plugin_cmp_next_update)) {
770                 sdb_object_deref(obj);
771                 return -1;
772         }
774         /* pass control to the list */
775         sdb_object_deref(obj);
777         sdb_log(SDB_LOG_INFO, "core: Registered collector callback '%s' "
778                         "(interval = %.3fs).", name,
779                         SDB_TIME_TO_DOUBLE(SDB_PLUGIN_CCB(obj)->ccb_interval));
780         return 0;
781 } /* sdb_plugin_register_collector */
783 sdb_plugin_ctx_t
784 sdb_plugin_get_ctx(void)
786         ctx_t *c;
788         c = ctx_get();
789         if (! c) {
790                 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid read access to plugin "
791                                 "context outside a plugin");
792                 return plugin_default_ctx;
793         }
794         return c->public;
795 } /* sdb_plugin_get_ctx */
797 int
798 sdb_plugin_set_ctx(sdb_plugin_ctx_t ctx, sdb_plugin_ctx_t *old)
800         ctx_t *c;
802         c = ctx_get();
803         if (! c) {
804                 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid write access to plugin "
805                                 "context outside a plugin");
806                 return -1;
807         }
809         if (old)
810                 *old = c->public;
811         c->public = ctx;
812         return 0;
813 } /* sdb_plugin_set_ctx */
815 int
816 sdb_plugin_configure(const char *name, oconfig_item_t *ci)
818         sdb_plugin_cb_t *plugin;
819         sdb_plugin_config_cb callback;
821         ctx_t *old_ctx;
823         int status;
825         if ((! name) || (! ci))
826                 return -1;
828         plugin = SDB_PLUGIN_CB(sdb_llist_search_by_name(config_list, name));
829         if (! plugin) {
830                 ctx_t *ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
831                 if (! ctx)
832                         sdb_log(SDB_LOG_ERR, "core: Plugin '%s' not loaded.", name);
833                 else
834                         sdb_log(SDB_LOG_ERR, "core: Plugin '%s' did not register "
835                                         "a config callback.", name);
836                 errno = ENOENT;
837                 return -1;
838         }
840         old_ctx = ctx_set(plugin->cb_ctx);
841         callback = (sdb_plugin_config_cb)plugin->cb_callback;
842         status = callback(ci);
843         ctx_set(old_ctx);
844         return status;
845 } /* sdb_plugin_configure */
847 int
848 sdb_plugin_reconfigure_init(void)
850         sdb_llist_iter_t *iter;
852         iter = sdb_llist_get_iter(config_list);
853         if (config_list && (! iter))
854                 return -1;
856         /* deconfigure all plugins */
857         while (sdb_llist_iter_has_next(iter)) {
858                 sdb_plugin_cb_t *plugin;
859                 sdb_plugin_config_cb callback;
860                 ctx_t *old_ctx;
862                 plugin = SDB_PLUGIN_CB(sdb_llist_iter_get_next(iter));
863                 old_ctx = ctx_set(plugin->cb_ctx);
864                 callback = (sdb_plugin_config_cb)plugin->cb_callback;
865                 callback(NULL);
866                 ctx_set(old_ctx);
867         }
868         sdb_llist_iter_destroy(iter);
870         iter = sdb_llist_get_iter(all_plugins);
871         if (all_plugins && (! iter))
872                 return -1;
874         /* record all plugins as being unused */
875         while (sdb_llist_iter_has_next(iter))
876                 CTX(sdb_llist_iter_get_next(iter))->use_cnt = 0;
877         sdb_llist_iter_destroy(iter);
879         plugin_unregister_all();
880         return 0;
881 } /* sdb_plugin_reconfigure_init */
883 int
884 sdb_plugin_reconfigure_finish(void)
886         sdb_llist_iter_t *iter;
888         iter = sdb_llist_get_iter(all_plugins);
889         if (all_plugins && (! iter))
890                 return -1;
892         while (sdb_llist_iter_has_next(iter)) {
893                 ctx_t *ctx = CTX(sdb_llist_iter_get_next(iter));
894                 if (ctx->use_cnt)
895                         continue;
897                 sdb_log(SDB_LOG_INFO, "core: Module %s no longer in use",
898                                 ctx->info.plugin_name);
899                 sdb_llist_iter_remove_current(iter);
900                 plugin_unregister_by_name(ctx->info.plugin_name);
901                 sdb_object_deref(SDB_OBJ(ctx));
902         }
903         sdb_llist_iter_destroy(iter);
904         return 0;
905 } /* sdb_plugin_reconfigure_finish */
907 int
908 sdb_plugin_init_all(void)
910         sdb_llist_iter_t *iter;
911         int ret = 0;
913         iter = sdb_llist_get_iter(init_list);
914         while (sdb_llist_iter_has_next(iter)) {
915                 sdb_plugin_cb_t *cb;
916                 sdb_plugin_init_cb callback;
917                 ctx_t *old_ctx;
919                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
920                 assert(obj);
921                 cb = SDB_PLUGIN_CB(obj);
923                 callback = (sdb_plugin_init_cb)cb->cb_callback;
925                 old_ctx = ctx_set(cb->cb_ctx);
926                 if (callback(cb->cb_user_data)) {
927                         sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin "
928                                         "'%s'. Unregistering all callbacks.", obj->name);
929                         ctx_set(old_ctx);
930                         plugin_unregister_by_name(cb->cb_ctx->info.plugin_name);
931                         ++ret;
932                 }
933                 else
934                         ctx_set(old_ctx);
935         }
936         sdb_llist_iter_destroy(iter);
937         return ret;
938 } /* sdb_plugin_init_all */
940 int
941 sdb_plugin_shutdown_all(void)
943         sdb_llist_iter_t *iter;
944         int ret = 0;
946         iter = sdb_llist_get_iter(shutdown_list);
947         while (sdb_llist_iter_has_next(iter)) {
948                 sdb_plugin_cb_t *cb;
949                 sdb_plugin_shutdown_cb callback;
950                 ctx_t *old_ctx;
952                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
953                 assert(obj);
954                 cb = SDB_PLUGIN_CB(obj);
956                 callback = (sdb_plugin_shutdown_cb)cb->cb_callback;
958                 old_ctx = ctx_set(cb->cb_ctx);
959                 if (callback(cb->cb_user_data)) {
960                         sdb_log(SDB_LOG_ERR, "core: Failed to shutdown plugin '%s'.",
961                                         obj->name);
962                         ++ret;
963                 }
964                 ctx_set(old_ctx);
965         }
966         sdb_llist_iter_destroy(iter);
967         return ret;
968 } /* sdb_plugin_shutdown_all */
970 int
971 sdb_plugin_collector_loop(sdb_plugin_loop_t *loop)
973         if (! collector_list) {
974                 sdb_log(SDB_LOG_WARNING, "core: No collectors registered. "
975                                 "Quiting main loop.");
976                 return -1;
977         }
979         if (! loop)
980                 return -1;
982         while (loop->do_loop) {
983                 sdb_plugin_collector_cb callback;
984                 ctx_t *old_ctx;
986                 sdb_time_t interval, now;
988                 sdb_object_t *obj = sdb_llist_shift(collector_list);
989                 if (! obj)
990                         return -1;
992                 callback = (sdb_plugin_collector_cb)SDB_PLUGIN_CCB(obj)->ccb_callback;
994                 if (! (now = sdb_gettime())) {
995                         char errbuf[1024];
996                         sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
997                                         "time in collector main loop: %s",
998                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
999                         now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
1000                 }
1002                 if (now < SDB_PLUGIN_CCB(obj)->ccb_next_update) {
1003                         interval = SDB_PLUGIN_CCB(obj)->ccb_next_update - now;
1005                         errno = 0;
1006                         while (loop->do_loop && sdb_sleep(interval, &interval)) {
1007                                 if (errno != EINTR) {
1008                                         char errbuf[1024];
1009                                         sdb_log(SDB_LOG_ERR, "core: Failed to sleep "
1010                                                         "in collector main loop: %s",
1011                                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
1012                                         sdb_llist_insert_sorted(collector_list, obj,
1013                                                         plugin_cmp_next_update);
1014                                         sdb_object_deref(obj);
1015                                         return -1;
1016                                 }
1017                                 errno = 0;
1018                         }
1020                         if (! loop->do_loop) {
1021                                 /* put back; don't worry about errors */
1022                                 sdb_llist_insert_sorted(collector_list, obj,
1023                                                 plugin_cmp_next_update);
1024                                 sdb_object_deref(obj);
1025                                 return 0;
1026                         }
1027                 }
1029                 old_ctx = ctx_set(SDB_PLUGIN_CCB(obj)->ccb_ctx);
1030                 if (callback(SDB_PLUGIN_CCB(obj)->ccb_user_data)) {
1031                         /* XXX */
1032                 }
1033                 ctx_set(old_ctx);
1035                 interval = SDB_PLUGIN_CCB(obj)->ccb_interval;
1036                 if (! interval)
1037                         interval = loop->default_interval;
1038                 if (! interval) {
1039                         sdb_log(SDB_LOG_WARNING, "core: No interval configured "
1040                                         "for plugin '%s'; skipping any further "
1041                                         "iterations.", obj->name);
1042                         sdb_object_deref(obj);
1043                         continue;
1044                 }
1046                 SDB_PLUGIN_CCB(obj)->ccb_next_update += interval;
1048                 if (! (now = sdb_gettime())) {
1049                         char errbuf[1024];
1050                         sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
1051                                         "time in collector main loop: %s",
1052                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
1053                         now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
1054                 }
1056                 if (now > SDB_PLUGIN_CCB(obj)->ccb_next_update) {
1057                         sdb_log(SDB_LOG_WARNING, "core: Plugin '%s' took too "
1058                                         "long; skipping iterations to keep up.",
1059                                         obj->name);
1060                         SDB_PLUGIN_CCB(obj)->ccb_next_update = now;
1061                 }
1063                 if (sdb_llist_insert_sorted(collector_list, obj,
1064                                         plugin_cmp_next_update)) {
1065                         sdb_log(SDB_LOG_ERR, "core: Failed to re-insert "
1066                                         "plugin '%s' into collector list. Unable to further "
1067                                         "use the plugin.",
1068                                         obj->name);
1069                         sdb_object_deref(obj);
1070                         return -1;
1071                 }
1073                 /* pass control back to the list */
1074                 sdb_object_deref(obj);
1075         }
1076         return 0;
1077 } /* sdb_plugin_read_loop */
1079 char *
1080 sdb_plugin_cname(char *hostname)
1082         sdb_llist_iter_t *iter;
1084         if (! hostname)
1085                 return NULL;
1087         if (! cname_list)
1088                 return hostname;
1090         iter = sdb_llist_get_iter(cname_list);
1091         while (sdb_llist_iter_has_next(iter)) {
1092                 sdb_plugin_cname_cb callback;
1093                 char *cname;
1095                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
1096                 assert(obj);
1098                 callback = (sdb_plugin_cname_cb)SDB_PLUGIN_CB(obj)->cb_callback;
1099                 cname = callback(hostname, SDB_PLUGIN_CB(obj)->cb_user_data);
1100                 if (cname) {
1101                         free(hostname);
1102                         hostname = cname;
1103                 }
1104                 /* else: don't change hostname */
1105         }
1106         sdb_llist_iter_destroy(iter);
1107         return hostname;
1108 } /* sdb_plugin_cname */
1110 int
1111 sdb_plugin_log(int prio, const char *msg)
1113         sdb_llist_iter_t *iter;
1114         int ret = -1;
1116         if (! msg)
1117                 return 0;
1119         if (! sdb_llist_len(log_list))
1120                 return fprintf(stderr, "[%s] %s\n", SDB_LOG_PRIO_TO_STRING(prio), msg);
1122         iter = sdb_llist_get_iter(log_list);
1123         while (sdb_llist_iter_has_next(iter)) {
1124                 sdb_plugin_log_cb callback;
1125                 int tmp;
1127                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
1128                 assert(obj);
1130                 callback = (sdb_plugin_log_cb)SDB_PLUGIN_CB(obj)->cb_callback;
1131                 tmp = callback(prio, msg, SDB_PLUGIN_CB(obj)->cb_user_data);
1132                 if (tmp > ret)
1133                         ret = tmp;
1134         }
1135         sdb_llist_iter_destroy(iter);
1136         return ret;
1137 } /* sdb_plugin_log */
1139 int
1140 sdb_plugin_vlogf(int prio, const char *fmt, va_list ap)
1142         sdb_strbuf_t *buf;
1143         int ret;
1145         if (! fmt)
1146                 return 0;
1148         buf = sdb_strbuf_create(64);
1149         if (! buf) {
1150                 ret = fprintf(stderr, "[%s] ", SDB_LOG_PRIO_TO_STRING(prio));
1151                 ret += vfprintf(stderr, fmt, ap);
1152                 return ret;
1153         }
1155         if (sdb_strbuf_vsprintf(buf, fmt, ap) < 0) {
1156                 sdb_strbuf_destroy(buf);
1157                 return -1;
1158         }
1160         ret = sdb_plugin_log(prio, sdb_strbuf_string(buf));
1161         sdb_strbuf_destroy(buf);
1162         return ret;
1163 } /* sdb_plugin_vlogf */
1165 int
1166 sdb_plugin_logf(int prio, const char *fmt, ...)
1168         va_list ap;
1169         int ret;
1171         if (! fmt)
1172                 return 0;
1174         va_start(ap, fmt);
1175         ret = sdb_plugin_vlogf(prio, fmt, ap);
1176         va_end(ap);
1177         return ret;
1178 } /* sdb_plugin_logf */
1180 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */