Code

Renamed the project to SysDB (System DataBase).
[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 "utils/time.h"
35 #include "liboconfig/oconfig.h"
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
41 typedef struct {
42         sdb_time_t interval;
43 } sdb_plugin_ctx_t;
44 #define SDB_PLUGIN_CTX_INIT { 0 }
46 struct sdb_plugin_info;
47 typedef struct sdb_plugin_info sdb_plugin_info_t;
49 /* this should be used in the header of a plugin to avoid
50  * missing prototype warnings/errors for the plugin init
51  * function */
52 #define SDB_PLUGIN_MAGIC \
53         int sdb_module_init(sdb_plugin_info_t *info);
55 typedef struct {
56         _Bool do_loop;
57         sdb_time_t default_interval;
58 } sdb_plugin_loop_t;
59 #define SDB_PLUGIN_LOOP_INIT { 1, 0 }
61 /*
62  * sdb_plugin_load:
63  * Load (any type of) plugin by loading the shared object file and calling the
64  * sdb_module_init function.
65  */
66 int
67 sdb_plugin_load(const char *name);
69 /*
70  * sdb_plugin_set_info:
71  * Fill in the fields of the sdb_plugin_info_t object passed to the
72  * sdb_module_init function. This information is used to identify the plugin
73  * and also to provide additional information to the user.
74  */
75 enum {
76         SDB_PLUGIN_INFO_NAME,          /* plugin name: string */
77         SDB_PLUGIN_INFO_DESC,          /* plugin description: string */
78         SDB_PLUGIN_INFO_COPYRIGHT,     /* plugin copyright: string */
79         SDB_PLUGIN_INFO_LICENSE,       /* plugin license: string */
80         SDB_PLUGIN_INFO_VERSION,       /* libsysdb version: integer */
81         SDB_PLUGIN_INFO_PLUGIN_VERSION /* plugin version: integer */
82 };
84 int
85 sdb_plugin_set_info(sdb_plugin_info_t *info, int type, ...);
87 /*
88  * plugin callback functions
89  */
91 typedef int (*sdb_plugin_config_cb)(oconfig_item_t *ci);
92 typedef int (*sdb_plugin_init_cb)(sdb_object_t *user_data);
93 typedef int (*sdb_plugin_collector_cb)(sdb_object_t *user_data);
94 typedef int (*sdb_plugin_shutdown_cb)(sdb_object_t *user_data);
96 /*
97  * sdb_plugin_register_config:
98  * Register a "config" function. This will be used to pass on the
99  * configuration for a plugin. The plugin has to make sure that the function
100  * can be called multiple times in order to process multiple <Plugin> blocks
101  * specified in the configuration file(s).
102  *
103  * Returns:
104  *  - 0 on success
105  *  - a negative value else
106  */
107 int
108 sdb_plugin_register_config(const char *name, sdb_plugin_config_cb callback);
110 /*
111  * sdb_plugin_register_init:
112  * Register an "init" function. All "init" functions will be called after
113  * finishing the config parsing and before starting any other work. The
114  * functions will be called in the same order as they have been registered,
115  * that is, functions of different plugins will be called in the same order as
116  * the appropriate "Load" statements in the config file.
117  *
118  * If the "init" function returns a non-zero value, *all* callbacks of the
119  * plugin will be unloaded.
120  *
121  * Arguments:
122  *  - user_data: If specified, this will be passed on to each call of the
123  *    callback. The function will take ownership of the object, that is,
124  *    increment the reference count by one. In case the caller does not longer
125  *    use the object for other purposes, it should thus deref it.
126  *
127  * Returns:
128  *  - 0 on success
129  *  - a negative value else
130  */
131 int
132 sdb_plugin_register_init(const char *name, sdb_plugin_init_cb callback,
133                 sdb_object_t *user_data);
135 /*
136  * sdb_plugin_register_collector:
137  * Register a "collector" function. This is where a backend is doing its main
138  * work. This function will be called whenever an update of a backend has been
139  * requested (either by regular interval or by user request). The backend
140  * should then query the appropriate data-source and submit all values to the
141  * core.
142  *
143  * Arguments:
144  *  - interval: Specifies the regular interval at which to update the backend.
145  *    If this is NULL, global settings will be used.
146  *  - user_data: If specified, this will be passed on to each call of the
147  *    callback. The function will take ownership of the object, that is,
148  *    increment the reference count by one. In case the caller does not longer
149  *    use the object for other purposes, it should thus deref it.
150  *
151  * Returns:
152  *  - 0 on success
153  *  - a negative value else
154  */
155 int
156 sdb_plugin_register_collector(const char *name,
157                 sdb_plugin_collector_cb callback,
158                 const sdb_time_t *interval, sdb_object_t *user_data);
160 /*
161  * sdb_plugin_register_shutdown:
162  * Register a "shutdown" function to be called after stopping all update
163  * processes and before shutting down the daemon.
164  *
165  * Arguments:
166  *  - user_data: If specified, this will be passed on to each call of the
167  *    callback. The function will take ownership of the object, that is,
168  *    increment the reference count by one. In case the caller does not longer
169  *    use the object for other purposes, it should thus deref it.
170  */
171 int
172 sdb_plugin_register_shutdown(const char *name,
173                 sdb_plugin_shutdown_cb callback,
174                 sdb_object_t *user_data);
176 /*
177  * sdb_plugin_get_ctx, sdb_plugin_set_ctx:
178  * The plugin context defines a set of settings that are available whenever a
179  * plugin has been called. It may be used to pass around various information
180  * between the different component of the library without having each and
181  * every plugin care about it.
182  */
183 sdb_plugin_ctx_t
184 sdb_plugin_get_ctx(void);
185 sdb_plugin_ctx_t
186 sdb_plugin_set_ctx(sdb_plugin_ctx_t ctx);
188 /*
189  * sdb_plugin_configure:
190  * Configure the plugin called 'name' (according to the registered config
191  * callback) using the config tree 'ci'.
192  *
193  * Returns:
194  *  - 0 on success
195  *  - a negative value else
196  */
197 int
198 sdb_plugin_configure(const char *name, oconfig_item_t *ci);
200 /*
201  * sdb_plugin_init_all:
202  * Initialize all plugins using their registered "init" function.
203  */
204 int
205 sdb_plugin_init_all(void);
207 /*
208  * sdb_plugin_collector_loop:
209  * Loop until loop->do_loop is false, calling the next collector function on
210  * each iteration and once its next update interval is passed.
211  *
212  * Returns:
213  *  - 0 on success
214  *  - a negative value else
215  */
216 int
217 sdb_plugin_collector_loop(sdb_plugin_loop_t *loop);
219 #ifdef __cplusplus
220 } /* extern "C" */
221 #endif
223 #endif /* ! SDB_CORE_PLUGIN_H */
225 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */