Base Code merged to SPIN 2.4
[platform/upstream/connman.git] / gdbus / watch.c
1 /*
2  *
3  *  D-Bus helper library
4  *
5  *  Copyright (C) 2004-2011  Marcel Holtmann <marcel@holtmann.org>
6  *
7  *
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.
12  *
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.
17  *
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
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         /* 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,
287                                                                         NULL);
288
289         for (l = data->callbacks; l != NULL; l = l->next)
290                 g_free(l->data);
291
292         g_slist_free(data->callbacks);
293         g_dbus_remove_watch(data->connection, data->name_watch);
294         g_free(data->name);
295         g_free(data->owner);
296         g_free(data->path);
297         g_free(data->interface);
298         g_free(data->member);
299         g_free(data->argument);
300         dbus_connection_unref(data->connection);
301         g_free(data);
302 }
303
304 static void filter_data_call_and_free(struct filter_data *data)
305 {
306         GSList *l;
307
308         for (l = data->callbacks; l != NULL; l = l->next) {
309                 struct filter_callback *cb = l->data;
310                 if (cb->disc_func)
311                         cb->disc_func(data->connection, cb->user_data);
312                 if (cb->destroy_func)
313                         cb->destroy_func(cb->user_data);
314                 g_free(cb);
315         }
316
317         filter_data_free(data);
318 }
319
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,
326                                                 void *user_data)
327 {
328         struct filter_callback *cb = NULL;
329
330         cb = g_new0(struct filter_callback, 1);
331
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;
338
339         if (data->lock)
340                 data->processed = g_slist_append(data->processed, cb);
341         else
342                 data->callbacks = g_slist_append(data->callbacks, cb);
343
344         return cb;
345 }
346
347 static void service_data_free(struct service_data *data)
348 {
349         struct filter_callback *callback = data->callback;
350
351         dbus_connection_unref(data->conn);
352
353         if (data->call)
354                 dbus_pending_call_unref(data->call);
355
356         if (data->id)
357                 g_source_remove(data->id);
358
359         g_free(data->name);
360         g_free(data);
361
362         callback->data = NULL;
363 }
364
365 /* Returns TRUE if data is freed */
366 static gboolean filter_data_remove_callback(struct filter_data *data,
367                                                 struct filter_callback *cb)
368 {
369         data->callbacks = g_slist_remove(data->callbacks, cb);
370         data->processed = g_slist_remove(data->processed, cb);
371
372         /* Cancel pending operations */
373         if (cb->data) {
374                 if (cb->data->call)
375                         dbus_pending_call_cancel(cb->data->call);
376                 service_data_free(cb->data);
377         }
378
379         if (cb->destroy_func)
380                 cb->destroy_func(cb->user_data);
381
382         g_free(cb);
383
384         /* Don't remove the filter if other callbacks exist or data is lock
385          * processing callbacks */
386         if (data->callbacks || data->lock)
387                 return FALSE;
388
389         if (data->registered && !remove_match(data))
390                 return FALSE;
391
392         listeners = g_slist_remove(listeners, data);
393         filter_data_free(data);
394
395         return TRUE;
396 }
397
398 static DBusHandlerResult signal_filter(DBusConnection *connection,
399                                         DBusMessage *message, void *user_data)
400 {
401         struct filter_data *data = user_data;
402         struct filter_callback *cb;
403
404         while (data->callbacks) {
405                 cb = data->callbacks->data;
406
407                 if (cb->signal_func && !cb->signal_func(connection, message,
408                                                         cb->user_data)) {
409                         if (filter_data_remove_callback(data, cb))
410                                 break;
411
412                         continue;
413                 }
414
415                 /* Check if the watch was removed/freed by the callback
416                  * function */
417                 if (!g_slist_find(data->callbacks, cb))
418                         continue;
419
420                 data->callbacks = g_slist_remove(data->callbacks, cb);
421                 data->processed = g_slist_append(data->processed, cb);
422         }
423
424         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
425 }
426
427 static void update_name_cache(const char *name, const char *owner)
428 {
429         GSList *l;
430
431         for (l = listeners; l != NULL; l = l->next) {
432                 struct filter_data *data = l->data;
433
434                 if (g_strcmp0(data->name, name) != 0)
435                         continue;
436
437                 g_free(data->owner);
438                 data->owner = g_strdup(owner);
439         }
440 }
441
442 static const char *check_name_cache(const char *name)
443 {
444         GSList *l;
445
446         for (l = listeners; l != NULL; l = l->next) {
447                 struct filter_data *data = l->data;
448
449                 if (g_strcmp0(data->name, name) != 0)
450                         continue;
451
452                 return data->owner;
453         }
454
455         return NULL;
456 }
457
458 static DBusHandlerResult service_filter(DBusConnection *connection,
459                                         DBusMessage *message, void *user_data)
460 {
461         struct filter_data *data = user_data;
462         struct filter_callback *cb;
463         char *name, *old, *new;
464
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;
472         }
473
474         update_name_cache(name, new);
475
476         while (data->callbacks) {
477                 cb = data->callbacks->data;
478
479                 if (*new == '\0') {
480                         if (cb->disc_func)
481                                 cb->disc_func(connection, cb->user_data);
482                 } else {
483                         if (cb->conn_func)
484                                 cb->conn_func(connection, cb->user_data);
485                 }
486
487                 /* Check if the watch was removed/freed by the callback
488                  * function */
489                 if (!g_slist_find(data->callbacks, cb))
490                         continue;
491
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))
496                                 break;
497
498                         continue;
499                 }
500
501                 data->callbacks = g_slist_remove(data->callbacks, cb);
502                 data->processed = g_slist_append(data->processed, cb);
503         }
504
505         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
506 }
507
508
509 static DBusHandlerResult message_filter(DBusConnection *connection,
510                                         DBusMessage *message, void *user_data)
511 {
512         struct filter_data *data;
513         const char *sender, *path, *iface, *member, *arg = NULL;
514         GSList *current, *delete_listener = NULL;
515
516         /* Only filter signals */
517         if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_SIGNAL)
518                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
519
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);
525
526         /* If sender != NULL it is always the owner */
527
528         for (current = listeners; current != NULL; current = current->next) {
529                 data = current->data;
530
531                 if (connection != data->connection)
532                         continue;
533
534                 if (!sender && data->owner)
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         if (delete_listener == NULL)
570                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
571
572         for (current = delete_listener; current != NULL;
573                                         current = delete_listener->next) {
574                 GSList *l = current->data;
575
576                 data = l->data;
577
578                 /* Has any other callback added callbacks back to this data? */
579                 if (data->callbacks != NULL)
580                         continue;
581
582                 remove_match(data);
583                 listeners = g_slist_delete_link(listeners, l);
584
585                 filter_data_free(data);
586         }
587
588         g_slist_free(delete_listener);
589
590         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
591 }
592
593 static gboolean update_service(void *user_data)
594 {
595         struct service_data *data = user_data;
596         struct filter_callback *cb = data->callback;
597         DBusConnection *conn;
598
599         conn = dbus_connection_ref(data->conn);
600         service_data_free(data);
601
602         if (cb->conn_func)
603                 cb->conn_func(conn, cb->user_data);
604
605         dbus_connection_unref(conn);
606
607         return FALSE;
608 }
609
610 static void service_reply(DBusPendingCall *call, void *user_data)
611 {
612         struct service_data *data = user_data;
613         DBusMessage *reply;
614         DBusError err;
615
616         reply = dbus_pending_call_steal_reply(call);
617         if (reply == NULL)
618                 return;
619
620         dbus_error_init(&err);
621
622         if (dbus_set_error_from_message(&err, reply))
623                 goto fail;
624
625         if (dbus_message_get_args(reply, &err,
626                                         DBUS_TYPE_STRING, &data->owner,
627                                                 DBUS_TYPE_INVALID) == FALSE)
628                 goto fail;
629
630         update_service(data);
631
632         goto done;
633
634 fail:
635         error("%s", err.message);
636         dbus_error_free(&err);
637         service_data_free(data);
638 done:
639         dbus_message_unref(reply);
640 }
641
642 static void check_service(DBusConnection *connection,
643                                         const char *name,
644                                         struct filter_callback *callback)
645 {
646         DBusMessage *message;
647         struct service_data *data;
648
649         data = g_try_malloc0(sizeof(*data));
650         if (data == NULL) {
651                 error("Can't allocate data structure");
652                 return;
653         }
654
655         data->conn = dbus_connection_ref(connection);
656         data->name = g_strdup(name);
657         data->callback = callback;
658         callback->data = data;
659
660         data->owner = check_name_cache(name);
661         if (data->owner != NULL) {
662                 data->id = g_idle_add(update_service, data);
663                 return;
664         }
665
666         message = dbus_message_new_method_call(DBUS_SERVICE_DBUS,
667                         DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "GetNameOwner");
668         if (message == NULL) {
669                 error("Can't allocate new message");
670                 g_free(data);
671                 return;
672         }
673
674         dbus_message_append_args(message, DBUS_TYPE_STRING, &name,
675                                                         DBUS_TYPE_INVALID);
676
677         if (dbus_connection_send_with_reply(connection, message,
678                                                         &data->call, -1) == FALSE) {
679                 error("Failed to execute method call");
680                 g_free(data);
681                 goto done;
682         }
683
684         if (data->call == NULL) {
685                 error("D-Bus connection not available");
686                 g_free(data);
687                 goto done;
688         }
689
690         dbus_pending_call_set_notify(data->call, service_reply, data, NULL);
691
692 done:
693         dbus_message_unref(message);
694 }
695
696 guint g_dbus_add_service_watch(DBusConnection *connection, const char *name,
697                                 GDBusWatchFunction connect,
698                                 GDBusWatchFunction disconnect,
699                                 void *user_data, GDBusDestroyFunction destroy)
700 {
701         struct filter_data *data;
702         struct filter_callback *cb;
703
704         if (name == NULL)
705                 return 0;
706
707         data = filter_data_get(connection, service_filter,
708                                 DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
709                                 DBUS_INTERFACE_DBUS, "NameOwnerChanged",
710                                 name);
711         if (data == NULL)
712                 return 0;
713
714         cb = filter_data_add_callback(data, connect, disconnect, NULL, destroy,
715                                         user_data);
716         if (cb == NULL)
717                 return 0;
718
719         if (connect)
720                 check_service(connection, name, cb);
721
722         return cb->id;
723 }
724
725 guint g_dbus_add_disconnect_watch(DBusConnection *connection, const char *name,
726                                 GDBusWatchFunction func,
727                                 void *user_data, GDBusDestroyFunction destroy)
728 {
729         return g_dbus_add_service_watch(connection, name, NULL, func,
730                                                         user_data, destroy);
731 }
732
733 guint g_dbus_add_signal_watch(DBusConnection *connection,
734                                 const char *sender, const char *path,
735                                 const char *interface, const char *member,
736                                 GDBusSignalFunction function, void *user_data,
737                                 GDBusDestroyFunction destroy)
738 {
739         struct filter_data *data;
740         struct filter_callback *cb;
741
742         data = filter_data_get(connection, signal_filter, sender, path,
743                                 interface, member, NULL);
744         if (data == NULL)
745                 return 0;
746
747         cb = filter_data_add_callback(data, NULL, NULL, function, destroy,
748                                         user_data);
749         if (cb == NULL)
750                 return 0;
751
752         if (data->name != NULL && data->name_watch == 0)
753                 data->name_watch = g_dbus_add_service_watch(connection,
754                                                         data->name, NULL,
755                                                         NULL, NULL, NULL);
756
757         return cb->id;
758 }
759
760 guint g_dbus_add_properties_watch(DBusConnection *connection,
761                                 const char *sender, const char *path,
762                                 const char *interface,
763                                 GDBusSignalFunction function, void *user_data,
764                                 GDBusDestroyFunction destroy)
765 {
766         struct filter_data *data;
767         struct filter_callback *cb;
768
769         data = filter_data_get(connection, signal_filter, sender, path,
770                                 DBUS_INTERFACE_PROPERTIES, "PropertiesChanged",
771                                 interface);
772         if (data == NULL)
773                 return 0;
774
775         cb = filter_data_add_callback(data, NULL, NULL, function, destroy,
776                                         user_data);
777         if (cb == NULL)
778                 return 0;
779
780         if (data->name != NULL && data->name_watch == 0)
781                 data->name_watch = g_dbus_add_service_watch(connection,
782                                                         data->name, NULL,
783                                                         NULL, NULL, NULL);
784
785         return cb->id;
786 }
787
788 gboolean g_dbus_remove_watch(DBusConnection *connection, guint id)
789 {
790         struct filter_data *data;
791         struct filter_callback *cb;
792         GSList *ldata;
793
794         if (id == 0)
795                 return FALSE;
796
797         for (ldata = listeners; ldata; ldata = ldata->next) {
798                 data = ldata->data;
799
800                 cb = filter_data_find_callback(data, id);
801                 if (cb) {
802                         filter_data_remove_callback(data, cb);
803                         return TRUE;
804                 }
805         }
806
807         return FALSE;
808 }
809
810 void g_dbus_remove_all_watches(DBusConnection *connection)
811 {
812         struct filter_data *data;
813
814         while ((data = filter_data_find(connection))) {
815                 listeners = g_slist_remove(listeners, data);
816                 filter_data_call_and_free(data);
817         }
818 }