gdbus: Fix issue with unref of client while parsing interfaces
[platform/upstream/connman.git] / gdbus / watch.c
index 2661928..9e4f994 100644 (file)
@@ -119,13 +119,7 @@ static struct filter_data *filter_data_find_match(DBusConnection *connection,
        return NULL;
 }
 
-static struct filter_data *filter_data_find(DBusConnection *connection,
-                                                       const char *name,
-                                                       const char *owner,
-                                                       const char *path,
-                                                       const char *interface,
-                                                       const char *member,
-                                                       const char *argument)
+static struct filter_data *filter_data_find(DBusConnection *connection)
 {
        GSList *current;
 
@@ -136,30 +130,6 @@ static struct filter_data *filter_data_find(DBusConnection *connection,
                if (connection != data->connection)
                        continue;
 
-               if (name && data->name &&
-                               g_str_equal(name, data->name) == FALSE)
-                       continue;
-
-               if (owner && data->owner &&
-                               g_str_equal(owner, data->owner) == FALSE)
-                       continue;
-
-               if (path && data->path &&
-                               g_str_equal(path, data->path) == FALSE)
-                       continue;
-
-               if (interface && data->interface &&
-                               g_str_equal(interface, data->interface) == FALSE)
-                       continue;
-
-               if (member && data->member &&
-                               g_str_equal(member, data->member) == FALSE)
-                       continue;
-
-               if (argument && data->argument &&
-                               g_str_equal(argument, data->argument) == FALSE)
-                       continue;
-
                return data;
        }
 
@@ -245,7 +215,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) == NULL) {
+       if (filter_data_find(connection) == NULL) {
                if (!dbus_connection_add_filter(connection,
                                        message_filter, NULL, NULL)) {
                        error("dbus_connection_add_filter() failed");
@@ -270,8 +240,8 @@ proceed:
        data = g_new0(struct filter_data, 1);
 
        data->connection = dbus_connection_ref(connection);
-       data->name = name ? g_strdup(name) : NULL;
-       data->owner = owner ? g_strdup(owner) : NULL;
+       data->name = g_strdup(name);
+       data->owner = g_strdup(owner);
        data->path = g_strdup(path);
        data->interface = g_strdup(interface);
        data->member = g_strdup(member);
@@ -419,8 +389,7 @@ static gboolean filter_data_remove_callback(struct filter_data *data,
        listeners = g_slist_remove(listeners, data);
 
        /* Remove filter if there are no listeners left for the connection */
-       if (filter_data_find(connection, NULL, NULL, NULL, NULL, NULL,
-                                                               NULL) == NULL)
+       if (filter_data_find(connection) == NULL)
                dbus_connection_remove_filter(connection, message_filter,
                                                NULL);
 
@@ -605,7 +574,7 @@ static DBusHandlerResult message_filter(DBusConnection *connection,
                        continue;
 
                remove_match(data);
-               listeners = g_slist_remove_link(listeners, l);
+               listeners = g_slist_delete_link(listeners, l);
 
                filter_data_free(data);
        }
@@ -613,8 +582,7 @@ static DBusHandlerResult message_filter(DBusConnection *connection,
        g_slist_free(delete_listener);
 
        /* Remove filter if there are no listeners left for the connection */
-       if (filter_data_find(connection, NULL, NULL, NULL, NULL, NULL,
-                                                               NULL) == NULL)
+       if (filter_data_find(connection) == NULL)
                dbus_connection_remove_filter(connection, message_filter,
                                                NULL);
 
@@ -784,6 +752,34 @@ guint g_dbus_add_signal_watch(DBusConnection *connection,
        return cb->id;
 }
 
+guint g_dbus_add_properties_watch(DBusConnection *connection,
+                               const char *sender, const char *path,
+                               const char *interface,
+                               GDBusSignalFunction function, void *user_data,
+                               GDBusDestroyFunction destroy)
+{
+       struct filter_data *data;
+       struct filter_callback *cb;
+
+       data = filter_data_get(connection, signal_filter, sender, path,
+                               DBUS_INTERFACE_PROPERTIES, "PropertiesChanged",
+                               interface);
+       if (data == NULL)
+               return 0;
+
+       cb = filter_data_add_callback(data, NULL, NULL, function, destroy,
+                                       user_data);
+       if (cb == NULL)
+               return 0;
+
+       if (data->name != NULL && data->name_watch == 0)
+               data->name_watch = g_dbus_add_service_watch(connection,
+                                                       data->name, NULL,
+                                                       NULL, NULL, NULL);
+
+       return cb->id;
+}
+
 gboolean g_dbus_remove_watch(DBusConnection *connection, guint id)
 {
        struct filter_data *data;
@@ -810,8 +806,7 @@ void g_dbus_remove_all_watches(DBusConnection *connection)
 {
        struct filter_data *data;
 
-       while ((data = filter_data_find(connection, NULL, NULL, NULL, NULL,
-                                       NULL, NULL))) {
+       while ((data = filter_data_find(connection))) {
                listeners = g_slist_remove(listeners, data);
                filter_data_call_and_free(data);
        }