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"
37 * @defgroup DBusBus Message bus APIs
39 * @brief Functions for communicating with the message bus
41 * dbus_bus_get() allows all modules and libraries in a given
42 * process to share the same connection to the bus daemon by storing
43 * the connection globally.
45 * All other functions in this module are just convenience functions;
46 * most of them invoke methods on the bus daemon, by sending method
47 * call messages to #DBUS_SERVICE_DBUS. These convenience functions
48 * often make blocking method calls. If you don't want to block,
49 * you can send the method call messages manually in the same way
50 * you would any other method call message.
52 * This module is the only one in libdbus that's specific to
53 * communicating with the message bus daemon. The rest of the API can
54 * also be used for connecting to another application directly.
56 * @todo right now the default address of the system bus is hardcoded,
57 * so if you change it in the global config file suddenly you have to
58 * set DBUS_SYSTEM_BUS_ADDRESS env variable. Might be nice if the
59 * client lib somehow read the config file, or if the bus on startup
60 * somehow wrote out its address to a well-known spot, but might also
65 * @defgroup DBusBusInternals Message bus APIs internals
66 * @ingroup DBusInternals
67 * @brief Internals of functions for communicating with the message bus
73 * Block of message-bus-related data we attach to each
74 * #DBusConnection used with these convenience functions.
79 DBusConnection *connection; /**< Connection we're associated with */
80 char *unique_name; /**< Unique name of this connection */
82 unsigned int is_well_known : 1; /**< Is one of the well-known connections in our global array */
85 /** The slot we have reserved to store BusData.
87 static dbus_int32_t bus_data_slot = -1;
89 /** Number of bus types */
92 static DBusConnection *bus_connections[N_BUS_TYPES];
93 static char *bus_connection_addresses[N_BUS_TYPES] = { NULL, NULL, NULL };
95 static DBusBusType activation_bus_type = DBUS_BUS_STARTER;
97 static dbus_bool_t initialized = FALSE;
100 addresses_shutdown_func (void *data)
105 while (i < N_BUS_TYPES)
107 if (bus_connections[i] != NULL)
108 _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");
110 dbus_free (bus_connection_addresses[i]);
111 bus_connection_addresses[i] = NULL;
115 activation_bus_type = DBUS_BUS_STARTER;
121 get_from_env (char **connection_p,
126 _dbus_assert (*connection_p == NULL);
128 s = _dbus_getenv (env_var);
129 if (s == NULL || *s == '\0')
130 return TRUE; /* successfully didn't use the env var */
133 *connection_p = _dbus_strdup (s);
134 return *connection_p != NULL;
139 init_session_address (void)
145 /* First, look in the environment. This is the normal case on
146 * freedesktop.org/Unix systems. */
147 get_from_env (&bus_connection_addresses[DBUS_BUS_SESSION],
148 "DBUS_SESSION_BUS_ADDRESS");
149 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
151 dbus_bool_t supported;
153 DBusError error = DBUS_ERROR_INIT;
155 if (!_dbus_string_init (&addr))
159 /* So it's not in the environment - let's try a platform-specific method.
160 * On MacOS, this involves asking launchd. On Windows (not specified yet)
161 * we might do a COM lookup.
162 * Ignore errors - if we failed, fall back to autolaunch. */
163 retval = _dbus_lookup_session_address (&supported, &addr, &error);
164 if (supported && retval)
166 retval =_dbus_string_steal_data (&addr, &bus_connection_addresses[DBUS_BUS_SESSION]);
168 else if (supported && !retval)
170 if (dbus_error_is_set(&error))
171 _dbus_warn ("Dynamic session lookup supported but failed: %s\n", error.message);
173 _dbus_warn ("Dynamic session lookup supported but failed silently\n");
175 _dbus_string_free (&addr);
183 /* We have a hard-coded (but compile-time-configurable) fallback address for
184 * the session bus. */
185 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
186 bus_connection_addresses[DBUS_BUS_SESSION] =
187 _dbus_strdup (DBUS_SESSION_BUS_CONNECT_ADDRESS);
189 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
196 init_connections_unlocked (void)
204 while (i < N_BUS_TYPES)
206 bus_connections[i] = NULL;
210 /* Don't init these twice, we may run this code twice if
211 * init_connections_unlocked() fails midway through.
212 * In practice, each block below should contain only one
213 * "return FALSE" or running through twice may not
217 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
219 _dbus_verbose ("Filling in system bus address...\n");
221 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SYSTEM],
222 "DBUS_SYSTEM_BUS_ADDRESS"))
227 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
229 /* Use default system bus address if none set in environment */
230 bus_connection_addresses[DBUS_BUS_SYSTEM] =
231 _dbus_strdup (DBUS_SYSTEM_BUS_DEFAULT_ADDRESS);
233 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
236 _dbus_verbose (" used default system bus \"%s\"\n",
237 bus_connection_addresses[DBUS_BUS_SYSTEM]);
240 _dbus_verbose (" used env var system bus \"%s\"\n",
241 bus_connection_addresses[DBUS_BUS_SYSTEM]);
243 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
245 _dbus_verbose ("Filling in session bus address...\n");
247 if (!init_session_address ())
250 _dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_SESSION] ?
251 bus_connection_addresses[DBUS_BUS_SESSION] : "none set");
254 if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
256 _dbus_verbose ("Filling in activation bus address...\n");
258 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_STARTER],
259 "DBUS_STARTER_ADDRESS"))
262 _dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_STARTER] ?
263 bus_connection_addresses[DBUS_BUS_STARTER] : "none set");
267 if (bus_connection_addresses[DBUS_BUS_STARTER] != NULL)
269 s = _dbus_getenv ("DBUS_STARTER_BUS_TYPE");
273 _dbus_verbose ("Bus activation type was set to \"%s\"\n", s);
275 if (strcmp (s, "system") == 0)
276 activation_bus_type = DBUS_BUS_SYSTEM;
277 else if (strcmp (s, "session") == 0)
278 activation_bus_type = DBUS_BUS_SESSION;
283 /* Default to the session bus instead if available */
284 if (bus_connection_addresses[DBUS_BUS_SESSION] != NULL)
286 bus_connection_addresses[DBUS_BUS_STARTER] =
287 _dbus_strdup (bus_connection_addresses[DBUS_BUS_SESSION]);
288 if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
293 /* If we return FALSE we have to be sure that restarting
294 * the above code will work right
297 if (!_dbus_register_shutdown_func (addresses_shutdown_func,
308 bus_data_free (void *data)
312 if (bd->is_well_known)
316 if (!_DBUS_LOCK (bus))
317 _dbus_assert_not_reached ("global locks should have been initialized "
318 "when we attached bus data");
320 /* We may be stored in more than one slot */
321 /* This should now be impossible - these slots are supposed to
322 * be cleared on disconnect, so should not need to be cleared on
326 while (i < N_BUS_TYPES)
328 if (bus_connections[i] == bd->connection)
329 bus_connections[i] = NULL;
336 dbus_free (bd->unique_name);
339 dbus_connection_free_data_slot (&bus_data_slot);
343 ensure_bus_data (DBusConnection *connection)
347 if (!dbus_connection_allocate_data_slot (&bus_data_slot))
350 bd = dbus_connection_get_data (connection, bus_data_slot);
353 bd = dbus_new0 (BusData, 1);
356 dbus_connection_free_data_slot (&bus_data_slot);
360 bd->connection = connection;
362 if (!dbus_connection_set_data (connection, bus_data_slot, bd,
366 dbus_connection_free_data_slot (&bus_data_slot);
370 /* Data slot refcount now held by the BusData */
374 dbus_connection_free_data_slot (&bus_data_slot);
381 * Internal function that checks to see if this
382 * is a shared connection owned by the bus and if it is unref it.
384 * @param connection a connection that has been disconnected.
387 _dbus_bus_notify_shared_connection_disconnected_unlocked (DBusConnection *connection)
391 if (!_DBUS_LOCK (bus))
393 /* If it was in bus_connections, we would have initialized global locks
394 * when we added it. So, it can't be. */
398 /* We are expecting to have the connection saved in only one of these
399 * slots, but someone could in a pathological case set system and session
400 * bus to the same bus or something. Or set one of them to the starter
401 * bus without setting the starter bus type in the env variable.
402 * So we don't break the loop as soon as we find a match.
404 for (i = 0; i < N_BUS_TYPES; ++i)
406 if (bus_connections[i] == connection)
408 bus_connections[i] = NULL;
415 static DBusConnection *
416 internal_bus_get (DBusBusType type,
421 DBusConnection *connection;
423 DBusBusType address_type;
425 _dbus_return_val_if_fail (type >= 0 && type < N_BUS_TYPES, NULL);
426 _dbus_return_val_if_error_is_set (error, NULL);
430 if (!_DBUS_LOCK (bus))
432 _DBUS_SET_OOM (error);
433 /* do not "goto out", that would try to unlock */
437 if (!init_connections_unlocked ())
439 _DBUS_SET_OOM (error);
443 /* We want to use the activation address even if the
444 * activating bus is the session or system bus,
449 /* Use the real type of the activation bus for getting its
450 * connection, but only if the real type's address is available. (If
451 * the activating bus isn't a well-known bus then
452 * activation_bus_type == DBUS_BUS_STARTER)
454 if (type == DBUS_BUS_STARTER &&
455 bus_connection_addresses[activation_bus_type] != NULL)
456 type = activation_bus_type;
458 if (!private && bus_connections[type] != NULL)
460 connection = bus_connections[type];
461 dbus_connection_ref (connection);
465 address = bus_connection_addresses[address_type];
468 dbus_set_error (error, DBUS_ERROR_FAILED,
469 "Unable to determine the address of the message bus (try 'man dbus-launch' and 'man dbus-daemon' for help)");
474 connection = dbus_connection_open_private (address, error);
476 connection = dbus_connection_open (address, error);
483 if (!dbus_bus_register (connection, error))
485 _dbus_connection_close_possibly_shared (connection);
486 dbus_connection_unref (connection);
493 /* store a weak ref to the connection (dbus-connection.c is
494 * supposed to have a strong ref that it drops on disconnect,
495 * since this is a shared connection)
497 bus_connections[type] = connection;
500 /* By default we're bound to the lifecycle of
503 dbus_connection_set_exit_on_disconnect (connection,
506 if (!_DBUS_LOCK (bus_datas))
507 _dbus_assert_not_reached ("global locks were initialized already");
509 bd = ensure_bus_data (connection);
510 _dbus_assert (bd != NULL); /* it should have been created on
511 register, so OOM not possible */
512 bd->is_well_known = TRUE;
513 _DBUS_UNLOCK (bus_datas);
516 /* Return a reference to the caller, or NULL with error set. */
517 if (connection == NULL)
518 _DBUS_ASSERT_ERROR_IS_SET (error);
525 /** @} */ /* end of implementation details docs */
528 * @addtogroup DBusBus
533 * Connects to a bus daemon and registers the client with it. If a
534 * connection to the bus already exists, then that connection is
535 * returned. The caller of this function owns a reference to the bus.
537 * The caller may NOT call dbus_connection_close() on this connection;
538 * see dbus_connection_open() and dbus_connection_close() for details
541 * If this function obtains a new connection object never before
542 * returned from dbus_bus_get(), it will call
543 * dbus_connection_set_exit_on_disconnect(), so the application
544 * will exit if the connection closes. You can undo this
545 * by calling dbus_connection_set_exit_on_disconnect() yourself
546 * after you get the connection.
548 * dbus_bus_get() calls dbus_bus_register() for you.
550 * If returning a newly-created connection, this function will block
551 * until authentication and bus registration are complete.
553 * @param type bus type
554 * @param error address where an error can be returned.
555 * @returns a #DBusConnection with new ref or #NULL on error
558 dbus_bus_get (DBusBusType type,
561 return internal_bus_get (type, FALSE, error);
565 * Connects to a bus daemon and registers the client with it as with
566 * dbus_bus_register(). Unlike dbus_bus_get(), always creates a new
567 * connection. This connection will not be saved or recycled by
568 * libdbus. Caller owns a reference to the bus and must either close
569 * it or know it to be closed prior to releasing this reference.
571 * See dbus_connection_open_private() for more details on when to
572 * close and unref this connection.
574 * This function calls
575 * dbus_connection_set_exit_on_disconnect() on the new connection, so the application
576 * will exit if the connection closes. You can undo this
577 * by calling dbus_connection_set_exit_on_disconnect() yourself
578 * after you get the connection.
580 * dbus_bus_get_private() calls dbus_bus_register() for you.
582 * This function will block until authentication and bus registration
585 * @param type bus type
586 * @param error address where an error can be returned.
587 * @returns a DBusConnection with new ref
590 dbus_bus_get_private (DBusBusType type,
593 return internal_bus_get (type, TRUE, error);
597 * Registers a connection with the bus. This must be the first
598 * thing an application does when connecting to the message bus.
599 * If registration succeeds, the unique name will be set,
600 * and can be obtained using dbus_bus_get_unique_name().
602 * This function will block until registration is complete.
604 * If the connection has already registered with the bus
605 * (determined by checking whether dbus_bus_get_unique_name()
606 * returns a non-#NULL value), then this function does nothing.
608 * If you use dbus_bus_get() or dbus_bus_get_private() this
609 * function will be called for you.
611 * @note Just use dbus_bus_get() or dbus_bus_get_private() instead of
612 * dbus_bus_register() and save yourself some pain. Using
613 * dbus_bus_register() manually is only useful if you have your
614 * own custom message bus not found in #DBusBusType.
616 * If you open a bus connection with dbus_connection_open() or
617 * dbus_connection_open_private() you will have to dbus_bus_register()
618 * yourself, or make the appropriate registration method calls
619 * yourself. If you send the method calls yourself, call
620 * dbus_bus_set_unique_name() with the unique bus name you get from
623 * For shared connections (created with dbus_connection_open()) in a
624 * multithreaded application, you can't really make the registration
625 * calls yourself, because you don't know whether some other thread is
626 * also registering, and the bus will kick you off if you send two
627 * registration messages.
629 * If you use dbus_bus_register() however, there is a lock that
630 * keeps both apps from registering at the same time.
632 * The rule in a multithreaded app, then, is that dbus_bus_register()
633 * must be used to register, or you need to have your own locks that
634 * all threads in the app will respect.
636 * In a single-threaded application you can register by hand instead
637 * of using dbus_bus_register(), as long as you check
638 * dbus_bus_get_unique_name() to see if a unique name has already been
639 * stored by another thread before you send the registration messages.
641 * @param connection the connection
642 * @param error place to store errors
643 * @returns #TRUE on success
646 dbus_bus_register (DBusConnection *connection,
649 DBusMessage *message, *reply;
654 _dbus_return_val_if_fail (connection != NULL, FALSE);
655 _dbus_return_val_if_error_is_set (error, FALSE);
661 if (!_DBUS_LOCK (bus_datas))
663 _DBUS_SET_OOM (error);
664 /* do not "goto out", that would try to unlock */
668 bd = ensure_bus_data (connection);
671 _DBUS_SET_OOM (error);
675 if (bd->unique_name != NULL)
677 _dbus_verbose ("Ignoring attempt to register the same DBusConnection %s with the message bus a second time.\n",
684 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
691 _DBUS_SET_OOM (error);
695 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
699 else if (dbus_set_error_from_message (error, reply))
701 else if (!dbus_message_get_args (reply, error,
702 DBUS_TYPE_STRING, &name,
706 bd->unique_name = _dbus_strdup (name);
707 if (bd->unique_name == NULL)
709 _DBUS_SET_OOM (error);
716 _DBUS_UNLOCK (bus_datas);
719 dbus_message_unref (message);
722 dbus_message_unref (reply);
725 _DBUS_ASSERT_ERROR_IS_SET (error);
732 * Sets the unique name of the connection, as assigned by the message
733 * bus. Can only be used if you registered with the bus manually
734 * (i.e. if you did not call dbus_bus_register()). Can only be called
735 * once per connection. After the unique name is set, you can get it
736 * with dbus_bus_get_unique_name().
738 * The only reason to use this function is to re-implement the
739 * equivalent of dbus_bus_register() yourself. One (probably unusual)
740 * reason to do that might be to do the bus registration call
741 * asynchronously instead of synchronously.
743 * @note Just use dbus_bus_get() or dbus_bus_get_private(), or worst
744 * case dbus_bus_register(), instead of messing with this
745 * function. There's really no point creating pain for yourself by
746 * doing things manually.
748 * It's hard to use this function safely on shared connections
749 * (created by dbus_connection_open()) in a multithreaded application,
750 * because only one registration attempt can be sent to the bus. If
751 * two threads are both sending the registration message, there is no
752 * mechanism in libdbus itself to avoid sending it twice.
754 * Thus, you need a way to coordinate which thread sends the
755 * registration attempt; which also means you know which thread
756 * will call dbus_bus_set_unique_name(). If you don't know
757 * about all threads in the app (for example, if some libraries
758 * you're using might start libdbus-using threads), then you
759 * need to avoid using this function on shared connections.
761 * @param connection the connection
762 * @param unique_name the unique name
763 * @returns #FALSE if not enough memory
766 dbus_bus_set_unique_name (DBusConnection *connection,
767 const char *unique_name)
770 dbus_bool_t success = FALSE;
772 _dbus_return_val_if_fail (connection != NULL, FALSE);
773 _dbus_return_val_if_fail (unique_name != NULL, FALSE);
775 if (!_DBUS_LOCK (bus_datas))
777 /* do not "goto out", that would try to unlock */
781 bd = ensure_bus_data (connection);
785 _dbus_assert (bd->unique_name == NULL);
787 bd->unique_name = _dbus_strdup (unique_name);
788 success = bd->unique_name != NULL;
791 _DBUS_UNLOCK (bus_datas);
797 * Gets the unique name of the connection as assigned by the message
798 * bus. Only possible after the connection has been registered with
799 * the message bus. All connections returned by dbus_bus_get() or
800 * dbus_bus_get_private() have been successfully registered.
802 * The name remains valid until the connection is freed, and
803 * should not be freed by the caller.
805 * Other than dbus_bus_get(), there are two ways to set the unique
806 * name; one is dbus_bus_register(), the other is
807 * dbus_bus_set_unique_name(). You are responsible for calling
808 * dbus_bus_set_unique_name() if you register by hand instead of using
809 * dbus_bus_register().
811 * @param connection the connection
812 * @returns the unique name or #NULL on error
815 dbus_bus_get_unique_name (DBusConnection *connection)
818 const char *unique_name = NULL;
820 _dbus_return_val_if_fail (connection != NULL, NULL);
822 if (!_DBUS_LOCK (bus_datas))
824 /* We'd have initialized locks when we gave it its unique name, if it
825 * had one. Don't "goto out", that would try to unlock. */
829 bd = ensure_bus_data (connection);
833 unique_name = bd->unique_name;
836 _DBUS_UNLOCK (bus_datas);
842 * Asks the bus to return the UID the named connection authenticated
843 * as, if any. Only works on UNIX; only works for connections on the
844 * same machine as the bus. If you are not on the same machine as the
845 * bus, then calling this is probably a bad idea, since the UID will
846 * mean little to your application.
848 * For the system message bus you're guaranteed to be on the same
849 * machine since it only listens on a UNIX domain socket (at least,
850 * as shipped by default).
852 * This function only works for connections that authenticated as
853 * a UNIX user, right now that includes all bus connections, but
854 * it's very possible to have connections with no associated UID.
855 * So check for errors and do something sensible if they happen.
857 * This function will always return an error on Windows.
859 * @param connection the connection
860 * @param name a name owned by the connection
861 * @param error location to store the error
862 * @returns the unix user id, or ((unsigned)-1) if error is set
865 dbus_bus_get_unix_user (DBusConnection *connection,
869 DBusMessage *message, *reply;
872 _dbus_return_val_if_fail (connection != NULL, DBUS_UID_UNSET);
873 _dbus_return_val_if_fail (name != NULL, DBUS_UID_UNSET);
874 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), DBUS_UID_UNSET);
875 _dbus_return_val_if_error_is_set (error, DBUS_UID_UNSET);
877 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
880 "GetConnectionUnixUser");
884 _DBUS_SET_OOM (error);
885 return DBUS_UID_UNSET;
888 if (!dbus_message_append_args (message,
889 DBUS_TYPE_STRING, &name,
892 dbus_message_unref (message);
893 _DBUS_SET_OOM (error);
894 return DBUS_UID_UNSET;
897 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
900 dbus_message_unref (message);
904 _DBUS_ASSERT_ERROR_IS_SET (error);
905 return DBUS_UID_UNSET;
908 if (dbus_set_error_from_message (error, reply))
910 _DBUS_ASSERT_ERROR_IS_SET (error);
911 dbus_message_unref (reply);
912 return DBUS_UID_UNSET;
915 if (!dbus_message_get_args (reply, error,
916 DBUS_TYPE_UINT32, &uid,
919 _DBUS_ASSERT_ERROR_IS_SET (error);
920 dbus_message_unref (reply);
921 return DBUS_UID_UNSET;
924 dbus_message_unref (reply);
926 return (unsigned long) uid;
930 * Asks the bus to return its globally unique ID, as described in the
931 * D-Bus specification. For the session bus, this is useful as a way
932 * to uniquely identify each user session. For the system bus,
933 * probably the bus ID is not useful; instead, use the machine ID
934 * since it's accessible without necessarily connecting to the bus and
935 * may be persistent beyond a single bus instance (across reboots for
936 * example). See dbus_get_local_machine_id().
938 * In addition to an ID for each bus and an ID for each machine, there is
939 * an ID for each address that the bus is listening on; that can
940 * be retrieved with dbus_connection_get_server_id(), though it is
941 * probably not very useful.
943 * @param connection the connection
944 * @param error location to store the error
945 * @returns the bus ID or #NULL if error is set
948 dbus_bus_get_id (DBusConnection *connection,
951 DBusMessage *message, *reply;
953 const char *v_STRING;
955 _dbus_return_val_if_fail (connection != NULL, NULL);
956 _dbus_return_val_if_error_is_set (error, NULL);
958 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
965 _DBUS_SET_OOM (error);
969 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
972 dbus_message_unref (message);
976 _DBUS_ASSERT_ERROR_IS_SET (error);
980 if (dbus_set_error_from_message (error, reply))
982 _DBUS_ASSERT_ERROR_IS_SET (error);
983 dbus_message_unref (reply);
988 if (!dbus_message_get_args (reply, error,
989 DBUS_TYPE_STRING, &v_STRING,
992 _DBUS_ASSERT_ERROR_IS_SET (error);
993 dbus_message_unref (reply);
997 id = _dbus_strdup (v_STRING); /* may be NULL */
999 dbus_message_unref (reply);
1002 _DBUS_SET_OOM (error);
1004 /* FIXME it might be nice to cache the ID locally */
1010 * Asks the bus to assign the given name to this connection by invoking
1011 * the RequestName method on the bus. This method is fully documented
1012 * in the D-Bus specification. For quick reference, the flags and
1013 * result codes are discussed here, but the specification is the
1014 * canonical version of this information.
1016 * First you should know that for each bus name, the bus stores
1017 * a queue of connections that would like to own it. Only
1018 * one owns it at a time - called the primary owner. If the primary
1019 * owner releases the name or disconnects, then the next owner in the
1020 * queue atomically takes over.
1022 * So for example if you have an application org.freedesktop.TextEditor
1023 * and multiple instances of it can be run, you can have all of them
1024 * sitting in the queue. The first one to start up will receive messages
1025 * sent to org.freedesktop.TextEditor, but if that one exits another
1026 * will become the primary owner and receive messages.
1028 * The queue means you don't need to manually watch for the current owner to
1029 * disappear and then request the name again.
1031 * When requesting a name, you can specify several flags.
1033 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT and #DBUS_NAME_FLAG_DO_NOT_QUEUE
1034 * are properties stored by the bus for this connection with respect to
1035 * each requested bus name. These properties are stored even if the
1036 * connection is queued and does not become the primary owner.
1037 * You can update these flags by calling RequestName again (even if
1038 * you already own the name).
1040 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT means that another requestor of the
1041 * name can take it away from you by specifying #DBUS_NAME_FLAG_REPLACE_EXISTING.
1043 * #DBUS_NAME_FLAG_DO_NOT_QUEUE means that if you aren't the primary owner,
1044 * you don't want to be queued up - you only care about being the
1047 * Unlike the other two flags, #DBUS_NAME_FLAG_REPLACE_EXISTING is a property
1048 * of the individual RequestName call, i.e. the bus does not persistently
1049 * associate it with the connection-name pair. If a RequestName call includes
1050 * the #DBUS_NAME_FLAG_REPLACE_EXISTING flag, and the current primary
1051 * owner has #DBUS_NAME_FLAG_ALLOW_REPLACEMENT set, then the current primary
1052 * owner will be kicked off.
1054 * If no flags are given, an application will receive the requested
1055 * name only if the name is currently unowned; and it will NOT give
1056 * up the name if another application asks to take it over using
1057 * #DBUS_NAME_FLAG_REPLACE_EXISTING.
1059 * This function returns a result code. The possible result codes
1062 * #DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER means that the name had no
1063 * existing owner, and the caller is now the primary owner; or that
1064 * the name had an owner, and the caller specified
1065 * #DBUS_NAME_FLAG_REPLACE_EXISTING, and the current owner
1066 * specified #DBUS_NAME_FLAG_ALLOW_REPLACEMENT.
1068 * #DBUS_REQUEST_NAME_REPLY_IN_QUEUE happens only if the caller does NOT
1069 * specify #DBUS_NAME_FLAG_DO_NOT_QUEUE and either the current owner
1070 * did NOT specify #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or the caller did NOT
1071 * specify #DBUS_NAME_FLAG_REPLACE_EXISTING. In this case the caller ends up
1072 * in a queue to own the name after the current owner gives it up.
1074 * #DBUS_REQUEST_NAME_REPLY_EXISTS happens if the name has an owner
1075 * already and the caller specifies #DBUS_NAME_FLAG_DO_NOT_QUEUE
1076 * and either the current owner has NOT specified
1077 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or the caller did NOT specify
1078 * #DBUS_NAME_FLAG_REPLACE_EXISTING.
1080 * #DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER happens if an application
1081 * requests a name it already owns. (Re-requesting a name is useful if
1082 * you want to change the #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or
1083 * #DBUS_NAME_FLAG_DO_NOT_QUEUE settings.)
1085 * When a service represents an application, say "text editor," then
1086 * it should specify #DBUS_NAME_FLAG_ALLOW_REPLACEMENT if it wants
1087 * the last editor started to be the user's editor vs. the first one
1088 * started. Then any editor that can be the user's editor should
1089 * specify #DBUS_NAME_FLAG_REPLACE_EXISTING to either take over
1090 * (last-started-wins) or be queued up (first-started-wins) according
1091 * to whether #DBUS_NAME_FLAG_ALLOW_REPLACEMENT was given.
1093 * Conventionally, single-instance applications often offer a command
1094 * line option called --replace which means to replace the current
1095 * instance. To implement this, always set
1096 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT when you request your
1097 * application's bus name. When you lose ownership of your bus name,
1098 * you need to exit. Look for the signal "NameLost" from
1099 * #DBUS_SERVICE_DBUS and #DBUS_INTERFACE_DBUS (the signal's first
1100 * argument is the bus name that was lost). If starting up without
1101 * --replace, do not specify #DBUS_NAME_FLAG_REPLACE_EXISTING, and
1102 * exit if you fail to become the bus name owner. If --replace is
1103 * given, ask to replace the old owner.
1105 * @param connection the connection
1106 * @param name the name to request
1107 * @param flags flags
1108 * @param error location to store the error
1109 * @returns a result code, -1 if error is set
1112 dbus_bus_request_name (DBusConnection *connection,
1117 DBusMessage *message, *reply;
1118 dbus_uint32_t result;
1120 _dbus_return_val_if_fail (connection != NULL, 0);
1121 _dbus_return_val_if_fail (name != NULL, 0);
1122 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
1123 _dbus_return_val_if_error_is_set (error, 0);
1125 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1127 DBUS_INTERFACE_DBUS,
1130 if (message == NULL)
1132 _DBUS_SET_OOM (error);
1136 if (!dbus_message_append_args (message,
1137 DBUS_TYPE_STRING, &name,
1138 DBUS_TYPE_UINT32, &flags,
1141 dbus_message_unref (message);
1142 _DBUS_SET_OOM (error);
1146 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
1149 dbus_message_unref (message);
1153 _DBUS_ASSERT_ERROR_IS_SET (error);
1157 if (dbus_set_error_from_message (error, reply))
1159 _DBUS_ASSERT_ERROR_IS_SET (error);
1160 dbus_message_unref (reply);
1164 if (!dbus_message_get_args (reply, error,
1165 DBUS_TYPE_UINT32, &result,
1168 _DBUS_ASSERT_ERROR_IS_SET (error);
1169 dbus_message_unref (reply);
1173 dbus_message_unref (reply);
1180 * Asks the bus to unassign the given name from this connection by
1181 * invoking the ReleaseName method on the bus. The "ReleaseName"
1182 * method is canonically documented in the D-Bus specification.
1184 * Possible results are: #DBUS_RELEASE_NAME_REPLY_RELEASED
1185 * which means you owned the name or were in the queue to own it,
1186 * and and now you don't own it and aren't in the queue.
1187 * #DBUS_RELEASE_NAME_REPLY_NOT_OWNER which means someone else
1188 * owns the name so you can't release it.
1189 * #DBUS_RELEASE_NAME_REPLY_NON_EXISTENT
1190 * which means nobody owned the name.
1192 * @param connection the connection
1193 * @param name the name to remove
1194 * @param error location to store the error
1195 * @returns a result code, -1 if error is set
1198 dbus_bus_release_name (DBusConnection *connection,
1202 DBusMessage *message, *reply;
1203 dbus_uint32_t result;
1205 _dbus_return_val_if_fail (connection != NULL, 0);
1206 _dbus_return_val_if_fail (name != NULL, 0);
1207 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
1208 _dbus_return_val_if_error_is_set (error, 0);
1210 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1212 DBUS_INTERFACE_DBUS,
1215 if (message == NULL)
1217 _DBUS_SET_OOM (error);
1221 if (!dbus_message_append_args (message,
1222 DBUS_TYPE_STRING, &name,
1225 dbus_message_unref (message);
1226 _DBUS_SET_OOM (error);
1230 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
1233 dbus_message_unref (message);
1237 _DBUS_ASSERT_ERROR_IS_SET (error);
1241 if (dbus_set_error_from_message (error, reply))
1243 _DBUS_ASSERT_ERROR_IS_SET (error);
1244 dbus_message_unref (reply);
1248 if (!dbus_message_get_args (reply, error,
1249 DBUS_TYPE_UINT32, &result,
1252 _DBUS_ASSERT_ERROR_IS_SET (error);
1253 dbus_message_unref (reply);
1257 dbus_message_unref (reply);
1263 * Asks the bus whether a certain name has an owner.
1265 * Using this can easily result in a race condition,
1266 * since an owner can appear or disappear after you
1269 * If you want to request a name, just request it;
1270 * if you want to avoid replacing a current owner,
1271 * don't specify #DBUS_NAME_FLAG_REPLACE_EXISTING and
1272 * you will get an error if there's already an owner.
1274 * @param connection the connection
1275 * @param name the name
1276 * @param error location to store any errors
1277 * @returns #TRUE if the name exists, #FALSE if not or on error
1280 dbus_bus_name_has_owner (DBusConnection *connection,
1284 DBusMessage *message, *reply;
1287 _dbus_return_val_if_fail (connection != NULL, FALSE);
1288 _dbus_return_val_if_fail (name != NULL, FALSE);
1289 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
1290 _dbus_return_val_if_error_is_set (error, FALSE);
1292 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1294 DBUS_INTERFACE_DBUS,
1296 if (message == NULL)
1298 _DBUS_SET_OOM (error);
1302 if (!dbus_message_append_args (message,
1303 DBUS_TYPE_STRING, &name,
1306 dbus_message_unref (message);
1307 _DBUS_SET_OOM (error);
1311 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
1312 dbus_message_unref (message);
1316 _DBUS_ASSERT_ERROR_IS_SET (error);
1320 if (!dbus_message_get_args (reply, error,
1321 DBUS_TYPE_BOOLEAN, &exists,
1324 _DBUS_ASSERT_ERROR_IS_SET (error);
1325 dbus_message_unref (reply);
1329 dbus_message_unref (reply);
1334 * Starts a service that will request ownership of the given name.
1335 * The returned result will be one of be one of
1336 * #DBUS_START_REPLY_SUCCESS or #DBUS_START_REPLY_ALREADY_RUNNING if
1337 * successful. Pass #NULL if you don't care about the result.
1339 * The flags parameter is for future expansion, currently you should
1342 * It's often easier to avoid explicitly starting services, and
1343 * just send a method call to the service's bus name instead.
1344 * Method calls start a service to handle them by default
1345 * unless you call dbus_message_set_auto_start() to disable this
1348 * @param connection the connection
1349 * @param name the name we want the new service to request
1350 * @param flags the flags (should always be 0 for now)
1351 * @param result a place to store the result or #NULL
1352 * @param error location to store any errors
1353 * @returns #TRUE if the activation succeeded, #FALSE if not
1356 dbus_bus_start_service_by_name (DBusConnection *connection,
1358 dbus_uint32_t flags,
1359 dbus_uint32_t *result,
1365 _dbus_return_val_if_fail (connection != NULL, FALSE);
1366 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
1368 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1370 DBUS_INTERFACE_DBUS,
1371 "StartServiceByName");
1373 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &name,
1374 DBUS_TYPE_UINT32, &flags, DBUS_TYPE_INVALID))
1376 dbus_message_unref (msg);
1377 _DBUS_SET_OOM (error);
1381 reply = dbus_connection_send_with_reply_and_block (connection, msg,
1383 dbus_message_unref (msg);
1387 _DBUS_ASSERT_ERROR_IS_SET (error);
1391 if (dbus_set_error_from_message (error, reply))
1393 _DBUS_ASSERT_ERROR_IS_SET (error);
1394 dbus_message_unref (reply);
1398 if (result != NULL &&
1399 !dbus_message_get_args (reply, error, DBUS_TYPE_UINT32,
1400 result, DBUS_TYPE_INVALID))
1402 _DBUS_ASSERT_ERROR_IS_SET (error);
1403 dbus_message_unref (reply);
1407 dbus_message_unref (reply);
1412 send_no_return_values (DBusConnection *connection,
1418 /* Block to check success codepath */
1421 reply = dbus_connection_send_with_reply_and_block (connection, msg,
1425 _DBUS_ASSERT_ERROR_IS_SET (error);
1427 dbus_message_unref (reply);
1431 /* Silently-fail nonblocking codepath */
1432 dbus_message_set_no_reply (msg, TRUE);
1433 dbus_connection_send (connection, msg, NULL);
1438 * Adds a match rule to match messages going through the message bus.
1439 * The "rule" argument is the string form of a match rule.
1441 * If you pass #NULL for the error, this function will not
1442 * block; the match thus won't be added until you flush the
1443 * connection, and if there's an error adding the match
1444 * you won't find out about it. This is generally acceptable, since the
1445 * possible errors (including a lack of resources in the bus, the connection
1446 * having exceeded its quota of active match rules, or the match rule being
1447 * unparseable) are generally unrecoverable.
1449 * If you pass non-#NULL for the error this function will
1450 * block until it gets a reply. This may be useful when using match rule keys
1451 * introduced in recent versions of D-Bus, like 'arg0namespace', to allow the
1452 * application to fall back to less efficient match rules supported by older
1453 * versions of the daemon if the running version is not new enough; or when
1454 * using user-supplied rules rather than rules hard-coded at compile time.
1456 * Normal API conventions would have the function return
1457 * a boolean value indicating whether the error was set,
1458 * but that would require blocking always to determine
1461 * The AddMatch method is fully documented in the D-Bus
1462 * specification. For quick reference, the format of the
1463 * match rules is discussed here, but the specification
1464 * is the canonical version of this information.
1466 * Rules are specified as a string of comma separated
1467 * key/value pairs. An example is
1468 * "type='signal',sender='org.freedesktop.DBus',
1469 * interface='org.freedesktop.DBus',member='Foo',
1470 * path='/bar/foo',destination=':452345.34'"
1472 * Possible keys you can match on are type, sender,
1473 * interface, member, path, destination and numbered
1474 * keys to match message args (keys are 'arg0', 'arg1', etc.).
1475 * Omitting a key from the rule indicates
1476 * a wildcard match. For instance omitting
1477 * the member from a match rule but adding a sender would
1478 * let all messages from that sender through regardless of
1481 * Matches are inclusive not exclusive so as long as one
1482 * rule matches the message will get through. It is important
1483 * to note this because every time a message is received the
1484 * application will be paged into memory to process it. This
1485 * can cause performance problems such as draining batteries
1486 * on embedded platforms.
1488 * If you match message args ('arg0', 'arg1', and so forth)
1489 * only string arguments will match. That is, arg0='5' means
1490 * match the string "5" not the integer 5.
1492 * Currently there is no way to match against non-string arguments.
1494 * A specialised form of wildcard matching on arguments is
1495 * supported for path-like namespaces. If your argument match has
1496 * a 'path' suffix (eg: "arg0path='/some/path/'") then it is
1497 * considered a match if the argument exactly matches the given
1498 * string or if one of them ends in a '/' and is a prefix of the
1501 * Matching on interface is tricky because method call
1502 * messages only optionally specify the interface.
1503 * If a message omits the interface, then it will NOT match
1504 * if the rule specifies an interface name. This means match
1505 * rules on method calls should not usually give an interface.
1507 * However, signal messages are required to include the interface
1508 * so when matching signals usually you should specify the interface
1509 * in the match rule.
1511 * For security reasons, you can match arguments only up to
1512 * #DBUS_MAXIMUM_MATCH_RULE_ARG_NUMBER.
1514 * Match rules have a maximum length of #DBUS_MAXIMUM_MATCH_RULE_LENGTH
1517 * Both of these maximums are much higher than you're likely to need,
1518 * they only exist because the D-Bus bus daemon has fixed limits on
1519 * all resource usage.
1521 * @param connection connection to the message bus
1522 * @param rule textual form of match rule
1523 * @param error location to store any errors
1526 dbus_bus_add_match (DBusConnection *connection,
1532 _dbus_return_if_fail (rule != NULL);
1534 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1536 DBUS_INTERFACE_DBUS,
1541 _DBUS_SET_OOM (error);
1545 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
1548 dbus_message_unref (msg);
1549 _DBUS_SET_OOM (error);
1553 send_no_return_values (connection, msg, error);
1555 dbus_message_unref (msg);
1559 * Removes a previously-added match rule "by value" (the most
1560 * recently-added identical rule gets removed). The "rule" argument
1561 * is the string form of a match rule.
1563 * The bus compares match rules semantically, not textually, so
1564 * whitespace and ordering don't have to be identical to
1565 * the rule you passed to dbus_bus_add_match().
1567 * If you pass #NULL for the error, this function will not
1568 * block; otherwise it will. See detailed explanation in
1569 * docs for dbus_bus_add_match().
1571 * @param connection connection to the message bus
1572 * @param rule textual form of match rule
1573 * @param error location to store any errors
1576 dbus_bus_remove_match (DBusConnection *connection,
1582 _dbus_return_if_fail (rule != NULL);
1584 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1586 DBUS_INTERFACE_DBUS,
1589 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
1592 dbus_message_unref (msg);
1593 _DBUS_SET_OOM (error);
1597 send_no_return_values (connection, msg, error);
1599 dbus_message_unref (msg);