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-threads-internal.h"
32 #include "dbus-connection-internal.h"
33 #include "dbus-string.h"
34 #include "dbus-transport-kdbus.h"
38 * @defgroup DBusBus Message bus APIs
40 * @brief Functions for communicating with the message bus
42 * dbus_bus_get() allows all modules and libraries in a given
43 * process to share the same connection to the bus daemon by storing
44 * the connection globally.
46 * All other functions in this module are just convenience functions;
47 * most of them invoke methods on the bus daemon, by sending method
48 * call messages to #DBUS_SERVICE_DBUS. These convenience functions
49 * often make blocking method calls. If you don't want to block,
50 * you can send the method call messages manually in the same way
51 * you would any other method call message.
53 * This module is the only one in libdbus that's specific to
54 * communicating with the message bus daemon. The rest of the API can
55 * also be used for connecting to another application directly.
57 * @todo right now the default address of the system bus is hardcoded,
58 * so if you change it in the global config file suddenly you have to
59 * set DBUS_SYSTEM_BUS_ADDRESS env variable. Might be nice if the
60 * client lib somehow read the config file, or if the bus on startup
61 * somehow wrote out its address to a well-known spot, but might also
66 * @defgroup DBusBusInternals Message bus APIs internals
67 * @ingroup DBusInternals
68 * @brief Internals of functions for communicating with the message bus
74 * Block of message-bus-related data we attach to each
75 * #DBusConnection used with these convenience functions.
80 DBusConnection *connection; /**< Connection we're associated with */
81 char *unique_name; /**< Unique name of this connection */
83 unsigned int is_well_known : 1; /**< Is one of the well-known connections in our global array */
86 /** The slot we have reserved to store BusData.
88 static dbus_int32_t bus_data_slot = -1;
90 /** Number of bus types */
93 static DBusConnection *bus_connections[N_BUS_TYPES];
94 static char *bus_connection_addresses[N_BUS_TYPES] = { NULL, NULL, NULL };
96 static DBusBusType activation_bus_type = DBUS_BUS_STARTER;
98 static dbus_bool_t initialized = FALSE;
101 addresses_shutdown_func (void *data)
106 while (i < N_BUS_TYPES)
108 if (bus_connections[i] != NULL)
109 _dbus_warn_check_failed ("dbus_shutdown() called but connections were still live. This probably means the application did not drop all its references to bus connections.\n");
111 dbus_free (bus_connection_addresses[i]);
112 bus_connection_addresses[i] = NULL;
116 activation_bus_type = DBUS_BUS_STARTER;
122 get_from_env (char **connection_p,
127 _dbus_assert (*connection_p == NULL);
129 s = _dbus_getenv (env_var);
130 if (s == NULL || *s == '\0')
131 return TRUE; /* successfully didn't use the env var */
134 *connection_p = _dbus_strdup (s);
135 return *connection_p != NULL;
140 init_session_address (void)
146 /* First, look in the environment. This is the normal case on
147 * freedesktop.org/Unix systems. */
148 get_from_env (&bus_connection_addresses[DBUS_BUS_SESSION],
149 "DBUS_SESSION_BUS_ADDRESS");
150 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
152 dbus_bool_t supported;
154 DBusError error = DBUS_ERROR_INIT;
156 if (!_dbus_string_init (&addr))
160 /* So it's not in the environment - let's try a platform-specific method.
161 * On MacOS, this involves asking launchd. On Windows (not specified yet)
162 * we might do a COM lookup.
163 * Ignore errors - if we failed, fall back to autolaunch. */
164 retval = _dbus_lookup_session_address (&supported, &addr, &error);
165 if (supported && retval)
167 retval =_dbus_string_steal_data (&addr, &bus_connection_addresses[DBUS_BUS_SESSION]);
169 else if (supported && !retval)
171 if (dbus_error_is_set(&error))
172 _dbus_warn ("Dynamic session lookup supported but failed: %s\n", error.message);
174 _dbus_warn ("Dynamic session lookup supported but failed silently\n");
176 _dbus_string_free (&addr);
184 /* We have a hard-coded (but compile-time-configurable) fallback address for
185 * the session bus. */
186 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
187 bus_connection_addresses[DBUS_BUS_SESSION] =
188 _dbus_strdup (DBUS_SESSION_BUS_CONNECT_ADDRESS);
190 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
197 init_connections_unlocked (void)
205 while (i < N_BUS_TYPES)
207 bus_connections[i] = NULL;
211 /* Don't init these twice, we may run this code twice if
212 * init_connections_unlocked() fails midway through.
213 * In practice, each block below should contain only one
214 * "return FALSE" or running through twice may not
218 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
220 _dbus_verbose ("Filling in system bus address...\n");
222 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SYSTEM],
223 "DBUS_SYSTEM_BUS_ADDRESS"))
228 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
230 /* Use default system bus address if none set in environment */
231 bus_connection_addresses[DBUS_BUS_SYSTEM] =
232 _dbus_strdup (DBUS_SYSTEM_BUS_DEFAULT_ADDRESS);
234 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
237 _dbus_verbose (" used default system bus \"%s\"\n",
238 bus_connection_addresses[DBUS_BUS_SYSTEM]);
241 _dbus_verbose (" used env var system bus \"%s\"\n",
242 bus_connection_addresses[DBUS_BUS_SYSTEM]);
244 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
246 _dbus_verbose ("Filling in session bus address...\n");
248 if (!init_session_address ())
251 _dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_SESSION] ?
252 bus_connection_addresses[DBUS_BUS_SESSION] : "none set");
255 if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
257 _dbus_verbose ("Filling in activation bus address...\n");
259 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_STARTER],
260 "DBUS_STARTER_ADDRESS"))
263 _dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_STARTER] ?
264 bus_connection_addresses[DBUS_BUS_STARTER] : "none set");
268 if (bus_connection_addresses[DBUS_BUS_STARTER] != NULL)
270 s = _dbus_getenv ("DBUS_STARTER_BUS_TYPE");
274 _dbus_verbose ("Bus activation type was set to \"%s\"\n", s);
276 if (strcmp (s, "system") == 0)
277 activation_bus_type = DBUS_BUS_SYSTEM;
278 else if (strcmp (s, "session") == 0)
279 activation_bus_type = DBUS_BUS_SESSION;
284 /* Default to the session bus instead if available */
285 if (bus_connection_addresses[DBUS_BUS_SESSION] != NULL)
287 bus_connection_addresses[DBUS_BUS_STARTER] =
288 _dbus_strdup (bus_connection_addresses[DBUS_BUS_SESSION]);
289 if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
294 /* If we return FALSE we have to be sure that restarting
295 * the above code will work right
298 if (!_dbus_setenv ("DBUS_ACTIVATION_ADDRESS", NULL))
301 if (!_dbus_setenv ("DBUS_ACTIVATION_BUS_TYPE", NULL))
304 if (!_dbus_register_shutdown_func (addresses_shutdown_func,
315 bus_data_free (void *data)
319 if (bd->is_well_known)
323 if (!_DBUS_LOCK (bus))
324 _dbus_assert_not_reached ("global locks should have been initialized "
325 "when we attached bus data");
327 /* We may be stored in more than one slot */
328 /* This should now be impossible - these slots are supposed to
329 * be cleared on disconnect, so should not need to be cleared on
333 while (i < N_BUS_TYPES)
335 if (bus_connections[i] == bd->connection)
336 bus_connections[i] = NULL;
343 dbus_free (bd->unique_name);
346 dbus_connection_free_data_slot (&bus_data_slot);
350 ensure_bus_data (DBusConnection *connection)
354 if (!dbus_connection_allocate_data_slot (&bus_data_slot))
357 bd = dbus_connection_get_data (connection, bus_data_slot);
360 bd = dbus_new0 (BusData, 1);
363 dbus_connection_free_data_slot (&bus_data_slot);
367 bd->connection = connection;
369 if (!dbus_connection_set_data (connection, bus_data_slot, bd,
373 dbus_connection_free_data_slot (&bus_data_slot);
377 /* Data slot refcount now held by the BusData */
381 dbus_connection_free_data_slot (&bus_data_slot);
388 * Internal function that checks to see if this
389 * is a shared connection owned by the bus and if it is unref it.
391 * @param connection a connection that has been disconnected.
394 _dbus_bus_notify_shared_connection_disconnected_unlocked (DBusConnection *connection)
398 if (!_DBUS_LOCK (bus))
400 /* If it was in bus_connections, we would have initialized global locks
401 * when we added it. So, it can't be. */
405 /* We are expecting to have the connection saved in only one of these
406 * slots, but someone could in a pathological case set system and session
407 * bus to the same bus or something. Or set one of them to the starter
408 * bus without setting the starter bus type in the env variable.
409 * So we don't break the loop as soon as we find a match.
411 for (i = 0; i < N_BUS_TYPES; ++i)
413 if (bus_connections[i] == connection)
415 bus_connections[i] = NULL;
422 static DBusConnection *
423 internal_bus_get (DBusBusType type,
428 DBusConnection *connection;
430 DBusBusType address_type;
432 _dbus_return_val_if_fail (type >= 0 && type < N_BUS_TYPES, NULL);
433 _dbus_return_val_if_error_is_set (error, NULL);
437 if (!_DBUS_LOCK (bus))
439 _DBUS_SET_OOM (error);
440 /* do not "goto out", that would try to unlock */
444 if (!init_connections_unlocked ())
446 _DBUS_SET_OOM (error);
450 /* We want to use the activation address even if the
451 * activating bus is the session or system bus,
456 /* Use the real type of the activation bus for getting its
457 * connection, but only if the real type's address is available. (If
458 * the activating bus isn't a well-known bus then
459 * activation_bus_type == DBUS_BUS_STARTER)
461 if (type == DBUS_BUS_STARTER &&
462 bus_connection_addresses[activation_bus_type] != NULL)
463 type = activation_bus_type;
465 if (!private && bus_connections[type] != NULL)
467 connection = bus_connections[type];
468 dbus_connection_ref (connection);
472 address = bus_connection_addresses[address_type];
475 dbus_set_error (error, DBUS_ERROR_FAILED,
476 "Unable to determine the address of the message bus (try 'man dbus-launch' and 'man dbus-daemon' for help)");
481 connection = dbus_connection_open_private (address, error);
483 connection = dbus_connection_open (address, error);
490 _dbus_verbose (" !!! dbus_connection_open finished successfully !!!! \n"); //todo RP to be removed
492 if (!dbus_bus_register (connection, error))
494 _dbus_connection_close_possibly_shared (connection);
495 dbus_connection_unref (connection);
502 /* store a weak ref to the connection (dbus-connection.c is
503 * supposed to have a strong ref that it drops on disconnect,
504 * since this is a shared connection)
506 bus_connections[type] = connection;
509 /* By default we're bound to the lifecycle of
512 dbus_connection_set_exit_on_disconnect (connection,
515 if (!_DBUS_LOCK (bus_datas))
516 _dbus_assert_not_reached ("global locks were initialized already");
518 bd = ensure_bus_data (connection);
519 _dbus_assert (bd != NULL); /* it should have been created on
520 register, so OOM not possible */
521 bd->is_well_known = TRUE;
522 _DBUS_UNLOCK (bus_datas);
525 /* Return a reference to the caller, or NULL with error set. */
526 if (connection == NULL)
527 _DBUS_ASSERT_ERROR_IS_SET (error);
534 /** @} */ /* end of implementation details docs */
537 * @addtogroup DBusBus
542 * Connects to a bus daemon and registers the client with it. If a
543 * connection to the bus already exists, then that connection is
544 * returned. The caller of this function owns a reference to the bus.
546 * The caller may NOT call dbus_connection_close() on this connection;
547 * see dbus_connection_open() and dbus_connection_close() for details
550 * If this function obtains a new connection object never before
551 * returned from dbus_bus_get(), it will call
552 * dbus_connection_set_exit_on_disconnect(), so the application
553 * will exit if the connection closes. You can undo this
554 * by calling dbus_connection_set_exit_on_disconnect() yourself
555 * after you get the connection.
557 * dbus_bus_get() calls dbus_bus_register() for you.
559 * If returning a newly-created connection, this function will block
560 * until authentication and bus registration are complete.
562 * @param type bus type
563 * @param error address where an error can be returned.
564 * @returns a #DBusConnection with new ref
567 dbus_bus_get (DBusBusType type,
570 return internal_bus_get (type, FALSE, error);
574 * Connects to a bus daemon and registers the client with it as with
575 * dbus_bus_register(). Unlike dbus_bus_get(), always creates a new
576 * connection. This connection will not be saved or recycled by
577 * libdbus. Caller owns a reference to the bus and must either close
578 * it or know it to be closed prior to releasing this reference.
580 * See dbus_connection_open_private() for more details on when to
581 * close and unref this connection.
583 * This function calls
584 * dbus_connection_set_exit_on_disconnect() on the new connection, so the application
585 * will exit if the connection closes. You can undo this
586 * by calling dbus_connection_set_exit_on_disconnect() yourself
587 * after you get the connection.
589 * dbus_bus_get_private() calls dbus_bus_register() for you.
591 * This function will block until authentication and bus registration
594 * @param type bus type
595 * @param error address where an error can be returned.
596 * @returns a DBusConnection with new ref
599 dbus_bus_get_private (DBusBusType type,
602 return internal_bus_get (type, TRUE, error);
606 * Registers a connection with the bus. This must be the first
607 * thing an application does when connecting to the message bus.
608 * If registration succeeds, the unique name will be set,
609 * and can be obtained using dbus_bus_get_unique_name().
611 * This function will block until registration is complete.
613 * If the connection has already registered with the bus
614 * (determined by checking whether dbus_bus_get_unique_name()
615 * returns a non-#NULL value), then this function does nothing.
617 * If you use dbus_bus_get() or dbus_bus_get_private() this
618 * function will be called for you.
620 * @note Just use dbus_bus_get() or dbus_bus_get_private() instead of
621 * dbus_bus_register() and save yourself some pain. Using
622 * dbus_bus_register() manually is only useful if you have your
623 * own custom message bus not found in #DBusBusType.
625 * If you open a bus connection with dbus_connection_open() or
626 * dbus_connection_open_private() you will have to dbus_bus_register()
627 * yourself, or make the appropriate registration method calls
628 * yourself. If you send the method calls yourself, call
629 * dbus_bus_set_unique_name() with the unique bus name you get from
632 * For shared connections (created with dbus_connection_open()) in a
633 * multithreaded application, you can't really make the registration
634 * calls yourself, because you don't know whether some other thread is
635 * also registering, and the bus will kick you off if you send two
636 * registration messages.
638 * If you use dbus_bus_register() however, there is a lock that
639 * keeps both apps from registering at the same time.
641 * The rule in a multithreaded app, then, is that dbus_bus_register()
642 * must be used to register, or you need to have your own locks that
643 * all threads in the app will respect.
645 * In a single-threaded application you can register by hand instead
646 * of using dbus_bus_register(), as long as you check
647 * dbus_bus_get_unique_name() to see if a unique name has already been
648 * stored by another thread before you send the registration messages.
650 * @param connection the connection
651 * @param error place to store errors
652 * @returns #TRUE on success
655 dbus_bus_register (DBusConnection *connection,
658 DBusMessage *message, *reply;
663 _dbus_return_val_if_fail (connection != NULL, FALSE);
664 _dbus_return_val_if_error_is_set (error, FALSE);
670 if (!_DBUS_LOCK (bus_datas))
672 _DBUS_SET_OOM (error);
673 /* do not "goto out", that would try to unlock */
677 bd = ensure_bus_data (connection);
680 _DBUS_SET_OOM (error);
684 if (bd->unique_name != NULL)
686 _dbus_verbose ("Ignoring attempt to register the same DBusConnection %s with the message bus a second time.\n",
692 if(dbus_transport_is_kdbus(connection))
694 if(!bus_register_kdbus(name, connection, error))
697 if(!bus_register_policy_kdbus(name, connection, error))
700 dbus_connection_set_is_authenticated(connection);
704 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
711 _DBUS_SET_OOM (error);
715 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
719 else if (dbus_set_error_from_message (error, reply))
721 else if (!dbus_message_get_args (reply, error,
722 DBUS_TYPE_STRING, &name,
727 bd->unique_name = _dbus_strdup (name);
728 if (bd->unique_name == NULL)
730 _DBUS_SET_OOM (error);
733 //_dbus_verbose("-- Our uniqe name is: %s\n", bd->unique_name);
737 _DBUS_UNLOCK (bus_datas);
740 dbus_message_unref (message);
743 dbus_message_unref (reply);
746 _DBUS_ASSERT_ERROR_IS_SET (error);
753 * Sets the unique name of the connection, as assigned by the message
754 * bus. Can only be used if you registered with the bus manually
755 * (i.e. if you did not call dbus_bus_register()). Can only be called
756 * once per connection. After the unique name is set, you can get it
757 * with dbus_bus_get_unique_name().
759 * The only reason to use this function is to re-implement the
760 * equivalent of dbus_bus_register() yourself. One (probably unusual)
761 * reason to do that might be to do the bus registration call
762 * asynchronously instead of synchronously.
764 * @note Just use dbus_bus_get() or dbus_bus_get_private(), or worst
765 * case dbus_bus_register(), instead of messing with this
766 * function. There's really no point creating pain for yourself by
767 * doing things manually.
769 * It's hard to use this function safely on shared connections
770 * (created by dbus_connection_open()) in a multithreaded application,
771 * because only one registration attempt can be sent to the bus. If
772 * two threads are both sending the registration message, there is no
773 * mechanism in libdbus itself to avoid sending it twice.
775 * Thus, you need a way to coordinate which thread sends the
776 * registration attempt; which also means you know which thread
777 * will call dbus_bus_set_unique_name(). If you don't know
778 * about all threads in the app (for example, if some libraries
779 * you're using might start libdbus-using threads), then you
780 * need to avoid using this function on shared connections.
782 * @param connection the connection
783 * @param unique_name the unique name
784 * @returns #FALSE if not enough memory
787 dbus_bus_set_unique_name (DBusConnection *connection,
788 const char *unique_name)
791 dbus_bool_t success = FALSE;
793 _dbus_return_val_if_fail (connection != NULL, FALSE);
794 _dbus_return_val_if_fail (unique_name != NULL, FALSE);
796 if (!_DBUS_LOCK (bus_datas))
798 /* do not "goto out", that would try to unlock */
802 bd = ensure_bus_data (connection);
806 _dbus_assert (bd->unique_name == NULL);
808 bd->unique_name = _dbus_strdup (unique_name);
809 success = bd->unique_name != NULL;
812 _DBUS_UNLOCK (bus_datas);
818 * Gets the unique name of the connection as assigned by the message
819 * bus. Only possible after the connection has been registered with
820 * the message bus. All connections returned by dbus_bus_get() or
821 * dbus_bus_get_private() have been successfully registered.
823 * The name remains valid until the connection is freed, and
824 * should not be freed by the caller.
826 * Other than dbus_bus_get(), there are two ways to set the unique
827 * name; one is dbus_bus_register(), the other is
828 * dbus_bus_set_unique_name(). You are responsible for calling
829 * dbus_bus_set_unique_name() if you register by hand instead of using
830 * dbus_bus_register().
832 * @param connection the connection
833 * @returns the unique name or #NULL on error
836 dbus_bus_get_unique_name (DBusConnection *connection)
839 const char *unique_name = NULL;
841 _dbus_return_val_if_fail (connection != NULL, NULL);
843 if (!_DBUS_LOCK (bus_datas))
845 /* We'd have initialized locks when we gave it its unique name, if it
846 * had one. Don't "goto out", that would try to unlock. */
850 bd = ensure_bus_data (connection);
854 unique_name = bd->unique_name;
857 _DBUS_UNLOCK (bus_datas);
863 * Asks the bus to return the UID the named connection authenticated
864 * as, if any. Only works on UNIX; only works for connections on the
865 * same machine as the bus. If you are not on the same machine as the
866 * bus, then calling this is probably a bad idea, since the UID will
867 * mean little to your application.
869 * For the system message bus you're guaranteed to be on the same
870 * machine since it only listens on a UNIX domain socket (at least,
871 * as shipped by default).
873 * This function only works for connections that authenticated as
874 * a UNIX user, right now that includes all bus connections, but
875 * it's very possible to have connections with no associated UID.
876 * So check for errors and do something sensible if they happen.
878 * This function will always return an error on Windows.
880 * @param connection the connection
881 * @param name a name owned by the connection
882 * @param error location to store the error
883 * @returns the unix user id, or ((unsigned)-1) if error is set
886 dbus_bus_get_unix_user (DBusConnection *connection,
890 DBusMessage *message, *reply;
893 _dbus_return_val_if_fail (connection != NULL, DBUS_UID_UNSET);
894 _dbus_return_val_if_fail (name != NULL, DBUS_UID_UNSET);
895 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), DBUS_UID_UNSET);
896 _dbus_return_val_if_error_is_set (error, DBUS_UID_UNSET);
898 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
901 "GetConnectionUnixUser");
905 _DBUS_SET_OOM (error);
906 return DBUS_UID_UNSET;
909 if (!dbus_message_append_args (message,
910 DBUS_TYPE_STRING, &name,
913 dbus_message_unref (message);
914 _DBUS_SET_OOM (error);
915 return DBUS_UID_UNSET;
918 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
921 dbus_message_unref (message);
925 _DBUS_ASSERT_ERROR_IS_SET (error);
926 return DBUS_UID_UNSET;
929 if (dbus_set_error_from_message (error, reply))
931 _DBUS_ASSERT_ERROR_IS_SET (error);
932 dbus_message_unref (reply);
933 return DBUS_UID_UNSET;
936 if (!dbus_message_get_args (reply, error,
937 DBUS_TYPE_UINT32, &uid,
940 _DBUS_ASSERT_ERROR_IS_SET (error);
941 dbus_message_unref (reply);
942 return DBUS_UID_UNSET;
945 dbus_message_unref (reply);
947 return (unsigned long) uid;
951 * Asks the bus to return its globally unique ID, as described in the
952 * D-Bus specification. For the session bus, this is useful as a way
953 * to uniquely identify each user session. For the system bus,
954 * probably the bus ID is not useful; instead, use the machine ID
955 * since it's accessible without necessarily connecting to the bus and
956 * may be persistent beyond a single bus instance (across reboots for
957 * example). See dbus_get_local_machine_id().
959 * In addition to an ID for each bus and an ID for each machine, there is
960 * an ID for each address that the bus is listening on; that can
961 * be retrieved with dbus_connection_get_server_id(), though it is
962 * probably not very useful.
964 * @param connection the connection
965 * @param error location to store the error
966 * @returns the bus ID or #NULL if error is set
969 dbus_bus_get_id (DBusConnection *connection,
972 DBusMessage *message, *reply;
974 const char *v_STRING;
976 _dbus_return_val_if_fail (connection != NULL, NULL);
977 _dbus_return_val_if_error_is_set (error, NULL);
979 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
986 _DBUS_SET_OOM (error);
990 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
993 dbus_message_unref (message);
997 _DBUS_ASSERT_ERROR_IS_SET (error);
1001 if (dbus_set_error_from_message (error, reply))
1003 _DBUS_ASSERT_ERROR_IS_SET (error);
1004 dbus_message_unref (reply);
1009 if (!dbus_message_get_args (reply, error,
1010 DBUS_TYPE_STRING, &v_STRING,
1013 _DBUS_ASSERT_ERROR_IS_SET (error);
1014 dbus_message_unref (reply);
1018 id = _dbus_strdup (v_STRING); /* may be NULL */
1020 dbus_message_unref (reply);
1023 _DBUS_SET_OOM (error);
1025 /* FIXME it might be nice to cache the ID locally */
1031 * Asks the bus to assign the given name to this connection by invoking
1032 * the RequestName method on the bus. This method is fully documented
1033 * in the D-Bus specification. For quick reference, the flags and
1034 * result codes are discussed here, but the specification is the
1035 * canonical version of this information.
1037 * First you should know that for each bus name, the bus stores
1038 * a queue of connections that would like to own it. Only
1039 * one owns it at a time - called the primary owner. If the primary
1040 * owner releases the name or disconnects, then the next owner in the
1041 * queue atomically takes over.
1043 * So for example if you have an application org.freedesktop.TextEditor
1044 * and multiple instances of it can be run, you can have all of them
1045 * sitting in the queue. The first one to start up will receive messages
1046 * sent to org.freedesktop.TextEditor, but if that one exits another
1047 * will become the primary owner and receive messages.
1049 * The queue means you don't need to manually watch for the current owner to
1050 * disappear and then request the name again.
1052 * When requesting a name, you can specify several flags.
1054 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT and #DBUS_NAME_FLAG_DO_NOT_QUEUE
1055 * are properties stored by the bus for this connection with respect to
1056 * each requested bus name. These properties are stored even if the
1057 * connection is queued and does not become the primary owner.
1058 * You can update these flags by calling RequestName again (even if
1059 * you already own the name).
1061 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT means that another requestor of the
1062 * name can take it away from you by specifying #DBUS_NAME_FLAG_REPLACE_EXISTING.
1064 * #DBUS_NAME_FLAG_DO_NOT_QUEUE means that if you aren't the primary owner,
1065 * you don't want to be queued up - you only care about being the
1068 * Unlike the other two flags, #DBUS_NAME_FLAG_REPLACE_EXISTING is a property
1069 * of the individual RequestName call, i.e. the bus does not persistently
1070 * associate it with the connection-name pair. If a RequestName call includes
1071 * the #DBUS_NAME_FLAG_REPLACE_EXISTING flag, and the current primary
1072 * owner has #DBUS_NAME_FLAG_ALLOW_REPLACEMENT set, then the current primary
1073 * owner will be kicked off.
1075 * If no flags are given, an application will receive the requested
1076 * name only if the name is currently unowned; and it will NOT give
1077 * up the name if another application asks to take it over using
1078 * #DBUS_NAME_FLAG_REPLACE_EXISTING.
1080 * This function returns a result code. The possible result codes
1083 * #DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER means that the name had no
1084 * existing owner, and the caller is now the primary owner; or that
1085 * the name had an owner, and the caller specified
1086 * #DBUS_NAME_FLAG_REPLACE_EXISTING, and the current owner
1087 * specified #DBUS_NAME_FLAG_ALLOW_REPLACEMENT.
1089 * #DBUS_REQUEST_NAME_REPLY_IN_QUEUE happens only if the caller does NOT
1090 * specify #DBUS_NAME_FLAG_DO_NOT_QUEUE and either the current owner
1091 * did NOT specify #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or the caller did NOT
1092 * specify #DBUS_NAME_FLAG_REPLACE_EXISTING. In this case the caller ends up
1093 * in a queue to own the name after the current owner gives it up.
1095 * #DBUS_REQUEST_NAME_REPLY_EXISTS happens if the name has an owner
1096 * already and the caller specifies #DBUS_NAME_FLAG_DO_NOT_QUEUE
1097 * and either the current owner has NOT specified
1098 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or the caller did NOT specify
1099 * #DBUS_NAME_FLAG_REPLACE_EXISTING.
1101 * #DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER happens if an application
1102 * requests a name it already owns. (Re-requesting a name is useful if
1103 * you want to change the #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or
1104 * #DBUS_NAME_FLAG_DO_NOT_QUEUE settings.)
1106 * When a service represents an application, say "text editor," then
1107 * it should specify #DBUS_NAME_FLAG_ALLOW_REPLACEMENT if it wants
1108 * the last editor started to be the user's editor vs. the first one
1109 * started. Then any editor that can be the user's editor should
1110 * specify #DBUS_NAME_FLAG_REPLACE_EXISTING to either take over
1111 * (last-started-wins) or be queued up (first-started-wins) according
1112 * to whether #DBUS_NAME_FLAG_ALLOW_REPLACEMENT was given.
1114 * Conventionally, single-instance applications often offer a command
1115 * line option called --replace which means to replace the current
1116 * instance. To implement this, always set
1117 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT when you request your
1118 * application's bus name. When you lose ownership of your bus name,
1119 * you need to exit. Look for the signal "NameLost" from
1120 * #DBUS_SERVICE_DBUS and #DBUS_INTERFACE_DBUS (the signal's first
1121 * argument is the bus name that was lost). If starting up without
1122 * --replace, do not specify #DBUS_NAME_FLAG_REPLACE_EXISTING, and
1123 * exit if you fail to become the bus name owner. If --replace is
1124 * given, ask to replace the old owner.
1126 * @param connection the connection
1127 * @param name the name to request
1128 * @param flags flags
1129 * @param error location to store the error
1130 * @returns a result code, -1 if error is set
1133 dbus_bus_request_name (DBusConnection *connection,
1138 dbus_uint32_t result;
1140 _dbus_return_val_if_fail (connection != NULL, 0);
1141 _dbus_return_val_if_fail (name != NULL, 0);
1142 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
1143 _dbus_return_val_if_error_is_set (error, 0);
1145 if(!dbus_transport_is_kdbus(connection))
1147 DBusMessage *message, *reply;
1149 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1151 DBUS_INTERFACE_DBUS,
1153 if (message == NULL)
1155 _DBUS_SET_OOM (error);
1159 if (!dbus_message_append_args (message,
1160 DBUS_TYPE_STRING, &name,
1161 DBUS_TYPE_UINT32, &flags,
1164 dbus_message_unref (message);
1165 _DBUS_SET_OOM (error);
1169 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
1172 dbus_message_unref (message);
1176 _DBUS_ASSERT_ERROR_IS_SET (error);
1180 if (dbus_set_error_from_message (error, reply))
1182 _DBUS_ASSERT_ERROR_IS_SET (error);
1183 dbus_message_unref (reply);
1187 if (!dbus_message_get_args (reply, error,
1188 DBUS_TYPE_UINT32, &result,
1191 _DBUS_ASSERT_ERROR_IS_SET (error);
1192 dbus_message_unref (reply);
1196 dbus_message_unref (reply);
1200 if(!bus_register_policy_kdbus(name, connection, error))
1203 result = bus_request_name_kdbus(connection, name, flags, error);
1204 if(dbus_error_is_set(error))
1213 * Asks the bus to unassign the given name from this connection by
1214 * invoking the ReleaseName method on the bus. The "ReleaseName"
1215 * method is canonically documented in the D-Bus specification.
1217 * Possible results are: #DBUS_RELEASE_NAME_REPLY_RELEASED
1218 * which means you owned the name or were in the queue to own it,
1219 * and and now you don't own it and aren't in the queue.
1220 * #DBUS_RELEASE_NAME_REPLY_NOT_OWNER which means someone else
1221 * owns the name so you can't release it.
1222 * #DBUS_RELEASE_NAME_REPLY_NON_EXISTENT
1223 * which means nobody owned the name.
1225 * @param connection the connection
1226 * @param name the name to remove
1227 * @param error location to store the error
1228 * @returns a result code, -1 if error is set
1231 dbus_bus_release_name (DBusConnection *connection,
1235 DBusMessage *message, *reply;
1236 dbus_uint32_t result;
1238 _dbus_return_val_if_fail (connection != NULL, 0);
1239 _dbus_return_val_if_fail (name != NULL, 0);
1240 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
1241 _dbus_return_val_if_error_is_set (error, 0);
1243 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1245 DBUS_INTERFACE_DBUS,
1248 if (message == NULL)
1250 _DBUS_SET_OOM (error);
1254 if (!dbus_message_append_args (message,
1255 DBUS_TYPE_STRING, &name,
1258 dbus_message_unref (message);
1259 _DBUS_SET_OOM (error);
1263 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
1266 dbus_message_unref (message);
1270 _DBUS_ASSERT_ERROR_IS_SET (error);
1274 if (dbus_set_error_from_message (error, reply))
1276 _DBUS_ASSERT_ERROR_IS_SET (error);
1277 dbus_message_unref (reply);
1281 if (!dbus_message_get_args (reply, error,
1282 DBUS_TYPE_UINT32, &result,
1285 _DBUS_ASSERT_ERROR_IS_SET (error);
1286 dbus_message_unref (reply);
1290 dbus_message_unref (reply);
1296 * Asks the bus whether a certain name has an owner.
1298 * Using this can easily result in a race condition,
1299 * since an owner can appear or disappear after you
1302 * If you want to request a name, just request it;
1303 * if you want to avoid replacing a current owner,
1304 * don't specify #DBUS_NAME_FLAG_REPLACE_EXISTING and
1305 * you will get an error if there's already an owner.
1307 * @param connection the connection
1308 * @param name the name
1309 * @param error location to store any errors
1310 * @returns #TRUE if the name exists, #FALSE if not or on error
1313 dbus_bus_name_has_owner (DBusConnection *connection,
1317 DBusMessage *message, *reply;
1320 _dbus_return_val_if_fail (connection != NULL, FALSE);
1321 _dbus_return_val_if_fail (name != NULL, FALSE);
1322 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
1323 _dbus_return_val_if_error_is_set (error, FALSE);
1325 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1327 DBUS_INTERFACE_DBUS,
1329 if (message == NULL)
1331 _DBUS_SET_OOM (error);
1335 if (!dbus_message_append_args (message,
1336 DBUS_TYPE_STRING, &name,
1339 dbus_message_unref (message);
1340 _DBUS_SET_OOM (error);
1344 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
1345 dbus_message_unref (message);
1349 _DBUS_ASSERT_ERROR_IS_SET (error);
1353 if (!dbus_message_get_args (reply, error,
1354 DBUS_TYPE_BOOLEAN, &exists,
1357 _DBUS_ASSERT_ERROR_IS_SET (error);
1358 dbus_message_unref (reply);
1362 dbus_message_unref (reply);
1367 * Starts a service that will request ownership of the given name.
1368 * The returned result will be one of be one of
1369 * #DBUS_START_REPLY_SUCCESS or #DBUS_START_REPLY_ALREADY_RUNNING if
1370 * successful. Pass #NULL if you don't care about the result.
1372 * The flags parameter is for future expansion, currently you should
1375 * It's often easier to avoid explicitly starting services, and
1376 * just send a method call to the service's bus name instead.
1377 * Method calls start a service to handle them by default
1378 * unless you call dbus_message_set_auto_start() to disable this
1381 * @param connection the connection
1382 * @param name the name we want the new service to request
1383 * @param flags the flags (should always be 0 for now)
1384 * @param result a place to store the result or #NULL
1385 * @param error location to store any errors
1386 * @returns #TRUE if the activation succeeded, #FALSE if not
1389 dbus_bus_start_service_by_name (DBusConnection *connection,
1391 dbus_uint32_t flags,
1392 dbus_uint32_t *result,
1398 _dbus_return_val_if_fail (connection != NULL, FALSE);
1399 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
1401 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1403 DBUS_INTERFACE_DBUS,
1404 "StartServiceByName");
1406 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &name,
1407 DBUS_TYPE_UINT32, &flags, DBUS_TYPE_INVALID))
1409 dbus_message_unref (msg);
1410 _DBUS_SET_OOM (error);
1414 reply = dbus_connection_send_with_reply_and_block (connection, msg,
1416 dbus_message_unref (msg);
1420 _DBUS_ASSERT_ERROR_IS_SET (error);
1424 if (dbus_set_error_from_message (error, reply))
1426 _DBUS_ASSERT_ERROR_IS_SET (error);
1427 dbus_message_unref (reply);
1431 if (result != NULL &&
1432 !dbus_message_get_args (reply, error, DBUS_TYPE_UINT32,
1433 result, DBUS_TYPE_INVALID))
1435 _DBUS_ASSERT_ERROR_IS_SET (error);
1436 dbus_message_unref (reply);
1440 dbus_message_unref (reply);
1445 send_no_return_values (DBusConnection *connection,
1451 /* Block to check success codepath */
1454 reply = dbus_connection_send_with_reply_and_block (connection, msg,
1458 _DBUS_ASSERT_ERROR_IS_SET (error);
1460 dbus_message_unref (reply);
1464 /* Silently-fail nonblocking codepath */
1465 dbus_message_set_no_reply (msg, TRUE);
1466 dbus_connection_send (connection, msg, NULL);
1471 * Adds a match rule to match messages going through the message bus.
1472 * The "rule" argument is the string form of a match rule.
1474 * If you pass #NULL for the error, this function will not
1475 * block; the match thus won't be added until you flush the
1476 * connection, and if there's an error adding the match
1477 * you won't find out about it. This is generally acceptable, since the
1478 * possible errors (including a lack of resources in the bus, the connection
1479 * having exceeded its quota of active match rules, or the match rule being
1480 * unparseable) are generally unrecoverable.
1482 * If you pass non-#NULL for the error this function will
1483 * block until it gets a reply. This may be useful when using match rule keys
1484 * introduced in recent versions of D-Bus, like 'arg0namespace', to allow the
1485 * application to fall back to less efficient match rules supported by older
1486 * versions of the daemon if the running version is not new enough; or when
1487 * using user-supplied rules rather than rules hard-coded at compile time.
1489 * Normal API conventions would have the function return
1490 * a boolean value indicating whether the error was set,
1491 * but that would require blocking always to determine
1494 * The AddMatch method is fully documented in the D-Bus
1495 * specification. For quick reference, the format of the
1496 * match rules is discussed here, but the specification
1497 * is the canonical version of this information.
1499 * Rules are specified as a string of comma separated
1500 * key/value pairs. An example is
1501 * "type='signal',sender='org.freedesktop.DBus',
1502 * interface='org.freedesktop.DBus',member='Foo',
1503 * path='/bar/foo',destination=':452345.34'"
1505 * Possible keys you can match on are type, sender,
1506 * interface, member, path, destination and numbered
1507 * keys to match message args (keys are 'arg0', 'arg1', etc.).
1508 * Omitting a key from the rule indicates
1509 * a wildcard match. For instance omitting
1510 * the member from a match rule but adding a sender would
1511 * let all messages from that sender through regardless of
1514 * Matches are inclusive not exclusive so as long as one
1515 * rule matches the message will get through. It is important
1516 * to note this because every time a message is received the
1517 * application will be paged into memory to process it. This
1518 * can cause performance problems such as draining batteries
1519 * on embedded platforms.
1521 * If you match message args ('arg0', 'arg1', and so forth)
1522 * only string arguments will match. That is, arg0='5' means
1523 * match the string "5" not the integer 5.
1525 * Currently there is no way to match against non-string arguments.
1527 * A specialised form of wildcard matching on arguments is
1528 * supported for path-like namespaces. If your argument match has
1529 * a 'path' suffix (eg: "arg0path='/some/path/'") then it is
1530 * considered a match if the argument exactly matches the given
1531 * string or if one of them ends in a '/' and is a prefix of the
1534 * Matching on interface is tricky because method call
1535 * messages only optionally specify the interface.
1536 * If a message omits the interface, then it will NOT match
1537 * if the rule specifies an interface name. This means match
1538 * rules on method calls should not usually give an interface.
1540 * However, signal messages are required to include the interface
1541 * so when matching signals usually you should specify the interface
1542 * in the match rule.
1544 * For security reasons, you can match arguments only up to
1545 * #DBUS_MAXIMUM_MATCH_RULE_ARG_NUMBER.
1547 * Match rules have a maximum length of #DBUS_MAXIMUM_MATCH_RULE_LENGTH
1550 * Both of these maximums are much higher than you're likely to need,
1551 * they only exist because the D-Bus bus daemon has fixed limits on
1552 * all resource usage.
1554 * @param connection connection to the message bus
1555 * @param rule textual form of match rule
1556 * @param error location to store any errors
1559 dbus_bus_add_match (DBusConnection *connection,
1563 _dbus_return_if_fail (rule != NULL);
1565 if(!dbus_transport_is_kdbus(connection))
1569 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1571 DBUS_INTERFACE_DBUS,
1576 _DBUS_SET_OOM (error);
1580 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
1583 dbus_message_unref (msg);
1584 _DBUS_SET_OOM (error);
1588 send_no_return_values (connection, msg, error);
1590 dbus_message_unref (msg);
1593 dbus_bus_add_match_kdbus(connection, rule, error);
1597 * Removes a previously-added match rule "by value" (the most
1598 * recently-added identical rule gets removed). The "rule" argument
1599 * is the string form of a match rule.
1601 * The bus compares match rules semantically, not textually, so
1602 * whitespace and ordering don't have to be identical to
1603 * the rule you passed to dbus_bus_add_match().
1605 * If you pass #NULL for the error, this function will not
1606 * block; otherwise it will. See detailed explanation in
1607 * docs for dbus_bus_add_match().
1609 * @param connection connection to the message bus
1610 * @param rule textual form of match rule
1611 * @param error location to store any errors
1614 dbus_bus_remove_match (DBusConnection *connection,
1620 _dbus_return_if_fail (rule != NULL);
1622 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1624 DBUS_INTERFACE_DBUS,
1627 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
1630 dbus_message_unref (msg);
1631 _DBUS_SET_OOM (error);
1635 send_no_return_values (connection, msg, error);
1637 dbus_message_unref (msg);