1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "dbus-protocol.h"
28 #include "dbus-internals.h"
29 #include "dbus-message.h"
30 #include "dbus-marshal-validate.h"
31 #include "dbus-misc.h"
32 #include "dbus-threads-internal.h"
33 #include "dbus-connection-internal.h"
34 #include "dbus-string.h"
35 #include "dbus-transport-kdbus.h"
41 * @defgroup DBusBus Message bus APIs
43 * @brief Functions for communicating with the message bus
45 * dbus_bus_get() allows all modules and libraries in a given
46 * process to share the same connection to the bus daemon by storing
47 * the connection globally.
49 * All other functions in this module are just convenience functions;
50 * most of them invoke methods on the bus daemon, by sending method
51 * call messages to #DBUS_SERVICE_DBUS. These convenience functions
52 * often make blocking method calls. If you don't want to block,
53 * you can send the method call messages manually in the same way
54 * you would any other method call message.
56 * This module is the only one in libdbus that's specific to
57 * communicating with the message bus daemon. The rest of the API can
58 * also be used for connecting to another application directly.
60 * @todo right now the default address of the system bus is hardcoded,
61 * so if you change it in the global config file suddenly you have to
62 * set DBUS_SYSTEM_BUS_ADDRESS env variable. Might be nice if the
63 * client lib somehow read the config file, or if the bus on startup
64 * somehow wrote out its address to a well-known spot, but might also
69 * @defgroup DBusBusInternals Message bus APIs internals
70 * @ingroup DBusInternals
71 * @brief Internals of functions for communicating with the message bus
77 * Block of message-bus-related data we attach to each
78 * #DBusConnection used with these convenience functions.
83 DBusConnection *connection; /**< Connection we're associated with */
84 char *unique_name; /**< Unique name of this connection */
86 unsigned int is_well_known : 1; /**< Is one of the well-known connections in our global array */
89 /** The slot we have reserved to store BusData.
91 static dbus_int32_t bus_data_slot = -1;
93 /** Number of bus types */
96 static DBusConnection *bus_connections[N_BUS_TYPES];
97 static char *bus_connection_addresses[N_BUS_TYPES] = { NULL, NULL, NULL };
99 static DBusBusType activation_bus_type = DBUS_BUS_STARTER;
101 static dbus_bool_t initialized = FALSE;
104 addresses_shutdown_func (void *data)
109 while (i < N_BUS_TYPES)
111 if (bus_connections[i] != NULL)
112 _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");
114 dbus_free (bus_connection_addresses[i]);
115 bus_connection_addresses[i] = NULL;
119 activation_bus_type = DBUS_BUS_STARTER;
125 get_from_env (char **connection_p,
130 _dbus_assert (*connection_p == NULL);
132 s = _dbus_getenv (env_var);
133 if (s == NULL || *s == '\0')
134 return TRUE; /* successfully didn't use the env var */
137 *connection_p = _dbus_strdup (s);
138 return *connection_p != NULL;
143 init_session_address (void)
149 /* First, look in the environment. This is the normal case on
150 * freedesktop.org/Unix systems. */
151 get_from_env (&bus_connection_addresses[DBUS_BUS_SESSION],
152 "DBUS_SESSION_BUS_ADDRESS");
153 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
155 dbus_bool_t supported;
157 DBusError error = DBUS_ERROR_INIT;
159 if (!_dbus_string_init (&addr))
163 /* So it's not in the environment - let's try a platform-specific method.
164 * On MacOS, this involves asking launchd. On Windows (not specified yet)
165 * we might do a COM lookup.
166 * Ignore errors - if we failed, fall back to autolaunch. */
167 retval = _dbus_lookup_session_address (&supported, &addr, &error);
168 if (supported && retval)
170 retval =_dbus_string_steal_data (&addr, &bus_connection_addresses[DBUS_BUS_SESSION]);
172 else if (supported && !retval)
174 if (dbus_error_is_set(&error))
175 _dbus_warn ("Dynamic session lookup supported but failed: %s\n", error.message);
177 _dbus_warn ("Dynamic session lookup supported but failed silently\n");
179 _dbus_string_free (&addr);
187 /* We have a hard-coded (but compile-time-configurable) fallback address for
188 * the session bus. */
189 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
190 bus_connection_addresses[DBUS_BUS_SESSION] =
191 _dbus_strdup (DBUS_SESSION_BUS_CONNECT_ADDRESS);
193 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
200 init_connections_unlocked (void)
208 while (i < N_BUS_TYPES)
210 bus_connections[i] = NULL;
214 /* Don't init these twice, we may run this code twice if
215 * init_connections_unlocked() fails midway through.
216 * In practice, each block below should contain only one
217 * "return FALSE" or running through twice may not
221 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
223 _dbus_verbose ("Filling in system bus address...\n");
225 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SYSTEM],
226 "DBUS_SYSTEM_BUS_ADDRESS"))
231 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
233 /* Use default system bus address if none set in environment */
234 bus_connection_addresses[DBUS_BUS_SYSTEM] =
235 _dbus_strdup (DBUS_SYSTEM_BUS_DEFAULT_ADDRESS);
237 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
240 _dbus_verbose (" used default system bus \"%s\"\n",
241 bus_connection_addresses[DBUS_BUS_SYSTEM]);
244 _dbus_verbose (" used env var system bus \"%s\"\n",
245 bus_connection_addresses[DBUS_BUS_SYSTEM]);
247 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
249 _dbus_verbose ("Filling in session bus address...\n");
251 if (!init_session_address ())
254 _dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_SESSION] ?
255 bus_connection_addresses[DBUS_BUS_SESSION] : "none set");
258 if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
260 _dbus_verbose ("Filling in activation bus address...\n");
262 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_STARTER],
263 "DBUS_STARTER_ADDRESS"))
266 _dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_STARTER] ?
267 bus_connection_addresses[DBUS_BUS_STARTER] : "none set");
271 if (bus_connection_addresses[DBUS_BUS_STARTER] != NULL)
273 s = _dbus_getenv ("DBUS_STARTER_BUS_TYPE");
277 _dbus_verbose ("Bus activation type was set to \"%s\"\n", s);
279 if (strcmp (s, "system") == 0)
280 activation_bus_type = DBUS_BUS_SYSTEM;
281 else if (strcmp (s, "session") == 0)
282 activation_bus_type = DBUS_BUS_SESSION;
287 /* Default to the session bus instead if available */
288 if (bus_connection_addresses[DBUS_BUS_SESSION] != NULL)
290 bus_connection_addresses[DBUS_BUS_STARTER] =
291 _dbus_strdup (bus_connection_addresses[DBUS_BUS_SESSION]);
292 if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
297 /* If we return FALSE we have to be sure that restarting
298 * the above code will work right
301 if (!dbus_setenv ("DBUS_ACTIVATION_ADDRESS", NULL))
304 if (!dbus_setenv ("DBUS_ACTIVATION_BUS_TYPE", NULL))
307 if (!_dbus_register_shutdown_func (addresses_shutdown_func,
318 bus_data_free (void *data)
322 if (bd->is_well_known)
326 if (!_DBUS_LOCK (bus))
327 _dbus_assert_not_reached ("global locks should have been initialized "
328 "when we attached bus data");
330 /* We may be stored in more than one slot */
331 /* This should now be impossible - these slots are supposed to
332 * be cleared on disconnect, so should not need to be cleared on
336 while (i < N_BUS_TYPES)
338 if (bus_connections[i] == bd->connection)
339 bus_connections[i] = NULL;
346 dbus_free (bd->unique_name);
349 dbus_connection_free_data_slot (&bus_data_slot);
353 ensure_bus_data (DBusConnection *connection)
357 if (!dbus_connection_allocate_data_slot (&bus_data_slot))
360 bd = dbus_connection_get_data (connection, bus_data_slot);
363 bd = dbus_new0 (BusData, 1);
366 dbus_connection_free_data_slot (&bus_data_slot);
370 bd->connection = connection;
372 if (!dbus_connection_set_data (connection, bus_data_slot, bd,
376 dbus_connection_free_data_slot (&bus_data_slot);
380 /* Data slot refcount now held by the BusData */
384 dbus_connection_free_data_slot (&bus_data_slot);
391 * Internal function that checks to see if this
392 * is a shared connection owned by the bus and if it is unref it.
394 * @param connection a connection that has been disconnected.
397 _dbus_bus_notify_shared_connection_disconnected_unlocked (DBusConnection *connection)
401 if (!_DBUS_LOCK (bus))
403 /* If it was in bus_connections, we would have initialized global locks
404 * when we added it. So, it can't be. */
408 /* We are expecting to have the connection saved in only one of these
409 * slots, but someone could in a pathological case set system and session
410 * bus to the same bus or something. Or set one of them to the starter
411 * bus without setting the starter bus type in the env variable.
412 * So we don't break the loop as soon as we find a match.
414 for (i = 0; i < N_BUS_TYPES; ++i)
416 if (bus_connections[i] == connection)
418 bus_connections[i] = NULL;
425 static DBusConnection *
426 internal_bus_get (DBusBusType type,
431 DBusConnection *connection;
433 DBusBusType address_type;
435 _dbus_return_val_if_fail (type >= 0 && type < N_BUS_TYPES, NULL);
436 _dbus_return_val_if_error_is_set (error, NULL);
440 if (!_DBUS_LOCK (bus))
442 _DBUS_SET_OOM (error);
443 /* do not "goto out", that would try to unlock */
447 if (!init_connections_unlocked ())
449 _DBUS_SET_OOM (error);
453 /* We want to use the activation address even if the
454 * activating bus is the session or system bus,
459 /* Use the real type of the activation bus for getting its
460 * connection, but only if the real type's address is available. (If
461 * the activating bus isn't a well-known bus then
462 * activation_bus_type == DBUS_BUS_STARTER)
464 if (type == DBUS_BUS_STARTER &&
465 bus_connection_addresses[activation_bus_type] != NULL)
466 type = activation_bus_type;
468 if (!private && bus_connections[type] != NULL)
470 connection = bus_connections[type];
471 dbus_connection_ref (connection);
475 address = bus_connection_addresses[address_type];
478 dbus_set_error (error, DBUS_ERROR_FAILED,
479 "Unable to determine the address of the message bus (try 'man dbus-launch' and 'man dbus-daemon' for help)");
484 connection = dbus_connection_open_private (address, error);
486 connection = dbus_connection_open (address, error);
493 _dbus_verbose (" !!! dbus_connection_open finished successfully !!!! \n"); //todo RP to be removed
495 if (!dbus_bus_register (connection, error))
497 _dbus_connection_close_possibly_shared (connection);
498 dbus_connection_unref (connection);
505 /* store a weak ref to the connection (dbus-connection.c is
506 * supposed to have a strong ref that it drops on disconnect,
507 * since this is a shared connection)
509 bus_connections[type] = connection;
512 /* By default we're bound to the lifecycle of
515 dbus_connection_set_exit_on_disconnect (connection,
518 if (!_DBUS_LOCK (bus_datas))
519 _dbus_assert_not_reached ("global locks were initialized already");
521 bd = ensure_bus_data (connection);
522 _dbus_assert (bd != NULL); /* it should have been created on
523 register, so OOM not possible */
524 bd->is_well_known = TRUE;
525 _DBUS_UNLOCK (bus_datas);
528 /* Return a reference to the caller, or NULL with error set. */
529 if (connection == NULL)
530 _DBUS_ASSERT_ERROR_IS_SET (error);
537 /** @} */ /* end of implementation details docs */
540 * @addtogroup DBusBus
545 * Connects to a bus daemon and registers the client with it. If a
546 * connection to the bus already exists, then that connection is
547 * returned. The caller of this function owns a reference to the bus.
549 * The caller may NOT call dbus_connection_close() on this connection;
550 * see dbus_connection_open() and dbus_connection_close() for details
553 * If this function obtains a new connection object never before
554 * returned from dbus_bus_get(), it will call
555 * dbus_connection_set_exit_on_disconnect(), so the application
556 * will exit if the connection closes. You can undo this
557 * by calling dbus_connection_set_exit_on_disconnect() yourself
558 * after you get the connection.
560 * dbus_bus_get() calls dbus_bus_register() for you.
562 * If returning a newly-created connection, this function will block
563 * until authentication and bus registration are complete.
565 * @param type bus type
566 * @param error address where an error can be returned.
567 * @returns a #DBusConnection with new ref or #NULL on error
570 dbus_bus_get (DBusBusType type,
573 return internal_bus_get (type, FALSE, error);
577 * Connects to a bus daemon and registers the client with it as with
578 * dbus_bus_register(). Unlike dbus_bus_get(), always creates a new
579 * connection. This connection will not be saved or recycled by
580 * libdbus. Caller owns a reference to the bus and must either close
581 * it or know it to be closed prior to releasing this reference.
583 * See dbus_connection_open_private() for more details on when to
584 * close and unref this connection.
586 * This function calls
587 * dbus_connection_set_exit_on_disconnect() on the new connection, so the application
588 * will exit if the connection closes. You can undo this
589 * by calling dbus_connection_set_exit_on_disconnect() yourself
590 * after you get the connection.
592 * dbus_bus_get_private() calls dbus_bus_register() for you.
594 * This function will block until authentication and bus registration
597 * @param type bus type
598 * @param error address where an error can be returned.
599 * @returns a DBusConnection with new ref
602 dbus_bus_get_private (DBusBusType type,
605 return internal_bus_get (type, TRUE, error);
609 * Registers a connection with the bus. This must be the first
610 * thing an application does when connecting to the message bus.
611 * If registration succeeds, the unique name will be set,
612 * and can be obtained using dbus_bus_get_unique_name().
614 * This function will block until registration is complete.
616 * If the connection has already registered with the bus
617 * (determined by checking whether dbus_bus_get_unique_name()
618 * returns a non-#NULL value), then this function does nothing.
620 * If you use dbus_bus_get() or dbus_bus_get_private() this
621 * function will be called for you.
623 * @note Just use dbus_bus_get() or dbus_bus_get_private() instead of
624 * dbus_bus_register() and save yourself some pain. Using
625 * dbus_bus_register() manually is only useful if you have your
626 * own custom message bus not found in #DBusBusType.
628 * If you open a bus connection with dbus_connection_open() or
629 * dbus_connection_open_private() you will have to dbus_bus_register()
630 * yourself, or make the appropriate registration method calls
631 * yourself. If you send the method calls yourself, call
632 * dbus_bus_set_unique_name() with the unique bus name you get from
635 * For shared connections (created with dbus_connection_open()) in a
636 * multithreaded application, you can't really make the registration
637 * calls yourself, because you don't know whether some other thread is
638 * also registering, and the bus will kick you off if you send two
639 * registration messages.
641 * If you use dbus_bus_register() however, there is a lock that
642 * keeps both apps from registering at the same time.
644 * The rule in a multithreaded app, then, is that dbus_bus_register()
645 * must be used to register, or you need to have your own locks that
646 * all threads in the app will respect.
648 * In a single-threaded application you can register by hand instead
649 * of using dbus_bus_register(), as long as you check
650 * dbus_bus_get_unique_name() to see if a unique name has already been
651 * stored by another thread before you send the registration messages.
653 * @param connection the connection
654 * @param error place to store errors
655 * @returns #TRUE on success
658 dbus_bus_register (DBusConnection *connection,
661 DBusMessage *message, *reply;
666 _dbus_return_val_if_fail (connection != NULL, FALSE);
667 _dbus_return_val_if_error_is_set (error, FALSE);
673 if (!_DBUS_LOCK (bus_datas))
675 _DBUS_SET_OOM (error);
676 /* do not "goto out", that would try to unlock */
680 bd = ensure_bus_data (connection);
683 _DBUS_SET_OOM (error);
687 if (bd->unique_name != NULL)
689 _dbus_verbose ("Ignoring attempt to register the same DBusConnection %s with the message bus a second time.\n",
695 if(dbus_transport_is_kdbus(connection))
697 name = malloc(snprintf(name, 0, "%llu", ULLONG_MAX) + 1);
698 if(!bus_register_kdbus(name, connection, error))
701 if(!bus_register_policy_kdbus(name, connection, error))
704 dbus_connection_set_is_authenticated(connection);
708 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
714 _DBUS_SET_OOM (error);
718 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
722 else if (dbus_set_error_from_message (error, reply))
724 else if (!dbus_message_get_args (reply, error,
725 DBUS_TYPE_STRING, &name,
730 bd->unique_name = _dbus_strdup (name);
731 if (bd->unique_name == NULL)
733 _DBUS_SET_OOM (error);
736 //_dbus_verbose("-- Our uniqe name is: %s\n", bd->unique_name);
740 _DBUS_UNLOCK (bus_datas);
743 dbus_message_unref (message);
748 dbus_message_unref (reply);
751 _DBUS_ASSERT_ERROR_IS_SET (error);
758 * Sets the unique name of the connection, as assigned by the message
759 * bus. Can only be used if you registered with the bus manually
760 * (i.e. if you did not call dbus_bus_register()). Can only be called
761 * once per connection. After the unique name is set, you can get it
762 * with dbus_bus_get_unique_name().
764 * The only reason to use this function is to re-implement the
765 * equivalent of dbus_bus_register() yourself. One (probably unusual)
766 * reason to do that might be to do the bus registration call
767 * asynchronously instead of synchronously.
769 * @note Just use dbus_bus_get() or dbus_bus_get_private(), or worst
770 * case dbus_bus_register(), instead of messing with this
771 * function. There's really no point creating pain for yourself by
772 * doing things manually.
774 * It's hard to use this function safely on shared connections
775 * (created by dbus_connection_open()) in a multithreaded application,
776 * because only one registration attempt can be sent to the bus. If
777 * two threads are both sending the registration message, there is no
778 * mechanism in libdbus itself to avoid sending it twice.
780 * Thus, you need a way to coordinate which thread sends the
781 * registration attempt; which also means you know which thread
782 * will call dbus_bus_set_unique_name(). If you don't know
783 * about all threads in the app (for example, if some libraries
784 * you're using might start libdbus-using threads), then you
785 * need to avoid using this function on shared connections.
787 * @param connection the connection
788 * @param unique_name the unique name
789 * @returns #FALSE if not enough memory
792 dbus_bus_set_unique_name (DBusConnection *connection,
793 const char *unique_name)
796 dbus_bool_t success = FALSE;
798 _dbus_return_val_if_fail (connection != NULL, FALSE);
799 _dbus_return_val_if_fail (unique_name != NULL, FALSE);
801 if (!_DBUS_LOCK (bus_datas))
803 /* do not "goto out", that would try to unlock */
807 bd = ensure_bus_data (connection);
811 _dbus_assert (bd->unique_name == NULL);
813 bd->unique_name = _dbus_strdup (unique_name);
814 success = bd->unique_name != NULL;
817 _DBUS_UNLOCK (bus_datas);
823 * Gets the unique name of the connection as assigned by the message
824 * bus. Only possible after the connection has been registered with
825 * the message bus. All connections returned by dbus_bus_get() or
826 * dbus_bus_get_private() have been successfully registered.
828 * The name remains valid until the connection is freed, and
829 * should not be freed by the caller.
831 * Other than dbus_bus_get(), there are two ways to set the unique
832 * name; one is dbus_bus_register(), the other is
833 * dbus_bus_set_unique_name(). You are responsible for calling
834 * dbus_bus_set_unique_name() if you register by hand instead of using
835 * dbus_bus_register().
837 * @param connection the connection
838 * @returns the unique name or #NULL on error
841 dbus_bus_get_unique_name (DBusConnection *connection)
844 const char *unique_name = NULL;
846 _dbus_return_val_if_fail (connection != NULL, NULL);
848 if (!_DBUS_LOCK (bus_datas))
850 /* We'd have initialized locks when we gave it its unique name, if it
851 * had one. Don't "goto out", that would try to unlock. */
855 bd = ensure_bus_data (connection);
859 unique_name = bd->unique_name;
862 _DBUS_UNLOCK (bus_datas);
868 * Asks the bus to return the UID the named connection authenticated
869 * as, if any. Only works on UNIX; only works for connections on the
870 * same machine as the bus. If you are not on the same machine as the
871 * bus, then calling this is probably a bad idea, since the UID will
872 * mean little to your application.
874 * For the system message bus you're guaranteed to be on the same
875 * machine since it only listens on a UNIX domain socket (at least,
876 * as shipped by default).
878 * This function only works for connections that authenticated as
879 * a UNIX user, right now that includes all bus connections, but
880 * it's very possible to have connections with no associated UID.
881 * So check for errors and do something sensible if they happen.
883 * This function will always return an error on Windows.
885 * @param connection the connection
886 * @param name a name owned by the connection
887 * @param error location to store the error
888 * @returns the unix user id, or ((unsigned)-1) if error is set
891 dbus_bus_get_unix_user (DBusConnection *connection,
895 DBusMessage *message, *reply;
898 _dbus_return_val_if_fail (connection != NULL, DBUS_UID_UNSET);
899 _dbus_return_val_if_fail (name != NULL, DBUS_UID_UNSET);
900 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), DBUS_UID_UNSET);
901 _dbus_return_val_if_error_is_set (error, DBUS_UID_UNSET);
903 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
906 "GetConnectionUnixUser");
910 _DBUS_SET_OOM (error);
911 return DBUS_UID_UNSET;
914 if (!dbus_message_append_args (message,
915 DBUS_TYPE_STRING, &name,
918 dbus_message_unref (message);
919 _DBUS_SET_OOM (error);
920 return DBUS_UID_UNSET;
923 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
926 dbus_message_unref (message);
930 _DBUS_ASSERT_ERROR_IS_SET (error);
931 return DBUS_UID_UNSET;
934 if (dbus_set_error_from_message (error, reply))
936 _DBUS_ASSERT_ERROR_IS_SET (error);
937 dbus_message_unref (reply);
938 return DBUS_UID_UNSET;
941 if (!dbus_message_get_args (reply, error,
942 DBUS_TYPE_UINT32, &uid,
945 _DBUS_ASSERT_ERROR_IS_SET (error);
946 dbus_message_unref (reply);
947 return DBUS_UID_UNSET;
950 dbus_message_unref (reply);
952 return (unsigned long) uid;
956 * Asks the bus to return its globally unique ID, as described in the
957 * D-Bus specification. For the session bus, this is useful as a way
958 * to uniquely identify each user session. For the system bus,
959 * probably the bus ID is not useful; instead, use the machine ID
960 * since it's accessible without necessarily connecting to the bus and
961 * may be persistent beyond a single bus instance (across reboots for
962 * example). See dbus_get_local_machine_id().
964 * In addition to an ID for each bus and an ID for each machine, there is
965 * an ID for each address that the bus is listening on; that can
966 * be retrieved with dbus_connection_get_server_id(), though it is
967 * probably not very useful.
969 * @param connection the connection
970 * @param error location to store the error
971 * @returns the bus ID or #NULL if error is set
974 dbus_bus_get_id (DBusConnection *connection,
977 DBusMessage *message, *reply;
979 const char *v_STRING;
981 _dbus_return_val_if_fail (connection != NULL, NULL);
982 _dbus_return_val_if_error_is_set (error, NULL);
984 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
991 _DBUS_SET_OOM (error);
995 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
998 dbus_message_unref (message);
1002 _DBUS_ASSERT_ERROR_IS_SET (error);
1006 if (dbus_set_error_from_message (error, reply))
1008 _DBUS_ASSERT_ERROR_IS_SET (error);
1009 dbus_message_unref (reply);
1014 if (!dbus_message_get_args (reply, error,
1015 DBUS_TYPE_STRING, &v_STRING,
1018 _DBUS_ASSERT_ERROR_IS_SET (error);
1019 dbus_message_unref (reply);
1023 id = _dbus_strdup (v_STRING); /* may be NULL */
1025 dbus_message_unref (reply);
1028 _DBUS_SET_OOM (error);
1030 /* FIXME it might be nice to cache the ID locally */
1036 * Asks the bus to assign the given name to this connection by invoking
1037 * the RequestName method on the bus. This method is fully documented
1038 * in the D-Bus specification. For quick reference, the flags and
1039 * result codes are discussed here, but the specification is the
1040 * canonical version of this information.
1042 * First you should know that for each bus name, the bus stores
1043 * a queue of connections that would like to own it. Only
1044 * one owns it at a time - called the primary owner. If the primary
1045 * owner releases the name or disconnects, then the next owner in the
1046 * queue atomically takes over.
1048 * So for example if you have an application org.freedesktop.TextEditor
1049 * and multiple instances of it can be run, you can have all of them
1050 * sitting in the queue. The first one to start up will receive messages
1051 * sent to org.freedesktop.TextEditor, but if that one exits another
1052 * will become the primary owner and receive messages.
1054 * The queue means you don't need to manually watch for the current owner to
1055 * disappear and then request the name again.
1057 * When requesting a name, you can specify several flags.
1059 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT and #DBUS_NAME_FLAG_DO_NOT_QUEUE
1060 * are properties stored by the bus for this connection with respect to
1061 * each requested bus name. These properties are stored even if the
1062 * connection is queued and does not become the primary owner.
1063 * You can update these flags by calling RequestName again (even if
1064 * you already own the name).
1066 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT means that another requestor of the
1067 * name can take it away from you by specifying #DBUS_NAME_FLAG_REPLACE_EXISTING.
1069 * #DBUS_NAME_FLAG_DO_NOT_QUEUE means that if you aren't the primary owner,
1070 * you don't want to be queued up - you only care about being the
1073 * Unlike the other two flags, #DBUS_NAME_FLAG_REPLACE_EXISTING is a property
1074 * of the individual RequestName call, i.e. the bus does not persistently
1075 * associate it with the connection-name pair. If a RequestName call includes
1076 * the #DBUS_NAME_FLAG_REPLACE_EXISTING flag, and the current primary
1077 * owner has #DBUS_NAME_FLAG_ALLOW_REPLACEMENT set, then the current primary
1078 * owner will be kicked off.
1080 * If no flags are given, an application will receive the requested
1081 * name only if the name is currently unowned; and it will NOT give
1082 * up the name if another application asks to take it over using
1083 * #DBUS_NAME_FLAG_REPLACE_EXISTING.
1085 * This function returns a result code. The possible result codes
1088 * #DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER means that the name had no
1089 * existing owner, and the caller is now the primary owner; or that
1090 * the name had an owner, and the caller specified
1091 * #DBUS_NAME_FLAG_REPLACE_EXISTING, and the current owner
1092 * specified #DBUS_NAME_FLAG_ALLOW_REPLACEMENT.
1094 * #DBUS_REQUEST_NAME_REPLY_IN_QUEUE happens only if the caller does NOT
1095 * specify #DBUS_NAME_FLAG_DO_NOT_QUEUE and either the current owner
1096 * did NOT specify #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or the caller did NOT
1097 * specify #DBUS_NAME_FLAG_REPLACE_EXISTING. In this case the caller ends up
1098 * in a queue to own the name after the current owner gives it up.
1100 * #DBUS_REQUEST_NAME_REPLY_EXISTS happens if the name has an owner
1101 * already and the caller specifies #DBUS_NAME_FLAG_DO_NOT_QUEUE
1102 * and either the current owner has NOT specified
1103 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or the caller did NOT specify
1104 * #DBUS_NAME_FLAG_REPLACE_EXISTING.
1106 * #DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER happens if an application
1107 * requests a name it already owns. (Re-requesting a name is useful if
1108 * you want to change the #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or
1109 * #DBUS_NAME_FLAG_DO_NOT_QUEUE settings.)
1111 * When a service represents an application, say "text editor," then
1112 * it should specify #DBUS_NAME_FLAG_ALLOW_REPLACEMENT if it wants
1113 * the last editor started to be the user's editor vs. the first one
1114 * started. Then any editor that can be the user's editor should
1115 * specify #DBUS_NAME_FLAG_REPLACE_EXISTING to either take over
1116 * (last-started-wins) or be queued up (first-started-wins) according
1117 * to whether #DBUS_NAME_FLAG_ALLOW_REPLACEMENT was given.
1119 * Conventionally, single-instance applications often offer a command
1120 * line option called --replace which means to replace the current
1121 * instance. To implement this, always set
1122 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT when you request your
1123 * application's bus name. When you lose ownership of your bus name,
1124 * you need to exit. Look for the signal "NameLost" from
1125 * #DBUS_SERVICE_DBUS and #DBUS_INTERFACE_DBUS (the signal's first
1126 * argument is the bus name that was lost). If starting up without
1127 * --replace, do not specify #DBUS_NAME_FLAG_REPLACE_EXISTING, and
1128 * exit if you fail to become the bus name owner. If --replace is
1129 * given, ask to replace the old owner.
1131 * @param connection the connection
1132 * @param name the name to request
1133 * @param flags flags
1134 * @param error location to store the error
1135 * @returns a result code, -1 if error is set
1138 dbus_bus_request_name (DBusConnection *connection,
1143 dbus_uint32_t result;
1145 _dbus_return_val_if_fail (connection != NULL, 0);
1146 _dbus_return_val_if_fail (name != NULL, 0);
1147 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
1148 _dbus_return_val_if_error_is_set (error, 0);
1150 if(!dbus_transport_is_kdbus(connection))
1152 DBusMessage *message, *reply;
1154 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1156 DBUS_INTERFACE_DBUS,
1158 if (message == NULL)
1160 _DBUS_SET_OOM (error);
1164 if (!dbus_message_append_args (message,
1165 DBUS_TYPE_STRING, &name,
1166 DBUS_TYPE_UINT32, &flags,
1169 dbus_message_unref (message);
1170 _DBUS_SET_OOM (error);
1174 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
1177 dbus_message_unref (message);
1181 _DBUS_ASSERT_ERROR_IS_SET (error);
1185 if (dbus_set_error_from_message (error, reply))
1187 _DBUS_ASSERT_ERROR_IS_SET (error);
1188 dbus_message_unref (reply);
1192 if (!dbus_message_get_args (reply, error,
1193 DBUS_TYPE_UINT32, &result,
1196 _DBUS_ASSERT_ERROR_IS_SET (error);
1197 dbus_message_unref (reply);
1201 dbus_message_unref (reply);
1205 if(!bus_register_policy_kdbus(name, connection, error))
1208 result = bus_request_name_kdbus(connection, name, flags, error);
1216 * Asks the bus to unassign the given name from this connection by
1217 * invoking the ReleaseName method on the bus. The "ReleaseName"
1218 * method is canonically documented in the D-Bus specification.
1220 * Possible results are: #DBUS_RELEASE_NAME_REPLY_RELEASED
1221 * which means you owned the name or were in the queue to own it,
1222 * and and now you don't own it and aren't in the queue.
1223 * #DBUS_RELEASE_NAME_REPLY_NOT_OWNER which means someone else
1224 * owns the name so you can't release it.
1225 * #DBUS_RELEASE_NAME_REPLY_NON_EXISTENT
1226 * which means nobody owned the name.
1228 * @param connection the connection
1229 * @param name the name to remove
1230 * @param error location to store the error
1231 * @returns a result code, -1 if error is set
1234 dbus_bus_release_name (DBusConnection *connection,
1238 DBusMessage *message, *reply;
1239 dbus_uint32_t result;
1241 _dbus_return_val_if_fail (connection != NULL, 0);
1242 _dbus_return_val_if_fail (name != NULL, 0);
1243 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
1244 _dbus_return_val_if_error_is_set (error, 0);
1246 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1248 DBUS_INTERFACE_DBUS,
1251 if (message == NULL)
1253 _DBUS_SET_OOM (error);
1257 if (!dbus_message_append_args (message,
1258 DBUS_TYPE_STRING, &name,
1261 dbus_message_unref (message);
1262 _DBUS_SET_OOM (error);
1266 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
1269 dbus_message_unref (message);
1273 _DBUS_ASSERT_ERROR_IS_SET (error);
1277 if (dbus_set_error_from_message (error, reply))
1279 _DBUS_ASSERT_ERROR_IS_SET (error);
1280 dbus_message_unref (reply);
1284 if (!dbus_message_get_args (reply, error,
1285 DBUS_TYPE_UINT32, &result,
1288 _DBUS_ASSERT_ERROR_IS_SET (error);
1289 dbus_message_unref (reply);
1293 dbus_message_unref (reply);
1299 * Asks the bus whether a certain name has an owner.
1301 * Using this can easily result in a race condition,
1302 * since an owner can appear or disappear after you
1305 * If you want to request a name, just request it;
1306 * if you want to avoid replacing a current owner,
1307 * don't specify #DBUS_NAME_FLAG_REPLACE_EXISTING and
1308 * you will get an error if there's already an owner.
1310 * @param connection the connection
1311 * @param name the name
1312 * @param error location to store any errors
1313 * @returns #TRUE if the name exists, #FALSE if not or on error
1316 dbus_bus_name_has_owner (DBusConnection *connection,
1320 DBusMessage *message, *reply;
1323 _dbus_return_val_if_fail (connection != NULL, FALSE);
1324 _dbus_return_val_if_fail (name != NULL, FALSE);
1325 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
1326 _dbus_return_val_if_error_is_set (error, FALSE);
1328 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1330 DBUS_INTERFACE_DBUS,
1332 if (message == NULL)
1334 _DBUS_SET_OOM (error);
1338 if (!dbus_message_append_args (message,
1339 DBUS_TYPE_STRING, &name,
1342 dbus_message_unref (message);
1343 _DBUS_SET_OOM (error);
1347 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
1348 dbus_message_unref (message);
1352 _DBUS_ASSERT_ERROR_IS_SET (error);
1356 if (!dbus_message_get_args (reply, error,
1357 DBUS_TYPE_BOOLEAN, &exists,
1360 _DBUS_ASSERT_ERROR_IS_SET (error);
1361 dbus_message_unref (reply);
1365 dbus_message_unref (reply);
1370 * Starts a service that will request ownership of the given name.
1371 * The returned result will be one of be one of
1372 * #DBUS_START_REPLY_SUCCESS or #DBUS_START_REPLY_ALREADY_RUNNING if
1373 * successful. Pass #NULL if you don't care about the result.
1375 * The flags parameter is for future expansion, currently you should
1378 * It's often easier to avoid explicitly starting services, and
1379 * just send a method call to the service's bus name instead.
1380 * Method calls start a service to handle them by default
1381 * unless you call dbus_message_set_auto_start() to disable this
1384 * @param connection the connection
1385 * @param name the name we want the new service to request
1386 * @param flags the flags (should always be 0 for now)
1387 * @param result a place to store the result or #NULL
1388 * @param error location to store any errors
1389 * @returns #TRUE if the activation succeeded, #FALSE if not
1392 dbus_bus_start_service_by_name (DBusConnection *connection,
1394 dbus_uint32_t flags,
1395 dbus_uint32_t *result,
1401 _dbus_return_val_if_fail (connection != NULL, FALSE);
1402 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
1404 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1406 DBUS_INTERFACE_DBUS,
1407 "StartServiceByName");
1409 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &name,
1410 DBUS_TYPE_UINT32, &flags, DBUS_TYPE_INVALID))
1412 dbus_message_unref (msg);
1413 _DBUS_SET_OOM (error);
1417 reply = dbus_connection_send_with_reply_and_block (connection, msg,
1419 dbus_message_unref (msg);
1423 _DBUS_ASSERT_ERROR_IS_SET (error);
1427 if (dbus_set_error_from_message (error, reply))
1429 _DBUS_ASSERT_ERROR_IS_SET (error);
1430 dbus_message_unref (reply);
1434 if (result != NULL &&
1435 !dbus_message_get_args (reply, error, DBUS_TYPE_UINT32,
1436 result, DBUS_TYPE_INVALID))
1438 _DBUS_ASSERT_ERROR_IS_SET (error);
1439 dbus_message_unref (reply);
1443 dbus_message_unref (reply);
1448 send_no_return_values (DBusConnection *connection,
1454 /* Block to check success codepath */
1457 reply = dbus_connection_send_with_reply_and_block (connection, msg,
1461 _DBUS_ASSERT_ERROR_IS_SET (error);
1463 dbus_message_unref (reply);
1467 /* Silently-fail nonblocking codepath */
1468 dbus_message_set_no_reply (msg, TRUE);
1469 dbus_connection_send (connection, msg, NULL);
1474 * Adds a match rule to match messages going through the message bus.
1475 * The "rule" argument is the string form of a match rule.
1477 * If you pass #NULL for the error, this function will not
1478 * block; the match thus won't be added until you flush the
1479 * connection, and if there's an error adding the match
1480 * you won't find out about it. This is generally acceptable, since the
1481 * possible errors (including a lack of resources in the bus, the connection
1482 * having exceeded its quota of active match rules, or the match rule being
1483 * unparseable) are generally unrecoverable.
1485 * If you pass non-#NULL for the error this function will
1486 * block until it gets a reply. This may be useful when using match rule keys
1487 * introduced in recent versions of D-Bus, like 'arg0namespace', to allow the
1488 * application to fall back to less efficient match rules supported by older
1489 * versions of the daemon if the running version is not new enough; or when
1490 * using user-supplied rules rather than rules hard-coded at compile time.
1492 * Normal API conventions would have the function return
1493 * a boolean value indicating whether the error was set,
1494 * but that would require blocking always to determine
1497 * The AddMatch method is fully documented in the D-Bus
1498 * specification. For quick reference, the format of the
1499 * match rules is discussed here, but the specification
1500 * is the canonical version of this information.
1502 * Rules are specified as a string of comma separated
1503 * key/value pairs. An example is
1504 * "type='signal',sender='org.freedesktop.DBus',
1505 * interface='org.freedesktop.DBus',member='Foo',
1506 * path='/bar/foo',destination=':452345.34'"
1508 * Possible keys you can match on are type, sender,
1509 * interface, member, path, destination and numbered
1510 * keys to match message args (keys are 'arg0', 'arg1', etc.).
1511 * Omitting a key from the rule indicates
1512 * a wildcard match. For instance omitting
1513 * the member from a match rule but adding a sender would
1514 * let all messages from that sender through regardless of
1517 * Matches are inclusive not exclusive so as long as one
1518 * rule matches the message will get through. It is important
1519 * to note this because every time a message is received the
1520 * application will be paged into memory to process it. This
1521 * can cause performance problems such as draining batteries
1522 * on embedded platforms.
1524 * If you match message args ('arg0', 'arg1', and so forth)
1525 * only string arguments will match. That is, arg0='5' means
1526 * match the string "5" not the integer 5.
1528 * Currently there is no way to match against non-string arguments.
1530 * A specialised form of wildcard matching on arguments is
1531 * supported for path-like namespaces. If your argument match has
1532 * a 'path' suffix (eg: "arg0path='/some/path/'") then it is
1533 * considered a match if the argument exactly matches the given
1534 * string or if one of them ends in a '/' and is a prefix of the
1537 * Matching on interface is tricky because method call
1538 * messages only optionally specify the interface.
1539 * If a message omits the interface, then it will NOT match
1540 * if the rule specifies an interface name. This means match
1541 * rules on method calls should not usually give an interface.
1543 * However, signal messages are required to include the interface
1544 * so when matching signals usually you should specify the interface
1545 * in the match rule.
1547 * For security reasons, you can match arguments only up to
1548 * #DBUS_MAXIMUM_MATCH_RULE_ARG_NUMBER.
1550 * Match rules have a maximum length of #DBUS_MAXIMUM_MATCH_RULE_LENGTH
1553 * Both of these maximums are much higher than you're likely to need,
1554 * they only exist because the D-Bus bus daemon has fixed limits on
1555 * all resource usage.
1557 * @param connection connection to the message bus
1558 * @param rule textual form of match rule
1559 * @param error location to store any errors
1562 dbus_bus_add_match (DBusConnection *connection,
1566 _dbus_return_if_fail (rule != NULL);
1568 if(!dbus_transport_is_kdbus(connection))
1572 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1574 DBUS_INTERFACE_DBUS,
1579 _DBUS_SET_OOM (error);
1583 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
1586 dbus_message_unref (msg);
1587 _DBUS_SET_OOM (error);
1591 send_no_return_values (connection, msg, error);
1593 dbus_message_unref (msg);
1596 dbus_bus_add_match_kdbus(connection, rule, error);
1600 * Removes a previously-added match rule "by value" (the most
1601 * recently-added identical rule gets removed). The "rule" argument
1602 * is the string form of a match rule.
1604 * The bus compares match rules semantically, not textually, so
1605 * whitespace and ordering don't have to be identical to
1606 * the rule you passed to dbus_bus_add_match().
1608 * If you pass #NULL for the error, this function will not
1609 * block; otherwise it will. See detailed explanation in
1610 * docs for dbus_bus_add_match().
1612 * @param connection connection to the message bus
1613 * @param rule textual form of match rule
1614 * @param error location to store any errors
1617 dbus_bus_remove_match (DBusConnection *connection,
1621 if(!dbus_transport_is_kdbus(connection))
1625 _dbus_return_if_fail (rule != NULL);
1627 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1629 DBUS_INTERFACE_DBUS,
1632 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
1635 dbus_message_unref (msg);
1636 _DBUS_SET_OOM (error);
1640 send_no_return_values (connection, msg, error);
1642 dbus_message_unref (msg);
1645 dbus_bus_remove_match_kdbus(connection, error);