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 /*
188 * private types
189 */
191 static int
192 sdb_plugin_cb_init(sdb_object_t *obj, va_list ap)
193 {
194 sdb_llist_t **list = va_arg(ap, sdb_llist_t **);
195 const char *type = va_arg(ap, const char *);
196 const char *name = va_arg(ap, const char *);
197 void *callback = va_arg(ap, void *);
198 sdb_object_t *ud = va_arg(ap, sdb_object_t *);
200 assert(list);
201 assert(type);
202 assert(obj);
204 if (sdb_plugin_find_by_name(*list, name)) {
205 sdb_log(SDB_LOG_WARNING, "plugin: %s callback '%s' "
206 "has already been registered. Ignoring newly "
207 "registered version.", type, name);
208 return -1;
209 }
211 snprintf(SDB_PLUGIN_CB(obj)->cb_name,
212 sizeof(SDB_PLUGIN_CB(obj)->cb_name),
213 "%s", name);
214 SDB_PLUGIN_CB(obj)->cb_name[sizeof(SDB_PLUGIN_CB(obj)->cb_name) - 1] = '\0';
215 SDB_PLUGIN_CB(obj)->cb_callback = callback;
216 SDB_PLUGIN_CB(obj)->cb_ctx = sdb_plugin_get_ctx();
218 sdb_object_ref(ud);
219 SDB_PLUGIN_CB(obj)->cb_user_data = ud;
220 return 0;
221 } /* sdb_plugin_cb_init */
223 static void
224 sdb_plugin_cb_destroy(sdb_object_t *obj)
225 {
226 assert(obj);
227 sdb_object_deref(SDB_PLUGIN_CB(obj)->cb_user_data);
228 } /* sdb_plugin_cb_destroy */
230 static sdb_type_t sdb_plugin_cb_type = {
231 sizeof(sdb_plugin_cb_t),
233 sdb_plugin_cb_init,
234 sdb_plugin_cb_destroy
235 };
237 static sdb_type_t sdb_plugin_collector_cb_type = {
238 sizeof(sdb_plugin_collector_cb_t),
240 sdb_plugin_cb_init,
241 sdb_plugin_cb_destroy
242 };
244 static int
245 sdb_plugin_add_callback(sdb_llist_t **list, const char *type,
246 const char *name, void *callback, sdb_object_t *user_data)
247 {
248 sdb_object_t *obj;
250 if ((! name) || (! callback))
251 return -1;
253 assert(list);
255 if (! *list)
256 *list = sdb_llist_create();
257 if (! *list)
258 return -1;
260 obj = sdb_object_create(sdb_plugin_cb_type,
261 list, type, name, callback, user_data);
262 if (! obj)
263 return -1;
265 if (sdb_llist_append(*list, obj)) {
266 sdb_object_deref(obj);
267 return -1;
268 }
270 /* pass control to the list */
271 sdb_object_deref(obj);
273 sdb_log(SDB_LOG_INFO, "plugin: Registered %s callback '%s'.",
274 type, name);
275 return 0;
276 } /* sdb_plugin_add_callback */
278 /*
279 * public API
280 */
282 int
283 sdb_plugin_load(const char *name)
284 {
285 char real_name[strlen(name) > 0 ? strlen(name) : 1];
286 const char *name_ptr;
287 char *tmp;
289 char filename[1024];
291 lt_dlhandle lh;
293 int (*mod_init)(sdb_plugin_info_t *);
294 sdb_plugin_info_t plugin_info = SDB_PLUGIN_INFO_INIT;
296 int status;
298 if ((! name) || (! *name))
299 return -1;
301 real_name[0] = '\0';
302 name_ptr = name;
304 while ((tmp = strstr(name_ptr, "::"))) {
305 strncat(real_name, name_ptr, (size_t)(tmp - name_ptr));
306 strcat(real_name, "/");
307 name_ptr = tmp + strlen("::");
308 }
309 strcat(real_name, name_ptr);
311 snprintf(filename, sizeof(filename), "%s/%s.so",
312 PKGLIBDIR, real_name);
313 filename[sizeof(filename) - 1] = '\0';
315 if (access(filename, R_OK)) {
316 char errbuf[1024];
317 sdb_log(SDB_LOG_ERR, "plugin: Failed to load plugin '%s' (%s): %s",
318 name, filename, sdb_strerror(errno, errbuf, sizeof(errbuf)));
319 return -1;
320 }
322 lt_dlinit();
323 lt_dlerror();
325 lh = lt_dlopen(filename);
326 if (! lh) {
327 sdb_log(SDB_LOG_ERR, "plugin: Failed to load plugin '%s': %s"
328 "The most common cause for this problem are missing "
329 "dependencies.\n", name, lt_dlerror());
330 return -1;
331 }
333 mod_init = (int (*)(sdb_plugin_info_t *))lt_dlsym(lh, "sdb_module_init");
334 if (! mod_init) {
335 sdb_log(SDB_LOG_ERR, "plugin: Failed to load plugin '%s': "
336 "could not find symbol 'sdb_module_init'", name);
337 return -1;
338 }
340 status = mod_init(&plugin_info);
341 if (status) {
342 sdb_log(SDB_LOG_ERR, "plugin: Failed to initialize "
343 "plugin '%s'", name);
344 return -1;
345 }
347 /* compare minor version */
348 if ((plugin_info.version < 0)
349 || ((int)(plugin_info.version / 100) != (int)(SDB_VERSION / 100)))
350 sdb_log(SDB_LOG_WARNING, "plugin: WARNING: version of "
351 "plugin '%s' (%i.%i.%i) does not match our version "
352 "(%i.%i.%i); this might cause problems",
353 name, SDB_VERSION_DECODE(plugin_info.version),
354 SDB_VERSION_DECODE(SDB_VERSION));
356 sdb_log(SDB_LOG_INFO, "plugin: Successfully loaded "
357 "plugin '%s' v%i (%s)\n\t%s",
358 plugin_info.name, plugin_info.plugin_version,
359 plugin_info.description, plugin_info.copyright);
360 return 0;
361 } /* sdb_plugin_load */
363 int
364 sdb_plugin_set_info(sdb_plugin_info_t *info, int type, ...)
365 {
366 va_list ap;
368 if (! info)
369 return -1;
371 va_start(ap, type);
373 switch (type) {
374 case SDB_PLUGIN_INFO_NAME:
375 {
376 char *name = va_arg(ap, char *);
377 info->name = name;
378 }
379 break;
380 case SDB_PLUGIN_INFO_DESC:
381 {
382 char *desc = va_arg(ap, char *);
383 info->description = desc;
384 }
385 break;
386 case SDB_PLUGIN_INFO_COPYRIGHT:
387 {
388 char *copyright = va_arg(ap, char *);
389 info->copyright = copyright;
390 }
391 break;
392 case SDB_PLUGIN_INFO_LICENSE:
393 {
394 char *license = va_arg(ap, char *);
395 info->license = license;
396 }
397 break;
398 case SDB_PLUGIN_INFO_VERSION:
399 {
400 int version = va_arg(ap, int);
401 info->version = version;
402 }
403 break;
404 case SDB_PLUGIN_INFO_PLUGIN_VERSION:
405 {
406 int version = va_arg(ap, int);
407 info->plugin_version = version;
408 }
409 break;
410 default:
411 va_end(ap);
412 return -1;
413 }
415 va_end(ap);
416 return 0;
417 } /* sdb_plugin_set_info */
419 int
420 sdb_plugin_register_config(const char *name, sdb_plugin_config_cb callback)
421 {
422 return sdb_plugin_add_callback(&config_list, "init", name,
423 callback, NULL);
424 } /* sdb_plugin_register_config */
426 int
427 sdb_plugin_register_init(const char *name, sdb_plugin_init_cb callback,
428 sdb_object_t *user_data)
429 {
430 return sdb_plugin_add_callback(&init_list, "init", name,
431 callback, user_data);
432 } /* sdb_plugin_register_init */
434 int
435 sdb_plugin_register_shutdown(const char *name, sdb_plugin_shutdown_cb callback,
436 sdb_object_t *user_data)
437 {
438 return sdb_plugin_add_callback(&shutdown_list, "shutdown", name,
439 callback, user_data);
440 } /* sdb_plugin_register_shutdown */
442 int
443 sdb_plugin_register_collector(const char *name, sdb_plugin_collector_cb callback,
444 const sdb_time_t *interval, sdb_object_t *user_data)
445 {
446 sdb_object_t *obj;
448 if ((! name) || (! callback))
449 return -1;
451 if (! collector_list)
452 collector_list = sdb_llist_create();
453 if (! collector_list)
454 return -1;
456 obj = sdb_object_create(sdb_plugin_collector_cb_type,
457 &collector_list, "collector", name, callback, user_data);
458 if (! obj)
459 return -1;
461 if (interval)
462 SDB_PLUGIN_CCB(obj)->ccb_interval = *interval;
463 else {
464 sdb_time_t tmp = sdb_plugin_get_ctx().interval;
466 if (tmp > 0)
467 SDB_PLUGIN_CCB(obj)->ccb_interval = tmp;
468 else
469 SDB_PLUGIN_CCB(obj)->ccb_interval = 0;
470 }
472 if (! (SDB_PLUGIN_CCB(obj)->ccb_next_update = sdb_gettime())) {
473 char errbuf[1024];
474 sdb_log(SDB_LOG_ERR, "plugin: Failed to determine current "
475 "time: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
476 sdb_object_deref(obj);
477 return -1;
478 }
480 if (sdb_llist_insert_sorted(collector_list, obj,
481 sdb_plugin_cmp_next_update)) {
482 sdb_object_deref(obj);
483 return -1;
484 }
486 /* pass control to the list */
487 sdb_object_deref(obj);
489 sdb_log(SDB_LOG_INFO, "plugin: Registered collector callback '%s' "
490 "(interval = %.3fs).", name,
491 SDB_TIME_TO_DOUBLE(SDB_PLUGIN_CCB(obj)->ccb_interval));
492 return 0;
493 } /* sdb_plugin_register_collector */
495 sdb_plugin_ctx_t
496 sdb_plugin_get_ctx(void)
497 {
498 sdb_plugin_ctx_t *ctx;
500 if (! plugin_ctx_key_initialized)
501 sdb_plugin_ctx_init();
502 ctx = pthread_getspecific(plugin_ctx_key);
504 if (! ctx)
505 ctx = sdb_plugin_ctx_create();
506 if (! ctx)
507 return plugin_default_ctx;
508 return *ctx;
509 } /* sdb_plugin_get_ctx */
511 sdb_plugin_ctx_t
512 sdb_plugin_set_ctx(sdb_plugin_ctx_t ctx)
513 {
514 sdb_plugin_ctx_t *tmp;
515 sdb_plugin_ctx_t old;
517 if (! plugin_ctx_key_initialized)
518 sdb_plugin_ctx_init();
519 tmp = pthread_getspecific(plugin_ctx_key);
521 if (! tmp)
522 tmp = sdb_plugin_ctx_create();
523 if (! tmp)
524 return plugin_default_ctx;
526 old = *tmp;
527 *tmp = ctx;
528 return old;
529 } /* sdb_plugin_set_ctx */
531 int
532 sdb_plugin_configure(const char *name, oconfig_item_t *ci)
533 {
534 sdb_plugin_cb_t *plugin;
535 sdb_plugin_config_cb callback;
537 sdb_plugin_ctx_t old_ctx;
539 int status;
541 if ((! name) || (! ci))
542 return -1;
544 plugin = sdb_plugin_find_by_name(config_list, name);
545 if (! plugin) {
546 /* XXX: check if any such plugin has been loaded */
547 sdb_log(SDB_LOG_ERR, "plugin: Plugin '%s' did not register "
548 "a config callback.", name);
549 errno = ENOENT;
550 return -1;
551 }
553 old_ctx = sdb_plugin_set_ctx(plugin->cb_ctx);
554 callback = plugin->cb_callback;
555 status = callback(ci);
556 sdb_plugin_set_ctx(old_ctx);
557 return status;
558 } /* sdb_plugin_configure */
560 int
561 sdb_plugin_init_all(void)
562 {
563 sdb_llist_iter_t *iter;
565 iter = sdb_llist_get_iter(init_list);
566 while (sdb_llist_iter_has_next(iter)) {
567 sdb_plugin_init_cb callback;
568 sdb_plugin_ctx_t old_ctx;
570 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
571 assert(obj);
573 callback = SDB_PLUGIN_CB(obj)->cb_callback;
575 old_ctx = sdb_plugin_set_ctx(SDB_PLUGIN_CB(obj)->cb_ctx);
576 if (callback(SDB_PLUGIN_CB(obj)->cb_user_data)) {
577 /* XXX: unload plugin */
578 }
579 sdb_plugin_set_ctx(old_ctx);
580 }
581 return 0;
582 } /* sdb_plugin_init_all */
584 int
585 sdb_plugin_collector_loop(sdb_plugin_loop_t *loop)
586 {
587 if (! collector_list) {
588 sdb_log(SDB_LOG_WARNING, "plugin: No collectors registered. "
589 "Quiting main loop.");
590 return -1;
591 }
593 if (! loop)
594 return -1;
596 while (loop->do_loop) {
597 sdb_plugin_collector_cb callback;
598 sdb_plugin_ctx_t old_ctx;
600 sdb_time_t interval, now;
602 sdb_object_t *obj = sdb_llist_shift(collector_list);
603 if (! obj)
604 return -1;
606 callback = SDB_PLUGIN_CCB(obj)->ccb_callback;
608 if (! (now = sdb_gettime())) {
609 char errbuf[1024];
610 sdb_log(SDB_LOG_ERR, "plugin: Failed to determine current "
611 "time: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
612 now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
613 }
615 if (now < SDB_PLUGIN_CCB(obj)->ccb_next_update) {
616 interval = SDB_PLUGIN_CCB(obj)->ccb_next_update - now;
618 errno = 0;
619 while (loop->do_loop && sdb_sleep(interval, &interval)) {
620 if (errno != EINTR) {
621 char errbuf[1024];
622 sdb_log(SDB_LOG_ERR, "plugin: Failed to sleep: %s",
623 sdb_strerror(errno, errbuf, sizeof(errbuf)));
624 return -1;
625 }
626 errno = 0;
627 }
629 if (! loop->do_loop)
630 return 0;
631 }
633 old_ctx = sdb_plugin_set_ctx(SDB_PLUGIN_CCB(obj)->ccb_ctx);
634 if (callback(SDB_PLUGIN_CCB(obj)->ccb_user_data)) {
635 /* XXX */
636 }
637 sdb_plugin_set_ctx(old_ctx);
639 interval = SDB_PLUGIN_CCB(obj)->ccb_interval;
640 if (! interval)
641 interval = loop->default_interval;
642 if (! interval) {
643 sdb_log(SDB_LOG_WARNING, "plugin: No interval configured "
644 "for plugin '%s'; skipping any further "
645 "iterations.", SDB_PLUGIN_CCB(obj)->ccb_name);
646 sdb_object_deref(obj);
647 continue;
648 }
650 SDB_PLUGIN_CCB(obj)->ccb_next_update += interval;
652 if (! (now = sdb_gettime())) {
653 char errbuf[1024];
654 sdb_log(SDB_LOG_ERR, "plugin: Failed to determine current "
655 "time: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
656 now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
657 }
659 if (now > SDB_PLUGIN_CCB(obj)->ccb_next_update) {
660 sdb_log(SDB_LOG_WARNING, "plugin: Plugin '%s' took too "
661 "long; skipping iterations to keep up.",
662 SDB_PLUGIN_CCB(obj)->ccb_name);
663 SDB_PLUGIN_CCB(obj)->ccb_next_update = now;
664 }
666 if (sdb_llist_insert_sorted(collector_list, obj,
667 sdb_plugin_cmp_next_update)) {
668 sdb_log(SDB_LOG_ERR, "plugin: Failed to re-insert "
669 "plugin '%s' into collector list. Unable to further "
670 "use the plugin.",
671 SDB_PLUGIN_CCB(obj)->ccb_name);
672 sdb_object_deref(obj);
673 return -1;
674 }
676 /* pass control back to the list */
677 sdb_object_deref(obj);
678 }
679 return 0;
680 } /* sdb_plugin_read_loop */
682 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */