Home | History | Annotate | Download | only in dbus
      1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
      2 /* dbus-bus.c  Convenience functions for communicating with the bus.
      3  *
      4  * Copyright (C) 2003  CodeFactory AB
      5  * Copyright (C) 2003  Red Hat, Inc.
      6  *
      7  * Licensed under the Academic Free License version 2.1
      8  *
      9  * This program is free software; you can redistribute it and/or modify
     10  * it under the terms of the GNU General Public License as published by
     11  * the Free Software Foundation; either version 2 of the License, or
     12  * (at your option) any later version.
     13  *
     14  * This program is distributed in the hope that it will be useful,
     15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17  * GNU General Public License for more details.
     18  *
     19  * You should have received a copy of the GNU General Public License
     20  * along with this program; if not, write to the Free Software
     21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
     22  *
     23  */
     24 
     25 #include <config.h>
     26 #include "dbus-bus.h"
     27 #include "dbus-protocol.h"
     28 #include "dbus-internals.h"
     29 #include "dbus-message.h"
     30 #include "dbus-marshal-validate.h"
     31 #include "dbus-threads-internal.h"
     32 #include "dbus-connection-internal.h"
     33 #include "dbus-string.h"
     34 
     35 /**
     36  * @defgroup DBusBus Message bus APIs
     37  * @ingroup DBus
     38  * @brief Functions for communicating with the message bus
     39  *
     40  * dbus_bus_get() allows all modules and libraries in a given
     41  * process to share the same connection to the bus daemon by storing
     42  * the connection globally.
     43  *
     44  * All other functions in this module are just convenience functions;
     45  * most of them invoke methods on the bus daemon, by sending method
     46  * call messages to #DBUS_SERVICE_DBUS. These convenience functions
     47  * often make blocking method calls. If you don't want to block,
     48  * you can send the method call messages manually in the same way
     49  * you would any other method call message.
     50  *
     51  * This module is the only one in libdbus that's specific to
     52  * communicating with the message bus daemon. The rest of the API can
     53  * also be used for connecting to another application directly.
     54  *
     55  * @todo right now the default address of the system bus is hardcoded,
     56  * so if you change it in the global config file suddenly you have to
     57  * set DBUS_SYSTEM_BUS_ADDRESS env variable.  Might be nice if the
     58  * client lib somehow read the config file, or if the bus on startup
     59  * somehow wrote out its address to a well-known spot, but might also
     60  * not be worth it.
     61  */
     62 
     63 /**
     64  * @defgroup DBusBusInternals Message bus APIs internals
     65  * @ingroup DBusInternals
     66  * @brief Internals of functions for communicating with the message bus
     67  *
     68  * @{
     69  */
     70 
     71 /**
     72  * Block of message-bus-related data we attach to each
     73  * #DBusConnection used with these convenience functions.
     74  *
     75  */
     76 typedef struct
     77 {
     78   DBusConnection *connection; /**< Connection we're associated with */
     79   char *unique_name; /**< Unique name of this connection */
     80 
     81   unsigned int is_well_known : 1; /**< Is one of the well-known connections in our global array */
     82 } BusData;
     83 
     84 /** The slot we have reserved to store BusData.
     85  */
     86 static dbus_int32_t bus_data_slot = -1;
     87 
     88 /** Number of bus types */
     89 #define N_BUS_TYPES 3
     90 
     91 static DBusConnection *bus_connections[N_BUS_TYPES];
     92 static char *bus_connection_addresses[N_BUS_TYPES] = { NULL, NULL, NULL };
     93 
     94 static DBusBusType activation_bus_type = DBUS_BUS_STARTER;
     95 
     96 static dbus_bool_t initialized = FALSE;
     97 
     98 /**
     99  * Lock for globals in this file
    100  */
    101 _DBUS_DEFINE_GLOBAL_LOCK (bus);
    102 
    103 /**
    104  * Global lock covering all BusData on any connection. The bet is
    105  * that some lock contention is better than more memory
    106  * for a per-connection lock, but it's tough to imagine it mattering
    107  * either way.
    108  */
    109 _DBUS_DEFINE_GLOBAL_LOCK (bus_datas);
    110 
    111 static void
    112 addresses_shutdown_func (void *data)
    113 {
    114   int i;
    115 
    116   i = 0;
    117   while (i < N_BUS_TYPES)
    118     {
    119       if (bus_connections[i] != NULL)
    120         _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");
    121 
    122       dbus_free (bus_connection_addresses[i]);
    123       bus_connection_addresses[i] = NULL;
    124       ++i;
    125     }
    126 
    127   activation_bus_type = DBUS_BUS_STARTER;
    128 
    129   initialized = FALSE;
    130 }
    131 
    132 static dbus_bool_t
    133 get_from_env (char           **connection_p,
    134               const char      *env_var)
    135 {
    136   const char *s;
    137 
    138   _dbus_assert (*connection_p == NULL);
    139 
    140   s = _dbus_getenv (env_var);
    141   if (s == NULL || *s == '\0')
    142     return TRUE; /* successfully didn't use the env var */
    143   else
    144     {
    145       *connection_p = _dbus_strdup (s);
    146       return *connection_p != NULL;
    147     }
    148 }
    149 
    150 static dbus_bool_t
    151 init_session_address (void)
    152 {
    153   dbus_bool_t retval;
    154 
    155   retval = FALSE;
    156 
    157   /* First, look in the environment.  This is the normal case on
    158    * freedesktop.org/Unix systems. */
    159   get_from_env (&bus_connection_addresses[DBUS_BUS_SESSION],
    160                      "DBUS_SESSION_BUS_ADDRESS");
    161   if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
    162     {
    163       dbus_bool_t supported;
    164       DBusString addr;
    165       DBusError error = DBUS_ERROR_INIT;
    166 
    167       if (!_dbus_string_init (&addr))
    168         return FALSE;
    169 
    170       supported = FALSE;
    171       /* So it's not in the environment - let's try a platform-specific method.
    172        * On MacOS, this involves asking launchd.  On Windows (not specified yet)
    173        * we might do a COM lookup.
    174        * Ignore errors - if we failed, fall back to autolaunch. */
    175       retval = _dbus_lookup_session_address (&supported, &addr, &error);
    176       if (supported && retval)
    177         {
    178           retval =_dbus_string_steal_data (&addr, &bus_connection_addresses[DBUS_BUS_SESSION]);
    179         }
    180       else if (supported && !retval)
    181         {
    182           if (dbus_error_is_set(&error))
    183             _dbus_warn ("Dynamic session lookup supported but failed: %s\n", error.message);
    184           else
    185             _dbus_warn ("Dynamic session lookup supported but failed silently\n");
    186         }
    187       _dbus_string_free (&addr);
    188     }
    189   else
    190     retval = TRUE;
    191 
    192   if (!retval)
    193     return FALSE;
    194 
    195   /* The DBUS_SESSION_BUS_DEFAULT_ADDRESS should have really been named
    196    * DBUS_SESSION_BUS_FALLBACK_ADDRESS.
    197    */
    198   if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
    199     bus_connection_addresses[DBUS_BUS_SESSION] =
    200       _dbus_strdup (DBUS_SESSION_BUS_DEFAULT_ADDRESS);
    201   if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
    202     return FALSE;
    203 
    204   return TRUE;
    205 }
    206 
    207 static dbus_bool_t
    208 init_connections_unlocked (void)
    209 {
    210   if (!initialized)
    211     {
    212       const char *s;
    213       int i;
    214 
    215       i = 0;
    216       while (i < N_BUS_TYPES)
    217         {
    218           bus_connections[i] = NULL;
    219           ++i;
    220         }
    221 
    222       /* Don't init these twice, we may run this code twice if
    223        * init_connections_unlocked() fails midway through.
    224        * In practice, each block below should contain only one
    225        * "return FALSE" or running through twice may not
    226        * work right.
    227        */
    228 
    229        if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
    230          {
    231            _dbus_verbose ("Filling in system bus address...\n");
    232 
    233            if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SYSTEM],
    234                               "DBUS_SYSTEM_BUS_ADDRESS"))
    235              return FALSE;
    236          }
    237 
    238 
    239        if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
    240          {
    241            /* Use default system bus address if none set in environment */
    242            bus_connection_addresses[DBUS_BUS_SYSTEM] =
    243              _dbus_strdup (DBUS_SYSTEM_BUS_DEFAULT_ADDRESS);
    244 
    245            if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
    246              return FALSE;
    247 
    248            _dbus_verbose ("  used default system bus \"%s\"\n",
    249                           bus_connection_addresses[DBUS_BUS_SYSTEM]);
    250          }
    251        else
    252          _dbus_verbose ("  used env var system bus \"%s\"\n",
    253                         bus_connection_addresses[DBUS_BUS_SYSTEM]);
    254 
    255       if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
    256         {
    257           _dbus_verbose ("Filling in session bus address...\n");
    258 
    259           if (!init_session_address ())
    260             return FALSE;
    261 
    262           _dbus_verbose ("  \"%s\"\n", bus_connection_addresses[DBUS_BUS_SESSION] ?
    263                          bus_connection_addresses[DBUS_BUS_SESSION] : "none set");
    264         }
    265 
    266       if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
    267         {
    268           _dbus_verbose ("Filling in activation bus address...\n");
    269 
    270           if (!get_from_env (&bus_connection_addresses[DBUS_BUS_STARTER],
    271                              "DBUS_STARTER_ADDRESS"))
    272             return FALSE;
    273 
    274           _dbus_verbose ("  \"%s\"\n", bus_connection_addresses[DBUS_BUS_STARTER] ?
    275                          bus_connection_addresses[DBUS_BUS_STARTER] : "none set");
    276         }
    277 
    278 
    279       if (bus_connection_addresses[DBUS_BUS_STARTER] != NULL)
    280         {
    281           s = _dbus_getenv ("DBUS_STARTER_BUS_TYPE");
    282 
    283           if (s != NULL)
    284             {
    285               _dbus_verbose ("Bus activation type was set to \"%s\"\n", s);
    286 
    287               if (strcmp (s, "system") == 0)
    288                 activation_bus_type = DBUS_BUS_SYSTEM;
    289               else if (strcmp (s, "session") == 0)
    290                 activation_bus_type = DBUS_BUS_SESSION;
    291             }
    292         }
    293       else
    294         {
    295           /* Default to the session bus instead if available */
    296           if (bus_connection_addresses[DBUS_BUS_SESSION] != NULL)
    297             {
    298               bus_connection_addresses[DBUS_BUS_STARTER] =
    299                 _dbus_strdup (bus_connection_addresses[DBUS_BUS_SESSION]);
    300               if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
    301                 return FALSE;
    302             }
    303         }
    304 
    305       /* If we return FALSE we have to be sure that restarting
    306        * the above code will work right
    307        */
    308 
    309       if (!_dbus_setenv ("DBUS_ACTIVATION_ADDRESS", NULL))
    310         return FALSE;
    311 
    312       if (!_dbus_setenv ("DBUS_ACTIVATION_BUS_TYPE", NULL))
    313         return FALSE;
    314 
    315       if (!_dbus_register_shutdown_func (addresses_shutdown_func,
    316                                          NULL))
    317         return FALSE;
    318 
    319       initialized = TRUE;
    320     }
    321 
    322   return initialized;
    323 }
    324 
    325 static void
    326 bus_data_free (void *data)
    327 {
    328   BusData *bd = data;
    329 
    330   if (bd->is_well_known)
    331     {
    332       int i;
    333       _DBUS_LOCK (bus);
    334       /* We may be stored in more than one slot */
    335       /* This should now be impossible - these slots are supposed to
    336        * be cleared on disconnect, so should not need to be cleared on
    337        * finalize
    338        */
    339       i = 0;
    340       while (i < N_BUS_TYPES)
    341         {
    342           if (bus_connections[i] == bd->connection)
    343             bus_connections[i] = NULL;
    344 
    345           ++i;
    346         }
    347       _DBUS_UNLOCK (bus);
    348     }
    349 
    350   dbus_free (bd->unique_name);
    351   dbus_free (bd);
    352 
    353   dbus_connection_free_data_slot (&bus_data_slot);
    354 }
    355 
    356 static BusData*
    357 ensure_bus_data (DBusConnection *connection)
    358 {
    359   BusData *bd;
    360 
    361   if (!dbus_connection_allocate_data_slot (&bus_data_slot))
    362     return NULL;
    363 
    364   bd = dbus_connection_get_data (connection, bus_data_slot);
    365   if (bd == NULL)
    366     {
    367       bd = dbus_new0 (BusData, 1);
    368       if (bd == NULL)
    369         {
    370           dbus_connection_free_data_slot (&bus_data_slot);
    371           return NULL;
    372         }
    373 
    374       bd->connection = connection;
    375 
    376       if (!dbus_connection_set_data (connection, bus_data_slot, bd,
    377                                      bus_data_free))
    378         {
    379           dbus_free (bd);
    380           dbus_connection_free_data_slot (&bus_data_slot);
    381           return NULL;
    382         }
    383 
    384       /* Data slot refcount now held by the BusData */
    385     }
    386   else
    387     {
    388       dbus_connection_free_data_slot (&bus_data_slot);
    389     }
    390 
    391   return bd;
    392 }
    393 
    394 /**
    395  * Internal function that checks to see if this
    396  * is a shared connection owned by the bus and if it is unref it.
    397  *
    398  * @param connection a connection that has been disconnected.
    399  */
    400 void
    401 _dbus_bus_notify_shared_connection_disconnected_unlocked (DBusConnection *connection)
    402 {
    403   int i;
    404 
    405   _DBUS_LOCK (bus);
    406 
    407   /* We are expecting to have the connection saved in only one of these
    408    * slots, but someone could in a pathological case set system and session
    409    * bus to the same bus or something. Or set one of them to the starter
    410    * bus without setting the starter bus type in the env variable.
    411    * So we don't break the loop as soon as we find a match.
    412    */
    413   for (i = 0; i < N_BUS_TYPES; ++i)
    414     {
    415       if (bus_connections[i] == connection)
    416         {
    417           bus_connections[i] = NULL;
    418         }
    419     }
    420 
    421   _DBUS_UNLOCK (bus);
    422 }
    423 
    424 static DBusConnection *
    425 internal_bus_get (DBusBusType  type,
    426                   dbus_bool_t  private,
    427                   DBusError   *error)
    428 {
    429   const char *address;
    430   DBusConnection *connection;
    431   BusData *bd;
    432   DBusBusType address_type;
    433 
    434   _dbus_return_val_if_fail (type >= 0 && type < N_BUS_TYPES, NULL);
    435   _dbus_return_val_if_error_is_set (error, NULL);
    436 
    437   _DBUS_LOCK (bus);
    438 
    439   if (!init_connections_unlocked ())
    440     {
    441       _DBUS_UNLOCK (bus);
    442       _DBUS_SET_OOM (error);
    443       return NULL;
    444     }
    445 
    446   /* We want to use the activation address even if the
    447    * activating bus is the session or system bus,
    448    * per the spec.
    449    */
    450   address_type = type;
    451 
    452   /* Use the real type of the activation bus for getting its
    453    * connection, but only if the real type's address is available. (If
    454    * the activating bus isn't a well-known bus then
    455    * activation_bus_type == DBUS_BUS_STARTER)
    456    */
    457   if (type == DBUS_BUS_STARTER &&
    458       bus_connection_addresses[activation_bus_type] != NULL)
    459     type = activation_bus_type;
    460 
    461   if (!private && bus_connections[type] != NULL)
    462     {
    463       connection = bus_connections[type];
    464       dbus_connection_ref (connection);
    465 
    466       _DBUS_UNLOCK (bus);
    467       return connection;
    468     }
    469 
    470   address = bus_connection_addresses[address_type];
    471   if (address == NULL)
    472     {
    473       dbus_set_error (error, DBUS_ERROR_FAILED,
    474                       "Unable to determine the address of the message bus (try 'man dbus-launch' and 'man dbus-daemon' for help)");
    475       _DBUS_UNLOCK (bus);
    476       return NULL;
    477     }
    478 
    479   if (private)
    480     connection = dbus_connection_open_private (address, error);
    481   else
    482     connection = dbus_connection_open (address, error);
    483 
    484   if (!connection)
    485     {
    486       _DBUS_ASSERT_ERROR_IS_SET (error);
    487       _DBUS_UNLOCK (bus);
    488       return NULL;
    489     }
    490 
    491   if (!dbus_bus_register (connection, error))
    492     {
    493       _DBUS_ASSERT_ERROR_IS_SET (error);
    494       _dbus_connection_close_possibly_shared (connection);
    495       dbus_connection_unref (connection);
    496 
    497       _DBUS_UNLOCK (bus);
    498       return NULL;
    499     }
    500 
    501   if (!private)
    502     {
    503       /* store a weak ref to the connection (dbus-connection.c is
    504        * supposed to have a strong ref that it drops on disconnect,
    505        * since this is a shared connection)
    506        */
    507       bus_connections[type] = connection;
    508     }
    509 
    510   /* By default we're bound to the lifecycle of
    511    * the message bus.
    512    */
    513   dbus_connection_set_exit_on_disconnect (connection,
    514                                           TRUE);
    515 
    516   _DBUS_LOCK (bus_datas);
    517   bd = ensure_bus_data (connection);
    518   _dbus_assert (bd != NULL); /* it should have been created on
    519                                 register, so OOM not possible */
    520   bd->is_well_known = TRUE;
    521   _DBUS_UNLOCK (bus_datas);
    522 
    523 
    524   _DBUS_UNLOCK (bus);
    525 
    526   /* Return a reference to the caller */
    527   return connection;
    528 }
    529 
    530 
    531 /** @} */ /* end of implementation details docs */
    532 
    533 /**
    534  * @addtogroup DBusBus
    535  * @{
    536  */
    537 
    538 /**
    539  * Connects to a bus daemon and registers the client with it.  If a
    540  * connection to the bus already exists, then that connection is
    541  * returned.  The caller of this function owns a reference to the bus.
    542  *
    543  * The caller may NOT call dbus_connection_close() on this connection;
    544  * see dbus_connection_open() and dbus_connection_close() for details
    545  * on that.
    546  *
    547  * If this function obtains a new connection object never before
    548  * returned from dbus_bus_get(), it will call
    549  * dbus_connection_set_exit_on_disconnect(), so the application
    550  * will exit if the connection closes. You can undo this
    551  * by calling dbus_connection_set_exit_on_disconnect() yourself
    552  * after you get the connection.
    553  *
    554  * dbus_bus_get() calls dbus_bus_register() for you.
    555  *
    556  * If returning a newly-created connection, this function will block
    557  * until authentication and bus registration are complete.
    558  *
    559  * @param type bus type
    560  * @param error address where an error can be returned.
    561  * @returns a #DBusConnection with new ref
    562  */
    563 DBusConnection *
    564 dbus_bus_get (DBusBusType  type,
    565 	      DBusError   *error)
    566 {
    567   return internal_bus_get (type, FALSE, error);
    568 }
    569 
    570 /**
    571  * Connects to a bus daemon and registers the client with it as with
    572  * dbus_bus_register().  Unlike dbus_bus_get(), always creates a new
    573  * connection. This connection will not be saved or recycled by
    574  * libdbus. Caller owns a reference to the bus and must either close
    575  * it or know it to be closed prior to releasing this reference.
    576  *
    577  * See dbus_connection_open_private() for more details on when to
    578  * close and unref this connection.
    579  *
    580  * This function calls
    581  * dbus_connection_set_exit_on_disconnect() on the new connection, so the application
    582  * will exit if the connection closes. You can undo this
    583  * by calling dbus_connection_set_exit_on_disconnect() yourself
    584  * after you get the connection.
    585  *
    586  * dbus_bus_get_private() calls dbus_bus_register() for you.
    587  *
    588  * This function will block until authentication and bus registration
    589  * are complete.
    590  *
    591  * @param type bus type
    592  * @param error address where an error can be returned.
    593  * @returns a DBusConnection with new ref
    594  */
    595 DBusConnection *
    596 dbus_bus_get_private (DBusBusType  type,
    597                       DBusError   *error)
    598 {
    599   return internal_bus_get (type, TRUE, error);
    600 }
    601 
    602 /**
    603  * Registers a connection with the bus. This must be the first
    604  * thing an application does when connecting to the message bus.
    605  * If registration succeeds, the unique name will be set,
    606  * and can be obtained using dbus_bus_get_unique_name().
    607  *
    608  * This function will block until registration is complete.
    609  *
    610  * If the connection has already registered with the bus
    611  * (determined by checking whether dbus_bus_get_unique_name()
    612  * returns a non-#NULL value), then this function does nothing.
    613  *
    614  * If you use dbus_bus_get() or dbus_bus_get_private() this
    615  * function will be called for you.
    616  *
    617  * @note Just use dbus_bus_get() or dbus_bus_get_private() instead of
    618  * dbus_bus_register() and save yourself some pain. Using
    619  * dbus_bus_register() manually is only useful if you have your
    620  * own custom message bus not found in #DBusBusType.
    621  *
    622  * If you open a bus connection with dbus_connection_open() or
    623  * dbus_connection_open_private() you will have to dbus_bus_register()
    624  * yourself, or make the appropriate registration method calls
    625  * yourself. If you send the method calls yourself, call
    626  * dbus_bus_set_unique_name() with the unique bus name you get from
    627  * the bus.
    628  *
    629  * For shared connections (created with dbus_connection_open()) in a
    630  * multithreaded application, you can't really make the registration
    631  * calls yourself, because you don't know whether some other thread is
    632  * also registering, and the bus will kick you off if you send two
    633  * registration messages.
    634  *
    635  * If you use dbus_bus_register() however, there is a lock that
    636  * keeps both apps from registering at the same time.
    637  *
    638  * The rule in a multithreaded app, then, is that dbus_bus_register()
    639  * must be used to register, or you need to have your own locks that
    640  * all threads in the app will respect.
    641  *
    642  * In a single-threaded application you can register by hand instead
    643  * of using dbus_bus_register(), as long as you check
    644  * dbus_bus_get_unique_name() to see if a unique name has already been
    645  * stored by another thread before you send the registration messages.
    646  *
    647  * @param connection the connection
    648  * @param error place to store errors
    649  * @returns #TRUE on success
    650  */
    651 dbus_bool_t
    652 dbus_bus_register (DBusConnection *connection,
    653                    DBusError      *error)
    654 {
    655   DBusMessage *message, *reply;
    656   char *name;
    657   BusData *bd;
    658   dbus_bool_t retval;
    659 
    660   _dbus_return_val_if_fail (connection != NULL, FALSE);
    661   _dbus_return_val_if_error_is_set (error, FALSE);
    662 
    663   retval = FALSE;
    664 
    665   _DBUS_LOCK (bus_datas);
    666 
    667   bd = ensure_bus_data (connection);
    668   if (bd == NULL)
    669     {
    670       _DBUS_SET_OOM (error);
    671       _DBUS_UNLOCK (bus_datas);
    672       return FALSE;
    673     }
    674 
    675   if (bd->unique_name != NULL)
    676     {
    677       _dbus_verbose ("Ignoring attempt to register the same DBusConnection %s with the message bus a second time.\n",
    678                      bd->unique_name);
    679       _DBUS_UNLOCK (bus_datas);
    680 
    681       /* Success! */
    682       return TRUE;
    683     }
    684 
    685   message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
    686                                           DBUS_PATH_DBUS,
    687                                           DBUS_INTERFACE_DBUS,
    688                                           "Hello");
    689 
    690   if (!message)
    691     {
    692       _DBUS_SET_OOM (error);
    693 
    694       _DBUS_UNLOCK (bus_datas);
    695       return FALSE;
    696     }
    697 
    698   reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
    699 
    700   dbus_message_unref (message);
    701 
    702   if (reply == NULL)
    703     goto out;
    704   else if (dbus_set_error_from_message (error, reply))
    705     goto out;
    706   else if (!dbus_message_get_args (reply, error,
    707                                    DBUS_TYPE_STRING, &name,
    708                                    DBUS_TYPE_INVALID))
    709     goto out;
    710 
    711   bd->unique_name = _dbus_strdup (name);
    712   if (bd->unique_name == NULL)
    713     {
    714       _DBUS_SET_OOM (error);
    715       goto out;
    716     }
    717 
    718   retval = TRUE;
    719 
    720  out:
    721   if (reply)
    722     dbus_message_unref (reply);
    723 
    724   if (!retval)
    725     _DBUS_ASSERT_ERROR_IS_SET (error);
    726 
    727   _DBUS_UNLOCK (bus_datas);
    728 
    729   return retval;
    730 }
    731 
    732 
    733 /**
    734  * Sets the unique name of the connection, as assigned by the message
    735  * bus.  Can only be used if you registered with the bus manually
    736  * (i.e. if you did not call dbus_bus_register()). Can only be called
    737  * once per connection.  After the unique name is set, you can get it
    738  * with dbus_bus_get_unique_name().
    739  *
    740  * The only reason to use this function is to re-implement the
    741  * equivalent of dbus_bus_register() yourself. One (probably unusual)
    742  * reason to do that might be to do the bus registration call
    743  * asynchronously instead of synchronously.
    744  *
    745  * @note Just use dbus_bus_get() or dbus_bus_get_private(), or worst
    746  * case dbus_bus_register(), instead of messing with this
    747  * function. There's really no point creating pain for yourself by
    748  * doing things manually.
    749  *
    750  * It's hard to use this function safely on shared connections
    751  * (created by dbus_connection_open()) in a multithreaded application,
    752  * because only one registration attempt can be sent to the bus. If
    753  * two threads are both sending the registration message, there is no
    754  * mechanism in libdbus itself to avoid sending it twice.
    755  *
    756  * Thus, you need a way to coordinate which thread sends the
    757  * registration attempt; which also means you know which thread
    758  * will call dbus_bus_set_unique_name(). If you don't know
    759  * about all threads in the app (for example, if some libraries
    760  * you're using might start libdbus-using threads), then you
    761  * need to avoid using this function on shared connections.
    762  *
    763  * @param connection the connection
    764  * @param unique_name the unique name
    765  * @returns #FALSE if not enough memory
    766  */
    767 dbus_bool_t
    768 dbus_bus_set_unique_name (DBusConnection *connection,
    769                           const char     *unique_name)
    770 {
    771   BusData *bd;
    772   dbus_bool_t success;
    773 
    774   _dbus_return_val_if_fail (connection != NULL, FALSE);
    775   _dbus_return_val_if_fail (unique_name != NULL, FALSE);
    776 
    777   _DBUS_LOCK (bus_datas);
    778 
    779   bd = ensure_bus_data (connection);
    780   if (bd == NULL)
    781     return FALSE;
    782 
    783   _dbus_assert (bd->unique_name == NULL);
    784 
    785   bd->unique_name = _dbus_strdup (unique_name);
    786   success = bd->unique_name != NULL;
    787 
    788   _DBUS_UNLOCK (bus_datas);
    789 
    790   return success;
    791 }
    792 
    793 /**
    794  * Gets the unique name of the connection as assigned by the message
    795  * bus. Only possible after the connection has been registered with
    796  * the message bus. All connections returned by dbus_bus_get() or
    797  * dbus_bus_get_private() have been successfully registered.
    798  *
    799  * The name remains valid until the connection is freed, and
    800  * should not be freed by the caller.
    801  *
    802  * Other than dbus_bus_get(), there are two ways to set the unique
    803  * name; one is dbus_bus_register(), the other is
    804  * dbus_bus_set_unique_name().  You are responsible for calling
    805  * dbus_bus_set_unique_name() if you register by hand instead of using
    806  * dbus_bus_register().
    807  *
    808  * @param connection the connection
    809  * @returns the unique name or #NULL on error
    810  */
    811 const char*
    812 dbus_bus_get_unique_name (DBusConnection *connection)
    813 {
    814   BusData *bd;
    815   const char *unique_name;
    816 
    817   _dbus_return_val_if_fail (connection != NULL, NULL);
    818 
    819   _DBUS_LOCK (bus_datas);
    820 
    821   bd = ensure_bus_data (connection);
    822   if (bd == NULL)
    823     return NULL;
    824 
    825   unique_name = bd->unique_name;
    826 
    827   _DBUS_UNLOCK (bus_datas);
    828 
    829   return unique_name;
    830 }
    831 
    832 /**
    833  * Asks the bus to return the UID the named connection authenticated
    834  * as, if any.  Only works on UNIX; only works for connections on the
    835  * same machine as the bus. If you are not on the same machine as the
    836  * bus, then calling this is probably a bad idea, since the UID will
    837  * mean little to your application.
    838  *
    839  * For the system message bus you're guaranteed to be on the same
    840  * machine since it only listens on a UNIX domain socket (at least,
    841  * as shipped by default).
    842  *
    843  * This function only works for connections that authenticated as
    844  * a UNIX user, right now that includes all bus connections, but
    845  * it's very possible to have connections with no associated UID.
    846  * So check for errors and do something sensible if they happen.
    847  *
    848  * This function will always return an error on Windows.
    849  *
    850  * @param connection the connection
    851  * @param name a name owned by the connection
    852  * @param error location to store the error
    853  * @returns the unix user id, or ((unsigned)-1) if error is set
    854  */
    855 unsigned long
    856 dbus_bus_get_unix_user (DBusConnection *connection,
    857                         const char     *name,
    858                         DBusError      *error)
    859 {
    860   DBusMessage *message, *reply;
    861   dbus_uint32_t uid;
    862 
    863   _dbus_return_val_if_fail (connection != NULL, DBUS_UID_UNSET);
    864   _dbus_return_val_if_fail (name != NULL, DBUS_UID_UNSET);
    865   _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), DBUS_UID_UNSET);
    866   _dbus_return_val_if_error_is_set (error, DBUS_UID_UNSET);
    867 
    868   message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
    869                                           DBUS_PATH_DBUS,
    870                                           DBUS_INTERFACE_DBUS,
    871                                           "GetConnectionUnixUser");
    872 
    873   if (message == NULL)
    874     {
    875       _DBUS_SET_OOM (error);
    876       return DBUS_UID_UNSET;
    877     }
    878 
    879   if (!dbus_message_append_args (message,
    880 				 DBUS_TYPE_STRING, &name,
    881 				 DBUS_TYPE_INVALID))
    882     {
    883       dbus_message_unref (message);
    884       _DBUS_SET_OOM (error);
    885       return DBUS_UID_UNSET;
    886     }
    887 
    888   reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
    889                                                      error);
    890 
    891   dbus_message_unref (message);
    892 
    893   if (reply == NULL)
    894     {
    895       _DBUS_ASSERT_ERROR_IS_SET (error);
    896       return DBUS_UID_UNSET;
    897     }
    898 
    899   if (dbus_set_error_from_message (error, reply))
    900     {
    901       _DBUS_ASSERT_ERROR_IS_SET (error);
    902       dbus_message_unref (reply);
    903       return DBUS_UID_UNSET;
    904     }
    905 
    906   if (!dbus_message_get_args (reply, error,
    907                               DBUS_TYPE_UINT32, &uid,
    908                               DBUS_TYPE_INVALID))
    909     {
    910       _DBUS_ASSERT_ERROR_IS_SET (error);
    911       dbus_message_unref (reply);
    912       return DBUS_UID_UNSET;
    913     }
    914 
    915   dbus_message_unref (reply);
    916 
    917   return (unsigned long) uid;
    918 }
    919 
    920 /**
    921  * Asks the bus to return its globally unique ID, as described in the
    922  * D-Bus specification. For the session bus, this is useful as a way
    923  * to uniquely identify each user session. For the system bus,
    924  * probably the bus ID is not useful; instead, use the machine ID
    925  * since it's accessible without necessarily connecting to the bus and
    926  * may be persistent beyond a single bus instance (across reboots for
    927  * example). See dbus_get_local_machine_id().
    928  *
    929  * In addition to an ID for each bus and an ID for each machine, there is
    930  * an ID for each address that the bus is listening on; that can
    931  * be retrieved with dbus_connection_get_server_id(), though it is
    932  * probably not very useful.
    933  *
    934  * @param connection the connection
    935  * @param error location to store the error
    936  * @returns the bus ID or #NULL if error is set
    937  */
    938 char*
    939 dbus_bus_get_id (DBusConnection *connection,
    940                  DBusError      *error)
    941 {
    942   DBusMessage *message, *reply;
    943   char *id;
    944   const char *v_STRING;
    945 
    946   _dbus_return_val_if_fail (connection != NULL, NULL);
    947   _dbus_return_val_if_error_is_set (error, NULL);
    948 
    949   message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
    950                                           DBUS_PATH_DBUS,
    951                                           DBUS_INTERFACE_DBUS,
    952                                           "GetId");
    953 
    954   if (message == NULL)
    955     {
    956       _DBUS_SET_OOM (error);
    957       return NULL;
    958     }
    959 
    960   reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
    961                                                      error);
    962 
    963   dbus_message_unref (message);
    964 
    965   if (reply == NULL)
    966     {
    967       _DBUS_ASSERT_ERROR_IS_SET (error);
    968       return NULL;
    969     }
    970 
    971   if (dbus_set_error_from_message (error, reply))
    972     {
    973       _DBUS_ASSERT_ERROR_IS_SET (error);
    974       dbus_message_unref (reply);
    975       return NULL;
    976     }
    977 
    978   v_STRING = NULL;
    979   if (!dbus_message_get_args (reply, error,
    980                               DBUS_TYPE_STRING, &v_STRING,
    981                               DBUS_TYPE_INVALID))
    982     {
    983       _DBUS_ASSERT_ERROR_IS_SET (error);
    984       dbus_message_unref (reply);
    985       return NULL;
    986     }
    987 
    988   id = _dbus_strdup (v_STRING); /* may be NULL */
    989 
    990   dbus_message_unref (reply);
    991 
    992   if (id == NULL)
    993     _DBUS_SET_OOM (error);
    994 
    995   /* FIXME it might be nice to cache the ID locally */
    996 
    997   return id;
    998 }
    999 
   1000 /**
   1001  * Asks the bus to assign the given name to this connection by invoking
   1002  * the RequestName method on the bus. This method is fully documented
   1003  * in the D-Bus specification. For quick reference, the flags and
   1004  * result codes are discussed here, but the specification is the
   1005  * canonical version of this information.
   1006  *
   1007  * First you should know that for each bus name, the bus stores
   1008  * a queue of connections that would like to own it. Only
   1009  * one owns it at a time - called the primary owner. If the primary
   1010  * owner releases the name or disconnects, then the next owner in the
   1011  * queue atomically takes over.
   1012  *
   1013  * So for example if you have an application org.freedesktop.TextEditor
   1014  * and multiple instances of it can be run, you can have all of them
   1015  * sitting in the queue. The first one to start up will receive messages
   1016  * sent to org.freedesktop.TextEditor, but if that one exits another
   1017  * will become the primary owner and receive messages.
   1018  *
   1019  * The queue means you don't need to manually watch for the current owner to
   1020  * disappear and then request the name again.
   1021  *
   1022  * When requesting a name, you can specify several flags.
   1023  *
   1024  * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT and #DBUS_NAME_FLAG_DO_NOT_QUEUE
   1025  * are properties stored by the bus for this connection with respect to
   1026  * each requested bus name. These properties are stored even if the
   1027  * connection is queued and does not become the primary owner.
   1028  * You can update these flags by calling RequestName again (even if
   1029  * you already own the name).
   1030  *
   1031  * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT means that another requestor of the
   1032  * name can take it away from you by specifying #DBUS_NAME_FLAG_REPLACE_EXISTING.
   1033  *
   1034  * #DBUS_NAME_FLAG_DO_NOT_QUEUE means that if you aren't the primary owner,
   1035  * you don't want to be queued up - you only care about being the
   1036  * primary owner.
   1037  *
   1038  * Unlike the other two flags, #DBUS_NAME_FLAG_REPLACE_EXISTING is a property
   1039  * of the individual RequestName call, i.e. the bus does not persistently
   1040  * associate it with the connection-name pair. If a RequestName call includes
   1041  * the #DBUS_NAME_FLAG_REPLACE_EXISTING flag, and the current primary
   1042  * owner has #DBUS_NAME_FLAG_ALLOW_REPLACEMENT set, then the current primary
   1043  * owner will be kicked off.
   1044  *
   1045  * If no flags are given, an application will receive the requested
   1046  * name only if the name is currently unowned; and it will NOT give
   1047  * up the name if another application asks to take it over using
   1048  * #DBUS_NAME_FLAG_REPLACE_EXISTING.
   1049  *
   1050  * This function returns a result code. The possible result codes
   1051  * are as follows.
   1052  *
   1053  * #DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER means that the name had no
   1054  * existing owner, and the caller is now the primary owner; or that
   1055  * the name had an owner, and the caller specified
   1056  * #DBUS_NAME_FLAG_REPLACE_EXISTING, and the current owner
   1057  * specified #DBUS_NAME_FLAG_ALLOW_REPLACEMENT.
   1058  *
   1059  * #DBUS_REQUEST_NAME_REPLY_IN_QUEUE happens only if the caller does NOT
   1060  * specify #DBUS_NAME_FLAG_DO_NOT_QUEUE and either the current owner
   1061  * did NOT specify #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or the caller did NOT
   1062  * specify #DBUS_NAME_FLAG_REPLACE_EXISTING. In this case the caller ends up
   1063  * in a queue to own the name after the current owner gives it up.
   1064  *
   1065  * #DBUS_REQUEST_NAME_REPLY_EXISTS happens if the name has an owner
   1066  * already and the caller specifies #DBUS_NAME_FLAG_DO_NOT_QUEUE
   1067  * and either the current owner has NOT specified
   1068  * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or the caller did NOT specify
   1069  * #DBUS_NAME_FLAG_REPLACE_EXISTING.
   1070  *
   1071  * #DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER happens if an application
   1072  * requests a name it already owns. (Re-requesting a name is useful if
   1073  * you want to change the #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or
   1074  * #DBUS_NAME_FLAG_DO_NOT_QUEUE settings.)
   1075  *
   1076  * When a service represents an application, say "text editor," then
   1077  * it should specify #DBUS_NAME_FLAG_ALLOW_REPLACEMENT if it wants
   1078  * the last editor started to be the user's editor vs. the first one
   1079  * started.  Then any editor that can be the user's editor should
   1080  * specify #DBUS_NAME_FLAG_REPLACE_EXISTING to either take over
   1081  * (last-started-wins) or be queued up (first-started-wins) according
   1082  * to whether #DBUS_NAME_FLAG_ALLOW_REPLACEMENT was given.
   1083  *
   1084  * Conventionally, single-instance applications often offer a command
   1085  * line option called --replace which means to replace the current
   1086  * instance.  To implement this, always set
   1087  * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT when you request your
   1088  * application's bus name.  When you lose ownership of your bus name,
   1089  * you need to exit.  Look for the signal "NameLost" from
   1090  * #DBUS_SERVICE_DBUS and #DBUS_INTERFACE_DBUS (the signal's first
   1091  * argument is the bus name that was lost).  If starting up without
   1092  * --replace, do not specify #DBUS_NAME_FLAG_REPLACE_EXISTING, and
   1093  * exit if you fail to become the bus name owner. If --replace is
   1094  * given, ask to replace the old owner.
   1095  *
   1096  * @param connection the connection
   1097  * @param name the name to request
   1098  * @param flags flags
   1099  * @param error location to store the error
   1100  * @returns a result code, -1 if error is set
   1101  */
   1102 int
   1103 dbus_bus_request_name (DBusConnection *connection,
   1104                        const char     *name,
   1105                        unsigned int    flags,
   1106                        DBusError      *error)
   1107 {
   1108   DBusMessage *message, *reply;
   1109   dbus_uint32_t result;
   1110 
   1111   _dbus_return_val_if_fail (connection != NULL, 0);
   1112   _dbus_return_val_if_fail (name != NULL, 0);
   1113   _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
   1114   _dbus_return_val_if_error_is_set (error, 0);
   1115 
   1116   message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
   1117                                           DBUS_PATH_DBUS,
   1118                                           DBUS_INTERFACE_DBUS,
   1119                                           "RequestName");
   1120 
   1121   if (message == NULL)
   1122     {
   1123       _DBUS_SET_OOM (error);
   1124       return -1;
   1125     }
   1126 
   1127   if (!dbus_message_append_args (message,
   1128 				 DBUS_TYPE_STRING, &name,
   1129 				 DBUS_TYPE_UINT32, &flags,
   1130 				 DBUS_TYPE_INVALID))
   1131     {
   1132       dbus_message_unref (message);
   1133       _DBUS_SET_OOM (error);
   1134       return -1;
   1135     }
   1136 
   1137   reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
   1138                                                      error);
   1139 
   1140   dbus_message_unref (message);
   1141 
   1142   if (reply == NULL)
   1143     {
   1144       _DBUS_ASSERT_ERROR_IS_SET (error);
   1145       return -1;
   1146     }
   1147 
   1148   if (dbus_set_error_from_message (error, reply))
   1149     {
   1150       _DBUS_ASSERT_ERROR_IS_SET (error);
   1151       dbus_message_unref (reply);
   1152       return -1;
   1153     }
   1154 
   1155   if (!dbus_message_get_args (reply, error,
   1156                               DBUS_TYPE_UINT32, &result,
   1157                               DBUS_TYPE_INVALID))
   1158     {
   1159       _DBUS_ASSERT_ERROR_IS_SET (error);
   1160       dbus_message_unref (reply);
   1161       return -1;
   1162     }
   1163 
   1164   dbus_message_unref (reply);
   1165 
   1166   return result;
   1167 }
   1168 
   1169 
   1170 /**
   1171  * Asks the bus to unassign the given name from this connection by
   1172  * invoking the ReleaseName method on the bus. The "ReleaseName"
   1173  * method is canonically documented in the D-Bus specification.
   1174  *
   1175  * Possible results are: #DBUS_RELEASE_NAME_REPLY_RELEASED
   1176  * which means you owned the name or were in the queue to own it,
   1177  * and and now you don't own it and aren't in the queue.
   1178  * #DBUS_RELEASE_NAME_REPLY_NOT_OWNER which means someone else
   1179  * owns the name so you can't release it.
   1180  * #DBUS_RELEASE_NAME_REPLY_NON_EXISTENT
   1181  * which means nobody owned the name.
   1182  *
   1183  * @param connection the connection
   1184  * @param name the name to remove
   1185  * @param error location to store the error
   1186  * @returns a result code, -1 if error is set
   1187  */
   1188 int
   1189 dbus_bus_release_name (DBusConnection *connection,
   1190                        const char     *name,
   1191                        DBusError      *error)
   1192 {
   1193   DBusMessage *message, *reply;
   1194   dbus_uint32_t result;
   1195 
   1196   _dbus_return_val_if_fail (connection != NULL, 0);
   1197   _dbus_return_val_if_fail (name != NULL, 0);
   1198   _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
   1199   _dbus_return_val_if_error_is_set (error, 0);
   1200 
   1201   message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
   1202                                           DBUS_PATH_DBUS,
   1203                                           DBUS_INTERFACE_DBUS,
   1204                                           "ReleaseName");
   1205 
   1206   if (message == NULL)
   1207     {
   1208       _DBUS_SET_OOM (error);
   1209       return -1;
   1210     }
   1211 
   1212   if (!dbus_message_append_args (message,
   1213                                  DBUS_TYPE_STRING, &name,
   1214                                  DBUS_TYPE_INVALID))
   1215     {
   1216       dbus_message_unref (message);
   1217       _DBUS_SET_OOM (error);
   1218       return -1;
   1219     }
   1220 
   1221   reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
   1222                                                      error);
   1223 
   1224   dbus_message_unref (message);
   1225 
   1226   if (reply == NULL)
   1227     {
   1228       _DBUS_ASSERT_ERROR_IS_SET (error);
   1229       return -1;
   1230     }
   1231 
   1232   if (dbus_set_error_from_message (error, reply))
   1233     {
   1234       _DBUS_ASSERT_ERROR_IS_SET (error);
   1235       dbus_message_unref (reply);
   1236       return -1;
   1237     }
   1238 
   1239   if (!dbus_message_get_args (reply, error,
   1240                               DBUS_TYPE_UINT32, &result,
   1241                               DBUS_TYPE_INVALID))
   1242     {
   1243       _DBUS_ASSERT_ERROR_IS_SET (error);
   1244       dbus_message_unref (reply);
   1245       return -1;
   1246     }
   1247 
   1248   dbus_message_unref (reply);
   1249 
   1250   return result;
   1251 }
   1252 
   1253 /**
   1254  * Asks the bus whether a certain name has an owner.
   1255  *
   1256  * Using this can easily result in a race condition,
   1257  * since an owner can appear or disappear after you
   1258  * call this.
   1259  *
   1260  * If you want to request a name, just request it;
   1261  * if you want to avoid replacing a current owner,
   1262  * don't specify #DBUS_NAME_FLAG_REPLACE_EXISTING and
   1263  * you will get an error if there's already an owner.
   1264  *
   1265  * @param connection the connection
   1266  * @param name the name
   1267  * @param error location to store any errors
   1268  * @returns #TRUE if the name exists, #FALSE if not or on error
   1269  */
   1270 dbus_bool_t
   1271 dbus_bus_name_has_owner (DBusConnection *connection,
   1272 			 const char     *name,
   1273                          DBusError      *error)
   1274 {
   1275   DBusMessage *message, *reply;
   1276   dbus_bool_t exists;
   1277 
   1278   _dbus_return_val_if_fail (connection != NULL, FALSE);
   1279   _dbus_return_val_if_fail (name != NULL, FALSE);
   1280   _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
   1281   _dbus_return_val_if_error_is_set (error, FALSE);
   1282 
   1283   message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
   1284                                           DBUS_PATH_DBUS,
   1285                                           DBUS_INTERFACE_DBUS,
   1286                                           "NameHasOwner");
   1287   if (message == NULL)
   1288     {
   1289       _DBUS_SET_OOM (error);
   1290       return FALSE;
   1291     }
   1292 
   1293   if (!dbus_message_append_args (message,
   1294 				 DBUS_TYPE_STRING, &name,
   1295 				 DBUS_TYPE_INVALID))
   1296     {
   1297       dbus_message_unref (message);
   1298       _DBUS_SET_OOM (error);
   1299       return FALSE;
   1300     }
   1301 
   1302   reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
   1303   dbus_message_unref (message);
   1304 
   1305   if (reply == NULL)
   1306     {
   1307       _DBUS_ASSERT_ERROR_IS_SET (error);
   1308       return FALSE;
   1309     }
   1310 
   1311   if (!dbus_message_get_args (reply, error,
   1312                               DBUS_TYPE_BOOLEAN, &exists,
   1313                               DBUS_TYPE_INVALID))
   1314     {
   1315       _DBUS_ASSERT_ERROR_IS_SET (error);
   1316       dbus_message_unref (reply);
   1317       return FALSE;
   1318     }
   1319 
   1320   dbus_message_unref (reply);
   1321   return exists;
   1322 }
   1323 
   1324 /**
   1325  * Starts a service that will request ownership of the given name.
   1326  * The returned result will be one of be one of
   1327  * #DBUS_START_REPLY_SUCCESS or #DBUS_START_REPLY_ALREADY_RUNNING if
   1328  * successful.  Pass #NULL if you don't care about the result.
   1329  *
   1330  * The flags parameter is for future expansion, currently you should
   1331  * specify 0.
   1332  *
   1333  * It's often easier to avoid explicitly starting services, and
   1334  * just send a method call to the service's bus name instead.
   1335  * Method calls start a service to handle them by default
   1336  * unless you call dbus_message_set_auto_start() to disable this
   1337  * behavior.
   1338  *
   1339  * @param connection the connection
   1340  * @param name the name we want the new service to request
   1341  * @param flags the flags (should always be 0 for now)
   1342  * @param result a place to store the result or #NULL
   1343  * @param error location to store any errors
   1344  * @returns #TRUE if the activation succeeded, #FALSE if not
   1345  */
   1346 dbus_bool_t
   1347 dbus_bus_start_service_by_name (DBusConnection *connection,
   1348                                 const char     *name,
   1349                                 dbus_uint32_t   flags,
   1350                                 dbus_uint32_t  *result,
   1351                                 DBusError      *error)
   1352 {
   1353   DBusMessage *msg;
   1354   DBusMessage *reply;
   1355 
   1356   _dbus_return_val_if_fail (connection != NULL, FALSE);
   1357   _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
   1358 
   1359   msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
   1360                                       DBUS_PATH_DBUS,
   1361                                       DBUS_INTERFACE_DBUS,
   1362                                       "StartServiceByName");
   1363 
   1364   if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &name,
   1365 			  	 DBUS_TYPE_UINT32, &flags, DBUS_TYPE_INVALID))
   1366     {
   1367       dbus_message_unref (msg);
   1368       _DBUS_SET_OOM (error);
   1369       return FALSE;
   1370     }
   1371 
   1372   reply = dbus_connection_send_with_reply_and_block (connection, msg,
   1373                                                      -1, error);
   1374   dbus_message_unref (msg);
   1375 
   1376   if (reply == NULL)
   1377     {
   1378       _DBUS_ASSERT_ERROR_IS_SET (error);
   1379       return FALSE;
   1380     }
   1381 
   1382   if (dbus_set_error_from_message (error, reply))
   1383     {
   1384       _DBUS_ASSERT_ERROR_IS_SET (error);
   1385       dbus_message_unref (reply);
   1386       return FALSE;
   1387     }
   1388 
   1389   if (result != NULL &&
   1390       !dbus_message_get_args (reply, error, DBUS_TYPE_UINT32,
   1391 	      		      result, DBUS_TYPE_INVALID))
   1392     {
   1393       _DBUS_ASSERT_ERROR_IS_SET (error);
   1394       dbus_message_unref (reply);
   1395       return FALSE;
   1396     }
   1397 
   1398   dbus_message_unref (reply);
   1399   return TRUE;
   1400 }
   1401 
   1402 static void
   1403 send_no_return_values (DBusConnection *connection,
   1404                        DBusMessage    *msg,
   1405                        DBusError      *error)
   1406 {
   1407   if (error)
   1408     {
   1409       /* Block to check success codepath */
   1410       DBusMessage *reply;
   1411 
   1412       reply = dbus_connection_send_with_reply_and_block (connection, msg,
   1413                                                          -1, error);
   1414 
   1415       if (reply == NULL)
   1416         _DBUS_ASSERT_ERROR_IS_SET (error);
   1417       else
   1418         dbus_message_unref (reply);
   1419     }
   1420   else
   1421     {
   1422       /* Silently-fail nonblocking codepath */
   1423       dbus_message_set_no_reply (msg, TRUE);
   1424       dbus_connection_send (connection, msg, NULL);
   1425     }
   1426 }
   1427 
   1428 /**
   1429  * Adds a match rule to match messages going through the message bus.
   1430  * The "rule" argument is the string form of a match rule.
   1431  *
   1432  * If you pass #NULL for the error, this function will not
   1433  * block; the match thus won't be added until you flush the
   1434  * connection, and if there's an error adding the match
   1435  * (only possible error is lack of resources in the bus),
   1436  * you won't find out about it.
   1437  *
   1438  * If you pass non-#NULL for the error this function will
   1439  * block until it gets a reply.
   1440  *
   1441  * Normal API conventions would have the function return
   1442  * a boolean value indicating whether the error was set,
   1443  * but that would require blocking always to determine
   1444  * the return value.
   1445  *
   1446  * The AddMatch method is fully documented in the D-Bus
   1447  * specification. For quick reference, the format of the
   1448  * match rules is discussed here, but the specification
   1449  * is the canonical version of this information.
   1450  *
   1451  * Rules are specified as a string of comma separated
   1452  * key/value pairs. An example is
   1453  * "type='signal',sender='org.freedesktop.DBus',
   1454  * interface='org.freedesktop.DBus',member='Foo',
   1455  * path='/bar/foo',destination=':452345.34'"
   1456  *
   1457  * Possible keys you can match on are type, sender,
   1458  * interface, member, path, destination and numbered
   1459  * keys to match message args (keys are 'arg0', 'arg1', etc.).
   1460  * Omitting a key from the rule indicates
   1461  * a wildcard match.  For instance omitting
   1462  * the member from a match rule but adding a sender would
   1463  * let all messages from that sender through regardless of
   1464  * the member.
   1465  *
   1466  * Matches are inclusive not exclusive so as long as one
   1467  * rule matches the message will get through.  It is important
   1468  * to note this because every time a message is received the
   1469  * application will be paged into memory to process it.  This
   1470  * can cause performance problems such as draining batteries
   1471  * on embedded platforms.
   1472  *
   1473  * If you match message args ('arg0', 'arg1', and so forth)
   1474  * only string arguments will match. That is, arg0='5' means
   1475  * match the string "5" not the integer 5.
   1476  *
   1477  * Currently there is no way to match against non-string arguments.
   1478  *
   1479  * A specialised form of wildcard matching on arguments is
   1480  * supported for path-like namespaces.  If your argument match has
   1481  * a 'path' suffix (eg: "arg0path='/some/path/'") then it is
   1482  * considered a match if the argument exactly matches the given
   1483  * string or if one of them ends in a '/' and is a prefix of the
   1484  * other.
   1485  *
   1486  * Matching on interface is tricky because method call
   1487  * messages only optionally specify the interface.
   1488  * If a message omits the interface, then it will NOT match
   1489  * if the rule specifies an interface name. This means match
   1490  * rules on method calls should not usually give an interface.
   1491  *
   1492  * However, signal messages are required to include the interface
   1493  * so when matching signals usually you should specify the interface
   1494  * in the match rule.
   1495  *
   1496  * For security reasons, you can match arguments only up to
   1497  * #DBUS_MAXIMUM_MATCH_RULE_ARG_NUMBER.
   1498  *
   1499  * Match rules have a maximum length of #DBUS_MAXIMUM_MATCH_RULE_LENGTH
   1500  * bytes.
   1501  *
   1502  * Both of these maximums are much higher than you're likely to need,
   1503  * they only exist because the D-Bus bus daemon has fixed limits on
   1504  * all resource usage.
   1505  *
   1506  * @param connection connection to the message bus
   1507  * @param rule textual form of match rule
   1508  * @param error location to store any errors
   1509  */
   1510 void
   1511 dbus_bus_add_match (DBusConnection *connection,
   1512                     const char     *rule,
   1513                     DBusError      *error)
   1514 {
   1515   DBusMessage *msg;
   1516 
   1517   _dbus_return_if_fail (rule != NULL);
   1518 
   1519   msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
   1520                                       DBUS_PATH_DBUS,
   1521                                       DBUS_INTERFACE_DBUS,
   1522                                       "AddMatch");
   1523 
   1524   if (msg == NULL)
   1525     {
   1526       _DBUS_SET_OOM (error);
   1527       return;
   1528     }
   1529 
   1530   if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
   1531                                  DBUS_TYPE_INVALID))
   1532     {
   1533       dbus_message_unref (msg);
   1534       _DBUS_SET_OOM (error);
   1535       return;
   1536     }
   1537 
   1538   send_no_return_values (connection, msg, error);
   1539 
   1540   dbus_message_unref (msg);
   1541 }
   1542 
   1543 /**
   1544  * Removes a previously-added match rule "by value" (the most
   1545  * recently-added identical rule gets removed).  The "rule" argument
   1546  * is the string form of a match rule.
   1547  *
   1548  * The bus compares match rules semantically, not textually, so
   1549  * whitespace and ordering don't have to be identical to
   1550  * the rule you passed to dbus_bus_add_match().
   1551  *
   1552  * If you pass #NULL for the error, this function will not
   1553  * block; otherwise it will. See detailed explanation in
   1554  * docs for dbus_bus_add_match().
   1555  *
   1556  * @param connection connection to the message bus
   1557  * @param rule textual form of match rule
   1558  * @param error location to store any errors
   1559  */
   1560 void
   1561 dbus_bus_remove_match (DBusConnection *connection,
   1562                        const char     *rule,
   1563                        DBusError      *error)
   1564 {
   1565   DBusMessage *msg;
   1566 
   1567   _dbus_return_if_fail (rule != NULL);
   1568 
   1569   msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
   1570                                       DBUS_PATH_DBUS,
   1571                                       DBUS_INTERFACE_DBUS,
   1572                                       "RemoveMatch");
   1573 
   1574   if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
   1575                                  DBUS_TYPE_INVALID))
   1576     {
   1577       dbus_message_unref (msg);
   1578       _DBUS_SET_OOM (error);
   1579       return;
   1580     }
   1581 
   1582   send_no_return_values (connection, msg, error);
   1583 
   1584   dbus_message_unref (msg);
   1585 }
   1586 
   1587 /** @} */
   1588