/
dbus.c
563 lines (453 loc) · 16.9 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#include <crm_internal.h>
#include <crm/crm.h>
#include <crm/services.h>
#include <dbus/dbus.h>
#include <pcmk-dbus.h>
#define BUS_PROPERTY_IFACE "org.freedesktop.DBus.Properties"
struct db_getall_data
{
char *name;
char *target;
char *object;
void *userdata;
void (*callback)(const char *name, const char *value, void *userdata);
};
static bool pcmk_dbus_error_check(DBusError *err, const char *prefix, const char *function, int line)
{
if (err && dbus_error_is_set(err)) {
do_crm_log_alias(LOG_ERR, __FILE__, function, line, "%s: DBus error '%s'", prefix, err->message);
dbus_error_free(err);
return TRUE;
}
return FALSE;
}
DBusConnection *pcmk_dbus_connect(void)
{
DBusError err;
DBusConnection *connection;
dbus_error_init(&err);
connection = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
if(pcmk_dbus_error_check(&err, "Could not connect to System DBus", __FUNCTION__, __LINE__)) {
return NULL;
}
if(connection) {
pcmk_dbus_connection_setup_with_select(connection);
}
return connection;
}
void pcmk_dbus_disconnect(DBusConnection *connection)
{
}
bool
pcmk_dbus_find_error(const char *method, DBusPendingCall* pending, DBusMessage *reply, DBusError *ret)
{
DBusError error;
dbus_error_init(&error);
if(pending == NULL) {
error.name = "org.clusterlabs.pacemaker.NoRequest";
error.message = "No request sent";
} else if(reply == NULL) {
error.name = "org.clusterlabs.pacemaker.NoReply";
error.message = "No reply";
} else {
DBusMessageIter args;
int dtype = dbus_message_get_type(reply);
char *sig;
switch(dtype) {
case DBUS_MESSAGE_TYPE_METHOD_RETURN:
dbus_message_iter_init(reply, &args);
sig = dbus_message_iter_get_signature(&args);
crm_trace("Call to %s returned '%s'", method, sig);
dbus_free(sig);
break;
case DBUS_MESSAGE_TYPE_INVALID:
error.message = "Invalid reply";
error.name = "org.clusterlabs.pacemaker.InvalidReply";
crm_err("Error processing %s response: %s", method, error.message);
break;
case DBUS_MESSAGE_TYPE_METHOD_CALL:
error.message = "Invalid reply (method call)";
error.name = "org.clusterlabs.pacemaker.InvalidReply.Method";
crm_err("Error processing %s response: %s", method, error.message);
break;
case DBUS_MESSAGE_TYPE_SIGNAL:
error.message = "Invalid reply (signal)";
error.name = "org.clusterlabs.pacemaker.InvalidReply.Signal";
crm_err("Error processing %s response: %s", method, error.message);
break;
case DBUS_MESSAGE_TYPE_ERROR:
dbus_set_error_from_message (&error, reply);
crm_info("%s error '%s': %s", method, error.name, error.message);
break;
default:
error.message = "Unknown reply type";
error.name = "org.clusterlabs.pacemaker.InvalidReply.Type";
crm_err("Error processing %s response: %s (%d)", method, error.message, dtype);
}
}
if(ret && (error.name || error.message)) {
*ret = error;
return TRUE;
}
return FALSE;
}
DBusMessage *pcmk_dbus_send_recv(DBusMessage *msg, DBusConnection *connection, DBusError *error, int timeout)
{
const char *method = NULL;
DBusMessage *reply = NULL;
DBusPendingCall* pending = NULL;
CRM_ASSERT(dbus_message_get_type (msg) == DBUS_MESSAGE_TYPE_METHOD_CALL);
method = dbus_message_get_member (msg);
if (timeout <= 0) {
timeout = DBUS_TIMEOUT_USE_DEFAULT;
}
// send message and get a handle for a reply
if (!dbus_connection_send_with_reply (connection, msg, &pending, timeout/* -1 is default timeout, aka. DBUS_TIMEOUT_USE_DEFAULT */)) {
if(error) {
dbus_error_init(error);
error->message = "Call to dbus_connection_send_with_reply() failed";
error->name = "org.clusterlabs.pacemaker.SendFailed";
}
crm_err("Error sending %s request", method);
return NULL;
}
dbus_connection_flush(connection);
if(pending) {
/* block until we receive a reply */
dbus_pending_call_block(pending);
/* get the reply message */
reply = dbus_pending_call_steal_reply(pending);
}
(void)pcmk_dbus_find_error(method, pending, reply, error);
if(pending) {
/* free the pending message handle */
dbus_pending_call_unref(pending);
}
return reply;
}
DBusPendingCall* pcmk_dbus_send(DBusMessage *msg, DBusConnection *connection,
void(*done)(DBusPendingCall *pending, void *user_data), void *user_data, int timeout)
{
DBusError error;
const char *method = NULL;
DBusPendingCall* pending = NULL;
dbus_error_init(&error);
CRM_ASSERT(done);
CRM_ASSERT(dbus_message_get_type (msg) == DBUS_MESSAGE_TYPE_METHOD_CALL);
method = dbus_message_get_member (msg);
if (timeout <= 0) {
timeout = DBUS_TIMEOUT_USE_DEFAULT;
}
// send message and get a handle for a reply
if (!dbus_connection_send_with_reply (connection, msg, &pending, timeout/* -1 is default timeout, aka. DBUS_TIMEOUT_USE_DEFAULT */)) {
crm_err("Send with reply failed for %s", method);
return NULL;
} else if (pending == NULL) {
crm_err("No pending call found for %s: Connection to System DBus may be closed", method);
return NULL;
}
crm_trace("DBus %s call sent", method);
if (dbus_pending_call_get_completed(pending)) {
crm_info("DBus %s call completed too soon", method);
if(done) {
#if 0
/* This sounds like a good idea, but allegedly it breaks things */
done(pending, user_data);
pending = NULL;
#else
CRM_ASSERT(dbus_pending_call_set_notify(pending, done, user_data, NULL));
#endif
}
} else if(done) {
CRM_ASSERT(dbus_pending_call_set_notify(pending, done, user_data, NULL));
}
return pending;
}
bool pcmk_dbus_type_check(DBusMessage *msg, DBusMessageIter *field, int expected, const char *function, int line)
{
int dtype = 0;
DBusMessageIter lfield;
if(field == NULL) {
if(dbus_message_iter_init(msg, &lfield)) {
field = &lfield;
}
}
if(field == NULL) {
do_crm_log_alias(LOG_ERR, __FILE__, function, line,
"Empty parameter list in reply expecting '%c'", expected);
return FALSE;
}
dtype = dbus_message_iter_get_arg_type(field);
if(dtype != expected) {
DBusMessageIter args;
char *sig;
dbus_message_iter_init(msg, &args);
sig = dbus_message_iter_get_signature(&args);
do_crm_log_alias(LOG_ERR, __FILE__, function, line,
"Unexpected DBus type, expected %c in '%s' instead of %c",
expected, sig, dtype);
dbus_free(sig);
return FALSE;
}
return TRUE;
}
static char *
pcmk_dbus_lookup_result(DBusMessage *reply, struct db_getall_data *data)
{
DBusError error;
char *output = NULL;
DBusMessageIter dict;
DBusMessageIter args;
if(pcmk_dbus_find_error("GetAll", (void*)&error, reply, &error)) {
crm_err("Cannot get properties from %s for %s", data->target, data->object);
goto cleanup;
}
dbus_message_iter_init(reply, &args);
if(!pcmk_dbus_type_check(reply, &args, DBUS_TYPE_ARRAY, __FUNCTION__, __LINE__)) {
crm_err("Invalid reply from %s for %s", data->target, data->object);
goto cleanup;
}
dbus_message_iter_recurse(&args, &dict);
while (dbus_message_iter_get_arg_type (&dict) != DBUS_TYPE_INVALID) {
DBusMessageIter sv;
DBusMessageIter v;
DBusBasicValue name;
DBusBasicValue value;
if(!pcmk_dbus_type_check(reply, &dict, DBUS_TYPE_DICT_ENTRY, __FUNCTION__, __LINE__)) {
dbus_message_iter_next (&dict);
continue;
}
dbus_message_iter_recurse(&dict, &sv);
while (dbus_message_iter_get_arg_type (&sv) != DBUS_TYPE_INVALID) {
int dtype = dbus_message_iter_get_arg_type(&sv);
switch(dtype) {
case DBUS_TYPE_STRING:
dbus_message_iter_get_basic(&sv, &name);
if(data->name && strcmp(name.str, data->name) != 0) {
dbus_message_iter_next (&sv); /* Skip the value */
}
break;
case DBUS_TYPE_VARIANT:
dbus_message_iter_recurse(&sv, &v);
if(pcmk_dbus_type_check(reply, &v, DBUS_TYPE_STRING, __FUNCTION__, __LINE__)) {
dbus_message_iter_get_basic(&v, &value);
crm_trace("Property %s[%s] is '%s'", data->object, name.str, value.str);
if(data->callback) {
data->callback(name.str, value.str, data->userdata);
} else {
free(output);
output = strdup(value.str);
}
if(data->name) {
goto cleanup;
}
}
break;
default:
pcmk_dbus_type_check(reply, &sv, DBUS_TYPE_STRING, __FUNCTION__, __LINE__);
}
dbus_message_iter_next (&sv);
}
dbus_message_iter_next (&dict);
}
if(data->name && data->callback) {
crm_trace("No value for property %s[%s]", data->object, data->name);
data->callback(data->name, NULL, data->userdata);
}
cleanup:
free(data->target);
free(data->object);
free(data->name);
free(data);
return output;
}
static void
pcmk_dbus_lookup_cb(DBusPendingCall *pending, void *user_data)
{
DBusMessage *reply = NULL;
char *value = NULL;
if(pending) {
reply = dbus_pending_call_steal_reply(pending);
}
value = pcmk_dbus_lookup_result(reply, user_data);
free(value);
if(reply) {
dbus_message_unref(reply);
}
}
char *
pcmk_dbus_get_property(
DBusConnection *connection, const char *target, const char *obj, const gchar * iface, const char *name,
void (*callback)(const char *name, const char *value, void *userdata), void *userdata, DBusPendingCall **pending,
int timeout)
{
DBusMessage *msg;
const char *method = "GetAll";
char *output = NULL;
struct db_getall_data *query_data = NULL;
/* char *state = pcmk_dbus_get_property(systemd_proxy, BUS_NAME, unit, BUS_NAME ".Unit", "ActiveState"); */
crm_debug("Calling: %s on %s", method, target);
msg = dbus_message_new_method_call(target, // target for the method call
obj, // object to call on
BUS_PROPERTY_IFACE, // interface to call on
method); // method name
if (NULL == msg) {
crm_err("Call to %s failed: No message", method);
return NULL;
}
CRM_LOG_ASSERT(dbus_message_append_args(msg, DBUS_TYPE_STRING, &iface, DBUS_TYPE_INVALID));
query_data = malloc(sizeof(struct db_getall_data));
if(query_data == NULL) {
crm_err("Call to %s failed: malloc failed", method);
return NULL;
}
query_data->target = strdup(target);
query_data->object = strdup(obj);
query_data->callback = callback;
query_data->userdata = userdata;
query_data->name = NULL;
if(name) {
query_data->name = strdup(name);
}
if(query_data->callback) {
DBusPendingCall* _pending;
_pending = pcmk_dbus_send(msg, connection, pcmk_dbus_lookup_cb, query_data, timeout);
if (pending != NULL) {
*pending = _pending;
}
} else {
DBusMessage *reply = pcmk_dbus_send_recv(msg, connection, NULL, timeout);
output = pcmk_dbus_lookup_result(reply, query_data);
if(reply) {
dbus_message_unref(reply);
}
}
dbus_message_unref(msg);
return output;
}
static void pcmk_dbus_connection_dispatch(DBusConnection *connection, DBusDispatchStatus new_status, void *data){
crm_trace("status %d for %p", new_status, data);
if (new_status == DBUS_DISPATCH_DATA_REMAINS){
dbus_connection_dispatch(connection);
while (dbus_connection_get_dispatch_status(connection) == DBUS_DISPATCH_DATA_REMAINS) {
dbus_connection_dispatch(connection);
}
}
}
/* Copied from dbus-watch.c */
static const char*
dbus_watch_flags_to_string (int flags)
{
const char *watch_type;
if ((flags & DBUS_WATCH_READABLE) &&
(flags & DBUS_WATCH_WRITABLE))
watch_type = "readwrite";
else if (flags & DBUS_WATCH_READABLE)
watch_type = "read";
else if (flags & DBUS_WATCH_WRITABLE)
watch_type = "write";
else
watch_type = "not read or write";
return watch_type;
}
static int
pcmk_dbus_watch_dispatch(gpointer userdata)
{
bool oom = FALSE;
DBusWatch *watch = userdata;
int flags = dbus_watch_get_flags(watch);
bool enabled = dbus_watch_get_enabled (watch);
mainloop_io_t *client = dbus_watch_get_data(watch);
crm_trace("Dispatching client %p: %s", client, dbus_watch_flags_to_string(flags));
if (enabled && is_set(flags, DBUS_WATCH_READABLE)) {
oom = !dbus_watch_handle(watch, flags);
} else if (enabled && is_set(flags, DBUS_WATCH_READABLE)) {
oom = !dbus_watch_handle(watch, flags);
} else if(enabled) {
oom = !dbus_watch_handle(watch, DBUS_WATCH_ERROR);
}
if(flags != dbus_watch_get_flags(watch)) {
flags = dbus_watch_get_flags(watch);
crm_trace("Dispatched client %p: %s (%d)", client, dbus_watch_flags_to_string(flags), flags);
}
if(oom) {
crm_err("DBus encountered OOM while attempting to dispatch %p (%s)", client, dbus_watch_flags_to_string(flags));
}
return 0;
}
static void
pcmk_dbus_watch_destroy(gpointer userdata)
{
mainloop_io_t *client = dbus_watch_get_data(userdata);
crm_trace("Destroyed %p", client);
}
struct mainloop_fd_callbacks pcmk_dbus_cb = {
.dispatch = pcmk_dbus_watch_dispatch,
.destroy = pcmk_dbus_watch_destroy,
};
static dbus_bool_t
pcmk_dbus_watch_add(DBusWatch *watch, void *data){
int fd = dbus_watch_get_unix_fd(watch);
mainloop_io_t *client = mainloop_add_fd(
"dbus", G_PRIORITY_DEFAULT, fd, watch, &pcmk_dbus_cb);
crm_trace("Added watch %p with fd=%d to client %p", watch, fd, client);
dbus_watch_set_data(watch, client, NULL);
return TRUE;
}
static void
pcmk_dbus_watch_toggle(DBusWatch *watch, void *data)
{
mainloop_io_t *client = dbus_watch_get_data(watch);
crm_notice("DBus client %p is now %s", client, dbus_watch_get_enabled(watch)?"enabled":"disabled");
}
static void
pcmk_dbus_watch_remove(DBusWatch *watch, void *data){
mainloop_io_t *client = dbus_watch_get_data(watch);
crm_trace("Removed client %p (%p)", client, data);
mainloop_del_fd(client);
}
static gboolean
pcmk_dbus_timeout_dispatch(gpointer data)
{
crm_info("Timeout %p expired", data);
dbus_timeout_handle(data);
return FALSE;
}
static dbus_bool_t
pcmk_dbus_timeout_add(DBusTimeout *timeout, void *data){
guint id = g_timeout_add(dbus_timeout_get_interval(timeout), pcmk_dbus_timeout_dispatch, timeout);
crm_trace("Adding timeout %p (%ld)", timeout, dbus_timeout_get_interval(timeout));
if(id) {
dbus_timeout_set_data(timeout, GUINT_TO_POINTER(id), NULL);
}
return TRUE;
}
static void
pcmk_dbus_timeout_remove(DBusTimeout *timeout, void *data){
void *vid = dbus_timeout_get_data(timeout);
guint id = GPOINTER_TO_UINT(vid);
crm_trace("Removing timeout %p (%p)", timeout, data);
if(id) {
g_source_remove(id);
dbus_timeout_set_data(timeout, 0, NULL);
}
}
static void
pcmk_dbus_timeout_toggle(DBusTimeout *timeout, void *data){
bool enabled = dbus_timeout_get_enabled(timeout);
crm_trace("Toggling timeout for %p to %s", timeout, enabled?"off":"on");
if(enabled) {
pcmk_dbus_timeout_add(timeout, data);
} else {
pcmk_dbus_timeout_remove(timeout, data);
}
}
/* Inspired by http://www.kolej.mff.cuni.cz/~vesej3am/devel/dbus-select.c */
void pcmk_dbus_connection_setup_with_select(DBusConnection *c){
dbus_connection_set_exit_on_disconnect (c, FALSE);
dbus_connection_set_timeout_functions(
c, pcmk_dbus_timeout_add, pcmk_dbus_timeout_remove, pcmk_dbus_timeout_toggle, NULL, NULL);
dbus_connection_set_watch_functions(c, pcmk_dbus_watch_add, pcmk_dbus_watch_remove, pcmk_dbus_watch_toggle, NULL, NULL);
dbus_connection_set_dispatch_status_function(c, pcmk_dbus_connection_dispatch, NULL, NULL);
pcmk_dbus_connection_dispatch(c, dbus_connection_get_dispatch_status(c), NULL);
}