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"
34 * @defgroup DBusBus Message bus APIs
36 * @brief Functions for communicating with the message bus
38 * @todo right now the default address of the system bus is hardcoded,
39 * so if you change it in the global config file suddenly you have to
40 * set DBUS_SYSTEM_BUS_ADDRESS env variable. Might be nice if the
41 * client lib somehow read the config file, or if the bus on startup
42 * somehow wrote out its address to a well-known spot, but might also
47 * @defgroup DBusBusInternals Message bus APIs internals
48 * @ingroup DBusInternals
49 * @brief Internals of functions for communicating with the message bus
55 * Block of message-bus-related data we attach to each
56 * #DBusConnection used with these convenience functions.
59 * @todo get rid of most of these; they should be done
60 * with DBusGProxy and the Qt equivalent, i.e. the same
61 * way any other interface would be used.
65 DBusConnection *connection; /**< Connection we're associated with */
66 char *unique_name; /**< Unique name of this connection */
68 unsigned int is_well_known : 1; /**< Is one of the well-known connections in our global array */
71 /** The slot we have reserved to store BusData.
73 static dbus_int32_t bus_data_slot = -1;
75 /** Number of bus types */
78 static DBusConnection *bus_connections[N_BUS_TYPES];
79 static char *bus_connection_addresses[N_BUS_TYPES] = { NULL, NULL, NULL };
81 static DBusBusType activation_bus_type = DBUS_BUS_STARTER;
83 static dbus_bool_t initialized = FALSE;
86 * Lock for globals in this file
88 _DBUS_DEFINE_GLOBAL_LOCK (bus);
91 addresses_shutdown_func (void *data)
96 while (i < N_BUS_TYPES)
98 if (bus_connections[i] != NULL)
99 _dbus_warn ("dbus_shutdown() called but connections were still live!");
101 dbus_free (bus_connection_addresses[i]);
102 bus_connection_addresses[i] = NULL;
106 activation_bus_type = DBUS_BUS_STARTER;
110 get_from_env (char **connection_p,
115 _dbus_assert (*connection_p == NULL);
117 s = _dbus_getenv (env_var);
118 if (s == NULL || *s == '\0')
119 return TRUE; /* successfully didn't use the env var */
122 *connection_p = _dbus_strdup (s);
123 return *connection_p != NULL;
128 init_connections_unlocked (void)
136 while (i < N_BUS_TYPES)
138 bus_connections[i] = NULL;
142 /* Don't init these twice, we may run this code twice if
143 * init_connections_unlocked() fails midway through.
144 * In practice, each block below should contain only one
145 * "return FALSE" or running through twice may not
149 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
151 _dbus_verbose ("Filling in system bus address...\n");
153 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SYSTEM],
154 "DBUS_SYSTEM_BUS_ADDRESS"))
159 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
161 /* Use default system bus address if none set in environment */
162 bus_connection_addresses[DBUS_BUS_SYSTEM] =
163 _dbus_strdup (DBUS_SYSTEM_BUS_DEFAULT_ADDRESS);
164 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
167 _dbus_verbose (" used default system bus \"%s\"\n",
168 bus_connection_addresses[DBUS_BUS_SYSTEM]);
171 _dbus_verbose (" used env var system bus \"%s\"\n",
172 bus_connection_addresses[DBUS_BUS_SYSTEM]);
174 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
176 _dbus_verbose ("Filling in session bus address...\n");
178 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SESSION],
179 "DBUS_SESSION_BUS_ADDRESS"))
181 _dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_SESSION] ?
182 bus_connection_addresses[DBUS_BUS_SESSION] : "none set");
185 if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
187 _dbus_verbose ("Filling in activation bus address...\n");
189 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_STARTER],
190 "DBUS_STARTER_ADDRESS"))
193 _dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_STARTER] ?
194 bus_connection_addresses[DBUS_BUS_STARTER] : "none set");
198 if (bus_connection_addresses[DBUS_BUS_STARTER] != NULL)
200 s = _dbus_getenv ("DBUS_STARTER_BUS_TYPE");
204 _dbus_verbose ("Bus activation type was set to \"%s\"\n", s);
206 if (strcmp (s, "system") == 0)
207 activation_bus_type = DBUS_BUS_SYSTEM;
208 else if (strcmp (s, "session") == 0)
209 activation_bus_type = DBUS_BUS_SESSION;
214 /* Default to the session bus instead if available */
215 if (bus_connection_addresses[DBUS_BUS_SESSION] != NULL)
217 bus_connection_addresses[DBUS_BUS_STARTER] =
218 _dbus_strdup (bus_connection_addresses[DBUS_BUS_SESSION]);
219 if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
224 /* If we return FALSE we have to be sure that restarting
225 * the above code will work right
228 if (!_dbus_setenv ("DBUS_ACTIVATION_ADDRESS", NULL))
231 if (!_dbus_setenv ("DBUS_ACTIVATION_BUS_TYPE", NULL))
234 if (!_dbus_register_shutdown_func (addresses_shutdown_func,
245 bus_data_free (void *data)
249 if (bd->is_well_known)
253 /* We may be stored in more than one slot */
255 while (i < N_BUS_TYPES)
257 if (bus_connections[i] == bd->connection)
258 bus_connections[i] = NULL;
265 dbus_free (bd->unique_name);
268 dbus_connection_free_data_slot (&bus_data_slot);
272 ensure_bus_data (DBusConnection *connection)
276 if (!dbus_connection_allocate_data_slot (&bus_data_slot))
279 bd = dbus_connection_get_data (connection, bus_data_slot);
282 bd = dbus_new0 (BusData, 1);
285 dbus_connection_free_data_slot (&bus_data_slot);
289 bd->connection = connection;
291 if (!dbus_connection_set_data (connection, bus_data_slot, bd,
295 dbus_connection_free_data_slot (&bus_data_slot);
299 /* Data slot refcount now held by the BusData */
303 dbus_connection_free_data_slot (&bus_data_slot);
309 /** @} */ /* end of implementation details docs */
312 * @addtogroup DBusBus
317 * Connects to a bus daemon and registers the client with it. If a
318 * connection to the bus already exists, then that connection is
319 * returned. Caller owns a reference to the bus.
321 * @todo alex thinks we should nullify the connection when we get a disconnect-message.
323 * @param type bus type
324 * @param error address where an error can be returned.
325 * @returns a DBusConnection with new ref
328 dbus_bus_get (DBusBusType type,
332 DBusConnection *connection;
334 DBusBusType address_type;
336 _dbus_return_val_if_fail (type >= 0 && type < N_BUS_TYPES, NULL);
337 _dbus_return_val_if_error_is_set (error, NULL);
341 if (!init_connections_unlocked ())
344 _DBUS_SET_OOM (error);
348 /* We want to use the activation address even if the
349 * activating bus is the session or system bus,
354 /* Use the real type of the activation bus for getting its
355 * connection, but only if the real type's address is available. (If
356 * the activating bus isn't a well-known bus then
357 * activation_bus_type == DBUS_BUS_STARTER)
359 if (type == DBUS_BUS_STARTER &&
360 bus_connection_addresses[activation_bus_type] != NULL)
361 type = activation_bus_type;
363 if (bus_connections[type] != NULL)
365 connection = bus_connections[type];
366 dbus_connection_ref (connection);
372 address = bus_connection_addresses[address_type];
375 dbus_set_error (error, DBUS_ERROR_FAILED,
376 "Unable to determine the address of the message bus");
381 connection = dbus_connection_open (address, error);
385 _DBUS_ASSERT_ERROR_IS_SET (error);
390 /* By default we're bound to the lifecycle of
393 dbus_connection_set_exit_on_disconnect (connection,
396 if (!dbus_bus_register (connection, error))
398 _DBUS_ASSERT_ERROR_IS_SET (error);
399 dbus_connection_disconnect (connection);
400 dbus_connection_unref (connection);
406 bus_connections[type] = connection;
407 bd = ensure_bus_data (connection);
408 _dbus_assert (bd != NULL);
410 bd->is_well_known = TRUE;
418 * Registers a connection with the bus. This must be the first
419 * thing an application does when connecting to the message bus.
420 * If registration succeeds, the unique name will be set,
421 * and can be obtained using dbus_bus_get_unique_name().
423 * @param connection the connection
424 * @param error place to store errors
425 * @returns #TRUE on success
428 dbus_bus_register (DBusConnection *connection,
431 DBusMessage *message, *reply;
436 _dbus_return_val_if_fail (connection != NULL, FALSE);
437 _dbus_return_val_if_error_is_set (error, FALSE);
441 bd = ensure_bus_data (connection);
444 _DBUS_SET_OOM (error);
448 if (bd->unique_name != NULL)
450 _dbus_warn ("Attempt to register the same DBusConnection with the message bus, but it is already registered\n");
451 /* This isn't an error, it's a programming bug. We'll be nice
452 * and not _dbus_assert_not_reached()
457 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
464 _DBUS_SET_OOM (error);
468 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
470 dbus_message_unref (message);
474 else if (dbus_set_error_from_message (error, reply))
476 else if (!dbus_message_get_args (reply, error,
477 DBUS_TYPE_STRING, &name,
481 bd->unique_name = _dbus_strdup (name);
482 if (bd->unique_name == NULL)
484 _DBUS_SET_OOM (error);
492 dbus_message_unref (reply);
495 _DBUS_ASSERT_ERROR_IS_SET (error);
502 * Sets the unique name of the connection. Can only be used if you
503 * registered with the bus manually (i.e. if you did not call
504 * dbus_bus_register()). Can only be called once per connection.
506 * @param connection the connection
507 * @param unique_name the unique name
508 * @returns #FALSE if not enough memory
511 dbus_bus_set_unique_name (DBusConnection *connection,
512 const char *unique_name)
516 _dbus_return_val_if_fail (connection != NULL, FALSE);
517 _dbus_return_val_if_fail (unique_name != NULL, FALSE);
519 bd = ensure_bus_data (connection);
523 _dbus_assert (bd->unique_name == NULL);
525 bd->unique_name = _dbus_strdup (unique_name);
526 return bd->unique_name != NULL;
530 * Gets the unique name of the connection. Only possible after the
531 * connection has been registered with the message bus.
533 * @param connection the connection
534 * @returns the unique name
537 dbus_bus_get_unique_name (DBusConnection *connection)
541 _dbus_return_val_if_fail (connection != NULL, NULL);
543 bd = ensure_bus_data (connection);
547 return bd->unique_name;
551 * Asks the bus to return the uid of the named
554 * @param connection the connection
555 * @param name a name owned by the connection
556 * @param error location to store the error
557 * @returns a result code, -1 if error is set
560 dbus_bus_get_unix_user (DBusConnection *connection,
564 DBusMessage *message, *reply;
567 _dbus_return_val_if_fail (connection != NULL, DBUS_UID_UNSET);
568 _dbus_return_val_if_fail (name != NULL, DBUS_UID_UNSET);
569 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), DBUS_UID_UNSET);
570 _dbus_return_val_if_error_is_set (error, DBUS_UID_UNSET);
572 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
575 "GetConnectionUnixUser");
579 _DBUS_SET_OOM (error);
580 return DBUS_UID_UNSET;
583 if (!dbus_message_append_args (message,
584 DBUS_TYPE_STRING, &name,
587 dbus_message_unref (message);
588 _DBUS_SET_OOM (error);
589 return DBUS_UID_UNSET;
592 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
595 dbus_message_unref (message);
599 _DBUS_ASSERT_ERROR_IS_SET (error);
600 return DBUS_UID_UNSET;
603 if (dbus_set_error_from_message (error, reply))
605 _DBUS_ASSERT_ERROR_IS_SET (error);
606 dbus_message_unref (reply);
607 return DBUS_UID_UNSET;
610 if (!dbus_message_get_args (reply, error,
611 DBUS_TYPE_UINT32, &uid,
614 _DBUS_ASSERT_ERROR_IS_SET (error);
615 dbus_message_unref (reply);
616 return DBUS_UID_UNSET;
619 dbus_message_unref (reply);
621 return (unsigned long) uid;
626 * Asks the bus to assign the given name to this connection.
628 * @todo these docs are not complete, need to document the
629 * return value and flags
631 * @todo if we get an error reply, it has to be converted into
632 * DBusError and returned
634 * @param connection the connection
635 * @param name the name to request
637 * @param error location to store the error
638 * @returns a result code, -1 if error is set
641 dbus_bus_request_name (DBusConnection *connection,
646 DBusMessage *message, *reply;
647 dbus_uint32_t result;
649 _dbus_return_val_if_fail (connection != NULL, 0);
650 _dbus_return_val_if_fail (name != NULL, 0);
651 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
652 _dbus_return_val_if_error_is_set (error, 0);
654 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
661 _DBUS_SET_OOM (error);
665 if (!dbus_message_append_args (message,
666 DBUS_TYPE_STRING, &name,
667 DBUS_TYPE_UINT32, &flags,
670 dbus_message_unref (message);
671 _DBUS_SET_OOM (error);
675 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
678 dbus_message_unref (message);
682 _DBUS_ASSERT_ERROR_IS_SET (error);
686 if (dbus_set_error_from_message (error, reply))
688 _DBUS_ASSERT_ERROR_IS_SET (error);
689 dbus_message_unref (reply);
693 if (!dbus_message_get_args (reply, error,
694 DBUS_TYPE_UINT32, &result,
697 _DBUS_ASSERT_ERROR_IS_SET (error);
698 dbus_message_unref (reply);
702 dbus_message_unref (reply);
708 * Checks whether a certain name has an owner.
710 * @param connection the connection
711 * @param name the name
712 * @param error location to store any errors
713 * @returns #TRUE if the name exists, #FALSE if not or on error
716 dbus_bus_name_has_owner (DBusConnection *connection,
720 DBusMessage *message, *reply;
723 _dbus_return_val_if_fail (connection != NULL, FALSE);
724 _dbus_return_val_if_fail (name != NULL, FALSE);
725 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
726 _dbus_return_val_if_error_is_set (error, FALSE);
728 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
734 _DBUS_SET_OOM (error);
738 if (!dbus_message_append_args (message,
739 DBUS_TYPE_STRING, &name,
742 dbus_message_unref (message);
743 _DBUS_SET_OOM (error);
747 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
748 dbus_message_unref (message);
752 _DBUS_ASSERT_ERROR_IS_SET (error);
756 if (!dbus_message_get_args (reply, error,
757 DBUS_TYPE_BOOLEAN, &exists,
760 _DBUS_ASSERT_ERROR_IS_SET (error);
761 dbus_message_unref (reply);
765 dbus_message_unref (reply);
770 * Starts a service that will request ownership of the given name.
771 * The returned result will be one of be one of
772 * #DBUS_START_REPLY_SUCCESS or #DBUS_START_REPLY_ALREADY_RUNNING if
773 * successful. Pass #NULL if you don't care about the result.
775 * @param connection the connection
776 * @param name the name we want the new service to request
777 * @param flags the flags
778 * @param result a place to store the result or #NULL
779 * @param error location to store any errors
780 * @returns #TRUE if the activation succeeded, #FALSE if not
782 * @todo document what the flags do
785 dbus_bus_start_service_by_name (DBusConnection *connection,
788 dbus_uint32_t *result,
794 _dbus_return_val_if_fail (connection != NULL, FALSE);
795 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
797 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
800 "StartServiceByName");
802 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &name,
803 DBUS_TYPE_UINT32, &flags, DBUS_TYPE_INVALID))
805 dbus_message_unref (msg);
806 _DBUS_SET_OOM (error);
810 reply = dbus_connection_send_with_reply_and_block (connection, msg,
812 dbus_message_unref (msg);
816 _DBUS_ASSERT_ERROR_IS_SET (error);
820 if (dbus_set_error_from_message (error, reply))
822 _DBUS_ASSERT_ERROR_IS_SET (error);
823 dbus_message_unref (reply);
827 if (result != NULL &&
828 !dbus_message_get_args (reply, error, DBUS_TYPE_UINT32,
829 result, DBUS_TYPE_INVALID))
831 _DBUS_ASSERT_ERROR_IS_SET (error);
832 dbus_message_unref (reply);
836 dbus_message_unref (reply);
841 send_no_return_values (DBusConnection *connection,
847 /* Block to check success codepath */
850 reply = dbus_connection_send_with_reply_and_block (connection, msg,
854 _DBUS_ASSERT_ERROR_IS_SET (error);
856 dbus_message_unref (reply);
860 /* Silently-fail nonblocking codepath */
861 dbus_message_set_no_reply (msg, TRUE);
862 dbus_connection_send (connection, msg, NULL);
867 * Adds a match rule to match messages going through the message bus.
868 * The "rule" argument is the string form of a match rule.
870 * If you pass #NULL for the error, this function will not
871 * block; the match thus won't be added until you flush the
872 * connection, and if there's an error adding the match
873 * (only possible error is lack of resources in the bus),
874 * you won't find out about it.
876 * If you pass non-#NULL for the error this function will
877 * block until it gets a reply.
879 * Normal API conventions would have the function return
880 * a boolean value indicating whether the error was set,
881 * but that would require blocking always to determine
884 * @param connection connection to the message bus
885 * @param rule textual form of match rule
886 * @param error location to store any errors
889 dbus_bus_add_match (DBusConnection *connection,
895 _dbus_return_if_fail (rule != NULL);
897 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
904 _DBUS_SET_OOM (error);
908 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
911 dbus_message_unref (msg);
912 _DBUS_SET_OOM (error);
916 send_no_return_values (connection, msg, error);
918 dbus_message_unref (msg);
922 * Removes a previously-added match rule "by value" (the most
923 * recently-added identical rule gets removed). The "rule" argument
924 * is the string form of a match rule.
926 * If you pass #NULL for the error, this function will not
927 * block; otherwise it will. See detailed explanation in
928 * docs for dbus_bus_add_match().
930 * @param connection connection to the message bus
931 * @param rule textual form of match rule
932 * @param error location to store any errors
935 dbus_bus_remove_match (DBusConnection *connection,
941 _dbus_return_if_fail (rule != NULL);
943 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
948 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
951 dbus_message_unref (msg);
952 _DBUS_SET_OOM (error);
956 send_no_return_values (connection, msg, error);
958 dbus_message_unref (msg);