forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbus.c
204 lines (175 loc) · 5.42 KB
/
dbus.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
/**
* @file
*
* @brief Source for dbus plugin
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#ifndef HAVE_KDBCONFIG
#include "kdbconfig.h"
#endif
#include "dbus.h"
#include <kdbhelper.h>
int elektraDbusOpen (Plugin * handle, Key * errorKey ELEKTRA_UNUSED)
{
ElektraDbusPluginData * data = elektraPluginGetData (handle);
if (!data)
{
data = elektraMalloc (sizeof (*data));
data->keys = NULL;
data->systemBus = NULL;
data->sessionBus = NULL;
}
elektraPluginSetData (handle, data);
return 1; /* success */
}
int elektraDbusGet (Plugin * handle, KeySet * returned, Key * parentKey)
{
if (!strcmp (keyName (parentKey), "system/elektra/modules/dbus"))
{
KeySet * contract =
ksNew (30, keyNew ("system/elektra/modules/dbus", KEY_VALUE, "dbus plugin waits for your orders", KEY_END),
keyNew ("system/elektra/modules/dbus/exports", KEY_END),
keyNew ("system/elektra/modules/dbus/exports/open", KEY_FUNC, elektraDbusOpen, KEY_END),
keyNew ("system/elektra/modules/dbus/exports/get", KEY_FUNC, elektraDbusGet, KEY_END),
keyNew ("system/elektra/modules/dbus/exports/set", KEY_FUNC, elektraDbusSet, KEY_END),
keyNew ("system/elektra/modules/dbus/exports/close", KEY_FUNC, elektraDbusClose, KEY_END),
#include ELEKTRA_README
keyNew ("system/elektra/modules/dbus/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END);
ksAppend (returned, contract);
ksDel (contract);
return 1; /* success */
}
// remember all keys
ElektraDbusPluginData * pluginData = elektraPluginGetData (handle);
ELEKTRA_NOT_NULL (pluginData);
KeySet * ks = pluginData->keys;
if (ks) ksDel (ks);
pluginData->keys = ksDup (returned);
return 1; /* success */
}
/**
* @internal
* Announce multiple keys with same signal name.
*
* @param ks key set containing modified keys
* @param signalName signal name to use
* @param busType D-Bus bus type
* @param data plugin data containing D-Bus connections, etc.
*/
static void announceKeys (KeySet * ks, const char * signalName, DBusBusType busType, ElektraDbusPluginData * data)
{
ELEKTRA_NOT_NULL (ks);
ELEKTRA_NOT_NULL (signalName);
ELEKTRA_NOT_NULL (data);
ksRewind (ks);
Key * k = 0;
while ((k = ksNext (ks)) != 0)
{
elektraDbusSendMessage (data, busType, keyName (k), signalName);
}
}
int elektraDbusSet (Plugin * handle, KeySet * returned, Key * parentKey)
{
ElektraDbusPluginData * pluginData = elektraPluginGetData (handle);
ELEKTRA_NOT_NULL (pluginData);
KeySet * oldKeys = pluginData->keys;
// because elektraLogchangeGet will always be executed before elektraLogchangeSet
// we know that oldKeys must exist here!
ksRewind (oldKeys);
ksRewind (returned);
KeySet * addedKeys = ksDup (returned);
KeySet * changedKeys = ksNew (0, KS_END);
KeySet * removedKeys = ksNew (0, KS_END);
Key * k = 0;
while ((k = ksNext (oldKeys)) != 0)
{
Key * p = ksLookup (addedKeys, k, KDB_O_POP);
// Note: keyDel not needed, because at least two references exist
if (p)
{
if (keyNeedSync (p))
{
ksAppendKey (changedKeys, p);
}
}
else
{
ksAppendKey (removedKeys, k);
}
}
Key * resolvedParentKey = parentKey;
// Resolve cascaded parent key to get its namespace
if (!strncmp (keyName (parentKey), "/", 1))
{
resolvedParentKey = ksLookup (returned, parentKey, 0);
}
int announceSession = 0;
int announceSystem = 0;
if (resolvedParentKey != NULL)
{
announceSession = !strncmp (keyName (resolvedParentKey), "user", 4);
announceSystem = !strncmp (keyName (resolvedParentKey), "system", 6);
}
if (!strncmp (keyString (ksLookupByName (elektraPluginGetConfig (handle), "/announce", 0)), "once", 4))
{
if (announceSession) elektraDbusSendMessage (pluginData, DBUS_BUS_SESSION, keyName (resolvedParentKey), "Commit");
if (announceSystem) elektraDbusSendMessage (pluginData, DBUS_BUS_SYSTEM, keyName (resolvedParentKey), "Commit");
}
else
{
if (announceSession)
{
announceKeys (addedKeys, "KeyAdded", DBUS_BUS_SESSION, pluginData);
announceKeys (changedKeys, "KeyChanged", DBUS_BUS_SESSION, pluginData);
announceKeys (removedKeys, "KeyDeleted", DBUS_BUS_SESSION, pluginData);
}
if (announceSystem)
{
announceKeys (addedKeys, "KeyAdded", DBUS_BUS_SYSTEM, pluginData);
announceKeys (changedKeys, "KeyChanged", DBUS_BUS_SYSTEM, pluginData);
announceKeys (removedKeys, "KeyDeleted", DBUS_BUS_SYSTEM, pluginData);
}
}
ksDel (oldKeys);
ksDel (addedKeys);
ksDel (changedKeys);
ksDel (removedKeys);
// for next invocation of elektraLogchangeSet, remember our current keyset
pluginData->keys = ksDup (returned);
return 1; /* success */
}
int elektraDbusClose (Plugin * handle, Key * parentKey ELEKTRA_UNUSED)
{
ElektraDbusPluginData * pluginData = elektraPluginGetData (handle);
if (pluginData == NULL)
{
return 1;
}
KeySet * ks = pluginData->keys;
if (ks) ksDel (ks);
if (pluginData->systemBus)
{
dbus_connection_unref (pluginData->systemBus);
pluginData->systemBus = NULL;
}
if (pluginData->sessionBus)
{
dbus_connection_unref (pluginData->sessionBus);
pluginData->sessionBus = NULL;
}
elektraFree (pluginData);
elektraPluginSetData (handle, NULL);
return 1; /* success */
}
Plugin * ELEKTRA_PLUGIN_EXPORT
{
// clang-format off
return elektraPluginExport("dbus",
ELEKTRA_PLUGIN_OPEN, &elektraDbusOpen,
ELEKTRA_PLUGIN_GET, &elektraDbusGet,
ELEKTRA_PLUGIN_SET, &elektraDbusSet,
ELEKTRA_PLUGIN_CLOSE, &elektraDbusClose,
ELEKTRA_PLUGIN_END);
}