forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
invoke.c
541 lines (498 loc) · 12.5 KB
/
invoke.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/**
* @file
*
* @brief Library for invoking exported plugin functions
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include <kdbassert.h>
#include <kdbinvoke.h>
#include <kdbmodule.h>
#include <kdbprivate.h> // for elektraPluginOpen/Close
#include <stdio.h>
struct _ElektraInvokeHandle
{
Plugin * plugin;
KeySet * modules;
KeySet * exports;
};
/**
* Structure for deferred calls
* @internal
*/
typedef struct _ElektraDeferredCall
{
char * name;
KeySet * parameters;
struct _ElektraDeferredCall * next;
} _ElektraDeferredCall;
/**
* Structure for internal plugin state
* @internal
*/
struct _ElektraDeferredCallList
{
_ElektraDeferredCall * head;
_ElektraDeferredCall * last;
};
/**
* @defgroup invoke
* @brief Functionality to use plugins and invoke functions
*
* Allows invoking functions of plugins as needed within applications
* and plugins inside and outside of the KDB.
*
* To use this library, you need to include:
*
* @code
* #include <kdbinvoke.h>`
* @endcode
*
* and link against `libelektra-invoke`.
* Then you can use it:
*
* @code
* ElektraInvokeHandle * handle = elektraInvokeOpen ("dini", 0);
* elektraInvoke2Args (handle, "get", ks, k);
* elektraInvokeClose (handle);
* @endcode
*
* @{
*
*/
/**
* @deprecated Do not use.
*
* Use `elektraInvokeOpen (name, 0, 0)` instead.
*
* @see elektraInvokeOpen()
*/
ElektraInvokeHandle * elektraInvokeInitialize (const char * elektraPluginName)
{
return elektraInvokeOpen (elektraPluginName, 0, 0);
}
/**
* @brief Opens a new handle to invoke functions for a plugin.
*
* When opening the plugin, it calls the "open" function of the plugin.
*
* @param elektraPluginName the plugin on which we want to invoke functions.
* @param config the config to be passed to the plugin.
* @param errorKey a key where error messages will be stored
*
* @return the handle
* @retval 0 on errors
*/
ElektraInvokeHandle * elektraInvokeOpen (const char * elektraPluginName, KeySet * config, Key * errorKey)
{
if (!elektraPluginName)
{
return NULL;
}
ElektraInvokeHandle * handle = elektraCalloc (sizeof (struct _ElektraInvokeHandle));
if (!handle)
{
return NULL;
}
KeySet * modules = ksNew (0, KS_END);
handle->modules = modules;
elektraModulesInit (modules, NULL);
if (!config)
{
config = ksNew (0, KS_END);
}
else
{
config = ksDup (config);
}
int errorKeyMissing = !errorKey;
if (errorKeyMissing)
{
errorKey = keyNew ("/", KEY_END);
}
Plugin * plugin = elektraPluginOpen (elektraPluginName, modules, config, errorKey);
if (errorKeyMissing)
{
keyDel (errorKey);
}
if (!plugin)
{
elektraModulesClose (modules, NULL);
ksDel (modules);
elektraFree (handle);
return NULL;
}
handle->plugin = plugin;
return handle;
}
/**
* @brief Get a function pointer.
*
* @param handle the handle to use
* @param elektraPluginFunctionName the name of the function to use, e.g., get/set
*
* @pre handle must be as returned from elektraInvokeOpen()
*
* Example:
*
* @code
typedef int (*elektra2Args) (KeySet*, Key *);
elektra2Args func = *(elektra2Args *)elektraInvokeGetFunction (handle, elektraPluginFunctionName);
if (!func) exit(1); // no function found, handle error
func (ks, k); // otherwise, call function
* @endcode
*
* @see elektraInvoke2Args() a convenience function to be used for KeySet,Key arguments.
*
* @return a function pointer for the specified function.
*/
const void * elektraInvokeGetFunction (ElektraInvokeHandle * handle, const char * elektraPluginFunctionName)
{
if (!handle || !elektraPluginFunctionName)
{
return NULL;
}
Plugin * plugin = handle->plugin;
KeySet * exports = NULL;
Key * exportParent = keyNew ("system:/elektra/modules", KEY_END);
keyAddBaseName (exportParent, plugin->name);
if (handle->exports)
{
exports = handle->exports;
}
else
{
exports = ksNew (0, KS_END);
handle->exports = exports;
plugin->kdbGet (plugin, exports, exportParent);
}
keyAddBaseName (exportParent, "exports");
keyAddBaseName (exportParent, elektraPluginFunctionName);
Key * functionKey = ksLookup (exports, exportParent, 0);
keyDel (exportParent);
if (!functionKey)
{
return NULL;
}
else
{
return keyValue (functionKey);
}
}
/**
* @brief Get the configuration the plugin uses.
*
* @param handle the handle to use
*
* @pre handle must be as returned from elektraInvokeOpen()
*
* @return the config of the plugin.
*/
KeySet * elektraInvokeGetPluginConfig (ElektraInvokeHandle * handle)
{
if (!handle) return NULL;
return handle->plugin->config;
}
/**
* @brief Get the name of the plugin.
*
* The name might differ from the name as passed with elektraInvokeOpen
* when symlinks are used.
*
* @param handle the handle to work with.
*
* @pre handle must be as returned from elektraInvokeOpen()
*
* @return the name of the plugin
*/
const char * elektraInvokeGetPluginName (ElektraInvokeHandle * handle)
{
if (!handle) return NULL;
return handle->plugin->name;
}
/**
* @brief Get the data of the plugin.
*
* @param handle the handle to work with.
*
* @pre handle must be as returned from elektraInvokeOpen()
*
* @return a pointer to the plugin's data.
*/
void * elektraInvokeGetPluginData (ElektraInvokeHandle * handle)
{
if (!handle) return NULL;
return handle->plugin->data;
}
/**
* @brief Get the modules used for invoking.
*
* @warning The modules are closed within elektraInvokeClose().
* It is *not* enough to ksDup() the keyset, you must not call
* elektraInvokeClose() if you want to reuse the modules.
*
* @param handle the handle to work with.
*
* @pre handle must be as returned from elektraInvokeOpen()
*
* @return the modules used for invoking.
*/
KeySet * elektraInvokeGetModules (ElektraInvokeHandle * handle)
{
if (!handle) return NULL;
return handle->modules;
}
/**
* @brief Get the exports from the plugin.
*
* @param handle the handle to work with.
*
* @pre handle must be as returned from elektraInvokeOpen()
*
* @return the exports of the plugin.
*/
KeySet * elektraInvokeGetExports (ElektraInvokeHandle * handle)
{
if (!handle) return NULL;
return handle->exports;
}
/**
* @brief A convenience function to call a function with two arguments.
*
* @param handle the handle to work with
* @param elektraPluginFunctionName the function to call, e.g. "get"
* @param ks the keyset to be used as first parameter
* @param k the key to be used as second parameter
*
* @pre handle must be as returned from elektraInvokeOpen()
*
* @return the return value of the invoked function (i.e. -1, 0, or 1)
* @retval -2 if the function was not found.
*/
int elektraInvoke2Args (ElektraInvokeHandle * handle, const char * elektraPluginFunctionName, KeySet * ks, Key * k)
{
if (!handle || !elektraPluginFunctionName) return -2;
// If we cast this right away although the function wasn't found it will cause a deadlock
const void * rawFunc = elektraInvokeGetFunction (handle, elektraPluginFunctionName);
if (!rawFunc) return -2;
typedef int (*elektra2Args) (Plugin *, KeySet *, Key *);
elektra2Args func = *(elektra2Args *) rawFunc;
return func (handle->plugin, ks, k);
}
/**
* @brief Closes all affairs with the handle.
*
* The close function of the plugin will be called.
*
* @param handle the handle to work with
* @param errorKey a key where error messages will be stored
*
* @pre handle must be as returned from elektraInvokeOpen()
*/
void elektraInvokeClose (ElektraInvokeHandle * handle, Key * errorKey)
{
if (!handle)
{
return;
}
int errorKeyMissing = !errorKey;
if (errorKeyMissing)
{
errorKey = keyNew ("/", KEY_END);
}
elektraPluginClose (handle->plugin, errorKey);
if (errorKeyMissing)
{
keyDel (errorKey);
}
elektraModulesClose (handle->modules, NULL);
ksDel (handle->modules);
ksDel (handle->exports);
elektraFree (handle);
}
/**
* Invokes a deferable function on an invoke handle.
* If the function is exported by the plugin it is directly invoked,
* if the plugin supports deferring calls, the call is deferred.
*
* The parameters key set can be freed afterwards.
*
* @param handle invoke handle
* @param elektraPluginFunctionName function name
* @param parameters parameter key set
* @retval 0 on success
* @retval -1 when the call failed (direct call and deferring not available)
*/
int elektraInvokeCallDeferable (ElektraInvokeHandle * handle, const char * elektraPluginFunctionName, KeySet * parameters)
{
if (!handle)
{
return -1;
}
return elektraDeferredCall (handle->plugin, elektraPluginFunctionName, parameters);
}
/**
* Execute deferred calls from list on given invoke handle.
*
* Used internally by plugins holding invoke handles.
*
* @param handle invoke handle
* @param list list
*/
void elektraInvokeExecuteDeferredCalls (ElektraInvokeHandle * handle, ElektraDeferredCallList * list)
{
if (!handle)
{
return;
}
elektraDeferredCallsExecute (handle->plugin, list);
}
/**
* Call a deferrable function on a plugin handle.
* If the function is exported by the plugin it is directly invoked,
* if the plugin supports deferring calls, the call is deferred.
* If both is possible (function is exported and deferred calls are supported),
* the function is directly called and the call is deferred (i.e. for nested plugins).
*
* @param handle invoke handle
* @param elektraPluginFunctionName function name
* @param parameters parameter key set. Can bee freed afterwards.
* @retval 0 on success
* @retval -1 when the call failed (direct call and deferring not available)
*/
int elektraDeferredCall (Plugin * handle, const char * elektraPluginFunctionName, KeySet * parameters)
{
ELEKTRA_NOT_NULL (handle);
ELEKTRA_NOT_NULL (elektraPluginFunctionName);
int result;
size_t direct = elektraPluginGetFunction (handle, elektraPluginFunctionName);
if (direct)
{
ElektraDeferredCallable directFn = (ElektraDeferredCallable) direct;
directFn (handle, parameters);
result = 0; // success
}
else
{
// no direct call possible
result = -1;
}
size_t deferredCall = elektraPluginGetFunction (handle, "deferredCall");
if (deferredCall)
{
ElektraDeferredCall deferredCallFn = (ElektraDeferredCall) deferredCall;
deferredCallFn (handle, elektraPluginFunctionName, parameters);
result = 0; // success
}
else
{
// deferred calls not possible
result = -1;
}
return result;
}
/**
* Add a new deferred call to the deferred call list.
*
* Used internally by plugins.
*
* @param list deferred call list
* @param name function name
* @param parameters function parameters
* @retval 1 on success
* @retval 0 when malloc failed
*/
int elektraDeferredCallAdd (ElektraDeferredCallList * list, const char * name, KeySet * parameters)
{
ELEKTRA_NOT_NULL (list);
ELEKTRA_NOT_NULL (name);
_ElektraDeferredCall * item = elektraMalloc (sizeof *item);
if (item == NULL)
{
return 0;
}
item->name = elektraStrDup (name);
item->parameters = ksDup (parameters);
item->next = NULL;
if (list->head == NULL)
{
// Initialize list
list->head = list->last = item;
}
else
{
// Make new item end of list
list->last->next = item;
list->last = item;
}
return 1;
}
/**
* Create new deferred call list.
*
* The list needs to be deleted with elektraDeferredCallDeleteList().
* Used internally by plugins.
*
* @return new list
*/
ElektraDeferredCallList * elektraDeferredCallCreateList (void)
{
ElektraDeferredCallList * list = elektraMalloc (sizeof *list);
if (list == NULL)
{
return NULL;
}
list->head = NULL;
list->last = NULL;
return list;
}
/**
* Delete deferred call list.
*
* Used internally by plugins.
*
* @param list list
*/
void elektraDeferredCallDeleteList (ElektraDeferredCallList * list)
{
ELEKTRA_NOT_NULL (list);
_ElektraDeferredCall * item = list->head;
while (item != NULL)
{
elektraFree (item->name);
ksDel (item->parameters);
_ElektraDeferredCall * next = item->next;
elektraFree (item);
item = next;
}
elektraFree (list);
}
/**
* Execute deferred calls on given plugin.
*
* Used internally by plugins.
*
* @param plugin plugin handle
* @param list list
*/
void elektraDeferredCallsExecute (Plugin * plugin, ElektraDeferredCallList * list)
{
ELEKTRA_NOT_NULL (plugin);
ELEKTRA_NOT_NULL (list);
_ElektraDeferredCall * item = list->head;
while (item != NULL)
{
size_t func = elektraPluginGetFunction (plugin, item->name);
if (!func)
{
item = item->next;
continue;
}
ElektraDeferredCallable callable = (ElektraDeferredCallable) func;
callable (plugin, item->parameters);
item = item->next;
}
}
/**
* @}
*/