5 * Copyright (C) 2004-2011 Marcel Holtmann <marcel@holtmann.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <dbus/dbus.h>
40 static DBusHandlerResult message_filter(DBusConnection *connection,
41 DBusMessage *message, void *user_data);
43 static guint listener_id = 0;
44 static GSList *listeners = NULL;
48 DBusPendingCall *call;
52 struct filter_callback *callback;
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;
66 DBusConnection *connection;
67 DBusHandleMessageFunction handle_func;
81 static struct filter_data *filter_data_find(DBusConnection *connection,
85 const char *interface,
91 for (current = listeners;
92 current != NULL; current = current->next) {
93 struct filter_data *data = current->data;
95 if (connection != data->connection)
98 if (name && data->name &&
99 g_str_equal(name, data->name) == FALSE)
102 if (owner && data->owner &&
103 g_str_equal(owner, data->owner) == FALSE)
106 if (path && data->path &&
107 g_str_equal(path, data->path) == FALSE)
110 if (interface && data->interface &&
111 g_str_equal(interface, data->interface) == FALSE)
114 if (member && data->member &&
115 g_str_equal(member, data->member) == FALSE)
118 if (argument && data->argument &&
119 g_str_equal(argument, data->argument) == FALSE)
128 static void format_rule(struct filter_data *data, char *rule, size_t size)
133 offset = snprintf(rule, size, "type='signal'");
134 sender = data->name ? : data->owner;
137 offset += snprintf(rule + offset, size - offset,
138 ",sender='%s'", sender);
140 offset += snprintf(rule + offset, size - offset,
141 ",path='%s'", data->path);
143 offset += snprintf(rule + offset, size - offset,
144 ",interface='%s'", data->interface);
146 offset += snprintf(rule + offset, size - offset,
147 ",member='%s'", data->member);
149 snprintf(rule + offset, size - offset,
150 ",arg0='%s'", data->argument);
153 static gboolean add_match(struct filter_data *data,
154 DBusHandleMessageFunction filter)
157 char rule[DBUS_MAXIMUM_MATCH_RULE_LENGTH];
159 format_rule(data, rule, sizeof(rule));
160 dbus_error_init(&err);
162 dbus_bus_add_match(data->connection, rule, &err);
163 if (dbus_error_is_set(&err)) {
164 error("Adding match rule \"%s\" failed: %s", rule,
166 dbus_error_free(&err);
170 data->handle_func = filter;
171 data->registered = TRUE;
176 static gboolean remove_match(struct filter_data *data)
179 char rule[DBUS_MAXIMUM_MATCH_RULE_LENGTH];
181 format_rule(data, rule, sizeof(rule));
183 dbus_error_init(&err);
185 dbus_bus_remove_match(data->connection, rule, &err);
186 if (dbus_error_is_set(&err)) {
187 error("Removing owner match rule for %s failed: %s",
189 dbus_error_free(&err);
196 static struct filter_data *filter_data_get(DBusConnection *connection,
197 DBusHandleMessageFunction filter,
200 const char *interface,
202 const char *argument)
204 struct filter_data *data;
205 const char *name = NULL, *owner = NULL;
207 if (filter_data_find(connection, NULL, NULL, NULL, NULL, NULL, NULL) == NULL) {
208 if (!dbus_connection_add_filter(connection,
209 message_filter, NULL, NULL)) {
210 error("dbus_connection_add_filter() failed");
218 if (sender[0] == ':')
224 data = filter_data_find(connection, name, owner, path, interface,
229 data = g_new0(struct filter_data, 1);
231 data->connection = dbus_connection_ref(connection);
232 data->name = name ? g_strdup(name) : NULL;
233 data->owner = owner ? g_strdup(owner) : NULL;
234 data->path = g_strdup(path);
235 data->interface = g_strdup(interface);
236 data->member = g_strdup(member);
237 data->argument = g_strdup(argument);
239 if (!add_match(data, filter)) {
244 listeners = g_slist_append(listeners, data);
249 static struct filter_callback *filter_data_find_callback(
250 struct filter_data *data,
255 for (l = data->callbacks; l; l = l->next) {
256 struct filter_callback *cb = l->data;
260 for (l = data->processed; l; l = l->next) {
261 struct filter_callback *cb = l->data;
269 static void filter_data_free(struct filter_data *data)
273 for (l = data->callbacks; l != NULL; l = l->next)
276 g_slist_free(data->callbacks);
277 g_dbus_remove_watch(data->connection, data->name_watch);
281 g_free(data->interface);
282 g_free(data->member);
283 g_free(data->argument);
284 dbus_connection_unref(data->connection);
288 static void filter_data_call_and_free(struct filter_data *data)
292 for (l = data->callbacks; l != NULL; l = l->next) {
293 struct filter_callback *cb = l->data;
295 cb->disc_func(data->connection, cb->user_data);
296 if (cb->destroy_func)
297 cb->destroy_func(cb->user_data);
301 filter_data_free(data);
304 static struct filter_callback *filter_data_add_callback(
305 struct filter_data *data,
306 GDBusWatchFunction connect,
307 GDBusWatchFunction disconnect,
308 GDBusSignalFunction signal,
309 GDBusDestroyFunction destroy,
312 struct filter_callback *cb = NULL;
314 cb = g_new0(struct filter_callback, 1);
316 cb->conn_func = connect;
317 cb->disc_func = disconnect;
318 cb->signal_func = signal;
319 cb->destroy_func = destroy;
320 cb->user_data = user_data;
321 cb->id = ++listener_id;
324 data->processed = g_slist_append(data->processed, cb);
326 data->callbacks = g_slist_append(data->callbacks, cb);
331 static void service_data_free(struct service_data *data)
333 struct filter_callback *callback = data->callback;
335 dbus_connection_unref(data->conn);
338 dbus_pending_call_unref(data->call);
341 g_source_remove(data->id);
346 callback->data = NULL;
349 static gboolean filter_data_remove_callback(struct filter_data *data,
350 struct filter_callback *cb)
352 DBusConnection *connection;
354 data->callbacks = g_slist_remove(data->callbacks, cb);
355 data->processed = g_slist_remove(data->processed, cb);
357 /* Cancel pending operations */
360 dbus_pending_call_cancel(cb->data->call);
361 service_data_free(cb->data);
364 if (cb->destroy_func)
365 cb->destroy_func(cb->user_data);
369 /* Don't remove the filter if other callbacks exist or data is lock
370 * processing callbacks */
371 if (data->callbacks || data->lock)
374 if (data->registered && !remove_match(data))
377 connection = dbus_connection_ref(data->connection);
378 listeners = g_slist_remove(listeners, data);
379 filter_data_free(data);
381 /* Remove filter if there are no listeners left for the connection */
382 data = filter_data_find(connection, NULL, NULL, NULL, NULL, NULL,
385 dbus_connection_remove_filter(connection, message_filter,
388 dbus_connection_unref(connection);
393 static DBusHandlerResult signal_filter(DBusConnection *connection,
394 DBusMessage *message, void *user_data)
396 struct filter_data *data = user_data;
397 struct filter_callback *cb;
399 while (data->callbacks) {
400 cb = data->callbacks->data;
402 if (cb->signal_func && !cb->signal_func(connection, message,
404 filter_data_remove_callback(data, cb);
408 /* Check if the watch was removed/freed by the callback
410 if (!g_slist_find(data->callbacks, cb))
413 data->callbacks = g_slist_remove(data->callbacks, cb);
414 data->processed = g_slist_append(data->processed, cb);
417 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
420 static void update_name_cache(const char *name, const char *owner)
424 for (l = listeners; l != NULL; l = l->next) {
425 struct filter_data *data = l->data;
427 if (g_strcmp0(data->name, name) != 0)
431 data->owner = g_strdup(owner);
435 static const char *check_name_cache(const char *name)
439 for (l = listeners; l != NULL; l = l->next) {
440 struct filter_data *data = l->data;
442 if (g_strcmp0(data->name, name) != 0)
451 static DBusHandlerResult service_filter(DBusConnection *connection,
452 DBusMessage *message, void *user_data)
454 struct filter_data *data = user_data;
455 struct filter_callback *cb;
456 char *name, *old, *new;
458 if (!dbus_message_get_args(message, NULL,
459 DBUS_TYPE_STRING, &name,
460 DBUS_TYPE_STRING, &old,
461 DBUS_TYPE_STRING, &new,
462 DBUS_TYPE_INVALID)) {
463 error("Invalid arguments for NameOwnerChanged signal");
464 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
467 update_name_cache(name, new);
469 while (data->callbacks) {
470 cb = data->callbacks->data;
474 cb->disc_func(connection, cb->user_data);
477 cb->conn_func(connection, cb->user_data);
480 /* Check if the watch was removed/freed by the callback
482 if (!g_slist_find(data->callbacks, cb))
485 /* Only auto remove if it is a bus name watch */
486 if (data->argument[0] == ':' &&
487 (cb->conn_func == NULL || cb->disc_func == NULL)) {
488 filter_data_remove_callback(data, cb);
492 data->callbacks = g_slist_remove(data->callbacks, cb);
493 data->processed = g_slist_append(data->processed, cb);
496 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
500 static DBusHandlerResult message_filter(DBusConnection *connection,
501 DBusMessage *message, void *user_data)
503 struct filter_data *data;
504 const char *sender, *path, *iface, *member, *arg = NULL;
506 /* Only filter signals */
507 if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_SIGNAL)
508 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
510 sender = dbus_message_get_sender(message);
511 path = dbus_message_get_path(message);
512 iface = dbus_message_get_interface(message);
513 member = dbus_message_get_member(message);
514 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &arg, DBUS_TYPE_INVALID);
516 /* Sender is always bus name */
517 data = filter_data_find(connection, NULL, sender, path, iface, member,
520 error("Got %s.%s signal which has no listeners", iface, member);
521 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
524 if (data->handle_func) {
527 data->handle_func(connection, message, data);
529 data->callbacks = data->processed;
530 data->processed = NULL;
535 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
539 listeners = g_slist_remove(listeners, data);
540 filter_data_free(data);
542 /* Remove filter if there no listener left for the connection */
543 data = filter_data_find(connection, NULL, NULL, NULL, NULL, NULL,
546 dbus_connection_remove_filter(connection, message_filter,
549 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
552 static gboolean update_service(void *user_data)
554 struct service_data *data = user_data;
555 struct filter_callback *cb = data->callback;
557 update_name_cache(data->name, data->owner);
559 cb->conn_func(data->conn, cb->user_data);
561 service_data_free(data);
566 static void service_reply(DBusPendingCall *call, void *user_data)
568 struct service_data *data = user_data;
572 reply = dbus_pending_call_steal_reply(call);
576 dbus_error_init(&err);
578 if (dbus_set_error_from_message(&err, reply))
581 if (dbus_message_get_args(reply, &err,
582 DBUS_TYPE_STRING, &data->owner,
583 DBUS_TYPE_INVALID) == FALSE)
586 update_service(data);
591 error("%s", err.message);
592 dbus_error_free(&err);
593 service_data_free(data);
595 dbus_message_unref(reply);
598 static void check_service(DBusConnection *connection,
600 struct filter_callback *callback)
602 DBusMessage *message;
603 struct service_data *data;
605 data = g_try_malloc0(sizeof(*data));
607 error("Can't allocate data structure");
611 data->conn = dbus_connection_ref(connection);
612 data->name = g_strdup(name);
613 data->callback = callback;
614 callback->data = data;
616 data->owner = check_name_cache(name);
617 if (data->owner != NULL) {
618 data->id = g_idle_add(update_service, data);
622 message = dbus_message_new_method_call(DBUS_SERVICE_DBUS,
623 DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "GetNameOwner");
624 if (message == NULL) {
625 error("Can't allocate new message");
630 dbus_message_append_args(message, DBUS_TYPE_STRING, &name,
633 if (dbus_connection_send_with_reply(connection, message,
634 &data->call, -1) == FALSE) {
635 error("Failed to execute method call");
640 if (data->call == NULL) {
641 error("D-Bus connection not available");
646 dbus_pending_call_set_notify(data->call, service_reply, data, NULL);
649 dbus_message_unref(message);
652 guint g_dbus_add_service_watch(DBusConnection *connection, const char *name,
653 GDBusWatchFunction connect,
654 GDBusWatchFunction disconnect,
655 void *user_data, GDBusDestroyFunction destroy)
657 struct filter_data *data;
658 struct filter_callback *cb;
663 data = filter_data_get(connection, service_filter, NULL, NULL,
664 DBUS_INTERFACE_DBUS, "NameOwnerChanged",
669 cb = filter_data_add_callback(data, connect, disconnect, NULL, NULL,
675 check_service(connection, name, cb);
680 guint g_dbus_add_disconnect_watch(DBusConnection *connection, const char *name,
681 GDBusWatchFunction func,
682 void *user_data, GDBusDestroyFunction destroy)
684 return g_dbus_add_service_watch(connection, name, NULL, func,
688 guint g_dbus_add_signal_watch(DBusConnection *connection,
689 const char *sender, const char *path,
690 const char *interface, const char *member,
691 GDBusSignalFunction function, void *user_data,
692 GDBusDestroyFunction destroy)
694 struct filter_data *data;
695 struct filter_callback *cb;
697 data = filter_data_get(connection, signal_filter, sender, path,
698 interface, member, NULL);
702 cb = filter_data_add_callback(data, NULL, NULL, function, destroy,
707 if (data->name != NULL && data->name_watch == 0)
708 data->name_watch = g_dbus_add_service_watch(connection,
715 gboolean g_dbus_remove_watch(DBusConnection *connection, guint id)
717 struct filter_data *data;
718 struct filter_callback *cb;
724 for (ldata = listeners; ldata; ldata = ldata->next) {
727 cb = filter_data_find_callback(data, id);
729 filter_data_remove_callback(data, cb);
737 void g_dbus_remove_all_watches(DBusConnection *connection)
739 struct filter_data *data;
741 while ((data = filter_data_find(connection, NULL, NULL, NULL, NULL,
743 listeners = g_slist_remove(listeners, data);
744 filter_data_call_and_free(data);
747 dbus_connection_remove_filter(connection, message_filter, NULL);