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"
33 * @defgroup DBusBus Message bus APIs
35 * @brief Functions for communicating with the message bus
37 * @todo right now the default address of the system bus is hardcoded,
38 * so if you change it in the global config file suddenly you have to
39 * set DBUS_SYSTEM_BUS_ADDRESS env variable. Might be nice if the
40 * client lib somehow read the config file, or if the bus on startup
41 * somehow wrote out its address to a well-known spot, but might also
46 * @defgroup DBusBusInternals Message bus APIs internals
47 * @ingroup DBusInternals
48 * @brief Internals of functions for communicating with the message bus
54 * Block of message-bus-related data we attach to each
55 * #DBusConnection used with these convenience functions.
58 * @todo get rid of most of these; they should be done
59 * with DBusGProxy and the Qt equivalent, i.e. the same
60 * way any other interface would be used.
64 DBusConnection *connection; /**< Connection we're associated with */
65 char *unique_name; /**< Unique name of this connection */
67 unsigned int is_well_known : 1; /**< Is one of the well-known connections in our global array */
70 /** The slot we have reserved to store BusData.
72 static dbus_int32_t bus_data_slot = -1;
74 /** Number of bus types */
77 static DBusConnection *bus_connections[N_BUS_TYPES];
78 static char *bus_connection_addresses[N_BUS_TYPES] = { NULL, NULL, NULL };
80 static DBusBusType activation_bus_type = DBUS_BUS_STARTER;
82 static dbus_bool_t initialized = FALSE;
85 * Lock for globals in this file
87 _DBUS_DEFINE_GLOBAL_LOCK (bus);
90 addresses_shutdown_func (void *data)
95 while (i < N_BUS_TYPES)
97 if (bus_connections[i] != NULL)
98 _dbus_warn ("dbus_shutdown() called but connections were still live!");
100 dbus_free (bus_connection_addresses[i]);
101 bus_connection_addresses[i] = NULL;
105 activation_bus_type = DBUS_BUS_STARTER;
109 get_from_env (char **connection_p,
114 _dbus_assert (*connection_p == NULL);
116 s = _dbus_getenv (env_var);
117 if (s == NULL || *s == '\0')
118 return TRUE; /* successfully didn't use the env var */
121 *connection_p = _dbus_strdup (s);
122 return *connection_p != NULL;
127 init_connections_unlocked (void)
135 while (i < N_BUS_TYPES)
137 bus_connections[i] = NULL;
141 /* Don't init these twice, we may run this code twice if
142 * init_connections_unlocked() fails midway through.
143 * In practice, each block below should contain only one
144 * "return FALSE" or running through twice may not
148 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
150 _dbus_verbose ("Filling in system bus address...\n");
152 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SYSTEM],
153 "DBUS_SYSTEM_BUS_ADDRESS"))
158 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
160 /* Use default system bus address if none set in environment */
161 bus_connection_addresses[DBUS_BUS_SYSTEM] =
162 _dbus_strdup (DBUS_SYSTEM_BUS_DEFAULT_ADDRESS);
163 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
166 _dbus_verbose (" used default system bus \"%s\"\n",
167 bus_connection_addresses[DBUS_BUS_SYSTEM]);
170 _dbus_verbose (" used env var system bus \"%s\"\n",
171 bus_connection_addresses[DBUS_BUS_SYSTEM]);
173 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
175 _dbus_verbose ("Filling in session bus address...\n");
177 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SESSION],
178 "DBUS_SESSION_BUS_ADDRESS"))
180 _dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_SESSION] ?
181 bus_connection_addresses[DBUS_BUS_SESSION] : "none set");
184 if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
186 _dbus_verbose ("Filling in activation bus address...\n");
188 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_STARTER],
189 "DBUS_STARTER_ADDRESS"))
192 _dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_STARTER] ?
193 bus_connection_addresses[DBUS_BUS_STARTER] : "none set");
197 if (bus_connection_addresses[DBUS_BUS_STARTER] != NULL)
199 s = _dbus_getenv ("DBUS_STARTER_BUS_TYPE");
203 _dbus_verbose ("Bus activation type was set to \"%s\"\n", s);
205 if (strcmp (s, "system") == 0)
206 activation_bus_type = DBUS_BUS_SYSTEM;
207 else if (strcmp (s, "session") == 0)
208 activation_bus_type = DBUS_BUS_SESSION;
213 /* Default to the session bus instead if available */
214 if (bus_connection_addresses[DBUS_BUS_SESSION] != NULL)
216 bus_connection_addresses[DBUS_BUS_STARTER] =
217 _dbus_strdup (bus_connection_addresses[DBUS_BUS_SESSION]);
218 if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
223 /* If we return FALSE we have to be sure that restarting
224 * the above code will work right
227 if (!_dbus_setenv ("DBUS_ACTIVATION_ADDRESS", NULL))
230 if (!_dbus_setenv ("DBUS_ACTIVATION_BUS_TYPE", NULL))
233 if (!_dbus_register_shutdown_func (addresses_shutdown_func,
244 bus_data_free (void *data)
248 if (bd->is_well_known)
252 /* We may be stored in more than one slot */
254 while (i < N_BUS_TYPES)
256 if (bus_connections[i] == bd->connection)
257 bus_connections[i] = NULL;
264 dbus_free (bd->unique_name);
267 dbus_connection_free_data_slot (&bus_data_slot);
271 ensure_bus_data (DBusConnection *connection)
275 if (!dbus_connection_allocate_data_slot (&bus_data_slot))
278 bd = dbus_connection_get_data (connection, bus_data_slot);
281 bd = dbus_new0 (BusData, 1);
284 dbus_connection_free_data_slot (&bus_data_slot);
288 bd->connection = connection;
290 if (!dbus_connection_set_data (connection, bus_data_slot, bd,
294 dbus_connection_free_data_slot (&bus_data_slot);
298 /* Data slot refcount now held by the BusData */
302 dbus_connection_free_data_slot (&bus_data_slot);
308 /** @} */ /* end of implementation details docs */
311 * @addtogroup DBusBus
316 * Connects to a bus daemon and registers the client with it. If a
317 * connection to the bus already exists, then that connection is
318 * returned. Caller owns a reference to the bus.
320 * @todo alex thinks we should nullify the connection when we get a disconnect-message.
322 * @param type bus type
323 * @param error address where an error can be returned.
324 * @returns a DBusConnection with new ref
327 dbus_bus_get (DBusBusType type,
331 DBusConnection *connection;
333 DBusBusType address_type;
335 _dbus_return_val_if_fail (type >= 0 && type < N_BUS_TYPES, NULL);
336 _dbus_return_val_if_error_is_set (error, NULL);
340 if (!init_connections_unlocked ())
343 _DBUS_SET_OOM (error);
347 /* We want to use the activation address even if the
348 * activating bus is the session or system bus,
353 /* Use the real type of the activation bus for getting its
354 * connection, but only if the real type's address is available. (If
355 * the activating bus isn't a well-known bus then
356 * activation_bus_type == DBUS_BUS_STARTER)
358 if (type == DBUS_BUS_STARTER &&
359 bus_connection_addresses[activation_bus_type] != NULL)
360 type = activation_bus_type;
362 if (bus_connections[type] != NULL)
364 connection = bus_connections[type];
365 dbus_connection_ref (connection);
371 address = bus_connection_addresses[address_type];
374 dbus_set_error (error, DBUS_ERROR_FAILED,
375 "Unable to determine the address of the message bus");
380 connection = dbus_connection_open (address, error);
384 _DBUS_ASSERT_ERROR_IS_SET (error);
389 /* By default we're bound to the lifecycle of
392 dbus_connection_set_exit_on_disconnect (connection,
395 if (!dbus_bus_register (connection, error))
397 _DBUS_ASSERT_ERROR_IS_SET (error);
398 dbus_connection_disconnect (connection);
399 dbus_connection_unref (connection);
405 bus_connections[type] = connection;
406 bd = ensure_bus_data (connection);
407 _dbus_assert (bd != NULL);
409 bd->is_well_known = TRUE;
417 * Registers a connection with the bus. This must be the first
418 * thing an application does when connecting to the message bus.
419 * If registration succeeds, the unique name will be set,
420 * and can be obtained using dbus_bus_get_unique_name().
422 * @param connection the connection
423 * @param error place to store errors
424 * @returns #TRUE on success
427 dbus_bus_register (DBusConnection *connection,
430 DBusMessage *message, *reply;
435 _dbus_return_val_if_fail (connection != NULL, FALSE);
436 _dbus_return_val_if_error_is_set (error, FALSE);
440 bd = ensure_bus_data (connection);
443 _DBUS_SET_OOM (error);
447 if (bd->unique_name != NULL)
449 _dbus_warn ("Attempt to register the same DBusConnection with the message bus, but it is already registered\n");
450 /* This isn't an error, it's a programming bug. We'll be nice
451 * and not _dbus_assert_not_reached()
456 message = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
457 DBUS_PATH_ORG_FREEDESKTOP_DBUS,
458 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
463 _DBUS_SET_OOM (error);
467 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
469 dbus_message_unref (message);
473 else if (dbus_set_error_from_message (error, reply))
475 else if (!dbus_message_get_args (reply, error,
476 DBUS_TYPE_STRING, &name,
480 bd->unique_name = _dbus_strdup (name);
481 if (bd->unique_name == NULL)
483 _DBUS_SET_OOM (error);
491 dbus_message_unref (reply);
494 _DBUS_ASSERT_ERROR_IS_SET (error);
501 * Sets the unique name of the connection. Can only be used if you
502 * registered with the bus manually (i.e. if you did not call
503 * dbus_bus_register()). Can only be called once per connection.
505 * @param connection the connection
506 * @param unique_name the unique name
507 * @returns #FALSE if not enough memory
510 dbus_bus_set_unique_name (DBusConnection *connection,
511 const char *unique_name)
515 _dbus_return_val_if_fail (connection != NULL, FALSE);
516 _dbus_return_val_if_fail (unique_name != NULL, FALSE);
518 bd = ensure_bus_data (connection);
522 _dbus_assert (bd->unique_name == NULL);
524 bd->unique_name = _dbus_strdup (unique_name);
525 return bd->unique_name != NULL;
529 * Gets the unique name of the connection. Only possible after the
530 * connection has been registered with the message bus.
532 * @param connection the connection
533 * @returns the unique name
536 dbus_bus_get_unique_name (DBusConnection *connection)
540 _dbus_return_val_if_fail (connection != NULL, NULL);
542 bd = ensure_bus_data (connection);
546 return bd->unique_name;
550 * Asks the bus to return the uid of the named
553 * @param connection the connection
554 * @param name a name owned by the connection
555 * @param error location to store the error
556 * @returns a result code, -1 if error is set
559 dbus_bus_get_unix_user (DBusConnection *connection,
563 DBusMessage *message, *reply;
566 _dbus_return_val_if_fail (connection != NULL, DBUS_UID_UNSET);
567 _dbus_return_val_if_fail (name != NULL, DBUS_UID_UNSET);
568 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), DBUS_UID_UNSET);
569 _dbus_return_val_if_error_is_set (error, DBUS_UID_UNSET);
571 message = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
572 DBUS_PATH_ORG_FREEDESKTOP_DBUS,
573 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
574 "GetConnectionUnixUser");
578 _DBUS_SET_OOM (error);
579 return DBUS_UID_UNSET;
582 if (!dbus_message_append_args (message,
583 DBUS_TYPE_STRING, &name,
586 dbus_message_unref (message);
587 _DBUS_SET_OOM (error);
588 return DBUS_UID_UNSET;
591 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
594 dbus_message_unref (message);
598 _DBUS_ASSERT_ERROR_IS_SET (error);
599 return DBUS_UID_UNSET;
602 if (dbus_set_error_from_message (error, reply))
604 _DBUS_ASSERT_ERROR_IS_SET (error);
605 dbus_message_unref (reply);
606 return DBUS_UID_UNSET;
609 if (!dbus_message_get_args (reply, error,
610 DBUS_TYPE_UINT32, &uid,
613 _DBUS_ASSERT_ERROR_IS_SET (error);
614 dbus_message_unref (reply);
615 return DBUS_UID_UNSET;
618 dbus_message_unref (reply);
620 return (unsigned long) uid;
625 * Asks the bus to assign the given name to this connection.
627 * @todo these docs are not complete, need to document the
628 * return value and flags
630 * @todo if we get an error reply, it has to be converted into
631 * DBusError and returned
633 * @param connection the connection
634 * @param name the name to request
636 * @param error location to store the error
637 * @returns a result code, -1 if error is set
640 dbus_bus_request_name (DBusConnection *connection,
645 DBusMessage *message, *reply;
646 dbus_uint32_t result;
648 _dbus_return_val_if_fail (connection != NULL, 0);
649 _dbus_return_val_if_fail (name != NULL, 0);
650 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
651 _dbus_return_val_if_error_is_set (error, 0);
653 message = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
654 DBUS_PATH_ORG_FREEDESKTOP_DBUS,
655 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
660 _DBUS_SET_OOM (error);
664 if (!dbus_message_append_args (message,
665 DBUS_TYPE_STRING, &name,
666 DBUS_TYPE_UINT32, &flags,
669 dbus_message_unref (message);
670 _DBUS_SET_OOM (error);
674 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
677 dbus_message_unref (message);
681 _DBUS_ASSERT_ERROR_IS_SET (error);
685 if (dbus_set_error_from_message (error, reply))
687 _DBUS_ASSERT_ERROR_IS_SET (error);
688 dbus_message_unref (reply);
692 if (!dbus_message_get_args (reply, error,
693 DBUS_TYPE_UINT32, &result,
696 _DBUS_ASSERT_ERROR_IS_SET (error);
697 dbus_message_unref (reply);
701 dbus_message_unref (reply);
707 * Checks whether a certain name has an owner.
709 * @param connection the connection
710 * @param name the name
711 * @param error location to store any errors
712 * @returns #TRUE if the name exists, #FALSE if not or on error
715 dbus_bus_name_has_owner (DBusConnection *connection,
719 DBusMessage *message, *reply;
722 _dbus_return_val_if_fail (connection != NULL, FALSE);
723 _dbus_return_val_if_fail (name != NULL, FALSE);
724 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
725 _dbus_return_val_if_error_is_set (error, FALSE);
727 message = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
728 DBUS_PATH_ORG_FREEDESKTOP_DBUS,
729 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
733 _DBUS_SET_OOM (error);
737 if (!dbus_message_append_args (message,
738 DBUS_TYPE_STRING, &name,
741 dbus_message_unref (message);
742 _DBUS_SET_OOM (error);
746 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
747 dbus_message_unref (message);
751 _DBUS_ASSERT_ERROR_IS_SET (error);
755 if (!dbus_message_get_args (reply, error,
756 DBUS_TYPE_BOOLEAN, &exists,
759 _DBUS_ASSERT_ERROR_IS_SET (error);
760 dbus_message_unref (reply);
764 dbus_message_unref (reply);
769 * Starts a service that will request ownership of the given name.
770 * The returned result will be one of be one of
771 * #DBUS_START_REPLY_SUCCESS or #DBUS_START_REPLY_ALREADY_RUNNING if
772 * successful. Pass #NULL if you don't care about the result.
774 * @param connection the connection
775 * @param name the name we want the new service to request
776 * @param flags the flags
777 * @param result a place to store the result or #NULL
778 * @param error location to store any errors
779 * @returns #TRUE if the activation succeeded, #FALSE if not
781 * @todo document what the flags do
784 dbus_bus_start_service_by_name (DBusConnection *connection,
787 dbus_uint32_t *result,
793 _dbus_return_val_if_fail (connection != NULL, FALSE);
794 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
796 msg = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
797 DBUS_PATH_ORG_FREEDESKTOP_DBUS,
798 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
799 "StartServiceByName");
801 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &name,
802 DBUS_TYPE_UINT32, &flags, DBUS_TYPE_INVALID))
804 dbus_message_unref (msg);
805 _DBUS_SET_OOM (error);
809 reply = dbus_connection_send_with_reply_and_block (connection, msg,
811 dbus_message_unref (msg);
815 _DBUS_ASSERT_ERROR_IS_SET (error);
819 if (dbus_set_error_from_message (error, reply))
821 _DBUS_ASSERT_ERROR_IS_SET (error);
822 dbus_message_unref (reply);
826 if (result != NULL &&
827 !dbus_message_get_args (reply, error, DBUS_TYPE_UINT32,
828 result, DBUS_TYPE_INVALID))
830 _DBUS_ASSERT_ERROR_IS_SET (error);
831 dbus_message_unref (reply);
835 dbus_message_unref (reply);
840 send_no_return_values (DBusConnection *connection,
846 /* Block to check success codepath */
849 reply = dbus_connection_send_with_reply_and_block (connection, msg,
853 _DBUS_ASSERT_ERROR_IS_SET (error);
855 dbus_message_unref (reply);
859 /* Silently-fail nonblocking codepath */
860 dbus_message_set_no_reply (msg, TRUE);
861 dbus_connection_send (connection, msg, NULL);
866 * Adds a match rule to match messages going through the message bus.
867 * The "rule" argument is the string form of a match rule.
869 * If you pass #NULL for the error, this function will not
870 * block; the match thus won't be added until you flush the
871 * connection, and if there's an error adding the match
872 * (only possible error is lack of resources in the bus),
873 * you won't find out about it.
875 * If you pass non-#NULL for the error this function will
876 * block until it gets a reply.
878 * Normal API conventions would have the function return
879 * a boolean value indicating whether the error was set,
880 * but that would require blocking always to determine
883 * @param connection connection to the message bus
884 * @param rule textual form of match rule
885 * @param error location to store any errors
888 dbus_bus_add_match (DBusConnection *connection,
894 _dbus_return_if_fail (rule != NULL);
896 msg = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
897 DBUS_PATH_ORG_FREEDESKTOP_DBUS,
898 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
903 _DBUS_SET_OOM (error);
907 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
910 dbus_message_unref (msg);
911 _DBUS_SET_OOM (error);
915 send_no_return_values (connection, msg, error);
917 dbus_message_unref (msg);
921 * Removes a previously-added match rule "by value" (the most
922 * recently-added identical rule gets removed). The "rule" argument
923 * is the string form of a match rule.
925 * If you pass #NULL for the error, this function will not
926 * block; otherwise it will. See detailed explanation in
927 * docs for dbus_bus_add_match().
929 * @param connection connection to the message bus
930 * @param rule textual form of match rule
931 * @param error location to store any errors
934 dbus_bus_remove_match (DBusConnection *connection,
940 _dbus_return_if_fail (rule != NULL);
942 msg = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
943 DBUS_PATH_ORG_FREEDESKTOP_DBUS,
944 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
947 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
950 dbus_message_unref (msg);
951 _DBUS_SET_OOM (error);
955 send_no_return_values (connection, msg, error);
957 dbus_message_unref (msg);