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/error.h"
31 #include "core/time.h"
32 #include "utils/llist.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 void *cb_callback;
69 sdb_object_t *cb_user_data;
70 sdb_plugin_ctx_t cb_ctx;
71 } sdb_plugin_cb_t;
72 #define SDB_PLUGIN_CB_INIT { SDB_OBJECT_INIT, \
73 /* callback = */ NULL, /* user_data = */ NULL, \
74 SDB_PLUGIN_CTX_INIT }
76 typedef struct {
77 sdb_plugin_cb_t super;
78 #define ccb_callback super.cb_callback
79 #define ccb_user_data super.cb_user_data
80 #define ccb_ctx super.cb_ctx
81 sdb_time_t ccb_interval;
82 sdb_time_t ccb_next_update;
83 } sdb_plugin_collector_cb_t;
85 #define SDB_PLUGIN_CB(obj) ((sdb_plugin_cb_t *)(obj))
86 #define SDB_PLUGIN_CCB(obj) ((sdb_plugin_collector_cb_t *)(obj))
88 /*
89 * private variables
90 */
92 static sdb_plugin_ctx_t plugin_default_ctx = SDB_PLUGIN_CTX_INIT;
94 static pthread_key_t plugin_ctx_key;
95 static _Bool plugin_ctx_key_initialized = 0;
97 static sdb_llist_t *config_list = NULL;
98 static sdb_llist_t *init_list = NULL;
99 static sdb_llist_t *collector_list = NULL;
100 static sdb_llist_t *shutdown_list = NULL;
101 static sdb_llist_t *log_list = NULL;
103 /*
104 * private helper functions
105 */
107 static void
108 sdb_plugin_ctx_destructor(void *ctx)
109 {
110 if (! ctx)
111 return;
112 free(ctx);
113 } /* sdb_plugin_ctx_destructor */
115 static void
116 sdb_plugin_ctx_init(void)
117 {
118 if (plugin_ctx_key_initialized)
119 return;
121 pthread_key_create(&plugin_ctx_key, sdb_plugin_ctx_destructor);
122 plugin_ctx_key_initialized = 1;
123 } /* sdb_plugin_ctx_init */
125 static sdb_plugin_ctx_t *
126 sdb_plugin_ctx_create(void)
127 {
128 sdb_plugin_ctx_t *ctx;
130 ctx = malloc(sizeof(*ctx));
131 if (! ctx)
132 return NULL;
134 *ctx = plugin_default_ctx;
136 if (! plugin_ctx_key_initialized)
137 sdb_plugin_ctx_init();
138 pthread_setspecific(plugin_ctx_key, ctx);
139 return ctx;
140 } /* sdb_plugin_ctx_create */
142 static int
143 sdb_plugin_cmp_next_update(const sdb_object_t *a, const sdb_object_t *b)
144 {
145 const sdb_plugin_collector_cb_t *ccb1
146 = (const sdb_plugin_collector_cb_t *)a;
147 const sdb_plugin_collector_cb_t *ccb2
148 = (const sdb_plugin_collector_cb_t *)b;
150 assert(ccb1 && ccb2);
152 return (ccb1->ccb_next_update > ccb2->ccb_next_update)
153 ? 1 : (ccb1->ccb_next_update < ccb2->ccb_next_update)
154 ? -1 : 0;
155 } /* sdb_plugin_cmp_next_update */
157 /*
158 * private types
159 */
161 static int
162 sdb_plugin_cb_init(sdb_object_t *obj, va_list ap)
163 {
164 sdb_llist_t **list = va_arg(ap, sdb_llist_t **);
165 const char *type = va_arg(ap, const char *);
166 void *callback = va_arg(ap, void *);
167 sdb_object_t *ud = va_arg(ap, sdb_object_t *);
169 assert(list);
170 assert(type);
171 assert(obj);
173 if (sdb_llist_search_by_name(*list, obj->name)) {
174 sdb_log(SDB_LOG_WARNING, "plugin: %s callback '%s' "
175 "has already been registered. Ignoring newly "
176 "registered version.", type, obj->name);
177 return -1;
178 }
180 SDB_PLUGIN_CB(obj)->cb_callback = callback;
181 SDB_PLUGIN_CB(obj)->cb_ctx = sdb_plugin_get_ctx();
183 sdb_object_ref(ud);
184 SDB_PLUGIN_CB(obj)->cb_user_data = ud;
185 return 0;
186 } /* sdb_plugin_cb_init */
188 static void
189 sdb_plugin_cb_destroy(sdb_object_t *obj)
190 {
191 assert(obj);
192 sdb_object_deref(SDB_PLUGIN_CB(obj)->cb_user_data);
193 } /* sdb_plugin_cb_destroy */
195 static sdb_type_t sdb_plugin_cb_type = {
196 sizeof(sdb_plugin_cb_t),
198 sdb_plugin_cb_init,
199 sdb_plugin_cb_destroy,
200 /* clone = */ NULL
201 };
203 static sdb_type_t sdb_plugin_collector_cb_type = {
204 sizeof(sdb_plugin_collector_cb_t),
206 sdb_plugin_cb_init,
207 sdb_plugin_cb_destroy,
208 /* clone = */ NULL
209 };
211 static int
212 sdb_plugin_add_callback(sdb_llist_t **list, const char *type,
213 const char *name, void *callback, sdb_object_t *user_data)
214 {
215 sdb_object_t *obj;
217 if ((! name) || (! callback))
218 return -1;
220 assert(list);
222 if (! *list)
223 *list = sdb_llist_create();
224 if (! *list)
225 return -1;
227 obj = sdb_object_create(name, sdb_plugin_cb_type,
228 list, type, callback, user_data);
229 if (! obj)
230 return -1;
232 if (sdb_llist_append(*list, obj)) {
233 sdb_object_deref(obj);
234 return -1;
235 }
237 /* pass control to the list */
238 sdb_object_deref(obj);
240 sdb_log(SDB_LOG_INFO, "plugin: Registered %s callback '%s'.",
241 type, name);
242 return 0;
243 } /* sdb_plugin_add_callback */
245 /*
246 * public API
247 */
249 int
250 sdb_plugin_load(const char *name)
251 {
252 char real_name[strlen(name) > 0 ? strlen(name) : 1];
253 const char *name_ptr;
254 char *tmp;
256 char filename[1024];
258 lt_dlhandle lh;
260 int (*mod_init)(sdb_plugin_info_t *);
261 sdb_plugin_info_t plugin_info = SDB_PLUGIN_INFO_INIT;
263 int status;
265 if ((! name) || (! *name))
266 return -1;
268 real_name[0] = '\0';
269 name_ptr = name;
271 while ((tmp = strstr(name_ptr, "::"))) {
272 strncat(real_name, name_ptr, (size_t)(tmp - name_ptr));
273 strcat(real_name, "/");
274 name_ptr = tmp + strlen("::");
275 }
276 strcat(real_name, name_ptr);
278 snprintf(filename, sizeof(filename), "%s/%s.so",
279 PKGLIBDIR, real_name);
280 filename[sizeof(filename) - 1] = '\0';
282 if (access(filename, R_OK)) {
283 char errbuf[1024];
284 sdb_log(SDB_LOG_ERR, "plugin: Failed to load plugin '%s' (%s): %s",
285 name, filename, sdb_strerror(errno, errbuf, sizeof(errbuf)));
286 return -1;
287 }
289 lt_dlinit();
290 lt_dlerror();
292 lh = lt_dlopen(filename);
293 if (! lh) {
294 sdb_log(SDB_LOG_ERR, "plugin: Failed to load plugin '%s': %s"
295 "The most common cause for this problem are missing "
296 "dependencies.\n", name, lt_dlerror());
297 return -1;
298 }
300 mod_init = (int (*)(sdb_plugin_info_t *))lt_dlsym(lh, "sdb_module_init");
301 if (! mod_init) {
302 sdb_log(SDB_LOG_ERR, "plugin: Failed to load plugin '%s': "
303 "could not find symbol 'sdb_module_init'", name);
304 return -1;
305 }
307 status = mod_init(&plugin_info);
308 if (status) {
309 sdb_log(SDB_LOG_ERR, "plugin: Failed to initialize "
310 "plugin '%s'", name);
311 return -1;
312 }
314 /* compare minor version */
315 if ((plugin_info.version < 0)
316 || ((int)(plugin_info.version / 100) != (int)(SDB_VERSION / 100)))
317 sdb_log(SDB_LOG_WARNING, "plugin: WARNING: version of "
318 "plugin '%s' (%i.%i.%i) does not match our version "
319 "(%i.%i.%i); this might cause problems",
320 name, SDB_VERSION_DECODE(plugin_info.version),
321 SDB_VERSION_DECODE(SDB_VERSION));
323 sdb_log(SDB_LOG_INFO, "plugin: Successfully loaded "
324 "plugin '%s' v%i (%s)\n\t%s",
325 plugin_info.name, plugin_info.plugin_version,
326 plugin_info.description, plugin_info.copyright);
327 return 0;
328 } /* sdb_plugin_load */
330 int
331 sdb_plugin_set_info(sdb_plugin_info_t *info, int type, ...)
332 {
333 va_list ap;
335 if (! info)
336 return -1;
338 va_start(ap, type);
340 switch (type) {
341 case SDB_PLUGIN_INFO_NAME:
342 {
343 char *name = va_arg(ap, char *);
344 info->name = name;
345 }
346 break;
347 case SDB_PLUGIN_INFO_DESC:
348 {
349 char *desc = va_arg(ap, char *);
350 info->description = desc;
351 }
352 break;
353 case SDB_PLUGIN_INFO_COPYRIGHT:
354 {
355 char *copyright = va_arg(ap, char *);
356 info->copyright = copyright;
357 }
358 break;
359 case SDB_PLUGIN_INFO_LICENSE:
360 {
361 char *license = va_arg(ap, char *);
362 info->license = license;
363 }
364 break;
365 case SDB_PLUGIN_INFO_VERSION:
366 {
367 int version = va_arg(ap, int);
368 info->version = version;
369 }
370 break;
371 case SDB_PLUGIN_INFO_PLUGIN_VERSION:
372 {
373 int version = va_arg(ap, int);
374 info->plugin_version = version;
375 }
376 break;
377 default:
378 va_end(ap);
379 return -1;
380 }
382 va_end(ap);
383 return 0;
384 } /* sdb_plugin_set_info */
386 int
387 sdb_plugin_register_config(const char *name, sdb_plugin_config_cb callback)
388 {
389 return sdb_plugin_add_callback(&config_list, "init", name,
390 callback, NULL);
391 } /* sdb_plugin_register_config */
393 int
394 sdb_plugin_register_init(const char *name, sdb_plugin_init_cb callback,
395 sdb_object_t *user_data)
396 {
397 return sdb_plugin_add_callback(&init_list, "init", name,
398 callback, user_data);
399 } /* sdb_plugin_register_init */
401 int
402 sdb_plugin_register_shutdown(const char *name, sdb_plugin_shutdown_cb callback,
403 sdb_object_t *user_data)
404 {
405 return sdb_plugin_add_callback(&shutdown_list, "shutdown", name,
406 callback, user_data);
407 } /* sdb_plugin_register_shutdown */
409 int
410 sdb_plugin_register_log(const char *name, sdb_plugin_log_cb callback,
411 sdb_object_t *user_data)
412 {
413 return sdb_plugin_add_callback(&log_list, "log", name, callback,
414 user_data);
415 } /* sdb_plugin_register_log */
417 int
418 sdb_plugin_register_collector(const char *name, sdb_plugin_collector_cb callback,
419 const sdb_time_t *interval, sdb_object_t *user_data)
420 {
421 sdb_object_t *obj;
423 if ((! name) || (! callback))
424 return -1;
426 if (! collector_list)
427 collector_list = sdb_llist_create();
428 if (! collector_list)
429 return -1;
431 obj = sdb_object_create(name, sdb_plugin_collector_cb_type,
432 &collector_list, "collector", callback, user_data);
433 if (! obj)
434 return -1;
436 if (interval)
437 SDB_PLUGIN_CCB(obj)->ccb_interval = *interval;
438 else {
439 sdb_time_t tmp = sdb_plugin_get_ctx().interval;
441 if (tmp > 0)
442 SDB_PLUGIN_CCB(obj)->ccb_interval = tmp;
443 else
444 SDB_PLUGIN_CCB(obj)->ccb_interval = 0;
445 }
447 if (! (SDB_PLUGIN_CCB(obj)->ccb_next_update = sdb_gettime())) {
448 char errbuf[1024];
449 sdb_log(SDB_LOG_ERR, "plugin: Failed to determine current "
450 "time: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
451 sdb_object_deref(obj);
452 return -1;
453 }
455 if (sdb_llist_insert_sorted(collector_list, obj,
456 sdb_plugin_cmp_next_update)) {
457 sdb_object_deref(obj);
458 return -1;
459 }
461 /* pass control to the list */
462 sdb_object_deref(obj);
464 sdb_log(SDB_LOG_INFO, "plugin: Registered collector callback '%s' "
465 "(interval = %.3fs).", name,
466 SDB_TIME_TO_DOUBLE(SDB_PLUGIN_CCB(obj)->ccb_interval));
467 return 0;
468 } /* sdb_plugin_register_collector */
470 sdb_plugin_ctx_t
471 sdb_plugin_get_ctx(void)
472 {
473 sdb_plugin_ctx_t *ctx;
475 if (! plugin_ctx_key_initialized)
476 sdb_plugin_ctx_init();
477 ctx = pthread_getspecific(plugin_ctx_key);
479 if (! ctx)
480 ctx = sdb_plugin_ctx_create();
481 if (! ctx)
482 return plugin_default_ctx;
483 return *ctx;
484 } /* sdb_plugin_get_ctx */
486 sdb_plugin_ctx_t
487 sdb_plugin_set_ctx(sdb_plugin_ctx_t ctx)
488 {
489 sdb_plugin_ctx_t *tmp;
490 sdb_plugin_ctx_t old;
492 if (! plugin_ctx_key_initialized)
493 sdb_plugin_ctx_init();
494 tmp = pthread_getspecific(plugin_ctx_key);
496 if (! tmp)
497 tmp = sdb_plugin_ctx_create();
498 if (! tmp)
499 return plugin_default_ctx;
501 old = *tmp;
502 *tmp = ctx;
503 return old;
504 } /* sdb_plugin_set_ctx */
506 int
507 sdb_plugin_configure(const char *name, oconfig_item_t *ci)
508 {
509 sdb_plugin_cb_t *plugin;
510 sdb_plugin_config_cb callback;
512 sdb_plugin_ctx_t old_ctx;
514 int status;
516 if ((! name) || (! ci))
517 return -1;
519 plugin = SDB_PLUGIN_CB(sdb_llist_search_by_name(config_list, name));
520 if (! plugin) {
521 /* XXX: check if any such plugin has been loaded */
522 sdb_log(SDB_LOG_ERR, "plugin: Plugin '%s' did not register "
523 "a config callback.", name);
524 errno = ENOENT;
525 return -1;
526 }
528 old_ctx = sdb_plugin_set_ctx(plugin->cb_ctx);
529 callback = plugin->cb_callback;
530 status = callback(ci);
531 sdb_plugin_set_ctx(old_ctx);
532 return status;
533 } /* sdb_plugin_configure */
535 int
536 sdb_plugin_init_all(void)
537 {
538 sdb_llist_iter_t *iter;
540 iter = sdb_llist_get_iter(init_list);
541 while (sdb_llist_iter_has_next(iter)) {
542 sdb_plugin_init_cb callback;
543 sdb_plugin_ctx_t old_ctx;
545 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
546 assert(obj);
548 callback = SDB_PLUGIN_CB(obj)->cb_callback;
550 old_ctx = sdb_plugin_set_ctx(SDB_PLUGIN_CB(obj)->cb_ctx);
551 if (callback(SDB_PLUGIN_CB(obj)->cb_user_data)) {
552 /* XXX: unload plugin */
553 }
554 sdb_plugin_set_ctx(old_ctx);
555 }
556 sdb_llist_iter_destroy(iter);
557 return 0;
558 } /* sdb_plugin_init_all */
560 int
561 sdb_plugin_collector_loop(sdb_plugin_loop_t *loop)
562 {
563 if (! collector_list) {
564 sdb_log(SDB_LOG_WARNING, "plugin: No collectors registered. "
565 "Quiting main loop.");
566 return -1;
567 }
569 if (! loop)
570 return -1;
572 while (loop->do_loop) {
573 sdb_plugin_collector_cb callback;
574 sdb_plugin_ctx_t old_ctx;
576 sdb_time_t interval, now;
578 sdb_object_t *obj = sdb_llist_shift(collector_list);
579 if (! obj)
580 return -1;
582 callback = SDB_PLUGIN_CCB(obj)->ccb_callback;
584 if (! (now = sdb_gettime())) {
585 char errbuf[1024];
586 sdb_log(SDB_LOG_ERR, "plugin: Failed to determine current "
587 "time: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
588 now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
589 }
591 if (now < SDB_PLUGIN_CCB(obj)->ccb_next_update) {
592 interval = SDB_PLUGIN_CCB(obj)->ccb_next_update - now;
594 errno = 0;
595 while (loop->do_loop && sdb_sleep(interval, &interval)) {
596 if (errno != EINTR) {
597 char errbuf[1024];
598 sdb_log(SDB_LOG_ERR, "plugin: Failed to sleep: %s",
599 sdb_strerror(errno, errbuf, sizeof(errbuf)));
600 return -1;
601 }
602 errno = 0;
603 }
605 if (! loop->do_loop)
606 return 0;
607 }
609 old_ctx = sdb_plugin_set_ctx(SDB_PLUGIN_CCB(obj)->ccb_ctx);
610 if (callback(SDB_PLUGIN_CCB(obj)->ccb_user_data)) {
611 /* XXX */
612 }
613 sdb_plugin_set_ctx(old_ctx);
615 interval = SDB_PLUGIN_CCB(obj)->ccb_interval;
616 if (! interval)
617 interval = loop->default_interval;
618 if (! interval) {
619 sdb_log(SDB_LOG_WARNING, "plugin: No interval configured "
620 "for plugin '%s'; skipping any further "
621 "iterations.", obj->name);
622 sdb_object_deref(obj);
623 continue;
624 }
626 SDB_PLUGIN_CCB(obj)->ccb_next_update += interval;
628 if (! (now = sdb_gettime())) {
629 char errbuf[1024];
630 sdb_log(SDB_LOG_ERR, "plugin: Failed to determine current "
631 "time: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
632 now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
633 }
635 if (now > SDB_PLUGIN_CCB(obj)->ccb_next_update) {
636 sdb_log(SDB_LOG_WARNING, "plugin: Plugin '%s' took too "
637 "long; skipping iterations to keep up.",
638 obj->name);
639 SDB_PLUGIN_CCB(obj)->ccb_next_update = now;
640 }
642 if (sdb_llist_insert_sorted(collector_list, obj,
643 sdb_plugin_cmp_next_update)) {
644 sdb_log(SDB_LOG_ERR, "plugin: Failed to re-insert "
645 "plugin '%s' into collector list. Unable to further "
646 "use the plugin.",
647 obj->name);
648 sdb_object_deref(obj);
649 return -1;
650 }
652 /* pass control back to the list */
653 sdb_object_deref(obj);
654 }
655 return 0;
656 } /* sdb_plugin_read_loop */
658 int
659 sdb_plugin_log(int prio, const char *msg)
660 {
661 sdb_llist_iter_t *iter;
662 int ret = -1;
664 if (! log_list)
665 return fprintf(stderr, "[%s] %s\n", SDB_LOG_PRIO_TO_STRING(prio), msg);
667 iter = sdb_llist_get_iter(log_list);
668 while (sdb_llist_iter_has_next(iter)) {
669 sdb_plugin_log_cb callback;
670 int tmp;
672 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
673 assert(obj);
675 callback = SDB_PLUGIN_CB(obj)->cb_callback;
676 tmp = callback(prio, msg, SDB_PLUGIN_CB(obj)->cb_user_data);
677 if (tmp > ret)
678 ret = tmp;
679 }
680 sdb_llist_iter_destroy(iter);
681 return ret;
682 } /* sdb_plugin_log */
684 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */