[daemon-fix] Fixed daemon creation, services activation and little more
authorRadoslaw Pajak <r.pajak@samsung.com>
Tue, 22 Oct 2013 13:53:00 +0000 (15:53 +0200)
committerRadoslaw Pajak <r.pajak@samsung.com>
Tue, 22 Oct 2013 13:53:00 +0000 (15:53 +0200)
- fixed registering org.freedesktop.DBus name by daemon
- fixed auto activating services and activation messages delivering
- from now starters are not listed by ListQueuedOwners method
- fixed error handling during daemon creation process
- fixed updating kdbus starters

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

index b97a047..1dbe414 100644 (file)
--- a/bus/bus.c
+++ b/bus/bus.c
@@ -973,17 +973,26 @@ bus_context_new (const DBusString *config_file,
   {
          DBusString unique_name;
 
-         bus_connections_setup_connection(context->connections, context->myKdbusConnection);
+         if(!bus_connections_setup_connection(context->connections, context->myKdbusConnection))
+         {
+          _dbus_verbose ("Bus connections setup connection failed for myKdbusConnection!\n");
+          dbus_connection_close (context->myKdbusConnection);
+          dbus_connection_unref (context->myKdbusConnection);
+          goto failed;
+         }
          dbus_connection_set_route_peer_messages (context->myKdbusConnection, FALSE);
-         _dbus_string_init_const(&unique_name, ":1.1"); //dbus_bus_get_unique_name(context->myConnection)); this is without :1.
-         if(!bus_connection_complete(context->myKdbusConnection, &unique_name, error))
+         _dbus_string_init_const (&unique_name, ":1.1"); //dbus_bus_get_unique_name(context->myConnection)); this is without :1.
+         if(!bus_connection_complete (context->myKdbusConnection, &unique_name, error))
          {
-                 _dbus_verbose ("Bus connection complete failed for kdbus!\n");
-                 _dbus_string_free(&unique_name);
+                 _dbus_verbose ("Bus connection complete failed for myKdbusConnection!\n");
                  goto failed;
          }
-         _dbus_string_free(&unique_name);
 
+         if(!register_daemon_name(context->myKdbusConnection))
+         {
+             _dbus_verbose ("Registering org.freedesktop.DBus name for daemon failed!\n");
+             goto failed;
+         }
          if(!register_kdbus_starters(context->myKdbusConnection))
          {
           _dbus_verbose ("Registering kdbus starters for dbus activatable names failed!\n");
index c540e6c..de956b4 100644 (file)
@@ -314,7 +314,7 @@ bus_dispatch (DBusConnection *connection,
       _dbus_string_init_const (&service_string, service_name);
       service = bus_registry_lookup (registry, &service_string);
 
-      if (service == NULL && dbus_message_get_auto_start (message))
+      if (dbus_message_get_auto_start (message) && (service == NULL || bus_service_get_is_kdbus_starter(service)))
         {
           BusActivation *activation;
           /* We can't do the security policy check here, since the addressed
index 9f5c756..a145673 100644 (file)
@@ -102,7 +102,6 @@ DBusServer* empty_server_init(char* address)
 DBusConnection* daemon_as_client(DBusBusType type, char* address, DBusError *error)
 {
        DBusConnection* connection;
-       DBusString daemon_name;
 
        dbus_bus_set_bus_connection_address(type, address);
 
@@ -110,32 +109,25 @@ DBusConnection* daemon_as_client(DBusBusType type, char* address, DBusError *err
        if(connection == NULL)
                return NULL;
 
-       _dbus_string_init_const(&daemon_name, DBUS_SERVICE_DBUS);
-       if(!kdbus_register_policy (&daemon_name, connection))
-               goto failed;
-
-       if(kdbus_request_name(connection, &daemon_name, 0, 0) != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
-               goto failed;
-
-    if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "member='IdRemoved'"))
+       if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "member='IdRemoved'"))
     {
           dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:1, %s", _dbus_strerror_from_errno ());
-          return FALSE;
+          goto failed;
     }
     if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "member='NameChanged'"))
     {
           dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:1, %s", _dbus_strerror_from_errno ());
-          return FALSE;
+          goto failed;
     }
     if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "member='NameLost'"))
     {
           dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:1, %s", _dbus_strerror_from_errno ());
-          return FALSE;
+          goto failed;
     }
     if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "member='NameAcquired'"))
     {
           dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:1, %s", _dbus_strerror_from_errno ());
-          return FALSE;
+          goto failed;
     }
 
        if(dbus_error_is_set(error))
@@ -151,6 +143,39 @@ failed:
        return connection;
 }
 
+dbus_bool_t register_daemon_name(DBusConnection* connection)
+{
+    DBusString daemon_name;
+    dbus_bool_t retval = FALSE;
+    BusTransaction *transaction;
+
+    _dbus_string_init_const(&daemon_name, DBUS_SERVICE_DBUS);
+    if(!kdbus_register_policy (&daemon_name, connection))
+        return FALSE;
+
+    if(kdbus_request_name(connection, &daemon_name, 0, 0) != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
+       return FALSE;
+
+    transaction = bus_transaction_new (bus_connection_get_context(connection));
+    if (transaction == NULL)
+    {
+        kdbus_release_name(connection, &daemon_name, 0);
+        goto out;
+    }
+
+    if(!bus_registry_ensure (bus_connection_get_registry (connection), &daemon_name, connection, 0, transaction, NULL))
+    {
+        kdbus_release_name(connection, &daemon_name, 0);
+        goto out;
+    }
+
+    retval = TRUE;
+
+out:
+    bus_transaction_free(transaction);
+    return retval;
+}
+
 dbus_bool_t kdbus_register_policy (const DBusString *service_name, DBusConnection* connection)
 {
        int fd;
@@ -349,7 +374,7 @@ dbus_bool_t register_kdbus_starters(DBusConnection* connection)
     if (!bus_activation_list_services (bus_connection_get_activation (connection), &services, &len))
         return FALSE;
 
-    _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
+    _dbus_transport_get_socket_fd (dbus_connection_get_transport(connection), &fd);
     _dbus_string_init(&name);
 
     for(i=0; i<len; i++)
@@ -400,6 +425,7 @@ dbus_bool_t update_kdbus_starters(DBusConnection* connection)
 
     services_old = bus_connection_get_services_owned(connection);
     link = _dbus_list_get_first_link(services_old);
+    link = _dbus_list_get_next_link (services_old, link); //skip org.freedesktop.DBus which is not starter
 
     while (link != NULL)
     {
index c6f9bd6..1f06d37 100644 (file)
@@ -22,6 +22,7 @@ __u64 sender_name_to_id(const char* name, DBusError* error);
 char* make_kdbus_bus(DBusBusType type, DBusError *error);
 DBusServer* empty_server_init(char* address);
 DBusConnection* daemon_as_client(DBusBusType type, char* address, DBusError *error);
+dbus_bool_t register_daemon_name(DBusConnection* connection);
 dbus_bool_t kdbus_register_policy (const DBusString *service_name, DBusConnection* connection);
 dbus_uint32_t kdbus_request_name(DBusConnection* connection, const DBusString *service_name, dbus_uint32_t flags, __u64 sender_id);
 dbus_uint32_t kdbus_release_name(DBusConnection* connection, const DBusString *service_name, __u64 sender_id);
index 9318eef..6ef52de 100644 (file)
@@ -41,6 +41,7 @@
 #include "bus.h"
 #include "selinux.h"
 #include "kdbus-d.h"
+#include "dbus/kdbus.h"
 
 struct BusService
 {
@@ -60,6 +61,7 @@ struct BusOwner
 
   unsigned int allow_replacement : 1;
   unsigned int do_not_queue : 1;
+  unsigned int is_kdbus_starter : 1;
 };
 
 struct BusRegistry
@@ -208,6 +210,9 @@ bus_owner_set_flags (BusOwner *owner,
 
    owner->do_not_queue =
         (flags & DBUS_NAME_FLAG_DO_NOT_QUEUE) != FALSE;
+
+   owner->is_kdbus_starter =
+        (flags & KDBUS_NAME_STARTER) != FALSE;
 }
 
 static BusOwner *
@@ -633,12 +638,12 @@ bus_registry_acquire_kdbus_service (BusRegistry      *registry,
   dbus_bool_t retval;
   BusService *service;
   BusActivation  *activation;
-
   DBusString service_name_real;
   const DBusString *service_name = &service_name_real;
   char* name;
   dbus_uint32_t flags;
   __u64 sender_id;
+  dbus_bool_t rm_owner_daemon = FALSE;
 
   if (!dbus_message_get_args (message, error,
                               DBUS_TYPE_STRING, &name,
@@ -695,6 +700,7 @@ bus_registry_acquire_kdbus_service (BusRegistry      *registry,
                if (service == NULL)
                  goto out;
 
+               rm_owner_daemon = TRUE;
                if(!kdbus_register_policy(service_name, connection))
                {
                        dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
@@ -726,6 +732,7 @@ bus_registry_acquire_kdbus_service (BusRegistry      *registry,
        {
            DBusConnection* phantom;
            const char* name;
+//         DBusList *link;
 
            name = dbus_message_get_sender(message);
            phantom = bus_connections_find_conn_by_name(bus_connection_get_connections(connection), name);
@@ -735,7 +742,7 @@ bus_registry_acquire_kdbus_service (BusRegistry      *registry,
                goto failed2;
            if (!bus_service_add_owner (service, phantom, flags, transaction, error))
                goto failed2;
-           if(*result == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
+           if((*result == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) && rm_owner_daemon)
            {
             /* Here we are removing DBus daemon as an owner of the service,
              * which is set by bus_registry_ensure.
@@ -745,6 +752,17 @@ bus_registry_acquire_kdbus_service (BusRegistry      *registry,
                if(_bus_service_find_owner_link (service, connection))
                 bus_service_remove_owner (service, connection, transaction, NULL);
            }
+           /*if(bus_service_get_is_kdbus_starter(service))
+           {
+               if (!bus_service_swap_owner (service, bus_service_get_primary_owners_connection(service),
+                                              transaction, error))
+                   goto failed2;
+           }*/
+       /*   if((link = _bus_service_find_owner_link (service, connection)))  //if daemon is a starter
+           {
+               _dbus_list_unlink (&service->owners, link);
+               _dbus_list_append_link (&service->owners, link);  //it must be moved at the end of the queue
+           }*/
        }
 
   activation = bus_context_get_activation (registry->context);
@@ -1512,6 +1530,20 @@ bus_service_get_allow_replacement (BusService *service)
 }
 
 dbus_bool_t
+bus_service_get_is_kdbus_starter (BusService *service)
+{
+  BusOwner *owner;
+  DBusList *link;
+
+  _dbus_assert (service->owners != NULL);
+
+  link = _dbus_list_get_first_link (&service->owners);
+  owner = (BusOwner *) link->data;
+
+  return owner->is_kdbus_starter;
+}
+
+dbus_bool_t
 bus_service_has_owner (BusService     *service,
                       DBusConnection *connection)
 {
@@ -1545,8 +1577,9 @@ bus_service_list_queued_owners (BusService *service,
       owner = (BusOwner *) link->data;
       uname = bus_connection_get_name (owner->conn);
 
-      if (!_dbus_list_append (return_list, (char *)uname))
-        goto oom;
+      if(!owner->is_kdbus_starter)
+          if (!_dbus_list_append (return_list, (char *)uname))
+              goto oom;
 
       link = _dbus_list_get_next_link (&service->owners, link);
     }
index c7543bf..5df2888 100644 (file)
@@ -97,6 +97,7 @@ dbus_bool_t     bus_service_has_owner                 (BusService     *service,
                                                        DBusConnection *connection);
 BusOwner*       bus_service_get_primary_owner         (BusService     *service);
 dbus_bool_t     bus_service_get_allow_replacement     (BusService     *service);
+dbus_bool_t     bus_service_get_is_kdbus_starter      (BusService     *service);
 const char*     bus_service_get_name                  (BusService     *service);
 dbus_bool_t     bus_service_list_queued_owners        (BusService *service,
                                                        DBusList  **return_list,
index 6e48d4a..e7c37a2 100644 (file)
@@ -2115,7 +2115,7 @@ do_writing (DBusTransport *transport)
 
                message = _dbus_connection_get_message_to_send (transport->connection);
                _dbus_assert (message != NULL);
-//             if(dbus_message_get_sender(message) == NULL)  //needed for daemon  todo check if really needed
+               if(dbus_message_get_sender(message) == NULL)  //needed for daemon to pass pending activation messages
                {
             dbus_message_unlock(message);
             dbus_message_set_sender(message, socket_transport->sender);