Merge branch 'dbus-1.4'
[platform/upstream/dbus.git] / dbus / dbus-bus.c
index bc8c107..f05ddea 100644 (file)
@@ -1,4 +1,4 @@
-/* -*- mode: C; c-file-style: "gnu" -*- */
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
 /* dbus-bus.c  Convenience functions for communicating with the bus.
  *
  * Copyright (C) 2003  CodeFactory AB
  * 
  * 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-bus.h"
 #include "dbus-protocol.h"
 #include "dbus-internals.h"
 #include "dbus-marshal-validate.h"
 #include "dbus-threads-internal.h"
 #include "dbus-connection-internal.h"
-#include <string.h>
+#include "dbus-string.h"
 
 /**
  * @defgroup DBusBus Message bus APIs
  * @ingroup DBus
  * @brief Functions for communicating with the message bus
  *
+ * dbus_bus_get() allows all modules and libraries in a given
+ * process to share the same connection to the bus daemon by storing
+ * the connection globally.
+ *
+ * All other functions in this module are just convenience functions;
+ * most of them invoke methods on the bus daemon, by sending method
+ * call messages to #DBUS_SERVICE_DBUS. These convenience functions
+ * often make blocking method calls. If you don't want to block,
+ * you can send the method call messages manually in the same way
+ * you would any other method call message.
+ *
+ * This module is the only one in libdbus that's specific to
+ * communicating with the message bus daemon. The rest of the API can
+ * also be used for connecting to another application directly.
+ * 
  * @todo right now the default address of the system bus is hardcoded,
  * so if you change it in the global config file suddenly you have to
  * set DBUS_SYSTEM_BUS_ADDRESS env variable.  Might be nice if the
@@ -84,6 +100,14 @@ static dbus_bool_t initialized = FALSE;
  */
 _DBUS_DEFINE_GLOBAL_LOCK (bus);
 
+/**
+ * Global lock covering all BusData on any connection. The bet is
+ * that some lock contention is better than more memory
+ * for a per-connection lock, but it's tough to imagine it mattering
+ * either way.
+ */
+_DBUS_DEFINE_GLOBAL_LOCK (bus_datas);
+
 static void
 addresses_shutdown_func (void *data)
 {
@@ -93,7 +117,7 @@ addresses_shutdown_func (void *data)
   while (i < N_BUS_TYPES)
     {
       if (bus_connections[i] != NULL)
-        _dbus_warn ("dbus_shutdown() called but connections were still live!");
+        _dbus_warn_check_failed ("dbus_shutdown() called but connections were still live. This probably means the application did not drop all its references to bus connections.\n");
       
       dbus_free (bus_connection_addresses[i]);
       bus_connection_addresses[i] = NULL;
@@ -101,6 +125,8 @@ addresses_shutdown_func (void *data)
     }
 
   activation_bus_type = DBUS_BUS_STARTER;
+
+  initialized = FALSE;
 }
 
 static dbus_bool_t
@@ -122,6 +148,63 @@ get_from_env (char           **connection_p,
 }
 
 static dbus_bool_t
