1 /* -*- mode: C; c-file-style: "gnu" -*- */
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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;
130 get_from_env (char **connection_p,
135 _dbus_assert (*connection_p == NULL);
137 s = _dbus_getenv (env_var);
138 if (s == NULL || *s == '\0')
139 return TRUE; /* successfully didn't use the env var */
142 *connection_p = _dbus_strdup (s);
143 return *connection_p != NULL;
148 init_connections_unlocked (void)
156 while (i < N_BUS_TYPES)
158 bus_connections[i] = NULL;
162 /* Don't init these twice, we may run this code twice if
163 * init_connections_unlocked() fails midway through.
164 * In practice, each block below should contain only one
165 * "return FALSE" or running through twice may not
169 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
171 _dbus_verbose ("Filling in system bus address...\n");
173 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SYSTEM],
174 "DBUS_SYSTEM_BUS_ADDRESS"))
179 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
181 /* Use default system bus address if none set in environment */
182 bus_connection_addresses[DBUS_BUS_SYSTEM] =
183 _dbus_strdup (DBUS_SYSTEM_BUS_DEFAULT_ADDRESS);
185 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
188 _dbus_verbose (" used default system bus \"%s\"\n",
189 bus_connection_addresses[DBUS_BUS_SYSTEM]);
192 _dbus_verbose (" used env var system bus \"%s\"\n",
193 bus_connection_addresses[DBUS_BUS_SYSTEM]);
195 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
197 _dbus_verbose ("Filling in session bus address...\n");
199 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SESSION],
200 "DBUS_SESSION_BUS_ADDRESS"))
203 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
204 bus_connection_addresses[DBUS_BUS_SESSION] =
205 _dbus_strdup (DBUS_SESSION_BUS_DEFAULT_ADDRESS);
207 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
210 _dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_SESSION] ?
211 bus_connection_addresses[DBUS_BUS_SESSION] : "none set");
214 if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
216 _dbus_verbose ("Filling in activation bus address...\n");
218 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_STARTER],
219 "DBUS_STARTER_ADDRESS"))
222 _dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_STARTER] ?
223 bus_connection_addresses[DBUS_BUS_STARTER] : "none set");
227 if (bus_connection_addresses[DBUS_BUS_STARTER] != NULL)
229 s = _dbus_getenv ("DBUS_STARTER_BUS_TYPE");
233 _dbus_verbose ("Bus activation type was set to \"%s\"\n", s);
235 if (strcmp (s, "system") == 0)
236 activation_bus_type = DBUS_BUS_SYSTEM;
237 else if (strcmp (s, "session") == 0)
238 activation_bus_type = DBUS_BUS_SESSION;
243 /* Default to the session bus instead if available */
244 if (bus_connection_addresses[DBUS_BUS_SESSION] != NULL)
246 bus_connection_addresses[DBUS_BUS_STARTER] =
247 _dbus_strdup (bus_connection_addresses[DBUS_BUS_SESSION]);
248 if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
253 /* If we return FALSE we have to be sure that restarting
254 * the above code will work right
257 if (!_dbus_setenv ("DBUS_ACTIVATION_ADDRESS", NULL))
260 if (!_dbus_setenv ("DBUS_ACTIVATION_BUS_TYPE", NULL))
263 if (!_dbus_register_shutdown_func (addresses_shutdown_func,
274 bus_data_free (void *data)
278 if (bd->is_well_known)
282 /* We may be stored in more than one slot */
283 /* This should now be impossible - these slots are supposed to
284 * be cleared on disconnect, so should not need to be cleared on
288 while (i < N_BUS_TYPES)
290 if (bus_connections[i] == bd->connection)
291 bus_connections[i] = NULL;
298 dbus_free (bd->unique_name);
301 dbus_connection_free_data_slot (&bus_data_slot);
305 ensure_bus_data (DBusConnection *connection)
309 if (!dbus_connection_allocate_data_slot (&bus_data_slot))
312 bd = dbus_connection_get_data (connection, bus_data_slot);
315 bd = dbus_new0 (BusData, 1);
318 dbus_connection_free_data_slot (&bus_data_slot);
322 bd->connection = connection;
324 if (!dbus_connection_set_data (connection, bus_data_slot, bd,
328 dbus_connection_free_data_slot (&bus_data_slot);
332 /* Data slot refcount now held by the BusData */
336 dbus_connection_free_data_slot (&bus_data_slot);
343 * Internal function that checks to see if this
344 * is a shared connection owned by the bus and if it is unref it.
346 * @param connection a connection that has been disconnected.
349 _dbus_bus_notify_shared_connection_disconnected_unlocked (DBusConnection *connection)
355 /* We are expecting to have the connection saved in only one of these
356 * slots, but someone could in a pathological case set system and session
357 * bus to the same bus or something. Or set one of them to the starter
358 * bus without setting the starter bus type in the env variable.
359 * So we don't break the loop as soon as we find a match.
361 for (i = 0; i < N_BUS_TYPES; ++i)
363 if (bus_connections[i] == connection)
365 bus_connections[i] = NULL;
372 static DBusConnection *
373 internal_bus_get (DBusBusType type,
378 DBusConnection *connection;
380 DBusBusType address_type;
382 _dbus_return_val_if_fail (type >= 0 && type < N_BUS_TYPES, NULL);
383 _dbus_return_val_if_error_is_set (error, NULL);
387 if (!init_connections_unlocked ())
390 _DBUS_SET_OOM (error);
394 /* We want to use the activation address even if the
395 * activating bus is the session or system bus,
400 /* Use the real type of the activation bus for getting its
401 * connection, but only if the real type's address is available. (If
402 * the activating bus isn't a well-known bus then
403 * activation_bus_type == DBUS_BUS_STARTER)
405 if (type == DBUS_BUS_STARTER &&
406 bus_connection_addresses[activation_bus_type] != NULL)
407 type = activation_bus_type;
409 if (!private && bus_connections[type] != NULL)
411 connection = bus_connections[type];
412 dbus_connection_ref (connection);
418 address = bus_connection_addresses[address_type];
421 dbus_set_error (error, DBUS_ERROR_FAILED,
422 "Unable to determine the address of the message bus (try 'man dbus-launch' and 'man dbus-daemon' for help)");
428 connection = dbus_connection_open_private (address, error);
430 connection = dbus_connection_open (address, error);
434 _DBUS_ASSERT_ERROR_IS_SET (error);
439 /* By default we're bound to the lifecycle of
442 dbus_connection_set_exit_on_disconnect (connection,
445 if (!dbus_bus_register (connection, error))
447 _DBUS_ASSERT_ERROR_IS_SET (error);
448 _dbus_connection_close_possibly_shared (connection);
449 dbus_connection_unref (connection);
457 /* store a weak ref to the connection (dbus-connection.c is
458 * supposed to have a strong ref that it drops on disconnect,
459 * since this is a shared connection)
461 bus_connections[type] = connection;
464 _DBUS_LOCK (bus_datas);
465 bd = ensure_bus_data (connection);
466 _dbus_assert (bd != NULL); /* it should have been created on
467 register, so OOM not possible */
468 bd->is_well_known = TRUE;
469 _DBUS_UNLOCK (bus_datas);
474 /* Return a reference to the caller */
479 /** @} */ /* end of implementation details docs */
482 * @addtogroup DBusBus
487 * Connects to a bus daemon and registers the client with it. If a
488 * connection to the bus already exists, then that connection is
489 * returned. The caller of this function owns a reference to the bus.
491 * The caller may NOT call dbus_connection_close() on this connection;
492 * see dbus_connection_open() and dbus_connection_close() for details
495 * If this function obtains a new connection object never before
496 * returned from dbus_bus_get(), it will call
497 * dbus_connection_set_exit_on_disconnect(), so the application
498 * will exit if the connection closes. You can undo this
499 * by calling dbus_connection_set_exit_on_disconnect() yourself
500 * after you get the connection.
502 * dbus_bus_get() calls dbus_bus_register() for you.
504 * If returning a newly-created connection, this function will block
505 * until authentication and bus registration are complete.
507 * @param type bus type
508 * @param error address where an error can be returned.
509 * @returns a #DBusConnection with new ref
512 dbus_bus_get (DBusBusType type,
515 return internal_bus_get (type, FALSE, error);
519 * Connects to a bus daemon and registers the client with it as with
520 * dbus_bus_register(). Unlike dbus_bus_get(), always creates a new
521 * connection. This connection will not be saved or recycled by
522 * libdbus. Caller owns a reference to the bus and must either close
523 * it or know it to be closed prior to releasing this reference.
525 * See dbus_connection_open_private() for more details on when to
526 * close and unref this connection.
528 * This function calls
529 * dbus_connection_set_exit_on_disconnect() on the new connection, so the application
530 * will exit if the connection closes. You can undo this
531 * by calling dbus_connection_set_exit_on_disconnect() yourself
532 * after you get the connection.
534 * dbus_bus_get_private() calls dbus_bus_register() for you.
536 * This function will block until authentication and bus registration
539 * @param type bus type
540 * @param error address where an error can be returned.
541 * @returns a DBusConnection with new ref
544 dbus_bus_get_private (DBusBusType type,
547 return internal_bus_get (type, TRUE, error);
551 * Registers a connection with the bus. This must be the first
552 * thing an application does when connecting to the message bus.
553 * If registration succeeds, the unique name will be set,
554 * and can be obtained using dbus_bus_get_unique_name().
556 * This function will block until registration is complete.
558 * If the connection has already registered with the bus
559 * (determined by checking whether dbus_bus_get_unique_name()
560 * returns a non-#NULL value), then this function does nothing.
562 * If you use dbus_bus_get() or dbus_bus_get_private() this
563 * function will be called for you.
565 * @note Just use dbus_bus_get() or dbus_bus_get_private() instead of
566 * dbus_bus_register() and save yourself some pain. Using
567 * dbus_bus_register() manually is only useful if you have your
568 * own custom message bus not found in #DBusBusType.
570 * If you open a bus connection with dbus_connection_open() or
571 * dbus_connection_open_private() you will have to dbus_bus_register()
572 * yourself, or make the appropriate registration method calls
573 * yourself. If you send the method calls yourself, call
574 * dbus_bus_set_unique_name() with the unique bus name you get from
577 * For shared connections (created with dbus_connection_open()) in a
578 * multithreaded application, you can't really make the registration
579 * calls yourself, because you don't know whether some other thread is
580 * also registering, and the bus will kick you off if you send two
581 * registration messages.
583 * If you use dbus_bus_register() however, there is a lock that
584 * keeps both apps from registering at the same time.
586 * The rule in a multithreaded app, then, is that dbus_bus_register()
587 * must be used to register, or you need to have your own locks that
588 * all threads in the app will respect.
590 * In a single-threaded application you can register by hand instead
591 * of using dbus_bus_register(), as long as you check
592 * dbus_bus_get_unique_name() to see if a unique name has already been
593 * stored by another thread before you send the registration messages.
595 * @param connection the connection
596 * @param error place to store errors
597 * @returns #TRUE on success
600 dbus_bus_register (DBusConnection *connection,
603 DBusMessage *message, *reply;
608 _dbus_return_val_if_fail (connection != NULL, FALSE);
609 _dbus_return_val_if_error_is_set (error, FALSE);
613 _DBUS_LOCK (bus_datas);
615 bd = ensure_bus_data (connection);
618 _DBUS_SET_OOM (error);
619 _DBUS_UNLOCK (bus_datas);
623 if (bd->unique_name != NULL)
625 _dbus_verbose ("Ignoring attempt to register the same DBusConnection %s with the message bus a second time.\n",
627 _DBUS_UNLOCK (bus_datas);
633 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
640 _DBUS_SET_OOM (error);
642 _DBUS_UNLOCK (bus_datas);
646 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
648 dbus_message_unref (message);
652 else if (dbus_set_error_from_message (error, reply))
654 else if (!dbus_message_get_args (reply, error,
655 DBUS_TYPE_STRING, &name,
659 bd->unique_name = _dbus_strdup (name);
660 if (bd->unique_name == NULL)
662 _DBUS_SET_OOM (error);
670 dbus_message_unref (reply);
673 _DBUS_ASSERT_ERROR_IS_SET (error);
675 _DBUS_UNLOCK (bus_datas);
682 * Sets the unique name of the connection, as assigned by the message
683 * bus. Can only be used if you registered with the bus manually
684 * (i.e. if you did not call dbus_bus_register()). Can only be called
685 * once per connection. After the unique name is set, you can get it
686 * with dbus_bus_get_unique_name().
688 * The only reason to use this function is to re-implement the
689 * equivalent of dbus_bus_register() yourself. One (probably unusual)
690 * reason to do that might be to do the bus registration call
691 * asynchronously instead of synchronously.
693 * @note Just use dbus_bus_get() or dbus_bus_get_private(), or worst
694 * case dbus_bus_register(), instead of messing with this
695 * function. There's really no point creating pain for yourself by
696 * doing things manually.
698 * It's hard to use this function safely on shared connections
699 * (created by dbus_connection_open()) in a multithreaded application,
700 * because only one registration attempt can be sent to the bus. If
701 * two threads are both sending the registration message, there is no
702 * mechanism in libdbus itself to avoid sending it twice.
704 * Thus, you need a way to coordinate which thread sends the
705 * registration attempt; which also means you know which thread
706 * will call dbus_bus_set_unique_name(). If you don't know
707 * about all threads in the app (for example, if some libraries
708 * you're using might start libdbus-using threads), then you
709 * need to avoid using this function on shared connections.
711 * @param connection the connection
712 * @param unique_name the unique name
713 * @returns #FALSE if not enough memory
716 dbus_bus_set_unique_name (DBusConnection *connection,
717 const char *unique_name)
722 _dbus_return_val_if_fail (connection != NULL, FALSE);
723 _dbus_return_val_if_fail (unique_name != NULL, FALSE);
725 _DBUS_LOCK (bus_datas);
727 bd = ensure_bus_data (connection);
731 _dbus_assert (bd->unique_name == NULL);
733 bd->unique_name = _dbus_strdup (unique_name);
734 success = bd->unique_name != NULL;
736 _DBUS_UNLOCK (bus_datas);
742 * Gets the unique name of the connection as assigned by the message
743 * bus. Only possible after the connection has been registered with
744 * the message bus. All connections returned by dbus_bus_get() or
745 * dbus_bus_get_private() have been successfully registered.
747 * The name remains valid until the connection is freed, and
748 * should not be freed by the caller.
750 * Other than dbus_bus_get(), there are two ways to set the unique
751 * name; one is dbus_bus_register(), the other is
752 * dbus_bus_set_unique_name(). You are responsible for calling
753 * dbus_bus_set_unique_name() if you register by hand instead of using
754 * dbus_bus_register().
756 * @param connection the connection
757 * @returns the unique name or #NULL on error
760 dbus_bus_get_unique_name (DBusConnection *connection)
763 const char *unique_name;
765 _dbus_return_val_if_fail (connection != NULL, NULL);
767 _DBUS_LOCK (bus_datas);
769 bd = ensure_bus_data (connection);
773 unique_name = bd->unique_name;
775 _DBUS_UNLOCK (bus_datas);
781 * Asks the bus to return the UID the named connection authenticated
782 * as, if any. Only works on UNIX; only works for connections on the
783 * same machine as the bus. If you are not on the same machine as the
784 * bus, then calling this is probably a bad idea, since the UID will
785 * mean little to your application.
787 * For the system message bus you're guaranteed to be on the same
788 * machine since it only listens on a UNIX domain socket (at least,
789 * as shipped by default).
791 * This function only works for connections that authenticated as
792 * a UNIX user, right now that includes all bus connections, but
793 * it's very possible to have connections with no associated UID.
794 * So check for errors and do something sensible if they happen.
796 * This function will always return an error on Windows.
798 * @param connection the connection
799 * @param name a name owned by the connection
800 * @param error location to store the error
801 * @returns the unix user id, or ((unsigned)-1) if error is set
804 dbus_bus_get_unix_user (DBusConnection *connection,
808 DBusMessage *message, *reply;
811 _dbus_return_val_if_fail (connection != NULL, DBUS_UID_UNSET);
812 _dbus_return_val_if_fail (name != NULL, DBUS_UID_UNSET);
813 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), DBUS_UID_UNSET);
814 _dbus_return_val_if_error_is_set (error, DBUS_UID_UNSET);
816 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
819 "GetConnectionUnixUser");
823 _DBUS_SET_OOM (error);
824 return DBUS_UID_UNSET;
827 if (!dbus_message_append_args (message,
828 DBUS_TYPE_STRING, &name,
831 dbus_message_unref (message);
832 _DBUS_SET_OOM (error);
833 return DBUS_UID_UNSET;
836 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
839 dbus_message_unref (message);
843 _DBUS_ASSERT_ERROR_IS_SET (error);
844 return DBUS_UID_UNSET;
847 if (dbus_set_error_from_message (error, reply))
849 _DBUS_ASSERT_ERROR_IS_SET (error);
850 dbus_message_unref (reply);
851 return DBUS_UID_UNSET;
854 if (!dbus_message_get_args (reply, error,
855 DBUS_TYPE_UINT32, &uid,
858 _DBUS_ASSERT_ERROR_IS_SET (error);
859 dbus_message_unref (reply);
860 return DBUS_UID_UNSET;
863 dbus_message_unref (reply);
865 return (unsigned long) uid;
869 * Asks the bus to return its globally unique ID, as described in the
870 * D-Bus specification. For the session bus, this is useful as a way
871 * to uniquely identify each user session. For the system bus,
872 * probably the bus ID is not useful; instead, use the machine ID
873 * since it's accessible without necessarily connecting to the bus and
874 * may be persistent beyond a single bus instance (across reboots for
875 * example). See dbus_get_local_machine_id().
877 * In addition to an ID for each bus and an ID for each machine, there is
878 * an ID for each address that the bus is listening on; that can
879 * be retrieved with dbus_connection_get_server_id(), though it is
880 * probably not very useful.
882 * @param connection the connection
883 * @param error location to store the error
884 * @returns the bus ID or #NULL if error is set
887 dbus_bus_get_id (DBusConnection *connection,
890 DBusMessage *message, *reply;
892 const char *v_STRING;
894 _dbus_return_val_if_fail (connection != NULL, NULL);
895 _dbus_return_val_if_error_is_set (error, NULL);
897 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
904 _DBUS_SET_OOM (error);
908 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
911 dbus_message_unref (message);
915 _DBUS_ASSERT_ERROR_IS_SET (error);
919 if (dbus_set_error_from_message (error, reply))
921 _DBUS_ASSERT_ERROR_IS_SET (error);
922 dbus_message_unref (reply);
927 if (!dbus_message_get_args (reply, error,
928 DBUS_TYPE_STRING, &v_STRING,
931 _DBUS_ASSERT_ERROR_IS_SET (error);
932 dbus_message_unref (reply);
936 id = _dbus_strdup (v_STRING); /* may be NULL */
938 dbus_message_unref (reply);
941 _DBUS_SET_OOM (error);
943 /* FIXME it might be nice to cache the ID locally */
949 * Asks the bus to assign the given name to this connection by invoking
950 * the RequestName method on the bus. This method is fully documented
951 * in the D-Bus specification. For quick reference, the flags and
952 * result codes are discussed here, but the specification is the
953 * canonical version of this information.
955 * First you should know that for each bus name, the bus stores
956 * a queue of connections that would like to own it. Only
957 * one owns it at a time - called the primary owner. If the primary
958 * owner releases the name or disconnects, then the next owner in the
959 * queue atomically takes over.
961 * So for example if you have an application org.freedesktop.TextEditor
962 * and multiple instances of it can be run, you can have all of them
963 * sitting in the queue. The first one to start up will receive messages
964 * sent to org.freedesktop.TextEditor, but if that one exits another
965 * will become the primary owner and receive messages.
967 * The queue means you don't need to manually watch for the current owner to
968 * disappear and then request the name again.
970 * When requesting a name, you can specify several flags.
972 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT and #DBUS_NAME_FLAG_DO_NOT_QUEUE
973 * are properties stored by the bus for this connection with respect to
974 * each requested bus name. These properties are stored even if the
975 * connection is queued and does not become the primary owner.
976 * You can update these flags by calling RequestName again (even if
977 * you already own the name).
979 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT means that another requestor of the
980 * name can take it away from you by specifying #DBUS_NAME_FLAG_REPLACE_EXISTING.
982 * #DBUS_NAME_FLAG_DO_NOT_QUEUE means that if you aren't the primary owner,
983 * you don't want to be queued up - you only care about being the
986 * Unlike the other two flags, #DBUS_NAME_FLAG_REPLACE_EXISTING is a property
987 * of the individual RequestName call, i.e. the bus does not persistently
988 * associate it with the connection-name pair. If a RequestName call includes
989 * the #DBUS_NAME_FLAG_REPLACE_EXISTING flag, and the current primary
990 * owner has #DBUS_NAME_FLAG_ALLOW_REPLACEMENT set, then the current primary
991 * owner will be kicked off.
993 * If no flags are given, an application will receive the requested
994 * name only if the name is currently unowned; and it will NOT give
995 * up the name if another application asks to take it over using
996 * #DBUS_NAME_FLAG_REPLACE_EXISTING.
998 * This function returns a result code. The possible result codes
1001 * #DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER means that the name had no
1002 * existing owner, and the caller is now the primary owner; or that
1003 * the name had an owner, and the caller specified
1004 * #DBUS_NAME_FLAG_REPLACE_EXISTING, and the current owner
1005 * specified #DBUS_NAME_FLAG_ALLOW_REPLACEMENT.
1007 * #DBUS_REQUEST_NAME_REPLY_IN_QUEUE happens only if the caller does NOT
1008 * specify #DBUS_NAME_FLAG_DO_NOT_QUEUE and either the current owner
1009 * did NOT specify #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or the caller did NOT
1010 * specify #DBUS_NAME_FLAG_REPLACE_EXISTING. In this case the caller ends up
1011 * in a queue to own the name after the current owner gives it up.
1013 * #DBUS_REQUEST_NAME_REPLY_EXISTS happens if the name has an owner
1014 * already and the caller specifies #DBUS_NAME_FLAG_DO_NOT_QUEUE
1015 * and either the current owner has NOT specified
1016 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or the caller did NOT specify
1017 * #DBUS_NAME_FLAG_REPLACE_EXISTING.
1019 * #DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER happens if an application
1020 * requests a name it already owns. (Re-requesting a name is useful if
1021 * you want to change the #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or
1022 * #DBUS_NAME_FLAG_DO_NOT_QUEUE settings.)
1024 * When a service represents an application, say "text editor," then
1025 * it should specify #DBUS_NAME_FLAG_ALLOW_REPLACEMENT if it wants
1026 * the last editor started to be the user's editor vs. the first one
1027 * started. Then any editor that can be the user's editor should
1028 * specify #DBUS_NAME_FLAG_REPLACE_EXISTING to either take over
1029 * (last-started-wins) or be queued up (first-started-wins) according
1030 * to whether #DBUS_NAME_FLAG_ALLOW_REPLACEMENT was given.
1032 * Conventionally, single-instance applications often offer a command
1033 * line option called --replace which means to replace the current
1034 * instance. To implement this, always set
1035 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT when you request your
1036 * application's bus name. When you lose ownership of your bus name,
1037 * you need to exit. Look for the signal "NameLost" from
1038 * #DBUS_SERVICE_DBUS and #DBUS_INTERFACE_DBUS (the signal's first
1039 * argument is the bus name that was lost). If starting up without
1040 * --replace, do not specify #DBUS_NAME_FLAG_REPLACE_EXISTING, and
1041 * exit if you fail to become the bus name owner. If --replace is
1042 * given, ask to replace the old owner.
1044 * @param connection the connection
1045 * @param name the name to request
1046 * @param flags flags
1047 * @param error location to store the error
1048 * @returns a result code, -1 if error is set
1051 dbus_bus_request_name (DBusConnection *connection,
1056 DBusMessage *message, *reply;
1057 dbus_uint32_t result;
1059 _dbus_return_val_if_fail (connection != NULL, 0);
1060 _dbus_return_val_if_fail (name != NULL, 0);
1061 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
1062 _dbus_return_val_if_error_is_set (error, 0);
1064 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1066 DBUS_INTERFACE_DBUS,
1069 if (message == NULL)
1071 _DBUS_SET_OOM (error);
1075 if (!dbus_message_append_args (message,
1076 DBUS_TYPE_STRING, &name,
1077 DBUS_TYPE_UINT32, &flags,
1080 dbus_message_unref (message);
1081 _DBUS_SET_OOM (error);
1085 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
1088 dbus_message_unref (message);
1092 _DBUS_ASSERT_ERROR_IS_SET (error);
1096 if (dbus_set_error_from_message (error, reply))
1098 _DBUS_ASSERT_ERROR_IS_SET (error);
1099 dbus_message_unref (reply);
1103 if (!dbus_message_get_args (reply, error,
1104 DBUS_TYPE_UINT32, &result,
1107 _DBUS_ASSERT_ERROR_IS_SET (error);
1108 dbus_message_unref (reply);
1112 dbus_message_unref (reply);
1119 * Asks the bus to unassign the given name from this connection by
1120 * invoking the ReleaseName method on the bus. The "ReleaseName"
1121 * method is canonically documented in the D-Bus specification.
1123 * Possible results are: #DBUS_RELEASE_NAME_REPLY_RELEASED
1124 * which means you owned the name or were in the queue to own it,
1125 * and and now you don't own it and aren't in the queue.
1126 * #DBUS_RELEASE_NAME_REPLY_NOT_OWNER which means someone else
1127 * owns the name so you can't release it.
1128 * #DBUS_RELEASE_NAME_REPLY_NON_EXISTENT
1129 * which means nobody owned the name.
1131 * @param connection the connection
1132 * @param name the name to remove
1133 * @param error location to store the error
1134 * @returns a result code, -1 if error is set
1137 dbus_bus_release_name (DBusConnection *connection,
1141 DBusMessage *message, *reply;
1142 dbus_uint32_t result;
1144 _dbus_return_val_if_fail (connection != NULL, 0);
1145 _dbus_return_val_if_fail (name != NULL, 0);
1146 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
1147 _dbus_return_val_if_error_is_set (error, 0);
1149 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1151 DBUS_INTERFACE_DBUS,
1154 if (message == NULL)
1156 _DBUS_SET_OOM (error);
1160 if (!dbus_message_append_args (message,
1161 DBUS_TYPE_STRING, &name,
1164 dbus_message_unref (message);
1165 _DBUS_SET_OOM (error);
1169 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
1172 dbus_message_unref (message);
1176 _DBUS_ASSERT_ERROR_IS_SET (error);
1180 if (dbus_set_error_from_message (error, reply))
1182 _DBUS_ASSERT_ERROR_IS_SET (error);
1183 dbus_message_unref (reply);
1187 if (!dbus_message_get_args (reply, error,
1188 DBUS_TYPE_UINT32, &result,
1191 _DBUS_ASSERT_ERROR_IS_SET (error);
1192 dbus_message_unref (reply);
1196 dbus_message_unref (reply);
1202 * Asks the bus whether a certain name has an owner.
1204 * Using this can easily result in a race condition,
1205 * since an owner can appear or disappear after you
1208 * If you want to request a name, just request it;
1209 * if you want to avoid replacing a current owner,
1210 * don't specify #DBUS_NAME_FLAG_REPLACE_EXISTING and
1211 * you will get an error if there's already an owner.
1213 * @param connection the connection
1214 * @param name the name
1215 * @param error location to store any errors
1216 * @returns #TRUE if the name exists, #FALSE if not or on error
1219 dbus_bus_name_has_owner (DBusConnection *connection,
1223 DBusMessage *message, *reply;
1226 _dbus_return_val_if_fail (connection != NULL, FALSE);
1227 _dbus_return_val_if_fail (name != NULL, FALSE);
1228 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
1229 _dbus_return_val_if_error_is_set (error, FALSE);
1231 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1233 DBUS_INTERFACE_DBUS,
1235 if (message == NULL)
1237 _DBUS_SET_OOM (error);
1241 if (!dbus_message_append_args (message,
1242 DBUS_TYPE_STRING, &name,
1245 dbus_message_unref (message);
1246 _DBUS_SET_OOM (error);
1250 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
1251 dbus_message_unref (message);
1255 _DBUS_ASSERT_ERROR_IS_SET (error);
1259 if (!dbus_message_get_args (reply, error,
1260 DBUS_TYPE_BOOLEAN, &exists,
1263 _DBUS_ASSERT_ERROR_IS_SET (error);
1264 dbus_message_unref (reply);
1268 dbus_message_unref (reply);
1273 * Starts a service that will request ownership of the given name.
1274 * The returned result will be one of be one of
1275 * #DBUS_START_REPLY_SUCCESS or #DBUS_START_REPLY_ALREADY_RUNNING if
1276 * successful. Pass #NULL if you don't care about the result.
1278 * The flags parameter is for future expansion, currently you should
1281 * It's often easier to avoid explicitly starting services, and
1282 * just send a method call to the service's bus name instead.
1283 * Method calls start a service to handle them by default
1284 * unless you call dbus_message_set_auto_start() to disable this
1287 * @param connection the connection
1288 * @param name the name we want the new service to request
1289 * @param flags the flags (should always be 0 for now)
1290 * @param result a place to store the result or #NULL
1291 * @param error location to store any errors
1292 * @returns #TRUE if the activation succeeded, #FALSE if not
1295 dbus_bus_start_service_by_name (DBusConnection *connection,
1297 dbus_uint32_t flags,
1298 dbus_uint32_t *result,
1304 _dbus_return_val_if_fail (connection != NULL, FALSE);
1305 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
1307 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1309 DBUS_INTERFACE_DBUS,
1310 "StartServiceByName");
1312 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &name,
1313 DBUS_TYPE_UINT32, &flags, DBUS_TYPE_INVALID))
1315 dbus_message_unref (msg);
1316 _DBUS_SET_OOM (error);
1320 reply = dbus_connection_send_with_reply_and_block (connection, msg,
1322 dbus_message_unref (msg);
1326 _DBUS_ASSERT_ERROR_IS_SET (error);
1330 if (dbus_set_error_from_message (error, reply))
1332 _DBUS_ASSERT_ERROR_IS_SET (error);
1333 dbus_message_unref (reply);
1337 if (result != NULL &&
1338 !dbus_message_get_args (reply, error, DBUS_TYPE_UINT32,
1339 result, DBUS_TYPE_INVALID))
1341 _DBUS_ASSERT_ERROR_IS_SET (error);
1342 dbus_message_unref (reply);
1346 dbus_message_unref (reply);
1351 send_no_return_values (DBusConnection *connection,
1357 /* Block to check success codepath */
1360 reply = dbus_connection_send_with_reply_and_block (connection, msg,
1364 _DBUS_ASSERT_ERROR_IS_SET (error);
1366 dbus_message_unref (reply);
1370 /* Silently-fail nonblocking codepath */
1371 dbus_message_set_no_reply (msg, TRUE);
1372 dbus_connection_send (connection, msg, NULL);
1377 * Adds a match rule to match messages going through the message bus.
1378 * The "rule" argument is the string form of a match rule.
1380 * If you pass #NULL for the error, this function will not
1381 * block; the match thus won't be added until you flush the
1382 * connection, and if there's an error adding the match
1383 * (only possible error is lack of resources in the bus),
1384 * you won't find out about it.
1386 * If you pass non-#NULL for the error this function will
1387 * block until it gets a reply.
1389 * Normal API conventions would have the function return
1390 * a boolean value indicating whether the error was set,
1391 * but that would require blocking always to determine
1394 * The AddMatch method is fully documented in the D-Bus
1395 * specification. For quick reference, the format of the
1396 * match rules is discussed here, but the specification
1397 * is the canonical version of this information.
1399 * Rules are specified as a string of comma separated
1400 * key/value pairs. An example is
1401 * "type='signal',sender='org.freedesktop.DBus',
1402 * interface='org.freedesktop.DBus',member='Foo',
1403 * path='/bar/foo',destination=':452345.34'"
1405 * Possible keys you can match on are type, sender,
1406 * interface, member, path, destination and numbered
1407 * keys to match message args (keys are 'arg0', 'arg1', etc.).
1408 * Omitting a key from the rule indicates
1409 * a wildcard match. For instance omitting
1410 * the member from a match rule but adding a sender would
1411 * let all messages from that sender through regardless of
1414 * Matches are inclusive not exclusive so as long as one
1415 * rule matches the message will get through. It is important
1416 * to note this because every time a message is received the
1417 * application will be paged into memory to process it. This
1418 * can cause performance problems such as draining batteries
1419 * on embedded platforms.
1421 * If you match message args ('arg0', 'arg1', and so forth)
1422 * only string arguments will match. That is, arg0='5' means
1423 * match the string "5" not the integer 5.
1425 * Currently there is no way to match against non-string arguments.
1427 * Matching on interface is tricky because method call
1428 * messages only optionally specify the interface.
1429 * If a message omits the interface, then it will NOT match
1430 * if the rule specifies an interface name. This means match
1431 * rules on method calls should not usually give an interface.
1433 * However, signal messages are required to include the interface
1434 * so when matching signals usually you should specify the interface
1435 * in the match rule.
1437 * For security reasons, you can match arguments only up to
1438 * #DBUS_MAXIMUM_MATCH_RULE_ARG_NUMBER.
1440 * Match rules have a maximum length of #DBUS_MAXIMUM_MATCH_RULE_LENGTH
1443 * Both of these maximums are much higher than you're likely to need,
1444 * they only exist because the D-Bus bus daemon has fixed limits on
1445 * all resource usage.
1447 * @param connection connection to the message bus
1448 * @param rule textual form of match rule
1449 * @param error location to store any errors
1452 dbus_bus_add_match (DBusConnection *connection,
1458 _dbus_return_if_fail (rule != NULL);
1460 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1462 DBUS_INTERFACE_DBUS,
1467 _DBUS_SET_OOM (error);
1471 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
1474 dbus_message_unref (msg);
1475 _DBUS_SET_OOM (error);
1479 send_no_return_values (connection, msg, error);
1481 dbus_message_unref (msg);
1485 * Removes a previously-added match rule "by value" (the most
1486 * recently-added identical rule gets removed). The "rule" argument
1487 * is the string form of a match rule.
1489 * The bus compares match rules semantically, not textually, so
1490 * whitespace and ordering don't have to be identical to
1491 * the rule you passed to dbus_bus_add_match().
1493 * If you pass #NULL for the error, this function will not
1494 * block; otherwise it will. See detailed explanation in
1495 * docs for dbus_bus_add_match().
1497 * @param connection connection to the message bus
1498 * @param rule textual form of match rule
1499 * @param error location to store any errors
1502 dbus_bus_remove_match (DBusConnection *connection,
1508 _dbus_return_if_fail (rule != NULL);
1510 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1512 DBUS_INTERFACE_DBUS,
1515 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
1518 dbus_message_unref (msg);
1519 _DBUS_SET_OOM (error);
1523 send_no_return_values (connection, msg, error);
1525 dbus_message_unref (msg);