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