forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.c
199 lines (165 loc) · 4.52 KB
/
plugin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**
* @file
*
* @brief Interna of plugin functionality.
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#ifdef HAVE_KDBCONFIG_H
#include "kdbconfig.h"
#endif
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STDARG_H
#include <stdarg.h>
#endif
#ifdef HAVE_CTYPE_H
#include <ctype.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include <kdbassert.h>
#include <kdberrors.h>
#include <kdbinternal.h>
#include <kdbversion.h>
/**
* Opens a plugin.
*
* The config will be used as is. So be sure to transfer ownership
* of the config to it, with e.g. ksDup().
* elektraPluginClose() will delete the config.
*
* @return a pointer to a new created plugin or 0 on error
*/
Plugin * elektraPluginOpen (const char * name, KeySet * modules, KeySet * config, Key * errorKey)
{
Plugin * handle = 0;
const char * n;
// TODO [new_backend]: change elektraPluginFactory to return contract KeySet instead of half-initialized Plugin*
elektraPluginFactory pluginFactory = 0;
if (!name || name[0] == '\0')
{
ELEKTRA_ADD_INSTALLATION_WARNING (errorKey, "Not a valid name supplied for a plugin: name is null or empty");
goto err_clup;
}
n = name;
while (*n != '\0')
{
if (*n == '/')
++n;
else
break;
}
if (*n == '\0')
{
ELEKTRA_ADD_INSTALLATION_WARNING (errorKey, "Not a valid name supplied for a plugin: name contained slashes only");
goto err_clup;
}
pluginFactory = elektraModulesLoad (modules, name, errorKey);
if (pluginFactory == 0)
{
/* warning already set by elektraModulesLoad */
goto err_clup;
}
handle = pluginFactory ();
if (handle == 0)
{
ELEKTRA_ADD_INSTALLATION_WARNINGF (errorKey, "Could not call function exported by ELEKTRA_PLUGIN_EXPORT: %s", name);
goto err_clup;
}
/* init reference counting */
handle->refcounter = 1;
handle->config = config;
handle->modules = modules; // to enable the modules keyset to be used within plugins as well
config = 0; // for err_clup case
/* let the plugin initialize itself */
if (handle->kdbOpen)
{
if ((handle->kdbOpen (handle, errorKey)) == -1)
{
ELEKTRA_ADD_PLUGIN_MISBEHAVIOR_WARNINGF (
errorKey,
"Open of plugin returned unsuccessfully: %s. Reason contains plugin, see other warnings for details", name);
elektraPluginClose (handle, errorKey);
goto err_clup;
}
}
ELEKTRA_LOG_DEBUG ("Finished loading plugin %s", name);
return handle;
err_clup:
ELEKTRA_LOG ("Failed to load plugin %s\n", name);
ksDel (config);
return 0;
}
int elektraPluginClose (Plugin * handle, Key * errorKey)
{
int rc = 0;
if (!handle) return 0;
--handle->refcounter;
/* Check if we have the last reference on the plugin (unsigned!) */
if (handle->refcounter > 0) return 0;
if (handle->kdbClose)
{
rc = handle->kdbClose (handle, errorKey);
if (rc == -1) ELEKTRA_ADD_RESOURCE_WARNING (errorKey, "Method 'kdbClose()' failed");
}
ksDel (handle->config);
elektraFree (handle);
return rc;
}
/**
* Retrieves a function exported by a plugin.
*
* @param plugin Plugin handle
* @param name Function name. Must be a valid key name suffix. May not contain the sequence '..'
* @return Pointer to function. NULL if function not found or not enough memory available
*/
size_t elektraPluginGetFunction (Plugin * plugin, const char * name)
{
ELEKTRA_NOT_NULL (plugin);
ELEKTRA_NOT_NULL (name);
if (strstr (name, "..") != NULL)
{
// The sequence ".." is contained in the name.
// For security and stability purposes we do not allow that.
ELEKTRA_LOG_WARNING ("Can't get function '%s' from plugin because '..' is not allowed in function name", name);
return 0;
}
KeySet * exports = ksNew (0, KS_END);
Key * pk = keyNew ("system:/elektra/modules", KEY_END);
keyAddBaseName (pk, plugin->name);
plugin->kdbGet (plugin, exports, pk);
ksRewind (exports);
keyAddBaseName (pk, "exports");
keyAddName (pk, name);
Key * keyFunction = ksLookup (exports, pk, 0);
if (!keyFunction)
{
ELEKTRA_LOG_DEBUG ("function \"%s\" from plugin \"%s\" not found", name, plugin->name);
ksDel (exports);
keyDel (pk);
return 0;
}
size_t * buffer;
size_t bufferSize = keyGetValueSize (keyFunction);
buffer = elektraMalloc (bufferSize);
if (buffer)
{
int result = keyGetBinary (keyFunction, buffer, bufferSize);
if (result == -1 || buffer == NULL)
{
ELEKTRA_LOG_WARNING ("could not get function \"%s\" from plugin \"%s\"", name, plugin->name);
return 0;
}
}
size_t func = *buffer;
elektraFree (buffer);
ksDel (exports);
keyDel (pk);
return func;
}