+init_session_address (void)
+{
+  dbus_bool_t retval;
+  retval = FALSE;
+
+  /* First, look in the environment.  This is the normal case on 
+   * freedesktop.org/Unix systems. */
+  get_from_env (&bus_connection_addresses[DBUS_BUS_SESSION],
+                     "DBUS_SESSION_BUS_ADDRESS");
+  if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
+    {
+      dbus_bool_t supported;
+      DBusString addr;
+      DBusError error = DBUS_ERROR_INIT;
+
+      if (!_dbus_string_init (&addr))
+        return FALSE;
+
+      supported = FALSE;
+      /* So it's not in the environment - let's try a platform-specific method.
+       * On MacOS, this involves asking launchd.  On Windows (not specified yet)
+       * we might do a COM lookup.
+       * Ignore errors - if we failed, fall back to autolaunch. */
+      retval = _dbus_lookup_session_address (&supported, &addr, &error);
+      if (supported && retval)
+        {
+          retval =_dbus_string_steal_data (&addr, &bus_connection_addresses[DBUS_BUS_SESSION]);
+        }
+      else if (supported && !retval)
+        {
+          if (dbus_error_is_set(&error))
+            _dbus_warn ("Dynamic session lookup supported but failed: %s\n", error.message);
+          else
+            _dbus_warn ("Dynamic session lookup supported but failed silently\n");
+        }
+      _dbus_string_free (&addr);
+    }
+  else
+    retval = TRUE;
+
+  if (!retval)
+    return FALSE;
+
+  /* The DBUS_SESSION_BUS_DEFAULT_ADDRESS should have really been named
+   * DBUS_SESSION_BUS_FALLBACK_ADDRESS. 
+   */
+  if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
+    bus_connection_addresses[DBUS_BUS_SESSION] =
+      _dbus_strdup (DBUS_SESSION_BUS_DEFAULT_ADDRESS);
+  if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
+    return FALSE;
+
+  return TRUE;
+}
+
+static dbus_bool_t
 init_connections_unlocked (void)
 {
   if (!initialized)
@@ -173,17 +256,9 @@ init_connections_unlocked (void)
         {
           _dbus_verbose ("Filling in session bus address...\n");
           
-          if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SESSION],
-                             "DBUS_SESSION_BUS_ADDRESS"))
+          if (!init_session_address ())
             return FALSE;
 
-         if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
-           bus_connection_addresses[DBUS_BUS_SESSION] =
-             _dbus_strdup (DBUS_SESSION_BUS_DEFAULT_ADDRESS);
-          
-          if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
-             return FALSE;
-
           _dbus_verbose ("  \"%s\"\n", bus_connection_addresses[DBUS_BUS_SESSION] ?
                          bus_connection_addresses[DBUS_BUS_SESSION] : "none set");
         }
@@ -257,6 +332,10 @@ bus_data_free (void *data)
       int i;
       _DBUS_LOCK (bus);
       /* We may be stored in more than one slot */
