D-Bus 1.6.12

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   connection = NULL;
00438 
00439   _DBUS_LOCK (bus);
00440 
00441   if (!init_connections_unlocked ())
00442     {
00443       _DBUS_SET_OOM (error);
00444       goto out;
00445     }
00446 
00447   /* We want to use the activation address even if the
00448    * activating bus is the session or system bus,
00449    * per the spec.
00450    */
00451   address_type = type;
00452   
00453   /* Use the real type of the activation bus for getting its
00454    * connection, but only if the real type's address is available. (If
00455    * the activating bus isn't a well-known bus then
00456    * activation_bus_type == DBUS_BUS_STARTER)
00457    */
00458   if (type == DBUS_BUS_STARTER &&
00459       bus_connection_addresses[activation_bus_type] != NULL)
00460     type = activation_bus_type;
00461   
00462   if (!private && bus_connections[type] != NULL)
00463     {
00464       connection = bus_connections[type];
00465       dbus_connection_ref (connection);
00466       goto out;
00467     }
00468 
00469   address = bus_connection_addresses[address_type];
00470   if (address == NULL)
00471     {
00472       dbus_set_error (error, DBUS_ERROR_FAILED,
00473                       "Unable to determine the address of the message bus (try 'man dbus-launch' and 'man dbus-daemon' for help)");
00474       goto out;
00475     }
00476 
00477   if (private)
00478     connection = dbus_connection_open_private (address, error);
00479   else
00480     connection = dbus_connection_open (address, error);
00481   
00482   if (!connection)
00483     {
00484       goto out;
00485     }
00486 
00487   if (!dbus_bus_register (connection, error))
00488     {
00489       _dbus_connection_close_possibly_shared (connection);
00490       dbus_connection_unref (connection);
00491       connection = NULL;
00492       goto out;
00493     }
00494 
00495   if (!private)
00496     {
00497       /* store a weak ref to the connection (dbus-connection.c is
00498        * supposed to have a strong ref that it drops on disconnect,
00499        * since this is a shared connection)
00500        */
00501       bus_connections[type] = connection;
00502     }
00503 
00504   /* By default we're bound to the lifecycle of
00505    * the message bus.
00506    */
00507   dbus_connection_set_exit_on_disconnect (connection,
00508                                           TRUE);
00509  
00510   _DBUS_LOCK (bus_datas);
00511   bd = ensure_bus_data (connection);
00512   _dbus_assert (bd != NULL); /* it should have been created on
00513                                 register, so OOM not possible */
00514   bd->is_well_known = TRUE;
00515   _DBUS_UNLOCK (bus_datas);
00516 
00517 out:
00518   /* Return a reference to the caller, or NULL with error set. */
00519   if (connection == NULL)
00520     _DBUS_ASSERT_ERROR_IS_SET (error);
00521 
00522   _DBUS_UNLOCK (bus);
00523   return connection;
00524 }
00525 
00526  /* end of implementation details docs */
00528 
00559 DBusConnection *
00560 dbus_bus_get (DBusBusType  type,
00561               DBusError   *error)
00562 {
00563   return internal_bus_get (type, FALSE, error);
00564 }
00565 
00591 DBusConnection *
00592 dbus_bus_get_private (DBusBusType  type,
00593                       DBusError   *error)
00594 {
00595   return internal_bus_get (type, TRUE, error);
00596 }
00597 
00647 dbus_bool_t
00648 dbus_bus_register (DBusConnection *connection,
00649                    DBusError      *error)
00650 {
00651   DBusMessage *message, *reply;
00652   char *name;
00653   BusData *bd;
00654   dbus_bool_t retval;
00655 
00656   _dbus_return_val_if_fail (connection != NULL, FALSE);
00657   _dbus_return_val_if_error_is_set (error, FALSE);
00658 
00659   retval = FALSE;
00660   message = NULL;
00661   reply = NULL;
00662 
00663   _DBUS_LOCK (bus_datas);
00664 
00665   bd = ensure_bus_data (connection);
00666   if (bd == NULL)
00667     {
00668       _DBUS_SET_OOM (error);
00669       goto out;
00670     }
00671 
00672   if (bd->unique_name != NULL)
00673     {
00674       _dbus_verbose ("Ignoring attempt to register the same DBusConnection %s with the message bus a second time.\n",
00675                      bd->unique_name);
00676       /* Success! */
00677       retval = TRUE;
00678       goto out;
00679     }
00680   
00681   message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
00682                                           DBUS_PATH_DBUS,
00683                                           DBUS_INTERFACE_DBUS,
00684                                           "Hello"); 
00685 
00686   if (!message)
00687     {
00688       _DBUS_SET_OOM (error);
00689       goto out;
00690     }
00691   
00692   reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
00693 
00694   if (reply == NULL)
00695     goto out;
00696   else if (dbus_set_error_from_message (error, reply))
00697     goto out;
00698   else if (!dbus_message_get_args (reply, error,
00699                                    DBUS_TYPE_STRING, &name,
00700                                    DBUS_TYPE_INVALID))
00701     goto out;
00702   
00703   bd->unique_name = _dbus_strdup (name);
00704   if (bd->unique_name == NULL)
00705     {
00706       _DBUS_SET_OOM (error);
00707       goto out;
00708     }
00709   
00710   retval = TRUE;
00711   
00712  out:
00713   _DBUS_UNLOCK (bus_datas);
00714 
00715   if (message)
00716     dbus_message_unref (message);
00717 
00718   if (reply)
00719     dbus_message_unref (reply);
00720 
00721   if (!retval)
00722     _DBUS_ASSERT_ERROR_IS_SET (error);
00723 
00724   return retval;
00725 }
00726 
00727 
00762 dbus_bool_t
00763 dbus_bus_set_unique_name (DBusConnection *connection,
00764                           const char     *unique_name)
00765 {
00766   BusData *bd;
00767   dbus_bool_t success = FALSE;
00768 
00769   _dbus_return_val_if_fail (connection != NULL, FALSE);
00770   _dbus_return_val_if_fail (unique_name != NULL, FALSE);
00771 
00772   _DBUS_LOCK (bus_datas);
00773   
00774   bd = ensure_bus_data (connection);
00775   if (bd == NULL)
00776     goto out;
00777 
00778   _dbus_assert (bd->unique_name == NULL);
00779   
00780   bd->unique_name = _dbus_strdup (unique_name);
00781   success = bd->unique_name != NULL;
00782 
00783 out:
00784   _DBUS_UNLOCK (bus_datas);
00785   
00786   return success;
00787 }
00788 
00807 const char*
00808 dbus_bus_get_unique_name (DBusConnection *connection)
00809 {
00810   BusData *bd;
00811   const char *unique_name = NULL;
00812 
00813   _dbus_return_val_if_fail (connection != NULL, NULL);
00814 
00815   _DBUS_LOCK (bus_datas);
00816   
00817   bd = ensure_bus_data (connection);
00818   if (bd == NULL)
00819     goto out;
00820 
00821   unique_name = bd->unique_name;
00822 
00823 out:
00824   _DBUS_UNLOCK (bus_datas);
00825 
00826   return unique_name;
00827 }
00828 
00852 unsigned long
00853 dbus_bus_get_unix_user (DBusConnection *connection,
00854                         const char     *name,
00855                         DBusError      *error)
00856 {
00857   DBusMessage *message, *reply;
00858   dbus_uint32_t uid;
00859 
00860   _dbus_return_val_if_fail (connection != NULL, DBUS_UID_UNSET);
00861   _dbus_return_val_if_fail (name != NULL, DBUS_UID_UNSET);
00862   _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), DBUS_UID_UNSET);
00863   _dbus_return_val_if_error_is_set (error, DBUS_UID_UNSET);
00864   
00865   message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
00866                                           DBUS_PATH_DBUS,
00867                                           DBUS_INTERFACE_DBUS,
00868                                           "GetConnectionUnixUser");
00869 
00870   if (message == NULL)
00871     {
00872       _DBUS_SET_OOM (error);
00873       return DBUS_UID_UNSET;
00874     }
00875  
00876   if (!dbus_message_append_args (message,
00877                                  DBUS_TYPE_STRING, &name,
00878                                  DBUS_TYPE_INVALID))
00879     {
00880       dbus_message_unref (message);
00881       _DBUS_SET_OOM (error);
00882       return DBUS_UID_UNSET;
00883     }
00884   
00885   reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
00886                                                      error);
00887   
00888   dbus_message_unref (message);
00889   
00890   if (reply == NULL)
00891     {
00892       _DBUS_ASSERT_ERROR_IS_SET (error);
00893       return DBUS_UID_UNSET;
00894     }  
00895 
00896   if (dbus_set_error_from_message (error, reply))
00897     {
00898       _DBUS_ASSERT_ERROR_IS_SET (error);
00899       dbus_message_unref (reply);
00900       return DBUS_UID_UNSET;
00901     }
00902   
00903   if (!dbus_message_get_args (reply, error,
00904                               DBUS_TYPE_UINT32, &uid,
00905                               DBUS_TYPE_INVALID))
00906     {
00907       _DBUS_ASSERT_ERROR_IS_SET (error);
00908       dbus_message_unref (reply);
00909       return DBUS_UID_UNSET;
00910     }
00911 
00912   dbus_message_unref (reply);
00913   
00914   return (unsigned long) uid;
00915 }
00916 
00935 char*
00936 dbus_bus_get_id (DBusConnection *connection,
00937                  DBusError      *error)
00938 {
00939   DBusMessage *message, *reply;
00940   char *id;
00941   const char *v_STRING;
00942 
00943   _dbus_return_val_if_fail (connection != NULL, NULL);
00944   _dbus_return_val_if_error_is_set (error, NULL);
00945   
00946   message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
00947                                           DBUS_PATH_DBUS,
00948                                           DBUS_INTERFACE_DBUS,
00949                                           "GetId");
00950   
00951   if (message == NULL)
00952     {
00953       _DBUS_SET_OOM (error);
00954       return NULL;
00955     }
00956   
00957   reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
00958                                                      error);
00959   
00960   dbus_message_unref (message);
00961   
00962   if (reply == NULL)
00963     {
00964       _DBUS_ASSERT_ERROR_IS_SET (error);
00965       return NULL;
00966     }  
00967 
00968   if (dbus_set_error_from_message (error, reply))
00969     {
00970       _DBUS_ASSERT_ERROR_IS_SET (error);
00971       dbus_message_unref (reply);
00972       return NULL;
00973     }
00974 
00975   v_STRING = NULL;
00976   if (!dbus_message_get_args (reply, error,
00977                               DBUS_TYPE_STRING, &v_STRING,
00978                               DBUS_TYPE_INVALID))
00979     {
00980       _DBUS_ASSERT_ERROR_IS_SET (error);
00981       dbus_message_unref (reply);
00982       return NULL;
00983     }
00984 
00985   id = _dbus_strdup (v_STRING); /* may be NULL */
00986   
00987   dbus_message_unref (reply);
00988 
00989   if (id == NULL)
00990     _DBUS_SET_OOM (error);
00991 
00992   /* FIXME it might be nice to cache the ID locally */
00993   
00994   return id;
00995 }
00996 
01099 int
01100 dbus_bus_request_name (DBusConnection *connection,
01101                        const char     *name,
01102                        unsigned int    flags,
01103                        DBusError      *error)
01104 {
01105   DBusMessage *message, *reply;
01106   dbus_uint32_t result;
01107 
01108   _dbus_return_val_if_fail (connection != NULL, 0);
01109   _dbus_return_val_if_fail (name != NULL, 0);
01110   _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
01111   _dbus_return_val_if_error_is_set (error, 0);
01112   
01113   message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
01114                                           DBUS_PATH_DBUS,
01115                                           DBUS_INTERFACE_DBUS,
01116                                           "RequestName");
01117 
01118   if (message == NULL)
01119     {
01120       _DBUS_SET_OOM (error);
01121       return -1;
01122     }
01123  
01124   if (!dbus_message_append_args (message,
01125                                  DBUS_TYPE_STRING, &name,
01126                                  DBUS_TYPE_UINT32, &flags,
01127                                  DBUS_TYPE_INVALID))
01128     {
01129       dbus_message_unref (message);
01130       _DBUS_SET_OOM (error);
01131       return -1;
01132     }
01133   
01134   reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
01135                                                      error);
01136   
01137   dbus_message_unref (message);
01138   
01139   if (reply == NULL)
01140     {
01141       _DBUS_ASSERT_ERROR_IS_SET (error);
01142       return -1;
01143     }  
01144 
01145   if (dbus_set_error_from_message (error, reply))
01146     {
01147       _DBUS_ASSERT_ERROR_IS_SET (error);
01148       dbus_message_unref (reply);
01149       return -1;
01150     }
01151   
01152   if (!dbus_message_get_args (reply, error,
01153                               DBUS_TYPE_UINT32, &result,
01154                               DBUS_TYPE_INVALID))
01155     {
01156       _DBUS_ASSERT_ERROR_IS_SET (error);
01157       dbus_message_unref (reply);
01158       return -1;
01159     }
01160 
01161   dbus_message_unref (reply);
01162   
01163   return result;
01164 }
01165 
01166 
01185 int
01186 dbus_bus_release_name (DBusConnection *connection,
01187                        const char     *name,
01188                        DBusError      *error)
01189 {
01190   DBusMessage *message, *reply;
01191   dbus_uint32_t result;
01192 
01193   _dbus_return_val_if_fail (connection != NULL, 0);
01194   _dbus_return_val_if_fail (name != NULL, 0);
01195   _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
01196   _dbus_return_val_if_error_is_set (error, 0);
01197 
01198   message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
01199                                           DBUS_PATH_DBUS,
01200                                           DBUS_INTERFACE_DBUS,
01201                                           "ReleaseName");
01202 
01203   if (message == NULL)
01204     {
01205       _DBUS_SET_OOM (error);
01206       return -1;
01207     }
01208 
01209   if (!dbus_message_append_args (message,
01210                                  DBUS_TYPE_STRING, &name,
01211                                  DBUS_TYPE_INVALID))
01212     {
01213       dbus_message_unref (message);
01214       _DBUS_SET_OOM (error);
01215       return -1;
01216     }
01217 
01218   reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
01219                                                      error);
01220 
01221   dbus_message_unref (message);
01222 
01223   if (reply == NULL)
01224     {
01225       _DBUS_ASSERT_ERROR_IS_SET (error);
01226       return -1;
01227     }
01228 
01229   if (dbus_set_error_from_message (error, reply))
01230     {
01231       _DBUS_ASSERT_ERROR_IS_SET (error);
01232       dbus_message_unref (reply);
01233       return -1;
01234     }
01235 
01236   if (!dbus_message_get_args (reply, error,
01237                               DBUS_TYPE_UINT32, &result,
01238                               DBUS_TYPE_INVALID))
01239     {
01240       _DBUS_ASSERT_ERROR_IS_SET (error);
01241       dbus_message_unref (reply);
01242       return -1;
01243     }
01244 
01245   dbus_message_unref (reply);
01246 
01247   return result;
01248 }
01249 
01267 dbus_bool_t
01268 dbus_bus_name_has_owner (DBusConnection *connection,
01269                          const char     *name,
01270                          DBusError      *error)
01271 {
01272   DBusMessage *message, *reply;
01273   dbus_bool_t exists;
01274 
01275   _dbus_return_val_if_fail (connection != NULL, FALSE);
01276   _dbus_return_val_if_fail (name != NULL, FALSE);
01277   _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
01278   _dbus_return_val_if_error_is_set (error, FALSE);
01279   
01280   message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
01281                                           DBUS_PATH_DBUS,
01282                                           DBUS_INTERFACE_DBUS,
01283                                           "NameHasOwner");
01284   if (message == NULL)
01285     {
01286       _DBUS_SET_OOM (error);
01287       return FALSE;
01288     }
01289   
01290   if (!dbus_message_append_args (message,
01291                                  DBUS_TYPE_STRING, &name,
01292                                  DBUS_TYPE_INVALID))
01293     {
01294       dbus_message_unref (message);
01295       _DBUS_SET_OOM (error);
01296       return FALSE;
01297     }
01298   
01299   reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
01300   dbus_message_unref (message);
01301 
01302   if (reply == NULL)
01303     {
01304       _DBUS_ASSERT_ERROR_IS_SET (error);
01305       return FALSE;
01306     }
01307 
01308   if (!dbus_message_get_args (reply, error,
01309                               DBUS_TYPE_BOOLEAN, &exists,
01310                               DBUS_TYPE_INVALID))
01311     {
01312       _DBUS_ASSERT_ERROR_IS_SET (error);
01313       dbus_message_unref (reply);
01314       return FALSE;
01315     }
01316   
01317   dbus_message_unref (reply);
01318   return exists;
01319 }
01320 
01343 dbus_bool_t
01344 dbus_bus_start_service_by_name (DBusConnection *connection,
01345                                 const char     *name,
01346                                 dbus_uint32_t   flags,
01347                                 dbus_uint32_t  *result,
01348                                 DBusError      *error)
01349 {
01350   DBusMessage *msg;
01351   DBusMessage *reply;
01352 
01353   _dbus_return_val_if_fail (connection != NULL, FALSE);
01354   _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
01355   
01356   msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
01357                                       DBUS_PATH_DBUS,
01358                                       DBUS_INTERFACE_DBUS,
01359                                       "StartServiceByName");
01360 
01361   if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &name,
01362                                  DBUS_TYPE_UINT32, &flags, DBUS_TYPE_INVALID))
01363     {
01364       dbus_message_unref (msg);
01365       _DBUS_SET_OOM (error);
01366       return FALSE;
01367     }
01368 
01369   reply = dbus_connection_send_with_reply_and_block (connection, msg,
01370                                                      -1, error);
01371   dbus_message_unref (msg);
01372 
01373   if (reply == NULL)
01374     {
01375       _DBUS_ASSERT_ERROR_IS_SET (error);
01376       return FALSE;
01377     }
01378 
01379   if (dbus_set_error_from_message (error, reply))
01380     {
01381       _DBUS_ASSERT_ERROR_IS_SET (error);
01382       dbus_message_unref (reply);
01383       return FALSE;
01384     }
01385 
01386   if (result != NULL &&
01387       !dbus_message_get_args (reply, error, DBUS_TYPE_UINT32,
01388                               result, DBUS_TYPE_INVALID))
01389     {
01390       _DBUS_ASSERT_ERROR_IS_SET (error);
01391       dbus_message_unref (reply);
01392       return FALSE;
01393     }
01394   
01395   dbus_message_unref (reply);
01396   return TRUE;
01397 }
01398 
01399 static void
01400 send_no_return_values (DBusConnection *connection,
01401                        DBusMessage    *msg,
01402                        DBusError      *error)
01403 {
01404   if (error)
01405     {
01406       /* Block to check success codepath */
01407       DBusMessage *reply;
01408       
01409       reply = dbus_connection_send_with_reply_and_block (connection, msg,
01410                                                          -1, error);
01411       
01412       if (reply == NULL)
01413         _DBUS_ASSERT_ERROR_IS_SET (error);
01414       else
01415         dbus_message_unref (reply);
01416     }
01417   else
01418     {
01419       /* Silently-fail nonblocking codepath */
01420       dbus_message_set_no_reply (msg, TRUE);
01421       dbus_connection_send (connection, msg, NULL);
01422     }
01423 }
01424 
01513 void
01514 dbus_bus_add_match (DBusConnection *connection,
01515                     const char     *rule,
01516                     DBusError      *error)
01517 {
01518   DBusMessage *msg;
01519 
01520   _dbus_return_if_fail (rule != NULL);
01521 
01522   msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
01523                                       DBUS_PATH_DBUS,
01524                                       DBUS_INTERFACE_DBUS,
01525                                       "AddMatch");
01526 
01527   if (msg == NULL)
01528     {
01529       _DBUS_SET_OOM (error);
01530       return;
01531     }
01532 
01533   if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
01534                                  DBUS_TYPE_INVALID))
01535     {
01536       dbus_message_unref (msg);
01537       _DBUS_SET_OOM (error);
01538       return;
01539     }
01540 
01541   send_no_return_values (connection, msg, error);
01542 
01543   dbus_message_unref (msg);
01544 }
01545 
01563 void
01564 dbus_bus_remove_match (DBusConnection *connection,
01565                        const char     *rule,
01566                        DBusError      *error)
01567 {
01568   DBusMessage *msg;
01569 
01570   _dbus_return_if_fail (rule != NULL);
01571   
01572   msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
01573                                       DBUS_PATH_DBUS,
01574                                       DBUS_INTERFACE_DBUS,
01575                                       "RemoveMatch");
01576 
01577   if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
01578                                  DBUS_TYPE_INVALID))
01579     {
01580       dbus_message_unref (msg);
01581       _DBUS_SET_OOM (error);
01582       return;
01583     }
01584 
01585   send_no_return_values (connection, msg, error);
01586 
01587   dbus_message_unref (msg);
01588 }
01589