D-Bus  1.4.10
dbus-bus.c
00001 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
00002 /* dbus-bus.c  Convenience functions for communicating with the bus.
00003  *
00004  * Copyright (C) 2003  CodeFactory AB
00005  * Copyright (C) 2003  Red Hat, Inc.
00006  *
00007  * Licensed under the Academic Free License version 2.1
00008  * 
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  * 
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00022  *
00023  */
00024 
00025 #include <config.h>
00026 #include "dbus-bus.h"
00027 #include "dbus-protocol.h"
00028 #include "dbus-internals.h"
00029 #include "dbus-message.h"
00030 #include "dbus-marshal-validate.h"
00031 #include "dbus-threads-internal.h"
00032 #include "dbus-connection-internal.h"
00033 #include "dbus-string.h"
00034 
00076 typedef struct
00077 {
00078   DBusConnection *connection; 
00079   char *unique_name; 
00081   unsigned int is_well_known : 1; 
00082 } BusData;
00083 
00086 static dbus_int32_t bus_data_slot = -1;
00087 
00089 #define N_BUS_TYPES 3
00090 
00091 static DBusConnection *bus_connections[N_BUS_TYPES];
00092 static char *bus_connection_addresses[N_BUS_TYPES] = { NULL, NULL, NULL };
00093 
00094 static DBusBusType activation_bus_type = DBUS_BUS_STARTER;
00095 
00096 static dbus_bool_t initialized = FALSE;
00097 
00101 _DBUS_DEFINE_GLOBAL_LOCK (bus);
00102 
00109 _DBUS_DEFINE_GLOBAL_LOCK (bus_datas);
00110 
00111 static void
00112 addresses_shutdown_func (void *data)
00113 {
00114   int i;
00115 
00116   i = 0;
00117   while (i < N_BUS_TYPES)
00118     {
00119       if (bus_connections[i] != NULL)
00120         _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");
00121       
00122       dbus_free (bus_connection_addresses[i]);
00123       bus_connection_addresses[i] = NULL;
00124       ++i;
00125     }
00126 
00127   activation_bus_type = DBUS_BUS_STARTER;
00128 
00129   initialized = FALSE;
00130 }
00131 
00132 static dbus_bool_t
00133 get_from_env (char           **connection_p,
00134               const char      *env_var)
00135 {
00136   const char *s;
00137   
00138   _dbus_assert (*connection_p == NULL);
00139   
00140   s = _dbus_getenv (env_var);
00141   if (s == NULL || *s == '\0')
00142     return TRUE; /* successfully didn't use the env var */
00143   else
00144     {
00145       *connection_p = _dbus_strdup (s);
00146       return *connection_p != NULL;
00147     }
00148 }
00149 
00150 static dbus_bool_t
00151 init_session_address (void)
00152 {
00153   dbus_bool_t retval;
00154  
00155   retval = FALSE;
00156 
00157   /* First, look in the environment.  This is the normal case on 
00158    * freedesktop.org/Unix systems. */
00159   get_from_env (&bus_connection_addresses[DBUS_BUS_SESSION],
00160                      "DBUS_SESSION_BUS_ADDRESS");
00161   if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
00162     {
00163       dbus_bool_t supported;
00164       DBusString addr;
00165       DBusError error = DBUS_ERROR_INIT;
00166 
00167       if (!_dbus_string_init (&addr))
00168         return FALSE;
00169 
00170       supported = FALSE;
00171       /* So it's not in the environment - let's try a platform-specific method.
00172        * On MacOS, this involves asking launchd.  On Windows (not specified yet)
00173        * we might do a COM lookup.
00174        * Ignore errors - if we failed, fall back to autolaunch. */
00175       retval = _dbus_lookup_session_address (&supported, &addr, &error);
00176       if (supported && retval)
00177         {
00178           retval =_dbus_string_steal_data (&addr, &bus_connection_addresses[DBUS_BUS_SESSION]);
00179         }
00180       else if (supported && !retval)
00181         {
00182           if (dbus_error_is_set(&error))
00183             _dbus_warn ("Dynamic session lookup supported but failed: %s\n", error.message);
00184           else
00185             _dbus_warn ("Dynamic session lookup supported but failed silently\n");
00186         }
00187       _dbus_string_free (&addr);
00188     }
00189   else
00190     retval = TRUE;
00191 
00192   if (!retval)
00193     return FALSE;
00194 
00195   /* The DBUS_SESSION_BUS_DEFAULT_ADDRESS should have really been named
00196    * DBUS_SESSION_BUS_FALLBACK_ADDRESS. 
00197    */
00198   if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
00199     bus_connection_addresses[DBUS_BUS_SESSION] =
00200       _dbus_strdup (DBUS_SESSION_BUS_DEFAULT_ADDRESS);
00201   if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
00202     return FALSE;
00203 
00204   return TRUE;
00205 }
00206 
00207 static dbus_bool_t
00208 init_connections_unlocked (void)
00209 {
00210   if (!initialized)
00211     {
00212       const char *s;
00213       int i;
00214 
00215       i = 0;
00216       while (i < N_BUS_TYPES)
00217         {
00218           bus_connections[i] = NULL;
00219           ++i;
00220         }
00221 
00222       /* Don't init these twice, we may run this code twice if
00223        * init_connections_unlocked() fails midway through.
00224        * In practice, each block below should contain only one
00225        * "return FALSE" or running through twice may not
00226        * work right.
00227        */
00228       
00229        if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
00230          {
00231            _dbus_verbose ("Filling in system bus address...\n");
00232            
00233            if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SYSTEM],
00234                               "DBUS_SYSTEM_BUS_ADDRESS"))
00235              return FALSE;
00236          }
00237 
00238                   
00239        if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
00240          {
00241            /* Use default system bus address if none set in environment */
00242            bus_connection_addresses[DBUS_BUS_SYSTEM] =
00243              _dbus_strdup (DBUS_SYSTEM_BUS_DEFAULT_ADDRESS);
00244 
00245            if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
00246              return FALSE;
00247            
00248            _dbus_verbose ("  used default system bus \"%s\"\n",
00249                           bus_connection_addresses[DBUS_BUS_SYSTEM]);
00250          }
00251        else
00252          _dbus_verbose ("  used env var system bus \"%s\"\n",
00253                         bus_connection_addresses[DBUS_BUS_SYSTEM]);
00254           
00255       if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
00256         {
00257           _dbus_verbose ("Filling in session bus address...\n");
00258           
00259           if (!init_session_address ())
00260             return FALSE;
00261 
00262           _dbus_verbose ("  \"%s\"\n", bus_connection_addresses[DBUS_BUS_SESSION] ?
00263                          bus_connection_addresses[DBUS_BUS_SESSION] : "none set");
00264         }
00265 
00266       if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
00267         {
00268           _dbus_verbose ("Filling in activation bus address...\n");
00269           
00270           if (!get_from_env (&bus_connection_addresses[DBUS_BUS_STARTER],
00271                              "DBUS_STARTER_ADDRESS"))
00272             return FALSE;
00273           
00274           _dbus_verbose ("  \"%s\"\n", bus_connection_addresses[DBUS_BUS_STARTER] ?
00275                          bus_connection_addresses[DBUS_BUS_STARTER] : "none set");
00276         }
00277 
00278 
00279       if (bus_connection_addresses[DBUS_BUS_STARTER] != NULL)
00280         {
00281           s = _dbus_getenv ("DBUS_STARTER_BUS_TYPE");
00282               
00283           if (s != NULL)
00284             {
00285               _dbus_verbose ("Bus activation type was set to \"%s\"\n", s);
00286                   
00287               if (strcmp (s, "system") == 0)
00288                 activation_bus_type = DBUS_BUS_SYSTEM;
00289               else if (strcmp (s, "session") == 0)
00290                 activation_bus_type = DBUS_BUS_SESSION;
00291             }
00292         }
00293       else
00294         {
00295           /* Default to the session bus instead if available */
00296           if (bus_connection_addresses[DBUS_BUS_SESSION] != NULL)
00297             {
00298               bus_connection_addresses[DBUS_BUS_STARTER] =
00299                 _dbus_strdup (bus_connection_addresses[DBUS_BUS_SESSION]);
00300               if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
00301                 return FALSE;
00302             }
00303         }
00304       
00305       /* If we return FALSE we have to be sure that restarting
00306        * the above code will work right
00307        */
00308       
00309       if (!_dbus_setenv ("DBUS_ACTIVATION_ADDRESS", NULL))
00310         return FALSE;
00311 
00312       if (!_dbus_setenv ("DBUS_ACTIVATION_BUS_TYPE", NULL))
00313         return FALSE;
00314       
00315       if (!_dbus_register_shutdown_func (addresses_shutdown_func,
00316                                          NULL))
00317         return FALSE;
00318       
00319       initialized = TRUE;
00320     }
00321 
00322   return initialized;
00323 }
00324 
00325 static void
00326 bus_data_free (void *data)
00327 {
00328   BusData *bd = data;
00329   
00330   if (bd->is_well_known)
00331     {
00332       int i;
00333       _DBUS_LOCK (bus);
00334       /* We may be stored in more than one slot */
00335       /* This should now be impossible - these slots are supposed to
00336        * be cleared on disconnect, so should not need to be cleared on
00337        * finalize
00338        */
00339       i = 0;
00340       while (i < N_BUS_TYPES)
00341         {
00342           if (bus_connections[i] == bd->connection)
00343             bus_connections[i] = NULL;
00344           
00345           ++i;
00346         }
00347       _DBUS_UNLOCK (bus);
00348     }
00349   
00350   dbus_free (bd->unique_name);
00351   dbus_free (bd);
00352 
00353   dbus_connection_free_data_slot (&bus_data_slot);
00354 }
00355 
00356 static BusData*
00357 ensure_bus_data (DBusConnection *connection)
00358 {
00359   BusData *bd;
00360 
00361   if (!dbus_connection_allocate_data_slot (&bus_data_slot))
00362     return NULL;
00363 
00364   bd = dbus_connection_get_data (connection, bus_data_slot);
00365   if (bd == NULL)
00366     {      
00367       bd = dbus_new0 (BusData, 1);
00368       if (bd == NULL)
00369         {
00370           dbus_connection_free_data_slot (&bus_data_slot);
00371           return NULL;
00372         }
00373 
00374       bd->connection = connection;
00375       
00376       if (!dbus_connection_set_data (connection, bus_data_slot, bd,
00377                                      bus_data_free))
00378         {
00379           dbus_free (bd);
00380           dbus_connection_free_data_slot (&bus_data_slot);
00381           return NULL;
00382         }
00383 
00384       /* Data slot refcount now held by the BusData */
00385     }
00386   else
00387     {
00388       dbus_connection_free_data_slot (&bus_data_slot);
00389     }
00390 
00391   return bd;
00392 }
00393 
00400 void
00401 _dbus_bus_notify_shared_connection_disconnected_unlocked (DBusConnection *connection)
00402 {
00403   int i;
00404   
00405   _DBUS_LOCK (bus);
00406 
00407   /* We are expecting to have the connection saved in only one of these
00408    * slots, but someone could in a pathological case set system and session
00409    * bus to the same bus or something. Or set one of them to the starter
00410    * bus without setting the starter bus type in the env variable.
00411    * So we don't break the loop as soon as we find a match.
00412    */
00413   for (i = 0; i < N_BUS_TYPES; ++i)
00414     {
00415       if (bus_connections[i] == connection)
00416         {
00417           bus_connections[i] = NULL;
00418         }
00419     }
00420 
00421   _DBUS_UNLOCK (bus);
00422 }
00423 
00424 static DBusConnection *
00425 internal_bus_get (DBusBusType  type,
00426                   dbus_bool_t  private,
00427                   DBusError   *error)
00428 {
00429   const char *address;
00430   DBusConnection *connection;
00431   BusData *bd;
00432   DBusBusType address_type;
00433 
00434   _dbus_return_val_if_fail (type >= 0 && type < N_BUS_TYPES, NULL);
00435   _dbus_return_val_if_error_is_set (error, NULL);
00436 
00437   _DBUS_LOCK (bus);
00438 
00439   if (!init_connections_unlocked ())
00440     {
00441       _DBUS_UNLOCK (bus);
00442       _DBUS_SET_OOM (error);
00443       return NULL;
00444     }
00445 
00446   /* We want to use the activation address even if the
00447    * activating bus is the session or system bus,
00448    * per the spec.
00449    */
00450   address_type = type;
00451   
00452   /* Use the real type of the activation bus for getting its
00453    * connection, but only if the real type's address is available. (If
00454    * the activating bus isn't a well-known bus then
00455    * activation_bus_type == DBUS_BUS_STARTER)
00456    */
00457   if (type == DBUS_BUS_STARTER &&
00458       bus_connection_addresses[activation_bus_type] != NULL)
00459     type = activation_bus_type;
00460   
00461   if (!private && bus_connections[type] != NULL)
00462     {
00463       connection = bus_connections[type];
00464       dbus_connection_ref (connection);
00465       
00466       _DBUS_UNLOCK (bus);
00467       return connection;
00468     }
00469 
00470   address = bus_connection_addresses[address_type];
00471   if (address == NULL)
00472     {
00473       dbus_set_error (error, DBUS_ERROR_FAILED,
00474                       "Unable to determine the address of the message bus (try 'man dbus-launch' and 'man dbus-daemon' for help)");
00475       _DBUS_UNLOCK (bus);
00476       return NULL;
00477     }
00478 
00479   if (private)
00480     connection = dbus_connection_open_private (address, error);
00481   else
00482     connection = dbus_connection_open (address, error);
00483   
00484   if (!connection)
00485     {
00486       _DBUS_ASSERT_ERROR_IS_SET (error);
00487       _DBUS_UNLOCK (bus);
00488       return NULL;
00489     }
00490 
00491   if (!dbus_bus_register (connection, error))
00492     {
00493       _DBUS_ASSERT_ERROR_IS_SET (error);
00494       _dbus_connection_close_possibly_shared (connection);
00495       dbus_connection_unref (connection);
00496 
00497       _DBUS_UNLOCK (bus);
00498       return NULL;
00499     }
00500 
00501   if (!private)
00502     {
00503       /* store a weak ref to the connection (dbus-connection.c is
00504        * supposed to have a strong ref that it drops on disconnect,
00505        * since this is a shared connection)
00506        */
00507       bus_connections[type] = connection;
00508     }
00509 
00510   /* By default we're bound to the lifecycle of
00511    * the message bus.
00512    */
00513   dbus_connection_set_exit_on_disconnect (connection,
00514                                           TRUE);
00515  
00516   _DBUS_LOCK (bus_datas);
00517   bd = ensure_bus_data (connection);
00518   _dbus_assert (bd != NULL); /* it should have been created on
00519                                 register, so OOM not possible */
00520   bd->is_well_known = TRUE;
00521   _DBUS_UNLOCK (bus_datas);
00522 
00523   
00524   _DBUS_UNLOCK (bus);
00525 
00526   /* Return a reference to the caller */
00527   return connection;
00528 }
00529 
00530  /* end of implementation details docs */
00532 
00563 DBusConnection *
00564 dbus_bus_get (DBusBusType  type,
00565               DBusError   *error)
00566 {
00567   return internal_bus_get (type, FALSE, error);
00568 }
00569 
00595 DBusConnection *
00596 dbus_bus_get_private (DBusBusType  type,
00597                       DBusError   *error)
00598 {
00599   return internal_bus_get (type, TRUE, error);
00600 }
00601 
00651 dbus_bool_t
00652 dbus_bus_register (DBusConnection *connection,
00653                    DBusError      *error)
00654 {
00655   DBusMessage *message, *reply;
00656   char *name;
00657   BusData *bd;
00658   dbus_bool_t retval;
00659 
00660   _dbus_return_val_if_fail (connection != NULL, FALSE);
00661   _dbus_return_val_if_error_is_set (error, FALSE);
00662 
00663   retval = FALSE;
00664 
00665   _DBUS_LOCK (bus_datas);
00666 
00667   bd = ensure_bus_data (connection);
00668   if (bd == NULL)
00669     {
00670       _DBUS_SET_OOM (error);
00671       _DBUS_UNLOCK (bus_datas);
00672       return FALSE;
00673     }
00674 
00675   if (bd->unique_name != NULL)
00676     {
00677       _dbus_verbose ("Ignoring attempt to register the same DBusConnection %s with the message bus a second time.\n",
00678                      bd->unique_name);
00679       _DBUS_UNLOCK (bus_datas);
00680 
00681       /* Success! */
00682       return TRUE;
00683     }
00684   
00685   message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
00686                                           DBUS_PATH_DBUS,
00687                                           DBUS_INTERFACE_DBUS,
00688                                           "Hello"); 
00689 
00690   if (!message)
00691     {
00692       _DBUS_SET_OOM (error);
00693 
00694       _DBUS_UNLOCK (bus_datas);
00695       return FALSE;
00696     }
00697   
00698   reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
00699 
00700   dbus_message_unref (message);
00701   
00702   if (reply == NULL)
00703     goto out;
00704   else if (dbus_set_error_from_message (error, reply))
00705     goto out;
00706   else if (!dbus_message_get_args (reply, error,
00707                                    DBUS_TYPE_STRING, &name,
00708                                    DBUS_TYPE_INVALID))
00709     goto out;
00710   
00711   bd->unique_name = _dbus_strdup (name);
00712   if (bd->unique_name == NULL)
00713     {
00714       _DBUS_SET_OOM (error);
00715       goto out;
00716     }
00717   
00718   retval = TRUE;
00719   
00720  out:
00721   if (reply)
00722     dbus_message_unref (reply);
00723 
00724   if (!retval)
00725     _DBUS_ASSERT_ERROR_IS_SET (error);
00726 
00727   _DBUS_UNLOCK (bus_datas);
00728   
00729   return retval;
00730 }
00731 
00732 
00767 dbus_bool_t
00768 dbus_bus_set_unique_name (DBusConnection *connection,
00769                           const char     *unique_name)
00770 {
00771   BusData *bd;
00772   dbus_bool_t success = FALSE;
00773 
00774   _dbus_return_val_if_fail (connection != NULL, FALSE);
00775   _dbus_return_val_if_fail (unique_name != NULL, FALSE);
00776 
00777   _DBUS_LOCK (bus_datas);
00778   
00779   bd = ensure_bus_data (connection);
00780   if (bd == NULL)
00781     goto out;
00782 
00783   _dbus_assert (bd->unique_name == NULL);
00784   
00785   bd->unique_name = _dbus_strdup (unique_name);
00786   success = bd->unique_name != NULL;
00787 
00788 out:
00789   _DBUS_UNLOCK (bus_datas);
00790   
00791   return success;
00792 }
00793 
00812 const char*
00813 dbus_bus_get_unique_name (DBusConnection *connection)
00814 {
00815   BusData *bd;
00816   const char *unique_name = NULL;
00817 
00818   _dbus_return_val_if_fail (connection != NULL, NULL);
00819 
00820   _DBUS_LOCK (bus_datas);
00821   
00822   bd = ensure_bus_data (connection);
00823   if (bd == NULL)
00824     goto out;
00825 
00826   unique_name = bd->unique_name;
00827 
00828 out:
00829   _DBUS_UNLOCK (bus_datas);
00830 
00831   return unique_name;
00832 }
00833 
00857 unsigned long
00858 dbus_bus_get_unix_user (DBusConnection *connection,
00859                         const char     *name,
00860                         DBusError      *error)
00861 {
00862   DBusMessage *message, *reply;
00863   dbus_uint32_t uid;
00864 
00865   _dbus_return_val_if_fail (connection != NULL, DBUS_UID_UNSET);
00866   _dbus_return_val_if_fail (name != NULL, DBUS_UID_UNSET);
00867   _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), DBUS_UID_UNSET);
00868   _dbus_return_val_if_error_is_set (error, DBUS_UID_UNSET);
00869   
00870   message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
00871                                           DBUS_PATH_DBUS,
00872                                           DBUS_INTERFACE_DBUS,
00873                                           "GetConnectionUnixUser");
00874 
00875   if (message == NULL)
00876     {
00877       _DBUS_SET_OOM (error);
00878       return DBUS_UID_UNSET;
00879     }
00880  
00881   if (!dbus_message_append_args (message,
00882                                  DBUS_TYPE_STRING, &name,
00883                                  DBUS_TYPE_INVALID))
00884     {
00885       dbus_message_unref (message);
00886       _DBUS_SET_OOM (error);
00887       return DBUS_UID_UNSET;
00888     }
00889   
00890   reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
00891                                                      error);
00892   
00893   dbus_message_unref (message);
00894   
00895   if (reply == NULL)
00896     {
00897       _DBUS_ASSERT_ERROR_IS_SET (error);
00898       return DBUS_UID_UNSET;
00899     }  
00900 
00901   if (dbus_set_error_from_message (error, reply))
00902     {
00903       _DBUS_ASSERT_ERROR_IS_SET (error);
00904       dbus_message_unref (reply);
00905       return DBUS_UID_UNSET;
00906     }
00907   
00908   if (!dbus_message_get_args (reply, error,
00909                               DBUS_TYPE_UINT32, &uid,
00910                               DBUS_TYPE_INVALID))
00911     {
00912       _DBUS_ASSERT_ERROR_IS_SET (error);
00913       dbus_message_unref (reply);
00914       return DBUS_UID_UNSET;
00915     }
00916 
00917   dbus_message_unref (reply);
00918   
00919   return (unsigned long) uid;
00920 }
00921 
00940 char*
00941 dbus_bus_get_id (DBusConnection *connection,
00942                  DBusError      *error)
00943 {
00944   DBusMessage *message, *reply;
00945   char *id;
00946   const char *v_STRING;
00947 
00948   _dbus_return_val_if_fail (connection != NULL, NULL);
00949   _dbus_return_val_if_error_is_set (error, NULL);
00950   
00951   message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
00952                                           DBUS_PATH_DBUS,
00953                                           DBUS_INTERFACE_DBUS,
00954                                           "GetId");
00955   
00956   if (message == NULL)
00957     {
00958       _DBUS_SET_OOM (error);
00959       return NULL;
00960     }
00961   
00962   reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
00963                                                      error);
00964   
00965   dbus_message_unref (message);
00966   
00967   if (reply == NULL)
00968     {
00969       _DBUS_ASSERT_ERROR_IS_SET (error);
00970       return NULL;
00971     }  
00972 
00973   if (dbus_set_error_from_message (error, reply))
00974     {
00975       _DBUS_ASSERT_ERROR_IS_SET (error);
00976       dbus_message_unref (reply);
00977       return NULL;
00978     }
00979 
00980   v_STRING = NULL;
00981   if (!dbus_message_get_args (reply, error,
00982                               DBUS_TYPE_STRING, &v_STRING,
00983                               DBUS_TYPE_INVALID))
00984     {
00985       _DBUS_ASSERT_ERROR_IS_SET (error);
00986       dbus_message_unref (reply);
00987       return NULL;
00988     }
00989 
00990   id = _dbus_strdup (v_STRING); /* may be NULL */
00991   
00992   dbus_message_unref (reply);
00993 
00994   if (id == NULL)
00995     _DBUS_SET_OOM (error);
00996 
00997   /* FIXME it might be nice to cache the ID locally */
00998   
00999   return id;
01000 }
01001 
01104 int
01105 dbus_bus_request_name (DBusConnection *connection,
01106                        const char     *name,
01107                        unsigned int    flags,
01108                        DBusError      *error)
01109 {
01110   DBusMessage *message, *reply;
01111   dbus_uint32_t result;
01112 
01113   _dbus_return_val_if_fail (connection != NULL, 0);
01114   _dbus_return_val_if_fail (name != NULL, 0);
01115   _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
01116   _dbus_return_val_if_error_is_set (error, 0);
01117   
01118   message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
01119                                           DBUS_PATH_DBUS,
01120                                           DBUS_INTERFACE_DBUS,
01121                                           "RequestName");
01122 
01123   if (message == NULL)
01124     {
01125       _DBUS_SET_OOM (error);
01126       return -1;
01127     }
01128  
01129   if (!dbus_message_append_args (message,
01130                                  DBUS_TYPE_STRING, &name,
01131                                  DBUS_TYPE_UINT32, &flags,
01132                                  DBUS_TYPE_INVALID))
01133     {
01134       dbus_message_unref (message);
01135       _DBUS_SET_OOM (error);
01136       return -1;
01137     }
01138   
01139   reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
01140                                                      error);
01141   
01142   dbus_message_unref (message);
01143   
01144   if (reply == NULL)
01145     {
01146       _DBUS_ASSERT_ERROR_IS_SET (error);
01147       return -1;
01148     }  
01149 
01150   if (dbus_set_error_from_message (error, reply))
01151     {
01152       _DBUS_ASSERT_ERROR_IS_SET (error);
01153       dbus_message_unref (reply);
01154       return -1;
01155     }
01156   
01157   if (!dbus_message_get_args (reply, error,
01158                               DBUS_TYPE_UINT32, &result,
01159                               DBUS_TYPE_INVALID))
01160     {
01161       _DBUS_ASSERT_ERROR_IS_SET (error);
01162       dbus_message_unref (reply);
01163       return -1;
01164     }
01165 
01166   dbus_message_unref (reply);
01167   
01168   return result;
01169 }
01170 
01171 
01190 int
01191 dbus_bus_release_name (DBusConnection *connection,
01192                        const char     *name,
01193                        DBusError      *error)
01194 {
01195   DBusMessage *message, *reply;
01196   dbus_uint32_t result;
01197 
01198   _dbus_return_val_if_fail (connection != NULL, 0);
01199   _dbus_return_val_if_fail (name != NULL, 0);
01200   _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
01201   _dbus_return_val_if_error_is_set (error, 0);
01202 
01203   message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
01204                                           DBUS_PATH_DBUS,
01205                                           DBUS_INTERFACE_DBUS,
01206                                           "ReleaseName");
01207 
01208   if (message == NULL)
01209     {
01210       _DBUS_SET_OOM (error);
01211       return -1;
01212     }
01213 
01214   if (!dbus_message_append_args (message,
01215                                  DBUS_TYPE_STRING, &name,
01216                                  DBUS_TYPE_INVALID))
01217     {
01218       dbus_message_unref (message);
01219       _DBUS_SET_OOM (error);
01220       return -1;
01221     }
01222 
01223   reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
01224                                                      error);
01225 
01226   dbus_message_unref (message);
01227 
01228   if (reply == NULL)
01229     {
01230       _DBUS_ASSERT_ERROR_IS_SET (error);
01231       return -1;
01232     }
01233 
01234   if (dbus_set_error_from_message (error, reply))
01235     {
01236       _DBUS_ASSERT_ERROR_IS_SET (error);
01237       dbus_message_unref (reply);
01238       return -1;
01239     }
01240 
01241   if (!dbus_message_get_args (reply, error,
01242                               DBUS_TYPE_UINT32, &result,
01243                               DBUS_TYPE_INVALID))
01244     {
01245       _DBUS_ASSERT_ERROR_IS_SET (error);
01246       dbus_message_unref (reply);
01247       return -1;
01248     }
01249 
01250   dbus_message_unref (reply);
01251 
01252   return result;
01253 }
01254 
01272 dbus_bool_t
01273 dbus_bus_name_has_owner (DBusConnection *connection,
01274                          const char     *name,
01275                          DBusError      *error)
01276 {
01277   DBusMessage *message, *reply;
01278   dbus_bool_t exists;
01279 
01280   _dbus_return_val_if_fail (connection != NULL, FALSE);
01281   _dbus_return_val_if_fail (name != NULL, FALSE);
01282   _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
01283   _dbus_return_val_if_error_is_set (error, FALSE);
01284   
01285   message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
01286                                           DBUS_PATH_DBUS,
01287                                           DBUS_INTERFACE_DBUS,
01288                                           "NameHasOwner");
01289   if (message == NULL)
01290     {
01291       _DBUS_SET_OOM (error);
01292       return FALSE;
01293     }
01294   
01295   if (!dbus_message_append_args (message,
01296                                  DBUS_TYPE_STRING, &name,
01297                                  DBUS_TYPE_INVALID))
01298     {
01299       dbus_message_unref (message);
01300       _DBUS_SET_OOM (error);
01301       return FALSE;
01302     }
01303   
01304   reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
01305   dbus_message_unref (message);
01306 
01307   if (reply == NULL)
01308     {
01309       _DBUS_ASSERT_ERROR_IS_SET (error);
01310       return FALSE;
01311     }
01312 
01313   if (!dbus_message_get_args (reply, error,
01314                               DBUS_TYPE_BOOLEAN, &exists,
01315                               DBUS_TYPE_INVALID))
01316     {
01317       _DBUS_ASSERT_ERROR_IS_SET (error);
01318       dbus_message_unref (reply);
01319       return FALSE;
01320     }
01321   
01322   dbus_message_unref (reply);
01323   return exists;
01324 }
01325 
01348 dbus_bool_t
01349 dbus_bus_start_service_by_name (DBusConnection *connection,
01350                                 const char     *name,
01351                                 dbus_uint32_t   flags,
01352                                 dbus_uint32_t  *result,
01353                                 DBusError      *error)
01354 {
01355   DBusMessage *msg;
01356   DBusMessage *reply;
01357 
01358   _dbus_return_val_if_fail (connection != NULL, FALSE);
01359   _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
01360   
01361   msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
01362                                       DBUS_PATH_DBUS,
01363                                       DBUS_INTERFACE_DBUS,
01364                                       "StartServiceByName");
01365 
01366   if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &name,
01367                                  DBUS_TYPE_UINT32, &flags, DBUS_TYPE_INVALID))
01368     {
01369       dbus_message_unref (msg);
01370       _DBUS_SET_OOM (error);
01371       return FALSE;
01372     }
01373 
01374   reply = dbus_connection_send_with_reply_and_block (connection, msg,
01375                                                      -1, error);
01376   dbus_message_unref (msg);
01377 
01378   if (reply == NULL)
01379     {
01380       _DBUS_ASSERT_ERROR_IS_SET (error);
01381       return FALSE;
01382     }
01383 
01384   if (dbus_set_error_from_message (error, reply))
01385     {
01386       _DBUS_ASSERT_ERROR_IS_SET (error);
01387       dbus_message_unref (reply);
01388       return FALSE;
01389     }
01390 
01391   if (result != NULL &&
01392       !dbus_message_get_args (reply, error, DBUS_TYPE_UINT32,
01393                               result, DBUS_TYPE_INVALID))
01394     {
01395       _DBUS_ASSERT_ERROR_IS_SET (error);
01396       dbus_message_unref (reply);
01397       return FALSE;
01398     }
01399   
01400   dbus_message_unref (reply);
01401   return TRUE;
01402 }
01403 
01404 static void
01405 send_no_return_values (DBusConnection *connection,
01406                        DBusMessage    *msg,
01407                        DBusError      *error)
01408 {
01409   if (error)
01410     {
01411       /* Block to check success codepath */
01412       DBusMessage *reply;
01413       
01414       reply = dbus_connection_send_with_reply_and_block (connection, msg,
01415                                                          -1, error);
01416       
01417       if (reply == NULL)
01418         _DBUS_ASSERT_ERROR_IS_SET (error);
01419       else
01420         dbus_message_unref (reply);
01421     }
01422   else
01423     {
01424       /* Silently-fail nonblocking codepath */
01425       dbus_message_set_no_reply (msg, TRUE);
01426       dbus_connection_send (connection, msg, NULL);
01427     }
01428 }
01429 
01512 void
01513 dbus_bus_add_match (DBusConnection *connection,
01514                     const char     *rule,
01515                     DBusError      *error)
01516 {
01517   DBusMessage *msg;
01518 
01519   _dbus_return_if_fail (rule != NULL);
01520 
01521   msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
01522                                       DBUS_PATH_DBUS,
01523                                       DBUS_INTERFACE_DBUS,
01524                                       "AddMatch");
01525 
01526   if (msg == NULL)
01527     {
01528       _DBUS_SET_OOM (error);
01529       return;
01530     }
01531 
01532   if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
01533                                  DBUS_TYPE_INVALID))
01534     {
01535       dbus_message_unref (msg);
01536       _DBUS_SET_OOM (error);
01537       return;
01538     }
01539 
01540   send_no_return_values (connection, msg, error);
01541 
01542   dbus_message_unref (msg);
01543 }
01544 
01562 void
01563 dbus_bus_remove_match (DBusConnection *connection,
01564                        const char     *rule,
01565                        DBusError      *error)
01566 {
01567   DBusMessage *msg;
01568 
01569   _dbus_return_if_fail (rule != NULL);
01570   
01571   msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
01572                                       DBUS_PATH_DBUS,
01573                                       DBUS_INTERFACE_DBUS,
01574                                       "RemoveMatch");
01575 
01576   if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
01577                                  DBUS_TYPE_INVALID))
01578     {
01579       dbus_message_unref (msg);
01580       _DBUS_SET_OOM (error);
01581       return;
01582     }
01583 
01584   send_no_return_values (connection, msg, error);
01585 
01586   dbus_message_unref (msg);
01587 }
01588