Code

Added missing daemon/config.h.
[sysdb.git] / src / core / plugin.c
1 /*
2  * syscollector - 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 "syscollector.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 sc_plugin_info {
53         char *name;
55         char *description;
56         char *copyright;
57         char *license;
59         int   version;
60         int   plugin_version;
61 };
62 #define SC_PLUGIN_INFO_INIT { "no name set", "no description set", \
63         /* copyright */ "", /* license */ "", \
64         /* version */ -1, /* plugin_version */ -1 }
66 typedef struct {
67         sc_object_t super;
68         char cb_name[64];
69         void *cb_callback;
70         sc_object_t *cb_user_data;
71         sc_plugin_ctx_t cb_ctx;
72 } sc_plugin_cb_t;
73 #define SC_PLUGIN_CB_INIT { SC_OBJECT_INIT, "", NULL, NULL, SC_PLUGIN_CTX_INIT }
75 typedef struct {
76         sc_plugin_cb_t super;
77 #define ccb_name super.cb_name
78 #define ccb_callback super.cb_callback
79 #define ccb_user_data super.cb_user_data
80 #define ccb_ctx super.cb_ctx
81         sc_time_t ccb_interval;
82         sc_time_t ccb_next_update;
83 } sc_plugin_collector_cb_t;
85 #define SC_PLUGIN_CB(obj) ((sc_plugin_cb_t *)(obj))
86 #define SC_PLUGIN_CCB(obj) ((sc_plugin_collector_cb_t *)(obj))
88 /*
89  * private variables
90  */
92 static sc_plugin_ctx_t  plugin_default_ctx = SC_PLUGIN_CTX_INIT;
94 static pthread_key_t    plugin_ctx_key;
95 static _Bool            plugin_ctx_key_initialized = 0;
97 static sc_llist_t      *config_list = NULL;
98 static sc_llist_t      *init_list = NULL;
99 static sc_llist_t      *collector_list = NULL;
100 static sc_llist_t      *shutdown_list = NULL;
102 /*
103  * private helper functions
104  */
106 static void
107 sc_plugin_ctx_destructor(void *ctx)
109         if (! ctx)
110                 return;
111         free(ctx);
112 } /* sc_plugin_ctx_destructor */
114 static void
115 sc_plugin_ctx_init(void)
117         if (plugin_ctx_key_initialized)
118                 return;
120         pthread_key_create(&plugin_ctx_key, sc_plugin_ctx_destructor);
121         plugin_ctx_key_initialized = 1;
122 } /* sc_plugin_ctx_init */
124 static sc_plugin_ctx_t *
125 sc_plugin_ctx_create(void)
127         sc_plugin_ctx_t *ctx;
129         ctx = malloc(sizeof(*ctx));
130         if (! ctx)
131                 return NULL;
133         *ctx = plugin_default_ctx;
135         if (! plugin_ctx_key_initialized)
136                 sc_plugin_ctx_init();
137         pthread_setspecific(plugin_ctx_key, ctx);
138         return ctx;
139 } /* sc_plugin_ctx_create */
141 static int
142 sc_plugin_cmp_name(const sc_object_t *a, const sc_object_t *b)
144         const sc_plugin_cb_t *cb1 = (const sc_plugin_cb_t *)a;
145         const sc_plugin_cb_t *cb2 = (const sc_plugin_cb_t *)b;
147         assert(cb1 && cb2);
148         return strcasecmp(cb1->cb_name, cb2->cb_name);
149 } /* sc_plugin_cmp_name */
151 static int
152 sc_plugin_cmp_next_update(const sc_object_t *a, const sc_object_t *b)
154         const sc_plugin_collector_cb_t *ccb1
155                 = (const sc_plugin_collector_cb_t *)a;
156         const sc_plugin_collector_cb_t *ccb2
157                 = (const sc_plugin_collector_cb_t *)b;
159         assert(ccb1 && ccb2);
161         return (ccb1->ccb_next_update > ccb2->ccb_next_update)
162                 ? 1 : (ccb1->ccb_next_update < ccb2->ccb_next_update)
163                 ? -1 : 0;
164 } /* sc_plugin_cmp_next_update */
166 static sc_plugin_cb_t *
167 sc_plugin_find_by_name(sc_llist_t *list, const char *name)
169         sc_plugin_cb_t tmp = SC_PLUGIN_CB_INIT;
171         sc_object_t *obj;
172         assert(name);
174         if (! list)
175                 return NULL;
177         snprintf(tmp.cb_name, sizeof(tmp.cb_name), "%s", name);
178         tmp.cb_name[sizeof(tmp.cb_name) - 1] = '\0';
179         obj = sc_llist_search(list, SC_OBJ(&tmp), sc_plugin_cmp_name);
180         if (! obj)
181                 return NULL;
182         return SC_PLUGIN_CB(obj);
183 } /* sc_plugin_find_by_name */
185 static int
186 sc_plugin_cb_init(sc_object_t *obj, va_list ap)
188         sc_llist_t **list = va_arg(ap, sc_llist_t **);
189         const char  *type = va_arg(ap, const char *);
190         const char  *name = va_arg(ap, const char *);
191         void    *callback = va_arg(ap, void *);
192         sc_object_t   *ud = va_arg(ap, sc_object_t *);
194         assert(list);
195         assert(type);
196         assert(obj);
198         if (sc_plugin_find_by_name(*list, name)) {
199                 fprintf(stderr, "plugin: %s callback '%s' has already been "
200                                 "registered. Ignoring newly registered version.\n",
201                                 type, name);
202                 return -1;
203         }
205         snprintf(SC_PLUGIN_CB(obj)->cb_name,
206                         sizeof(SC_PLUGIN_CB(obj)->cb_name),
207                         "%s", name);
208         SC_PLUGIN_CB(obj)->cb_name[sizeof(SC_PLUGIN_CB(obj)->cb_name) - 1] = '\0';
209         SC_PLUGIN_CB(obj)->cb_callback = callback;
210         SC_PLUGIN_CB(obj)->cb_ctx      = sc_plugin_get_ctx();
212         sc_object_ref(ud);
213         SC_PLUGIN_CB(obj)->cb_user_data = ud;
214         return 0;
215 } /* sc_plugin_cb_init */
217 static void
218 sc_plugin_cb_destroy(sc_object_t *obj)
220         assert(obj);
221         sc_object_deref(SC_PLUGIN_CB(obj)->cb_user_data);
222 } /* sc_plugin_cb_destroy */
224 static int
225 sc_plugin_add_callback(sc_llist_t **list, const char *type,
226                 const char *name, void *callback, sc_object_t *user_data)
228         sc_object_t *obj;
230         if ((! name) || (! callback))
231                 return -1;
233         assert(list);
235         if (! *list)
236                 *list = sc_llist_create();
237         if (! *list)
238                 return -1;
240         obj = sc_object_create(sizeof(sc_plugin_cb_t), sc_plugin_cb_init,
241                         sc_plugin_cb_destroy, list, type, name, callback, user_data);
242         if (! obj)
243                 return -1;
245         if (sc_llist_append(*list, obj)) {
246                 sc_object_deref(obj);
247                 return -1;
248         }
250         /* pass control to the list */
251         sc_object_deref(obj);
253         fprintf(stderr, "plugin: Registered %s callback '%s'.\n", type, name);
254         return 0;
255 } /* sc_plugin_add_callback */
257 /*
258  * public API
259  */
261 int
262 sc_plugin_load(const char *name)
264         char filename[1024];
266         lt_dlhandle lh;
268         int (*mod_init)(sc_plugin_info_t *);
269         sc_plugin_info_t plugin_info = SC_PLUGIN_INFO_INIT;
271         int status;
273         snprintf(filename, sizeof(filename), "%s/%s.so",
274                         PKGLIBDIR, name);
275         filename[sizeof(filename) - 1] = '\0';
277         if (access(filename, R_OK)) {
278                 char errbuf[1024];
279                 fprintf(stderr, "plugin: Failed to load plugin '%s': %s\n",
280                                 name, sc_strerror(errno, errbuf, sizeof(errbuf)));
281                 return -1;
282         }
284         lt_dlinit();
285         lt_dlerror();
287         lh = lt_dlopen(filename);
288         if (! lh) {
289                 fprintf(stderr, "plugin: Failed to load plugin '%s': %s\n"
290                                 "The most common cause for this problem are missing "
291                                 "dependencies.\n", name, lt_dlerror());
292                 return -1;
293         }
295         mod_init = (int (*)(sc_plugin_info_t *))lt_dlsym(lh, "sc_module_init");
296         if (! mod_init) {
297                 fprintf(stderr, "plugin: Failed to load plugin '%s': "
298                                 "could not find symbol 'sc_module_init'\n", name);
299                 return -1;
300         }
302         status = mod_init(&plugin_info);
303         if (status) {
304                 fprintf(stderr, "plugin: Failed to initialize plugin '%s'\n", name);
305                 return -1;
306         }
308         /* compare minor version */
309         if ((plugin_info.version < 0)
310                         || ((int)(plugin_info.version / 100) != (int)(SC_VERSION / 100)))
311                 fprintf(stderr, "plugin: WARNING: version of plugin '%s' (%i.%i.%i) "
312                                 "does not match our version (%i.%i.%i); "
313                                 "this might cause problems\n",
314                                 name, SC_VERSION_DECODE(plugin_info.version),
315                                 SC_VERSION_DECODE(SC_VERSION));
317         fprintf(stderr, "plugin: Successfully loaded "
318                         "plugin '%s' v%i (%s)\n\t%s\n",
319                         plugin_info.name, plugin_info.plugin_version,
320                         plugin_info.description, plugin_info.copyright);
321         return 0;
322 } /* sc_plugin_load */
324 int
325 sc_plugin_set_info(sc_plugin_info_t *info, int type, ...)
327         va_list ap;
329         if (! info)
330                 return -1;
332         va_start(ap, type);
334         switch (type) {
335                 case SC_PLUGIN_INFO_NAME:
336                         {
337                                 char *name = va_arg(ap, char *);
338                                 info->name = name;
339                         }
340                         break;
341                 case SC_PLUGIN_INFO_DESC:
342                         {
343                                 char *desc = va_arg(ap, char *);
344                                 info->description = desc;
345                         }
346                         break;
347                 case SC_PLUGIN_INFO_COPYRIGHT:
348                         {
349                                 char *copyright = va_arg(ap, char *);
350                                 info->copyright = copyright;
351                         }
352                         break;
353                 case SC_PLUGIN_INFO_LICENSE:
354                         {
355                                 char *license = va_arg(ap, char *);
356                                 info->license = license;
357                         }
358                         break;
359                 case SC_PLUGIN_INFO_VERSION:
360                         {
361                                 int version = va_arg(ap, int);
362                                 info->version = version;
363                         }
364                         break;
365                 case SC_PLUGIN_INFO_PLUGIN_VERSION:
366                         {
367                                 int version = va_arg(ap, int);
368                                 info->plugin_version = version;
369                         }
370                         break;
371                 default:
372                         va_end(ap);
373                         return -1;
374         }
376         va_end(ap);
377         return 0;
378 } /* sc_plugin_set_info */
380 int
381 sc_plugin_register_config(const char *name, sc_plugin_config_cb callback)
383         return sc_plugin_add_callback(&config_list, "init", name,
384                         callback, NULL);
385 } /* sc_plugin_register_config */
387 int
388 sc_plugin_register_init(const char *name, sc_plugin_init_cb callback,
389                 sc_object_t *user_data)
391         return sc_plugin_add_callback(&init_list, "init", name,
392                         callback, user_data);
393 } /* sc_plugin_register_init */
395 int
396 sc_plugin_register_shutdown(const char *name, sc_plugin_shutdown_cb callback,
397                 sc_object_t *user_data)
399         return sc_plugin_add_callback(&shutdown_list, "shutdown", name,
400                         callback, user_data);
401 } /* sc_plugin_register_shutdown */
403 int
404 sc_plugin_register_collector(const char *name, sc_plugin_collector_cb callback,
405                 const sc_time_t *interval, sc_object_t *user_data)
407         sc_object_t *obj;
409         if ((! name) || (! callback))
410                 return -1;
412         if (! collector_list)
413                 collector_list = sc_llist_create();
414         if (! collector_list)
415                 return -1;
417         obj = sc_object_create(sizeof(sc_plugin_collector_cb_t),
418                         sc_plugin_cb_init, sc_plugin_cb_destroy,
419                         &collector_list, "collector", name, callback, user_data);
420         if (! obj)
421                 return -1;
423         if (interval)
424                 SC_PLUGIN_CCB(obj)->ccb_interval = *interval;
425         else {
426                 sc_time_t tmp = sc_plugin_get_ctx().interval;
428                 if (tmp > 0)
429                         SC_PLUGIN_CCB(obj)->ccb_interval = tmp;
430                 else
431                         SC_PLUGIN_CCB(obj)->ccb_interval = 0;
432         }
434         if (! (SC_PLUGIN_CCB(obj)->ccb_next_update = sc_gettime())) {
435                 char errbuf[1024];
436                 fprintf(stderr, "plugin: Failed to determine current time: %s\n",
437                                 sc_strerror(errno, errbuf, sizeof(errbuf)));
438                 sc_object_deref(obj);
439                 return -1;
440         }
442         if (sc_llist_insert_sorted(collector_list, obj,
443                                 sc_plugin_cmp_next_update)) {
444                 sc_object_deref(obj);
445                 return -1;
446         }
448         /* pass control to the list */
449         sc_object_deref(obj);
451         fprintf(stderr, "plugin: Registered collector callback '%s' "
452                         "(interval = %.3fs).\n", name,
453                         SC_TIME_TO_DOUBLE(SC_PLUGIN_CCB(obj)->ccb_interval));
454         return 0;
455 } /* sc_plugin_register_collector */
457 sc_plugin_ctx_t
458 sc_plugin_get_ctx(void)
460         sc_plugin_ctx_t *ctx;
462         if (! plugin_ctx_key_initialized)
463                 sc_plugin_ctx_init();
464         ctx = pthread_getspecific(plugin_ctx_key);
466         if (! ctx)
467                 ctx = sc_plugin_ctx_create();
468         if (! ctx)
469                 return plugin_default_ctx;
470         return *ctx;
471 } /* sc_plugin_get_ctx */
473 sc_plugin_ctx_t
474 sc_plugin_set_ctx(sc_plugin_ctx_t ctx)
476         sc_plugin_ctx_t *tmp;
477         sc_plugin_ctx_t old;
479         if (! plugin_ctx_key_initialized)
480                 sc_plugin_ctx_init();
481         tmp = pthread_getspecific(plugin_ctx_key);
483         if (! tmp)
484                 tmp = sc_plugin_ctx_create();
485         if (! tmp)
486                 return plugin_default_ctx;
488         old = *tmp;
489         *tmp = ctx;
490         return old;
491 } /* sc_plugin_set_ctx */
493 int
494 sc_plugin_configure(const char *name, oconfig_item_t *ci)
496         sc_plugin_cb_t *plugin;
497         sc_plugin_config_cb callback;
499         sc_plugin_ctx_t old_ctx;
501         int status;
503         if ((! name) || (! ci))
504                 return -1;
506         plugin = sc_plugin_find_by_name(config_list, name);
507         if (! plugin) {
508                 fprintf(stderr, "plugin: Plugin '%s' did not register "
509                                 "a config callback.\n", name);
510                 errno = ENOENT;
511                 return -1;
512         }
514         old_ctx = sc_plugin_set_ctx(plugin->cb_ctx);
515         callback = plugin->cb_callback;
516         status = callback(ci);
517         sc_plugin_set_ctx(old_ctx);
518         return status;
519 } /* sc_plugin_configure */
521 int
522 sc_plugin_init_all(void)
524         sc_llist_iter_t *iter;
526         iter = sc_llist_get_iter(init_list);
527         while (sc_llist_iter_has_next(iter)) {
528                 sc_plugin_init_cb callback;
529                 sc_plugin_ctx_t old_ctx;
531                 sc_object_t *obj = sc_llist_iter_get_next(iter);
532                 assert(obj);
534                 callback = SC_PLUGIN_CB(obj)->cb_callback;
536                 old_ctx = sc_plugin_set_ctx(SC_PLUGIN_CB(obj)->cb_ctx);
537                 if (callback(SC_PLUGIN_CB(obj)->cb_user_data)) {
538                         /* XXX: unload plugin */
539                 }
540                 sc_plugin_set_ctx(old_ctx);
541         }
542         return 0;
543 } /* sc_plugin_init_all */
545 int
546 sc_plugin_collector_loop(sc_plugin_loop_t *loop)
548         if ((! collector_list) || (! loop))
549                 return -1;
551         while (loop->do_loop) {
552                 sc_plugin_collector_cb callback;
553                 sc_plugin_ctx_t old_ctx;
555                 sc_time_t interval, now;
557                 sc_object_t *obj = sc_llist_shift(collector_list);
558                 if (! obj)
559                         return -1;
561                 callback = SC_PLUGIN_CCB(obj)->ccb_callback;
563                 if (! (now = sc_gettime())) {
564                         char errbuf[1024];
565                         fprintf(stderr, "plugin: Failed to determine current time: %s\n",
566                                         sc_strerror(errno, errbuf, sizeof(errbuf)));
567                         now = SC_PLUGIN_CCB(obj)->ccb_next_update;
568                 }
570                 if (now < SC_PLUGIN_CCB(obj)->ccb_next_update) {
571                         interval = SC_PLUGIN_CCB(obj)->ccb_next_update - now;
573                         errno = 0;
574                         while (loop->do_loop && sc_sleep(interval, &interval)) {
575                                 if (errno != EINTR) {
576                                         char errbuf[1024];
577                                         fprintf(stderr, "plugin: Failed to sleep: %s\n",
578                                                         sc_strerror(errno, errbuf, sizeof(errbuf)));
579                                         return -1;
580                                 }
581                                 errno = 0;
582                         }
584                         if (! loop->do_loop)
585                                 return 0;
586                 }
588                 old_ctx = sc_plugin_set_ctx(SC_PLUGIN_CCB(obj)->ccb_ctx);
589                 if (callback(SC_PLUGIN_CCB(obj)->ccb_user_data)) {
590                         /* XXX */
591                 }
592                 sc_plugin_set_ctx(old_ctx);
594                 interval = SC_PLUGIN_CCB(obj)->ccb_interval;
595                 if (! interval)
596                         interval = loop->default_interval;
597                 if (! interval) {
598                         fprintf(stderr, "plugin: No interval configured "
599                                         "for plugin '%s'; skipping any further "
600                                         "iterations.\n", SC_PLUGIN_CCB(obj)->ccb_name);
601                         sc_object_deref(obj);
602                         continue;
603                 }
605                 SC_PLUGIN_CCB(obj)->ccb_next_update += interval;
607                 if (! (now = sc_gettime())) {
608                         char errbuf[1024];
609                         fprintf(stderr, "plugin: Failed to determine current time: %s\n",
610                                         sc_strerror(errno, errbuf, sizeof(errbuf)));
611                         now = SC_PLUGIN_CCB(obj)->ccb_next_update;
612                 }
614                 if (now > SC_PLUGIN_CCB(obj)->ccb_next_update) {
615                         fprintf(stderr, "plugin: Plugin '%s' took too long; "
616                                         "skipping iterations to keep up.\n",
617                                         SC_PLUGIN_CCB(obj)->ccb_name);
618                         SC_PLUGIN_CCB(obj)->ccb_next_update = now;
619                 }
621                 if (sc_llist_insert_sorted(collector_list, obj,
622                                         sc_plugin_cmp_next_update)) {
623                         fprintf(stderr, "plugin: Failed to re-insert "
624                                         "plugin '%s' into collector list.\n",
625                                         SC_PLUGIN_CCB(obj)->ccb_name);
626                         sc_object_deref(obj);
627                         return -1;
628                 }
630                 /* pass control back to the list */
631                 sc_object_deref(obj);
632         }
633         return 0;
634 } /* sc_plugin_read_loop */
636 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */