Code

plugin: Record all loaded plugins and use that for improved error messages.
[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 #include "sysdb.h"
29 #include "core/plugin.h"
30 #include "core/time.h"
31 #include "utils/error.h"
32 #include "utils/llist.h"
33 #include "utils/strbuf.h"
35 #include <assert.h>
37 #include <errno.h>
39 #include <stdarg.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <strings.h>
43 #include <unistd.h>
45 #include <ltdl.h>
47 #include <pthread.h>
49 /*
50  * private data types
51  */
53 struct sdb_plugin_info {
54         char *plugin_name;
55         char *filename;
57         /* public attributes */
58         char *name;
60         char *description;
61         char *copyright;
62         char *license;
64         int   version;
65         int   plugin_version;
66 };
67 #define SDB_PLUGIN_INFO_INIT { \
68         /* plugin_name */ NULL, /* filename */ NULL, \
69         /* name */ NULL, /* desc */ NULL, \
70         /* copyright */ NULL, /* license */ NULL, \
71         /* version */ -1, /* plugin_version */ -1 }
72 #define INFO_GET(i, attr) \
73         ((i)->attr ? (i)->attr : #attr" not set")
75 typedef struct {
76         sdb_object_t super;
77         sdb_plugin_ctx_t public;
79         sdb_plugin_info_t info;
80 } ctx_t;
81 #define CTX_INIT { SDB_OBJECT_INIT, \
82         SDB_PLUGIN_CTX_INIT, SDB_PLUGIN_INFO_INIT }
84 #define CTX(obj) ((ctx_t *)(obj))
86 typedef struct {
87         sdb_object_t super;
88         void *cb_callback;
89         sdb_object_t *cb_user_data;
90         ctx_t *cb_ctx;
91 } sdb_plugin_cb_t;
92 #define SDB_PLUGIN_CB_INIT { SDB_OBJECT_INIT, \
93         /* callback = */ NULL, /* user_data = */ NULL, \
94         SDB_PLUGIN_CTX_INIT }
96 typedef struct {
97         sdb_plugin_cb_t super;
98 #define ccb_callback super.cb_callback
99 #define ccb_user_data super.cb_user_data
100 #define ccb_ctx super.cb_ctx
101         sdb_time_t ccb_interval;
102         sdb_time_t ccb_next_update;
103 } sdb_plugin_collector_cb_t;
105 #define SDB_PLUGIN_CB(obj) ((sdb_plugin_cb_t *)(obj))
106 #define SDB_CONST_PLUGIN_CB(obj) ((const sdb_plugin_cb_t *)(obj))
107 #define SDB_PLUGIN_CCB(obj) ((sdb_plugin_collector_cb_t *)(obj))
108 #define SDB_CONST_PLUGIN_CCB(obj) ((const sdb_plugin_collector_cb_t *)(obj))
110 /*
111  * private variables
112  */
114 static sdb_plugin_ctx_t  plugin_default_ctx  = SDB_PLUGIN_CTX_INIT;
115 static sdb_plugin_info_t plugin_default_info = SDB_PLUGIN_INFO_INIT;
117 static pthread_key_t     plugin_ctx_key;
118 static _Bool             plugin_ctx_key_initialized = 0;
120 /* a list of the plugin contexts of all registered plugins */
121 static sdb_llist_t      *all_plugins = NULL;
123 static sdb_llist_t      *config_list = NULL;
124 static sdb_llist_t      *init_list = NULL;
125 static sdb_llist_t      *collector_list = NULL;
126 static sdb_llist_t      *cname_list = NULL;
127 static sdb_llist_t      *shutdown_list = NULL;
128 static sdb_llist_t      *log_list = NULL;
130 /*
131  * private helper functions
132  */
134 static void
135 plugin_info_clear(sdb_plugin_info_t *info)
137         sdb_plugin_info_t empty_info = SDB_PLUGIN_INFO_INIT;
138         if (! info)
139                 return;
141         if (info->plugin_name)
142                 free(info->plugin_name);
143         if (info->filename)
144                 free(info->filename);
146         if (info->name)
147                 free(info->name);
148         if (info->description)
149                 free(info->description);
150         if (info->copyright)
151                 free(info->copyright);
152         if (info->license)
153                 free(info->license);
155         *info = empty_info;
156 } /* plugin_info_clear */
158 static void
159 ctx_key_init(void)
161         if (plugin_ctx_key_initialized)
162                 return;
164         pthread_key_create(&plugin_ctx_key, /* destructor */ NULL);
165         plugin_ctx_key_initialized = 1;
166 } /* ctx_key_init */
168 static int
169 plugin_cmp_next_update(const sdb_object_t *a, const sdb_object_t *b)
171         const sdb_plugin_collector_cb_t *ccb1
172                 = (const sdb_plugin_collector_cb_t *)a;
173         const sdb_plugin_collector_cb_t *ccb2
174                 = (const sdb_plugin_collector_cb_t *)b;
176         assert(ccb1 && ccb2);
178         return (ccb1->ccb_next_update > ccb2->ccb_next_update)
179                 ? 1 : (ccb1->ccb_next_update < ccb2->ccb_next_update)
180                 ? -1 : 0;
181 } /* plugin_cmp_next_update */
183 static int
184 plugin_lookup_by_name(const sdb_object_t *obj, const void *id)
186         const sdb_plugin_cb_t *cb = SDB_CONST_PLUGIN_CB(obj);
187         const char *name = id;
189         assert(cb && id && cb->cb_ctx);
190         if (!strcasecmp(cb->cb_ctx->info.plugin_name, name))
191                 return 0;
192         return 1;
193 } /* plugin_lookup_by_name */
195 static void
196 plugin_unregister_by_name(const char *plugin_name)
198         size_t i;
200         struct {
201                 const char  *type;
202                 sdb_llist_t *list;
203         } all_lists[] = {
204                 { "config",    config_list },
205                 { "init",      init_list },
206                 { "collector", collector_list },
207                 { "cname",     cname_list },
208                 { "shutdown",  shutdown_list },
209                 { "log",       log_list },
210         };
212         for (i = 0; i < SDB_STATIC_ARRAY_LEN(all_lists); ++i) {
213                 const char  *type = all_lists[i].type;
214                 sdb_llist_t *list = all_lists[i].list;
216                 while (1) {
217                         sdb_object_t *obj = sdb_llist_remove(list,
218                                         plugin_lookup_by_name, plugin_name);
219                         sdb_plugin_cb_t *cb = SDB_PLUGIN_CB(obj);
221                         if (! obj)
222                                 break;
224                         sdb_log(SDB_LOG_INFO, "core: Unregistering "
225                                         "%s callback '%s' (module %s)", type, obj->name,
226                                         cb->cb_ctx->info.plugin_name);
227                         sdb_object_deref(obj);
228                 }
229         }
230 } /* plugin_unregister_by_name */
232 /*
233  * private types
234  */
236 static int
237 ctx_init(sdb_object_t *obj, va_list __attribute__((unused)) ap)
239         ctx_t *ctx = CTX(obj);
241         assert(ctx);
243         ctx->public = plugin_default_ctx;
244         ctx->info = plugin_default_info;
245         return 0;
246 } /* ctx_init */
248 static void
249 ctx_destroy(sdb_object_t *obj)
251         ctx_t *ctx = CTX(obj);
252         plugin_info_clear(&ctx->info);
253 } /* ctx_destroy */
255 static sdb_type_t ctx_type = {
256         sizeof(ctx_t),
258         ctx_init,
259         ctx_destroy
260 };
262 static ctx_t *
263 ctx_create(const char *name)
265         ctx_t *ctx;
267         ctx = CTX(sdb_object_create(name, ctx_type));
268         if (! ctx)
269                 return NULL;
271         if (! plugin_ctx_key_initialized)
272                 ctx_key_init();
273         pthread_setspecific(plugin_ctx_key, ctx);
274         return ctx;
275 } /* ctx_create */
277 static ctx_t *
278 ctx_get(void)
280         if (! plugin_ctx_key_initialized)
281                 ctx_key_init();
282         return pthread_getspecific(plugin_ctx_key);
283 } /* ctx_get */
285 static ctx_t *
286 ctx_set(ctx_t *new)
288         ctx_t *old;
290         if (! plugin_ctx_key_initialized)
291                 ctx_key_init();
293         old = pthread_getspecific(plugin_ctx_key);
294         pthread_setspecific(plugin_ctx_key, new);
295         return old;
296 } /* ctx_set */
298 static int
299 plugin_cb_init(sdb_object_t *obj, va_list ap)
301         sdb_llist_t **list = va_arg(ap, sdb_llist_t **);
302         const char   *type = va_arg(ap, const char *);
303         void     *callback = va_arg(ap, void *);
304         sdb_object_t   *ud = va_arg(ap, sdb_object_t *);
306         assert(list);
307         assert(type);
308         assert(obj);
310         if (sdb_llist_search_by_name(*list, obj->name)) {
311                 sdb_log(SDB_LOG_WARNING, "core: %s callback '%s' "
312                                 "has already been registered. Ignoring newly "
313                                 "registered version.", type, obj->name);
314                 return -1;
315         }
317         SDB_PLUGIN_CB(obj)->cb_callback = callback;
318         SDB_PLUGIN_CB(obj)->cb_ctx      = ctx_get();
319         sdb_object_ref(SDB_OBJ(SDB_PLUGIN_CB(obj)->cb_ctx));
321         sdb_object_ref(ud);
322         SDB_PLUGIN_CB(obj)->cb_user_data = ud;
323         return 0;
324 } /* plugin_cb_init */
326 static void
327 plugin_cb_destroy(sdb_object_t *obj)
329         assert(obj);
330         sdb_object_deref(SDB_PLUGIN_CB(obj)->cb_user_data);
331         sdb_object_deref(SDB_OBJ(SDB_PLUGIN_CB(obj)->cb_ctx));
332 } /* plugin_cb_destroy */
334 static sdb_type_t sdb_plugin_cb_type = {
335         sizeof(sdb_plugin_cb_t),
337         plugin_cb_init,
338         plugin_cb_destroy
339 };
341 static sdb_type_t sdb_plugin_collector_cb_type = {
342         sizeof(sdb_plugin_collector_cb_t),
344         plugin_cb_init,
345         plugin_cb_destroy
346 };
348 static int
349 plugin_add_callback(sdb_llist_t **list, const char *type,
350                 const char *name, void *callback, sdb_object_t *user_data)
352         sdb_object_t *obj;
354         if ((! name) || (! callback))
355                 return -1;
357         assert(list);
359         if (! *list)
360                 *list = sdb_llist_create();
361         if (! *list)
362                 return -1;
364         obj = sdb_object_create(name, sdb_plugin_cb_type,
365                         list, type, callback, user_data);
366         if (! obj)
367                 return -1;
369         if (sdb_llist_append(*list, obj)) {
370                 sdb_object_deref(obj);
371                 return -1;
372         }
374         /* pass control to the list */
375         sdb_object_deref(obj);
377         sdb_log(SDB_LOG_INFO, "core: Registered %s callback '%s'.",
378                         type, name);
379         return 0;
380 } /* plugin_add_callback */
382 /*
383  * public API
384  */
386 int
387 sdb_plugin_load(const char *name, const sdb_plugin_ctx_t *plugin_ctx)
389         char  real_name[strlen(name) > 0 ? strlen(name) : 1];
390         const char *name_ptr;
391         char *tmp;
393         char filename[1024];
394         ctx_t *ctx;
396         lt_dlhandle lh;
398         int (*mod_init)(sdb_plugin_info_t *);
399         int status;
401         if ((! name) || (! *name))
402                 return -1;
404         real_name[0] = '\0';
405         name_ptr = name;
407         while ((tmp = strstr(name_ptr, "::"))) {
408                 strncat(real_name, name_ptr, (size_t)(tmp - name_ptr));
409                 strcat(real_name, "/");
410                 name_ptr = tmp + strlen("::");
411         }
412         strcat(real_name, name_ptr);
414         snprintf(filename, sizeof(filename), "%s/%s.so",
415                         PKGLIBDIR, real_name);
416         filename[sizeof(filename) - 1] = '\0';
418         if (access(filename, R_OK)) {
419                 char errbuf[1024];
420                 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s' (%s): %s",
421                                 name, filename, sdb_strerror(errno, errbuf, sizeof(errbuf)));
422                 return -1;
423         }
425         lt_dlinit();
426         lt_dlerror();
428         lh = lt_dlopen(filename);
429         if (! lh) {
430                 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': %s"
431                                 "The most common cause for this problem are missing "
432                                 "dependencies.\n", name, lt_dlerror());
433                 return -1;
434         }
436         if (ctx_get())
437                 sdb_log(SDB_LOG_WARNING, "core: Discarding old plugin context");
439         ctx = ctx_create(real_name);
440         if (! ctx) {
441                 sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin context");
442                 return -1;
443         }
445         ctx->info.plugin_name = strdup(name);
446         ctx->info.filename = strdup(filename);
448         if (plugin_ctx)
449                 ctx->public = *plugin_ctx;
451         mod_init = (int (*)(sdb_plugin_info_t *))lt_dlsym(lh, "sdb_module_init");
452         if (! mod_init) {
453                 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': "
454                                 "could not find symbol 'sdb_module_init'", name);
455                 sdb_object_deref(SDB_OBJ(ctx));
456                 return -1;
457         }
459         status = mod_init(&ctx->info);
460         if (status) {
461                 sdb_log(SDB_LOG_ERR, "core: Failed to initialize "
462                                 "module '%s'", name);
463                 plugin_unregister_by_name(ctx->info.plugin_name);
464                 sdb_object_deref(SDB_OBJ(ctx));
465                 return -1;
466         }
468         /* compare minor version */
469         if ((ctx->info.version < 0)
470                         || ((int)(ctx->info.version / 100) != (int)(SDB_VERSION / 100)))
471                 sdb_log(SDB_LOG_WARNING, "core: WARNING: version of "
472                                 "plugin '%s' (%i.%i.%i) does not match our version "
473                                 "(%i.%i.%i); this might cause problems",
474                                 name, SDB_VERSION_DECODE(ctx->info.version),
475                                 SDB_VERSION_DECODE(SDB_VERSION));
477         if (! all_plugins) {
478                 if (! (all_plugins = sdb_llist_create())) {
479                         sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': "
480                                         "internal error while creating linked list", name);
481                         plugin_unregister_by_name(ctx->info.plugin_name);
482                         sdb_object_deref(SDB_OBJ(ctx));
483                         return -1;
484                 }
485         }
487         sdb_llist_append(all_plugins, SDB_OBJ(ctx));
489         sdb_log(SDB_LOG_INFO, "core: Successfully loaded "
490                         "plugin '%s' v%i (%s)\n\t%s\n\tLicense: %s",
491                         INFO_GET(&ctx->info, name), ctx->info.plugin_version,
492                         INFO_GET(&ctx->info, description),
493                         INFO_GET(&ctx->info, copyright),
494                         INFO_GET(&ctx->info, license));
496         /* any registered callbacks took ownership of the context */
497         sdb_object_deref(SDB_OBJ(ctx));
499         /* reset */
500         ctx_set(NULL);
501         return 0;
502 } /* sdb_plugin_load */
504 int
505 sdb_plugin_set_info(sdb_plugin_info_t *info, int type, ...)
507         va_list ap;
509         if (! info)
510                 return -1;
512         va_start(ap, type);
514         switch (type) {
515                 case SDB_PLUGIN_INFO_NAME:
516                         {
517                                 char *name = va_arg(ap, char *);
518                                 if (name) {
519                                         if (info->name)
520                                                 free(info->name);
521                                         info->name = strdup(name);
522                                 }
523                         }
524                         break;
525                 case SDB_PLUGIN_INFO_DESC:
526                         {
527                                 char *desc = va_arg(ap, char *);
528                                 if (desc) {
529                                         if (info->description)
530                                                 free(info->description);
531                                         info->description = strdup(desc);
532                                 }
533                         }
534                         break;
535                 case SDB_PLUGIN_INFO_COPYRIGHT:
536                         {
537                                 char *copyright = va_arg(ap, char *);
538                                 if (copyright)
539                                         info->copyright = strdup(copyright);
540                         }
541                         break;
542                 case SDB_PLUGIN_INFO_LICENSE:
543                         {
544                                 char *license = va_arg(ap, char *);
545                                 if (license) {
546                                         if (info->license)
547                                                 free(info->license);
548                                         info->license = strdup(license);
549                                 }
550                         }
551                         break;
552                 case SDB_PLUGIN_INFO_VERSION:
553                         {
554                                 int version = va_arg(ap, int);
555                                 info->version = version;
556                         }
557                         break;
558                 case SDB_PLUGIN_INFO_PLUGIN_VERSION:
559                         {
560                                 int version = va_arg(ap, int);
561                                 info->plugin_version = version;
562                         }
563                         break;
564                 default:
565                         va_end(ap);
566                         return -1;
567         }
569         va_end(ap);
570         return 0;
571 } /* sdb_plugin_set_info */
573 int
574 sdb_plugin_register_config(const char *name, sdb_plugin_config_cb callback)
576         return plugin_add_callback(&config_list, "init", name,
577                         callback, NULL);
578 } /* sdb_plugin_register_config */
580 int
581 sdb_plugin_register_init(const char *name, sdb_plugin_init_cb callback,
582                 sdb_object_t *user_data)
584         return plugin_add_callback(&init_list, "init", name,
585                         callback, user_data);
586 } /* sdb_plugin_register_init */
588 int
589 sdb_plugin_register_shutdown(const char *name, sdb_plugin_shutdown_cb callback,
590                 sdb_object_t *user_data)
592         return plugin_add_callback(&shutdown_list, "shutdown", name,
593                         callback, user_data);
594 } /* sdb_plugin_register_shutdown */
596 int
597 sdb_plugin_register_log(const char *name, sdb_plugin_log_cb callback,
598                 sdb_object_t *user_data)
600         return plugin_add_callback(&log_list, "log", name, callback,
601                         user_data);
602 } /* sdb_plugin_register_log */
604 int
605 sdb_plugin_register_cname(const char *name, sdb_plugin_cname_cb callback,
606                 sdb_object_t *user_data)
608         return plugin_add_callback(&cname_list, "cname", name, callback,
609                         user_data);
610 } /* sdb_plugin_register_cname */
612 int
613 sdb_plugin_register_collector(const char *name, sdb_plugin_collector_cb callback,
614                 const sdb_time_t *interval, sdb_object_t *user_data)
616         sdb_object_t *obj;
618         if ((! name) || (! callback))
619                 return -1;
621         if (! collector_list)
622                 collector_list = sdb_llist_create();
623         if (! collector_list)
624                 return -1;
626         obj = sdb_object_create(name, sdb_plugin_collector_cb_type,
627                         &collector_list, "collector", callback, user_data);
628         if (! obj)
629                 return -1;
631         if (interval)
632                 SDB_PLUGIN_CCB(obj)->ccb_interval = *interval;
633         else {
634                 sdb_time_t tmp = sdb_plugin_get_ctx().interval;
636                 if (tmp > 0)
637                         SDB_PLUGIN_CCB(obj)->ccb_interval = tmp;
638                 else
639                         SDB_PLUGIN_CCB(obj)->ccb_interval = 0;
640         }
642         if (! (SDB_PLUGIN_CCB(obj)->ccb_next_update = sdb_gettime())) {
643                 char errbuf[1024];
644                 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
645                                 "time: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
646                 sdb_object_deref(obj);
647                 return -1;
648         }
650         if (sdb_llist_insert_sorted(collector_list, obj,
651                                 plugin_cmp_next_update)) {
652                 sdb_object_deref(obj);
653                 return -1;
654         }
656         /* pass control to the list */
657         sdb_object_deref(obj);
659         sdb_log(SDB_LOG_INFO, "core: Registered collector callback '%s' "
660                         "(interval = %.3fs).", name,
661                         SDB_TIME_TO_DOUBLE(SDB_PLUGIN_CCB(obj)->ccb_interval));
662         return 0;
663 } /* sdb_plugin_register_collector */
665 sdb_plugin_ctx_t
666 sdb_plugin_get_ctx(void)
668         ctx_t *c;
670         c = ctx_get();
671         if (! c) {
672                 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid read access to plugin "
673                                 "context outside a plugin");
674                 return plugin_default_ctx;
675         }
676         return c->public;
677 } /* sdb_plugin_get_ctx */
679 int
680 sdb_plugin_set_ctx(sdb_plugin_ctx_t ctx, sdb_plugin_ctx_t *old)
682         ctx_t *c;
684         c = ctx_get();
685         if (! c) {
686                 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid write access to plugin "
687                                 "context outside a plugin");
688                 return -1;
689         }
691         if (old)
692                 *old = c->public;
693         c->public = ctx;
694         return 0;
695 } /* sdb_plugin_set_ctx */
697 int
698 sdb_plugin_configure(const char *name, oconfig_item_t *ci)
700         sdb_plugin_cb_t *plugin;
701         sdb_plugin_config_cb callback;
703         ctx_t *old_ctx;
705         int status;
707         if ((! name) || (! ci))
708                 return -1;
710         plugin = SDB_PLUGIN_CB(sdb_llist_search_by_name(config_list, name));
711         if (! plugin) {
712                 /* XXX: check if any such plugin has been loaded */
713                 ctx_t *ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
714                 if (! ctx)
715                         sdb_log(SDB_LOG_ERR, "core: Plugin '%s' not loaded.", name);
716                 else
717                         sdb_log(SDB_LOG_ERR, "core: Plugin '%s' did not register "
718                                         "a config callback.", name);
719                 errno = ENOENT;
720                 return -1;
721         }
723         old_ctx = ctx_set(plugin->cb_ctx);
724         callback = plugin->cb_callback;
725         status = callback(ci);
726         ctx_set(old_ctx);
727         return status;
728 } /* sdb_plugin_configure */
730 int
731 sdb_plugin_init_all(void)
733         sdb_llist_iter_t *iter;
734         int ret = 0;
736         iter = sdb_llist_get_iter(init_list);
737         while (sdb_llist_iter_has_next(iter)) {
738                 sdb_plugin_cb_t *cb;
739                 sdb_plugin_init_cb callback;
740                 ctx_t *old_ctx;
742                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
743                 assert(obj);
744                 cb = SDB_PLUGIN_CB(obj);
746                 callback = cb->cb_callback;
748                 old_ctx = ctx_set(cb->cb_ctx);
749                 if (callback(cb->cb_user_data)) {
750                         sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin "
751                                         "'%s'. Unregistering all callbacks.", obj->name);
752                         plugin_unregister_by_name(cb->cb_ctx->info.plugin_name);
753                         ++ret;
754                 }
755                 ctx_set(old_ctx);
756         }
757         sdb_llist_iter_destroy(iter);
758         return ret;
759 } /* sdb_plugin_init_all */
761 int
762 sdb_plugin_collector_loop(sdb_plugin_loop_t *loop)
764         if (! collector_list) {
765                 sdb_log(SDB_LOG_WARNING, "core: No collectors registered. "
766                                 "Quiting main loop.");
767                 return -1;
768         }
770         if (! loop)
771                 return -1;
773         while (loop->do_loop) {
774                 sdb_plugin_collector_cb callback;
775                 ctx_t *old_ctx;
777                 sdb_time_t interval, now;
779                 sdb_object_t *obj = sdb_llist_shift(collector_list);
780                 if (! obj)
781                         return -1;
783                 callback = SDB_PLUGIN_CCB(obj)->ccb_callback;
785                 if (! (now = sdb_gettime())) {
786                         char errbuf[1024];
787                         sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
788                                         "time in collector main loop: %s",
789                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
790                         now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
791                 }
793                 if (now < SDB_PLUGIN_CCB(obj)->ccb_next_update) {
794                         interval = SDB_PLUGIN_CCB(obj)->ccb_next_update - now;
796                         errno = 0;
797                         while (loop->do_loop && sdb_sleep(interval, &interval)) {
798                                 if (errno != EINTR) {
799                                         char errbuf[1024];
800                                         sdb_log(SDB_LOG_ERR, "core: Failed to sleep "
801                                                         "in collector main loop: %s",
802                                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
803                                         return -1;
804                                 }
805                                 errno = 0;
806                         }
808                         if (! loop->do_loop)
809                                 return 0;
810                 }
812                 old_ctx = ctx_set(SDB_PLUGIN_CCB(obj)->ccb_ctx);
813                 if (callback(SDB_PLUGIN_CCB(obj)->ccb_user_data)) {
814                         /* XXX */
815                 }
816                 ctx_set(old_ctx);
818                 interval = SDB_PLUGIN_CCB(obj)->ccb_interval;
819                 if (! interval)
820                         interval = loop->default_interval;
821                 if (! interval) {
822                         sdb_log(SDB_LOG_WARNING, "core: No interval configured "
823                                         "for plugin '%s'; skipping any further "
824                                         "iterations.", obj->name);
825                         sdb_object_deref(obj);
826                         continue;
827                 }
829                 SDB_PLUGIN_CCB(obj)->ccb_next_update += interval;
831                 if (! (now = sdb_gettime())) {
832                         char errbuf[1024];
833                         sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
834                                         "time in collector main loop: %s",
835                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
836                         now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
837                 }
839                 if (now > SDB_PLUGIN_CCB(obj)->ccb_next_update) {
840                         sdb_log(SDB_LOG_WARNING, "core: Plugin '%s' took too "
841                                         "long; skipping iterations to keep up.",
842                                         obj->name);
843                         SDB_PLUGIN_CCB(obj)->ccb_next_update = now;
844                 }
846                 if (sdb_llist_insert_sorted(collector_list, obj,
847                                         plugin_cmp_next_update)) {
848                         sdb_log(SDB_LOG_ERR, "core: Failed to re-insert "
849                                         "plugin '%s' into collector list. Unable to further "
850                                         "use the plugin.",
851                                         obj->name);
852                         sdb_object_deref(obj);
853                         return -1;
854                 }
856                 /* pass control back to the list */
857                 sdb_object_deref(obj);
858         }
859         return 0;
860 } /* sdb_plugin_read_loop */
862 char *
863 sdb_plugin_cname(char *hostname)
865         sdb_llist_iter_t *iter;
867         if (! hostname)
868                 return NULL;
870         if (! cname_list)
871                 return hostname;
873         iter = sdb_llist_get_iter(cname_list);
874         while (sdb_llist_iter_has_next(iter)) {
875                 sdb_plugin_cname_cb callback;
876                 char *cname;
878                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
879                 assert(obj);
881                 callback = SDB_PLUGIN_CB(obj)->cb_callback;
882                 cname = callback(hostname, SDB_PLUGIN_CB(obj)->cb_user_data);
883                 if (cname) {
884                         free(hostname);
885                         hostname = cname;
886                 }
887                 /* else: don't change hostname */
888         }
889         sdb_llist_iter_destroy(iter);
890         return hostname;
891 } /* sdb_plugin_cname */
893 int
894 sdb_plugin_log(int prio, const char *msg)
896         sdb_llist_iter_t *iter;
897         int ret = -1;
899         if (! msg)
900                 return 0;
902         if (! log_list)
903                 return fprintf(stderr, "[%s] %s\n", SDB_LOG_PRIO_TO_STRING(prio), msg);
905         iter = sdb_llist_get_iter(log_list);
906         while (sdb_llist_iter_has_next(iter)) {
907                 sdb_plugin_log_cb callback;
908                 int tmp;
910                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
911                 assert(obj);
913                 callback = SDB_PLUGIN_CB(obj)->cb_callback;
914                 tmp = callback(prio, msg, SDB_PLUGIN_CB(obj)->cb_user_data);
915                 if (tmp > ret)
916                         ret = tmp;
917         }
918         sdb_llist_iter_destroy(iter);
919         return ret;
920 } /* sdb_plugin_log */
922 int
923 sdb_plugin_vlogf(int prio, const char *fmt, va_list ap)
925         sdb_strbuf_t *buf;
926         int ret;
928         if (! fmt)
929                 return 0;
931         buf = sdb_strbuf_create(64);
932         if (! buf) {
933                 ret = fprintf(stderr, "[%s] ", SDB_LOG_PRIO_TO_STRING(prio));
934                 ret += vfprintf(stderr, fmt, ap);
935                 return ret;
936         }
938         if (sdb_strbuf_vsprintf(buf, fmt, ap) < 0) {
939                 sdb_strbuf_destroy(buf);
940                 return -1;
941         }
943         ret = sdb_plugin_log(prio, sdb_strbuf_string(buf));
944         sdb_strbuf_destroy(buf);
945         return ret;
946 } /* sdb_plugin_vlogf */
948 int
949 sdb_plugin_logf(int prio, const char *fmt, ...)
951         va_list ap;
952         int ret;
954         if (! fmt)
955                 return 0;
957         va_start(ap, fmt);
958         ret = sdb_plugin_vlogf(prio, fmt, ap);
959         va_end(ap);
960         return ret;
961 } /* sdb_plugin_logf */
963 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */