Code

282a5213a45e98cae33ae3c1fde14484972d972a
[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(sdb_plugin_config_cb callback)
694         ctx_t *ctx = ctx_get();
696         if (! ctx) {
697                 sdb_log(SDB_LOG_ERR, "core: Invalid attempt to register a "
698                                 "config callback from outside a plugin");
699                 return -1;
700         }
701         return plugin_add_callback(&config_list, "init", ctx->info.plugin_name,
702                         (void *)callback, NULL);
703 } /* sdb_plugin_register_config */
705 int
706 sdb_plugin_register_init(const char *name, sdb_plugin_init_cb callback,
707                 sdb_object_t *user_data)
709         return plugin_add_callback(&init_list, "init", name,
710                         (void *)callback, user_data);
711 } /* sdb_plugin_register_init */
713 int
714 sdb_plugin_register_shutdown(const char *name, sdb_plugin_shutdown_cb callback,
715                 sdb_object_t *user_data)
717         return plugin_add_callback(&shutdown_list, "shutdown", name,
718                         (void *)callback, user_data);
719 } /* sdb_plugin_register_shutdown */
721 int
722 sdb_plugin_register_log(const char *name, sdb_plugin_log_cb callback,
723                 sdb_object_t *user_data)
725         return plugin_add_callback(&log_list, "log", name, (void *)callback,
726                         user_data);
727 } /* sdb_plugin_register_log */
729 int
730 sdb_plugin_register_cname(const char *name, sdb_plugin_cname_cb callback,
731                 sdb_object_t *user_data)
733         return plugin_add_callback(&cname_list, "cname", name, (void *)callback,
734                         user_data);
735 } /* sdb_plugin_register_cname */
737 int
738 sdb_plugin_register_collector(const char *name, sdb_plugin_collector_cb callback,
739                 const sdb_time_t *interval, sdb_object_t *user_data)
741         sdb_object_t *obj;
743         if ((! name) || (! callback))
744                 return -1;
746         if (! collector_list)
747                 collector_list = sdb_llist_create();
748         if (! collector_list)
749                 return -1;
751         obj = sdb_object_create(name, sdb_plugin_collector_cb_type,
752                         &collector_list, "collector", callback, user_data);
753         if (! obj)
754                 return -1;
756         if (interval)
757                 SDB_PLUGIN_CCB(obj)->ccb_interval = *interval;
758         else {
759                 sdb_time_t tmp = sdb_plugin_get_ctx().interval;
761                 if (tmp > 0)
762                         SDB_PLUGIN_CCB(obj)->ccb_interval = tmp;
763                 else
764                         SDB_PLUGIN_CCB(obj)->ccb_interval = 0;
765         }
767         if (! (SDB_PLUGIN_CCB(obj)->ccb_next_update = sdb_gettime())) {
768                 char errbuf[1024];
769                 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
770                                 "time: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
771                 sdb_object_deref(obj);
772                 return -1;
773         }
775         if (sdb_llist_insert_sorted(collector_list, obj,
776                                 plugin_cmp_next_update)) {
777                 sdb_object_deref(obj);
778                 return -1;
779         }
781         /* pass control to the list */
782         sdb_object_deref(obj);
784         sdb_log(SDB_LOG_INFO, "core: Registered collector callback '%s' "
785                         "(interval = %.3fs).", name,
786                         SDB_TIME_TO_DOUBLE(SDB_PLUGIN_CCB(obj)->ccb_interval));
787         return 0;
788 } /* sdb_plugin_register_collector */
790 sdb_plugin_ctx_t
791 sdb_plugin_get_ctx(void)
793         ctx_t *c;
795         c = ctx_get();
796         if (! c) {
797                 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid read access to plugin "
798                                 "context outside a plugin");
799                 return plugin_default_ctx;
800         }
801         return c->public;
802 } /* sdb_plugin_get_ctx */
804 int
805 sdb_plugin_set_ctx(sdb_plugin_ctx_t ctx, sdb_plugin_ctx_t *old)
807         ctx_t *c;
809         c = ctx_get();
810         if (! c) {
811                 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid write access to plugin "
812                                 "context outside a plugin");
813                 return -1;
814         }
816         if (old)
817                 *old = c->public;
818         c->public = ctx;
819         return 0;
820 } /* sdb_plugin_set_ctx */
822 int
823 sdb_plugin_configure(const char *name, oconfig_item_t *ci)
825         sdb_plugin_cb_t *plugin;
826         sdb_plugin_config_cb callback;
828         ctx_t *old_ctx;
830         int status;
832         if ((! name) || (! ci))
833                 return -1;
835         plugin = SDB_PLUGIN_CB(sdb_llist_search_by_name(config_list, name));
836         if (! plugin) {
837                 ctx_t *ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
838                 if (! ctx)
839                         sdb_log(SDB_LOG_ERR, "core: Cannot configure unknown "
840                                         "plugin '%s'. Missing 'LoadPlugin \"%s\"'?",
841                                         name, name);
842                 else
843                         sdb_log(SDB_LOG_ERR, "core: Plugin '%s' did not register "
844                                         "a config callback.", name);
845                 errno = ENOENT;
846                 return -1;
847         }
849         old_ctx = ctx_set(plugin->cb_ctx);
850         callback = (sdb_plugin_config_cb)plugin->cb_callback;
851         status = callback(ci);
852         ctx_set(old_ctx);
853         return status;
854 } /* sdb_plugin_configure */
856 int
857 sdb_plugin_reconfigure_init(void)
859         sdb_llist_iter_t *iter;
861         iter = sdb_llist_get_iter(config_list);
862         if (config_list && (! iter))
863                 return -1;
865         /* deconfigure all plugins */
866         while (sdb_llist_iter_has_next(iter)) {
867                 sdb_plugin_cb_t *plugin;
868                 sdb_plugin_config_cb callback;
869                 ctx_t *old_ctx;
871                 plugin = SDB_PLUGIN_CB(sdb_llist_iter_get_next(iter));
872                 old_ctx = ctx_set(plugin->cb_ctx);
873                 callback = (sdb_plugin_config_cb)plugin->cb_callback;
874                 callback(NULL);
875                 ctx_set(old_ctx);
876         }
877         sdb_llist_iter_destroy(iter);
879         iter = sdb_llist_get_iter(all_plugins);
880         if (all_plugins && (! iter))
881                 return -1;
883         /* record all plugins as being unused */
884         while (sdb_llist_iter_has_next(iter))
885                 CTX(sdb_llist_iter_get_next(iter))->use_cnt = 0;
886         sdb_llist_iter_destroy(iter);
888         plugin_unregister_all();
889         return 0;
890 } /* sdb_plugin_reconfigure_init */
892 int
893 sdb_plugin_reconfigure_finish(void)
895         sdb_llist_iter_t *iter;
897         iter = sdb_llist_get_iter(all_plugins);
898         if (all_plugins && (! iter))
899                 return -1;
901         while (sdb_llist_iter_has_next(iter)) {
902                 ctx_t *ctx = CTX(sdb_llist_iter_get_next(iter));
903                 if (ctx->use_cnt)
904                         continue;
906                 sdb_log(SDB_LOG_INFO, "core: Module %s no longer in use",
907                                 ctx->info.plugin_name);
908                 sdb_llist_iter_remove_current(iter);
909                 plugin_unregister_by_name(ctx->info.plugin_name);
910                 sdb_object_deref(SDB_OBJ(ctx));
911         }
912         sdb_llist_iter_destroy(iter);
913         return 0;
914 } /* sdb_plugin_reconfigure_finish */
916 int
917 sdb_plugin_init_all(void)
919         sdb_llist_iter_t *iter;
920         int ret = 0;
922         iter = sdb_llist_get_iter(init_list);
923         while (sdb_llist_iter_has_next(iter)) {
924                 sdb_plugin_cb_t *cb;
925                 sdb_plugin_init_cb callback;
926                 ctx_t *old_ctx;
928                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
929                 assert(obj);
930                 cb = SDB_PLUGIN_CB(obj);
932                 callback = (sdb_plugin_init_cb)cb->cb_callback;
934                 old_ctx = ctx_set(cb->cb_ctx);
935                 if (callback(cb->cb_user_data)) {
936                         sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin "
937                                         "'%s'. Unregistering all callbacks.", obj->name);
938                         ctx_set(old_ctx);
939                         plugin_unregister_by_name(cb->cb_ctx->info.plugin_name);
940                         ++ret;
941                 }
942                 else
943                         ctx_set(old_ctx);
944         }
945         sdb_llist_iter_destroy(iter);
946         return ret;
947 } /* sdb_plugin_init_all */
949 int
950 sdb_plugin_shutdown_all(void)
952         sdb_llist_iter_t *iter;
953         int ret = 0;
955         iter = sdb_llist_get_iter(shutdown_list);
956         while (sdb_llist_iter_has_next(iter)) {
957                 sdb_plugin_cb_t *cb;
958                 sdb_plugin_shutdown_cb callback;
959                 ctx_t *old_ctx;
961                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
962                 assert(obj);
963                 cb = SDB_PLUGIN_CB(obj);
965                 callback = (sdb_plugin_shutdown_cb)cb->cb_callback;
967                 old_ctx = ctx_set(cb->cb_ctx);
968                 if (callback(cb->cb_user_data)) {
969                         sdb_log(SDB_LOG_ERR, "core: Failed to shutdown plugin '%s'.",
970                                         obj->name);
971                         ++ret;
972                 }
973                 ctx_set(old_ctx);
974         }
975         sdb_llist_iter_destroy(iter);
976         return ret;
977 } /* sdb_plugin_shutdown_all */
979 int
980 sdb_plugin_collector_loop(sdb_plugin_loop_t *loop)
982         if (! collector_list) {
983                 sdb_log(SDB_LOG_WARNING, "core: No collectors registered. "
984                                 "Quiting main loop.");
985                 return -1;
986         }
988         if (! loop)
989                 return -1;
991         while (loop->do_loop) {
992                 sdb_plugin_collector_cb callback;
993                 ctx_t *old_ctx;
995                 sdb_time_t interval, now;
997                 sdb_object_t *obj = sdb_llist_shift(collector_list);
998                 if (! obj)
999                         return -1;
1001                 callback = (sdb_plugin_collector_cb)SDB_PLUGIN_CCB(obj)->ccb_callback;
1003                 if (! (now = sdb_gettime())) {
1004                         char errbuf[1024];
1005                         sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
1006                                         "time in collector main loop: %s",
1007                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
1008                         now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
1009                 }
1011                 if (now < SDB_PLUGIN_CCB(obj)->ccb_next_update) {
1012                         interval = SDB_PLUGIN_CCB(obj)->ccb_next_update - now;
1014                         errno = 0;
1015                         while (loop->do_loop && sdb_sleep(interval, &interval)) {
1016                                 if (errno != EINTR) {
1017                                         char errbuf[1024];
1018                                         sdb_log(SDB_LOG_ERR, "core: Failed to sleep "
1019                                                         "in collector main loop: %s",
1020                                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
1021                                         sdb_llist_insert_sorted(collector_list, obj,
1022                                                         plugin_cmp_next_update);
1023                                         sdb_object_deref(obj);
1024                                         return -1;
1025                                 }
1026                                 errno = 0;
1027                         }
1029                         if (! loop->do_loop) {
1030                                 /* put back; don't worry about errors */
1031                                 sdb_llist_insert_sorted(collector_list, obj,
1032                                                 plugin_cmp_next_update);
1033                                 sdb_object_deref(obj);
1034                                 return 0;
1035                         }
1036                 }
1038                 old_ctx = ctx_set(SDB_PLUGIN_CCB(obj)->ccb_ctx);
1039                 if (callback(SDB_PLUGIN_CCB(obj)->ccb_user_data)) {
1040                         /* XXX */
1041                 }
1042                 ctx_set(old_ctx);
1044                 interval = SDB_PLUGIN_CCB(obj)->ccb_interval;
1045                 if (! interval)
1046                         interval = loop->default_interval;
1047                 if (! interval) {
1048                         sdb_log(SDB_LOG_WARNING, "core: No interval configured "
1049                                         "for plugin '%s'; skipping any further "
1050                                         "iterations.", obj->name);
1051                         sdb_object_deref(obj);
1052                         continue;
1053                 }
1055                 SDB_PLUGIN_CCB(obj)->ccb_next_update += interval;
1057                 if (! (now = sdb_gettime())) {
1058                         char errbuf[1024];
1059                         sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
1060                                         "time in collector main loop: %s",
1061                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
1062                         now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
1063                 }
1065                 if (now > SDB_PLUGIN_CCB(obj)->ccb_next_update) {
1066                         sdb_log(SDB_LOG_WARNING, "core: Plugin '%s' took too "
1067                                         "long; skipping iterations to keep up.",
1068                                         obj->name);
1069                         SDB_PLUGIN_CCB(obj)->ccb_next_update = now;
1070                 }
1072                 if (sdb_llist_insert_sorted(collector_list, obj,
1073                                         plugin_cmp_next_update)) {
1074                         sdb_log(SDB_LOG_ERR, "core: Failed to re-insert "
1075                                         "plugin '%s' into collector list. Unable to further "
1076                                         "use the plugin.",
1077                                         obj->name);
1078                         sdb_object_deref(obj);
1079                         return -1;
1080                 }
1082                 /* pass control back to the list */
1083                 sdb_object_deref(obj);
1084         }
1085         return 0;
1086 } /* sdb_plugin_read_loop */
1088 char *
1089 sdb_plugin_cname(char *hostname)
1091         sdb_llist_iter_t *iter;
1093         if (! hostname)
1094                 return NULL;
1096         if (! cname_list)
1097                 return hostname;
1099         iter = sdb_llist_get_iter(cname_list);
1100         while (sdb_llist_iter_has_next(iter)) {
1101                 sdb_plugin_cname_cb callback;
1102                 char *cname;
1104                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
1105                 assert(obj);
1107                 callback = (sdb_plugin_cname_cb)SDB_PLUGIN_CB(obj)->cb_callback;
1108                 cname = callback(hostname, SDB_PLUGIN_CB(obj)->cb_user_data);
1109                 if (cname) {
1110                         free(hostname);
1111                         hostname = cname;
1112                 }
1113                 /* else: don't change hostname */
1114         }
1115         sdb_llist_iter_destroy(iter);
1116         return hostname;
1117 } /* sdb_plugin_cname */
1119 int
1120 sdb_plugin_log(int prio, const char *msg)
1122         sdb_llist_iter_t *iter;
1123         int ret = -1;
1125         _Bool logged = 0;
1127         if (! msg)
1128                 return 0;
1130         iter = sdb_llist_get_iter(log_list);
1131         while (sdb_llist_iter_has_next(iter)) {
1132                 sdb_plugin_log_cb callback;
1133                 int tmp;
1135                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
1136                 assert(obj);
1138                 callback = (sdb_plugin_log_cb)SDB_PLUGIN_CB(obj)->cb_callback;
1139                 tmp = callback(prio, msg, SDB_PLUGIN_CB(obj)->cb_user_data);
1140                 if (tmp > ret)
1141                         ret = tmp;
1143                 if (SDB_PLUGIN_CB(obj)->cb_ctx)
1144                         logged = 1;
1145                 /* else: this is an internally registered callback */
1146         }
1147         sdb_llist_iter_destroy(iter);
1149         if (! logged)
1150                 return fprintf(stderr, "[%s] %s\n", SDB_LOG_PRIO_TO_STRING(prio), msg);
1151         return ret;
1152 } /* sdb_plugin_log */
1154 int
1155 sdb_plugin_vlogf(int prio, const char *fmt, va_list ap)
1157         sdb_strbuf_t *buf;
1158         int ret;
1160         if (! fmt)
1161                 return 0;
1163         buf = sdb_strbuf_create(64);
1164         if (! buf) {
1165                 ret = fprintf(stderr, "[%s] ", SDB_LOG_PRIO_TO_STRING(prio));
1166                 ret += vfprintf(stderr, fmt, ap);
1167                 return ret;
1168         }
1170         if (sdb_strbuf_vsprintf(buf, fmt, ap) < 0) {
1171                 sdb_strbuf_destroy(buf);
1172                 return -1;
1173         }
1175         ret = sdb_plugin_log(prio, sdb_strbuf_string(buf));
1176         sdb_strbuf_destroy(buf);
1177         return ret;
1178 } /* sdb_plugin_vlogf */
1180 int
1181 sdb_plugin_logf(int prio, const char *fmt, ...)
1183         va_list ap;
1184         int ret;
1186         if (! fmt)
1187                 return 0;
1189         va_start(ap, fmt);
1190         ret = sdb_plugin_vlogf(prio, fmt, ap);
1191         va_end(ap);
1192         return ret;
1193 } /* sdb_plugin_logf */
1195 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */