dnsproxy: Only one copy of the relevant buffers will be made to a TCP request
[framework/connectivity/connman.git] / gdbus / watch.c
index 8ad4815..9a716b0 100644 (file)
@@ -2,7 +2,7 @@
  *
  *  D-Bus helper library
  *
- *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
+ *  Copyright (C) 2004-2011  Marcel Holtmann <marcel@holtmann.org>
  *
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -43,11 +43,21 @@ static DBusHandlerResult message_filter(DBusConnection *connection,
 static guint listener_id = 0;
 static GSList *listeners = NULL;
 
+struct service_data {
+       DBusConnection *conn;
+       DBusPendingCall *call;
+       char *name;
+       const char *owner;
+       guint id;
+       struct filter_callback *callback;
+};
+
 struct filter_callback {
        GDBusWatchFunction conn_func;
        GDBusWatchFunction disc_func;
        GDBusSignalFunction signal_func;
        GDBusDestroyFunction destroy_func;
+       struct service_data *data;
        void *user_data;
        guint id;
 };
@@ -194,8 +204,7 @@ static struct filter_data *filter_data_get(DBusConnection *connection,
        struct filter_data *data;
        const char *name = NULL, *owner = NULL;
 
-       if (!filter_data_find(connection, NULL, NULL, NULL, NULL, NULL,
-                               NULL)) {
+       if (filter_data_find(connection, NULL, NULL, NULL, NULL, NULL, NULL) == NULL) {
                if (!dbus_connection_add_filter(connection,
                                        message_filter, NULL, NULL)) {
                        error("dbus_connection_add_filter() failed");
@@ -302,7 +311,7 @@ static struct filter_callback *filter_data_add_callback(
 {
        struct filter_callback *cb = NULL;
 
-       cb = g_new(struct filter_callback, 1);
+       cb = g_new0(struct filter_callback, 1);
 
        cb->conn_func = connect;
        cb->disc_func = disconnect;
@@ -319,6 +328,24 @@ static struct filter_callback *filter_data_add_callback(
        return cb;
 }
 
+static void service_data_free(struct service_data *data)
+{
+       struct filter_callback *callback = data->callback;
+
+       dbus_connection_unref(data->conn);
+
+       if (data->call)
+               dbus_pending_call_unref(data->call);
+
+       if (data->id)
+               g_source_remove(data->id);
+
+       g_free(data->name);
+       g_free(data);
+
+       callback->data = NULL;
+}
+
 static gboolean filter_data_remove_callback(struct filter_data *data,
                                                struct filter_callback *cb)
 {
@@ -327,6 +354,13 @@ static gboolean filter_data_remove_callback(struct filter_data *data,
        data->callbacks = g_slist_remove(data->callbacks, cb);
        data->processed = g_slist_remove(data->processed, cb);
 
+       /* Cancel pending operations */
+       if (cb->data) {
+               if (cb->data->call)
+                       dbus_pending_call_cancel(cb->data->call);
+               service_data_free(cb->data);
+       }
+
        if (cb->destroy_func)
                cb->destroy_func(cb->user_data);
 
@@ -347,7 +381,7 @@ static gboolean filter_data_remove_callback(struct filter_data *data,
        /* Remove filter if there are no listeners left for the connection */
        data = filter_data_find(connection, NULL, NULL, NULL, NULL, NULL,
                                        NULL);
-       if (!data)
+       if (data == NULL)
                dbus_connection_remove_filter(connection, message_filter,
                                                NULL);
 
@@ -443,18 +477,18 @@ static DBusHandlerResult service_filter(DBusConnection *connection,
                                cb->conn_func(connection, cb->user_data);
                }
 
+               /* Check if the watch was removed/freed by the callback
+                * function */
+               if (!g_slist_find(data->callbacks, cb))
+                       continue;
+
                /* Only auto remove if it is a bus name watch */
                if (data->argument[0] == ':' &&
-                               (!cb->conn_func || !cb->disc_func)) {
+                               (cb->conn_func == NULL || cb->disc_func == NULL)) {
                        filter_data_remove_callback(data, cb);
                        continue;
                }
 
-               /* Check if the watch was removed/freed by the callback
-                * function */
-               if (!g_slist_find(data->callbacks, cb))
-                       continue;
-
                data->callbacks = g_slist_remove(data->callbacks, cb);
                data->processed = g_slist_append(data->processed, cb);
        }
@@ -482,7 +516,7 @@ static DBusHandlerResult message_filter(DBusConnection *connection,
        /* Sender is always bus name */
        data = filter_data_find(connection, NULL, sender, path, iface, member,
                                        arg);
-       if (!data) {
+       if (data == NULL) {
                error("Got %s.%s signal which has no listeners", iface, member);
                return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
        }
@@ -508,35 +542,21 @@ static DBusHandlerResult message_filter(DBusConnection *connection,
        /* Remove filter if there no listener left for the connection */
        data = filter_data_find(connection, NULL, NULL, NULL, NULL, NULL,
                                        NULL);
-       if (!data)
+       if (data == NULL)
                dbus_connection_remove_filter(connection, message_filter,
                                                NULL);
 
        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 }
 
-struct service_data {
-       DBusConnection *conn;
-       char *name;
-       const char *owner;
-       GDBusWatchFunction conn_func;
-       void *user_data;
-};
-
-static void service_data_free(struct service_data *data)
-{
-       dbus_connection_unref(data->conn);
-       g_free(data->name);
-       g_free(data);
-}
-
 static gboolean update_service(void *user_data)
 {
        struct service_data *data = user_data;
+       struct filter_callback *cb = data->callback;
 
        update_name_cache(data->name, data->owner);
-       if (data->conn_func)
-               data->conn_func(data->conn, data->user_data);
+       if (cb->conn_func)
+               cb->conn_func(data->conn, cb->user_data);
 
        service_data_free(data);
 
@@ -575,11 +595,11 @@ done:
        dbus_message_unref(reply);
 }
 
-static void check_service(DBusConnection *connection, const char *name,
-                               GDBusWatchFunction connect, void *user_data)
+static void check_service(DBusConnection *connection,
+                                       const char *name,
+                                       struct filter_callback *callback)
 {
        DBusMessage *message;
-       DBusPendingCall *call;
        struct service_data *data;
 
        data = g_try_malloc0(sizeof(*data));
@@ -590,12 +610,12 @@ static void check_service(DBusConnection *connection, const char *name,
 
        data->conn = dbus_connection_ref(connection);
        data->name = g_strdup(name);
-       data->conn_func = connect;
-       data->user_data = user_data;
+       data->callback = callback;
+       callback->data = data;
 
        data->owner = check_name_cache(name);
        if (data->owner != NULL) {
-               g_idle_add(update_service, data);
+               data->id = g_idle_add(update_service, data);
                return;
        }
 
@@ -611,21 +631,19 @@ static void check_service(DBusConnection *connection, const char *name,
                                                        DBUS_TYPE_INVALID);
 
        if (dbus_connection_send_with_reply(connection, message,
-                                                       &call, -1) == FALSE) {
+                                                       &data->call, -1) == FALSE) {
                error("Failed to execute method call");
                g_free(data);
                goto done;
        }
 
-       if (call == NULL) {
+       if (data->call == NULL) {
                error("D-Bus connection not available");
                g_free(data);
                goto done;
        }
 
-       dbus_pending_call_set_notify(call, service_reply, data, g_free);
-
-       dbus_pending_call_unref(call);
+       dbus_pending_call_set_notify(data->call, service_reply, data, NULL);
 
 done:
        dbus_message_unref(message);
@@ -639,22 +657,22 @@ guint g_dbus_add_service_watch(DBusConnection *connection, const char *name,
        struct filter_data *data;
        struct filter_callback *cb;
 
-       if (!name)
+       if (name == NULL)
                return 0;
 
        data = filter_data_get(connection, service_filter, NULL, NULL,
                                DBUS_INTERFACE_DBUS, "NameOwnerChanged",
                                name);
-       if (!data)
+       if (data == NULL)
                return 0;
 
-       cb = filter_data_add_callback(data, connect, disconnect, NULL, NULL,
+       cb = filter_data_add_callback(data, connect, disconnect, NULL, destroy,
                                        user_data);
-       if (!cb)
+       if (cb == NULL)
                return 0;
 
        if (connect)
-               check_service(connection, name, connect, user_data);
+               check_service(connection, name, cb);
 
        return cb->id;
 }
@@ -678,12 +696,12 @@ guint g_dbus_add_signal_watch(DBusConnection *connection,
 
        data = filter_data_get(connection, signal_filter, sender, path,
                                interface, member, NULL);
-       if (!data)
+       if (data == NULL)
                return 0;
 
        cb = filter_data_add_callback(data, NULL, NULL, function, destroy,
                                        user_data);
-       if (!cb)
+       if (cb == NULL)
                return 0;
 
        if (data->name != NULL && data->name_watch == 0)