Fix blocking service watch initial connect handling
[framework/connectivity/connman.git] / gdbus / watch.c
1 /*
2  *
3  *  D-Bus helper library
4  *
5  *  Copyright (C) 2004-2009  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 name_exit_filter(DBusConnection *connection,
41                                         DBusMessage *message, void *user_data);
42
43 static guint listener_id = 0;
44 static GSList *name_listeners = NULL;
45
46 struct name_callback {
47         GDBusWatchFunction conn_func;
48         GDBusWatchFunction disc_func;
49         void *user_data;
50         guint id;
51 };
52
53 struct name_data {
54         DBusConnection *connection;
55         char *name;
56         GSList *callbacks;
57         GSList *processed;
58         gboolean lock;
59 };
60
61 static struct name_data *name_data_find(DBusConnection *connection,
62                                                         const char *name)
63 {
64         GSList *current;
65
66         for (current = name_listeners;
67                         current != NULL; current = current->next) {
68                 struct name_data *data = current->data;
69
70                 if (connection != data->connection)
71                         continue;
72
73                 if (name == NULL || g_str_equal(name, data->name))
74                         return data;
75         }
76
77         return NULL;
78 }
79
80 static struct name_callback *name_callback_find(GSList *callbacks, guint id)
81 {
82         GSList *current;
83
84         for (current = callbacks; current != NULL; current = current->next) {
85                 struct name_callback *cb = current->data;
86                 if (cb->id == id)
87                         return cb;
88         }
89
90         return NULL;
91 }
92
93 static void name_data_call_and_free(struct name_data *data)
94 {
95         GSList *l;
96
97         for (l = data->callbacks; l != NULL; l = l->next) {
98                 struct name_callback *cb = l->data;
99                 if (cb->disc_func)
100                         cb->disc_func(data->connection, cb->user_data);
101                 g_free(cb);
102         }
103
104         g_slist_free(data->callbacks);
105         g_free(data->name);
106         g_free(data);
107 }
108
109 static void name_data_free(struct name_data *data)
110 {
111         GSList *l;
112
113         for (l = data->callbacks; l != NULL; l = l->next)
114                 g_free(l->data);
115
116         g_slist_free(data->callbacks);
117         g_free(data->name);
118         g_free(data);
119 }
120
121 static int name_data_add(DBusConnection *connection, const char *name,
122                                                 GDBusWatchFunction connect,
123                                                 GDBusWatchFunction disconnect,
124                                                 void *user_data, guint id)
125 {
126         int first = 1;
127         struct name_data *data = NULL;
128         struct name_callback *cb = NULL;
129
130         cb = g_new(struct name_callback, 1);
131
132         cb->conn_func = connect;
133         cb->disc_func = disconnect;
134         cb->user_data = user_data;
135         cb->id = id;
136
137         data = name_data_find(connection, name);
138         if (data) {
139                 first = 0;
140                 goto done;
141         }
142
143         data = g_new0(struct name_data, 1);
144
145         data->connection = connection;
146         data->name = g_strdup(name);
147
148         name_listeners = g_slist_append(name_listeners, data);
149
150 done:
151         if (data->lock)
152                 data->processed = g_slist_append(data->processed, cb);
153         else
154                 data->callbacks = g_slist_append(data->callbacks, cb);
155
156         return first;
157 }
158
159 static void name_data_remove(DBusConnection *connection,
160                                         const char *name, guint id)
161 {
162         struct name_data *data;
163         struct name_callback *cb = NULL;
164
165         data = name_data_find(connection, name);
166         if (!data)
167                 return;
168
169         cb = name_callback_find(data->callbacks, id);
170         if (cb) {
171                 data->callbacks = g_slist_remove(data->callbacks, cb);
172                 g_free(cb);
173         }
174
175         if (data->callbacks)
176                 return;
177
178         name_listeners = g_slist_remove(name_listeners, data);
179         name_data_free(data);
180
181         /* Remove filter if there are no listeners left for the connection */
182         data = name_data_find(connection, NULL);
183         if (!data)
184                 dbus_connection_remove_filter(connection,
185                                                 name_exit_filter,
186                                                 NULL);
187 }
188
189 static gboolean add_match(DBusConnection *connection, const char *name)
190 {
191         DBusError err;
192         char match_string[128];
193
194         snprintf(match_string, sizeof(match_string),
195                         "interface=%s,member=NameOwnerChanged,arg0=%s",
196                         DBUS_INTERFACE_DBUS, name);
197
198         dbus_error_init(&err);
199
200         dbus_bus_add_match(connection, match_string, &err);
201
202         if (dbus_error_is_set(&err)) {
203                 error("Adding match rule \"%s\" failed: %s", match_string,
204                                 err.message);
205                 dbus_error_free(&err);
206                 return FALSE;
207         }
208
209         return TRUE;
210 }
211
212 static gboolean remove_match(DBusConnection *connection, const char *name)
213 {
214         DBusError err;
215         char match_string[128];
216
217         snprintf(match_string, sizeof(match_string),
218                         "interface=%s,member=NameOwnerChanged,arg0=%s",
219                         DBUS_INTERFACE_DBUS, name);
220
221         dbus_error_init(&err);
222
223         dbus_bus_remove_match(connection, match_string, &err);
224
225         if (dbus_error_is_set(&err)) {
226                 error("Removing owner match rule for %s failed: %s",
227                                 name, err.message);
228                 dbus_error_free(&err);
229                 return FALSE;
230         }
231
232         return TRUE;
233 }
234
235 static DBusHandlerResult name_exit_filter(DBusConnection *connection,
236                                         DBusMessage *message, void *user_data)
237 {
238         struct name_data *data;
239         struct name_callback *cb;
240         char *name, *old, *new;
241
242         if (!dbus_message_is_signal(message, DBUS_INTERFACE_DBUS,
243                                                         "NameOwnerChanged"))
244                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
245
246         if (!dbus_message_get_args(message, NULL,
247                                 DBUS_TYPE_STRING, &name,
248                                 DBUS_TYPE_STRING, &old,
249                                 DBUS_TYPE_STRING, &new,
250                                 DBUS_TYPE_INVALID)) {
251                 error("Invalid arguments for NameOwnerChanged signal");
252                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
253         }
254
255         data = name_data_find(connection, name);
256         if (!data) {
257                 error("Got NameOwnerChanged signal for %s which has no listeners", name);
258                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
259         }
260
261         data->lock = TRUE;
262
263         while (data->callbacks) {
264                 cb = data->callbacks->data;
265
266                 if (*new == '\0') {
267                         if (cb->disc_func)
268                                 cb->disc_func(connection, cb->user_data);
269                 } else {
270                         if (cb->conn_func)
271                                 cb->conn_func(connection, cb->user_data);
272                 }
273
274                 /* Check if the watch was removed/freed by the callback
275                  * function */
276                 if (!g_slist_find(data->callbacks, cb))
277                         continue;
278
279                 data->callbacks = g_slist_remove(data->callbacks, cb);
280
281                 if (!cb->conn_func || !cb->disc_func) {
282                         g_free(cb);
283                         continue;
284                 }
285
286                 data->processed = g_slist_append(data->processed, cb);
287         }
288
289         data->callbacks = data->processed;
290         data->processed = NULL;
291         data->lock = FALSE;
292
293         if (data->callbacks)
294                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
295
296         name_listeners = g_slist_remove(name_listeners, data);
297         name_data_free(data);
298
299         /* Remove filter if there no listener left for the connection */
300         data = name_data_find(connection, NULL);
301         if (!data)
302                 dbus_connection_remove_filter(connection, name_exit_filter,
303                                                 NULL);
304
305         remove_match(connection, name);
306
307         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
308 }
309
310 struct service_data {
311         DBusConnection *conn;
312         const char *name;
313         GDBusWatchFunction conn_func;
314         void *user_data;
315 };
316
317 static void service_reply(DBusPendingCall *call, void *user_data)
318 {
319         struct service_data *data = user_data;
320         DBusMessage *reply;
321         DBusError error;
322         char **names;
323         int i, count;
324
325         reply = dbus_pending_call_steal_reply(call);
326         if (reply == NULL)
327                 return;
328
329         dbus_error_init(&error);
330
331         if (dbus_message_get_args(reply, &error,
332                         DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &names, &count,
333                                                 DBUS_TYPE_INVALID) == FALSE) {
334                 if (dbus_error_is_set(&error) == TRUE) {
335                         error("%s", error.message);
336                         dbus_error_free(&error);
337                 } else {
338                         error("Wrong arguments for name list");
339                 }
340                 goto done;
341         }
342
343         for (i = 0; i < count; i++)
344                 if (g_strcmp0(names[i], data->name) == 0) {
345                         if (data->conn_func)
346                                 data->conn_func(data->conn, data->user_data);
347                         break;
348                 }
349
350         g_strfreev(names);
351
352 done:
353         dbus_message_unref(reply);
354 }
355
356 static void check_service(DBusConnection *connection, const char *name,
357                                 GDBusWatchFunction connect, void *user_data)
358 {
359         DBusMessage *message;
360         DBusPendingCall *call;
361         struct service_data *data;
362
363         data = g_try_malloc0(sizeof(*data));
364         if (data == NULL) {
365                 error("Can't allocate data structure");
366                 return;
367         }
368
369         data->conn = connection;
370         data->name = name;
371         data->conn_func = connect;
372         data->user_data = user_data;
373
374         message = dbus_message_new_method_call(DBUS_SERVICE_DBUS,
375                         DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "ListNames");
376         if (message == NULL) {
377                 error("Can't allocate new message");
378                 g_free(data);
379                 return;
380         }
381
382         if (dbus_connection_send_with_reply(connection, message,
383                                                         &call, -1) == FALSE) {
384                 error("Failed to execute method call");
385                 g_free(data);
386                 goto done;
387         }
388
389         if (call == NULL) {
390                 error("D-Bus connection not available");
391                 g_free(data);
392                 goto done;
393         }
394
395         dbus_pending_call_set_notify(call, service_reply, data, NULL);
396
397 done:
398         dbus_message_unref(message);
399 }
400
401 guint g_dbus_add_service_watch(DBusConnection *connection, const char *name,
402                                 GDBusWatchFunction connect,
403                                 GDBusWatchFunction disconnect,
404                                 void *user_data, GDBusDestroyFunction destroy)
405 {
406         int first;
407
408         if (!name_data_find(connection, NULL)) {
409                 if (!dbus_connection_add_filter(connection,
410                                         name_exit_filter, NULL, NULL)) {
411                         error("dbus_connection_add_filter() failed");
412                         return 0;
413                 }
414         }
415
416         listener_id++;
417         first = name_data_add(connection, name, connect, disconnect,
418                                                 user_data, listener_id);
419         /* The filter is already added if this is not the first callback
420          * registration for the name */
421         if (!first)
422                 goto done;
423
424         if (name) {
425                 debug("name_listener_add(%s)", name);
426
427                 if (!add_match(connection, name)) {
428                         name_data_remove(connection, name, listener_id);
429                         return 0;
430                 }
431         }
432
433 done:
434         if (connect)
435                 check_service(connection, name, connect, user_data);
436
437         return listener_id;
438 }
439
440 guint g_dbus_add_disconnect_watch(DBusConnection *connection, const char *name,
441                                 GDBusWatchFunction func,
442                                 void *user_data, GDBusDestroyFunction destroy)
443 {
444         return g_dbus_add_service_watch(connection, name, NULL, func,
445                                                         user_data, destroy);
446 }
447
448 guint g_dbus_add_signal_watch(DBusConnection *connection,
449                                 const char *rule, GDBusSignalFunction function,
450                                 void *user_data, GDBusDestroyFunction destroy)
451 {
452         return 0;
453 }
454
455 gboolean g_dbus_remove_watch(DBusConnection *connection, guint id)
456 {
457         struct name_data *data;
458         struct name_callback *cb;
459         GSList *ldata, *lcb;
460
461         if (id == 0)
462                 return FALSE;
463
464         for (ldata = name_listeners; ldata; ldata = ldata->next) {
465                 data = ldata->data;
466                 for (lcb = data->callbacks; lcb; lcb = lcb->next) {
467                         cb = lcb->data;
468                         if (cb->id == id)
469                                 goto remove;
470                 }
471                 for (lcb = data->processed; lcb; lcb = lcb->next) {
472                         cb = lcb->data;
473                         if (cb->id == id)
474                                 goto remove;
475                 }
476         }
477
478         return FALSE;
479
480 remove:
481         data->callbacks = g_slist_remove(data->callbacks, cb);
482         data->processed = g_slist_remove(data->processed, cb);
483         g_free(cb);
484
485         /* Don't remove the filter if other callbacks exist or data is lock
486          * processing callbacks */
487         if (data->callbacks || data->lock)
488                 return TRUE;
489
490         if (data->name) {
491                 if (!remove_match(data->connection, data->name))
492                         return FALSE;
493         }
494
495         name_listeners = g_slist_remove(name_listeners, data);
496         name_data_free(data);
497
498         /* Remove filter if there are no listeners left for the connection */
499         data = name_data_find(connection, NULL);
500         if (!data)
501                 dbus_connection_remove_filter(connection, name_exit_filter,
502                                                 NULL);
503
504         return TRUE;
505 }
506
507 void g_dbus_remove_all_watches(DBusConnection *connection)
508 {
509         struct name_data *data;
510
511         while ((data = name_data_find(connection, NULL))) {
512                 name_listeners = g_slist_remove(name_listeners, data);
513                 name_data_call_and_free(data);
514         }
515
516         dbus_connection_remove_filter(connection, name_exit_filter, NULL);
517 }