Code

plugin: Let ctx_create() use ctx_set().
[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 /*
141  * private helper functions
142  */
144 static void
145 plugin_info_clear(sdb_plugin_info_t *info)
147         sdb_plugin_info_t empty_info = SDB_PLUGIN_INFO_INIT;
148         if (! info)
149                 return;
151         if (info->plugin_name)
152                 free(info->plugin_name);
153         if (info->filename)
154                 free(info->filename);
156         if (info->name)
157                 free(info->name);
158         if (info->description)
159                 free(info->description);
160         if (info->copyright)
161                 free(info->copyright);
162         if (info->license)
163                 free(info->license);
165         *info = empty_info;
166 } /* plugin_info_clear */
168 static void
169 ctx_key_init(void)
171         if (plugin_ctx_key_initialized)
172                 return;
174         pthread_key_create(&plugin_ctx_key, /* destructor */ NULL);
175         plugin_ctx_key_initialized = 1;
176 } /* ctx_key_init */
178 static int
179 plugin_cmp_next_update(const sdb_object_t *a, const sdb_object_t *b)
181         const sdb_plugin_collector_cb_t *ccb1
182                 = (const sdb_plugin_collector_cb_t *)a;
183         const sdb_plugin_collector_cb_t *ccb2
184                 = (const sdb_plugin_collector_cb_t *)b;
186         assert(ccb1 && ccb2);
188         return (ccb1->ccb_next_update > ccb2->ccb_next_update)
189                 ? 1 : (ccb1->ccb_next_update < ccb2->ccb_next_update)
190                 ? -1 : 0;
191 } /* plugin_cmp_next_update */
193 static int
194 plugin_lookup_by_name(const sdb_object_t *obj, const void *id)
196         const sdb_plugin_cb_t *cb = SDB_CONST_PLUGIN_CB(obj);
197         const char *name = id;
199         assert(cb && id && cb->cb_ctx);
200         if (!strcasecmp(cb->cb_ctx->info.plugin_name, name))
201                 return 0;
202         return 1;
203 } /* plugin_lookup_by_name */
205 static void
206 plugin_unregister_by_name(const char *plugin_name)
208         sdb_object_t *obj;
209         size_t i;
211         struct {
212                 const char  *type;
213                 sdb_llist_t *list;
214         } all_lists[] = {
215                 { "config",    config_list },
216                 { "init",      init_list },
217                 { "collector", collector_list },
218                 { "cname",     cname_list },
219                 { "shutdown",  shutdown_list },
220                 { "log",       log_list },
221         };
223         for (i = 0; i < SDB_STATIC_ARRAY_LEN(all_lists); ++i) {
224                 const char  *type = all_lists[i].type;
225                 sdb_llist_t *list = all_lists[i].list;
227                 while (1) {
228                         sdb_plugin_cb_t *cb;
230                         cb = SDB_PLUGIN_CB(sdb_llist_remove(list,
231                                                 plugin_lookup_by_name, plugin_name));
232                         if (! cb)
233                                 break;
235                         sdb_log(SDB_LOG_INFO, "core: Unregistering "
236                                         "%s callback '%s' (module %s)", type, cb->super.name,
237                                         cb->cb_ctx->info.plugin_name);
238                         sdb_object_deref(SDB_OBJ(cb));
239                 }
240         }
242         obj = sdb_llist_search_by_name(all_plugins, plugin_name);
243         if (obj->ref_cnt <= 1)
244                 sdb_llist_remove_by_name(all_plugins, plugin_name);
245         /* else: other callbacks still reference it */
246         sdb_object_deref(obj);
247 } /* plugin_unregister_by_name */
249 /*
250  * private types
251  */
253 static int
254 ctx_init(sdb_object_t *obj, va_list __attribute__((unused)) ap)
256         ctx_t *ctx = CTX(obj);
258         assert(ctx);
260         ctx->public = plugin_default_ctx;
261         ctx->info = plugin_default_info;
262         ctx->handle = NULL;
263         ctx->use_cnt = 1;
264         return 0;
265 } /* ctx_init */
267 static void
268 ctx_destroy(sdb_object_t *obj)
270         ctx_t *ctx = CTX(obj);
272         if (ctx->handle) {
273                 const char *err;
275                 sdb_log(SDB_LOG_INFO, "core: Unloading module %s",
276                                 ctx->info.plugin_name);
278                 lt_dlerror();
279                 lt_dlclose(ctx->handle);
280                 if ((err = lt_dlerror()))
281                         sdb_log(SDB_LOG_WARNING, "core: Failed to unload module %s: %s",
282                                         ctx->info.plugin_name, err);
283         }
285         plugin_info_clear(&ctx->info);
286 } /* ctx_destroy */
288 static sdb_type_t ctx_type = {
289         sizeof(ctx_t),
291         ctx_init,
292         ctx_destroy
293 };
295 static ctx_t *
296 ctx_get(void)
298         if (! plugin_ctx_key_initialized)
299                 ctx_key_init();
300         return pthread_getspecific(plugin_ctx_key);
301 } /* ctx_get */
303 static ctx_t *
304 ctx_set(ctx_t *new)
306         ctx_t *old;
308         if (! plugin_ctx_key_initialized)
309                 ctx_key_init();
311         old = pthread_getspecific(plugin_ctx_key);
312         if (old)
313                 sdb_object_deref(SDB_OBJ(old));
314         if (new)
315                 sdb_object_ref(SDB_OBJ(new));
316         pthread_setspecific(plugin_ctx_key, new);
317         return old;
318 } /* ctx_set */
320 static ctx_t *
321 ctx_create(const char *name)
323         ctx_t *ctx;
325         ctx = CTX(sdb_object_create(name, ctx_type));
326         if (! ctx)
327                 return NULL;
329         if (! plugin_ctx_key_initialized)
330                 ctx_key_init();
331         ctx_set(ctx);
332         return ctx;
333 } /* ctx_create */
335 static int
336 plugin_cb_init(sdb_object_t *obj, va_list ap)
338         sdb_llist_t **list = va_arg(ap, sdb_llist_t **);
339         const char   *type = va_arg(ap, const char *);
340         void     *callback = va_arg(ap, void *);
341         sdb_object_t   *ud = va_arg(ap, sdb_object_t *);
343         assert(list);
344         assert(type);
345         assert(obj);
347         if (sdb_llist_search_by_name(*list, obj->name)) {
348                 sdb_log(SDB_LOG_WARNING, "core: %s callback '%s' "
349                                 "has already been registered. Ignoring newly "
350                                 "registered version.", type, obj->name);
351                 return -1;
352         }
354         SDB_PLUGIN_CB(obj)->cb_callback = callback;
355         SDB_PLUGIN_CB(obj)->cb_ctx      = ctx_get();
356         sdb_object_ref(SDB_OBJ(SDB_PLUGIN_CB(obj)->cb_ctx));
358         sdb_object_ref(ud);
359         SDB_PLUGIN_CB(obj)->cb_user_data = ud;
360         return 0;
361 } /* plugin_cb_init */
363 static void
364 plugin_cb_destroy(sdb_object_t *obj)
366         assert(obj);
367         sdb_object_deref(SDB_PLUGIN_CB(obj)->cb_user_data);
368         sdb_object_deref(SDB_OBJ(SDB_PLUGIN_CB(obj)->cb_ctx));
369 } /* plugin_cb_destroy */
371 static sdb_type_t sdb_plugin_cb_type = {
372         sizeof(sdb_plugin_cb_t),
374         plugin_cb_init,
375         plugin_cb_destroy
376 };
378 static sdb_type_t sdb_plugin_collector_cb_type = {
379         sizeof(sdb_plugin_collector_cb_t),
381         plugin_cb_init,
382         plugin_cb_destroy
383 };
385 static int
386 module_init(const char *name, lt_dlhandle lh, sdb_plugin_info_t *info)
388         int (*mod_init)(sdb_plugin_info_t *);
389         int status;
391         mod_init = (int (*)(sdb_plugin_info_t *))lt_dlsym(lh, "sdb_module_init");
392         if (! mod_init) {
393                 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': "
394                                 "could not find symbol 'sdb_module_init'", name);
395                 return -1;
396         }
398         status = mod_init(info);
399         if (status) {
400                 sdb_log(SDB_LOG_ERR, "core: Failed to initialize "
401                                 "module '%s'", name);
402                 plugin_unregister_by_name(name);
403                 return -1;
404         }
405         return 0;
406 } /* module_init */
408 static int
409 module_load(const char *name, const sdb_plugin_ctx_t *plugin_ctx)
411         char  base_name[name ? strlen(name) + 1 : 1];
412         const char *name_ptr;
413         char *tmp;
415         char filename[1024];
416         lt_dlhandle lh;
418         ctx_t *ctx;
420         int status;
422         base_name[0] = '\0';
423         name_ptr = name;
425         while ((tmp = strstr(name_ptr, "::"))) {
426                 strncat(base_name, name_ptr, (size_t)(tmp - name_ptr));
427                 strcat(base_name, "/");
428                 name_ptr = tmp + strlen("::");
429         }
430         strcat(base_name, name_ptr);
432         snprintf(filename, sizeof(filename), "%s/%s.so",
433                         PKGLIBDIR, base_name);
434         filename[sizeof(filename) - 1] = '\0';
436         if (access(filename, R_OK)) {
437                 char errbuf[1024];
438                 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s' (%s): %s",
439                                 name, filename, sdb_strerror(errno, errbuf, sizeof(errbuf)));
440                 return -1;
441         }
443         lt_dlinit();
444         lt_dlerror();
446         lh = lt_dlopen(filename);
447         if (! lh) {
448                 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': %s"
449                                 "The most common cause for this problem are missing "
450                                 "dependencies.\n", name, lt_dlerror());
451                 return -1;
452         }
454         if (ctx_get())
455                 sdb_log(SDB_LOG_WARNING, "core: Discarding old plugin context");
457         ctx = ctx_create(name);
458         if (! ctx) {
459                 sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin context");
460                 return -1;
461         }
463         ctx->info.plugin_name = strdup(name);
464         ctx->info.filename = strdup(filename);
465         ctx->handle = lh;
467         if (plugin_ctx)
468                 ctx->public = *plugin_ctx;
470         if ((status = module_init(name, lh, &ctx->info))) {
471                 sdb_object_deref(SDB_OBJ(ctx));
472                 return status;
473         }
475         /* compare minor version */
476         if ((ctx->info.version < 0)
477                         || ((int)(ctx->info.version / 100) != (int)(SDB_VERSION / 100)))
478                 sdb_log(SDB_LOG_WARNING, "core: WARNING: version of "
479                                 "plugin '%s' (%i.%i.%i) does not match our version "
480                                 "(%i.%i.%i); this might cause problems",
481                                 name, SDB_VERSION_DECODE(ctx->info.version),
482                                 SDB_VERSION_DECODE(SDB_VERSION));
484         sdb_llist_append(all_plugins, SDB_OBJ(ctx));
486         sdb_log(SDB_LOG_INFO, "core: Successfully loaded "
487                         "plugin '%s' v%i (%s)\n\t%s\n\tLicense: %s",
488                         INFO_GET(&ctx->info, name), ctx->info.plugin_version,
489                         INFO_GET(&ctx->info, description),
490                         INFO_GET(&ctx->info, copyright),
491                         INFO_GET(&ctx->info, license));
493         /* any registered callbacks took ownership of the context */
494         sdb_object_deref(SDB_OBJ(ctx));
496         /* reset */
497         ctx_set(NULL);
498         return 0;
499 } /* module_load */
501 static int
502 plugin_add_callback(sdb_llist_t **list, const char *type,
503                 const char *name, void *callback, sdb_object_t *user_data)
505         sdb_object_t *obj;
507         if ((! name) || (! callback))
508                 return -1;
510         assert(list);
512         if (! *list)
513                 *list = sdb_llist_create();
514         if (! *list)
515                 return -1;
517         obj = sdb_object_create(name, sdb_plugin_cb_type,
518                         list, type, callback, user_data);
519         if (! obj)
520                 return -1;
522         if (sdb_llist_append(*list, obj)) {
523                 sdb_object_deref(obj);
524                 return -1;
525         }
527         /* pass control to the list */
528         sdb_object_deref(obj);
530         sdb_log(SDB_LOG_INFO, "core: Registered %s callback '%s'.",
531                         type, name);
532         return 0;
533 } /* plugin_add_callback */
535 /*
536  * public API
537  */
539 int
540 sdb_plugin_load(const char *name, const sdb_plugin_ctx_t *plugin_ctx)
542         ctx_t *ctx;
544         if ((! name) || (! *name))
545                 return -1;
547         if (! all_plugins) {
548                 if (! (all_plugins = sdb_llist_create())) {
549                         sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': "
550                                         "internal error while creating linked list", name);
551                         return -1;
552                 }
553         }
555         ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
556         if (ctx) {
557                 /* plugin already loaded */
558                 ++ctx->use_cnt;
559                 return 0;
560         }
562         return module_load(name, plugin_ctx);
563 } /* sdb_plugin_load */
565 int
566 sdb_plugin_set_info(sdb_plugin_info_t *info, int type, ...)
568         va_list ap;
570         if (! info)
571                 return -1;
573         va_start(ap, type);
575         switch (type) {
576                 case SDB_PLUGIN_INFO_NAME:
577                         {
578                                 char *name = va_arg(ap, char *);
579                                 if (name) {
580                                         if (info->name)
581                                                 free(info->name);
582                                         info->name = strdup(name);
583                                 }
584                         }
585                         break;
586                 case SDB_PLUGIN_INFO_DESC:
587                         {
588                                 char *desc = va_arg(ap, char *);
589                                 if (desc) {
590                                         if (info->description)
591                                                 free(info->description);
592                                         info->description = strdup(desc);
593                                 }
594                         }
595                         break;
596                 case SDB_PLUGIN_INFO_COPYRIGHT:
597                         {
598                                 char *copyright = va_arg(ap, char *);
599                                 if (copyright)
600                                         info->copyright = strdup(copyright);
601                         }
602                         break;
603                 case SDB_PLUGIN_INFO_LICENSE:
604                         {
605                                 char *license = va_arg(ap, char *);
606                                 if (license) {
607                                         if (info->license)
608                                                 free(info->license);
609                                         info->license = strdup(license);
610                                 }
611                         }
612                         break;
613                 case SDB_PLUGIN_INFO_VERSION:
614                         {
615                                 int version = va_arg(ap, int);
616                                 info->version = version;
617                         }
618                         break;
619                 case SDB_PLUGIN_INFO_PLUGIN_VERSION:
620                         {
621                                 int version = va_arg(ap, int);
622                                 info->plugin_version = version;
623                         }
624                         break;
625                 default:
626                         va_end(ap);
627                         return -1;
628         }
630         va_end(ap);
631         return 0;
632 } /* sdb_plugin_set_info */
634 int
635 sdb_plugin_register_config(const char *name, sdb_plugin_config_cb callback)
637         return plugin_add_callback(&config_list, "init", name,
638                         (void *)callback, NULL);
639 } /* sdb_plugin_register_config */
641 int
642 sdb_plugin_register_init(const char *name, sdb_plugin_init_cb callback,
643                 sdb_object_t *user_data)
645         return plugin_add_callback(&init_list, "init", name,
646                         (void *)callback, user_data);
647 } /* sdb_plugin_register_init */
649 int
650 sdb_plugin_register_shutdown(const char *name, sdb_plugin_shutdown_cb callback,
651                 sdb_object_t *user_data)
653         return plugin_add_callback(&shutdown_list, "shutdown", name,
654                         (void *)callback, user_data);
655 } /* sdb_plugin_register_shutdown */
657 int
658 sdb_plugin_register_log(const char *name, sdb_plugin_log_cb callback,
659                 sdb_object_t *user_data)
661         return plugin_add_callback(&log_list, "log", name, (void *)callback,
662                         user_data);
663 } /* sdb_plugin_register_log */
665 int
666 sdb_plugin_register_cname(const char *name, sdb_plugin_cname_cb callback,
667                 sdb_object_t *user_data)
669         return plugin_add_callback(&cname_list, "cname", name, (void *)callback,
670                         user_data);
671 } /* sdb_plugin_register_cname */
673 int
674 sdb_plugin_register_collector(const char *name, sdb_plugin_collector_cb callback,
675                 const sdb_time_t *interval, sdb_object_t *user_data)
677         sdb_object_t *obj;
679         if ((! name) || (! callback))
680                 return -1;
682         if (! collector_list)
683                 collector_list = sdb_llist_create();
684         if (! collector_list)
685                 return -1;
687         obj = sdb_object_create(name, sdb_plugin_collector_cb_type,
688                         &collector_list, "collector", callback, user_data);
689         if (! obj)
690                 return -1;
692         if (interval)
693                 SDB_PLUGIN_CCB(obj)->ccb_interval = *interval;
694         else {
695                 sdb_time_t tmp = sdb_plugin_get_ctx().interval;
697                 if (tmp > 0)
698                         SDB_PLUGIN_CCB(obj)->ccb_interval = tmp;
699                 else
700                         SDB_PLUGIN_CCB(obj)->ccb_interval = 0;
701         }
703         if (! (SDB_PLUGIN_CCB(obj)->ccb_next_update = sdb_gettime())) {
704                 char errbuf[1024];
705                 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
706                                 "time: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
707                 sdb_object_deref(obj);
708                 return -1;
709         }
711         if (sdb_llist_insert_sorted(collector_list, obj,
712                                 plugin_cmp_next_update)) {
713                 sdb_object_deref(obj);
714                 return -1;
715         }
717         /* pass control to the list */
718         sdb_object_deref(obj);
720         sdb_log(SDB_LOG_INFO, "core: Registered collector callback '%s' "
721                         "(interval = %.3fs).", name,
722                         SDB_TIME_TO_DOUBLE(SDB_PLUGIN_CCB(obj)->ccb_interval));
723         return 0;
724 } /* sdb_plugin_register_collector */
726 sdb_plugin_ctx_t
727 sdb_plugin_get_ctx(void)
729         ctx_t *c;
731         c = ctx_get();
732         if (! c) {
733                 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid read access to plugin "
734                                 "context outside a plugin");
735                 return plugin_default_ctx;
736         }
737         return c->public;
738 } /* sdb_plugin_get_ctx */
740 int
741 sdb_plugin_set_ctx(sdb_plugin_ctx_t ctx, sdb_plugin_ctx_t *old)
743         ctx_t *c;
745         c = ctx_get();
746         if (! c) {
747                 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid write access to plugin "
748                                 "context outside a plugin");
749                 return -1;
750         }
752         if (old)
753                 *old = c->public;
754         c->public = ctx;
755         return 0;
756 } /* sdb_plugin_set_ctx */
758 int
759 sdb_plugin_configure(const char *name, oconfig_item_t *ci)
761         sdb_plugin_cb_t *plugin;
762         sdb_plugin_config_cb callback;
764         ctx_t *old_ctx;
766         int status;
768         if ((! name) || (! ci))
769                 return -1;
771         plugin = SDB_PLUGIN_CB(sdb_llist_search_by_name(config_list, name));
772         if (! plugin) {
773                 ctx_t *ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
774                 if (! ctx)
775                         sdb_log(SDB_LOG_ERR, "core: Plugin '%s' not loaded.", name);
776                 else
777                         sdb_log(SDB_LOG_ERR, "core: Plugin '%s' did not register "
778                                         "a config callback.", name);
779                 errno = ENOENT;
780                 return -1;
781         }
783         old_ctx = ctx_set(plugin->cb_ctx);
784         callback = (sdb_plugin_config_cb)plugin->cb_callback;
785         status = callback(ci);
786         ctx_set(old_ctx);
787         return status;
788 } /* sdb_plugin_configure */
790 int
791 sdb_plugin_init_all(void)
793         sdb_llist_iter_t *iter;
794         int ret = 0;
796         iter = sdb_llist_get_iter(init_list);
797         while (sdb_llist_iter_has_next(iter)) {
798                 sdb_plugin_cb_t *cb;
799                 sdb_plugin_init_cb callback;
800                 ctx_t *old_ctx;
802                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
803                 assert(obj);
804                 cb = SDB_PLUGIN_CB(obj);
806                 callback = (sdb_plugin_init_cb)cb->cb_callback;
808                 old_ctx = ctx_set(cb->cb_ctx);
809                 if (callback(cb->cb_user_data)) {
810                         sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin "
811                                         "'%s'. Unregistering all callbacks.", obj->name);
812                         plugin_unregister_by_name(cb->cb_ctx->info.plugin_name);
813                         ++ret;
814                 }
815                 ctx_set(old_ctx);
816         }
817         sdb_llist_iter_destroy(iter);
818         return ret;
819 } /* sdb_plugin_init_all */
821 int
822 sdb_plugin_collector_loop(sdb_plugin_loop_t *loop)
824         if (! collector_list) {
825                 sdb_log(SDB_LOG_WARNING, "core: No collectors registered. "
826                                 "Quiting main loop.");
827                 return -1;
828         }
830         if (! loop)
831                 return -1;
833         while (loop->do_loop) {
834                 sdb_plugin_collector_cb callback;
835                 ctx_t *old_ctx;
837                 sdb_time_t interval, now;
839                 sdb_object_t *obj = sdb_llist_shift(collector_list);
840                 if (! obj)
841                         return -1;
843                 callback = (sdb_plugin_collector_cb)SDB_PLUGIN_CCB(obj)->ccb_callback;
845                 if (! (now = sdb_gettime())) {
846                         char errbuf[1024];
847                         sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
848                                         "time in collector main loop: %s",
849                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
850                         now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
851                 }
853                 if (now < SDB_PLUGIN_CCB(obj)->ccb_next_update) {
854                         interval = SDB_PLUGIN_CCB(obj)->ccb_next_update - now;
856                         errno = 0;
857                         while (loop->do_loop && sdb_sleep(interval, &interval)) {
858                                 if (errno != EINTR) {
859                                         char errbuf[1024];
860                                         sdb_log(SDB_LOG_ERR, "core: Failed to sleep "
861                                                         "in collector main loop: %s",
862                                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
863                                         return -1;
864                                 }
865                                 errno = 0;
866                         }
868                         if (! loop->do_loop)
869                                 return 0;
870                 }
872                 old_ctx = ctx_set(SDB_PLUGIN_CCB(obj)->ccb_ctx);
873                 if (callback(SDB_PLUGIN_CCB(obj)->ccb_user_data)) {
874                         /* XXX */
875                 }
876                 ctx_set(old_ctx);
878                 interval = SDB_PLUGIN_CCB(obj)->ccb_interval;
879                 if (! interval)
880                         interval = loop->default_interval;
881                 if (! interval) {
882                         sdb_log(SDB_LOG_WARNING, "core: No interval configured "
883                                         "for plugin '%s'; skipping any further "
884                                         "iterations.", obj->name);
885                         sdb_object_deref(obj);
886                         continue;
887                 }
889                 SDB_PLUGIN_CCB(obj)->ccb_next_update += interval;
891                 if (! (now = sdb_gettime())) {
892                         char errbuf[1024];
893                         sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
894                                         "time in collector main loop: %s",
895                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
896                         now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
897                 }
899                 if (now > SDB_PLUGIN_CCB(obj)->ccb_next_update) {
900                         sdb_log(SDB_LOG_WARNING, "core: Plugin '%s' took too "
901                                         "long; skipping iterations to keep up.",
902                                         obj->name);
903                         SDB_PLUGIN_CCB(obj)->ccb_next_update = now;
904                 }
906                 if (sdb_llist_insert_sorted(collector_list, obj,
907                                         plugin_cmp_next_update)) {
908                         sdb_log(SDB_LOG_ERR, "core: Failed to re-insert "
909                                         "plugin '%s' into collector list. Unable to further "
910                                         "use the plugin.",
911                                         obj->name);
912                         sdb_object_deref(obj);
913                         return -1;
914                 }
916                 /* pass control back to the list */
917                 sdb_object_deref(obj);
918         }
919         return 0;
920 } /* sdb_plugin_read_loop */
922 char *
923 sdb_plugin_cname(char *hostname)
925         sdb_llist_iter_t *iter;
927         if (! hostname)
928                 return NULL;
930         if (! cname_list)
931                 return hostname;
933         iter = sdb_llist_get_iter(cname_list);
934         while (sdb_llist_iter_has_next(iter)) {
935                 sdb_plugin_cname_cb callback;
936                 char *cname;
938                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
939                 assert(obj);
941                 callback = (sdb_plugin_cname_cb)SDB_PLUGIN_CB(obj)->cb_callback;
942                 cname = callback(hostname, SDB_PLUGIN_CB(obj)->cb_user_data);
943                 if (cname) {
944                         free(hostname);
945                         hostname = cname;
946                 }
947                 /* else: don't change hostname */
948         }
949         sdb_llist_iter_destroy(iter);
950         return hostname;
951 } /* sdb_plugin_cname */
953 int
954 sdb_plugin_log(int prio, const char *msg)
956         sdb_llist_iter_t *iter;
957         int ret = -1;
959         if (! msg)
960                 return 0;
962         if (! log_list)
963                 return fprintf(stderr, "[%s] %s\n", SDB_LOG_PRIO_TO_STRING(prio), msg);
965         iter = sdb_llist_get_iter(log_list);
966         while (sdb_llist_iter_has_next(iter)) {
967                 sdb_plugin_log_cb callback;
968                 int tmp;
970                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
971                 assert(obj);
973                 callback = (sdb_plugin_log_cb)SDB_PLUGIN_CB(obj)->cb_callback;
974                 tmp = callback(prio, msg, SDB_PLUGIN_CB(obj)->cb_user_data);
975                 if (tmp > ret)
976                         ret = tmp;
977         }
978         sdb_llist_iter_destroy(iter);
979         return ret;
980 } /* sdb_plugin_log */
982 int
983 sdb_plugin_vlogf(int prio, const char *fmt, va_list ap)
985         sdb_strbuf_t *buf;
986         int ret;
988         if (! fmt)
989                 return 0;
991         buf = sdb_strbuf_create(64);
992         if (! buf) {
993                 ret = fprintf(stderr, "[%s] ", SDB_LOG_PRIO_TO_STRING(prio));
994                 ret += vfprintf(stderr, fmt, ap);
995                 return ret;
996         }
998         if (sdb_strbuf_vsprintf(buf, fmt, ap) < 0) {
999                 sdb_strbuf_destroy(buf);
1000                 return -1;
1001         }
1003         ret = sdb_plugin_log(prio, sdb_strbuf_string(buf));
1004         sdb_strbuf_destroy(buf);
1005         return ret;
1006 } /* sdb_plugin_vlogf */
1008 int
1009 sdb_plugin_logf(int prio, const char *fmt, ...)
1011         va_list ap;
1012         int ret;
1014         if (! fmt)
1015                 return 0;
1017         va_start(ap, fmt);
1018         ret = sdb_plugin_vlogf(prio, fmt, ap);
1019         va_end(ap);
1020         return ret;
1021 } /* sdb_plugin_logf */
1023 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */