This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
/
sendmessage.c
117 lines (101 loc) · 2.62 KB
/
sendmessage.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
/**
* @file
*
* @brief Source for dbus plugin
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#include "dbus.h"
#ifndef HAVE_KDBCONFIG
#include "kdbconfig.h"
#endif
#include <kdblogger.h>
/**
* @internal
* Get and setup D-Bus connection.
*
* @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)
{
DBusError error;
dbus_error_init (&error);
DBusConnection * connection = dbus_bus_get (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);
return connection;
}
/**
* @internal
* Send Elektra's signal message over D-Bus.
*
* @param pluginData Plugin data, stores D-Bus connection, I/O binding and more
* @param type D-Bus bus type
* @param keyName Key name to include in message
* @param signalName Signal name
* @retval 1 on success
* @retval -1 on error
*/
int elektraDbusSendMessage (ElektraDbusPluginData * pluginData, DBusBusType type, const char * keyName, const char * signalName)
{
DBusConnection * connection;
DBusMessage * message;
const char * dest = NULL; // to all receivers
const char * interface = "org.libelektra";
const char * path = "/org/libelektra/configuration";
switch (type)
{
case DBUS_BUS_SYSTEM:
if (!pluginData->systemBus)
{
pluginData->systemBus = dbusGetConnection (type);
}
connection = pluginData->systemBus;
break;
case DBUS_BUS_SESSION:
if (!pluginData->sessionBus)
{
pluginData->sessionBus = dbusGetConnection (type);
}
connection = pluginData->sessionBus;
break;
default:
connection = NULL;
}
if (connection == NULL)
{
return -1;
}
message = dbus_message_new_signal (path, interface, signalName);
if (message == NULL)
{
ELEKTRA_LOG_WARNING ("Couldn't allocate D-Bus message");
return -1;
}
if (dest && !dbus_message_set_destination (message, dest))
{
ELEKTRA_LOG_WARNING ("Not enough memory");
dbus_message_unref (message);
return -1;
}
if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &keyName, DBUS_TYPE_INVALID))
{
ELEKTRA_LOG_WARNING ("Couldn't add message argument");
dbus_message_unref (message);
return -1;
}
dbus_connection_send (connection, message, NULL);
dbus_message_unref (message);
return 1;
}