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