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
26 #include "dbus-protocol.h"
27 #include "dbus-internals.h"
28 #include "dbus-message.h"
29 #include "dbus-marshal-validate.h"
30 #include "dbus-threads-internal.h"
31 #include "dbus-connection-internal.h"
35 * @defgroup DBusBus Message bus APIs
37 * @brief Functions for communicating with the message bus
39 * dbus_bus_get() allows all modules and libraries in a given
40 * process to share the same connection to the bus daemon by storing
41 * the connection globally.
43 * All other functions in this module are just convenience functions;
44 * most of them invoke methods on the bus daemon, by sending method
45 * call messages to #DBUS_SERVICE_DBUS. These convenience functions
46 * often make blocking method calls. If you don't want to block,
47 * you can send the method call messages manually in the same way
48 * you would any other method call message.
50 * This module is the only one in libdbus that's specific to
51 * communicating with the message bus daemon. The rest of the API can
52 * also be used for connecting to another application directly.
54 * @todo right now the default address of the system bus is hardcoded,
55 * so if you change it in the global config file suddenly you have to
56 * set DBUS_SYSTEM_BUS_ADDRESS env variable. Might be nice if the
57 * client lib somehow read the config file, or if the bus on startup
58 * somehow wrote out its address to a well-known spot, but might also
63 * @defgroup DBusBusInternals Message bus APIs internals
64 * @ingroup DBusInternals
65 * @brief Internals of functions for communicating with the message bus
71 * Block of message-bus-related data we attach to each
72 * #DBusConnection used with these convenience functions.
77 DBusConnection *connection; /**< Connection we're associated with */
78 char *unique_name; /**< Unique name of this connection */
80 unsigned int is_well_known : 1; /**< Is one of the well-known connections in our global array */
83 /** The slot we have reserved to store BusData.
85 static dbus_int32_t bus_data_slot = -1;
87 /** Number of bus types */
90 static DBusConnection *bus_connections[N_BUS_TYPES];
91 static char *bus_connection_addresses[N_BUS_TYPES] = { NULL, NULL, NULL };
93 static DBusBusType activation_bus_type = DBUS_BUS_STARTER;
95 static dbus_bool_t initialized = FALSE;
98 * Lock for globals in this file
100 _DBUS_DEFINE_GLOBAL_LOCK (bus);
103 * Global lock covering all BusData on any connection. The bet is
104 * that some lock contention is better than more memory
105 * for a per-connection lock, but it's tough to imagine it mattering
108 _DBUS_DEFINE_GLOBAL_LOCK (bus_datas);
111 addresses_shutdown_func (void *data)
116 while (i < N_BUS_TYPES)
118 if (bus_connections[i] != NULL)
119 _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");
121 dbus_free (bus_connection_addresses[i]);
122 bus_connection_addresses[i] = NULL;
126 activation_bus_type = DBUS_BUS_STARTER;
132 get_from_env (char **connection_p,
137 _dbus_assert (*connection_p == NULL);
139 s = _dbus_getenv (env_var);
140 if (s == NULL || *s == '\0')
141 return TRUE; /* successfully didn't use the env var */
144 *connection_p = _dbus_strdup (s);
145 return *connection_p != NULL;
150 init_connections_unlocked (void)
158 while (i < N_BUS_TYPES)
160 bus_connections[i] = NULL;
164 /* Don't init these twice, we may run this code twice if
165 * init_connections_unlocked() fails midway through.
166 * In practice, each block below should contain only one
167 * "return FALSE" or running through twice may not
171 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
173 _dbus_verbose ("Filling in system bus address...\n");
175 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SYSTEM],
176 "DBUS_SYSTEM_BUS_ADDRESS"))
181 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
183 /* Use default system bus address if none set in environment */
184 bus_connection_addresses[DBUS_BUS_SYSTEM] =
185 _dbus_strdup (DBUS_SYSTEM_BUS_DEFAULT_ADDRESS);
187 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
190 _dbus_verbose (" used default system bus \"%s\"\n",
191 bus_connection_addresses[DBUS_BUS_SYSTEM]);
194 _dbus_verbose (" used env var system bus \"%s\"\n",
195 bus_connection_addresses[DBUS_BUS_SYSTEM]);
197 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
199 _dbus_verbose ("Filling in session bus address...\n");
201 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SESSION],
202 "DBUS_SESSION_BUS_ADDRESS"))
205 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
206 bus_connection_addresses[DBUS_BUS_SESSION] =
207 _dbus_strdup (DBUS_SESSION_BUS_DEFAULT_ADDRESS);
209 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
212 _dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_SESSION] ?
213 bus_connection_addresses[DBUS_BUS_SESSION] : "none set");
216 if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
218 _dbus_verbose ("Filling in activation bus address...\n");
220 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_STARTER],
221 "DBUS_STARTER_ADDRESS"))
224 _dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_STARTER] ?
225 bus_connection_addresses[DBUS_BUS_STARTER] : "none set");
229 if (bus_connection_addresses[DBUS_BUS_STARTER] != NULL)
231 s = _dbus_getenv ("DBUS_STARTER_BUS_TYPE");
235 _dbus_verbose ("Bus activation type was set to \"%s\"\n", s);
237 if (strcmp (s, "system") == 0)
238 activation_bus_type = DBUS_BUS_SYSTEM;
239 else if (strcmp (s, "session") == 0)
240 activation_bus_type = DBUS_BUS_SESSION;
245 /* Default to the session bus instead if available */
246 if (bus_connection_addresses[DBUS_BUS_SESSION] != NULL)
248 bus_connection_addresses[DBUS_BUS_STARTER] =
249 _dbus_strdup (bus_connection_addresses[DBUS_BUS_SESSION]);
250 if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
255 /* If we return FALSE we have to be sure that restarting
256 * the above code will work right
259 if (!_dbus_setenv ("DBUS_ACTIVATION_ADDRESS", NULL))
262 if (!_dbus_setenv ("DBUS_ACTIVATION_BUS_TYPE", NULL))
265 if (!_dbus_register_shutdown_func (addresses_shutdown_func,
276 bus_data_free (void *data)
280 if (bd->is_well_known)
284 /* We may be stored in more than one slot */
285 /* This should now be impossible - these slots are supposed to
286 * be cleared on disconnect, so should not need to be cleared on
290 while (i < N_BUS_TYPES)
292 if (bus_connections[i] == bd->connection)
293 bus_connections[i] = NULL;
300 dbus_free (bd->unique_name);
303 dbus_connection_free_data_slot (&bus_data_slot);
307 ensure_bus_data (DBusConnection *connection)
311 if (!dbus_connection_allocate_data_slot (&bus_data_slot))
314 bd = dbus_connection_get_data (connection, bus_data_slot);
317 bd = dbus_new0 (BusData, 1);
320 dbus_connection_free_data_slot (&bus_data_slot);
324 bd->connection = connection;
326 if (!dbus_connection_set_data (connection, bus_data_slot, bd,
330 dbus_connection_free_data_slot (&bus_data_slot);
334 /* Data slot refcount now held by the BusData */
338 dbus_connection_free_data_slot (&bus_data_slot);
345 * Internal function that checks to see if this
346 * is a shared connection owned by the bus and if it is unref it.
348 * @param connection a connection that has been disconnected.
351 _dbus_bus_notify_shared_connection_disconnected_unlocked (DBusConnection *connection)
357 /* We are expecting to have the connection saved in only one of these
358 * slots, but someone could in a pathological case set system and session
359 * bus to the same bus or something. Or set one of them to the starter
360 * bus without setting the starter bus type in the env variable.
361 * So we don't break the loop as soon as we find a match.
363 for (i = 0; i < N_BUS_TYPES; ++i)
365 if (bus_connections[i] == connection)
367 bus_connections[i] = NULL;
374 static DBusConnection *
375 internal_bus_get (DBusBusType type,
380 DBusConnection *connection;
382 DBusBusType address_type;
384 _dbus_return_val_if_fail (type >= 0 && type < N_BUS_TYPES, NULL);
385 _dbus_return_val_if_error_is_set (error, NULL);
389 if (!init_connections_unlocked ())
392 _DBUS_SET_OOM (error);
396 /* We want to use the activation address even if the
397 * activating bus is the session or system bus,
402 /* Use the real type of the activation bus for getting its
403 * connection, but only if the real type's address is available. (If
404 * the activating bus isn't a well-known bus then
405 * activation_bus_type == DBUS_BUS_STARTER)
407 if (type == DBUS_BUS_STARTER &&
408 bus_connection_addresses[activation_bus_type] != NULL)
409 type = activation_bus_type;
411 if (!private && bus_connections[type] != NULL)
413 connection = bus_connections[type];
414 dbus_connection_ref (connection);
420 address = bus_connection_addresses[address_type];
423 dbus_set_error (error, DBUS_ERROR_FAILED,
424 "Unable to determine the address of the message bus (try 'man dbus-launch' and 'man dbus-daemon' for help)");
430 connection = dbus_connection_open_private (address, error);
432 connection = dbus_connection_open (address, error);
436 _DBUS_ASSERT_ERROR_IS_SET (error);
441 if (!dbus_bus_register (connection, error))
443 _DBUS_ASSERT_ERROR_IS_SET (error);
444 _dbus_connection_close_possibly_shared (connection);
445 dbus_connection_unref (connection);
453 /* store a weak ref to the connection (dbus-connection.c is
454 * supposed to have a strong ref that it drops on disconnect,
455 * since this is a shared connection)
457 bus_connections[type] = connection;
460 /* By default we're bound to the lifecycle of
463 dbus_connection_set_exit_on_disconnect (connection,
466 _DBUS_LOCK (bus_datas);
467 bd = ensure_bus_data (connection);
468 _dbus_assert (bd != NULL); /* it should have been created on
469 register, so OOM not possible */
470 bd->is_well_known = TRUE;
471 _DBUS_UNLOCK (bus_datas);
476 /* Return a reference to the caller */
481 /** @} */ /* end of implementation details docs */
484 * @addtogroup DBusBus
489 * Connects to a bus daemon and registers the client with it. If a
490 * connection to the bus already exists, then that connection is
491 * returned. The caller of this function owns a reference to the bus.
493 * The caller may NOT call dbus_connection_close() on this connection;
494 * see dbus_connection_open() and dbus_connection_close() for details
497 * If this function obtains a new connection object never before
498 * returned from dbus_bus_get(), it will call
499 * dbus_connection_set_exit_on_disconnect(), so the application
500 * will exit if the connection closes. You can undo this
501 * by calling dbus_connection_set_exit_on_disconnect() yourself
502 * after you get the connection.
504 * dbus_bus_get() calls dbus_bus_register() for you.
506 * If returning a newly-created connection, this function will block
507 * until authentication and bus registration are complete.
509 * @param type bus type
510 * @param error address where an error can be returned.
511 * @returns a #DBusConnection with new ref
514 dbus_bus_get (DBusBusType type,
517 return internal_bus_get (type, FALSE, error);
521 * Connects to a bus daemon and registers the client with it as with
522 * dbus_bus_register(). Unlike dbus_bus_get(), always creates a new
523 * connection. This connection will not be saved or recycled by
524 * libdbus. Caller owns a reference to the bus and must either close
525 * it or know it to be closed prior to releasing this reference.
527 * See dbus_connection_open_private() for more details on when to
528 * close and unref this connection.
530 * This function calls
531 * dbus_connection_set_exit_on_disconnect() on the new connection, so the application
532 * will exit if the connection closes. You can undo this
533 * by calling dbus_connection_set_exit_on_disconnect() yourself
534 * after you get the connection.
536 * dbus_bus_get_private() calls dbus_bus_register() for you.
538 * This function will block until authentication and bus registration
541 * @param type bus type
542 * @param error address where an error can be returned.
543 * @returns a DBusConnection with new ref
546 dbus_bus_get_private (DBusBusType type,
549 return internal_bus_get (type, TRUE, error);
553 * Registers a connection with the bus. This must be the first
554 * thing an application does when connecting to the message bus.
555 * If registration succeeds, the unique name will be set,
556 * and can be obtained using dbus_bus_get_unique_name().
558 * This function will block until registration is complete.
560 * If the connection has already registered with the bus
561 * (determined by checking whether dbus_bus_get_unique_name()
562 * returns a non-#NULL value), then this function does nothing.
564 * If you use dbus_bus_get() or dbus_bus_get_private() this
565 * function will be called for you.
567 * @note Just use dbus_bus_get() or dbus_bus_get_private() instead of
568 * dbus_bus_register() and save yourself some pain. Using
569 * dbus_bus_register() manually is only useful if you have your
570 * own custom message bus not found in #DBusBusType.
572 * If you open a bus connection with dbus_connection_open() or
573 * dbus_connection_open_private() you will have to dbus_bus_register()
574 * yourself, or make the appropriate registration method calls
575 * yourself. If you send the method calls yourself, call
576 * dbus_bus_set_unique_name() with the unique bus name you get from
579 * For shared connections (created with dbus_connection_open()) in a
580 * multithreaded application, you can't really make the registration
581 * calls yourself, because you don't know whether some other thread is
582 * also registering, and the bus will kick you off if you send two
583 * registration messages.
585 * If you use dbus_bus_register() however, there is a lock that
586 * keeps both apps from registering at the same time.
588 * The rule in a multithreaded app, then, is that dbus_bus_register()
589 * must be used to register, or you need to have your own locks that
590 * all threads in the app will respect.
592 * In a single-threaded application you can register by hand instead
593 * of using dbus_bus_register(), as long as you check
594 * dbus_bus_get_unique_name() to see if a unique name has already been
595 * stored by another thread before you send the registration messages.
597 * @param connection the connection
598 * @param error place to store errors
599 * @returns #TRUE on success
602 dbus_bus_register (DBusConnection *connection,
605 DBusMessage *message, *reply;
610 _dbus_return_val_if_fail (connection != NULL, FALSE);
611 _dbus_return_val_if_error_is_set (error, FALSE);
615 _DBUS_LOCK (bus_datas);
617 bd = ensure_bus_data (connection);
620 _DBUS_SET_OOM (error);
621 _DBUS_UNLOCK (bus_datas);
625 if (bd->unique_name != NULL)
627 _dbus_verbose ("Ignoring attempt to register the same DBusConnection %s with the message bus a second time.\n",
629 _DBUS_UNLOCK (bus_datas);
635 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
642 _DBUS_SET_OOM (error);
644 _DBUS_UNLOCK (bus_datas);
648 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
650 dbus_message_unref (message);
654 else if (dbus_set_error_from_message (error, reply))
656 else if (!dbus_message_get_args (reply, error,
657 DBUS_TYPE_STRING, &name,
661 bd->unique_name = _dbus_strdup (name);
662 if (bd->unique_name == NULL)
664 _DBUS_SET_OOM (error);
672 dbus_message_unref (reply);
675 _DBUS_ASSERT_ERROR_IS_SET (error);
677 _DBUS_UNLOCK (bus_datas);
684 * Sets the unique name of the connection, as assigned by the message
685 * bus. Can only be used if you registered with the bus manually
686 * (i.e. if you did not call dbus_bus_register()). Can only be called
687 * once per connection. After the unique name is set, you can get it
688 * with dbus_bus_get_unique_name().
690 * The only reason to use this function is to re-implement the
691 * equivalent of dbus_bus_register() yourself. One (probably unusual)
692 * reason to do that might be to do the bus registration call
693 * asynchronously instead of synchronously.
695 * @note Just use dbus_bus_get() or dbus_bus_get_private(), or worst
696 * case dbus_bus_register(), instead of messing with this
697 * function. There's really no point creating pain for yourself by
698 * doing things manually.
700 * It's hard to use this function safely on shared connections
701 * (created by dbus_connection_open()) in a multithreaded application,
702 * because only one registration attempt can be sent to the bus. If
703 * two threads are both sending the registration message, there is no
704 * mechanism in libdbus itself to avoid sending it twice.
706 * Thus, you need a way to coordinate which thread sends the
707 * registration attempt; which also means you know which thread
708 * will call dbus_bus_set_unique_name(). If you don't know
709 * about all threads in the app (for example, if some libraries
710 * you're using might start libdbus-using threads), then you
711 * need to avoid using this function on shared connections.
713 * @param connection the connection
714 * @param unique_name the unique name
715 * @returns #FALSE if not enough memory
718 dbus_bus_set_unique_name (DBusConnection *connection,
719 const char *unique_name)
724 _dbus_return_val_if_fail (connection != NULL, FALSE);
725 _dbus_return_val_if_fail (unique_name != NULL, FALSE);
727 _DBUS_LOCK (bus_datas);
729 bd = ensure_bus_data (connection);
733 _dbus_assert (bd->unique_name == NULL);
735 bd->unique_name = _dbus_strdup (unique_name);
736 success = bd->unique_name != NULL;
738 _DBUS_UNLOCK (bus_datas);
744 * Gets the unique name of the connection as assigned by the message
745 * bus. Only possible after the connection has been registered with
746 * the message bus. All connections returned by dbus_bus_get() or
747 * dbus_bus_get_private() have been successfully registered.
749 * The name remains valid until the connection is freed, and
750 * should not be freed by the caller.
752 * Other than dbus_bus_get(), there are two ways to set the unique
753 * name; one is dbus_bus_register(), the other is
754 * dbus_bus_set_unique_name(). You are responsible for calling
755 * dbus_bus_set_unique_name() if you register by hand instead of using
756 * dbus_bus_register().
758 * @param connection the connection
759 * @returns the unique name or #NULL on error
762 dbus_bus_get_unique_name (DBusConnection *connection)
765 const char *unique_name;
767 _dbus_return_val_if_fail (connection != NULL, NULL);
769 _DBUS_LOCK (bus_datas);
771 bd = ensure_bus_data (connection);
775 unique_name = bd->unique_name;
777 _DBUS_UNLOCK (bus_datas);
783 * Asks the bus to return the UID the named connection authenticated
784 * as, if any. Only works on UNIX; only works for connections on the
785 * same machine as the bus. If you are not on the same machine as the
786 * bus, then calling this is probably a bad idea, since the UID will
787 * mean little to your application.
789 * For the system message bus you're guaranteed to be on the same
790 * machine since it only listens on a UNIX domain socket (at least,
791 * as shipped by default).
793 * This function only works for connections that authenticated as
794 * a UNIX user, right now that includes all bus connections, but
795 * it's very possible to have connections with no associated UID.
796 * So check for errors and do something sensible if they happen.
798 * This function will always return an error on Windows.
800 * @param connection the connection
801 * @param name a name owned by the connection
802 * @param error location to store the error
803 * @returns the unix user id, or ((unsigned)-1) if error is set
806 dbus_bus_get_unix_user (DBusConnection *connection,
810 DBusMessage *message, *reply;
813 _dbus_return_val_if_fail (connection != NULL, DBUS_UID_UNSET);
814 _dbus_return_val_if_fail (name != NULL, DBUS_UID_UNSET);
815 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), DBUS_UID_UNSET);
816 _dbus_return_val_if_error_is_set (error, DBUS_UID_UNSET);
818 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
821 "GetConnectionUnixUser");
825 _DBUS_SET_OOM (error);
826 return DBUS_UID_UNSET;
829 if (!dbus_message_append_args (message,
830 DBUS_TYPE_STRING, &name,
833 dbus_message_unref (message);
834 _DBUS_SET_OOM (error);
835 return DBUS_UID_UNSET;
838 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
841 dbus_message_unref (message);
845 _DBUS_ASSERT_ERROR_IS_SET (error);
846 return DBUS_UID_UNSET;
849 if (dbus_set_error_from_message (error, reply))
851 _DBUS_ASSERT_ERROR_IS_SET (error);
852 dbus_message_unref (reply);
853 return DBUS_UID_UNSET;
856 if (!dbus_message_get_args (reply, error,
857 DBUS_TYPE_UINT32, &uid,
860 _DBUS_ASSERT_ERROR_IS_SET (error);
861 dbus_message_unref (reply);
862 return DBUS_UID_UNSET;
865 dbus_message_unref (reply);
867 return (unsigned long) uid;
871 * Asks the bus to return its globally unique ID, as described in the
872 * D-Bus specification. For the session bus, this is useful as a way
873 * to uniquely identify each user session. For the system bus,
874 * probably the bus ID is not useful; instead, use the machine ID
875 * since it's accessible without necessarily connecting to the bus and
876 * may be persistent beyond a single bus instance (across reboots for
877 * example). See dbus_get_local_machine_id().
879 * In addition to an ID for each bus and an ID for each machine, there is
880 * an ID for each address that the bus is listening on; that can
881 * be retrieved with dbus_connection_get_server_id(), though it is
882 * probably not very useful.
884 * @param connection the connection
885 * @param error location to store the error
886 * @returns the bus ID or #NULL if error is set
889 dbus_bus_get_id (DBusConnection *connection,
892 DBusMessage *message, *reply;
894 const char *v_STRING;
896 _dbus_return_val_if_fail (connection != NULL, NULL);
897 _dbus_return_val_if_error_is_set (error, NULL);
899 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
906 _DBUS_SET_OOM (error);
910 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
913 dbus_message_unref (message);
917 _DBUS_ASSERT_ERROR_IS_SET (error);
921 if (dbus_set_error_from_message (error, reply))
923 _DBUS_ASSERT_ERROR_IS_SET (error);
924 dbus_message_unref (reply);
929 if (!dbus_message_get_args (reply, error,
930 DBUS_TYPE_STRING, &v_STRING,
933 _DBUS_ASSERT_ERROR_IS_SET (error);
934 dbus_message_unref (reply);
938 id = _dbus_strdup (v_STRING); /* may be NULL */
940 dbus_message_unref (reply);
943 _DBUS_SET_OOM (error);
945 /* FIXME it might be nice to cache the ID locally */
951 * Asks the bus to assign the given name to this connection by invoking
952 * the RequestName method on the bus. This method is fully documented
953 * in the D-Bus specification. For quick reference, the flags and
954 * result codes are discussed here, but the specification is the
955 * canonical version of this information.
957 * First you should know that for each bus name, the bus stores
958 * a queue of connections that would like to own it. Only
959 * one owns it at a time - called the primary owner. If the primary
960 * owner releases the name or disconnects, then the next owner in the
961 * queue atomically takes over.
963 * So for example if you have an application org.freedesktop.TextEditor
964 * and multiple instances of it can be run, you can have all of them
965 * sitting in the queue. The first one to start up will receive messages
966 * sent to org.freedesktop.TextEditor, but if that one exits another
967 * will become the primary owner and receive messages.
969 * The queue means you don't need to manually watch for the current owner to
970 * disappear and then request the name again.
972 * When requesting a name, you can specify several flags.
974 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT and #DBUS_NAME_FLAG_DO_NOT_QUEUE
975 * are properties stored by the bus for this connection with respect to
976 * each requested bus name. These properties are stored even if the
977 * connection is queued and does not become the primary owner.
978 * You can update these flags by calling RequestName again (even if
979 * you already own the name).
981 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT means that another requestor of the
982 * name can take it away from you by specifying #DBUS_NAME_FLAG_REPLACE_EXISTING.
984 * #DBUS_NAME_FLAG_DO_NOT_QUEUE means that if you aren't the primary owner,
985 * you don't want to be queued up - you only care about being the
988 * Unlike the other two flags, #DBUS_NAME_FLAG_REPLACE_EXISTING is a property
989 * of the individual RequestName call, i.e. the bus does not persistently
990 * associate it with the connection-name pair. If a RequestName call includes
991 * the #DBUS_NAME_FLAG_REPLACE_EXISTING flag, and the current primary
992 * owner has #DBUS_NAME_FLAG_ALLOW_REPLACEMENT set, then the current primary
993 * owner will be kicked off.
995 * If no flags are given, an application will receive the requested
996 * name only if the name is currently unowned; and it will NOT give
997 * up the name if another application asks to take it over using
998 * #DBUS_NAME_FLAG_REPLACE_EXISTING.
1000 * This function returns a result code. The possible result codes
1003 * #DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER means that the name had no
1004 * existing owner, and the caller is now the primary owner; or that
1005 * the name had an owner, and the caller specified
1006 * #DBUS_NAME_FLAG_REPLACE_EXISTING, and the current owner
1007 * specified #DBUS_NAME_FLAG_ALLOW_REPLACEMENT.
1009 * #DBUS_REQUEST_NAME_REPLY_IN_QUEUE happens only if the caller does NOT
1010 * specify #DBUS_NAME_FLAG_DO_NOT_QUEUE and either the current owner
1011 * did NOT specify #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or the caller did NOT
1012 * specify #DBUS_NAME_FLAG_REPLACE_EXISTING. In this case the caller ends up
1013 * in a queue to own the name after the current owner gives it up.
1015 * #DBUS_REQUEST_NAME_REPLY_EXISTS happens if the name has an owner
1016 * already and the caller specifies #DBUS_NAME_FLAG_DO_NOT_QUEUE
1017 * and either the current owner has NOT specified
1018 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or the caller did NOT specify
1019 * #DBUS_NAME_FLAG_REPLACE_EXISTING.
1021 * #DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER happens if an application
1022 * requests a name it already owns. (Re-requesting a name is useful if
1023 * you want to change the #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or
1024 * #DBUS_NAME_FLAG_DO_NOT_QUEUE settings.)
1026 * When a service represents an application, say "text editor," then
1027 * it should specify #DBUS_NAME_FLAG_ALLOW_REPLACEMENT if it wants
1028 * the last editor started to be the user's editor vs. the first one
1029 * started. Then any editor that can be the user's editor should
1030 * specify #DBUS_NAME_FLAG_REPLACE_EXISTING to either take over
1031 * (last-started-wins) or be queued up (first-started-wins) according
1032 * to whether #DBUS_NAME_FLAG_ALLOW_REPLACEMENT was given.
1034 * Conventionally, single-instance applications often offer a command
1035 * line option called --replace which means to replace the current
1036 * instance. To implement this, always set
1037 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT when you request your
1038 * application's bus name. When you lose ownership of your bus name,
1039 * you need to exit. Look for the signal "NameLost" from
1040 * #DBUS_SERVICE_DBUS and #DBUS_INTERFACE_DBUS (the signal's first
1041 * argument is the bus name that was lost). If starting up without
1042 * --replace, do not specify #DBUS_NAME_FLAG_REPLACE_EXISTING, and
1043 * exit if you fail to become the bus name owner. If --replace is
1044 * given, ask to replace the old owner.
1046 * @param connection the connection
1047 * @param name the name to request
1048 * @param flags flags
1049 * @param error location to store the error
1050 * @returns a result code, -1 if error is set
1053 dbus_bus_request_name (DBusConnection *connection,
1058 DBusMessage *message, *reply;
1059 dbus_uint32_t result;
1061 _dbus_return_val_if_fail (connection != NULL, 0);
1062 _dbus_return_val_if_fail (name != NULL, 0);
1063 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
1064 _dbus_return_val_if_error_is_set (error, 0);
1066 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1068 DBUS_INTERFACE_DBUS,
1071 if (message == NULL)
1073 _DBUS_SET_OOM (error);
1077 if (!dbus_message_append_args (message,
1078 DBUS_TYPE_STRING, &name,
1079 DBUS_TYPE_UINT32, &flags,
1082 dbus_message_unref (message);
1083 _DBUS_SET_OOM (error);
1087 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
1090 dbus_message_unref (message);
1094 _DBUS_ASSERT_ERROR_IS_SET (error);
1098 if (dbus_set_error_from_message (error, reply))
1100 _DBUS_ASSERT_ERROR_IS_SET (error);
1101 dbus_message_unref (reply);
1105 if (!dbus_message_get_args (reply, error,
1106 DBUS_TYPE_UINT32, &result,
1109 _DBUS_ASSERT_ERROR_IS_SET (error);
1110 dbus_message_unref (reply);
1114 dbus_message_unref (reply);
1121 * Asks the bus to unassign the given name from this connection by
1122 * invoking the ReleaseName method on the bus. The "ReleaseName"
1123 * method is canonically documented in the D-Bus specification.
1125 * Possible results are: #DBUS_RELEASE_NAME_REPLY_RELEASED
1126 * which means you owned the name or were in the queue to own it,
1127 * and and now you don't own it and aren't in the queue.
1128 * #DBUS_RELEASE_NAME_REPLY_NOT_OWNER which means someone else
1129 * owns the name so you can't release it.
1130 * #DBUS_RELEASE_NAME_REPLY_NON_EXISTENT
1131 * which means nobody owned the name.
1133 * @param connection the connection
1134 * @param name the name to remove
1135 * @param error location to store the error
1136 * @returns a result code, -1 if error is set
1139 dbus_bus_release_name (DBusConnection *connection,
1143 DBusMessage *message, *reply;
1144 dbus_uint32_t result;
1146 _dbus_return_val_if_fail (connection != NULL, 0);
1147 _dbus_return_val_if_fail (name != NULL, 0);
1148 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
1149 _dbus_return_val_if_error_is_set (error, 0);
1151 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1153 DBUS_INTERFACE_DBUS,
1156 if (message == NULL)
1158 _DBUS_SET_OOM (error);
1162 if (!dbus_message_append_args (message,
1163 DBUS_TYPE_STRING, &name,
1166 dbus_message_unref (message);
1167 _DBUS_SET_OOM (error);
1171 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
1174 dbus_message_unref (message);
1178 _DBUS_ASSERT_ERROR_IS_SET (error);
1182 if (dbus_set_error_from_message (error, reply))
1184 _DBUS_ASSERT_ERROR_IS_SET (error);
1185 dbus_message_unref (reply);
1189 if (!dbus_message_get_args (reply, error,
1190 DBUS_TYPE_UINT32, &result,
1193 _DBUS_ASSERT_ERROR_IS_SET (error);
1194 dbus_message_unref (reply);
1198 dbus_message_unref (reply);
1204 * Asks the bus whether a certain name has an owner.
1206 * Using this can easily result in a race condition,
1207 * since an owner can appear or disappear after you
1210 * If you want to request a name, just request it;
1211 * if you want to avoid replacing a current owner,
1212 * don't specify #DBUS_NAME_FLAG_REPLACE_EXISTING and
1213 * you will get an error if there's already an owner.
1215 * @param connection the connection
1216 * @param name the name
1217 * @param error location to store any errors
1218 * @returns #TRUE if the name exists, #FALSE if not or on error
1221 dbus_bus_name_has_owner (DBusConnection *connection,
1225 DBusMessage *message, *reply;
1228 _dbus_return_val_if_fail (connection != NULL, FALSE);
1229 _dbus_return_val_if_fail (name != NULL, FALSE);
1230 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
1231 _dbus_return_val_if_error_is_set (error, FALSE);
1233 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1235 DBUS_INTERFACE_DBUS,
1237 if (message == NULL)
1239 _DBUS_SET_OOM (error);
1243 if (!dbus_message_append_args (message,
1244 DBUS_TYPE_STRING, &name,
1247 dbus_message_unref (message);
1248 _DBUS_SET_OOM (error);
1252 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
1253 dbus_message_unref (message);
1257 _DBUS_ASSERT_ERROR_IS_SET (error);
1261 if (!dbus_message_get_args (reply, error,
1262 DBUS_TYPE_BOOLEAN, &exists,
1265 _DBUS_ASSERT_ERROR_IS_SET (error);
1266 dbus_message_unref (reply);
1270 dbus_message_unref (reply);
1275 * Starts a service that will request ownership of the given name.
1276 * The returned result will be one of be one of
1277 * #DBUS_START_REPLY_SUCCESS or #DBUS_START_REPLY_ALREADY_RUNNING if
1278 * successful. Pass #NULL if you don't care about the result.
1280 * The flags parameter is for future expansion, currently you should
1283 * It's often easier to avoid explicitly starting services, and
1284 * just send a method call to the service's bus name instead.
1285 * Method calls start a service to handle them by default
1286 * unless you call dbus_message_set_auto_start() to disable this
1289 * @param connection the connection
1290 * @param name the name we want the new service to request
1291 * @param flags the flags (should always be 0 for now)
1292 * @param result a place to store the result or #NULL
1293 * @param error location to store any errors
1294 * @returns #TRUE if the activation succeeded, #FALSE if not
1297 dbus_bus_start_service_by_name (DBusConnection *connection,
1299 dbus_uint32_t flags,
1300 dbus_uint32_t *result,
1306 _dbus_return_val_if_fail (connection != NULL, FALSE);
1307 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
1309 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1311 DBUS_INTERFACE_DBUS,
1312 "StartServiceByName");
1314 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &name,
1315 DBUS_TYPE_UINT32, &flags, DBUS_TYPE_INVALID))
1317 dbus_message_unref (msg);
1318 _DBUS_SET_OOM (error);
1322 reply = dbus_connection_send_with_reply_and_block (connection, msg,
1324 dbus_message_unref (msg);
1328 _DBUS_ASSERT_ERROR_IS_SET (error);
1332 if (dbus_set_error_from_message (error, reply))
1334 _DBUS_ASSERT_ERROR_IS_SET (error);
1335 dbus_message_unref (reply);
1339 if (result != NULL &&
1340 !dbus_message_get_args (reply, error, DBUS_TYPE_UINT32,
1341 result, DBUS_TYPE_INVALID))
1343 _DBUS_ASSERT_ERROR_IS_SET (error);
1344 dbus_message_unref (reply);
1348 dbus_message_unref (reply);
1353 send_no_return_values (DBusConnection *connection,
1359 /* Block to check success codepath */
1362 reply = dbus_connection_send_with_reply_and_block (connection, msg,
1366 _DBUS_ASSERT_ERROR_IS_SET (error);
1368 dbus_message_unref (reply);
1372 /* Silently-fail nonblocking codepath */
1373 dbus_message_set_no_reply (msg, TRUE);
1374 dbus_connection_send (connection, msg, NULL);
1379 * Adds a match rule to match messages going through the message bus.
1380 * The "rule" argument is the string form of a match rule.
1382 * If you pass #NULL for the error, this function will not
1383 * block; the match thus won't be added until you flush the
1384 * connection, and if there's an error adding the match
1385 * (only possible error is lack of resources in the bus),
1386 * you won't find out about it.
1388 * If you pass non-#NULL for the error this function will
1389 * block until it gets a reply.
1391 * Normal API conventions would have the function return
1392 * a boolean value indicating whether the error was set,
1393 * but that would require blocking always to determine
1396 * The AddMatch method is fully documented in the D-Bus
1397 * specification. For quick reference, the format of the
1398 * match rules is discussed here, but the specification
1399 * is the canonical version of this information.
1401 * Rules are specified as a string of comma separated
1402 * key/value pairs. An example is
1403 * "type='signal',sender='org.freedesktop.DBus',
1404 * interface='org.freedesktop.DBus',member='Foo',
1405 * path='/bar/foo',destination=':452345.34'"
1407 * Possible keys you can match on are type, sender,
1408 * interface, member, path, destination and numbered
1409 * keys to match message args (keys are 'arg0', 'arg1', etc.).
1410 * Omitting a key from the rule indicates
1411 * a wildcard match. For instance omitting
1412 * the member from a match rule but adding a sender would
1413 * let all messages from that sender through regardless of
1416 * Matches are inclusive not exclusive so as long as one
1417 * rule matches the message will get through. It is important
1418 * to note this because every time a message is received the
1419 * application will be paged into memory to process it. This
1420 * can cause performance problems such as draining batteries
1421 * on embedded platforms.
1423 * If you match message args ('arg0', 'arg1', and so forth)
1424 * only string arguments will match. That is, arg0='5' means
1425 * match the string "5" not the integer 5.
1427 * Currently there is no way to match against non-string arguments.
1429 * A specialised form of wildcard matching on arguments is
1430 * supported for path-like namespaces. If your argument match has
1431 * a 'path' suffix (eg: "arg0path='/some/path/'") then it is
1432 * considered a match if the argument exactly matches the given
1433 * string or if one of them ends in a '/' and is a prefix of the
1436 * Matching on interface is tricky because method call
1437 * messages only optionally specify the interface.
1438 * If a message omits the interface, then it will NOT match
1439 * if the rule specifies an interface name. This means match
1440 * rules on method calls should not usually give an interface.
1442 * However, signal messages are required to include the interface
1443 * so when matching signals usually you should specify the interface
1444 * in the match rule.
1446 * For security reasons, you can match arguments only up to
1447 * #DBUS_MAXIMUM_MATCH_RULE_ARG_NUMBER.
1449 * Match rules have a maximum length of #DBUS_MAXIMUM_MATCH_RULE_LENGTH
1452 * Both of these maximums are much higher than you're likely to need,
1453 * they only exist because the D-Bus bus daemon has fixed limits on
1454 * all resource usage.
1456 * @param connection connection to the message bus
1457 * @param rule textual form of match rule
1458 * @param error location to store any errors
1461 dbus_bus_add_match (DBusConnection *connection,
1467 _dbus_return_if_fail (rule != NULL);
1469 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1471 DBUS_INTERFACE_DBUS,
1476 _DBUS_SET_OOM (error);
1480 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
1483 dbus_message_unref (msg);
1484 _DBUS_SET_OOM (error);
1488 send_no_return_values (connection, msg, error);
1490 dbus_message_unref (msg);
1494 * Removes a previously-added match rule "by value" (the most
1495 * recently-added identical rule gets removed). The "rule" argument
1496 * is the string form of a match rule.
1498 * The bus compares match rules semantically, not textually, so
1499 * whitespace and ordering don't have to be identical to
1500 * the rule you passed to dbus_bus_add_match().
1502 * If you pass #NULL for the error, this function will not
1503 * block; otherwise it will. See detailed explanation in
1504 * docs for dbus_bus_add_match().
1506 * @param connection connection to the message bus
1507 * @param rule textual form of match rule
1508 * @param error location to store any errors
1511 dbus_bus_remove_match (DBusConnection *connection,
1517 _dbus_return_if_fail (rule != NULL);
1519 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1521 DBUS_INTERFACE_DBUS,
1524 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
1527 dbus_message_unref (msg);
1528 _DBUS_SET_OOM (error);
1532 send_no_return_values (connection, msg, error);
1534 dbus_message_unref (msg);