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