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 /* internal function that checks to see if this
306 is a shared bus connection and if it is unref it */
308 _dbus_bus_check_connection_and_unref (DBusConnection *connection)
310 if (bus_connections[DBUS_BUS_SYSTEM] == connection)
312 bus_connections[DBUS_BUS_SYSTEM] = NULL;
313 dbus_connection_unref (connection);
315 else if (bus_connections[DBUS_BUS_SESSION] == connection)
317 bus_connections[DBUS_BUS_SESSION] = NULL;
318 dbus_connection_unref (connection);
322 static DBusConnection *
323 internal_bus_get (DBusBusType type,
324 DBusError *error, dbus_bool_t private)
327 DBusConnection *connection;
329 DBusBusType address_type;
331 _dbus_return_val_if_fail (type >= 0 && type < N_BUS_TYPES, NULL);
332 _dbus_return_val_if_error_is_set (error, NULL);
336 if (!init_connections_unlocked ())
339 _DBUS_SET_OOM (error);
343 /* We want to use the activation address even if the
344 * activating bus is the session or system bus,
349 /* Use the real type of the activation bus for getting its
350 * connection, but only if the real type's address is available. (If
351 * the activating bus isn't a well-known bus then
352 * activation_bus_type == DBUS_BUS_STARTER)
354 if (type == DBUS_BUS_STARTER &&
355 bus_connection_addresses[activation_bus_type] != NULL)
356 type = activation_bus_type;
358 if (!private && bus_connections[type] != NULL)
360 connection = bus_connections[type];
361 dbus_connection_ref (connection);
367 address = bus_connection_addresses[address_type];
370 dbus_set_error (error, DBUS_ERROR_FAILED,
371 "Unable to determine the address of the message bus (try 'man dbus-launch' and 'man dbus-daemon' for help)");
377 connection = dbus_connection_open_private(address, error);
379 connection = dbus_connection_open (address, error);
383 _DBUS_ASSERT_ERROR_IS_SET (error);
388 /* By default we're bound to the lifecycle of
391 dbus_connection_set_exit_on_disconnect (connection,
394 if (!dbus_bus_register (connection, error))
396 _DBUS_ASSERT_ERROR_IS_SET (error);
397 dbus_connection_close (connection);
398 dbus_connection_unref (connection);
406 /* get a hard ref to the connection */
407 bus_connections[type] = connection;
408 dbus_connection_ref (bus_connections[type]);
411 bd = ensure_bus_data (connection);
412 _dbus_assert (bd != NULL);
414 bd->is_well_known = TRUE;
421 /** @} */ /* end of implementation details docs */
424 * @addtogroup DBusBus
429 * Connects to a bus daemon and registers the client with it. If a
430 * connection to the bus already exists, then that connection is
431 * returned. Caller owns a reference to the bus.
433 * @param type bus type
434 * @param error address where an error can be returned.
435 * @returns a DBusConnection with new ref
438 dbus_bus_get (DBusBusType type,
440 return internal_bus_get(type, error, FALSE);
444 * Connects to a bus daemon and registers the client with it. Unlike
445 * dbus_bus_get(), always creates a new connection. This connection
446 * will not be saved or recycled by libdbus. Caller owns a reference
449 * @param type bus type
450 * @param error address where an error can be returned.
451 * @returns a DBusConnection with new ref
454 dbus_bus_get_private (DBusBusType type,
456 return internal_bus_get(type, error, TRUE);
460 * Registers a connection with the bus. This must be the first
461 * thing an application does when connecting to the message bus.
462 * If registration succeeds, the unique name will be set,
463 * and can be obtained using dbus_bus_get_unique_name().
465 * @param connection the connection
466 * @param error place to store errors
467 * @returns #TRUE on success
470 dbus_bus_register (DBusConnection *connection,
473 DBusMessage *message, *reply;
478 _dbus_return_val_if_fail (connection != NULL, FALSE);
479 _dbus_return_val_if_error_is_set (error, FALSE);
483 bd = ensure_bus_data (connection);
486 _DBUS_SET_OOM (error);
490 if (bd->unique_name != NULL)
492 _dbus_warn ("Attempt to register the same DBusConnection with the message bus, but it is already registered\n");
493 /* This isn't an error, it's a programming bug. We'll be nice
494 * and not _dbus_assert_not_reached()
499 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
506 _DBUS_SET_OOM (error);
510 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
512 dbus_message_unref (message);
516 else if (dbus_set_error_from_message (error, reply))
518 else if (!dbus_message_get_args (reply, error,
519 DBUS_TYPE_STRING, &name,
523 bd->unique_name = _dbus_strdup (name);
524 if (bd->unique_name == NULL)
526 _DBUS_SET_OOM (error);
534 dbus_message_unref (reply);
537 _DBUS_ASSERT_ERROR_IS_SET (error);
544 * Sets the unique name of the connection. Can only be used if you
545 * registered with the bus manually (i.e. if you did not call
546 * dbus_bus_register()). Can only be called once per connection.
548 * @param connection the connection
549 * @param unique_name the unique name
550 * @returns #FALSE if not enough memory
553 dbus_bus_set_unique_name (DBusConnection *connection,
554 const char *unique_name)
558 _dbus_return_val_if_fail (connection != NULL, FALSE);
559 _dbus_return_val_if_fail (unique_name != NULL, FALSE);
561 bd = ensure_bus_data (connection);
565 _dbus_assert (bd->unique_name == NULL);
567 bd->unique_name = _dbus_strdup (unique_name);
568 return bd->unique_name != NULL;
572 * Gets the unique name of the connection. Only possible after the
573 * connection has been registered with the message bus.
575 * The name remains valid for the duration of the connection and
576 * should not be freed by the caller.
578 * @param connection the connection
579 * @returns the unique name or NULL on error
582 dbus_bus_get_unique_name (DBusConnection *connection)
586 _dbus_return_val_if_fail (connection != NULL, NULL);
588 bd = ensure_bus_data (connection);
592 return bd->unique_name;
596 * Asks the bus to return the uid of the named
599 * @param connection the connection
600 * @param name a name owned by the connection
601 * @param error location to store the error
602 * @returns a result code, -1 if error is set
605 dbus_bus_get_unix_user (DBusConnection *connection,
609 DBusMessage *message, *reply;
612 _dbus_return_val_if_fail (connection != NULL, DBUS_UID_UNSET);
613 _dbus_return_val_if_fail (name != NULL, DBUS_UID_UNSET);
614 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), DBUS_UID_UNSET);
615 _dbus_return_val_if_error_is_set (error, DBUS_UID_UNSET);
617 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
620 "GetConnectionUnixUser");
624 _DBUS_SET_OOM (error);
625 return DBUS_UID_UNSET;
628 if (!dbus_message_append_args (message,
629 DBUS_TYPE_STRING, &name,
632 dbus_message_unref (message);
633 _DBUS_SET_OOM (error);
634 return DBUS_UID_UNSET;
637 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
640 dbus_message_unref (message);
644 _DBUS_ASSERT_ERROR_IS_SET (error);
645 return DBUS_UID_UNSET;
648 if (dbus_set_error_from_message (error, reply))
650 _DBUS_ASSERT_ERROR_IS_SET (error);
651 dbus_message_unref (reply);
652 return DBUS_UID_UNSET;
655 if (!dbus_message_get_args (reply, error,
656 DBUS_TYPE_UINT32, &uid,
659 _DBUS_ASSERT_ERROR_IS_SET (error);
660 dbus_message_unref (reply);
661 return DBUS_UID_UNSET;
664 dbus_message_unref (reply);
666 return (unsigned long) uid;
671 * Asks the bus to assign the given name to this connection by invoking
672 * the RequestName method on the bus. This method is fully documented
673 * in the D-Bus specification. For quick reference, the flags and
674 * result codes are discussed here, but the specification is the
675 * canonical version of this information.
677 * The #DBUS_NAME_FLAG_ALLOW_REPLACEMENT flag indicates that the caller
678 * will allow other services to take over the name from the current owner.
680 * The #DBUS_NAME_FLAG_REPLACE_EXISTING flag indicates that the caller
681 * would like to take over the name from the current owner.
682 * If the current name owner did not use #DBUS_NAME_FLAG_ALLOW_REPLACEMENT
683 * then this flag indicates that the caller would like to be placed
684 * in the queue to own the name when the current owner lets go.
686 * If no flags are given, an application will receive the requested
687 * name only if the name is currently unowned; it will NOT give
688 * up the name if another application asks to take it over using
689 * #DBUS_NAME_FLAG_REPLACE_EXISTING.
691 * This function returns a result code. The possible result codes
694 * #DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER means that the name had no
695 * existing owner, and the caller is now the primary owner; or that
696 * the name had an owner, and the caller specified
697 * #DBUS_NAME_FLAG_REPLACE_EXISTING, and the current owner
698 * specified #DBUS_NAME_FLAG_ALLOW_REPLACEMENT.
700 * #DBUS_REQUEST_NAME_REPLY_IN_QUEUE happens only if the caller does NOT
701 * specify #DBUS_NAME_FLAG_DO_NOT_QUEUE and either the current owner
702 * did NOT specify #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or the caller did NOT
703 * specify #DBUS_NAME_FLAG_REPLACE_EXISTING. In this case the caller ends up
704 * in a queue to own the name after the current owner gives it up.
706 * #DBUS_REQUEST_NAME_REPLY_EXISTS happens if the name has an owner
707 * already and the caller specifies #DBUS_NAME_FLAG_DO_NOT_QUEUE
708 * and either the current owner has NOT specified
709 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or the caller did NOT specify
710 * #DBUS_NAME_FLAG_REPLACE_EXISTING.
712 * #DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER happens if an application
713 * requests a name it already owns.
715 * When a service represents an application, say "text editor," then
716 * it should specify #DBUS_NAME_FLAG_ALLOW_REPLACEMENT if it wants
717 * the last editor started to be the user's editor vs. the first one
718 * started. Then any editor that can be the user's editor should
719 * specify #DBUS_NAME_FLAG_REPLACE_EXISTING to either take over
720 * (last-started-wins) or be queued up (first-started-wins) according
721 * to whether #DBUS_NAME_FLAG_ALLOW_REPLACEMENT was given.
723 * @param connection the connection
724 * @param name the name to request
726 * @param error location to store the error
727 * @returns a result code, -1 if error is set
730 dbus_bus_request_name (DBusConnection *connection,
735 DBusMessage *message, *reply;
736 dbus_uint32_t result;
738 _dbus_return_val_if_fail (connection != NULL, 0);
739 _dbus_return_val_if_fail (name != NULL, 0);
740 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
741 _dbus_return_val_if_error_is_set (error, 0);
743 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
750 _DBUS_SET_OOM (error);
754 if (!dbus_message_append_args (message,
755 DBUS_TYPE_STRING, &name,
756 DBUS_TYPE_UINT32, &flags,
759 dbus_message_unref (message);
760 _DBUS_SET_OOM (error);
764 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
767 dbus_message_unref (message);
771 _DBUS_ASSERT_ERROR_IS_SET (error);
775 if (dbus_set_error_from_message (error, reply))
777 _DBUS_ASSERT_ERROR_IS_SET (error);
778 dbus_message_unref (reply);
782 if (!dbus_message_get_args (reply, error,
783 DBUS_TYPE_UINT32, &result,
786 _DBUS_ASSERT_ERROR_IS_SET (error);
787 dbus_message_unref (reply);
791 dbus_message_unref (reply);
798 * Asks the bus to unassign the given name to this connection by invoking
799 * the ReleaseName method on the bus. This method is fully documented
800 * in the D-Bus specification.
802 * @param connection the connection
803 * @param name the name to remove
804 * @param error location to store the error
805 * @returns a result code, -1 if error is set
808 dbus_bus_release_name (DBusConnection *connection,
812 DBusMessage *message, *reply;
813 dbus_uint32_t result;
815 _dbus_return_val_if_fail (connection != NULL, 0);
816 _dbus_return_val_if_fail (name != NULL, 0);
817 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
818 _dbus_return_val_if_error_is_set (error, 0);
820 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
827 _DBUS_SET_OOM (error);
831 if (!dbus_message_append_args (message,
832 DBUS_TYPE_STRING, &name,
835 dbus_message_unref (message);
836 _DBUS_SET_OOM (error);
840 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
843 dbus_message_unref (message);
847 _DBUS_ASSERT_ERROR_IS_SET (error);
851 if (dbus_set_error_from_message (error, reply))
853 _DBUS_ASSERT_ERROR_IS_SET (error);
854 dbus_message_unref (reply);
858 if (!dbus_message_get_args (reply, error,
859 DBUS_TYPE_UINT32, &result,
862 _DBUS_ASSERT_ERROR_IS_SET (error);
863 dbus_message_unref (reply);
867 dbus_message_unref (reply);
873 * Checks whether a certain name has an owner.
875 * @param connection the connection
876 * @param name the name
877 * @param error location to store any errors
878 * @returns #TRUE if the name exists, #FALSE if not or on error
881 dbus_bus_name_has_owner (DBusConnection *connection,
885 DBusMessage *message, *reply;
888 _dbus_return_val_if_fail (connection != NULL, FALSE);
889 _dbus_return_val_if_fail (name != NULL, FALSE);
890 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
891 _dbus_return_val_if_error_is_set (error, FALSE);
893 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
899 _DBUS_SET_OOM (error);
903 if (!dbus_message_append_args (message,
904 DBUS_TYPE_STRING, &name,
907 dbus_message_unref (message);
908 _DBUS_SET_OOM (error);
912 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
913 dbus_message_unref (message);
917 _DBUS_ASSERT_ERROR_IS_SET (error);
921 if (!dbus_message_get_args (reply, error,
922 DBUS_TYPE_BOOLEAN, &exists,
925 _DBUS_ASSERT_ERROR_IS_SET (error);
926 dbus_message_unref (reply);
930 dbus_message_unref (reply);
935 * Starts a service that will request ownership of the given name.
936 * The returned result will be one of be one of
937 * #DBUS_START_REPLY_SUCCESS or #DBUS_START_REPLY_ALREADY_RUNNING if
938 * successful. Pass #NULL if you don't care about the result.
940 * The flags parameter is for future expansion, currently you should
943 * @param connection the connection
944 * @param name the name we want the new service to request
945 * @param flags the flags (should always be 0 for now)
946 * @param result a place to store the result or #NULL
947 * @param error location to store any errors
948 * @returns #TRUE if the activation succeeded, #FALSE if not
951 dbus_bus_start_service_by_name (DBusConnection *connection,
954 dbus_uint32_t *result,
960 _dbus_return_val_if_fail (connection != NULL, FALSE);
961 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
963 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
966 "StartServiceByName");
968 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &name,
969 DBUS_TYPE_UINT32, &flags, DBUS_TYPE_INVALID))
971 dbus_message_unref (msg);
972 _DBUS_SET_OOM (error);
976 reply = dbus_connection_send_with_reply_and_block (connection, msg,
978 dbus_message_unref (msg);
982 _DBUS_ASSERT_ERROR_IS_SET (error);
986 if (dbus_set_error_from_message (error, reply))
988 _DBUS_ASSERT_ERROR_IS_SET (error);
989 dbus_message_unref (reply);
993 if (result != NULL &&
994 !dbus_message_get_args (reply, error, DBUS_TYPE_UINT32,
995 result, DBUS_TYPE_INVALID))
997 _DBUS_ASSERT_ERROR_IS_SET (error);
998 dbus_message_unref (reply);
1002 dbus_message_unref (reply);
1007 send_no_return_values (DBusConnection *connection,
1013 /* Block to check success codepath */
1016 reply = dbus_connection_send_with_reply_and_block (connection, msg,
1020 _DBUS_ASSERT_ERROR_IS_SET (error);
1022 dbus_message_unref (reply);
1026 /* Silently-fail nonblocking codepath */
1027 dbus_message_set_no_reply (msg, TRUE);
1028 dbus_connection_send (connection, msg, NULL);
1033 * Adds a match rule to match messages going through the message bus.
1034 * The "rule" argument is the string form of a match rule.
1036 * If you pass #NULL for the error, this function will not
1037 * block; the match thus won't be added until you flush the
1038 * connection, and if there's an error adding the match
1039 * (only possible error is lack of resources in the bus),
1040 * you won't find out about it.
1042 * If you pass non-#NULL for the error this function will
1043 * block until it gets a reply.
1045 * Normal API conventions would have the function return
1046 * a boolean value indicating whether the error was set,
1047 * but that would require blocking always to determine
1050 * The AddMatch method is fully documented in the D-Bus
1051 * specification. For quick reference, the format of the
1052 * match rules is discussed here, but the specification
1053 * is the canonical version of this information.
1055 * Rules are specified as a string of comma separated
1056 * key/value pairs. An example is
1057 * "type='signal',sender='org.freedesktop.DBus',
1058 * interface='org.freedesktop.DBus',member='Foo',
1059 * path='/bar/foo',destination=':452345.34'"
1061 * Possible keys you can match on are type, sender,
1062 * interface, member, path, destination and the special
1063 * arg keys. Excluding a key from the rule indicates
1064 * a wildcard match. For instance excluding the
1065 * the member from a match rule but adding a sender would
1066 * let all messages from that sender through.
1068 * Matches are inclusive not exclusive so as long as one
1069 * rule matches the message will get through. It is important
1070 * to note this because every time a message is received the
1071 * application will be paged into memory to process it. This
1072 * can cause performance problems such as draining batteries
1073 * on embedded platforms.
1075 * The special arg keys are used for further restricting the
1076 * match based on the parameters sent by the signal or method.
1077 * For instance arg1='foo' will check the first argument,
1078 * arg2='bar' the second and so on. For performance reasons
1079 * there is a set limit on the highest number parameter that
1080 * can be checked which is set in dbus-protocol.h
1082 * @param connection connection to the message bus
1083 * @param rule textual form of match rule
1084 * @param error location to store any errors
1087 dbus_bus_add_match (DBusConnection *connection,
1093 _dbus_return_if_fail (rule != NULL);
1095 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1097 DBUS_INTERFACE_DBUS,
1102 _DBUS_SET_OOM (error);
1106 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
1109 dbus_message_unref (msg);
1110 _DBUS_SET_OOM (error);
1114 send_no_return_values (connection, msg, error);
1116 dbus_message_unref (msg);
1120 * Removes a previously-added match rule "by value" (the most
1121 * recently-added identical rule gets removed). The "rule" argument
1122 * is the string form of a match rule.
1124 * If you pass #NULL for the error, this function will not
1125 * block; otherwise it will. See detailed explanation in
1126 * docs for dbus_bus_add_match().
1128 * @param connection connection to the message bus
1129 * @param rule textual form of match rule
1130 * @param error location to store any errors
1133 dbus_bus_remove_match (DBusConnection *connection,
1139 _dbus_return_if_fail (rule != NULL);
1141 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1143 DBUS_INTERFACE_DBUS,
1146 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
1149 dbus_message_unref (msg);
1150 _DBUS_SET_OOM (error);
1154 send_no_return_values (connection, msg, error);
1156 dbus_message_unref (msg);