- fixed dbus_bus_request_name
[platform/upstream/dbus.git] / dbus / dbus-transport.c
index 35b7027..af5f97b 100644 (file)
  * 
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  *
  */
 
+#include <../config.h>
 #include "dbus-transport-protected.h"
 #include "dbus-transport-unix.h"
 #include "dbus-transport-socket.h"
 #include "dbus-auth.h"
 #include "dbus-address.h"
 #include "dbus-credentials.h"
+#include "dbus-mainloop.h"
+#include "dbus-message.h"
+
+#include "dbus-transport-kdbus.h"
 #ifdef DBUS_BUILD_TESTS
 #include "dbus-server-debug-pipe.h"
 #endif
@@ -55,7 +60,7 @@
  */
 
 static void
-live_messages_size_notify (DBusCounter *counter,
+live_messages_notify (DBusCounter *counter,
                            void        *user_data)
 {
   DBusTransport *transport = user_data;
@@ -63,15 +68,21 @@ live_messages_size_notify (DBusCounter *counter,
   _dbus_transport_ref (transport);
 
 #if 0
-  _dbus_verbose ("Counter value is now %d\n",
-                 (int) _dbus_counter_get_value (counter));
+  _dbus_verbose ("Size counter value is now %d\n",
+                 (int) _dbus_counter_get_size_value (counter));
+  _dbus_verbose ("Unix FD counter value is now %d\n",
+                 (int) _dbus_counter_get_unix_fd_value (counter));
 #endif
-  
+
   /* disable or re-enable the read watch for the transport if
    * required.
    */
   if (transport->vtable->live_messages_changed)
-    (* transport->vtable->live_messages_changed) (transport);
+    {
+      _dbus_connection_lock (transport->connection);
+      (* transport->vtable->live_messages_changed) (transport);
+      _dbus_connection_unlock (transport->connection);
+    }
 
   _dbus_transport_unref (transport);
 }
@@ -108,7 +119,19 @@ _dbus_transport_init_base (DBusTransport             *transport,
   if (server_guid)
     auth = _dbus_auth_server_new (server_guid);
   else
-    auth = _dbus_auth_client_new ();
+  {
+         _dbus_assert (address != NULL);
+         if (!_dbus_string_copy_data (address, &address_copy))
+        {
+          _dbus_message_loader_unref (loader);
+          return FALSE;
+        }
+      if(address_copy == strstr(address_copy, "kdbus:path="))
+         auth = _dbus_auth_client_new_kdbus();
+         else
+                 auth = _dbus_auth_client_new ();
+  }
+
   if (auth == NULL)
     {
       _dbus_message_loader_unref (loader);
@@ -137,25 +160,12 @@ _dbus_transport_init_base (DBusTransport             *transport,
       _dbus_assert (address == NULL);
       address_copy = NULL;
     }
-  else
-    {
-      _dbus_assert (address != NULL);
-
-      if (!_dbus_string_copy_data (address, &address_copy))
-        {
-          _dbus_credentials_unref (creds);
-          _dbus_counter_unref (counter);
-          _dbus_auth_unref (auth);
-          _dbus_message_loader_unref (loader);
-          return FALSE;
-        }
-    }
   
   transport->refcount = 1;
   transport->vtable = vtable;
   transport->loader = loader;
   transport->auth = auth;
-  transport->live_messages_size = counter;
+  transport->live_messages = counter;
   transport->authenticated = FALSE;
   transport->disconnected = FALSE;
   transport->is_server = (server_guid != NULL);
@@ -178,17 +188,22 @@ _dbus_transport_init_base (DBusTransport             *transport,
    */
   transport->max_live_messages_size = _DBUS_ONE_MEGABYTE * 63;
 
+  /* On Linux RLIMIT_NOFILE defaults to 1024, so allowing 4096 fds live
+     should be more than enough */
+  transport->max_live_messages_unix_fds = 4096;
+
   /* credentials read from socket if any */
   transport->credentials = creds;
-  
-  _dbus_counter_set_notify (transport->live_messages_size,
+
+  _dbus_counter_set_notify (transport->live_messages,
                             transport->max_live_messages_size,
-                            live_messages_size_notify,
+                            transport->max_live_messages_unix_fds,
+                            live_messages_notify,
                             transport);
 
   if (transport->address)
     _dbus_verbose ("Initialized transport on address %s\n", transport->address);
-  
+
   return TRUE;
 }
 
@@ -212,9 +227,9 @@ _dbus_transport_finalize_base (DBusTransport *transport)
   
   _dbus_message_loader_unref (transport->loader);
   _dbus_auth_unref (transport->auth);
-  _dbus_counter_set_notify (transport->live_messages_size,
-                            0, NULL, NULL);
-  _dbus_counter_unref (transport->live_messages_size);
+  _dbus_counter_set_notify (transport->live_messages,
+                            0, 0, NULL, NULL);
+  _dbus_counter_unref (transport->live_messages);
   dbus_free (transport->address);
   dbus_free (transport->expected_guid);
   if (transport->credentials)
@@ -239,7 +254,6 @@ check_address (const char *address, DBusError *error)
   int len, i;
 
   _dbus_assert (address != NULL);
-  _dbus_assert (*address != '\0');
 
   if (!dbus_parse_address (address, &entries, &len, error))
     return NULL;              /* not a valid address */
@@ -263,7 +277,7 @@ check_address (const char *address, DBusError *error)
  * @returns a new transport, or #NULL on failure.
  */
 static DBusTransport*
-_dbus_transport_new_for_autolaunch (DBusError      *error)
+_dbus_transport_new_for_autolaunch (const char *scope, DBusError *error)
 {
   DBusString address;
   DBusTransport *result = NULL;
@@ -276,7 +290,7 @@ _dbus_transport_new_for_autolaunch (DBusError      *error)
       return NULL;
     }
 
-  if (!_dbus_get_autolaunch_address (&address, error))
+  if (!_dbus_get_autolaunch_address (scope, &address, error))
     {
       _DBUS_ASSERT_ERROR_IS_SET (error);
       goto out;
@@ -305,7 +319,9 @@ _dbus_transport_open_autolaunch (DBusAddressEntry  *entry,
 
   if (strcmp (method, "autolaunch") == 0)
     {
-      *transport_p = _dbus_transport_new_for_autolaunch (error);
+      const char *scope = dbus_address_entry_get_value (entry, "scope");
+
+      *transport_p = _dbus_transport_new_for_autolaunch (scope, error);
 
       if (*transport_p == NULL)
         {
@@ -332,6 +348,7 @@ static const struct {
 } open_funcs[] = {
   { _dbus_transport_open_socket },
   { _dbus_transport_open_platform_specific },
+  { _dbus_transport_open_kdbus },
   { _dbus_transport_open_autolaunch }
 #ifdef DBUS_BUILD_TESTS
   , { _dbus_transport_open_debug_pipe }
@@ -465,7 +482,7 @@ _dbus_transport_unref (DBusTransport *transport)
   transport->refcount -= 1;
   if (transport->refcount == 0)
     {
-      _dbus_verbose ("%s: finalizing\n", _DBUS_FUNCTION_NAME);
+      _dbus_verbose ("finalizing\n");
       
       _dbus_assert (transport->vtable->finalize != NULL);
       
@@ -484,7 +501,7 @@ _dbus_transport_unref (DBusTransport *transport)
 void
 _dbus_transport_disconnect (DBusTransport *transport)
 {
-  _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
+  _dbus_verbose ("start\n");
   
   _dbus_assert (transport->vtable->disconnect != NULL);
   
@@ -495,7 +512,7 @@ _dbus_transport_disconnect (DBusTransport *transport)
   
   transport->disconnected = TRUE;
 
-  _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
+  _dbus_verbose ("end\n");
 }
 
 /**
@@ -532,14 +549,14 @@ auth_via_unix_user_function (DBusTransport *transport)
   unix_user_data = transport->unix_user_data;
   uid = _dbus_credentials_get_unix_uid (auth_identity);
               
-  _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
+  _dbus_verbose ("unlock\n");
   _dbus_connection_unlock (connection);
 
   allow = (* unix_user_function) (connection,
                                   uid,
                                   unix_user_data);
               
-  _dbus_verbose ("lock %s post unix user function\n", _DBUS_FUNCTION_NAME);
+  _dbus_verbose ("lock post unix user function\n");
   _dbus_connection_lock (connection);
 
   if (allow)
@@ -583,14 +600,14 @@ auth_via_windows_user_function (DBusTransport *transport)
       return FALSE;
     }
                 
-  _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
+  _dbus_verbose ("unlock\n");
   _dbus_connection_unlock (connection);
 
   allow = (* windows_user_function) (connection,
                                      windows_sid,
                                      windows_user_data);
               
-  _dbus_verbose ("lock %s post windows user function\n", _DBUS_FUNCTION_NAME);
+  _dbus_verbose ("lock post windows user function\n");
   _dbus_connection_lock (connection);
 
   if (allow)
@@ -651,12 +668,14 @@ auth_via_default_rules (DBusTransport *transport)
       if (_dbus_credentials_include(our_identity,DBUS_CREDENTIAL_WINDOWS_SID))
           _dbus_verbose ("Client authorized as SID '%s'"
                          " but our SID is '%s', disconnecting\n",
-                         _dbus_credentials_get_windows_sid(our_identity),
-                         _dbus_credentials_get_windows_sid(our_identity));
+                         (_dbus_credentials_get_windows_sid(auth_identity) ?
+                          _dbus_credentials_get_windows_sid(auth_identity) : "<null>"),
+                         (_dbus_credentials_get_windows_sid(our_identity) ?
+                          _dbus_credentials_get_windows_sid(our_identity) : "<null>"));
       else
           _dbus_verbose ("Client authorized as UID "DBUS_UID_FORMAT
                          " but our UID is "DBUS_UID_FORMAT", disconnecting\n",
-                         _dbus_credentials_get_unix_uid(our_identity),
+                         _dbus_credentials_get_unix_uid(auth_identity),
                          _dbus_credentials_get_unix_uid(our_identity));
       _dbus_transport_disconnect (transport);
       allow = FALSE;
@@ -727,17 +746,6 @@ _dbus_transport_get_is_authenticated (DBusTransport *transport)
               _dbus_connection_unref_unlocked (transport->connection);
               return FALSE;
             }
-
-          if (transport->expected_guid == NULL)
-            {
-              transport->expected_guid = _dbus_strdup (server_guid);
-
-              if (transport->expected_guid == NULL)
-                {
-                  _dbus_verbose ("No memory to complete auth in %s\n", _DBUS_FUNCTION_NAME);
-                  return FALSE;
-                }
-            }
         }
 
       /* If we're the server, see if we want to allow this identity to proceed.
@@ -803,6 +811,18 @@ _dbus_transport_get_is_anonymous (DBusTransport *transport)
 }
 
 /**
+ * Returns TRUE if the transport supports sending unix fds.
+ *
+ * @param transport the transport
+ * @returns #TRUE if TRUE it is possible to send unix fds across the transport.
+ */
+dbus_bool_t
+_dbus_transport_can_pass_unix_fd(DBusTransport *transport)
+{
+  return DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport);
+}
+
+/**
  * Gets the address of a transport. It will be
  * #NULL for a server-side transport.
  *
@@ -827,6 +847,8 @@ _dbus_transport_get_server_id (DBusTransport *transport)
 {
   if (transport->is_server)
     return NULL;
+  else if (transport->authenticated)
+    return _dbus_auth_get_guid_from_server (transport->auth);
   else
     return transport->expected_guid;
 }
@@ -957,7 +979,7 @@ _dbus_transport_do_iteration (DBusTransport  *transport,
                                        timeout_milliseconds);
   _dbus_transport_unref (transport);
 
-  _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
+  _dbus_verbose ("end\n");
 }
 
 static dbus_bool_t
@@ -969,7 +991,7 @@ recover_unused_bytes (DBusTransport *transport)
       const DBusString *encoded;
       DBusString *buffer;
       int orig_len;
-      
+
       if (!_dbus_string_init (&plaintext))
         goto nomem;
       
@@ -982,12 +1004,12 @@ recover_unused_bytes (DBusTransport *transport)
           _dbus_string_free (&plaintext);
           goto nomem;
         }
-      
+
       _dbus_message_loader_get_buffer (transport->loader,
                                        &buffer);
       
       orig_len = _dbus_string_get_length (buffer);
-      
+
       if (!_dbus_string_move (&plaintext, 0, buffer,
                               orig_len))
         {
@@ -1019,7 +1041,7 @@ recover_unused_bytes (DBusTransport *transport)
                                        &buffer);
                 
       orig_len = _dbus_string_get_length (buffer);
-                
+
       _dbus_auth_get_unused_bytes (transport->auth,
                                    &bytes);
 
@@ -1059,7 +1081,8 @@ recover_unused_bytes (DBusTransport *transport)
 DBusDispatchStatus
 _dbus_transport_get_dispatch_status (DBusTransport *transport)
 {
-  if (_dbus_counter_get_value (transport->live_messages_size) >= transport->max_live_messages_size)
+  if (_dbus_counter_get_size_value (transport->live_messages) >= transport->max_live_messages_size ||
+      _dbus_counter_get_unix_fd_value (transport->live_messages) >= transport->max_live_messages_unix_fds)
     return DBUS_DISPATCH_COMPLETE; /* complete for now */
 
   if (!_dbus_transport_get_is_authenticated (transport))
@@ -1116,7 +1139,7 @@ _dbus_transport_queue_messages (DBusTransport *transport)
       
       _dbus_verbose ("queueing received message %p\n", message);
 
-      if (!_dbus_message_add_size_counter (message, transport->live_messages_size))
+      if (!_dbus_message_add_counter (message, transport->live_messages))
         {
           _dbus_message_loader_putback_message_link (transport->loader,
                                                      link);
@@ -1125,6 +1148,13 @@ _dbus_transport_queue_messages (DBusTransport *transport)
         }
       else
         {
+          /* We didn't call the notify function when we added the counter, so
+           * catch up now. Since we have the connection's lock, it's desirable
+           * that we bypass the notify function and call this virtual method
+           * directly. */
+          if (transport->vtable->live_messages_changed)
+            (* transport->vtable->live_messages_changed) (transport);
+
           /* pass ownership of link and message ref to connection */
           _dbus_connection_queue_received_message_link (transport->connection,
                                                         link);
@@ -1154,6 +1184,19 @@ _dbus_transport_set_max_message_size (DBusTransport  *transport,
 }
 
 /**
+ * See dbus_connection_set_max_message_unix_fds().
+ *
+ * @param transport the transport
+ * @param n the max number of unix fds of a single message
+ */
+void
+_dbus_transport_set_max_message_unix_fds (DBusTransport  *transport,
+                                          long            n)
+{
+  _dbus_message_loader_set_max_message_unix_fds (transport->loader, n);
+}
+
+/**
  * See dbus_connection_get_max_message_size().
  *
  * @param transport the transport
@@ -1166,6 +1209,18 @@ _dbus_transport_get_max_message_size (DBusTransport  *transport)
 }
 
 /**
+ * See dbus_connection_get_max_message_unix_fds().
+ *
+ * @param transport the transport
+ * @returns max message unix fds
+ */
+long
+_dbus_transport_get_max_message_unix_fds (DBusTransport  *transport)
+{
+  return _dbus_message_loader_get_max_message_unix_fds (transport->loader);
+}
+
+/**
  * See dbus_connection_set_max_received_size().
  *
  * @param transport the transport
@@ -1176,12 +1231,30 @@ _dbus_transport_set_max_received_size (DBusTransport  *transport,
                                        long            size)
 {
   transport->max_live_messages_size = size;
-  _dbus_counter_set_notify (transport->live_messages_size,
+  _dbus_counter_set_notify (transport->live_messages,
                             transport->max_live_messages_size,
-                            live_messages_size_notify,
+                            transport->max_live_messages_unix_fds,
+                            live_messages_notify,
                             transport);
 }
 
+/**
+ * See dbus_connection_set_max_received_unix_fds().
+ *
+ * @param transport the transport
+ * @param n the max unix fds of all incoming messages
+ */
+void
+_dbus_transport_set_max_received_unix_fds (DBusTransport  *transport,
+                                           long            n)
+{
+  transport->max_live_messages_unix_fds = n;
+  _dbus_counter_set_notify (transport->live_messages,
+                            transport->max_live_messages_size,
+                            transport->max_live_messages_unix_fds,
+                            live_messages_notify,
+                            transport);
+}
 
 /**
  * See dbus_connection_get_max_received_size().
@@ -1196,6 +1269,18 @@ _dbus_transport_get_max_received_size (DBusTransport  *transport)
 }
 
 /**
+ * See dbus_connection_set_max_received_unix_fds().
+ *
+ * @param transport the transport
+ * @returns max unix fds for all live messages
+ */
+long
+_dbus_transport_get_max_received_unix_fds (DBusTransport  *transport)
+{
+  return transport->max_live_messages_unix_fds;
+}
+
+/**
  * See dbus_connection_get_unix_user().
  *
  * @param transport the transport
@@ -1254,7 +1339,7 @@ _dbus_transport_get_unix_process_id (DBusTransport *transport,
   if (_dbus_credentials_include (auth_identity,
                                  DBUS_CREDENTIAL_UNIX_PROCESS_ID))
     {
-      *pid = _dbus_credentials_get_unix_pid (auth_identity);
+      *pid = _dbus_credentials_get_pid (auth_identity);
       return TRUE;
     }
   else
@@ -1408,4 +1493,26 @@ _dbus_transport_set_allow_anonymous (DBusTransport              *transport,
   transport->allow_anonymous = value != FALSE;
 }
 
+#ifdef DBUS_ENABLE_STATS
+void
+_dbus_transport_get_stats (DBusTransport  *transport,
+                           dbus_uint32_t  *queue_bytes,
+                           dbus_uint32_t  *queue_fds,
+                           dbus_uint32_t  *peak_queue_bytes,
+                           dbus_uint32_t  *peak_queue_fds)
+{
+  if (queue_bytes != NULL)
+    *queue_bytes = _dbus_counter_get_size_value (transport->live_messages);
+
+  if (queue_fds != NULL)
+    *queue_fds = _dbus_counter_get_unix_fd_value (transport->live_messages);
+
+  if (peak_queue_bytes != NULL)
+    *peak_queue_bytes = _dbus_counter_get_peak_size_value (transport->live_messages);
+
+  if (peak_queue_fds != NULL)
+    *peak_queue_fds = _dbus_counter_get_peak_unix_fd_value (transport->live_messages);
+}
+#endif /* DBUS_ENABLE_STATS */
+
 /** @} */