[daemon-fix] Fixes for creating and removing phantom connections and sending NameLost...
authorRadoslaw Pajak <r.pajak@samsung.com>
Tue, 8 Oct 2013 07:07:46 +0000 (09:07 +0200)
committerRadoslaw Pajak <r.pajak@samsung.com>
Tue, 8 Oct 2013 07:07:46 +0000 (09:07 +0200)
- Now phantom connection is created once for unique name, not for each well-known name,
- Fixed sending NameLost signal to disconected client

Change-Id: I33f856c56a8fbb8c865b023aa1e773ffd1f3343e
Signed-off-by: Radoslaw Pajak <r.pajak@samsung.com>
bus/connection.c
bus/connection.h
bus/kdbus-d.c
bus/services.c

index c70e0b3..b8c3df8 100644 (file)
@@ -1001,6 +1001,26 @@ bus_connections_foreach (BusConnections               *connections,
   foreach_inactive (connections, function, data);
 }
 
+DBusConnection*
+bus_connections_find_conn_by_name(BusConnections *connections, const char* name)
+{
+    DBusList *link;
+
+    link = _dbus_list_get_first_link (&connections->completed);
+    while (link != NULL)
+      {
+        DBusConnection *connection = link->data;
+        DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
+
+        if (!strcmp(bus_connection_get_name(connection), name))
+          return connection;
+
+        link = next;
+      }
+
+    return NULL;
+}
+
 BusContext*
 bus_connections_get_context (BusConnections *connections)
 {
index c936021..4b113ee 100644 (file)
@@ -43,6 +43,8 @@ void            bus_connections_foreach           (BusConnections
 void            bus_connections_foreach_active    (BusConnections               *connections,
                                                    BusConnectionForeachFunction  function,
                                                    void                         *data);
+DBusConnection* bus_connections_find_conn_by_name (BusConnections *connections,
+                                                   const char* name);
 BusContext*     bus_connections_get_context       (BusConnections               *connections);
 void            bus_connections_increment_stamp   (BusConnections               *connections);
 BusContext*     bus_connection_get_context        (DBusConnection               *connection);
index 2d7676a..bb2a953 100644 (file)
@@ -354,19 +354,16 @@ out:
     return retval;
 }
 
+/*
 static dbus_bool_t remove_conn_if_name_match (DBusConnection *connection, void *data)
 {
     if(!strcmp(bus_connection_get_name(connection), (char*)data))
     {
         bus_connection_disconnected(connection);
-//        return FALSE; //needed to break foreach function
-        /* todo should we brake? Now we create phantom for each name, so if someone acquire more than
-         * one name he will have more than one phantom. I think that there should be one phantom for one name
-         * but if so, name acquiring and releasing must be changed
-         */
+        return FALSE; //this is to break foreach function
     }
     return TRUE;
-}
+}*/
 
 void handleNameOwnerChanged(DBusMessage *msg, BusTransaction *transaction, DBusConnection *connection)
 {
@@ -378,13 +375,12 @@ void handleNameOwnerChanged(DBusMessage *msg, BusTransaction *transaction, DBusC
         return;
     }
 
+    _dbus_verbose ("Got NameOwnerChanged signal:\nName: %s\nOld: %s\nNew: %s\n", name, old, new);
+
     if(!strncmp(name, ":1.", 3))/*if it starts from :1. it is unique name - this might be IdRemoved info*/
     {
         if(!strcmp(name, old))  //yes it is - someone has disconnected
-        {
-            _dbus_verbose ("Connection %s has disconnected. Removing.\n", name);  //todo to remove at the end of development
-            bus_connections_foreach_active(bus_connection_get_connections(connection), remove_conn_if_name_match, (void*)name);
-        }
+            bus_connection_disconnected(bus_connections_find_conn_by_name(bus_connection_get_connections(connection), name));
     }
     else //it is well-known name
     {
@@ -392,6 +388,9 @@ void handleNameOwnerChanged(DBusMessage *msg, BusTransaction *transaction, DBusC
         {
             DBusMessage *message;
 
+            if(bus_connections_find_conn_by_name(bus_connection_get_connections(connection), old) == NULL)
+                goto next;
+
             _dbus_verbose ("Owner '%s' lost name '%s'. Sending NameLost.\n", old, name);
 
             message = dbus_message_new_signal (DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameLost");
index 7773d00..9318eef 100644 (file)
@@ -725,16 +725,16 @@ bus_registry_acquire_kdbus_service (BusRegistry      *registry,
        if((*result == DBUS_REQUEST_NAME_REPLY_IN_QUEUE) || (*result == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER))
        {
            DBusConnection* phantom;
+           const char* name;
 
-           //todo what if we already have phantom for that sender?
-           phantom = create_phantom_connection(connection, dbus_message_get_sender(message), error);
+           name = dbus_message_get_sender(message);
+           phantom = bus_connections_find_conn_by_name(bus_connection_get_connections(connection), name);
+        if(phantom == NULL)
+            phantom = create_phantom_connection(connection, name, error);
            if(phantom == NULL)
                goto failed2;
            if (!bus_service_add_owner (service, phantom, flags, transaction, error))
-           {
-               bus_connection_disconnected(phantom);
                goto failed2;
-           }
            if(*result == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
            {
             /* Here we are removing DBus daemon as an owner of the service,
@@ -911,7 +911,11 @@ bus_registry_release_service_kdbus (const char* sender_name,
             if(phantom)
             {
                 bus_service_remove_owner (service, phantom, transaction, NULL);
-                dbus_connection_unref_phantom(phantom);  //todo if there will be one phantom for one id not for one name, than it must be changed
+                /* todo we could remove phantom if he doesn't own any name
+                 * to do this we should write function in connection.c to check if
+                 * _dbus_list_get_last (&d->services_owned) returns not NULL
+                 *  or we can leave phantom - he will be removed when he disconnects from the bus
+                 */
             }
             else
                 _dbus_verbose ("Didn't find phantom connection for released name!\n");