Code

306f5e902565d3a0b025ea8d08851672a4b53fbe
[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 "utils/error.h"
31 #include "utils/llist.h"
32 #include "utils/time.h"
34 #include <assert.h>
36 #include <errno.h>
38 #include <stdarg.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <strings.h>
42 #include <unistd.h>
44 #include <ltdl.h>
46 #include <pthread.h>
48 /*
49  * private data types
50  */
52 struct sdb_plugin_info {
53         char *name;
55         char *description;
56         char *copyright;
57         char *license;
59         int   version;
60         int   plugin_version;
61 };
62 #define SDB_PLUGIN_INFO_INIT { "no name set", "no description set", \
63         /* copyright */ "", /* license */ "", \
64         /* version */ -1, /* plugin_version */ -1 }
66 typedef struct {
67         sdb_object_t super;
68         char cb_name[64];
69         void *cb_callback;
70         sdb_object_t *cb_user_data;
71         sdb_plugin_ctx_t cb_ctx;
72 } sdb_plugin_cb_t;
73 #define SDB_PLUGIN_CB_INIT { SDB_OBJECT_INIT, /* name = */ "", \
74         /* callback = */ NULL, /* user_data = */ NULL, \
75         SDB_PLUGIN_CTX_INIT }
77 typedef struct {
78         sdb_plugin_cb_t super;
79 #define ccb_name super.cb_name
80 #define ccb_callback super.cb_callback
81 #define ccb_user_data super.cb_user_data
82 #define ccb_ctx super.cb_ctx
83         sdb_time_t ccb_interval;
84         sdb_time_t ccb_next_update;
85 } sdb_plugin_collector_cb_t;
87 #define SDB_PLUGIN_CB(obj) ((sdb_plugin_cb_t *)(obj))
88 #define SDB_PLUGIN_CCB(obj) ((sdb_plugin_collector_cb_t *)(obj))
90 /*
91  * private variables
92  */
94 static sdb_plugin_ctx_t  plugin_default_ctx = SDB_PLUGIN_CTX_INIT;
96 static pthread_key_t    plugin_ctx_key;
97 static _Bool            plugin_ctx_key_initialized = 0;
99 static sdb_llist_t      *config_list = NULL;
100 static sdb_llist_t      *init_list = NULL;
101 static sdb_llist_t      *collector_list = NULL;
102 static sdb_llist_t      *shutdown_list = NULL;
104 /*
105  * private helper functions
106  */
108 static void
109 sdb_plugin_ctx_destructor(void *ctx)
111         if (! ctx)
112                 return;
113         free(ctx);
114 } /* sdb_plugin_ctx_destructor */
116 static void
117 sdb_plugin_ctx_init(void)
119         if (plugin_ctx_key_initialized)
120                 return;
122         pthread_key_create(&plugin_ctx_key, sdb_plugin_ctx_destructor);
123         plugin_ctx_key_initialized = 1;
124 } /* sdb_plugin_ctx_init */
126 static sdb_plugin_ctx_t *
127 sdb_plugin_ctx_create(void)
129         sdb_plugin_ctx_t *ctx;
131         ctx = malloc(sizeof(*ctx));
132         if (! ctx)
133                 return NULL;
135         *ctx = plugin_default_ctx;
137         if (! plugin_ctx_key_initialized)
138                 sdb_plugin_ctx_init();
139         pthread_setspecific(plugin_ctx_key, ctx);
140         return ctx;
141 } /* sdb_plugin_ctx_create */
143 static int
144 sdb_plugin_cmp_name(const sdb_object_t *a, const sdb_object_t *b)
146         const sdb_plugin_cb_t *cb1 = (const sdb_plugin_cb_t *)a;
147         const sdb_plugin_cb_t *cb2 = (const sdb_plugin_cb_t *)b;
149         assert(cb1 && cb2);
150         return strcasecmp(cb1->cb_name, cb2->cb_name);
151 } /* sdb_plugin_cmp_name */
153 static int
154 sdb_plugin_cmp_next_update(const sdb_object_t *a, const sdb_object_t *b)
156         const sdb_plugin_collector_cb_t *ccb1
157                 = (const sdb_plugin_collector_cb_t *)a;
158         const sdb_plugin_collector_cb_t *ccb2
159                 = (const sdb_plugin_collector_cb_t *)b;
161         assert(ccb1 && ccb2);
163         return (ccb1->ccb_next_update > ccb2->ccb_next_update)
164                 ? 1 : (ccb1->ccb_next_update < ccb2->ccb_next_update)
165                 ? -1 : 0;
166 } /* sdb_plugin_cmp_next_update */
168 static sdb_plugin_cb_t *
169 sdb_plugin_find_by_name(sdb_llist_t *list, const char *name)
171         sdb_plugin_cb_t tmp = SDB_PLUGIN_CB_INIT;
173         sdb_object_t *obj;
174         assert(name);
176         if (! list)
177                 return NULL;
179         snprintf(tmp.cb_name, sizeof(tmp.cb_name), "%s", name);
180         tmp.cb_name[sizeof(tmp.cb_name) - 1] = '\0';
181         obj = sdb_llist_search(list, SDB_OBJ(&tmp), sdb_plugin_cmp_name);
182         if (! obj)
183                 return NULL;
184         return SDB_PLUGIN_CB(obj);
185 } /* sdb_plugin_find_by_name */
187 static int
188 sdb_plugin_cb_init(sdb_object_t *obj, va_list ap)
190         sdb_llist_t **list = va_arg(ap, sdb_llist_t **);
191         const char  *type = va_arg(ap, const char *);
192         const char  *name = va_arg(ap, const char *);
193         void    *callback = va_arg(ap, void *);
194         sdb_object_t   *ud = va_arg(ap, sdb_object_t *);
196         assert(list);
197         assert(type);
198         assert(obj);
200         if (sdb_plugin_find_by_name(*list, name)) {
201                 sdb_log(SDB_LOG_WARNING, "plugin: %s callback '%s' "
202                                 "has already been registered. Ignoring newly "
203                                 "registered version.\n", type, name);
204                 return -1;
205         }
207         snprintf(SDB_PLUGIN_CB(obj)->cb_name,
208                         sizeof(SDB_PLUGIN_CB(obj)->cb_name),
209                         "%s", name);
210         SDB_PLUGIN_CB(obj)->cb_name[sizeof(SDB_PLUGIN_CB(obj)->cb_name) - 1] = '\0';
211         SDB_PLUGIN_CB(obj)->cb_callback = callback;
212         SDB_PLUGIN_CB(obj)->cb_ctx      = sdb_plugin_get_ctx();
214         sdb_object_ref(ud);
215         SDB_PLUGIN_CB(obj)->cb_user_data = ud;
216         return 0;
217 } /* sdb_plugin_cb_init */
219 static void
220 sdb_plugin_cb_destroy(sdb_object_t *obj)
222         assert(obj);
223         sdb_object_deref(SDB_PLUGIN_CB(obj)->cb_user_data);
224 } /* sdb_plugin_cb_destroy */
226 static int
227 sdb_plugin_add_callback(sdb_llist_t **list, const char *type,
228                 const char *name, void *callback, sdb_object_t *user_data)
230         sdb_object_t *obj;
232         if ((! name) || (! callback))
233                 return -1;
235         assert(list);
237         if (! *list)
238                 *list = sdb_llist_create();
239         if (! *list)
240                 return -1;
242         obj = sdb_object_create(sizeof(sdb_plugin_cb_t), sdb_plugin_cb_init,
243                         sdb_plugin_cb_destroy, list, type, name, callback, user_data);
244         if (! obj)
245                 return -1;
247         if (sdb_llist_append(*list, obj)) {
248                 sdb_object_deref(obj);
249                 return -1;
250         }
252         /* pass control to the list */
253         sdb_object_deref(obj);
255         sdb_log(SDB_LOG_INFO, "plugin: Registered %s callback '%s'.\n",
256                         type, name);
257         return 0;
258 } /* sdb_plugin_add_callback */
260 /*
261  * public API
262  */
264 int
265 sdb_plugin_load(const char *name)
267         char filename[1024];
269         lt_dlhandle lh;
271         int (*mod_init)(sdb_plugin_info_t *);
272         sdb_plugin_info_t plugin_info = SDB_PLUGIN_INFO_INIT;
274         int status;
276         snprintf(filename, sizeof(filename), "%s/%s.so",
277                         PKGLIBDIR, name);
278         filename[sizeof(filename) - 1] = '\0';
280         if (access(filename, R_OK)) {
281                 char errbuf[1024];
282                 sdb_log(SDB_LOG_ERR, "plugin: Failed to load plugin '%s': %s\n",
283                                 name, sdb_strerror(errno, errbuf, sizeof(errbuf)));
284                 return -1;
285         }
287         lt_dlinit();
288         lt_dlerror();
290         lh = lt_dlopen(filename);
291         if (! lh) {
292                 sdb_log(SDB_LOG_ERR, "plugin: Failed to load plugin '%s': %s\n"
293                                 "The most common cause for this problem are missing "
294                                 "dependencies.\n", name, lt_dlerror());
295                 return -1;
296         }
298         mod_init = (int (*)(sdb_plugin_info_t *))lt_dlsym(lh, "sdb_module_init");
299         if (! mod_init) {
300                 sdb_log(SDB_LOG_ERR, "plugin: Failed to load plugin '%s': "
301                                 "could not find symbol 'sdb_module_init'\n", name);
302                 return -1;
303         }
305         status = mod_init(&plugin_info);
306         if (status) {
307                 sdb_log(SDB_LOG_ERR, "plugin: Failed to initialize "
308                                 "plugin '%s'\n", name);
309                 return -1;
310         }
312         /* compare minor version */
313         if ((plugin_info.version < 0)
314                         || ((int)(plugin_info.version / 100) != (int)(SDB_VERSION / 100)))
315                 sdb_log(SDB_LOG_WARNING, "plugin: WARNING: version of "
316                                 "plugin '%s' (%i.%i.%i) does not match our version "
317                                 "(%i.%i.%i); this might cause problems\n",
318                                 name, SDB_VERSION_DECODE(plugin_info.version),
319                                 SDB_VERSION_DECODE(SDB_VERSION));
321         sdb_log(SDB_LOG_INFO, "plugin: Successfully loaded "
322                         "plugin '%s' v%i (%s)\n\t%s\n",
323                         plugin_info.name, plugin_info.plugin_version,
324                         plugin_info.description, plugin_info.copyright);
325         return 0;
326 } /* sdb_plugin_load */
328 int
329 sdb_plugin_set_info(sdb_plugin_info_t *info, int type, ...)
331         va_list ap;
333         if (! info)
334                 return -1;
336         va_start(ap, type);
338         switch (type) {
339                 case SDB_PLUGIN_INFO_NAME:
340                         {
341                                 char *name = va_arg(ap, char *);
342                                 info->name = name;
343                         }
344                         break;
345                 case SDB_PLUGIN_INFO_DESC:
346                         {
347                                 char *desc = va_arg(ap, char *);
348                                 info->description = desc;
349                         }
350                         break;
351                 case SDB_PLUGIN_INFO_COPYRIGHT:
352                         {
353                                 char *copyright = va_arg(ap, char *);
354                                 info->copyright = copyright;
355                         }
356                         break;
357                 case SDB_PLUGIN_INFO_LICENSE:
358                         {
359                                 char *license = va_arg(ap, char *);
360                                 info->license = license;
361                         }
362                         break;
363                 case SDB_PLUGIN_INFO_VERSION:
364                         {
365                                 int version = va_arg(ap, int);
366                                 info->version = version;
367                         }
368                         break;
369                 case SDB_PLUGIN_INFO_PLUGIN_VERSION:
370                         {
371                                 int version = va_arg(ap, int);
372                                 info->plugin_version = version;
373                         }
374                         break;
375                 default:
376                         va_end(ap);
377                         return -1;
378         }
380         va_end(ap);
381         return 0;
382 } /* sdb_plugin_set_info */
384 int
385 sdb_plugin_register_config(const char *name, sdb_plugin_config_cb callback)
387         return sdb_plugin_add_callback(&config_list, "init", name,
388                         callback, NULL);
389 } /* sdb_plugin_register_config */
391 int
392 sdb_plugin_register_init(const char *name, sdb_plugin_init_cb callback,
393                 sdb_object_t *user_data)
395         return sdb_plugin_add_callback(&init_list, "init", name,
396                         callback, user_data);
397 } /* sdb_plugin_register_init */
399 int
400 sdb_plugin_register_shutdown(const char *name, sdb_plugin_shutdown_cb callback,
401                 sdb_object_t *user_data)
403         return sdb_plugin_add_callback(&shutdown_list, "shutdown", name,
404                         callback, user_data);
405 } /* sdb_plugin_register_shutdown */
407 int
408 sdb_plugin_register_collector(const char *name, sdb_plugin_collector_cb callback,
409                 const sdb_time_t *interval, sdb_object_t *user_data)
411         sdb_object_t *obj;
413         if ((! name) || (! callback))
414                 return -1;
416         if (! collector_list)
417                 collector_list = sdb_llist_create();
418         if (! collector_list)
419                 return -1;
421         obj = sdb_object_create(sizeof(sdb_plugin_collector_cb_t),
422                         sdb_plugin_cb_init, sdb_plugin_cb_destroy,
423                         &collector_list, "collector", name, callback, user_data);
424         if (! obj)
425                 return -1;
427         if (interval)
428                 SDB_PLUGIN_CCB(obj)->ccb_interval = *interval;
429         else {
430                 sdb_time_t tmp = sdb_plugin_get_ctx().interval;
432                 if (tmp > 0)
433                         SDB_PLUGIN_CCB(obj)->ccb_interval = tmp;
434                 else
435                         SDB_PLUGIN_CCB(obj)->ccb_interval = 0;
436         }
438         if (! (SDB_PLUGIN_CCB(obj)->ccb_next_update = sdb_gettime())) {
439                 char errbuf[1024];
440                 sdb_log(SDB_LOG_ERR, "plugin: Failed to determine current "
441                                 "time: %s\n", sdb_strerror(errno, errbuf, sizeof(errbuf)));
442                 sdb_object_deref(obj);
443                 return -1;
444         }
446         if (sdb_llist_insert_sorted(collector_list, obj,
447                                 sdb_plugin_cmp_next_update)) {
448                 sdb_object_deref(obj);
449                 return -1;
450         }
452         /* pass control to the list */
453         sdb_object_deref(obj);
455         sdb_log(SDB_LOG_INFO, "plugin: Registered collector callback '%s' "
456                         "(interval = %.3fs).\n", name,
457                         SDB_TIME_TO_DOUBLE(SDB_PLUGIN_CCB(obj)->ccb_interval));
458         return 0;
459 } /* sdb_plugin_register_collector */
461 sdb_plugin_ctx_t
462 sdb_plugin_get_ctx(void)
464         sdb_plugin_ctx_t *ctx;
466         if (! plugin_ctx_key_initialized)
467                 sdb_plugin_ctx_init();
468         ctx = pthread_getspecific(plugin_ctx_key);
470         if (! ctx)
471                 ctx = sdb_plugin_ctx_create();
472         if (! ctx)
473                 return plugin_default_ctx;
474         return *ctx;
475 } /* sdb_plugin_get_ctx */
477 sdb_plugin_ctx_t
478 sdb_plugin_set_ctx(sdb_plugin_ctx_t ctx)
480         sdb_plugin_ctx_t *tmp;
481         sdb_plugin_ctx_t old;
483         if (! plugin_ctx_key_initialized)
484                 sdb_plugin_ctx_init();
485         tmp = pthread_getspecific(plugin_ctx_key);
487         if (! tmp)
488                 tmp = sdb_plugin_ctx_create();
489         if (! tmp)
490                 return plugin_default_ctx;
492         old = *tmp;
493         *tmp = ctx;
494         return old;
495 } /* sdb_plugin_set_ctx */
497 int
498 sdb_plugin_configure(const char *name, oconfig_item_t *ci)
500         sdb_plugin_cb_t *plugin;
501         sdb_plugin_config_cb callback;
503         sdb_plugin_ctx_t old_ctx;
505         int status;
507         if ((! name) || (! ci))
508                 return -1;
510         plugin = sdb_plugin_find_by_name(config_list, name);
511         if (! plugin) {
512                 /* XXX: check if any such plugin has been loaded */
513                 sdb_log(SDB_LOG_ERR, "plugin: Plugin '%s' did not register "
514                                 "a config callback.\n", name);
515                 errno = ENOENT;
516                 return -1;
517         }
519         old_ctx = sdb_plugin_set_ctx(plugin->cb_ctx);
520         callback = plugin->cb_callback;
521         status = callback(ci);
522         sdb_plugin_set_ctx(old_ctx);
523         return status;
524 } /* sdb_plugin_configure */
526 int
527 sdb_plugin_init_all(void)
529         sdb_llist_iter_t *iter;
531         iter = sdb_llist_get_iter(init_list);
532         while (sdb_llist_iter_has_next(iter)) {
533                 sdb_plugin_init_cb callback;
534                 sdb_plugin_ctx_t old_ctx;
536                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
537                 assert(obj);
539                 callback = SDB_PLUGIN_CB(obj)->cb_callback;
541                 old_ctx = sdb_plugin_set_ctx(SDB_PLUGIN_CB(obj)->cb_ctx);
542                 if (callback(SDB_PLUGIN_CB(obj)->cb_user_data)) {
543                         /* XXX: unload plugin */
544                 }
545                 sdb_plugin_set_ctx(old_ctx);
546         }
547         return 0;
548 } /* sdb_plugin_init_all */
550 int
551 sdb_plugin_collector_loop(sdb_plugin_loop_t *loop)
553         if ((! collector_list) || (! loop))
554                 return -1;
556         while (loop->do_loop) {
557                 sdb_plugin_collector_cb callback;
558                 sdb_plugin_ctx_t old_ctx;
560                 sdb_time_t interval, now;
562                 sdb_object_t *obj = sdb_llist_shift(collector_list);
563                 if (! obj)
564                         return -1;
566                 callback = SDB_PLUGIN_CCB(obj)->ccb_callback;
568                 if (! (now = sdb_gettime())) {
569                         char errbuf[1024];
570                         sdb_log(SDB_LOG_ERR, "plugin: Failed to determine current "
571                                         "time: %s\n", sdb_strerror(errno, errbuf, sizeof(errbuf)));
572                         now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
573                 }
575                 if (now < SDB_PLUGIN_CCB(obj)->ccb_next_update) {
576                         interval = SDB_PLUGIN_CCB(obj)->ccb_next_update - now;
578                         errno = 0;
579                         while (loop->do_loop && sdb_sleep(interval, &interval)) {
580                                 if (errno != EINTR) {
581                                         char errbuf[1024];
582                                         sdb_log(SDB_LOG_ERR, "plugin: Failed to sleep: %s\n",
583                                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
584                                         return -1;
585                                 }
586                                 errno = 0;
587                         }
589                         if (! loop->do_loop)
590                                 return 0;
591                 }
593                 old_ctx = sdb_plugin_set_ctx(SDB_PLUGIN_CCB(obj)->ccb_ctx);
594                 if (callback(SDB_PLUGIN_CCB(obj)->ccb_user_data)) {
595                         /* XXX */
596                 }
597                 sdb_plugin_set_ctx(old_ctx);
599                 interval = SDB_PLUGIN_CCB(obj)->ccb_interval;
600                 if (! interval)
601                         interval = loop->default_interval;
602                 if (! interval) {
603                         sdb_log(SDB_LOG_WARNING, "plugin: No interval configured "
604                                         "for plugin '%s'; skipping any further "
605                                         "iterations.\n", SDB_PLUGIN_CCB(obj)->ccb_name);
606                         sdb_object_deref(obj);
607                         continue;
608                 }
610                 SDB_PLUGIN_CCB(obj)->ccb_next_update += interval;
612                 if (! (now = sdb_gettime())) {
613                         char errbuf[1024];
614                         sdb_log(SDB_LOG_ERR, "plugin: Failed to determine current "
615                                         "time: %s\n", sdb_strerror(errno, errbuf, sizeof(errbuf)));
616                         now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
617                 }
619                 if (now > SDB_PLUGIN_CCB(obj)->ccb_next_update) {
620                         sdb_log(SDB_LOG_WARNING, "plugin: Plugin '%s' took too "
621                                         "long; skipping iterations to keep up.\n",
622                                         SDB_PLUGIN_CCB(obj)->ccb_name);
623                         SDB_PLUGIN_CCB(obj)->ccb_next_update = now;
624                 }
626                 if (sdb_llist_insert_sorted(collector_list, obj,
627                                         sdb_plugin_cmp_next_update)) {
628                         sdb_log(SDB_LOG_ERR, "plugin: Failed to re-insert "
629                                         "plugin '%s' into collector list. Unable to further "
630                                         "use the plugin.\n",
631                                         SDB_PLUGIN_CCB(obj)->ccb_name);
632                         sdb_object_deref(obj);
633                         return -1;
634                 }
636                 /* pass control back to the list */
637                 sdb_object_deref(obj);
638         }
639         return 0;
640 } /* sdb_plugin_read_loop */
642 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */