| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#ifdef HAVE_CONFIG_H |
| 25 |
#include <config.h> |
| 26 |
#endif |
| 27 |
|
| 28 |
#include <stdio.h> |
| 29 |
#include <string.h> |
| 30 |
|
| 31 |
#include <glib.h> |
| 32 |
#include <dbus/dbus.h> |
| 33 |
|
| 34 |
#include "gdbus.h" |
| 35 |
|
| 36 |
#define info(fmt...) |
| 37 |
#define error(fmt...) |
| 38 |
#define debug(fmt...) |
| 39 |
|
| 40 |
static DBusHandlerResult message_filter(DBusConnection *connection, |
| 41 |
DBusMessage *message, void *user_data); |
| 42 |
|
| 43 |
static guint listener_id = 0; |
| 44 |
static GSList *listeners = NULL; |
| 45 |
|
| 46 |
struct service_data { |
| 47 |
DBusConnection *conn; |
| 48 |
DBusPendingCall *call; |
| 49 |
char *name; |
| 50 |
const char *owner; |
| 51 |
guint id; |
| 52 |
struct filter_callback *callback; |
| 53 |
}; |
| 54 |
|
| 55 |
struct filter_callback { |
| 56 |
GDBusWatchFunction conn_func; |
| 57 |
GDBusWatchFunction disc_func; |
| 58 |
GDBusSignalFunction signal_func; |
| 59 |
GDBusDestroyFunction destroy_func; |
| 60 |
struct service_data *data; |
| 61 |
void *user_data; |
| 62 |
guint id; |
| 63 |
}; |
| 64 |
|
| 65 |
struct filter_data { |
| 66 |
DBusConnection *connection; |
| 67 |
DBusHandleMessageFunction handle_func; |
| 68 |
char *name; |
| 69 |
char *owner; |
| 70 |
char *path; |
| 71 |
char *interface; |
| 72 |
char *member; |
| 73 |
char *argument; |
| 74 |
GSList *callbacks; |
| 75 |
GSList *processed; |
| 76 |
guint name_watch; |
| 77 |
gboolean lock; |
| 78 |
gboolean registered; |
| 79 |
}; |
| 80 |
|
| 81 |
static struct filter_data *filter_data_find_match(DBusConnection *connection, |
| 82 |
const char *name, |
| 83 |
const char *owner, |
| 84 |
const char *path, |
| 85 |
const char *interface, |
| 86 |
const char *member, |
| 87 |
const char *argument) |
| 88 |
{ |
| 89 |
GSList *current; |
| 90 |
|
| 91 |
for (current = listeners; |
| 92 |
current != NULL; current = current->next) { |
| 93 |
struct filter_data *data = current->data; |
| 94 |
|
| 95 |
if (connection != data->connection) |
| 96 |
continue; |
| 97 |
|
| 98 |
if (g_strcmp0(name, data->name) != 0) |
| 99 |
continue; |
| 100 |
|
| 101 |
if (g_strcmp0(owner, data->owner) != 0) |
| 102 |
continue; |
| 103 |
|
| 104 |
if (g_strcmp0(path, data->path) != 0) |
| 105 |
continue; |
| 106 |
|
| 107 |
if (g_strcmp0(interface, data->interface) != 0) |
| 108 |
continue; |
| 109 |
|
| 110 |
if (g_strcmp0(member, data->member) != 0) |
| 111 |
continue; |
| 112 |
|
| 113 |
if (g_strcmp0(argument, data->argument) != 0) |
| 114 |
continue; |
| 115 |
|
| 116 |
return data; |
| 117 |
} |
| 118 |
|
| 119 |
return NULL; |
| 120 |
} |
| 121 |
|
| 122 |
static struct filter_data *filter_data_find(DBusConnection *connection) |
| 123 |
{ |
| 124 |
GSList *current; |
| 125 |
|
| 126 |
for (current = listeners; |
| 127 |
current != NULL; current = current->next) { |
| 128 |
struct filter_data *data = current->data; |
| 129 |
|
| 130 |
if (connection != data->connection) |
| 131 |
continue; |
| 132 |
|
| 133 |
return data; |
| 134 |
} |
| 135 |
|
| 136 |
return NULL; |
| 137 |
} |
| 138 |
|
| 139 |
static void format_rule(struct filter_data *data, char *rule, size_t size) |
| 140 |
{ |
| 141 |
const char *sender; |
| 142 |
int offset; |
| 143 |
|
| 144 |
offset = snprintf(rule, size, "type='signal'"); |
| 145 |
sender = data->name ? : data->owner; |
| 146 |
|
| 147 |
if (sender) |
| 148 |
offset += snprintf(rule + offset, size - offset, |
| 149 |
",sender='%s'", sender); |
| 150 |
if (data->path) |
| 151 |
offset += snprintf(rule + offset, size - offset, |
| 152 |
",path='%s'", data->path); |
| 153 |
if (data->interface) |
| 154 |
offset += snprintf(rule + offset, size - offset, |
| 155 |
",interface='%s'", data->interface); |
| 156 |
if (data->member) |
| 157 |
offset += snprintf(rule + offset, size - offset, |
| 158 |
",member='%s'", data->member); |
| 159 |
if (data->argument) |
| 160 |
snprintf(rule + offset, size - offset, |
| 161 |
",arg0='%s'", data->argument); |
| 162 |
} |
| 163 |
|
| 164 |
static gboolean add_match(struct filter_data *data, |
| 165 |
DBusHandleMessageFunction filter) |
| 166 |
{ |
| 167 |
DBusError err; |
| 168 |
char rule[DBUS_MAXIMUM_MATCH_RULE_LENGTH]; |
| 169 |
|
| 170 |
format_rule(data, rule, sizeof(rule)); |
| 171 |
dbus_error_init(&err); |
| 172 |
|
| 173 |
dbus_bus_add_match(data->connection, rule, &err); |
| 174 |
if (dbus_error_is_set(&err)) { |
| 175 |
error("Adding match rule \"%s\" failed: %s", rule, |
| 176 |
err.message); |
| 177 |
dbus_error_free(&err); |
| 178 |
return FALSE; |
| 179 |
} |
| 180 |
|
| 181 |
data->handle_func = filter; |
| 182 |
data->registered = TRUE; |
| 183 |
|
| 184 |
return TRUE; |
| 185 |
} |
| 186 |
|
| 187 |
static gboolean remove_match(struct filter_data *data) |
| 188 |
{ |
| 189 |
DBusError err; |
| 190 |
char rule[DBUS_MAXIMUM_MATCH_RULE_LENGTH]; |
| 191 |
|
| 192 |
format_rule(data, rule, sizeof(rule)); |
| 193 |
|
| 194 |
dbus_error_init(&err); |
| 195 |
|
| 196 |
dbus_bus_remove_match(data->connection, rule, &err); |
| 197 |
if (dbus_error_is_set(&err)) { |
| 198 |
error("Removing owner match rule for %s failed: %s", |
| 199 |
rule, err.message); |
| 200 |
dbus_error_free(&err); |
| 201 |
return FALSE; |
| 202 |
} |
| 203 |
|
| 204 |
return TRUE; |
| 205 |
} |
| 206 |
|
| 207 |
static struct filter_data *filter_data_get(DBusConnection *connection, |
| 208 |
DBusHandleMessageFunction filter, |
| 209 |
const char *sender, |
| 210 |
const char *path, |
| 211 |
const char *interface, |
| 212 |
const char *member, |
| 213 |
const char *argument) |
| 214 |
{ |
| 215 |
struct filter_data *data; |
| 216 |
const char *name = NULL, *owner = NULL; |
| 217 |
|
| 218 |
if (filter_data_find(connection) == NULL) { |
| 219 |
if (!dbus_connection_add_filter(connection, |
| 220 |
message_filter, NULL, NULL)) { |
| 221 |
error("dbus_connection_add_filter() failed"); |
| 222 |
return NULL; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
if (sender == NULL) |
| 227 |
goto proceed; |
| 228 |
|
| 229 |
if (sender[0] == ':') |
| 230 |
owner = sender; |
| 231 |
else |
| 232 |
name = sender; |
| 233 |
|
| 234 |
proceed: |
| 235 |
data = filter_data_find_match(connection, name, owner, path, |
| 236 |
interface, member, argument); |
| 237 |
if (data) |
| 238 |
return data; |
| 239 |
|
| 240 |
data = g_new0(struct filter_data, 1); |
| 241 |
|
| 242 |
data->connection = dbus_connection_ref(connection); |
| 243 |
data->name = g_strdup(name); |
| 244 |
data->owner = g_strdup(owner); |
| 245 |
data->path = g_strdup(path); |
| 246 |
data->interface = g_strdup(interface); |
| 247 |
data->member = g_strdup(member); |
| 248 |
data->argument = g_strdup(argument); |
| 249 |
|
| 250 |
if (!add_match(data, filter)) { |
| 251 |
g_free(data); |
| 252 |
return NULL; |
| 253 |
} |
| 254 |
|
| 255 |
listeners = g_slist_append(listeners, data); |
| 256 |
|
| 257 |
return data; |
| 258 |
} |
| 259 |
|
| 260 |
static struct filter_callback *filter_data_find_callback( |
| 261 |
struct filter_data *data, |
| 262 |
guint id) |
| 263 |
{ |
| 264 |
GSList *l; |
| 265 |
|
| 266 |
for (l = data->callbacks; l; l = l->next) { |
| 267 |
struct filter_callback *cb = l->data; |
| 268 |
if (cb->id == id) |
| 269 |
return cb; |
| 270 |
} |
| 271 |
for (l = data->processed; l; l = l->next) { |
| 272 |
struct filter_callback *cb = l->data; |
| 273 |
if (cb->id == id) |
| 274 |
return cb; |
| 275 |
} |
| 276 |
|
| 277 |
return NULL; |
| 278 |
} |
| 279 |
|
| 280 |
static void filter_data_free(struct filter_data *data) |
| 281 |
{ |
| 282 |
GSList *l; |
| 283 |
|
| 284 |
for (l = data->callbacks; l != NULL; l = l->next) |
| 285 |
g_free(l->data); |
| 286 |
|
| 287 |
g_slist_free(data->callbacks); |
| 288 |
g_dbus_remove_watch(data->connection, data->name_watch); |
| 289 |
g_free(data->name); |
| 290 |
g_free(data->owner); |
| 291 |
g_free(data->path); |
| 292 |
g_free(data->interface); |
| 293 |
g_free(data->member); |
| 294 |
g_free(data->argument); |
| 295 |
dbus_connection_unref(data->connection); |
| 296 |
g_free(data); |
| 297 |
} |
| 298 |
|
| 299 |
static void filter_data_call_and_free(struct filter_data *data) |
| 300 |
{ |
| 301 |
GSList *l; |
| 302 |
|
| 303 |
for (l = data->callbacks; l != NULL; l = l->next) { |
| 304 |
struct filter_callback *cb = l->data; |
| 305 |
if (cb->disc_func) |
| 306 |
cb->disc_func(data->connection, cb->user_data); |
| 307 |
if (cb->destroy_func) |
| 308 |
cb->destroy_func(cb->user_data); |
| 309 |
g_free(cb); |
| 310 |
} |
| 311 |
|
| 312 |
g_slist_free(data->callbacks); |
| 313 |
data->callbacks = NULL; |
| 314 |
|
| 315 |
filter_data_free(data); |
| 316 |
} |
| 317 |
|
| 318 |
static struct filter_callback *filter_data_add_callback( |
| 319 |
struct filter_data *data, |
| 320 |
GDBusWatchFunction connect, |
| 321 |
GDBusWatchFunction disconnect, |
| 322 |
GDBusSignalFunction signal, |
| 323 |
GDBusDestroyFunction destroy, |
| 324 |
void *user_data) |
| 325 |
{ |
| 326 |
struct filter_callback *cb = NULL; |
| 327 |
|
| 328 |
cb = g_new0(struct filter_callback, 1); |
| 329 |
|
| 330 |
cb->conn_func = connect; |
| 331 |
cb->disc_func = disconnect; |
| 332 |
cb->signal_func = signal; |
| 333 |
cb->destroy_func = destroy; |
| 334 |
cb->user_data = user_data; |
| 335 |
cb->id = ++listener_id; |
| 336 |
|
| 337 |
if (data->lock) |
| 338 |
data->processed = g_slist_append(data->processed, cb); |
| 339 |
else |
| 340 |
data->callbacks = g_slist_append(data->callbacks, cb); |
| 341 |
|
| 342 |
return cb; |
| 343 |
} |
| 344 |
|
| 345 |
static void service_data_free(struct service_data *data) |
| 346 |
{ |
| 347 |
struct filter_callback *callback = data->callback; |
| 348 |
|
| 349 |
dbus_connection_unref(data->conn); |
| 350 |
|
| 351 |
if (data->call) |
| 352 |
dbus_pending_call_unref(data->call); |
| 353 |
|
| 354 |
if (data->id) |
| 355 |
g_source_remove(data->id); |
| 356 |
|
| 357 |
g_free(data->name); |
| 358 |
g_free(data); |
| 359 |
|
| 360 |
callback->data = NULL; |
| 361 |
} |
| 362 |
|
| 363 |
static gboolean filter_data_remove_callback(struct filter_data *data, |
| 364 |
struct filter_callback *cb) |
| 365 |
{ |
| 366 |
DBusConnection *connection; |
| 367 |
|
| 368 |
data->callbacks = g_slist_remove(data->callbacks, cb); |
| 369 |
data->processed = g_slist_remove(data->processed, cb); |
| 370 |
|
| 371 |
|
| 372 |
if (cb->data) { |
| 373 |
if (cb->data->call) |
| 374 |
dbus_pending_call_cancel(cb->data->call); |
| 375 |
service_data_free(cb->data); |
| 376 |
} |
| 377 |
|
| 378 |
if (cb->destroy_func) |
| 379 |
cb->destroy_func(cb->user_data); |
| 380 |
|
| 381 |
g_free(cb); |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
if (data->callbacks || data->lock) |
| 386 |
return TRUE; |
| 387 |
|
| 388 |
if (data->registered && !remove_match(data)) |
| 389 |
return FALSE; |
| 390 |
|
| 391 |
connection = dbus_connection_ref(data->connection); |
| 392 |
listeners = g_slist_remove(listeners, data); |
| 393 |
|
| 394 |
|
| 395 |
if (filter_data_find(connection) == NULL) |
| 396 |
dbus_connection_remove_filter(connection, message_filter, |
| 397 |
NULL); |
| 398 |
|
| 399 |
filter_data_free(data); |
| 400 |
dbus_connection_unref(connection); |
| 401 |
|
| 402 |
return TRUE; |
| 403 |
} |
| 404 |
|
| 405 |
static DBusHandlerResult signal_filter(DBusConnection *connection, |
| 406 |
DBusMessage *message, void *user_data) |
| 407 |
{ |
| 408 |
struct filter_data *data = user_data; |
| 409 |
struct filter_callback *cb; |
| 410 |
|
| 411 |
while (data->callbacks) { |
| 412 |
cb = data->callbacks->data; |
| 413 |
|
| 414 |
if (cb->signal_func && !cb->signal_func(connection, message, |
| 415 |
cb->user_data)) { |
| 416 |
filter_data_remove_callback(data, cb); |
| 417 |
continue; |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
if (!g_slist_find(data->callbacks, cb)) |
| 423 |
continue; |
| 424 |
|
| 425 |
data->callbacks = g_slist_remove(data->callbacks, cb); |
| 426 |
data->processed = g_slist_append(data->processed, cb); |
| 427 |
} |
| 428 |
|
| 429 |
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 430 |
} |
| 431 |
|
| 432 |
static void update_name_cache(const char *name, const char *owner) |
| 433 |
{ |
| 434 |
GSList *l; |
| 435 |
|
| 436 |
for (l = listeners; l != NULL; l = l->next) { |
| 437 |
struct filter_data *data = l->data; |
| 438 |
|
| 439 |
if (g_strcmp0(data->name, name) != 0) |
| 440 |
continue; |
| 441 |
|
| 442 |
g_free(data->owner); |
| 443 |
data->owner = g_strdup(owner); |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
static const char *check_name_cache(const char *name) |
| 448 |
{ |
| 449 |
GSList *l; |
| 450 |
|
| 451 |
for (l = listeners; l != NULL; l = l->next) { |
| 452 |
struct filter_data *data = l->data; |
| 453 |
|
| 454 |
if (g_strcmp0(data->name, name) != 0) |
| 455 |
continue; |
| 456 |
|
| 457 |
return data->owner; |
| 458 |
} |
| 459 |
|
| 460 |
return NULL; |
| 461 |
} |
| 462 |
|
| 463 |
static DBusHandlerResult service_filter(DBusConnection *connection, |
| 464 |
DBusMessage *message, void *user_data) |
| 465 |
{ |
| 466 |
struct filter_data *data = user_data; |
| 467 |
struct filter_callback *cb; |
| 468 |
char *name, *old, *new; |
| 469 |
|
| 470 |
if (!dbus_message_get_args(message, NULL, |
| 471 |
DBUS_TYPE_STRING, &name, |
| 472 |
DBUS_TYPE_STRING, &old, |
| 473 |
DBUS_TYPE_STRING, &new, |
| 474 |
DBUS_TYPE_INVALID)) { |
| 475 |
error("Invalid arguments for NameOwnerChanged signal"); |
| 476 |
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 477 |
} |
| 478 |
|
| 479 |
update_name_cache(name, new); |
| 480 |
|
| 481 |
while (data->callbacks) { |
| 482 |
cb = data->callbacks->data; |
| 483 |
|
| 484 |
if (*new == '\0') { |
| 485 |
if (cb->disc_func) |
| 486 |
cb->disc_func(connection, cb->user_data); |
| 487 |
} else { |
| 488 |
if (cb->conn_func) |
| 489 |
cb->conn_func(connection, cb->user_data); |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
if (!g_slist_find(data->callbacks, cb)) |
| 495 |
continue; |
| 496 |
|
| 497 |
|
| 498 |
if (data->argument[0] == ':' && |
| 499 |
(cb->conn_func == NULL || cb->disc_func == NULL)) { |
| 500 |
filter_data_remove_callback(data, cb); |
| 501 |
continue; |
| 502 |
} |
| 503 |
|
| 504 |
data->callbacks = g_slist_remove(data->callbacks, cb); |
| 505 |
data->processed = g_slist_append(data->processed, cb); |
| 506 |
} |
| 507 |
|
| 508 |
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
static DBusHandlerResult message_filter(DBusConnection *connection, |
| 513 |
DBusMessage *message, void *user_data) |
| 514 |
{ |
| 515 |
struct filter_data *data; |
| 516 |
const char *sender, *path, *iface, *member, *arg = NULL; |
| 517 |
GSList *current, *delete_listener = NULL; |
| 518 |
|
| 519 |
|
| 520 |
if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_SIGNAL) |
| 521 |
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 522 |
|
| 523 |
sender = dbus_message_get_sender(message); |
| 524 |
path = dbus_message_get_path(message); |
| 525 |
iface = dbus_message_get_interface(message); |
| 526 |
member = dbus_message_get_member(message); |
| 527 |
dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &arg, DBUS_TYPE_INVALID); |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
for (current = listeners; current != NULL; current = current->next) { |
| 532 |
data = current->data; |
| 533 |
|
| 534 |
if (connection != data->connection) |
| 535 |
continue; |
| 536 |
|
| 537 |
if (data->owner && g_str_equal(sender, data->owner) == FALSE) |
| 538 |
continue; |
| 539 |
|
| 540 |
if (data->path && g_str_equal(path, data->path) == FALSE) |
| 541 |
continue; |
| 542 |
|
| 543 |
if (data->interface && g_str_equal(iface, |
| 544 |
data->interface) == FALSE) |
| 545 |
continue; |
| 546 |
|
| 547 |
if (data->member && g_str_equal(member, data->member) == FALSE) |
| 548 |
continue; |
| 549 |
|
| 550 |
if (data->argument && g_str_equal(arg, |
| 551 |
data->argument) == FALSE) |
| 552 |
continue; |
| 553 |
|
| 554 |
if (data->handle_func) { |
| 555 |
data->lock = TRUE; |
| 556 |
|
| 557 |
data->handle_func(connection, message, data); |
| 558 |
|
| 559 |
data->callbacks = data->processed; |
| 560 |
data->processed = NULL; |
| 561 |
data->lock = FALSE; |
| 562 |
} |
| 563 |
|
| 564 |
if (!data->callbacks) |
| 565 |
delete_listener = g_slist_prepend(delete_listener, |
| 566 |
current); |
| 567 |
} |
| 568 |
|
| 569 |
for (current = delete_listener; current != NULL; |
| 570 |
current = delete_listener->next) { |
| 571 |
GSList *l = current->data; |
| 572 |
|
| 573 |
data = l->data; |
| 574 |
|
| 575 |
|
| 576 |
if (data->callbacks != NULL) |
| 577 |
continue; |
| 578 |
|
| 579 |
remove_match(data); |
| 580 |
listeners = g_slist_delete_link(listeners, l); |
| 581 |
|
| 582 |
filter_data_free(data); |
| 583 |
} |
| 584 |
|
| 585 |
g_slist_free(delete_listener); |
| 586 |
|
| 587 |
|
| 588 |
if (filter_data_find(connection) == NULL) |
| 589 |
dbus_connection_remove_filter(connection, message_filter, |
| 590 |
NULL); |
| 591 |
|
| 592 |
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 593 |
} |
| 594 |
|
| 595 |
static gboolean update_service(void *user_data) |
| 596 |
{ |
| 597 |
struct service_data *data = user_data; |
| 598 |
struct filter_callback *cb = data->callback; |
| 599 |
|
| 600 |
update_name_cache(data->name, data->owner); |
| 601 |
if (cb->conn_func) |
| 602 |
cb->conn_func(data->conn, cb->user_data); |
| 603 |
|
| 604 |
service_data_free(data); |
| 605 |
|
| 606 |
return FALSE; |
| 607 |
} |
| 608 |
|
| 609 |
static void service_reply(DBusPendingCall *call, void *user_data) |
| 610 |
{ |
| 611 |
struct service_data *data = user_data; |
| 612 |
DBusMessage *reply; |
| 613 |
DBusError err; |
| 614 |
|
| 615 |
reply = dbus_pending_call_steal_reply(call); |
| 616 |
if (reply == NULL) |
| 617 |
return; |
| 618 |
|
| 619 |
dbus_error_init(&err); |
| 620 |
|
| 621 |
if (dbus_set_error_from_message(&err, reply)) |
| 622 |
goto fail; |
| 623 |
|
| 624 |
if (dbus_message_get_args(reply, &err, |
| 625 |
DBUS_TYPE_STRING, &data->owner, |
| 626 |
DBUS_TYPE_INVALID) == FALSE) |
| 627 |
goto fail; |
| 628 |
|
| 629 |
update_service(data); |
| 630 |
|
| 631 |
goto done; |
| 632 |
|
| 633 |
fail: |
| 634 |
error("%s", err.message); |
| 635 |
dbus_error_free(&err); |
| 636 |
service_data_free(data); |
| 637 |
done: |
| 638 |
dbus_message_unref(reply); |
| 639 |
} |
| 640 |
|
| 641 |
static void check_service(DBusConnection *connection, |
| 642 |
const char *name, |
| 643 |
struct filter_callback *callback) |
| 644 |
{ |
| 645 |
DBusMessage *message; |
| 646 |
struct service_data *data; |
| 647 |
|
| 648 |
data = g_try_malloc0(sizeof(*data)); |
| 649 |
if (data == NULL) { |
| 650 |
error("Can't allocate data structure"); |
| 651 |
return; |
| 652 |
} |
| 653 |
|
| 654 |
data->conn = dbus_connection_ref(connection); |
| 655 |
data->name = g_strdup(name); |
| 656 |
data->callback = callback; |
| 657 |
callback->data = data; |
| 658 |
|
| 659 |
data->owner = check_name_cache(name); |
| 660 |
if (data->owner != NULL) { |
| 661 |
data->id = g_idle_add(update_service, data); |
| 662 |
return; |
| 663 |
} |
| 664 |
|
| 665 |
message = dbus_message_new_method_call(DBUS_SERVICE_DBUS, |
| 666 |
DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "GetNameOwner"); |
| 667 |
if (message == NULL) { |
| 668 |
error("Can't allocate new message"); |
| 669 |
g_free(data); |
| 670 |
return; |
| 671 |
} |
| 672 |
|
| 673 |
dbus_message_append_args(message, DBUS_TYPE_STRING, &name, |
| 674 |
DBUS_TYPE_INVALID); |
| 675 |
|
| 676 |
if (dbus_connection_send_with_reply(connection, message, |
| 677 |
&data->call, -1) == FALSE) { |
| 678 |
error("Failed to execute method call"); |
| 679 |
g_free(data); |
| 680 |
goto done; |
| 681 |
} |
| 682 |
|
| 683 |
if (data->call == NULL) { |
| 684 |
error("D-Bus connection not available"); |
| 685 |
g_free(data); |
| 686 |
goto done; |
| 687 |
} |
| 688 |
|
| 689 |
dbus_pending_call_set_notify(data->call, service_reply, data, NULL); |
| 690 |
|
| 691 |
done: |
| 692 |
dbus_message_unref(message); |
| 693 |
} |
| 694 |
|
| 695 |
guint g_dbus_add_service_watch(DBusConnection *connection, const char *name, |
| 696 |
GDBusWatchFunction connect, |
| 697 |
GDBusWatchFunction disconnect, |
| 698 |
void *user_data, GDBusDestroyFunction destroy) |
| 699 |
{ |
| 700 |
struct filter_data *data; |
| 701 |
struct filter_callback *cb; |
| 702 |
|
| 703 |
if (name == NULL) |
| 704 |
return 0; |
| 705 |
|
| 706 |
data = filter_data_get(connection, service_filter, NULL, NULL, |
| 707 |
DBUS_INTERFACE_DBUS, "NameOwnerChanged", |
| 708 |
name); |
| 709 |
if (data == NULL) |
| 710 |
return 0; |
| 711 |
|
| 712 |
cb = filter_data_add_callback(data, connect, disconnect, NULL, destroy, |
| 713 |
user_data); |
| 714 |
if (cb == NULL) |
| 715 |
return 0; |
| 716 |
|
| 717 |
if (connect) |
| 718 |
check_service(connection, name, cb); |
| 719 |
|
| 720 |
return cb->id; |
| 721 |
} |
| 722 |
|
| 723 |
guint g_dbus_add_disconnect_watch(DBusConnection *connection, const char *name, |
| 724 |
GDBusWatchFunction func, |
| 725 |
void *user_data, GDBusDestroyFunction destroy) |
| 726 |
{ |
| 727 |
return g_dbus_add_service_watch(connection, name, NULL, func, |
| 728 |
user_data, destroy); |
| 729 |
} |
| 730 |
|
| 731 |
guint g_dbus_add_signal_watch(DBusConnection *connection, |
| 732 |
const char *sender, const char *path, |
| 733 |
const char *interface, const char *member, |
| 734 |
GDBusSignalFunction function, void *user_data, |
| 735 |
GDBusDestroyFunction destroy) |
| 736 |
{ |
| 737 |
struct filter_data *data; |
| 738 |
struct filter_callback *cb; |
| 739 |
|
| 740 |
data = filter_data_get(connection, signal_filter, sender, path, |
| 741 |
interface, member, NULL); |
| 742 |
if (data == NULL) |
| 743 |
return 0; |
| 744 |
|
| 745 |
cb = filter_data_add_callback(data, NULL, NULL, function, destroy, |
| 746 |
user_data); |
| 747 |
if (cb == NULL) |
| 748 |
return 0; |
| 749 |
|
| 750 |
if (data->name != NULL && data->name_watch == 0) |
| 751 |
data->name_watch = g_dbus_add_service_watch(connection, |
| 752 |
data->name, NULL, |
| 753 |
NULL, NULL, NULL); |
| 754 |
|
| 755 |
return cb->id; |
| 756 |
} |
| 757 |
|
| 758 |
guint g_dbus_add_properties_watch(DBusConnection *connection, |
| 759 |
const char *sender, const char *path, |
| 760 |
const char *interface, |
| 761 |
GDBusSignalFunction function, void *user_data, |
| 762 |
GDBusDestroyFunction destroy) |
| 763 |
{ |
| 764 |
struct filter_data *data; |
| 765 |
struct filter_callback *cb; |
| 766 |
|
| 767 |
data = filter_data_get(connection, signal_filter, sender, path, |
| 768 |
DBUS_INTERFACE_PROPERTIES, "PropertiesChanged", |
| 769 |
interface); |
| 770 |
if (data == NULL) |
| 771 |
return 0; |
| 772 |
|
| 773 |
cb = filter_data_add_callback(data, NULL, NULL, function, destroy, |
| 774 |
user_data); |
| 775 |
if (cb == NULL) |
| 776 |
return 0; |
| 777 |
|
| 778 |
if (data->name != NULL && data->name_watch == 0) |
| 779 |
data->name_watch = g_dbus_add_service_watch(connection, |
| 780 |
data->name, NULL, |
| 781 |
NULL, NULL, NULL); |
| 782 |
|
| 783 |
return cb->id; |
| 784 |
} |
| 785 |
|
| 786 |
gboolean g_dbus_remove_watch(DBusConnection *connection, guint id) |
| 787 |
{ |
| 788 |
struct filter_data *data; |
| 789 |
struct filter_callback *cb; |
| 790 |
GSList *ldata; |
| 791 |
|
| 792 |
if (id == 0) |
| 793 |
return FALSE; |
| 794 |
|
| 795 |
for (ldata = listeners; ldata; ldata = ldata->next) { |
| 796 |
data = ldata->data; |
| 797 |
|
| 798 |
cb = filter_data_find_callback(data, id); |
| 799 |
if (cb) { |
| 800 |
filter_data_remove_callback(data, cb); |
| 801 |
return TRUE; |
| 802 |
} |
| 803 |
} |
| 804 |
|
| 805 |
return FALSE; |
| 806 |
} |
| 807 |
|
| 808 |
void g_dbus_remove_all_watches(DBusConnection *connection) |
| 809 |
{ |
| 810 |
struct filter_data *data; |
| 811 |
|
| 812 |
while ((data = filter_data_find(connection))) { |
| 813 |
listeners = g_slist_remove(listeners, data); |
| 814 |
filter_data_call_and_free(data); |
| 815 |
} |
| 816 |
|
| 817 |
dbus_connection_remove_filter(connection, message_filter, NULL); |
| 818 |
} |