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"
36 * @defgroup DBusBus Message bus APIs
38 * @brief Functions for communicating with the message bus
40 * dbus_bus_get() allows all modules and libraries in a given
41 * process to share the same connection to the bus daemon by storing
42 * the connection globally.
44 * All other functions in this module are just convenience functions;
45 * most of them invoke methods on the bus daemon, by sending method
46 * call messages to #DBUS_SERVICE_DBUS. These convenience functions
47 * often make blocking method calls. If you don't want to block,
48 * you can send the method call messages manually in the same way
49 * you would any other method call message.
51 * This module is the only one in libdbus that's specific to
52 * communicating with the message bus daemon. The rest of the API can
53 * also be used for connecting to another application directly.
55 * @todo right now the default address of the system bus is hardcoded,
56 * so if you change it in the global config file suddenly you have to
57 * set DBUS_SYSTEM_BUS_ADDRESS env variable. Might be nice if the
58 * client lib somehow read the config file, or if the bus on startup
59 * somehow wrote out its address to a well-known spot, but might also
64 * @defgroup DBusBusInternals Message bus APIs internals
65 * @ingroup DBusInternals
66 * @brief Internals of functions for communicating with the message bus
72 * Block of message-bus-related data we attach to each
73 * #DBusConnection used with these convenience functions.
78 DBusConnection *connection; /**< Connection we're associated with */
79 char *unique_name; /**< Unique name of this connection */
81 unsigned int is_well_known : 1; /**< Is one of the well-known connections in our global array */
84 /** The slot we have reserved to store BusData.
86 static dbus_int32_t bus_data_slot = -1;
88 /** Number of bus types */
91 static DBusConnection *bus_connections[N_BUS_TYPES];
92 static char *bus_connection_addresses[N_BUS_TYPES] = { NULL, NULL, NULL };
94 static DBusBusType activation_bus_type = DBUS_BUS_STARTER;
96 static dbus_bool_t initialized = FALSE;
99 addresses_shutdown_func (void *data)
104 while (i < N_BUS_TYPES)
106 if (bus_connections[i] != NULL)
107 _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");
109 dbus_free (bus_connection_addresses[i]);
110 bus_connection_addresses[i] = NULL;
114 activation_bus_type = DBUS_BUS_STARTER;
120 get_from_env (char **connection_p,
125 _dbus_assert (*connection_p == NULL);
127 s = _dbus_getenv (env_var);
128 if (s == NULL || *s == '\0')
129 return TRUE; /* successfully didn't use the env var */
132 *connection_p = _dbus_strdup (s);
133 return *connection_p != NULL;
138 init_session_address (void)
144 /* First, look in the environment. This is the normal case on
145 * freedesktop.org/Unix systems. */
146 get_from_env (&bus_connection_addresses[DBUS_BUS_SESSION],
147 "DBUS_SESSION_BUS_ADDRESS");
148 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
150 dbus_bool_t supported;
152 DBusError error = DBUS_ERROR_INIT;
154 if (!_dbus_string_init (&addr))
158 /* So it's not in the environment - let's try a platform-specific method.
159 * On MacOS, this involves asking launchd. On Windows (not specified yet)
160 * we might do a COM lookup.
161 * Ignore errors - if we failed, fall back to autolaunch. */
162 retval = _dbus_lookup_session_address (&supported, &addr, &error);
163 if (supported && retval)
165 retval =_dbus_string_steal_data (&addr, &bus_connection_addresses[DBUS_BUS_SESSION]);
167 else if (supported && !retval)
169 if (dbus_error_is_set(&error))
170 _dbus_warn ("Dynamic session lookup supported but failed: %s\n", error.message);
172 _dbus_warn ("Dynamic session lookup supported but failed silently\n");
174 _dbus_string_free (&addr);
182 /* We have a hard-coded (but compile-time-configurable) fallback address for
183 * the session bus. */
184 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
185 bus_connection_addresses[DBUS_BUS_SESSION] =
186 _dbus_strdup (DBUS_SESSION_BUS_CONNECT_ADDRESS);
188 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
195 init_connections_unlocked (void)
203 while (i < N_BUS_TYPES)
205 bus_connections[i] = NULL;
209 /* Don't init these twice, we may run this code twice if
210 * init_connections_unlocked() fails midway through.
211 * In practice, each block below should contain only one
212 * "return FALSE" or running through twice may not
216 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
218 _dbus_verbose ("Filling in system bus address...\n");
220 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SYSTEM],
221 "DBUS_SYSTEM_BUS_ADDRESS"))
226 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
228 /* Use default system bus address if none set in environment */
229 bus_connection_addresses[DBUS_BUS_SYSTEM] =
230 _dbus_strdup (DBUS_SYSTEM_BUS_DEFAULT_ADDRESS);
232 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
235 _dbus_verbose (" used default system bus \"%s\"\n",
236 bus_connection_addresses[DBUS_BUS_SYSTEM]);
239 _dbus_verbose (" used env var system bus \"%s\"\n",
240 bus_connection_addresses[DBUS_BUS_SYSTEM]);
242 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
244 _dbus_verbose ("Filling in session bus address...\n");
246 if (!init_session_address ())
249 _dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_SESSION] ?
250 bus_connection_addresses[DBUS_BUS_SESSION] : "none set");
253 if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
255 _dbus_verbose ("Filling in activation bus address...\n");
257 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_STARTER],
258 "DBUS_STARTER_ADDRESS"))
261 _dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_STARTER] ?
262 bus_connection_addresses[DBUS_BUS_STARTER] : "none set");
266 if (bus_connection_addresses[DBUS_BUS_STARTER] != NULL)
268 s = _dbus_getenv ("DBUS_STARTER_BUS_TYPE");
272 _dbus_verbose ("Bus activation type was set to \"%s\"\n", s);
274 if (strcmp (s, "system") == 0)
275 activation_bus_type = DBUS_BUS_SYSTEM;
276 else if (strcmp (s, "session") == 0)
277 activation_bus_type = DBUS_BUS_SESSION;
282 /* Default to the session bus instead if available */
283 if (bus_connection_addresses[DBUS_BUS_SESSION] != NULL)
285 bus_connection_addresses[DBUS_BUS_STARTER] =
286 _dbus_strdup (bus_connection_addresses[DBUS_BUS_SESSION]);
287 if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
292 /* If we return FALSE we have to be sure that restarting
293 * the above code will work right
296 if (!_dbus_setenv ("DBUS_ACTIVATION_ADDRESS", NULL))
299 if (!_dbus_setenv ("DBUS_ACTIVATION_BUS_TYPE", NULL))
302 if (!_dbus_register_shutdown_func (addresses_shutdown_func,
313 bus_data_free (void *data)
317 if (bd->is_well_known)
321 /* We may be stored in more than one slot */
322 /* This should now be impossible - these slots are supposed to
323 * be cleared on disconnect, so should not need to be cleared on
327 while (i < N_BUS_TYPES)
329 if (bus_connections[i] == bd->connection)
330 bus_connections[i] = NULL;
337 dbus_free (bd->unique_name);
340 dbus_connection_free_data_slot (&bus_data_slot);
344 ensure_bus_data (DBusConnection *connection)
348 if (!dbus_connection_allocate_data_slot (&bus_data_slot))
351 bd = dbus_connection_get_data (connection, bus_data_slot);
354 bd = dbus_new0 (BusData, 1);
357 dbus_connection_free_data_slot (&bus_data_slot);
361 bd->connection = connection;
363 if (!dbus_connection_set_data (connection, bus_data_slot, bd,
367 dbus_connection_free_data_slot (&bus_data_slot);
371 /* Data slot refcount now held by the BusData */
375 dbus_connection_free_data_slot (&bus_data_slot);
382 * Internal function that checks to see if this
383 * is a shared connection owned by the bus and if it is unref it.
385 * @param connection a connection that has been disconnected.
388 _dbus_bus_notify_shared_connection_disconnected_unlocked (DBusConnection *connection)
394 /* We are expecting to have the connection saved in only one of these
395 * slots, but someone could in a pathological case set system and session
396 * bus to the same bus or something. Or set one of them to the starter
397 * bus without setting the starter bus type in the env variable.
398 * So we don't break the loop as soon as we find a match.
400 for (i = 0; i < N_BUS_TYPES; ++i)
402 if (bus_connections[i] == connection)
404 bus_connections[i] = NULL;
411 static DBusConnection *
412 internal_bus_get (DBusBusType type,
417 DBusConnection *connection;
419 DBusBusType address_type;
421 _dbus_return_val_if_fail (type >= 0 && type < N_BUS_TYPES, NULL);
422 _dbus_return_val_if_error_is_set (error, NULL);
428 if (!init_connections_unlocked ())
430 _DBUS_SET_OOM (error);
434 /* We want to use the activation address even if the
435 * activating bus is the session or system bus,
440 /* Use the real type of the activation bus for getting its
441 * connection, but only if the real type's address is available. (If
442 * the activating bus isn't a well-known bus then
443 * activation_bus_type == DBUS_BUS_STARTER)
445 if (type == DBUS_BUS_STARTER &&
446 bus_connection_addresses[activation_bus_type] != NULL)
447 type = activation_bus_type;
449 if (!private && bus_connections[type] != NULL)
451 connection = bus_connections[type];
452 dbus_connection_ref (connection);
456 address = bus_connection_addresses[address_type];
459 dbus_set_error (error, DBUS_ERROR_FAILED,
460 "Unable to determine the address of the message bus (try 'man dbus-launch' and 'man dbus-daemon' for help)");
465 connection = dbus_connection_open_private (address, error);
467 connection = dbus_connection_open (address, error);
474 if (!dbus_bus_register (connection, error))
476 _dbus_connection_close_possibly_shared (connection);
477 dbus_connection_unref (connection);
484 /* store a weak ref to the connection (dbus-connection.c is
485 * supposed to have a strong ref that it drops on disconnect,
486 * since this is a shared connection)
488 bus_connections[type] = connection;
491 /* By default we're bound to the lifecycle of
494 dbus_connection_set_exit_on_disconnect (connection,
497 _DBUS_LOCK (bus_datas);
498 bd = ensure_bus_data (connection);
499 _dbus_assert (bd != NULL); /* it should have been created on
500 register, so OOM not possible */
501 bd->is_well_known = TRUE;
502 _DBUS_UNLOCK (bus_datas);
505 /* Return a reference to the caller, or NULL with error set. */
506 if (connection == NULL)
507 _DBUS_ASSERT_ERROR_IS_SET (error);
514 /** @} */ /* end of implementation details docs */
517 * @addtogroup DBusBus
522 * Connects to a bus daemon and registers the client with it. If a
523 * connection to the bus already exists, then that connection is
524 * returned. The caller of this function owns a reference to the bus.
526 * The caller may NOT call dbus_connection_close() on this connection;
527 * see dbus_connection_open() and dbus_connection_close() for details
530 * If this function obtains a new connection object never before
531 * returned from dbus_bus_get(), it will call
532 * dbus_connection_set_exit_on_disconnect(), so the application
533 * will exit if the connection closes. You can undo this
534 * by calling dbus_connection_set_exit_on_disconnect() yourself
535 * after you get the connection.
537 * dbus_bus_get() calls dbus_bus_register() for you.
539 * If returning a newly-created connection, this function will block
540 * until authentication and bus registration are complete.
542 * @param type bus type
543 * @param error address where an error can be returned.
544 * @returns a #DBusConnection with new ref
547 dbus_bus_get (DBusBusType type,
550 return internal_bus_get (type, FALSE, error);
554 * Connects to a bus daemon and registers the client with it as with
555 * dbus_bus_register(). Unlike dbus_bus_get(), always creates a new
556 * connection. This connection will not be saved or recycled by
557 * libdbus. Caller owns a reference to the bus and must either close
558 * it or know it to be closed prior to releasing this reference.
560 * See dbus_connection_open_private() for more details on when to
561 * close and unref this connection.
563 * This function calls
564 * dbus_connection_set_exit_on_disconnect() on the new connection, so the application
565 * will exit if the connection closes. You can undo this
566 * by calling dbus_connection_set_exit_on_disconnect() yourself
567 * after you get the connection.
569 * dbus_bus_get_private() calls dbus_bus_register() for you.
571 * This function will block until authentication and bus registration
574 * @param type bus type
575 * @param error address where an error can be returned.
576 * @returns a DBusConnection with new ref
579 dbus_bus_get_private (DBusBusType type,
582 return internal_bus_get (type, TRUE, error);
586 * Registers a connection with the bus. This must be the first
587 * thing an application does when connecting to the message bus.
588 * If registration succeeds, the unique name will be set,
589 * and can be obtained using dbus_bus_get_unique_name().
591 * This function will block until registration is complete.
593 * If the connection has already registered with the bus
594 * (determined by checking whether dbus_bus_get_unique_name()
595 * returns a non-#NULL value), then this function does nothing.
597 * If you use dbus_bus_get() or dbus_bus_get_private() this
598 * function will be called for you.
600 * @note Just use dbus_bus_get() or dbus_bus_get_private() instead of
601 * dbus_bus_register() and save yourself some pain. Using
602 * dbus_bus_register() manually is only useful if you have your
603 * own custom message bus not found in #DBusBusType.
605 * If you open a bus connection with dbus_connection_open() or
606 * dbus_connection_open_private() you will have to dbus_bus_register()
607 * yourself, or make the appropriate registration method calls
608 * yourself. If you send the method calls yourself, call
609 * dbus_bus_set_unique_name() with the unique bus name you get from
612 * For shared connections (created with dbus_connection_open()) in a
613 * multithreaded application, you can't really make the registration
614 * calls yourself, because you don't know whether some other thread is
615 * also registering, and the bus will kick you off if you send two
616 * registration messages.
618 * If you use dbus_bus_register() however, there is a lock that
619 * keeps both apps from registering at the same time.
621 * The rule in a multithreaded app, then, is that dbus_bus_register()
622 * must be used to register, or you need to have your own locks that
623 * all threads in the app will respect.
625 * In a single-threaded application you can register by hand instead
626 * of using dbus_bus_register(), as long as you check
627 * dbus_bus_get_unique_name() to see if a unique name has already been
628 * stored by another thread before you send the registration messages.
630 * @param connection the connection
631 * @param error place to store errors
632 * @returns #TRUE on success
635 dbus_bus_register (DBusConnection *connection,
638 DBusMessage *message, *reply;
643 _dbus_return_val_if_fail (connection != NULL, FALSE);
644 _dbus_return_val_if_error_is_set (error, FALSE);
650 _DBUS_LOCK (bus_datas);
652 bd = ensure_bus_data (connection);
655 _DBUS_SET_OOM (error);
659 if (bd->unique_name != NULL)
661 _dbus_verbose ("Ignoring attempt to register the same DBusConnection %s with the message bus a second time.\n",
668 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
675 _DBUS_SET_OOM (error);
679 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
683 else if (dbus_set_error_from_message (error, reply))
685 else if (!dbus_message_get_args (reply, error,
686 DBUS_TYPE_STRING, &name,
690 bd->unique_name = _dbus_strdup (name);
691 if (bd->unique_name == NULL)
693 _DBUS_SET_OOM (error);
700 _DBUS_UNLOCK (bus_datas);
703 dbus_message_unref (message);
706 dbus_message_unref (reply);
709 _DBUS_ASSERT_ERROR_IS_SET (error);
716 * Sets the unique name of the connection, as assigned by the message
717 * bus. Can only be used if you registered with the bus manually
718 * (i.e. if you did not call dbus_bus_register()). Can only be called
719 * once per connection. After the unique name is set, you can get it
720 * with dbus_bus_get_unique_name().
722 * The only reason to use this function is to re-implement the
723 * equivalent of dbus_bus_register() yourself. One (probably unusual)
724 * reason to do that might be to do the bus registration call
725 * asynchronously instead of synchronously.
727 * @note Just use dbus_bus_get() or dbus_bus_get_private(), or worst
728 * case dbus_bus_register(), instead of messing with this
729 * function. There's really no point creating pain for yourself by
730 * doing things manually.
732 * It's hard to use this function safely on shared connections
733 * (created by dbus_connection_open()) in a multithreaded application,
734 * because only one registration attempt can be sent to the bus. If
735 * two threads are both sending the registration message, there is no
736 * mechanism in libdbus itself to avoid sending it twice.
738 * Thus, you need a way to coordinate which thread sends the
739 * registration attempt; which also means you know which thread
740 * will call dbus_bus_set_unique_name(). If you don't know
741 * about all threads in the app (for example, if some libraries
742 * you're using might start libdbus-using threads), then you
743 * need to avoid using this function on shared connections.
745 * @param connection the connection
746 * @param unique_name the unique name
747 * @returns #FALSE if not enough memory
750 dbus_bus_set_unique_name (DBusConnection *connection,
751 const char *unique_name)
754 dbus_bool_t success = FALSE;
756 _dbus_return_val_if_fail (connection != NULL, FALSE);
757 _dbus_return_val_if_fail (unique_name != NULL, FALSE);
759 _DBUS_LOCK (bus_datas);
761 bd = ensure_bus_data (connection);
765 _dbus_assert (bd->unique_name == NULL);
767 bd->unique_name = _dbus_strdup (unique_name);
768 success = bd->unique_name != NULL;
771 _DBUS_UNLOCK (bus_datas);
777 * Gets the unique name of the connection as assigned by the message
778 * bus. Only possible after the connection has been registered with
779 * the message bus. All connections returned by dbus_bus_get() or
780 * dbus_bus_get_private() have been successfully registered.
782 * The name remains valid until the connection is freed, and
783 * should not be freed by the caller.
785 * Other than dbus_bus_get(), there are two ways to set the unique
786 * name; one is dbus_bus_register(), the other is
787 * dbus_bus_set_unique_name(). You are responsible for calling
788 * dbus_bus_set_unique_name() if you register by hand instead of using
789 * dbus_bus_register().
791 * @param connection the connection
792 * @returns the unique name or #NULL on error
795 dbus_bus_get_unique_name (DBusConnection *connection)
798 const char *unique_name = NULL;
800 _dbus_return_val_if_fail (connection != NULL, NULL);
802 _DBUS_LOCK (bus_datas);
804 bd = ensure_bus_data (connection);
808 unique_name = bd->unique_name;
811 _DBUS_UNLOCK (bus_datas);
817 * Asks the bus to return the UID the named connection authenticated
818 * as, if any. Only works on UNIX; only works for connections on the
819 * same machine as the bus. If you are not on the same machine as the
820 * bus, then calling this is probably a bad idea, since the UID will
821 * mean little to your application.
823 * For the system message bus you're guaranteed to be on the same
824 * machine since it only listens on a UNIX domain socket (at least,
825 * as shipped by default).
827 * This function only works for connections that authenticated as
828 * a UNIX user, right now that includes all bus connections, but
829 * it's very possible to have connections with no associated UID.
830 * So check for errors and do something sensible if they happen.
832 * This function will always return an error on Windows.
834 * @param connection the connection
835 * @param name a name owned by the connection
836 * @param error location to store the error
837 * @returns the unix user id, or ((unsigned)-1) if error is set
840 dbus_bus_get_unix_user (DBusConnection *connection,
844 DBusMessage *message, *reply;
847 _dbus_return_val_if_fail (connection != NULL, DBUS_UID_UNSET);
848 _dbus_return_val_if_fail (name != NULL, DBUS_UID_UNSET);
849 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), DBUS_UID_UNSET);
850 _dbus_return_val_if_error_is_set (error, DBUS_UID_UNSET);
852 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
855 "GetConnectionUnixUser");
859 _DBUS_SET_OOM (error);
860 return DBUS_UID_UNSET;
863 if (!dbus_message_append_args (message,
864 DBUS_TYPE_STRING, &name,
867 dbus_message_unref (message);
868 _DBUS_SET_OOM (error);
869 return DBUS_UID_UNSET;
872 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
875 dbus_message_unref (message);
879 _DBUS_ASSERT_ERROR_IS_SET (error);
880 return DBUS_UID_UNSET;
883 if (dbus_set_error_from_message (error, reply))
885 _DBUS_ASSERT_ERROR_IS_SET (error);
886 dbus_message_unref (reply);
887 return DBUS_UID_UNSET;
890 if (!dbus_message_get_args (reply, error,
891 DBUS_TYPE_UINT32, &uid,
894 _DBUS_ASSERT_ERROR_IS_SET (error);
895 dbus_message_unref (reply);
896 return DBUS_UID_UNSET;
899 dbus_message_unref (reply);
901 return (unsigned long) uid;
905 * Asks the bus to return its globally unique ID, as described in the
906 * D-Bus specification. For the session bus, this is useful as a way
907 * to uniquely identify each user session. For the system bus,
908 * probably the bus ID is not useful; instead, use the machine ID
909 * since it's accessible without necessarily connecting to the bus and
910 * may be persistent beyond a single bus instance (across reboots for
911 * example). See dbus_get_local_machine_id().
913 * In addition to an ID for each bus and an ID for each machine, there is
914 * an ID for each address that the bus is listening on; that can
915 * be retrieved with dbus_connection_get_server_id(), though it is
916 * probably not very useful.
918 * @param connection the connection
919 * @param error location to store the error
920 * @returns the bus ID or #NULL if error is set
923 dbus_bus_get_id (DBusConnection *connection,
926 DBusMessage *message, *reply;
928 const char *v_STRING;
930 _dbus_return_val_if_fail (connection != NULL, NULL);
931 _dbus_return_val_if_error_is_set (error, NULL);
933 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
940 _DBUS_SET_OOM (error);
944 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
947 dbus_message_unref (message);
951 _DBUS_ASSERT_ERROR_IS_SET (error);
955 if (dbus_set_error_from_message (error, reply))
957 _DBUS_ASSERT_ERROR_IS_SET (error);
958 dbus_message_unref (reply);
963 if (!dbus_message_get_args (reply, error,
964 DBUS_TYPE_STRING, &v_STRING,
967 _DBUS_ASSERT_ERROR_IS_SET (error);
968 dbus_message_unref (reply);
972 id = _dbus_strdup (v_STRING); /* may be NULL */
974 dbus_message_unref (reply);
977 _DBUS_SET_OOM (error);
979 /* FIXME it might be nice to cache the ID locally */
985 * Asks the bus to assign the given name to this connection by invoking
986 * the RequestName method on the bus. This method is fully documented
987 * in the D-Bus specification. For quick reference, the flags and
988 * result codes are discussed here, but the specification is the
989 * canonical version of this information.
991 * First you should know that for each bus name, the bus stores
992 * a queue of connections that would like to own it. Only
993 * one owns it at a time - called the primary owner. If the primary
994 * owner releases the name or disconnects, then the next owner in the
995 * queue atomically takes over.
997 * So for example if you have an application org.freedesktop.TextEditor
998 * and multiple instances of it can be run, you can have all of them
999 * sitting in the queue. The first one to start up will receive messages
1000 * sent to org.freedesktop.TextEditor, but if that one exits another
1001 * will become the primary owner and receive messages.
1003 * The queue means you don't need to manually watch for the current owner to
1004 * disappear and then request the name again.
1006 * When requesting a name, you can specify several flags.
1008 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT and #DBUS_NAME_FLAG_DO_NOT_QUEUE
1009 * are properties stored by the bus for this connection with respect to
1010 * each requested bus name. These properties are stored even if the
1011 * connection is queued and does not become the primary owner.
1012 * You can update these flags by calling RequestName again (even if
1013 * you already own the name).
1015 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT means that another requestor of the
1016 * name can take it away from you by specifying #DBUS_NAME_FLAG_REPLACE_EXISTING.
1018 * #DBUS_NAME_FLAG_DO_NOT_QUEUE means that if you aren't the primary owner,
1019 * you don't want to be queued up - you only care about being the
1022 * Unlike the other two flags, #DBUS_NAME_FLAG_REPLACE_EXISTING is a property
1023 * of the individual RequestName call, i.e. the bus does not persistently
1024 * associate it with the connection-name pair. If a RequestName call includes
1025 * the #DBUS_NAME_FLAG_REPLACE_EXISTING flag, and the current primary
1026 * owner has #DBUS_NAME_FLAG_ALLOW_REPLACEMENT set, then the current primary
1027 * owner will be kicked off.
1029 * If no flags are given, an application will receive the requested
1030 * name only if the name is currently unowned; and it will NOT give
1031 * up the name if another application asks to take it over using
1032 * #DBUS_NAME_FLAG_REPLACE_EXISTING.
1034 * This function returns a result code. The possible result codes
1037 * #DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER means that the name had no
1038 * existing owner, and the caller is now the primary owner; or that
1039 * the name had an owner, and the caller specified
1040 * #DBUS_NAME_FLAG_REPLACE_EXISTING, and the current owner
1041 * specified #DBUS_NAME_FLAG_ALLOW_REPLACEMENT.
1043 * #DBUS_REQUEST_NAME_REPLY_IN_QUEUE happens only if the caller does NOT
1044 * specify #DBUS_NAME_FLAG_DO_NOT_QUEUE and either the current owner
1045 * did NOT specify #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or the caller did NOT
1046 * specify #DBUS_NAME_FLAG_REPLACE_EXISTING. In this case the caller ends up
1047 * in a queue to own the name after the current owner gives it up.
1049 * #DBUS_REQUEST_NAME_REPLY_EXISTS happens if the name has an owner
1050 * already and the caller specifies #DBUS_NAME_FLAG_DO_NOT_QUEUE
1051 * and either the current owner has NOT specified
1052 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or the caller did NOT specify
1053 * #DBUS_NAME_FLAG_REPLACE_EXISTING.
1055 * #DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER happens if an application
1056 * requests a name it already owns. (Re-requesting a name is useful if
1057 * you want to change the #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or
1058 * #DBUS_NAME_FLAG_DO_NOT_QUEUE settings.)
1060 * When a service represents an application, say "text editor," then
1061 * it should specify #DBUS_NAME_FLAG_ALLOW_REPLACEMENT if it wants
1062 * the last editor started to be the user's editor vs. the first one
1063 * started. Then any editor that can be the user's editor should
1064 * specify #DBUS_NAME_FLAG_REPLACE_EXISTING to either take over
1065 * (last-started-wins) or be queued up (first-started-wins) according
1066 * to whether #DBUS_NAME_FLAG_ALLOW_REPLACEMENT was given.
1068 * Conventionally, single-instance applications often offer a command
1069 * line option called --replace which means to replace the current
1070 * instance. To implement this, always set
1071 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT when you request your
1072 * application's bus name. When you lose ownership of your bus name,
1073 * you need to exit. Look for the signal "NameLost" from
1074 * #DBUS_SERVICE_DBUS and #DBUS_INTERFACE_DBUS (the signal's first
1075 * argument is the bus name that was lost). If starting up without
1076 * --replace, do not specify #DBUS_NAME_FLAG_REPLACE_EXISTING, and
1077 * exit if you fail to become the bus name owner. If --replace is
1078 * given, ask to replace the old owner.
1080 * @param connection the connection
1081 * @param name the name to request
1082 * @param flags flags
1083 * @param error location to store the error
1084 * @returns a result code, -1 if error is set
1087 dbus_bus_request_name (DBusConnection *connection,
1092 DBusMessage *message, *reply;
1093 dbus_uint32_t result;
1095 _dbus_return_val_if_fail (connection != NULL, 0);
1096 _dbus_return_val_if_fail (name != NULL, 0);
1097 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
1098 _dbus_return_val_if_error_is_set (error, 0);
1100 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1102 DBUS_INTERFACE_DBUS,
1105 if (message == NULL)
1107 _DBUS_SET_OOM (error);
1111 if (!dbus_message_append_args (message,
1112 DBUS_TYPE_STRING, &name,
1113 DBUS_TYPE_UINT32, &flags,
1116 dbus_message_unref (message);
1117 _DBUS_SET_OOM (error);
1121 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
1124 dbus_message_unref (message);
1128 _DBUS_ASSERT_ERROR_IS_SET (error);
1132 if (dbus_set_error_from_message (error, reply))
1134 _DBUS_ASSERT_ERROR_IS_SET (error);
1135 dbus_message_unref (reply);
1139 if (!dbus_message_get_args (reply, error,
1140 DBUS_TYPE_UINT32, &result,
1143 _DBUS_ASSERT_ERROR_IS_SET (error);
1144 dbus_message_unref (reply);
1148 dbus_message_unref (reply);
1155 * Asks the bus to unassign the given name from this connection by
1156 * invoking the ReleaseName method on the bus. The "ReleaseName"
1157 * method is canonically documented in the D-Bus specification.
1159 * Possible results are: #DBUS_RELEASE_NAME_REPLY_RELEASED
1160 * which means you owned the name or were in the queue to own it,
1161 * and and now you don't own it and aren't in the queue.
1162 * #DBUS_RELEASE_NAME_REPLY_NOT_OWNER which means someone else
1163 * owns the name so you can't release it.
1164 * #DBUS_RELEASE_NAME_REPLY_NON_EXISTENT
1165 * which means nobody owned the name.
1167 * @param connection the connection
1168 * @param name the name to remove
1169 * @param error location to store the error
1170 * @returns a result code, -1 if error is set
1173 dbus_bus_release_name (DBusConnection *connection,
1177 DBusMessage *message, *reply;
1178 dbus_uint32_t result;
1180 _dbus_return_val_if_fail (connection != NULL, 0);
1181 _dbus_return_val_if_fail (name != NULL, 0);
1182 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
1183 _dbus_return_val_if_error_is_set (error, 0);
1185 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1187 DBUS_INTERFACE_DBUS,
1190 if (message == NULL)
1192 _DBUS_SET_OOM (error);
1196 if (!dbus_message_append_args (message,
1197 DBUS_TYPE_STRING, &name,
1200 dbus_message_unref (message);
1201 _DBUS_SET_OOM (error);
1205 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
1208 dbus_message_unref (message);
1212 _DBUS_ASSERT_ERROR_IS_SET (error);
1216 if (dbus_set_error_from_message (error, reply))
1218 _DBUS_ASSERT_ERROR_IS_SET (error);
1219 dbus_message_unref (reply);
1223 if (!dbus_message_get_args (reply, error,
1224 DBUS_TYPE_UINT32, &result,
1227 _DBUS_ASSERT_ERROR_IS_SET (error);
1228 dbus_message_unref (reply);
1232 dbus_message_unref (reply);
1238 * Asks the bus whether a certain name has an owner.
1240 * Using this can easily result in a race condition,
1241 * since an owner can appear or disappear after you
1244 * If you want to request a name, just request it;
1245 * if you want to avoid replacing a current owner,
1246 * don't specify #DBUS_NAME_FLAG_REPLACE_EXISTING and
1247 * you will get an error if there's already an owner.
1249 * @param connection the connection
1250 * @param name the name
1251 * @param error location to store any errors
1252 * @returns #TRUE if the name exists, #FALSE if not or on error
1255 dbus_bus_name_has_owner (DBusConnection *connection,
1259 DBusMessage *message, *reply;
1262 _dbus_return_val_if_fail (connection != NULL, FALSE);
1263 _dbus_return_val_if_fail (name != NULL, FALSE);
1264 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
1265 _dbus_return_val_if_error_is_set (error, FALSE);
1267 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1269 DBUS_INTERFACE_DBUS,
1271 if (message == NULL)
1273 _DBUS_SET_OOM (error);
1277 if (!dbus_message_append_args (message,
1278 DBUS_TYPE_STRING, &name,
1281 dbus_message_unref (message);
1282 _DBUS_SET_OOM (error);
1286 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
1287 dbus_message_unref (message);
1291 _DBUS_ASSERT_ERROR_IS_SET (error);
1295 if (!dbus_message_get_args (reply, error,
1296 DBUS_TYPE_BOOLEAN, &exists,
1299 _DBUS_ASSERT_ERROR_IS_SET (error);
1300 dbus_message_unref (reply);
1304 dbus_message_unref (reply);
1309 * Starts a service that will request ownership of the given name.
1310 * The returned result will be one of be one of
1311 * #DBUS_START_REPLY_SUCCESS or #DBUS_START_REPLY_ALREADY_RUNNING if
1312 * successful. Pass #NULL if you don't care about the result.
1314 * The flags parameter is for future expansion, currently you should
1317 * It's often easier to avoid explicitly starting services, and
1318 * just send a method call to the service's bus name instead.
1319 * Method calls start a service to handle them by default
1320 * unless you call dbus_message_set_auto_start() to disable this
1323 * @param connection the connection
1324 * @param name the name we want the new service to request
1325 * @param flags the flags (should always be 0 for now)
1326 * @param result a place to store the result or #NULL
1327 * @param error location to store any errors
1328 * @returns #TRUE if the activation succeeded, #FALSE if not
1331 dbus_bus_start_service_by_name (DBusConnection *connection,
1333 dbus_uint32_t flags,
1334 dbus_uint32_t *result,
1340 _dbus_return_val_if_fail (connection != NULL, FALSE);
1341 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
1343 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1345 DBUS_INTERFACE_DBUS,
1346 "StartServiceByName");
1348 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &name,
1349 DBUS_TYPE_UINT32, &flags, DBUS_TYPE_INVALID))
1351 dbus_message_unref (msg);
1352 _DBUS_SET_OOM (error);
1356 reply = dbus_connection_send_with_reply_and_block (connection, msg,
1358 dbus_message_unref (msg);
1362 _DBUS_ASSERT_ERROR_IS_SET (error);
1366 if (dbus_set_error_from_message (error, reply))
1368 _DBUS_ASSERT_ERROR_IS_SET (error);
1369 dbus_message_unref (reply);
1373 if (result != NULL &&
1374 !dbus_message_get_args (reply, error, DBUS_TYPE_UINT32,
1375 result, DBUS_TYPE_INVALID))
1377 _DBUS_ASSERT_ERROR_IS_SET (error);
1378 dbus_message_unref (reply);
1382 dbus_message_unref (reply);
1387 send_no_return_values (DBusConnection *connection,
1393 /* Block to check success codepath */
1396 reply = dbus_connection_send_with_reply_and_block (connection, msg,
1400 _DBUS_ASSERT_ERROR_IS_SET (error);
1402 dbus_message_unref (reply);
1406 /* Silently-fail nonblocking codepath */
1407 dbus_message_set_no_reply (msg, TRUE);
1408 dbus_connection_send (connection, msg, NULL);
1413 * Adds a match rule to match messages going through the message bus.
1414 * The "rule" argument is the string form of a match rule.
1416 * If you pass #NULL for the error, this function will not
1417 * block; the match thus won't be added until you flush the
1418 * connection, and if there's an error adding the match
1419 * you won't find out about it. This is generally acceptable, since the
1420 * possible errors (including a lack of resources in the bus, the connection
1421 * having exceeded its quota of active match rules, or the match rule being
1422 * unparseable) are generally unrecoverable.
1424 * If you pass non-#NULL for the error this function will
1425 * block until it gets a reply. This may be useful when using match rule keys
1426 * introduced in recent versions of D-Bus, like 'arg0namespace', to allow the
1427 * application to fall back to less efficient match rules supported by older
1428 * versions of the daemon if the running version is not new enough; or when
1429 * using user-supplied rules rather than rules hard-coded at compile time.
1431 * Normal API conventions would have the function return
1432 * a boolean value indicating whether the error was set,
1433 * but that would require blocking always to determine
1436 * The AddMatch method is fully documented in the D-Bus
1437 * specification. For quick reference, the format of the
1438 * match rules is discussed here, but the specification
1439 * is the canonical version of this information.
1441 * Rules are specified as a string of comma separated
1442 * key/value pairs. An example is
1443 * "type='signal',sender='org.freedesktop.DBus',
1444 * interface='org.freedesktop.DBus',member='Foo',
1445 * path='/bar/foo',destination=':452345.34'"
1447 * Possible keys you can match on are type, sender,
1448 * interface, member, path, destination and numbered
1449 * keys to match message args (keys are 'arg0', 'arg1', etc.).
1450 * Omitting a key from the rule indicates
1451 * a wildcard match. For instance omitting
1452 * the member from a match rule but adding a sender would
1453 * let all messages from that sender through regardless of
1456 * Matches are inclusive not exclusive so as long as one
1457 * rule matches the message will get through. It is important
1458 * to note this because every time a message is received the
1459 * application will be paged into memory to process it. This
1460 * can cause performance problems such as draining batteries
1461 * on embedded platforms.
1463 * If you match message args ('arg0', 'arg1', and so forth)
1464 * only string arguments will match. That is, arg0='5' means
1465 * match the string "5" not the integer 5.
1467 * Currently there is no way to match against non-string arguments.
1469 * A specialised form of wildcard matching on arguments is
1470 * supported for path-like namespaces. If your argument match has
1471 * a 'path' suffix (eg: "arg0path='/some/path/'") then it is
1472 * considered a match if the argument exactly matches the given
1473 * string or if one of them ends in a '/' and is a prefix of the
1476 * Matching on interface is tricky because method call
1477 * messages only optionally specify the interface.
1478 * If a message omits the interface, then it will NOT match
1479 * if the rule specifies an interface name. This means match
1480 * rules on method calls should not usually give an interface.
1482 * However, signal messages are required to include the interface
1483 * so when matching signals usually you should specify the interface
1484 * in the match rule.
1486 * For security reasons, you can match arguments only up to
1487 * #DBUS_MAXIMUM_MATCH_RULE_ARG_NUMBER.
1489 * Match rules have a maximum length of #DBUS_MAXIMUM_MATCH_RULE_LENGTH
1492 * Both of these maximums are much higher than you're likely to need,
1493 * they only exist because the D-Bus bus daemon has fixed limits on
1494 * all resource usage.
1496 * @param connection connection to the message bus
1497 * @param rule textual form of match rule
1498 * @param error location to store any errors
1501 dbus_bus_add_match (DBusConnection *connection,
1507 _dbus_return_if_fail (rule != NULL);
1509 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1511 DBUS_INTERFACE_DBUS,
1516 _DBUS_SET_OOM (error);
1520 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
1523 dbus_message_unref (msg);
1524 _DBUS_SET_OOM (error);
1528 send_no_return_values (connection, msg, error);
1530 dbus_message_unref (msg);
1534 * Removes a previously-added match rule "by value" (the most
1535 * recently-added identical rule gets removed). The "rule" argument
1536 * is the string form of a match rule.
1538 * The bus compares match rules semantically, not textually, so
1539 * whitespace and ordering don't have to be identical to
1540 * the rule you passed to dbus_bus_add_match().
1542 * If you pass #NULL for the error, this function will not
1543 * block; otherwise it will. See detailed explanation in
1544 * docs for dbus_bus_add_match().
1546 * @param connection connection to the message bus
1547 * @param rule textual form of match rule
1548 * @param error location to store any errors
1551 dbus_bus_remove_match (DBusConnection *connection,
1557 _dbus_return_if_fail (rule != NULL);
1559 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1561 DBUS_INTERFACE_DBUS,
1564 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
1567 dbus_message_unref (msg);
1568 _DBUS_SET_OOM (error);
1572 send_no_return_values (connection, msg, error);
1574 dbus_message_unref (msg);