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 static DBusConnection *
306 internal_bus_get (DBusBusType type,
307 DBusError *error, dbus_bool_t private)
310 DBusConnection *connection;
312 DBusBusType address_type;
314 _dbus_return_val_if_fail (type >= 0 && type < N_BUS_TYPES, NULL);
315 _dbus_return_val_if_error_is_set (error, NULL);
319 if (!init_connections_unlocked ())
322 _DBUS_SET_OOM (error);
326 /* We want to use the activation address even if the
327 * activating bus is the session or system bus,
332 /* Use the real type of the activation bus for getting its
333 * connection, but only if the real type's address is available. (If
334 * the activating bus isn't a well-known bus then
335 * activation_bus_type == DBUS_BUS_STARTER)
337 if (type == DBUS_BUS_STARTER &&
338 bus_connection_addresses[activation_bus_type] != NULL)
339 type = activation_bus_type;
341 if (!private && bus_connections[type] != NULL)
343 connection = bus_connections[type];
344 dbus_connection_ref (connection);
350 address = bus_connection_addresses[address_type];
353 dbus_set_error (error, DBUS_ERROR_FAILED,
354 "Unable to determine the address of the message bus (try 'man dbus-launch' and 'man dbus-daemon' for help)");
360 connection = dbus_connection_open_private(address, error);
362 connection = dbus_connection_open (address, error);
366 _DBUS_ASSERT_ERROR_IS_SET (error);
371 /* By default we're bound to the lifecycle of
374 dbus_connection_set_exit_on_disconnect (connection,
377 if (!dbus_bus_register (connection, error))
379 _DBUS_ASSERT_ERROR_IS_SET (error);
380 dbus_connection_close (connection);
381 dbus_connection_unref (connection);
388 bus_connections[type] = connection;
390 bd = ensure_bus_data (connection);
391 _dbus_assert (bd != NULL);
393 bd->is_well_known = TRUE;
400 /** @} */ /* end of implementation details docs */
403 * @addtogroup DBusBus
408 * Connects to a bus daemon and registers the client with it. If a
409 * connection to the bus already exists, then that connection is
410 * returned. Caller owns a reference to the bus.
412 * @todo alex thinks we should nullify the connection when we get a disconnect-message.
414 * @param type bus type
415 * @param error address where an error can be returned.
416 * @returns a DBusConnection with new ref
419 dbus_bus_get (DBusBusType type,
421 return internal_bus_get(type, error, FALSE);
425 * Connects to a bus daemon and registers the client with it. Unlike
426 * dbus_bus_get(), always creates a new connection. This connection
427 * will not be saved or recycled by libdbus. Caller owns a reference
430 * @param type bus type
431 * @param error address where an error can be returned.
432 * @returns a DBusConnection with new ref
435 dbus_bus_get_private (DBusBusType type,
437 return internal_bus_get(type, error, TRUE);
441 * Registers a connection with the bus. This must be the first
442 * thing an application does when connecting to the message bus.
443 * If registration succeeds, the unique name will be set,
444 * and can be obtained using dbus_bus_get_unique_name().
446 * @param connection the connection
447 * @param error place to store errors
448 * @returns #TRUE on success
451 dbus_bus_register (DBusConnection *connection,
454 DBusMessage *message, *reply;
459 _dbus_return_val_if_fail (connection != NULL, FALSE);
460 _dbus_return_val_if_error_is_set (error, FALSE);
464 bd = ensure_bus_data (connection);
467 _DBUS_SET_OOM (error);
471 if (bd->unique_name != NULL)
473 _dbus_warn ("Attempt to register the same DBusConnection with the message bus, but it is already registered\n");
474 /* This isn't an error, it's a programming bug. We'll be nice
475 * and not _dbus_assert_not_reached()
480 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
487 _DBUS_SET_OOM (error);
491 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
493 dbus_message_unref (message);
497 else if (dbus_set_error_from_message (error, reply))
499 else if (!dbus_message_get_args (reply, error,
500 DBUS_TYPE_STRING, &name,
504 bd->unique_name = _dbus_strdup (name);
505 if (bd->unique_name == NULL)
507 _DBUS_SET_OOM (error);
515 dbus_message_unref (reply);
518 _DBUS_ASSERT_ERROR_IS_SET (error);
525 * Sets the unique name of the connection. Can only be used if you
526 * registered with the bus manually (i.e. if you did not call
527 * dbus_bus_register()). Can only be called once per connection.
529 * @param connection the connection
530 * @param unique_name the unique name
531 * @returns #FALSE if not enough memory
534 dbus_bus_set_unique_name (DBusConnection *connection,
535 const char *unique_name)
539 _dbus_return_val_if_fail (connection != NULL, FALSE);
540 _dbus_return_val_if_fail (unique_name != NULL, FALSE);
542 bd = ensure_bus_data (connection);
546 _dbus_assert (bd->unique_name == NULL);
548 bd->unique_name = _dbus_strdup (unique_name);
549 return bd->unique_name != NULL;
553 * Gets the unique name of the connection. Only possible after the
554 * connection has been registered with the message bus.
556 * The name remains valid for the duration of the connection and
557 * should not be freed by the caller.
559 * @param connection the connection
560 * @returns the unique name or NULL on error
563 dbus_bus_get_unique_name (DBusConnection *connection)
567 _dbus_return_val_if_fail (connection != NULL, NULL);
569 bd = ensure_bus_data (connection);
573 return bd->unique_name;
577 * Asks the bus to return the uid of the named
580 * @param connection the connection
581 * @param name a name owned by the connection
582 * @param error location to store the error
583 * @returns a result code, -1 if error is set
586 dbus_bus_get_unix_user (DBusConnection *connection,
590 DBusMessage *message, *reply;
593 _dbus_return_val_if_fail (connection != NULL, DBUS_UID_UNSET);
594 _dbus_return_val_if_fail (name != NULL, DBUS_UID_UNSET);
595 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), DBUS_UID_UNSET);
596 _dbus_return_val_if_error_is_set (error, DBUS_UID_UNSET);
598 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
601 "GetConnectionUnixUser");
605 _DBUS_SET_OOM (error);
606 return DBUS_UID_UNSET;
609 if (!dbus_message_append_args (message,
610 DBUS_TYPE_STRING, &name,
613 dbus_message_unref (message);
614 _DBUS_SET_OOM (error);
615 return DBUS_UID_UNSET;
618 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
621 dbus_message_unref (message);
625 _DBUS_ASSERT_ERROR_IS_SET (error);
626 return DBUS_UID_UNSET;
629 if (dbus_set_error_from_message (error, reply))
631 _DBUS_ASSERT_ERROR_IS_SET (error);
632 dbus_message_unref (reply);
633 return DBUS_UID_UNSET;
636 if (!dbus_message_get_args (reply, error,
637 DBUS_TYPE_UINT32, &uid,
640 _DBUS_ASSERT_ERROR_IS_SET (error);
641 dbus_message_unref (reply);
642 return DBUS_UID_UNSET;
645 dbus_message_unref (reply);
647 return (unsigned long) uid;
652 * Asks the bus to assign the given name to this connection by invoking
653 * the RequestName method on the bus. This method is fully documented
654 * in the D-BUS specification. For quick reference, the flags and
655 * result codes are discussed here, but the specification is the
656 * canonical version of this information.
658 * The #DBUS_NAME_FLAG_ALLOW_REPLACEMENT flag indicates that the caller
659 * will allow other services to take over the name from the current owner.
661 * The #DBUS_NAME_FLAG_REPLACE_EXISTING flag indicates that the caller
662 * would like to take over the name from the current owner.
663 * If the current name owner did not use #DBUS_NAME_FLAG_ALLOW_REPLACEMENT
664 * then this flag indicates that the caller would like to be placed
665 * in the queue to own the name when the current owner lets go.
667 * If no flags are given, an application will receive the requested
668 * name only if the name is currently unowned; it will NOT give
669 * up the name if another application asks to take it over using
670 * #DBUS_NAME_FLAG_REPLACE_EXISTING.
672 * This function returns a result code. The possible result codes
675 * #DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER means that the name had no
676 * existing owner, and the caller is now the primary owner; or that
677 * the name had an owner, and the caller specified
678 * #DBUS_NAME_FLAG_REPLACE_EXISTING, and the current owner
679 * specified #DBUS_NAME_FLAG_ALLOW_REPLACEMENT.
681 * #DBUS_REQUEST_NAME_REPLY_IN_QUEUE happens only if the caller does NOT
682 * specify #DBUS_NAME_FLAG_DO_NOT_QUEUE and either the current owner
683 * did NOT specify #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or the caller did NOT
684 * specify #DBUS_NAME_FLAG_REPLACE_EXISTING. In this case the caller ends up
685 * in a queue to own the name after the current owner gives it up.
687 * #DBUS_REQUEST_NAME_REPLY_EXISTS happens if the name has an owner
688 * already and the caller specifies #DBUS_NAME_FLAG_DO_NOT_QUEUE
689 * and either the current owner has NOT specified
690 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or the caller did NOT specify
691 * #DBUS_NAME_FLAG_REPLACE_EXISTING.
693 * #DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER happens if an application
694 * requests a name it already owns.
696 * When a service represents an application, say "text editor," then
697 * it should specify #DBUS_NAME_FLAG_ALLOW_REPLACEMENT if it wants
698 * the last editor started to be the user's editor vs. the first one
699 * started. Then any editor that can be the user's editor should
700 * specify #DBUS_NAME_FLAG_REPLACE_EXISTING to either take over
701 * (last-started-wins) or be queued up (first-started-wins) according
702 * to whether #DBUS_NAME_FLAG_ALLOW_REPLACEMENT was given.
704 * @todo this all seems sort of broken. Shouldn't the flags be a property
705 * of the name, not the app requesting the name? What are the use-cases
706 * other than the "text editor" thing and how are we supporting them?
708 * @param connection the connection
709 * @param name the name to request
711 * @param error location to store the error
712 * @returns a result code, -1 if error is set
715 dbus_bus_request_name (DBusConnection *connection,
720 DBusMessage *message, *reply;
721 dbus_uint32_t result;
723 _dbus_return_val_if_fail (connection != NULL, 0);
724 _dbus_return_val_if_fail (name != NULL, 0);
725 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
726 _dbus_return_val_if_error_is_set (error, 0);
728 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
735 _DBUS_SET_OOM (error);
739 if (!dbus_message_append_args (message,
740 DBUS_TYPE_STRING, &name,
741 DBUS_TYPE_UINT32, &flags,
744 dbus_message_unref (message);
745 _DBUS_SET_OOM (error);
749 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
752 dbus_message_unref (message);
756 _DBUS_ASSERT_ERROR_IS_SET (error);
760 if (dbus_set_error_from_message (error, reply))
762 _DBUS_ASSERT_ERROR_IS_SET (error);
763 dbus_message_unref (reply);
767 if (!dbus_message_get_args (reply, error,
768 DBUS_TYPE_UINT32, &result,
771 _DBUS_ASSERT_ERROR_IS_SET (error);
772 dbus_message_unref (reply);
776 dbus_message_unref (reply);
783 * Asks the bus to unassign the given name to this connection by invoking
784 * the ReleaseName method on the bus. This method is fully documented
785 * in the D-BUS specification.
787 * @param connection the connection
788 * @param name the name to remove
789 * @param error location to store the error
790 * @returns a result code, -1 if error is set
793 dbus_bus_release_name (DBusConnection *connection,
797 DBusMessage *message, *reply;
798 dbus_uint32_t result;
800 _dbus_return_val_if_fail (connection != NULL, 0);
801 _dbus_return_val_if_fail (name != NULL, 0);
802 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
803 _dbus_return_val_if_error_is_set (error, 0);
805 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
812 _DBUS_SET_OOM (error);
816 if (!dbus_message_append_args (message,
817 DBUS_TYPE_STRING, &name,
820 dbus_message_unref (message);
821 _DBUS_SET_OOM (error);
825 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
828 dbus_message_unref (message);
832 _DBUS_ASSERT_ERROR_IS_SET (error);
836 if (dbus_set_error_from_message (error, reply))
838 _DBUS_ASSERT_ERROR_IS_SET (error);
839 dbus_message_unref (reply);
843 if (!dbus_message_get_args (reply, error,
844 DBUS_TYPE_UINT32, &result,
847 _DBUS_ASSERT_ERROR_IS_SET (error);
848 dbus_message_unref (reply);
852 dbus_message_unref (reply);
858 * Checks whether a certain name has an owner.
860 * @param connection the connection
861 * @param name the name
862 * @param error location to store any errors
863 * @returns #TRUE if the name exists, #FALSE if not or on error
866 dbus_bus_name_has_owner (DBusConnection *connection,
870 DBusMessage *message, *reply;
873 _dbus_return_val_if_fail (connection != NULL, FALSE);
874 _dbus_return_val_if_fail (name != NULL, FALSE);
875 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
876 _dbus_return_val_if_error_is_set (error, FALSE);
878 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
884 _DBUS_SET_OOM (error);
888 if (!dbus_message_append_args (message,
889 DBUS_TYPE_STRING, &name,
892 dbus_message_unref (message);
893 _DBUS_SET_OOM (error);
897 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
898 dbus_message_unref (message);
902 _DBUS_ASSERT_ERROR_IS_SET (error);
906 if (!dbus_message_get_args (reply, error,
907 DBUS_TYPE_BOOLEAN, &exists,
910 _DBUS_ASSERT_ERROR_IS_SET (error);
911 dbus_message_unref (reply);
915 dbus_message_unref (reply);
920 * Starts a service that will request ownership of the given name.
921 * The returned result will be one of be one of
922 * #DBUS_START_REPLY_SUCCESS or #DBUS_START_REPLY_ALREADY_RUNNING if
923 * successful. Pass #NULL if you don't care about the result.
925 * The flags parameter is for future expansion, currently you should
928 * @param connection the connection
929 * @param name the name we want the new service to request
930 * @param flags the flags (should always be 0 for now)
931 * @param result a place to store the result or #NULL
932 * @param error location to store any errors
933 * @returns #TRUE if the activation succeeded, #FALSE if not
936 dbus_bus_start_service_by_name (DBusConnection *connection,
939 dbus_uint32_t *result,
945 _dbus_return_val_if_fail (connection != NULL, FALSE);
946 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
948 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
951 "StartServiceByName");
953 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &name,
954 DBUS_TYPE_UINT32, &flags, DBUS_TYPE_INVALID))
956 dbus_message_unref (msg);
957 _DBUS_SET_OOM (error);
961 reply = dbus_connection_send_with_reply_and_block (connection, msg,
963 dbus_message_unref (msg);
967 _DBUS_ASSERT_ERROR_IS_SET (error);
971 if (dbus_set_error_from_message (error, reply))
973 _DBUS_ASSERT_ERROR_IS_SET (error);
974 dbus_message_unref (reply);
978 if (result != NULL &&
979 !dbus_message_get_args (reply, error, DBUS_TYPE_UINT32,
980 result, DBUS_TYPE_INVALID))
982 _DBUS_ASSERT_ERROR_IS_SET (error);
983 dbus_message_unref (reply);
987 dbus_message_unref (reply);
992 send_no_return_values (DBusConnection *connection,
998 /* Block to check success codepath */
1001 reply = dbus_connection_send_with_reply_and_block (connection, msg,
1005 _DBUS_ASSERT_ERROR_IS_SET (error);
1007 dbus_message_unref (reply);
1011 /* Silently-fail nonblocking codepath */
1012 dbus_message_set_no_reply (msg, TRUE);
1013 dbus_connection_send (connection, msg, NULL);
1018 * Adds a match rule to match messages going through the message bus.
1019 * The "rule" argument is the string form of a match rule.
1021 * If you pass #NULL for the error, this function will not
1022 * block; the match thus won't be added until you flush the
1023 * connection, and if there's an error adding the match
1024 * (only possible error is lack of resources in the bus),
1025 * you won't find out about it.
1027 * If you pass non-#NULL for the error this function will
1028 * block until it gets a reply.
1030 * Normal API conventions would have the function return
1031 * a boolean value indicating whether the error was set,
1032 * but that would require blocking always to determine
1035 * The AddMatch method is fully documented in the D-BUS
1036 * specification. For quick reference, the format of the
1037 * match rules is discussed here, but the specification
1038 * is the canonical version of this information.
1040 * Rules are specified as a string of comma separated
1041 * key/value pairs. An example is
1042 * "type='signal',sender='org.freedesktop.DBus',
1043 * interface='org.freedesktop.DBus',member='Foo',
1044 * path='/bar/foo',destination=':452345.34'"
1046 * Possible keys you can match on are type, sender,
1047 * interface, member, path, destination and the special
1048 * arg keys. Excluding a key from the rule indicates
1049 * a wildcard match. For instance excluding the
1050 * the member from a match rule but adding a sender would
1051 * let all messages from that sender through.
1053 * Matches are inclusive not exclusive so as long as one
1054 * rule matches the message will get through. It is important
1055 * to note this because every time a message is received the
1056 * application will be paged into memory to process it. This
1057 * can cause performance problems such as draining batteries
1058 * on embedded platforms.
1060 * The special arg keys are used for further restricting the
1061 * match based on the parameters sent by the signal or method.
1062 * For instance arg1='foo' will check the first argument,
1063 * arg2='bar' the second and so on. For performance reasons
1064 * there is a set limit on the highest number parameter that
1065 * can be checked which is set in dbus-protocol.h
1067 * @param connection connection to the message bus
1068 * @param rule textual form of match rule
1069 * @param error location to store any errors
1072 dbus_bus_add_match (DBusConnection *connection,
1078 _dbus_return_if_fail (rule != NULL);
1080 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1082 DBUS_INTERFACE_DBUS,
1087 _DBUS_SET_OOM (error);
1091 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
1094 dbus_message_unref (msg);
1095 _DBUS_SET_OOM (error);
1099 send_no_return_values (connection, msg, error);
1101 dbus_message_unref (msg);
1105 * Removes a previously-added match rule "by value" (the most
1106 * recently-added identical rule gets removed). The "rule" argument
1107 * is the string form of a match rule.
1109 * If you pass #NULL for the error, this function will not
1110 * block; otherwise it will. See detailed explanation in
1111 * docs for dbus_bus_add_match().
1113 * @param connection connection to the message bus
1114 * @param rule textual form of match rule
1115 * @param error location to store any errors
1118 dbus_bus_remove_match (DBusConnection *connection,
1124 _dbus_return_if_fail (rule != NULL);
1126 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1128 DBUS_INTERFACE_DBUS,
1131 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
1134 dbus_message_unref (msg);
1135 _DBUS_SET_OOM (error);
1139 send_no_return_values (connection, msg, error);
1141 dbus_message_unref (msg);
1144 #ifdef DBUS_BUILD_TESTS
1146 dbus_bus_connection_get_unique_name (DBusConnection *connection)
1149 bd = dbus_connection_get_data (connection, bus_data_slot);
1151 return bd->unique_name;
1153 #endif /* DBUS_BUILD_TESTS */