Code

core: Make the plugin directory configurable.
[sysdb.git] / src / include / core / plugin.h
1 /*
2  * SysDB - src/include/core/plugin.h
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 #ifndef SDB_CORE_PLUGIN_H
29 #define SDB_CORE_PLUGIN_H 1
31 #include "sysdb.h"
32 #include "core/object.h"
33 #include "core/time.h"
35 #include "liboconfig/oconfig.h"
37 #include <stdarg.h>
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
43 typedef struct {
44         sdb_time_t interval;
45 } sdb_plugin_ctx_t;
46 #define SDB_PLUGIN_CTX_INIT { 0 }
48 struct sdb_plugin_info;
49 typedef struct sdb_plugin_info sdb_plugin_info_t;
51 /* this should be used in the header of a plugin to avoid
52  * missing prototype warnings/errors for the plugin init
53  * function */
54 #define SDB_PLUGIN_MAGIC \
55         int sdb_module_init(sdb_plugin_info_t *info)
57 typedef struct {
58         _Bool do_loop;
59         sdb_time_t default_interval;
60 } sdb_plugin_loop_t;
61 #define SDB_PLUGIN_LOOP_INIT { 1, 0 }
63 /*
64  * sdb_plugin_load:
65  * Load (any type of) plugin by loading the shared object file and calling the
66  * sdb_module_init function. If specified, 'plugin_ctx' fine-tunes the
67  * behavior of the plugin. If specified, the plugin will be looked up in
68  * 'basedir', else it defaults to the package libdir.
69  */
70 int
71 sdb_plugin_load(const char *basedir, const char *name,
72                 const sdb_plugin_ctx_t *plugin_ctx);
74 /*
75  * sdb_plugin_set_info:
76  * Fill in the fields of the sdb_plugin_info_t object passed to the
77  * sdb_module_init function. This information is used to identify the plugin
78  * and also to provide additional information to the user.
79  */
80 enum {
81         SDB_PLUGIN_INFO_NAME,          /* plugin name: string */
82         SDB_PLUGIN_INFO_DESC,          /* plugin description: string */
83         SDB_PLUGIN_INFO_COPYRIGHT,     /* plugin copyright: string */
84         SDB_PLUGIN_INFO_LICENSE,       /* plugin license: string */
85         SDB_PLUGIN_INFO_VERSION,       /* libsysdb version: integer */
86         SDB_PLUGIN_INFO_PLUGIN_VERSION /* plugin version: integer */
87 };
89 int
90 sdb_plugin_set_info(sdb_plugin_info_t *info, int type, ...);
92 /*
93  * plugin callback functions:
94  * See the description of the respective register function for what arguments
95  * the callbacks expect.
96  */
98 typedef int (*sdb_plugin_config_cb)(oconfig_item_t *ci);
99 typedef int (*sdb_plugin_init_cb)(sdb_object_t *user_data);
100 typedef int (*sdb_plugin_collector_cb)(sdb_object_t *user_data);
101 typedef char *(*sdb_plugin_cname_cb)(const char *name,
102                 sdb_object_t *user_data);
103 typedef int (*sdb_plugin_shutdown_cb)(sdb_object_t *user_data);
104 typedef int (*sdb_plugin_log_cb)(int prio, const char *msg,
105                 sdb_object_t *user_data);
107 /*
108  * sdb_plugin_register_config:
109  * Register a "config" function. This will be used to pass on the
110  * configuration for a plugin. The plugin has to make sure that the function
111  * can be called multiple times in order to process multiple <Plugin> blocks
112  * specified in the configuration file(s).
113  *
114  * Returns:
115  *  - 0 on success
116  *  - a negative value else
117  */
118 int
119 sdb_plugin_register_config(const char *name, sdb_plugin_config_cb callback);
121 /*
122  * sdb_plugin_register_init:
123  * Register an "init" function. All "init" functions will be called after
124  * finishing the config parsing and before starting any other work. The
125  * functions will be called in the same order as they have been registered,
126  * that is, functions of different plugins will be called in the same order as
127  * the appropriate "Load" statements in the config file.
128  *
129  * If the "init" function returns a non-zero value, *all* callbacks of the
130  * plugin will be unloaded.
131  *
132  * Arguments:
133  *  - user_data: If specified, this will be passed on to each call of the
134  *    callback. The function will take ownership of the object, that is,
135  *    increment the reference count by one. In case the caller does not longer
136  *    use the object for other purposes, it should thus deref it.
137  *
138  * Returns:
139  *  - 0 on success
140  *  - a negative value else
141  */
142 int
143 sdb_plugin_register_init(const char *name, sdb_plugin_init_cb callback,
144                 sdb_object_t *user_data);
146 /*
147  * sdb_plugin_register_collector:
148  * Register a "collector" function. This is where a backend is doing its main
149  * work. This function will be called whenever an update of a backend has been
150  * requested (either by regular interval or by user request). The backend
151  * should then query the appropriate data-source and submit all values to the
152  * core.
153  *
154  * Arguments:
155  *  - interval: Specifies the regular interval at which to update the backend.
156  *    If this is NULL, global settings will be used.
157  *  - user_data: If specified, this will be passed on to each call of the
158  *    callback. The function will take ownership of the object, that is,
159  *    increment the reference count by one. In case the caller does not longer
160  *    use the object for other purposes, it should thus deref it.
161  *
162  * Returns:
163  *  - 0 on success
164  *  - a negative value else
165  */
166 int
167 sdb_plugin_register_collector(const char *name,
168                 sdb_plugin_collector_cb callback,
169                 const sdb_time_t *interval, sdb_object_t *user_data);
171 /*
172  * sdb_plugin_register_cname:
173  * Register a "cname" (canonicalize name) function. This type of function is
174  * called whenever a host is stored. It accepts the hostname and returns a
175  * canonicalized hostname which will then be used to actually store the host.
176  * If multiple such callbacks are registered, each one of them will be called
177  * in the order they have been registered, each one receiving the result of
178  * the previous callback. If the function returns NULL, the original hostname
179  * is not changed. Any other value has to be dynamically allocated. It will be
180  * freed later on by the core.
181  *
182  * Returns:
183  *  - 0 on success
184  *  - a negative value else
185  */
186 int
187 sdb_plugin_register_cname(const char *name, sdb_plugin_cname_cb callback,
188                 sdb_object_t *user_data);
190 /*
191  * sdb_plugin_register_shutdown:
192  * Register a "shutdown" function to be called after stopping all update
193  * processes and before shutting down the daemon.
194  *
195  * Arguments:
196  *  - user_data: If specified, this will be passed on to each call of the
197  *    callback. The function will take ownership of the object, that is,
198  *    increment the reference count by one. In case the caller does not longer
199  *    use the object for other purposes, it should thus deref it.
200  */
201 int
202 sdb_plugin_register_shutdown(const char *name,
203                 sdb_plugin_shutdown_cb callback,
204                 sdb_object_t *user_data);
206 /*
207  * sdb_plugin_register_log:
208  * Register a "log" function to be called whenever logging is to be done.
209  *
210  * Arguments:
211  *  - user_data: If specified, this will be passed on to each call of the
212  *    callback. The function will take ownership of the object, that is,
213  *    increment the reference count by one. In case the caller does not longer
214  *    use the object for other purposes, it should thus deref it.
215  */
216 int
217 sdb_plugin_register_log(const char *name, sdb_plugin_log_cb callback,
218                 sdb_object_t *user_data);
220 /*
221  * sdb_plugin_get_ctx, sdb_plugin_set_ctx:
222  * The plugin context defines a set of settings that are available whenever a
223  * plugin has been called. It may be used to pass around various information
224  * between the different component of the library without having each and
225  * every plugin care about it.
226  *
227  * If non-NULL, sdb_plugin_set_ctx stores the previous context in the location
228  * pointed to be 'old'.
229  */
230 sdb_plugin_ctx_t
231 sdb_plugin_get_ctx(void);
232 int
233 sdb_plugin_set_ctx(sdb_plugin_ctx_t ctx, sdb_plugin_ctx_t *old);
235 /*
236  * sdb_plugin_configure:
237  * Configure the plugin called 'name' (according to the registered config
238  * callback) using the config tree 'ci'.
239  *
240  * Returns:
241  *  - 0 on success
242  *  - a negative value else
243  */
244 int
245 sdb_plugin_configure(const char *name, oconfig_item_t *ci);
247 /*
248  * sdb_plugin_reconfigure_init, sdb_plugin_reconfigure_finish:
249  * Reconfigure all plugins. This happens in multiple steps: first, call
250  * sdb_plugin_reconfigure_init to deconfigure all plugins by calling their
251  * config callbacks with a NULL config tree and unregistering all callbacks.
252  * Then, sdb_plugin_configure and other functions may be used to provide the
253  * new configuration or load new plugins. For all plugins which were already
254  * loaded before, sdb_module_init will be called with a NULL argument when
255  * reloading them.
256  * Finally, sdb_plugin_reconfigure_finish will clean up leftover pieces, like
257  * unloading plugins which are no longer in use.
258  *
259  * Returns:
260  *  - 0 on success
261  *  - a negative value else
262  */
263 int
264 sdb_plugin_reconfigure_init(void);
265 int
266 sdb_plugin_reconfigure_finish(void);
268 /*
269  * sdb_plugin_init_all:
270  * Initialize all plugins using their registered "init" function.
271  *
272  * Returns:
273  * The number of failed initializations.
274  */
275 int
276 sdb_plugin_init_all(void);
278 /*
279  * sdb_plugin_shutdown_all:
280  * Shutdown all plugins using their registered "shutdown" function.
281  *
282  * Returns:
283  * The number of failed shutdowns.
284  */
285 int
286 sdb_plugin_shutdown_all(void);
288 /*
289  * sdb_plugin_collector_loop:
290  * Loop until loop->do_loop is false, calling the next collector function on
291  * each iteration and once its next update interval is passed.
292  *
293  * Returns:
294  *  - 0 on success
295  *  - a negative value else
296  */
297 int
298 sdb_plugin_collector_loop(sdb_plugin_loop_t *loop);
300 /*
301  * sdb_plugin_cname:
302  * Returns the canonicalized hostname. The given hostname argument has to
303  * point to dynamically allocated memory and might be freed by the function.
304  * The return value will also be dynamically allocated (but it might be
305  * unchanged) and has to be freed by the caller.
306  */
307 char *
308 sdb_plugin_cname(char *hostname);
310 /*
311  * sdb_plugin_log:
312  * Log the specified message using all registered log callbacks. The message
313  * will be logged with the specified priority.
314  */
315 int
316 sdb_plugin_log(int prio, const char *msg);
318 /*
319  * sdb_plugin_logf:
320  * Log a formatted message. See sdb_plugin_log for more information.
321  */
322 int
323 sdb_plugin_vlogf(int prio, const char *fmt, va_list ap);
324 int
325 sdb_plugin_logf(int prio, const char *fmt, ...);
327 #ifdef __cplusplus
328 } /* extern "C" */
329 #endif
331 #endif /* ! SDB_CORE_PLUGIN_H */
333 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */