* dbus/dbus-bus.c:
[platform/upstream/dbus.git] / dbus / dbus-bus.c
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 <string.h>
32
33 /**
34  * @defgroup DBusBus Message bus APIs
35  * @ingroup DBus
36  * @brief Functions for communicating with the message bus
37  *
38  * @todo right now the default address of the system bus is hardcoded,
39  * so if you change it in the global config file suddenly you have to
40  * set DBUS_SYSTEM_BUS_ADDRESS env variable.  Might be nice if the
41  * client lib somehow read the config file, or if the bus on startup
42  * somehow wrote out its address to a well-known spot, but might also
43  * not be worth it.
44  */
45
46 /**
47  * @defgroup DBusBusInternals Message bus APIs internals
48  * @ingroup DBusInternals
49  * @brief Internals of functions for communicating with the message bus
50  *
51  * @{
52  */
53
54 /**
55  * Block of message-bus-related data we attach to each
56  * #DBusConnection used with these convenience functions.
57  *
58  */
59 typedef struct
60 {
61   DBusConnection *connection; /**< Connection we're associated with */
62   char *unique_name; /**< Unique name of this connection */
63
64   unsigned int is_well_known : 1; /**< Is one of the well-known connections in our global array */
65 } BusData;
66
67 /** The slot we have reserved to store BusData.
68  */
69 static dbus_int32_t bus_data_slot = -1;
70
71 /** Number of bus types */
72 #define N_BUS_TYPES 3
73
74 static DBusConnection *bus_connections[N_BUS_TYPES];
75 static char *bus_connection_addresses[N_BUS_TYPES] = { NULL, NULL, NULL };
76
77 static DBusBusType activation_bus_type = DBUS_BUS_STARTER;
78
79 static dbus_bool_t initialized = FALSE;
80
81 /**
82  * Lock for globals in this file
83  */
84 _DBUS_DEFINE_GLOBAL_LOCK (bus);
85
86 static void
87 addresses_shutdown_func (void *data)
88 {
89   int i;
90
91   i = 0;
92   while (i < N_BUS_TYPES)
93     {
94       if (bus_connections[i] != NULL)
95         _dbus_warn ("dbus_shutdown() called but connections were still live!");
96       
97       dbus_free (bus_connection_addresses[i]);
98       bus_connection_addresses[i] = NULL;
99       ++i;
100     }
101
102   activation_bus_type = DBUS_BUS_STARTER;
103 }
104
105 static dbus_bool_t
106 get_from_env (char           **connection_p,
107               const char      *env_var)
108 {
109   const char *s;
110   
111   _dbus_assert (*connection_p == NULL);
112   
113   s = _dbus_getenv (env_var);
114   if (s == NULL || *s == '\0')
115     return TRUE; /* successfully didn't use the env var */
116   else
117     {
118       *connection_p = _dbus_strdup (s);
119       return *connection_p != NULL;
120     }
121 }
122
123 static dbus_bool_t
124 init_connections_unlocked (void)
125 {
126   if (!initialized)
127     {
128       const char *s;
129       int i;
130
131       i = 0;
132       while (i < N_BUS_TYPES)
133         {
134           bus_connections[i] = NULL;
135           ++i;
136         }
137
138       /* Don't init these twice, we may run this code twice if
139        * init_connections_unlocked() fails midway through.
140        * In practice, each block below should contain only one
141        * "return FALSE" or running through twice may not
142        * work right.
143        */
144       
145        if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
146          {
147            _dbus_verbose ("Filling in system bus address...\n");
148            
149            if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SYSTEM],
150                               "DBUS_SYSTEM_BUS_ADDRESS"))
151              return FALSE;
152          }
153
154                   
155        if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
156          {
157            /* Use default system bus address if none set in environment */
158            bus_connection_addresses[DBUS_BUS_SYSTEM] =
159              _dbus_strdup (DBUS_SYSTEM_BUS_DEFAULT_ADDRESS);
160            if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
161              return FALSE;
162            
163            _dbus_verbose ("  used default system bus \"%s\"\n",
164                           bus_connection_addresses[DBUS_BUS_SYSTEM]);
165          }
166        else
167          _dbus_verbose ("  used env var system bus \"%s\"\n",
168                         bus_connection_addresses[DBUS_BUS_SYSTEM]);
169           
170       if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
171         {
172           _dbus_verbose ("Filling in session bus address...\n");
173           
174           if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SESSION],
175                              "DBUS_SESSION_BUS_ADDRESS"))
176             return FALSE;
177           _dbus_verbose ("  \"%s\"\n", bus_connection_addresses[DBUS_BUS_SESSION] ?
178                          bus_connection_addresses[DBUS_BUS_SESSION] : "none set");
179         }
180
181       if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
182         {
183           _dbus_verbose ("Filling in activation bus address...\n");
184           
185           if (!get_from_env (&bus_connection_addresses[DBUS_BUS_STARTER],
186                              "DBUS_STARTER_ADDRESS"))
187             return FALSE;
188           
189           _dbus_verbose ("  \"%s\"\n", bus_connection_addresses[DBUS_BUS_STARTER] ?
190                          bus_connection_addresses[DBUS_BUS_STARTER] : "none set");
191         }
192
193
194       if (bus_connection_addresses[DBUS_BUS_STARTER] != NULL)
195         {
196           s = _dbus_getenv ("DBUS_STARTER_BUS_TYPE");
197               
198           if (s != NULL)
199             {
200               _dbus_verbose ("Bus activation type was set to \"%s\"\n", s);
201                   
202               if (strcmp (s, "system") == 0)
203                 activation_bus_type = DBUS_BUS_SYSTEM;
204               else if (strcmp (s, "session") == 0)
205                 activation_bus_type = DBUS_BUS_SESSION;
206             }
207         }
208       else
209         {
210           /* Default to the session bus instead if available */
211           if (bus_connection_addresses[DBUS_BUS_SESSION] != NULL)
212             {
213               bus_connection_addresses[DBUS_BUS_STARTER] =
214                 _dbus_strdup (bus_connection_addresses[DBUS_BUS_SESSION]);
215               if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
216                 return FALSE;
217             }
218         }
219       
220       /* If we return FALSE we have to be sure that restarting
221        * the above code will work right
222        */
223       
224       if (!_dbus_setenv ("DBUS_ACTIVATION_ADDRESS", NULL))
225         return FALSE;
226
227       if (!_dbus_setenv ("DBUS_ACTIVATION_BUS_TYPE", NULL))
228         return FALSE;
229       
230       if (!_dbus_register_shutdown_func (addresses_shutdown_func,
231                                          NULL))
232         return FALSE;
233       
234       initialized = TRUE;
235     }
236
237   return initialized;
238 }
239
240 static void
241 bus_data_free (void *data)
242 {
243   BusData *bd = data;
244   
245   if (bd->is_well_known)
246     {
247       int i;
248       _DBUS_LOCK (bus);
249       /* We may be stored in more than one slot */
250       i = 0;
251       while (i < N_BUS_TYPES)
252         {
253           if (bus_connections[i] == bd->connection)
254             bus_connections[i] = NULL;
255           
256           ++i;
257         }
258       _DBUS_UNLOCK (bus);
259     }
260   
261   dbus_free (bd->unique_name);
262   dbus_free (bd);
263
264   dbus_connection_free_data_slot (&bus_data_slot);
265 }
266
267 static BusData*
268 ensure_bus_data (DBusConnection *connection)
269 {
270   BusData *bd;
271
272   if (!dbus_connection_allocate_data_slot (&bus_data_slot))
273     return NULL;
274
275   bd = dbus_connection_get_data (connection, bus_data_slot);
276   if (bd == NULL)
277     {      
278       bd = dbus_new0 (BusData, 1);
279       if (bd == NULL)
280         {
281           dbus_connection_free_data_slot (&bus_data_slot);
282           return NULL;
283         }
284
285       bd->connection = connection;
286       
287       if (!dbus_connection_set_data (connection, bus_data_slot, bd,
288                                      bus_data_free))
289         {
290           dbus_free (bd);
291           dbus_connection_free_data_slot (&bus_data_slot);
292           return NULL;
293         }
294
295       /* Data slot refcount now held by the BusData */
296     }
297   else
298     {
299       dbus_connection_free_data_slot (&bus_data_slot);
300     }
301
302   return bd;
303 }
304
305 /* internal function that checks to see if this
306    is a shared bus connection and if it is unref it */
307 void
308 _dbus_bus_check_connection_and_unref (DBusConnection *connection)
309 {
310   if (bus_connections[DBUS_BUS_SYSTEM] == connection)
311     {
312       bus_connections[DBUS_BUS_SYSTEM] = NULL;
313       dbus_connection_unref (connection);
314     }
315   else if (bus_connections[DBUS_BUS_SESSION] == connection)
316     {
317       bus_connections[DBUS_BUS_SESSION] = NULL;
318       dbus_connection_unref (connection);
319     }
320 }
321
322 static DBusConnection *
323 internal_bus_get (DBusBusType  type,
324               DBusError   *error, dbus_bool_t private)
325 {
326   const char *address;
327   DBusConnection *connection;
328   BusData *bd;
329   DBusBusType address_type;
330
331   _dbus_return_val_if_fail (type >= 0 && type < N_BUS_TYPES, NULL);
332   _dbus_return_val_if_error_is_set (error, NULL);
333
334   _DBUS_LOCK (bus);
335
336   if (!init_connections_unlocked ())
337     {
338       _DBUS_UNLOCK (bus);
339       _DBUS_SET_OOM (error);
340       return NULL;
341     }
342
343   /* We want to use the activation address even if the
344    * activating bus is the session or system bus,
345    * per the spec.
346    */
347   address_type = type;
348   
349   /* Use the real type of the activation bus for getting its
350    * connection, but only if the real type's address is available. (If
351    * the activating bus isn't a well-known bus then
352    * activation_bus_type == DBUS_BUS_STARTER)
353    */
354   if (type == DBUS_BUS_STARTER &&
355       bus_connection_addresses[activation_bus_type] != NULL)
356     type = activation_bus_type;
357   
358   if (!private && bus_connections[type] != NULL)
359     {
360       connection = bus_connections[type];
361       dbus_connection_ref (connection);
362       
363       _DBUS_UNLOCK (bus);
364       return connection;
365     }
366
367   address = bus_connection_addresses[address_type];
368   if (address == NULL)
369     {
370       dbus_set_error (error, DBUS_ERROR_FAILED,
371                       "Unable to determine the address of the message bus (try 'man dbus-launch' and 'man dbus-daemon' for help)");
372       _DBUS_UNLOCK (bus);
373       return NULL;
374     }
375
376   if (private)
377     connection = dbus_connection_open_private(address, error);
378   else
379     connection = dbus_connection_open (address, error);
380   
381   if (!connection)
382     {
383       _DBUS_ASSERT_ERROR_IS_SET (error);
384       _DBUS_UNLOCK (bus);
385       return NULL;
386     }
387
388   /* By default we're bound to the lifecycle of
389    * the message bus.
390    */
391   dbus_connection_set_exit_on_disconnect (connection,
392                                           TRUE);
393   
394   if (!dbus_bus_register (connection, error))
395     {
396       _DBUS_ASSERT_ERROR_IS_SET (error);
397       dbus_connection_close (connection);
398       dbus_connection_unref (connection);
399
400       _DBUS_UNLOCK (bus);
401       return NULL;
402     }
403
404   if (!private)
405     {
406       /* get a hard ref to the connection */
407       bus_connections[type] = connection;
408       dbus_connection_ref (bus_connections[type]);
409     }
410   
411   bd = ensure_bus_data (connection);
412   _dbus_assert (bd != NULL);
413
414   bd->is_well_known = TRUE;
415
416   _DBUS_UNLOCK (bus);
417   return connection;
418 }
419
420
421 /** @} */ /* end of implementation details docs */
422
423 /**
424  * @addtogroup DBusBus
425  * @{
426  */
427
428 /**
429  * Connects to a bus daemon and registers the client with it.  If a
430  * connection to the bus already exists, then that connection is
431  * returned.  Caller owns a reference to the bus.
432  *
433  * @param type bus type
434  * @param error address where an error can be returned.
435  * @returns a DBusConnection with new ref
436  */
437 DBusConnection *
438 dbus_bus_get (DBusBusType  type,
439               DBusError   *error) {
440   return internal_bus_get(type, error, FALSE);
441 }
442
443 /**
444  * Connects to a bus daemon and registers the client with it.  Unlike
445  * dbus_bus_get(), always creates a new connection. This connection
446  * will not be saved or recycled by libdbus. Caller owns a reference
447  * to the bus.
448  *
449  * @param type bus type
450  * @param error address where an error can be returned.
451  * @returns a DBusConnection with new ref
452  */
453 DBusConnection *
454 dbus_bus_get_private (DBusBusType  type,
455               DBusError   *error) {
456   return internal_bus_get(type, error, TRUE);
457 }
458
459 /**
460  * Registers a connection with the bus. This must be the first
461  * thing an application does when connecting to the message bus.
462  * If registration succeeds, the unique name will be set,
463  * and can be obtained using dbus_bus_get_unique_name().
464  * 
465  * @param connection the connection
466  * @param error place to store errors
467  * @returns #TRUE on success
468  */
469 dbus_bool_t
470 dbus_bus_register (DBusConnection *connection,
471                    DBusError      *error)
472 {
473   DBusMessage *message, *reply;
474   char *name;
475   BusData *bd;
476   dbus_bool_t retval;
477
478   _dbus_return_val_if_fail (connection != NULL, FALSE);
479   _dbus_return_val_if_error_is_set (error, FALSE);
480
481   retval = FALSE;
482   
483   bd = ensure_bus_data (connection);
484   if (bd == NULL)
485     {
486       _DBUS_SET_OOM (error);
487       return FALSE;
488     }
489
490   if (bd->unique_name != NULL)
491     {
492       _dbus_warn ("Attempt to register the same DBusConnection with the message bus, but it is already registered\n");
493       /* This isn't an error, it's a programming bug. We'll be nice
494        * and not _dbus_assert_not_reached()
495        */
496       return TRUE;
497     }
498   
499   message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
500                                           DBUS_PATH_DBUS,
501                                           DBUS_INTERFACE_DBUS,
502                                           "Hello"); 
503
504   if (!message)
505     {
506       _DBUS_SET_OOM (error);
507       return FALSE;
508     }
509   
510   reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
511
512   dbus_message_unref (message);
513   
514   if (reply == NULL)
515     goto out;
516   else if (dbus_set_error_from_message (error, reply))
517     goto out;
518   else if (!dbus_message_get_args (reply, error,
519                                    DBUS_TYPE_STRING, &name,
520                                    DBUS_TYPE_INVALID))
521     goto out;
522   
523   bd->unique_name = _dbus_strdup (name);
524   if (bd->unique_name == NULL)
525     {
526       _DBUS_SET_OOM (error);
527       goto out;
528     }
529   
530   retval = TRUE;
531   
532  out:
533   if (reply)
534     dbus_message_unref (reply);
535
536   if (!retval)
537     _DBUS_ASSERT_ERROR_IS_SET (error);
538   
539   return retval;
540 }
541
542
543 /**
544  * Sets the unique name of the connection.  Can only be used if you
545  * registered with the bus manually (i.e. if you did not call
546  * dbus_bus_register()). Can only be called once per connection.
547  *
548  * @param connection the connection
549  * @param unique_name the unique name
550  * @returns #FALSE if not enough memory
551  */
552 dbus_bool_t
553 dbus_bus_set_unique_name (DBusConnection *connection,
554                           const char     *unique_name)
555 {
556   BusData *bd;
557
558   _dbus_return_val_if_fail (connection != NULL, FALSE);
559   _dbus_return_val_if_fail (unique_name != NULL, FALSE);
560   
561   bd = ensure_bus_data (connection);
562   if (bd == NULL)
563     return FALSE;
564
565   _dbus_assert (bd->unique_name == NULL);
566   
567   bd->unique_name = _dbus_strdup (unique_name);
568   return bd->unique_name != NULL;
569 }
570
571 /**
572  * Gets the unique name of the connection.  Only possible after the
573  * connection has been registered with the message bus.
574  *
575  * The name remains valid for the duration of the connection and
576  * should not be freed by the caller.
577  * 
578  * @param connection the connection
579  * @returns the unique name or NULL on error
580  */
581 const char*
582 dbus_bus_get_unique_name (DBusConnection *connection)
583 {
584   BusData *bd;
585
586   _dbus_return_val_if_fail (connection != NULL, NULL);
587   
588   bd = ensure_bus_data (connection);
589   if (bd == NULL)
590     return NULL;
591   
592   return bd->unique_name;
593 }
594
595 /**
596  * Asks the bus to return the uid of the named
597  * connection.
598  *
599  * @param connection the connection
600  * @param name a name owned by the connection
601  * @param error location to store the error
602  * @returns a result code, -1 if error is set
603  */ 
604 unsigned long
605 dbus_bus_get_unix_user (DBusConnection *connection,
606                         const char     *name,
607                         DBusError      *error)
608 {
609   DBusMessage *message, *reply;
610   dbus_uint32_t uid;
611
612   _dbus_return_val_if_fail (connection != NULL, DBUS_UID_UNSET);
613   _dbus_return_val_if_fail (name != NULL, DBUS_UID_UNSET);
614   _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), DBUS_UID_UNSET);
615   _dbus_return_val_if_error_is_set (error, DBUS_UID_UNSET);
616   
617   message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
618                                           DBUS_PATH_DBUS,
619                                           DBUS_INTERFACE_DBUS,
620                                           "GetConnectionUnixUser");
621
622   if (message == NULL)
623     {
624       _DBUS_SET_OOM (error);
625       return DBUS_UID_UNSET;
626     }
627  
628   if (!dbus_message_append_args (message,
629                                  DBUS_TYPE_STRING, &name,
630                                  DBUS_TYPE_INVALID))
631     {
632       dbus_message_unref (message);
633       _DBUS_SET_OOM (error);
634       return DBUS_UID_UNSET;
635     }
636   
637   reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
638                                                      error);
639   
640   dbus_message_unref (message);
641   
642   if (reply == NULL)
643     {
644       _DBUS_ASSERT_ERROR_IS_SET (error);
645       return DBUS_UID_UNSET;
646     }  
647
648   if (dbus_set_error_from_message (error, reply))
649     {
650       _DBUS_ASSERT_ERROR_IS_SET (error);
651       dbus_message_unref (reply);
652       return DBUS_UID_UNSET;
653     }
654   
655   if (!dbus_message_get_args (reply, error,
656                               DBUS_TYPE_UINT32, &uid,
657                               DBUS_TYPE_INVALID))
658     {
659       _DBUS_ASSERT_ERROR_IS_SET (error);
660       dbus_message_unref (reply);
661       return DBUS_UID_UNSET;
662     }
663
664   dbus_message_unref (reply);
665   
666   return (unsigned long) uid;
667 }
668
669
670 /**
671  * Asks the bus to assign the given name to this connection by invoking
672  * the RequestName method on the bus. This method is fully documented
673  * in the D-Bus specification. For quick reference, the flags and
674  * result codes are discussed here, but the specification is the
675  * canonical version of this information.
676  *
677  * The #DBUS_NAME_FLAG_ALLOW_REPLACEMENT flag indicates that the caller
678  * will allow other services to take over the name from the current owner.
679  *
680  * The #DBUS_NAME_FLAG_REPLACE_EXISTING flag indicates that the caller
681  * would like to take over the name from the current owner.
682  * If the current name owner did not use #DBUS_NAME_FLAG_ALLOW_REPLACEMENT
683  * then this flag indicates that the caller would like to be placed
684  * in the queue to own the name when the current owner lets go.
685  *
686  * If no flags are given, an application will receive the requested
687  * name only if the name is currently unowned; it will NOT give
688  * up the name if another application asks to take it over using
689  * #DBUS_NAME_FLAG_REPLACE_EXISTING.
690  *
691  * This function returns a result code. The possible result codes
692  * are as follows.
693  * 
694  * #DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER means that the name had no
695  * existing owner, and the caller is now the primary owner; or that
696  * the name had an owner, and the caller specified
697  * #DBUS_NAME_FLAG_REPLACE_EXISTING, and the current owner
698  * specified #DBUS_NAME_FLAG_ALLOW_REPLACEMENT.
699  *
700  * #DBUS_REQUEST_NAME_REPLY_IN_QUEUE happens only if the caller does NOT
701  * specify #DBUS_NAME_FLAG_DO_NOT_QUEUE and either the current owner
702  * did NOT specify #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or the caller did NOT
703  * specify #DBUS_NAME_FLAG_REPLACE_EXISTING. In this case the caller ends up 
704  * in a queue to own the name after the current owner gives it up.
705  *
706  * #DBUS_REQUEST_NAME_REPLY_EXISTS happens if the name has an owner
707  * already and the caller specifies #DBUS_NAME_FLAG_DO_NOT_QUEUE
708  * and either the current owner has NOT specified 
709  * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or the caller did NOT specify 
710  * #DBUS_NAME_FLAG_REPLACE_EXISTING.
711  *
712  * #DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER happens if an application
713  * requests a name it already owns.
714  *
715  * When a service represents an application, say "text editor," then
716  * it should specify #DBUS_NAME_FLAG_ALLOW_REPLACEMENT if it wants
717  * the last editor started to be the user's editor vs. the first one
718  * started.  Then any editor that can be the user's editor should
719  * specify #DBUS_NAME_FLAG_REPLACE_EXISTING to either take over
720  * (last-started-wins) or be queued up (first-started-wins) according
721  * to whether #DBUS_NAME_FLAG_ALLOW_REPLACEMENT was given.
722  * 
723  * @param connection the connection
724  * @param name the name to request
725  * @param flags flags
726  * @param error location to store the error
727  * @returns a result code, -1 if error is set
728  */ 
729 int
730 dbus_bus_request_name (DBusConnection *connection,
731                        const char     *name,
732                        unsigned int    flags,
733                        DBusError      *error)
734 {
735   DBusMessage *message, *reply;
736   dbus_uint32_t result;
737
738   _dbus_return_val_if_fail (connection != NULL, 0);
739   _dbus_return_val_if_fail (name != NULL, 0);
740   _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
741   _dbus_return_val_if_error_is_set (error, 0);
742   
743   message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
744                                           DBUS_PATH_DBUS,
745                                           DBUS_INTERFACE_DBUS,
746                                           "RequestName");
747
748   if (message == NULL)
749     {
750       _DBUS_SET_OOM (error);
751       return -1;
752     }
753  
754   if (!dbus_message_append_args (message,
755                                  DBUS_TYPE_STRING, &name,
756                                  DBUS_TYPE_UINT32, &flags,
757                                  DBUS_TYPE_INVALID))
758     {
759       dbus_message_unref (message);
760       _DBUS_SET_OOM (error);
761       return -1;
762     }
763   
764   reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
765                                                      error);
766   
767   dbus_message_unref (message);
768   
769   if (reply == NULL)
770     {
771       _DBUS_ASSERT_ERROR_IS_SET (error);
772       return -1;
773     }  
774
775   if (dbus_set_error_from_message (error, reply))
776     {
777       _DBUS_ASSERT_ERROR_IS_SET (error);
778       dbus_message_unref (reply);
779       return -1;
780     }
781   
782   if (!dbus_message_get_args (reply, error,
783                               DBUS_TYPE_UINT32, &result,
784                               DBUS_TYPE_INVALID))
785     {
786       _DBUS_ASSERT_ERROR_IS_SET (error);
787       dbus_message_unref (reply);
788       return -1;
789     }
790
791   dbus_message_unref (reply);
792   
793   return result;
794 }
795
796
797 /**
798  * Asks the bus to unassign the given name to this connection by invoking
799  * the ReleaseName method on the bus. This method is fully documented
800  * in the D-Bus specification.
801  *
802  * @param connection the connection
803  * @param name the name to remove 
804  * @param error location to store the error
805  * @returns a result code, -1 if error is set
806  */ 
807 int
808 dbus_bus_release_name (DBusConnection *connection,
809                        const char     *name,
810                        DBusError      *error)
811 {
812   DBusMessage *message, *reply;
813   dbus_uint32_t result;
814
815   _dbus_return_val_if_fail (connection != NULL, 0);
816   _dbus_return_val_if_fail (name != NULL, 0);
817   _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
818   _dbus_return_val_if_error_is_set (error, 0);
819
820   message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
821                                           DBUS_PATH_DBUS,
822                                           DBUS_INTERFACE_DBUS,
823                                           "ReleaseName");
824
825   if (message == NULL)
826     {
827       _DBUS_SET_OOM (error);
828       return -1;
829     }
830
831   if (!dbus_message_append_args (message,
832                                  DBUS_TYPE_STRING, &name,
833                                  DBUS_TYPE_INVALID))
834     {
835       dbus_message_unref (message);
836       _DBUS_SET_OOM (error);
837       return -1;
838     }
839
840   reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
841                                                      error);
842
843   dbus_message_unref (message);
844
845   if (reply == NULL)
846     {
847       _DBUS_ASSERT_ERROR_IS_SET (error);
848       return -1;
849     }
850
851   if (dbus_set_error_from_message (error, reply))
852     {
853       _DBUS_ASSERT_ERROR_IS_SET (error);
854       dbus_message_unref (reply);
855       return -1;
856     }
857
858   if (!dbus_message_get_args (reply, error,
859                               DBUS_TYPE_UINT32, &result,
860                               DBUS_TYPE_INVALID))
861     {
862       _DBUS_ASSERT_ERROR_IS_SET (error);
863       dbus_message_unref (reply);
864       return -1;
865     }
866
867   dbus_message_unref (reply);
868
869   return result;
870 }
871
872 /**
873  * Checks whether a certain name has an owner.
874  *
875  * @param connection the connection
876  * @param name the name
877  * @param error location to store any errors
878  * @returns #TRUE if the name exists, #FALSE if not or on error
879  */
880 dbus_bool_t
881 dbus_bus_name_has_owner (DBusConnection *connection,
882                          const char     *name,
883                          DBusError      *error)
884 {
885   DBusMessage *message, *reply;
886   dbus_bool_t exists;
887
888   _dbus_return_val_if_fail (connection != NULL, FALSE);
889   _dbus_return_val_if_fail (name != NULL, FALSE);
890   _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
891   _dbus_return_val_if_error_is_set (error, FALSE);
892   
893   message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
894                                           DBUS_PATH_DBUS,
895                                           DBUS_INTERFACE_DBUS,
896                                           "NameHasOwner");
897   if (message == NULL)
898     {
899       _DBUS_SET_OOM (error);
900       return FALSE;
901     }
902   
903   if (!dbus_message_append_args (message,
904                                  DBUS_TYPE_STRING, &name,
905                                  DBUS_TYPE_INVALID))
906     {
907       dbus_message_unref (message);
908       _DBUS_SET_OOM (error);
909       return FALSE;
910     }
911   
912   reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
913   dbus_message_unref (message);
914
915   if (reply == NULL)
916     {
917       _DBUS_ASSERT_ERROR_IS_SET (error);
918       return FALSE;
919     }
920
921   if (!dbus_message_get_args (reply, error,
922                               DBUS_TYPE_BOOLEAN, &exists,
923                               DBUS_TYPE_INVALID))
924     {
925       _DBUS_ASSERT_ERROR_IS_SET (error);
926       dbus_message_unref (reply);
927       return FALSE;
928     }
929   
930   dbus_message_unref (reply);
931   return exists;
932 }
933
934 /**
935  * Starts a service that will request ownership of the given name.
936  * The returned result will be one of be one of
937  * #DBUS_START_REPLY_SUCCESS or #DBUS_START_REPLY_ALREADY_RUNNING if
938  * successful.  Pass #NULL if you don't care about the result.
939  * 
940  * The flags parameter is for future expansion, currently you should
941  * specify 0.
942  *
943  * @param connection the connection
944  * @param name the name we want the new service to request
945  * @param flags the flags (should always be 0 for now)
946  * @param result a place to store the result or #NULL
947  * @param error location to store any errors
948  * @returns #TRUE if the activation succeeded, #FALSE if not
949  */
950 dbus_bool_t
951 dbus_bus_start_service_by_name (DBusConnection *connection,
952                                 const char     *name,
953                                 dbus_uint32_t   flags,
954                                 dbus_uint32_t  *result,
955                                 DBusError      *error)
956 {
957   DBusMessage *msg;
958   DBusMessage *reply;
959
960   _dbus_return_val_if_fail (connection != NULL, FALSE);
961   _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
962   
963   msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
964                                       DBUS_PATH_DBUS,
965                                       DBUS_INTERFACE_DBUS,
966                                       "StartServiceByName");
967
968   if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &name,
969                                  DBUS_TYPE_UINT32, &flags, DBUS_TYPE_INVALID))
970     {
971       dbus_message_unref (msg);
972       _DBUS_SET_OOM (error);
973       return FALSE;
974     }
975
976   reply = dbus_connection_send_with_reply_and_block (connection, msg,
977                                                      -1, error);
978   dbus_message_unref (msg);
979
980   if (reply == NULL)
981     {
982       _DBUS_ASSERT_ERROR_IS_SET (error);
983       return FALSE;
984     }
985
986   if (dbus_set_error_from_message (error, reply))
987     {
988       _DBUS_ASSERT_ERROR_IS_SET (error);
989       dbus_message_unref (reply);
990       return FALSE;
991     }
992
993   if (result != NULL &&
994       !dbus_message_get_args (reply, error, DBUS_TYPE_UINT32,
995                               result, DBUS_TYPE_INVALID))
996     {
997       _DBUS_ASSERT_ERROR_IS_SET (error);
998       dbus_message_unref (reply);
999       return FALSE;
1000     }
1001   
1002   dbus_message_unref (reply);
1003   return TRUE;
1004 }
1005
1006 static void
1007 send_no_return_values (DBusConnection *connection,
1008                        DBusMessage    *msg,
1009                        DBusError      *error)
1010 {
1011   if (error)
1012     {
1013       /* Block to check success codepath */
1014       DBusMessage *reply;
1015       
1016       reply = dbus_connection_send_with_reply_and_block (connection, msg,
1017                                                          -1, error);
1018       
1019       if (reply == NULL)
1020         _DBUS_ASSERT_ERROR_IS_SET (error);
1021       else
1022         dbus_message_unref (reply);
1023     }
1024   else
1025     {
1026       /* Silently-fail nonblocking codepath */
1027       dbus_message_set_no_reply (msg, TRUE);
1028       dbus_connection_send (connection, msg, NULL);
1029     }
1030 }
1031
1032 /**
1033  * Adds a match rule to match messages going through the message bus.
1034  * The "rule" argument is the string form of a match rule.
1035  *
1036  * If you pass #NULL for the error, this function will not
1037  * block; the match thus won't be added until you flush the
1038  * connection, and if there's an error adding the match
1039  * (only possible error is lack of resources in the bus),
1040  * you won't find out about it.
1041  *
1042  * If you pass non-#NULL for the error this function will
1043  * block until it gets a reply.
1044  *
1045  * Normal API conventions would have the function return
1046  * a boolean value indicating whether the error was set,
1047  * but that would require blocking always to determine
1048  * the return value.
1049  *
1050  * The AddMatch method is fully documented in the D-Bus 
1051  * specification. For quick reference, the format of the 
1052  * match rules is discussed here, but the specification 
1053  * is the canonical version of this information.
1054  *
1055  * Rules are specified as a string of comma separated 
1056  * key/value pairs. An example is 
1057  * "type='signal',sender='org.freedesktop.DBus',
1058  * interface='org.freedesktop.DBus',member='Foo',
1059  * path='/bar/foo',destination=':452345.34'"
1060  *
1061  * Possible keys you can match on are type, sender, 
1062  * interface, member, path, destination and the special
1063  * arg keys.  Excluding a key from the rule indicates 
1064  * a wildcard match.  For instance excluding the
1065  * the member from a match rule but adding a sender would
1066  * let all messages from that sender through.  
1067  *
1068  * Matches are inclusive not exclusive so as long as one 
1069  * rule matches the message will get through.  It is important
1070  * to note this because every time a message is received the 
1071  * application will be paged into memory to process it.  This
1072  * can cause performance problems such as draining batteries
1073  * on embedded platforms.
1074  *
1075  * The special arg keys are used for further restricting the 
1076  * match based on the parameters sent by the signal or method.
1077  * For instance arg1='foo' will check the first argument, 
1078  * arg2='bar' the second and so on.  For performance reasons
1079  * there is a set limit on the highest number parameter that
1080  * can be checked which is set in dbus-protocol.h
1081  *
1082  * @param connection connection to the message bus
1083  * @param rule textual form of match rule
1084  * @param error location to store any errors
1085  */
1086 void
1087 dbus_bus_add_match (DBusConnection *connection,
1088                     const char     *rule,
1089                     DBusError      *error)
1090 {
1091   DBusMessage *msg;
1092
1093   _dbus_return_if_fail (rule != NULL);
1094
1095   msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1096                                       DBUS_PATH_DBUS,
1097                                       DBUS_INTERFACE_DBUS,
1098                                       "AddMatch");
1099
1100   if (msg == NULL)
1101     {
1102       _DBUS_SET_OOM (error);
1103       return;
1104     }
1105
1106   if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
1107                                  DBUS_TYPE_INVALID))
1108     {
1109       dbus_message_unref (msg);
1110       _DBUS_SET_OOM (error);
1111       return;
1112     }
1113
1114   send_no_return_values (connection, msg, error);
1115
1116   dbus_message_unref (msg);
1117 }
1118
1119 /**
1120  * Removes a previously-added match rule "by value" (the most
1121  * recently-added identical rule gets removed).  The "rule" argument
1122  * is the string form of a match rule.
1123  *
1124  * If you pass #NULL for the error, this function will not
1125  * block; otherwise it will. See detailed explanation in
1126  * docs for dbus_bus_add_match().
1127  * 
1128  * @param connection connection to the message bus
1129  * @param rule textual form of match rule
1130  * @param error location to store any errors
1131  */
1132 void
1133 dbus_bus_remove_match (DBusConnection *connection,
1134                        const char     *rule,
1135                        DBusError      *error)
1136 {
1137   DBusMessage *msg;
1138
1139   _dbus_return_if_fail (rule != NULL);
1140   
1141   msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1142                                       DBUS_PATH_DBUS,
1143                                       DBUS_INTERFACE_DBUS,
1144                                       "RemoveMatch");
1145
1146   if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
1147                                  DBUS_TYPE_INVALID))
1148     {
1149       dbus_message_unref (msg);
1150       _DBUS_SET_OOM (error);
1151       return;
1152     }
1153
1154   send_no_return_values (connection, msg, error);
1155
1156   dbus_message_unref (msg);
1157 }
1158
1159 /** @} */