+      /* This should now be impossible - these slots are supposed to
+       * be cleared on disconnect, so should not need to be cleared on
+       * finalize
+       */
       i = 0;
       while (i < N_BUS_TYPES)
         {
@@ -336,7 +415,6 @@ _dbus_bus_notify_shared_connection_disconnected_unlocked (DBusConnection *connec
       if (bus_connections[i] == connection)
         {
           bus_connections[i] = NULL;
-          _dbus_connection_unref_unlocked (connection);
         }
     }
 
@@ -410,12 +488,6 @@ internal_bus_get (DBusBusType  type,
       return NULL;
     }
 
-  /* By default we're bound to the lifecycle of
-   * the message bus.
-   */
-  dbus_connection_set_exit_on_disconnect (connection,
-                                          TRUE);
-  
   if (!dbus_bus_register (connection, error))
     {
       _DBUS_ASSERT_ERROR_IS_SET (error);
@@ -428,16 +500,27 @@ internal_bus_get (DBusBusType  type,
 
   if (!private)
     {
-      /* get a hard ref to the connection */
+      /* store a weak ref to the connection (dbus-connection.c is
+       * supposed to have a strong ref that it drops on disconnect,
+       * since this is a shared connection)
+       */
       bus_connections[type] = connection;
-      dbus_connection_ref (bus_connections[type]);
     }
-  
-  bd = ensure_bus_data (connection);
-  _dbus_assert (bd != NULL);
 
+  /* By default we're bound to the lifecycle of
+   * the message bus.
+   */
+  dbus_connection_set_exit_on_disconnect (connection,
+                                          TRUE);
+  _DBUS_LOCK (bus_datas);
+  bd = ensure_bus_data (connection);
+  _dbus_assert (bd != NULL); /* it should have been created on
+                                register, so OOM not possible */
   bd->is_well_known = TRUE;
+  _DBUS_UNLOCK (bus_datas);
 
+  
   _DBUS_UNLOCK (bus);
 
   /* Return a reference to the caller */
@@ -467,6 +550,11 @@ internal_bus_get (DBusBusType  type,
  * will exit if the connection closes. You can undo this
  * by calling dbus_connection_set_exit_on_disconnect() yourself
  * after you get the connection.
+ *
+ * dbus_bus_get() calls dbus_bus_register() for you.
+ * 
+ * If returning a newly-created connection, this function will block
+ * until authentication and bus registration are complete.
  * 
  * @param type bus type
  * @param error address where an error can be returned.
@@ -480,11 +568,11 @@ dbus_bus_get (DBusBusType  type,
 }
 
 /**
- * Connects to a bus daemon and registers the client with it as with dbus_bus_register().
- * Unlike dbus_bus_get(), always creates a new connection. This connection
- * will not be saved or recycled by libdbus. Caller owns a reference
- * to the bus and must either close it or know it to be closed
- * prior to releasing this reference.
+ * Connects to a bus daemon and registers the client with it as with
+ * dbus_bus_register().  Unlike dbus_bus_get(), always creates a new
+ * connection. This connection will not be saved or recycled by
+ * libdbus. Caller owns a reference to the bus and must either close
+ * it or know it to be closed prior to releasing this reference.
  *
  * See dbus_connection_open_private() for more details on when to
  * close and unref this connection.
@@ -495,6 +583,11 @@ dbus_bus_get (DBusBusType  type,
  * by calling dbus_connection_set_exit_on_disconnect() yourself
  * after you get the connection.
  *
+ * dbus_bus_get_private() calls dbus_bus_register() for you.
+ *
+ * This function will block until authentication and bus registration
+ * are complete.
+ *
  * @param type bus type
  * @param error address where an error can be returned.
  * @returns a DBusConnection with new ref
@@ -512,9 +605,45 @@ dbus_bus_get_private (DBusBusType  type,
  * If registration succeeds, the unique name will be set,
  * and can be obtained using dbus_bus_get_unique_name().
  *
+ * This function will block until registration is complete.
+ *
+ * If the connection has already registered with the bus
+ * (determined by checking whether dbus_bus_get_unique_name()
+ * returns a non-#NULL value), then this function does nothing.
+ *
  * If you use dbus_bus_get() or dbus_bus_get_private() this
  * function will be called for you.
  * 
+ * @note Just use dbus_bus_get() or dbus_bus_get_private() instead of
+ * dbus_bus_register() and save yourself some pain. Using
+ * dbus_bus_register() manually is only useful if you have your
+ * own custom message bus not found in #DBusBusType.
+ *
+ * If you open a bus connection with dbus_connection_open() or
+ * dbus_connection_open_private() you will have to dbus_bus_register()
+ * yourself, or make the appropriate registration method calls
+ * yourself. If you send the method calls yourself, call
+ * dbus_bus_set_unique_name() with the unique bus name you get from
+ * the bus.
+ *
+ * For shared connections (created with dbus_connection_open()) in a
+ * multithreaded application, you can't really make the registration
+ * calls yourself, because you don't know whether some other thread is
+ * also registering, and the bus will kick you off if you send two
+ * registration messages.
+ *
+ * If you use dbus_bus_register() however, there is a lock that
+ * keeps both apps from registering at the same time.
+ *
+ * The rule in a multithreaded app, then, is that dbus_bus_register()
+ * must be used to register, or you need to have your own locks that
+ * all threads in the app will respect.
+ *
+ * In a single-threaded application you can register by hand instead
+ * of using dbus_bus_register(), as long as you check
+ * dbus_bus_get_unique_name() to see if a unique name has already been
+ * stored by another thread before you send the registration messages.
+ * 
  * @param connection the connection
  * @param error place to store errors
  * @returns #TRUE on success
@@ -532,21 +661,25 @@ dbus_bus_register (DBusConnection *connection,
   _dbus_return_val_if_error_is_set (error, FALSE);
 
   retval = FALSE;
-  
+  message = NULL;
+  reply = NULL;
+
+  _DBUS_LOCK (bus_datas);
+
   bd = ensure_bus_data (connection);
   if (bd == NULL)
     {
       _DBUS_SET_OOM (error);
-      return FALSE;
+      goto out;
     }
 
   if (bd->unique_name != NULL)
     {
-      _dbus_warn ("Attempt to register the same DBusConnection with the message bus, but it is already registered\n");
-      /* This isn't an error, it's a programming bug. We'll be nice
-       * and not _dbus_assert_not_reached()
-       */
-      return TRUE;
+      _dbus_verbose ("Ignoring attempt to register the same DBusConnection %s with the message bus a second time.\n",
+                     bd->unique_name);
+      /* Success! */
+      retval = TRUE;
+      goto out;
     }
   
   message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
@@ -557,13 +690,11 @@ dbus_bus_register (DBusConnection *connection,
   if (!message)
     {
       _DBUS_SET_OOM (error);
-      return FALSE;
+      goto out;
     }
   
   reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
 
-  dbus_message_unref (message);
-  
   if (reply == NULL)
     goto out;
   else if (dbus_set_error_from_message (error, reply))
@@ -583,20 +714,50 @@ dbus_bus_register (DBusConnection *connection,
   retval = TRUE;
   
  out:
+  _DBUS_UNLOCK (bus_datas);
+
+  if (message)
+    dbus_message_unref (message);
+
   if (reply)
     dbus_message_unref (reply);
 
   if (!retval)
     _DBUS_ASSERT_ERROR_IS_SET (error);
-  
+
   return retval;
 }
 
 
 /**
- * Sets the unique name of the connection.  Can only be used if you
- * registered with the bus manually (i.e. if you did not call
- * dbus_bus_register()). Can only be called once per connection.
+ * Sets the unique name of the connection, as assigned by the message
+ * bus.  Can only be used if you registered with the bus manually
+ * (i.e. if you did not call dbus_bus_register()). Can only be called
+ * once per connection.  After the unique name is set, you can get it
+ * with dbus_bus_get_unique_name().
+ *
+ * The only reason to use this function is to re-implement the
+ * equivalent of dbus_bus_register() yourself. One (probably unusual)
+ * reason to do that might be to do the bus registration call
+ * asynchronously instead of synchronously.
+ *
+ * @note Just use dbus_bus_get() or dbus_bus_get_private(), or worst
+ * case dbus_bus_register(), instead of messing with this
+ * function. There's really no point creating pain for yourself by
+ * doing things manually.
+ *
+ * It's hard to use this function safely on shared connections
+ * (created by dbus_connection_open()) in a multithreaded application,
+ * because only one registration attempt can be sent to the bus. If
+ * two threads are both sending the registration message, there is no
+ * mechanism in libdbus itself to avoid sending it twice.
+ *
+ * Thus, you need a way to coordinate which thread sends the
+ * registration attempt; which also means you know which thread
+ * will call dbus_bus_set_unique_name(). If you don't know
+ * about all threads in the app (for example, if some libraries
+ * you're using might start libdbus-using threads), then you
+ * need to avoid using this function on shared connections.
  *
  * @param connection the connection
  * @param unique_name the unique name
@@ -607,55 +768,90 @@ dbus_bus_set_unique_name (DBusConnection *connection,
                           const char     *unique_name)
 {
   BusData *bd;
+  dbus_bool_t success = FALSE;
 
   _dbus_return_val_if_fail (connection != NULL, FALSE);
   _dbus_return_val_if_fail (unique_name != NULL, FALSE);
+
+  _DBUS_LOCK (bus_datas);
   
   bd = ensure_bus_data (connection);
   if (bd == NULL)
-    return FALSE;
+    goto out;
 
   _dbus_assert (bd->unique_name == NULL);
   
   bd->unique_name = _dbus_strdup (unique_name);
-  return bd->unique_name != NULL;
+  success = bd->unique_name != NULL;
+
+out:
+  _DBUS_UNLOCK (bus_datas);
+  
+  return success;
 }
 
 /**
- * Gets the unique name of the connection.  Only possible after the
- * connection has been registered with the message bus.
+ * Gets the unique name of the connection as assigned by the message
+ * bus. Only possible after the connection has been registered with
+ * the message bus. All connections returned by dbus_bus_get() or
+ * dbus_bus_get_private() have been successfully registered.
  *
- * The name remains valid for the duration of the connection and
+ * The name remains valid until the connection is freed, and
  * should not be freed by the caller.
+ *
+ * Other than dbus_bus_get(), there are two ways to set the unique
+ * name; one is dbus_bus_register(), the other is
+ * dbus_bus_set_unique_name().  You are responsible for calling
+ * dbus_bus_set_unique_name() if you register by hand instead of using
+ * dbus_bus_register().
  * 
  * @param connection the connection
- * @returns the unique name or NULL on error
+ * @returns the unique name or #NULL on error
  */
 const char*
 dbus_bus_get_unique_name (DBusConnection *connection)
 {
   BusData *bd;
+  const char *unique_name = NULL;
 
   _dbus_return_val_if_fail (connection != NULL, NULL);
+
+  _DBUS_LOCK (bus_datas);
   
   bd = ensure_bus_data (connection);
   if (bd == NULL)
-    return NULL;
-  
-  return bd->unique_name;
+    goto out;
+
+  unique_name = bd->unique_name;
+
+out:
+  _DBUS_UNLOCK (bus_datas);
+
+  return unique_name;
 }
 
 /**
- * Asks the bus to return the uid of the named
- * connection.
+ * Asks the bus to return the UID the named connection authenticated
+ * as, if any.  Only works on UNIX; only works for connections on the
+ * same machine as the bus. If you are not on the same machine as the
+ * bus, then calling this is probably a bad idea, since the UID will
+ * mean little to your application.
+ *
+ * For the system message bus you're guaranteed to be on the same
+ * machine since it only listens on a UNIX domain socket (at least,
+ * as shipped by default).
  *
- * Not going to work on Windows, the bus should return
- * an error then.
+ * This function only works for connections that authenticated as
+ * a UNIX user, right now that includes all bus connections, but
+ * it's very possible to have connections with no associated UID.
+ * So check for errors and do something sensible if they happen.
+ * 
+ * This function will always return an error on Windows.
  * 
  * @param connection the connection
  * @param name a name owned by the connection
  * @param error location to store the error
- * @returns a result code, -1 if error is set
+ * @returns the unix user id, or ((unsigned)-1) if error is set
  */ 
 unsigned long
 dbus_bus_get_unix_user (DBusConnection *connection,
@@ -722,6 +918,85 @@ dbus_bus_get_unix_user (DBusConnection *connection,
   return (unsigned long) uid;
 }
 
+/**
+ * Asks the bus to return its globally unique ID, as described in the
+ * D-Bus specification. For the session bus, this is useful as a way
+ * to uniquely identify each user session. For the system bus,
+ * probably the bus ID is not useful; instead, use the machine ID
+ * since it's accessible without necessarily connecting to the bus and
+ * may be persistent beyond a single bus instance (across reboots for
+ * example). See dbus_get_local_machine_id().
+ *
+ * In addition to an ID for each bus and an ID for each machine, there is
+ * an ID for each address that the bus is listening on; that can
+ * be retrieved with dbus_connection_get_server_id(), though it is
+ * probably not very useful.
+ * 
+ * @param connection the connection
+ * @param error location to store the error
+ * @returns the bus ID or #NULL if error is set
+ */ 
+char*
+dbus_bus_get_id (DBusConnection *connection,
+                 DBusError      *error)
+{
+  DBusMessage *message, *reply;
+  char *id;
+  const char *v_STRING;
+
+  _dbus_return_val_if_fail (connection != NULL, NULL);
+  _dbus_return_val_if_error_is_set (error, NULL);
+  
+  message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
+                                          DBUS_PATH_DBUS,
+                                          DBUS_INTERFACE_DBUS,
+                                          "GetId");
+  
+  if (message == NULL)
+    {
+      _DBUS_SET_OOM (error);
+      return NULL;
+    }
+  
+  reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
+                                                     error);
+  
+  dbus_message_unref (message);
+  
+  if (reply == NULL)
+    {
+      _DBUS_ASSERT_ERROR_IS_SET (error);
+      return NULL;
+    }  
+
+  if (dbus_set_error_from_message (error, reply))
+    {
+      _DBUS_ASSERT_ERROR_IS_SET (error);
+      dbus_message_unref (reply);
+      return NULL;
+    }
+
+  v_STRING = NULL;
+  if (!dbus_message_get_args (reply, error,
+                              DBUS_TYPE_STRING, &v_STRING,
+                              DBUS_TYPE_INVALID))
+    {
+      _DBUS_ASSERT_ERROR_IS_SET (error);
+      dbus_message_unref (reply);
+      return NULL;
+    }
+
+  id = _dbus_strdup (v_STRING); /* may be NULL */
+  
+  dbus_message_unref (reply);
+
+  if (id == NULL)
+    _DBUS_SET_OOM (error);
+
+  /* FIXME it might be nice to cache the ID locally */
+  
+  return id;
+}
 
 /**
  * Asks the bus to assign the given name to this connection by invoking
@@ -730,17 +1005,46 @@ dbus_bus_get_unix_user (DBusConnection *connection,
  * result codes are discussed here, but the specification is the
  * canonical version of this information.
  *
- * The #DBUS_NAME_FLAG_ALLOW_REPLACEMENT flag indicates that the caller
- * will allow other services to take over the name from the current owner.
+ * First you should know that for each bus name, the bus stores
+ * a queue of connections that would like to own it. Only
+ * one owns it at a time - called the primary owner. If the primary
+ * owner releases the name or disconnects, then the next owner in the
+ * queue atomically takes over.
+ *
+ * So for example if you have an application org.freedesktop.TextEditor
+ * and multiple instances of it can be run, you can have all of them
+ * sitting in the queue. The first one to start up will receive messages
+ * sent to org.freedesktop.TextEditor, but if that one exits another
+ * will become the primary owner and receive messages.
+ *
+ * The queue means you don't need to manually watch for the current owner to
+ * disappear and then request the name again.
+ *
+ * When requesting a name, you can specify several flags.
+ * 
+ * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT and #DBUS_NAME_FLAG_DO_NOT_QUEUE
+ * are properties stored by the bus for this connection with respect to
+ * each requested bus name. These properties are stored even if the
+ * connection is queued and does not become the primary owner.
+ * You can update these flags by calling RequestName again (even if
+ * you already own the name).
+ *
+ * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT means that another requestor of the
+ * name can take it away from you by specifying #DBUS_NAME_FLAG_REPLACE_EXISTING.
  *
- * The #DBUS_NAME_FLAG_REPLACE_EXISTING flag indicates that the caller
- * would like to take over the name from the current owner.
- * If the current name owner did not use #DBUS_NAME_FLAG_ALLOW_REPLACEMENT
- * then this flag indicates that the caller would like to be placed
- * in the queue to own the name when the current owner lets go.
+ * #DBUS_NAME_FLAG_DO_NOT_QUEUE means that if you aren't the primary owner,
+ * you don't want to be queued up - you only care about being the
+ * primary owner.
+ *
+ * Unlike the other two flags, #DBUS_NAME_FLAG_REPLACE_EXISTING is a property
+ * of the individual RequestName call, i.e. the bus does not persistently
+ * associate it with the connection-name pair. If a RequestName call includes
+ * the #DBUS_NAME_FLAG_REPLACE_EXISTING flag, and the current primary
+ * owner has #DBUS_NAME_FLAG_ALLOW_REPLACEMENT set, then the current primary
+ * owner will be kicked off.
  *
  * If no flags are given, an application will receive the requested
- * name only if the name is currently unowned; it will NOT give
+ * name only if the name is currently unowned; and it will NOT give
  * up the name if another application asks to take it over using
  * #DBUS_NAME_FLAG_REPLACE_EXISTING.
  *
@@ -766,7 +1070,9 @@ dbus_bus_get_unix_user (DBusConnection *connection,
  * #DBUS_NAME_FLAG_REPLACE_EXISTING.
  *
  * #DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER happens if an application
- * requests a name it already owns.
+ * requests a name it already owns. (Re-requesting a name is useful if
+ * you want to change the #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or
+ * #DBUS_NAME_FLAG_DO_NOT_QUEUE settings.)
  *
  * When a service represents an application, say "text editor," then
  * it should specify #DBUS_NAME_FLAG_ALLOW_REPLACEMENT if it wants
@@ -775,7 +1081,19 @@ dbus_bus_get_unix_user (DBusConnection *connection,
  * specify #DBUS_NAME_FLAG_REPLACE_EXISTING to either take over
  * (last-started-wins) or be queued up (first-started-wins) according
  * to whether #DBUS_NAME_FLAG_ALLOW_REPLACEMENT was given.
- * 
+ *
+ * Conventionally, single-instance applications often offer a command
+ * line option called --replace which means to replace the current
+ * instance.  To implement this, always set
+ * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT when you request your
+ * application's bus name.  When you lose ownership of your bus name,
+ * you need to exit.  Look for the signal "NameLost" from
+ * #DBUS_SERVICE_DBUS and #DBUS_INTERFACE_DBUS (the signal's first
+ * argument is the bus name that was lost).  If starting up without
+ * --replace, do not specify #DBUS_NAME_FLAG_REPLACE_EXISTING, and
+ * exit if you fail to become the bus name owner. If --replace is
+ * given, ask to replace the old owner.
+ *
  * @param connection the connection
  * @param name the name to request
  * @param flags flags
@@ -851,10 +1169,18 @@ dbus_bus_request_name (DBusConnection *connection,
 
 
 /**
- * Asks the bus to unassign the given name to this connection by invoking
- * the ReleaseName method on the bus. This method is fully documented
- * in the D-Bus specification.
+ * Asks the bus to unassign the given name from this connection by
+ * invoking the ReleaseName method on the bus. The "ReleaseName"
+ * method is canonically documented in the D-Bus specification.
  *
+ * Possible results are: #DBUS_RELEASE_NAME_REPLY_RELEASED
+ * which means you owned the name or were in the queue to own it,
+ * and and now you don't own it and aren't in the queue.
+ * #DBUS_RELEASE_NAME_REPLY_NOT_OWNER which means someone else
+ * owns the name so you can't release it.
+ * #DBUS_RELEASE_NAME_REPLY_NON_EXISTENT
+ * which means nobody owned the name.
+ * 
  * @param connection the connection
  * @param name the name to remove 
  * @param error location to store the error
@@ -926,8 +1252,17 @@ dbus_bus_release_name (DBusConnection *connection,
 }
 
 /**
- * Checks whether a certain name has an owner.
+ * Asks the bus whether a certain name has an owner.
  *
+ * Using this can easily result in a race condition,
+ * since an owner can appear or disappear after you
+ * call this.
+ *
+ * If you want to request a name, just request it;
+ * if you want to avoid replacing a current owner,
+ * don't specify #DBUS_NAME_FLAG_REPLACE_EXISTING and
+ * you will get an error if there's already an owner.
+ * 
  * @param connection the connection
  * @param name the name
  * @param error location to store any errors
@@ -996,6 +1331,12 @@ dbus_bus_name_has_owner (DBusConnection *connection,
  * The flags parameter is for future expansion, currently you should
  * specify 0.
  *
+ * It's often easier to avoid explicitly starting services, and
+ * just send a method call to the service's bus name instead.
+ * Method calls start a service to handle them by default
+ * unless you call dbus_message_set_auto_start() to disable this
+ * behavior.
+ * 
  * @param connection the connection
  * @param name the name we want the new service to request
  * @param flags the flags (should always be 0 for now)
@@ -1092,11 +1433,17 @@ send_no_return_values (DBusConnection *connection,
  * If you pass #NULL for the error, this function will not
  * block; the match thus won't be added until you flush the
  * connection, and if there's an error adding the match
- * (only possible error is lack of resources in the bus),
- * you won't find out about it.
+ * you won't find out about it. This is generally acceptable, since the
+ * possible errors (including a lack of resources in the bus, the connection
+ * having exceeded its quota of active match rules, or the match rule being
+ * unparseable) are generally unrecoverable.
  *
  * If you pass non-#NULL for the error this function will
- * block until it gets a reply.
+ * block until it gets a reply. This may be useful when using match rule keys
+ * introduced in recent versions of D-Bus, like 'arg0namespace', to allow the
+ * application to fall back to less efficient match rules supported by older
+ * versions of the daemon if the running version is not new enough; or when
+ * using user-supplied rules rather than rules hard-coded at compile time.
  *
  * Normal API conventions would have the function return
  * a boolean value indicating whether the error was set,
@@ -1115,11 +1462,13 @@ send_no_return_values (DBusConnection *connection,
  * path='/bar/foo',destination=':452345.34'"
  *
  * Possible keys you can match on are type, sender, 
- * interface, member, path, destination and the special
- * arg keys.  Excluding a key from the rule indicates 
- * a wildcard match.  For instance excluding the
+ * interface, member, path, destination and numbered
+ * keys to match message args (keys are 'arg0', 'arg1', etc.).
+ * Omitting a key from the rule indicates 
+ * a wildcard match.  For instance omitting
  * the member from a match rule but adding a sender would
- * let all messages from that sender through.  
+ * let all messages from that sender through regardless of
+ * the member.
  *
  * Matches are inclusive not exclusive so as long as one 
  * rule matches the message will get through.  It is important
@@ -1128,12 +1477,38 @@ send_no_return_values (DBusConnection *connection,
  * can cause performance problems such as draining batteries
  * on embedded platforms.
  *
- * The special arg keys are used for further restricting the 
- * match based on the parameters sent by the signal or method.
- * For instance arg1='foo' will check the first argument, 
- * arg2='bar' the second and so on.  For performance reasons
- * there is a set limit on the highest number parameter that
- * can be checked which is set in dbus-protocol.h
+ * If you match message args ('arg0', 'arg1', and so forth)
+ * only string arguments will match. That is, arg0='5' means
+ * match the string "5" not the integer 5.
+ *
+ * Currently there is no way to match against non-string arguments.
+ *
+ * A specialised form of wildcard matching on arguments is
+ * supported for path-like namespaces.  If your argument match has
+ * a 'path' suffix (eg: "arg0path='/some/path/'") then it is
+ * considered a match if the argument exactly matches the given
+ * string or if one of them ends in a '/' and is a prefix of the
+ * other.
+ *
+ * Matching on interface is tricky because method call
+ * messages only optionally specify the interface.
+ * If a message omits the interface, then it will NOT match
+ * if the rule specifies an interface name. This means match
+ * rules on method calls should not usually give an interface.
+ *
+ * However, signal messages are required to include the interface
+ * so when matching signals usually you should specify the interface
+ * in the match rule.
+ * 
+ * For security reasons, you can match arguments only up to
+ * #DBUS_MAXIMUM_MATCH_RULE_ARG_NUMBER.
+ *
+ * Match rules have a maximum length of #DBUS_MAXIMUM_MATCH_RULE_LENGTH
+ * bytes.
+ *
+ * Both of these maximums are much higher than you're likely to need,
+ * they only exist because the D-Bus bus daemon has fixed limits on
+ * all resource usage.
  *
  * @param connection connection to the message bus
  * @param rule textual form of match rule
@@ -1177,6 +1552,10 @@ dbus_bus_add_match (DBusConnection *connection,
  * recently-added identical rule gets removed).  The "rule" argument
  * is the string form of a match rule.
  *
+ * The bus compares match rules semantically, not textually, so
+ * whitespace and ordering don't have to be identical to
+ * the rule you passed to dbus_bus_add_match().
+ * 
  * If you pass #NULL for the error, this function will not
  * block; otherwise it will. See detailed explanation in
  * docs for dbus_bus_add_match().