This repository was archived by the owner on Feb 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathreceivemessage.c
More file actions
165 lines (143 loc) · 3.98 KB
/
Copy pathreceivemessage.c
File metadata and controls
165 lines (143 loc) · 3.98 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
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include "dbusrecv.h"
#include <kdblogger.h>
#define RECEIVE_MATCH_RULE "type='signal',interface='org.libelektra',path='/org/libelektra/configuration'"
/**
* @internal
* Get and setup D-Bus connection.
*
* handlePointer is updated to point to D-Bus I/O adapter handle.
*
* @param type D-Bus bus type
* @param ioBinding I/O binding (optional)
* @param handlePointer Pointer to D-Bus I/O adapter handle
* @return D-Bus connection or NULL on error
*/
static DBusConnection * dbusGetConnection (DBusBusType type, ElektraIoInterface * ioBinding, ElektraIoAdapterDbusHandle ** handlePointer)
{
DBusError error;
dbus_error_init (&error);
DBusConnection * connection = dbus_bus_get_private (type, &error);
if (connection == NULL)
{
ELEKTRA_LOG_WARNING ("Failed to open connection to %s message bus: %s", (type == DBUS_BUS_SYSTEM) ? "system" : "session",
error.message);
dbus_error_free (&error);
return NULL;
}
dbus_error_free (&error);
dbus_connection_set_exit_on_disconnect (connection, FALSE);
ElektraIoAdapterDbusHandle * handle = elektraIoAdapterDbusAttach (connection, ioBinding);
if (!handle)
{
ELEKTRA_LOG_WARNING ("Failed to attach to the I/O binding");
return NULL;
}
*handlePointer = handle;
return connection;
}
/**
* @internal
* Revert changes made to D-Bus connection by elektraDbusRecvSetupReceive().
*
* @param handle Plugin handle
* @param connection D-bus bus type
* @param filter_func message handler
* @retval 1 on success
* @retval 0 on error
*/
int elektraDbusRecvTeardownReceive (Plugin * handle, DBusBusType type, DBusHandleMessageFunction filter_func)
{
ElektraDbusRecvPluginData * pluginData = elektraPluginGetData (handle);
DBusConnection * connection;
DBusError error;
switch (type)
{
case DBUS_BUS_SYSTEM:
if (!pluginData->systemBus)
{
pluginData->systemBus = dbusGetConnection (type, pluginData->ioBinding, &pluginData->systemBusAdapter);
}
connection = pluginData->systemBus;
break;
case DBUS_BUS_SESSION:
if (!pluginData->sessionBus)
{
pluginData->sessionBus = dbusGetConnection (type, pluginData->ioBinding, &pluginData->sessionBusAdapter);
}
connection = pluginData->sessionBus;
break;
default:
connection = NULL;
}
if (connection == NULL)
{
return 0;
}
dbus_error_init (&error);
dbus_bus_remove_match (connection, RECEIVE_MATCH_RULE, &error);
if (dbus_error_is_set (&error)) goto error;
dbus_connection_remove_filter (connection, filter_func, handle);
return 1;
error:
ELEKTRA_LOG_WARNING ("Error occurred\n");
dbus_error_free (&error);
return 0;
}
/**
* @internal
* Setup D-Bus connection for receiving Elektra's signal messages.
*
* @param handle Plugin handle
* @param connection D-Bus connection
* @param filter_func message handler
* @retval 1 on success
* @retval 0 on error
*/
int elektraDbusRecvSetupReceive (Plugin * handle, DBusBusType type, DBusHandleMessageFunction filter_func)
{
ElektraDbusRecvPluginData * pluginData = elektraPluginGetData (handle);
DBusConnection * connection;
DBusError error;
switch (type)
{
case DBUS_BUS_SYSTEM:
if (!pluginData->systemBus)
{
pluginData->systemBus = dbusGetConnection (type, pluginData->ioBinding, &pluginData->systemBusAdapter);
}
connection = pluginData->systemBus;
break;
case DBUS_BUS_SESSION:
if (!pluginData->sessionBus)
{
pluginData->sessionBus = dbusGetConnection (type, pluginData->ioBinding, &pluginData->sessionBusAdapter);
}
connection = pluginData->sessionBus;
break;
default:
connection = NULL;
}
if (connection == NULL)
{
return 0;
}
dbus_error_init (&error);
dbus_bus_add_match (connection, RECEIVE_MATCH_RULE, &error);
if (dbus_error_is_set (&error)) goto error;
if (!dbus_connection_add_filter (connection, filter_func, handle, NULL))
{
goto error;
}
return 1;
error:
ELEKTRA_LOG_WARNING ("Error occurred\n");
dbus_error_free (&error);
return 0;
}