1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-bus.c Convenience functions for communicating with the bus.
4 * Copyright (C) 2003 CodeFactory AB
5 * Copyright (C) 2003 Red Hat, Inc.
7 * Licensed under the Academic Free License version 2.1
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.
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.
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
26 #include "dbus-protocol.h"
27 #include "dbus-internals.h"
28 #include "dbus-message.h"
29 #include "dbus-marshal-validate.h"
30 #include "dbus-threads-internal.h"
31 #include "dbus-connection-internal.h"
35 * @defgroup DBusBus Message bus APIs
37 * @brief Functions for communicating with the message bus
39 * dbus_bus_get() allows all modules and libraries in a given
40 * process to share the same connection to the bus daemon by storing
41 * the connection globally.
43 * All other functions in this module are just convenience functions;
44 * most of them invoke methods on the bus daemon, by sending method
45 * call messages to #DBUS_SERVICE_DBUS. These convenience functions
46 * often make blocking method calls. If you don't want to block,
47 * you can send the method call messages manually in the same way
48 * you would any other method call message.
50 * This module is the only one in libdbus that's specific to
51 * communicating with the message bus daemon. The rest of the API can
52 * also be used for connecting to another application directly.
54 * @todo right now the default address of the system bus is hardcoded,
55 * so if you change it in the global config file suddenly you have to
56 * set DBUS_SYSTEM_BUS_ADDRESS env variable. Might be nice if the
57 * client lib somehow read the config file, or if the bus on startup
58 * somehow wrote out its address to a well-known spot, but might also
63 * @defgroup DBusBusInternals Message bus APIs internals
64 * @ingroup DBusInternals
65 * @brief Internals of functions for communicating with the message bus
71 * Block of message-bus-related data we attach to each
72 * #DBusConnection used with these convenience functions.
77 DBusConnection *connection; /**< Connection we're associated with */
78 char *unique_name; /**< Unique name of this connection */
80 unsigned int is_well_known : 1; /**< Is one of the well-known connections in our global array */
83 /** The slot we have reserved to store BusData.
85 static dbus_int32_t bus_data_slot = -1;
87 /** Number of bus types */
90 static DBusConnection *bus_connections[N_BUS_TYPES];
91 static char *bus_connection_addresses[N_BUS_TYPES] = { NULL, NULL, NULL };
93 static DBusBusType activation_bus_type = DBUS_BUS_STARTER;
95 static dbus_bool_t initialized = FALSE;
98 * Lock for globals in this file
100 _DBUS_DEFINE_GLOBAL_LOCK (bus);
103 * Global lock covering all BusData on any connection. The bet is
104 * that some lock contention is better than more memory
105 * for a per-connection lock, but it's tough to imagine it mattering
108 _DBUS_DEFINE_GLOBAL_LOCK (bus_datas);
111 addresses_shutdown_func (void *data)
116 while (i < N_BUS_TYPES)
118 if (bus_connections[i] != NULL)
119 _dbus_warn_check_failed ("dbus_shutdown() called but connections were still live. This probably means the application did not drop all its references to bus connections.\n");
121 dbus_free (bus_connection_addresses[i]);
122 bus_connection_addresses[i] = NULL;
126 activation_bus_type = DBUS_BUS_STARTER;
130 get_from_env (char **connection_p,
135 _dbus_assert (*connection_p == NULL);
137 s = _dbus_getenv (env_var);
138 if (s == NULL || *s == '\0')
139 return TRUE; /* successfully didn't use the env var */
142 *connection_p = _dbus_strdup (s);
143 return *connection_p != NULL;
148 init_connections_unlocked (void)
156 while (i < N_BUS_TYPES)
158 bus_connections[i] = NULL;
162 /* Don't init these twice, we may run this code twice if
163 * init_connections_unlocked() fails midway through.
164 * In practice, each block below should contain only one
165 * "return FALSE" or running through twice may not
169 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
171 _dbus_verbose ("Filling in system bus address...\n");
173 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SYSTEM],
174 "DBUS_SYSTEM_BUS_ADDRESS"))
179 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
181 /* Use default system bus address if none set in environment */
182 bus_connection_addresses[DBUS_BUS_SYSTEM] =
183 _dbus_strdup (DBUS_SYSTEM_BUS_DEFAULT_ADDRESS);
185 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
188 _dbus_verbose (" used default system bus \"%s\"\n",
189 bus_connection_addresses[DBUS_BUS_SYSTEM]);
192 _dbus_verbose (" used env var system bus \"%s\"\n",
193 bus_connection_addresses[DBUS_BUS_SYSTEM]);
195 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
197 _dbus_verbose ("Filling in session bus address...\n");
199 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SESSION],
200 "DBUS_SESSION_BUS_ADDRESS"))
203 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
204 bus_connection_addresses[DBUS_BUS_SESSION] =
205 _dbus_strdup (DBUS_SESSION_BUS_DEFAULT_ADDRESS);
207 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
210 _dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_SESSION] ?
211 bus_connection_addresses[DBUS_BUS_SESSION] : "none set");
214 if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
216 _dbus_verbose ("Filling in activation bus address...\n");
218 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_STARTER],
219 "DBUS_STARTER_ADDRESS"))
222 _dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_STARTER] ?
223 bus_connection_addresses[DBUS_BUS_STARTER] : "none set");
227 if (bus_connection_addresses[DBUS_BUS_STARTER] != NULL)
229 s = _dbus_getenv ("DBUS_STARTER_BUS_TYPE");
233 _dbus_verbose ("Bus activation type was set to \"%s\"\n", s);
235 if (strcmp (s, "system") == 0)
236 activation_bus_type = DBUS_BUS_SYSTEM;
237 else if (strcmp (s, "session") == 0)
238 activation_bus_type = DBUS_BUS_SESSION;
243 /* Default to the session bus instead if available */
244 if (bus_connection_addresses[DBUS_BUS_SESSION] != NULL)
246 bus_connection_addresses[DBUS_BUS_STARTER] =
247 _dbus_strdup (bus_connection_addresses[DBUS_BUS_SESSION]);
248 if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
253 /* If we return FALSE we have to be sure that restarting
254 * the above code will work right
257 if (!_dbus_setenv ("DBUS_ACTIVATION_ADDRESS", NULL))
260 if (!_dbus_setenv ("DBUS_ACTIVATION_BUS_TYPE", NULL))
263 if (!_dbus_register_shutdown_func (addresses_shutdown_func,
274 bus_data_free (void *data)
278 if (bd->is_well_known)
282 /* We may be stored in more than one slot */
283 /* This should now be impossible - these slots are supposed to
284 * be cleared on disconnect, so should not need to be cleared on
288 while (i < N_BUS_TYPES)
290 if (bus_connections[i] == bd->connection)
291 bus_connections[i] = NULL;
298 dbus_free (bd->unique_name);
301 dbus_connection_free_data_slot (&bus_data_slot);
305 ensure_bus_data (DBusConnection *connection)
309 if (!dbus_connection_allocate_data_slot (&bus_data_slot))
312 bd = dbus_connection_get_data (connection, bus_data_slot);
315 bd = dbus_new0 (BusData, 1);
318 dbus_connection_free_data_slot (&bus_data_slot);
322 bd->connection = connection;
324 if (!dbus_connection_set_data (connection, bus_data_slot, bd,
328 dbus_connection_free_data_slot (&bus_data_slot);
332 /* Data slot refcount now held by the BusData */
336 dbus_connection_free_data_slot (&bus_data_slot);
343 * Internal function that checks to see if this
344 * is a shared connection owned by the bus and if it is unref it.
346 * @param connection a connection that has been disconnected.
349 _dbus_bus_notify_shared_connection_disconnected_unlocked (DBusConnection *connection)
355 /* We are expecting to have the connection saved in only one of these
356 * slots, but someone could in a pathological case set system and session
357 * bus to the same bus or something. Or set one of them to the starter
358 * bus without setting the starter bus type in the env variable.
359 * So we don't break the loop as soon as we find a match.
361 for (i = 0; i < N_BUS_TYPES; ++i)
363 if (bus_connections[i] == connection)
365 bus_connections[i] = NULL;
372 static DBusConnection *
373 internal_bus_get (DBusBusType type,
378 DBusConnection *connection;
380 DBusBusType address_type;
382 _dbus_return_val_if_fail (type >= 0 && type < N_BUS_TYPES, NULL);
383 _dbus_return_val_if_error_is_set (error, NULL);
387 if (!init_connections_unlocked ())
390 _DBUS_SET_OOM (error);
394 /* We want to use the activation address even if the
395 * activating bus is the session or system bus,
400 /* Use the real type of the activation bus for getting its
401 * connection, but only if the real type's address is available. (If
402 * the activating bus isn't a well-known bus then
403 * activation_bus_type == DBUS_BUS_STARTER)
405 if (type == DBUS_BUS_STARTER &&
406 bus_connection_addresses[activation_bus_type] != NULL)
407 type = activation_bus_type;
409 if (!private && bus_connections[type] != NULL)
411 connection = bus_connections[type];
412 dbus_connection_ref (connection);
418 address = bus_connection_addresses[address_type];
421 dbus_set_error (error, DBUS_ERROR_FAILED,
422 "Unable to determine the address of the message bus (try 'man dbus-launch' and 'man dbus-daemon' for help)");
428 connection = dbus_connection_open_private (address, error);
430 connection = dbus_connection_open (address, error);
434 _DBUS_ASSERT_ERROR_IS_SET (error);
439 /* By default we're bound to the lifecycle of
442 dbus_connection_set_exit_on_disconnect (connection,
445 if (!dbus_bus_register (connection, error))
447 _DBUS_ASSERT_ERROR_IS_SET (error);
448 _dbus_connection_close_possibly_shared (connection);
449 dbus_connection_unref (connection);
457 /* store a weak ref to the connection (dbus-connection.c is
458 * supposed to have a strong ref that it drops on disconnect,
459 * since this is a shared connection)
461 bus_connections[type] = connection;
464 _DBUS_LOCK (bus_datas);
465 bd = ensure_bus_data (connection);
466 _dbus_assert (bd != NULL); /* it should have been created on
467 register, so OOM not possible */
468 bd->is_well_known = TRUE;
469 _DBUS_UNLOCK (bus_datas);
474 /* Return a reference to the caller */
479 /** @} */ /* end of implementation details docs */
482 * @addtogroup DBusBus
487 * Connects to a bus daemon and registers the client with it. If a
488 * connection to the bus already exists, then that connection is
489 * returned. The caller of this function owns a reference to the bus.
491 * The caller may NOT call dbus_connection_close() on this connection;
492 * see dbus_connection_open() and dbus_connection_close() for details
495 * If this function obtains a new connection object never before
496 * returned from dbus_bus_get(), it will call
497 * dbus_connection_set_exit_on_disconnect(), so the application
498 * will exit if the connection closes. You can undo this
499 * by calling dbus_connection_set_exit_on_disconnect() yourself
500 * after you get the connection.
502 * dbus_bus_get() calls dbus_bus_register() for you.
504 * If returning a newly-created connection, this function will block
505 * until authentication and bus registration are complete.
507 * @param type bus type
508 * @param error address where an error can be returned.
509 * @returns a #DBusConnection with new ref
512 dbus_bus_get (DBusBusType type,
515 return internal_bus_get (type, FALSE, error);
519 * Connects to a bus daemon and registers the client with it as with
520 * dbus_bus_register(). Unlike dbus_bus_get(), always creates a new
521 * connection. This connection will not be saved or recycled by
522 * libdbus. Caller owns a reference to the bus and must either close
523 * it or know it to be closed prior to releasing this reference.
525 * See dbus_connection_open_private() for more details on when to
526 * close and unref this connection.
528 * This function calls
529 * dbus_connection_set_exit_on_disconnect() on the new connection, so the application
530 * will exit if the connection closes. You can undo this
531 * by calling dbus_connection_set_exit_on_disconnect() yourself
532 * after you get the connection.
534 * dbus_bus_get_private() calls dbus_bus_register() for you.
536 * This function will block until authentication and bus registration
539 * @param type bus type
540 * @param error address where an error can be returned.
541 * @returns a DBusConnection with new ref
544 dbus_bus_get_private (DBusBusType type,
547 return internal_bus_get (type, TRUE, error);
551 * Registers a connection with the bus. This must be the first
552 * thing an application does when connecting to the message bus.
553 * If registration succeeds, the unique name will be set,
554 * and can be obtained using dbus_bus_get_unique_name().
556 * This function will block until registration is complete.
558 * If the connection has already registered with the bus
559 * (determined by checking whether dbus_bus_get_unique_name()
560 * returns a non-#NULL value), then this function does nothing.
562 * If you use dbus_bus_get() or dbus_bus_get_private() this
563 * function will be called for you.
565 * @note Just use dbus_bus_get() or dbus_bus_get_private() instead of
566 * dbus_bus_register() and save yourself some pain. Using
567 * dbus_bus_register() manually is only useful if you have your
568 * own custom message bus not found in #DBusBusType.
570 * If you open a bus connection with dbus_connection_open() or
571 * dbus_connection_open_private() you will have to dbus_bus_register()
572 * yourself, or make the appropriate registration method calls
573 * yourself. If you send the method calls yourself, call
574 * dbus_bus_set_unique_name() with the unique bus name you get from
577 * For shared connections (created with dbus_connection_open()) in a
578 * multithreaded application, you can't really make the registration
579 * calls yourself, because you don't know whether some other thread is
580 * also registering, and the bus will kick you off if you send two
581 * registration messages.
583 * If you use dbus_bus_register() however, there is a lock that
584 * keeps both apps from registering at the same time.
586 * The rule in a multithreaded app, then, is that dbus_bus_register()
587 * must be used to register, or you need to have your own locks that
588 * all threads in the app will respect.
590 * In a single-threaded application you can register by hand instead
591 * of using dbus_bus_register(), as long as you check
592 * dbus_bus_get_unique_name() to see if a unique name has already been
593 * stored by another thread before you send the registration messages.
595 * @param connection the connection
596 * @param error place to store errors
597 * @returns #TRUE on success
600 dbus_bus_register (DBusConnection *connection,
603 DBusMessage *message, *reply;
608 _dbus_return_val_if_fail (connection != NULL, FALSE);
609 _dbus_return_val_if_error_is_set (error, FALSE);
613 _DBUS_LOCK (bus_datas);
615 bd = ensure_bus_data (connection);
618 _DBUS_SET_OOM (error);
619 _DBUS_UNLOCK (bus_datas);
623 if (bd->unique_name != NULL)
625 _dbus_verbose ("Ignoring attempt to register the same DBusConnection %s with the message bus a second time.\n",
627 _DBUS_UNLOCK (bus_datas);
633 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
640 _DBUS_SET_OOM (error);
642 _DBUS_UNLOCK (bus_datas);
646 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
648 dbus_message_unref (message);
652 else if (dbus_set_error_from_message (error, reply))
654 else if (!dbus_message_get_args (reply, error,
655 DBUS_TYPE_STRING, &name,
659 bd->unique_name = _dbus_strdup (name);
660 if (bd->unique_name == NULL)
662 _DBUS_SET_OOM (error);
670 dbus_message_unref (reply);
673 _DBUS_ASSERT_ERROR_IS_SET (error);
675 _DBUS_UNLOCK (bus_datas);
682 * Sets the unique name of the connection, as assigned by the message
683 * bus. Can only be used if you registered with the bus manually
684 * (i.e. if you did not call dbus_bus_register()). Can only be called
685 * once per connection. After the unique name is set, you can get it
686 * with dbus_bus_get_unique_name().
688 * The only reason to use this function is to re-implement the
689 * equivalent of dbus_bus_register() yourself. One (probably unusual)
690 * reason to do that might be to do the bus registration call
691 * asynchronously instead of synchronously.
693 * @note Just use dbus_bus_get() or dbus_bus_get_private(), or worst
694 * case dbus_bus_register(), instead of messing with this
695 * function. There's really no point creating pain for yourself by
696 * doing things manually.
698 * It's hard to use this function safely on shared connections
699 * (created by dbus_connection_open()) in a multithreaded application,
700 * because only one registration attempt can be sent to the bus. If
701 * two threads are both sending the registration message, there is no
702 * mechanism in libdbus itself to avoid sending it twice.
704 * Thus, you need a way to coordinate which thread sends the
705 * registration attempt; which also means you know which thread
706 * will call dbus_bus_set_unique_name(). If you don't know
707 * about all threads in the app (for example, if some libraries
708 * you're using might start libdbus-using threads), then you
709 * need to avoid using this function on shared connections.
711 * @param connection the connection
712 * @param unique_name the unique name
713 * @returns #FALSE if not enough memory
716 dbus_bus_set_unique_name (DBusConnection *connection,
717 const char *unique_name)
722 _dbus_return_val_if_fail (connection != NULL, FALSE);
723 _dbus_return_val_if_fail (unique_name != NULL, FALSE);
725 _DBUS_LOCK (bus_datas);
727 bd = ensure_bus_data (connection);
731 _dbus_assert (bd->unique_name == NULL);
733 bd->unique_name = _dbus_strdup (unique_name);
734 success = bd->unique_name != NULL;
736 _DBUS_UNLOCK (bus_datas);
742 * Gets the unique name of the connection as assigned by the message
743 * bus. Only possible after the connection has been registered with
744 * the message bus. All connections returned by dbus_bus_get() or
745 * dbus_bus_get_private() have been successfully registered.
747 * The name remains valid until the connection is freed, and
748 * should not be freed by the caller.
750 * Other than dbus_bus_get(), there are two ways to set the unique
751 * name; one is dbus_bus_register(), the other is
752 * dbus_bus_set_unique_name(). You are responsible for calling
753 * dbus_bus_set_unique_name() if you register by hand instead of using
754 * dbus_bus_register().
756 * @param connection the connection
757 * @returns the unique name or #NULL on error
760 dbus_bus_get_unique_name (DBusConnection *connection)
763 const char *unique_name;
765 _dbus_return_val_if_fail (connection != NULL, NULL);
767 _DBUS_LOCK (bus_datas);
769 bd = ensure_bus_data (connection);
773 unique_name = bd->unique_name;
775 _DBUS_UNLOCK (bus_datas);
781 * Asks the bus to return the UID the named connection authenticated
782 * as, if any. Only works on UNIX; only works for connections on the
783 * same machine as the bus. If you are not on the same machine as the
784 * bus, then calling this is probably a bad idea, since the UID will
785 * mean little to your application.
787 * For the system message bus you're guaranteed to be on the same
788 * machine since it only listens on a UNIX domain socket (at least,
789 * as shipped by default).
791 * This function only works for connections that authenticated as
792 * a UNIX user, right now that includes all bus connections, but
793 * it's very possible to have connections with no associated UID.
794 * So check for errors and do something sensible if they happen.
796 * This function will always return an error on Windows.
798 * @param connection the connection
799 * @param name a name owned by the connection
800 * @param error location to store the error
801 * @returns the unix user id, or ((unsigned)-1) if error is set
804 dbus_bus_get_unix_user (DBusConnection *connection,
808 DBusMessage *message, *reply;
811 _dbus_return_val_if_fail (connection != NULL, DBUS_UID_UNSET);
812 _dbus_return_val_if_fail (name != NULL, DBUS_UID_UNSET);
813 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), DBUS_UID_UNSET);
814 _dbus_return_val_if_error_is_set (error, DBUS_UID_UNSET);
816 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
819 "GetConnectionUnixUser");
823 _DBUS_SET_OOM (error);
824 return DBUS_UID_UNSET;
827 if (!dbus_message_append_args (message,
828 DBUS_TYPE_STRING, &name,
831 dbus_message_unref (message);
832 _DBUS_SET_OOM (error);
833 return DBUS_UID_UNSET;
836 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
839 dbus_message_unref (message);
843 _DBUS_ASSERT_ERROR_IS_SET (error);
844 return DBUS_UID_UNSET;
847 if (dbus_set_error_from_message (error, reply))
849 _DBUS_ASSERT_ERROR_IS_SET (error);
850 dbus_message_unref (reply);
851 return DBUS_UID_UNSET;
854 if (!dbus_message_get_args (reply, error,
855 DBUS_TYPE_UINT32, &uid,
858 _DBUS_ASSERT_ERROR_IS_SET (error);
859 dbus_message_unref (reply);
860 return DBUS_UID_UNSET;
863 dbus_message_unref (reply);
865 return (unsigned long) uid;
870 * Asks the bus to assign the given name to this connection by invoking
871 * the RequestName method on the bus. This method is fully documented
872 * in the D-Bus specification. For quick reference, the flags and
873 * result codes are discussed here, but the specification is the
874 * canonical version of this information.
876 * First you should know that for each bus name, the bus stores
877 * a queue of connections that would like to own it. Only
878 * one owns it at a time - called the primary owner. If the primary
879 * owner releases the name or disconnects, then the next owner in the
880 * queue atomically takes over.
882 * So for example if you have an application org.freedesktop.TextEditor
883 * and multiple instances of it can be run, you can have all of them
884 * sitting in the queue. The first one to start up will receive messages
885 * sent to org.freedesktop.TextEditor, but if that one exits another
886 * will become the primary owner and receive messages.
888 * The queue means you don't need to manually watch for the current owner to
889 * disappear and then request the name again.
891 * When requesting a name, you can specify several flags.
893 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT and #DBUS_NAME_FLAG_DO_NOT_QUEUE
894 * are properties stored by the bus for this connection with respect to
895 * each requested bus name. These properties are stored even if the
896 * connection is queued and does not become the primary owner.
897 * You can update these flags by calling RequestName again (even if
898 * you already own the name).
900 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT means that another requestor of the
901 * name can take it away from you by specifying #DBUS_NAME_FLAG_REPLACE_EXISTING.
903 * #DBUS_NAME_FLAG_DO_NOT_QUEUE means that if you aren't the primary owner,
904 * you don't want to be queued up - you only care about being the
907 * Unlike the other two flags, #DBUS_NAME_FLAG_REPLACE_EXISTING is a property
908 * of the individual RequestName call, i.e. the bus does not persistently
909 * associate it with the connection-name pair. If a RequestName call includes
910 * the #DBUS_NAME_FLAG_REPLACE_EXISTING flag, and the current primary
911 * owner has #DBUS_NAME_FLAG_ALLOW_REPLACEMENT set, then the current primary
912 * owner will be kicked off.
914 * If no flags are given, an application will receive the requested
915 * name only if the name is currently unowned; and it will NOT give
916 * up the name if another application asks to take it over using
917 * #DBUS_NAME_FLAG_REPLACE_EXISTING.
919 * This function returns a result code. The possible result codes
922 * #DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER means that the name had no
923 * existing owner, and the caller is now the primary owner; or that
924 * the name had an owner, and the caller specified
925 * #DBUS_NAME_FLAG_REPLACE_EXISTING, and the current owner
926 * specified #DBUS_NAME_FLAG_ALLOW_REPLACEMENT.
928 * #DBUS_REQUEST_NAME_REPLY_IN_QUEUE happens only if the caller does NOT
929 * specify #DBUS_NAME_FLAG_DO_NOT_QUEUE and either the current owner
930 * did NOT specify #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or the caller did NOT
931 * specify #DBUS_NAME_FLAG_REPLACE_EXISTING. In this case the caller ends up
932 * in a queue to own the name after the current owner gives it up.
934 * #DBUS_REQUEST_NAME_REPLY_EXISTS happens if the name has an owner
935 * already and the caller specifies #DBUS_NAME_FLAG_DO_NOT_QUEUE
936 * and either the current owner has NOT specified
937 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or the caller did NOT specify
938 * #DBUS_NAME_FLAG_REPLACE_EXISTING.
940 * #DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER happens if an application
941 * requests a name it already owns. (Re-requesting a name is useful if
942 * you want to change the #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or
943 * #DBUS_NAME_FLAG_DO_NOT_QUEUE settings.)
945 * When a service represents an application, say "text editor," then
946 * it should specify #DBUS_NAME_FLAG_ALLOW_REPLACEMENT if it wants
947 * the last editor started to be the user's editor vs. the first one
948 * started. Then any editor that can be the user's editor should
949 * specify #DBUS_NAME_FLAG_REPLACE_EXISTING to either take over
950 * (last-started-wins) or be queued up (first-started-wins) according
951 * to whether #DBUS_NAME_FLAG_ALLOW_REPLACEMENT was given.
953 * Conventionally, single-instance applications often offer a command
954 * line option called --replace which means to replace the current
955 * instance. To implement this, always set
956 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT when you request your
957 * application's bus name. When you lose ownership of your bus name,
958 * you need to exit. Look for the signal "NameLost" from
959 * #DBUS_SERVICE_DBUS and #DBUS_INTERFACE_DBUS (the signal's first
960 * argument is the bus name that was lost). If starting up without
961 * --replace, do not specify #DBUS_NAME_FLAG_REPLACE_EXISTING, and
962 * exit if you fail to become the bus name owner. If --replace is
963 * given, ask to replace the old owner.
965 * @param connection the connection
966 * @param name the name to request
968 * @param error location to store the error
969 * @returns a result code, -1 if error is set
972 dbus_bus_request_name (DBusConnection *connection,
977 DBusMessage *message, *reply;
978 dbus_uint32_t result;
980 _dbus_return_val_if_fail (connection != NULL, 0);
981 _dbus_return_val_if_fail (name != NULL, 0);
982 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
983 _dbus_return_val_if_error_is_set (error, 0);
985 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
992 _DBUS_SET_OOM (error);
996 if (!dbus_message_append_args (message,
997 DBUS_TYPE_STRING, &name,
998 DBUS_TYPE_UINT32, &flags,
1001 dbus_message_unref (message);
1002 _DBUS_SET_OOM (error);
1006 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
1009 dbus_message_unref (message);
1013 _DBUS_ASSERT_ERROR_IS_SET (error);
1017 if (dbus_set_error_from_message (error, reply))
1019 _DBUS_ASSERT_ERROR_IS_SET (error);
1020 dbus_message_unref (reply);
1024 if (!dbus_message_get_args (reply, error,
1025 DBUS_TYPE_UINT32, &result,
1028 _DBUS_ASSERT_ERROR_IS_SET (error);
1029 dbus_message_unref (reply);
1033 dbus_message_unref (reply);
1040 * Asks the bus to unassign the given name from this connection by
1041 * invoking the ReleaseName method on the bus. The "ReleaseName"
1042 * method is canonically documented in the D-Bus specification.
1044 * Possible results are: #DBUS_RELEASE_NAME_REPLY_RELEASED
1045 * which means you owned the name or were in the queue to own it,
1046 * and and now you don't own it and aren't in the queue.
1047 * #DBUS_RELEASE_NAME_REPLY_NOT_OWNER which means someone else
1048 * owns the name so you can't release it.
1049 * #DBUS_RELEASE_NAME_REPLY_NON_EXISTENT
1050 * which means nobody owned the name.
1052 * @param connection the connection
1053 * @param name the name to remove
1054 * @param error location to store the error
1055 * @returns a result code, -1 if error is set
1058 dbus_bus_release_name (DBusConnection *connection,
1062 DBusMessage *message, *reply;
1063 dbus_uint32_t result;
1065 _dbus_return_val_if_fail (connection != NULL, 0);
1066 _dbus_return_val_if_fail (name != NULL, 0);
1067 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
1068 _dbus_return_val_if_error_is_set (error, 0);
1070 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1072 DBUS_INTERFACE_DBUS,
1075 if (message == NULL)
1077 _DBUS_SET_OOM (error);
1081 if (!dbus_message_append_args (message,
1082 DBUS_TYPE_STRING, &name,
1085 dbus_message_unref (message);
1086 _DBUS_SET_OOM (error);
1090 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
1093 dbus_message_unref (message);
1097 _DBUS_ASSERT_ERROR_IS_SET (error);
1101 if (dbus_set_error_from_message (error, reply))
1103 _DBUS_ASSERT_ERROR_IS_SET (error);
1104 dbus_message_unref (reply);
1108 if (!dbus_message_get_args (reply, error,
1109 DBUS_TYPE_UINT32, &result,
1112 _DBUS_ASSERT_ERROR_IS_SET (error);
1113 dbus_message_unref (reply);
1117 dbus_message_unref (reply);
1123 * Asks the bus whether a certain name has an owner.
1125 * Using this can easily result in a race condition,
1126 * since an owner can appear or disappear after you
1129 * If you want to request a name, just request it;
1130 * if you want to avoid replacing a current owner,
1131 * don't specify #DBUS_NAME_FLAG_REPLACE_EXISTING and
1132 * you will get an error if there's already an owner.
1134 * @param connection the connection
1135 * @param name the name
1136 * @param error location to store any errors
1137 * @returns #TRUE if the name exists, #FALSE if not or on error
1140 dbus_bus_name_has_owner (DBusConnection *connection,
1144 DBusMessage *message, *reply;
1147 _dbus_return_val_if_fail (connection != NULL, FALSE);
1148 _dbus_return_val_if_fail (name != NULL, FALSE);
1149 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
1150 _dbus_return_val_if_error_is_set (error, FALSE);
1152 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1154 DBUS_INTERFACE_DBUS,
1156 if (message == NULL)
1158 _DBUS_SET_OOM (error);
1162 if (!dbus_message_append_args (message,
1163 DBUS_TYPE_STRING, &name,
1166 dbus_message_unref (message);
1167 _DBUS_SET_OOM (error);
1171 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
1172 dbus_message_unref (message);
1176 _DBUS_ASSERT_ERROR_IS_SET (error);
1180 if (!dbus_message_get_args (reply, error,
1181 DBUS_TYPE_BOOLEAN, &exists,
1184 _DBUS_ASSERT_ERROR_IS_SET (error);
1185 dbus_message_unref (reply);
1189 dbus_message_unref (reply);
1194 * Starts a service that will request ownership of the given name.
1195 * The returned result will be one of be one of
1196 * #DBUS_START_REPLY_SUCCESS or #DBUS_START_REPLY_ALREADY_RUNNING if
1197 * successful. Pass #NULL if you don't care about the result.
1199 * The flags parameter is for future expansion, currently you should
1202 * It's often easier to avoid explicitly starting services, and
1203 * just send a method call to the service's bus name instead.
1204 * Method calls start a service to handle them by default
1205 * unless you call dbus_message_set_auto_start() to disable this
1208 * @param connection the connection
1209 * @param name the name we want the new service to request
1210 * @param flags the flags (should always be 0 for now)
1211 * @param result a place to store the result or #NULL
1212 * @param error location to store any errors
1213 * @returns #TRUE if the activation succeeded, #FALSE if not
1216 dbus_bus_start_service_by_name (DBusConnection *connection,
1218 dbus_uint32_t flags,
1219 dbus_uint32_t *result,
1225 _dbus_return_val_if_fail (connection != NULL, FALSE);
1226 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
1228 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1230 DBUS_INTERFACE_DBUS,
1231 "StartServiceByName");
1233 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &name,
1234 DBUS_TYPE_UINT32, &flags, DBUS_TYPE_INVALID))
1236 dbus_message_unref (msg);
1237 _DBUS_SET_OOM (error);
1241 reply = dbus_connection_send_with_reply_and_block (connection, msg,
1243 dbus_message_unref (msg);
1247 _DBUS_ASSERT_ERROR_IS_SET (error);
1251 if (dbus_set_error_from_message (error, reply))
1253 _DBUS_ASSERT_ERROR_IS_SET (error);
1254 dbus_message_unref (reply);
1258 if (result != NULL &&
1259 !dbus_message_get_args (reply, error, DBUS_TYPE_UINT32,
1260 result, DBUS_TYPE_INVALID))
1262 _DBUS_ASSERT_ERROR_IS_SET (error);
1263 dbus_message_unref (reply);
1267 dbus_message_unref (reply);
1272 send_no_return_values (DBusConnection *connection,
1278 /* Block to check success codepath */
1281 reply = dbus_connection_send_with_reply_and_block (connection, msg,
1285 _DBUS_ASSERT_ERROR_IS_SET (error);
1287 dbus_message_unref (reply);
1291 /* Silently-fail nonblocking codepath */
1292 dbus_message_set_no_reply (msg, TRUE);
1293 dbus_connection_send (connection, msg, NULL);
1298 * Adds a match rule to match messages going through the message bus.
1299 * The "rule" argument is the string form of a match rule.
1301 * If you pass #NULL for the error, this function will not
1302 * block; the match thus won't be added until you flush the
1303 * connection, and if there's an error adding the match
1304 * (only possible error is lack of resources in the bus),
1305 * you won't find out about it.
1307 * If you pass non-#NULL for the error this function will
1308 * block until it gets a reply.
1310 * Normal API conventions would have the function return
1311 * a boolean value indicating whether the error was set,
1312 * but that would require blocking always to determine
1315 * The AddMatch method is fully documented in the D-Bus
1316 * specification. For quick reference, the format of the
1317 * match rules is discussed here, but the specification
1318 * is the canonical version of this information.
1320 * Rules are specified as a string of comma separated
1321 * key/value pairs. An example is
1322 * "type='signal',sender='org.freedesktop.DBus',
1323 * interface='org.freedesktop.DBus',member='Foo',
1324 * path='/bar/foo',destination=':452345.34'"
1326 * Possible keys you can match on are type, sender,
1327 * interface, member, path, destination and numbered
1328 * keys to match message args (keys are 'arg0', 'arg1', etc.).
1329 * Omitting a key from the rule indicates
1330 * a wildcard match. For instance omitting
1331 * the member from a match rule but adding a sender would
1332 * let all messages from that sender through regardless of
1335 * Matches are inclusive not exclusive so as long as one
1336 * rule matches the message will get through. It is important
1337 * to note this because every time a message is received the
1338 * application will be paged into memory to process it. This
1339 * can cause performance problems such as draining batteries
1340 * on embedded platforms.
1342 * If you match message args ('arg0', 'arg1', and so forth)
1343 * only string arguments will match. That is, arg0='5' means
1344 * match the string "5" not the integer 5.
1346 * Currently there is no way to match against non-string arguments.
1348 * Matching on interface is tricky because method call
1349 * messages only optionally specify the interface.
1350 * If a message omits the interface, then it will NOT match
1351 * if the rule specifies an interface name. This means match
1352 * rules on method calls should not usually give an interface.
1354 * However, signal messages are required to include the interface
1355 * so when matching signals usually you should specify the interface
1356 * in the match rule.
1358 * For security reasons, you can match arguments only up to
1359 * #DBUS_MAXIMUM_MATCH_RULE_ARG_NUMBER.
1361 * Match rules have a maximum length of #DBUS_MAXIMUM_MATCH_RULE_LENGTH
1364 * Both of these maximums are much higher than you're likely to need,
1365 * they only exist because the D-Bus bus daemon has fixed limits on
1366 * all resource usage.
1368 * @param connection connection to the message bus
1369 * @param rule textual form of match rule
1370 * @param error location to store any errors
1373 dbus_bus_add_match (DBusConnection *connection,
1379 _dbus_return_if_fail (rule != NULL);
1381 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1383 DBUS_INTERFACE_DBUS,
1388 _DBUS_SET_OOM (error);
1392 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
1395 dbus_message_unref (msg);
1396 _DBUS_SET_OOM (error);
1400 send_no_return_values (connection, msg, error);
1402 dbus_message_unref (msg);
1406 * Removes a previously-added match rule "by value" (the most
1407 * recently-added identical rule gets removed). The "rule" argument
1408 * is the string form of a match rule.
1410 * The bus compares match rules semantically, not textually, so
1411 * whitespace and ordering don't have to be identical to
1412 * the rule you passed to dbus_bus_add_match().
1414 * If you pass #NULL for the error, this function will not
1415 * block; otherwise it will. See detailed explanation in
1416 * docs for dbus_bus_add_match().
1418 * @param connection connection to the message bus
1419 * @param rule textual form of match rule
1420 * @param error location to store any errors
1423 dbus_bus_remove_match (DBusConnection *connection,
1429 _dbus_return_if_fail (rule != NULL);
1431 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1433 DBUS_INTERFACE_DBUS,
1436 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
1439 dbus_message_unref (msg);
1440 _DBUS_SET_OOM (error);
1444 send_no_return_values (connection, msg, error);
1446 dbus_message_unref (msg);