Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief Test helpers for dbus plugin
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #include "dbus.h"
10 :
11 : #include <stdio.h>
12 :
13 : #define RECEIVE_MATCH_RULE "type='signal',interface='org.libelektra',path='/org/libelektra/configuration'"
14 :
15 : /**
16 : * @internal
17 : * Setup D-Bus connection for receiving Elektra's signal messages.
18 : *
19 : * @param connection D-Bus connection
20 : * @param filter_func message handler
21 : * @param data data passed to message handler
22 : * @retval 1 on success
23 : * @retval 0 on error
24 : */
25 0 : int elektraDbusSetupReceiveMessage (DBusConnection * connection, DBusHandleMessageFunction filter_func, void * data)
26 : {
27 0 : ELEKTRA_NOT_NULL (connection);
28 0 : ELEKTRA_NOT_NULL (filter_func);
29 :
30 : DBusError error;
31 0 : dbus_error_init (&error);
32 :
33 0 : dbus_bus_add_match (connection, RECEIVE_MATCH_RULE, &error);
34 0 : if (dbus_error_is_set (&error)) goto error;
35 :
36 0 : if (!dbus_connection_add_filter (connection, filter_func, data, NULL))
37 : {
38 : goto error;
39 : }
40 :
41 0 : dbus_error_free (&error);
42 0 : return 1;
43 :
44 : error:
45 0 : printf ("Error occurred\n");
46 0 : dbus_error_free (&error);
47 0 : return 0;
48 : }
49 :
50 : /**
51 : * @internal
52 : * Revert changes made to D-Bus connection by elektraDbusSetupReceiveMessage().
53 : *
54 : * @param connection D-Bus connection
55 : * @param filter_func message handler
56 : * @param data data passed to message handler
57 : * @retval 1 on success
58 : * @retval 0 on error
59 : */
60 0 : int elektraDbusTeardownReceiveMessage (DBusConnection * connection, DBusHandleMessageFunction filter_func, void * data)
61 : {
62 0 : ELEKTRA_NOT_NULL (connection);
63 0 : ELEKTRA_NOT_NULL (filter_func);
64 :
65 : DBusError error;
66 0 : dbus_error_init (&error);
67 :
68 0 : dbus_bus_remove_match (connection, RECEIVE_MATCH_RULE, &error);
69 0 : if (dbus_error_is_set (&error)) goto error;
70 :
71 0 : dbus_connection_remove_filter (connection, filter_func, data);
72 :
73 0 : dbus_error_free (&error);
74 0 : return 1;
75 :
76 : error:
77 0 : printf ("Error occurred\n");
78 0 : dbus_error_free (&error);
79 0 : return 0;
80 : }
81 :
82 : /**
83 : * Setup receiving of Elektra's D-Bus signal messages and do blocking dispatch.
84 : *
85 : * Messages are passed to filter_func
86 : *
87 : * @param type D-Bus bus type
88 : * @param filter_func message handler
89 : * @retval 0 on success
90 : * @retval -1 on error
91 : */
92 0 : int elektraDbusReceiveMessage (DBusBusType type, DBusHandleMessageFunction filter_func)
93 : {
94 0 : ELEKTRA_NOT_NULL (filter_func);
95 :
96 : DBusConnection * connection;
97 : DBusError error;
98 :
99 0 : dbus_error_init (&error);
100 0 : connection = dbus_bus_get (type, &error);
101 0 : if (connection == NULL)
102 : {
103 0 : fprintf (stderr, "Failed to open connection to %s message bus: %s\n", (type == DBUS_BUS_SYSTEM) ? "system" : "session",
104 : error.message);
105 0 : goto error;
106 : }
107 :
108 0 : int result = elektraDbusSetupReceiveMessage (connection, filter_func, NULL);
109 0 : if (!result) goto error;
110 :
111 0 : while (dbus_connection_read_write_dispatch (connection, -1))
112 : ;
113 : return 0;
114 : error:
115 0 : printf ("Error occurred\n");
116 0 : dbus_error_free (&error);
117 0 : return -1;
118 : }
|