• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List

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   message = NULL;
00665   reply = NULL;
00666 
00667   _DBUS_LOCK (bus_datas);
00668 
00669   bd = ensure_bus_data (connection);
00670   if (bd == NULL)
00671     {
00672       _DBUS_SET_OOM (error);
00673       goto out;
00674     }
00675 
00676   if (bd->unique_name != NULL)
00677     {
00678       _dbus_verbose ("Ignoring attempt to register the same DBusConnection %s with the message bus a second time.\n",
00679                      bd->unique_name);
00680       /* Success! */
00681       retval = TRUE;
00682       goto out;
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       goto out;
00694     }
00695   
00696   reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
00697 
00698   if (reply == NULL)
00699     goto out;
00700   else if (dbus_set_error_from_message (error, reply))
00701     goto out;
00702   else if (!dbus_message_get_args (reply, error,
00703                                    DBUS_TYPE_STRING, &name,
00704                                    DBUS_TYPE_INVALID))
00705     goto out;
00706   
00707   bd->unique_name = _dbus_strdup (name);
00708   if (bd->unique_name == NULL)
00709     {
00710       _DBUS_SET_OOM (error);
00711       goto out;
00712     }
00713   
00714   retval = TRUE;
00715   
00716  out:
00717   _DBUS_UNLOCK (bus_datas);
00718 
00719   if (message)
00720     dbus_message_unref (message);
00721 
00722   if (reply)
00723     dbus_message_unref (reply);
00724 
00725   if (!retval)
00726     _DBUS_ASSERT_ERROR_IS_SET (error);
00727 
00728   return retval;
00729 }
00730 
00731 
00766 dbus_bool_t
00767 dbus_bus_set_unique_name (DBusConnection *connection,
00768                           const char     *unique_name)
00769 {
00770   BusData *bd;
00771   dbus_bool_t success = FALSE;
00772 
00773   _dbus_return_val_if_fail (connection != NULL, FALSE);
00774   _dbus_return_val_if_fail (unique_name != NULL, FALSE);
00775 
00776   _DBUS_LOCK (bus_datas);
00777   
00778   bd = ensure_bus_data (connection);
00779   if (bd == NULL)
00780     goto out;
00781 
00782   _dbus_assert (bd->unique_name == NULL);
00783   
00784   bd->unique_name = _dbus_strdup (unique_name);
00785   success = bd->unique_name != NULL;
00786 
00787 out:
00788   _DBUS_UNLOCK (bus_datas);
00789   
00790   return success;
00791 }
00792 
00811 const char*
00812 dbus_bus_get_unique_name (DBusConnection *connection)
00813 {
00814   BusData *bd;
00815   const char *unique_name = NULL;
00816 
00817   _dbus_return_val_if_fail (connection != NULL, NULL);
00818 
00819   _DBUS_LOCK (bus_datas);
00820   
00821   bd = ensure_bus_data (connection);
00822   if (bd == NULL)
00823     goto out;
00824 
00825   unique_name = bd->unique_name;
00826 
00827 out:
00828   _DBUS_UNLOCK (bus_datas);
00829 
00830   return unique_name;
00831 }
00832 
00856 unsigned long
00857 dbus_bus_get_unix_user (DBusConnection *connection,
00858                         const char     *name,
00859                         DBusError      *error)
00860 {
00861   DBusMessage *message, *reply;
00862   dbus_uint32_t uid;
00863 
00864   _dbus_return_val_if_fail (connection != NULL, DBUS_UID_UNSET);
00865   _dbus_return_val_if_fail (name != NULL, DBUS_UID_UNSET);
00866   _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), DBUS_UID_UNSET);
00867   _dbus_return_val_if_error_is_set (error, DBUS_UID_UNSET);
00868   
00869   message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
00870                                           DBUS_PATH_DBUS,
00871                                           DBUS_INTERFACE_DBUS,
00872                                           "GetConnectionUnixUser");
00873 
00874   if (message == NULL)
00875     {
00876       _DBUS_SET_OOM (error);
00877       return DBUS_UID_UNSET;
00878     }
00879  
00880   if (!dbus_message_append_args (message,
00881                                  DBUS_TYPE_STRING, &name,
00882                                  DBUS_TYPE_INVALID))
00883     {
00884       dbus_message_unref (message);
00885       _DBUS_SET_OOM (error);
00886       return DBUS_UID_UNSET;
00887     }
00888   
00889   reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
00890                                                      error);
00891   
00892   dbus_message_unref (message);
00893   
00894   if (reply == NULL)
00895     {
00896       _DBUS_ASSERT_ERROR_IS_SET (error);
00897       return DBUS_UID_UNSET;
00898     }  
00899 
00900   if (dbus_set_error_from_message (error, reply))
00901     {
00902       _DBUS_ASSERT_ERROR_IS_SET (error);
00903       dbus_message_unref (reply);
00904       return DBUS_UID_UNSET;
00905     }
00906   
00907   if (!dbus_message_get_args (reply, error,
00908                               DBUS_TYPE_UINT32, &uid,
00909                               DBUS_TYPE_INVALID))
00910     {
00911       _DBUS_ASSERT_ERROR_IS_SET (error);
00912       dbus_message_unref (reply);
00913       return DBUS_UID_UNSET;
00914     }
00915 
00916   dbus_message_unref (reply);
00917   
00918   return (unsigned long) uid;
00919 }
00920 
00939 char*
00940 dbus_bus_get_id (DBusConnection *connection,
00941                  DBusError      *error)
00942 {
00943   DBusMessage *message, *reply;
00944   char *id;
00945   const char *v_STRING;
00946 
00947   _dbus_return_val_if_fail (connection != NULL, NULL);
00948   _dbus_return_val_if_error_is_set (error, NULL);
00949   
00950   message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
00951                                           DBUS_PATH_DBUS,
00952                                           DBUS_INTERFACE_DBUS,
00953                                           "GetId");
00954   
00955   if (message == NULL)
00956     {
00957       _DBUS_SET_OOM (error);
00958       return NULL;
00959     }
00960   
00961   reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
00962                                                      error);
00963   
00964   dbus_message_unref (message);
00965   
00966   if (reply == NULL)
00967     {
00968       _DBUS_ASSERT_ERROR_IS_SET (error);
00969       return NULL;
00970     }  
00971 
00972   if (dbus_set_error_from_message (error, reply))
00973     {
00974       _DBUS_ASSERT_ERROR_IS_SET (error);
00975       dbus_message_unref (reply);
00976       return NULL;
00977     }
00978 
00979   v_STRING = NULL;
00980   if (!dbus_message_get_args (reply, error,
00981                               DBUS_TYPE_STRING, &v_STRING,
00982                               DBUS_TYPE_INVALID))
00983     {
00984       _DBUS_ASSERT_ERROR_IS_SET (error);
00985       dbus_message_unref (reply);
00986       return NULL;
00987     }
00988 
00989   id = _dbus_strdup (v_STRING); /* may be NULL */
00990   
00991   dbus_message_unref (reply);
00992 
00993   if (id == NULL)
00994     _DBUS_SET_OOM (error);
00995 
00996   /* FIXME it might be nice to cache the ID locally */
00997   
00998   return id;
00999 }
01000 
01103 int
01104 dbus_bus_request_name (DBusConnection *connection,
01105                        const char     *name,
01106                        unsigned int    flags,
01107                        DBusError      *error)
01108 {
01109   DBusMessage *message, *reply;
01110   dbus_uint32_t result;
01111 
01112   _dbus_return_val_if_fail (connection != NULL, 0);
01113   _dbus_return_val_if_fail (name != NULL, 0);
01114   _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
01115   _dbus_return_val_if_error_is_set (error, 0);
01116   
01117   message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
01118                                           DBUS_PATH_DBUS,
01119                                           DBUS_INTERFACE_DBUS,
01120                                           "RequestName");
01121 
01122   if (message == NULL)
01123     {
01124       _DBUS_SET_OOM (error);
01125       return -1;
01126     }
01127  
01128   if (!dbus_message_append_args (message,
01129                                  DBUS_TYPE_STRING, &name,
01130                                  DBUS_TYPE_UINT32, &flags,
01131                                  DBUS_TYPE_INVALID))
01132     {
01133       dbus_message_unref (message);
01134       _DBUS_SET_OOM (error);
01135       return -1;
01136     }
01137   
01138   reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
01139                                                      error);
01140   
01141   dbus_message_unref (message);
01142   
01143   if (reply == NULL)
01144     {
01145       _DBUS_ASSERT_ERROR_IS_SET (error);
01146       return -1;
01147     }  
01148 
01149   if (dbus_set_error_from_message (error, reply))
01150     {
01151       _DBUS_ASSERT_ERROR_IS_SET (error);
01152       dbus_message_unref (reply);
01153       return -1;
01154     }
01155   
01156   if (!dbus_message_get_args (reply, error,
01157                               DBUS_TYPE_UINT32, &result,
01158                               DBUS_TYPE_INVALID))
01159     {
01160       _DBUS_ASSERT_ERROR_IS_SET (error);
01161       dbus_message_unref (reply);
01162       return -1;
01163     }
01164 
01165   dbus_message_unref (reply);
01166   
01167   return result;
01168 }
01169 
01170 
01189 int
01190 dbus_bus_release_name (DBusConnection *connection,
01191                        const char     *name,
01192                        DBusError      *error)
01193 {
01194   DBusMessage *message, *reply;
01195   dbus_uint32_t result;
01196 
01197   _dbus_return_val_if_fail (connection != NULL, 0);
01198   _dbus_return_val_if_fail (name != NULL, 0);
01199   _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
01200   _dbus_return_val_if_error_is_set (error, 0);
01201 
01202   message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
01203                                           DBUS_PATH_DBUS,
01204                                           DBUS_INTERFACE_DBUS,
01205                                           "ReleaseName");
01206 
01207   if (message == NULL)
01208     {
01209       _DBUS_SET_OOM (error);
01210       return -1;
01211     }
01212 
01213   if (!dbus_message_append_args (message,
01214                                  DBUS_TYPE_STRING, &name,
01215                                  DBUS_TYPE_INVALID))
01216     {
01217       dbus_message_unref (message);
01218       _DBUS_SET_OOM (error);
01219       return -1;
01220     }
01221 
01222   reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
01223                                                      error);
01224 
01225   dbus_message_unref (message);
01226 
01227   if (reply == NULL)
01228     {
01229       _DBUS_ASSERT_ERROR_IS_SET (error);
01230       return -1;
01231     }
01232 
01233   if (dbus_set_error_from_message (error, reply))
01234     {
01235       _DBUS_ASSERT_ERROR_IS_SET (error);
01236       dbus_message_unref (reply);
01237       return -1;
01238     }
01239 
01240   if (!dbus_message_get_args (reply, error,
01241                               DBUS_TYPE_UINT32, &result,
01242                               DBUS_TYPE_INVALID))
01243     {
01244       _DBUS_ASSERT_ERROR_IS_SET (error);
01245       dbus_message_unref (reply);
01246       return -1;
01247     }
01248 
01249   dbus_message_unref (reply);
01250 
01251   return result;
01252 }
01253 
01271 dbus_bool_t
01272 dbus_bus_name_has_owner (DBusConnection *connection,
01273                          const char     *name,
01274                          DBusError      *error)
01275 {
01276   DBusMessage *message, *reply;
01277   dbus_bool_t exists;
01278 
01279   _dbus_return_val_if_fail (connection != NULL, FALSE);
01280   _dbus_return_val_if_fail (name != NULL, FALSE);
01281   _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
01282   _dbus_return_val_if_error_is_set (error, FALSE);
01283   
01284   message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
01285                                           DBUS_PATH_DBUS,
01286                                           DBUS_INTERFACE_DBUS,
01287                                           "NameHasOwner");
01288   if (message == NULL)
01289     {
01290       _DBUS_SET_OOM (error);
01291       return FALSE;
01292     }
01293   
01294   if (!dbus_message_append_args (message,
01295                                  DBUS_TYPE_STRING, &name,
01296                                  DBUS_TYPE_INVALID))
01297     {
01298       dbus_message_unref (message);
01299       _DBUS_SET_OOM (error);
01300       return FALSE;
01301     }
01302   
01303   reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
01304   dbus_message_unref (message);
01305 
01306   if (reply == NULL)
01307     {
01308       _DBUS_ASSERT_ERROR_IS_SET (error);
01309       return FALSE;
01310     }
01311 
01312   if (!dbus_message_get_args (reply, error,
01313                               DBUS_TYPE_BOOLEAN, &exists,
01314                               DBUS_TYPE_INVALID))
01315     {
01316       _DBUS_ASSERT_ERROR_IS_SET (error);
01317       dbus_message_unref (reply);
01318       return FALSE;
01319     }
01320   
01321   dbus_message_unref (reply);
01322   return exists;
01323 }
01324 
01347 dbus_bool_t
01348 dbus_bus_start_service_by_name (DBusConnection *connection,
01349                                 const char     *name,
01350                                 dbus_uint32_t   flags,
01351                                 dbus_uint32_t  *result,
01352                                 DBusError      *error)
01353 {
01354   DBusMessage *msg;
01355   DBusMessage *reply;
01356 
01357   _dbus_return_val_if_fail (connection != NULL, FALSE);
01358   _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
01359   
01360   msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
01361                                       DBUS_PATH_DBUS,
01362                                       DBUS_INTERFACE_DBUS,
01363                                       "StartServiceByName");
01364 
01365   if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &name,
01366                                  DBUS_TYPE_UINT32, &flags, DBUS_TYPE_INVALID))
01367     {
01368       dbus_message_unref (msg);
01369       _DBUS_SET_OOM (error);
01370       return FALSE;
01371     }
01372 
01373   reply = dbus_connection_send_with_reply_and_block (connection, msg,
01374                                                      -1, error);
01375   dbus_message_unref (msg);
01376 
01377   if (reply == NULL)
01378     {
01379       _DBUS_ASSERT_ERROR_IS_SET (error);
01380       return FALSE;
01381     }
01382 
01383   if (dbus_set_error_from_message (error, reply))
01384     {
01385       _DBUS_ASSERT_ERROR_IS_SET (error);
01386       dbus_message_unref (reply);
01387       return FALSE;
01388     }
01389 
01390   if (result != NULL &&
01391       !dbus_message_get_args (reply, error, DBUS_TYPE_UINT32,
01392                               result, DBUS_TYPE_INVALID))
01393     {
01394       _DBUS_ASSERT_ERROR_IS_SET (error);
01395       dbus_message_unref (reply);
01396       return FALSE;
01397     }
01398   
01399   dbus_message_unref (reply);
01400   return TRUE;
01401 }
01402 
01403 static void
01404 send_no_return_values (DBusConnection *connection,
01405                        DBusMessage    *msg,
01406                        DBusError      *error)
01407 {
01408   if (error)
01409     {
01410       /* Block to check success codepath */
01411       DBusMessage *reply;
01412       
01413       reply = dbus_connection_send_with_reply_and_block (connection, msg,
01414                                                          -1, error);
01415       
01416       if (reply == NULL)
01417         _DBUS_ASSERT_ERROR_IS_SET (error);
01418       else
01419         dbus_message_unref (reply);
01420     }
01421   else
01422     {
01423       /* Silently-fail nonblocking codepath */
01424       dbus_message_set_no_reply (msg, TRUE);
01425       dbus_connection_send (connection, msg, NULL);
01426     }
01427 }
01428 
01511 void
01512 dbus_bus_add_match (DBusConnection *connection,
01513                     const char     *rule,
01514                     DBusError      *error)
01515 {
01516   DBusMessage *msg;
01517 
01518   _dbus_return_if_fail (rule != NULL);
01519 
01520   msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
01521                                       DBUS_PATH_DBUS,
01522                                       DBUS_INTERFACE_DBUS,
01523                                       "AddMatch");
01524 
01525   if (msg == NULL)
01526     {
01527       _DBUS_SET_OOM (error);
01528       return;
01529     }
01530 
01531   if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
01532                                  DBUS_TYPE_INVALID))
01533     {
01534       dbus_message_unref (msg);
01535       _DBUS_SET_OOM (error);
01536       return;
01537     }
01538 
01539   send_no_return_values (connection, msg, error);
01540 
01541   dbus_message_unref (msg);
01542 }
01543 
01561 void
01562 dbus_bus_remove_match (DBusConnection *connection,
01563                        const char     *rule,
01564                        DBusError      *error)
01565 {
01566   DBusMessage *msg;
01567 
01568   _dbus_return_if_fail (rule != NULL);
01569   
01570   msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
01571                                       DBUS_PATH_DBUS,
01572                                       DBUS_INTERFACE_DBUS,
01573                                       "RemoveMatch");
01574 
01575   if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
01576                                  DBUS_TYPE_INVALID))
01577     {
01578       dbus_message_unref (msg);
01579       _DBUS_SET_OOM (error);
01580       return;
01581     }
01582 
01583   send_no_return_values (connection, msg, error);
01584 
01585   dbus_message_unref (msg);
01586 }
01587 

Generated on Fri Jul 11 2014 20:41:27 for D-Bus by  doxygen 1.7.1