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;
142 void dbus_bus_set_bus_connection_address(DBusBusType address_type, char* address)
144 if(bus_connection_addresses[address_type] != NULL)
145 free(bus_connection_addresses[address_type]);
146 bus_connection_addresses[address_type] = address;
150 init_session_address (void)
156 /* First, look in the environment. This is the normal case on
157 * freedesktop.org/Unix systems. */
158 get_from_env (&bus_connection_addresses[DBUS_BUS_SESSION],
159 "DBUS_SESSION_BUS_ADDRESS");
160 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
162 dbus_bool_t supported;
164 DBusError error = DBUS_ERROR_INIT;
166 if (!_dbus_string_init (&addr))
170 /* So it's not in the environment - let's try a platform-specific method.
171 * On MacOS, this involves asking launchd. On Windows (not specified yet)
172 * we might do a COM lookup.
173 * Ignore errors - if we failed, fall back to autolaunch. */
174 retval = _dbus_lookup_session_address (&supported, &addr, &error);
175 if (supported && retval)
177 retval =_dbus_string_steal_data (&addr, &bus_connection_addresses[DBUS_BUS_SESSION]);
179 else if (supported && !retval)
181 if (dbus_error_is_set(&error))
182 _dbus_warn ("Dynamic session lookup supported but failed: %s\n", error.message);
184 _dbus_warn ("Dynamic session lookup supported but failed silently\n");
186 _dbus_string_free (&addr);
194 /* We have a hard-coded (but compile-time-configurable) fallback address for
195 * the session bus. */
196 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
197 bus_connection_addresses[DBUS_BUS_SESSION] =
198 _dbus_strdup (DBUS_SESSION_BUS_CONNECT_ADDRESS);
200 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
207 init_connections_unlocked (void)
215 while (i < N_BUS_TYPES)
217 bus_connections[i] = NULL;
221 /* Don't init these twice, we may run this code twice if
222 * init_connections_unlocked() fails midway through.
223 * In practice, each block below should contain only one
224 * "return FALSE" or running through twice may not
228 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
230 _dbus_verbose ("Filling in system bus address...\n");
232 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SYSTEM],
233 "DBUS_SYSTEM_BUS_ADDRESS"))
238 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
240 /* Use default system bus address if none set in environment */
241 bus_connection_addresses[DBUS_BUS_SYSTEM] =
242 _dbus_strdup (DBUS_SYSTEM_BUS_DEFAULT_ADDRESS);
244 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
247 _dbus_verbose (" used default system bus \"%s\"\n",
248 bus_connection_addresses[DBUS_BUS_SYSTEM]);
251 _dbus_verbose (" used env var system bus \"%s\"\n",
252 bus_connection_addresses[DBUS_BUS_SYSTEM]);
254 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
256 _dbus_verbose ("Filling in session bus address...\n");
258 if (!init_session_address ())
261 _dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_SESSION] ?
262 bus_connection_addresses[DBUS_BUS_SESSION] : "none set");
265 if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
267 _dbus_verbose ("Filling in activation bus address...\n");
269 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_STARTER],
270 "DBUS_STARTER_ADDRESS"))
273 _dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_STARTER] ?
274 bus_connection_addresses[DBUS_BUS_STARTER] : "none set");
278 if (bus_connection_addresses[DBUS_BUS_STARTER] != NULL)
280 s = _dbus_getenv ("DBUS_STARTER_BUS_TYPE");
284 _dbus_verbose ("Bus activation type was set to \"%s\"\n", s);
286 if (strcmp (s, "system") == 0)
287 activation_bus_type = DBUS_BUS_SYSTEM;
288 else if (strcmp (s, "session") == 0)
289 activation_bus_type = DBUS_BUS_SESSION;
294 /* Default to the session bus instead if available */
295 if (bus_connection_addresses[DBUS_BUS_SESSION] != NULL)
297 bus_connection_addresses[DBUS_BUS_STARTER] =
298 _dbus_strdup (bus_connection_addresses[DBUS_BUS_SESSION]);
299 if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
304 /* If we return FALSE we have to be sure that restarting
305 * the above code will work right
308 if (!dbus_setenv ("DBUS_ACTIVATION_ADDRESS", NULL))
311 if (!dbus_setenv ("DBUS_ACTIVATION_BUS_TYPE", NULL))
314 if (!_dbus_register_shutdown_func (addresses_shutdown_func,
325 bus_data_free (void *data)
329 if (bd->is_well_known)
333 if (!_DBUS_LOCK (bus))
334 _dbus_assert_not_reached ("global locks should have been initialized "
335 "when we attached bus data");
337 /* We may be stored in more than one slot */
338 /* This should now be impossible - these slots are supposed to
339 * be cleared on disconnect, so should not need to be cleared on
343 while (i < N_BUS_TYPES)
345 if (bus_connections[i] == bd->connection)
346 bus_connections[i] = NULL;
353 dbus_free (bd->unique_name);
356 dbus_connection_free_data_slot (&bus_data_slot);
360 ensure_bus_data (DBusConnection *connection)
364 if (!dbus_connection_allocate_data_slot (&bus_data_slot))
367 bd = dbus_connection_get_data (connection, bus_data_slot);
370 bd = dbus_new0 (BusData, 1);
373 dbus_connection_free_data_slot (&bus_data_slot);
377 bd->connection = connection;
379 if (!dbus_connection_set_data (connection, bus_data_slot, bd,
383 dbus_connection_free_data_slot (&bus_data_slot);
387 /* Data slot refcount now held by the BusData */
391 dbus_connection_free_data_slot (&bus_data_slot);
398 * Internal function that checks to see if this
399 * is a shared connection owned by the bus and if it is unref it.
401 * @param connection a connection that has been disconnected.
404 _dbus_bus_notify_shared_connection_disconnected_unlocked (DBusConnection *connection)
408 if (!_DBUS_LOCK (bus))
410 /* If it was in bus_connections, we would have initialized global locks
411 * when we added it. So, it can't be. */
415 /* We are expecting to have the connection saved in only one of these
416 * slots, but someone could in a pathological case set system and session
417 * bus to the same bus or something. Or set one of them to the starter
418 * bus without setting the starter bus type in the env variable.
419 * So we don't break the loop as soon as we find a match.
421 for (i = 0; i < N_BUS_TYPES; ++i)
423 if (bus_connections[i] == connection)
425 bus_connections[i] = NULL;
432 static DBusConnection *
433 internal_bus_get (DBusBusType type,
438 DBusConnection *connection;
440 DBusBusType address_type;
442 _dbus_return_val_if_fail (type >= 0 && type < N_BUS_TYPES, NULL);
443 _dbus_return_val_if_error_is_set (error, NULL);
447 if (!_DBUS_LOCK (bus))
449 _DBUS_SET_OOM (error);
450 /* do not "goto out", that would try to unlock */
454 if (!init_connections_unlocked ())
456 _DBUS_SET_OOM (error);
460 /* We want to use the activation address even if the
461 * activating bus is the session or system bus,
466 /* Use the real type of the activation bus for getting its
467 * connection, but only if the real type's address is available. (If
468 * the activating bus isn't a well-known bus then
469 * activation_bus_type == DBUS_BUS_STARTER)
471 if (type == DBUS_BUS_STARTER &&
472 bus_connection_addresses[activation_bus_type] != NULL)
473 type = activation_bus_type;
475 if (!private && bus_connections[type] != NULL)
477 connection = bus_connections[type];
478 dbus_connection_ref (connection);
482 address = bus_connection_addresses[address_type];
485 dbus_set_error (error, DBUS_ERROR_FAILED,
486 "Unable to determine the address of the message bus (try 'man dbus-launch' and 'man dbus-daemon' for help)");
491 connection = dbus_connection_open_private (address, error);
493 connection = dbus_connection_open (address, error);
500 if (!dbus_bus_register (connection, error))
502 _dbus_connection_close_possibly_shared (connection);
503 dbus_connection_unref (connection);
510 /* store a weak ref to the connection (dbus-connection.c is
511 * supposed to have a strong ref that it drops on disconnect,
512 * since this is a shared connection)
514 bus_connections[type] = connection;
517 /* By default we're bound to the lifecycle of
520 dbus_connection_set_exit_on_disconnect (connection,
523 if (!_DBUS_LOCK (bus_datas))
524 _dbus_assert_not_reached ("global locks were initialized already");
526 bd = ensure_bus_data (connection);
527 _dbus_assert (bd != NULL); /* it should have been created on
528 register, so OOM not possible */
529 bd->is_well_known = TRUE;
530 _DBUS_UNLOCK (bus_datas);
533 /* Return a reference to the caller, or NULL with error set. */
534 if (connection == NULL)
535 _DBUS_ASSERT_ERROR_IS_SET (error);
542 /** @} */ /* end of implementation details docs */
545 * @addtogroup DBusBus
550 * Connects to a bus daemon and registers the client with it. If a
551 * connection to the bus already exists, then that connection is
552 * returned. The caller of this function owns a reference to the bus.
554 * The caller may NOT call dbus_connection_close() on this connection;
555 * see dbus_connection_open() and dbus_connection_close() for details
558 * If this function obtains a new connection object never before
559 * returned from dbus_bus_get(), it will call
560 * dbus_connection_set_exit_on_disconnect(), so the application
561 * will exit if the connection closes. You can undo this
562 * by calling dbus_connection_set_exit_on_disconnect() yourself
563 * after you get the connection.
565 * dbus_bus_get() calls dbus_bus_register() for you.
567 * If returning a newly-created connection, this function will block
568 * until authentication and bus registration are complete.
570 * @param type bus type
571 * @param error address where an error can be returned.
572 * @returns a #DBusConnection with new ref or #NULL on error
575 dbus_bus_get (DBusBusType type,
578 return internal_bus_get (type, FALSE, error);
582 * Connects to a bus daemon and registers the client with it as with
583 * dbus_bus_register(). Unlike dbus_bus_get(), always creates a new
584 * connection. This connection will not be saved or recycled by
585 * libdbus. Caller owns a reference to the bus and must either close
586 * it or know it to be closed prior to releasing this reference.
588 * See dbus_connection_open_private() for more details on when to
589 * close and unref this connection.
591 * This function calls
592 * dbus_connection_set_exit_on_disconnect() on the new connection, so the application
593 * will exit if the connection closes. You can undo this
594 * by calling dbus_connection_set_exit_on_disconnect() yourself
595 * after you get the connection.
597 * dbus_bus_get_private() calls dbus_bus_register() for you.
599 * This function will block until authentication and bus registration
602 * @param type bus type
603 * @param error address where an error can be returned.
604 * @returns a DBusConnection with new ref
607 dbus_bus_get_private (DBusBusType type,
610 return internal_bus_get (type, TRUE, error);
614 * Registers a connection with the bus. This must be the first
615 * thing an application does when connecting to the message bus.
616 * If registration succeeds, the unique name will be set,
617 * and can be obtained using dbus_bus_get_unique_name().
619 * This function will block until registration is complete.
621 * If the connection has already registered with the bus
622 * (determined by checking whether dbus_bus_get_unique_name()
623 * returns a non-#NULL value), then this function does nothing.
625 * If you use dbus_bus_get() or dbus_bus_get_private() this
626 * function will be called for you.
628 * @note Just use dbus_bus_get() or dbus_bus_get_private() instead of
629 * dbus_bus_register() and save yourself some pain. Using
630 * dbus_bus_register() manually is only useful if you have your
631 * own custom message bus not found in #DBusBusType.
633 * If you open a bus connection with dbus_connection_open() or
634 * dbus_connection_open_private() you will have to dbus_bus_register()
635 * yourself, or make the appropriate registration method calls
636 * yourself. If you send the method calls yourself, call
637 * dbus_bus_set_unique_name() with the unique bus name you get from
640 * For shared connections (created with dbus_connection_open()) in a
641 * multithreaded application, you can't really make the registration
642 * calls yourself, because you don't know whether some other thread is
643 * also registering, and the bus will kick you off if you send two
644 * registration messages.
646 * If you use dbus_bus_register() however, there is a lock that
647 * keeps both apps from registering at the same time.
649 * The rule in a multithreaded app, then, is that dbus_bus_register()
650 * must be used to register, or you need to have your own locks that
651 * all threads in the app will respect.
653 * In a single-threaded application you can register by hand instead
654 * of using dbus_bus_register(), as long as you check
655 * dbus_bus_get_unique_name() to see if a unique name has already been
656 * stored by another thread before you send the registration messages.
658 * @param connection the connection
659 * @param error place to store errors
660 * @returns #TRUE on success
663 dbus_bus_register (DBusConnection *connection,
666 DBusMessage *message, *reply;
672 _dbus_return_val_if_fail (connection != NULL, FALSE);
673 _dbus_return_val_if_error_is_set (error, FALSE);
679 if (!_DBUS_LOCK (bus_datas))
681 _DBUS_SET_OOM (error);
682 /* do not "goto out", that would try to unlock */
686 bd = ensure_bus_data (connection);
689 _DBUS_SET_OOM (error);
693 if (bd->unique_name != NULL)
695 _dbus_verbose ("Ignoring attempt to register the same DBusConnection %s with the message bus a second time.\n",
701 if(dbus_connection_is_kdbus(connection))
703 name = malloc(snprintf(name, 0, "%llu", ULLONG_MAX) + 1);
704 if(!bus_register_kdbus(name, connection, error))
707 if(!bus_register_policy_kdbus(name, connection, error))
710 dbus_connection_set_is_authenticated(connection);
712 /* Set sender for messages.
713 * If not kdbus - daemon does this. */
714 sender = malloc (strlen(name) + 4);
717 sprintf(sender, ":1.%s", name);
718 dbus_connection_set_sender(connection, sender);
722 _DBUS_SET_OOM (error);
728 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
734 _DBUS_SET_OOM (error);
738 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
742 else if (dbus_set_error_from_message (error, reply))
744 else if (!dbus_message_get_args (reply, error,
745 DBUS_TYPE_STRING, &name,
750 bd->unique_name = _dbus_strdup (name);
752 if (bd->unique_name == NULL)
754 _DBUS_SET_OOM (error);
757 //_dbus_verbose("-- Our uniqe name is: %s\n", bd->unique_name);
761 _DBUS_UNLOCK (bus_datas);
764 dbus_message_unref (message);
769 dbus_message_unref (reply);
772 _DBUS_ASSERT_ERROR_IS_SET (error);
779 * Sets the unique name of the connection, as assigned by the message
780 * bus. Can only be used if you registered with the bus manually
781 * (i.e. if you did not call dbus_bus_register()). Can only be called
782 * once per connection. After the unique name is set, you can get it
783 * with dbus_bus_get_unique_name().
785 * The only reason to use this function is to re-implement the
786 * equivalent of dbus_bus_register() yourself. One (probably unusual)
787 * reason to do that might be to do the bus registration call
788 * asynchronously instead of synchronously.
790 * @note Just use dbus_bus_get() or dbus_bus_get_private(), or worst
791 * case dbus_bus_register(), instead of messing with this
792 * function. There's really no point creating pain for yourself by
793 * doing things manually.
795 * It's hard to use this function safely on shared connections
796 * (created by dbus_connection_open()) in a multithreaded application,
797 * because only one registration attempt can be sent to the bus. If
798 * two threads are both sending the registration message, there is no
799 * mechanism in libdbus itself to avoid sending it twice.
801 * Thus, you need a way to coordinate which thread sends the
802 * registration attempt; which also means you know which thread
803 * will call dbus_bus_set_unique_name(). If you don't know
804 * about all threads in the app (for example, if some libraries
805 * you're using might start libdbus-using threads), then you
806 * need to avoid using this function on shared connections.
808 * @param connection the connection
809 * @param unique_name the unique name
810 * @returns #FALSE if not enough memory
813 dbus_bus_set_unique_name (DBusConnection *connection,
814 const char *unique_name)
817 dbus_bool_t success = FALSE;
819 _dbus_return_val_if_fail (connection != NULL, FALSE);
820 _dbus_return_val_if_fail (unique_name != NULL, FALSE);
822 if (!_DBUS_LOCK (bus_datas))
824 /* do not "goto out", that would try to unlock */
828 bd = ensure_bus_data (connection);
832 _dbus_assert (bd->unique_name == NULL);
834 bd->unique_name = _dbus_strdup (unique_name);
835 success = bd->unique_name != NULL;
838 _DBUS_UNLOCK (bus_datas);
844 * Gets the unique name of the connection as assigned by the message
845 * bus. Only possible after the connection has been registered with
846 * the message bus. All connections returned by dbus_bus_get() or
847 * dbus_bus_get_private() have been successfully registered.
849 * The name remains valid until the connection is freed, and
850 * should not be freed by the caller.
852 * Other than dbus_bus_get(), there are two ways to set the unique
853 * name; one is dbus_bus_register(), the other is
854 * dbus_bus_set_unique_name(). You are responsible for calling
855 * dbus_bus_set_unique_name() if you register by hand instead of using
856 * dbus_bus_register().
858 * @param connection the connection
859 * @returns the unique name or #NULL on error
862 dbus_bus_get_unique_name (DBusConnection *connection)
865 const char *unique_name = NULL;
867 _dbus_return_val_if_fail (connection != NULL, NULL);
869 if (!_DBUS_LOCK (bus_datas))
871 /* We'd have initialized locks when we gave it its unique name, if it
872 * had one. Don't "goto out", that would try to unlock. */
876 bd = ensure_bus_data (connection);
880 unique_name = bd->unique_name;
883 _DBUS_UNLOCK (bus_datas);
889 * Asks the bus to return the UID the named connection authenticated
890 * as, if any. Only works on UNIX; only works for connections on the
891 * same machine as the bus. If you are not on the same machine as the
892 * bus, then calling this is probably a bad idea, since the UID will
893 * mean little to your application.
895 * For the system message bus you're guaranteed to be on the same
896 * machine since it only listens on a UNIX domain socket (at least,
897 * as shipped by default).
899 * This function only works for connections that authenticated as
900 * a UNIX user, right now that includes all bus connections, but
901 * it's very possible to have connections with no associated UID.
902 * So check for errors and do something sensible if they happen.
904 * This function will always return an error on Windows.
906 * @param connection the connection
907 * @param name a name owned by the connection
908 * @param error location to store the error
909 * @returns the unix user id, or ((unsigned)-1) if error is set
912 dbus_bus_get_unix_user (DBusConnection *connection,
916 DBusMessage *message, *reply;
919 _dbus_return_val_if_fail (connection != NULL, DBUS_UID_UNSET);
920 _dbus_return_val_if_fail (name != NULL, DBUS_UID_UNSET);
921 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), DBUS_UID_UNSET);
922 _dbus_return_val_if_error_is_set (error, DBUS_UID_UNSET);
924 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
927 "GetConnectionUnixUser");
931 _DBUS_SET_OOM (error);
932 return DBUS_UID_UNSET;
935 if (!dbus_message_append_args (message,
936 DBUS_TYPE_STRING, &name,
939 dbus_message_unref (message);
940 _DBUS_SET_OOM (error);
941 return DBUS_UID_UNSET;
944 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
947 dbus_message_unref (message);
951 _DBUS_ASSERT_ERROR_IS_SET (error);
952 return DBUS_UID_UNSET;
955 if (dbus_set_error_from_message (error, reply))
957 _DBUS_ASSERT_ERROR_IS_SET (error);
958 dbus_message_unref (reply);
959 return DBUS_UID_UNSET;
962 if (!dbus_message_get_args (reply, error,
963 DBUS_TYPE_UINT32, &uid,
966 _DBUS_ASSERT_ERROR_IS_SET (error);
967 dbus_message_unref (reply);
968 return DBUS_UID_UNSET;
971 dbus_message_unref (reply);
973 return (unsigned long) uid;
977 * Asks the bus to return its globally unique ID, as described in the
978 * D-Bus specification. For the session bus, this is useful as a way
979 * to uniquely identify each user session. For the system bus,
980 * probably the bus ID is not useful; instead, use the machine ID
981 * since it's accessible without necessarily connecting to the bus and
982 * may be persistent beyond a single bus instance (across reboots for
983 * example). See dbus_get_local_machine_id().
985 * In addition to an ID for each bus and an ID for each machine, there is
986 * an ID for each address that the bus is listening on; that can
987 * be retrieved with dbus_connection_get_server_id(), though it is
988 * probably not very useful.
990 * @param connection the connection
991 * @param error location to store the error
992 * @returns the bus ID or #NULL if error is set
995 dbus_bus_get_id (DBusConnection *connection,
998 DBusMessage *message, *reply;
1000 const char *v_STRING;
1002 _dbus_return_val_if_fail (connection != NULL, NULL);
1003 _dbus_return_val_if_error_is_set (error, NULL);
1005 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1007 DBUS_INTERFACE_DBUS,
1010 if (message == NULL)
1012 _DBUS_SET_OOM (error);
1016 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
1019 dbus_message_unref (message);
1023 _DBUS_ASSERT_ERROR_IS_SET (error);
1027 if (dbus_set_error_from_message (error, reply))
1029 _DBUS_ASSERT_ERROR_IS_SET (error);
1030 dbus_message_unref (reply);
1035 if (!dbus_message_get_args (reply, error,
1036 DBUS_TYPE_STRING, &v_STRING,
1039 _DBUS_ASSERT_ERROR_IS_SET (error);
1040 dbus_message_unref (reply);
1044 id = _dbus_strdup (v_STRING); /* may be NULL */
1046 dbus_message_unref (reply);
1049 _DBUS_SET_OOM (error);
1051 /* FIXME it might be nice to cache the ID locally */
1057 * Asks the bus to assign the given name to this connection by invoking
1058 * the RequestName method on the bus. This method is fully documented
1059 * in the D-Bus specification. For quick reference, the flags and
1060 * result codes are discussed here, but the specification is the
1061 * canonical version of this information.
1063 * First you should know that for each bus name, the bus stores
1064 * a queue of connections that would like to own it. Only
1065 * one owns it at a time - called the primary owner. If the primary
1066 * owner releases the name or disconnects, then the next owner in the
1067 * queue atomically takes over.
1069 * So for example if you have an application org.freedesktop.TextEditor
1070 * and multiple instances of it can be run, you can have all of them
1071 * sitting in the queue. The first one to start up will receive messages
1072 * sent to org.freedesktop.TextEditor, but if that one exits another
1073 * will become the primary owner and receive messages.
1075 * The queue means you don't need to manually watch for the current owner to
1076 * disappear and then request the name again.
1078 * When requesting a name, you can specify several flags.
1080 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT and #DBUS_NAME_FLAG_DO_NOT_QUEUE
1081 * are properties stored by the bus for this connection with respect to
1082 * each requested bus name. These properties are stored even if the
1083 * connection is queued and does not become the primary owner.
1084 * You can update these flags by calling RequestName again (even if
1085 * you already own the name).
1087 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT means that another requestor of the
1088 * name can take it away from you by specifying #DBUS_NAME_FLAG_REPLACE_EXISTING.
1090 * #DBUS_NAME_FLAG_DO_NOT_QUEUE means that if you aren't the primary owner,
1091 * you don't want to be queued up - you only care about being the
1094 * Unlike the other two flags, #DBUS_NAME_FLAG_REPLACE_EXISTING is a property
1095 * of the individual RequestName call, i.e. the bus does not persistently
1096 * associate it with the connection-name pair. If a RequestName call includes
1097 * the #DBUS_NAME_FLAG_REPLACE_EXISTING flag, and the current primary
1098 * owner has #DBUS_NAME_FLAG_ALLOW_REPLACEMENT set, then the current primary
1099 * owner will be kicked off.
1101 * If no flags are given, an application will receive the requested
1102 * name only if the name is currently unowned; and it will NOT give
1103 * up the name if another application asks to take it over using
1104 * #DBUS_NAME_FLAG_REPLACE_EXISTING.
1106 * This function returns a result code. The possible result codes
1109 * #DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER means that the name had no
1110 * existing owner, and the caller is now the primary owner; or that
1111 * the name had an owner, and the caller specified
1112 * #DBUS_NAME_FLAG_REPLACE_EXISTING, and the current owner
1113 * specified #DBUS_NAME_FLAG_ALLOW_REPLACEMENT.
1115 * #DBUS_REQUEST_NAME_REPLY_IN_QUEUE happens only if the caller does NOT
1116 * specify #DBUS_NAME_FLAG_DO_NOT_QUEUE and either the current owner
1117 * did NOT specify #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or the caller did NOT
1118 * specify #DBUS_NAME_FLAG_REPLACE_EXISTING. In this case the caller ends up
1119 * in a queue to own the name after the current owner gives it up.
1121 * #DBUS_REQUEST_NAME_REPLY_EXISTS happens if the name has an owner
1122 * already and the caller specifies #DBUS_NAME_FLAG_DO_NOT_QUEUE
1123 * and either the current owner has NOT specified
1124 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or the caller did NOT specify
1125 * #DBUS_NAME_FLAG_REPLACE_EXISTING.
1127 * #DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER happens if an application
1128 * requests a name it already owns. (Re-requesting a name is useful if
1129 * you want to change the #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or
1130 * #DBUS_NAME_FLAG_DO_NOT_QUEUE settings.)
1132 * When a service represents an application, say "text editor," then
1133 * it should specify #DBUS_NAME_FLAG_ALLOW_REPLACEMENT if it wants
1134 * the last editor started to be the user's editor vs. the first one
1135 * started. Then any editor that can be the user's editor should
1136 * specify #DBUS_NAME_FLAG_REPLACE_EXISTING to either take over
1137 * (last-started-wins) or be queued up (first-started-wins) according
1138 * to whether #DBUS_NAME_FLAG_ALLOW_REPLACEMENT was given.
1140 * Conventionally, single-instance applications often offer a command
1141 * line option called --replace which means to replace the current
1142 * instance. To implement this, always set
1143 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT when you request your
1144 * application's bus name. When you lose ownership of your bus name,
1145 * you need to exit. Look for the signal "NameLost" from
1146 * #DBUS_SERVICE_DBUS and #DBUS_INTERFACE_DBUS (the signal's first
1147 * argument is the bus name that was lost). If starting up without
1148 * --replace, do not specify #DBUS_NAME_FLAG_REPLACE_EXISTING, and
1149 * exit if you fail to become the bus name owner. If --replace is
1150 * given, ask to replace the old owner.
1152 * @param connection the connection
1153 * @param name the name to request
1154 * @param flags flags
1155 * @param error location to store the error
1156 * @returns a result code, -1 if error is set
1159 dbus_bus_request_name (DBusConnection *connection,
1164 dbus_uint32_t result;
1166 _dbus_return_val_if_fail (connection != NULL, 0);
1167 _dbus_return_val_if_fail (name != NULL, 0);
1168 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
1169 _dbus_return_val_if_error_is_set (error, 0);
1171 if(!dbus_connection_is_kdbus(connection))
1173 DBusMessage *message, *reply;
1175 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1177 DBUS_INTERFACE_DBUS,
1179 if (message == NULL)
1181 _DBUS_SET_OOM (error);
1185 if (!dbus_message_append_args (message,
1186 DBUS_TYPE_STRING, &name,
1187 DBUS_TYPE_UINT32, &flags,
1190 dbus_message_unref (message);
1191 _DBUS_SET_OOM (error);
1195 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
1198 dbus_message_unref (message);
1202 _DBUS_ASSERT_ERROR_IS_SET (error);
1206 if (dbus_set_error_from_message (error, reply))
1208 _DBUS_ASSERT_ERROR_IS_SET (error);
1209 dbus_message_unref (reply);
1213 if (!dbus_message_get_args (reply, error,
1214 DBUS_TYPE_UINT32, &result,
1217 _DBUS_ASSERT_ERROR_IS_SET (error);
1218 dbus_message_unref (reply);
1222 dbus_message_unref (reply);
1226 if(!bus_register_policy_kdbus(name, connection, error))
1229 result = bus_request_name_kdbus(connection, name, flags, error);
1237 * Asks the bus to unassign the given name from this connection by
1238 * invoking the ReleaseName method on the bus. The "ReleaseName"
1239 * method is canonically documented in the D-Bus specification.
1241 * Possible results are: #DBUS_RELEASE_NAME_REPLY_RELEASED
1242 * which means you owned the name or were in the queue to own it,
1243 * and and now you don't own it and aren't in the queue.
1244 * #DBUS_RELEASE_NAME_REPLY_NOT_OWNER which means someone else
1245 * owns the name so you can't release it.
1246 * #DBUS_RELEASE_NAME_REPLY_NON_EXISTENT
1247 * which means nobody owned the name.
1249 * @param connection the connection
1250 * @param name the name to remove
1251 * @param error location to store the error
1252 * @returns a result code, -1 if error is set
1255 dbus_bus_release_name (DBusConnection *connection,
1259 DBusMessage *message, *reply;
1260 dbus_uint32_t result;
1262 _dbus_return_val_if_fail (connection != NULL, 0);
1263 _dbus_return_val_if_fail (name != NULL, 0);
1264 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
1265 _dbus_return_val_if_error_is_set (error, 0);
1267 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1269 DBUS_INTERFACE_DBUS,
1272 if (message == NULL)
1274 _DBUS_SET_OOM (error);
1278 if (!dbus_message_append_args (message,
1279 DBUS_TYPE_STRING, &name,
1282 dbus_message_unref (message);
1283 _DBUS_SET_OOM (error);
1287 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
1290 dbus_message_unref (message);
1294 _DBUS_ASSERT_ERROR_IS_SET (error);
1298 if (dbus_set_error_from_message (error, reply))
1300 _DBUS_ASSERT_ERROR_IS_SET (error);
1301 dbus_message_unref (reply);
1305 if (!dbus_message_get_args (reply, error,
1306 DBUS_TYPE_UINT32, &result,
1309 _DBUS_ASSERT_ERROR_IS_SET (error);
1310 dbus_message_unref (reply);
1314 dbus_message_unref (reply);
1320 * Asks the bus whether a certain name has an owner.
1322 * Using this can easily result in a race condition,
1323 * since an owner can appear or disappear after you
1326 * If you want to request a name, just request it;
1327 * if you want to avoid replacing a current owner,
1328 * don't specify #DBUS_NAME_FLAG_REPLACE_EXISTING and
1329 * you will get an error if there's already an owner.
1331 * @param connection the connection
1332 * @param name the name
1333 * @param error location to store any errors
1334 * @returns #TRUE if the name exists, #FALSE if not or on error
1337 dbus_bus_name_has_owner (DBusConnection *connection,
1341 DBusMessage *message, *reply;
1344 _dbus_return_val_if_fail (connection != NULL, FALSE);
1345 _dbus_return_val_if_fail (name != NULL, FALSE);
1346 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
1347 _dbus_return_val_if_error_is_set (error, FALSE);
1349 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1351 DBUS_INTERFACE_DBUS,
1353 if (message == NULL)
1355 _DBUS_SET_OOM (error);
1359 if (!dbus_message_append_args (message,
1360 DBUS_TYPE_STRING, &name,
1363 dbus_message_unref (message);
1364 _DBUS_SET_OOM (error);
1368 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
1369 dbus_message_unref (message);
1373 _DBUS_ASSERT_ERROR_IS_SET (error);
1377 if (!dbus_message_get_args (reply, error,
1378 DBUS_TYPE_BOOLEAN, &exists,
1381 _DBUS_ASSERT_ERROR_IS_SET (error);
1382 dbus_message_unref (reply);
1386 dbus_message_unref (reply);
1391 * Starts a service that will request ownership of the given name.
1392 * The returned result will be one of be one of
1393 * #DBUS_START_REPLY_SUCCESS or #DBUS_START_REPLY_ALREADY_RUNNING if
1394 * successful. Pass #NULL if you don't care about the result.
1396 * The flags parameter is for future expansion, currently you should
1399 * It's often easier to avoid explicitly starting services, and
1400 * just send a method call to the service's bus name instead.
1401 * Method calls start a service to handle them by default
1402 * unless you call dbus_message_set_auto_start() to disable this
1405 * @param connection the connection
1406 * @param name the name we want the new service to request
1407 * @param flags the flags (should always be 0 for now)
1408 * @param result a place to store the result or #NULL
1409 * @param error location to store any errors
1410 * @returns #TRUE if the activation succeeded, #FALSE if not
1413 dbus_bus_start_service_by_name (DBusConnection *connection,
1415 dbus_uint32_t flags,
1416 dbus_uint32_t *result,
1422 _dbus_return_val_if_fail (connection != NULL, FALSE);
1423 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
1425 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1427 DBUS_INTERFACE_DBUS,
1428 "StartServiceByName");
1430 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &name,
1431 DBUS_TYPE_UINT32, &flags, DBUS_TYPE_INVALID))
1433 dbus_message_unref (msg);
1434 _DBUS_SET_OOM (error);
1438 reply = dbus_connection_send_with_reply_and_block (connection, msg,
1440 dbus_message_unref (msg);
1444 _DBUS_ASSERT_ERROR_IS_SET (error);
1448 if (dbus_set_error_from_message (error, reply))
1450 _DBUS_ASSERT_ERROR_IS_SET (error);
1451 dbus_message_unref (reply);
1455 if (result != NULL &&
1456 !dbus_message_get_args (reply, error, DBUS_TYPE_UINT32,
1457 result, DBUS_TYPE_INVALID))
1459 _DBUS_ASSERT_ERROR_IS_SET (error);
1460 dbus_message_unref (reply);
1464 dbus_message_unref (reply);
1469 send_no_return_values (DBusConnection *connection,
1475 /* Block to check success codepath */
1478 reply = dbus_connection_send_with_reply_and_block (connection, msg,
1482 _DBUS_ASSERT_ERROR_IS_SET (error);
1484 dbus_message_unref (reply);
1488 /* Silently-fail nonblocking codepath */
1489 dbus_message_set_no_reply (msg, TRUE);
1490 dbus_connection_send (connection, msg, NULL);
1495 * Adds a match rule to match messages going through the message bus.
1496 * The "rule" argument is the string form of a match rule.
1498 * If you pass #NULL for the error, this function will not
1499 * block; the match thus won't be added until you flush the
1500 * connection, and if there's an error adding the match
1501 * you won't find out about it. This is generally acceptable, since the
1502 * possible errors (including a lack of resources in the bus, the connection
1503 * having exceeded its quota of active match rules, or the match rule being
1504 * unparseable) are generally unrecoverable.
1506 * If you pass non-#NULL for the error this function will
1507 * block until it gets a reply. This may be useful when using match rule keys
1508 * introduced in recent versions of D-Bus, like 'arg0namespace', to allow the
1509 * application to fall back to less efficient match rules supported by older
1510 * versions of the daemon if the running version is not new enough; or when
1511 * using user-supplied rules rather than rules hard-coded at compile time.
1513 * Normal API conventions would have the function return
1514 * a boolean value indicating whether the error was set,
1515 * but that would require blocking always to determine
1518 * The AddMatch method is fully documented in the D-Bus
1519 * specification. For quick reference, the format of the
1520 * match rules is discussed here, but the specification
1521 * is the canonical version of this information.
1523 * Rules are specified as a string of comma separated
1524 * key/value pairs. An example is
1525 * "type='signal',sender='org.freedesktop.DBus',
1526 * interface='org.freedesktop.DBus',member='Foo',
1527 * path='/bar/foo',destination=':452345.34'"
1529 * Possible keys you can match on are type, sender,
1530 * interface, member, path, destination and numbered
1531 * keys to match message args (keys are 'arg0', 'arg1', etc.).
1532 * Omitting a key from the rule indicates
1533 * a wildcard match. For instance omitting
1534 * the member from a match rule but adding a sender would
1535 * let all messages from that sender through regardless of
1538 * Matches are inclusive not exclusive so as long as one
1539 * rule matches the message will get through. It is important
1540 * to note this because every time a message is received the
1541 * application will be paged into memory to process it. This
1542 * can cause performance problems such as draining batteries
1543 * on embedded platforms.
1545 * If you match message args ('arg0', 'arg1', and so forth)
1546 * only string arguments will match. That is, arg0='5' means
1547 * match the string "5" not the integer 5.
1549 * Currently there is no way to match against non-string arguments.
1551 * A specialised form of wildcard matching on arguments is
1552 * supported for path-like namespaces. If your argument match has
1553 * a 'path' suffix (eg: "arg0path='/some/path/'") then it is
1554 * considered a match if the argument exactly matches the given
1555 * string or if one of them ends in a '/' and is a prefix of the
1558 * Matching on interface is tricky because method call
1559 * messages only optionally specify the interface.
1560 * If a message omits the interface, then it will NOT match
1561 * if the rule specifies an interface name. This means match
1562 * rules on method calls should not usually give an interface.
1564 * However, signal messages are required to include the interface
1565 * so when matching signals usually you should specify the interface
1566 * in the match rule.
1568 * For security reasons, you can match arguments only up to
1569 * #DBUS_MAXIMUM_MATCH_RULE_ARG_NUMBER.
1571 * Match rules have a maximum length of #DBUS_MAXIMUM_MATCH_RULE_LENGTH
1574 * Both of these maximums are much higher than you're likely to need,
1575 * they only exist because the D-Bus bus daemon has fixed limits on
1576 * all resource usage.
1578 * @param connection connection to the message bus
1579 * @param rule textual form of match rule
1580 * @param error location to store any errors
1583 dbus_bus_add_match (DBusConnection *connection,
1587 _dbus_return_if_fail (rule != NULL);
1589 if(!dbus_connection_is_kdbus(connection))
1593 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1595 DBUS_INTERFACE_DBUS,
1600 _DBUS_SET_OOM (error);
1604 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
1607 dbus_message_unref (msg);
1608 _DBUS_SET_OOM (error);
1612 send_no_return_values (connection, msg, error);
1614 dbus_message_unref (msg);
1617 dbus_bus_add_match_kdbus(connection, rule, error);
1621 * Removes a previously-added match rule "by value" (the most
1622 * recently-added identical rule gets removed). The "rule" argument
1623 * is the string form of a match rule.
1625 * The bus compares match rules semantically, not textually, so
1626 * whitespace and ordering don't have to be identical to
1627 * the rule you passed to dbus_bus_add_match().
1629 * If you pass #NULL for the error, this function will not
1630 * block; otherwise it will. See detailed explanation in
1631 * docs for dbus_bus_add_match().
1633 * @param connection connection to the message bus
1634 * @param rule textual form of match rule
1635 * @param error location to store any errors
1638 dbus_bus_remove_match (DBusConnection *connection,
1642 if(!dbus_connection_is_kdbus(connection))
1646 _dbus_return_if_fail (rule != NULL);
1648 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1650 DBUS_INTERFACE_DBUS,
1653 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
1656 dbus_message_unref (msg);
1657 _DBUS_SET_OOM (error);
1661 send_no_return_values (connection, msg, error);
1663 dbus_message_unref (msg);
1666 dbus_bus_remove_match_kdbus(connection, error);