ListNames and ListQueuedOwners updated to work with new kdbus
[platform/upstream/dbus.git] / bus / driver.c
index a664517..3c4239d 100644 (file)
@@ -3,6 +3,7 @@
  *
  * Copyright (C) 2003 CodeFactory AB
  * Copyright (C) 2003, 2004, 2005 Red Hat, Inc.
+ * Copyright (C) 2013  Samsung Electronics
  *
  * Licensed under the Academic Free License version 2.1
  *
 #include <dbus/dbus-marshal-recursive.h>
 #include <string.h>
 
+#ifdef ENABLE_KDBUS_TRANSPORT
 #include "kdbus-d.h"
 #include <stdio.h>
 #include <errno.h>
 #include <limits.h>
+#include <stdlib.h>
+#endif
 
 static DBusConnection *
 bus_driver_get_conn_helper (DBusConnection  *connection,
@@ -103,8 +107,10 @@ bus_driver_send_service_owner_changed (const char     *service_name,
   dbus_bool_t retval;
   const char *null_service;
 
-  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
          return TRUE;
+#endif
 
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
@@ -155,8 +161,10 @@ bus_driver_send_service_lost (DBusConnection *connection,
 {
   DBusMessage *message;
 
-  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
          return TRUE;
+#endif
 
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
@@ -201,8 +209,10 @@ bus_driver_send_service_acquired (DBusConnection *connection,
 {
   DBusMessage *message;
 
-  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
          return TRUE;
+#endif
 
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
@@ -451,8 +461,8 @@ bus_driver_handle_list_services (DBusConnection *connection,
       return FALSE;
     }
 
-
-  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
   {
          if(!kdbus_list_services (connection, &services, &len))
            {
@@ -462,12 +472,15 @@ bus_driver_handle_list_services (DBusConnection *connection,
            }
   }
   else
-  if (!bus_registry_list_services (registry, &services, &len))
-    {
-      dbus_message_unref (reply);
-      BUS_SET_OOM (error);
-      return FALSE;
-    }
+#endif
+  {
+      if (!bus_registry_list_services (registry, &services, &len))
+        {
+          dbus_message_unref (reply);
+          BUS_SET_OOM (error);
+          return FALSE;
+        }
+  }
 
   dbus_message_iter_init_append (reply, &iter);
 
@@ -481,8 +494,11 @@ bus_driver_handle_list_services (DBusConnection *connection,
       return FALSE;
     }
 
-  if(!bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(!bus_context_is_kdbus(bus_transaction_get_context (transaction))) //not needed for kdbus, we got it from kdbus_list_services
+#endif
   {
+
     /* Include the bus driver in the list */
     const char *v_STRING = DBUS_SERVICE_DBUS;
     if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
@@ -653,7 +669,8 @@ bus_driver_handle_acquire_service (DBusConnection *connection,
   retval = FALSE;
   reply = NULL;
 
-  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
   {
          if (!bus_registry_acquire_kdbus_service (registry, connection,
                                                                                 message,
@@ -662,7 +679,9 @@ bus_driver_handle_acquire_service (DBusConnection *connection,
            goto out;
   }
   else
+#endif
   {
+
          _dbus_string_init_const (&service_name, name);
 
          if (!bus_registry_acquire_service (registry, connection,
@@ -670,7 +689,6 @@ bus_driver_handle_acquire_service (DBusConnection *connection,
                                             &service_reply, transaction,
                                             error))
            goto out;
-
   }
 
   reply = dbus_message_new_method_return (message);
@@ -686,8 +704,6 @@ bus_driver_handle_acquire_service (DBusConnection *connection,
       goto out;
     }
 
- _dbus_verbose ("Reply sender: %s, destination: %s\n", dbus_message_get_sender(reply), dbus_message_get_destination(reply)); //todo kdbus incl
-
   if (!bus_transaction_send_from_driver (transaction, connection, reply))
     {
       BUS_SET_OOM (error);
@@ -717,8 +733,6 @@ bus_driver_handle_release_service (DBusConnection *connection,
 
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
-  registry = bus_connection_get_registry (connection);
-
   if (!dbus_message_get_args (message, error,
                               DBUS_TYPE_STRING, &name,
                               DBUS_TYPE_INVALID))
@@ -730,15 +744,18 @@ bus_driver_handle_release_service (DBusConnection *connection,
   reply = NULL;
 
   _dbus_string_init_const (&service_name, name);
+  registry = bus_connection_get_registry (connection);
 
+#ifdef ENABLE_KDBUS_TRANSPORT
   if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
   {
-         registry = (BusRegistry*) dbus_message_get_sender(message);
-         /* todo looks like hack? Yes.
-          * But how to pass sender of message to bus_registry_release_service in other way?
-          */
+         if (!bus_registry_release_service_kdbus (dbus_message_get_sender(message), connection,
+                                            &service_name, &service_reply,
+                                            transaction, error))
+             goto out;
   }
-
+  else
+#endif
   if (!bus_registry_release_service (registry, connection,
                                      &service_name, &service_reply,
                                      transaction, error))
@@ -802,13 +819,14 @@ bus_driver_handle_service_exists (DBusConnection *connection,
     }
   else
     {
-         if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
+#ifdef ENABLE_KDBUS_TRANSPORT
+         if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
          {
                  int inter_ret;
                  struct nameInfo info;
 
                  inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
-                       if((inter_ret == 0) || (inter_ret == -ENOENT))
+                       if((inter_ret == 0) || (inter_ret == -ENOENT) || (inter_ret == -ENXIO))
                                service_exists = (inter_ret == 0) ? TRUE : FALSE;
                        else
                        {
@@ -818,6 +836,7 @@ bus_driver_handle_service_exists (DBusConnection *connection,
                        }
          }
          else
+#endif
          {
              _dbus_string_init_const (&service_name, name);
              service = bus_registry_lookup (registry, &service_name);
@@ -1092,7 +1111,8 @@ bus_driver_handle_add_match (DBusConnection *connection,
   if (rule == NULL)
     goto failed;
 
-  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
   {
 
          if (!kdbus_add_match_rule (connection, message, text, error))
@@ -1103,7 +1123,9 @@ bus_driver_handle_add_match (DBusConnection *connection,
              goto failed;
   }
   else
+#endif
   {
+
          matchmaker = bus_connection_get_matchmaker (connection);
 
          if (!bus_matchmaker_add_rule (matchmaker, rule))
@@ -1161,11 +1183,13 @@ bus_driver_handle_remove_match (DBusConnection *connection,
   if (rule == NULL)
     goto failed;
 
-  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
   {
          if(!kdbus_remove_match(connection, message, error))
                  goto failed;
   }
+#endif
 
   /* Send the ack before we remove the rule, since the ack is undone
    * on transaction cancel, but rule removal isn't.
@@ -1174,7 +1198,9 @@ bus_driver_handle_remove_match (DBusConnection *connection,
                        message, error))
     goto failed;
 
-  if(!bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(!bus_context_is_kdbus(bus_transaction_get_context (transaction)))
+#endif
   {
          matchmaker = bus_connection_get_matchmaker (connection);
 
@@ -1205,7 +1231,9 @@ bus_driver_handle_get_service_owner (DBusConnection *connection,
   BusRegistry *registry;
   BusService *service;
   DBusMessage *reply;
+#ifdef ENABLE_KDBUS_TRANSPORT
   char unique_name[(unsigned int)(snprintf((char*)base_name, 0, "%llu", ULLONG_MAX) + sizeof(":1."))];
+#endif
 
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
@@ -1219,34 +1247,30 @@ bus_driver_handle_get_service_owner (DBusConnection *connection,
                               DBUS_TYPE_INVALID))
       goto failed;
 
-  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
-  {
-         int ret;
-         struct nameInfo info;
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
+    {
+      int ret;
 
-         ret = kdbus_NameQuery(text, dbus_connection_get_transport(connection), &info);
-               if(ret == 0) //unique id of the name
-               {
-                       sprintf(unique_name, ":1.%llu", (unsigned long long int)info.uniqueId);
-                       _dbus_verbose("Unique name discovered:%s\n", unique_name);
-                       base_name = unique_name;
-               }
-               else if(ret == -ENOENT)  //name has no owner
-               {
-                         dbus_set_error (error, DBUS_ERROR_NAME_HAS_NO_OWNER,
-                                                         "Could not get owner of name '%s': no such name", text);
-                         goto failed;
-               }
-               else
-               {
-                       _dbus_verbose("kdbus error sending name query: err %d (%m)\n", errno);
-                       dbus_set_error (error, DBUS_ERROR_FAILED,
-                                                         "Could not determine unique name for '%s'", text);
-                       goto failed;
-               }
-  }
+      ret = kdbus_get_name_owner(connection, text, unique_name);
+      if(ret == 0)
+        base_name = unique_name;
+      else if((ret == -ENOENT) || (ret == -ENXIO)) //name has no owner
+        {
+          dbus_set_error (error, DBUS_ERROR_NAME_HAS_NO_OWNER,
+                  "Could not get owner of name '%s': no such name", text);
+          goto failed;
+        }
+      else
+        {
+          dbus_set_error (error, DBUS_ERROR_FAILED,
+                  "Could not determine unique name for '%s'", text);
+          goto failed;
+        }
+    }
   else
-  {
+#endif
+    {
          _dbus_string_init_const (&str, text);
          service = bus_registry_lookup (registry, &str);
          if (service == NULL &&
@@ -1342,7 +1366,11 @@ bus_driver_handle_list_queued_owners (DBusConnection *connection,
       if (! _dbus_list_append (&base_names, dbus_service_name))
         goto oom;
     }
+#ifdef ENABLE_KDBUS_TRANSPORT
+  else if (!bus_context_is_kdbus(bus_transaction_get_context (transaction)) && (service == NULL))
+#else
   else if (service == NULL)
+#endif
     {
       dbus_set_error (error,
                       DBUS_ERROR_NAME_HAS_NO_OWNER,
@@ -1351,10 +1379,20 @@ bus_driver_handle_list_queued_owners (DBusConnection *connection,
     }
   else
     {
-      if (!bus_service_list_queued_owners (service,
-                                           &base_names,
-                                           error))
-        goto failed;
+#ifdef ENABLE_KDBUS_TRANSPORT
+      if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
+        {
+          if(!kdbus_list_queued (connection,  &base_names, text ,error))
+            goto failed;
+        }
+      else
+#endif
+        {
+          if (!bus_service_list_queued_owners (service,
+              &base_names,
+              error))
+            goto failed;
+        }
     }
 
   _dbus_assert (base_names != NULL);
@@ -1395,6 +1433,23 @@ bus_driver_handle_list_queued_owners (DBusConnection *connection,
 
   dbus_message_unref (reply);
 
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
+    {
+      link = _dbus_list_get_first_link (&base_names);
+      while (link != NULL)
+        {
+          DBusList *next = _dbus_list_get_next_link (&base_names, link);
+
+          if(link->data != NULL)
+            free(link->data);
+
+          _dbus_list_free_link (link);
+          link = next;
+        }
+    }
+#endif
+
   return TRUE;
 
  oom:
@@ -1404,10 +1459,8 @@ bus_driver_handle_list_queued_owners (DBusConnection *connection,
   _DBUS_ASSERT_ERROR_IS_SET (error);
   if (reply)
     dbus_message_unref (reply);
-
   if (base_names)
     _dbus_list_clear (&base_names);
-
   return FALSE;
 }
 
@@ -1429,12 +1482,18 @@ bus_driver_handle_get_connection_unix_user (DBusConnection *connection,
   if (reply == NULL)
     goto oom;
 
-  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
   {
-         if(!kdbus_get_connection_unix_user(connection, message, &uid, error))
+         const char* name;
+
+         if(!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID))
+                 goto failed;
+         if(!kdbus_get_unix_user(connection, name, &uid, error))
                  goto failed;
   }
   else
+#endif
   {
          conn = bus_driver_get_conn_helper (connection, message, "UID", &service,
                                      error);
@@ -1491,29 +1550,31 @@ bus_driver_handle_get_connection_unix_process_id (DBusConnection *connection,
   reply = dbus_message_new_method_return (message);
   if (reply == NULL)
     goto oom;
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
+    {
+      const char* name;
 
-  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
-  {
-         if(!kdbus_get_connection_unix_process_id(connection, message, &pid, error))
-                 goto failed;
-  }
-  else
+      if(!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID))
+        goto failed;
+      if(!kdbus_get_connection_unix_process_id(connection, name, &pid, error))
+        goto failed;
+    }
+    else
+#endif
   {
-         conn = bus_driver_get_conn_helper (connection, message, "PID", &service,
-                                                                                error);
-
-         if (conn == NULL)
-               goto failed;
-
-
+    conn = bus_driver_get_conn_helper (connection, message, "PID", &service,
+                       error);
+    if (conn == NULL)
+      goto failed;
 
-         if (!dbus_connection_get_unix_process_id (conn, &pid))
-               {
-                 dbus_set_error (error,
-                                                 DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,
-                                                 "Could not determine PID for '%s'", service);
-                 goto failed;
-               }
+    if (!dbus_connection_get_unix_process_id (conn, &pid))
+    {
+      dbus_set_error (error,
+                DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,
+                "Could not determine PID for '%s'", service);
+      goto failed;
+    }
   }
 
   pid32 = pid;
@@ -1612,12 +1673,14 @@ bus_driver_handle_get_connection_selinux_security_context (DBusConnection *conne
   if (reply == NULL)
     goto oom;
 
-  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
   {
          if(!kdbus_get_connection_unix_selinux_security_context(connection, message, reply, error))
                  goto failed;
   }
   else
+#endif
   {
          conn = bus_driver_get_conn_helper (connection, message, "security context",
                                                                                 &service, error);
@@ -1674,8 +1737,10 @@ bus_driver_handle_get_connection_credentials (DBusConnection *connection,
   if (reply == NULL)
     goto oom;
 
-  if(!bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(!bus_context_is_kdbus(bus_transaction_get_context (transaction)))
   {
+#endif
          conn = bus_driver_get_conn_helper (connection, message, "credentials",
                                                                                 &service, error);
 
@@ -1699,10 +1764,15 @@ bus_driver_handle_get_connection_credentials (DBusConnection *connection,
                  if (!_dbus_asv_add_uint32 (&array_iter, "UnixUserID", ulong_val))
                        goto oom;
                }
+#ifdef ENABLE_KDBUS_TRANSPORT
   }
   else
   {
-         if(kdbus_get_connection_unix_process_id(connection, message, &ulong_val, error))
+         const char* name;
+
+         if(!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID))
+                 goto failed;
+         if(kdbus_get_connection_unix_process_id(connection, name, &ulong_val, error))
          {
                  if (!_dbus_asv_add_uint32 (&array_iter, "ProcessID", ulong_val))
                        goto oom;
@@ -1710,7 +1780,7 @@ bus_driver_handle_get_connection_credentials (DBusConnection *connection,
          else
                  goto failed;
 
-         if(kdbus_get_connection_unix_user(connection, message, &ulong_val, error))
+         if(kdbus_get_unix_user(connection, name, &ulong_val, error))
          {
                  if (!_dbus_asv_add_uint32 (&array_iter, "UnixUserID", ulong_val))
                        goto oom;
@@ -1718,6 +1788,7 @@ bus_driver_handle_get_connection_credentials (DBusConnection *connection,
          else
                  goto failed;
   }
+#endif
 
   if (!_dbus_asv_close (&reply_iter, &array_iter))
     goto oom;