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.0
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"
32 * @defgroup DBusBus Message bus APIs
34 * @brief Functions for communicating with the message bus
36 * @todo right now the default address of the system bus is hardcoded,
37 * so if you change it in the global config file suddenly you have to
38 * set DBUS_SYSTEM_BUS_ADDRESS env variable. Might be nice if the
39 * client lib somehow read the config file, or if the bus on startup
40 * somehow wrote out its address to a well-known spot, but might also
45 * @defgroup DBusBusInternals Message bus APIs internals
46 * @ingroup DBusInternals
47 * @brief Internals of functions for communicating with the message bus
53 * Block of message-bus-related data we attach to each
54 * #DBusConnection used with these convenience functions.
57 * @todo get rid of most of these; they should be done
58 * with DBusGProxy and the Qt equivalent, i.e. the same
59 * way any other interface would be used.
63 DBusConnection *connection; /**< Connection we're associated with */
64 char *base_service; /**< Base service name of this connection */
66 unsigned int is_well_known : 1; /**< Is one of the well-known connections in our global array */
69 /** The slot we have reserved to store BusData.
71 static dbus_int32_t bus_data_slot = -1;
73 /** Number of bus types */
76 static DBusConnection *bus_connections[N_BUS_TYPES];
77 static char *bus_connection_addresses[N_BUS_TYPES] = { NULL, NULL, NULL };
79 static DBusBusType activation_bus_type = DBUS_BUS_ACTIVATION;
81 static dbus_bool_t initialized = FALSE;
84 * Lock for globals in this file
86 _DBUS_DEFINE_GLOBAL_LOCK (bus);
89 addresses_shutdown_func (void *data)
94 while (i < N_BUS_TYPES)
96 if (bus_connections[i] != NULL)
97 _dbus_warn ("dbus_shutdown() called but connections were still live!");
99 dbus_free (bus_connection_addresses[i]);
100 bus_connection_addresses[i] = NULL;
104 activation_bus_type = DBUS_BUS_ACTIVATION;
108 get_from_env (char **connection_p,
113 _dbus_assert (*connection_p == NULL);
115 s = _dbus_getenv (env_var);
116 if (s == NULL || *s == '\0')
117 return TRUE; /* successfully didn't use the env var */
120 *connection_p = _dbus_strdup (s);
121 return *connection_p != NULL;
126 init_connections_unlocked (void)
134 while (i < N_BUS_TYPES)
136 bus_connections[i] = NULL;
140 /* Don't init these twice, we may run this code twice if
141 * init_connections_unlocked() fails midway through.
142 * In practice, each block below should contain only one
143 * "return FALSE" or running through twice may not
147 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
149 _dbus_verbose ("Filling in system bus address...\n");
151 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SYSTEM],
152 "DBUS_SYSTEM_BUS_ADDRESS"))
157 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
159 /* Use default system bus address if none set in environment */
160 bus_connection_addresses[DBUS_BUS_SYSTEM] =
161 _dbus_strdup (DBUS_SYSTEM_BUS_DEFAULT_ADDRESS);
162 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
165 _dbus_verbose (" used default system bus \"%s\"\n",
166 bus_connection_addresses[DBUS_BUS_SYSTEM]);
169 _dbus_verbose (" used env var system bus \"%s\"\n",
170 bus_connection_addresses[DBUS_BUS_SYSTEM]);
172 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
174 _dbus_verbose ("Filling in session bus address...\n");
176 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SESSION],
177 "DBUS_SESSION_BUS_ADDRESS"))
179 _dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_SESSION] ?
180 bus_connection_addresses[DBUS_BUS_SESSION] : "none set");
183 if (bus_connection_addresses[DBUS_BUS_ACTIVATION] == NULL)
185 _dbus_verbose ("Filling in activation bus address...\n");
187 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_ACTIVATION],
188 "DBUS_ACTIVATION_ADDRESS"))
191 _dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_ACTIVATION] ?
192 bus_connection_addresses[DBUS_BUS_ACTIVATION] : "none set");
196 if (bus_connection_addresses[DBUS_BUS_ACTIVATION] != NULL)
198 s = _dbus_getenv ("DBUS_ACTIVATION_BUS_TYPE");
202 _dbus_verbose ("Bus activation type was set to \"%s\"\n", s);
204 if (strcmp (s, "system") == 0)
205 activation_bus_type = DBUS_BUS_SYSTEM;
206 else if (strcmp (s, "session") == 0)
207 activation_bus_type = DBUS_BUS_SESSION;
212 /* Default to the session bus instead if available */
213 if (bus_connection_addresses[DBUS_BUS_SESSION] != NULL)
215 bus_connection_addresses[DBUS_BUS_ACTIVATION] =
216 _dbus_strdup (bus_connection_addresses[DBUS_BUS_SESSION]);
217 if (bus_connection_addresses[DBUS_BUS_ACTIVATION] == NULL)
222 /* If we return FALSE we have to be sure that restarting
223 * the above code will work right
226 if (!_dbus_setenv ("DBUS_ACTIVATION_ADDRESS", NULL))
229 if (!_dbus_setenv ("DBUS_ACTIVATION_BUS_TYPE", NULL))
232 if (!_dbus_register_shutdown_func (addresses_shutdown_func,
243 bus_data_free (void *data)
247 if (bd->is_well_known)
251 /* We may be stored in more than one slot */
253 while (i < N_BUS_TYPES)
255 if (bus_connections[i] == bd->connection)
256 bus_connections[i] = NULL;
263 dbus_free (bd->base_service);
266 dbus_connection_free_data_slot (&bus_data_slot);
270 ensure_bus_data (DBusConnection *connection)
274 if (!dbus_connection_allocate_data_slot (&bus_data_slot))
277 bd = dbus_connection_get_data (connection, bus_data_slot);
280 bd = dbus_new0 (BusData, 1);
283 dbus_connection_free_data_slot (&bus_data_slot);
287 bd->connection = connection;
289 if (!dbus_connection_set_data (connection, bus_data_slot, bd,
293 dbus_connection_free_data_slot (&bus_data_slot);
297 /* Data slot refcount now held by the BusData */
301 dbus_connection_free_data_slot (&bus_data_slot);
307 /** @} */ /* end of implementation details docs */
310 * @addtogroup DBusBus
315 * Connects to a bus daemon and registers the client with it.
316 * If a connection to the bus already exists, then that connection is returned.
318 * @todo alex thinks we should nullify the connection when we get a disconnect-message.
320 * @param type bus type
321 * @param error address where an error can be returned.
322 * @returns a DBusConnection
325 dbus_bus_get (DBusBusType type,
329 DBusConnection *connection;
331 DBusBusType address_type;
333 _dbus_return_val_if_fail (type >= 0 && type < N_BUS_TYPES, NULL);
334 _dbus_return_val_if_error_is_set (error, NULL);
338 if (!init_connections_unlocked ())
341 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
345 /* We want to use the activation address even if the
346 * activating bus is the session or system bus,
351 /* Use the real type of the activation bus for getting its
352 * connection, but only if the real type's address is available. (If
353 * the activating bus isn't a well-known bus then
354 * activation_bus_type == DBUS_BUS_ACTIVATION)
356 if (type == DBUS_BUS_ACTIVATION &&
357 bus_connection_addresses[activation_bus_type] != NULL)
358 type = activation_bus_type;
360 if (bus_connections[type] != NULL)
362 connection = bus_connections[type];
363 dbus_connection_ref (connection);
369 address = bus_connection_addresses[address_type];
372 dbus_set_error (error, DBUS_ERROR_FAILED,
373 "Unable to determine the address of the message bus");
378 connection = dbus_connection_open (address, error);
382 _DBUS_ASSERT_ERROR_IS_SET (error);
387 /* By default we're bound to the lifecycle of
390 dbus_connection_set_exit_on_disconnect (connection,
393 if (!dbus_bus_register (connection, error))
395 _DBUS_ASSERT_ERROR_IS_SET (error);
396 dbus_connection_disconnect (connection);
397 dbus_connection_unref (connection);
403 bus_connections[type] = connection;
404 bd = ensure_bus_data (connection);
405 _dbus_assert (bd != NULL);
407 bd->is_well_known = TRUE;
415 * Registers a connection with the bus. This must be the first
416 * thing an application does when connecting to the message bus.
417 * If registration succeeds, the base service name will be set,
418 * and can be obtained using dbus_bus_get_base_service().
420 * @todo if we get an error reply, it has to be converted into
421 * DBusError and returned
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->base_service != 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_ORG_FREEDESKTOP_DBUS,
458 DBUS_PATH_ORG_FREEDESKTOP_DBUS,
459 DBUS_INTERFACE_ORG_FREEDESKTOP_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->base_service = name;
487 dbus_message_unref (reply);
490 _DBUS_ASSERT_ERROR_IS_SET (error);
497 * Sets the base service name of the connection.
498 * Can only be used if you registered with the
499 * bus manually (i.e. if you did not call
500 * dbus_bus_register()). Can only be called
501 * once per connection.
503 * @param connection the connection
504 * @param base_service the base service name
505 * @returns #FALSE if not enough memory
508 dbus_bus_set_base_service (DBusConnection *connection,
509 const char *base_service)
513 _dbus_return_val_if_fail (connection != NULL, FALSE);
514 _dbus_return_val_if_fail (base_service != NULL, FALSE);
516 bd = ensure_bus_data (connection);
520 _dbus_assert (bd->base_service == NULL);
522 bd->base_service = _dbus_strdup (base_service);
523 return bd->base_service != NULL;
527 * Gets the base service name of the connection.
528 * Only possible after the connection has been registered
529 * with the message bus.
531 * @param connection the connection
532 * @returns the base service name
535 dbus_bus_get_base_service (DBusConnection *connection)
539 _dbus_return_val_if_fail (connection != NULL, NULL);
541 bd = ensure_bus_data (connection);
545 return bd->base_service;
549 * Asks the bus to try to acquire a certain service.
551 * @todo these docs are not complete, need to document the
552 * return value and flags
554 * @todo if we get an error reply, it has to be converted into
555 * DBusError and returned
557 * @param connection the connection
558 * @param service_name the service name
560 * @param error location to store the error
561 * @returns a result code, -1 if error is set
564 dbus_bus_acquire_service (DBusConnection *connection,
565 const char *service_name,
569 DBusMessage *message, *reply;
570 dbus_uint32_t service_result;
572 _dbus_return_val_if_fail (connection != NULL, 0);
573 _dbus_return_val_if_fail (service_name != NULL, 0);
574 _dbus_return_val_if_error_is_set (error, 0);
576 message = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
577 DBUS_PATH_ORG_FREEDESKTOP_DBUS,
578 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
583 _DBUS_SET_OOM (error);
587 if (!dbus_message_append_args (message,
588 DBUS_TYPE_STRING, service_name,
589 DBUS_TYPE_UINT32, flags,
592 dbus_message_unref (message);
593 _DBUS_SET_OOM (error);
597 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
600 dbus_message_unref (message);
604 _DBUS_ASSERT_ERROR_IS_SET (error);
608 if (dbus_set_error_from_message (error, reply))
610 _DBUS_ASSERT_ERROR_IS_SET (error);
611 dbus_message_unref (reply);
615 if (!dbus_message_get_args (reply, error,
616 DBUS_TYPE_UINT32, &service_result,
619 _DBUS_ASSERT_ERROR_IS_SET (error);
620 dbus_message_unref (reply);
624 dbus_message_unref (reply);
626 return service_result;
630 * Checks whether a certain service exists.
632 * @todo the SERVICE_EXISTS message should use BOOLEAN not UINT32
634 * @param connection the connection
635 * @param service_name the service name
636 * @param error location to store any errors
637 * @returns #TRUE if the service exists, #FALSE if not or on error
640 dbus_bus_service_exists (DBusConnection *connection,
641 const char *service_name,
644 DBusMessage *message, *reply;
647 _dbus_return_val_if_fail (connection != NULL, FALSE);
648 _dbus_return_val_if_fail (service_name != NULL, FALSE);
649 _dbus_return_val_if_error_is_set (error, FALSE);
651 message = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
652 DBUS_PATH_ORG_FREEDESKTOP_DBUS,
653 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
657 _DBUS_SET_OOM (error);
661 if (!dbus_message_append_args (message,
662 DBUS_TYPE_STRING, service_name,
665 dbus_message_unref (message);
666 _DBUS_SET_OOM (error);
670 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
671 dbus_message_unref (message);
675 _DBUS_ASSERT_ERROR_IS_SET (error);
679 if (!dbus_message_get_args (reply, error,
680 DBUS_TYPE_UINT32, &exists,
683 _DBUS_ASSERT_ERROR_IS_SET (error);
687 return (exists != FALSE);
691 * Activates a given service
693 * @param connection the connection
694 * @param service_name the service name
695 * @param flags the flags
696 * @param result a place to store the result of the activation, which will
697 * be one of DBUS_ACTIVATION_REPLY_ACTIVATED or
698 * DBUS_ACTIVATION_REPLY_ALREADY_ACTIVE if successful. Pass NULL if you
699 * don't care about the result.
700 * @param error location to store any errors
701 * @returns #TRUE if the activation succeeded, #FALSE if not
703 * @todo document what the flags do
706 dbus_bus_activate_service (DBusConnection *connection,
707 const char *service_name,
709 dbus_uint32_t *result,
715 msg = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
716 DBUS_PATH_ORG_FREEDESKTOP_DBUS,
717 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
720 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, service_name,
721 DBUS_TYPE_UINT32, flags, DBUS_TYPE_INVALID))
723 dbus_message_unref (msg);
724 _DBUS_SET_OOM (error);
728 reply = dbus_connection_send_with_reply_and_block (connection, msg,
730 dbus_message_unref (msg);
734 _DBUS_ASSERT_ERROR_IS_SET (error);
738 if (dbus_set_error_from_message (error, reply))
740 _DBUS_ASSERT_ERROR_IS_SET (error);
741 dbus_message_unref (reply);
745 if (result != NULL &&
746 !dbus_message_get_args (reply, error, DBUS_TYPE_UINT32,
747 result, DBUS_TYPE_INVALID))
749 _DBUS_ASSERT_ERROR_IS_SET (error);
750 dbus_message_unref (reply);
754 dbus_message_unref (reply);
759 send_no_return_values (DBusConnection *connection,
765 /* Block to check success codepath */
768 reply = dbus_connection_send_with_reply_and_block (connection, msg,
773 _DBUS_ASSERT_ERROR_IS_SET (error);
777 if (dbus_set_error_from_message (error, reply))
779 _DBUS_ASSERT_ERROR_IS_SET (error);
780 dbus_message_unref (reply);
784 dbus_message_unref (reply);
788 /* Silently-fail nonblocking codepath */
789 if (!dbus_connection_send (connection, msg, NULL))
795 * Adds a match rule to match messages going through the message bus.
796 * The "rule" argument is the string form of a match rule.
798 * If you pass #NULL for the error, this function will not
799 * block; the match thus won't be added until you flush the
800 * connection, and if there's an error adding the match
801 * (only possible error is lack of resources in the bus),
802 * you won't find out about it.
804 * If you pass non-#NULL for the error this function will
805 * block until it gets a reply.
807 * Normal API conventions would have the function return
808 * a boolean value indicating whether the error was set,
809 * but that would require blocking always to determine
812 * @param connection connection to the message bus
813 * @param rule textual form of match rule
814 * @param error location to store any errors
817 dbus_bus_add_match (DBusConnection *connection,
823 msg = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
824 DBUS_PATH_ORG_FREEDESKTOP_DBUS,
825 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
828 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, rule,
831 dbus_message_unref (msg);
832 _DBUS_SET_OOM (error);
836 send_no_return_values (connection, msg, error);
838 dbus_message_unref (msg);
842 * Removes a previously-added match rule "by value" (the most
843 * recently-added identical rule gets removed). The "rule" argument
844 * is the string form of a match rule.
846 * If you pass #NULL for the error, this function will not
847 * block; otherwise it will. See detailed explanation in
848 * docs for dbus_bus_add_match().
850 * @param connection connection to the message bus
851 * @param rule textual form of match rule
852 * @param error location to store any errors
855 dbus_bus_remove_match (DBusConnection *connection,
861 msg = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
862 DBUS_PATH_ORG_FREEDESKTOP_DBUS,
863 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
866 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, rule,
869 dbus_message_unref (msg);
870 _DBUS_SET_OOM (error);
874 send_no_return_values (connection, msg, error);
876 dbus_message_unref (msg);