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