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)
110 {
111 if (! ctx)
112 return;
113 free(ctx);
114 } /* sdb_plugin_ctx_destructor */
116 static void
117 sdb_plugin_ctx_init(void)
118 {
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)
128 {
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)
145 {
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)
155 {
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)
170 {
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)
189 {
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.", 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)
221 {
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)
229 {
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'.",
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)
266 {
267 char real_name[strlen(name) > 0 ? strlen(name) : 1];
268 const char *name_ptr;
269 char *tmp;
271 char filename[1024];
273 lt_dlhandle lh;
275 int (*mod_init)(sdb_plugin_info_t *);
276 sdb_plugin_info_t plugin_info = SDB_PLUGIN_INFO_INIT;
278 int status;
280 if ((! name) || (! *name))
281 return -1;
283 real_name[0] = '\0';
284 name_ptr = name;
286 while ((tmp = strstr(name_ptr, "::"))) {
287 strncat(real_name, name_ptr, (size_t)(tmp - name_ptr));
288 strcat(real_name, "/");
289 name_ptr = tmp + strlen("::");
290 }
291 strcat(real_name, name_ptr);
293 snprintf(filename, sizeof(filename), "%s/%s.so",
294 PKGLIBDIR, real_name);
295 filename[sizeof(filename) - 1] = '\0';
297 if (access(filename, R_OK)) {
298 char errbuf[1024];
299 sdb_log(SDB_LOG_ERR, "plugin: Failed to load plugin '%s' (%s): %s",
300 name, filename, sdb_strerror(errno, errbuf, sizeof(errbuf)));
301 return -1;
302 }
304 lt_dlinit();
305 lt_dlerror();
307 lh = lt_dlopen(filename);
308 if (! lh) {
309 sdb_log(SDB_LOG_ERR, "plugin: Failed to load plugin '%s': %s"
310 "The most common cause for this problem are missing "
311 "dependencies.\n", name, lt_dlerror());
312 return -1;
313 }
315 mod_init = (int (*)(sdb_plugin_info_t *))lt_dlsym(lh, "sdb_module_init");
316 if (! mod_init) {
317 sdb_log(SDB_LOG_ERR, "plugin: Failed to load plugin '%s': "
318 "could not find symbol 'sdb_module_init'", name);
319 return -1;
320 }
322 status = mod_init(&plugin_info);
323 if (status) {
324 sdb_log(SDB_LOG_ERR, "plugin: Failed to initialize "
325 "plugin '%s'", name);
326 return -1;
327 }
329 /* compare minor version */
330 if ((plugin_info.version < 0)
331 || ((int)(plugin_info.version / 100) != (int)(SDB_VERSION / 100)))
332 sdb_log(SDB_LOG_WARNING, "plugin: WARNING: version of "
333 "plugin '%s' (%i.%i.%i) does not match our version "
334 "(%i.%i.%i); this might cause problems",
335 name, SDB_VERSION_DECODE(plugin_info.version),
336 SDB_VERSION_DECODE(SDB_VERSION));
338 sdb_log(SDB_LOG_INFO, "plugin: Successfully loaded "
339 "plugin '%s' v%i (%s)\n\t%s",
340 plugin_info.name, plugin_info.plugin_version,
341 plugin_info.description, plugin_info.copyright);
342 return 0;
343 } /* sdb_plugin_load */
345 int
346 sdb_plugin_set_info(sdb_plugin_info_t *info, int type, ...)
347 {
348 va_list ap;
350 if (! info)
351 return -1;
353 va_start(ap, type);
355 switch (type) {
356 case SDB_PLUGIN_INFO_NAME:
357 {
358 char *name = va_arg(ap, char *);
359 info->name = name;
360 }
361 break;
362 case SDB_PLUGIN_INFO_DESC:
363 {
364 char *desc = va_arg(ap, char *);
365 info->description = desc;
366 }
367 break;
368 case SDB_PLUGIN_INFO_COPYRIGHT:
369 {
370 char *copyright = va_arg(ap, char *);
371 info->copyright = copyright;
372 }
373 break;
374 case SDB_PLUGIN_INFO_LICENSE:
375 {
376 char *license = va_arg(ap, char *);
377 info->license = license;
378 }
379 break;
380 case SDB_PLUGIN_INFO_VERSION:
381 {
382 int version = va_arg(ap, int);
383 info->version = version;
384 }
385 break;
386 case SDB_PLUGIN_INFO_PLUGIN_VERSION:
387 {
388 int version = va_arg(ap, int);
389 info->plugin_version = version;
390 }
391 break;
392 default:
393 va_end(ap);
394 return -1;
395 }
397 va_end(ap);
398 return 0;
399 } /* sdb_plugin_set_info */
401 int
402 sdb_plugin_register_config(const char *name, sdb_plugin_config_cb callback)
403 {
404 return sdb_plugin_add_callback(&config_list, "init", name,
405 callback, NULL);
406 } /* sdb_plugin_register_config */
408 int
409 sdb_plugin_register_init(const char *name, sdb_plugin_init_cb callback,
410 sdb_object_t *user_data)
411 {
412 return sdb_plugin_add_callback(&init_list, "init", name,
413 callback, user_data);
414 } /* sdb_plugin_register_init */
416 int
417 sdb_plugin_register_shutdown(const char *name, sdb_plugin_shutdown_cb callback,
418 sdb_object_t *user_data)
419 {
420 return sdb_plugin_add_callback(&shutdown_list, "shutdown", name,
421 callback, user_data);
422 } /* sdb_plugin_register_shutdown */
424 int
425 sdb_plugin_register_collector(const char *name, sdb_plugin_collector_cb callback,
426 const sdb_time_t *interval, sdb_object_t *user_data)
427 {
428 sdb_object_t *obj;
430 if ((! name) || (! callback))
431 return -1;
433 if (! collector_list)
434 collector_list = sdb_llist_create();
435 if (! collector_list)
436 return -1;
438 obj = sdb_object_create(sizeof(sdb_plugin_collector_cb_t),
439 sdb_plugin_cb_init, sdb_plugin_cb_destroy,
440 &collector_list, "collector", name, callback, user_data);
441 if (! obj)
442 return -1;
444 if (interval)
445 SDB_PLUGIN_CCB(obj)->ccb_interval = *interval;
446 else {
447 sdb_time_t tmp = sdb_plugin_get_ctx().interval;
449 if (tmp > 0)
450 SDB_PLUGIN_CCB(obj)->ccb_interval = tmp;
451 else
452 SDB_PLUGIN_CCB(obj)->ccb_interval = 0;
453 }
455 if (! (SDB_PLUGIN_CCB(obj)->ccb_next_update = sdb_gettime())) {
456 char errbuf[1024];
457 sdb_log(SDB_LOG_ERR, "plugin: Failed to determine current "
458 "time: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
459 sdb_object_deref(obj);
460 return -1;
461 }
463 if (sdb_llist_insert_sorted(collector_list, obj,
464 sdb_plugin_cmp_next_update)) {
465 sdb_object_deref(obj);
466 return -1;
467 }
469 /* pass control to the list */
470 sdb_object_deref(obj);
472 sdb_log(SDB_LOG_INFO, "plugin: Registered collector callback '%s' "
473 "(interval = %.3fs).", name,
474 SDB_TIME_TO_DOUBLE(SDB_PLUGIN_CCB(obj)->ccb_interval));
475 return 0;
476 } /* sdb_plugin_register_collector */
478 sdb_plugin_ctx_t
479 sdb_plugin_get_ctx(void)
480 {
481 sdb_plugin_ctx_t *ctx;
483 if (! plugin_ctx_key_initialized)
484 sdb_plugin_ctx_init();
485 ctx = pthread_getspecific(plugin_ctx_key);
487 if (! ctx)
488 ctx = sdb_plugin_ctx_create();
489 if (! ctx)
490 return plugin_default_ctx;
491 return *ctx;
492 } /* sdb_plugin_get_ctx */
494 sdb_plugin_ctx_t
495 sdb_plugin_set_ctx(sdb_plugin_ctx_t ctx)
496 {
497 sdb_plugin_ctx_t *tmp;
498 sdb_plugin_ctx_t old;
500 if (! plugin_ctx_key_initialized)
501 sdb_plugin_ctx_init();
502 tmp = pthread_getspecific(plugin_ctx_key);
504 if (! tmp)
505 tmp = sdb_plugin_ctx_create();
506 if (! tmp)
507 return plugin_default_ctx;
509 old = *tmp;
510 *tmp = ctx;
511 return old;
512 } /* sdb_plugin_set_ctx */
514 int
515 sdb_plugin_configure(const char *name, oconfig_item_t *ci)
516 {
517 sdb_plugin_cb_t *plugin;
518 sdb_plugin_config_cb callback;
520 sdb_plugin_ctx_t old_ctx;
522 int status;
524 if ((! name) || (! ci))
525 return -1;
527 plugin = sdb_plugin_find_by_name(config_list, name);
528 if (! plugin) {
529 /* XXX: check if any such plugin has been loaded */
530 sdb_log(SDB_LOG_ERR, "plugin: Plugin '%s' did not register "
531 "a config callback.", name);
532 errno = ENOENT;
533 return -1;
534 }
536 old_ctx = sdb_plugin_set_ctx(plugin->cb_ctx);
537 callback = plugin->cb_callback;
538 status = callback(ci);
539 sdb_plugin_set_ctx(old_ctx);
540 return status;
541 } /* sdb_plugin_configure */
543 int
544 sdb_plugin_init_all(void)
545 {
546 sdb_llist_iter_t *iter;
548 iter = sdb_llist_get_iter(init_list);
549 while (sdb_llist_iter_has_next(iter)) {
550 sdb_plugin_init_cb callback;
551 sdb_plugin_ctx_t old_ctx;
553 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
554 assert(obj);
556 callback = SDB_PLUGIN_CB(obj)->cb_callback;
558 old_ctx = sdb_plugin_set_ctx(SDB_PLUGIN_CB(obj)->cb_ctx);
559 if (callback(SDB_PLUGIN_CB(obj)->cb_user_data)) {
560 /* XXX: unload plugin */
561 }
562 sdb_plugin_set_ctx(old_ctx);
563 }
564 return 0;
565 } /* sdb_plugin_init_all */
567 int
568 sdb_plugin_collector_loop(sdb_plugin_loop_t *loop)
569 {
570 if (! collector_list) {
571 sdb_log(SDB_LOG_WARNING, "plugin: No collectors registered. "
572 "Quiting main loop.");
573 return -1;
574 }
576 if (! loop)
577 return -1;
579 while (loop->do_loop) {
580 sdb_plugin_collector_cb callback;
581 sdb_plugin_ctx_t old_ctx;
583 sdb_time_t interval, now;
585 sdb_object_t *obj = sdb_llist_shift(collector_list);
586 if (! obj)
587 return -1;
589 callback = SDB_PLUGIN_CCB(obj)->ccb_callback;
591 if (! (now = sdb_gettime())) {
592 char errbuf[1024];
593 sdb_log(SDB_LOG_ERR, "plugin: Failed to determine current "
594 "time: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
595 now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
596 }
598 if (now < SDB_PLUGIN_CCB(obj)->ccb_next_update) {
599 interval = SDB_PLUGIN_CCB(obj)->ccb_next_update - now;
601 errno = 0;
602 while (loop->do_loop && sdb_sleep(interval, &interval)) {
603 if (errno != EINTR) {
604 char errbuf[1024];
605 sdb_log(SDB_LOG_ERR, "plugin: Failed to sleep: %s",
606 sdb_strerror(errno, errbuf, sizeof(errbuf)));
607 return -1;
608 }
609 errno = 0;
610 }
612 if (! loop->do_loop)
613 return 0;
614 }
616 old_ctx = sdb_plugin_set_ctx(SDB_PLUGIN_CCB(obj)->ccb_ctx);
617 if (callback(SDB_PLUGIN_CCB(obj)->ccb_user_data)) {
618 /* XXX */
619 }
620 sdb_plugin_set_ctx(old_ctx);
622 interval = SDB_PLUGIN_CCB(obj)->ccb_interval;
623 if (! interval)
624 interval = loop->default_interval;
625 if (! interval) {
626 sdb_log(SDB_LOG_WARNING, "plugin: No interval configured "
627 "for plugin '%s'; skipping any further "
628 "iterations.", SDB_PLUGIN_CCB(obj)->ccb_name);
629 sdb_object_deref(obj);
630 continue;
631 }
633 SDB_PLUGIN_CCB(obj)->ccb_next_update += interval;
635 if (! (now = sdb_gettime())) {
636 char errbuf[1024];
637 sdb_log(SDB_LOG_ERR, "plugin: Failed to determine current "
638 "time: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
639 now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
640 }
642 if (now > SDB_PLUGIN_CCB(obj)->ccb_next_update) {
643 sdb_log(SDB_LOG_WARNING, "plugin: Plugin '%s' took too "
644 "long; skipping iterations to keep up.",
645 SDB_PLUGIN_CCB(obj)->ccb_name);
646 SDB_PLUGIN_CCB(obj)->ccb_next_update = now;
647 }
649 if (sdb_llist_insert_sorted(collector_list, obj,
650 sdb_plugin_cmp_next_update)) {
651 sdb_log(SDB_LOG_ERR, "plugin: Failed to re-insert "
652 "plugin '%s' into collector list. Unable to further "
653 "use the plugin.",
654 SDB_PLUGIN_CCB(obj)->ccb_name);
655 sdb_object_deref(obj);
656 return -1;
657 }
659 /* pass control back to the list */
660 sdb_object_deref(obj);
661 }
662 return 0;
663 } /* sdb_plugin_read_loop */
665 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */