forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.c
More file actions
275 lines (226 loc) · 9.28 KB
/
Copy pathprocess.c
File metadata and controls
275 lines (226 loc) · 9.28 KB
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
/**
* @file
*
* @brief Source for process plugin
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include "process.h"
#include <kdberrors.h>
#include <kdbhelper.h>
#include <kdbinvoke.h>
#include <kdbpluginprocess.h>
#include <stdio.h>
typedef struct
{
ElektraInvokeHandle * plugin;
Key * pluginName;
KeySet * pluginConfig;
} Process;
static int validPluginName (Key * pluginNameKey, Key * errorKey)
{
if (pluginNameKey == NULL)
{
ELEKTRA_ADD_INSTALLATION_WARNING (errorKey, "Missing plugin configuration parameter plugin=<name of plugin to be proxied>");
return 0;
}
// and this key should obviously contain the plugin's name, so check for any name
// furthermore by invoking process in a process we'd create a deadloop, check that too
const char * pluginName = keyString (pluginNameKey);
if (elektraStrCmp (pluginName, "(null)") == 0 || keyIsBinary (pluginNameKey))
{
ELEKTRA_ADD_VALIDATION_SEMANTIC_WARNINGF (errorKey, "Plugin configuration parameter plugin has an invalid value: %s",
pluginName);
return 0;
}
else if (elektraStrCmp (pluginName, "process") == 0)
{
ELEKTRA_ADD_INTERFACE_WARNING (errorKey, "Cannot proxy the process plugin itself");
return 0;
}
return 1;
}
static void cleanup (Process * process, Key * errorKey)
{
if (process->plugin) elektraInvokeClose (process->plugin, errorKey);
if (process->pluginName) keyDel (process->pluginName);
ksDel (process->pluginConfig);
elektraFree (process);
}
int elektraInvoke1Arg (ElektraInvokeHandle * handle, const char * elektraPluginFunctionName, 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 (*elektra1Arg) (Key *);
elektra1Arg func = *(elektra1Arg *) rawFunc;
return func (k);
}
static int isContractKey (Key * key)
{
return !elektraStrCmp (keyName (key), "system:/elektra/modules/process");
}
int elektraProcessOpen (Plugin * handle, Key * errorKey)
{
ElektraPluginProcess * pp = elektraPluginGetData (handle);
if (pp == NULL)
{
// process initialization
Process * process = elektraMalloc (sizeof (Process));
KeySet * processConfig = elektraPluginGetConfig (handle);
process->pluginName = ksLookupByName (processConfig, "/plugin", KDB_O_POP);
process->pluginConfig = ksDup (processConfig);
ksAppendKey (processConfig, process->pluginName);
process->plugin = NULL;
if ((pp = elektraPluginProcessInit (errorKey)) == NULL) return ELEKTRA_PLUGIN_STATUS_ERROR;
elektraPluginProcessSetData (pp, process);
elektraPluginSetData (handle, pp);
if (!elektraPluginProcessIsParent (pp)) elektraPluginProcessStart (handle, pp);
}
if (elektraPluginProcessIsParent (pp)) return elektraPluginProcessOpen (pp, errorKey);
Process * process = elektraPluginProcessGetData (pp);
// First time child initialization
// elektraInvokeOpen will call the plugin's open function, this has to happen in the other process
if (process->plugin == NULL && !isContractKey (errorKey) && validPluginName (process->pluginName, errorKey))
{
process->plugin = elektraInvokeOpen (keyString (process->pluginName), process->pluginConfig, errorKey);
if (!process->plugin)
{
ELEKTRA_SET_RESOURCE_ERRORF (errorKey, "Failed to open the proxied plugin %s", keyString (process->pluginName));
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
if (process->plugin == NULL) return ELEKTRA_PLUGIN_STATUS_SUCCESS;
// Otherwise just call the open function if it exists and don't reinitialize with elektraInvokeOpen
int ret = elektraInvoke1Arg (process->plugin, "open", errorKey);
if (ret == -2) return ELEKTRA_PLUGIN_STATUS_SUCCESS;
return ret;
}
int elektraProcessClose (Plugin * handle, Key * errorKey)
{
ElektraPluginProcess * pp = elektraPluginGetData (handle);
if (!pp) return ELEKTRA_PLUGIN_STATUS_SUCCESS;
Process * process = elektraPluginProcessGetData (pp);
if (elektraPluginProcessIsParent (pp))
{
ElektraPluginProcessCloseResult result = elektraPluginProcessClose (pp, errorKey);
if (result.cleanedUp)
{
cleanup (process, errorKey);
elektraPluginSetData (handle, NULL);
}
return result.result;
}
// Handle may be null if initialization failed in the child, ignore that it will exit anyway afterwards
if (process->plugin == NULL) return ELEKTRA_PLUGIN_STATUS_SUCCESS;
int ret = elektraInvoke1Arg (process->plugin, "close", errorKey);
if (ret == -2) return ELEKTRA_PLUGIN_STATUS_SUCCESS;
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
static void adjustContract (KeySet * pluginContract, KeySet * contract)
{
ksRewind (pluginContract);
Key * cur;
while ((cur = ksNext (pluginContract)) != NULL)
{
Key * cpy = keyDup (cur, KEY_CP_ALL);
keySetBaseName (cpy, NULL);
if (!elektraStrCmp ("infos", keyBaseName (cpy)))
{
keySetBaseName (cpy, NULL);
keySetBaseName (cpy, NULL);
keyAddBaseName (cpy, "process");
keyAddBaseName (cpy, "infos");
keyAddBaseName (cpy, keyBaseName (cur));
Key * infoKey = ksLookup (contract, cpy, KDB_O_NONE);
keySetString (infoKey, keyString (cpy));
}
keyDel (cpy);
}
}
int elektraProcessGet (Plugin * handle, KeySet * returned, Key * parentKey)
{
ElektraPluginProcess * pp = elektraPluginGetData (handle);
Process * process = elektraPluginProcessGetData (pp);
if (elektraPluginProcessIsParent (pp)) return elektraPluginProcessSend (pp, ELEKTRA_PLUGINPROCESS_GET, returned, parentKey);
if (isContractKey (parentKey))
{
KeySet * processConfig = elektraPluginGetConfig (handle);
Key * pluginName = ksLookupByName (processConfig, "/plugin", KDB_O_NONE);
KeySet * contract =
ksNew (30, keyNew ("system:/elektra/modules/process", KEY_VALUE, "process plugin waits for your orders", KEY_END),
keyNew ("system:/elektra/modules/process/exports", KEY_END),
keyNew ("system:/elektra/modules/process/exports/open", KEY_FUNC, elektraProcessOpen, KEY_END),
keyNew ("system:/elektra/modules/process/exports/close", KEY_FUNC, elektraProcessClose, KEY_END),
keyNew ("system:/elektra/modules/process/exports/get", KEY_FUNC, elektraProcessGet, KEY_END),
keyNew ("system:/elektra/modules/process/exports/set", KEY_FUNC, elektraProcessSet, KEY_END),
keyNew ("system:/elektra/modules/process/exports/error", KEY_FUNC, elektraProcessError, KEY_END),
keyNew ("system:/elektra/modules/process/exports/checkconf", KEY_FUNC, elektraProcessCheckConf, KEY_END),
#include ELEKTRA_README
keyNew ("system:/elektra/modules/process/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END);
ksAppend (returned, contract);
ksDel (contract);
if (!validPluginName (pluginName, parentKey) || !process->plugin) return ELEKTRA_PLUGIN_STATUS_SUCCESS;
Key * pluginParentKey = keyDup (parentKey, KEY_CP_ALL);
keySetBaseName (pluginParentKey, keyString (pluginName));
KeySet * pluginContract = ksNew (30, KS_END);
elektraInvoke2Args (process->plugin, "get", pluginContract, pluginParentKey);
keyDel (pluginParentKey);
if (ksGetSize (pluginContract) == 0)
{
ELEKTRA_SET_INTERFACE_ERRORF (parentKey, "Failed to get the contract for %s", keyString (pluginName));
ksDel (pluginContract);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
adjustContract (pluginContract, returned);
ksDel (pluginContract);
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
if (!process->plugin) return ELEKTRA_PLUGIN_STATUS_SUCCESS;
int ret = elektraInvoke2Args (process->plugin, "get", returned, parentKey);
if (ret == -2) return ELEKTRA_PLUGIN_STATUS_NO_UPDATE;
return ret;
}
int elektraProcessSet (Plugin * handle, KeySet * returned, Key * parentKey)
{
ElektraPluginProcess * pp = elektraPluginGetData (handle);
Process * process = elektraPluginProcessGetData (pp);
if (elektraPluginProcessIsParent (pp)) return elektraPluginProcessSend (pp, ELEKTRA_PLUGINPROCESS_SET, returned, parentKey);
if (!process->plugin) return ELEKTRA_PLUGIN_STATUS_SUCCESS;
int ret = elektraInvoke2Args (process->plugin, "set", returned, parentKey);
if (ret == -2) return ELEKTRA_PLUGIN_STATUS_NO_UPDATE;
return ret;
}
int elektraProcessError (Plugin * handle, KeySet * returned, Key * parentKey)
{
ElektraPluginProcess * pp = elektraPluginGetData (handle);
Process * process = elektraPluginProcessGetData (pp);
if (elektraPluginProcessIsParent (pp)) return elektraPluginProcessSend (pp, ELEKTRA_PLUGINPROCESS_ERROR, returned, parentKey);
if (!process->plugin) return ELEKTRA_PLUGIN_STATUS_SUCCESS;
int ret = elektraInvoke2Args (process->plugin, "error", returned, parentKey);
if (ret == -2) return ELEKTRA_PLUGIN_STATUS_SUCCESS;
return ret;
}
int elektraProcessCheckConf (Key * errorKey, KeySet * conf)
{
// We need the plugin key to know which plugin we should proxy
Key * pluginNameKey = ksLookupByName (conf, "/plugin", KDB_O_NONE);
if (!validPluginName (pluginNameKey, errorKey))
{
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
return ELEKTRA_PLUGIN_STATUS_NO_UPDATE;
}
Plugin * ELEKTRA_PLUGIN_EXPORT
{
// clang-format off
return elektraPluginExport ("process",
ELEKTRA_PLUGIN_OPEN, &elektraProcessOpen,
ELEKTRA_PLUGIN_CLOSE, &elektraProcessClose,
ELEKTRA_PLUGIN_GET, &elektraProcessGet,
ELEKTRA_PLUGIN_SET, &elektraProcessSet,
ELEKTRA_PLUGIN_ERROR, &elektraProcessError,
ELEKTRA_PLUGIN_END);
}