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/llist.h"
31 #include "utils/string.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 fprintf(stderr, "plugin: %s callback '%s' has already been "
202 "registered. Ignoring newly registered version.\n",
203 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 fprintf(stderr, "plugin: Registered %s callback '%s'.\n", type, name);
256 return 0;
257 } /* sdb_plugin_add_callback */
259 /*
260 * public API
261 */
263 int
264 sdb_plugin_load(const char *name)
265 {
266 char filename[1024];
268 lt_dlhandle lh;
270 int (*mod_init)(sdb_plugin_info_t *);
271 sdb_plugin_info_t plugin_info = SDB_PLUGIN_INFO_INIT;
273 int status;
275 snprintf(filename, sizeof(filename), "%s/%s.so",
276 PKGLIBDIR, name);
277 filename[sizeof(filename) - 1] = '\0';
279 if (access(filename, R_OK)) {
280 char errbuf[1024];
281 fprintf(stderr, "plugin: Failed to load plugin '%s': %s\n",
282 name, sdb_strerror(errno, errbuf, sizeof(errbuf)));
283 return -1;
284 }
286 lt_dlinit();
287 lt_dlerror();
289 lh = lt_dlopen(filename);
290 if (! lh) {
291 fprintf(stderr, "plugin: Failed to load plugin '%s': %s\n"
292 "The most common cause for this problem are missing "
293 "dependencies.\n", name, lt_dlerror());
294 return -1;
295 }
297 mod_init = (int (*)(sdb_plugin_info_t *))lt_dlsym(lh, "sdb_module_init");
298 if (! mod_init) {
299 fprintf(stderr, "plugin: Failed to load plugin '%s': "
300 "could not find symbol 'sdb_module_init'\n", name);
301 return -1;
302 }
304 status = mod_init(&plugin_info);
305 if (status) {
306 fprintf(stderr, "plugin: Failed to initialize plugin '%s'\n", name);
307 return -1;
308 }
310 /* compare minor version */
311 if ((plugin_info.version < 0)
312 || ((int)(plugin_info.version / 100) != (int)(SDB_VERSION / 100)))
313 fprintf(stderr, "plugin: WARNING: version of plugin '%s' (%i.%i.%i) "
314 "does not match our version (%i.%i.%i); "
315 "this might cause problems\n",
316 name, SDB_VERSION_DECODE(plugin_info.version),
317 SDB_VERSION_DECODE(SDB_VERSION));
319 fprintf(stderr, "plugin: Successfully loaded "
320 "plugin '%s' v%i (%s)\n\t%s\n",
321 plugin_info.name, plugin_info.plugin_version,
322 plugin_info.description, plugin_info.copyright);
323 return 0;
324 } /* sdb_plugin_load */
326 int
327 sdb_plugin_set_info(sdb_plugin_info_t *info, int type, ...)
328 {
329 va_list ap;
331 if (! info)
332 return -1;
334 va_start(ap, type);
336 switch (type) {
337 case SDB_PLUGIN_INFO_NAME:
338 {
339 char *name = va_arg(ap, char *);
340 info->name = name;
341 }
342 break;
343 case SDB_PLUGIN_INFO_DESC:
344 {
345 char *desc = va_arg(ap, char *);
346 info->description = desc;
347 }
348 break;
349 case SDB_PLUGIN_INFO_COPYRIGHT:
350 {
351 char *copyright = va_arg(ap, char *);
352 info->copyright = copyright;
353 }
354 break;
355 case SDB_PLUGIN_INFO_LICENSE:
356 {
357 char *license = va_arg(ap, char *);
358 info->license = license;
359 }
360 break;
361 case SDB_PLUGIN_INFO_VERSION:
362 {
363 int version = va_arg(ap, int);
364 info->version = version;
365 }
366 break;
367 case SDB_PLUGIN_INFO_PLUGIN_VERSION:
368 {
369 int version = va_arg(ap, int);
370 info->plugin_version = version;
371 }
372 break;
373 default:
374 va_end(ap);
375 return -1;
376 }
378 va_end(ap);
379 return 0;
380 } /* sdb_plugin_set_info */
382 int
383 sdb_plugin_register_config(const char *name, sdb_plugin_config_cb callback)
384 {
385 return sdb_plugin_add_callback(&config_list, "init", name,
386 callback, NULL);
387 } /* sdb_plugin_register_config */
389 int
390 sdb_plugin_register_init(const char *name, sdb_plugin_init_cb callback,
391 sdb_object_t *user_data)
392 {
393 return sdb_plugin_add_callback(&init_list, "init", name,
394 callback, user_data);
395 } /* sdb_plugin_register_init */
397 int
398 sdb_plugin_register_shutdown(const char *name, sdb_plugin_shutdown_cb callback,
399 sdb_object_t *user_data)
400 {
401 return sdb_plugin_add_callback(&shutdown_list, "shutdown", name,
402 callback, user_data);
403 } /* sdb_plugin_register_shutdown */
405 int
406 sdb_plugin_register_collector(const char *name, sdb_plugin_collector_cb callback,
407 const sdb_time_t *interval, sdb_object_t *user_data)
408 {
409 sdb_object_t *obj;
411 if ((! name) || (! callback))
412 return -1;
414 if (! collector_list)
415 collector_list = sdb_llist_create();
416 if (! collector_list)
417 return -1;
419 obj = sdb_object_create(sizeof(sdb_plugin_collector_cb_t),
420 sdb_plugin_cb_init, sdb_plugin_cb_destroy,
421 &collector_list, "collector", name, callback, user_data);
422 if (! obj)
423 return -1;
425 if (interval)
426 SDB_PLUGIN_CCB(obj)->ccb_interval = *interval;
427 else {
428 sdb_time_t tmp = sdb_plugin_get_ctx().interval;
430 if (tmp > 0)
431 SDB_PLUGIN_CCB(obj)->ccb_interval = tmp;
432 else
433 SDB_PLUGIN_CCB(obj)->ccb_interval = 0;
434 }
436 if (! (SDB_PLUGIN_CCB(obj)->ccb_next_update = sdb_gettime())) {
437 char errbuf[1024];
438 fprintf(stderr, "plugin: Failed to determine current time: %s\n",
439 sdb_strerror(errno, errbuf, sizeof(errbuf)));
440 sdb_object_deref(obj);
441 return -1;
442 }
444 if (sdb_llist_insert_sorted(collector_list, obj,
445 sdb_plugin_cmp_next_update)) {
446 sdb_object_deref(obj);
447 return -1;
448 }
450 /* pass control to the list */
451 sdb_object_deref(obj);
453 fprintf(stderr, "plugin: Registered collector callback '%s' "
454 "(interval = %.3fs).\n", name,
455 SDB_TIME_TO_DOUBLE(SDB_PLUGIN_CCB(obj)->ccb_interval));
456 return 0;
457 } /* sdb_plugin_register_collector */
459 sdb_plugin_ctx_t
460 sdb_plugin_get_ctx(void)
461 {
462 sdb_plugin_ctx_t *ctx;
464 if (! plugin_ctx_key_initialized)
465 sdb_plugin_ctx_init();
466 ctx = pthread_getspecific(plugin_ctx_key);
468 if (! ctx)
469 ctx = sdb_plugin_ctx_create();
470 if (! ctx)
471 return plugin_default_ctx;
472 return *ctx;
473 } /* sdb_plugin_get_ctx */
475 sdb_plugin_ctx_t
476 sdb_plugin_set_ctx(sdb_plugin_ctx_t ctx)
477 {
478 sdb_plugin_ctx_t *tmp;
479 sdb_plugin_ctx_t old;
481 if (! plugin_ctx_key_initialized)
482 sdb_plugin_ctx_init();
483 tmp = pthread_getspecific(plugin_ctx_key);
485 if (! tmp)
486 tmp = sdb_plugin_ctx_create();
487 if (! tmp)
488 return plugin_default_ctx;
490 old = *tmp;
491 *tmp = ctx;
492 return old;
493 } /* sdb_plugin_set_ctx */
495 int
496 sdb_plugin_configure(const char *name, oconfig_item_t *ci)
497 {
498 sdb_plugin_cb_t *plugin;
499 sdb_plugin_config_cb callback;
501 sdb_plugin_ctx_t old_ctx;
503 int status;
505 if ((! name) || (! ci))
506 return -1;
508 plugin = sdb_plugin_find_by_name(config_list, name);
509 if (! plugin) {
510 fprintf(stderr, "plugin: Plugin '%s' did not register "
511 "a config callback.\n", name);
512 errno = ENOENT;
513 return -1;
514 }
516 old_ctx = sdb_plugin_set_ctx(plugin->cb_ctx);
517 callback = plugin->cb_callback;
518 status = callback(ci);
519 sdb_plugin_set_ctx(old_ctx);
520 return status;
521 } /* sdb_plugin_configure */
523 int
524 sdb_plugin_init_all(void)
525 {
526 sdb_llist_iter_t *iter;
528 iter = sdb_llist_get_iter(init_list);
529 while (sdb_llist_iter_has_next(iter)) {
530 sdb_plugin_init_cb callback;
531 sdb_plugin_ctx_t old_ctx;
533 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
534 assert(obj);
536 callback = SDB_PLUGIN_CB(obj)->cb_callback;
538 old_ctx = sdb_plugin_set_ctx(SDB_PLUGIN_CB(obj)->cb_ctx);
539 if (callback(SDB_PLUGIN_CB(obj)->cb_user_data)) {
540 /* XXX: unload plugin */
541 }
542 sdb_plugin_set_ctx(old_ctx);
543 }
544 return 0;
545 } /* sdb_plugin_init_all */
547 int
548 sdb_plugin_collector_loop(sdb_plugin_loop_t *loop)
549 {
550 if ((! collector_list) || (! loop))
551 return -1;
553 while (loop->do_loop) {
554 sdb_plugin_collector_cb callback;
555 sdb_plugin_ctx_t old_ctx;
557 sdb_time_t interval, now;
559 sdb_object_t *obj = sdb_llist_shift(collector_list);
560 if (! obj)
561 return -1;
563 callback = SDB_PLUGIN_CCB(obj)->ccb_callback;
565 if (! (now = sdb_gettime())) {
566 char errbuf[1024];
567 fprintf(stderr, "plugin: Failed to determine current time: %s\n",
568 sdb_strerror(errno, errbuf, sizeof(errbuf)));
569 now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
570 }
572 if (now < SDB_PLUGIN_CCB(obj)->ccb_next_update) {
573 interval = SDB_PLUGIN_CCB(obj)->ccb_next_update - now;
575 errno = 0;
576 while (loop->do_loop && sdb_sleep(interval, &interval)) {
577 if (errno != EINTR) {
578 char errbuf[1024];
579 fprintf(stderr, "plugin: Failed to sleep: %s\n",
580 sdb_strerror(errno, errbuf, sizeof(errbuf)));
581 return -1;
582 }
583 errno = 0;
584 }
586 if (! loop->do_loop)
587 return 0;
588 }
590 old_ctx = sdb_plugin_set_ctx(SDB_PLUGIN_CCB(obj)->ccb_ctx);
591 if (callback(SDB_PLUGIN_CCB(obj)->ccb_user_data)) {
592 /* XXX */
593 }
594 sdb_plugin_set_ctx(old_ctx);
596 interval = SDB_PLUGIN_CCB(obj)->ccb_interval;
597 if (! interval)
598 interval = loop->default_interval;
599 if (! interval) {
600 fprintf(stderr, "plugin: No interval configured "
601 "for plugin '%s'; skipping any further "
602 "iterations.\n", SDB_PLUGIN_CCB(obj)->ccb_name);
603 sdb_object_deref(obj);
604 continue;
605 }
607 SDB_PLUGIN_CCB(obj)->ccb_next_update += interval;
609 if (! (now = sdb_gettime())) {
610 char errbuf[1024];
611 fprintf(stderr, "plugin: Failed to determine current time: %s\n",
612 sdb_strerror(errno, errbuf, sizeof(errbuf)));
613 now = SDB_PLUGIN_CCB(obj)->ccb_next_update;
614 }
616 if (now > SDB_PLUGIN_CCB(obj)->ccb_next_update) {
617 fprintf(stderr, "plugin: Plugin '%s' took too long; "
618 "skipping iterations to keep up.\n",
619 SDB_PLUGIN_CCB(obj)->ccb_name);
620 SDB_PLUGIN_CCB(obj)->ccb_next_update = now;
621 }
623 if (sdb_llist_insert_sorted(collector_list, obj,
624 sdb_plugin_cmp_next_update)) {
625 fprintf(stderr, "plugin: Failed to re-insert "
626 "plugin '%s' into collector list.\n",
627 SDB_PLUGIN_CCB(obj)->ccb_name);
628 sdb_object_deref(obj);
629 return -1;
630 }
632 /* pass control back to the list */
633 sdb_object_deref(obj);
634 }
635 return 0;
636 } /* sdb_plugin_read_loop */
638 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */