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.
61 DBusConnection *connection; /**< Connection we're associated with */
62 char *unique_name; /**< Unique name of this connection */
64 unsigned int is_well_known : 1; /**< Is one of the well-known connections in our global array */
67 /** The slot we have reserved to store BusData.
69 static dbus_int32_t bus_data_slot = -1;
71 /** Number of bus types */
74 static DBusConnection *bus_connections[N_BUS_TYPES];
75 static char *bus_connection_addresses[N_BUS_TYPES] = { NULL, NULL, NULL };
77 static DBusBusType activation_bus_type = DBUS_BUS_STARTER;
79 static dbus_bool_t initialized = FALSE;
82 * Lock for globals in this file
84 _DBUS_DEFINE_GLOBAL_LOCK (bus);
87 addresses_shutdown_func (void *data)
92 while (i < N_BUS_TYPES)
94 if (bus_connections[i] != NULL)
95 _dbus_warn ("dbus_shutdown() called but connections were still live!");
97 dbus_free (bus_connection_addresses[i]);
98 bus_connection_addresses[i] = NULL;
102 activation_bus_type = DBUS_BUS_STARTER;
106 get_from_env (char **connection_p,
111 _dbus_assert (*connection_p == NULL);
113 s = _dbus_getenv (env_var);
114 if (s == NULL || *s == '\0')
115 return TRUE; /* successfully didn't use the env var */
118 *connection_p = _dbus_strdup (s);
119 return *connection_p != NULL;
124 init_connections_unlocked (void)
132 while (i < N_BUS_TYPES)
134 bus_connections[i] = NULL;
138 /* Don't init these twice, we may run this code twice if
139 * init_connections_unlocked() fails midway through.
140 * In practice, each block below should contain only one
141 * "return FALSE" or running through twice may not
145 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
147 _dbus_verbose ("Filling in system bus address...\n");
149 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SYSTEM],
150 "DBUS_SYSTEM_BUS_ADDRESS"))
155 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
157 /* Use default system bus address if none set in environment */
158 bus_connection_addresses[DBUS_BUS_SYSTEM] =
159 _dbus_strdup (DBUS_SYSTEM_BUS_DEFAULT_ADDRESS);
160 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
163 _dbus_verbose (" used default system bus \"%s\"\n",
164 bus_connection_addresses[DBUS_BUS_SYSTEM]);
167 _dbus_verbose (" used env var system bus \"%s\"\n",
168 bus_connection_addresses[DBUS_BUS_SYSTEM]);
170 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
172 _dbus_verbose ("Filling in session bus address...\n");
174 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SESSION],
175 "DBUS_SESSION_BUS_ADDRESS"))
177 _dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_SESSION] ?
178 bus_connection_addresses[DBUS_BUS_SESSION] : "none set");
181 if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
183 _dbus_verbose ("Filling in activation bus address...\n");
185 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_STARTER],
186 "DBUS_STARTER_ADDRESS"))
189 _dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_STARTER] ?
190 bus_connection_addresses[DBUS_BUS_STARTER] : "none set");
194 if (bus_connection_addresses[DBUS_BUS_STARTER] != NULL)
196 s = _dbus_getenv ("DBUS_STARTER_BUS_TYPE");
200 _dbus_verbose ("Bus activation type was set to \"%s\"\n", s);
202 if (strcmp (s, "system") == 0)
203 activation_bus_type = DBUS_BUS_SYSTEM;
204 else if (strcmp (s, "session") == 0)
205 activation_bus_type = DBUS_BUS_SESSION;
210 /* Default to the session bus instead if available */
211 if (bus_connection_addresses[DBUS_BUS_SESSION] != NULL)
213 bus_connection_addresses[DBUS_BUS_STARTER] =
214 _dbus_strdup (bus_connection_addresses[DBUS_BUS_SESSION]);
215 if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
220 /* If we return FALSE we have to be sure that restarting
221 * the above code will work right
224 if (!_dbus_setenv ("DBUS_ACTIVATION_ADDRESS", NULL))
227 if (!_dbus_setenv ("DBUS_ACTIVATION_BUS_TYPE", NULL))
230 if (!_dbus_register_shutdown_func (addresses_shutdown_func,
241 bus_data_free (void *data)
245 if (bd->is_well_known)
249 /* We may be stored in more than one slot */
251 while (i < N_BUS_TYPES)
253 if (bus_connections[i] == bd->connection)
254 bus_connections[i] = NULL;
261 dbus_free (bd->unique_name);
264 dbus_connection_free_data_slot (&bus_data_slot);
268 ensure_bus_data (DBusConnection *connection)
272 if (!dbus_connection_allocate_data_slot (&bus_data_slot))
275 bd = dbus_connection_get_data (connection, bus_data_slot);
278 bd = dbus_new0 (BusData, 1);
281 dbus_connection_free_data_slot (&bus_data_slot);
285 bd->connection = connection;
287 if (!dbus_connection_set_data (connection, bus_data_slot, bd,
291 dbus_connection_free_data_slot (&bus_data_slot);
295 /* Data slot refcount now held by the BusData */
299 dbus_connection_free_data_slot (&bus_data_slot);
305 /** @} */ /* end of implementation details docs */
308 * @addtogroup DBusBus
313 * Connects to a bus daemon and registers the client with it. If a
314 * connection to the bus already exists, then that connection is
315 * returned. Caller owns a reference to the bus.
317 * @todo alex thinks we should nullify the connection when we get a disconnect-message.
319 * @param type bus type
320 * @param error address where an error can be returned.
321 * @returns a DBusConnection with new ref
324 dbus_bus_get (DBusBusType type,
328 DBusConnection *connection;
330 DBusBusType address_type;
332 _dbus_return_val_if_fail (type >= 0 && type < N_BUS_TYPES, NULL);
333 _dbus_return_val_if_error_is_set (error, NULL);
337 if (!init_connections_unlocked ())
340 _DBUS_SET_OOM (error);
344 /* We want to use the activation address even if the
345 * activating bus is the session or system bus,
350 /* Use the real type of the activation bus for getting its
351 * connection, but only if the real type's address is available. (If
352 * the activating bus isn't a well-known bus then
353 * activation_bus_type == DBUS_BUS_STARTER)
355 if (type == DBUS_BUS_STARTER &&
356 bus_connection_addresses[activation_bus_type] != NULL)
357 type = activation_bus_type;
359 if (bus_connections[type] != NULL)
361 connection = bus_connections[type];
362 dbus_connection_ref (connection);
368 address = bus_connection_addresses[address_type];
371 dbus_set_error (error, DBUS_ERROR_FAILED,
372 "Unable to determine the address of the message bus");
377 connection = dbus_connection_open (address, error);
381 _DBUS_ASSERT_ERROR_IS_SET (error);
386 /* By default we're bound to the lifecycle of
389 dbus_connection_set_exit_on_disconnect (connection,
392 if (!dbus_bus_register (connection, error))
394 _DBUS_ASSERT_ERROR_IS_SET (error);
395 dbus_connection_close (connection);
396 dbus_connection_unref (connection);
402 bus_connections[type] = connection;
403 bd = ensure_bus_data (connection);
404 _dbus_assert (bd != NULL);
406 bd->is_well_known = TRUE;
414 * Registers a connection with the bus. This must be the first
415 * thing an application does when connecting to the message bus.
416 * If registration succeeds, the unique name will be set,
417 * and can be obtained using dbus_bus_get_unique_name().
419 * @param connection the connection
420 * @param error place to store errors
421 * @returns #TRUE on success
424 dbus_bus_register (DBusConnection *connection,
427 DBusMessage *message, *reply;
432 _dbus_return_val_if_fail (connection != NULL, FALSE);
433 _dbus_return_val_if_error_is_set (error, FALSE);
437 bd = ensure_bus_data (connection);
440 _DBUS_SET_OOM (error);
444 if (bd->unique_name != NULL)
446 _dbus_warn ("Attempt to register the same DBusConnection with the message bus, but it is already registered\n");
447 /* This isn't an error, it's a programming bug. We'll be nice
448 * and not _dbus_assert_not_reached()
453 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
460 _DBUS_SET_OOM (error);
464 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
466 dbus_message_unref (message);
470 else if (dbus_set_error_from_message (error, reply))
472 else if (!dbus_message_get_args (reply, error,
473 DBUS_TYPE_STRING, &name,
477 bd->unique_name = _dbus_strdup (name);
478 if (bd->unique_name == NULL)
480 _DBUS_SET_OOM (error);
488 dbus_message_unref (reply);
491 _DBUS_ASSERT_ERROR_IS_SET (error);
498 * Sets the unique name of the connection. Can only be used if you
499 * registered with the bus manually (i.e. if you did not call
500 * dbus_bus_register()). Can only be called once per connection.
502 * @param connection the connection
503 * @param unique_name the unique name
504 * @returns #FALSE if not enough memory
507 dbus_bus_set_unique_name (DBusConnection *connection,
508 const char *unique_name)
512 _dbus_return_val_if_fail (connection != NULL, FALSE);
513 _dbus_return_val_if_fail (unique_name != NULL, FALSE);
515 bd = ensure_bus_data (connection);
519 _dbus_assert (bd->unique_name == NULL);
521 bd->unique_name = _dbus_strdup (unique_name);
522 return bd->unique_name != NULL;
526 * Gets the unique name of the connection. Only possible after the
527 * connection has been registered with the message bus.
529 * @param connection the connection
530 * @returns the unique name
533 dbus_bus_get_unique_name (DBusConnection *connection)
537 _dbus_return_val_if_fail (connection != NULL, NULL);
539 bd = ensure_bus_data (connection);
543 return bd->unique_name;
547 * Asks the bus to return the uid of the named
550 * @param connection the connection
551 * @param name a name owned by the connection
552 * @param error location to store the error
553 * @returns a result code, -1 if error is set
556 dbus_bus_get_unix_user (DBusConnection *connection,
560 DBusMessage *message, *reply;
563 _dbus_return_val_if_fail (connection != NULL, DBUS_UID_UNSET);
564 _dbus_return_val_if_fail (name != NULL, DBUS_UID_UNSET);
565 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), DBUS_UID_UNSET);
566 _dbus_return_val_if_error_is_set (error, DBUS_UID_UNSET);
568 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
571 "GetConnectionUnixUser");
575 _DBUS_SET_OOM (error);
576 return DBUS_UID_UNSET;
579 if (!dbus_message_append_args (message,
580 DBUS_TYPE_STRING, &name,
583 dbus_message_unref (message);
584 _DBUS_SET_OOM (error);
585 return DBUS_UID_UNSET;
588 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
591 dbus_message_unref (message);
595 _DBUS_ASSERT_ERROR_IS_SET (error);
596 return DBUS_UID_UNSET;
599 if (dbus_set_error_from_message (error, reply))
601 _DBUS_ASSERT_ERROR_IS_SET (error);
602 dbus_message_unref (reply);
603 return DBUS_UID_UNSET;
606 if (!dbus_message_get_args (reply, error,
607 DBUS_TYPE_UINT32, &uid,
610 _DBUS_ASSERT_ERROR_IS_SET (error);
611 dbus_message_unref (reply);
612 return DBUS_UID_UNSET;
615 dbus_message_unref (reply);
617 return (unsigned long) uid;
622 * Asks the bus to assign the given name to this connection by invoking
623 * the RequestName method on the bus. This method is fully documented
624 * in the D-BUS specification. For quick reference, the flags and
625 * result codes are discussed here, but the specification is the
626 * canonical version of this information.
628 * The #DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT flag indicates that
629 * if the name is successfully requested, other applications
630 * will not be able to take over the name. i.e. the name's
631 * owner (the application calling this function) must let go of
632 * the name, it will not lose it involuntarily.
634 * The #DBUS_NAME_FLAG_REPLACE_EXISTING flag indicates that the caller
635 * would like to take over the name from the current owner.
636 * If the current name owner used #DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT
637 * then this flag indicates that the caller would like to be placed
638 * in the queue to own the name when the current owner lets go.
640 * If no flags are given, an application will receive the requested
641 * name only if the name is currently unowned; and it will give
642 * up the name if another application asks to take it over using
643 * #DBUS_NAME_FLAG_REPLACE_EXISTING.
645 * This function returns a result code. The possible result codes
648 * #DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER means that the name had no
649 * existing owner, and the caller is now the primary owner; or that
650 * the name had an owner, and the caller specified
651 * #DBUS_NAME_FLAG_REPLACE_EXISTING, and the current owner did not
652 * specify #DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT.
654 * #DBUS_REQUEST_NAME_REPLY_IN_QUEUE happens only if the current owner
655 * specified #DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT and the caller specified
656 * #DBUS_NAME_FLAG_REPLACE_EXISTING. In this case the caller ends up in
657 * a queue to own the name after the current owner gives it up.
659 * #DBUS_REQUEST_NAME_REPLY_EXISTS happens if the name has an owner
660 * #already and DBUS_NAME_FLAG_REPLACE_EXISTING was not specified.
662 * #DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER happens if an application
663 * requests a name it already owns.
665 * When a service represents an application, say "text editor," then
666 * it should specify #DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT if it wants
667 * the first editor started to be the user's editor vs. the last one
668 * started. Then any editor that can be the user's editor should
669 * specify #DBUS_NAME_FLAG_REPLACE_EXISTING to either take over
670 * (last-started-wins) or be queued up (first-started-wins) according
671 * to whether #DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT was given.
673 * @todo this all seems sort of broken. Shouldn't the flags be a property
674 * of the name, not the app requesting the name? What are the use-cases
675 * other than the "text editor" thing and how are we supporting them?
677 * @param connection the connection
678 * @param name the name to request
680 * @param error location to store the error
681 * @returns a result code, -1 if error is set
684 dbus_bus_request_name (DBusConnection *connection,
689 DBusMessage *message, *reply;
690 dbus_uint32_t result;
692 _dbus_return_val_if_fail (connection != NULL, 0);
693 _dbus_return_val_if_fail (name != NULL, 0);
694 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
695 _dbus_return_val_if_error_is_set (error, 0);
697 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
704 _DBUS_SET_OOM (error);
708 if (!dbus_message_append_args (message,
709 DBUS_TYPE_STRING, &name,
710 DBUS_TYPE_UINT32, &flags,
713 dbus_message_unref (message);
714 _DBUS_SET_OOM (error);
718 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
721 dbus_message_unref (message);
725 _DBUS_ASSERT_ERROR_IS_SET (error);
729 if (dbus_set_error_from_message (error, reply))
731 _DBUS_ASSERT_ERROR_IS_SET (error);
732 dbus_message_unref (reply);
736 if (!dbus_message_get_args (reply, error,
737 DBUS_TYPE_UINT32, &result,
740 _DBUS_ASSERT_ERROR_IS_SET (error);
741 dbus_message_unref (reply);
745 dbus_message_unref (reply);
751 * Checks whether a certain name has an owner.
753 * @param connection the connection
754 * @param name the name
755 * @param error location to store any errors
756 * @returns #TRUE if the name exists, #FALSE if not or on error
759 dbus_bus_name_has_owner (DBusConnection *connection,
763 DBusMessage *message, *reply;
766 _dbus_return_val_if_fail (connection != NULL, FALSE);
767 _dbus_return_val_if_fail (name != NULL, FALSE);
768 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
769 _dbus_return_val_if_error_is_set (error, FALSE);
771 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
777 _DBUS_SET_OOM (error);
781 if (!dbus_message_append_args (message,
782 DBUS_TYPE_STRING, &name,
785 dbus_message_unref (message);
786 _DBUS_SET_OOM (error);
790 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
791 dbus_message_unref (message);
795 _DBUS_ASSERT_ERROR_IS_SET (error);
799 if (!dbus_message_get_args (reply, error,
800 DBUS_TYPE_BOOLEAN, &exists,
803 _DBUS_ASSERT_ERROR_IS_SET (error);
804 dbus_message_unref (reply);
808 dbus_message_unref (reply);
813 * Starts a service that will request ownership of the given name.
814 * The returned result will be one of be one of
815 * #DBUS_START_REPLY_SUCCESS or #DBUS_START_REPLY_ALREADY_RUNNING if
816 * successful. Pass #NULL if you don't care about the result.
818 * The flags parameter is for future expansion, currently you should
821 * @param connection the connection
822 * @param name the name we want the new service to request
823 * @param flags the flags (should always be 0 for now)
824 * @param result a place to store the result or #NULL
825 * @param error location to store any errors
826 * @returns #TRUE if the activation succeeded, #FALSE if not
829 dbus_bus_start_service_by_name (DBusConnection *connection,
832 dbus_uint32_t *result,
838 _dbus_return_val_if_fail (connection != NULL, FALSE);
839 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
841 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
844 "StartServiceByName");
846 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &name,
847 DBUS_TYPE_UINT32, &flags, DBUS_TYPE_INVALID))
849 dbus_message_unref (msg);
850 _DBUS_SET_OOM (error);
854 reply = dbus_connection_send_with_reply_and_block (connection, msg,
856 dbus_message_unref (msg);
860 _DBUS_ASSERT_ERROR_IS_SET (error);
864 if (dbus_set_error_from_message (error, reply))
866 _DBUS_ASSERT_ERROR_IS_SET (error);
867 dbus_message_unref (reply);
871 if (result != NULL &&
872 !dbus_message_get_args (reply, error, DBUS_TYPE_UINT32,
873 result, DBUS_TYPE_INVALID))
875 _DBUS_ASSERT_ERROR_IS_SET (error);
876 dbus_message_unref (reply);
880 dbus_message_unref (reply);
885 send_no_return_values (DBusConnection *connection,
891 /* Block to check success codepath */
894 reply = dbus_connection_send_with_reply_and_block (connection, msg,
898 _DBUS_ASSERT_ERROR_IS_SET (error);
900 dbus_message_unref (reply);
904 /* Silently-fail nonblocking codepath */
905 dbus_message_set_no_reply (msg, TRUE);
906 dbus_connection_send (connection, msg, NULL);
911 * Adds a match rule to match messages going through the message bus.
912 * The "rule" argument is the string form of a match rule.
914 * If you pass #NULL for the error, this function will not
915 * block; the match thus won't be added until you flush the
916 * connection, and if there's an error adding the match
917 * (only possible error is lack of resources in the bus),
918 * you won't find out about it.
920 * If you pass non-#NULL for the error this function will
921 * block until it gets a reply.
923 * Normal API conventions would have the function return
924 * a boolean value indicating whether the error was set,
925 * but that would require blocking always to determine
928 * @param connection connection to the message bus
929 * @param rule textual form of match rule
930 * @param error location to store any errors
933 dbus_bus_add_match (DBusConnection *connection,
939 _dbus_return_if_fail (rule != NULL);
941 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
948 _DBUS_SET_OOM (error);
952 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
955 dbus_message_unref (msg);
956 _DBUS_SET_OOM (error);
960 send_no_return_values (connection, msg, error);
962 dbus_message_unref (msg);
966 * Removes a previously-added match rule "by value" (the most
967 * recently-added identical rule gets removed). The "rule" argument
968 * is the string form of a match rule.
970 * If you pass #NULL for the error, this function will not
971 * block; otherwise it will. See detailed explanation in
972 * docs for dbus_bus_add_match().
974 * @param connection connection to the message bus
975 * @param rule textual form of match rule
976 * @param error location to store any errors
979 dbus_bus_remove_match (DBusConnection *connection,
985 _dbus_return_if_fail (rule != NULL);
987 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
992 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
995 dbus_message_unref (msg);
996 _DBUS_SET_OOM (error);
1000 send_no_return_values (connection, msg, error);
1002 dbus_message_unref (msg);