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_match(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 (g_strcmp0(name, data->name) != 0)
101 if (g_strcmp0(owner, data->owner) != 0)
104 if (g_strcmp0(path, data->path) != 0)
107 if (g_strcmp0(interface, data->interface) != 0)
110 if (g_strcmp0(member, data->member) != 0)
113 if (g_strcmp0(argument, data->argument) != 0)
122 static struct filter_data *filter_data_find(DBusConnection *connection)
126 for (current = listeners;
127 current != NULL; current = current->next) {
128 struct filter_data *data = current->data;
130 if (connection != data->connection)
139 static void format_rule(struct filter_data *data, char *rule, size_t size)
144 offset = snprintf(rule, size, "type='signal'");
145 sender = data->name ? : data->owner;
148 offset += snprintf(rule + offset, size - offset,
149 ",sender='%s'", sender);
151 offset += snprintf(rule + offset, size - offset,
152 ",path='%s'", data->path);
154 offset += snprintf(rule + offset, size - offset,
155 ",interface='%s'", data->interface);
157 offset += snprintf(rule + offset, size - offset,
158 ",member='%s'", data->member);
160 snprintf(rule + offset, size - offset,
161 ",arg0='%s'", data->argument);
164 static gboolean add_match(struct filter_data *data,
165 DBusHandleMessageFunction filter)
168 char rule[DBUS_MAXIMUM_MATCH_RULE_LENGTH];
170 format_rule(data, rule, sizeof(rule));
171 dbus_error_init(&err);
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,
177 dbus_error_free(&err);
181 data->handle_func = filter;
182 data->registered = TRUE;
187 static gboolean remove_match(struct filter_data *data)
190 char rule[DBUS_MAXIMUM_MATCH_RULE_LENGTH];
192 format_rule(data, rule, sizeof(rule));
194 dbus_error_init(&err);
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",
200 dbus_error_free(&err);
207 static struct filter_data *filter_data_get(DBusConnection *connection,
208 DBusHandleMessageFunction filter,
211 const char *interface,
213 const char *argument)
215 struct filter_data *data;
216 const char *name = NULL, *owner = NULL;
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");
229 if (sender[0] == ':')
235 data = filter_data_find_match(connection, name, owner, path,
236 interface, member, argument);
240 data = g_new0(struct filter_data, 1);
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);
250 if (!add_match(data, filter)) {
255 listeners = g_slist_append(listeners, data);
260 static struct filter_callback *filter_data_find_callback(
261 struct filter_data *data,
266 for (l = data->callbacks; l; l = l->next) {
267 struct filter_callback *cb = l->data;
271 for (l = data->processed; l; l = l->next) {
272 struct filter_callback *cb = l->data;
280 static void filter_data_free(struct filter_data *data)
284 /* Remove filter if there are no listeners left for the connection */
285 if (filter_data_find(data->connection) == NULL)
286 dbus_connection_remove_filter(data->connection, message_filter,
289 for (l = data->callbacks; l != NULL; l = l->next)
292 g_slist_free(data->callbacks);
293 g_dbus_remove_watch(data->connection, data->name_watch);
297 g_free(data->interface);
298 g_free(data->member);
299 g_free(data->argument);
300 dbus_connection_unref(data->connection);
304 static void filter_data_call_and_free(struct filter_data *data)
308 for (l = data->callbacks; l != NULL; l = l->next) {
309 struct filter_callback *cb = l->data;
311 cb->disc_func(data->connection, cb->user_data);
312 if (cb->destroy_func)
313 cb->destroy_func(cb->user_data);
317 filter_data_free(data);
320 static struct filter_callback *filter_data_add_callback(
321 struct filter_data *data,
322 GDBusWatchFunction connect,
323 GDBusWatchFunction disconnect,
324 GDBusSignalFunction signal,
325 GDBusDestroyFunction destroy,
328 struct filter_callback *cb = NULL;
330 cb = g_new0(struct filter_callback, 1);
332 cb->conn_func = connect;
333 cb->disc_func = disconnect;
334 cb->signal_func = signal;
335 cb->destroy_func = destroy;
336 cb->user_data = user_data;
337 cb->id = ++listener_id;
340 data->processed = g_slist_append(data->processed, cb);
342 data->callbacks = g_slist_append(data->callbacks, cb);
347 static void service_data_free(struct service_data *data)
349 struct filter_callback *callback = data->callback;
351 dbus_connection_unref(data->conn);
354 dbus_pending_call_unref(data->call);
357 g_source_remove(data->id);
362 callback->data = NULL;
365 /* Returns TRUE if data is freed */
366 static gboolean filter_data_remove_callback(struct filter_data *data,
367 struct filter_callback *cb)
369 data->callbacks = g_slist_remove(data->callbacks, cb);
370 data->processed = g_slist_remove(data->processed, cb);
372 /* Cancel pending operations */
375 dbus_pending_call_cancel(cb->data->call);
376 service_data_free(cb->data);
379 if (cb->destroy_func)
380 cb->destroy_func(cb->user_data);
384 /* Don't remove the filter if other callbacks exist or data is lock
385 * processing callbacks */
386 if (data->callbacks || data->lock)
389 if (data->registered && !remove_match(data))
392 listeners = g_slist_remove(listeners, data);
393 filter_data_free(data);
398 static DBusHandlerResult signal_filter(DBusConnection *connection,
399 DBusMessage *message, void *user_data)
401 struct filter_data *data = user_data;
402 struct filter_callback *cb;
404 while (data->callbacks) {
405 cb = data->callbacks->data;
407 if (cb->signal_func && !cb->signal_func(connection, message,
409 if (filter_data_remove_callback(data, cb))
415 /* Check if the watch was removed/freed by the callback
417 if (!g_slist_find(data->callbacks, cb))
420 data->callbacks = g_slist_remove(data->callbacks, cb);
421 data->processed = g_slist_append(data->processed, cb);
424 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
427 static void update_name_cache(const char *name, const char *owner)
431 for (l = listeners; l != NULL; l = l->next) {
432 struct filter_data *data = l->data;
434 if (g_strcmp0(data->name, name) != 0)
438 data->owner = g_strdup(owner);
442 static const char *check_name_cache(const char *name)
446 for (l = listeners; l != NULL; l = l->next) {
447 struct filter_data *data = l->data;
449 if (g_strcmp0(data->name, name) != 0)
458 static DBusHandlerResult service_filter(DBusConnection *connection,
459 DBusMessage *message, void *user_data)
461 struct filter_data *data = user_data;
462 struct filter_callback *cb;
463 char *name, *old, *new;
465 if (!dbus_message_get_args(message, NULL,
466 DBUS_TYPE_STRING, &name,
467 DBUS_TYPE_STRING, &old,
468 DBUS_TYPE_STRING, &new,
469 DBUS_TYPE_INVALID)) {
470 error("Invalid arguments for NameOwnerChanged signal");
471 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
474 update_name_cache(name, new);
476 while (data->callbacks) {
477 cb = data->callbacks->data;
481 cb->disc_func(connection, cb->user_data);
484 cb->conn_func(connection, cb->user_data);
487 /* Check if the watch was removed/freed by the callback
489 if (!g_slist_find(data->callbacks, cb))
492 /* Only auto remove if it is a bus name watch */
493 if (data->argument[0] == ':' &&
494 (cb->conn_func == NULL || cb->disc_func == NULL)) {
495 if (filter_data_remove_callback(data, cb))
501 data->callbacks = g_slist_remove(data->callbacks, cb);
502 data->processed = g_slist_append(data->processed, cb);
505 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
509 static DBusHandlerResult message_filter(DBusConnection *connection,
510 DBusMessage *message, void *user_data)
512 struct filter_data *data;
513 const char *sender, *path, *iface, *member, *arg = NULL;
514 GSList *current, *delete_listener = NULL;
516 /* Only filter signals */
517 if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_SIGNAL)
518 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
520 sender = dbus_message_get_sender(message);
521 path = dbus_message_get_path(message);
522 iface = dbus_message_get_interface(message);
523 member = dbus_message_get_member(message);
524 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &arg, DBUS_TYPE_INVALID);
526 /* Sender is always the owner */
528 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
530 for (current = listeners; current != NULL; current = current->next) {
531 data = current->data;
533 if (connection != data->connection)
536 if (data->owner && g_str_equal(sender, data->owner) == FALSE)
539 if (data->path && g_str_equal(path, data->path) == FALSE)
542 if (data->interface && g_str_equal(iface,
543 data->interface) == FALSE)
546 if (data->member && g_str_equal(member, data->member) == FALSE)
549 if (data->argument && g_str_equal(arg,
550 data->argument) == FALSE)
553 if (data->handle_func) {
556 data->handle_func(connection, message, data);
558 data->callbacks = data->processed;
559 data->processed = NULL;
563 if (!data->callbacks)
564 delete_listener = g_slist_prepend(delete_listener,
568 if (delete_listener == NULL)
569 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
571 for (current = delete_listener; current != NULL;
572 current = delete_listener->next) {
573 GSList *l = current->data;
577 /* Has any other callback added callbacks back to this data? */
578 if (data->callbacks != NULL)
582 listeners = g_slist_delete_link(listeners, l);
584 filter_data_free(data);
587 g_slist_free(delete_listener);
589 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
592 static gboolean update_service(void *user_data)
594 struct service_data *data = user_data;
595 struct filter_callback *cb = data->callback;
596 DBusConnection *conn;
598 conn = dbus_connection_ref(data->conn);
599 service_data_free(data);
602 cb->conn_func(conn, cb->user_data);
604 dbus_connection_unref(conn);
609 static void service_reply(DBusPendingCall *call, void *user_data)
611 struct service_data *data = user_data;
615 reply = dbus_pending_call_steal_reply(call);
619 dbus_error_init(&err);
621 if (dbus_set_error_from_message(&err, reply))
624 if (dbus_message_get_args(reply, &err,
625 DBUS_TYPE_STRING, &data->owner,
626 DBUS_TYPE_INVALID) == FALSE)
629 update_service(data);
634 error("%s", err.message);
635 dbus_error_free(&err);
636 service_data_free(data);
638 dbus_message_unref(reply);
641 static void check_service(DBusConnection *connection,
643 struct filter_callback *callback)
645 DBusMessage *message;
646 struct service_data *data;
648 data = g_try_malloc0(sizeof(*data));
650 error("Can't allocate data structure");
654 data->conn = dbus_connection_ref(connection);
655 data->name = g_strdup(name);
656 data->callback = callback;
657 callback->data = data;
659 data->owner = check_name_cache(name);
660 if (data->owner != NULL) {
661 data->id = g_idle_add(update_service, data);
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");
673 dbus_message_append_args(message, DBUS_TYPE_STRING, &name,
676 if (dbus_connection_send_with_reply(connection, message,
677 &data->call, -1) == FALSE) {
678 error("Failed to execute method call");
683 if (data->call == NULL) {
684 error("D-Bus connection not available");
689 dbus_pending_call_set_notify(data->call, service_reply, data, NULL);
692 dbus_message_unref(message);
695 guint g_dbus_add_service_watch(DBusConnection *connection, const char *name,
696 GDBusWatchFunction connect,
697 GDBusWatchFunction disconnect,
698 void *user_data, GDBusDestroyFunction destroy)
700 struct filter_data *data;
701 struct filter_callback *cb;
706 data = filter_data_get(connection, service_filter,
707 DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
708 DBUS_INTERFACE_DBUS, "NameOwnerChanged",
713 cb = filter_data_add_callback(data, connect, disconnect, NULL, destroy,
719 check_service(connection, name, cb);
724 guint g_dbus_add_disconnect_watch(DBusConnection *connection, const char *name,
725 GDBusWatchFunction func,
726 void *user_data, GDBusDestroyFunction destroy)
728 return g_dbus_add_service_watch(connection, name, NULL, func,
732 guint g_dbus_add_signal_watch(DBusConnection *connection,
733 const char *sender, const char *path,
734 const char *interface, const char *member,
735 GDBusSignalFunction function, void *user_data,
736 GDBusDestroyFunction destroy)
738 struct filter_data *data;
739 struct filter_callback *cb;
741 data = filter_data_get(connection, signal_filter, sender, path,
742 interface, member, NULL);
746 cb = filter_data_add_callback(data, NULL, NULL, function, destroy,
751 if (data->name != NULL && data->name_watch == 0)
752 data->name_watch = g_dbus_add_service_watch(connection,
759 guint g_dbus_add_properties_watch(DBusConnection *connection,
760 const char *sender, const char *path,
761 const char *interface,
762 GDBusSignalFunction function, void *user_data,
763 GDBusDestroyFunction destroy)
765 struct filter_data *data;
766 struct filter_callback *cb;
768 data = filter_data_get(connection, signal_filter, sender, path,
769 DBUS_INTERFACE_PROPERTIES, "PropertiesChanged",
774 cb = filter_data_add_callback(data, NULL, NULL, function, destroy,
779 if (data->name != NULL && data->name_watch == 0)
780 data->name_watch = g_dbus_add_service_watch(connection,
787 gboolean g_dbus_remove_watch(DBusConnection *connection, guint id)
789 struct filter_data *data;
790 struct filter_callback *cb;
796 for (ldata = listeners; ldata; ldata = ldata->next) {
799 cb = filter_data_find_callback(data, id);
801 filter_data_remove_callback(data, cb);
809 void g_dbus_remove_all_watches(DBusConnection *connection)
811 struct filter_data *data;
813 while ((data = filter_data_find(connection))) {
814 listeners = g_slist_remove(listeners, data);
815 filter_data_call_and_free(data);